android_media_AudioRecord.cpp revision 5b8fd44365f03601aaba41879ac18e70ce814a0f
19066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project/* 29066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * Copyright (C) 2008 The Android Open Source Project 39066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * 49066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * Licensed under the Apache License, Version 2.0 (the "License"); 59066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * you may not use this file except in compliance with the License. 69066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * You may obtain a copy of the License at 79066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * 89066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * http://www.apache.org/licenses/LICENSE-2.0 99066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * 109066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * Unless required by applicable law or agreed to in writing, software 119066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * distributed under the License is distributed on an "AS IS" BASIS, 129066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 139066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * See the License for the specific language governing permissions and 149066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * limitations under the License. 159066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project */ 169066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 179066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project//#define LOG_NDEBUG 0 189066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 199066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project#define LOG_TAG "AudioRecord-JNI" 209066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 2195ece35c2f8b86203e88b49d67cef8e80e107a2bKévin PETIT#include <inttypes.h> 22c81d31c3f801ba3d559a22c27b926ace38a7ab49Glenn Kasten#include <jni.h> 23c81d31c3f801ba3d559a22c27b926ace38a7ab49Glenn Kasten#include <JNIHelp.h> 24c81d31c3f801ba3d559a22c27b926ace38a7ab49Glenn Kasten#include <android_runtime/AndroidRuntime.h> 259066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 26c81d31c3f801ba3d559a22c27b926ace38a7ab49Glenn Kasten#include <utils/Log.h> 27c81d31c3f801ba3d559a22c27b926ace38a7ab49Glenn Kasten#include <media/AudioRecord.h> 2824fc2fb1c541e954b83fd31ea9f786a5e9b45501Dima Zavin 29fe834d30f4f3f51b754d55fecb36f11279733948Glenn Kasten#include "android_media_AudioFormat.h" 309066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 319066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project// ---------------------------------------------------------------------------- 329066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 339066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectusing namespace android; 349066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 359066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project// ---------------------------------------------------------------------------- 369066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectstatic const char* const kClassPathName = "android/media/AudioRecord"; 379066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 389066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectstruct fields_t { 399066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project // these fields provide access from C++ to the... 409066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project jmethodID postNativeEventInJava; //... event post callback method 419066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project jfieldID nativeRecorderInJavaObj; // provides access to the C++ AudioRecord object 429066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project jfieldID nativeCallbackCookie; // provides access to the AudioRecord callback data 439066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}; 449066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectstatic fields_t javaAudioRecordFields; 459066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 469066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectstruct audiorecord_callback_cookie { 479066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project jclass audioRecord_class; 489066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project jobject audioRecord_ref; 49532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent bool busy; 50532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent Condition cond; 51532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent}; 529066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 53532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurentstatic Mutex sLock; 54532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurentstatic SortedVector <audiorecord_callback_cookie *> sAudioRecordCallBackCookies; 55e6335c904aafa17795b059a5d83b8b7db6c761c8Dave Sparks 569066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project// ---------------------------------------------------------------------------- 579066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 589066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project#define AUDIORECORD_SUCCESS 0 599066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project#define AUDIORECORD_ERROR -1 609066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project#define AUDIORECORD_ERROR_BAD_VALUE -2 619066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project#define AUDIORECORD_ERROR_INVALID_OPERATION -3 629066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project#define AUDIORECORD_ERROR_SETUP_ZEROFRAMECOUNT -16 63a553c25b33c99b345cf1c8688f8df0ed8df14e5aEric Laurent#define AUDIORECORD_ERROR_SETUP_INVALIDCHANNELMASK -17 649066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project#define AUDIORECORD_ERROR_SETUP_INVALIDFORMAT -18 654bc035a65cac177be9294e69f110497e3b6e34e6Eric Laurent#define AUDIORECORD_ERROR_SETUP_INVALIDSOURCE -19 669066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project#define AUDIORECORD_ERROR_SETUP_NATIVEINITFAILED -20 679066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 689066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectjint android_media_translateRecorderErrorCode(int code) { 6918db49a46259020387c40fff36d92edc1087a366Glenn Kasten switch (code) { 709066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project case NO_ERROR: 719066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project return AUDIORECORD_SUCCESS; 729066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project case BAD_VALUE: 739066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project return AUDIORECORD_ERROR_BAD_VALUE; 749066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project case INVALID_OPERATION: 759066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project return AUDIORECORD_ERROR_INVALID_OPERATION; 769066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project default: 779066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project return AUDIORECORD_ERROR; 7818db49a46259020387c40fff36d92edc1087a366Glenn Kasten } 799066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project} 809066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 819066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project// ---------------------------------------------------------------------------- 829066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectstatic void recorderCallback(int event, void* user, void *info) { 83532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent 84532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent audiorecord_callback_cookie *callbackInfo = (audiorecord_callback_cookie *)user; 85532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent { 86532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent Mutex::Autolock l(sLock); 87532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent if (sAudioRecordCallBackCookies.indexOf(callbackInfo) < 0) { 88532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent return; 89532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent } 90532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent callbackInfo->busy = true; 91532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent } 9218db49a46259020387c40fff36d92edc1087a366Glenn Kasten 935b1576ccbd9ab43d5e3238589cf5b43a4834447dGlenn Kasten switch (event) { 945b1576ccbd9ab43d5e3238589cf5b43a4834447dGlenn Kasten case AudioRecord::EVENT_MARKER: { 959066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project JNIEnv *env = AndroidRuntime::getJNIEnv(); 96a667ff30e4fb6f741c2a930e7c31ef7e962d3b40Glenn Kasten if (user != NULL && env != NULL) { 979066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project env->CallStaticVoidMethod( 9818db49a46259020387c40fff36d92edc1087a366Glenn Kasten callbackInfo->audioRecord_class, 999066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project javaAudioRecordFields.postNativeEventInJava, 1009066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project callbackInfo->audioRecord_ref, event, 0,0, NULL); 1019066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project if (env->ExceptionCheck()) { 1029066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project env->ExceptionDescribe(); 1039066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project env->ExceptionClear(); 1049066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } 1059066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } 1065b1576ccbd9ab43d5e3238589cf5b43a4834447dGlenn Kasten } break; 1079066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 1085b1576ccbd9ab43d5e3238589cf5b43a4834447dGlenn Kasten case AudioRecord::EVENT_NEW_POS: { 1099066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project JNIEnv *env = AndroidRuntime::getJNIEnv(); 110a667ff30e4fb6f741c2a930e7c31ef7e962d3b40Glenn Kasten if (user != NULL && env != NULL) { 1119066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project env->CallStaticVoidMethod( 11218db49a46259020387c40fff36d92edc1087a366Glenn Kasten callbackInfo->audioRecord_class, 1139066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project javaAudioRecordFields.postNativeEventInJava, 1149066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project callbackInfo->audioRecord_ref, event, 0,0, NULL); 1159066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project if (env->ExceptionCheck()) { 1169066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project env->ExceptionDescribe(); 1179066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project env->ExceptionClear(); 1189066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } 1199066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } 1205b1576ccbd9ab43d5e3238589cf5b43a4834447dGlenn Kasten } break; 1219066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } 1225b1576ccbd9ab43d5e3238589cf5b43a4834447dGlenn Kasten 123532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent { 124532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent Mutex::Autolock l(sLock); 125532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent callbackInfo->busy = false; 126532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent callbackInfo->cond.broadcast(); 127532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent } 1289066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project} 1299066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 130532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent// ---------------------------------------------------------------------------- 131532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurentstatic sp<AudioRecord> getAudioRecord(JNIEnv* env, jobject thiz) 132532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent{ 133532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent Mutex::Autolock l(sLock); 134532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent AudioRecord* const ar = 135075e9a19ce645752f8282bc19c91b25978a7dc52Ashok Bhat (AudioRecord*)env->GetLongField(thiz, javaAudioRecordFields.nativeRecorderInJavaObj); 136532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent return sp<AudioRecord>(ar); 137532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent} 138532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent 139532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurentstatic sp<AudioRecord> setAudioRecord(JNIEnv* env, jobject thiz, const sp<AudioRecord>& ar) 140532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent{ 141532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent Mutex::Autolock l(sLock); 142532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent sp<AudioRecord> old = 143075e9a19ce645752f8282bc19c91b25978a7dc52Ashok Bhat (AudioRecord*)env->GetLongField(thiz, javaAudioRecordFields.nativeRecorderInJavaObj); 144532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent if (ar.get()) { 145b1d90c8f60f71422196c337f1d078b68867f5710Mathias Agopian ar->incStrong((void*)setAudioRecord); 146532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent } 147532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent if (old != 0) { 148b1d90c8f60f71422196c337f1d078b68867f5710Mathias Agopian old->decStrong((void*)setAudioRecord); 149532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent } 150075e9a19ce645752f8282bc19c91b25978a7dc52Ashok Bhat env->SetLongField(thiz, javaAudioRecordFields.nativeRecorderInJavaObj, (jlong)ar.get()); 151532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent return old; 152532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent} 1539066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 1549066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project// ---------------------------------------------------------------------------- 155075e9a19ce645752f8282bc19c91b25978a7dc52Ashok Bhatstatic jint 1569066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectandroid_media_AudioRecord_setup(JNIEnv *env, jobject thiz, jobject weak_this, 15733c437df39029b70d4ffafac2ca31bd71eafd0c6Glenn Kasten jint source, jint sampleRateInHertz, jint channelMask, 15833c437df39029b70d4ffafac2ca31bd71eafd0c6Glenn Kasten // Java channel masks map directly to the native definition 15944ff4cd8be50768d5bd471bc6c034acefa0b59caEric Laurent jint audioFormat, jint buffSizeInBytes, jintArray jSession) 1609066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project{ 16171f2cf116aab893e224056c38ab146bd1538dd3eSteve Block //ALOGV(">> Entering android_media_AudioRecord_setup"); 16233c437df39029b70d4ffafac2ca31bd71eafd0c6Glenn Kasten //ALOGV("sampleRate=%d, audioFormat=%d, channel mask=%x, buffSizeInBytes=%d", 16333c437df39029b70d4ffafac2ca31bd71eafd0c6Glenn Kasten // sampleRateInHertz, audioFormat, channelMask, buffSizeInBytes); 1649066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 16533c437df39029b70d4ffafac2ca31bd71eafd0c6Glenn Kasten if (!audio_is_input_channel(channelMask)) { 16633c437df39029b70d4ffafac2ca31bd71eafd0c6Glenn Kasten ALOGE("Error creating AudioRecord: channel mask %#x is not valid.", channelMask); 167075e9a19ce645752f8282bc19c91b25978a7dc52Ashok Bhat return (jint) AUDIORECORD_ERROR_SETUP_INVALIDCHANNELMASK; 1689066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } 1695b8fd44365f03601aaba41879ac18e70ce814a0fGlenn Kasten uint32_t channelCount = popcount(channelMask); 1709066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 1719066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project // compare the format against the Java constants 172a5a42384367230b8e16cf441d5dcf4225332f9d7Glenn Kasten audio_format_t format = audioFormatToNative(audioFormat); 173a5a42384367230b8e16cf441d5dcf4225332f9d7Glenn Kasten if (format == AUDIO_FORMAT_INVALID) { 174a9838afbe3ef58b63e5d52060d5ff2776c1c603aGlenn Kasten ALOGE("Error creating AudioRecord: unsupported audio format %d.", audioFormat); 175075e9a19ce645752f8282bc19c91b25978a7dc52Ashok Bhat return (jint) AUDIORECORD_ERROR_SETUP_INVALIDFORMAT; 1769066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } 1779066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 178a5a42384367230b8e16cf441d5dcf4225332f9d7Glenn Kasten size_t bytesPerSample = audio_bytes_per_sample(format); 1799066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 1809066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project if (buffSizeInBytes == 0) { 1813762c311729fe9f3af085c14c5c1fb471d994c03Steve Block ALOGE("Error creating AudioRecord: frameCount is 0."); 182075e9a19ce645752f8282bc19c91b25978a7dc52Ashok Bhat return (jint) AUDIORECORD_ERROR_SETUP_ZEROFRAMECOUNT; 1839066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } 1845b8fd44365f03601aaba41879ac18e70ce814a0fGlenn Kasten size_t frameSize = channelCount * bytesPerSample; 1859066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project size_t frameCount = buffSizeInBytes / frameSize; 18618db49a46259020387c40fff36d92edc1087a366Glenn Kasten 187357263da0ec2bc2be9d48c1becc3d94288c2f3edEric Laurent if ((uint32_t(source) >= AUDIO_SOURCE_CNT) && (uint32_t(source) != AUDIO_SOURCE_HOTWORD)) { 188a9838afbe3ef58b63e5d52060d5ff2776c1c603aGlenn Kasten ALOGE("Error creating AudioRecord: unknown source %d.", source); 189075e9a19ce645752f8282bc19c91b25978a7dc52Ashok Bhat return (jint) AUDIORECORD_ERROR_SETUP_INVALIDSOURCE; 1909066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } 1914bc035a65cac177be9294e69f110497e3b6e34e6Eric Laurent 192532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent jclass clazz = env->GetObjectClass(thiz); 193532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent if (clazz == NULL) { 194532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent ALOGE("Can't find %s when setting up callback.", kClassPathName); 195075e9a19ce645752f8282bc19c91b25978a7dc52Ashok Bhat return (jint) AUDIORECORD_ERROR_SETUP_NATIVEINITFAILED; 196532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent } 197532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent 19844ff4cd8be50768d5bd471bc6c034acefa0b59caEric Laurent if (jSession == NULL) { 1993762c311729fe9f3af085c14c5c1fb471d994c03Steve Block ALOGE("Error creating AudioRecord: invalid session ID pointer"); 200075e9a19ce645752f8282bc19c91b25978a7dc52Ashok Bhat return (jint) AUDIORECORD_ERROR; 20144ff4cd8be50768d5bd471bc6c034acefa0b59caEric Laurent } 20244ff4cd8be50768d5bd471bc6c034acefa0b59caEric Laurent 20344ff4cd8be50768d5bd471bc6c034acefa0b59caEric Laurent jint* nSession = (jint *) env->GetPrimitiveArrayCritical(jSession, NULL); 20444ff4cd8be50768d5bd471bc6c034acefa0b59caEric Laurent if (nSession == NULL) { 2053762c311729fe9f3af085c14c5c1fb471d994c03Steve Block ALOGE("Error creating AudioRecord: Error retrieving session id pointer"); 206075e9a19ce645752f8282bc19c91b25978a7dc52Ashok Bhat return (jint) AUDIORECORD_ERROR; 20744ff4cd8be50768d5bd471bc6c034acefa0b59caEric Laurent } 20844ff4cd8be50768d5bd471bc6c034acefa0b59caEric Laurent int sessionId = nSession[0]; 20944ff4cd8be50768d5bd471bc6c034acefa0b59caEric Laurent env->ReleasePrimitiveArrayCritical(jSession, nSession, 0); 21044ff4cd8be50768d5bd471bc6c034acefa0b59caEric Laurent nSession = NULL; 21144ff4cd8be50768d5bd471bc6c034acefa0b59caEric Laurent 2129066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project // create an uninitialized AudioRecord object 213532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent sp<AudioRecord> lpRecorder = new AudioRecord(); 21418db49a46259020387c40fff36d92edc1087a366Glenn Kasten 2159066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project // create the callback information: 2169066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project // this data will be passed with every AudioRecord callback 217532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent audiorecord_callback_cookie *lpCallbackData = new audiorecord_callback_cookie; 2189066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project lpCallbackData->audioRecord_class = (jclass)env->NewGlobalRef(clazz); 2199066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project // we use a weak reference so the AudioRecord object can be garbage collected. 2209066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project lpCallbackData->audioRecord_ref = env->NewGlobalRef(weak_this); 221532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent lpCallbackData->busy = false; 22218db49a46259020387c40fff36d92edc1087a366Glenn Kasten 2230f0fbd9441f40c6f99470b89774e397f99bf61ebGlenn Kasten lpRecorder->set((audio_source_t) source, 2249066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project sampleRateInHertz, 2259066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project format, // word length, PCM 22633c437df39029b70d4ffafac2ca31bd71eafd0c6Glenn Kasten channelMask, 2279066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project frameCount, 2289066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project recorderCallback,// callback_t 2299066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project lpCallbackData,// void* user 2309066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 0, // notificationFrames, 23186fad47e68e344b19fabeab1156c5e6742d8dfadGlenn Kasten true, // threadCanCallJava 23244ff4cd8be50768d5bd471bc6c034acefa0b59caEric Laurent sessionId); 2339066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 234a9838afbe3ef58b63e5d52060d5ff2776c1c603aGlenn Kasten const status_t status = lpRecorder->initCheck(); 235a9838afbe3ef58b63e5d52060d5ff2776c1c603aGlenn Kasten if (status != NO_ERROR) { 236a9838afbe3ef58b63e5d52060d5ff2776c1c603aGlenn Kasten ALOGE("Error creating AudioRecord instance: initialization check failed with status %d.", 237a9838afbe3ef58b63e5d52060d5ff2776c1c603aGlenn Kasten status); 2389066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project goto native_init_failure; 2399066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } 2409066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 24144ff4cd8be50768d5bd471bc6c034acefa0b59caEric Laurent nSession = (jint *) env->GetPrimitiveArrayCritical(jSession, NULL); 24244ff4cd8be50768d5bd471bc6c034acefa0b59caEric Laurent if (nSession == NULL) { 2433762c311729fe9f3af085c14c5c1fb471d994c03Steve Block ALOGE("Error creating AudioRecord: Error retrieving session id pointer"); 24444ff4cd8be50768d5bd471bc6c034acefa0b59caEric Laurent goto native_init_failure; 24544ff4cd8be50768d5bd471bc6c034acefa0b59caEric Laurent } 246b3db213eb55acb661e4b9ea40bcc00af4b76fab9Glenn Kasten // read the audio session ID back from AudioRecord in case a new session was created during set() 24744ff4cd8be50768d5bd471bc6c034acefa0b59caEric Laurent nSession[0] = lpRecorder->getSessionId(); 24844ff4cd8be50768d5bd471bc6c034acefa0b59caEric Laurent env->ReleasePrimitiveArrayCritical(jSession, nSession, 0); 24944ff4cd8be50768d5bd471bc6c034acefa0b59caEric Laurent nSession = NULL; 25044ff4cd8be50768d5bd471bc6c034acefa0b59caEric Laurent 251532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent { // scope for the lock 252532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent Mutex::Autolock l(sLock); 253532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent sAudioRecordCallBackCookies.add(lpCallbackData); 254532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent } 25518db49a46259020387c40fff36d92edc1087a366Glenn Kasten // save our newly created C++ AudioRecord in the "nativeRecorderInJavaObj" field 2569066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project // of the Java object 257532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent setAudioRecord(env, thiz, lpRecorder); 25818db49a46259020387c40fff36d92edc1087a366Glenn Kasten 2599066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project // save our newly created callback information in the "nativeCallbackCookie" field 2609066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project // of the Java object (in mNativeCallbackCookie) so we can free the memory in finalize() 261075e9a19ce645752f8282bc19c91b25978a7dc52Ashok Bhat env->SetLongField(thiz, javaAudioRecordFields.nativeCallbackCookie, (jlong)lpCallbackData); 26218db49a46259020387c40fff36d92edc1087a366Glenn Kasten 263075e9a19ce645752f8282bc19c91b25978a7dc52Ashok Bhat return (jint) AUDIORECORD_SUCCESS; 26418db49a46259020387c40fff36d92edc1087a366Glenn Kasten 2659066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project // failure: 2669066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectnative_init_failure: 2674bac5a3928d57ac193141eb6d45f3b39200d27deJean-Michel Trivi env->DeleteGlobalRef(lpCallbackData->audioRecord_class); 2684bac5a3928d57ac193141eb6d45f3b39200d27deJean-Michel Trivi env->DeleteGlobalRef(lpCallbackData->audioRecord_ref); 2699066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project delete lpCallbackData; 270075e9a19ce645752f8282bc19c91b25978a7dc52Ashok Bhat env->SetLongField(thiz, javaAudioRecordFields.nativeCallbackCookie, 0); 27118db49a46259020387c40fff36d92edc1087a366Glenn Kasten 272075e9a19ce645752f8282bc19c91b25978a7dc52Ashok Bhat return (jint) AUDIORECORD_ERROR_SETUP_NATIVEINITFAILED; 2739066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project} 2749066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 2759066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 2769066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 2779066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project// ---------------------------------------------------------------------------- 278075e9a19ce645752f8282bc19c91b25978a7dc52Ashok Bhatstatic jint 279505e5c8859f596ed58489be565d6e029314b2ac8Eric Laurentandroid_media_AudioRecord_start(JNIEnv *env, jobject thiz, jint event, jint triggerSession) 2809066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project{ 281532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent sp<AudioRecord> lpRecorder = getAudioRecord(env, thiz); 2829066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project if (lpRecorder == NULL ) { 2839066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project jniThrowException(env, "java/lang/IllegalStateException", NULL); 284075e9a19ce645752f8282bc19c91b25978a7dc52Ashok Bhat return (jint) AUDIORECORD_ERROR; 2859066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } 28618db49a46259020387c40fff36d92edc1087a366Glenn Kasten 287075e9a19ce645752f8282bc19c91b25978a7dc52Ashok Bhat return (jint) android_media_translateRecorderErrorCode( 288505e5c8859f596ed58489be565d6e029314b2ac8Eric Laurent lpRecorder->start((AudioSystem::sync_event_t)event, triggerSession)); 2899066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project} 2909066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 2919066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 2929066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project// ---------------------------------------------------------------------------- 2939066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectstatic void 2949066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectandroid_media_AudioRecord_stop(JNIEnv *env, jobject thiz) 2959066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project{ 296532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent sp<AudioRecord> lpRecorder = getAudioRecord(env, thiz); 2979066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project if (lpRecorder == NULL ) { 2989066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project jniThrowException(env, "java/lang/IllegalStateException", NULL); 2999066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project return; 3009066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } 3019066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 3029066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project lpRecorder->stop(); 30371f2cf116aab893e224056c38ab146bd1538dd3eSteve Block //ALOGV("Called lpRecorder->stop()"); 3049066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project} 3059066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 3069066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 3079066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project// ---------------------------------------------------------------------------- 308532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent 309532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent#define CALLBACK_COND_WAIT_TIMEOUT_MS 1000 310e6335c904aafa17795b059a5d83b8b7db6c761c8Dave Sparksstatic void android_media_AudioRecord_release(JNIEnv *env, jobject thiz) { 311532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent sp<AudioRecord> lpRecorder = setAudioRecord(env, thiz, 0); 312532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent if (lpRecorder == NULL) { 313532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent return; 314532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent } 31595ece35c2f8b86203e88b49d67cef8e80e107a2bKévin PETIT ALOGV("About to delete lpRecorder: %" PRIxPTR "\n", lpRecorder.get()); 316532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent lpRecorder->stop(); 317e6335c904aafa17795b059a5d83b8b7db6c761c8Dave Sparks 318075e9a19ce645752f8282bc19c91b25978a7dc52Ashok Bhat audiorecord_callback_cookie *lpCookie = (audiorecord_callback_cookie *)env->GetLongField( 319e6335c904aafa17795b059a5d83b8b7db6c761c8Dave Sparks thiz, javaAudioRecordFields.nativeCallbackCookie); 3209066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 321e6335c904aafa17795b059a5d83b8b7db6c761c8Dave Sparks // reset the native resources in the Java object so any attempt to access 322e6335c904aafa17795b059a5d83b8b7db6c761c8Dave Sparks // them after a call to release fails. 323075e9a19ce645752f8282bc19c91b25978a7dc52Ashok Bhat env->SetLongField(thiz, javaAudioRecordFields.nativeCallbackCookie, 0); 324e6335c904aafa17795b059a5d83b8b7db6c761c8Dave Sparks 3259066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project // delete the callback information 3269066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project if (lpCookie) { 327532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent Mutex::Autolock l(sLock); 32895ece35c2f8b86203e88b49d67cef8e80e107a2bKévin PETIT ALOGV("deleting lpCookie: %" PRIxPTR "\n", lpCookie); 329532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent while (lpCookie->busy) { 330532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent if (lpCookie->cond.waitRelative(sLock, 331532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent milliseconds(CALLBACK_COND_WAIT_TIMEOUT_MS)) != 332532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent NO_ERROR) { 333532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent break; 334532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent } 335532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent } 336532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent sAudioRecordCallBackCookies.remove(lpCookie); 3374bac5a3928d57ac193141eb6d45f3b39200d27deJean-Michel Trivi env->DeleteGlobalRef(lpCookie->audioRecord_class); 3384bac5a3928d57ac193141eb6d45f3b39200d27deJean-Michel Trivi env->DeleteGlobalRef(lpCookie->audioRecord_ref); 3399066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project delete lpCookie; 3409066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } 3419066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project} 3429066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 3439066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 3449066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project// ---------------------------------------------------------------------------- 345e6335c904aafa17795b059a5d83b8b7db6c761c8Dave Sparksstatic void android_media_AudioRecord_finalize(JNIEnv *env, jobject thiz) { 346e6335c904aafa17795b059a5d83b8b7db6c761c8Dave Sparks android_media_AudioRecord_release(env, thiz); 3479066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project} 3489066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 3499066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 3509066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project// ---------------------------------------------------------------------------- 3519066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectstatic jint android_media_AudioRecord_readInByteArray(JNIEnv *env, jobject thiz, 3529066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project jbyteArray javaAudioData, 3539066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project jint offsetInBytes, jint sizeInBytes) { 3549066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project jbyte* recordBuff = NULL; 3559066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project // get the audio recorder from which we'll read new audio samples 356532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent sp<AudioRecord> lpRecorder = getAudioRecord(env, thiz); 3579066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project if (lpRecorder == NULL) { 3583762c311729fe9f3af085c14c5c1fb471d994c03Steve Block ALOGE("Unable to retrieve AudioRecord object, can't record"); 3599066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project return 0; 3609066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } 3619066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 3629066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project if (!javaAudioData) { 3633762c311729fe9f3af085c14c5c1fb471d994c03Steve Block ALOGE("Invalid Java array to store recorded audio, can't record"); 3649066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project return 0; 3659066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } 3669066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 3679066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project // get the pointer to where we'll record the audio 368421ddc014b31478ba0ef115e098d8780a35af331Eric Laurent // NOTE: We may use GetPrimitiveArrayCritical() when the JNI implementation changes in such 369421ddc014b31478ba0ef115e098d8780a35af331Eric Laurent // a way that it becomes much more efficient. When doing so, we will have to prevent the 370421ddc014b31478ba0ef115e098d8780a35af331Eric Laurent // AudioSystem callback to be called while in critical section (in case of media server 371421ddc014b31478ba0ef115e098d8780a35af331Eric Laurent // process crash for instance) 372421ddc014b31478ba0ef115e098d8780a35af331Eric Laurent recordBuff = (jbyte *)env->GetByteArrayElements(javaAudioData, NULL); 3739066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 3749066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project if (recordBuff == NULL) { 3753762c311729fe9f3af085c14c5c1fb471d994c03Steve Block ALOGE("Error retrieving destination for recorded audio data, can't record"); 3769066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project return 0; 3779066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } 3789066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 3799066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project // read the new audio data from the native AudioRecord object 3809066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project ssize_t recorderBuffSize = lpRecorder->frameCount()*lpRecorder->frameSize(); 38118db49a46259020387c40fff36d92edc1087a366Glenn Kasten ssize_t readSize = lpRecorder->read(recordBuff + offsetInBytes, 38218db49a46259020387c40fff36d92edc1087a366Glenn Kasten sizeInBytes > (jint)recorderBuffSize ? 3839066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project (jint)recorderBuffSize : sizeInBytes ); 384421ddc014b31478ba0ef115e098d8780a35af331Eric Laurent env->ReleaseByteArrayElements(javaAudioData, recordBuff, 0); 3859066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 386357263da0ec2bc2be9d48c1becc3d94288c2f3edEric Laurent if (readSize < 0) { 387357263da0ec2bc2be9d48c1becc3d94288c2f3edEric Laurent readSize = AUDIORECORD_ERROR_INVALID_OPERATION; 388357263da0ec2bc2be9d48c1becc3d94288c2f3edEric Laurent } 3899066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project return (jint) readSize; 3909066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project} 3919066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 3929066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project// ---------------------------------------------------------------------------- 3939066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectstatic jint android_media_AudioRecord_readInShortArray(JNIEnv *env, jobject thiz, 3949066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project jshortArray javaAudioData, 3959066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project jint offsetInShorts, jint sizeInShorts) { 3969066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 3973f0af8be9e11112b03fc487bc2cc393286b923b5Glenn Kasten jint read = android_media_AudioRecord_readInByteArray(env, thiz, 3989066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project (jbyteArray) javaAudioData, 3993f0af8be9e11112b03fc487bc2cc393286b923b5Glenn Kasten offsetInShorts*2, sizeInShorts*2); 4003f0af8be9e11112b03fc487bc2cc393286b923b5Glenn Kasten if (read > 0) { 4013f0af8be9e11112b03fc487bc2cc393286b923b5Glenn Kasten read /= 2; 4023f0af8be9e11112b03fc487bc2cc393286b923b5Glenn Kasten } 4033f0af8be9e11112b03fc487bc2cc393286b923b5Glenn Kasten return read; 4049066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project} 4059066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 4069066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project// ---------------------------------------------------------------------------- 4079066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectstatic jint android_media_AudioRecord_readInDirectBuffer(JNIEnv *env, jobject thiz, 4089066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project jobject jBuffer, jint sizeInBytes) { 4099066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project // get the audio recorder from which we'll read new audio samples 410532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent sp<AudioRecord> lpRecorder = getAudioRecord(env, thiz); 41118db49a46259020387c40fff36d92edc1087a366Glenn Kasten if (lpRecorder==NULL) 4129066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project return 0; 4139066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 4149066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project // direct buffer and direct access supported? 4159066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project long capacity = env->GetDirectBufferCapacity(jBuffer); 41618db49a46259020387c40fff36d92edc1087a366Glenn Kasten if (capacity == -1) { 4179066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project // buffer direct access is not supported 4183762c311729fe9f3af085c14c5c1fb471d994c03Steve Block ALOGE("Buffer direct access is not supported, can't record"); 4199066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project return 0; 4209066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } 42171f2cf116aab893e224056c38ab146bd1538dd3eSteve Block //ALOGV("capacity = %ld", capacity); 4229066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project jbyte* nativeFromJavaBuf = (jbyte*) env->GetDirectBufferAddress(jBuffer); 42318db49a46259020387c40fff36d92edc1087a366Glenn Kasten if (nativeFromJavaBuf==NULL) { 4243762c311729fe9f3af085c14c5c1fb471d994c03Steve Block ALOGE("Buffer direct access is not supported, can't record"); 4259066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project return 0; 42618db49a46259020387c40fff36d92edc1087a366Glenn Kasten } 4279066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 4289066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project // read new data from the recorder 429357263da0ec2bc2be9d48c1becc3d94288c2f3edEric Laurent ssize_t readSize = lpRecorder->read(nativeFromJavaBuf, 4309066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project capacity < sizeInBytes ? capacity : sizeInBytes); 431357263da0ec2bc2be9d48c1becc3d94288c2f3edEric Laurent if (readSize < 0) { 432357263da0ec2bc2be9d48c1becc3d94288c2f3edEric Laurent readSize = AUDIORECORD_ERROR_INVALID_OPERATION; 433357263da0ec2bc2be9d48c1becc3d94288c2f3edEric Laurent } 434357263da0ec2bc2be9d48c1becc3d94288c2f3edEric Laurent return (jint)readSize; 4359066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project} 4369066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 4379066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 4389066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project// ---------------------------------------------------------------------------- 43918db49a46259020387c40fff36d92edc1087a366Glenn Kastenstatic jint android_media_AudioRecord_set_marker_pos(JNIEnv *env, jobject thiz, 4409066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project jint markerPos) { 44118db49a46259020387c40fff36d92edc1087a366Glenn Kasten 442532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent sp<AudioRecord> lpRecorder = getAudioRecord(env, thiz); 443532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent if (lpRecorder == NULL) { 4449066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project jniThrowException(env, "java/lang/IllegalStateException", 4459066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project "Unable to retrieve AudioRecord pointer for setMarkerPosition()"); 4469066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project return AUDIORECORD_ERROR; 4479066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } 448532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent return android_media_translateRecorderErrorCode( lpRecorder->setMarkerPosition(markerPos) ); 4499066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project} 4509066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 4519066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 4529066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project// ---------------------------------------------------------------------------- 4539066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectstatic jint android_media_AudioRecord_get_marker_pos(JNIEnv *env, jobject thiz) { 45418db49a46259020387c40fff36d92edc1087a366Glenn Kasten 455532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent sp<AudioRecord> lpRecorder = getAudioRecord(env, thiz); 4569066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project uint32_t markerPos = 0; 45718db49a46259020387c40fff36d92edc1087a366Glenn Kasten 458532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent if (lpRecorder == NULL) { 4599066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project jniThrowException(env, "java/lang/IllegalStateException", 4609066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project "Unable to retrieve AudioRecord pointer for getMarkerPosition()"); 4619066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project return AUDIORECORD_ERROR; 4629066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } 463532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent lpRecorder->getMarkerPosition(&markerPos); 464532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent return (jint)markerPos; 4659066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project} 4669066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 4679066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 4689066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project// ---------------------------------------------------------------------------- 4699066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectstatic jint android_media_AudioRecord_set_pos_update_period(JNIEnv *env, jobject thiz, 4709066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project jint period) { 47118db49a46259020387c40fff36d92edc1087a366Glenn Kasten 472532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent sp<AudioRecord> lpRecorder = getAudioRecord(env, thiz); 47318db49a46259020387c40fff36d92edc1087a366Glenn Kasten 474532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent if (lpRecorder == NULL) { 4759066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project jniThrowException(env, "java/lang/IllegalStateException", 4769066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project "Unable to retrieve AudioRecord pointer for setPositionUpdatePeriod()"); 4779066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project return AUDIORECORD_ERROR; 47818db49a46259020387c40fff36d92edc1087a366Glenn Kasten } 479532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent return android_media_translateRecorderErrorCode( lpRecorder->setPositionUpdatePeriod(period) ); 4809066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project} 4819066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 4829066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 4839066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project// ---------------------------------------------------------------------------- 4849066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectstatic jint android_media_AudioRecord_get_pos_update_period(JNIEnv *env, jobject thiz) { 48518db49a46259020387c40fff36d92edc1087a366Glenn Kasten 486532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent sp<AudioRecord> lpRecorder = getAudioRecord(env, thiz); 4879066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project uint32_t period = 0; 48818db49a46259020387c40fff36d92edc1087a366Glenn Kasten 489532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent if (lpRecorder == NULL) { 4909066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project jniThrowException(env, "java/lang/IllegalStateException", 4919066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project "Unable to retrieve AudioRecord pointer for getPositionUpdatePeriod()"); 4929066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project return AUDIORECORD_ERROR; 4939066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } 494532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent lpRecorder->getPositionUpdatePeriod(&period); 495532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent return (jint)period; 4969066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project} 4979066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 4989066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 4999066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project// ---------------------------------------------------------------------------- 5009066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project// returns the minimum required size for the successful creation of an AudioRecord instance. 5019066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project// returns 0 if the parameter combination is not supported. 5029066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project// return -1 if there was an error querying the buffer size. 5039066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectstatic jint android_media_AudioRecord_get_min_buff_size(JNIEnv *env, jobject thiz, 5045b8fd44365f03601aaba41879ac18e70ce814a0fGlenn Kasten jint sampleRateInHertz, jint channelCount, jint audioFormat) { 505c3308074cd6f06bbb35c1999b5260f938742a9e7Chia-chi Yeh 506532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent ALOGV(">> android_media_AudioRecord_get_min_buff_size(%d, %d, %d)", 5075b8fd44365f03601aaba41879ac18e70ce814a0fGlenn Kasten sampleRateInHertz, channelCount, audioFormat); 508c3308074cd6f06bbb35c1999b5260f938742a9e7Chia-chi Yeh 509fd1e3dfde2a70906c50144a7b1b53593ec1f7891Glenn Kasten size_t frameCount = 0; 510a5a42384367230b8e16cf441d5dcf4225332f9d7Glenn Kasten audio_format_t format = audioFormatToNative(audioFormat); 511c3308074cd6f06bbb35c1999b5260f938742a9e7Chia-chi Yeh status_t result = AudioRecord::getMinFrameCount(&frameCount, 512c3308074cd6f06bbb35c1999b5260f938742a9e7Chia-chi Yeh sampleRateInHertz, 513a5a42384367230b8e16cf441d5dcf4225332f9d7Glenn Kasten format, 5145b8fd44365f03601aaba41879ac18e70ce814a0fGlenn Kasten audio_channel_in_mask_from_count(channelCount)); 515c3308074cd6f06bbb35c1999b5260f938742a9e7Chia-chi Yeh 516c3308074cd6f06bbb35c1999b5260f938742a9e7Chia-chi Yeh if (result == BAD_VALUE) { 517c3308074cd6f06bbb35c1999b5260f938742a9e7Chia-chi Yeh return 0; 518c3308074cd6f06bbb35c1999b5260f938742a9e7Chia-chi Yeh } 519c3308074cd6f06bbb35c1999b5260f938742a9e7Chia-chi Yeh if (result != NO_ERROR) { 520c3308074cd6f06bbb35c1999b5260f938742a9e7Chia-chi Yeh return -1; 5219066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } 5225b8fd44365f03601aaba41879ac18e70ce814a0fGlenn Kasten return frameCount * channelCount * audio_bytes_per_sample(format); 5239066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project} 5249066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 5259066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 5269066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project// ---------------------------------------------------------------------------- 5279066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project// ---------------------------------------------------------------------------- 5289066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectstatic JNINativeMethod gMethods[] = { 5299066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project // name, signature, funcPtr 530505e5c8859f596ed58489be565d6e029314b2ac8Eric Laurent {"native_start", "(II)I", (void *)android_media_AudioRecord_start}, 5319066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project {"native_stop", "()V", (void *)android_media_AudioRecord_stop}, 53244ff4cd8be50768d5bd471bc6c034acefa0b59caEric Laurent {"native_setup", "(Ljava/lang/Object;IIIII[I)I", 5339066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project (void *)android_media_AudioRecord_setup}, 5349066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project {"native_finalize", "()V", (void *)android_media_AudioRecord_finalize}, 5359066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project {"native_release", "()V", (void *)android_media_AudioRecord_release}, 53618db49a46259020387c40fff36d92edc1087a366Glenn Kasten {"native_read_in_byte_array", 5379066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project "([BII)I", (void *)android_media_AudioRecord_readInByteArray}, 5389066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project {"native_read_in_short_array", 5399066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project "([SII)I", (void *)android_media_AudioRecord_readInShortArray}, 5409066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project {"native_read_in_direct_buffer","(Ljava/lang/Object;I)I", 5419066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project (void *)android_media_AudioRecord_readInDirectBuffer}, 5429066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project {"native_set_marker_pos","(I)I", (void *)android_media_AudioRecord_set_marker_pos}, 5439066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project {"native_get_marker_pos","()I", (void *)android_media_AudioRecord_get_marker_pos}, 5449066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project {"native_set_pos_update_period", 5459066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project "(I)I", (void *)android_media_AudioRecord_set_pos_update_period}, 5469066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project {"native_get_pos_update_period", 5479066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project "()I", (void *)android_media_AudioRecord_get_pos_update_period}, 5489066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project {"native_get_min_buff_size", 5499066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project "(III)I", (void *)android_media_AudioRecord_get_min_buff_size}, 5509066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}; 5519066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 5529066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project// field names found in android/media/AudioRecord.java 5539066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project#define JAVA_POSTEVENT_CALLBACK_NAME "postEventFromNative" 5549066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project#define JAVA_NATIVERECORDERINJAVAOBJ_FIELD_NAME "mNativeRecorderInJavaObj" 5559066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project#define JAVA_NATIVECALLBACKINFO_FIELD_NAME "mNativeCallbackCookie" 5569066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 5579066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project// ---------------------------------------------------------------------------- 5589066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectint register_android_media_AudioRecord(JNIEnv *env) 5599066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project{ 5609066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project javaAudioRecordFields.postNativeEventInJava = NULL; 5619066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project javaAudioRecordFields.nativeRecorderInJavaObj = NULL; 5629066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project javaAudioRecordFields.nativeCallbackCookie = NULL; 56318db49a46259020387c40fff36d92edc1087a366Glenn Kasten 5649066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 5659066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project // Get the AudioRecord class 56646e18c11d46a2bc1a46174f963d0ed1224d94cd2Brian Carlstrom jclass audioRecordClass = env->FindClass(kClassPathName); 56746e18c11d46a2bc1a46174f963d0ed1224d94cd2Brian Carlstrom if (audioRecordClass == NULL) { 5683762c311729fe9f3af085c14c5c1fb471d994c03Steve Block ALOGE("Can't find %s", kClassPathName); 5699066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project return -1; 5709066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } 5719066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project // Get the postEvent method 5729066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project javaAudioRecordFields.postNativeEventInJava = env->GetStaticMethodID( 57346e18c11d46a2bc1a46174f963d0ed1224d94cd2Brian Carlstrom audioRecordClass, 5749066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project JAVA_POSTEVENT_CALLBACK_NAME, "(Ljava/lang/Object;IIILjava/lang/Object;)V"); 5759066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project if (javaAudioRecordFields.postNativeEventInJava == NULL) { 5763762c311729fe9f3af085c14c5c1fb471d994c03Steve Block ALOGE("Can't find AudioRecord.%s", JAVA_POSTEVENT_CALLBACK_NAME); 5779066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project return -1; 5789066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } 5799066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 5809066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project // Get the variables 5819066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project // mNativeRecorderInJavaObj 58218db49a46259020387c40fff36d92edc1087a366Glenn Kasten javaAudioRecordFields.nativeRecorderInJavaObj = 58346e18c11d46a2bc1a46174f963d0ed1224d94cd2Brian Carlstrom env->GetFieldID(audioRecordClass, 584075e9a19ce645752f8282bc19c91b25978a7dc52Ashok Bhat JAVA_NATIVERECORDERINJAVAOBJ_FIELD_NAME, "J"); 5859066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project if (javaAudioRecordFields.nativeRecorderInJavaObj == NULL) { 5863762c311729fe9f3af085c14c5c1fb471d994c03Steve Block ALOGE("Can't find AudioRecord.%s", JAVA_NATIVERECORDERINJAVAOBJ_FIELD_NAME); 5879066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project return -1; 5889066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } 5899066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project // mNativeCallbackCookie 5909066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project javaAudioRecordFields.nativeCallbackCookie = env->GetFieldID( 59146e18c11d46a2bc1a46174f963d0ed1224d94cd2Brian Carlstrom audioRecordClass, 592075e9a19ce645752f8282bc19c91b25978a7dc52Ashok Bhat JAVA_NATIVECALLBACKINFO_FIELD_NAME, "J"); 5939066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project if (javaAudioRecordFields.nativeCallbackCookie == NULL) { 5943762c311729fe9f3af085c14c5c1fb471d994c03Steve Block ALOGE("Can't find AudioRecord.%s", JAVA_NATIVECALLBACKINFO_FIELD_NAME); 5959066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project return -1; 5969066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } 5979066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 5989066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project return AndroidRuntime::registerNativeMethods(env, 5999066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project kClassPathName, gMethods, NELEM(gMethods)); 6009066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project} 6019066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 6029066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project// ---------------------------------------------------------------------------- 603