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