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