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 | //#define LOG_NDEBUG 0 |
| 17 | |
| 18 | #define LOG_TAG "AudioTrack-JNI" |
| 19 | |
Wei Jia | 071a8b7 | 2015-03-09 16:38:25 -0700 | [diff] [blame] | 20 | #include "android_media_AudioTrack.h" |
| 21 | |
Glenn Kasten | c81d31c | 2012-03-13 14:46:23 -0700 | [diff] [blame] | 22 | #include <JNIHelp.h> |
Jean-Michel Trivi | 7ca0452 | 2014-02-07 09:39:34 -0800 | [diff] [blame] | 23 | #include <JniConstants.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 | |
Jean-Michel Trivi | 7ca0452 | 2014-02-07 09:39:34 -0800 | [diff] [blame] | 26 | #include "ScopedBytes.h" |
| 27 | |
Glenn Kasten | c81d31c | 2012-03-13 14:46:23 -0700 | [diff] [blame] | 28 | #include <utils/Log.h> |
| 29 | #include <media/AudioSystem.h> |
| 30 | #include <media/AudioTrack.h> |
Glenn Kasten | 37967d4 | 2012-11-28 14:37:36 -0800 | [diff] [blame] | 31 | #include <audio_utils/primitives.h> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 32 | |
Mathias Agopian | 0795272 | 2009-05-19 19:08:10 -0700 | [diff] [blame] | 33 | #include <binder/MemoryHeapBase.h> |
| 34 | #include <binder/MemoryBase.h> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 35 | |
Glenn Kasten | fe834d3 | 2014-01-08 14:49:08 -0800 | [diff] [blame] | 36 | #include "android_media_AudioFormat.h" |
Eric Laurent | bc11a69 | 2014-05-16 12:19:25 -0700 | [diff] [blame] | 37 | #include "android_media_AudioErrors.h" |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 38 | |
| 39 | // ---------------------------------------------------------------------------- |
| 40 | |
| 41 | using namespace android; |
| 42 | |
| 43 | // ---------------------------------------------------------------------------- |
| 44 | static const char* const kClassPathName = "android/media/AudioTrack"; |
Jean-Michel Trivi | a1d80e3 | 2014-06-18 08:18:41 -0700 | [diff] [blame] | 45 | static const char* const kAudioAttributesClassPathName = "android/media/AudioAttributes"; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 46 | |
Jean-Michel Trivi | a1d80e3 | 2014-06-18 08:18:41 -0700 | [diff] [blame] | 47 | struct audio_track_fields_t { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 48 | // these fields provide access from C++ to the... |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 49 | jmethodID postNativeEventInJava; //... event post callback method |
Eric Laurent | 83b3685 | 2009-07-28 07:49:22 -0700 | [diff] [blame] | 50 | jfieldID nativeTrackInJavaObj; // stores in Java the native AudioTrack object |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 51 | jfieldID jniData; // stores in Java additional resources used by the native AudioTrack |
Jean-Michel Trivi | a1d80e3 | 2014-06-18 08:18:41 -0700 | [diff] [blame] | 52 | jfieldID fieldStreamType; // ... mStreamType field in the AudioTrack Java object |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 53 | }; |
Jean-Michel Trivi | a1d80e3 | 2014-06-18 08:18:41 -0700 | [diff] [blame] | 54 | struct audio_attributes_fields_t { |
Jean-Michel Trivi | 701d6ff | 2014-07-16 07:51:22 -0700 | [diff] [blame] | 55 | jfieldID fieldUsage; // AudioAttributes.mUsage |
| 56 | jfieldID fieldContentType; // AudioAttributes.mContentType |
| 57 | jfieldID fieldFlags; // AudioAttributes.mFlags |
| 58 | jfieldID fieldFormattedTags;// AudioAttributes.mFormattedTags |
Jean-Michel Trivi | a1d80e3 | 2014-06-18 08:18:41 -0700 | [diff] [blame] | 59 | }; |
| 60 | static audio_track_fields_t javaAudioTrackFields; |
| 61 | static audio_attributes_fields_t javaAudioAttrFields; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 62 | |
| 63 | struct audiotrack_callback_cookie { |
| 64 | jclass audioTrack_class; |
| 65 | jobject audioTrack_ref; |
Eric Laurent | 532bc1c | 2012-04-20 12:45:03 -0700 | [diff] [blame] | 66 | bool busy; |
| 67 | Condition cond; |
| 68 | }; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 69 | |
Glenn Kasten | 3d301cb | 2012-01-16 14:46:54 -0800 | [diff] [blame] | 70 | // keep these values in sync with AudioTrack.java |
| 71 | #define MODE_STATIC 0 |
| 72 | #define MODE_STREAM 1 |
| 73 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 74 | // ---------------------------------------------------------------------------- |
| 75 | class AudioTrackJniStorage { |
| 76 | public: |
| 77 | sp<MemoryHeapBase> mMemHeap; |
| 78 | sp<MemoryBase> mMemBase; |
| 79 | audiotrack_callback_cookie mCallbackData; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 80 | |
| 81 | AudioTrackJniStorage() { |
Jean-Michel Trivi | 8a14968 | 2009-07-15 18:31:11 -0700 | [diff] [blame] | 82 | mCallbackData.audioTrack_class = 0; |
| 83 | mCallbackData.audioTrack_ref = 0; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | ~AudioTrackJniStorage() { |
| 87 | mMemBase.clear(); |
| 88 | mMemHeap.clear(); |
| 89 | } |
| 90 | |
| 91 | bool allocSharedMem(int sizeInBytes) { |
| 92 | mMemHeap = new MemoryHeapBase(sizeInBytes, 0, "AudioTrack Heap Base"); |
| 93 | if (mMemHeap->getHeapID() < 0) { |
| 94 | return false; |
| 95 | } |
| 96 | mMemBase = new MemoryBase(mMemHeap, 0, sizeInBytes); |
| 97 | return true; |
| 98 | } |
| 99 | }; |
| 100 | |
Eric Laurent | 532bc1c | 2012-04-20 12:45:03 -0700 | [diff] [blame] | 101 | static Mutex sLock; |
| 102 | static SortedVector <audiotrack_callback_cookie *> sAudioTrackCallBackCookies; |
| 103 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 104 | // ---------------------------------------------------------------------------- |
| 105 | #define DEFAULT_OUTPUT_SAMPLE_RATE 44100 |
| 106 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 107 | #define AUDIOTRACK_ERROR_SETUP_AUDIOSYSTEM -16 |
Glenn Kasten | fb2ab9e | 2011-12-12 09:05:55 -0800 | [diff] [blame] | 108 | #define AUDIOTRACK_ERROR_SETUP_INVALIDCHANNELMASK -17 |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 109 | #define AUDIOTRACK_ERROR_SETUP_INVALIDFORMAT -18 |
| 110 | #define AUDIOTRACK_ERROR_SETUP_INVALIDSTREAMTYPE -19 |
| 111 | #define AUDIOTRACK_ERROR_SETUP_NATIVEINITFAILED -20 |
| 112 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 113 | // ---------------------------------------------------------------------------- |
Glenn Kasten | e46a86f | 2011-06-01 15:20:35 -0700 | [diff] [blame] | 114 | static void audioCallback(int event, void* user, void *info) { |
Eric Laurent | 532bc1c | 2012-04-20 12:45:03 -0700 | [diff] [blame] | 115 | |
| 116 | audiotrack_callback_cookie *callbackInfo = (audiotrack_callback_cookie *)user; |
| 117 | { |
| 118 | Mutex::Autolock l(sLock); |
| 119 | if (sAudioTrackCallBackCookies.indexOf(callbackInfo) < 0) { |
| 120 | return; |
| 121 | } |
| 122 | callbackInfo->busy = true; |
| 123 | } |
| 124 | |
Glenn Kasten | 5b1576c | 2013-07-18 16:58:19 -0700 | [diff] [blame] | 125 | switch (event) { |
| 126 | case AudioTrack::EVENT_MARKER: { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 127 | JNIEnv *env = AndroidRuntime::getJNIEnv(); |
Glenn Kasten | a667ff3 | 2013-07-22 07:36:34 -0700 | [diff] [blame] | 128 | if (user != NULL && env != NULL) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 129 | env->CallStaticVoidMethod( |
Glenn Kasten | 18db49a | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 130 | callbackInfo->audioTrack_class, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 131 | javaAudioTrackFields.postNativeEventInJava, |
| 132 | callbackInfo->audioTrack_ref, event, 0,0, NULL); |
| 133 | if (env->ExceptionCheck()) { |
| 134 | env->ExceptionDescribe(); |
| 135 | env->ExceptionClear(); |
| 136 | } |
| 137 | } |
Glenn Kasten | 5b1576c | 2013-07-18 16:58:19 -0700 | [diff] [blame] | 138 | } break; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 139 | |
Glenn Kasten | 5b1576c | 2013-07-18 16:58:19 -0700 | [diff] [blame] | 140 | case AudioTrack::EVENT_NEW_POS: { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 141 | JNIEnv *env = AndroidRuntime::getJNIEnv(); |
Glenn Kasten | a667ff3 | 2013-07-22 07:36:34 -0700 | [diff] [blame] | 142 | if (user != NULL && env != NULL) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 143 | env->CallStaticVoidMethod( |
Glenn Kasten | 18db49a | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 144 | callbackInfo->audioTrack_class, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 145 | javaAudioTrackFields.postNativeEventInJava, |
| 146 | callbackInfo->audioTrack_ref, event, 0,0, NULL); |
| 147 | if (env->ExceptionCheck()) { |
| 148 | env->ExceptionDescribe(); |
| 149 | env->ExceptionClear(); |
| 150 | } |
| 151 | } |
Glenn Kasten | 5b1576c | 2013-07-18 16:58:19 -0700 | [diff] [blame] | 152 | } break; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 153 | } |
Glenn Kasten | 5b1576c | 2013-07-18 16:58:19 -0700 | [diff] [blame] | 154 | |
Eric Laurent | 532bc1c | 2012-04-20 12:45:03 -0700 | [diff] [blame] | 155 | { |
| 156 | Mutex::Autolock l(sLock); |
| 157 | callbackInfo->busy = false; |
| 158 | callbackInfo->cond.broadcast(); |
| 159 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 160 | } |
| 161 | |
| 162 | |
| 163 | // ---------------------------------------------------------------------------- |
Eric Laurent | 532bc1c | 2012-04-20 12:45:03 -0700 | [diff] [blame] | 164 | static sp<AudioTrack> getAudioTrack(JNIEnv* env, jobject thiz) |
| 165 | { |
| 166 | Mutex::Autolock l(sLock); |
| 167 | AudioTrack* const at = |
Ashok Bhat | 075e9a1 | 2014-01-06 13:45:09 +0000 | [diff] [blame] | 168 | (AudioTrack*)env->GetLongField(thiz, javaAudioTrackFields.nativeTrackInJavaObj); |
Eric Laurent | 532bc1c | 2012-04-20 12:45:03 -0700 | [diff] [blame] | 169 | return sp<AudioTrack>(at); |
| 170 | } |
| 171 | |
| 172 | static sp<AudioTrack> setAudioTrack(JNIEnv* env, jobject thiz, const sp<AudioTrack>& at) |
| 173 | { |
| 174 | Mutex::Autolock l(sLock); |
| 175 | sp<AudioTrack> old = |
Ashok Bhat | 075e9a1 | 2014-01-06 13:45:09 +0000 | [diff] [blame] | 176 | (AudioTrack*)env->GetLongField(thiz, javaAudioTrackFields.nativeTrackInJavaObj); |
Eric Laurent | 532bc1c | 2012-04-20 12:45:03 -0700 | [diff] [blame] | 177 | if (at.get()) { |
Mathias Agopian | b1d90c8 | 2013-03-06 17:45:42 -0800 | [diff] [blame] | 178 | at->incStrong((void*)setAudioTrack); |
Eric Laurent | 532bc1c | 2012-04-20 12:45:03 -0700 | [diff] [blame] | 179 | } |
| 180 | if (old != 0) { |
Mathias Agopian | b1d90c8 | 2013-03-06 17:45:42 -0800 | [diff] [blame] | 181 | old->decStrong((void*)setAudioTrack); |
Eric Laurent | 532bc1c | 2012-04-20 12:45:03 -0700 | [diff] [blame] | 182 | } |
Ashok Bhat | 075e9a1 | 2014-01-06 13:45:09 +0000 | [diff] [blame] | 183 | env->SetLongField(thiz, javaAudioTrackFields.nativeTrackInJavaObj, (jlong)at.get()); |
Eric Laurent | 532bc1c | 2012-04-20 12:45:03 -0700 | [diff] [blame] | 184 | return old; |
| 185 | } |
Wei Jia | 071a8b7 | 2015-03-09 16:38:25 -0700 | [diff] [blame] | 186 | |
| 187 | // ---------------------------------------------------------------------------- |
| 188 | sp<AudioTrack> android_media_AudioTrack_getAudioTrack(JNIEnv* env, jobject audioTrackObj) { |
| 189 | return getAudioTrack(env, audioTrackObj); |
| 190 | } |
| 191 | |
Eric Laurent | 532bc1c | 2012-04-20 12:45:03 -0700 | [diff] [blame] | 192 | // ---------------------------------------------------------------------------- |
Ashok Bhat | 075e9a1 | 2014-01-06 13:45:09 +0000 | [diff] [blame] | 193 | static jint |
Jean-Michel Trivi | eac8438 | 2014-02-04 14:50:40 -0800 | [diff] [blame] | 194 | android_media_AudioTrack_setup(JNIEnv *env, jobject thiz, jobject weak_this, |
Jean-Michel Trivi | a1d80e3 | 2014-06-18 08:18:41 -0700 | [diff] [blame] | 195 | jobject jaa, |
| 196 | jint sampleRateInHertz, jint javaChannelMask, |
| 197 | jint audioFormat, jint buffSizeInBytes, jint memoryMode, jintArray jSession) { |
| 198 | |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 199 | ALOGV("sampleRate=%d, audioFormat(from Java)=%d, channel mask=%x, buffSize=%d", |
Jean-Michel Trivi | d9ae1c5 | 2011-07-25 12:58:14 -0700 | [diff] [blame] | 200 | sampleRateInHertz, audioFormat, javaChannelMask, buffSizeInBytes); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 201 | |
Jean-Michel Trivi | a1d80e3 | 2014-06-18 08:18:41 -0700 | [diff] [blame] | 202 | if (jaa == 0) { |
| 203 | ALOGE("Error creating AudioTrack: invalid audio attributes"); |
| 204 | return (jint) AUDIO_JAVA_ERROR; |
| 205 | } |
| 206 | |
Andy Hung | a9470c1 | 2015-01-29 17:16:03 -0800 | [diff] [blame] | 207 | // Java channel masks don't map directly to the native definition for positional |
| 208 | // channel masks: it's a shift by 2 to skip the two deprecated channel |
| 209 | // configurations "default" and "mono". |
| 210 | // Invalid channel representations are caught by !audio_is_output_channel() below. |
| 211 | audio_channel_mask_t nativeChannelMask = |
| 212 | audio_channel_mask_get_representation(javaChannelMask) |
| 213 | == AUDIO_CHANNEL_REPRESENTATION_POSITION |
| 214 | ? javaChannelMask >> 2 : javaChannelMask; |
Jean-Michel Trivi | d9ae1c5 | 2011-07-25 12:58:14 -0700 | [diff] [blame] | 215 | |
| 216 | if (!audio_is_output_channel(nativeChannelMask)) { |
Glenn Kasten | 33c437d | 2013-07-18 17:04:21 -0700 | [diff] [blame] | 217 | ALOGE("Error creating AudioTrack: invalid channel mask %#x.", javaChannelMask); |
Ashok Bhat | 075e9a1 | 2014-01-06 13:45:09 +0000 | [diff] [blame] | 218 | return (jint) AUDIOTRACK_ERROR_SETUP_INVALIDCHANNELMASK; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 219 | } |
Jean-Michel Trivi | d9ae1c5 | 2011-07-25 12:58:14 -0700 | [diff] [blame] | 220 | |
Eric Laurent | dbf5566 | 2014-07-01 18:56:30 -0700 | [diff] [blame] | 221 | uint32_t channelCount = audio_channel_count_from_out_mask(nativeChannelMask); |
Glenn Kasten | 18db49a | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 222 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 223 | // check the format. |
| 224 | // This function was called from Java, so we compare the format against the Java constants |
Glenn Kasten | fe834d3 | 2014-01-08 14:49:08 -0800 | [diff] [blame] | 225 | audio_format_t format = audioFormatToNative(audioFormat); |
| 226 | if (format == AUDIO_FORMAT_INVALID) { |
Glenn Kasten | 8f81d08 | 2012-11-28 14:37:48 -0800 | [diff] [blame] | 227 | ALOGE("Error creating AudioTrack: unsupported audio format %d.", audioFormat); |
Ashok Bhat | 075e9a1 | 2014-01-06 13:45:09 +0000 | [diff] [blame] | 228 | return (jint) AUDIOTRACK_ERROR_SETUP_INVALIDFORMAT; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 229 | } |
| 230 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 231 | // compute the frame count |
Eric Laurent | ff0d9f0 | 2014-06-09 17:23:02 -0700 | [diff] [blame] | 232 | size_t frameCount; |
| 233 | if (audio_is_linear_pcm(format)) { |
| 234 | const size_t bytesPerSample = audio_bytes_per_sample(format); |
| 235 | frameCount = buffSizeInBytes / (channelCount * bytesPerSample); |
| 236 | } else { |
| 237 | frameCount = buffSizeInBytes; |
| 238 | } |
Glenn Kasten | 18db49a | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 239 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 240 | jclass clazz = env->GetObjectClass(thiz); |
| 241 | if (clazz == NULL) { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 242 | ALOGE("Can't find %s when setting up callback.", kClassPathName); |
Ashok Bhat | 075e9a1 | 2014-01-06 13:45:09 +0000 | [diff] [blame] | 243 | return (jint) AUDIOTRACK_ERROR_SETUP_NATIVEINITFAILED; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 244 | } |
Eric Laurent | 2fb43ef | 2010-09-24 12:03:36 -0700 | [diff] [blame] | 245 | |
| 246 | if (jSession == NULL) { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 247 | ALOGE("Error creating AudioTrack: invalid session ID pointer"); |
Eric Laurent | bc11a69 | 2014-05-16 12:19:25 -0700 | [diff] [blame] | 248 | return (jint) AUDIO_JAVA_ERROR; |
Eric Laurent | 619346f | 2010-06-21 09:27:30 -0700 | [diff] [blame] | 249 | } |
| 250 | |
Eric Laurent | 2fb43ef | 2010-09-24 12:03:36 -0700 | [diff] [blame] | 251 | jint* 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 AudioTrack: Error retrieving session id pointer"); |
Eric Laurent | bc11a69 | 2014-05-16 12:19:25 -0700 | [diff] [blame] | 254 | return (jint) AUDIO_JAVA_ERROR; |
Eric Laurent | 2fb43ef | 2010-09-24 12:03:36 -0700 | [diff] [blame] | 255 | } |
| 256 | int sessionId = nSession[0]; |
| 257 | env->ReleasePrimitiveArrayCritical(jSession, nSession, 0); |
| 258 | nSession = NULL; |
| 259 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 260 | // create the native AudioTrack object |
Eric Laurent | 532bc1c | 2012-04-20 12:45:03 -0700 | [diff] [blame] | 261 | sp<AudioTrack> lpTrack = new AudioTrack(); |
Glenn Kasten | 18db49a | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 262 | |
Jean-Michel Trivi | a1d80e3 | 2014-06-18 08:18:41 -0700 | [diff] [blame] | 263 | audio_attributes_t *paa = NULL; |
| 264 | // read the AudioAttributes values |
| 265 | paa = (audio_attributes_t *) calloc(1, sizeof(audio_attributes_t)); |
Jean-Michel Trivi | 701d6ff | 2014-07-16 07:51:22 -0700 | [diff] [blame] | 266 | const jstring jtags = |
| 267 | (jstring) env->GetObjectField(jaa, javaAudioAttrFields.fieldFormattedTags); |
Jean-Michel Trivi | a1d80e3 | 2014-06-18 08:18:41 -0700 | [diff] [blame] | 268 | const char* tags = env->GetStringUTFChars(jtags, NULL); |
| 269 | // copying array size -1, char array for tags was calloc'd, no need to NULL-terminate it |
| 270 | strncpy(paa->tags, tags, AUDIO_ATTRIBUTES_TAGS_MAX_SIZE - 1); |
| 271 | env->ReleaseStringUTFChars(jtags, tags); |
| 272 | paa->usage = (audio_usage_t) env->GetIntField(jaa, javaAudioAttrFields.fieldUsage); |
| 273 | paa->content_type = |
| 274 | (audio_content_type_t) env->GetIntField(jaa, javaAudioAttrFields.fieldContentType); |
| 275 | paa->flags = env->GetIntField(jaa, javaAudioAttrFields.fieldFlags); |
| 276 | |
| 277 | ALOGV("AudioTrack_setup for usage=%d content=%d flags=0x%#x tags=%s", |
| 278 | paa->usage, paa->content_type, paa->flags, paa->tags); |
| 279 | |
Eric Laurent | 532bc1c | 2012-04-20 12:45:03 -0700 | [diff] [blame] | 280 | // initialize the callback information: |
| 281 | // this data will be passed with every AudioTrack callback |
| 282 | AudioTrackJniStorage* lpJniStorage = new AudioTrackJniStorage(); |
Eric Laurent | 532bc1c | 2012-04-20 12:45:03 -0700 | [diff] [blame] | 283 | lpJniStorage->mCallbackData.audioTrack_class = (jclass)env->NewGlobalRef(clazz); |
| 284 | // we use a weak reference so the AudioTrack object can be garbage collected. |
| 285 | lpJniStorage->mCallbackData.audioTrack_ref = env->NewGlobalRef(weak_this); |
| 286 | lpJniStorage->mCallbackData.busy = false; |
| 287 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 288 | // initialize the native AudioTrack object |
Jean-Michel Trivi | 2defed9 | 2014-05-14 18:15:59 -0700 | [diff] [blame] | 289 | status_t status = NO_ERROR; |
Glenn Kasten | 3d301cb | 2012-01-16 14:46:54 -0800 | [diff] [blame] | 290 | switch (memoryMode) { |
| 291 | case MODE_STREAM: |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 292 | |
Glenn Kasten | c5d1424 | 2014-03-28 16:19:52 -0700 | [diff] [blame] | 293 | status = lpTrack->set( |
Jean-Michel Trivi | c8d6b6f | 2014-07-28 09:52:14 -0700 | [diff] [blame] | 294 | AUDIO_STREAM_DEFAULT,// stream type, but more info conveyed in paa (last argument) |
Jean-Michel Trivi | a1d80e3 | 2014-06-18 08:18:41 -0700 | [diff] [blame] | 295 | sampleRateInHertz, |
| 296 | format,// word length, PCM |
| 297 | nativeChannelMask, |
| 298 | frameCount, |
| 299 | AUDIO_OUTPUT_FLAG_NONE, |
| 300 | audioCallback, &(lpJniStorage->mCallbackData),//callback, callback data (user) |
| 301 | 0,// notificationFrames == 0 since not using EVENT_MORE_DATA to feed the AudioTrack |
| 302 | 0,// shared mem |
| 303 | true,// thread can call Java |
| 304 | sessionId,// audio session ID |
Jean-Michel Trivi | c8d6b6f | 2014-07-28 09:52:14 -0700 | [diff] [blame] | 305 | AudioTrack::TRANSFER_SYNC, |
Jean-Michel Trivi | a1d80e3 | 2014-06-18 08:18:41 -0700 | [diff] [blame] | 306 | NULL, // default offloadInfo |
| 307 | -1, -1, // default uid, pid values |
| 308 | paa); |
Glenn Kasten | 3d301cb | 2012-01-16 14:46:54 -0800 | [diff] [blame] | 309 | break; |
Glenn Kasten | 18db49a | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 310 | |
Glenn Kasten | 3d301cb | 2012-01-16 14:46:54 -0800 | [diff] [blame] | 311 | case MODE_STATIC: |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 312 | // AudioTrack is using shared memory |
Glenn Kasten | 18db49a | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 313 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 314 | if (!lpJniStorage->allocSharedMem(buffSizeInBytes)) { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 315 | ALOGE("Error creating AudioTrack in static mode: error creating mem heap base"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 316 | goto native_init_failure; |
| 317 | } |
Glenn Kasten | 18db49a | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 318 | |
Glenn Kasten | c5d1424 | 2014-03-28 16:19:52 -0700 | [diff] [blame] | 319 | status = lpTrack->set( |
Jean-Michel Trivi | c8d6b6f | 2014-07-28 09:52:14 -0700 | [diff] [blame] | 320 | AUDIO_STREAM_DEFAULT,// stream type, but more info conveyed in paa (last argument) |
Jean-Michel Trivi | a1d80e3 | 2014-06-18 08:18:41 -0700 | [diff] [blame] | 321 | sampleRateInHertz, |
| 322 | format,// word length, PCM |
| 323 | nativeChannelMask, |
| 324 | frameCount, |
| 325 | AUDIO_OUTPUT_FLAG_NONE, |
| 326 | audioCallback, &(lpJniStorage->mCallbackData),//callback, callback data (user)); |
| 327 | 0,// notificationFrames == 0 since not using EVENT_MORE_DATA to feed the AudioTrack |
| 328 | lpJniStorage->mMemBase,// shared mem |
| 329 | true,// thread can call Java |
| 330 | sessionId,// audio session ID |
Jean-Michel Trivi | c8d6b6f | 2014-07-28 09:52:14 -0700 | [diff] [blame] | 331 | AudioTrack::TRANSFER_SHARED, |
Jean-Michel Trivi | a1d80e3 | 2014-06-18 08:18:41 -0700 | [diff] [blame] | 332 | NULL, // default offloadInfo |
| 333 | -1, -1, // default uid, pid values |
| 334 | paa); |
Glenn Kasten | 3d301cb | 2012-01-16 14:46:54 -0800 | [diff] [blame] | 335 | break; |
| 336 | |
| 337 | default: |
| 338 | ALOGE("Unknown mode %d", memoryMode); |
| 339 | goto native_init_failure; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 340 | } |
| 341 | |
Glenn Kasten | c5d1424 | 2014-03-28 16:19:52 -0700 | [diff] [blame] | 342 | if (status != NO_ERROR) { |
| 343 | ALOGE("Error %d initializing AudioTrack", status); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 344 | goto native_init_failure; |
| 345 | } |
| 346 | |
Eric Laurent | 2fb43ef | 2010-09-24 12:03:36 -0700 | [diff] [blame] | 347 | nSession = (jint *) env->GetPrimitiveArrayCritical(jSession, NULL); |
| 348 | if (nSession == NULL) { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 349 | ALOGE("Error creating AudioTrack: Error retrieving session id pointer"); |
Eric Laurent | 2fb43ef | 2010-09-24 12:03:36 -0700 | [diff] [blame] | 350 | goto native_init_failure; |
| 351 | } |
Eric Laurent | 619346f | 2010-06-21 09:27:30 -0700 | [diff] [blame] | 352 | // read the audio session ID back from AudioTrack in case we create a new session |
| 353 | nSession[0] = lpTrack->getSessionId(); |
Eric Laurent | 619346f | 2010-06-21 09:27:30 -0700 | [diff] [blame] | 354 | env->ReleasePrimitiveArrayCritical(jSession, nSession, 0); |
| 355 | nSession = NULL; |
| 356 | |
Eric Laurent | 532bc1c | 2012-04-20 12:45:03 -0700 | [diff] [blame] | 357 | { // scope for the lock |
| 358 | Mutex::Autolock l(sLock); |
| 359 | sAudioTrackCallBackCookies.add(&lpJniStorage->mCallbackData); |
| 360 | } |
Glenn Kasten | 18db49a | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 361 | // save our newly created C++ AudioTrack in the "nativeTrackInJavaObj" field |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 362 | // of the Java object (in mNativeTrackInJavaObj) |
Eric Laurent | 532bc1c | 2012-04-20 12:45:03 -0700 | [diff] [blame] | 363 | setAudioTrack(env, thiz, lpTrack); |
Glenn Kasten | 18db49a | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 364 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 365 | // save the JNI resources so we can free them later |
Ashok Bhat | 075e9a1 | 2014-01-06 13:45:09 +0000 | [diff] [blame] | 366 | //ALOGV("storing lpJniStorage: %x\n", (long)lpJniStorage); |
| 367 | env->SetLongField(thiz, javaAudioTrackFields.jniData, (jlong)lpJniStorage); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 368 | |
Jean-Michel Trivi | a1d80e3 | 2014-06-18 08:18:41 -0700 | [diff] [blame] | 369 | // since we had audio attributes, the stream type was derived from them during the |
| 370 | // creation of the native AudioTrack: push the same value to the Java object |
| 371 | env->SetIntField(thiz, javaAudioTrackFields.fieldStreamType, (jint) lpTrack->streamType()); |
| 372 | // audio attributes were copied in AudioTrack creation |
| 373 | free(paa); |
| 374 | paa = NULL; |
| 375 | |
| 376 | |
Eric Laurent | bc11a69 | 2014-05-16 12:19:25 -0700 | [diff] [blame] | 377 | return (jint) AUDIO_JAVA_SUCCESS; |
Glenn Kasten | 18db49a | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 378 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 379 | // failures: |
| 380 | native_init_failure: |
Jean-Michel Trivi | a1d80e3 | 2014-06-18 08:18:41 -0700 | [diff] [blame] | 381 | if (paa != NULL) { |
| 382 | free(paa); |
| 383 | } |
Eric Laurent | 619346f | 2010-06-21 09:27:30 -0700 | [diff] [blame] | 384 | if (nSession != NULL) { |
| 385 | env->ReleasePrimitiveArrayCritical(jSession, nSession, 0); |
| 386 | } |
Jean-Michel Trivi | 8a14968 | 2009-07-15 18:31:11 -0700 | [diff] [blame] | 387 | env->DeleteGlobalRef(lpJniStorage->mCallbackData.audioTrack_class); |
| 388 | env->DeleteGlobalRef(lpJniStorage->mCallbackData.audioTrack_ref); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 389 | delete lpJniStorage; |
Ashok Bhat | 075e9a1 | 2014-01-06 13:45:09 +0000 | [diff] [blame] | 390 | env->SetLongField(thiz, javaAudioTrackFields.jniData, 0); |
Glenn Kasten | 18db49a | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 391 | |
Ashok Bhat | 075e9a1 | 2014-01-06 13:45:09 +0000 | [diff] [blame] | 392 | return (jint) AUDIOTRACK_ERROR_SETUP_NATIVEINITFAILED; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 393 | } |
| 394 | |
| 395 | |
| 396 | // ---------------------------------------------------------------------------- |
| 397 | static void |
| 398 | android_media_AudioTrack_start(JNIEnv *env, jobject thiz) |
| 399 | { |
Eric Laurent | 532bc1c | 2012-04-20 12:45:03 -0700 | [diff] [blame] | 400 | sp<AudioTrack> lpTrack = getAudioTrack(env, thiz); |
| 401 | if (lpTrack == NULL) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 402 | jniThrowException(env, "java/lang/IllegalStateException", |
| 403 | "Unable to retrieve AudioTrack pointer for start()"); |
Eric Laurent | 7070b36 | 2010-07-16 07:43:46 -0700 | [diff] [blame] | 404 | return; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 405 | } |
Jean-Michel Trivi | 21dc037 | 2009-05-08 16:06:34 -0700 | [diff] [blame] | 406 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 407 | lpTrack->start(); |
| 408 | } |
| 409 | |
| 410 | |
| 411 | // ---------------------------------------------------------------------------- |
| 412 | static void |
| 413 | android_media_AudioTrack_stop(JNIEnv *env, jobject thiz) |
| 414 | { |
Eric Laurent | 532bc1c | 2012-04-20 12:45:03 -0700 | [diff] [blame] | 415 | sp<AudioTrack> lpTrack = getAudioTrack(env, thiz); |
| 416 | if (lpTrack == NULL) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 417 | jniThrowException(env, "java/lang/IllegalStateException", |
| 418 | "Unable to retrieve AudioTrack pointer for stop()"); |
Eric Laurent | 7070b36 | 2010-07-16 07:43:46 -0700 | [diff] [blame] | 419 | return; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 420 | } |
| 421 | |
| 422 | lpTrack->stop(); |
| 423 | } |
| 424 | |
| 425 | |
| 426 | // ---------------------------------------------------------------------------- |
| 427 | static void |
| 428 | android_media_AudioTrack_pause(JNIEnv *env, jobject thiz) |
| 429 | { |
Eric Laurent | 532bc1c | 2012-04-20 12:45:03 -0700 | [diff] [blame] | 430 | sp<AudioTrack> lpTrack = getAudioTrack(env, thiz); |
| 431 | if (lpTrack == NULL) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 432 | jniThrowException(env, "java/lang/IllegalStateException", |
| 433 | "Unable to retrieve AudioTrack pointer for pause()"); |
Eric Laurent | 7070b36 | 2010-07-16 07:43:46 -0700 | [diff] [blame] | 434 | return; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 435 | } |
| 436 | |
| 437 | lpTrack->pause(); |
| 438 | } |
| 439 | |
| 440 | |
| 441 | // ---------------------------------------------------------------------------- |
| 442 | static void |
| 443 | android_media_AudioTrack_flush(JNIEnv *env, jobject thiz) |
| 444 | { |
Eric Laurent | 532bc1c | 2012-04-20 12:45:03 -0700 | [diff] [blame] | 445 | sp<AudioTrack> lpTrack = getAudioTrack(env, thiz); |
| 446 | if (lpTrack == NULL) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 447 | jniThrowException(env, "java/lang/IllegalStateException", |
| 448 | "Unable to retrieve AudioTrack pointer for flush()"); |
Eric Laurent | 7070b36 | 2010-07-16 07:43:46 -0700 | [diff] [blame] | 449 | return; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 450 | } |
| 451 | |
| 452 | lpTrack->flush(); |
| 453 | } |
| 454 | |
| 455 | // ---------------------------------------------------------------------------- |
| 456 | static void |
| 457 | android_media_AudioTrack_set_volume(JNIEnv *env, jobject thiz, jfloat leftVol, jfloat rightVol ) |
| 458 | { |
Eric Laurent | 532bc1c | 2012-04-20 12:45:03 -0700 | [diff] [blame] | 459 | sp<AudioTrack> lpTrack = getAudioTrack(env, thiz); |
| 460 | if (lpTrack == NULL) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 461 | jniThrowException(env, "java/lang/IllegalStateException", |
| 462 | "Unable to retrieve AudioTrack pointer for setVolume()"); |
Eric Laurent | 7070b36 | 2010-07-16 07:43:46 -0700 | [diff] [blame] | 463 | return; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 464 | } |
| 465 | |
| 466 | lpTrack->setVolume(leftVol, rightVol); |
| 467 | } |
| 468 | |
| 469 | // ---------------------------------------------------------------------------- |
Glenn Kasten | 18db49a | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 470 | |
Eric Laurent | 532bc1c | 2012-04-20 12:45:03 -0700 | [diff] [blame] | 471 | #define CALLBACK_COND_WAIT_TIMEOUT_MS 1000 |
Jean-Michel Trivi | eac8438 | 2014-02-04 14:50:40 -0800 | [diff] [blame] | 472 | static void android_media_AudioTrack_release(JNIEnv *env, jobject thiz) { |
Eric Laurent | 532bc1c | 2012-04-20 12:45:03 -0700 | [diff] [blame] | 473 | sp<AudioTrack> lpTrack = setAudioTrack(env, thiz, 0); |
| 474 | if (lpTrack == NULL) { |
| 475 | return; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 476 | } |
Eric Laurent | 532bc1c | 2012-04-20 12:45:03 -0700 | [diff] [blame] | 477 | //ALOGV("deleting lpTrack: %x\n", (int)lpTrack); |
| 478 | lpTrack->stop(); |
Glenn Kasten | 18db49a | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 479 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 480 | // delete the JNI data |
Ashok Bhat | 075e9a1 | 2014-01-06 13:45:09 +0000 | [diff] [blame] | 481 | AudioTrackJniStorage* pJniStorage = (AudioTrackJniStorage *)env->GetLongField( |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 482 | thiz, javaAudioTrackFields.jniData); |
Eric Laurent | 532bc1c | 2012-04-20 12:45:03 -0700 | [diff] [blame] | 483 | // reset the native resources in the Java object so any attempt to access |
| 484 | // them after a call to release fails. |
Ashok Bhat | 075e9a1 | 2014-01-06 13:45:09 +0000 | [diff] [blame] | 485 | env->SetLongField(thiz, javaAudioTrackFields.jniData, 0); |
Eric Laurent | 532bc1c | 2012-04-20 12:45:03 -0700 | [diff] [blame] | 486 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 487 | if (pJniStorage) { |
Eric Laurent | 532bc1c | 2012-04-20 12:45:03 -0700 | [diff] [blame] | 488 | Mutex::Autolock l(sLock); |
| 489 | audiotrack_callback_cookie *lpCookie = &pJniStorage->mCallbackData; |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 490 | //ALOGV("deleting pJniStorage: %x\n", (int)pJniStorage); |
Eric Laurent | 532bc1c | 2012-04-20 12:45:03 -0700 | [diff] [blame] | 491 | while (lpCookie->busy) { |
| 492 | if (lpCookie->cond.waitRelative(sLock, |
| 493 | milliseconds(CALLBACK_COND_WAIT_TIMEOUT_MS)) != |
| 494 | NO_ERROR) { |
| 495 | break; |
| 496 | } |
| 497 | } |
| 498 | sAudioTrackCallBackCookies.remove(lpCookie); |
| 499 | // delete global refs created in native_setup |
| 500 | env->DeleteGlobalRef(lpCookie->audioTrack_class); |
| 501 | env->DeleteGlobalRef(lpCookie->audioTrack_ref); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 502 | delete pJniStorage; |
| 503 | } |
| 504 | } |
| 505 | |
Glenn Kasten | 18db49a | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 506 | |
Eric Laurent | 532bc1c | 2012-04-20 12:45:03 -0700 | [diff] [blame] | 507 | // ---------------------------------------------------------------------------- |
Jean-Michel Trivi | eac8438 | 2014-02-04 14:50:40 -0800 | [diff] [blame] | 508 | static void android_media_AudioTrack_finalize(JNIEnv *env, jobject thiz) { |
| 509 | //ALOGV("android_media_AudioTrack_finalize jobject: %x\n", (int)thiz); |
| 510 | android_media_AudioTrack_release(env, thiz); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 511 | } |
| 512 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 513 | // ---------------------------------------------------------------------------- |
Jean-Michel Trivi | 7ca0452 | 2014-02-07 09:39:34 -0800 | [diff] [blame] | 514 | jint writeToTrack(const sp<AudioTrack>& track, jint audioFormat, const jbyte* data, |
| 515 | jint offsetInBytes, jint sizeInBytes, bool blocking = true) { |
Jean-Michel Trivi | 21dc037 | 2009-05-08 16:06:34 -0700 | [diff] [blame] | 516 | // give the data to the native AudioTrack object (the data starts at the offset) |
| 517 | ssize_t written = 0; |
| 518 | // regular write() or copy the data to the AudioTrack's shared memory? |
Eric Laurent | 532bc1c | 2012-04-20 12:45:03 -0700 | [diff] [blame] | 519 | if (track->sharedBuffer() == 0) { |
Jean-Michel Trivi | 7ca0452 | 2014-02-07 09:39:34 -0800 | [diff] [blame] | 520 | written = track->write(data + offsetInBytes, sizeInBytes, blocking); |
Glenn Kasten | 9b53db3 | 2013-07-10 14:13:39 -0700 | [diff] [blame] | 521 | // for compatibility with earlier behavior of write(), return 0 in this case |
| 522 | if (written == (ssize_t) WOULD_BLOCK) { |
| 523 | written = 0; |
| 524 | } |
Jean-Michel Trivi | 21dc037 | 2009-05-08 16:06:34 -0700 | [diff] [blame] | 525 | } else { |
Andy Hung | 2c0e17c | 2015-01-12 15:07:14 -0800 | [diff] [blame] | 526 | // writing to shared memory, check for capacity |
| 527 | if ((size_t)sizeInBytes > track->sharedBuffer()->size()) { |
| 528 | sizeInBytes = track->sharedBuffer()->size(); |
Jean-Michel Trivi | 21dc037 | 2009-05-08 16:06:34 -0700 | [diff] [blame] | 529 | } |
Andy Hung | 2c0e17c | 2015-01-12 15:07:14 -0800 | [diff] [blame] | 530 | memcpy(track->sharedBuffer()->pointer(), data + offsetInBytes, sizeInBytes); |
| 531 | written = sizeInBytes; |
Jean-Michel Trivi | 21dc037 | 2009-05-08 16:06:34 -0700 | [diff] [blame] | 532 | } |
| 533 | return written; |
Jean-Michel Trivi | 21dc037 | 2009-05-08 16:06:34 -0700 | [diff] [blame] | 534 | } |
| 535 | |
| 536 | // ---------------------------------------------------------------------------- |
Jean-Michel Trivi | eac8438 | 2014-02-04 14:50:40 -0800 | [diff] [blame] | 537 | static jint android_media_AudioTrack_write_byte(JNIEnv *env, jobject thiz, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 538 | jbyteArray javaAudioData, |
| 539 | jint offsetInBytes, jint sizeInBytes, |
Jean-Michel Trivi | 7ca0452 | 2014-02-07 09:39:34 -0800 | [diff] [blame] | 540 | jint javaAudioFormat, |
| 541 | jboolean isWriteBlocking) { |
Jean-Michel Trivi | eac8438 | 2014-02-04 14:50:40 -0800 | [diff] [blame] | 542 | //ALOGV("android_media_AudioTrack_write_byte(offset=%d, sizeInBytes=%d) called", |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 543 | // offsetInBytes, sizeInBytes); |
Eric Laurent | 532bc1c | 2012-04-20 12:45:03 -0700 | [diff] [blame] | 544 | sp<AudioTrack> lpTrack = getAudioTrack(env, thiz); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 545 | if (lpTrack == NULL) { |
| 546 | jniThrowException(env, "java/lang/IllegalStateException", |
| 547 | "Unable to retrieve AudioTrack pointer for write()"); |
Eric Laurent | 7070b36 | 2010-07-16 07:43:46 -0700 | [diff] [blame] | 548 | return 0; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 549 | } |
| 550 | |
| 551 | // get the pointer for the audio data from the java array |
Eric Laurent | 421ddc0 | 2011-03-07 14:52:59 -0800 | [diff] [blame] | 552 | // NOTE: We may use GetPrimitiveArrayCritical() when the JNI implementation changes in such |
| 553 | // a way that it becomes much more efficient. When doing so, we will have to prevent the |
| 554 | // AudioSystem callback to be called while in critical section (in case of media server |
| 555 | // process crash for instance) |
Eric Laurent | 532bc1c | 2012-04-20 12:45:03 -0700 | [diff] [blame] | 556 | jbyte* cAudioData = NULL; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 557 | if (javaAudioData) { |
Eric Laurent | 421ddc0 | 2011-03-07 14:52:59 -0800 | [diff] [blame] | 558 | cAudioData = (jbyte *)env->GetByteArrayElements(javaAudioData, NULL); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 559 | if (cAudioData == NULL) { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 560 | ALOGE("Error retrieving source of audio data to play, can't play"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 561 | return 0; // out of memory or no data to load |
| 562 | } |
| 563 | } else { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 564 | ALOGE("NULL java array of audio data to play, can't play"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 565 | return 0; |
| 566 | } |
| 567 | |
Jean-Michel Trivi | 7ca0452 | 2014-02-07 09:39:34 -0800 | [diff] [blame] | 568 | jint written = writeToTrack(lpTrack, javaAudioFormat, cAudioData, offsetInBytes, sizeInBytes, |
| 569 | isWriteBlocking == JNI_TRUE /* blocking */); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 570 | |
Eric Laurent | 421ddc0 | 2011-03-07 14:52:59 -0800 | [diff] [blame] | 571 | env->ReleaseByteArrayElements(javaAudioData, cAudioData, 0); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 572 | |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 573 | //ALOGV("write wrote %d (tried %d) bytes in the native AudioTrack with offset %d", |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 574 | // (int)written, (int)(sizeInBytes), (int)offsetInBytes); |
Jean-Michel Trivi | 21dc037 | 2009-05-08 16:06:34 -0700 | [diff] [blame] | 575 | return written; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 576 | } |
| 577 | |
| 578 | |
| 579 | // ---------------------------------------------------------------------------- |
Jean-Michel Trivi | 7ca0452 | 2014-02-07 09:39:34 -0800 | [diff] [blame] | 580 | static jint android_media_AudioTrack_write_native_bytes(JNIEnv *env, jobject thiz, |
Jean-Michel Trivi | 5d21f67 | 2014-03-20 17:02:31 -0700 | [diff] [blame] | 581 | jbyteArray javaBytes, jint byteOffset, jint sizeInBytes, |
Jean-Michel Trivi | 7ca0452 | 2014-02-07 09:39:34 -0800 | [diff] [blame] | 582 | jint javaAudioFormat, jboolean isWriteBlocking) { |
| 583 | //ALOGV("android_media_AudioTrack_write_native_bytes(offset=%d, sizeInBytes=%d) called", |
| 584 | // offsetInBytes, sizeInBytes); |
| 585 | sp<AudioTrack> lpTrack = getAudioTrack(env, thiz); |
| 586 | if (lpTrack == NULL) { |
| 587 | jniThrowException(env, "java/lang/IllegalStateException", |
| 588 | "Unable to retrieve AudioTrack pointer for write()"); |
| 589 | return 0; |
| 590 | } |
| 591 | |
| 592 | ScopedBytesRO bytes(env, javaBytes); |
| 593 | if (bytes.get() == NULL) { |
| 594 | ALOGE("Error retrieving source of audio data to play, can't play"); |
Eric Laurent | bc11a69 | 2014-05-16 12:19:25 -0700 | [diff] [blame] | 595 | return (jint)AUDIO_JAVA_BAD_VALUE; |
Jean-Michel Trivi | 7ca0452 | 2014-02-07 09:39:34 -0800 | [diff] [blame] | 596 | } |
| 597 | |
Jean-Michel Trivi | 5d21f67 | 2014-03-20 17:02:31 -0700 | [diff] [blame] | 598 | jint written = writeToTrack(lpTrack, javaAudioFormat, bytes.get(), byteOffset, |
Jean-Michel Trivi | 7ca0452 | 2014-02-07 09:39:34 -0800 | [diff] [blame] | 599 | sizeInBytes, isWriteBlocking == JNI_TRUE /* blocking */); |
| 600 | |
| 601 | return written; |
| 602 | } |
| 603 | |
| 604 | // ---------------------------------------------------------------------------- |
Jean-Michel Trivi | eac8438 | 2014-02-04 14:50:40 -0800 | [diff] [blame] | 605 | static jint android_media_AudioTrack_write_short(JNIEnv *env, jobject thiz, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 606 | jshortArray javaAudioData, |
| 607 | jint offsetInShorts, jint sizeInShorts, |
| 608 | jint javaAudioFormat) { |
Eric Laurent | 3d4c641 | 2014-04-30 11:59:21 -0700 | [diff] [blame] | 609 | |
| 610 | //ALOGV("android_media_AudioTrack_write_short(offset=%d, sizeInShorts=%d) called", |
| 611 | // offsetInShorts, sizeInShorts); |
| 612 | sp<AudioTrack> lpTrack = getAudioTrack(env, thiz); |
| 613 | if (lpTrack == NULL) { |
| 614 | jniThrowException(env, "java/lang/IllegalStateException", |
| 615 | "Unable to retrieve AudioTrack pointer for write()"); |
| 616 | return 0; |
Glenn Kasten | f7e0a370 | 2013-07-10 14:03:03 -0700 | [diff] [blame] | 617 | } |
Eric Laurent | 3d4c641 | 2014-04-30 11:59:21 -0700 | [diff] [blame] | 618 | |
| 619 | // get the pointer for the audio data from the java array |
| 620 | // NOTE: We may use GetPrimitiveArrayCritical() when the JNI implementation changes in such |
| 621 | // a way that it becomes much more efficient. When doing so, we will have to prevent the |
| 622 | // AudioSystem callback to be called while in critical section (in case of media server |
| 623 | // process crash for instance) |
| 624 | jshort* cAudioData = NULL; |
| 625 | if (javaAudioData) { |
| 626 | cAudioData = (jshort *)env->GetShortArrayElements(javaAudioData, NULL); |
| 627 | if (cAudioData == NULL) { |
| 628 | ALOGE("Error retrieving source of audio data to play, can't play"); |
| 629 | return 0; // out of memory or no data to load |
| 630 | } |
| 631 | } else { |
| 632 | ALOGE("NULL java array of audio data to play, can't play"); |
| 633 | return 0; |
| 634 | } |
| 635 | jint written = writeToTrack(lpTrack, javaAudioFormat, (jbyte *)cAudioData, |
| 636 | offsetInShorts * sizeof(short), sizeInShorts * sizeof(short), |
| 637 | true /*blocking write, legacy behavior*/); |
| 638 | env->ReleaseShortArrayElements(javaAudioData, cAudioData, 0); |
| 639 | |
| 640 | if (written > 0) { |
| 641 | written /= sizeof(short); |
| 642 | } |
| 643 | //ALOGV("write wrote %d (tried %d) shorts in the native AudioTrack with offset %d", |
| 644 | // (int)written, (int)(sizeInShorts), (int)offsetInShorts); |
| 645 | |
Glenn Kasten | f7e0a370 | 2013-07-10 14:03:03 -0700 | [diff] [blame] | 646 | return written; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 647 | } |
| 648 | |
| 649 | |
| 650 | // ---------------------------------------------------------------------------- |
Glenn Kasten | 54955e3 | 2014-04-30 18:03:15 -0700 | [diff] [blame] | 651 | static jint android_media_AudioTrack_write_float(JNIEnv *env, jobject thiz, |
| 652 | jfloatArray javaAudioData, |
| 653 | jint offsetInFloats, jint sizeInFloats, |
| 654 | jint javaAudioFormat, |
| 655 | jboolean isWriteBlocking) { |
| 656 | |
| 657 | sp<AudioTrack> lpTrack = getAudioTrack(env, thiz); |
| 658 | if (lpTrack == NULL) { |
| 659 | jniThrowException(env, "java/lang/IllegalStateException", |
| 660 | "Unable to retrieve AudioTrack pointer for write()"); |
| 661 | return 0; |
| 662 | } |
| 663 | |
| 664 | jfloat* cAudioData = NULL; |
| 665 | if (javaAudioData) { |
| 666 | cAudioData = (jfloat *)env->GetFloatArrayElements(javaAudioData, NULL); |
| 667 | if (cAudioData == NULL) { |
| 668 | ALOGE("Error retrieving source of audio data to play, can't play"); |
| 669 | return 0; // out of memory or no data to load |
| 670 | } |
| 671 | } else { |
| 672 | ALOGE("NULL java array of audio data to play, can't play"); |
| 673 | return 0; |
| 674 | } |
| 675 | jint written = writeToTrack(lpTrack, javaAudioFormat, (jbyte *)cAudioData, |
| 676 | offsetInFloats * sizeof(float), sizeInFloats * sizeof(float), |
| 677 | isWriteBlocking == JNI_TRUE /* blocking */); |
| 678 | env->ReleaseFloatArrayElements(javaAudioData, cAudioData, 0); |
| 679 | |
| 680 | if (written > 0) { |
| 681 | written /= sizeof(float); |
| 682 | } |
| 683 | |
| 684 | return written; |
| 685 | } |
| 686 | |
| 687 | |
| 688 | // ---------------------------------------------------------------------------- |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 689 | static jint android_media_AudioTrack_get_native_frame_count(JNIEnv *env, jobject thiz) { |
Eric Laurent | 532bc1c | 2012-04-20 12:45:03 -0700 | [diff] [blame] | 690 | sp<AudioTrack> lpTrack = getAudioTrack(env, thiz); |
| 691 | if (lpTrack == NULL) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 692 | jniThrowException(env, "java/lang/IllegalStateException", |
| 693 | "Unable to retrieve AudioTrack pointer for frameCount()"); |
Eric Laurent | bc11a69 | 2014-05-16 12:19:25 -0700 | [diff] [blame] | 694 | return (jint)AUDIO_JAVA_ERROR; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 695 | } |
Eric Laurent | 532bc1c | 2012-04-20 12:45:03 -0700 | [diff] [blame] | 696 | |
| 697 | return lpTrack->frameCount(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 698 | } |
| 699 | |
| 700 | |
| 701 | // ---------------------------------------------------------------------------- |
Eric Laurent | 88e209d | 2009-07-07 07:10:45 -0700 | [diff] [blame] | 702 | static jint android_media_AudioTrack_set_playback_rate(JNIEnv *env, jobject thiz, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 703 | jint sampleRateInHz) { |
Eric Laurent | 532bc1c | 2012-04-20 12:45:03 -0700 | [diff] [blame] | 704 | sp<AudioTrack> lpTrack = getAudioTrack(env, thiz); |
| 705 | if (lpTrack == NULL) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 706 | jniThrowException(env, "java/lang/IllegalStateException", |
| 707 | "Unable to retrieve AudioTrack pointer for setSampleRate()"); |
Eric Laurent | bc11a69 | 2014-05-16 12:19:25 -0700 | [diff] [blame] | 708 | return (jint)AUDIO_JAVA_ERROR; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 709 | } |
Eric Laurent | bc11a69 | 2014-05-16 12:19:25 -0700 | [diff] [blame] | 710 | return nativeToJavaStatus(lpTrack->setSampleRate(sampleRateInHz)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 711 | } |
| 712 | |
| 713 | |
| 714 | // ---------------------------------------------------------------------------- |
| 715 | static jint android_media_AudioTrack_get_playback_rate(JNIEnv *env, jobject thiz) { |
Eric Laurent | 532bc1c | 2012-04-20 12:45:03 -0700 | [diff] [blame] | 716 | sp<AudioTrack> lpTrack = getAudioTrack(env, thiz); |
| 717 | if (lpTrack == NULL) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 718 | jniThrowException(env, "java/lang/IllegalStateException", |
| 719 | "Unable to retrieve AudioTrack pointer for getSampleRate()"); |
Eric Laurent | bc11a69 | 2014-05-16 12:19:25 -0700 | [diff] [blame] | 720 | return (jint)AUDIO_JAVA_ERROR; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 721 | } |
Eric Laurent | 532bc1c | 2012-04-20 12:45:03 -0700 | [diff] [blame] | 722 | return (jint) lpTrack->getSampleRate(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 723 | } |
| 724 | |
| 725 | |
| 726 | // ---------------------------------------------------------------------------- |
Glenn Kasten | 18db49a | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 727 | static jint android_media_AudioTrack_set_marker_pos(JNIEnv *env, jobject thiz, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 728 | jint markerPos) { |
Eric Laurent | 532bc1c | 2012-04-20 12:45:03 -0700 | [diff] [blame] | 729 | sp<AudioTrack> lpTrack = getAudioTrack(env, thiz); |
| 730 | if (lpTrack == NULL) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 731 | jniThrowException(env, "java/lang/IllegalStateException", |
| 732 | "Unable to retrieve AudioTrack pointer for setMarkerPosition()"); |
Eric Laurent | bc11a69 | 2014-05-16 12:19:25 -0700 | [diff] [blame] | 733 | return (jint)AUDIO_JAVA_ERROR; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 734 | } |
Eric Laurent | bc11a69 | 2014-05-16 12:19:25 -0700 | [diff] [blame] | 735 | return nativeToJavaStatus( lpTrack->setMarkerPosition(markerPos) ); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 736 | } |
| 737 | |
| 738 | |
| 739 | // ---------------------------------------------------------------------------- |
| 740 | static jint android_media_AudioTrack_get_marker_pos(JNIEnv *env, jobject thiz) { |
Eric Laurent | 532bc1c | 2012-04-20 12:45:03 -0700 | [diff] [blame] | 741 | sp<AudioTrack> lpTrack = getAudioTrack(env, thiz); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 742 | uint32_t markerPos = 0; |
Glenn Kasten | 18db49a | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 743 | |
Eric Laurent | 532bc1c | 2012-04-20 12:45:03 -0700 | [diff] [blame] | 744 | if (lpTrack == NULL) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 745 | jniThrowException(env, "java/lang/IllegalStateException", |
| 746 | "Unable to retrieve AudioTrack pointer for getMarkerPosition()"); |
Eric Laurent | bc11a69 | 2014-05-16 12:19:25 -0700 | [diff] [blame] | 747 | return (jint)AUDIO_JAVA_ERROR; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 748 | } |
Eric Laurent | 532bc1c | 2012-04-20 12:45:03 -0700 | [diff] [blame] | 749 | lpTrack->getMarkerPosition(&markerPos); |
| 750 | return (jint)markerPos; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 751 | } |
| 752 | |
| 753 | |
| 754 | // ---------------------------------------------------------------------------- |
| 755 | static jint android_media_AudioTrack_set_pos_update_period(JNIEnv *env, jobject thiz, |
| 756 | jint period) { |
Eric Laurent | 532bc1c | 2012-04-20 12:45:03 -0700 | [diff] [blame] | 757 | sp<AudioTrack> lpTrack = getAudioTrack(env, thiz); |
| 758 | if (lpTrack == NULL) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 759 | jniThrowException(env, "java/lang/IllegalStateException", |
| 760 | "Unable to retrieve AudioTrack pointer for setPositionUpdatePeriod()"); |
Eric Laurent | bc11a69 | 2014-05-16 12:19:25 -0700 | [diff] [blame] | 761 | return (jint)AUDIO_JAVA_ERROR; |
Glenn Kasten | 18db49a | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 762 | } |
Eric Laurent | bc11a69 | 2014-05-16 12:19:25 -0700 | [diff] [blame] | 763 | return nativeToJavaStatus( lpTrack->setPositionUpdatePeriod(period) ); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 764 | } |
| 765 | |
| 766 | |
| 767 | // ---------------------------------------------------------------------------- |
| 768 | static jint android_media_AudioTrack_get_pos_update_period(JNIEnv *env, jobject thiz) { |
Eric Laurent | 532bc1c | 2012-04-20 12:45:03 -0700 | [diff] [blame] | 769 | sp<AudioTrack> lpTrack = getAudioTrack(env, thiz); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 770 | uint32_t period = 0; |
Glenn Kasten | 18db49a | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 771 | |
Eric Laurent | 532bc1c | 2012-04-20 12:45:03 -0700 | [diff] [blame] | 772 | if (lpTrack == NULL) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 773 | jniThrowException(env, "java/lang/IllegalStateException", |
| 774 | "Unable to retrieve AudioTrack pointer for getPositionUpdatePeriod()"); |
Eric Laurent | bc11a69 | 2014-05-16 12:19:25 -0700 | [diff] [blame] | 775 | return (jint)AUDIO_JAVA_ERROR; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 776 | } |
Eric Laurent | 532bc1c | 2012-04-20 12:45:03 -0700 | [diff] [blame] | 777 | lpTrack->getPositionUpdatePeriod(&period); |
| 778 | return (jint)period; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 779 | } |
| 780 | |
| 781 | |
| 782 | // ---------------------------------------------------------------------------- |
Glenn Kasten | 18db49a | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 783 | static jint android_media_AudioTrack_set_position(JNIEnv *env, jobject thiz, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 784 | jint position) { |
Eric Laurent | 532bc1c | 2012-04-20 12:45:03 -0700 | [diff] [blame] | 785 | sp<AudioTrack> lpTrack = getAudioTrack(env, thiz); |
| 786 | if (lpTrack == NULL) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 787 | jniThrowException(env, "java/lang/IllegalStateException", |
| 788 | "Unable to retrieve AudioTrack pointer for setPosition()"); |
Eric Laurent | bc11a69 | 2014-05-16 12:19:25 -0700 | [diff] [blame] | 789 | return (jint)AUDIO_JAVA_ERROR; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 790 | } |
Eric Laurent | bc11a69 | 2014-05-16 12:19:25 -0700 | [diff] [blame] | 791 | return nativeToJavaStatus( lpTrack->setPosition(position) ); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 792 | } |
| 793 | |
| 794 | |
| 795 | // ---------------------------------------------------------------------------- |
| 796 | static jint android_media_AudioTrack_get_position(JNIEnv *env, jobject thiz) { |
Eric Laurent | 532bc1c | 2012-04-20 12:45:03 -0700 | [diff] [blame] | 797 | sp<AudioTrack> lpTrack = getAudioTrack(env, thiz); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 798 | uint32_t position = 0; |
Glenn Kasten | 18db49a | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 799 | |
Eric Laurent | 532bc1c | 2012-04-20 12:45:03 -0700 | [diff] [blame] | 800 | if (lpTrack == NULL) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 801 | jniThrowException(env, "java/lang/IllegalStateException", |
| 802 | "Unable to retrieve AudioTrack pointer for getPosition()"); |
Eric Laurent | bc11a69 | 2014-05-16 12:19:25 -0700 | [diff] [blame] | 803 | return (jint)AUDIO_JAVA_ERROR; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 804 | } |
Eric Laurent | 532bc1c | 2012-04-20 12:45:03 -0700 | [diff] [blame] | 805 | lpTrack->getPosition(&position); |
| 806 | return (jint)position; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 807 | } |
| 808 | |
| 809 | |
| 810 | // ---------------------------------------------------------------------------- |
Oliver Woodman | 61dcdf3 | 2013-06-26 12:43:36 +0100 | [diff] [blame] | 811 | static jint android_media_AudioTrack_get_latency(JNIEnv *env, jobject thiz) { |
| 812 | sp<AudioTrack> lpTrack = getAudioTrack(env, thiz); |
| 813 | |
| 814 | if (lpTrack == NULL) { |
| 815 | jniThrowException(env, "java/lang/IllegalStateException", |
| 816 | "Unable to retrieve AudioTrack pointer for latency()"); |
Eric Laurent | bc11a69 | 2014-05-16 12:19:25 -0700 | [diff] [blame] | 817 | return (jint)AUDIO_JAVA_ERROR; |
Oliver Woodman | 61dcdf3 | 2013-06-26 12:43:36 +0100 | [diff] [blame] | 818 | } |
| 819 | return (jint)lpTrack->latency(); |
| 820 | } |
| 821 | |
| 822 | |
| 823 | // ---------------------------------------------------------------------------- |
Glenn Kasten | 948c2e6 | 2013-09-04 13:51:29 -0700 | [diff] [blame] | 824 | static jint android_media_AudioTrack_get_timestamp(JNIEnv *env, jobject thiz, jlongArray jTimestamp) { |
| 825 | sp<AudioTrack> lpTrack = getAudioTrack(env, thiz); |
| 826 | |
| 827 | if (lpTrack == NULL) { |
| 828 | ALOGE("Unable to retrieve AudioTrack pointer for getTimestamp()"); |
Eric Laurent | bc11a69 | 2014-05-16 12:19:25 -0700 | [diff] [blame] | 829 | return (jint)AUDIO_JAVA_ERROR; |
Glenn Kasten | 948c2e6 | 2013-09-04 13:51:29 -0700 | [diff] [blame] | 830 | } |
| 831 | AudioTimestamp timestamp; |
| 832 | status_t status = lpTrack->getTimestamp(timestamp); |
| 833 | if (status == OK) { |
| 834 | jlong* nTimestamp = (jlong *) env->GetPrimitiveArrayCritical(jTimestamp, NULL); |
| 835 | if (nTimestamp == NULL) { |
| 836 | ALOGE("Unable to get array for getTimestamp()"); |
Eric Laurent | bc11a69 | 2014-05-16 12:19:25 -0700 | [diff] [blame] | 837 | return (jint)AUDIO_JAVA_ERROR; |
Glenn Kasten | 948c2e6 | 2013-09-04 13:51:29 -0700 | [diff] [blame] | 838 | } |
| 839 | nTimestamp[0] = (jlong) timestamp.mPosition; |
| 840 | nTimestamp[1] = (jlong) ((timestamp.mTime.tv_sec * 1000000000LL) + timestamp.mTime.tv_nsec); |
| 841 | env->ReleasePrimitiveArrayCritical(jTimestamp, nTimestamp, 0); |
| 842 | } |
Eric Laurent | bc11a69 | 2014-05-16 12:19:25 -0700 | [diff] [blame] | 843 | return (jint) nativeToJavaStatus(status); |
Glenn Kasten | 948c2e6 | 2013-09-04 13:51:29 -0700 | [diff] [blame] | 844 | } |
| 845 | |
| 846 | |
| 847 | // ---------------------------------------------------------------------------- |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 848 | static jint android_media_AudioTrack_set_loop(JNIEnv *env, jobject thiz, |
| 849 | jint loopStart, jint loopEnd, jint loopCount) { |
Eric Laurent | 532bc1c | 2012-04-20 12:45:03 -0700 | [diff] [blame] | 850 | sp<AudioTrack> lpTrack = getAudioTrack(env, thiz); |
| 851 | if (lpTrack == NULL) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 852 | jniThrowException(env, "java/lang/IllegalStateException", |
| 853 | "Unable to retrieve AudioTrack pointer for setLoop()"); |
Eric Laurent | bc11a69 | 2014-05-16 12:19:25 -0700 | [diff] [blame] | 854 | return (jint)AUDIO_JAVA_ERROR; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 855 | } |
Eric Laurent | bc11a69 | 2014-05-16 12:19:25 -0700 | [diff] [blame] | 856 | return nativeToJavaStatus( lpTrack->setLoop(loopStart, loopEnd, loopCount) ); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 857 | } |
| 858 | |
| 859 | |
| 860 | // ---------------------------------------------------------------------------- |
| 861 | static jint android_media_AudioTrack_reload(JNIEnv *env, jobject thiz) { |
Eric Laurent | 532bc1c | 2012-04-20 12:45:03 -0700 | [diff] [blame] | 862 | sp<AudioTrack> lpTrack = getAudioTrack(env, thiz); |
| 863 | if (lpTrack == NULL) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 864 | jniThrowException(env, "java/lang/IllegalStateException", |
| 865 | "Unable to retrieve AudioTrack pointer for reload()"); |
Eric Laurent | bc11a69 | 2014-05-16 12:19:25 -0700 | [diff] [blame] | 866 | return (jint)AUDIO_JAVA_ERROR; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 867 | } |
Eric Laurent | bc11a69 | 2014-05-16 12:19:25 -0700 | [diff] [blame] | 868 | return nativeToJavaStatus( lpTrack->reload() ); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 869 | } |
| 870 | |
| 871 | |
| 872 | // ---------------------------------------------------------------------------- |
| 873 | static jint android_media_AudioTrack_get_output_sample_rate(JNIEnv *env, jobject thiz, |
| 874 | jint javaStreamType) { |
Glenn Kasten | 85fbc87 | 2012-11-14 13:21:09 -0800 | [diff] [blame] | 875 | uint32_t afSamplingRate; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 876 | // convert the stream type from Java to native value |
Jean-Michel Trivi | eac8438 | 2014-02-04 14:50:40 -0800 | [diff] [blame] | 877 | // FIXME: code duplication with android_media_AudioTrack_setup() |
Dima Zavin | 24fc2fb | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 878 | audio_stream_type_t nativeStreamType; |
Glenn Kasten | 29a0909 | 2012-01-16 14:37:12 -0800 | [diff] [blame] | 879 | switch (javaStreamType) { |
| 880 | case AUDIO_STREAM_VOICE_CALL: |
| 881 | case AUDIO_STREAM_SYSTEM: |
| 882 | case AUDIO_STREAM_RING: |
| 883 | case AUDIO_STREAM_MUSIC: |
| 884 | case AUDIO_STREAM_ALARM: |
| 885 | case AUDIO_STREAM_NOTIFICATION: |
| 886 | case AUDIO_STREAM_BLUETOOTH_SCO: |
| 887 | case AUDIO_STREAM_DTMF: |
| 888 | nativeStreamType = (audio_stream_type_t) javaStreamType; |
| 889 | break; |
| 890 | default: |
Dima Zavin | 24fc2fb | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 891 | nativeStreamType = AUDIO_STREAM_DEFAULT; |
Glenn Kasten | 29a0909 | 2012-01-16 14:37:12 -0800 | [diff] [blame] | 892 | break; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 893 | } |
| 894 | |
Glenn Kasten | 8f81d08 | 2012-11-28 14:37:48 -0800 | [diff] [blame] | 895 | status_t status = AudioSystem::getOutputSamplingRate(&afSamplingRate, nativeStreamType); |
| 896 | if (status != NO_ERROR) { |
| 897 | ALOGE("Error %d in AudioSystem::getOutputSamplingRate() for stream type %d " |
| 898 | "in AudioTrack JNI", status, nativeStreamType); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 899 | return DEFAULT_OUTPUT_SAMPLE_RATE; |
| 900 | } else { |
| 901 | return afSamplingRate; |
| 902 | } |
| 903 | } |
| 904 | |
| 905 | |
| 906 | // ---------------------------------------------------------------------------- |
| 907 | // returns the minimum required size for the successful creation of a streaming AudioTrack |
| 908 | // returns -1 if there was an error querying the hardware. |
| 909 | static jint android_media_AudioTrack_get_min_buff_size(JNIEnv *env, jobject thiz, |
Glenn Kasten | 5b8fd44 | 2013-11-14 09:44:14 -0800 | [diff] [blame] | 910 | jint sampleRateInHertz, jint channelCount, jint audioFormat) { |
Chia-chi Yeh | c330807 | 2010-08-19 17:14:36 +0800 | [diff] [blame] | 911 | |
Glenn Kasten | 659a971 | 2014-01-08 11:38:33 -0800 | [diff] [blame] | 912 | size_t frameCount; |
| 913 | const status_t status = AudioTrack::getMinFrameCount(&frameCount, AUDIO_STREAM_DEFAULT, |
| 914 | sampleRateInHertz); |
| 915 | if (status != NO_ERROR) { |
| 916 | ALOGE("AudioTrack::getMinFrameCount() for sample rate %d failed with status %d", |
| 917 | sampleRateInHertz, status); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 918 | return -1; |
| 919 | } |
Glenn Kasten | fe834d3 | 2014-01-08 14:49:08 -0800 | [diff] [blame] | 920 | const audio_format_t format = audioFormatToNative(audioFormat); |
Eric Laurent | ff0d9f0 | 2014-06-09 17:23:02 -0700 | [diff] [blame] | 921 | if (audio_is_linear_pcm(format)) { |
| 922 | const size_t bytesPerSample = audio_bytes_per_sample(format); |
| 923 | return frameCount * channelCount * bytesPerSample; |
| 924 | } else { |
| 925 | return frameCount; |
| 926 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 927 | } |
| 928 | |
Eric Laurent | 7070b36 | 2010-07-16 07:43:46 -0700 | [diff] [blame] | 929 | // ---------------------------------------------------------------------------- |
Glenn Kasten | 3009f0b | 2014-03-28 16:02:26 -0700 | [diff] [blame] | 930 | static jint |
Eric Laurent | 7070b36 | 2010-07-16 07:43:46 -0700 | [diff] [blame] | 931 | android_media_AudioTrack_setAuxEffectSendLevel(JNIEnv *env, jobject thiz, jfloat level ) |
| 932 | { |
Eric Laurent | 532bc1c | 2012-04-20 12:45:03 -0700 | [diff] [blame] | 933 | sp<AudioTrack> lpTrack = getAudioTrack(env, thiz); |
Eric Laurent | 7070b36 | 2010-07-16 07:43:46 -0700 | [diff] [blame] | 934 | if (lpTrack == NULL ) { |
| 935 | jniThrowException(env, "java/lang/IllegalStateException", |
| 936 | "Unable to retrieve AudioTrack pointer for setAuxEffectSendLevel()"); |
Glenn Kasten | 3009f0b | 2014-03-28 16:02:26 -0700 | [diff] [blame] | 937 | return -1; |
Eric Laurent | 7070b36 | 2010-07-16 07:43:46 -0700 | [diff] [blame] | 938 | } |
| 939 | |
Glenn Kasten | 3009f0b | 2014-03-28 16:02:26 -0700 | [diff] [blame] | 940 | status_t status = lpTrack->setAuxEffectSendLevel(level); |
| 941 | if (status != NO_ERROR) { |
| 942 | ALOGE("AudioTrack::setAuxEffectSendLevel() for level %g failed with status %d", |
| 943 | level, status); |
| 944 | } |
| 945 | return (jint) status; |
Eric Laurent | 7070b36 | 2010-07-16 07:43:46 -0700 | [diff] [blame] | 946 | } |
| 947 | |
| 948 | // ---------------------------------------------------------------------------- |
| 949 | static jint android_media_AudioTrack_attachAuxEffect(JNIEnv *env, jobject thiz, |
| 950 | jint effectId) { |
Eric Laurent | 532bc1c | 2012-04-20 12:45:03 -0700 | [diff] [blame] | 951 | sp<AudioTrack> lpTrack = getAudioTrack(env, thiz); |
| 952 | if (lpTrack == NULL) { |
Eric Laurent | 7070b36 | 2010-07-16 07:43:46 -0700 | [diff] [blame] | 953 | jniThrowException(env, "java/lang/IllegalStateException", |
| 954 | "Unable to retrieve AudioTrack pointer for attachAuxEffect()"); |
Eric Laurent | bc11a69 | 2014-05-16 12:19:25 -0700 | [diff] [blame] | 955 | return (jint)AUDIO_JAVA_ERROR; |
Eric Laurent | 7070b36 | 2010-07-16 07:43:46 -0700 | [diff] [blame] | 956 | } |
Eric Laurent | bc11a69 | 2014-05-16 12:19:25 -0700 | [diff] [blame] | 957 | return nativeToJavaStatus( lpTrack->attachAuxEffect(effectId) ); |
Eric Laurent | 7070b36 | 2010-07-16 07:43:46 -0700 | [diff] [blame] | 958 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 959 | |
| 960 | // ---------------------------------------------------------------------------- |
| 961 | // ---------------------------------------------------------------------------- |
| 962 | static JNINativeMethod gMethods[] = { |
| 963 | // name, signature, funcPtr |
| 964 | {"native_start", "()V", (void *)android_media_AudioTrack_start}, |
| 965 | {"native_stop", "()V", (void *)android_media_AudioTrack_stop}, |
| 966 | {"native_pause", "()V", (void *)android_media_AudioTrack_pause}, |
| 967 | {"native_flush", "()V", (void *)android_media_AudioTrack_flush}, |
Jean-Michel Trivi | a1d80e3 | 2014-06-18 08:18:41 -0700 | [diff] [blame] | 968 | {"native_setup", "(Ljava/lang/Object;Ljava/lang/Object;IIIII[I)I", |
Jean-Michel Trivi | eac8438 | 2014-02-04 14:50:40 -0800 | [diff] [blame] | 969 | (void *)android_media_AudioTrack_setup}, |
| 970 | {"native_finalize", "()V", (void *)android_media_AudioTrack_finalize}, |
| 971 | {"native_release", "()V", (void *)android_media_AudioTrack_release}, |
Jean-Michel Trivi | 7ca0452 | 2014-02-07 09:39:34 -0800 | [diff] [blame] | 972 | {"native_write_byte", "([BIIIZ)I",(void *)android_media_AudioTrack_write_byte}, |
| 973 | {"native_write_native_bytes", |
Jean-Michel Trivi | 5d21f67 | 2014-03-20 17:02:31 -0700 | [diff] [blame] | 974 | "(Ljava/lang/Object;IIIZ)I", |
Jean-Michel Trivi | 7ca0452 | 2014-02-07 09:39:34 -0800 | [diff] [blame] | 975 | (void *)android_media_AudioTrack_write_native_bytes}, |
Jean-Michel Trivi | eac8438 | 2014-02-04 14:50:40 -0800 | [diff] [blame] | 976 | {"native_write_short", "([SIII)I", (void *)android_media_AudioTrack_write_short}, |
Glenn Kasten | 54955e3 | 2014-04-30 18:03:15 -0700 | [diff] [blame] | 977 | {"native_write_float", "([FIIIZ)I",(void *)android_media_AudioTrack_write_float}, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 978 | {"native_setVolume", "(FF)V", (void *)android_media_AudioTrack_set_volume}, |
| 979 | {"native_get_native_frame_count", |
| 980 | "()I", (void *)android_media_AudioTrack_get_native_frame_count}, |
| 981 | {"native_set_playback_rate", |
Eric Laurent | 88e209d | 2009-07-07 07:10:45 -0700 | [diff] [blame] | 982 | "(I)I", (void *)android_media_AudioTrack_set_playback_rate}, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 983 | {"native_get_playback_rate", |
| 984 | "()I", (void *)android_media_AudioTrack_get_playback_rate}, |
| 985 | {"native_set_marker_pos","(I)I", (void *)android_media_AudioTrack_set_marker_pos}, |
| 986 | {"native_get_marker_pos","()I", (void *)android_media_AudioTrack_get_marker_pos}, |
| 987 | {"native_set_pos_update_period", |
| 988 | "(I)I", (void *)android_media_AudioTrack_set_pos_update_period}, |
| 989 | {"native_get_pos_update_period", |
| 990 | "()I", (void *)android_media_AudioTrack_get_pos_update_period}, |
| 991 | {"native_set_position", "(I)I", (void *)android_media_AudioTrack_set_position}, |
| 992 | {"native_get_position", "()I", (void *)android_media_AudioTrack_get_position}, |
Oliver Woodman | 61dcdf3 | 2013-06-26 12:43:36 +0100 | [diff] [blame] | 993 | {"native_get_latency", "()I", (void *)android_media_AudioTrack_get_latency}, |
Glenn Kasten | 948c2e6 | 2013-09-04 13:51:29 -0700 | [diff] [blame] | 994 | {"native_get_timestamp", "([J)I", (void *)android_media_AudioTrack_get_timestamp}, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 995 | {"native_set_loop", "(III)I", (void *)android_media_AudioTrack_set_loop}, |
| 996 | {"native_reload_static", "()I", (void *)android_media_AudioTrack_reload}, |
| 997 | {"native_get_output_sample_rate", |
| 998 | "(I)I", (void *)android_media_AudioTrack_get_output_sample_rate}, |
| 999 | {"native_get_min_buff_size", |
| 1000 | "(III)I", (void *)android_media_AudioTrack_get_min_buff_size}, |
Eric Laurent | 7070b36 | 2010-07-16 07:43:46 -0700 | [diff] [blame] | 1001 | {"native_setAuxEffectSendLevel", |
Glenn Kasten | 3009f0b | 2014-03-28 16:02:26 -0700 | [diff] [blame] | 1002 | "(F)I", (void *)android_media_AudioTrack_setAuxEffectSendLevel}, |
Eric Laurent | 7070b36 | 2010-07-16 07:43:46 -0700 | [diff] [blame] | 1003 | {"native_attachAuxEffect", |
| 1004 | "(I)I", (void *)android_media_AudioTrack_attachAuxEffect}, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1005 | }; |
| 1006 | |
| 1007 | |
| 1008 | // field names found in android/media/AudioTrack.java |
| 1009 | #define JAVA_POSTEVENT_CALLBACK_NAME "postEventFromNative" |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1010 | #define JAVA_NATIVETRACKINJAVAOBJ_FIELD_NAME "mNativeTrackInJavaObj" |
| 1011 | #define JAVA_JNIDATA_FIELD_NAME "mJniData" |
Jean-Michel Trivi | a1d80e3 | 2014-06-18 08:18:41 -0700 | [diff] [blame] | 1012 | #define JAVA_STREAMTYPE_FIELD_NAME "mStreamType" |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1013 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1014 | // ---------------------------------------------------------------------------- |
| 1015 | // preconditions: |
| 1016 | // theClass is valid |
| 1017 | bool android_media_getIntConstantFromClass(JNIEnv* pEnv, jclass theClass, const char* className, |
| 1018 | const char* constName, int* constVal) { |
| 1019 | jfieldID javaConst = NULL; |
| 1020 | javaConst = pEnv->GetStaticFieldID(theClass, constName, "I"); |
| 1021 | if (javaConst != NULL) { |
| 1022 | *constVal = pEnv->GetStaticIntField(theClass, javaConst); |
| 1023 | return true; |
| 1024 | } else { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 1025 | ALOGE("Can't find %s.%s", className, constName); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1026 | return false; |
| 1027 | } |
| 1028 | } |
| 1029 | |
| 1030 | |
| 1031 | // ---------------------------------------------------------------------------- |
| 1032 | int register_android_media_AudioTrack(JNIEnv *env) |
| 1033 | { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1034 | javaAudioTrackFields.nativeTrackInJavaObj = NULL; |
| 1035 | javaAudioTrackFields.postNativeEventInJava = NULL; |
| 1036 | |
| 1037 | // Get the AudioTrack class |
Andreas Gampe | ed6b9df | 2014-11-20 22:02:20 -0800 | [diff] [blame] | 1038 | jclass audioTrackClass = FindClassOrDie(env, kClassPathName); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1039 | |
| 1040 | // Get the postEvent method |
Andreas Gampe | ed6b9df | 2014-11-20 22:02:20 -0800 | [diff] [blame] | 1041 | javaAudioTrackFields.postNativeEventInJava = GetStaticMethodIDOrDie(env, |
| 1042 | audioTrackClass, JAVA_POSTEVENT_CALLBACK_NAME, |
| 1043 | "(Ljava/lang/Object;IIILjava/lang/Object;)V"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1044 | |
| 1045 | // Get the variables fields |
| 1046 | // nativeTrackInJavaObj |
Andreas Gampe | ed6b9df | 2014-11-20 22:02:20 -0800 | [diff] [blame] | 1047 | javaAudioTrackFields.nativeTrackInJavaObj = GetFieldIDOrDie(env, |
| 1048 | audioTrackClass, JAVA_NATIVETRACKINJAVAOBJ_FIELD_NAME, "J"); |
Jean-Michel Trivi | a1d80e3 | 2014-06-18 08:18:41 -0700 | [diff] [blame] | 1049 | // jniData |
Andreas Gampe | ed6b9df | 2014-11-20 22:02:20 -0800 | [diff] [blame] | 1050 | javaAudioTrackFields.jniData = GetFieldIDOrDie(env, |
| 1051 | audioTrackClass, JAVA_JNIDATA_FIELD_NAME, "J"); |
Jean-Michel Trivi | a1d80e3 | 2014-06-18 08:18:41 -0700 | [diff] [blame] | 1052 | // fieldStreamType |
Andreas Gampe | ed6b9df | 2014-11-20 22:02:20 -0800 | [diff] [blame] | 1053 | javaAudioTrackFields.fieldStreamType = GetFieldIDOrDie(env, |
| 1054 | audioTrackClass, JAVA_STREAMTYPE_FIELD_NAME, "I"); |
Jean-Michel Trivi | a1d80e3 | 2014-06-18 08:18:41 -0700 | [diff] [blame] | 1055 | |
| 1056 | // Get the AudioAttributes class and fields |
Andreas Gampe | ed6b9df | 2014-11-20 22:02:20 -0800 | [diff] [blame] | 1057 | jclass audioAttrClass = FindClassOrDie(env, kAudioAttributesClassPathName); |
| 1058 | javaAudioAttrFields.fieldUsage = GetFieldIDOrDie(env, audioAttrClass, "mUsage", "I"); |
| 1059 | javaAudioAttrFields.fieldContentType = GetFieldIDOrDie(env, |
| 1060 | audioAttrClass, "mContentType", "I"); |
| 1061 | javaAudioAttrFields.fieldFlags = GetFieldIDOrDie(env, audioAttrClass, "mFlags", "I"); |
| 1062 | javaAudioAttrFields.fieldFormattedTags = GetFieldIDOrDie(env, |
| 1063 | audioAttrClass, "mFormattedTags", "Ljava/lang/String;"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1064 | |
Andreas Gampe | ed6b9df | 2014-11-20 22:02:20 -0800 | [diff] [blame] | 1065 | return RegisterMethodsOrDie(env, kClassPathName, gMethods, NELEM(gMethods)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1066 | } |
| 1067 | |
| 1068 | |
| 1069 | // ---------------------------------------------------------------------------- |