blob: 1ea4ed1b17384a90fcb2800518bb61220713c8ec [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17//#define LOG_NDEBUG 0
18
19#define LOG_TAG "AudioRecord-JNI"
20
Kévin PETIT95ece352014-02-13 11:02:27 +000021#include <inttypes.h>
Glenn Kastenc81d31c2012-03-13 14:46:23 -070022#include <jni.h>
Steven Moreland2279b252017-07-19 09:50:45 -070023#include <nativehelper/JNIHelp.h>
Andreas Gampeed6b9df2014-11-20 22:02:20 -080024#include "core_jni_helpers.h"
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080025
Glenn Kastenc81d31c2012-03-13 14:46:23 -070026#include <utils/Log.h>
27#include <media/AudioRecord.h>
jiabin23675f62018-01-17 18:05:25 -080028#include <media/MicrophoneInfo.h>
29#include <vector>
Dima Zavin24fc2fb2011-04-19 22:30:36 -070030
Steven Moreland2279b252017-07-19 09:50:45 -070031#include <nativehelper/ScopedUtfChars.h>
Svet Ganovfa5ecdc2015-04-28 12:03:28 -070032
Glenn Kastenfe834d32014-01-08 14:49:08 -080033#include "android_media_AudioFormat.h"
Eric Laurentbc11a692014-05-16 12:19:25 -070034#include "android_media_AudioErrors.h"
Eric Laurent4bcdba82015-05-01 11:37:49 -070035#include "android_media_DeviceCallback.h"
Ray Essick510225b2018-01-24 14:27:16 -080036#include "android_media_MediaMetricsJNI.h"
jiabin23675f62018-01-17 18:05:25 -080037#include "android_media_MicrophoneInfo.h"
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080038
39// ----------------------------------------------------------------------------
40
41using namespace android;
42
43// ----------------------------------------------------------------------------
44static const char* const kClassPathName = "android/media/AudioRecord";
Jean-Michel Trivi701d6ff2014-07-16 07:51:22 -070045static const char* const kAudioAttributesClassPathName = "android/media/AudioAttributes";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080046
jiabin23675f62018-01-17 18:05:25 -080047static jclass gArrayListClass;
48static struct {
49 jmethodID add;
50} gArrayListMethods;
51
Jean-Michel Trivi701d6ff2014-07-16 07:51:22 -070052struct audio_record_fields_t {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080053 // these fields provide access from C++ to the...
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080054 jmethodID postNativeEventInJava; //... event post callback method
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080055 jfieldID nativeRecorderInJavaObj; // provides access to the C++ AudioRecord object
56 jfieldID nativeCallbackCookie; // provides access to the AudioRecord callback data
Eric Laurent4bcdba82015-05-01 11:37:49 -070057 jfieldID nativeDeviceCallback; // provides access to the JNIDeviceCallback instance
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080058};
Jean-Michel Trivi701d6ff2014-07-16 07:51:22 -070059struct audio_attributes_fields_t {
60 jfieldID fieldRecSource; // AudioAttributes.mSource
Eric Laurentbdad1af2014-09-19 17:43:29 -070061 jfieldID fieldFlags; // AudioAttributes.mFlags
Jean-Michel Trivi701d6ff2014-07-16 07:51:22 -070062 jfieldID fieldFormattedTags;// AudioAttributes.mFormattedTags
63};
64static audio_attributes_fields_t javaAudioAttrFields;
65static audio_record_fields_t javaAudioRecordFields;
Andy Hung0ad99c02016-01-15 17:53:47 -080066static struct {
67 jfieldID fieldFramePosition; // AudioTimestamp.framePosition
68 jfieldID fieldNanoTime; // AudioTimestamp.nanoTime
69} javaAudioTimestampFields;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080070
71struct audiorecord_callback_cookie {
72 jclass audioRecord_class;
73 jobject audioRecord_ref;
Eric Laurent532bc1c2012-04-20 12:45:03 -070074 bool busy;
75 Condition cond;
76};
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080077
Eric Laurent532bc1c2012-04-20 12:45:03 -070078static Mutex sLock;
79static SortedVector <audiorecord_callback_cookie *> sAudioRecordCallBackCookies;
Dave Sparkse6335c92010-03-13 17:08:22 -080080
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080081// ----------------------------------------------------------------------------
82
Chih-Hung Hsieh0ca16ef2016-05-19 15:14:54 -070083#define AUDIORECORD_ERROR_SETUP_ZEROFRAMECOUNT (-16)
84#define AUDIORECORD_ERROR_SETUP_INVALIDCHANNELMASK (-17)
85#define AUDIORECORD_ERROR_SETUP_INVALIDFORMAT (-18)
86#define AUDIORECORD_ERROR_SETUP_INVALIDSOURCE (-19)
87#define AUDIORECORD_ERROR_SETUP_NATIVEINITFAILED (-20)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080088
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080089// ----------------------------------------------------------------------------
90static void recorderCallback(int event, void* user, void *info) {
Eric Laurent532bc1c2012-04-20 12:45:03 -070091
92 audiorecord_callback_cookie *callbackInfo = (audiorecord_callback_cookie *)user;
93 {
94 Mutex::Autolock l(sLock);
95 if (sAudioRecordCallBackCookies.indexOf(callbackInfo) < 0) {
96 return;
97 }
98 callbackInfo->busy = true;
99 }
Glenn Kasten18db49a2012-03-12 16:29:55 -0700100
Glenn Kasten5b1576c2013-07-18 16:58:19 -0700101 switch (event) {
102 case AudioRecord::EVENT_MARKER: {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800103 JNIEnv *env = AndroidRuntime::getJNIEnv();
Glenn Kastena667ff32013-07-22 07:36:34 -0700104 if (user != NULL && env != NULL) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800105 env->CallStaticVoidMethod(
Glenn Kasten18db49a2012-03-12 16:29:55 -0700106 callbackInfo->audioRecord_class,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800107 javaAudioRecordFields.postNativeEventInJava,
108 callbackInfo->audioRecord_ref, event, 0,0, NULL);
109 if (env->ExceptionCheck()) {
110 env->ExceptionDescribe();
111 env->ExceptionClear();
112 }
113 }
Glenn Kasten5b1576c2013-07-18 16:58:19 -0700114 } break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800115
Glenn Kasten5b1576c2013-07-18 16:58:19 -0700116 case AudioRecord::EVENT_NEW_POS: {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800117 JNIEnv *env = AndroidRuntime::getJNIEnv();
Glenn Kastena667ff32013-07-22 07:36:34 -0700118 if (user != NULL && env != NULL) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800119 env->CallStaticVoidMethod(
Glenn Kasten18db49a2012-03-12 16:29:55 -0700120 callbackInfo->audioRecord_class,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800121 javaAudioRecordFields.postNativeEventInJava,
122 callbackInfo->audioRecord_ref, event, 0,0, NULL);
123 if (env->ExceptionCheck()) {
124 env->ExceptionDescribe();
125 env->ExceptionClear();
126 }
127 }
Glenn Kasten5b1576c2013-07-18 16:58:19 -0700128 } break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800129 }
Glenn Kasten5b1576c2013-07-18 16:58:19 -0700130
Eric Laurent532bc1c2012-04-20 12:45:03 -0700131 {
132 Mutex::Autolock l(sLock);
133 callbackInfo->busy = false;
134 callbackInfo->cond.broadcast();
135 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800136}
137
Eric Laurent4bcdba82015-05-01 11:37:49 -0700138static sp<JNIDeviceCallback> getJniDeviceCallback(JNIEnv* env, jobject thiz)
139{
140 Mutex::Autolock l(sLock);
141 JNIDeviceCallback* const cb =
142 (JNIDeviceCallback*)env->GetLongField(thiz,
143 javaAudioRecordFields.nativeDeviceCallback);
144 return sp<JNIDeviceCallback>(cb);
145}
146
147static sp<JNIDeviceCallback> setJniDeviceCallback(JNIEnv* env,
148 jobject thiz,
149 const sp<JNIDeviceCallback>& cb)
150{
151 Mutex::Autolock l(sLock);
152 sp<JNIDeviceCallback> old =
153 (JNIDeviceCallback*)env->GetLongField(thiz,
154 javaAudioRecordFields.nativeDeviceCallback);
155 if (cb.get()) {
156 cb->incStrong((void*)setJniDeviceCallback);
157 }
158 if (old != 0) {
159 old->decStrong((void*)setJniDeviceCallback);
160 }
161 env->SetLongField(thiz, javaAudioRecordFields.nativeDeviceCallback, (jlong)cb.get());
162 return old;
163}
164
Eric Laurent532bc1c2012-04-20 12:45:03 -0700165// ----------------------------------------------------------------------------
166static sp<AudioRecord> getAudioRecord(JNIEnv* env, jobject thiz)
167{
168 Mutex::Autolock l(sLock);
169 AudioRecord* const ar =
Ashok Bhat075e9a12014-01-06 13:45:09 +0000170 (AudioRecord*)env->GetLongField(thiz, javaAudioRecordFields.nativeRecorderInJavaObj);
Eric Laurent532bc1c2012-04-20 12:45:03 -0700171 return sp<AudioRecord>(ar);
172}
173
174static sp<AudioRecord> setAudioRecord(JNIEnv* env, jobject thiz, const sp<AudioRecord>& ar)
175{
176 Mutex::Autolock l(sLock);
177 sp<AudioRecord> old =
Ashok Bhat075e9a12014-01-06 13:45:09 +0000178 (AudioRecord*)env->GetLongField(thiz, javaAudioRecordFields.nativeRecorderInJavaObj);
Eric Laurent532bc1c2012-04-20 12:45:03 -0700179 if (ar.get()) {
Mathias Agopianb1d90c82013-03-06 17:45:42 -0800180 ar->incStrong((void*)setAudioRecord);
Eric Laurent532bc1c2012-04-20 12:45:03 -0700181 }
182 if (old != 0) {
Mathias Agopianb1d90c82013-03-06 17:45:42 -0800183 old->decStrong((void*)setAudioRecord);
Eric Laurent532bc1c2012-04-20 12:45:03 -0700184 }
Ashok Bhat075e9a12014-01-06 13:45:09 +0000185 env->SetLongField(thiz, javaAudioRecordFields.nativeRecorderInJavaObj, (jlong)ar.get());
Eric Laurent532bc1c2012-04-20 12:45:03 -0700186 return old;
187}
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800188
189// ----------------------------------------------------------------------------
Ashok Bhat075e9a12014-01-06 13:45:09 +0000190static jint
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800191android_media_AudioRecord_setup(JNIEnv *env, jobject thiz, jobject weak_this,
Glenn Kasten1cbf9b32016-02-02 12:04:09 -0800192 jobject jaa, jintArray jSampleRate, jint channelMask, jint channelIndexMask,
Paul McLean9b09e532016-01-26 14:43:35 -0700193 jint audioFormat, jint buffSizeInBytes, jintArray jSession, jstring opPackageName,
194 jlong nativeRecordInJavaObj)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800195{
Steve Block71f2cf12011-10-20 11:56:00 +0100196 //ALOGV(">> Entering android_media_AudioRecord_setup");
Paul McLean9b09e532016-01-26 14:43:35 -0700197 //ALOGV("sampleRate=%d, audioFormat=%d, channel mask=%x, buffSizeInBytes=%d "
198 // "nativeRecordInJavaObj=0x%llX",
199 // sampleRateInHertz, audioFormat, channelMask, buffSizeInBytes, nativeRecordInJavaObj);
200 audio_channel_mask_t localChanMask = inChannelMaskToNative(channelMask);
Eric Laurent532bc1c2012-04-20 12:45:03 -0700201
Eric Laurent44ff4cd2011-06-18 10:34:05 -0700202 if (jSession == NULL) {
Steve Block3762c312012-01-06 19:20:56 +0000203 ALOGE("Error creating AudioRecord: invalid session ID pointer");
Eric Laurentbc11a692014-05-16 12:19:25 -0700204 return (jint) AUDIO_JAVA_ERROR;
Eric Laurent44ff4cd2011-06-18 10:34:05 -0700205 }
206
207 jint* nSession = (jint *) env->GetPrimitiveArrayCritical(jSession, NULL);
208 if (nSession == NULL) {
Steve Block3762c312012-01-06 19:20:56 +0000209 ALOGE("Error creating AudioRecord: Error retrieving session id pointer");
Eric Laurentbc11a692014-05-16 12:19:25 -0700210 return (jint) AUDIO_JAVA_ERROR;
Eric Laurent44ff4cd2011-06-18 10:34:05 -0700211 }
Glenn Kasten33b84042016-03-08 12:02:55 -0800212 audio_session_t sessionId = (audio_session_t) nSession[0];
Eric Laurent44ff4cd2011-06-18 10:34:05 -0700213 env->ReleasePrimitiveArrayCritical(jSession, nSession, 0);
214 nSession = NULL;
215
Jean-Michel Trivi701d6ff2014-07-16 07:51:22 -0700216 audio_attributes_t *paa = NULL;
Paul McLean9b09e532016-01-26 14:43:35 -0700217 sp<AudioRecord> lpRecorder = 0;
218 audiorecord_callback_cookie *lpCallbackData = NULL;
Jean-Michel Trivi701d6ff2014-07-16 07:51:22 -0700219
Paul McLean9b09e532016-01-26 14:43:35 -0700220 jclass clazz = env->GetObjectClass(thiz);
221 if (clazz == NULL) {
222 ALOGE("Can't find %s when setting up callback.", kClassPathName);
223 return (jint) AUDIORECORD_ERROR_SETUP_NATIVEINITFAILED;
Eric Laurentbdad1af2014-09-19 17:43:29 -0700224 }
Glenn Kasten18db49a2012-03-12 16:29:55 -0700225
Paul McLean9b09e532016-01-26 14:43:35 -0700226 // if we pass in an existing *Native* AudioRecord, we don't need to create/initialize one.
227 if (nativeRecordInJavaObj == 0) {
228 if (jaa == 0) {
229 ALOGE("Error creating AudioRecord: invalid audio attributes");
230 return (jint) AUDIO_JAVA_ERROR;
231 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800232
Paul McLean9b09e532016-01-26 14:43:35 -0700233 if (jSampleRate == 0) {
234 ALOGE("Error creating AudioRecord: invalid sample rates");
235 return (jint) AUDIO_JAVA_ERROR;
236 }
237 jint elements[1];
238 env->GetIntArrayRegion(jSampleRate, 0, 1, elements);
239 int sampleRateInHertz = elements[0];
240
241 // channel index mask takes priority over channel position masks.
242 if (channelIndexMask) {
243 // Java channel index masks need the representation bits set.
244 localChanMask = audio_channel_mask_from_representation_and_bits(
245 AUDIO_CHANNEL_REPRESENTATION_INDEX,
246 channelIndexMask);
247 }
248 // Java channel position masks map directly to the native definition
249
250 if (!audio_is_input_channel(localChanMask)) {
251 ALOGE("Error creating AudioRecord: channel mask %#x is not valid.", localChanMask);
252 return (jint) AUDIORECORD_ERROR_SETUP_INVALIDCHANNELMASK;
253 }
254 uint32_t channelCount = audio_channel_count_from_in_mask(localChanMask);
255
256 // compare the format against the Java constants
257 audio_format_t format = audioFormatToNative(audioFormat);
258 if (format == AUDIO_FORMAT_INVALID) {
259 ALOGE("Error creating AudioRecord: unsupported audio format %d.", audioFormat);
260 return (jint) AUDIORECORD_ERROR_SETUP_INVALIDFORMAT;
261 }
262
263 size_t bytesPerSample = audio_bytes_per_sample(format);
264
265 if (buffSizeInBytes == 0) {
266 ALOGE("Error creating AudioRecord: frameCount is 0.");
267 return (jint) AUDIORECORD_ERROR_SETUP_ZEROFRAMECOUNT;
268 }
269 size_t frameSize = channelCount * bytesPerSample;
270 size_t frameCount = buffSizeInBytes / frameSize;
271
272 ScopedUtfChars opPackageNameStr(env, opPackageName);
273
274 // create an uninitialized AudioRecord object
275 lpRecorder = new AudioRecord(String16(opPackageNameStr.c_str()));
276
277 // read the AudioAttributes values
278 paa = (audio_attributes_t *) calloc(1, sizeof(audio_attributes_t));
279 const jstring jtags =
280 (jstring) env->GetObjectField(jaa, javaAudioAttrFields.fieldFormattedTags);
281 const char* tags = env->GetStringUTFChars(jtags, NULL);
282 // copying array size -1, char array for tags was calloc'd, no need to NULL-terminate it
283 strncpy(paa->tags, tags, AUDIO_ATTRIBUTES_TAGS_MAX_SIZE - 1);
284 env->ReleaseStringUTFChars(jtags, tags);
285 paa->source = (audio_source_t) env->GetIntField(jaa, javaAudioAttrFields.fieldRecSource);
286 paa->flags = (audio_flags_mask_t)env->GetIntField(jaa, javaAudioAttrFields.fieldFlags);
287 ALOGV("AudioRecord_setup for source=%d tags=%s flags=%08x", paa->source, paa->tags, paa->flags);
288
289 audio_input_flags_t flags = AUDIO_INPUT_FLAG_NONE;
290 if (paa->flags & AUDIO_FLAG_HW_HOTWORD) {
291 flags = AUDIO_INPUT_FLAG_HW_HOTWORD;
292 }
293 // create the callback information:
294 // this data will be passed with every AudioRecord callback
295 lpCallbackData = new audiorecord_callback_cookie;
296 lpCallbackData->audioRecord_class = (jclass)env->NewGlobalRef(clazz);
297 // we use a weak reference so the AudioRecord object can be garbage collected.
298 lpCallbackData->audioRecord_ref = env->NewGlobalRef(weak_this);
299 lpCallbackData->busy = false;
300
301 const status_t status = lpRecorder->set(paa->source,
302 sampleRateInHertz,
303 format, // word length, PCM
304 localChanMask,
305 frameCount,
306 recorderCallback,// callback_t
307 lpCallbackData,// void* user
308 0, // notificationFrames,
309 true, // threadCanCallJava
310 sessionId,
311 AudioRecord::TRANSFER_DEFAULT,
312 flags,
313 -1, -1, // default uid, pid
314 paa);
315
316 if (status != NO_ERROR) {
317 ALOGE("Error creating AudioRecord instance: initialization check failed with status %d.",
318 status);
319 goto native_init_failure;
320 }
321 } else { // end if nativeRecordInJavaObj == 0)
322 lpRecorder = (AudioRecord*)nativeRecordInJavaObj;
323 // TODO: We need to find out which members of the Java AudioRecord might need to be
324 // initialized from the Native AudioRecord
325 // these are directly returned from getters:
326 // mSampleRate
327 // mRecordSource
328 // mAudioFormat
329 // mChannelMask
330 // mChannelCount
331 // mState (?)
332 // mRecordingState (?)
333 // mPreferredDevice
334
335 // create the callback information:
336 // this data will be passed with every AudioRecord callback
337 lpCallbackData = new audiorecord_callback_cookie;
338 lpCallbackData->audioRecord_class = (jclass)env->NewGlobalRef(clazz);
339 // we use a weak reference so the AudioRecord object can be garbage collected.
340 lpCallbackData->audioRecord_ref = env->NewGlobalRef(weak_this);
341 lpCallbackData->busy = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800342 }
343
Eric Laurent44ff4cd2011-06-18 10:34:05 -0700344 nSession = (jint *) env->GetPrimitiveArrayCritical(jSession, NULL);
345 if (nSession == NULL) {
Steve Block3762c312012-01-06 19:20:56 +0000346 ALOGE("Error creating AudioRecord: Error retrieving session id pointer");
Eric Laurent44ff4cd2011-06-18 10:34:05 -0700347 goto native_init_failure;
348 }
Glenn Kastenb3db2132012-01-19 08:59:58 -0800349 // read the audio session ID back from AudioRecord in case a new session was created during set()
Eric Laurent44ff4cd2011-06-18 10:34:05 -0700350 nSession[0] = lpRecorder->getSessionId();
351 env->ReleasePrimitiveArrayCritical(jSession, nSession, 0);
352 nSession = NULL;
353
Glenn Kasten1cbf9b32016-02-02 12:04:09 -0800354 {
355 const jint elements[1] = { (jint) lpRecorder->getSampleRate() };
356 env->SetIntArrayRegion(jSampleRate, 0, 1, elements);
357 }
358
Eric Laurent532bc1c2012-04-20 12:45:03 -0700359 { // scope for the lock
360 Mutex::Autolock l(sLock);
361 sAudioRecordCallBackCookies.add(lpCallbackData);
362 }
Glenn Kasten18db49a2012-03-12 16:29:55 -0700363 // save our newly created C++ AudioRecord in the "nativeRecorderInJavaObj" field
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800364 // of the Java object
Eric Laurent532bc1c2012-04-20 12:45:03 -0700365 setAudioRecord(env, thiz, lpRecorder);
Glenn Kasten18db49a2012-03-12 16:29:55 -0700366
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800367 // save our newly created callback information in the "nativeCallbackCookie" field
368 // of the Java object (in mNativeCallbackCookie) so we can free the memory in finalize()
Ashok Bhat075e9a12014-01-06 13:45:09 +0000369 env->SetLongField(thiz, javaAudioRecordFields.nativeCallbackCookie, (jlong)lpCallbackData);
Glenn Kasten18db49a2012-03-12 16:29:55 -0700370
Jean-Michel Trivi4ce27b52018-06-05 12:01:35 -0700371 if (paa != NULL) {
372 // audio attributes were copied in AudioRecord creation
373 free(paa);
374 paa = NULL;
375 }
376
Eric Laurentbc11a692014-05-16 12:19:25 -0700377 return (jint) AUDIO_JAVA_SUCCESS;
Glenn Kasten18db49a2012-03-12 16:29:55 -0700378
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800379 // failure:
380native_init_failure:
Jean-Michel Trivi4ce27b52018-06-05 12:01:35 -0700381 if (paa != NULL) {
382 free(paa);
383 }
Jean-Michel Trivi4bac5a32009-07-17 12:05:31 -0700384 env->DeleteGlobalRef(lpCallbackData->audioRecord_class);
385 env->DeleteGlobalRef(lpCallbackData->audioRecord_ref);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800386 delete lpCallbackData;
Ashok Bhat075e9a12014-01-06 13:45:09 +0000387 env->SetLongField(thiz, javaAudioRecordFields.nativeCallbackCookie, 0);
Glenn Kasten18db49a2012-03-12 16:29:55 -0700388
Glenn Kasten14d226a2015-05-18 13:53:39 -0700389 // lpRecorder goes out of scope, so reference count drops to zero
Ashok Bhat075e9a12014-01-06 13:45:09 +0000390 return (jint) AUDIORECORD_ERROR_SETUP_NATIVEINITFAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800391}
392
393
394
395// ----------------------------------------------------------------------------
Ashok Bhat075e9a12014-01-06 13:45:09 +0000396static jint
Eric Laurent505e5c82012-03-29 15:19:36 -0700397android_media_AudioRecord_start(JNIEnv *env, jobject thiz, jint event, jint triggerSession)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800398{
Eric Laurent532bc1c2012-04-20 12:45:03 -0700399 sp<AudioRecord> lpRecorder = getAudioRecord(env, thiz);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800400 if (lpRecorder == NULL ) {
401 jniThrowException(env, "java/lang/IllegalStateException", NULL);
Eric Laurentbc11a692014-05-16 12:19:25 -0700402 return (jint) AUDIO_JAVA_ERROR;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800403 }
Glenn Kasten18db49a2012-03-12 16:29:55 -0700404
Eric Laurentbc11a692014-05-16 12:19:25 -0700405 return nativeToJavaStatus(
Glenn Kasten33b84042016-03-08 12:02:55 -0800406 lpRecorder->start((AudioSystem::sync_event_t)event, (audio_session_t) triggerSession));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800407}
408
409
410// ----------------------------------------------------------------------------
411static void
412android_media_AudioRecord_stop(JNIEnv *env, jobject thiz)
413{
Eric Laurent532bc1c2012-04-20 12:45:03 -0700414 sp<AudioRecord> lpRecorder = getAudioRecord(env, thiz);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800415 if (lpRecorder == NULL ) {
416 jniThrowException(env, "java/lang/IllegalStateException", NULL);
417 return;
418 }
419
420 lpRecorder->stop();
Steve Block71f2cf12011-10-20 11:56:00 +0100421 //ALOGV("Called lpRecorder->stop()");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800422}
423
424
425// ----------------------------------------------------------------------------
Dave Sparkse6335c92010-03-13 17:08:22 -0800426
Eric Laurent532bc1c2012-04-20 12:45:03 -0700427#define CALLBACK_COND_WAIT_TIMEOUT_MS 1000
428static void android_media_AudioRecord_release(JNIEnv *env, jobject thiz) {
429 sp<AudioRecord> lpRecorder = setAudioRecord(env, thiz, 0);
430 if (lpRecorder == NULL) {
431 return;
432 }
Glenn Kasten2fbf25b2014-03-28 15:41:58 -0700433 ALOGV("About to delete lpRecorder: %p", lpRecorder.get());
Eric Laurent532bc1c2012-04-20 12:45:03 -0700434 lpRecorder->stop();
435
Ashok Bhat075e9a12014-01-06 13:45:09 +0000436 audiorecord_callback_cookie *lpCookie = (audiorecord_callback_cookie *)env->GetLongField(
Dave Sparkse6335c92010-03-13 17:08:22 -0800437 thiz, javaAudioRecordFields.nativeCallbackCookie);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800438
Dave Sparkse6335c92010-03-13 17:08:22 -0800439 // reset the native resources in the Java object so any attempt to access
440 // them after a call to release fails.
Ashok Bhat075e9a12014-01-06 13:45:09 +0000441 env->SetLongField(thiz, javaAudioRecordFields.nativeCallbackCookie, 0);
Dave Sparkse6335c92010-03-13 17:08:22 -0800442
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800443 // delete the callback information
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800444 if (lpCookie) {
Eric Laurent532bc1c2012-04-20 12:45:03 -0700445 Mutex::Autolock l(sLock);
Glenn Kasten2fbf25b2014-03-28 15:41:58 -0700446 ALOGV("deleting lpCookie: %p", lpCookie);
Eric Laurent532bc1c2012-04-20 12:45:03 -0700447 while (lpCookie->busy) {
448 if (lpCookie->cond.waitRelative(sLock,
449 milliseconds(CALLBACK_COND_WAIT_TIMEOUT_MS)) !=
450 NO_ERROR) {
451 break;
452 }
453 }
454 sAudioRecordCallBackCookies.remove(lpCookie);
Jean-Michel Trivi4bac5a32009-07-17 12:05:31 -0700455 env->DeleteGlobalRef(lpCookie->audioRecord_class);
456 env->DeleteGlobalRef(lpCookie->audioRecord_ref);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800457 delete lpCookie;
458 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800459}
460
461
462// ----------------------------------------------------------------------------
Dave Sparkse6335c92010-03-13 17:08:22 -0800463static void android_media_AudioRecord_finalize(JNIEnv *env, jobject thiz) {
464 android_media_AudioRecord_release(env, thiz);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800465}
466
Andy Hung0e9e2f32015-04-13 23:47:18 -0700467// overloaded JNI array helper functions
468static inline
469jbyte *envGetArrayElements(JNIEnv *env, jbyteArray array, jboolean *isCopy) {
470 return env->GetByteArrayElements(array, isCopy);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800471}
472
Andy Hung0e9e2f32015-04-13 23:47:18 -0700473static inline
474void envReleaseArrayElements(JNIEnv *env, jbyteArray array, jbyte *elems, jint mode) {
475 env->ReleaseByteArrayElements(array, elems, mode);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800476}
477
Andy Hung0e9e2f32015-04-13 23:47:18 -0700478static inline
479jshort *envGetArrayElements(JNIEnv *env, jshortArray array, jboolean *isCopy) {
480 return env->GetShortArrayElements(array, isCopy);
481}
482
483static inline
484void envReleaseArrayElements(JNIEnv *env, jshortArray array, jshort *elems, jint mode) {
485 env->ReleaseShortArrayElements(array, elems, mode);
486}
487
488static inline
489jfloat *envGetArrayElements(JNIEnv *env, jfloatArray array, jboolean *isCopy) {
490 return env->GetFloatArrayElements(array, isCopy);
491}
492
493static inline
494void envReleaseArrayElements(JNIEnv *env, jfloatArray array, jfloat *elems, jint mode) {
495 env->ReleaseFloatArrayElements(array, elems, mode);
496}
497
498static inline
499jint interpretReadSizeError(ssize_t readSize) {
Eric Laurent219de732016-05-23 12:41:50 -0700500 if (readSize == WOULD_BLOCK) {
Andy Hung0e9e2f32015-04-13 23:47:18 -0700501 return (jint)0;
Eric Laurent219de732016-05-23 12:41:50 -0700502 } else if (readSize == NO_INIT) {
503 return AUDIO_JAVA_DEAD_OBJECT;
504 } else {
505 ALOGE("Error %zd during AudioRecord native read", readSize);
506 return nativeToJavaStatus(readSize);
Andy Hung0e9e2f32015-04-13 23:47:18 -0700507 }
508}
509
510template <typename T>
511static jint android_media_AudioRecord_readInArray(JNIEnv *env, jobject thiz,
512 T javaAudioData,
513 jint offsetInSamples, jint sizeInSamples,
514 jboolean isReadBlocking) {
Andy Hung58b0f3f2015-03-27 17:59:45 -0700515 // get the audio recorder from which we'll read new audio samples
516 sp<AudioRecord> lpRecorder = getAudioRecord(env, thiz);
517 if (lpRecorder == NULL) {
518 ALOGE("Unable to retrieve AudioRecord object");
519 return (jint)AUDIO_JAVA_INVALID_OPERATION;
520 }
521
522 if (javaAudioData == NULL) {
Andy Hung0e9e2f32015-04-13 23:47:18 -0700523 ALOGE("Invalid Java array to store recorded audio");
524 return (jint)AUDIO_JAVA_BAD_VALUE;
525 }
Andy Hung58b0f3f2015-03-27 17:59:45 -0700526
Andy Hung58b0f3f2015-03-27 17:59:45 -0700527 // NOTE: We may use GetPrimitiveArrayCritical() when the JNI implementation changes in such
528 // a way that it becomes much more efficient. When doing so, we will have to prevent the
529 // AudioSystem callback to be called while in critical section (in case of media server
530 // process crash for instance)
Andy Hung0e9e2f32015-04-13 23:47:18 -0700531
532 // get the pointer to where we'll record the audio
533 auto *recordBuff = envGetArrayElements(env, javaAudioData, NULL);
Andy Hung58b0f3f2015-03-27 17:59:45 -0700534 if (recordBuff == NULL) {
535 ALOGE("Error retrieving destination for recorded audio data");
536 return (jint)AUDIO_JAVA_BAD_VALUE;
537 }
538
539 // read the new audio data from the native AudioRecord object
Andy Hung0e9e2f32015-04-13 23:47:18 -0700540 const size_t sizeInBytes = sizeInSamples * sizeof(*recordBuff);
541 ssize_t readSize = lpRecorder->read(
542 recordBuff + offsetInSamples, sizeInBytes, isReadBlocking == JNI_TRUE /* blocking */);
Andy Hung58b0f3f2015-03-27 17:59:45 -0700543
Andy Hung0e9e2f32015-04-13 23:47:18 -0700544 envReleaseArrayElements(env, javaAudioData, recordBuff, 0);
Andy Hung58b0f3f2015-03-27 17:59:45 -0700545
546 if (readSize < 0) {
Andy Hung0e9e2f32015-04-13 23:47:18 -0700547 return interpretReadSizeError(readSize);
Andy Hung58b0f3f2015-03-27 17:59:45 -0700548 }
Andy Hung0e9e2f32015-04-13 23:47:18 -0700549 return (jint)(readSize / sizeof(*recordBuff));
Andy Hung58b0f3f2015-03-27 17:59:45 -0700550}
551
552// ----------------------------------------------------------------------------
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800553static jint android_media_AudioRecord_readInDirectBuffer(JNIEnv *env, jobject thiz,
Andy Hung0e9e2f32015-04-13 23:47:18 -0700554 jobject jBuffer, jint sizeInBytes,
555 jboolean isReadBlocking) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800556 // get the audio recorder from which we'll read new audio samples
Eric Laurent532bc1c2012-04-20 12:45:03 -0700557 sp<AudioRecord> lpRecorder = getAudioRecord(env, thiz);
Glenn Kasten18db49a2012-03-12 16:29:55 -0700558 if (lpRecorder==NULL)
Andy Hung0e9e2f32015-04-13 23:47:18 -0700559 return (jint)AUDIO_JAVA_INVALID_OPERATION;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800560
561 // direct buffer and direct access supported?
562 long capacity = env->GetDirectBufferCapacity(jBuffer);
Glenn Kasten18db49a2012-03-12 16:29:55 -0700563 if (capacity == -1) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800564 // buffer direct access is not supported
Steve Block3762c312012-01-06 19:20:56 +0000565 ALOGE("Buffer direct access is not supported, can't record");
Andy Hung0e9e2f32015-04-13 23:47:18 -0700566 return (jint)AUDIO_JAVA_BAD_VALUE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800567 }
Steve Block71f2cf12011-10-20 11:56:00 +0100568 //ALOGV("capacity = %ld", capacity);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800569 jbyte* nativeFromJavaBuf = (jbyte*) env->GetDirectBufferAddress(jBuffer);
Glenn Kasten18db49a2012-03-12 16:29:55 -0700570 if (nativeFromJavaBuf==NULL) {
Steve Block3762c312012-01-06 19:20:56 +0000571 ALOGE("Buffer direct access is not supported, can't record");
Andy Hung0e9e2f32015-04-13 23:47:18 -0700572 return (jint)AUDIO_JAVA_BAD_VALUE;
Glenn Kasten18db49a2012-03-12 16:29:55 -0700573 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800574
575 // read new data from the recorder
Eric Laurent357263d2013-09-09 10:31:59 -0700576 ssize_t readSize = lpRecorder->read(nativeFromJavaBuf,
Andy Hung0e9e2f32015-04-13 23:47:18 -0700577 capacity < sizeInBytes ? capacity : sizeInBytes,
578 isReadBlocking == JNI_TRUE /* blocking */);
Eric Laurent357263d2013-09-09 10:31:59 -0700579 if (readSize < 0) {
Andy Hung0e9e2f32015-04-13 23:47:18 -0700580 return interpretReadSizeError(readSize);
Eric Laurent357263d2013-09-09 10:31:59 -0700581 }
582 return (jint)readSize;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800583}
584
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800585// ----------------------------------------------------------------------------
Andy Hunge90a0a82015-05-19 15:44:31 -0700586static jint android_media_AudioRecord_get_buffer_size_in_frames(JNIEnv *env, jobject thiz) {
Andy Hung864ae672015-04-14 11:06:48 -0700587 sp<AudioRecord> lpRecorder = getAudioRecord(env, thiz);
588 if (lpRecorder == NULL) {
589 jniThrowException(env, "java/lang/IllegalStateException",
Andy Hunge90a0a82015-05-19 15:44:31 -0700590 "Unable to retrieve AudioRecord pointer for frameCount()");
Andy Hung864ae672015-04-14 11:06:48 -0700591 return (jint)AUDIO_JAVA_ERROR;
592 }
593 return lpRecorder->frameCount();
594}
595
596// ----------------------------------------------------------------------------
Glenn Kasten18db49a2012-03-12 16:29:55 -0700597static jint android_media_AudioRecord_set_marker_pos(JNIEnv *env, jobject thiz,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800598 jint markerPos) {
Glenn Kasten18db49a2012-03-12 16:29:55 -0700599
Eric Laurent532bc1c2012-04-20 12:45:03 -0700600 sp<AudioRecord> lpRecorder = getAudioRecord(env, thiz);
601 if (lpRecorder == NULL) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800602 jniThrowException(env, "java/lang/IllegalStateException",
603 "Unable to retrieve AudioRecord pointer for setMarkerPosition()");
Eric Laurentbc11a692014-05-16 12:19:25 -0700604 return (jint)AUDIO_JAVA_ERROR;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800605 }
Eric Laurentbc11a692014-05-16 12:19:25 -0700606 return nativeToJavaStatus( lpRecorder->setMarkerPosition(markerPos) );
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800607}
608
609
610// ----------------------------------------------------------------------------
611static jint android_media_AudioRecord_get_marker_pos(JNIEnv *env, jobject thiz) {
Glenn Kasten18db49a2012-03-12 16:29:55 -0700612
Eric Laurent532bc1c2012-04-20 12:45:03 -0700613 sp<AudioRecord> lpRecorder = getAudioRecord(env, thiz);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800614 uint32_t markerPos = 0;
Glenn Kasten18db49a2012-03-12 16:29:55 -0700615
Eric Laurent532bc1c2012-04-20 12:45:03 -0700616 if (lpRecorder == NULL) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800617 jniThrowException(env, "java/lang/IllegalStateException",
618 "Unable to retrieve AudioRecord pointer for getMarkerPosition()");
Eric Laurentbc11a692014-05-16 12:19:25 -0700619 return (jint)AUDIO_JAVA_ERROR;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800620 }
Eric Laurent532bc1c2012-04-20 12:45:03 -0700621 lpRecorder->getMarkerPosition(&markerPos);
622 return (jint)markerPos;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800623}
624
625
626// ----------------------------------------------------------------------------
627static jint android_media_AudioRecord_set_pos_update_period(JNIEnv *env, jobject thiz,
628 jint period) {
Glenn Kasten18db49a2012-03-12 16:29:55 -0700629
Eric Laurent532bc1c2012-04-20 12:45:03 -0700630 sp<AudioRecord> lpRecorder = getAudioRecord(env, thiz);
Glenn Kasten18db49a2012-03-12 16:29:55 -0700631
Eric Laurent532bc1c2012-04-20 12:45:03 -0700632 if (lpRecorder == NULL) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800633 jniThrowException(env, "java/lang/IllegalStateException",
634 "Unable to retrieve AudioRecord pointer for setPositionUpdatePeriod()");
Eric Laurentbc11a692014-05-16 12:19:25 -0700635 return (jint)AUDIO_JAVA_ERROR;
Glenn Kasten18db49a2012-03-12 16:29:55 -0700636 }
Eric Laurentbc11a692014-05-16 12:19:25 -0700637 return nativeToJavaStatus( lpRecorder->setPositionUpdatePeriod(period) );
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800638}
639
640
641// ----------------------------------------------------------------------------
642static jint android_media_AudioRecord_get_pos_update_period(JNIEnv *env, jobject thiz) {
Glenn Kasten18db49a2012-03-12 16:29:55 -0700643
Eric Laurent532bc1c2012-04-20 12:45:03 -0700644 sp<AudioRecord> lpRecorder = getAudioRecord(env, thiz);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800645 uint32_t period = 0;
Glenn Kasten18db49a2012-03-12 16:29:55 -0700646
Eric Laurent532bc1c2012-04-20 12:45:03 -0700647 if (lpRecorder == NULL) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800648 jniThrowException(env, "java/lang/IllegalStateException",
649 "Unable to retrieve AudioRecord pointer for getPositionUpdatePeriod()");
Eric Laurentbc11a692014-05-16 12:19:25 -0700650 return (jint)AUDIO_JAVA_ERROR;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800651 }
Eric Laurent532bc1c2012-04-20 12:45:03 -0700652 lpRecorder->getPositionUpdatePeriod(&period);
653 return (jint)period;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800654}
655
656
657// ----------------------------------------------------------------------------
658// returns the minimum required size for the successful creation of an AudioRecord instance.
659// returns 0 if the parameter combination is not supported.
660// return -1 if there was an error querying the buffer size.
661static jint android_media_AudioRecord_get_min_buff_size(JNIEnv *env, jobject thiz,
Glenn Kasten5b8fd442013-11-14 09:44:14 -0800662 jint sampleRateInHertz, jint channelCount, jint audioFormat) {
Chia-chi Yehc3308072010-08-19 17:14:36 +0800663
Eric Laurent532bc1c2012-04-20 12:45:03 -0700664 ALOGV(">> android_media_AudioRecord_get_min_buff_size(%d, %d, %d)",
Glenn Kasten5b8fd442013-11-14 09:44:14 -0800665 sampleRateInHertz, channelCount, audioFormat);
Chia-chi Yehc3308072010-08-19 17:14:36 +0800666
Glenn Kastenfd1e3df2012-11-13 15:21:06 -0800667 size_t frameCount = 0;
Glenn Kastena5a42382013-12-19 12:34:56 -0800668 audio_format_t format = audioFormatToNative(audioFormat);
Chia-chi Yehc3308072010-08-19 17:14:36 +0800669 status_t result = AudioRecord::getMinFrameCount(&frameCount,
670 sampleRateInHertz,
Glenn Kastena5a42382013-12-19 12:34:56 -0800671 format,
Glenn Kasten5b8fd442013-11-14 09:44:14 -0800672 audio_channel_in_mask_from_count(channelCount));
Chia-chi Yehc3308072010-08-19 17:14:36 +0800673
674 if (result == BAD_VALUE) {
675 return 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800676 }
Chia-chi Yehc3308072010-08-19 17:14:36 +0800677 if (result != NO_ERROR) {
678 return -1;
679 }
Glenn Kasten5b8fd442013-11-14 09:44:14 -0800680 return frameCount * channelCount * audio_bytes_per_sample(format);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800681}
682
Paul McLean2d6de4c2015-04-17 13:13:28 -0600683static jboolean android_media_AudioRecord_setInputDevice(
684 JNIEnv *env, jobject thiz, jint device_id) {
685
Paul McLean6bd27e12015-04-24 14:01:29 -0600686 sp<AudioRecord> lpRecorder = getAudioRecord(env, thiz);
Eric Laurent4bcdba82015-05-01 11:37:49 -0700687 if (lpRecorder == 0) {
Paul McLeancef696e2015-05-21 08:51:18 -0700688 return false;
Eric Laurent4bcdba82015-05-01 11:37:49 -0700689 }
Paul McLean6bd27e12015-04-24 14:01:29 -0600690 return lpRecorder->setInputDevice(device_id) == NO_ERROR;
Paul McLean2d6de4c2015-04-17 13:13:28 -0600691}
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800692
Eric Laurent4bcdba82015-05-01 11:37:49 -0700693static jint android_media_AudioRecord_getRoutedDeviceId(
694 JNIEnv *env, jobject thiz) {
695
696 sp<AudioRecord> lpRecorder = getAudioRecord(env, thiz);
697 if (lpRecorder == 0) {
698 return 0;
699 }
700 return (jint)lpRecorder->getRoutedDeviceId();
701}
702
703static void android_media_AudioRecord_enableDeviceCallback(
704 JNIEnv *env, jobject thiz) {
705
706 sp<AudioRecord> lpRecorder = getAudioRecord(env, thiz);
707 if (lpRecorder == 0) {
708 return;
709 }
710 sp<JNIDeviceCallback> cb = getJniDeviceCallback(env, thiz);
711 if (cb != 0) {
712 return;
713 }
714 audiorecord_callback_cookie *cookie =
715 (audiorecord_callback_cookie *)env->GetLongField(thiz,
716 javaAudioRecordFields.nativeCallbackCookie);
717 if (cookie == NULL) {
718 return;
719 }
720
721 cb = new JNIDeviceCallback(env, thiz, cookie->audioRecord_ref,
722 javaAudioRecordFields.postNativeEventInJava);
723 status_t status = lpRecorder->addAudioDeviceCallback(cb);
724 if (status == NO_ERROR) {
725 setJniDeviceCallback(env, thiz, cb);
726 }
727}
728
729static void android_media_AudioRecord_disableDeviceCallback(
730 JNIEnv *env, jobject thiz) {
731
732 sp<AudioRecord> lpRecorder = getAudioRecord(env, thiz);
733 if (lpRecorder == 0) {
734 return;
735 }
736 sp<JNIDeviceCallback> cb = setJniDeviceCallback(env, thiz, 0);
737 if (cb != 0) {
738 lpRecorder->removeAudioDeviceCallback(cb);
739 }
740}
741
Andy Hung0ad99c02016-01-15 17:53:47 -0800742// ----------------------------------------------------------------------------
743static jint android_media_AudioRecord_get_timestamp(JNIEnv *env, jobject thiz,
744 jobject timestamp, jint timebase) {
745 sp<AudioRecord> lpRecorder = getAudioRecord(env, thiz);
Eric Laurent4bcdba82015-05-01 11:37:49 -0700746
Andy Hung0ad99c02016-01-15 17:53:47 -0800747 if (lpRecorder == NULL) {
748 jniThrowException(env, "java/lang/IllegalStateException",
749 "Unable to retrieve AudioRecord pointer for getTimestamp()");
750 return (jint)AUDIO_JAVA_ERROR;
751 }
752
Andy Hung0ad99c02016-01-15 17:53:47 -0800753 ExtendedTimestamp ts;
Andy Hungf4d81bd2016-01-28 17:47:56 -0800754 jint status = nativeToJavaStatus(lpRecorder->getTimestamp(&ts));
Andy Hung0ad99c02016-01-15 17:53:47 -0800755
756 if (status == AUDIO_JAVA_SUCCESS) {
757 // set the data
758 int64_t position, time;
759
760 status = nativeToJavaStatus(ts.getBestTimestamp(&position, &time, timebase));
761 if (status == AUDIO_JAVA_SUCCESS) {
762 env->SetLongField(
763 timestamp, javaAudioTimestampFields.fieldFramePosition, position);
764 env->SetLongField(
765 timestamp, javaAudioTimestampFields.fieldNanoTime, time);
766 }
767 }
768 return status;
Andy Hung0ad99c02016-01-15 17:53:47 -0800769}
Eric Laurent4bcdba82015-05-01 11:37:49 -0700770
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800771// ----------------------------------------------------------------------------
Ray Essick510225b2018-01-24 14:27:16 -0800772static jobject
773android_media_AudioRecord_native_getMetrics(JNIEnv *env, jobject thiz)
774{
775 ALOGV("android_media_AudioRecord_native_getMetrics");
776
777 sp<AudioRecord> lpRecord = getAudioRecord(env, thiz);
778
779 if (lpRecord == NULL) {
780 ALOGE("Unable to retrieve AudioRecord pointer for getMetrics()");
781 jniThrowException(env, "java/lang/IllegalStateException", NULL);
782 return (jobject) NULL;
783 }
784
785 // get what we have for the metrics from the record session
786 MediaAnalyticsItem *item = NULL;
787
788 status_t err = lpRecord->getMetrics(item);
789 if (err != OK) {
790 ALOGE("getMetrics failed");
791 jniThrowException(env, "java/lang/IllegalStateException", NULL);
792 return (jobject) NULL;
793 }
794
795 jobject mybundle = MediaMetricsJNI::writeMetricsToBundle(env, item, NULL /* mybundle */);
796
797 // housekeeping
798 delete item;
799 item = NULL;
800
801 return mybundle;
802}
803
804// ----------------------------------------------------------------------------
jiabin23675f62018-01-17 18:05:25 -0800805static jint android_media_AudioRecord_get_active_microphones(JNIEnv *env,
806 jobject thiz, jobject jActiveMicrophones) {
807 if (jActiveMicrophones == NULL) {
808 ALOGE("jActiveMicrophones is null");
809 return (jint)AUDIO_JAVA_BAD_VALUE;
810 }
811 if (!env->IsInstanceOf(jActiveMicrophones, gArrayListClass)) {
812 ALOGE("getActiveMicrophones not an arraylist");
813 return (jint)AUDIO_JAVA_BAD_VALUE;
814 }
815
816 sp<AudioRecord> lpRecorder = getAudioRecord(env, thiz);
817 if (lpRecorder == NULL) {
818 jniThrowException(env, "java/lang/IllegalStateException",
819 "Unable to retrieve AudioRecord pointer for getActiveMicrophones()");
820 return (jint)AUDIO_JAVA_ERROR;
821 }
822
823 jint jStatus = AUDIO_JAVA_SUCCESS;
824 std::vector<media::MicrophoneInfo> activeMicrophones;
825 status_t status = lpRecorder->getActiveMicrophones(&activeMicrophones);
826 if (status != NO_ERROR) {
827 ALOGE_IF(status != NO_ERROR, "AudioRecord::getActiveMicrophones error %d", status);
828 jStatus = nativeToJavaStatus(status);
829 return jStatus;
830 }
831
832 for (size_t i = 0; i < activeMicrophones.size(); i++) {
833 jobject jMicrophoneInfo;
834 jStatus = convertMicrophoneInfoFromNative(env, &jMicrophoneInfo, &activeMicrophones[i]);
835 if (jStatus != AUDIO_JAVA_SUCCESS) {
836 return jStatus;
837 }
838 env->CallBooleanMethod(jActiveMicrophones, gArrayListMethods.add, jMicrophoneInfo);
839 env->DeleteLocalRef(jMicrophoneInfo);
840 }
841 return jStatus;
842}
843
844// ----------------------------------------------------------------------------
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800845// ----------------------------------------------------------------------------
Daniel Micay76f6a862015-09-19 17:31:01 -0400846static const JNINativeMethod gMethods[] = {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800847 // name, signature, funcPtr
Eric Laurent505e5c82012-03-29 15:19:36 -0700848 {"native_start", "(II)I", (void *)android_media_AudioRecord_start},
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800849 {"native_stop", "()V", (void *)android_media_AudioRecord_stop},
Paul McLean9b09e532016-01-26 14:43:35 -0700850 {"native_setup", "(Ljava/lang/Object;Ljava/lang/Object;[IIIII[ILjava/lang/String;J)I",
851 (void *)android_media_AudioRecord_setup},
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800852 {"native_finalize", "()V", (void *)android_media_AudioRecord_finalize},
853 {"native_release", "()V", (void *)android_media_AudioRecord_release},
Glenn Kasten18db49a2012-03-12 16:29:55 -0700854 {"native_read_in_byte_array",
Andy Hung0e9e2f32015-04-13 23:47:18 -0700855 "([BIIZ)I",
856 (void *)android_media_AudioRecord_readInArray<jbyteArray>},
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800857 {"native_read_in_short_array",
Andy Hung0e9e2f32015-04-13 23:47:18 -0700858 "([SIIZ)I",
859 (void *)android_media_AudioRecord_readInArray<jshortArray>},
Andy Hung58b0f3f2015-03-27 17:59:45 -0700860 {"native_read_in_float_array",
Andy Hung0e9e2f32015-04-13 23:47:18 -0700861 "([FIIZ)I",
862 (void *)android_media_AudioRecord_readInArray<jfloatArray>},
863 {"native_read_in_direct_buffer","(Ljava/lang/Object;IZ)I",
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800864 (void *)android_media_AudioRecord_readInDirectBuffer},
Andy Hunge90a0a82015-05-19 15:44:31 -0700865 {"native_get_buffer_size_in_frames",
866 "()I", (void *)android_media_AudioRecord_get_buffer_size_in_frames},
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800867 {"native_set_marker_pos","(I)I", (void *)android_media_AudioRecord_set_marker_pos},
868 {"native_get_marker_pos","()I", (void *)android_media_AudioRecord_get_marker_pos},
869 {"native_set_pos_update_period",
870 "(I)I", (void *)android_media_AudioRecord_set_pos_update_period},
871 {"native_get_pos_update_period",
872 "()I", (void *)android_media_AudioRecord_get_pos_update_period},
873 {"native_get_min_buff_size",
874 "(III)I", (void *)android_media_AudioRecord_get_min_buff_size},
Ray Essick510225b2018-01-24 14:27:16 -0800875 {"native_getMetrics", "()Landroid/os/PersistableBundle;",
876 (void *)android_media_AudioRecord_native_getMetrics},
Paul McLean2d6de4c2015-04-17 13:13:28 -0600877 {"native_setInputDevice", "(I)Z", (void *)android_media_AudioRecord_setInputDevice},
Eric Laurent4bcdba82015-05-01 11:37:49 -0700878 {"native_getRoutedDeviceId", "()I", (void *)android_media_AudioRecord_getRoutedDeviceId},
879 {"native_enableDeviceCallback", "()V", (void *)android_media_AudioRecord_enableDeviceCallback},
880 {"native_disableDeviceCallback", "()V",
881 (void *)android_media_AudioRecord_disableDeviceCallback},
Andy Hung0ad99c02016-01-15 17:53:47 -0800882 {"native_get_timestamp", "(Landroid/media/AudioTimestamp;I)I",
883 (void *)android_media_AudioRecord_get_timestamp},
jiabin23675f62018-01-17 18:05:25 -0800884 {"native_get_active_microphones", "(Ljava/util/ArrayList;)I",
885 (void *)android_media_AudioRecord_get_active_microphones},
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800886};
887
888// field names found in android/media/AudioRecord.java
889#define JAVA_POSTEVENT_CALLBACK_NAME "postEventFromNative"
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800890#define JAVA_NATIVERECORDERINJAVAOBJ_FIELD_NAME "mNativeRecorderInJavaObj"
891#define JAVA_NATIVECALLBACKINFO_FIELD_NAME "mNativeCallbackCookie"
Eric Laurent4bcdba82015-05-01 11:37:49 -0700892#define JAVA_NATIVEDEVICECALLBACK_FIELD_NAME "mNativeDeviceCallback"
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800893
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800894// ----------------------------------------------------------------------------
895int register_android_media_AudioRecord(JNIEnv *env)
896{
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800897 javaAudioRecordFields.postNativeEventInJava = NULL;
898 javaAudioRecordFields.nativeRecorderInJavaObj = NULL;
899 javaAudioRecordFields.nativeCallbackCookie = NULL;
Eric Laurent4bcdba82015-05-01 11:37:49 -0700900 javaAudioRecordFields.nativeDeviceCallback = NULL;
Glenn Kasten18db49a2012-03-12 16:29:55 -0700901
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800902
903 // Get the AudioRecord class
Andreas Gampeed6b9df2014-11-20 22:02:20 -0800904 jclass audioRecordClass = FindClassOrDie(env, kClassPathName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800905 // Get the postEvent method
Andreas Gampeed6b9df2014-11-20 22:02:20 -0800906 javaAudioRecordFields.postNativeEventInJava = GetStaticMethodIDOrDie(env,
907 audioRecordClass, JAVA_POSTEVENT_CALLBACK_NAME,
908 "(Ljava/lang/Object;IIILjava/lang/Object;)V");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800909
910 // Get the variables
911 // mNativeRecorderInJavaObj
Andreas Gampeed6b9df2014-11-20 22:02:20 -0800912 javaAudioRecordFields.nativeRecorderInJavaObj = GetFieldIDOrDie(env,
913 audioRecordClass, JAVA_NATIVERECORDERINJAVAOBJ_FIELD_NAME, "J");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800914 // mNativeCallbackCookie
Andreas Gampeed6b9df2014-11-20 22:02:20 -0800915 javaAudioRecordFields.nativeCallbackCookie = GetFieldIDOrDie(env,
916 audioRecordClass, JAVA_NATIVECALLBACKINFO_FIELD_NAME, "J");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800917
Eric Laurent4bcdba82015-05-01 11:37:49 -0700918 javaAudioRecordFields.nativeDeviceCallback = GetFieldIDOrDie(env,
919 audioRecordClass, JAVA_NATIVEDEVICECALLBACK_FIELD_NAME, "J");
920
Jean-Michel Trivi701d6ff2014-07-16 07:51:22 -0700921 // Get the AudioAttributes class and fields
Andreas Gampeed6b9df2014-11-20 22:02:20 -0800922 jclass audioAttrClass = FindClassOrDie(env, kAudioAttributesClassPathName);
923 javaAudioAttrFields.fieldRecSource = GetFieldIDOrDie(env, audioAttrClass, "mSource", "I");
924 javaAudioAttrFields.fieldFlags = GetFieldIDOrDie(env, audioAttrClass, "mFlags", "I");
925 javaAudioAttrFields.fieldFormattedTags = GetFieldIDOrDie(env,
926 audioAttrClass, "mFormattedTags", "Ljava/lang/String;");
Jean-Michel Trivi701d6ff2014-07-16 07:51:22 -0700927
Andy Hung0ad99c02016-01-15 17:53:47 -0800928 // Get the RecordTimestamp class and fields
929 jclass audioTimestampClass = FindClassOrDie(env, "android/media/AudioTimestamp");
930 javaAudioTimestampFields.fieldFramePosition =
931 GetFieldIDOrDie(env, audioTimestampClass, "framePosition", "J");
932 javaAudioTimestampFields.fieldNanoTime =
933 GetFieldIDOrDie(env, audioTimestampClass, "nanoTime", "J");
934
jiabin23675f62018-01-17 18:05:25 -0800935 jclass arrayListClass = FindClassOrDie(env, "java/util/ArrayList");
936 gArrayListClass = MakeGlobalRefOrDie(env, arrayListClass);
937 gArrayListMethods.add = GetMethodIDOrDie(env, arrayListClass, "add", "(Ljava/lang/Object;)Z");
938
Andreas Gampeed6b9df2014-11-20 22:02:20 -0800939 return RegisterMethodsOrDie(env, kClassPathName, gMethods, NELEM(gMethods));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800940}
941
942// ----------------------------------------------------------------------------