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