blob: c2c67158299ffd568b9277e00b80f5f99e93bb89 [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/* //device/extlibs/pv/android/AudioTrack.cpp
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 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>
41
Dima Zavin34bb4192011-05-11 14:15:23 -070042#include <system/audio.h>
Dima Zavin290029d2011-06-13 18:16:26 -070043#include <system/audio_policy.h>
Dima Zavin24fc2fb2011-04-19 22:30:36 -070044
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080045#define LIKELY( exp ) (__builtin_expect( (exp) != 0, true ))
46#define UNLIKELY( exp ) (__builtin_expect( (exp) != 0, false ))
47
48namespace android {
Chia-chi Yehbd240c22010-06-16 06:33:13 +080049// ---------------------------------------------------------------------------
50
51// static
52status_t AudioTrack::getMinFrameCount(
53 int* frameCount,
54 int streamType,
55 uint32_t sampleRate)
56{
57 int afSampleRate;
58 if (AudioSystem::getOutputSamplingRate(&afSampleRate, streamType) != NO_ERROR) {
59 return NO_INIT;
60 }
61 int afFrameCount;
62 if (AudioSystem::getOutputFrameCount(&afFrameCount, streamType) != NO_ERROR) {
63 return NO_INIT;
64 }
65 uint32_t afLatency;
66 if (AudioSystem::getOutputLatency(&afLatency, streamType) != NO_ERROR) {
67 return NO_INIT;
68 }
69
70 // Ensure that buffer depth covers at least audio hardware latency
71 uint32_t minBufCount = afLatency / ((1000 * afFrameCount) / afSampleRate);
72 if (minBufCount < 2) minBufCount = 2;
73
74 *frameCount = (sampleRate == 0) ? afFrameCount * minBufCount :
75 afFrameCount * minBufCount * sampleRate / afSampleRate;
76 return NO_ERROR;
77}
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080078
79// ---------------------------------------------------------------------------
80
81AudioTrack::AudioTrack()
82 : mStatus(NO_INIT)
83{
84}
85
86AudioTrack::AudioTrack(
87 int streamType,
88 uint32_t sampleRate,
89 int format,
Jean-Michel Trivi54392232011-05-24 15:53:33 -070090 int channelMask,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080091 int frameCount,
92 uint32_t flags,
93 callback_t cbf,
94 void* user,
Eric Laurent65b65452010-06-01 23:49:17 -070095 int notificationFrames,
96 int sessionId)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080097 : mStatus(NO_INIT)
98{
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(
105 int streamType,
106 uint32_t sampleRate,
107 int 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)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800115 : mStatus(NO_INIT)
116{
Jean-Michel Trivi54392232011-05-24 15:53:33 -0700117 mStatus = set(streamType, sampleRate, format, channelMask,
Eric Laurent619346f2010-06-21 09:27:30 -0700118 0, flags, cbf, user, notificationFrames,
119 sharedBuffer, false, sessionId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800120}
121
122AudioTrack::~AudioTrack()
123{
124 LOGV_IF(mSharedBuffer != 0, "Destructor sharedBuffer: %p", mSharedBuffer->pointer());
125
126 if (mStatus == NO_ERROR) {
127 // Make sure that callback function exits in the case where
128 // it is looping on buffer full condition in obtainBuffer().
129 // Otherwise the callback thread will never exit.
130 stop();
131 if (mAudioTrackThread != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800132 mAudioTrackThread->requestExitAndWait();
133 mAudioTrackThread.clear();
134 }
135 mAudioTrack.clear();
136 IPCThreadState::self()->flushCommands();
Marco Nelissenc74b93f2011-08-02 13:33:41 -0700137 AudioSystem::releaseAudioSessionId(mSessionId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800138 }
139}
140
141status_t AudioTrack::set(
142 int streamType,
143 uint32_t sampleRate,
144 int format,
Jean-Michel Trivi54392232011-05-24 15:53:33 -0700145 int channelMask,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800146 int frameCount,
147 uint32_t flags,
148 callback_t cbf,
149 void* user,
150 int notificationFrames,
151 const sp<IMemory>& sharedBuffer,
Eric Laurent65b65452010-06-01 23:49:17 -0700152 bool threadCanCallJava,
153 int sessionId)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800154{
155
156 LOGV_IF(sharedBuffer != 0, "sharedBuffer: %p, size: %d", sharedBuffer->pointer(), sharedBuffer->size());
157
Eric Laurent421ddc02011-03-07 14:52:59 -0800158 AutoMutex lock(mLock);
Eric Laurentef028272009-04-21 07:56:33 -0700159 if (mAudioTrack != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800160 LOGE("Track already in use");
161 return INVALID_OPERATION;
162 }
163
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800164 int afSampleRate;
165 if (AudioSystem::getOutputSamplingRate(&afSampleRate, streamType) != NO_ERROR) {
166 return NO_INIT;
167 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800168 uint32_t afLatency;
169 if (AudioSystem::getOutputLatency(&afLatency, streamType) != NO_ERROR) {
170 return NO_INIT;
171 }
172
173 // handle default values first.
Dima Zavin24fc2fb2011-04-19 22:30:36 -0700174 if (streamType == AUDIO_STREAM_DEFAULT) {
175 streamType = AUDIO_STREAM_MUSIC;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800176 }
177 if (sampleRate == 0) {
178 sampleRate = afSampleRate;
179 }
180 // these below should probably come from the audioFlinger too...
181 if (format == 0) {
Dima Zavin24fc2fb2011-04-19 22:30:36 -0700182 format = AUDIO_FORMAT_PCM_16_BIT;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800183 }
Jean-Michel Trivi54392232011-05-24 15:53:33 -0700184 if (channelMask == 0) {
185 channelMask = AUDIO_CHANNEL_OUT_STEREO;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800186 }
187
188 // validate parameters
Dima Zavin24fc2fb2011-04-19 22:30:36 -0700189 if (!audio_is_valid_format(format)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800190 LOGE("Invalid format");
191 return BAD_VALUE;
192 }
Eric Laurenta553c252009-07-17 12:17:14 -0700193
194 // force direct flag if format is not linear PCM
Dima Zavin24fc2fb2011-04-19 22:30:36 -0700195 if (!audio_is_linear_pcm(format)) {
196 flags |= AUDIO_POLICY_OUTPUT_FLAG_DIRECT;
Eric Laurenta553c252009-07-17 12:17:14 -0700197 }
198
Jean-Michel Trivi54392232011-05-24 15:53:33 -0700199 if (!audio_is_output_channel(channelMask)) {
Eric Laurenta553c252009-07-17 12:17:14 -0700200 LOGE("Invalid channel mask");
201 return BAD_VALUE;
202 }
Jean-Michel Trivi54392232011-05-24 15:53:33 -0700203 uint32_t channelCount = popcount(channelMask);
Eric Laurenta553c252009-07-17 12:17:14 -0700204
Dima Zavin24fc2fb2011-04-19 22:30:36 -0700205 audio_io_handle_t output = AudioSystem::getOutput(
206 (audio_stream_type_t)streamType,
Jean-Michel Trivi54392232011-05-24 15:53:33 -0700207 sampleRate,format, channelMask,
Dima Zavin24fc2fb2011-04-19 22:30:36 -0700208 (audio_policy_output_flags_t)flags);
Eric Laurenta553c252009-07-17 12:17:14 -0700209
210 if (output == 0) {
211 LOGE("Could not get audio output for stream type %d", streamType);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800212 return BAD_VALUE;
213 }
214
Eric Laurentbda74692009-11-04 08:27:26 -0800215 mVolume[LEFT] = 1.0f;
216 mVolume[RIGHT] = 1.0f;
Eric Laurent65b65452010-06-01 23:49:17 -0700217 mSendLevel = 0;
Eric Laurenteb8f850d2010-05-14 03:26:45 -0700218 mFrameCount = frameCount;
219 mNotificationFramesReq = notificationFrames;
Eric Laurent65b65452010-06-01 23:49:17 -0700220 mSessionId = sessionId;
Eric Laurent7070b362010-07-16 07:43:46 -0700221 mAuxEffectId = 0;
Eric Laurent65b65452010-06-01 23:49:17 -0700222
Eric Laurentbda74692009-11-04 08:27:26 -0800223 // create the IAudioTrack
Eric Laurent421ddc02011-03-07 14:52:59 -0800224 status_t status = createTrack_l(streamType,
225 sampleRate,
Jean-Michel Trivi54392232011-05-24 15:53:33 -0700226 (uint32_t)format,
227 (uint32_t)channelMask,
Eric Laurent421ddc02011-03-07 14:52:59 -0800228 frameCount,
229 flags,
230 sharedBuffer,
231 output,
232 true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800233
Eric Laurentbda74692009-11-04 08:27:26 -0800234 if (status != NO_ERROR) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800235 return status;
236 }
Eric Laurentbda74692009-11-04 08:27:26 -0800237
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800238 if (cbf != 0) {
239 mAudioTrackThread = new AudioTrackThread(*this, threadCanCallJava);
240 if (mAudioTrackThread == 0) {
241 LOGE("Could not create callback thread");
242 return NO_INIT;
243 }
244 }
245
246 mStatus = NO_ERROR;
247
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800248 mStreamType = streamType;
Jean-Michel Trivi54392232011-05-24 15:53:33 -0700249 mFormat = (uint32_t)format;
250 mChannelMask = (uint32_t)channelMask;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800251 mChannelCount = channelCount;
252 mSharedBuffer = sharedBuffer;
253 mMuted = false;
254 mActive = 0;
255 mCbf = cbf;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800256 mUserData = user;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800257 mLoopCount = 0;
258 mMarkerPosition = 0;
Jean-Michel Trivi4a5c1a72009-03-24 18:11:07 -0700259 mMarkerReached = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800260 mNewPosition = 0;
261 mUpdatePeriod = 0;
Jean-Michel Trivi22cb2042011-08-25 16:47:23 -0700262 mFlushed = false;
Eric Laurenta553c252009-07-17 12:17:14 -0700263 mFlags = flags;
Marco Nelissenc74b93f2011-08-02 13:33:41 -0700264 AudioSystem::acquireAudioSessionId(mSessionId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800265
266 return NO_ERROR;
267}
268
269status_t AudioTrack::initCheck() const
270{
271 return mStatus;
272}
273
274// -------------------------------------------------------------------------
275
276uint32_t AudioTrack::latency() const
277{
278 return mLatency;
279}
280
281int AudioTrack::streamType() const
282{
283 return mStreamType;
284}
285
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800286int AudioTrack::format() const
287{
288 return mFormat;
289}
290
291int AudioTrack::channelCount() const
292{
293 return mChannelCount;
294}
295
296uint32_t AudioTrack::frameCount() const
297{
Eric Laurenteb8f850d2010-05-14 03:26:45 -0700298 return mCblk->frameCount;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800299}
300
301int AudioTrack::frameSize() const
302{
Dima Zavin24fc2fb2011-04-19 22:30:36 -0700303 if (audio_is_linear_pcm(mFormat)) {
Eric Laurentc310dcb2011-06-16 21:30:45 -0700304 return channelCount()*audio_bytes_per_sample(mFormat);
Eric Laurenta553c252009-07-17 12:17:14 -0700305 } else {
306 return sizeof(uint8_t);
307 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800308}
309
310sp<IMemory>& AudioTrack::sharedBuffer()
311{
312 return mSharedBuffer;
313}
314
315// -------------------------------------------------------------------------
316
317void AudioTrack::start()
318{
319 sp<AudioTrackThread> t = mAudioTrackThread;
Glenn Kasten028ab992011-06-22 16:18:04 -0700320 status_t status = NO_ERROR;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800321
322 LOGV("start %p", this);
323 if (t != 0) {
324 if (t->exitPending()) {
325 if (t->requestExitAndWait() == WOULD_BLOCK) {
326 LOGE("AudioTrack::start called from thread");
327 return;
328 }
329 }
330 t->mLock.lock();
331 }
332
Eric Laurentf3d6dd02010-11-18 08:40:16 -0800333 AutoMutex lock(mLock);
Eric Laurent421ddc02011-03-07 14:52:59 -0800334 // acquire a strong reference on the IMemory and IAudioTrack so that they cannot be destroyed
335 // while we are accessing the cblk
336 sp <IAudioTrack> audioTrack = mAudioTrack;
337 sp <IMemory> iMem = mCblkMemory;
338 audio_track_cblk_t* cblk = mCblk;
339
Eric Laurentf3d6dd02010-11-18 08:40:16 -0800340 if (mActive == 0) {
Jean-Michel Trivi22cb2042011-08-25 16:47:23 -0700341 mFlushed = false;
Eric Laurentf3d6dd02010-11-18 08:40:16 -0800342 mActive = 1;
Eric Laurent421ddc02011-03-07 14:52:59 -0800343 mNewPosition = cblk->server + mUpdatePeriod;
Eric Laurent913af0b2011-03-17 09:36:51 -0700344 cblk->lock.lock();
Eric Laurent421ddc02011-03-07 14:52:59 -0800345 cblk->bufferTimeoutMs = MAX_STARTUP_TIMEOUT_MS;
346 cblk->waitTimeMs = 0;
Eric Laurentae29b762011-03-28 18:37:07 -0700347 android_atomic_and(~CBLK_DISABLED_ON, &cblk->flags);
Eric Laurent059b4be2009-11-09 23:32:22 -0800348 if (t != 0) {
Glenn Kasten993fcce2011-06-01 16:46:29 -0700349 t->run("AudioTrackThread", ANDROID_PRIORITY_AUDIO);
Eric Laurent059b4be2009-11-09 23:32:22 -0800350 } else {
Glenn Kasten993fcce2011-06-01 16:46:29 -0700351 setpriority(PRIO_PROCESS, 0, ANDROID_PRIORITY_AUDIO);
Eric Laurent059b4be2009-11-09 23:32:22 -0800352 }
353
Eric Laurent421ddc02011-03-07 14:52:59 -0800354 LOGV("start %p before lock cblk %p", this, mCblk);
Eric Laurent421ddc02011-03-07 14:52:59 -0800355 if (!(cblk->flags & CBLK_INVALID_MSK)) {
356 cblk->lock.unlock();
Eric Laurenteb8f850d2010-05-14 03:26:45 -0700357 status = mAudioTrack->start();
Eric Laurent421ddc02011-03-07 14:52:59 -0800358 cblk->lock.lock();
359 if (status == DEAD_OBJECT) {
Eric Laurentae29b762011-03-28 18:37:07 -0700360 android_atomic_or(CBLK_INVALID_ON, &cblk->flags);
Eric Laurent49f02be2009-11-19 09:00:56 -0800361 }
Eric Laurent059b4be2009-11-09 23:32:22 -0800362 }
Eric Laurent421ddc02011-03-07 14:52:59 -0800363 if (cblk->flags & CBLK_INVALID_MSK) {
364 status = restoreTrack_l(cblk, true);
365 }
366 cblk->lock.unlock();
Eric Laurent059b4be2009-11-09 23:32:22 -0800367 if (status != NO_ERROR) {
Eric Laurentbda74692009-11-04 08:27:26 -0800368 LOGV("start() failed");
Eric Laurentf3d6dd02010-11-18 08:40:16 -0800369 mActive = 0;
Eric Laurent059b4be2009-11-09 23:32:22 -0800370 if (t != 0) {
371 t->requestExit();
372 } else {
373 setpriority(PRIO_PROCESS, 0, ANDROID_PRIORITY_NORMAL);
374 }
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
387 LOGV("stop %p", this);
388 if (t != 0) {
389 t->mLock.lock();
390 }
391
Eric Laurentf3d6dd02010-11-18 08:40:16 -0800392 AutoMutex lock(mLock);
393 if (mActive == 1) {
394 mActive = 0;
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 {
411 setpriority(PRIO_PROCESS, 0, ANDROID_PRIORITY_NORMAL);
412 }
413 }
414
415 if (t != 0) {
416 t->mLock.unlock();
417 }
418}
419
420bool AudioTrack::stopped() const
421{
422 return !mActive;
423}
424
425void AudioTrack::flush()
426{
Eric Laurent421ddc02011-03-07 14:52:59 -0800427 AutoMutex lock(mLock);
428 flush_l();
429}
430
431// must be called with mLock held
432void AudioTrack::flush_l()
433{
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800434 LOGV("flush");
Eric Laurenta553c252009-07-17 12:17:14 -0700435
Jean-Michel Trivi4a5c1a72009-03-24 18:11:07 -0700436 // clear playback marker and periodic update counter
437 mMarkerPosition = 0;
438 mMarkerReached = false;
439 mUpdatePeriod = 0;
Eric Laurenta553c252009-07-17 12:17:14 -0700440
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800441 if (!mActive) {
Jean-Michel Trivi22cb2042011-08-25 16:47:23 -0700442 mFlushed = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800443 mAudioTrack->flush();
444 // Release AudioTrack callback thread in case it was waiting for new buffers
445 // in AudioTrack::obtainBuffer()
446 mCblk->cv.signal();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800447 }
448}
449
450void AudioTrack::pause()
451{
452 LOGV("pause");
Eric Laurentf3d6dd02010-11-18 08:40:16 -0800453 AutoMutex lock(mLock);
454 if (mActive == 1) {
455 mActive = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800456 mAudioTrack->pause();
457 }
458}
459
460void AudioTrack::mute(bool e)
461{
462 mAudioTrack->mute(e);
463 mMuted = e;
464}
465
466bool AudioTrack::muted() const
467{
468 return mMuted;
469}
470
Eric Laurent65b65452010-06-01 23:49:17 -0700471status_t AudioTrack::setVolume(float left, float right)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800472{
Eric Laurent65b65452010-06-01 23:49:17 -0700473 if (left > 1.0f || right > 1.0f) {
474 return BAD_VALUE;
475 }
476
Eric Laurent421ddc02011-03-07 14:52:59 -0800477 AutoMutex lock(mLock);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800478 mVolume[LEFT] = left;
479 mVolume[RIGHT] = right;
480
481 // write must be atomic
Eric Laurent65b65452010-06-01 23:49:17 -0700482 mCblk->volumeLR = (uint32_t(uint16_t(right * 0x1000)) << 16) | uint16_t(left * 0x1000);
483
484 return NO_ERROR;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800485}
486
487void AudioTrack::getVolume(float* left, float* right)
488{
Eric Laurent65b65452010-06-01 23:49:17 -0700489 if (left != NULL) {
490 *left = mVolume[LEFT];
491 }
492 if (right != NULL) {
493 *right = mVolume[RIGHT];
494 }
495}
496
Eric Laurent7070b362010-07-16 07:43:46 -0700497status_t AudioTrack::setAuxEffectSendLevel(float level)
Eric Laurent65b65452010-06-01 23:49:17 -0700498{
Eric Laurent7070b362010-07-16 07:43:46 -0700499 LOGV("setAuxEffectSendLevel(%f)", level);
Eric Laurent65b65452010-06-01 23:49:17 -0700500 if (level > 1.0f) {
501 return BAD_VALUE;
502 }
Eric Laurent421ddc02011-03-07 14:52:59 -0800503 AutoMutex lock(mLock);
Eric Laurent65b65452010-06-01 23:49:17 -0700504
505 mSendLevel = level;
506
507 mCblk->sendLevel = uint16_t(level * 0x1000);
508
509 return NO_ERROR;
510}
511
Eric Laurent7070b362010-07-16 07:43:46 -0700512void AudioTrack::getAuxEffectSendLevel(float* level)
Eric Laurent65b65452010-06-01 23:49:17 -0700513{
514 if (level != NULL) {
515 *level = mSendLevel;
516 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800517}
518
Eric Laurent88e209d2009-07-07 07:10:45 -0700519status_t AudioTrack::setSampleRate(int rate)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800520{
521 int afSamplingRate;
522
523 if (AudioSystem::getOutputSamplingRate(&afSamplingRate, mStreamType) != NO_ERROR) {
Eric Laurent88e209d2009-07-07 07:10:45 -0700524 return NO_INIT;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800525 }
526 // Resampler implementation limits input sampling rate to 2 x output sampling rate.
Eric Laurent88e209d2009-07-07 07:10:45 -0700527 if (rate <= 0 || rate > afSamplingRate*2 ) return BAD_VALUE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800528
Eric Laurent421ddc02011-03-07 14:52:59 -0800529 AutoMutex lock(mLock);
Eric Laurent88e209d2009-07-07 07:10:45 -0700530 mCblk->sampleRate = rate;
531 return NO_ERROR;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800532}
533
534uint32_t AudioTrack::getSampleRate()
535{
Eric Laurent421ddc02011-03-07 14:52:59 -0800536 AutoMutex lock(mLock);
Eric Laurent88e209d2009-07-07 07:10:45 -0700537 return mCblk->sampleRate;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800538}
539
540status_t AudioTrack::setLoop(uint32_t loopStart, uint32_t loopEnd, int loopCount)
541{
Eric Laurent421ddc02011-03-07 14:52:59 -0800542 AutoMutex lock(mLock);
543 return setLoop_l(loopStart, loopEnd, loopCount);
544}
545
546// must be called with mLock held
547status_t AudioTrack::setLoop_l(uint32_t loopStart, uint32_t loopEnd, int loopCount)
548{
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800549 audio_track_cblk_t* cblk = mCblk;
550
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800551 Mutex::Autolock _l(cblk->lock);
552
553 if (loopCount == 0) {
554 cblk->loopStart = UINT_MAX;
555 cblk->loopEnd = UINT_MAX;
556 cblk->loopCount = 0;
557 mLoopCount = 0;
558 return NO_ERROR;
559 }
560
561 if (loopStart >= loopEnd ||
Eric Laurent6667ac32011-03-21 11:49:00 -0700562 loopEnd - loopStart > cblk->frameCount ||
563 cblk->server > loopStart) {
Eric Laurenteb8f850d2010-05-14 03:26:45 -0700564 LOGE("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 -0800565 return BAD_VALUE;
566 }
567
Eric Laurent6667ac32011-03-21 11:49:00 -0700568 if ((mSharedBuffer != 0) && (loopEnd > cblk->frameCount)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800569 LOGE("setLoop invalid value: loop markers beyond data: loopStart %d, loopEnd %d, framecount %d",
Eric Laurenteb8f850d2010-05-14 03:26:45 -0700570 loopStart, loopEnd, cblk->frameCount);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800571 return BAD_VALUE;
Eric Laurenta553c252009-07-17 12:17:14 -0700572 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800573
574 cblk->loopStart = loopStart;
575 cblk->loopEnd = loopEnd;
576 cblk->loopCount = loopCount;
577 mLoopCount = loopCount;
578
579 return NO_ERROR;
580}
581
582status_t AudioTrack::getLoop(uint32_t *loopStart, uint32_t *loopEnd, int *loopCount)
583{
Eric Laurent421ddc02011-03-07 14:52:59 -0800584 AutoMutex lock(mLock);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800585 if (loopStart != 0) {
586 *loopStart = mCblk->loopStart;
587 }
588 if (loopEnd != 0) {
589 *loopEnd = mCblk->loopEnd;
590 }
591 if (loopCount != 0) {
592 if (mCblk->loopCount < 0) {
593 *loopCount = -1;
594 } else {
595 *loopCount = mCblk->loopCount;
596 }
597 }
598
599 return NO_ERROR;
600}
601
602status_t AudioTrack::setMarkerPosition(uint32_t marker)
603{
604 if (mCbf == 0) return INVALID_OPERATION;
605
606 mMarkerPosition = marker;
Jean-Michel Trivi4a5c1a72009-03-24 18:11:07 -0700607 mMarkerReached = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800608
609 return NO_ERROR;
610}
611
612status_t AudioTrack::getMarkerPosition(uint32_t *marker)
613{
614 if (marker == 0) return BAD_VALUE;
615
616 *marker = mMarkerPosition;
617
618 return NO_ERROR;
619}
620
621status_t AudioTrack::setPositionUpdatePeriod(uint32_t updatePeriod)
622{
623 if (mCbf == 0) return INVALID_OPERATION;
624
625 uint32_t curPosition;
626 getPosition(&curPosition);
627 mNewPosition = curPosition + updatePeriod;
628 mUpdatePeriod = updatePeriod;
629
630 return NO_ERROR;
631}
632
633status_t AudioTrack::getPositionUpdatePeriod(uint32_t *updatePeriod)
634{
635 if (updatePeriod == 0) return BAD_VALUE;
636
637 *updatePeriod = mUpdatePeriod;
638
639 return NO_ERROR;
640}
641
642status_t AudioTrack::setPosition(uint32_t position)
643{
Eric Laurent421ddc02011-03-07 14:52:59 -0800644 AutoMutex lock(mLock);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800645 Mutex::Autolock _l(mCblk->lock);
646
647 if (!stopped()) return INVALID_OPERATION;
648
649 if (position > mCblk->user) return BAD_VALUE;
650
651 mCblk->server = position;
Eric Laurentae29b762011-03-28 18:37:07 -0700652 android_atomic_or(CBLK_FORCEREADY_ON, &mCblk->flags);
Eric Laurenta553c252009-07-17 12:17:14 -0700653
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800654 return NO_ERROR;
655}
656
657status_t AudioTrack::getPosition(uint32_t *position)
658{
659 if (position == 0) return BAD_VALUE;
Eric Laurent421ddc02011-03-07 14:52:59 -0800660 AutoMutex lock(mLock);
Jean-Michel Trivi22cb2042011-08-25 16:47:23 -0700661 *position = mFlushed ? 0 : mCblk->server;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800662
663 return NO_ERROR;
664}
665
666status_t AudioTrack::reload()
667{
Eric Laurent421ddc02011-03-07 14:52:59 -0800668 AutoMutex lock(mLock);
669
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800670 if (!stopped()) return INVALID_OPERATION;
Eric Laurenta553c252009-07-17 12:17:14 -0700671
Eric Laurent421ddc02011-03-07 14:52:59 -0800672 flush_l();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800673
Eric Laurenteb8f850d2010-05-14 03:26:45 -0700674 mCblk->stepUser(mCblk->frameCount);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800675
676 return NO_ERROR;
677}
678
Eric Laurenta553c252009-07-17 12:17:14 -0700679audio_io_handle_t AudioTrack::getOutput()
680{
Eric Laurent421ddc02011-03-07 14:52:59 -0800681 AutoMutex lock(mLock);
682 return getOutput_l();
683}
684
685// must be called with mLock held
686audio_io_handle_t AudioTrack::getOutput_l()
687{
Dima Zavin24fc2fb2011-04-19 22:30:36 -0700688 return AudioSystem::getOutput((audio_stream_type_t)mStreamType,
Jean-Michel Trivi54392232011-05-24 15:53:33 -0700689 mCblk->sampleRate, mFormat, mChannelMask, (audio_policy_output_flags_t)mFlags);
Eric Laurenta553c252009-07-17 12:17:14 -0700690}
691
Eric Laurent65b65452010-06-01 23:49:17 -0700692int AudioTrack::getSessionId()
693{
694 return mSessionId;
695}
696
697status_t AudioTrack::attachAuxEffect(int effectId)
698{
Eric Laurent7070b362010-07-16 07:43:46 -0700699 LOGV("attachAuxEffect(%d)", effectId);
700 status_t status = mAudioTrack->attachAuxEffect(effectId);
701 if (status == NO_ERROR) {
702 mAuxEffectId = effectId;
703 }
704 return status;
Eric Laurent65b65452010-06-01 23:49:17 -0700705}
706
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800707// -------------------------------------------------------------------------
708
Eric Laurent421ddc02011-03-07 14:52:59 -0800709// must be called with mLock held
710status_t AudioTrack::createTrack_l(
Eric Laurentbda74692009-11-04 08:27:26 -0800711 int streamType,
712 uint32_t sampleRate,
Jean-Michel Trivi54392232011-05-24 15:53:33 -0700713 uint32_t format,
714 uint32_t channelMask,
Eric Laurentbda74692009-11-04 08:27:26 -0800715 int frameCount,
716 uint32_t flags,
717 const sp<IMemory>& sharedBuffer,
Eric Laurenteb8f850d2010-05-14 03:26:45 -0700718 audio_io_handle_t output,
719 bool enforceFrameCount)
Eric Laurentbda74692009-11-04 08:27:26 -0800720{
721 status_t status;
722 const sp<IAudioFlinger>& audioFlinger = AudioSystem::get_audio_flinger();
723 if (audioFlinger == 0) {
724 LOGE("Could not get audioflinger");
725 return NO_INIT;
726 }
727
Eric Laurenteb8f850d2010-05-14 03:26:45 -0700728 int afSampleRate;
729 if (AudioSystem::getOutputSamplingRate(&afSampleRate, streamType) != NO_ERROR) {
730 return NO_INIT;
731 }
732 int afFrameCount;
733 if (AudioSystem::getOutputFrameCount(&afFrameCount, streamType) != NO_ERROR) {
734 return NO_INIT;
735 }
736 uint32_t afLatency;
737 if (AudioSystem::getOutputLatency(&afLatency, streamType) != NO_ERROR) {
738 return NO_INIT;
739 }
740
741 mNotificationFramesAct = mNotificationFramesReq;
Dima Zavin24fc2fb2011-04-19 22:30:36 -0700742 if (!audio_is_linear_pcm(format)) {
Eric Laurenteb8f850d2010-05-14 03:26:45 -0700743 if (sharedBuffer != 0) {
744 frameCount = sharedBuffer->size();
745 }
746 } else {
747 // Ensure that buffer depth covers at least audio hardware latency
748 uint32_t minBufCount = afLatency / ((1000 * afFrameCount)/afSampleRate);
749 if (minBufCount < 2) minBufCount = 2;
750
751 int minFrameCount = (afFrameCount*sampleRate*minBufCount)/afSampleRate;
752
753 if (sharedBuffer == 0) {
754 if (frameCount == 0) {
755 frameCount = minFrameCount;
756 }
757 if (mNotificationFramesAct == 0) {
758 mNotificationFramesAct = frameCount/2;
759 }
760 // Make sure that application is notified with sufficient margin
761 // before underrun
762 if (mNotificationFramesAct > (uint32_t)frameCount/2) {
763 mNotificationFramesAct = frameCount/2;
764 }
765 if (frameCount < minFrameCount) {
766 if (enforceFrameCount) {
767 LOGE("Invalid buffer size: minFrameCount %d, frameCount %d", minFrameCount, frameCount);
768 return BAD_VALUE;
769 } else {
770 frameCount = minFrameCount;
771 }
772 }
773 } else {
774 // Ensure that buffer alignment matches channelcount
Jean-Michel Trivi54392232011-05-24 15:53:33 -0700775 int channelCount = popcount(channelMask);
Eric Laurenteb8f850d2010-05-14 03:26:45 -0700776 if (((uint32_t)sharedBuffer->pointer() & (channelCount | 1)) != 0) {
777 LOGE("Invalid buffer alignement: address %p, channelCount %d", sharedBuffer->pointer(), channelCount);
778 return BAD_VALUE;
779 }
780 frameCount = sharedBuffer->size()/channelCount/sizeof(int16_t);
781 }
782 }
783
Eric Laurentbda74692009-11-04 08:27:26 -0800784 sp<IAudioTrack> track = audioFlinger->createTrack(getpid(),
785 streamType,
786 sampleRate,
787 format,
Jean-Michel Trivi54392232011-05-24 15:53:33 -0700788 channelMask,
Eric Laurentbda74692009-11-04 08:27:26 -0800789 frameCount,
790 ((uint16_t)flags) << 16,
791 sharedBuffer,
792 output,
Eric Laurent65b65452010-06-01 23:49:17 -0700793 &mSessionId,
Eric Laurentbda74692009-11-04 08:27:26 -0800794 &status);
795
796 if (track == 0) {
797 LOGE("AudioFlinger could not create track, status: %d", status);
798 return status;
799 }
800 sp<IMemory> cblk = track->getCblk();
801 if (cblk == 0) {
802 LOGE("Could not get control block");
803 return NO_INIT;
804 }
805 mAudioTrack.clear();
806 mAudioTrack = track;
807 mCblkMemory.clear();
808 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);
820 mCblk->sendLevel = uint16_t(mSendLevel * 0x1000);
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);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800832 int 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;
854 if (UNLIKELY(!active)) {
855 LOGV("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 }
Eric Laurentbda74692009-11-04 08:27:26 -0800859 if (UNLIKELY(!waitCount)) {
860 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();
868 if (mActive == 0) {
869 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 }
Eric Laurenta553c252009-07-17 12:17:14 -0700877 if (__builtin_expect(result!=NO_ERROR, false)) {
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) {
883 LOGW( "obtainBuffer timed out (is the CPU pegged?) %p "
884 "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) {
895 LOGW("obtainBuffer create Track error %d", result);
896 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);
918 LOGW("obtainBuffer() track %p disabled, restarting", this);
919 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.
964 LOGE("AudioTrack::write(buffer=%p, size=%u (%d)",
965 buffer, userSize, userSize);
966 return BAD_VALUE;
967 }
968
969 LOGV("write %p: %d bytes, mActive=%d", this, userSize, mActive);
970
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;
Eric Laurent913af0b2011-03-17 09:36:51 -0700981 size_t frameSz = (size_t)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;
1035 mLock.unlock();
1036
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001037 // Manage underrun callback
Eric Laurentae29b762011-03-28 18:37:07 -07001038 if (mActive && (cblk->framesAvailable() == cblk->frameCount)) {
Eric Laurent421ddc02011-03-07 14:52:59 -08001039 LOGV("Underrun user: %x, server: %x, flags %04x", cblk->user, cblk->server, cblk->flags);
Eric Laurentae29b762011-03-28 18:37:07 -07001040 if (!(android_atomic_or(CBLK_UNDERRUN_ON, &cblk->flags) & CBLK_UNDERRUN_MSK)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001041 mCbf(EVENT_UNDERRUN, mUserData, 0);
Eric Laurent421ddc02011-03-07 14:52:59 -08001042 if (cblk->server == cblk->frameCount) {
Eric Laurenta553c252009-07-17 12:17:14 -07001043 mCbf(EVENT_BUFFER_END, mUserData, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001044 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001045 if (mSharedBuffer != 0) return false;
1046 }
1047 }
Eric Laurenta553c252009-07-17 12:17:14 -07001048
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001049 // Manage loop end callback
Eric Laurent421ddc02011-03-07 14:52:59 -08001050 while (mLoopCount > cblk->loopCount) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001051 int loopCount = -1;
1052 mLoopCount--;
1053 if (mLoopCount >= 0) loopCount = mLoopCount;
1054
1055 mCbf(EVENT_LOOP_END, mUserData, (void *)&loopCount);
1056 }
1057
1058 // Manage marker callback
Jean-Michel Trivi4a5c1a72009-03-24 18:11:07 -07001059 if (!mMarkerReached && (mMarkerPosition > 0)) {
Eric Laurent421ddc02011-03-07 14:52:59 -08001060 if (cblk->server >= mMarkerPosition) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001061 mCbf(EVENT_MARKER, mUserData, (void *)&mMarkerPosition);
Jean-Michel Trivi4a5c1a72009-03-24 18:11:07 -07001062 mMarkerReached = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001063 }
1064 }
1065
1066 // Manage new position callback
Eric Laurenta553c252009-07-17 12:17:14 -07001067 if (mUpdatePeriod > 0) {
Eric Laurent421ddc02011-03-07 14:52:59 -08001068 while (cblk->server >= mNewPosition) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001069 mCbf(EVENT_NEW_POS, mUserData, (void *)&mNewPosition);
1070 mNewPosition += mUpdatePeriod;
1071 }
1072 }
1073
1074 // If Shared buffer is used, no data is requested from client.
1075 if (mSharedBuffer != 0) {
1076 frames = 0;
1077 } else {
1078 frames = mRemainingFrames;
1079 }
1080
Eric Laurentf1d360a2011-09-07 11:13:23 -07001081 int32_t waitCount = -1;
1082 if (mUpdatePeriod || (!mMarkerReached && mMarkerPosition) || mLoopCount) {
1083 waitCount = 1;
1084 }
1085
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001086 do {
1087
1088 audioBuffer.frameCount = frames;
Eric Laurenta553c252009-07-17 12:17:14 -07001089
1090 // Calling obtainBuffer() with a wait count of 1
1091 // limits wait time to WAIT_PERIOD_MS. This prevents from being
1092 // stuck here not being able to handle timed events (position, markers, loops).
Eric Laurentf1d360a2011-09-07 11:13:23 -07001093 status_t err = obtainBuffer(&audioBuffer, waitCount);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001094 if (err < NO_ERROR) {
1095 if (err != TIMED_OUT) {
Eric Laurentef028272009-04-21 07:56:33 -07001096 LOGE_IF(err != status_t(NO_MORE_BUFFERS), "Error obtaining an audio buffer, giving up.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001097 return false;
1098 }
1099 break;
1100 }
1101 if (err == status_t(STOPPED)) return false;
1102
1103 // Divide buffer size by 2 to take into account the expansion
1104 // due to 8 to 16 bit conversion: the callback must fill only half
1105 // of the destination buffer
Dima Zavin24fc2fb2011-04-19 22:30:36 -07001106 if (mFormat == AUDIO_FORMAT_PCM_8_BIT && !(mFlags & AUDIO_POLICY_OUTPUT_FLAG_DIRECT)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001107 audioBuffer.size >>= 1;
1108 }
1109
1110 size_t reqSize = audioBuffer.size;
1111 mCbf(EVENT_MORE_DATA, mUserData, &audioBuffer);
1112 writtenSize = audioBuffer.size;
1113
1114 // Sanity check on returned size
The Android Open Source Project4df24232009-03-05 14:34:35 -08001115 if (ssize_t(writtenSize) <= 0) {
1116 // The callback is done filling buffers
1117 // Keep this thread going to handle timed events and
1118 // still try to get more data in intervals of WAIT_PERIOD_MS
1119 // but don't just loop and block the CPU, so wait
1120 usleep(WAIT_PERIOD_MS*1000);
1121 break;
1122 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001123 if (writtenSize > reqSize) writtenSize = reqSize;
1124
Dima Zavin24fc2fb2011-04-19 22:30:36 -07001125 if (mFormat == AUDIO_FORMAT_PCM_8_BIT && !(mFlags & AUDIO_POLICY_OUTPUT_FLAG_DIRECT)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001126 // 8 to 16 bit conversion
1127 const int8_t *src = audioBuffer.i8 + writtenSize-1;
1128 int count = writtenSize;
1129 int16_t *dst = audioBuffer.i16 + writtenSize-1;
1130 while(count--) {
1131 *dst-- = (int16_t)(*src--^0x80) << 8;
1132 }
1133 writtenSize <<= 1;
1134 }
1135
1136 audioBuffer.size = writtenSize;
Eric Laurenta553c252009-07-17 12:17:14 -07001137 // NOTE: mCblk->frameSize is not equal to AudioTrack::frameSize() for
1138 // 8 bit PCM data: in this case, mCblk->frameSize is based on a sampel size of
1139 // 16 bit.
1140 audioBuffer.frameCount = writtenSize/mCblk->frameSize;
1141
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001142 frames -= audioBuffer.frameCount;
1143
1144 releaseBuffer(&audioBuffer);
1145 }
1146 while (frames);
1147
1148 if (frames == 0) {
Eric Laurenteb8f850d2010-05-14 03:26:45 -07001149 mRemainingFrames = mNotificationFramesAct;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001150 } else {
1151 mRemainingFrames = frames;
1152 }
1153 return true;
1154}
1155
Eric Laurent421ddc02011-03-07 14:52:59 -08001156// must be called with mLock and cblk.lock held. Callers must also hold strong references on
1157// the IAudioTrack and IMemory in case they are recreated here.
1158// If the IAudioTrack is successfully restored, the cblk pointer is updated
1159status_t AudioTrack::restoreTrack_l(audio_track_cblk_t*& cblk, bool fromStart)
1160{
1161 status_t result;
1162
Eric Laurentae29b762011-03-28 18:37:07 -07001163 if (!(android_atomic_or(CBLK_RESTORING_ON, &cblk->flags) & CBLK_RESTORING_MSK)) {
Eric Laurent421ddc02011-03-07 14:52:59 -08001164 LOGW("dead IAudioTrack, creating a new one from %s",
1165 fromStart ? "start()" : "obtainBuffer()");
1166
Eric Laurent421ddc02011-03-07 14:52:59 -08001167 // signal old cblk condition so that other threads waiting for available buffers stop
1168 // waiting now
1169 cblk->cv.broadcast();
1170 cblk->lock.unlock();
1171
Eric Laurent05ce0942011-08-30 10:18:54 -07001172 // refresh the audio configuration cache in this process to make sure we get new
1173 // output parameters in getOutput_l() and createTrack_l()
1174 AudioSystem::clearAudioConfigCache();
1175
Eric Laurent421ddc02011-03-07 14:52:59 -08001176 // if the new IAudioTrack is created, createTrack_l() will modify the
1177 // following member variables: mAudioTrack, mCblkMemory and mCblk.
1178 // It will also delete the strong references on previous IAudioTrack and IMemory
1179 result = createTrack_l(mStreamType,
1180 cblk->sampleRate,
1181 mFormat,
Jean-Michel Trivi54392232011-05-24 15:53:33 -07001182 mChannelMask,
Eric Laurent421ddc02011-03-07 14:52:59 -08001183 mFrameCount,
1184 mFlags,
1185 mSharedBuffer,
1186 getOutput_l(),
1187 false);
1188
1189 if (result == NO_ERROR) {
Eric Laurentb0808f92011-09-06 12:36:15 -07001190 uint32_t user = cblk->user;
1191 uint32_t server = cblk->server;
Eric Laurent6667ac32011-03-21 11:49:00 -07001192 // restore write index and set other indexes to reflect empty buffer status
Eric Laurentb0808f92011-09-06 12:36:15 -07001193 mCblk->user = user;
1194 mCblk->server = user;
1195 mCblk->userBase = user;
1196 mCblk->serverBase = user;
Eric Laurent6667ac32011-03-21 11:49:00 -07001197 // restore loop: this is not guaranteed to succeed if new frame count is not
1198 // compatible with loop length
1199 setLoop_l(cblk->loopStart, cblk->loopEnd, cblk->loopCount);
Eric Laurent421ddc02011-03-07 14:52:59 -08001200 if (!fromStart) {
1201 mCblk->bufferTimeoutMs = MAX_RUN_TIMEOUT_MS;
Eric Laurentb0808f92011-09-06 12:36:15 -07001202 // Make sure that a client relying on callback events indicating underrun or
1203 // the actual amount of audio frames played (e.g SoundPool) receives them.
1204 if (mSharedBuffer == 0) {
1205 uint32_t frames = 0;
1206 if (user > server) {
1207 frames = ((user - server) > mCblk->frameCount) ?
1208 mCblk->frameCount : (user - server);
1209 memset(mCblk->buffers, 0, frames * mCblk->frameSize);
1210 }
1211 // restart playback even if buffer is not completely filled.
1212 android_atomic_or(CBLK_FORCEREADY_ON, &mCblk->flags);
1213 // stepUser() clears CBLK_UNDERRUN_ON flag enabling underrun callbacks to
1214 // the client
1215 mCblk->stepUser(frames);
1216 }
Eric Laurent421ddc02011-03-07 14:52:59 -08001217 }
Eric Laurent6667ac32011-03-21 11:49:00 -07001218 if (mActive) {
1219 result = mAudioTrack->start();
1220 }
Eric Laurent421ddc02011-03-07 14:52:59 -08001221 if (fromStart && result == NO_ERROR) {
1222 mNewPosition = mCblk->server + mUpdatePeriod;
1223 }
1224 }
1225 if (result != NO_ERROR) {
1226 mActive = false;
1227 }
1228
1229 // signal old cblk condition for other threads waiting for restore completion
Eric Laurentae29b762011-03-28 18:37:07 -07001230 android_atomic_or(CBLK_RESTORED_ON, &cblk->flags);
Eric Laurent421ddc02011-03-07 14:52:59 -08001231 cblk->cv.broadcast();
Eric Laurent421ddc02011-03-07 14:52:59 -08001232 } else {
1233 if (!(cblk->flags & CBLK_RESTORED_MSK)) {
1234 LOGW("dead IAudioTrack, waiting for a new one");
1235 mLock.unlock();
1236 result = cblk->cv.waitRelative(cblk->lock, milliseconds(RESTORE_TIMEOUT_MS));
1237 cblk->lock.unlock();
1238 mLock.lock();
1239 } else {
1240 LOGW("dead IAudioTrack, already restored");
1241 result = NO_ERROR;
1242 cblk->lock.unlock();
1243 }
1244 if (result != NO_ERROR || mActive == 0) {
1245 result = status_t(STOPPED);
1246 }
1247 }
1248 LOGV("restoreTrack_l() status %d mActive %d cblk %p, old cblk %p flags %08x old flags %08x",
1249 result, mActive, mCblk, cblk, mCblk->flags, cblk->flags);
1250
1251 if (result == NO_ERROR) {
1252 // from now on we switch to the newly created cblk
1253 cblk = mCblk;
1254 }
1255 cblk->lock.lock();
1256
1257 LOGW_IF(result != NO_ERROR, "restoreTrack_l() error %d", result);
1258
1259 return result;
1260}
1261
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001262status_t AudioTrack::dump(int fd, const Vector<String16>& args) const
1263{
1264
1265 const size_t SIZE = 256;
1266 char buffer[SIZE];
1267 String8 result;
1268
1269 result.append(" AudioTrack::dump\n");
1270 snprintf(buffer, 255, " stream type(%d), left - right volume(%f, %f)\n", mStreamType, mVolume[0], mVolume[1]);
1271 result.append(buffer);
Eric Laurenteb8f850d2010-05-14 03:26:45 -07001272 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 -08001273 result.append(buffer);
Eric Laurent88e209d2009-07-07 07:10:45 -07001274 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 -08001275 result.append(buffer);
1276 snprintf(buffer, 255, " active(%d), latency (%d)\n", mActive, mLatency);
1277 result.append(buffer);
1278 ::write(fd, result.string(), result.size());
1279 return NO_ERROR;
1280}
1281
1282// =========================================================================
1283
1284AudioTrack::AudioTrackThread::AudioTrackThread(AudioTrack& receiver, bool bCanCallJava)
1285 : Thread(bCanCallJava), mReceiver(receiver)
1286{
1287}
1288
1289bool AudioTrack::AudioTrackThread::threadLoop()
1290{
1291 return mReceiver.processAudioBuffer(this);
1292}
1293
1294status_t AudioTrack::AudioTrackThread::readyToRun()
1295{
1296 return NO_ERROR;
1297}
1298
1299void AudioTrack::AudioTrackThread::onFirstRef()
1300{
1301}
1302
1303// =========================================================================
1304
Eric Laurentae29b762011-03-28 18:37:07 -07001305
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001306audio_track_cblk_t::audio_track_cblk_t()
Mathias Agopiana729f972010-03-19 16:14:13 -07001307 : lock(Mutex::SHARED), cv(Condition::SHARED), user(0), server(0),
1308 userBase(0), serverBase(0), buffers(0), frameCount(0),
1309 loopStart(UINT_MAX), loopEnd(UINT_MAX), loopCount(0), volumeLR(0),
Eric Laurentae29b762011-03-28 18:37:07 -07001310 sendLevel(0), flags(0)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001311{
1312}
1313
1314uint32_t audio_track_cblk_t::stepUser(uint32_t frameCount)
1315{
1316 uint32_t u = this->user;
1317
1318 u += frameCount;
1319 // Ensure that user is never ahead of server for AudioRecord
Eric Laurenteb8f850d2010-05-14 03:26:45 -07001320 if (flags & CBLK_DIRECTION_MSK) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001321 // If stepServer() has been called once, switch to normal obtainBuffer() timeout period
1322 if (bufferTimeoutMs == MAX_STARTUP_TIMEOUT_MS-1) {
1323 bufferTimeoutMs = MAX_RUN_TIMEOUT_MS;
1324 }
1325 } else if (u > this->server) {
1326 LOGW("stepServer occured after track reset");
1327 u = this->server;
1328 }
1329
1330 if (u >= userBase + this->frameCount) {
1331 userBase += this->frameCount;
1332 }
1333
1334 this->user = u;
1335
1336 // Clear flow control error condition as new data has been written/read to/from buffer.
Eric Laurent913af0b2011-03-17 09:36:51 -07001337 if (flags & CBLK_UNDERRUN_MSK) {
Eric Laurentae29b762011-03-28 18:37:07 -07001338 android_atomic_and(~CBLK_UNDERRUN_MSK, &flags);
Eric Laurent913af0b2011-03-17 09:36:51 -07001339 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001340
1341 return u;
1342}
1343
1344bool audio_track_cblk_t::stepServer(uint32_t frameCount)
1345{
Eric Laurentae29b762011-03-28 18:37:07 -07001346 if (!tryLock()) {
1347 LOGW("stepServer() could not lock cblk");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001348 return false;
1349 }
1350
1351 uint32_t s = this->server;
1352
1353 s += frameCount;
Eric Laurenteb8f850d2010-05-14 03:26:45 -07001354 if (flags & CBLK_DIRECTION_MSK) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001355 // Mark that we have read the first buffer so that next time stepUser() is called
1356 // we switch to normal obtainBuffer() timeout period
1357 if (bufferTimeoutMs == MAX_STARTUP_TIMEOUT_MS) {
Eric Laurentbda74692009-11-04 08:27:26 -08001358 bufferTimeoutMs = MAX_STARTUP_TIMEOUT_MS - 1;
Eric Laurenta553c252009-07-17 12:17:14 -07001359 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001360 // It is possible that we receive a flush()
1361 // while the mixer is processing a block: in this case,
1362 // stepServer() is called After the flush() has reset u & s and
1363 // we have s > u
1364 if (s > this->user) {
1365 LOGW("stepServer occured after track reset");
1366 s = this->user;
1367 }
1368 }
1369
1370 if (s >= loopEnd) {
1371 LOGW_IF(s > loopEnd, "stepServer: s %u > loopEnd %u", s, loopEnd);
1372 s = loopStart;
1373 if (--loopCount == 0) {
1374 loopEnd = UINT_MAX;
1375 loopStart = UINT_MAX;
1376 }
1377 }
1378 if (s >= serverBase + this->frameCount) {
1379 serverBase += this->frameCount;
1380 }
1381
1382 this->server = s;
1383
Eric Laurent421ddc02011-03-07 14:52:59 -08001384 if (!(flags & CBLK_INVALID_MSK)) {
1385 cv.signal();
1386 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001387 lock.unlock();
1388 return true;
1389}
1390
1391void* audio_track_cblk_t::buffer(uint32_t offset) const
1392{
Eric Laurenta553c252009-07-17 12:17:14 -07001393 return (int8_t *)this->buffers + (offset - userBase) * this->frameSize;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001394}
1395
1396uint32_t audio_track_cblk_t::framesAvailable()
1397{
1398 Mutex::Autolock _l(lock);
1399 return framesAvailable_l();
1400}
1401
1402uint32_t audio_track_cblk_t::framesAvailable_l()
1403{
1404 uint32_t u = this->user;
1405 uint32_t s = this->server;
1406
Eric Laurenteb8f850d2010-05-14 03:26:45 -07001407 if (flags & CBLK_DIRECTION_MSK) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001408 uint32_t limit = (s < loopStart) ? s : loopStart;
1409 return limit + frameCount - u;
1410 } else {
1411 return frameCount + u - s;
1412 }
1413}
1414
1415uint32_t audio_track_cblk_t::framesReady()
1416{
1417 uint32_t u = this->user;
1418 uint32_t s = this->server;
1419
Eric Laurenteb8f850d2010-05-14 03:26:45 -07001420 if (flags & CBLK_DIRECTION_MSK) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001421 if (u < loopEnd) {
1422 return u - s;
1423 } else {
Eric Laurentae29b762011-03-28 18:37:07 -07001424 // do not block on mutex shared with client on AudioFlinger side
1425 if (!tryLock()) {
1426 LOGW("framesReady() could not lock cblk");
1427 return 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001428 }
Eric Laurentae29b762011-03-28 18:37:07 -07001429 uint32_t frames = UINT_MAX;
1430 if (loopCount >= 0) {
1431 frames = (loopEnd - loopStart)*loopCount + u - s;
1432 }
1433 lock.unlock();
1434 return frames;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001435 }
1436 } else {
1437 return s - u;
1438 }
1439}
1440
Eric Laurentae29b762011-03-28 18:37:07 -07001441bool audio_track_cblk_t::tryLock()
1442{
1443 // the code below simulates lock-with-timeout
1444 // we MUST do this to protect the AudioFlinger server
1445 // as this lock is shared with the client.
1446 status_t err;
1447
1448 err = lock.tryLock();
1449 if (err == -EBUSY) { // just wait a bit
1450 usleep(1000);
1451 err = lock.tryLock();
1452 }
1453 if (err != NO_ERROR) {
1454 // probably, the client just died.
1455 return false;
1456 }
1457 return true;
1458}
1459
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001460// -------------------------------------------------------------------------
1461
1462}; // namespace android
1463