android_media_AudioRecord.cpp revision bc11a696f6856624de532a6830d5db9dbbe2384c
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" 30bc11a696f6856624de532a6830d5db9dbbe2384cEric Laurent#include "android_media_AudioErrors.h" 319066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 329066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project// ---------------------------------------------------------------------------- 339066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 349066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectusing namespace android; 359066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 369066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project// ---------------------------------------------------------------------------- 379066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectstatic const char* const kClassPathName = "android/media/AudioRecord"; 389066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 399066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectstruct fields_t { 409066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project // these fields provide access from C++ to the... 419066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project jmethodID postNativeEventInJava; //... event post callback method 429066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project jfieldID nativeRecorderInJavaObj; // provides access to the C++ AudioRecord object 439066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project jfieldID nativeCallbackCookie; // provides access to the AudioRecord callback data 449066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}; 459066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectstatic fields_t javaAudioRecordFields; 469066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 479066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectstruct audiorecord_callback_cookie { 489066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project jclass audioRecord_class; 499066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project jobject audioRecord_ref; 50532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent bool busy; 51532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent Condition cond; 52532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent}; 539066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 54532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurentstatic Mutex sLock; 55532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurentstatic SortedVector <audiorecord_callback_cookie *> sAudioRecordCallBackCookies; 56e6335c904aafa17795b059a5d83b8b7db6c761c8Dave Sparks 579066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project// ---------------------------------------------------------------------------- 589066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 599066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project#define AUDIORECORD_ERROR_SETUP_ZEROFRAMECOUNT -16 60a553c25b33c99b345cf1c8688f8df0ed8df14e5aEric Laurent#define AUDIORECORD_ERROR_SETUP_INVALIDCHANNELMASK -17 619066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project#define AUDIORECORD_ERROR_SETUP_INVALIDFORMAT -18 624bc035a65cac177be9294e69f110497e3b6e34e6Eric Laurent#define AUDIORECORD_ERROR_SETUP_INVALIDSOURCE -19 639066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project#define AUDIORECORD_ERROR_SETUP_NATIVEINITFAILED -20 649066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 659066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project// ---------------------------------------------------------------------------- 669066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectstatic void recorderCallback(int event, void* user, void *info) { 67532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent 68532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent audiorecord_callback_cookie *callbackInfo = (audiorecord_callback_cookie *)user; 69532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent { 70532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent Mutex::Autolock l(sLock); 71532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent if (sAudioRecordCallBackCookies.indexOf(callbackInfo) < 0) { 72532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent return; 73532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent } 74532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent callbackInfo->busy = true; 75532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent } 7618db49a46259020387c40fff36d92edc1087a366Glenn Kasten 775b1576ccbd9ab43d5e3238589cf5b43a4834447dGlenn Kasten switch (event) { 785b1576ccbd9ab43d5e3238589cf5b43a4834447dGlenn Kasten case AudioRecord::EVENT_MARKER: { 799066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project JNIEnv *env = AndroidRuntime::getJNIEnv(); 80a667ff30e4fb6f741c2a930e7c31ef7e962d3b40Glenn Kasten if (user != NULL && env != NULL) { 819066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project env->CallStaticVoidMethod( 8218db49a46259020387c40fff36d92edc1087a366Glenn Kasten callbackInfo->audioRecord_class, 839066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project javaAudioRecordFields.postNativeEventInJava, 849066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project callbackInfo->audioRecord_ref, event, 0,0, NULL); 859066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project if (env->ExceptionCheck()) { 869066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project env->ExceptionDescribe(); 879066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project env->ExceptionClear(); 889066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } 899066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } 905b1576ccbd9ab43d5e3238589cf5b43a4834447dGlenn Kasten } break; 919066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 925b1576ccbd9ab43d5e3238589cf5b43a4834447dGlenn Kasten case AudioRecord::EVENT_NEW_POS: { 939066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project JNIEnv *env = AndroidRuntime::getJNIEnv(); 94a667ff30e4fb6f741c2a930e7c31ef7e962d3b40Glenn Kasten if (user != NULL && env != NULL) { 959066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project env->CallStaticVoidMethod( 9618db49a46259020387c40fff36d92edc1087a366Glenn Kasten callbackInfo->audioRecord_class, 979066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project javaAudioRecordFields.postNativeEventInJava, 989066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project callbackInfo->audioRecord_ref, event, 0,0, NULL); 999066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project if (env->ExceptionCheck()) { 1009066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project env->ExceptionDescribe(); 1019066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project env->ExceptionClear(); 1029066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } 1039066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } 1045b1576ccbd9ab43d5e3238589cf5b43a4834447dGlenn Kasten } break; 1059066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } 1065b1576ccbd9ab43d5e3238589cf5b43a4834447dGlenn Kasten 107532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent { 108532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent Mutex::Autolock l(sLock); 109532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent callbackInfo->busy = false; 110532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent callbackInfo->cond.broadcast(); 111532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent } 1129066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project} 1139066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 114532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent// ---------------------------------------------------------------------------- 115532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurentstatic sp<AudioRecord> getAudioRecord(JNIEnv* env, jobject thiz) 116532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent{ 117532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent Mutex::Autolock l(sLock); 118532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent AudioRecord* const ar = 119075e9a19ce645752f8282bc19c91b25978a7dc52Ashok Bhat (AudioRecord*)env->GetLongField(thiz, javaAudioRecordFields.nativeRecorderInJavaObj); 120532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent return sp<AudioRecord>(ar); 121532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent} 122532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent 123532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurentstatic sp<AudioRecord> setAudioRecord(JNIEnv* env, jobject thiz, const sp<AudioRecord>& ar) 124532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent{ 125532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent Mutex::Autolock l(sLock); 126532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent sp<AudioRecord> old = 127075e9a19ce645752f8282bc19c91b25978a7dc52Ashok Bhat (AudioRecord*)env->GetLongField(thiz, javaAudioRecordFields.nativeRecorderInJavaObj); 128532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent if (ar.get()) { 129b1d90c8f60f71422196c337f1d078b68867f5710Mathias Agopian ar->incStrong((void*)setAudioRecord); 130532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent } 131532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent if (old != 0) { 132b1d90c8f60f71422196c337f1d078b68867f5710Mathias Agopian old->decStrong((void*)setAudioRecord); 133532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent } 134075e9a19ce645752f8282bc19c91b25978a7dc52Ashok Bhat env->SetLongField(thiz, javaAudioRecordFields.nativeRecorderInJavaObj, (jlong)ar.get()); 135532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent return old; 136532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent} 1379066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 1389066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project// ---------------------------------------------------------------------------- 139075e9a19ce645752f8282bc19c91b25978a7dc52Ashok Bhatstatic jint 1409066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectandroid_media_AudioRecord_setup(JNIEnv *env, jobject thiz, jobject weak_this, 14133c437df39029b70d4ffafac2ca31bd71eafd0c6Glenn Kasten jint source, jint sampleRateInHertz, jint channelMask, 14233c437df39029b70d4ffafac2ca31bd71eafd0c6Glenn Kasten // Java channel masks map directly to the native definition 14344ff4cd8be50768d5bd471bc6c034acefa0b59caEric Laurent jint audioFormat, jint buffSizeInBytes, jintArray jSession) 1449066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project{ 14571f2cf116aab893e224056c38ab146bd1538dd3eSteve Block //ALOGV(">> Entering android_media_AudioRecord_setup"); 14633c437df39029b70d4ffafac2ca31bd71eafd0c6Glenn Kasten //ALOGV("sampleRate=%d, audioFormat=%d, channel mask=%x, buffSizeInBytes=%d", 14733c437df39029b70d4ffafac2ca31bd71eafd0c6Glenn Kasten // sampleRateInHertz, audioFormat, channelMask, buffSizeInBytes); 1489066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 14933c437df39029b70d4ffafac2ca31bd71eafd0c6Glenn Kasten if (!audio_is_input_channel(channelMask)) { 15033c437df39029b70d4ffafac2ca31bd71eafd0c6Glenn Kasten ALOGE("Error creating AudioRecord: channel mask %#x is not valid.", channelMask); 151075e9a19ce645752f8282bc19c91b25978a7dc52Ashok Bhat return (jint) AUDIORECORD_ERROR_SETUP_INVALIDCHANNELMASK; 1529066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } 1535b8fd44365f03601aaba41879ac18e70ce814a0fGlenn Kasten uint32_t channelCount = popcount(channelMask); 1549066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 1559066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project // compare the format against the Java constants 156a5a42384367230b8e16cf441d5dcf4225332f9d7Glenn Kasten audio_format_t format = audioFormatToNative(audioFormat); 157a5a42384367230b8e16cf441d5dcf4225332f9d7Glenn Kasten if (format == AUDIO_FORMAT_INVALID) { 158a9838afbe3ef58b63e5d52060d5ff2776c1c603aGlenn Kasten ALOGE("Error creating AudioRecord: unsupported audio format %d.", audioFormat); 159075e9a19ce645752f8282bc19c91b25978a7dc52Ashok Bhat return (jint) AUDIORECORD_ERROR_SETUP_INVALIDFORMAT; 1609066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } 1619066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 162a5a42384367230b8e16cf441d5dcf4225332f9d7Glenn Kasten size_t bytesPerSample = audio_bytes_per_sample(format); 1639066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 1649066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project if (buffSizeInBytes == 0) { 1653762c311729fe9f3af085c14c5c1fb471d994c03Steve Block ALOGE("Error creating AudioRecord: frameCount is 0."); 166075e9a19ce645752f8282bc19c91b25978a7dc52Ashok Bhat return (jint) AUDIORECORD_ERROR_SETUP_ZEROFRAMECOUNT; 1679066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } 1685b8fd44365f03601aaba41879ac18e70ce814a0fGlenn Kasten size_t frameSize = channelCount * bytesPerSample; 1699066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project size_t frameCount = buffSizeInBytes / frameSize; 17018db49a46259020387c40fff36d92edc1087a366Glenn Kasten 171357263da0ec2bc2be9d48c1becc3d94288c2f3edEric Laurent if ((uint32_t(source) >= AUDIO_SOURCE_CNT) && (uint32_t(source) != AUDIO_SOURCE_HOTWORD)) { 172a9838afbe3ef58b63e5d52060d5ff2776c1c603aGlenn Kasten ALOGE("Error creating AudioRecord: unknown source %d.", source); 173075e9a19ce645752f8282bc19c91b25978a7dc52Ashok Bhat return (jint) AUDIORECORD_ERROR_SETUP_INVALIDSOURCE; 1749066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } 1754bc035a65cac177be9294e69f110497e3b6e34e6Eric Laurent 176532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent jclass clazz = env->GetObjectClass(thiz); 177532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent if (clazz == NULL) { 178532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent ALOGE("Can't find %s when setting up callback.", kClassPathName); 179075e9a19ce645752f8282bc19c91b25978a7dc52Ashok Bhat return (jint) AUDIORECORD_ERROR_SETUP_NATIVEINITFAILED; 180532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent } 181532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent 18244ff4cd8be50768d5bd471bc6c034acefa0b59caEric Laurent if (jSession == NULL) { 1833762c311729fe9f3af085c14c5c1fb471d994c03Steve Block ALOGE("Error creating AudioRecord: invalid session ID pointer"); 184bc11a696f6856624de532a6830d5db9dbbe2384cEric Laurent return (jint) AUDIO_JAVA_ERROR; 18544ff4cd8be50768d5bd471bc6c034acefa0b59caEric Laurent } 18644ff4cd8be50768d5bd471bc6c034acefa0b59caEric Laurent 18744ff4cd8be50768d5bd471bc6c034acefa0b59caEric Laurent jint* nSession = (jint *) env->GetPrimitiveArrayCritical(jSession, NULL); 18844ff4cd8be50768d5bd471bc6c034acefa0b59caEric Laurent if (nSession == NULL) { 1893762c311729fe9f3af085c14c5c1fb471d994c03Steve Block ALOGE("Error creating AudioRecord: Error retrieving session id pointer"); 190bc11a696f6856624de532a6830d5db9dbbe2384cEric Laurent return (jint) AUDIO_JAVA_ERROR; 19144ff4cd8be50768d5bd471bc6c034acefa0b59caEric Laurent } 19244ff4cd8be50768d5bd471bc6c034acefa0b59caEric Laurent int sessionId = nSession[0]; 19344ff4cd8be50768d5bd471bc6c034acefa0b59caEric Laurent env->ReleasePrimitiveArrayCritical(jSession, nSession, 0); 19444ff4cd8be50768d5bd471bc6c034acefa0b59caEric Laurent nSession = NULL; 19544ff4cd8be50768d5bd471bc6c034acefa0b59caEric Laurent 1969066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project // create an uninitialized AudioRecord object 197532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent sp<AudioRecord> lpRecorder = new AudioRecord(); 19818db49a46259020387c40fff36d92edc1087a366Glenn Kasten 1999066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project // create the callback information: 2009066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project // this data will be passed with every AudioRecord callback 201532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent audiorecord_callback_cookie *lpCallbackData = new audiorecord_callback_cookie; 2029066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project lpCallbackData->audioRecord_class = (jclass)env->NewGlobalRef(clazz); 2039066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project // we use a weak reference so the AudioRecord object can be garbage collected. 2049066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project lpCallbackData->audioRecord_ref = env->NewGlobalRef(weak_this); 205532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent lpCallbackData->busy = false; 20618db49a46259020387c40fff36d92edc1087a366Glenn Kasten 207c5d142428ad68dd8aa4ffb80719667b13ad5049fGlenn Kasten const status_t status = lpRecorder->set((audio_source_t) source, 2089066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project sampleRateInHertz, 2099066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project format, // word length, PCM 21033c437df39029b70d4ffafac2ca31bd71eafd0c6Glenn Kasten channelMask, 2119066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project frameCount, 2129066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project recorderCallback,// callback_t 2139066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project lpCallbackData,// void* user 2149066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 0, // notificationFrames, 21586fad47e68e344b19fabeab1156c5e6742d8dfadGlenn Kasten true, // threadCanCallJava 21644ff4cd8be50768d5bd471bc6c034acefa0b59caEric Laurent sessionId); 2179066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 218a9838afbe3ef58b63e5d52060d5ff2776c1c603aGlenn Kasten if (status != NO_ERROR) { 219a9838afbe3ef58b63e5d52060d5ff2776c1c603aGlenn Kasten ALOGE("Error creating AudioRecord instance: initialization check failed with status %d.", 220a9838afbe3ef58b63e5d52060d5ff2776c1c603aGlenn Kasten status); 2219066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project goto native_init_failure; 2229066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } 2239066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 22444ff4cd8be50768d5bd471bc6c034acefa0b59caEric Laurent nSession = (jint *) env->GetPrimitiveArrayCritical(jSession, NULL); 22544ff4cd8be50768d5bd471bc6c034acefa0b59caEric Laurent if (nSession == NULL) { 2263762c311729fe9f3af085c14c5c1fb471d994c03Steve Block ALOGE("Error creating AudioRecord: Error retrieving session id pointer"); 22744ff4cd8be50768d5bd471bc6c034acefa0b59caEric Laurent goto native_init_failure; 22844ff4cd8be50768d5bd471bc6c034acefa0b59caEric Laurent } 229b3db213eb55acb661e4b9ea40bcc00af4b76fab9Glenn Kasten // read the audio session ID back from AudioRecord in case a new session was created during set() 23044ff4cd8be50768d5bd471bc6c034acefa0b59caEric Laurent nSession[0] = lpRecorder->getSessionId(); 23144ff4cd8be50768d5bd471bc6c034acefa0b59caEric Laurent env->ReleasePrimitiveArrayCritical(jSession, nSession, 0); 23244ff4cd8be50768d5bd471bc6c034acefa0b59caEric Laurent nSession = NULL; 23344ff4cd8be50768d5bd471bc6c034acefa0b59caEric Laurent 234532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent { // scope for the lock 235532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent Mutex::Autolock l(sLock); 236532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent sAudioRecordCallBackCookies.add(lpCallbackData); 237532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent } 23818db49a46259020387c40fff36d92edc1087a366Glenn Kasten // save our newly created C++ AudioRecord in the "nativeRecorderInJavaObj" field 2399066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project // of the Java object 240532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent setAudioRecord(env, thiz, lpRecorder); 24118db49a46259020387c40fff36d92edc1087a366Glenn Kasten 2429066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project // save our newly created callback information in the "nativeCallbackCookie" field 2439066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project // of the Java object (in mNativeCallbackCookie) so we can free the memory in finalize() 244075e9a19ce645752f8282bc19c91b25978a7dc52Ashok Bhat env->SetLongField(thiz, javaAudioRecordFields.nativeCallbackCookie, (jlong)lpCallbackData); 24518db49a46259020387c40fff36d92edc1087a366Glenn Kasten 246bc11a696f6856624de532a6830d5db9dbbe2384cEric Laurent return (jint) AUDIO_JAVA_SUCCESS; 24718db49a46259020387c40fff36d92edc1087a366Glenn Kasten 2489066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project // failure: 2499066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectnative_init_failure: 2504bac5a3928d57ac193141eb6d45f3b39200d27deJean-Michel Trivi env->DeleteGlobalRef(lpCallbackData->audioRecord_class); 2514bac5a3928d57ac193141eb6d45f3b39200d27deJean-Michel Trivi env->DeleteGlobalRef(lpCallbackData->audioRecord_ref); 2529066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project delete lpCallbackData; 253075e9a19ce645752f8282bc19c91b25978a7dc52Ashok Bhat env->SetLongField(thiz, javaAudioRecordFields.nativeCallbackCookie, 0); 25418db49a46259020387c40fff36d92edc1087a366Glenn Kasten 255075e9a19ce645752f8282bc19c91b25978a7dc52Ashok Bhat return (jint) AUDIORECORD_ERROR_SETUP_NATIVEINITFAILED; 2569066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project} 2579066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 2589066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 2599066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 2609066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project// ---------------------------------------------------------------------------- 261075e9a19ce645752f8282bc19c91b25978a7dc52Ashok Bhatstatic jint 262505e5c8859f596ed58489be565d6e029314b2ac8Eric Laurentandroid_media_AudioRecord_start(JNIEnv *env, jobject thiz, jint event, jint triggerSession) 2639066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project{ 264532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent sp<AudioRecord> lpRecorder = getAudioRecord(env, thiz); 2659066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project if (lpRecorder == NULL ) { 2669066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project jniThrowException(env, "java/lang/IllegalStateException", NULL); 267bc11a696f6856624de532a6830d5db9dbbe2384cEric Laurent return (jint) AUDIO_JAVA_ERROR; 2689066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } 26918db49a46259020387c40fff36d92edc1087a366Glenn Kasten 270bc11a696f6856624de532a6830d5db9dbbe2384cEric Laurent return nativeToJavaStatus( 271505e5c8859f596ed58489be565d6e029314b2ac8Eric Laurent lpRecorder->start((AudioSystem::sync_event_t)event, triggerSession)); 2729066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project} 2739066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 2749066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 2759066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project// ---------------------------------------------------------------------------- 2769066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectstatic void 2779066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectandroid_media_AudioRecord_stop(JNIEnv *env, jobject thiz) 2789066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project{ 279532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent sp<AudioRecord> lpRecorder = getAudioRecord(env, thiz); 2809066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project if (lpRecorder == NULL ) { 2819066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project jniThrowException(env, "java/lang/IllegalStateException", NULL); 2829066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project return; 2839066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } 2849066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 2859066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project lpRecorder->stop(); 28671f2cf116aab893e224056c38ab146bd1538dd3eSteve Block //ALOGV("Called lpRecorder->stop()"); 2879066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project} 2889066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 2899066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 2909066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project// ---------------------------------------------------------------------------- 291532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent 292532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent#define CALLBACK_COND_WAIT_TIMEOUT_MS 1000 293e6335c904aafa17795b059a5d83b8b7db6c761c8Dave Sparksstatic void android_media_AudioRecord_release(JNIEnv *env, jobject thiz) { 294532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent sp<AudioRecord> lpRecorder = setAudioRecord(env, thiz, 0); 295532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent if (lpRecorder == NULL) { 296532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent return; 297532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent } 2982fbf25b50f8534c78f3b9063d35a0b5970ff8f1eGlenn Kasten ALOGV("About to delete lpRecorder: %p", lpRecorder.get()); 299532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent lpRecorder->stop(); 300e6335c904aafa17795b059a5d83b8b7db6c761c8Dave Sparks 301075e9a19ce645752f8282bc19c91b25978a7dc52Ashok Bhat audiorecord_callback_cookie *lpCookie = (audiorecord_callback_cookie *)env->GetLongField( 302e6335c904aafa17795b059a5d83b8b7db6c761c8Dave Sparks thiz, javaAudioRecordFields.nativeCallbackCookie); 3039066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 304e6335c904aafa17795b059a5d83b8b7db6c761c8Dave Sparks // reset the native resources in the Java object so any attempt to access 305e6335c904aafa17795b059a5d83b8b7db6c761c8Dave Sparks // them after a call to release fails. 306075e9a19ce645752f8282bc19c91b25978a7dc52Ashok Bhat env->SetLongField(thiz, javaAudioRecordFields.nativeCallbackCookie, 0); 307e6335c904aafa17795b059a5d83b8b7db6c761c8Dave Sparks 3089066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project // delete the callback information 3099066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project if (lpCookie) { 310532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent Mutex::Autolock l(sLock); 3112fbf25b50f8534c78f3b9063d35a0b5970ff8f1eGlenn Kasten ALOGV("deleting lpCookie: %p", lpCookie); 312532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent while (lpCookie->busy) { 313532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent if (lpCookie->cond.waitRelative(sLock, 314532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent milliseconds(CALLBACK_COND_WAIT_TIMEOUT_MS)) != 315532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent NO_ERROR) { 316532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent break; 317532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent } 318532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent } 319532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent sAudioRecordCallBackCookies.remove(lpCookie); 3204bac5a3928d57ac193141eb6d45f3b39200d27deJean-Michel Trivi env->DeleteGlobalRef(lpCookie->audioRecord_class); 3214bac5a3928d57ac193141eb6d45f3b39200d27deJean-Michel Trivi env->DeleteGlobalRef(lpCookie->audioRecord_ref); 3229066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project delete lpCookie; 3239066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } 3249066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project} 3259066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 3269066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 3279066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project// ---------------------------------------------------------------------------- 328e6335c904aafa17795b059a5d83b8b7db6c761c8Dave Sparksstatic void android_media_AudioRecord_finalize(JNIEnv *env, jobject thiz) { 329e6335c904aafa17795b059a5d83b8b7db6c761c8Dave Sparks android_media_AudioRecord_release(env, thiz); 3309066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project} 3319066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 3329066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 3339066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project// ---------------------------------------------------------------------------- 3349066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectstatic jint android_media_AudioRecord_readInByteArray(JNIEnv *env, jobject thiz, 3359066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project jbyteArray javaAudioData, 3369066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project jint offsetInBytes, jint sizeInBytes) { 3379066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project jbyte* recordBuff = NULL; 3389066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project // get the audio recorder from which we'll read new audio samples 339532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent sp<AudioRecord> lpRecorder = getAudioRecord(env, thiz); 3409066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project if (lpRecorder == NULL) { 3413762c311729fe9f3af085c14c5c1fb471d994c03Steve Block ALOGE("Unable to retrieve AudioRecord object, can't record"); 3429066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project return 0; 3439066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } 3449066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 3459066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project if (!javaAudioData) { 3463762c311729fe9f3af085c14c5c1fb471d994c03Steve Block ALOGE("Invalid Java array to store recorded audio, can't record"); 3479066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project return 0; 3489066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } 3499066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 3509066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project // get the pointer to where we'll record the audio 351421ddc014b31478ba0ef115e098d8780a35af331Eric Laurent // NOTE: We may use GetPrimitiveArrayCritical() when the JNI implementation changes in such 352421ddc014b31478ba0ef115e098d8780a35af331Eric Laurent // a way that it becomes much more efficient. When doing so, we will have to prevent the 353421ddc014b31478ba0ef115e098d8780a35af331Eric Laurent // AudioSystem callback to be called while in critical section (in case of media server 354421ddc014b31478ba0ef115e098d8780a35af331Eric Laurent // process crash for instance) 355421ddc014b31478ba0ef115e098d8780a35af331Eric Laurent recordBuff = (jbyte *)env->GetByteArrayElements(javaAudioData, NULL); 3569066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 3579066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project if (recordBuff == NULL) { 3583762c311729fe9f3af085c14c5c1fb471d994c03Steve Block ALOGE("Error retrieving destination for recorded audio data, 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 // read the new audio data from the native AudioRecord object 3639066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project ssize_t recorderBuffSize = lpRecorder->frameCount()*lpRecorder->frameSize(); 36418db49a46259020387c40fff36d92edc1087a366Glenn Kasten ssize_t readSize = lpRecorder->read(recordBuff + offsetInBytes, 36518db49a46259020387c40fff36d92edc1087a366Glenn Kasten sizeInBytes > (jint)recorderBuffSize ? 3669066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project (jint)recorderBuffSize : sizeInBytes ); 367421ddc014b31478ba0ef115e098d8780a35af331Eric Laurent env->ReleaseByteArrayElements(javaAudioData, recordBuff, 0); 3689066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 369357263da0ec2bc2be9d48c1becc3d94288c2f3edEric Laurent if (readSize < 0) { 370bc11a696f6856624de532a6830d5db9dbbe2384cEric Laurent readSize = (jint)AUDIO_JAVA_INVALID_OPERATION; 371357263da0ec2bc2be9d48c1becc3d94288c2f3edEric Laurent } 3729066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project return (jint) readSize; 3739066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project} 3749066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 3759066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project// ---------------------------------------------------------------------------- 3769066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectstatic jint android_media_AudioRecord_readInShortArray(JNIEnv *env, jobject thiz, 3779066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project jshortArray javaAudioData, 3789066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project jint offsetInShorts, jint sizeInShorts) { 3799066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 3803d4c64191a181aac0922abbd56f4046d6b7ec31cEric Laurent jshort* recordBuff = NULL; 3813d4c64191a181aac0922abbd56f4046d6b7ec31cEric Laurent // get the audio recorder from which we'll read new audio samples 3823d4c64191a181aac0922abbd56f4046d6b7ec31cEric Laurent sp<AudioRecord> lpRecorder = getAudioRecord(env, thiz); 3833d4c64191a181aac0922abbd56f4046d6b7ec31cEric Laurent if (lpRecorder == NULL) { 3843d4c64191a181aac0922abbd56f4046d6b7ec31cEric Laurent ALOGE("Unable to retrieve AudioRecord object, can't record"); 3853d4c64191a181aac0922abbd56f4046d6b7ec31cEric Laurent return 0; 3863f0af8be9e11112b03fc487bc2cc393286b923b5Glenn Kasten } 3873d4c64191a181aac0922abbd56f4046d6b7ec31cEric Laurent 3883d4c64191a181aac0922abbd56f4046d6b7ec31cEric Laurent if (!javaAudioData) { 3893d4c64191a181aac0922abbd56f4046d6b7ec31cEric Laurent ALOGE("Invalid Java array to store recorded audio, can't record"); 3903d4c64191a181aac0922abbd56f4046d6b7ec31cEric Laurent return 0; 3913d4c64191a181aac0922abbd56f4046d6b7ec31cEric Laurent } 3923d4c64191a181aac0922abbd56f4046d6b7ec31cEric Laurent 3933d4c64191a181aac0922abbd56f4046d6b7ec31cEric Laurent // get the pointer to where we'll record the audio 3943d4c64191a181aac0922abbd56f4046d6b7ec31cEric Laurent // NOTE: We may use GetPrimitiveArrayCritical() when the JNI implementation changes in such 3953d4c64191a181aac0922abbd56f4046d6b7ec31cEric Laurent // a way that it becomes much more efficient. When doing so, we will have to prevent the 3963d4c64191a181aac0922abbd56f4046d6b7ec31cEric Laurent // AudioSystem callback to be called while in critical section (in case of media server 3973d4c64191a181aac0922abbd56f4046d6b7ec31cEric Laurent // process crash for instance) 3983d4c64191a181aac0922abbd56f4046d6b7ec31cEric Laurent recordBuff = (jshort *)env->GetShortArrayElements(javaAudioData, NULL); 3993d4c64191a181aac0922abbd56f4046d6b7ec31cEric Laurent 4003d4c64191a181aac0922abbd56f4046d6b7ec31cEric Laurent if (recordBuff == NULL) { 4013d4c64191a181aac0922abbd56f4046d6b7ec31cEric Laurent ALOGE("Error retrieving destination for recorded audio data, can't record"); 4023d4c64191a181aac0922abbd56f4046d6b7ec31cEric Laurent return 0; 4033d4c64191a181aac0922abbd56f4046d6b7ec31cEric Laurent } 4043d4c64191a181aac0922abbd56f4046d6b7ec31cEric Laurent 4053d4c64191a181aac0922abbd56f4046d6b7ec31cEric Laurent // read the new audio data from the native AudioRecord object 4063d4c64191a181aac0922abbd56f4046d6b7ec31cEric Laurent const size_t recorderBuffSize = lpRecorder->frameCount()*lpRecorder->frameSize(); 4073d4c64191a181aac0922abbd56f4046d6b7ec31cEric Laurent const size_t sizeInBytes = sizeInShorts * sizeof(short); 4083d4c64191a181aac0922abbd56f4046d6b7ec31cEric Laurent ssize_t readSize = lpRecorder->read(recordBuff + offsetInShorts * sizeof(short), 4093d4c64191a181aac0922abbd56f4046d6b7ec31cEric Laurent sizeInBytes > recorderBuffSize ? 4103d4c64191a181aac0922abbd56f4046d6b7ec31cEric Laurent recorderBuffSize : sizeInBytes); 4113d4c64191a181aac0922abbd56f4046d6b7ec31cEric Laurent 4123d4c64191a181aac0922abbd56f4046d6b7ec31cEric Laurent env->ReleaseShortArrayElements(javaAudioData, recordBuff, 0); 4133d4c64191a181aac0922abbd56f4046d6b7ec31cEric Laurent 4143d4c64191a181aac0922abbd56f4046d6b7ec31cEric Laurent if (readSize < 0) { 415bc11a696f6856624de532a6830d5db9dbbe2384cEric Laurent readSize = (jint)AUDIO_JAVA_INVALID_OPERATION; 4163d4c64191a181aac0922abbd56f4046d6b7ec31cEric Laurent } else { 4173d4c64191a181aac0922abbd56f4046d6b7ec31cEric Laurent readSize /= sizeof(short); 4183d4c64191a181aac0922abbd56f4046d6b7ec31cEric Laurent } 4193d4c64191a181aac0922abbd56f4046d6b7ec31cEric Laurent return (jint) readSize; 4209066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project} 4219066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 4229066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project// ---------------------------------------------------------------------------- 4239066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectstatic jint android_media_AudioRecord_readInDirectBuffer(JNIEnv *env, jobject thiz, 4249066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project jobject jBuffer, jint sizeInBytes) { 4259066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project // get the audio recorder from which we'll read new audio samples 426532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent sp<AudioRecord> lpRecorder = getAudioRecord(env, thiz); 42718db49a46259020387c40fff36d92edc1087a366Glenn Kasten if (lpRecorder==NULL) 4289066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project return 0; 4299066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 4309066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project // direct buffer and direct access supported? 4319066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project long capacity = env->GetDirectBufferCapacity(jBuffer); 43218db49a46259020387c40fff36d92edc1087a366Glenn Kasten if (capacity == -1) { 4339066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project // buffer direct access is not supported 4343762c311729fe9f3af085c14c5c1fb471d994c03Steve Block ALOGE("Buffer direct access is not supported, can't record"); 4359066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project return 0; 4369066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } 43771f2cf116aab893e224056c38ab146bd1538dd3eSteve Block //ALOGV("capacity = %ld", capacity); 4389066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project jbyte* nativeFromJavaBuf = (jbyte*) env->GetDirectBufferAddress(jBuffer); 43918db49a46259020387c40fff36d92edc1087a366Glenn Kasten if (nativeFromJavaBuf==NULL) { 4403762c311729fe9f3af085c14c5c1fb471d994c03Steve Block ALOGE("Buffer direct access is not supported, can't record"); 4419066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project return 0; 44218db49a46259020387c40fff36d92edc1087a366Glenn Kasten } 4439066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 4449066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project // read new data from the recorder 445357263da0ec2bc2be9d48c1becc3d94288c2f3edEric Laurent ssize_t readSize = lpRecorder->read(nativeFromJavaBuf, 4469066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project capacity < sizeInBytes ? capacity : sizeInBytes); 447357263da0ec2bc2be9d48c1becc3d94288c2f3edEric Laurent if (readSize < 0) { 448bc11a696f6856624de532a6830d5db9dbbe2384cEric Laurent readSize = (jint)AUDIO_JAVA_INVALID_OPERATION; 449357263da0ec2bc2be9d48c1becc3d94288c2f3edEric Laurent } 450357263da0ec2bc2be9d48c1becc3d94288c2f3edEric Laurent return (jint)readSize; 4519066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project} 4529066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 4539066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 4549066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project// ---------------------------------------------------------------------------- 45518db49a46259020387c40fff36d92edc1087a366Glenn Kastenstatic jint android_media_AudioRecord_set_marker_pos(JNIEnv *env, jobject thiz, 4569066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project jint markerPos) { 45718db49a46259020387c40fff36d92edc1087a366Glenn Kasten 458532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent sp<AudioRecord> lpRecorder = getAudioRecord(env, thiz); 459532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent if (lpRecorder == NULL) { 4609066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project jniThrowException(env, "java/lang/IllegalStateException", 4619066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project "Unable to retrieve AudioRecord pointer for setMarkerPosition()"); 462bc11a696f6856624de532a6830d5db9dbbe2384cEric Laurent return (jint)AUDIO_JAVA_ERROR; 4639066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } 464bc11a696f6856624de532a6830d5db9dbbe2384cEric Laurent return nativeToJavaStatus( lpRecorder->setMarkerPosition(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_get_marker_pos(JNIEnv *env, jobject thiz) { 47018db49a46259020387c40fff36d92edc1087a366Glenn Kasten 471532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent sp<AudioRecord> lpRecorder = getAudioRecord(env, thiz); 4729066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project uint32_t markerPos = 0; 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 getMarkerPosition()"); 477bc11a696f6856624de532a6830d5db9dbbe2384cEric Laurent return (jint)AUDIO_JAVA_ERROR; 4789066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } 479532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent lpRecorder->getMarkerPosition(&markerPos); 480532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent return (jint)markerPos; 4819066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project} 4829066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 4839066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 4849066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project// ---------------------------------------------------------------------------- 4859066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectstatic jint android_media_AudioRecord_set_pos_update_period(JNIEnv *env, jobject thiz, 4869066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project jint period) { 48718db49a46259020387c40fff36d92edc1087a366Glenn Kasten 488532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent sp<AudioRecord> lpRecorder = getAudioRecord(env, thiz); 48918db49a46259020387c40fff36d92edc1087a366Glenn Kasten 490532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent if (lpRecorder == NULL) { 4919066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project jniThrowException(env, "java/lang/IllegalStateException", 4929066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project "Unable to retrieve AudioRecord pointer for setPositionUpdatePeriod()"); 493bc11a696f6856624de532a6830d5db9dbbe2384cEric Laurent return (jint)AUDIO_JAVA_ERROR; 49418db49a46259020387c40fff36d92edc1087a366Glenn Kasten } 495bc11a696f6856624de532a6830d5db9dbbe2384cEric Laurent return nativeToJavaStatus( lpRecorder->setPositionUpdatePeriod(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 Projectstatic jint android_media_AudioRecord_get_pos_update_period(JNIEnv *env, jobject thiz) { 50118db49a46259020387c40fff36d92edc1087a366Glenn Kasten 502532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent sp<AudioRecord> lpRecorder = getAudioRecord(env, thiz); 5039066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project uint32_t period = 0; 50418db49a46259020387c40fff36d92edc1087a366Glenn Kasten 505532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent if (lpRecorder == NULL) { 5069066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project jniThrowException(env, "java/lang/IllegalStateException", 5079066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project "Unable to retrieve AudioRecord pointer for getPositionUpdatePeriod()"); 508bc11a696f6856624de532a6830d5db9dbbe2384cEric Laurent return (jint)AUDIO_JAVA_ERROR; 5099066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } 510532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent lpRecorder->getPositionUpdatePeriod(&period); 511532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent return (jint)period; 5129066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project} 5139066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 5149066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 5159066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project// ---------------------------------------------------------------------------- 5169066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project// returns the minimum required size for the successful creation of an AudioRecord instance. 5179066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project// returns 0 if the parameter combination is not supported. 5189066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project// return -1 if there was an error querying the buffer size. 5199066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectstatic jint android_media_AudioRecord_get_min_buff_size(JNIEnv *env, jobject thiz, 5205b8fd44365f03601aaba41879ac18e70ce814a0fGlenn Kasten jint sampleRateInHertz, jint channelCount, jint audioFormat) { 521c3308074cd6f06bbb35c1999b5260f938742a9e7Chia-chi Yeh 522532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent ALOGV(">> android_media_AudioRecord_get_min_buff_size(%d, %d, %d)", 5235b8fd44365f03601aaba41879ac18e70ce814a0fGlenn Kasten sampleRateInHertz, channelCount, audioFormat); 524c3308074cd6f06bbb35c1999b5260f938742a9e7Chia-chi Yeh 525fd1e3dfde2a70906c50144a7b1b53593ec1f7891Glenn Kasten size_t frameCount = 0; 526a5a42384367230b8e16cf441d5dcf4225332f9d7Glenn Kasten audio_format_t format = audioFormatToNative(audioFormat); 527c3308074cd6f06bbb35c1999b5260f938742a9e7Chia-chi Yeh status_t result = AudioRecord::getMinFrameCount(&frameCount, 528c3308074cd6f06bbb35c1999b5260f938742a9e7Chia-chi Yeh sampleRateInHertz, 529a5a42384367230b8e16cf441d5dcf4225332f9d7Glenn Kasten format, 5305b8fd44365f03601aaba41879ac18e70ce814a0fGlenn Kasten audio_channel_in_mask_from_count(channelCount)); 531c3308074cd6f06bbb35c1999b5260f938742a9e7Chia-chi Yeh 532c3308074cd6f06bbb35c1999b5260f938742a9e7Chia-chi Yeh if (result == BAD_VALUE) { 533c3308074cd6f06bbb35c1999b5260f938742a9e7Chia-chi Yeh return 0; 534c3308074cd6f06bbb35c1999b5260f938742a9e7Chia-chi Yeh } 535c3308074cd6f06bbb35c1999b5260f938742a9e7Chia-chi Yeh if (result != NO_ERROR) { 536c3308074cd6f06bbb35c1999b5260f938742a9e7Chia-chi Yeh return -1; 5379066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } 5385b8fd44365f03601aaba41879ac18e70ce814a0fGlenn Kasten return frameCount * channelCount * audio_bytes_per_sample(format); 5399066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project} 5409066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 5419066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 5429066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project// ---------------------------------------------------------------------------- 5439066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project// ---------------------------------------------------------------------------- 5449066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectstatic JNINativeMethod gMethods[] = { 5459066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project // name, signature, funcPtr 546505e5c8859f596ed58489be565d6e029314b2ac8Eric Laurent {"native_start", "(II)I", (void *)android_media_AudioRecord_start}, 5479066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project {"native_stop", "()V", (void *)android_media_AudioRecord_stop}, 54844ff4cd8be50768d5bd471bc6c034acefa0b59caEric Laurent {"native_setup", "(Ljava/lang/Object;IIIII[I)I", 5499066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project (void *)android_media_AudioRecord_setup}, 5509066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project {"native_finalize", "()V", (void *)android_media_AudioRecord_finalize}, 5519066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project {"native_release", "()V", (void *)android_media_AudioRecord_release}, 55218db49a46259020387c40fff36d92edc1087a366Glenn Kasten {"native_read_in_byte_array", 5539066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project "([BII)I", (void *)android_media_AudioRecord_readInByteArray}, 5549066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project {"native_read_in_short_array", 5559066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project "([SII)I", (void *)android_media_AudioRecord_readInShortArray}, 5569066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project {"native_read_in_direct_buffer","(Ljava/lang/Object;I)I", 5579066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project (void *)android_media_AudioRecord_readInDirectBuffer}, 5589066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project {"native_set_marker_pos","(I)I", (void *)android_media_AudioRecord_set_marker_pos}, 5599066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project {"native_get_marker_pos","()I", (void *)android_media_AudioRecord_get_marker_pos}, 5609066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project {"native_set_pos_update_period", 5619066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project "(I)I", (void *)android_media_AudioRecord_set_pos_update_period}, 5629066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project {"native_get_pos_update_period", 5639066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project "()I", (void *)android_media_AudioRecord_get_pos_update_period}, 5649066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project {"native_get_min_buff_size", 5659066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project "(III)I", (void *)android_media_AudioRecord_get_min_buff_size}, 5669066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}; 5679066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 5689066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project// field names found in android/media/AudioRecord.java 5699066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project#define JAVA_POSTEVENT_CALLBACK_NAME "postEventFromNative" 5709066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project#define JAVA_NATIVERECORDERINJAVAOBJ_FIELD_NAME "mNativeRecorderInJavaObj" 5719066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project#define JAVA_NATIVECALLBACKINFO_FIELD_NAME "mNativeCallbackCookie" 5729066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 5739066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project// ---------------------------------------------------------------------------- 5749066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectint register_android_media_AudioRecord(JNIEnv *env) 5759066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project{ 5769066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project javaAudioRecordFields.postNativeEventInJava = NULL; 5779066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project javaAudioRecordFields.nativeRecorderInJavaObj = NULL; 5789066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project javaAudioRecordFields.nativeCallbackCookie = NULL; 57918db49a46259020387c40fff36d92edc1087a366Glenn Kasten 5809066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 5819066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project // Get the AudioRecord class 58246e18c11d46a2bc1a46174f963d0ed1224d94cd2Brian Carlstrom jclass audioRecordClass = env->FindClass(kClassPathName); 58346e18c11d46a2bc1a46174f963d0ed1224d94cd2Brian Carlstrom if (audioRecordClass == NULL) { 5843762c311729fe9f3af085c14c5c1fb471d994c03Steve Block ALOGE("Can't find %s", kClassPathName); 5859066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project return -1; 5869066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } 5879066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project // Get the postEvent method 5889066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project javaAudioRecordFields.postNativeEventInJava = env->GetStaticMethodID( 58946e18c11d46a2bc1a46174f963d0ed1224d94cd2Brian Carlstrom audioRecordClass, 5909066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project JAVA_POSTEVENT_CALLBACK_NAME, "(Ljava/lang/Object;IIILjava/lang/Object;)V"); 5919066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project if (javaAudioRecordFields.postNativeEventInJava == NULL) { 5923762c311729fe9f3af085c14c5c1fb471d994c03Steve Block ALOGE("Can't find AudioRecord.%s", JAVA_POSTEVENT_CALLBACK_NAME); 5939066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project return -1; 5949066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } 5959066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 5969066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project // Get the variables 5979066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project // mNativeRecorderInJavaObj 59818db49a46259020387c40fff36d92edc1087a366Glenn Kasten javaAudioRecordFields.nativeRecorderInJavaObj = 59946e18c11d46a2bc1a46174f963d0ed1224d94cd2Brian Carlstrom env->GetFieldID(audioRecordClass, 600075e9a19ce645752f8282bc19c91b25978a7dc52Ashok Bhat JAVA_NATIVERECORDERINJAVAOBJ_FIELD_NAME, "J"); 6019066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project if (javaAudioRecordFields.nativeRecorderInJavaObj == NULL) { 6023762c311729fe9f3af085c14c5c1fb471d994c03Steve Block ALOGE("Can't find AudioRecord.%s", JAVA_NATIVERECORDERINJAVAOBJ_FIELD_NAME); 6039066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project return -1; 6049066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } 6059066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project // mNativeCallbackCookie 6069066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project javaAudioRecordFields.nativeCallbackCookie = env->GetFieldID( 60746e18c11d46a2bc1a46174f963d0ed1224d94cd2Brian Carlstrom audioRecordClass, 608075e9a19ce645752f8282bc19c91b25978a7dc52Ashok Bhat JAVA_NATIVECALLBACKINFO_FIELD_NAME, "J"); 6099066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project if (javaAudioRecordFields.nativeCallbackCookie == NULL) { 6103762c311729fe9f3af085c14c5c1fb471d994c03Steve Block ALOGE("Can't find AudioRecord.%s", JAVA_NATIVECALLBACKINFO_FIELD_NAME); 6119066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project return -1; 6129066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } 6139066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 6149066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project return AndroidRuntime::registerNativeMethods(env, 6159066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project kClassPathName, gMethods, NELEM(gMethods)); 6169066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project} 6179066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 6189066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project// ---------------------------------------------------------------------------- 619