blob: adc1cbb3d5cb8076fd6fd72a89f09802819187c8 [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()
Glenn Kasten99d54432011-06-22 16:15:25 -070082 : mStatus(NO_INIT),
83 mPreviousPriority(ANDROID_PRIORITY_NORMAL), mPreviousSchedulingGroup(ANDROID_TGROUP_DEFAULT)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080084{
85}
86
87AudioTrack::AudioTrack(
88 int streamType,
89 uint32_t sampleRate,
90 int format,
Jean-Michel Trivi54392232011-05-24 15:53:33 -070091 int channelMask,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080092 int frameCount,
93 uint32_t flags,
94 callback_t cbf,
95 void* user,
Eric Laurent65b65452010-06-01 23:49:17 -070096 int notificationFrames,
97 int sessionId)
Glenn Kasten99d54432011-06-22 16:15:25 -070098 : mStatus(NO_INIT),
99 mPreviousPriority(ANDROID_PRIORITY_NORMAL), mPreviousSchedulingGroup(ANDROID_TGROUP_DEFAULT)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800100{
Jean-Michel Trivi54392232011-05-24 15:53:33 -0700101 mStatus = set(streamType, sampleRate, format, channelMask,
Eric Laurent619346f2010-06-21 09:27:30 -0700102 frameCount, flags, cbf, user, notificationFrames,
103 0, false, sessionId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800104}
105
106AudioTrack::AudioTrack(
107 int streamType,
108 uint32_t sampleRate,
109 int format,
Jean-Michel Trivi54392232011-05-24 15:53:33 -0700110 int channelMask,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800111 const sp<IMemory>& sharedBuffer,
112 uint32_t flags,
113 callback_t cbf,
114 void* user,
Eric Laurent65b65452010-06-01 23:49:17 -0700115 int notificationFrames,
116 int sessionId)
Glenn Kasten99d54432011-06-22 16:15:25 -0700117 : mStatus(NO_INIT),
118 mPreviousPriority(ANDROID_PRIORITY_NORMAL), mPreviousSchedulingGroup(ANDROID_TGROUP_DEFAULT)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800119{
Jean-Michel Trivi54392232011-05-24 15:53:33 -0700120 mStatus = set(streamType, sampleRate, format, channelMask,
Eric Laurent619346f2010-06-21 09:27:30 -0700121 0, flags, cbf, user, notificationFrames,
122 sharedBuffer, false, sessionId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800123}
124
125AudioTrack::~AudioTrack()
126{
Steve Block71f2cf12011-10-20 11:56:00 +0100127 ALOGV_IF(mSharedBuffer != 0, "Destructor sharedBuffer: %p", mSharedBuffer->pointer());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800128
129 if (mStatus == NO_ERROR) {
130 // Make sure that callback function exits in the case where
131 // it is looping on buffer full condition in obtainBuffer().
132 // Otherwise the callback thread will never exit.
133 stop();
134 if (mAudioTrackThread != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800135 mAudioTrackThread->requestExitAndWait();
136 mAudioTrackThread.clear();
137 }
138 mAudioTrack.clear();
139 IPCThreadState::self()->flushCommands();
Marco Nelissenc74b93f2011-08-02 13:33:41 -0700140 AudioSystem::releaseAudioSessionId(mSessionId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800141 }
142}
143
144status_t AudioTrack::set(
145 int streamType,
146 uint32_t sampleRate,
147 int format,
Jean-Michel Trivi54392232011-05-24 15:53:33 -0700148 int channelMask,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800149 int frameCount,
150 uint32_t flags,
151 callback_t cbf,
152 void* user,
153 int notificationFrames,
154 const sp<IMemory>& sharedBuffer,
Eric Laurent65b65452010-06-01 23:49:17 -0700155 bool threadCanCallJava,
156 int sessionId)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800157{
158
Steve Block71f2cf12011-10-20 11:56:00 +0100159 ALOGV_IF(sharedBuffer != 0, "sharedBuffer: %p, size: %d", sharedBuffer->pointer(), sharedBuffer->size());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800160
Eric Laurent421ddc02011-03-07 14:52:59 -0800161 AutoMutex lock(mLock);
Eric Laurentef028272009-04-21 07:56:33 -0700162 if (mAudioTrack != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800163 LOGE("Track already in use");
164 return INVALID_OPERATION;
165 }
166
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800167 int afSampleRate;
168 if (AudioSystem::getOutputSamplingRate(&afSampleRate, streamType) != NO_ERROR) {
169 return NO_INIT;
170 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800171 uint32_t afLatency;
172 if (AudioSystem::getOutputLatency(&afLatency, streamType) != NO_ERROR) {
173 return NO_INIT;
174 }
175
176 // handle default values first.
Dima Zavin24fc2fb2011-04-19 22:30:36 -0700177 if (streamType == AUDIO_STREAM_DEFAULT) {
178 streamType = AUDIO_STREAM_MUSIC;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800179 }
180 if (sampleRate == 0) {
181 sampleRate = afSampleRate;
182 }
183 // these below should probably come from the audioFlinger too...
184 if (format == 0) {
Dima Zavin24fc2fb2011-04-19 22:30:36 -0700185 format = AUDIO_FORMAT_PCM_16_BIT;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800186 }
Jean-Michel Trivi54392232011-05-24 15:53:33 -0700187 if (channelMask == 0) {
188 channelMask = AUDIO_CHANNEL_OUT_STEREO;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800189 }
190
191 // validate parameters
Dima Zavin24fc2fb2011-04-19 22:30:36 -0700192 if (!audio_is_valid_format(format)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800193 LOGE("Invalid format");
194 return BAD_VALUE;
195 }
Eric Laurenta553c252009-07-17 12:17:14 -0700196
197 // force direct flag if format is not linear PCM
Dima Zavin24fc2fb2011-04-19 22:30:36 -0700198 if (!audio_is_linear_pcm(format)) {
199 flags |= AUDIO_POLICY_OUTPUT_FLAG_DIRECT;
Eric Laurenta553c252009-07-17 12:17:14 -0700200 }
201
Jean-Michel Trivi54392232011-05-24 15:53:33 -0700202 if (!audio_is_output_channel(channelMask)) {
Eric Laurenta553c252009-07-17 12:17:14 -0700203 LOGE("Invalid channel mask");
204 return BAD_VALUE;
205 }
Jean-Michel Trivi54392232011-05-24 15:53:33 -0700206 uint32_t channelCount = popcount(channelMask);
Eric Laurenta553c252009-07-17 12:17:14 -0700207
Dima Zavin24fc2fb2011-04-19 22:30:36 -0700208 audio_io_handle_t output = AudioSystem::getOutput(
209 (audio_stream_type_t)streamType,
Jean-Michel Trivi54392232011-05-24 15:53:33 -0700210 sampleRate,format, channelMask,
Dima Zavin24fc2fb2011-04-19 22:30:36 -0700211 (audio_policy_output_flags_t)flags);
Eric Laurenta553c252009-07-17 12:17:14 -0700212
213 if (output == 0) {
214 LOGE("Could not get audio output for stream type %d", streamType);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800215 return BAD_VALUE;
216 }
217
Eric Laurentbda74692009-11-04 08:27:26 -0800218 mVolume[LEFT] = 1.0f;
219 mVolume[RIGHT] = 1.0f;
Eric Laurent65b65452010-06-01 23:49:17 -0700220 mSendLevel = 0;
Eric Laurenteb8f850d2010-05-14 03:26:45 -0700221 mFrameCount = frameCount;
222 mNotificationFramesReq = notificationFrames;
Eric Laurent65b65452010-06-01 23:49:17 -0700223 mSessionId = sessionId;
Eric Laurent7070b362010-07-16 07:43:46 -0700224 mAuxEffectId = 0;
Eric Laurent65b65452010-06-01 23:49:17 -0700225
Eric Laurentbda74692009-11-04 08:27:26 -0800226 // create the IAudioTrack
Eric Laurent421ddc02011-03-07 14:52:59 -0800227 status_t status = createTrack_l(streamType,
228 sampleRate,
Jean-Michel Trivi54392232011-05-24 15:53:33 -0700229 (uint32_t)format,
230 (uint32_t)channelMask,
Eric Laurent421ddc02011-03-07 14:52:59 -0800231 frameCount,
232 flags,
233 sharedBuffer,
234 output,
235 true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800236
Eric Laurentbda74692009-11-04 08:27:26 -0800237 if (status != NO_ERROR) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800238 return status;
239 }
Eric Laurentbda74692009-11-04 08:27:26 -0800240
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800241 if (cbf != 0) {
242 mAudioTrackThread = new AudioTrackThread(*this, threadCanCallJava);
243 if (mAudioTrackThread == 0) {
244 LOGE("Could not create callback thread");
245 return NO_INIT;
246 }
247 }
248
249 mStatus = NO_ERROR;
250
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800251 mStreamType = streamType;
Jean-Michel Trivi54392232011-05-24 15:53:33 -0700252 mFormat = (uint32_t)format;
253 mChannelMask = (uint32_t)channelMask;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800254 mChannelCount = channelCount;
255 mSharedBuffer = sharedBuffer;
256 mMuted = false;
257 mActive = 0;
258 mCbf = cbf;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800259 mUserData = user;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800260 mLoopCount = 0;
261 mMarkerPosition = 0;
Jean-Michel Trivi4a5c1a72009-03-24 18:11:07 -0700262 mMarkerReached = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800263 mNewPosition = 0;
264 mUpdatePeriod = 0;
Jean-Michel Trivi22cb2042011-08-25 16:47:23 -0700265 mFlushed = false;
Eric Laurenta553c252009-07-17 12:17:14 -0700266 mFlags = flags;
Marco Nelissenc74b93f2011-08-02 13:33:41 -0700267 AudioSystem::acquireAudioSessionId(mSessionId);
Eric Laurent7e8626f2011-09-13 15:04:17 -0700268 mRestoreStatus = NO_ERROR;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800269 return NO_ERROR;
270}
271
272status_t AudioTrack::initCheck() const
273{
274 return mStatus;
275}
276
277// -------------------------------------------------------------------------
278
279uint32_t AudioTrack::latency() const
280{
281 return mLatency;
282}
283
284int AudioTrack::streamType() const
285{
286 return mStreamType;
287}
288
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800289int AudioTrack::format() const
290{
291 return mFormat;
292}
293
294int AudioTrack::channelCount() const
295{
296 return mChannelCount;
297}
298
299uint32_t AudioTrack::frameCount() const
300{
Eric Laurenteb8f850d2010-05-14 03:26:45 -0700301 return mCblk->frameCount;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800302}
303
304int AudioTrack::frameSize() const
305{
Dima Zavin24fc2fb2011-04-19 22:30:36 -0700306 if (audio_is_linear_pcm(mFormat)) {
Eric Laurentc310dcb2011-06-16 21:30:45 -0700307 return channelCount()*audio_bytes_per_sample(mFormat);
Eric Laurenta553c252009-07-17 12:17:14 -0700308 } else {
309 return sizeof(uint8_t);
310 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800311}
312
313sp<IMemory>& AudioTrack::sharedBuffer()
314{
315 return mSharedBuffer;
316}
317
318// -------------------------------------------------------------------------
319
320void AudioTrack::start()
321{
322 sp<AudioTrackThread> t = mAudioTrackThread;
Glenn Kasten028ab992011-06-22 16:18:04 -0700323 status_t status = NO_ERROR;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800324
Steve Block71f2cf12011-10-20 11:56:00 +0100325 ALOGV("start %p", this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800326 if (t != 0) {
327 if (t->exitPending()) {
328 if (t->requestExitAndWait() == WOULD_BLOCK) {
329 LOGE("AudioTrack::start called from thread");
330 return;
331 }
332 }
333 t->mLock.lock();
334 }
335
Eric Laurentf3d6dd02010-11-18 08:40:16 -0800336 AutoMutex lock(mLock);
Eric Laurent421ddc02011-03-07 14:52:59 -0800337 // acquire a strong reference on the IMemory and IAudioTrack so that they cannot be destroyed
338 // while we are accessing the cblk
339 sp <IAudioTrack> audioTrack = mAudioTrack;
340 sp <IMemory> iMem = mCblkMemory;
341 audio_track_cblk_t* cblk = mCblk;
342
Eric Laurentf3d6dd02010-11-18 08:40:16 -0800343 if (mActive == 0) {
Jean-Michel Trivi22cb2042011-08-25 16:47:23 -0700344 mFlushed = false;
Eric Laurentf3d6dd02010-11-18 08:40:16 -0800345 mActive = 1;
Eric Laurent421ddc02011-03-07 14:52:59 -0800346 mNewPosition = cblk->server + mUpdatePeriod;
Eric Laurent913af0b2011-03-17 09:36:51 -0700347 cblk->lock.lock();
Eric Laurent421ddc02011-03-07 14:52:59 -0800348 cblk->bufferTimeoutMs = MAX_STARTUP_TIMEOUT_MS;
349 cblk->waitTimeMs = 0;
Eric Laurentae29b762011-03-28 18:37:07 -0700350 android_atomic_and(~CBLK_DISABLED_ON, &cblk->flags);
Eric Laurent059b4be2009-11-09 23:32:22 -0800351 if (t != 0) {
Glenn Kasten99d54432011-06-22 16:15:25 -0700352 t->run("AudioTrackThread", ANDROID_PRIORITY_AUDIO);
Eric Laurent059b4be2009-11-09 23:32:22 -0800353 } else {
Glenn Kasten99d54432011-06-22 16:15:25 -0700354 mPreviousPriority = getpriority(PRIO_PROCESS, 0);
355 mPreviousSchedulingGroup = androidGetThreadSchedulingGroup(0);
356 androidSetThreadPriority(0, ANDROID_PRIORITY_AUDIO);
Eric Laurent059b4be2009-11-09 23:32:22 -0800357 }
358
Steve Block71f2cf12011-10-20 11:56:00 +0100359 ALOGV("start %p before lock cblk %p", this, mCblk);
Eric Laurent421ddc02011-03-07 14:52:59 -0800360 if (!(cblk->flags & CBLK_INVALID_MSK)) {
361 cblk->lock.unlock();
Eric Laurenteb8f850d2010-05-14 03:26:45 -0700362 status = mAudioTrack->start();
Eric Laurent421ddc02011-03-07 14:52:59 -0800363 cblk->lock.lock();
364 if (status == DEAD_OBJECT) {
Eric Laurentae29b762011-03-28 18:37:07 -0700365 android_atomic_or(CBLK_INVALID_ON, &cblk->flags);
Eric Laurent49f02be2009-11-19 09:00:56 -0800366 }
Eric Laurent059b4be2009-11-09 23:32:22 -0800367 }
Eric Laurent421ddc02011-03-07 14:52:59 -0800368 if (cblk->flags & CBLK_INVALID_MSK) {
369 status = restoreTrack_l(cblk, true);
370 }
371 cblk->lock.unlock();
Eric Laurent059b4be2009-11-09 23:32:22 -0800372 if (status != NO_ERROR) {
Steve Block71f2cf12011-10-20 11:56:00 +0100373 ALOGV("start() failed");
Eric Laurentf3d6dd02010-11-18 08:40:16 -0800374 mActive = 0;
Eric Laurent059b4be2009-11-09 23:32:22 -0800375 if (t != 0) {
376 t->requestExit();
377 } else {
Glenn Kasten99d54432011-06-22 16:15:25 -0700378 setpriority(PRIO_PROCESS, 0, mPreviousPriority);
379 androidSetThreadSchedulingGroup(0, mPreviousSchedulingGroup);
Eric Laurent059b4be2009-11-09 23:32:22 -0800380 }
Eric Laurentbda74692009-11-04 08:27:26 -0800381 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800382 }
383
384 if (t != 0) {
385 t->mLock.unlock();
386 }
387}
388
389void AudioTrack::stop()
390{
391 sp<AudioTrackThread> t = mAudioTrackThread;
392
Steve Block71f2cf12011-10-20 11:56:00 +0100393 ALOGV("stop %p", this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800394 if (t != 0) {
395 t->mLock.lock();
396 }
397
Eric Laurentf3d6dd02010-11-18 08:40:16 -0800398 AutoMutex lock(mLock);
399 if (mActive == 1) {
400 mActive = 0;
Eric Laurentef028272009-04-21 07:56:33 -0700401 mCblk->cv.signal();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800402 mAudioTrack->stop();
403 // Cancel loops (If we are in the middle of a loop, playback
404 // would not stop until loopCount reaches 0).
Eric Laurent421ddc02011-03-07 14:52:59 -0800405 setLoop_l(0, 0, 0);
Jean-Michel Trivi4a5c1a72009-03-24 18:11:07 -0700406 // the playback head position will reset to 0, so if a marker is set, we need
407 // to activate it again
408 mMarkerReached = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800409 // Force flush if a shared buffer is used otherwise audioflinger
410 // will not stop before end of buffer is reached.
411 if (mSharedBuffer != 0) {
Eric Laurent421ddc02011-03-07 14:52:59 -0800412 flush_l();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800413 }
414 if (t != 0) {
415 t->requestExit();
416 } else {
Glenn Kasten99d54432011-06-22 16:15:25 -0700417 setpriority(PRIO_PROCESS, 0, mPreviousPriority);
418 androidSetThreadSchedulingGroup(0, mPreviousSchedulingGroup);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800419 }
420 }
421
422 if (t != 0) {
423 t->mLock.unlock();
424 }
425}
426
427bool AudioTrack::stopped() const
428{
429 return !mActive;
430}
431
432void AudioTrack::flush()
433{
Eric Laurent421ddc02011-03-07 14:52:59 -0800434 AutoMutex lock(mLock);
435 flush_l();
436}
437
438// must be called with mLock held
439void AudioTrack::flush_l()
440{
Steve Block71f2cf12011-10-20 11:56:00 +0100441 ALOGV("flush");
Eric Laurenta553c252009-07-17 12:17:14 -0700442
Jean-Michel Trivi4a5c1a72009-03-24 18:11:07 -0700443 // clear playback marker and periodic update counter
444 mMarkerPosition = 0;
445 mMarkerReached = false;
446 mUpdatePeriod = 0;
Eric Laurenta553c252009-07-17 12:17:14 -0700447
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800448 if (!mActive) {
Jean-Michel Trivi22cb2042011-08-25 16:47:23 -0700449 mFlushed = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800450 mAudioTrack->flush();
451 // Release AudioTrack callback thread in case it was waiting for new buffers
452 // in AudioTrack::obtainBuffer()
453 mCblk->cv.signal();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800454 }
455}
456
457void AudioTrack::pause()
458{
Steve Block71f2cf12011-10-20 11:56:00 +0100459 ALOGV("pause");
Eric Laurentf3d6dd02010-11-18 08:40:16 -0800460 AutoMutex lock(mLock);
461 if (mActive == 1) {
462 mActive = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800463 mAudioTrack->pause();
464 }
465}
466
467void AudioTrack::mute(bool e)
468{
469 mAudioTrack->mute(e);
470 mMuted = e;
471}
472
473bool AudioTrack::muted() const
474{
475 return mMuted;
476}
477
Eric Laurent65b65452010-06-01 23:49:17 -0700478status_t AudioTrack::setVolume(float left, float right)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800479{
Eric Laurent65b65452010-06-01 23:49:17 -0700480 if (left > 1.0f || right > 1.0f) {
481 return BAD_VALUE;
482 }
483
Eric Laurent421ddc02011-03-07 14:52:59 -0800484 AutoMutex lock(mLock);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800485 mVolume[LEFT] = left;
486 mVolume[RIGHT] = right;
487
488 // write must be atomic
Eric Laurent65b65452010-06-01 23:49:17 -0700489 mCblk->volumeLR = (uint32_t(uint16_t(right * 0x1000)) << 16) | uint16_t(left * 0x1000);
490
491 return NO_ERROR;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800492}
493
494void AudioTrack::getVolume(float* left, float* right)
495{
Eric Laurent65b65452010-06-01 23:49:17 -0700496 if (left != NULL) {
497 *left = mVolume[LEFT];
498 }
499 if (right != NULL) {
500 *right = mVolume[RIGHT];
501 }
502}
503
Eric Laurent7070b362010-07-16 07:43:46 -0700504status_t AudioTrack::setAuxEffectSendLevel(float level)
Eric Laurent65b65452010-06-01 23:49:17 -0700505{
Steve Block71f2cf12011-10-20 11:56:00 +0100506 ALOGV("setAuxEffectSendLevel(%f)", level);
Eric Laurent65b65452010-06-01 23:49:17 -0700507 if (level > 1.0f) {
508 return BAD_VALUE;
509 }
Eric Laurent421ddc02011-03-07 14:52:59 -0800510 AutoMutex lock(mLock);
Eric Laurent65b65452010-06-01 23:49:17 -0700511
512 mSendLevel = level;
513
514 mCblk->sendLevel = uint16_t(level * 0x1000);
515
516 return NO_ERROR;
517}
518
Eric Laurent7070b362010-07-16 07:43:46 -0700519void AudioTrack::getAuxEffectSendLevel(float* level)
Eric Laurent65b65452010-06-01 23:49:17 -0700520{
521 if (level != NULL) {
522 *level = mSendLevel;
523 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800524}
525
Eric Laurent88e209d2009-07-07 07:10:45 -0700526status_t AudioTrack::setSampleRate(int rate)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800527{
528 int afSamplingRate;
529
530 if (AudioSystem::getOutputSamplingRate(&afSamplingRate, mStreamType) != NO_ERROR) {
Eric Laurent88e209d2009-07-07 07:10:45 -0700531 return NO_INIT;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800532 }
533 // Resampler implementation limits input sampling rate to 2 x output sampling rate.
Eric Laurent88e209d2009-07-07 07:10:45 -0700534 if (rate <= 0 || rate > afSamplingRate*2 ) return BAD_VALUE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800535
Eric Laurent421ddc02011-03-07 14:52:59 -0800536 AutoMutex lock(mLock);
Eric Laurent88e209d2009-07-07 07:10:45 -0700537 mCblk->sampleRate = rate;
538 return NO_ERROR;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800539}
540
541uint32_t AudioTrack::getSampleRate()
542{
Eric Laurent421ddc02011-03-07 14:52:59 -0800543 AutoMutex lock(mLock);
Eric Laurent88e209d2009-07-07 07:10:45 -0700544 return mCblk->sampleRate;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800545}
546
547status_t AudioTrack::setLoop(uint32_t loopStart, uint32_t loopEnd, int loopCount)
548{
Eric Laurent421ddc02011-03-07 14:52:59 -0800549 AutoMutex lock(mLock);
550 return setLoop_l(loopStart, loopEnd, loopCount);
551}
552
553// must be called with mLock held
554status_t AudioTrack::setLoop_l(uint32_t loopStart, uint32_t loopEnd, int loopCount)
555{
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800556 audio_track_cblk_t* cblk = mCblk;
557
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800558 Mutex::Autolock _l(cblk->lock);
559
560 if (loopCount == 0) {
561 cblk->loopStart = UINT_MAX;
562 cblk->loopEnd = UINT_MAX;
563 cblk->loopCount = 0;
564 mLoopCount = 0;
565 return NO_ERROR;
566 }
567
568 if (loopStart >= loopEnd ||
Eric Laurent6667ac32011-03-21 11:49:00 -0700569 loopEnd - loopStart > cblk->frameCount ||
570 cblk->server > loopStart) {
Eric Laurenteb8f850d2010-05-14 03:26:45 -0700571 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 -0800572 return BAD_VALUE;
573 }
574
Eric Laurent6667ac32011-03-21 11:49:00 -0700575 if ((mSharedBuffer != 0) && (loopEnd > cblk->frameCount)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800576 LOGE("setLoop invalid value: loop markers beyond data: loopStart %d, loopEnd %d, framecount %d",
Eric Laurenteb8f850d2010-05-14 03:26:45 -0700577 loopStart, loopEnd, cblk->frameCount);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800578 return BAD_VALUE;
Eric Laurenta553c252009-07-17 12:17:14 -0700579 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800580
581 cblk->loopStart = loopStart;
582 cblk->loopEnd = loopEnd;
583 cblk->loopCount = loopCount;
584 mLoopCount = loopCount;
585
586 return NO_ERROR;
587}
588
589status_t AudioTrack::getLoop(uint32_t *loopStart, uint32_t *loopEnd, int *loopCount)
590{
Eric Laurent421ddc02011-03-07 14:52:59 -0800591 AutoMutex lock(mLock);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800592 if (loopStart != 0) {
593 *loopStart = mCblk->loopStart;
594 }
595 if (loopEnd != 0) {
596 *loopEnd = mCblk->loopEnd;
597 }
598 if (loopCount != 0) {
599 if (mCblk->loopCount < 0) {
600 *loopCount = -1;
601 } else {
602 *loopCount = mCblk->loopCount;
603 }
604 }
605
606 return NO_ERROR;
607}
608
609status_t AudioTrack::setMarkerPosition(uint32_t marker)
610{
611 if (mCbf == 0) return INVALID_OPERATION;
612
613 mMarkerPosition = marker;
Jean-Michel Trivi4a5c1a72009-03-24 18:11:07 -0700614 mMarkerReached = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800615
616 return NO_ERROR;
617}
618
619status_t AudioTrack::getMarkerPosition(uint32_t *marker)
620{
621 if (marker == 0) return BAD_VALUE;
622
623 *marker = mMarkerPosition;
624
625 return NO_ERROR;
626}
627
628status_t AudioTrack::setPositionUpdatePeriod(uint32_t updatePeriod)
629{
630 if (mCbf == 0) return INVALID_OPERATION;
631
632 uint32_t curPosition;
633 getPosition(&curPosition);
634 mNewPosition = curPosition + updatePeriod;
635 mUpdatePeriod = updatePeriod;
636
637 return NO_ERROR;
638}
639
640status_t AudioTrack::getPositionUpdatePeriod(uint32_t *updatePeriod)
641{
642 if (updatePeriod == 0) return BAD_VALUE;
643
644 *updatePeriod = mUpdatePeriod;
645
646 return NO_ERROR;
647}
648
649status_t AudioTrack::setPosition(uint32_t position)
650{
Eric Laurent421ddc02011-03-07 14:52:59 -0800651 AutoMutex lock(mLock);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800652 Mutex::Autolock _l(mCblk->lock);
653
654 if (!stopped()) return INVALID_OPERATION;
655
656 if (position > mCblk->user) return BAD_VALUE;
657
658 mCblk->server = position;
Eric Laurentae29b762011-03-28 18:37:07 -0700659 android_atomic_or(CBLK_FORCEREADY_ON, &mCblk->flags);
Eric Laurenta553c252009-07-17 12:17:14 -0700660
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800661 return NO_ERROR;
662}
663
664status_t AudioTrack::getPosition(uint32_t *position)
665{
666 if (position == 0) return BAD_VALUE;
Eric Laurent421ddc02011-03-07 14:52:59 -0800667 AutoMutex lock(mLock);
Jean-Michel Trivi22cb2042011-08-25 16:47:23 -0700668 *position = mFlushed ? 0 : mCblk->server;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800669
670 return NO_ERROR;
671}
672
673status_t AudioTrack::reload()
674{
Eric Laurent421ddc02011-03-07 14:52:59 -0800675 AutoMutex lock(mLock);
676
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800677 if (!stopped()) return INVALID_OPERATION;
Eric Laurenta553c252009-07-17 12:17:14 -0700678
Eric Laurent421ddc02011-03-07 14:52:59 -0800679 flush_l();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800680
Eric Laurenteb8f850d2010-05-14 03:26:45 -0700681 mCblk->stepUser(mCblk->frameCount);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800682
683 return NO_ERROR;
684}
685
Eric Laurenta553c252009-07-17 12:17:14 -0700686audio_io_handle_t AudioTrack::getOutput()
687{
Eric Laurent421ddc02011-03-07 14:52:59 -0800688 AutoMutex lock(mLock);
689 return getOutput_l();
690}
691
692// must be called with mLock held
693audio_io_handle_t AudioTrack::getOutput_l()
694{
Dima Zavin24fc2fb2011-04-19 22:30:36 -0700695 return AudioSystem::getOutput((audio_stream_type_t)mStreamType,
Jean-Michel Trivi54392232011-05-24 15:53:33 -0700696 mCblk->sampleRate, mFormat, mChannelMask, (audio_policy_output_flags_t)mFlags);
Eric Laurenta553c252009-07-17 12:17:14 -0700697}
698
Eric Laurent65b65452010-06-01 23:49:17 -0700699int AudioTrack::getSessionId()
700{
701 return mSessionId;
702}
703
704status_t AudioTrack::attachAuxEffect(int effectId)
705{
Steve Block71f2cf12011-10-20 11:56:00 +0100706 ALOGV("attachAuxEffect(%d)", effectId);
Eric Laurent7070b362010-07-16 07:43:46 -0700707 status_t status = mAudioTrack->attachAuxEffect(effectId);
708 if (status == NO_ERROR) {
709 mAuxEffectId = effectId;
710 }
711 return status;
Eric Laurent65b65452010-06-01 23:49:17 -0700712}
713
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800714// -------------------------------------------------------------------------
715
Eric Laurent421ddc02011-03-07 14:52:59 -0800716// must be called with mLock held
717status_t AudioTrack::createTrack_l(
Eric Laurentbda74692009-11-04 08:27:26 -0800718 int streamType,
719 uint32_t sampleRate,
Jean-Michel Trivi54392232011-05-24 15:53:33 -0700720 uint32_t format,
721 uint32_t channelMask,
Eric Laurentbda74692009-11-04 08:27:26 -0800722 int frameCount,
723 uint32_t flags,
724 const sp<IMemory>& sharedBuffer,
Eric Laurenteb8f850d2010-05-14 03:26:45 -0700725 audio_io_handle_t output,
726 bool enforceFrameCount)
Eric Laurentbda74692009-11-04 08:27:26 -0800727{
728 status_t status;
729 const sp<IAudioFlinger>& audioFlinger = AudioSystem::get_audio_flinger();
730 if (audioFlinger == 0) {
731 LOGE("Could not get audioflinger");
732 return NO_INIT;
733 }
734
Eric Laurenteb8f850d2010-05-14 03:26:45 -0700735 int afSampleRate;
736 if (AudioSystem::getOutputSamplingRate(&afSampleRate, streamType) != NO_ERROR) {
737 return NO_INIT;
738 }
739 int afFrameCount;
740 if (AudioSystem::getOutputFrameCount(&afFrameCount, streamType) != NO_ERROR) {
741 return NO_INIT;
742 }
743 uint32_t afLatency;
744 if (AudioSystem::getOutputLatency(&afLatency, streamType) != NO_ERROR) {
745 return NO_INIT;
746 }
747
748 mNotificationFramesAct = mNotificationFramesReq;
Dima Zavin24fc2fb2011-04-19 22:30:36 -0700749 if (!audio_is_linear_pcm(format)) {
Eric Laurenteb8f850d2010-05-14 03:26:45 -0700750 if (sharedBuffer != 0) {
751 frameCount = sharedBuffer->size();
752 }
753 } else {
754 // Ensure that buffer depth covers at least audio hardware latency
755 uint32_t minBufCount = afLatency / ((1000 * afFrameCount)/afSampleRate);
756 if (minBufCount < 2) minBufCount = 2;
757
758 int minFrameCount = (afFrameCount*sampleRate*minBufCount)/afSampleRate;
759
760 if (sharedBuffer == 0) {
761 if (frameCount == 0) {
762 frameCount = minFrameCount;
763 }
764 if (mNotificationFramesAct == 0) {
765 mNotificationFramesAct = frameCount/2;
766 }
767 // Make sure that application is notified with sufficient margin
768 // before underrun
769 if (mNotificationFramesAct > (uint32_t)frameCount/2) {
770 mNotificationFramesAct = frameCount/2;
771 }
772 if (frameCount < minFrameCount) {
773 if (enforceFrameCount) {
774 LOGE("Invalid buffer size: minFrameCount %d, frameCount %d", minFrameCount, frameCount);
775 return BAD_VALUE;
776 } else {
777 frameCount = minFrameCount;
778 }
779 }
780 } else {
781 // Ensure that buffer alignment matches channelcount
Jean-Michel Trivi54392232011-05-24 15:53:33 -0700782 int channelCount = popcount(channelMask);
Eric Laurenteb8f850d2010-05-14 03:26:45 -0700783 if (((uint32_t)sharedBuffer->pointer() & (channelCount | 1)) != 0) {
784 LOGE("Invalid buffer alignement: address %p, channelCount %d", sharedBuffer->pointer(), channelCount);
785 return BAD_VALUE;
786 }
787 frameCount = sharedBuffer->size()/channelCount/sizeof(int16_t);
788 }
789 }
790
Eric Laurentbda74692009-11-04 08:27:26 -0800791 sp<IAudioTrack> track = audioFlinger->createTrack(getpid(),
792 streamType,
793 sampleRate,
794 format,
Jean-Michel Trivi54392232011-05-24 15:53:33 -0700795 channelMask,
Eric Laurentbda74692009-11-04 08:27:26 -0800796 frameCount,
797 ((uint16_t)flags) << 16,
798 sharedBuffer,
799 output,
Eric Laurent65b65452010-06-01 23:49:17 -0700800 &mSessionId,
Eric Laurentbda74692009-11-04 08:27:26 -0800801 &status);
802
803 if (track == 0) {
804 LOGE("AudioFlinger could not create track, status: %d", status);
805 return status;
806 }
807 sp<IMemory> cblk = track->getCblk();
808 if (cblk == 0) {
809 LOGE("Could not get control block");
810 return NO_INIT;
811 }
812 mAudioTrack.clear();
813 mAudioTrack = track;
814 mCblkMemory.clear();
815 mCblkMemory = cblk;
816 mCblk = static_cast<audio_track_cblk_t*>(cblk->pointer());
Eric Laurentae29b762011-03-28 18:37:07 -0700817 android_atomic_or(CBLK_DIRECTION_OUT, &mCblk->flags);
Eric Laurentbda74692009-11-04 08:27:26 -0800818 if (sharedBuffer == 0) {
819 mCblk->buffers = (char*)mCblk + sizeof(audio_track_cblk_t);
820 } else {
821 mCblk->buffers = sharedBuffer->pointer();
822 // Force buffer full condition as data is already present in shared memory
Eric Laurenteb8f850d2010-05-14 03:26:45 -0700823 mCblk->stepUser(mCblk->frameCount);
Eric Laurentbda74692009-11-04 08:27:26 -0800824 }
825
Eric Laurent65b65452010-06-01 23:49:17 -0700826 mCblk->volumeLR = (uint32_t(uint16_t(mVolume[RIGHT] * 0x1000)) << 16) | uint16_t(mVolume[LEFT] * 0x1000);
827 mCblk->sendLevel = uint16_t(mSendLevel * 0x1000);
Eric Laurent7070b362010-07-16 07:43:46 -0700828 mAudioTrack->attachAuxEffect(mAuxEffectId);
Eric Laurent49f02be2009-11-19 09:00:56 -0800829 mCblk->bufferTimeoutMs = MAX_STARTUP_TIMEOUT_MS;
830 mCblk->waitTimeMs = 0;
Eric Laurenteb8f850d2010-05-14 03:26:45 -0700831 mRemainingFrames = mNotificationFramesAct;
832 mLatency = afLatency + (1000*mCblk->frameCount) / sampleRate;
Eric Laurentbda74692009-11-04 08:27:26 -0800833 return NO_ERROR;
834}
835
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800836status_t AudioTrack::obtainBuffer(Buffer* audioBuffer, int32_t waitCount)
837{
Eric Laurent421ddc02011-03-07 14:52:59 -0800838 AutoMutex lock(mLock);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800839 int active;
Glenn Kasten028ab992011-06-22 16:18:04 -0700840 status_t result = NO_ERROR;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800841 audio_track_cblk_t* cblk = mCblk;
842 uint32_t framesReq = audioBuffer->frameCount;
Eric Laurentef028272009-04-21 07:56:33 -0700843 uint32_t waitTimeMs = (waitCount < 0) ? cblk->bufferTimeoutMs : WAIT_PERIOD_MS;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800844
845 audioBuffer->frameCount = 0;
846 audioBuffer->size = 0;
847
848 uint32_t framesAvail = cblk->framesAvailable();
849
Eric Laurent6667ac32011-03-21 11:49:00 -0700850 cblk->lock.lock();
851 if (cblk->flags & CBLK_INVALID_MSK) {
852 goto create_new_track;
853 }
854 cblk->lock.unlock();
855
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800856 if (framesAvail == 0) {
Eric Laurentbda74692009-11-04 08:27:26 -0800857 cblk->lock.lock();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800858 goto start_loop_here;
859 while (framesAvail == 0) {
860 active = mActive;
861 if (UNLIKELY(!active)) {
Steve Block71f2cf12011-10-20 11:56:00 +0100862 ALOGV("Not active and NO_MORE_BUFFERS");
Eric Laurentbda74692009-11-04 08:27:26 -0800863 cblk->lock.unlock();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800864 return NO_MORE_BUFFERS;
865 }
Eric Laurentbda74692009-11-04 08:27:26 -0800866 if (UNLIKELY(!waitCount)) {
867 cblk->lock.unlock();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800868 return WOULD_BLOCK;
Eric Laurentbda74692009-11-04 08:27:26 -0800869 }
Eric Laurenteb8f850d2010-05-14 03:26:45 -0700870 if (!(cblk->flags & CBLK_INVALID_MSK)) {
Eric Laurent421ddc02011-03-07 14:52:59 -0800871 mLock.unlock();
Eric Laurenteb8f850d2010-05-14 03:26:45 -0700872 result = cblk->cv.waitRelative(cblk->lock, milliseconds(waitTimeMs));
Eric Laurenteb8f850d2010-05-14 03:26:45 -0700873 cblk->lock.unlock();
Eric Laurent421ddc02011-03-07 14:52:59 -0800874 mLock.lock();
875 if (mActive == 0) {
876 return status_t(STOPPED);
877 }
878 cblk->lock.lock();
879 }
880
881 if (cblk->flags & CBLK_INVALID_MSK) {
Eric Laurenteb8f850d2010-05-14 03:26:45 -0700882 goto create_new_track;
883 }
Eric Laurenta553c252009-07-17 12:17:14 -0700884 if (__builtin_expect(result!=NO_ERROR, false)) {
Eric Laurentef028272009-04-21 07:56:33 -0700885 cblk->waitTimeMs += waitTimeMs;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800886 if (cblk->waitTimeMs >= cblk->bufferTimeoutMs) {
887 // timing out when a loop has been set and we have already written upto loop end
888 // is a normal condition: no need to wake AudioFlinger up.
889 if (cblk->user < cblk->loopEnd) {
890 LOGW( "obtainBuffer timed out (is the CPU pegged?) %p "
891 "user=%08x, server=%08x", this, cblk->user, cblk->server);
Eric Laurenta553c252009-07-17 12:17:14 -0700892 //unlock cblk mutex before calling mAudioTrack->start() (see issue #1617140)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800893 cblk->lock.unlock();
Eric Laurentbda74692009-11-04 08:27:26 -0800894 result = mAudioTrack->start();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800895 cblk->lock.lock();
Eric Laurent421ddc02011-03-07 14:52:59 -0800896 if (result == DEAD_OBJECT) {
Eric Laurentae29b762011-03-28 18:37:07 -0700897 android_atomic_or(CBLK_INVALID_ON, &cblk->flags);
Eric Laurent421ddc02011-03-07 14:52:59 -0800898create_new_track:
899 result = restoreTrack_l(cblk, false);
900 }
901 if (result != NO_ERROR) {
902 LOGW("obtainBuffer create Track error %d", result);
903 cblk->lock.unlock();
904 return result;
905 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800906 }
907 cblk->waitTimeMs = 0;
908 }
Eric Laurenta553c252009-07-17 12:17:14 -0700909
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800910 if (--waitCount == 0) {
Eric Laurentbda74692009-11-04 08:27:26 -0800911 cblk->lock.unlock();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800912 return TIMED_OUT;
913 }
914 }
915 // read the server count again
916 start_loop_here:
917 framesAvail = cblk->framesAvailable_l();
918 }
Eric Laurentbda74692009-11-04 08:27:26 -0800919 cblk->lock.unlock();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800920 }
921
Eric Laurent4712baa2010-09-30 16:12:31 -0700922 // restart track if it was disabled by audioflinger due to previous underrun
Eric Laurent421ddc02011-03-07 14:52:59 -0800923 if (mActive && (cblk->flags & CBLK_DISABLED_MSK)) {
Eric Laurentae29b762011-03-28 18:37:07 -0700924 android_atomic_and(~CBLK_DISABLED_ON, &cblk->flags);
925 LOGW("obtainBuffer() track %p disabled, restarting", this);
926 mAudioTrack->start();
Eric Laurent4712baa2010-09-30 16:12:31 -0700927 }
928
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800929 cblk->waitTimeMs = 0;
Eric Laurenta553c252009-07-17 12:17:14 -0700930
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800931 if (framesReq > framesAvail) {
932 framesReq = framesAvail;
933 }
934
935 uint32_t u = cblk->user;
936 uint32_t bufferEnd = cblk->userBase + cblk->frameCount;
937
938 if (u + framesReq > bufferEnd) {
939 framesReq = bufferEnd - u;
940 }
941
Eric Laurenta553c252009-07-17 12:17:14 -0700942 audioBuffer->flags = mMuted ? Buffer::MUTE : 0;
943 audioBuffer->channelCount = mChannelCount;
944 audioBuffer->frameCount = framesReq;
945 audioBuffer->size = framesReq * cblk->frameSize;
Dima Zavin24fc2fb2011-04-19 22:30:36 -0700946 if (audio_is_linear_pcm(mFormat)) {
947 audioBuffer->format = AUDIO_FORMAT_PCM_16_BIT;
Eric Laurenta553c252009-07-17 12:17:14 -0700948 } else {
949 audioBuffer->format = mFormat;
950 }
951 audioBuffer->raw = (int8_t *)cblk->buffer(u);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800952 active = mActive;
953 return active ? status_t(NO_ERROR) : status_t(STOPPED);
954}
955
956void AudioTrack::releaseBuffer(Buffer* audioBuffer)
957{
Eric Laurent421ddc02011-03-07 14:52:59 -0800958 AutoMutex lock(mLock);
959 mCblk->stepUser(audioBuffer->frameCount);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800960}
961
962// -------------------------------------------------------------------------
963
964ssize_t AudioTrack::write(const void* buffer, size_t userSize)
965{
966
967 if (mSharedBuffer != 0) return INVALID_OPERATION;
968
969 if (ssize_t(userSize) < 0) {
970 // sanity-check. user is most-likely passing an error code.
971 LOGE("AudioTrack::write(buffer=%p, size=%u (%d)",
972 buffer, userSize, userSize);
973 return BAD_VALUE;
974 }
975
Steve Block71f2cf12011-10-20 11:56:00 +0100976 ALOGV("write %p: %d bytes, mActive=%d", this, userSize, mActive);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800977
Eric Laurent421ddc02011-03-07 14:52:59 -0800978 // acquire a strong reference on the IMemory and IAudioTrack so that they cannot be destroyed
979 // while we are accessing the cblk
980 mLock.lock();
981 sp <IAudioTrack> audioTrack = mAudioTrack;
982 sp <IMemory> iMem = mCblkMemory;
983 mLock.unlock();
984
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800985 ssize_t written = 0;
986 const int8_t *src = (const int8_t *)buffer;
987 Buffer audioBuffer;
Eric Laurent913af0b2011-03-17 09:36:51 -0700988 size_t frameSz = (size_t)frameSize();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800989
990 do {
Eric Laurent913af0b2011-03-17 09:36:51 -0700991 audioBuffer.frameCount = userSize/frameSz;
Eric Laurenta553c252009-07-17 12:17:14 -0700992
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800993 // Calling obtainBuffer() with a negative wait count causes
994 // an (almost) infinite wait time.
995 status_t err = obtainBuffer(&audioBuffer, -1);
996 if (err < 0) {
997 // out of buffers, return #bytes written
998 if (err == status_t(NO_MORE_BUFFERS))
999 break;
1000 return ssize_t(err);
1001 }
1002
1003 size_t toWrite;
Eric Laurenta553c252009-07-17 12:17:14 -07001004
Dima Zavin24fc2fb2011-04-19 22:30:36 -07001005 if (mFormat == AUDIO_FORMAT_PCM_8_BIT && !(mFlags & AUDIO_POLICY_OUTPUT_FLAG_DIRECT)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001006 // Divide capacity by 2 to take expansion into account
1007 toWrite = audioBuffer.size>>1;
1008 // 8 to 16 bit conversion
1009 int count = toWrite;
1010 int16_t *dst = (int16_t *)(audioBuffer.i8);
1011 while(count--) {
1012 *dst++ = (int16_t)(*src++^0x80) << 8;
1013 }
Eric Laurent28ad42b2009-08-04 10:42:26 -07001014 } else {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001015 toWrite = audioBuffer.size;
1016 memcpy(audioBuffer.i8, src, toWrite);
1017 src += toWrite;
1018 }
1019 userSize -= toWrite;
1020 written += toWrite;
1021
1022 releaseBuffer(&audioBuffer);
Eric Laurent913af0b2011-03-17 09:36:51 -07001023 } while (userSize >= frameSz);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001024
1025 return written;
1026}
1027
1028// -------------------------------------------------------------------------
1029
1030bool AudioTrack::processAudioBuffer(const sp<AudioTrackThread>& thread)
1031{
1032 Buffer audioBuffer;
1033 uint32_t frames;
1034 size_t writtenSize;
1035
Eric Laurent421ddc02011-03-07 14:52:59 -08001036 mLock.lock();
1037 // acquire a strong reference on the IMemory and IAudioTrack so that they cannot be destroyed
1038 // while we are accessing the cblk
1039 sp <IAudioTrack> audioTrack = mAudioTrack;
1040 sp <IMemory> iMem = mCblkMemory;
1041 audio_track_cblk_t* cblk = mCblk;
1042 mLock.unlock();
1043
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001044 // Manage underrun callback
Eric Laurentae29b762011-03-28 18:37:07 -07001045 if (mActive && (cblk->framesAvailable() == cblk->frameCount)) {
Steve Block71f2cf12011-10-20 11:56:00 +01001046 ALOGV("Underrun user: %x, server: %x, flags %04x", cblk->user, cblk->server, cblk->flags);
Eric Laurentae29b762011-03-28 18:37:07 -07001047 if (!(android_atomic_or(CBLK_UNDERRUN_ON, &cblk->flags) & CBLK_UNDERRUN_MSK)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001048 mCbf(EVENT_UNDERRUN, mUserData, 0);
Eric Laurent421ddc02011-03-07 14:52:59 -08001049 if (cblk->server == cblk->frameCount) {
Eric Laurenta553c252009-07-17 12:17:14 -07001050 mCbf(EVENT_BUFFER_END, mUserData, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001051 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001052 if (mSharedBuffer != 0) return false;
1053 }
1054 }
Eric Laurenta553c252009-07-17 12:17:14 -07001055
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001056 // Manage loop end callback
Eric Laurent421ddc02011-03-07 14:52:59 -08001057 while (mLoopCount > cblk->loopCount) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001058 int loopCount = -1;
1059 mLoopCount--;
1060 if (mLoopCount >= 0) loopCount = mLoopCount;
1061
1062 mCbf(EVENT_LOOP_END, mUserData, (void *)&loopCount);
1063 }
1064
1065 // Manage marker callback
Jean-Michel Trivi4a5c1a72009-03-24 18:11:07 -07001066 if (!mMarkerReached && (mMarkerPosition > 0)) {
Eric Laurent421ddc02011-03-07 14:52:59 -08001067 if (cblk->server >= mMarkerPosition) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001068 mCbf(EVENT_MARKER, mUserData, (void *)&mMarkerPosition);
Jean-Michel Trivi4a5c1a72009-03-24 18:11:07 -07001069 mMarkerReached = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001070 }
1071 }
1072
1073 // Manage new position callback
Eric Laurenta553c252009-07-17 12:17:14 -07001074 if (mUpdatePeriod > 0) {
Eric Laurent421ddc02011-03-07 14:52:59 -08001075 while (cblk->server >= mNewPosition) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001076 mCbf(EVENT_NEW_POS, mUserData, (void *)&mNewPosition);
1077 mNewPosition += mUpdatePeriod;
1078 }
1079 }
1080
1081 // If Shared buffer is used, no data is requested from client.
1082 if (mSharedBuffer != 0) {
1083 frames = 0;
1084 } else {
1085 frames = mRemainingFrames;
1086 }
1087
Eric Laurentf1d360a2011-09-07 11:13:23 -07001088 int32_t waitCount = -1;
1089 if (mUpdatePeriod || (!mMarkerReached && mMarkerPosition) || mLoopCount) {
1090 waitCount = 1;
1091 }
1092
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001093 do {
1094
1095 audioBuffer.frameCount = frames;
Eric Laurenta553c252009-07-17 12:17:14 -07001096
1097 // Calling obtainBuffer() with a wait count of 1
1098 // limits wait time to WAIT_PERIOD_MS. This prevents from being
1099 // stuck here not being able to handle timed events (position, markers, loops).
Eric Laurentf1d360a2011-09-07 11:13:23 -07001100 status_t err = obtainBuffer(&audioBuffer, waitCount);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001101 if (err < NO_ERROR) {
1102 if (err != TIMED_OUT) {
Eric Laurentef028272009-04-21 07:56:33 -07001103 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 -08001104 return false;
1105 }
1106 break;
1107 }
1108 if (err == status_t(STOPPED)) return false;
1109
1110 // Divide buffer size by 2 to take into account the expansion
1111 // due to 8 to 16 bit conversion: the callback must fill only half
1112 // of the destination buffer
Dima Zavin24fc2fb2011-04-19 22:30:36 -07001113 if (mFormat == AUDIO_FORMAT_PCM_8_BIT && !(mFlags & AUDIO_POLICY_OUTPUT_FLAG_DIRECT)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001114 audioBuffer.size >>= 1;
1115 }
1116
1117 size_t reqSize = audioBuffer.size;
1118 mCbf(EVENT_MORE_DATA, mUserData, &audioBuffer);
1119 writtenSize = audioBuffer.size;
1120
1121 // Sanity check on returned size
The Android Open Source Project4df24232009-03-05 14:34:35 -08001122 if (ssize_t(writtenSize) <= 0) {
1123 // The callback is done filling buffers
1124 // Keep this thread going to handle timed events and
1125 // still try to get more data in intervals of WAIT_PERIOD_MS
1126 // but don't just loop and block the CPU, so wait
1127 usleep(WAIT_PERIOD_MS*1000);
1128 break;
1129 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001130 if (writtenSize > reqSize) writtenSize = reqSize;
1131
Dima Zavin24fc2fb2011-04-19 22:30:36 -07001132 if (mFormat == AUDIO_FORMAT_PCM_8_BIT && !(mFlags & AUDIO_POLICY_OUTPUT_FLAG_DIRECT)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001133 // 8 to 16 bit conversion
1134 const int8_t *src = audioBuffer.i8 + writtenSize-1;
1135 int count = writtenSize;
1136 int16_t *dst = audioBuffer.i16 + writtenSize-1;
1137 while(count--) {
1138 *dst-- = (int16_t)(*src--^0x80) << 8;
1139 }
1140 writtenSize <<= 1;
1141 }
1142
1143 audioBuffer.size = writtenSize;
Eric Laurenta553c252009-07-17 12:17:14 -07001144 // NOTE: mCblk->frameSize is not equal to AudioTrack::frameSize() for
1145 // 8 bit PCM data: in this case, mCblk->frameSize is based on a sampel size of
1146 // 16 bit.
1147 audioBuffer.frameCount = writtenSize/mCblk->frameSize;
1148
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001149 frames -= audioBuffer.frameCount;
1150
1151 releaseBuffer(&audioBuffer);
1152 }
1153 while (frames);
1154
1155 if (frames == 0) {
Eric Laurenteb8f850d2010-05-14 03:26:45 -07001156 mRemainingFrames = mNotificationFramesAct;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001157 } else {
1158 mRemainingFrames = frames;
1159 }
1160 return true;
1161}
1162
Eric Laurent421ddc02011-03-07 14:52:59 -08001163// must be called with mLock and cblk.lock held. Callers must also hold strong references on
1164// the IAudioTrack and IMemory in case they are recreated here.
1165// If the IAudioTrack is successfully restored, the cblk pointer is updated
1166status_t AudioTrack::restoreTrack_l(audio_track_cblk_t*& cblk, bool fromStart)
1167{
1168 status_t result;
1169
Eric Laurentae29b762011-03-28 18:37:07 -07001170 if (!(android_atomic_or(CBLK_RESTORING_ON, &cblk->flags) & CBLK_RESTORING_MSK)) {
Eric Laurent7e8626f2011-09-13 15:04:17 -07001171 LOGW("dead IAudioTrack, creating a new one from %s TID %d",
1172 fromStart ? "start()" : "obtainBuffer()", gettid());
Eric Laurent421ddc02011-03-07 14:52:59 -08001173
Eric Laurent421ddc02011-03-07 14:52:59 -08001174 // signal old cblk condition so that other threads waiting for available buffers stop
1175 // waiting now
1176 cblk->cv.broadcast();
1177 cblk->lock.unlock();
1178
Eric Laurent05ce0942011-08-30 10:18:54 -07001179 // refresh the audio configuration cache in this process to make sure we get new
1180 // output parameters in getOutput_l() and createTrack_l()
1181 AudioSystem::clearAudioConfigCache();
1182
Eric Laurent421ddc02011-03-07 14:52:59 -08001183 // if the new IAudioTrack is created, createTrack_l() will modify the
1184 // following member variables: mAudioTrack, mCblkMemory and mCblk.
1185 // It will also delete the strong references on previous IAudioTrack and IMemory
1186 result = createTrack_l(mStreamType,
1187 cblk->sampleRate,
1188 mFormat,
Jean-Michel Trivi54392232011-05-24 15:53:33 -07001189 mChannelMask,
Eric Laurent421ddc02011-03-07 14:52:59 -08001190 mFrameCount,
1191 mFlags,
1192 mSharedBuffer,
1193 getOutput_l(),
1194 false);
1195
1196 if (result == NO_ERROR) {
Eric Laurentb0808f92011-09-06 12:36:15 -07001197 uint32_t user = cblk->user;
1198 uint32_t server = cblk->server;
Eric Laurent6667ac32011-03-21 11:49:00 -07001199 // restore write index and set other indexes to reflect empty buffer status
Eric Laurentb0808f92011-09-06 12:36:15 -07001200 mCblk->user = user;
1201 mCblk->server = user;
1202 mCblk->userBase = user;
1203 mCblk->serverBase = user;
Eric Laurent6667ac32011-03-21 11:49:00 -07001204 // restore loop: this is not guaranteed to succeed if new frame count is not
1205 // compatible with loop length
1206 setLoop_l(cblk->loopStart, cblk->loopEnd, cblk->loopCount);
Eric Laurent421ddc02011-03-07 14:52:59 -08001207 if (!fromStart) {
1208 mCblk->bufferTimeoutMs = MAX_RUN_TIMEOUT_MS;
Eric Laurentb0808f92011-09-06 12:36:15 -07001209 // Make sure that a client relying on callback events indicating underrun or
1210 // the actual amount of audio frames played (e.g SoundPool) receives them.
1211 if (mSharedBuffer == 0) {
1212 uint32_t frames = 0;
1213 if (user > server) {
1214 frames = ((user - server) > mCblk->frameCount) ?
1215 mCblk->frameCount : (user - server);
1216 memset(mCblk->buffers, 0, frames * mCblk->frameSize);
1217 }
1218 // restart playback even if buffer is not completely filled.
1219 android_atomic_or(CBLK_FORCEREADY_ON, &mCblk->flags);
1220 // stepUser() clears CBLK_UNDERRUN_ON flag enabling underrun callbacks to
1221 // the client
1222 mCblk->stepUser(frames);
1223 }
Eric Laurent421ddc02011-03-07 14:52:59 -08001224 }
Eric Laurent6667ac32011-03-21 11:49:00 -07001225 if (mActive) {
1226 result = mAudioTrack->start();
Eric Laurent7e8626f2011-09-13 15:04:17 -07001227 LOGW_IF(result != NO_ERROR, "restoreTrack_l() start() failed status %d", result);
Eric Laurent6667ac32011-03-21 11:49:00 -07001228 }
Eric Laurent421ddc02011-03-07 14:52:59 -08001229 if (fromStart && result == NO_ERROR) {
1230 mNewPosition = mCblk->server + mUpdatePeriod;
1231 }
1232 }
1233 if (result != NO_ERROR) {
Eric Laurent7e8626f2011-09-13 15:04:17 -07001234 android_atomic_and(~CBLK_RESTORING_ON, &cblk->flags);
1235 LOGW_IF(result != NO_ERROR, "restoreTrack_l() failed status %d", result);
Eric Laurent421ddc02011-03-07 14:52:59 -08001236 }
Eric Laurent7e8626f2011-09-13 15:04:17 -07001237 mRestoreStatus = result;
Eric Laurent421ddc02011-03-07 14:52:59 -08001238 // signal old cblk condition for other threads waiting for restore completion
Eric Laurentae29b762011-03-28 18:37:07 -07001239 android_atomic_or(CBLK_RESTORED_ON, &cblk->flags);
Eric Laurent421ddc02011-03-07 14:52:59 -08001240 cblk->cv.broadcast();
Eric Laurent421ddc02011-03-07 14:52:59 -08001241 } else {
1242 if (!(cblk->flags & CBLK_RESTORED_MSK)) {
Eric Laurent7e8626f2011-09-13 15:04:17 -07001243 LOGW("dead IAudioTrack, waiting for a new one TID %d", gettid());
Eric Laurent421ddc02011-03-07 14:52:59 -08001244 mLock.unlock();
1245 result = cblk->cv.waitRelative(cblk->lock, milliseconds(RESTORE_TIMEOUT_MS));
Eric Laurent7e8626f2011-09-13 15:04:17 -07001246 if (result == NO_ERROR) {
1247 result = mRestoreStatus;
1248 }
Eric Laurent421ddc02011-03-07 14:52:59 -08001249 cblk->lock.unlock();
1250 mLock.lock();
1251 } else {
Eric Laurent7e8626f2011-09-13 15:04:17 -07001252 LOGW("dead IAudioTrack, already restored TID %d", gettid());
1253 result = mRestoreStatus;
Eric Laurent421ddc02011-03-07 14:52:59 -08001254 cblk->lock.unlock();
1255 }
Eric Laurent421ddc02011-03-07 14:52:59 -08001256 }
Steve Block71f2cf12011-10-20 11:56:00 +01001257 ALOGV("restoreTrack_l() status %d mActive %d cblk %p, old cblk %p flags %08x old flags %08x",
Eric Laurent421ddc02011-03-07 14:52:59 -08001258 result, mActive, mCblk, cblk, mCblk->flags, cblk->flags);
1259
1260 if (result == NO_ERROR) {
1261 // from now on we switch to the newly created cblk
1262 cblk = mCblk;
1263 }
1264 cblk->lock.lock();
1265
Eric Laurent7e8626f2011-09-13 15:04:17 -07001266 LOGW_IF(result != NO_ERROR, "restoreTrack_l() error %d TID %d", result, gettid());
Eric Laurent421ddc02011-03-07 14:52:59 -08001267
1268 return result;
1269}
1270
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001271status_t AudioTrack::dump(int fd, const Vector<String16>& args) const
1272{
1273
1274 const size_t SIZE = 256;
1275 char buffer[SIZE];
1276 String8 result;
1277
1278 result.append(" AudioTrack::dump\n");
1279 snprintf(buffer, 255, " stream type(%d), left - right volume(%f, %f)\n", mStreamType, mVolume[0], mVolume[1]);
1280 result.append(buffer);
Eric Laurenteb8f850d2010-05-14 03:26:45 -07001281 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 -08001282 result.append(buffer);
Eric Laurent88e209d2009-07-07 07:10:45 -07001283 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 -08001284 result.append(buffer);
1285 snprintf(buffer, 255, " active(%d), latency (%d)\n", mActive, mLatency);
1286 result.append(buffer);
1287 ::write(fd, result.string(), result.size());
1288 return NO_ERROR;
1289}
1290
1291// =========================================================================
1292
1293AudioTrack::AudioTrackThread::AudioTrackThread(AudioTrack& receiver, bool bCanCallJava)
1294 : Thread(bCanCallJava), mReceiver(receiver)
1295{
1296}
1297
1298bool AudioTrack::AudioTrackThread::threadLoop()
1299{
1300 return mReceiver.processAudioBuffer(this);
1301}
1302
1303status_t AudioTrack::AudioTrackThread::readyToRun()
1304{
1305 return NO_ERROR;
1306}
1307
1308void AudioTrack::AudioTrackThread::onFirstRef()
1309{
1310}
1311
1312// =========================================================================
1313
Eric Laurentae29b762011-03-28 18:37:07 -07001314
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001315audio_track_cblk_t::audio_track_cblk_t()
Mathias Agopiana729f972010-03-19 16:14:13 -07001316 : lock(Mutex::SHARED), cv(Condition::SHARED), user(0), server(0),
1317 userBase(0), serverBase(0), buffers(0), frameCount(0),
1318 loopStart(UINT_MAX), loopEnd(UINT_MAX), loopCount(0), volumeLR(0),
Eric Laurentae29b762011-03-28 18:37:07 -07001319 sendLevel(0), flags(0)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001320{
1321}
1322
1323uint32_t audio_track_cblk_t::stepUser(uint32_t frameCount)
1324{
Glenn Kastendb298a42011-12-13 11:45:07 -08001325 uint32_t u = user;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001326
1327 u += frameCount;
1328 // Ensure that user is never ahead of server for AudioRecord
Eric Laurenteb8f850d2010-05-14 03:26:45 -07001329 if (flags & CBLK_DIRECTION_MSK) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001330 // If stepServer() has been called once, switch to normal obtainBuffer() timeout period
1331 if (bufferTimeoutMs == MAX_STARTUP_TIMEOUT_MS-1) {
1332 bufferTimeoutMs = MAX_RUN_TIMEOUT_MS;
1333 }
Glenn Kastendb298a42011-12-13 11:45:07 -08001334 } else if (u > server) {
1335 LOGW("stepServer occurred after track reset");
1336 u = server;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001337 }
1338
1339 if (u >= userBase + this->frameCount) {
1340 userBase += this->frameCount;
1341 }
1342
Glenn Kastendb298a42011-12-13 11:45:07 -08001343 user = u;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001344
1345 // Clear flow control error condition as new data has been written/read to/from buffer.
Eric Laurent913af0b2011-03-17 09:36:51 -07001346 if (flags & CBLK_UNDERRUN_MSK) {
Eric Laurentae29b762011-03-28 18:37:07 -07001347 android_atomic_and(~CBLK_UNDERRUN_MSK, &flags);
Eric Laurent913af0b2011-03-17 09:36:51 -07001348 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001349
1350 return u;
1351}
1352
1353bool audio_track_cblk_t::stepServer(uint32_t frameCount)
1354{
Eric Laurentae29b762011-03-28 18:37:07 -07001355 if (!tryLock()) {
1356 LOGW("stepServer() could not lock cblk");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001357 return false;
1358 }
1359
Glenn Kastendb298a42011-12-13 11:45:07 -08001360 uint32_t s = server;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001361
1362 s += frameCount;
Eric Laurenteb8f850d2010-05-14 03:26:45 -07001363 if (flags & CBLK_DIRECTION_MSK) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001364 // Mark that we have read the first buffer so that next time stepUser() is called
1365 // we switch to normal obtainBuffer() timeout period
1366 if (bufferTimeoutMs == MAX_STARTUP_TIMEOUT_MS) {
Eric Laurentbda74692009-11-04 08:27:26 -08001367 bufferTimeoutMs = MAX_STARTUP_TIMEOUT_MS - 1;
Eric Laurenta553c252009-07-17 12:17:14 -07001368 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001369 // It is possible that we receive a flush()
1370 // while the mixer is processing a block: in this case,
1371 // stepServer() is called After the flush() has reset u & s and
1372 // we have s > u
Glenn Kastendb298a42011-12-13 11:45:07 -08001373 if (s > user) {
1374 LOGW("stepServer occurred after track reset");
1375 s = user;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001376 }
1377 }
1378
1379 if (s >= loopEnd) {
1380 LOGW_IF(s > loopEnd, "stepServer: s %u > loopEnd %u", s, loopEnd);
1381 s = loopStart;
1382 if (--loopCount == 0) {
1383 loopEnd = UINT_MAX;
1384 loopStart = UINT_MAX;
1385 }
1386 }
1387 if (s >= serverBase + this->frameCount) {
1388 serverBase += this->frameCount;
1389 }
1390
Glenn Kastendb298a42011-12-13 11:45:07 -08001391 server = s;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001392
Eric Laurent421ddc02011-03-07 14:52:59 -08001393 if (!(flags & CBLK_INVALID_MSK)) {
1394 cv.signal();
1395 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001396 lock.unlock();
1397 return true;
1398}
1399
1400void* audio_track_cblk_t::buffer(uint32_t offset) const
1401{
Glenn Kastendb298a42011-12-13 11:45:07 -08001402 return (int8_t *)buffers + (offset - userBase) * frameSize;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001403}
1404
1405uint32_t audio_track_cblk_t::framesAvailable()
1406{
1407 Mutex::Autolock _l(lock);
1408 return framesAvailable_l();
1409}
1410
1411uint32_t audio_track_cblk_t::framesAvailable_l()
1412{
Glenn Kastendb298a42011-12-13 11:45:07 -08001413 uint32_t u = user;
1414 uint32_t s = server;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001415
Eric Laurenteb8f850d2010-05-14 03:26:45 -07001416 if (flags & CBLK_DIRECTION_MSK) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001417 uint32_t limit = (s < loopStart) ? s : loopStart;
1418 return limit + frameCount - u;
1419 } else {
1420 return frameCount + u - s;
1421 }
1422}
1423
1424uint32_t audio_track_cblk_t::framesReady()
1425{
Glenn Kastendb298a42011-12-13 11:45:07 -08001426 uint32_t u = user;
1427 uint32_t s = server;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001428
Eric Laurenteb8f850d2010-05-14 03:26:45 -07001429 if (flags & CBLK_DIRECTION_MSK) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001430 if (u < loopEnd) {
1431 return u - s;
1432 } else {
Eric Laurentae29b762011-03-28 18:37:07 -07001433 // do not block on mutex shared with client on AudioFlinger side
1434 if (!tryLock()) {
1435 LOGW("framesReady() could not lock cblk");
1436 return 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001437 }
Eric Laurentae29b762011-03-28 18:37:07 -07001438 uint32_t frames = UINT_MAX;
1439 if (loopCount >= 0) {
1440 frames = (loopEnd - loopStart)*loopCount + u - s;
1441 }
1442 lock.unlock();
1443 return frames;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001444 }
1445 } else {
1446 return s - u;
1447 }
1448}
1449
Eric Laurentae29b762011-03-28 18:37:07 -07001450bool audio_track_cblk_t::tryLock()
1451{
1452 // the code below simulates lock-with-timeout
1453 // we MUST do this to protect the AudioFlinger server
1454 // as this lock is shared with the client.
1455 status_t err;
1456
1457 err = lock.tryLock();
1458 if (err == -EBUSY) { // just wait a bit
1459 usleep(1000);
1460 err = lock.tryLock();
1461 }
1462 if (err != NO_ERROR) {
1463 // probably, the client just died.
1464 return false;
1465 }
1466 return true;
1467}
1468
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001469// -------------------------------------------------------------------------
1470
1471}; // namespace android
1472