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