Glenn Kasten | b3db213 | 2012-01-19 08:59:58 -0800 | [diff] [blame] | 1 | /* |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2 | ** |
| 3 | ** Copyright 2006, The Android Open Source Project |
| 4 | ** |
| 5 | ** Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | ** you may not use this file except in compliance with the License. |
| 7 | ** You may obtain a copy of the License at |
| 8 | ** |
| 9 | ** http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | ** |
| 11 | ** Unless required by applicable law or agreed to in writing, software |
| 12 | ** distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | ** See the License for the specific language governing permissions and |
| 15 | ** limitations under the License. |
| 16 | */ |
| 17 | |
Eric Laurent | 3a24199 | 2014-05-19 19:33:26 -0700 | [diff] [blame] | 18 | //#define LOG_NDEBUG 0 |
| 19 | |
Eric Laurent | b69681c | 2014-05-19 19:02:51 -0700 | [diff] [blame] | 20 | #define LOG_TAG "AudioSystem-JNI" |
Glenn Kasten | c81d31c | 2012-03-13 14:46:23 -0700 | [diff] [blame] | 21 | #include <utils/Log.h> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 22 | |
Andy Hung | 737d9fb | 2016-08-16 18:38:49 -0700 | [diff] [blame] | 23 | #include <sstream> |
jiabin | c0f4944 | 2018-01-05 10:23:50 -0800 | [diff] [blame] | 24 | #include <vector> |
Glenn Kasten | c81d31c | 2012-03-13 14:46:23 -0700 | [diff] [blame] | 25 | #include <jni.h> |
Steven Moreland | 2279b25 | 2017-07-19 09:50:45 -0700 | [diff] [blame] | 26 | #include <nativehelper/JNIHelp.h> |
Andreas Gampe | ed6b9df | 2014-11-20 22:02:20 -0800 | [diff] [blame] | 27 | #include "core_jni_helpers.h" |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 28 | |
| 29 | #include <media/AudioSystem.h> |
Eric Laurent | 7f5eb9f | 2014-12-01 19:36:30 -0800 | [diff] [blame] | 30 | #include <media/AudioPolicy.h> |
jiabin | c0f4944 | 2018-01-05 10:23:50 -0800 | [diff] [blame] | 31 | #include <media/MicrophoneInfo.h> |
Andy Hung | 737d9fb | 2016-08-16 18:38:49 -0700 | [diff] [blame] | 32 | #include <nativehelper/ScopedLocalRef.h> |
Dima Zavin | 34bb419 | 2011-05-11 14:15:23 -0700 | [diff] [blame] | 33 | #include <system/audio.h> |
Dima Zavin | 290029d | 2011-06-13 18:16:26 -0700 | [diff] [blame] | 34 | #include <system/audio_policy.h> |
Eric Laurent | c7a0cdf | 2018-11-30 12:16:52 -0800 | [diff] [blame] | 35 | #include "android_media_AudioEffectDescriptor.h" |
Eric Laurent | b69681c | 2014-05-19 19:02:51 -0700 | [diff] [blame] | 36 | #include "android_media_AudioFormat.h" |
| 37 | #include "android_media_AudioErrors.h" |
jiabin | c0f4944 | 2018-01-05 10:23:50 -0800 | [diff] [blame] | 38 | #include "android_media_MicrophoneInfo.h" |
François Gaffie | b469128 | 2018-07-09 13:07:32 +0200 | [diff] [blame^] | 39 | #include "android_media_AudioAttributes.h" |
Dima Zavin | 24fc2fb | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 40 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 41 | // ---------------------------------------------------------------------------- |
| 42 | |
| 43 | using namespace android; |
| 44 | |
Glenn Kasten | ed0079d | 2011-04-04 10:50:50 -0700 | [diff] [blame] | 45 | static const char* const kClassPathName = "android/media/AudioSystem"; |
| 46 | |
Eric Laurent | b69681c | 2014-05-19 19:02:51 -0700 | [diff] [blame] | 47 | static jclass gArrayListClass; |
| 48 | static struct { |
| 49 | jmethodID add; |
Eric Laurent | 7f5eb9f | 2014-12-01 19:36:30 -0800 | [diff] [blame] | 50 | jmethodID toArray; |
Eric Laurent | b69681c | 2014-05-19 19:02:51 -0700 | [diff] [blame] | 51 | } gArrayListMethods; |
| 52 | |
jiabin | 3994075 | 2018-04-02 18:18:45 -0700 | [diff] [blame] | 53 | static jclass gBooleanClass; |
| 54 | static jmethodID gBooleanCstor; |
| 55 | |
| 56 | static jclass gIntegerClass; |
| 57 | static jmethodID gIntegerCstor; |
| 58 | |
| 59 | static jclass gMapClass; |
| 60 | static jmethodID gMapPut; |
| 61 | |
Eric Laurent | b69681c | 2014-05-19 19:02:51 -0700 | [diff] [blame] | 62 | static jclass gAudioHandleClass; |
| 63 | static jmethodID gAudioHandleCstor; |
| 64 | static struct { |
| 65 | jfieldID mId; |
| 66 | } gAudioHandleFields; |
| 67 | |
| 68 | static jclass gAudioPortClass; |
| 69 | static jmethodID gAudioPortCstor; |
| 70 | static struct { |
| 71 | jfieldID mHandle; |
| 72 | jfieldID mRole; |
| 73 | jfieldID mGains; |
| 74 | jfieldID mActiveConfig; |
Hongwei Wang | f44a568 | 2018-08-24 10:37:56 -0700 | [diff] [blame] | 75 | // Valid only if an AudioDevicePort |
| 76 | jfieldID mType; |
| 77 | jfieldID mAddress; |
Eric Laurent | b69681c | 2014-05-19 19:02:51 -0700 | [diff] [blame] | 78 | // other fields unused by JNI |
| 79 | } gAudioPortFields; |
| 80 | |
| 81 | static jclass gAudioPortConfigClass; |
| 82 | static jmethodID gAudioPortConfigCstor; |
| 83 | static struct { |
| 84 | jfieldID mPort; |
| 85 | jfieldID mSamplingRate; |
| 86 | jfieldID mChannelMask; |
| 87 | jfieldID mFormat; |
| 88 | jfieldID mGain; |
Eric Laurent | 3a24199 | 2014-05-19 19:33:26 -0700 | [diff] [blame] | 89 | jfieldID mConfigMask; |
Eric Laurent | b69681c | 2014-05-19 19:02:51 -0700 | [diff] [blame] | 90 | } gAudioPortConfigFields; |
| 91 | |
| 92 | static jclass gAudioDevicePortClass; |
| 93 | static jmethodID gAudioDevicePortCstor; |
| 94 | |
| 95 | static jclass gAudioDevicePortConfigClass; |
| 96 | static jmethodID gAudioDevicePortConfigCstor; |
| 97 | |
| 98 | static jclass gAudioMixPortClass; |
| 99 | static jmethodID gAudioMixPortCstor; |
| 100 | |
| 101 | static jclass gAudioMixPortConfigClass; |
| 102 | static jmethodID gAudioMixPortConfigCstor; |
| 103 | |
| 104 | static jclass gAudioGainClass; |
| 105 | static jmethodID gAudioGainCstor; |
| 106 | |
| 107 | static jclass gAudioGainConfigClass; |
| 108 | static jmethodID gAudioGainConfigCstor; |
| 109 | static struct { |
| 110 | jfieldID mIndex; |
| 111 | jfieldID mMode; |
| 112 | jfieldID mChannelMask; |
| 113 | jfieldID mValues; |
| 114 | jfieldID mRampDurationMs; |
| 115 | // other fields unused by JNI |
| 116 | } gAudioGainConfigFields; |
| 117 | |
| 118 | static jclass gAudioPatchClass; |
| 119 | static jmethodID gAudioPatchCstor; |
| 120 | static struct { |
| 121 | jfieldID mHandle; |
| 122 | // other fields unused by JNI |
| 123 | } gAudioPatchFields; |
| 124 | |
Eric Laurent | 7f5eb9f | 2014-12-01 19:36:30 -0800 | [diff] [blame] | 125 | static jclass gAudioMixClass; |
| 126 | static struct { |
| 127 | jfieldID mRule; |
| 128 | jfieldID mFormat; |
| 129 | jfieldID mRouteFlags; |
Jean-Michel Trivi | 4ad3988 | 2016-03-14 18:46:41 -0700 | [diff] [blame] | 130 | jfieldID mDeviceType; |
| 131 | jfieldID mDeviceAddress; |
Eric Laurent | 7f5eb9f | 2014-12-01 19:36:30 -0800 | [diff] [blame] | 132 | jfieldID mMixType; |
Jean-Michel Trivi | 5a56109 | 2015-04-23 18:48:08 -0700 | [diff] [blame] | 133 | jfieldID mCallbackFlags; |
Eric Laurent | 7f5eb9f | 2014-12-01 19:36:30 -0800 | [diff] [blame] | 134 | } gAudioMixFields; |
| 135 | |
| 136 | static jclass gAudioFormatClass; |
| 137 | static struct { |
| 138 | jfieldID mEncoding; |
| 139 | jfieldID mSampleRate; |
| 140 | jfieldID mChannelMask; |
| 141 | // other fields unused by JNI |
| 142 | } gAudioFormatFields; |
| 143 | |
| 144 | static jclass gAudioMixingRuleClass; |
| 145 | static struct { |
| 146 | jfieldID mCriteria; |
| 147 | // other fields unused by JNI |
| 148 | } gAudioMixingRuleFields; |
| 149 | |
Jean-Michel Trivi | ba6b4b4 | 2016-02-10 14:26:35 -0800 | [diff] [blame] | 150 | static jclass gAudioMixMatchCriterionClass; |
Eric Laurent | 7f5eb9f | 2014-12-01 19:36:30 -0800 | [diff] [blame] | 151 | static struct { |
| 152 | jfieldID mAttr; |
Jean-Michel Trivi | ba6b4b4 | 2016-02-10 14:26:35 -0800 | [diff] [blame] | 153 | jfieldID mIntProp; |
Eric Laurent | 7f5eb9f | 2014-12-01 19:36:30 -0800 | [diff] [blame] | 154 | jfieldID mRule; |
Jean-Michel Trivi | ba6b4b4 | 2016-02-10 14:26:35 -0800 | [diff] [blame] | 155 | } gAudioMixMatchCriterionFields; |
Eric Laurent | 7f5eb9f | 2014-12-01 19:36:30 -0800 | [diff] [blame] | 156 | |
Eric Laurent | 700e734 | 2014-05-02 18:33:15 -0700 | [diff] [blame] | 157 | static const char* const kEventHandlerClassPathName = |
| 158 | "android/media/AudioPortEventHandler"; |
Eric Laurent | 2615afb | 2015-03-25 14:51:08 -0700 | [diff] [blame] | 159 | static struct { |
| 160 | jfieldID mJniCallback; |
| 161 | } gEventHandlerFields; |
| 162 | static struct { |
| 163 | jmethodID postEventFromNative; |
| 164 | } gAudioPortEventHandlerMethods; |
| 165 | |
Jean-Michel Trivi | 5a56109 | 2015-04-23 18:48:08 -0700 | [diff] [blame] | 166 | static struct { |
| 167 | jmethodID postDynPolicyEventFromNative; |
Jean-Michel Trivi | d3c71f0 | 2015-12-07 11:59:31 -0800 | [diff] [blame] | 168 | jmethodID postRecordConfigEventFromNative; |
| 169 | } gAudioPolicyEventHandlerMethods; |
Jean-Michel Trivi | 5a56109 | 2015-04-23 18:48:08 -0700 | [diff] [blame] | 170 | |
Paul McLean | b3ae6d9 | 2018-11-05 15:16:59 -0700 | [diff] [blame] | 171 | // |
| 172 | // JNI Initialization for OpenSLES routing |
| 173 | // |
| 174 | jmethodID gMidAudioTrackRoutingProxy_ctor; |
| 175 | jmethodID gMidAudioTrackRoutingProxy_release; |
| 176 | jmethodID gMidAudioRecordRoutingProxy_ctor; |
| 177 | jmethodID gMidAudioRecordRoutingProxy_release; |
| 178 | |
| 179 | jclass gClsAudioTrackRoutingProxy; |
| 180 | jclass gClsAudioRecordRoutingProxy; |
| 181 | |
Eric Laurent | 2615afb | 2015-03-25 14:51:08 -0700 | [diff] [blame] | 182 | static Mutex gLock; |
Eric Laurent | b69681c | 2014-05-19 19:02:51 -0700 | [diff] [blame] | 183 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 184 | enum AudioError { |
| 185 | kAudioStatusOk = 0, |
| 186 | kAudioStatusError = 1, |
| 187 | kAudioStatusMediaServerDied = 100 |
| 188 | }; |
| 189 | |
Eric Laurent | 700e734 | 2014-05-02 18:33:15 -0700 | [diff] [blame] | 190 | enum { |
| 191 | AUDIOPORT_EVENT_PORT_LIST_UPDATED = 1, |
| 192 | AUDIOPORT_EVENT_PATCH_LIST_UPDATED = 2, |
| 193 | AUDIOPORT_EVENT_SERVICE_DIED = 3, |
| 194 | }; |
| 195 | |
Eric Laurent | b69681c | 2014-05-19 19:02:51 -0700 | [diff] [blame] | 196 | #define MAX_PORT_GENERATION_SYNC_ATTEMPTS 5 |
| 197 | |
Eric Laurent | 700e734 | 2014-05-02 18:33:15 -0700 | [diff] [blame] | 198 | // ---------------------------------------------------------------------------- |
Jean-Michel Trivi | 5a56109 | 2015-04-23 18:48:08 -0700 | [diff] [blame] | 199 | // ref-counted object for audio port callbacks |
Eric Laurent | 700e734 | 2014-05-02 18:33:15 -0700 | [diff] [blame] | 200 | class JNIAudioPortCallback: public AudioSystem::AudioPortCallback |
| 201 | { |
| 202 | public: |
| 203 | JNIAudioPortCallback(JNIEnv* env, jobject thiz, jobject weak_thiz); |
| 204 | ~JNIAudioPortCallback(); |
| 205 | |
| 206 | virtual void onAudioPortListUpdate(); |
| 207 | virtual void onAudioPatchListUpdate(); |
| 208 | virtual void onServiceDied(); |
| 209 | |
| 210 | private: |
| 211 | void sendEvent(int event); |
| 212 | |
Eric Laurent | 2615afb | 2015-03-25 14:51:08 -0700 | [diff] [blame] | 213 | jclass mClass; // Reference to AudioPortEventHandler class |
| 214 | jobject mObject; // Weak ref to AudioPortEventHandler Java object to call on |
Eric Laurent | 700e734 | 2014-05-02 18:33:15 -0700 | [diff] [blame] | 215 | }; |
| 216 | |
| 217 | JNIAudioPortCallback::JNIAudioPortCallback(JNIEnv* env, jobject thiz, jobject weak_thiz) |
| 218 | { |
| 219 | |
Eric Laurent | 2615afb | 2015-03-25 14:51:08 -0700 | [diff] [blame] | 220 | // Hold onto the AudioPortEventHandler class for use in calling the static method |
Eric Laurent | 700e734 | 2014-05-02 18:33:15 -0700 | [diff] [blame] | 221 | // that posts events to the application thread. |
| 222 | jclass clazz = env->GetObjectClass(thiz); |
| 223 | if (clazz == NULL) { |
| 224 | ALOGE("Can't find class %s", kEventHandlerClassPathName); |
| 225 | return; |
| 226 | } |
| 227 | mClass = (jclass)env->NewGlobalRef(clazz); |
| 228 | |
Eric Laurent | 2615afb | 2015-03-25 14:51:08 -0700 | [diff] [blame] | 229 | // We use a weak reference so the AudioPortEventHandler object can be garbage collected. |
Eric Laurent | 700e734 | 2014-05-02 18:33:15 -0700 | [diff] [blame] | 230 | // The reference is only used as a proxy for callbacks. |
| 231 | mObject = env->NewGlobalRef(weak_thiz); |
| 232 | } |
| 233 | |
| 234 | JNIAudioPortCallback::~JNIAudioPortCallback() |
| 235 | { |
| 236 | // remove global references |
| 237 | JNIEnv *env = AndroidRuntime::getJNIEnv(); |
Eric Laurent | 449c588 | 2014-11-12 11:07:48 -0800 | [diff] [blame] | 238 | if (env == NULL) { |
| 239 | return; |
| 240 | } |
Eric Laurent | 700e734 | 2014-05-02 18:33:15 -0700 | [diff] [blame] | 241 | env->DeleteGlobalRef(mObject); |
| 242 | env->DeleteGlobalRef(mClass); |
| 243 | } |
| 244 | |
| 245 | void JNIAudioPortCallback::sendEvent(int event) |
| 246 | { |
| 247 | JNIEnv *env = AndroidRuntime::getJNIEnv(); |
Eric Laurent | 449c588 | 2014-11-12 11:07:48 -0800 | [diff] [blame] | 248 | if (env == NULL) { |
| 249 | return; |
| 250 | } |
Eric Laurent | 2615afb | 2015-03-25 14:51:08 -0700 | [diff] [blame] | 251 | env->CallStaticVoidMethod(mClass, gAudioPortEventHandlerMethods.postEventFromNative, mObject, |
Eric Laurent | 700e734 | 2014-05-02 18:33:15 -0700 | [diff] [blame] | 252 | event, 0, 0, NULL); |
| 253 | if (env->ExceptionCheck()) { |
| 254 | ALOGW("An exception occurred while notifying an event."); |
| 255 | env->ExceptionClear(); |
| 256 | } |
| 257 | } |
| 258 | |
| 259 | void JNIAudioPortCallback::onAudioPortListUpdate() |
| 260 | { |
| 261 | sendEvent(AUDIOPORT_EVENT_PORT_LIST_UPDATED); |
| 262 | } |
| 263 | |
| 264 | void JNIAudioPortCallback::onAudioPatchListUpdate() |
| 265 | { |
| 266 | sendEvent(AUDIOPORT_EVENT_PATCH_LIST_UPDATED); |
| 267 | } |
| 268 | |
| 269 | void JNIAudioPortCallback::onServiceDied() |
| 270 | { |
| 271 | sendEvent(AUDIOPORT_EVENT_SERVICE_DIED); |
| 272 | } |
| 273 | |
Eric Laurent | 2615afb | 2015-03-25 14:51:08 -0700 | [diff] [blame] | 274 | static sp<JNIAudioPortCallback> setJniCallback(JNIEnv* env, |
| 275 | jobject thiz, |
| 276 | const sp<JNIAudioPortCallback>& callback) |
| 277 | { |
| 278 | Mutex::Autolock l(gLock); |
| 279 | sp<JNIAudioPortCallback> old = |
| 280 | (JNIAudioPortCallback*)env->GetLongField(thiz, gEventHandlerFields.mJniCallback); |
| 281 | if (callback.get()) { |
| 282 | callback->incStrong((void*)setJniCallback); |
| 283 | } |
| 284 | if (old != 0) { |
| 285 | old->decStrong((void*)setJniCallback); |
| 286 | } |
| 287 | env->SetLongField(thiz, gEventHandlerFields.mJniCallback, (jlong)callback.get()); |
| 288 | return old; |
| 289 | } |
| 290 | |
Mikhail Naganov | 5caa57d | 2018-04-02 10:35:45 -0700 | [diff] [blame] | 291 | #define check_AudioSystem_Command(status) _check_AudioSystem_Command(__func__, (status)) |
| 292 | |
| 293 | static int _check_AudioSystem_Command(const char* caller, status_t status) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 294 | { |
Mikhail Naganov | 5caa57d | 2018-04-02 10:35:45 -0700 | [diff] [blame] | 295 | ALOGE_IF(status, "Command failed for %s: %d", caller, status); |
Eric Laurent | dfb881f | 2013-07-18 14:41:39 -0700 | [diff] [blame] | 296 | switch (status) { |
| 297 | case DEAD_OBJECT: |
| 298 | return kAudioStatusMediaServerDied; |
| 299 | case NO_ERROR: |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 300 | return kAudioStatusOk; |
Eric Laurent | dfb881f | 2013-07-18 14:41:39 -0700 | [diff] [blame] | 301 | default: |
| 302 | break; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 303 | } |
Eric Laurent | dfb881f | 2013-07-18 14:41:39 -0700 | [diff] [blame] | 304 | return kAudioStatusError; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 305 | } |
| 306 | |
Ashok Bhat | 075e9a1 | 2014-01-06 13:45:09 +0000 | [diff] [blame] | 307 | static jint |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 308 | android_media_AudioSystem_muteMicrophone(JNIEnv *env, jobject thiz, jboolean on) |
| 309 | { |
Ashok Bhat | 075e9a1 | 2014-01-06 13:45:09 +0000 | [diff] [blame] | 310 | return (jint) check_AudioSystem_Command(AudioSystem::muteMicrophone(on)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 311 | } |
| 312 | |
| 313 | static jboolean |
| 314 | android_media_AudioSystem_isMicrophoneMuted(JNIEnv *env, jobject thiz) |
| 315 | { |
| 316 | bool state = false; |
| 317 | AudioSystem::isMicrophoneMuted(&state); |
| 318 | return state; |
| 319 | } |
| 320 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 321 | static jboolean |
Eric Laurent | 25101b0 | 2011-02-02 09:33:30 -0800 | [diff] [blame] | 322 | android_media_AudioSystem_isStreamActive(JNIEnv *env, jobject thiz, jint stream, jint inPastMs) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 323 | { |
| 324 | bool state = false; |
Glenn Kasten | bc1d77b | 2012-01-12 16:38:12 -0800 | [diff] [blame] | 325 | AudioSystem::isStreamActive((audio_stream_type_t) stream, &state, inPastMs); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 326 | return state; |
| 327 | } |
| 328 | |
Jean-Michel Trivi | d677054 | 2012-10-10 12:03:41 -0700 | [diff] [blame] | 329 | static jboolean |
Jean-Michel Trivi | 679d504 | 2013-02-04 16:24:09 -0800 | [diff] [blame] | 330 | android_media_AudioSystem_isStreamActiveRemotely(JNIEnv *env, jobject thiz, jint stream, |
| 331 | jint inPastMs) |
| 332 | { |
| 333 | bool state = false; |
| 334 | AudioSystem::isStreamActiveRemotely((audio_stream_type_t) stream, &state, inPastMs); |
| 335 | return state; |
| 336 | } |
| 337 | |
| 338 | static jboolean |
Jean-Michel Trivi | d677054 | 2012-10-10 12:03:41 -0700 | [diff] [blame] | 339 | android_media_AudioSystem_isSourceActive(JNIEnv *env, jobject thiz, jint source) |
| 340 | { |
| 341 | bool state = false; |
| 342 | AudioSystem::isSourceActive((audio_source_t) source, &state); |
| 343 | return state; |
| 344 | } |
| 345 | |
Ashok Bhat | 075e9a1 | 2014-01-06 13:45:09 +0000 | [diff] [blame] | 346 | static jint |
Jean-Michel Trivi | 8a21f5d | 2014-06-05 15:03:52 -0700 | [diff] [blame] | 347 | android_media_AudioSystem_newAudioSessionId(JNIEnv *env, jobject thiz) |
| 348 | { |
Glenn Kasten | 211d905 | 2016-02-26 16:22:40 -0800 | [diff] [blame] | 349 | return AudioSystem::newAudioUniqueId(AUDIO_UNIQUE_ID_USE_SESSION); |
Jean-Michel Trivi | 8a21f5d | 2014-06-05 15:03:52 -0700 | [diff] [blame] | 350 | } |
| 351 | |
| 352 | static jint |
Jean-Michel Trivi | 292a6a4 | 2016-12-01 08:32:15 -0800 | [diff] [blame] | 353 | android_media_AudioSystem_newAudioPlayerId(JNIEnv *env, jobject thiz) |
| 354 | { |
| 355 | return AudioSystem::newAudioUniqueId(AUDIO_UNIQUE_ID_USE_PLAYER); |
| 356 | } |
| 357 | |
| 358 | static jint |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 359 | android_media_AudioSystem_setParameters(JNIEnv *env, jobject thiz, jstring keyValuePairs) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 360 | { |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 361 | const jchar* c_keyValuePairs = env->GetStringCritical(keyValuePairs, 0); |
| 362 | String8 c_keyValuePairs8; |
| 363 | if (keyValuePairs) { |
Dan Albert | 6698749 | 2014-11-20 11:41:21 -0800 | [diff] [blame] | 364 | c_keyValuePairs8 = String8( |
| 365 | reinterpret_cast<const char16_t*>(c_keyValuePairs), |
| 366 | env->GetStringLength(keyValuePairs)); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 367 | env->ReleaseStringCritical(keyValuePairs, c_keyValuePairs); |
| 368 | } |
Glenn Kasten | c94752a | 2013-12-19 12:40:33 -0800 | [diff] [blame] | 369 | int status = check_AudioSystem_Command(AudioSystem::setParameters(c_keyValuePairs8)); |
Ashok Bhat | 075e9a1 | 2014-01-06 13:45:09 +0000 | [diff] [blame] | 370 | return (jint) status; |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 371 | } |
| 372 | |
| 373 | static jstring |
| 374 | android_media_AudioSystem_getParameters(JNIEnv *env, jobject thiz, jstring keys) |
| 375 | { |
| 376 | const jchar* c_keys = env->GetStringCritical(keys, 0); |
| 377 | String8 c_keys8; |
| 378 | if (keys) { |
Dan Albert | 6698749 | 2014-11-20 11:41:21 -0800 | [diff] [blame] | 379 | c_keys8 = String8(reinterpret_cast<const char16_t*>(c_keys), |
| 380 | env->GetStringLength(keys)); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 381 | env->ReleaseStringCritical(keys, c_keys); |
| 382 | } |
Glenn Kasten | c94752a | 2013-12-19 12:40:33 -0800 | [diff] [blame] | 383 | return env->NewStringUTF(AudioSystem::getParameters(c_keys8).string()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 384 | } |
| 385 | |
Glenn Kasten | ed0079d | 2011-04-04 10:50:50 -0700 | [diff] [blame] | 386 | static void |
| 387 | android_media_AudioSystem_error_callback(status_t err) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 388 | { |
Eric Laurent | 539719a | 2013-07-18 14:08:00 -0700 | [diff] [blame] | 389 | JNIEnv *env = AndroidRuntime::getJNIEnv(); |
Eric Laurent | 0e7a4d9 | 2010-10-15 12:33:16 -0700 | [diff] [blame] | 390 | if (env == NULL) { |
| 391 | return; |
| 392 | } |
| 393 | |
Glenn Kasten | ed0079d | 2011-04-04 10:50:50 -0700 | [diff] [blame] | 394 | jclass clazz = env->FindClass(kClassPathName); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 395 | |
Eric Laurent | dfb881f | 2013-07-18 14:41:39 -0700 | [diff] [blame] | 396 | env->CallStaticVoidMethod(clazz, env->GetStaticMethodID(clazz, |
| 397 | "errorCallbackFromNative","(I)V"), |
| 398 | check_AudioSystem_Command(err)); |
Viorel Suman | b637ccf | 2014-05-30 16:51:07 +0300 | [diff] [blame] | 399 | |
| 400 | env->DeleteLocalRef(clazz); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 401 | } |
| 402 | |
Jean-Michel Trivi | 5a56109 | 2015-04-23 18:48:08 -0700 | [diff] [blame] | 403 | static void |
| 404 | android_media_AudioSystem_dyn_policy_callback(int event, String8 regId, int val) |
| 405 | { |
| 406 | JNIEnv *env = AndroidRuntime::getJNIEnv(); |
| 407 | if (env == NULL) { |
| 408 | return; |
| 409 | } |
| 410 | |
| 411 | jclass clazz = env->FindClass(kClassPathName); |
| 412 | const char* zechars = regId.string(); |
| 413 | jstring zestring = env->NewStringUTF(zechars); |
| 414 | |
Jean-Michel Trivi | d3c71f0 | 2015-12-07 11:59:31 -0800 | [diff] [blame] | 415 | env->CallStaticVoidMethod(clazz, gAudioPolicyEventHandlerMethods.postDynPolicyEventFromNative, |
Jean-Michel Trivi | 5a56109 | 2015-04-23 18:48:08 -0700 | [diff] [blame] | 416 | event, zestring, val); |
| 417 | |
| 418 | env->ReleaseStringUTFChars(zestring, zechars); |
| 419 | env->DeleteLocalRef(clazz); |
Jean-Michel Trivi | d3c71f0 | 2015-12-07 11:59:31 -0800 | [diff] [blame] | 420 | } |
Jean-Michel Trivi | 5a56109 | 2015-04-23 18:48:08 -0700 | [diff] [blame] | 421 | |
Jean-Michel Trivi | d3c71f0 | 2015-12-07 11:59:31 -0800 | [diff] [blame] | 422 | static void |
Eric Laurent | c7a0cdf | 2018-11-30 12:16:52 -0800 | [diff] [blame] | 423 | android_media_AudioSystem_recording_callback(int event, |
| 424 | const record_client_info_t *clientInfo, |
| 425 | const audio_config_base_t *clientConfig, |
| 426 | std::vector<effect_descriptor_t> clientEffects, |
| 427 | const audio_config_base_t *deviceConfig, |
| 428 | std::vector<effect_descriptor_t> effects __unused, |
| 429 | audio_patch_handle_t patchHandle, |
| 430 | audio_source_t source) |
Jean-Michel Trivi | d3c71f0 | 2015-12-07 11:59:31 -0800 | [diff] [blame] | 431 | { |
| 432 | JNIEnv *env = AndroidRuntime::getJNIEnv(); |
| 433 | if (env == NULL) { |
| 434 | return; |
| 435 | } |
Jean-Michel Trivi | 66ffacf | 2017-02-04 17:25:31 -0800 | [diff] [blame] | 436 | if (clientInfo == NULL || clientConfig == NULL || deviceConfig == NULL) { |
| 437 | ALOGE("Unexpected null client/device info or configurations in recording callback"); |
Jean-Michel Trivi | 33fd816 | 2016-02-22 10:52:41 -0800 | [diff] [blame] | 438 | return; |
| 439 | } |
Jean-Michel Trivi | d3c71f0 | 2015-12-07 11:59:31 -0800 | [diff] [blame] | 440 | |
Jean-Michel Trivi | 33fd816 | 2016-02-22 10:52:41 -0800 | [diff] [blame] | 441 | // create an array for 2*3 integers to store the record configurations (client + device) |
Jean-Michel Trivi | 8ab7280 | 2016-02-25 16:31:45 -0800 | [diff] [blame] | 442 | // plus 1 integer for the patch handle |
| 443 | const int REC_PARAM_SIZE = 7; |
| 444 | jintArray recParamArray = env->NewIntArray(REC_PARAM_SIZE); |
Jean-Michel Trivi | 33fd816 | 2016-02-22 10:52:41 -0800 | [diff] [blame] | 445 | if (recParamArray == NULL) { |
| 446 | ALOGE("recording callback: Couldn't allocate int array for configuration data"); |
| 447 | return; |
| 448 | } |
Jean-Michel Trivi | 8ab7280 | 2016-02-25 16:31:45 -0800 | [diff] [blame] | 449 | jint recParamData[REC_PARAM_SIZE]; |
Jean-Michel Trivi | 33fd816 | 2016-02-22 10:52:41 -0800 | [diff] [blame] | 450 | recParamData[0] = (jint) audioFormatFromNative(clientConfig->format); |
| 451 | // FIXME this doesn't support index-based masks |
| 452 | recParamData[1] = (jint) inChannelMaskFromNative(clientConfig->channel_mask); |
| 453 | recParamData[2] = (jint) clientConfig->sample_rate; |
| 454 | recParamData[3] = (jint) audioFormatFromNative(deviceConfig->format); |
| 455 | // FIXME this doesn't support index-based masks |
| 456 | recParamData[4] = (jint) inChannelMaskFromNative(deviceConfig->channel_mask); |
| 457 | recParamData[5] = (jint) deviceConfig->sample_rate; |
Jean-Michel Trivi | 8ab7280 | 2016-02-25 16:31:45 -0800 | [diff] [blame] | 458 | recParamData[6] = (jint) patchHandle; |
| 459 | env->SetIntArrayRegion(recParamArray, 0, REC_PARAM_SIZE, recParamData); |
Jean-Michel Trivi | 33fd816 | 2016-02-22 10:52:41 -0800 | [diff] [blame] | 460 | |
Eric Laurent | c7a0cdf | 2018-11-30 12:16:52 -0800 | [diff] [blame] | 461 | jobjectArray jClientEffects; |
| 462 | convertAudioEffectDescriptorVectorFromNative(env, &jClientEffects, clientEffects); |
| 463 | |
| 464 | jobjectArray jEffects; |
| 465 | convertAudioEffectDescriptorVectorFromNative(env, &jEffects, effects); |
| 466 | |
Jean-Michel Trivi | 33fd816 | 2016-02-22 10:52:41 -0800 | [diff] [blame] | 467 | // callback into java |
Jean-Michel Trivi | d3c71f0 | 2015-12-07 11:59:31 -0800 | [diff] [blame] | 468 | jclass clazz = env->FindClass(kClassPathName); |
Jean-Michel Trivi | 33fd816 | 2016-02-22 10:52:41 -0800 | [diff] [blame] | 469 | |
Eric Laurent | c7a0cdf | 2018-11-30 12:16:52 -0800 | [diff] [blame] | 470 | env->CallStaticVoidMethod(clazz, |
| 471 | gAudioPolicyEventHandlerMethods.postRecordConfigEventFromNative, |
| 472 | event, (jint) clientInfo->uid, clientInfo->session, |
| 473 | clientInfo->source, clientInfo->port_id, clientInfo->silenced, |
| 474 | recParamArray, jClientEffects, jEffects, source); |
| 475 | env->DeleteLocalRef(clazz); |
Jean-Michel Trivi | 33fd816 | 2016-02-22 10:52:41 -0800 | [diff] [blame] | 476 | env->DeleteLocalRef(recParamArray); |
Eric Laurent | c7a0cdf | 2018-11-30 12:16:52 -0800 | [diff] [blame] | 477 | env->DeleteLocalRef(jClientEffects); |
| 478 | env->DeleteLocalRef(jEffects); |
Jean-Michel Trivi | 5a56109 | 2015-04-23 18:48:08 -0700 | [diff] [blame] | 479 | } |
| 480 | |
Ashok Bhat | 075e9a1 | 2014-01-06 13:45:09 +0000 | [diff] [blame] | 481 | static jint |
Aniket Kumar Lata | 9fbc205 | 2019-01-11 02:02:33 -0800 | [diff] [blame] | 482 | android_media_AudioSystem_setDeviceConnectionState(JNIEnv *env, jobject thiz, jint device, jint state, jstring device_address, jstring device_name, |
Eric Laurent | 1d43aec | 2019-01-16 18:24:08 -0800 | [diff] [blame] | 483 | jint codec) |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 484 | { |
| 485 | const char *c_address = env->GetStringUTFChars(device_address, NULL); |
Paul McLean | 10804eb | 2015-01-28 11:16:35 -0800 | [diff] [blame] | 486 | const char *c_name = env->GetStringUTFChars(device_name, NULL); |
Dima Zavin | 24fc2fb | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 487 | int status = check_AudioSystem_Command(AudioSystem::setDeviceConnectionState(static_cast <audio_devices_t>(device), |
| 488 | static_cast <audio_policy_dev_state_t>(state), |
Eric Laurent | 1d43aec | 2019-01-16 18:24:08 -0800 | [diff] [blame] | 489 | c_address, c_name, |
| 490 | static_cast <audio_format_t>(codec))); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 491 | env->ReleaseStringUTFChars(device_address, c_address); |
Paul McLean | 10804eb | 2015-01-28 11:16:35 -0800 | [diff] [blame] | 492 | env->ReleaseStringUTFChars(device_name, c_name); |
Ashok Bhat | 075e9a1 | 2014-01-06 13:45:09 +0000 | [diff] [blame] | 493 | return (jint) status; |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 494 | } |
| 495 | |
Ashok Bhat | 075e9a1 | 2014-01-06 13:45:09 +0000 | [diff] [blame] | 496 | static jint |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 497 | android_media_AudioSystem_getDeviceConnectionState(JNIEnv *env, jobject thiz, jint device, jstring device_address) |
| 498 | { |
| 499 | const char *c_address = env->GetStringUTFChars(device_address, NULL); |
Dima Zavin | 24fc2fb | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 500 | int state = static_cast <int>(AudioSystem::getDeviceConnectionState(static_cast <audio_devices_t>(device), |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 501 | c_address)); |
| 502 | env->ReleaseStringUTFChars(device_address, c_address); |
Ashok Bhat | 075e9a1 | 2014-01-06 13:45:09 +0000 | [diff] [blame] | 503 | return (jint) state; |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 504 | } |
| 505 | |
Ashok Bhat | 075e9a1 | 2014-01-06 13:45:09 +0000 | [diff] [blame] | 506 | static jint |
Aniket Kumar Lata | 9fbc205 | 2019-01-11 02:02:33 -0800 | [diff] [blame] | 507 | android_media_AudioSystem_handleDeviceConfigChange(JNIEnv *env, jobject thiz, jint device, jstring device_address, jstring device_name, |
Eric Laurent | 1d43aec | 2019-01-16 18:24:08 -0800 | [diff] [blame] | 508 | jint codec) |
Pavlin Radoslavov | 44a4ef0 | 2016-12-21 12:05:51 -0800 | [diff] [blame] | 509 | { |
| 510 | const char *c_address = env->GetStringUTFChars(device_address, NULL); |
| 511 | const char *c_name = env->GetStringUTFChars(device_name, NULL); |
| 512 | int status = check_AudioSystem_Command(AudioSystem::handleDeviceConfigChange(static_cast <audio_devices_t>(device), |
Eric Laurent | 1d43aec | 2019-01-16 18:24:08 -0800 | [diff] [blame] | 513 | c_address, c_name, static_cast <audio_format_t>(codec))); |
Pavlin Radoslavov | 44a4ef0 | 2016-12-21 12:05:51 -0800 | [diff] [blame] | 514 | env->ReleaseStringUTFChars(device_address, c_address); |
| 515 | env->ReleaseStringUTFChars(device_name, c_name); |
| 516 | return (jint) status; |
| 517 | } |
| 518 | |
| 519 | static jint |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 520 | android_media_AudioSystem_setPhoneState(JNIEnv *env, jobject thiz, jint state) |
| 521 | { |
Ashok Bhat | 075e9a1 | 2014-01-06 13:45:09 +0000 | [diff] [blame] | 522 | return (jint) check_AudioSystem_Command(AudioSystem::setPhoneState((audio_mode_t) state)); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 523 | } |
| 524 | |
Ashok Bhat | 075e9a1 | 2014-01-06 13:45:09 +0000 | [diff] [blame] | 525 | static jint |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 526 | android_media_AudioSystem_setForceUse(JNIEnv *env, jobject thiz, jint usage, jint config) |
| 527 | { |
Ashok Bhat | 075e9a1 | 2014-01-06 13:45:09 +0000 | [diff] [blame] | 528 | return (jint) check_AudioSystem_Command(AudioSystem::setForceUse(static_cast <audio_policy_force_use_t>(usage), |
Dima Zavin | 24fc2fb | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 529 | static_cast <audio_policy_forced_cfg_t>(config))); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 530 | } |
| 531 | |
Ashok Bhat | 075e9a1 | 2014-01-06 13:45:09 +0000 | [diff] [blame] | 532 | static jint |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 533 | android_media_AudioSystem_getForceUse(JNIEnv *env, jobject thiz, jint usage) |
| 534 | { |
Ashok Bhat | 075e9a1 | 2014-01-06 13:45:09 +0000 | [diff] [blame] | 535 | return static_cast <jint>(AudioSystem::getForceUse(static_cast <audio_policy_force_use_t>(usage))); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 536 | } |
| 537 | |
Ashok Bhat | 075e9a1 | 2014-01-06 13:45:09 +0000 | [diff] [blame] | 538 | static jint |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 539 | android_media_AudioSystem_initStreamVolume(JNIEnv *env, jobject thiz, jint stream, jint indexMin, jint indexMax) |
| 540 | { |
Ashok Bhat | 075e9a1 | 2014-01-06 13:45:09 +0000 | [diff] [blame] | 541 | return (jint) check_AudioSystem_Command(AudioSystem::initStreamVolume(static_cast <audio_stream_type_t>(stream), |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 542 | indexMin, |
| 543 | indexMax)); |
| 544 | } |
| 545 | |
Ashok Bhat | 075e9a1 | 2014-01-06 13:45:09 +0000 | [diff] [blame] | 546 | static jint |
Eric Laurent | 9bc8358d | 2011-11-18 16:43:31 -0800 | [diff] [blame] | 547 | android_media_AudioSystem_setStreamVolumeIndex(JNIEnv *env, |
| 548 | jobject thiz, |
| 549 | jint stream, |
| 550 | jint index, |
| 551 | jint device) |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 552 | { |
Ashok Bhat | 075e9a1 | 2014-01-06 13:45:09 +0000 | [diff] [blame] | 553 | return (jint) check_AudioSystem_Command( |
Eric Laurent | 9bc8358d | 2011-11-18 16:43:31 -0800 | [diff] [blame] | 554 | AudioSystem::setStreamVolumeIndex(static_cast <audio_stream_type_t>(stream), |
| 555 | index, |
| 556 | (audio_devices_t)device)); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 557 | } |
| 558 | |
Ashok Bhat | 075e9a1 | 2014-01-06 13:45:09 +0000 | [diff] [blame] | 559 | static jint |
Eric Laurent | 9bc8358d | 2011-11-18 16:43:31 -0800 | [diff] [blame] | 560 | android_media_AudioSystem_getStreamVolumeIndex(JNIEnv *env, |
| 561 | jobject thiz, |
| 562 | jint stream, |
| 563 | jint device) |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 564 | { |
| 565 | int index; |
Eric Laurent | 9bc8358d | 2011-11-18 16:43:31 -0800 | [diff] [blame] | 566 | if (AudioSystem::getStreamVolumeIndex(static_cast <audio_stream_type_t>(stream), |
| 567 | &index, |
| 568 | (audio_devices_t)device) |
| 569 | != NO_ERROR) { |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 570 | index = -1; |
| 571 | } |
Ashok Bhat | 075e9a1 | 2014-01-06 13:45:09 +0000 | [diff] [blame] | 572 | return (jint) index; |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 573 | } |
| 574 | |
Ashok Bhat | 075e9a1 | 2014-01-06 13:45:09 +0000 | [diff] [blame] | 575 | static jint |
Mike Lockwood | cbdb49d | 2011-10-20 12:54:05 -0400 | [diff] [blame] | 576 | android_media_AudioSystem_setMasterVolume(JNIEnv *env, jobject thiz, jfloat value) |
| 577 | { |
Ashok Bhat | 075e9a1 | 2014-01-06 13:45:09 +0000 | [diff] [blame] | 578 | return (jint) check_AudioSystem_Command(AudioSystem::setMasterVolume(value)); |
Mike Lockwood | cbdb49d | 2011-10-20 12:54:05 -0400 | [diff] [blame] | 579 | } |
| 580 | |
| 581 | static jfloat |
| 582 | android_media_AudioSystem_getMasterVolume(JNIEnv *env, jobject thiz) |
| 583 | { |
| 584 | float value; |
| 585 | if (AudioSystem::getMasterVolume(&value) != NO_ERROR) { |
| 586 | value = -1.0; |
| 587 | } |
| 588 | return value; |
| 589 | } |
| 590 | |
Ashok Bhat | 075e9a1 | 2014-01-06 13:45:09 +0000 | [diff] [blame] | 591 | static jint |
Mike Lockwood | cbdb49d | 2011-10-20 12:54:05 -0400 | [diff] [blame] | 592 | android_media_AudioSystem_setMasterMute(JNIEnv *env, jobject thiz, jboolean mute) |
| 593 | { |
Ashok Bhat | 075e9a1 | 2014-01-06 13:45:09 +0000 | [diff] [blame] | 594 | return (jint) check_AudioSystem_Command(AudioSystem::setMasterMute(mute)); |
Mike Lockwood | cbdb49d | 2011-10-20 12:54:05 -0400 | [diff] [blame] | 595 | } |
| 596 | |
Vladimir Marko | c1bcdbb | 2014-10-29 20:34:15 +0000 | [diff] [blame] | 597 | static jboolean |
Mike Lockwood | cbdb49d | 2011-10-20 12:54:05 -0400 | [diff] [blame] | 598 | android_media_AudioSystem_getMasterMute(JNIEnv *env, jobject thiz) |
| 599 | { |
| 600 | bool mute; |
| 601 | if (AudioSystem::getMasterMute(&mute) != NO_ERROR) { |
| 602 | mute = false; |
| 603 | } |
| 604 | return mute; |
| 605 | } |
| 606 | |
Glenn Kasten | 8b4b97a | 2011-02-04 13:54:26 -0800 | [diff] [blame] | 607 | static jint |
Andy Hung | f04b84d | 2015-12-18 17:33:27 -0800 | [diff] [blame] | 608 | android_media_AudioSystem_setMasterMono(JNIEnv *env, jobject thiz, jboolean mono) |
| 609 | { |
| 610 | return (jint) check_AudioSystem_Command(AudioSystem::setMasterMono(mono)); |
| 611 | } |
| 612 | |
| 613 | static jboolean |
| 614 | android_media_AudioSystem_getMasterMono(JNIEnv *env, jobject thiz) |
| 615 | { |
| 616 | bool mono; |
| 617 | if (AudioSystem::getMasterMono(&mono) != NO_ERROR) { |
| 618 | mono = false; |
| 619 | } |
| 620 | return mono; |
| 621 | } |
| 622 | |
| 623 | static jint |
Glenn Kasten | 8b4b97a | 2011-02-04 13:54:26 -0800 | [diff] [blame] | 624 | android_media_AudioSystem_getDevicesForStream(JNIEnv *env, jobject thiz, jint stream) |
| 625 | { |
Dima Zavin | 24fc2fb | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 626 | return (jint) AudioSystem::getDevicesForStream(static_cast <audio_stream_type_t>(stream)); |
Glenn Kasten | 8b4b97a | 2011-02-04 13:54:26 -0800 | [diff] [blame] | 627 | } |
| 628 | |
Glenn Kasten | c6c4365 | 2012-09-24 17:32:30 -0700 | [diff] [blame] | 629 | static jint |
| 630 | android_media_AudioSystem_getPrimaryOutputSamplingRate(JNIEnv *env, jobject clazz) |
| 631 | { |
| 632 | return (jint) AudioSystem::getPrimaryOutputSamplingRate(); |
| 633 | } |
| 634 | |
| 635 | static jint |
| 636 | android_media_AudioSystem_getPrimaryOutputFrameCount(JNIEnv *env, jobject clazz) |
| 637 | { |
| 638 | return (jint) AudioSystem::getPrimaryOutputFrameCount(); |
| 639 | } |
| 640 | |
Oliver Woodman | 61dcdf3 | 2013-06-26 12:43:36 +0100 | [diff] [blame] | 641 | static jint |
| 642 | android_media_AudioSystem_getOutputLatency(JNIEnv *env, jobject clazz, jint stream) |
| 643 | { |
| 644 | uint32_t afLatency; |
| 645 | if (AudioSystem::getOutputLatency(&afLatency, static_cast <audio_stream_type_t>(stream)) |
| 646 | != NO_ERROR) { |
| 647 | afLatency = -1; |
| 648 | } |
| 649 | return (jint) afLatency; |
| 650 | } |
| 651 | |
Glenn Kasten | fd116ad | 2013-07-12 17:10:39 -0700 | [diff] [blame] | 652 | static jint |
Andy Hung | 7958358 | 2018-01-23 13:58:02 -0800 | [diff] [blame] | 653 | android_media_AudioSystem_setLowRamDevice( |
| 654 | JNIEnv *env, jobject clazz, jboolean isLowRamDevice, jlong totalMemory) |
Glenn Kasten | fd116ad | 2013-07-12 17:10:39 -0700 | [diff] [blame] | 655 | { |
Andy Hung | 7958358 | 2018-01-23 13:58:02 -0800 | [diff] [blame] | 656 | return (jint) AudioSystem::setLowRamDevice((bool) isLowRamDevice, (int64_t) totalMemory); |
Glenn Kasten | fd116ad | 2013-07-12 17:10:39 -0700 | [diff] [blame] | 657 | } |
| 658 | |
Ashok Bhat | 075e9a1 | 2014-01-06 13:45:09 +0000 | [diff] [blame] | 659 | static jint |
Eric Laurent | dfb881f | 2013-07-18 14:41:39 -0700 | [diff] [blame] | 660 | android_media_AudioSystem_checkAudioFlinger(JNIEnv *env, jobject clazz) |
| 661 | { |
Ashok Bhat | 075e9a1 | 2014-01-06 13:45:09 +0000 | [diff] [blame] | 662 | return (jint) check_AudioSystem_Command(AudioSystem::checkAudioFlinger()); |
Eric Laurent | dfb881f | 2013-07-18 14:41:39 -0700 | [diff] [blame] | 663 | } |
| 664 | |
Eric Laurent | b69681c | 2014-05-19 19:02:51 -0700 | [diff] [blame] | 665 | |
| 666 | static bool useInChannelMask(audio_port_type_t type, audio_port_role_t role) |
| 667 | { |
| 668 | return ((type == AUDIO_PORT_TYPE_DEVICE) && (role == AUDIO_PORT_ROLE_SOURCE)) || |
| 669 | ((type == AUDIO_PORT_TYPE_MIX) && (role == AUDIO_PORT_ROLE_SINK)); |
| 670 | } |
| 671 | |
Eric Laurent | 3a24199 | 2014-05-19 19:33:26 -0700 | [diff] [blame] | 672 | static void convertAudioGainConfigToNative(JNIEnv *env, |
| 673 | struct audio_gain_config *nAudioGainConfig, |
| 674 | const jobject jAudioGainConfig, |
| 675 | bool useInMask) |
| 676 | { |
| 677 | nAudioGainConfig->index = env->GetIntField(jAudioGainConfig, gAudioGainConfigFields.mIndex); |
| 678 | nAudioGainConfig->mode = env->GetIntField(jAudioGainConfig, gAudioGainConfigFields.mMode); |
| 679 | ALOGV("convertAudioGainConfigToNative got gain index %d", nAudioGainConfig->index); |
| 680 | jint jMask = env->GetIntField(jAudioGainConfig, gAudioGainConfigFields.mChannelMask); |
| 681 | audio_channel_mask_t nMask; |
| 682 | if (useInMask) { |
| 683 | nMask = inChannelMaskToNative(jMask); |
| 684 | ALOGV("convertAudioGainConfigToNative IN mask java %x native %x", jMask, nMask); |
| 685 | } else { |
| 686 | nMask = outChannelMaskToNative(jMask); |
| 687 | ALOGV("convertAudioGainConfigToNative OUT mask java %x native %x", jMask, nMask); |
| 688 | } |
| 689 | nAudioGainConfig->channel_mask = nMask; |
| 690 | nAudioGainConfig->ramp_duration_ms = env->GetIntField(jAudioGainConfig, |
| 691 | gAudioGainConfigFields.mRampDurationMs); |
| 692 | jintArray jValues = (jintArray)env->GetObjectField(jAudioGainConfig, |
| 693 | gAudioGainConfigFields.mValues); |
| 694 | int *nValues = env->GetIntArrayElements(jValues, NULL); |
| 695 | size_t size = env->GetArrayLength(jValues); |
| 696 | memcpy(nAudioGainConfig->values, nValues, size * sizeof(int)); |
| 697 | env->DeleteLocalRef(jValues); |
| 698 | } |
| 699 | |
Eric Laurent | b69681c | 2014-05-19 19:02:51 -0700 | [diff] [blame] | 700 | static jint convertAudioPortConfigToNative(JNIEnv *env, |
| 701 | struct audio_port_config *nAudioPortConfig, |
Eric Laurent | 6368a7d | 2014-11-19 12:18:32 -0800 | [diff] [blame] | 702 | const jobject jAudioPortConfig, |
| 703 | bool useConfigMask) |
Eric Laurent | b69681c | 2014-05-19 19:02:51 -0700 | [diff] [blame] | 704 | { |
| 705 | jobject jAudioPort = env->GetObjectField(jAudioPortConfig, gAudioPortConfigFields.mPort); |
| 706 | jobject jHandle = env->GetObjectField(jAudioPort, gAudioPortFields.mHandle); |
| 707 | nAudioPortConfig->id = env->GetIntField(jHandle, gAudioHandleFields.mId); |
| 708 | nAudioPortConfig->role = (audio_port_role_t)env->GetIntField(jAudioPort, |
| 709 | gAudioPortFields.mRole); |
| 710 | if (env->IsInstanceOf(jAudioPort, gAudioDevicePortClass)) { |
| 711 | nAudioPortConfig->type = AUDIO_PORT_TYPE_DEVICE; |
| 712 | } else if (env->IsInstanceOf(jAudioPort, gAudioMixPortClass)) { |
| 713 | nAudioPortConfig->type = AUDIO_PORT_TYPE_MIX; |
| 714 | } else { |
| 715 | env->DeleteLocalRef(jAudioPort); |
| 716 | env->DeleteLocalRef(jHandle); |
| 717 | return (jint)AUDIO_JAVA_ERROR; |
| 718 | } |
| 719 | ALOGV("convertAudioPortConfigToNative handle %d role %d type %d", |
| 720 | nAudioPortConfig->id, nAudioPortConfig->role, nAudioPortConfig->type); |
| 721 | |
Eric Laurent | 6368a7d | 2014-11-19 12:18:32 -0800 | [diff] [blame] | 722 | unsigned int configMask = 0; |
| 723 | |
Eric Laurent | b69681c | 2014-05-19 19:02:51 -0700 | [diff] [blame] | 724 | nAudioPortConfig->sample_rate = env->GetIntField(jAudioPortConfig, |
| 725 | gAudioPortConfigFields.mSamplingRate); |
Eric Laurent | 6368a7d | 2014-11-19 12:18:32 -0800 | [diff] [blame] | 726 | if (nAudioPortConfig->sample_rate != 0) { |
| 727 | configMask |= AUDIO_PORT_CONFIG_SAMPLE_RATE; |
| 728 | } |
Eric Laurent | b69681c | 2014-05-19 19:02:51 -0700 | [diff] [blame] | 729 | |
| 730 | bool useInMask = useInChannelMask(nAudioPortConfig->type, nAudioPortConfig->role); |
| 731 | audio_channel_mask_t nMask; |
| 732 | jint jMask = env->GetIntField(jAudioPortConfig, |
| 733 | gAudioPortConfigFields.mChannelMask); |
| 734 | if (useInMask) { |
| 735 | nMask = inChannelMaskToNative(jMask); |
| 736 | ALOGV("convertAudioPortConfigToNative IN mask java %x native %x", jMask, nMask); |
| 737 | } else { |
| 738 | nMask = outChannelMaskToNative(jMask); |
| 739 | ALOGV("convertAudioPortConfigToNative OUT mask java %x native %x", jMask, nMask); |
| 740 | } |
| 741 | nAudioPortConfig->channel_mask = nMask; |
Eric Laurent | 6368a7d | 2014-11-19 12:18:32 -0800 | [diff] [blame] | 742 | if (nAudioPortConfig->channel_mask != AUDIO_CHANNEL_NONE) { |
| 743 | configMask |= AUDIO_PORT_CONFIG_CHANNEL_MASK; |
| 744 | } |
Eric Laurent | b69681c | 2014-05-19 19:02:51 -0700 | [diff] [blame] | 745 | |
| 746 | jint jFormat = env->GetIntField(jAudioPortConfig, gAudioPortConfigFields.mFormat); |
| 747 | audio_format_t nFormat = audioFormatToNative(jFormat); |
| 748 | ALOGV("convertAudioPortConfigToNative format %d native %d", jFormat, nFormat); |
| 749 | nAudioPortConfig->format = nFormat; |
Eric Laurent | 6368a7d | 2014-11-19 12:18:32 -0800 | [diff] [blame] | 750 | if (nAudioPortConfig->format != AUDIO_FORMAT_DEFAULT && |
| 751 | nAudioPortConfig->format != AUDIO_FORMAT_INVALID) { |
| 752 | configMask |= AUDIO_PORT_CONFIG_FORMAT; |
| 753 | } |
| 754 | |
Eric Laurent | b69681c | 2014-05-19 19:02:51 -0700 | [diff] [blame] | 755 | jobject jGain = env->GetObjectField(jAudioPortConfig, gAudioPortConfigFields.mGain); |
| 756 | if (jGain != NULL) { |
Eric Laurent | 3a24199 | 2014-05-19 19:33:26 -0700 | [diff] [blame] | 757 | convertAudioGainConfigToNative(env, &nAudioPortConfig->gain, jGain, useInMask); |
Eric Laurent | b69681c | 2014-05-19 19:02:51 -0700 | [diff] [blame] | 758 | env->DeleteLocalRef(jGain); |
Eric Laurent | 6368a7d | 2014-11-19 12:18:32 -0800 | [diff] [blame] | 759 | configMask |= AUDIO_PORT_CONFIG_GAIN; |
Eric Laurent | b69681c | 2014-05-19 19:02:51 -0700 | [diff] [blame] | 760 | } else { |
| 761 | ALOGV("convertAudioPortConfigToNative no gain"); |
| 762 | nAudioPortConfig->gain.index = -1; |
| 763 | } |
Eric Laurent | 6368a7d | 2014-11-19 12:18:32 -0800 | [diff] [blame] | 764 | if (useConfigMask) { |
| 765 | nAudioPortConfig->config_mask = env->GetIntField(jAudioPortConfig, |
| 766 | gAudioPortConfigFields.mConfigMask); |
| 767 | } else { |
| 768 | nAudioPortConfig->config_mask = configMask; |
| 769 | } |
Eric Laurent | b69681c | 2014-05-19 19:02:51 -0700 | [diff] [blame] | 770 | env->DeleteLocalRef(jAudioPort); |
| 771 | env->DeleteLocalRef(jHandle); |
| 772 | return (jint)AUDIO_JAVA_SUCCESS; |
| 773 | } |
| 774 | |
Hongwei Wang | f44a568 | 2018-08-24 10:37:56 -0700 | [diff] [blame] | 775 | /** |
| 776 | * Extends convertAudioPortConfigToNative with extra device port info. |
| 777 | * Mix / Session specific info is not fulfilled. |
| 778 | */ |
| 779 | static jint convertAudioPortConfigToNativeWithDevicePort(JNIEnv *env, |
| 780 | struct audio_port_config *nAudioPortConfig, |
| 781 | const jobject jAudioPortConfig, |
| 782 | bool useConfigMask) |
| 783 | { |
| 784 | jint jStatus = convertAudioPortConfigToNative(env, |
| 785 | nAudioPortConfig, |
| 786 | jAudioPortConfig, |
| 787 | useConfigMask); |
| 788 | if (jStatus != AUDIO_JAVA_SUCCESS) { |
| 789 | return jStatus; |
| 790 | } |
| 791 | // Supports AUDIO_PORT_TYPE_DEVICE only |
| 792 | if (nAudioPortConfig->type != AUDIO_PORT_TYPE_DEVICE) { |
| 793 | return (jint)AUDIO_JAVA_BAD_VALUE; |
| 794 | } |
| 795 | |
| 796 | jobject jAudioDevicePort = env->GetObjectField(jAudioPortConfig, |
| 797 | gAudioPortConfigFields.mPort); |
| 798 | nAudioPortConfig->ext.device.type = env->GetIntField(jAudioDevicePort, |
| 799 | gAudioPortFields.mType); |
| 800 | jstring jDeviceAddress = (jstring)env->GetObjectField(jAudioDevicePort, |
| 801 | gAudioPortFields.mAddress); |
| 802 | const char *nDeviceAddress = env->GetStringUTFChars(jDeviceAddress, NULL); |
| 803 | strncpy(nAudioPortConfig->ext.device.address, |
| 804 | nDeviceAddress, AUDIO_DEVICE_MAX_ADDRESS_LEN - 1); |
| 805 | env->ReleaseStringUTFChars(jDeviceAddress, nDeviceAddress); |
| 806 | env->DeleteLocalRef(jDeviceAddress); |
| 807 | env->DeleteLocalRef(jAudioDevicePort); |
| 808 | return jStatus; |
| 809 | } |
| 810 | |
Eric Laurent | b69681c | 2014-05-19 19:02:51 -0700 | [diff] [blame] | 811 | static jint convertAudioPortConfigFromNative(JNIEnv *env, |
| 812 | jobject jAudioPort, |
| 813 | jobject *jAudioPortConfig, |
| 814 | const struct audio_port_config *nAudioPortConfig) |
| 815 | { |
| 816 | jint jStatus = AUDIO_JAVA_SUCCESS; |
| 817 | jobject jAudioGainConfig = NULL; |
| 818 | jobject jAudioGain = NULL; |
| 819 | jintArray jGainValues; |
| 820 | bool audioportCreated = false; |
| 821 | |
| 822 | ALOGV("convertAudioPortConfigFromNative jAudioPort %p", jAudioPort); |
| 823 | |
| 824 | if (jAudioPort == NULL) { |
| 825 | jobject jHandle = env->NewObject(gAudioHandleClass, gAudioHandleCstor, |
| 826 | nAudioPortConfig->id); |
| 827 | |
| 828 | ALOGV("convertAudioPortConfigFromNative handle %d is a %s", nAudioPortConfig->id, |
| 829 | nAudioPortConfig->type == AUDIO_PORT_TYPE_DEVICE ? "device" : "mix"); |
| 830 | |
| 831 | if (jHandle == NULL) { |
| 832 | return (jint)AUDIO_JAVA_ERROR; |
| 833 | } |
| 834 | // create dummy port and port config objects with just the correct handle |
| 835 | // and configuration data. The actual AudioPortConfig objects will be |
| 836 | // constructed by java code with correct class type (device, mix etc...) |
| 837 | // and reference to AudioPort instance in this client |
| 838 | jAudioPort = env->NewObject(gAudioPortClass, gAudioPortCstor, |
Paul McLean | f29e5f3 | 2015-06-15 10:19:22 -0700 | [diff] [blame] | 839 | jHandle, // handle |
| 840 | 0, // role |
| 841 | NULL, // name |
| 842 | NULL, // samplingRates |
| 843 | NULL, // channelMasks |
| 844 | NULL, // channelIndexMasks |
| 845 | NULL, // formats |
| 846 | NULL); // gains |
Eric Laurent | b69681c | 2014-05-19 19:02:51 -0700 | [diff] [blame] | 847 | env->DeleteLocalRef(jHandle); |
| 848 | if (jAudioPort == NULL) { |
| 849 | return (jint)AUDIO_JAVA_ERROR; |
| 850 | } |
| 851 | ALOGV("convertAudioPortConfigFromNative jAudioPort created for handle %d", |
| 852 | nAudioPortConfig->id); |
| 853 | |
| 854 | audioportCreated = true; |
| 855 | } |
| 856 | |
| 857 | bool useInMask = useInChannelMask(nAudioPortConfig->type, nAudioPortConfig->role); |
| 858 | |
| 859 | audio_channel_mask_t nMask; |
| 860 | jint jMask; |
| 861 | |
| 862 | int gainIndex = nAudioPortConfig->gain.index; |
| 863 | if (gainIndex >= 0) { |
| 864 | ALOGV("convertAudioPortConfigFromNative gain found with index %d mode %x", |
| 865 | gainIndex, nAudioPortConfig->gain.mode); |
| 866 | if (audioportCreated) { |
| 867 | ALOGV("convertAudioPortConfigFromNative creating gain"); |
| 868 | jAudioGain = env->NewObject(gAudioGainClass, gAudioGainCstor, |
| 869 | gainIndex, |
| 870 | 0, |
| 871 | 0, |
| 872 | 0, |
| 873 | 0, |
| 874 | 0, |
| 875 | 0, |
| 876 | 0, |
| 877 | 0); |
| 878 | if (jAudioGain == NULL) { |
| 879 | ALOGV("convertAudioPortConfigFromNative creating gain FAILED"); |
| 880 | jStatus = (jint)AUDIO_JAVA_ERROR; |
| 881 | goto exit; |
| 882 | } |
| 883 | } else { |
| 884 | ALOGV("convertAudioPortConfigFromNative reading gain from port"); |
| 885 | jobjectArray jGains = (jobjectArray)env->GetObjectField(jAudioPort, |
| 886 | gAudioPortFields.mGains); |
| 887 | if (jGains == NULL) { |
| 888 | ALOGV("convertAudioPortConfigFromNative could not get gains from port"); |
| 889 | jStatus = (jint)AUDIO_JAVA_ERROR; |
| 890 | goto exit; |
| 891 | } |
| 892 | jAudioGain = env->GetObjectArrayElement(jGains, gainIndex); |
| 893 | env->DeleteLocalRef(jGains); |
| 894 | if (jAudioGain == NULL) { |
| 895 | ALOGV("convertAudioPortConfigFromNative could not get gain at index %d", gainIndex); |
| 896 | jStatus = (jint)AUDIO_JAVA_ERROR; |
| 897 | goto exit; |
| 898 | } |
| 899 | } |
Eric Laurent | 0078c7c | 2014-06-04 09:49:52 -0700 | [diff] [blame] | 900 | int numValues; |
| 901 | if (useInMask) { |
| 902 | numValues = audio_channel_count_from_in_mask(nAudioPortConfig->gain.channel_mask); |
| 903 | } else { |
| 904 | numValues = audio_channel_count_from_out_mask(nAudioPortConfig->gain.channel_mask); |
| 905 | } |
Eric Laurent | b69681c | 2014-05-19 19:02:51 -0700 | [diff] [blame] | 906 | jGainValues = env->NewIntArray(numValues); |
| 907 | if (jGainValues == NULL) { |
| 908 | ALOGV("convertAudioPortConfigFromNative could not create gain values %d", numValues); |
| 909 | jStatus = (jint)AUDIO_JAVA_ERROR; |
| 910 | goto exit; |
| 911 | } |
| 912 | env->SetIntArrayRegion(jGainValues, 0, numValues, |
| 913 | nAudioPortConfig->gain.values); |
| 914 | |
| 915 | nMask = nAudioPortConfig->gain.channel_mask; |
| 916 | if (useInMask) { |
| 917 | jMask = inChannelMaskFromNative(nMask); |
| 918 | ALOGV("convertAudioPortConfigFromNative IN mask java %x native %x", jMask, nMask); |
| 919 | } else { |
| 920 | jMask = outChannelMaskFromNative(nMask); |
| 921 | ALOGV("convertAudioPortConfigFromNative OUT mask java %x native %x", jMask, nMask); |
| 922 | } |
| 923 | |
| 924 | jAudioGainConfig = env->NewObject(gAudioGainConfigClass, |
| 925 | gAudioGainConfigCstor, |
| 926 | gainIndex, |
| 927 | jAudioGain, |
| 928 | nAudioPortConfig->gain.mode, |
| 929 | jMask, |
| 930 | jGainValues, |
| 931 | nAudioPortConfig->gain.ramp_duration_ms); |
| 932 | env->DeleteLocalRef(jGainValues); |
| 933 | if (jAudioGainConfig == NULL) { |
| 934 | ALOGV("convertAudioPortConfigFromNative could not create gain config"); |
| 935 | jStatus = (jint)AUDIO_JAVA_ERROR; |
| 936 | goto exit; |
| 937 | } |
| 938 | } |
| 939 | jclass clazz; |
| 940 | jmethodID methodID; |
| 941 | if (audioportCreated) { |
| 942 | clazz = gAudioPortConfigClass; |
| 943 | methodID = gAudioPortConfigCstor; |
| 944 | ALOGV("convertAudioPortConfigFromNative building a generic port config"); |
| 945 | } else { |
| 946 | if (env->IsInstanceOf(jAudioPort, gAudioDevicePortClass)) { |
| 947 | clazz = gAudioDevicePortConfigClass; |
| 948 | methodID = gAudioDevicePortConfigCstor; |
| 949 | ALOGV("convertAudioPortConfigFromNative building a device config"); |
| 950 | } else if (env->IsInstanceOf(jAudioPort, gAudioMixPortClass)) { |
| 951 | clazz = gAudioMixPortConfigClass; |
| 952 | methodID = gAudioMixPortConfigCstor; |
| 953 | ALOGV("convertAudioPortConfigFromNative building a mix config"); |
| 954 | } else { |
| 955 | jStatus = (jint)AUDIO_JAVA_ERROR; |
| 956 | goto exit; |
| 957 | } |
| 958 | } |
| 959 | nMask = nAudioPortConfig->channel_mask; |
| 960 | if (useInMask) { |
| 961 | jMask = inChannelMaskFromNative(nMask); |
| 962 | ALOGV("convertAudioPortConfigFromNative IN mask java %x native %x", jMask, nMask); |
| 963 | } else { |
| 964 | jMask = outChannelMaskFromNative(nMask); |
| 965 | ALOGV("convertAudioPortConfigFromNative OUT mask java %x native %x", jMask, nMask); |
| 966 | } |
| 967 | |
| 968 | *jAudioPortConfig = env->NewObject(clazz, methodID, |
| 969 | jAudioPort, |
| 970 | nAudioPortConfig->sample_rate, |
| 971 | jMask, |
| 972 | audioFormatFromNative(nAudioPortConfig->format), |
| 973 | jAudioGainConfig); |
| 974 | if (*jAudioPortConfig == NULL) { |
| 975 | ALOGV("convertAudioPortConfigFromNative could not create new port config"); |
| 976 | jStatus = (jint)AUDIO_JAVA_ERROR; |
| 977 | } else { |
| 978 | ALOGV("convertAudioPortConfigFromNative OK"); |
| 979 | } |
| 980 | |
| 981 | exit: |
| 982 | if (audioportCreated) { |
| 983 | env->DeleteLocalRef(jAudioPort); |
| 984 | if (jAudioGain != NULL) { |
| 985 | env->DeleteLocalRef(jAudioGain); |
| 986 | } |
| 987 | } |
| 988 | if (jAudioGainConfig != NULL) { |
| 989 | env->DeleteLocalRef(jAudioGainConfig); |
| 990 | } |
| 991 | return jStatus; |
| 992 | } |
| 993 | |
Paul McLean | 4e5e9e9 | 2015-06-17 10:10:11 -0700 | [diff] [blame] | 994 | static bool hasFormat(int* formats, size_t size, int format) { |
| 995 | for (size_t index = 0; index < size; index++) { |
| 996 | if (formats[index] == format) { |
| 997 | return true; // found |
| 998 | } |
| 999 | } |
| 1000 | return false; // not found |
| 1001 | } |
| 1002 | |
Andy Hung | 737d9fb | 2016-08-16 18:38:49 -0700 | [diff] [blame] | 1003 | // TODO: pull out to separate file |
| 1004 | template <typename T, size_t N> |
| 1005 | static constexpr size_t array_size(const T (&)[N]) { |
| 1006 | return N; |
| 1007 | } |
| 1008 | |
Eric Laurent | b69681c | 2014-05-19 19:02:51 -0700 | [diff] [blame] | 1009 | static jint convertAudioPortFromNative(JNIEnv *env, |
| 1010 | jobject *jAudioPort, const struct audio_port *nAudioPort) |
| 1011 | { |
| 1012 | jint jStatus = (jint)AUDIO_JAVA_SUCCESS; |
| 1013 | jintArray jSamplingRates = NULL; |
| 1014 | jintArray jChannelMasks = NULL; |
Paul McLean | f29e5f3 | 2015-06-15 10:19:22 -0700 | [diff] [blame] | 1015 | jintArray jChannelIndexMasks = NULL; |
Paul McLean | 4e5e9e9 | 2015-06-17 10:10:11 -0700 | [diff] [blame] | 1016 | int* cFormats = NULL; |
Eric Laurent | b69681c | 2014-05-19 19:02:51 -0700 | [diff] [blame] | 1017 | jintArray jFormats = NULL; |
| 1018 | jobjectArray jGains = NULL; |
| 1019 | jobject jHandle = NULL; |
xiaomi/hanli | 82840c4 | 2017-07-19 18:53:03 +0800 | [diff] [blame] | 1020 | jobject jAudioPortConfig = NULL; |
Paul McLean | 10804eb | 2015-01-28 11:16:35 -0800 | [diff] [blame] | 1021 | jstring jDeviceName = NULL; |
Eric Laurent | b69681c | 2014-05-19 19:02:51 -0700 | [diff] [blame] | 1022 | bool useInMask; |
Paul McLean | f29e5f3 | 2015-06-15 10:19:22 -0700 | [diff] [blame] | 1023 | size_t numPositionMasks = 0; |
| 1024 | size_t numIndexMasks = 0; |
Paul McLean | 2db9437 | 2015-06-18 12:47:34 -0700 | [diff] [blame] | 1025 | size_t numUniqueFormats = 0; |
| 1026 | |
Paul McLean | 10804eb | 2015-01-28 11:16:35 -0800 | [diff] [blame] | 1027 | ALOGV("convertAudioPortFromNative id %d role %d type %d name %s", |
| 1028 | nAudioPort->id, nAudioPort->role, nAudioPort->type, nAudioPort->name); |
Eric Laurent | b69681c | 2014-05-19 19:02:51 -0700 | [diff] [blame] | 1029 | |
Andy Hung | 737d9fb | 2016-08-16 18:38:49 -0700 | [diff] [blame] | 1030 | // Verify audio port array count info. |
| 1031 | if (nAudioPort->num_sample_rates > array_size(nAudioPort->sample_rates) |
| 1032 | || nAudioPort->num_channel_masks > array_size(nAudioPort->channel_masks) |
| 1033 | || nAudioPort->num_formats > array_size(nAudioPort->formats) |
| 1034 | || nAudioPort->num_gains > array_size(nAudioPort->gains)) { |
| 1035 | |
| 1036 | std::stringstream ss; |
| 1037 | ss << "convertAudioPortFromNative array count out of bounds:" |
| 1038 | << " num_sample_rates " << nAudioPort->num_sample_rates |
| 1039 | << " num_channel_masks " << nAudioPort->num_channel_masks |
| 1040 | << " num_formats " << nAudioPort->num_formats |
| 1041 | << " num_gains " << nAudioPort->num_gains |
| 1042 | ; |
| 1043 | std::string s = ss.str(); |
| 1044 | |
| 1045 | // Prefer to log through Java wtf instead of native ALOGE. |
| 1046 | ScopedLocalRef<jclass> jLogClass(env, env->FindClass("android/util/Log")); |
| 1047 | jmethodID jWtfId = (jLogClass.get() == nullptr) |
| 1048 | ? nullptr |
| 1049 | : env->GetStaticMethodID(jLogClass.get(), "wtf", |
| 1050 | "(Ljava/lang/String;Ljava/lang/String;)I"); |
| 1051 | if (jWtfId != nullptr) { |
| 1052 | ScopedLocalRef<jstring> jMessage(env, env->NewStringUTF(s.c_str())); |
| 1053 | ScopedLocalRef<jstring> jTag(env, env->NewStringUTF(LOG_TAG)); |
| 1054 | (void)env->CallStaticIntMethod(jLogClass.get(), jWtfId, jTag.get(), jMessage.get()); |
| 1055 | } else { |
| 1056 | ALOGE("%s", s.c_str()); |
| 1057 | } |
| 1058 | jStatus = (jint)AUDIO_JAVA_ERROR; |
| 1059 | goto exit; |
| 1060 | } |
| 1061 | |
Eric Laurent | b69681c | 2014-05-19 19:02:51 -0700 | [diff] [blame] | 1062 | jSamplingRates = env->NewIntArray(nAudioPort->num_sample_rates); |
| 1063 | if (jSamplingRates == NULL) { |
| 1064 | jStatus = (jint)AUDIO_JAVA_ERROR; |
| 1065 | goto exit; |
| 1066 | } |
| 1067 | if (nAudioPort->num_sample_rates) { |
| 1068 | env->SetIntArrayRegion(jSamplingRates, 0, nAudioPort->num_sample_rates, |
| 1069 | (jint *)nAudioPort->sample_rates); |
| 1070 | } |
| 1071 | |
Paul McLean | f29e5f3 | 2015-06-15 10:19:22 -0700 | [diff] [blame] | 1072 | // count up how many masks are positional and indexed |
| 1073 | for(size_t index = 0; index < nAudioPort->num_channel_masks; index++) { |
| 1074 | const audio_channel_mask_t mask = nAudioPort->channel_masks[index]; |
| 1075 | if (audio_channel_mask_get_representation(mask) == AUDIO_CHANNEL_REPRESENTATION_INDEX) { |
| 1076 | numIndexMasks++; |
| 1077 | } else { |
| 1078 | numPositionMasks++; |
| 1079 | } |
| 1080 | } |
| 1081 | |
| 1082 | jChannelMasks = env->NewIntArray(numPositionMasks); |
Eric Laurent | b69681c | 2014-05-19 19:02:51 -0700 | [diff] [blame] | 1083 | if (jChannelMasks == NULL) { |
| 1084 | jStatus = (jint)AUDIO_JAVA_ERROR; |
| 1085 | goto exit; |
| 1086 | } |
Paul McLean | f29e5f3 | 2015-06-15 10:19:22 -0700 | [diff] [blame] | 1087 | jChannelIndexMasks = env->NewIntArray(numIndexMasks); |
| 1088 | if (jChannelIndexMasks == NULL) { |
| 1089 | jStatus = (jint)AUDIO_JAVA_ERROR; |
| 1090 | goto exit; |
| 1091 | } |
Eric Laurent | b69681c | 2014-05-19 19:02:51 -0700 | [diff] [blame] | 1092 | useInMask = useInChannelMask(nAudioPort->type, nAudioPort->role); |
| 1093 | |
Paul McLean | f29e5f3 | 2015-06-15 10:19:22 -0700 | [diff] [blame] | 1094 | // put the masks in the output arrays |
| 1095 | for (size_t maskIndex = 0, posMaskIndex = 0, indexedMaskIndex = 0; |
| 1096 | maskIndex < nAudioPort->num_channel_masks; maskIndex++) { |
| 1097 | const audio_channel_mask_t mask = nAudioPort->channel_masks[maskIndex]; |
| 1098 | if (audio_channel_mask_get_representation(mask) == AUDIO_CHANNEL_REPRESENTATION_INDEX) { |
| 1099 | jint jMask = audio_channel_mask_get_bits(mask); |
| 1100 | env->SetIntArrayRegion(jChannelIndexMasks, indexedMaskIndex++, 1, &jMask); |
Eric Laurent | b69681c | 2014-05-19 19:02:51 -0700 | [diff] [blame] | 1101 | } else { |
Paul McLean | f29e5f3 | 2015-06-15 10:19:22 -0700 | [diff] [blame] | 1102 | jint jMask = useInMask ? inChannelMaskFromNative(mask) |
| 1103 | : outChannelMaskFromNative(mask); |
| 1104 | env->SetIntArrayRegion(jChannelMasks, posMaskIndex++, 1, &jMask); |
Eric Laurent | b69681c | 2014-05-19 19:02:51 -0700 | [diff] [blame] | 1105 | } |
Eric Laurent | b69681c | 2014-05-19 19:02:51 -0700 | [diff] [blame] | 1106 | } |
| 1107 | |
Paul McLean | f29e5f3 | 2015-06-15 10:19:22 -0700 | [diff] [blame] | 1108 | // formats |
Paul McLean | 2db9437 | 2015-06-18 12:47:34 -0700 | [diff] [blame] | 1109 | if (nAudioPort->num_formats != 0) { |
| 1110 | cFormats = new int[nAudioPort->num_formats]; |
| 1111 | for (size_t index = 0; index < nAudioPort->num_formats; index++) { |
| 1112 | int format = audioFormatFromNative(nAudioPort->formats[index]); |
| 1113 | if (!hasFormat(cFormats, numUniqueFormats, format)) { |
| 1114 | cFormats[numUniqueFormats++] = format; |
| 1115 | } |
Paul McLean | 4e5e9e9 | 2015-06-17 10:10:11 -0700 | [diff] [blame] | 1116 | } |
| 1117 | } |
| 1118 | jFormats = env->NewIntArray(numUniqueFormats); |
Eric Laurent | b69681c | 2014-05-19 19:02:51 -0700 | [diff] [blame] | 1119 | if (jFormats == NULL) { |
| 1120 | jStatus = (jint)AUDIO_JAVA_ERROR; |
| 1121 | goto exit; |
| 1122 | } |
Paul McLean | 2db9437 | 2015-06-18 12:47:34 -0700 | [diff] [blame] | 1123 | if (numUniqueFormats != 0) { |
| 1124 | env->SetIntArrayRegion(jFormats, 0, numUniqueFormats, cFormats); |
| 1125 | } |
Eric Laurent | b69681c | 2014-05-19 19:02:51 -0700 | [diff] [blame] | 1126 | |
Paul McLean | f29e5f3 | 2015-06-15 10:19:22 -0700 | [diff] [blame] | 1127 | // gains |
Eric Laurent | b69681c | 2014-05-19 19:02:51 -0700 | [diff] [blame] | 1128 | jGains = env->NewObjectArray(nAudioPort->num_gains, |
| 1129 | gAudioGainClass, NULL); |
| 1130 | if (jGains == NULL) { |
| 1131 | jStatus = (jint)AUDIO_JAVA_ERROR; |
| 1132 | goto exit; |
| 1133 | } |
Paul McLean | f29e5f3 | 2015-06-15 10:19:22 -0700 | [diff] [blame] | 1134 | |
Eric Laurent | b69681c | 2014-05-19 19:02:51 -0700 | [diff] [blame] | 1135 | for (size_t j = 0; j < nAudioPort->num_gains; j++) { |
| 1136 | audio_channel_mask_t nMask = nAudioPort->gains[j].channel_mask; |
Paul McLean | f29e5f3 | 2015-06-15 10:19:22 -0700 | [diff] [blame] | 1137 | jint jMask; |
Eric Laurent | b69681c | 2014-05-19 19:02:51 -0700 | [diff] [blame] | 1138 | if (useInMask) { |
| 1139 | jMask = inChannelMaskFromNative(nMask); |
| 1140 | ALOGV("convertAudioPortConfigFromNative IN mask java %x native %x", jMask, nMask); |
| 1141 | } else { |
| 1142 | jMask = outChannelMaskFromNative(nMask); |
| 1143 | ALOGV("convertAudioPortConfigFromNative OUT mask java %x native %x", jMask, nMask); |
| 1144 | } |
| 1145 | |
| 1146 | jobject jGain = env->NewObject(gAudioGainClass, gAudioGainCstor, |
| 1147 | j, |
| 1148 | nAudioPort->gains[j].mode, |
| 1149 | jMask, |
| 1150 | nAudioPort->gains[j].min_value, |
| 1151 | nAudioPort->gains[j].max_value, |
| 1152 | nAudioPort->gains[j].default_value, |
| 1153 | nAudioPort->gains[j].step_value, |
| 1154 | nAudioPort->gains[j].min_ramp_ms, |
| 1155 | nAudioPort->gains[j].max_ramp_ms); |
| 1156 | if (jGain == NULL) { |
| 1157 | jStatus = (jint)AUDIO_JAVA_ERROR; |
| 1158 | goto exit; |
| 1159 | } |
| 1160 | env->SetObjectArrayElement(jGains, j, jGain); |
| 1161 | env->DeleteLocalRef(jGain); |
| 1162 | } |
| 1163 | |
| 1164 | jHandle = env->NewObject(gAudioHandleClass, gAudioHandleCstor, |
| 1165 | nAudioPort->id); |
| 1166 | if (jHandle == NULL) { |
| 1167 | jStatus = (jint)AUDIO_JAVA_ERROR; |
| 1168 | goto exit; |
| 1169 | } |
| 1170 | |
Paul McLean | 10804eb | 2015-01-28 11:16:35 -0800 | [diff] [blame] | 1171 | jDeviceName = env->NewStringUTF(nAudioPort->name); |
| 1172 | |
Eric Laurent | b69681c | 2014-05-19 19:02:51 -0700 | [diff] [blame] | 1173 | if (nAudioPort->type == AUDIO_PORT_TYPE_DEVICE) { |
| 1174 | ALOGV("convertAudioPortFromNative is a device %08x", nAudioPort->ext.device.type); |
| 1175 | jstring jAddress = env->NewStringUTF(nAudioPort->ext.device.address); |
| 1176 | *jAudioPort = env->NewObject(gAudioDevicePortClass, gAudioDevicePortCstor, |
Paul McLean | 10804eb | 2015-01-28 11:16:35 -0800 | [diff] [blame] | 1177 | jHandle, jDeviceName, |
Paul McLean | f29e5f3 | 2015-06-15 10:19:22 -0700 | [diff] [blame] | 1178 | jSamplingRates, jChannelMasks, jChannelIndexMasks, |
| 1179 | jFormats, jGains, |
Eric Laurent | b69681c | 2014-05-19 19:02:51 -0700 | [diff] [blame] | 1180 | nAudioPort->ext.device.type, jAddress); |
| 1181 | env->DeleteLocalRef(jAddress); |
| 1182 | } else if (nAudioPort->type == AUDIO_PORT_TYPE_MIX) { |
| 1183 | ALOGV("convertAudioPortFromNative is a mix"); |
| 1184 | *jAudioPort = env->NewObject(gAudioMixPortClass, gAudioMixPortCstor, |
Eric Laurent | 4bcdba8 | 2015-05-01 11:37:49 -0700 | [diff] [blame] | 1185 | jHandle, nAudioPort->ext.mix.handle, |
| 1186 | nAudioPort->role, jDeviceName, |
Paul McLean | f29e5f3 | 2015-06-15 10:19:22 -0700 | [diff] [blame] | 1187 | jSamplingRates, jChannelMasks, jChannelIndexMasks, |
Eric Laurent | b69681c | 2014-05-19 19:02:51 -0700 | [diff] [blame] | 1188 | jFormats, jGains); |
| 1189 | } else { |
| 1190 | ALOGE("convertAudioPortFromNative unknown nAudioPort type %d", nAudioPort->type); |
| 1191 | jStatus = (jint)AUDIO_JAVA_ERROR; |
| 1192 | goto exit; |
| 1193 | } |
| 1194 | if (*jAudioPort == NULL) { |
| 1195 | jStatus = (jint)AUDIO_JAVA_ERROR; |
| 1196 | goto exit; |
| 1197 | } |
| 1198 | |
Eric Laurent | b69681c | 2014-05-19 19:02:51 -0700 | [diff] [blame] | 1199 | jStatus = convertAudioPortConfigFromNative(env, |
| 1200 | *jAudioPort, |
| 1201 | &jAudioPortConfig, |
| 1202 | &nAudioPort->active_config); |
| 1203 | if (jStatus != AUDIO_JAVA_SUCCESS) { |
Andy Hung | 737d9fb | 2016-08-16 18:38:49 -0700 | [diff] [blame] | 1204 | goto exit; |
Eric Laurent | b69681c | 2014-05-19 19:02:51 -0700 | [diff] [blame] | 1205 | } |
| 1206 | |
| 1207 | env->SetObjectField(*jAudioPort, gAudioPortFields.mActiveConfig, jAudioPortConfig); |
| 1208 | |
| 1209 | exit: |
Paul McLean | 10804eb | 2015-01-28 11:16:35 -0800 | [diff] [blame] | 1210 | if (jDeviceName != NULL) { |
| 1211 | env->DeleteLocalRef(jDeviceName); |
| 1212 | } |
Eric Laurent | b69681c | 2014-05-19 19:02:51 -0700 | [diff] [blame] | 1213 | if (jSamplingRates != NULL) { |
| 1214 | env->DeleteLocalRef(jSamplingRates); |
| 1215 | } |
| 1216 | if (jChannelMasks != NULL) { |
| 1217 | env->DeleteLocalRef(jChannelMasks); |
| 1218 | } |
Paul McLean | 4e5e9e9 | 2015-06-17 10:10:11 -0700 | [diff] [blame] | 1219 | if (jChannelIndexMasks != NULL) { |
| 1220 | env->DeleteLocalRef(jChannelIndexMasks); |
| 1221 | } |
| 1222 | if (cFormats != NULL) { |
| 1223 | delete[] cFormats; |
| 1224 | } |
Eric Laurent | b69681c | 2014-05-19 19:02:51 -0700 | [diff] [blame] | 1225 | if (jFormats != NULL) { |
| 1226 | env->DeleteLocalRef(jFormats); |
| 1227 | } |
| 1228 | if (jGains != NULL) { |
| 1229 | env->DeleteLocalRef(jGains); |
| 1230 | } |
| 1231 | if (jHandle != NULL) { |
| 1232 | env->DeleteLocalRef(jHandle); |
| 1233 | } |
xiaomi/hanli | 82840c4 | 2017-07-19 18:53:03 +0800 | [diff] [blame] | 1234 | if (jAudioPortConfig != NULL) { |
| 1235 | env->DeleteLocalRef(jAudioPortConfig); |
| 1236 | } |
Eric Laurent | b69681c | 2014-05-19 19:02:51 -0700 | [diff] [blame] | 1237 | |
| 1238 | return jStatus; |
| 1239 | } |
| 1240 | |
Eric Laurent | b69681c | 2014-05-19 19:02:51 -0700 | [diff] [blame] | 1241 | static jint |
| 1242 | android_media_AudioSystem_listAudioPorts(JNIEnv *env, jobject clazz, |
| 1243 | jobject jPorts, jintArray jGeneration) |
| 1244 | { |
| 1245 | ALOGV("listAudioPorts"); |
| 1246 | |
| 1247 | if (jPorts == NULL) { |
| 1248 | ALOGE("listAudioPorts NULL AudioPort ArrayList"); |
| 1249 | return (jint)AUDIO_JAVA_BAD_VALUE; |
| 1250 | } |
| 1251 | if (!env->IsInstanceOf(jPorts, gArrayListClass)) { |
| 1252 | ALOGE("listAudioPorts not an arraylist"); |
| 1253 | return (jint)AUDIO_JAVA_BAD_VALUE; |
| 1254 | } |
| 1255 | |
| 1256 | if (jGeneration == NULL || env->GetArrayLength(jGeneration) != 1) { |
| 1257 | return (jint)AUDIO_JAVA_BAD_VALUE; |
| 1258 | } |
| 1259 | |
| 1260 | status_t status; |
| 1261 | unsigned int generation1; |
| 1262 | unsigned int generation; |
| 1263 | unsigned int numPorts; |
| 1264 | jint *nGeneration; |
| 1265 | struct audio_port *nPorts = NULL; |
| 1266 | int attempts = MAX_PORT_GENERATION_SYNC_ATTEMPTS; |
Eric Laurent | b36d104 | 2016-04-14 18:00:38 -0700 | [diff] [blame] | 1267 | jint jStatus; |
Eric Laurent | b69681c | 2014-05-19 19:02:51 -0700 | [diff] [blame] | 1268 | |
| 1269 | // get the port count and all the ports until they both return the same generation |
| 1270 | do { |
| 1271 | if (attempts-- < 0) { |
| 1272 | status = TIMED_OUT; |
| 1273 | break; |
| 1274 | } |
| 1275 | |
| 1276 | numPorts = 0; |
| 1277 | status = AudioSystem::listAudioPorts(AUDIO_PORT_ROLE_NONE, |
| 1278 | AUDIO_PORT_TYPE_NONE, |
| 1279 | &numPorts, |
| 1280 | NULL, |
| 1281 | &generation1); |
Eric Laurent | b36d104 | 2016-04-14 18:00:38 -0700 | [diff] [blame] | 1282 | if (status != NO_ERROR) { |
Eric Laurent | b69681c | 2014-05-19 19:02:51 -0700 | [diff] [blame] | 1283 | ALOGE_IF(status != NO_ERROR, "AudioSystem::listAudioPorts error %d", status); |
| 1284 | break; |
| 1285 | } |
Eric Laurent | b36d104 | 2016-04-14 18:00:38 -0700 | [diff] [blame] | 1286 | if (numPorts == 0) { |
| 1287 | jStatus = (jint)AUDIO_JAVA_SUCCESS; |
| 1288 | goto exit; |
| 1289 | } |
Eric Laurent | b69681c | 2014-05-19 19:02:51 -0700 | [diff] [blame] | 1290 | nPorts = (struct audio_port *)realloc(nPorts, numPorts * sizeof(struct audio_port)); |
| 1291 | |
| 1292 | status = AudioSystem::listAudioPorts(AUDIO_PORT_ROLE_NONE, |
| 1293 | AUDIO_PORT_TYPE_NONE, |
| 1294 | &numPorts, |
| 1295 | nPorts, |
| 1296 | &generation); |
| 1297 | ALOGV("listAudioPorts AudioSystem::listAudioPorts numPorts %d generation %d generation1 %d", |
| 1298 | numPorts, generation, generation1); |
| 1299 | } while (generation1 != generation && status == NO_ERROR); |
| 1300 | |
Eric Laurent | b36d104 | 2016-04-14 18:00:38 -0700 | [diff] [blame] | 1301 | jStatus = nativeToJavaStatus(status); |
Eric Laurent | b69681c | 2014-05-19 19:02:51 -0700 | [diff] [blame] | 1302 | if (jStatus != AUDIO_JAVA_SUCCESS) { |
| 1303 | goto exit; |
| 1304 | } |
| 1305 | |
Eric Laurent | b69681c | 2014-05-19 19:02:51 -0700 | [diff] [blame] | 1306 | for (size_t i = 0; i < numPorts; i++) { |
xiaomi/hanli | 82840c4 | 2017-07-19 18:53:03 +0800 | [diff] [blame] | 1307 | jobject jAudioPort = NULL; |
Eric Laurent | b69681c | 2014-05-19 19:02:51 -0700 | [diff] [blame] | 1308 | jStatus = convertAudioPortFromNative(env, &jAudioPort, &nPorts[i]); |
| 1309 | if (jStatus != AUDIO_JAVA_SUCCESS) { |
| 1310 | goto exit; |
| 1311 | } |
| 1312 | env->CallBooleanMethod(jPorts, gArrayListMethods.add, jAudioPort); |
xiaomi/hanli | 82840c4 | 2017-07-19 18:53:03 +0800 | [diff] [blame] | 1313 | if (jAudioPort != NULL) { |
| 1314 | env->DeleteLocalRef(jAudioPort); |
| 1315 | } |
Eric Laurent | b69681c | 2014-05-19 19:02:51 -0700 | [diff] [blame] | 1316 | } |
| 1317 | |
| 1318 | exit: |
Eric Laurent | b36d104 | 2016-04-14 18:00:38 -0700 | [diff] [blame] | 1319 | nGeneration = env->GetIntArrayElements(jGeneration, NULL); |
| 1320 | if (nGeneration == NULL) { |
| 1321 | jStatus = (jint)AUDIO_JAVA_ERROR; |
| 1322 | } else { |
| 1323 | nGeneration[0] = generation1; |
| 1324 | env->ReleaseIntArrayElements(jGeneration, nGeneration, 0); |
| 1325 | } |
Eric Laurent | b69681c | 2014-05-19 19:02:51 -0700 | [diff] [blame] | 1326 | free(nPorts); |
| 1327 | return jStatus; |
| 1328 | } |
| 1329 | |
| 1330 | static int |
| 1331 | android_media_AudioSystem_createAudioPatch(JNIEnv *env, jobject clazz, |
| 1332 | jobjectArray jPatches, jobjectArray jSources, jobjectArray jSinks) |
| 1333 | { |
| 1334 | status_t status; |
| 1335 | jint jStatus; |
| 1336 | |
| 1337 | ALOGV("createAudioPatch"); |
| 1338 | if (jPatches == NULL || jSources == NULL || jSinks == NULL) { |
| 1339 | return (jint)AUDIO_JAVA_BAD_VALUE; |
| 1340 | } |
| 1341 | |
| 1342 | if (env->GetArrayLength(jPatches) != 1) { |
| 1343 | return (jint)AUDIO_JAVA_BAD_VALUE; |
| 1344 | } |
| 1345 | jint numSources = env->GetArrayLength(jSources); |
| 1346 | if (numSources == 0 || numSources > AUDIO_PATCH_PORTS_MAX) { |
| 1347 | return (jint)AUDIO_JAVA_BAD_VALUE; |
| 1348 | } |
| 1349 | |
| 1350 | jint numSinks = env->GetArrayLength(jSinks); |
| 1351 | if (numSinks == 0 || numSinks > AUDIO_PATCH_PORTS_MAX) { |
| 1352 | return (jint)AUDIO_JAVA_BAD_VALUE; |
| 1353 | } |
| 1354 | |
| 1355 | audio_patch_handle_t handle = (audio_patch_handle_t)0; |
| 1356 | jobject jPatch = env->GetObjectArrayElement(jPatches, 0); |
| 1357 | jobject jPatchHandle = NULL; |
| 1358 | if (jPatch != NULL) { |
| 1359 | if (!env->IsInstanceOf(jPatch, gAudioPatchClass)) { |
| 1360 | return (jint)AUDIO_JAVA_BAD_VALUE; |
| 1361 | } |
| 1362 | jPatchHandle = env->GetObjectField(jPatch, gAudioPatchFields.mHandle); |
| 1363 | handle = (audio_patch_handle_t)env->GetIntField(jPatchHandle, gAudioHandleFields.mId); |
| 1364 | } |
| 1365 | |
Mikhail Naganov | 304eafc | 2018-05-23 16:54:12 -0700 | [diff] [blame] | 1366 | struct audio_patch nPatch = { .id = handle }; |
Eric Laurent | b69681c | 2014-05-19 19:02:51 -0700 | [diff] [blame] | 1367 | |
Eric Laurent | b69681c | 2014-05-19 19:02:51 -0700 | [diff] [blame] | 1368 | jobject jSource = NULL; |
| 1369 | jobject jSink = NULL; |
| 1370 | |
| 1371 | for (jint i = 0; i < numSources; i++) { |
| 1372 | jSource = env->GetObjectArrayElement(jSources, i); |
| 1373 | if (!env->IsInstanceOf(jSource, gAudioPortConfigClass)) { |
| 1374 | jStatus = (jint)AUDIO_JAVA_BAD_VALUE; |
| 1375 | goto exit; |
| 1376 | } |
Eric Laurent | 6368a7d | 2014-11-19 12:18:32 -0800 | [diff] [blame] | 1377 | jStatus = convertAudioPortConfigToNative(env, &nPatch.sources[i], jSource, false); |
Eric Laurent | b69681c | 2014-05-19 19:02:51 -0700 | [diff] [blame] | 1378 | env->DeleteLocalRef(jSource); |
| 1379 | jSource = NULL; |
| 1380 | if (jStatus != AUDIO_JAVA_SUCCESS) { |
| 1381 | goto exit; |
| 1382 | } |
| 1383 | nPatch.num_sources++; |
| 1384 | } |
| 1385 | |
| 1386 | for (jint i = 0; i < numSinks; i++) { |
| 1387 | jSink = env->GetObjectArrayElement(jSinks, i); |
| 1388 | if (!env->IsInstanceOf(jSink, gAudioPortConfigClass)) { |
| 1389 | jStatus = (jint)AUDIO_JAVA_BAD_VALUE; |
| 1390 | goto exit; |
| 1391 | } |
Eric Laurent | 6368a7d | 2014-11-19 12:18:32 -0800 | [diff] [blame] | 1392 | jStatus = convertAudioPortConfigToNative(env, &nPatch.sinks[i], jSink, false); |
Eric Laurent | b69681c | 2014-05-19 19:02:51 -0700 | [diff] [blame] | 1393 | env->DeleteLocalRef(jSink); |
| 1394 | jSink = NULL; |
| 1395 | if (jStatus != AUDIO_JAVA_SUCCESS) { |
| 1396 | goto exit; |
| 1397 | } |
| 1398 | nPatch.num_sinks++; |
| 1399 | } |
| 1400 | |
| 1401 | ALOGV("AudioSystem::createAudioPatch"); |
| 1402 | status = AudioSystem::createAudioPatch(&nPatch, &handle); |
| 1403 | ALOGV("AudioSystem::createAudioPatch() returned %d hande %d", status, handle); |
| 1404 | |
| 1405 | jStatus = nativeToJavaStatus(status); |
| 1406 | if (jStatus != AUDIO_JAVA_SUCCESS) { |
| 1407 | goto exit; |
| 1408 | } |
| 1409 | |
| 1410 | if (jPatchHandle == NULL) { |
| 1411 | jPatchHandle = env->NewObject(gAudioHandleClass, gAudioHandleCstor, |
| 1412 | handle); |
| 1413 | if (jPatchHandle == NULL) { |
| 1414 | jStatus = (jint)AUDIO_JAVA_ERROR; |
| 1415 | goto exit; |
| 1416 | } |
| 1417 | jPatch = env->NewObject(gAudioPatchClass, gAudioPatchCstor, jPatchHandle, jSources, jSinks); |
| 1418 | if (jPatch == NULL) { |
| 1419 | jStatus = (jint)AUDIO_JAVA_ERROR; |
| 1420 | goto exit; |
| 1421 | } |
| 1422 | env->SetObjectArrayElement(jPatches, 0, jPatch); |
| 1423 | } else { |
| 1424 | env->SetIntField(jPatchHandle, gAudioHandleFields.mId, handle); |
| 1425 | } |
| 1426 | |
| 1427 | exit: |
| 1428 | if (jPatchHandle != NULL) { |
| 1429 | env->DeleteLocalRef(jPatchHandle); |
| 1430 | } |
| 1431 | if (jPatch != NULL) { |
| 1432 | env->DeleteLocalRef(jPatch); |
| 1433 | } |
| 1434 | if (jSource != NULL) { |
| 1435 | env->DeleteLocalRef(jSource); |
| 1436 | } |
| 1437 | if (jSink != NULL) { |
| 1438 | env->DeleteLocalRef(jSink); |
| 1439 | } |
| 1440 | return jStatus; |
| 1441 | } |
| 1442 | |
Andreas Gampe | 0f0b491 | 2014-11-12 08:03:48 -0800 | [diff] [blame] | 1443 | static jint |
Eric Laurent | b69681c | 2014-05-19 19:02:51 -0700 | [diff] [blame] | 1444 | android_media_AudioSystem_releaseAudioPatch(JNIEnv *env, jobject clazz, |
| 1445 | jobject jPatch) |
| 1446 | { |
| 1447 | ALOGV("releaseAudioPatch"); |
| 1448 | if (jPatch == NULL) { |
| 1449 | return (jint)AUDIO_JAVA_BAD_VALUE; |
| 1450 | } |
| 1451 | |
| 1452 | audio_patch_handle_t handle = (audio_patch_handle_t)0; |
| 1453 | jobject jPatchHandle = NULL; |
| 1454 | if (!env->IsInstanceOf(jPatch, gAudioPatchClass)) { |
| 1455 | return (jint)AUDIO_JAVA_BAD_VALUE; |
| 1456 | } |
| 1457 | jPatchHandle = env->GetObjectField(jPatch, gAudioPatchFields.mHandle); |
| 1458 | handle = (audio_patch_handle_t)env->GetIntField(jPatchHandle, gAudioHandleFields.mId); |
| 1459 | env->DeleteLocalRef(jPatchHandle); |
| 1460 | |
| 1461 | ALOGV("AudioSystem::releaseAudioPatch"); |
| 1462 | status_t status = AudioSystem::releaseAudioPatch(handle); |
| 1463 | ALOGV("AudioSystem::releaseAudioPatch() returned %d", status); |
| 1464 | jint jStatus = nativeToJavaStatus(status); |
Andreas Gampe | 0f0b491 | 2014-11-12 08:03:48 -0800 | [diff] [blame] | 1465 | return jStatus; |
Eric Laurent | b69681c | 2014-05-19 19:02:51 -0700 | [diff] [blame] | 1466 | } |
| 1467 | |
| 1468 | static jint |
| 1469 | android_media_AudioSystem_listAudioPatches(JNIEnv *env, jobject clazz, |
| 1470 | jobject jPatches, jintArray jGeneration) |
| 1471 | { |
| 1472 | ALOGV("listAudioPatches"); |
| 1473 | if (jPatches == NULL) { |
| 1474 | ALOGE("listAudioPatches NULL AudioPatch ArrayList"); |
| 1475 | return (jint)AUDIO_JAVA_BAD_VALUE; |
| 1476 | } |
| 1477 | if (!env->IsInstanceOf(jPatches, gArrayListClass)) { |
| 1478 | ALOGE("listAudioPatches not an arraylist"); |
| 1479 | return (jint)AUDIO_JAVA_BAD_VALUE; |
| 1480 | } |
| 1481 | |
| 1482 | if (jGeneration == NULL || env->GetArrayLength(jGeneration) != 1) { |
| 1483 | return (jint)AUDIO_JAVA_BAD_VALUE; |
| 1484 | } |
| 1485 | |
| 1486 | status_t status; |
| 1487 | unsigned int generation1; |
| 1488 | unsigned int generation; |
| 1489 | unsigned int numPatches; |
| 1490 | jint *nGeneration; |
| 1491 | struct audio_patch *nPatches = NULL; |
| 1492 | jobjectArray jSources = NULL; |
| 1493 | jobject jSource = NULL; |
| 1494 | jobjectArray jSinks = NULL; |
| 1495 | jobject jSink = NULL; |
| 1496 | jobject jPatch = NULL; |
| 1497 | int attempts = MAX_PORT_GENERATION_SYNC_ATTEMPTS; |
Eric Laurent | b36d104 | 2016-04-14 18:00:38 -0700 | [diff] [blame] | 1498 | jint jStatus; |
Eric Laurent | b69681c | 2014-05-19 19:02:51 -0700 | [diff] [blame] | 1499 | |
| 1500 | // get the patch count and all the patches until they both return the same generation |
| 1501 | do { |
| 1502 | if (attempts-- < 0) { |
| 1503 | status = TIMED_OUT; |
| 1504 | break; |
| 1505 | } |
| 1506 | |
| 1507 | numPatches = 0; |
| 1508 | status = AudioSystem::listAudioPatches(&numPatches, |
| 1509 | NULL, |
| 1510 | &generation1); |
Eric Laurent | b36d104 | 2016-04-14 18:00:38 -0700 | [diff] [blame] | 1511 | if (status != NO_ERROR) { |
Eric Laurent | b69681c | 2014-05-19 19:02:51 -0700 | [diff] [blame] | 1512 | ALOGE_IF(status != NO_ERROR, "listAudioPatches AudioSystem::listAudioPatches error %d", |
| 1513 | status); |
| 1514 | break; |
| 1515 | } |
Eric Laurent | b36d104 | 2016-04-14 18:00:38 -0700 | [diff] [blame] | 1516 | if (numPatches == 0) { |
| 1517 | jStatus = (jint)AUDIO_JAVA_SUCCESS; |
| 1518 | goto exit; |
| 1519 | } |
| 1520 | |
Eric Laurent | b69681c | 2014-05-19 19:02:51 -0700 | [diff] [blame] | 1521 | nPatches = (struct audio_patch *)realloc(nPatches, numPatches * sizeof(struct audio_patch)); |
| 1522 | |
| 1523 | status = AudioSystem::listAudioPatches(&numPatches, |
| 1524 | nPatches, |
| 1525 | &generation); |
| 1526 | ALOGV("listAudioPatches AudioSystem::listAudioPatches numPatches %d generation %d generation1 %d", |
| 1527 | numPatches, generation, generation1); |
| 1528 | |
| 1529 | } while (generation1 != generation && status == NO_ERROR); |
| 1530 | |
Eric Laurent | b36d104 | 2016-04-14 18:00:38 -0700 | [diff] [blame] | 1531 | jStatus = nativeToJavaStatus(status); |
Eric Laurent | b69681c | 2014-05-19 19:02:51 -0700 | [diff] [blame] | 1532 | if (jStatus != AUDIO_JAVA_SUCCESS) { |
| 1533 | goto exit; |
| 1534 | } |
| 1535 | |
Eric Laurent | b69681c | 2014-05-19 19:02:51 -0700 | [diff] [blame] | 1536 | for (size_t i = 0; i < numPatches; i++) { |
| 1537 | jobject patchHandle = env->NewObject(gAudioHandleClass, gAudioHandleCstor, |
| 1538 | nPatches[i].id); |
| 1539 | if (patchHandle == NULL) { |
| 1540 | jStatus = AUDIO_JAVA_ERROR; |
| 1541 | goto exit; |
| 1542 | } |
Dan Albert | 46d8444 | 2014-11-18 16:07:51 -0800 | [diff] [blame] | 1543 | ALOGV("listAudioPatches patch %zu num_sources %d num_sinks %d", |
Eric Laurent | b69681c | 2014-05-19 19:02:51 -0700 | [diff] [blame] | 1544 | i, nPatches[i].num_sources, nPatches[i].num_sinks); |
| 1545 | |
| 1546 | env->SetIntField(patchHandle, gAudioHandleFields.mId, nPatches[i].id); |
| 1547 | |
| 1548 | // load sources |
| 1549 | jSources = env->NewObjectArray(nPatches[i].num_sources, |
| 1550 | gAudioPortConfigClass, NULL); |
| 1551 | if (jSources == NULL) { |
| 1552 | jStatus = AUDIO_JAVA_ERROR; |
| 1553 | goto exit; |
| 1554 | } |
| 1555 | |
| 1556 | for (size_t j = 0; j < nPatches[i].num_sources; j++) { |
| 1557 | jStatus = convertAudioPortConfigFromNative(env, |
| 1558 | NULL, |
| 1559 | &jSource, |
| 1560 | &nPatches[i].sources[j]); |
| 1561 | if (jStatus != AUDIO_JAVA_SUCCESS) { |
| 1562 | goto exit; |
| 1563 | } |
| 1564 | env->SetObjectArrayElement(jSources, j, jSource); |
| 1565 | env->DeleteLocalRef(jSource); |
| 1566 | jSource = NULL; |
Dan Albert | 46d8444 | 2014-11-18 16:07:51 -0800 | [diff] [blame] | 1567 | ALOGV("listAudioPatches patch %zu source %zu is a %s handle %d", |
Eric Laurent | b69681c | 2014-05-19 19:02:51 -0700 | [diff] [blame] | 1568 | i, j, |
| 1569 | nPatches[i].sources[j].type == AUDIO_PORT_TYPE_DEVICE ? "device" : "mix", |
| 1570 | nPatches[i].sources[j].id); |
| 1571 | } |
| 1572 | // load sinks |
| 1573 | jSinks = env->NewObjectArray(nPatches[i].num_sinks, |
| 1574 | gAudioPortConfigClass, NULL); |
| 1575 | if (jSinks == NULL) { |
| 1576 | jStatus = AUDIO_JAVA_ERROR; |
| 1577 | goto exit; |
| 1578 | } |
| 1579 | |
| 1580 | for (size_t j = 0; j < nPatches[i].num_sinks; j++) { |
| 1581 | jStatus = convertAudioPortConfigFromNative(env, |
| 1582 | NULL, |
| 1583 | &jSink, |
| 1584 | &nPatches[i].sinks[j]); |
| 1585 | |
| 1586 | if (jStatus != AUDIO_JAVA_SUCCESS) { |
| 1587 | goto exit; |
| 1588 | } |
| 1589 | env->SetObjectArrayElement(jSinks, j, jSink); |
| 1590 | env->DeleteLocalRef(jSink); |
| 1591 | jSink = NULL; |
Dan Albert | 46d8444 | 2014-11-18 16:07:51 -0800 | [diff] [blame] | 1592 | ALOGV("listAudioPatches patch %zu sink %zu is a %s handle %d", |
Eric Laurent | b69681c | 2014-05-19 19:02:51 -0700 | [diff] [blame] | 1593 | i, j, |
| 1594 | nPatches[i].sinks[j].type == AUDIO_PORT_TYPE_DEVICE ? "device" : "mix", |
| 1595 | nPatches[i].sinks[j].id); |
| 1596 | } |
| 1597 | |
| 1598 | jPatch = env->NewObject(gAudioPatchClass, gAudioPatchCstor, |
| 1599 | patchHandle, jSources, jSinks); |
| 1600 | env->DeleteLocalRef(jSources); |
| 1601 | jSources = NULL; |
| 1602 | env->DeleteLocalRef(jSinks); |
| 1603 | jSinks = NULL; |
| 1604 | if (jPatch == NULL) { |
| 1605 | jStatus = AUDIO_JAVA_ERROR; |
| 1606 | goto exit; |
| 1607 | } |
| 1608 | env->CallBooleanMethod(jPatches, gArrayListMethods.add, jPatch); |
| 1609 | env->DeleteLocalRef(jPatch); |
| 1610 | jPatch = NULL; |
| 1611 | } |
| 1612 | |
| 1613 | exit: |
Eric Laurent | b36d104 | 2016-04-14 18:00:38 -0700 | [diff] [blame] | 1614 | |
| 1615 | nGeneration = env->GetIntArrayElements(jGeneration, NULL); |
| 1616 | if (nGeneration == NULL) { |
| 1617 | jStatus = AUDIO_JAVA_ERROR; |
| 1618 | } else { |
| 1619 | nGeneration[0] = generation1; |
| 1620 | env->ReleaseIntArrayElements(jGeneration, nGeneration, 0); |
| 1621 | } |
| 1622 | |
Eric Laurent | b69681c | 2014-05-19 19:02:51 -0700 | [diff] [blame] | 1623 | if (jSources != NULL) { |
| 1624 | env->DeleteLocalRef(jSources); |
| 1625 | } |
| 1626 | if (jSource != NULL) { |
| 1627 | env->DeleteLocalRef(jSource); |
| 1628 | } |
| 1629 | if (jSinks != NULL) { |
| 1630 | env->DeleteLocalRef(jSinks); |
| 1631 | } |
| 1632 | if (jSink != NULL) { |
| 1633 | env->DeleteLocalRef(jSink); |
| 1634 | } |
| 1635 | if (jPatch != NULL) { |
| 1636 | env->DeleteLocalRef(jPatch); |
| 1637 | } |
| 1638 | free(nPatches); |
| 1639 | return jStatus; |
| 1640 | } |
| 1641 | |
Eric Laurent | 3a24199 | 2014-05-19 19:33:26 -0700 | [diff] [blame] | 1642 | static jint |
| 1643 | android_media_AudioSystem_setAudioPortConfig(JNIEnv *env, jobject clazz, |
| 1644 | jobject jAudioPortConfig) |
| 1645 | { |
| 1646 | ALOGV("setAudioPortConfig"); |
| 1647 | if (jAudioPortConfig == NULL) { |
| 1648 | return AUDIO_JAVA_BAD_VALUE; |
| 1649 | } |
| 1650 | if (!env->IsInstanceOf(jAudioPortConfig, gAudioPortConfigClass)) { |
| 1651 | return AUDIO_JAVA_BAD_VALUE; |
| 1652 | } |
Mikhail Naganov | 304eafc | 2018-05-23 16:54:12 -0700 | [diff] [blame] | 1653 | struct audio_port_config nAudioPortConfig = {}; |
Eric Laurent | 6368a7d | 2014-11-19 12:18:32 -0800 | [diff] [blame] | 1654 | jint jStatus = convertAudioPortConfigToNative(env, &nAudioPortConfig, jAudioPortConfig, true); |
Eric Laurent | 3a24199 | 2014-05-19 19:33:26 -0700 | [diff] [blame] | 1655 | if (jStatus != AUDIO_JAVA_SUCCESS) { |
| 1656 | return jStatus; |
| 1657 | } |
| 1658 | status_t status = AudioSystem::setAudioPortConfig(&nAudioPortConfig); |
| 1659 | ALOGV("AudioSystem::setAudioPortConfig() returned %d", status); |
| 1660 | jStatus = nativeToJavaStatus(status); |
| 1661 | return jStatus; |
| 1662 | } |
| 1663 | |
Hongwei Wang | f44a568 | 2018-08-24 10:37:56 -0700 | [diff] [blame] | 1664 | /** |
| 1665 | * Returns handle if the audio source is successfully started. |
| 1666 | */ |
| 1667 | static jint |
| 1668 | android_media_AudioSystem_startAudioSource(JNIEnv *env, jobject clazz, |
| 1669 | jobject jAudioPortConfig, |
| 1670 | jobject jAudioAttributes) |
| 1671 | { |
| 1672 | ALOGV("startAudioSource"); |
| 1673 | if (jAudioPortConfig == NULL || jAudioAttributes == NULL) { |
| 1674 | return AUDIO_JAVA_BAD_VALUE; |
| 1675 | } |
| 1676 | if (!env->IsInstanceOf(jAudioPortConfig, gAudioPortConfigClass)) { |
| 1677 | return AUDIO_JAVA_BAD_VALUE; |
| 1678 | } |
Hongwei Wang | f44a568 | 2018-08-24 10:37:56 -0700 | [diff] [blame] | 1679 | struct audio_port_config nAudioPortConfig = {}; |
| 1680 | jint jStatus = convertAudioPortConfigToNativeWithDevicePort(env, |
| 1681 | &nAudioPortConfig, jAudioPortConfig, false); |
| 1682 | if (jStatus != AUDIO_JAVA_SUCCESS) { |
| 1683 | return jStatus; |
| 1684 | } |
François Gaffie | b469128 | 2018-07-09 13:07:32 +0200 | [diff] [blame^] | 1685 | auto paa = JNIAudioAttributeHelper::makeUnique(); |
| 1686 | jStatus = JNIAudioAttributeHelper::nativeFromJava(env, jAudioAttributes, paa.get()); |
| 1687 | if (jStatus != (jint)AUDIO_JAVA_SUCCESS) { |
Hongwei Wang | f44a568 | 2018-08-24 10:37:56 -0700 | [diff] [blame] | 1688 | return jStatus; |
| 1689 | } |
| 1690 | audio_port_handle_t handle; |
François Gaffie | b469128 | 2018-07-09 13:07:32 +0200 | [diff] [blame^] | 1691 | status_t status = AudioSystem::startAudioSource(&nAudioPortConfig, paa.get(), &handle); |
Hongwei Wang | f44a568 | 2018-08-24 10:37:56 -0700 | [diff] [blame] | 1692 | ALOGV("AudioSystem::startAudioSource() returned %d handle %d", status, handle); |
| 1693 | return handle > 0 ? handle : nativeToJavaStatus(status); |
| 1694 | } |
| 1695 | |
| 1696 | static jint |
| 1697 | android_media_AudioSystem_stopAudioSource(JNIEnv *env, jobject clazz, jint handle) |
| 1698 | { |
| 1699 | ALOGV("stopAudioSource"); |
| 1700 | status_t status = AudioSystem::stopAudioSource( |
| 1701 | static_cast <audio_port_handle_t>(handle)); |
| 1702 | ALOGV("AudioSystem::stopAudioSource() returned %d", status); |
| 1703 | return nativeToJavaStatus(status); |
| 1704 | } |
| 1705 | |
Eric Laurent | 700e734 | 2014-05-02 18:33:15 -0700 | [diff] [blame] | 1706 | static void |
| 1707 | android_media_AudioSystem_eventHandlerSetup(JNIEnv *env, jobject thiz, jobject weak_this) |
| 1708 | { |
| 1709 | ALOGV("eventHandlerSetup"); |
| 1710 | |
| 1711 | sp<JNIAudioPortCallback> callback = new JNIAudioPortCallback(env, thiz, weak_this); |
| 1712 | |
Eric Laurent | 2615afb | 2015-03-25 14:51:08 -0700 | [diff] [blame] | 1713 | if (AudioSystem::addAudioPortCallback(callback) == NO_ERROR) { |
| 1714 | setJniCallback(env, thiz, callback); |
| 1715 | } |
Eric Laurent | 700e734 | 2014-05-02 18:33:15 -0700 | [diff] [blame] | 1716 | } |
| 1717 | |
| 1718 | static void |
| 1719 | android_media_AudioSystem_eventHandlerFinalize(JNIEnv *env, jobject thiz) |
| 1720 | { |
| 1721 | ALOGV("eventHandlerFinalize"); |
| 1722 | |
Eric Laurent | 2615afb | 2015-03-25 14:51:08 -0700 | [diff] [blame] | 1723 | sp<JNIAudioPortCallback> callback = setJniCallback(env, thiz, 0); |
Eric Laurent | 700e734 | 2014-05-02 18:33:15 -0700 | [diff] [blame] | 1724 | |
Eric Laurent | 2615afb | 2015-03-25 14:51:08 -0700 | [diff] [blame] | 1725 | if (callback != 0) { |
| 1726 | AudioSystem::removeAudioPortCallback(callback); |
| 1727 | } |
Eric Laurent | 700e734 | 2014-05-02 18:33:15 -0700 | [diff] [blame] | 1728 | } |
| 1729 | |
Eric Laurent | b634e1b | 2014-08-01 14:44:46 -0700 | [diff] [blame] | 1730 | static jint |
| 1731 | android_media_AudioSystem_getAudioHwSyncForSession(JNIEnv *env, jobject thiz, jint sessionId) |
| 1732 | { |
Glenn Kasten | 33b8404 | 2016-03-08 12:02:55 -0800 | [diff] [blame] | 1733 | return (jint) AudioSystem::getAudioHwSyncForSession((audio_session_t) sessionId); |
Eric Laurent | b634e1b | 2014-08-01 14:44:46 -0700 | [diff] [blame] | 1734 | } |
| 1735 | |
Jean-Michel Trivi | 5a56109 | 2015-04-23 18:48:08 -0700 | [diff] [blame] | 1736 | static void |
| 1737 | android_media_AudioSystem_registerDynPolicyCallback(JNIEnv *env, jobject thiz) |
| 1738 | { |
| 1739 | AudioSystem::setDynPolicyCallback(android_media_AudioSystem_dyn_policy_callback); |
| 1740 | } |
Eric Laurent | 7f5eb9f | 2014-12-01 19:36:30 -0800 | [diff] [blame] | 1741 | |
Jean-Michel Trivi | d3c71f0 | 2015-12-07 11:59:31 -0800 | [diff] [blame] | 1742 | static void |
| 1743 | android_media_AudioSystem_registerRecordingCallback(JNIEnv *env, jobject thiz) |
| 1744 | { |
| 1745 | AudioSystem::setRecordConfigCallback(android_media_AudioSystem_recording_callback); |
| 1746 | } |
| 1747 | |
Eric Laurent | 7f5eb9f | 2014-12-01 19:36:30 -0800 | [diff] [blame] | 1748 | |
| 1749 | static jint convertAudioMixToNative(JNIEnv *env, |
| 1750 | AudioMix *nAudioMix, |
| 1751 | const jobject jAudioMix) |
| 1752 | { |
| 1753 | nAudioMix->mMixType = env->GetIntField(jAudioMix, gAudioMixFields.mMixType); |
| 1754 | nAudioMix->mRouteFlags = env->GetIntField(jAudioMix, gAudioMixFields.mRouteFlags); |
Jean-Michel Trivi | 4ad3988 | 2016-03-14 18:46:41 -0700 | [diff] [blame] | 1755 | nAudioMix->mDeviceType = (audio_devices_t) |
| 1756 | env->GetIntField(jAudioMix, gAudioMixFields.mDeviceType); |
Eric Laurent | 7f5eb9f | 2014-12-01 19:36:30 -0800 | [diff] [blame] | 1757 | |
Jean-Michel Trivi | 4ad3988 | 2016-03-14 18:46:41 -0700 | [diff] [blame] | 1758 | jstring jDeviceAddress = (jstring)env->GetObjectField(jAudioMix, |
| 1759 | gAudioMixFields.mDeviceAddress); |
| 1760 | const char *nDeviceAddress = env->GetStringUTFChars(jDeviceAddress, NULL); |
| 1761 | nAudioMix->mDeviceAddress = String8(nDeviceAddress); |
| 1762 | env->ReleaseStringUTFChars(jDeviceAddress, nDeviceAddress); |
| 1763 | env->DeleteLocalRef(jDeviceAddress); |
Eric Laurent | 7f5eb9f | 2014-12-01 19:36:30 -0800 | [diff] [blame] | 1764 | |
Jean-Michel Trivi | 5a56109 | 2015-04-23 18:48:08 -0700 | [diff] [blame] | 1765 | nAudioMix->mCbFlags = env->GetIntField(jAudioMix, gAudioMixFields.mCallbackFlags); |
| 1766 | |
Eric Laurent | 7f5eb9f | 2014-12-01 19:36:30 -0800 | [diff] [blame] | 1767 | jobject jFormat = env->GetObjectField(jAudioMix, gAudioMixFields.mFormat); |
| 1768 | nAudioMix->mFormat.sample_rate = env->GetIntField(jFormat, |
| 1769 | gAudioFormatFields.mSampleRate); |
| 1770 | nAudioMix->mFormat.channel_mask = outChannelMaskToNative(env->GetIntField(jFormat, |
| 1771 | gAudioFormatFields.mChannelMask)); |
| 1772 | nAudioMix->mFormat.format = audioFormatToNative(env->GetIntField(jFormat, |
| 1773 | gAudioFormatFields.mEncoding)); |
| 1774 | env->DeleteLocalRef(jFormat); |
| 1775 | |
| 1776 | jobject jRule = env->GetObjectField(jAudioMix, gAudioMixFields.mRule); |
| 1777 | jobject jRuleCriteria = env->GetObjectField(jRule, gAudioMixingRuleFields.mCriteria); |
| 1778 | env->DeleteLocalRef(jRule); |
| 1779 | jobjectArray jCriteria = (jobjectArray)env->CallObjectMethod(jRuleCriteria, |
| 1780 | gArrayListMethods.toArray); |
| 1781 | env->DeleteLocalRef(jRuleCriteria); |
| 1782 | |
| 1783 | jint numCriteria = env->GetArrayLength(jCriteria); |
| 1784 | if (numCriteria > MAX_CRITERIA_PER_MIX) { |
| 1785 | numCriteria = MAX_CRITERIA_PER_MIX; |
| 1786 | } |
| 1787 | |
| 1788 | for (jint i = 0; i < numCriteria; i++) { |
Jean-Michel Trivi | ba6b4b4 | 2016-02-10 14:26:35 -0800 | [diff] [blame] | 1789 | AudioMixMatchCriterion nCriterion; |
Eric Laurent | 7f5eb9f | 2014-12-01 19:36:30 -0800 | [diff] [blame] | 1790 | |
| 1791 | jobject jCriterion = env->GetObjectArrayElement(jCriteria, i); |
| 1792 | |
Jean-Michel Trivi | ba6b4b4 | 2016-02-10 14:26:35 -0800 | [diff] [blame] | 1793 | nCriterion.mRule = env->GetIntField(jCriterion, gAudioMixMatchCriterionFields.mRule); |
Eric Laurent | 7f5eb9f | 2014-12-01 19:36:30 -0800 | [diff] [blame] | 1794 | |
Jean-Michel Trivi | ba6b4b4 | 2016-02-10 14:26:35 -0800 | [diff] [blame] | 1795 | const uint32_t match_rule = nCriterion.mRule & ~RULE_EXCLUSION_MASK; |
| 1796 | switch (match_rule) { |
| 1797 | case RULE_MATCH_UID: |
| 1798 | nCriterion.mValue.mUid = env->GetIntField(jCriterion, |
| 1799 | gAudioMixMatchCriterionFields.mIntProp); |
| 1800 | break; |
| 1801 | case RULE_MATCH_ATTRIBUTE_USAGE: |
| 1802 | case RULE_MATCH_ATTRIBUTE_CAPTURE_PRESET: { |
| 1803 | jobject jAttributes = env->GetObjectField(jCriterion, gAudioMixMatchCriterionFields.mAttr); |
François Gaffie | b469128 | 2018-07-09 13:07:32 +0200 | [diff] [blame^] | 1804 | |
| 1805 | auto paa = JNIAudioAttributeHelper::makeUnique(); |
| 1806 | jint jStatus = JNIAudioAttributeHelper::nativeFromJava(env, jAttributes, paa.get()); |
| 1807 | if (jStatus != (jint)AUDIO_JAVA_SUCCESS) { |
| 1808 | return jStatus; |
| 1809 | } |
Jean-Michel Trivi | ba6b4b4 | 2016-02-10 14:26:35 -0800 | [diff] [blame] | 1810 | if (match_rule == RULE_MATCH_ATTRIBUTE_USAGE) { |
François Gaffie | b469128 | 2018-07-09 13:07:32 +0200 | [diff] [blame^] | 1811 | nCriterion.mValue.mUsage = paa->usage; |
Jean-Michel Trivi | ba6b4b4 | 2016-02-10 14:26:35 -0800 | [diff] [blame] | 1812 | } else { |
François Gaffie | b469128 | 2018-07-09 13:07:32 +0200 | [diff] [blame^] | 1813 | nCriterion.mValue.mSource = paa->source; |
Jean-Michel Trivi | ba6b4b4 | 2016-02-10 14:26:35 -0800 | [diff] [blame] | 1814 | } |
| 1815 | env->DeleteLocalRef(jAttributes); |
| 1816 | } |
| 1817 | break; |
Eric Laurent | 7f5eb9f | 2014-12-01 19:36:30 -0800 | [diff] [blame] | 1818 | } |
Eric Laurent | 7f5eb9f | 2014-12-01 19:36:30 -0800 | [diff] [blame] | 1819 | |
| 1820 | nAudioMix->mCriteria.add(nCriterion); |
| 1821 | env->DeleteLocalRef(jCriterion); |
| 1822 | } |
| 1823 | |
| 1824 | env->DeleteLocalRef(jCriteria); |
| 1825 | |
| 1826 | return (jint)AUDIO_JAVA_SUCCESS; |
| 1827 | } |
| 1828 | |
| 1829 | static jint |
| 1830 | android_media_AudioSystem_registerPolicyMixes(JNIEnv *env, jobject clazz, |
| 1831 | jobject jMixesList, jboolean registration) |
| 1832 | { |
| 1833 | ALOGV("registerPolicyMixes"); |
| 1834 | |
| 1835 | if (jMixesList == NULL) { |
| 1836 | return (jint)AUDIO_JAVA_BAD_VALUE; |
| 1837 | } |
| 1838 | if (!env->IsInstanceOf(jMixesList, gArrayListClass)) { |
| 1839 | return (jint)AUDIO_JAVA_BAD_VALUE; |
| 1840 | } |
| 1841 | jobjectArray jMixes = (jobjectArray)env->CallObjectMethod(jMixesList, |
| 1842 | gArrayListMethods.toArray); |
| 1843 | jint numMixes = env->GetArrayLength(jMixes); |
| 1844 | if (numMixes > MAX_MIXES_PER_POLICY) { |
| 1845 | numMixes = MAX_MIXES_PER_POLICY; |
| 1846 | } |
| 1847 | |
| 1848 | status_t status; |
| 1849 | jint jStatus; |
| 1850 | jobject jAudioMix = NULL; |
| 1851 | Vector <AudioMix> mixes; |
| 1852 | for (jint i = 0; i < numMixes; i++) { |
| 1853 | jAudioMix = env->GetObjectArrayElement(jMixes, i); |
| 1854 | if (!env->IsInstanceOf(jAudioMix, gAudioMixClass)) { |
| 1855 | jStatus = (jint)AUDIO_JAVA_BAD_VALUE; |
| 1856 | goto exit; |
| 1857 | } |
| 1858 | AudioMix mix; |
| 1859 | jStatus = convertAudioMixToNative(env, &mix, jAudioMix); |
| 1860 | env->DeleteLocalRef(jAudioMix); |
| 1861 | jAudioMix = NULL; |
| 1862 | if (jStatus != AUDIO_JAVA_SUCCESS) { |
| 1863 | goto exit; |
| 1864 | } |
| 1865 | mixes.add(mix); |
| 1866 | } |
| 1867 | |
| 1868 | ALOGV("AudioSystem::registerPolicyMixes numMixes %d registration %d", numMixes, registration); |
| 1869 | status = AudioSystem::registerPolicyMixes(mixes, registration); |
| 1870 | ALOGV("AudioSystem::registerPolicyMixes() returned %d", status); |
| 1871 | |
| 1872 | jStatus = nativeToJavaStatus(status); |
| 1873 | if (jStatus != AUDIO_JAVA_SUCCESS) { |
| 1874 | goto exit; |
| 1875 | } |
| 1876 | |
| 1877 | exit: |
| 1878 | if (jAudioMix != NULL) { |
| 1879 | env->DeleteLocalRef(jAudioMix); |
| 1880 | } |
| 1881 | return jStatus; |
| 1882 | } |
| 1883 | |
Jean-Michel Trivi | d6f65de | 2018-12-18 18:49:14 -0800 | [diff] [blame] | 1884 | static jint android_media_AudioSystem_setUidDeviceAffinities(JNIEnv *env, jobject clazz, |
| 1885 | jint uid, jintArray deviceTypes, jobjectArray deviceAddresses) { |
| 1886 | if (deviceTypes == nullptr || deviceAddresses == nullptr) { |
| 1887 | return (jint) AUDIO_JAVA_BAD_VALUE; |
| 1888 | } |
| 1889 | jsize nb = env->GetArrayLength(deviceTypes); |
| 1890 | if (nb == 0 || nb != env->GetArrayLength(deviceAddresses)) { |
| 1891 | return (jint) AUDIO_JAVA_BAD_VALUE; |
| 1892 | } |
| 1893 | // retrieve all device types |
| 1894 | std::vector<audio_devices_t> deviceTypesVector; |
| 1895 | jint* typesPtr = nullptr; |
| 1896 | typesPtr = env->GetIntArrayElements(deviceTypes, 0); |
| 1897 | if (typesPtr == nullptr) { |
| 1898 | return (jint) AUDIO_JAVA_BAD_VALUE; |
| 1899 | } |
| 1900 | for (jint i = 0; i < nb; i++) { |
| 1901 | deviceTypesVector.push_back((audio_devices_t) typesPtr[i]); |
| 1902 | } |
| 1903 | env->ReleaseIntArrayElements(deviceTypes, typesPtr, 0); |
| 1904 | |
| 1905 | // check each address is a string and add device type/address to list for device affinity |
| 1906 | Vector<AudioDeviceTypeAddr> deviceVector; |
| 1907 | jclass stringClass = FindClassOrDie(env, "java/lang/String"); |
| 1908 | for (jint i = 0; i < nb; i++) { |
| 1909 | jobject addrJobj = env->GetObjectArrayElement(deviceAddresses, i); |
| 1910 | if (!env->IsInstanceOf(addrJobj, stringClass)) { |
| 1911 | return (jint) AUDIO_JAVA_BAD_VALUE; |
| 1912 | } |
| 1913 | String8 address = String8(env->GetStringUTFChars((jstring) addrJobj, NULL)); |
| 1914 | AudioDeviceTypeAddr dev = AudioDeviceTypeAddr(typesPtr[i], address); |
| 1915 | deviceVector.add(dev); |
| 1916 | } |
| 1917 | |
| 1918 | status_t status = AudioSystem::setUidDeviceAffinities((uid_t) uid, deviceVector); |
| 1919 | return (jint) nativeToJavaStatus(status); |
| 1920 | } |
| 1921 | |
| 1922 | static jint android_media_AudioSystem_removeUidDeviceAffinities(JNIEnv *env, jobject clazz, |
| 1923 | jint uid) { |
| 1924 | |
| 1925 | //### |
| 1926 | status_t status = NO_ERROR;//AudioSystem::removeUidDeviceAffinities(); |
| 1927 | return (jint) nativeToJavaStatus(status); |
| 1928 | } |
| 1929 | |
| 1930 | |
Eric Laurent | 0867bed | 2015-05-20 14:49:08 -0700 | [diff] [blame] | 1931 | static jint |
| 1932 | android_media_AudioSystem_systemReady(JNIEnv *env, jobject thiz) |
| 1933 | { |
| 1934 | return nativeToJavaStatus(AudioSystem::systemReady()); |
| 1935 | } |
Eric Laurent | 7f5eb9f | 2014-12-01 19:36:30 -0800 | [diff] [blame] | 1936 | |
Eric Laurent | eab40d1 | 2017-06-09 12:45:21 -0700 | [diff] [blame] | 1937 | static jfloat |
| 1938 | android_media_AudioSystem_getStreamVolumeDB(JNIEnv *env, jobject thiz, |
| 1939 | jint stream, jint index, jint device) |
| 1940 | { |
| 1941 | return (jfloat)AudioSystem::getStreamVolumeDB((audio_stream_type_t)stream, |
| 1942 | (int)index, |
| 1943 | (audio_devices_t)device); |
| 1944 | } |
Eric Laurent | 7f5eb9f | 2014-12-01 19:36:30 -0800 | [diff] [blame] | 1945 | |
Jean-Michel Trivi | 980d38f | 2018-01-08 15:43:35 -0800 | [diff] [blame] | 1946 | static jboolean |
| 1947 | android_media_AudioSystem_isOffloadSupported(JNIEnv *env, jobject thiz, |
| 1948 | jint encoding, jint sampleRate, jint channelMask, jint channelIndexMask) |
| 1949 | { |
| 1950 | audio_offload_info_t format = AUDIO_INFO_INITIALIZER; |
| 1951 | format.format = (audio_format_t) audioFormatToNative(encoding); |
| 1952 | format.sample_rate = (uint32_t) sampleRate; |
| 1953 | format.channel_mask = nativeChannelMaskFromJavaChannelMasks(channelMask, channelIndexMask); |
| 1954 | format.stream_type = AUDIO_STREAM_MUSIC; |
| 1955 | format.has_video = false; |
| 1956 | format.is_streaming = false; |
| 1957 | // offload duration unknown at this point: |
| 1958 | // client side code cannot access "audio.offload.min.duration.secs" property to make a query |
| 1959 | // agnostic of duration, so using acceptable estimate of 2mn |
| 1960 | format.duration_us = 120 * 1000000; |
| 1961 | return AudioSystem::isOffloadSupported(format); |
| 1962 | } |
| 1963 | |
jiabin | c0f4944 | 2018-01-05 10:23:50 -0800 | [diff] [blame] | 1964 | static jint |
| 1965 | android_media_AudioSystem_getMicrophones(JNIEnv *env, jobject thiz, jobject jMicrophonesInfo) |
| 1966 | { |
| 1967 | ALOGV("getMicrophones"); |
| 1968 | |
| 1969 | if (jMicrophonesInfo == NULL) { |
| 1970 | ALOGE("jMicrophonesInfo NULL MicrophoneInfo ArrayList"); |
| 1971 | return (jint)AUDIO_JAVA_BAD_VALUE; |
| 1972 | } |
| 1973 | if (!env->IsInstanceOf(jMicrophonesInfo, gArrayListClass)) { |
| 1974 | ALOGE("getMicrophones not an arraylist"); |
| 1975 | return (jint)AUDIO_JAVA_BAD_VALUE; |
| 1976 | } |
| 1977 | |
| 1978 | jint jStatus; |
| 1979 | std::vector<media::MicrophoneInfo> microphones; |
| 1980 | status_t status = AudioSystem::getMicrophones(µphones); |
| 1981 | if (status != NO_ERROR) { |
Arun Mirpuri | cb102fa | 2019-01-11 18:39:21 -0800 | [diff] [blame] | 1982 | ALOGE("AudioSystem::getMicrophones error %d", status); |
jiabin | c0f4944 | 2018-01-05 10:23:50 -0800 | [diff] [blame] | 1983 | jStatus = nativeToJavaStatus(status); |
| 1984 | return jStatus; |
| 1985 | } |
| 1986 | if (microphones.size() == 0) { |
| 1987 | jStatus = (jint)AUDIO_JAVA_SUCCESS; |
| 1988 | return jStatus; |
| 1989 | } |
| 1990 | for (size_t i = 0; i < microphones.size(); i++) { |
| 1991 | jobject jMicrophoneInfo; |
| 1992 | jStatus = convertMicrophoneInfoFromNative(env, &jMicrophoneInfo, µphones[i]); |
| 1993 | if (jStatus != AUDIO_JAVA_SUCCESS) { |
| 1994 | return jStatus; |
| 1995 | } |
| 1996 | env->CallBooleanMethod(jMicrophonesInfo, gArrayListMethods.add, jMicrophoneInfo); |
| 1997 | env->DeleteLocalRef(jMicrophoneInfo); |
| 1998 | } |
| 1999 | |
| 2000 | return jStatus; |
| 2001 | } |
| 2002 | |
jiabin | 3994075 | 2018-04-02 18:18:45 -0700 | [diff] [blame] | 2003 | static jint |
Arun Mirpuri | cb102fa | 2019-01-11 18:39:21 -0800 | [diff] [blame] | 2004 | android_media_AudioSystem_getHwOffloadEncodingFormatsSupportedForA2DP( |
| 2005 | JNIEnv *env, jobject thiz, jobject jEncodingFormatList) |
| 2006 | { |
| 2007 | ALOGV("%s", __FUNCTION__); |
| 2008 | jint jStatus = AUDIO_JAVA_SUCCESS; |
| 2009 | if (!env->IsInstanceOf(jEncodingFormatList, gArrayListClass)) { |
| 2010 | ALOGE("%s: jEncodingFormatList not an ArrayList", __FUNCTION__); |
| 2011 | return (jint)AUDIO_JAVA_BAD_VALUE; |
| 2012 | } |
| 2013 | std::vector<audio_format_t> encodingFormats; |
Eric Laurent | fffeac8 | 2019-01-17 09:03:47 -0800 | [diff] [blame] | 2014 | status_t status = AudioSystem::getHwOffloadEncodingFormatsSupportedForA2DP( |
| 2015 | &encodingFormats); |
Arun Mirpuri | cb102fa | 2019-01-11 18:39:21 -0800 | [diff] [blame] | 2016 | if (status != NO_ERROR) { |
| 2017 | ALOGE("%s: error %d", __FUNCTION__, status); |
| 2018 | jStatus = nativeToJavaStatus(status); |
| 2019 | return jStatus; |
| 2020 | } |
| 2021 | |
| 2022 | for (size_t i = 0; i < encodingFormats.size(); i++) { |
| 2023 | ScopedLocalRef<jobject> jEncodingFormat( |
| 2024 | env, env->NewObject(gIntegerClass, gIntegerCstor, encodingFormats[i])); |
| 2025 | env->CallBooleanMethod(jEncodingFormatList, gArrayListMethods.add, |
| 2026 | jEncodingFormat.get()); |
| 2027 | } |
| 2028 | return jStatus; |
| 2029 | } |
| 2030 | |
| 2031 | static jint |
jiabin | 3994075 | 2018-04-02 18:18:45 -0700 | [diff] [blame] | 2032 | android_media_AudioSystem_getSurroundFormats(JNIEnv *env, jobject thiz, |
| 2033 | jobject jSurroundFormats, jboolean reported) |
| 2034 | { |
| 2035 | ALOGV("getSurroundFormats"); |
| 2036 | |
| 2037 | if (jSurroundFormats == NULL) { |
| 2038 | ALOGE("jSurroundFormats is NULL"); |
| 2039 | return (jint)AUDIO_JAVA_BAD_VALUE; |
| 2040 | } |
| 2041 | if (!env->IsInstanceOf(jSurroundFormats, gMapClass)) { |
| 2042 | ALOGE("getSurroundFormats not a map"); |
| 2043 | return (jint)AUDIO_JAVA_BAD_VALUE; |
| 2044 | } |
| 2045 | |
| 2046 | jint jStatus; |
| 2047 | unsigned int numSurroundFormats = 0; |
| 2048 | audio_format_t *surroundFormats = NULL; |
| 2049 | bool *surroundFormatsEnabled = NULL; |
| 2050 | status_t status = AudioSystem::getSurroundFormats( |
| 2051 | &numSurroundFormats, surroundFormats, surroundFormatsEnabled, reported); |
| 2052 | if (status != NO_ERROR) { |
| 2053 | ALOGE_IF(status != NO_ERROR, "AudioSystem::getSurroundFormats error %d", status); |
| 2054 | jStatus = nativeToJavaStatus(status); |
| 2055 | goto exit; |
| 2056 | } |
| 2057 | if (numSurroundFormats == 0) { |
| 2058 | jStatus = (jint)AUDIO_JAVA_SUCCESS; |
| 2059 | goto exit; |
| 2060 | } |
| 2061 | surroundFormats = (audio_format_t *)calloc(numSurroundFormats, sizeof(audio_format_t)); |
| 2062 | surroundFormatsEnabled = (bool *)calloc(numSurroundFormats, sizeof(bool)); |
| 2063 | status = AudioSystem::getSurroundFormats( |
| 2064 | &numSurroundFormats, surroundFormats, surroundFormatsEnabled, reported); |
| 2065 | jStatus = nativeToJavaStatus(status); |
| 2066 | if (status != NO_ERROR) { |
| 2067 | ALOGE_IF(status != NO_ERROR, "AudioSystem::getSurroundFormats error %d", status); |
| 2068 | goto exit; |
| 2069 | } |
| 2070 | for (size_t i = 0; i < numSurroundFormats; i++) { |
| 2071 | jobject surroundFormat = env->NewObject(gIntegerClass, gIntegerCstor, |
| 2072 | audioFormatFromNative(surroundFormats[i])); |
| 2073 | jobject enabled = env->NewObject(gBooleanClass, gBooleanCstor, surroundFormatsEnabled[i]); |
| 2074 | env->CallObjectMethod(jSurroundFormats, gMapPut, surroundFormat, enabled); |
| 2075 | env->DeleteLocalRef(surroundFormat); |
| 2076 | env->DeleteLocalRef(enabled); |
| 2077 | } |
| 2078 | |
| 2079 | exit: |
| 2080 | free(surroundFormats); |
| 2081 | free(surroundFormatsEnabled); |
| 2082 | return jStatus; |
| 2083 | } |
| 2084 | |
| 2085 | static jint |
| 2086 | android_media_AudioSystem_setSurroundFormatEnabled(JNIEnv *env, jobject thiz, |
| 2087 | jint audioFormat, jboolean enabled) |
| 2088 | { |
| 2089 | status_t status = AudioSystem::setSurroundFormatEnabled(audioFormatToNative(audioFormat), |
| 2090 | (bool)enabled); |
| 2091 | if (status != NO_ERROR) { |
| 2092 | ALOGE_IF(status != NO_ERROR, "AudioSystem::setSurroundFormatEnabled error %d", status); |
| 2093 | } |
| 2094 | return (jint)nativeToJavaStatus(status); |
| 2095 | } |
| 2096 | |
Paul McLean | b3ae6d9 | 2018-11-05 15:16:59 -0700 | [diff] [blame] | 2097 | static jint android_media_AudioSystem_get_FCC_8(JNIEnv *env, jobject thiz) { |
| 2098 | return FCC_8; |
| 2099 | } |
| 2100 | |
Eric Laurent | 1c9c1d5 | 2018-10-17 10:06:46 -0700 | [diff] [blame] | 2101 | static jint |
| 2102 | android_media_AudioSystem_setAssistantUid(JNIEnv *env, jobject thiz, jint uid) |
| 2103 | { |
| 2104 | status_t status = AudioSystem::setAssistantUid(uid); |
| 2105 | return (jint)nativeToJavaStatus(status); |
| 2106 | } |
| 2107 | |
| 2108 | static jint |
| 2109 | android_media_AudioSystem_setA11yServicesUids(JNIEnv *env, jobject thiz, jintArray uids) { |
| 2110 | std::vector<uid_t> nativeUidsVector; |
| 2111 | |
| 2112 | if (uids != nullptr) { |
| 2113 | jsize len = env->GetArrayLength(uids); |
| 2114 | |
| 2115 | if (len > 0) { |
| 2116 | int *nativeUids = nullptr; |
| 2117 | nativeUids = env->GetIntArrayElements(uids, 0); |
| 2118 | if (nativeUids != nullptr) { |
| 2119 | for (size_t i = 0; i < len; i++) { |
| 2120 | nativeUidsVector.push_back(nativeUids[i]); |
| 2121 | } |
| 2122 | env->ReleaseIntArrayElements(uids, nativeUids, 0); |
| 2123 | } |
| 2124 | } |
| 2125 | } |
| 2126 | status_t status = AudioSystem::setA11yServicesUids(nativeUidsVector); |
| 2127 | return (jint)nativeToJavaStatus(status); |
| 2128 | } |
| 2129 | |
jiabin | 66f9e72 | 2018-11-02 16:20:19 -0700 | [diff] [blame] | 2130 | static jboolean |
| 2131 | android_media_AudioSystem_isHapticPlaybackSupported(JNIEnv *env, jobject thiz) |
| 2132 | { |
| 2133 | return AudioSystem::isHapticPlaybackSupported(); |
| 2134 | } |
| 2135 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2136 | // ---------------------------------------------------------------------------- |
| 2137 | |
Daniel Micay | 76f6a86 | 2015-09-19 17:31:01 -0400 | [diff] [blame] | 2138 | static const JNINativeMethod gMethods[] = { |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 2139 | {"setParameters", "(Ljava/lang/String;)I", (void *)android_media_AudioSystem_setParameters}, |
| 2140 | {"getParameters", "(Ljava/lang/String;)Ljava/lang/String;", (void *)android_media_AudioSystem_getParameters}, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2141 | {"muteMicrophone", "(Z)I", (void *)android_media_AudioSystem_muteMicrophone}, |
| 2142 | {"isMicrophoneMuted", "()Z", (void *)android_media_AudioSystem_isMicrophoneMuted}, |
Jean-Michel Trivi | d677054 | 2012-10-10 12:03:41 -0700 | [diff] [blame] | 2143 | {"isStreamActive", "(II)Z", (void *)android_media_AudioSystem_isStreamActive}, |
Jean-Michel Trivi | 679d504 | 2013-02-04 16:24:09 -0800 | [diff] [blame] | 2144 | {"isStreamActiveRemotely","(II)Z", (void *)android_media_AudioSystem_isStreamActiveRemotely}, |
Jean-Michel Trivi | d677054 | 2012-10-10 12:03:41 -0700 | [diff] [blame] | 2145 | {"isSourceActive", "(I)Z", (void *)android_media_AudioSystem_isSourceActive}, |
Jean-Michel Trivi | 8a21f5d | 2014-06-05 15:03:52 -0700 | [diff] [blame] | 2146 | {"newAudioSessionId", "()I", (void *)android_media_AudioSystem_newAudioSessionId}, |
Jean-Michel Trivi | 292a6a4 | 2016-12-01 08:32:15 -0800 | [diff] [blame] | 2147 | {"newAudioPlayerId", "()I", (void *)android_media_AudioSystem_newAudioPlayerId}, |
Aniket Kumar Lata | 9fbc205 | 2019-01-11 02:02:33 -0800 | [diff] [blame] | 2148 | {"setDeviceConnectionState", "(IILjava/lang/String;Ljava/lang/String;I)I", (void *)android_media_AudioSystem_setDeviceConnectionState}, |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 2149 | {"getDeviceConnectionState", "(ILjava/lang/String;)I", (void *)android_media_AudioSystem_getDeviceConnectionState}, |
Aniket Kumar Lata | 9fbc205 | 2019-01-11 02:02:33 -0800 | [diff] [blame] | 2150 | {"handleDeviceConfigChange", "(ILjava/lang/String;Ljava/lang/String;I)I", (void *)android_media_AudioSystem_handleDeviceConfigChange}, |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 2151 | {"setPhoneState", "(I)I", (void *)android_media_AudioSystem_setPhoneState}, |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 2152 | {"setForceUse", "(II)I", (void *)android_media_AudioSystem_setForceUse}, |
| 2153 | {"getForceUse", "(I)I", (void *)android_media_AudioSystem_getForceUse}, |
| 2154 | {"initStreamVolume", "(III)I", (void *)android_media_AudioSystem_initStreamVolume}, |
Eric Laurent | 9bc8358d | 2011-11-18 16:43:31 -0800 | [diff] [blame] | 2155 | {"setStreamVolumeIndex","(III)I", (void *)android_media_AudioSystem_setStreamVolumeIndex}, |
| 2156 | {"getStreamVolumeIndex","(II)I", (void *)android_media_AudioSystem_getStreamVolumeIndex}, |
Mike Lockwood | cbdb49d | 2011-10-20 12:54:05 -0400 | [diff] [blame] | 2157 | {"setMasterVolume", "(F)I", (void *)android_media_AudioSystem_setMasterVolume}, |
| 2158 | {"getMasterVolume", "()F", (void *)android_media_AudioSystem_getMasterVolume}, |
| 2159 | {"setMasterMute", "(Z)I", (void *)android_media_AudioSystem_setMasterMute}, |
| 2160 | {"getMasterMute", "()Z", (void *)android_media_AudioSystem_getMasterMute}, |
Andy Hung | f04b84d | 2015-12-18 17:33:27 -0800 | [diff] [blame] | 2161 | {"setMasterMono", "(Z)I", (void *)android_media_AudioSystem_setMasterMono}, |
| 2162 | {"getMasterMono", "()Z", (void *)android_media_AudioSystem_getMasterMono}, |
Glenn Kasten | 8b4b97a | 2011-02-04 13:54:26 -0800 | [diff] [blame] | 2163 | {"getDevicesForStream", "(I)I", (void *)android_media_AudioSystem_getDevicesForStream}, |
Glenn Kasten | c6c4365 | 2012-09-24 17:32:30 -0700 | [diff] [blame] | 2164 | {"getPrimaryOutputSamplingRate", "()I", (void *)android_media_AudioSystem_getPrimaryOutputSamplingRate}, |
| 2165 | {"getPrimaryOutputFrameCount", "()I", (void *)android_media_AudioSystem_getPrimaryOutputFrameCount}, |
Oliver Woodman | 61dcdf3 | 2013-06-26 12:43:36 +0100 | [diff] [blame] | 2166 | {"getOutputLatency", "(I)I", (void *)android_media_AudioSystem_getOutputLatency}, |
Andy Hung | 7958358 | 2018-01-23 13:58:02 -0800 | [diff] [blame] | 2167 | {"setLowRamDevice", "(ZJ)I", (void *)android_media_AudioSystem_setLowRamDevice}, |
Eric Laurent | dfb881f | 2013-07-18 14:41:39 -0700 | [diff] [blame] | 2168 | {"checkAudioFlinger", "()I", (void *)android_media_AudioSystem_checkAudioFlinger}, |
Eric Laurent | b69681c | 2014-05-19 19:02:51 -0700 | [diff] [blame] | 2169 | {"listAudioPorts", "(Ljava/util/ArrayList;[I)I", |
| 2170 | (void *)android_media_AudioSystem_listAudioPorts}, |
| 2171 | {"createAudioPatch", "([Landroid/media/AudioPatch;[Landroid/media/AudioPortConfig;[Landroid/media/AudioPortConfig;)I", |
| 2172 | (void *)android_media_AudioSystem_createAudioPatch}, |
| 2173 | {"releaseAudioPatch", "(Landroid/media/AudioPatch;)I", |
| 2174 | (void *)android_media_AudioSystem_releaseAudioPatch}, |
| 2175 | {"listAudioPatches", "(Ljava/util/ArrayList;[I)I", |
| 2176 | (void *)android_media_AudioSystem_listAudioPatches}, |
Eric Laurent | 3a24199 | 2014-05-19 19:33:26 -0700 | [diff] [blame] | 2177 | {"setAudioPortConfig", "(Landroid/media/AudioPortConfig;)I", |
| 2178 | (void *)android_media_AudioSystem_setAudioPortConfig}, |
Hongwei Wang | f44a568 | 2018-08-24 10:37:56 -0700 | [diff] [blame] | 2179 | {"startAudioSource", "(Landroid/media/AudioPortConfig;Landroid/media/AudioAttributes;)I", |
| 2180 | (void *)android_media_AudioSystem_startAudioSource}, |
| 2181 | {"stopAudioSource", "(I)I", (void *)android_media_AudioSystem_stopAudioSource}, |
Eric Laurent | b634e1b | 2014-08-01 14:44:46 -0700 | [diff] [blame] | 2182 | {"getAudioHwSyncForSession", "(I)I", |
| 2183 | (void *)android_media_AudioSystem_getAudioHwSyncForSession}, |
Eric Laurent | 7f5eb9f | 2014-12-01 19:36:30 -0800 | [diff] [blame] | 2184 | {"registerPolicyMixes", "(Ljava/util/ArrayList;Z)I", |
| 2185 | (void *)android_media_AudioSystem_registerPolicyMixes}, |
Jean-Michel Trivi | d6f65de | 2018-12-18 18:49:14 -0800 | [diff] [blame] | 2186 | {"setUidDeviceAffinities", "(I[I[Ljava/lang/String;)I", |
| 2187 | (void *)android_media_AudioSystem_setUidDeviceAffinities}, |
| 2188 | {"removeUidDeviceAffinities", "(I)I", |
| 2189 | (void *)android_media_AudioSystem_removeUidDeviceAffinities}, |
Jean-Michel Trivi | 5a56109 | 2015-04-23 18:48:08 -0700 | [diff] [blame] | 2190 | {"native_register_dynamic_policy_callback", "()V", |
| 2191 | (void *)android_media_AudioSystem_registerDynPolicyCallback}, |
Jean-Michel Trivi | d3c71f0 | 2015-12-07 11:59:31 -0800 | [diff] [blame] | 2192 | {"native_register_recording_callback", "()V", |
| 2193 | (void *)android_media_AudioSystem_registerRecordingCallback}, |
Eric Laurent | 0867bed | 2015-05-20 14:49:08 -0700 | [diff] [blame] | 2194 | {"systemReady", "()I", (void *)android_media_AudioSystem_systemReady}, |
Eric Laurent | eab40d1 | 2017-06-09 12:45:21 -0700 | [diff] [blame] | 2195 | {"getStreamVolumeDB", "(III)F", (void *)android_media_AudioSystem_getStreamVolumeDB}, |
Jean-Michel Trivi | 980d38f | 2018-01-08 15:43:35 -0800 | [diff] [blame] | 2196 | {"native_is_offload_supported", "(IIII)Z", (void *)android_media_AudioSystem_isOffloadSupported}, |
jiabin | c0f4944 | 2018-01-05 10:23:50 -0800 | [diff] [blame] | 2197 | {"getMicrophones", "(Ljava/util/ArrayList;)I", (void *)android_media_AudioSystem_getMicrophones}, |
jiabin | 3994075 | 2018-04-02 18:18:45 -0700 | [diff] [blame] | 2198 | {"getSurroundFormats", "(Ljava/util/Map;Z)I", (void *)android_media_AudioSystem_getSurroundFormats}, |
| 2199 | {"setSurroundFormatEnabled", "(IZ)I", (void *)android_media_AudioSystem_setSurroundFormatEnabled}, |
Eric Laurent | 1c9c1d5 | 2018-10-17 10:06:46 -0700 | [diff] [blame] | 2200 | {"setAssistantUid", "(I)I", (void *)android_media_AudioSystem_setAssistantUid}, |
| 2201 | {"setA11yServicesUids", "([I)I", (void *)android_media_AudioSystem_setA11yServicesUids}, |
jiabin | 66f9e72 | 2018-11-02 16:20:19 -0700 | [diff] [blame] | 2202 | {"isHapticPlaybackSupported", "()Z", (void *)android_media_AudioSystem_isHapticPlaybackSupported}, |
Arun Mirpuri | cb102fa | 2019-01-11 18:39:21 -0800 | [diff] [blame] | 2203 | {"getHwOffloadEncodingFormatsSupportedForA2DP", "(Ljava/util/ArrayList;)I", |
| 2204 | (void*)android_media_AudioSystem_getHwOffloadEncodingFormatsSupportedForA2DP}, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2205 | }; |
| 2206 | |
Daniel Micay | 76f6a86 | 2015-09-19 17:31:01 -0400 | [diff] [blame] | 2207 | static const JNINativeMethod gEventHandlerMethods[] = { |
Eric Laurent | 700e734 | 2014-05-02 18:33:15 -0700 | [diff] [blame] | 2208 | {"native_setup", |
| 2209 | "(Ljava/lang/Object;)V", |
| 2210 | (void *)android_media_AudioSystem_eventHandlerSetup}, |
| 2211 | {"native_finalize", |
| 2212 | "()V", |
| 2213 | (void *)android_media_AudioSystem_eventHandlerFinalize}, |
| 2214 | }; |
| 2215 | |
Paul McLean | b3ae6d9 | 2018-11-05 15:16:59 -0700 | [diff] [blame] | 2216 | static const JNINativeMethod gGetFCC8Methods[] = { |
| 2217 | {"native_get_FCC_8", "()I", (void *)android_media_AudioSystem_get_FCC_8}, |
| 2218 | }; |
| 2219 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2220 | int register_android_media_AudioSystem(JNIEnv *env) |
| 2221 | { |
Paul McLean | b3ae6d9 | 2018-11-05 15:16:59 -0700 | [diff] [blame] | 2222 | // This needs to be done before hooking up methods AudioTrackRoutingProxy (below) |
| 2223 | RegisterMethodsOrDie(env, kClassPathName, gGetFCC8Methods, NELEM(gGetFCC8Methods)); |
| 2224 | |
Andreas Gampe | ed6b9df | 2014-11-20 22:02:20 -0800 | [diff] [blame] | 2225 | jclass arrayListClass = FindClassOrDie(env, "java/util/ArrayList"); |
| 2226 | gArrayListClass = MakeGlobalRefOrDie(env, arrayListClass); |
| 2227 | gArrayListMethods.add = GetMethodIDOrDie(env, arrayListClass, "add", "(Ljava/lang/Object;)Z"); |
Eric Laurent | 0937897 | 2014-12-10 10:42:08 -0800 | [diff] [blame] | 2228 | gArrayListMethods.toArray = GetMethodIDOrDie(env, arrayListClass, "toArray", "()[Ljava/lang/Object;"); |
Eric Laurent | b69681c | 2014-05-19 19:02:51 -0700 | [diff] [blame] | 2229 | |
jiabin | 3994075 | 2018-04-02 18:18:45 -0700 | [diff] [blame] | 2230 | jclass booleanClass = FindClassOrDie(env, "java/lang/Boolean"); |
| 2231 | gBooleanClass = MakeGlobalRefOrDie(env, booleanClass); |
| 2232 | gBooleanCstor = GetMethodIDOrDie(env, booleanClass, "<init>", "(Z)V"); |
| 2233 | |
| 2234 | jclass integerClass = FindClassOrDie(env, "java/lang/Integer"); |
| 2235 | gIntegerClass = MakeGlobalRefOrDie(env, integerClass); |
| 2236 | gIntegerCstor = GetMethodIDOrDie(env, integerClass, "<init>", "(I)V"); |
| 2237 | |
| 2238 | jclass mapClass = FindClassOrDie(env, "java/util/Map"); |
| 2239 | gMapClass = MakeGlobalRefOrDie(env, mapClass); |
| 2240 | gMapPut = GetMethodIDOrDie(env, mapClass, "put", "(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;"); |
| 2241 | |
Andreas Gampe | ed6b9df | 2014-11-20 22:02:20 -0800 | [diff] [blame] | 2242 | jclass audioHandleClass = FindClassOrDie(env, "android/media/AudioHandle"); |
| 2243 | gAudioHandleClass = MakeGlobalRefOrDie(env, audioHandleClass); |
| 2244 | gAudioHandleCstor = GetMethodIDOrDie(env, audioHandleClass, "<init>", "(I)V"); |
| 2245 | gAudioHandleFields.mId = GetFieldIDOrDie(env, audioHandleClass, "mId", "I"); |
Eric Laurent | b69681c | 2014-05-19 19:02:51 -0700 | [diff] [blame] | 2246 | |
Andreas Gampe | ed6b9df | 2014-11-20 22:02:20 -0800 | [diff] [blame] | 2247 | jclass audioPortClass = FindClassOrDie(env, "android/media/AudioPort"); |
| 2248 | gAudioPortClass = MakeGlobalRefOrDie(env, audioPortClass); |
| 2249 | gAudioPortCstor = GetMethodIDOrDie(env, audioPortClass, "<init>", |
Paul McLean | f29e5f3 | 2015-06-15 10:19:22 -0700 | [diff] [blame] | 2250 | "(Landroid/media/AudioHandle;ILjava/lang/String;[I[I[I[I[Landroid/media/AudioGain;)V"); |
Andreas Gampe | ed6b9df | 2014-11-20 22:02:20 -0800 | [diff] [blame] | 2251 | gAudioPortFields.mHandle = GetFieldIDOrDie(env, audioPortClass, "mHandle", |
Eric Laurent | b69681c | 2014-05-19 19:02:51 -0700 | [diff] [blame] | 2252 | "Landroid/media/AudioHandle;"); |
Andreas Gampe | ed6b9df | 2014-11-20 22:02:20 -0800 | [diff] [blame] | 2253 | gAudioPortFields.mRole = GetFieldIDOrDie(env, audioPortClass, "mRole", "I"); |
| 2254 | gAudioPortFields.mGains = GetFieldIDOrDie(env, audioPortClass, "mGains", |
Eric Laurent | b69681c | 2014-05-19 19:02:51 -0700 | [diff] [blame] | 2255 | "[Landroid/media/AudioGain;"); |
Andreas Gampe | ed6b9df | 2014-11-20 22:02:20 -0800 | [diff] [blame] | 2256 | gAudioPortFields.mActiveConfig = GetFieldIDOrDie(env, audioPortClass, "mActiveConfig", |
| 2257 | "Landroid/media/AudioPortConfig;"); |
Eric Laurent | b69681c | 2014-05-19 19:02:51 -0700 | [diff] [blame] | 2258 | |
Andreas Gampe | ed6b9df | 2014-11-20 22:02:20 -0800 | [diff] [blame] | 2259 | jclass audioPortConfigClass = FindClassOrDie(env, "android/media/AudioPortConfig"); |
| 2260 | gAudioPortConfigClass = MakeGlobalRefOrDie(env, audioPortConfigClass); |
| 2261 | gAudioPortConfigCstor = GetMethodIDOrDie(env, audioPortConfigClass, "<init>", |
| 2262 | "(Landroid/media/AudioPort;IIILandroid/media/AudioGainConfig;)V"); |
| 2263 | gAudioPortConfigFields.mPort = GetFieldIDOrDie(env, audioPortConfigClass, "mPort", |
Eric Laurent | b69681c | 2014-05-19 19:02:51 -0700 | [diff] [blame] | 2264 | "Landroid/media/AudioPort;"); |
Andreas Gampe | ed6b9df | 2014-11-20 22:02:20 -0800 | [diff] [blame] | 2265 | gAudioPortConfigFields.mSamplingRate = GetFieldIDOrDie(env, audioPortConfigClass, |
Eric Laurent | b69681c | 2014-05-19 19:02:51 -0700 | [diff] [blame] | 2266 | "mSamplingRate", "I"); |
Andreas Gampe | ed6b9df | 2014-11-20 22:02:20 -0800 | [diff] [blame] | 2267 | gAudioPortConfigFields.mChannelMask = GetFieldIDOrDie(env, audioPortConfigClass, |
Eric Laurent | b69681c | 2014-05-19 19:02:51 -0700 | [diff] [blame] | 2268 | "mChannelMask", "I"); |
Andreas Gampe | ed6b9df | 2014-11-20 22:02:20 -0800 | [diff] [blame] | 2269 | gAudioPortConfigFields.mFormat = GetFieldIDOrDie(env, audioPortConfigClass, "mFormat", "I"); |
| 2270 | gAudioPortConfigFields.mGain = GetFieldIDOrDie(env, audioPortConfigClass, "mGain", |
Eric Laurent | b69681c | 2014-05-19 19:02:51 -0700 | [diff] [blame] | 2271 | "Landroid/media/AudioGainConfig;"); |
Andreas Gampe | ed6b9df | 2014-11-20 22:02:20 -0800 | [diff] [blame] | 2272 | gAudioPortConfigFields.mConfigMask = GetFieldIDOrDie(env, audioPortConfigClass, "mConfigMask", |
| 2273 | "I"); |
Eric Laurent | b69681c | 2014-05-19 19:02:51 -0700 | [diff] [blame] | 2274 | |
Andreas Gampe | ed6b9df | 2014-11-20 22:02:20 -0800 | [diff] [blame] | 2275 | jclass audioDevicePortConfigClass = FindClassOrDie(env, "android/media/AudioDevicePortConfig"); |
| 2276 | gAudioDevicePortConfigClass = MakeGlobalRefOrDie(env, audioDevicePortConfigClass); |
| 2277 | gAudioDevicePortConfigCstor = GetMethodIDOrDie(env, audioDevicePortConfigClass, "<init>", |
| 2278 | "(Landroid/media/AudioDevicePort;IIILandroid/media/AudioGainConfig;)V"); |
Eric Laurent | b69681c | 2014-05-19 19:02:51 -0700 | [diff] [blame] | 2279 | |
Andreas Gampe | ed6b9df | 2014-11-20 22:02:20 -0800 | [diff] [blame] | 2280 | jclass audioMixPortConfigClass = FindClassOrDie(env, "android/media/AudioMixPortConfig"); |
| 2281 | gAudioMixPortConfigClass = MakeGlobalRefOrDie(env, audioMixPortConfigClass); |
| 2282 | gAudioMixPortConfigCstor = GetMethodIDOrDie(env, audioMixPortConfigClass, "<init>", |
| 2283 | "(Landroid/media/AudioMixPort;IIILandroid/media/AudioGainConfig;)V"); |
Eric Laurent | b69681c | 2014-05-19 19:02:51 -0700 | [diff] [blame] | 2284 | |
Andreas Gampe | ed6b9df | 2014-11-20 22:02:20 -0800 | [diff] [blame] | 2285 | jclass audioDevicePortClass = FindClassOrDie(env, "android/media/AudioDevicePort"); |
| 2286 | gAudioDevicePortClass = MakeGlobalRefOrDie(env, audioDevicePortClass); |
| 2287 | gAudioDevicePortCstor = GetMethodIDOrDie(env, audioDevicePortClass, "<init>", |
Paul McLean | f29e5f3 | 2015-06-15 10:19:22 -0700 | [diff] [blame] | 2288 | "(Landroid/media/AudioHandle;Ljava/lang/String;[I[I[I[I[Landroid/media/AudioGain;ILjava/lang/String;)V"); |
Eric Laurent | b69681c | 2014-05-19 19:02:51 -0700 | [diff] [blame] | 2289 | |
Hongwei Wang | f44a568 | 2018-08-24 10:37:56 -0700 | [diff] [blame] | 2290 | // When access AudioPort as AudioDevicePort |
| 2291 | gAudioPortFields.mType = GetFieldIDOrDie(env, audioDevicePortClass, "mType", "I"); |
| 2292 | gAudioPortFields.mAddress = GetFieldIDOrDie(env, audioDevicePortClass, "mAddress", |
| 2293 | "Ljava/lang/String;"); |
| 2294 | |
Andreas Gampe | ed6b9df | 2014-11-20 22:02:20 -0800 | [diff] [blame] | 2295 | jclass audioMixPortClass = FindClassOrDie(env, "android/media/AudioMixPort"); |
| 2296 | gAudioMixPortClass = MakeGlobalRefOrDie(env, audioMixPortClass); |
| 2297 | gAudioMixPortCstor = GetMethodIDOrDie(env, audioMixPortClass, "<init>", |
Paul McLean | f29e5f3 | 2015-06-15 10:19:22 -0700 | [diff] [blame] | 2298 | "(Landroid/media/AudioHandle;IILjava/lang/String;[I[I[I[I[Landroid/media/AudioGain;)V"); |
Eric Laurent | b69681c | 2014-05-19 19:02:51 -0700 | [diff] [blame] | 2299 | |
Andreas Gampe | ed6b9df | 2014-11-20 22:02:20 -0800 | [diff] [blame] | 2300 | jclass audioGainClass = FindClassOrDie(env, "android/media/AudioGain"); |
| 2301 | gAudioGainClass = MakeGlobalRefOrDie(env, audioGainClass); |
| 2302 | gAudioGainCstor = GetMethodIDOrDie(env, audioGainClass, "<init>", "(IIIIIIIII)V"); |
Eric Laurent | b69681c | 2014-05-19 19:02:51 -0700 | [diff] [blame] | 2303 | |
Andreas Gampe | ed6b9df | 2014-11-20 22:02:20 -0800 | [diff] [blame] | 2304 | jclass audioGainConfigClass = FindClassOrDie(env, "android/media/AudioGainConfig"); |
| 2305 | gAudioGainConfigClass = MakeGlobalRefOrDie(env, audioGainConfigClass); |
| 2306 | gAudioGainConfigCstor = GetMethodIDOrDie(env, audioGainConfigClass, "<init>", |
Eric Laurent | b69681c | 2014-05-19 19:02:51 -0700 | [diff] [blame] | 2307 | "(ILandroid/media/AudioGain;II[II)V"); |
Andreas Gampe | ed6b9df | 2014-11-20 22:02:20 -0800 | [diff] [blame] | 2308 | gAudioGainConfigFields.mIndex = GetFieldIDOrDie(env, gAudioGainConfigClass, "mIndex", "I"); |
| 2309 | gAudioGainConfigFields.mMode = GetFieldIDOrDie(env, audioGainConfigClass, "mMode", "I"); |
| 2310 | gAudioGainConfigFields.mChannelMask = GetFieldIDOrDie(env, audioGainConfigClass, "mChannelMask", |
Eric Laurent | b69681c | 2014-05-19 19:02:51 -0700 | [diff] [blame] | 2311 | "I"); |
Andreas Gampe | ed6b9df | 2014-11-20 22:02:20 -0800 | [diff] [blame] | 2312 | gAudioGainConfigFields.mValues = GetFieldIDOrDie(env, audioGainConfigClass, "mValues", "[I"); |
| 2313 | gAudioGainConfigFields.mRampDurationMs = GetFieldIDOrDie(env, audioGainConfigClass, |
Eric Laurent | b69681c | 2014-05-19 19:02:51 -0700 | [diff] [blame] | 2314 | "mRampDurationMs", "I"); |
| 2315 | |
Andreas Gampe | ed6b9df | 2014-11-20 22:02:20 -0800 | [diff] [blame] | 2316 | jclass audioPatchClass = FindClassOrDie(env, "android/media/AudioPatch"); |
| 2317 | gAudioPatchClass = MakeGlobalRefOrDie(env, audioPatchClass); |
| 2318 | gAudioPatchCstor = GetMethodIDOrDie(env, audioPatchClass, "<init>", |
Eric Laurent | b69681c | 2014-05-19 19:02:51 -0700 | [diff] [blame] | 2319 | "(Landroid/media/AudioHandle;[Landroid/media/AudioPortConfig;[Landroid/media/AudioPortConfig;)V"); |
Andreas Gampe | ed6b9df | 2014-11-20 22:02:20 -0800 | [diff] [blame] | 2320 | gAudioPatchFields.mHandle = GetFieldIDOrDie(env, audioPatchClass, "mHandle", |
Eric Laurent | b69681c | 2014-05-19 19:02:51 -0700 | [diff] [blame] | 2321 | "Landroid/media/AudioHandle;"); |
| 2322 | |
Andreas Gampe | ed6b9df | 2014-11-20 22:02:20 -0800 | [diff] [blame] | 2323 | jclass eventHandlerClass = FindClassOrDie(env, kEventHandlerClassPathName); |
Eric Laurent | 2615afb | 2015-03-25 14:51:08 -0700 | [diff] [blame] | 2324 | gAudioPortEventHandlerMethods.postEventFromNative = GetStaticMethodIDOrDie( |
| 2325 | env, eventHandlerClass, "postEventFromNative", |
| 2326 | "(Ljava/lang/Object;IIILjava/lang/Object;)V"); |
| 2327 | gEventHandlerFields.mJniCallback = GetFieldIDOrDie(env, |
| 2328 | eventHandlerClass, "mJniCallback", "J"); |
Eric Laurent | 700e734 | 2014-05-02 18:33:15 -0700 | [diff] [blame] | 2329 | |
Jean-Michel Trivi | d3c71f0 | 2015-12-07 11:59:31 -0800 | [diff] [blame] | 2330 | gAudioPolicyEventHandlerMethods.postDynPolicyEventFromNative = |
Jean-Michel Trivi | 5a56109 | 2015-04-23 18:48:08 -0700 | [diff] [blame] | 2331 | GetStaticMethodIDOrDie(env, env->FindClass(kClassPathName), |
| 2332 | "dynamicPolicyCallbackFromNative", "(ILjava/lang/String;I)V"); |
Jean-Michel Trivi | d3c71f0 | 2015-12-07 11:59:31 -0800 | [diff] [blame] | 2333 | gAudioPolicyEventHandlerMethods.postRecordConfigEventFromNative = |
| 2334 | GetStaticMethodIDOrDie(env, env->FindClass(kClassPathName), |
Eric Laurent | c7a0cdf | 2018-11-30 12:16:52 -0800 | [diff] [blame] | 2335 | "recordingCallbackFromNative", "(IIIIIZ[I[Landroid/media/audiofx/AudioEffect$Descriptor;[Landroid/media/audiofx/AudioEffect$Descriptor;I)V"); |
Jean-Michel Trivi | 5a56109 | 2015-04-23 18:48:08 -0700 | [diff] [blame] | 2336 | |
Eric Laurent | 0937897 | 2014-12-10 10:42:08 -0800 | [diff] [blame] | 2337 | jclass audioMixClass = FindClassOrDie(env, "android/media/audiopolicy/AudioMix"); |
| 2338 | gAudioMixClass = MakeGlobalRefOrDie(env, audioMixClass); |
| 2339 | gAudioMixFields.mRule = GetFieldIDOrDie(env, audioMixClass, "mRule", |
Eric Laurent | 7f5eb9f | 2014-12-01 19:36:30 -0800 | [diff] [blame] | 2340 | "Landroid/media/audiopolicy/AudioMixingRule;"); |
Eric Laurent | 0937897 | 2014-12-10 10:42:08 -0800 | [diff] [blame] | 2341 | gAudioMixFields.mFormat = GetFieldIDOrDie(env, audioMixClass, "mFormat", |
Eric Laurent | 7f5eb9f | 2014-12-01 19:36:30 -0800 | [diff] [blame] | 2342 | "Landroid/media/AudioFormat;"); |
Eric Laurent | 0937897 | 2014-12-10 10:42:08 -0800 | [diff] [blame] | 2343 | gAudioMixFields.mRouteFlags = GetFieldIDOrDie(env, audioMixClass, "mRouteFlags", "I"); |
Jean-Michel Trivi | 4ad3988 | 2016-03-14 18:46:41 -0700 | [diff] [blame] | 2344 | gAudioMixFields.mDeviceType = GetFieldIDOrDie(env, audioMixClass, "mDeviceSystemType", "I"); |
| 2345 | gAudioMixFields.mDeviceAddress = GetFieldIDOrDie(env, audioMixClass, "mDeviceAddress", |
Eric Laurent | 7f5eb9f | 2014-12-01 19:36:30 -0800 | [diff] [blame] | 2346 | "Ljava/lang/String;"); |
Eric Laurent | 0937897 | 2014-12-10 10:42:08 -0800 | [diff] [blame] | 2347 | gAudioMixFields.mMixType = GetFieldIDOrDie(env, audioMixClass, "mMixType", "I"); |
Jean-Michel Trivi | 5a56109 | 2015-04-23 18:48:08 -0700 | [diff] [blame] | 2348 | gAudioMixFields.mCallbackFlags = GetFieldIDOrDie(env, audioMixClass, "mCallbackFlags", "I"); |
Eric Laurent | 7f5eb9f | 2014-12-01 19:36:30 -0800 | [diff] [blame] | 2349 | |
Eric Laurent | 0937897 | 2014-12-10 10:42:08 -0800 | [diff] [blame] | 2350 | jclass audioFormatClass = FindClassOrDie(env, "android/media/AudioFormat"); |
| 2351 | gAudioFormatClass = MakeGlobalRefOrDie(env, audioFormatClass); |
| 2352 | gAudioFormatFields.mEncoding = GetFieldIDOrDie(env, audioFormatClass, "mEncoding", "I"); |
| 2353 | gAudioFormatFields.mSampleRate = GetFieldIDOrDie(env, audioFormatClass, "mSampleRate", "I"); |
| 2354 | gAudioFormatFields.mChannelMask = GetFieldIDOrDie(env, audioFormatClass, "mChannelMask", "I"); |
Eric Laurent | 7f5eb9f | 2014-12-01 19:36:30 -0800 | [diff] [blame] | 2355 | |
Eric Laurent | 0937897 | 2014-12-10 10:42:08 -0800 | [diff] [blame] | 2356 | jclass audioMixingRuleClass = FindClassOrDie(env, "android/media/audiopolicy/AudioMixingRule"); |
| 2357 | gAudioMixingRuleClass = MakeGlobalRefOrDie(env, audioMixingRuleClass); |
| 2358 | gAudioMixingRuleFields.mCriteria = GetFieldIDOrDie(env, audioMixingRuleClass, "mCriteria", |
Eric Laurent | 7f5eb9f | 2014-12-01 19:36:30 -0800 | [diff] [blame] | 2359 | "Ljava/util/ArrayList;"); |
| 2360 | |
Jean-Michel Trivi | ba6b4b4 | 2016-02-10 14:26:35 -0800 | [diff] [blame] | 2361 | jclass audioMixMatchCriterionClass = |
| 2362 | FindClassOrDie(env, "android/media/audiopolicy/AudioMixingRule$AudioMixMatchCriterion"); |
| 2363 | gAudioMixMatchCriterionClass = MakeGlobalRefOrDie(env,audioMixMatchCriterionClass); |
| 2364 | gAudioMixMatchCriterionFields.mAttr = GetFieldIDOrDie(env, audioMixMatchCriterionClass, "mAttr", |
Eric Laurent | 7f5eb9f | 2014-12-01 19:36:30 -0800 | [diff] [blame] | 2365 | "Landroid/media/AudioAttributes;"); |
Jean-Michel Trivi | ba6b4b4 | 2016-02-10 14:26:35 -0800 | [diff] [blame] | 2366 | gAudioMixMatchCriterionFields.mIntProp = GetFieldIDOrDie(env, audioMixMatchCriterionClass, "mIntProp", |
| 2367 | "I"); |
| 2368 | gAudioMixMatchCriterionFields.mRule = GetFieldIDOrDie(env, audioMixMatchCriterionClass, "mRule", |
Eric Laurent | 7f5eb9f | 2014-12-01 19:36:30 -0800 | [diff] [blame] | 2369 | "I"); |
Paul McLean | b3ae6d9 | 2018-11-05 15:16:59 -0700 | [diff] [blame] | 2370 | // AudioTrackRoutingProxy methods |
| 2371 | gClsAudioTrackRoutingProxy = |
| 2372 | android::FindClassOrDie(env, "android/media/AudioTrackRoutingProxy"); |
| 2373 | // make sure this reference doesn't get deleted |
| 2374 | gClsAudioTrackRoutingProxy = (jclass)env->NewGlobalRef(gClsAudioTrackRoutingProxy); |
| 2375 | |
| 2376 | gMidAudioTrackRoutingProxy_ctor = |
| 2377 | android::GetMethodIDOrDie(env, gClsAudioTrackRoutingProxy, "<init>", "(J)V"); |
| 2378 | gMidAudioTrackRoutingProxy_release = |
| 2379 | android::GetMethodIDOrDie(env, gClsAudioTrackRoutingProxy, "native_release", "()V"); |
| 2380 | |
| 2381 | // AudioRecordRoutingProxy |
| 2382 | gClsAudioRecordRoutingProxy = |
| 2383 | android::FindClassOrDie(env, "android/media/AudioRecordRoutingProxy"); |
| 2384 | // make sure this reference doesn't get deleted |
| 2385 | gClsAudioRecordRoutingProxy = (jclass)env->NewGlobalRef(gClsAudioRecordRoutingProxy); |
| 2386 | |
| 2387 | gMidAudioRecordRoutingProxy_ctor = |
| 2388 | android::GetMethodIDOrDie(env, gClsAudioRecordRoutingProxy, "<init>", "(J)V"); |
| 2389 | gMidAudioRecordRoutingProxy_release = |
| 2390 | android::GetMethodIDOrDie(env, gClsAudioRecordRoutingProxy, "native_release", "()V"); |
| 2391 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2392 | AudioSystem::setErrorCallback(android_media_AudioSystem_error_callback); |
Glenn Kasten | 18db49a | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 2393 | |
Andreas Gampe | ed6b9df | 2014-11-20 22:02:20 -0800 | [diff] [blame] | 2394 | RegisterMethodsOrDie(env, kClassPathName, gMethods, NELEM(gMethods)); |
| 2395 | return RegisterMethodsOrDie(env, kEventHandlerClassPathName, gEventHandlerMethods, |
| 2396 | NELEM(gEventHandlerMethods)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2397 | } |