blob: 4b61131029648e561381680db648acfacec5316e [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>
38#include <cutils/atomic.h>
39
40#define LIKELY( exp ) (__builtin_expect( (exp) != 0, true ))
41#define UNLIKELY( exp ) (__builtin_expect( (exp) != 0, false ))
42
43namespace android {
44
45// ---------------------------------------------------------------------------
46
47AudioTrack::AudioTrack()
48 : mStatus(NO_INIT)
49{
50}
51
52AudioTrack::AudioTrack(
53 int streamType,
54 uint32_t sampleRate,
55 int format,
Eric Laurenta553c252009-07-17 12:17:14 -070056 int channels,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080057 int frameCount,
58 uint32_t flags,
59 callback_t cbf,
60 void* user,
Eric Laurent65b65452010-06-01 23:49:17 -070061 int notificationFrames,
62 int sessionId)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080063 : mStatus(NO_INIT)
64{
Eric Laurenta553c252009-07-17 12:17:14 -070065 mStatus = set(streamType, sampleRate, format, channels,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080066 frameCount, flags, cbf, user, notificationFrames, 0);
67}
68
69AudioTrack::AudioTrack(
70 int streamType,
71 uint32_t sampleRate,
72 int format,
Eric Laurenta553c252009-07-17 12:17:14 -070073 int channels,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080074 const sp<IMemory>& sharedBuffer,
75 uint32_t flags,
76 callback_t cbf,
77 void* user,
Eric Laurent65b65452010-06-01 23:49:17 -070078 int notificationFrames,
79 int sessionId)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080080 : mStatus(NO_INIT)
81{
Eric Laurenta553c252009-07-17 12:17:14 -070082 mStatus = set(streamType, sampleRate, format, channels,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080083 0, flags, cbf, user, notificationFrames, sharedBuffer);
84}
85
86AudioTrack::~AudioTrack()
87{
88 LOGV_IF(mSharedBuffer != 0, "Destructor sharedBuffer: %p", mSharedBuffer->pointer());
89
90 if (mStatus == NO_ERROR) {
91 // Make sure that callback function exits in the case where
92 // it is looping on buffer full condition in obtainBuffer().
93 // Otherwise the callback thread will never exit.
94 stop();
95 if (mAudioTrackThread != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080096 mAudioTrackThread->requestExitAndWait();
97 mAudioTrackThread.clear();
98 }
99 mAudioTrack.clear();
100 IPCThreadState::self()->flushCommands();
101 }
102}
103
104status_t AudioTrack::set(
105 int streamType,
106 uint32_t sampleRate,
107 int format,
Eric Laurenta553c252009-07-17 12:17:14 -0700108 int channels,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800109 int frameCount,
110 uint32_t flags,
111 callback_t cbf,
112 void* user,
113 int notificationFrames,
114 const sp<IMemory>& sharedBuffer,
Eric Laurent65b65452010-06-01 23:49:17 -0700115 bool threadCanCallJava,
116 int sessionId)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800117{
118
119 LOGV_IF(sharedBuffer != 0, "sharedBuffer: %p, size: %d", sharedBuffer->pointer(), sharedBuffer->size());
120
Eric Laurentef028272009-04-21 07:56:33 -0700121 if (mAudioTrack != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800122 LOGE("Track already in use");
123 return INVALID_OPERATION;
124 }
125
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800126 int afSampleRate;
127 if (AudioSystem::getOutputSamplingRate(&afSampleRate, streamType) != NO_ERROR) {
128 return NO_INIT;
129 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800130 uint32_t afLatency;
131 if (AudioSystem::getOutputLatency(&afLatency, streamType) != NO_ERROR) {
132 return NO_INIT;
133 }
134
135 // handle default values first.
136 if (streamType == AudioSystem::DEFAULT) {
137 streamType = AudioSystem::MUSIC;
138 }
139 if (sampleRate == 0) {
140 sampleRate = afSampleRate;
141 }
142 // these below should probably come from the audioFlinger too...
143 if (format == 0) {
144 format = AudioSystem::PCM_16_BIT;
145 }
Eric Laurenta553c252009-07-17 12:17:14 -0700146 if (channels == 0) {
147 channels = AudioSystem::CHANNEL_OUT_STEREO;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800148 }
149
150 // validate parameters
Eric Laurenta553c252009-07-17 12:17:14 -0700151 if (!AudioSystem::isValidFormat(format)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800152 LOGE("Invalid format");
153 return BAD_VALUE;
154 }
Eric Laurenta553c252009-07-17 12:17:14 -0700155
156 // force direct flag if format is not linear PCM
157 if (!AudioSystem::isLinearPCM(format)) {
158 flags |= AudioSystem::OUTPUT_FLAG_DIRECT;
159 }
160
161 if (!AudioSystem::isOutputChannel(channels)) {
162 LOGE("Invalid channel mask");
163 return BAD_VALUE;
164 }
165 uint32_t channelCount = AudioSystem::popCount(channels);
166
167 audio_io_handle_t output = AudioSystem::getOutput((AudioSystem::stream_type)streamType,
168 sampleRate, format, channels, (AudioSystem::output_flags)flags);
169
170 if (output == 0) {
171 LOGE("Could not get audio output for stream type %d", streamType);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800172 return BAD_VALUE;
173 }
174
Eric Laurentbda74692009-11-04 08:27:26 -0800175 mVolume[LEFT] = 1.0f;
176 mVolume[RIGHT] = 1.0f;
Eric Laurent65b65452010-06-01 23:49:17 -0700177 mSendLevel = 0;
Eric Laurenteb8f850d2010-05-14 03:26:45 -0700178 mFrameCount = frameCount;
179 mNotificationFramesReq = notificationFrames;
Eric Laurent65b65452010-06-01 23:49:17 -0700180 mSessionId = sessionId;
181
Eric Laurentbda74692009-11-04 08:27:26 -0800182 // create the IAudioTrack
183 status_t status = createTrack(streamType, sampleRate, format, channelCount,
Eric Laurenteb8f850d2010-05-14 03:26:45 -0700184 frameCount, flags, sharedBuffer, output, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800185
Eric Laurentbda74692009-11-04 08:27:26 -0800186 if (status != NO_ERROR) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800187 return status;
188 }
Eric Laurentbda74692009-11-04 08:27:26 -0800189
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800190 if (cbf != 0) {
191 mAudioTrackThread = new AudioTrackThread(*this, threadCanCallJava);
192 if (mAudioTrackThread == 0) {
193 LOGE("Could not create callback thread");
194 return NO_INIT;
195 }
196 }
197
198 mStatus = NO_ERROR;
199
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800200 mStreamType = streamType;
201 mFormat = format;
Eric Laurenta553c252009-07-17 12:17:14 -0700202 mChannels = channels;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800203 mChannelCount = channelCount;
204 mSharedBuffer = sharedBuffer;
205 mMuted = false;
206 mActive = 0;
207 mCbf = cbf;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800208 mUserData = user;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800209 mLoopCount = 0;
210 mMarkerPosition = 0;
Jean-Michel Trivi4a5c1a72009-03-24 18:11:07 -0700211 mMarkerReached = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800212 mNewPosition = 0;
213 mUpdatePeriod = 0;
Eric Laurenta553c252009-07-17 12:17:14 -0700214 mFlags = flags;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800215
216 return NO_ERROR;
217}
218
219status_t AudioTrack::initCheck() const
220{
221 return mStatus;
222}
223
224// -------------------------------------------------------------------------
225
226uint32_t AudioTrack::latency() const
227{
228 return mLatency;
229}
230
231int AudioTrack::streamType() const
232{
233 return mStreamType;
234}
235
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800236int AudioTrack::format() const
237{
238 return mFormat;
239}
240
241int AudioTrack::channelCount() const
242{
243 return mChannelCount;
244}
245
246uint32_t AudioTrack::frameCount() const
247{
Eric Laurenteb8f850d2010-05-14 03:26:45 -0700248 return mCblk->frameCount;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800249}
250
251int AudioTrack::frameSize() const
252{
Eric Laurenta553c252009-07-17 12:17:14 -0700253 if (AudioSystem::isLinearPCM(mFormat)) {
254 return channelCount()*((format() == AudioSystem::PCM_8_BIT) ? sizeof(uint8_t) : sizeof(int16_t));
255 } else {
256 return sizeof(uint8_t);
257 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800258}
259
260sp<IMemory>& AudioTrack::sharedBuffer()
261{
262 return mSharedBuffer;
263}
264
265// -------------------------------------------------------------------------
266
267void AudioTrack::start()
268{
269 sp<AudioTrackThread> t = mAudioTrackThread;
Eric Laurenteb8f850d2010-05-14 03:26:45 -0700270 status_t status;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800271
272 LOGV("start %p", this);
273 if (t != 0) {
274 if (t->exitPending()) {
275 if (t->requestExitAndWait() == WOULD_BLOCK) {
276 LOGE("AudioTrack::start called from thread");
277 return;
278 }
279 }
280 t->mLock.lock();
281 }
282
283 if (android_atomic_or(1, &mActive) == 0) {
Eric Laurent059b4be2009-11-09 23:32:22 -0800284 mNewPosition = mCblk->server + mUpdatePeriod;
285 mCblk->bufferTimeoutMs = MAX_STARTUP_TIMEOUT_MS;
286 mCblk->waitTimeMs = 0;
287 if (t != 0) {
288 t->run("AudioTrackThread", THREAD_PRIORITY_AUDIO_CLIENT);
289 } else {
290 setpriority(PRIO_PROCESS, 0, THREAD_PRIORITY_AUDIO_CLIENT);
291 }
292
Eric Laurenteb8f850d2010-05-14 03:26:45 -0700293 if (mCblk->flags & CBLK_INVALID_MSK) {
294 LOGW("start() track %p invalidated, creating a new one", this);
295 // no need to clear the invalid flag as this cblk will not be used anymore
296 // force new track creation
297 status = DEAD_OBJECT;
298 } else {
299 status = mAudioTrack->start();
300 }
Eric Laurentbda74692009-11-04 08:27:26 -0800301 if (status == DEAD_OBJECT) {
302 LOGV("start() dead IAudioTrack: creating a new one");
303 status = createTrack(mStreamType, mCblk->sampleRate, mFormat, mChannelCount,
Eric Laurenteb8f850d2010-05-14 03:26:45 -0700304 mFrameCount, mFlags, mSharedBuffer, getOutput(), false);
Eric Laurent49f02be2009-11-19 09:00:56 -0800305 if (status == NO_ERROR) {
306 status = mAudioTrack->start();
307 if (status == NO_ERROR) {
308 mNewPosition = mCblk->server + mUpdatePeriod;
309 }
310 }
Eric Laurent059b4be2009-11-09 23:32:22 -0800311 }
312 if (status != NO_ERROR) {
Eric Laurentbda74692009-11-04 08:27:26 -0800313 LOGV("start() failed");
314 android_atomic_and(~1, &mActive);
Eric Laurent059b4be2009-11-09 23:32:22 -0800315 if (t != 0) {
316 t->requestExit();
317 } else {
318 setpriority(PRIO_PROCESS, 0, ANDROID_PRIORITY_NORMAL);
319 }
Eric Laurentbda74692009-11-04 08:27:26 -0800320 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800321 }
322
323 if (t != 0) {
324 t->mLock.unlock();
325 }
326}
327
328void AudioTrack::stop()
329{
330 sp<AudioTrackThread> t = mAudioTrackThread;
331
332 LOGV("stop %p", this);
333 if (t != 0) {
334 t->mLock.lock();
335 }
336
337 if (android_atomic_and(~1, &mActive) == 1) {
Eric Laurentef028272009-04-21 07:56:33 -0700338 mCblk->cv.signal();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800339 mAudioTrack->stop();
340 // Cancel loops (If we are in the middle of a loop, playback
341 // would not stop until loopCount reaches 0).
342 setLoop(0, 0, 0);
Jean-Michel Trivi4a5c1a72009-03-24 18:11:07 -0700343 // the playback head position will reset to 0, so if a marker is set, we need
344 // to activate it again
345 mMarkerReached = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800346 // Force flush if a shared buffer is used otherwise audioflinger
347 // will not stop before end of buffer is reached.
348 if (mSharedBuffer != 0) {
349 flush();
350 }
351 if (t != 0) {
352 t->requestExit();
353 } else {
354 setpriority(PRIO_PROCESS, 0, ANDROID_PRIORITY_NORMAL);
355 }
356 }
357
358 if (t != 0) {
359 t->mLock.unlock();
360 }
361}
362
363bool AudioTrack::stopped() const
364{
365 return !mActive;
366}
367
368void AudioTrack::flush()
369{
370 LOGV("flush");
Eric Laurenta553c252009-07-17 12:17:14 -0700371
Jean-Michel Trivi4a5c1a72009-03-24 18:11:07 -0700372 // clear playback marker and periodic update counter
373 mMarkerPosition = 0;
374 mMarkerReached = false;
375 mUpdatePeriod = 0;
Eric Laurenta553c252009-07-17 12:17:14 -0700376
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800377
378 if (!mActive) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800379 mAudioTrack->flush();
380 // Release AudioTrack callback thread in case it was waiting for new buffers
381 // in AudioTrack::obtainBuffer()
382 mCblk->cv.signal();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800383 }
384}
385
386void AudioTrack::pause()
387{
388 LOGV("pause");
389 if (android_atomic_and(~1, &mActive) == 1) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800390 mAudioTrack->pause();
391 }
392}
393
394void AudioTrack::mute(bool e)
395{
396 mAudioTrack->mute(e);
397 mMuted = e;
398}
399
400bool AudioTrack::muted() const
401{
402 return mMuted;
403}
404
Eric Laurent65b65452010-06-01 23:49:17 -0700405status_t AudioTrack::setVolume(float left, float right)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800406{
Eric Laurent65b65452010-06-01 23:49:17 -0700407 if (left > 1.0f || right > 1.0f) {
408 return BAD_VALUE;
409 }
410
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800411 mVolume[LEFT] = left;
412 mVolume[RIGHT] = right;
413
414 // write must be atomic
Eric Laurent65b65452010-06-01 23:49:17 -0700415 mCblk->volumeLR = (uint32_t(uint16_t(right * 0x1000)) << 16) | uint16_t(left * 0x1000);
416
417 return NO_ERROR;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800418}
419
420void AudioTrack::getVolume(float* left, float* right)
421{
Eric Laurent65b65452010-06-01 23:49:17 -0700422 if (left != NULL) {
423 *left = mVolume[LEFT];
424 }
425 if (right != NULL) {
426 *right = mVolume[RIGHT];
427 }
428}
429
430status_t AudioTrack::setSendLevel(float level)
431{
432 if (level > 1.0f) {
433 return BAD_VALUE;
434 }
435
436 mSendLevel = level;
437
438 mCblk->sendLevel = uint16_t(level * 0x1000);
439
440 return NO_ERROR;
441}
442
443void AudioTrack::getSendLevel(float* level)
444{
445 if (level != NULL) {
446 *level = mSendLevel;
447 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800448}
449
Eric Laurent88e209d2009-07-07 07:10:45 -0700450status_t AudioTrack::setSampleRate(int rate)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800451{
452 int afSamplingRate;
453
454 if (AudioSystem::getOutputSamplingRate(&afSamplingRate, mStreamType) != NO_ERROR) {
Eric Laurent88e209d2009-07-07 07:10:45 -0700455 return NO_INIT;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800456 }
457 // Resampler implementation limits input sampling rate to 2 x output sampling rate.
Eric Laurent88e209d2009-07-07 07:10:45 -0700458 if (rate <= 0 || rate > afSamplingRate*2 ) return BAD_VALUE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800459
Eric Laurent88e209d2009-07-07 07:10:45 -0700460 mCblk->sampleRate = rate;
461 return NO_ERROR;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800462}
463
464uint32_t AudioTrack::getSampleRate()
465{
Eric Laurent88e209d2009-07-07 07:10:45 -0700466 return mCblk->sampleRate;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800467}
468
469status_t AudioTrack::setLoop(uint32_t loopStart, uint32_t loopEnd, int loopCount)
470{
471 audio_track_cblk_t* cblk = mCblk;
472
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800473 Mutex::Autolock _l(cblk->lock);
474
475 if (loopCount == 0) {
476 cblk->loopStart = UINT_MAX;
477 cblk->loopEnd = UINT_MAX;
478 cblk->loopCount = 0;
479 mLoopCount = 0;
480 return NO_ERROR;
481 }
482
483 if (loopStart >= loopEnd ||
Eric Laurenteb8f850d2010-05-14 03:26:45 -0700484 loopEnd - loopStart > cblk->frameCount) {
485 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 -0800486 return BAD_VALUE;
487 }
488
Eric Laurenteb8f850d2010-05-14 03:26:45 -0700489 if ((mSharedBuffer != 0) && (loopEnd > cblk->frameCount)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800490 LOGE("setLoop invalid value: loop markers beyond data: loopStart %d, loopEnd %d, framecount %d",
Eric Laurenteb8f850d2010-05-14 03:26:45 -0700491 loopStart, loopEnd, cblk->frameCount);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800492 return BAD_VALUE;
Eric Laurenta553c252009-07-17 12:17:14 -0700493 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800494
495 cblk->loopStart = loopStart;
496 cblk->loopEnd = loopEnd;
497 cblk->loopCount = loopCount;
498 mLoopCount = loopCount;
499
500 return NO_ERROR;
501}
502
503status_t AudioTrack::getLoop(uint32_t *loopStart, uint32_t *loopEnd, int *loopCount)
504{
505 if (loopStart != 0) {
506 *loopStart = mCblk->loopStart;
507 }
508 if (loopEnd != 0) {
509 *loopEnd = mCblk->loopEnd;
510 }
511 if (loopCount != 0) {
512 if (mCblk->loopCount < 0) {
513 *loopCount = -1;
514 } else {
515 *loopCount = mCblk->loopCount;
516 }
517 }
518
519 return NO_ERROR;
520}
521
522status_t AudioTrack::setMarkerPosition(uint32_t marker)
523{
524 if (mCbf == 0) return INVALID_OPERATION;
525
526 mMarkerPosition = marker;
Jean-Michel Trivi4a5c1a72009-03-24 18:11:07 -0700527 mMarkerReached = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800528
529 return NO_ERROR;
530}
531
532status_t AudioTrack::getMarkerPosition(uint32_t *marker)
533{
534 if (marker == 0) return BAD_VALUE;
535
536 *marker = mMarkerPosition;
537
538 return NO_ERROR;
539}
540
541status_t AudioTrack::setPositionUpdatePeriod(uint32_t updatePeriod)
542{
543 if (mCbf == 0) return INVALID_OPERATION;
544
545 uint32_t curPosition;
546 getPosition(&curPosition);
547 mNewPosition = curPosition + updatePeriod;
548 mUpdatePeriod = updatePeriod;
549
550 return NO_ERROR;
551}
552
553status_t AudioTrack::getPositionUpdatePeriod(uint32_t *updatePeriod)
554{
555 if (updatePeriod == 0) return BAD_VALUE;
556
557 *updatePeriod = mUpdatePeriod;
558
559 return NO_ERROR;
560}
561
562status_t AudioTrack::setPosition(uint32_t position)
563{
564 Mutex::Autolock _l(mCblk->lock);
565
566 if (!stopped()) return INVALID_OPERATION;
567
568 if (position > mCblk->user) return BAD_VALUE;
569
570 mCblk->server = position;
Eric Laurenteb8f850d2010-05-14 03:26:45 -0700571 mCblk->flags |= CBLK_FORCEREADY_ON;
Eric Laurenta553c252009-07-17 12:17:14 -0700572
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800573 return NO_ERROR;
574}
575
576status_t AudioTrack::getPosition(uint32_t *position)
577{
578 if (position == 0) return BAD_VALUE;
579
580 *position = mCblk->server;
581
582 return NO_ERROR;
583}
584
585status_t AudioTrack::reload()
586{
587 if (!stopped()) return INVALID_OPERATION;
Eric Laurenta553c252009-07-17 12:17:14 -0700588
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800589 flush();
590
Eric Laurenteb8f850d2010-05-14 03:26:45 -0700591 mCblk->stepUser(mCblk->frameCount);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800592
593 return NO_ERROR;
594}
595
Eric Laurenta553c252009-07-17 12:17:14 -0700596audio_io_handle_t AudioTrack::getOutput()
597{
598 return AudioSystem::getOutput((AudioSystem::stream_type)mStreamType,
599 mCblk->sampleRate, mFormat, mChannels, (AudioSystem::output_flags)mFlags);
600}
601
Eric Laurent65b65452010-06-01 23:49:17 -0700602int AudioTrack::getSessionId()
603{
604 return mSessionId;
605}
606
607status_t AudioTrack::attachAuxEffect(int effectId)
608{
609 return mAudioTrack->attachAuxEffect(effectId);
610}
611
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800612// -------------------------------------------------------------------------
613
Eric Laurentbda74692009-11-04 08:27:26 -0800614status_t AudioTrack::createTrack(
615 int streamType,
616 uint32_t sampleRate,
617 int format,
618 int channelCount,
619 int frameCount,
620 uint32_t flags,
621 const sp<IMemory>& sharedBuffer,
Eric Laurenteb8f850d2010-05-14 03:26:45 -0700622 audio_io_handle_t output,
623 bool enforceFrameCount)
Eric Laurentbda74692009-11-04 08:27:26 -0800624{
625 status_t status;
626 const sp<IAudioFlinger>& audioFlinger = AudioSystem::get_audio_flinger();
627 if (audioFlinger == 0) {
628 LOGE("Could not get audioflinger");
629 return NO_INIT;
630 }
631
Eric Laurenteb8f850d2010-05-14 03:26:45 -0700632 int afSampleRate;
633 if (AudioSystem::getOutputSamplingRate(&afSampleRate, streamType) != NO_ERROR) {
634 return NO_INIT;
635 }
636 int afFrameCount;
637 if (AudioSystem::getOutputFrameCount(&afFrameCount, streamType) != NO_ERROR) {
638 return NO_INIT;
639 }
640 uint32_t afLatency;
641 if (AudioSystem::getOutputLatency(&afLatency, streamType) != NO_ERROR) {
642 return NO_INIT;
643 }
644
645 mNotificationFramesAct = mNotificationFramesReq;
646 if (!AudioSystem::isLinearPCM(format)) {
647 if (sharedBuffer != 0) {
648 frameCount = sharedBuffer->size();
649 }
650 } else {
651 // Ensure that buffer depth covers at least audio hardware latency
652 uint32_t minBufCount = afLatency / ((1000 * afFrameCount)/afSampleRate);
653 if (minBufCount < 2) minBufCount = 2;
654
655 int minFrameCount = (afFrameCount*sampleRate*minBufCount)/afSampleRate;
656
657 if (sharedBuffer == 0) {
658 if (frameCount == 0) {
659 frameCount = minFrameCount;
660 }
661 if (mNotificationFramesAct == 0) {
662 mNotificationFramesAct = frameCount/2;
663 }
664 // Make sure that application is notified with sufficient margin
665 // before underrun
666 if (mNotificationFramesAct > (uint32_t)frameCount/2) {
667 mNotificationFramesAct = frameCount/2;
668 }
669 if (frameCount < minFrameCount) {
670 if (enforceFrameCount) {
671 LOGE("Invalid buffer size: minFrameCount %d, frameCount %d", minFrameCount, frameCount);
672 return BAD_VALUE;
673 } else {
674 frameCount = minFrameCount;
675 }
676 }
677 } else {
678 // Ensure that buffer alignment matches channelcount
679 if (((uint32_t)sharedBuffer->pointer() & (channelCount | 1)) != 0) {
680 LOGE("Invalid buffer alignement: address %p, channelCount %d", sharedBuffer->pointer(), channelCount);
681 return BAD_VALUE;
682 }
683 frameCount = sharedBuffer->size()/channelCount/sizeof(int16_t);
684 }
685 }
686
Eric Laurentbda74692009-11-04 08:27:26 -0800687 sp<IAudioTrack> track = audioFlinger->createTrack(getpid(),
688 streamType,
689 sampleRate,
690 format,
691 channelCount,
692 frameCount,
693 ((uint16_t)flags) << 16,
694 sharedBuffer,
695 output,
Eric Laurent65b65452010-06-01 23:49:17 -0700696 &mSessionId,
Eric Laurentbda74692009-11-04 08:27:26 -0800697 &status);
698
699 if (track == 0) {
700 LOGE("AudioFlinger could not create track, status: %d", status);
701 return status;
702 }
703 sp<IMemory> cblk = track->getCblk();
704 if (cblk == 0) {
705 LOGE("Could not get control block");
706 return NO_INIT;
707 }
708 mAudioTrack.clear();
709 mAudioTrack = track;
710 mCblkMemory.clear();
711 mCblkMemory = cblk;
712 mCblk = static_cast<audio_track_cblk_t*>(cblk->pointer());
Eric Laurenteb8f850d2010-05-14 03:26:45 -0700713 mCblk->flags |= CBLK_DIRECTION_OUT;
Eric Laurentbda74692009-11-04 08:27:26 -0800714 if (sharedBuffer == 0) {
715 mCblk->buffers = (char*)mCblk + sizeof(audio_track_cblk_t);
716 } else {
717 mCblk->buffers = sharedBuffer->pointer();
718 // Force buffer full condition as data is already present in shared memory
Eric Laurenteb8f850d2010-05-14 03:26:45 -0700719 mCblk->stepUser(mCblk->frameCount);
Eric Laurentbda74692009-11-04 08:27:26 -0800720 }
721
Eric Laurent65b65452010-06-01 23:49:17 -0700722 mCblk->volumeLR = (uint32_t(uint16_t(mVolume[RIGHT] * 0x1000)) << 16) | uint16_t(mVolume[LEFT] * 0x1000);
723 mCblk->sendLevel = uint16_t(mSendLevel * 0x1000);
Eric Laurent49f02be2009-11-19 09:00:56 -0800724 mCblk->bufferTimeoutMs = MAX_STARTUP_TIMEOUT_MS;
725 mCblk->waitTimeMs = 0;
Eric Laurenteb8f850d2010-05-14 03:26:45 -0700726 mRemainingFrames = mNotificationFramesAct;
727 mLatency = afLatency + (1000*mCblk->frameCount) / sampleRate;
Eric Laurentbda74692009-11-04 08:27:26 -0800728 return NO_ERROR;
729}
730
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800731status_t AudioTrack::obtainBuffer(Buffer* audioBuffer, int32_t waitCount)
732{
733 int active;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800734 status_t result;
735 audio_track_cblk_t* cblk = mCblk;
736 uint32_t framesReq = audioBuffer->frameCount;
Eric Laurentef028272009-04-21 07:56:33 -0700737 uint32_t waitTimeMs = (waitCount < 0) ? cblk->bufferTimeoutMs : WAIT_PERIOD_MS;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800738
739 audioBuffer->frameCount = 0;
740 audioBuffer->size = 0;
741
742 uint32_t framesAvail = cblk->framesAvailable();
743
744 if (framesAvail == 0) {
Eric Laurentbda74692009-11-04 08:27:26 -0800745 cblk->lock.lock();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800746 goto start_loop_here;
747 while (framesAvail == 0) {
748 active = mActive;
749 if (UNLIKELY(!active)) {
750 LOGV("Not active and NO_MORE_BUFFERS");
Eric Laurentbda74692009-11-04 08:27:26 -0800751 cblk->lock.unlock();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800752 return NO_MORE_BUFFERS;
753 }
Eric Laurentbda74692009-11-04 08:27:26 -0800754 if (UNLIKELY(!waitCount)) {
755 cblk->lock.unlock();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800756 return WOULD_BLOCK;
Eric Laurentbda74692009-11-04 08:27:26 -0800757 }
Eric Laurenteb8f850d2010-05-14 03:26:45 -0700758 if (!(cblk->flags & CBLK_INVALID_MSK)) {
759 result = cblk->cv.waitRelative(cblk->lock, milliseconds(waitTimeMs));
760 }
761 if (cblk->flags & CBLK_INVALID_MSK) {
762 LOGW("obtainBuffer() track %p invalidated, creating a new one", this);
763 // no need to clear the invalid flag as this cblk will not be used anymore
764 cblk->lock.unlock();
765 goto create_new_track;
766 }
Eric Laurenta553c252009-07-17 12:17:14 -0700767 if (__builtin_expect(result!=NO_ERROR, false)) {
Eric Laurentef028272009-04-21 07:56:33 -0700768 cblk->waitTimeMs += waitTimeMs;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800769 if (cblk->waitTimeMs >= cblk->bufferTimeoutMs) {
770 // timing out when a loop has been set and we have already written upto loop end
771 // is a normal condition: no need to wake AudioFlinger up.
772 if (cblk->user < cblk->loopEnd) {
773 LOGW( "obtainBuffer timed out (is the CPU pegged?) %p "
774 "user=%08x, server=%08x", this, cblk->user, cblk->server);
Eric Laurenta553c252009-07-17 12:17:14 -0700775 //unlock cblk mutex before calling mAudioTrack->start() (see issue #1617140)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800776 cblk->lock.unlock();
Eric Laurentbda74692009-11-04 08:27:26 -0800777 result = mAudioTrack->start();
778 if (result == DEAD_OBJECT) {
779 LOGW("obtainBuffer() dead IAudioTrack: creating a new one");
Eric Laurenteb8f850d2010-05-14 03:26:45 -0700780create_new_track:
Eric Laurentbda74692009-11-04 08:27:26 -0800781 result = createTrack(mStreamType, cblk->sampleRate, mFormat, mChannelCount,
Eric Laurenteb8f850d2010-05-14 03:26:45 -0700782 mFrameCount, mFlags, mSharedBuffer, getOutput(), false);
Eric Laurentbda74692009-11-04 08:27:26 -0800783 if (result == NO_ERROR) {
784 cblk = mCblk;
785 cblk->bufferTimeoutMs = MAX_RUN_TIMEOUT_MS;
Eric Laurent49f02be2009-11-19 09:00:56 -0800786 mAudioTrack->start();
Eric Laurentbda74692009-11-04 08:27:26 -0800787 }
788 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800789 cblk->lock.lock();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800790 }
791 cblk->waitTimeMs = 0;
792 }
Eric Laurenta553c252009-07-17 12:17:14 -0700793
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800794 if (--waitCount == 0) {
Eric Laurentbda74692009-11-04 08:27:26 -0800795 cblk->lock.unlock();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800796 return TIMED_OUT;
797 }
798 }
799 // read the server count again
800 start_loop_here:
801 framesAvail = cblk->framesAvailable_l();
802 }
Eric Laurentbda74692009-11-04 08:27:26 -0800803 cblk->lock.unlock();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800804 }
805
806 cblk->waitTimeMs = 0;
Eric Laurenta553c252009-07-17 12:17:14 -0700807
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800808 if (framesReq > framesAvail) {
809 framesReq = framesAvail;
810 }
811
812 uint32_t u = cblk->user;
813 uint32_t bufferEnd = cblk->userBase + cblk->frameCount;
814
815 if (u + framesReq > bufferEnd) {
816 framesReq = bufferEnd - u;
817 }
818
Eric Laurenta553c252009-07-17 12:17:14 -0700819 audioBuffer->flags = mMuted ? Buffer::MUTE : 0;
820 audioBuffer->channelCount = mChannelCount;
821 audioBuffer->frameCount = framesReq;
822 audioBuffer->size = framesReq * cblk->frameSize;
823 if (AudioSystem::isLinearPCM(mFormat)) {
824 audioBuffer->format = AudioSystem::PCM_16_BIT;
825 } else {
826 audioBuffer->format = mFormat;
827 }
828 audioBuffer->raw = (int8_t *)cblk->buffer(u);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800829 active = mActive;
830 return active ? status_t(NO_ERROR) : status_t(STOPPED);
831}
832
833void AudioTrack::releaseBuffer(Buffer* audioBuffer)
834{
835 audio_track_cblk_t* cblk = mCblk;
836 cblk->stepUser(audioBuffer->frameCount);
837}
838
839// -------------------------------------------------------------------------
840
841ssize_t AudioTrack::write(const void* buffer, size_t userSize)
842{
843
844 if (mSharedBuffer != 0) return INVALID_OPERATION;
845
846 if (ssize_t(userSize) < 0) {
847 // sanity-check. user is most-likely passing an error code.
848 LOGE("AudioTrack::write(buffer=%p, size=%u (%d)",
849 buffer, userSize, userSize);
850 return BAD_VALUE;
851 }
852
853 LOGV("write %p: %d bytes, mActive=%d", this, userSize, mActive);
854
855 ssize_t written = 0;
856 const int8_t *src = (const int8_t *)buffer;
857 Buffer audioBuffer;
858
859 do {
Eric Laurenta553c252009-07-17 12:17:14 -0700860 audioBuffer.frameCount = userSize/frameSize();
861
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800862 // Calling obtainBuffer() with a negative wait count causes
863 // an (almost) infinite wait time.
864 status_t err = obtainBuffer(&audioBuffer, -1);
865 if (err < 0) {
866 // out of buffers, return #bytes written
867 if (err == status_t(NO_MORE_BUFFERS))
868 break;
869 return ssize_t(err);
870 }
871
872 size_t toWrite;
Eric Laurenta553c252009-07-17 12:17:14 -0700873
Eric Laurent28ad42b2009-08-04 10:42:26 -0700874 if (mFormat == AudioSystem::PCM_8_BIT && !(mFlags & AudioSystem::OUTPUT_FLAG_DIRECT)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800875 // Divide capacity by 2 to take expansion into account
876 toWrite = audioBuffer.size>>1;
877 // 8 to 16 bit conversion
878 int count = toWrite;
879 int16_t *dst = (int16_t *)(audioBuffer.i8);
880 while(count--) {
881 *dst++ = (int16_t)(*src++^0x80) << 8;
882 }
Eric Laurent28ad42b2009-08-04 10:42:26 -0700883 } else {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800884 toWrite = audioBuffer.size;
885 memcpy(audioBuffer.i8, src, toWrite);
886 src += toWrite;
887 }
888 userSize -= toWrite;
889 written += toWrite;
890
891 releaseBuffer(&audioBuffer);
892 } while (userSize);
893
894 return written;
895}
896
897// -------------------------------------------------------------------------
898
899bool AudioTrack::processAudioBuffer(const sp<AudioTrackThread>& thread)
900{
901 Buffer audioBuffer;
902 uint32_t frames;
903 size_t writtenSize;
904
905 // Manage underrun callback
906 if (mActive && (mCblk->framesReady() == 0)) {
Eric Laurenteb8f850d2010-05-14 03:26:45 -0700907 LOGV("Underrun user: %x, server: %x, flags %04x", mCblk->user, mCblk->server, mCblk->flags);
908 if ((mCblk->flags & CBLK_UNDERRUN_MSK) == CBLK_UNDERRUN_OFF) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800909 mCbf(EVENT_UNDERRUN, mUserData, 0);
910 if (mCblk->server == mCblk->frameCount) {
Eric Laurenta553c252009-07-17 12:17:14 -0700911 mCbf(EVENT_BUFFER_END, mUserData, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800912 }
Eric Laurenteb8f850d2010-05-14 03:26:45 -0700913 mCblk->flags |= CBLK_UNDERRUN_ON;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800914 if (mSharedBuffer != 0) return false;
915 }
916 }
Eric Laurenta553c252009-07-17 12:17:14 -0700917
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800918 // Manage loop end callback
919 while (mLoopCount > mCblk->loopCount) {
920 int loopCount = -1;
921 mLoopCount--;
922 if (mLoopCount >= 0) loopCount = mLoopCount;
923
924 mCbf(EVENT_LOOP_END, mUserData, (void *)&loopCount);
925 }
926
927 // Manage marker callback
Jean-Michel Trivi4a5c1a72009-03-24 18:11:07 -0700928 if (!mMarkerReached && (mMarkerPosition > 0)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800929 if (mCblk->server >= mMarkerPosition) {
930 mCbf(EVENT_MARKER, mUserData, (void *)&mMarkerPosition);
Jean-Michel Trivi4a5c1a72009-03-24 18:11:07 -0700931 mMarkerReached = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800932 }
933 }
934
935 // Manage new position callback
Eric Laurenta553c252009-07-17 12:17:14 -0700936 if (mUpdatePeriod > 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800937 while (mCblk->server >= mNewPosition) {
938 mCbf(EVENT_NEW_POS, mUserData, (void *)&mNewPosition);
939 mNewPosition += mUpdatePeriod;
940 }
941 }
942
943 // If Shared buffer is used, no data is requested from client.
944 if (mSharedBuffer != 0) {
945 frames = 0;
946 } else {
947 frames = mRemainingFrames;
948 }
949
950 do {
951
952 audioBuffer.frameCount = frames;
Eric Laurenta553c252009-07-17 12:17:14 -0700953
954 // Calling obtainBuffer() with a wait count of 1
955 // limits wait time to WAIT_PERIOD_MS. This prevents from being
956 // stuck here not being able to handle timed events (position, markers, loops).
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800957 status_t err = obtainBuffer(&audioBuffer, 1);
958 if (err < NO_ERROR) {
959 if (err != TIMED_OUT) {
Eric Laurentef028272009-04-21 07:56:33 -0700960 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 -0800961 return false;
962 }
963 break;
964 }
965 if (err == status_t(STOPPED)) return false;
966
967 // Divide buffer size by 2 to take into account the expansion
968 // due to 8 to 16 bit conversion: the callback must fill only half
969 // of the destination buffer
Eric Laurent28ad42b2009-08-04 10:42:26 -0700970 if (mFormat == AudioSystem::PCM_8_BIT && !(mFlags & AudioSystem::OUTPUT_FLAG_DIRECT)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800971 audioBuffer.size >>= 1;
972 }
973
974 size_t reqSize = audioBuffer.size;
975 mCbf(EVENT_MORE_DATA, mUserData, &audioBuffer);
976 writtenSize = audioBuffer.size;
977
978 // Sanity check on returned size
The Android Open Source Project4df24232009-03-05 14:34:35 -0800979 if (ssize_t(writtenSize) <= 0) {
980 // The callback is done filling buffers
981 // Keep this thread going to handle timed events and
982 // still try to get more data in intervals of WAIT_PERIOD_MS
983 // but don't just loop and block the CPU, so wait
984 usleep(WAIT_PERIOD_MS*1000);
985 break;
986 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800987 if (writtenSize > reqSize) writtenSize = reqSize;
988
Eric Laurent28ad42b2009-08-04 10:42:26 -0700989 if (mFormat == AudioSystem::PCM_8_BIT && !(mFlags & AudioSystem::OUTPUT_FLAG_DIRECT)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800990 // 8 to 16 bit conversion
991 const int8_t *src = audioBuffer.i8 + writtenSize-1;
992 int count = writtenSize;
993 int16_t *dst = audioBuffer.i16 + writtenSize-1;
994 while(count--) {
995 *dst-- = (int16_t)(*src--^0x80) << 8;
996 }
997 writtenSize <<= 1;
998 }
999
1000 audioBuffer.size = writtenSize;
Eric Laurenta553c252009-07-17 12:17:14 -07001001 // NOTE: mCblk->frameSize is not equal to AudioTrack::frameSize() for
1002 // 8 bit PCM data: in this case, mCblk->frameSize is based on a sampel size of
1003 // 16 bit.
1004 audioBuffer.frameCount = writtenSize/mCblk->frameSize;
1005
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001006 frames -= audioBuffer.frameCount;
1007
1008 releaseBuffer(&audioBuffer);
1009 }
1010 while (frames);
1011
1012 if (frames == 0) {
Eric Laurenteb8f850d2010-05-14 03:26:45 -07001013 mRemainingFrames = mNotificationFramesAct;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001014 } else {
1015 mRemainingFrames = frames;
1016 }
1017 return true;
1018}
1019
1020status_t AudioTrack::dump(int fd, const Vector<String16>& args) const
1021{
1022
1023 const size_t SIZE = 256;
1024 char buffer[SIZE];
1025 String8 result;
1026
1027 result.append(" AudioTrack::dump\n");
1028 snprintf(buffer, 255, " stream type(%d), left - right volume(%f, %f)\n", mStreamType, mVolume[0], mVolume[1]);
1029 result.append(buffer);
Eric Laurenteb8f850d2010-05-14 03:26:45 -07001030 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 -08001031 result.append(buffer);
Eric Laurent88e209d2009-07-07 07:10:45 -07001032 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 -08001033 result.append(buffer);
1034 snprintf(buffer, 255, " active(%d), latency (%d)\n", mActive, mLatency);
1035 result.append(buffer);
1036 ::write(fd, result.string(), result.size());
1037 return NO_ERROR;
1038}
1039
1040// =========================================================================
1041
1042AudioTrack::AudioTrackThread::AudioTrackThread(AudioTrack& receiver, bool bCanCallJava)
1043 : Thread(bCanCallJava), mReceiver(receiver)
1044{
1045}
1046
1047bool AudioTrack::AudioTrackThread::threadLoop()
1048{
1049 return mReceiver.processAudioBuffer(this);
1050}
1051
1052status_t AudioTrack::AudioTrackThread::readyToRun()
1053{
1054 return NO_ERROR;
1055}
1056
1057void AudioTrack::AudioTrackThread::onFirstRef()
1058{
1059}
1060
1061// =========================================================================
1062
1063audio_track_cblk_t::audio_track_cblk_t()
Mathias Agopiana729f972010-03-19 16:14:13 -07001064 : lock(Mutex::SHARED), cv(Condition::SHARED), user(0), server(0),
1065 userBase(0), serverBase(0), buffers(0), frameCount(0),
1066 loopStart(UINT_MAX), loopEnd(UINT_MAX), loopCount(0), volumeLR(0),
Eric Laurent65b65452010-06-01 23:49:17 -07001067 flags(0), sendLevel(0)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001068{
1069}
1070
1071uint32_t audio_track_cblk_t::stepUser(uint32_t frameCount)
1072{
1073 uint32_t u = this->user;
1074
1075 u += frameCount;
1076 // Ensure that user is never ahead of server for AudioRecord
Eric Laurenteb8f850d2010-05-14 03:26:45 -07001077 if (flags & CBLK_DIRECTION_MSK) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001078 // If stepServer() has been called once, switch to normal obtainBuffer() timeout period
1079 if (bufferTimeoutMs == MAX_STARTUP_TIMEOUT_MS-1) {
1080 bufferTimeoutMs = MAX_RUN_TIMEOUT_MS;
1081 }
1082 } else if (u > this->server) {
1083 LOGW("stepServer occured after track reset");
1084 u = this->server;
1085 }
1086
1087 if (u >= userBase + this->frameCount) {
1088 userBase += this->frameCount;
1089 }
1090
1091 this->user = u;
1092
1093 // Clear flow control error condition as new data has been written/read to/from buffer.
Eric Laurenteb8f850d2010-05-14 03:26:45 -07001094 flags &= ~CBLK_UNDERRUN_MSK;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001095
1096 return u;
1097}
1098
1099bool audio_track_cblk_t::stepServer(uint32_t frameCount)
1100{
1101 // the code below simulates lock-with-timeout
1102 // we MUST do this to protect the AudioFlinger server
1103 // as this lock is shared with the client.
1104 status_t err;
1105
1106 err = lock.tryLock();
1107 if (err == -EBUSY) { // just wait a bit
1108 usleep(1000);
1109 err = lock.tryLock();
1110 }
1111 if (err != NO_ERROR) {
1112 // probably, the client just died.
1113 return false;
1114 }
1115
1116 uint32_t s = this->server;
1117
1118 s += frameCount;
Eric Laurenteb8f850d2010-05-14 03:26:45 -07001119 if (flags & CBLK_DIRECTION_MSK) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001120 // Mark that we have read the first buffer so that next time stepUser() is called
1121 // we switch to normal obtainBuffer() timeout period
1122 if (bufferTimeoutMs == MAX_STARTUP_TIMEOUT_MS) {
Eric Laurentbda74692009-11-04 08:27:26 -08001123 bufferTimeoutMs = MAX_STARTUP_TIMEOUT_MS - 1;
Eric Laurenta553c252009-07-17 12:17:14 -07001124 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001125 // It is possible that we receive a flush()
1126 // while the mixer is processing a block: in this case,
1127 // stepServer() is called After the flush() has reset u & s and
1128 // we have s > u
1129 if (s > this->user) {
1130 LOGW("stepServer occured after track reset");
1131 s = this->user;
1132 }
1133 }
1134
1135 if (s >= loopEnd) {
1136 LOGW_IF(s > loopEnd, "stepServer: s %u > loopEnd %u", s, loopEnd);
1137 s = loopStart;
1138 if (--loopCount == 0) {
1139 loopEnd = UINT_MAX;
1140 loopStart = UINT_MAX;
1141 }
1142 }
1143 if (s >= serverBase + this->frameCount) {
1144 serverBase += this->frameCount;
1145 }
1146
1147 this->server = s;
1148
1149 cv.signal();
1150 lock.unlock();
1151 return true;
1152}
1153
1154void* audio_track_cblk_t::buffer(uint32_t offset) const
1155{
Eric Laurenta553c252009-07-17 12:17:14 -07001156 return (int8_t *)this->buffers + (offset - userBase) * this->frameSize;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001157}
1158
1159uint32_t audio_track_cblk_t::framesAvailable()
1160{
1161 Mutex::Autolock _l(lock);
1162 return framesAvailable_l();
1163}
1164
1165uint32_t audio_track_cblk_t::framesAvailable_l()
1166{
1167 uint32_t u = this->user;
1168 uint32_t s = this->server;
1169
Eric Laurenteb8f850d2010-05-14 03:26:45 -07001170 if (flags & CBLK_DIRECTION_MSK) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001171 uint32_t limit = (s < loopStart) ? s : loopStart;
1172 return limit + frameCount - u;
1173 } else {
1174 return frameCount + u - s;
1175 }
1176}
1177
1178uint32_t audio_track_cblk_t::framesReady()
1179{
1180 uint32_t u = this->user;
1181 uint32_t s = this->server;
1182
Eric Laurenteb8f850d2010-05-14 03:26:45 -07001183 if (flags & CBLK_DIRECTION_MSK) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001184 if (u < loopEnd) {
1185 return u - s;
1186 } else {
1187 Mutex::Autolock _l(lock);
1188 if (loopCount >= 0) {
1189 return (loopEnd - loopStart)*loopCount + u - s;
1190 } else {
1191 return UINT_MAX;
1192 }
1193 }
1194 } else {
1195 return s - u;
1196 }
1197}
1198
1199// -------------------------------------------------------------------------
1200
1201}; // namespace android
1202