blob: 7e338dee853ca28782e62baf8bd5a2ae26ec1f58 [file] [log] [blame]
Glenn Kastene5fb2632011-12-14 10:28:06 -08001/* frameworks/base/media/libmedia/AudioTrack.cpp
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002**
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 Agopian07952722009-05-19 19:08:10 -070035#include <binder/Parcel.h>
36#include <binder/IPCThreadState.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080037#include <utils/Timers.h>
Eric Laurentae29b762011-03-28 18:37:07 -070038#include <utils/Atomic.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080039
Dima Zavin24fc2fb2011-04-19 22:30:36 -070040#include <cutils/bitops.h>
Glenn Kastene80a4cc2011-12-15 09:51:17 -080041#include <cutils/compiler.h>
Dima Zavin24fc2fb2011-04-19 22:30:36 -070042
Dima Zavin34bb4192011-05-11 14:15:23 -070043#include <system/audio.h>
Dima Zavin290029d2011-06-13 18:16:26 -070044#include <system/audio_policy.h>
Dima Zavin24fc2fb2011-04-19 22:30:36 -070045
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080046namespace android {
Chia-chi Yehbd240c22010-06-16 06:33:13 +080047// ---------------------------------------------------------------------------
48
49// static
50status_t AudioTrack::getMinFrameCount(
51 int* frameCount,
Glenn Kastenbc1d77b2012-01-12 16:38:12 -080052 audio_stream_type_t streamType,
Chia-chi Yehbd240c22010-06-16 06:33:13 +080053 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 Project9066cfe2009-03-03 19:31:44 -080076
77// ---------------------------------------------------------------------------
78
79AudioTrack::AudioTrack()
Glenn Kasten99d54432011-06-22 16:15:25 -070080 : mStatus(NO_INIT),
81 mPreviousPriority(ANDROID_PRIORITY_NORMAL), mPreviousSchedulingGroup(ANDROID_TGROUP_DEFAULT)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080082{
83}
84
85AudioTrack::AudioTrack(
Glenn Kastenbc1d77b2012-01-12 16:38:12 -080086 audio_stream_type_t streamType,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080087 uint32_t sampleRate,
Glenn Kasten1c5a89d2012-01-04 09:36:37 -080088 audio_format_t format,
Jean-Michel Trivi54392232011-05-24 15:53:33 -070089 int channelMask,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080090 int frameCount,
91 uint32_t flags,
92 callback_t cbf,
93 void* user,
Eric Laurent65b65452010-06-01 23:49:17 -070094 int notificationFrames,
95 int sessionId)
Glenn Kasten99d54432011-06-22 16:15:25 -070096 : mStatus(NO_INIT),
97 mPreviousPriority(ANDROID_PRIORITY_NORMAL), mPreviousSchedulingGroup(ANDROID_TGROUP_DEFAULT)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080098{
Jean-Michel Trivi54392232011-05-24 15:53:33 -070099 mStatus = set(streamType, sampleRate, format, channelMask,
Eric Laurent619346f2010-06-21 09:27:30 -0700100 frameCount, flags, cbf, user, notificationFrames,
101 0, false, sessionId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800102}
103
104AudioTrack::AudioTrack(
Glenn Kastenbc1d77b2012-01-12 16:38:12 -0800105 audio_stream_type_t streamType,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800106 uint32_t sampleRate,
Glenn Kasten1c5a89d2012-01-04 09:36:37 -0800107 audio_format_t format,
Jean-Michel Trivi54392232011-05-24 15:53:33 -0700108 int channelMask,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800109 const sp<IMemory>& sharedBuffer,
110 uint32_t flags,
111 callback_t cbf,
112 void* user,
Eric Laurent65b65452010-06-01 23:49:17 -0700113 int notificationFrames,
114 int sessionId)
Glenn Kasten99d54432011-06-22 16:15:25 -0700115 : mStatus(NO_INIT),
116 mPreviousPriority(ANDROID_PRIORITY_NORMAL), mPreviousSchedulingGroup(ANDROID_TGROUP_DEFAULT)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800117{
Jean-Michel Trivi54392232011-05-24 15:53:33 -0700118 mStatus = set(streamType, sampleRate, format, channelMask,
Eric Laurent619346f2010-06-21 09:27:30 -0700119 0, flags, cbf, user, notificationFrames,
120 sharedBuffer, false, sessionId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800121}
122
123AudioTrack::~AudioTrack()
124{
Steve Block71f2cf12011-10-20 11:56:00 +0100125 ALOGV_IF(mSharedBuffer != 0, "Destructor sharedBuffer: %p", mSharedBuffer->pointer());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800126
127 if (mStatus == NO_ERROR) {
128 // Make sure that callback function exits in the case where
129 // it is looping on buffer full condition in obtainBuffer().
130 // Otherwise the callback thread will never exit.
131 stop();
132 if (mAudioTrackThread != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800133 mAudioTrackThread->requestExitAndWait();
134 mAudioTrackThread.clear();
135 }
136 mAudioTrack.clear();
137 IPCThreadState::self()->flushCommands();
Marco Nelissenc74b93f2011-08-02 13:33:41 -0700138 AudioSystem::releaseAudioSessionId(mSessionId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800139 }
140}
141
142status_t AudioTrack::set(
Glenn Kastenbc1d77b2012-01-12 16:38:12 -0800143 audio_stream_type_t streamType,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800144 uint32_t sampleRate,
Glenn Kasten1c5a89d2012-01-04 09:36:37 -0800145 audio_format_t format,
Jean-Michel Trivi54392232011-05-24 15:53:33 -0700146 int channelMask,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800147 int frameCount,
148 uint32_t flags,
149 callback_t cbf,
150 void* user,
151 int notificationFrames,
152 const sp<IMemory>& sharedBuffer,
Eric Laurent65b65452010-06-01 23:49:17 -0700153 bool threadCanCallJava,
154 int sessionId)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800155{
156
Steve Block71f2cf12011-10-20 11:56:00 +0100157 ALOGV_IF(sharedBuffer != 0, "sharedBuffer: %p, size: %d", sharedBuffer->pointer(), sharedBuffer->size());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800158
Eric Laurent421ddc02011-03-07 14:52:59 -0800159 AutoMutex lock(mLock);
Eric Laurentef028272009-04-21 07:56:33 -0700160 if (mAudioTrack != 0) {
Steve Block3762c312012-01-06 19:20:56 +0000161 ALOGE("Track already in use");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800162 return INVALID_OPERATION;
163 }
164
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800165 int afSampleRate;
166 if (AudioSystem::getOutputSamplingRate(&afSampleRate, streamType) != NO_ERROR) {
167 return NO_INIT;
168 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800169 uint32_t afLatency;
170 if (AudioSystem::getOutputLatency(&afLatency, streamType) != NO_ERROR) {
171 return NO_INIT;
172 }
173
174 // handle default values first.
Dima Zavin24fc2fb2011-04-19 22:30:36 -0700175 if (streamType == AUDIO_STREAM_DEFAULT) {
176 streamType = AUDIO_STREAM_MUSIC;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800177 }
178 if (sampleRate == 0) {
179 sampleRate = afSampleRate;
180 }
181 // these below should probably come from the audioFlinger too...
Glenn Kasten1c5a89d2012-01-04 09:36:37 -0800182 if (format == AUDIO_FORMAT_DEFAULT) {
Dima Zavin24fc2fb2011-04-19 22:30:36 -0700183 format = AUDIO_FORMAT_PCM_16_BIT;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800184 }
Jean-Michel Trivi54392232011-05-24 15:53:33 -0700185 if (channelMask == 0) {
186 channelMask = AUDIO_CHANNEL_OUT_STEREO;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800187 }
188
189 // validate parameters
Dima Zavin24fc2fb2011-04-19 22:30:36 -0700190 if (!audio_is_valid_format(format)) {
Steve Block3762c312012-01-06 19:20:56 +0000191 ALOGE("Invalid format");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800192 return BAD_VALUE;
193 }
Eric Laurenta553c252009-07-17 12:17:14 -0700194
195 // force direct flag if format is not linear PCM
Dima Zavin24fc2fb2011-04-19 22:30:36 -0700196 if (!audio_is_linear_pcm(format)) {
197 flags |= AUDIO_POLICY_OUTPUT_FLAG_DIRECT;
Eric Laurenta553c252009-07-17 12:17:14 -0700198 }
199
Jean-Michel Trivi54392232011-05-24 15:53:33 -0700200 if (!audio_is_output_channel(channelMask)) {
Steve Block3762c312012-01-06 19:20:56 +0000201 ALOGE("Invalid channel mask");
Eric Laurenta553c252009-07-17 12:17:14 -0700202 return BAD_VALUE;
203 }
Jean-Michel Trivi54392232011-05-24 15:53:33 -0700204 uint32_t channelCount = popcount(channelMask);
Eric Laurenta553c252009-07-17 12:17:14 -0700205
Dima Zavin24fc2fb2011-04-19 22:30:36 -0700206 audio_io_handle_t output = AudioSystem::getOutput(
Glenn Kastenbc1d77b2012-01-12 16:38:12 -0800207 streamType,
Glenn Kasten1c5a89d2012-01-04 09:36:37 -0800208 sampleRate, format, channelMask,
Dima Zavin24fc2fb2011-04-19 22:30:36 -0700209 (audio_policy_output_flags_t)flags);
Eric Laurenta553c252009-07-17 12:17:14 -0700210
211 if (output == 0) {
Steve Block3762c312012-01-06 19:20:56 +0000212 ALOGE("Could not get audio output for stream type %d", streamType);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800213 return BAD_VALUE;
214 }
215
Eric Laurentbda74692009-11-04 08:27:26 -0800216 mVolume[LEFT] = 1.0f;
217 mVolume[RIGHT] = 1.0f;
Glenn Kasten4790bd82012-01-03 14:22:33 -0800218 mSendLevel = 0.0f;
Eric Laurenteb8f850d2010-05-14 03:26:45 -0700219 mFrameCount = frameCount;
220 mNotificationFramesReq = notificationFrames;
Eric Laurent65b65452010-06-01 23:49:17 -0700221 mSessionId = sessionId;
Eric Laurent7070b362010-07-16 07:43:46 -0700222 mAuxEffectId = 0;
Eric Laurent65b65452010-06-01 23:49:17 -0700223
Eric Laurentbda74692009-11-04 08:27:26 -0800224 // create the IAudioTrack
Eric Laurent421ddc02011-03-07 14:52:59 -0800225 status_t status = createTrack_l(streamType,
226 sampleRate,
Glenn Kasten1c5a89d2012-01-04 09:36:37 -0800227 format,
Jean-Michel Trivi54392232011-05-24 15:53:33 -0700228 (uint32_t)channelMask,
Eric Laurent421ddc02011-03-07 14:52:59 -0800229 frameCount,
230 flags,
231 sharedBuffer,
232 output,
233 true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800234
Eric Laurentbda74692009-11-04 08:27:26 -0800235 if (status != NO_ERROR) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800236 return status;
237 }
Eric Laurentbda74692009-11-04 08:27:26 -0800238
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800239 if (cbf != 0) {
240 mAudioTrackThread = new AudioTrackThread(*this, threadCanCallJava);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800241 }
242
243 mStatus = NO_ERROR;
244
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800245 mStreamType = streamType;
Glenn Kasten1c5a89d2012-01-04 09:36:37 -0800246 mFormat = format;
Jean-Michel Trivi54392232011-05-24 15:53:33 -0700247 mChannelMask = (uint32_t)channelMask;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800248 mChannelCount = channelCount;
249 mSharedBuffer = sharedBuffer;
250 mMuted = false;
Glenn Kastene6810ff2012-01-03 09:42:47 -0800251 mActive = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800252 mCbf = cbf;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800253 mUserData = user;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800254 mLoopCount = 0;
255 mMarkerPosition = 0;
Jean-Michel Trivi4a5c1a72009-03-24 18:11:07 -0700256 mMarkerReached = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800257 mNewPosition = 0;
258 mUpdatePeriod = 0;
Jean-Michel Trivi22cb2042011-08-25 16:47:23 -0700259 mFlushed = false;
Eric Laurenta553c252009-07-17 12:17:14 -0700260 mFlags = flags;
Marco Nelissenc74b93f2011-08-02 13:33:41 -0700261 AudioSystem::acquireAudioSessionId(mSessionId);
Eric Laurent7e8626f2011-09-13 15:04:17 -0700262 mRestoreStatus = NO_ERROR;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800263 return NO_ERROR;
264}
265
266status_t AudioTrack::initCheck() const
267{
268 return mStatus;
269}
270
271// -------------------------------------------------------------------------
272
273uint32_t AudioTrack::latency() const
274{
275 return mLatency;
276}
277
Glenn Kastenbc1d77b2012-01-12 16:38:12 -0800278audio_stream_type_t AudioTrack::streamType() const
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800279{
280 return mStreamType;
281}
282
Glenn Kasten1c5a89d2012-01-04 09:36:37 -0800283audio_format_t AudioTrack::format() const
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800284{
285 return mFormat;
286}
287
288int AudioTrack::channelCount() const
289{
290 return mChannelCount;
291}
292
293uint32_t AudioTrack::frameCount() const
294{
Eric Laurenteb8f850d2010-05-14 03:26:45 -0700295 return mCblk->frameCount;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800296}
297
Glenn Kastenfaf354d2012-01-11 09:48:27 -0800298size_t AudioTrack::frameSize() const
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800299{
Dima Zavin24fc2fb2011-04-19 22:30:36 -0700300 if (audio_is_linear_pcm(mFormat)) {
Eric Laurentc310dcb2011-06-16 21:30:45 -0700301 return channelCount()*audio_bytes_per_sample(mFormat);
Eric Laurenta553c252009-07-17 12:17:14 -0700302 } else {
303 return sizeof(uint8_t);
304 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800305}
306
307sp<IMemory>& AudioTrack::sharedBuffer()
308{
309 return mSharedBuffer;
310}
311
312// -------------------------------------------------------------------------
313
314void AudioTrack::start()
315{
316 sp<AudioTrackThread> t = mAudioTrackThread;
Glenn Kasten028ab992011-06-22 16:18:04 -0700317 status_t status = NO_ERROR;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800318
Steve Block71f2cf12011-10-20 11:56:00 +0100319 ALOGV("start %p", this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800320 if (t != 0) {
321 if (t->exitPending()) {
322 if (t->requestExitAndWait() == WOULD_BLOCK) {
Steve Block3762c312012-01-06 19:20:56 +0000323 ALOGE("AudioTrack::start called from thread");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800324 return;
325 }
326 }
327 t->mLock.lock();
328 }
329
Eric Laurentf3d6dd02010-11-18 08:40:16 -0800330 AutoMutex lock(mLock);
Eric Laurent421ddc02011-03-07 14:52:59 -0800331 // acquire a strong reference on the IMemory and IAudioTrack so that they cannot be destroyed
332 // while we are accessing the cblk
333 sp <IAudioTrack> audioTrack = mAudioTrack;
334 sp <IMemory> iMem = mCblkMemory;
335 audio_track_cblk_t* cblk = mCblk;
336
Glenn Kastene6810ff2012-01-03 09:42:47 -0800337 if (!mActive) {
Jean-Michel Trivi22cb2042011-08-25 16:47:23 -0700338 mFlushed = false;
Glenn Kastene6810ff2012-01-03 09:42:47 -0800339 mActive = true;
Eric Laurent421ddc02011-03-07 14:52:59 -0800340 mNewPosition = cblk->server + mUpdatePeriod;
Eric Laurent913af0b2011-03-17 09:36:51 -0700341 cblk->lock.lock();
Eric Laurent421ddc02011-03-07 14:52:59 -0800342 cblk->bufferTimeoutMs = MAX_STARTUP_TIMEOUT_MS;
343 cblk->waitTimeMs = 0;
Eric Laurentae29b762011-03-28 18:37:07 -0700344 android_atomic_and(~CBLK_DISABLED_ON, &cblk->flags);
Eric Laurent059b4be2009-11-09 23:32:22 -0800345 if (t != 0) {
Glenn Kasten99d54432011-06-22 16:15:25 -0700346 t->run("AudioTrackThread", ANDROID_PRIORITY_AUDIO);
Eric Laurent059b4be2009-11-09 23:32:22 -0800347 } else {
Glenn Kasten99d54432011-06-22 16:15:25 -0700348 mPreviousPriority = getpriority(PRIO_PROCESS, 0);
349 mPreviousSchedulingGroup = androidGetThreadSchedulingGroup(0);
350 androidSetThreadPriority(0, ANDROID_PRIORITY_AUDIO);
Eric Laurent059b4be2009-11-09 23:32:22 -0800351 }
352
Steve Block71f2cf12011-10-20 11:56:00 +0100353 ALOGV("start %p before lock cblk %p", this, mCblk);
Eric Laurent421ddc02011-03-07 14:52:59 -0800354 if (!(cblk->flags & CBLK_INVALID_MSK)) {
355 cblk->lock.unlock();
Eric Laurenteb8f850d2010-05-14 03:26:45 -0700356 status = mAudioTrack->start();
Eric Laurent421ddc02011-03-07 14:52:59 -0800357 cblk->lock.lock();
358 if (status == DEAD_OBJECT) {
Eric Laurentae29b762011-03-28 18:37:07 -0700359 android_atomic_or(CBLK_INVALID_ON, &cblk->flags);
Eric Laurent49f02be2009-11-19 09:00:56 -0800360 }
Eric Laurent059b4be2009-11-09 23:32:22 -0800361 }
Eric Laurent421ddc02011-03-07 14:52:59 -0800362 if (cblk->flags & CBLK_INVALID_MSK) {
363 status = restoreTrack_l(cblk, true);
364 }
365 cblk->lock.unlock();
Eric Laurent059b4be2009-11-09 23:32:22 -0800366 if (status != NO_ERROR) {
Steve Block71f2cf12011-10-20 11:56:00 +0100367 ALOGV("start() failed");
Glenn Kastene6810ff2012-01-03 09:42:47 -0800368 mActive = false;
Eric Laurent059b4be2009-11-09 23:32:22 -0800369 if (t != 0) {
370 t->requestExit();
371 } else {
Glenn Kasten99d54432011-06-22 16:15:25 -0700372 setpriority(PRIO_PROCESS, 0, mPreviousPriority);
373 androidSetThreadSchedulingGroup(0, mPreviousSchedulingGroup);
Eric Laurent059b4be2009-11-09 23:32:22 -0800374 }
Eric Laurentbda74692009-11-04 08:27:26 -0800375 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800376 }
377
378 if (t != 0) {
379 t->mLock.unlock();
380 }
381}
382
383void AudioTrack::stop()
384{
385 sp<AudioTrackThread> t = mAudioTrackThread;
386
Steve Block71f2cf12011-10-20 11:56:00 +0100387 ALOGV("stop %p", this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800388 if (t != 0) {
389 t->mLock.lock();
390 }
391
Eric Laurentf3d6dd02010-11-18 08:40:16 -0800392 AutoMutex lock(mLock);
Glenn Kastene6810ff2012-01-03 09:42:47 -0800393 if (mActive) {
394 mActive = false;
Eric Laurentef028272009-04-21 07:56:33 -0700395 mCblk->cv.signal();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800396 mAudioTrack->stop();
397 // Cancel loops (If we are in the middle of a loop, playback
398 // would not stop until loopCount reaches 0).
Eric Laurent421ddc02011-03-07 14:52:59 -0800399 setLoop_l(0, 0, 0);
Jean-Michel Trivi4a5c1a72009-03-24 18:11:07 -0700400 // the playback head position will reset to 0, so if a marker is set, we need
401 // to activate it again
402 mMarkerReached = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800403 // Force flush if a shared buffer is used otherwise audioflinger
404 // will not stop before end of buffer is reached.
405 if (mSharedBuffer != 0) {
Eric Laurent421ddc02011-03-07 14:52:59 -0800406 flush_l();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800407 }
408 if (t != 0) {
409 t->requestExit();
410 } else {
Glenn Kasten99d54432011-06-22 16:15:25 -0700411 setpriority(PRIO_PROCESS, 0, mPreviousPriority);
412 androidSetThreadSchedulingGroup(0, mPreviousSchedulingGroup);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800413 }
414 }
415
416 if (t != 0) {
417 t->mLock.unlock();
418 }
419}
420
421bool AudioTrack::stopped() const
422{
Glenn Kastene6810ff2012-01-03 09:42:47 -0800423 AutoMutex lock(mLock);
424 return stopped_l();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800425}
426
427void AudioTrack::flush()
428{
Eric Laurent421ddc02011-03-07 14:52:59 -0800429 AutoMutex lock(mLock);
430 flush_l();
431}
432
433// must be called with mLock held
434void AudioTrack::flush_l()
435{
Steve Block71f2cf12011-10-20 11:56:00 +0100436 ALOGV("flush");
Eric Laurenta553c252009-07-17 12:17:14 -0700437
Jean-Michel Trivi4a5c1a72009-03-24 18:11:07 -0700438 // clear playback marker and periodic update counter
439 mMarkerPosition = 0;
440 mMarkerReached = false;
441 mUpdatePeriod = 0;
Eric Laurenta553c252009-07-17 12:17:14 -0700442
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800443 if (!mActive) {
Jean-Michel Trivi22cb2042011-08-25 16:47:23 -0700444 mFlushed = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800445 mAudioTrack->flush();
446 // Release AudioTrack callback thread in case it was waiting for new buffers
447 // in AudioTrack::obtainBuffer()
448 mCblk->cv.signal();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800449 }
450}
451
452void AudioTrack::pause()
453{
Steve Block71f2cf12011-10-20 11:56:00 +0100454 ALOGV("pause");
Eric Laurentf3d6dd02010-11-18 08:40:16 -0800455 AutoMutex lock(mLock);
Glenn Kastene6810ff2012-01-03 09:42:47 -0800456 if (mActive) {
457 mActive = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800458 mAudioTrack->pause();
459 }
460}
461
462void AudioTrack::mute(bool e)
463{
464 mAudioTrack->mute(e);
465 mMuted = e;
466}
467
468bool AudioTrack::muted() const
469{
470 return mMuted;
471}
472
Eric Laurent65b65452010-06-01 23:49:17 -0700473status_t AudioTrack::setVolume(float left, float right)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800474{
Glenn Kasten1c50a782011-11-30 09:46:04 -0800475 if (left < 0.0f || left > 1.0f || right < 0.0f || right > 1.0f) {
Eric Laurent65b65452010-06-01 23:49:17 -0700476 return BAD_VALUE;
477 }
478
Eric Laurent421ddc02011-03-07 14:52:59 -0800479 AutoMutex lock(mLock);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800480 mVolume[LEFT] = left;
481 mVolume[RIGHT] = right;
482
Eric Laurent65b65452010-06-01 23:49:17 -0700483 mCblk->volumeLR = (uint32_t(uint16_t(right * 0x1000)) << 16) | uint16_t(left * 0x1000);
484
485 return NO_ERROR;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800486}
487
488void AudioTrack::getVolume(float* left, float* right)
489{
Eric Laurent65b65452010-06-01 23:49:17 -0700490 if (left != NULL) {
491 *left = mVolume[LEFT];
492 }
493 if (right != NULL) {
494 *right = mVolume[RIGHT];
495 }
496}
497
Eric Laurent7070b362010-07-16 07:43:46 -0700498status_t AudioTrack::setAuxEffectSendLevel(float level)
Eric Laurent65b65452010-06-01 23:49:17 -0700499{
Steve Block71f2cf12011-10-20 11:56:00 +0100500 ALOGV("setAuxEffectSendLevel(%f)", level);
Glenn Kasten4790bd82012-01-03 14:22:33 -0800501 if (level < 0.0f || level > 1.0f) {
Eric Laurent65b65452010-06-01 23:49:17 -0700502 return BAD_VALUE;
503 }
Eric Laurent421ddc02011-03-07 14:52:59 -0800504 AutoMutex lock(mLock);
Eric Laurent65b65452010-06-01 23:49:17 -0700505
506 mSendLevel = level;
507
Glenn Kasten4790bd82012-01-03 14:22:33 -0800508 mCblk->setSendLevel(level);
Eric Laurent65b65452010-06-01 23:49:17 -0700509
510 return NO_ERROR;
511}
512
Eric Laurent7070b362010-07-16 07:43:46 -0700513void AudioTrack::getAuxEffectSendLevel(float* level)
Eric Laurent65b65452010-06-01 23:49:17 -0700514{
515 if (level != NULL) {
516 *level = mSendLevel;
517 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800518}
519
Eric Laurent88e209d2009-07-07 07:10:45 -0700520status_t AudioTrack::setSampleRate(int rate)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800521{
522 int afSamplingRate;
523
524 if (AudioSystem::getOutputSamplingRate(&afSamplingRate, mStreamType) != NO_ERROR) {
Eric Laurent88e209d2009-07-07 07:10:45 -0700525 return NO_INIT;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800526 }
527 // Resampler implementation limits input sampling rate to 2 x output sampling rate.
Eric Laurent88e209d2009-07-07 07:10:45 -0700528 if (rate <= 0 || rate > afSamplingRate*2 ) return BAD_VALUE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800529
Eric Laurent421ddc02011-03-07 14:52:59 -0800530 AutoMutex lock(mLock);
Eric Laurent88e209d2009-07-07 07:10:45 -0700531 mCblk->sampleRate = rate;
532 return NO_ERROR;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800533}
534
535uint32_t AudioTrack::getSampleRate()
536{
Eric Laurent421ddc02011-03-07 14:52:59 -0800537 AutoMutex lock(mLock);
Eric Laurent88e209d2009-07-07 07:10:45 -0700538 return mCblk->sampleRate;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800539}
540
541status_t AudioTrack::setLoop(uint32_t loopStart, uint32_t loopEnd, int loopCount)
542{
Eric Laurent421ddc02011-03-07 14:52:59 -0800543 AutoMutex lock(mLock);
544 return setLoop_l(loopStart, loopEnd, loopCount);
545}
546
547// must be called with mLock held
548status_t AudioTrack::setLoop_l(uint32_t loopStart, uint32_t loopEnd, int loopCount)
549{
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800550 audio_track_cblk_t* cblk = mCblk;
551
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800552 Mutex::Autolock _l(cblk->lock);
553
554 if (loopCount == 0) {
555 cblk->loopStart = UINT_MAX;
556 cblk->loopEnd = UINT_MAX;
557 cblk->loopCount = 0;
558 mLoopCount = 0;
559 return NO_ERROR;
560 }
561
562 if (loopStart >= loopEnd ||
Eric Laurent6667ac32011-03-21 11:49:00 -0700563 loopEnd - loopStart > cblk->frameCount ||
564 cblk->server > loopStart) {
Steve Block3762c312012-01-06 19:20:56 +0000565 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 Project9066cfe2009-03-03 19:31:44 -0800566 return BAD_VALUE;
567 }
568
Eric Laurent6667ac32011-03-21 11:49:00 -0700569 if ((mSharedBuffer != 0) && (loopEnd > cblk->frameCount)) {
Steve Block3762c312012-01-06 19:20:56 +0000570 ALOGE("setLoop invalid value: loop markers beyond data: loopStart %d, loopEnd %d, framecount %d",
Eric Laurenteb8f850d2010-05-14 03:26:45 -0700571 loopStart, loopEnd, cblk->frameCount);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800572 return BAD_VALUE;
Eric Laurenta553c252009-07-17 12:17:14 -0700573 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800574
575 cblk->loopStart = loopStart;
576 cblk->loopEnd = loopEnd;
577 cblk->loopCount = loopCount;
578 mLoopCount = loopCount;
579
580 return NO_ERROR;
581}
582
583status_t AudioTrack::getLoop(uint32_t *loopStart, uint32_t *loopEnd, int *loopCount)
584{
Eric Laurent421ddc02011-03-07 14:52:59 -0800585 AutoMutex lock(mLock);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800586 if (loopStart != 0) {
587 *loopStart = mCblk->loopStart;
588 }
589 if (loopEnd != 0) {
590 *loopEnd = mCblk->loopEnd;
591 }
592 if (loopCount != 0) {
593 if (mCblk->loopCount < 0) {
594 *loopCount = -1;
595 } else {
596 *loopCount = mCblk->loopCount;
597 }
598 }
599
600 return NO_ERROR;
601}
602
603status_t AudioTrack::setMarkerPosition(uint32_t marker)
604{
605 if (mCbf == 0) return INVALID_OPERATION;
606
607 mMarkerPosition = marker;
Jean-Michel Trivi4a5c1a72009-03-24 18:11:07 -0700608 mMarkerReached = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800609
610 return NO_ERROR;
611}
612
613status_t AudioTrack::getMarkerPosition(uint32_t *marker)
614{
615 if (marker == 0) return BAD_VALUE;
616
617 *marker = mMarkerPosition;
618
619 return NO_ERROR;
620}
621
622status_t AudioTrack::setPositionUpdatePeriod(uint32_t updatePeriod)
623{
624 if (mCbf == 0) return INVALID_OPERATION;
625
626 uint32_t curPosition;
627 getPosition(&curPosition);
628 mNewPosition = curPosition + updatePeriod;
629 mUpdatePeriod = updatePeriod;
630
631 return NO_ERROR;
632}
633
634status_t AudioTrack::getPositionUpdatePeriod(uint32_t *updatePeriod)
635{
636 if (updatePeriod == 0) return BAD_VALUE;
637
638 *updatePeriod = mUpdatePeriod;
639
640 return NO_ERROR;
641}
642
643status_t AudioTrack::setPosition(uint32_t position)
644{
Eric Laurent421ddc02011-03-07 14:52:59 -0800645 AutoMutex lock(mLock);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800646
Glenn Kastene6810ff2012-01-03 09:42:47 -0800647 if (!stopped_l()) return INVALID_OPERATION;
648
649 Mutex::Autolock _l(mCblk->lock);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800650
651 if (position > mCblk->user) return BAD_VALUE;
652
653 mCblk->server = position;
Eric Laurentae29b762011-03-28 18:37:07 -0700654 android_atomic_or(CBLK_FORCEREADY_ON, &mCblk->flags);
Eric Laurenta553c252009-07-17 12:17:14 -0700655
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800656 return NO_ERROR;
657}
658
659status_t AudioTrack::getPosition(uint32_t *position)
660{
661 if (position == 0) return BAD_VALUE;
Eric Laurent421ddc02011-03-07 14:52:59 -0800662 AutoMutex lock(mLock);
Jean-Michel Trivi22cb2042011-08-25 16:47:23 -0700663 *position = mFlushed ? 0 : mCblk->server;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800664
665 return NO_ERROR;
666}
667
668status_t AudioTrack::reload()
669{
Eric Laurent421ddc02011-03-07 14:52:59 -0800670 AutoMutex lock(mLock);
671
Glenn Kastene6810ff2012-01-03 09:42:47 -0800672 if (!stopped_l()) return INVALID_OPERATION;
Eric Laurenta553c252009-07-17 12:17:14 -0700673
Eric Laurent421ddc02011-03-07 14:52:59 -0800674 flush_l();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800675
Eric Laurenteb8f850d2010-05-14 03:26:45 -0700676 mCblk->stepUser(mCblk->frameCount);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800677
678 return NO_ERROR;
679}
680
Eric Laurenta553c252009-07-17 12:17:14 -0700681audio_io_handle_t AudioTrack::getOutput()
682{
Eric Laurent421ddc02011-03-07 14:52:59 -0800683 AutoMutex lock(mLock);
684 return getOutput_l();
685}
686
687// must be called with mLock held
688audio_io_handle_t AudioTrack::getOutput_l()
689{
Glenn Kastenbc1d77b2012-01-12 16:38:12 -0800690 return AudioSystem::getOutput(mStreamType,
Jean-Michel Trivi54392232011-05-24 15:53:33 -0700691 mCblk->sampleRate, mFormat, mChannelMask, (audio_policy_output_flags_t)mFlags);
Eric Laurenta553c252009-07-17 12:17:14 -0700692}
693
Eric Laurent65b65452010-06-01 23:49:17 -0700694int AudioTrack::getSessionId()
695{
696 return mSessionId;
697}
698
699status_t AudioTrack::attachAuxEffect(int effectId)
700{
Steve Block71f2cf12011-10-20 11:56:00 +0100701 ALOGV("attachAuxEffect(%d)", effectId);
Eric Laurent7070b362010-07-16 07:43:46 -0700702 status_t status = mAudioTrack->attachAuxEffect(effectId);
703 if (status == NO_ERROR) {
704 mAuxEffectId = effectId;
705 }
706 return status;
Eric Laurent65b65452010-06-01 23:49:17 -0700707}
708
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800709// -------------------------------------------------------------------------
710
Eric Laurent421ddc02011-03-07 14:52:59 -0800711// must be called with mLock held
712status_t AudioTrack::createTrack_l(
Glenn Kastenbc1d77b2012-01-12 16:38:12 -0800713 audio_stream_type_t streamType,
Eric Laurentbda74692009-11-04 08:27:26 -0800714 uint32_t sampleRate,
Glenn Kasten1c5a89d2012-01-04 09:36:37 -0800715 audio_format_t format,
Jean-Michel Trivi54392232011-05-24 15:53:33 -0700716 uint32_t channelMask,
Eric Laurentbda74692009-11-04 08:27:26 -0800717 int frameCount,
718 uint32_t flags,
719 const sp<IMemory>& sharedBuffer,
Eric Laurenteb8f850d2010-05-14 03:26:45 -0700720 audio_io_handle_t output,
721 bool enforceFrameCount)
Eric Laurentbda74692009-11-04 08:27:26 -0800722{
723 status_t status;
724 const sp<IAudioFlinger>& audioFlinger = AudioSystem::get_audio_flinger();
725 if (audioFlinger == 0) {
Steve Block3762c312012-01-06 19:20:56 +0000726 ALOGE("Could not get audioflinger");
Eric Laurentbda74692009-11-04 08:27:26 -0800727 return NO_INIT;
728 }
729
Eric Laurenteb8f850d2010-05-14 03:26:45 -0700730 int afSampleRate;
731 if (AudioSystem::getOutputSamplingRate(&afSampleRate, streamType) != NO_ERROR) {
732 return NO_INIT;
733 }
734 int afFrameCount;
735 if (AudioSystem::getOutputFrameCount(&afFrameCount, streamType) != NO_ERROR) {
736 return NO_INIT;
737 }
738 uint32_t afLatency;
739 if (AudioSystem::getOutputLatency(&afLatency, streamType) != NO_ERROR) {
740 return NO_INIT;
741 }
742
743 mNotificationFramesAct = mNotificationFramesReq;
Dima Zavin24fc2fb2011-04-19 22:30:36 -0700744 if (!audio_is_linear_pcm(format)) {
Eric Laurenteb8f850d2010-05-14 03:26:45 -0700745 if (sharedBuffer != 0) {
746 frameCount = sharedBuffer->size();
747 }
748 } else {
749 // Ensure that buffer depth covers at least audio hardware latency
750 uint32_t minBufCount = afLatency / ((1000 * afFrameCount)/afSampleRate);
751 if (minBufCount < 2) minBufCount = 2;
752
753 int minFrameCount = (afFrameCount*sampleRate*minBufCount)/afSampleRate;
754
755 if (sharedBuffer == 0) {
756 if (frameCount == 0) {
757 frameCount = minFrameCount;
758 }
759 if (mNotificationFramesAct == 0) {
760 mNotificationFramesAct = frameCount/2;
761 }
762 // Make sure that application is notified with sufficient margin
763 // before underrun
764 if (mNotificationFramesAct > (uint32_t)frameCount/2) {
765 mNotificationFramesAct = frameCount/2;
766 }
767 if (frameCount < minFrameCount) {
768 if (enforceFrameCount) {
Steve Block3762c312012-01-06 19:20:56 +0000769 ALOGE("Invalid buffer size: minFrameCount %d, frameCount %d", minFrameCount, frameCount);
Eric Laurenteb8f850d2010-05-14 03:26:45 -0700770 return BAD_VALUE;
771 } else {
772 frameCount = minFrameCount;
773 }
774 }
775 } else {
776 // Ensure that buffer alignment matches channelcount
Jean-Michel Trivi54392232011-05-24 15:53:33 -0700777 int channelCount = popcount(channelMask);
Eric Laurenteb8f850d2010-05-14 03:26:45 -0700778 if (((uint32_t)sharedBuffer->pointer() & (channelCount | 1)) != 0) {
Steve Block3762c312012-01-06 19:20:56 +0000779 ALOGE("Invalid buffer alignement: address %p, channelCount %d", sharedBuffer->pointer(), channelCount);
Eric Laurenteb8f850d2010-05-14 03:26:45 -0700780 return BAD_VALUE;
781 }
782 frameCount = sharedBuffer->size()/channelCount/sizeof(int16_t);
783 }
784 }
785
Eric Laurentbda74692009-11-04 08:27:26 -0800786 sp<IAudioTrack> track = audioFlinger->createTrack(getpid(),
787 streamType,
788 sampleRate,
789 format,
Jean-Michel Trivi54392232011-05-24 15:53:33 -0700790 channelMask,
Eric Laurentbda74692009-11-04 08:27:26 -0800791 frameCount,
792 ((uint16_t)flags) << 16,
793 sharedBuffer,
794 output,
Eric Laurent65b65452010-06-01 23:49:17 -0700795 &mSessionId,
Eric Laurentbda74692009-11-04 08:27:26 -0800796 &status);
797
798 if (track == 0) {
Steve Block3762c312012-01-06 19:20:56 +0000799 ALOGE("AudioFlinger could not create track, status: %d", status);
Eric Laurentbda74692009-11-04 08:27:26 -0800800 return status;
801 }
802 sp<IMemory> cblk = track->getCblk();
803 if (cblk == 0) {
Steve Block3762c312012-01-06 19:20:56 +0000804 ALOGE("Could not get control block");
Eric Laurentbda74692009-11-04 08:27:26 -0800805 return NO_INIT;
806 }
Eric Laurentbda74692009-11-04 08:27:26 -0800807 mAudioTrack = track;
Eric Laurentbda74692009-11-04 08:27:26 -0800808 mCblkMemory = cblk;
809 mCblk = static_cast<audio_track_cblk_t*>(cblk->pointer());
Eric Laurentae29b762011-03-28 18:37:07 -0700810 android_atomic_or(CBLK_DIRECTION_OUT, &mCblk->flags);
Eric Laurentbda74692009-11-04 08:27:26 -0800811 if (sharedBuffer == 0) {
812 mCblk->buffers = (char*)mCblk + sizeof(audio_track_cblk_t);
813 } else {
814 mCblk->buffers = sharedBuffer->pointer();
815 // Force buffer full condition as data is already present in shared memory
Eric Laurenteb8f850d2010-05-14 03:26:45 -0700816 mCblk->stepUser(mCblk->frameCount);
Eric Laurentbda74692009-11-04 08:27:26 -0800817 }
818
Eric Laurent65b65452010-06-01 23:49:17 -0700819 mCblk->volumeLR = (uint32_t(uint16_t(mVolume[RIGHT] * 0x1000)) << 16) | uint16_t(mVolume[LEFT] * 0x1000);
Glenn Kasten4790bd82012-01-03 14:22:33 -0800820 mCblk->setSendLevel(mSendLevel);
Eric Laurent7070b362010-07-16 07:43:46 -0700821 mAudioTrack->attachAuxEffect(mAuxEffectId);
Eric Laurent49f02be2009-11-19 09:00:56 -0800822 mCblk->bufferTimeoutMs = MAX_STARTUP_TIMEOUT_MS;
823 mCblk->waitTimeMs = 0;
Eric Laurenteb8f850d2010-05-14 03:26:45 -0700824 mRemainingFrames = mNotificationFramesAct;
825 mLatency = afLatency + (1000*mCblk->frameCount) / sampleRate;
Eric Laurentbda74692009-11-04 08:27:26 -0800826 return NO_ERROR;
827}
828
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800829status_t AudioTrack::obtainBuffer(Buffer* audioBuffer, int32_t waitCount)
830{
Eric Laurent421ddc02011-03-07 14:52:59 -0800831 AutoMutex lock(mLock);
Glenn Kastene6810ff2012-01-03 09:42:47 -0800832 bool active;
Glenn Kasten028ab992011-06-22 16:18:04 -0700833 status_t result = NO_ERROR;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800834 audio_track_cblk_t* cblk = mCblk;
835 uint32_t framesReq = audioBuffer->frameCount;
Eric Laurentef028272009-04-21 07:56:33 -0700836 uint32_t waitTimeMs = (waitCount < 0) ? cblk->bufferTimeoutMs : WAIT_PERIOD_MS;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800837
838 audioBuffer->frameCount = 0;
839 audioBuffer->size = 0;
840
841 uint32_t framesAvail = cblk->framesAvailable();
842
Eric Laurent6667ac32011-03-21 11:49:00 -0700843 cblk->lock.lock();
844 if (cblk->flags & CBLK_INVALID_MSK) {
845 goto create_new_track;
846 }
847 cblk->lock.unlock();
848
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800849 if (framesAvail == 0) {
Eric Laurentbda74692009-11-04 08:27:26 -0800850 cblk->lock.lock();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800851 goto start_loop_here;
852 while (framesAvail == 0) {
853 active = mActive;
Glenn Kastene80a4cc2011-12-15 09:51:17 -0800854 if (CC_UNLIKELY(!active)) {
Steve Block71f2cf12011-10-20 11:56:00 +0100855 ALOGV("Not active and NO_MORE_BUFFERS");
Eric Laurentbda74692009-11-04 08:27:26 -0800856 cblk->lock.unlock();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800857 return NO_MORE_BUFFERS;
858 }
Glenn Kastene80a4cc2011-12-15 09:51:17 -0800859 if (CC_UNLIKELY(!waitCount)) {
Eric Laurentbda74692009-11-04 08:27:26 -0800860 cblk->lock.unlock();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800861 return WOULD_BLOCK;
Eric Laurentbda74692009-11-04 08:27:26 -0800862 }
Eric Laurenteb8f850d2010-05-14 03:26:45 -0700863 if (!(cblk->flags & CBLK_INVALID_MSK)) {
Eric Laurent421ddc02011-03-07 14:52:59 -0800864 mLock.unlock();
Eric Laurenteb8f850d2010-05-14 03:26:45 -0700865 result = cblk->cv.waitRelative(cblk->lock, milliseconds(waitTimeMs));
Eric Laurenteb8f850d2010-05-14 03:26:45 -0700866 cblk->lock.unlock();
Eric Laurent421ddc02011-03-07 14:52:59 -0800867 mLock.lock();
Glenn Kastene6810ff2012-01-03 09:42:47 -0800868 if (!mActive) {
Eric Laurent421ddc02011-03-07 14:52:59 -0800869 return status_t(STOPPED);
870 }
871 cblk->lock.lock();
872 }
873
874 if (cblk->flags & CBLK_INVALID_MSK) {
Eric Laurenteb8f850d2010-05-14 03:26:45 -0700875 goto create_new_track;
876 }
Glenn Kastene80a4cc2011-12-15 09:51:17 -0800877 if (CC_UNLIKELY(result != NO_ERROR)) {
Eric Laurentef028272009-04-21 07:56:33 -0700878 cblk->waitTimeMs += waitTimeMs;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800879 if (cblk->waitTimeMs >= cblk->bufferTimeoutMs) {
880 // timing out when a loop has been set and we have already written upto loop end
881 // is a normal condition: no need to wake AudioFlinger up.
882 if (cblk->user < cblk->loopEnd) {
Steve Block8564c8d2012-01-05 23:22:43 +0000883 ALOGW( "obtainBuffer timed out (is the CPU pegged?) %p "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800884 "user=%08x, server=%08x", this, cblk->user, cblk->server);
Eric Laurenta553c252009-07-17 12:17:14 -0700885 //unlock cblk mutex before calling mAudioTrack->start() (see issue #1617140)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800886 cblk->lock.unlock();
Eric Laurentbda74692009-11-04 08:27:26 -0800887 result = mAudioTrack->start();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800888 cblk->lock.lock();
Eric Laurent421ddc02011-03-07 14:52:59 -0800889 if (result == DEAD_OBJECT) {
Eric Laurentae29b762011-03-28 18:37:07 -0700890 android_atomic_or(CBLK_INVALID_ON, &cblk->flags);
Eric Laurent421ddc02011-03-07 14:52:59 -0800891create_new_track:
892 result = restoreTrack_l(cblk, false);
893 }
894 if (result != NO_ERROR) {
Steve Block8564c8d2012-01-05 23:22:43 +0000895 ALOGW("obtainBuffer create Track error %d", result);
Eric Laurent421ddc02011-03-07 14:52:59 -0800896 cblk->lock.unlock();
897 return result;
898 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800899 }
900 cblk->waitTimeMs = 0;
901 }
Eric Laurenta553c252009-07-17 12:17:14 -0700902
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800903 if (--waitCount == 0) {
Eric Laurentbda74692009-11-04 08:27:26 -0800904 cblk->lock.unlock();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800905 return TIMED_OUT;
906 }
907 }
908 // read the server count again
909 start_loop_here:
910 framesAvail = cblk->framesAvailable_l();
911 }
Eric Laurentbda74692009-11-04 08:27:26 -0800912 cblk->lock.unlock();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800913 }
914
Eric Laurent4712baa2010-09-30 16:12:31 -0700915 // restart track if it was disabled by audioflinger due to previous underrun
Eric Laurent421ddc02011-03-07 14:52:59 -0800916 if (mActive && (cblk->flags & CBLK_DISABLED_MSK)) {
Eric Laurentae29b762011-03-28 18:37:07 -0700917 android_atomic_and(~CBLK_DISABLED_ON, &cblk->flags);
Steve Block8564c8d2012-01-05 23:22:43 +0000918 ALOGW("obtainBuffer() track %p disabled, restarting", this);
Eric Laurentae29b762011-03-28 18:37:07 -0700919 mAudioTrack->start();
Eric Laurent4712baa2010-09-30 16:12:31 -0700920 }
921
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800922 cblk->waitTimeMs = 0;
Eric Laurenta553c252009-07-17 12:17:14 -0700923
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800924 if (framesReq > framesAvail) {
925 framesReq = framesAvail;
926 }
927
928 uint32_t u = cblk->user;
929 uint32_t bufferEnd = cblk->userBase + cblk->frameCount;
930
931 if (u + framesReq > bufferEnd) {
932 framesReq = bufferEnd - u;
933 }
934
Eric Laurenta553c252009-07-17 12:17:14 -0700935 audioBuffer->flags = mMuted ? Buffer::MUTE : 0;
936 audioBuffer->channelCount = mChannelCount;
937 audioBuffer->frameCount = framesReq;
938 audioBuffer->size = framesReq * cblk->frameSize;
Dima Zavin24fc2fb2011-04-19 22:30:36 -0700939 if (audio_is_linear_pcm(mFormat)) {
940 audioBuffer->format = AUDIO_FORMAT_PCM_16_BIT;
Eric Laurenta553c252009-07-17 12:17:14 -0700941 } else {
942 audioBuffer->format = mFormat;
943 }
944 audioBuffer->raw = (int8_t *)cblk->buffer(u);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800945 active = mActive;
946 return active ? status_t(NO_ERROR) : status_t(STOPPED);
947}
948
949void AudioTrack::releaseBuffer(Buffer* audioBuffer)
950{
Eric Laurent421ddc02011-03-07 14:52:59 -0800951 AutoMutex lock(mLock);
952 mCblk->stepUser(audioBuffer->frameCount);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800953}
954
955// -------------------------------------------------------------------------
956
957ssize_t AudioTrack::write(const void* buffer, size_t userSize)
958{
959
960 if (mSharedBuffer != 0) return INVALID_OPERATION;
961
962 if (ssize_t(userSize) < 0) {
963 // sanity-check. user is most-likely passing an error code.
Steve Block3762c312012-01-06 19:20:56 +0000964 ALOGE("AudioTrack::write(buffer=%p, size=%u (%d)",
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800965 buffer, userSize, userSize);
966 return BAD_VALUE;
967 }
968
Steve Block71f2cf12011-10-20 11:56:00 +0100969 ALOGV("write %p: %d bytes, mActive=%d", this, userSize, mActive);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800970
Eric Laurent421ddc02011-03-07 14:52:59 -0800971 // acquire a strong reference on the IMemory and IAudioTrack so that they cannot be destroyed
972 // while we are accessing the cblk
973 mLock.lock();
974 sp <IAudioTrack> audioTrack = mAudioTrack;
975 sp <IMemory> iMem = mCblkMemory;
976 mLock.unlock();
977
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800978 ssize_t written = 0;
979 const int8_t *src = (const int8_t *)buffer;
980 Buffer audioBuffer;
Glenn Kastenfaf354d2012-01-11 09:48:27 -0800981 size_t frameSz = frameSize();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800982
983 do {
Eric Laurent913af0b2011-03-17 09:36:51 -0700984 audioBuffer.frameCount = userSize/frameSz;
Eric Laurenta553c252009-07-17 12:17:14 -0700985
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800986 // Calling obtainBuffer() with a negative wait count causes
987 // an (almost) infinite wait time.
988 status_t err = obtainBuffer(&audioBuffer, -1);
989 if (err < 0) {
990 // out of buffers, return #bytes written
991 if (err == status_t(NO_MORE_BUFFERS))
992 break;
993 return ssize_t(err);
994 }
995
996 size_t toWrite;
Eric Laurenta553c252009-07-17 12:17:14 -0700997
Dima Zavin24fc2fb2011-04-19 22:30:36 -0700998 if (mFormat == AUDIO_FORMAT_PCM_8_BIT && !(mFlags & AUDIO_POLICY_OUTPUT_FLAG_DIRECT)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800999 // Divide capacity by 2 to take expansion into account
1000 toWrite = audioBuffer.size>>1;
1001 // 8 to 16 bit conversion
1002 int count = toWrite;
1003 int16_t *dst = (int16_t *)(audioBuffer.i8);
1004 while(count--) {
1005 *dst++ = (int16_t)(*src++^0x80) << 8;
1006 }
Eric Laurent28ad42b2009-08-04 10:42:26 -07001007 } else {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001008 toWrite = audioBuffer.size;
1009 memcpy(audioBuffer.i8, src, toWrite);
1010 src += toWrite;
1011 }
1012 userSize -= toWrite;
1013 written += toWrite;
1014
1015 releaseBuffer(&audioBuffer);
Eric Laurent913af0b2011-03-17 09:36:51 -07001016 } while (userSize >= frameSz);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001017
1018 return written;
1019}
1020
1021// -------------------------------------------------------------------------
1022
1023bool AudioTrack::processAudioBuffer(const sp<AudioTrackThread>& thread)
1024{
1025 Buffer audioBuffer;
1026 uint32_t frames;
1027 size_t writtenSize;
1028
Eric Laurent421ddc02011-03-07 14:52:59 -08001029 mLock.lock();
1030 // acquire a strong reference on the IMemory and IAudioTrack so that they cannot be destroyed
1031 // while we are accessing the cblk
1032 sp <IAudioTrack> audioTrack = mAudioTrack;
1033 sp <IMemory> iMem = mCblkMemory;
1034 audio_track_cblk_t* cblk = mCblk;
Glenn Kastene6810ff2012-01-03 09:42:47 -08001035 bool active = mActive;
Eric Laurent421ddc02011-03-07 14:52:59 -08001036 mLock.unlock();
1037
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001038 // Manage underrun callback
Glenn Kastene6810ff2012-01-03 09:42:47 -08001039 if (active && (cblk->framesAvailable() == cblk->frameCount)) {
Steve Block71f2cf12011-10-20 11:56:00 +01001040 ALOGV("Underrun user: %x, server: %x, flags %04x", cblk->user, cblk->server, cblk->flags);
Eric Laurentae29b762011-03-28 18:37:07 -07001041 if (!(android_atomic_or(CBLK_UNDERRUN_ON, &cblk->flags) & CBLK_UNDERRUN_MSK)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001042 mCbf(EVENT_UNDERRUN, mUserData, 0);
Eric Laurent421ddc02011-03-07 14:52:59 -08001043 if (cblk->server == cblk->frameCount) {
Eric Laurenta553c252009-07-17 12:17:14 -07001044 mCbf(EVENT_BUFFER_END, mUserData, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001045 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001046 if (mSharedBuffer != 0) return false;
1047 }
1048 }
Eric Laurenta553c252009-07-17 12:17:14 -07001049
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001050 // Manage loop end callback
Eric Laurent421ddc02011-03-07 14:52:59 -08001051 while (mLoopCount > cblk->loopCount) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001052 int loopCount = -1;
1053 mLoopCount--;
1054 if (mLoopCount >= 0) loopCount = mLoopCount;
1055
1056 mCbf(EVENT_LOOP_END, mUserData, (void *)&loopCount);
1057 }
1058
1059 // Manage marker callback
Jean-Michel Trivi4a5c1a72009-03-24 18:11:07 -07001060 if (!mMarkerReached && (mMarkerPosition > 0)) {
Eric Laurent421ddc02011-03-07 14:52:59 -08001061 if (cblk->server >= mMarkerPosition) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001062 mCbf(EVENT_MARKER, mUserData, (void *)&mMarkerPosition);
Jean-Michel Trivi4a5c1a72009-03-24 18:11:07 -07001063 mMarkerReached = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001064 }
1065 }
1066
1067 // Manage new position callback
Eric Laurenta553c252009-07-17 12:17:14 -07001068 if (mUpdatePeriod > 0) {
Eric Laurent421ddc02011-03-07 14:52:59 -08001069 while (cblk->server >= mNewPosition) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001070 mCbf(EVENT_NEW_POS, mUserData, (void *)&mNewPosition);
1071 mNewPosition += mUpdatePeriod;
1072 }
1073 }
1074
1075 // If Shared buffer is used, no data is requested from client.
1076 if (mSharedBuffer != 0) {
1077 frames = 0;
1078 } else {
1079 frames = mRemainingFrames;
1080 }
1081
Eric Laurentf1d360a2011-09-07 11:13:23 -07001082 int32_t waitCount = -1;
1083 if (mUpdatePeriod || (!mMarkerReached && mMarkerPosition) || mLoopCount) {
1084 waitCount = 1;
1085 }
1086
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001087 do {
1088
1089 audioBuffer.frameCount = frames;
Eric Laurenta553c252009-07-17 12:17:14 -07001090
1091 // Calling obtainBuffer() with a wait count of 1
1092 // limits wait time to WAIT_PERIOD_MS. This prevents from being
1093 // stuck here not being able to handle timed events (position, markers, loops).
Eric Laurentf1d360a2011-09-07 11:13:23 -07001094 status_t err = obtainBuffer(&audioBuffer, waitCount);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001095 if (err < NO_ERROR) {
1096 if (err != TIMED_OUT) {
Steve Block3762c312012-01-06 19:20:56 +00001097 ALOGE_IF(err != status_t(NO_MORE_BUFFERS), "Error obtaining an audio buffer, giving up.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001098 return false;
1099 }
1100 break;
1101 }
1102 if (err == status_t(STOPPED)) return false;
1103
1104 // Divide buffer size by 2 to take into account the expansion
1105 // due to 8 to 16 bit conversion: the callback must fill only half
1106 // of the destination buffer
Dima Zavin24fc2fb2011-04-19 22:30:36 -07001107 if (mFormat == AUDIO_FORMAT_PCM_8_BIT && !(mFlags & AUDIO_POLICY_OUTPUT_FLAG_DIRECT)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001108 audioBuffer.size >>= 1;
1109 }
1110
1111 size_t reqSize = audioBuffer.size;
1112 mCbf(EVENT_MORE_DATA, mUserData, &audioBuffer);
1113 writtenSize = audioBuffer.size;
1114
1115 // Sanity check on returned size
The Android Open Source Project4df24232009-03-05 14:34:35 -08001116 if (ssize_t(writtenSize) <= 0) {
1117 // The callback is done filling buffers
1118 // Keep this thread going to handle timed events and
1119 // still try to get more data in intervals of WAIT_PERIOD_MS
1120 // but don't just loop and block the CPU, so wait
1121 usleep(WAIT_PERIOD_MS*1000);
1122 break;
1123 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001124 if (writtenSize > reqSize) writtenSize = reqSize;
1125
Dima Zavin24fc2fb2011-04-19 22:30:36 -07001126 if (mFormat == AUDIO_FORMAT_PCM_8_BIT && !(mFlags & AUDIO_POLICY_OUTPUT_FLAG_DIRECT)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001127 // 8 to 16 bit conversion
1128 const int8_t *src = audioBuffer.i8 + writtenSize-1;
1129 int count = writtenSize;
1130 int16_t *dst = audioBuffer.i16 + writtenSize-1;
1131 while(count--) {
1132 *dst-- = (int16_t)(*src--^0x80) << 8;
1133 }
1134 writtenSize <<= 1;
1135 }
1136
1137 audioBuffer.size = writtenSize;
Eric Laurenta553c252009-07-17 12:17:14 -07001138 // NOTE: mCblk->frameSize is not equal to AudioTrack::frameSize() for
Glenn Kastenfaf354d2012-01-11 09:48:27 -08001139 // 8 bit PCM data: in this case, mCblk->frameSize is based on a sample size of
Eric Laurenta553c252009-07-17 12:17:14 -07001140 // 16 bit.
1141 audioBuffer.frameCount = writtenSize/mCblk->frameSize;
1142
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001143 frames -= audioBuffer.frameCount;
1144
1145 releaseBuffer(&audioBuffer);
1146 }
1147 while (frames);
1148
1149 if (frames == 0) {
Eric Laurenteb8f850d2010-05-14 03:26:45 -07001150 mRemainingFrames = mNotificationFramesAct;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001151 } else {
1152 mRemainingFrames = frames;
1153 }
1154 return true;
1155}
1156
Eric Laurent421ddc02011-03-07 14:52:59 -08001157// must be called with mLock and cblk.lock held. Callers must also hold strong references on
1158// the IAudioTrack and IMemory in case they are recreated here.
1159// If the IAudioTrack is successfully restored, the cblk pointer is updated
1160status_t AudioTrack::restoreTrack_l(audio_track_cblk_t*& cblk, bool fromStart)
1161{
1162 status_t result;
1163
Eric Laurentae29b762011-03-28 18:37:07 -07001164 if (!(android_atomic_or(CBLK_RESTORING_ON, &cblk->flags) & CBLK_RESTORING_MSK)) {
Steve Block8564c8d2012-01-05 23:22:43 +00001165 ALOGW("dead IAudioTrack, creating a new one from %s TID %d",
Eric Laurent7e8626f2011-09-13 15:04:17 -07001166 fromStart ? "start()" : "obtainBuffer()", gettid());
Eric Laurent421ddc02011-03-07 14:52:59 -08001167
Eric Laurent421ddc02011-03-07 14:52:59 -08001168 // signal old cblk condition so that other threads waiting for available buffers stop
1169 // waiting now
1170 cblk->cv.broadcast();
1171 cblk->lock.unlock();
1172
Eric Laurent05ce0942011-08-30 10:18:54 -07001173 // refresh the audio configuration cache in this process to make sure we get new
1174 // output parameters in getOutput_l() and createTrack_l()
1175 AudioSystem::clearAudioConfigCache();
1176
Eric Laurent421ddc02011-03-07 14:52:59 -08001177 // if the new IAudioTrack is created, createTrack_l() will modify the
1178 // following member variables: mAudioTrack, mCblkMemory and mCblk.
1179 // It will also delete the strong references on previous IAudioTrack and IMemory
1180 result = createTrack_l(mStreamType,
1181 cblk->sampleRate,
1182 mFormat,
Jean-Michel Trivi54392232011-05-24 15:53:33 -07001183 mChannelMask,
Eric Laurent421ddc02011-03-07 14:52:59 -08001184 mFrameCount,
1185 mFlags,
1186 mSharedBuffer,
1187 getOutput_l(),
1188 false);
1189
1190 if (result == NO_ERROR) {
Eric Laurentb0808f92011-09-06 12:36:15 -07001191 uint32_t user = cblk->user;
1192 uint32_t server = cblk->server;
Eric Laurent6667ac32011-03-21 11:49:00 -07001193 // restore write index and set other indexes to reflect empty buffer status
Eric Laurentb0808f92011-09-06 12:36:15 -07001194 mCblk->user = user;
1195 mCblk->server = user;
1196 mCblk->userBase = user;
1197 mCblk->serverBase = user;
Eric Laurent6667ac32011-03-21 11:49:00 -07001198 // restore loop: this is not guaranteed to succeed if new frame count is not
1199 // compatible with loop length
1200 setLoop_l(cblk->loopStart, cblk->loopEnd, cblk->loopCount);
Eric Laurent421ddc02011-03-07 14:52:59 -08001201 if (!fromStart) {
1202 mCblk->bufferTimeoutMs = MAX_RUN_TIMEOUT_MS;
Eric Laurentb0808f92011-09-06 12:36:15 -07001203 // Make sure that a client relying on callback events indicating underrun or
1204 // the actual amount of audio frames played (e.g SoundPool) receives them.
1205 if (mSharedBuffer == 0) {
1206 uint32_t frames = 0;
1207 if (user > server) {
1208 frames = ((user - server) > mCblk->frameCount) ?
1209 mCblk->frameCount : (user - server);
1210 memset(mCblk->buffers, 0, frames * mCblk->frameSize);
1211 }
1212 // restart playback even if buffer is not completely filled.
1213 android_atomic_or(CBLK_FORCEREADY_ON, &mCblk->flags);
1214 // stepUser() clears CBLK_UNDERRUN_ON flag enabling underrun callbacks to
1215 // the client
1216 mCblk->stepUser(frames);
1217 }
Eric Laurent421ddc02011-03-07 14:52:59 -08001218 }
Eric Laurent6667ac32011-03-21 11:49:00 -07001219 if (mActive) {
1220 result = mAudioTrack->start();
Steve Block8564c8d2012-01-05 23:22:43 +00001221 ALOGW_IF(result != NO_ERROR, "restoreTrack_l() start() failed status %d", result);
Eric Laurent6667ac32011-03-21 11:49:00 -07001222 }
Eric Laurent421ddc02011-03-07 14:52:59 -08001223 if (fromStart && result == NO_ERROR) {
1224 mNewPosition = mCblk->server + mUpdatePeriod;
1225 }
1226 }
1227 if (result != NO_ERROR) {
Eric Laurent7e8626f2011-09-13 15:04:17 -07001228 android_atomic_and(~CBLK_RESTORING_ON, &cblk->flags);
Steve Block8564c8d2012-01-05 23:22:43 +00001229 ALOGW_IF(result != NO_ERROR, "restoreTrack_l() failed status %d", result);
Eric Laurent421ddc02011-03-07 14:52:59 -08001230 }
Eric Laurent7e8626f2011-09-13 15:04:17 -07001231 mRestoreStatus = result;
Eric Laurent421ddc02011-03-07 14:52:59 -08001232 // signal old cblk condition for other threads waiting for restore completion
Eric Laurentae29b762011-03-28 18:37:07 -07001233 android_atomic_or(CBLK_RESTORED_ON, &cblk->flags);
Eric Laurent421ddc02011-03-07 14:52:59 -08001234 cblk->cv.broadcast();
Eric Laurent421ddc02011-03-07 14:52:59 -08001235 } else {
1236 if (!(cblk->flags & CBLK_RESTORED_MSK)) {
Steve Block8564c8d2012-01-05 23:22:43 +00001237 ALOGW("dead IAudioTrack, waiting for a new one TID %d", gettid());
Eric Laurent421ddc02011-03-07 14:52:59 -08001238 mLock.unlock();
1239 result = cblk->cv.waitRelative(cblk->lock, milliseconds(RESTORE_TIMEOUT_MS));
Eric Laurent7e8626f2011-09-13 15:04:17 -07001240 if (result == NO_ERROR) {
1241 result = mRestoreStatus;
1242 }
Eric Laurent421ddc02011-03-07 14:52:59 -08001243 cblk->lock.unlock();
1244 mLock.lock();
1245 } else {
Steve Block8564c8d2012-01-05 23:22:43 +00001246 ALOGW("dead IAudioTrack, already restored TID %d", gettid());
Eric Laurent7e8626f2011-09-13 15:04:17 -07001247 result = mRestoreStatus;
Eric Laurent421ddc02011-03-07 14:52:59 -08001248 cblk->lock.unlock();
1249 }
Eric Laurent421ddc02011-03-07 14:52:59 -08001250 }
Steve Block71f2cf12011-10-20 11:56:00 +01001251 ALOGV("restoreTrack_l() status %d mActive %d cblk %p, old cblk %p flags %08x old flags %08x",
Eric Laurent421ddc02011-03-07 14:52:59 -08001252 result, mActive, mCblk, cblk, mCblk->flags, cblk->flags);
1253
1254 if (result == NO_ERROR) {
1255 // from now on we switch to the newly created cblk
1256 cblk = mCblk;
1257 }
1258 cblk->lock.lock();
1259
Steve Block8564c8d2012-01-05 23:22:43 +00001260 ALOGW_IF(result != NO_ERROR, "restoreTrack_l() error %d TID %d", result, gettid());
Eric Laurent421ddc02011-03-07 14:52:59 -08001261
1262 return result;
1263}
1264
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001265status_t AudioTrack::dump(int fd, const Vector<String16>& args) const
1266{
1267
1268 const size_t SIZE = 256;
1269 char buffer[SIZE];
1270 String8 result;
1271
1272 result.append(" AudioTrack::dump\n");
1273 snprintf(buffer, 255, " stream type(%d), left - right volume(%f, %f)\n", mStreamType, mVolume[0], mVolume[1]);
1274 result.append(buffer);
Eric Laurenteb8f850d2010-05-14 03:26:45 -07001275 snprintf(buffer, 255, " format(%d), channel count(%d), frame count(%d)\n", mFormat, mChannelCount, mCblk->frameCount);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001276 result.append(buffer);
Eric Laurent88e209d2009-07-07 07:10:45 -07001277 snprintf(buffer, 255, " sample rate(%d), status(%d), muted(%d)\n", (mCblk == 0) ? 0 : mCblk->sampleRate, mStatus, mMuted);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001278 result.append(buffer);
1279 snprintf(buffer, 255, " active(%d), latency (%d)\n", mActive, mLatency);
1280 result.append(buffer);
1281 ::write(fd, result.string(), result.size());
1282 return NO_ERROR;
1283}
1284
1285// =========================================================================
1286
1287AudioTrack::AudioTrackThread::AudioTrackThread(AudioTrack& receiver, bool bCanCallJava)
1288 : Thread(bCanCallJava), mReceiver(receiver)
1289{
1290}
1291
1292bool AudioTrack::AudioTrackThread::threadLoop()
1293{
1294 return mReceiver.processAudioBuffer(this);
1295}
1296
1297status_t AudioTrack::AudioTrackThread::readyToRun()
1298{
1299 return NO_ERROR;
1300}
1301
1302void AudioTrack::AudioTrackThread::onFirstRef()
1303{
1304}
1305
1306// =========================================================================
1307
Eric Laurentae29b762011-03-28 18:37:07 -07001308
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001309audio_track_cblk_t::audio_track_cblk_t()
Mathias Agopiana729f972010-03-19 16:14:13 -07001310 : lock(Mutex::SHARED), cv(Condition::SHARED), user(0), server(0),
1311 userBase(0), serverBase(0), buffers(0), frameCount(0),
1312 loopStart(UINT_MAX), loopEnd(UINT_MAX), loopCount(0), volumeLR(0),
Glenn Kasten4790bd82012-01-03 14:22:33 -08001313 mSendLevel(0), flags(0)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001314{
1315}
1316
1317uint32_t audio_track_cblk_t::stepUser(uint32_t frameCount)
1318{
Glenn Kastendb298a42011-12-13 11:45:07 -08001319 uint32_t u = user;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001320
1321 u += frameCount;
1322 // Ensure that user is never ahead of server for AudioRecord
Eric Laurenteb8f850d2010-05-14 03:26:45 -07001323 if (flags & CBLK_DIRECTION_MSK) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001324 // If stepServer() has been called once, switch to normal obtainBuffer() timeout period
1325 if (bufferTimeoutMs == MAX_STARTUP_TIMEOUT_MS-1) {
1326 bufferTimeoutMs = MAX_RUN_TIMEOUT_MS;
1327 }
Glenn Kastendb298a42011-12-13 11:45:07 -08001328 } else if (u > server) {
Steve Block8564c8d2012-01-05 23:22:43 +00001329 ALOGW("stepServer occurred after track reset");
Glenn Kastendb298a42011-12-13 11:45:07 -08001330 u = server;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001331 }
1332
1333 if (u >= userBase + this->frameCount) {
1334 userBase += this->frameCount;
1335 }
1336
Glenn Kastendb298a42011-12-13 11:45:07 -08001337 user = u;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001338
1339 // Clear flow control error condition as new data has been written/read to/from buffer.
Eric Laurent913af0b2011-03-17 09:36:51 -07001340 if (flags & CBLK_UNDERRUN_MSK) {
Eric Laurentae29b762011-03-28 18:37:07 -07001341 android_atomic_and(~CBLK_UNDERRUN_MSK, &flags);
Eric Laurent913af0b2011-03-17 09:36:51 -07001342 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001343
1344 return u;
1345}
1346
1347bool audio_track_cblk_t::stepServer(uint32_t frameCount)
1348{
Eric Laurentae29b762011-03-28 18:37:07 -07001349 if (!tryLock()) {
Steve Block8564c8d2012-01-05 23:22:43 +00001350 ALOGW("stepServer() could not lock cblk");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001351 return false;
1352 }
1353
Glenn Kastendb298a42011-12-13 11:45:07 -08001354 uint32_t s = server;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001355
1356 s += frameCount;
Eric Laurenteb8f850d2010-05-14 03:26:45 -07001357 if (flags & CBLK_DIRECTION_MSK) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001358 // Mark that we have read the first buffer so that next time stepUser() is called
1359 // we switch to normal obtainBuffer() timeout period
1360 if (bufferTimeoutMs == MAX_STARTUP_TIMEOUT_MS) {
Eric Laurentbda74692009-11-04 08:27:26 -08001361 bufferTimeoutMs = MAX_STARTUP_TIMEOUT_MS - 1;
Eric Laurenta553c252009-07-17 12:17:14 -07001362 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001363 // It is possible that we receive a flush()
1364 // while the mixer is processing a block: in this case,
1365 // stepServer() is called After the flush() has reset u & s and
1366 // we have s > u
Glenn Kastendb298a42011-12-13 11:45:07 -08001367 if (s > user) {
Steve Block8564c8d2012-01-05 23:22:43 +00001368 ALOGW("stepServer occurred after track reset");
Glenn Kastendb298a42011-12-13 11:45:07 -08001369 s = user;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001370 }
1371 }
1372
1373 if (s >= loopEnd) {
Steve Block8564c8d2012-01-05 23:22:43 +00001374 ALOGW_IF(s > loopEnd, "stepServer: s %u > loopEnd %u", s, loopEnd);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001375 s = loopStart;
1376 if (--loopCount == 0) {
1377 loopEnd = UINT_MAX;
1378 loopStart = UINT_MAX;
1379 }
1380 }
1381 if (s >= serverBase + this->frameCount) {
1382 serverBase += this->frameCount;
1383 }
1384
Glenn Kastendb298a42011-12-13 11:45:07 -08001385 server = s;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001386
Eric Laurent421ddc02011-03-07 14:52:59 -08001387 if (!(flags & CBLK_INVALID_MSK)) {
1388 cv.signal();
1389 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001390 lock.unlock();
1391 return true;
1392}
1393
1394void* audio_track_cblk_t::buffer(uint32_t offset) const
1395{
Glenn Kastendb298a42011-12-13 11:45:07 -08001396 return (int8_t *)buffers + (offset - userBase) * frameSize;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001397}
1398
1399uint32_t audio_track_cblk_t::framesAvailable()
1400{
1401 Mutex::Autolock _l(lock);
1402 return framesAvailable_l();
1403}
1404
1405uint32_t audio_track_cblk_t::framesAvailable_l()
1406{
Glenn Kastendb298a42011-12-13 11:45:07 -08001407 uint32_t u = user;
1408 uint32_t s = server;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001409
Eric Laurenteb8f850d2010-05-14 03:26:45 -07001410 if (flags & CBLK_DIRECTION_MSK) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001411 uint32_t limit = (s < loopStart) ? s : loopStart;
1412 return limit + frameCount - u;
1413 } else {
1414 return frameCount + u - s;
1415 }
1416}
1417
1418uint32_t audio_track_cblk_t::framesReady()
1419{
Glenn Kastendb298a42011-12-13 11:45:07 -08001420 uint32_t u = user;
1421 uint32_t s = server;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001422
Eric Laurenteb8f850d2010-05-14 03:26:45 -07001423 if (flags & CBLK_DIRECTION_MSK) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001424 if (u < loopEnd) {
1425 return u - s;
1426 } else {
Eric Laurentae29b762011-03-28 18:37:07 -07001427 // do not block on mutex shared with client on AudioFlinger side
1428 if (!tryLock()) {
Steve Block8564c8d2012-01-05 23:22:43 +00001429 ALOGW("framesReady() could not lock cblk");
Eric Laurentae29b762011-03-28 18:37:07 -07001430 return 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001431 }
Eric Laurentae29b762011-03-28 18:37:07 -07001432 uint32_t frames = UINT_MAX;
1433 if (loopCount >= 0) {
1434 frames = (loopEnd - loopStart)*loopCount + u - s;
1435 }
1436 lock.unlock();
1437 return frames;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001438 }
1439 } else {
1440 return s - u;
1441 }
1442}
1443
Eric Laurentae29b762011-03-28 18:37:07 -07001444bool audio_track_cblk_t::tryLock()
1445{
1446 // the code below simulates lock-with-timeout
1447 // we MUST do this to protect the AudioFlinger server
1448 // as this lock is shared with the client.
1449 status_t err;
1450
1451 err = lock.tryLock();
1452 if (err == -EBUSY) { // just wait a bit
1453 usleep(1000);
1454 err = lock.tryLock();
1455 }
1456 if (err != NO_ERROR) {
1457 // probably, the client just died.
1458 return false;
1459 }
1460 return true;
1461}
1462
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001463// -------------------------------------------------------------------------
1464
1465}; // namespace android