android_media_AudioRecord.cpp revision bdad1afa92a6e7cd688061d5432f25a3dd15b64e
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"; 38701d6ff12f36bf5e9de0dafdaced06744fd411ebJean-Michel Trivistatic const char* const kAudioAttributesClassPathName = "android/media/AudioAttributes"; 399066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 40701d6ff12f36bf5e9de0dafdaced06744fd411ebJean-Michel Trivistruct audio_record_fields_t { 419066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project // these fields provide access from C++ to the... 429066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project jmethodID postNativeEventInJava; //... event post callback method 439066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project jfieldID nativeRecorderInJavaObj; // provides access to the C++ AudioRecord object 449066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project jfieldID nativeCallbackCookie; // provides access to the AudioRecord callback data 459066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}; 46701d6ff12f36bf5e9de0dafdaced06744fd411ebJean-Michel Trivistruct audio_attributes_fields_t { 47701d6ff12f36bf5e9de0dafdaced06744fd411ebJean-Michel Trivi jfieldID fieldRecSource; // AudioAttributes.mSource 48bdad1afa92a6e7cd688061d5432f25a3dd15b64eEric Laurent jfieldID fieldFlags; // AudioAttributes.mFlags 49701d6ff12f36bf5e9de0dafdaced06744fd411ebJean-Michel Trivi jfieldID fieldFormattedTags;// AudioAttributes.mFormattedTags 50701d6ff12f36bf5e9de0dafdaced06744fd411ebJean-Michel Trivi}; 51701d6ff12f36bf5e9de0dafdaced06744fd411ebJean-Michel Trivistatic audio_attributes_fields_t javaAudioAttrFields; 52701d6ff12f36bf5e9de0dafdaced06744fd411ebJean-Michel Trivistatic audio_record_fields_t javaAudioRecordFields; 539066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 549066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectstruct audiorecord_callback_cookie { 559066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project jclass audioRecord_class; 569066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project jobject audioRecord_ref; 57532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent bool busy; 58532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent Condition cond; 59532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent}; 609066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 61532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurentstatic Mutex sLock; 62532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurentstatic SortedVector <audiorecord_callback_cookie *> sAudioRecordCallBackCookies; 63e6335c904aafa17795b059a5d83b8b7db6c761c8Dave Sparks 649066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project// ---------------------------------------------------------------------------- 659066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 669066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project#define AUDIORECORD_ERROR_SETUP_ZEROFRAMECOUNT -16 67a553c25b33c99b345cf1c8688f8df0ed8df14e5aEric Laurent#define AUDIORECORD_ERROR_SETUP_INVALIDCHANNELMASK -17 689066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project#define AUDIORECORD_ERROR_SETUP_INVALIDFORMAT -18 694bc035a65cac177be9294e69f110497e3b6e34e6Eric Laurent#define AUDIORECORD_ERROR_SETUP_INVALIDSOURCE -19 709066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project#define AUDIORECORD_ERROR_SETUP_NATIVEINITFAILED -20 719066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 729066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project// ---------------------------------------------------------------------------- 739066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectstatic void recorderCallback(int event, void* user, void *info) { 74532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent 75532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent audiorecord_callback_cookie *callbackInfo = (audiorecord_callback_cookie *)user; 76532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent { 77532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent Mutex::Autolock l(sLock); 78532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent if (sAudioRecordCallBackCookies.indexOf(callbackInfo) < 0) { 79532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent return; 80532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent } 81532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent callbackInfo->busy = true; 82532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent } 8318db49a46259020387c40fff36d92edc1087a366Glenn Kasten 845b1576ccbd9ab43d5e3238589cf5b43a4834447dGlenn Kasten switch (event) { 855b1576ccbd9ab43d5e3238589cf5b43a4834447dGlenn Kasten case AudioRecord::EVENT_MARKER: { 869066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project JNIEnv *env = AndroidRuntime::getJNIEnv(); 87a667ff30e4fb6f741c2a930e7c31ef7e962d3b40Glenn Kasten if (user != NULL && env != NULL) { 889066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project env->CallStaticVoidMethod( 8918db49a46259020387c40fff36d92edc1087a366Glenn Kasten callbackInfo->audioRecord_class, 909066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project javaAudioRecordFields.postNativeEventInJava, 919066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project callbackInfo->audioRecord_ref, event, 0,0, NULL); 929066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project if (env->ExceptionCheck()) { 939066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project env->ExceptionDescribe(); 949066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project env->ExceptionClear(); 959066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } 969066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } 975b1576ccbd9ab43d5e3238589cf5b43a4834447dGlenn Kasten } break; 989066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 995b1576ccbd9ab43d5e3238589cf5b43a4834447dGlenn Kasten case AudioRecord::EVENT_NEW_POS: { 1009066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project JNIEnv *env = AndroidRuntime::getJNIEnv(); 101a667ff30e4fb6f741c2a930e7c31ef7e962d3b40Glenn Kasten if (user != NULL && env != NULL) { 1029066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project env->CallStaticVoidMethod( 10318db49a46259020387c40fff36d92edc1087a366Glenn Kasten callbackInfo->audioRecord_class, 1049066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project javaAudioRecordFields.postNativeEventInJava, 1059066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project callbackInfo->audioRecord_ref, event, 0,0, NULL); 1069066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project if (env->ExceptionCheck()) { 1079066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project env->ExceptionDescribe(); 1089066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project env->ExceptionClear(); 1099066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } 1109066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } 1115b1576ccbd9ab43d5e3238589cf5b43a4834447dGlenn Kasten } break; 1129066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } 1135b1576ccbd9ab43d5e3238589cf5b43a4834447dGlenn Kasten 114532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent { 115532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent Mutex::Autolock l(sLock); 116532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent callbackInfo->busy = false; 117532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent callbackInfo->cond.broadcast(); 118532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent } 1199066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project} 1209066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 121532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent// ---------------------------------------------------------------------------- 122532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurentstatic sp<AudioRecord> getAudioRecord(JNIEnv* env, jobject thiz) 123532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent{ 124532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent Mutex::Autolock l(sLock); 125532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent AudioRecord* const ar = 126075e9a19ce645752f8282bc19c91b25978a7dc52Ashok Bhat (AudioRecord*)env->GetLongField(thiz, javaAudioRecordFields.nativeRecorderInJavaObj); 127532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent return sp<AudioRecord>(ar); 128532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent} 129532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent 130532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurentstatic sp<AudioRecord> setAudioRecord(JNIEnv* env, jobject thiz, const sp<AudioRecord>& ar) 131532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent{ 132532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent Mutex::Autolock l(sLock); 133532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent sp<AudioRecord> old = 134075e9a19ce645752f8282bc19c91b25978a7dc52Ashok Bhat (AudioRecord*)env->GetLongField(thiz, javaAudioRecordFields.nativeRecorderInJavaObj); 135532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent if (ar.get()) { 136b1d90c8f60f71422196c337f1d078b68867f5710Mathias Agopian ar->incStrong((void*)setAudioRecord); 137532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent } 138532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent if (old != 0) { 139b1d90c8f60f71422196c337f1d078b68867f5710Mathias Agopian old->decStrong((void*)setAudioRecord); 140532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent } 141075e9a19ce645752f8282bc19c91b25978a7dc52Ashok Bhat env->SetLongField(thiz, javaAudioRecordFields.nativeRecorderInJavaObj, (jlong)ar.get()); 142532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent return old; 143532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent} 1449066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 1459066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project// ---------------------------------------------------------------------------- 146075e9a19ce645752f8282bc19c91b25978a7dc52Ashok Bhatstatic jint 1479066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectandroid_media_AudioRecord_setup(JNIEnv *env, jobject thiz, jobject weak_this, 148701d6ff12f36bf5e9de0dafdaced06744fd411ebJean-Michel Trivi jobject jaa, jint sampleRateInHertz, jint channelMask, 14933c437df39029b70d4ffafac2ca31bd71eafd0c6Glenn Kasten // Java channel masks map directly to the native definition 15044ff4cd8be50768d5bd471bc6c034acefa0b59caEric Laurent jint audioFormat, jint buffSizeInBytes, jintArray jSession) 1519066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project{ 15271f2cf116aab893e224056c38ab146bd1538dd3eSteve Block //ALOGV(">> Entering android_media_AudioRecord_setup"); 15333c437df39029b70d4ffafac2ca31bd71eafd0c6Glenn Kasten //ALOGV("sampleRate=%d, audioFormat=%d, channel mask=%x, buffSizeInBytes=%d", 15433c437df39029b70d4ffafac2ca31bd71eafd0c6Glenn Kasten // sampleRateInHertz, audioFormat, channelMask, buffSizeInBytes); 1559066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 156701d6ff12f36bf5e9de0dafdaced06744fd411ebJean-Michel Trivi if (jaa == 0) { 157701d6ff12f36bf5e9de0dafdaced06744fd411ebJean-Michel Trivi ALOGE("Error creating AudioRecord: invalid audio attributes"); 158701d6ff12f36bf5e9de0dafdaced06744fd411ebJean-Michel Trivi return (jint) AUDIO_JAVA_ERROR; 159701d6ff12f36bf5e9de0dafdaced06744fd411ebJean-Michel Trivi } 160701d6ff12f36bf5e9de0dafdaced06744fd411ebJean-Michel Trivi 16133c437df39029b70d4ffafac2ca31bd71eafd0c6Glenn Kasten if (!audio_is_input_channel(channelMask)) { 16233c437df39029b70d4ffafac2ca31bd71eafd0c6Glenn Kasten ALOGE("Error creating AudioRecord: channel mask %#x is not valid.", channelMask); 163075e9a19ce645752f8282bc19c91b25978a7dc52Ashok Bhat return (jint) AUDIORECORD_ERROR_SETUP_INVALIDCHANNELMASK; 1649066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } 165dbf55665c5c4802cbfc56463198805be48ec75afEric Laurent uint32_t channelCount = audio_channel_count_from_in_mask(channelMask); 1669066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 1679066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project // compare the format against the Java constants 168a5a42384367230b8e16cf441d5dcf4225332f9d7Glenn Kasten audio_format_t format = audioFormatToNative(audioFormat); 169a5a42384367230b8e16cf441d5dcf4225332f9d7Glenn Kasten if (format == AUDIO_FORMAT_INVALID) { 170a9838afbe3ef58b63e5d52060d5ff2776c1c603aGlenn Kasten ALOGE("Error creating AudioRecord: unsupported audio format %d.", audioFormat); 171075e9a19ce645752f8282bc19c91b25978a7dc52Ashok Bhat return (jint) AUDIORECORD_ERROR_SETUP_INVALIDFORMAT; 1729066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } 1739066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 174a5a42384367230b8e16cf441d5dcf4225332f9d7Glenn Kasten size_t bytesPerSample = audio_bytes_per_sample(format); 1759066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 1769066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project if (buffSizeInBytes == 0) { 1773762c311729fe9f3af085c14c5c1fb471d994c03Steve Block ALOGE("Error creating AudioRecord: frameCount is 0."); 178075e9a19ce645752f8282bc19c91b25978a7dc52Ashok Bhat return (jint) AUDIORECORD_ERROR_SETUP_ZEROFRAMECOUNT; 1799066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } 1805b8fd44365f03601aaba41879ac18e70ce814a0fGlenn Kasten size_t frameSize = channelCount * bytesPerSample; 1819066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project size_t frameCount = buffSizeInBytes / frameSize; 18218db49a46259020387c40fff36d92edc1087a366Glenn Kasten 183532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent jclass clazz = env->GetObjectClass(thiz); 184532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent if (clazz == NULL) { 185532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent ALOGE("Can't find %s when setting up callback.", kClassPathName); 186075e9a19ce645752f8282bc19c91b25978a7dc52Ashok Bhat return (jint) AUDIORECORD_ERROR_SETUP_NATIVEINITFAILED; 187532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent } 188532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent 18944ff4cd8be50768d5bd471bc6c034acefa0b59caEric Laurent if (jSession == NULL) { 1903762c311729fe9f3af085c14c5c1fb471d994c03Steve Block ALOGE("Error creating AudioRecord: invalid session ID pointer"); 191bc11a696f6856624de532a6830d5db9dbbe2384cEric Laurent return (jint) AUDIO_JAVA_ERROR; 19244ff4cd8be50768d5bd471bc6c034acefa0b59caEric Laurent } 19344ff4cd8be50768d5bd471bc6c034acefa0b59caEric Laurent 19444ff4cd8be50768d5bd471bc6c034acefa0b59caEric Laurent jint* nSession = (jint *) env->GetPrimitiveArrayCritical(jSession, NULL); 19544ff4cd8be50768d5bd471bc6c034acefa0b59caEric Laurent if (nSession == NULL) { 1963762c311729fe9f3af085c14c5c1fb471d994c03Steve Block ALOGE("Error creating AudioRecord: Error retrieving session id pointer"); 197bc11a696f6856624de532a6830d5db9dbbe2384cEric Laurent return (jint) AUDIO_JAVA_ERROR; 19844ff4cd8be50768d5bd471bc6c034acefa0b59caEric Laurent } 19944ff4cd8be50768d5bd471bc6c034acefa0b59caEric Laurent int sessionId = nSession[0]; 20044ff4cd8be50768d5bd471bc6c034acefa0b59caEric Laurent env->ReleasePrimitiveArrayCritical(jSession, nSession, 0); 20144ff4cd8be50768d5bd471bc6c034acefa0b59caEric Laurent nSession = NULL; 20244ff4cd8be50768d5bd471bc6c034acefa0b59caEric Laurent 2039066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project // create an uninitialized AudioRecord object 204532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent sp<AudioRecord> lpRecorder = new AudioRecord(); 20518db49a46259020387c40fff36d92edc1087a366Glenn Kasten 206701d6ff12f36bf5e9de0dafdaced06744fd411ebJean-Michel Trivi audio_attributes_t *paa = NULL; 207701d6ff12f36bf5e9de0dafdaced06744fd411ebJean-Michel Trivi // read the AudioAttributes values 208701d6ff12f36bf5e9de0dafdaced06744fd411ebJean-Michel Trivi paa = (audio_attributes_t *) calloc(1, sizeof(audio_attributes_t)); 209701d6ff12f36bf5e9de0dafdaced06744fd411ebJean-Michel Trivi const jstring jtags = 210701d6ff12f36bf5e9de0dafdaced06744fd411ebJean-Michel Trivi (jstring) env->GetObjectField(jaa, javaAudioAttrFields.fieldFormattedTags); 211701d6ff12f36bf5e9de0dafdaced06744fd411ebJean-Michel Trivi const char* tags = env->GetStringUTFChars(jtags, NULL); 212701d6ff12f36bf5e9de0dafdaced06744fd411ebJean-Michel Trivi // copying array size -1, char array for tags was calloc'd, no need to NULL-terminate it 213701d6ff12f36bf5e9de0dafdaced06744fd411ebJean-Michel Trivi strncpy(paa->tags, tags, AUDIO_ATTRIBUTES_TAGS_MAX_SIZE - 1); 214701d6ff12f36bf5e9de0dafdaced06744fd411ebJean-Michel Trivi env->ReleaseStringUTFChars(jtags, tags); 215701d6ff12f36bf5e9de0dafdaced06744fd411ebJean-Michel Trivi paa->source = (audio_source_t) env->GetIntField(jaa, javaAudioAttrFields.fieldRecSource); 216bdad1afa92a6e7cd688061d5432f25a3dd15b64eEric Laurent paa->flags = (audio_flags_mask_t)env->GetIntField(jaa, javaAudioAttrFields.fieldFlags); 217bdad1afa92a6e7cd688061d5432f25a3dd15b64eEric Laurent ALOGV("AudioRecord_setup for source=%d tags=%s flags=%08x", paa->source, paa->tags, paa->flags); 218701d6ff12f36bf5e9de0dafdaced06744fd411ebJean-Michel Trivi 219bdad1afa92a6e7cd688061d5432f25a3dd15b64eEric Laurent audio_input_flags_t flags = AUDIO_INPUT_FLAG_NONE; 220bdad1afa92a6e7cd688061d5432f25a3dd15b64eEric Laurent if (paa->flags & AUDIO_FLAG_HW_HOTWORD) { 221bdad1afa92a6e7cd688061d5432f25a3dd15b64eEric Laurent flags = AUDIO_INPUT_FLAG_HW_HOTWORD; 222bdad1afa92a6e7cd688061d5432f25a3dd15b64eEric Laurent } 2239066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project // create the callback information: 2249066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project // this data will be passed with every AudioRecord callback 225532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent audiorecord_callback_cookie *lpCallbackData = new audiorecord_callback_cookie; 2269066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project lpCallbackData->audioRecord_class = (jclass)env->NewGlobalRef(clazz); 2279066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project // we use a weak reference so the AudioRecord object can be garbage collected. 2289066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project lpCallbackData->audioRecord_ref = env->NewGlobalRef(weak_this); 229532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent lpCallbackData->busy = false; 23018db49a46259020387c40fff36d92edc1087a366Glenn Kasten 231701d6ff12f36bf5e9de0dafdaced06744fd411ebJean-Michel Trivi const status_t status = lpRecorder->set(paa->source, 2329066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project sampleRateInHertz, 2339066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project format, // word length, PCM 23433c437df39029b70d4ffafac2ca31bd71eafd0c6Glenn Kasten channelMask, 2359066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project frameCount, 2369066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project recorderCallback,// callback_t 2379066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project lpCallbackData,// void* user 2389066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 0, // notificationFrames, 23986fad47e68e344b19fabeab1156c5e6742d8dfadGlenn Kasten true, // threadCanCallJava 240bdad1afa92a6e7cd688061d5432f25a3dd15b64eEric Laurent sessionId, 241bdad1afa92a6e7cd688061d5432f25a3dd15b64eEric Laurent AudioRecord::TRANSFER_DEFAULT, 242bdad1afa92a6e7cd688061d5432f25a3dd15b64eEric Laurent flags); 2439066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 244a9838afbe3ef58b63e5d52060d5ff2776c1c603aGlenn Kasten if (status != NO_ERROR) { 245a9838afbe3ef58b63e5d52060d5ff2776c1c603aGlenn Kasten ALOGE("Error creating AudioRecord instance: initialization check failed with status %d.", 246a9838afbe3ef58b63e5d52060d5ff2776c1c603aGlenn Kasten status); 2479066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project goto native_init_failure; 2489066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } 2499066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 25044ff4cd8be50768d5bd471bc6c034acefa0b59caEric Laurent nSession = (jint *) env->GetPrimitiveArrayCritical(jSession, NULL); 25144ff4cd8be50768d5bd471bc6c034acefa0b59caEric Laurent if (nSession == NULL) { 2523762c311729fe9f3af085c14c5c1fb471d994c03Steve Block ALOGE("Error creating AudioRecord: Error retrieving session id pointer"); 25344ff4cd8be50768d5bd471bc6c034acefa0b59caEric Laurent goto native_init_failure; 25444ff4cd8be50768d5bd471bc6c034acefa0b59caEric Laurent } 255b3db213eb55acb661e4b9ea40bcc00af4b76fab9Glenn Kasten // read the audio session ID back from AudioRecord in case a new session was created during set() 25644ff4cd8be50768d5bd471bc6c034acefa0b59caEric Laurent nSession[0] = lpRecorder->getSessionId(); 25744ff4cd8be50768d5bd471bc6c034acefa0b59caEric Laurent env->ReleasePrimitiveArrayCritical(jSession, nSession, 0); 25844ff4cd8be50768d5bd471bc6c034acefa0b59caEric Laurent nSession = NULL; 25944ff4cd8be50768d5bd471bc6c034acefa0b59caEric Laurent 260532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent { // scope for the lock 261532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent Mutex::Autolock l(sLock); 262532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent sAudioRecordCallBackCookies.add(lpCallbackData); 263532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent } 26418db49a46259020387c40fff36d92edc1087a366Glenn Kasten // save our newly created C++ AudioRecord in the "nativeRecorderInJavaObj" field 2659066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project // of the Java object 266532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent setAudioRecord(env, thiz, lpRecorder); 26718db49a46259020387c40fff36d92edc1087a366Glenn Kasten 2689066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project // save our newly created callback information in the "nativeCallbackCookie" field 2699066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project // of the Java object (in mNativeCallbackCookie) so we can free the memory in finalize() 270075e9a19ce645752f8282bc19c91b25978a7dc52Ashok Bhat env->SetLongField(thiz, javaAudioRecordFields.nativeCallbackCookie, (jlong)lpCallbackData); 27118db49a46259020387c40fff36d92edc1087a366Glenn Kasten 272bc11a696f6856624de532a6830d5db9dbbe2384cEric Laurent return (jint) AUDIO_JAVA_SUCCESS; 27318db49a46259020387c40fff36d92edc1087a366Glenn Kasten 2749066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project // failure: 2759066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectnative_init_failure: 2764bac5a3928d57ac193141eb6d45f3b39200d27deJean-Michel Trivi env->DeleteGlobalRef(lpCallbackData->audioRecord_class); 2774bac5a3928d57ac193141eb6d45f3b39200d27deJean-Michel Trivi env->DeleteGlobalRef(lpCallbackData->audioRecord_ref); 2789066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project delete lpCallbackData; 279075e9a19ce645752f8282bc19c91b25978a7dc52Ashok Bhat env->SetLongField(thiz, javaAudioRecordFields.nativeCallbackCookie, 0); 28018db49a46259020387c40fff36d92edc1087a366Glenn Kasten 281075e9a19ce645752f8282bc19c91b25978a7dc52Ashok Bhat return (jint) AUDIORECORD_ERROR_SETUP_NATIVEINITFAILED; 2829066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project} 2839066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 2849066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 2859066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 2869066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project// ---------------------------------------------------------------------------- 287075e9a19ce645752f8282bc19c91b25978a7dc52Ashok Bhatstatic jint 288505e5c8859f596ed58489be565d6e029314b2ac8Eric Laurentandroid_media_AudioRecord_start(JNIEnv *env, jobject thiz, jint event, jint triggerSession) 2899066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project{ 290532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent sp<AudioRecord> lpRecorder = getAudioRecord(env, thiz); 2919066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project if (lpRecorder == NULL ) { 2929066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project jniThrowException(env, "java/lang/IllegalStateException", NULL); 293bc11a696f6856624de532a6830d5db9dbbe2384cEric Laurent return (jint) AUDIO_JAVA_ERROR; 2949066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } 29518db49a46259020387c40fff36d92edc1087a366Glenn Kasten 296bc11a696f6856624de532a6830d5db9dbbe2384cEric Laurent return nativeToJavaStatus( 297505e5c8859f596ed58489be565d6e029314b2ac8Eric Laurent lpRecorder->start((AudioSystem::sync_event_t)event, triggerSession)); 2989066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project} 2999066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 3009066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 3019066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project// ---------------------------------------------------------------------------- 3029066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectstatic void 3039066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectandroid_media_AudioRecord_stop(JNIEnv *env, jobject thiz) 3049066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project{ 305532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent sp<AudioRecord> lpRecorder = getAudioRecord(env, thiz); 3069066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project if (lpRecorder == NULL ) { 3079066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project jniThrowException(env, "java/lang/IllegalStateException", NULL); 3089066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project return; 3099066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } 3109066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 3119066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project lpRecorder->stop(); 31271f2cf116aab893e224056c38ab146bd1538dd3eSteve Block //ALOGV("Called lpRecorder->stop()"); 3139066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project} 3149066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 3159066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 3169066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project// ---------------------------------------------------------------------------- 317532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent 318532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent#define CALLBACK_COND_WAIT_TIMEOUT_MS 1000 319e6335c904aafa17795b059a5d83b8b7db6c761c8Dave Sparksstatic void android_media_AudioRecord_release(JNIEnv *env, jobject thiz) { 320532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent sp<AudioRecord> lpRecorder = setAudioRecord(env, thiz, 0); 321532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent if (lpRecorder == NULL) { 322532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent return; 323532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent } 3242fbf25b50f8534c78f3b9063d35a0b5970ff8f1eGlenn Kasten ALOGV("About to delete lpRecorder: %p", lpRecorder.get()); 325532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent lpRecorder->stop(); 326e6335c904aafa17795b059a5d83b8b7db6c761c8Dave Sparks 327075e9a19ce645752f8282bc19c91b25978a7dc52Ashok Bhat audiorecord_callback_cookie *lpCookie = (audiorecord_callback_cookie *)env->GetLongField( 328e6335c904aafa17795b059a5d83b8b7db6c761c8Dave Sparks thiz, javaAudioRecordFields.nativeCallbackCookie); 3299066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 330e6335c904aafa17795b059a5d83b8b7db6c761c8Dave Sparks // reset the native resources in the Java object so any attempt to access 331e6335c904aafa17795b059a5d83b8b7db6c761c8Dave Sparks // them after a call to release fails. 332075e9a19ce645752f8282bc19c91b25978a7dc52Ashok Bhat env->SetLongField(thiz, javaAudioRecordFields.nativeCallbackCookie, 0); 333e6335c904aafa17795b059a5d83b8b7db6c761c8Dave Sparks 3349066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project // delete the callback information 3359066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project if (lpCookie) { 336532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent Mutex::Autolock l(sLock); 3372fbf25b50f8534c78f3b9063d35a0b5970ff8f1eGlenn Kasten ALOGV("deleting lpCookie: %p", lpCookie); 338532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent while (lpCookie->busy) { 339532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent if (lpCookie->cond.waitRelative(sLock, 340532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent milliseconds(CALLBACK_COND_WAIT_TIMEOUT_MS)) != 341532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent NO_ERROR) { 342532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent break; 343532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent } 344532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent } 345532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent sAudioRecordCallBackCookies.remove(lpCookie); 3464bac5a3928d57ac193141eb6d45f3b39200d27deJean-Michel Trivi env->DeleteGlobalRef(lpCookie->audioRecord_class); 3474bac5a3928d57ac193141eb6d45f3b39200d27deJean-Michel Trivi env->DeleteGlobalRef(lpCookie->audioRecord_ref); 3489066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project delete lpCookie; 3499066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } 3509066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project} 3519066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 3529066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 3539066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project// ---------------------------------------------------------------------------- 354e6335c904aafa17795b059a5d83b8b7db6c761c8Dave Sparksstatic void android_media_AudioRecord_finalize(JNIEnv *env, jobject thiz) { 355e6335c904aafa17795b059a5d83b8b7db6c761c8Dave Sparks android_media_AudioRecord_release(env, thiz); 3569066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project} 3579066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 3589066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 3599066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project// ---------------------------------------------------------------------------- 3609066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectstatic jint android_media_AudioRecord_readInByteArray(JNIEnv *env, jobject thiz, 3619066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project jbyteArray javaAudioData, 3629066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project jint offsetInBytes, jint sizeInBytes) { 3639066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project jbyte* recordBuff = NULL; 3649066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project // get the audio recorder from which we'll read new audio samples 365532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent sp<AudioRecord> lpRecorder = getAudioRecord(env, thiz); 3669066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project if (lpRecorder == NULL) { 3673762c311729fe9f3af085c14c5c1fb471d994c03Steve Block ALOGE("Unable to retrieve AudioRecord object, can't record"); 3689066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project return 0; 3699066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } 3709066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 3719066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project if (!javaAudioData) { 3723762c311729fe9f3af085c14c5c1fb471d994c03Steve Block ALOGE("Invalid Java array to store recorded audio, can't record"); 3739066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project return 0; 3749066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } 3759066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 3769066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project // get the pointer to where we'll record the audio 377421ddc014b31478ba0ef115e098d8780a35af331Eric Laurent // NOTE: We may use GetPrimitiveArrayCritical() when the JNI implementation changes in such 378421ddc014b31478ba0ef115e098d8780a35af331Eric Laurent // a way that it becomes much more efficient. When doing so, we will have to prevent the 379421ddc014b31478ba0ef115e098d8780a35af331Eric Laurent // AudioSystem callback to be called while in critical section (in case of media server 380421ddc014b31478ba0ef115e098d8780a35af331Eric Laurent // process crash for instance) 381421ddc014b31478ba0ef115e098d8780a35af331Eric Laurent recordBuff = (jbyte *)env->GetByteArrayElements(javaAudioData, NULL); 3829066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 3839066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project if (recordBuff == NULL) { 3843762c311729fe9f3af085c14c5c1fb471d994c03Steve Block ALOGE("Error retrieving destination for recorded audio data, can't record"); 3859066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project return 0; 3869066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } 3879066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 3889066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project // read the new audio data from the native AudioRecord object 3899066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project ssize_t recorderBuffSize = lpRecorder->frameCount()*lpRecorder->frameSize(); 39018db49a46259020387c40fff36d92edc1087a366Glenn Kasten ssize_t readSize = lpRecorder->read(recordBuff + offsetInBytes, 39118db49a46259020387c40fff36d92edc1087a366Glenn Kasten sizeInBytes > (jint)recorderBuffSize ? 3929066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project (jint)recorderBuffSize : sizeInBytes ); 393421ddc014b31478ba0ef115e098d8780a35af331Eric Laurent env->ReleaseByteArrayElements(javaAudioData, recordBuff, 0); 3949066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 395357263da0ec2bc2be9d48c1becc3d94288c2f3edEric Laurent if (readSize < 0) { 396bc11a696f6856624de532a6830d5db9dbbe2384cEric Laurent readSize = (jint)AUDIO_JAVA_INVALID_OPERATION; 397357263da0ec2bc2be9d48c1becc3d94288c2f3edEric Laurent } 3989066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project return (jint) readSize; 3999066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project} 4009066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 4019066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project// ---------------------------------------------------------------------------- 4029066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectstatic jint android_media_AudioRecord_readInShortArray(JNIEnv *env, jobject thiz, 4039066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project jshortArray javaAudioData, 4049066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project jint offsetInShorts, jint sizeInShorts) { 4059066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 4063d4c64191a181aac0922abbd56f4046d6b7ec31cEric Laurent jshort* recordBuff = NULL; 4073d4c64191a181aac0922abbd56f4046d6b7ec31cEric Laurent // get the audio recorder from which we'll read new audio samples 4083d4c64191a181aac0922abbd56f4046d6b7ec31cEric Laurent sp<AudioRecord> lpRecorder = getAudioRecord(env, thiz); 4093d4c64191a181aac0922abbd56f4046d6b7ec31cEric Laurent if (lpRecorder == NULL) { 4103d4c64191a181aac0922abbd56f4046d6b7ec31cEric Laurent ALOGE("Unable to retrieve AudioRecord object, can't record"); 4113d4c64191a181aac0922abbd56f4046d6b7ec31cEric Laurent return 0; 4123f0af8be9e11112b03fc487bc2cc393286b923b5Glenn Kasten } 4133d4c64191a181aac0922abbd56f4046d6b7ec31cEric Laurent 4143d4c64191a181aac0922abbd56f4046d6b7ec31cEric Laurent if (!javaAudioData) { 4153d4c64191a181aac0922abbd56f4046d6b7ec31cEric Laurent ALOGE("Invalid Java array to store recorded audio, can't record"); 4163d4c64191a181aac0922abbd56f4046d6b7ec31cEric Laurent return 0; 4173d4c64191a181aac0922abbd56f4046d6b7ec31cEric Laurent } 4183d4c64191a181aac0922abbd56f4046d6b7ec31cEric Laurent 4193d4c64191a181aac0922abbd56f4046d6b7ec31cEric Laurent // get the pointer to where we'll record the audio 4203d4c64191a181aac0922abbd56f4046d6b7ec31cEric Laurent // NOTE: We may use GetPrimitiveArrayCritical() when the JNI implementation changes in such 4213d4c64191a181aac0922abbd56f4046d6b7ec31cEric Laurent // a way that it becomes much more efficient. When doing so, we will have to prevent the 4223d4c64191a181aac0922abbd56f4046d6b7ec31cEric Laurent // AudioSystem callback to be called while in critical section (in case of media server 4233d4c64191a181aac0922abbd56f4046d6b7ec31cEric Laurent // process crash for instance) 4243d4c64191a181aac0922abbd56f4046d6b7ec31cEric Laurent recordBuff = (jshort *)env->GetShortArrayElements(javaAudioData, NULL); 4253d4c64191a181aac0922abbd56f4046d6b7ec31cEric Laurent 4263d4c64191a181aac0922abbd56f4046d6b7ec31cEric Laurent if (recordBuff == NULL) { 4273d4c64191a181aac0922abbd56f4046d6b7ec31cEric Laurent ALOGE("Error retrieving destination for recorded audio data, can't record"); 4283d4c64191a181aac0922abbd56f4046d6b7ec31cEric Laurent return 0; 4293d4c64191a181aac0922abbd56f4046d6b7ec31cEric Laurent } 4303d4c64191a181aac0922abbd56f4046d6b7ec31cEric Laurent 4313d4c64191a181aac0922abbd56f4046d6b7ec31cEric Laurent // read the new audio data from the native AudioRecord object 4323d4c64191a181aac0922abbd56f4046d6b7ec31cEric Laurent const size_t recorderBuffSize = lpRecorder->frameCount()*lpRecorder->frameSize(); 4333d4c64191a181aac0922abbd56f4046d6b7ec31cEric Laurent const size_t sizeInBytes = sizeInShorts * sizeof(short); 4343d4c64191a181aac0922abbd56f4046d6b7ec31cEric Laurent ssize_t readSize = lpRecorder->read(recordBuff + offsetInShorts * sizeof(short), 4353d4c64191a181aac0922abbd56f4046d6b7ec31cEric Laurent sizeInBytes > recorderBuffSize ? 4363d4c64191a181aac0922abbd56f4046d6b7ec31cEric Laurent recorderBuffSize : sizeInBytes); 4373d4c64191a181aac0922abbd56f4046d6b7ec31cEric Laurent 4383d4c64191a181aac0922abbd56f4046d6b7ec31cEric Laurent env->ReleaseShortArrayElements(javaAudioData, recordBuff, 0); 4393d4c64191a181aac0922abbd56f4046d6b7ec31cEric Laurent 4403d4c64191a181aac0922abbd56f4046d6b7ec31cEric Laurent if (readSize < 0) { 441bc11a696f6856624de532a6830d5db9dbbe2384cEric Laurent readSize = (jint)AUDIO_JAVA_INVALID_OPERATION; 4423d4c64191a181aac0922abbd56f4046d6b7ec31cEric Laurent } else { 4433d4c64191a181aac0922abbd56f4046d6b7ec31cEric Laurent readSize /= sizeof(short); 4443d4c64191a181aac0922abbd56f4046d6b7ec31cEric Laurent } 4453d4c64191a181aac0922abbd56f4046d6b7ec31cEric Laurent return (jint) readSize; 4469066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project} 4479066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 4489066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project// ---------------------------------------------------------------------------- 4499066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectstatic jint android_media_AudioRecord_readInDirectBuffer(JNIEnv *env, jobject thiz, 4509066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project jobject jBuffer, jint sizeInBytes) { 4519066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project // get the audio recorder from which we'll read new audio samples 452532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent sp<AudioRecord> lpRecorder = getAudioRecord(env, thiz); 45318db49a46259020387c40fff36d92edc1087a366Glenn Kasten if (lpRecorder==NULL) 4549066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project return 0; 4559066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 4569066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project // direct buffer and direct access supported? 4579066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project long capacity = env->GetDirectBufferCapacity(jBuffer); 45818db49a46259020387c40fff36d92edc1087a366Glenn Kasten if (capacity == -1) { 4599066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project // buffer direct access is not supported 4603762c311729fe9f3af085c14c5c1fb471d994c03Steve Block ALOGE("Buffer direct access is not supported, can't record"); 4619066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project return 0; 4629066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } 46371f2cf116aab893e224056c38ab146bd1538dd3eSteve Block //ALOGV("capacity = %ld", capacity); 4649066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project jbyte* nativeFromJavaBuf = (jbyte*) env->GetDirectBufferAddress(jBuffer); 46518db49a46259020387c40fff36d92edc1087a366Glenn Kasten if (nativeFromJavaBuf==NULL) { 4663762c311729fe9f3af085c14c5c1fb471d994c03Steve Block ALOGE("Buffer direct access is not supported, can't record"); 4679066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project return 0; 46818db49a46259020387c40fff36d92edc1087a366Glenn Kasten } 4699066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 4709066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project // read new data from the recorder 471357263da0ec2bc2be9d48c1becc3d94288c2f3edEric Laurent ssize_t readSize = lpRecorder->read(nativeFromJavaBuf, 4729066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project capacity < sizeInBytes ? capacity : sizeInBytes); 473357263da0ec2bc2be9d48c1becc3d94288c2f3edEric Laurent if (readSize < 0) { 474bc11a696f6856624de532a6830d5db9dbbe2384cEric Laurent readSize = (jint)AUDIO_JAVA_INVALID_OPERATION; 475357263da0ec2bc2be9d48c1becc3d94288c2f3edEric Laurent } 476357263da0ec2bc2be9d48c1becc3d94288c2f3edEric Laurent return (jint)readSize; 4779066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project} 4789066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 4799066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 4809066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project// ---------------------------------------------------------------------------- 48118db49a46259020387c40fff36d92edc1087a366Glenn Kastenstatic jint android_media_AudioRecord_set_marker_pos(JNIEnv *env, jobject thiz, 4829066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project jint markerPos) { 48318db49a46259020387c40fff36d92edc1087a366Glenn Kasten 484532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent sp<AudioRecord> lpRecorder = getAudioRecord(env, thiz); 485532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent if (lpRecorder == NULL) { 4869066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project jniThrowException(env, "java/lang/IllegalStateException", 4879066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project "Unable to retrieve AudioRecord pointer for setMarkerPosition()"); 488bc11a696f6856624de532a6830d5db9dbbe2384cEric Laurent return (jint)AUDIO_JAVA_ERROR; 4899066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } 490bc11a696f6856624de532a6830d5db9dbbe2384cEric Laurent return nativeToJavaStatus( lpRecorder->setMarkerPosition(markerPos) ); 4919066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project} 4929066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 4939066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 4949066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project// ---------------------------------------------------------------------------- 4959066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectstatic jint android_media_AudioRecord_get_marker_pos(JNIEnv *env, jobject thiz) { 49618db49a46259020387c40fff36d92edc1087a366Glenn Kasten 497532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent sp<AudioRecord> lpRecorder = getAudioRecord(env, thiz); 4989066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project uint32_t markerPos = 0; 49918db49a46259020387c40fff36d92edc1087a366Glenn Kasten 500532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent if (lpRecorder == NULL) { 5019066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project jniThrowException(env, "java/lang/IllegalStateException", 5029066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project "Unable to retrieve AudioRecord pointer for getMarkerPosition()"); 503bc11a696f6856624de532a6830d5db9dbbe2384cEric Laurent return (jint)AUDIO_JAVA_ERROR; 5049066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } 505532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent lpRecorder->getMarkerPosition(&markerPos); 506532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent return (jint)markerPos; 5079066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project} 5089066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 5099066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 5109066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project// ---------------------------------------------------------------------------- 5119066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectstatic jint android_media_AudioRecord_set_pos_update_period(JNIEnv *env, jobject thiz, 5129066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project jint period) { 51318db49a46259020387c40fff36d92edc1087a366Glenn Kasten 514532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent sp<AudioRecord> lpRecorder = getAudioRecord(env, thiz); 51518db49a46259020387c40fff36d92edc1087a366Glenn Kasten 516532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent if (lpRecorder == NULL) { 5179066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project jniThrowException(env, "java/lang/IllegalStateException", 5189066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project "Unable to retrieve AudioRecord pointer for setPositionUpdatePeriod()"); 519bc11a696f6856624de532a6830d5db9dbbe2384cEric Laurent return (jint)AUDIO_JAVA_ERROR; 52018db49a46259020387c40fff36d92edc1087a366Glenn Kasten } 521bc11a696f6856624de532a6830d5db9dbbe2384cEric Laurent return nativeToJavaStatus( lpRecorder->setPositionUpdatePeriod(period) ); 5229066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project} 5239066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 5249066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 5259066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project// ---------------------------------------------------------------------------- 5269066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectstatic jint android_media_AudioRecord_get_pos_update_period(JNIEnv *env, jobject thiz) { 52718db49a46259020387c40fff36d92edc1087a366Glenn Kasten 528532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent sp<AudioRecord> lpRecorder = getAudioRecord(env, thiz); 5299066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project uint32_t period = 0; 53018db49a46259020387c40fff36d92edc1087a366Glenn Kasten 531532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent if (lpRecorder == NULL) { 5329066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project jniThrowException(env, "java/lang/IllegalStateException", 5339066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project "Unable to retrieve AudioRecord pointer for getPositionUpdatePeriod()"); 534bc11a696f6856624de532a6830d5db9dbbe2384cEric Laurent return (jint)AUDIO_JAVA_ERROR; 5359066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } 536532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent lpRecorder->getPositionUpdatePeriod(&period); 537532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent return (jint)period; 5389066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project} 5399066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 5409066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 5419066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project// ---------------------------------------------------------------------------- 5429066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project// returns the minimum required size for the successful creation of an AudioRecord instance. 5439066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project// returns 0 if the parameter combination is not supported. 5449066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project// return -1 if there was an error querying the buffer size. 5459066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectstatic jint android_media_AudioRecord_get_min_buff_size(JNIEnv *env, jobject thiz, 5465b8fd44365f03601aaba41879ac18e70ce814a0fGlenn Kasten jint sampleRateInHertz, jint channelCount, jint audioFormat) { 547c3308074cd6f06bbb35c1999b5260f938742a9e7Chia-chi Yeh 548532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent ALOGV(">> android_media_AudioRecord_get_min_buff_size(%d, %d, %d)", 5495b8fd44365f03601aaba41879ac18e70ce814a0fGlenn Kasten sampleRateInHertz, channelCount, audioFormat); 550c3308074cd6f06bbb35c1999b5260f938742a9e7Chia-chi Yeh 551fd1e3dfde2a70906c50144a7b1b53593ec1f7891Glenn Kasten size_t frameCount = 0; 552a5a42384367230b8e16cf441d5dcf4225332f9d7Glenn Kasten audio_format_t format = audioFormatToNative(audioFormat); 553c3308074cd6f06bbb35c1999b5260f938742a9e7Chia-chi Yeh status_t result = AudioRecord::getMinFrameCount(&frameCount, 554c3308074cd6f06bbb35c1999b5260f938742a9e7Chia-chi Yeh sampleRateInHertz, 555a5a42384367230b8e16cf441d5dcf4225332f9d7Glenn Kasten format, 5565b8fd44365f03601aaba41879ac18e70ce814a0fGlenn Kasten audio_channel_in_mask_from_count(channelCount)); 557c3308074cd6f06bbb35c1999b5260f938742a9e7Chia-chi Yeh 558c3308074cd6f06bbb35c1999b5260f938742a9e7Chia-chi Yeh if (result == BAD_VALUE) { 559c3308074cd6f06bbb35c1999b5260f938742a9e7Chia-chi Yeh return 0; 560c3308074cd6f06bbb35c1999b5260f938742a9e7Chia-chi Yeh } 561c3308074cd6f06bbb35c1999b5260f938742a9e7Chia-chi Yeh if (result != NO_ERROR) { 562c3308074cd6f06bbb35c1999b5260f938742a9e7Chia-chi Yeh return -1; 5639066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } 5645b8fd44365f03601aaba41879ac18e70ce814a0fGlenn Kasten return frameCount * channelCount * audio_bytes_per_sample(format); 5659066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project} 5669066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 5679066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 5689066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project// ---------------------------------------------------------------------------- 5699066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project// ---------------------------------------------------------------------------- 5709066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectstatic JNINativeMethod gMethods[] = { 5719066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project // name, signature, funcPtr 572505e5c8859f596ed58489be565d6e029314b2ac8Eric Laurent {"native_start", "(II)I", (void *)android_media_AudioRecord_start}, 5739066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project {"native_stop", "()V", (void *)android_media_AudioRecord_stop}, 574701d6ff12f36bf5e9de0dafdaced06744fd411ebJean-Michel Trivi {"native_setup", "(Ljava/lang/Object;Ljava/lang/Object;IIII[I)I", 5759066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project (void *)android_media_AudioRecord_setup}, 5769066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project {"native_finalize", "()V", (void *)android_media_AudioRecord_finalize}, 5779066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project {"native_release", "()V", (void *)android_media_AudioRecord_release}, 57818db49a46259020387c40fff36d92edc1087a366Glenn Kasten {"native_read_in_byte_array", 5799066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project "([BII)I", (void *)android_media_AudioRecord_readInByteArray}, 5809066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project {"native_read_in_short_array", 5819066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project "([SII)I", (void *)android_media_AudioRecord_readInShortArray}, 5829066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project {"native_read_in_direct_buffer","(Ljava/lang/Object;I)I", 5839066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project (void *)android_media_AudioRecord_readInDirectBuffer}, 5849066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project {"native_set_marker_pos","(I)I", (void *)android_media_AudioRecord_set_marker_pos}, 5859066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project {"native_get_marker_pos","()I", (void *)android_media_AudioRecord_get_marker_pos}, 5869066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project {"native_set_pos_update_period", 5879066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project "(I)I", (void *)android_media_AudioRecord_set_pos_update_period}, 5889066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project {"native_get_pos_update_period", 5899066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project "()I", (void *)android_media_AudioRecord_get_pos_update_period}, 5909066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project {"native_get_min_buff_size", 5919066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project "(III)I", (void *)android_media_AudioRecord_get_min_buff_size}, 5929066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}; 5939066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 5949066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project// field names found in android/media/AudioRecord.java 5959066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project#define JAVA_POSTEVENT_CALLBACK_NAME "postEventFromNative" 5969066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project#define JAVA_NATIVERECORDERINJAVAOBJ_FIELD_NAME "mNativeRecorderInJavaObj" 5979066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project#define JAVA_NATIVECALLBACKINFO_FIELD_NAME "mNativeCallbackCookie" 5989066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 5999066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project// ---------------------------------------------------------------------------- 6009066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectint register_android_media_AudioRecord(JNIEnv *env) 6019066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project{ 6029066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project javaAudioRecordFields.postNativeEventInJava = NULL; 6039066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project javaAudioRecordFields.nativeRecorderInJavaObj = NULL; 6049066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project javaAudioRecordFields.nativeCallbackCookie = NULL; 60518db49a46259020387c40fff36d92edc1087a366Glenn Kasten 6069066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 6079066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project // Get the AudioRecord class 60846e18c11d46a2bc1a46174f963d0ed1224d94cd2Brian Carlstrom jclass audioRecordClass = env->FindClass(kClassPathName); 60946e18c11d46a2bc1a46174f963d0ed1224d94cd2Brian Carlstrom if (audioRecordClass == NULL) { 6103762c311729fe9f3af085c14c5c1fb471d994c03Steve Block ALOGE("Can't find %s", kClassPathName); 6119066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project return -1; 6129066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } 6139066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project // Get the postEvent method 6149066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project javaAudioRecordFields.postNativeEventInJava = env->GetStaticMethodID( 61546e18c11d46a2bc1a46174f963d0ed1224d94cd2Brian Carlstrom audioRecordClass, 6169066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project JAVA_POSTEVENT_CALLBACK_NAME, "(Ljava/lang/Object;IIILjava/lang/Object;)V"); 6179066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project if (javaAudioRecordFields.postNativeEventInJava == NULL) { 6183762c311729fe9f3af085c14c5c1fb471d994c03Steve Block ALOGE("Can't find AudioRecord.%s", JAVA_POSTEVENT_CALLBACK_NAME); 6199066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project return -1; 6209066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } 6219066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 6229066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project // Get the variables 6239066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project // mNativeRecorderInJavaObj 62418db49a46259020387c40fff36d92edc1087a366Glenn Kasten javaAudioRecordFields.nativeRecorderInJavaObj = 62546e18c11d46a2bc1a46174f963d0ed1224d94cd2Brian Carlstrom env->GetFieldID(audioRecordClass, 626075e9a19ce645752f8282bc19c91b25978a7dc52Ashok Bhat JAVA_NATIVERECORDERINJAVAOBJ_FIELD_NAME, "J"); 6279066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project if (javaAudioRecordFields.nativeRecorderInJavaObj == NULL) { 6283762c311729fe9f3af085c14c5c1fb471d994c03Steve Block ALOGE("Can't find AudioRecord.%s", JAVA_NATIVERECORDERINJAVAOBJ_FIELD_NAME); 6299066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project return -1; 6309066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } 6319066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project // mNativeCallbackCookie 6329066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project javaAudioRecordFields.nativeCallbackCookie = env->GetFieldID( 63346e18c11d46a2bc1a46174f963d0ed1224d94cd2Brian Carlstrom audioRecordClass, 634075e9a19ce645752f8282bc19c91b25978a7dc52Ashok Bhat JAVA_NATIVECALLBACKINFO_FIELD_NAME, "J"); 6359066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project if (javaAudioRecordFields.nativeCallbackCookie == NULL) { 6363762c311729fe9f3af085c14c5c1fb471d994c03Steve Block ALOGE("Can't find AudioRecord.%s", JAVA_NATIVECALLBACKINFO_FIELD_NAME); 6379066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project return -1; 6389066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } 6399066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 640701d6ff12f36bf5e9de0dafdaced06744fd411ebJean-Michel Trivi // Get the AudioAttributes class and fields 641701d6ff12f36bf5e9de0dafdaced06744fd411ebJean-Michel Trivi jclass audioAttrClass = env->FindClass(kAudioAttributesClassPathName); 642701d6ff12f36bf5e9de0dafdaced06744fd411ebJean-Michel Trivi if (audioAttrClass == NULL) { 643701d6ff12f36bf5e9de0dafdaced06744fd411ebJean-Michel Trivi ALOGE("Can't find %s", kAudioAttributesClassPathName); 644701d6ff12f36bf5e9de0dafdaced06744fd411ebJean-Michel Trivi return -1; 645701d6ff12f36bf5e9de0dafdaced06744fd411ebJean-Michel Trivi } 646701d6ff12f36bf5e9de0dafdaced06744fd411ebJean-Michel Trivi jclass audioAttributesClassRef = (jclass)env->NewGlobalRef(audioAttrClass); 647701d6ff12f36bf5e9de0dafdaced06744fd411ebJean-Michel Trivi javaAudioAttrFields.fieldRecSource = env->GetFieldID(audioAttributesClassRef, "mSource", "I"); 648bdad1afa92a6e7cd688061d5432f25a3dd15b64eEric Laurent javaAudioAttrFields.fieldFlags = env->GetFieldID(audioAttributesClassRef, "mFlags", "I"); 649701d6ff12f36bf5e9de0dafdaced06744fd411ebJean-Michel Trivi javaAudioAttrFields.fieldFormattedTags = 650701d6ff12f36bf5e9de0dafdaced06744fd411ebJean-Michel Trivi env->GetFieldID(audioAttributesClassRef, "mFormattedTags", "Ljava/lang/String;"); 651701d6ff12f36bf5e9de0dafdaced06744fd411ebJean-Michel Trivi env->DeleteGlobalRef(audioAttributesClassRef); 652701d6ff12f36bf5e9de0dafdaced06744fd411ebJean-Michel Trivi if (javaAudioAttrFields.fieldRecSource == NULL 653bdad1afa92a6e7cd688061d5432f25a3dd15b64eEric Laurent || javaAudioAttrFields.fieldFlags == NULL 654701d6ff12f36bf5e9de0dafdaced06744fd411ebJean-Michel Trivi || javaAudioAttrFields.fieldFormattedTags == NULL) { 655701d6ff12f36bf5e9de0dafdaced06744fd411ebJean-Michel Trivi ALOGE("Can't initialize AudioAttributes fields"); 656701d6ff12f36bf5e9de0dafdaced06744fd411ebJean-Michel Trivi return -1; 657701d6ff12f36bf5e9de0dafdaced06744fd411ebJean-Michel Trivi } 658701d6ff12f36bf5e9de0dafdaced06744fd411ebJean-Michel Trivi 6599066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project return AndroidRuntime::registerNativeMethods(env, 6609066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project kClassPathName, gMethods, NELEM(gMethods)); 6619066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project} 6629066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 6639066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project// ---------------------------------------------------------------------------- 664