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 | |
| 17 | //#define LOG_NDEBUG 0 |
| 18 | |
| 19 | #define LOG_TAG "AudioRecord-JNI" |
| 20 | |
| 21 | #include <stdio.h> |
| 22 | #include <unistd.h> |
| 23 | #include <fcntl.h> |
| 24 | #include <math.h> |
| 25 | |
Glenn Kasten | c81d31c | 2012-03-13 14:46:23 -0700 | [diff] [blame] | 26 | #include <jni.h> |
| 27 | #include <JNIHelp.h> |
| 28 | #include <android_runtime/AndroidRuntime.h> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 29 | |
Glenn Kasten | c81d31c | 2012-03-13 14:46:23 -0700 | [diff] [blame] | 30 | #include <utils/Log.h> |
Eric Laurent | 532bc1c | 2012-04-20 12:45:03 -0700 | [diff] [blame] | 31 | #include <utils/SortedVector.h> |
| 32 | #include <utils/threads.h> |
Glenn Kasten | c81d31c | 2012-03-13 14:46:23 -0700 | [diff] [blame] | 33 | #include <media/AudioRecord.h> |
| 34 | #include <media/mediarecorder.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/AudioRecord"; |
| 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 | jfieldID nativeRecorderInJavaObj; // provides access to the C++ AudioRecord object |
| 53 | jfieldID nativeCallbackCookie; // provides access to the AudioRecord callback data |
| 54 | }; |
| 55 | static fields_t javaAudioRecordFields; |
| 56 | |
| 57 | struct audiorecord_callback_cookie { |
| 58 | jclass audioRecord_class; |
| 59 | jobject audioRecord_ref; |
Eric Laurent | 532bc1c | 2012-04-20 12:45:03 -0700 | [diff] [blame] | 60 | bool busy; |
| 61 | Condition cond; |
| 62 | }; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 63 | |
Eric Laurent | 532bc1c | 2012-04-20 12:45:03 -0700 | [diff] [blame] | 64 | static Mutex sLock; |
| 65 | static SortedVector <audiorecord_callback_cookie *> sAudioRecordCallBackCookies; |
Dave Sparks | e6335c9 | 2010-03-13 17:08:22 -0800 | [diff] [blame] | 66 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 67 | // ---------------------------------------------------------------------------- |
| 68 | |
| 69 | #define AUDIORECORD_SUCCESS 0 |
| 70 | #define AUDIORECORD_ERROR -1 |
| 71 | #define AUDIORECORD_ERROR_BAD_VALUE -2 |
| 72 | #define AUDIORECORD_ERROR_INVALID_OPERATION -3 |
| 73 | #define AUDIORECORD_ERROR_SETUP_ZEROFRAMECOUNT -16 |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 74 | #define AUDIORECORD_ERROR_SETUP_INVALIDCHANNELMASK -17 |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 75 | #define AUDIORECORD_ERROR_SETUP_INVALIDFORMAT -18 |
Eric Laurent | 4bc035a | 2009-05-22 09:18:15 -0700 | [diff] [blame] | 76 | #define AUDIORECORD_ERROR_SETUP_INVALIDSOURCE -19 |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 77 | #define AUDIORECORD_ERROR_SETUP_NATIVEINITFAILED -20 |
| 78 | |
| 79 | jint android_media_translateRecorderErrorCode(int code) { |
Glenn Kasten | 18db49a | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 80 | switch (code) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 81 | case NO_ERROR: |
| 82 | return AUDIORECORD_SUCCESS; |
| 83 | case BAD_VALUE: |
| 84 | return AUDIORECORD_ERROR_BAD_VALUE; |
| 85 | case INVALID_OPERATION: |
| 86 | return AUDIORECORD_ERROR_INVALID_OPERATION; |
| 87 | default: |
| 88 | return AUDIORECORD_ERROR; |
Glenn Kasten | 18db49a | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 89 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | |
| 93 | // ---------------------------------------------------------------------------- |
| 94 | static void recorderCallback(int event, void* user, void *info) { |
Eric Laurent | 532bc1c | 2012-04-20 12:45:03 -0700 | [diff] [blame] | 95 | |
| 96 | audiorecord_callback_cookie *callbackInfo = (audiorecord_callback_cookie *)user; |
| 97 | { |
| 98 | Mutex::Autolock l(sLock); |
| 99 | if (sAudioRecordCallBackCookies.indexOf(callbackInfo) < 0) { |
| 100 | return; |
| 101 | } |
| 102 | callbackInfo->busy = true; |
| 103 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 104 | if (event == AudioRecord::EVENT_MORE_DATA) { |
| 105 | // set size to 0 to signal we're not using the callback to read more data |
| 106 | AudioRecord::Buffer* pBuff = (AudioRecord::Buffer*)info; |
Glenn Kasten | 18db49a | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 107 | pBuff->size = 0; |
| 108 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 109 | } else if (event == AudioRecord::EVENT_MARKER) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 110 | JNIEnv *env = AndroidRuntime::getJNIEnv(); |
| 111 | if (user && env) { |
| 112 | env->CallStaticVoidMethod( |
Glenn Kasten | 18db49a | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 113 | callbackInfo->audioRecord_class, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 114 | javaAudioRecordFields.postNativeEventInJava, |
| 115 | callbackInfo->audioRecord_ref, event, 0,0, NULL); |
| 116 | if (env->ExceptionCheck()) { |
| 117 | env->ExceptionDescribe(); |
| 118 | env->ExceptionClear(); |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | } else if (event == AudioRecord::EVENT_NEW_POS) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 123 | JNIEnv *env = AndroidRuntime::getJNIEnv(); |
| 124 | if (user && env) { |
| 125 | env->CallStaticVoidMethod( |
Glenn Kasten | 18db49a | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 126 | callbackInfo->audioRecord_class, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 127 | javaAudioRecordFields.postNativeEventInJava, |
| 128 | callbackInfo->audioRecord_ref, event, 0,0, NULL); |
| 129 | if (env->ExceptionCheck()) { |
| 130 | env->ExceptionDescribe(); |
| 131 | env->ExceptionClear(); |
| 132 | } |
| 133 | } |
| 134 | } |
Eric Laurent | 532bc1c | 2012-04-20 12:45:03 -0700 | [diff] [blame] | 135 | { |
| 136 | Mutex::Autolock l(sLock); |
| 137 | callbackInfo->busy = false; |
| 138 | callbackInfo->cond.broadcast(); |
| 139 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 140 | } |
| 141 | |
Eric Laurent | 532bc1c | 2012-04-20 12:45:03 -0700 | [diff] [blame] | 142 | // ---------------------------------------------------------------------------- |
| 143 | static sp<AudioRecord> getAudioRecord(JNIEnv* env, jobject thiz) |
| 144 | { |
| 145 | Mutex::Autolock l(sLock); |
| 146 | AudioRecord* const ar = |
| 147 | (AudioRecord*)env->GetIntField(thiz, javaAudioRecordFields.nativeRecorderInJavaObj); |
| 148 | return sp<AudioRecord>(ar); |
| 149 | } |
| 150 | |
| 151 | static sp<AudioRecord> setAudioRecord(JNIEnv* env, jobject thiz, const sp<AudioRecord>& ar) |
| 152 | { |
| 153 | Mutex::Autolock l(sLock); |
| 154 | sp<AudioRecord> old = |
| 155 | (AudioRecord*)env->GetIntField(thiz, javaAudioRecordFields.nativeRecorderInJavaObj); |
| 156 | if (ar.get()) { |
| 157 | ar->incStrong(thiz); |
| 158 | } |
| 159 | if (old != 0) { |
| 160 | old->decStrong(thiz); |
| 161 | } |
| 162 | env->SetIntField(thiz, javaAudioRecordFields.nativeRecorderInJavaObj, (int)ar.get()); |
| 163 | return old; |
| 164 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 165 | |
| 166 | // ---------------------------------------------------------------------------- |
| 167 | static int |
| 168 | android_media_AudioRecord_setup(JNIEnv *env, jobject thiz, jobject weak_this, |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 169 | jint source, jint sampleRateInHertz, jint channels, |
Eric Laurent | 44ff4cd | 2011-06-18 10:34:05 -0700 | [diff] [blame] | 170 | jint audioFormat, jint buffSizeInBytes, jintArray jSession) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 171 | { |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 172 | //ALOGV(">> Entering android_media_AudioRecord_setup"); |
| 173 | //ALOGV("sampleRate=%d, audioFormat=%d, channels=%x, buffSizeInBytes=%d", |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 174 | // sampleRateInHertz, audioFormat, channels, buffSizeInBytes); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 175 | |
Dima Zavin | 24fc2fb | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 176 | if (!audio_is_input_channel(channels)) { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 177 | ALOGE("Error creating AudioRecord: channel count is not 1 or 2."); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 178 | return AUDIORECORD_ERROR_SETUP_INVALIDCHANNELMASK; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 179 | } |
Dima Zavin | 24fc2fb | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 180 | uint32_t nbChannels = popcount(channels); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 181 | |
| 182 | // compare the format against the Java constants |
Glenn Kasten | 18db49a | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 183 | if ((audioFormat != javaAudioRecordFields.PCM16) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 184 | && (audioFormat != javaAudioRecordFields.PCM8)) { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 185 | ALOGE("Error creating AudioRecord: unsupported audio format."); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 186 | return AUDIORECORD_ERROR_SETUP_INVALIDFORMAT; |
| 187 | } |
| 188 | |
| 189 | int bytesPerSample = audioFormat==javaAudioRecordFields.PCM16 ? 2 : 1; |
Glenn Kasten | 0a204ed | 2012-01-12 12:27:51 -0800 | [diff] [blame] | 190 | audio_format_t format = audioFormat==javaAudioRecordFields.PCM16 ? |
Dima Zavin | 24fc2fb | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 191 | AUDIO_FORMAT_PCM_16_BIT : AUDIO_FORMAT_PCM_8_BIT; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 192 | |
| 193 | if (buffSizeInBytes == 0) { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 194 | ALOGE("Error creating AudioRecord: frameCount is 0."); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 195 | return AUDIORECORD_ERROR_SETUP_ZEROFRAMECOUNT; |
| 196 | } |
| 197 | int frameSize = nbChannels * bytesPerSample; |
| 198 | size_t frameCount = buffSizeInBytes / frameSize; |
Glenn Kasten | 18db49a | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 199 | |
Glenn Kasten | 0f0fbd9 | 2012-01-23 13:58:49 -0800 | [diff] [blame] | 200 | if (uint32_t(source) >= AUDIO_SOURCE_CNT) { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 201 | ALOGE("Error creating AudioRecord: unknown source."); |
Eric Laurent | 4bc035a | 2009-05-22 09:18:15 -0700 | [diff] [blame] | 202 | return AUDIORECORD_ERROR_SETUP_INVALIDSOURCE; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 203 | } |
Eric Laurent | 4bc035a | 2009-05-22 09:18:15 -0700 | [diff] [blame] | 204 | |
Eric Laurent | 532bc1c | 2012-04-20 12:45:03 -0700 | [diff] [blame] | 205 | jclass clazz = env->GetObjectClass(thiz); |
| 206 | if (clazz == NULL) { |
| 207 | ALOGE("Can't find %s when setting up callback.", kClassPathName); |
| 208 | return AUDIORECORD_ERROR_SETUP_NATIVEINITFAILED; |
| 209 | } |
| 210 | |
Eric Laurent | 44ff4cd | 2011-06-18 10:34:05 -0700 | [diff] [blame] | 211 | if (jSession == NULL) { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 212 | ALOGE("Error creating AudioRecord: invalid session ID pointer"); |
Eric Laurent | 44ff4cd | 2011-06-18 10:34:05 -0700 | [diff] [blame] | 213 | return AUDIORECORD_ERROR; |
| 214 | } |
| 215 | |
| 216 | jint* nSession = (jint *) env->GetPrimitiveArrayCritical(jSession, NULL); |
| 217 | if (nSession == NULL) { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 218 | ALOGE("Error creating AudioRecord: Error retrieving session id pointer"); |
Eric Laurent | 44ff4cd | 2011-06-18 10:34:05 -0700 | [diff] [blame] | 219 | return AUDIORECORD_ERROR; |
| 220 | } |
| 221 | int sessionId = nSession[0]; |
| 222 | env->ReleasePrimitiveArrayCritical(jSession, nSession, 0); |
| 223 | nSession = NULL; |
| 224 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 225 | // create an uninitialized AudioRecord object |
Eric Laurent | 532bc1c | 2012-04-20 12:45:03 -0700 | [diff] [blame] | 226 | sp<AudioRecord> lpRecorder = new AudioRecord(); |
| 227 | if (lpRecorder == NULL) { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 228 | ALOGE("Error creating AudioRecord instance."); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 229 | return AUDIORECORD_ERROR_SETUP_NATIVEINITFAILED; |
| 230 | } |
Glenn Kasten | 18db49a | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 231 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 232 | // create the callback information: |
| 233 | // this data will be passed with every AudioRecord callback |
Eric Laurent | 532bc1c | 2012-04-20 12:45:03 -0700 | [diff] [blame] | 234 | audiorecord_callback_cookie *lpCallbackData = new audiorecord_callback_cookie; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 235 | lpCallbackData->audioRecord_class = (jclass)env->NewGlobalRef(clazz); |
| 236 | // we use a weak reference so the AudioRecord object can be garbage collected. |
| 237 | lpCallbackData->audioRecord_ref = env->NewGlobalRef(weak_this); |
Eric Laurent | 532bc1c | 2012-04-20 12:45:03 -0700 | [diff] [blame] | 238 | lpCallbackData->busy = false; |
Glenn Kasten | 18db49a | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 239 | |
Glenn Kasten | 0f0fbd9 | 2012-01-23 13:58:49 -0800 | [diff] [blame] | 240 | lpRecorder->set((audio_source_t) source, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 241 | sampleRateInHertz, |
| 242 | format, // word length, PCM |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 243 | channels, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 244 | frameCount, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 245 | recorderCallback,// callback_t |
| 246 | lpCallbackData,// void* user |
| 247 | 0, // notificationFrames, |
Eric Laurent | 44ff4cd | 2011-06-18 10:34:05 -0700 | [diff] [blame] | 248 | true, // threadCanCallJava) |
| 249 | sessionId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 250 | |
Glenn Kasten | 18db49a | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 251 | if (lpRecorder->initCheck() != NO_ERROR) { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 252 | ALOGE("Error creating AudioRecord instance: initialization check failed."); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 253 | goto native_init_failure; |
| 254 | } |
| 255 | |
Eric Laurent | 44ff4cd | 2011-06-18 10:34:05 -0700 | [diff] [blame] | 256 | 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 AudioRecord: Error retrieving session id pointer"); |
Eric Laurent | 44ff4cd | 2011-06-18 10:34:05 -0700 | [diff] [blame] | 259 | goto native_init_failure; |
| 260 | } |
Glenn Kasten | b3db213 | 2012-01-19 08:59:58 -0800 | [diff] [blame] | 261 | // read the audio session ID back from AudioRecord in case a new session was created during set() |
Eric Laurent | 44ff4cd | 2011-06-18 10:34:05 -0700 | [diff] [blame] | 262 | nSession[0] = lpRecorder->getSessionId(); |
| 263 | env->ReleasePrimitiveArrayCritical(jSession, nSession, 0); |
| 264 | nSession = NULL; |
| 265 | |
Eric Laurent | 532bc1c | 2012-04-20 12:45:03 -0700 | [diff] [blame] | 266 | { // scope for the lock |
| 267 | Mutex::Autolock l(sLock); |
| 268 | sAudioRecordCallBackCookies.add(lpCallbackData); |
| 269 | } |
Glenn Kasten | 18db49a | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 270 | // save our newly created C++ AudioRecord in the "nativeRecorderInJavaObj" field |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 271 | // of the Java object |
Eric Laurent | 532bc1c | 2012-04-20 12:45:03 -0700 | [diff] [blame] | 272 | setAudioRecord(env, thiz, lpRecorder); |
Glenn Kasten | 18db49a | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 273 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 274 | // save our newly created callback information in the "nativeCallbackCookie" field |
| 275 | // of the Java object (in mNativeCallbackCookie) so we can free the memory in finalize() |
| 276 | env->SetIntField(thiz, javaAudioRecordFields.nativeCallbackCookie, (int)lpCallbackData); |
Glenn Kasten | 18db49a | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 277 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 278 | return AUDIORECORD_SUCCESS; |
Glenn Kasten | 18db49a | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 279 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 280 | // failure: |
| 281 | native_init_failure: |
Jean-Michel Trivi | 4bac5a3 | 2009-07-17 12:05:31 -0700 | [diff] [blame] | 282 | env->DeleteGlobalRef(lpCallbackData->audioRecord_class); |
| 283 | env->DeleteGlobalRef(lpCallbackData->audioRecord_ref); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 284 | delete lpCallbackData; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 285 | env->SetIntField(thiz, javaAudioRecordFields.nativeCallbackCookie, 0); |
Glenn Kasten | 18db49a | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 286 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 287 | return AUDIORECORD_ERROR_SETUP_NATIVEINITFAILED; |
| 288 | } |
| 289 | |
| 290 | |
| 291 | |
| 292 | // ---------------------------------------------------------------------------- |
Eric Laurent | 9cc489a | 2009-12-05 05:20:01 -0800 | [diff] [blame] | 293 | static int |
Eric Laurent | 505e5c8 | 2012-03-29 15:19:36 -0700 | [diff] [blame] | 294 | android_media_AudioRecord_start(JNIEnv *env, jobject thiz, jint event, jint triggerSession) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 295 | { |
Eric Laurent | 532bc1c | 2012-04-20 12:45:03 -0700 | [diff] [blame] | 296 | sp<AudioRecord> lpRecorder = getAudioRecord(env, thiz); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 297 | if (lpRecorder == NULL ) { |
| 298 | jniThrowException(env, "java/lang/IllegalStateException", NULL); |
Eric Laurent | 9cc489a | 2009-12-05 05:20:01 -0800 | [diff] [blame] | 299 | return AUDIORECORD_ERROR; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 300 | } |
Glenn Kasten | 18db49a | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 301 | |
Eric Laurent | 505e5c8 | 2012-03-29 15:19:36 -0700 | [diff] [blame] | 302 | return android_media_translateRecorderErrorCode( |
| 303 | lpRecorder->start((AudioSystem::sync_event_t)event, triggerSession)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 304 | } |
| 305 | |
| 306 | |
| 307 | // ---------------------------------------------------------------------------- |
| 308 | static void |
| 309 | android_media_AudioRecord_stop(JNIEnv *env, jobject thiz) |
| 310 | { |
Eric Laurent | 532bc1c | 2012-04-20 12:45:03 -0700 | [diff] [blame] | 311 | sp<AudioRecord> lpRecorder = getAudioRecord(env, thiz); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 312 | if (lpRecorder == NULL ) { |
| 313 | jniThrowException(env, "java/lang/IllegalStateException", NULL); |
| 314 | return; |
| 315 | } |
| 316 | |
| 317 | lpRecorder->stop(); |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 318 | //ALOGV("Called lpRecorder->stop()"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 319 | } |
| 320 | |
| 321 | |
| 322 | // ---------------------------------------------------------------------------- |
Dave Sparks | e6335c9 | 2010-03-13 17:08:22 -0800 | [diff] [blame] | 323 | |
Eric Laurent | 532bc1c | 2012-04-20 12:45:03 -0700 | [diff] [blame] | 324 | #define CALLBACK_COND_WAIT_TIMEOUT_MS 1000 |
| 325 | static void android_media_AudioRecord_release(JNIEnv *env, jobject thiz) { |
| 326 | sp<AudioRecord> lpRecorder = setAudioRecord(env, thiz, 0); |
| 327 | if (lpRecorder == NULL) { |
| 328 | return; |
| 329 | } |
| 330 | ALOGV("About to delete lpRecorder: %x\n", (int)lpRecorder.get()); |
| 331 | lpRecorder->stop(); |
| 332 | |
Dave Sparks | e6335c9 | 2010-03-13 17:08:22 -0800 | [diff] [blame] | 333 | audiorecord_callback_cookie *lpCookie = (audiorecord_callback_cookie *)env->GetIntField( |
| 334 | thiz, javaAudioRecordFields.nativeCallbackCookie); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 335 | |
Dave Sparks | e6335c9 | 2010-03-13 17:08:22 -0800 | [diff] [blame] | 336 | // reset the native resources in the Java object so any attempt to access |
| 337 | // them after a call to release fails. |
Dave Sparks | e6335c9 | 2010-03-13 17:08:22 -0800 | [diff] [blame] | 338 | env->SetIntField(thiz, javaAudioRecordFields.nativeCallbackCookie, 0); |
| 339 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 340 | // delete the callback information |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 341 | if (lpCookie) { |
Eric Laurent | 532bc1c | 2012-04-20 12:45:03 -0700 | [diff] [blame] | 342 | Mutex::Autolock l(sLock); |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 343 | ALOGV("deleting lpCookie: %x\n", (int)lpCookie); |
Eric Laurent | 532bc1c | 2012-04-20 12:45:03 -0700 | [diff] [blame] | 344 | while (lpCookie->busy) { |
| 345 | if (lpCookie->cond.waitRelative(sLock, |
| 346 | milliseconds(CALLBACK_COND_WAIT_TIMEOUT_MS)) != |
| 347 | NO_ERROR) { |
| 348 | break; |
| 349 | } |
| 350 | } |
| 351 | sAudioRecordCallBackCookies.remove(lpCookie); |
Jean-Michel Trivi | 4bac5a3 | 2009-07-17 12:05:31 -0700 | [diff] [blame] | 352 | env->DeleteGlobalRef(lpCookie->audioRecord_class); |
| 353 | env->DeleteGlobalRef(lpCookie->audioRecord_ref); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 354 | delete lpCookie; |
| 355 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 356 | } |
| 357 | |
| 358 | |
| 359 | // ---------------------------------------------------------------------------- |
Dave Sparks | e6335c9 | 2010-03-13 17:08:22 -0800 | [diff] [blame] | 360 | static void android_media_AudioRecord_finalize(JNIEnv *env, jobject thiz) { |
| 361 | android_media_AudioRecord_release(env, thiz); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 362 | } |
| 363 | |
| 364 | |
| 365 | // ---------------------------------------------------------------------------- |
| 366 | static jint android_media_AudioRecord_readInByteArray(JNIEnv *env, jobject thiz, |
| 367 | jbyteArray javaAudioData, |
| 368 | jint offsetInBytes, jint sizeInBytes) { |
| 369 | jbyte* recordBuff = NULL; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 370 | // get the audio recorder from which we'll read new audio samples |
Eric Laurent | 532bc1c | 2012-04-20 12:45:03 -0700 | [diff] [blame] | 371 | sp<AudioRecord> lpRecorder = getAudioRecord(env, thiz); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 372 | if (lpRecorder == NULL) { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 373 | ALOGE("Unable to retrieve AudioRecord object, can't record"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 374 | return 0; |
| 375 | } |
| 376 | |
| 377 | if (!javaAudioData) { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 378 | ALOGE("Invalid Java array to store recorded audio, can't record"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 379 | return 0; |
| 380 | } |
| 381 | |
| 382 | // get the pointer to where we'll record the audio |
Eric Laurent | 421ddc0 | 2011-03-07 14:52:59 -0800 | [diff] [blame] | 383 | // NOTE: We may use GetPrimitiveArrayCritical() when the JNI implementation changes in such |
| 384 | // a way that it becomes much more efficient. When doing so, we will have to prevent the |
| 385 | // AudioSystem callback to be called while in critical section (in case of media server |
| 386 | // process crash for instance) |
| 387 | recordBuff = (jbyte *)env->GetByteArrayElements(javaAudioData, NULL); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 388 | |
| 389 | if (recordBuff == NULL) { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 390 | ALOGE("Error retrieving destination for recorded audio data, can't record"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 391 | return 0; |
| 392 | } |
| 393 | |
| 394 | // read the new audio data from the native AudioRecord object |
| 395 | ssize_t recorderBuffSize = lpRecorder->frameCount()*lpRecorder->frameSize(); |
Glenn Kasten | 18db49a | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 396 | ssize_t readSize = lpRecorder->read(recordBuff + offsetInBytes, |
| 397 | sizeInBytes > (jint)recorderBuffSize ? |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 398 | (jint)recorderBuffSize : sizeInBytes ); |
Eric Laurent | 421ddc0 | 2011-03-07 14:52:59 -0800 | [diff] [blame] | 399 | env->ReleaseByteArrayElements(javaAudioData, recordBuff, 0); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 400 | |
| 401 | return (jint) readSize; |
| 402 | } |
| 403 | |
| 404 | // ---------------------------------------------------------------------------- |
| 405 | static jint android_media_AudioRecord_readInShortArray(JNIEnv *env, jobject thiz, |
| 406 | jshortArray javaAudioData, |
| 407 | jint offsetInShorts, jint sizeInShorts) { |
| 408 | |
| 409 | return (android_media_AudioRecord_readInByteArray(env, thiz, |
| 410 | (jbyteArray) javaAudioData, |
| 411 | offsetInShorts*2, sizeInShorts*2) |
| 412 | / 2); |
| 413 | } |
| 414 | |
| 415 | // ---------------------------------------------------------------------------- |
| 416 | static jint android_media_AudioRecord_readInDirectBuffer(JNIEnv *env, jobject thiz, |
| 417 | jobject jBuffer, jint sizeInBytes) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 418 | // get the audio recorder from which we'll read new audio samples |
Eric Laurent | 532bc1c | 2012-04-20 12:45:03 -0700 | [diff] [blame] | 419 | sp<AudioRecord> lpRecorder = getAudioRecord(env, thiz); |
Glenn Kasten | 18db49a | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 420 | if (lpRecorder==NULL) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 421 | return 0; |
| 422 | |
| 423 | // direct buffer and direct access supported? |
| 424 | long capacity = env->GetDirectBufferCapacity(jBuffer); |
Glenn Kasten | 18db49a | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 425 | if (capacity == -1) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 426 | // buffer direct access is not supported |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 427 | ALOGE("Buffer direct access is not supported, can't record"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 428 | return 0; |
| 429 | } |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 430 | //ALOGV("capacity = %ld", capacity); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 431 | jbyte* nativeFromJavaBuf = (jbyte*) env->GetDirectBufferAddress(jBuffer); |
Glenn Kasten | 18db49a | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 432 | if (nativeFromJavaBuf==NULL) { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 433 | ALOGE("Buffer direct access is not supported, can't record"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 434 | return 0; |
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 | |
| 437 | // read new data from the recorder |
Glenn Kasten | 18db49a | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 438 | return (jint) lpRecorder->read(nativeFromJavaBuf, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 439 | capacity < sizeInBytes ? capacity : sizeInBytes); |
| 440 | } |
| 441 | |
| 442 | |
| 443 | // ---------------------------------------------------------------------------- |
Glenn Kasten | 18db49a | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 444 | static jint android_media_AudioRecord_set_marker_pos(JNIEnv *env, jobject thiz, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 445 | jint markerPos) { |
Glenn Kasten | 18db49a | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 446 | |
Eric Laurent | 532bc1c | 2012-04-20 12:45:03 -0700 | [diff] [blame] | 447 | sp<AudioRecord> lpRecorder = getAudioRecord(env, thiz); |
| 448 | if (lpRecorder == NULL) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 449 | jniThrowException(env, "java/lang/IllegalStateException", |
| 450 | "Unable to retrieve AudioRecord pointer for setMarkerPosition()"); |
| 451 | return AUDIORECORD_ERROR; |
| 452 | } |
Eric Laurent | 532bc1c | 2012-04-20 12:45:03 -0700 | [diff] [blame] | 453 | return android_media_translateRecorderErrorCode( lpRecorder->setMarkerPosition(markerPos) ); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 454 | } |
| 455 | |
| 456 | |
| 457 | // ---------------------------------------------------------------------------- |
| 458 | static jint android_media_AudioRecord_get_marker_pos(JNIEnv *env, jobject thiz) { |
Glenn Kasten | 18db49a | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 459 | |
Eric Laurent | 532bc1c | 2012-04-20 12:45:03 -0700 | [diff] [blame] | 460 | sp<AudioRecord> lpRecorder = getAudioRecord(env, thiz); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 461 | uint32_t markerPos = 0; |
Glenn Kasten | 18db49a | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 462 | |
Eric Laurent | 532bc1c | 2012-04-20 12:45:03 -0700 | [diff] [blame] | 463 | if (lpRecorder == NULL) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 464 | jniThrowException(env, "java/lang/IllegalStateException", |
| 465 | "Unable to retrieve AudioRecord pointer for getMarkerPosition()"); |
| 466 | return AUDIORECORD_ERROR; |
| 467 | } |
Eric Laurent | 532bc1c | 2012-04-20 12:45:03 -0700 | [diff] [blame] | 468 | lpRecorder->getMarkerPosition(&markerPos); |
| 469 | return (jint)markerPos; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 470 | } |
| 471 | |
| 472 | |
| 473 | // ---------------------------------------------------------------------------- |
| 474 | static jint android_media_AudioRecord_set_pos_update_period(JNIEnv *env, jobject thiz, |
| 475 | jint period) { |
Glenn Kasten | 18db49a | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 476 | |
Eric Laurent | 532bc1c | 2012-04-20 12:45:03 -0700 | [diff] [blame] | 477 | sp<AudioRecord> lpRecorder = getAudioRecord(env, thiz); |
Glenn Kasten | 18db49a | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 478 | |
Eric Laurent | 532bc1c | 2012-04-20 12:45:03 -0700 | [diff] [blame] | 479 | if (lpRecorder == NULL) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 480 | jniThrowException(env, "java/lang/IllegalStateException", |
| 481 | "Unable to retrieve AudioRecord pointer for setPositionUpdatePeriod()"); |
| 482 | return AUDIORECORD_ERROR; |
Glenn Kasten | 18db49a | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 483 | } |
Eric Laurent | 532bc1c | 2012-04-20 12:45:03 -0700 | [diff] [blame] | 484 | return android_media_translateRecorderErrorCode( lpRecorder->setPositionUpdatePeriod(period) ); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 485 | } |
| 486 | |
| 487 | |
| 488 | // ---------------------------------------------------------------------------- |
| 489 | static jint android_media_AudioRecord_get_pos_update_period(JNIEnv *env, jobject thiz) { |
Glenn Kasten | 18db49a | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 490 | |
Eric Laurent | 532bc1c | 2012-04-20 12:45:03 -0700 | [diff] [blame] | 491 | sp<AudioRecord> lpRecorder = getAudioRecord(env, thiz); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 492 | uint32_t period = 0; |
Glenn Kasten | 18db49a | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 493 | |
Eric Laurent | 532bc1c | 2012-04-20 12:45:03 -0700 | [diff] [blame] | 494 | if (lpRecorder == NULL) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 495 | jniThrowException(env, "java/lang/IllegalStateException", |
| 496 | "Unable to retrieve AudioRecord pointer for getPositionUpdatePeriod()"); |
| 497 | return AUDIORECORD_ERROR; |
| 498 | } |
Eric Laurent | 532bc1c | 2012-04-20 12:45:03 -0700 | [diff] [blame] | 499 | lpRecorder->getPositionUpdatePeriod(&period); |
| 500 | return (jint)period; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 501 | } |
| 502 | |
| 503 | |
| 504 | // ---------------------------------------------------------------------------- |
| 505 | // returns the minimum required size for the successful creation of an AudioRecord instance. |
| 506 | // returns 0 if the parameter combination is not supported. |
| 507 | // return -1 if there was an error querying the buffer size. |
| 508 | static jint android_media_AudioRecord_get_min_buff_size(JNIEnv *env, jobject thiz, |
| 509 | jint sampleRateInHertz, jint nbChannels, jint audioFormat) { |
Chia-chi Yeh | c330807 | 2010-08-19 17:14:36 +0800 | [diff] [blame] | 510 | |
Eric Laurent | 532bc1c | 2012-04-20 12:45:03 -0700 | [diff] [blame] | 511 | ALOGV(">> android_media_AudioRecord_get_min_buff_size(%d, %d, %d)", |
| 512 | sampleRateInHertz, nbChannels, audioFormat); |
Chia-chi Yeh | c330807 | 2010-08-19 17:14:36 +0800 | [diff] [blame] | 513 | |
| 514 | int frameCount = 0; |
| 515 | status_t result = AudioRecord::getMinFrameCount(&frameCount, |
| 516 | sampleRateInHertz, |
| 517 | (audioFormat == javaAudioRecordFields.PCM16 ? |
Dima Zavin | 24fc2fb | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 518 | AUDIO_FORMAT_PCM_16_BIT : AUDIO_FORMAT_PCM_8_BIT), |
Glenn Kasten | 845b471 | 2012-07-02 13:12:31 -0700 | [diff] [blame] | 519 | audio_channel_in_mask_from_count(nbChannels)); |
Chia-chi Yeh | c330807 | 2010-08-19 17:14:36 +0800 | [diff] [blame] | 520 | |
| 521 | if (result == BAD_VALUE) { |
| 522 | return 0; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 523 | } |
Chia-chi Yeh | c330807 | 2010-08-19 17:14:36 +0800 | [diff] [blame] | 524 | if (result != NO_ERROR) { |
| 525 | return -1; |
| 526 | } |
| 527 | return frameCount * nbChannels * (audioFormat == javaAudioRecordFields.PCM16 ? 2 : 1); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 528 | } |
| 529 | |
| 530 | |
| 531 | // ---------------------------------------------------------------------------- |
| 532 | // ---------------------------------------------------------------------------- |
| 533 | static JNINativeMethod gMethods[] = { |
| 534 | // name, signature, funcPtr |
Eric Laurent | 505e5c8 | 2012-03-29 15:19:36 -0700 | [diff] [blame] | 535 | {"native_start", "(II)I", (void *)android_media_AudioRecord_start}, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 536 | {"native_stop", "()V", (void *)android_media_AudioRecord_stop}, |
Eric Laurent | 44ff4cd | 2011-06-18 10:34:05 -0700 | [diff] [blame] | 537 | {"native_setup", "(Ljava/lang/Object;IIIII[I)I", |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 538 | (void *)android_media_AudioRecord_setup}, |
| 539 | {"native_finalize", "()V", (void *)android_media_AudioRecord_finalize}, |
| 540 | {"native_release", "()V", (void *)android_media_AudioRecord_release}, |
Glenn Kasten | 18db49a | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 541 | {"native_read_in_byte_array", |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 542 | "([BII)I", (void *)android_media_AudioRecord_readInByteArray}, |
| 543 | {"native_read_in_short_array", |
| 544 | "([SII)I", (void *)android_media_AudioRecord_readInShortArray}, |
| 545 | {"native_read_in_direct_buffer","(Ljava/lang/Object;I)I", |
| 546 | (void *)android_media_AudioRecord_readInDirectBuffer}, |
| 547 | {"native_set_marker_pos","(I)I", (void *)android_media_AudioRecord_set_marker_pos}, |
| 548 | {"native_get_marker_pos","()I", (void *)android_media_AudioRecord_get_marker_pos}, |
| 549 | {"native_set_pos_update_period", |
| 550 | "(I)I", (void *)android_media_AudioRecord_set_pos_update_period}, |
| 551 | {"native_get_pos_update_period", |
| 552 | "()I", (void *)android_media_AudioRecord_get_pos_update_period}, |
| 553 | {"native_get_min_buff_size", |
| 554 | "(III)I", (void *)android_media_AudioRecord_get_min_buff_size}, |
| 555 | }; |
| 556 | |
| 557 | // field names found in android/media/AudioRecord.java |
| 558 | #define JAVA_POSTEVENT_CALLBACK_NAME "postEventFromNative" |
| 559 | #define JAVA_CONST_PCM16_NAME "ENCODING_PCM_16BIT" |
| 560 | #define JAVA_CONST_PCM8_NAME "ENCODING_PCM_8BIT" |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 561 | #define JAVA_NATIVERECORDERINJAVAOBJ_FIELD_NAME "mNativeRecorderInJavaObj" |
| 562 | #define JAVA_NATIVECALLBACKINFO_FIELD_NAME "mNativeCallbackCookie" |
| 563 | |
| 564 | #define JAVA_AUDIOFORMAT_CLASS_NAME "android/media/AudioFormat" |
| 565 | |
| 566 | // ---------------------------------------------------------------------------- |
| 567 | |
Glenn Kasten | 18db49a | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 568 | extern bool android_media_getIntConstantFromClass(JNIEnv* pEnv, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 569 | jclass theClass, const char* className, const char* constName, int* constVal); |
| 570 | |
| 571 | // ---------------------------------------------------------------------------- |
| 572 | int register_android_media_AudioRecord(JNIEnv *env) |
| 573 | { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 574 | javaAudioRecordFields.postNativeEventInJava = NULL; |
| 575 | javaAudioRecordFields.nativeRecorderInJavaObj = NULL; |
| 576 | javaAudioRecordFields.nativeCallbackCookie = NULL; |
Glenn Kasten | 18db49a | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 577 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 578 | |
| 579 | // Get the AudioRecord class |
Brian Carlstrom | 46e18c11 | 2011-04-05 22:44:45 -0700 | [diff] [blame] | 580 | jclass audioRecordClass = env->FindClass(kClassPathName); |
| 581 | if (audioRecordClass == NULL) { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 582 | ALOGE("Can't find %s", kClassPathName); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 583 | return -1; |
| 584 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 585 | // Get the postEvent method |
| 586 | javaAudioRecordFields.postNativeEventInJava = env->GetStaticMethodID( |
Brian Carlstrom | 46e18c11 | 2011-04-05 22:44:45 -0700 | [diff] [blame] | 587 | audioRecordClass, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 588 | JAVA_POSTEVENT_CALLBACK_NAME, "(Ljava/lang/Object;IIILjava/lang/Object;)V"); |
| 589 | if (javaAudioRecordFields.postNativeEventInJava == NULL) { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 590 | ALOGE("Can't find AudioRecord.%s", JAVA_POSTEVENT_CALLBACK_NAME); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 591 | return -1; |
| 592 | } |
| 593 | |
| 594 | // Get the variables |
| 595 | // mNativeRecorderInJavaObj |
Glenn Kasten | 18db49a | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 596 | javaAudioRecordFields.nativeRecorderInJavaObj = |
Brian Carlstrom | 46e18c11 | 2011-04-05 22:44:45 -0700 | [diff] [blame] | 597 | env->GetFieldID(audioRecordClass, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 598 | JAVA_NATIVERECORDERINJAVAOBJ_FIELD_NAME, "I"); |
| 599 | if (javaAudioRecordFields.nativeRecorderInJavaObj == NULL) { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 600 | ALOGE("Can't find AudioRecord.%s", JAVA_NATIVERECORDERINJAVAOBJ_FIELD_NAME); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 601 | return -1; |
| 602 | } |
| 603 | // mNativeCallbackCookie |
| 604 | javaAudioRecordFields.nativeCallbackCookie = env->GetFieldID( |
Brian Carlstrom | 46e18c11 | 2011-04-05 22:44:45 -0700 | [diff] [blame] | 605 | audioRecordClass, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 606 | JAVA_NATIVECALLBACKINFO_FIELD_NAME, "I"); |
| 607 | if (javaAudioRecordFields.nativeCallbackCookie == NULL) { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 608 | ALOGE("Can't find AudioRecord.%s", JAVA_NATIVECALLBACKINFO_FIELD_NAME); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 609 | return -1; |
| 610 | } |
| 611 | |
| 612 | // Get the format constants from the AudioFormat class |
| 613 | jclass audioFormatClass = NULL; |
| 614 | audioFormatClass = env->FindClass(JAVA_AUDIOFORMAT_CLASS_NAME); |
| 615 | if (audioFormatClass == NULL) { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 616 | ALOGE("Can't find %s", JAVA_AUDIOFORMAT_CLASS_NAME); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 617 | return -1; |
| 618 | } |
Glenn Kasten | 18db49a | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 619 | if ( !android_media_getIntConstantFromClass(env, audioFormatClass, |
| 620 | JAVA_AUDIOFORMAT_CLASS_NAME, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 621 | JAVA_CONST_PCM16_NAME, &(javaAudioRecordFields.PCM16)) |
Glenn Kasten | 18db49a | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 622 | || !android_media_getIntConstantFromClass(env, audioFormatClass, |
| 623 | JAVA_AUDIOFORMAT_CLASS_NAME, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 624 | JAVA_CONST_PCM8_NAME, &(javaAudioRecordFields.PCM8)) ) { |
Glenn Kasten | 18db49a | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 625 | // error log performed in getIntConstantFromClass() |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 626 | return -1; |
| 627 | } |
| 628 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 629 | return AndroidRuntime::registerNativeMethods(env, |
| 630 | kClassPathName, gMethods, NELEM(gMethods)); |
| 631 | } |
| 632 | |
| 633 | // ---------------------------------------------------------------------------- |