19066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project/*
29066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * Copyright (C) 2008 The Android Open Source Project
39066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project *
49066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * Licensed under the Apache License, Version 2.0 (the "License");
59066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * you may not use this file except in compliance with the License.
69066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * You may obtain a copy of the License at
79066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project *
89066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project *      http://www.apache.org/licenses/LICENSE-2.0
99066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project *
109066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * Unless required by applicable law or agreed to in writing, software
119066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * distributed under the License is distributed on an "AS IS" BASIS,
129066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
139066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * See the License for the specific language governing permissions and
149066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * limitations under the License.
159066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project */
169066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project//#define LOG_NDEBUG 0
179066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
189066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project#define LOG_TAG "AudioTrack-JNI"
199066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
209066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project#include <stdio.h>
219066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project#include <unistd.h>
229066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project#include <fcntl.h>
239066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project#include <math.h>
249066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
25c81d31c3f801ba3d559a22c27b926ace38a7ab49Glenn Kasten#include <jni.h>
26c81d31c3f801ba3d559a22c27b926ace38a7ab49Glenn Kasten#include <JNIHelp.h>
27c81d31c3f801ba3d559a22c27b926ace38a7ab49Glenn Kasten#include <android_runtime/AndroidRuntime.h>
289066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
29c81d31c3f801ba3d559a22c27b926ace38a7ab49Glenn Kasten#include <utils/Log.h>
30c81d31c3f801ba3d559a22c27b926ace38a7ab49Glenn Kasten#include <media/AudioSystem.h>
31c81d31c3f801ba3d559a22c27b926ace38a7ab49Glenn Kasten#include <media/AudioTrack.h>
329066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
330795272aa226f4e965968a03daddc53ce30b7cdaMathias Agopian#include <binder/MemoryHeapBase.h>
340795272aa226f4e965968a03daddc53ce30b7cdaMathias Agopian#include <binder/MemoryBase.h>
359066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
3624fc2fb1c541e954b83fd31ea9f786a5e9b45501Dima Zavin#include <cutils/bitops.h>
3724fc2fb1c541e954b83fd31ea9f786a5e9b45501Dima Zavin
3834bb419e5946ab28112e9e27a4d1b3928d31e0e2Dima Zavin#include <system/audio.h>
399066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
409066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project// ----------------------------------------------------------------------------
419066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
429066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectusing namespace android;
439066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
449066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project// ----------------------------------------------------------------------------
459066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectstatic const char* const kClassPathName = "android/media/AudioTrack";
469066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
479066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectstruct fields_t {
489066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    // these fields provide access from C++ to the...
499066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    jmethodID postNativeEventInJava; //... event post callback method
509066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    int       PCM16;                 //...  format constants
519066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    int       PCM8;                  //...  format constants
529066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    int       MODE_STREAM;           //...  memory mode
539066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    int       MODE_STATIC;           //...  memory mode
5483b368505052f59616d29558e65e2250d01fd848Eric Laurent    jfieldID  nativeTrackInJavaObj;  // stores in Java the native AudioTrack object
559066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    jfieldID  jniData;      // stores in Java additional resources used by the native AudioTrack
569066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project};
579066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectstatic fields_t javaAudioTrackFields;
589066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
599066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectstruct audiotrack_callback_cookie {
609066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    jclass      audioTrack_class;
619066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    jobject     audioTrack_ref;
62532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent    bool        busy;
63532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent    Condition   cond;
64532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent};
659066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
669066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project// ----------------------------------------------------------------------------
679066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectclass AudioTrackJniStorage {
689066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public:
699066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        sp<MemoryHeapBase>         mMemHeap;
709066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        sp<MemoryBase>             mMemBase;
719066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        audiotrack_callback_cookie mCallbackData;
72bc1d77b6cbce23fbe25f7231651037ae195bc90eGlenn Kasten        audio_stream_type_t        mStreamType;
739066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
749066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    AudioTrackJniStorage() {
758a1496855cb81a6f2e9e8d51dd6b6ed595047a2aJean-Michel Trivi        mCallbackData.audioTrack_class = 0;
768a1496855cb81a6f2e9e8d51dd6b6ed595047a2aJean-Michel Trivi        mCallbackData.audioTrack_ref = 0;
7724fc2fb1c541e954b83fd31ea9f786a5e9b45501Dima Zavin        mStreamType = AUDIO_STREAM_DEFAULT;
789066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
799066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
809066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    ~AudioTrackJniStorage() {
819066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        mMemBase.clear();
829066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        mMemHeap.clear();
839066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
849066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
859066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    bool allocSharedMem(int sizeInBytes) {
869066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        mMemHeap = new MemoryHeapBase(sizeInBytes, 0, "AudioTrack Heap Base");
879066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        if (mMemHeap->getHeapID() < 0) {
889066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            return false;
899066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
909066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        mMemBase = new MemoryBase(mMemHeap, 0, sizeInBytes);
919066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return true;
929066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
939066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project};
949066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
95532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurentstatic Mutex sLock;
96532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurentstatic SortedVector <audiotrack_callback_cookie *> sAudioTrackCallBackCookies;
97532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent
989066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project// ----------------------------------------------------------------------------
999066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project#define DEFAULT_OUTPUT_SAMPLE_RATE   44100
1009066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
1019066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project#define AUDIOTRACK_SUCCESS                         0
1029066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project#define AUDIOTRACK_ERROR                           -1
1039066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project#define AUDIOTRACK_ERROR_BAD_VALUE                 -2
1049066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project#define AUDIOTRACK_ERROR_INVALID_OPERATION         -3
1059066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project#define AUDIOTRACK_ERROR_SETUP_AUDIOSYSTEM         -16
106fb2ab9efc3805c81521afb9ff91a58ff5097a36eGlenn Kasten#define AUDIOTRACK_ERROR_SETUP_INVALIDCHANNELMASK  -17
1079066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project#define AUDIOTRACK_ERROR_SETUP_INVALIDFORMAT       -18
1089066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project#define AUDIOTRACK_ERROR_SETUP_INVALIDSTREAMTYPE   -19
1099066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project#define AUDIOTRACK_ERROR_SETUP_NATIVEINITFAILED    -20
1109066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
1119066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
1129066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectjint android_media_translateErrorCode(int code) {
11318db49a46259020387c40fff36d92edc1087a366Glenn Kasten    switch (code) {
1149066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    case NO_ERROR:
1159066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return AUDIOTRACK_SUCCESS;
1169066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    case BAD_VALUE:
1179066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return AUDIOTRACK_ERROR_BAD_VALUE;
1189066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    case INVALID_OPERATION:
1199066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return AUDIOTRACK_ERROR_INVALID_OPERATION;
1209066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    default:
1219066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return AUDIOTRACK_ERROR;
12218db49a46259020387c40fff36d92edc1087a366Glenn Kasten    }
1239066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
1249066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
1259066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
1269066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project// ----------------------------------------------------------------------------
127e46a86fcb6a0849fc05c4cade6cb94aad0325295Glenn Kastenstatic void audioCallback(int event, void* user, void *info) {
128532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent
129532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent    audiotrack_callback_cookie *callbackInfo = (audiotrack_callback_cookie *)user;
130532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent    {
131532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent        Mutex::Autolock l(sLock);
132532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent        if (sAudioTrackCallBackCookies.indexOf(callbackInfo) < 0) {
133532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent            return;
134532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent        }
135532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent        callbackInfo->busy = true;
136532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent    }
137532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent
1389066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    if (event == AudioTrack::EVENT_MORE_DATA) {
1399066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        // set size to 0 to signal we're not using the callback to write more data
1409066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        AudioTrack::Buffer* pBuff = (AudioTrack::Buffer*)info;
14118db49a46259020387c40fff36d92edc1087a366Glenn Kasten        pBuff->size = 0;
14218db49a46259020387c40fff36d92edc1087a366Glenn Kasten
1439066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    } else if (event == AudioTrack::EVENT_MARKER) {
1449066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        JNIEnv *env = AndroidRuntime::getJNIEnv();
1459066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        if (user && env) {
1469066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            env->CallStaticVoidMethod(
14718db49a46259020387c40fff36d92edc1087a366Glenn Kasten                callbackInfo->audioTrack_class,
1489066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                javaAudioTrackFields.postNativeEventInJava,
1499066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                callbackInfo->audioTrack_ref, event, 0,0, NULL);
1509066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            if (env->ExceptionCheck()) {
1519066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                env->ExceptionDescribe();
1529066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                env->ExceptionClear();
1539066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            }
1549066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
1559066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
1569066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    } else if (event == AudioTrack::EVENT_NEW_POS) {
1579066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        JNIEnv *env = AndroidRuntime::getJNIEnv();
1589066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        if (user && env) {
1599066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            env->CallStaticVoidMethod(
16018db49a46259020387c40fff36d92edc1087a366Glenn Kasten                callbackInfo->audioTrack_class,
1619066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                javaAudioTrackFields.postNativeEventInJava,
1629066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                callbackInfo->audioTrack_ref, event, 0,0, NULL);
1639066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            if (env->ExceptionCheck()) {
1649066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                env->ExceptionDescribe();
1659066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                env->ExceptionClear();
1669066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            }
1679066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
1689066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
169532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent    {
170532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent        Mutex::Autolock l(sLock);
171532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent        callbackInfo->busy = false;
172532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent        callbackInfo->cond.broadcast();
173532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent    }
1749066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
1759066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
1769066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
1779066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project// ----------------------------------------------------------------------------
178532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurentstatic sp<AudioTrack> getAudioTrack(JNIEnv* env, jobject thiz)
179532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent{
180532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent    Mutex::Autolock l(sLock);
181532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent    AudioTrack* const at =
182532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent            (AudioTrack*)env->GetIntField(thiz, javaAudioTrackFields.nativeTrackInJavaObj);
183532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent    return sp<AudioTrack>(at);
184532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent}
185532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent
186532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurentstatic sp<AudioTrack> setAudioTrack(JNIEnv* env, jobject thiz, const sp<AudioTrack>& at)
187532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent{
188532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent    Mutex::Autolock l(sLock);
189532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent    sp<AudioTrack> old =
190532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent            (AudioTrack*)env->GetIntField(thiz, javaAudioTrackFields.nativeTrackInJavaObj);
191532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent    if (at.get()) {
192532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent        at->incStrong(thiz);
193532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent    }
194532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent    if (old != 0) {
195532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent        old->decStrong(thiz);
196532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent    }
197532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent    env->SetIntField(thiz, javaAudioTrackFields.nativeTrackInJavaObj, (int)at.get());
198532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent    return old;
199532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent}
200532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent
201532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent// ----------------------------------------------------------------------------
2029066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectstatic int
2039066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectandroid_media_AudioTrack_native_setup(JNIEnv *env, jobject thiz, jobject weak_this,
204d9ae1c573e37e245c7f4f0480a8fa925e57bd49dJean-Michel Trivi        jint streamType, jint sampleRateInHertz, jint javaChannelMask,
205619346f902241736d933657a4fe10f10c50a1ba8Eric Laurent        jint audioFormat, jint buffSizeInBytes, jint memoryMode, jintArray jSession)
2069066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project{
20771f2cf116aab893e224056c38ab146bd1538dd3eSteve Block    ALOGV("sampleRate=%d, audioFormat(from Java)=%d, channel mask=%x, buffSize=%d",
208d9ae1c573e37e245c7f4f0480a8fa925e57bd49dJean-Michel Trivi        sampleRateInHertz, audioFormat, javaChannelMask, buffSizeInBytes);
2099066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    int afSampleRate;
2109066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    int afFrameCount;
2119066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
212bc1d77b6cbce23fbe25f7231651037ae195bc90eGlenn Kasten    if (AudioSystem::getOutputFrameCount(&afFrameCount, (audio_stream_type_t) streamType) != NO_ERROR) {
2133762c311729fe9f3af085c14c5c1fb471d994c03Steve Block        ALOGE("Error creating AudioTrack: Could not get AudioSystem frame count.");
2149066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return AUDIOTRACK_ERROR_SETUP_AUDIOSYSTEM;
2159066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
216bc1d77b6cbce23fbe25f7231651037ae195bc90eGlenn Kasten    if (AudioSystem::getOutputSamplingRate(&afSampleRate, (audio_stream_type_t) streamType) != NO_ERROR) {
2173762c311729fe9f3af085c14c5c1fb471d994c03Steve Block        ALOGE("Error creating AudioTrack: Could not get AudioSystem sampling rate.");
2189066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return AUDIOTRACK_ERROR_SETUP_AUDIOSYSTEM;
2199066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
2209066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
221d9ae1c573e37e245c7f4f0480a8fa925e57bd49dJean-Michel Trivi    // Java channel masks don't map directly to the native definition, but it's a simple shift
222d9ae1c573e37e245c7f4f0480a8fa925e57bd49dJean-Michel Trivi    // to skip the two deprecated channel configurations "default" and "mono".
223d9ae1c573e37e245c7f4f0480a8fa925e57bd49dJean-Michel Trivi    uint32_t nativeChannelMask = ((uint32_t)javaChannelMask) >> 2;
224d9ae1c573e37e245c7f4f0480a8fa925e57bd49dJean-Michel Trivi
225d9ae1c573e37e245c7f4f0480a8fa925e57bd49dJean-Michel Trivi    if (!audio_is_output_channel(nativeChannelMask)) {
2263762c311729fe9f3af085c14c5c1fb471d994c03Steve Block        ALOGE("Error creating AudioTrack: invalid channel mask.");
227a553c25b33c99b345cf1c8688f8df0ed8df14e5aEric Laurent        return AUDIOTRACK_ERROR_SETUP_INVALIDCHANNELMASK;
2289066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
229d9ae1c573e37e245c7f4f0480a8fa925e57bd49dJean-Michel Trivi
230d9ae1c573e37e245c7f4f0480a8fa925e57bd49dJean-Michel Trivi    int nbChannels = popcount(nativeChannelMask);
23118db49a46259020387c40fff36d92edc1087a366Glenn Kasten
2329066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    // check the stream type
23324fc2fb1c541e954b83fd31ea9f786a5e9b45501Dima Zavin    audio_stream_type_t atStreamType;
23429a0909c5f777230b9f34ef0703fed3cd4633833Glenn Kasten    switch (streamType) {
23529a0909c5f777230b9f34ef0703fed3cd4633833Glenn Kasten    case AUDIO_STREAM_VOICE_CALL:
23629a0909c5f777230b9f34ef0703fed3cd4633833Glenn Kasten    case AUDIO_STREAM_SYSTEM:
23729a0909c5f777230b9f34ef0703fed3cd4633833Glenn Kasten    case AUDIO_STREAM_RING:
23829a0909c5f777230b9f34ef0703fed3cd4633833Glenn Kasten    case AUDIO_STREAM_MUSIC:
23929a0909c5f777230b9f34ef0703fed3cd4633833Glenn Kasten    case AUDIO_STREAM_ALARM:
24029a0909c5f777230b9f34ef0703fed3cd4633833Glenn Kasten    case AUDIO_STREAM_NOTIFICATION:
24129a0909c5f777230b9f34ef0703fed3cd4633833Glenn Kasten    case AUDIO_STREAM_BLUETOOTH_SCO:
24229a0909c5f777230b9f34ef0703fed3cd4633833Glenn Kasten    case AUDIO_STREAM_DTMF:
24329a0909c5f777230b9f34ef0703fed3cd4633833Glenn Kasten        atStreamType = (audio_stream_type_t) streamType;
24429a0909c5f777230b9f34ef0703fed3cd4633833Glenn Kasten        break;
24529a0909c5f777230b9f34ef0703fed3cd4633833Glenn Kasten    default:
2463762c311729fe9f3af085c14c5c1fb471d994c03Steve Block        ALOGE("Error creating AudioTrack: unknown stream type.");
2479066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return AUDIOTRACK_ERROR_SETUP_INVALIDSTREAMTYPE;
2489066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
2499066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
2509066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    // check the format.
2519066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    // This function was called from Java, so we compare the format against the Java constants
2529066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    if ((audioFormat != javaAudioTrackFields.PCM16) && (audioFormat != javaAudioTrackFields.PCM8)) {
2533762c311729fe9f3af085c14c5c1fb471d994c03Steve Block        ALOGE("Error creating AudioTrack: unsupported audio format.");
2549066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return AUDIOTRACK_ERROR_SETUP_INVALIDFORMAT;
2559066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
2569066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
2579066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    // for the moment 8bitPCM in MODE_STATIC is not supported natively in the AudioTrack C++ class
2589066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    // so we declare everything as 16bitPCM, the 8->16bit conversion for MODE_STATIC will be handled
25900db1f52b1c12d3670a0419305aa7548f7932aa0Glenn Kasten    // in android_media_AudioTrack_native_write_byte()
26018db49a46259020387c40fff36d92edc1087a366Glenn Kasten    if ((audioFormat == javaAudioTrackFields.PCM8)
2619066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        && (memoryMode == javaAudioTrackFields.MODE_STATIC)) {
26271f2cf116aab893e224056c38ab146bd1538dd3eSteve Block        ALOGV("android_media_AudioTrack_native_setup(): requesting MODE_STATIC for 8bit \
2639066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            buff size of %dbytes, switching to 16bit, buff size of %dbytes",
2649066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            buffSizeInBytes, 2*buffSizeInBytes);
2659066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        audioFormat = javaAudioTrackFields.PCM16;
2669066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        // we will need twice the memory to store the data
2679066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        buffSizeInBytes *= 2;
2689066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
2699066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
2709066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    // compute the frame count
2719066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    int bytesPerSample = audioFormat == javaAudioTrackFields.PCM16 ? 2 : 1;
2721c5a89d4af193e3ce9be9992ffee0ab0a4d8da5aGlenn Kasten    audio_format_t format = audioFormat == javaAudioTrackFields.PCM16 ?
27324fc2fb1c541e954b83fd31ea9f786a5e9b45501Dima Zavin            AUDIO_FORMAT_PCM_16_BIT : AUDIO_FORMAT_PCM_8_BIT;
274a553c25b33c99b345cf1c8688f8df0ed8df14e5aEric Laurent    int frameCount = buffSizeInBytes / (nbChannels * bytesPerSample);
27518db49a46259020387c40fff36d92edc1087a366Glenn Kasten
2769066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    jclass clazz = env->GetObjectClass(thiz);
2779066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    if (clazz == NULL) {
2783762c311729fe9f3af085c14c5c1fb471d994c03Steve Block        ALOGE("Can't find %s when setting up callback.", kClassPathName);
2799066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return AUDIOTRACK_ERROR_SETUP_NATIVEINITFAILED;
2809066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
2812fb43ef8c0b922c1bd0d7cb6867e30d702d4bdb8Eric Laurent
2822fb43ef8c0b922c1bd0d7cb6867e30d702d4bdb8Eric Laurent    if (jSession == NULL) {
2833762c311729fe9f3af085c14c5c1fb471d994c03Steve Block        ALOGE("Error creating AudioTrack: invalid session ID pointer");
284619346f902241736d933657a4fe10f10c50a1ba8Eric Laurent        return AUDIOTRACK_ERROR;
285619346f902241736d933657a4fe10f10c50a1ba8Eric Laurent    }
286619346f902241736d933657a4fe10f10c50a1ba8Eric Laurent
2872fb43ef8c0b922c1bd0d7cb6867e30d702d4bdb8Eric Laurent    jint* nSession = (jint *) env->GetPrimitiveArrayCritical(jSession, NULL);
2882fb43ef8c0b922c1bd0d7cb6867e30d702d4bdb8Eric Laurent    if (nSession == NULL) {
2893762c311729fe9f3af085c14c5c1fb471d994c03Steve Block        ALOGE("Error creating AudioTrack: Error retrieving session id pointer");
2902fb43ef8c0b922c1bd0d7cb6867e30d702d4bdb8Eric Laurent        return AUDIOTRACK_ERROR;
2912fb43ef8c0b922c1bd0d7cb6867e30d702d4bdb8Eric Laurent    }
2922fb43ef8c0b922c1bd0d7cb6867e30d702d4bdb8Eric Laurent    int sessionId = nSession[0];
2932fb43ef8c0b922c1bd0d7cb6867e30d702d4bdb8Eric Laurent    env->ReleasePrimitiveArrayCritical(jSession, nSession, 0);
2942fb43ef8c0b922c1bd0d7cb6867e30d702d4bdb8Eric Laurent    nSession = NULL;
2952fb43ef8c0b922c1bd0d7cb6867e30d702d4bdb8Eric Laurent
2969066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    // create the native AudioTrack object
297532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent    sp<AudioTrack> lpTrack = new AudioTrack();
2989066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    if (lpTrack == NULL) {
2993762c311729fe9f3af085c14c5c1fb471d994c03Steve Block        ALOGE("Error creating uninitialized AudioTrack");
300532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent        return AUDIOTRACK_ERROR_SETUP_NATIVEINITFAILED;
3019066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
30218db49a46259020387c40fff36d92edc1087a366Glenn Kasten
303532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent    // initialize the callback information:
304532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent    // this data will be passed with every AudioTrack callback
305532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent    AudioTrackJniStorage* lpJniStorage = new AudioTrackJniStorage();
306532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent    lpJniStorage->mStreamType = atStreamType;
307532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent    lpJniStorage->mCallbackData.audioTrack_class = (jclass)env->NewGlobalRef(clazz);
308532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent    // we use a weak reference so the AudioTrack object can be garbage collected.
309532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent    lpJniStorage->mCallbackData.audioTrack_ref = env->NewGlobalRef(weak_this);
310532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent    lpJniStorage->mCallbackData.busy = false;
311532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent
3129066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    // initialize the native AudioTrack object
3139066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    if (memoryMode == javaAudioTrackFields.MODE_STREAM) {
3149066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
3159066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        lpTrack->set(
3169066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            atStreamType,// stream type
3179066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            sampleRateInHertz,
3189066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            format,// word length, PCM
319d9ae1c573e37e245c7f4f0480a8fa925e57bd49dJean-Michel Trivi            nativeChannelMask,
3209066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            frameCount,
32110536b657d306416dfec738c6538a1afcb7e078eEric Laurent            AUDIO_OUTPUT_FLAG_NONE,
3229066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            audioCallback, &(lpJniStorage->mCallbackData),//callback, callback data (user)
3239066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            0,// notificationFrames == 0 since not using EVENT_MORE_DATA to feed the AudioTrack
3249066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            0,// shared mem
325619346f902241736d933657a4fe10f10c50a1ba8Eric Laurent            true,// thread can call Java
3262fb43ef8c0b922c1bd0d7cb6867e30d702d4bdb8Eric Laurent            sessionId);// audio session ID
32718db49a46259020387c40fff36d92edc1087a366Glenn Kasten
3289066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    } else if (memoryMode == javaAudioTrackFields.MODE_STATIC) {
3299066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        // AudioTrack is using shared memory
33018db49a46259020387c40fff36d92edc1087a366Glenn Kasten
3319066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        if (!lpJniStorage->allocSharedMem(buffSizeInBytes)) {
3323762c311729fe9f3af085c14c5c1fb471d994c03Steve Block            ALOGE("Error creating AudioTrack in static mode: error creating mem heap base");
3339066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            goto native_init_failure;
3349066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
33518db49a46259020387c40fff36d92edc1087a366Glenn Kasten
3369066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        lpTrack->set(
3379066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            atStreamType,// stream type
3389066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            sampleRateInHertz,
3399066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            format,// word length, PCM
340d9ae1c573e37e245c7f4f0480a8fa925e57bd49dJean-Michel Trivi            nativeChannelMask,
3419066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            frameCount,
34210536b657d306416dfec738c6538a1afcb7e078eEric Laurent            AUDIO_OUTPUT_FLAG_NONE,
3439066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            audioCallback, &(lpJniStorage->mCallbackData),//callback, callback data (user));
34418db49a46259020387c40fff36d92edc1087a366Glenn Kasten            0,// notificationFrames == 0 since not using EVENT_MORE_DATA to feed the AudioTrack
3459066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            lpJniStorage->mMemBase,// shared mem
346619346f902241736d933657a4fe10f10c50a1ba8Eric Laurent            true,// thread can call Java
3472fb43ef8c0b922c1bd0d7cb6867e30d702d4bdb8Eric Laurent            sessionId);// audio session ID
3489066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
3499066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
3509066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    if (lpTrack->initCheck() != NO_ERROR) {
3513762c311729fe9f3af085c14c5c1fb471d994c03Steve Block        ALOGE("Error initializing AudioTrack");
3529066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        goto native_init_failure;
3539066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
3549066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
3552fb43ef8c0b922c1bd0d7cb6867e30d702d4bdb8Eric Laurent    nSession = (jint *) env->GetPrimitiveArrayCritical(jSession, NULL);
3562fb43ef8c0b922c1bd0d7cb6867e30d702d4bdb8Eric Laurent    if (nSession == NULL) {
3573762c311729fe9f3af085c14c5c1fb471d994c03Steve Block        ALOGE("Error creating AudioTrack: Error retrieving session id pointer");
3582fb43ef8c0b922c1bd0d7cb6867e30d702d4bdb8Eric Laurent        goto native_init_failure;
3592fb43ef8c0b922c1bd0d7cb6867e30d702d4bdb8Eric Laurent    }
360619346f902241736d933657a4fe10f10c50a1ba8Eric Laurent    // read the audio session ID back from AudioTrack in case we create a new session
361619346f902241736d933657a4fe10f10c50a1ba8Eric Laurent    nSession[0] = lpTrack->getSessionId();
362619346f902241736d933657a4fe10f10c50a1ba8Eric Laurent    env->ReleasePrimitiveArrayCritical(jSession, nSession, 0);
363619346f902241736d933657a4fe10f10c50a1ba8Eric Laurent    nSession = NULL;
364619346f902241736d933657a4fe10f10c50a1ba8Eric Laurent
365532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent    {   // scope for the lock
366532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent        Mutex::Autolock l(sLock);
367532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent        sAudioTrackCallBackCookies.add(&lpJniStorage->mCallbackData);
368532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent    }
36918db49a46259020387c40fff36d92edc1087a366Glenn Kasten    // save our newly created C++ AudioTrack in the "nativeTrackInJavaObj" field
3709066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    // of the Java object (in mNativeTrackInJavaObj)
371532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent    setAudioTrack(env, thiz, lpTrack);
37218db49a46259020387c40fff36d92edc1087a366Glenn Kasten
3739066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    // save the JNI resources so we can free them later
37471f2cf116aab893e224056c38ab146bd1538dd3eSteve Block    //ALOGV("storing lpJniStorage: %x\n", (int)lpJniStorage);
3759066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    env->SetIntField(thiz, javaAudioTrackFields.jniData, (int)lpJniStorage);
3769066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
3779066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    return AUDIOTRACK_SUCCESS;
37818db49a46259020387c40fff36d92edc1087a366Glenn Kasten
3799066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    // failures:
3809066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectnative_init_failure:
381619346f902241736d933657a4fe10f10c50a1ba8Eric Laurent    if (nSession != NULL) {
382619346f902241736d933657a4fe10f10c50a1ba8Eric Laurent        env->ReleasePrimitiveArrayCritical(jSession, nSession, 0);
383619346f902241736d933657a4fe10f10c50a1ba8Eric Laurent    }
3848a1496855cb81a6f2e9e8d51dd6b6ed595047a2aJean-Michel Trivi    env->DeleteGlobalRef(lpJniStorage->mCallbackData.audioTrack_class);
3858a1496855cb81a6f2e9e8d51dd6b6ed595047a2aJean-Michel Trivi    env->DeleteGlobalRef(lpJniStorage->mCallbackData.audioTrack_ref);
3869066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    delete lpJniStorage;
3879066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    env->SetIntField(thiz, javaAudioTrackFields.jniData, 0);
38818db49a46259020387c40fff36d92edc1087a366Glenn Kasten
389532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent    return AUDIOTRACK_ERROR_SETUP_NATIVEINITFAILED;
3909066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
3919066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
3929066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
3939066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project// ----------------------------------------------------------------------------
3949066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectstatic void
3959066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectandroid_media_AudioTrack_start(JNIEnv *env, jobject thiz)
3969066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project{
397532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent    sp<AudioTrack> lpTrack = getAudioTrack(env, thiz);
398532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent    if (lpTrack == NULL) {
3999066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        jniThrowException(env, "java/lang/IllegalStateException",
4009066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            "Unable to retrieve AudioTrack pointer for start()");
4017070b36549d511d6627538c73dfbab23fbae5b43Eric Laurent        return;
4029066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
40321dc037e7e7c51e33b5808320f47339ee3a2b103Jean-Michel Trivi
4049066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    lpTrack->start();
4059066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
4069066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
4079066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
4089066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project// ----------------------------------------------------------------------------
4099066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectstatic void
4109066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectandroid_media_AudioTrack_stop(JNIEnv *env, jobject thiz)
4119066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project{
412532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent    sp<AudioTrack> lpTrack = getAudioTrack(env, thiz);
413532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent    if (lpTrack == NULL) {
4149066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        jniThrowException(env, "java/lang/IllegalStateException",
4159066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            "Unable to retrieve AudioTrack pointer for stop()");
4167070b36549d511d6627538c73dfbab23fbae5b43Eric Laurent        return;
4179066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
4189066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
4199066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    lpTrack->stop();
4209066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
4219066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
4229066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
4239066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project// ----------------------------------------------------------------------------
4249066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectstatic void
4259066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectandroid_media_AudioTrack_pause(JNIEnv *env, jobject thiz)
4269066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project{
427532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent    sp<AudioTrack> lpTrack = getAudioTrack(env, thiz);
428532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent    if (lpTrack == NULL) {
4299066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        jniThrowException(env, "java/lang/IllegalStateException",
4309066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            "Unable to retrieve AudioTrack pointer for pause()");
4317070b36549d511d6627538c73dfbab23fbae5b43Eric Laurent        return;
4329066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
4339066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
4349066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    lpTrack->pause();
4359066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
4369066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
4379066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
4389066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project// ----------------------------------------------------------------------------
4399066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectstatic void
4409066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectandroid_media_AudioTrack_flush(JNIEnv *env, jobject thiz)
4419066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project{
442532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent    sp<AudioTrack> lpTrack = getAudioTrack(env, thiz);
443532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent    if (lpTrack == NULL) {
4449066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        jniThrowException(env, "java/lang/IllegalStateException",
4459066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            "Unable to retrieve AudioTrack pointer for flush()");
4467070b36549d511d6627538c73dfbab23fbae5b43Eric Laurent        return;
4479066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
4489066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
4499066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    lpTrack->flush();
4509066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
4519066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
4529066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project// ----------------------------------------------------------------------------
4539066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectstatic void
4549066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectandroid_media_AudioTrack_set_volume(JNIEnv *env, jobject thiz, jfloat leftVol, jfloat rightVol )
4559066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project{
456532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent    sp<AudioTrack> lpTrack = getAudioTrack(env, thiz);
457532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent    if (lpTrack == NULL) {
4589066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        jniThrowException(env, "java/lang/IllegalStateException",
4599066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            "Unable to retrieve AudioTrack pointer for setVolume()");
4607070b36549d511d6627538c73dfbab23fbae5b43Eric Laurent        return;
4619066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
4629066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
4639066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    lpTrack->setVolume(leftVol, rightVol);
4649066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
4659066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
4669066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project// ----------------------------------------------------------------------------
46718db49a46259020387c40fff36d92edc1087a366Glenn Kasten
468532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent#define CALLBACK_COND_WAIT_TIMEOUT_MS 1000
469532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurentstatic void android_media_AudioTrack_native_release(JNIEnv *env,  jobject thiz) {
470532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent    sp<AudioTrack> lpTrack = setAudioTrack(env, thiz, 0);
471532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent    if (lpTrack == NULL) {
472532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent        return;
4739066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
474532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent    //ALOGV("deleting lpTrack: %x\n", (int)lpTrack);
475532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent    lpTrack->stop();
47618db49a46259020387c40fff36d92edc1087a366Glenn Kasten
4779066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    // delete the JNI data
4789066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    AudioTrackJniStorage* pJniStorage = (AudioTrackJniStorage *)env->GetIntField(
4799066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        thiz, javaAudioTrackFields.jniData);
480532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent    // reset the native resources in the Java object so any attempt to access
481532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent    // them after a call to release fails.
482532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent    env->SetIntField(thiz, javaAudioTrackFields.jniData, 0);
483532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent
4849066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    if (pJniStorage) {
485532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent        Mutex::Autolock l(sLock);
486532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent        audiotrack_callback_cookie *lpCookie = &pJniStorage->mCallbackData;
48771f2cf116aab893e224056c38ab146bd1538dd3eSteve Block        //ALOGV("deleting pJniStorage: %x\n", (int)pJniStorage);
488532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent        while (lpCookie->busy) {
489532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent            if (lpCookie->cond.waitRelative(sLock,
490532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent                                            milliseconds(CALLBACK_COND_WAIT_TIMEOUT_MS)) !=
491532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent                                                    NO_ERROR) {
492532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent                break;
493532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent            }
494532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent        }
495532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent        sAudioTrackCallBackCookies.remove(lpCookie);
496532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent        // delete global refs created in native_setup
497532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent        env->DeleteGlobalRef(lpCookie->audioTrack_class);
498532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent        env->DeleteGlobalRef(lpCookie->audioTrack_ref);
4999066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        delete pJniStorage;
5009066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
5019066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
5029066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
50318db49a46259020387c40fff36d92edc1087a366Glenn Kasten
504532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent// ----------------------------------------------------------------------------
505532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurentstatic void android_media_AudioTrack_native_finalize(JNIEnv *env,  jobject thiz) {
506532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent    //ALOGV("android_media_AudioTrack_native_finalize jobject: %x\n", (int)thiz);
507532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent    android_media_AudioTrack_native_release(env, thiz);
5089066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
5099066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
5109066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project// ----------------------------------------------------------------------------
511532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurentjint writeToTrack(const sp<AudioTrack>& track, jint audioFormat, jbyte* data,
51221dc037e7e7c51e33b5808320f47339ee3a2b103Jean-Michel Trivi                  jint offsetInBytes, jint sizeInBytes) {
51321dc037e7e7c51e33b5808320f47339ee3a2b103Jean-Michel Trivi    // give the data to the native AudioTrack object (the data starts at the offset)
51421dc037e7e7c51e33b5808320f47339ee3a2b103Jean-Michel Trivi    ssize_t written = 0;
51521dc037e7e7c51e33b5808320f47339ee3a2b103Jean-Michel Trivi    // regular write() or copy the data to the AudioTrack's shared memory?
516532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent    if (track->sharedBuffer() == 0) {
517532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent        written = track->write(data + offsetInBytes, sizeInBytes);
51821dc037e7e7c51e33b5808320f47339ee3a2b103Jean-Michel Trivi    } else {
51921dc037e7e7c51e33b5808320f47339ee3a2b103Jean-Michel Trivi        if (audioFormat == javaAudioTrackFields.PCM16) {
52021dc037e7e7c51e33b5808320f47339ee3a2b103Jean-Michel Trivi            // writing to shared memory, check for capacity
521532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent            if ((size_t)sizeInBytes > track->sharedBuffer()->size()) {
522532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent                sizeInBytes = track->sharedBuffer()->size();
52321dc037e7e7c51e33b5808320f47339ee3a2b103Jean-Michel Trivi            }
524532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent            memcpy(track->sharedBuffer()->pointer(), data + offsetInBytes, sizeInBytes);
52521dc037e7e7c51e33b5808320f47339ee3a2b103Jean-Michel Trivi            written = sizeInBytes;
52621dc037e7e7c51e33b5808320f47339ee3a2b103Jean-Michel Trivi        } else if (audioFormat == javaAudioTrackFields.PCM8) {
52721dc037e7e7c51e33b5808320f47339ee3a2b103Jean-Michel Trivi            // data contains 8bit data we need to expand to 16bit before copying
52821dc037e7e7c51e33b5808320f47339ee3a2b103Jean-Michel Trivi            // to the shared memory
52921dc037e7e7c51e33b5808320f47339ee3a2b103Jean-Michel Trivi            // writing to shared memory, check for capacity,
53021dc037e7e7c51e33b5808320f47339ee3a2b103Jean-Michel Trivi            // note that input data will occupy 2X the input space due to 8 to 16bit conversion
531532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent            if (((size_t)sizeInBytes)*2 > track->sharedBuffer()->size()) {
532532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent                sizeInBytes = track->sharedBuffer()->size() / 2;
53321dc037e7e7c51e33b5808320f47339ee3a2b103Jean-Michel Trivi            }
53421dc037e7e7c51e33b5808320f47339ee3a2b103Jean-Michel Trivi            int count = sizeInBytes;
535532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent            int16_t *dst = (int16_t *)track->sharedBuffer()->pointer();
53621dc037e7e7c51e33b5808320f47339ee3a2b103Jean-Michel Trivi            const int8_t *src = (const int8_t *)(data + offsetInBytes);
53718db49a46259020387c40fff36d92edc1087a366Glenn Kasten            while (count--) {
53821dc037e7e7c51e33b5808320f47339ee3a2b103Jean-Michel Trivi                *dst++ = (int16_t)(*src++^0x80) << 8;
53921dc037e7e7c51e33b5808320f47339ee3a2b103Jean-Michel Trivi            }
54021dc037e7e7c51e33b5808320f47339ee3a2b103Jean-Michel Trivi            // even though we wrote 2*sizeInBytes, we only report sizeInBytes as written to hide
54121dc037e7e7c51e33b5808320f47339ee3a2b103Jean-Michel Trivi            // the 8bit mixer restriction from the user of this function
54221dc037e7e7c51e33b5808320f47339ee3a2b103Jean-Michel Trivi            written = sizeInBytes;
54321dc037e7e7c51e33b5808320f47339ee3a2b103Jean-Michel Trivi        }
54421dc037e7e7c51e33b5808320f47339ee3a2b103Jean-Michel Trivi    }
54521dc037e7e7c51e33b5808320f47339ee3a2b103Jean-Michel Trivi    return written;
54621dc037e7e7c51e33b5808320f47339ee3a2b103Jean-Michel Trivi
54721dc037e7e7c51e33b5808320f47339ee3a2b103Jean-Michel Trivi}
54821dc037e7e7c51e33b5808320f47339ee3a2b103Jean-Michel Trivi
54921dc037e7e7c51e33b5808320f47339ee3a2b103Jean-Michel Trivi// ----------------------------------------------------------------------------
55000db1f52b1c12d3670a0419305aa7548f7932aa0Glenn Kastenstatic jint android_media_AudioTrack_native_write_byte(JNIEnv *env,  jobject thiz,
5519066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                                                  jbyteArray javaAudioData,
5529066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                                                  jint offsetInBytes, jint sizeInBytes,
5539066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                                                  jint javaAudioFormat) {
55400db1f52b1c12d3670a0419305aa7548f7932aa0Glenn Kasten    //ALOGV("android_media_AudioTrack_native_write_byte(offset=%d, sizeInBytes=%d) called",
5559066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    //    offsetInBytes, sizeInBytes);
556532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent    sp<AudioTrack> lpTrack = getAudioTrack(env, thiz);
5579066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    if (lpTrack == NULL) {
5589066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        jniThrowException(env, "java/lang/IllegalStateException",
5599066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            "Unable to retrieve AudioTrack pointer for write()");
5607070b36549d511d6627538c73dfbab23fbae5b43Eric Laurent        return 0;
5619066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
5629066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
5639066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    // get the pointer for the audio data from the java array
564421ddc014b31478ba0ef115e098d8780a35af331Eric Laurent    // NOTE: We may use GetPrimitiveArrayCritical() when the JNI implementation changes in such
565421ddc014b31478ba0ef115e098d8780a35af331Eric Laurent    // a way that it becomes much more efficient. When doing so, we will have to prevent the
566421ddc014b31478ba0ef115e098d8780a35af331Eric Laurent    // AudioSystem callback to be called while in critical section (in case of media server
567421ddc014b31478ba0ef115e098d8780a35af331Eric Laurent    // process crash for instance)
568532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent    jbyte* cAudioData = NULL;
5699066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    if (javaAudioData) {
570421ddc014b31478ba0ef115e098d8780a35af331Eric Laurent        cAudioData = (jbyte *)env->GetByteArrayElements(javaAudioData, NULL);
5719066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        if (cAudioData == NULL) {
5723762c311729fe9f3af085c14c5c1fb471d994c03Steve Block            ALOGE("Error retrieving source of audio data to play, can't play");
5739066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            return 0; // out of memory or no data to load
5749066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
5759066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    } else {
5763762c311729fe9f3af085c14c5c1fb471d994c03Steve Block        ALOGE("NULL java array of audio data to play, can't play");
5779066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return 0;
5789066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
5799066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
58021dc037e7e7c51e33b5808320f47339ee3a2b103Jean-Michel Trivi    jint written = writeToTrack(lpTrack, javaAudioFormat, cAudioData, offsetInBytes, sizeInBytes);
5819066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
582421ddc014b31478ba0ef115e098d8780a35af331Eric Laurent    env->ReleaseByteArrayElements(javaAudioData, cAudioData, 0);
5839066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
58471f2cf116aab893e224056c38ab146bd1538dd3eSteve Block    //ALOGV("write wrote %d (tried %d) bytes in the native AudioTrack with offset %d",
5859066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    //     (int)written, (int)(sizeInBytes), (int)offsetInBytes);
58621dc037e7e7c51e33b5808320f47339ee3a2b103Jean-Michel Trivi    return written;
5879066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
5889066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
5899066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
5909066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project// ----------------------------------------------------------------------------
5919066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectstatic jint android_media_AudioTrack_native_write_short(JNIEnv *env,  jobject thiz,
5929066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                                                  jshortArray javaAudioData,
5939066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                                                  jint offsetInShorts, jint sizeInShorts,
5949066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                                                  jint javaAudioFormat) {
59500db1f52b1c12d3670a0419305aa7548f7932aa0Glenn Kasten    return (android_media_AudioTrack_native_write_byte(env, thiz,
5969066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                                                 (jbyteArray) javaAudioData,
5979066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                                                 offsetInShorts*2, sizeInShorts*2,
5989066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                                                 javaAudioFormat)
5999066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            / 2);
6009066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
6019066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
6029066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
6039066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project// ----------------------------------------------------------------------------
6049066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectstatic jint android_media_AudioTrack_get_native_frame_count(JNIEnv *env,  jobject thiz) {
605532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent    sp<AudioTrack> lpTrack = getAudioTrack(env, thiz);
606532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent    if (lpTrack == NULL) {
6079066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        jniThrowException(env, "java/lang/IllegalStateException",
6089066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            "Unable to retrieve AudioTrack pointer for frameCount()");
6099066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return AUDIOTRACK_ERROR;
6109066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
611532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent
612532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent    return lpTrack->frameCount();
6139066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
6149066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
6159066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
6169066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project// ----------------------------------------------------------------------------
61788e209dcf8c2ebddda5c272f46d1bd5478bc639cEric Laurentstatic jint android_media_AudioTrack_set_playback_rate(JNIEnv *env,  jobject thiz,
6189066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        jint sampleRateInHz) {
619532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent    sp<AudioTrack> lpTrack = getAudioTrack(env, thiz);
620532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent    if (lpTrack == NULL) {
6219066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        jniThrowException(env, "java/lang/IllegalStateException",
6229066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            "Unable to retrieve AudioTrack pointer for setSampleRate()");
62388e209dcf8c2ebddda5c272f46d1bd5478bc639cEric Laurent        return AUDIOTRACK_ERROR;
6249066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
625532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent    return android_media_translateErrorCode(lpTrack->setSampleRate(sampleRateInHz));
6269066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
6279066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
6289066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
6299066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project// ----------------------------------------------------------------------------
6309066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectstatic jint android_media_AudioTrack_get_playback_rate(JNIEnv *env,  jobject thiz) {
631532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent    sp<AudioTrack> lpTrack = getAudioTrack(env, thiz);
632532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent    if (lpTrack == NULL) {
6339066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        jniThrowException(env, "java/lang/IllegalStateException",
6349066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            "Unable to retrieve AudioTrack pointer for getSampleRate()");
6359066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return AUDIOTRACK_ERROR;
6369066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
637532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent    return (jint) lpTrack->getSampleRate();
6389066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
6399066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
6409066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
6419066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project// ----------------------------------------------------------------------------
64218db49a46259020387c40fff36d92edc1087a366Glenn Kastenstatic jint android_media_AudioTrack_set_marker_pos(JNIEnv *env,  jobject thiz,
6439066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        jint markerPos) {
644532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent    sp<AudioTrack> lpTrack = getAudioTrack(env, thiz);
645532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent    if (lpTrack == NULL) {
6469066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        jniThrowException(env, "java/lang/IllegalStateException",
6479066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            "Unable to retrieve AudioTrack pointer for setMarkerPosition()");
6489066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return AUDIOTRACK_ERROR;
6499066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
650532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent    return android_media_translateErrorCode( lpTrack->setMarkerPosition(markerPos) );
6519066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
6529066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
6539066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
6549066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project// ----------------------------------------------------------------------------
6559066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectstatic jint android_media_AudioTrack_get_marker_pos(JNIEnv *env,  jobject thiz) {
656532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent    sp<AudioTrack> lpTrack = getAudioTrack(env, thiz);
6579066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    uint32_t markerPos = 0;
65818db49a46259020387c40fff36d92edc1087a366Glenn Kasten
659532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent    if (lpTrack == NULL) {
6609066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        jniThrowException(env, "java/lang/IllegalStateException",
6619066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            "Unable to retrieve AudioTrack pointer for getMarkerPosition()");
6629066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return AUDIOTRACK_ERROR;
6639066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
664532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent    lpTrack->getMarkerPosition(&markerPos);
665532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent    return (jint)markerPos;
6669066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
6679066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
6689066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
6699066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project// ----------------------------------------------------------------------------
6709066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectstatic jint android_media_AudioTrack_set_pos_update_period(JNIEnv *env,  jobject thiz,
6719066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        jint period) {
672532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent    sp<AudioTrack> lpTrack = getAudioTrack(env, thiz);
673532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent    if (lpTrack == NULL) {
6749066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        jniThrowException(env, "java/lang/IllegalStateException",
6759066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            "Unable to retrieve AudioTrack pointer for setPositionUpdatePeriod()");
6769066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return AUDIOTRACK_ERROR;
67718db49a46259020387c40fff36d92edc1087a366Glenn Kasten    }
678532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent    return android_media_translateErrorCode( lpTrack->setPositionUpdatePeriod(period) );
6799066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
6809066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
6819066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
6829066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project// ----------------------------------------------------------------------------
6839066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectstatic jint android_media_AudioTrack_get_pos_update_period(JNIEnv *env,  jobject thiz) {
684532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent    sp<AudioTrack> lpTrack = getAudioTrack(env, thiz);
6859066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    uint32_t period = 0;
68618db49a46259020387c40fff36d92edc1087a366Glenn Kasten
687532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent    if (lpTrack == NULL) {
6889066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        jniThrowException(env, "java/lang/IllegalStateException",
6899066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            "Unable to retrieve AudioTrack pointer for getPositionUpdatePeriod()");
6909066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return AUDIOTRACK_ERROR;
6919066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
692532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent    lpTrack->getPositionUpdatePeriod(&period);
693532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent    return (jint)period;
6949066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
6959066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
6969066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
6979066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project// ----------------------------------------------------------------------------
69818db49a46259020387c40fff36d92edc1087a366Glenn Kastenstatic jint android_media_AudioTrack_set_position(JNIEnv *env,  jobject thiz,
6999066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        jint position) {
700532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent    sp<AudioTrack> lpTrack = getAudioTrack(env, thiz);
701532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent    if (lpTrack == NULL) {
7029066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        jniThrowException(env, "java/lang/IllegalStateException",
7039066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            "Unable to retrieve AudioTrack pointer for setPosition()");
7049066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return AUDIOTRACK_ERROR;
7059066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
706532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent    return android_media_translateErrorCode( lpTrack->setPosition(position) );
7079066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
7089066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
7099066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
7109066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project// ----------------------------------------------------------------------------
7119066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectstatic jint android_media_AudioTrack_get_position(JNIEnv *env,  jobject thiz) {
712532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent    sp<AudioTrack> lpTrack = getAudioTrack(env, thiz);
7139066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    uint32_t position = 0;
71418db49a46259020387c40fff36d92edc1087a366Glenn Kasten
715532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent    if (lpTrack == NULL) {
7169066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        jniThrowException(env, "java/lang/IllegalStateException",
7179066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            "Unable to retrieve AudioTrack pointer for getPosition()");
7189066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return AUDIOTRACK_ERROR;
7199066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
720532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent    lpTrack->getPosition(&position);
721532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent    return (jint)position;
7229066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
7239066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
7249066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
7259066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project// ----------------------------------------------------------------------------
7269066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectstatic jint android_media_AudioTrack_set_loop(JNIEnv *env,  jobject thiz,
7279066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        jint loopStart, jint loopEnd, jint loopCount) {
728532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent    sp<AudioTrack> lpTrack = getAudioTrack(env, thiz);
729532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent    if (lpTrack == NULL) {
7309066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        jniThrowException(env, "java/lang/IllegalStateException",
7319066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            "Unable to retrieve AudioTrack pointer for setLoop()");
7329066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return AUDIOTRACK_ERROR;
7339066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
734532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent    return android_media_translateErrorCode( lpTrack->setLoop(loopStart, loopEnd, loopCount) );
7359066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
7369066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
7379066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
7389066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project// ----------------------------------------------------------------------------
7399066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectstatic jint android_media_AudioTrack_reload(JNIEnv *env,  jobject thiz) {
740532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent    sp<AudioTrack> lpTrack = getAudioTrack(env, thiz);
741532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent    if (lpTrack == NULL) {
7429066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        jniThrowException(env, "java/lang/IllegalStateException",
7439066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            "Unable to retrieve AudioTrack pointer for reload()");
7449066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return AUDIOTRACK_ERROR;
7459066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
746532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent    return android_media_translateErrorCode( lpTrack->reload() );
7479066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
7489066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
7499066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
7509066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project// ----------------------------------------------------------------------------
7519066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectstatic jint android_media_AudioTrack_get_output_sample_rate(JNIEnv *env,  jobject thiz,
7529066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        jint javaStreamType) {
7539066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    int afSamplingRate;
7549066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    // convert the stream type from Java to native value
7559066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    // FIXME: code duplication with android_media_AudioTrack_native_setup()
75624fc2fb1c541e954b83fd31ea9f786a5e9b45501Dima Zavin    audio_stream_type_t nativeStreamType;
75729a0909c5f777230b9f34ef0703fed3cd4633833Glenn Kasten    switch (javaStreamType) {
75829a0909c5f777230b9f34ef0703fed3cd4633833Glenn Kasten    case AUDIO_STREAM_VOICE_CALL:
75929a0909c5f777230b9f34ef0703fed3cd4633833Glenn Kasten    case AUDIO_STREAM_SYSTEM:
76029a0909c5f777230b9f34ef0703fed3cd4633833Glenn Kasten    case AUDIO_STREAM_RING:
76129a0909c5f777230b9f34ef0703fed3cd4633833Glenn Kasten    case AUDIO_STREAM_MUSIC:
76229a0909c5f777230b9f34ef0703fed3cd4633833Glenn Kasten    case AUDIO_STREAM_ALARM:
76329a0909c5f777230b9f34ef0703fed3cd4633833Glenn Kasten    case AUDIO_STREAM_NOTIFICATION:
76429a0909c5f777230b9f34ef0703fed3cd4633833Glenn Kasten    case AUDIO_STREAM_BLUETOOTH_SCO:
76529a0909c5f777230b9f34ef0703fed3cd4633833Glenn Kasten    case AUDIO_STREAM_DTMF:
76629a0909c5f777230b9f34ef0703fed3cd4633833Glenn Kasten        nativeStreamType = (audio_stream_type_t) javaStreamType;
76729a0909c5f777230b9f34ef0703fed3cd4633833Glenn Kasten        break;
76829a0909c5f777230b9f34ef0703fed3cd4633833Glenn Kasten    default:
76924fc2fb1c541e954b83fd31ea9f786a5e9b45501Dima Zavin        nativeStreamType = AUDIO_STREAM_DEFAULT;
77029a0909c5f777230b9f34ef0703fed3cd4633833Glenn Kasten        break;
7719066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
7729066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
7739066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    if (AudioSystem::getOutputSamplingRate(&afSamplingRate, nativeStreamType) != NO_ERROR) {
7743762c311729fe9f3af085c14c5c1fb471d994c03Steve Block        ALOGE("AudioSystem::getOutputSamplingRate() for stream type %d failed in AudioTrack JNI",
7759066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            nativeStreamType);
7769066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return DEFAULT_OUTPUT_SAMPLE_RATE;
7779066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    } else {
7789066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return afSamplingRate;
7799066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
7809066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
7819066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
7829066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
7839066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project// ----------------------------------------------------------------------------
7849066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project// returns the minimum required size for the successful creation of a streaming AudioTrack
7859066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project// returns -1 if there was an error querying the hardware.
7869066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectstatic jint android_media_AudioTrack_get_min_buff_size(JNIEnv *env,  jobject thiz,
7879066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    jint sampleRateInHertz, jint nbChannels, jint audioFormat) {
788c3308074cd6f06bbb35c1999b5260f938742a9e7Chia-chi Yeh
789c3308074cd6f06bbb35c1999b5260f938742a9e7Chia-chi Yeh    int frameCount = 0;
79024fc2fb1c541e954b83fd31ea9f786a5e9b45501Dima Zavin    if (AudioTrack::getMinFrameCount(&frameCount, AUDIO_STREAM_DEFAULT,
791c3308074cd6f06bbb35c1999b5260f938742a9e7Chia-chi Yeh            sampleRateInHertz) != NO_ERROR) {
7929066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return -1;
7939066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
794c3308074cd6f06bbb35c1999b5260f938742a9e7Chia-chi Yeh    return frameCount * nbChannels * (audioFormat == javaAudioTrackFields.PCM16 ? 2 : 1);
7959066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
7969066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
7977070b36549d511d6627538c73dfbab23fbae5b43Eric Laurent// ----------------------------------------------------------------------------
7987070b36549d511d6627538c73dfbab23fbae5b43Eric Laurentstatic void
7997070b36549d511d6627538c73dfbab23fbae5b43Eric Laurentandroid_media_AudioTrack_setAuxEffectSendLevel(JNIEnv *env, jobject thiz, jfloat level )
8007070b36549d511d6627538c73dfbab23fbae5b43Eric Laurent{
801532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent    sp<AudioTrack> lpTrack = getAudioTrack(env, thiz);
8027070b36549d511d6627538c73dfbab23fbae5b43Eric Laurent    if (lpTrack == NULL ) {
8037070b36549d511d6627538c73dfbab23fbae5b43Eric Laurent        jniThrowException(env, "java/lang/IllegalStateException",
8047070b36549d511d6627538c73dfbab23fbae5b43Eric Laurent            "Unable to retrieve AudioTrack pointer for setAuxEffectSendLevel()");
8057070b36549d511d6627538c73dfbab23fbae5b43Eric Laurent        return;
8067070b36549d511d6627538c73dfbab23fbae5b43Eric Laurent    }
8077070b36549d511d6627538c73dfbab23fbae5b43Eric Laurent
8087070b36549d511d6627538c73dfbab23fbae5b43Eric Laurent    lpTrack->setAuxEffectSendLevel(level);
8097070b36549d511d6627538c73dfbab23fbae5b43Eric Laurent}
8107070b36549d511d6627538c73dfbab23fbae5b43Eric Laurent
8117070b36549d511d6627538c73dfbab23fbae5b43Eric Laurent// ----------------------------------------------------------------------------
8127070b36549d511d6627538c73dfbab23fbae5b43Eric Laurentstatic jint android_media_AudioTrack_attachAuxEffect(JNIEnv *env,  jobject thiz,
8137070b36549d511d6627538c73dfbab23fbae5b43Eric Laurent        jint effectId) {
814532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent    sp<AudioTrack> lpTrack = getAudioTrack(env, thiz);
815532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent    if (lpTrack == NULL) {
8167070b36549d511d6627538c73dfbab23fbae5b43Eric Laurent        jniThrowException(env, "java/lang/IllegalStateException",
8177070b36549d511d6627538c73dfbab23fbae5b43Eric Laurent            "Unable to retrieve AudioTrack pointer for attachAuxEffect()");
8187070b36549d511d6627538c73dfbab23fbae5b43Eric Laurent        return AUDIOTRACK_ERROR;
8197070b36549d511d6627538c73dfbab23fbae5b43Eric Laurent    }
820532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent    return android_media_translateErrorCode( lpTrack->attachAuxEffect(effectId) );
8217070b36549d511d6627538c73dfbab23fbae5b43Eric Laurent}
8229066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
8239066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project// ----------------------------------------------------------------------------
8249066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project// ----------------------------------------------------------------------------
8259066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectstatic JNINativeMethod gMethods[] = {
8269066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    // name,              signature,     funcPtr
8279066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    {"native_start",         "()V",      (void *)android_media_AudioTrack_start},
8289066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    {"native_stop",          "()V",      (void *)android_media_AudioTrack_stop},
8299066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    {"native_pause",         "()V",      (void *)android_media_AudioTrack_pause},
8309066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    {"native_flush",         "()V",      (void *)android_media_AudioTrack_flush},
831619346f902241736d933657a4fe10f10c50a1ba8Eric Laurent    {"native_setup",         "(Ljava/lang/Object;IIIIII[I)I",
8329066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                                         (void *)android_media_AudioTrack_native_setup},
8339066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    {"native_finalize",      "()V",      (void *)android_media_AudioTrack_native_finalize},
8349066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    {"native_release",       "()V",      (void *)android_media_AudioTrack_native_release},
83500db1f52b1c12d3670a0419305aa7548f7932aa0Glenn Kasten    {"native_write_byte",    "([BIII)I", (void *)android_media_AudioTrack_native_write_byte},
8369066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    {"native_write_short",   "([SIII)I", (void *)android_media_AudioTrack_native_write_short},
8379066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    {"native_setVolume",     "(FF)V",    (void *)android_media_AudioTrack_set_volume},
8389066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    {"native_get_native_frame_count",
8399066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                             "()I",      (void *)android_media_AudioTrack_get_native_frame_count},
8409066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    {"native_set_playback_rate",
84188e209dcf8c2ebddda5c272f46d1bd5478bc639cEric Laurent                             "(I)I",     (void *)android_media_AudioTrack_set_playback_rate},
8429066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    {"native_get_playback_rate",
8439066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                             "()I",      (void *)android_media_AudioTrack_get_playback_rate},
8449066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    {"native_set_marker_pos","(I)I",     (void *)android_media_AudioTrack_set_marker_pos},
8459066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    {"native_get_marker_pos","()I",      (void *)android_media_AudioTrack_get_marker_pos},
8469066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    {"native_set_pos_update_period",
8479066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                             "(I)I",     (void *)android_media_AudioTrack_set_pos_update_period},
8489066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    {"native_get_pos_update_period",
8499066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                             "()I",      (void *)android_media_AudioTrack_get_pos_update_period},
8509066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    {"native_set_position",  "(I)I",     (void *)android_media_AudioTrack_set_position},
8519066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    {"native_get_position",  "()I",      (void *)android_media_AudioTrack_get_position},
8529066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    {"native_set_loop",      "(III)I",   (void *)android_media_AudioTrack_set_loop},
8539066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    {"native_reload_static", "()I",      (void *)android_media_AudioTrack_reload},
8549066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    {"native_get_output_sample_rate",
8559066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                             "(I)I",      (void *)android_media_AudioTrack_get_output_sample_rate},
8569066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    {"native_get_min_buff_size",
8579066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                             "(III)I",   (void *)android_media_AudioTrack_get_min_buff_size},
8587070b36549d511d6627538c73dfbab23fbae5b43Eric Laurent    {"native_setAuxEffectSendLevel",
8597070b36549d511d6627538c73dfbab23fbae5b43Eric Laurent                             "(F)V",     (void *)android_media_AudioTrack_setAuxEffectSendLevel},
8607070b36549d511d6627538c73dfbab23fbae5b43Eric Laurent    {"native_attachAuxEffect",
8617070b36549d511d6627538c73dfbab23fbae5b43Eric Laurent                             "(I)I",     (void *)android_media_AudioTrack_attachAuxEffect},
8629066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project};
8639066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
8649066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
8659066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project// field names found in android/media/AudioTrack.java
8669066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project#define JAVA_POSTEVENT_CALLBACK_NAME                    "postEventFromNative"
8679066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project#define JAVA_CONST_PCM16_NAME                           "ENCODING_PCM_16BIT"
8689066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project#define JAVA_CONST_PCM8_NAME                            "ENCODING_PCM_8BIT"
8699066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project#define JAVA_CONST_BUFFER_COUNT_NAME                    "BUFFER_COUNT"
8709066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project#define JAVA_CONST_STREAM_VOICE_CALL_NAME               "STREAM_VOICE_CALL"
8719066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project#define JAVA_CONST_STREAM_SYSTEM_NAME                   "STREAM_SYSTEM"
8729066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project#define JAVA_CONST_STREAM_RING_NAME                     "STREAM_RING"
8739066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project#define JAVA_CONST_STREAM_MUSIC_NAME                    "STREAM_MUSIC"
8749066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project#define JAVA_CONST_STREAM_ALARM_NAME                    "STREAM_ALARM"
8759066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project#define JAVA_CONST_STREAM_NOTIFICATION_NAME             "STREAM_NOTIFICATION"
8769066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project#define JAVA_CONST_STREAM_BLUETOOTH_SCO_NAME            "STREAM_BLUETOOTH_SCO"
87783b368505052f59616d29558e65e2250d01fd848Eric Laurent#define JAVA_CONST_STREAM_DTMF_NAME                     "STREAM_DTMF"
8789066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project#define JAVA_CONST_MODE_STREAM_NAME                     "MODE_STREAM"
8799066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project#define JAVA_CONST_MODE_STATIC_NAME                     "MODE_STATIC"
8809066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project#define JAVA_NATIVETRACKINJAVAOBJ_FIELD_NAME            "mNativeTrackInJavaObj"
8819066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project#define JAVA_JNIDATA_FIELD_NAME                         "mJniData"
8829066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
8839066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project#define JAVA_AUDIOFORMAT_CLASS_NAME             "android/media/AudioFormat"
8849066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project#define JAVA_AUDIOMANAGER_CLASS_NAME            "android/media/AudioManager"
8859066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
8869066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project// ----------------------------------------------------------------------------
8879066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project// preconditions:
8889066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project//    theClass is valid
8899066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectbool android_media_getIntConstantFromClass(JNIEnv* pEnv, jclass theClass, const char* className,
8909066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                             const char* constName, int* constVal) {
8919066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    jfieldID javaConst = NULL;
8929066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    javaConst = pEnv->GetStaticFieldID(theClass, constName, "I");
8939066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    if (javaConst != NULL) {
8949066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        *constVal = pEnv->GetStaticIntField(theClass, javaConst);
8959066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return true;
8969066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    } else {
8973762c311729fe9f3af085c14c5c1fb471d994c03Steve Block        ALOGE("Can't find %s.%s", className, constName);
8989066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return false;
8999066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
9009066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
9019066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
9029066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
9039066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project// ----------------------------------------------------------------------------
9049066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectint register_android_media_AudioTrack(JNIEnv *env)
9059066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project{
9069066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    javaAudioTrackFields.nativeTrackInJavaObj = NULL;
9079066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    javaAudioTrackFields.postNativeEventInJava = NULL;
9089066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
9099066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    // Get the AudioTrack class
91046e18c11d46a2bc1a46174f963d0ed1224d94cd2Brian Carlstrom    jclass audioTrackClass = env->FindClass(kClassPathName);
91146e18c11d46a2bc1a46174f963d0ed1224d94cd2Brian Carlstrom    if (audioTrackClass == NULL) {
9123762c311729fe9f3af085c14c5c1fb471d994c03Steve Block        ALOGE("Can't find %s", kClassPathName);
9139066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return -1;
9149066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
9159066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
9169066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    // Get the postEvent method
9179066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    javaAudioTrackFields.postNativeEventInJava = env->GetStaticMethodID(
91846e18c11d46a2bc1a46174f963d0ed1224d94cd2Brian Carlstrom            audioTrackClass,
9199066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            JAVA_POSTEVENT_CALLBACK_NAME, "(Ljava/lang/Object;IIILjava/lang/Object;)V");
9209066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    if (javaAudioTrackFields.postNativeEventInJava == NULL) {
9213762c311729fe9f3af085c14c5c1fb471d994c03Steve Block        ALOGE("Can't find AudioTrack.%s", JAVA_POSTEVENT_CALLBACK_NAME);
9229066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return -1;
9239066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
9249066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
9259066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    // Get the variables fields
9269066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    //      nativeTrackInJavaObj
9279066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    javaAudioTrackFields.nativeTrackInJavaObj = env->GetFieldID(
92846e18c11d46a2bc1a46174f963d0ed1224d94cd2Brian Carlstrom            audioTrackClass,
9299066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            JAVA_NATIVETRACKINJAVAOBJ_FIELD_NAME, "I");
9309066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    if (javaAudioTrackFields.nativeTrackInJavaObj == NULL) {
9313762c311729fe9f3af085c14c5c1fb471d994c03Steve Block        ALOGE("Can't find AudioTrack.%s", JAVA_NATIVETRACKINJAVAOBJ_FIELD_NAME);
9329066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return -1;
9339066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
9349066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    //      jniData;
9359066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    javaAudioTrackFields.jniData = env->GetFieldID(
93646e18c11d46a2bc1a46174f963d0ed1224d94cd2Brian Carlstrom            audioTrackClass,
9379066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            JAVA_JNIDATA_FIELD_NAME, "I");
9389066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    if (javaAudioTrackFields.jniData == NULL) {
9393762c311729fe9f3af085c14c5c1fb471d994c03Steve Block        ALOGE("Can't find AudioTrack.%s", JAVA_JNIDATA_FIELD_NAME);
9409066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return -1;
9419066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
9429066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
943