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