blob: 942d33fadcc53fd8aeba27b9c8c4dfee8bc34d2b [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);
Glenn Kasten9b53db32013-07-10 14:13:39 -0700523 // for compatibility with earlier behavior of write(), return 0 in this case
524 if (written == (ssize_t) WOULD_BLOCK) {
525 written = 0;
526 }
Jean-Michel Trivi21dc0372009-05-08 16:06:34 -0700527 } else {
528 if (audioFormat == javaAudioTrackFields.PCM16) {
529 // writing to shared memory, check for capacity
Eric Laurent532bc1c2012-04-20 12:45:03 -0700530 if ((size_t)sizeInBytes > track->sharedBuffer()->size()) {
531 sizeInBytes = track->sharedBuffer()->size();
Jean-Michel Trivi21dc0372009-05-08 16:06:34 -0700532 }
Eric Laurent532bc1c2012-04-20 12:45:03 -0700533 memcpy(track->sharedBuffer()->pointer(), data + offsetInBytes, sizeInBytes);
Jean-Michel Trivi21dc0372009-05-08 16:06:34 -0700534 written = sizeInBytes;
535 } else if (audioFormat == javaAudioTrackFields.PCM8) {
536 // data contains 8bit data we need to expand to 16bit before copying
537 // to the shared memory
538 // writing to shared memory, check for capacity,
539 // note that input data will occupy 2X the input space due to 8 to 16bit conversion
Eric Laurent532bc1c2012-04-20 12:45:03 -0700540 if (((size_t)sizeInBytes)*2 > track->sharedBuffer()->size()) {
541 sizeInBytes = track->sharedBuffer()->size() / 2;
Jean-Michel Trivi21dc0372009-05-08 16:06:34 -0700542 }
543 int count = sizeInBytes;
Eric Laurent532bc1c2012-04-20 12:45:03 -0700544 int16_t *dst = (int16_t *)track->sharedBuffer()->pointer();
Jean-Michel Trivi21dc0372009-05-08 16:06:34 -0700545 const int8_t *src = (const int8_t *)(data + offsetInBytes);
Glenn Kasten18db49a2012-03-12 16:29:55 -0700546 while (count--) {
Jean-Michel Trivi21dc0372009-05-08 16:06:34 -0700547 *dst++ = (int16_t)(*src++^0x80) << 8;
548 }
549 // even though we wrote 2*sizeInBytes, we only report sizeInBytes as written to hide
550 // the 8bit mixer restriction from the user of this function
551 written = sizeInBytes;
552 }
553 }
554 return written;
555
556}
557
558// ----------------------------------------------------------------------------
Glenn Kasten00db1f52012-01-16 14:41:30 -0800559static jint android_media_AudioTrack_native_write_byte(JNIEnv *env, jobject thiz,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800560 jbyteArray javaAudioData,
561 jint offsetInBytes, jint sizeInBytes,
562 jint javaAudioFormat) {
Glenn Kasten00db1f52012-01-16 14:41:30 -0800563 //ALOGV("android_media_AudioTrack_native_write_byte(offset=%d, sizeInBytes=%d) called",
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800564 // offsetInBytes, sizeInBytes);
Eric Laurent532bc1c2012-04-20 12:45:03 -0700565 sp<AudioTrack> lpTrack = getAudioTrack(env, thiz);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800566 if (lpTrack == NULL) {
567 jniThrowException(env, "java/lang/IllegalStateException",
568 "Unable to retrieve AudioTrack pointer for write()");
Eric Laurent7070b362010-07-16 07:43:46 -0700569 return 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800570 }
571
572 // get the pointer for the audio data from the java array
Eric Laurent421ddc02011-03-07 14:52:59 -0800573 // NOTE: We may use GetPrimitiveArrayCritical() when the JNI implementation changes in such
574 // a way that it becomes much more efficient. When doing so, we will have to prevent the
575 // AudioSystem callback to be called while in critical section (in case of media server
576 // process crash for instance)
Eric Laurent532bc1c2012-04-20 12:45:03 -0700577 jbyte* cAudioData = NULL;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800578 if (javaAudioData) {
Eric Laurent421ddc02011-03-07 14:52:59 -0800579 cAudioData = (jbyte *)env->GetByteArrayElements(javaAudioData, NULL);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800580 if (cAudioData == NULL) {
Steve Block3762c312012-01-06 19:20:56 +0000581 ALOGE("Error retrieving source of audio data to play, can't play");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800582 return 0; // out of memory or no data to load
583 }
584 } else {
Steve Block3762c312012-01-06 19:20:56 +0000585 ALOGE("NULL java array of audio data to play, can't play");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800586 return 0;
587 }
588
Jean-Michel Trivi21dc0372009-05-08 16:06:34 -0700589 jint written = writeToTrack(lpTrack, javaAudioFormat, cAudioData, offsetInBytes, sizeInBytes);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800590
Eric Laurent421ddc02011-03-07 14:52:59 -0800591 env->ReleaseByteArrayElements(javaAudioData, cAudioData, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800592
Steve Block71f2cf12011-10-20 11:56:00 +0100593 //ALOGV("write wrote %d (tried %d) bytes in the native AudioTrack with offset %d",
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800594 // (int)written, (int)(sizeInBytes), (int)offsetInBytes);
Jean-Michel Trivi21dc0372009-05-08 16:06:34 -0700595 return written;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800596}
597
598
599// ----------------------------------------------------------------------------
600static jint android_media_AudioTrack_native_write_short(JNIEnv *env, jobject thiz,
601 jshortArray javaAudioData,
602 jint offsetInShorts, jint sizeInShorts,
603 jint javaAudioFormat) {
Glenn Kastenf7e0a3702013-07-10 14:03:03 -0700604 jint written = android_media_AudioTrack_native_write_byte(env, thiz,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800605 (jbyteArray) javaAudioData,
606 offsetInShorts*2, sizeInShorts*2,
Glenn Kastenf7e0a3702013-07-10 14:03:03 -0700607 javaAudioFormat);
608 if (written > 0) {
609 written /= 2;
610 }
611 return written;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800612}
613
614
615// ----------------------------------------------------------------------------
616static jint android_media_AudioTrack_get_native_frame_count(JNIEnv *env, jobject thiz) {
Eric Laurent532bc1c2012-04-20 12:45:03 -0700617 sp<AudioTrack> lpTrack = getAudioTrack(env, thiz);
618 if (lpTrack == NULL) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800619 jniThrowException(env, "java/lang/IllegalStateException",
620 "Unable to retrieve AudioTrack pointer for frameCount()");
621 return AUDIOTRACK_ERROR;
622 }
Eric Laurent532bc1c2012-04-20 12:45:03 -0700623
624 return lpTrack->frameCount();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800625}
626
627
628// ----------------------------------------------------------------------------
Eric Laurent88e209d2009-07-07 07:10:45 -0700629static jint android_media_AudioTrack_set_playback_rate(JNIEnv *env, jobject thiz,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800630 jint sampleRateInHz) {
Eric Laurent532bc1c2012-04-20 12:45:03 -0700631 sp<AudioTrack> lpTrack = getAudioTrack(env, thiz);
632 if (lpTrack == NULL) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800633 jniThrowException(env, "java/lang/IllegalStateException",
634 "Unable to retrieve AudioTrack pointer for setSampleRate()");
Eric Laurent88e209d2009-07-07 07:10:45 -0700635 return AUDIOTRACK_ERROR;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800636 }
Eric Laurent532bc1c2012-04-20 12:45:03 -0700637 return android_media_translateErrorCode(lpTrack->setSampleRate(sampleRateInHz));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800638}
639
640
641// ----------------------------------------------------------------------------
642static jint android_media_AudioTrack_get_playback_rate(JNIEnv *env, jobject thiz) {
Eric Laurent532bc1c2012-04-20 12:45:03 -0700643 sp<AudioTrack> lpTrack = getAudioTrack(env, thiz);
644 if (lpTrack == NULL) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800645 jniThrowException(env, "java/lang/IllegalStateException",
646 "Unable to retrieve AudioTrack pointer for getSampleRate()");
647 return AUDIOTRACK_ERROR;
648 }
Eric Laurent532bc1c2012-04-20 12:45:03 -0700649 return (jint) lpTrack->getSampleRate();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800650}
651
652
653// ----------------------------------------------------------------------------
Glenn Kasten18db49a2012-03-12 16:29:55 -0700654static jint android_media_AudioTrack_set_marker_pos(JNIEnv *env, jobject thiz,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800655 jint markerPos) {
Eric Laurent532bc1c2012-04-20 12:45:03 -0700656 sp<AudioTrack> lpTrack = getAudioTrack(env, thiz);
657 if (lpTrack == NULL) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800658 jniThrowException(env, "java/lang/IllegalStateException",
659 "Unable to retrieve AudioTrack pointer for setMarkerPosition()");
660 return AUDIOTRACK_ERROR;
661 }
Eric Laurent532bc1c2012-04-20 12:45:03 -0700662 return android_media_translateErrorCode( lpTrack->setMarkerPosition(markerPos) );
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800663}
664
665
666// ----------------------------------------------------------------------------
667static jint android_media_AudioTrack_get_marker_pos(JNIEnv *env, jobject thiz) {
Eric Laurent532bc1c2012-04-20 12:45:03 -0700668 sp<AudioTrack> lpTrack = getAudioTrack(env, thiz);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800669 uint32_t markerPos = 0;
Glenn Kasten18db49a2012-03-12 16:29:55 -0700670
Eric Laurent532bc1c2012-04-20 12:45:03 -0700671 if (lpTrack == NULL) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800672 jniThrowException(env, "java/lang/IllegalStateException",
673 "Unable to retrieve AudioTrack pointer for getMarkerPosition()");
674 return AUDIOTRACK_ERROR;
675 }
Eric Laurent532bc1c2012-04-20 12:45:03 -0700676 lpTrack->getMarkerPosition(&markerPos);
677 return (jint)markerPos;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800678}
679
680
681// ----------------------------------------------------------------------------
682static jint android_media_AudioTrack_set_pos_update_period(JNIEnv *env, jobject thiz,
683 jint period) {
Eric Laurent532bc1c2012-04-20 12:45:03 -0700684 sp<AudioTrack> lpTrack = getAudioTrack(env, thiz);
685 if (lpTrack == NULL) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800686 jniThrowException(env, "java/lang/IllegalStateException",
687 "Unable to retrieve AudioTrack pointer for setPositionUpdatePeriod()");
688 return AUDIOTRACK_ERROR;
Glenn Kasten18db49a2012-03-12 16:29:55 -0700689 }
Eric Laurent532bc1c2012-04-20 12:45:03 -0700690 return android_media_translateErrorCode( lpTrack->setPositionUpdatePeriod(period) );
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800691}
692
693
694// ----------------------------------------------------------------------------
695static jint android_media_AudioTrack_get_pos_update_period(JNIEnv *env, jobject thiz) {
Eric Laurent532bc1c2012-04-20 12:45:03 -0700696 sp<AudioTrack> lpTrack = getAudioTrack(env, thiz);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800697 uint32_t period = 0;
Glenn Kasten18db49a2012-03-12 16:29:55 -0700698
Eric Laurent532bc1c2012-04-20 12:45:03 -0700699 if (lpTrack == NULL) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800700 jniThrowException(env, "java/lang/IllegalStateException",
701 "Unable to retrieve AudioTrack pointer for getPositionUpdatePeriod()");
702 return AUDIOTRACK_ERROR;
703 }
Eric Laurent532bc1c2012-04-20 12:45:03 -0700704 lpTrack->getPositionUpdatePeriod(&period);
705 return (jint)period;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800706}
707
708
709// ----------------------------------------------------------------------------
Glenn Kasten18db49a2012-03-12 16:29:55 -0700710static jint android_media_AudioTrack_set_position(JNIEnv *env, jobject thiz,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800711 jint position) {
Eric Laurent532bc1c2012-04-20 12:45:03 -0700712 sp<AudioTrack> lpTrack = getAudioTrack(env, thiz);
713 if (lpTrack == NULL) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800714 jniThrowException(env, "java/lang/IllegalStateException",
715 "Unable to retrieve AudioTrack pointer for setPosition()");
716 return AUDIOTRACK_ERROR;
717 }
Eric Laurent532bc1c2012-04-20 12:45:03 -0700718 return android_media_translateErrorCode( lpTrack->setPosition(position) );
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800719}
720
721
722// ----------------------------------------------------------------------------
723static jint android_media_AudioTrack_get_position(JNIEnv *env, jobject thiz) {
Eric Laurent532bc1c2012-04-20 12:45:03 -0700724 sp<AudioTrack> lpTrack = getAudioTrack(env, thiz);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800725 uint32_t position = 0;
Glenn Kasten18db49a2012-03-12 16:29:55 -0700726
Eric Laurent532bc1c2012-04-20 12:45:03 -0700727 if (lpTrack == NULL) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800728 jniThrowException(env, "java/lang/IllegalStateException",
729 "Unable to retrieve AudioTrack pointer for getPosition()");
730 return AUDIOTRACK_ERROR;
731 }
Eric Laurent532bc1c2012-04-20 12:45:03 -0700732 lpTrack->getPosition(&position);
733 return (jint)position;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800734}
735
736
737// ----------------------------------------------------------------------------
Oliver Woodman61dcdf32013-06-26 12:43:36 +0100738static jint android_media_AudioTrack_get_latency(JNIEnv *env, jobject thiz) {
739 sp<AudioTrack> lpTrack = getAudioTrack(env, thiz);
740
741 if (lpTrack == NULL) {
742 jniThrowException(env, "java/lang/IllegalStateException",
743 "Unable to retrieve AudioTrack pointer for latency()");
744 return AUDIOTRACK_ERROR;
745 }
746 return (jint)lpTrack->latency();
747}
748
749
750// ----------------------------------------------------------------------------
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800751static jint android_media_AudioTrack_set_loop(JNIEnv *env, jobject thiz,
752 jint loopStart, jint loopEnd, jint loopCount) {
Eric Laurent532bc1c2012-04-20 12:45:03 -0700753 sp<AudioTrack> lpTrack = getAudioTrack(env, thiz);
754 if (lpTrack == NULL) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800755 jniThrowException(env, "java/lang/IllegalStateException",
756 "Unable to retrieve AudioTrack pointer for setLoop()");
757 return AUDIOTRACK_ERROR;
758 }
Eric Laurent532bc1c2012-04-20 12:45:03 -0700759 return android_media_translateErrorCode( lpTrack->setLoop(loopStart, loopEnd, loopCount) );
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800760}
761
762
763// ----------------------------------------------------------------------------
764static jint android_media_AudioTrack_reload(JNIEnv *env, jobject thiz) {
Eric Laurent532bc1c2012-04-20 12:45:03 -0700765 sp<AudioTrack> lpTrack = getAudioTrack(env, thiz);
766 if (lpTrack == NULL) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800767 jniThrowException(env, "java/lang/IllegalStateException",
768 "Unable to retrieve AudioTrack pointer for reload()");
769 return AUDIOTRACK_ERROR;
770 }
Eric Laurent532bc1c2012-04-20 12:45:03 -0700771 return android_media_translateErrorCode( lpTrack->reload() );
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800772}
773
774
775// ----------------------------------------------------------------------------
776static jint android_media_AudioTrack_get_output_sample_rate(JNIEnv *env, jobject thiz,
777 jint javaStreamType) {
Glenn Kasten85fbc872012-11-14 13:21:09 -0800778 uint32_t afSamplingRate;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800779 // convert the stream type from Java to native value
780 // FIXME: code duplication with android_media_AudioTrack_native_setup()
Dima Zavin24fc2fb2011-04-19 22:30:36 -0700781 audio_stream_type_t nativeStreamType;
Glenn Kasten29a09092012-01-16 14:37:12 -0800782 switch (javaStreamType) {
783 case AUDIO_STREAM_VOICE_CALL:
784 case AUDIO_STREAM_SYSTEM:
785 case AUDIO_STREAM_RING:
786 case AUDIO_STREAM_MUSIC:
787 case AUDIO_STREAM_ALARM:
788 case AUDIO_STREAM_NOTIFICATION:
789 case AUDIO_STREAM_BLUETOOTH_SCO:
790 case AUDIO_STREAM_DTMF:
791 nativeStreamType = (audio_stream_type_t) javaStreamType;
792 break;
793 default:
Dima Zavin24fc2fb2011-04-19 22:30:36 -0700794 nativeStreamType = AUDIO_STREAM_DEFAULT;
Glenn Kasten29a09092012-01-16 14:37:12 -0800795 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800796 }
797
798 if (AudioSystem::getOutputSamplingRate(&afSamplingRate, nativeStreamType) != NO_ERROR) {
Steve Block3762c312012-01-06 19:20:56 +0000799 ALOGE("AudioSystem::getOutputSamplingRate() for stream type %d failed in AudioTrack JNI",
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800800 nativeStreamType);
801 return DEFAULT_OUTPUT_SAMPLE_RATE;
802 } else {
803 return afSamplingRate;
804 }
805}
806
807
808// ----------------------------------------------------------------------------
809// returns the minimum required size for the successful creation of a streaming AudioTrack
810// returns -1 if there was an error querying the hardware.
811static jint android_media_AudioTrack_get_min_buff_size(JNIEnv *env, jobject thiz,
812 jint sampleRateInHertz, jint nbChannels, jint audioFormat) {
Chia-chi Yehc3308072010-08-19 17:14:36 +0800813
Glenn Kastenfd1e3df2012-11-13 15:21:06 -0800814 size_t frameCount = 0;
Dima Zavin24fc2fb2011-04-19 22:30:36 -0700815 if (AudioTrack::getMinFrameCount(&frameCount, AUDIO_STREAM_DEFAULT,
Chia-chi Yehc3308072010-08-19 17:14:36 +0800816 sampleRateInHertz) != NO_ERROR) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800817 return -1;
818 }
Chia-chi Yehc3308072010-08-19 17:14:36 +0800819 return frameCount * nbChannels * (audioFormat == javaAudioTrackFields.PCM16 ? 2 : 1);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800820}
821
Eric Laurent7070b362010-07-16 07:43:46 -0700822// ----------------------------------------------------------------------------
823static void
824android_media_AudioTrack_setAuxEffectSendLevel(JNIEnv *env, jobject thiz, jfloat level )
825{
Eric Laurent532bc1c2012-04-20 12:45:03 -0700826 sp<AudioTrack> lpTrack = getAudioTrack(env, thiz);
Eric Laurent7070b362010-07-16 07:43:46 -0700827 if (lpTrack == NULL ) {
828 jniThrowException(env, "java/lang/IllegalStateException",
829 "Unable to retrieve AudioTrack pointer for setAuxEffectSendLevel()");
830 return;
831 }
832
833 lpTrack->setAuxEffectSendLevel(level);
834}
835
836// ----------------------------------------------------------------------------
837static jint android_media_AudioTrack_attachAuxEffect(JNIEnv *env, jobject thiz,
838 jint effectId) {
Eric Laurent532bc1c2012-04-20 12:45:03 -0700839 sp<AudioTrack> lpTrack = getAudioTrack(env, thiz);
840 if (lpTrack == NULL) {
Eric Laurent7070b362010-07-16 07:43:46 -0700841 jniThrowException(env, "java/lang/IllegalStateException",
842 "Unable to retrieve AudioTrack pointer for attachAuxEffect()");
843 return AUDIOTRACK_ERROR;
844 }
Eric Laurent532bc1c2012-04-20 12:45:03 -0700845 return android_media_translateErrorCode( lpTrack->attachAuxEffect(effectId) );
Eric Laurent7070b362010-07-16 07:43:46 -0700846}
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800847
848// ----------------------------------------------------------------------------
849// ----------------------------------------------------------------------------
850static JNINativeMethod gMethods[] = {
851 // name, signature, funcPtr
852 {"native_start", "()V", (void *)android_media_AudioTrack_start},
853 {"native_stop", "()V", (void *)android_media_AudioTrack_stop},
854 {"native_pause", "()V", (void *)android_media_AudioTrack_pause},
855 {"native_flush", "()V", (void *)android_media_AudioTrack_flush},
Eric Laurent619346f2010-06-21 09:27:30 -0700856 {"native_setup", "(Ljava/lang/Object;IIIIII[I)I",
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800857 (void *)android_media_AudioTrack_native_setup},
858 {"native_finalize", "()V", (void *)android_media_AudioTrack_native_finalize},
859 {"native_release", "()V", (void *)android_media_AudioTrack_native_release},
Glenn Kasten00db1f52012-01-16 14:41:30 -0800860 {"native_write_byte", "([BIII)I", (void *)android_media_AudioTrack_native_write_byte},
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800861 {"native_write_short", "([SIII)I", (void *)android_media_AudioTrack_native_write_short},
862 {"native_setVolume", "(FF)V", (void *)android_media_AudioTrack_set_volume},
863 {"native_get_native_frame_count",
864 "()I", (void *)android_media_AudioTrack_get_native_frame_count},
865 {"native_set_playback_rate",
Eric Laurent88e209d2009-07-07 07:10:45 -0700866 "(I)I", (void *)android_media_AudioTrack_set_playback_rate},
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800867 {"native_get_playback_rate",
868 "()I", (void *)android_media_AudioTrack_get_playback_rate},
869 {"native_set_marker_pos","(I)I", (void *)android_media_AudioTrack_set_marker_pos},
870 {"native_get_marker_pos","()I", (void *)android_media_AudioTrack_get_marker_pos},
871 {"native_set_pos_update_period",
872 "(I)I", (void *)android_media_AudioTrack_set_pos_update_period},
873 {"native_get_pos_update_period",
874 "()I", (void *)android_media_AudioTrack_get_pos_update_period},
875 {"native_set_position", "(I)I", (void *)android_media_AudioTrack_set_position},
876 {"native_get_position", "()I", (void *)android_media_AudioTrack_get_position},
Oliver Woodman61dcdf32013-06-26 12:43:36 +0100877 {"native_get_latency", "()I", (void *)android_media_AudioTrack_get_latency},
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800878 {"native_set_loop", "(III)I", (void *)android_media_AudioTrack_set_loop},
879 {"native_reload_static", "()I", (void *)android_media_AudioTrack_reload},
880 {"native_get_output_sample_rate",
881 "(I)I", (void *)android_media_AudioTrack_get_output_sample_rate},
882 {"native_get_min_buff_size",
883 "(III)I", (void *)android_media_AudioTrack_get_min_buff_size},
Eric Laurent7070b362010-07-16 07:43:46 -0700884 {"native_setAuxEffectSendLevel",
885 "(F)V", (void *)android_media_AudioTrack_setAuxEffectSendLevel},
886 {"native_attachAuxEffect",
887 "(I)I", (void *)android_media_AudioTrack_attachAuxEffect},
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800888};
889
890
891// field names found in android/media/AudioTrack.java
892#define JAVA_POSTEVENT_CALLBACK_NAME "postEventFromNative"
893#define JAVA_CONST_PCM16_NAME "ENCODING_PCM_16BIT"
894#define JAVA_CONST_PCM8_NAME "ENCODING_PCM_8BIT"
895#define JAVA_CONST_BUFFER_COUNT_NAME "BUFFER_COUNT"
896#define JAVA_CONST_STREAM_VOICE_CALL_NAME "STREAM_VOICE_CALL"
897#define JAVA_CONST_STREAM_SYSTEM_NAME "STREAM_SYSTEM"
898#define JAVA_CONST_STREAM_RING_NAME "STREAM_RING"
899#define JAVA_CONST_STREAM_MUSIC_NAME "STREAM_MUSIC"
900#define JAVA_CONST_STREAM_ALARM_NAME "STREAM_ALARM"
901#define JAVA_CONST_STREAM_NOTIFICATION_NAME "STREAM_NOTIFICATION"
902#define JAVA_CONST_STREAM_BLUETOOTH_SCO_NAME "STREAM_BLUETOOTH_SCO"
Eric Laurent83b36852009-07-28 07:49:22 -0700903#define JAVA_CONST_STREAM_DTMF_NAME "STREAM_DTMF"
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800904#define JAVA_NATIVETRACKINJAVAOBJ_FIELD_NAME "mNativeTrackInJavaObj"
905#define JAVA_JNIDATA_FIELD_NAME "mJniData"
906
907#define JAVA_AUDIOFORMAT_CLASS_NAME "android/media/AudioFormat"
908#define JAVA_AUDIOMANAGER_CLASS_NAME "android/media/AudioManager"
909
910// ----------------------------------------------------------------------------
911// preconditions:
912// theClass is valid
913bool android_media_getIntConstantFromClass(JNIEnv* pEnv, jclass theClass, const char* className,
914 const char* constName, int* constVal) {
915 jfieldID javaConst = NULL;
916 javaConst = pEnv->GetStaticFieldID(theClass, constName, "I");
917 if (javaConst != NULL) {
918 *constVal = pEnv->GetStaticIntField(theClass, javaConst);
919 return true;
920 } else {
Steve Block3762c312012-01-06 19:20:56 +0000921 ALOGE("Can't find %s.%s", className, constName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800922 return false;
923 }
924}
925
926
927// ----------------------------------------------------------------------------
928int register_android_media_AudioTrack(JNIEnv *env)
929{
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800930 javaAudioTrackFields.nativeTrackInJavaObj = NULL;
931 javaAudioTrackFields.postNativeEventInJava = NULL;
932
933 // Get the AudioTrack class
Brian Carlstrom46e18c112011-04-05 22:44:45 -0700934 jclass audioTrackClass = env->FindClass(kClassPathName);
935 if (audioTrackClass == NULL) {
Steve Block3762c312012-01-06 19:20:56 +0000936 ALOGE("Can't find %s", kClassPathName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800937 return -1;
938 }
939
940 // Get the postEvent method
941 javaAudioTrackFields.postNativeEventInJava = env->GetStaticMethodID(
Brian Carlstrom46e18c112011-04-05 22:44:45 -0700942 audioTrackClass,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800943 JAVA_POSTEVENT_CALLBACK_NAME, "(Ljava/lang/Object;IIILjava/lang/Object;)V");
944 if (javaAudioTrackFields.postNativeEventInJava == NULL) {
Steve Block3762c312012-01-06 19:20:56 +0000945 ALOGE("Can't find AudioTrack.%s", JAVA_POSTEVENT_CALLBACK_NAME);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800946 return -1;
947 }
948
949 // Get the variables fields
950 // nativeTrackInJavaObj
951 javaAudioTrackFields.nativeTrackInJavaObj = env->GetFieldID(
Brian Carlstrom46e18c112011-04-05 22:44:45 -0700952 audioTrackClass,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800953 JAVA_NATIVETRACKINJAVAOBJ_FIELD_NAME, "I");
954 if (javaAudioTrackFields.nativeTrackInJavaObj == NULL) {
Steve Block3762c312012-01-06 19:20:56 +0000955 ALOGE("Can't find AudioTrack.%s", JAVA_NATIVETRACKINJAVAOBJ_FIELD_NAME);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800956 return -1;
957 }
958 // jniData;
959 javaAudioTrackFields.jniData = env->GetFieldID(
Brian Carlstrom46e18c112011-04-05 22:44:45 -0700960 audioTrackClass,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800961 JAVA_JNIDATA_FIELD_NAME, "I");
962 if (javaAudioTrackFields.jniData == NULL) {
Steve Block3762c312012-01-06 19:20:56 +0000963 ALOGE("Can't find AudioTrack.%s", JAVA_JNIDATA_FIELD_NAME);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800964 return -1;
965 }
966
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800967 // Get the format constants from the AudioFormat class
968 jclass audioFormatClass = NULL;
969 audioFormatClass = env->FindClass(JAVA_AUDIOFORMAT_CLASS_NAME);
970 if (audioFormatClass == NULL) {
Steve Block3762c312012-01-06 19:20:56 +0000971 ALOGE("Can't find %s", JAVA_AUDIOFORMAT_CLASS_NAME);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800972 return -1;
973 }
Glenn Kasten18db49a2012-03-12 16:29:55 -0700974 if ( !android_media_getIntConstantFromClass(env, audioFormatClass,
975 JAVA_AUDIOFORMAT_CLASS_NAME,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800976 JAVA_CONST_PCM16_NAME, &(javaAudioTrackFields.PCM16))
Glenn Kasten18db49a2012-03-12 16:29:55 -0700977 || !android_media_getIntConstantFromClass(env, audioFormatClass,
978 JAVA_AUDIOFORMAT_CLASS_NAME,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800979 JAVA_CONST_PCM8_NAME, &(javaAudioTrackFields.PCM8)) ) {
Glenn Kasten18db49a2012-03-12 16:29:55 -0700980 // error log performed in android_media_getIntConstantFromClass()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800981 return -1;
982 }
Glenn Kasten18db49a2012-03-12 16:29:55 -0700983
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800984 return AndroidRuntime::registerNativeMethods(env, kClassPathName, gMethods, NELEM(gMethods));
985}
986
987
988// ----------------------------------------------------------------------------