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