android_media_AudioRecord.cpp revision 58b0f3f3324ad04d53b31a75c9ad617957dfbba6
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> 24ed6b9dff563c5e22f040ff37e12c0d771e0478aeAndreas Gampe#include "core_jni_helpers.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, 2424c3fc59b4d0c849016b17ce8e73540bdce847702Eric Laurent flags, 2434c3fc59b4d0c849016b17ce8e73540bdce847702Eric Laurent paa); 2449066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 245a9838afbe3ef58b63e5d52060d5ff2776c1c603aGlenn Kasten if (status != NO_ERROR) { 246a9838afbe3ef58b63e5d52060d5ff2776c1c603aGlenn Kasten ALOGE("Error creating AudioRecord instance: initialization check failed with status %d.", 247a9838afbe3ef58b63e5d52060d5ff2776c1c603aGlenn Kasten status); 2489066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project goto native_init_failure; 2499066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } 2509066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 25144ff4cd8be50768d5bd471bc6c034acefa0b59caEric Laurent nSession = (jint *) env->GetPrimitiveArrayCritical(jSession, NULL); 25244ff4cd8be50768d5bd471bc6c034acefa0b59caEric Laurent if (nSession == NULL) { 2533762c311729fe9f3af085c14c5c1fb471d994c03Steve Block ALOGE("Error creating AudioRecord: Error retrieving session id pointer"); 25444ff4cd8be50768d5bd471bc6c034acefa0b59caEric Laurent goto native_init_failure; 25544ff4cd8be50768d5bd471bc6c034acefa0b59caEric Laurent } 256b3db213eb55acb661e4b9ea40bcc00af4b76fab9Glenn Kasten // read the audio session ID back from AudioRecord in case a new session was created during set() 25744ff4cd8be50768d5bd471bc6c034acefa0b59caEric Laurent nSession[0] = lpRecorder->getSessionId(); 25844ff4cd8be50768d5bd471bc6c034acefa0b59caEric Laurent env->ReleasePrimitiveArrayCritical(jSession, nSession, 0); 25944ff4cd8be50768d5bd471bc6c034acefa0b59caEric Laurent nSession = NULL; 26044ff4cd8be50768d5bd471bc6c034acefa0b59caEric Laurent 261532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent { // scope for the lock 262532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent Mutex::Autolock l(sLock); 263532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent sAudioRecordCallBackCookies.add(lpCallbackData); 264532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent } 26518db49a46259020387c40fff36d92edc1087a366Glenn Kasten // save our newly created C++ AudioRecord in the "nativeRecorderInJavaObj" field 2669066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project // of the Java object 267532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent setAudioRecord(env, thiz, lpRecorder); 26818db49a46259020387c40fff36d92edc1087a366Glenn Kasten 2699066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project // save our newly created callback information in the "nativeCallbackCookie" field 2709066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project // of the Java object (in mNativeCallbackCookie) so we can free the memory in finalize() 271075e9a19ce645752f8282bc19c91b25978a7dc52Ashok Bhat env->SetLongField(thiz, javaAudioRecordFields.nativeCallbackCookie, (jlong)lpCallbackData); 27218db49a46259020387c40fff36d92edc1087a366Glenn Kasten 273bc11a696f6856624de532a6830d5db9dbbe2384cEric Laurent return (jint) AUDIO_JAVA_SUCCESS; 27418db49a46259020387c40fff36d92edc1087a366Glenn Kasten 2759066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project // failure: 2769066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectnative_init_failure: 2774bac5a3928d57ac193141eb6d45f3b39200d27deJean-Michel Trivi env->DeleteGlobalRef(lpCallbackData->audioRecord_class); 2784bac5a3928d57ac193141eb6d45f3b39200d27deJean-Michel Trivi env->DeleteGlobalRef(lpCallbackData->audioRecord_ref); 2799066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project delete lpCallbackData; 280075e9a19ce645752f8282bc19c91b25978a7dc52Ashok Bhat env->SetLongField(thiz, javaAudioRecordFields.nativeCallbackCookie, 0); 28118db49a46259020387c40fff36d92edc1087a366Glenn Kasten 282075e9a19ce645752f8282bc19c91b25978a7dc52Ashok Bhat return (jint) AUDIORECORD_ERROR_SETUP_NATIVEINITFAILED; 2839066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project} 2849066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 2859066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 2869066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 2879066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project// ---------------------------------------------------------------------------- 288075e9a19ce645752f8282bc19c91b25978a7dc52Ashok Bhatstatic jint 289505e5c8859f596ed58489be565d6e029314b2ac8Eric Laurentandroid_media_AudioRecord_start(JNIEnv *env, jobject thiz, jint event, jint triggerSession) 2909066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project{ 291532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent sp<AudioRecord> lpRecorder = getAudioRecord(env, thiz); 2929066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project if (lpRecorder == NULL ) { 2939066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project jniThrowException(env, "java/lang/IllegalStateException", NULL); 294bc11a696f6856624de532a6830d5db9dbbe2384cEric Laurent return (jint) AUDIO_JAVA_ERROR; 2959066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } 29618db49a46259020387c40fff36d92edc1087a366Glenn Kasten 297bc11a696f6856624de532a6830d5db9dbbe2384cEric Laurent return nativeToJavaStatus( 298505e5c8859f596ed58489be565d6e029314b2ac8Eric Laurent lpRecorder->start((AudioSystem::sync_event_t)event, triggerSession)); 2999066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project} 3009066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 3019066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 3029066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project// ---------------------------------------------------------------------------- 3039066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectstatic void 3049066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectandroid_media_AudioRecord_stop(JNIEnv *env, jobject thiz) 3059066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project{ 306532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent sp<AudioRecord> lpRecorder = getAudioRecord(env, thiz); 3079066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project if (lpRecorder == NULL ) { 3089066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project jniThrowException(env, "java/lang/IllegalStateException", NULL); 3099066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project return; 3109066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } 3119066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 3129066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project lpRecorder->stop(); 31371f2cf116aab893e224056c38ab146bd1538dd3eSteve Block //ALOGV("Called lpRecorder->stop()"); 3149066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project} 3159066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 3169066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 3179066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project// ---------------------------------------------------------------------------- 318532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent 319532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent#define CALLBACK_COND_WAIT_TIMEOUT_MS 1000 320e6335c904aafa17795b059a5d83b8b7db6c761c8Dave Sparksstatic void android_media_AudioRecord_release(JNIEnv *env, jobject thiz) { 321532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent sp<AudioRecord> lpRecorder = setAudioRecord(env, thiz, 0); 322532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent if (lpRecorder == NULL) { 323532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent return; 324532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent } 3252fbf25b50f8534c78f3b9063d35a0b5970ff8f1eGlenn Kasten ALOGV("About to delete lpRecorder: %p", lpRecorder.get()); 326532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent lpRecorder->stop(); 327e6335c904aafa17795b059a5d83b8b7db6c761c8Dave Sparks 328075e9a19ce645752f8282bc19c91b25978a7dc52Ashok Bhat audiorecord_callback_cookie *lpCookie = (audiorecord_callback_cookie *)env->GetLongField( 329e6335c904aafa17795b059a5d83b8b7db6c761c8Dave Sparks thiz, javaAudioRecordFields.nativeCallbackCookie); 3309066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 331e6335c904aafa17795b059a5d83b8b7db6c761c8Dave Sparks // reset the native resources in the Java object so any attempt to access 332e6335c904aafa17795b059a5d83b8b7db6c761c8Dave Sparks // them after a call to release fails. 333075e9a19ce645752f8282bc19c91b25978a7dc52Ashok Bhat env->SetLongField(thiz, javaAudioRecordFields.nativeCallbackCookie, 0); 334e6335c904aafa17795b059a5d83b8b7db6c761c8Dave Sparks 3359066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project // delete the callback information 3369066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project if (lpCookie) { 337532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent Mutex::Autolock l(sLock); 3382fbf25b50f8534c78f3b9063d35a0b5970ff8f1eGlenn Kasten ALOGV("deleting lpCookie: %p", lpCookie); 339532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent while (lpCookie->busy) { 340532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent if (lpCookie->cond.waitRelative(sLock, 341532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent milliseconds(CALLBACK_COND_WAIT_TIMEOUT_MS)) != 342532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent NO_ERROR) { 343532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent break; 344532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent } 345532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent } 346532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent sAudioRecordCallBackCookies.remove(lpCookie); 3474bac5a3928d57ac193141eb6d45f3b39200d27deJean-Michel Trivi env->DeleteGlobalRef(lpCookie->audioRecord_class); 3484bac5a3928d57ac193141eb6d45f3b39200d27deJean-Michel Trivi env->DeleteGlobalRef(lpCookie->audioRecord_ref); 3499066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project delete lpCookie; 3509066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } 3519066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project} 3529066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 3539066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 3549066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project// ---------------------------------------------------------------------------- 355e6335c904aafa17795b059a5d83b8b7db6c761c8Dave Sparksstatic void android_media_AudioRecord_finalize(JNIEnv *env, jobject thiz) { 356e6335c904aafa17795b059a5d83b8b7db6c761c8Dave Sparks android_media_AudioRecord_release(env, thiz); 3579066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project} 3589066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 3599066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 3609066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project// ---------------------------------------------------------------------------- 3619066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectstatic jint android_media_AudioRecord_readInByteArray(JNIEnv *env, jobject thiz, 3629066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project jbyteArray javaAudioData, 3639066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project jint offsetInBytes, jint sizeInBytes) { 3649066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project jbyte* recordBuff = NULL; 3659066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project // get the audio recorder from which we'll read new audio samples 366532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent sp<AudioRecord> lpRecorder = getAudioRecord(env, thiz); 3679066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project if (lpRecorder == NULL) { 3683762c311729fe9f3af085c14c5c1fb471d994c03Steve Block ALOGE("Unable to retrieve AudioRecord object, can't record"); 3699066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project return 0; 3709066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } 3719066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 3729066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project if (!javaAudioData) { 3733762c311729fe9f3af085c14c5c1fb471d994c03Steve Block ALOGE("Invalid Java array to store recorded audio, can't record"); 3749066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project return 0; 3759066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } 3769066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 3779066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project // get the pointer to where we'll record the audio 378421ddc014b31478ba0ef115e098d8780a35af331Eric Laurent // NOTE: We may use GetPrimitiveArrayCritical() when the JNI implementation changes in such 379421ddc014b31478ba0ef115e098d8780a35af331Eric Laurent // a way that it becomes much more efficient. When doing so, we will have to prevent the 380421ddc014b31478ba0ef115e098d8780a35af331Eric Laurent // AudioSystem callback to be called while in critical section (in case of media server 381421ddc014b31478ba0ef115e098d8780a35af331Eric Laurent // process crash for instance) 382421ddc014b31478ba0ef115e098d8780a35af331Eric Laurent recordBuff = (jbyte *)env->GetByteArrayElements(javaAudioData, NULL); 3839066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 3849066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project if (recordBuff == NULL) { 3853762c311729fe9f3af085c14c5c1fb471d994c03Steve Block ALOGE("Error retrieving destination for recorded audio data, can't record"); 3869066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project return 0; 3879066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } 3889066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 3899066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project // read the new audio data from the native AudioRecord object 3907a60ae86ec5c8950d5baf1a8ba33207ddaa72a7dAndy Hung ssize_t readSize = lpRecorder->read(recordBuff + offsetInBytes, sizeInBytes); 391421ddc014b31478ba0ef115e098d8780a35af331Eric Laurent env->ReleaseByteArrayElements(javaAudioData, recordBuff, 0); 3929066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 393357263da0ec2bc2be9d48c1becc3d94288c2f3edEric Laurent if (readSize < 0) { 394bc11a696f6856624de532a6830d5db9dbbe2384cEric Laurent readSize = (jint)AUDIO_JAVA_INVALID_OPERATION; 395357263da0ec2bc2be9d48c1becc3d94288c2f3edEric Laurent } 3969066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project return (jint) readSize; 3979066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project} 3989066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 3999066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project// ---------------------------------------------------------------------------- 4009066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectstatic jint android_media_AudioRecord_readInShortArray(JNIEnv *env, jobject thiz, 4019066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project jshortArray javaAudioData, 4029066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project jint offsetInShorts, jint sizeInShorts) { 4039066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 4043d4c64191a181aac0922abbd56f4046d6b7ec31cEric Laurent jshort* recordBuff = NULL; 4053d4c64191a181aac0922abbd56f4046d6b7ec31cEric Laurent // get the audio recorder from which we'll read new audio samples 4063d4c64191a181aac0922abbd56f4046d6b7ec31cEric Laurent sp<AudioRecord> lpRecorder = getAudioRecord(env, thiz); 4073d4c64191a181aac0922abbd56f4046d6b7ec31cEric Laurent if (lpRecorder == NULL) { 4083d4c64191a181aac0922abbd56f4046d6b7ec31cEric Laurent ALOGE("Unable to retrieve AudioRecord object, can't record"); 4093d4c64191a181aac0922abbd56f4046d6b7ec31cEric Laurent return 0; 4103f0af8be9e11112b03fc487bc2cc393286b923b5Glenn Kasten } 4113d4c64191a181aac0922abbd56f4046d6b7ec31cEric Laurent 4123d4c64191a181aac0922abbd56f4046d6b7ec31cEric Laurent if (!javaAudioData) { 4133d4c64191a181aac0922abbd56f4046d6b7ec31cEric Laurent ALOGE("Invalid Java array to store recorded audio, can't record"); 4143d4c64191a181aac0922abbd56f4046d6b7ec31cEric Laurent return 0; 4153d4c64191a181aac0922abbd56f4046d6b7ec31cEric Laurent } 4163d4c64191a181aac0922abbd56f4046d6b7ec31cEric Laurent 4173d4c64191a181aac0922abbd56f4046d6b7ec31cEric Laurent // get the pointer to where we'll record the audio 4183d4c64191a181aac0922abbd56f4046d6b7ec31cEric Laurent // NOTE: We may use GetPrimitiveArrayCritical() when the JNI implementation changes in such 4193d4c64191a181aac0922abbd56f4046d6b7ec31cEric Laurent // a way that it becomes much more efficient. When doing so, we will have to prevent the 4203d4c64191a181aac0922abbd56f4046d6b7ec31cEric Laurent // AudioSystem callback to be called while in critical section (in case of media server 4213d4c64191a181aac0922abbd56f4046d6b7ec31cEric Laurent // process crash for instance) 4223d4c64191a181aac0922abbd56f4046d6b7ec31cEric Laurent recordBuff = (jshort *)env->GetShortArrayElements(javaAudioData, NULL); 4233d4c64191a181aac0922abbd56f4046d6b7ec31cEric Laurent 4243d4c64191a181aac0922abbd56f4046d6b7ec31cEric Laurent if (recordBuff == NULL) { 4253d4c64191a181aac0922abbd56f4046d6b7ec31cEric Laurent ALOGE("Error retrieving destination for recorded audio data, can't record"); 4263d4c64191a181aac0922abbd56f4046d6b7ec31cEric Laurent return 0; 4273d4c64191a181aac0922abbd56f4046d6b7ec31cEric Laurent } 4283d4c64191a181aac0922abbd56f4046d6b7ec31cEric Laurent 4293d4c64191a181aac0922abbd56f4046d6b7ec31cEric Laurent // read the new audio data from the native AudioRecord object 4303d4c64191a181aac0922abbd56f4046d6b7ec31cEric Laurent const size_t sizeInBytes = sizeInShorts * sizeof(short); 4317a60ae86ec5c8950d5baf1a8ba33207ddaa72a7dAndy Hung ssize_t readSize = lpRecorder->read(recordBuff + offsetInShorts, sizeInBytes); 4323d4c64191a181aac0922abbd56f4046d6b7ec31cEric Laurent 4333d4c64191a181aac0922abbd56f4046d6b7ec31cEric Laurent env->ReleaseShortArrayElements(javaAudioData, recordBuff, 0); 4343d4c64191a181aac0922abbd56f4046d6b7ec31cEric Laurent 4353d4c64191a181aac0922abbd56f4046d6b7ec31cEric Laurent if (readSize < 0) { 436bc11a696f6856624de532a6830d5db9dbbe2384cEric Laurent readSize = (jint)AUDIO_JAVA_INVALID_OPERATION; 4373d4c64191a181aac0922abbd56f4046d6b7ec31cEric Laurent } else { 4383d4c64191a181aac0922abbd56f4046d6b7ec31cEric Laurent readSize /= sizeof(short); 4393d4c64191a181aac0922abbd56f4046d6b7ec31cEric Laurent } 4403d4c64191a181aac0922abbd56f4046d6b7ec31cEric Laurent return (jint) readSize; 4419066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project} 4429066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 4439066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project// ---------------------------------------------------------------------------- 44458b0f3f3324ad04d53b31a75c9ad617957dfbba6Andy Hungstatic jint android_media_AudioRecord_readInFloatArray(JNIEnv *env, jobject thiz, 44558b0f3f3324ad04d53b31a75c9ad617957dfbba6Andy Hung jfloatArray javaAudioData, 44658b0f3f3324ad04d53b31a75c9ad617957dfbba6Andy Hung jint offsetInFloats, jint sizeInFloats, 44758b0f3f3324ad04d53b31a75c9ad617957dfbba6Andy Hung jboolean isReadBlocking) { 44858b0f3f3324ad04d53b31a75c9ad617957dfbba6Andy Hung // get the audio recorder from which we'll read new audio samples 44958b0f3f3324ad04d53b31a75c9ad617957dfbba6Andy Hung sp<AudioRecord> lpRecorder = getAudioRecord(env, thiz); 45058b0f3f3324ad04d53b31a75c9ad617957dfbba6Andy Hung if (lpRecorder == NULL) { 45158b0f3f3324ad04d53b31a75c9ad617957dfbba6Andy Hung ALOGE("Unable to retrieve AudioRecord object"); 45258b0f3f3324ad04d53b31a75c9ad617957dfbba6Andy Hung return (jint)AUDIO_JAVA_INVALID_OPERATION; 45358b0f3f3324ad04d53b31a75c9ad617957dfbba6Andy Hung } 45458b0f3f3324ad04d53b31a75c9ad617957dfbba6Andy Hung 45558b0f3f3324ad04d53b31a75c9ad617957dfbba6Andy Hung if (javaAudioData == NULL) { 45658b0f3f3324ad04d53b31a75c9ad617957dfbba6Andy Hung ALOGE("Invalid Java array to store recorded audio"); 45758b0f3f3324ad04d53b31a75c9ad617957dfbba6Andy Hung return (jint)AUDIO_JAVA_BAD_VALUE; 45858b0f3f3324ad04d53b31a75c9ad617957dfbba6Andy Hung } 45958b0f3f3324ad04d53b31a75c9ad617957dfbba6Andy Hung 46058b0f3f3324ad04d53b31a75c9ad617957dfbba6Andy Hung // get the pointer to where we'll record the audio 46158b0f3f3324ad04d53b31a75c9ad617957dfbba6Andy Hung // NOTE: We may use GetPrimitiveArrayCritical() when the JNI implementation changes in such 46258b0f3f3324ad04d53b31a75c9ad617957dfbba6Andy Hung // a way that it becomes much more efficient. When doing so, we will have to prevent the 46358b0f3f3324ad04d53b31a75c9ad617957dfbba6Andy Hung // AudioSystem callback to be called while in critical section (in case of media server 46458b0f3f3324ad04d53b31a75c9ad617957dfbba6Andy Hung // process crash for instance) 46558b0f3f3324ad04d53b31a75c9ad617957dfbba6Andy Hung jfloat *recordBuff = (jfloat *)env->GetFloatArrayElements(javaAudioData, NULL); 46658b0f3f3324ad04d53b31a75c9ad617957dfbba6Andy Hung if (recordBuff == NULL) { 46758b0f3f3324ad04d53b31a75c9ad617957dfbba6Andy Hung ALOGE("Error retrieving destination for recorded audio data"); 46858b0f3f3324ad04d53b31a75c9ad617957dfbba6Andy Hung return (jint)AUDIO_JAVA_BAD_VALUE; 46958b0f3f3324ad04d53b31a75c9ad617957dfbba6Andy Hung } 47058b0f3f3324ad04d53b31a75c9ad617957dfbba6Andy Hung 47158b0f3f3324ad04d53b31a75c9ad617957dfbba6Andy Hung // read the new audio data from the native AudioRecord object 47258b0f3f3324ad04d53b31a75c9ad617957dfbba6Andy Hung const size_t sizeInBytes = sizeInFloats * sizeof(float); 47358b0f3f3324ad04d53b31a75c9ad617957dfbba6Andy Hung ssize_t readSize = lpRecorder->read(recordBuff + offsetInFloats, sizeInBytes); 47458b0f3f3324ad04d53b31a75c9ad617957dfbba6Andy Hung 47558b0f3f3324ad04d53b31a75c9ad617957dfbba6Andy Hung env->ReleaseFloatArrayElements(javaAudioData, recordBuff, 0); 47658b0f3f3324ad04d53b31a75c9ad617957dfbba6Andy Hung 47758b0f3f3324ad04d53b31a75c9ad617957dfbba6Andy Hung if (readSize < 0) { 47858b0f3f3324ad04d53b31a75c9ad617957dfbba6Andy Hung ALOGE_IF(readSize != WOULD_BLOCK, "Error %zd during AudioRecord native read", readSize); 47958b0f3f3324ad04d53b31a75c9ad617957dfbba6Andy Hung switch (readSize) { 48058b0f3f3324ad04d53b31a75c9ad617957dfbba6Andy Hung case WOULD_BLOCK: 48158b0f3f3324ad04d53b31a75c9ad617957dfbba6Andy Hung return (jint)0; 48258b0f3f3324ad04d53b31a75c9ad617957dfbba6Andy Hung case BAD_VALUE: 48358b0f3f3324ad04d53b31a75c9ad617957dfbba6Andy Hung return (jint)AUDIO_JAVA_BAD_VALUE; 48458b0f3f3324ad04d53b31a75c9ad617957dfbba6Andy Hung default: 48558b0f3f3324ad04d53b31a75c9ad617957dfbba6Andy Hung // may be possible for other errors such as 48658b0f3f3324ad04d53b31a75c9ad617957dfbba6Andy Hung // NO_INIT to happen if restoreRecord_l fails. 48758b0f3f3324ad04d53b31a75c9ad617957dfbba6Andy Hung case INVALID_OPERATION: 48858b0f3f3324ad04d53b31a75c9ad617957dfbba6Andy Hung return (jint)AUDIO_JAVA_INVALID_OPERATION; 48958b0f3f3324ad04d53b31a75c9ad617957dfbba6Andy Hung } 49058b0f3f3324ad04d53b31a75c9ad617957dfbba6Andy Hung } 49158b0f3f3324ad04d53b31a75c9ad617957dfbba6Andy Hung return (jint)(readSize / sizeof(float)); 49258b0f3f3324ad04d53b31a75c9ad617957dfbba6Andy Hung} 49358b0f3f3324ad04d53b31a75c9ad617957dfbba6Andy Hung 49458b0f3f3324ad04d53b31a75c9ad617957dfbba6Andy Hung// ---------------------------------------------------------------------------- 4959066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectstatic jint android_media_AudioRecord_readInDirectBuffer(JNIEnv *env, jobject thiz, 4969066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project jobject jBuffer, jint sizeInBytes) { 4979066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project // get the audio recorder from which we'll read new audio samples 498532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent sp<AudioRecord> lpRecorder = getAudioRecord(env, thiz); 49918db49a46259020387c40fff36d92edc1087a366Glenn Kasten if (lpRecorder==NULL) 5009066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project return 0; 5019066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 5029066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project // direct buffer and direct access supported? 5039066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project long capacity = env->GetDirectBufferCapacity(jBuffer); 50418db49a46259020387c40fff36d92edc1087a366Glenn Kasten if (capacity == -1) { 5059066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project // buffer direct access is not supported 5063762c311729fe9f3af085c14c5c1fb471d994c03Steve Block ALOGE("Buffer direct access is not supported, can't record"); 5079066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project return 0; 5089066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } 50971f2cf116aab893e224056c38ab146bd1538dd3eSteve Block //ALOGV("capacity = %ld", capacity); 5109066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project jbyte* nativeFromJavaBuf = (jbyte*) env->GetDirectBufferAddress(jBuffer); 51118db49a46259020387c40fff36d92edc1087a366Glenn Kasten if (nativeFromJavaBuf==NULL) { 5123762c311729fe9f3af085c14c5c1fb471d994c03Steve Block ALOGE("Buffer direct access is not supported, can't record"); 5139066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project return 0; 51418db49a46259020387c40fff36d92edc1087a366Glenn Kasten } 5159066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 5169066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project // read new data from the recorder 517357263da0ec2bc2be9d48c1becc3d94288c2f3edEric Laurent ssize_t readSize = lpRecorder->read(nativeFromJavaBuf, 5189066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project capacity < sizeInBytes ? capacity : sizeInBytes); 519357263da0ec2bc2be9d48c1becc3d94288c2f3edEric Laurent if (readSize < 0) { 520bc11a696f6856624de532a6830d5db9dbbe2384cEric Laurent readSize = (jint)AUDIO_JAVA_INVALID_OPERATION; 521357263da0ec2bc2be9d48c1becc3d94288c2f3edEric Laurent } 522357263da0ec2bc2be9d48c1becc3d94288c2f3edEric Laurent return (jint)readSize; 5239066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project} 5249066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 5259066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 5269066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project// ---------------------------------------------------------------------------- 52718db49a46259020387c40fff36d92edc1087a366Glenn Kastenstatic jint android_media_AudioRecord_set_marker_pos(JNIEnv *env, jobject thiz, 5289066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project jint markerPos) { 52918db49a46259020387c40fff36d92edc1087a366Glenn Kasten 530532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent sp<AudioRecord> lpRecorder = getAudioRecord(env, thiz); 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 setMarkerPosition()"); 534bc11a696f6856624de532a6830d5db9dbbe2384cEric Laurent return (jint)AUDIO_JAVA_ERROR; 5359066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } 536bc11a696f6856624de532a6830d5db9dbbe2384cEric Laurent return nativeToJavaStatus( lpRecorder->setMarkerPosition(markerPos) ); 5379066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project} 5389066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 5399066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 5409066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project// ---------------------------------------------------------------------------- 5419066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectstatic jint android_media_AudioRecord_get_marker_pos(JNIEnv *env, jobject thiz) { 54218db49a46259020387c40fff36d92edc1087a366Glenn Kasten 543532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent sp<AudioRecord> lpRecorder = getAudioRecord(env, thiz); 5449066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project uint32_t markerPos = 0; 54518db49a46259020387c40fff36d92edc1087a366Glenn Kasten 546532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent if (lpRecorder == NULL) { 5479066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project jniThrowException(env, "java/lang/IllegalStateException", 5489066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project "Unable to retrieve AudioRecord pointer for getMarkerPosition()"); 549bc11a696f6856624de532a6830d5db9dbbe2384cEric Laurent return (jint)AUDIO_JAVA_ERROR; 5509066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } 551532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent lpRecorder->getMarkerPosition(&markerPos); 552532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent return (jint)markerPos; 5539066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project} 5549066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 5559066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 5569066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project// ---------------------------------------------------------------------------- 5579066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectstatic jint android_media_AudioRecord_set_pos_update_period(JNIEnv *env, jobject thiz, 5589066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project jint period) { 55918db49a46259020387c40fff36d92edc1087a366Glenn Kasten 560532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent sp<AudioRecord> lpRecorder = getAudioRecord(env, thiz); 56118db49a46259020387c40fff36d92edc1087a366Glenn Kasten 562532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent if (lpRecorder == NULL) { 5639066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project jniThrowException(env, "java/lang/IllegalStateException", 5649066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project "Unable to retrieve AudioRecord pointer for setPositionUpdatePeriod()"); 565bc11a696f6856624de532a6830d5db9dbbe2384cEric Laurent return (jint)AUDIO_JAVA_ERROR; 56618db49a46259020387c40fff36d92edc1087a366Glenn Kasten } 567bc11a696f6856624de532a6830d5db9dbbe2384cEric Laurent return nativeToJavaStatus( lpRecorder->setPositionUpdatePeriod(period) ); 5689066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project} 5699066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 5709066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 5719066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project// ---------------------------------------------------------------------------- 5729066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectstatic jint android_media_AudioRecord_get_pos_update_period(JNIEnv *env, jobject thiz) { 57318db49a46259020387c40fff36d92edc1087a366Glenn Kasten 574532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent sp<AudioRecord> lpRecorder = getAudioRecord(env, thiz); 5759066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project uint32_t period = 0; 57618db49a46259020387c40fff36d92edc1087a366Glenn Kasten 577532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent if (lpRecorder == NULL) { 5789066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project jniThrowException(env, "java/lang/IllegalStateException", 5799066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project "Unable to retrieve AudioRecord pointer for getPositionUpdatePeriod()"); 580bc11a696f6856624de532a6830d5db9dbbe2384cEric Laurent return (jint)AUDIO_JAVA_ERROR; 5819066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } 582532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent lpRecorder->getPositionUpdatePeriod(&period); 583532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent return (jint)period; 5849066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project} 5859066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 5869066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 5879066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project// ---------------------------------------------------------------------------- 5889066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project// returns the minimum required size for the successful creation of an AudioRecord instance. 5899066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project// returns 0 if the parameter combination is not supported. 5909066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project// return -1 if there was an error querying the buffer size. 5919066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectstatic jint android_media_AudioRecord_get_min_buff_size(JNIEnv *env, jobject thiz, 5925b8fd44365f03601aaba41879ac18e70ce814a0fGlenn Kasten jint sampleRateInHertz, jint channelCount, jint audioFormat) { 593c3308074cd6f06bbb35c1999b5260f938742a9e7Chia-chi Yeh 594532bc1cb7da9fdbf34893fa48b2ae11f54cf6a8eEric Laurent ALOGV(">> android_media_AudioRecord_get_min_buff_size(%d, %d, %d)", 5955b8fd44365f03601aaba41879ac18e70ce814a0fGlenn Kasten sampleRateInHertz, channelCount, audioFormat); 596c3308074cd6f06bbb35c1999b5260f938742a9e7Chia-chi Yeh 597fd1e3dfde2a70906c50144a7b1b53593ec1f7891Glenn Kasten size_t frameCount = 0; 598a5a42384367230b8e16cf441d5dcf4225332f9d7Glenn Kasten audio_format_t format = audioFormatToNative(audioFormat); 599c3308074cd6f06bbb35c1999b5260f938742a9e7Chia-chi Yeh status_t result = AudioRecord::getMinFrameCount(&frameCount, 600c3308074cd6f06bbb35c1999b5260f938742a9e7Chia-chi Yeh sampleRateInHertz, 601a5a42384367230b8e16cf441d5dcf4225332f9d7Glenn Kasten format, 6025b8fd44365f03601aaba41879ac18e70ce814a0fGlenn Kasten audio_channel_in_mask_from_count(channelCount)); 603c3308074cd6f06bbb35c1999b5260f938742a9e7Chia-chi Yeh 604c3308074cd6f06bbb35c1999b5260f938742a9e7Chia-chi Yeh if (result == BAD_VALUE) { 605c3308074cd6f06bbb35c1999b5260f938742a9e7Chia-chi Yeh return 0; 606c3308074cd6f06bbb35c1999b5260f938742a9e7Chia-chi Yeh } 607c3308074cd6f06bbb35c1999b5260f938742a9e7Chia-chi Yeh if (result != NO_ERROR) { 608c3308074cd6f06bbb35c1999b5260f938742a9e7Chia-chi Yeh return -1; 6099066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } 6105b8fd44365f03601aaba41879ac18e70ce814a0fGlenn Kasten return frameCount * channelCount * audio_bytes_per_sample(format); 6119066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project} 6129066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 6139066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 6149066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project// ---------------------------------------------------------------------------- 6159066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project// ---------------------------------------------------------------------------- 6169066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectstatic JNINativeMethod gMethods[] = { 6179066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project // name, signature, funcPtr 618505e5c8859f596ed58489be565d6e029314b2ac8Eric Laurent {"native_start", "(II)I", (void *)android_media_AudioRecord_start}, 6199066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project {"native_stop", "()V", (void *)android_media_AudioRecord_stop}, 620701d6ff12f36bf5e9de0dafdaced06744fd411ebJean-Michel Trivi {"native_setup", "(Ljava/lang/Object;Ljava/lang/Object;IIII[I)I", 6219066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project (void *)android_media_AudioRecord_setup}, 6229066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project {"native_finalize", "()V", (void *)android_media_AudioRecord_finalize}, 6239066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project {"native_release", "()V", (void *)android_media_AudioRecord_release}, 62418db49a46259020387c40fff36d92edc1087a366Glenn Kasten {"native_read_in_byte_array", 6259066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project "([BII)I", (void *)android_media_AudioRecord_readInByteArray}, 6269066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project {"native_read_in_short_array", 6279066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project "([SII)I", (void *)android_media_AudioRecord_readInShortArray}, 62858b0f3f3324ad04d53b31a75c9ad617957dfbba6Andy Hung {"native_read_in_float_array", 62958b0f3f3324ad04d53b31a75c9ad617957dfbba6Andy Hung "([FIIZ)I", (void *)android_media_AudioRecord_readInFloatArray}, 6309066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project {"native_read_in_direct_buffer","(Ljava/lang/Object;I)I", 6319066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project (void *)android_media_AudioRecord_readInDirectBuffer}, 6329066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project {"native_set_marker_pos","(I)I", (void *)android_media_AudioRecord_set_marker_pos}, 6339066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project {"native_get_marker_pos","()I", (void *)android_media_AudioRecord_get_marker_pos}, 6349066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project {"native_set_pos_update_period", 6359066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project "(I)I", (void *)android_media_AudioRecord_set_pos_update_period}, 6369066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project {"native_get_pos_update_period", 6379066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project "()I", (void *)android_media_AudioRecord_get_pos_update_period}, 6389066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project {"native_get_min_buff_size", 6399066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project "(III)I", (void *)android_media_AudioRecord_get_min_buff_size}, 6409066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}; 6419066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 6429066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project// field names found in android/media/AudioRecord.java 6439066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project#define JAVA_POSTEVENT_CALLBACK_NAME "postEventFromNative" 6449066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project#define JAVA_NATIVERECORDERINJAVAOBJ_FIELD_NAME "mNativeRecorderInJavaObj" 6459066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project#define JAVA_NATIVECALLBACKINFO_FIELD_NAME "mNativeCallbackCookie" 6469066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 6479066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project// ---------------------------------------------------------------------------- 6489066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectint register_android_media_AudioRecord(JNIEnv *env) 6499066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project{ 6509066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project javaAudioRecordFields.postNativeEventInJava = NULL; 6519066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project javaAudioRecordFields.nativeRecorderInJavaObj = NULL; 6529066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project javaAudioRecordFields.nativeCallbackCookie = NULL; 65318db49a46259020387c40fff36d92edc1087a366Glenn Kasten 6549066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 6559066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project // Get the AudioRecord class 656ed6b9dff563c5e22f040ff37e12c0d771e0478aeAndreas Gampe jclass audioRecordClass = FindClassOrDie(env, kClassPathName); 6579066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project // Get the postEvent method 658ed6b9dff563c5e22f040ff37e12c0d771e0478aeAndreas Gampe javaAudioRecordFields.postNativeEventInJava = GetStaticMethodIDOrDie(env, 659ed6b9dff563c5e22f040ff37e12c0d771e0478aeAndreas Gampe audioRecordClass, JAVA_POSTEVENT_CALLBACK_NAME, 660ed6b9dff563c5e22f040ff37e12c0d771e0478aeAndreas Gampe "(Ljava/lang/Object;IIILjava/lang/Object;)V"); 6619066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 6629066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project // Get the variables 6639066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project // mNativeRecorderInJavaObj 664ed6b9dff563c5e22f040ff37e12c0d771e0478aeAndreas Gampe javaAudioRecordFields.nativeRecorderInJavaObj = GetFieldIDOrDie(env, 665ed6b9dff563c5e22f040ff37e12c0d771e0478aeAndreas Gampe audioRecordClass, JAVA_NATIVERECORDERINJAVAOBJ_FIELD_NAME, "J"); 6669066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project // mNativeCallbackCookie 667ed6b9dff563c5e22f040ff37e12c0d771e0478aeAndreas Gampe javaAudioRecordFields.nativeCallbackCookie = GetFieldIDOrDie(env, 668ed6b9dff563c5e22f040ff37e12c0d771e0478aeAndreas Gampe audioRecordClass, JAVA_NATIVECALLBACKINFO_FIELD_NAME, "J"); 6699066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 670701d6ff12f36bf5e9de0dafdaced06744fd411ebJean-Michel Trivi // Get the AudioAttributes class and fields 671ed6b9dff563c5e22f040ff37e12c0d771e0478aeAndreas Gampe jclass audioAttrClass = FindClassOrDie(env, kAudioAttributesClassPathName); 672ed6b9dff563c5e22f040ff37e12c0d771e0478aeAndreas Gampe javaAudioAttrFields.fieldRecSource = GetFieldIDOrDie(env, audioAttrClass, "mSource", "I"); 673ed6b9dff563c5e22f040ff37e12c0d771e0478aeAndreas Gampe javaAudioAttrFields.fieldFlags = GetFieldIDOrDie(env, audioAttrClass, "mFlags", "I"); 674ed6b9dff563c5e22f040ff37e12c0d771e0478aeAndreas Gampe javaAudioAttrFields.fieldFormattedTags = GetFieldIDOrDie(env, 675ed6b9dff563c5e22f040ff37e12c0d771e0478aeAndreas Gampe audioAttrClass, "mFormattedTags", "Ljava/lang/String;"); 676701d6ff12f36bf5e9de0dafdaced06744fd411ebJean-Michel Trivi 677ed6b9dff563c5e22f040ff37e12c0d771e0478aeAndreas Gampe return RegisterMethodsOrDie(env, kClassPathName, gMethods, NELEM(gMethods)); 6789066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project} 6799066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 6809066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project// ---------------------------------------------------------------------------- 681