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 | |
| 20 | #include <stdio.h> |
| 21 | #include <unistd.h> |
| 22 | #include <fcntl.h> |
| 23 | #include <math.h> |
| 24 | |
Glenn Kasten | c81d31c | 2012-03-13 14:46:23 -0700 | [diff] [blame] | 25 | #include <jni.h> |
| 26 | #include <JNIHelp.h> |
| 27 | #include <android_runtime/AndroidRuntime.h> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 28 | |
Glenn Kasten | c81d31c | 2012-03-13 14:46:23 -0700 | [diff] [blame] | 29 | #include <utils/Log.h> |
| 30 | #include <media/AudioSystem.h> |
| 31 | #include <media/AudioTrack.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 | |
Dima Zavin | 24fc2fb | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 36 | #include <cutils/bitops.h> |
| 37 | |
Dima Zavin | 34bb419 | 2011-05-11 14:15:23 -0700 | [diff] [blame] | 38 | #include <system/audio.h> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 39 | |
| 40 | // ---------------------------------------------------------------------------- |
| 41 | |
| 42 | using namespace android; |
| 43 | |
| 44 | // ---------------------------------------------------------------------------- |
| 45 | static const char* const kClassPathName = "android/media/AudioTrack"; |
| 46 | |
| 47 | struct fields_t { |
| 48 | // these fields provide access from C++ to the... |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 49 | jmethodID postNativeEventInJava; //... event post callback method |
| 50 | int PCM16; //... format constants |
| 51 | int PCM8; //... format constants |
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 |
| 54 | }; |
| 55 | static fields_t javaAudioTrackFields; |
| 56 | |
| 57 | struct audiotrack_callback_cookie { |
| 58 | jclass audioTrack_class; |
| 59 | jobject audioTrack_ref; |
Eric Laurent | 532bc1c | 2012-04-20 12:45:03 -0700 | [diff] [blame] | 60 | bool busy; |
| 61 | Condition cond; |
| 62 | }; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 63 | |
Glenn Kasten | 3d301cb | 2012-01-16 14:46:54 -0800 | [diff] [blame] | 64 | // keep these values in sync with AudioTrack.java |
| 65 | #define MODE_STATIC 0 |
| 66 | #define MODE_STREAM 1 |
| 67 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 68 | // ---------------------------------------------------------------------------- |
| 69 | class AudioTrackJniStorage { |
| 70 | public: |
| 71 | sp<MemoryHeapBase> mMemHeap; |
| 72 | sp<MemoryBase> mMemBase; |
| 73 | audiotrack_callback_cookie mCallbackData; |
Glenn Kasten | bc1d77b | 2012-01-12 16:38:12 -0800 | [diff] [blame] | 74 | audio_stream_type_t mStreamType; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 75 | |
| 76 | AudioTrackJniStorage() { |
Jean-Michel Trivi | 8a14968 | 2009-07-15 18:31:11 -0700 | [diff] [blame] | 77 | mCallbackData.audioTrack_class = 0; |
| 78 | mCallbackData.audioTrack_ref = 0; |
Dima Zavin | 24fc2fb | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 79 | mStreamType = AUDIO_STREAM_DEFAULT; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | ~AudioTrackJniStorage() { |
| 83 | mMemBase.clear(); |
| 84 | mMemHeap.clear(); |
| 85 | } |
| 86 | |
| 87 | bool allocSharedMem(int sizeInBytes) { |
| 88 | mMemHeap = new MemoryHeapBase(sizeInBytes, 0, "AudioTrack Heap Base"); |
| 89 | if (mMemHeap->getHeapID() < 0) { |
| 90 | return false; |
| 91 | } |
| 92 | mMemBase = new MemoryBase(mMemHeap, 0, sizeInBytes); |
| 93 | return true; |
| 94 | } |
| 95 | }; |
| 96 | |
Eric Laurent | 532bc1c | 2012-04-20 12:45:03 -0700 | [diff] [blame] | 97 | static Mutex sLock; |
| 98 | static SortedVector <audiotrack_callback_cookie *> sAudioTrackCallBackCookies; |
| 99 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 100 | // ---------------------------------------------------------------------------- |
| 101 | #define DEFAULT_OUTPUT_SAMPLE_RATE 44100 |
| 102 | |
| 103 | #define AUDIOTRACK_SUCCESS 0 |
| 104 | #define AUDIOTRACK_ERROR -1 |
| 105 | #define AUDIOTRACK_ERROR_BAD_VALUE -2 |
| 106 | #define AUDIOTRACK_ERROR_INVALID_OPERATION -3 |
| 107 | #define AUDIOTRACK_ERROR_SETUP_AUDIOSYSTEM -16 |
Glenn Kasten | fb2ab9e | 2011-12-12 09:05:55 -0800 | [diff] [blame] | 108 | #define AUDIOTRACK_ERROR_SETUP_INVALIDCHANNELMASK -17 |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 109 | #define AUDIOTRACK_ERROR_SETUP_INVALIDFORMAT -18 |
| 110 | #define AUDIOTRACK_ERROR_SETUP_INVALIDSTREAMTYPE -19 |
| 111 | #define AUDIOTRACK_ERROR_SETUP_NATIVEINITFAILED -20 |
| 112 | |
| 113 | |
| 114 | jint android_media_translateErrorCode(int code) { |
Glenn Kasten | 18db49a | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 115 | switch (code) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 116 | case NO_ERROR: |
| 117 | return AUDIOTRACK_SUCCESS; |
| 118 | case BAD_VALUE: |
| 119 | return AUDIOTRACK_ERROR_BAD_VALUE; |
| 120 | case INVALID_OPERATION: |
| 121 | return AUDIOTRACK_ERROR_INVALID_OPERATION; |
| 122 | default: |
| 123 | return AUDIOTRACK_ERROR; |
Glenn Kasten | 18db49a | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 124 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | |
| 128 | // ---------------------------------------------------------------------------- |
Glenn Kasten | e46a86f | 2011-06-01 15:20:35 -0700 | [diff] [blame] | 129 | static void audioCallback(int event, void* user, void *info) { |
Eric Laurent | 532bc1c | 2012-04-20 12:45:03 -0700 | [diff] [blame] | 130 | |
| 131 | audiotrack_callback_cookie *callbackInfo = (audiotrack_callback_cookie *)user; |
| 132 | { |
| 133 | Mutex::Autolock l(sLock); |
| 134 | if (sAudioTrackCallBackCookies.indexOf(callbackInfo) < 0) { |
| 135 | return; |
| 136 | } |
| 137 | callbackInfo->busy = true; |
| 138 | } |
| 139 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 140 | if (event == AudioTrack::EVENT_MORE_DATA) { |
| 141 | // set size to 0 to signal we're not using the callback to write more data |
| 142 | AudioTrack::Buffer* pBuff = (AudioTrack::Buffer*)info; |
Glenn Kasten | 18db49a | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 143 | pBuff->size = 0; |
| 144 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 145 | } else if (event == AudioTrack::EVENT_MARKER) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 146 | JNIEnv *env = AndroidRuntime::getJNIEnv(); |
| 147 | if (user && env) { |
| 148 | env->CallStaticVoidMethod( |
Glenn Kasten | 18db49a | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 149 | callbackInfo->audioTrack_class, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 150 | javaAudioTrackFields.postNativeEventInJava, |
| 151 | callbackInfo->audioTrack_ref, event, 0,0, NULL); |
| 152 | if (env->ExceptionCheck()) { |
| 153 | env->ExceptionDescribe(); |
| 154 | env->ExceptionClear(); |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | } else if (event == AudioTrack::EVENT_NEW_POS) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 159 | JNIEnv *env = AndroidRuntime::getJNIEnv(); |
| 160 | if (user && env) { |
| 161 | env->CallStaticVoidMethod( |
Glenn Kasten | 18db49a | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 162 | callbackInfo->audioTrack_class, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 163 | javaAudioTrackFields.postNativeEventInJava, |
| 164 | callbackInfo->audioTrack_ref, event, 0,0, NULL); |
| 165 | if (env->ExceptionCheck()) { |
| 166 | env->ExceptionDescribe(); |
| 167 | env->ExceptionClear(); |
| 168 | } |
| 169 | } |
| 170 | } |
Eric Laurent | 532bc1c | 2012-04-20 12:45:03 -0700 | [diff] [blame] | 171 | { |
| 172 | Mutex::Autolock l(sLock); |
| 173 | callbackInfo->busy = false; |
| 174 | callbackInfo->cond.broadcast(); |
| 175 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 176 | } |
| 177 | |
| 178 | |
| 179 | // ---------------------------------------------------------------------------- |
Eric Laurent | 532bc1c | 2012-04-20 12:45:03 -0700 | [diff] [blame] | 180 | static sp<AudioTrack> getAudioTrack(JNIEnv* env, jobject thiz) |
| 181 | { |
| 182 | Mutex::Autolock l(sLock); |
| 183 | AudioTrack* const at = |
| 184 | (AudioTrack*)env->GetIntField(thiz, javaAudioTrackFields.nativeTrackInJavaObj); |
| 185 | return sp<AudioTrack>(at); |
| 186 | } |
| 187 | |
| 188 | static sp<AudioTrack> setAudioTrack(JNIEnv* env, jobject thiz, const sp<AudioTrack>& at) |
| 189 | { |
| 190 | Mutex::Autolock l(sLock); |
| 191 | sp<AudioTrack> old = |
| 192 | (AudioTrack*)env->GetIntField(thiz, javaAudioTrackFields.nativeTrackInJavaObj); |
| 193 | if (at.get()) { |
Mathias Agopian | b1d90c8 | 2013-03-06 17:45:42 -0800 | [diff] [blame] | 194 | at->incStrong((void*)setAudioTrack); |
Eric Laurent | 532bc1c | 2012-04-20 12:45:03 -0700 | [diff] [blame] | 195 | } |
| 196 | if (old != 0) { |
Mathias Agopian | b1d90c8 | 2013-03-06 17:45:42 -0800 | [diff] [blame] | 197 | old->decStrong((void*)setAudioTrack); |
Eric Laurent | 532bc1c | 2012-04-20 12:45:03 -0700 | [diff] [blame] | 198 | } |
| 199 | env->SetIntField(thiz, javaAudioTrackFields.nativeTrackInJavaObj, (int)at.get()); |
| 200 | return old; |
| 201 | } |
| 202 | |
| 203 | // ---------------------------------------------------------------------------- |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 204 | static int |
| 205 | android_media_AudioTrack_native_setup(JNIEnv *env, jobject thiz, jobject weak_this, |
Jean-Michel Trivi | d9ae1c5 | 2011-07-25 12:58:14 -0700 | [diff] [blame] | 206 | jint streamType, jint sampleRateInHertz, jint javaChannelMask, |
Eric Laurent | 619346f | 2010-06-21 09:27:30 -0700 | [diff] [blame] | 207 | jint audioFormat, jint buffSizeInBytes, jint memoryMode, jintArray jSession) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 208 | { |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 209 | ALOGV("sampleRate=%d, audioFormat(from Java)=%d, channel mask=%x, buffSize=%d", |
Jean-Michel Trivi | d9ae1c5 | 2011-07-25 12:58:14 -0700 | [diff] [blame] | 210 | sampleRateInHertz, audioFormat, javaChannelMask, buffSizeInBytes); |
Glenn Kasten | 85fbc87 | 2012-11-14 13:21:09 -0800 | [diff] [blame] | 211 | uint32_t afSampleRate; |
Glenn Kasten | fd1e3df | 2012-11-13 15:21:06 -0800 | [diff] [blame] | 212 | size_t afFrameCount; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 213 | |
Glenn Kasten | bc1d77b | 2012-01-12 16:38:12 -0800 | [diff] [blame] | 214 | if (AudioSystem::getOutputFrameCount(&afFrameCount, (audio_stream_type_t) streamType) != NO_ERROR) { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 215 | ALOGE("Error creating AudioTrack: Could not get AudioSystem frame count."); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 216 | return AUDIOTRACK_ERROR_SETUP_AUDIOSYSTEM; |
| 217 | } |
Glenn Kasten | bc1d77b | 2012-01-12 16:38:12 -0800 | [diff] [blame] | 218 | if (AudioSystem::getOutputSamplingRate(&afSampleRate, (audio_stream_type_t) streamType) != NO_ERROR) { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 219 | ALOGE("Error creating AudioTrack: Could not get AudioSystem sampling rate."); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 220 | return AUDIOTRACK_ERROR_SETUP_AUDIOSYSTEM; |
| 221 | } |
| 222 | |
Jean-Michel Trivi | d9ae1c5 | 2011-07-25 12:58:14 -0700 | [diff] [blame] | 223 | // Java channel masks don't map directly to the native definition, but it's a simple shift |
| 224 | // to skip the two deprecated channel configurations "default" and "mono". |
| 225 | uint32_t nativeChannelMask = ((uint32_t)javaChannelMask) >> 2; |
| 226 | |
| 227 | if (!audio_is_output_channel(nativeChannelMask)) { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 228 | ALOGE("Error creating AudioTrack: invalid channel mask."); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 229 | return AUDIOTRACK_ERROR_SETUP_INVALIDCHANNELMASK; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 230 | } |
Jean-Michel Trivi | d9ae1c5 | 2011-07-25 12:58:14 -0700 | [diff] [blame] | 231 | |
| 232 | int nbChannels = popcount(nativeChannelMask); |
Glenn Kasten | 18db49a | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 233 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 234 | // check the stream type |
Dima Zavin | 24fc2fb | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 235 | audio_stream_type_t atStreamType; |
Glenn Kasten | 29a0909 | 2012-01-16 14:37:12 -0800 | [diff] [blame] | 236 | switch (streamType) { |
| 237 | case AUDIO_STREAM_VOICE_CALL: |
| 238 | case AUDIO_STREAM_SYSTEM: |
| 239 | case AUDIO_STREAM_RING: |
| 240 | case AUDIO_STREAM_MUSIC: |
| 241 | case AUDIO_STREAM_ALARM: |
| 242 | case AUDIO_STREAM_NOTIFICATION: |
| 243 | case AUDIO_STREAM_BLUETOOTH_SCO: |
| 244 | case AUDIO_STREAM_DTMF: |
| 245 | atStreamType = (audio_stream_type_t) streamType; |
| 246 | break; |
| 247 | default: |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 248 | ALOGE("Error creating AudioTrack: unknown stream type."); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 249 | return AUDIOTRACK_ERROR_SETUP_INVALIDSTREAMTYPE; |
| 250 | } |
| 251 | |
| 252 | // check the format. |
| 253 | // This function was called from Java, so we compare the format against the Java constants |
| 254 | if ((audioFormat != javaAudioTrackFields.PCM16) && (audioFormat != javaAudioTrackFields.PCM8)) { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 255 | ALOGE("Error creating AudioTrack: unsupported audio format."); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 256 | return AUDIOTRACK_ERROR_SETUP_INVALIDFORMAT; |
| 257 | } |
| 258 | |
| 259 | // for the moment 8bitPCM in MODE_STATIC is not supported natively in the AudioTrack C++ class |
| 260 | // so we declare everything as 16bitPCM, the 8->16bit conversion for MODE_STATIC will be handled |
Glenn Kasten | 00db1f5 | 2012-01-16 14:41:30 -0800 | [diff] [blame] | 261 | // in android_media_AudioTrack_native_write_byte() |
Glenn Kasten | 18db49a | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 262 | if ((audioFormat == javaAudioTrackFields.PCM8) |
Glenn Kasten | 3d301cb | 2012-01-16 14:46:54 -0800 | [diff] [blame] | 263 | && (memoryMode == MODE_STATIC)) { |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 264 | ALOGV("android_media_AudioTrack_native_setup(): requesting MODE_STATIC for 8bit \ |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 265 | buff size of %dbytes, switching to 16bit, buff size of %dbytes", |
| 266 | buffSizeInBytes, 2*buffSizeInBytes); |
| 267 | audioFormat = javaAudioTrackFields.PCM16; |
| 268 | // we will need twice the memory to store the data |
| 269 | buffSizeInBytes *= 2; |
| 270 | } |
| 271 | |
| 272 | // compute the frame count |
| 273 | int bytesPerSample = audioFormat == javaAudioTrackFields.PCM16 ? 2 : 1; |
Glenn Kasten | 1c5a89d | 2012-01-04 09:36:37 -0800 | [diff] [blame] | 274 | audio_format_t format = audioFormat == javaAudioTrackFields.PCM16 ? |
Dima Zavin | 24fc2fb | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 275 | AUDIO_FORMAT_PCM_16_BIT : AUDIO_FORMAT_PCM_8_BIT; |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 276 | int frameCount = buffSizeInBytes / (nbChannels * bytesPerSample); |
Glenn Kasten | 18db49a | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 277 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 278 | jclass clazz = env->GetObjectClass(thiz); |
| 279 | if (clazz == NULL) { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 280 | ALOGE("Can't find %s when setting up callback.", kClassPathName); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 281 | return AUDIOTRACK_ERROR_SETUP_NATIVEINITFAILED; |
| 282 | } |
Eric Laurent | 2fb43ef | 2010-09-24 12:03:36 -0700 | [diff] [blame] | 283 | |
| 284 | if (jSession == NULL) { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 285 | ALOGE("Error creating AudioTrack: invalid session ID pointer"); |
Eric Laurent | 619346f | 2010-06-21 09:27:30 -0700 | [diff] [blame] | 286 | return AUDIOTRACK_ERROR; |
| 287 | } |
| 288 | |
Eric Laurent | 2fb43ef | 2010-09-24 12:03:36 -0700 | [diff] [blame] | 289 | jint* nSession = (jint *) env->GetPrimitiveArrayCritical(jSession, NULL); |
| 290 | if (nSession == NULL) { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 291 | ALOGE("Error creating AudioTrack: Error retrieving session id pointer"); |
Eric Laurent | 2fb43ef | 2010-09-24 12:03:36 -0700 | [diff] [blame] | 292 | return AUDIOTRACK_ERROR; |
| 293 | } |
| 294 | int sessionId = nSession[0]; |
| 295 | env->ReleasePrimitiveArrayCritical(jSession, nSession, 0); |
| 296 | nSession = NULL; |
| 297 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 298 | // create the native AudioTrack object |
Eric Laurent | 532bc1c | 2012-04-20 12:45:03 -0700 | [diff] [blame] | 299 | sp<AudioTrack> lpTrack = new AudioTrack(); |
Glenn Kasten | 18db49a | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 300 | |
Eric Laurent | 532bc1c | 2012-04-20 12:45:03 -0700 | [diff] [blame] | 301 | // initialize the callback information: |
| 302 | // this data will be passed with every AudioTrack callback |
| 303 | AudioTrackJniStorage* lpJniStorage = new AudioTrackJniStorage(); |
| 304 | lpJniStorage->mStreamType = atStreamType; |
| 305 | lpJniStorage->mCallbackData.audioTrack_class = (jclass)env->NewGlobalRef(clazz); |
| 306 | // we use a weak reference so the AudioTrack object can be garbage collected. |
| 307 | lpJniStorage->mCallbackData.audioTrack_ref = env->NewGlobalRef(weak_this); |
| 308 | lpJniStorage->mCallbackData.busy = false; |
| 309 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 310 | // initialize the native AudioTrack object |
Glenn Kasten | 3d301cb | 2012-01-16 14:46:54 -0800 | [diff] [blame] | 311 | switch (memoryMode) { |
| 312 | case MODE_STREAM: |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 313 | |
| 314 | lpTrack->set( |
| 315 | atStreamType,// stream type |
| 316 | sampleRateInHertz, |
| 317 | format,// word length, PCM |
Jean-Michel Trivi | d9ae1c5 | 2011-07-25 12:58:14 -0700 | [diff] [blame] | 318 | nativeChannelMask, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 319 | frameCount, |
Eric Laurent | 10536b6 | 2012-04-18 09:27:14 -0700 | [diff] [blame] | 320 | AUDIO_OUTPUT_FLAG_NONE, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 321 | audioCallback, &(lpJniStorage->mCallbackData),//callback, callback data (user) |
| 322 | 0,// notificationFrames == 0 since not using EVENT_MORE_DATA to feed the AudioTrack |
| 323 | 0,// shared mem |
Eric Laurent | 619346f | 2010-06-21 09:27:30 -0700 | [diff] [blame] | 324 | true,// thread can call Java |
Eric Laurent | 2fb43ef | 2010-09-24 12:03:36 -0700 | [diff] [blame] | 325 | sessionId);// audio session ID |
Glenn Kasten | 3d301cb | 2012-01-16 14:46:54 -0800 | [diff] [blame] | 326 | break; |
Glenn Kasten | 18db49a | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 327 | |
Glenn Kasten | 3d301cb | 2012-01-16 14:46:54 -0800 | [diff] [blame] | 328 | case MODE_STATIC: |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 329 | // AudioTrack is using shared memory |
Glenn Kasten | 18db49a | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 330 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 331 | if (!lpJniStorage->allocSharedMem(buffSizeInBytes)) { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 332 | ALOGE("Error creating AudioTrack in static mode: error creating mem heap base"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 333 | goto native_init_failure; |
| 334 | } |
Glenn Kasten | 18db49a | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 335 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 336 | lpTrack->set( |
| 337 | atStreamType,// stream type |
| 338 | sampleRateInHertz, |
| 339 | format,// word length, PCM |
Jean-Michel Trivi | d9ae1c5 | 2011-07-25 12:58:14 -0700 | [diff] [blame] | 340 | nativeChannelMask, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 341 | frameCount, |
Eric Laurent | 10536b6 | 2012-04-18 09:27:14 -0700 | [diff] [blame] | 342 | AUDIO_OUTPUT_FLAG_NONE, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 343 | audioCallback, &(lpJniStorage->mCallbackData),//callback, callback data (user)); |
Glenn Kasten | 18db49a | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 344 | 0,// notificationFrames == 0 since not using EVENT_MORE_DATA to feed the AudioTrack |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 345 | lpJniStorage->mMemBase,// shared mem |
Eric Laurent | 619346f | 2010-06-21 09:27:30 -0700 | [diff] [blame] | 346 | true,// thread can call Java |
Eric Laurent | 2fb43ef | 2010-09-24 12:03:36 -0700 | [diff] [blame] | 347 | sessionId);// audio session ID |
Glenn Kasten | 3d301cb | 2012-01-16 14:46:54 -0800 | [diff] [blame] | 348 | break; |
| 349 | |
| 350 | default: |
| 351 | ALOGE("Unknown mode %d", memoryMode); |
| 352 | goto native_init_failure; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 353 | } |
| 354 | |
| 355 | if (lpTrack->initCheck() != NO_ERROR) { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 356 | ALOGE("Error initializing AudioTrack"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 357 | goto native_init_failure; |
| 358 | } |
| 359 | |
Eric Laurent | 2fb43ef | 2010-09-24 12:03:36 -0700 | [diff] [blame] | 360 | nSession = (jint *) env->GetPrimitiveArrayCritical(jSession, NULL); |
| 361 | if (nSession == NULL) { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 362 | ALOGE("Error creating AudioTrack: Error retrieving session id pointer"); |
Eric Laurent | 2fb43ef | 2010-09-24 12:03:36 -0700 | [diff] [blame] | 363 | goto native_init_failure; |
| 364 | } |
Eric Laurent | 619346f | 2010-06-21 09:27:30 -0700 | [diff] [blame] | 365 | // read the audio session ID back from AudioTrack in case we create a new session |
| 366 | nSession[0] = lpTrack->getSessionId(); |
Eric Laurent | 619346f | 2010-06-21 09:27:30 -0700 | [diff] [blame] | 367 | env->ReleasePrimitiveArrayCritical(jSession, nSession, 0); |
| 368 | nSession = NULL; |
| 369 | |
Eric Laurent | 532bc1c | 2012-04-20 12:45:03 -0700 | [diff] [blame] | 370 | { // scope for the lock |
| 371 | Mutex::Autolock l(sLock); |
| 372 | sAudioTrackCallBackCookies.add(&lpJniStorage->mCallbackData); |
| 373 | } |
Glenn Kasten | 18db49a | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 374 | // 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] | 375 | // of the Java object (in mNativeTrackInJavaObj) |
Eric Laurent | 532bc1c | 2012-04-20 12:45:03 -0700 | [diff] [blame] | 376 | setAudioTrack(env, thiz, lpTrack); |
Glenn Kasten | 18db49a | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 377 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 378 | // save the JNI resources so we can free them later |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 379 | //ALOGV("storing lpJniStorage: %x\n", (int)lpJniStorage); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 380 | env->SetIntField(thiz, javaAudioTrackFields.jniData, (int)lpJniStorage); |
| 381 | |
| 382 | return AUDIOTRACK_SUCCESS; |
Glenn Kasten | 18db49a | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 383 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 384 | // failures: |
| 385 | native_init_failure: |
Eric Laurent | 619346f | 2010-06-21 09:27:30 -0700 | [diff] [blame] | 386 | if (nSession != NULL) { |
| 387 | env->ReleasePrimitiveArrayCritical(jSession, nSession, 0); |
| 388 | } |
Jean-Michel Trivi | 8a14968 | 2009-07-15 18:31:11 -0700 | [diff] [blame] | 389 | env->DeleteGlobalRef(lpJniStorage->mCallbackData.audioTrack_class); |
| 390 | env->DeleteGlobalRef(lpJniStorage->mCallbackData.audioTrack_ref); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 391 | delete lpJniStorage; |
| 392 | env->SetIntField(thiz, javaAudioTrackFields.jniData, 0); |
Glenn Kasten | 18db49a | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 393 | |
Eric Laurent | 532bc1c | 2012-04-20 12:45:03 -0700 | [diff] [blame] | 394 | return AUDIOTRACK_ERROR_SETUP_NATIVEINITFAILED; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 395 | } |
| 396 | |
| 397 | |
| 398 | // ---------------------------------------------------------------------------- |
| 399 | static void |
| 400 | android_media_AudioTrack_start(JNIEnv *env, jobject thiz) |
| 401 | { |
Eric Laurent | 532bc1c | 2012-04-20 12:45:03 -0700 | [diff] [blame] | 402 | sp<AudioTrack> lpTrack = getAudioTrack(env, thiz); |
| 403 | if (lpTrack == NULL) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 404 | jniThrowException(env, "java/lang/IllegalStateException", |
| 405 | "Unable to retrieve AudioTrack pointer for start()"); |
Eric Laurent | 7070b36 | 2010-07-16 07:43:46 -0700 | [diff] [blame] | 406 | return; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 407 | } |
Jean-Michel Trivi | 21dc037 | 2009-05-08 16:06:34 -0700 | [diff] [blame] | 408 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 409 | lpTrack->start(); |
| 410 | } |
| 411 | |
| 412 | |
| 413 | // ---------------------------------------------------------------------------- |
| 414 | static void |
| 415 | android_media_AudioTrack_stop(JNIEnv *env, jobject thiz) |
| 416 | { |
Eric Laurent | 532bc1c | 2012-04-20 12:45:03 -0700 | [diff] [blame] | 417 | sp<AudioTrack> lpTrack = getAudioTrack(env, thiz); |
| 418 | if (lpTrack == NULL) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 419 | jniThrowException(env, "java/lang/IllegalStateException", |
| 420 | "Unable to retrieve AudioTrack pointer for stop()"); |
Eric Laurent | 7070b36 | 2010-07-16 07:43:46 -0700 | [diff] [blame] | 421 | return; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 422 | } |
| 423 | |
| 424 | lpTrack->stop(); |
| 425 | } |
| 426 | |
| 427 | |
| 428 | // ---------------------------------------------------------------------------- |
| 429 | static void |
| 430 | android_media_AudioTrack_pause(JNIEnv *env, jobject thiz) |
| 431 | { |
Eric Laurent | 532bc1c | 2012-04-20 12:45:03 -0700 | [diff] [blame] | 432 | sp<AudioTrack> lpTrack = getAudioTrack(env, thiz); |
| 433 | if (lpTrack == NULL) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 434 | jniThrowException(env, "java/lang/IllegalStateException", |
| 435 | "Unable to retrieve AudioTrack pointer for pause()"); |
Eric Laurent | 7070b36 | 2010-07-16 07:43:46 -0700 | [diff] [blame] | 436 | return; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 437 | } |
| 438 | |
| 439 | lpTrack->pause(); |
| 440 | } |
| 441 | |
| 442 | |
| 443 | // ---------------------------------------------------------------------------- |
| 444 | static void |
| 445 | android_media_AudioTrack_flush(JNIEnv *env, jobject thiz) |
| 446 | { |
Eric Laurent | 532bc1c | 2012-04-20 12:45:03 -0700 | [diff] [blame] | 447 | sp<AudioTrack> lpTrack = getAudioTrack(env, thiz); |
| 448 | if (lpTrack == NULL) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 449 | jniThrowException(env, "java/lang/IllegalStateException", |
| 450 | "Unable to retrieve AudioTrack pointer for flush()"); |
Eric Laurent | 7070b36 | 2010-07-16 07:43:46 -0700 | [diff] [blame] | 451 | return; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 452 | } |
| 453 | |
| 454 | lpTrack->flush(); |
| 455 | } |
| 456 | |
| 457 | // ---------------------------------------------------------------------------- |
| 458 | static void |
| 459 | android_media_AudioTrack_set_volume(JNIEnv *env, jobject thiz, jfloat leftVol, jfloat rightVol ) |
| 460 | { |
Eric Laurent | 532bc1c | 2012-04-20 12:45:03 -0700 | [diff] [blame] | 461 | sp<AudioTrack> lpTrack = getAudioTrack(env, thiz); |
| 462 | if (lpTrack == NULL) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 463 | jniThrowException(env, "java/lang/IllegalStateException", |
| 464 | "Unable to retrieve AudioTrack pointer for setVolume()"); |
Eric Laurent | 7070b36 | 2010-07-16 07:43:46 -0700 | [diff] [blame] | 465 | return; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 466 | } |
| 467 | |
| 468 | lpTrack->setVolume(leftVol, rightVol); |
| 469 | } |
| 470 | |
| 471 | // ---------------------------------------------------------------------------- |
Glenn Kasten | 18db49a | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 472 | |
Eric Laurent | 532bc1c | 2012-04-20 12:45:03 -0700 | [diff] [blame] | 473 | #define CALLBACK_COND_WAIT_TIMEOUT_MS 1000 |
| 474 | static void android_media_AudioTrack_native_release(JNIEnv *env, jobject thiz) { |
| 475 | sp<AudioTrack> lpTrack = setAudioTrack(env, thiz, 0); |
| 476 | if (lpTrack == NULL) { |
| 477 | return; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 478 | } |
Eric Laurent | 532bc1c | 2012-04-20 12:45:03 -0700 | [diff] [blame] | 479 | //ALOGV("deleting lpTrack: %x\n", (int)lpTrack); |
| 480 | lpTrack->stop(); |
Glenn Kasten | 18db49a | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 481 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 482 | // delete the JNI data |
| 483 | AudioTrackJniStorage* pJniStorage = (AudioTrackJniStorage *)env->GetIntField( |
| 484 | thiz, javaAudioTrackFields.jniData); |
Eric Laurent | 532bc1c | 2012-04-20 12:45:03 -0700 | [diff] [blame] | 485 | // reset the native resources in the Java object so any attempt to access |
| 486 | // them after a call to release fails. |
| 487 | env->SetIntField(thiz, javaAudioTrackFields.jniData, 0); |
| 488 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 489 | if (pJniStorage) { |
Eric Laurent | 532bc1c | 2012-04-20 12:45:03 -0700 | [diff] [blame] | 490 | Mutex::Autolock l(sLock); |
| 491 | audiotrack_callback_cookie *lpCookie = &pJniStorage->mCallbackData; |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 492 | //ALOGV("deleting pJniStorage: %x\n", (int)pJniStorage); |
Eric Laurent | 532bc1c | 2012-04-20 12:45:03 -0700 | [diff] [blame] | 493 | while (lpCookie->busy) { |
| 494 | if (lpCookie->cond.waitRelative(sLock, |
| 495 | milliseconds(CALLBACK_COND_WAIT_TIMEOUT_MS)) != |
| 496 | NO_ERROR) { |
| 497 | break; |
| 498 | } |
| 499 | } |
| 500 | sAudioTrackCallBackCookies.remove(lpCookie); |
| 501 | // delete global refs created in native_setup |
| 502 | env->DeleteGlobalRef(lpCookie->audioTrack_class); |
| 503 | env->DeleteGlobalRef(lpCookie->audioTrack_ref); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 504 | delete pJniStorage; |
| 505 | } |
| 506 | } |
| 507 | |
Glenn Kasten | 18db49a | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 508 | |
Eric Laurent | 532bc1c | 2012-04-20 12:45:03 -0700 | [diff] [blame] | 509 | // ---------------------------------------------------------------------------- |
| 510 | static void android_media_AudioTrack_native_finalize(JNIEnv *env, jobject thiz) { |
| 511 | //ALOGV("android_media_AudioTrack_native_finalize jobject: %x\n", (int)thiz); |
| 512 | android_media_AudioTrack_native_release(env, thiz); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 513 | } |
| 514 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 515 | // ---------------------------------------------------------------------------- |
Eric Laurent | 532bc1c | 2012-04-20 12:45:03 -0700 | [diff] [blame] | 516 | jint writeToTrack(const sp<AudioTrack>& track, jint audioFormat, jbyte* data, |
Jean-Michel Trivi | 21dc037 | 2009-05-08 16:06:34 -0700 | [diff] [blame] | 517 | jint offsetInBytes, jint sizeInBytes) { |
| 518 | // give the data to the native AudioTrack object (the data starts at the offset) |
| 519 | ssize_t written = 0; |
| 520 | // regular write() or copy the data to the AudioTrack's shared memory? |
Eric Laurent | 532bc1c | 2012-04-20 12:45:03 -0700 | [diff] [blame] | 521 | if (track->sharedBuffer() == 0) { |
| 522 | written = track->write(data + offsetInBytes, sizeInBytes); |
Glenn Kasten | 9b53db3 | 2013-07-10 14:13:39 -0700 | [diff] [blame] | 523 | // for compatibility with earlier behavior of write(), return 0 in this case |
| 524 | if (written == (ssize_t) WOULD_BLOCK) { |
| 525 | written = 0; |
| 526 | } |
Jean-Michel Trivi | 21dc037 | 2009-05-08 16:06:34 -0700 | [diff] [blame] | 527 | } else { |
| 528 | if (audioFormat == javaAudioTrackFields.PCM16) { |
| 529 | // writing to shared memory, check for capacity |
Eric Laurent | 532bc1c | 2012-04-20 12:45:03 -0700 | [diff] [blame] | 530 | if ((size_t)sizeInBytes > track->sharedBuffer()->size()) { |
| 531 | sizeInBytes = track->sharedBuffer()->size(); |
Jean-Michel Trivi | 21dc037 | 2009-05-08 16:06:34 -0700 | [diff] [blame] | 532 | } |
Eric Laurent | 532bc1c | 2012-04-20 12:45:03 -0700 | [diff] [blame] | 533 | memcpy(track->sharedBuffer()->pointer(), data + offsetInBytes, sizeInBytes); |
Jean-Michel Trivi | 21dc037 | 2009-05-08 16:06:34 -0700 | [diff] [blame] | 534 | written = sizeInBytes; |
| 535 | } else if (audioFormat == javaAudioTrackFields.PCM8) { |
| 536 | // data contains 8bit data we need to expand to 16bit before copying |
| 537 | // to the shared memory |
| 538 | // writing to shared memory, check for capacity, |
| 539 | // note that input data will occupy 2X the input space due to 8 to 16bit conversion |
Eric Laurent | 532bc1c | 2012-04-20 12:45:03 -0700 | [diff] [blame] | 540 | if (((size_t)sizeInBytes)*2 > track->sharedBuffer()->size()) { |
| 541 | sizeInBytes = track->sharedBuffer()->size() / 2; |
Jean-Michel Trivi | 21dc037 | 2009-05-08 16:06:34 -0700 | [diff] [blame] | 542 | } |
| 543 | int count = sizeInBytes; |
Eric Laurent | 532bc1c | 2012-04-20 12:45:03 -0700 | [diff] [blame] | 544 | int16_t *dst = (int16_t *)track->sharedBuffer()->pointer(); |
Jean-Michel Trivi | 21dc037 | 2009-05-08 16:06:34 -0700 | [diff] [blame] | 545 | const int8_t *src = (const int8_t *)(data + offsetInBytes); |
Glenn Kasten | 18db49a | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 546 | while (count--) { |
Jean-Michel Trivi | 21dc037 | 2009-05-08 16:06:34 -0700 | [diff] [blame] | 547 | *dst++ = (int16_t)(*src++^0x80) << 8; |
| 548 | } |
| 549 | // even though we wrote 2*sizeInBytes, we only report sizeInBytes as written to hide |
| 550 | // the 8bit mixer restriction from the user of this function |
| 551 | written = sizeInBytes; |
| 552 | } |
| 553 | } |
| 554 | return written; |
| 555 | |
| 556 | } |
| 557 | |
| 558 | // ---------------------------------------------------------------------------- |
Glenn Kasten | 00db1f5 | 2012-01-16 14:41:30 -0800 | [diff] [blame] | 559 | static jint android_media_AudioTrack_native_write_byte(JNIEnv *env, jobject thiz, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 560 | jbyteArray javaAudioData, |
| 561 | jint offsetInBytes, jint sizeInBytes, |
| 562 | jint javaAudioFormat) { |
Glenn Kasten | 00db1f5 | 2012-01-16 14:41:30 -0800 | [diff] [blame] | 563 | //ALOGV("android_media_AudioTrack_native_write_byte(offset=%d, sizeInBytes=%d) called", |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 564 | // offsetInBytes, sizeInBytes); |
Eric Laurent | 532bc1c | 2012-04-20 12:45:03 -0700 | [diff] [blame] | 565 | sp<AudioTrack> lpTrack = getAudioTrack(env, thiz); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 566 | if (lpTrack == NULL) { |
| 567 | jniThrowException(env, "java/lang/IllegalStateException", |
| 568 | "Unable to retrieve AudioTrack pointer for write()"); |
Eric Laurent | 7070b36 | 2010-07-16 07:43:46 -0700 | [diff] [blame] | 569 | return 0; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 570 | } |
| 571 | |
| 572 | // get the pointer for the audio data from the java array |
Eric Laurent | 421ddc0 | 2011-03-07 14:52:59 -0800 | [diff] [blame] | 573 | // NOTE: We may use GetPrimitiveArrayCritical() when the JNI implementation changes in such |
| 574 | // a way that it becomes much more efficient. When doing so, we will have to prevent the |
| 575 | // AudioSystem callback to be called while in critical section (in case of media server |
| 576 | // process crash for instance) |
Eric Laurent | 532bc1c | 2012-04-20 12:45:03 -0700 | [diff] [blame] | 577 | jbyte* cAudioData = NULL; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 578 | if (javaAudioData) { |
Eric Laurent | 421ddc0 | 2011-03-07 14:52:59 -0800 | [diff] [blame] | 579 | cAudioData = (jbyte *)env->GetByteArrayElements(javaAudioData, NULL); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 580 | if (cAudioData == NULL) { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 581 | ALOGE("Error retrieving source of audio data to play, can't play"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 582 | return 0; // out of memory or no data to load |
| 583 | } |
| 584 | } else { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 585 | ALOGE("NULL java array of audio data to play, can't play"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 586 | return 0; |
| 587 | } |
| 588 | |
Jean-Michel Trivi | 21dc037 | 2009-05-08 16:06:34 -0700 | [diff] [blame] | 589 | jint written = writeToTrack(lpTrack, javaAudioFormat, cAudioData, offsetInBytes, sizeInBytes); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 590 | |
Eric Laurent | 421ddc0 | 2011-03-07 14:52:59 -0800 | [diff] [blame] | 591 | env->ReleaseByteArrayElements(javaAudioData, cAudioData, 0); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 592 | |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 593 | //ALOGV("write wrote %d (tried %d) bytes in the native AudioTrack with offset %d", |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 594 | // (int)written, (int)(sizeInBytes), (int)offsetInBytes); |
Jean-Michel Trivi | 21dc037 | 2009-05-08 16:06:34 -0700 | [diff] [blame] | 595 | return written; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 596 | } |
| 597 | |
| 598 | |
| 599 | // ---------------------------------------------------------------------------- |
| 600 | static jint android_media_AudioTrack_native_write_short(JNIEnv *env, jobject thiz, |
| 601 | jshortArray javaAudioData, |
| 602 | jint offsetInShorts, jint sizeInShorts, |
| 603 | jint javaAudioFormat) { |
Glenn Kasten | f7e0a370 | 2013-07-10 14:03:03 -0700 | [diff] [blame] | 604 | jint written = android_media_AudioTrack_native_write_byte(env, thiz, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 605 | (jbyteArray) javaAudioData, |
| 606 | offsetInShorts*2, sizeInShorts*2, |
Glenn Kasten | f7e0a370 | 2013-07-10 14:03:03 -0700 | [diff] [blame] | 607 | javaAudioFormat); |
| 608 | if (written > 0) { |
| 609 | written /= 2; |
| 610 | } |
| 611 | return written; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 612 | } |
| 613 | |
| 614 | |
| 615 | // ---------------------------------------------------------------------------- |
| 616 | static jint android_media_AudioTrack_get_native_frame_count(JNIEnv *env, jobject thiz) { |
Eric Laurent | 532bc1c | 2012-04-20 12:45:03 -0700 | [diff] [blame] | 617 | sp<AudioTrack> lpTrack = getAudioTrack(env, thiz); |
| 618 | if (lpTrack == NULL) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 619 | jniThrowException(env, "java/lang/IllegalStateException", |
| 620 | "Unable to retrieve AudioTrack pointer for frameCount()"); |
| 621 | return AUDIOTRACK_ERROR; |
| 622 | } |
Eric Laurent | 532bc1c | 2012-04-20 12:45:03 -0700 | [diff] [blame] | 623 | |
| 624 | return lpTrack->frameCount(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 625 | } |
| 626 | |
| 627 | |
| 628 | // ---------------------------------------------------------------------------- |
Eric Laurent | 88e209d | 2009-07-07 07:10:45 -0700 | [diff] [blame] | 629 | 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] | 630 | jint sampleRateInHz) { |
Eric Laurent | 532bc1c | 2012-04-20 12:45:03 -0700 | [diff] [blame] | 631 | sp<AudioTrack> lpTrack = getAudioTrack(env, thiz); |
| 632 | if (lpTrack == NULL) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 633 | jniThrowException(env, "java/lang/IllegalStateException", |
| 634 | "Unable to retrieve AudioTrack pointer for setSampleRate()"); |
Eric Laurent | 88e209d | 2009-07-07 07:10:45 -0700 | [diff] [blame] | 635 | return AUDIOTRACK_ERROR; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 636 | } |
Eric Laurent | 532bc1c | 2012-04-20 12:45:03 -0700 | [diff] [blame] | 637 | return android_media_translateErrorCode(lpTrack->setSampleRate(sampleRateInHz)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 638 | } |
| 639 | |
| 640 | |
| 641 | // ---------------------------------------------------------------------------- |
| 642 | static jint android_media_AudioTrack_get_playback_rate(JNIEnv *env, jobject thiz) { |
Eric Laurent | 532bc1c | 2012-04-20 12:45:03 -0700 | [diff] [blame] | 643 | sp<AudioTrack> lpTrack = getAudioTrack(env, thiz); |
| 644 | if (lpTrack == NULL) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 645 | jniThrowException(env, "java/lang/IllegalStateException", |
| 646 | "Unable to retrieve AudioTrack pointer for getSampleRate()"); |
| 647 | return AUDIOTRACK_ERROR; |
| 648 | } |
Eric Laurent | 532bc1c | 2012-04-20 12:45:03 -0700 | [diff] [blame] | 649 | return (jint) lpTrack->getSampleRate(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 650 | } |
| 651 | |
| 652 | |
| 653 | // ---------------------------------------------------------------------------- |
Glenn Kasten | 18db49a | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 654 | 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] | 655 | jint markerPos) { |
Eric Laurent | 532bc1c | 2012-04-20 12:45:03 -0700 | [diff] [blame] | 656 | sp<AudioTrack> lpTrack = getAudioTrack(env, thiz); |
| 657 | if (lpTrack == NULL) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 658 | jniThrowException(env, "java/lang/IllegalStateException", |
| 659 | "Unable to retrieve AudioTrack pointer for setMarkerPosition()"); |
| 660 | return AUDIOTRACK_ERROR; |
| 661 | } |
Eric Laurent | 532bc1c | 2012-04-20 12:45:03 -0700 | [diff] [blame] | 662 | return android_media_translateErrorCode( lpTrack->setMarkerPosition(markerPos) ); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 663 | } |
| 664 | |
| 665 | |
| 666 | // ---------------------------------------------------------------------------- |
| 667 | static jint android_media_AudioTrack_get_marker_pos(JNIEnv *env, jobject thiz) { |
Eric Laurent | 532bc1c | 2012-04-20 12:45:03 -0700 | [diff] [blame] | 668 | sp<AudioTrack> lpTrack = getAudioTrack(env, thiz); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 669 | uint32_t markerPos = 0; |
Glenn Kasten | 18db49a | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 670 | |
Eric Laurent | 532bc1c | 2012-04-20 12:45:03 -0700 | [diff] [blame] | 671 | if (lpTrack == NULL) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 672 | jniThrowException(env, "java/lang/IllegalStateException", |
| 673 | "Unable to retrieve AudioTrack pointer for getMarkerPosition()"); |
| 674 | return AUDIOTRACK_ERROR; |
| 675 | } |
Eric Laurent | 532bc1c | 2012-04-20 12:45:03 -0700 | [diff] [blame] | 676 | lpTrack->getMarkerPosition(&markerPos); |
| 677 | return (jint)markerPos; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 678 | } |
| 679 | |
| 680 | |
| 681 | // ---------------------------------------------------------------------------- |
| 682 | static jint android_media_AudioTrack_set_pos_update_period(JNIEnv *env, jobject thiz, |
| 683 | jint period) { |
Eric Laurent | 532bc1c | 2012-04-20 12:45:03 -0700 | [diff] [blame] | 684 | sp<AudioTrack> lpTrack = getAudioTrack(env, thiz); |
| 685 | if (lpTrack == NULL) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 686 | jniThrowException(env, "java/lang/IllegalStateException", |
| 687 | "Unable to retrieve AudioTrack pointer for setPositionUpdatePeriod()"); |
| 688 | return AUDIOTRACK_ERROR; |
Glenn Kasten | 18db49a | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 689 | } |
Eric Laurent | 532bc1c | 2012-04-20 12:45:03 -0700 | [diff] [blame] | 690 | return android_media_translateErrorCode( lpTrack->setPositionUpdatePeriod(period) ); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 691 | } |
| 692 | |
| 693 | |
| 694 | // ---------------------------------------------------------------------------- |
| 695 | 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] | 696 | sp<AudioTrack> lpTrack = getAudioTrack(env, thiz); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 697 | uint32_t period = 0; |
Glenn Kasten | 18db49a | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 698 | |
Eric Laurent | 532bc1c | 2012-04-20 12:45:03 -0700 | [diff] [blame] | 699 | if (lpTrack == NULL) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 700 | jniThrowException(env, "java/lang/IllegalStateException", |
| 701 | "Unable to retrieve AudioTrack pointer for getPositionUpdatePeriod()"); |
| 702 | return AUDIOTRACK_ERROR; |
| 703 | } |
Eric Laurent | 532bc1c | 2012-04-20 12:45:03 -0700 | [diff] [blame] | 704 | lpTrack->getPositionUpdatePeriod(&period); |
| 705 | return (jint)period; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 706 | } |
| 707 | |
| 708 | |
| 709 | // ---------------------------------------------------------------------------- |
Glenn Kasten | 18db49a | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 710 | 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] | 711 | jint position) { |
Eric Laurent | 532bc1c | 2012-04-20 12:45:03 -0700 | [diff] [blame] | 712 | sp<AudioTrack> lpTrack = getAudioTrack(env, thiz); |
| 713 | if (lpTrack == NULL) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 714 | jniThrowException(env, "java/lang/IllegalStateException", |
| 715 | "Unable to retrieve AudioTrack pointer for setPosition()"); |
| 716 | return AUDIOTRACK_ERROR; |
| 717 | } |
Eric Laurent | 532bc1c | 2012-04-20 12:45:03 -0700 | [diff] [blame] | 718 | return android_media_translateErrorCode( lpTrack->setPosition(position) ); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 719 | } |
| 720 | |
| 721 | |
| 722 | // ---------------------------------------------------------------------------- |
| 723 | static jint android_media_AudioTrack_get_position(JNIEnv *env, jobject thiz) { |
Eric Laurent | 532bc1c | 2012-04-20 12:45:03 -0700 | [diff] [blame] | 724 | sp<AudioTrack> lpTrack = getAudioTrack(env, thiz); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 725 | uint32_t position = 0; |
Glenn Kasten | 18db49a | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 726 | |
Eric Laurent | 532bc1c | 2012-04-20 12:45:03 -0700 | [diff] [blame] | 727 | if (lpTrack == NULL) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 728 | jniThrowException(env, "java/lang/IllegalStateException", |
| 729 | "Unable to retrieve AudioTrack pointer for getPosition()"); |
| 730 | return AUDIOTRACK_ERROR; |
| 731 | } |
Eric Laurent | 532bc1c | 2012-04-20 12:45:03 -0700 | [diff] [blame] | 732 | lpTrack->getPosition(&position); |
| 733 | return (jint)position; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 734 | } |
| 735 | |
| 736 | |
| 737 | // ---------------------------------------------------------------------------- |
Oliver Woodman | 61dcdf3 | 2013-06-26 12:43:36 +0100 | [diff] [blame] | 738 | static jint android_media_AudioTrack_get_latency(JNIEnv *env, jobject thiz) { |
| 739 | sp<AudioTrack> lpTrack = getAudioTrack(env, thiz); |
| 740 | |
| 741 | if (lpTrack == NULL) { |
| 742 | jniThrowException(env, "java/lang/IllegalStateException", |
| 743 | "Unable to retrieve AudioTrack pointer for latency()"); |
| 744 | return AUDIOTRACK_ERROR; |
| 745 | } |
| 746 | return (jint)lpTrack->latency(); |
| 747 | } |
| 748 | |
| 749 | |
| 750 | // ---------------------------------------------------------------------------- |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 751 | static jint android_media_AudioTrack_set_loop(JNIEnv *env, jobject thiz, |
| 752 | jint loopStart, jint loopEnd, jint loopCount) { |
Eric Laurent | 532bc1c | 2012-04-20 12:45:03 -0700 | [diff] [blame] | 753 | sp<AudioTrack> lpTrack = getAudioTrack(env, thiz); |
| 754 | if (lpTrack == NULL) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 755 | jniThrowException(env, "java/lang/IllegalStateException", |
| 756 | "Unable to retrieve AudioTrack pointer for setLoop()"); |
| 757 | return AUDIOTRACK_ERROR; |
| 758 | } |
Eric Laurent | 532bc1c | 2012-04-20 12:45:03 -0700 | [diff] [blame] | 759 | return android_media_translateErrorCode( lpTrack->setLoop(loopStart, loopEnd, loopCount) ); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 760 | } |
| 761 | |
| 762 | |
| 763 | // ---------------------------------------------------------------------------- |
| 764 | static jint android_media_AudioTrack_reload(JNIEnv *env, jobject thiz) { |
Eric Laurent | 532bc1c | 2012-04-20 12:45:03 -0700 | [diff] [blame] | 765 | sp<AudioTrack> lpTrack = getAudioTrack(env, thiz); |
| 766 | if (lpTrack == NULL) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 767 | jniThrowException(env, "java/lang/IllegalStateException", |
| 768 | "Unable to retrieve AudioTrack pointer for reload()"); |
| 769 | return AUDIOTRACK_ERROR; |
| 770 | } |
Eric Laurent | 532bc1c | 2012-04-20 12:45:03 -0700 | [diff] [blame] | 771 | return android_media_translateErrorCode( lpTrack->reload() ); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 772 | } |
| 773 | |
| 774 | |
| 775 | // ---------------------------------------------------------------------------- |
| 776 | static jint android_media_AudioTrack_get_output_sample_rate(JNIEnv *env, jobject thiz, |
| 777 | jint javaStreamType) { |
Glenn Kasten | 85fbc87 | 2012-11-14 13:21:09 -0800 | [diff] [blame] | 778 | uint32_t afSamplingRate; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 779 | // convert the stream type from Java to native value |
| 780 | // FIXME: code duplication with android_media_AudioTrack_native_setup() |
Dima Zavin | 24fc2fb | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 781 | audio_stream_type_t nativeStreamType; |
Glenn Kasten | 29a0909 | 2012-01-16 14:37:12 -0800 | [diff] [blame] | 782 | switch (javaStreamType) { |
| 783 | case AUDIO_STREAM_VOICE_CALL: |
| 784 | case AUDIO_STREAM_SYSTEM: |
| 785 | case AUDIO_STREAM_RING: |
| 786 | case AUDIO_STREAM_MUSIC: |
| 787 | case AUDIO_STREAM_ALARM: |
| 788 | case AUDIO_STREAM_NOTIFICATION: |
| 789 | case AUDIO_STREAM_BLUETOOTH_SCO: |
| 790 | case AUDIO_STREAM_DTMF: |
| 791 | nativeStreamType = (audio_stream_type_t) javaStreamType; |
| 792 | break; |
| 793 | default: |
Dima Zavin | 24fc2fb | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 794 | nativeStreamType = AUDIO_STREAM_DEFAULT; |
Glenn Kasten | 29a0909 | 2012-01-16 14:37:12 -0800 | [diff] [blame] | 795 | break; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 796 | } |
| 797 | |
| 798 | if (AudioSystem::getOutputSamplingRate(&afSamplingRate, nativeStreamType) != NO_ERROR) { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 799 | ALOGE("AudioSystem::getOutputSamplingRate() for stream type %d failed in AudioTrack JNI", |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 800 | nativeStreamType); |
| 801 | return DEFAULT_OUTPUT_SAMPLE_RATE; |
| 802 | } else { |
| 803 | return afSamplingRate; |
| 804 | } |
| 805 | } |
| 806 | |
| 807 | |
| 808 | // ---------------------------------------------------------------------------- |
| 809 | // returns the minimum required size for the successful creation of a streaming AudioTrack |
| 810 | // returns -1 if there was an error querying the hardware. |
| 811 | static jint android_media_AudioTrack_get_min_buff_size(JNIEnv *env, jobject thiz, |
| 812 | jint sampleRateInHertz, jint nbChannels, jint audioFormat) { |
Chia-chi Yeh | c330807 | 2010-08-19 17:14:36 +0800 | [diff] [blame] | 813 | |
Glenn Kasten | fd1e3df | 2012-11-13 15:21:06 -0800 | [diff] [blame] | 814 | size_t frameCount = 0; |
Dima Zavin | 24fc2fb | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 815 | if (AudioTrack::getMinFrameCount(&frameCount, AUDIO_STREAM_DEFAULT, |
Chia-chi Yeh | c330807 | 2010-08-19 17:14:36 +0800 | [diff] [blame] | 816 | sampleRateInHertz) != NO_ERROR) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 817 | return -1; |
| 818 | } |
Chia-chi Yeh | c330807 | 2010-08-19 17:14:36 +0800 | [diff] [blame] | 819 | return frameCount * nbChannels * (audioFormat == javaAudioTrackFields.PCM16 ? 2 : 1); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 820 | } |
| 821 | |
Eric Laurent | 7070b36 | 2010-07-16 07:43:46 -0700 | [diff] [blame] | 822 | // ---------------------------------------------------------------------------- |
| 823 | static void |
| 824 | android_media_AudioTrack_setAuxEffectSendLevel(JNIEnv *env, jobject thiz, jfloat level ) |
| 825 | { |
Eric Laurent | 532bc1c | 2012-04-20 12:45:03 -0700 | [diff] [blame] | 826 | sp<AudioTrack> lpTrack = getAudioTrack(env, thiz); |
Eric Laurent | 7070b36 | 2010-07-16 07:43:46 -0700 | [diff] [blame] | 827 | if (lpTrack == NULL ) { |
| 828 | jniThrowException(env, "java/lang/IllegalStateException", |
| 829 | "Unable to retrieve AudioTrack pointer for setAuxEffectSendLevel()"); |
| 830 | return; |
| 831 | } |
| 832 | |
| 833 | lpTrack->setAuxEffectSendLevel(level); |
| 834 | } |
| 835 | |
| 836 | // ---------------------------------------------------------------------------- |
| 837 | static jint android_media_AudioTrack_attachAuxEffect(JNIEnv *env, jobject thiz, |
| 838 | jint effectId) { |
Eric Laurent | 532bc1c | 2012-04-20 12:45:03 -0700 | [diff] [blame] | 839 | sp<AudioTrack> lpTrack = getAudioTrack(env, thiz); |
| 840 | if (lpTrack == NULL) { |
Eric Laurent | 7070b36 | 2010-07-16 07:43:46 -0700 | [diff] [blame] | 841 | jniThrowException(env, "java/lang/IllegalStateException", |
| 842 | "Unable to retrieve AudioTrack pointer for attachAuxEffect()"); |
| 843 | return AUDIOTRACK_ERROR; |
| 844 | } |
Eric Laurent | 532bc1c | 2012-04-20 12:45:03 -0700 | [diff] [blame] | 845 | return android_media_translateErrorCode( lpTrack->attachAuxEffect(effectId) ); |
Eric Laurent | 7070b36 | 2010-07-16 07:43:46 -0700 | [diff] [blame] | 846 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 847 | |
| 848 | // ---------------------------------------------------------------------------- |
| 849 | // ---------------------------------------------------------------------------- |
| 850 | static JNINativeMethod gMethods[] = { |
| 851 | // name, signature, funcPtr |
| 852 | {"native_start", "()V", (void *)android_media_AudioTrack_start}, |
| 853 | {"native_stop", "()V", (void *)android_media_AudioTrack_stop}, |
| 854 | {"native_pause", "()V", (void *)android_media_AudioTrack_pause}, |
| 855 | {"native_flush", "()V", (void *)android_media_AudioTrack_flush}, |
Eric Laurent | 619346f | 2010-06-21 09:27:30 -0700 | [diff] [blame] | 856 | {"native_setup", "(Ljava/lang/Object;IIIIII[I)I", |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 857 | (void *)android_media_AudioTrack_native_setup}, |
| 858 | {"native_finalize", "()V", (void *)android_media_AudioTrack_native_finalize}, |
| 859 | {"native_release", "()V", (void *)android_media_AudioTrack_native_release}, |
Glenn Kasten | 00db1f5 | 2012-01-16 14:41:30 -0800 | [diff] [blame] | 860 | {"native_write_byte", "([BIII)I", (void *)android_media_AudioTrack_native_write_byte}, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 861 | {"native_write_short", "([SIII)I", (void *)android_media_AudioTrack_native_write_short}, |
| 862 | {"native_setVolume", "(FF)V", (void *)android_media_AudioTrack_set_volume}, |
| 863 | {"native_get_native_frame_count", |
| 864 | "()I", (void *)android_media_AudioTrack_get_native_frame_count}, |
| 865 | {"native_set_playback_rate", |
Eric Laurent | 88e209d | 2009-07-07 07:10:45 -0700 | [diff] [blame] | 866 | "(I)I", (void *)android_media_AudioTrack_set_playback_rate}, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 867 | {"native_get_playback_rate", |
| 868 | "()I", (void *)android_media_AudioTrack_get_playback_rate}, |
| 869 | {"native_set_marker_pos","(I)I", (void *)android_media_AudioTrack_set_marker_pos}, |
| 870 | {"native_get_marker_pos","()I", (void *)android_media_AudioTrack_get_marker_pos}, |
| 871 | {"native_set_pos_update_period", |
| 872 | "(I)I", (void *)android_media_AudioTrack_set_pos_update_period}, |
| 873 | {"native_get_pos_update_period", |
| 874 | "()I", (void *)android_media_AudioTrack_get_pos_update_period}, |
| 875 | {"native_set_position", "(I)I", (void *)android_media_AudioTrack_set_position}, |
| 876 | {"native_get_position", "()I", (void *)android_media_AudioTrack_get_position}, |
Oliver Woodman | 61dcdf3 | 2013-06-26 12:43:36 +0100 | [diff] [blame] | 877 | {"native_get_latency", "()I", (void *)android_media_AudioTrack_get_latency}, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 878 | {"native_set_loop", "(III)I", (void *)android_media_AudioTrack_set_loop}, |
| 879 | {"native_reload_static", "()I", (void *)android_media_AudioTrack_reload}, |
| 880 | {"native_get_output_sample_rate", |
| 881 | "(I)I", (void *)android_media_AudioTrack_get_output_sample_rate}, |
| 882 | {"native_get_min_buff_size", |
| 883 | "(III)I", (void *)android_media_AudioTrack_get_min_buff_size}, |
Eric Laurent | 7070b36 | 2010-07-16 07:43:46 -0700 | [diff] [blame] | 884 | {"native_setAuxEffectSendLevel", |
| 885 | "(F)V", (void *)android_media_AudioTrack_setAuxEffectSendLevel}, |
| 886 | {"native_attachAuxEffect", |
| 887 | "(I)I", (void *)android_media_AudioTrack_attachAuxEffect}, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 888 | }; |
| 889 | |
| 890 | |
| 891 | // field names found in android/media/AudioTrack.java |
| 892 | #define JAVA_POSTEVENT_CALLBACK_NAME "postEventFromNative" |
| 893 | #define JAVA_CONST_PCM16_NAME "ENCODING_PCM_16BIT" |
| 894 | #define JAVA_CONST_PCM8_NAME "ENCODING_PCM_8BIT" |
| 895 | #define JAVA_CONST_BUFFER_COUNT_NAME "BUFFER_COUNT" |
| 896 | #define JAVA_CONST_STREAM_VOICE_CALL_NAME "STREAM_VOICE_CALL" |
| 897 | #define JAVA_CONST_STREAM_SYSTEM_NAME "STREAM_SYSTEM" |
| 898 | #define JAVA_CONST_STREAM_RING_NAME "STREAM_RING" |
| 899 | #define JAVA_CONST_STREAM_MUSIC_NAME "STREAM_MUSIC" |
| 900 | #define JAVA_CONST_STREAM_ALARM_NAME "STREAM_ALARM" |
| 901 | #define JAVA_CONST_STREAM_NOTIFICATION_NAME "STREAM_NOTIFICATION" |
| 902 | #define JAVA_CONST_STREAM_BLUETOOTH_SCO_NAME "STREAM_BLUETOOTH_SCO" |
Eric Laurent | 83b3685 | 2009-07-28 07:49:22 -0700 | [diff] [blame] | 903 | #define JAVA_CONST_STREAM_DTMF_NAME "STREAM_DTMF" |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 904 | #define JAVA_NATIVETRACKINJAVAOBJ_FIELD_NAME "mNativeTrackInJavaObj" |
| 905 | #define JAVA_JNIDATA_FIELD_NAME "mJniData" |
| 906 | |
| 907 | #define JAVA_AUDIOFORMAT_CLASS_NAME "android/media/AudioFormat" |
| 908 | #define JAVA_AUDIOMANAGER_CLASS_NAME "android/media/AudioManager" |
| 909 | |
| 910 | // ---------------------------------------------------------------------------- |
| 911 | // preconditions: |
| 912 | // theClass is valid |
| 913 | bool android_media_getIntConstantFromClass(JNIEnv* pEnv, jclass theClass, const char* className, |
| 914 | const char* constName, int* constVal) { |
| 915 | jfieldID javaConst = NULL; |
| 916 | javaConst = pEnv->GetStaticFieldID(theClass, constName, "I"); |
| 917 | if (javaConst != NULL) { |
| 918 | *constVal = pEnv->GetStaticIntField(theClass, javaConst); |
| 919 | return true; |
| 920 | } else { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 921 | ALOGE("Can't find %s.%s", className, constName); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 922 | return false; |
| 923 | } |
| 924 | } |
| 925 | |
| 926 | |
| 927 | // ---------------------------------------------------------------------------- |
| 928 | int register_android_media_AudioTrack(JNIEnv *env) |
| 929 | { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 930 | javaAudioTrackFields.nativeTrackInJavaObj = NULL; |
| 931 | javaAudioTrackFields.postNativeEventInJava = NULL; |
| 932 | |
| 933 | // Get the AudioTrack class |
Brian Carlstrom | 46e18c11 | 2011-04-05 22:44:45 -0700 | [diff] [blame] | 934 | jclass audioTrackClass = env->FindClass(kClassPathName); |
| 935 | if (audioTrackClass == NULL) { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 936 | ALOGE("Can't find %s", kClassPathName); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 937 | return -1; |
| 938 | } |
| 939 | |
| 940 | // Get the postEvent method |
| 941 | javaAudioTrackFields.postNativeEventInJava = env->GetStaticMethodID( |
Brian Carlstrom | 46e18c11 | 2011-04-05 22:44:45 -0700 | [diff] [blame] | 942 | audioTrackClass, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 943 | JAVA_POSTEVENT_CALLBACK_NAME, "(Ljava/lang/Object;IIILjava/lang/Object;)V"); |
| 944 | if (javaAudioTrackFields.postNativeEventInJava == NULL) { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 945 | ALOGE("Can't find AudioTrack.%s", JAVA_POSTEVENT_CALLBACK_NAME); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 946 | return -1; |
| 947 | } |
| 948 | |
| 949 | // Get the variables fields |
| 950 | // nativeTrackInJavaObj |
| 951 | javaAudioTrackFields.nativeTrackInJavaObj = env->GetFieldID( |
Brian Carlstrom | 46e18c11 | 2011-04-05 22:44:45 -0700 | [diff] [blame] | 952 | audioTrackClass, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 953 | JAVA_NATIVETRACKINJAVAOBJ_FIELD_NAME, "I"); |
| 954 | if (javaAudioTrackFields.nativeTrackInJavaObj == NULL) { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 955 | ALOGE("Can't find AudioTrack.%s", JAVA_NATIVETRACKINJAVAOBJ_FIELD_NAME); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 956 | return -1; |
| 957 | } |
| 958 | // jniData; |
| 959 | javaAudioTrackFields.jniData = env->GetFieldID( |
Brian Carlstrom | 46e18c11 | 2011-04-05 22:44:45 -0700 | [diff] [blame] | 960 | audioTrackClass, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 961 | JAVA_JNIDATA_FIELD_NAME, "I"); |
| 962 | if (javaAudioTrackFields.jniData == NULL) { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 963 | ALOGE("Can't find AudioTrack.%s", JAVA_JNIDATA_FIELD_NAME); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 964 | return -1; |
| 965 | } |
| 966 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 967 | // Get the format constants from the AudioFormat class |
| 968 | jclass audioFormatClass = NULL; |
| 969 | audioFormatClass = env->FindClass(JAVA_AUDIOFORMAT_CLASS_NAME); |
| 970 | if (audioFormatClass == NULL) { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 971 | ALOGE("Can't find %s", JAVA_AUDIOFORMAT_CLASS_NAME); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 972 | return -1; |
| 973 | } |
Glenn Kasten | 18db49a | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 974 | if ( !android_media_getIntConstantFromClass(env, audioFormatClass, |
| 975 | JAVA_AUDIOFORMAT_CLASS_NAME, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 976 | JAVA_CONST_PCM16_NAME, &(javaAudioTrackFields.PCM16)) |
Glenn Kasten | 18db49a | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 977 | || !android_media_getIntConstantFromClass(env, audioFormatClass, |
| 978 | JAVA_AUDIOFORMAT_CLASS_NAME, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 979 | JAVA_CONST_PCM8_NAME, &(javaAudioTrackFields.PCM8)) ) { |
Glenn Kasten | 18db49a | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 980 | // error log performed in android_media_getIntConstantFromClass() |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 981 | return -1; |
| 982 | } |
Glenn Kasten | 18db49a | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 983 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 984 | return AndroidRuntime::registerNativeMethods(env, kClassPathName, gMethods, NELEM(gMethods)); |
| 985 | } |
| 986 | |
| 987 | |
| 988 | // ---------------------------------------------------------------------------- |