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