blob: 9fe459bc3321a48f65b879d541f990468b2df715 [file] [log] [blame]
Eric Laurent81784c32012-11-19 14:55:58 -08001/*
2**
3** Copyright 2012, 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_TAG "AudioFlinger"
20//#define LOG_NDEBUG 0
21
Glenn Kasten153b9fe2013-07-15 11:23:36 -070022#include "Configuration.h"
Eric Laurent81784c32012-11-19 14:55:58 -080023#include <math.h>
Eric Laurent81784c32012-11-19 14:55:58 -080024#include <utils/Log.h>
25
26#include <private/media/AudioTrackShared.h>
27
28#include <common_time/cc_helper.h>
29#include <common_time/local_clock.h>
30
31#include "AudioMixer.h"
32#include "AudioFlinger.h"
33#include "ServiceUtilities.h"
34
Glenn Kastenda6ef132013-01-10 12:31:01 -080035#include <media/nbaio/Pipe.h>
36#include <media/nbaio/PipeReader.h>
37
Eric Laurent81784c32012-11-19 14:55:58 -080038// ----------------------------------------------------------------------------
39
40// Note: the following macro is used for extremely verbose logging message. In
41// order to run with ALOG_ASSERT turned on, we need to have LOG_NDEBUG set to
42// 0; but one side effect of this is to turn all LOGV's as well. Some messages
43// are so verbose that we want to suppress them even when we have ALOG_ASSERT
44// turned on. Do not uncomment the #def below unless you really know what you
45// are doing and want to see all of the extremely verbose messages.
46//#define VERY_VERY_VERBOSE_LOGGING
47#ifdef VERY_VERY_VERBOSE_LOGGING
48#define ALOGVV ALOGV
49#else
50#define ALOGVV(a...) do { } while(0)
51#endif
52
53namespace android {
54
55// ----------------------------------------------------------------------------
56// TrackBase
57// ----------------------------------------------------------------------------
58
Glenn Kastenda6ef132013-01-10 12:31:01 -080059static volatile int32_t nextTrackId = 55;
60
Eric Laurent81784c32012-11-19 14:55:58 -080061// TrackBase constructor must be called with AudioFlinger::mLock held
62AudioFlinger::ThreadBase::TrackBase::TrackBase(
63 ThreadBase *thread,
64 const sp<Client>& client,
65 uint32_t sampleRate,
66 audio_format_t format,
67 audio_channel_mask_t channelMask,
68 size_t frameCount,
69 const sp<IMemory>& sharedBuffer,
Glenn Kastene3aa6592012-12-04 12:22:46 -080070 int sessionId,
Marco Nelissen462fd2f2013-01-14 14:12:05 -080071 int clientUid,
Glenn Kastene3aa6592012-12-04 12:22:46 -080072 bool isOut)
Eric Laurent81784c32012-11-19 14:55:58 -080073 : RefBase(),
74 mThread(thread),
75 mClient(client),
76 mCblk(NULL),
77 // mBuffer
Eric Laurent81784c32012-11-19 14:55:58 -080078 mState(IDLE),
79 mSampleRate(sampleRate),
80 mFormat(format),
81 mChannelMask(channelMask),
82 mChannelCount(popcount(channelMask)),
83 mFrameSize(audio_is_linear_pcm(format) ?
84 mChannelCount * audio_bytes_per_sample(format) : sizeof(int8_t)),
85 mFrameCount(frameCount),
Glenn Kastene3aa6592012-12-04 12:22:46 -080086 mSessionId(sessionId),
87 mIsOut(isOut),
Glenn Kastenda6ef132013-01-10 12:31:01 -080088 mServerProxy(NULL),
Eric Laurentbfb1b832013-01-07 09:53:42 -080089 mId(android_atomic_inc(&nextTrackId)),
90 mTerminated(false)
Eric Laurent81784c32012-11-19 14:55:58 -080091{
Marco Nelissen462fd2f2013-01-14 14:12:05 -080092 // if the caller is us, trust the specified uid
93 if (IPCThreadState::self()->getCallingPid() != getpid_cached || clientUid == -1) {
94 int newclientUid = IPCThreadState::self()->getCallingUid();
95 if (clientUid != -1 && clientUid != newclientUid) {
96 ALOGW("uid %d tried to pass itself off as %d", newclientUid, clientUid);
97 }
98 clientUid = newclientUid;
99 }
100 // clientUid contains the uid of the app that is responsible for this track, so we can blame
101 // battery usage on it.
102 mUid = clientUid;
103
Eric Laurent81784c32012-11-19 14:55:58 -0800104 // client == 0 implies sharedBuffer == 0
105 ALOG_ASSERT(!(client == 0 && sharedBuffer != 0));
106
107 ALOGV_IF(sharedBuffer != 0, "sharedBuffer: %p, size: %d", sharedBuffer->pointer(),
108 sharedBuffer->size());
109
110 // ALOGD("Creating track with %d buffers @ %d bytes", bufferCount, bufferSize);
111 size_t size = sizeof(audio_track_cblk_t);
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800112 size_t bufferSize = (sharedBuffer == 0 ? roundup(frameCount) : frameCount) * mFrameSize;
Eric Laurent81784c32012-11-19 14:55:58 -0800113 if (sharedBuffer == 0) {
114 size += bufferSize;
115 }
116
117 if (client != 0) {
118 mCblkMemory = client->heap()->allocate(size);
Glenn Kasten663c2242013-09-24 11:52:37 -0700119 if (mCblkMemory == 0 ||
120 (mCblk = static_cast<audio_track_cblk_t *>(mCblkMemory->pointer())) == NULL) {
Eric Laurent81784c32012-11-19 14:55:58 -0800121 ALOGE("not enough memory for AudioTrack size=%u", size);
122 client->heap()->dump("AudioTrack");
Glenn Kasten663c2242013-09-24 11:52:37 -0700123 mCblkMemory.clear();
Eric Laurent81784c32012-11-19 14:55:58 -0800124 return;
125 }
126 } else {
Glenn Kastene3aa6592012-12-04 12:22:46 -0800127 // this syntax avoids calling the audio_track_cblk_t constructor twice
128 mCblk = (audio_track_cblk_t *) new uint8_t[size];
Eric Laurent81784c32012-11-19 14:55:58 -0800129 // assume mCblk != NULL
130 }
131
132 // construct the shared structure in-place.
133 if (mCblk != NULL) {
134 new(mCblk) audio_track_cblk_t();
135 // clear all buffers
Eric Laurent81784c32012-11-19 14:55:58 -0800136 if (sharedBuffer == 0) {
137 mBuffer = (char*)mCblk + sizeof(audio_track_cblk_t);
138 memset(mBuffer, 0, bufferSize);
Eric Laurent81784c32012-11-19 14:55:58 -0800139 } else {
140 mBuffer = sharedBuffer->pointer();
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800141#if 0
Glenn Kasten96f60d82013-07-12 10:21:18 -0700142 mCblk->mFlags = CBLK_FORCEREADY; // FIXME hack, need to fix the track ready logic
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800143#endif
Eric Laurent81784c32012-11-19 14:55:58 -0800144 }
Glenn Kastenda6ef132013-01-10 12:31:01 -0800145
Glenn Kasten46909e72013-02-26 09:20:22 -0800146#ifdef TEE_SINK
Glenn Kastenda6ef132013-01-10 12:31:01 -0800147 if (mTeeSinkTrackEnabled) {
Glenn Kasten46909e72013-02-26 09:20:22 -0800148 NBAIO_Format pipeFormat = Format_from_SR_C(mSampleRate, mChannelCount);
Glenn Kasten6e0d67d2014-01-31 09:41:08 -0800149 if (Format_isValid(pipeFormat)) {
Glenn Kasten46909e72013-02-26 09:20:22 -0800150 Pipe *pipe = new Pipe(mTeeSinkTrackFrames, pipeFormat);
151 size_t numCounterOffers = 0;
152 const NBAIO_Format offers[1] = {pipeFormat};
153 ssize_t index = pipe->negotiate(offers, 1, NULL, numCounterOffers);
154 ALOG_ASSERT(index == 0);
155 PipeReader *pipeReader = new PipeReader(*pipe);
156 numCounterOffers = 0;
157 index = pipeReader->negotiate(offers, 1, NULL, numCounterOffers);
158 ALOG_ASSERT(index == 0);
159 mTeeSink = pipe;
160 mTeeSource = pipeReader;
161 }
Glenn Kastenda6ef132013-01-10 12:31:01 -0800162 }
Glenn Kasten46909e72013-02-26 09:20:22 -0800163#endif
Glenn Kastenda6ef132013-01-10 12:31:01 -0800164
Eric Laurent81784c32012-11-19 14:55:58 -0800165 }
166}
167
168AudioFlinger::ThreadBase::TrackBase::~TrackBase()
169{
Glenn Kasten46909e72013-02-26 09:20:22 -0800170#ifdef TEE_SINK
Glenn Kastenda6ef132013-01-10 12:31:01 -0800171 dumpTee(-1, mTeeSource, mId);
Glenn Kasten46909e72013-02-26 09:20:22 -0800172#endif
Glenn Kastene3aa6592012-12-04 12:22:46 -0800173 // delete the proxy before deleting the shared memory it refers to, to avoid dangling reference
174 delete mServerProxy;
Eric Laurent81784c32012-11-19 14:55:58 -0800175 if (mCblk != NULL) {
176 if (mClient == 0) {
177 delete mCblk;
178 } else {
179 mCblk->~audio_track_cblk_t(); // destroy our shared-structure.
180 }
181 }
182 mCblkMemory.clear(); // free the shared memory before releasing the heap it belongs to
183 if (mClient != 0) {
184 // Client destructor must run with AudioFlinger mutex locked
185 Mutex::Autolock _l(mClient->audioFlinger()->mLock);
186 // If the client's reference count drops to zero, the associated destructor
187 // must run with AudioFlinger lock held. Thus the explicit clear() rather than
188 // relying on the automatic clear() at end of scope.
189 mClient.clear();
190 }
191}
192
193// AudioBufferProvider interface
194// getNextBuffer() = 0;
195// This implementation of releaseBuffer() is used by Track and RecordTrack, but not TimedTrack
196void AudioFlinger::ThreadBase::TrackBase::releaseBuffer(AudioBufferProvider::Buffer* buffer)
197{
Glenn Kasten46909e72013-02-26 09:20:22 -0800198#ifdef TEE_SINK
Glenn Kastenda6ef132013-01-10 12:31:01 -0800199 if (mTeeSink != 0) {
200 (void) mTeeSink->write(buffer->raw, buffer->frameCount);
201 }
Glenn Kasten46909e72013-02-26 09:20:22 -0800202#endif
Glenn Kastenda6ef132013-01-10 12:31:01 -0800203
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800204 ServerProxy::Buffer buf;
205 buf.mFrameCount = buffer->frameCount;
206 buf.mRaw = buffer->raw;
Eric Laurent81784c32012-11-19 14:55:58 -0800207 buffer->frameCount = 0;
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800208 buffer->raw = NULL;
209 mServerProxy->releaseBuffer(&buf);
Eric Laurent81784c32012-11-19 14:55:58 -0800210}
211
Eric Laurent81784c32012-11-19 14:55:58 -0800212status_t AudioFlinger::ThreadBase::TrackBase::setSyncEvent(const sp<SyncEvent>& event)
213{
214 mSyncEvents.add(event);
215 return NO_ERROR;
216}
217
218// ----------------------------------------------------------------------------
219// Playback
220// ----------------------------------------------------------------------------
221
222AudioFlinger::TrackHandle::TrackHandle(const sp<AudioFlinger::PlaybackThread::Track>& track)
223 : BnAudioTrack(),
224 mTrack(track)
225{
226}
227
228AudioFlinger::TrackHandle::~TrackHandle() {
229 // just stop the track on deletion, associated resources
230 // will be freed from the main thread once all pending buffers have
231 // been played. Unless it's not in the active track list, in which
232 // case we free everything now...
233 mTrack->destroy();
234}
235
236sp<IMemory> AudioFlinger::TrackHandle::getCblk() const {
237 return mTrack->getCblk();
238}
239
240status_t AudioFlinger::TrackHandle::start() {
241 return mTrack->start();
242}
243
244void AudioFlinger::TrackHandle::stop() {
245 mTrack->stop();
246}
247
248void AudioFlinger::TrackHandle::flush() {
249 mTrack->flush();
250}
251
Eric Laurent81784c32012-11-19 14:55:58 -0800252void AudioFlinger::TrackHandle::pause() {
253 mTrack->pause();
254}
255
256status_t AudioFlinger::TrackHandle::attachAuxEffect(int EffectId)
257{
258 return mTrack->attachAuxEffect(EffectId);
259}
260
261status_t AudioFlinger::TrackHandle::allocateTimedBuffer(size_t size,
262 sp<IMemory>* buffer) {
263 if (!mTrack->isTimedTrack())
264 return INVALID_OPERATION;
265
266 PlaybackThread::TimedTrack* tt =
267 reinterpret_cast<PlaybackThread::TimedTrack*>(mTrack.get());
268 return tt->allocateTimedBuffer(size, buffer);
269}
270
271status_t AudioFlinger::TrackHandle::queueTimedBuffer(const sp<IMemory>& buffer,
272 int64_t pts) {
273 if (!mTrack->isTimedTrack())
274 return INVALID_OPERATION;
275
Glenn Kasten663c2242013-09-24 11:52:37 -0700276 if (buffer == 0 || buffer->pointer() == NULL) {
277 ALOGE("queueTimedBuffer() buffer is 0 or has NULL pointer()");
278 return BAD_VALUE;
279 }
280
Eric Laurent81784c32012-11-19 14:55:58 -0800281 PlaybackThread::TimedTrack* tt =
282 reinterpret_cast<PlaybackThread::TimedTrack*>(mTrack.get());
283 return tt->queueTimedBuffer(buffer, pts);
284}
285
286status_t AudioFlinger::TrackHandle::setMediaTimeTransform(
287 const LinearTransform& xform, int target) {
288
289 if (!mTrack->isTimedTrack())
290 return INVALID_OPERATION;
291
292 PlaybackThread::TimedTrack* tt =
293 reinterpret_cast<PlaybackThread::TimedTrack*>(mTrack.get());
294 return tt->setMediaTimeTransform(
295 xform, static_cast<TimedAudioTrack::TargetTimeline>(target));
296}
297
Glenn Kasten3dcd00d2013-07-17 10:10:23 -0700298status_t AudioFlinger::TrackHandle::setParameters(const String8& keyValuePairs) {
299 return mTrack->setParameters(keyValuePairs);
300}
301
Glenn Kasten53cec222013-08-29 09:01:02 -0700302status_t AudioFlinger::TrackHandle::getTimestamp(AudioTimestamp& timestamp)
303{
Glenn Kasten573d80a2013-08-26 09:36:23 -0700304 return mTrack->getTimestamp(timestamp);
Glenn Kasten53cec222013-08-29 09:01:02 -0700305}
306
Eric Laurent59fe0102013-09-27 18:48:26 -0700307
308void AudioFlinger::TrackHandle::signal()
309{
310 return mTrack->signal();
311}
312
Eric Laurent81784c32012-11-19 14:55:58 -0800313status_t AudioFlinger::TrackHandle::onTransact(
314 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
315{
316 return BnAudioTrack::onTransact(code, data, reply, flags);
317}
318
319// ----------------------------------------------------------------------------
320
321// Track constructor must be called with AudioFlinger::mLock and ThreadBase::mLock held
322AudioFlinger::PlaybackThread::Track::Track(
323 PlaybackThread *thread,
324 const sp<Client>& client,
325 audio_stream_type_t streamType,
326 uint32_t sampleRate,
327 audio_format_t format,
328 audio_channel_mask_t channelMask,
329 size_t frameCount,
330 const sp<IMemory>& sharedBuffer,
331 int sessionId,
Marco Nelissen462fd2f2013-01-14 14:12:05 -0800332 int uid,
Eric Laurent81784c32012-11-19 14:55:58 -0800333 IAudioFlinger::track_flags_t flags)
334 : TrackBase(thread, client, sampleRate, format, channelMask, frameCount, sharedBuffer,
Marco Nelissen462fd2f2013-01-14 14:12:05 -0800335 sessionId, uid, true /*isOut*/),
Eric Laurent81784c32012-11-19 14:55:58 -0800336 mFillingUpStatus(FS_INVALID),
337 // mRetryCount initialized later when needed
338 mSharedBuffer(sharedBuffer),
339 mStreamType(streamType),
340 mName(-1), // see note below
341 mMainBuffer(thread->mixBuffer()),
342 mAuxBuffer(NULL),
343 mAuxEffectId(0), mHasVolumeController(false),
344 mPresentationCompleteFrames(0),
345 mFlags(flags),
346 mFastIndex(-1),
Glenn Kasten5736c352012-12-04 12:12:34 -0800347 mCachedVolume(1.0),
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800348 mIsInvalid(false),
Eric Laurentbfb1b832013-01-07 09:53:42 -0800349 mAudioTrackServerProxy(NULL),
Haynes Mathew George7844f672014-01-15 12:32:55 -0800350 mResumeToStopping(false),
351 mFlushHwPending(false)
Eric Laurent81784c32012-11-19 14:55:58 -0800352{
Glenn Kasten3ef14ef2014-03-13 15:08:51 -0700353 if (mCblk == NULL) {
354 return;
Eric Laurent81784c32012-11-19 14:55:58 -0800355 }
Glenn Kasten3ef14ef2014-03-13 15:08:51 -0700356
357 if (sharedBuffer == 0) {
358 mAudioTrackServerProxy = new AudioTrackServerProxy(mCblk, mBuffer, frameCount,
359 mFrameSize);
360 } else {
361 mAudioTrackServerProxy = new StaticAudioTrackServerProxy(mCblk, mBuffer, frameCount,
362 mFrameSize);
363 }
364 mServerProxy = mAudioTrackServerProxy;
365
366 mName = thread->getTrackName_l(channelMask, sessionId);
367 if (mName < 0) {
368 ALOGE("no more track names available");
369 return;
370 }
371 // only allocate a fast track index if we were able to allocate a normal track name
372 if (flags & IAudioFlinger::TRACK_FAST) {
373 mAudioTrackServerProxy->framesReadyIsCalledByMultipleThreads();
374 ALOG_ASSERT(thread->mFastTrackAvailMask != 0);
375 int i = __builtin_ctz(thread->mFastTrackAvailMask);
376 ALOG_ASSERT(0 < i && i < (int)FastMixerState::kMaxFastTracks);
377 // FIXME This is too eager. We allocate a fast track index before the
378 // fast track becomes active. Since fast tracks are a scarce resource,
379 // this means we are potentially denying other more important fast tracks from
380 // being created. It would be better to allocate the index dynamically.
381 mFastIndex = i;
382 // Read the initial underruns because this field is never cleared by the fast mixer
383 mObservedUnderruns = thread->getFastTrackUnderruns(i);
384 thread->mFastTrackAvailMask &= ~(1 << i);
385 }
Eric Laurent81784c32012-11-19 14:55:58 -0800386}
387
388AudioFlinger::PlaybackThread::Track::~Track()
389{
390 ALOGV("PlaybackThread::Track destructor");
Glenn Kasten0c72b242013-09-11 09:14:16 -0700391
392 // The destructor would clear mSharedBuffer,
393 // but it will not push the decremented reference count,
394 // leaving the client's IMemory dangling indefinitely.
395 // This prevents that leak.
396 if (mSharedBuffer != 0) {
397 mSharedBuffer.clear();
398 // flush the binder command buffer
399 IPCThreadState::self()->flushCommands();
400 }
Eric Laurent81784c32012-11-19 14:55:58 -0800401}
402
Glenn Kasten03003332013-08-06 15:40:54 -0700403status_t AudioFlinger::PlaybackThread::Track::initCheck() const
404{
405 status_t status = TrackBase::initCheck();
406 if (status == NO_ERROR && mName < 0) {
407 status = NO_MEMORY;
408 }
409 return status;
410}
411
Eric Laurent81784c32012-11-19 14:55:58 -0800412void AudioFlinger::PlaybackThread::Track::destroy()
413{
414 // NOTE: destroyTrack_l() can remove a strong reference to this Track
415 // by removing it from mTracks vector, so there is a risk that this Tracks's
416 // destructor is called. As the destructor needs to lock mLock,
417 // we must acquire a strong reference on this Track before locking mLock
418 // here so that the destructor is called only when exiting this function.
419 // On the other hand, as long as Track::destroy() is only called by
420 // TrackHandle destructor, the TrackHandle still holds a strong ref on
421 // this Track with its member mTrack.
422 sp<Track> keep(this);
423 { // scope for mLock
424 sp<ThreadBase> thread = mThread.promote();
425 if (thread != 0) {
Eric Laurent81784c32012-11-19 14:55:58 -0800426 Mutex::Autolock _l(thread->mLock);
427 PlaybackThread *playbackThread = (PlaybackThread *)thread.get();
Eric Laurentbfb1b832013-01-07 09:53:42 -0800428 bool wasActive = playbackThread->destroyTrack_l(this);
429 if (!isOutputTrack() && !wasActive) {
430 AudioSystem::releaseOutput(thread->id());
431 }
Eric Laurent81784c32012-11-19 14:55:58 -0800432 }
433 }
434}
435
436/*static*/ void AudioFlinger::PlaybackThread::Track::appendDumpHeader(String8& result)
437{
Marco Nelissenb2208842014-02-07 14:00:50 -0800438 result.append(" Name Active Client Type Fmt Chn mask Session fCount S F SRate "
Glenn Kasten82aaf942013-07-17 16:05:07 -0700439 "L dB R dB Server Main buf Aux Buf Flags UndFrmCnt\n");
Eric Laurent81784c32012-11-19 14:55:58 -0800440}
441
Marco Nelissenb2208842014-02-07 14:00:50 -0800442void AudioFlinger::PlaybackThread::Track::dump(char* buffer, size_t size, bool active)
Eric Laurent81784c32012-11-19 14:55:58 -0800443{
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800444 uint32_t vlr = mAudioTrackServerProxy->getVolumeLR();
Eric Laurent81784c32012-11-19 14:55:58 -0800445 if (isFastTrack()) {
Marco Nelissenb2208842014-02-07 14:00:50 -0800446 sprintf(buffer, " F %2d", mFastIndex);
447 } else if (mName >= AudioMixer::TRACK0) {
448 sprintf(buffer, " %4d", mName - AudioMixer::TRACK0);
Eric Laurent81784c32012-11-19 14:55:58 -0800449 } else {
Marco Nelissenb2208842014-02-07 14:00:50 -0800450 sprintf(buffer, " none");
Eric Laurent81784c32012-11-19 14:55:58 -0800451 }
452 track_state state = mState;
453 char stateChar;
Eric Laurentbfb1b832013-01-07 09:53:42 -0800454 if (isTerminated()) {
Eric Laurent81784c32012-11-19 14:55:58 -0800455 stateChar = 'T';
Eric Laurentbfb1b832013-01-07 09:53:42 -0800456 } else {
457 switch (state) {
458 case IDLE:
459 stateChar = 'I';
460 break;
461 case STOPPING_1:
462 stateChar = 's';
463 break;
464 case STOPPING_2:
465 stateChar = '5';
466 break;
467 case STOPPED:
468 stateChar = 'S';
469 break;
470 case RESUMING:
471 stateChar = 'R';
472 break;
473 case ACTIVE:
474 stateChar = 'A';
475 break;
476 case PAUSING:
477 stateChar = 'p';
478 break;
479 case PAUSED:
480 stateChar = 'P';
481 break;
482 case FLUSHED:
483 stateChar = 'F';
484 break;
485 default:
486 stateChar = '?';
487 break;
488 }
Eric Laurent81784c32012-11-19 14:55:58 -0800489 }
490 char nowInUnderrun;
491 switch (mObservedUnderruns.mBitFields.mMostRecent) {
492 case UNDERRUN_FULL:
493 nowInUnderrun = ' ';
494 break;
495 case UNDERRUN_PARTIAL:
496 nowInUnderrun = '<';
497 break;
498 case UNDERRUN_EMPTY:
499 nowInUnderrun = '*';
500 break;
501 default:
502 nowInUnderrun = '?';
503 break;
504 }
Narayan Kamath1d6fa7a2014-02-11 13:47:53 +0000505 snprintf(&buffer[8], size-8, " %6s %6u %4u %08X %08X %7u %6zu %1c %1d %5u %5.2g %5.2g "
Kévin PETIT377b2ec2014-02-03 12:35:36 +0000506 "%08X %p %p 0x%03X %9u%c\n",
Marco Nelissenb2208842014-02-07 14:00:50 -0800507 active ? "yes" : "no",
Eric Laurent81784c32012-11-19 14:55:58 -0800508 (mClient == 0) ? getpid_cached : mClient->pid(),
509 mStreamType,
510 mFormat,
511 mChannelMask,
512 mSessionId,
Eric Laurent81784c32012-11-19 14:55:58 -0800513 mFrameCount,
514 stateChar,
Eric Laurent81784c32012-11-19 14:55:58 -0800515 mFillingUpStatus,
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800516 mAudioTrackServerProxy->getSampleRate(),
Eric Laurent81784c32012-11-19 14:55:58 -0800517 20.0 * log10((vlr & 0xFFFF) / 4096.0),
518 20.0 * log10((vlr >> 16) / 4096.0),
Glenn Kastenf20e1d82013-07-12 09:45:18 -0700519 mCblk->mServer,
Kévin PETIT377b2ec2014-02-03 12:35:36 +0000520 mMainBuffer,
521 mAuxBuffer,
Glenn Kasten96f60d82013-07-12 10:21:18 -0700522 mCblk->mFlags,
Glenn Kasten82aaf942013-07-17 16:05:07 -0700523 mAudioTrackServerProxy->getUnderrunFrames(),
Eric Laurent81784c32012-11-19 14:55:58 -0800524 nowInUnderrun);
525}
526
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800527uint32_t AudioFlinger::PlaybackThread::Track::sampleRate() const {
528 return mAudioTrackServerProxy->getSampleRate();
529}
530
Eric Laurent81784c32012-11-19 14:55:58 -0800531// AudioBufferProvider interface
532status_t AudioFlinger::PlaybackThread::Track::getNextBuffer(
Glenn Kasten0f11b512014-01-31 16:18:54 -0800533 AudioBufferProvider::Buffer* buffer, int64_t pts __unused)
Eric Laurent81784c32012-11-19 14:55:58 -0800534{
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800535 ServerProxy::Buffer buf;
536 size_t desiredFrames = buffer->frameCount;
537 buf.mFrameCount = desiredFrames;
538 status_t status = mServerProxy->obtainBuffer(&buf);
539 buffer->frameCount = buf.mFrameCount;
540 buffer->raw = buf.mRaw;
541 if (buf.mFrameCount == 0) {
Glenn Kasten82aaf942013-07-17 16:05:07 -0700542 mAudioTrackServerProxy->tallyUnderrunFrames(desiredFrames);
Eric Laurent81784c32012-11-19 14:55:58 -0800543 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800544 return status;
Eric Laurent81784c32012-11-19 14:55:58 -0800545}
546
Glenn Kasten6466c9e2013-08-23 10:54:07 -0700547// releaseBuffer() is not overridden
548
549// ExtendedAudioBufferProvider interface
550
Eric Laurent81784c32012-11-19 14:55:58 -0800551// Note that framesReady() takes a mutex on the control block using tryLock().
552// This could result in priority inversion if framesReady() is called by the normal mixer,
553// as the normal mixer thread runs at lower
554// priority than the client's callback thread: there is a short window within framesReady()
555// during which the normal mixer could be preempted, and the client callback would block.
556// Another problem can occur if framesReady() is called by the fast mixer:
557// the tryLock() could block for up to 1 ms, and a sequence of these could delay fast mixer.
558// FIXME Replace AudioTrackShared control block implementation by a non-blocking FIFO queue.
559size_t AudioFlinger::PlaybackThread::Track::framesReady() const {
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800560 return mAudioTrackServerProxy->framesReady();
Eric Laurent81784c32012-11-19 14:55:58 -0800561}
562
Glenn Kasten6466c9e2013-08-23 10:54:07 -0700563size_t AudioFlinger::PlaybackThread::Track::framesReleased() const
564{
565 return mAudioTrackServerProxy->framesReleased();
566}
567
Eric Laurent81784c32012-11-19 14:55:58 -0800568// Don't call for fast tracks; the framesReady() could result in priority inversion
569bool AudioFlinger::PlaybackThread::Track::isReady() const {
Krishnankutty Kolathappilly8d6c2922014-02-04 16:23:42 -0800570 if (mFillingUpStatus != FS_FILLING || isStopped() || isPausing()) {
571 return true;
572 }
573
Eric Laurent16498512014-03-17 17:22:08 -0700574 if (isStopping()) {
575 if (framesReady() > 0) {
576 mFillingUpStatus = FS_FILLED;
577 }
Eric Laurent81784c32012-11-19 14:55:58 -0800578 return true;
579 }
580
581 if (framesReady() >= mFrameCount ||
Glenn Kasten96f60d82013-07-12 10:21:18 -0700582 (mCblk->mFlags & CBLK_FORCEREADY)) {
Eric Laurent81784c32012-11-19 14:55:58 -0800583 mFillingUpStatus = FS_FILLED;
Glenn Kasten96f60d82013-07-12 10:21:18 -0700584 android_atomic_and(~CBLK_FORCEREADY, &mCblk->mFlags);
Eric Laurent81784c32012-11-19 14:55:58 -0800585 return true;
586 }
587 return false;
588}
589
Glenn Kasten0f11b512014-01-31 16:18:54 -0800590status_t AudioFlinger::PlaybackThread::Track::start(AudioSystem::sync_event_t event __unused,
591 int triggerSession __unused)
Eric Laurent81784c32012-11-19 14:55:58 -0800592{
593 status_t status = NO_ERROR;
594 ALOGV("start(%d), calling pid %d session %d",
595 mName, IPCThreadState::self()->getCallingPid(), mSessionId);
596
597 sp<ThreadBase> thread = mThread.promote();
598 if (thread != 0) {
Eric Laurent813e2a72013-08-31 12:59:48 -0700599 if (isOffloaded()) {
600 Mutex::Autolock _laf(thread->mAudioFlinger->mLock);
601 Mutex::Autolock _lth(thread->mLock);
602 sp<EffectChain> ec = thread->getEffectChain_l(mSessionId);
Eric Laurent5baf2af2013-09-12 17:37:00 -0700603 if (thread->mAudioFlinger->isNonOffloadableGlobalEffectEnabled_l() ||
604 (ec != 0 && ec->isNonOffloadableEnabled())) {
Eric Laurent813e2a72013-08-31 12:59:48 -0700605 invalidate();
606 return PERMISSION_DENIED;
607 }
608 }
609 Mutex::Autolock _lth(thread->mLock);
Eric Laurent81784c32012-11-19 14:55:58 -0800610 track_state state = mState;
611 // here the track could be either new, or restarted
612 // in both cases "unstop" the track
Eric Laurentbfb1b832013-01-07 09:53:42 -0800613
Krishnankutty Kolathappilly8d6c2922014-02-04 16:23:42 -0800614 // initial state-stopping. next state-pausing.
615 // What if resume is called ?
616
617 if (state == PAUSED || state == PAUSING) {
Eric Laurentbfb1b832013-01-07 09:53:42 -0800618 if (mResumeToStopping) {
619 // happened we need to resume to STOPPING_1
620 mState = TrackBase::STOPPING_1;
621 ALOGV("PAUSED => STOPPING_1 (%d) on thread %p", mName, this);
622 } else {
623 mState = TrackBase::RESUMING;
624 ALOGV("PAUSED => RESUMING (%d) on thread %p", mName, this);
625 }
Eric Laurent81784c32012-11-19 14:55:58 -0800626 } else {
627 mState = TrackBase::ACTIVE;
628 ALOGV("? => ACTIVE (%d) on thread %p", mName, this);
629 }
630
Eric Laurentbfb1b832013-01-07 09:53:42 -0800631 PlaybackThread *playbackThread = (PlaybackThread *)thread.get();
632 status = playbackThread->addTrack_l(this);
633 if (status == INVALID_OPERATION || status == PERMISSION_DENIED) {
Eric Laurent81784c32012-11-19 14:55:58 -0800634 triggerEvents(AudioSystem::SYNC_EVENT_PRESENTATION_COMPLETE);
Eric Laurentbfb1b832013-01-07 09:53:42 -0800635 // restore previous state if start was rejected by policy manager
636 if (status == PERMISSION_DENIED) {
637 mState = state;
638 }
639 }
640 // track was already in the active list, not a problem
641 if (status == ALREADY_EXISTS) {
642 status = NO_ERROR;
Glenn Kasten12022ff2013-10-17 11:32:39 -0700643 } else {
644 // Acknowledge any pending flush(), so that subsequent new data isn't discarded.
645 // It is usually unsafe to access the server proxy from a binder thread.
646 // But in this case we know the mixer thread (whether normal mixer or fast mixer)
647 // isn't looking at this track yet: we still hold the normal mixer thread lock,
648 // and for fast tracks the track is not yet in the fast mixer thread's active set.
649 ServerProxy::Buffer buffer;
650 buffer.mFrameCount = 1;
Glenn Kasten2e422c42013-10-18 13:00:29 -0700651 (void) mAudioTrackServerProxy->obtainBuffer(&buffer, true /*ackFlush*/);
Eric Laurent81784c32012-11-19 14:55:58 -0800652 }
653 } else {
654 status = BAD_VALUE;
655 }
656 return status;
657}
658
659void AudioFlinger::PlaybackThread::Track::stop()
660{
661 ALOGV("stop(%d), calling pid %d", mName, IPCThreadState::self()->getCallingPid());
662 sp<ThreadBase> thread = mThread.promote();
663 if (thread != 0) {
664 Mutex::Autolock _l(thread->mLock);
665 track_state state = mState;
666 if (state == RESUMING || state == ACTIVE || state == PAUSING || state == PAUSED) {
667 // If the track is not active (PAUSED and buffers full), flush buffers
668 PlaybackThread *playbackThread = (PlaybackThread *)thread.get();
669 if (playbackThread->mActiveTracks.indexOf(this) < 0) {
670 reset();
671 mState = STOPPED;
Eric Laurentbfb1b832013-01-07 09:53:42 -0800672 } else if (!isFastTrack() && !isOffloaded()) {
Eric Laurent81784c32012-11-19 14:55:58 -0800673 mState = STOPPED;
674 } else {
Eric Laurentbfb1b832013-01-07 09:53:42 -0800675 // For fast tracks prepareTracks_l() will set state to STOPPING_2
676 // presentation is complete
677 // For an offloaded track this starts a drain and state will
678 // move to STOPPING_2 when drain completes and then STOPPED
Eric Laurent81784c32012-11-19 14:55:58 -0800679 mState = STOPPING_1;
680 }
681 ALOGV("not stopping/stopped => stopping/stopped (%d) on thread %p", mName,
682 playbackThread);
683 }
Eric Laurent81784c32012-11-19 14:55:58 -0800684 }
685}
686
687void AudioFlinger::PlaybackThread::Track::pause()
688{
689 ALOGV("pause(%d), calling pid %d", mName, IPCThreadState::self()->getCallingPid());
690 sp<ThreadBase> thread = mThread.promote();
691 if (thread != 0) {
692 Mutex::Autolock _l(thread->mLock);
Eric Laurentbfb1b832013-01-07 09:53:42 -0800693 PlaybackThread *playbackThread = (PlaybackThread *)thread.get();
694 switch (mState) {
695 case STOPPING_1:
696 case STOPPING_2:
697 if (!isOffloaded()) {
698 /* nothing to do if track is not offloaded */
699 break;
700 }
701
702 // Offloaded track was draining, we need to carry on draining when resumed
703 mResumeToStopping = true;
704 // fall through...
705 case ACTIVE:
706 case RESUMING:
Eric Laurent81784c32012-11-19 14:55:58 -0800707 mState = PAUSING;
708 ALOGV("ACTIVE/RESUMING => PAUSING (%d) on thread %p", mName, thread.get());
Eric Laurentede6c3b2013-09-19 14:37:46 -0700709 playbackThread->broadcast_l();
Eric Laurentbfb1b832013-01-07 09:53:42 -0800710 break;
Eric Laurent81784c32012-11-19 14:55:58 -0800711
Eric Laurentbfb1b832013-01-07 09:53:42 -0800712 default:
713 break;
Eric Laurent81784c32012-11-19 14:55:58 -0800714 }
715 }
716}
717
718void AudioFlinger::PlaybackThread::Track::flush()
719{
720 ALOGV("flush(%d)", mName);
721 sp<ThreadBase> thread = mThread.promote();
722 if (thread != 0) {
723 Mutex::Autolock _l(thread->mLock);
Eric Laurent81784c32012-11-19 14:55:58 -0800724 PlaybackThread *playbackThread = (PlaybackThread *)thread.get();
Eric Laurentbfb1b832013-01-07 09:53:42 -0800725
726 if (isOffloaded()) {
727 // If offloaded we allow flush during any state except terminated
728 // and keep the track active to avoid problems if user is seeking
729 // rapidly and underlying hardware has a significant delay handling
730 // a pause
731 if (isTerminated()) {
732 return;
733 }
734
735 ALOGV("flush: offload flush");
Eric Laurent81784c32012-11-19 14:55:58 -0800736 reset();
Eric Laurentbfb1b832013-01-07 09:53:42 -0800737
738 if (mState == STOPPING_1 || mState == STOPPING_2) {
739 ALOGV("flushed in STOPPING_1 or 2 state, change state to ACTIVE");
740 mState = ACTIVE;
741 }
742
743 if (mState == ACTIVE) {
744 ALOGV("flush called in active state, resetting buffer time out retry count");
745 mRetryCount = PlaybackThread::kMaxTrackRetriesOffload;
746 }
747
Haynes Mathew George7844f672014-01-15 12:32:55 -0800748 mFlushHwPending = true;
Eric Laurentbfb1b832013-01-07 09:53:42 -0800749 mResumeToStopping = false;
750 } else {
751 if (mState != STOPPING_1 && mState != STOPPING_2 && mState != STOPPED &&
752 mState != PAUSED && mState != PAUSING && mState != IDLE && mState != FLUSHED) {
753 return;
754 }
755 // No point remaining in PAUSED state after a flush => go to
756 // FLUSHED state
757 mState = FLUSHED;
758 // do not reset the track if it is still in the process of being stopped or paused.
759 // this will be done by prepareTracks_l() when the track is stopped.
760 // prepareTracks_l() will see mState == FLUSHED, then
761 // remove from active track list, reset(), and trigger presentation complete
762 if (playbackThread->mActiveTracks.indexOf(this) < 0) {
763 reset();
764 }
Eric Laurent81784c32012-11-19 14:55:58 -0800765 }
Eric Laurentbfb1b832013-01-07 09:53:42 -0800766 // Prevent flush being lost if the track is flushed and then resumed
767 // before mixer thread can run. This is important when offloading
768 // because the hardware buffer could hold a large amount of audio
Eric Laurentede6c3b2013-09-19 14:37:46 -0700769 playbackThread->broadcast_l();
Eric Laurent81784c32012-11-19 14:55:58 -0800770 }
771}
772
Haynes Mathew George7844f672014-01-15 12:32:55 -0800773// must be called with thread lock held
774void AudioFlinger::PlaybackThread::Track::flushAck()
775{
776 if (!isOffloaded())
777 return;
778
779 mFlushHwPending = false;
780}
781
Eric Laurent81784c32012-11-19 14:55:58 -0800782void AudioFlinger::PlaybackThread::Track::reset()
783{
784 // Do not reset twice to avoid discarding data written just after a flush and before
785 // the audioflinger thread detects the track is stopped.
786 if (!mResetDone) {
Eric Laurent81784c32012-11-19 14:55:58 -0800787 // Force underrun condition to avoid false underrun callback until first data is
788 // written to buffer
Glenn Kasten96f60d82013-07-12 10:21:18 -0700789 android_atomic_and(~CBLK_FORCEREADY, &mCblk->mFlags);
Eric Laurent81784c32012-11-19 14:55:58 -0800790 mFillingUpStatus = FS_FILLING;
791 mResetDone = true;
792 if (mState == FLUSHED) {
793 mState = IDLE;
794 }
795 }
796}
797
Eric Laurentbfb1b832013-01-07 09:53:42 -0800798status_t AudioFlinger::PlaybackThread::Track::setParameters(const String8& keyValuePairs)
799{
800 sp<ThreadBase> thread = mThread.promote();
801 if (thread == 0) {
802 ALOGE("thread is dead");
803 return FAILED_TRANSACTION;
804 } else if ((thread->type() == ThreadBase::DIRECT) ||
805 (thread->type() == ThreadBase::OFFLOAD)) {
806 return thread->setParameters(keyValuePairs);
807 } else {
808 return PERMISSION_DENIED;
809 }
810}
811
Glenn Kasten573d80a2013-08-26 09:36:23 -0700812status_t AudioFlinger::PlaybackThread::Track::getTimestamp(AudioTimestamp& timestamp)
813{
Glenn Kastenfe346c72013-08-30 13:28:22 -0700814 // Client should implement this using SSQ; the unpresented frame count in latch is irrelevant
815 if (isFastTrack()) {
816 return INVALID_OPERATION;
817 }
Glenn Kasten573d80a2013-08-26 09:36:23 -0700818 sp<ThreadBase> thread = mThread.promote();
819 if (thread == 0) {
Glenn Kastenfe346c72013-08-30 13:28:22 -0700820 return INVALID_OPERATION;
Glenn Kasten573d80a2013-08-26 09:36:23 -0700821 }
822 Mutex::Autolock _l(thread->mLock);
823 PlaybackThread *playbackThread = (PlaybackThread *)thread.get();
Eric Laurentaccc1472013-09-20 09:36:34 -0700824 if (!isOffloaded()) {
825 if (!playbackThread->mLatchQValid) {
826 return INVALID_OPERATION;
827 }
828 uint32_t unpresentedFrames =
829 ((int64_t) playbackThread->mLatchQ.mUnpresentedFrames * mSampleRate) /
830 playbackThread->mSampleRate;
831 uint32_t framesWritten = mAudioTrackServerProxy->framesReleased();
832 if (framesWritten < unpresentedFrames) {
833 return INVALID_OPERATION;
834 }
835 timestamp.mPosition = framesWritten - unpresentedFrames;
836 timestamp.mTime = playbackThread->mLatchQ.mTimestamp.mTime;
837 return NO_ERROR;
Glenn Kastenbd096fd2013-08-23 13:53:56 -0700838 }
Eric Laurentaccc1472013-09-20 09:36:34 -0700839
840 return playbackThread->getTimestamp_l(timestamp);
Glenn Kasten573d80a2013-08-26 09:36:23 -0700841}
842
Eric Laurent81784c32012-11-19 14:55:58 -0800843status_t AudioFlinger::PlaybackThread::Track::attachAuxEffect(int EffectId)
844{
845 status_t status = DEAD_OBJECT;
846 sp<ThreadBase> thread = mThread.promote();
847 if (thread != 0) {
848 PlaybackThread *playbackThread = (PlaybackThread *)thread.get();
849 sp<AudioFlinger> af = mClient->audioFlinger();
850
851 Mutex::Autolock _l(af->mLock);
852
853 sp<PlaybackThread> srcThread = af->getEffectThread_l(AUDIO_SESSION_OUTPUT_MIX, EffectId);
854
855 if (EffectId != 0 && srcThread != 0 && playbackThread != srcThread.get()) {
856 Mutex::Autolock _dl(playbackThread->mLock);
857 Mutex::Autolock _sl(srcThread->mLock);
858 sp<EffectChain> chain = srcThread->getEffectChain_l(AUDIO_SESSION_OUTPUT_MIX);
859 if (chain == 0) {
860 return INVALID_OPERATION;
861 }
862
863 sp<EffectModule> effect = chain->getEffectFromId_l(EffectId);
864 if (effect == 0) {
865 return INVALID_OPERATION;
866 }
867 srcThread->removeEffect_l(effect);
Eric Laurent5baf2af2013-09-12 17:37:00 -0700868 status = playbackThread->addEffect_l(effect);
869 if (status != NO_ERROR) {
870 srcThread->addEffect_l(effect);
871 return INVALID_OPERATION;
872 }
Eric Laurent81784c32012-11-19 14:55:58 -0800873 // removeEffect_l() has stopped the effect if it was active so it must be restarted
874 if (effect->state() == EffectModule::ACTIVE ||
875 effect->state() == EffectModule::STOPPING) {
876 effect->start();
877 }
878
879 sp<EffectChain> dstChain = effect->chain().promote();
880 if (dstChain == 0) {
881 srcThread->addEffect_l(effect);
882 return INVALID_OPERATION;
883 }
884 AudioSystem::unregisterEffect(effect->id());
885 AudioSystem::registerEffect(&effect->desc(),
886 srcThread->id(),
887 dstChain->strategy(),
888 AUDIO_SESSION_OUTPUT_MIX,
889 effect->id());
Eric Laurentd72b7c02013-10-12 16:17:46 -0700890 AudioSystem::setEffectEnabled(effect->id(), effect->isEnabled());
Eric Laurent81784c32012-11-19 14:55:58 -0800891 }
892 status = playbackThread->attachAuxEffect(this, EffectId);
893 }
894 return status;
895}
896
897void AudioFlinger::PlaybackThread::Track::setAuxBuffer(int EffectId, int32_t *buffer)
898{
899 mAuxEffectId = EffectId;
900 mAuxBuffer = buffer;
901}
902
903bool AudioFlinger::PlaybackThread::Track::presentationComplete(size_t framesWritten,
904 size_t audioHalFrames)
905{
906 // a track is considered presented when the total number of frames written to audio HAL
907 // corresponds to the number of frames written when presentationComplete() is called for the
908 // first time (mPresentationCompleteFrames == 0) plus the buffer filling status at that time.
Eric Laurentbfb1b832013-01-07 09:53:42 -0800909 // For an offloaded track the HAL+h/w delay is variable so a HAL drain() is used
910 // to detect when all frames have been played. In this case framesWritten isn't
911 // useful because it doesn't always reflect whether there is data in the h/w
912 // buffers, particularly if a track has been paused and resumed during draining
913 ALOGV("presentationComplete() mPresentationCompleteFrames %d framesWritten %d",
914 mPresentationCompleteFrames, framesWritten);
Eric Laurent81784c32012-11-19 14:55:58 -0800915 if (mPresentationCompleteFrames == 0) {
916 mPresentationCompleteFrames = framesWritten + audioHalFrames;
917 ALOGV("presentationComplete() reset: mPresentationCompleteFrames %d audioHalFrames %d",
918 mPresentationCompleteFrames, audioHalFrames);
919 }
Eric Laurentbfb1b832013-01-07 09:53:42 -0800920
921 if (framesWritten >= mPresentationCompleteFrames || isOffloaded()) {
Eric Laurent81784c32012-11-19 14:55:58 -0800922 ALOGV("presentationComplete() session %d complete: framesWritten %d",
923 mSessionId, framesWritten);
924 triggerEvents(AudioSystem::SYNC_EVENT_PRESENTATION_COMPLETE);
Eric Laurentbfb1b832013-01-07 09:53:42 -0800925 mAudioTrackServerProxy->setStreamEndDone();
Eric Laurent81784c32012-11-19 14:55:58 -0800926 return true;
927 }
928 return false;
929}
930
931void AudioFlinger::PlaybackThread::Track::triggerEvents(AudioSystem::sync_event_t type)
932{
933 for (int i = 0; i < (int)mSyncEvents.size(); i++) {
934 if (mSyncEvents[i]->type() == type) {
935 mSyncEvents[i]->trigger();
936 mSyncEvents.removeAt(i);
937 i--;
938 }
939 }
940}
941
942// implement VolumeBufferProvider interface
943
944uint32_t AudioFlinger::PlaybackThread::Track::getVolumeLR()
945{
946 // called by FastMixer, so not allowed to take any locks, block, or do I/O including logs
947 ALOG_ASSERT(isFastTrack() && (mCblk != NULL));
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800948 uint32_t vlr = mAudioTrackServerProxy->getVolumeLR();
Eric Laurent81784c32012-11-19 14:55:58 -0800949 uint32_t vl = vlr & 0xFFFF;
950 uint32_t vr = vlr >> 16;
951 // track volumes come from shared memory, so can't be trusted and must be clamped
952 if (vl > MAX_GAIN_INT) {
953 vl = MAX_GAIN_INT;
954 }
955 if (vr > MAX_GAIN_INT) {
956 vr = MAX_GAIN_INT;
957 }
958 // now apply the cached master volume and stream type volume;
959 // this is trusted but lacks any synchronization or barrier so may be stale
960 float v = mCachedVolume;
961 vl *= v;
962 vr *= v;
963 // re-combine into U4.16
964 vlr = (vr << 16) | (vl & 0xFFFF);
965 // FIXME look at mute, pause, and stop flags
966 return vlr;
967}
968
969status_t AudioFlinger::PlaybackThread::Track::setSyncEvent(const sp<SyncEvent>& event)
970{
Eric Laurentbfb1b832013-01-07 09:53:42 -0800971 if (isTerminated() || mState == PAUSED ||
Eric Laurent81784c32012-11-19 14:55:58 -0800972 ((framesReady() == 0) && ((mSharedBuffer != 0) ||
973 (mState == STOPPED)))) {
974 ALOGW("Track::setSyncEvent() in invalid state %d on session %d %s mode, framesReady %d ",
975 mState, mSessionId, (mSharedBuffer != 0) ? "static" : "stream", framesReady());
976 event->cancel();
977 return INVALID_OPERATION;
978 }
979 (void) TrackBase::setSyncEvent(event);
980 return NO_ERROR;
981}
982
Glenn Kasten5736c352012-12-04 12:12:34 -0800983void AudioFlinger::PlaybackThread::Track::invalidate()
984{
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800985 // FIXME should use proxy, and needs work
986 audio_track_cblk_t* cblk = mCblk;
Glenn Kasten96f60d82013-07-12 10:21:18 -0700987 android_atomic_or(CBLK_INVALID, &cblk->mFlags);
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800988 android_atomic_release_store(0x40000000, &cblk->mFutex);
989 // client is not in server, so FUTEX_WAKE is needed instead of FUTEX_WAKE_PRIVATE
990 (void) __futex_syscall3(&cblk->mFutex, FUTEX_WAKE, INT_MAX);
Glenn Kasten5736c352012-12-04 12:12:34 -0800991 mIsInvalid = true;
992}
993
Eric Laurent59fe0102013-09-27 18:48:26 -0700994void AudioFlinger::PlaybackThread::Track::signal()
995{
996 sp<ThreadBase> thread = mThread.promote();
997 if (thread != 0) {
998 PlaybackThread *t = (PlaybackThread *)thread.get();
999 Mutex::Autolock _l(t->mLock);
1000 t->broadcast_l();
1001 }
1002}
1003
Krishnankutty Kolathappilly8d6c2922014-02-04 16:23:42 -08001004//To be called with thread lock held
1005bool AudioFlinger::PlaybackThread::Track::isResumePending() {
1006
1007 if (mState == RESUMING)
1008 return true;
1009 /* Resume is pending if track was stopping before pause was called */
1010 if (mState == STOPPING_1 &&
1011 mResumeToStopping)
1012 return true;
1013
1014 return false;
1015}
1016
1017//To be called with thread lock held
1018void AudioFlinger::PlaybackThread::Track::resumeAck() {
1019
1020
1021 if (mState == RESUMING)
1022 mState = ACTIVE;
Haynes Mathew George2d3ca682014-03-07 13:43:49 -08001023
Krishnankutty Kolathappilly8d6c2922014-02-04 16:23:42 -08001024 // Other possibility of pending resume is stopping_1 state
1025 // Do not update the state from stopping as this prevents
Haynes Mathew George2d3ca682014-03-07 13:43:49 -08001026 // drain being called.
1027 if (mState == STOPPING_1) {
1028 mResumeToStopping = false;
1029 }
Krishnankutty Kolathappilly8d6c2922014-02-04 16:23:42 -08001030}
Eric Laurent81784c32012-11-19 14:55:58 -08001031// ----------------------------------------------------------------------------
1032
1033sp<AudioFlinger::PlaybackThread::TimedTrack>
1034AudioFlinger::PlaybackThread::TimedTrack::create(
1035 PlaybackThread *thread,
1036 const sp<Client>& client,
1037 audio_stream_type_t streamType,
1038 uint32_t sampleRate,
1039 audio_format_t format,
1040 audio_channel_mask_t channelMask,
1041 size_t frameCount,
1042 const sp<IMemory>& sharedBuffer,
Marco Nelissen462fd2f2013-01-14 14:12:05 -08001043 int sessionId,
Glenn Kasten4944acb2013-08-19 08:39:20 -07001044 int uid)
1045{
Eric Laurent81784c32012-11-19 14:55:58 -08001046 if (!client->reserveTimedTrack())
1047 return 0;
1048
1049 return new TimedTrack(
1050 thread, client, streamType, sampleRate, format, channelMask, frameCount,
Marco Nelissen462fd2f2013-01-14 14:12:05 -08001051 sharedBuffer, sessionId, uid);
Eric Laurent81784c32012-11-19 14:55:58 -08001052}
1053
1054AudioFlinger::PlaybackThread::TimedTrack::TimedTrack(
1055 PlaybackThread *thread,
1056 const sp<Client>& client,
1057 audio_stream_type_t streamType,
1058 uint32_t sampleRate,
1059 audio_format_t format,
1060 audio_channel_mask_t channelMask,
1061 size_t frameCount,
1062 const sp<IMemory>& sharedBuffer,
Marco Nelissen462fd2f2013-01-14 14:12:05 -08001063 int sessionId,
1064 int uid)
Eric Laurent81784c32012-11-19 14:55:58 -08001065 : Track(thread, client, streamType, sampleRate, format, channelMask,
Marco Nelissen462fd2f2013-01-14 14:12:05 -08001066 frameCount, sharedBuffer, sessionId, uid, IAudioFlinger::TRACK_TIMED),
Eric Laurent81784c32012-11-19 14:55:58 -08001067 mQueueHeadInFlight(false),
1068 mTrimQueueHeadOnRelease(false),
1069 mFramesPendingInQueue(0),
1070 mTimedSilenceBuffer(NULL),
1071 mTimedSilenceBufferSize(0),
1072 mTimedAudioOutputOnTime(false),
1073 mMediaTimeTransformValid(false)
1074{
1075 LocalClock lc;
1076 mLocalTimeFreq = lc.getLocalFreq();
1077
1078 mLocalTimeToSampleTransform.a_zero = 0;
1079 mLocalTimeToSampleTransform.b_zero = 0;
1080 mLocalTimeToSampleTransform.a_to_b_numer = sampleRate;
1081 mLocalTimeToSampleTransform.a_to_b_denom = mLocalTimeFreq;
1082 LinearTransform::reduce(&mLocalTimeToSampleTransform.a_to_b_numer,
1083 &mLocalTimeToSampleTransform.a_to_b_denom);
1084
1085 mMediaTimeToSampleTransform.a_zero = 0;
1086 mMediaTimeToSampleTransform.b_zero = 0;
1087 mMediaTimeToSampleTransform.a_to_b_numer = sampleRate;
1088 mMediaTimeToSampleTransform.a_to_b_denom = 1000000;
1089 LinearTransform::reduce(&mMediaTimeToSampleTransform.a_to_b_numer,
1090 &mMediaTimeToSampleTransform.a_to_b_denom);
1091}
1092
1093AudioFlinger::PlaybackThread::TimedTrack::~TimedTrack() {
1094 mClient->releaseTimedTrack();
1095 delete [] mTimedSilenceBuffer;
1096}
1097
1098status_t AudioFlinger::PlaybackThread::TimedTrack::allocateTimedBuffer(
1099 size_t size, sp<IMemory>* buffer) {
1100
1101 Mutex::Autolock _l(mTimedBufferQueueLock);
1102
1103 trimTimedBufferQueue_l();
1104
1105 // lazily initialize the shared memory heap for timed buffers
1106 if (mTimedMemoryDealer == NULL) {
1107 const int kTimedBufferHeapSize = 512 << 10;
1108
1109 mTimedMemoryDealer = new MemoryDealer(kTimedBufferHeapSize,
1110 "AudioFlingerTimed");
Glenn Kasten6e2ebe92013-08-13 09:14:51 -07001111 if (mTimedMemoryDealer == NULL) {
Eric Laurent81784c32012-11-19 14:55:58 -08001112 return NO_MEMORY;
Glenn Kasten6e2ebe92013-08-13 09:14:51 -07001113 }
Eric Laurent81784c32012-11-19 14:55:58 -08001114 }
1115
1116 sp<IMemory> newBuffer = mTimedMemoryDealer->allocate(size);
Glenn Kasten663c2242013-09-24 11:52:37 -07001117 if (newBuffer == 0 || newBuffer->pointer() == NULL) {
Glenn Kasten30ff92c2013-11-20 11:57:08 -08001118 return NO_MEMORY;
Eric Laurent81784c32012-11-19 14:55:58 -08001119 }
1120
1121 *buffer = newBuffer;
1122 return NO_ERROR;
1123}
1124
1125// caller must hold mTimedBufferQueueLock
1126void AudioFlinger::PlaybackThread::TimedTrack::trimTimedBufferQueue_l() {
1127 int64_t mediaTimeNow;
1128 {
1129 Mutex::Autolock mttLock(mMediaTimeTransformLock);
1130 if (!mMediaTimeTransformValid)
1131 return;
1132
1133 int64_t targetTimeNow;
1134 status_t res = (mMediaTimeTransformTarget == TimedAudioTrack::COMMON_TIME)
1135 ? mCCHelper.getCommonTime(&targetTimeNow)
1136 : mCCHelper.getLocalTime(&targetTimeNow);
1137
1138 if (OK != res)
1139 return;
1140
1141 if (!mMediaTimeTransform.doReverseTransform(targetTimeNow,
1142 &mediaTimeNow)) {
1143 return;
1144 }
1145 }
1146
1147 size_t trimEnd;
1148 for (trimEnd = 0; trimEnd < mTimedBufferQueue.size(); trimEnd++) {
1149 int64_t bufEnd;
1150
1151 if ((trimEnd + 1) < mTimedBufferQueue.size()) {
1152 // We have a next buffer. Just use its PTS as the PTS of the frame
1153 // following the last frame in this buffer. If the stream is sparse
1154 // (ie, there are deliberate gaps left in the stream which should be
1155 // filled with silence by the TimedAudioTrack), then this can result
1156 // in one extra buffer being left un-trimmed when it could have
1157 // been. In general, this is not typical, and we would rather
1158 // optimized away the TS calculation below for the more common case
1159 // where PTSes are contiguous.
1160 bufEnd = mTimedBufferQueue[trimEnd + 1].pts();
1161 } else {
1162 // We have no next buffer. Compute the PTS of the frame following
1163 // the last frame in this buffer by computing the duration of of
1164 // this frame in media time units and adding it to the PTS of the
1165 // buffer.
1166 int64_t frameCount = mTimedBufferQueue[trimEnd].buffer()->size()
1167 / mFrameSize;
1168
1169 if (!mMediaTimeToSampleTransform.doReverseTransform(frameCount,
1170 &bufEnd)) {
1171 ALOGE("Failed to convert frame count of %lld to media time"
1172 " duration" " (scale factor %d/%u) in %s",
1173 frameCount,
1174 mMediaTimeToSampleTransform.a_to_b_numer,
1175 mMediaTimeToSampleTransform.a_to_b_denom,
1176 __PRETTY_FUNCTION__);
1177 break;
1178 }
1179 bufEnd += mTimedBufferQueue[trimEnd].pts();
1180 }
1181
1182 if (bufEnd > mediaTimeNow)
1183 break;
1184
1185 // Is the buffer we want to use in the middle of a mix operation right
1186 // now? If so, don't actually trim it. Just wait for the releaseBuffer
1187 // from the mixer which should be coming back shortly.
1188 if (!trimEnd && mQueueHeadInFlight) {
1189 mTrimQueueHeadOnRelease = true;
1190 }
1191 }
1192
1193 size_t trimStart = mTrimQueueHeadOnRelease ? 1 : 0;
1194 if (trimStart < trimEnd) {
1195 // Update the bookkeeping for framesReady()
1196 for (size_t i = trimStart; i < trimEnd; ++i) {
1197 updateFramesPendingAfterTrim_l(mTimedBufferQueue[i], "trim");
1198 }
1199
1200 // Now actually remove the buffers from the queue.
1201 mTimedBufferQueue.removeItemsAt(trimStart, trimEnd);
1202 }
1203}
1204
1205void AudioFlinger::PlaybackThread::TimedTrack::trimTimedBufferQueueHead_l(
1206 const char* logTag) {
1207 ALOG_ASSERT(mTimedBufferQueue.size() > 0,
1208 "%s called (reason \"%s\"), but timed buffer queue has no"
1209 " elements to trim.", __FUNCTION__, logTag);
1210
1211 updateFramesPendingAfterTrim_l(mTimedBufferQueue[0], logTag);
1212 mTimedBufferQueue.removeAt(0);
1213}
1214
1215void AudioFlinger::PlaybackThread::TimedTrack::updateFramesPendingAfterTrim_l(
1216 const TimedBuffer& buf,
Glenn Kasten0f11b512014-01-31 16:18:54 -08001217 const char* logTag __unused) {
Eric Laurent81784c32012-11-19 14:55:58 -08001218 uint32_t bufBytes = buf.buffer()->size();
1219 uint32_t consumedAlready = buf.position();
1220
1221 ALOG_ASSERT(consumedAlready <= bufBytes,
1222 "Bad bookkeeping while updating frames pending. Timed buffer is"
1223 " only %u bytes long, but claims to have consumed %u"
1224 " bytes. (update reason: \"%s\")",
1225 bufBytes, consumedAlready, logTag);
1226
1227 uint32_t bufFrames = (bufBytes - consumedAlready) / mFrameSize;
1228 ALOG_ASSERT(mFramesPendingInQueue >= bufFrames,
1229 "Bad bookkeeping while updating frames pending. Should have at"
1230 " least %u queued frames, but we think we have only %u. (update"
1231 " reason: \"%s\")",
1232 bufFrames, mFramesPendingInQueue, logTag);
1233
1234 mFramesPendingInQueue -= bufFrames;
1235}
1236
1237status_t AudioFlinger::PlaybackThread::TimedTrack::queueTimedBuffer(
1238 const sp<IMemory>& buffer, int64_t pts) {
1239
1240 {
1241 Mutex::Autolock mttLock(mMediaTimeTransformLock);
1242 if (!mMediaTimeTransformValid)
1243 return INVALID_OPERATION;
1244 }
1245
1246 Mutex::Autolock _l(mTimedBufferQueueLock);
1247
1248 uint32_t bufFrames = buffer->size() / mFrameSize;
1249 mFramesPendingInQueue += bufFrames;
1250 mTimedBufferQueue.add(TimedBuffer(buffer, pts));
1251
1252 return NO_ERROR;
1253}
1254
1255status_t AudioFlinger::PlaybackThread::TimedTrack::setMediaTimeTransform(
1256 const LinearTransform& xform, TimedAudioTrack::TargetTimeline target) {
1257
1258 ALOGVV("setMediaTimeTransform az=%lld bz=%lld n=%d d=%u tgt=%d",
1259 xform.a_zero, xform.b_zero, xform.a_to_b_numer, xform.a_to_b_denom,
1260 target);
1261
1262 if (!(target == TimedAudioTrack::LOCAL_TIME ||
1263 target == TimedAudioTrack::COMMON_TIME)) {
1264 return BAD_VALUE;
1265 }
1266
1267 Mutex::Autolock lock(mMediaTimeTransformLock);
1268 mMediaTimeTransform = xform;
1269 mMediaTimeTransformTarget = target;
1270 mMediaTimeTransformValid = true;
1271
1272 return NO_ERROR;
1273}
1274
1275#define min(a, b) ((a) < (b) ? (a) : (b))
1276
1277// implementation of getNextBuffer for tracks whose buffers have timestamps
1278status_t AudioFlinger::PlaybackThread::TimedTrack::getNextBuffer(
1279 AudioBufferProvider::Buffer* buffer, int64_t pts)
1280{
1281 if (pts == AudioBufferProvider::kInvalidPTS) {
1282 buffer->raw = NULL;
1283 buffer->frameCount = 0;
1284 mTimedAudioOutputOnTime = false;
1285 return INVALID_OPERATION;
1286 }
1287
1288 Mutex::Autolock _l(mTimedBufferQueueLock);
1289
1290 ALOG_ASSERT(!mQueueHeadInFlight,
1291 "getNextBuffer called without releaseBuffer!");
1292
1293 while (true) {
1294
1295 // if we have no timed buffers, then fail
1296 if (mTimedBufferQueue.isEmpty()) {
1297 buffer->raw = NULL;
1298 buffer->frameCount = 0;
1299 return NOT_ENOUGH_DATA;
1300 }
1301
1302 TimedBuffer& head = mTimedBufferQueue.editItemAt(0);
1303
1304 // calculate the PTS of the head of the timed buffer queue expressed in
1305 // local time
1306 int64_t headLocalPTS;
1307 {
1308 Mutex::Autolock mttLock(mMediaTimeTransformLock);
1309
1310 ALOG_ASSERT(mMediaTimeTransformValid, "media time transform invalid");
1311
1312 if (mMediaTimeTransform.a_to_b_denom == 0) {
1313 // the transform represents a pause, so yield silence
1314 timedYieldSilence_l(buffer->frameCount, buffer);
1315 return NO_ERROR;
1316 }
1317
1318 int64_t transformedPTS;
1319 if (!mMediaTimeTransform.doForwardTransform(head.pts(),
1320 &transformedPTS)) {
1321 // the transform failed. this shouldn't happen, but if it does
1322 // then just drop this buffer
1323 ALOGW("timedGetNextBuffer transform failed");
1324 buffer->raw = NULL;
1325 buffer->frameCount = 0;
1326 trimTimedBufferQueueHead_l("getNextBuffer; no transform");
1327 return NO_ERROR;
1328 }
1329
1330 if (mMediaTimeTransformTarget == TimedAudioTrack::COMMON_TIME) {
1331 if (OK != mCCHelper.commonTimeToLocalTime(transformedPTS,
1332 &headLocalPTS)) {
1333 buffer->raw = NULL;
1334 buffer->frameCount = 0;
1335 return INVALID_OPERATION;
1336 }
1337 } else {
1338 headLocalPTS = transformedPTS;
1339 }
1340 }
1341
Glenn Kasten9fdcb0a2013-06-26 16:11:36 -07001342 uint32_t sr = sampleRate();
1343
Eric Laurent81784c32012-11-19 14:55:58 -08001344 // adjust the head buffer's PTS to reflect the portion of the head buffer
1345 // that has already been consumed
1346 int64_t effectivePTS = headLocalPTS +
Glenn Kasten9fdcb0a2013-06-26 16:11:36 -07001347 ((head.position() / mFrameSize) * mLocalTimeFreq / sr);
Eric Laurent81784c32012-11-19 14:55:58 -08001348
1349 // Calculate the delta in samples between the head of the input buffer
1350 // queue and the start of the next output buffer that will be written.
1351 // If the transformation fails because of over or underflow, it means
1352 // that the sample's position in the output stream is so far out of
1353 // whack that it should just be dropped.
1354 int64_t sampleDelta;
1355 if (llabs(effectivePTS - pts) >= (static_cast<int64_t>(1) << 31)) {
1356 ALOGV("*** head buffer is too far from PTS: dropped buffer");
1357 trimTimedBufferQueueHead_l("getNextBuffer, buf pts too far from"
1358 " mix");
1359 continue;
1360 }
1361 if (!mLocalTimeToSampleTransform.doForwardTransform(
1362 (effectivePTS - pts) << 32, &sampleDelta)) {
1363 ALOGV("*** too late during sample rate transform: dropped buffer");
1364 trimTimedBufferQueueHead_l("getNextBuffer, bad local to sample");
1365 continue;
1366 }
1367
1368 ALOGVV("*** getNextBuffer head.pts=%lld head.pos=%d pts=%lld"
1369 " sampleDelta=[%d.%08x]",
1370 head.pts(), head.position(), pts,
1371 static_cast<int32_t>((sampleDelta >= 0 ? 0 : 1)
1372 + (sampleDelta >> 32)),
1373 static_cast<uint32_t>(sampleDelta & 0xFFFFFFFF));
1374
1375 // if the delta between the ideal placement for the next input sample and
1376 // the current output position is within this threshold, then we will
1377 // concatenate the next input samples to the previous output
1378 const int64_t kSampleContinuityThreshold =
Glenn Kasten9fdcb0a2013-06-26 16:11:36 -07001379 (static_cast<int64_t>(sr) << 32) / 250;
Eric Laurent81784c32012-11-19 14:55:58 -08001380
1381 // if this is the first buffer of audio that we're emitting from this track
1382 // then it should be almost exactly on time.
1383 const int64_t kSampleStartupThreshold = 1LL << 32;
1384
1385 if ((mTimedAudioOutputOnTime && llabs(sampleDelta) <= kSampleContinuityThreshold) ||
1386 (!mTimedAudioOutputOnTime && llabs(sampleDelta) <= kSampleStartupThreshold)) {
1387 // the next input is close enough to being on time, so concatenate it
1388 // with the last output
1389 timedYieldSamples_l(buffer);
1390
1391 ALOGVV("*** on time: head.pos=%d frameCount=%u",
1392 head.position(), buffer->frameCount);
1393 return NO_ERROR;
1394 }
1395
1396 // Looks like our output is not on time. Reset our on timed status.
1397 // Next time we mix samples from our input queue, then should be within
1398 // the StartupThreshold.
1399 mTimedAudioOutputOnTime = false;
1400 if (sampleDelta > 0) {
1401 // the gap between the current output position and the proper start of
1402 // the next input sample is too big, so fill it with silence
1403 uint32_t framesUntilNextInput = (sampleDelta + 0x80000000) >> 32;
1404
1405 timedYieldSilence_l(framesUntilNextInput, buffer);
1406 ALOGV("*** silence: frameCount=%u", buffer->frameCount);
1407 return NO_ERROR;
1408 } else {
1409 // the next input sample is late
1410 uint32_t lateFrames = static_cast<uint32_t>(-((sampleDelta + 0x80000000) >> 32));
1411 size_t onTimeSamplePosition =
1412 head.position() + lateFrames * mFrameSize;
1413
1414 if (onTimeSamplePosition > head.buffer()->size()) {
1415 // all the remaining samples in the head are too late, so
1416 // drop it and move on
1417 ALOGV("*** too late: dropped buffer");
1418 trimTimedBufferQueueHead_l("getNextBuffer, dropped late buffer");
1419 continue;
1420 } else {
1421 // skip over the late samples
1422 head.setPosition(onTimeSamplePosition);
1423
1424 // yield the available samples
1425 timedYieldSamples_l(buffer);
1426
1427 ALOGV("*** late: head.pos=%d frameCount=%u", head.position(), buffer->frameCount);
1428 return NO_ERROR;
1429 }
1430 }
1431 }
1432}
1433
1434// Yield samples from the timed buffer queue head up to the given output
1435// buffer's capacity.
1436//
1437// Caller must hold mTimedBufferQueueLock
1438void AudioFlinger::PlaybackThread::TimedTrack::timedYieldSamples_l(
1439 AudioBufferProvider::Buffer* buffer) {
1440
1441 const TimedBuffer& head = mTimedBufferQueue[0];
1442
1443 buffer->raw = (static_cast<uint8_t*>(head.buffer()->pointer()) +
1444 head.position());
1445
1446 uint32_t framesLeftInHead = ((head.buffer()->size() - head.position()) /
1447 mFrameSize);
1448 size_t framesRequested = buffer->frameCount;
1449 buffer->frameCount = min(framesLeftInHead, framesRequested);
1450
1451 mQueueHeadInFlight = true;
1452 mTimedAudioOutputOnTime = true;
1453}
1454
1455// Yield samples of silence up to the given output buffer's capacity
1456//
1457// Caller must hold mTimedBufferQueueLock
1458void AudioFlinger::PlaybackThread::TimedTrack::timedYieldSilence_l(
1459 uint32_t numFrames, AudioBufferProvider::Buffer* buffer) {
1460
1461 // lazily allocate a buffer filled with silence
1462 if (mTimedSilenceBufferSize < numFrames * mFrameSize) {
1463 delete [] mTimedSilenceBuffer;
1464 mTimedSilenceBufferSize = numFrames * mFrameSize;
1465 mTimedSilenceBuffer = new uint8_t[mTimedSilenceBufferSize];
1466 memset(mTimedSilenceBuffer, 0, mTimedSilenceBufferSize);
1467 }
1468
1469 buffer->raw = mTimedSilenceBuffer;
1470 size_t framesRequested = buffer->frameCount;
1471 buffer->frameCount = min(numFrames, framesRequested);
1472
1473 mTimedAudioOutputOnTime = false;
1474}
1475
1476// AudioBufferProvider interface
1477void AudioFlinger::PlaybackThread::TimedTrack::releaseBuffer(
1478 AudioBufferProvider::Buffer* buffer) {
1479
1480 Mutex::Autolock _l(mTimedBufferQueueLock);
1481
1482 // If the buffer which was just released is part of the buffer at the head
1483 // of the queue, be sure to update the amt of the buffer which has been
1484 // consumed. If the buffer being returned is not part of the head of the
1485 // queue, its either because the buffer is part of the silence buffer, or
1486 // because the head of the timed queue was trimmed after the mixer called
1487 // getNextBuffer but before the mixer called releaseBuffer.
1488 if (buffer->raw == mTimedSilenceBuffer) {
1489 ALOG_ASSERT(!mQueueHeadInFlight,
1490 "Queue head in flight during release of silence buffer!");
1491 goto done;
1492 }
1493
1494 ALOG_ASSERT(mQueueHeadInFlight,
1495 "TimedTrack::releaseBuffer of non-silence buffer, but no queue"
1496 " head in flight.");
1497
1498 if (mTimedBufferQueue.size()) {
1499 TimedBuffer& head = mTimedBufferQueue.editItemAt(0);
1500
1501 void* start = head.buffer()->pointer();
1502 void* end = reinterpret_cast<void*>(
1503 reinterpret_cast<uint8_t*>(head.buffer()->pointer())
1504 + head.buffer()->size());
1505
1506 ALOG_ASSERT((buffer->raw >= start) && (buffer->raw < end),
1507 "released buffer not within the head of the timed buffer"
1508 " queue; qHead = [%p, %p], released buffer = %p",
1509 start, end, buffer->raw);
1510
1511 head.setPosition(head.position() +
1512 (buffer->frameCount * mFrameSize));
1513 mQueueHeadInFlight = false;
1514
1515 ALOG_ASSERT(mFramesPendingInQueue >= buffer->frameCount,
1516 "Bad bookkeeping during releaseBuffer! Should have at"
1517 " least %u queued frames, but we think we have only %u",
1518 buffer->frameCount, mFramesPendingInQueue);
1519
1520 mFramesPendingInQueue -= buffer->frameCount;
1521
1522 if ((static_cast<size_t>(head.position()) >= head.buffer()->size())
1523 || mTrimQueueHeadOnRelease) {
1524 trimTimedBufferQueueHead_l("releaseBuffer");
1525 mTrimQueueHeadOnRelease = false;
1526 }
1527 } else {
1528 LOG_FATAL("TimedTrack::releaseBuffer of non-silence buffer with no"
1529 " buffers in the timed buffer queue");
1530 }
1531
1532done:
1533 buffer->raw = 0;
1534 buffer->frameCount = 0;
1535}
1536
1537size_t AudioFlinger::PlaybackThread::TimedTrack::framesReady() const {
1538 Mutex::Autolock _l(mTimedBufferQueueLock);
1539 return mFramesPendingInQueue;
1540}
1541
1542AudioFlinger::PlaybackThread::TimedTrack::TimedBuffer::TimedBuffer()
1543 : mPTS(0), mPosition(0) {}
1544
1545AudioFlinger::PlaybackThread::TimedTrack::TimedBuffer::TimedBuffer(
1546 const sp<IMemory>& buffer, int64_t pts)
1547 : mBuffer(buffer), mPTS(pts), mPosition(0) {}
1548
1549
1550// ----------------------------------------------------------------------------
1551
1552AudioFlinger::PlaybackThread::OutputTrack::OutputTrack(
1553 PlaybackThread *playbackThread,
1554 DuplicatingThread *sourceThread,
1555 uint32_t sampleRate,
1556 audio_format_t format,
1557 audio_channel_mask_t channelMask,
Marco Nelissen462fd2f2013-01-14 14:12:05 -08001558 size_t frameCount,
1559 int uid)
Eric Laurent81784c32012-11-19 14:55:58 -08001560 : Track(playbackThread, NULL, AUDIO_STREAM_CNT, sampleRate, format, channelMask, frameCount,
Marco Nelissen462fd2f2013-01-14 14:12:05 -08001561 NULL, 0, uid, IAudioFlinger::TRACK_DEFAULT),
Glenn Kastene3aa6592012-12-04 12:22:46 -08001562 mActive(false), mSourceThread(sourceThread), mClientProxy(NULL)
Eric Laurent81784c32012-11-19 14:55:58 -08001563{
1564
1565 if (mCblk != NULL) {
Eric Laurent81784c32012-11-19 14:55:58 -08001566 mOutBuffer.frameCount = 0;
1567 playbackThread->mTracks.add(this);
Glenn Kastene3aa6592012-12-04 12:22:46 -08001568 ALOGV("OutputTrack constructor mCblk %p, mBuffer %p, "
Glenn Kasten74935e42013-12-19 08:56:45 -08001569 "frameCount %u, mChannelMask 0x%08x",
Glenn Kastene3aa6592012-12-04 12:22:46 -08001570 mCblk, mBuffer,
Glenn Kasten74935e42013-12-19 08:56:45 -08001571 frameCount, mChannelMask);
Glenn Kastene3aa6592012-12-04 12:22:46 -08001572 // since client and server are in the same process,
1573 // the buffer has the same virtual address on both sides
1574 mClientProxy = new AudioTrackClientProxy(mCblk, mBuffer, mFrameCount, mFrameSize);
Eric Laurent8d2d4932013-04-25 12:56:18 -07001575 mClientProxy->setVolumeLR((uint32_t(uint16_t(0x1000)) << 16) | uint16_t(0x1000));
1576 mClientProxy->setSendLevel(0.0);
1577 mClientProxy->setSampleRate(sampleRate);
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001578 mClientProxy = new AudioTrackClientProxy(mCblk, mBuffer, mFrameCount, mFrameSize,
1579 true /*clientInServer*/);
Eric Laurent81784c32012-11-19 14:55:58 -08001580 } else {
1581 ALOGW("Error creating output track on thread %p", playbackThread);
1582 }
1583}
1584
1585AudioFlinger::PlaybackThread::OutputTrack::~OutputTrack()
1586{
1587 clearBufferQueue();
Glenn Kastene3aa6592012-12-04 12:22:46 -08001588 delete mClientProxy;
1589 // superclass destructor will now delete the server proxy and shared memory both refer to
Eric Laurent81784c32012-11-19 14:55:58 -08001590}
1591
1592status_t AudioFlinger::PlaybackThread::OutputTrack::start(AudioSystem::sync_event_t event,
1593 int triggerSession)
1594{
1595 status_t status = Track::start(event, triggerSession);
1596 if (status != NO_ERROR) {
1597 return status;
1598 }
1599
1600 mActive = true;
1601 mRetryCount = 127;
1602 return status;
1603}
1604
1605void AudioFlinger::PlaybackThread::OutputTrack::stop()
1606{
1607 Track::stop();
1608 clearBufferQueue();
1609 mOutBuffer.frameCount = 0;
1610 mActive = false;
1611}
1612
1613bool AudioFlinger::PlaybackThread::OutputTrack::write(int16_t* data, uint32_t frames)
1614{
1615 Buffer *pInBuffer;
1616 Buffer inBuffer;
1617 uint32_t channelCount = mChannelCount;
1618 bool outputBufferFull = false;
1619 inBuffer.frameCount = frames;
1620 inBuffer.i16 = data;
1621
1622 uint32_t waitTimeLeftMs = mSourceThread->waitTimeMs();
1623
1624 if (!mActive && frames != 0) {
1625 start();
1626 sp<ThreadBase> thread = mThread.promote();
1627 if (thread != 0) {
1628 MixerThread *mixerThread = (MixerThread *)thread.get();
1629 if (mFrameCount > frames) {
1630 if (mBufferQueue.size() < kMaxOverFlowBuffers) {
1631 uint32_t startFrames = (mFrameCount - frames);
1632 pInBuffer = new Buffer;
1633 pInBuffer->mBuffer = new int16_t[startFrames * channelCount];
1634 pInBuffer->frameCount = startFrames;
1635 pInBuffer->i16 = pInBuffer->mBuffer;
1636 memset(pInBuffer->raw, 0, startFrames * channelCount * sizeof(int16_t));
1637 mBufferQueue.add(pInBuffer);
1638 } else {
Glenn Kasten7c027242012-12-26 14:43:16 -08001639 ALOGW("OutputTrack::write() %p no more buffers in queue", this);
Eric Laurent81784c32012-11-19 14:55:58 -08001640 }
1641 }
1642 }
1643 }
1644
1645 while (waitTimeLeftMs) {
1646 // First write pending buffers, then new data
1647 if (mBufferQueue.size()) {
1648 pInBuffer = mBufferQueue.itemAt(0);
1649 } else {
1650 pInBuffer = &inBuffer;
1651 }
1652
1653 if (pInBuffer->frameCount == 0) {
1654 break;
1655 }
1656
1657 if (mOutBuffer.frameCount == 0) {
1658 mOutBuffer.frameCount = pInBuffer->frameCount;
1659 nsecs_t startTime = systemTime();
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001660 status_t status = obtainBuffer(&mOutBuffer, waitTimeLeftMs);
1661 if (status != NO_ERROR) {
1662 ALOGV("OutputTrack::write() %p thread %p no more output buffers; status %d", this,
1663 mThread.unsafe_get(), status);
Eric Laurent81784c32012-11-19 14:55:58 -08001664 outputBufferFull = true;
1665 break;
1666 }
1667 uint32_t waitTimeMs = (uint32_t)ns2ms(systemTime() - startTime);
1668 if (waitTimeLeftMs >= waitTimeMs) {
1669 waitTimeLeftMs -= waitTimeMs;
1670 } else {
1671 waitTimeLeftMs = 0;
1672 }
1673 }
1674
1675 uint32_t outFrames = pInBuffer->frameCount > mOutBuffer.frameCount ? mOutBuffer.frameCount :
1676 pInBuffer->frameCount;
1677 memcpy(mOutBuffer.raw, pInBuffer->raw, outFrames * channelCount * sizeof(int16_t));
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001678 Proxy::Buffer buf;
1679 buf.mFrameCount = outFrames;
1680 buf.mRaw = NULL;
1681 mClientProxy->releaseBuffer(&buf);
Eric Laurent81784c32012-11-19 14:55:58 -08001682 pInBuffer->frameCount -= outFrames;
1683 pInBuffer->i16 += outFrames * channelCount;
1684 mOutBuffer.frameCount -= outFrames;
1685 mOutBuffer.i16 += outFrames * channelCount;
1686
1687 if (pInBuffer->frameCount == 0) {
1688 if (mBufferQueue.size()) {
1689 mBufferQueue.removeAt(0);
1690 delete [] pInBuffer->mBuffer;
1691 delete pInBuffer;
1692 ALOGV("OutputTrack::write() %p thread %p released overflow buffer %d", this,
1693 mThread.unsafe_get(), mBufferQueue.size());
1694 } else {
1695 break;
1696 }
1697 }
1698 }
1699
1700 // If we could not write all frames, allocate a buffer and queue it for next time.
1701 if (inBuffer.frameCount) {
1702 sp<ThreadBase> thread = mThread.promote();
1703 if (thread != 0 && !thread->standby()) {
1704 if (mBufferQueue.size() < kMaxOverFlowBuffers) {
1705 pInBuffer = new Buffer;
1706 pInBuffer->mBuffer = new int16_t[inBuffer.frameCount * channelCount];
1707 pInBuffer->frameCount = inBuffer.frameCount;
1708 pInBuffer->i16 = pInBuffer->mBuffer;
1709 memcpy(pInBuffer->raw, inBuffer.raw, inBuffer.frameCount * channelCount *
1710 sizeof(int16_t));
1711 mBufferQueue.add(pInBuffer);
1712 ALOGV("OutputTrack::write() %p thread %p adding overflow buffer %d", this,
1713 mThread.unsafe_get(), mBufferQueue.size());
1714 } else {
1715 ALOGW("OutputTrack::write() %p thread %p no more overflow buffers",
1716 mThread.unsafe_get(), this);
1717 }
1718 }
1719 }
1720
1721 // Calling write() with a 0 length buffer, means that no more data will be written:
1722 // If no more buffers are pending, fill output track buffer to make sure it is started
1723 // by output mixer.
1724 if (frames == 0 && mBufferQueue.size() == 0) {
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001725 // FIXME borken, replace by getting framesReady() from proxy
1726 size_t user = 0; // was mCblk->user
1727 if (user < mFrameCount) {
1728 frames = mFrameCount - user;
Eric Laurent81784c32012-11-19 14:55:58 -08001729 pInBuffer = new Buffer;
1730 pInBuffer->mBuffer = new int16_t[frames * channelCount];
1731 pInBuffer->frameCount = frames;
1732 pInBuffer->i16 = pInBuffer->mBuffer;
1733 memset(pInBuffer->raw, 0, frames * channelCount * sizeof(int16_t));
1734 mBufferQueue.add(pInBuffer);
1735 } else if (mActive) {
1736 stop();
1737 }
1738 }
1739
1740 return outputBufferFull;
1741}
1742
1743status_t AudioFlinger::PlaybackThread::OutputTrack::obtainBuffer(
1744 AudioBufferProvider::Buffer* buffer, uint32_t waitTimeMs)
1745{
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001746 ClientProxy::Buffer buf;
1747 buf.mFrameCount = buffer->frameCount;
1748 struct timespec timeout;
1749 timeout.tv_sec = waitTimeMs / 1000;
1750 timeout.tv_nsec = (int) (waitTimeMs % 1000) * 1000000;
1751 status_t status = mClientProxy->obtainBuffer(&buf, &timeout);
1752 buffer->frameCount = buf.mFrameCount;
1753 buffer->raw = buf.mRaw;
1754 return status;
Eric Laurent81784c32012-11-19 14:55:58 -08001755}
1756
Eric Laurent81784c32012-11-19 14:55:58 -08001757void AudioFlinger::PlaybackThread::OutputTrack::clearBufferQueue()
1758{
1759 size_t size = mBufferQueue.size();
1760
1761 for (size_t i = 0; i < size; i++) {
1762 Buffer *pBuffer = mBufferQueue.itemAt(i);
1763 delete [] pBuffer->mBuffer;
1764 delete pBuffer;
1765 }
1766 mBufferQueue.clear();
1767}
1768
1769
1770// ----------------------------------------------------------------------------
1771// Record
1772// ----------------------------------------------------------------------------
1773
1774AudioFlinger::RecordHandle::RecordHandle(
1775 const sp<AudioFlinger::RecordThread::RecordTrack>& recordTrack)
1776 : BnAudioRecord(),
1777 mRecordTrack(recordTrack)
1778{
1779}
1780
1781AudioFlinger::RecordHandle::~RecordHandle() {
1782 stop_nonvirtual();
1783 mRecordTrack->destroy();
1784}
1785
1786sp<IMemory> AudioFlinger::RecordHandle::getCblk() const {
1787 return mRecordTrack->getCblk();
1788}
1789
1790status_t AudioFlinger::RecordHandle::start(int /*AudioSystem::sync_event_t*/ event,
1791 int triggerSession) {
1792 ALOGV("RecordHandle::start()");
1793 return mRecordTrack->start((AudioSystem::sync_event_t)event, triggerSession);
1794}
1795
1796void AudioFlinger::RecordHandle::stop() {
1797 stop_nonvirtual();
1798}
1799
1800void AudioFlinger::RecordHandle::stop_nonvirtual() {
1801 ALOGV("RecordHandle::stop()");
1802 mRecordTrack->stop();
1803}
1804
1805status_t AudioFlinger::RecordHandle::onTransact(
1806 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
1807{
1808 return BnAudioRecord::onTransact(code, data, reply, flags);
1809}
1810
1811// ----------------------------------------------------------------------------
1812
Glenn Kasten05997e22014-03-13 15:08:33 -07001813// RecordTrack constructor must be called with AudioFlinger::mLock and ThreadBase::mLock held
Eric Laurent81784c32012-11-19 14:55:58 -08001814AudioFlinger::RecordThread::RecordTrack::RecordTrack(
1815 RecordThread *thread,
1816 const sp<Client>& client,
1817 uint32_t sampleRate,
1818 audio_format_t format,
1819 audio_channel_mask_t channelMask,
1820 size_t frameCount,
Marco Nelissen462fd2f2013-01-14 14:12:05 -08001821 int sessionId,
1822 int uid)
Eric Laurent81784c32012-11-19 14:55:58 -08001823 : TrackBase(thread, client, sampleRate, format,
Marco Nelissen462fd2f2013-01-14 14:12:05 -08001824 channelMask, frameCount, 0 /*sharedBuffer*/, sessionId, uid, false /*isOut*/),
Glenn Kasten6dd62fb2013-12-05 16:35:58 -08001825 mOverflow(false), mResampler(NULL), mRsmpOutBuffer(NULL), mRsmpOutFrameCount(0),
1826 // See real initialization of mRsmpInFront at RecordThread::start()
1827 mRsmpInUnrel(0), mRsmpInFront(0), mFramesToDrop(0), mResamplerBufferProvider(NULL)
Eric Laurent81784c32012-11-19 14:55:58 -08001828{
Glenn Kasten3ef14ef2014-03-13 15:08:51 -07001829 if (mCblk == NULL) {
1830 return;
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001831 }
Glenn Kasten6dd62fb2013-12-05 16:35:58 -08001832
Glenn Kasten3ef14ef2014-03-13 15:08:51 -07001833 mServerProxy = new AudioRecordServerProxy(mCblk, mBuffer, frameCount, mFrameSize);
1834
Glenn Kasten6dd62fb2013-12-05 16:35:58 -08001835 uint32_t channelCount = popcount(channelMask);
1836 // FIXME I don't understand either of the channel count checks
1837 if (thread->mSampleRate != sampleRate && thread->mChannelCount <= FCC_2 &&
1838 channelCount <= FCC_2) {
1839 // sink SR
1840 mResampler = AudioResampler::create(16, thread->mChannelCount, sampleRate);
1841 // source SR
1842 mResampler->setSampleRate(thread->mSampleRate);
1843 mResampler->setVolume(AudioMixer::UNITY_GAIN, AudioMixer::UNITY_GAIN);
1844 mResamplerBufferProvider = new ResamplerBufferProvider(this);
1845 }
Eric Laurent81784c32012-11-19 14:55:58 -08001846}
1847
1848AudioFlinger::RecordThread::RecordTrack::~RecordTrack()
1849{
1850 ALOGV("%s", __func__);
Glenn Kasten6dd62fb2013-12-05 16:35:58 -08001851 delete mResampler;
1852 delete[] mRsmpOutBuffer;
1853 delete mResamplerBufferProvider;
Eric Laurent81784c32012-11-19 14:55:58 -08001854}
1855
1856// AudioBufferProvider interface
1857status_t AudioFlinger::RecordThread::RecordTrack::getNextBuffer(AudioBufferProvider::Buffer* buffer,
Glenn Kasten0f11b512014-01-31 16:18:54 -08001858 int64_t pts __unused)
Eric Laurent81784c32012-11-19 14:55:58 -08001859{
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001860 ServerProxy::Buffer buf;
1861 buf.mFrameCount = buffer->frameCount;
1862 status_t status = mServerProxy->obtainBuffer(&buf);
1863 buffer->frameCount = buf.mFrameCount;
1864 buffer->raw = buf.mRaw;
1865 if (buf.mFrameCount == 0) {
1866 // FIXME also wake futex so that overrun is noticed more quickly
Glenn Kasten96f60d82013-07-12 10:21:18 -07001867 (void) android_atomic_or(CBLK_OVERRUN, &mCblk->mFlags);
Eric Laurent81784c32012-11-19 14:55:58 -08001868 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001869 return status;
Eric Laurent81784c32012-11-19 14:55:58 -08001870}
1871
1872status_t AudioFlinger::RecordThread::RecordTrack::start(AudioSystem::sync_event_t event,
1873 int triggerSession)
1874{
1875 sp<ThreadBase> thread = mThread.promote();
1876 if (thread != 0) {
1877 RecordThread *recordThread = (RecordThread *)thread.get();
1878 return recordThread->start(this, event, triggerSession);
1879 } else {
1880 return BAD_VALUE;
1881 }
1882}
1883
1884void AudioFlinger::RecordThread::RecordTrack::stop()
1885{
1886 sp<ThreadBase> thread = mThread.promote();
1887 if (thread != 0) {
1888 RecordThread *recordThread = (RecordThread *)thread.get();
Glenn Kastena8356f62013-07-25 14:37:52 -07001889 if (recordThread->stop(this)) {
Eric Laurent81784c32012-11-19 14:55:58 -08001890 AudioSystem::stopInput(recordThread->id());
1891 }
1892 }
1893}
1894
1895void AudioFlinger::RecordThread::RecordTrack::destroy()
1896{
1897 // see comments at AudioFlinger::PlaybackThread::Track::destroy()
1898 sp<RecordTrack> keep(this);
1899 {
1900 sp<ThreadBase> thread = mThread.promote();
1901 if (thread != 0) {
1902 if (mState == ACTIVE || mState == RESUMING) {
1903 AudioSystem::stopInput(thread->id());
1904 }
1905 AudioSystem::releaseInput(thread->id());
1906 Mutex::Autolock _l(thread->mLock);
1907 RecordThread *recordThread = (RecordThread *) thread.get();
1908 recordThread->destroyTrack_l(this);
1909 }
1910 }
1911}
1912
Eric Laurent9a54bc22013-09-09 09:08:44 -07001913void AudioFlinger::RecordThread::RecordTrack::invalidate()
1914{
1915 // FIXME should use proxy, and needs work
1916 audio_track_cblk_t* cblk = mCblk;
1917 android_atomic_or(CBLK_INVALID, &cblk->mFlags);
1918 android_atomic_release_store(0x40000000, &cblk->mFutex);
1919 // client is not in server, so FUTEX_WAKE is needed instead of FUTEX_WAKE_PRIVATE
1920 (void) __futex_syscall3(&cblk->mFutex, FUTEX_WAKE, INT_MAX);
1921}
1922
Eric Laurent81784c32012-11-19 14:55:58 -08001923
1924/*static*/ void AudioFlinger::RecordThread::RecordTrack::appendDumpHeader(String8& result)
1925{
Glenn Kasten6dd62fb2013-12-05 16:35:58 -08001926 result.append(" Active Client Fmt Chn mask Session S Server fCount Resampling\n");
Eric Laurent81784c32012-11-19 14:55:58 -08001927}
1928
Marco Nelissenb2208842014-02-07 14:00:50 -08001929void AudioFlinger::RecordThread::RecordTrack::dump(char* buffer, size_t size, bool active)
Eric Laurent81784c32012-11-19 14:55:58 -08001930{
Glenn Kasten6dd62fb2013-12-05 16:35:58 -08001931 snprintf(buffer, size, " %6s %6u %3u %08X %7u %1d %08X %6zu %10d\n",
Marco Nelissenb2208842014-02-07 14:00:50 -08001932 active ? "yes" : "no",
Eric Laurent81784c32012-11-19 14:55:58 -08001933 (mClient == 0) ? getpid_cached : mClient->pid(),
1934 mFormat,
1935 mChannelMask,
1936 mSessionId,
Eric Laurent81784c32012-11-19 14:55:58 -08001937 mState,
Glenn Kastenf20e1d82013-07-12 09:45:18 -07001938 mCblk->mServer,
Glenn Kasten6dd62fb2013-12-05 16:35:58 -08001939 mFrameCount,
1940 mResampler != NULL);
1941
Eric Laurent81784c32012-11-19 14:55:58 -08001942}
1943
Glenn Kasten25f4aa82014-02-07 10:50:43 -08001944void AudioFlinger::RecordThread::RecordTrack::handleSyncStartEvent(const sp<SyncEvent>& event)
1945{
1946 if (event == mSyncStartEvent) {
1947 ssize_t framesToDrop = 0;
1948 sp<ThreadBase> threadBase = mThread.promote();
1949 if (threadBase != 0) {
1950 // TODO: use actual buffer filling status instead of 2 buffers when info is available
1951 // from audio HAL
1952 framesToDrop = threadBase->mFrameCount * 2;
1953 }
1954 mFramesToDrop = framesToDrop;
1955 }
1956}
1957
1958void AudioFlinger::RecordThread::RecordTrack::clearSyncStartEvent()
1959{
1960 if (mSyncStartEvent != 0) {
1961 mSyncStartEvent->cancel();
1962 mSyncStartEvent.clear();
1963 }
1964 mFramesToDrop = 0;
1965}
1966
Eric Laurent81784c32012-11-19 14:55:58 -08001967}; // namespace android