blob: bd70dad7de21c1b30e5313fbbab301efbd9b5f55 [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
25#include "jni.h"
26#include "JNIHelp.h"
27#include "android_runtime/AndroidRuntime.h"
28
29#include "utils/Log.h"
30#include "media/AudioSystem.h"
31#include "media/AudioTrack.h"
32
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
52 int STREAM_VOICE_CALL; //... stream type constants
53 int STREAM_SYSTEM; //... stream type constants
54 int STREAM_RING; //... stream type constants
55 int STREAM_MUSIC; //... stream type constants
56 int STREAM_ALARM; //... stream type constants
57 int STREAM_NOTIFICATION; //... stream type constants
Eric Laurent83b36852009-07-28 07:49:22 -070058 int STREAM_BLUETOOTH_SCO; //... stream type constants
59 int STREAM_DTMF; //... stream type constants
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080060 int MODE_STREAM; //... memory mode
61 int MODE_STATIC; //... memory mode
Eric Laurent83b36852009-07-28 07:49:22 -070062 jfieldID nativeTrackInJavaObj; // stores in Java the native AudioTrack object
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080063 jfieldID jniData; // stores in Java additional resources used by the native AudioTrack
64};
65static fields_t javaAudioTrackFields;
66
67struct audiotrack_callback_cookie {
68 jclass audioTrack_class;
69 jobject audioTrack_ref;
70 };
71
72// ----------------------------------------------------------------------------
73class AudioTrackJniStorage {
74 public:
75 sp<MemoryHeapBase> mMemHeap;
76 sp<MemoryBase> mMemBase;
77 audiotrack_callback_cookie mCallbackData;
78 int mStreamType;
79
80 AudioTrackJniStorage() {
Jean-Michel Trivi8a149682009-07-15 18:31:11 -070081 mCallbackData.audioTrack_class = 0;
82 mCallbackData.audioTrack_ref = 0;
Dima Zavin24fc2fb2011-04-19 22:30:36 -070083 mStreamType = AUDIO_STREAM_DEFAULT;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080084 }
85
86 ~AudioTrackJniStorage() {
87 mMemBase.clear();
88 mMemHeap.clear();
89 }
90
91 bool allocSharedMem(int sizeInBytes) {
92 mMemHeap = new MemoryHeapBase(sizeInBytes, 0, "AudioTrack Heap Base");
93 if (mMemHeap->getHeapID() < 0) {
94 return false;
95 }
96 mMemBase = new MemoryBase(mMemHeap, 0, sizeInBytes);
97 return true;
98 }
99};
100
101// ----------------------------------------------------------------------------
102#define DEFAULT_OUTPUT_SAMPLE_RATE 44100
103
104#define AUDIOTRACK_SUCCESS 0
105#define AUDIOTRACK_ERROR -1
106#define AUDIOTRACK_ERROR_BAD_VALUE -2
107#define AUDIOTRACK_ERROR_INVALID_OPERATION -3
108#define AUDIOTRACK_ERROR_SETUP_AUDIOSYSTEM -16
Eric Laurenta553c252009-07-17 12:17:14 -0700109#define AUDIOTRACK_ERROR_SETUP_INVALIDCHANNELMASK -17
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800110#define AUDIOTRACK_ERROR_SETUP_INVALIDFORMAT -18
111#define AUDIOTRACK_ERROR_SETUP_INVALIDSTREAMTYPE -19
112#define AUDIOTRACK_ERROR_SETUP_NATIVEINITFAILED -20
113
114
115jint android_media_translateErrorCode(int code) {
116 switch(code) {
117 case NO_ERROR:
118 return AUDIOTRACK_SUCCESS;
119 case BAD_VALUE:
120 return AUDIOTRACK_ERROR_BAD_VALUE;
121 case INVALID_OPERATION:
122 return AUDIOTRACK_ERROR_INVALID_OPERATION;
123 default:
124 return AUDIOTRACK_ERROR;
125 }
126}
127
128
129// ----------------------------------------------------------------------------
130static void audioCallback(int event, void* user, void *info) {
131 if (event == AudioTrack::EVENT_MORE_DATA) {
132 // set size to 0 to signal we're not using the callback to write more data
133 AudioTrack::Buffer* pBuff = (AudioTrack::Buffer*)info;
134 pBuff->size = 0;
135
136 } else if (event == AudioTrack::EVENT_MARKER) {
137 audiotrack_callback_cookie *callbackInfo = (audiotrack_callback_cookie *)user;
138 JNIEnv *env = AndroidRuntime::getJNIEnv();
139 if (user && env) {
140 env->CallStaticVoidMethod(
141 callbackInfo->audioTrack_class,
142 javaAudioTrackFields.postNativeEventInJava,
143 callbackInfo->audioTrack_ref, event, 0,0, NULL);
144 if (env->ExceptionCheck()) {
145 env->ExceptionDescribe();
146 env->ExceptionClear();
147 }
148 }
149
150 } else if (event == AudioTrack::EVENT_NEW_POS) {
151 audiotrack_callback_cookie *callbackInfo = (audiotrack_callback_cookie *)user;
152 JNIEnv *env = AndroidRuntime::getJNIEnv();
153 if (user && env) {
154 env->CallStaticVoidMethod(
155 callbackInfo->audioTrack_class,
156 javaAudioTrackFields.postNativeEventInJava,
157 callbackInfo->audioTrack_ref, event, 0,0, NULL);
158 if (env->ExceptionCheck()) {
159 env->ExceptionDescribe();
160 env->ExceptionClear();
161 }
162 }
163 }
164}
165
166
167// ----------------------------------------------------------------------------
168static int
169android_media_AudioTrack_native_setup(JNIEnv *env, jobject thiz, jobject weak_this,
Eric Laurenta553c252009-07-17 12:17:14 -0700170 jint streamType, jint sampleRateInHertz, jint channels,
Eric Laurent619346f2010-06-21 09:27:30 -0700171 jint audioFormat, jint buffSizeInBytes, jint memoryMode, jintArray jSession)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800172{
Eric Laurenta553c252009-07-17 12:17:14 -0700173 LOGV("sampleRate=%d, audioFormat(from Java)=%d, channels=%x, buffSize=%d",
174 sampleRateInHertz, audioFormat, channels, buffSizeInBytes);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800175 int afSampleRate;
176 int afFrameCount;
177
178 if (AudioSystem::getOutputFrameCount(&afFrameCount, streamType) != NO_ERROR) {
179 LOGE("Error creating AudioTrack: Could not get AudioSystem frame count.");
180 return AUDIOTRACK_ERROR_SETUP_AUDIOSYSTEM;
181 }
182 if (AudioSystem::getOutputSamplingRate(&afSampleRate, streamType) != NO_ERROR) {
183 LOGE("Error creating AudioTrack: Could not get AudioSystem sampling rate.");
184 return AUDIOTRACK_ERROR_SETUP_AUDIOSYSTEM;
185 }
186
Dima Zavin24fc2fb2011-04-19 22:30:36 -0700187 if (!audio_is_output_channel(channels)) {
Eric Laurenta553c252009-07-17 12:17:14 -0700188 LOGE("Error creating AudioTrack: invalid channel mask.");
189 return AUDIOTRACK_ERROR_SETUP_INVALIDCHANNELMASK;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800190 }
Dima Zavin24fc2fb2011-04-19 22:30:36 -0700191 int nbChannels = popcount(channels);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800192
193 // check the stream type
Dima Zavin24fc2fb2011-04-19 22:30:36 -0700194 audio_stream_type_t atStreamType;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800195 if (streamType == javaAudioTrackFields.STREAM_VOICE_CALL) {
Dima Zavin24fc2fb2011-04-19 22:30:36 -0700196 atStreamType = AUDIO_STREAM_VOICE_CALL;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800197 } else if (streamType == javaAudioTrackFields.STREAM_SYSTEM) {
Dima Zavin24fc2fb2011-04-19 22:30:36 -0700198 atStreamType = AUDIO_STREAM_SYSTEM;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800199 } else if (streamType == javaAudioTrackFields.STREAM_RING) {
Dima Zavin24fc2fb2011-04-19 22:30:36 -0700200 atStreamType = AUDIO_STREAM_RING;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800201 } else if (streamType == javaAudioTrackFields.STREAM_MUSIC) {
Dima Zavin24fc2fb2011-04-19 22:30:36 -0700202 atStreamType = AUDIO_STREAM_MUSIC;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800203 } else if (streamType == javaAudioTrackFields.STREAM_ALARM) {
Dima Zavin24fc2fb2011-04-19 22:30:36 -0700204 atStreamType = AUDIO_STREAM_ALARM;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800205 } else if (streamType == javaAudioTrackFields.STREAM_NOTIFICATION) {
Dima Zavin24fc2fb2011-04-19 22:30:36 -0700206 atStreamType = AUDIO_STREAM_NOTIFICATION;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800207 } else if (streamType == javaAudioTrackFields.STREAM_BLUETOOTH_SCO) {
Dima Zavin24fc2fb2011-04-19 22:30:36 -0700208 atStreamType = AUDIO_STREAM_BLUETOOTH_SCO;
Eric Laurent83b36852009-07-28 07:49:22 -0700209 } else if (streamType == javaAudioTrackFields.STREAM_DTMF) {
Dima Zavin24fc2fb2011-04-19 22:30:36 -0700210 atStreamType = AUDIO_STREAM_DTMF;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800211 } else {
212 LOGE("Error creating AudioTrack: unknown stream type.");
213 return AUDIOTRACK_ERROR_SETUP_INVALIDSTREAMTYPE;
214 }
215
216 // check the format.
217 // This function was called from Java, so we compare the format against the Java constants
218 if ((audioFormat != javaAudioTrackFields.PCM16) && (audioFormat != javaAudioTrackFields.PCM8)) {
219 LOGE("Error creating AudioTrack: unsupported audio format.");
220 return AUDIOTRACK_ERROR_SETUP_INVALIDFORMAT;
221 }
222
223 // for the moment 8bitPCM in MODE_STATIC is not supported natively in the AudioTrack C++ class
224 // so we declare everything as 16bitPCM, the 8->16bit conversion for MODE_STATIC will be handled
225 // in android_media_AudioTrack_native_write()
226 if ((audioFormat == javaAudioTrackFields.PCM8)
227 && (memoryMode == javaAudioTrackFields.MODE_STATIC)) {
228 LOGV("android_media_AudioTrack_native_setup(): requesting MODE_STATIC for 8bit \
229 buff size of %dbytes, switching to 16bit, buff size of %dbytes",
230 buffSizeInBytes, 2*buffSizeInBytes);
231 audioFormat = javaAudioTrackFields.PCM16;
232 // we will need twice the memory to store the data
233 buffSizeInBytes *= 2;
234 }
235
236 // compute the frame count
237 int bytesPerSample = audioFormat == javaAudioTrackFields.PCM16 ? 2 : 1;
238 int format = audioFormat == javaAudioTrackFields.PCM16 ?
Dima Zavin24fc2fb2011-04-19 22:30:36 -0700239 AUDIO_FORMAT_PCM_16_BIT : AUDIO_FORMAT_PCM_8_BIT;
Eric Laurenta553c252009-07-17 12:17:14 -0700240 int frameCount = buffSizeInBytes / (nbChannels * bytesPerSample);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800241
242 AudioTrackJniStorage* lpJniStorage = new AudioTrackJniStorage();
243
244 // initialize the callback information:
245 // this data will be passed with every AudioTrack callback
246 jclass clazz = env->GetObjectClass(thiz);
247 if (clazz == NULL) {
248 LOGE("Can't find %s when setting up callback.", kClassPathName);
249 delete lpJniStorage;
250 return AUDIOTRACK_ERROR_SETUP_NATIVEINITFAILED;
251 }
252 lpJniStorage->mCallbackData.audioTrack_class = (jclass)env->NewGlobalRef(clazz);
253 // we use a weak reference so the AudioTrack object can be garbage collected.
254 lpJniStorage->mCallbackData.audioTrack_ref = env->NewGlobalRef(weak_this);
255
256 lpJniStorage->mStreamType = atStreamType;
Eric Laurent2fb43ef2010-09-24 12:03:36 -0700257
258 if (jSession == NULL) {
Eric Laurent619346f2010-06-21 09:27:30 -0700259 LOGE("Error creating AudioTrack: invalid session ID pointer");
260 delete lpJniStorage;
261 return AUDIOTRACK_ERROR;
262 }
263
Eric Laurent2fb43ef2010-09-24 12:03:36 -0700264 jint* nSession = (jint *) env->GetPrimitiveArrayCritical(jSession, NULL);
265 if (nSession == NULL) {
266 LOGE("Error creating AudioTrack: Error retrieving session id pointer");
267 delete lpJniStorage;
268 return AUDIOTRACK_ERROR;
269 }
270 int sessionId = nSession[0];
271 env->ReleasePrimitiveArrayCritical(jSession, nSession, 0);
272 nSession = NULL;
273
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800274 // create the native AudioTrack object
275 AudioTrack* lpTrack = new AudioTrack();
276 if (lpTrack == NULL) {
277 LOGE("Error creating uninitialized AudioTrack");
278 goto native_track_failure;
279 }
280
281 // initialize the native AudioTrack object
282 if (memoryMode == javaAudioTrackFields.MODE_STREAM) {
283
284 lpTrack->set(
285 atStreamType,// stream type
286 sampleRateInHertz,
287 format,// word length, PCM
Eric Laurenta553c252009-07-17 12:17:14 -0700288 channels,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800289 frameCount,
290 0,// flags
291 audioCallback, &(lpJniStorage->mCallbackData),//callback, callback data (user)
292 0,// notificationFrames == 0 since not using EVENT_MORE_DATA to feed the AudioTrack
293 0,// shared mem
Eric Laurent619346f2010-06-21 09:27:30 -0700294 true,// thread can call Java
Eric Laurent2fb43ef2010-09-24 12:03:36 -0700295 sessionId);// audio session ID
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800296
297 } else if (memoryMode == javaAudioTrackFields.MODE_STATIC) {
298 // AudioTrack is using shared memory
299
300 if (!lpJniStorage->allocSharedMem(buffSizeInBytes)) {
301 LOGE("Error creating AudioTrack in static mode: error creating mem heap base");
302 goto native_init_failure;
303 }
304
305 lpTrack->set(
306 atStreamType,// stream type
307 sampleRateInHertz,
308 format,// word length, PCM
Eric Laurenta553c252009-07-17 12:17:14 -0700309 channels,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800310 frameCount,
311 0,// flags
312 audioCallback, &(lpJniStorage->mCallbackData),//callback, callback data (user));
313 0,// notificationFrames == 0 since not using EVENT_MORE_DATA to feed the AudioTrack
314 lpJniStorage->mMemBase,// shared mem
Eric Laurent619346f2010-06-21 09:27:30 -0700315 true,// thread can call Java
Eric Laurent2fb43ef2010-09-24 12:03:36 -0700316 sessionId);// audio session ID
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800317 }
318
319 if (lpTrack->initCheck() != NO_ERROR) {
320 LOGE("Error initializing AudioTrack");
321 goto native_init_failure;
322 }
323
Eric Laurent2fb43ef2010-09-24 12:03:36 -0700324 nSession = (jint *) env->GetPrimitiveArrayCritical(jSession, NULL);
325 if (nSession == NULL) {
326 LOGE("Error creating AudioTrack: Error retrieving session id pointer");
327 goto native_init_failure;
328 }
Eric Laurent619346f2010-06-21 09:27:30 -0700329 // read the audio session ID back from AudioTrack in case we create a new session
330 nSession[0] = lpTrack->getSessionId();
Eric Laurent619346f2010-06-21 09:27:30 -0700331 env->ReleasePrimitiveArrayCritical(jSession, nSession, 0);
332 nSession = NULL;
333
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800334 // save our newly created C++ AudioTrack in the "nativeTrackInJavaObj" field
335 // of the Java object (in mNativeTrackInJavaObj)
336 env->SetIntField(thiz, javaAudioTrackFields.nativeTrackInJavaObj, (int)lpTrack);
337
338 // save the JNI resources so we can free them later
339 //LOGV("storing lpJniStorage: %x\n", (int)lpJniStorage);
340 env->SetIntField(thiz, javaAudioTrackFields.jniData, (int)lpJniStorage);
341
342 return AUDIOTRACK_SUCCESS;
343
344 // failures:
345native_init_failure:
346 delete lpTrack;
347 env->SetIntField(thiz, javaAudioTrackFields.nativeTrackInJavaObj, 0);
348
349native_track_failure:
Eric Laurent619346f2010-06-21 09:27:30 -0700350 if (nSession != NULL) {
351 env->ReleasePrimitiveArrayCritical(jSession, nSession, 0);
352 }
Jean-Michel Trivi8a149682009-07-15 18:31:11 -0700353 env->DeleteGlobalRef(lpJniStorage->mCallbackData.audioTrack_class);
354 env->DeleteGlobalRef(lpJniStorage->mCallbackData.audioTrack_ref);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800355 delete lpJniStorage;
356 env->SetIntField(thiz, javaAudioTrackFields.jniData, 0);
357 return AUDIOTRACK_ERROR_SETUP_NATIVEINITFAILED;
358
359}
360
361
362// ----------------------------------------------------------------------------
363static void
364android_media_AudioTrack_start(JNIEnv *env, jobject thiz)
365{
366 AudioTrack *lpTrack = (AudioTrack *)env->GetIntField(
367 thiz, javaAudioTrackFields.nativeTrackInJavaObj);
368 if (lpTrack == NULL ) {
369 jniThrowException(env, "java/lang/IllegalStateException",
370 "Unable to retrieve AudioTrack pointer for start()");
Eric Laurent7070b362010-07-16 07:43:46 -0700371 return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800372 }
Jean-Michel Trivi21dc0372009-05-08 16:06:34 -0700373
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800374 lpTrack->start();
375}
376
377
378// ----------------------------------------------------------------------------
379static void
380android_media_AudioTrack_stop(JNIEnv *env, jobject thiz)
381{
382 AudioTrack *lpTrack = (AudioTrack *)env->GetIntField(
383 thiz, javaAudioTrackFields.nativeTrackInJavaObj);
384 if (lpTrack == NULL ) {
385 jniThrowException(env, "java/lang/IllegalStateException",
386 "Unable to retrieve AudioTrack pointer for stop()");
Eric Laurent7070b362010-07-16 07:43:46 -0700387 return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800388 }
389
390 lpTrack->stop();
391}
392
393
394// ----------------------------------------------------------------------------
395static void
396android_media_AudioTrack_pause(JNIEnv *env, jobject thiz)
397{
398 AudioTrack *lpTrack = (AudioTrack *)env->GetIntField(
399 thiz, javaAudioTrackFields.nativeTrackInJavaObj);
400 if (lpTrack == NULL ) {
401 jniThrowException(env, "java/lang/IllegalStateException",
402 "Unable to retrieve AudioTrack pointer for pause()");
Eric Laurent7070b362010-07-16 07:43:46 -0700403 return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800404 }
405
406 lpTrack->pause();
407}
408
409
410// ----------------------------------------------------------------------------
411static void
412android_media_AudioTrack_flush(JNIEnv *env, jobject thiz)
413{
414 AudioTrack *lpTrack = (AudioTrack *)env->GetIntField(
415 thiz, javaAudioTrackFields.nativeTrackInJavaObj);
416 if (lpTrack == NULL ) {
417 jniThrowException(env, "java/lang/IllegalStateException",
418 "Unable to retrieve AudioTrack pointer for flush()");
Eric Laurent7070b362010-07-16 07:43:46 -0700419 return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800420 }
421
422 lpTrack->flush();
423}
424
425// ----------------------------------------------------------------------------
426static void
427android_media_AudioTrack_set_volume(JNIEnv *env, jobject thiz, jfloat leftVol, jfloat rightVol )
428{
429 AudioTrack *lpTrack = (AudioTrack *)env->GetIntField(
430 thiz, javaAudioTrackFields.nativeTrackInJavaObj);
431 if (lpTrack == NULL ) {
432 jniThrowException(env, "java/lang/IllegalStateException",
433 "Unable to retrieve AudioTrack pointer for setVolume()");
Eric Laurent7070b362010-07-16 07:43:46 -0700434 return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800435 }
436
437 lpTrack->setVolume(leftVol, rightVol);
438}
439
440// ----------------------------------------------------------------------------
441static void android_media_AudioTrack_native_finalize(JNIEnv *env, jobject thiz) {
442 //LOGV("android_media_AudioTrack_native_finalize jobject: %x\n", (int)thiz);
443
444 // delete the AudioTrack object
445 AudioTrack *lpTrack = (AudioTrack *)env->GetIntField(
446 thiz, javaAudioTrackFields.nativeTrackInJavaObj);
447 if (lpTrack) {
448 //LOGV("deleting lpTrack: %x\n", (int)lpTrack);
449 lpTrack->stop();
450 delete lpTrack;
451 }
452
453 // delete the JNI data
454 AudioTrackJniStorage* pJniStorage = (AudioTrackJniStorage *)env->GetIntField(
455 thiz, javaAudioTrackFields.jniData);
456 if (pJniStorage) {
Jean-Michel Trivi8a149682009-07-15 18:31:11 -0700457 // delete global refs created in native_setup
458 env->DeleteGlobalRef(pJniStorage->mCallbackData.audioTrack_class);
459 env->DeleteGlobalRef(pJniStorage->mCallbackData.audioTrack_ref);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800460 //LOGV("deleting pJniStorage: %x\n", (int)pJniStorage);
461 delete pJniStorage;
462 }
463}
464
465// ----------------------------------------------------------------------------
466static void android_media_AudioTrack_native_release(JNIEnv *env, jobject thiz) {
467
468 // do everything a call to finalize would
469 android_media_AudioTrack_native_finalize(env, thiz);
470 // + reset the native resources in the Java object so any attempt to access
471 // them after a call to release fails.
472 env->SetIntField(thiz, javaAudioTrackFields.nativeTrackInJavaObj, 0);
473 env->SetIntField(thiz, javaAudioTrackFields.jniData, 0);
474}
475
476
477// ----------------------------------------------------------------------------
Jean-Michel Trivi21dc0372009-05-08 16:06:34 -0700478jint writeToTrack(AudioTrack* pTrack, jint audioFormat, jbyte* data,
479 jint offsetInBytes, jint sizeInBytes) {
480 // give the data to the native AudioTrack object (the data starts at the offset)
481 ssize_t written = 0;
482 // regular write() or copy the data to the AudioTrack's shared memory?
483 if (pTrack->sharedBuffer() == 0) {
484 written = pTrack->write(data + offsetInBytes, sizeInBytes);
485 } else {
486 if (audioFormat == javaAudioTrackFields.PCM16) {
487 // writing to shared memory, check for capacity
488 if ((size_t)sizeInBytes > pTrack->sharedBuffer()->size()) {
489 sizeInBytes = pTrack->sharedBuffer()->size();
490 }
491 memcpy(pTrack->sharedBuffer()->pointer(), data + offsetInBytes, sizeInBytes);
492 written = sizeInBytes;
493 } else if (audioFormat == javaAudioTrackFields.PCM8) {
494 // data contains 8bit data we need to expand to 16bit before copying
495 // to the shared memory
496 // writing to shared memory, check for capacity,
497 // note that input data will occupy 2X the input space due to 8 to 16bit conversion
498 if (((size_t)sizeInBytes)*2 > pTrack->sharedBuffer()->size()) {
499 sizeInBytes = pTrack->sharedBuffer()->size() / 2;
500 }
501 int count = sizeInBytes;
502 int16_t *dst = (int16_t *)pTrack->sharedBuffer()->pointer();
503 const int8_t *src = (const int8_t *)(data + offsetInBytes);
504 while(count--) {
505 *dst++ = (int16_t)(*src++^0x80) << 8;
506 }
507 // even though we wrote 2*sizeInBytes, we only report sizeInBytes as written to hide
508 // the 8bit mixer restriction from the user of this function
509 written = sizeInBytes;
510 }
511 }
512 return written;
513
514}
515
516// ----------------------------------------------------------------------------
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800517static jint android_media_AudioTrack_native_write(JNIEnv *env, jobject thiz,
518 jbyteArray javaAudioData,
519 jint offsetInBytes, jint sizeInBytes,
520 jint javaAudioFormat) {
521 jbyte* cAudioData = NULL;
522 AudioTrack *lpTrack = NULL;
523 //LOGV("android_media_AudioTrack_native_write(offset=%d, sizeInBytes=%d) called",
524 // offsetInBytes, sizeInBytes);
525
526 // get the audio track to load with samples
527 lpTrack = (AudioTrack *)env->GetIntField(thiz, javaAudioTrackFields.nativeTrackInJavaObj);
528 if (lpTrack == NULL) {
529 jniThrowException(env, "java/lang/IllegalStateException",
530 "Unable to retrieve AudioTrack pointer for write()");
Eric Laurent7070b362010-07-16 07:43:46 -0700531 return 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800532 }
533
534 // get the pointer for the audio data from the java array
Eric Laurent421ddc02011-03-07 14:52:59 -0800535 // NOTE: We may use GetPrimitiveArrayCritical() when the JNI implementation changes in such
536 // a way that it becomes much more efficient. When doing so, we will have to prevent the
537 // AudioSystem callback to be called while in critical section (in case of media server
538 // process crash for instance)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800539 if (javaAudioData) {
Eric Laurent421ddc02011-03-07 14:52:59 -0800540 cAudioData = (jbyte *)env->GetByteArrayElements(javaAudioData, NULL);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800541 if (cAudioData == NULL) {
542 LOGE("Error retrieving source of audio data to play, can't play");
543 return 0; // out of memory or no data to load
544 }
545 } else {
546 LOGE("NULL java array of audio data to play, can't play");
547 return 0;
548 }
549
Jean-Michel Trivi21dc0372009-05-08 16:06:34 -0700550 jint written = writeToTrack(lpTrack, javaAudioFormat, cAudioData, offsetInBytes, sizeInBytes);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800551
Eric Laurent421ddc02011-03-07 14:52:59 -0800552 env->ReleaseByteArrayElements(javaAudioData, cAudioData, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800553
554 //LOGV("write wrote %d (tried %d) bytes in the native AudioTrack with offset %d",
555 // (int)written, (int)(sizeInBytes), (int)offsetInBytes);
Jean-Michel Trivi21dc0372009-05-08 16:06:34 -0700556 return written;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800557}
558
559
560// ----------------------------------------------------------------------------
561static jint android_media_AudioTrack_native_write_short(JNIEnv *env, jobject thiz,
562 jshortArray javaAudioData,
563 jint offsetInShorts, jint sizeInShorts,
564 jint javaAudioFormat) {
565 return (android_media_AudioTrack_native_write(env, thiz,
566 (jbyteArray) javaAudioData,
567 offsetInShorts*2, sizeInShorts*2,
568 javaAudioFormat)
569 / 2);
570}
571
572
573// ----------------------------------------------------------------------------
574static jint android_media_AudioTrack_get_native_frame_count(JNIEnv *env, jobject thiz) {
575 AudioTrack *lpTrack = (AudioTrack *)env->GetIntField(
576 thiz, javaAudioTrackFields.nativeTrackInJavaObj);
577
578 if (lpTrack) {
579 return lpTrack->frameCount();
580 } else {
581 jniThrowException(env, "java/lang/IllegalStateException",
582 "Unable to retrieve AudioTrack pointer for frameCount()");
583 return AUDIOTRACK_ERROR;
584 }
585}
586
587
588// ----------------------------------------------------------------------------
Eric Laurent88e209d2009-07-07 07:10:45 -0700589static jint android_media_AudioTrack_set_playback_rate(JNIEnv *env, jobject thiz,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800590 jint sampleRateInHz) {
591 AudioTrack *lpTrack = (AudioTrack *)env->GetIntField(
592 thiz, javaAudioTrackFields.nativeTrackInJavaObj);
593
594 if (lpTrack) {
Eric Laurent88e209d2009-07-07 07:10:45 -0700595 return android_media_translateErrorCode(lpTrack->setSampleRate(sampleRateInHz));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800596 } else {
597 jniThrowException(env, "java/lang/IllegalStateException",
598 "Unable to retrieve AudioTrack pointer for setSampleRate()");
Eric Laurent88e209d2009-07-07 07:10:45 -0700599 return AUDIOTRACK_ERROR;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800600 }
601}
602
603
604// ----------------------------------------------------------------------------
605static jint android_media_AudioTrack_get_playback_rate(JNIEnv *env, jobject thiz) {
606 AudioTrack *lpTrack = (AudioTrack *)env->GetIntField(
607 thiz, javaAudioTrackFields.nativeTrackInJavaObj);
608
609 if (lpTrack) {
610 return (jint) lpTrack->getSampleRate();
611 } else {
612 jniThrowException(env, "java/lang/IllegalStateException",
613 "Unable to retrieve AudioTrack pointer for getSampleRate()");
614 return AUDIOTRACK_ERROR;
615 }
616}
617
618
619// ----------------------------------------------------------------------------
620static jint android_media_AudioTrack_set_marker_pos(JNIEnv *env, jobject thiz,
621 jint markerPos) {
622
623 AudioTrack *lpTrack = (AudioTrack *)env->GetIntField(
624 thiz, javaAudioTrackFields.nativeTrackInJavaObj);
625
626 if (lpTrack) {
627 return android_media_translateErrorCode( lpTrack->setMarkerPosition(markerPos) );
628 } else {
629 jniThrowException(env, "java/lang/IllegalStateException",
630 "Unable to retrieve AudioTrack pointer for setMarkerPosition()");
631 return AUDIOTRACK_ERROR;
632 }
633}
634
635
636// ----------------------------------------------------------------------------
637static jint android_media_AudioTrack_get_marker_pos(JNIEnv *env, jobject thiz) {
638
639 AudioTrack *lpTrack = (AudioTrack *)env->GetIntField(
640 thiz, javaAudioTrackFields.nativeTrackInJavaObj);
641 uint32_t markerPos = 0;
642
643 if (lpTrack) {
644 lpTrack->getMarkerPosition(&markerPos);
645 return (jint)markerPos;
646 } else {
647 jniThrowException(env, "java/lang/IllegalStateException",
648 "Unable to retrieve AudioTrack pointer for getMarkerPosition()");
649 return AUDIOTRACK_ERROR;
650 }
651}
652
653
654// ----------------------------------------------------------------------------
655static jint android_media_AudioTrack_set_pos_update_period(JNIEnv *env, jobject thiz,
656 jint period) {
657
658 AudioTrack *lpTrack = (AudioTrack *)env->GetIntField(
659 thiz, javaAudioTrackFields.nativeTrackInJavaObj);
660
661 if (lpTrack) {
662 return android_media_translateErrorCode( lpTrack->setPositionUpdatePeriod(period) );
663 } else {
664 jniThrowException(env, "java/lang/IllegalStateException",
665 "Unable to retrieve AudioTrack pointer for setPositionUpdatePeriod()");
666 return AUDIOTRACK_ERROR;
667 }
668}
669
670
671// ----------------------------------------------------------------------------
672static jint android_media_AudioTrack_get_pos_update_period(JNIEnv *env, jobject thiz) {
673
674 AudioTrack *lpTrack = (AudioTrack *)env->GetIntField(
675 thiz, javaAudioTrackFields.nativeTrackInJavaObj);
676 uint32_t period = 0;
677
678 if (lpTrack) {
679 lpTrack->getPositionUpdatePeriod(&period);
680 return (jint)period;
681 } else {
682 jniThrowException(env, "java/lang/IllegalStateException",
683 "Unable to retrieve AudioTrack pointer for getPositionUpdatePeriod()");
684 return AUDIOTRACK_ERROR;
685 }
686}
687
688
689// ----------------------------------------------------------------------------
690static jint android_media_AudioTrack_set_position(JNIEnv *env, jobject thiz,
691 jint position) {
692
693 AudioTrack *lpTrack = (AudioTrack *)env->GetIntField(
694 thiz, javaAudioTrackFields.nativeTrackInJavaObj);
695
696 if (lpTrack) {
697 return android_media_translateErrorCode( lpTrack->setPosition(position) );
698 } else {
699 jniThrowException(env, "java/lang/IllegalStateException",
700 "Unable to retrieve AudioTrack pointer for setPosition()");
701 return AUDIOTRACK_ERROR;
702 }
703}
704
705
706// ----------------------------------------------------------------------------
707static jint android_media_AudioTrack_get_position(JNIEnv *env, jobject thiz) {
708
709 AudioTrack *lpTrack = (AudioTrack *)env->GetIntField(
710 thiz, javaAudioTrackFields.nativeTrackInJavaObj);
711 uint32_t position = 0;
712
713 if (lpTrack) {
714 lpTrack->getPosition(&position);
715 return (jint)position;
716 } else {
717 jniThrowException(env, "java/lang/IllegalStateException",
718 "Unable to retrieve AudioTrack pointer for getPosition()");
719 return AUDIOTRACK_ERROR;
720 }
721}
722
723
724// ----------------------------------------------------------------------------
725static jint android_media_AudioTrack_set_loop(JNIEnv *env, jobject thiz,
726 jint loopStart, jint loopEnd, jint loopCount) {
727
728 AudioTrack *lpTrack = (AudioTrack *)env->GetIntField(
729 thiz, javaAudioTrackFields.nativeTrackInJavaObj);
730 if (lpTrack) {
731 return android_media_translateErrorCode( lpTrack->setLoop(loopStart, loopEnd, loopCount) );
732 } else {
733 jniThrowException(env, "java/lang/IllegalStateException",
734 "Unable to retrieve AudioTrack pointer for setLoop()");
735 return AUDIOTRACK_ERROR;
736 }
737}
738
739
740// ----------------------------------------------------------------------------
741static jint android_media_AudioTrack_reload(JNIEnv *env, jobject thiz) {
742
743 AudioTrack *lpTrack = (AudioTrack *)env->GetIntField(
744 thiz, javaAudioTrackFields.nativeTrackInJavaObj);
745 if (lpTrack) {
746 return android_media_translateErrorCode( lpTrack->reload() );
747 } else {
748 jniThrowException(env, "java/lang/IllegalStateException",
749 "Unable to retrieve AudioTrack pointer for reload()");
750 return AUDIOTRACK_ERROR;
751 }
752}
753
754
755// ----------------------------------------------------------------------------
756static jint android_media_AudioTrack_get_output_sample_rate(JNIEnv *env, jobject thiz,
757 jint javaStreamType) {
758 int afSamplingRate;
759 // convert the stream type from Java to native value
760 // FIXME: code duplication with android_media_AudioTrack_native_setup()
Dima Zavin24fc2fb2011-04-19 22:30:36 -0700761 audio_stream_type_t nativeStreamType;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800762 if (javaStreamType == javaAudioTrackFields.STREAM_VOICE_CALL) {
Dima Zavin24fc2fb2011-04-19 22:30:36 -0700763 nativeStreamType = AUDIO_STREAM_VOICE_CALL;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800764 } else if (javaStreamType == javaAudioTrackFields.STREAM_SYSTEM) {
Dima Zavin24fc2fb2011-04-19 22:30:36 -0700765 nativeStreamType = AUDIO_STREAM_SYSTEM;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800766 } else if (javaStreamType == javaAudioTrackFields.STREAM_RING) {
Dima Zavin24fc2fb2011-04-19 22:30:36 -0700767 nativeStreamType = AUDIO_STREAM_RING;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800768 } else if (javaStreamType == javaAudioTrackFields.STREAM_MUSIC) {
Dima Zavin24fc2fb2011-04-19 22:30:36 -0700769 nativeStreamType = AUDIO_STREAM_MUSIC;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800770 } else if (javaStreamType == javaAudioTrackFields.STREAM_ALARM) {
Dima Zavin24fc2fb2011-04-19 22:30:36 -0700771 nativeStreamType = AUDIO_STREAM_ALARM;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800772 } else if (javaStreamType == javaAudioTrackFields.STREAM_NOTIFICATION) {
Dima Zavin24fc2fb2011-04-19 22:30:36 -0700773 nativeStreamType = AUDIO_STREAM_NOTIFICATION;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800774 } else if (javaStreamType == javaAudioTrackFields.STREAM_BLUETOOTH_SCO) {
Dima Zavin24fc2fb2011-04-19 22:30:36 -0700775 nativeStreamType = AUDIO_STREAM_BLUETOOTH_SCO;
Eric Laurent83b36852009-07-28 07:49:22 -0700776 } else if (javaStreamType == javaAudioTrackFields.STREAM_DTMF) {
Dima Zavin24fc2fb2011-04-19 22:30:36 -0700777 nativeStreamType = AUDIO_STREAM_DTMF;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800778 } else {
Dima Zavin24fc2fb2011-04-19 22:30:36 -0700779 nativeStreamType = AUDIO_STREAM_DEFAULT;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800780 }
781
782 if (AudioSystem::getOutputSamplingRate(&afSamplingRate, nativeStreamType) != NO_ERROR) {
783 LOGE("AudioSystem::getOutputSamplingRate() for stream type %d failed in AudioTrack JNI",
784 nativeStreamType);
785 return DEFAULT_OUTPUT_SAMPLE_RATE;
786 } else {
787 return afSamplingRate;
788 }
789}
790
791
792// ----------------------------------------------------------------------------
793// returns the minimum required size for the successful creation of a streaming AudioTrack
794// returns -1 if there was an error querying the hardware.
795static jint android_media_AudioTrack_get_min_buff_size(JNIEnv *env, jobject thiz,
796 jint sampleRateInHertz, jint nbChannels, jint audioFormat) {
Chia-chi Yehc3308072010-08-19 17:14:36 +0800797
798 int frameCount = 0;
Dima Zavin24fc2fb2011-04-19 22:30:36 -0700799 if (AudioTrack::getMinFrameCount(&frameCount, AUDIO_STREAM_DEFAULT,
Chia-chi Yehc3308072010-08-19 17:14:36 +0800800 sampleRateInHertz) != NO_ERROR) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800801 return -1;
802 }
Chia-chi Yehc3308072010-08-19 17:14:36 +0800803 return frameCount * nbChannels * (audioFormat == javaAudioTrackFields.PCM16 ? 2 : 1);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800804}
805
Eric Laurent7070b362010-07-16 07:43:46 -0700806// ----------------------------------------------------------------------------
807static void
808android_media_AudioTrack_setAuxEffectSendLevel(JNIEnv *env, jobject thiz, jfloat level )
809{
810 AudioTrack *lpTrack = (AudioTrack *)env->GetIntField(
811 thiz, javaAudioTrackFields.nativeTrackInJavaObj);
812 if (lpTrack == NULL ) {
813 jniThrowException(env, "java/lang/IllegalStateException",
814 "Unable to retrieve AudioTrack pointer for setAuxEffectSendLevel()");
815 return;
816 }
817
818 lpTrack->setAuxEffectSendLevel(level);
819}
820
821// ----------------------------------------------------------------------------
822static jint android_media_AudioTrack_attachAuxEffect(JNIEnv *env, jobject thiz,
823 jint effectId) {
824
825 AudioTrack *lpTrack = (AudioTrack *)env->GetIntField(
826 thiz, javaAudioTrackFields.nativeTrackInJavaObj);
827
828 if (lpTrack) {
829 return android_media_translateErrorCode( lpTrack->attachAuxEffect(effectId) );
830 } else {
831 jniThrowException(env, "java/lang/IllegalStateException",
832 "Unable to retrieve AudioTrack pointer for attachAuxEffect()");
833 return AUDIOTRACK_ERROR;
834 }
835}
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800836
837// ----------------------------------------------------------------------------
838// ----------------------------------------------------------------------------
839static JNINativeMethod gMethods[] = {
840 // name, signature, funcPtr
841 {"native_start", "()V", (void *)android_media_AudioTrack_start},
842 {"native_stop", "()V", (void *)android_media_AudioTrack_stop},
843 {"native_pause", "()V", (void *)android_media_AudioTrack_pause},
844 {"native_flush", "()V", (void *)android_media_AudioTrack_flush},
Eric Laurent619346f2010-06-21 09:27:30 -0700845 {"native_setup", "(Ljava/lang/Object;IIIIII[I)I",
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800846 (void *)android_media_AudioTrack_native_setup},
847 {"native_finalize", "()V", (void *)android_media_AudioTrack_native_finalize},
848 {"native_release", "()V", (void *)android_media_AudioTrack_native_release},
849 {"native_write_byte", "([BIII)I", (void *)android_media_AudioTrack_native_write},
850 {"native_write_short", "([SIII)I", (void *)android_media_AudioTrack_native_write_short},
851 {"native_setVolume", "(FF)V", (void *)android_media_AudioTrack_set_volume},
852 {"native_get_native_frame_count",
853 "()I", (void *)android_media_AudioTrack_get_native_frame_count},
854 {"native_set_playback_rate",
Eric Laurent88e209d2009-07-07 07:10:45 -0700855 "(I)I", (void *)android_media_AudioTrack_set_playback_rate},
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800856 {"native_get_playback_rate",
857 "()I", (void *)android_media_AudioTrack_get_playback_rate},
858 {"native_set_marker_pos","(I)I", (void *)android_media_AudioTrack_set_marker_pos},
859 {"native_get_marker_pos","()I", (void *)android_media_AudioTrack_get_marker_pos},
860 {"native_set_pos_update_period",
861 "(I)I", (void *)android_media_AudioTrack_set_pos_update_period},
862 {"native_get_pos_update_period",
863 "()I", (void *)android_media_AudioTrack_get_pos_update_period},
864 {"native_set_position", "(I)I", (void *)android_media_AudioTrack_set_position},
865 {"native_get_position", "()I", (void *)android_media_AudioTrack_get_position},
866 {"native_set_loop", "(III)I", (void *)android_media_AudioTrack_set_loop},
867 {"native_reload_static", "()I", (void *)android_media_AudioTrack_reload},
868 {"native_get_output_sample_rate",
869 "(I)I", (void *)android_media_AudioTrack_get_output_sample_rate},
870 {"native_get_min_buff_size",
871 "(III)I", (void *)android_media_AudioTrack_get_min_buff_size},
Eric Laurent7070b362010-07-16 07:43:46 -0700872 {"native_setAuxEffectSendLevel",
873 "(F)V", (void *)android_media_AudioTrack_setAuxEffectSendLevel},
874 {"native_attachAuxEffect",
875 "(I)I", (void *)android_media_AudioTrack_attachAuxEffect},
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800876};
877
878
879// field names found in android/media/AudioTrack.java
880#define JAVA_POSTEVENT_CALLBACK_NAME "postEventFromNative"
881#define JAVA_CONST_PCM16_NAME "ENCODING_PCM_16BIT"
882#define JAVA_CONST_PCM8_NAME "ENCODING_PCM_8BIT"
883#define JAVA_CONST_BUFFER_COUNT_NAME "BUFFER_COUNT"
884#define JAVA_CONST_STREAM_VOICE_CALL_NAME "STREAM_VOICE_CALL"
885#define JAVA_CONST_STREAM_SYSTEM_NAME "STREAM_SYSTEM"
886#define JAVA_CONST_STREAM_RING_NAME "STREAM_RING"
887#define JAVA_CONST_STREAM_MUSIC_NAME "STREAM_MUSIC"
888#define JAVA_CONST_STREAM_ALARM_NAME "STREAM_ALARM"
889#define JAVA_CONST_STREAM_NOTIFICATION_NAME "STREAM_NOTIFICATION"
890#define JAVA_CONST_STREAM_BLUETOOTH_SCO_NAME "STREAM_BLUETOOTH_SCO"
Eric Laurent83b36852009-07-28 07:49:22 -0700891#define JAVA_CONST_STREAM_DTMF_NAME "STREAM_DTMF"
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800892#define JAVA_CONST_MODE_STREAM_NAME "MODE_STREAM"
893#define JAVA_CONST_MODE_STATIC_NAME "MODE_STATIC"
894#define JAVA_NATIVETRACKINJAVAOBJ_FIELD_NAME "mNativeTrackInJavaObj"
895#define JAVA_JNIDATA_FIELD_NAME "mJniData"
896
897#define JAVA_AUDIOFORMAT_CLASS_NAME "android/media/AudioFormat"
898#define JAVA_AUDIOMANAGER_CLASS_NAME "android/media/AudioManager"
899
900// ----------------------------------------------------------------------------
901// preconditions:
902// theClass is valid
903bool android_media_getIntConstantFromClass(JNIEnv* pEnv, jclass theClass, const char* className,
904 const char* constName, int* constVal) {
905 jfieldID javaConst = NULL;
906 javaConst = pEnv->GetStaticFieldID(theClass, constName, "I");
907 if (javaConst != NULL) {
908 *constVal = pEnv->GetStaticIntField(theClass, javaConst);
909 return true;
910 } else {
911 LOGE("Can't find %s.%s", className, constName);
912 return false;
913 }
914}
915
916
917// ----------------------------------------------------------------------------
918int register_android_media_AudioTrack(JNIEnv *env)
919{
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800920 javaAudioTrackFields.nativeTrackInJavaObj = NULL;
921 javaAudioTrackFields.postNativeEventInJava = NULL;
922
923 // Get the AudioTrack class
Brian Carlstrom46e18c112011-04-05 22:44:45 -0700924 jclass audioTrackClass = env->FindClass(kClassPathName);
925 if (audioTrackClass == NULL) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800926 LOGE("Can't find %s", kClassPathName);
927 return -1;
928 }
929
930 // Get the postEvent method
931 javaAudioTrackFields.postNativeEventInJava = env->GetStaticMethodID(
Brian Carlstrom46e18c112011-04-05 22:44:45 -0700932 audioTrackClass,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800933 JAVA_POSTEVENT_CALLBACK_NAME, "(Ljava/lang/Object;IIILjava/lang/Object;)V");
934 if (javaAudioTrackFields.postNativeEventInJava == NULL) {
935 LOGE("Can't find AudioTrack.%s", JAVA_POSTEVENT_CALLBACK_NAME);
936 return -1;
937 }
938
939 // Get the variables fields
940 // nativeTrackInJavaObj
941 javaAudioTrackFields.nativeTrackInJavaObj = env->GetFieldID(
Brian Carlstrom46e18c112011-04-05 22:44:45 -0700942 audioTrackClass,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800943 JAVA_NATIVETRACKINJAVAOBJ_FIELD_NAME, "I");
944 if (javaAudioTrackFields.nativeTrackInJavaObj == NULL) {
945 LOGE("Can't find AudioTrack.%s", JAVA_NATIVETRACKINJAVAOBJ_FIELD_NAME);
946 return -1;
947 }
948 // jniData;
949 javaAudioTrackFields.jniData = env->GetFieldID(
Brian Carlstrom46e18c112011-04-05 22:44:45 -0700950 audioTrackClass,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800951 JAVA_JNIDATA_FIELD_NAME, "I");
952 if (javaAudioTrackFields.jniData == NULL) {
953 LOGE("Can't find AudioTrack.%s", JAVA_JNIDATA_FIELD_NAME);
954 return -1;
955 }
956
957 // Get the memory mode constants
Brian Carlstrom46e18c112011-04-05 22:44:45 -0700958 if ( !android_media_getIntConstantFromClass(env, audioTrackClass,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800959 kClassPathName,
960 JAVA_CONST_MODE_STATIC_NAME, &(javaAudioTrackFields.MODE_STATIC))
Brian Carlstrom46e18c112011-04-05 22:44:45 -0700961 || !android_media_getIntConstantFromClass(env, audioTrackClass,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800962 kClassPathName,
963 JAVA_CONST_MODE_STREAM_NAME, &(javaAudioTrackFields.MODE_STREAM)) ) {
964 // error log performed in android_media_getIntConstantFromClass()
965 return -1;
966 }
967
968 // Get the format constants from the AudioFormat class
969 jclass audioFormatClass = NULL;
970 audioFormatClass = env->FindClass(JAVA_AUDIOFORMAT_CLASS_NAME);
971 if (audioFormatClass == NULL) {
972 LOGE("Can't find %s", JAVA_AUDIOFORMAT_CLASS_NAME);
973 return -1;
974 }
975 if ( !android_media_getIntConstantFromClass(env, audioFormatClass,
976 JAVA_AUDIOFORMAT_CLASS_NAME,
977 JAVA_CONST_PCM16_NAME, &(javaAudioTrackFields.PCM16))
978 || !android_media_getIntConstantFromClass(env, audioFormatClass,
979 JAVA_AUDIOFORMAT_CLASS_NAME,
980 JAVA_CONST_PCM8_NAME, &(javaAudioTrackFields.PCM8)) ) {
981 // error log performed in android_media_getIntConstantFromClass()
982 return -1;
983 }
984
985 // Get the stream types from the AudioManager class
986 jclass audioManagerClass = NULL;
987 audioManagerClass = env->FindClass(JAVA_AUDIOMANAGER_CLASS_NAME);
988 if (audioManagerClass == NULL) {
989 LOGE("Can't find %s", JAVA_AUDIOMANAGER_CLASS_NAME);
990 return -1;
991 }
992 if ( !android_media_getIntConstantFromClass(env, audioManagerClass,
993 JAVA_AUDIOMANAGER_CLASS_NAME,
994 JAVA_CONST_STREAM_VOICE_CALL_NAME, &(javaAudioTrackFields.STREAM_VOICE_CALL))
995 || !android_media_getIntConstantFromClass(env, audioManagerClass,
996 JAVA_AUDIOMANAGER_CLASS_NAME,
997 JAVA_CONST_STREAM_MUSIC_NAME, &(javaAudioTrackFields.STREAM_MUSIC))
998 || !android_media_getIntConstantFromClass(env, audioManagerClass,
999 JAVA_AUDIOMANAGER_CLASS_NAME,
1000 JAVA_CONST_STREAM_SYSTEM_NAME, &(javaAudioTrackFields.STREAM_SYSTEM))
1001 || !android_media_getIntConstantFromClass(env, audioManagerClass,
1002 JAVA_AUDIOMANAGER_CLASS_NAME,
1003 JAVA_CONST_STREAM_RING_NAME, &(javaAudioTrackFields.STREAM_RING))
1004 || !android_media_getIntConstantFromClass(env, audioManagerClass,
1005 JAVA_AUDIOMANAGER_CLASS_NAME,
1006 JAVA_CONST_STREAM_ALARM_NAME, &(javaAudioTrackFields.STREAM_ALARM))
1007 || !android_media_getIntConstantFromClass(env, audioManagerClass,
1008 JAVA_AUDIOMANAGER_CLASS_NAME,
1009 JAVA_CONST_STREAM_NOTIFICATION_NAME, &(javaAudioTrackFields.STREAM_NOTIFICATION))
1010 || !android_media_getIntConstantFromClass(env, audioManagerClass,
1011 JAVA_AUDIOMANAGER_CLASS_NAME,
Eric Laurent83b36852009-07-28 07:49:22 -07001012 JAVA_CONST_STREAM_BLUETOOTH_SCO_NAME, &(javaAudioTrackFields.STREAM_BLUETOOTH_SCO))
1013 || !android_media_getIntConstantFromClass(env, audioManagerClass,
1014 JAVA_AUDIOMANAGER_CLASS_NAME,
1015 JAVA_CONST_STREAM_DTMF_NAME, &(javaAudioTrackFields.STREAM_DTMF))) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001016 // error log performed in android_media_getIntConstantFromClass()
1017 return -1;
1018 }
1019
1020 return AndroidRuntime::registerNativeMethods(env, kClassPathName, gMethods, NELEM(gMethods));
1021}
1022
1023
1024// ----------------------------------------------------------------------------