The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2008 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | //#define LOG_NDEBUG 0 |
| 17 | |
| 18 | #define LOG_TAG "AudioTrack-JNI" |
| 19 | |
Wei Jia | 071a8b7 | 2015-03-09 16:38:25 -0700 | [diff] [blame] | 20 | #include "android_media_AudioTrack.h" |
| 21 | |
Steven Moreland | 2279b25 | 2017-07-19 09:50:45 -0700 | [diff] [blame] | 22 | #include <nativehelper/JNIHelp.h> |
jiabin | fcd4cbf | 2020-08-21 18:07:27 -0700 | [diff] [blame] | 23 | #include <nativehelper/ScopedUtfChars.h> |
Andreas Gampe | ed6b9df | 2014-11-20 22:02:20 -0800 | [diff] [blame] | 24 | #include "core_jni_helpers.h" |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 25 | |
Glenn Kasten | c81d31c | 2012-03-13 14:46:23 -0700 | [diff] [blame] | 26 | #include <utils/Log.h> |
Jean-Michel Trivi | 157cba4 | 2019-01-25 18:40:03 -0800 | [diff] [blame] | 27 | #include <media/AudioParameter.h> |
Glenn Kasten | c81d31c | 2012-03-13 14:46:23 -0700 | [diff] [blame] | 28 | #include <media/AudioSystem.h> |
| 29 | #include <media/AudioTrack.h> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 30 | |
Jean-Michel Trivi | 03f5139 | 2018-08-31 15:47:13 -0700 | [diff] [blame] | 31 | #include <android-base/macros.h> |
Mathias Agopian | 0795272 | 2009-05-19 19:08:10 -0700 | [diff] [blame] | 32 | #include <binder/MemoryHeapBase.h> |
| 33 | #include <binder/MemoryBase.h> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 34 | |
jiabin | a02129b | 2020-02-25 14:04:36 -0800 | [diff] [blame] | 35 | #include "android_media_AudioAttributes.h" |
Eric Laurent | bc11a69 | 2014-05-16 12:19:25 -0700 | [diff] [blame] | 36 | #include "android_media_AudioErrors.h" |
jiabin | a02129b | 2020-02-25 14:04:36 -0800 | [diff] [blame] | 37 | #include "android_media_AudioFormat.h" |
| 38 | #include "android_media_AudioTrackCallback.h" |
| 39 | #include "android_media_DeviceCallback.h" |
Ray Essick | 510225b | 2018-01-24 14:27:16 -0800 | [diff] [blame] | 40 | #include "android_media_MediaMetricsJNI.h" |
Wei Jia | 2d61e2b | 2015-05-08 15:23:28 -0700 | [diff] [blame] | 41 | #include "android_media_PlaybackParams.h" |
Andy Hung | 035d4ec | 2017-01-24 13:45:02 -0800 | [diff] [blame] | 42 | #include "android_media_VolumeShaper.h" |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 43 | |
Ben Wagner | 85ea32b | 2016-03-16 17:10:42 -0400 | [diff] [blame] | 44 | #include <cinttypes> |
| 45 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 46 | // ---------------------------------------------------------------------------- |
| 47 | |
| 48 | using namespace android; |
| 49 | |
Ivan Lozano | 330d876 | 2017-08-08 12:51:06 -0700 | [diff] [blame] | 50 | using ::android::media::VolumeShaper; |
| 51 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 52 | // ---------------------------------------------------------------------------- |
| 53 | static const char* const kClassPathName = "android/media/AudioTrack"; |
| 54 | |
Jean-Michel Trivi | a1d80e3 | 2014-06-18 08:18:41 -0700 | [diff] [blame] | 55 | struct audio_track_fields_t { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 56 | // these fields provide access from C++ to the... |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 57 | jmethodID postNativeEventInJava; //... event post callback method |
Eric Laurent | 83b3685 | 2009-07-28 07:49:22 -0700 | [diff] [blame] | 58 | jfieldID nativeTrackInJavaObj; // stores in Java the native AudioTrack object |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 59 | jfieldID jniData; // stores in Java additional resources used by the native AudioTrack |
Jean-Michel Trivi | a1d80e3 | 2014-06-18 08:18:41 -0700 | [diff] [blame] | 60 | jfieldID fieldStreamType; // ... mStreamType field in the AudioTrack Java object |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 61 | }; |
Jean-Michel Trivi | a1d80e3 | 2014-06-18 08:18:41 -0700 | [diff] [blame] | 62 | static audio_track_fields_t javaAudioTrackFields; |
Wei Jia | 2d61e2b | 2015-05-08 15:23:28 -0700 | [diff] [blame] | 63 | static PlaybackParams::fields_t gPlaybackParamsFields; |
Andy Hung | 035d4ec | 2017-01-24 13:45:02 -0800 | [diff] [blame] | 64 | static VolumeShaperHelper::fields_t gVolumeShaperFields; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 65 | |
| 66 | struct audiotrack_callback_cookie { |
| 67 | jclass audioTrack_class; |
| 68 | jobject audioTrack_ref; |
Eric Laurent | 532bc1c | 2012-04-20 12:45:03 -0700 | [diff] [blame] | 69 | bool busy; |
| 70 | Condition cond; |
Jean-Michel Trivi | 980d38f | 2018-01-08 15:43:35 -0800 | [diff] [blame] | 71 | bool isOffload; |
Eric Laurent | 532bc1c | 2012-04-20 12:45:03 -0700 | [diff] [blame] | 72 | }; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 73 | |
Glenn Kasten | 3d301cb | 2012-01-16 14:46:54 -0800 | [diff] [blame] | 74 | // keep these values in sync with AudioTrack.java |
| 75 | #define MODE_STATIC 0 |
| 76 | #define MODE_STREAM 1 |
| 77 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 78 | // ---------------------------------------------------------------------------- |
| 79 | class AudioTrackJniStorage { |
jiabin | a02129b | 2020-02-25 14:04:36 -0800 | [diff] [blame] | 80 | public: |
| 81 | sp<MemoryHeapBase> mMemHeap; |
| 82 | sp<MemoryBase> mMemBase; |
| 83 | audiotrack_callback_cookie mCallbackData{}; |
| 84 | sp<JNIDeviceCallback> mDeviceCallback; |
| 85 | sp<JNIAudioTrackCallback> mAudioTrackCallback; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 86 | |
| 87 | bool allocSharedMem(int sizeInBytes) { |
| 88 | mMemHeap = new MemoryHeapBase(sizeInBytes, 0, "AudioTrack Heap Base"); |
| 89 | if (mMemHeap->getHeapID() < 0) { |
| 90 | return false; |
| 91 | } |
| 92 | mMemBase = new MemoryBase(mMemHeap, 0, sizeInBytes); |
| 93 | return true; |
| 94 | } |
| 95 | }; |
| 96 | |
Andy Hung | 59dffec | 2020-01-14 17:29:03 -0800 | [diff] [blame] | 97 | class TunerConfigurationHelper { |
| 98 | JNIEnv *const mEnv; |
| 99 | jobject const mTunerConfiguration; |
| 100 | |
| 101 | struct Ids { |
| 102 | Ids(JNIEnv *env) |
| 103 | : mClass(FindClassOrDie(env, "android/media/AudioTrack$TunerConfiguration")), |
| 104 | mContentId(GetFieldIDOrDie(env, mClass, "mContentId", "I")), |
| 105 | mSyncId(GetFieldIDOrDie(env, mClass, "mSyncId", "I")) {} |
| 106 | const jclass mClass; |
| 107 | const jfieldID mContentId; |
| 108 | const jfieldID mSyncId; |
| 109 | }; |
| 110 | |
| 111 | static const Ids &getIds(JNIEnv *env) { |
| 112 | // Meyer's singleton, initializes first time control passes through |
| 113 | // declaration in a block and is thread-safe per ISO/IEC 14882:2011 6.7.4. |
| 114 | static Ids ids(env); |
| 115 | return ids; |
| 116 | } |
| 117 | |
| 118 | public: |
| 119 | TunerConfigurationHelper(JNIEnv *env, jobject tunerConfiguration) |
| 120 | : mEnv(env), mTunerConfiguration(tunerConfiguration) {} |
| 121 | |
| 122 | int32_t getContentId() const { |
| 123 | if (mEnv == nullptr || mTunerConfiguration == nullptr) return 0; |
| 124 | const Ids &ids = getIds(mEnv); |
| 125 | return (int32_t)mEnv->GetIntField(mTunerConfiguration, ids.mContentId); |
| 126 | } |
| 127 | |
| 128 | int32_t getSyncId() const { |
| 129 | if (mEnv == nullptr || mTunerConfiguration == nullptr) return 0; |
| 130 | const Ids &ids = getIds(mEnv); |
| 131 | return (int32_t)mEnv->GetIntField(mTunerConfiguration, ids.mSyncId); |
| 132 | } |
| 133 | |
| 134 | // optional check to confirm class and field ids can be found. |
| 135 | static void initCheckOrDie(JNIEnv *env) { (void)getIds(env); } |
| 136 | }; |
| 137 | |
Eric Laurent | 532bc1c | 2012-04-20 12:45:03 -0700 | [diff] [blame] | 138 | static Mutex sLock; |
| 139 | static SortedVector <audiotrack_callback_cookie *> sAudioTrackCallBackCookies; |
| 140 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 141 | // ---------------------------------------------------------------------------- |
| 142 | #define DEFAULT_OUTPUT_SAMPLE_RATE 44100 |
| 143 | |
Chih-Hung Hsieh | 0ca16ef | 2016-05-19 15:14:54 -0700 | [diff] [blame] | 144 | #define AUDIOTRACK_ERROR_SETUP_AUDIOSYSTEM (-16) |
| 145 | #define AUDIOTRACK_ERROR_SETUP_INVALIDCHANNELMASK (-17) |
| 146 | #define AUDIOTRACK_ERROR_SETUP_INVALIDFORMAT (-18) |
| 147 | #define AUDIOTRACK_ERROR_SETUP_INVALIDSTREAMTYPE (-19) |
| 148 | #define AUDIOTRACK_ERROR_SETUP_NATIVEINITFAILED (-20) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 149 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 150 | // ---------------------------------------------------------------------------- |
Glenn Kasten | e46a86f | 2011-06-01 15:20:35 -0700 | [diff] [blame] | 151 | static void audioCallback(int event, void* user, void *info) { |
Eric Laurent | 532bc1c | 2012-04-20 12:45:03 -0700 | [diff] [blame] | 152 | |
| 153 | audiotrack_callback_cookie *callbackInfo = (audiotrack_callback_cookie *)user; |
| 154 | { |
| 155 | Mutex::Autolock l(sLock); |
| 156 | if (sAudioTrackCallBackCookies.indexOf(callbackInfo) < 0) { |
| 157 | return; |
| 158 | } |
| 159 | callbackInfo->busy = true; |
| 160 | } |
| 161 | |
Jean-Michel Trivi | 03f5139 | 2018-08-31 15:47:13 -0700 | [diff] [blame] | 162 | // used as default argument when event callback doesn't have any, or number of |
| 163 | // frames for EVENT_CAN_WRITE_MORE_DATA |
| 164 | int arg = 0; |
| 165 | bool postEvent = false; |
Glenn Kasten | 5b1576c | 2013-07-18 16:58:19 -0700 | [diff] [blame] | 166 | switch (event) { |
Jean-Michel Trivi | 980d38f | 2018-01-08 15:43:35 -0800 | [diff] [blame] | 167 | // Offload only events |
Jean-Michel Trivi | 03f5139 | 2018-08-31 15:47:13 -0700 | [diff] [blame] | 168 | case AudioTrack::EVENT_CAN_WRITE_MORE_DATA: |
| 169 | // this event will read the info return parameter of the callback: |
| 170 | // for JNI offload, use the returned size to indicate: |
| 171 | // 1/ no data is returned through callback, as it's all done through write() |
| 172 | // 2/ do not wait as AudioTrack does when it receives 0 bytes |
Jean-Michel Trivi | 980d38f | 2018-01-08 15:43:35 -0800 | [diff] [blame] | 173 | if (callbackInfo->isOffload) { |
Jean-Michel Trivi | 03f5139 | 2018-08-31 15:47:13 -0700 | [diff] [blame] | 174 | AudioTrack::Buffer* pBuffer = (AudioTrack::Buffer*) info; |
| 175 | const size_t availableForWrite = pBuffer->size; |
| 176 | arg = availableForWrite > INT32_MAX ? INT32_MAX : (int) availableForWrite; |
| 177 | pBuffer->size = 0; |
| 178 | } |
| 179 | FALLTHROUGH_INTENDED; |
| 180 | case AudioTrack::EVENT_STREAM_END: |
| 181 | case AudioTrack::EVENT_NEW_IAUDIOTRACK: // a.k.a. tear down |
| 182 | if (callbackInfo->isOffload) { |
| 183 | postEvent = true; |
| 184 | } |
| 185 | break; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 186 | |
Jean-Michel Trivi | 980d38f | 2018-01-08 15:43:35 -0800 | [diff] [blame] | 187 | // PCM and offload events |
| 188 | case AudioTrack::EVENT_MARKER: |
Jean-Michel Trivi | 03f5139 | 2018-08-31 15:47:13 -0700 | [diff] [blame] | 189 | case AudioTrack::EVENT_NEW_POS: |
| 190 | postEvent = true; |
| 191 | break; |
| 192 | default: |
| 193 | // event will not be posted |
| 194 | break; |
| 195 | } |
| 196 | |
| 197 | if (postEvent) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 198 | JNIEnv *env = AndroidRuntime::getJNIEnv(); |
Jean-Michel Trivi | 03f5139 | 2018-08-31 15:47:13 -0700 | [diff] [blame] | 199 | if (env != NULL) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 200 | env->CallStaticVoidMethod( |
Jean-Michel Trivi | 980d38f | 2018-01-08 15:43:35 -0800 | [diff] [blame] | 201 | callbackInfo->audioTrack_class, |
| 202 | javaAudioTrackFields.postNativeEventInJava, |
Jean-Michel Trivi | 03f5139 | 2018-08-31 15:47:13 -0700 | [diff] [blame] | 203 | callbackInfo->audioTrack_ref, event, arg, 0, NULL); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 204 | if (env->ExceptionCheck()) { |
| 205 | env->ExceptionDescribe(); |
| 206 | env->ExceptionClear(); |
| 207 | } |
| 208 | } |
| 209 | } |
Glenn Kasten | 5b1576c | 2013-07-18 16:58:19 -0700 | [diff] [blame] | 210 | |
Eric Laurent | 532bc1c | 2012-04-20 12:45:03 -0700 | [diff] [blame] | 211 | { |
| 212 | Mutex::Autolock l(sLock); |
| 213 | callbackInfo->busy = false; |
| 214 | callbackInfo->cond.broadcast(); |
| 215 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 216 | } |
| 217 | |
| 218 | |
| 219 | // ---------------------------------------------------------------------------- |
Eric Laurent | 532bc1c | 2012-04-20 12:45:03 -0700 | [diff] [blame] | 220 | static sp<AudioTrack> getAudioTrack(JNIEnv* env, jobject thiz) |
| 221 | { |
| 222 | Mutex::Autolock l(sLock); |
| 223 | AudioTrack* const at = |
Ashok Bhat | 075e9a1 | 2014-01-06 13:45:09 +0000 | [diff] [blame] | 224 | (AudioTrack*)env->GetLongField(thiz, javaAudioTrackFields.nativeTrackInJavaObj); |
Eric Laurent | 532bc1c | 2012-04-20 12:45:03 -0700 | [diff] [blame] | 225 | return sp<AudioTrack>(at); |
| 226 | } |
| 227 | |
| 228 | static sp<AudioTrack> setAudioTrack(JNIEnv* env, jobject thiz, const sp<AudioTrack>& at) |
| 229 | { |
| 230 | Mutex::Autolock l(sLock); |
| 231 | sp<AudioTrack> old = |
Ashok Bhat | 075e9a1 | 2014-01-06 13:45:09 +0000 | [diff] [blame] | 232 | (AudioTrack*)env->GetLongField(thiz, javaAudioTrackFields.nativeTrackInJavaObj); |
Eric Laurent | 532bc1c | 2012-04-20 12:45:03 -0700 | [diff] [blame] | 233 | if (at.get()) { |
Mathias Agopian | b1d90c8 | 2013-03-06 17:45:42 -0800 | [diff] [blame] | 234 | at->incStrong((void*)setAudioTrack); |
Eric Laurent | 532bc1c | 2012-04-20 12:45:03 -0700 | [diff] [blame] | 235 | } |
| 236 | if (old != 0) { |
Mathias Agopian | b1d90c8 | 2013-03-06 17:45:42 -0800 | [diff] [blame] | 237 | old->decStrong((void*)setAudioTrack); |
Eric Laurent | 532bc1c | 2012-04-20 12:45:03 -0700 | [diff] [blame] | 238 | } |
Ashok Bhat | 075e9a1 | 2014-01-06 13:45:09 +0000 | [diff] [blame] | 239 | env->SetLongField(thiz, javaAudioTrackFields.nativeTrackInJavaObj, (jlong)at.get()); |
Eric Laurent | 532bc1c | 2012-04-20 12:45:03 -0700 | [diff] [blame] | 240 | return old; |
| 241 | } |
Wei Jia | 071a8b7 | 2015-03-09 16:38:25 -0700 | [diff] [blame] | 242 | |
| 243 | // ---------------------------------------------------------------------------- |
| 244 | sp<AudioTrack> android_media_AudioTrack_getAudioTrack(JNIEnv* env, jobject audioTrackObj) { |
| 245 | return getAudioTrack(env, audioTrackObj); |
| 246 | } |
| 247 | |
Eric Laurent | 532bc1c | 2012-04-20 12:45:03 -0700 | [diff] [blame] | 248 | // ---------------------------------------------------------------------------- |
Andy Hung | 59dffec | 2020-01-14 17:29:03 -0800 | [diff] [blame] | 249 | static jint android_media_AudioTrack_setup(JNIEnv *env, jobject thiz, jobject weak_this, |
| 250 | jobject jaa, jintArray jSampleRate, |
| 251 | jint channelPositionMask, jint channelIndexMask, |
| 252 | jint audioFormat, jint buffSizeInBytes, jint memoryMode, |
| 253 | jintArray jSession, jlong nativeAudioTrack, |
| 254 | jboolean offload, jint encapsulationMode, |
jiabin | fcd4cbf | 2020-08-21 18:07:27 -0700 | [diff] [blame] | 255 | jobject tunerConfiguration, jstring opPackageName) { |
Jean-Michel Trivi | 03f5139 | 2018-08-31 15:47:13 -0700 | [diff] [blame] | 256 | ALOGV("sampleRates=%p, channel mask=%x, index mask=%x, audioFormat(Java)=%d, buffSize=%d," |
Andy Hung | 59dffec | 2020-01-14 17:29:03 -0800 | [diff] [blame] | 257 | " nativeAudioTrack=0x%" PRIX64 ", offload=%d encapsulationMode=%d tuner=%p", |
| 258 | jSampleRate, channelPositionMask, channelIndexMask, audioFormat, buffSizeInBytes, |
| 259 | nativeAudioTrack, offload, encapsulationMode, tunerConfiguration); |
Eric Laurent | 2fb43ef | 2010-09-24 12:03:36 -0700 | [diff] [blame] | 260 | |
| 261 | if (jSession == NULL) { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 262 | ALOGE("Error creating AudioTrack: invalid session ID pointer"); |
Eric Laurent | bc11a69 | 2014-05-16 12:19:25 -0700 | [diff] [blame] | 263 | return (jint) AUDIO_JAVA_ERROR; |
Eric Laurent | 619346f | 2010-06-21 09:27:30 -0700 | [diff] [blame] | 264 | } |
| 265 | |
Andy Hung | 59dffec | 2020-01-14 17:29:03 -0800 | [diff] [blame] | 266 | // TODO: replace when we land matching AudioTrack::set() in frameworks/av in r or r-tv-dev. |
| 267 | if (tunerConfiguration != nullptr) { |
| 268 | const TunerConfigurationHelper tunerHelper(env, tunerConfiguration); |
| 269 | ALOGE("Error creating AudioTrack: unsupported tuner contentId:%d syncId:%d", |
| 270 | tunerHelper.getContentId(), tunerHelper.getSyncId()); |
| 271 | return (jint)AUDIOTRACK_ERROR_SETUP_NATIVEINITFAILED; |
| 272 | } |
| 273 | // TODO: replace when we land matching AudioTrack::set() in frameworks/av in r or r-tv-dev. |
| 274 | if (encapsulationMode != 0 /* ENCAPSULATION_MODE_NONE */) { |
| 275 | ALOGE("Error creating AudioTrack: unsupported encapsulationMode %d", encapsulationMode); |
| 276 | return (jint)AUDIOTRACK_ERROR_SETUP_NATIVEINITFAILED; |
| 277 | } |
| 278 | |
Eric Laurent | 2fb43ef | 2010-09-24 12:03:36 -0700 | [diff] [blame] | 279 | jint* nSession = (jint *) env->GetPrimitiveArrayCritical(jSession, NULL); |
| 280 | if (nSession == NULL) { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 281 | ALOGE("Error creating AudioTrack: Error retrieving session id pointer"); |
Eric Laurent | bc11a69 | 2014-05-16 12:19:25 -0700 | [diff] [blame] | 282 | return (jint) AUDIO_JAVA_ERROR; |
Eric Laurent | 2fb43ef | 2010-09-24 12:03:36 -0700 | [diff] [blame] | 283 | } |
Glenn Kasten | 33b8404 | 2016-03-08 12:02:55 -0800 | [diff] [blame] | 284 | audio_session_t sessionId = (audio_session_t) nSession[0]; |
Eric Laurent | 2fb43ef | 2010-09-24 12:03:36 -0700 | [diff] [blame] | 285 | env->ReleasePrimitiveArrayCritical(jSession, nSession, 0); |
| 286 | nSession = NULL; |
| 287 | |
Paul McLean | 9b09e53 | 2016-01-26 14:43:35 -0700 | [diff] [blame] | 288 | AudioTrackJniStorage* lpJniStorage = NULL; |
Glenn Kasten | 18db49a | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 289 | |
Paul McLean | 9b09e53 | 2016-01-26 14:43:35 -0700 | [diff] [blame] | 290 | jclass clazz = env->GetObjectClass(thiz); |
| 291 | if (clazz == NULL) { |
| 292 | ALOGE("Can't find %s when setting up callback.", kClassPathName); |
| 293 | return (jint) AUDIOTRACK_ERROR_SETUP_NATIVEINITFAILED; |
| 294 | } |
Jean-Michel Trivi | a1d80e3 | 2014-06-18 08:18:41 -0700 | [diff] [blame] | 295 | |
Paul McLean | 9b09e53 | 2016-01-26 14:43:35 -0700 | [diff] [blame] | 296 | // if we pass in an existing *Native* AudioTrack, we don't need to create/initialize one. |
Andy Hung | 59dffec | 2020-01-14 17:29:03 -0800 | [diff] [blame] | 297 | sp<AudioTrack> lpTrack; |
Paul McLean | 9b09e53 | 2016-01-26 14:43:35 -0700 | [diff] [blame] | 298 | if (nativeAudioTrack == 0) { |
| 299 | if (jaa == 0) { |
| 300 | ALOGE("Error creating AudioTrack: invalid audio attributes"); |
| 301 | return (jint) AUDIO_JAVA_ERROR; |
| 302 | } |
Eric Laurent | 532bc1c | 2012-04-20 12:45:03 -0700 | [diff] [blame] | 303 | |
Paul McLean | 9b09e53 | 2016-01-26 14:43:35 -0700 | [diff] [blame] | 304 | if (jSampleRate == 0) { |
| 305 | ALOGE("Error creating AudioTrack: invalid sample rates"); |
| 306 | return (jint) AUDIO_JAVA_ERROR; |
| 307 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 308 | |
Paul McLean | 9b09e53 | 2016-01-26 14:43:35 -0700 | [diff] [blame] | 309 | int* sampleRates = env->GetIntArrayElements(jSampleRate, NULL); |
| 310 | int sampleRateInHertz = sampleRates[0]; |
| 311 | env->ReleaseIntArrayElements(jSampleRate, sampleRates, JNI_ABORT); |
Glenn Kasten | 18db49a | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 312 | |
Paul McLean | 9b09e53 | 2016-01-26 14:43:35 -0700 | [diff] [blame] | 313 | // Invalid channel representations are caught by !audio_is_output_channel() below. |
| 314 | audio_channel_mask_t nativeChannelMask = nativeChannelMaskFromJavaChannelMasks( |
| 315 | channelPositionMask, channelIndexMask); |
| 316 | if (!audio_is_output_channel(nativeChannelMask)) { |
| 317 | ALOGE("Error creating AudioTrack: invalid native channel mask %#x.", nativeChannelMask); |
| 318 | return (jint) AUDIOTRACK_ERROR_SETUP_INVALIDCHANNELMASK; |
| 319 | } |
Glenn Kasten | 18db49a | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 320 | |
Paul McLean | 9b09e53 | 2016-01-26 14:43:35 -0700 | [diff] [blame] | 321 | uint32_t channelCount = audio_channel_count_from_out_mask(nativeChannelMask); |
| 322 | |
| 323 | // check the format. |
| 324 | // This function was called from Java, so we compare the format against the Java constants |
| 325 | audio_format_t format = audioFormatToNative(audioFormat); |
| 326 | if (format == AUDIO_FORMAT_INVALID) { |
| 327 | ALOGE("Error creating AudioTrack: unsupported audio format %d.", audioFormat); |
| 328 | return (jint) AUDIOTRACK_ERROR_SETUP_INVALIDFORMAT; |
| 329 | } |
| 330 | |
| 331 | // compute the frame count |
| 332 | size_t frameCount; |
jiabin | f2d8e9c | 2018-02-23 17:44:56 -0800 | [diff] [blame] | 333 | if (audio_has_proportional_frames(format)) { |
Paul McLean | 9b09e53 | 2016-01-26 14:43:35 -0700 | [diff] [blame] | 334 | const size_t bytesPerSample = audio_bytes_per_sample(format); |
| 335 | frameCount = buffSizeInBytes / (channelCount * bytesPerSample); |
| 336 | } else { |
| 337 | frameCount = buffSizeInBytes; |
| 338 | } |
| 339 | |
| 340 | // create the native AudioTrack object |
jiabin | fcd4cbf | 2020-08-21 18:07:27 -0700 | [diff] [blame] | 341 | ScopedUtfChars opPackageNameStr(env, opPackageName); |
| 342 | lpTrack = new AudioTrack(opPackageNameStr.c_str()); |
Paul McLean | 9b09e53 | 2016-01-26 14:43:35 -0700 | [diff] [blame] | 343 | |
| 344 | // read the AudioAttributes values |
François Gaffie | b469128 | 2018-07-09 13:07:32 +0200 | [diff] [blame] | 345 | auto paa = JNIAudioAttributeHelper::makeUnique(); |
| 346 | jint jStatus = JNIAudioAttributeHelper::nativeFromJava(env, jaa, paa.get()); |
| 347 | if (jStatus != (jint)AUDIO_JAVA_SUCCESS) { |
| 348 | return jStatus; |
| 349 | } |
Paul McLean | 9b09e53 | 2016-01-26 14:43:35 -0700 | [diff] [blame] | 350 | ALOGV("AudioTrack_setup for usage=%d content=%d flags=0x%#x tags=%s", |
| 351 | paa->usage, paa->content_type, paa->flags, paa->tags); |
| 352 | |
| 353 | // initialize the callback information: |
| 354 | // this data will be passed with every AudioTrack callback |
| 355 | lpJniStorage = new AudioTrackJniStorage(); |
| 356 | lpJniStorage->mCallbackData.audioTrack_class = (jclass)env->NewGlobalRef(clazz); |
| 357 | // we use a weak reference so the AudioTrack object can be garbage collected. |
| 358 | lpJniStorage->mCallbackData.audioTrack_ref = env->NewGlobalRef(weak_this); |
Jean-Michel Trivi | 980d38f | 2018-01-08 15:43:35 -0800 | [diff] [blame] | 359 | lpJniStorage->mCallbackData.isOffload = offload; |
Paul McLean | 9b09e53 | 2016-01-26 14:43:35 -0700 | [diff] [blame] | 360 | lpJniStorage->mCallbackData.busy = false; |
| 361 | |
Jean-Michel Trivi | 980d38f | 2018-01-08 15:43:35 -0800 | [diff] [blame] | 362 | audio_offload_info_t offloadInfo; |
Jean-Michel Trivi | 03f5139 | 2018-08-31 15:47:13 -0700 | [diff] [blame] | 363 | if (offload == JNI_TRUE) { |
Jean-Michel Trivi | 980d38f | 2018-01-08 15:43:35 -0800 | [diff] [blame] | 364 | offloadInfo = AUDIO_INFO_INITIALIZER; |
| 365 | offloadInfo.format = format; |
| 366 | offloadInfo.sample_rate = sampleRateInHertz; |
| 367 | offloadInfo.channel_mask = nativeChannelMask; |
| 368 | offloadInfo.has_video = false; |
| 369 | offloadInfo.stream_type = AUDIO_STREAM_MUSIC; //required for offload |
| 370 | } |
| 371 | |
Paul McLean | 9b09e53 | 2016-01-26 14:43:35 -0700 | [diff] [blame] | 372 | // initialize the native AudioTrack object |
| 373 | status_t status = NO_ERROR; |
| 374 | switch (memoryMode) { |
| 375 | case MODE_STREAM: |
jiabin | fcd4cbf | 2020-08-21 18:07:27 -0700 | [diff] [blame] | 376 | status = lpTrack->set(AUDIO_STREAM_DEFAULT, // stream type, but more info conveyed |
| 377 | // in paa (last argument) |
| 378 | sampleRateInHertz, |
| 379 | format, // word length, PCM |
| 380 | nativeChannelMask, offload ? 0 : frameCount, |
| 381 | offload ? AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD |
| 382 | : AUDIO_OUTPUT_FLAG_NONE, |
| 383 | audioCallback, |
| 384 | &(lpJniStorage->mCallbackData), // callback, callback data (user) |
| 385 | 0, // notificationFrames == 0 since not using EVENT_MORE_DATA |
| 386 | // to feed the AudioTrack |
| 387 | 0, // shared mem |
| 388 | true, // thread can call Java |
| 389 | sessionId, // audio session ID |
| 390 | offload ? AudioTrack::TRANSFER_SYNC_NOTIF_CALLBACK |
| 391 | : AudioTrack::TRANSFER_SYNC, |
| 392 | offload ? &offloadInfo : NULL, -1, -1, // default uid, pid values |
| 393 | paa.get()); |
Paul McLean | 9b09e53 | 2016-01-26 14:43:35 -0700 | [diff] [blame] | 394 | break; |
| 395 | |
| 396 | case MODE_STATIC: |
| 397 | // AudioTrack is using shared memory |
| 398 | |
| 399 | if (!lpJniStorage->allocSharedMem(buffSizeInBytes)) { |
| 400 | ALOGE("Error creating AudioTrack in static mode: error creating mem heap base"); |
| 401 | goto native_init_failure; |
| 402 | } |
| 403 | |
jiabin | fcd4cbf | 2020-08-21 18:07:27 -0700 | [diff] [blame] | 404 | status = lpTrack->set(AUDIO_STREAM_DEFAULT, // stream type, but more info conveyed |
| 405 | // in paa (last argument) |
| 406 | sampleRateInHertz, |
| 407 | format, // word length, PCM |
| 408 | nativeChannelMask, frameCount, AUDIO_OUTPUT_FLAG_NONE, |
| 409 | audioCallback, |
| 410 | &(lpJniStorage->mCallbackData), // callback, callback data (user) |
| 411 | 0, // notificationFrames == 0 since not using EVENT_MORE_DATA |
| 412 | // to feed the AudioTrack |
| 413 | lpJniStorage->mMemBase, // shared mem |
| 414 | true, // thread can call Java |
| 415 | sessionId, // audio session ID |
| 416 | AudioTrack::TRANSFER_SHARED, |
| 417 | NULL, // default offloadInfo |
| 418 | -1, -1, // default uid, pid values |
| 419 | paa.get()); |
Paul McLean | 9b09e53 | 2016-01-26 14:43:35 -0700 | [diff] [blame] | 420 | break; |
| 421 | |
| 422 | default: |
| 423 | ALOGE("Unknown mode %d", memoryMode); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 424 | goto native_init_failure; |
| 425 | } |
Glenn Kasten | 18db49a | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 426 | |
Paul McLean | 9b09e53 | 2016-01-26 14:43:35 -0700 | [diff] [blame] | 427 | if (status != NO_ERROR) { |
| 428 | ALOGE("Error %d initializing AudioTrack", status); |
| 429 | goto native_init_failure; |
| 430 | } |
Andy Hung | ac5c3e6 | 2020-04-27 09:22:37 -0700 | [diff] [blame] | 431 | // Set caller name so it can be logged in destructor. |
| 432 | // MediaMetricsConstants.h: AMEDIAMETRICS_PROP_CALLERNAME_VALUE_JAVA |
| 433 | lpTrack->setCallerName("java"); |
Paul McLean | 9b09e53 | 2016-01-26 14:43:35 -0700 | [diff] [blame] | 434 | } else { // end if (nativeAudioTrack == 0) |
| 435 | lpTrack = (AudioTrack*)nativeAudioTrack; |
| 436 | // TODO: We need to find out which members of the Java AudioTrack might |
| 437 | // need to be initialized from the Native AudioTrack |
| 438 | // these are directly returned from getters: |
| 439 | // mSampleRate |
| 440 | // mAudioFormat |
| 441 | // mStreamType |
| 442 | // mChannelConfiguration |
| 443 | // mChannelCount |
| 444 | // mState (?) |
| 445 | // mPlayState (?) |
| 446 | // these may be used internally (Java AudioTrack.audioParamCheck(): |
| 447 | // mChannelMask |
| 448 | // mChannelIndexMask |
| 449 | // mDataLoadMode |
Glenn Kasten | 3d301cb | 2012-01-16 14:46:54 -0800 | [diff] [blame] | 450 | |
Paul McLean | 9b09e53 | 2016-01-26 14:43:35 -0700 | [diff] [blame] | 451 | // initialize the callback information: |
| 452 | // this data will be passed with every AudioTrack callback |
| 453 | lpJniStorage = new AudioTrackJniStorage(); |
| 454 | lpJniStorage->mCallbackData.audioTrack_class = (jclass)env->NewGlobalRef(clazz); |
| 455 | // we use a weak reference so the AudioTrack object can be garbage collected. |
| 456 | lpJniStorage->mCallbackData.audioTrack_ref = env->NewGlobalRef(weak_this); |
| 457 | lpJniStorage->mCallbackData.busy = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 458 | } |
jiabin | a02129b | 2020-02-25 14:04:36 -0800 | [diff] [blame] | 459 | lpJniStorage->mAudioTrackCallback = |
| 460 | new JNIAudioTrackCallback(env, thiz, lpJniStorage->mCallbackData.audioTrack_ref, |
| 461 | javaAudioTrackFields.postNativeEventInJava); |
| 462 | lpTrack->setAudioTrackCallback(lpJniStorage->mAudioTrackCallback); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 463 | |
Eric Laurent | 2fb43ef | 2010-09-24 12:03:36 -0700 | [diff] [blame] | 464 | nSession = (jint *) env->GetPrimitiveArrayCritical(jSession, NULL); |
| 465 | if (nSession == NULL) { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 466 | ALOGE("Error creating AudioTrack: Error retrieving session id pointer"); |
Eric Laurent | 2fb43ef | 2010-09-24 12:03:36 -0700 | [diff] [blame] | 467 | goto native_init_failure; |
| 468 | } |
Eric Laurent | 619346f | 2010-06-21 09:27:30 -0700 | [diff] [blame] | 469 | // read the audio session ID back from AudioTrack in case we create a new session |
| 470 | nSession[0] = lpTrack->getSessionId(); |
Eric Laurent | 619346f | 2010-06-21 09:27:30 -0700 | [diff] [blame] | 471 | env->ReleasePrimitiveArrayCritical(jSession, nSession, 0); |
| 472 | nSession = NULL; |
| 473 | |
Glenn Kasten | 1cbf9b3 | 2016-02-02 12:04:09 -0800 | [diff] [blame] | 474 | { |
| 475 | const jint elements[1] = { (jint) lpTrack->getSampleRate() }; |
| 476 | env->SetIntArrayRegion(jSampleRate, 0, 1, elements); |
| 477 | } |
| 478 | |
Eric Laurent | 532bc1c | 2012-04-20 12:45:03 -0700 | [diff] [blame] | 479 | { // scope for the lock |
| 480 | Mutex::Autolock l(sLock); |
| 481 | sAudioTrackCallBackCookies.add(&lpJniStorage->mCallbackData); |
| 482 | } |
Glenn Kasten | 18db49a | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 483 | // save our newly created C++ AudioTrack in the "nativeTrackInJavaObj" field |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 484 | // of the Java object (in mNativeTrackInJavaObj) |
Eric Laurent | 532bc1c | 2012-04-20 12:45:03 -0700 | [diff] [blame] | 485 | setAudioTrack(env, thiz, lpTrack); |
Glenn Kasten | 18db49a | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 486 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 487 | // save the JNI resources so we can free them later |
Ashok Bhat | 075e9a1 | 2014-01-06 13:45:09 +0000 | [diff] [blame] | 488 | //ALOGV("storing lpJniStorage: %x\n", (long)lpJniStorage); |
| 489 | env->SetLongField(thiz, javaAudioTrackFields.jniData, (jlong)lpJniStorage); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 490 | |
Jean-Michel Trivi | a1d80e3 | 2014-06-18 08:18:41 -0700 | [diff] [blame] | 491 | // since we had audio attributes, the stream type was derived from them during the |
| 492 | // creation of the native AudioTrack: push the same value to the Java object |
| 493 | env->SetIntField(thiz, javaAudioTrackFields.fieldStreamType, (jint) lpTrack->streamType()); |
Jean-Michel Trivi | a1d80e3 | 2014-06-18 08:18:41 -0700 | [diff] [blame] | 494 | |
Eric Laurent | bc11a69 | 2014-05-16 12:19:25 -0700 | [diff] [blame] | 495 | return (jint) AUDIO_JAVA_SUCCESS; |
Glenn Kasten | 18db49a | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 496 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 497 | // failures: |
| 498 | native_init_failure: |
Eric Laurent | 619346f | 2010-06-21 09:27:30 -0700 | [diff] [blame] | 499 | if (nSession != NULL) { |
| 500 | env->ReleasePrimitiveArrayCritical(jSession, nSession, 0); |
| 501 | } |
Jean-Michel Trivi | 8a14968 | 2009-07-15 18:31:11 -0700 | [diff] [blame] | 502 | env->DeleteGlobalRef(lpJniStorage->mCallbackData.audioTrack_class); |
| 503 | env->DeleteGlobalRef(lpJniStorage->mCallbackData.audioTrack_ref); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 504 | delete lpJniStorage; |
Ashok Bhat | 075e9a1 | 2014-01-06 13:45:09 +0000 | [diff] [blame] | 505 | env->SetLongField(thiz, javaAudioTrackFields.jniData, 0); |
Glenn Kasten | 18db49a | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 506 | |
Glenn Kasten | 14d226a | 2015-05-18 13:53:39 -0700 | [diff] [blame] | 507 | // lpTrack goes out of scope, so reference count drops to zero |
Ashok Bhat | 075e9a1 | 2014-01-06 13:45:09 +0000 | [diff] [blame] | 508 | return (jint) AUDIOTRACK_ERROR_SETUP_NATIVEINITFAILED; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 509 | } |
| 510 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 511 | // ---------------------------------------------------------------------------- |
Michael Chan | 2de156d | 2018-04-24 14:33:57 +1000 | [diff] [blame] | 512 | static jboolean |
| 513 | android_media_AudioTrack_is_direct_output_supported(JNIEnv *env, jobject thiz, |
| 514 | jint encoding, jint sampleRate, |
| 515 | jint channelMask, jint channelIndexMask, |
| 516 | jint contentType, jint usage, jint flags) { |
| 517 | audio_config_base_t config = {}; |
| 518 | audio_attributes_t attributes = {}; |
| 519 | config.format = static_cast<audio_format_t>(audioFormatToNative(encoding)); |
| 520 | config.sample_rate = static_cast<uint32_t>(sampleRate); |
| 521 | config.channel_mask = nativeChannelMaskFromJavaChannelMasks(channelMask, channelIndexMask); |
| 522 | attributes.content_type = static_cast<audio_content_type_t>(contentType); |
| 523 | attributes.usage = static_cast<audio_usage_t>(usage); |
| 524 | attributes.flags = static_cast<audio_flags_mask_t>(flags); |
| 525 | // ignore source and tags attributes as they don't affect querying whether output is supported |
| 526 | return AudioTrack::isDirectOutputSupported(config, attributes); |
| 527 | } |
| 528 | |
| 529 | // ---------------------------------------------------------------------------- |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 530 | static void |
| 531 | android_media_AudioTrack_start(JNIEnv *env, jobject thiz) |
| 532 | { |
Eric Laurent | 532bc1c | 2012-04-20 12:45:03 -0700 | [diff] [blame] | 533 | sp<AudioTrack> lpTrack = getAudioTrack(env, thiz); |
| 534 | if (lpTrack == NULL) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 535 | jniThrowException(env, "java/lang/IllegalStateException", |
| 536 | "Unable to retrieve AudioTrack pointer for start()"); |
Eric Laurent | 7070b36 | 2010-07-16 07:43:46 -0700 | [diff] [blame] | 537 | return; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 538 | } |
Jean-Michel Trivi | 21dc037 | 2009-05-08 16:06:34 -0700 | [diff] [blame] | 539 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 540 | lpTrack->start(); |
| 541 | } |
| 542 | |
| 543 | |
| 544 | // ---------------------------------------------------------------------------- |
| 545 | static void |
| 546 | android_media_AudioTrack_stop(JNIEnv *env, jobject thiz) |
| 547 | { |
Eric Laurent | 532bc1c | 2012-04-20 12:45:03 -0700 | [diff] [blame] | 548 | sp<AudioTrack> lpTrack = getAudioTrack(env, thiz); |
| 549 | if (lpTrack == NULL) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 550 | jniThrowException(env, "java/lang/IllegalStateException", |
| 551 | "Unable to retrieve AudioTrack pointer for stop()"); |
Eric Laurent | 7070b36 | 2010-07-16 07:43:46 -0700 | [diff] [blame] | 552 | return; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 553 | } |
| 554 | |
| 555 | lpTrack->stop(); |
| 556 | } |
| 557 | |
| 558 | |
| 559 | // ---------------------------------------------------------------------------- |
| 560 | static void |
| 561 | android_media_AudioTrack_pause(JNIEnv *env, jobject thiz) |
| 562 | { |
Eric Laurent | 532bc1c | 2012-04-20 12:45:03 -0700 | [diff] [blame] | 563 | sp<AudioTrack> lpTrack = getAudioTrack(env, thiz); |
| 564 | if (lpTrack == NULL) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 565 | jniThrowException(env, "java/lang/IllegalStateException", |
| 566 | "Unable to retrieve AudioTrack pointer for pause()"); |
Eric Laurent | 7070b36 | 2010-07-16 07:43:46 -0700 | [diff] [blame] | 567 | return; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 568 | } |
| 569 | |
| 570 | lpTrack->pause(); |
| 571 | } |
| 572 | |
| 573 | |
| 574 | // ---------------------------------------------------------------------------- |
| 575 | static void |
| 576 | android_media_AudioTrack_flush(JNIEnv *env, jobject thiz) |
| 577 | { |
Eric Laurent | 532bc1c | 2012-04-20 12:45:03 -0700 | [diff] [blame] | 578 | sp<AudioTrack> lpTrack = getAudioTrack(env, thiz); |
| 579 | if (lpTrack == NULL) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 580 | jniThrowException(env, "java/lang/IllegalStateException", |
| 581 | "Unable to retrieve AudioTrack pointer for flush()"); |
Eric Laurent | 7070b36 | 2010-07-16 07:43:46 -0700 | [diff] [blame] | 582 | return; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 583 | } |
| 584 | |
| 585 | lpTrack->flush(); |
| 586 | } |
| 587 | |
| 588 | // ---------------------------------------------------------------------------- |
| 589 | static void |
| 590 | android_media_AudioTrack_set_volume(JNIEnv *env, jobject thiz, jfloat leftVol, jfloat rightVol ) |
| 591 | { |
Eric Laurent | 532bc1c | 2012-04-20 12:45:03 -0700 | [diff] [blame] | 592 | sp<AudioTrack> lpTrack = getAudioTrack(env, thiz); |
| 593 | if (lpTrack == NULL) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 594 | jniThrowException(env, "java/lang/IllegalStateException", |
| 595 | "Unable to retrieve AudioTrack pointer for setVolume()"); |
Eric Laurent | 7070b36 | 2010-07-16 07:43:46 -0700 | [diff] [blame] | 596 | return; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 597 | } |
| 598 | |
| 599 | lpTrack->setVolume(leftVol, rightVol); |
| 600 | } |
| 601 | |
| 602 | // ---------------------------------------------------------------------------- |
Glenn Kasten | 18db49a | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 603 | |
Eric Laurent | 532bc1c | 2012-04-20 12:45:03 -0700 | [diff] [blame] | 604 | #define CALLBACK_COND_WAIT_TIMEOUT_MS 1000 |
Jean-Michel Trivi | eac8438 | 2014-02-04 14:50:40 -0800 | [diff] [blame] | 605 | static void android_media_AudioTrack_release(JNIEnv *env, jobject thiz) { |
Eric Laurent | 532bc1c | 2012-04-20 12:45:03 -0700 | [diff] [blame] | 606 | sp<AudioTrack> lpTrack = setAudioTrack(env, thiz, 0); |
| 607 | if (lpTrack == NULL) { |
| 608 | return; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 609 | } |
Eric Laurent | 532bc1c | 2012-04-20 12:45:03 -0700 | [diff] [blame] | 610 | //ALOGV("deleting lpTrack: %x\n", (int)lpTrack); |
Glenn Kasten | 18db49a | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 611 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 612 | // delete the JNI data |
Ashok Bhat | 075e9a1 | 2014-01-06 13:45:09 +0000 | [diff] [blame] | 613 | AudioTrackJniStorage* pJniStorage = (AudioTrackJniStorage *)env->GetLongField( |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 614 | thiz, javaAudioTrackFields.jniData); |
Eric Laurent | 532bc1c | 2012-04-20 12:45:03 -0700 | [diff] [blame] | 615 | // reset the native resources in the Java object so any attempt to access |
| 616 | // them after a call to release fails. |
Ashok Bhat | 075e9a1 | 2014-01-06 13:45:09 +0000 | [diff] [blame] | 617 | env->SetLongField(thiz, javaAudioTrackFields.jniData, 0); |
Eric Laurent | 532bc1c | 2012-04-20 12:45:03 -0700 | [diff] [blame] | 618 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 619 | if (pJniStorage) { |
Eric Laurent | 532bc1c | 2012-04-20 12:45:03 -0700 | [diff] [blame] | 620 | Mutex::Autolock l(sLock); |
| 621 | audiotrack_callback_cookie *lpCookie = &pJniStorage->mCallbackData; |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 622 | //ALOGV("deleting pJniStorage: %x\n", (int)pJniStorage); |
Eric Laurent | 532bc1c | 2012-04-20 12:45:03 -0700 | [diff] [blame] | 623 | while (lpCookie->busy) { |
| 624 | if (lpCookie->cond.waitRelative(sLock, |
| 625 | milliseconds(CALLBACK_COND_WAIT_TIMEOUT_MS)) != |
| 626 | NO_ERROR) { |
| 627 | break; |
| 628 | } |
| 629 | } |
| 630 | sAudioTrackCallBackCookies.remove(lpCookie); |
| 631 | // delete global refs created in native_setup |
| 632 | env->DeleteGlobalRef(lpCookie->audioTrack_class); |
| 633 | env->DeleteGlobalRef(lpCookie->audioTrack_ref); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 634 | delete pJniStorage; |
| 635 | } |
| 636 | } |
| 637 | |
Glenn Kasten | 18db49a | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 638 | |
Eric Laurent | 532bc1c | 2012-04-20 12:45:03 -0700 | [diff] [blame] | 639 | // ---------------------------------------------------------------------------- |
Jean-Michel Trivi | eac8438 | 2014-02-04 14:50:40 -0800 | [diff] [blame] | 640 | static void android_media_AudioTrack_finalize(JNIEnv *env, jobject thiz) { |
| 641 | //ALOGV("android_media_AudioTrack_finalize jobject: %x\n", (int)thiz); |
| 642 | android_media_AudioTrack_release(env, thiz); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 643 | } |
| 644 | |
Andy Hung | 4aacc90 | 2015-04-14 15:01:29 -0700 | [diff] [blame] | 645 | // overloaded JNI array helper functions (same as in android_media_AudioRecord) |
| 646 | static inline |
| 647 | jbyte *envGetArrayElements(JNIEnv *env, jbyteArray array, jboolean *isCopy) { |
| 648 | return env->GetByteArrayElements(array, isCopy); |
| 649 | } |
| 650 | |
| 651 | static inline |
| 652 | void envReleaseArrayElements(JNIEnv *env, jbyteArray array, jbyte *elems, jint mode) { |
| 653 | env->ReleaseByteArrayElements(array, elems, mode); |
| 654 | } |
| 655 | |
| 656 | static inline |
| 657 | jshort *envGetArrayElements(JNIEnv *env, jshortArray array, jboolean *isCopy) { |
| 658 | return env->GetShortArrayElements(array, isCopy); |
| 659 | } |
| 660 | |
| 661 | static inline |
| 662 | void envReleaseArrayElements(JNIEnv *env, jshortArray array, jshort *elems, jint mode) { |
| 663 | env->ReleaseShortArrayElements(array, elems, mode); |
| 664 | } |
| 665 | |
| 666 | static inline |
| 667 | jfloat *envGetArrayElements(JNIEnv *env, jfloatArray array, jboolean *isCopy) { |
| 668 | return env->GetFloatArrayElements(array, isCopy); |
| 669 | } |
| 670 | |
| 671 | static inline |
| 672 | void envReleaseArrayElements(JNIEnv *env, jfloatArray array, jfloat *elems, jint mode) { |
| 673 | env->ReleaseFloatArrayElements(array, elems, mode); |
| 674 | } |
| 675 | |
Eric Laurent | 219de73 | 2016-05-23 12:41:50 -0700 | [diff] [blame] | 676 | static inline |
| 677 | jint interpretWriteSizeError(ssize_t writeSize) { |
| 678 | if (writeSize == WOULD_BLOCK) { |
| 679 | return (jint)0; |
| 680 | } else if (writeSize == NO_INIT) { |
| 681 | return AUDIO_JAVA_DEAD_OBJECT; |
| 682 | } else { |
| 683 | ALOGE("Error %zd during AudioTrack native read", writeSize); |
| 684 | return nativeToJavaStatus(writeSize); |
| 685 | } |
| 686 | } |
| 687 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 688 | // ---------------------------------------------------------------------------- |
Andy Hung | 4aacc90 | 2015-04-14 15:01:29 -0700 | [diff] [blame] | 689 | template <typename T> |
| 690 | static jint writeToTrack(const sp<AudioTrack>& track, jint audioFormat, const T *data, |
| 691 | jint offsetInSamples, jint sizeInSamples, bool blocking) { |
Jean-Michel Trivi | 21dc037 | 2009-05-08 16:06:34 -0700 | [diff] [blame] | 692 | // give the data to the native AudioTrack object (the data starts at the offset) |
| 693 | ssize_t written = 0; |
| 694 | // regular write() or copy the data to the AudioTrack's shared memory? |
Andy Hung | 4aacc90 | 2015-04-14 15:01:29 -0700 | [diff] [blame] | 695 | size_t sizeInBytes = sizeInSamples * sizeof(T); |
Eric Laurent | 532bc1c | 2012-04-20 12:45:03 -0700 | [diff] [blame] | 696 | if (track->sharedBuffer() == 0) { |
Andy Hung | 4aacc90 | 2015-04-14 15:01:29 -0700 | [diff] [blame] | 697 | written = track->write(data + offsetInSamples, sizeInBytes, blocking); |
Glenn Kasten | 9b53db3 | 2013-07-10 14:13:39 -0700 | [diff] [blame] | 698 | // for compatibility with earlier behavior of write(), return 0 in this case |
| 699 | if (written == (ssize_t) WOULD_BLOCK) { |
| 700 | written = 0; |
| 701 | } |
Jean-Michel Trivi | 21dc037 | 2009-05-08 16:06:34 -0700 | [diff] [blame] | 702 | } else { |
Andy Hung | 2c0e17c | 2015-01-12 15:07:14 -0800 | [diff] [blame] | 703 | // writing to shared memory, check for capacity |
| 704 | if ((size_t)sizeInBytes > track->sharedBuffer()->size()) { |
| 705 | sizeInBytes = track->sharedBuffer()->size(); |
Jean-Michel Trivi | 21dc037 | 2009-05-08 16:06:34 -0700 | [diff] [blame] | 706 | } |
Ytai Ben-Tsvi | 2c9d8f6 | 2019-09-05 15:14:31 -0700 | [diff] [blame] | 707 | memcpy(track->sharedBuffer()->unsecurePointer(), data + offsetInSamples, sizeInBytes); |
Andy Hung | 2c0e17c | 2015-01-12 15:07:14 -0800 | [diff] [blame] | 708 | written = sizeInBytes; |
Jean-Michel Trivi | 21dc037 | 2009-05-08 16:06:34 -0700 | [diff] [blame] | 709 | } |
Eric Laurent | 219de73 | 2016-05-23 12:41:50 -0700 | [diff] [blame] | 710 | if (written >= 0) { |
Andy Hung | 4aacc90 | 2015-04-14 15:01:29 -0700 | [diff] [blame] | 711 | return written / sizeof(T); |
| 712 | } |
Eric Laurent | 219de73 | 2016-05-23 12:41:50 -0700 | [diff] [blame] | 713 | return interpretWriteSizeError(written); |
Jean-Michel Trivi | 21dc037 | 2009-05-08 16:06:34 -0700 | [diff] [blame] | 714 | } |
| 715 | |
| 716 | // ---------------------------------------------------------------------------- |
Andy Hung | 4aacc90 | 2015-04-14 15:01:29 -0700 | [diff] [blame] | 717 | template <typename T> |
| 718 | static jint android_media_AudioTrack_writeArray(JNIEnv *env, jobject thiz, |
| 719 | T javaAudioData, |
| 720 | jint offsetInSamples, jint sizeInSamples, |
| 721 | jint javaAudioFormat, |
| 722 | jboolean isWriteBlocking) { |
| 723 | //ALOGV("android_media_AudioTrack_writeArray(offset=%d, sizeInSamples=%d) called", |
| 724 | // offsetInSamples, sizeInSamples); |
Eric Laurent | 532bc1c | 2012-04-20 12:45:03 -0700 | [diff] [blame] | 725 | sp<AudioTrack> lpTrack = getAudioTrack(env, thiz); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 726 | if (lpTrack == NULL) { |
| 727 | jniThrowException(env, "java/lang/IllegalStateException", |
| 728 | "Unable to retrieve AudioTrack pointer for write()"); |
Andy Hung | 4aacc90 | 2015-04-14 15:01:29 -0700 | [diff] [blame] | 729 | return (jint)AUDIO_JAVA_INVALID_OPERATION; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 730 | } |
| 731 | |
Andy Hung | 4aacc90 | 2015-04-14 15:01:29 -0700 | [diff] [blame] | 732 | if (javaAudioData == NULL) { |
| 733 | ALOGE("NULL java array of audio data to play"); |
| 734 | return (jint)AUDIO_JAVA_BAD_VALUE; |
| 735 | } |
| 736 | |
Eric Laurent | 421ddc0 | 2011-03-07 14:52:59 -0800 | [diff] [blame] | 737 | // NOTE: We may use GetPrimitiveArrayCritical() when the JNI implementation changes in such |
| 738 | // a way that it becomes much more efficient. When doing so, we will have to prevent the |
| 739 | // AudioSystem callback to be called while in critical section (in case of media server |
| 740 | // process crash for instance) |
Andy Hung | 4aacc90 | 2015-04-14 15:01:29 -0700 | [diff] [blame] | 741 | |
| 742 | // get the pointer for the audio data from the java array |
| 743 | auto cAudioData = envGetArrayElements(env, javaAudioData, NULL); |
| 744 | if (cAudioData == NULL) { |
| 745 | ALOGE("Error retrieving source of audio data to play"); |
| 746 | return (jint)AUDIO_JAVA_BAD_VALUE; // out of memory or no data to load |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 747 | } |
| 748 | |
Andy Hung | 4aacc90 | 2015-04-14 15:01:29 -0700 | [diff] [blame] | 749 | jint samplesWritten = writeToTrack(lpTrack, javaAudioFormat, cAudioData, |
| 750 | offsetInSamples, sizeInSamples, isWriteBlocking == JNI_TRUE /* blocking */); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 751 | |
Andy Hung | 4aacc90 | 2015-04-14 15:01:29 -0700 | [diff] [blame] | 752 | envReleaseArrayElements(env, javaAudioData, cAudioData, 0); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 753 | |
Andy Hung | 4aacc90 | 2015-04-14 15:01:29 -0700 | [diff] [blame] | 754 | //ALOGV("write wrote %d (tried %d) samples in the native AudioTrack with offset %d", |
| 755 | // (int)samplesWritten, (int)(sizeInSamples), (int)offsetInSamples); |
| 756 | return samplesWritten; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 757 | } |
| 758 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 759 | // ---------------------------------------------------------------------------- |
Jean-Michel Trivi | 7ca0452 | 2014-02-07 09:39:34 -0800 | [diff] [blame] | 760 | static jint android_media_AudioTrack_write_native_bytes(JNIEnv *env, jobject thiz, |
Orion Hodson | 9b909c3 | 2018-11-23 11:05:27 +0000 | [diff] [blame] | 761 | jobject javaByteBuffer, jint byteOffset, jint sizeInBytes, |
Jean-Michel Trivi | 7ca0452 | 2014-02-07 09:39:34 -0800 | [diff] [blame] | 762 | jint javaAudioFormat, jboolean isWriteBlocking) { |
| 763 | //ALOGV("android_media_AudioTrack_write_native_bytes(offset=%d, sizeInBytes=%d) called", |
| 764 | // offsetInBytes, sizeInBytes); |
| 765 | sp<AudioTrack> lpTrack = getAudioTrack(env, thiz); |
| 766 | if (lpTrack == NULL) { |
| 767 | jniThrowException(env, "java/lang/IllegalStateException", |
| 768 | "Unable to retrieve AudioTrack pointer for write()"); |
Andy Hung | 4aacc90 | 2015-04-14 15:01:29 -0700 | [diff] [blame] | 769 | return (jint)AUDIO_JAVA_INVALID_OPERATION; |
Jean-Michel Trivi | 7ca0452 | 2014-02-07 09:39:34 -0800 | [diff] [blame] | 770 | } |
| 771 | |
Orion Hodson | 9b909c3 | 2018-11-23 11:05:27 +0000 | [diff] [blame] | 772 | const jbyte* bytes = |
| 773 | reinterpret_cast<const jbyte*>(env->GetDirectBufferAddress(javaByteBuffer)); |
| 774 | if (bytes == NULL) { |
Jean-Michel Trivi | 7ca0452 | 2014-02-07 09:39:34 -0800 | [diff] [blame] | 775 | ALOGE("Error retrieving source of audio data to play, can't play"); |
Eric Laurent | bc11a69 | 2014-05-16 12:19:25 -0700 | [diff] [blame] | 776 | return (jint)AUDIO_JAVA_BAD_VALUE; |
Jean-Michel Trivi | 7ca0452 | 2014-02-07 09:39:34 -0800 | [diff] [blame] | 777 | } |
| 778 | |
Orion Hodson | 9b909c3 | 2018-11-23 11:05:27 +0000 | [diff] [blame] | 779 | jint written = writeToTrack(lpTrack, javaAudioFormat, bytes, byteOffset, |
Jean-Michel Trivi | 7ca0452 | 2014-02-07 09:39:34 -0800 | [diff] [blame] | 780 | sizeInBytes, isWriteBlocking == JNI_TRUE /* blocking */); |
| 781 | |
| 782 | return written; |
| 783 | } |
| 784 | |
| 785 | // ---------------------------------------------------------------------------- |
Phil Burk | 10a33e4 | 2016-01-08 12:40:41 -0800 | [diff] [blame] | 786 | static jint android_media_AudioTrack_get_buffer_size_frames(JNIEnv *env, jobject thiz) { |
Eric Laurent | 532bc1c | 2012-04-20 12:45:03 -0700 | [diff] [blame] | 787 | sp<AudioTrack> lpTrack = getAudioTrack(env, thiz); |
| 788 | if (lpTrack == NULL) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 789 | jniThrowException(env, "java/lang/IllegalStateException", |
Phil Burk | 10a33e4 | 2016-01-08 12:40:41 -0800 | [diff] [blame] | 790 | "Unable to retrieve AudioTrack pointer for getBufferSizeInFrames()"); |
| 791 | return (jint)AUDIO_JAVA_ERROR; |
| 792 | } |
| 793 | |
| 794 | ssize_t result = lpTrack->getBufferSizeInFrames(); |
| 795 | if (result < 0) { |
Ben Wagner | 85ea32b | 2016-03-16 17:10:42 -0400 | [diff] [blame] | 796 | jniThrowExceptionFmt(env, "java/lang/IllegalStateException", |
| 797 | "Internal error detected in getBufferSizeInFrames() = %zd", result); |
Phil Burk | 10a33e4 | 2016-01-08 12:40:41 -0800 | [diff] [blame] | 798 | return (jint)AUDIO_JAVA_ERROR; |
| 799 | } |
| 800 | return (jint)result; |
| 801 | } |
| 802 | |
| 803 | // ---------------------------------------------------------------------------- |
| 804 | static jint android_media_AudioTrack_set_buffer_size_frames(JNIEnv *env, |
| 805 | jobject thiz, jint bufferSizeInFrames) { |
| 806 | sp<AudioTrack> lpTrack = getAudioTrack(env, thiz); |
| 807 | if (lpTrack == NULL) { |
| 808 | jniThrowException(env, "java/lang/IllegalStateException", |
| 809 | "Unable to retrieve AudioTrack pointer for setBufferSizeInFrames()"); |
| 810 | return (jint)AUDIO_JAVA_ERROR; |
| 811 | } |
| 812 | // Value will be coerced into the valid range. |
| 813 | // But internal values are unsigned, size_t, so we need to clip |
| 814 | // against zero here where it is signed. |
| 815 | if (bufferSizeInFrames < 0) { |
| 816 | bufferSizeInFrames = 0; |
| 817 | } |
| 818 | ssize_t result = lpTrack->setBufferSizeInFrames(bufferSizeInFrames); |
| 819 | if (result < 0) { |
Ben Wagner | 85ea32b | 2016-03-16 17:10:42 -0400 | [diff] [blame] | 820 | jniThrowExceptionFmt(env, "java/lang/IllegalStateException", |
| 821 | "Internal error detected in setBufferSizeInFrames() = %zd", result); |
Phil Burk | 10a33e4 | 2016-01-08 12:40:41 -0800 | [diff] [blame] | 822 | return (jint)AUDIO_JAVA_ERROR; |
| 823 | } |
| 824 | return (jint)result; |
| 825 | } |
| 826 | |
| 827 | // ---------------------------------------------------------------------------- |
| 828 | static jint android_media_AudioTrack_get_buffer_capacity_frames(JNIEnv *env, jobject thiz) { |
| 829 | sp<AudioTrack> lpTrack = getAudioTrack(env, thiz); |
| 830 | if (lpTrack == NULL) { |
| 831 | jniThrowException(env, "java/lang/IllegalStateException", |
| 832 | "Unable to retrieve AudioTrack pointer for getBufferCapacityInFrames()"); |
Eric Laurent | bc11a69 | 2014-05-16 12:19:25 -0700 | [diff] [blame] | 833 | return (jint)AUDIO_JAVA_ERROR; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 834 | } |
Eric Laurent | 532bc1c | 2012-04-20 12:45:03 -0700 | [diff] [blame] | 835 | |
| 836 | return lpTrack->frameCount(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 837 | } |
| 838 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 839 | // ---------------------------------------------------------------------------- |
Eric Laurent | 88e209d | 2009-07-07 07:10:45 -0700 | [diff] [blame] | 840 | static jint android_media_AudioTrack_set_playback_rate(JNIEnv *env, jobject thiz, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 841 | jint sampleRateInHz) { |
Eric Laurent | 532bc1c | 2012-04-20 12:45:03 -0700 | [diff] [blame] | 842 | sp<AudioTrack> lpTrack = getAudioTrack(env, thiz); |
| 843 | if (lpTrack == NULL) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 844 | jniThrowException(env, "java/lang/IllegalStateException", |
| 845 | "Unable to retrieve AudioTrack pointer for setSampleRate()"); |
Eric Laurent | bc11a69 | 2014-05-16 12:19:25 -0700 | [diff] [blame] | 846 | return (jint)AUDIO_JAVA_ERROR; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 847 | } |
Eric Laurent | bc11a69 | 2014-05-16 12:19:25 -0700 | [diff] [blame] | 848 | return nativeToJavaStatus(lpTrack->setSampleRate(sampleRateInHz)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 849 | } |
| 850 | |
| 851 | |
| 852 | // ---------------------------------------------------------------------------- |
| 853 | static jint android_media_AudioTrack_get_playback_rate(JNIEnv *env, jobject thiz) { |
Eric Laurent | 532bc1c | 2012-04-20 12:45:03 -0700 | [diff] [blame] | 854 | sp<AudioTrack> lpTrack = getAudioTrack(env, thiz); |
| 855 | if (lpTrack == NULL) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 856 | jniThrowException(env, "java/lang/IllegalStateException", |
| 857 | "Unable to retrieve AudioTrack pointer for getSampleRate()"); |
Eric Laurent | bc11a69 | 2014-05-16 12:19:25 -0700 | [diff] [blame] | 858 | return (jint)AUDIO_JAVA_ERROR; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 859 | } |
Eric Laurent | 532bc1c | 2012-04-20 12:45:03 -0700 | [diff] [blame] | 860 | return (jint) lpTrack->getSampleRate(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 861 | } |
| 862 | |
| 863 | |
| 864 | // ---------------------------------------------------------------------------- |
Wei Jia | 2d61e2b | 2015-05-08 15:23:28 -0700 | [diff] [blame] | 865 | static void android_media_AudioTrack_set_playback_params(JNIEnv *env, jobject thiz, |
| 866 | jobject params) { |
Andy Hung | 263b4c9 | 2015-04-16 11:16:29 -0700 | [diff] [blame] | 867 | sp<AudioTrack> lpTrack = getAudioTrack(env, thiz); |
| 868 | if (lpTrack == NULL) { |
| 869 | jniThrowException(env, "java/lang/IllegalStateException", |
| 870 | "AudioTrack not initialized"); |
| 871 | return; |
| 872 | } |
| 873 | |
Andy Hung | 973b885 | 2015-05-13 15:15:25 -0700 | [diff] [blame] | 874 | PlaybackParams pbp; |
| 875 | pbp.fillFromJobject(env, gPlaybackParamsFields, params); |
Andy Hung | fe48e0d | 2015-04-27 18:14:02 -0700 | [diff] [blame] | 876 | |
Wei Jia | 2d61e2b | 2015-05-08 15:23:28 -0700 | [diff] [blame] | 877 | ALOGV("setPlaybackParams: %d:%f %d:%f %d:%u %d:%u", |
Andy Hung | 973b885 | 2015-05-13 15:15:25 -0700 | [diff] [blame] | 878 | pbp.speedSet, pbp.audioRate.mSpeed, |
| 879 | pbp.pitchSet, pbp.audioRate.mPitch, |
| 880 | pbp.audioFallbackModeSet, pbp.audioRate.mFallbackMode, |
| 881 | pbp.audioStretchModeSet, pbp.audioRate.mStretchMode); |
Andy Hung | fe48e0d | 2015-04-27 18:14:02 -0700 | [diff] [blame] | 882 | |
Andy Hung | 973b885 | 2015-05-13 15:15:25 -0700 | [diff] [blame] | 883 | // to simulate partially set params, we do a read-modify-write. |
| 884 | // TODO: pass in the valid set mask into AudioTrack. |
| 885 | AudioPlaybackRate rate = lpTrack->getPlaybackRate(); |
| 886 | bool updatedRate = false; |
| 887 | if (pbp.speedSet) { |
| 888 | rate.mSpeed = pbp.audioRate.mSpeed; |
| 889 | updatedRate = true; |
| 890 | } |
| 891 | if (pbp.pitchSet) { |
| 892 | rate.mPitch = pbp.audioRate.mPitch; |
| 893 | updatedRate = true; |
| 894 | } |
| 895 | if (pbp.audioFallbackModeSet) { |
| 896 | rate.mFallbackMode = pbp.audioRate.mFallbackMode; |
| 897 | updatedRate = true; |
| 898 | } |
| 899 | if (pbp.audioStretchModeSet) { |
| 900 | rate.mStretchMode = pbp.audioRate.mStretchMode; |
| 901 | updatedRate = true; |
| 902 | } |
| 903 | if (updatedRate) { |
| 904 | if (lpTrack->setPlaybackRate(rate) != OK) { |
| 905 | jniThrowException(env, "java/lang/IllegalArgumentException", |
| 906 | "arguments out of range"); |
| 907 | } |
Andy Hung | 263b4c9 | 2015-04-16 11:16:29 -0700 | [diff] [blame] | 908 | } |
| 909 | } |
| 910 | |
| 911 | |
| 912 | // ---------------------------------------------------------------------------- |
Wei Jia | 2d61e2b | 2015-05-08 15:23:28 -0700 | [diff] [blame] | 913 | static jobject android_media_AudioTrack_get_playback_params(JNIEnv *env, jobject thiz, |
| 914 | jobject params) { |
Andy Hung | 263b4c9 | 2015-04-16 11:16:29 -0700 | [diff] [blame] | 915 | sp<AudioTrack> lpTrack = getAudioTrack(env, thiz); |
| 916 | if (lpTrack == NULL) { |
| 917 | jniThrowException(env, "java/lang/IllegalStateException", |
| 918 | "AudioTrack not initialized"); |
Andy Hung | fe48e0d | 2015-04-27 18:14:02 -0700 | [diff] [blame] | 919 | return NULL; |
Andy Hung | 263b4c9 | 2015-04-16 11:16:29 -0700 | [diff] [blame] | 920 | } |
| 921 | |
Wei Jia | 2d61e2b | 2015-05-08 15:23:28 -0700 | [diff] [blame] | 922 | PlaybackParams pbs; |
Andy Hung | fe48e0d | 2015-04-27 18:14:02 -0700 | [diff] [blame] | 923 | pbs.audioRate = lpTrack->getPlaybackRate(); |
| 924 | pbs.speedSet = true; |
| 925 | pbs.pitchSet = true; |
| 926 | pbs.audioFallbackModeSet = true; |
| 927 | pbs.audioStretchModeSet = true; |
Wei Jia | 2d61e2b | 2015-05-08 15:23:28 -0700 | [diff] [blame] | 928 | return pbs.asJobject(env, gPlaybackParamsFields); |
Andy Hung | 263b4c9 | 2015-04-16 11:16:29 -0700 | [diff] [blame] | 929 | } |
| 930 | |
| 931 | |
| 932 | // ---------------------------------------------------------------------------- |
Glenn Kasten | 18db49a | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 933 | static jint android_media_AudioTrack_set_marker_pos(JNIEnv *env, jobject thiz, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 934 | jint markerPos) { |
Eric Laurent | 532bc1c | 2012-04-20 12:45:03 -0700 | [diff] [blame] | 935 | sp<AudioTrack> lpTrack = getAudioTrack(env, thiz); |
| 936 | if (lpTrack == NULL) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 937 | jniThrowException(env, "java/lang/IllegalStateException", |
| 938 | "Unable to retrieve AudioTrack pointer for setMarkerPosition()"); |
Eric Laurent | bc11a69 | 2014-05-16 12:19:25 -0700 | [diff] [blame] | 939 | return (jint)AUDIO_JAVA_ERROR; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 940 | } |
Eric Laurent | bc11a69 | 2014-05-16 12:19:25 -0700 | [diff] [blame] | 941 | return nativeToJavaStatus( lpTrack->setMarkerPosition(markerPos) ); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 942 | } |
| 943 | |
| 944 | |
| 945 | // ---------------------------------------------------------------------------- |
| 946 | static jint android_media_AudioTrack_get_marker_pos(JNIEnv *env, jobject thiz) { |
Eric Laurent | 532bc1c | 2012-04-20 12:45:03 -0700 | [diff] [blame] | 947 | sp<AudioTrack> lpTrack = getAudioTrack(env, thiz); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 948 | uint32_t markerPos = 0; |
Glenn Kasten | 18db49a | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 949 | |
Eric Laurent | 532bc1c | 2012-04-20 12:45:03 -0700 | [diff] [blame] | 950 | if (lpTrack == NULL) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 951 | jniThrowException(env, "java/lang/IllegalStateException", |
| 952 | "Unable to retrieve AudioTrack pointer for getMarkerPosition()"); |
Eric Laurent | bc11a69 | 2014-05-16 12:19:25 -0700 | [diff] [blame] | 953 | return (jint)AUDIO_JAVA_ERROR; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 954 | } |
Eric Laurent | 532bc1c | 2012-04-20 12:45:03 -0700 | [diff] [blame] | 955 | lpTrack->getMarkerPosition(&markerPos); |
| 956 | return (jint)markerPos; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 957 | } |
| 958 | |
| 959 | |
| 960 | // ---------------------------------------------------------------------------- |
| 961 | static jint android_media_AudioTrack_set_pos_update_period(JNIEnv *env, jobject thiz, |
| 962 | jint period) { |
Eric Laurent | 532bc1c | 2012-04-20 12:45:03 -0700 | [diff] [blame] | 963 | sp<AudioTrack> lpTrack = getAudioTrack(env, thiz); |
| 964 | if (lpTrack == NULL) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 965 | jniThrowException(env, "java/lang/IllegalStateException", |
| 966 | "Unable to retrieve AudioTrack pointer for setPositionUpdatePeriod()"); |
Eric Laurent | bc11a69 | 2014-05-16 12:19:25 -0700 | [diff] [blame] | 967 | return (jint)AUDIO_JAVA_ERROR; |
Glenn Kasten | 18db49a | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 968 | } |
Eric Laurent | bc11a69 | 2014-05-16 12:19:25 -0700 | [diff] [blame] | 969 | return nativeToJavaStatus( lpTrack->setPositionUpdatePeriod(period) ); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 970 | } |
| 971 | |
| 972 | |
| 973 | // ---------------------------------------------------------------------------- |
| 974 | static jint android_media_AudioTrack_get_pos_update_period(JNIEnv *env, jobject thiz) { |
Eric Laurent | 532bc1c | 2012-04-20 12:45:03 -0700 | [diff] [blame] | 975 | sp<AudioTrack> lpTrack = getAudioTrack(env, thiz); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 976 | uint32_t period = 0; |
Glenn Kasten | 18db49a | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 977 | |
Eric Laurent | 532bc1c | 2012-04-20 12:45:03 -0700 | [diff] [blame] | 978 | if (lpTrack == NULL) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 979 | jniThrowException(env, "java/lang/IllegalStateException", |
| 980 | "Unable to retrieve AudioTrack pointer for getPositionUpdatePeriod()"); |
Eric Laurent | bc11a69 | 2014-05-16 12:19:25 -0700 | [diff] [blame] | 981 | return (jint)AUDIO_JAVA_ERROR; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 982 | } |
Eric Laurent | 532bc1c | 2012-04-20 12:45:03 -0700 | [diff] [blame] | 983 | lpTrack->getPositionUpdatePeriod(&period); |
| 984 | return (jint)period; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 985 | } |
| 986 | |
| 987 | |
| 988 | // ---------------------------------------------------------------------------- |
Glenn Kasten | 18db49a | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 989 | static jint android_media_AudioTrack_set_position(JNIEnv *env, jobject thiz, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 990 | jint position) { |
Eric Laurent | 532bc1c | 2012-04-20 12:45:03 -0700 | [diff] [blame] | 991 | sp<AudioTrack> lpTrack = getAudioTrack(env, thiz); |
| 992 | if (lpTrack == NULL) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 993 | jniThrowException(env, "java/lang/IllegalStateException", |
| 994 | "Unable to retrieve AudioTrack pointer for setPosition()"); |
Eric Laurent | bc11a69 | 2014-05-16 12:19:25 -0700 | [diff] [blame] | 995 | return (jint)AUDIO_JAVA_ERROR; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 996 | } |
Eric Laurent | bc11a69 | 2014-05-16 12:19:25 -0700 | [diff] [blame] | 997 | return nativeToJavaStatus( lpTrack->setPosition(position) ); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 998 | } |
| 999 | |
| 1000 | |
| 1001 | // ---------------------------------------------------------------------------- |
| 1002 | static jint android_media_AudioTrack_get_position(JNIEnv *env, jobject thiz) { |
Eric Laurent | 532bc1c | 2012-04-20 12:45:03 -0700 | [diff] [blame] | 1003 | sp<AudioTrack> lpTrack = getAudioTrack(env, thiz); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1004 | uint32_t position = 0; |
Glenn Kasten | 18db49a | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 1005 | |
Eric Laurent | 532bc1c | 2012-04-20 12:45:03 -0700 | [diff] [blame] | 1006 | if (lpTrack == NULL) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1007 | jniThrowException(env, "java/lang/IllegalStateException", |
| 1008 | "Unable to retrieve AudioTrack pointer for getPosition()"); |
Eric Laurent | bc11a69 | 2014-05-16 12:19:25 -0700 | [diff] [blame] | 1009 | return (jint)AUDIO_JAVA_ERROR; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1010 | } |
Eric Laurent | 532bc1c | 2012-04-20 12:45:03 -0700 | [diff] [blame] | 1011 | lpTrack->getPosition(&position); |
| 1012 | return (jint)position; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1013 | } |
| 1014 | |
| 1015 | |
| 1016 | // ---------------------------------------------------------------------------- |
Oliver Woodman | 61dcdf3 | 2013-06-26 12:43:36 +0100 | [diff] [blame] | 1017 | static jint android_media_AudioTrack_get_latency(JNIEnv *env, jobject thiz) { |
| 1018 | sp<AudioTrack> lpTrack = getAudioTrack(env, thiz); |
| 1019 | |
| 1020 | if (lpTrack == NULL) { |
| 1021 | jniThrowException(env, "java/lang/IllegalStateException", |
| 1022 | "Unable to retrieve AudioTrack pointer for latency()"); |
Eric Laurent | bc11a69 | 2014-05-16 12:19:25 -0700 | [diff] [blame] | 1023 | return (jint)AUDIO_JAVA_ERROR; |
Oliver Woodman | 61dcdf3 | 2013-06-26 12:43:36 +0100 | [diff] [blame] | 1024 | } |
| 1025 | return (jint)lpTrack->latency(); |
| 1026 | } |
| 1027 | |
Phil Burk | 03f61bb | 2016-01-17 21:49:58 +0000 | [diff] [blame] | 1028 | // ---------------------------------------------------------------------------- |
| 1029 | static jint android_media_AudioTrack_get_underrun_count(JNIEnv *env, jobject thiz) { |
| 1030 | sp<AudioTrack> lpTrack = getAudioTrack(env, thiz); |
| 1031 | |
| 1032 | if (lpTrack == NULL) { |
| 1033 | jniThrowException(env, "java/lang/IllegalStateException", |
| 1034 | "Unable to retrieve AudioTrack pointer for getUnderrunCount()"); |
| 1035 | return (jint)AUDIO_JAVA_ERROR; |
| 1036 | } |
| 1037 | return (jint)lpTrack->getUnderrunCount(); |
| 1038 | } |
Oliver Woodman | 61dcdf3 | 2013-06-26 12:43:36 +0100 | [diff] [blame] | 1039 | |
| 1040 | // ---------------------------------------------------------------------------- |
Andy Hung | ebc2c14 | 2017-01-12 19:20:29 -0800 | [diff] [blame] | 1041 | static jint android_media_AudioTrack_get_flags(JNIEnv *env, jobject thiz) { |
| 1042 | sp<AudioTrack> lpTrack = getAudioTrack(env, thiz); |
| 1043 | |
| 1044 | if (lpTrack == NULL) { |
| 1045 | jniThrowException(env, "java/lang/IllegalStateException", |
| 1046 | "Unable to retrieve AudioTrack pointer for getFlags()"); |
| 1047 | return (jint)AUDIO_JAVA_ERROR; |
| 1048 | } |
| 1049 | return (jint)lpTrack->getFlags(); |
| 1050 | } |
| 1051 | |
| 1052 | // ---------------------------------------------------------------------------- |
Glenn Kasten | 948c2e6 | 2013-09-04 13:51:29 -0700 | [diff] [blame] | 1053 | static jint android_media_AudioTrack_get_timestamp(JNIEnv *env, jobject thiz, jlongArray jTimestamp) { |
| 1054 | sp<AudioTrack> lpTrack = getAudioTrack(env, thiz); |
| 1055 | |
| 1056 | if (lpTrack == NULL) { |
| 1057 | ALOGE("Unable to retrieve AudioTrack pointer for getTimestamp()"); |
Eric Laurent | bc11a69 | 2014-05-16 12:19:25 -0700 | [diff] [blame] | 1058 | return (jint)AUDIO_JAVA_ERROR; |
Glenn Kasten | 948c2e6 | 2013-09-04 13:51:29 -0700 | [diff] [blame] | 1059 | } |
| 1060 | AudioTimestamp timestamp; |
| 1061 | status_t status = lpTrack->getTimestamp(timestamp); |
| 1062 | if (status == OK) { |
| 1063 | jlong* nTimestamp = (jlong *) env->GetPrimitiveArrayCritical(jTimestamp, NULL); |
| 1064 | if (nTimestamp == NULL) { |
| 1065 | ALOGE("Unable to get array for getTimestamp()"); |
Eric Laurent | bc11a69 | 2014-05-16 12:19:25 -0700 | [diff] [blame] | 1066 | return (jint)AUDIO_JAVA_ERROR; |
Glenn Kasten | 948c2e6 | 2013-09-04 13:51:29 -0700 | [diff] [blame] | 1067 | } |
| 1068 | nTimestamp[0] = (jlong) timestamp.mPosition; |
| 1069 | nTimestamp[1] = (jlong) ((timestamp.mTime.tv_sec * 1000000000LL) + timestamp.mTime.tv_nsec); |
| 1070 | env->ReleasePrimitiveArrayCritical(jTimestamp, nTimestamp, 0); |
| 1071 | } |
Eric Laurent | bc11a69 | 2014-05-16 12:19:25 -0700 | [diff] [blame] | 1072 | return (jint) nativeToJavaStatus(status); |
Glenn Kasten | 948c2e6 | 2013-09-04 13:51:29 -0700 | [diff] [blame] | 1073 | } |
| 1074 | |
Ray Essick | 510225b | 2018-01-24 14:27:16 -0800 | [diff] [blame] | 1075 | // ---------------------------------------------------------------------------- |
| 1076 | static jobject |
| 1077 | android_media_AudioTrack_native_getMetrics(JNIEnv *env, jobject thiz) |
| 1078 | { |
| 1079 | ALOGD("android_media_AudioTrack_native_getMetrics"); |
| 1080 | |
| 1081 | sp<AudioTrack> lpTrack = getAudioTrack(env, thiz); |
| 1082 | |
| 1083 | if (lpTrack == NULL) { |
| 1084 | ALOGE("Unable to retrieve AudioTrack pointer for getMetrics()"); |
| 1085 | jniThrowException(env, "java/lang/IllegalStateException", NULL); |
| 1086 | return (jobject) NULL; |
| 1087 | } |
| 1088 | |
| 1089 | // get what we have for the metrics from the track |
Ray Essick | 81fbc5b | 2019-12-07 06:24:59 -0800 | [diff] [blame] | 1090 | mediametrics::Item *item = NULL; |
Ray Essick | 510225b | 2018-01-24 14:27:16 -0800 | [diff] [blame] | 1091 | |
| 1092 | status_t err = lpTrack->getMetrics(item); |
| 1093 | if (err != OK) { |
| 1094 | ALOGE("getMetrics failed"); |
| 1095 | jniThrowException(env, "java/lang/IllegalStateException", NULL); |
| 1096 | return (jobject) NULL; |
| 1097 | } |
| 1098 | |
| 1099 | jobject mybundle = MediaMetricsJNI::writeMetricsToBundle(env, item, NULL /* mybundle */); |
| 1100 | |
| 1101 | // housekeeping |
| 1102 | delete item; |
| 1103 | item = NULL; |
| 1104 | |
| 1105 | return mybundle; |
| 1106 | } |
| 1107 | |
Glenn Kasten | 948c2e6 | 2013-09-04 13:51:29 -0700 | [diff] [blame] | 1108 | |
| 1109 | // ---------------------------------------------------------------------------- |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1110 | static jint android_media_AudioTrack_set_loop(JNIEnv *env, jobject thiz, |
| 1111 | jint loopStart, jint loopEnd, jint loopCount) { |
Eric Laurent | 532bc1c | 2012-04-20 12:45:03 -0700 | [diff] [blame] | 1112 | sp<AudioTrack> lpTrack = getAudioTrack(env, thiz); |
| 1113 | if (lpTrack == NULL) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1114 | jniThrowException(env, "java/lang/IllegalStateException", |
| 1115 | "Unable to retrieve AudioTrack pointer for setLoop()"); |
Eric Laurent | bc11a69 | 2014-05-16 12:19:25 -0700 | [diff] [blame] | 1116 | return (jint)AUDIO_JAVA_ERROR; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1117 | } |
Eric Laurent | bc11a69 | 2014-05-16 12:19:25 -0700 | [diff] [blame] | 1118 | return nativeToJavaStatus( lpTrack->setLoop(loopStart, loopEnd, loopCount) ); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1119 | } |
| 1120 | |
| 1121 | |
| 1122 | // ---------------------------------------------------------------------------- |
| 1123 | static jint android_media_AudioTrack_reload(JNIEnv *env, jobject thiz) { |
Eric Laurent | 532bc1c | 2012-04-20 12:45:03 -0700 | [diff] [blame] | 1124 | sp<AudioTrack> lpTrack = getAudioTrack(env, thiz); |
| 1125 | if (lpTrack == NULL) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1126 | jniThrowException(env, "java/lang/IllegalStateException", |
| 1127 | "Unable to retrieve AudioTrack pointer for reload()"); |
Eric Laurent | bc11a69 | 2014-05-16 12:19:25 -0700 | [diff] [blame] | 1128 | return (jint)AUDIO_JAVA_ERROR; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1129 | } |
Eric Laurent | bc11a69 | 2014-05-16 12:19:25 -0700 | [diff] [blame] | 1130 | return nativeToJavaStatus( lpTrack->reload() ); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1131 | } |
| 1132 | |
| 1133 | |
| 1134 | // ---------------------------------------------------------------------------- |
| 1135 | static jint android_media_AudioTrack_get_output_sample_rate(JNIEnv *env, jobject thiz, |
| 1136 | jint javaStreamType) { |
Glenn Kasten | 85fbc87 | 2012-11-14 13:21:09 -0800 | [diff] [blame] | 1137 | uint32_t afSamplingRate; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1138 | // convert the stream type from Java to native value |
Jean-Michel Trivi | eac8438 | 2014-02-04 14:50:40 -0800 | [diff] [blame] | 1139 | // FIXME: code duplication with android_media_AudioTrack_setup() |
Dima Zavin | 24fc2fb | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 1140 | audio_stream_type_t nativeStreamType; |
Glenn Kasten | 29a0909 | 2012-01-16 14:37:12 -0800 | [diff] [blame] | 1141 | switch (javaStreamType) { |
| 1142 | case AUDIO_STREAM_VOICE_CALL: |
| 1143 | case AUDIO_STREAM_SYSTEM: |
| 1144 | case AUDIO_STREAM_RING: |
| 1145 | case AUDIO_STREAM_MUSIC: |
| 1146 | case AUDIO_STREAM_ALARM: |
| 1147 | case AUDIO_STREAM_NOTIFICATION: |
| 1148 | case AUDIO_STREAM_BLUETOOTH_SCO: |
| 1149 | case AUDIO_STREAM_DTMF: |
| 1150 | nativeStreamType = (audio_stream_type_t) javaStreamType; |
| 1151 | break; |
| 1152 | default: |
Dima Zavin | 24fc2fb | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 1153 | nativeStreamType = AUDIO_STREAM_DEFAULT; |
Glenn Kasten | 29a0909 | 2012-01-16 14:37:12 -0800 | [diff] [blame] | 1154 | break; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1155 | } |
| 1156 | |
Glenn Kasten | 8f81d08 | 2012-11-28 14:37:48 -0800 | [diff] [blame] | 1157 | status_t status = AudioSystem::getOutputSamplingRate(&afSamplingRate, nativeStreamType); |
| 1158 | if (status != NO_ERROR) { |
| 1159 | ALOGE("Error %d in AudioSystem::getOutputSamplingRate() for stream type %d " |
| 1160 | "in AudioTrack JNI", status, nativeStreamType); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1161 | return DEFAULT_OUTPUT_SAMPLE_RATE; |
| 1162 | } else { |
| 1163 | return afSamplingRate; |
| 1164 | } |
| 1165 | } |
| 1166 | |
| 1167 | |
| 1168 | // ---------------------------------------------------------------------------- |
| 1169 | // returns the minimum required size for the successful creation of a streaming AudioTrack |
| 1170 | // returns -1 if there was an error querying the hardware. |
| 1171 | static jint android_media_AudioTrack_get_min_buff_size(JNIEnv *env, jobject thiz, |
Glenn Kasten | 5b8fd44 | 2013-11-14 09:44:14 -0800 | [diff] [blame] | 1172 | jint sampleRateInHertz, jint channelCount, jint audioFormat) { |
Chia-chi Yeh | c330807 | 2010-08-19 17:14:36 +0800 | [diff] [blame] | 1173 | |
Glenn Kasten | 659a971 | 2014-01-08 11:38:33 -0800 | [diff] [blame] | 1174 | size_t frameCount; |
| 1175 | const status_t status = AudioTrack::getMinFrameCount(&frameCount, AUDIO_STREAM_DEFAULT, |
| 1176 | sampleRateInHertz); |
| 1177 | if (status != NO_ERROR) { |
| 1178 | ALOGE("AudioTrack::getMinFrameCount() for sample rate %d failed with status %d", |
| 1179 | sampleRateInHertz, status); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1180 | return -1; |
| 1181 | } |
Glenn Kasten | fe834d3 | 2014-01-08 14:49:08 -0800 | [diff] [blame] | 1182 | const audio_format_t format = audioFormatToNative(audioFormat); |
Phil Burk | 43f4b27 | 2016-01-27 15:35:20 -0800 | [diff] [blame] | 1183 | if (audio_has_proportional_frames(format)) { |
Eric Laurent | ff0d9f0 | 2014-06-09 17:23:02 -0700 | [diff] [blame] | 1184 | const size_t bytesPerSample = audio_bytes_per_sample(format); |
| 1185 | return frameCount * channelCount * bytesPerSample; |
| 1186 | } else { |
| 1187 | return frameCount; |
| 1188 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1189 | } |
| 1190 | |
Eric Laurent | 7070b36 | 2010-07-16 07:43:46 -0700 | [diff] [blame] | 1191 | // ---------------------------------------------------------------------------- |
Glenn Kasten | 3009f0b | 2014-03-28 16:02:26 -0700 | [diff] [blame] | 1192 | static jint |
Eric Laurent | 7070b36 | 2010-07-16 07:43:46 -0700 | [diff] [blame] | 1193 | android_media_AudioTrack_setAuxEffectSendLevel(JNIEnv *env, jobject thiz, jfloat level ) |
| 1194 | { |
Eric Laurent | 532bc1c | 2012-04-20 12:45:03 -0700 | [diff] [blame] | 1195 | sp<AudioTrack> lpTrack = getAudioTrack(env, thiz); |
Eric Laurent | 7070b36 | 2010-07-16 07:43:46 -0700 | [diff] [blame] | 1196 | if (lpTrack == NULL ) { |
| 1197 | jniThrowException(env, "java/lang/IllegalStateException", |
| 1198 | "Unable to retrieve AudioTrack pointer for setAuxEffectSendLevel()"); |
Glenn Kasten | 3009f0b | 2014-03-28 16:02:26 -0700 | [diff] [blame] | 1199 | return -1; |
Eric Laurent | 7070b36 | 2010-07-16 07:43:46 -0700 | [diff] [blame] | 1200 | } |
| 1201 | |
Glenn Kasten | 3009f0b | 2014-03-28 16:02:26 -0700 | [diff] [blame] | 1202 | status_t status = lpTrack->setAuxEffectSendLevel(level); |
| 1203 | if (status != NO_ERROR) { |
| 1204 | ALOGE("AudioTrack::setAuxEffectSendLevel() for level %g failed with status %d", |
| 1205 | level, status); |
| 1206 | } |
| 1207 | return (jint) status; |
Eric Laurent | 7070b36 | 2010-07-16 07:43:46 -0700 | [diff] [blame] | 1208 | } |
| 1209 | |
| 1210 | // ---------------------------------------------------------------------------- |
| 1211 | static jint android_media_AudioTrack_attachAuxEffect(JNIEnv *env, jobject thiz, |
| 1212 | jint effectId) { |
Eric Laurent | 532bc1c | 2012-04-20 12:45:03 -0700 | [diff] [blame] | 1213 | sp<AudioTrack> lpTrack = getAudioTrack(env, thiz); |
| 1214 | if (lpTrack == NULL) { |
Eric Laurent | 7070b36 | 2010-07-16 07:43:46 -0700 | [diff] [blame] | 1215 | jniThrowException(env, "java/lang/IllegalStateException", |
| 1216 | "Unable to retrieve AudioTrack pointer for attachAuxEffect()"); |
Eric Laurent | bc11a69 | 2014-05-16 12:19:25 -0700 | [diff] [blame] | 1217 | return (jint)AUDIO_JAVA_ERROR; |
Eric Laurent | 7070b36 | 2010-07-16 07:43:46 -0700 | [diff] [blame] | 1218 | } |
Eric Laurent | bc11a69 | 2014-05-16 12:19:25 -0700 | [diff] [blame] | 1219 | return nativeToJavaStatus( lpTrack->attachAuxEffect(effectId) ); |
Eric Laurent | 7070b36 | 2010-07-16 07:43:46 -0700 | [diff] [blame] | 1220 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1221 | |
Paul McLean | 88e1d86 | 2015-04-06 16:36:51 -0700 | [diff] [blame] | 1222 | static jboolean android_media_AudioTrack_setOutputDevice( |
| 1223 | JNIEnv *env, jobject thiz, jint device_id) { |
| 1224 | |
| 1225 | sp<AudioTrack> lpTrack = getAudioTrack(env, thiz); |
Paul McLean | cef696e | 2015-05-21 08:51:18 -0700 | [diff] [blame] | 1226 | if (lpTrack == 0) { |
| 1227 | return false; |
| 1228 | } |
Paul McLean | 88e1d86 | 2015-04-06 16:36:51 -0700 | [diff] [blame] | 1229 | return lpTrack->setOutputDevice(device_id) == NO_ERROR; |
| 1230 | } |
| 1231 | |
Eric Laurent | 4bcdba8 | 2015-05-01 11:37:49 -0700 | [diff] [blame] | 1232 | static jint android_media_AudioTrack_getRoutedDeviceId( |
| 1233 | JNIEnv *env, jobject thiz) { |
| 1234 | |
| 1235 | sp<AudioTrack> lpTrack = getAudioTrack(env, thiz); |
| 1236 | if (lpTrack == NULL) { |
| 1237 | return 0; |
| 1238 | } |
| 1239 | return (jint)lpTrack->getRoutedDeviceId(); |
| 1240 | } |
| 1241 | |
| 1242 | static void android_media_AudioTrack_enableDeviceCallback( |
| 1243 | JNIEnv *env, jobject thiz) { |
| 1244 | |
| 1245 | sp<AudioTrack> lpTrack = getAudioTrack(env, thiz); |
| 1246 | if (lpTrack == NULL) { |
| 1247 | return; |
| 1248 | } |
| 1249 | AudioTrackJniStorage* pJniStorage = (AudioTrackJniStorage *)env->GetLongField( |
| 1250 | thiz, javaAudioTrackFields.jniData); |
| 1251 | if (pJniStorage == NULL || pJniStorage->mDeviceCallback != 0) { |
| 1252 | return; |
| 1253 | } |
| 1254 | pJniStorage->mDeviceCallback = |
| 1255 | new JNIDeviceCallback(env, thiz, pJniStorage->mCallbackData.audioTrack_ref, |
| 1256 | javaAudioTrackFields.postNativeEventInJava); |
| 1257 | lpTrack->addAudioDeviceCallback(pJniStorage->mDeviceCallback); |
| 1258 | } |
| 1259 | |
| 1260 | static void android_media_AudioTrack_disableDeviceCallback( |
| 1261 | JNIEnv *env, jobject thiz) { |
| 1262 | |
| 1263 | sp<AudioTrack> lpTrack = getAudioTrack(env, thiz); |
| 1264 | if (lpTrack == NULL) { |
| 1265 | return; |
| 1266 | } |
| 1267 | AudioTrackJniStorage* pJniStorage = (AudioTrackJniStorage *)env->GetLongField( |
| 1268 | thiz, javaAudioTrackFields.jniData); |
| 1269 | if (pJniStorage == NULL || pJniStorage->mDeviceCallback == 0) { |
| 1270 | return; |
| 1271 | } |
| 1272 | lpTrack->removeAudioDeviceCallback(pJniStorage->mDeviceCallback); |
| 1273 | pJniStorage->mDeviceCallback.clear(); |
| 1274 | } |
| 1275 | |
Andy Hung | 035d4ec | 2017-01-24 13:45:02 -0800 | [diff] [blame] | 1276 | // Pass through the arguments to the AudioFlinger track implementation. |
| 1277 | static jint android_media_AudioTrack_apply_volume_shaper(JNIEnv *env, jobject thiz, |
| 1278 | jobject jconfig, jobject joperation) { |
| 1279 | // NOTE: hard code here to prevent platform issues. Must match VolumeShaper.java |
| 1280 | const int VOLUME_SHAPER_INVALID_OPERATION = -38; |
| 1281 | |
| 1282 | sp<AudioTrack> lpTrack = getAudioTrack(env, thiz); |
| 1283 | if (lpTrack == nullptr) { |
| 1284 | return (jint)VOLUME_SHAPER_INVALID_OPERATION; |
| 1285 | } |
| 1286 | |
| 1287 | sp<VolumeShaper::Configuration> configuration; |
| 1288 | sp<VolumeShaper::Operation> operation; |
| 1289 | if (jconfig != nullptr) { |
| 1290 | configuration = VolumeShaperHelper::convertJobjectToConfiguration( |
| 1291 | env, gVolumeShaperFields, jconfig); |
| 1292 | ALOGV("applyVolumeShaper configuration: %s", configuration->toString().c_str()); |
| 1293 | } |
| 1294 | if (joperation != nullptr) { |
| 1295 | operation = VolumeShaperHelper::convertJobjectToOperation( |
| 1296 | env, gVolumeShaperFields, joperation); |
| 1297 | ALOGV("applyVolumeShaper operation: %s", operation->toString().c_str()); |
| 1298 | } |
| 1299 | VolumeShaper::Status status = lpTrack->applyVolumeShaper(configuration, operation); |
| 1300 | if (status == INVALID_OPERATION) { |
| 1301 | status = VOLUME_SHAPER_INVALID_OPERATION; |
| 1302 | } |
| 1303 | return (jint)status; // if status < 0 an error, else a VolumeShaper id |
| 1304 | } |
| 1305 | |
| 1306 | // Pass through the arguments to the AudioFlinger track implementation. |
| 1307 | static jobject android_media_AudioTrack_get_volume_shaper_state(JNIEnv *env, jobject thiz, |
| 1308 | jint id) { |
| 1309 | sp<AudioTrack> lpTrack = getAudioTrack(env, thiz); |
| 1310 | if (lpTrack == nullptr) { |
| 1311 | return (jobject)nullptr; |
| 1312 | } |
| 1313 | |
| 1314 | sp<VolumeShaper::State> state = lpTrack->getVolumeShaperState((int)id); |
| 1315 | if (state.get() == nullptr) { |
| 1316 | return (jobject)nullptr; |
| 1317 | } |
| 1318 | return VolumeShaperHelper::convertStateToJobject(env, gVolumeShaperFields, state); |
| 1319 | } |
Eric Laurent | 4bcdba8 | 2015-05-01 11:37:49 -0700 | [diff] [blame] | 1320 | |
Previr Rangroo | 58822be | 2017-11-28 17:53:54 +1100 | [diff] [blame] | 1321 | static int android_media_AudioTrack_setPresentation( |
| 1322 | JNIEnv *env, jobject thiz, jint presentationId, jint programId) { |
| 1323 | sp<AudioTrack> lpTrack = getAudioTrack(env, thiz); |
| 1324 | if (lpTrack == NULL) { |
| 1325 | jniThrowException(env, "java/lang/IllegalStateException", |
| 1326 | "AudioTrack not initialized"); |
| 1327 | return (jint)AUDIO_JAVA_ERROR; |
| 1328 | } |
| 1329 | |
| 1330 | return (jint)lpTrack->selectPresentation((int)presentationId, (int)programId); |
| 1331 | } |
| 1332 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1333 | // ---------------------------------------------------------------------------- |
Eric Laurent | 2dc5483 | 2018-11-20 17:49:43 -0800 | [diff] [blame] | 1334 | static jint android_media_AudioTrack_get_port_id(JNIEnv *env, jobject thiz) { |
| 1335 | sp<AudioTrack> lpTrack = getAudioTrack(env, thiz); |
| 1336 | if (lpTrack == NULL) { |
| 1337 | jniThrowException(env, "java/lang/IllegalStateException", |
| 1338 | "AudioTrack not initialized"); |
| 1339 | return (jint)AUDIO_PORT_HANDLE_NONE; |
| 1340 | } |
| 1341 | return (jint)lpTrack->getPortId(); |
| 1342 | } |
| 1343 | |
| 1344 | // ---------------------------------------------------------------------------- |
Jean-Michel Trivi | 157cba4 | 2019-01-25 18:40:03 -0800 | [diff] [blame] | 1345 | static void android_media_AudioTrack_set_delay_padding(JNIEnv *env, jobject thiz, |
| 1346 | jint delayInFrames, jint paddingInFrames) { |
| 1347 | sp<AudioTrack> lpTrack = getAudioTrack(env, thiz); |
| 1348 | if (lpTrack == NULL) { |
| 1349 | jniThrowException(env, "java/lang/IllegalStateException", |
| 1350 | "AudioTrack not initialized"); |
| 1351 | return; |
| 1352 | } |
| 1353 | AudioParameter param = AudioParameter(); |
| 1354 | param.addInt(String8(AUDIO_OFFLOAD_CODEC_DELAY_SAMPLES), (int) delayInFrames); |
| 1355 | param.addInt(String8(AUDIO_OFFLOAD_CODEC_PADDING_SAMPLES), (int) paddingInFrames); |
| 1356 | lpTrack->setParameters(param.toString()); |
| 1357 | } |
| 1358 | |
Andy Hung | c33c908 | 2020-01-17 14:05:06 -0800 | [diff] [blame] | 1359 | static jint android_media_AudioTrack_setAudioDescriptionMixLeveldB(JNIEnv *env, jobject thiz, |
| 1360 | jfloat level) { |
| 1361 | sp<AudioTrack> lpTrack = getAudioTrack(env, thiz); |
| 1362 | if (lpTrack == nullptr) { |
| 1363 | jniThrowException(env, "java/lang/IllegalStateException", "AudioTrack not initialized"); |
| 1364 | return (jint)AUDIO_JAVA_ERROR; |
| 1365 | } |
| 1366 | |
| 1367 | // TODO: replace in r-dev or r-tv-dev with code if HW is able to set audio mix level. |
| 1368 | return (jint)AUDIO_JAVA_ERROR; |
| 1369 | } |
| 1370 | |
| 1371 | static jint android_media_AudioTrack_getAudioDescriptionMixLeveldB(JNIEnv *env, jobject thiz, |
| 1372 | jfloatArray level) { |
| 1373 | sp<AudioTrack> lpTrack = getAudioTrack(env, thiz); |
| 1374 | if (lpTrack == nullptr) { |
| 1375 | ALOGE("%s: AudioTrack not initialized", __func__); |
| 1376 | return (jint)AUDIO_JAVA_ERROR; |
| 1377 | } |
| 1378 | jfloat *nativeLevel = (jfloat *)env->GetPrimitiveArrayCritical(level, NULL); |
| 1379 | if (nativeLevel == nullptr) { |
| 1380 | ALOGE("%s: Cannot retrieve level pointer", __func__); |
| 1381 | return (jint)AUDIO_JAVA_ERROR; |
| 1382 | } |
| 1383 | |
| 1384 | // TODO: replace in r-dev or r-tv-dev with code if HW is able to set audio mix level. |
| 1385 | // By contract we can return -infinity if unsupported. |
| 1386 | *nativeLevel = -std::numeric_limits<float>::infinity(); |
| 1387 | env->ReleasePrimitiveArrayCritical(level, nativeLevel, 0 /* mode */); |
| 1388 | nativeLevel = nullptr; |
| 1389 | return (jint)AUDIO_JAVA_SUCCESS; |
| 1390 | } |
| 1391 | |
| 1392 | static jint android_media_AudioTrack_setDualMonoMode(JNIEnv *env, jobject thiz, jint dualMonoMode) { |
| 1393 | sp<AudioTrack> lpTrack = getAudioTrack(env, thiz); |
| 1394 | if (lpTrack == nullptr) { |
| 1395 | jniThrowException(env, "java/lang/IllegalStateException", "AudioTrack not initialized"); |
| 1396 | return (jint)AUDIO_JAVA_ERROR; |
| 1397 | } |
| 1398 | |
| 1399 | // TODO: replace in r-dev or r-tv-dev with code if HW is able to set audio mix level. |
| 1400 | return (jint)AUDIO_JAVA_ERROR; |
| 1401 | } |
| 1402 | |
| 1403 | static jint android_media_AudioTrack_getDualMonoMode(JNIEnv *env, jobject thiz, |
| 1404 | jintArray dualMonoMode) { |
| 1405 | sp<AudioTrack> lpTrack = getAudioTrack(env, thiz); |
| 1406 | if (lpTrack == nullptr) { |
| 1407 | ALOGE("%s: AudioTrack not initialized", __func__); |
| 1408 | return (jint)AUDIO_JAVA_ERROR; |
| 1409 | } |
| 1410 | jfloat *nativeDualMonoMode = (jfloat *)env->GetPrimitiveArrayCritical(dualMonoMode, NULL); |
| 1411 | if (nativeDualMonoMode == nullptr) { |
| 1412 | ALOGE("%s: Cannot retrieve dualMonoMode pointer", __func__); |
| 1413 | return (jint)AUDIO_JAVA_ERROR; |
| 1414 | } |
| 1415 | |
| 1416 | // TODO: replace in r-dev or r-tv-dev with code if HW is able to select dual mono mode. |
| 1417 | // By contract we can return DUAL_MONO_MODE_OFF if unsupported. |
| 1418 | *nativeDualMonoMode = 0; // DUAL_MONO_MODE_OFF for now. |
| 1419 | env->ReleasePrimitiveArrayCritical(dualMonoMode, nativeDualMonoMode, 0 /* mode */); |
| 1420 | nativeDualMonoMode = nullptr; |
| 1421 | return (jint)AUDIO_JAVA_SUCCESS; |
| 1422 | } |
| 1423 | |
Jean-Michel Trivi | 157cba4 | 2019-01-25 18:40:03 -0800 | [diff] [blame] | 1424 | // ---------------------------------------------------------------------------- |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1425 | // ---------------------------------------------------------------------------- |
Daniel Micay | 76f6a86 | 2015-09-19 17:31:01 -0400 | [diff] [blame] | 1426 | static const JNINativeMethod gMethods[] = { |
Andy Hung | 59dffec | 2020-01-14 17:29:03 -0800 | [diff] [blame] | 1427 | // name, signature, funcPtr |
| 1428 | {"native_is_direct_output_supported", "(IIIIIII)Z", |
| 1429 | (void *)android_media_AudioTrack_is_direct_output_supported}, |
| 1430 | {"native_start", "()V", (void *)android_media_AudioTrack_start}, |
| 1431 | {"native_stop", "()V", (void *)android_media_AudioTrack_stop}, |
| 1432 | {"native_pause", "()V", (void *)android_media_AudioTrack_pause}, |
| 1433 | {"native_flush", "()V", (void *)android_media_AudioTrack_flush}, |
jiabin | fcd4cbf | 2020-08-21 18:07:27 -0700 | [diff] [blame] | 1434 | {"native_setup", |
| 1435 | "(Ljava/lang/Object;Ljava/lang/Object;[IIIIII[IJZILjava/lang/Object;Ljava/lang/String;)I", |
Andy Hung | 59dffec | 2020-01-14 17:29:03 -0800 | [diff] [blame] | 1436 | (void *)android_media_AudioTrack_setup}, |
| 1437 | {"native_finalize", "()V", (void *)android_media_AudioTrack_finalize}, |
| 1438 | {"native_release", "()V", (void *)android_media_AudioTrack_release}, |
| 1439 | {"native_write_byte", "([BIIIZ)I", (void *)android_media_AudioTrack_writeArray<jbyteArray>}, |
| 1440 | {"native_write_native_bytes", "(Ljava/nio/ByteBuffer;IIIZ)I", |
| 1441 | (void *)android_media_AudioTrack_write_native_bytes}, |
| 1442 | {"native_write_short", "([SIIIZ)I", |
| 1443 | (void *)android_media_AudioTrack_writeArray<jshortArray>}, |
| 1444 | {"native_write_float", "([FIIIZ)I", |
| 1445 | (void *)android_media_AudioTrack_writeArray<jfloatArray>}, |
| 1446 | {"native_setVolume", "(FF)V", (void *)android_media_AudioTrack_set_volume}, |
| 1447 | {"native_get_buffer_size_frames", "()I", |
| 1448 | (void *)android_media_AudioTrack_get_buffer_size_frames}, |
| 1449 | {"native_set_buffer_size_frames", "(I)I", |
| 1450 | (void *)android_media_AudioTrack_set_buffer_size_frames}, |
| 1451 | {"native_get_buffer_capacity_frames", "()I", |
| 1452 | (void *)android_media_AudioTrack_get_buffer_capacity_frames}, |
| 1453 | {"native_set_playback_rate", "(I)I", (void *)android_media_AudioTrack_set_playback_rate}, |
| 1454 | {"native_get_playback_rate", "()I", (void *)android_media_AudioTrack_get_playback_rate}, |
| 1455 | {"native_set_playback_params", "(Landroid/media/PlaybackParams;)V", |
| 1456 | (void *)android_media_AudioTrack_set_playback_params}, |
| 1457 | {"native_get_playback_params", "()Landroid/media/PlaybackParams;", |
| 1458 | (void *)android_media_AudioTrack_get_playback_params}, |
| 1459 | {"native_set_marker_pos", "(I)I", (void *)android_media_AudioTrack_set_marker_pos}, |
| 1460 | {"native_get_marker_pos", "()I", (void *)android_media_AudioTrack_get_marker_pos}, |
| 1461 | {"native_set_pos_update_period", "(I)I", |
| 1462 | (void *)android_media_AudioTrack_set_pos_update_period}, |
| 1463 | {"native_get_pos_update_period", "()I", |
| 1464 | (void *)android_media_AudioTrack_get_pos_update_period}, |
| 1465 | {"native_set_position", "(I)I", (void *)android_media_AudioTrack_set_position}, |
| 1466 | {"native_get_position", "()I", (void *)android_media_AudioTrack_get_position}, |
| 1467 | {"native_get_latency", "()I", (void *)android_media_AudioTrack_get_latency}, |
| 1468 | {"native_get_underrun_count", "()I", (void *)android_media_AudioTrack_get_underrun_count}, |
| 1469 | {"native_get_flags", "()I", (void *)android_media_AudioTrack_get_flags}, |
| 1470 | {"native_get_timestamp", "([J)I", (void *)android_media_AudioTrack_get_timestamp}, |
| 1471 | {"native_getMetrics", "()Landroid/os/PersistableBundle;", |
| 1472 | (void *)android_media_AudioTrack_native_getMetrics}, |
| 1473 | {"native_set_loop", "(III)I", (void *)android_media_AudioTrack_set_loop}, |
| 1474 | {"native_reload_static", "()I", (void *)android_media_AudioTrack_reload}, |
| 1475 | {"native_get_output_sample_rate", "(I)I", |
| 1476 | (void *)android_media_AudioTrack_get_output_sample_rate}, |
| 1477 | {"native_get_min_buff_size", "(III)I", (void *)android_media_AudioTrack_get_min_buff_size}, |
| 1478 | {"native_setAuxEffectSendLevel", "(F)I", |
| 1479 | (void *)android_media_AudioTrack_setAuxEffectSendLevel}, |
| 1480 | {"native_attachAuxEffect", "(I)I", (void *)android_media_AudioTrack_attachAuxEffect}, |
| 1481 | {"native_setOutputDevice", "(I)Z", (void *)android_media_AudioTrack_setOutputDevice}, |
| 1482 | {"native_getRoutedDeviceId", "()I", (void *)android_media_AudioTrack_getRoutedDeviceId}, |
| 1483 | {"native_enableDeviceCallback", "()V", |
| 1484 | (void *)android_media_AudioTrack_enableDeviceCallback}, |
| 1485 | {"native_disableDeviceCallback", "()V", |
| 1486 | (void *)android_media_AudioTrack_disableDeviceCallback}, |
| 1487 | {"native_applyVolumeShaper", |
| 1488 | "(Landroid/media/VolumeShaper$Configuration;Landroid/media/VolumeShaper$Operation;)I", |
| 1489 | (void *)android_media_AudioTrack_apply_volume_shaper}, |
| 1490 | {"native_getVolumeShaperState", "(I)Landroid/media/VolumeShaper$State;", |
| 1491 | (void *)android_media_AudioTrack_get_volume_shaper_state}, |
| 1492 | {"native_setPresentation", "(II)I", (void *)android_media_AudioTrack_setPresentation}, |
| 1493 | {"native_getPortId", "()I", (void *)android_media_AudioTrack_get_port_id}, |
| 1494 | {"native_set_delay_padding", "(II)V", (void *)android_media_AudioTrack_set_delay_padding}, |
Andy Hung | c33c908 | 2020-01-17 14:05:06 -0800 | [diff] [blame] | 1495 | {"native_set_audio_description_mix_level_db", "(F)I", |
| 1496 | (void *)android_media_AudioTrack_setAudioDescriptionMixLeveldB}, |
| 1497 | {"native_get_audio_description_mix_level_db", "([F)I", |
| 1498 | (void *)android_media_AudioTrack_getAudioDescriptionMixLeveldB}, |
| 1499 | {"native_set_dual_mono_mode", "(I)I", (void *)android_media_AudioTrack_setDualMonoMode}, |
| 1500 | {"native_get_dual_mono_mode", "([I)I", (void *)android_media_AudioTrack_getDualMonoMode}, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1501 | }; |
| 1502 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1503 | // field names found in android/media/AudioTrack.java |
| 1504 | #define JAVA_POSTEVENT_CALLBACK_NAME "postEventFromNative" |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1505 | #define JAVA_NATIVETRACKINJAVAOBJ_FIELD_NAME "mNativeTrackInJavaObj" |
| 1506 | #define JAVA_JNIDATA_FIELD_NAME "mJniData" |
Jean-Michel Trivi | a1d80e3 | 2014-06-18 08:18:41 -0700 | [diff] [blame] | 1507 | #define JAVA_STREAMTYPE_FIELD_NAME "mStreamType" |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1508 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1509 | // ---------------------------------------------------------------------------- |
| 1510 | // preconditions: |
| 1511 | // theClass is valid |
| 1512 | bool android_media_getIntConstantFromClass(JNIEnv* pEnv, jclass theClass, const char* className, |
| 1513 | const char* constName, int* constVal) { |
| 1514 | jfieldID javaConst = NULL; |
| 1515 | javaConst = pEnv->GetStaticFieldID(theClass, constName, "I"); |
| 1516 | if (javaConst != NULL) { |
| 1517 | *constVal = pEnv->GetStaticIntField(theClass, javaConst); |
| 1518 | return true; |
| 1519 | } else { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 1520 | ALOGE("Can't find %s.%s", className, constName); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1521 | return false; |
| 1522 | } |
| 1523 | } |
| 1524 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1525 | // ---------------------------------------------------------------------------- |
| 1526 | int register_android_media_AudioTrack(JNIEnv *env) |
| 1527 | { |
Glenn Kasten | 931fde4 | 2016-01-07 15:59:38 -0800 | [diff] [blame] | 1528 | // must be first |
| 1529 | int res = RegisterMethodsOrDie(env, kClassPathName, gMethods, NELEM(gMethods)); |
| 1530 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1531 | javaAudioTrackFields.nativeTrackInJavaObj = NULL; |
| 1532 | javaAudioTrackFields.postNativeEventInJava = NULL; |
| 1533 | |
| 1534 | // Get the AudioTrack class |
Andreas Gampe | ed6b9df | 2014-11-20 22:02:20 -0800 | [diff] [blame] | 1535 | jclass audioTrackClass = FindClassOrDie(env, kClassPathName); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1536 | |
| 1537 | // Get the postEvent method |
Andreas Gampe | ed6b9df | 2014-11-20 22:02:20 -0800 | [diff] [blame] | 1538 | javaAudioTrackFields.postNativeEventInJava = GetStaticMethodIDOrDie(env, |
| 1539 | audioTrackClass, JAVA_POSTEVENT_CALLBACK_NAME, |
| 1540 | "(Ljava/lang/Object;IIILjava/lang/Object;)V"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1541 | |
| 1542 | // Get the variables fields |
| 1543 | // nativeTrackInJavaObj |
Andreas Gampe | ed6b9df | 2014-11-20 22:02:20 -0800 | [diff] [blame] | 1544 | javaAudioTrackFields.nativeTrackInJavaObj = GetFieldIDOrDie(env, |
| 1545 | audioTrackClass, JAVA_NATIVETRACKINJAVAOBJ_FIELD_NAME, "J"); |
Jean-Michel Trivi | a1d80e3 | 2014-06-18 08:18:41 -0700 | [diff] [blame] | 1546 | // jniData |
Andreas Gampe | ed6b9df | 2014-11-20 22:02:20 -0800 | [diff] [blame] | 1547 | javaAudioTrackFields.jniData = GetFieldIDOrDie(env, |
| 1548 | audioTrackClass, JAVA_JNIDATA_FIELD_NAME, "J"); |
Jean-Michel Trivi | a1d80e3 | 2014-06-18 08:18:41 -0700 | [diff] [blame] | 1549 | // fieldStreamType |
Andreas Gampe | ed6b9df | 2014-11-20 22:02:20 -0800 | [diff] [blame] | 1550 | javaAudioTrackFields.fieldStreamType = GetFieldIDOrDie(env, |
| 1551 | audioTrackClass, JAVA_STREAMTYPE_FIELD_NAME, "I"); |
Jean-Michel Trivi | a1d80e3 | 2014-06-18 08:18:41 -0700 | [diff] [blame] | 1552 | |
Andy Hung | fe48e0d | 2015-04-27 18:14:02 -0700 | [diff] [blame] | 1553 | env->DeleteLocalRef(audioTrackClass); |
| 1554 | |
Wei Jia | 2d61e2b | 2015-05-08 15:23:28 -0700 | [diff] [blame] | 1555 | // initialize PlaybackParams field info |
| 1556 | gPlaybackParamsFields.init(env); |
Andy Hung | fe48e0d | 2015-04-27 18:14:02 -0700 | [diff] [blame] | 1557 | |
Andy Hung | 035d4ec | 2017-01-24 13:45:02 -0800 | [diff] [blame] | 1558 | gVolumeShaperFields.init(env); |
Andy Hung | 59dffec | 2020-01-14 17:29:03 -0800 | [diff] [blame] | 1559 | |
| 1560 | // optional check that the TunerConfiguration class and fields exist. |
| 1561 | TunerConfigurationHelper::initCheckOrDie(env); |
| 1562 | |
Glenn Kasten | 931fde4 | 2016-01-07 15:59:38 -0800 | [diff] [blame] | 1563 | return res; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1564 | } |
| 1565 | |
| 1566 | |
| 1567 | // ---------------------------------------------------------------------------- |