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