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