blob: 3ff9dda1de7bb8276745d7320e783b05c986e584 [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
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 Kastenc81d31c2012-03-13 14:46:23 -070025#include <jni.h>
26#include <JNIHelp.h>
27#include <android_runtime/AndroidRuntime.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080028
Glenn Kastenc81d31c2012-03-13 14:46:23 -070029#include <utils/Log.h>
30#include <media/AudioSystem.h>
31#include <media/AudioTrack.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080032
Mathias Agopian07952722009-05-19 19:08:10 -070033#include <binder/MemoryHeapBase.h>
34#include <binder/MemoryBase.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080035
Dima Zavin24fc2fb2011-04-19 22:30:36 -070036#include <cutils/bitops.h>
37
Dima Zavin34bb4192011-05-11 14:15:23 -070038#include <system/audio.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080039
40// ----------------------------------------------------------------------------
41
42using namespace android;
43
44// ----------------------------------------------------------------------------
45static const char* const kClassPathName = "android/media/AudioTrack";
46
47struct fields_t {
48 // these fields provide access from C++ to the...
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080049 jmethodID postNativeEventInJava; //... event post callback method
50 int PCM16; //... format constants
51 int PCM8; //... format constants
Eric Laurent83b36852009-07-28 07:49:22 -070052 jfieldID nativeTrackInJavaObj; // stores in Java the native AudioTrack object
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080053 jfieldID jniData; // stores in Java additional resources used by the native AudioTrack
54};
55static fields_t javaAudioTrackFields;
56
57struct audiotrack_callback_cookie {
58 jclass audioTrack_class;
59 jobject audioTrack_ref;
Eric Laurent532bc1c2012-04-20 12:45:03 -070060 bool busy;
61 Condition cond;
62};
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080063
Glenn Kasten3d301cb2012-01-16 14:46:54 -080064// keep these values in sync with AudioTrack.java
65#define MODE_STATIC 0
66#define MODE_STREAM 1
67
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080068// ----------------------------------------------------------------------------
69class AudioTrackJniStorage {
70 public:
71 sp<MemoryHeapBase> mMemHeap;
72 sp<MemoryBase> mMemBase;
73 audiotrack_callback_cookie mCallbackData;
Glenn Kastenbc1d77b2012-01-12 16:38:12 -080074 audio_stream_type_t mStreamType;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080075
76 AudioTrackJniStorage() {
Jean-Michel Trivi8a149682009-07-15 18:31:11 -070077 mCallbackData.audioTrack_class = 0;
78 mCallbackData.audioTrack_ref = 0;
Dima Zavin24fc2fb2011-04-19 22:30:36 -070079 mStreamType = AUDIO_STREAM_DEFAULT;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080080 }
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 Laurent532bc1c2012-04-20 12:45:03 -070097static Mutex sLock;
98static SortedVector <audiotrack_callback_cookie *> sAudioTrackCallBackCookies;
99
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800100// ----------------------------------------------------------------------------
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 Kastenfb2ab9e2011-12-12 09:05:55 -0800108#define AUDIOTRACK_ERROR_SETUP_INVALIDCHANNELMASK -17
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800109#define AUDIOTRACK_ERROR_SETUP_INVALIDFORMAT -18
110#define AUDIOTRACK_ERROR_SETUP_INVALIDSTREAMTYPE -19
111#define AUDIOTRACK_ERROR_SETUP_NATIVEINITFAILED -20
112
113
114jint android_media_translateErrorCode(int code) {
Glenn Kasten18db49a2012-03-12 16:29:55 -0700115 switch (code) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800116 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 Kasten18db49a2012-03-12 16:29:55 -0700124 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800125}
126
127
128// ----------------------------------------------------------------------------
Glenn Kastene46a86f2011-06-01 15:20:35 -0700129static void audioCallback(int event, void* user, void *info) {
Eric Laurent532bc1c2012-04-20 12:45:03 -0700130
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 Project9066cfe2009-03-03 19:31:44 -0800140 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 Kasten18db49a2012-03-12 16:29:55 -0700143 pBuff->size = 0;
144
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800145 } else if (event == AudioTrack::EVENT_MARKER) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800146 JNIEnv *env = AndroidRuntime::getJNIEnv();
147 if (user && env) {
148 env->CallStaticVoidMethod(
Glenn Kasten18db49a2012-03-12 16:29:55 -0700149 callbackInfo->audioTrack_class,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800150 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 Project9066cfe2009-03-03 19:31:44 -0800159 JNIEnv *env = AndroidRuntime::getJNIEnv();
160 if (user && env) {
161 env->CallStaticVoidMethod(
Glenn Kasten18db49a2012-03-12 16:29:55 -0700162 callbackInfo->audioTrack_class,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800163 javaAudioTrackFields.postNativeEventInJava,
164 callbackInfo->audioTrack_ref, event, 0,0, NULL);
165 if (env->ExceptionCheck()) {
166 env->ExceptionDescribe();
167 env->ExceptionClear();
168 }
169 }
170 }
Eric Laurent532bc1c2012-04-20 12:45:03 -0700171 {
172 Mutex::Autolock l(sLock);
173 callbackInfo->busy = false;
174 callbackInfo->cond.broadcast();
175 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800176}
177
178
179// ----------------------------------------------------------------------------
Eric Laurent532bc1c2012-04-20 12:45:03 -0700180static 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
188static 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 Agopianb1d90c82013-03-06 17:45:42 -0800194 at->incStrong((void*)setAudioTrack);
Eric Laurent532bc1c2012-04-20 12:45:03 -0700195 }
196 if (old != 0) {
Mathias Agopianb1d90c82013-03-06 17:45:42 -0800197 old->decStrong((void*)setAudioTrack);
Eric Laurent532bc1c2012-04-20 12:45:03 -0700198 }
199 env->SetIntField(thiz, javaAudioTrackFields.nativeTrackInJavaObj, (int)at.get());
200 return old;
201}
202
203// ----------------------------------------------------------------------------
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800204static int
205android_media_AudioTrack_native_setup(JNIEnv *env, jobject thiz, jobject weak_this,
Jean-Michel Trivid9ae1c52011-07-25 12:58:14 -0700206 jint streamType, jint sampleRateInHertz, jint javaChannelMask,
Eric Laurent619346f2010-06-21 09:27:30 -0700207 jint audioFormat, jint buffSizeInBytes, jint memoryMode, jintArray jSession)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800208{
Steve Block71f2cf12011-10-20 11:56:00 +0100209 ALOGV("sampleRate=%d, audioFormat(from Java)=%d, channel mask=%x, buffSize=%d",
Jean-Michel Trivid9ae1c52011-07-25 12:58:14 -0700210 sampleRateInHertz, audioFormat, javaChannelMask, buffSizeInBytes);
Glenn Kasten85fbc872012-11-14 13:21:09 -0800211 uint32_t afSampleRate;
Glenn Kastenfd1e3df2012-11-13 15:21:06 -0800212 size_t afFrameCount;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800213
Glenn Kastenbc1d77b2012-01-12 16:38:12 -0800214 if (AudioSystem::getOutputFrameCount(&afFrameCount, (audio_stream_type_t) streamType) != NO_ERROR) {
Steve Block3762c312012-01-06 19:20:56 +0000215 ALOGE("Error creating AudioTrack: Could not get AudioSystem frame count.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800216 return AUDIOTRACK_ERROR_SETUP_AUDIOSYSTEM;
217 }
Glenn Kastenbc1d77b2012-01-12 16:38:12 -0800218 if (AudioSystem::getOutputSamplingRate(&afSampleRate, (audio_stream_type_t) streamType) != NO_ERROR) {
Steve Block3762c312012-01-06 19:20:56 +0000219 ALOGE("Error creating AudioTrack: Could not get AudioSystem sampling rate.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800220 return AUDIOTRACK_ERROR_SETUP_AUDIOSYSTEM;
221 }
222
Jean-Michel Trivid9ae1c52011-07-25 12:58:14 -0700223 // 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 Block3762c312012-01-06 19:20:56 +0000228 ALOGE("Error creating AudioTrack: invalid channel mask.");
Eric Laurenta553c252009-07-17 12:17:14 -0700229 return AUDIOTRACK_ERROR_SETUP_INVALIDCHANNELMASK;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800230 }
Jean-Michel Trivid9ae1c52011-07-25 12:58:14 -0700231
232 int nbChannels = popcount(nativeChannelMask);
Glenn Kasten18db49a2012-03-12 16:29:55 -0700233
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800234 // check the stream type
Dima Zavin24fc2fb2011-04-19 22:30:36 -0700235 audio_stream_type_t atStreamType;
Glenn Kasten29a09092012-01-16 14:37:12 -0800236 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 Block3762c312012-01-06 19:20:56 +0000248 ALOGE("Error creating AudioTrack: unknown stream type.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800249 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 Block3762c312012-01-06 19:20:56 +0000255 ALOGE("Error creating AudioTrack: unsupported audio format.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800256 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 Kasten00db1f52012-01-16 14:41:30 -0800261 // in android_media_AudioTrack_native_write_byte()
Glenn Kasten18db49a2012-03-12 16:29:55 -0700262 if ((audioFormat == javaAudioTrackFields.PCM8)
Glenn Kasten3d301cb2012-01-16 14:46:54 -0800263 && (memoryMode == MODE_STATIC)) {
Steve Block71f2cf12011-10-20 11:56:00 +0100264 ALOGV("android_media_AudioTrack_native_setup(): requesting MODE_STATIC for 8bit \
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800265 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 Kasten1c5a89d2012-01-04 09:36:37 -0800274 audio_format_t format = audioFormat == javaAudioTrackFields.PCM16 ?
Dima Zavin24fc2fb2011-04-19 22:30:36 -0700275 AUDIO_FORMAT_PCM_16_BIT : AUDIO_FORMAT_PCM_8_BIT;
Eric Laurenta553c252009-07-17 12:17:14 -0700276 int frameCount = buffSizeInBytes / (nbChannels * bytesPerSample);
Glenn Kasten18db49a2012-03-12 16:29:55 -0700277
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800278 jclass clazz = env->GetObjectClass(thiz);
279 if (clazz == NULL) {
Steve Block3762c312012-01-06 19:20:56 +0000280 ALOGE("Can't find %s when setting up callback.", kClassPathName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800281 return AUDIOTRACK_ERROR_SETUP_NATIVEINITFAILED;
282 }
Eric Laurent2fb43ef2010-09-24 12:03:36 -0700283
284 if (jSession == NULL) {
Steve Block3762c312012-01-06 19:20:56 +0000285 ALOGE("Error creating AudioTrack: invalid session ID pointer");
Eric Laurent619346f2010-06-21 09:27:30 -0700286 return AUDIOTRACK_ERROR;
287 }
288
Eric Laurent2fb43ef2010-09-24 12:03:36 -0700289 jint* nSession = (jint *) env->GetPrimitiveArrayCritical(jSession, NULL);
290 if (nSession == NULL) {
Steve Block3762c312012-01-06 19:20:56 +0000291 ALOGE("Error creating AudioTrack: Error retrieving session id pointer");
Eric Laurent2fb43ef2010-09-24 12:03:36 -0700292 return AUDIOTRACK_ERROR;
293 }
294 int sessionId = nSession[0];
295 env->ReleasePrimitiveArrayCritical(jSession, nSession, 0);
296 nSession = NULL;
297
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800298 // create the native AudioTrack object
Eric Laurent532bc1c2012-04-20 12:45:03 -0700299 sp<AudioTrack> lpTrack = new AudioTrack();
Glenn Kasten18db49a2012-03-12 16:29:55 -0700300
Eric Laurent532bc1c2012-04-20 12:45:03 -0700301 // 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 Project9066cfe2009-03-03 19:31:44 -0800310 // initialize the native AudioTrack object
Glenn Kasten3d301cb2012-01-16 14:46:54 -0800311 switch (memoryMode) {
312 case MODE_STREAM:
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800313
314 lpTrack->set(
315 atStreamType,// stream type
316 sampleRateInHertz,
317 format,// word length, PCM
Jean-Michel Trivid9ae1c52011-07-25 12:58:14 -0700318 nativeChannelMask,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800319 frameCount,
Eric Laurent10536b62012-04-18 09:27:14 -0700320 AUDIO_OUTPUT_FLAG_NONE,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800321 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 Laurent619346f2010-06-21 09:27:30 -0700324 true,// thread can call Java
Eric Laurent2fb43ef2010-09-24 12:03:36 -0700325 sessionId);// audio session ID
Glenn Kasten3d301cb2012-01-16 14:46:54 -0800326 break;
Glenn Kasten18db49a2012-03-12 16:29:55 -0700327
Glenn Kasten3d301cb2012-01-16 14:46:54 -0800328 case MODE_STATIC:
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800329 // AudioTrack is using shared memory
Glenn Kasten18db49a2012-03-12 16:29:55 -0700330
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800331 if (!lpJniStorage->allocSharedMem(buffSizeInBytes)) {
Steve Block3762c312012-01-06 19:20:56 +0000332 ALOGE("Error creating AudioTrack in static mode: error creating mem heap base");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800333 goto native_init_failure;
334 }
Glenn Kasten18db49a2012-03-12 16:29:55 -0700335
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800336 lpTrack->set(
337 atStreamType,// stream type
338 sampleRateInHertz,
339 format,// word length, PCM
Jean-Michel Trivid9ae1c52011-07-25 12:58:14 -0700340 nativeChannelMask,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800341 frameCount,
Eric Laurent10536b62012-04-18 09:27:14 -0700342 AUDIO_OUTPUT_FLAG_NONE,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800343 audioCallback, &(lpJniStorage->mCallbackData),//callback, callback data (user));
Glenn Kasten18db49a2012-03-12 16:29:55 -0700344 0,// notificationFrames == 0 since not using EVENT_MORE_DATA to feed the AudioTrack
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800345 lpJniStorage->mMemBase,// shared mem
Eric Laurent619346f2010-06-21 09:27:30 -0700346 true,// thread can call Java
Eric Laurent2fb43ef2010-09-24 12:03:36 -0700347 sessionId);// audio session ID
Glenn Kasten3d301cb2012-01-16 14:46:54 -0800348 break;
349
350 default:
351 ALOGE("Unknown mode %d", memoryMode);
352 goto native_init_failure;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800353 }
354
355 if (lpTrack->initCheck() != NO_ERROR) {
Steve Block3762c312012-01-06 19:20:56 +0000356 ALOGE("Error initializing AudioTrack");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800357 goto native_init_failure;
358 }
359
Eric Laurent2fb43ef2010-09-24 12:03:36 -0700360 nSession = (jint *) env->GetPrimitiveArrayCritical(jSession, NULL);
361 if (nSession == NULL) {
Steve Block3762c312012-01-06 19:20:56 +0000362 ALOGE("Error creating AudioTrack: Error retrieving session id pointer");
Eric Laurent2fb43ef2010-09-24 12:03:36 -0700363 goto native_init_failure;
364 }
Eric Laurent619346f2010-06-21 09:27:30 -0700365 // read the audio session ID back from AudioTrack in case we create a new session
366 nSession[0] = lpTrack->getSessionId();
Eric Laurent619346f2010-06-21 09:27:30 -0700367 env->ReleasePrimitiveArrayCritical(jSession, nSession, 0);
368 nSession = NULL;
369
Eric Laurent532bc1c2012-04-20 12:45:03 -0700370 { // scope for the lock
371 Mutex::Autolock l(sLock);
372 sAudioTrackCallBackCookies.add(&lpJniStorage->mCallbackData);
373 }
Glenn Kasten18db49a2012-03-12 16:29:55 -0700374 // save our newly created C++ AudioTrack in the "nativeTrackInJavaObj" field
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800375 // of the Java object (in mNativeTrackInJavaObj)
Eric Laurent532bc1c2012-04-20 12:45:03 -0700376 setAudioTrack(env, thiz, lpTrack);
Glenn Kasten18db49a2012-03-12 16:29:55 -0700377
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800378 // save the JNI resources so we can free them later
Steve Block71f2cf12011-10-20 11:56:00 +0100379 //ALOGV("storing lpJniStorage: %x\n", (int)lpJniStorage);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800380 env->SetIntField(thiz, javaAudioTrackFields.jniData, (int)lpJniStorage);
381
382 return AUDIOTRACK_SUCCESS;
Glenn Kasten18db49a2012-03-12 16:29:55 -0700383
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800384 // failures:
385native_init_failure:
Eric Laurent619346f2010-06-21 09:27:30 -0700386 if (nSession != NULL) {
387 env->ReleasePrimitiveArrayCritical(jSession, nSession, 0);
388 }
Jean-Michel Trivi8a149682009-07-15 18:31:11 -0700389 env->DeleteGlobalRef(lpJniStorage->mCallbackData.audioTrack_class);
390 env->DeleteGlobalRef(lpJniStorage->mCallbackData.audioTrack_ref);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800391 delete lpJniStorage;
392 env->SetIntField(thiz, javaAudioTrackFields.jniData, 0);
Glenn Kasten18db49a2012-03-12 16:29:55 -0700393
Eric Laurent532bc1c2012-04-20 12:45:03 -0700394 return AUDIOTRACK_ERROR_SETUP_NATIVEINITFAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800395}
396
397
398// ----------------------------------------------------------------------------
399static void
400android_media_AudioTrack_start(JNIEnv *env, jobject thiz)
401{
Eric Laurent532bc1c2012-04-20 12:45:03 -0700402 sp<AudioTrack> lpTrack = getAudioTrack(env, thiz);
403 if (lpTrack == NULL) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800404 jniThrowException(env, "java/lang/IllegalStateException",
405 "Unable to retrieve AudioTrack pointer for start()");
Eric Laurent7070b362010-07-16 07:43:46 -0700406 return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800407 }
Jean-Michel Trivi21dc0372009-05-08 16:06:34 -0700408
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800409 lpTrack->start();
410}
411
412
413// ----------------------------------------------------------------------------
414static void
415android_media_AudioTrack_stop(JNIEnv *env, jobject thiz)
416{
Eric Laurent532bc1c2012-04-20 12:45:03 -0700417 sp<AudioTrack> lpTrack = getAudioTrack(env, thiz);
418 if (lpTrack == NULL) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800419 jniThrowException(env, "java/lang/IllegalStateException",
420 "Unable to retrieve AudioTrack pointer for stop()");
Eric Laurent7070b362010-07-16 07:43:46 -0700421 return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800422 }
423
424 lpTrack->stop();
425}
426
427
428// ----------------------------------------------------------------------------
429static void
430android_media_AudioTrack_pause(JNIEnv *env, jobject thiz)
431{
Eric Laurent532bc1c2012-04-20 12:45:03 -0700432 sp<AudioTrack> lpTrack = getAudioTrack(env, thiz);
433 if (lpTrack == NULL) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800434 jniThrowException(env, "java/lang/IllegalStateException",
435 "Unable to retrieve AudioTrack pointer for pause()");
Eric Laurent7070b362010-07-16 07:43:46 -0700436 return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800437 }
438
439 lpTrack->pause();
440}
441
442
443// ----------------------------------------------------------------------------
444static void
445android_media_AudioTrack_flush(JNIEnv *env, jobject thiz)
446{
Eric Laurent532bc1c2012-04-20 12:45:03 -0700447 sp<AudioTrack> lpTrack = getAudioTrack(env, thiz);
448 if (lpTrack == NULL) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800449 jniThrowException(env, "java/lang/IllegalStateException",
450 "Unable to retrieve AudioTrack pointer for flush()");
Eric Laurent7070b362010-07-16 07:43:46 -0700451 return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800452 }
453
454 lpTrack->flush();
455}
456
457// ----------------------------------------------------------------------------
458static void
459android_media_AudioTrack_set_volume(JNIEnv *env, jobject thiz, jfloat leftVol, jfloat rightVol )
460{
Eric Laurent532bc1c2012-04-20 12:45:03 -0700461 sp<AudioTrack> lpTrack = getAudioTrack(env, thiz);
462 if (lpTrack == NULL) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800463 jniThrowException(env, "java/lang/IllegalStateException",
464 "Unable to retrieve AudioTrack pointer for setVolume()");
Eric Laurent7070b362010-07-16 07:43:46 -0700465 return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800466 }
467
468 lpTrack->setVolume(leftVol, rightVol);
469}
470
471// ----------------------------------------------------------------------------
Glenn Kasten18db49a2012-03-12 16:29:55 -0700472
Eric Laurent532bc1c2012-04-20 12:45:03 -0700473#define CALLBACK_COND_WAIT_TIMEOUT_MS 1000
474static 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 Project9066cfe2009-03-03 19:31:44 -0800478 }
Eric Laurent532bc1c2012-04-20 12:45:03 -0700479 //ALOGV("deleting lpTrack: %x\n", (int)lpTrack);
480 lpTrack->stop();
Glenn Kasten18db49a2012-03-12 16:29:55 -0700481
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800482 // delete the JNI data
483 AudioTrackJniStorage* pJniStorage = (AudioTrackJniStorage *)env->GetIntField(
484 thiz, javaAudioTrackFields.jniData);
Eric Laurent532bc1c2012-04-20 12:45:03 -0700485 // 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 Project9066cfe2009-03-03 19:31:44 -0800489 if (pJniStorage) {
Eric Laurent532bc1c2012-04-20 12:45:03 -0700490 Mutex::Autolock l(sLock);
491 audiotrack_callback_cookie *lpCookie = &pJniStorage->mCallbackData;
Steve Block71f2cf12011-10-20 11:56:00 +0100492 //ALOGV("deleting pJniStorage: %x\n", (int)pJniStorage);
Eric Laurent532bc1c2012-04-20 12:45:03 -0700493 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 Project9066cfe2009-03-03 19:31:44 -0800504 delete pJniStorage;
505 }
506}
507
Glenn Kasten18db49a2012-03-12 16:29:55 -0700508
Eric Laurent532bc1c2012-04-20 12:45:03 -0700509// ----------------------------------------------------------------------------
510static 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 Project9066cfe2009-03-03 19:31:44 -0800513}
514
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800515// ----------------------------------------------------------------------------
Eric Laurent532bc1c2012-04-20 12:45:03 -0700516jint writeToTrack(const sp<AudioTrack>& track, jint audioFormat, jbyte* data,
Jean-Michel Trivi21dc0372009-05-08 16:06:34 -0700517 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 Laurent532bc1c2012-04-20 12:45:03 -0700521 if (track->sharedBuffer() == 0) {
522 written = track->write(data + offsetInBytes, sizeInBytes);
Jean-Michel Trivi21dc0372009-05-08 16:06:34 -0700523 } else {
524 if (audioFormat == javaAudioTrackFields.PCM16) {
525 // writing to shared memory, check for capacity
Eric Laurent532bc1c2012-04-20 12:45:03 -0700526 if ((size_t)sizeInBytes > track->sharedBuffer()->size()) {
527 sizeInBytes = track->sharedBuffer()->size();
Jean-Michel Trivi21dc0372009-05-08 16:06:34 -0700528 }
Eric Laurent532bc1c2012-04-20 12:45:03 -0700529 memcpy(track->sharedBuffer()->pointer(), data + offsetInBytes, sizeInBytes);
Jean-Michel Trivi21dc0372009-05-08 16:06:34 -0700530 written = sizeInBytes;
531 } else if (audioFormat == javaAudioTrackFields.PCM8) {
532 // data contains 8bit data we need to expand to 16bit before copying
533 // to the shared memory
534 // writing to shared memory, check for capacity,
535 // note that input data will occupy 2X the input space due to 8 to 16bit conversion
Eric Laurent532bc1c2012-04-20 12:45:03 -0700536 if (((size_t)sizeInBytes)*2 > track->sharedBuffer()->size()) {
537 sizeInBytes = track->sharedBuffer()->size() / 2;
Jean-Michel Trivi21dc0372009-05-08 16:06:34 -0700538 }
539 int count = sizeInBytes;
Eric Laurent532bc1c2012-04-20 12:45:03 -0700540 int16_t *dst = (int16_t *)track->sharedBuffer()->pointer();
Jean-Michel Trivi21dc0372009-05-08 16:06:34 -0700541 const int8_t *src = (const int8_t *)(data + offsetInBytes);
Glenn Kasten18db49a2012-03-12 16:29:55 -0700542 while (count--) {
Jean-Michel Trivi21dc0372009-05-08 16:06:34 -0700543 *dst++ = (int16_t)(*src++^0x80) << 8;
544 }
545 // even though we wrote 2*sizeInBytes, we only report sizeInBytes as written to hide
546 // the 8bit mixer restriction from the user of this function
547 written = sizeInBytes;
548 }
549 }
550 return written;
551
552}
553
554// ----------------------------------------------------------------------------
Glenn Kasten00db1f52012-01-16 14:41:30 -0800555static jint android_media_AudioTrack_native_write_byte(JNIEnv *env, jobject thiz,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800556 jbyteArray javaAudioData,
557 jint offsetInBytes, jint sizeInBytes,
558 jint javaAudioFormat) {
Glenn Kasten00db1f52012-01-16 14:41:30 -0800559 //ALOGV("android_media_AudioTrack_native_write_byte(offset=%d, sizeInBytes=%d) called",
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800560 // offsetInBytes, sizeInBytes);
Eric Laurent532bc1c2012-04-20 12:45:03 -0700561 sp<AudioTrack> lpTrack = getAudioTrack(env, thiz);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800562 if (lpTrack == NULL) {
563 jniThrowException(env, "java/lang/IllegalStateException",
564 "Unable to retrieve AudioTrack pointer for write()");
Eric Laurent7070b362010-07-16 07:43:46 -0700565 return 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800566 }
567
568 // get the pointer for the audio data from the java array
Eric Laurent421ddc02011-03-07 14:52:59 -0800569 // NOTE: We may use GetPrimitiveArrayCritical() when the JNI implementation changes in such
570 // a way that it becomes much more efficient. When doing so, we will have to prevent the
571 // AudioSystem callback to be called while in critical section (in case of media server
572 // process crash for instance)
Eric Laurent532bc1c2012-04-20 12:45:03 -0700573 jbyte* cAudioData = NULL;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800574 if (javaAudioData) {
Eric Laurent421ddc02011-03-07 14:52:59 -0800575 cAudioData = (jbyte *)env->GetByteArrayElements(javaAudioData, NULL);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800576 if (cAudioData == NULL) {
Steve Block3762c312012-01-06 19:20:56 +0000577 ALOGE("Error retrieving source of audio data to play, can't play");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800578 return 0; // out of memory or no data to load
579 }
580 } else {
Steve Block3762c312012-01-06 19:20:56 +0000581 ALOGE("NULL java array of audio data to play, can't play");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800582 return 0;
583 }
584
Jean-Michel Trivi21dc0372009-05-08 16:06:34 -0700585 jint written = writeToTrack(lpTrack, javaAudioFormat, cAudioData, offsetInBytes, sizeInBytes);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800586
Eric Laurent421ddc02011-03-07 14:52:59 -0800587 env->ReleaseByteArrayElements(javaAudioData, cAudioData, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800588
Steve Block71f2cf12011-10-20 11:56:00 +0100589 //ALOGV("write wrote %d (tried %d) bytes in the native AudioTrack with offset %d",
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800590 // (int)written, (int)(sizeInBytes), (int)offsetInBytes);
Jean-Michel Trivi21dc0372009-05-08 16:06:34 -0700591 return written;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800592}
593
594
595// ----------------------------------------------------------------------------
596static jint android_media_AudioTrack_native_write_short(JNIEnv *env, jobject thiz,
597 jshortArray javaAudioData,
598 jint offsetInShorts, jint sizeInShorts,
599 jint javaAudioFormat) {
Glenn Kasten00db1f52012-01-16 14:41:30 -0800600 return (android_media_AudioTrack_native_write_byte(env, thiz,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800601 (jbyteArray) javaAudioData,
602 offsetInShorts*2, sizeInShorts*2,
603 javaAudioFormat)
604 / 2);
605}
606
607
608// ----------------------------------------------------------------------------
609static jint android_media_AudioTrack_get_native_frame_count(JNIEnv *env, jobject thiz) {
Eric Laurent532bc1c2012-04-20 12:45:03 -0700610 sp<AudioTrack> lpTrack = getAudioTrack(env, thiz);
611 if (lpTrack == NULL) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800612 jniThrowException(env, "java/lang/IllegalStateException",
613 "Unable to retrieve AudioTrack pointer for frameCount()");
614 return AUDIOTRACK_ERROR;
615 }
Eric Laurent532bc1c2012-04-20 12:45:03 -0700616
617 return lpTrack->frameCount();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800618}
619
620
621// ----------------------------------------------------------------------------
Eric Laurent88e209d2009-07-07 07:10:45 -0700622static jint android_media_AudioTrack_set_playback_rate(JNIEnv *env, jobject thiz,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800623 jint sampleRateInHz) {
Eric Laurent532bc1c2012-04-20 12:45:03 -0700624 sp<AudioTrack> lpTrack = getAudioTrack(env, thiz);
625 if (lpTrack == NULL) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800626 jniThrowException(env, "java/lang/IllegalStateException",
627 "Unable to retrieve AudioTrack pointer for setSampleRate()");
Eric Laurent88e209d2009-07-07 07:10:45 -0700628 return AUDIOTRACK_ERROR;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800629 }
Eric Laurent532bc1c2012-04-20 12:45:03 -0700630 return android_media_translateErrorCode(lpTrack->setSampleRate(sampleRateInHz));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800631}
632
633
634// ----------------------------------------------------------------------------
635static jint android_media_AudioTrack_get_playback_rate(JNIEnv *env, jobject thiz) {
Eric Laurent532bc1c2012-04-20 12:45:03 -0700636 sp<AudioTrack> lpTrack = getAudioTrack(env, thiz);
637 if (lpTrack == NULL) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800638 jniThrowException(env, "java/lang/IllegalStateException",
639 "Unable to retrieve AudioTrack pointer for getSampleRate()");
640 return AUDIOTRACK_ERROR;
641 }
Eric Laurent532bc1c2012-04-20 12:45:03 -0700642 return (jint) lpTrack->getSampleRate();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800643}
644
645
646// ----------------------------------------------------------------------------
Glenn Kasten18db49a2012-03-12 16:29:55 -0700647static jint android_media_AudioTrack_set_marker_pos(JNIEnv *env, jobject thiz,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800648 jint markerPos) {
Eric Laurent532bc1c2012-04-20 12:45:03 -0700649 sp<AudioTrack> lpTrack = getAudioTrack(env, thiz);
650 if (lpTrack == NULL) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800651 jniThrowException(env, "java/lang/IllegalStateException",
652 "Unable to retrieve AudioTrack pointer for setMarkerPosition()");
653 return AUDIOTRACK_ERROR;
654 }
Eric Laurent532bc1c2012-04-20 12:45:03 -0700655 return android_media_translateErrorCode( lpTrack->setMarkerPosition(markerPos) );
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800656}
657
658
659// ----------------------------------------------------------------------------
660static jint android_media_AudioTrack_get_marker_pos(JNIEnv *env, jobject thiz) {
Eric Laurent532bc1c2012-04-20 12:45:03 -0700661 sp<AudioTrack> lpTrack = getAudioTrack(env, thiz);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800662 uint32_t markerPos = 0;
Glenn Kasten18db49a2012-03-12 16:29:55 -0700663
Eric Laurent532bc1c2012-04-20 12:45:03 -0700664 if (lpTrack == NULL) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800665 jniThrowException(env, "java/lang/IllegalStateException",
666 "Unable to retrieve AudioTrack pointer for getMarkerPosition()");
667 return AUDIOTRACK_ERROR;
668 }
Eric Laurent532bc1c2012-04-20 12:45:03 -0700669 lpTrack->getMarkerPosition(&markerPos);
670 return (jint)markerPos;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800671}
672
673
674// ----------------------------------------------------------------------------
675static jint android_media_AudioTrack_set_pos_update_period(JNIEnv *env, jobject thiz,
676 jint period) {
Eric Laurent532bc1c2012-04-20 12:45:03 -0700677 sp<AudioTrack> lpTrack = getAudioTrack(env, thiz);
678 if (lpTrack == NULL) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800679 jniThrowException(env, "java/lang/IllegalStateException",
680 "Unable to retrieve AudioTrack pointer for setPositionUpdatePeriod()");
681 return AUDIOTRACK_ERROR;
Glenn Kasten18db49a2012-03-12 16:29:55 -0700682 }
Eric Laurent532bc1c2012-04-20 12:45:03 -0700683 return android_media_translateErrorCode( lpTrack->setPositionUpdatePeriod(period) );
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800684}
685
686
687// ----------------------------------------------------------------------------
688static jint android_media_AudioTrack_get_pos_update_period(JNIEnv *env, jobject thiz) {
Eric Laurent532bc1c2012-04-20 12:45:03 -0700689 sp<AudioTrack> lpTrack = getAudioTrack(env, thiz);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800690 uint32_t period = 0;
Glenn Kasten18db49a2012-03-12 16:29:55 -0700691
Eric Laurent532bc1c2012-04-20 12:45:03 -0700692 if (lpTrack == NULL) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800693 jniThrowException(env, "java/lang/IllegalStateException",
694 "Unable to retrieve AudioTrack pointer for getPositionUpdatePeriod()");
695 return AUDIOTRACK_ERROR;
696 }
Eric Laurent532bc1c2012-04-20 12:45:03 -0700697 lpTrack->getPositionUpdatePeriod(&period);
698 return (jint)period;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800699}
700
701
702// ----------------------------------------------------------------------------
Glenn Kasten18db49a2012-03-12 16:29:55 -0700703static jint android_media_AudioTrack_set_position(JNIEnv *env, jobject thiz,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800704 jint position) {
Eric Laurent532bc1c2012-04-20 12:45:03 -0700705 sp<AudioTrack> lpTrack = getAudioTrack(env, thiz);
706 if (lpTrack == NULL) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800707 jniThrowException(env, "java/lang/IllegalStateException",
708 "Unable to retrieve AudioTrack pointer for setPosition()");
709 return AUDIOTRACK_ERROR;
710 }
Eric Laurent532bc1c2012-04-20 12:45:03 -0700711 return android_media_translateErrorCode( lpTrack->setPosition(position) );
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800712}
713
714
715// ----------------------------------------------------------------------------
716static jint android_media_AudioTrack_get_position(JNIEnv *env, jobject thiz) {
Eric Laurent532bc1c2012-04-20 12:45:03 -0700717 sp<AudioTrack> lpTrack = getAudioTrack(env, thiz);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800718 uint32_t position = 0;
Glenn Kasten18db49a2012-03-12 16:29:55 -0700719
Eric Laurent532bc1c2012-04-20 12:45:03 -0700720 if (lpTrack == NULL) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800721 jniThrowException(env, "java/lang/IllegalStateException",
722 "Unable to retrieve AudioTrack pointer for getPosition()");
723 return AUDIOTRACK_ERROR;
724 }
Eric Laurent532bc1c2012-04-20 12:45:03 -0700725 lpTrack->getPosition(&position);
726 return (jint)position;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800727}
728
729
730// ----------------------------------------------------------------------------
Oliver Woodmane64716a2013-06-26 12:43:36 +0100731static jint android_media_AudioTrack_get_latency(JNIEnv *env, jobject thiz) {
732 sp<AudioTrack> lpTrack = getAudioTrack(env, thiz);
733
734 if (lpTrack == NULL) {
735 jniThrowException(env, "java/lang/IllegalStateException",
736 "Unable to retrieve AudioTrack pointer for latency()");
737 return AUDIOTRACK_ERROR;
738 }
739 return (jint)lpTrack->latency();
740}
741
742
743// ----------------------------------------------------------------------------
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800744static jint android_media_AudioTrack_set_loop(JNIEnv *env, jobject thiz,
745 jint loopStart, jint loopEnd, jint loopCount) {
Eric Laurent532bc1c2012-04-20 12:45:03 -0700746 sp<AudioTrack> lpTrack = getAudioTrack(env, thiz);
747 if (lpTrack == NULL) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800748 jniThrowException(env, "java/lang/IllegalStateException",
749 "Unable to retrieve AudioTrack pointer for setLoop()");
750 return AUDIOTRACK_ERROR;
751 }
Eric Laurent532bc1c2012-04-20 12:45:03 -0700752 return android_media_translateErrorCode( lpTrack->setLoop(loopStart, loopEnd, loopCount) );
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800753}
754
755
756// ----------------------------------------------------------------------------
757static jint android_media_AudioTrack_reload(JNIEnv *env, jobject thiz) {
Eric Laurent532bc1c2012-04-20 12:45:03 -0700758 sp<AudioTrack> lpTrack = getAudioTrack(env, thiz);
759 if (lpTrack == NULL) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800760 jniThrowException(env, "java/lang/IllegalStateException",
761 "Unable to retrieve AudioTrack pointer for reload()");
762 return AUDIOTRACK_ERROR;
763 }
Eric Laurent532bc1c2012-04-20 12:45:03 -0700764 return android_media_translateErrorCode( lpTrack->reload() );
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800765}
766
767
768// ----------------------------------------------------------------------------
769static jint android_media_AudioTrack_get_output_sample_rate(JNIEnv *env, jobject thiz,
770 jint javaStreamType) {
Glenn Kasten85fbc872012-11-14 13:21:09 -0800771 uint32_t afSamplingRate;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800772 // convert the stream type from Java to native value
773 // FIXME: code duplication with android_media_AudioTrack_native_setup()
Dima Zavin24fc2fb2011-04-19 22:30:36 -0700774 audio_stream_type_t nativeStreamType;
Glenn Kasten29a09092012-01-16 14:37:12 -0800775 switch (javaStreamType) {
776 case AUDIO_STREAM_VOICE_CALL:
777 case AUDIO_STREAM_SYSTEM:
778 case AUDIO_STREAM_RING:
779 case AUDIO_STREAM_MUSIC:
780 case AUDIO_STREAM_ALARM:
781 case AUDIO_STREAM_NOTIFICATION:
782 case AUDIO_STREAM_BLUETOOTH_SCO:
783 case AUDIO_STREAM_DTMF:
784 nativeStreamType = (audio_stream_type_t) javaStreamType;
785 break;
786 default:
Dima Zavin24fc2fb2011-04-19 22:30:36 -0700787 nativeStreamType = AUDIO_STREAM_DEFAULT;
Glenn Kasten29a09092012-01-16 14:37:12 -0800788 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800789 }
790
791 if (AudioSystem::getOutputSamplingRate(&afSamplingRate, nativeStreamType) != NO_ERROR) {
Steve Block3762c312012-01-06 19:20:56 +0000792 ALOGE("AudioSystem::getOutputSamplingRate() for stream type %d failed in AudioTrack JNI",
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800793 nativeStreamType);
794 return DEFAULT_OUTPUT_SAMPLE_RATE;
795 } else {
796 return afSamplingRate;
797 }
798}
799
800
801// ----------------------------------------------------------------------------
802// returns the minimum required size for the successful creation of a streaming AudioTrack
803// returns -1 if there was an error querying the hardware.
804static jint android_media_AudioTrack_get_min_buff_size(JNIEnv *env, jobject thiz,
805 jint sampleRateInHertz, jint nbChannels, jint audioFormat) {
Chia-chi Yehc3308072010-08-19 17:14:36 +0800806
Glenn Kastenfd1e3df2012-11-13 15:21:06 -0800807 size_t frameCount = 0;
Dima Zavin24fc2fb2011-04-19 22:30:36 -0700808 if (AudioTrack::getMinFrameCount(&frameCount, AUDIO_STREAM_DEFAULT,
Chia-chi Yehc3308072010-08-19 17:14:36 +0800809 sampleRateInHertz) != NO_ERROR) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800810 return -1;
811 }
Chia-chi Yehc3308072010-08-19 17:14:36 +0800812 return frameCount * nbChannels * (audioFormat == javaAudioTrackFields.PCM16 ? 2 : 1);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800813}
814
Eric Laurent7070b362010-07-16 07:43:46 -0700815// ----------------------------------------------------------------------------
816static void
817android_media_AudioTrack_setAuxEffectSendLevel(JNIEnv *env, jobject thiz, jfloat level )
818{
Eric Laurent532bc1c2012-04-20 12:45:03 -0700819 sp<AudioTrack> lpTrack = getAudioTrack(env, thiz);
Eric Laurent7070b362010-07-16 07:43:46 -0700820 if (lpTrack == NULL ) {
821 jniThrowException(env, "java/lang/IllegalStateException",
822 "Unable to retrieve AudioTrack pointer for setAuxEffectSendLevel()");
823 return;
824 }
825
826 lpTrack->setAuxEffectSendLevel(level);
827}
828
829// ----------------------------------------------------------------------------
830static jint android_media_AudioTrack_attachAuxEffect(JNIEnv *env, jobject thiz,
831 jint effectId) {
Eric Laurent532bc1c2012-04-20 12:45:03 -0700832 sp<AudioTrack> lpTrack = getAudioTrack(env, thiz);
833 if (lpTrack == NULL) {
Eric Laurent7070b362010-07-16 07:43:46 -0700834 jniThrowException(env, "java/lang/IllegalStateException",
835 "Unable to retrieve AudioTrack pointer for attachAuxEffect()");
836 return AUDIOTRACK_ERROR;
837 }
Eric Laurent532bc1c2012-04-20 12:45:03 -0700838 return android_media_translateErrorCode( lpTrack->attachAuxEffect(effectId) );
Eric Laurent7070b362010-07-16 07:43:46 -0700839}
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800840
841// ----------------------------------------------------------------------------
842// ----------------------------------------------------------------------------
843static JNINativeMethod gMethods[] = {
844 // name, signature, funcPtr
845 {"native_start", "()V", (void *)android_media_AudioTrack_start},
846 {"native_stop", "()V", (void *)android_media_AudioTrack_stop},
847 {"native_pause", "()V", (void *)android_media_AudioTrack_pause},
848 {"native_flush", "()V", (void *)android_media_AudioTrack_flush},
Eric Laurent619346f2010-06-21 09:27:30 -0700849 {"native_setup", "(Ljava/lang/Object;IIIIII[I)I",
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800850 (void *)android_media_AudioTrack_native_setup},
851 {"native_finalize", "()V", (void *)android_media_AudioTrack_native_finalize},
852 {"native_release", "()V", (void *)android_media_AudioTrack_native_release},
Glenn Kasten00db1f52012-01-16 14:41:30 -0800853 {"native_write_byte", "([BIII)I", (void *)android_media_AudioTrack_native_write_byte},
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800854 {"native_write_short", "([SIII)I", (void *)android_media_AudioTrack_native_write_short},
855 {"native_setVolume", "(FF)V", (void *)android_media_AudioTrack_set_volume},
856 {"native_get_native_frame_count",
857 "()I", (void *)android_media_AudioTrack_get_native_frame_count},
858 {"native_set_playback_rate",
Eric Laurent88e209d2009-07-07 07:10:45 -0700859 "(I)I", (void *)android_media_AudioTrack_set_playback_rate},
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800860 {"native_get_playback_rate",
861 "()I", (void *)android_media_AudioTrack_get_playback_rate},
862 {"native_set_marker_pos","(I)I", (void *)android_media_AudioTrack_set_marker_pos},
863 {"native_get_marker_pos","()I", (void *)android_media_AudioTrack_get_marker_pos},
864 {"native_set_pos_update_period",
865 "(I)I", (void *)android_media_AudioTrack_set_pos_update_period},
866 {"native_get_pos_update_period",
867 "()I", (void *)android_media_AudioTrack_get_pos_update_period},
868 {"native_set_position", "(I)I", (void *)android_media_AudioTrack_set_position},
869 {"native_get_position", "()I", (void *)android_media_AudioTrack_get_position},
Oliver Woodmane64716a2013-06-26 12:43:36 +0100870 {"native_get_latency", "()I", (void *)android_media_AudioTrack_get_latency},
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800871 {"native_set_loop", "(III)I", (void *)android_media_AudioTrack_set_loop},
872 {"native_reload_static", "()I", (void *)android_media_AudioTrack_reload},
873 {"native_get_output_sample_rate",
874 "(I)I", (void *)android_media_AudioTrack_get_output_sample_rate},
875 {"native_get_min_buff_size",
876 "(III)I", (void *)android_media_AudioTrack_get_min_buff_size},
Eric Laurent7070b362010-07-16 07:43:46 -0700877 {"native_setAuxEffectSendLevel",
878 "(F)V", (void *)android_media_AudioTrack_setAuxEffectSendLevel},
879 {"native_attachAuxEffect",
880 "(I)I", (void *)android_media_AudioTrack_attachAuxEffect},
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800881};
882
883
884// field names found in android/media/AudioTrack.java
885#define JAVA_POSTEVENT_CALLBACK_NAME "postEventFromNative"
886#define JAVA_CONST_PCM16_NAME "ENCODING_PCM_16BIT"
887#define JAVA_CONST_PCM8_NAME "ENCODING_PCM_8BIT"
888#define JAVA_CONST_BUFFER_COUNT_NAME "BUFFER_COUNT"
889#define JAVA_CONST_STREAM_VOICE_CALL_NAME "STREAM_VOICE_CALL"
890#define JAVA_CONST_STREAM_SYSTEM_NAME "STREAM_SYSTEM"
891#define JAVA_CONST_STREAM_RING_NAME "STREAM_RING"
892#define JAVA_CONST_STREAM_MUSIC_NAME "STREAM_MUSIC"
893#define JAVA_CONST_STREAM_ALARM_NAME "STREAM_ALARM"
894#define JAVA_CONST_STREAM_NOTIFICATION_NAME "STREAM_NOTIFICATION"
895#define JAVA_CONST_STREAM_BLUETOOTH_SCO_NAME "STREAM_BLUETOOTH_SCO"
Eric Laurent83b36852009-07-28 07:49:22 -0700896#define JAVA_CONST_STREAM_DTMF_NAME "STREAM_DTMF"
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800897#define JAVA_NATIVETRACKINJAVAOBJ_FIELD_NAME "mNativeTrackInJavaObj"
898#define JAVA_JNIDATA_FIELD_NAME "mJniData"
899
900#define JAVA_AUDIOFORMAT_CLASS_NAME "android/media/AudioFormat"
901#define JAVA_AUDIOMANAGER_CLASS_NAME "android/media/AudioManager"
902
903// ----------------------------------------------------------------------------
904// preconditions:
905// theClass is valid
906bool android_media_getIntConstantFromClass(JNIEnv* pEnv, jclass theClass, const char* className,
907 const char* constName, int* constVal) {
908 jfieldID javaConst = NULL;
909 javaConst = pEnv->GetStaticFieldID(theClass, constName, "I");
910 if (javaConst != NULL) {
911 *constVal = pEnv->GetStaticIntField(theClass, javaConst);
912 return true;
913 } else {
Steve Block3762c312012-01-06 19:20:56 +0000914 ALOGE("Can't find %s.%s", className, constName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800915 return false;
916 }
917}
918
919
920// ----------------------------------------------------------------------------
921int register_android_media_AudioTrack(JNIEnv *env)
922{
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800923 javaAudioTrackFields.nativeTrackInJavaObj = NULL;
924 javaAudioTrackFields.postNativeEventInJava = NULL;
925
926 // Get the AudioTrack class
Brian Carlstrom46e18c112011-04-05 22:44:45 -0700927 jclass audioTrackClass = env->FindClass(kClassPathName);
928 if (audioTrackClass == NULL) {
Steve Block3762c312012-01-06 19:20:56 +0000929 ALOGE("Can't find %s", kClassPathName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800930 return -1;
931 }
932
933 // Get the postEvent method
934 javaAudioTrackFields.postNativeEventInJava = env->GetStaticMethodID(
Brian Carlstrom46e18c112011-04-05 22:44:45 -0700935 audioTrackClass,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800936 JAVA_POSTEVENT_CALLBACK_NAME, "(Ljava/lang/Object;IIILjava/lang/Object;)V");
937 if (javaAudioTrackFields.postNativeEventInJava == NULL) {
Steve Block3762c312012-01-06 19:20:56 +0000938 ALOGE("Can't find AudioTrack.%s", JAVA_POSTEVENT_CALLBACK_NAME);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800939 return -1;
940 }
941
942 // Get the variables fields
943 // nativeTrackInJavaObj
944 javaAudioTrackFields.nativeTrackInJavaObj = env->GetFieldID(
Brian Carlstrom46e18c112011-04-05 22:44:45 -0700945 audioTrackClass,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800946 JAVA_NATIVETRACKINJAVAOBJ_FIELD_NAME, "I");
947 if (javaAudioTrackFields.nativeTrackInJavaObj == NULL) {
Steve Block3762c312012-01-06 19:20:56 +0000948 ALOGE("Can't find AudioTrack.%s", JAVA_NATIVETRACKINJAVAOBJ_FIELD_NAME);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800949 return -1;
950 }
951 // jniData;
952 javaAudioTrackFields.jniData = env->GetFieldID(
Brian Carlstrom46e18c112011-04-05 22:44:45 -0700953 audioTrackClass,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800954 JAVA_JNIDATA_FIELD_NAME, "I");
955 if (javaAudioTrackFields.jniData == NULL) {
Steve Block3762c312012-01-06 19:20:56 +0000956 ALOGE("Can't find AudioTrack.%s", JAVA_JNIDATA_FIELD_NAME);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800957 return -1;
958 }
959
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800960 // Get the format constants from the AudioFormat class
961 jclass audioFormatClass = NULL;
962 audioFormatClass = env->FindClass(JAVA_AUDIOFORMAT_CLASS_NAME);
963 if (audioFormatClass == NULL) {
Steve Block3762c312012-01-06 19:20:56 +0000964 ALOGE("Can't find %s", JAVA_AUDIOFORMAT_CLASS_NAME);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800965 return -1;
966 }
Glenn Kasten18db49a2012-03-12 16:29:55 -0700967 if ( !android_media_getIntConstantFromClass(env, audioFormatClass,
968 JAVA_AUDIOFORMAT_CLASS_NAME,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800969 JAVA_CONST_PCM16_NAME, &(javaAudioTrackFields.PCM16))
Glenn Kasten18db49a2012-03-12 16:29:55 -0700970 || !android_media_getIntConstantFromClass(env, audioFormatClass,
971 JAVA_AUDIOFORMAT_CLASS_NAME,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800972 JAVA_CONST_PCM8_NAME, &(javaAudioTrackFields.PCM8)) ) {
Glenn Kasten18db49a2012-03-12 16:29:55 -0700973 // error log performed in android_media_getIntConstantFromClass()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800974 return -1;
975 }
Glenn Kasten18db49a2012-03-12 16:29:55 -0700976
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800977 return AndroidRuntime::registerNativeMethods(env, kClassPathName, gMethods, NELEM(gMethods));
978}
979
980
981// ----------------------------------------------------------------------------