Glenn Kasten | 362c4e6 | 2011-12-14 10:28:06 -0800 | [diff] [blame] | 1 | /* frameworks/base/media/libmedia/AudioTrack.cpp |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2 | ** |
| 3 | ** Copyright 2007, The Android Open Source Project |
| 4 | ** |
| 5 | ** Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | ** you may not use this file except in compliance with the License. |
| 7 | ** You may obtain a copy of the License at |
| 8 | ** |
| 9 | ** http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | ** |
| 11 | ** Unless required by applicable law or agreed to in writing, software |
| 12 | ** distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | ** See the License for the specific language governing permissions and |
| 15 | ** limitations under the License. |
| 16 | */ |
| 17 | |
| 18 | |
| 19 | //#define LOG_NDEBUG 0 |
| 20 | #define LOG_TAG "AudioTrack" |
| 21 | |
| 22 | #include <stdint.h> |
| 23 | #include <sys/types.h> |
| 24 | #include <limits.h> |
| 25 | |
| 26 | #include <sched.h> |
| 27 | #include <sys/resource.h> |
| 28 | |
| 29 | #include <private/media/AudioTrackShared.h> |
| 30 | |
| 31 | #include <media/AudioSystem.h> |
| 32 | #include <media/AudioTrack.h> |
| 33 | |
| 34 | #include <utils/Log.h> |
Mathias Agopian | 7562408 | 2009-05-19 19:08:10 -0700 | [diff] [blame] | 35 | #include <binder/Parcel.h> |
| 36 | #include <binder/IPCThreadState.h> |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 37 | #include <utils/Timers.h> |
Eric Laurent | 38ccae2 | 2011-03-28 18:37:07 -0700 | [diff] [blame] | 38 | #include <utils/Atomic.h> |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 39 | |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 40 | #include <cutils/bitops.h> |
Glenn Kasten | f6b1678 | 2011-12-15 09:51:17 -0800 | [diff] [blame] | 41 | #include <cutils/compiler.h> |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 42 | |
Dima Zavin | 6476024 | 2011-05-11 14:15:23 -0700 | [diff] [blame] | 43 | #include <system/audio.h> |
Dima Zavin | 7394a4f | 2011-06-13 18:16:26 -0700 | [diff] [blame] | 44 | #include <system/audio_policy.h> |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 45 | |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 46 | namespace android { |
Chia-chi Yeh | 33005a9 | 2010-06-16 06:33:13 +0800 | [diff] [blame] | 47 | // --------------------------------------------------------------------------- |
| 48 | |
| 49 | // static |
| 50 | status_t AudioTrack::getMinFrameCount( |
| 51 | int* frameCount, |
Glenn Kasten | fff6d71 | 2012-01-12 16:38:12 -0800 | [diff] [blame] | 52 | audio_stream_type_t streamType, |
Chia-chi Yeh | 33005a9 | 2010-06-16 06:33:13 +0800 | [diff] [blame] | 53 | uint32_t sampleRate) |
| 54 | { |
| 55 | int afSampleRate; |
| 56 | if (AudioSystem::getOutputSamplingRate(&afSampleRate, streamType) != NO_ERROR) { |
| 57 | return NO_INIT; |
| 58 | } |
| 59 | int afFrameCount; |
| 60 | if (AudioSystem::getOutputFrameCount(&afFrameCount, streamType) != NO_ERROR) { |
| 61 | return NO_INIT; |
| 62 | } |
| 63 | uint32_t afLatency; |
| 64 | if (AudioSystem::getOutputLatency(&afLatency, streamType) != NO_ERROR) { |
| 65 | return NO_INIT; |
| 66 | } |
| 67 | |
| 68 | // Ensure that buffer depth covers at least audio hardware latency |
| 69 | uint32_t minBufCount = afLatency / ((1000 * afFrameCount) / afSampleRate); |
| 70 | if (minBufCount < 2) minBufCount = 2; |
| 71 | |
| 72 | *frameCount = (sampleRate == 0) ? afFrameCount * minBufCount : |
| 73 | afFrameCount * minBufCount * sampleRate / afSampleRate; |
| 74 | return NO_ERROR; |
| 75 | } |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 76 | |
| 77 | // --------------------------------------------------------------------------- |
| 78 | |
| 79 | AudioTrack::AudioTrack() |
Glenn Kasten | 8791351 | 2011-06-22 16:15:25 -0700 | [diff] [blame] | 80 | : mStatus(NO_INIT), |
| 81 | mPreviousPriority(ANDROID_PRIORITY_NORMAL), mPreviousSchedulingGroup(ANDROID_TGROUP_DEFAULT) |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 82 | { |
| 83 | } |
| 84 | |
| 85 | AudioTrack::AudioTrack( |
Glenn Kasten | fff6d71 | 2012-01-12 16:38:12 -0800 | [diff] [blame] | 86 | audio_stream_type_t streamType, |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 87 | uint32_t sampleRate, |
Glenn Kasten | e1c3962 | 2012-01-04 09:36:37 -0800 | [diff] [blame] | 88 | audio_format_t format, |
Jean-Michel Trivi | 0d255b2 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 89 | int channelMask, |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 90 | int frameCount, |
| 91 | uint32_t flags, |
| 92 | callback_t cbf, |
| 93 | void* user, |
Eric Laurent | be916aa | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 94 | int notificationFrames, |
| 95 | int sessionId) |
Glenn Kasten | 8791351 | 2011-06-22 16:15:25 -0700 | [diff] [blame] | 96 | : mStatus(NO_INIT), |
| 97 | mPreviousPriority(ANDROID_PRIORITY_NORMAL), mPreviousSchedulingGroup(ANDROID_TGROUP_DEFAULT) |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 98 | { |
Jean-Michel Trivi | 0d255b2 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 99 | mStatus = set(streamType, sampleRate, format, channelMask, |
Eric Laurent | a514bdb | 2010-06-21 09:27:30 -0700 | [diff] [blame] | 100 | frameCount, flags, cbf, user, notificationFrames, |
| 101 | 0, false, sessionId); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | AudioTrack::AudioTrack( |
Andreas Huber | c813985 | 2012-01-18 10:51:55 -0800 | [diff] [blame^] | 105 | int streamType, |
| 106 | uint32_t sampleRate, |
| 107 | int format, |
| 108 | int channelMask, |
| 109 | int frameCount, |
| 110 | uint32_t flags, |
| 111 | callback_t cbf, |
| 112 | void* user, |
| 113 | int notificationFrames, |
| 114 | int sessionId) |
| 115 | : mStatus(NO_INIT), |
| 116 | mPreviousPriority(ANDROID_PRIORITY_NORMAL), mPreviousSchedulingGroup(ANDROID_TGROUP_DEFAULT) |
| 117 | { |
| 118 | mStatus = set((audio_stream_type_t)streamType, sampleRate, (audio_format_t)format, channelMask, |
| 119 | frameCount, flags, cbf, user, notificationFrames, |
| 120 | 0, false, sessionId); |
| 121 | } |
| 122 | |
| 123 | AudioTrack::AudioTrack( |
Glenn Kasten | fff6d71 | 2012-01-12 16:38:12 -0800 | [diff] [blame] | 124 | audio_stream_type_t streamType, |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 125 | uint32_t sampleRate, |
Glenn Kasten | e1c3962 | 2012-01-04 09:36:37 -0800 | [diff] [blame] | 126 | audio_format_t format, |
Jean-Michel Trivi | 0d255b2 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 127 | int channelMask, |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 128 | const sp<IMemory>& sharedBuffer, |
| 129 | uint32_t flags, |
| 130 | callback_t cbf, |
| 131 | void* user, |
Eric Laurent | be916aa | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 132 | int notificationFrames, |
| 133 | int sessionId) |
Glenn Kasten | 8791351 | 2011-06-22 16:15:25 -0700 | [diff] [blame] | 134 | : mStatus(NO_INIT), |
| 135 | mPreviousPriority(ANDROID_PRIORITY_NORMAL), mPreviousSchedulingGroup(ANDROID_TGROUP_DEFAULT) |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 136 | { |
Jean-Michel Trivi | 0d255b2 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 137 | mStatus = set(streamType, sampleRate, format, channelMask, |
Eric Laurent | a514bdb | 2010-06-21 09:27:30 -0700 | [diff] [blame] | 138 | 0, flags, cbf, user, notificationFrames, |
| 139 | sharedBuffer, false, sessionId); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 140 | } |
| 141 | |
| 142 | AudioTrack::~AudioTrack() |
| 143 | { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 144 | ALOGV_IF(mSharedBuffer != 0, "Destructor sharedBuffer: %p", mSharedBuffer->pointer()); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 145 | |
| 146 | if (mStatus == NO_ERROR) { |
| 147 | // Make sure that callback function exits in the case where |
| 148 | // it is looping on buffer full condition in obtainBuffer(). |
| 149 | // Otherwise the callback thread will never exit. |
| 150 | stop(); |
| 151 | if (mAudioTrackThread != 0) { |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 152 | mAudioTrackThread->requestExitAndWait(); |
| 153 | mAudioTrackThread.clear(); |
| 154 | } |
| 155 | mAudioTrack.clear(); |
| 156 | IPCThreadState::self()->flushCommands(); |
Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 157 | AudioSystem::releaseAudioSessionId(mSessionId); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 158 | } |
| 159 | } |
| 160 | |
| 161 | status_t AudioTrack::set( |
Glenn Kasten | fff6d71 | 2012-01-12 16:38:12 -0800 | [diff] [blame] | 162 | audio_stream_type_t streamType, |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 163 | uint32_t sampleRate, |
Glenn Kasten | e1c3962 | 2012-01-04 09:36:37 -0800 | [diff] [blame] | 164 | audio_format_t format, |
Jean-Michel Trivi | 0d255b2 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 165 | int channelMask, |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 166 | int frameCount, |
| 167 | uint32_t flags, |
| 168 | callback_t cbf, |
| 169 | void* user, |
| 170 | int notificationFrames, |
| 171 | const sp<IMemory>& sharedBuffer, |
Eric Laurent | be916aa | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 172 | bool threadCanCallJava, |
| 173 | int sessionId) |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 174 | { |
| 175 | |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 176 | ALOGV_IF(sharedBuffer != 0, "sharedBuffer: %p, size: %d", sharedBuffer->pointer(), sharedBuffer->size()); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 177 | |
Eric Laurent | 1703cdf | 2011-03-07 14:52:59 -0800 | [diff] [blame] | 178 | AutoMutex lock(mLock); |
Eric Laurent | 1dd70b9 | 2009-04-21 07:56:33 -0700 | [diff] [blame] | 179 | if (mAudioTrack != 0) { |
Steve Block | 29357bc | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 180 | ALOGE("Track already in use"); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 181 | return INVALID_OPERATION; |
| 182 | } |
| 183 | |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 184 | int afSampleRate; |
| 185 | if (AudioSystem::getOutputSamplingRate(&afSampleRate, streamType) != NO_ERROR) { |
| 186 | return NO_INIT; |
| 187 | } |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 188 | uint32_t afLatency; |
| 189 | if (AudioSystem::getOutputLatency(&afLatency, streamType) != NO_ERROR) { |
| 190 | return NO_INIT; |
| 191 | } |
| 192 | |
| 193 | // handle default values first. |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 194 | if (streamType == AUDIO_STREAM_DEFAULT) { |
| 195 | streamType = AUDIO_STREAM_MUSIC; |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 196 | } |
| 197 | if (sampleRate == 0) { |
| 198 | sampleRate = afSampleRate; |
| 199 | } |
| 200 | // these below should probably come from the audioFlinger too... |
Glenn Kasten | e1c3962 | 2012-01-04 09:36:37 -0800 | [diff] [blame] | 201 | if (format == AUDIO_FORMAT_DEFAULT) { |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 202 | format = AUDIO_FORMAT_PCM_16_BIT; |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 203 | } |
Jean-Michel Trivi | 0d255b2 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 204 | if (channelMask == 0) { |
| 205 | channelMask = AUDIO_CHANNEL_OUT_STEREO; |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 206 | } |
| 207 | |
| 208 | // validate parameters |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 209 | if (!audio_is_valid_format(format)) { |
Steve Block | 29357bc | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 210 | ALOGE("Invalid format"); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 211 | return BAD_VALUE; |
| 212 | } |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 213 | |
| 214 | // force direct flag if format is not linear PCM |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 215 | if (!audio_is_linear_pcm(format)) { |
| 216 | flags |= AUDIO_POLICY_OUTPUT_FLAG_DIRECT; |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 217 | } |
| 218 | |
Jean-Michel Trivi | 0d255b2 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 219 | if (!audio_is_output_channel(channelMask)) { |
Steve Block | 29357bc | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 220 | ALOGE("Invalid channel mask"); |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 221 | return BAD_VALUE; |
| 222 | } |
Jean-Michel Trivi | 0d255b2 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 223 | uint32_t channelCount = popcount(channelMask); |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 224 | |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 225 | audio_io_handle_t output = AudioSystem::getOutput( |
Glenn Kasten | fff6d71 | 2012-01-12 16:38:12 -0800 | [diff] [blame] | 226 | streamType, |
Glenn Kasten | e1c3962 | 2012-01-04 09:36:37 -0800 | [diff] [blame] | 227 | sampleRate, format, channelMask, |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 228 | (audio_policy_output_flags_t)flags); |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 229 | |
| 230 | if (output == 0) { |
Steve Block | 29357bc | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 231 | ALOGE("Could not get audio output for stream type %d", streamType); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 232 | return BAD_VALUE; |
| 233 | } |
| 234 | |
Eric Laurent | 34f1d8e | 2009-11-04 08:27:26 -0800 | [diff] [blame] | 235 | mVolume[LEFT] = 1.0f; |
| 236 | mVolume[RIGHT] = 1.0f; |
Glenn Kasten | 05632a5 | 2012-01-03 14:22:33 -0800 | [diff] [blame] | 237 | mSendLevel = 0.0f; |
Eric Laurent | d1b449a | 2010-05-14 03:26:45 -0700 | [diff] [blame] | 238 | mFrameCount = frameCount; |
| 239 | mNotificationFramesReq = notificationFrames; |
Eric Laurent | be916aa | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 240 | mSessionId = sessionId; |
Eric Laurent | 2beeb50 | 2010-07-16 07:43:46 -0700 | [diff] [blame] | 241 | mAuxEffectId = 0; |
Eric Laurent | be916aa | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 242 | |
Eric Laurent | 34f1d8e | 2009-11-04 08:27:26 -0800 | [diff] [blame] | 243 | // create the IAudioTrack |
Eric Laurent | 1703cdf | 2011-03-07 14:52:59 -0800 | [diff] [blame] | 244 | status_t status = createTrack_l(streamType, |
| 245 | sampleRate, |
Glenn Kasten | e1c3962 | 2012-01-04 09:36:37 -0800 | [diff] [blame] | 246 | format, |
Jean-Michel Trivi | 0d255b2 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 247 | (uint32_t)channelMask, |
Eric Laurent | 1703cdf | 2011-03-07 14:52:59 -0800 | [diff] [blame] | 248 | frameCount, |
| 249 | flags, |
| 250 | sharedBuffer, |
| 251 | output, |
| 252 | true); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 253 | |
Eric Laurent | 34f1d8e | 2009-11-04 08:27:26 -0800 | [diff] [blame] | 254 | if (status != NO_ERROR) { |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 255 | return status; |
| 256 | } |
Eric Laurent | 34f1d8e | 2009-11-04 08:27:26 -0800 | [diff] [blame] | 257 | |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 258 | if (cbf != 0) { |
| 259 | mAudioTrackThread = new AudioTrackThread(*this, threadCanCallJava); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 260 | } |
| 261 | |
| 262 | mStatus = NO_ERROR; |
| 263 | |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 264 | mStreamType = streamType; |
Glenn Kasten | e1c3962 | 2012-01-04 09:36:37 -0800 | [diff] [blame] | 265 | mFormat = format; |
Jean-Michel Trivi | 0d255b2 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 266 | mChannelMask = (uint32_t)channelMask; |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 267 | mChannelCount = channelCount; |
| 268 | mSharedBuffer = sharedBuffer; |
| 269 | mMuted = false; |
Glenn Kasten | 9a2aaf9 | 2012-01-03 09:42:47 -0800 | [diff] [blame] | 270 | mActive = false; |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 271 | mCbf = cbf; |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 272 | mUserData = user; |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 273 | mLoopCount = 0; |
| 274 | mMarkerPosition = 0; |
Jean-Michel Trivi | 2c22aeb | 2009-03-24 18:11:07 -0700 | [diff] [blame] | 275 | mMarkerReached = false; |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 276 | mNewPosition = 0; |
| 277 | mUpdatePeriod = 0; |
Jean-Michel Trivi | cd07594 | 2011-08-25 16:47:23 -0700 | [diff] [blame] | 278 | mFlushed = false; |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 279 | mFlags = flags; |
Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 280 | AudioSystem::acquireAudioSessionId(mSessionId); |
Eric Laurent | cfe2ba6 | 2011-09-13 15:04:17 -0700 | [diff] [blame] | 281 | mRestoreStatus = NO_ERROR; |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 282 | return NO_ERROR; |
| 283 | } |
| 284 | |
| 285 | status_t AudioTrack::initCheck() const |
| 286 | { |
| 287 | return mStatus; |
| 288 | } |
| 289 | |
| 290 | // ------------------------------------------------------------------------- |
| 291 | |
| 292 | uint32_t AudioTrack::latency() const |
| 293 | { |
| 294 | return mLatency; |
| 295 | } |
| 296 | |
Glenn Kasten | fff6d71 | 2012-01-12 16:38:12 -0800 | [diff] [blame] | 297 | audio_stream_type_t AudioTrack::streamType() const |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 298 | { |
| 299 | return mStreamType; |
| 300 | } |
| 301 | |
Glenn Kasten | e1c3962 | 2012-01-04 09:36:37 -0800 | [diff] [blame] | 302 | audio_format_t AudioTrack::format() const |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 303 | { |
| 304 | return mFormat; |
| 305 | } |
| 306 | |
| 307 | int AudioTrack::channelCount() const |
| 308 | { |
| 309 | return mChannelCount; |
| 310 | } |
| 311 | |
| 312 | uint32_t AudioTrack::frameCount() const |
| 313 | { |
Eric Laurent | d1b449a | 2010-05-14 03:26:45 -0700 | [diff] [blame] | 314 | return mCblk->frameCount; |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 315 | } |
| 316 | |
Glenn Kasten | b998065 | 2012-01-11 09:48:27 -0800 | [diff] [blame] | 317 | size_t AudioTrack::frameSize() const |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 318 | { |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 319 | if (audio_is_linear_pcm(mFormat)) { |
Eric Laurent | 671a636 | 2011-06-16 21:30:45 -0700 | [diff] [blame] | 320 | return channelCount()*audio_bytes_per_sample(mFormat); |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 321 | } else { |
| 322 | return sizeof(uint8_t); |
| 323 | } |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 324 | } |
| 325 | |
| 326 | sp<IMemory>& AudioTrack::sharedBuffer() |
| 327 | { |
| 328 | return mSharedBuffer; |
| 329 | } |
| 330 | |
| 331 | // ------------------------------------------------------------------------- |
| 332 | |
| 333 | void AudioTrack::start() |
| 334 | { |
| 335 | sp<AudioTrackThread> t = mAudioTrackThread; |
Glenn Kasten | d0965dd | 2011-06-22 16:18:04 -0700 | [diff] [blame] | 336 | status_t status = NO_ERROR; |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 337 | |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 338 | ALOGV("start %p", this); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 339 | if (t != 0) { |
| 340 | if (t->exitPending()) { |
| 341 | if (t->requestExitAndWait() == WOULD_BLOCK) { |
Steve Block | 29357bc | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 342 | ALOGE("AudioTrack::start called from thread"); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 343 | return; |
| 344 | } |
| 345 | } |
| 346 | t->mLock.lock(); |
| 347 | } |
| 348 | |
Eric Laurent | f5aafb2 | 2010-11-18 08:40:16 -0800 | [diff] [blame] | 349 | AutoMutex lock(mLock); |
Eric Laurent | 1703cdf | 2011-03-07 14:52:59 -0800 | [diff] [blame] | 350 | // acquire a strong reference on the IMemory and IAudioTrack so that they cannot be destroyed |
| 351 | // while we are accessing the cblk |
| 352 | sp <IAudioTrack> audioTrack = mAudioTrack; |
| 353 | sp <IMemory> iMem = mCblkMemory; |
| 354 | audio_track_cblk_t* cblk = mCblk; |
| 355 | |
Glenn Kasten | 9a2aaf9 | 2012-01-03 09:42:47 -0800 | [diff] [blame] | 356 | if (!mActive) { |
Jean-Michel Trivi | cd07594 | 2011-08-25 16:47:23 -0700 | [diff] [blame] | 357 | mFlushed = false; |
Glenn Kasten | 9a2aaf9 | 2012-01-03 09:42:47 -0800 | [diff] [blame] | 358 | mActive = true; |
Eric Laurent | 1703cdf | 2011-03-07 14:52:59 -0800 | [diff] [blame] | 359 | mNewPosition = cblk->server + mUpdatePeriod; |
Eric Laurent | 33797ea | 2011-03-17 09:36:51 -0700 | [diff] [blame] | 360 | cblk->lock.lock(); |
Eric Laurent | 1703cdf | 2011-03-07 14:52:59 -0800 | [diff] [blame] | 361 | cblk->bufferTimeoutMs = MAX_STARTUP_TIMEOUT_MS; |
| 362 | cblk->waitTimeMs = 0; |
Eric Laurent | 38ccae2 | 2011-03-28 18:37:07 -0700 | [diff] [blame] | 363 | android_atomic_and(~CBLK_DISABLED_ON, &cblk->flags); |
Eric Laurent | 2b58424 | 2009-11-09 23:32:22 -0800 | [diff] [blame] | 364 | if (t != 0) { |
Glenn Kasten | 8791351 | 2011-06-22 16:15:25 -0700 | [diff] [blame] | 365 | t->run("AudioTrackThread", ANDROID_PRIORITY_AUDIO); |
Eric Laurent | 2b58424 | 2009-11-09 23:32:22 -0800 | [diff] [blame] | 366 | } else { |
Glenn Kasten | 8791351 | 2011-06-22 16:15:25 -0700 | [diff] [blame] | 367 | mPreviousPriority = getpriority(PRIO_PROCESS, 0); |
| 368 | mPreviousSchedulingGroup = androidGetThreadSchedulingGroup(0); |
| 369 | androidSetThreadPriority(0, ANDROID_PRIORITY_AUDIO); |
Eric Laurent | 2b58424 | 2009-11-09 23:32:22 -0800 | [diff] [blame] | 370 | } |
| 371 | |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 372 | ALOGV("start %p before lock cblk %p", this, mCblk); |
Eric Laurent | 1703cdf | 2011-03-07 14:52:59 -0800 | [diff] [blame] | 373 | if (!(cblk->flags & CBLK_INVALID_MSK)) { |
| 374 | cblk->lock.unlock(); |
Eric Laurent | d1b449a | 2010-05-14 03:26:45 -0700 | [diff] [blame] | 375 | status = mAudioTrack->start(); |
Eric Laurent | 1703cdf | 2011-03-07 14:52:59 -0800 | [diff] [blame] | 376 | cblk->lock.lock(); |
| 377 | if (status == DEAD_OBJECT) { |
Eric Laurent | 38ccae2 | 2011-03-28 18:37:07 -0700 | [diff] [blame] | 378 | android_atomic_or(CBLK_INVALID_ON, &cblk->flags); |
Eric Laurent | 6100d2d | 2009-11-19 09:00:56 -0800 | [diff] [blame] | 379 | } |
Eric Laurent | 2b58424 | 2009-11-09 23:32:22 -0800 | [diff] [blame] | 380 | } |
Eric Laurent | 1703cdf | 2011-03-07 14:52:59 -0800 | [diff] [blame] | 381 | if (cblk->flags & CBLK_INVALID_MSK) { |
| 382 | status = restoreTrack_l(cblk, true); |
| 383 | } |
| 384 | cblk->lock.unlock(); |
Eric Laurent | 2b58424 | 2009-11-09 23:32:22 -0800 | [diff] [blame] | 385 | if (status != NO_ERROR) { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 386 | ALOGV("start() failed"); |
Glenn Kasten | 9a2aaf9 | 2012-01-03 09:42:47 -0800 | [diff] [blame] | 387 | mActive = false; |
Eric Laurent | 2b58424 | 2009-11-09 23:32:22 -0800 | [diff] [blame] | 388 | if (t != 0) { |
| 389 | t->requestExit(); |
| 390 | } else { |
Glenn Kasten | 8791351 | 2011-06-22 16:15:25 -0700 | [diff] [blame] | 391 | setpriority(PRIO_PROCESS, 0, mPreviousPriority); |
| 392 | androidSetThreadSchedulingGroup(0, mPreviousSchedulingGroup); |
Eric Laurent | 2b58424 | 2009-11-09 23:32:22 -0800 | [diff] [blame] | 393 | } |
Eric Laurent | 34f1d8e | 2009-11-04 08:27:26 -0800 | [diff] [blame] | 394 | } |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 395 | } |
| 396 | |
| 397 | if (t != 0) { |
| 398 | t->mLock.unlock(); |
| 399 | } |
| 400 | } |
| 401 | |
| 402 | void AudioTrack::stop() |
| 403 | { |
| 404 | sp<AudioTrackThread> t = mAudioTrackThread; |
| 405 | |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 406 | ALOGV("stop %p", this); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 407 | if (t != 0) { |
| 408 | t->mLock.lock(); |
| 409 | } |
| 410 | |
Eric Laurent | f5aafb2 | 2010-11-18 08:40:16 -0800 | [diff] [blame] | 411 | AutoMutex lock(mLock); |
Glenn Kasten | 9a2aaf9 | 2012-01-03 09:42:47 -0800 | [diff] [blame] | 412 | if (mActive) { |
| 413 | mActive = false; |
Eric Laurent | 1dd70b9 | 2009-04-21 07:56:33 -0700 | [diff] [blame] | 414 | mCblk->cv.signal(); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 415 | mAudioTrack->stop(); |
| 416 | // Cancel loops (If we are in the middle of a loop, playback |
| 417 | // would not stop until loopCount reaches 0). |
Eric Laurent | 1703cdf | 2011-03-07 14:52:59 -0800 | [diff] [blame] | 418 | setLoop_l(0, 0, 0); |
Jean-Michel Trivi | 2c22aeb | 2009-03-24 18:11:07 -0700 | [diff] [blame] | 419 | // the playback head position will reset to 0, so if a marker is set, we need |
| 420 | // to activate it again |
| 421 | mMarkerReached = false; |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 422 | // Force flush if a shared buffer is used otherwise audioflinger |
| 423 | // will not stop before end of buffer is reached. |
| 424 | if (mSharedBuffer != 0) { |
Eric Laurent | 1703cdf | 2011-03-07 14:52:59 -0800 | [diff] [blame] | 425 | flush_l(); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 426 | } |
| 427 | if (t != 0) { |
| 428 | t->requestExit(); |
| 429 | } else { |
Glenn Kasten | 8791351 | 2011-06-22 16:15:25 -0700 | [diff] [blame] | 430 | setpriority(PRIO_PROCESS, 0, mPreviousPriority); |
| 431 | androidSetThreadSchedulingGroup(0, mPreviousSchedulingGroup); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 432 | } |
| 433 | } |
| 434 | |
| 435 | if (t != 0) { |
| 436 | t->mLock.unlock(); |
| 437 | } |
| 438 | } |
| 439 | |
| 440 | bool AudioTrack::stopped() const |
| 441 | { |
Glenn Kasten | 9a2aaf9 | 2012-01-03 09:42:47 -0800 | [diff] [blame] | 442 | AutoMutex lock(mLock); |
| 443 | return stopped_l(); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 444 | } |
| 445 | |
| 446 | void AudioTrack::flush() |
| 447 | { |
Eric Laurent | 1703cdf | 2011-03-07 14:52:59 -0800 | [diff] [blame] | 448 | AutoMutex lock(mLock); |
| 449 | flush_l(); |
| 450 | } |
| 451 | |
| 452 | // must be called with mLock held |
| 453 | void AudioTrack::flush_l() |
| 454 | { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 455 | ALOGV("flush"); |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 456 | |
Jean-Michel Trivi | 2c22aeb | 2009-03-24 18:11:07 -0700 | [diff] [blame] | 457 | // clear playback marker and periodic update counter |
| 458 | mMarkerPosition = 0; |
| 459 | mMarkerReached = false; |
| 460 | mUpdatePeriod = 0; |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 461 | |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 462 | if (!mActive) { |
Jean-Michel Trivi | cd07594 | 2011-08-25 16:47:23 -0700 | [diff] [blame] | 463 | mFlushed = true; |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 464 | mAudioTrack->flush(); |
| 465 | // Release AudioTrack callback thread in case it was waiting for new buffers |
| 466 | // in AudioTrack::obtainBuffer() |
| 467 | mCblk->cv.signal(); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 468 | } |
| 469 | } |
| 470 | |
| 471 | void AudioTrack::pause() |
| 472 | { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 473 | ALOGV("pause"); |
Eric Laurent | f5aafb2 | 2010-11-18 08:40:16 -0800 | [diff] [blame] | 474 | AutoMutex lock(mLock); |
Glenn Kasten | 9a2aaf9 | 2012-01-03 09:42:47 -0800 | [diff] [blame] | 475 | if (mActive) { |
| 476 | mActive = false; |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 477 | mAudioTrack->pause(); |
| 478 | } |
| 479 | } |
| 480 | |
| 481 | void AudioTrack::mute(bool e) |
| 482 | { |
| 483 | mAudioTrack->mute(e); |
| 484 | mMuted = e; |
| 485 | } |
| 486 | |
| 487 | bool AudioTrack::muted() const |
| 488 | { |
| 489 | return mMuted; |
| 490 | } |
| 491 | |
Eric Laurent | be916aa | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 492 | status_t AudioTrack::setVolume(float left, float right) |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 493 | { |
Glenn Kasten | f0c4950 | 2011-11-30 09:46:04 -0800 | [diff] [blame] | 494 | if (left < 0.0f || left > 1.0f || right < 0.0f || right > 1.0f) { |
Eric Laurent | be916aa | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 495 | return BAD_VALUE; |
| 496 | } |
| 497 | |
Eric Laurent | 1703cdf | 2011-03-07 14:52:59 -0800 | [diff] [blame] | 498 | AutoMutex lock(mLock); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 499 | mVolume[LEFT] = left; |
| 500 | mVolume[RIGHT] = right; |
| 501 | |
Eric Laurent | be916aa | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 502 | mCblk->volumeLR = (uint32_t(uint16_t(right * 0x1000)) << 16) | uint16_t(left * 0x1000); |
| 503 | |
| 504 | return NO_ERROR; |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 505 | } |
| 506 | |
| 507 | void AudioTrack::getVolume(float* left, float* right) |
| 508 | { |
Eric Laurent | be916aa | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 509 | if (left != NULL) { |
| 510 | *left = mVolume[LEFT]; |
| 511 | } |
| 512 | if (right != NULL) { |
| 513 | *right = mVolume[RIGHT]; |
| 514 | } |
| 515 | } |
| 516 | |
Eric Laurent | 2beeb50 | 2010-07-16 07:43:46 -0700 | [diff] [blame] | 517 | status_t AudioTrack::setAuxEffectSendLevel(float level) |
Eric Laurent | be916aa | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 518 | { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 519 | ALOGV("setAuxEffectSendLevel(%f)", level); |
Glenn Kasten | 05632a5 | 2012-01-03 14:22:33 -0800 | [diff] [blame] | 520 | if (level < 0.0f || level > 1.0f) { |
Eric Laurent | be916aa | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 521 | return BAD_VALUE; |
| 522 | } |
Eric Laurent | 1703cdf | 2011-03-07 14:52:59 -0800 | [diff] [blame] | 523 | AutoMutex lock(mLock); |
Eric Laurent | be916aa | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 524 | |
| 525 | mSendLevel = level; |
| 526 | |
Glenn Kasten | 05632a5 | 2012-01-03 14:22:33 -0800 | [diff] [blame] | 527 | mCblk->setSendLevel(level); |
Eric Laurent | be916aa | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 528 | |
| 529 | return NO_ERROR; |
| 530 | } |
| 531 | |
Eric Laurent | 2beeb50 | 2010-07-16 07:43:46 -0700 | [diff] [blame] | 532 | void AudioTrack::getAuxEffectSendLevel(float* level) |
Eric Laurent | be916aa | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 533 | { |
| 534 | if (level != NULL) { |
| 535 | *level = mSendLevel; |
| 536 | } |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 537 | } |
| 538 | |
Eric Laurent | 5732662 | 2009-07-07 07:10:45 -0700 | [diff] [blame] | 539 | status_t AudioTrack::setSampleRate(int rate) |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 540 | { |
| 541 | int afSamplingRate; |
| 542 | |
| 543 | if (AudioSystem::getOutputSamplingRate(&afSamplingRate, mStreamType) != NO_ERROR) { |
Eric Laurent | 5732662 | 2009-07-07 07:10:45 -0700 | [diff] [blame] | 544 | return NO_INIT; |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 545 | } |
| 546 | // Resampler implementation limits input sampling rate to 2 x output sampling rate. |
Eric Laurent | 5732662 | 2009-07-07 07:10:45 -0700 | [diff] [blame] | 547 | if (rate <= 0 || rate > afSamplingRate*2 ) return BAD_VALUE; |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 548 | |
Eric Laurent | 1703cdf | 2011-03-07 14:52:59 -0800 | [diff] [blame] | 549 | AutoMutex lock(mLock); |
Eric Laurent | 5732662 | 2009-07-07 07:10:45 -0700 | [diff] [blame] | 550 | mCblk->sampleRate = rate; |
| 551 | return NO_ERROR; |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 552 | } |
| 553 | |
| 554 | uint32_t AudioTrack::getSampleRate() |
| 555 | { |
Eric Laurent | 1703cdf | 2011-03-07 14:52:59 -0800 | [diff] [blame] | 556 | AutoMutex lock(mLock); |
Eric Laurent | 5732662 | 2009-07-07 07:10:45 -0700 | [diff] [blame] | 557 | return mCblk->sampleRate; |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 558 | } |
| 559 | |
| 560 | status_t AudioTrack::setLoop(uint32_t loopStart, uint32_t loopEnd, int loopCount) |
| 561 | { |
Eric Laurent | 1703cdf | 2011-03-07 14:52:59 -0800 | [diff] [blame] | 562 | AutoMutex lock(mLock); |
| 563 | return setLoop_l(loopStart, loopEnd, loopCount); |
| 564 | } |
| 565 | |
| 566 | // must be called with mLock held |
| 567 | status_t AudioTrack::setLoop_l(uint32_t loopStart, uint32_t loopEnd, int loopCount) |
| 568 | { |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 569 | audio_track_cblk_t* cblk = mCblk; |
| 570 | |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 571 | Mutex::Autolock _l(cblk->lock); |
| 572 | |
| 573 | if (loopCount == 0) { |
| 574 | cblk->loopStart = UINT_MAX; |
| 575 | cblk->loopEnd = UINT_MAX; |
| 576 | cblk->loopCount = 0; |
| 577 | mLoopCount = 0; |
| 578 | return NO_ERROR; |
| 579 | } |
| 580 | |
| 581 | if (loopStart >= loopEnd || |
Eric Laurent | 9b7d950 | 2011-03-21 11:49:00 -0700 | [diff] [blame] | 582 | loopEnd - loopStart > cblk->frameCount || |
| 583 | cblk->server > loopStart) { |
Steve Block | 29357bc | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 584 | ALOGE("setLoop invalid value: loopStart %d, loopEnd %d, loopCount %d, framecount %d, user %d", loopStart, loopEnd, loopCount, cblk->frameCount, cblk->user); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 585 | return BAD_VALUE; |
| 586 | } |
| 587 | |
Eric Laurent | 9b7d950 | 2011-03-21 11:49:00 -0700 | [diff] [blame] | 588 | if ((mSharedBuffer != 0) && (loopEnd > cblk->frameCount)) { |
Steve Block | 29357bc | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 589 | ALOGE("setLoop invalid value: loop markers beyond data: loopStart %d, loopEnd %d, framecount %d", |
Eric Laurent | d1b449a | 2010-05-14 03:26:45 -0700 | [diff] [blame] | 590 | loopStart, loopEnd, cblk->frameCount); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 591 | return BAD_VALUE; |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 592 | } |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 593 | |
| 594 | cblk->loopStart = loopStart; |
| 595 | cblk->loopEnd = loopEnd; |
| 596 | cblk->loopCount = loopCount; |
| 597 | mLoopCount = loopCount; |
| 598 | |
| 599 | return NO_ERROR; |
| 600 | } |
| 601 | |
| 602 | status_t AudioTrack::getLoop(uint32_t *loopStart, uint32_t *loopEnd, int *loopCount) |
| 603 | { |
Eric Laurent | 1703cdf | 2011-03-07 14:52:59 -0800 | [diff] [blame] | 604 | AutoMutex lock(mLock); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 605 | if (loopStart != 0) { |
| 606 | *loopStart = mCblk->loopStart; |
| 607 | } |
| 608 | if (loopEnd != 0) { |
| 609 | *loopEnd = mCblk->loopEnd; |
| 610 | } |
| 611 | if (loopCount != 0) { |
| 612 | if (mCblk->loopCount < 0) { |
| 613 | *loopCount = -1; |
| 614 | } else { |
| 615 | *loopCount = mCblk->loopCount; |
| 616 | } |
| 617 | } |
| 618 | |
| 619 | return NO_ERROR; |
| 620 | } |
| 621 | |
| 622 | status_t AudioTrack::setMarkerPosition(uint32_t marker) |
| 623 | { |
| 624 | if (mCbf == 0) return INVALID_OPERATION; |
| 625 | |
| 626 | mMarkerPosition = marker; |
Jean-Michel Trivi | 2c22aeb | 2009-03-24 18:11:07 -0700 | [diff] [blame] | 627 | mMarkerReached = false; |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 628 | |
| 629 | return NO_ERROR; |
| 630 | } |
| 631 | |
| 632 | status_t AudioTrack::getMarkerPosition(uint32_t *marker) |
| 633 | { |
| 634 | if (marker == 0) return BAD_VALUE; |
| 635 | |
| 636 | *marker = mMarkerPosition; |
| 637 | |
| 638 | return NO_ERROR; |
| 639 | } |
| 640 | |
| 641 | status_t AudioTrack::setPositionUpdatePeriod(uint32_t updatePeriod) |
| 642 | { |
| 643 | if (mCbf == 0) return INVALID_OPERATION; |
| 644 | |
| 645 | uint32_t curPosition; |
| 646 | getPosition(&curPosition); |
| 647 | mNewPosition = curPosition + updatePeriod; |
| 648 | mUpdatePeriod = updatePeriod; |
| 649 | |
| 650 | return NO_ERROR; |
| 651 | } |
| 652 | |
| 653 | status_t AudioTrack::getPositionUpdatePeriod(uint32_t *updatePeriod) |
| 654 | { |
| 655 | if (updatePeriod == 0) return BAD_VALUE; |
| 656 | |
| 657 | *updatePeriod = mUpdatePeriod; |
| 658 | |
| 659 | return NO_ERROR; |
| 660 | } |
| 661 | |
| 662 | status_t AudioTrack::setPosition(uint32_t position) |
| 663 | { |
Eric Laurent | 1703cdf | 2011-03-07 14:52:59 -0800 | [diff] [blame] | 664 | AutoMutex lock(mLock); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 665 | |
Glenn Kasten | 9a2aaf9 | 2012-01-03 09:42:47 -0800 | [diff] [blame] | 666 | if (!stopped_l()) return INVALID_OPERATION; |
| 667 | |
| 668 | Mutex::Autolock _l(mCblk->lock); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 669 | |
| 670 | if (position > mCblk->user) return BAD_VALUE; |
| 671 | |
| 672 | mCblk->server = position; |
Eric Laurent | 38ccae2 | 2011-03-28 18:37:07 -0700 | [diff] [blame] | 673 | android_atomic_or(CBLK_FORCEREADY_ON, &mCblk->flags); |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 674 | |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 675 | return NO_ERROR; |
| 676 | } |
| 677 | |
| 678 | status_t AudioTrack::getPosition(uint32_t *position) |
| 679 | { |
| 680 | if (position == 0) return BAD_VALUE; |
Eric Laurent | 1703cdf | 2011-03-07 14:52:59 -0800 | [diff] [blame] | 681 | AutoMutex lock(mLock); |
Jean-Michel Trivi | cd07594 | 2011-08-25 16:47:23 -0700 | [diff] [blame] | 682 | *position = mFlushed ? 0 : mCblk->server; |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 683 | |
| 684 | return NO_ERROR; |
| 685 | } |
| 686 | |
| 687 | status_t AudioTrack::reload() |
| 688 | { |
Eric Laurent | 1703cdf | 2011-03-07 14:52:59 -0800 | [diff] [blame] | 689 | AutoMutex lock(mLock); |
| 690 | |
Glenn Kasten | 9a2aaf9 | 2012-01-03 09:42:47 -0800 | [diff] [blame] | 691 | if (!stopped_l()) return INVALID_OPERATION; |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 692 | |
Eric Laurent | 1703cdf | 2011-03-07 14:52:59 -0800 | [diff] [blame] | 693 | flush_l(); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 694 | |
Eric Laurent | d1b449a | 2010-05-14 03:26:45 -0700 | [diff] [blame] | 695 | mCblk->stepUser(mCblk->frameCount); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 696 | |
| 697 | return NO_ERROR; |
| 698 | } |
| 699 | |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 700 | audio_io_handle_t AudioTrack::getOutput() |
| 701 | { |
Eric Laurent | 1703cdf | 2011-03-07 14:52:59 -0800 | [diff] [blame] | 702 | AutoMutex lock(mLock); |
| 703 | return getOutput_l(); |
| 704 | } |
| 705 | |
| 706 | // must be called with mLock held |
| 707 | audio_io_handle_t AudioTrack::getOutput_l() |
| 708 | { |
Glenn Kasten | fff6d71 | 2012-01-12 16:38:12 -0800 | [diff] [blame] | 709 | return AudioSystem::getOutput(mStreamType, |
Jean-Michel Trivi | 0d255b2 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 710 | mCblk->sampleRate, mFormat, mChannelMask, (audio_policy_output_flags_t)mFlags); |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 711 | } |
| 712 | |
Eric Laurent | be916aa | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 713 | int AudioTrack::getSessionId() |
| 714 | { |
| 715 | return mSessionId; |
| 716 | } |
| 717 | |
| 718 | status_t AudioTrack::attachAuxEffect(int effectId) |
| 719 | { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 720 | ALOGV("attachAuxEffect(%d)", effectId); |
Eric Laurent | 2beeb50 | 2010-07-16 07:43:46 -0700 | [diff] [blame] | 721 | status_t status = mAudioTrack->attachAuxEffect(effectId); |
| 722 | if (status == NO_ERROR) { |
| 723 | mAuxEffectId = effectId; |
| 724 | } |
| 725 | return status; |
Eric Laurent | be916aa | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 726 | } |
| 727 | |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 728 | // ------------------------------------------------------------------------- |
| 729 | |
Eric Laurent | 1703cdf | 2011-03-07 14:52:59 -0800 | [diff] [blame] | 730 | // must be called with mLock held |
| 731 | status_t AudioTrack::createTrack_l( |
Glenn Kasten | fff6d71 | 2012-01-12 16:38:12 -0800 | [diff] [blame] | 732 | audio_stream_type_t streamType, |
Eric Laurent | 34f1d8e | 2009-11-04 08:27:26 -0800 | [diff] [blame] | 733 | uint32_t sampleRate, |
Glenn Kasten | e1c3962 | 2012-01-04 09:36:37 -0800 | [diff] [blame] | 734 | audio_format_t format, |
Jean-Michel Trivi | 0d255b2 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 735 | uint32_t channelMask, |
Eric Laurent | 34f1d8e | 2009-11-04 08:27:26 -0800 | [diff] [blame] | 736 | int frameCount, |
| 737 | uint32_t flags, |
| 738 | const sp<IMemory>& sharedBuffer, |
Eric Laurent | d1b449a | 2010-05-14 03:26:45 -0700 | [diff] [blame] | 739 | audio_io_handle_t output, |
| 740 | bool enforceFrameCount) |
Eric Laurent | 34f1d8e | 2009-11-04 08:27:26 -0800 | [diff] [blame] | 741 | { |
| 742 | status_t status; |
| 743 | const sp<IAudioFlinger>& audioFlinger = AudioSystem::get_audio_flinger(); |
| 744 | if (audioFlinger == 0) { |
Steve Block | 29357bc | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 745 | ALOGE("Could not get audioflinger"); |
Eric Laurent | 34f1d8e | 2009-11-04 08:27:26 -0800 | [diff] [blame] | 746 | return NO_INIT; |
| 747 | } |
| 748 | |
Eric Laurent | d1b449a | 2010-05-14 03:26:45 -0700 | [diff] [blame] | 749 | int afSampleRate; |
| 750 | if (AudioSystem::getOutputSamplingRate(&afSampleRate, streamType) != NO_ERROR) { |
| 751 | return NO_INIT; |
| 752 | } |
| 753 | int afFrameCount; |
| 754 | if (AudioSystem::getOutputFrameCount(&afFrameCount, streamType) != NO_ERROR) { |
| 755 | return NO_INIT; |
| 756 | } |
| 757 | uint32_t afLatency; |
| 758 | if (AudioSystem::getOutputLatency(&afLatency, streamType) != NO_ERROR) { |
| 759 | return NO_INIT; |
| 760 | } |
| 761 | |
| 762 | mNotificationFramesAct = mNotificationFramesReq; |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 763 | if (!audio_is_linear_pcm(format)) { |
Eric Laurent | d1b449a | 2010-05-14 03:26:45 -0700 | [diff] [blame] | 764 | if (sharedBuffer != 0) { |
| 765 | frameCount = sharedBuffer->size(); |
| 766 | } |
| 767 | } else { |
| 768 | // Ensure that buffer depth covers at least audio hardware latency |
| 769 | uint32_t minBufCount = afLatency / ((1000 * afFrameCount)/afSampleRate); |
| 770 | if (minBufCount < 2) minBufCount = 2; |
| 771 | |
| 772 | int minFrameCount = (afFrameCount*sampleRate*minBufCount)/afSampleRate; |
| 773 | |
| 774 | if (sharedBuffer == 0) { |
| 775 | if (frameCount == 0) { |
| 776 | frameCount = minFrameCount; |
| 777 | } |
| 778 | if (mNotificationFramesAct == 0) { |
| 779 | mNotificationFramesAct = frameCount/2; |
| 780 | } |
| 781 | // Make sure that application is notified with sufficient margin |
| 782 | // before underrun |
| 783 | if (mNotificationFramesAct > (uint32_t)frameCount/2) { |
| 784 | mNotificationFramesAct = frameCount/2; |
| 785 | } |
| 786 | if (frameCount < minFrameCount) { |
| 787 | if (enforceFrameCount) { |
Steve Block | 29357bc | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 788 | ALOGE("Invalid buffer size: minFrameCount %d, frameCount %d", minFrameCount, frameCount); |
Eric Laurent | d1b449a | 2010-05-14 03:26:45 -0700 | [diff] [blame] | 789 | return BAD_VALUE; |
| 790 | } else { |
| 791 | frameCount = minFrameCount; |
| 792 | } |
| 793 | } |
| 794 | } else { |
| 795 | // Ensure that buffer alignment matches channelcount |
Jean-Michel Trivi | 0d255b2 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 796 | int channelCount = popcount(channelMask); |
Eric Laurent | d1b449a | 2010-05-14 03:26:45 -0700 | [diff] [blame] | 797 | if (((uint32_t)sharedBuffer->pointer() & (channelCount | 1)) != 0) { |
Steve Block | 29357bc | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 798 | ALOGE("Invalid buffer alignement: address %p, channelCount %d", sharedBuffer->pointer(), channelCount); |
Eric Laurent | d1b449a | 2010-05-14 03:26:45 -0700 | [diff] [blame] | 799 | return BAD_VALUE; |
| 800 | } |
| 801 | frameCount = sharedBuffer->size()/channelCount/sizeof(int16_t); |
| 802 | } |
| 803 | } |
| 804 | |
Eric Laurent | 34f1d8e | 2009-11-04 08:27:26 -0800 | [diff] [blame] | 805 | sp<IAudioTrack> track = audioFlinger->createTrack(getpid(), |
| 806 | streamType, |
| 807 | sampleRate, |
| 808 | format, |
Jean-Michel Trivi | 0d255b2 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 809 | channelMask, |
Eric Laurent | 34f1d8e | 2009-11-04 08:27:26 -0800 | [diff] [blame] | 810 | frameCount, |
| 811 | ((uint16_t)flags) << 16, |
| 812 | sharedBuffer, |
| 813 | output, |
Eric Laurent | be916aa | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 814 | &mSessionId, |
Eric Laurent | 34f1d8e | 2009-11-04 08:27:26 -0800 | [diff] [blame] | 815 | &status); |
| 816 | |
| 817 | if (track == 0) { |
Steve Block | 29357bc | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 818 | ALOGE("AudioFlinger could not create track, status: %d", status); |
Eric Laurent | 34f1d8e | 2009-11-04 08:27:26 -0800 | [diff] [blame] | 819 | return status; |
| 820 | } |
| 821 | sp<IMemory> cblk = track->getCblk(); |
| 822 | if (cblk == 0) { |
Steve Block | 29357bc | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 823 | ALOGE("Could not get control block"); |
Eric Laurent | 34f1d8e | 2009-11-04 08:27:26 -0800 | [diff] [blame] | 824 | return NO_INIT; |
| 825 | } |
Eric Laurent | 34f1d8e | 2009-11-04 08:27:26 -0800 | [diff] [blame] | 826 | mAudioTrack = track; |
Eric Laurent | 34f1d8e | 2009-11-04 08:27:26 -0800 | [diff] [blame] | 827 | mCblkMemory = cblk; |
| 828 | mCblk = static_cast<audio_track_cblk_t*>(cblk->pointer()); |
Eric Laurent | 38ccae2 | 2011-03-28 18:37:07 -0700 | [diff] [blame] | 829 | android_atomic_or(CBLK_DIRECTION_OUT, &mCblk->flags); |
Eric Laurent | 34f1d8e | 2009-11-04 08:27:26 -0800 | [diff] [blame] | 830 | if (sharedBuffer == 0) { |
| 831 | mCblk->buffers = (char*)mCblk + sizeof(audio_track_cblk_t); |
| 832 | } else { |
| 833 | mCblk->buffers = sharedBuffer->pointer(); |
| 834 | // Force buffer full condition as data is already present in shared memory |
Eric Laurent | d1b449a | 2010-05-14 03:26:45 -0700 | [diff] [blame] | 835 | mCblk->stepUser(mCblk->frameCount); |
Eric Laurent | 34f1d8e | 2009-11-04 08:27:26 -0800 | [diff] [blame] | 836 | } |
| 837 | |
Eric Laurent | be916aa | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 838 | mCblk->volumeLR = (uint32_t(uint16_t(mVolume[RIGHT] * 0x1000)) << 16) | uint16_t(mVolume[LEFT] * 0x1000); |
Glenn Kasten | 05632a5 | 2012-01-03 14:22:33 -0800 | [diff] [blame] | 839 | mCblk->setSendLevel(mSendLevel); |
Eric Laurent | 2beeb50 | 2010-07-16 07:43:46 -0700 | [diff] [blame] | 840 | mAudioTrack->attachAuxEffect(mAuxEffectId); |
Eric Laurent | 6100d2d | 2009-11-19 09:00:56 -0800 | [diff] [blame] | 841 | mCblk->bufferTimeoutMs = MAX_STARTUP_TIMEOUT_MS; |
| 842 | mCblk->waitTimeMs = 0; |
Eric Laurent | d1b449a | 2010-05-14 03:26:45 -0700 | [diff] [blame] | 843 | mRemainingFrames = mNotificationFramesAct; |
| 844 | mLatency = afLatency + (1000*mCblk->frameCount) / sampleRate; |
Eric Laurent | 34f1d8e | 2009-11-04 08:27:26 -0800 | [diff] [blame] | 845 | return NO_ERROR; |
| 846 | } |
| 847 | |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 848 | status_t AudioTrack::obtainBuffer(Buffer* audioBuffer, int32_t waitCount) |
| 849 | { |
Eric Laurent | 1703cdf | 2011-03-07 14:52:59 -0800 | [diff] [blame] | 850 | AutoMutex lock(mLock); |
Glenn Kasten | 9a2aaf9 | 2012-01-03 09:42:47 -0800 | [diff] [blame] | 851 | bool active; |
Glenn Kasten | d0965dd | 2011-06-22 16:18:04 -0700 | [diff] [blame] | 852 | status_t result = NO_ERROR; |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 853 | audio_track_cblk_t* cblk = mCblk; |
| 854 | uint32_t framesReq = audioBuffer->frameCount; |
Eric Laurent | 1dd70b9 | 2009-04-21 07:56:33 -0700 | [diff] [blame] | 855 | uint32_t waitTimeMs = (waitCount < 0) ? cblk->bufferTimeoutMs : WAIT_PERIOD_MS; |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 856 | |
| 857 | audioBuffer->frameCount = 0; |
| 858 | audioBuffer->size = 0; |
| 859 | |
| 860 | uint32_t framesAvail = cblk->framesAvailable(); |
| 861 | |
Eric Laurent | 9b7d950 | 2011-03-21 11:49:00 -0700 | [diff] [blame] | 862 | cblk->lock.lock(); |
| 863 | if (cblk->flags & CBLK_INVALID_MSK) { |
| 864 | goto create_new_track; |
| 865 | } |
| 866 | cblk->lock.unlock(); |
| 867 | |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 868 | if (framesAvail == 0) { |
Eric Laurent | 34f1d8e | 2009-11-04 08:27:26 -0800 | [diff] [blame] | 869 | cblk->lock.lock(); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 870 | goto start_loop_here; |
| 871 | while (framesAvail == 0) { |
| 872 | active = mActive; |
Glenn Kasten | f6b1678 | 2011-12-15 09:51:17 -0800 | [diff] [blame] | 873 | if (CC_UNLIKELY(!active)) { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 874 | ALOGV("Not active and NO_MORE_BUFFERS"); |
Eric Laurent | 34f1d8e | 2009-11-04 08:27:26 -0800 | [diff] [blame] | 875 | cblk->lock.unlock(); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 876 | return NO_MORE_BUFFERS; |
| 877 | } |
Glenn Kasten | f6b1678 | 2011-12-15 09:51:17 -0800 | [diff] [blame] | 878 | if (CC_UNLIKELY(!waitCount)) { |
Eric Laurent | 34f1d8e | 2009-11-04 08:27:26 -0800 | [diff] [blame] | 879 | cblk->lock.unlock(); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 880 | return WOULD_BLOCK; |
Eric Laurent | 34f1d8e | 2009-11-04 08:27:26 -0800 | [diff] [blame] | 881 | } |
Eric Laurent | d1b449a | 2010-05-14 03:26:45 -0700 | [diff] [blame] | 882 | if (!(cblk->flags & CBLK_INVALID_MSK)) { |
Eric Laurent | 1703cdf | 2011-03-07 14:52:59 -0800 | [diff] [blame] | 883 | mLock.unlock(); |
Eric Laurent | d1b449a | 2010-05-14 03:26:45 -0700 | [diff] [blame] | 884 | result = cblk->cv.waitRelative(cblk->lock, milliseconds(waitTimeMs)); |
Eric Laurent | d1b449a | 2010-05-14 03:26:45 -0700 | [diff] [blame] | 885 | cblk->lock.unlock(); |
Eric Laurent | 1703cdf | 2011-03-07 14:52:59 -0800 | [diff] [blame] | 886 | mLock.lock(); |
Glenn Kasten | 9a2aaf9 | 2012-01-03 09:42:47 -0800 | [diff] [blame] | 887 | if (!mActive) { |
Eric Laurent | 1703cdf | 2011-03-07 14:52:59 -0800 | [diff] [blame] | 888 | return status_t(STOPPED); |
| 889 | } |
| 890 | cblk->lock.lock(); |
| 891 | } |
| 892 | |
| 893 | if (cblk->flags & CBLK_INVALID_MSK) { |
Eric Laurent | d1b449a | 2010-05-14 03:26:45 -0700 | [diff] [blame] | 894 | goto create_new_track; |
| 895 | } |
Glenn Kasten | f6b1678 | 2011-12-15 09:51:17 -0800 | [diff] [blame] | 896 | if (CC_UNLIKELY(result != NO_ERROR)) { |
Eric Laurent | 1dd70b9 | 2009-04-21 07:56:33 -0700 | [diff] [blame] | 897 | cblk->waitTimeMs += waitTimeMs; |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 898 | if (cblk->waitTimeMs >= cblk->bufferTimeoutMs) { |
| 899 | // timing out when a loop has been set and we have already written upto loop end |
| 900 | // is a normal condition: no need to wake AudioFlinger up. |
| 901 | if (cblk->user < cblk->loopEnd) { |
Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 902 | ALOGW( "obtainBuffer timed out (is the CPU pegged?) %p " |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 903 | "user=%08x, server=%08x", this, cblk->user, cblk->server); |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 904 | //unlock cblk mutex before calling mAudioTrack->start() (see issue #1617140) |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 905 | cblk->lock.unlock(); |
Eric Laurent | 34f1d8e | 2009-11-04 08:27:26 -0800 | [diff] [blame] | 906 | result = mAudioTrack->start(); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 907 | cblk->lock.lock(); |
Eric Laurent | 1703cdf | 2011-03-07 14:52:59 -0800 | [diff] [blame] | 908 | if (result == DEAD_OBJECT) { |
Eric Laurent | 38ccae2 | 2011-03-28 18:37:07 -0700 | [diff] [blame] | 909 | android_atomic_or(CBLK_INVALID_ON, &cblk->flags); |
Eric Laurent | 1703cdf | 2011-03-07 14:52:59 -0800 | [diff] [blame] | 910 | create_new_track: |
| 911 | result = restoreTrack_l(cblk, false); |
| 912 | } |
| 913 | if (result != NO_ERROR) { |
Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 914 | ALOGW("obtainBuffer create Track error %d", result); |
Eric Laurent | 1703cdf | 2011-03-07 14:52:59 -0800 | [diff] [blame] | 915 | cblk->lock.unlock(); |
| 916 | return result; |
| 917 | } |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 918 | } |
| 919 | cblk->waitTimeMs = 0; |
| 920 | } |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 921 | |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 922 | if (--waitCount == 0) { |
Eric Laurent | 34f1d8e | 2009-11-04 08:27:26 -0800 | [diff] [blame] | 923 | cblk->lock.unlock(); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 924 | return TIMED_OUT; |
| 925 | } |
| 926 | } |
| 927 | // read the server count again |
| 928 | start_loop_here: |
| 929 | framesAvail = cblk->framesAvailable_l(); |
| 930 | } |
Eric Laurent | 34f1d8e | 2009-11-04 08:27:26 -0800 | [diff] [blame] | 931 | cblk->lock.unlock(); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 932 | } |
| 933 | |
Eric Laurent | 44d9848 | 2010-09-30 16:12:31 -0700 | [diff] [blame] | 934 | // restart track if it was disabled by audioflinger due to previous underrun |
Eric Laurent | 1703cdf | 2011-03-07 14:52:59 -0800 | [diff] [blame] | 935 | if (mActive && (cblk->flags & CBLK_DISABLED_MSK)) { |
Eric Laurent | 38ccae2 | 2011-03-28 18:37:07 -0700 | [diff] [blame] | 936 | android_atomic_and(~CBLK_DISABLED_ON, &cblk->flags); |
Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 937 | ALOGW("obtainBuffer() track %p disabled, restarting", this); |
Eric Laurent | 38ccae2 | 2011-03-28 18:37:07 -0700 | [diff] [blame] | 938 | mAudioTrack->start(); |
Eric Laurent | 44d9848 | 2010-09-30 16:12:31 -0700 | [diff] [blame] | 939 | } |
| 940 | |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 941 | cblk->waitTimeMs = 0; |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 942 | |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 943 | if (framesReq > framesAvail) { |
| 944 | framesReq = framesAvail; |
| 945 | } |
| 946 | |
| 947 | uint32_t u = cblk->user; |
| 948 | uint32_t bufferEnd = cblk->userBase + cblk->frameCount; |
| 949 | |
| 950 | if (u + framesReq > bufferEnd) { |
| 951 | framesReq = bufferEnd - u; |
| 952 | } |
| 953 | |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 954 | audioBuffer->flags = mMuted ? Buffer::MUTE : 0; |
| 955 | audioBuffer->channelCount = mChannelCount; |
| 956 | audioBuffer->frameCount = framesReq; |
| 957 | audioBuffer->size = framesReq * cblk->frameSize; |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 958 | if (audio_is_linear_pcm(mFormat)) { |
| 959 | audioBuffer->format = AUDIO_FORMAT_PCM_16_BIT; |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 960 | } else { |
| 961 | audioBuffer->format = mFormat; |
| 962 | } |
| 963 | audioBuffer->raw = (int8_t *)cblk->buffer(u); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 964 | active = mActive; |
| 965 | return active ? status_t(NO_ERROR) : status_t(STOPPED); |
| 966 | } |
| 967 | |
| 968 | void AudioTrack::releaseBuffer(Buffer* audioBuffer) |
| 969 | { |
Eric Laurent | 1703cdf | 2011-03-07 14:52:59 -0800 | [diff] [blame] | 970 | AutoMutex lock(mLock); |
| 971 | mCblk->stepUser(audioBuffer->frameCount); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 972 | } |
| 973 | |
| 974 | // ------------------------------------------------------------------------- |
| 975 | |
| 976 | ssize_t AudioTrack::write(const void* buffer, size_t userSize) |
| 977 | { |
| 978 | |
| 979 | if (mSharedBuffer != 0) return INVALID_OPERATION; |
| 980 | |
| 981 | if (ssize_t(userSize) < 0) { |
| 982 | // sanity-check. user is most-likely passing an error code. |
Steve Block | 29357bc | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 983 | ALOGE("AudioTrack::write(buffer=%p, size=%u (%d)", |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 984 | buffer, userSize, userSize); |
| 985 | return BAD_VALUE; |
| 986 | } |
| 987 | |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 988 | ALOGV("write %p: %d bytes, mActive=%d", this, userSize, mActive); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 989 | |
Eric Laurent | 1703cdf | 2011-03-07 14:52:59 -0800 | [diff] [blame] | 990 | // acquire a strong reference on the IMemory and IAudioTrack so that they cannot be destroyed |
| 991 | // while we are accessing the cblk |
| 992 | mLock.lock(); |
| 993 | sp <IAudioTrack> audioTrack = mAudioTrack; |
| 994 | sp <IMemory> iMem = mCblkMemory; |
| 995 | mLock.unlock(); |
| 996 | |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 997 | ssize_t written = 0; |
| 998 | const int8_t *src = (const int8_t *)buffer; |
| 999 | Buffer audioBuffer; |
Glenn Kasten | b998065 | 2012-01-11 09:48:27 -0800 | [diff] [blame] | 1000 | size_t frameSz = frameSize(); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1001 | |
| 1002 | do { |
Eric Laurent | 33797ea | 2011-03-17 09:36:51 -0700 | [diff] [blame] | 1003 | audioBuffer.frameCount = userSize/frameSz; |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 1004 | |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1005 | // Calling obtainBuffer() with a negative wait count causes |
| 1006 | // an (almost) infinite wait time. |
| 1007 | status_t err = obtainBuffer(&audioBuffer, -1); |
| 1008 | if (err < 0) { |
| 1009 | // out of buffers, return #bytes written |
| 1010 | if (err == status_t(NO_MORE_BUFFERS)) |
| 1011 | break; |
| 1012 | return ssize_t(err); |
| 1013 | } |
| 1014 | |
| 1015 | size_t toWrite; |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 1016 | |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 1017 | if (mFormat == AUDIO_FORMAT_PCM_8_BIT && !(mFlags & AUDIO_POLICY_OUTPUT_FLAG_DIRECT)) { |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1018 | // Divide capacity by 2 to take expansion into account |
| 1019 | toWrite = audioBuffer.size>>1; |
| 1020 | // 8 to 16 bit conversion |
| 1021 | int count = toWrite; |
| 1022 | int16_t *dst = (int16_t *)(audioBuffer.i8); |
| 1023 | while(count--) { |
| 1024 | *dst++ = (int16_t)(*src++^0x80) << 8; |
| 1025 | } |
Eric Laurent | 3302526 | 2009-08-04 10:42:26 -0700 | [diff] [blame] | 1026 | } else { |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1027 | toWrite = audioBuffer.size; |
| 1028 | memcpy(audioBuffer.i8, src, toWrite); |
| 1029 | src += toWrite; |
| 1030 | } |
| 1031 | userSize -= toWrite; |
| 1032 | written += toWrite; |
| 1033 | |
| 1034 | releaseBuffer(&audioBuffer); |
Eric Laurent | 33797ea | 2011-03-17 09:36:51 -0700 | [diff] [blame] | 1035 | } while (userSize >= frameSz); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1036 | |
| 1037 | return written; |
| 1038 | } |
| 1039 | |
| 1040 | // ------------------------------------------------------------------------- |
| 1041 | |
| 1042 | bool AudioTrack::processAudioBuffer(const sp<AudioTrackThread>& thread) |
| 1043 | { |
| 1044 | Buffer audioBuffer; |
| 1045 | uint32_t frames; |
| 1046 | size_t writtenSize; |
| 1047 | |
Eric Laurent | 1703cdf | 2011-03-07 14:52:59 -0800 | [diff] [blame] | 1048 | mLock.lock(); |
| 1049 | // acquire a strong reference on the IMemory and IAudioTrack so that they cannot be destroyed |
| 1050 | // while we are accessing the cblk |
| 1051 | sp <IAudioTrack> audioTrack = mAudioTrack; |
| 1052 | sp <IMemory> iMem = mCblkMemory; |
| 1053 | audio_track_cblk_t* cblk = mCblk; |
Glenn Kasten | 9a2aaf9 | 2012-01-03 09:42:47 -0800 | [diff] [blame] | 1054 | bool active = mActive; |
Eric Laurent | 1703cdf | 2011-03-07 14:52:59 -0800 | [diff] [blame] | 1055 | mLock.unlock(); |
| 1056 | |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1057 | // Manage underrun callback |
Glenn Kasten | 9a2aaf9 | 2012-01-03 09:42:47 -0800 | [diff] [blame] | 1058 | if (active && (cblk->framesAvailable() == cblk->frameCount)) { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1059 | ALOGV("Underrun user: %x, server: %x, flags %04x", cblk->user, cblk->server, cblk->flags); |
Eric Laurent | 38ccae2 | 2011-03-28 18:37:07 -0700 | [diff] [blame] | 1060 | if (!(android_atomic_or(CBLK_UNDERRUN_ON, &cblk->flags) & CBLK_UNDERRUN_MSK)) { |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1061 | mCbf(EVENT_UNDERRUN, mUserData, 0); |
Eric Laurent | 1703cdf | 2011-03-07 14:52:59 -0800 | [diff] [blame] | 1062 | if (cblk->server == cblk->frameCount) { |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 1063 | mCbf(EVENT_BUFFER_END, mUserData, 0); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1064 | } |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1065 | if (mSharedBuffer != 0) return false; |
| 1066 | } |
| 1067 | } |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 1068 | |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1069 | // Manage loop end callback |
Eric Laurent | 1703cdf | 2011-03-07 14:52:59 -0800 | [diff] [blame] | 1070 | while (mLoopCount > cblk->loopCount) { |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1071 | int loopCount = -1; |
| 1072 | mLoopCount--; |
| 1073 | if (mLoopCount >= 0) loopCount = mLoopCount; |
| 1074 | |
| 1075 | mCbf(EVENT_LOOP_END, mUserData, (void *)&loopCount); |
| 1076 | } |
| 1077 | |
| 1078 | // Manage marker callback |
Jean-Michel Trivi | 2c22aeb | 2009-03-24 18:11:07 -0700 | [diff] [blame] | 1079 | if (!mMarkerReached && (mMarkerPosition > 0)) { |
Eric Laurent | 1703cdf | 2011-03-07 14:52:59 -0800 | [diff] [blame] | 1080 | if (cblk->server >= mMarkerPosition) { |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1081 | mCbf(EVENT_MARKER, mUserData, (void *)&mMarkerPosition); |
Jean-Michel Trivi | 2c22aeb | 2009-03-24 18:11:07 -0700 | [diff] [blame] | 1082 | mMarkerReached = true; |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1083 | } |
| 1084 | } |
| 1085 | |
| 1086 | // Manage new position callback |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 1087 | if (mUpdatePeriod > 0) { |
Eric Laurent | 1703cdf | 2011-03-07 14:52:59 -0800 | [diff] [blame] | 1088 | while (cblk->server >= mNewPosition) { |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1089 | mCbf(EVENT_NEW_POS, mUserData, (void *)&mNewPosition); |
| 1090 | mNewPosition += mUpdatePeriod; |
| 1091 | } |
| 1092 | } |
| 1093 | |
| 1094 | // If Shared buffer is used, no data is requested from client. |
| 1095 | if (mSharedBuffer != 0) { |
| 1096 | frames = 0; |
| 1097 | } else { |
| 1098 | frames = mRemainingFrames; |
| 1099 | } |
| 1100 | |
Eric Laurent | 2267ba1 | 2011-09-07 11:13:23 -0700 | [diff] [blame] | 1101 | int32_t waitCount = -1; |
| 1102 | if (mUpdatePeriod || (!mMarkerReached && mMarkerPosition) || mLoopCount) { |
| 1103 | waitCount = 1; |
| 1104 | } |
| 1105 | |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1106 | do { |
| 1107 | |
| 1108 | audioBuffer.frameCount = frames; |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 1109 | |
| 1110 | // Calling obtainBuffer() with a wait count of 1 |
| 1111 | // limits wait time to WAIT_PERIOD_MS. This prevents from being |
| 1112 | // stuck here not being able to handle timed events (position, markers, loops). |
Eric Laurent | 2267ba1 | 2011-09-07 11:13:23 -0700 | [diff] [blame] | 1113 | status_t err = obtainBuffer(&audioBuffer, waitCount); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1114 | if (err < NO_ERROR) { |
| 1115 | if (err != TIMED_OUT) { |
Steve Block | 29357bc | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 1116 | ALOGE_IF(err != status_t(NO_MORE_BUFFERS), "Error obtaining an audio buffer, giving up."); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1117 | return false; |
| 1118 | } |
| 1119 | break; |
| 1120 | } |
| 1121 | if (err == status_t(STOPPED)) return false; |
| 1122 | |
| 1123 | // Divide buffer size by 2 to take into account the expansion |
| 1124 | // due to 8 to 16 bit conversion: the callback must fill only half |
| 1125 | // of the destination buffer |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 1126 | if (mFormat == AUDIO_FORMAT_PCM_8_BIT && !(mFlags & AUDIO_POLICY_OUTPUT_FLAG_DIRECT)) { |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1127 | audioBuffer.size >>= 1; |
| 1128 | } |
| 1129 | |
| 1130 | size_t reqSize = audioBuffer.size; |
| 1131 | mCbf(EVENT_MORE_DATA, mUserData, &audioBuffer); |
| 1132 | writtenSize = audioBuffer.size; |
| 1133 | |
| 1134 | // Sanity check on returned size |
The Android Open Source Project | 8555d08 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 1135 | if (ssize_t(writtenSize) <= 0) { |
| 1136 | // The callback is done filling buffers |
| 1137 | // Keep this thread going to handle timed events and |
| 1138 | // still try to get more data in intervals of WAIT_PERIOD_MS |
| 1139 | // but don't just loop and block the CPU, so wait |
| 1140 | usleep(WAIT_PERIOD_MS*1000); |
| 1141 | break; |
| 1142 | } |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1143 | if (writtenSize > reqSize) writtenSize = reqSize; |
| 1144 | |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 1145 | if (mFormat == AUDIO_FORMAT_PCM_8_BIT && !(mFlags & AUDIO_POLICY_OUTPUT_FLAG_DIRECT)) { |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1146 | // 8 to 16 bit conversion |
| 1147 | const int8_t *src = audioBuffer.i8 + writtenSize-1; |
| 1148 | int count = writtenSize; |
| 1149 | int16_t *dst = audioBuffer.i16 + writtenSize-1; |
| 1150 | while(count--) { |
| 1151 | *dst-- = (int16_t)(*src--^0x80) << 8; |
| 1152 | } |
| 1153 | writtenSize <<= 1; |
| 1154 | } |
| 1155 | |
| 1156 | audioBuffer.size = writtenSize; |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 1157 | // NOTE: mCblk->frameSize is not equal to AudioTrack::frameSize() for |
Glenn Kasten | b998065 | 2012-01-11 09:48:27 -0800 | [diff] [blame] | 1158 | // 8 bit PCM data: in this case, mCblk->frameSize is based on a sample size of |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 1159 | // 16 bit. |
| 1160 | audioBuffer.frameCount = writtenSize/mCblk->frameSize; |
| 1161 | |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1162 | frames -= audioBuffer.frameCount; |
| 1163 | |
| 1164 | releaseBuffer(&audioBuffer); |
| 1165 | } |
| 1166 | while (frames); |
| 1167 | |
| 1168 | if (frames == 0) { |
Eric Laurent | d1b449a | 2010-05-14 03:26:45 -0700 | [diff] [blame] | 1169 | mRemainingFrames = mNotificationFramesAct; |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1170 | } else { |
| 1171 | mRemainingFrames = frames; |
| 1172 | } |
| 1173 | return true; |
| 1174 | } |
| 1175 | |
Eric Laurent | 1703cdf | 2011-03-07 14:52:59 -0800 | [diff] [blame] | 1176 | // must be called with mLock and cblk.lock held. Callers must also hold strong references on |
| 1177 | // the IAudioTrack and IMemory in case they are recreated here. |
| 1178 | // If the IAudioTrack is successfully restored, the cblk pointer is updated |
| 1179 | status_t AudioTrack::restoreTrack_l(audio_track_cblk_t*& cblk, bool fromStart) |
| 1180 | { |
| 1181 | status_t result; |
| 1182 | |
Eric Laurent | 38ccae2 | 2011-03-28 18:37:07 -0700 | [diff] [blame] | 1183 | if (!(android_atomic_or(CBLK_RESTORING_ON, &cblk->flags) & CBLK_RESTORING_MSK)) { |
Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 1184 | ALOGW("dead IAudioTrack, creating a new one from %s TID %d", |
Eric Laurent | cfe2ba6 | 2011-09-13 15:04:17 -0700 | [diff] [blame] | 1185 | fromStart ? "start()" : "obtainBuffer()", gettid()); |
Eric Laurent | 1703cdf | 2011-03-07 14:52:59 -0800 | [diff] [blame] | 1186 | |
Eric Laurent | 1703cdf | 2011-03-07 14:52:59 -0800 | [diff] [blame] | 1187 | // signal old cblk condition so that other threads waiting for available buffers stop |
| 1188 | // waiting now |
| 1189 | cblk->cv.broadcast(); |
| 1190 | cblk->lock.unlock(); |
| 1191 | |
Eric Laurent | 9f6530f | 2011-08-30 10:18:54 -0700 | [diff] [blame] | 1192 | // refresh the audio configuration cache in this process to make sure we get new |
| 1193 | // output parameters in getOutput_l() and createTrack_l() |
| 1194 | AudioSystem::clearAudioConfigCache(); |
| 1195 | |
Eric Laurent | 1703cdf | 2011-03-07 14:52:59 -0800 | [diff] [blame] | 1196 | // if the new IAudioTrack is created, createTrack_l() will modify the |
| 1197 | // following member variables: mAudioTrack, mCblkMemory and mCblk. |
| 1198 | // It will also delete the strong references on previous IAudioTrack and IMemory |
| 1199 | result = createTrack_l(mStreamType, |
| 1200 | cblk->sampleRate, |
| 1201 | mFormat, |
Jean-Michel Trivi | 0d255b2 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 1202 | mChannelMask, |
Eric Laurent | 1703cdf | 2011-03-07 14:52:59 -0800 | [diff] [blame] | 1203 | mFrameCount, |
| 1204 | mFlags, |
| 1205 | mSharedBuffer, |
| 1206 | getOutput_l(), |
| 1207 | false); |
| 1208 | |
| 1209 | if (result == NO_ERROR) { |
Eric Laurent | 408b8dc | 2011-09-06 12:36:15 -0700 | [diff] [blame] | 1210 | uint32_t user = cblk->user; |
| 1211 | uint32_t server = cblk->server; |
Eric Laurent | 9b7d950 | 2011-03-21 11:49:00 -0700 | [diff] [blame] | 1212 | // restore write index and set other indexes to reflect empty buffer status |
Eric Laurent | 408b8dc | 2011-09-06 12:36:15 -0700 | [diff] [blame] | 1213 | mCblk->user = user; |
| 1214 | mCblk->server = user; |
| 1215 | mCblk->userBase = user; |
| 1216 | mCblk->serverBase = user; |
Eric Laurent | 9b7d950 | 2011-03-21 11:49:00 -0700 | [diff] [blame] | 1217 | // restore loop: this is not guaranteed to succeed if new frame count is not |
| 1218 | // compatible with loop length |
| 1219 | setLoop_l(cblk->loopStart, cblk->loopEnd, cblk->loopCount); |
Eric Laurent | 1703cdf | 2011-03-07 14:52:59 -0800 | [diff] [blame] | 1220 | if (!fromStart) { |
| 1221 | mCblk->bufferTimeoutMs = MAX_RUN_TIMEOUT_MS; |
Eric Laurent | 408b8dc | 2011-09-06 12:36:15 -0700 | [diff] [blame] | 1222 | // Make sure that a client relying on callback events indicating underrun or |
| 1223 | // the actual amount of audio frames played (e.g SoundPool) receives them. |
| 1224 | if (mSharedBuffer == 0) { |
| 1225 | uint32_t frames = 0; |
| 1226 | if (user > server) { |
| 1227 | frames = ((user - server) > mCblk->frameCount) ? |
| 1228 | mCblk->frameCount : (user - server); |
| 1229 | memset(mCblk->buffers, 0, frames * mCblk->frameSize); |
| 1230 | } |
| 1231 | // restart playback even if buffer is not completely filled. |
| 1232 | android_atomic_or(CBLK_FORCEREADY_ON, &mCblk->flags); |
| 1233 | // stepUser() clears CBLK_UNDERRUN_ON flag enabling underrun callbacks to |
| 1234 | // the client |
| 1235 | mCblk->stepUser(frames); |
| 1236 | } |
Eric Laurent | 1703cdf | 2011-03-07 14:52:59 -0800 | [diff] [blame] | 1237 | } |
Eric Laurent | 9b7d950 | 2011-03-21 11:49:00 -0700 | [diff] [blame] | 1238 | if (mActive) { |
| 1239 | result = mAudioTrack->start(); |
Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 1240 | ALOGW_IF(result != NO_ERROR, "restoreTrack_l() start() failed status %d", result); |
Eric Laurent | 9b7d950 | 2011-03-21 11:49:00 -0700 | [diff] [blame] | 1241 | } |
Eric Laurent | 1703cdf | 2011-03-07 14:52:59 -0800 | [diff] [blame] | 1242 | if (fromStart && result == NO_ERROR) { |
| 1243 | mNewPosition = mCblk->server + mUpdatePeriod; |
| 1244 | } |
| 1245 | } |
| 1246 | if (result != NO_ERROR) { |
Eric Laurent | cfe2ba6 | 2011-09-13 15:04:17 -0700 | [diff] [blame] | 1247 | android_atomic_and(~CBLK_RESTORING_ON, &cblk->flags); |
Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 1248 | ALOGW_IF(result != NO_ERROR, "restoreTrack_l() failed status %d", result); |
Eric Laurent | 1703cdf | 2011-03-07 14:52:59 -0800 | [diff] [blame] | 1249 | } |
Eric Laurent | cfe2ba6 | 2011-09-13 15:04:17 -0700 | [diff] [blame] | 1250 | mRestoreStatus = result; |
Eric Laurent | 1703cdf | 2011-03-07 14:52:59 -0800 | [diff] [blame] | 1251 | // signal old cblk condition for other threads waiting for restore completion |
Eric Laurent | 38ccae2 | 2011-03-28 18:37:07 -0700 | [diff] [blame] | 1252 | android_atomic_or(CBLK_RESTORED_ON, &cblk->flags); |
Eric Laurent | 1703cdf | 2011-03-07 14:52:59 -0800 | [diff] [blame] | 1253 | cblk->cv.broadcast(); |
Eric Laurent | 1703cdf | 2011-03-07 14:52:59 -0800 | [diff] [blame] | 1254 | } else { |
| 1255 | if (!(cblk->flags & CBLK_RESTORED_MSK)) { |
Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 1256 | ALOGW("dead IAudioTrack, waiting for a new one TID %d", gettid()); |
Eric Laurent | 1703cdf | 2011-03-07 14:52:59 -0800 | [diff] [blame] | 1257 | mLock.unlock(); |
| 1258 | result = cblk->cv.waitRelative(cblk->lock, milliseconds(RESTORE_TIMEOUT_MS)); |
Eric Laurent | cfe2ba6 | 2011-09-13 15:04:17 -0700 | [diff] [blame] | 1259 | if (result == NO_ERROR) { |
| 1260 | result = mRestoreStatus; |
| 1261 | } |
Eric Laurent | 1703cdf | 2011-03-07 14:52:59 -0800 | [diff] [blame] | 1262 | cblk->lock.unlock(); |
| 1263 | mLock.lock(); |
| 1264 | } else { |
Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 1265 | ALOGW("dead IAudioTrack, already restored TID %d", gettid()); |
Eric Laurent | cfe2ba6 | 2011-09-13 15:04:17 -0700 | [diff] [blame] | 1266 | result = mRestoreStatus; |
Eric Laurent | 1703cdf | 2011-03-07 14:52:59 -0800 | [diff] [blame] | 1267 | cblk->lock.unlock(); |
| 1268 | } |
Eric Laurent | 1703cdf | 2011-03-07 14:52:59 -0800 | [diff] [blame] | 1269 | } |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1270 | ALOGV("restoreTrack_l() status %d mActive %d cblk %p, old cblk %p flags %08x old flags %08x", |
Eric Laurent | 1703cdf | 2011-03-07 14:52:59 -0800 | [diff] [blame] | 1271 | result, mActive, mCblk, cblk, mCblk->flags, cblk->flags); |
| 1272 | |
| 1273 | if (result == NO_ERROR) { |
| 1274 | // from now on we switch to the newly created cblk |
| 1275 | cblk = mCblk; |
| 1276 | } |
| 1277 | cblk->lock.lock(); |
| 1278 | |
Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 1279 | ALOGW_IF(result != NO_ERROR, "restoreTrack_l() error %d TID %d", result, gettid()); |
Eric Laurent | 1703cdf | 2011-03-07 14:52:59 -0800 | [diff] [blame] | 1280 | |
| 1281 | return result; |
| 1282 | } |
| 1283 | |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1284 | status_t AudioTrack::dump(int fd, const Vector<String16>& args) const |
| 1285 | { |
| 1286 | |
| 1287 | const size_t SIZE = 256; |
| 1288 | char buffer[SIZE]; |
| 1289 | String8 result; |
| 1290 | |
| 1291 | result.append(" AudioTrack::dump\n"); |
| 1292 | snprintf(buffer, 255, " stream type(%d), left - right volume(%f, %f)\n", mStreamType, mVolume[0], mVolume[1]); |
| 1293 | result.append(buffer); |
Eric Laurent | d1b449a | 2010-05-14 03:26:45 -0700 | [diff] [blame] | 1294 | snprintf(buffer, 255, " format(%d), channel count(%d), frame count(%d)\n", mFormat, mChannelCount, mCblk->frameCount); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1295 | result.append(buffer); |
Eric Laurent | 5732662 | 2009-07-07 07:10:45 -0700 | [diff] [blame] | 1296 | snprintf(buffer, 255, " sample rate(%d), status(%d), muted(%d)\n", (mCblk == 0) ? 0 : mCblk->sampleRate, mStatus, mMuted); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1297 | result.append(buffer); |
| 1298 | snprintf(buffer, 255, " active(%d), latency (%d)\n", mActive, mLatency); |
| 1299 | result.append(buffer); |
| 1300 | ::write(fd, result.string(), result.size()); |
| 1301 | return NO_ERROR; |
| 1302 | } |
| 1303 | |
| 1304 | // ========================================================================= |
| 1305 | |
| 1306 | AudioTrack::AudioTrackThread::AudioTrackThread(AudioTrack& receiver, bool bCanCallJava) |
| 1307 | : Thread(bCanCallJava), mReceiver(receiver) |
| 1308 | { |
| 1309 | } |
| 1310 | |
| 1311 | bool AudioTrack::AudioTrackThread::threadLoop() |
| 1312 | { |
| 1313 | return mReceiver.processAudioBuffer(this); |
| 1314 | } |
| 1315 | |
| 1316 | status_t AudioTrack::AudioTrackThread::readyToRun() |
| 1317 | { |
| 1318 | return NO_ERROR; |
| 1319 | } |
| 1320 | |
| 1321 | void AudioTrack::AudioTrackThread::onFirstRef() |
| 1322 | { |
| 1323 | } |
| 1324 | |
| 1325 | // ========================================================================= |
| 1326 | |
Eric Laurent | 38ccae2 | 2011-03-28 18:37:07 -0700 | [diff] [blame] | 1327 | |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1328 | audio_track_cblk_t::audio_track_cblk_t() |
Mathias Agopian | 54b1a05 | 2010-03-19 16:14:13 -0700 | [diff] [blame] | 1329 | : lock(Mutex::SHARED), cv(Condition::SHARED), user(0), server(0), |
| 1330 | userBase(0), serverBase(0), buffers(0), frameCount(0), |
| 1331 | loopStart(UINT_MAX), loopEnd(UINT_MAX), loopCount(0), volumeLR(0), |
Glenn Kasten | 05632a5 | 2012-01-03 14:22:33 -0800 | [diff] [blame] | 1332 | mSendLevel(0), flags(0) |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1333 | { |
| 1334 | } |
| 1335 | |
| 1336 | uint32_t audio_track_cblk_t::stepUser(uint32_t frameCount) |
| 1337 | { |
Glenn Kasten | 9054897 | 2011-12-13 11:45:07 -0800 | [diff] [blame] | 1338 | uint32_t u = user; |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1339 | |
| 1340 | u += frameCount; |
| 1341 | // Ensure that user is never ahead of server for AudioRecord |
Eric Laurent | d1b449a | 2010-05-14 03:26:45 -0700 | [diff] [blame] | 1342 | if (flags & CBLK_DIRECTION_MSK) { |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1343 | // If stepServer() has been called once, switch to normal obtainBuffer() timeout period |
| 1344 | if (bufferTimeoutMs == MAX_STARTUP_TIMEOUT_MS-1) { |
| 1345 | bufferTimeoutMs = MAX_RUN_TIMEOUT_MS; |
| 1346 | } |
Glenn Kasten | 9054897 | 2011-12-13 11:45:07 -0800 | [diff] [blame] | 1347 | } else if (u > server) { |
Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 1348 | ALOGW("stepServer occurred after track reset"); |
Glenn Kasten | 9054897 | 2011-12-13 11:45:07 -0800 | [diff] [blame] | 1349 | u = server; |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1350 | } |
| 1351 | |
| 1352 | if (u >= userBase + this->frameCount) { |
| 1353 | userBase += this->frameCount; |
| 1354 | } |
| 1355 | |
Glenn Kasten | 9054897 | 2011-12-13 11:45:07 -0800 | [diff] [blame] | 1356 | user = u; |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1357 | |
| 1358 | // Clear flow control error condition as new data has been written/read to/from buffer. |
Eric Laurent | 33797ea | 2011-03-17 09:36:51 -0700 | [diff] [blame] | 1359 | if (flags & CBLK_UNDERRUN_MSK) { |
Eric Laurent | 38ccae2 | 2011-03-28 18:37:07 -0700 | [diff] [blame] | 1360 | android_atomic_and(~CBLK_UNDERRUN_MSK, &flags); |
Eric Laurent | 33797ea | 2011-03-17 09:36:51 -0700 | [diff] [blame] | 1361 | } |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1362 | |
| 1363 | return u; |
| 1364 | } |
| 1365 | |
| 1366 | bool audio_track_cblk_t::stepServer(uint32_t frameCount) |
| 1367 | { |
Eric Laurent | 38ccae2 | 2011-03-28 18:37:07 -0700 | [diff] [blame] | 1368 | if (!tryLock()) { |
Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 1369 | ALOGW("stepServer() could not lock cblk"); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1370 | return false; |
| 1371 | } |
| 1372 | |
Glenn Kasten | 9054897 | 2011-12-13 11:45:07 -0800 | [diff] [blame] | 1373 | uint32_t s = server; |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1374 | |
| 1375 | s += frameCount; |
Eric Laurent | d1b449a | 2010-05-14 03:26:45 -0700 | [diff] [blame] | 1376 | if (flags & CBLK_DIRECTION_MSK) { |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1377 | // Mark that we have read the first buffer so that next time stepUser() is called |
| 1378 | // we switch to normal obtainBuffer() timeout period |
| 1379 | if (bufferTimeoutMs == MAX_STARTUP_TIMEOUT_MS) { |
Eric Laurent | 34f1d8e | 2009-11-04 08:27:26 -0800 | [diff] [blame] | 1380 | bufferTimeoutMs = MAX_STARTUP_TIMEOUT_MS - 1; |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 1381 | } |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1382 | // It is possible that we receive a flush() |
| 1383 | // while the mixer is processing a block: in this case, |
| 1384 | // stepServer() is called After the flush() has reset u & s and |
| 1385 | // we have s > u |
Glenn Kasten | 9054897 | 2011-12-13 11:45:07 -0800 | [diff] [blame] | 1386 | if (s > user) { |
Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 1387 | ALOGW("stepServer occurred after track reset"); |
Glenn Kasten | 9054897 | 2011-12-13 11:45:07 -0800 | [diff] [blame] | 1388 | s = user; |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1389 | } |
| 1390 | } |
| 1391 | |
| 1392 | if (s >= loopEnd) { |
Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 1393 | ALOGW_IF(s > loopEnd, "stepServer: s %u > loopEnd %u", s, loopEnd); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1394 | s = loopStart; |
| 1395 | if (--loopCount == 0) { |
| 1396 | loopEnd = UINT_MAX; |
| 1397 | loopStart = UINT_MAX; |
| 1398 | } |
| 1399 | } |
| 1400 | if (s >= serverBase + this->frameCount) { |
| 1401 | serverBase += this->frameCount; |
| 1402 | } |
| 1403 | |
Glenn Kasten | 9054897 | 2011-12-13 11:45:07 -0800 | [diff] [blame] | 1404 | server = s; |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1405 | |
Eric Laurent | 1703cdf | 2011-03-07 14:52:59 -0800 | [diff] [blame] | 1406 | if (!(flags & CBLK_INVALID_MSK)) { |
| 1407 | cv.signal(); |
| 1408 | } |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1409 | lock.unlock(); |
| 1410 | return true; |
| 1411 | } |
| 1412 | |
| 1413 | void* audio_track_cblk_t::buffer(uint32_t offset) const |
| 1414 | { |
Glenn Kasten | 9054897 | 2011-12-13 11:45:07 -0800 | [diff] [blame] | 1415 | return (int8_t *)buffers + (offset - userBase) * frameSize; |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1416 | } |
| 1417 | |
| 1418 | uint32_t audio_track_cblk_t::framesAvailable() |
| 1419 | { |
| 1420 | Mutex::Autolock _l(lock); |
| 1421 | return framesAvailable_l(); |
| 1422 | } |
| 1423 | |
| 1424 | uint32_t audio_track_cblk_t::framesAvailable_l() |
| 1425 | { |
Glenn Kasten | 9054897 | 2011-12-13 11:45:07 -0800 | [diff] [blame] | 1426 | uint32_t u = user; |
| 1427 | uint32_t s = server; |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1428 | |
Eric Laurent | d1b449a | 2010-05-14 03:26:45 -0700 | [diff] [blame] | 1429 | if (flags & CBLK_DIRECTION_MSK) { |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1430 | uint32_t limit = (s < loopStart) ? s : loopStart; |
| 1431 | return limit + frameCount - u; |
| 1432 | } else { |
| 1433 | return frameCount + u - s; |
| 1434 | } |
| 1435 | } |
| 1436 | |
| 1437 | uint32_t audio_track_cblk_t::framesReady() |
| 1438 | { |
Glenn Kasten | 9054897 | 2011-12-13 11:45:07 -0800 | [diff] [blame] | 1439 | uint32_t u = user; |
| 1440 | uint32_t s = server; |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1441 | |
Eric Laurent | d1b449a | 2010-05-14 03:26:45 -0700 | [diff] [blame] | 1442 | if (flags & CBLK_DIRECTION_MSK) { |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1443 | if (u < loopEnd) { |
| 1444 | return u - s; |
| 1445 | } else { |
Eric Laurent | 38ccae2 | 2011-03-28 18:37:07 -0700 | [diff] [blame] | 1446 | // do not block on mutex shared with client on AudioFlinger side |
| 1447 | if (!tryLock()) { |
Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 1448 | ALOGW("framesReady() could not lock cblk"); |
Eric Laurent | 38ccae2 | 2011-03-28 18:37:07 -0700 | [diff] [blame] | 1449 | return 0; |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1450 | } |
Eric Laurent | 38ccae2 | 2011-03-28 18:37:07 -0700 | [diff] [blame] | 1451 | uint32_t frames = UINT_MAX; |
| 1452 | if (loopCount >= 0) { |
| 1453 | frames = (loopEnd - loopStart)*loopCount + u - s; |
| 1454 | } |
| 1455 | lock.unlock(); |
| 1456 | return frames; |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1457 | } |
| 1458 | } else { |
| 1459 | return s - u; |
| 1460 | } |
| 1461 | } |
| 1462 | |
Eric Laurent | 38ccae2 | 2011-03-28 18:37:07 -0700 | [diff] [blame] | 1463 | bool audio_track_cblk_t::tryLock() |
| 1464 | { |
| 1465 | // the code below simulates lock-with-timeout |
| 1466 | // we MUST do this to protect the AudioFlinger server |
| 1467 | // as this lock is shared with the client. |
| 1468 | status_t err; |
| 1469 | |
| 1470 | err = lock.tryLock(); |
| 1471 | if (err == -EBUSY) { // just wait a bit |
| 1472 | usleep(1000); |
| 1473 | err = lock.tryLock(); |
| 1474 | } |
| 1475 | if (err != NO_ERROR) { |
| 1476 | // probably, the client just died. |
| 1477 | return false; |
| 1478 | } |
| 1479 | return true; |
| 1480 | } |
| 1481 | |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1482 | // ------------------------------------------------------------------------- |
| 1483 | |
| 1484 | }; // namespace android |