The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1 | /* |
| 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 PETIT | 95ece35 | 2014-02-13 11:02:27 +0000 | [diff] [blame] | 21 | #include <inttypes.h> |
Glenn Kasten | c81d31c | 2012-03-13 14:46:23 -0700 | [diff] [blame] | 22 | #include <jni.h> |
| 23 | #include <JNIHelp.h> |
Andreas Gampe | ed6b9df | 2014-11-20 22:02:20 -0800 | [diff] [blame] | 24 | #include "core_jni_helpers.h" |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 25 | |
Glenn Kasten | c81d31c | 2012-03-13 14:46:23 -0700 | [diff] [blame] | 26 | #include <utils/Log.h> |
| 27 | #include <media/AudioRecord.h> |
Dima Zavin | 24fc2fb | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 28 | |
Glenn Kasten | fe834d3 | 2014-01-08 14:49:08 -0800 | [diff] [blame] | 29 | #include "android_media_AudioFormat.h" |
Eric Laurent | bc11a69 | 2014-05-16 12:19:25 -0700 | [diff] [blame] | 30 | #include "android_media_AudioErrors.h" |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 31 | |
| 32 | // ---------------------------------------------------------------------------- |
| 33 | |
| 34 | using namespace android; |
| 35 | |
| 36 | // ---------------------------------------------------------------------------- |
| 37 | static const char* const kClassPathName = "android/media/AudioRecord"; |
Jean-Michel Trivi | 701d6ff | 2014-07-16 07:51:22 -0700 | [diff] [blame] | 38 | static const char* const kAudioAttributesClassPathName = "android/media/AudioAttributes"; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 39 | |
Jean-Michel Trivi | 701d6ff | 2014-07-16 07:51:22 -0700 | [diff] [blame] | 40 | struct audio_record_fields_t { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 41 | // these fields provide access from C++ to the... |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 42 | jmethodID postNativeEventInJava; //... event post callback method |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 43 | jfieldID nativeRecorderInJavaObj; // provides access to the C++ AudioRecord object |
| 44 | jfieldID nativeCallbackCookie; // provides access to the AudioRecord callback data |
| 45 | }; |
Jean-Michel Trivi | 701d6ff | 2014-07-16 07:51:22 -0700 | [diff] [blame] | 46 | struct audio_attributes_fields_t { |
| 47 | jfieldID fieldRecSource; // AudioAttributes.mSource |
Eric Laurent | bdad1af | 2014-09-19 17:43:29 -0700 | [diff] [blame] | 48 | jfieldID fieldFlags; // AudioAttributes.mFlags |
Jean-Michel Trivi | 701d6ff | 2014-07-16 07:51:22 -0700 | [diff] [blame] | 49 | jfieldID fieldFormattedTags;// AudioAttributes.mFormattedTags |
| 50 | }; |
| 51 | static audio_attributes_fields_t javaAudioAttrFields; |
| 52 | static audio_record_fields_t javaAudioRecordFields; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 53 | |
| 54 | struct audiorecord_callback_cookie { |
| 55 | jclass audioRecord_class; |
| 56 | jobject audioRecord_ref; |
Eric Laurent | 532bc1c | 2012-04-20 12:45:03 -0700 | [diff] [blame] | 57 | bool busy; |
| 58 | Condition cond; |
| 59 | }; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 60 | |
Eric Laurent | 532bc1c | 2012-04-20 12:45:03 -0700 | [diff] [blame] | 61 | static Mutex sLock; |
| 62 | static SortedVector <audiorecord_callback_cookie *> sAudioRecordCallBackCookies; |
Dave Sparks | e6335c9 | 2010-03-13 17:08:22 -0800 | [diff] [blame] | 63 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 64 | // ---------------------------------------------------------------------------- |
| 65 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 66 | #define AUDIORECORD_ERROR_SETUP_ZEROFRAMECOUNT -16 |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 67 | #define AUDIORECORD_ERROR_SETUP_INVALIDCHANNELMASK -17 |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 68 | #define AUDIORECORD_ERROR_SETUP_INVALIDFORMAT -18 |
Eric Laurent | 4bc035a | 2009-05-22 09:18:15 -0700 | [diff] [blame] | 69 | #define AUDIORECORD_ERROR_SETUP_INVALIDSOURCE -19 |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 70 | #define AUDIORECORD_ERROR_SETUP_NATIVEINITFAILED -20 |
| 71 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 72 | // ---------------------------------------------------------------------------- |
| 73 | static void recorderCallback(int event, void* user, void *info) { |
Eric Laurent | 532bc1c | 2012-04-20 12:45:03 -0700 | [diff] [blame] | 74 | |
| 75 | audiorecord_callback_cookie *callbackInfo = (audiorecord_callback_cookie *)user; |
| 76 | { |
| 77 | Mutex::Autolock l(sLock); |
| 78 | if (sAudioRecordCallBackCookies.indexOf(callbackInfo) < 0) { |
| 79 | return; |
| 80 | } |
| 81 | callbackInfo->busy = true; |
| 82 | } |
Glenn Kasten | 18db49a | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 83 | |
Glenn Kasten | 5b1576c | 2013-07-18 16:58:19 -0700 | [diff] [blame] | 84 | switch (event) { |
| 85 | case AudioRecord::EVENT_MARKER: { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 86 | JNIEnv *env = AndroidRuntime::getJNIEnv(); |
Glenn Kasten | a667ff3 | 2013-07-22 07:36:34 -0700 | [diff] [blame] | 87 | if (user != NULL && env != NULL) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 88 | env->CallStaticVoidMethod( |
Glenn Kasten | 18db49a | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 89 | callbackInfo->audioRecord_class, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 90 | javaAudioRecordFields.postNativeEventInJava, |
| 91 | callbackInfo->audioRecord_ref, event, 0,0, NULL); |
| 92 | if (env->ExceptionCheck()) { |
| 93 | env->ExceptionDescribe(); |
| 94 | env->ExceptionClear(); |
| 95 | } |
| 96 | } |
Glenn Kasten | 5b1576c | 2013-07-18 16:58:19 -0700 | [diff] [blame] | 97 | } break; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 98 | |
Glenn Kasten | 5b1576c | 2013-07-18 16:58:19 -0700 | [diff] [blame] | 99 | case AudioRecord::EVENT_NEW_POS: { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 100 | JNIEnv *env = AndroidRuntime::getJNIEnv(); |
Glenn Kasten | a667ff3 | 2013-07-22 07:36:34 -0700 | [diff] [blame] | 101 | if (user != NULL && env != NULL) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 102 | env->CallStaticVoidMethod( |
Glenn Kasten | 18db49a | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 103 | callbackInfo->audioRecord_class, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 104 | javaAudioRecordFields.postNativeEventInJava, |
| 105 | callbackInfo->audioRecord_ref, event, 0,0, NULL); |
| 106 | if (env->ExceptionCheck()) { |
| 107 | env->ExceptionDescribe(); |
| 108 | env->ExceptionClear(); |
| 109 | } |
| 110 | } |
Glenn Kasten | 5b1576c | 2013-07-18 16:58:19 -0700 | [diff] [blame] | 111 | } break; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 112 | } |
Glenn Kasten | 5b1576c | 2013-07-18 16:58:19 -0700 | [diff] [blame] | 113 | |
Eric Laurent | 532bc1c | 2012-04-20 12:45:03 -0700 | [diff] [blame] | 114 | { |
| 115 | Mutex::Autolock l(sLock); |
| 116 | callbackInfo->busy = false; |
| 117 | callbackInfo->cond.broadcast(); |
| 118 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 119 | } |
| 120 | |
Eric Laurent | 532bc1c | 2012-04-20 12:45:03 -0700 | [diff] [blame] | 121 | // ---------------------------------------------------------------------------- |
| 122 | static sp<AudioRecord> getAudioRecord(JNIEnv* env, jobject thiz) |
| 123 | { |
| 124 | Mutex::Autolock l(sLock); |
| 125 | AudioRecord* const ar = |
Ashok Bhat | 075e9a1 | 2014-01-06 13:45:09 +0000 | [diff] [blame] | 126 | (AudioRecord*)env->GetLongField(thiz, javaAudioRecordFields.nativeRecorderInJavaObj); |
Eric Laurent | 532bc1c | 2012-04-20 12:45:03 -0700 | [diff] [blame] | 127 | return sp<AudioRecord>(ar); |
| 128 | } |
| 129 | |
| 130 | static sp<AudioRecord> setAudioRecord(JNIEnv* env, jobject thiz, const sp<AudioRecord>& ar) |
| 131 | { |
| 132 | Mutex::Autolock l(sLock); |
| 133 | sp<AudioRecord> old = |
Ashok Bhat | 075e9a1 | 2014-01-06 13:45:09 +0000 | [diff] [blame] | 134 | (AudioRecord*)env->GetLongField(thiz, javaAudioRecordFields.nativeRecorderInJavaObj); |
Eric Laurent | 532bc1c | 2012-04-20 12:45:03 -0700 | [diff] [blame] | 135 | if (ar.get()) { |
Mathias Agopian | b1d90c8 | 2013-03-06 17:45:42 -0800 | [diff] [blame] | 136 | ar->incStrong((void*)setAudioRecord); |
Eric Laurent | 532bc1c | 2012-04-20 12:45:03 -0700 | [diff] [blame] | 137 | } |
| 138 | if (old != 0) { |
Mathias Agopian | b1d90c8 | 2013-03-06 17:45:42 -0800 | [diff] [blame] | 139 | old->decStrong((void*)setAudioRecord); |
Eric Laurent | 532bc1c | 2012-04-20 12:45:03 -0700 | [diff] [blame] | 140 | } |
Ashok Bhat | 075e9a1 | 2014-01-06 13:45:09 +0000 | [diff] [blame] | 141 | env->SetLongField(thiz, javaAudioRecordFields.nativeRecorderInJavaObj, (jlong)ar.get()); |
Eric Laurent | 532bc1c | 2012-04-20 12:45:03 -0700 | [diff] [blame] | 142 | return old; |
| 143 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 144 | |
| 145 | // ---------------------------------------------------------------------------- |
Ashok Bhat | 075e9a1 | 2014-01-06 13:45:09 +0000 | [diff] [blame] | 146 | static jint |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 147 | android_media_AudioRecord_setup(JNIEnv *env, jobject thiz, jobject weak_this, |
Jean-Michel Trivi | 701d6ff | 2014-07-16 07:51:22 -0700 | [diff] [blame] | 148 | jobject jaa, jint sampleRateInHertz, jint channelMask, |
Glenn Kasten | 33c437d | 2013-07-18 17:04:21 -0700 | [diff] [blame] | 149 | // Java channel masks map directly to the native definition |
Eric Laurent | 44ff4cd | 2011-06-18 10:34:05 -0700 | [diff] [blame] | 150 | jint audioFormat, jint buffSizeInBytes, jintArray jSession) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 151 | { |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 152 | //ALOGV(">> Entering android_media_AudioRecord_setup"); |
Glenn Kasten | 33c437d | 2013-07-18 17:04:21 -0700 | [diff] [blame] | 153 | //ALOGV("sampleRate=%d, audioFormat=%d, channel mask=%x, buffSizeInBytes=%d", |
| 154 | // sampleRateInHertz, audioFormat, channelMask, buffSizeInBytes); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 155 | |
Jean-Michel Trivi | 701d6ff | 2014-07-16 07:51:22 -0700 | [diff] [blame] | 156 | if (jaa == 0) { |
| 157 | ALOGE("Error creating AudioRecord: invalid audio attributes"); |
| 158 | return (jint) AUDIO_JAVA_ERROR; |
| 159 | } |
| 160 | |
Glenn Kasten | 33c437d | 2013-07-18 17:04:21 -0700 | [diff] [blame] | 161 | if (!audio_is_input_channel(channelMask)) { |
| 162 | ALOGE("Error creating AudioRecord: channel mask %#x is not valid.", channelMask); |
Ashok Bhat | 075e9a1 | 2014-01-06 13:45:09 +0000 | [diff] [blame] | 163 | return (jint) AUDIORECORD_ERROR_SETUP_INVALIDCHANNELMASK; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 164 | } |
Eric Laurent | dbf5566 | 2014-07-01 18:56:30 -0700 | [diff] [blame] | 165 | uint32_t channelCount = audio_channel_count_from_in_mask(channelMask); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 166 | |
| 167 | // compare the format against the Java constants |
Glenn Kasten | a5a4238 | 2013-12-19 12:34:56 -0800 | [diff] [blame] | 168 | audio_format_t format = audioFormatToNative(audioFormat); |
| 169 | if (format == AUDIO_FORMAT_INVALID) { |
Glenn Kasten | a9838af | 2013-11-22 13:34:35 -0800 | [diff] [blame] | 170 | ALOGE("Error creating AudioRecord: unsupported audio format %d.", audioFormat); |
Ashok Bhat | 075e9a1 | 2014-01-06 13:45:09 +0000 | [diff] [blame] | 171 | return (jint) AUDIORECORD_ERROR_SETUP_INVALIDFORMAT; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 172 | } |
| 173 | |
Glenn Kasten | a5a4238 | 2013-12-19 12:34:56 -0800 | [diff] [blame] | 174 | size_t bytesPerSample = audio_bytes_per_sample(format); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 175 | |
| 176 | if (buffSizeInBytes == 0) { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 177 | ALOGE("Error creating AudioRecord: frameCount is 0."); |
Ashok Bhat | 075e9a1 | 2014-01-06 13:45:09 +0000 | [diff] [blame] | 178 | return (jint) AUDIORECORD_ERROR_SETUP_ZEROFRAMECOUNT; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 179 | } |
Glenn Kasten | 5b8fd44 | 2013-11-14 09:44:14 -0800 | [diff] [blame] | 180 | size_t frameSize = channelCount * bytesPerSample; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 181 | size_t frameCount = buffSizeInBytes / frameSize; |
Glenn Kasten | 18db49a | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 182 | |
Eric Laurent | 532bc1c | 2012-04-20 12:45:03 -0700 | [diff] [blame] | 183 | jclass clazz = env->GetObjectClass(thiz); |
| 184 | if (clazz == NULL) { |
| 185 | ALOGE("Can't find %s when setting up callback.", kClassPathName); |
Ashok Bhat | 075e9a1 | 2014-01-06 13:45:09 +0000 | [diff] [blame] | 186 | return (jint) AUDIORECORD_ERROR_SETUP_NATIVEINITFAILED; |
Eric Laurent | 532bc1c | 2012-04-20 12:45:03 -0700 | [diff] [blame] | 187 | } |
| 188 | |
Eric Laurent | 44ff4cd | 2011-06-18 10:34:05 -0700 | [diff] [blame] | 189 | if (jSession == NULL) { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 190 | ALOGE("Error creating AudioRecord: invalid session ID pointer"); |
Eric Laurent | bc11a69 | 2014-05-16 12:19:25 -0700 | [diff] [blame] | 191 | return (jint) AUDIO_JAVA_ERROR; |
Eric Laurent | 44ff4cd | 2011-06-18 10:34:05 -0700 | [diff] [blame] | 192 | } |
| 193 | |
| 194 | jint* nSession = (jint *) env->GetPrimitiveArrayCritical(jSession, NULL); |
| 195 | if (nSession == NULL) { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 196 | ALOGE("Error creating AudioRecord: Error retrieving session id pointer"); |
Eric Laurent | bc11a69 | 2014-05-16 12:19:25 -0700 | [diff] [blame] | 197 | return (jint) AUDIO_JAVA_ERROR; |
Eric Laurent | 44ff4cd | 2011-06-18 10:34:05 -0700 | [diff] [blame] | 198 | } |
| 199 | int sessionId = nSession[0]; |
| 200 | env->ReleasePrimitiveArrayCritical(jSession, nSession, 0); |
| 201 | nSession = NULL; |
| 202 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 203 | // create an uninitialized AudioRecord object |
Eric Laurent | 532bc1c | 2012-04-20 12:45:03 -0700 | [diff] [blame] | 204 | sp<AudioRecord> lpRecorder = new AudioRecord(); |
Glenn Kasten | 18db49a | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 205 | |
Jean-Michel Trivi | 701d6ff | 2014-07-16 07:51:22 -0700 | [diff] [blame] | 206 | audio_attributes_t *paa = NULL; |
| 207 | // read the AudioAttributes values |
| 208 | paa = (audio_attributes_t *) calloc(1, sizeof(audio_attributes_t)); |
| 209 | const jstring jtags = |
| 210 | (jstring) env->GetObjectField(jaa, javaAudioAttrFields.fieldFormattedTags); |
| 211 | const char* tags = env->GetStringUTFChars(jtags, NULL); |
| 212 | // copying array size -1, char array for tags was calloc'd, no need to NULL-terminate it |
| 213 | strncpy(paa->tags, tags, AUDIO_ATTRIBUTES_TAGS_MAX_SIZE - 1); |
| 214 | env->ReleaseStringUTFChars(jtags, tags); |
| 215 | paa->source = (audio_source_t) env->GetIntField(jaa, javaAudioAttrFields.fieldRecSource); |
Eric Laurent | bdad1af | 2014-09-19 17:43:29 -0700 | [diff] [blame] | 216 | paa->flags = (audio_flags_mask_t)env->GetIntField(jaa, javaAudioAttrFields.fieldFlags); |
| 217 | ALOGV("AudioRecord_setup for source=%d tags=%s flags=%08x", paa->source, paa->tags, paa->flags); |
Jean-Michel Trivi | 701d6ff | 2014-07-16 07:51:22 -0700 | [diff] [blame] | 218 | |
Eric Laurent | bdad1af | 2014-09-19 17:43:29 -0700 | [diff] [blame] | 219 | audio_input_flags_t flags = AUDIO_INPUT_FLAG_NONE; |
| 220 | if (paa->flags & AUDIO_FLAG_HW_HOTWORD) { |
| 221 | flags = AUDIO_INPUT_FLAG_HW_HOTWORD; |
| 222 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 223 | // create the callback information: |
| 224 | // this data will be passed with every AudioRecord callback |
Eric Laurent | 532bc1c | 2012-04-20 12:45:03 -0700 | [diff] [blame] | 225 | audiorecord_callback_cookie *lpCallbackData = new audiorecord_callback_cookie; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 226 | lpCallbackData->audioRecord_class = (jclass)env->NewGlobalRef(clazz); |
| 227 | // we use a weak reference so the AudioRecord object can be garbage collected. |
| 228 | lpCallbackData->audioRecord_ref = env->NewGlobalRef(weak_this); |
Eric Laurent | 532bc1c | 2012-04-20 12:45:03 -0700 | [diff] [blame] | 229 | lpCallbackData->busy = false; |
Glenn Kasten | 18db49a | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 230 | |
Jean-Michel Trivi | 701d6ff | 2014-07-16 07:51:22 -0700 | [diff] [blame] | 231 | const status_t status = lpRecorder->set(paa->source, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 232 | sampleRateInHertz, |
| 233 | format, // word length, PCM |
Glenn Kasten | 33c437d | 2013-07-18 17:04:21 -0700 | [diff] [blame] | 234 | channelMask, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 235 | frameCount, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 236 | recorderCallback,// callback_t |
| 237 | lpCallbackData,// void* user |
| 238 | 0, // notificationFrames, |
Glenn Kasten | 86fad47 | 2012-06-21 16:21:36 -0700 | [diff] [blame] | 239 | true, // threadCanCallJava |
Eric Laurent | bdad1af | 2014-09-19 17:43:29 -0700 | [diff] [blame] | 240 | sessionId, |
| 241 | AudioRecord::TRANSFER_DEFAULT, |
Eric Laurent | 4c3fc59b | 2014-11-26 09:46:44 -0800 | [diff] [blame] | 242 | flags, |
| 243 | paa); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 244 | |
Glenn Kasten | a9838af | 2013-11-22 13:34:35 -0800 | [diff] [blame] | 245 | if (status != NO_ERROR) { |
| 246 | ALOGE("Error creating AudioRecord instance: initialization check failed with status %d.", |
| 247 | status); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 248 | goto native_init_failure; |
| 249 | } |
| 250 | |
Eric Laurent | 44ff4cd | 2011-06-18 10:34:05 -0700 | [diff] [blame] | 251 | nSession = (jint *) env->GetPrimitiveArrayCritical(jSession, NULL); |
| 252 | if (nSession == NULL) { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 253 | ALOGE("Error creating AudioRecord: Error retrieving session id pointer"); |
Eric Laurent | 44ff4cd | 2011-06-18 10:34:05 -0700 | [diff] [blame] | 254 | goto native_init_failure; |
| 255 | } |
Glenn Kasten | b3db213 | 2012-01-19 08:59:58 -0800 | [diff] [blame] | 256 | // read the audio session ID back from AudioRecord in case a new session was created during set() |
Eric Laurent | 44ff4cd | 2011-06-18 10:34:05 -0700 | [diff] [blame] | 257 | nSession[0] = lpRecorder->getSessionId(); |
| 258 | env->ReleasePrimitiveArrayCritical(jSession, nSession, 0); |
| 259 | nSession = NULL; |
| 260 | |
Eric Laurent | 532bc1c | 2012-04-20 12:45:03 -0700 | [diff] [blame] | 261 | { // scope for the lock |
| 262 | Mutex::Autolock l(sLock); |
| 263 | sAudioRecordCallBackCookies.add(lpCallbackData); |
| 264 | } |
Glenn Kasten | 18db49a | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 265 | // save our newly created C++ AudioRecord in the "nativeRecorderInJavaObj" field |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 266 | // of the Java object |
Eric Laurent | 532bc1c | 2012-04-20 12:45:03 -0700 | [diff] [blame] | 267 | setAudioRecord(env, thiz, lpRecorder); |
Glenn Kasten | 18db49a | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 268 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 269 | // save our newly created callback information in the "nativeCallbackCookie" field |
| 270 | // of the Java object (in mNativeCallbackCookie) so we can free the memory in finalize() |
Ashok Bhat | 075e9a1 | 2014-01-06 13:45:09 +0000 | [diff] [blame] | 271 | env->SetLongField(thiz, javaAudioRecordFields.nativeCallbackCookie, (jlong)lpCallbackData); |
Glenn Kasten | 18db49a | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 272 | |
Eric Laurent | bc11a69 | 2014-05-16 12:19:25 -0700 | [diff] [blame] | 273 | return (jint) AUDIO_JAVA_SUCCESS; |
Glenn Kasten | 18db49a | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 274 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 275 | // failure: |
| 276 | native_init_failure: |
Jean-Michel Trivi | 4bac5a3 | 2009-07-17 12:05:31 -0700 | [diff] [blame] | 277 | env->DeleteGlobalRef(lpCallbackData->audioRecord_class); |
| 278 | env->DeleteGlobalRef(lpCallbackData->audioRecord_ref); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 279 | delete lpCallbackData; |
Ashok Bhat | 075e9a1 | 2014-01-06 13:45:09 +0000 | [diff] [blame] | 280 | env->SetLongField(thiz, javaAudioRecordFields.nativeCallbackCookie, 0); |
Glenn Kasten | 18db49a | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 281 | |
Ashok Bhat | 075e9a1 | 2014-01-06 13:45:09 +0000 | [diff] [blame] | 282 | return (jint) AUDIORECORD_ERROR_SETUP_NATIVEINITFAILED; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 283 | } |
| 284 | |
| 285 | |
| 286 | |
| 287 | // ---------------------------------------------------------------------------- |
Ashok Bhat | 075e9a1 | 2014-01-06 13:45:09 +0000 | [diff] [blame] | 288 | static jint |
Eric Laurent | 505e5c8 | 2012-03-29 15:19:36 -0700 | [diff] [blame] | 289 | android_media_AudioRecord_start(JNIEnv *env, jobject thiz, jint event, jint triggerSession) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 290 | { |
Eric Laurent | 532bc1c | 2012-04-20 12:45:03 -0700 | [diff] [blame] | 291 | sp<AudioRecord> lpRecorder = getAudioRecord(env, thiz); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 292 | if (lpRecorder == NULL ) { |
| 293 | jniThrowException(env, "java/lang/IllegalStateException", NULL); |
Eric Laurent | bc11a69 | 2014-05-16 12:19:25 -0700 | [diff] [blame] | 294 | return (jint) AUDIO_JAVA_ERROR; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 295 | } |
Glenn Kasten | 18db49a | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 296 | |
Eric Laurent | bc11a69 | 2014-05-16 12:19:25 -0700 | [diff] [blame] | 297 | return nativeToJavaStatus( |
Eric Laurent | 505e5c8 | 2012-03-29 15:19:36 -0700 | [diff] [blame] | 298 | lpRecorder->start((AudioSystem::sync_event_t)event, triggerSession)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 299 | } |
| 300 | |
| 301 | |
| 302 | // ---------------------------------------------------------------------------- |
| 303 | static void |
| 304 | android_media_AudioRecord_stop(JNIEnv *env, jobject thiz) |
| 305 | { |
Eric Laurent | 532bc1c | 2012-04-20 12:45:03 -0700 | [diff] [blame] | 306 | sp<AudioRecord> lpRecorder = getAudioRecord(env, thiz); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 307 | if (lpRecorder == NULL ) { |
| 308 | jniThrowException(env, "java/lang/IllegalStateException", NULL); |
| 309 | return; |
| 310 | } |
| 311 | |
| 312 | lpRecorder->stop(); |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 313 | //ALOGV("Called lpRecorder->stop()"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 314 | } |
| 315 | |
| 316 | |
| 317 | // ---------------------------------------------------------------------------- |
Dave Sparks | e6335c9 | 2010-03-13 17:08:22 -0800 | [diff] [blame] | 318 | |
Eric Laurent | 532bc1c | 2012-04-20 12:45:03 -0700 | [diff] [blame] | 319 | #define CALLBACK_COND_WAIT_TIMEOUT_MS 1000 |
| 320 | static void android_media_AudioRecord_release(JNIEnv *env, jobject thiz) { |
| 321 | sp<AudioRecord> lpRecorder = setAudioRecord(env, thiz, 0); |
| 322 | if (lpRecorder == NULL) { |
| 323 | return; |
| 324 | } |
Glenn Kasten | 2fbf25b | 2014-03-28 15:41:58 -0700 | [diff] [blame] | 325 | ALOGV("About to delete lpRecorder: %p", lpRecorder.get()); |
Eric Laurent | 532bc1c | 2012-04-20 12:45:03 -0700 | [diff] [blame] | 326 | lpRecorder->stop(); |
| 327 | |
Ashok Bhat | 075e9a1 | 2014-01-06 13:45:09 +0000 | [diff] [blame] | 328 | audiorecord_callback_cookie *lpCookie = (audiorecord_callback_cookie *)env->GetLongField( |
Dave Sparks | e6335c9 | 2010-03-13 17:08:22 -0800 | [diff] [blame] | 329 | thiz, javaAudioRecordFields.nativeCallbackCookie); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 330 | |
Dave Sparks | e6335c9 | 2010-03-13 17:08:22 -0800 | [diff] [blame] | 331 | // reset the native resources in the Java object so any attempt to access |
| 332 | // them after a call to release fails. |
Ashok Bhat | 075e9a1 | 2014-01-06 13:45:09 +0000 | [diff] [blame] | 333 | env->SetLongField(thiz, javaAudioRecordFields.nativeCallbackCookie, 0); |
Dave Sparks | e6335c9 | 2010-03-13 17:08:22 -0800 | [diff] [blame] | 334 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 335 | // delete the callback information |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 336 | if (lpCookie) { |
Eric Laurent | 532bc1c | 2012-04-20 12:45:03 -0700 | [diff] [blame] | 337 | Mutex::Autolock l(sLock); |
Glenn Kasten | 2fbf25b | 2014-03-28 15:41:58 -0700 | [diff] [blame] | 338 | ALOGV("deleting lpCookie: %p", lpCookie); |
Eric Laurent | 532bc1c | 2012-04-20 12:45:03 -0700 | [diff] [blame] | 339 | while (lpCookie->busy) { |
| 340 | if (lpCookie->cond.waitRelative(sLock, |
| 341 | milliseconds(CALLBACK_COND_WAIT_TIMEOUT_MS)) != |
| 342 | NO_ERROR) { |
| 343 | break; |
| 344 | } |
| 345 | } |
| 346 | sAudioRecordCallBackCookies.remove(lpCookie); |
Jean-Michel Trivi | 4bac5a3 | 2009-07-17 12:05:31 -0700 | [diff] [blame] | 347 | env->DeleteGlobalRef(lpCookie->audioRecord_class); |
| 348 | env->DeleteGlobalRef(lpCookie->audioRecord_ref); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 349 | delete lpCookie; |
| 350 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 351 | } |
| 352 | |
| 353 | |
| 354 | // ---------------------------------------------------------------------------- |
Dave Sparks | e6335c9 | 2010-03-13 17:08:22 -0800 | [diff] [blame] | 355 | static void android_media_AudioRecord_finalize(JNIEnv *env, jobject thiz) { |
| 356 | android_media_AudioRecord_release(env, thiz); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 357 | } |
| 358 | |
Andy Hung | 0e9e2f3 | 2015-04-13 23:47:18 -0700 | [diff] [blame] | 359 | // overloaded JNI array helper functions |
| 360 | static inline |
| 361 | jbyte *envGetArrayElements(JNIEnv *env, jbyteArray array, jboolean *isCopy) { |
| 362 | return env->GetByteArrayElements(array, isCopy); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 363 | } |
| 364 | |
Andy Hung | 0e9e2f3 | 2015-04-13 23:47:18 -0700 | [diff] [blame] | 365 | static inline |
| 366 | void envReleaseArrayElements(JNIEnv *env, jbyteArray array, jbyte *elems, jint mode) { |
| 367 | env->ReleaseByteArrayElements(array, elems, mode); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 368 | } |
| 369 | |
Andy Hung | 0e9e2f3 | 2015-04-13 23:47:18 -0700 | [diff] [blame] | 370 | static inline |
| 371 | jshort *envGetArrayElements(JNIEnv *env, jshortArray array, jboolean *isCopy) { |
| 372 | return env->GetShortArrayElements(array, isCopy); |
| 373 | } |
| 374 | |
| 375 | static inline |
| 376 | void envReleaseArrayElements(JNIEnv *env, jshortArray array, jshort *elems, jint mode) { |
| 377 | env->ReleaseShortArrayElements(array, elems, mode); |
| 378 | } |
| 379 | |
| 380 | static inline |
| 381 | jfloat *envGetArrayElements(JNIEnv *env, jfloatArray array, jboolean *isCopy) { |
| 382 | return env->GetFloatArrayElements(array, isCopy); |
| 383 | } |
| 384 | |
| 385 | static inline |
| 386 | void envReleaseArrayElements(JNIEnv *env, jfloatArray array, jfloat *elems, jint mode) { |
| 387 | env->ReleaseFloatArrayElements(array, elems, mode); |
| 388 | } |
| 389 | |
| 390 | static inline |
| 391 | jint interpretReadSizeError(ssize_t readSize) { |
| 392 | ALOGE_IF(readSize != WOULD_BLOCK, "Error %zd during AudioRecord native read", readSize); |
| 393 | switch (readSize) { |
| 394 | case WOULD_BLOCK: |
| 395 | return (jint)0; |
| 396 | case BAD_VALUE: |
| 397 | return (jint)AUDIO_JAVA_BAD_VALUE; |
| 398 | default: |
| 399 | // may be possible for other errors such as |
| 400 | // NO_INIT to happen if restoreRecord_l fails. |
| 401 | case INVALID_OPERATION: |
| 402 | return (jint)AUDIO_JAVA_INVALID_OPERATION; |
| 403 | } |
| 404 | } |
| 405 | |
| 406 | template <typename T> |
| 407 | static jint android_media_AudioRecord_readInArray(JNIEnv *env, jobject thiz, |
| 408 | T javaAudioData, |
| 409 | jint offsetInSamples, jint sizeInSamples, |
| 410 | jboolean isReadBlocking) { |
Andy Hung | 58b0f3f | 2015-03-27 17:59:45 -0700 | [diff] [blame] | 411 | // get the audio recorder from which we'll read new audio samples |
| 412 | sp<AudioRecord> lpRecorder = getAudioRecord(env, thiz); |
| 413 | if (lpRecorder == NULL) { |
| 414 | ALOGE("Unable to retrieve AudioRecord object"); |
| 415 | return (jint)AUDIO_JAVA_INVALID_OPERATION; |
| 416 | } |
| 417 | |
| 418 | if (javaAudioData == NULL) { |
Andy Hung | 0e9e2f3 | 2015-04-13 23:47:18 -0700 | [diff] [blame] | 419 | ALOGE("Invalid Java array to store recorded audio"); |
| 420 | return (jint)AUDIO_JAVA_BAD_VALUE; |
| 421 | } |
Andy Hung | 58b0f3f | 2015-03-27 17:59:45 -0700 | [diff] [blame] | 422 | |
Andy Hung | 58b0f3f | 2015-03-27 17:59:45 -0700 | [diff] [blame] | 423 | // NOTE: We may use GetPrimitiveArrayCritical() when the JNI implementation changes in such |
| 424 | // a way that it becomes much more efficient. When doing so, we will have to prevent the |
| 425 | // AudioSystem callback to be called while in critical section (in case of media server |
| 426 | // process crash for instance) |
Andy Hung | 0e9e2f3 | 2015-04-13 23:47:18 -0700 | [diff] [blame] | 427 | |
| 428 | // get the pointer to where we'll record the audio |
| 429 | auto *recordBuff = envGetArrayElements(env, javaAudioData, NULL); |
Andy Hung | 58b0f3f | 2015-03-27 17:59:45 -0700 | [diff] [blame] | 430 | if (recordBuff == NULL) { |
| 431 | ALOGE("Error retrieving destination for recorded audio data"); |
| 432 | return (jint)AUDIO_JAVA_BAD_VALUE; |
| 433 | } |
| 434 | |
| 435 | // read the new audio data from the native AudioRecord object |
Andy Hung | 0e9e2f3 | 2015-04-13 23:47:18 -0700 | [diff] [blame] | 436 | const size_t sizeInBytes = sizeInSamples * sizeof(*recordBuff); |
| 437 | ssize_t readSize = lpRecorder->read( |
| 438 | recordBuff + offsetInSamples, sizeInBytes, isReadBlocking == JNI_TRUE /* blocking */); |
Andy Hung | 58b0f3f | 2015-03-27 17:59:45 -0700 | [diff] [blame] | 439 | |
Andy Hung | 0e9e2f3 | 2015-04-13 23:47:18 -0700 | [diff] [blame] | 440 | envReleaseArrayElements(env, javaAudioData, recordBuff, 0); |
Andy Hung | 58b0f3f | 2015-03-27 17:59:45 -0700 | [diff] [blame] | 441 | |
| 442 | if (readSize < 0) { |
Andy Hung | 0e9e2f3 | 2015-04-13 23:47:18 -0700 | [diff] [blame] | 443 | return interpretReadSizeError(readSize); |
Andy Hung | 58b0f3f | 2015-03-27 17:59:45 -0700 | [diff] [blame] | 444 | } |
Andy Hung | 0e9e2f3 | 2015-04-13 23:47:18 -0700 | [diff] [blame] | 445 | return (jint)(readSize / sizeof(*recordBuff)); |
Andy Hung | 58b0f3f | 2015-03-27 17:59:45 -0700 | [diff] [blame] | 446 | } |
| 447 | |
| 448 | // ---------------------------------------------------------------------------- |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 449 | static jint android_media_AudioRecord_readInDirectBuffer(JNIEnv *env, jobject thiz, |
Andy Hung | 0e9e2f3 | 2015-04-13 23:47:18 -0700 | [diff] [blame] | 450 | jobject jBuffer, jint sizeInBytes, |
| 451 | jboolean isReadBlocking) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 452 | // get the audio recorder from which we'll read new audio samples |
Eric Laurent | 532bc1c | 2012-04-20 12:45:03 -0700 | [diff] [blame] | 453 | sp<AudioRecord> lpRecorder = getAudioRecord(env, thiz); |
Glenn Kasten | 18db49a | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 454 | if (lpRecorder==NULL) |
Andy Hung | 0e9e2f3 | 2015-04-13 23:47:18 -0700 | [diff] [blame] | 455 | return (jint)AUDIO_JAVA_INVALID_OPERATION; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 456 | |
| 457 | // direct buffer and direct access supported? |
| 458 | long capacity = env->GetDirectBufferCapacity(jBuffer); |
Glenn Kasten | 18db49a | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 459 | if (capacity == -1) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 460 | // buffer direct access is not supported |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 461 | ALOGE("Buffer direct access is not supported, can't record"); |
Andy Hung | 0e9e2f3 | 2015-04-13 23:47:18 -0700 | [diff] [blame] | 462 | return (jint)AUDIO_JAVA_BAD_VALUE; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 463 | } |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 464 | //ALOGV("capacity = %ld", capacity); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 465 | jbyte* nativeFromJavaBuf = (jbyte*) env->GetDirectBufferAddress(jBuffer); |
Glenn Kasten | 18db49a | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 466 | if (nativeFromJavaBuf==NULL) { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 467 | ALOGE("Buffer direct access is not supported, can't record"); |
Andy Hung | 0e9e2f3 | 2015-04-13 23:47:18 -0700 | [diff] [blame] | 468 | return (jint)AUDIO_JAVA_BAD_VALUE; |
Glenn Kasten | 18db49a | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 469 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 470 | |
| 471 | // read new data from the recorder |
Eric Laurent | 357263d | 2013-09-09 10:31:59 -0700 | [diff] [blame] | 472 | ssize_t readSize = lpRecorder->read(nativeFromJavaBuf, |
Andy Hung | 0e9e2f3 | 2015-04-13 23:47:18 -0700 | [diff] [blame] | 473 | capacity < sizeInBytes ? capacity : sizeInBytes, |
| 474 | isReadBlocking == JNI_TRUE /* blocking */); |
Eric Laurent | 357263d | 2013-09-09 10:31:59 -0700 | [diff] [blame] | 475 | if (readSize < 0) { |
Andy Hung | 0e9e2f3 | 2015-04-13 23:47:18 -0700 | [diff] [blame] | 476 | return interpretReadSizeError(readSize); |
Eric Laurent | 357263d | 2013-09-09 10:31:59 -0700 | [diff] [blame] | 477 | } |
| 478 | return (jint)readSize; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 479 | } |
| 480 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 481 | // ---------------------------------------------------------------------------- |
Andy Hung | 864ae67 | 2015-04-14 11:06:48 -0700 | [diff] [blame] | 482 | static jint android_media_AudioRecord_get_native_frame_count(JNIEnv *env, jobject thiz) { |
| 483 | sp<AudioRecord> lpRecorder = getAudioRecord(env, thiz); |
| 484 | if (lpRecorder == NULL) { |
| 485 | jniThrowException(env, "java/lang/IllegalStateException", |
| 486 | "Unable to retrieve AudioRecord pointer for getNativeFrameCount()"); |
| 487 | return (jint)AUDIO_JAVA_ERROR; |
| 488 | } |
| 489 | return lpRecorder->frameCount(); |
| 490 | } |
| 491 | |
| 492 | // ---------------------------------------------------------------------------- |
Glenn Kasten | 18db49a | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 493 | static jint android_media_AudioRecord_set_marker_pos(JNIEnv *env, jobject thiz, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 494 | jint markerPos) { |
Glenn Kasten | 18db49a | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 495 | |
Eric Laurent | 532bc1c | 2012-04-20 12:45:03 -0700 | [diff] [blame] | 496 | sp<AudioRecord> lpRecorder = getAudioRecord(env, thiz); |
| 497 | if (lpRecorder == NULL) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 498 | jniThrowException(env, "java/lang/IllegalStateException", |
| 499 | "Unable to retrieve AudioRecord pointer for setMarkerPosition()"); |
Eric Laurent | bc11a69 | 2014-05-16 12:19:25 -0700 | [diff] [blame] | 500 | return (jint)AUDIO_JAVA_ERROR; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 501 | } |
Eric Laurent | bc11a69 | 2014-05-16 12:19:25 -0700 | [diff] [blame] | 502 | return nativeToJavaStatus( lpRecorder->setMarkerPosition(markerPos) ); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 503 | } |
| 504 | |
| 505 | |
| 506 | // ---------------------------------------------------------------------------- |
| 507 | static jint android_media_AudioRecord_get_marker_pos(JNIEnv *env, jobject thiz) { |
Glenn Kasten | 18db49a | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 508 | |
Eric Laurent | 532bc1c | 2012-04-20 12:45:03 -0700 | [diff] [blame] | 509 | sp<AudioRecord> lpRecorder = getAudioRecord(env, thiz); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 510 | uint32_t markerPos = 0; |
Glenn Kasten | 18db49a | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 511 | |
Eric Laurent | 532bc1c | 2012-04-20 12:45:03 -0700 | [diff] [blame] | 512 | if (lpRecorder == NULL) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 513 | jniThrowException(env, "java/lang/IllegalStateException", |
| 514 | "Unable to retrieve AudioRecord pointer for getMarkerPosition()"); |
Eric Laurent | bc11a69 | 2014-05-16 12:19:25 -0700 | [diff] [blame] | 515 | return (jint)AUDIO_JAVA_ERROR; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 516 | } |
Eric Laurent | 532bc1c | 2012-04-20 12:45:03 -0700 | [diff] [blame] | 517 | lpRecorder->getMarkerPosition(&markerPos); |
| 518 | return (jint)markerPos; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 519 | } |
| 520 | |
| 521 | |
| 522 | // ---------------------------------------------------------------------------- |
| 523 | static jint android_media_AudioRecord_set_pos_update_period(JNIEnv *env, jobject thiz, |
| 524 | jint period) { |
Glenn Kasten | 18db49a | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 525 | |
Eric Laurent | 532bc1c | 2012-04-20 12:45:03 -0700 | [diff] [blame] | 526 | sp<AudioRecord> lpRecorder = getAudioRecord(env, thiz); |
Glenn Kasten | 18db49a | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 527 | |
Eric Laurent | 532bc1c | 2012-04-20 12:45:03 -0700 | [diff] [blame] | 528 | if (lpRecorder == NULL) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 529 | jniThrowException(env, "java/lang/IllegalStateException", |
| 530 | "Unable to retrieve AudioRecord pointer for setPositionUpdatePeriod()"); |
Eric Laurent | bc11a69 | 2014-05-16 12:19:25 -0700 | [diff] [blame] | 531 | return (jint)AUDIO_JAVA_ERROR; |
Glenn Kasten | 18db49a | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 532 | } |
Eric Laurent | bc11a69 | 2014-05-16 12:19:25 -0700 | [diff] [blame] | 533 | return nativeToJavaStatus( lpRecorder->setPositionUpdatePeriod(period) ); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 534 | } |
| 535 | |
| 536 | |
| 537 | // ---------------------------------------------------------------------------- |
| 538 | static jint android_media_AudioRecord_get_pos_update_period(JNIEnv *env, jobject thiz) { |
Glenn Kasten | 18db49a | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 539 | |
Eric Laurent | 532bc1c | 2012-04-20 12:45:03 -0700 | [diff] [blame] | 540 | sp<AudioRecord> lpRecorder = getAudioRecord(env, thiz); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 541 | uint32_t period = 0; |
Glenn Kasten | 18db49a | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 542 | |
Eric Laurent | 532bc1c | 2012-04-20 12:45:03 -0700 | [diff] [blame] | 543 | if (lpRecorder == NULL) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 544 | jniThrowException(env, "java/lang/IllegalStateException", |
| 545 | "Unable to retrieve AudioRecord pointer for getPositionUpdatePeriod()"); |
Eric Laurent | bc11a69 | 2014-05-16 12:19:25 -0700 | [diff] [blame] | 546 | return (jint)AUDIO_JAVA_ERROR; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 547 | } |
Eric Laurent | 532bc1c | 2012-04-20 12:45:03 -0700 | [diff] [blame] | 548 | lpRecorder->getPositionUpdatePeriod(&period); |
| 549 | return (jint)period; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 550 | } |
| 551 | |
| 552 | |
| 553 | // ---------------------------------------------------------------------------- |
| 554 | // returns the minimum required size for the successful creation of an AudioRecord instance. |
| 555 | // returns 0 if the parameter combination is not supported. |
| 556 | // return -1 if there was an error querying the buffer size. |
| 557 | static jint android_media_AudioRecord_get_min_buff_size(JNIEnv *env, jobject thiz, |
Glenn Kasten | 5b8fd44 | 2013-11-14 09:44:14 -0800 | [diff] [blame] | 558 | jint sampleRateInHertz, jint channelCount, jint audioFormat) { |
Chia-chi Yeh | c330807 | 2010-08-19 17:14:36 +0800 | [diff] [blame] | 559 | |
Eric Laurent | 532bc1c | 2012-04-20 12:45:03 -0700 | [diff] [blame] | 560 | ALOGV(">> android_media_AudioRecord_get_min_buff_size(%d, %d, %d)", |
Glenn Kasten | 5b8fd44 | 2013-11-14 09:44:14 -0800 | [diff] [blame] | 561 | sampleRateInHertz, channelCount, audioFormat); |
Chia-chi Yeh | c330807 | 2010-08-19 17:14:36 +0800 | [diff] [blame] | 562 | |
Glenn Kasten | fd1e3df | 2012-11-13 15:21:06 -0800 | [diff] [blame] | 563 | size_t frameCount = 0; |
Glenn Kasten | a5a4238 | 2013-12-19 12:34:56 -0800 | [diff] [blame] | 564 | audio_format_t format = audioFormatToNative(audioFormat); |
Chia-chi Yeh | c330807 | 2010-08-19 17:14:36 +0800 | [diff] [blame] | 565 | status_t result = AudioRecord::getMinFrameCount(&frameCount, |
| 566 | sampleRateInHertz, |
Glenn Kasten | a5a4238 | 2013-12-19 12:34:56 -0800 | [diff] [blame] | 567 | format, |
Glenn Kasten | 5b8fd44 | 2013-11-14 09:44:14 -0800 | [diff] [blame] | 568 | audio_channel_in_mask_from_count(channelCount)); |
Chia-chi Yeh | c330807 | 2010-08-19 17:14:36 +0800 | [diff] [blame] | 569 | |
| 570 | if (result == BAD_VALUE) { |
| 571 | return 0; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 572 | } |
Chia-chi Yeh | c330807 | 2010-08-19 17:14:36 +0800 | [diff] [blame] | 573 | if (result != NO_ERROR) { |
| 574 | return -1; |
| 575 | } |
Glenn Kasten | 5b8fd44 | 2013-11-14 09:44:14 -0800 | [diff] [blame] | 576 | return frameCount * channelCount * audio_bytes_per_sample(format); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 577 | } |
| 578 | |
| 579 | |
| 580 | // ---------------------------------------------------------------------------- |
| 581 | // ---------------------------------------------------------------------------- |
| 582 | static JNINativeMethod gMethods[] = { |
| 583 | // name, signature, funcPtr |
Eric Laurent | 505e5c8 | 2012-03-29 15:19:36 -0700 | [diff] [blame] | 584 | {"native_start", "(II)I", (void *)android_media_AudioRecord_start}, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 585 | {"native_stop", "()V", (void *)android_media_AudioRecord_stop}, |
Jean-Michel Trivi | 701d6ff | 2014-07-16 07:51:22 -0700 | [diff] [blame] | 586 | {"native_setup", "(Ljava/lang/Object;Ljava/lang/Object;IIII[I)I", |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 587 | (void *)android_media_AudioRecord_setup}, |
| 588 | {"native_finalize", "()V", (void *)android_media_AudioRecord_finalize}, |
| 589 | {"native_release", "()V", (void *)android_media_AudioRecord_release}, |
Glenn Kasten | 18db49a | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 590 | {"native_read_in_byte_array", |
Andy Hung | 0e9e2f3 | 2015-04-13 23:47:18 -0700 | [diff] [blame] | 591 | "([BIIZ)I", |
| 592 | (void *)android_media_AudioRecord_readInArray<jbyteArray>}, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 593 | {"native_read_in_short_array", |
Andy Hung | 0e9e2f3 | 2015-04-13 23:47:18 -0700 | [diff] [blame] | 594 | "([SIIZ)I", |
| 595 | (void *)android_media_AudioRecord_readInArray<jshortArray>}, |
Andy Hung | 58b0f3f | 2015-03-27 17:59:45 -0700 | [diff] [blame] | 596 | {"native_read_in_float_array", |
Andy Hung | 0e9e2f3 | 2015-04-13 23:47:18 -0700 | [diff] [blame] | 597 | "([FIIZ)I", |
| 598 | (void *)android_media_AudioRecord_readInArray<jfloatArray>}, |
| 599 | {"native_read_in_direct_buffer","(Ljava/lang/Object;IZ)I", |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 600 | (void *)android_media_AudioRecord_readInDirectBuffer}, |
Andy Hung | 864ae67 | 2015-04-14 11:06:48 -0700 | [diff] [blame] | 601 | {"native_get_native_frame_count", |
| 602 | "()I", (void *)android_media_AudioRecord_get_native_frame_count}, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 603 | {"native_set_marker_pos","(I)I", (void *)android_media_AudioRecord_set_marker_pos}, |
| 604 | {"native_get_marker_pos","()I", (void *)android_media_AudioRecord_get_marker_pos}, |
| 605 | {"native_set_pos_update_period", |
| 606 | "(I)I", (void *)android_media_AudioRecord_set_pos_update_period}, |
| 607 | {"native_get_pos_update_period", |
| 608 | "()I", (void *)android_media_AudioRecord_get_pos_update_period}, |
| 609 | {"native_get_min_buff_size", |
| 610 | "(III)I", (void *)android_media_AudioRecord_get_min_buff_size}, |
| 611 | }; |
| 612 | |
| 613 | // field names found in android/media/AudioRecord.java |
| 614 | #define JAVA_POSTEVENT_CALLBACK_NAME "postEventFromNative" |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 615 | #define JAVA_NATIVERECORDERINJAVAOBJ_FIELD_NAME "mNativeRecorderInJavaObj" |
| 616 | #define JAVA_NATIVECALLBACKINFO_FIELD_NAME "mNativeCallbackCookie" |
| 617 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 618 | // ---------------------------------------------------------------------------- |
| 619 | int register_android_media_AudioRecord(JNIEnv *env) |
| 620 | { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 621 | javaAudioRecordFields.postNativeEventInJava = NULL; |
| 622 | javaAudioRecordFields.nativeRecorderInJavaObj = NULL; |
| 623 | javaAudioRecordFields.nativeCallbackCookie = NULL; |
Glenn Kasten | 18db49a | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 624 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 625 | |
| 626 | // Get the AudioRecord class |
Andreas Gampe | ed6b9df | 2014-11-20 22:02:20 -0800 | [diff] [blame] | 627 | jclass audioRecordClass = FindClassOrDie(env, kClassPathName); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 628 | // Get the postEvent method |
Andreas Gampe | ed6b9df | 2014-11-20 22:02:20 -0800 | [diff] [blame] | 629 | javaAudioRecordFields.postNativeEventInJava = GetStaticMethodIDOrDie(env, |
| 630 | audioRecordClass, JAVA_POSTEVENT_CALLBACK_NAME, |
| 631 | "(Ljava/lang/Object;IIILjava/lang/Object;)V"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 632 | |
| 633 | // Get the variables |
| 634 | // mNativeRecorderInJavaObj |
Andreas Gampe | ed6b9df | 2014-11-20 22:02:20 -0800 | [diff] [blame] | 635 | javaAudioRecordFields.nativeRecorderInJavaObj = GetFieldIDOrDie(env, |
| 636 | audioRecordClass, JAVA_NATIVERECORDERINJAVAOBJ_FIELD_NAME, "J"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 637 | // mNativeCallbackCookie |
Andreas Gampe | ed6b9df | 2014-11-20 22:02:20 -0800 | [diff] [blame] | 638 | javaAudioRecordFields.nativeCallbackCookie = GetFieldIDOrDie(env, |
| 639 | audioRecordClass, JAVA_NATIVECALLBACKINFO_FIELD_NAME, "J"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 640 | |
Jean-Michel Trivi | 701d6ff | 2014-07-16 07:51:22 -0700 | [diff] [blame] | 641 | // Get the AudioAttributes class and fields |
Andreas Gampe | ed6b9df | 2014-11-20 22:02:20 -0800 | [diff] [blame] | 642 | jclass audioAttrClass = FindClassOrDie(env, kAudioAttributesClassPathName); |
| 643 | javaAudioAttrFields.fieldRecSource = GetFieldIDOrDie(env, audioAttrClass, "mSource", "I"); |
| 644 | javaAudioAttrFields.fieldFlags = GetFieldIDOrDie(env, audioAttrClass, "mFlags", "I"); |
| 645 | javaAudioAttrFields.fieldFormattedTags = GetFieldIDOrDie(env, |
| 646 | audioAttrClass, "mFormattedTags", "Ljava/lang/String;"); |
Jean-Michel Trivi | 701d6ff | 2014-07-16 07:51:22 -0700 | [diff] [blame] | 647 | |
Andreas Gampe | ed6b9df | 2014-11-20 22:02:20 -0800 | [diff] [blame] | 648 | return RegisterMethodsOrDie(env, kClassPathName, gMethods, NELEM(gMethods)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 649 | } |
| 650 | |
| 651 | // ---------------------------------------------------------------------------- |