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 | |
Glenn Kasten | c81d31c | 2012-03-13 14:46:23 -0700 | [diff] [blame] | 23 | #include <jni.h> |
| 24 | #include <JNIHelp.h> |
Andreas Gampe | ed6b9df | 2014-11-20 22:02:20 -0800 | [diff] [blame] | 25 | #include "core_jni_helpers.h" |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 26 | |
| 27 | #include <media/AudioSystem.h> |
Eric Laurent | 7f5eb9f | 2014-12-01 19:36:30 -0800 | [diff] [blame] | 28 | #include <media/AudioPolicy.h> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 29 | |
Dima Zavin | 34bb419 | 2011-05-11 14:15:23 -0700 | [diff] [blame] | 30 | #include <system/audio.h> |
Dima Zavin | 290029d | 2011-06-13 18:16:26 -0700 | [diff] [blame] | 31 | #include <system/audio_policy.h> |
Eric Laurent | b69681c | 2014-05-19 19:02:51 -0700 | [diff] [blame] | 32 | #include "android_media_AudioFormat.h" |
| 33 | #include "android_media_AudioErrors.h" |
Dima Zavin | 24fc2fb | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 34 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 35 | // ---------------------------------------------------------------------------- |
| 36 | |
| 37 | using namespace android; |
| 38 | |
Glenn Kasten | ed0079d | 2011-04-04 10:50:50 -0700 | [diff] [blame] | 39 | static const char* const kClassPathName = "android/media/AudioSystem"; |
| 40 | |
Eric Laurent | b69681c | 2014-05-19 19:02:51 -0700 | [diff] [blame] | 41 | static jclass gArrayListClass; |
| 42 | static struct { |
| 43 | jmethodID add; |
Eric Laurent | 7f5eb9f | 2014-12-01 19:36:30 -0800 | [diff] [blame] | 44 | jmethodID toArray; |
Eric Laurent | b69681c | 2014-05-19 19:02:51 -0700 | [diff] [blame] | 45 | } gArrayListMethods; |
| 46 | |
| 47 | static jclass gAudioHandleClass; |
| 48 | static jmethodID gAudioHandleCstor; |
| 49 | static struct { |
| 50 | jfieldID mId; |
| 51 | } gAudioHandleFields; |
| 52 | |
| 53 | static jclass gAudioPortClass; |
| 54 | static jmethodID gAudioPortCstor; |
| 55 | static struct { |
| 56 | jfieldID mHandle; |
| 57 | jfieldID mRole; |
| 58 | jfieldID mGains; |
| 59 | jfieldID mActiveConfig; |
| 60 | // other fields unused by JNI |
| 61 | } gAudioPortFields; |
| 62 | |
| 63 | static jclass gAudioPortConfigClass; |
| 64 | static jmethodID gAudioPortConfigCstor; |
| 65 | static struct { |
| 66 | jfieldID mPort; |
| 67 | jfieldID mSamplingRate; |
| 68 | jfieldID mChannelMask; |
| 69 | jfieldID mFormat; |
| 70 | jfieldID mGain; |
Eric Laurent | 3a24199 | 2014-05-19 19:33:26 -0700 | [diff] [blame] | 71 | jfieldID mConfigMask; |
Eric Laurent | b69681c | 2014-05-19 19:02:51 -0700 | [diff] [blame] | 72 | } gAudioPortConfigFields; |
| 73 | |
| 74 | static jclass gAudioDevicePortClass; |
| 75 | static jmethodID gAudioDevicePortCstor; |
| 76 | |
| 77 | static jclass gAudioDevicePortConfigClass; |
| 78 | static jmethodID gAudioDevicePortConfigCstor; |
| 79 | |
| 80 | static jclass gAudioMixPortClass; |
| 81 | static jmethodID gAudioMixPortCstor; |
| 82 | |
| 83 | static jclass gAudioMixPortConfigClass; |
| 84 | static jmethodID gAudioMixPortConfigCstor; |
| 85 | |
| 86 | static jclass gAudioGainClass; |
| 87 | static jmethodID gAudioGainCstor; |
| 88 | |
| 89 | static jclass gAudioGainConfigClass; |
| 90 | static jmethodID gAudioGainConfigCstor; |
| 91 | static struct { |
| 92 | jfieldID mIndex; |
| 93 | jfieldID mMode; |
| 94 | jfieldID mChannelMask; |
| 95 | jfieldID mValues; |
| 96 | jfieldID mRampDurationMs; |
| 97 | // other fields unused by JNI |
| 98 | } gAudioGainConfigFields; |
| 99 | |
| 100 | static jclass gAudioPatchClass; |
| 101 | static jmethodID gAudioPatchCstor; |
| 102 | static struct { |
| 103 | jfieldID mHandle; |
| 104 | // other fields unused by JNI |
| 105 | } gAudioPatchFields; |
| 106 | |
Eric Laurent | 7f5eb9f | 2014-12-01 19:36:30 -0800 | [diff] [blame] | 107 | static jclass gAudioMixClass; |
| 108 | static struct { |
| 109 | jfieldID mRule; |
| 110 | jfieldID mFormat; |
| 111 | jfieldID mRouteFlags; |
| 112 | jfieldID mRegistrationId; |
| 113 | jfieldID mMixType; |
Jean-Michel Trivi | 5a56109 | 2015-04-23 18:48:08 -0700 | [diff] [blame] | 114 | jfieldID mCallbackFlags; |
Eric Laurent | 7f5eb9f | 2014-12-01 19:36:30 -0800 | [diff] [blame] | 115 | } gAudioMixFields; |
| 116 | |
| 117 | static jclass gAudioFormatClass; |
| 118 | static struct { |
| 119 | jfieldID mEncoding; |
| 120 | jfieldID mSampleRate; |
| 121 | jfieldID mChannelMask; |
| 122 | // other fields unused by JNI |
| 123 | } gAudioFormatFields; |
| 124 | |
| 125 | static jclass gAudioMixingRuleClass; |
| 126 | static struct { |
| 127 | jfieldID mCriteria; |
| 128 | // other fields unused by JNI |
| 129 | } gAudioMixingRuleFields; |
| 130 | |
Jean-Michel Trivi | ba6b4b4 | 2016-02-10 14:26:35 -0800 | [diff] [blame] | 131 | static jclass gAudioMixMatchCriterionClass; |
Eric Laurent | 7f5eb9f | 2014-12-01 19:36:30 -0800 | [diff] [blame] | 132 | static struct { |
| 133 | jfieldID mAttr; |
Jean-Michel Trivi | ba6b4b4 | 2016-02-10 14:26:35 -0800 | [diff] [blame] | 134 | jfieldID mIntProp; |
Eric Laurent | 7f5eb9f | 2014-12-01 19:36:30 -0800 | [diff] [blame] | 135 | jfieldID mRule; |
Jean-Michel Trivi | ba6b4b4 | 2016-02-10 14:26:35 -0800 | [diff] [blame] | 136 | } gAudioMixMatchCriterionFields; |
Eric Laurent | 7f5eb9f | 2014-12-01 19:36:30 -0800 | [diff] [blame] | 137 | |
| 138 | static jclass gAudioAttributesClass; |
| 139 | static struct { |
| 140 | jfieldID mUsage; |
| 141 | jfieldID mSource; |
| 142 | } gAudioAttributesFields; |
| 143 | |
| 144 | |
Eric Laurent | 700e734 | 2014-05-02 18:33:15 -0700 | [diff] [blame] | 145 | static const char* const kEventHandlerClassPathName = |
| 146 | "android/media/AudioPortEventHandler"; |
Eric Laurent | 2615afb | 2015-03-25 14:51:08 -0700 | [diff] [blame] | 147 | static struct { |
| 148 | jfieldID mJniCallback; |
| 149 | } gEventHandlerFields; |
| 150 | static struct { |
| 151 | jmethodID postEventFromNative; |
| 152 | } gAudioPortEventHandlerMethods; |
| 153 | |
Jean-Michel Trivi | 5a56109 | 2015-04-23 18:48:08 -0700 | [diff] [blame] | 154 | static struct { |
| 155 | jmethodID postDynPolicyEventFromNative; |
Jean-Michel Trivi | d3c71f0 | 2015-12-07 11:59:31 -0800 | [diff] [blame] | 156 | jmethodID postRecordConfigEventFromNative; |
| 157 | } gAudioPolicyEventHandlerMethods; |
Jean-Michel Trivi | 5a56109 | 2015-04-23 18:48:08 -0700 | [diff] [blame] | 158 | |
Eric Laurent | 2615afb | 2015-03-25 14:51:08 -0700 | [diff] [blame] | 159 | static Mutex gLock; |
Eric Laurent | b69681c | 2014-05-19 19:02:51 -0700 | [diff] [blame] | 160 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 161 | enum AudioError { |
| 162 | kAudioStatusOk = 0, |
| 163 | kAudioStatusError = 1, |
| 164 | kAudioStatusMediaServerDied = 100 |
| 165 | }; |
| 166 | |
Eric Laurent | 700e734 | 2014-05-02 18:33:15 -0700 | [diff] [blame] | 167 | enum { |
| 168 | AUDIOPORT_EVENT_PORT_LIST_UPDATED = 1, |
| 169 | AUDIOPORT_EVENT_PATCH_LIST_UPDATED = 2, |
| 170 | AUDIOPORT_EVENT_SERVICE_DIED = 3, |
| 171 | }; |
| 172 | |
Eric Laurent | b69681c | 2014-05-19 19:02:51 -0700 | [diff] [blame] | 173 | #define MAX_PORT_GENERATION_SYNC_ATTEMPTS 5 |
| 174 | |
Eric Laurent | 700e734 | 2014-05-02 18:33:15 -0700 | [diff] [blame] | 175 | // ---------------------------------------------------------------------------- |
Jean-Michel Trivi | 5a56109 | 2015-04-23 18:48:08 -0700 | [diff] [blame] | 176 | // ref-counted object for audio port callbacks |
Eric Laurent | 700e734 | 2014-05-02 18:33:15 -0700 | [diff] [blame] | 177 | class JNIAudioPortCallback: public AudioSystem::AudioPortCallback |
| 178 | { |
| 179 | public: |
| 180 | JNIAudioPortCallback(JNIEnv* env, jobject thiz, jobject weak_thiz); |
| 181 | ~JNIAudioPortCallback(); |
| 182 | |
| 183 | virtual void onAudioPortListUpdate(); |
| 184 | virtual void onAudioPatchListUpdate(); |
| 185 | virtual void onServiceDied(); |
| 186 | |
| 187 | private: |
| 188 | void sendEvent(int event); |
| 189 | |
Eric Laurent | 2615afb | 2015-03-25 14:51:08 -0700 | [diff] [blame] | 190 | jclass mClass; // Reference to AudioPortEventHandler class |
| 191 | jobject mObject; // Weak ref to AudioPortEventHandler Java object to call on |
Eric Laurent | 700e734 | 2014-05-02 18:33:15 -0700 | [diff] [blame] | 192 | }; |
| 193 | |
| 194 | JNIAudioPortCallback::JNIAudioPortCallback(JNIEnv* env, jobject thiz, jobject weak_thiz) |
| 195 | { |
| 196 | |
Eric Laurent | 2615afb | 2015-03-25 14:51:08 -0700 | [diff] [blame] | 197 | // Hold onto the AudioPortEventHandler class for use in calling the static method |
Eric Laurent | 700e734 | 2014-05-02 18:33:15 -0700 | [diff] [blame] | 198 | // that posts events to the application thread. |
| 199 | jclass clazz = env->GetObjectClass(thiz); |
| 200 | if (clazz == NULL) { |
| 201 | ALOGE("Can't find class %s", kEventHandlerClassPathName); |
| 202 | return; |
| 203 | } |
| 204 | mClass = (jclass)env->NewGlobalRef(clazz); |
| 205 | |
Eric Laurent | 2615afb | 2015-03-25 14:51:08 -0700 | [diff] [blame] | 206 | // 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] | 207 | // The reference is only used as a proxy for callbacks. |
| 208 | mObject = env->NewGlobalRef(weak_thiz); |
| 209 | } |
| 210 | |
| 211 | JNIAudioPortCallback::~JNIAudioPortCallback() |
| 212 | { |
| 213 | // remove global references |
| 214 | JNIEnv *env = AndroidRuntime::getJNIEnv(); |
Eric Laurent | 449c588 | 2014-11-12 11:07:48 -0800 | [diff] [blame] | 215 | if (env == NULL) { |
| 216 | return; |
| 217 | } |
Eric Laurent | 700e734 | 2014-05-02 18:33:15 -0700 | [diff] [blame] | 218 | env->DeleteGlobalRef(mObject); |
| 219 | env->DeleteGlobalRef(mClass); |
| 220 | } |
| 221 | |
| 222 | void JNIAudioPortCallback::sendEvent(int event) |
| 223 | { |
| 224 | JNIEnv *env = AndroidRuntime::getJNIEnv(); |
Eric Laurent | 449c588 | 2014-11-12 11:07:48 -0800 | [diff] [blame] | 225 | if (env == NULL) { |
| 226 | return; |
| 227 | } |
Eric Laurent | 2615afb | 2015-03-25 14:51:08 -0700 | [diff] [blame] | 228 | env->CallStaticVoidMethod(mClass, gAudioPortEventHandlerMethods.postEventFromNative, mObject, |
Eric Laurent | 700e734 | 2014-05-02 18:33:15 -0700 | [diff] [blame] | 229 | event, 0, 0, NULL); |
| 230 | if (env->ExceptionCheck()) { |
| 231 | ALOGW("An exception occurred while notifying an event."); |
| 232 | env->ExceptionClear(); |
| 233 | } |
| 234 | } |
| 235 | |
| 236 | void JNIAudioPortCallback::onAudioPortListUpdate() |
| 237 | { |
| 238 | sendEvent(AUDIOPORT_EVENT_PORT_LIST_UPDATED); |
| 239 | } |
| 240 | |
| 241 | void JNIAudioPortCallback::onAudioPatchListUpdate() |
| 242 | { |
| 243 | sendEvent(AUDIOPORT_EVENT_PATCH_LIST_UPDATED); |
| 244 | } |
| 245 | |
| 246 | void JNIAudioPortCallback::onServiceDied() |
| 247 | { |
| 248 | sendEvent(AUDIOPORT_EVENT_SERVICE_DIED); |
| 249 | } |
| 250 | |
Eric Laurent | 2615afb | 2015-03-25 14:51:08 -0700 | [diff] [blame] | 251 | static sp<JNIAudioPortCallback> setJniCallback(JNIEnv* env, |
| 252 | jobject thiz, |
| 253 | const sp<JNIAudioPortCallback>& callback) |
| 254 | { |
| 255 | Mutex::Autolock l(gLock); |
| 256 | sp<JNIAudioPortCallback> old = |
| 257 | (JNIAudioPortCallback*)env->GetLongField(thiz, gEventHandlerFields.mJniCallback); |
| 258 | if (callback.get()) { |
| 259 | callback->incStrong((void*)setJniCallback); |
| 260 | } |
| 261 | if (old != 0) { |
| 262 | old->decStrong((void*)setJniCallback); |
| 263 | } |
| 264 | env->SetLongField(thiz, gEventHandlerFields.mJniCallback, (jlong)callback.get()); |
| 265 | return old; |
| 266 | } |
| 267 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 268 | static int check_AudioSystem_Command(status_t status) |
| 269 | { |
Eric Laurent | dfb881f | 2013-07-18 14:41:39 -0700 | [diff] [blame] | 270 | switch (status) { |
| 271 | case DEAD_OBJECT: |
| 272 | return kAudioStatusMediaServerDied; |
| 273 | case NO_ERROR: |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 274 | return kAudioStatusOk; |
Eric Laurent | dfb881f | 2013-07-18 14:41:39 -0700 | [diff] [blame] | 275 | default: |
| 276 | break; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 277 | } |
Eric Laurent | dfb881f | 2013-07-18 14:41:39 -0700 | [diff] [blame] | 278 | return kAudioStatusError; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 279 | } |
| 280 | |
Ashok Bhat | 075e9a1 | 2014-01-06 13:45:09 +0000 | [diff] [blame] | 281 | static jint |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 282 | android_media_AudioSystem_muteMicrophone(JNIEnv *env, jobject thiz, jboolean on) |
| 283 | { |
Ashok Bhat | 075e9a1 | 2014-01-06 13:45:09 +0000 | [diff] [blame] | 284 | return (jint) check_AudioSystem_Command(AudioSystem::muteMicrophone(on)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 285 | } |
| 286 | |
| 287 | static jboolean |
| 288 | android_media_AudioSystem_isMicrophoneMuted(JNIEnv *env, jobject thiz) |
| 289 | { |
| 290 | bool state = false; |
| 291 | AudioSystem::isMicrophoneMuted(&state); |
| 292 | return state; |
| 293 | } |
| 294 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 295 | static jboolean |
Eric Laurent | 25101b0 | 2011-02-02 09:33:30 -0800 | [diff] [blame] | 296 | 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] | 297 | { |
| 298 | bool state = false; |
Glenn Kasten | bc1d77b | 2012-01-12 16:38:12 -0800 | [diff] [blame] | 299 | AudioSystem::isStreamActive((audio_stream_type_t) stream, &state, inPastMs); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 300 | return state; |
| 301 | } |
| 302 | |
Jean-Michel Trivi | d677054 | 2012-10-10 12:03:41 -0700 | [diff] [blame] | 303 | static jboolean |
Jean-Michel Trivi | 679d504 | 2013-02-04 16:24:09 -0800 | [diff] [blame] | 304 | android_media_AudioSystem_isStreamActiveRemotely(JNIEnv *env, jobject thiz, jint stream, |
| 305 | jint inPastMs) |
| 306 | { |
| 307 | bool state = false; |
| 308 | AudioSystem::isStreamActiveRemotely((audio_stream_type_t) stream, &state, inPastMs); |
| 309 | return state; |
| 310 | } |
| 311 | |
| 312 | static jboolean |
Jean-Michel Trivi | d677054 | 2012-10-10 12:03:41 -0700 | [diff] [blame] | 313 | android_media_AudioSystem_isSourceActive(JNIEnv *env, jobject thiz, jint source) |
| 314 | { |
| 315 | bool state = false; |
| 316 | AudioSystem::isSourceActive((audio_source_t) source, &state); |
| 317 | return state; |
| 318 | } |
| 319 | |
Ashok Bhat | 075e9a1 | 2014-01-06 13:45:09 +0000 | [diff] [blame] | 320 | static jint |
Jean-Michel Trivi | 8a21f5d | 2014-06-05 15:03:52 -0700 | [diff] [blame] | 321 | android_media_AudioSystem_newAudioSessionId(JNIEnv *env, jobject thiz) |
| 322 | { |
Eric Laurent | 62dc176 | 2014-07-25 08:13:10 -0700 | [diff] [blame] | 323 | return AudioSystem::newAudioUniqueId(); |
Jean-Michel Trivi | 8a21f5d | 2014-06-05 15:03:52 -0700 | [diff] [blame] | 324 | } |
| 325 | |
| 326 | static jint |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 327 | 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] | 328 | { |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 329 | const jchar* c_keyValuePairs = env->GetStringCritical(keyValuePairs, 0); |
| 330 | String8 c_keyValuePairs8; |
| 331 | if (keyValuePairs) { |
Dan Albert | 6698749 | 2014-11-20 11:41:21 -0800 | [diff] [blame] | 332 | c_keyValuePairs8 = String8( |
| 333 | reinterpret_cast<const char16_t*>(c_keyValuePairs), |
| 334 | env->GetStringLength(keyValuePairs)); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 335 | env->ReleaseStringCritical(keyValuePairs, c_keyValuePairs); |
| 336 | } |
Glenn Kasten | c94752a | 2013-12-19 12:40:33 -0800 | [diff] [blame] | 337 | int status = check_AudioSystem_Command(AudioSystem::setParameters(c_keyValuePairs8)); |
Ashok Bhat | 075e9a1 | 2014-01-06 13:45:09 +0000 | [diff] [blame] | 338 | return (jint) status; |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 339 | } |
| 340 | |
| 341 | static jstring |
| 342 | android_media_AudioSystem_getParameters(JNIEnv *env, jobject thiz, jstring keys) |
| 343 | { |
| 344 | const jchar* c_keys = env->GetStringCritical(keys, 0); |
| 345 | String8 c_keys8; |
| 346 | if (keys) { |
Dan Albert | 6698749 | 2014-11-20 11:41:21 -0800 | [diff] [blame] | 347 | c_keys8 = String8(reinterpret_cast<const char16_t*>(c_keys), |
| 348 | env->GetStringLength(keys)); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 349 | env->ReleaseStringCritical(keys, c_keys); |
| 350 | } |
Glenn Kasten | c94752a | 2013-12-19 12:40:33 -0800 | [diff] [blame] | 351 | return env->NewStringUTF(AudioSystem::getParameters(c_keys8).string()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 352 | } |
| 353 | |
Glenn Kasten | ed0079d | 2011-04-04 10:50:50 -0700 | [diff] [blame] | 354 | static void |
| 355 | android_media_AudioSystem_error_callback(status_t err) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 356 | { |
Eric Laurent | 539719a | 2013-07-18 14:08:00 -0700 | [diff] [blame] | 357 | JNIEnv *env = AndroidRuntime::getJNIEnv(); |
Eric Laurent | 0e7a4d9 | 2010-10-15 12:33:16 -0700 | [diff] [blame] | 358 | if (env == NULL) { |
| 359 | return; |
| 360 | } |
| 361 | |
Glenn Kasten | ed0079d | 2011-04-04 10:50:50 -0700 | [diff] [blame] | 362 | jclass clazz = env->FindClass(kClassPathName); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 363 | |
Eric Laurent | dfb881f | 2013-07-18 14:41:39 -0700 | [diff] [blame] | 364 | env->CallStaticVoidMethod(clazz, env->GetStaticMethodID(clazz, |
| 365 | "errorCallbackFromNative","(I)V"), |
| 366 | check_AudioSystem_Command(err)); |
Viorel Suman | b637ccf | 2014-05-30 16:51:07 +0300 | [diff] [blame] | 367 | |
| 368 | env->DeleteLocalRef(clazz); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 369 | } |
| 370 | |
Jean-Michel Trivi | 5a56109 | 2015-04-23 18:48:08 -0700 | [diff] [blame] | 371 | static void |
| 372 | android_media_AudioSystem_dyn_policy_callback(int event, String8 regId, int val) |
| 373 | { |
| 374 | JNIEnv *env = AndroidRuntime::getJNIEnv(); |
| 375 | if (env == NULL) { |
| 376 | return; |
| 377 | } |
| 378 | |
| 379 | jclass clazz = env->FindClass(kClassPathName); |
| 380 | const char* zechars = regId.string(); |
| 381 | jstring zestring = env->NewStringUTF(zechars); |
| 382 | |
Jean-Michel Trivi | d3c71f0 | 2015-12-07 11:59:31 -0800 | [diff] [blame] | 383 | env->CallStaticVoidMethod(clazz, gAudioPolicyEventHandlerMethods.postDynPolicyEventFromNative, |
Jean-Michel Trivi | 5a56109 | 2015-04-23 18:48:08 -0700 | [diff] [blame] | 384 | event, zestring, val); |
| 385 | |
| 386 | env->ReleaseStringUTFChars(zestring, zechars); |
| 387 | env->DeleteLocalRef(clazz); |
Jean-Michel Trivi | d3c71f0 | 2015-12-07 11:59:31 -0800 | [diff] [blame] | 388 | } |
Jean-Michel Trivi | 5a56109 | 2015-04-23 18:48:08 -0700 | [diff] [blame] | 389 | |
Jean-Michel Trivi | d3c71f0 | 2015-12-07 11:59:31 -0800 | [diff] [blame] | 390 | static void |
Jean-Michel Trivi | 33fd816 | 2016-02-22 10:52:41 -0800 | [diff] [blame] | 391 | android_media_AudioSystem_recording_callback(int event, int session, int source, |
| 392 | const audio_config_base_t *clientConfig, const audio_config_base_t *deviceConfig) |
Jean-Michel Trivi | d3c71f0 | 2015-12-07 11:59:31 -0800 | [diff] [blame] | 393 | { |
| 394 | JNIEnv *env = AndroidRuntime::getJNIEnv(); |
| 395 | if (env == NULL) { |
| 396 | return; |
| 397 | } |
Jean-Michel Trivi | 33fd816 | 2016-02-22 10:52:41 -0800 | [diff] [blame] | 398 | if (clientConfig == NULL || deviceConfig == NULL) { |
| 399 | ALOGE("Unexpected null client/device configurations in recording callback"); |
| 400 | return; |
| 401 | } |
Jean-Michel Trivi | d3c71f0 | 2015-12-07 11:59:31 -0800 | [diff] [blame] | 402 | |
Jean-Michel Trivi | 33fd816 | 2016-02-22 10:52:41 -0800 | [diff] [blame] | 403 | // create an array for 2*3 integers to store the record configurations (client + device) |
| 404 | jintArray recParamArray = env->NewIntArray(6); |
| 405 | if (recParamArray == NULL) { |
| 406 | ALOGE("recording callback: Couldn't allocate int array for configuration data"); |
| 407 | return; |
| 408 | } |
| 409 | jint recParamData[6]; |
| 410 | recParamData[0] = (jint) audioFormatFromNative(clientConfig->format); |
| 411 | // FIXME this doesn't support index-based masks |
| 412 | recParamData[1] = (jint) inChannelMaskFromNative(clientConfig->channel_mask); |
| 413 | recParamData[2] = (jint) clientConfig->sample_rate; |
| 414 | recParamData[3] = (jint) audioFormatFromNative(deviceConfig->format); |
| 415 | // FIXME this doesn't support index-based masks |
| 416 | recParamData[4] = (jint) inChannelMaskFromNative(deviceConfig->channel_mask); |
| 417 | recParamData[5] = (jint) deviceConfig->sample_rate; |
| 418 | env->SetIntArrayRegion(recParamArray, 0, 6, recParamData); |
| 419 | |
| 420 | // callback into java |
Jean-Michel Trivi | d3c71f0 | 2015-12-07 11:59:31 -0800 | [diff] [blame] | 421 | jclass clazz = env->FindClass(kClassPathName); |
| 422 | env->CallStaticVoidMethod(clazz, |
| 423 | gAudioPolicyEventHandlerMethods.postRecordConfigEventFromNative, |
Jean-Michel Trivi | 33fd816 | 2016-02-22 10:52:41 -0800 | [diff] [blame] | 424 | event, session, source, recParamArray); |
Jean-Michel Trivi | d3c71f0 | 2015-12-07 11:59:31 -0800 | [diff] [blame] | 425 | env->DeleteLocalRef(clazz); |
Jean-Michel Trivi | 33fd816 | 2016-02-22 10:52:41 -0800 | [diff] [blame] | 426 | |
| 427 | env->DeleteLocalRef(recParamArray); |
Jean-Michel Trivi | 5a56109 | 2015-04-23 18:48:08 -0700 | [diff] [blame] | 428 | } |
| 429 | |
Ashok Bhat | 075e9a1 | 2014-01-06 13:45:09 +0000 | [diff] [blame] | 430 | static jint |
Paul McLean | 10804eb | 2015-01-28 11:16:35 -0800 | [diff] [blame] | 431 | android_media_AudioSystem_setDeviceConnectionState(JNIEnv *env, jobject thiz, jint device, jint state, jstring device_address, jstring device_name) |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 432 | { |
| 433 | const char *c_address = env->GetStringUTFChars(device_address, NULL); |
Paul McLean | 10804eb | 2015-01-28 11:16:35 -0800 | [diff] [blame] | 434 | const char *c_name = env->GetStringUTFChars(device_name, NULL); |
Dima Zavin | 24fc2fb | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 435 | int status = check_AudioSystem_Command(AudioSystem::setDeviceConnectionState(static_cast <audio_devices_t>(device), |
| 436 | static_cast <audio_policy_dev_state_t>(state), |
Paul McLean | 10804eb | 2015-01-28 11:16:35 -0800 | [diff] [blame] | 437 | c_address, c_name)); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 438 | env->ReleaseStringUTFChars(device_address, c_address); |
Paul McLean | 10804eb | 2015-01-28 11:16:35 -0800 | [diff] [blame] | 439 | env->ReleaseStringUTFChars(device_name, c_name); |
Ashok Bhat | 075e9a1 | 2014-01-06 13:45:09 +0000 | [diff] [blame] | 440 | return (jint) status; |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 441 | } |
| 442 | |
Ashok Bhat | 075e9a1 | 2014-01-06 13:45:09 +0000 | [diff] [blame] | 443 | static jint |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 444 | android_media_AudioSystem_getDeviceConnectionState(JNIEnv *env, jobject thiz, jint device, jstring device_address) |
| 445 | { |
| 446 | const char *c_address = env->GetStringUTFChars(device_address, NULL); |
Dima Zavin | 24fc2fb | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 447 | 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] | 448 | c_address)); |
| 449 | env->ReleaseStringUTFChars(device_address, c_address); |
Ashok Bhat | 075e9a1 | 2014-01-06 13:45:09 +0000 | [diff] [blame] | 450 | return (jint) state; |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 451 | } |
| 452 | |
Ashok Bhat | 075e9a1 | 2014-01-06 13:45:09 +0000 | [diff] [blame] | 453 | static jint |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 454 | android_media_AudioSystem_setPhoneState(JNIEnv *env, jobject thiz, jint state) |
| 455 | { |
Ashok Bhat | 075e9a1 | 2014-01-06 13:45:09 +0000 | [diff] [blame] | 456 | return (jint) check_AudioSystem_Command(AudioSystem::setPhoneState((audio_mode_t) state)); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 457 | } |
| 458 | |
Ashok Bhat | 075e9a1 | 2014-01-06 13:45:09 +0000 | [diff] [blame] | 459 | static jint |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 460 | android_media_AudioSystem_setForceUse(JNIEnv *env, jobject thiz, jint usage, jint config) |
| 461 | { |
Ashok Bhat | 075e9a1 | 2014-01-06 13:45:09 +0000 | [diff] [blame] | 462 | 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] | 463 | static_cast <audio_policy_forced_cfg_t>(config))); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 464 | } |
| 465 | |
Ashok Bhat | 075e9a1 | 2014-01-06 13:45:09 +0000 | [diff] [blame] | 466 | static jint |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 467 | android_media_AudioSystem_getForceUse(JNIEnv *env, jobject thiz, jint usage) |
| 468 | { |
Ashok Bhat | 075e9a1 | 2014-01-06 13:45:09 +0000 | [diff] [blame] | 469 | 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] | 470 | } |
| 471 | |
Ashok Bhat | 075e9a1 | 2014-01-06 13:45:09 +0000 | [diff] [blame] | 472 | static jint |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 473 | android_media_AudioSystem_initStreamVolume(JNIEnv *env, jobject thiz, jint stream, jint indexMin, jint indexMax) |
| 474 | { |
Ashok Bhat | 075e9a1 | 2014-01-06 13:45:09 +0000 | [diff] [blame] | 475 | 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] | 476 | indexMin, |
| 477 | indexMax)); |
| 478 | } |
| 479 | |
Ashok Bhat | 075e9a1 | 2014-01-06 13:45:09 +0000 | [diff] [blame] | 480 | static jint |
Eric Laurent | 9bc8358d | 2011-11-18 16:43:31 -0800 | [diff] [blame] | 481 | android_media_AudioSystem_setStreamVolumeIndex(JNIEnv *env, |
| 482 | jobject thiz, |
| 483 | jint stream, |
| 484 | jint index, |
| 485 | jint device) |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 486 | { |
Ashok Bhat | 075e9a1 | 2014-01-06 13:45:09 +0000 | [diff] [blame] | 487 | return (jint) check_AudioSystem_Command( |
Eric Laurent | 9bc8358d | 2011-11-18 16:43:31 -0800 | [diff] [blame] | 488 | AudioSystem::setStreamVolumeIndex(static_cast <audio_stream_type_t>(stream), |
| 489 | index, |
| 490 | (audio_devices_t)device)); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 491 | } |
| 492 | |
Ashok Bhat | 075e9a1 | 2014-01-06 13:45:09 +0000 | [diff] [blame] | 493 | static jint |
Eric Laurent | 9bc8358d | 2011-11-18 16:43:31 -0800 | [diff] [blame] | 494 | android_media_AudioSystem_getStreamVolumeIndex(JNIEnv *env, |
| 495 | jobject thiz, |
| 496 | jint stream, |
| 497 | jint device) |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 498 | { |
| 499 | int index; |
Eric Laurent | 9bc8358d | 2011-11-18 16:43:31 -0800 | [diff] [blame] | 500 | if (AudioSystem::getStreamVolumeIndex(static_cast <audio_stream_type_t>(stream), |
| 501 | &index, |
| 502 | (audio_devices_t)device) |
| 503 | != NO_ERROR) { |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 504 | index = -1; |
| 505 | } |
Ashok Bhat | 075e9a1 | 2014-01-06 13:45:09 +0000 | [diff] [blame] | 506 | return (jint) index; |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 507 | } |
| 508 | |
Ashok Bhat | 075e9a1 | 2014-01-06 13:45:09 +0000 | [diff] [blame] | 509 | static jint |
Mike Lockwood | cbdb49d | 2011-10-20 12:54:05 -0400 | [diff] [blame] | 510 | android_media_AudioSystem_setMasterVolume(JNIEnv *env, jobject thiz, jfloat value) |
| 511 | { |
Ashok Bhat | 075e9a1 | 2014-01-06 13:45:09 +0000 | [diff] [blame] | 512 | return (jint) check_AudioSystem_Command(AudioSystem::setMasterVolume(value)); |
Mike Lockwood | cbdb49d | 2011-10-20 12:54:05 -0400 | [diff] [blame] | 513 | } |
| 514 | |
| 515 | static jfloat |
| 516 | android_media_AudioSystem_getMasterVolume(JNIEnv *env, jobject thiz) |
| 517 | { |
| 518 | float value; |
| 519 | if (AudioSystem::getMasterVolume(&value) != NO_ERROR) { |
| 520 | value = -1.0; |
| 521 | } |
| 522 | return value; |
| 523 | } |
| 524 | |
Ashok Bhat | 075e9a1 | 2014-01-06 13:45:09 +0000 | [diff] [blame] | 525 | static jint |
Mike Lockwood | cbdb49d | 2011-10-20 12:54:05 -0400 | [diff] [blame] | 526 | android_media_AudioSystem_setMasterMute(JNIEnv *env, jobject thiz, jboolean mute) |
| 527 | { |
Ashok Bhat | 075e9a1 | 2014-01-06 13:45:09 +0000 | [diff] [blame] | 528 | return (jint) check_AudioSystem_Command(AudioSystem::setMasterMute(mute)); |
Mike Lockwood | cbdb49d | 2011-10-20 12:54:05 -0400 | [diff] [blame] | 529 | } |
| 530 | |
Vladimir Marko | c1bcdbb | 2014-10-29 20:34:15 +0000 | [diff] [blame] | 531 | static jboolean |
Mike Lockwood | cbdb49d | 2011-10-20 12:54:05 -0400 | [diff] [blame] | 532 | android_media_AudioSystem_getMasterMute(JNIEnv *env, jobject thiz) |
| 533 | { |
| 534 | bool mute; |
| 535 | if (AudioSystem::getMasterMute(&mute) != NO_ERROR) { |
| 536 | mute = false; |
| 537 | } |
| 538 | return mute; |
| 539 | } |
| 540 | |
Glenn Kasten | 8b4b97a | 2011-02-04 13:54:26 -0800 | [diff] [blame] | 541 | static jint |
Andy Hung | f04b84d | 2015-12-18 17:33:27 -0800 | [diff] [blame] | 542 | android_media_AudioSystem_setMasterMono(JNIEnv *env, jobject thiz, jboolean mono) |
| 543 | { |
| 544 | return (jint) check_AudioSystem_Command(AudioSystem::setMasterMono(mono)); |
| 545 | } |
| 546 | |
| 547 | static jboolean |
| 548 | android_media_AudioSystem_getMasterMono(JNIEnv *env, jobject thiz) |
| 549 | { |
| 550 | bool mono; |
| 551 | if (AudioSystem::getMasterMono(&mono) != NO_ERROR) { |
| 552 | mono = false; |
| 553 | } |
| 554 | return mono; |
| 555 | } |
| 556 | |
| 557 | static jint |
Glenn Kasten | 8b4b97a | 2011-02-04 13:54:26 -0800 | [diff] [blame] | 558 | android_media_AudioSystem_getDevicesForStream(JNIEnv *env, jobject thiz, jint stream) |
| 559 | { |
Dima Zavin | 24fc2fb | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 560 | return (jint) AudioSystem::getDevicesForStream(static_cast <audio_stream_type_t>(stream)); |
Glenn Kasten | 8b4b97a | 2011-02-04 13:54:26 -0800 | [diff] [blame] | 561 | } |
| 562 | |
Glenn Kasten | c6c4365 | 2012-09-24 17:32:30 -0700 | [diff] [blame] | 563 | static jint |
| 564 | android_media_AudioSystem_getPrimaryOutputSamplingRate(JNIEnv *env, jobject clazz) |
| 565 | { |
| 566 | return (jint) AudioSystem::getPrimaryOutputSamplingRate(); |
| 567 | } |
| 568 | |
| 569 | static jint |
| 570 | android_media_AudioSystem_getPrimaryOutputFrameCount(JNIEnv *env, jobject clazz) |
| 571 | { |
| 572 | return (jint) AudioSystem::getPrimaryOutputFrameCount(); |
| 573 | } |
| 574 | |
Oliver Woodman | 61dcdf3 | 2013-06-26 12:43:36 +0100 | [diff] [blame] | 575 | static jint |
| 576 | android_media_AudioSystem_getOutputLatency(JNIEnv *env, jobject clazz, jint stream) |
| 577 | { |
| 578 | uint32_t afLatency; |
| 579 | if (AudioSystem::getOutputLatency(&afLatency, static_cast <audio_stream_type_t>(stream)) |
| 580 | != NO_ERROR) { |
| 581 | afLatency = -1; |
| 582 | } |
| 583 | return (jint) afLatency; |
| 584 | } |
| 585 | |
Glenn Kasten | fd116ad | 2013-07-12 17:10:39 -0700 | [diff] [blame] | 586 | static jint |
| 587 | android_media_AudioSystem_setLowRamDevice(JNIEnv *env, jobject clazz, jboolean isLowRamDevice) |
| 588 | { |
| 589 | return (jint) AudioSystem::setLowRamDevice((bool) isLowRamDevice); |
| 590 | } |
| 591 | |
Ashok Bhat | 075e9a1 | 2014-01-06 13:45:09 +0000 | [diff] [blame] | 592 | static jint |
Eric Laurent | dfb881f | 2013-07-18 14:41:39 -0700 | [diff] [blame] | 593 | android_media_AudioSystem_checkAudioFlinger(JNIEnv *env, jobject clazz) |
| 594 | { |
Ashok Bhat | 075e9a1 | 2014-01-06 13:45:09 +0000 | [diff] [blame] | 595 | return (jint) check_AudioSystem_Command(AudioSystem::checkAudioFlinger()); |
Eric Laurent | dfb881f | 2013-07-18 14:41:39 -0700 | [diff] [blame] | 596 | } |
| 597 | |
Eric Laurent | b69681c | 2014-05-19 19:02:51 -0700 | [diff] [blame] | 598 | |
| 599 | static bool useInChannelMask(audio_port_type_t type, audio_port_role_t role) |
| 600 | { |
| 601 | return ((type == AUDIO_PORT_TYPE_DEVICE) && (role == AUDIO_PORT_ROLE_SOURCE)) || |
| 602 | ((type == AUDIO_PORT_TYPE_MIX) && (role == AUDIO_PORT_ROLE_SINK)); |
| 603 | } |
| 604 | |
Eric Laurent | 3a24199 | 2014-05-19 19:33:26 -0700 | [diff] [blame] | 605 | static void convertAudioGainConfigToNative(JNIEnv *env, |
| 606 | struct audio_gain_config *nAudioGainConfig, |
| 607 | const jobject jAudioGainConfig, |
| 608 | bool useInMask) |
| 609 | { |
| 610 | nAudioGainConfig->index = env->GetIntField(jAudioGainConfig, gAudioGainConfigFields.mIndex); |
| 611 | nAudioGainConfig->mode = env->GetIntField(jAudioGainConfig, gAudioGainConfigFields.mMode); |
| 612 | ALOGV("convertAudioGainConfigToNative got gain index %d", nAudioGainConfig->index); |
| 613 | jint jMask = env->GetIntField(jAudioGainConfig, gAudioGainConfigFields.mChannelMask); |
| 614 | audio_channel_mask_t nMask; |
| 615 | if (useInMask) { |
| 616 | nMask = inChannelMaskToNative(jMask); |
| 617 | ALOGV("convertAudioGainConfigToNative IN mask java %x native %x", jMask, nMask); |
| 618 | } else { |
| 619 | nMask = outChannelMaskToNative(jMask); |
| 620 | ALOGV("convertAudioGainConfigToNative OUT mask java %x native %x", jMask, nMask); |
| 621 | } |
| 622 | nAudioGainConfig->channel_mask = nMask; |
| 623 | nAudioGainConfig->ramp_duration_ms = env->GetIntField(jAudioGainConfig, |
| 624 | gAudioGainConfigFields.mRampDurationMs); |
| 625 | jintArray jValues = (jintArray)env->GetObjectField(jAudioGainConfig, |
| 626 | gAudioGainConfigFields.mValues); |
| 627 | int *nValues = env->GetIntArrayElements(jValues, NULL); |
| 628 | size_t size = env->GetArrayLength(jValues); |
| 629 | memcpy(nAudioGainConfig->values, nValues, size * sizeof(int)); |
| 630 | env->DeleteLocalRef(jValues); |
| 631 | } |
| 632 | |
| 633 | |
Eric Laurent | b69681c | 2014-05-19 19:02:51 -0700 | [diff] [blame] | 634 | static jint convertAudioPortConfigToNative(JNIEnv *env, |
| 635 | struct audio_port_config *nAudioPortConfig, |
Eric Laurent | 6368a7d | 2014-11-19 12:18:32 -0800 | [diff] [blame] | 636 | const jobject jAudioPortConfig, |
| 637 | bool useConfigMask) |
Eric Laurent | b69681c | 2014-05-19 19:02:51 -0700 | [diff] [blame] | 638 | { |
| 639 | jobject jAudioPort = env->GetObjectField(jAudioPortConfig, gAudioPortConfigFields.mPort); |
| 640 | jobject jHandle = env->GetObjectField(jAudioPort, gAudioPortFields.mHandle); |
| 641 | nAudioPortConfig->id = env->GetIntField(jHandle, gAudioHandleFields.mId); |
| 642 | nAudioPortConfig->role = (audio_port_role_t)env->GetIntField(jAudioPort, |
| 643 | gAudioPortFields.mRole); |
| 644 | if (env->IsInstanceOf(jAudioPort, gAudioDevicePortClass)) { |
| 645 | nAudioPortConfig->type = AUDIO_PORT_TYPE_DEVICE; |
| 646 | } else if (env->IsInstanceOf(jAudioPort, gAudioMixPortClass)) { |
| 647 | nAudioPortConfig->type = AUDIO_PORT_TYPE_MIX; |
| 648 | } else { |
| 649 | env->DeleteLocalRef(jAudioPort); |
| 650 | env->DeleteLocalRef(jHandle); |
| 651 | return (jint)AUDIO_JAVA_ERROR; |
| 652 | } |
| 653 | ALOGV("convertAudioPortConfigToNative handle %d role %d type %d", |
| 654 | nAudioPortConfig->id, nAudioPortConfig->role, nAudioPortConfig->type); |
| 655 | |
Eric Laurent | 6368a7d | 2014-11-19 12:18:32 -0800 | [diff] [blame] | 656 | unsigned int configMask = 0; |
| 657 | |
Eric Laurent | b69681c | 2014-05-19 19:02:51 -0700 | [diff] [blame] | 658 | nAudioPortConfig->sample_rate = env->GetIntField(jAudioPortConfig, |
| 659 | gAudioPortConfigFields.mSamplingRate); |
Eric Laurent | 6368a7d | 2014-11-19 12:18:32 -0800 | [diff] [blame] | 660 | if (nAudioPortConfig->sample_rate != 0) { |
| 661 | configMask |= AUDIO_PORT_CONFIG_SAMPLE_RATE; |
| 662 | } |
Eric Laurent | b69681c | 2014-05-19 19:02:51 -0700 | [diff] [blame] | 663 | |
| 664 | bool useInMask = useInChannelMask(nAudioPortConfig->type, nAudioPortConfig->role); |
| 665 | audio_channel_mask_t nMask; |
| 666 | jint jMask = env->GetIntField(jAudioPortConfig, |
| 667 | gAudioPortConfigFields.mChannelMask); |
| 668 | if (useInMask) { |
| 669 | nMask = inChannelMaskToNative(jMask); |
| 670 | ALOGV("convertAudioPortConfigToNative IN mask java %x native %x", jMask, nMask); |
| 671 | } else { |
| 672 | nMask = outChannelMaskToNative(jMask); |
| 673 | ALOGV("convertAudioPortConfigToNative OUT mask java %x native %x", jMask, nMask); |
| 674 | } |
| 675 | nAudioPortConfig->channel_mask = nMask; |
Eric Laurent | 6368a7d | 2014-11-19 12:18:32 -0800 | [diff] [blame] | 676 | if (nAudioPortConfig->channel_mask != AUDIO_CHANNEL_NONE) { |
| 677 | configMask |= AUDIO_PORT_CONFIG_CHANNEL_MASK; |
| 678 | } |
Eric Laurent | b69681c | 2014-05-19 19:02:51 -0700 | [diff] [blame] | 679 | |
| 680 | jint jFormat = env->GetIntField(jAudioPortConfig, gAudioPortConfigFields.mFormat); |
| 681 | audio_format_t nFormat = audioFormatToNative(jFormat); |
| 682 | ALOGV("convertAudioPortConfigToNative format %d native %d", jFormat, nFormat); |
| 683 | nAudioPortConfig->format = nFormat; |
Eric Laurent | 6368a7d | 2014-11-19 12:18:32 -0800 | [diff] [blame] | 684 | if (nAudioPortConfig->format != AUDIO_FORMAT_DEFAULT && |
| 685 | nAudioPortConfig->format != AUDIO_FORMAT_INVALID) { |
| 686 | configMask |= AUDIO_PORT_CONFIG_FORMAT; |
| 687 | } |
| 688 | |
Eric Laurent | b69681c | 2014-05-19 19:02:51 -0700 | [diff] [blame] | 689 | jobject jGain = env->GetObjectField(jAudioPortConfig, gAudioPortConfigFields.mGain); |
| 690 | if (jGain != NULL) { |
Eric Laurent | 3a24199 | 2014-05-19 19:33:26 -0700 | [diff] [blame] | 691 | convertAudioGainConfigToNative(env, &nAudioPortConfig->gain, jGain, useInMask); |
Eric Laurent | b69681c | 2014-05-19 19:02:51 -0700 | [diff] [blame] | 692 | env->DeleteLocalRef(jGain); |
Eric Laurent | 6368a7d | 2014-11-19 12:18:32 -0800 | [diff] [blame] | 693 | configMask |= AUDIO_PORT_CONFIG_GAIN; |
Eric Laurent | b69681c | 2014-05-19 19:02:51 -0700 | [diff] [blame] | 694 | } else { |
| 695 | ALOGV("convertAudioPortConfigToNative no gain"); |
| 696 | nAudioPortConfig->gain.index = -1; |
| 697 | } |
Eric Laurent | 6368a7d | 2014-11-19 12:18:32 -0800 | [diff] [blame] | 698 | if (useConfigMask) { |
| 699 | nAudioPortConfig->config_mask = env->GetIntField(jAudioPortConfig, |
| 700 | gAudioPortConfigFields.mConfigMask); |
| 701 | } else { |
| 702 | nAudioPortConfig->config_mask = configMask; |
| 703 | } |
Eric Laurent | b69681c | 2014-05-19 19:02:51 -0700 | [diff] [blame] | 704 | env->DeleteLocalRef(jAudioPort); |
| 705 | env->DeleteLocalRef(jHandle); |
| 706 | return (jint)AUDIO_JAVA_SUCCESS; |
| 707 | } |
| 708 | |
| 709 | static jint convertAudioPortConfigFromNative(JNIEnv *env, |
| 710 | jobject jAudioPort, |
| 711 | jobject *jAudioPortConfig, |
| 712 | const struct audio_port_config *nAudioPortConfig) |
| 713 | { |
| 714 | jint jStatus = AUDIO_JAVA_SUCCESS; |
| 715 | jobject jAudioGainConfig = NULL; |
| 716 | jobject jAudioGain = NULL; |
| 717 | jintArray jGainValues; |
| 718 | bool audioportCreated = false; |
| 719 | |
| 720 | ALOGV("convertAudioPortConfigFromNative jAudioPort %p", jAudioPort); |
| 721 | |
| 722 | if (jAudioPort == NULL) { |
| 723 | jobject jHandle = env->NewObject(gAudioHandleClass, gAudioHandleCstor, |
| 724 | nAudioPortConfig->id); |
| 725 | |
| 726 | ALOGV("convertAudioPortConfigFromNative handle %d is a %s", nAudioPortConfig->id, |
| 727 | nAudioPortConfig->type == AUDIO_PORT_TYPE_DEVICE ? "device" : "mix"); |
| 728 | |
| 729 | if (jHandle == NULL) { |
| 730 | return (jint)AUDIO_JAVA_ERROR; |
| 731 | } |
| 732 | // create dummy port and port config objects with just the correct handle |
| 733 | // and configuration data. The actual AudioPortConfig objects will be |
| 734 | // constructed by java code with correct class type (device, mix etc...) |
| 735 | // and reference to AudioPort instance in this client |
| 736 | jAudioPort = env->NewObject(gAudioPortClass, gAudioPortCstor, |
Paul McLean | f29e5f3 | 2015-06-15 10:19:22 -0700 | [diff] [blame] | 737 | jHandle, // handle |
| 738 | 0, // role |
| 739 | NULL, // name |
| 740 | NULL, // samplingRates |
| 741 | NULL, // channelMasks |
| 742 | NULL, // channelIndexMasks |
| 743 | NULL, // formats |
| 744 | NULL); // gains |
Eric Laurent | b69681c | 2014-05-19 19:02:51 -0700 | [diff] [blame] | 745 | env->DeleteLocalRef(jHandle); |
| 746 | if (jAudioPort == NULL) { |
| 747 | return (jint)AUDIO_JAVA_ERROR; |
| 748 | } |
| 749 | ALOGV("convertAudioPortConfigFromNative jAudioPort created for handle %d", |
| 750 | nAudioPortConfig->id); |
| 751 | |
| 752 | audioportCreated = true; |
| 753 | } |
| 754 | |
| 755 | bool useInMask = useInChannelMask(nAudioPortConfig->type, nAudioPortConfig->role); |
| 756 | |
| 757 | audio_channel_mask_t nMask; |
| 758 | jint jMask; |
| 759 | |
| 760 | int gainIndex = nAudioPortConfig->gain.index; |
| 761 | if (gainIndex >= 0) { |
| 762 | ALOGV("convertAudioPortConfigFromNative gain found with index %d mode %x", |
| 763 | gainIndex, nAudioPortConfig->gain.mode); |
| 764 | if (audioportCreated) { |
| 765 | ALOGV("convertAudioPortConfigFromNative creating gain"); |
| 766 | jAudioGain = env->NewObject(gAudioGainClass, gAudioGainCstor, |
| 767 | gainIndex, |
| 768 | 0, |
| 769 | 0, |
| 770 | 0, |
| 771 | 0, |
| 772 | 0, |
| 773 | 0, |
| 774 | 0, |
| 775 | 0); |
| 776 | if (jAudioGain == NULL) { |
| 777 | ALOGV("convertAudioPortConfigFromNative creating gain FAILED"); |
| 778 | jStatus = (jint)AUDIO_JAVA_ERROR; |
| 779 | goto exit; |
| 780 | } |
| 781 | } else { |
| 782 | ALOGV("convertAudioPortConfigFromNative reading gain from port"); |
| 783 | jobjectArray jGains = (jobjectArray)env->GetObjectField(jAudioPort, |
| 784 | gAudioPortFields.mGains); |
| 785 | if (jGains == NULL) { |
| 786 | ALOGV("convertAudioPortConfigFromNative could not get gains from port"); |
| 787 | jStatus = (jint)AUDIO_JAVA_ERROR; |
| 788 | goto exit; |
| 789 | } |
| 790 | jAudioGain = env->GetObjectArrayElement(jGains, gainIndex); |
| 791 | env->DeleteLocalRef(jGains); |
| 792 | if (jAudioGain == NULL) { |
| 793 | ALOGV("convertAudioPortConfigFromNative could not get gain at index %d", gainIndex); |
| 794 | jStatus = (jint)AUDIO_JAVA_ERROR; |
| 795 | goto exit; |
| 796 | } |
| 797 | } |
Eric Laurent | 0078c7c | 2014-06-04 09:49:52 -0700 | [diff] [blame] | 798 | int numValues; |
| 799 | if (useInMask) { |
| 800 | numValues = audio_channel_count_from_in_mask(nAudioPortConfig->gain.channel_mask); |
| 801 | } else { |
| 802 | numValues = audio_channel_count_from_out_mask(nAudioPortConfig->gain.channel_mask); |
| 803 | } |
Eric Laurent | b69681c | 2014-05-19 19:02:51 -0700 | [diff] [blame] | 804 | jGainValues = env->NewIntArray(numValues); |
| 805 | if (jGainValues == NULL) { |
| 806 | ALOGV("convertAudioPortConfigFromNative could not create gain values %d", numValues); |
| 807 | jStatus = (jint)AUDIO_JAVA_ERROR; |
| 808 | goto exit; |
| 809 | } |
| 810 | env->SetIntArrayRegion(jGainValues, 0, numValues, |
| 811 | nAudioPortConfig->gain.values); |
| 812 | |
| 813 | nMask = nAudioPortConfig->gain.channel_mask; |
| 814 | if (useInMask) { |
| 815 | jMask = inChannelMaskFromNative(nMask); |
| 816 | ALOGV("convertAudioPortConfigFromNative IN mask java %x native %x", jMask, nMask); |
| 817 | } else { |
| 818 | jMask = outChannelMaskFromNative(nMask); |
| 819 | ALOGV("convertAudioPortConfigFromNative OUT mask java %x native %x", jMask, nMask); |
| 820 | } |
| 821 | |
| 822 | jAudioGainConfig = env->NewObject(gAudioGainConfigClass, |
| 823 | gAudioGainConfigCstor, |
| 824 | gainIndex, |
| 825 | jAudioGain, |
| 826 | nAudioPortConfig->gain.mode, |
| 827 | jMask, |
| 828 | jGainValues, |
| 829 | nAudioPortConfig->gain.ramp_duration_ms); |
| 830 | env->DeleteLocalRef(jGainValues); |
| 831 | if (jAudioGainConfig == NULL) { |
| 832 | ALOGV("convertAudioPortConfigFromNative could not create gain config"); |
| 833 | jStatus = (jint)AUDIO_JAVA_ERROR; |
| 834 | goto exit; |
| 835 | } |
| 836 | } |
| 837 | jclass clazz; |
| 838 | jmethodID methodID; |
| 839 | if (audioportCreated) { |
| 840 | clazz = gAudioPortConfigClass; |
| 841 | methodID = gAudioPortConfigCstor; |
| 842 | ALOGV("convertAudioPortConfigFromNative building a generic port config"); |
| 843 | } else { |
| 844 | if (env->IsInstanceOf(jAudioPort, gAudioDevicePortClass)) { |
| 845 | clazz = gAudioDevicePortConfigClass; |
| 846 | methodID = gAudioDevicePortConfigCstor; |
| 847 | ALOGV("convertAudioPortConfigFromNative building a device config"); |
| 848 | } else if (env->IsInstanceOf(jAudioPort, gAudioMixPortClass)) { |
| 849 | clazz = gAudioMixPortConfigClass; |
| 850 | methodID = gAudioMixPortConfigCstor; |
| 851 | ALOGV("convertAudioPortConfigFromNative building a mix config"); |
| 852 | } else { |
| 853 | jStatus = (jint)AUDIO_JAVA_ERROR; |
| 854 | goto exit; |
| 855 | } |
| 856 | } |
| 857 | nMask = nAudioPortConfig->channel_mask; |
| 858 | if (useInMask) { |
| 859 | jMask = inChannelMaskFromNative(nMask); |
| 860 | ALOGV("convertAudioPortConfigFromNative IN mask java %x native %x", jMask, nMask); |
| 861 | } else { |
| 862 | jMask = outChannelMaskFromNative(nMask); |
| 863 | ALOGV("convertAudioPortConfigFromNative OUT mask java %x native %x", jMask, nMask); |
| 864 | } |
| 865 | |
| 866 | *jAudioPortConfig = env->NewObject(clazz, methodID, |
| 867 | jAudioPort, |
| 868 | nAudioPortConfig->sample_rate, |
| 869 | jMask, |
| 870 | audioFormatFromNative(nAudioPortConfig->format), |
| 871 | jAudioGainConfig); |
| 872 | if (*jAudioPortConfig == NULL) { |
| 873 | ALOGV("convertAudioPortConfigFromNative could not create new port config"); |
| 874 | jStatus = (jint)AUDIO_JAVA_ERROR; |
| 875 | } else { |
| 876 | ALOGV("convertAudioPortConfigFromNative OK"); |
| 877 | } |
| 878 | |
| 879 | exit: |
| 880 | if (audioportCreated) { |
| 881 | env->DeleteLocalRef(jAudioPort); |
| 882 | if (jAudioGain != NULL) { |
| 883 | env->DeleteLocalRef(jAudioGain); |
| 884 | } |
| 885 | } |
| 886 | if (jAudioGainConfig != NULL) { |
| 887 | env->DeleteLocalRef(jAudioGainConfig); |
| 888 | } |
| 889 | return jStatus; |
| 890 | } |
| 891 | |
Paul McLean | 4e5e9e9 | 2015-06-17 10:10:11 -0700 | [diff] [blame] | 892 | static bool hasFormat(int* formats, size_t size, int format) { |
| 893 | for (size_t index = 0; index < size; index++) { |
| 894 | if (formats[index] == format) { |
| 895 | return true; // found |
| 896 | } |
| 897 | } |
| 898 | return false; // not found |
| 899 | } |
| 900 | |
Eric Laurent | b69681c | 2014-05-19 19:02:51 -0700 | [diff] [blame] | 901 | static jint convertAudioPortFromNative(JNIEnv *env, |
| 902 | jobject *jAudioPort, const struct audio_port *nAudioPort) |
| 903 | { |
| 904 | jint jStatus = (jint)AUDIO_JAVA_SUCCESS; |
| 905 | jintArray jSamplingRates = NULL; |
| 906 | jintArray jChannelMasks = NULL; |
Paul McLean | f29e5f3 | 2015-06-15 10:19:22 -0700 | [diff] [blame] | 907 | jintArray jChannelIndexMasks = NULL; |
Paul McLean | 4e5e9e9 | 2015-06-17 10:10:11 -0700 | [diff] [blame] | 908 | int* cFormats = NULL; |
Eric Laurent | b69681c | 2014-05-19 19:02:51 -0700 | [diff] [blame] | 909 | jintArray jFormats = NULL; |
| 910 | jobjectArray jGains = NULL; |
| 911 | jobject jHandle = NULL; |
Paul McLean | 10804eb | 2015-01-28 11:16:35 -0800 | [diff] [blame] | 912 | jstring jDeviceName = NULL; |
Eric Laurent | b69681c | 2014-05-19 19:02:51 -0700 | [diff] [blame] | 913 | bool useInMask; |
Paul McLean | f29e5f3 | 2015-06-15 10:19:22 -0700 | [diff] [blame] | 914 | size_t numPositionMasks = 0; |
| 915 | size_t numIndexMasks = 0; |
Paul McLean | 2db9437 | 2015-06-18 12:47:34 -0700 | [diff] [blame] | 916 | size_t numUniqueFormats = 0; |
| 917 | |
Paul McLean | 10804eb | 2015-01-28 11:16:35 -0800 | [diff] [blame] | 918 | ALOGV("convertAudioPortFromNative id %d role %d type %d name %s", |
| 919 | nAudioPort->id, nAudioPort->role, nAudioPort->type, nAudioPort->name); |
Eric Laurent | b69681c | 2014-05-19 19:02:51 -0700 | [diff] [blame] | 920 | |
| 921 | jSamplingRates = env->NewIntArray(nAudioPort->num_sample_rates); |
| 922 | if (jSamplingRates == NULL) { |
| 923 | jStatus = (jint)AUDIO_JAVA_ERROR; |
| 924 | goto exit; |
| 925 | } |
| 926 | if (nAudioPort->num_sample_rates) { |
| 927 | env->SetIntArrayRegion(jSamplingRates, 0, nAudioPort->num_sample_rates, |
| 928 | (jint *)nAudioPort->sample_rates); |
| 929 | } |
| 930 | |
Paul McLean | f29e5f3 | 2015-06-15 10:19:22 -0700 | [diff] [blame] | 931 | // count up how many masks are positional and indexed |
| 932 | for(size_t index = 0; index < nAudioPort->num_channel_masks; index++) { |
| 933 | const audio_channel_mask_t mask = nAudioPort->channel_masks[index]; |
| 934 | if (audio_channel_mask_get_representation(mask) == AUDIO_CHANNEL_REPRESENTATION_INDEX) { |
| 935 | numIndexMasks++; |
| 936 | } else { |
| 937 | numPositionMasks++; |
| 938 | } |
| 939 | } |
| 940 | |
| 941 | jChannelMasks = env->NewIntArray(numPositionMasks); |
Eric Laurent | b69681c | 2014-05-19 19:02:51 -0700 | [diff] [blame] | 942 | if (jChannelMasks == NULL) { |
| 943 | jStatus = (jint)AUDIO_JAVA_ERROR; |
| 944 | goto exit; |
| 945 | } |
Paul McLean | f29e5f3 | 2015-06-15 10:19:22 -0700 | [diff] [blame] | 946 | jChannelIndexMasks = env->NewIntArray(numIndexMasks); |
| 947 | if (jChannelIndexMasks == NULL) { |
| 948 | jStatus = (jint)AUDIO_JAVA_ERROR; |
| 949 | goto exit; |
| 950 | } |
Eric Laurent | b69681c | 2014-05-19 19:02:51 -0700 | [diff] [blame] | 951 | useInMask = useInChannelMask(nAudioPort->type, nAudioPort->role); |
| 952 | |
Paul McLean | f29e5f3 | 2015-06-15 10:19:22 -0700 | [diff] [blame] | 953 | // put the masks in the output arrays |
| 954 | for (size_t maskIndex = 0, posMaskIndex = 0, indexedMaskIndex = 0; |
| 955 | maskIndex < nAudioPort->num_channel_masks; maskIndex++) { |
| 956 | const audio_channel_mask_t mask = nAudioPort->channel_masks[maskIndex]; |
| 957 | if (audio_channel_mask_get_representation(mask) == AUDIO_CHANNEL_REPRESENTATION_INDEX) { |
| 958 | jint jMask = audio_channel_mask_get_bits(mask); |
| 959 | env->SetIntArrayRegion(jChannelIndexMasks, indexedMaskIndex++, 1, &jMask); |
Eric Laurent | b69681c | 2014-05-19 19:02:51 -0700 | [diff] [blame] | 960 | } else { |
Paul McLean | f29e5f3 | 2015-06-15 10:19:22 -0700 | [diff] [blame] | 961 | jint jMask = useInMask ? inChannelMaskFromNative(mask) |
| 962 | : outChannelMaskFromNative(mask); |
| 963 | env->SetIntArrayRegion(jChannelMasks, posMaskIndex++, 1, &jMask); |
Eric Laurent | b69681c | 2014-05-19 19:02:51 -0700 | [diff] [blame] | 964 | } |
Eric Laurent | b69681c | 2014-05-19 19:02:51 -0700 | [diff] [blame] | 965 | } |
| 966 | |
Paul McLean | f29e5f3 | 2015-06-15 10:19:22 -0700 | [diff] [blame] | 967 | // formats |
Paul McLean | 2db9437 | 2015-06-18 12:47:34 -0700 | [diff] [blame] | 968 | if (nAudioPort->num_formats != 0) { |
| 969 | cFormats = new int[nAudioPort->num_formats]; |
| 970 | for (size_t index = 0; index < nAudioPort->num_formats; index++) { |
| 971 | int format = audioFormatFromNative(nAudioPort->formats[index]); |
| 972 | if (!hasFormat(cFormats, numUniqueFormats, format)) { |
| 973 | cFormats[numUniqueFormats++] = format; |
| 974 | } |
Paul McLean | 4e5e9e9 | 2015-06-17 10:10:11 -0700 | [diff] [blame] | 975 | } |
| 976 | } |
| 977 | jFormats = env->NewIntArray(numUniqueFormats); |
Eric Laurent | b69681c | 2014-05-19 19:02:51 -0700 | [diff] [blame] | 978 | if (jFormats == NULL) { |
| 979 | jStatus = (jint)AUDIO_JAVA_ERROR; |
| 980 | goto exit; |
| 981 | } |
Paul McLean | 2db9437 | 2015-06-18 12:47:34 -0700 | [diff] [blame] | 982 | if (numUniqueFormats != 0) { |
| 983 | env->SetIntArrayRegion(jFormats, 0, numUniqueFormats, cFormats); |
| 984 | } |
Eric Laurent | b69681c | 2014-05-19 19:02:51 -0700 | [diff] [blame] | 985 | |
Paul McLean | f29e5f3 | 2015-06-15 10:19:22 -0700 | [diff] [blame] | 986 | // gains |
Eric Laurent | b69681c | 2014-05-19 19:02:51 -0700 | [diff] [blame] | 987 | jGains = env->NewObjectArray(nAudioPort->num_gains, |
| 988 | gAudioGainClass, NULL); |
| 989 | if (jGains == NULL) { |
| 990 | jStatus = (jint)AUDIO_JAVA_ERROR; |
| 991 | goto exit; |
| 992 | } |
Paul McLean | f29e5f3 | 2015-06-15 10:19:22 -0700 | [diff] [blame] | 993 | |
Eric Laurent | b69681c | 2014-05-19 19:02:51 -0700 | [diff] [blame] | 994 | for (size_t j = 0; j < nAudioPort->num_gains; j++) { |
| 995 | audio_channel_mask_t nMask = nAudioPort->gains[j].channel_mask; |
Paul McLean | f29e5f3 | 2015-06-15 10:19:22 -0700 | [diff] [blame] | 996 | jint jMask; |
Eric Laurent | b69681c | 2014-05-19 19:02:51 -0700 | [diff] [blame] | 997 | if (useInMask) { |
| 998 | jMask = inChannelMaskFromNative(nMask); |
| 999 | ALOGV("convertAudioPortConfigFromNative IN mask java %x native %x", jMask, nMask); |
| 1000 | } else { |
| 1001 | jMask = outChannelMaskFromNative(nMask); |
| 1002 | ALOGV("convertAudioPortConfigFromNative OUT mask java %x native %x", jMask, nMask); |
| 1003 | } |
| 1004 | |
| 1005 | jobject jGain = env->NewObject(gAudioGainClass, gAudioGainCstor, |
| 1006 | j, |
| 1007 | nAudioPort->gains[j].mode, |
| 1008 | jMask, |
| 1009 | nAudioPort->gains[j].min_value, |
| 1010 | nAudioPort->gains[j].max_value, |
| 1011 | nAudioPort->gains[j].default_value, |
| 1012 | nAudioPort->gains[j].step_value, |
| 1013 | nAudioPort->gains[j].min_ramp_ms, |
| 1014 | nAudioPort->gains[j].max_ramp_ms); |
| 1015 | if (jGain == NULL) { |
| 1016 | jStatus = (jint)AUDIO_JAVA_ERROR; |
| 1017 | goto exit; |
| 1018 | } |
| 1019 | env->SetObjectArrayElement(jGains, j, jGain); |
| 1020 | env->DeleteLocalRef(jGain); |
| 1021 | } |
| 1022 | |
| 1023 | jHandle = env->NewObject(gAudioHandleClass, gAudioHandleCstor, |
| 1024 | nAudioPort->id); |
| 1025 | if (jHandle == NULL) { |
| 1026 | jStatus = (jint)AUDIO_JAVA_ERROR; |
| 1027 | goto exit; |
| 1028 | } |
| 1029 | |
Paul McLean | 10804eb | 2015-01-28 11:16:35 -0800 | [diff] [blame] | 1030 | jDeviceName = env->NewStringUTF(nAudioPort->name); |
| 1031 | |
Eric Laurent | b69681c | 2014-05-19 19:02:51 -0700 | [diff] [blame] | 1032 | if (nAudioPort->type == AUDIO_PORT_TYPE_DEVICE) { |
| 1033 | ALOGV("convertAudioPortFromNative is a device %08x", nAudioPort->ext.device.type); |
| 1034 | jstring jAddress = env->NewStringUTF(nAudioPort->ext.device.address); |
| 1035 | *jAudioPort = env->NewObject(gAudioDevicePortClass, gAudioDevicePortCstor, |
Paul McLean | 10804eb | 2015-01-28 11:16:35 -0800 | [diff] [blame] | 1036 | jHandle, jDeviceName, |
Paul McLean | f29e5f3 | 2015-06-15 10:19:22 -0700 | [diff] [blame] | 1037 | jSamplingRates, jChannelMasks, jChannelIndexMasks, |
| 1038 | jFormats, jGains, |
Eric Laurent | b69681c | 2014-05-19 19:02:51 -0700 | [diff] [blame] | 1039 | nAudioPort->ext.device.type, jAddress); |
| 1040 | env->DeleteLocalRef(jAddress); |
| 1041 | } else if (nAudioPort->type == AUDIO_PORT_TYPE_MIX) { |
| 1042 | ALOGV("convertAudioPortFromNative is a mix"); |
| 1043 | *jAudioPort = env->NewObject(gAudioMixPortClass, gAudioMixPortCstor, |
Eric Laurent | 4bcdba8 | 2015-05-01 11:37:49 -0700 | [diff] [blame] | 1044 | jHandle, nAudioPort->ext.mix.handle, |
| 1045 | nAudioPort->role, jDeviceName, |
Paul McLean | f29e5f3 | 2015-06-15 10:19:22 -0700 | [diff] [blame] | 1046 | jSamplingRates, jChannelMasks, jChannelIndexMasks, |
Eric Laurent | b69681c | 2014-05-19 19:02:51 -0700 | [diff] [blame] | 1047 | jFormats, jGains); |
| 1048 | } else { |
| 1049 | ALOGE("convertAudioPortFromNative unknown nAudioPort type %d", nAudioPort->type); |
| 1050 | jStatus = (jint)AUDIO_JAVA_ERROR; |
| 1051 | goto exit; |
| 1052 | } |
| 1053 | if (*jAudioPort == NULL) { |
| 1054 | jStatus = (jint)AUDIO_JAVA_ERROR; |
| 1055 | goto exit; |
| 1056 | } |
| 1057 | |
| 1058 | jobject jAudioPortConfig; |
| 1059 | jStatus = convertAudioPortConfigFromNative(env, |
| 1060 | *jAudioPort, |
| 1061 | &jAudioPortConfig, |
| 1062 | &nAudioPort->active_config); |
| 1063 | if (jStatus != AUDIO_JAVA_SUCCESS) { |
| 1064 | return jStatus; |
| 1065 | } |
| 1066 | |
| 1067 | env->SetObjectField(*jAudioPort, gAudioPortFields.mActiveConfig, jAudioPortConfig); |
| 1068 | |
| 1069 | exit: |
Paul McLean | 10804eb | 2015-01-28 11:16:35 -0800 | [diff] [blame] | 1070 | if (jDeviceName != NULL) { |
| 1071 | env->DeleteLocalRef(jDeviceName); |
| 1072 | } |
Eric Laurent | b69681c | 2014-05-19 19:02:51 -0700 | [diff] [blame] | 1073 | if (jSamplingRates != NULL) { |
| 1074 | env->DeleteLocalRef(jSamplingRates); |
| 1075 | } |
| 1076 | if (jChannelMasks != NULL) { |
| 1077 | env->DeleteLocalRef(jChannelMasks); |
| 1078 | } |
Paul McLean | 4e5e9e9 | 2015-06-17 10:10:11 -0700 | [diff] [blame] | 1079 | if (jChannelIndexMasks != NULL) { |
| 1080 | env->DeleteLocalRef(jChannelIndexMasks); |
| 1081 | } |
| 1082 | if (cFormats != NULL) { |
| 1083 | delete[] cFormats; |
| 1084 | } |
Eric Laurent | b69681c | 2014-05-19 19:02:51 -0700 | [diff] [blame] | 1085 | if (jFormats != NULL) { |
| 1086 | env->DeleteLocalRef(jFormats); |
| 1087 | } |
| 1088 | if (jGains != NULL) { |
| 1089 | env->DeleteLocalRef(jGains); |
| 1090 | } |
| 1091 | if (jHandle != NULL) { |
| 1092 | env->DeleteLocalRef(jHandle); |
| 1093 | } |
| 1094 | |
| 1095 | return jStatus; |
| 1096 | } |
| 1097 | |
| 1098 | |
| 1099 | static jint |
| 1100 | android_media_AudioSystem_listAudioPorts(JNIEnv *env, jobject clazz, |
| 1101 | jobject jPorts, jintArray jGeneration) |
| 1102 | { |
| 1103 | ALOGV("listAudioPorts"); |
| 1104 | |
| 1105 | if (jPorts == NULL) { |
| 1106 | ALOGE("listAudioPorts NULL AudioPort ArrayList"); |
| 1107 | return (jint)AUDIO_JAVA_BAD_VALUE; |
| 1108 | } |
| 1109 | if (!env->IsInstanceOf(jPorts, gArrayListClass)) { |
| 1110 | ALOGE("listAudioPorts not an arraylist"); |
| 1111 | return (jint)AUDIO_JAVA_BAD_VALUE; |
| 1112 | } |
| 1113 | |
| 1114 | if (jGeneration == NULL || env->GetArrayLength(jGeneration) != 1) { |
| 1115 | return (jint)AUDIO_JAVA_BAD_VALUE; |
| 1116 | } |
| 1117 | |
| 1118 | status_t status; |
| 1119 | unsigned int generation1; |
| 1120 | unsigned int generation; |
| 1121 | unsigned int numPorts; |
| 1122 | jint *nGeneration; |
| 1123 | struct audio_port *nPorts = NULL; |
| 1124 | int attempts = MAX_PORT_GENERATION_SYNC_ATTEMPTS; |
| 1125 | |
| 1126 | // get the port count and all the ports until they both return the same generation |
| 1127 | do { |
| 1128 | if (attempts-- < 0) { |
| 1129 | status = TIMED_OUT; |
| 1130 | break; |
| 1131 | } |
| 1132 | |
| 1133 | numPorts = 0; |
| 1134 | status = AudioSystem::listAudioPorts(AUDIO_PORT_ROLE_NONE, |
| 1135 | AUDIO_PORT_TYPE_NONE, |
| 1136 | &numPorts, |
| 1137 | NULL, |
| 1138 | &generation1); |
| 1139 | if (status != NO_ERROR || numPorts == 0) { |
| 1140 | ALOGE_IF(status != NO_ERROR, "AudioSystem::listAudioPorts error %d", status); |
| 1141 | break; |
| 1142 | } |
| 1143 | nPorts = (struct audio_port *)realloc(nPorts, numPorts * sizeof(struct audio_port)); |
| 1144 | |
| 1145 | status = AudioSystem::listAudioPorts(AUDIO_PORT_ROLE_NONE, |
| 1146 | AUDIO_PORT_TYPE_NONE, |
| 1147 | &numPorts, |
| 1148 | nPorts, |
| 1149 | &generation); |
| 1150 | ALOGV("listAudioPorts AudioSystem::listAudioPorts numPorts %d generation %d generation1 %d", |
| 1151 | numPorts, generation, generation1); |
| 1152 | } while (generation1 != generation && status == NO_ERROR); |
| 1153 | |
| 1154 | jint jStatus = nativeToJavaStatus(status); |
| 1155 | if (jStatus != AUDIO_JAVA_SUCCESS) { |
| 1156 | goto exit; |
| 1157 | } |
| 1158 | |
| 1159 | nGeneration = env->GetIntArrayElements(jGeneration, NULL); |
| 1160 | if (nGeneration == NULL) { |
| 1161 | jStatus = (jint)AUDIO_JAVA_ERROR; |
| 1162 | goto exit; |
| 1163 | } |
| 1164 | nGeneration[0] = generation1; |
| 1165 | env->ReleaseIntArrayElements(jGeneration, nGeneration, 0); |
| 1166 | |
| 1167 | for (size_t i = 0; i < numPorts; i++) { |
| 1168 | jobject jAudioPort; |
| 1169 | jStatus = convertAudioPortFromNative(env, &jAudioPort, &nPorts[i]); |
| 1170 | if (jStatus != AUDIO_JAVA_SUCCESS) { |
| 1171 | goto exit; |
| 1172 | } |
| 1173 | env->CallBooleanMethod(jPorts, gArrayListMethods.add, jAudioPort); |
| 1174 | } |
| 1175 | |
| 1176 | exit: |
| 1177 | free(nPorts); |
| 1178 | return jStatus; |
| 1179 | } |
| 1180 | |
| 1181 | static int |
| 1182 | android_media_AudioSystem_createAudioPatch(JNIEnv *env, jobject clazz, |
| 1183 | jobjectArray jPatches, jobjectArray jSources, jobjectArray jSinks) |
| 1184 | { |
| 1185 | status_t status; |
| 1186 | jint jStatus; |
| 1187 | |
| 1188 | ALOGV("createAudioPatch"); |
| 1189 | if (jPatches == NULL || jSources == NULL || jSinks == NULL) { |
| 1190 | return (jint)AUDIO_JAVA_BAD_VALUE; |
| 1191 | } |
| 1192 | |
| 1193 | if (env->GetArrayLength(jPatches) != 1) { |
| 1194 | return (jint)AUDIO_JAVA_BAD_VALUE; |
| 1195 | } |
| 1196 | jint numSources = env->GetArrayLength(jSources); |
| 1197 | if (numSources == 0 || numSources > AUDIO_PATCH_PORTS_MAX) { |
| 1198 | return (jint)AUDIO_JAVA_BAD_VALUE; |
| 1199 | } |
| 1200 | |
| 1201 | jint numSinks = env->GetArrayLength(jSinks); |
| 1202 | if (numSinks == 0 || numSinks > AUDIO_PATCH_PORTS_MAX) { |
| 1203 | return (jint)AUDIO_JAVA_BAD_VALUE; |
| 1204 | } |
| 1205 | |
| 1206 | audio_patch_handle_t handle = (audio_patch_handle_t)0; |
| 1207 | jobject jPatch = env->GetObjectArrayElement(jPatches, 0); |
| 1208 | jobject jPatchHandle = NULL; |
| 1209 | if (jPatch != NULL) { |
| 1210 | if (!env->IsInstanceOf(jPatch, gAudioPatchClass)) { |
| 1211 | return (jint)AUDIO_JAVA_BAD_VALUE; |
| 1212 | } |
| 1213 | jPatchHandle = env->GetObjectField(jPatch, gAudioPatchFields.mHandle); |
| 1214 | handle = (audio_patch_handle_t)env->GetIntField(jPatchHandle, gAudioHandleFields.mId); |
| 1215 | } |
| 1216 | |
| 1217 | struct audio_patch nPatch; |
| 1218 | |
| 1219 | nPatch.id = handle; |
| 1220 | nPatch.num_sources = 0; |
| 1221 | nPatch.num_sinks = 0; |
| 1222 | jobject jSource = NULL; |
| 1223 | jobject jSink = NULL; |
| 1224 | |
| 1225 | for (jint i = 0; i < numSources; i++) { |
| 1226 | jSource = env->GetObjectArrayElement(jSources, i); |
| 1227 | if (!env->IsInstanceOf(jSource, gAudioPortConfigClass)) { |
| 1228 | jStatus = (jint)AUDIO_JAVA_BAD_VALUE; |
| 1229 | goto exit; |
| 1230 | } |
Eric Laurent | 6368a7d | 2014-11-19 12:18:32 -0800 | [diff] [blame] | 1231 | jStatus = convertAudioPortConfigToNative(env, &nPatch.sources[i], jSource, false); |
Eric Laurent | b69681c | 2014-05-19 19:02:51 -0700 | [diff] [blame] | 1232 | env->DeleteLocalRef(jSource); |
| 1233 | jSource = NULL; |
| 1234 | if (jStatus != AUDIO_JAVA_SUCCESS) { |
| 1235 | goto exit; |
| 1236 | } |
| 1237 | nPatch.num_sources++; |
| 1238 | } |
| 1239 | |
| 1240 | for (jint i = 0; i < numSinks; i++) { |
| 1241 | jSink = env->GetObjectArrayElement(jSinks, i); |
| 1242 | if (!env->IsInstanceOf(jSink, gAudioPortConfigClass)) { |
| 1243 | jStatus = (jint)AUDIO_JAVA_BAD_VALUE; |
| 1244 | goto exit; |
| 1245 | } |
Eric Laurent | 6368a7d | 2014-11-19 12:18:32 -0800 | [diff] [blame] | 1246 | jStatus = convertAudioPortConfigToNative(env, &nPatch.sinks[i], jSink, false); |
Eric Laurent | b69681c | 2014-05-19 19:02:51 -0700 | [diff] [blame] | 1247 | env->DeleteLocalRef(jSink); |
| 1248 | jSink = NULL; |
| 1249 | if (jStatus != AUDIO_JAVA_SUCCESS) { |
| 1250 | goto exit; |
| 1251 | } |
| 1252 | nPatch.num_sinks++; |
| 1253 | } |
| 1254 | |
| 1255 | ALOGV("AudioSystem::createAudioPatch"); |
| 1256 | status = AudioSystem::createAudioPatch(&nPatch, &handle); |
| 1257 | ALOGV("AudioSystem::createAudioPatch() returned %d hande %d", status, handle); |
| 1258 | |
| 1259 | jStatus = nativeToJavaStatus(status); |
| 1260 | if (jStatus != AUDIO_JAVA_SUCCESS) { |
| 1261 | goto exit; |
| 1262 | } |
| 1263 | |
| 1264 | if (jPatchHandle == NULL) { |
| 1265 | jPatchHandle = env->NewObject(gAudioHandleClass, gAudioHandleCstor, |
| 1266 | handle); |
| 1267 | if (jPatchHandle == NULL) { |
| 1268 | jStatus = (jint)AUDIO_JAVA_ERROR; |
| 1269 | goto exit; |
| 1270 | } |
| 1271 | jPatch = env->NewObject(gAudioPatchClass, gAudioPatchCstor, jPatchHandle, jSources, jSinks); |
| 1272 | if (jPatch == NULL) { |
| 1273 | jStatus = (jint)AUDIO_JAVA_ERROR; |
| 1274 | goto exit; |
| 1275 | } |
| 1276 | env->SetObjectArrayElement(jPatches, 0, jPatch); |
| 1277 | } else { |
| 1278 | env->SetIntField(jPatchHandle, gAudioHandleFields.mId, handle); |
| 1279 | } |
| 1280 | |
| 1281 | exit: |
| 1282 | if (jPatchHandle != NULL) { |
| 1283 | env->DeleteLocalRef(jPatchHandle); |
| 1284 | } |
| 1285 | if (jPatch != NULL) { |
| 1286 | env->DeleteLocalRef(jPatch); |
| 1287 | } |
| 1288 | if (jSource != NULL) { |
| 1289 | env->DeleteLocalRef(jSource); |
| 1290 | } |
| 1291 | if (jSink != NULL) { |
| 1292 | env->DeleteLocalRef(jSink); |
| 1293 | } |
| 1294 | return jStatus; |
| 1295 | } |
| 1296 | |
Andreas Gampe | 0f0b491 | 2014-11-12 08:03:48 -0800 | [diff] [blame] | 1297 | static jint |
Eric Laurent | b69681c | 2014-05-19 19:02:51 -0700 | [diff] [blame] | 1298 | android_media_AudioSystem_releaseAudioPatch(JNIEnv *env, jobject clazz, |
| 1299 | jobject jPatch) |
| 1300 | { |
| 1301 | ALOGV("releaseAudioPatch"); |
| 1302 | if (jPatch == NULL) { |
| 1303 | return (jint)AUDIO_JAVA_BAD_VALUE; |
| 1304 | } |
| 1305 | |
| 1306 | audio_patch_handle_t handle = (audio_patch_handle_t)0; |
| 1307 | jobject jPatchHandle = NULL; |
| 1308 | if (!env->IsInstanceOf(jPatch, gAudioPatchClass)) { |
| 1309 | return (jint)AUDIO_JAVA_BAD_VALUE; |
| 1310 | } |
| 1311 | jPatchHandle = env->GetObjectField(jPatch, gAudioPatchFields.mHandle); |
| 1312 | handle = (audio_patch_handle_t)env->GetIntField(jPatchHandle, gAudioHandleFields.mId); |
| 1313 | env->DeleteLocalRef(jPatchHandle); |
| 1314 | |
| 1315 | ALOGV("AudioSystem::releaseAudioPatch"); |
| 1316 | status_t status = AudioSystem::releaseAudioPatch(handle); |
| 1317 | ALOGV("AudioSystem::releaseAudioPatch() returned %d", status); |
| 1318 | jint jStatus = nativeToJavaStatus(status); |
Andreas Gampe | 0f0b491 | 2014-11-12 08:03:48 -0800 | [diff] [blame] | 1319 | return jStatus; |
Eric Laurent | b69681c | 2014-05-19 19:02:51 -0700 | [diff] [blame] | 1320 | } |
| 1321 | |
| 1322 | static jint |
| 1323 | android_media_AudioSystem_listAudioPatches(JNIEnv *env, jobject clazz, |
| 1324 | jobject jPatches, jintArray jGeneration) |
| 1325 | { |
| 1326 | ALOGV("listAudioPatches"); |
| 1327 | if (jPatches == NULL) { |
| 1328 | ALOGE("listAudioPatches NULL AudioPatch ArrayList"); |
| 1329 | return (jint)AUDIO_JAVA_BAD_VALUE; |
| 1330 | } |
| 1331 | if (!env->IsInstanceOf(jPatches, gArrayListClass)) { |
| 1332 | ALOGE("listAudioPatches not an arraylist"); |
| 1333 | return (jint)AUDIO_JAVA_BAD_VALUE; |
| 1334 | } |
| 1335 | |
| 1336 | if (jGeneration == NULL || env->GetArrayLength(jGeneration) != 1) { |
| 1337 | return (jint)AUDIO_JAVA_BAD_VALUE; |
| 1338 | } |
| 1339 | |
| 1340 | status_t status; |
| 1341 | unsigned int generation1; |
| 1342 | unsigned int generation; |
| 1343 | unsigned int numPatches; |
| 1344 | jint *nGeneration; |
| 1345 | struct audio_patch *nPatches = NULL; |
| 1346 | jobjectArray jSources = NULL; |
| 1347 | jobject jSource = NULL; |
| 1348 | jobjectArray jSinks = NULL; |
| 1349 | jobject jSink = NULL; |
| 1350 | jobject jPatch = NULL; |
| 1351 | int attempts = MAX_PORT_GENERATION_SYNC_ATTEMPTS; |
| 1352 | |
| 1353 | // get the patch count and all the patches until they both return the same generation |
| 1354 | do { |
| 1355 | if (attempts-- < 0) { |
| 1356 | status = TIMED_OUT; |
| 1357 | break; |
| 1358 | } |
| 1359 | |
| 1360 | numPatches = 0; |
| 1361 | status = AudioSystem::listAudioPatches(&numPatches, |
| 1362 | NULL, |
| 1363 | &generation1); |
| 1364 | if (status != NO_ERROR || numPatches == 0) { |
| 1365 | ALOGE_IF(status != NO_ERROR, "listAudioPatches AudioSystem::listAudioPatches error %d", |
| 1366 | status); |
| 1367 | break; |
| 1368 | } |
| 1369 | nPatches = (struct audio_patch *)realloc(nPatches, numPatches * sizeof(struct audio_patch)); |
| 1370 | |
| 1371 | status = AudioSystem::listAudioPatches(&numPatches, |
| 1372 | nPatches, |
| 1373 | &generation); |
| 1374 | ALOGV("listAudioPatches AudioSystem::listAudioPatches numPatches %d generation %d generation1 %d", |
| 1375 | numPatches, generation, generation1); |
| 1376 | |
| 1377 | } while (generation1 != generation && status == NO_ERROR); |
| 1378 | |
| 1379 | jint jStatus = nativeToJavaStatus(status); |
| 1380 | if (jStatus != AUDIO_JAVA_SUCCESS) { |
| 1381 | goto exit; |
| 1382 | } |
| 1383 | |
| 1384 | nGeneration = env->GetIntArrayElements(jGeneration, NULL); |
| 1385 | if (nGeneration == NULL) { |
| 1386 | jStatus = AUDIO_JAVA_ERROR; |
| 1387 | goto exit; |
| 1388 | } |
| 1389 | nGeneration[0] = generation1; |
| 1390 | env->ReleaseIntArrayElements(jGeneration, nGeneration, 0); |
| 1391 | |
| 1392 | for (size_t i = 0; i < numPatches; i++) { |
| 1393 | jobject patchHandle = env->NewObject(gAudioHandleClass, gAudioHandleCstor, |
| 1394 | nPatches[i].id); |
| 1395 | if (patchHandle == NULL) { |
| 1396 | jStatus = AUDIO_JAVA_ERROR; |
| 1397 | goto exit; |
| 1398 | } |
Dan Albert | 46d8444 | 2014-11-18 16:07:51 -0800 | [diff] [blame] | 1399 | ALOGV("listAudioPatches patch %zu num_sources %d num_sinks %d", |
Eric Laurent | b69681c | 2014-05-19 19:02:51 -0700 | [diff] [blame] | 1400 | i, nPatches[i].num_sources, nPatches[i].num_sinks); |
| 1401 | |
| 1402 | env->SetIntField(patchHandle, gAudioHandleFields.mId, nPatches[i].id); |
| 1403 | |
| 1404 | // load sources |
| 1405 | jSources = env->NewObjectArray(nPatches[i].num_sources, |
| 1406 | gAudioPortConfigClass, NULL); |
| 1407 | if (jSources == NULL) { |
| 1408 | jStatus = AUDIO_JAVA_ERROR; |
| 1409 | goto exit; |
| 1410 | } |
| 1411 | |
| 1412 | for (size_t j = 0; j < nPatches[i].num_sources; j++) { |
| 1413 | jStatus = convertAudioPortConfigFromNative(env, |
| 1414 | NULL, |
| 1415 | &jSource, |
| 1416 | &nPatches[i].sources[j]); |
| 1417 | if (jStatus != AUDIO_JAVA_SUCCESS) { |
| 1418 | goto exit; |
| 1419 | } |
| 1420 | env->SetObjectArrayElement(jSources, j, jSource); |
| 1421 | env->DeleteLocalRef(jSource); |
| 1422 | jSource = NULL; |
Dan Albert | 46d8444 | 2014-11-18 16:07:51 -0800 | [diff] [blame] | 1423 | ALOGV("listAudioPatches patch %zu source %zu is a %s handle %d", |
Eric Laurent | b69681c | 2014-05-19 19:02:51 -0700 | [diff] [blame] | 1424 | i, j, |
| 1425 | nPatches[i].sources[j].type == AUDIO_PORT_TYPE_DEVICE ? "device" : "mix", |
| 1426 | nPatches[i].sources[j].id); |
| 1427 | } |
| 1428 | // load sinks |
| 1429 | jSinks = env->NewObjectArray(nPatches[i].num_sinks, |
| 1430 | gAudioPortConfigClass, NULL); |
| 1431 | if (jSinks == NULL) { |
| 1432 | jStatus = AUDIO_JAVA_ERROR; |
| 1433 | goto exit; |
| 1434 | } |
| 1435 | |
| 1436 | for (size_t j = 0; j < nPatches[i].num_sinks; j++) { |
| 1437 | jStatus = convertAudioPortConfigFromNative(env, |
| 1438 | NULL, |
| 1439 | &jSink, |
| 1440 | &nPatches[i].sinks[j]); |
| 1441 | |
| 1442 | if (jStatus != AUDIO_JAVA_SUCCESS) { |
| 1443 | goto exit; |
| 1444 | } |
| 1445 | env->SetObjectArrayElement(jSinks, j, jSink); |
| 1446 | env->DeleteLocalRef(jSink); |
| 1447 | jSink = NULL; |
Dan Albert | 46d8444 | 2014-11-18 16:07:51 -0800 | [diff] [blame] | 1448 | ALOGV("listAudioPatches patch %zu sink %zu is a %s handle %d", |
Eric Laurent | b69681c | 2014-05-19 19:02:51 -0700 | [diff] [blame] | 1449 | i, j, |
| 1450 | nPatches[i].sinks[j].type == AUDIO_PORT_TYPE_DEVICE ? "device" : "mix", |
| 1451 | nPatches[i].sinks[j].id); |
| 1452 | } |
| 1453 | |
| 1454 | jPatch = env->NewObject(gAudioPatchClass, gAudioPatchCstor, |
| 1455 | patchHandle, jSources, jSinks); |
| 1456 | env->DeleteLocalRef(jSources); |
| 1457 | jSources = NULL; |
| 1458 | env->DeleteLocalRef(jSinks); |
| 1459 | jSinks = NULL; |
| 1460 | if (jPatch == NULL) { |
| 1461 | jStatus = AUDIO_JAVA_ERROR; |
| 1462 | goto exit; |
| 1463 | } |
| 1464 | env->CallBooleanMethod(jPatches, gArrayListMethods.add, jPatch); |
| 1465 | env->DeleteLocalRef(jPatch); |
| 1466 | jPatch = NULL; |
| 1467 | } |
| 1468 | |
| 1469 | exit: |
| 1470 | if (jSources != NULL) { |
| 1471 | env->DeleteLocalRef(jSources); |
| 1472 | } |
| 1473 | if (jSource != NULL) { |
| 1474 | env->DeleteLocalRef(jSource); |
| 1475 | } |
| 1476 | if (jSinks != NULL) { |
| 1477 | env->DeleteLocalRef(jSinks); |
| 1478 | } |
| 1479 | if (jSink != NULL) { |
| 1480 | env->DeleteLocalRef(jSink); |
| 1481 | } |
| 1482 | if (jPatch != NULL) { |
| 1483 | env->DeleteLocalRef(jPatch); |
| 1484 | } |
| 1485 | free(nPatches); |
| 1486 | return jStatus; |
| 1487 | } |
| 1488 | |
Eric Laurent | 3a24199 | 2014-05-19 19:33:26 -0700 | [diff] [blame] | 1489 | static jint |
| 1490 | android_media_AudioSystem_setAudioPortConfig(JNIEnv *env, jobject clazz, |
| 1491 | jobject jAudioPortConfig) |
| 1492 | { |
| 1493 | ALOGV("setAudioPortConfig"); |
| 1494 | if (jAudioPortConfig == NULL) { |
| 1495 | return AUDIO_JAVA_BAD_VALUE; |
| 1496 | } |
| 1497 | if (!env->IsInstanceOf(jAudioPortConfig, gAudioPortConfigClass)) { |
| 1498 | return AUDIO_JAVA_BAD_VALUE; |
| 1499 | } |
| 1500 | struct audio_port_config nAudioPortConfig; |
Eric Laurent | 6368a7d | 2014-11-19 12:18:32 -0800 | [diff] [blame] | 1501 | jint jStatus = convertAudioPortConfigToNative(env, &nAudioPortConfig, jAudioPortConfig, true); |
Eric Laurent | 3a24199 | 2014-05-19 19:33:26 -0700 | [diff] [blame] | 1502 | if (jStatus != AUDIO_JAVA_SUCCESS) { |
| 1503 | return jStatus; |
| 1504 | } |
| 1505 | status_t status = AudioSystem::setAudioPortConfig(&nAudioPortConfig); |
| 1506 | ALOGV("AudioSystem::setAudioPortConfig() returned %d", status); |
| 1507 | jStatus = nativeToJavaStatus(status); |
| 1508 | return jStatus; |
| 1509 | } |
| 1510 | |
Eric Laurent | 700e734 | 2014-05-02 18:33:15 -0700 | [diff] [blame] | 1511 | static void |
| 1512 | android_media_AudioSystem_eventHandlerSetup(JNIEnv *env, jobject thiz, jobject weak_this) |
| 1513 | { |
| 1514 | ALOGV("eventHandlerSetup"); |
| 1515 | |
| 1516 | sp<JNIAudioPortCallback> callback = new JNIAudioPortCallback(env, thiz, weak_this); |
| 1517 | |
Eric Laurent | 2615afb | 2015-03-25 14:51:08 -0700 | [diff] [blame] | 1518 | if (AudioSystem::addAudioPortCallback(callback) == NO_ERROR) { |
| 1519 | setJniCallback(env, thiz, callback); |
| 1520 | } |
Eric Laurent | 700e734 | 2014-05-02 18:33:15 -0700 | [diff] [blame] | 1521 | } |
| 1522 | |
| 1523 | static void |
| 1524 | android_media_AudioSystem_eventHandlerFinalize(JNIEnv *env, jobject thiz) |
| 1525 | { |
| 1526 | ALOGV("eventHandlerFinalize"); |
| 1527 | |
Eric Laurent | 2615afb | 2015-03-25 14:51:08 -0700 | [diff] [blame] | 1528 | sp<JNIAudioPortCallback> callback = setJniCallback(env, thiz, 0); |
Eric Laurent | 700e734 | 2014-05-02 18:33:15 -0700 | [diff] [blame] | 1529 | |
Eric Laurent | 2615afb | 2015-03-25 14:51:08 -0700 | [diff] [blame] | 1530 | if (callback != 0) { |
| 1531 | AudioSystem::removeAudioPortCallback(callback); |
| 1532 | } |
Eric Laurent | 700e734 | 2014-05-02 18:33:15 -0700 | [diff] [blame] | 1533 | } |
| 1534 | |
Eric Laurent | b634e1b | 2014-08-01 14:44:46 -0700 | [diff] [blame] | 1535 | static jint |
| 1536 | android_media_AudioSystem_getAudioHwSyncForSession(JNIEnv *env, jobject thiz, jint sessionId) |
| 1537 | { |
| 1538 | return (jint)AudioSystem::getAudioHwSyncForSession((audio_session_t)sessionId); |
| 1539 | } |
| 1540 | |
Jean-Michel Trivi | 5a56109 | 2015-04-23 18:48:08 -0700 | [diff] [blame] | 1541 | static void |
| 1542 | android_media_AudioSystem_registerDynPolicyCallback(JNIEnv *env, jobject thiz) |
| 1543 | { |
| 1544 | AudioSystem::setDynPolicyCallback(android_media_AudioSystem_dyn_policy_callback); |
| 1545 | } |
Eric Laurent | 7f5eb9f | 2014-12-01 19:36:30 -0800 | [diff] [blame] | 1546 | |
Jean-Michel Trivi | d3c71f0 | 2015-12-07 11:59:31 -0800 | [diff] [blame] | 1547 | static void |
| 1548 | android_media_AudioSystem_registerRecordingCallback(JNIEnv *env, jobject thiz) |
| 1549 | { |
| 1550 | AudioSystem::setRecordConfigCallback(android_media_AudioSystem_recording_callback); |
| 1551 | } |
| 1552 | |
Eric Laurent | 7f5eb9f | 2014-12-01 19:36:30 -0800 | [diff] [blame] | 1553 | |
| 1554 | static jint convertAudioMixToNative(JNIEnv *env, |
| 1555 | AudioMix *nAudioMix, |
| 1556 | const jobject jAudioMix) |
| 1557 | { |
| 1558 | nAudioMix->mMixType = env->GetIntField(jAudioMix, gAudioMixFields.mMixType); |
| 1559 | nAudioMix->mRouteFlags = env->GetIntField(jAudioMix, gAudioMixFields.mRouteFlags); |
| 1560 | |
| 1561 | jstring jRegistrationId = (jstring)env->GetObjectField(jAudioMix, |
| 1562 | gAudioMixFields.mRegistrationId); |
| 1563 | const char *nRegistrationId = env->GetStringUTFChars(jRegistrationId, NULL); |
| 1564 | nAudioMix->mRegistrationId = String8(nRegistrationId); |
| 1565 | env->ReleaseStringUTFChars(jRegistrationId, nRegistrationId); |
| 1566 | env->DeleteLocalRef(jRegistrationId); |
| 1567 | |
Jean-Michel Trivi | 5a56109 | 2015-04-23 18:48:08 -0700 | [diff] [blame] | 1568 | nAudioMix->mCbFlags = env->GetIntField(jAudioMix, gAudioMixFields.mCallbackFlags); |
| 1569 | |
Eric Laurent | 7f5eb9f | 2014-12-01 19:36:30 -0800 | [diff] [blame] | 1570 | jobject jFormat = env->GetObjectField(jAudioMix, gAudioMixFields.mFormat); |
| 1571 | nAudioMix->mFormat.sample_rate = env->GetIntField(jFormat, |
| 1572 | gAudioFormatFields.mSampleRate); |
| 1573 | nAudioMix->mFormat.channel_mask = outChannelMaskToNative(env->GetIntField(jFormat, |
| 1574 | gAudioFormatFields.mChannelMask)); |
| 1575 | nAudioMix->mFormat.format = audioFormatToNative(env->GetIntField(jFormat, |
| 1576 | gAudioFormatFields.mEncoding)); |
| 1577 | env->DeleteLocalRef(jFormat); |
| 1578 | |
| 1579 | jobject jRule = env->GetObjectField(jAudioMix, gAudioMixFields.mRule); |
| 1580 | jobject jRuleCriteria = env->GetObjectField(jRule, gAudioMixingRuleFields.mCriteria); |
| 1581 | env->DeleteLocalRef(jRule); |
| 1582 | jobjectArray jCriteria = (jobjectArray)env->CallObjectMethod(jRuleCriteria, |
| 1583 | gArrayListMethods.toArray); |
| 1584 | env->DeleteLocalRef(jRuleCriteria); |
| 1585 | |
| 1586 | jint numCriteria = env->GetArrayLength(jCriteria); |
| 1587 | if (numCriteria > MAX_CRITERIA_PER_MIX) { |
| 1588 | numCriteria = MAX_CRITERIA_PER_MIX; |
| 1589 | } |
| 1590 | |
| 1591 | for (jint i = 0; i < numCriteria; i++) { |
Jean-Michel Trivi | ba6b4b4 | 2016-02-10 14:26:35 -0800 | [diff] [blame] | 1592 | AudioMixMatchCriterion nCriterion; |
Eric Laurent | 7f5eb9f | 2014-12-01 19:36:30 -0800 | [diff] [blame] | 1593 | |
| 1594 | jobject jCriterion = env->GetObjectArrayElement(jCriteria, i); |
| 1595 | |
Jean-Michel Trivi | ba6b4b4 | 2016-02-10 14:26:35 -0800 | [diff] [blame] | 1596 | nCriterion.mRule = env->GetIntField(jCriterion, gAudioMixMatchCriterionFields.mRule); |
Eric Laurent | 7f5eb9f | 2014-12-01 19:36:30 -0800 | [diff] [blame] | 1597 | |
Jean-Michel Trivi | ba6b4b4 | 2016-02-10 14:26:35 -0800 | [diff] [blame] | 1598 | const uint32_t match_rule = nCriterion.mRule & ~RULE_EXCLUSION_MASK; |
| 1599 | switch (match_rule) { |
| 1600 | case RULE_MATCH_UID: |
| 1601 | nCriterion.mValue.mUid = env->GetIntField(jCriterion, |
| 1602 | gAudioMixMatchCriterionFields.mIntProp); |
| 1603 | break; |
| 1604 | case RULE_MATCH_ATTRIBUTE_USAGE: |
| 1605 | case RULE_MATCH_ATTRIBUTE_CAPTURE_PRESET: { |
| 1606 | jobject jAttributes = env->GetObjectField(jCriterion, gAudioMixMatchCriterionFields.mAttr); |
| 1607 | if (match_rule == RULE_MATCH_ATTRIBUTE_USAGE) { |
| 1608 | nCriterion.mValue.mUsage = (audio_usage_t)env->GetIntField(jAttributes, |
| 1609 | gAudioAttributesFields.mUsage); |
| 1610 | } else { |
| 1611 | nCriterion.mValue.mSource = (audio_source_t)env->GetIntField(jAttributes, |
| 1612 | gAudioAttributesFields.mSource); |
| 1613 | } |
| 1614 | env->DeleteLocalRef(jAttributes); |
| 1615 | } |
| 1616 | break; |
Eric Laurent | 7f5eb9f | 2014-12-01 19:36:30 -0800 | [diff] [blame] | 1617 | } |
Eric Laurent | 7f5eb9f | 2014-12-01 19:36:30 -0800 | [diff] [blame] | 1618 | |
| 1619 | nAudioMix->mCriteria.add(nCriterion); |
| 1620 | env->DeleteLocalRef(jCriterion); |
| 1621 | } |
| 1622 | |
| 1623 | env->DeleteLocalRef(jCriteria); |
| 1624 | |
| 1625 | return (jint)AUDIO_JAVA_SUCCESS; |
| 1626 | } |
| 1627 | |
| 1628 | static jint |
| 1629 | android_media_AudioSystem_registerPolicyMixes(JNIEnv *env, jobject clazz, |
| 1630 | jobject jMixesList, jboolean registration) |
| 1631 | { |
| 1632 | ALOGV("registerPolicyMixes"); |
| 1633 | |
| 1634 | if (jMixesList == NULL) { |
| 1635 | return (jint)AUDIO_JAVA_BAD_VALUE; |
| 1636 | } |
| 1637 | if (!env->IsInstanceOf(jMixesList, gArrayListClass)) { |
| 1638 | return (jint)AUDIO_JAVA_BAD_VALUE; |
| 1639 | } |
| 1640 | jobjectArray jMixes = (jobjectArray)env->CallObjectMethod(jMixesList, |
| 1641 | gArrayListMethods.toArray); |
| 1642 | jint numMixes = env->GetArrayLength(jMixes); |
| 1643 | if (numMixes > MAX_MIXES_PER_POLICY) { |
| 1644 | numMixes = MAX_MIXES_PER_POLICY; |
| 1645 | } |
| 1646 | |
| 1647 | status_t status; |
| 1648 | jint jStatus; |
| 1649 | jobject jAudioMix = NULL; |
| 1650 | Vector <AudioMix> mixes; |
| 1651 | for (jint i = 0; i < numMixes; i++) { |
| 1652 | jAudioMix = env->GetObjectArrayElement(jMixes, i); |
| 1653 | if (!env->IsInstanceOf(jAudioMix, gAudioMixClass)) { |
| 1654 | jStatus = (jint)AUDIO_JAVA_BAD_VALUE; |
| 1655 | goto exit; |
| 1656 | } |
| 1657 | AudioMix mix; |
| 1658 | jStatus = convertAudioMixToNative(env, &mix, jAudioMix); |
| 1659 | env->DeleteLocalRef(jAudioMix); |
| 1660 | jAudioMix = NULL; |
| 1661 | if (jStatus != AUDIO_JAVA_SUCCESS) { |
| 1662 | goto exit; |
| 1663 | } |
| 1664 | mixes.add(mix); |
| 1665 | } |
| 1666 | |
| 1667 | ALOGV("AudioSystem::registerPolicyMixes numMixes %d registration %d", numMixes, registration); |
| 1668 | status = AudioSystem::registerPolicyMixes(mixes, registration); |
| 1669 | ALOGV("AudioSystem::registerPolicyMixes() returned %d", status); |
| 1670 | |
| 1671 | jStatus = nativeToJavaStatus(status); |
| 1672 | if (jStatus != AUDIO_JAVA_SUCCESS) { |
| 1673 | goto exit; |
| 1674 | } |
| 1675 | |
| 1676 | exit: |
| 1677 | if (jAudioMix != NULL) { |
| 1678 | env->DeleteLocalRef(jAudioMix); |
| 1679 | } |
| 1680 | return jStatus; |
| 1681 | } |
| 1682 | |
Eric Laurent | 0867bed | 2015-05-20 14:49:08 -0700 | [diff] [blame] | 1683 | static jint |
| 1684 | android_media_AudioSystem_systemReady(JNIEnv *env, jobject thiz) |
| 1685 | { |
| 1686 | return nativeToJavaStatus(AudioSystem::systemReady()); |
| 1687 | } |
Eric Laurent | 7f5eb9f | 2014-12-01 19:36:30 -0800 | [diff] [blame] | 1688 | |
| 1689 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1690 | // ---------------------------------------------------------------------------- |
| 1691 | |
Daniel Micay | 76f6a86 | 2015-09-19 17:31:01 -0400 | [diff] [blame] | 1692 | static const JNINativeMethod gMethods[] = { |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 1693 | {"setParameters", "(Ljava/lang/String;)I", (void *)android_media_AudioSystem_setParameters}, |
| 1694 | {"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] | 1695 | {"muteMicrophone", "(Z)I", (void *)android_media_AudioSystem_muteMicrophone}, |
| 1696 | {"isMicrophoneMuted", "()Z", (void *)android_media_AudioSystem_isMicrophoneMuted}, |
Jean-Michel Trivi | d677054 | 2012-10-10 12:03:41 -0700 | [diff] [blame] | 1697 | {"isStreamActive", "(II)Z", (void *)android_media_AudioSystem_isStreamActive}, |
Jean-Michel Trivi | 679d504 | 2013-02-04 16:24:09 -0800 | [diff] [blame] | 1698 | {"isStreamActiveRemotely","(II)Z", (void *)android_media_AudioSystem_isStreamActiveRemotely}, |
Jean-Michel Trivi | d677054 | 2012-10-10 12:03:41 -0700 | [diff] [blame] | 1699 | {"isSourceActive", "(I)Z", (void *)android_media_AudioSystem_isSourceActive}, |
Jean-Michel Trivi | 8a21f5d | 2014-06-05 15:03:52 -0700 | [diff] [blame] | 1700 | {"newAudioSessionId", "()I", (void *)android_media_AudioSystem_newAudioSessionId}, |
Paul McLean | 10804eb | 2015-01-28 11:16:35 -0800 | [diff] [blame] | 1701 | {"setDeviceConnectionState", "(IILjava/lang/String;Ljava/lang/String;)I", (void *)android_media_AudioSystem_setDeviceConnectionState}, |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 1702 | {"getDeviceConnectionState", "(ILjava/lang/String;)I", (void *)android_media_AudioSystem_getDeviceConnectionState}, |
| 1703 | {"setPhoneState", "(I)I", (void *)android_media_AudioSystem_setPhoneState}, |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 1704 | {"setForceUse", "(II)I", (void *)android_media_AudioSystem_setForceUse}, |
| 1705 | {"getForceUse", "(I)I", (void *)android_media_AudioSystem_getForceUse}, |
| 1706 | {"initStreamVolume", "(III)I", (void *)android_media_AudioSystem_initStreamVolume}, |
Eric Laurent | 9bc8358d | 2011-11-18 16:43:31 -0800 | [diff] [blame] | 1707 | {"setStreamVolumeIndex","(III)I", (void *)android_media_AudioSystem_setStreamVolumeIndex}, |
| 1708 | {"getStreamVolumeIndex","(II)I", (void *)android_media_AudioSystem_getStreamVolumeIndex}, |
Mike Lockwood | cbdb49d | 2011-10-20 12:54:05 -0400 | [diff] [blame] | 1709 | {"setMasterVolume", "(F)I", (void *)android_media_AudioSystem_setMasterVolume}, |
| 1710 | {"getMasterVolume", "()F", (void *)android_media_AudioSystem_getMasterVolume}, |
| 1711 | {"setMasterMute", "(Z)I", (void *)android_media_AudioSystem_setMasterMute}, |
| 1712 | {"getMasterMute", "()Z", (void *)android_media_AudioSystem_getMasterMute}, |
Andy Hung | f04b84d | 2015-12-18 17:33:27 -0800 | [diff] [blame] | 1713 | {"setMasterMono", "(Z)I", (void *)android_media_AudioSystem_setMasterMono}, |
| 1714 | {"getMasterMono", "()Z", (void *)android_media_AudioSystem_getMasterMono}, |
Glenn Kasten | 8b4b97a | 2011-02-04 13:54:26 -0800 | [diff] [blame] | 1715 | {"getDevicesForStream", "(I)I", (void *)android_media_AudioSystem_getDevicesForStream}, |
Glenn Kasten | c6c4365 | 2012-09-24 17:32:30 -0700 | [diff] [blame] | 1716 | {"getPrimaryOutputSamplingRate", "()I", (void *)android_media_AudioSystem_getPrimaryOutputSamplingRate}, |
| 1717 | {"getPrimaryOutputFrameCount", "()I", (void *)android_media_AudioSystem_getPrimaryOutputFrameCount}, |
Oliver Woodman | 61dcdf3 | 2013-06-26 12:43:36 +0100 | [diff] [blame] | 1718 | {"getOutputLatency", "(I)I", (void *)android_media_AudioSystem_getOutputLatency}, |
Glenn Kasten | fd116ad | 2013-07-12 17:10:39 -0700 | [diff] [blame] | 1719 | {"setLowRamDevice", "(Z)I", (void *)android_media_AudioSystem_setLowRamDevice}, |
Eric Laurent | dfb881f | 2013-07-18 14:41:39 -0700 | [diff] [blame] | 1720 | {"checkAudioFlinger", "()I", (void *)android_media_AudioSystem_checkAudioFlinger}, |
Eric Laurent | b69681c | 2014-05-19 19:02:51 -0700 | [diff] [blame] | 1721 | {"listAudioPorts", "(Ljava/util/ArrayList;[I)I", |
| 1722 | (void *)android_media_AudioSystem_listAudioPorts}, |
| 1723 | {"createAudioPatch", "([Landroid/media/AudioPatch;[Landroid/media/AudioPortConfig;[Landroid/media/AudioPortConfig;)I", |
| 1724 | (void *)android_media_AudioSystem_createAudioPatch}, |
| 1725 | {"releaseAudioPatch", "(Landroid/media/AudioPatch;)I", |
| 1726 | (void *)android_media_AudioSystem_releaseAudioPatch}, |
| 1727 | {"listAudioPatches", "(Ljava/util/ArrayList;[I)I", |
| 1728 | (void *)android_media_AudioSystem_listAudioPatches}, |
Eric Laurent | 3a24199 | 2014-05-19 19:33:26 -0700 | [diff] [blame] | 1729 | {"setAudioPortConfig", "(Landroid/media/AudioPortConfig;)I", |
| 1730 | (void *)android_media_AudioSystem_setAudioPortConfig}, |
Eric Laurent | b634e1b | 2014-08-01 14:44:46 -0700 | [diff] [blame] | 1731 | {"getAudioHwSyncForSession", "(I)I", |
| 1732 | (void *)android_media_AudioSystem_getAudioHwSyncForSession}, |
Eric Laurent | 7f5eb9f | 2014-12-01 19:36:30 -0800 | [diff] [blame] | 1733 | {"registerPolicyMixes", "(Ljava/util/ArrayList;Z)I", |
| 1734 | (void *)android_media_AudioSystem_registerPolicyMixes}, |
Jean-Michel Trivi | 5a56109 | 2015-04-23 18:48:08 -0700 | [diff] [blame] | 1735 | {"native_register_dynamic_policy_callback", "()V", |
| 1736 | (void *)android_media_AudioSystem_registerDynPolicyCallback}, |
Jean-Michel Trivi | d3c71f0 | 2015-12-07 11:59:31 -0800 | [diff] [blame] | 1737 | {"native_register_recording_callback", "()V", |
| 1738 | (void *)android_media_AudioSystem_registerRecordingCallback}, |
Eric Laurent | 0867bed | 2015-05-20 14:49:08 -0700 | [diff] [blame] | 1739 | {"systemReady", "()I", (void *)android_media_AudioSystem_systemReady}, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1740 | }; |
| 1741 | |
Eric Laurent | b69681c | 2014-05-19 19:02:51 -0700 | [diff] [blame] | 1742 | |
Daniel Micay | 76f6a86 | 2015-09-19 17:31:01 -0400 | [diff] [blame] | 1743 | static const JNINativeMethod gEventHandlerMethods[] = { |
Eric Laurent | 700e734 | 2014-05-02 18:33:15 -0700 | [diff] [blame] | 1744 | {"native_setup", |
| 1745 | "(Ljava/lang/Object;)V", |
| 1746 | (void *)android_media_AudioSystem_eventHandlerSetup}, |
| 1747 | {"native_finalize", |
| 1748 | "()V", |
| 1749 | (void *)android_media_AudioSystem_eventHandlerFinalize}, |
| 1750 | }; |
| 1751 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1752 | int register_android_media_AudioSystem(JNIEnv *env) |
| 1753 | { |
Andreas Gampe | ed6b9df | 2014-11-20 22:02:20 -0800 | [diff] [blame] | 1754 | jclass arrayListClass = FindClassOrDie(env, "java/util/ArrayList"); |
| 1755 | gArrayListClass = MakeGlobalRefOrDie(env, arrayListClass); |
| 1756 | gArrayListMethods.add = GetMethodIDOrDie(env, arrayListClass, "add", "(Ljava/lang/Object;)Z"); |
Eric Laurent | 0937897 | 2014-12-10 10:42:08 -0800 | [diff] [blame] | 1757 | gArrayListMethods.toArray = GetMethodIDOrDie(env, arrayListClass, "toArray", "()[Ljava/lang/Object;"); |
Eric Laurent | b69681c | 2014-05-19 19:02:51 -0700 | [diff] [blame] | 1758 | |
Andreas Gampe | ed6b9df | 2014-11-20 22:02:20 -0800 | [diff] [blame] | 1759 | jclass audioHandleClass = FindClassOrDie(env, "android/media/AudioHandle"); |
| 1760 | gAudioHandleClass = MakeGlobalRefOrDie(env, audioHandleClass); |
| 1761 | gAudioHandleCstor = GetMethodIDOrDie(env, audioHandleClass, "<init>", "(I)V"); |
| 1762 | gAudioHandleFields.mId = GetFieldIDOrDie(env, audioHandleClass, "mId", "I"); |
Eric Laurent | b69681c | 2014-05-19 19:02:51 -0700 | [diff] [blame] | 1763 | |
Andreas Gampe | ed6b9df | 2014-11-20 22:02:20 -0800 | [diff] [blame] | 1764 | jclass audioPortClass = FindClassOrDie(env, "android/media/AudioPort"); |
| 1765 | gAudioPortClass = MakeGlobalRefOrDie(env, audioPortClass); |
| 1766 | gAudioPortCstor = GetMethodIDOrDie(env, audioPortClass, "<init>", |
Paul McLean | f29e5f3 | 2015-06-15 10:19:22 -0700 | [diff] [blame] | 1767 | "(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] | 1768 | gAudioPortFields.mHandle = GetFieldIDOrDie(env, audioPortClass, "mHandle", |
Eric Laurent | b69681c | 2014-05-19 19:02:51 -0700 | [diff] [blame] | 1769 | "Landroid/media/AudioHandle;"); |
Andreas Gampe | ed6b9df | 2014-11-20 22:02:20 -0800 | [diff] [blame] | 1770 | gAudioPortFields.mRole = GetFieldIDOrDie(env, audioPortClass, "mRole", "I"); |
| 1771 | gAudioPortFields.mGains = GetFieldIDOrDie(env, audioPortClass, "mGains", |
Eric Laurent | b69681c | 2014-05-19 19:02:51 -0700 | [diff] [blame] | 1772 | "[Landroid/media/AudioGain;"); |
Andreas Gampe | ed6b9df | 2014-11-20 22:02:20 -0800 | [diff] [blame] | 1773 | gAudioPortFields.mActiveConfig = GetFieldIDOrDie(env, audioPortClass, "mActiveConfig", |
| 1774 | "Landroid/media/AudioPortConfig;"); |
Eric Laurent | b69681c | 2014-05-19 19:02:51 -0700 | [diff] [blame] | 1775 | |
Andreas Gampe | ed6b9df | 2014-11-20 22:02:20 -0800 | [diff] [blame] | 1776 | jclass audioPortConfigClass = FindClassOrDie(env, "android/media/AudioPortConfig"); |
| 1777 | gAudioPortConfigClass = MakeGlobalRefOrDie(env, audioPortConfigClass); |
| 1778 | gAudioPortConfigCstor = GetMethodIDOrDie(env, audioPortConfigClass, "<init>", |
| 1779 | "(Landroid/media/AudioPort;IIILandroid/media/AudioGainConfig;)V"); |
| 1780 | gAudioPortConfigFields.mPort = GetFieldIDOrDie(env, audioPortConfigClass, "mPort", |
Eric Laurent | b69681c | 2014-05-19 19:02:51 -0700 | [diff] [blame] | 1781 | "Landroid/media/AudioPort;"); |
Andreas Gampe | ed6b9df | 2014-11-20 22:02:20 -0800 | [diff] [blame] | 1782 | gAudioPortConfigFields.mSamplingRate = GetFieldIDOrDie(env, audioPortConfigClass, |
Eric Laurent | b69681c | 2014-05-19 19:02:51 -0700 | [diff] [blame] | 1783 | "mSamplingRate", "I"); |
Andreas Gampe | ed6b9df | 2014-11-20 22:02:20 -0800 | [diff] [blame] | 1784 | gAudioPortConfigFields.mChannelMask = GetFieldIDOrDie(env, audioPortConfigClass, |
Eric Laurent | b69681c | 2014-05-19 19:02:51 -0700 | [diff] [blame] | 1785 | "mChannelMask", "I"); |
Andreas Gampe | ed6b9df | 2014-11-20 22:02:20 -0800 | [diff] [blame] | 1786 | gAudioPortConfigFields.mFormat = GetFieldIDOrDie(env, audioPortConfigClass, "mFormat", "I"); |
| 1787 | gAudioPortConfigFields.mGain = GetFieldIDOrDie(env, audioPortConfigClass, "mGain", |
Eric Laurent | b69681c | 2014-05-19 19:02:51 -0700 | [diff] [blame] | 1788 | "Landroid/media/AudioGainConfig;"); |
Andreas Gampe | ed6b9df | 2014-11-20 22:02:20 -0800 | [diff] [blame] | 1789 | gAudioPortConfigFields.mConfigMask = GetFieldIDOrDie(env, audioPortConfigClass, "mConfigMask", |
| 1790 | "I"); |
Eric Laurent | b69681c | 2014-05-19 19:02:51 -0700 | [diff] [blame] | 1791 | |
Andreas Gampe | ed6b9df | 2014-11-20 22:02:20 -0800 | [diff] [blame] | 1792 | jclass audioDevicePortConfigClass = FindClassOrDie(env, "android/media/AudioDevicePortConfig"); |
| 1793 | gAudioDevicePortConfigClass = MakeGlobalRefOrDie(env, audioDevicePortConfigClass); |
| 1794 | gAudioDevicePortConfigCstor = GetMethodIDOrDie(env, audioDevicePortConfigClass, "<init>", |
| 1795 | "(Landroid/media/AudioDevicePort;IIILandroid/media/AudioGainConfig;)V"); |
Eric Laurent | b69681c | 2014-05-19 19:02:51 -0700 | [diff] [blame] | 1796 | |
Andreas Gampe | ed6b9df | 2014-11-20 22:02:20 -0800 | [diff] [blame] | 1797 | jclass audioMixPortConfigClass = FindClassOrDie(env, "android/media/AudioMixPortConfig"); |
| 1798 | gAudioMixPortConfigClass = MakeGlobalRefOrDie(env, audioMixPortConfigClass); |
| 1799 | gAudioMixPortConfigCstor = GetMethodIDOrDie(env, audioMixPortConfigClass, "<init>", |
| 1800 | "(Landroid/media/AudioMixPort;IIILandroid/media/AudioGainConfig;)V"); |
Eric Laurent | b69681c | 2014-05-19 19:02:51 -0700 | [diff] [blame] | 1801 | |
Andreas Gampe | ed6b9df | 2014-11-20 22:02:20 -0800 | [diff] [blame] | 1802 | jclass audioDevicePortClass = FindClassOrDie(env, "android/media/AudioDevicePort"); |
| 1803 | gAudioDevicePortClass = MakeGlobalRefOrDie(env, audioDevicePortClass); |
| 1804 | gAudioDevicePortCstor = GetMethodIDOrDie(env, audioDevicePortClass, "<init>", |
Paul McLean | f29e5f3 | 2015-06-15 10:19:22 -0700 | [diff] [blame] | 1805 | "(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] | 1806 | |
Andreas Gampe | ed6b9df | 2014-11-20 22:02:20 -0800 | [diff] [blame] | 1807 | jclass audioMixPortClass = FindClassOrDie(env, "android/media/AudioMixPort"); |
| 1808 | gAudioMixPortClass = MakeGlobalRefOrDie(env, audioMixPortClass); |
| 1809 | gAudioMixPortCstor = GetMethodIDOrDie(env, audioMixPortClass, "<init>", |
Paul McLean | f29e5f3 | 2015-06-15 10:19:22 -0700 | [diff] [blame] | 1810 | "(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] | 1811 | |
Andreas Gampe | ed6b9df | 2014-11-20 22:02:20 -0800 | [diff] [blame] | 1812 | jclass audioGainClass = FindClassOrDie(env, "android/media/AudioGain"); |
| 1813 | gAudioGainClass = MakeGlobalRefOrDie(env, audioGainClass); |
| 1814 | gAudioGainCstor = GetMethodIDOrDie(env, audioGainClass, "<init>", "(IIIIIIIII)V"); |
Eric Laurent | b69681c | 2014-05-19 19:02:51 -0700 | [diff] [blame] | 1815 | |
Andreas Gampe | ed6b9df | 2014-11-20 22:02:20 -0800 | [diff] [blame] | 1816 | jclass audioGainConfigClass = FindClassOrDie(env, "android/media/AudioGainConfig"); |
| 1817 | gAudioGainConfigClass = MakeGlobalRefOrDie(env, audioGainConfigClass); |
| 1818 | gAudioGainConfigCstor = GetMethodIDOrDie(env, audioGainConfigClass, "<init>", |
Eric Laurent | b69681c | 2014-05-19 19:02:51 -0700 | [diff] [blame] | 1819 | "(ILandroid/media/AudioGain;II[II)V"); |
Andreas Gampe | ed6b9df | 2014-11-20 22:02:20 -0800 | [diff] [blame] | 1820 | gAudioGainConfigFields.mIndex = GetFieldIDOrDie(env, gAudioGainConfigClass, "mIndex", "I"); |
| 1821 | gAudioGainConfigFields.mMode = GetFieldIDOrDie(env, audioGainConfigClass, "mMode", "I"); |
| 1822 | gAudioGainConfigFields.mChannelMask = GetFieldIDOrDie(env, audioGainConfigClass, "mChannelMask", |
Eric Laurent | b69681c | 2014-05-19 19:02:51 -0700 | [diff] [blame] | 1823 | "I"); |
Andreas Gampe | ed6b9df | 2014-11-20 22:02:20 -0800 | [diff] [blame] | 1824 | gAudioGainConfigFields.mValues = GetFieldIDOrDie(env, audioGainConfigClass, "mValues", "[I"); |
| 1825 | gAudioGainConfigFields.mRampDurationMs = GetFieldIDOrDie(env, audioGainConfigClass, |
Eric Laurent | b69681c | 2014-05-19 19:02:51 -0700 | [diff] [blame] | 1826 | "mRampDurationMs", "I"); |
| 1827 | |
Andreas Gampe | ed6b9df | 2014-11-20 22:02:20 -0800 | [diff] [blame] | 1828 | jclass audioPatchClass = FindClassOrDie(env, "android/media/AudioPatch"); |
| 1829 | gAudioPatchClass = MakeGlobalRefOrDie(env, audioPatchClass); |
| 1830 | gAudioPatchCstor = GetMethodIDOrDie(env, audioPatchClass, "<init>", |
Eric Laurent | b69681c | 2014-05-19 19:02:51 -0700 | [diff] [blame] | 1831 | "(Landroid/media/AudioHandle;[Landroid/media/AudioPortConfig;[Landroid/media/AudioPortConfig;)V"); |
Andreas Gampe | ed6b9df | 2014-11-20 22:02:20 -0800 | [diff] [blame] | 1832 | gAudioPatchFields.mHandle = GetFieldIDOrDie(env, audioPatchClass, "mHandle", |
Eric Laurent | b69681c | 2014-05-19 19:02:51 -0700 | [diff] [blame] | 1833 | "Landroid/media/AudioHandle;"); |
| 1834 | |
Andreas Gampe | ed6b9df | 2014-11-20 22:02:20 -0800 | [diff] [blame] | 1835 | jclass eventHandlerClass = FindClassOrDie(env, kEventHandlerClassPathName); |
Eric Laurent | 2615afb | 2015-03-25 14:51:08 -0700 | [diff] [blame] | 1836 | gAudioPortEventHandlerMethods.postEventFromNative = GetStaticMethodIDOrDie( |
| 1837 | env, eventHandlerClass, "postEventFromNative", |
| 1838 | "(Ljava/lang/Object;IIILjava/lang/Object;)V"); |
| 1839 | gEventHandlerFields.mJniCallback = GetFieldIDOrDie(env, |
| 1840 | eventHandlerClass, "mJniCallback", "J"); |
Eric Laurent | 700e734 | 2014-05-02 18:33:15 -0700 | [diff] [blame] | 1841 | |
Jean-Michel Trivi | d3c71f0 | 2015-12-07 11:59:31 -0800 | [diff] [blame] | 1842 | gAudioPolicyEventHandlerMethods.postDynPolicyEventFromNative = |
Jean-Michel Trivi | 5a56109 | 2015-04-23 18:48:08 -0700 | [diff] [blame] | 1843 | GetStaticMethodIDOrDie(env, env->FindClass(kClassPathName), |
| 1844 | "dynamicPolicyCallbackFromNative", "(ILjava/lang/String;I)V"); |
Jean-Michel Trivi | d3c71f0 | 2015-12-07 11:59:31 -0800 | [diff] [blame] | 1845 | gAudioPolicyEventHandlerMethods.postRecordConfigEventFromNative = |
| 1846 | GetStaticMethodIDOrDie(env, env->FindClass(kClassPathName), |
Jean-Michel Trivi | 33fd816 | 2016-02-22 10:52:41 -0800 | [diff] [blame] | 1847 | "recordingCallbackFromNative", "(III[I)V"); |
Jean-Michel Trivi | 5a56109 | 2015-04-23 18:48:08 -0700 | [diff] [blame] | 1848 | |
Eric Laurent | 0937897 | 2014-12-10 10:42:08 -0800 | [diff] [blame] | 1849 | jclass audioMixClass = FindClassOrDie(env, "android/media/audiopolicy/AudioMix"); |
| 1850 | gAudioMixClass = MakeGlobalRefOrDie(env, audioMixClass); |
| 1851 | gAudioMixFields.mRule = GetFieldIDOrDie(env, audioMixClass, "mRule", |
Eric Laurent | 7f5eb9f | 2014-12-01 19:36:30 -0800 | [diff] [blame] | 1852 | "Landroid/media/audiopolicy/AudioMixingRule;"); |
Eric Laurent | 0937897 | 2014-12-10 10:42:08 -0800 | [diff] [blame] | 1853 | gAudioMixFields.mFormat = GetFieldIDOrDie(env, audioMixClass, "mFormat", |
Eric Laurent | 7f5eb9f | 2014-12-01 19:36:30 -0800 | [diff] [blame] | 1854 | "Landroid/media/AudioFormat;"); |
Eric Laurent | 0937897 | 2014-12-10 10:42:08 -0800 | [diff] [blame] | 1855 | gAudioMixFields.mRouteFlags = GetFieldIDOrDie(env, audioMixClass, "mRouteFlags", "I"); |
| 1856 | gAudioMixFields.mRegistrationId = GetFieldIDOrDie(env, audioMixClass, "mRegistrationId", |
Eric Laurent | 7f5eb9f | 2014-12-01 19:36:30 -0800 | [diff] [blame] | 1857 | "Ljava/lang/String;"); |
Eric Laurent | 0937897 | 2014-12-10 10:42:08 -0800 | [diff] [blame] | 1858 | gAudioMixFields.mMixType = GetFieldIDOrDie(env, audioMixClass, "mMixType", "I"); |
Jean-Michel Trivi | 5a56109 | 2015-04-23 18:48:08 -0700 | [diff] [blame] | 1859 | gAudioMixFields.mCallbackFlags = GetFieldIDOrDie(env, audioMixClass, "mCallbackFlags", "I"); |
Eric Laurent | 7f5eb9f | 2014-12-01 19:36:30 -0800 | [diff] [blame] | 1860 | |
Eric Laurent | 0937897 | 2014-12-10 10:42:08 -0800 | [diff] [blame] | 1861 | jclass audioFormatClass = FindClassOrDie(env, "android/media/AudioFormat"); |
| 1862 | gAudioFormatClass = MakeGlobalRefOrDie(env, audioFormatClass); |
| 1863 | gAudioFormatFields.mEncoding = GetFieldIDOrDie(env, audioFormatClass, "mEncoding", "I"); |
| 1864 | gAudioFormatFields.mSampleRate = GetFieldIDOrDie(env, audioFormatClass, "mSampleRate", "I"); |
| 1865 | gAudioFormatFields.mChannelMask = GetFieldIDOrDie(env, audioFormatClass, "mChannelMask", "I"); |
Eric Laurent | 7f5eb9f | 2014-12-01 19:36:30 -0800 | [diff] [blame] | 1866 | |
Eric Laurent | 0937897 | 2014-12-10 10:42:08 -0800 | [diff] [blame] | 1867 | jclass audioMixingRuleClass = FindClassOrDie(env, "android/media/audiopolicy/AudioMixingRule"); |
| 1868 | gAudioMixingRuleClass = MakeGlobalRefOrDie(env, audioMixingRuleClass); |
| 1869 | gAudioMixingRuleFields.mCriteria = GetFieldIDOrDie(env, audioMixingRuleClass, "mCriteria", |
Eric Laurent | 7f5eb9f | 2014-12-01 19:36:30 -0800 | [diff] [blame] | 1870 | "Ljava/util/ArrayList;"); |
| 1871 | |
Jean-Michel Trivi | ba6b4b4 | 2016-02-10 14:26:35 -0800 | [diff] [blame] | 1872 | jclass audioMixMatchCriterionClass = |
| 1873 | FindClassOrDie(env, "android/media/audiopolicy/AudioMixingRule$AudioMixMatchCriterion"); |
| 1874 | gAudioMixMatchCriterionClass = MakeGlobalRefOrDie(env,audioMixMatchCriterionClass); |
| 1875 | gAudioMixMatchCriterionFields.mAttr = GetFieldIDOrDie(env, audioMixMatchCriterionClass, "mAttr", |
Eric Laurent | 7f5eb9f | 2014-12-01 19:36:30 -0800 | [diff] [blame] | 1876 | "Landroid/media/AudioAttributes;"); |
Jean-Michel Trivi | ba6b4b4 | 2016-02-10 14:26:35 -0800 | [diff] [blame] | 1877 | gAudioMixMatchCriterionFields.mIntProp = GetFieldIDOrDie(env, audioMixMatchCriterionClass, "mIntProp", |
| 1878 | "I"); |
| 1879 | gAudioMixMatchCriterionFields.mRule = GetFieldIDOrDie(env, audioMixMatchCriterionClass, "mRule", |
Eric Laurent | 7f5eb9f | 2014-12-01 19:36:30 -0800 | [diff] [blame] | 1880 | "I"); |
| 1881 | |
Eric Laurent | 0937897 | 2014-12-10 10:42:08 -0800 | [diff] [blame] | 1882 | jclass audioAttributesClass = FindClassOrDie(env, "android/media/AudioAttributes"); |
| 1883 | gAudioAttributesClass = MakeGlobalRefOrDie(env, audioAttributesClass); |
| 1884 | gAudioAttributesFields.mUsage = GetFieldIDOrDie(env, audioAttributesClass, "mUsage", "I"); |
| 1885 | gAudioAttributesFields.mSource = GetFieldIDOrDie(env, audioAttributesClass, "mSource", "I"); |
Eric Laurent | 7f5eb9f | 2014-12-01 19:36:30 -0800 | [diff] [blame] | 1886 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1887 | AudioSystem::setErrorCallback(android_media_AudioSystem_error_callback); |
Glenn Kasten | 18db49a | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 1888 | |
Andreas Gampe | ed6b9df | 2014-11-20 22:02:20 -0800 | [diff] [blame] | 1889 | RegisterMethodsOrDie(env, kClassPathName, gMethods, NELEM(gMethods)); |
| 1890 | return RegisterMethodsOrDie(env, kEventHandlerClassPathName, gEventHandlerMethods, |
| 1891 | NELEM(gEventHandlerMethods)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1892 | } |