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" |
Wei Jia | 2d61e2b | 2015-05-08 15:23:28 -0700 | [diff] [blame] | 38 | #include "android_media_PlaybackParams.h" |
Eric Laurent | 4bcdba8 | 2015-05-01 11:37:49 -0700 | [diff] [blame] | 39 | #include "android_media_DeviceCallback.h" |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 40 | |
| 41 | // ---------------------------------------------------------------------------- |
| 42 | |
| 43 | using namespace android; |
| 44 | |
| 45 | // ---------------------------------------------------------------------------- |
| 46 | static const char* const kClassPathName = "android/media/AudioTrack"; |
Jean-Michel Trivi | a1d80e3 | 2014-06-18 08:18:41 -0700 | [diff] [blame] | 47 | static const char* const kAudioAttributesClassPathName = "android/media/AudioAttributes"; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 48 | |
Jean-Michel Trivi | a1d80e3 | 2014-06-18 08:18:41 -0700 | [diff] [blame] | 49 | struct audio_track_fields_t { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 50 | // these fields provide access from C++ to the... |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 51 | jmethodID postNativeEventInJava; //... event post callback method |
Eric Laurent | 83b3685 | 2009-07-28 07:49:22 -0700 | [diff] [blame] | 52 | jfieldID nativeTrackInJavaObj; // stores in Java the native AudioTrack object |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 53 | 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] | 54 | jfieldID fieldStreamType; // ... mStreamType field in the AudioTrack Java object |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 55 | }; |
Jean-Michel Trivi | a1d80e3 | 2014-06-18 08:18:41 -0700 | [diff] [blame] | 56 | struct audio_attributes_fields_t { |
Jean-Michel Trivi | 701d6ff | 2014-07-16 07:51:22 -0700 | [diff] [blame] | 57 | jfieldID fieldUsage; // AudioAttributes.mUsage |
| 58 | jfieldID fieldContentType; // AudioAttributes.mContentType |
| 59 | jfieldID fieldFlags; // AudioAttributes.mFlags |
| 60 | jfieldID fieldFormattedTags;// AudioAttributes.mFormattedTags |
Jean-Michel Trivi | a1d80e3 | 2014-06-18 08:18:41 -0700 | [diff] [blame] | 61 | }; |
| 62 | static audio_track_fields_t javaAudioTrackFields; |
| 63 | static audio_attributes_fields_t javaAudioAttrFields; |
Wei Jia | 2d61e2b | 2015-05-08 15:23:28 -0700 | [diff] [blame] | 64 | static PlaybackParams::fields_t gPlaybackParamsFields; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 65 | |
| 66 | struct audiotrack_callback_cookie { |
| 67 | jclass audioTrack_class; |
| 68 | jobject audioTrack_ref; |
Eric Laurent | 532bc1c | 2012-04-20 12:45:03 -0700 | [diff] [blame] | 69 | bool busy; |
| 70 | Condition cond; |
| 71 | }; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 72 | |
Glenn Kasten | 3d301cb | 2012-01-16 14:46:54 -0800 | [diff] [blame] | 73 | // keep these values in sync with AudioTrack.java |
| 74 | #define MODE_STATIC 0 |
| 75 | #define MODE_STREAM 1 |
| 76 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 77 | // ---------------------------------------------------------------------------- |
| 78 | class AudioTrackJniStorage { |
| 79 | public: |
| 80 | sp<MemoryHeapBase> mMemHeap; |
| 81 | sp<MemoryBase> mMemBase; |
| 82 | audiotrack_callback_cookie mCallbackData; |
Eric Laurent | 4bcdba8 | 2015-05-01 11:37:49 -0700 | [diff] [blame] | 83 | sp<JNIDeviceCallback> mDeviceCallback; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 84 | |
| 85 | AudioTrackJniStorage() { |
Jean-Michel Trivi | 8a14968 | 2009-07-15 18:31:11 -0700 | [diff] [blame] | 86 | mCallbackData.audioTrack_class = 0; |
| 87 | mCallbackData.audioTrack_ref = 0; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | ~AudioTrackJniStorage() { |
| 91 | mMemBase.clear(); |
| 92 | mMemHeap.clear(); |
| 93 | } |
| 94 | |
| 95 | bool allocSharedMem(int sizeInBytes) { |
| 96 | mMemHeap = new MemoryHeapBase(sizeInBytes, 0, "AudioTrack Heap Base"); |
| 97 | if (mMemHeap->getHeapID() < 0) { |
| 98 | return false; |
| 99 | } |
| 100 | mMemBase = new MemoryBase(mMemHeap, 0, sizeInBytes); |
| 101 | return true; |
| 102 | } |
| 103 | }; |
| 104 | |
Eric Laurent | 532bc1c | 2012-04-20 12:45:03 -0700 | [diff] [blame] | 105 | static Mutex sLock; |
| 106 | static SortedVector <audiotrack_callback_cookie *> sAudioTrackCallBackCookies; |
| 107 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 108 | // ---------------------------------------------------------------------------- |
| 109 | #define DEFAULT_OUTPUT_SAMPLE_RATE 44100 |
| 110 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 111 | #define AUDIOTRACK_ERROR_SETUP_AUDIOSYSTEM -16 |
Glenn Kasten | fb2ab9e | 2011-12-12 09:05:55 -0800 | [diff] [blame] | 112 | #define AUDIOTRACK_ERROR_SETUP_INVALIDCHANNELMASK -17 |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 113 | #define AUDIOTRACK_ERROR_SETUP_INVALIDFORMAT -18 |
| 114 | #define AUDIOTRACK_ERROR_SETUP_INVALIDSTREAMTYPE -19 |
| 115 | #define AUDIOTRACK_ERROR_SETUP_NATIVEINITFAILED -20 |
| 116 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 117 | // ---------------------------------------------------------------------------- |
Glenn Kasten | e46a86f | 2011-06-01 15:20:35 -0700 | [diff] [blame] | 118 | static void audioCallback(int event, void* user, void *info) { |
Eric Laurent | 532bc1c | 2012-04-20 12:45:03 -0700 | [diff] [blame] | 119 | |
| 120 | audiotrack_callback_cookie *callbackInfo = (audiotrack_callback_cookie *)user; |
| 121 | { |
| 122 | Mutex::Autolock l(sLock); |
| 123 | if (sAudioTrackCallBackCookies.indexOf(callbackInfo) < 0) { |
| 124 | return; |
| 125 | } |
| 126 | callbackInfo->busy = true; |
| 127 | } |
| 128 | |
Glenn Kasten | 5b1576c | 2013-07-18 16:58:19 -0700 | [diff] [blame] | 129 | switch (event) { |
| 130 | case AudioTrack::EVENT_MARKER: { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 131 | JNIEnv *env = AndroidRuntime::getJNIEnv(); |
Glenn Kasten | a667ff3 | 2013-07-22 07:36:34 -0700 | [diff] [blame] | 132 | if (user != NULL && env != NULL) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 133 | env->CallStaticVoidMethod( |
Glenn Kasten | 18db49a | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 134 | callbackInfo->audioTrack_class, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 135 | javaAudioTrackFields.postNativeEventInJava, |
| 136 | callbackInfo->audioTrack_ref, event, 0,0, NULL); |
| 137 | if (env->ExceptionCheck()) { |
| 138 | env->ExceptionDescribe(); |
| 139 | env->ExceptionClear(); |
| 140 | } |
| 141 | } |
Glenn Kasten | 5b1576c | 2013-07-18 16:58:19 -0700 | [diff] [blame] | 142 | } break; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 143 | |
Glenn Kasten | 5b1576c | 2013-07-18 16:58:19 -0700 | [diff] [blame] | 144 | case AudioTrack::EVENT_NEW_POS: { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 145 | JNIEnv *env = AndroidRuntime::getJNIEnv(); |
Glenn Kasten | a667ff3 | 2013-07-22 07:36:34 -0700 | [diff] [blame] | 146 | if (user != NULL && env != NULL) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 147 | env->CallStaticVoidMethod( |
Glenn Kasten | 18db49a | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 148 | callbackInfo->audioTrack_class, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 149 | javaAudioTrackFields.postNativeEventInJava, |
| 150 | callbackInfo->audioTrack_ref, event, 0,0, NULL); |
| 151 | if (env->ExceptionCheck()) { |
| 152 | env->ExceptionDescribe(); |
| 153 | env->ExceptionClear(); |
| 154 | } |
| 155 | } |
Glenn Kasten | 5b1576c | 2013-07-18 16:58:19 -0700 | [diff] [blame] | 156 | } break; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 157 | } |
Glenn Kasten | 5b1576c | 2013-07-18 16:58:19 -0700 | [diff] [blame] | 158 | |
Eric Laurent | 532bc1c | 2012-04-20 12:45:03 -0700 | [diff] [blame] | 159 | { |
| 160 | Mutex::Autolock l(sLock); |
| 161 | callbackInfo->busy = false; |
| 162 | callbackInfo->cond.broadcast(); |
| 163 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 164 | } |
| 165 | |
| 166 | |
| 167 | // ---------------------------------------------------------------------------- |
Eric Laurent | 532bc1c | 2012-04-20 12:45:03 -0700 | [diff] [blame] | 168 | static sp<AudioTrack> getAudioTrack(JNIEnv* env, jobject thiz) |
| 169 | { |
| 170 | Mutex::Autolock l(sLock); |
| 171 | AudioTrack* const at = |
Ashok Bhat | 075e9a1 | 2014-01-06 13:45:09 +0000 | [diff] [blame] | 172 | (AudioTrack*)env->GetLongField(thiz, javaAudioTrackFields.nativeTrackInJavaObj); |
Eric Laurent | 532bc1c | 2012-04-20 12:45:03 -0700 | [diff] [blame] | 173 | return sp<AudioTrack>(at); |
| 174 | } |
| 175 | |
| 176 | static sp<AudioTrack> setAudioTrack(JNIEnv* env, jobject thiz, const sp<AudioTrack>& at) |
| 177 | { |
| 178 | Mutex::Autolock l(sLock); |
| 179 | sp<AudioTrack> old = |
Ashok Bhat | 075e9a1 | 2014-01-06 13:45:09 +0000 | [diff] [blame] | 180 | (AudioTrack*)env->GetLongField(thiz, javaAudioTrackFields.nativeTrackInJavaObj); |
Eric Laurent | 532bc1c | 2012-04-20 12:45:03 -0700 | [diff] [blame] | 181 | if (at.get()) { |
Mathias Agopian | b1d90c8 | 2013-03-06 17:45:42 -0800 | [diff] [blame] | 182 | at->incStrong((void*)setAudioTrack); |
Eric Laurent | 532bc1c | 2012-04-20 12:45:03 -0700 | [diff] [blame] | 183 | } |
| 184 | if (old != 0) { |
Mathias Agopian | b1d90c8 | 2013-03-06 17:45:42 -0800 | [diff] [blame] | 185 | old->decStrong((void*)setAudioTrack); |
Eric Laurent | 532bc1c | 2012-04-20 12:45:03 -0700 | [diff] [blame] | 186 | } |
Ashok Bhat | 075e9a1 | 2014-01-06 13:45:09 +0000 | [diff] [blame] | 187 | env->SetLongField(thiz, javaAudioTrackFields.nativeTrackInJavaObj, (jlong)at.get()); |
Eric Laurent | 532bc1c | 2012-04-20 12:45:03 -0700 | [diff] [blame] | 188 | return old; |
| 189 | } |
Wei Jia | 071a8b7 | 2015-03-09 16:38:25 -0700 | [diff] [blame] | 190 | |
| 191 | // ---------------------------------------------------------------------------- |
| 192 | sp<AudioTrack> android_media_AudioTrack_getAudioTrack(JNIEnv* env, jobject audioTrackObj) { |
| 193 | return getAudioTrack(env, audioTrackObj); |
| 194 | } |
| 195 | |
Andy Hung | 30d7943 | 2015-04-23 19:28:38 -0700 | [diff] [blame] | 196 | // This function converts Java channel masks to a native channel mask. |
| 197 | // validity should be checked with audio_is_output_channel(). |
| 198 | static inline audio_channel_mask_t nativeChannelMaskFromJavaChannelMasks( |
| 199 | jint channelPositionMask, jint channelIndexMask) |
| 200 | { |
| 201 | if (channelIndexMask != 0) { // channel index mask takes priority |
| 202 | // To convert to a native channel mask, the Java channel index mask |
| 203 | // requires adding the index representation. |
| 204 | return audio_channel_mask_from_representation_and_bits( |
| 205 | AUDIO_CHANNEL_REPRESENTATION_INDEX, |
| 206 | channelIndexMask); |
| 207 | } |
| 208 | // To convert to a native channel mask, the Java channel position mask |
| 209 | // requires a shift by 2 to skip the two deprecated channel |
| 210 | // configurations "default" and "mono". |
| 211 | return (audio_channel_mask_t)(channelPositionMask >> 2); |
| 212 | } |
| 213 | |
Eric Laurent | 532bc1c | 2012-04-20 12:45:03 -0700 | [diff] [blame] | 214 | // ---------------------------------------------------------------------------- |
Ashok Bhat | 075e9a1 | 2014-01-06 13:45:09 +0000 | [diff] [blame] | 215 | static jint |
Paul McLean | 9b09e53 | 2016-01-26 14:43:35 -0700 | [diff] [blame] | 216 | android_media_AudioTrack_setup(JNIEnv *env, jobject thiz, jobject weak_this, jobject jaa, |
Glenn Kasten | 1cbf9b3 | 2016-02-02 12:04:09 -0800 | [diff] [blame] | 217 | jintArray jSampleRate, jint channelPositionMask, jint channelIndexMask, |
Paul McLean | 9b09e53 | 2016-01-26 14:43:35 -0700 | [diff] [blame] | 218 | jint audioFormat, jint buffSizeInBytes, jint memoryMode, jintArray jSession, |
| 219 | jlong nativeAudioTrack) { |
Jean-Michel Trivi | a1d80e3 | 2014-06-18 08:18:41 -0700 | [diff] [blame] | 220 | |
Paul McLean | 9b09e53 | 2016-01-26 14:43:35 -0700 | [diff] [blame] | 221 | ALOGV("sampleRates=%p, channel mask=%x, index mask=%x, audioFormat(Java)=%d, buffSize=%d" |
| 222 | "nativeAudioTrack=0x%llX", |
| 223 | jSampleRate, channelPositionMask, channelIndexMask, audioFormat, buffSizeInBytes, |
| 224 | nativeAudioTrack); |
Glenn Kasten | 1cbf9b3 | 2016-02-02 12:04:09 -0800 | [diff] [blame] | 225 | |
Paul McLean | 9b09e53 | 2016-01-26 14:43:35 -0700 | [diff] [blame] | 226 | sp<AudioTrack> lpTrack = 0; |
Eric Laurent | 2fb43ef | 2010-09-24 12:03:36 -0700 | [diff] [blame] | 227 | |
| 228 | if (jSession == NULL) { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 229 | ALOGE("Error creating AudioTrack: invalid session ID pointer"); |
Eric Laurent | bc11a69 | 2014-05-16 12:19:25 -0700 | [diff] [blame] | 230 | return (jint) AUDIO_JAVA_ERROR; |
Eric Laurent | 619346f | 2010-06-21 09:27:30 -0700 | [diff] [blame] | 231 | } |
| 232 | |
Eric Laurent | 2fb43ef | 2010-09-24 12:03:36 -0700 | [diff] [blame] | 233 | jint* nSession = (jint *) env->GetPrimitiveArrayCritical(jSession, NULL); |
| 234 | if (nSession == NULL) { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 235 | ALOGE("Error creating AudioTrack: Error retrieving session id pointer"); |
Eric Laurent | bc11a69 | 2014-05-16 12:19:25 -0700 | [diff] [blame] | 236 | return (jint) AUDIO_JAVA_ERROR; |
Eric Laurent | 2fb43ef | 2010-09-24 12:03:36 -0700 | [diff] [blame] | 237 | } |
Glenn Kasten | 33b8404 | 2016-03-08 12:02:55 -0800 | [diff] [blame] | 238 | audio_session_t sessionId = (audio_session_t) nSession[0]; |
Eric Laurent | 2fb43ef | 2010-09-24 12:03:36 -0700 | [diff] [blame] | 239 | env->ReleasePrimitiveArrayCritical(jSession, nSession, 0); |
| 240 | nSession = NULL; |
| 241 | |
Paul McLean | 9b09e53 | 2016-01-26 14:43:35 -0700 | [diff] [blame] | 242 | AudioTrackJniStorage* lpJniStorage = NULL; |
Glenn Kasten | 18db49a | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 243 | |
Jean-Michel Trivi | a1d80e3 | 2014-06-18 08:18:41 -0700 | [diff] [blame] | 244 | audio_attributes_t *paa = NULL; |
Jean-Michel Trivi | a1d80e3 | 2014-06-18 08:18:41 -0700 | [diff] [blame] | 245 | |
Paul McLean | 9b09e53 | 2016-01-26 14:43:35 -0700 | [diff] [blame] | 246 | jclass clazz = env->GetObjectClass(thiz); |
| 247 | if (clazz == NULL) { |
| 248 | ALOGE("Can't find %s when setting up callback.", kClassPathName); |
| 249 | return (jint) AUDIOTRACK_ERROR_SETUP_NATIVEINITFAILED; |
| 250 | } |
Jean-Michel Trivi | a1d80e3 | 2014-06-18 08:18:41 -0700 | [diff] [blame] | 251 | |
Paul McLean | 9b09e53 | 2016-01-26 14:43:35 -0700 | [diff] [blame] | 252 | // if we pass in an existing *Native* AudioTrack, we don't need to create/initialize one. |
| 253 | if (nativeAudioTrack == 0) { |
| 254 | if (jaa == 0) { |
| 255 | ALOGE("Error creating AudioTrack: invalid audio attributes"); |
| 256 | return (jint) AUDIO_JAVA_ERROR; |
| 257 | } |
Eric Laurent | 532bc1c | 2012-04-20 12:45:03 -0700 | [diff] [blame] | 258 | |
Paul McLean | 9b09e53 | 2016-01-26 14:43:35 -0700 | [diff] [blame] | 259 | if (jSampleRate == 0) { |
| 260 | ALOGE("Error creating AudioTrack: invalid sample rates"); |
| 261 | return (jint) AUDIO_JAVA_ERROR; |
| 262 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 263 | |
Paul McLean | 9b09e53 | 2016-01-26 14:43:35 -0700 | [diff] [blame] | 264 | int* sampleRates = env->GetIntArrayElements(jSampleRate, NULL); |
| 265 | int sampleRateInHertz = sampleRates[0]; |
| 266 | env->ReleaseIntArrayElements(jSampleRate, sampleRates, JNI_ABORT); |
Glenn Kasten | 18db49a | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 267 | |
Paul McLean | 9b09e53 | 2016-01-26 14:43:35 -0700 | [diff] [blame] | 268 | // Invalid channel representations are caught by !audio_is_output_channel() below. |
| 269 | audio_channel_mask_t nativeChannelMask = nativeChannelMaskFromJavaChannelMasks( |
| 270 | channelPositionMask, channelIndexMask); |
| 271 | if (!audio_is_output_channel(nativeChannelMask)) { |
| 272 | ALOGE("Error creating AudioTrack: invalid native channel mask %#x.", nativeChannelMask); |
| 273 | return (jint) AUDIOTRACK_ERROR_SETUP_INVALIDCHANNELMASK; |
| 274 | } |
Glenn Kasten | 18db49a | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 275 | |
Paul McLean | 9b09e53 | 2016-01-26 14:43:35 -0700 | [diff] [blame] | 276 | uint32_t channelCount = audio_channel_count_from_out_mask(nativeChannelMask); |
| 277 | |
| 278 | // check the format. |
| 279 | // This function was called from Java, so we compare the format against the Java constants |
| 280 | audio_format_t format = audioFormatToNative(audioFormat); |
| 281 | if (format == AUDIO_FORMAT_INVALID) { |
| 282 | ALOGE("Error creating AudioTrack: unsupported audio format %d.", audioFormat); |
| 283 | return (jint) AUDIOTRACK_ERROR_SETUP_INVALIDFORMAT; |
| 284 | } |
| 285 | |
| 286 | // compute the frame count |
| 287 | size_t frameCount; |
| 288 | if (audio_is_linear_pcm(format)) { |
| 289 | const size_t bytesPerSample = audio_bytes_per_sample(format); |
| 290 | frameCount = buffSizeInBytes / (channelCount * bytesPerSample); |
| 291 | } else { |
| 292 | frameCount = buffSizeInBytes; |
| 293 | } |
| 294 | |
| 295 | // create the native AudioTrack object |
| 296 | lpTrack = new AudioTrack(); |
| 297 | |
| 298 | // read the AudioAttributes values |
| 299 | paa = (audio_attributes_t *) calloc(1, sizeof(audio_attributes_t)); |
| 300 | const jstring jtags = |
| 301 | (jstring) env->GetObjectField(jaa, javaAudioAttrFields.fieldFormattedTags); |
| 302 | const char* tags = env->GetStringUTFChars(jtags, NULL); |
| 303 | // copying array size -1, char array for tags was calloc'd, no need to NULL-terminate it |
| 304 | strncpy(paa->tags, tags, AUDIO_ATTRIBUTES_TAGS_MAX_SIZE - 1); |
| 305 | env->ReleaseStringUTFChars(jtags, tags); |
| 306 | paa->usage = (audio_usage_t) env->GetIntField(jaa, javaAudioAttrFields.fieldUsage); |
| 307 | paa->content_type = |
| 308 | (audio_content_type_t) env->GetIntField(jaa, javaAudioAttrFields.fieldContentType); |
| 309 | paa->flags = env->GetIntField(jaa, javaAudioAttrFields.fieldFlags); |
| 310 | |
| 311 | ALOGV("AudioTrack_setup for usage=%d content=%d flags=0x%#x tags=%s", |
| 312 | paa->usage, paa->content_type, paa->flags, paa->tags); |
| 313 | |
| 314 | // initialize the callback information: |
| 315 | // this data will be passed with every AudioTrack callback |
| 316 | lpJniStorage = new AudioTrackJniStorage(); |
| 317 | lpJniStorage->mCallbackData.audioTrack_class = (jclass)env->NewGlobalRef(clazz); |
| 318 | // we use a weak reference so the AudioTrack object can be garbage collected. |
| 319 | lpJniStorage->mCallbackData.audioTrack_ref = env->NewGlobalRef(weak_this); |
| 320 | lpJniStorage->mCallbackData.busy = false; |
| 321 | |
| 322 | // initialize the native AudioTrack object |
| 323 | status_t status = NO_ERROR; |
| 324 | switch (memoryMode) { |
| 325 | case MODE_STREAM: |
| 326 | |
| 327 | status = lpTrack->set( |
| 328 | AUDIO_STREAM_DEFAULT,// stream type, but more info conveyed in paa (last argument) |
| 329 | sampleRateInHertz, |
| 330 | format,// word length, PCM |
| 331 | nativeChannelMask, |
| 332 | frameCount, |
| 333 | AUDIO_OUTPUT_FLAG_NONE, |
| 334 | audioCallback, &(lpJniStorage->mCallbackData),//callback, callback data (user) |
| 335 | 0,// notificationFrames == 0 since not using EVENT_MORE_DATA to feed the AudioTrack |
| 336 | 0,// shared mem |
| 337 | true,// thread can call Java |
| 338 | sessionId,// audio session ID |
| 339 | AudioTrack::TRANSFER_SYNC, |
| 340 | NULL, // default offloadInfo |
| 341 | -1, -1, // default uid, pid values |
| 342 | paa); |
| 343 | break; |
| 344 | |
| 345 | case MODE_STATIC: |
| 346 | // AudioTrack is using shared memory |
| 347 | |
| 348 | if (!lpJniStorage->allocSharedMem(buffSizeInBytes)) { |
| 349 | ALOGE("Error creating AudioTrack in static mode: error creating mem heap base"); |
| 350 | goto native_init_failure; |
| 351 | } |
| 352 | |
| 353 | status = lpTrack->set( |
| 354 | AUDIO_STREAM_DEFAULT,// stream type, but more info conveyed in paa (last argument) |
| 355 | sampleRateInHertz, |
| 356 | format,// word length, PCM |
| 357 | nativeChannelMask, |
| 358 | frameCount, |
| 359 | AUDIO_OUTPUT_FLAG_NONE, |
| 360 | audioCallback, &(lpJniStorage->mCallbackData),//callback, callback data (user)); |
| 361 | 0,// notificationFrames == 0 since not using EVENT_MORE_DATA to feed the AudioTrack |
| 362 | lpJniStorage->mMemBase,// shared mem |
| 363 | true,// thread can call Java |
| 364 | sessionId,// audio session ID |
| 365 | AudioTrack::TRANSFER_SHARED, |
| 366 | NULL, // default offloadInfo |
| 367 | -1, -1, // default uid, pid values |
| 368 | paa); |
| 369 | break; |
| 370 | |
| 371 | default: |
| 372 | ALOGE("Unknown mode %d", memoryMode); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 373 | goto native_init_failure; |
| 374 | } |
Glenn Kasten | 18db49a | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 375 | |
Paul McLean | 9b09e53 | 2016-01-26 14:43:35 -0700 | [diff] [blame] | 376 | if (status != NO_ERROR) { |
| 377 | ALOGE("Error %d initializing AudioTrack", status); |
| 378 | goto native_init_failure; |
| 379 | } |
| 380 | } else { // end if (nativeAudioTrack == 0) |
| 381 | lpTrack = (AudioTrack*)nativeAudioTrack; |
| 382 | // TODO: We need to find out which members of the Java AudioTrack might |
| 383 | // need to be initialized from the Native AudioTrack |
| 384 | // these are directly returned from getters: |
| 385 | // mSampleRate |
| 386 | // mAudioFormat |
| 387 | // mStreamType |
| 388 | // mChannelConfiguration |
| 389 | // mChannelCount |
| 390 | // mState (?) |
| 391 | // mPlayState (?) |
| 392 | // these may be used internally (Java AudioTrack.audioParamCheck(): |
| 393 | // mChannelMask |
| 394 | // mChannelIndexMask |
| 395 | // mDataLoadMode |
Glenn Kasten | 3d301cb | 2012-01-16 14:46:54 -0800 | [diff] [blame] | 396 | |
Paul McLean | 9b09e53 | 2016-01-26 14:43:35 -0700 | [diff] [blame] | 397 | // initialize the callback information: |
| 398 | // this data will be passed with every AudioTrack callback |
| 399 | lpJniStorage = new AudioTrackJniStorage(); |
| 400 | lpJniStorage->mCallbackData.audioTrack_class = (jclass)env->NewGlobalRef(clazz); |
| 401 | // we use a weak reference so the AudioTrack object can be garbage collected. |
| 402 | lpJniStorage->mCallbackData.audioTrack_ref = env->NewGlobalRef(weak_this); |
| 403 | lpJniStorage->mCallbackData.busy = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 404 | } |
| 405 | |
Eric Laurent | 2fb43ef | 2010-09-24 12:03:36 -0700 | [diff] [blame] | 406 | nSession = (jint *) env->GetPrimitiveArrayCritical(jSession, NULL); |
| 407 | if (nSession == NULL) { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 408 | ALOGE("Error creating AudioTrack: Error retrieving session id pointer"); |
Eric Laurent | 2fb43ef | 2010-09-24 12:03:36 -0700 | [diff] [blame] | 409 | goto native_init_failure; |
| 410 | } |
Eric Laurent | 619346f | 2010-06-21 09:27:30 -0700 | [diff] [blame] | 411 | // read the audio session ID back from AudioTrack in case we create a new session |
| 412 | nSession[0] = lpTrack->getSessionId(); |
Eric Laurent | 619346f | 2010-06-21 09:27:30 -0700 | [diff] [blame] | 413 | env->ReleasePrimitiveArrayCritical(jSession, nSession, 0); |
| 414 | nSession = NULL; |
| 415 | |
Glenn Kasten | 1cbf9b3 | 2016-02-02 12:04:09 -0800 | [diff] [blame] | 416 | { |
| 417 | const jint elements[1] = { (jint) lpTrack->getSampleRate() }; |
| 418 | env->SetIntArrayRegion(jSampleRate, 0, 1, elements); |
| 419 | } |
| 420 | |
Eric Laurent | 532bc1c | 2012-04-20 12:45:03 -0700 | [diff] [blame] | 421 | { // scope for the lock |
| 422 | Mutex::Autolock l(sLock); |
| 423 | sAudioTrackCallBackCookies.add(&lpJniStorage->mCallbackData); |
| 424 | } |
Glenn Kasten | 18db49a | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 425 | // 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] | 426 | // of the Java object (in mNativeTrackInJavaObj) |
Eric Laurent | 532bc1c | 2012-04-20 12:45:03 -0700 | [diff] [blame] | 427 | setAudioTrack(env, thiz, lpTrack); |
Glenn Kasten | 18db49a | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 428 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 429 | // save the JNI resources so we can free them later |
Ashok Bhat | 075e9a1 | 2014-01-06 13:45:09 +0000 | [diff] [blame] | 430 | //ALOGV("storing lpJniStorage: %x\n", (long)lpJniStorage); |
| 431 | env->SetLongField(thiz, javaAudioTrackFields.jniData, (jlong)lpJniStorage); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 432 | |
Jean-Michel Trivi | a1d80e3 | 2014-06-18 08:18:41 -0700 | [diff] [blame] | 433 | // since we had audio attributes, the stream type was derived from them during the |
| 434 | // creation of the native AudioTrack: push the same value to the Java object |
| 435 | env->SetIntField(thiz, javaAudioTrackFields.fieldStreamType, (jint) lpTrack->streamType()); |
Paul McLean | 9b09e53 | 2016-01-26 14:43:35 -0700 | [diff] [blame] | 436 | if (paa != NULL) { |
| 437 | // audio attributes were copied in AudioTrack creation |
| 438 | free(paa); |
| 439 | paa = NULL; |
| 440 | } |
Jean-Michel Trivi | a1d80e3 | 2014-06-18 08:18:41 -0700 | [diff] [blame] | 441 | |
| 442 | |
Eric Laurent | bc11a69 | 2014-05-16 12:19:25 -0700 | [diff] [blame] | 443 | return (jint) AUDIO_JAVA_SUCCESS; |
Glenn Kasten | 18db49a | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 444 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 445 | // failures: |
| 446 | native_init_failure: |
Jean-Michel Trivi | a1d80e3 | 2014-06-18 08:18:41 -0700 | [diff] [blame] | 447 | if (paa != NULL) { |
| 448 | free(paa); |
| 449 | } |
Eric Laurent | 619346f | 2010-06-21 09:27:30 -0700 | [diff] [blame] | 450 | if (nSession != NULL) { |
| 451 | env->ReleasePrimitiveArrayCritical(jSession, nSession, 0); |
| 452 | } |
Jean-Michel Trivi | 8a14968 | 2009-07-15 18:31:11 -0700 | [diff] [blame] | 453 | env->DeleteGlobalRef(lpJniStorage->mCallbackData.audioTrack_class); |
| 454 | env->DeleteGlobalRef(lpJniStorage->mCallbackData.audioTrack_ref); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 455 | delete lpJniStorage; |
Ashok Bhat | 075e9a1 | 2014-01-06 13:45:09 +0000 | [diff] [blame] | 456 | env->SetLongField(thiz, javaAudioTrackFields.jniData, 0); |
Glenn Kasten | 18db49a | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 457 | |
Glenn Kasten | 14d226a | 2015-05-18 13:53:39 -0700 | [diff] [blame] | 458 | // lpTrack goes out of scope, so reference count drops to zero |
Ashok Bhat | 075e9a1 | 2014-01-06 13:45:09 +0000 | [diff] [blame] | 459 | return (jint) AUDIOTRACK_ERROR_SETUP_NATIVEINITFAILED; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 460 | } |
| 461 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 462 | // ---------------------------------------------------------------------------- |
| 463 | static void |
| 464 | android_media_AudioTrack_start(JNIEnv *env, jobject thiz) |
| 465 | { |
Eric Laurent | 532bc1c | 2012-04-20 12:45:03 -0700 | [diff] [blame] | 466 | sp<AudioTrack> lpTrack = getAudioTrack(env, thiz); |
| 467 | if (lpTrack == NULL) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 468 | jniThrowException(env, "java/lang/IllegalStateException", |
| 469 | "Unable to retrieve AudioTrack pointer for start()"); |
Eric Laurent | 7070b36 | 2010-07-16 07:43:46 -0700 | [diff] [blame] | 470 | return; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 471 | } |
Jean-Michel Trivi | 21dc037 | 2009-05-08 16:06:34 -0700 | [diff] [blame] | 472 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 473 | lpTrack->start(); |
| 474 | } |
| 475 | |
| 476 | |
| 477 | // ---------------------------------------------------------------------------- |
| 478 | static void |
| 479 | android_media_AudioTrack_stop(JNIEnv *env, jobject thiz) |
| 480 | { |
Eric Laurent | 532bc1c | 2012-04-20 12:45:03 -0700 | [diff] [blame] | 481 | sp<AudioTrack> lpTrack = getAudioTrack(env, thiz); |
| 482 | if (lpTrack == NULL) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 483 | jniThrowException(env, "java/lang/IllegalStateException", |
| 484 | "Unable to retrieve AudioTrack pointer for stop()"); |
Eric Laurent | 7070b36 | 2010-07-16 07:43:46 -0700 | [diff] [blame] | 485 | return; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 486 | } |
| 487 | |
| 488 | lpTrack->stop(); |
| 489 | } |
| 490 | |
| 491 | |
| 492 | // ---------------------------------------------------------------------------- |
| 493 | static void |
| 494 | android_media_AudioTrack_pause(JNIEnv *env, jobject thiz) |
| 495 | { |
Eric Laurent | 532bc1c | 2012-04-20 12:45:03 -0700 | [diff] [blame] | 496 | sp<AudioTrack> lpTrack = getAudioTrack(env, thiz); |
| 497 | if (lpTrack == NULL) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 498 | jniThrowException(env, "java/lang/IllegalStateException", |
| 499 | "Unable to retrieve AudioTrack pointer for pause()"); |
Eric Laurent | 7070b36 | 2010-07-16 07:43:46 -0700 | [diff] [blame] | 500 | return; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 501 | } |
| 502 | |
| 503 | lpTrack->pause(); |
| 504 | } |
| 505 | |
| 506 | |
| 507 | // ---------------------------------------------------------------------------- |
| 508 | static void |
| 509 | android_media_AudioTrack_flush(JNIEnv *env, jobject thiz) |
| 510 | { |
Eric Laurent | 532bc1c | 2012-04-20 12:45:03 -0700 | [diff] [blame] | 511 | sp<AudioTrack> lpTrack = getAudioTrack(env, thiz); |
| 512 | if (lpTrack == NULL) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 513 | jniThrowException(env, "java/lang/IllegalStateException", |
| 514 | "Unable to retrieve AudioTrack pointer for flush()"); |
Eric Laurent | 7070b36 | 2010-07-16 07:43:46 -0700 | [diff] [blame] | 515 | return; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 516 | } |
| 517 | |
| 518 | lpTrack->flush(); |
| 519 | } |
| 520 | |
| 521 | // ---------------------------------------------------------------------------- |
| 522 | static void |
| 523 | android_media_AudioTrack_set_volume(JNIEnv *env, jobject thiz, jfloat leftVol, jfloat rightVol ) |
| 524 | { |
Eric Laurent | 532bc1c | 2012-04-20 12:45:03 -0700 | [diff] [blame] | 525 | sp<AudioTrack> lpTrack = getAudioTrack(env, thiz); |
| 526 | if (lpTrack == NULL) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 527 | jniThrowException(env, "java/lang/IllegalStateException", |
| 528 | "Unable to retrieve AudioTrack pointer for setVolume()"); |
Eric Laurent | 7070b36 | 2010-07-16 07:43:46 -0700 | [diff] [blame] | 529 | return; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 530 | } |
| 531 | |
| 532 | lpTrack->setVolume(leftVol, rightVol); |
| 533 | } |
| 534 | |
| 535 | // ---------------------------------------------------------------------------- |
Glenn Kasten | 18db49a | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 536 | |
Eric Laurent | 532bc1c | 2012-04-20 12:45:03 -0700 | [diff] [blame] | 537 | #define CALLBACK_COND_WAIT_TIMEOUT_MS 1000 |
Jean-Michel Trivi | eac8438 | 2014-02-04 14:50:40 -0800 | [diff] [blame] | 538 | static void android_media_AudioTrack_release(JNIEnv *env, jobject thiz) { |
Eric Laurent | 532bc1c | 2012-04-20 12:45:03 -0700 | [diff] [blame] | 539 | sp<AudioTrack> lpTrack = setAudioTrack(env, thiz, 0); |
| 540 | if (lpTrack == NULL) { |
| 541 | return; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 542 | } |
Eric Laurent | 532bc1c | 2012-04-20 12:45:03 -0700 | [diff] [blame] | 543 | //ALOGV("deleting lpTrack: %x\n", (int)lpTrack); |
Glenn Kasten | 18db49a | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 544 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 545 | // delete the JNI data |
Ashok Bhat | 075e9a1 | 2014-01-06 13:45:09 +0000 | [diff] [blame] | 546 | AudioTrackJniStorage* pJniStorage = (AudioTrackJniStorage *)env->GetLongField( |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 547 | thiz, javaAudioTrackFields.jniData); |
Eric Laurent | 532bc1c | 2012-04-20 12:45:03 -0700 | [diff] [blame] | 548 | // reset the native resources in the Java object so any attempt to access |
| 549 | // them after a call to release fails. |
Ashok Bhat | 075e9a1 | 2014-01-06 13:45:09 +0000 | [diff] [blame] | 550 | env->SetLongField(thiz, javaAudioTrackFields.jniData, 0); |
Eric Laurent | 532bc1c | 2012-04-20 12:45:03 -0700 | [diff] [blame] | 551 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 552 | if (pJniStorage) { |
Eric Laurent | 532bc1c | 2012-04-20 12:45:03 -0700 | [diff] [blame] | 553 | Mutex::Autolock l(sLock); |
| 554 | audiotrack_callback_cookie *lpCookie = &pJniStorage->mCallbackData; |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 555 | //ALOGV("deleting pJniStorage: %x\n", (int)pJniStorage); |
Eric Laurent | 532bc1c | 2012-04-20 12:45:03 -0700 | [diff] [blame] | 556 | while (lpCookie->busy) { |
| 557 | if (lpCookie->cond.waitRelative(sLock, |
| 558 | milliseconds(CALLBACK_COND_WAIT_TIMEOUT_MS)) != |
| 559 | NO_ERROR) { |
| 560 | break; |
| 561 | } |
| 562 | } |
| 563 | sAudioTrackCallBackCookies.remove(lpCookie); |
| 564 | // delete global refs created in native_setup |
| 565 | env->DeleteGlobalRef(lpCookie->audioTrack_class); |
| 566 | env->DeleteGlobalRef(lpCookie->audioTrack_ref); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 567 | delete pJniStorage; |
| 568 | } |
| 569 | } |
| 570 | |
Glenn Kasten | 18db49a | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 571 | |
Eric Laurent | 532bc1c | 2012-04-20 12:45:03 -0700 | [diff] [blame] | 572 | // ---------------------------------------------------------------------------- |
Jean-Michel Trivi | eac8438 | 2014-02-04 14:50:40 -0800 | [diff] [blame] | 573 | static void android_media_AudioTrack_finalize(JNIEnv *env, jobject thiz) { |
| 574 | //ALOGV("android_media_AudioTrack_finalize jobject: %x\n", (int)thiz); |
| 575 | android_media_AudioTrack_release(env, thiz); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 576 | } |
| 577 | |
Andy Hung | 4aacc90 | 2015-04-14 15:01:29 -0700 | [diff] [blame] | 578 | // overloaded JNI array helper functions (same as in android_media_AudioRecord) |
| 579 | static inline |
| 580 | jbyte *envGetArrayElements(JNIEnv *env, jbyteArray array, jboolean *isCopy) { |
| 581 | return env->GetByteArrayElements(array, isCopy); |
| 582 | } |
| 583 | |
| 584 | static inline |
| 585 | void envReleaseArrayElements(JNIEnv *env, jbyteArray array, jbyte *elems, jint mode) { |
| 586 | env->ReleaseByteArrayElements(array, elems, mode); |
| 587 | } |
| 588 | |
| 589 | static inline |
| 590 | jshort *envGetArrayElements(JNIEnv *env, jshortArray array, jboolean *isCopy) { |
| 591 | return env->GetShortArrayElements(array, isCopy); |
| 592 | } |
| 593 | |
| 594 | static inline |
| 595 | void envReleaseArrayElements(JNIEnv *env, jshortArray array, jshort *elems, jint mode) { |
| 596 | env->ReleaseShortArrayElements(array, elems, mode); |
| 597 | } |
| 598 | |
| 599 | static inline |
| 600 | jfloat *envGetArrayElements(JNIEnv *env, jfloatArray array, jboolean *isCopy) { |
| 601 | return env->GetFloatArrayElements(array, isCopy); |
| 602 | } |
| 603 | |
| 604 | static inline |
| 605 | void envReleaseArrayElements(JNIEnv *env, jfloatArray array, jfloat *elems, jint mode) { |
| 606 | env->ReleaseFloatArrayElements(array, elems, mode); |
| 607 | } |
| 608 | |
Eric Laurent | 219de73 | 2016-05-23 12:41:50 -0700 | [diff] [blame] | 609 | static inline |
| 610 | jint interpretWriteSizeError(ssize_t writeSize) { |
| 611 | if (writeSize == WOULD_BLOCK) { |
| 612 | return (jint)0; |
| 613 | } else if (writeSize == NO_INIT) { |
| 614 | return AUDIO_JAVA_DEAD_OBJECT; |
| 615 | } else { |
| 616 | ALOGE("Error %zd during AudioTrack native read", writeSize); |
| 617 | return nativeToJavaStatus(writeSize); |
| 618 | } |
| 619 | } |
| 620 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 621 | // ---------------------------------------------------------------------------- |
Andy Hung | 4aacc90 | 2015-04-14 15:01:29 -0700 | [diff] [blame] | 622 | template <typename T> |
| 623 | static jint writeToTrack(const sp<AudioTrack>& track, jint audioFormat, const T *data, |
| 624 | jint offsetInSamples, jint sizeInSamples, bool blocking) { |
Jean-Michel Trivi | 21dc037 | 2009-05-08 16:06:34 -0700 | [diff] [blame] | 625 | // give the data to the native AudioTrack object (the data starts at the offset) |
| 626 | ssize_t written = 0; |
| 627 | // regular write() or copy the data to the AudioTrack's shared memory? |
Andy Hung | 4aacc90 | 2015-04-14 15:01:29 -0700 | [diff] [blame] | 628 | size_t sizeInBytes = sizeInSamples * sizeof(T); |
Eric Laurent | 532bc1c | 2012-04-20 12:45:03 -0700 | [diff] [blame] | 629 | if (track->sharedBuffer() == 0) { |
Andy Hung | 4aacc90 | 2015-04-14 15:01:29 -0700 | [diff] [blame] | 630 | written = track->write(data + offsetInSamples, sizeInBytes, blocking); |
Glenn Kasten | 9b53db3 | 2013-07-10 14:13:39 -0700 | [diff] [blame] | 631 | // for compatibility with earlier behavior of write(), return 0 in this case |
| 632 | if (written == (ssize_t) WOULD_BLOCK) { |
| 633 | written = 0; |
| 634 | } |
Jean-Michel Trivi | 21dc037 | 2009-05-08 16:06:34 -0700 | [diff] [blame] | 635 | } else { |
Andy Hung | 2c0e17c | 2015-01-12 15:07:14 -0800 | [diff] [blame] | 636 | // writing to shared memory, check for capacity |
| 637 | if ((size_t)sizeInBytes > track->sharedBuffer()->size()) { |
| 638 | sizeInBytes = track->sharedBuffer()->size(); |
Jean-Michel Trivi | 21dc037 | 2009-05-08 16:06:34 -0700 | [diff] [blame] | 639 | } |
Andy Hung | 4aacc90 | 2015-04-14 15:01:29 -0700 | [diff] [blame] | 640 | memcpy(track->sharedBuffer()->pointer(), data + offsetInSamples, sizeInBytes); |
Andy Hung | 2c0e17c | 2015-01-12 15:07:14 -0800 | [diff] [blame] | 641 | written = sizeInBytes; |
Jean-Michel Trivi | 21dc037 | 2009-05-08 16:06:34 -0700 | [diff] [blame] | 642 | } |
Eric Laurent | 219de73 | 2016-05-23 12:41:50 -0700 | [diff] [blame] | 643 | if (written >= 0) { |
Andy Hung | 4aacc90 | 2015-04-14 15:01:29 -0700 | [diff] [blame] | 644 | return written / sizeof(T); |
| 645 | } |
Eric Laurent | 219de73 | 2016-05-23 12:41:50 -0700 | [diff] [blame] | 646 | return interpretWriteSizeError(written); |
Jean-Michel Trivi | 21dc037 | 2009-05-08 16:06:34 -0700 | [diff] [blame] | 647 | } |
| 648 | |
| 649 | // ---------------------------------------------------------------------------- |
Andy Hung | 4aacc90 | 2015-04-14 15:01:29 -0700 | [diff] [blame] | 650 | template <typename T> |
| 651 | static jint android_media_AudioTrack_writeArray(JNIEnv *env, jobject thiz, |
| 652 | T javaAudioData, |
| 653 | jint offsetInSamples, jint sizeInSamples, |
| 654 | jint javaAudioFormat, |
| 655 | jboolean isWriteBlocking) { |
| 656 | //ALOGV("android_media_AudioTrack_writeArray(offset=%d, sizeInSamples=%d) called", |
| 657 | // offsetInSamples, sizeInSamples); |
Eric Laurent | 532bc1c | 2012-04-20 12:45:03 -0700 | [diff] [blame] | 658 | sp<AudioTrack> lpTrack = getAudioTrack(env, thiz); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 659 | if (lpTrack == NULL) { |
| 660 | jniThrowException(env, "java/lang/IllegalStateException", |
| 661 | "Unable to retrieve AudioTrack pointer for write()"); |
Andy Hung | 4aacc90 | 2015-04-14 15:01:29 -0700 | [diff] [blame] | 662 | return (jint)AUDIO_JAVA_INVALID_OPERATION; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 663 | } |
| 664 | |
Andy Hung | 4aacc90 | 2015-04-14 15:01:29 -0700 | [diff] [blame] | 665 | if (javaAudioData == NULL) { |
| 666 | ALOGE("NULL java array of audio data to play"); |
| 667 | return (jint)AUDIO_JAVA_BAD_VALUE; |
| 668 | } |
| 669 | |
Eric Laurent | 421ddc0 | 2011-03-07 14:52:59 -0800 | [diff] [blame] | 670 | // NOTE: We may use GetPrimitiveArrayCritical() when the JNI implementation changes in such |
| 671 | // a way that it becomes much more efficient. When doing so, we will have to prevent the |
| 672 | // AudioSystem callback to be called while in critical section (in case of media server |
| 673 | // process crash for instance) |
Andy Hung | 4aacc90 | 2015-04-14 15:01:29 -0700 | [diff] [blame] | 674 | |
| 675 | // get the pointer for the audio data from the java array |
| 676 | auto cAudioData = envGetArrayElements(env, javaAudioData, NULL); |
| 677 | if (cAudioData == NULL) { |
| 678 | ALOGE("Error retrieving source of audio data to play"); |
| 679 | return (jint)AUDIO_JAVA_BAD_VALUE; // out of memory or no data to load |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 680 | } |
| 681 | |
Andy Hung | 4aacc90 | 2015-04-14 15:01:29 -0700 | [diff] [blame] | 682 | jint samplesWritten = writeToTrack(lpTrack, javaAudioFormat, cAudioData, |
| 683 | offsetInSamples, sizeInSamples, isWriteBlocking == JNI_TRUE /* blocking */); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 684 | |
Andy Hung | 4aacc90 | 2015-04-14 15:01:29 -0700 | [diff] [blame] | 685 | envReleaseArrayElements(env, javaAudioData, cAudioData, 0); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 686 | |
Andy Hung | 4aacc90 | 2015-04-14 15:01:29 -0700 | [diff] [blame] | 687 | //ALOGV("write wrote %d (tried %d) samples in the native AudioTrack with offset %d", |
| 688 | // (int)samplesWritten, (int)(sizeInSamples), (int)offsetInSamples); |
| 689 | return samplesWritten; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 690 | } |
| 691 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 692 | // ---------------------------------------------------------------------------- |
Jean-Michel Trivi | 7ca0452 | 2014-02-07 09:39:34 -0800 | [diff] [blame] | 693 | 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] | 694 | jbyteArray javaBytes, jint byteOffset, jint sizeInBytes, |
Jean-Michel Trivi | 7ca0452 | 2014-02-07 09:39:34 -0800 | [diff] [blame] | 695 | jint javaAudioFormat, jboolean isWriteBlocking) { |
| 696 | //ALOGV("android_media_AudioTrack_write_native_bytes(offset=%d, sizeInBytes=%d) called", |
| 697 | // offsetInBytes, sizeInBytes); |
| 698 | sp<AudioTrack> lpTrack = getAudioTrack(env, thiz); |
| 699 | if (lpTrack == NULL) { |
| 700 | jniThrowException(env, "java/lang/IllegalStateException", |
| 701 | "Unable to retrieve AudioTrack pointer for write()"); |
Andy Hung | 4aacc90 | 2015-04-14 15:01:29 -0700 | [diff] [blame] | 702 | return (jint)AUDIO_JAVA_INVALID_OPERATION; |
Jean-Michel Trivi | 7ca0452 | 2014-02-07 09:39:34 -0800 | [diff] [blame] | 703 | } |
| 704 | |
| 705 | ScopedBytesRO bytes(env, javaBytes); |
| 706 | if (bytes.get() == NULL) { |
| 707 | ALOGE("Error retrieving source of audio data to play, can't play"); |
Eric Laurent | bc11a69 | 2014-05-16 12:19:25 -0700 | [diff] [blame] | 708 | return (jint)AUDIO_JAVA_BAD_VALUE; |
Jean-Michel Trivi | 7ca0452 | 2014-02-07 09:39:34 -0800 | [diff] [blame] | 709 | } |
| 710 | |
Jean-Michel Trivi | 5d21f67 | 2014-03-20 17:02:31 -0700 | [diff] [blame] | 711 | jint written = writeToTrack(lpTrack, javaAudioFormat, bytes.get(), byteOffset, |
Jean-Michel Trivi | 7ca0452 | 2014-02-07 09:39:34 -0800 | [diff] [blame] | 712 | sizeInBytes, isWriteBlocking == JNI_TRUE /* blocking */); |
| 713 | |
| 714 | return written; |
| 715 | } |
| 716 | |
| 717 | // ---------------------------------------------------------------------------- |
Phil Burk | 10a33e4 | 2016-01-08 12:40:41 -0800 | [diff] [blame] | 718 | static jint android_media_AudioTrack_get_buffer_size_frames(JNIEnv *env, jobject thiz) { |
Eric Laurent | 532bc1c | 2012-04-20 12:45:03 -0700 | [diff] [blame] | 719 | sp<AudioTrack> lpTrack = getAudioTrack(env, thiz); |
| 720 | if (lpTrack == NULL) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 721 | jniThrowException(env, "java/lang/IllegalStateException", |
Phil Burk | 10a33e4 | 2016-01-08 12:40:41 -0800 | [diff] [blame] | 722 | "Unable to retrieve AudioTrack pointer for getBufferSizeInFrames()"); |
| 723 | return (jint)AUDIO_JAVA_ERROR; |
| 724 | } |
| 725 | |
| 726 | ssize_t result = lpTrack->getBufferSizeInFrames(); |
| 727 | if (result < 0) { |
| 728 | jniThrowException(env, "java/lang/IllegalStateException", |
| 729 | "Internal error detected in getBufferSizeInFrames() = " + result); |
| 730 | return (jint)AUDIO_JAVA_ERROR; |
| 731 | } |
| 732 | return (jint)result; |
| 733 | } |
| 734 | |
| 735 | // ---------------------------------------------------------------------------- |
| 736 | static jint android_media_AudioTrack_set_buffer_size_frames(JNIEnv *env, |
| 737 | jobject thiz, jint bufferSizeInFrames) { |
| 738 | sp<AudioTrack> lpTrack = getAudioTrack(env, thiz); |
| 739 | if (lpTrack == NULL) { |
| 740 | jniThrowException(env, "java/lang/IllegalStateException", |
| 741 | "Unable to retrieve AudioTrack pointer for setBufferSizeInFrames()"); |
| 742 | return (jint)AUDIO_JAVA_ERROR; |
| 743 | } |
| 744 | // Value will be coerced into the valid range. |
| 745 | // But internal values are unsigned, size_t, so we need to clip |
| 746 | // against zero here where it is signed. |
| 747 | if (bufferSizeInFrames < 0) { |
| 748 | bufferSizeInFrames = 0; |
| 749 | } |
| 750 | ssize_t result = lpTrack->setBufferSizeInFrames(bufferSizeInFrames); |
| 751 | if (result < 0) { |
| 752 | jniThrowException(env, "java/lang/IllegalStateException", |
| 753 | "Internal error detected in setBufferSizeInFrames() = " + result); |
| 754 | return (jint)AUDIO_JAVA_ERROR; |
| 755 | } |
| 756 | return (jint)result; |
| 757 | } |
| 758 | |
| 759 | // ---------------------------------------------------------------------------- |
| 760 | static jint android_media_AudioTrack_get_buffer_capacity_frames(JNIEnv *env, jobject thiz) { |
| 761 | sp<AudioTrack> lpTrack = getAudioTrack(env, thiz); |
| 762 | if (lpTrack == NULL) { |
| 763 | jniThrowException(env, "java/lang/IllegalStateException", |
| 764 | "Unable to retrieve AudioTrack pointer for getBufferCapacityInFrames()"); |
Eric Laurent | bc11a69 | 2014-05-16 12:19:25 -0700 | [diff] [blame] | 765 | return (jint)AUDIO_JAVA_ERROR; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 766 | } |
Eric Laurent | 532bc1c | 2012-04-20 12:45:03 -0700 | [diff] [blame] | 767 | |
| 768 | return lpTrack->frameCount(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 769 | } |
| 770 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 771 | // ---------------------------------------------------------------------------- |
Eric Laurent | 88e209d | 2009-07-07 07:10:45 -0700 | [diff] [blame] | 772 | 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] | 773 | jint sampleRateInHz) { |
Eric Laurent | 532bc1c | 2012-04-20 12:45:03 -0700 | [diff] [blame] | 774 | sp<AudioTrack> lpTrack = getAudioTrack(env, thiz); |
| 775 | if (lpTrack == NULL) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 776 | jniThrowException(env, "java/lang/IllegalStateException", |
| 777 | "Unable to retrieve AudioTrack pointer for setSampleRate()"); |
Eric Laurent | bc11a69 | 2014-05-16 12:19:25 -0700 | [diff] [blame] | 778 | return (jint)AUDIO_JAVA_ERROR; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 779 | } |
Eric Laurent | bc11a69 | 2014-05-16 12:19:25 -0700 | [diff] [blame] | 780 | return nativeToJavaStatus(lpTrack->setSampleRate(sampleRateInHz)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 781 | } |
| 782 | |
| 783 | |
| 784 | // ---------------------------------------------------------------------------- |
| 785 | static jint android_media_AudioTrack_get_playback_rate(JNIEnv *env, jobject thiz) { |
Eric Laurent | 532bc1c | 2012-04-20 12:45:03 -0700 | [diff] [blame] | 786 | sp<AudioTrack> lpTrack = getAudioTrack(env, thiz); |
| 787 | if (lpTrack == NULL) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 788 | jniThrowException(env, "java/lang/IllegalStateException", |
| 789 | "Unable to retrieve AudioTrack pointer for getSampleRate()"); |
Eric Laurent | bc11a69 | 2014-05-16 12:19:25 -0700 | [diff] [blame] | 790 | return (jint)AUDIO_JAVA_ERROR; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 791 | } |
Eric Laurent | 532bc1c | 2012-04-20 12:45:03 -0700 | [diff] [blame] | 792 | return (jint) lpTrack->getSampleRate(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 793 | } |
| 794 | |
| 795 | |
| 796 | // ---------------------------------------------------------------------------- |
Wei Jia | 2d61e2b | 2015-05-08 15:23:28 -0700 | [diff] [blame] | 797 | static void android_media_AudioTrack_set_playback_params(JNIEnv *env, jobject thiz, |
| 798 | jobject params) { |
Andy Hung | 263b4c9 | 2015-04-16 11:16:29 -0700 | [diff] [blame] | 799 | sp<AudioTrack> lpTrack = getAudioTrack(env, thiz); |
| 800 | if (lpTrack == NULL) { |
| 801 | jniThrowException(env, "java/lang/IllegalStateException", |
| 802 | "AudioTrack not initialized"); |
| 803 | return; |
| 804 | } |
| 805 | |
Andy Hung | 973b885 | 2015-05-13 15:15:25 -0700 | [diff] [blame] | 806 | PlaybackParams pbp; |
| 807 | pbp.fillFromJobject(env, gPlaybackParamsFields, params); |
Andy Hung | fe48e0d | 2015-04-27 18:14:02 -0700 | [diff] [blame] | 808 | |
Wei Jia | 2d61e2b | 2015-05-08 15:23:28 -0700 | [diff] [blame] | 809 | ALOGV("setPlaybackParams: %d:%f %d:%f %d:%u %d:%u", |
Andy Hung | 973b885 | 2015-05-13 15:15:25 -0700 | [diff] [blame] | 810 | pbp.speedSet, pbp.audioRate.mSpeed, |
| 811 | pbp.pitchSet, pbp.audioRate.mPitch, |
| 812 | pbp.audioFallbackModeSet, pbp.audioRate.mFallbackMode, |
| 813 | pbp.audioStretchModeSet, pbp.audioRate.mStretchMode); |
Andy Hung | fe48e0d | 2015-04-27 18:14:02 -0700 | [diff] [blame] | 814 | |
Andy Hung | 973b885 | 2015-05-13 15:15:25 -0700 | [diff] [blame] | 815 | // to simulate partially set params, we do a read-modify-write. |
| 816 | // TODO: pass in the valid set mask into AudioTrack. |
| 817 | AudioPlaybackRate rate = lpTrack->getPlaybackRate(); |
| 818 | bool updatedRate = false; |
| 819 | if (pbp.speedSet) { |
| 820 | rate.mSpeed = pbp.audioRate.mSpeed; |
| 821 | updatedRate = true; |
| 822 | } |
| 823 | if (pbp.pitchSet) { |
| 824 | rate.mPitch = pbp.audioRate.mPitch; |
| 825 | updatedRate = true; |
| 826 | } |
| 827 | if (pbp.audioFallbackModeSet) { |
| 828 | rate.mFallbackMode = pbp.audioRate.mFallbackMode; |
| 829 | updatedRate = true; |
| 830 | } |
| 831 | if (pbp.audioStretchModeSet) { |
| 832 | rate.mStretchMode = pbp.audioRate.mStretchMode; |
| 833 | updatedRate = true; |
| 834 | } |
| 835 | if (updatedRate) { |
| 836 | if (lpTrack->setPlaybackRate(rate) != OK) { |
| 837 | jniThrowException(env, "java/lang/IllegalArgumentException", |
| 838 | "arguments out of range"); |
| 839 | } |
Andy Hung | 263b4c9 | 2015-04-16 11:16:29 -0700 | [diff] [blame] | 840 | } |
| 841 | } |
| 842 | |
| 843 | |
| 844 | // ---------------------------------------------------------------------------- |
Wei Jia | 2d61e2b | 2015-05-08 15:23:28 -0700 | [diff] [blame] | 845 | static jobject android_media_AudioTrack_get_playback_params(JNIEnv *env, jobject thiz, |
| 846 | jobject params) { |
Andy Hung | 263b4c9 | 2015-04-16 11:16:29 -0700 | [diff] [blame] | 847 | sp<AudioTrack> lpTrack = getAudioTrack(env, thiz); |
| 848 | if (lpTrack == NULL) { |
| 849 | jniThrowException(env, "java/lang/IllegalStateException", |
| 850 | "AudioTrack not initialized"); |
Andy Hung | fe48e0d | 2015-04-27 18:14:02 -0700 | [diff] [blame] | 851 | return NULL; |
Andy Hung | 263b4c9 | 2015-04-16 11:16:29 -0700 | [diff] [blame] | 852 | } |
| 853 | |
Wei Jia | 2d61e2b | 2015-05-08 15:23:28 -0700 | [diff] [blame] | 854 | PlaybackParams pbs; |
Andy Hung | fe48e0d | 2015-04-27 18:14:02 -0700 | [diff] [blame] | 855 | pbs.audioRate = lpTrack->getPlaybackRate(); |
| 856 | pbs.speedSet = true; |
| 857 | pbs.pitchSet = true; |
| 858 | pbs.audioFallbackModeSet = true; |
| 859 | pbs.audioStretchModeSet = true; |
Wei Jia | 2d61e2b | 2015-05-08 15:23:28 -0700 | [diff] [blame] | 860 | return pbs.asJobject(env, gPlaybackParamsFields); |
Andy Hung | 263b4c9 | 2015-04-16 11:16:29 -0700 | [diff] [blame] | 861 | } |
| 862 | |
| 863 | |
| 864 | // ---------------------------------------------------------------------------- |
Glenn Kasten | 18db49a | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 865 | 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] | 866 | jint markerPos) { |
Eric Laurent | 532bc1c | 2012-04-20 12:45:03 -0700 | [diff] [blame] | 867 | sp<AudioTrack> lpTrack = getAudioTrack(env, thiz); |
| 868 | if (lpTrack == NULL) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 869 | jniThrowException(env, "java/lang/IllegalStateException", |
| 870 | "Unable to retrieve AudioTrack pointer for setMarkerPosition()"); |
Eric Laurent | bc11a69 | 2014-05-16 12:19:25 -0700 | [diff] [blame] | 871 | return (jint)AUDIO_JAVA_ERROR; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 872 | } |
Eric Laurent | bc11a69 | 2014-05-16 12:19:25 -0700 | [diff] [blame] | 873 | return nativeToJavaStatus( lpTrack->setMarkerPosition(markerPos) ); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 874 | } |
| 875 | |
| 876 | |
| 877 | // ---------------------------------------------------------------------------- |
| 878 | static jint android_media_AudioTrack_get_marker_pos(JNIEnv *env, jobject thiz) { |
Eric Laurent | 532bc1c | 2012-04-20 12:45:03 -0700 | [diff] [blame] | 879 | sp<AudioTrack> lpTrack = getAudioTrack(env, thiz); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 880 | uint32_t markerPos = 0; |
Glenn Kasten | 18db49a | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 881 | |
Eric Laurent | 532bc1c | 2012-04-20 12:45:03 -0700 | [diff] [blame] | 882 | if (lpTrack == NULL) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 883 | jniThrowException(env, "java/lang/IllegalStateException", |
| 884 | "Unable to retrieve AudioTrack pointer for getMarkerPosition()"); |
Eric Laurent | bc11a69 | 2014-05-16 12:19:25 -0700 | [diff] [blame] | 885 | return (jint)AUDIO_JAVA_ERROR; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 886 | } |
Eric Laurent | 532bc1c | 2012-04-20 12:45:03 -0700 | [diff] [blame] | 887 | lpTrack->getMarkerPosition(&markerPos); |
| 888 | return (jint)markerPos; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 889 | } |
| 890 | |
| 891 | |
| 892 | // ---------------------------------------------------------------------------- |
| 893 | static jint android_media_AudioTrack_set_pos_update_period(JNIEnv *env, jobject thiz, |
| 894 | jint period) { |
Eric Laurent | 532bc1c | 2012-04-20 12:45:03 -0700 | [diff] [blame] | 895 | sp<AudioTrack> lpTrack = getAudioTrack(env, thiz); |
| 896 | if (lpTrack == NULL) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 897 | jniThrowException(env, "java/lang/IllegalStateException", |
| 898 | "Unable to retrieve AudioTrack pointer for setPositionUpdatePeriod()"); |
Eric Laurent | bc11a69 | 2014-05-16 12:19:25 -0700 | [diff] [blame] | 899 | return (jint)AUDIO_JAVA_ERROR; |
Glenn Kasten | 18db49a | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 900 | } |
Eric Laurent | bc11a69 | 2014-05-16 12:19:25 -0700 | [diff] [blame] | 901 | return nativeToJavaStatus( lpTrack->setPositionUpdatePeriod(period) ); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 902 | } |
| 903 | |
| 904 | |
| 905 | // ---------------------------------------------------------------------------- |
| 906 | 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] | 907 | sp<AudioTrack> lpTrack = getAudioTrack(env, thiz); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 908 | uint32_t period = 0; |
Glenn Kasten | 18db49a | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 909 | |
Eric Laurent | 532bc1c | 2012-04-20 12:45:03 -0700 | [diff] [blame] | 910 | if (lpTrack == NULL) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 911 | jniThrowException(env, "java/lang/IllegalStateException", |
| 912 | "Unable to retrieve AudioTrack pointer for getPositionUpdatePeriod()"); |
Eric Laurent | bc11a69 | 2014-05-16 12:19:25 -0700 | [diff] [blame] | 913 | return (jint)AUDIO_JAVA_ERROR; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 914 | } |
Eric Laurent | 532bc1c | 2012-04-20 12:45:03 -0700 | [diff] [blame] | 915 | lpTrack->getPositionUpdatePeriod(&period); |
| 916 | return (jint)period; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 917 | } |
| 918 | |
| 919 | |
| 920 | // ---------------------------------------------------------------------------- |
Glenn Kasten | 18db49a | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 921 | 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] | 922 | jint position) { |
Eric Laurent | 532bc1c | 2012-04-20 12:45:03 -0700 | [diff] [blame] | 923 | sp<AudioTrack> lpTrack = getAudioTrack(env, thiz); |
| 924 | if (lpTrack == NULL) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 925 | jniThrowException(env, "java/lang/IllegalStateException", |
| 926 | "Unable to retrieve AudioTrack pointer for setPosition()"); |
Eric Laurent | bc11a69 | 2014-05-16 12:19:25 -0700 | [diff] [blame] | 927 | return (jint)AUDIO_JAVA_ERROR; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 928 | } |
Eric Laurent | bc11a69 | 2014-05-16 12:19:25 -0700 | [diff] [blame] | 929 | return nativeToJavaStatus( lpTrack->setPosition(position) ); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 930 | } |
| 931 | |
| 932 | |
| 933 | // ---------------------------------------------------------------------------- |
| 934 | static jint android_media_AudioTrack_get_position(JNIEnv *env, jobject thiz) { |
Eric Laurent | 532bc1c | 2012-04-20 12:45:03 -0700 | [diff] [blame] | 935 | sp<AudioTrack> lpTrack = getAudioTrack(env, thiz); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 936 | uint32_t position = 0; |
Glenn Kasten | 18db49a | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 937 | |
Eric Laurent | 532bc1c | 2012-04-20 12:45:03 -0700 | [diff] [blame] | 938 | if (lpTrack == NULL) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 939 | jniThrowException(env, "java/lang/IllegalStateException", |
| 940 | "Unable to retrieve AudioTrack pointer for getPosition()"); |
Eric Laurent | bc11a69 | 2014-05-16 12:19:25 -0700 | [diff] [blame] | 941 | return (jint)AUDIO_JAVA_ERROR; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 942 | } |
Eric Laurent | 532bc1c | 2012-04-20 12:45:03 -0700 | [diff] [blame] | 943 | lpTrack->getPosition(&position); |
| 944 | return (jint)position; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 945 | } |
| 946 | |
| 947 | |
| 948 | // ---------------------------------------------------------------------------- |
Oliver Woodman | 61dcdf3 | 2013-06-26 12:43:36 +0100 | [diff] [blame] | 949 | static jint android_media_AudioTrack_get_latency(JNIEnv *env, jobject thiz) { |
| 950 | sp<AudioTrack> lpTrack = getAudioTrack(env, thiz); |
| 951 | |
| 952 | if (lpTrack == NULL) { |
| 953 | jniThrowException(env, "java/lang/IllegalStateException", |
| 954 | "Unable to retrieve AudioTrack pointer for latency()"); |
Eric Laurent | bc11a69 | 2014-05-16 12:19:25 -0700 | [diff] [blame] | 955 | return (jint)AUDIO_JAVA_ERROR; |
Oliver Woodman | 61dcdf3 | 2013-06-26 12:43:36 +0100 | [diff] [blame] | 956 | } |
| 957 | return (jint)lpTrack->latency(); |
| 958 | } |
| 959 | |
Phil Burk | 03f61bb | 2016-01-17 21:49:58 +0000 | [diff] [blame] | 960 | // ---------------------------------------------------------------------------- |
| 961 | static jint android_media_AudioTrack_get_underrun_count(JNIEnv *env, jobject thiz) { |
| 962 | sp<AudioTrack> lpTrack = getAudioTrack(env, thiz); |
| 963 | |
| 964 | if (lpTrack == NULL) { |
| 965 | jniThrowException(env, "java/lang/IllegalStateException", |
| 966 | "Unable to retrieve AudioTrack pointer for getUnderrunCount()"); |
| 967 | return (jint)AUDIO_JAVA_ERROR; |
| 968 | } |
| 969 | return (jint)lpTrack->getUnderrunCount(); |
| 970 | } |
Oliver Woodman | 61dcdf3 | 2013-06-26 12:43:36 +0100 | [diff] [blame] | 971 | |
| 972 | // ---------------------------------------------------------------------------- |
Glenn Kasten | 948c2e6 | 2013-09-04 13:51:29 -0700 | [diff] [blame] | 973 | static jint android_media_AudioTrack_get_timestamp(JNIEnv *env, jobject thiz, jlongArray jTimestamp) { |
| 974 | sp<AudioTrack> lpTrack = getAudioTrack(env, thiz); |
| 975 | |
| 976 | if (lpTrack == NULL) { |
| 977 | ALOGE("Unable to retrieve AudioTrack pointer for getTimestamp()"); |
Eric Laurent | bc11a69 | 2014-05-16 12:19:25 -0700 | [diff] [blame] | 978 | return (jint)AUDIO_JAVA_ERROR; |
Glenn Kasten | 948c2e6 | 2013-09-04 13:51:29 -0700 | [diff] [blame] | 979 | } |
| 980 | AudioTimestamp timestamp; |
| 981 | status_t status = lpTrack->getTimestamp(timestamp); |
| 982 | if (status == OK) { |
| 983 | jlong* nTimestamp = (jlong *) env->GetPrimitiveArrayCritical(jTimestamp, NULL); |
| 984 | if (nTimestamp == NULL) { |
| 985 | ALOGE("Unable to get array for getTimestamp()"); |
Eric Laurent | bc11a69 | 2014-05-16 12:19:25 -0700 | [diff] [blame] | 986 | return (jint)AUDIO_JAVA_ERROR; |
Glenn Kasten | 948c2e6 | 2013-09-04 13:51:29 -0700 | [diff] [blame] | 987 | } |
| 988 | nTimestamp[0] = (jlong) timestamp.mPosition; |
| 989 | nTimestamp[1] = (jlong) ((timestamp.mTime.tv_sec * 1000000000LL) + timestamp.mTime.tv_nsec); |
| 990 | env->ReleasePrimitiveArrayCritical(jTimestamp, nTimestamp, 0); |
| 991 | } |
Eric Laurent | bc11a69 | 2014-05-16 12:19:25 -0700 | [diff] [blame] | 992 | return (jint) nativeToJavaStatus(status); |
Glenn Kasten | 948c2e6 | 2013-09-04 13:51:29 -0700 | [diff] [blame] | 993 | } |
| 994 | |
| 995 | |
| 996 | // ---------------------------------------------------------------------------- |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 997 | static jint android_media_AudioTrack_set_loop(JNIEnv *env, jobject thiz, |
| 998 | jint loopStart, jint loopEnd, jint loopCount) { |
Eric Laurent | 532bc1c | 2012-04-20 12:45:03 -0700 | [diff] [blame] | 999 | sp<AudioTrack> lpTrack = getAudioTrack(env, thiz); |
| 1000 | if (lpTrack == NULL) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1001 | jniThrowException(env, "java/lang/IllegalStateException", |
| 1002 | "Unable to retrieve AudioTrack pointer for setLoop()"); |
Eric Laurent | bc11a69 | 2014-05-16 12:19:25 -0700 | [diff] [blame] | 1003 | return (jint)AUDIO_JAVA_ERROR; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1004 | } |
Eric Laurent | bc11a69 | 2014-05-16 12:19:25 -0700 | [diff] [blame] | 1005 | return nativeToJavaStatus( lpTrack->setLoop(loopStart, loopEnd, loopCount) ); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1006 | } |
| 1007 | |
| 1008 | |
| 1009 | // ---------------------------------------------------------------------------- |
| 1010 | static jint android_media_AudioTrack_reload(JNIEnv *env, jobject thiz) { |
Eric Laurent | 532bc1c | 2012-04-20 12:45:03 -0700 | [diff] [blame] | 1011 | sp<AudioTrack> lpTrack = getAudioTrack(env, thiz); |
| 1012 | if (lpTrack == NULL) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1013 | jniThrowException(env, "java/lang/IllegalStateException", |
| 1014 | "Unable to retrieve AudioTrack pointer for reload()"); |
Eric Laurent | bc11a69 | 2014-05-16 12:19:25 -0700 | [diff] [blame] | 1015 | return (jint)AUDIO_JAVA_ERROR; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1016 | } |
Eric Laurent | bc11a69 | 2014-05-16 12:19:25 -0700 | [diff] [blame] | 1017 | return nativeToJavaStatus( lpTrack->reload() ); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1018 | } |
| 1019 | |
| 1020 | |
| 1021 | // ---------------------------------------------------------------------------- |
| 1022 | static jint android_media_AudioTrack_get_output_sample_rate(JNIEnv *env, jobject thiz, |
| 1023 | jint javaStreamType) { |
Glenn Kasten | 85fbc87 | 2012-11-14 13:21:09 -0800 | [diff] [blame] | 1024 | uint32_t afSamplingRate; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1025 | // convert the stream type from Java to native value |
Jean-Michel Trivi | eac8438 | 2014-02-04 14:50:40 -0800 | [diff] [blame] | 1026 | // FIXME: code duplication with android_media_AudioTrack_setup() |
Dima Zavin | 24fc2fb | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 1027 | audio_stream_type_t nativeStreamType; |
Glenn Kasten | 29a0909 | 2012-01-16 14:37:12 -0800 | [diff] [blame] | 1028 | switch (javaStreamType) { |
| 1029 | case AUDIO_STREAM_VOICE_CALL: |
| 1030 | case AUDIO_STREAM_SYSTEM: |
| 1031 | case AUDIO_STREAM_RING: |
| 1032 | case AUDIO_STREAM_MUSIC: |
| 1033 | case AUDIO_STREAM_ALARM: |
| 1034 | case AUDIO_STREAM_NOTIFICATION: |
| 1035 | case AUDIO_STREAM_BLUETOOTH_SCO: |
| 1036 | case AUDIO_STREAM_DTMF: |
| 1037 | nativeStreamType = (audio_stream_type_t) javaStreamType; |
| 1038 | break; |
| 1039 | default: |
Dima Zavin | 24fc2fb | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 1040 | nativeStreamType = AUDIO_STREAM_DEFAULT; |
Glenn Kasten | 29a0909 | 2012-01-16 14:37:12 -0800 | [diff] [blame] | 1041 | break; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1042 | } |
| 1043 | |
Glenn Kasten | 8f81d08 | 2012-11-28 14:37:48 -0800 | [diff] [blame] | 1044 | status_t status = AudioSystem::getOutputSamplingRate(&afSamplingRate, nativeStreamType); |
| 1045 | if (status != NO_ERROR) { |
| 1046 | ALOGE("Error %d in AudioSystem::getOutputSamplingRate() for stream type %d " |
| 1047 | "in AudioTrack JNI", status, nativeStreamType); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1048 | return DEFAULT_OUTPUT_SAMPLE_RATE; |
| 1049 | } else { |
| 1050 | return afSamplingRate; |
| 1051 | } |
| 1052 | } |
| 1053 | |
| 1054 | |
| 1055 | // ---------------------------------------------------------------------------- |
| 1056 | // returns the minimum required size for the successful creation of a streaming AudioTrack |
| 1057 | // returns -1 if there was an error querying the hardware. |
| 1058 | 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] | 1059 | jint sampleRateInHertz, jint channelCount, jint audioFormat) { |
Chia-chi Yeh | c330807 | 2010-08-19 17:14:36 +0800 | [diff] [blame] | 1060 | |
Glenn Kasten | 659a971 | 2014-01-08 11:38:33 -0800 | [diff] [blame] | 1061 | size_t frameCount; |
| 1062 | const status_t status = AudioTrack::getMinFrameCount(&frameCount, AUDIO_STREAM_DEFAULT, |
| 1063 | sampleRateInHertz); |
| 1064 | if (status != NO_ERROR) { |
| 1065 | ALOGE("AudioTrack::getMinFrameCount() for sample rate %d failed with status %d", |
| 1066 | sampleRateInHertz, status); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1067 | return -1; |
| 1068 | } |
Glenn Kasten | fe834d3 | 2014-01-08 14:49:08 -0800 | [diff] [blame] | 1069 | const audio_format_t format = audioFormatToNative(audioFormat); |
Phil Burk | 43f4b27 | 2016-01-27 15:35:20 -0800 | [diff] [blame] | 1070 | if (audio_has_proportional_frames(format)) { |
Eric Laurent | ff0d9f0 | 2014-06-09 17:23:02 -0700 | [diff] [blame] | 1071 | const size_t bytesPerSample = audio_bytes_per_sample(format); |
| 1072 | return frameCount * channelCount * bytesPerSample; |
| 1073 | } else { |
| 1074 | return frameCount; |
| 1075 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1076 | } |
| 1077 | |
Eric Laurent | 7070b36 | 2010-07-16 07:43:46 -0700 | [diff] [blame] | 1078 | // ---------------------------------------------------------------------------- |
Glenn Kasten | 3009f0b | 2014-03-28 16:02:26 -0700 | [diff] [blame] | 1079 | static jint |
Eric Laurent | 7070b36 | 2010-07-16 07:43:46 -0700 | [diff] [blame] | 1080 | android_media_AudioTrack_setAuxEffectSendLevel(JNIEnv *env, jobject thiz, jfloat level ) |
| 1081 | { |
Eric Laurent | 532bc1c | 2012-04-20 12:45:03 -0700 | [diff] [blame] | 1082 | sp<AudioTrack> lpTrack = getAudioTrack(env, thiz); |
Eric Laurent | 7070b36 | 2010-07-16 07:43:46 -0700 | [diff] [blame] | 1083 | if (lpTrack == NULL ) { |
| 1084 | jniThrowException(env, "java/lang/IllegalStateException", |
| 1085 | "Unable to retrieve AudioTrack pointer for setAuxEffectSendLevel()"); |
Glenn Kasten | 3009f0b | 2014-03-28 16:02:26 -0700 | [diff] [blame] | 1086 | return -1; |
Eric Laurent | 7070b36 | 2010-07-16 07:43:46 -0700 | [diff] [blame] | 1087 | } |
| 1088 | |
Glenn Kasten | 3009f0b | 2014-03-28 16:02:26 -0700 | [diff] [blame] | 1089 | status_t status = lpTrack->setAuxEffectSendLevel(level); |
| 1090 | if (status != NO_ERROR) { |
| 1091 | ALOGE("AudioTrack::setAuxEffectSendLevel() for level %g failed with status %d", |
| 1092 | level, status); |
| 1093 | } |
| 1094 | return (jint) status; |
Eric Laurent | 7070b36 | 2010-07-16 07:43:46 -0700 | [diff] [blame] | 1095 | } |
| 1096 | |
| 1097 | // ---------------------------------------------------------------------------- |
| 1098 | static jint android_media_AudioTrack_attachAuxEffect(JNIEnv *env, jobject thiz, |
| 1099 | jint effectId) { |
Eric Laurent | 532bc1c | 2012-04-20 12:45:03 -0700 | [diff] [blame] | 1100 | sp<AudioTrack> lpTrack = getAudioTrack(env, thiz); |
| 1101 | if (lpTrack == NULL) { |
Eric Laurent | 7070b36 | 2010-07-16 07:43:46 -0700 | [diff] [blame] | 1102 | jniThrowException(env, "java/lang/IllegalStateException", |
| 1103 | "Unable to retrieve AudioTrack pointer for attachAuxEffect()"); |
Eric Laurent | bc11a69 | 2014-05-16 12:19:25 -0700 | [diff] [blame] | 1104 | return (jint)AUDIO_JAVA_ERROR; |
Eric Laurent | 7070b36 | 2010-07-16 07:43:46 -0700 | [diff] [blame] | 1105 | } |
Eric Laurent | bc11a69 | 2014-05-16 12:19:25 -0700 | [diff] [blame] | 1106 | return nativeToJavaStatus( lpTrack->attachAuxEffect(effectId) ); |
Eric Laurent | 7070b36 | 2010-07-16 07:43:46 -0700 | [diff] [blame] | 1107 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1108 | |
Paul McLean | 88e1d86 | 2015-04-06 16:36:51 -0700 | [diff] [blame] | 1109 | static jboolean android_media_AudioTrack_setOutputDevice( |
| 1110 | JNIEnv *env, jobject thiz, jint device_id) { |
| 1111 | |
| 1112 | sp<AudioTrack> lpTrack = getAudioTrack(env, thiz); |
Paul McLean | cef696e | 2015-05-21 08:51:18 -0700 | [diff] [blame] | 1113 | if (lpTrack == 0) { |
| 1114 | return false; |
| 1115 | } |
Paul McLean | 88e1d86 | 2015-04-06 16:36:51 -0700 | [diff] [blame] | 1116 | return lpTrack->setOutputDevice(device_id) == NO_ERROR; |
| 1117 | } |
| 1118 | |
Eric Laurent | 4bcdba8 | 2015-05-01 11:37:49 -0700 | [diff] [blame] | 1119 | static jint android_media_AudioTrack_getRoutedDeviceId( |
| 1120 | JNIEnv *env, jobject thiz) { |
| 1121 | |
| 1122 | sp<AudioTrack> lpTrack = getAudioTrack(env, thiz); |
| 1123 | if (lpTrack == NULL) { |
| 1124 | return 0; |
| 1125 | } |
| 1126 | return (jint)lpTrack->getRoutedDeviceId(); |
| 1127 | } |
| 1128 | |
| 1129 | static void android_media_AudioTrack_enableDeviceCallback( |
| 1130 | JNIEnv *env, jobject thiz) { |
| 1131 | |
| 1132 | sp<AudioTrack> lpTrack = getAudioTrack(env, thiz); |
| 1133 | if (lpTrack == NULL) { |
| 1134 | return; |
| 1135 | } |
| 1136 | AudioTrackJniStorage* pJniStorage = (AudioTrackJniStorage *)env->GetLongField( |
| 1137 | thiz, javaAudioTrackFields.jniData); |
| 1138 | if (pJniStorage == NULL || pJniStorage->mDeviceCallback != 0) { |
| 1139 | return; |
| 1140 | } |
| 1141 | pJniStorage->mDeviceCallback = |
| 1142 | new JNIDeviceCallback(env, thiz, pJniStorage->mCallbackData.audioTrack_ref, |
| 1143 | javaAudioTrackFields.postNativeEventInJava); |
| 1144 | lpTrack->addAudioDeviceCallback(pJniStorage->mDeviceCallback); |
| 1145 | } |
| 1146 | |
| 1147 | static void android_media_AudioTrack_disableDeviceCallback( |
| 1148 | JNIEnv *env, jobject thiz) { |
| 1149 | |
| 1150 | sp<AudioTrack> lpTrack = getAudioTrack(env, thiz); |
| 1151 | if (lpTrack == NULL) { |
| 1152 | return; |
| 1153 | } |
| 1154 | AudioTrackJniStorage* pJniStorage = (AudioTrackJniStorage *)env->GetLongField( |
| 1155 | thiz, javaAudioTrackFields.jniData); |
| 1156 | if (pJniStorage == NULL || pJniStorage->mDeviceCallback == 0) { |
| 1157 | return; |
| 1158 | } |
| 1159 | lpTrack->removeAudioDeviceCallback(pJniStorage->mDeviceCallback); |
| 1160 | pJniStorage->mDeviceCallback.clear(); |
| 1161 | } |
| 1162 | |
Glenn Kasten | bd2c3d6 | 2015-12-14 12:21:03 -0800 | [diff] [blame] | 1163 | static jint android_media_AudioTrack_get_FCC_8(JNIEnv *env, jobject thiz) { |
| 1164 | return FCC_8; |
| 1165 | } |
| 1166 | |
Eric Laurent | 4bcdba8 | 2015-05-01 11:37:49 -0700 | [diff] [blame] | 1167 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1168 | // ---------------------------------------------------------------------------- |
| 1169 | // ---------------------------------------------------------------------------- |
Daniel Micay | 76f6a86 | 2015-09-19 17:31:01 -0400 | [diff] [blame] | 1170 | static const JNINativeMethod gMethods[] = { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1171 | // name, signature, funcPtr |
| 1172 | {"native_start", "()V", (void *)android_media_AudioTrack_start}, |
| 1173 | {"native_stop", "()V", (void *)android_media_AudioTrack_stop}, |
| 1174 | {"native_pause", "()V", (void *)android_media_AudioTrack_pause}, |
| 1175 | {"native_flush", "()V", (void *)android_media_AudioTrack_flush}, |
Paul McLean | 9b09e53 | 2016-01-26 14:43:35 -0700 | [diff] [blame] | 1176 | {"native_setup", "(Ljava/lang/Object;Ljava/lang/Object;[IIIIII[IJ)I", |
Jean-Michel Trivi | eac8438 | 2014-02-04 14:50:40 -0800 | [diff] [blame] | 1177 | (void *)android_media_AudioTrack_setup}, |
| 1178 | {"native_finalize", "()V", (void *)android_media_AudioTrack_finalize}, |
| 1179 | {"native_release", "()V", (void *)android_media_AudioTrack_release}, |
Andy Hung | 4aacc90 | 2015-04-14 15:01:29 -0700 | [diff] [blame] | 1180 | {"native_write_byte", "([BIIIZ)I",(void *)android_media_AudioTrack_writeArray<jbyteArray>}, |
Jean-Michel Trivi | 7ca0452 | 2014-02-07 09:39:34 -0800 | [diff] [blame] | 1181 | {"native_write_native_bytes", |
Jean-Michel Trivi | 5d21f67 | 2014-03-20 17:02:31 -0700 | [diff] [blame] | 1182 | "(Ljava/lang/Object;IIIZ)I", |
Jean-Michel Trivi | 7ca0452 | 2014-02-07 09:39:34 -0800 | [diff] [blame] | 1183 | (void *)android_media_AudioTrack_write_native_bytes}, |
Andy Hung | 4aacc90 | 2015-04-14 15:01:29 -0700 | [diff] [blame] | 1184 | {"native_write_short", "([SIIIZ)I",(void *)android_media_AudioTrack_writeArray<jshortArray>}, |
| 1185 | {"native_write_float", "([FIIIZ)I",(void *)android_media_AudioTrack_writeArray<jfloatArray>}, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1186 | {"native_setVolume", "(FF)V", (void *)android_media_AudioTrack_set_volume}, |
Phil Burk | 10a33e4 | 2016-01-08 12:40:41 -0800 | [diff] [blame] | 1187 | {"native_get_buffer_size_frames", |
| 1188 | "()I", (void *)android_media_AudioTrack_get_buffer_size_frames}, |
| 1189 | {"native_set_buffer_size_frames", |
| 1190 | "(I)I", (void *)android_media_AudioTrack_set_buffer_size_frames}, |
| 1191 | {"native_get_buffer_capacity_frames", |
| 1192 | "()I", (void *)android_media_AudioTrack_get_buffer_capacity_frames}, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1193 | {"native_set_playback_rate", |
Eric Laurent | 88e209d | 2009-07-07 07:10:45 -0700 | [diff] [blame] | 1194 | "(I)I", (void *)android_media_AudioTrack_set_playback_rate}, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1195 | {"native_get_playback_rate", |
| 1196 | "()I", (void *)android_media_AudioTrack_get_playback_rate}, |
Wei Jia | 2d61e2b | 2015-05-08 15:23:28 -0700 | [diff] [blame] | 1197 | {"native_set_playback_params", |
| 1198 | "(Landroid/media/PlaybackParams;)V", |
| 1199 | (void *)android_media_AudioTrack_set_playback_params}, |
| 1200 | {"native_get_playback_params", |
| 1201 | "()Landroid/media/PlaybackParams;", |
| 1202 | (void *)android_media_AudioTrack_get_playback_params}, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1203 | {"native_set_marker_pos","(I)I", (void *)android_media_AudioTrack_set_marker_pos}, |
| 1204 | {"native_get_marker_pos","()I", (void *)android_media_AudioTrack_get_marker_pos}, |
| 1205 | {"native_set_pos_update_period", |
| 1206 | "(I)I", (void *)android_media_AudioTrack_set_pos_update_period}, |
| 1207 | {"native_get_pos_update_period", |
| 1208 | "()I", (void *)android_media_AudioTrack_get_pos_update_period}, |
| 1209 | {"native_set_position", "(I)I", (void *)android_media_AudioTrack_set_position}, |
| 1210 | {"native_get_position", "()I", (void *)android_media_AudioTrack_get_position}, |
Oliver Woodman | 61dcdf3 | 2013-06-26 12:43:36 +0100 | [diff] [blame] | 1211 | {"native_get_latency", "()I", (void *)android_media_AudioTrack_get_latency}, |
Phil Burk | 03f61bb | 2016-01-17 21:49:58 +0000 | [diff] [blame] | 1212 | {"native_get_underrun_count", "()I", (void *)android_media_AudioTrack_get_underrun_count}, |
Glenn Kasten | 948c2e6 | 2013-09-04 13:51:29 -0700 | [diff] [blame] | 1213 | {"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] | 1214 | {"native_set_loop", "(III)I", (void *)android_media_AudioTrack_set_loop}, |
| 1215 | {"native_reload_static", "()I", (void *)android_media_AudioTrack_reload}, |
| 1216 | {"native_get_output_sample_rate", |
| 1217 | "(I)I", (void *)android_media_AudioTrack_get_output_sample_rate}, |
| 1218 | {"native_get_min_buff_size", |
| 1219 | "(III)I", (void *)android_media_AudioTrack_get_min_buff_size}, |
Eric Laurent | 7070b36 | 2010-07-16 07:43:46 -0700 | [diff] [blame] | 1220 | {"native_setAuxEffectSendLevel", |
Glenn Kasten | 3009f0b | 2014-03-28 16:02:26 -0700 | [diff] [blame] | 1221 | "(F)I", (void *)android_media_AudioTrack_setAuxEffectSendLevel}, |
Eric Laurent | 7070b36 | 2010-07-16 07:43:46 -0700 | [diff] [blame] | 1222 | {"native_attachAuxEffect", |
| 1223 | "(I)I", (void *)android_media_AudioTrack_attachAuxEffect}, |
Paul McLean | 88e1d86 | 2015-04-06 16:36:51 -0700 | [diff] [blame] | 1224 | {"native_setOutputDevice", "(I)Z", |
| 1225 | (void *)android_media_AudioTrack_setOutputDevice}, |
Eric Laurent | 4bcdba8 | 2015-05-01 11:37:49 -0700 | [diff] [blame] | 1226 | {"native_getRoutedDeviceId", "()I", (void *)android_media_AudioTrack_getRoutedDeviceId}, |
| 1227 | {"native_enableDeviceCallback", "()V", (void *)android_media_AudioTrack_enableDeviceCallback}, |
| 1228 | {"native_disableDeviceCallback", "()V", (void *)android_media_AudioTrack_disableDeviceCallback}, |
Glenn Kasten | 25d3c7c | 2016-01-07 10:59:35 -0800 | [diff] [blame] | 1229 | {"native_get_FCC_8", "()I", (void *)android_media_AudioTrack_get_FCC_8}, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1230 | }; |
| 1231 | |
| 1232 | |
| 1233 | // field names found in android/media/AudioTrack.java |
| 1234 | #define JAVA_POSTEVENT_CALLBACK_NAME "postEventFromNative" |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1235 | #define JAVA_NATIVETRACKINJAVAOBJ_FIELD_NAME "mNativeTrackInJavaObj" |
| 1236 | #define JAVA_JNIDATA_FIELD_NAME "mJniData" |
Jean-Michel Trivi | a1d80e3 | 2014-06-18 08:18:41 -0700 | [diff] [blame] | 1237 | #define JAVA_STREAMTYPE_FIELD_NAME "mStreamType" |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1238 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1239 | // ---------------------------------------------------------------------------- |
| 1240 | // preconditions: |
| 1241 | // theClass is valid |
| 1242 | bool android_media_getIntConstantFromClass(JNIEnv* pEnv, jclass theClass, const char* className, |
| 1243 | const char* constName, int* constVal) { |
| 1244 | jfieldID javaConst = NULL; |
| 1245 | javaConst = pEnv->GetStaticFieldID(theClass, constName, "I"); |
| 1246 | if (javaConst != NULL) { |
| 1247 | *constVal = pEnv->GetStaticIntField(theClass, javaConst); |
| 1248 | return true; |
| 1249 | } else { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 1250 | ALOGE("Can't find %s.%s", className, constName); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1251 | return false; |
| 1252 | } |
| 1253 | } |
| 1254 | |
| 1255 | |
| 1256 | // ---------------------------------------------------------------------------- |
| 1257 | int register_android_media_AudioTrack(JNIEnv *env) |
| 1258 | { |
Glenn Kasten | 931fde4 | 2016-01-07 15:59:38 -0800 | [diff] [blame] | 1259 | // must be first |
| 1260 | int res = RegisterMethodsOrDie(env, kClassPathName, gMethods, NELEM(gMethods)); |
| 1261 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1262 | javaAudioTrackFields.nativeTrackInJavaObj = NULL; |
| 1263 | javaAudioTrackFields.postNativeEventInJava = NULL; |
| 1264 | |
| 1265 | // Get the AudioTrack class |
Andreas Gampe | ed6b9df | 2014-11-20 22:02:20 -0800 | [diff] [blame] | 1266 | jclass audioTrackClass = FindClassOrDie(env, kClassPathName); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1267 | |
| 1268 | // Get the postEvent method |
Andreas Gampe | ed6b9df | 2014-11-20 22:02:20 -0800 | [diff] [blame] | 1269 | javaAudioTrackFields.postNativeEventInJava = GetStaticMethodIDOrDie(env, |
| 1270 | audioTrackClass, JAVA_POSTEVENT_CALLBACK_NAME, |
| 1271 | "(Ljava/lang/Object;IIILjava/lang/Object;)V"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1272 | |
| 1273 | // Get the variables fields |
| 1274 | // nativeTrackInJavaObj |
Andreas Gampe | ed6b9df | 2014-11-20 22:02:20 -0800 | [diff] [blame] | 1275 | javaAudioTrackFields.nativeTrackInJavaObj = GetFieldIDOrDie(env, |
| 1276 | audioTrackClass, JAVA_NATIVETRACKINJAVAOBJ_FIELD_NAME, "J"); |
Jean-Michel Trivi | a1d80e3 | 2014-06-18 08:18:41 -0700 | [diff] [blame] | 1277 | // jniData |
Andreas Gampe | ed6b9df | 2014-11-20 22:02:20 -0800 | [diff] [blame] | 1278 | javaAudioTrackFields.jniData = GetFieldIDOrDie(env, |
| 1279 | audioTrackClass, JAVA_JNIDATA_FIELD_NAME, "J"); |
Jean-Michel Trivi | a1d80e3 | 2014-06-18 08:18:41 -0700 | [diff] [blame] | 1280 | // fieldStreamType |
Andreas Gampe | ed6b9df | 2014-11-20 22:02:20 -0800 | [diff] [blame] | 1281 | javaAudioTrackFields.fieldStreamType = GetFieldIDOrDie(env, |
| 1282 | audioTrackClass, JAVA_STREAMTYPE_FIELD_NAME, "I"); |
Jean-Michel Trivi | a1d80e3 | 2014-06-18 08:18:41 -0700 | [diff] [blame] | 1283 | |
Andy Hung | fe48e0d | 2015-04-27 18:14:02 -0700 | [diff] [blame] | 1284 | env->DeleteLocalRef(audioTrackClass); |
| 1285 | |
Jean-Michel Trivi | a1d80e3 | 2014-06-18 08:18:41 -0700 | [diff] [blame] | 1286 | // Get the AudioAttributes class and fields |
Andreas Gampe | ed6b9df | 2014-11-20 22:02:20 -0800 | [diff] [blame] | 1287 | jclass audioAttrClass = FindClassOrDie(env, kAudioAttributesClassPathName); |
| 1288 | javaAudioAttrFields.fieldUsage = GetFieldIDOrDie(env, audioAttrClass, "mUsage", "I"); |
| 1289 | javaAudioAttrFields.fieldContentType = GetFieldIDOrDie(env, |
| 1290 | audioAttrClass, "mContentType", "I"); |
| 1291 | javaAudioAttrFields.fieldFlags = GetFieldIDOrDie(env, audioAttrClass, "mFlags", "I"); |
| 1292 | javaAudioAttrFields.fieldFormattedTags = GetFieldIDOrDie(env, |
| 1293 | audioAttrClass, "mFormattedTags", "Ljava/lang/String;"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1294 | |
Andy Hung | fe48e0d | 2015-04-27 18:14:02 -0700 | [diff] [blame] | 1295 | env->DeleteLocalRef(audioAttrClass); |
| 1296 | |
Wei Jia | 2d61e2b | 2015-05-08 15:23:28 -0700 | [diff] [blame] | 1297 | // initialize PlaybackParams field info |
| 1298 | gPlaybackParamsFields.init(env); |
Andy Hung | fe48e0d | 2015-04-27 18:14:02 -0700 | [diff] [blame] | 1299 | |
Glenn Kasten | 931fde4 | 2016-01-07 15:59:38 -0800 | [diff] [blame] | 1300 | return res; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1301 | } |
| 1302 | |
| 1303 | |
| 1304 | // ---------------------------------------------------------------------------- |