blob: 53178fb861475090e5c9eabf448a384b9359d653 [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"
Glenn Kastenad8510a2015-02-17 16:24:07 -080023#include <linux/futex.h>
Eric Laurent81784c32012-11-19 14:55:58 -080024#include <math.h>
Elliott Hughesee499292014-05-21 17:55:51 -070025#include <sys/syscall.h>
Eric Laurent81784c32012-11-19 14:55:58 -080026#include <utils/Log.h>
27
28#include <private/media/AudioTrackShared.h>
29
Eric Laurent81784c32012-11-19 14:55:58 -080030#include "AudioMixer.h"
31#include "AudioFlinger.h"
32#include "ServiceUtilities.h"
33
Glenn Kastenda6ef132013-01-10 12:31:01 -080034#include <media/nbaio/Pipe.h>
35#include <media/nbaio/PipeReader.h>
Glenn Kastenc56f3422014-03-21 17:53:17 -070036#include <audio_utils/minifloat.h>
Glenn Kastenda6ef132013-01-10 12:31:01 -080037
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
Andy Hunge10393e2015-06-12 13:59:33 -070053// TODO move to a common header (Also shared with AudioTrack.cpp)
54#define NANOS_PER_SECOND 1000000000
55#define TIME_TO_NANOS(time) ((uint64_t)time.tv_sec * NANOS_PER_SECOND + time.tv_nsec)
56
Eric Laurent81784c32012-11-19 14:55:58 -080057namespace android {
58
59// ----------------------------------------------------------------------------
60// TrackBase
61// ----------------------------------------------------------------------------
62
Glenn Kastenda6ef132013-01-10 12:31:01 -080063static volatile int32_t nextTrackId = 55;
64
Eric Laurent81784c32012-11-19 14:55:58 -080065// TrackBase constructor must be called with AudioFlinger::mLock held
66AudioFlinger::ThreadBase::TrackBase::TrackBase(
67 ThreadBase *thread,
68 const sp<Client>& client,
69 uint32_t sampleRate,
70 audio_format_t format,
71 audio_channel_mask_t channelMask,
72 size_t frameCount,
Eric Laurent83b88082014-06-20 18:31:16 -070073 void *buffer,
Glenn Kastend848eb42016-03-08 13:42:11 -080074 audio_session_t sessionId,
Marco Nelissen462fd2f2013-01-14 14:12:05 -080075 int clientUid,
Glenn Kasten755b0a62014-05-13 11:30:28 -070076 IAudioFlinger::track_flags_t flags,
Glenn Kastend776ac62014-05-07 09:16:09 -070077 bool isOut,
Eric Laurent83b88082014-06-20 18:31:16 -070078 alloc_type alloc,
79 track_type type)
Eric Laurent81784c32012-11-19 14:55:58 -080080 : RefBase(),
81 mThread(thread),
82 mClient(client),
83 mCblk(NULL),
84 // mBuffer
Eric Laurent81784c32012-11-19 14:55:58 -080085 mState(IDLE),
86 mSampleRate(sampleRate),
87 mFormat(format),
88 mChannelMask(channelMask),
Andy Hunge5412692014-05-16 11:25:07 -070089 mChannelCount(isOut ?
90 audio_channel_count_from_out_mask(channelMask) :
91 audio_channel_count_from_in_mask(channelMask)),
Phil Burkfdb3c072016-02-09 10:47:02 -080092 mFrameSize(audio_has_proportional_frames(format) ?
Eric Laurent81784c32012-11-19 14:55:58 -080093 mChannelCount * audio_bytes_per_sample(format) : sizeof(int8_t)),
94 mFrameCount(frameCount),
Glenn Kastene3aa6592012-12-04 12:22:46 -080095 mSessionId(sessionId),
Glenn Kasten755b0a62014-05-13 11:30:28 -070096 mFlags(flags),
Glenn Kastene3aa6592012-12-04 12:22:46 -080097 mIsOut(isOut),
Eric Laurentbfb1b832013-01-07 09:53:42 -080098 mId(android_atomic_inc(&nextTrackId)),
Eric Laurent83b88082014-06-20 18:31:16 -070099 mTerminated(false),
Eric Laurentaaa44472014-09-12 17:41:50 -0700100 mType(type),
101 mThreadIoHandle(thread->id())
Eric Laurent81784c32012-11-19 14:55:58 -0800102{
Marco Nelissendcb346b2015-09-09 10:47:29 -0700103 const uid_t callingUid = IPCThreadState::self()->getCallingUid();
104 if (!isTrustedCallingUid(callingUid) || clientUid == -1) {
105 ALOGW_IF(clientUid != -1 && clientUid != (int)callingUid,
106 "%s uid %d tried to pass itself off as %d", __FUNCTION__, callingUid, clientUid);
107 clientUid = (int)callingUid;
Marco Nelissen462fd2f2013-01-14 14:12:05 -0800108 }
109 // clientUid contains the uid of the app that is responsible for this track, so we can blame
110 // battery usage on it.
111 mUid = clientUid;
112
Eric Laurent81784c32012-11-19 14:55:58 -0800113 // ALOGD("Creating track with %d buffers @ %d bytes", bufferCount, bufferSize);
114 size_t size = sizeof(audio_track_cblk_t);
Eric Laurent83b88082014-06-20 18:31:16 -0700115 size_t bufferSize = (buffer == NULL ? roundup(frameCount) : frameCount) * mFrameSize;
116 if (buffer == NULL && alloc == ALLOC_CBLK) {
Eric Laurent81784c32012-11-19 14:55:58 -0800117 size += bufferSize;
118 }
119
120 if (client != 0) {
121 mCblkMemory = client->heap()->allocate(size);
Glenn Kasten663c2242013-09-24 11:52:37 -0700122 if (mCblkMemory == 0 ||
123 (mCblk = static_cast<audio_track_cblk_t *>(mCblkMemory->pointer())) == NULL) {
Glenn Kastenc42e9b42016-03-21 11:35:03 -0700124 ALOGE("not enough memory for AudioTrack size=%zu", size);
Eric Laurent81784c32012-11-19 14:55:58 -0800125 client->heap()->dump("AudioTrack");
Glenn Kasten663c2242013-09-24 11:52:37 -0700126 mCblkMemory.clear();
Eric Laurent81784c32012-11-19 14:55:58 -0800127 return;
128 }
129 } else {
Glenn Kastene3aa6592012-12-04 12:22:46 -0800130 // this syntax avoids calling the audio_track_cblk_t constructor twice
131 mCblk = (audio_track_cblk_t *) new uint8_t[size];
Eric Laurent81784c32012-11-19 14:55:58 -0800132 // assume mCblk != NULL
133 }
134
135 // construct the shared structure in-place.
136 if (mCblk != NULL) {
137 new(mCblk) audio_track_cblk_t();
Glenn Kastenc263ca02014-06-04 20:31:46 -0700138 switch (alloc) {
139 case ALLOC_READONLY: {
Glenn Kastend776ac62014-05-07 09:16:09 -0700140 const sp<MemoryDealer> roHeap(thread->readOnlyHeap());
141 if (roHeap == 0 ||
142 (mBufferMemory = roHeap->allocate(bufferSize)) == 0 ||
143 (mBuffer = mBufferMemory->pointer()) == NULL) {
144 ALOGE("not enough memory for read-only buffer size=%zu", bufferSize);
145 if (roHeap != 0) {
146 roHeap->dump("buffer");
147 }
148 mCblkMemory.clear();
149 mBufferMemory.clear();
150 return;
151 }
Eric Laurent81784c32012-11-19 14:55:58 -0800152 memset(mBuffer, 0, bufferSize);
Glenn Kastenc263ca02014-06-04 20:31:46 -0700153 } break;
154 case ALLOC_PIPE:
155 mBufferMemory = thread->pipeMemory();
156 // mBuffer is the virtual address as seen from current process (mediaserver),
157 // and should normally be coming from mBufferMemory->pointer().
158 // However in this case the TrackBase does not reference the buffer directly.
159 // It should references the buffer via the pipe.
160 // Therefore, to detect incorrect usage of the buffer, we set mBuffer to NULL.
161 mBuffer = NULL;
162 break;
163 case ALLOC_CBLK:
Glenn Kastend776ac62014-05-07 09:16:09 -0700164 // clear all buffers
Eric Laurent83b88082014-06-20 18:31:16 -0700165 if (buffer == NULL) {
Glenn Kastend776ac62014-05-07 09:16:09 -0700166 mBuffer = (char*)mCblk + sizeof(audio_track_cblk_t);
167 memset(mBuffer, 0, bufferSize);
168 } else {
Eric Laurent83b88082014-06-20 18:31:16 -0700169 mBuffer = buffer;
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800170#if 0
Glenn Kastend776ac62014-05-07 09:16:09 -0700171 mCblk->mFlags = CBLK_FORCEREADY; // FIXME hack, need to fix the track ready logic
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800172#endif
Glenn Kastend776ac62014-05-07 09:16:09 -0700173 }
Glenn Kastenc263ca02014-06-04 20:31:46 -0700174 break;
Eric Laurent83b88082014-06-20 18:31:16 -0700175 case ALLOC_LOCAL:
176 mBuffer = calloc(1, bufferSize);
177 break;
178 case ALLOC_NONE:
179 mBuffer = buffer;
180 break;
Eric Laurent81784c32012-11-19 14:55:58 -0800181 }
Glenn Kastenda6ef132013-01-10 12:31:01 -0800182
Glenn Kasten46909e72013-02-26 09:20:22 -0800183#ifdef TEE_SINK
Glenn Kastenda6ef132013-01-10 12:31:01 -0800184 if (mTeeSinkTrackEnabled) {
Glenn Kasten329f6512014-08-28 16:23:16 -0700185 NBAIO_Format pipeFormat = Format_from_SR_C(mSampleRate, mChannelCount, mFormat);
Glenn Kasten6e0d67d2014-01-31 09:41:08 -0800186 if (Format_isValid(pipeFormat)) {
Glenn Kasten46909e72013-02-26 09:20:22 -0800187 Pipe *pipe = new Pipe(mTeeSinkTrackFrames, pipeFormat);
188 size_t numCounterOffers = 0;
189 const NBAIO_Format offers[1] = {pipeFormat};
190 ssize_t index = pipe->negotiate(offers, 1, NULL, numCounterOffers);
191 ALOG_ASSERT(index == 0);
192 PipeReader *pipeReader = new PipeReader(*pipe);
193 numCounterOffers = 0;
194 index = pipeReader->negotiate(offers, 1, NULL, numCounterOffers);
195 ALOG_ASSERT(index == 0);
196 mTeeSink = pipe;
197 mTeeSource = pipeReader;
198 }
Glenn Kastenda6ef132013-01-10 12:31:01 -0800199 }
Glenn Kasten46909e72013-02-26 09:20:22 -0800200#endif
Glenn Kastenda6ef132013-01-10 12:31:01 -0800201
Eric Laurent81784c32012-11-19 14:55:58 -0800202 }
203}
204
Eric Laurent83b88082014-06-20 18:31:16 -0700205status_t AudioFlinger::ThreadBase::TrackBase::initCheck() const
206{
207 status_t status;
208 if (mType == TYPE_OUTPUT || mType == TYPE_PATCH) {
209 status = cblk() != NULL ? NO_ERROR : NO_MEMORY;
210 } else {
211 status = getCblk() != 0 ? NO_ERROR : NO_MEMORY;
212 }
213 return status;
214}
215
Eric Laurent81784c32012-11-19 14:55:58 -0800216AudioFlinger::ThreadBase::TrackBase::~TrackBase()
217{
Glenn Kasten46909e72013-02-26 09:20:22 -0800218#ifdef TEE_SINK
Glenn Kastenda6ef132013-01-10 12:31:01 -0800219 dumpTee(-1, mTeeSource, mId);
Glenn Kasten46909e72013-02-26 09:20:22 -0800220#endif
Glenn Kastene3aa6592012-12-04 12:22:46 -0800221 // delete the proxy before deleting the shared memory it refers to, to avoid dangling reference
Eric Laurent5bba2f62016-03-18 11:14:14 -0700222 mServerProxy.clear();
Eric Laurent81784c32012-11-19 14:55:58 -0800223 if (mCblk != NULL) {
224 if (mClient == 0) {
225 delete mCblk;
226 } else {
227 mCblk->~audio_track_cblk_t(); // destroy our shared-structure.
228 }
229 }
230 mCblkMemory.clear(); // free the shared memory before releasing the heap it belongs to
231 if (mClient != 0) {
Eric Laurent021cf962014-05-13 10:18:14 -0700232 // Client destructor must run with AudioFlinger client mutex locked
233 Mutex::Autolock _l(mClient->audioFlinger()->mClientLock);
Eric Laurent81784c32012-11-19 14:55:58 -0800234 // If the client's reference count drops to zero, the associated destructor
235 // must run with AudioFlinger lock held. Thus the explicit clear() rather than
236 // relying on the automatic clear() at end of scope.
237 mClient.clear();
238 }
Eric Laurent3bcffa12014-06-12 18:38:45 -0700239 // flush the binder command buffer
240 IPCThreadState::self()->flushCommands();
Eric Laurent81784c32012-11-19 14:55:58 -0800241}
242
243// AudioBufferProvider interface
244// getNextBuffer() = 0;
Glenn Kastend79072e2016-01-06 08:41:20 -0800245// This implementation of releaseBuffer() is used by Track and RecordTrack
Eric Laurent81784c32012-11-19 14:55:58 -0800246void AudioFlinger::ThreadBase::TrackBase::releaseBuffer(AudioBufferProvider::Buffer* buffer)
247{
Glenn Kasten46909e72013-02-26 09:20:22 -0800248#ifdef TEE_SINK
Glenn Kastenda6ef132013-01-10 12:31:01 -0800249 if (mTeeSink != 0) {
250 (void) mTeeSink->write(buffer->raw, buffer->frameCount);
251 }
Glenn Kasten46909e72013-02-26 09:20:22 -0800252#endif
Glenn Kastenda6ef132013-01-10 12:31:01 -0800253
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800254 ServerProxy::Buffer buf;
255 buf.mFrameCount = buffer->frameCount;
256 buf.mRaw = buffer->raw;
Eric Laurent81784c32012-11-19 14:55:58 -0800257 buffer->frameCount = 0;
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800258 buffer->raw = NULL;
259 mServerProxy->releaseBuffer(&buf);
Eric Laurent81784c32012-11-19 14:55:58 -0800260}
261
Eric Laurent81784c32012-11-19 14:55:58 -0800262status_t AudioFlinger::ThreadBase::TrackBase::setSyncEvent(const sp<SyncEvent>& event)
263{
264 mSyncEvents.add(event);
265 return NO_ERROR;
266}
267
268// ----------------------------------------------------------------------------
269// Playback
270// ----------------------------------------------------------------------------
271
272AudioFlinger::TrackHandle::TrackHandle(const sp<AudioFlinger::PlaybackThread::Track>& track)
273 : BnAudioTrack(),
274 mTrack(track)
275{
276}
277
278AudioFlinger::TrackHandle::~TrackHandle() {
279 // just stop the track on deletion, associated resources
280 // will be freed from the main thread once all pending buffers have
281 // been played. Unless it's not in the active track list, in which
282 // case we free everything now...
283 mTrack->destroy();
284}
285
286sp<IMemory> AudioFlinger::TrackHandle::getCblk() const {
287 return mTrack->getCblk();
288}
289
290status_t AudioFlinger::TrackHandle::start() {
291 return mTrack->start();
292}
293
294void AudioFlinger::TrackHandle::stop() {
295 mTrack->stop();
296}
297
298void AudioFlinger::TrackHandle::flush() {
299 mTrack->flush();
300}
301
Eric Laurent81784c32012-11-19 14:55:58 -0800302void AudioFlinger::TrackHandle::pause() {
303 mTrack->pause();
304}
305
306status_t AudioFlinger::TrackHandle::attachAuxEffect(int EffectId)
307{
308 return mTrack->attachAuxEffect(EffectId);
309}
310
Glenn Kasten3dcd00d2013-07-17 10:10:23 -0700311status_t AudioFlinger::TrackHandle::setParameters(const String8& keyValuePairs) {
312 return mTrack->setParameters(keyValuePairs);
313}
314
Glenn Kasten53cec222013-08-29 09:01:02 -0700315status_t AudioFlinger::TrackHandle::getTimestamp(AudioTimestamp& timestamp)
316{
Glenn Kasten573d80a2013-08-26 09:36:23 -0700317 return mTrack->getTimestamp(timestamp);
Glenn Kasten53cec222013-08-29 09:01:02 -0700318}
319
Eric Laurent59fe0102013-09-27 18:48:26 -0700320
321void AudioFlinger::TrackHandle::signal()
322{
323 return mTrack->signal();
324}
325
Eric Laurent81784c32012-11-19 14:55:58 -0800326status_t AudioFlinger::TrackHandle::onTransact(
327 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
328{
329 return BnAudioTrack::onTransact(code, data, reply, flags);
330}
331
332// ----------------------------------------------------------------------------
333
334// Track constructor must be called with AudioFlinger::mLock and ThreadBase::mLock held
335AudioFlinger::PlaybackThread::Track::Track(
336 PlaybackThread *thread,
337 const sp<Client>& client,
338 audio_stream_type_t streamType,
339 uint32_t sampleRate,
340 audio_format_t format,
341 audio_channel_mask_t channelMask,
342 size_t frameCount,
Eric Laurent83b88082014-06-20 18:31:16 -0700343 void *buffer,
Eric Laurent81784c32012-11-19 14:55:58 -0800344 const sp<IMemory>& sharedBuffer,
Glenn Kastend848eb42016-03-08 13:42:11 -0800345 audio_session_t sessionId,
Marco Nelissen462fd2f2013-01-14 14:12:05 -0800346 int uid,
Eric Laurent83b88082014-06-20 18:31:16 -0700347 IAudioFlinger::track_flags_t flags,
348 track_type type)
349 : TrackBase(thread, client, sampleRate, format, channelMask, frameCount,
350 (sharedBuffer != 0) ? sharedBuffer->pointer() : buffer,
351 sessionId, uid, flags, true /*isOut*/,
352 (type == TYPE_PATCH) ? ( buffer == NULL ? ALLOC_LOCAL : ALLOC_NONE) : ALLOC_CBLK,
353 type),
Eric Laurent81784c32012-11-19 14:55:58 -0800354 mFillingUpStatus(FS_INVALID),
355 // mRetryCount initialized later when needed
356 mSharedBuffer(sharedBuffer),
357 mStreamType(streamType),
358 mName(-1), // see note below
359 mMainBuffer(thread->mixBuffer()),
360 mAuxBuffer(NULL),
361 mAuxEffectId(0), mHasVolumeController(false),
362 mPresentationCompleteFrames(0),
Andy Hunge10393e2015-06-12 13:59:33 -0700363 mFrameMap(16 /* sink-frame-to-track-frame map memory */),
Andy Hunge10393e2015-06-12 13:59:33 -0700364 // mSinkTimestamp
Eric Laurent81784c32012-11-19 14:55:58 -0800365 mFastIndex(-1),
Glenn Kasten5736c352012-12-04 12:12:34 -0800366 mCachedVolume(1.0),
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800367 mIsInvalid(false),
Haynes Mathew George7844f672014-01-15 12:32:55 -0800368 mResumeToStopping(false),
Phil Burk1b420972015-04-22 10:52:21 -0700369 mFlushHwPending(false)
Eric Laurent81784c32012-11-19 14:55:58 -0800370{
Eric Laurent83b88082014-06-20 18:31:16 -0700371 // client == 0 implies sharedBuffer == 0
372 ALOG_ASSERT(!(client == 0 && sharedBuffer != 0));
373
374 ALOGV_IF(sharedBuffer != 0, "sharedBuffer: %p, size: %d", sharedBuffer->pointer(),
375 sharedBuffer->size());
376
Glenn Kasten3ef14ef2014-03-13 15:08:51 -0700377 if (mCblk == NULL) {
378 return;
Eric Laurent81784c32012-11-19 14:55:58 -0800379 }
Glenn Kasten3ef14ef2014-03-13 15:08:51 -0700380
381 if (sharedBuffer == 0) {
382 mAudioTrackServerProxy = new AudioTrackServerProxy(mCblk, mBuffer, frameCount,
Eric Laurent83b88082014-06-20 18:31:16 -0700383 mFrameSize, !isExternalTrack(), sampleRate);
Glenn Kasten3ef14ef2014-03-13 15:08:51 -0700384 } else {
385 mAudioTrackServerProxy = new StaticAudioTrackServerProxy(mCblk, mBuffer, frameCount,
386 mFrameSize);
387 }
388 mServerProxy = mAudioTrackServerProxy;
389
Glenn Kastenc263ca02014-06-04 20:31:46 -0700390 mName = thread->getTrackName_l(channelMask, format, sessionId);
Glenn Kasten3ef14ef2014-03-13 15:08:51 -0700391 if (mName < 0) {
392 ALOGE("no more track names available");
393 return;
394 }
395 // only allocate a fast track index if we were able to allocate a normal track name
396 if (flags & IAudioFlinger::TRACK_FAST) {
Andy Hunga5427822015-09-11 16:15:35 -0700397 // FIXME: Not calling framesReadyIsCalledByMultipleThreads() exposes a potential
398 // race with setSyncEvent(). However, if we call it, we cannot properly start
399 // static fast tracks (SoundPool) immediately after stopping.
400 //mAudioTrackServerProxy->framesReadyIsCalledByMultipleThreads();
Glenn Kasten3ef14ef2014-03-13 15:08:51 -0700401 ALOG_ASSERT(thread->mFastTrackAvailMask != 0);
402 int i = __builtin_ctz(thread->mFastTrackAvailMask);
403 ALOG_ASSERT(0 < i && i < (int)FastMixerState::kMaxFastTracks);
404 // FIXME This is too eager. We allocate a fast track index before the
405 // fast track becomes active. Since fast tracks are a scarce resource,
406 // this means we are potentially denying other more important fast tracks from
407 // being created. It would be better to allocate the index dynamically.
408 mFastIndex = i;
Glenn Kasten3ef14ef2014-03-13 15:08:51 -0700409 thread->mFastTrackAvailMask &= ~(1 << i);
410 }
Eric Laurent81784c32012-11-19 14:55:58 -0800411}
412
413AudioFlinger::PlaybackThread::Track::~Track()
414{
415 ALOGV("PlaybackThread::Track destructor");
Glenn Kasten0c72b242013-09-11 09:14:16 -0700416
417 // The destructor would clear mSharedBuffer,
418 // but it will not push the decremented reference count,
419 // leaving the client's IMemory dangling indefinitely.
420 // This prevents that leak.
421 if (mSharedBuffer != 0) {
422 mSharedBuffer.clear();
Glenn Kasten0c72b242013-09-11 09:14:16 -0700423 }
Eric Laurent81784c32012-11-19 14:55:58 -0800424}
425
Glenn Kasten03003332013-08-06 15:40:54 -0700426status_t AudioFlinger::PlaybackThread::Track::initCheck() const
427{
428 status_t status = TrackBase::initCheck();
429 if (status == NO_ERROR && mName < 0) {
430 status = NO_MEMORY;
431 }
432 return status;
433}
434
Eric Laurent81784c32012-11-19 14:55:58 -0800435void AudioFlinger::PlaybackThread::Track::destroy()
436{
437 // NOTE: destroyTrack_l() can remove a strong reference to this Track
438 // by removing it from mTracks vector, so there is a risk that this Tracks's
439 // destructor is called. As the destructor needs to lock mLock,
440 // we must acquire a strong reference on this Track before locking mLock
441 // here so that the destructor is called only when exiting this function.
442 // On the other hand, as long as Track::destroy() is only called by
443 // TrackHandle destructor, the TrackHandle still holds a strong ref on
444 // this Track with its member mTrack.
445 sp<Track> keep(this);
446 { // scope for mLock
Eric Laurentaaa44472014-09-12 17:41:50 -0700447 bool wasActive = false;
Eric Laurent81784c32012-11-19 14:55:58 -0800448 sp<ThreadBase> thread = mThread.promote();
449 if (thread != 0) {
Eric Laurent81784c32012-11-19 14:55:58 -0800450 Mutex::Autolock _l(thread->mLock);
451 PlaybackThread *playbackThread = (PlaybackThread *)thread.get();
Eric Laurentaaa44472014-09-12 17:41:50 -0700452 wasActive = playbackThread->destroyTrack_l(this);
453 }
454 if (isExternalTrack() && !wasActive) {
Glenn Kastend848eb42016-03-08 13:42:11 -0800455 AudioSystem::releaseOutput(mThreadIoHandle, mStreamType, mSessionId);
Eric Laurent81784c32012-11-19 14:55:58 -0800456 }
457 }
458}
459
460/*static*/ void AudioFlinger::PlaybackThread::Track::appendDumpHeader(String8& result)
461{
Marco Nelissenb2208842014-02-07 14:00:50 -0800462 result.append(" Name Active Client Type Fmt Chn mask Session fCount S F SRate "
Glenn Kasten82aaf942013-07-17 16:05:07 -0700463 "L dB R dB Server Main buf Aux Buf Flags UndFrmCnt\n");
Eric Laurent81784c32012-11-19 14:55:58 -0800464}
465
Marco Nelissenb2208842014-02-07 14:00:50 -0800466void AudioFlinger::PlaybackThread::Track::dump(char* buffer, size_t size, bool active)
Eric Laurent81784c32012-11-19 14:55:58 -0800467{
Glenn Kastenc56f3422014-03-21 17:53:17 -0700468 gain_minifloat_packed_t vlr = mAudioTrackServerProxy->getVolumeLR();
Eric Laurent81784c32012-11-19 14:55:58 -0800469 if (isFastTrack()) {
Marco Nelissenb2208842014-02-07 14:00:50 -0800470 sprintf(buffer, " F %2d", mFastIndex);
471 } else if (mName >= AudioMixer::TRACK0) {
472 sprintf(buffer, " %4d", mName - AudioMixer::TRACK0);
Eric Laurent81784c32012-11-19 14:55:58 -0800473 } else {
Marco Nelissenb2208842014-02-07 14:00:50 -0800474 sprintf(buffer, " none");
Eric Laurent81784c32012-11-19 14:55:58 -0800475 }
476 track_state state = mState;
477 char stateChar;
Eric Laurentbfb1b832013-01-07 09:53:42 -0800478 if (isTerminated()) {
Eric Laurent81784c32012-11-19 14:55:58 -0800479 stateChar = 'T';
Eric Laurentbfb1b832013-01-07 09:53:42 -0800480 } else {
481 switch (state) {
482 case IDLE:
483 stateChar = 'I';
484 break;
485 case STOPPING_1:
486 stateChar = 's';
487 break;
488 case STOPPING_2:
489 stateChar = '5';
490 break;
491 case STOPPED:
492 stateChar = 'S';
493 break;
494 case RESUMING:
495 stateChar = 'R';
496 break;
497 case ACTIVE:
498 stateChar = 'A';
499 break;
500 case PAUSING:
501 stateChar = 'p';
502 break;
503 case PAUSED:
504 stateChar = 'P';
505 break;
506 case FLUSHED:
507 stateChar = 'F';
508 break;
509 default:
510 stateChar = '?';
511 break;
512 }
Eric Laurent81784c32012-11-19 14:55:58 -0800513 }
514 char nowInUnderrun;
515 switch (mObservedUnderruns.mBitFields.mMostRecent) {
516 case UNDERRUN_FULL:
517 nowInUnderrun = ' ';
518 break;
519 case UNDERRUN_PARTIAL:
520 nowInUnderrun = '<';
521 break;
522 case UNDERRUN_EMPTY:
523 nowInUnderrun = '*';
524 break;
525 default:
526 nowInUnderrun = '?';
527 break;
528 }
Narayan Kamath1d6fa7a2014-02-11 13:47:53 +0000529 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 +0000530 "%08X %p %p 0x%03X %9u%c\n",
Marco Nelissenb2208842014-02-07 14:00:50 -0800531 active ? "yes" : "no",
Eric Laurent81784c32012-11-19 14:55:58 -0800532 (mClient == 0) ? getpid_cached : mClient->pid(),
533 mStreamType,
534 mFormat,
535 mChannelMask,
536 mSessionId,
Eric Laurent81784c32012-11-19 14:55:58 -0800537 mFrameCount,
538 stateChar,
Eric Laurent81784c32012-11-19 14:55:58 -0800539 mFillingUpStatus,
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800540 mAudioTrackServerProxy->getSampleRate(),
Glenn Kastenc56f3422014-03-21 17:53:17 -0700541 20.0 * log10(float_from_gain(gain_minifloat_unpack_left(vlr))),
542 20.0 * log10(float_from_gain(gain_minifloat_unpack_right(vlr))),
Glenn Kastenf20e1d82013-07-12 09:45:18 -0700543 mCblk->mServer,
Kévin PETIT377b2ec2014-02-03 12:35:36 +0000544 mMainBuffer,
545 mAuxBuffer,
Glenn Kasten96f60d82013-07-12 10:21:18 -0700546 mCblk->mFlags,
Glenn Kasten82aaf942013-07-17 16:05:07 -0700547 mAudioTrackServerProxy->getUnderrunFrames(),
Eric Laurent81784c32012-11-19 14:55:58 -0800548 nowInUnderrun);
549}
550
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800551uint32_t AudioFlinger::PlaybackThread::Track::sampleRate() const {
552 return mAudioTrackServerProxy->getSampleRate();
553}
554
Eric Laurent81784c32012-11-19 14:55:58 -0800555// AudioBufferProvider interface
556status_t AudioFlinger::PlaybackThread::Track::getNextBuffer(
Glenn Kastend79072e2016-01-06 08:41:20 -0800557 AudioBufferProvider::Buffer* buffer)
Eric Laurent81784c32012-11-19 14:55:58 -0800558{
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800559 ServerProxy::Buffer buf;
560 size_t desiredFrames = buffer->frameCount;
561 buf.mFrameCount = desiredFrames;
562 status_t status = mServerProxy->obtainBuffer(&buf);
563 buffer->frameCount = buf.mFrameCount;
564 buffer->raw = buf.mRaw;
565 if (buf.mFrameCount == 0) {
Glenn Kasten82aaf942013-07-17 16:05:07 -0700566 mAudioTrackServerProxy->tallyUnderrunFrames(desiredFrames);
Phil Burk2812d9e2016-01-04 10:34:30 -0800567 } else {
568 mAudioTrackServerProxy->tallyUnderrunFrames(0);
Eric Laurent81784c32012-11-19 14:55:58 -0800569 }
Phil Burk2812d9e2016-01-04 10:34:30 -0800570
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800571 return status;
Eric Laurent81784c32012-11-19 14:55:58 -0800572}
573
Glenn Kasten6466c9e2013-08-23 10:54:07 -0700574// releaseBuffer() is not overridden
575
576// ExtendedAudioBufferProvider interface
577
Andy Hung27876c02014-09-09 18:07:55 -0700578// framesReady() may return an approximation of the number of frames if called
579// from a different thread than the one calling Proxy->obtainBuffer() and
580// Proxy->releaseBuffer(). Also note there is no mutual exclusion in the
581// AudioTrackServerProxy so be especially careful calling with FastTracks.
Eric Laurent81784c32012-11-19 14:55:58 -0800582size_t AudioFlinger::PlaybackThread::Track::framesReady() const {
Andy Hung27876c02014-09-09 18:07:55 -0700583 if (mSharedBuffer != 0 && (isStopped() || isStopping())) {
584 // Static tracks return zero frames immediately upon stopping (for FastTracks).
585 // The remainder of the buffer is not drained.
586 return 0;
587 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800588 return mAudioTrackServerProxy->framesReady();
Eric Laurent81784c32012-11-19 14:55:58 -0800589}
590
Andy Hung818e7a32016-02-16 18:08:07 -0800591int64_t AudioFlinger::PlaybackThread::Track::framesReleased() const
Glenn Kasten6466c9e2013-08-23 10:54:07 -0700592{
593 return mAudioTrackServerProxy->framesReleased();
594}
595
Andy Hung818e7a32016-02-16 18:08:07 -0800596void AudioFlinger::PlaybackThread::Track::onTimestamp(const ExtendedTimestamp &timestamp)
Andy Hung6ae58432016-02-16 18:32:24 -0800597{
598 // This call comes from a FastTrack and should be kept lockless.
599 // The server side frames are already translated to client frames.
Andy Hung818e7a32016-02-16 18:08:07 -0800600 mAudioTrackServerProxy->setTimestamp(timestamp);
Andy Hung6ae58432016-02-16 18:32:24 -0800601
Andy Hung818e7a32016-02-16 18:08:07 -0800602 // We do not set drained here, as FastTrack timestamp may not go to very last frame.
Andy Hung6ae58432016-02-16 18:32:24 -0800603}
604
Eric Laurent81784c32012-11-19 14:55:58 -0800605// Don't call for fast tracks; the framesReady() could result in priority inversion
606bool AudioFlinger::PlaybackThread::Track::isReady() const {
Krishnankutty Kolathappilly8d6c2922014-02-04 16:23:42 -0800607 if (mFillingUpStatus != FS_FILLING || isStopped() || isPausing()) {
608 return true;
609 }
610
Eric Laurent16498512014-03-17 17:22:08 -0700611 if (isStopping()) {
612 if (framesReady() > 0) {
613 mFillingUpStatus = FS_FILLED;
614 }
Eric Laurent81784c32012-11-19 14:55:58 -0800615 return true;
616 }
617
Phil Burke8972b02016-03-04 11:29:57 -0800618 if (framesReady() >= mServerProxy->getBufferSizeInFrames() ||
Glenn Kasten96f60d82013-07-12 10:21:18 -0700619 (mCblk->mFlags & CBLK_FORCEREADY)) {
Eric Laurent81784c32012-11-19 14:55:58 -0800620 mFillingUpStatus = FS_FILLED;
Glenn Kasten96f60d82013-07-12 10:21:18 -0700621 android_atomic_and(~CBLK_FORCEREADY, &mCblk->mFlags);
Eric Laurent81784c32012-11-19 14:55:58 -0800622 return true;
623 }
624 return false;
625}
626
Glenn Kasten0f11b512014-01-31 16:18:54 -0800627status_t AudioFlinger::PlaybackThread::Track::start(AudioSystem::sync_event_t event __unused,
Glenn Kastend848eb42016-03-08 13:42:11 -0800628 audio_session_t triggerSession __unused)
Eric Laurent81784c32012-11-19 14:55:58 -0800629{
630 status_t status = NO_ERROR;
631 ALOGV("start(%d), calling pid %d session %d",
632 mName, IPCThreadState::self()->getCallingPid(), mSessionId);
633
634 sp<ThreadBase> thread = mThread.promote();
635 if (thread != 0) {
Eric Laurent813e2a72013-08-31 12:59:48 -0700636 if (isOffloaded()) {
637 Mutex::Autolock _laf(thread->mAudioFlinger->mLock);
638 Mutex::Autolock _lth(thread->mLock);
639 sp<EffectChain> ec = thread->getEffectChain_l(mSessionId);
Eric Laurent5baf2af2013-09-12 17:37:00 -0700640 if (thread->mAudioFlinger->isNonOffloadableGlobalEffectEnabled_l() ||
641 (ec != 0 && ec->isNonOffloadableEnabled())) {
Eric Laurent813e2a72013-08-31 12:59:48 -0700642 invalidate();
643 return PERMISSION_DENIED;
644 }
645 }
646 Mutex::Autolock _lth(thread->mLock);
Eric Laurent81784c32012-11-19 14:55:58 -0800647 track_state state = mState;
648 // here the track could be either new, or restarted
649 // in both cases "unstop" the track
Eric Laurentbfb1b832013-01-07 09:53:42 -0800650
Krishnankutty Kolathappilly8d6c2922014-02-04 16:23:42 -0800651 // initial state-stopping. next state-pausing.
652 // What if resume is called ?
653
654 if (state == PAUSED || state == PAUSING) {
Eric Laurentbfb1b832013-01-07 09:53:42 -0800655 if (mResumeToStopping) {
656 // happened we need to resume to STOPPING_1
657 mState = TrackBase::STOPPING_1;
658 ALOGV("PAUSED => STOPPING_1 (%d) on thread %p", mName, this);
659 } else {
660 mState = TrackBase::RESUMING;
661 ALOGV("PAUSED => RESUMING (%d) on thread %p", mName, this);
662 }
Eric Laurent81784c32012-11-19 14:55:58 -0800663 } else {
664 mState = TrackBase::ACTIVE;
665 ALOGV("? => ACTIVE (%d) on thread %p", mName, this);
666 }
667
Andy Hunge10393e2015-06-12 13:59:33 -0700668 // states to reset position info for non-offloaded/direct tracks
669 if (!isOffloaded() && !isDirect()
670 && (state == IDLE || state == STOPPED || state == FLUSHED)) {
671 mFrameMap.reset();
672 }
Eric Laurentbfb1b832013-01-07 09:53:42 -0800673 PlaybackThread *playbackThread = (PlaybackThread *)thread.get();
Haynes Mathew George240934b2015-03-11 18:25:50 -0700674 if (isFastTrack()) {
675 // refresh fast track underruns on start because that field is never cleared
676 // by the fast mixer; furthermore, the same track can be recycled, i.e. start
677 // after stop.
678 mObservedUnderruns = playbackThread->getFastTrackUnderruns(mFastIndex);
679 }
Eric Laurentbfb1b832013-01-07 09:53:42 -0800680 status = playbackThread->addTrack_l(this);
681 if (status == INVALID_OPERATION || status == PERMISSION_DENIED) {
Eric Laurent81784c32012-11-19 14:55:58 -0800682 triggerEvents(AudioSystem::SYNC_EVENT_PRESENTATION_COMPLETE);
Eric Laurentbfb1b832013-01-07 09:53:42 -0800683 // restore previous state if start was rejected by policy manager
684 if (status == PERMISSION_DENIED) {
685 mState = state;
686 }
687 }
688 // track was already in the active list, not a problem
689 if (status == ALREADY_EXISTS) {
690 status = NO_ERROR;
Glenn Kasten12022ff2013-10-17 11:32:39 -0700691 } else {
692 // Acknowledge any pending flush(), so that subsequent new data isn't discarded.
693 // It is usually unsafe to access the server proxy from a binder thread.
694 // But in this case we know the mixer thread (whether normal mixer or fast mixer)
695 // isn't looking at this track yet: we still hold the normal mixer thread lock,
696 // and for fast tracks the track is not yet in the fast mixer thread's active set.
Andy Hunge6fb82a2015-09-09 14:39:02 -0700697 // For static tracks, this is used to acknowledge change in position or loop.
Eric Laurent564d1442015-09-09 12:26:52 -0700698 ServerProxy::Buffer buffer;
699 buffer.mFrameCount = 1;
700 (void) mAudioTrackServerProxy->obtainBuffer(&buffer, true /*ackFlush*/);
Eric Laurent81784c32012-11-19 14:55:58 -0800701 }
702 } else {
703 status = BAD_VALUE;
704 }
705 return status;
706}
707
708void AudioFlinger::PlaybackThread::Track::stop()
709{
710 ALOGV("stop(%d), calling pid %d", mName, IPCThreadState::self()->getCallingPid());
711 sp<ThreadBase> thread = mThread.promote();
712 if (thread != 0) {
713 Mutex::Autolock _l(thread->mLock);
714 track_state state = mState;
715 if (state == RESUMING || state == ACTIVE || state == PAUSING || state == PAUSED) {
716 // If the track is not active (PAUSED and buffers full), flush buffers
717 PlaybackThread *playbackThread = (PlaybackThread *)thread.get();
718 if (playbackThread->mActiveTracks.indexOf(this) < 0) {
719 reset();
720 mState = STOPPED;
Eric Laurentab5cdba2014-06-09 17:22:27 -0700721 } else if (!isFastTrack() && !isOffloaded() && !isDirect()) {
Eric Laurent81784c32012-11-19 14:55:58 -0800722 mState = STOPPED;
723 } else {
Eric Laurentbfb1b832013-01-07 09:53:42 -0800724 // For fast tracks prepareTracks_l() will set state to STOPPING_2
725 // presentation is complete
726 // For an offloaded track this starts a drain and state will
727 // move to STOPPING_2 when drain completes and then STOPPED
Eric Laurent81784c32012-11-19 14:55:58 -0800728 mState = STOPPING_1;
729 }
Eric Laurentb369caf2015-03-30 20:51:47 -0700730 playbackThread->broadcast_l();
Eric Laurent81784c32012-11-19 14:55:58 -0800731 ALOGV("not stopping/stopped => stopping/stopped (%d) on thread %p", mName,
732 playbackThread);
733 }
Eric Laurent81784c32012-11-19 14:55:58 -0800734 }
735}
736
737void AudioFlinger::PlaybackThread::Track::pause()
738{
739 ALOGV("pause(%d), calling pid %d", mName, IPCThreadState::self()->getCallingPid());
740 sp<ThreadBase> thread = mThread.promote();
741 if (thread != 0) {
742 Mutex::Autolock _l(thread->mLock);
Eric Laurentbfb1b832013-01-07 09:53:42 -0800743 PlaybackThread *playbackThread = (PlaybackThread *)thread.get();
744 switch (mState) {
745 case STOPPING_1:
746 case STOPPING_2:
747 if (!isOffloaded()) {
748 /* nothing to do if track is not offloaded */
749 break;
750 }
751
752 // Offloaded track was draining, we need to carry on draining when resumed
753 mResumeToStopping = true;
754 // fall through...
755 case ACTIVE:
756 case RESUMING:
Eric Laurent81784c32012-11-19 14:55:58 -0800757 mState = PAUSING;
758 ALOGV("ACTIVE/RESUMING => PAUSING (%d) on thread %p", mName, thread.get());
Eric Laurentede6c3b2013-09-19 14:37:46 -0700759 playbackThread->broadcast_l();
Eric Laurentbfb1b832013-01-07 09:53:42 -0800760 break;
Eric Laurent81784c32012-11-19 14:55:58 -0800761
Eric Laurentbfb1b832013-01-07 09:53:42 -0800762 default:
763 break;
Eric Laurent81784c32012-11-19 14:55:58 -0800764 }
765 }
766}
767
768void AudioFlinger::PlaybackThread::Track::flush()
769{
770 ALOGV("flush(%d)", mName);
771 sp<ThreadBase> thread = mThread.promote();
772 if (thread != 0) {
773 Mutex::Autolock _l(thread->mLock);
Eric Laurent81784c32012-11-19 14:55:58 -0800774 PlaybackThread *playbackThread = (PlaybackThread *)thread.get();
Eric Laurentbfb1b832013-01-07 09:53:42 -0800775
776 if (isOffloaded()) {
777 // If offloaded we allow flush during any state except terminated
778 // and keep the track active to avoid problems if user is seeking
779 // rapidly and underlying hardware has a significant delay handling
780 // a pause
781 if (isTerminated()) {
782 return;
783 }
784
785 ALOGV("flush: offload flush");
Eric Laurent81784c32012-11-19 14:55:58 -0800786 reset();
Eric Laurentbfb1b832013-01-07 09:53:42 -0800787
788 if (mState == STOPPING_1 || mState == STOPPING_2) {
789 ALOGV("flushed in STOPPING_1 or 2 state, change state to ACTIVE");
790 mState = ACTIVE;
791 }
792
Haynes Mathew George7844f672014-01-15 12:32:55 -0800793 mFlushHwPending = true;
Eric Laurentbfb1b832013-01-07 09:53:42 -0800794 mResumeToStopping = false;
795 } else {
796 if (mState != STOPPING_1 && mState != STOPPING_2 && mState != STOPPED &&
797 mState != PAUSED && mState != PAUSING && mState != IDLE && mState != FLUSHED) {
798 return;
799 }
800 // No point remaining in PAUSED state after a flush => go to
801 // FLUSHED state
802 mState = FLUSHED;
803 // do not reset the track if it is still in the process of being stopped or paused.
804 // this will be done by prepareTracks_l() when the track is stopped.
805 // prepareTracks_l() will see mState == FLUSHED, then
806 // remove from active track list, reset(), and trigger presentation complete
Eric Laurentd1f69b02014-12-15 14:33:13 -0800807 if (isDirect()) {
808 mFlushHwPending = true;
809 }
Eric Laurentbfb1b832013-01-07 09:53:42 -0800810 if (playbackThread->mActiveTracks.indexOf(this) < 0) {
811 reset();
812 }
Eric Laurent81784c32012-11-19 14:55:58 -0800813 }
Eric Laurentbfb1b832013-01-07 09:53:42 -0800814 // Prevent flush being lost if the track is flushed and then resumed
815 // before mixer thread can run. This is important when offloading
816 // because the hardware buffer could hold a large amount of audio
Eric Laurentede6c3b2013-09-19 14:37:46 -0700817 playbackThread->broadcast_l();
Eric Laurent81784c32012-11-19 14:55:58 -0800818 }
819}
820
Haynes Mathew George7844f672014-01-15 12:32:55 -0800821// must be called with thread lock held
822void AudioFlinger::PlaybackThread::Track::flushAck()
823{
Eric Laurentd1f69b02014-12-15 14:33:13 -0800824 if (!isOffloaded() && !isDirect())
Haynes Mathew George7844f672014-01-15 12:32:55 -0800825 return;
826
827 mFlushHwPending = false;
828}
829
Eric Laurent81784c32012-11-19 14:55:58 -0800830void AudioFlinger::PlaybackThread::Track::reset()
831{
832 // Do not reset twice to avoid discarding data written just after a flush and before
833 // the audioflinger thread detects the track is stopped.
834 if (!mResetDone) {
Eric Laurent81784c32012-11-19 14:55:58 -0800835 // Force underrun condition to avoid false underrun callback until first data is
836 // written to buffer
Glenn Kasten96f60d82013-07-12 10:21:18 -0700837 android_atomic_and(~CBLK_FORCEREADY, &mCblk->mFlags);
Eric Laurent81784c32012-11-19 14:55:58 -0800838 mFillingUpStatus = FS_FILLING;
839 mResetDone = true;
840 if (mState == FLUSHED) {
841 mState = IDLE;
842 }
843 }
844}
845
Eric Laurentbfb1b832013-01-07 09:53:42 -0800846status_t AudioFlinger::PlaybackThread::Track::setParameters(const String8& keyValuePairs)
847{
848 sp<ThreadBase> thread = mThread.promote();
849 if (thread == 0) {
850 ALOGE("thread is dead");
851 return FAILED_TRANSACTION;
852 } else if ((thread->type() == ThreadBase::DIRECT) ||
853 (thread->type() == ThreadBase::OFFLOAD)) {
854 return thread->setParameters(keyValuePairs);
855 } else {
856 return PERMISSION_DENIED;
857 }
858}
859
Glenn Kasten573d80a2013-08-26 09:36:23 -0700860status_t AudioFlinger::PlaybackThread::Track::getTimestamp(AudioTimestamp& timestamp)
861{
Andy Hung818e7a32016-02-16 18:08:07 -0800862 if (!isOffloaded() && !isDirect()) {
863 return INVALID_OPERATION; // normal tracks handled through SSQ
Glenn Kastenfe346c72013-08-30 13:28:22 -0700864 }
Glenn Kasten573d80a2013-08-26 09:36:23 -0700865 sp<ThreadBase> thread = mThread.promote();
866 if (thread == 0) {
Glenn Kastenfe346c72013-08-30 13:28:22 -0700867 return INVALID_OPERATION;
Glenn Kasten573d80a2013-08-26 09:36:23 -0700868 }
Phil Burk6140c792015-03-19 14:30:21 -0700869
Glenn Kasten573d80a2013-08-26 09:36:23 -0700870 Mutex::Autolock _l(thread->mLock);
871 PlaybackThread *playbackThread = (PlaybackThread *)thread.get();
Andy Hung818e7a32016-02-16 18:08:07 -0800872 return playbackThread->getTimestamp_l(timestamp);
Glenn Kasten573d80a2013-08-26 09:36:23 -0700873}
874
Eric Laurent81784c32012-11-19 14:55:58 -0800875status_t AudioFlinger::PlaybackThread::Track::attachAuxEffect(int EffectId)
876{
877 status_t status = DEAD_OBJECT;
878 sp<ThreadBase> thread = mThread.promote();
879 if (thread != 0) {
880 PlaybackThread *playbackThread = (PlaybackThread *)thread.get();
881 sp<AudioFlinger> af = mClient->audioFlinger();
882
883 Mutex::Autolock _l(af->mLock);
884
885 sp<PlaybackThread> srcThread = af->getEffectThread_l(AUDIO_SESSION_OUTPUT_MIX, EffectId);
886
887 if (EffectId != 0 && srcThread != 0 && playbackThread != srcThread.get()) {
888 Mutex::Autolock _dl(playbackThread->mLock);
889 Mutex::Autolock _sl(srcThread->mLock);
890 sp<EffectChain> chain = srcThread->getEffectChain_l(AUDIO_SESSION_OUTPUT_MIX);
891 if (chain == 0) {
892 return INVALID_OPERATION;
893 }
894
895 sp<EffectModule> effect = chain->getEffectFromId_l(EffectId);
896 if (effect == 0) {
897 return INVALID_OPERATION;
898 }
899 srcThread->removeEffect_l(effect);
Eric Laurent5baf2af2013-09-12 17:37:00 -0700900 status = playbackThread->addEffect_l(effect);
901 if (status != NO_ERROR) {
902 srcThread->addEffect_l(effect);
903 return INVALID_OPERATION;
904 }
Eric Laurent81784c32012-11-19 14:55:58 -0800905 // removeEffect_l() has stopped the effect if it was active so it must be restarted
906 if (effect->state() == EffectModule::ACTIVE ||
907 effect->state() == EffectModule::STOPPING) {
908 effect->start();
909 }
910
911 sp<EffectChain> dstChain = effect->chain().promote();
912 if (dstChain == 0) {
913 srcThread->addEffect_l(effect);
914 return INVALID_OPERATION;
915 }
916 AudioSystem::unregisterEffect(effect->id());
917 AudioSystem::registerEffect(&effect->desc(),
918 srcThread->id(),
919 dstChain->strategy(),
920 AUDIO_SESSION_OUTPUT_MIX,
921 effect->id());
Eric Laurentd72b7c02013-10-12 16:17:46 -0700922 AudioSystem::setEffectEnabled(effect->id(), effect->isEnabled());
Eric Laurent81784c32012-11-19 14:55:58 -0800923 }
924 status = playbackThread->attachAuxEffect(this, EffectId);
925 }
926 return status;
927}
928
929void AudioFlinger::PlaybackThread::Track::setAuxBuffer(int EffectId, int32_t *buffer)
930{
931 mAuxEffectId = EffectId;
932 mAuxBuffer = buffer;
933}
934
Andy Hung818e7a32016-02-16 18:08:07 -0800935bool AudioFlinger::PlaybackThread::Track::presentationComplete(
936 int64_t framesWritten, size_t audioHalFrames)
Eric Laurent81784c32012-11-19 14:55:58 -0800937{
Andy Hung818e7a32016-02-16 18:08:07 -0800938 // TODO: improve this based on FrameMap if it exists, to ensure full drain.
939 // This assists in proper timestamp computation as well as wakelock management.
940
Eric Laurent81784c32012-11-19 14:55:58 -0800941 // a track is considered presented when the total number of frames written to audio HAL
942 // corresponds to the number of frames written when presentationComplete() is called for the
943 // first time (mPresentationCompleteFrames == 0) plus the buffer filling status at that time.
Eric Laurentbfb1b832013-01-07 09:53:42 -0800944 // For an offloaded track the HAL+h/w delay is variable so a HAL drain() is used
945 // to detect when all frames have been played. In this case framesWritten isn't
946 // useful because it doesn't always reflect whether there is data in the h/w
947 // buffers, particularly if a track has been paused and resumed during draining
Andy Hung818e7a32016-02-16 18:08:07 -0800948 ALOGV("presentationComplete() mPresentationCompleteFrames %lld framesWritten %lld",
949 (long long)mPresentationCompleteFrames, (long long)framesWritten);
Eric Laurent81784c32012-11-19 14:55:58 -0800950 if (mPresentationCompleteFrames == 0) {
951 mPresentationCompleteFrames = framesWritten + audioHalFrames;
Andy Hung818e7a32016-02-16 18:08:07 -0800952 ALOGV("presentationComplete() reset: mPresentationCompleteFrames %lld audioHalFrames %zu",
953 (long long)mPresentationCompleteFrames, audioHalFrames);
Eric Laurent81784c32012-11-19 14:55:58 -0800954 }
Eric Laurentbfb1b832013-01-07 09:53:42 -0800955
Andy Hungc54b1ff2016-02-23 14:07:07 -0800956 bool complete;
957 if (isOffloaded()) {
958 complete = true;
959 } else if (isDirect() || isFastTrack()) { // these do not go through linear map
Glenn Kastenc42e9b42016-03-21 11:35:03 -0700960 complete = framesWritten >= (int64_t) mPresentationCompleteFrames;
Andy Hungc54b1ff2016-02-23 14:07:07 -0800961 } else { // Normal tracks, OutputTracks, and PatchTracks
Glenn Kastenc42e9b42016-03-21 11:35:03 -0700962 complete = framesWritten >= (int64_t) mPresentationCompleteFrames
Andy Hungc54b1ff2016-02-23 14:07:07 -0800963 && mAudioTrackServerProxy->isDrained();
964 }
965
966 if (complete) {
Eric Laurent81784c32012-11-19 14:55:58 -0800967 triggerEvents(AudioSystem::SYNC_EVENT_PRESENTATION_COMPLETE);
Eric Laurentbfb1b832013-01-07 09:53:42 -0800968 mAudioTrackServerProxy->setStreamEndDone();
Eric Laurent81784c32012-11-19 14:55:58 -0800969 return true;
970 }
971 return false;
972}
973
974void AudioFlinger::PlaybackThread::Track::triggerEvents(AudioSystem::sync_event_t type)
975{
Mark Salyzyn3ab368e2014-04-15 14:55:53 -0700976 for (size_t i = 0; i < mSyncEvents.size(); i++) {
Eric Laurent81784c32012-11-19 14:55:58 -0800977 if (mSyncEvents[i]->type() == type) {
978 mSyncEvents[i]->trigger();
979 mSyncEvents.removeAt(i);
980 i--;
981 }
982 }
983}
984
985// implement VolumeBufferProvider interface
986
Glenn Kastenc56f3422014-03-21 17:53:17 -0700987gain_minifloat_packed_t AudioFlinger::PlaybackThread::Track::getVolumeLR()
Eric Laurent81784c32012-11-19 14:55:58 -0800988{
989 // called by FastMixer, so not allowed to take any locks, block, or do I/O including logs
990 ALOG_ASSERT(isFastTrack() && (mCblk != NULL));
Glenn Kastenc56f3422014-03-21 17:53:17 -0700991 gain_minifloat_packed_t vlr = mAudioTrackServerProxy->getVolumeLR();
992 float vl = float_from_gain(gain_minifloat_unpack_left(vlr));
993 float vr = float_from_gain(gain_minifloat_unpack_right(vlr));
Eric Laurent81784c32012-11-19 14:55:58 -0800994 // track volumes come from shared memory, so can't be trusted and must be clamped
Glenn Kastenc56f3422014-03-21 17:53:17 -0700995 if (vl > GAIN_FLOAT_UNITY) {
996 vl = GAIN_FLOAT_UNITY;
Eric Laurent81784c32012-11-19 14:55:58 -0800997 }
Glenn Kastenc56f3422014-03-21 17:53:17 -0700998 if (vr > GAIN_FLOAT_UNITY) {
999 vr = GAIN_FLOAT_UNITY;
Eric Laurent81784c32012-11-19 14:55:58 -08001000 }
1001 // now apply the cached master volume and stream type volume;
1002 // this is trusted but lacks any synchronization or barrier so may be stale
1003 float v = mCachedVolume;
1004 vl *= v;
1005 vr *= v;
Glenn Kastenc56f3422014-03-21 17:53:17 -07001006 // re-combine into packed minifloat
1007 vlr = gain_minifloat_pack(gain_from_float(vl), gain_from_float(vr));
Eric Laurent81784c32012-11-19 14:55:58 -08001008 // FIXME look at mute, pause, and stop flags
1009 return vlr;
1010}
1011
1012status_t AudioFlinger::PlaybackThread::Track::setSyncEvent(const sp<SyncEvent>& event)
1013{
Eric Laurentbfb1b832013-01-07 09:53:42 -08001014 if (isTerminated() || mState == PAUSED ||
Eric Laurent81784c32012-11-19 14:55:58 -08001015 ((framesReady() == 0) && ((mSharedBuffer != 0) ||
1016 (mState == STOPPED)))) {
Glenn Kastenc42e9b42016-03-21 11:35:03 -07001017 ALOGW("Track::setSyncEvent() in invalid state %d on session %d %s mode, framesReady %zu",
Eric Laurent81784c32012-11-19 14:55:58 -08001018 mState, mSessionId, (mSharedBuffer != 0) ? "static" : "stream", framesReady());
1019 event->cancel();
1020 return INVALID_OPERATION;
1021 }
1022 (void) TrackBase::setSyncEvent(event);
1023 return NO_ERROR;
1024}
1025
Glenn Kasten5736c352012-12-04 12:12:34 -08001026void AudioFlinger::PlaybackThread::Track::invalidate()
1027{
Eric Laurent4d231dc2016-03-11 18:38:23 -08001028 signalClientFlag(CBLK_INVALID);
1029 mIsInvalid = true;
1030}
1031
1032void AudioFlinger::PlaybackThread::Track::disable()
1033{
1034 signalClientFlag(CBLK_DISABLED);
1035}
1036
1037void AudioFlinger::PlaybackThread::Track::signalClientFlag(int32_t flag)
1038{
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001039 // FIXME should use proxy, and needs work
1040 audio_track_cblk_t* cblk = mCblk;
Eric Laurent4d231dc2016-03-11 18:38:23 -08001041 android_atomic_or(flag, &cblk->mFlags);
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001042 android_atomic_release_store(0x40000000, &cblk->mFutex);
1043 // client is not in server, so FUTEX_WAKE is needed instead of FUTEX_WAKE_PRIVATE
Elliott Hughesee499292014-05-21 17:55:51 -07001044 (void) syscall(__NR_futex, &cblk->mFutex, FUTEX_WAKE, INT_MAX);
Glenn Kasten5736c352012-12-04 12:12:34 -08001045}
1046
Eric Laurent59fe0102013-09-27 18:48:26 -07001047void AudioFlinger::PlaybackThread::Track::signal()
1048{
1049 sp<ThreadBase> thread = mThread.promote();
1050 if (thread != 0) {
1051 PlaybackThread *t = (PlaybackThread *)thread.get();
1052 Mutex::Autolock _l(t->mLock);
1053 t->broadcast_l();
1054 }
1055}
1056
Krishnankutty Kolathappilly8d6c2922014-02-04 16:23:42 -08001057//To be called with thread lock held
1058bool AudioFlinger::PlaybackThread::Track::isResumePending() {
1059
1060 if (mState == RESUMING)
1061 return true;
1062 /* Resume is pending if track was stopping before pause was called */
1063 if (mState == STOPPING_1 &&
1064 mResumeToStopping)
1065 return true;
1066
1067 return false;
1068}
1069
1070//To be called with thread lock held
1071void AudioFlinger::PlaybackThread::Track::resumeAck() {
1072
1073
1074 if (mState == RESUMING)
1075 mState = ACTIVE;
Haynes Mathew George2d3ca682014-03-07 13:43:49 -08001076
Krishnankutty Kolathappilly8d6c2922014-02-04 16:23:42 -08001077 // Other possibility of pending resume is stopping_1 state
1078 // Do not update the state from stopping as this prevents
Haynes Mathew George2d3ca682014-03-07 13:43:49 -08001079 // drain being called.
1080 if (mState == STOPPING_1) {
1081 mResumeToStopping = false;
1082 }
Krishnankutty Kolathappilly8d6c2922014-02-04 16:23:42 -08001083}
Andy Hunge10393e2015-06-12 13:59:33 -07001084
1085//To be called with thread lock held
1086void AudioFlinger::PlaybackThread::Track::updateTrackFrameInfo(
Andy Hung818e7a32016-02-16 18:08:07 -08001087 int64_t trackFramesReleased, int64_t sinkFramesWritten,
1088 const ExtendedTimestamp &timeStamp) {
1089 //update frame map
Andy Hunge10393e2015-06-12 13:59:33 -07001090 mFrameMap.push(trackFramesReleased, sinkFramesWritten);
Andy Hung818e7a32016-02-16 18:08:07 -08001091
1092 // adjust server times and set drained state.
1093 //
1094 // Our timestamps are only updated when the track is on the Thread active list.
1095 // We need to ensure that tracks are not removed before full drain.
1096 ExtendedTimestamp local = timeStamp;
1097 bool checked = false;
1098 for (int i = ExtendedTimestamp::LOCATION_MAX - 1;
1099 i >= ExtendedTimestamp::LOCATION_SERVER; --i) {
1100 // Lookup the track frame corresponding to the sink frame position.
1101 if (local.mTimeNs[i] > 0) {
1102 local.mPosition[i] = mFrameMap.findX(local.mPosition[i]);
1103 // check drain state from the latest stage in the pipeline.
1104 if (!checked) {
1105 mAudioTrackServerProxy->setDrained(
1106 local.mPosition[i] >= mAudioTrackServerProxy->framesReleased());
1107 checked = true;
1108 }
1109 }
Andy Hunge10393e2015-06-12 13:59:33 -07001110 }
Andy Hung818e7a32016-02-16 18:08:07 -08001111 if (!checked) { // no server info, assume drained.
1112 mAudioTrackServerProxy->setDrained(true);
1113 }
Andy Hungea2b9c02016-02-12 17:06:53 -08001114 // Set correction for flushed frames that are not accounted for in released.
1115 // This is important for the new 64 bit timestamps which do not reset to 0 on flush.
1116 local.mFlushed = mAudioTrackServerProxy->framesFlushed();
Andy Hung818e7a32016-02-16 18:08:07 -08001117 mServerProxy->setTimestamp(local);
Andy Hunge10393e2015-06-12 13:59:33 -07001118}
1119
Eric Laurent81784c32012-11-19 14:55:58 -08001120// ----------------------------------------------------------------------------
1121
Eric Laurent81784c32012-11-19 14:55:58 -08001122AudioFlinger::PlaybackThread::OutputTrack::OutputTrack(
1123 PlaybackThread *playbackThread,
1124 DuplicatingThread *sourceThread,
1125 uint32_t sampleRate,
1126 audio_format_t format,
1127 audio_channel_mask_t channelMask,
Marco Nelissen462fd2f2013-01-14 14:12:05 -08001128 size_t frameCount,
1129 int uid)
Eric Laurent223fd5c2014-11-11 13:43:36 -08001130 : Track(playbackThread, NULL, AUDIO_STREAM_PATCH,
1131 sampleRate, format, channelMask, frameCount,
Glenn Kastend848eb42016-03-08 13:42:11 -08001132 NULL, 0, AUDIO_SESSION_NONE, uid, IAudioFlinger::TRACK_DEFAULT,
1133 TYPE_OUTPUT),
Eric Laurent5bba2f62016-03-18 11:14:14 -07001134 mActive(false), mSourceThread(sourceThread)
Eric Laurent81784c32012-11-19 14:55:58 -08001135{
1136
1137 if (mCblk != NULL) {
Eric Laurent81784c32012-11-19 14:55:58 -08001138 mOutBuffer.frameCount = 0;
1139 playbackThread->mTracks.add(this);
Glenn Kastene3aa6592012-12-04 12:22:46 -08001140 ALOGV("OutputTrack constructor mCblk %p, mBuffer %p, "
Glenn Kastenc42e9b42016-03-21 11:35:03 -07001141 "frameCount %zu, mChannelMask 0x%08x",
Glenn Kastene3aa6592012-12-04 12:22:46 -08001142 mCblk, mBuffer,
Glenn Kasten74935e42013-12-19 08:56:45 -08001143 frameCount, mChannelMask);
Glenn Kastene3aa6592012-12-04 12:22:46 -08001144 // since client and server are in the same process,
1145 // the buffer has the same virtual address on both sides
Glenn Kasten529c61b2014-07-18 15:31:02 -07001146 mClientProxy = new AudioTrackClientProxy(mCblk, mBuffer, mFrameCount, mFrameSize,
1147 true /*clientInServer*/);
Glenn Kastenc56f3422014-03-21 17:53:17 -07001148 mClientProxy->setVolumeLR(GAIN_MINIFLOAT_PACKED_UNITY);
Eric Laurent8d2d4932013-04-25 12:56:18 -07001149 mClientProxy->setSendLevel(0.0);
1150 mClientProxy->setSampleRate(sampleRate);
Eric Laurent81784c32012-11-19 14:55:58 -08001151 } else {
1152 ALOGW("Error creating output track on thread %p", playbackThread);
1153 }
1154}
1155
1156AudioFlinger::PlaybackThread::OutputTrack::~OutputTrack()
1157{
1158 clearBufferQueue();
Glenn Kastene3aa6592012-12-04 12:22:46 -08001159 // superclass destructor will now delete the server proxy and shared memory both refer to
Eric Laurent81784c32012-11-19 14:55:58 -08001160}
1161
1162status_t AudioFlinger::PlaybackThread::OutputTrack::start(AudioSystem::sync_event_t event,
Glenn Kastend848eb42016-03-08 13:42:11 -08001163 audio_session_t triggerSession)
Eric Laurent81784c32012-11-19 14:55:58 -08001164{
1165 status_t status = Track::start(event, triggerSession);
1166 if (status != NO_ERROR) {
1167 return status;
1168 }
1169
1170 mActive = true;
1171 mRetryCount = 127;
1172 return status;
1173}
1174
1175void AudioFlinger::PlaybackThread::OutputTrack::stop()
1176{
1177 Track::stop();
1178 clearBufferQueue();
1179 mOutBuffer.frameCount = 0;
1180 mActive = false;
1181}
1182
Andy Hungc25b84a2015-01-14 19:04:10 -08001183bool AudioFlinger::PlaybackThread::OutputTrack::write(void* data, uint32_t frames)
Eric Laurent81784c32012-11-19 14:55:58 -08001184{
1185 Buffer *pInBuffer;
1186 Buffer inBuffer;
Eric Laurent81784c32012-11-19 14:55:58 -08001187 bool outputBufferFull = false;
1188 inBuffer.frameCount = frames;
Andy Hungc25b84a2015-01-14 19:04:10 -08001189 inBuffer.raw = data;
Eric Laurent81784c32012-11-19 14:55:58 -08001190
1191 uint32_t waitTimeLeftMs = mSourceThread->waitTimeMs();
1192
1193 if (!mActive && frames != 0) {
Andy Hung5bedff62015-01-16 11:05:32 -08001194 (void) start();
Eric Laurent81784c32012-11-19 14:55:58 -08001195 }
1196
1197 while (waitTimeLeftMs) {
1198 // First write pending buffers, then new data
1199 if (mBufferQueue.size()) {
1200 pInBuffer = mBufferQueue.itemAt(0);
1201 } else {
1202 pInBuffer = &inBuffer;
1203 }
1204
1205 if (pInBuffer->frameCount == 0) {
1206 break;
1207 }
1208
1209 if (mOutBuffer.frameCount == 0) {
1210 mOutBuffer.frameCount = pInBuffer->frameCount;
1211 nsecs_t startTime = systemTime();
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001212 status_t status = obtainBuffer(&mOutBuffer, waitTimeLeftMs);
Eric Laurent4d231dc2016-03-11 18:38:23 -08001213 if (status != NO_ERROR && status != NOT_ENOUGH_DATA) {
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001214 ALOGV("OutputTrack::write() %p thread %p no more output buffers; status %d", this,
1215 mThread.unsafe_get(), status);
Eric Laurent81784c32012-11-19 14:55:58 -08001216 outputBufferFull = true;
1217 break;
1218 }
1219 uint32_t waitTimeMs = (uint32_t)ns2ms(systemTime() - startTime);
1220 if (waitTimeLeftMs >= waitTimeMs) {
1221 waitTimeLeftMs -= waitTimeMs;
1222 } else {
1223 waitTimeLeftMs = 0;
1224 }
Eric Laurent4d231dc2016-03-11 18:38:23 -08001225 if (status == NOT_ENOUGH_DATA) {
1226 restartIfDisabled();
1227 continue;
1228 }
Eric Laurent81784c32012-11-19 14:55:58 -08001229 }
1230
1231 uint32_t outFrames = pInBuffer->frameCount > mOutBuffer.frameCount ? mOutBuffer.frameCount :
1232 pInBuffer->frameCount;
Andy Hungc25b84a2015-01-14 19:04:10 -08001233 memcpy(mOutBuffer.raw, pInBuffer->raw, outFrames * mFrameSize);
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001234 Proxy::Buffer buf;
1235 buf.mFrameCount = outFrames;
1236 buf.mRaw = NULL;
1237 mClientProxy->releaseBuffer(&buf);
Eric Laurent4d231dc2016-03-11 18:38:23 -08001238 restartIfDisabled();
Eric Laurent81784c32012-11-19 14:55:58 -08001239 pInBuffer->frameCount -= outFrames;
Andy Hungc25b84a2015-01-14 19:04:10 -08001240 pInBuffer->raw = (int8_t *)pInBuffer->raw + outFrames * mFrameSize;
Eric Laurent81784c32012-11-19 14:55:58 -08001241 mOutBuffer.frameCount -= outFrames;
Andy Hungc25b84a2015-01-14 19:04:10 -08001242 mOutBuffer.raw = (int8_t *)mOutBuffer.raw + outFrames * mFrameSize;
Eric Laurent81784c32012-11-19 14:55:58 -08001243
1244 if (pInBuffer->frameCount == 0) {
1245 if (mBufferQueue.size()) {
1246 mBufferQueue.removeAt(0);
Andy Hungc25b84a2015-01-14 19:04:10 -08001247 free(pInBuffer->mBuffer);
Eric Laurent81784c32012-11-19 14:55:58 -08001248 delete pInBuffer;
Glenn Kastenc42e9b42016-03-21 11:35:03 -07001249 ALOGV("OutputTrack::write() %p thread %p released overflow buffer %zu", this,
Eric Laurent81784c32012-11-19 14:55:58 -08001250 mThread.unsafe_get(), mBufferQueue.size());
1251 } else {
1252 break;
1253 }
1254 }
1255 }
1256
1257 // If we could not write all frames, allocate a buffer and queue it for next time.
1258 if (inBuffer.frameCount) {
1259 sp<ThreadBase> thread = mThread.promote();
1260 if (thread != 0 && !thread->standby()) {
1261 if (mBufferQueue.size() < kMaxOverFlowBuffers) {
1262 pInBuffer = new Buffer;
Andy Hungc25b84a2015-01-14 19:04:10 -08001263 pInBuffer->mBuffer = malloc(inBuffer.frameCount * mFrameSize);
Eric Laurent81784c32012-11-19 14:55:58 -08001264 pInBuffer->frameCount = inBuffer.frameCount;
Andy Hungc25b84a2015-01-14 19:04:10 -08001265 pInBuffer->raw = pInBuffer->mBuffer;
1266 memcpy(pInBuffer->raw, inBuffer.raw, inBuffer.frameCount * mFrameSize);
Eric Laurent81784c32012-11-19 14:55:58 -08001267 mBufferQueue.add(pInBuffer);
Glenn Kastenc42e9b42016-03-21 11:35:03 -07001268 ALOGV("OutputTrack::write() %p thread %p adding overflow buffer %zu", this,
Eric Laurent81784c32012-11-19 14:55:58 -08001269 mThread.unsafe_get(), mBufferQueue.size());
1270 } else {
1271 ALOGW("OutputTrack::write() %p thread %p no more overflow buffers",
1272 mThread.unsafe_get(), this);
1273 }
1274 }
1275 }
1276
Andy Hungc25b84a2015-01-14 19:04:10 -08001277 // Calling write() with a 0 length buffer means that no more data will be written:
1278 // We rely on stop() to set the appropriate flags to allow the remaining frames to play out.
1279 if (frames == 0 && mBufferQueue.size() == 0 && mActive) {
1280 stop();
Eric Laurent81784c32012-11-19 14:55:58 -08001281 }
1282
1283 return outputBufferFull;
1284}
1285
1286status_t AudioFlinger::PlaybackThread::OutputTrack::obtainBuffer(
1287 AudioBufferProvider::Buffer* buffer, uint32_t waitTimeMs)
1288{
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001289 ClientProxy::Buffer buf;
1290 buf.mFrameCount = buffer->frameCount;
1291 struct timespec timeout;
1292 timeout.tv_sec = waitTimeMs / 1000;
1293 timeout.tv_nsec = (int) (waitTimeMs % 1000) * 1000000;
1294 status_t status = mClientProxy->obtainBuffer(&buf, &timeout);
1295 buffer->frameCount = buf.mFrameCount;
1296 buffer->raw = buf.mRaw;
1297 return status;
Eric Laurent81784c32012-11-19 14:55:58 -08001298}
1299
Eric Laurent81784c32012-11-19 14:55:58 -08001300void AudioFlinger::PlaybackThread::OutputTrack::clearBufferQueue()
1301{
1302 size_t size = mBufferQueue.size();
1303
1304 for (size_t i = 0; i < size; i++) {
1305 Buffer *pBuffer = mBufferQueue.itemAt(i);
Andy Hungc25b84a2015-01-14 19:04:10 -08001306 free(pBuffer->mBuffer);
Eric Laurent81784c32012-11-19 14:55:58 -08001307 delete pBuffer;
1308 }
1309 mBufferQueue.clear();
1310}
1311
Eric Laurent4d231dc2016-03-11 18:38:23 -08001312void AudioFlinger::PlaybackThread::OutputTrack::restartIfDisabled()
1313{
1314 int32_t flags = android_atomic_and(~CBLK_DISABLED, &mCblk->mFlags);
1315 if (mActive && (flags & CBLK_DISABLED)) {
1316 start();
1317 }
1318}
Eric Laurent81784c32012-11-19 14:55:58 -08001319
Eric Laurent83b88082014-06-20 18:31:16 -07001320AudioFlinger::PlaybackThread::PatchTrack::PatchTrack(PlaybackThread *playbackThread,
Eric Laurent3bcf8592015-04-03 12:13:24 -07001321 audio_stream_type_t streamType,
Eric Laurent83b88082014-06-20 18:31:16 -07001322 uint32_t sampleRate,
1323 audio_channel_mask_t channelMask,
1324 audio_format_t format,
1325 size_t frameCount,
1326 void *buffer,
1327 IAudioFlinger::track_flags_t flags)
Eric Laurent3bcf8592015-04-03 12:13:24 -07001328 : Track(playbackThread, NULL, streamType,
Eric Laurent223fd5c2014-11-11 13:43:36 -08001329 sampleRate, format, channelMask, frameCount,
Glenn Kastend848eb42016-03-08 13:42:11 -08001330 buffer, 0, AUDIO_SESSION_NONE, getuid(), flags, TYPE_PATCH),
Eric Laurent83b88082014-06-20 18:31:16 -07001331 mProxy(new ClientProxy(mCblk, mBuffer, frameCount, mFrameSize, true, true))
1332{
1333 uint64_t mixBufferNs = ((uint64_t)2 * playbackThread->frameCount() * 1000000000) /
1334 playbackThread->sampleRate();
1335 mPeerTimeout.tv_sec = mixBufferNs / 1000000000;
1336 mPeerTimeout.tv_nsec = (int) (mixBufferNs % 1000000000);
1337
1338 ALOGV("PatchTrack %p sampleRate %d mPeerTimeout %d.%03d sec",
1339 this, sampleRate,
1340 (int)mPeerTimeout.tv_sec,
1341 (int)(mPeerTimeout.tv_nsec / 1000000));
1342}
1343
1344AudioFlinger::PlaybackThread::PatchTrack::~PatchTrack()
1345{
1346}
1347
Eric Laurent4d231dc2016-03-11 18:38:23 -08001348status_t AudioFlinger::PlaybackThread::PatchTrack::start(AudioSystem::sync_event_t event,
Glenn Kastend848eb42016-03-08 13:42:11 -08001349 audio_session_t triggerSession)
Eric Laurent4d231dc2016-03-11 18:38:23 -08001350{
1351 status_t status = Track::start(event, triggerSession);
1352 if (status != NO_ERROR) {
1353 return status;
1354 }
1355 android_atomic_and(~CBLK_DISABLED, &mCblk->mFlags);
1356 return status;
1357}
1358
Eric Laurent83b88082014-06-20 18:31:16 -07001359// AudioBufferProvider interface
1360status_t AudioFlinger::PlaybackThread::PatchTrack::getNextBuffer(
Glenn Kastend79072e2016-01-06 08:41:20 -08001361 AudioBufferProvider::Buffer* buffer)
Eric Laurent83b88082014-06-20 18:31:16 -07001362{
1363 ALOG_ASSERT(mPeerProxy != 0, "PatchTrack::getNextBuffer() called without peer proxy");
1364 Proxy::Buffer buf;
1365 buf.mFrameCount = buffer->frameCount;
1366 status_t status = mPeerProxy->obtainBuffer(&buf, &mPeerTimeout);
1367 ALOGV_IF(status != NO_ERROR, "PatchTrack() %p getNextBuffer status %d", this, status);
Eric Laurentc2730ba2014-07-20 15:47:07 -07001368 buffer->frameCount = buf.mFrameCount;
Eric Laurent83b88082014-06-20 18:31:16 -07001369 if (buf.mFrameCount == 0) {
1370 return WOULD_BLOCK;
1371 }
Glenn Kastend79072e2016-01-06 08:41:20 -08001372 status = Track::getNextBuffer(buffer);
Eric Laurent83b88082014-06-20 18:31:16 -07001373 return status;
1374}
1375
1376void AudioFlinger::PlaybackThread::PatchTrack::releaseBuffer(AudioBufferProvider::Buffer* buffer)
1377{
1378 ALOG_ASSERT(mPeerProxy != 0, "PatchTrack::releaseBuffer() called without peer proxy");
1379 Proxy::Buffer buf;
1380 buf.mFrameCount = buffer->frameCount;
1381 buf.mRaw = buffer->raw;
1382 mPeerProxy->releaseBuffer(&buf);
1383 TrackBase::releaseBuffer(buffer);
1384}
1385
1386status_t AudioFlinger::PlaybackThread::PatchTrack::obtainBuffer(Proxy::Buffer* buffer,
1387 const struct timespec *timeOut)
1388{
Eric Laurent4d231dc2016-03-11 18:38:23 -08001389 status_t status = NO_ERROR;
1390 static const int32_t kMaxTries = 5;
1391 int32_t tryCounter = kMaxTries;
1392 do {
1393 if (status == NOT_ENOUGH_DATA) {
1394 restartIfDisabled();
1395 }
1396 status = mProxy->obtainBuffer(buffer, timeOut);
1397 } while ((status == NOT_ENOUGH_DATA) && (tryCounter-- > 0));
1398 return status;
Eric Laurent83b88082014-06-20 18:31:16 -07001399}
1400
1401void AudioFlinger::PlaybackThread::PatchTrack::releaseBuffer(Proxy::Buffer* buffer)
1402{
1403 mProxy->releaseBuffer(buffer);
Eric Laurent4d231dc2016-03-11 18:38:23 -08001404 restartIfDisabled();
1405 android_atomic_or(CBLK_FORCEREADY, &mCblk->mFlags);
1406}
1407
1408void AudioFlinger::PlaybackThread::PatchTrack::restartIfDisabled()
1409{
Eric Laurent83b88082014-06-20 18:31:16 -07001410 if (android_atomic_and(~CBLK_DISABLED, &mCblk->mFlags) & CBLK_DISABLED) {
1411 ALOGW("PatchTrack::releaseBuffer() disabled due to previous underrun, restarting");
1412 start();
1413 }
Eric Laurent83b88082014-06-20 18:31:16 -07001414}
1415
Eric Laurent81784c32012-11-19 14:55:58 -08001416// ----------------------------------------------------------------------------
1417// Record
1418// ----------------------------------------------------------------------------
1419
1420AudioFlinger::RecordHandle::RecordHandle(
1421 const sp<AudioFlinger::RecordThread::RecordTrack>& recordTrack)
1422 : BnAudioRecord(),
1423 mRecordTrack(recordTrack)
1424{
1425}
1426
1427AudioFlinger::RecordHandle::~RecordHandle() {
1428 stop_nonvirtual();
1429 mRecordTrack->destroy();
1430}
1431
Eric Laurent81784c32012-11-19 14:55:58 -08001432status_t AudioFlinger::RecordHandle::start(int /*AudioSystem::sync_event_t*/ event,
Glenn Kastend848eb42016-03-08 13:42:11 -08001433 audio_session_t triggerSession) {
Eric Laurent81784c32012-11-19 14:55:58 -08001434 ALOGV("RecordHandle::start()");
1435 return mRecordTrack->start((AudioSystem::sync_event_t)event, triggerSession);
1436}
1437
1438void AudioFlinger::RecordHandle::stop() {
1439 stop_nonvirtual();
1440}
1441
1442void AudioFlinger::RecordHandle::stop_nonvirtual() {
1443 ALOGV("RecordHandle::stop()");
1444 mRecordTrack->stop();
1445}
1446
1447status_t AudioFlinger::RecordHandle::onTransact(
1448 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
1449{
1450 return BnAudioRecord::onTransact(code, data, reply, flags);
1451}
1452
1453// ----------------------------------------------------------------------------
1454
Glenn Kasten05997e22014-03-13 15:08:33 -07001455// RecordTrack constructor must be called with AudioFlinger::mLock and ThreadBase::mLock held
Eric Laurent81784c32012-11-19 14:55:58 -08001456AudioFlinger::RecordThread::RecordTrack::RecordTrack(
1457 RecordThread *thread,
1458 const sp<Client>& client,
1459 uint32_t sampleRate,
1460 audio_format_t format,
1461 audio_channel_mask_t channelMask,
1462 size_t frameCount,
Eric Laurent83b88082014-06-20 18:31:16 -07001463 void *buffer,
Glenn Kastend848eb42016-03-08 13:42:11 -08001464 audio_session_t sessionId,
Glenn Kastend776ac62014-05-07 09:16:09 -07001465 int uid,
Eric Laurent83b88082014-06-20 18:31:16 -07001466 IAudioFlinger::track_flags_t flags,
1467 track_type type)
Eric Laurent81784c32012-11-19 14:55:58 -08001468 : TrackBase(thread, client, sampleRate, format,
Eric Laurent83b88082014-06-20 18:31:16 -07001469 channelMask, frameCount, buffer, sessionId, uid,
Glenn Kasten755b0a62014-05-13 11:30:28 -07001470 flags, false /*isOut*/,
Eric Laurent83b88082014-06-20 18:31:16 -07001471 (type == TYPE_DEFAULT) ?
1472 ((flags & IAudioFlinger::TRACK_FAST) ? ALLOC_PIPE : ALLOC_CBLK) :
1473 ((buffer == NULL) ? ALLOC_LOCAL : ALLOC_NONE),
1474 type),
Andy Hung97a893e2015-03-29 01:03:07 -07001475 mOverflow(false),
Andy Hung4c6afaf2015-06-12 18:23:35 -07001476 mFramesToDrop(0),
1477 mResamplerBufferProvider(NULL), // initialize in case of early constructor exit
1478 mRecordBufferConverter(NULL)
Eric Laurent81784c32012-11-19 14:55:58 -08001479{
Glenn Kasten3ef14ef2014-03-13 15:08:51 -07001480 if (mCblk == NULL) {
1481 return;
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001482 }
Glenn Kasten6dd62fb2013-12-05 16:35:58 -08001483
Andy Hung97a893e2015-03-29 01:03:07 -07001484 mRecordBufferConverter = new RecordBufferConverter(
1485 thread->mChannelMask, thread->mFormat, thread->mSampleRate,
1486 channelMask, format, sampleRate);
1487 // Check if the RecordBufferConverter construction was successful.
1488 // If not, don't continue with construction.
1489 //
1490 // NOTE: It would be extremely rare that the record track cannot be created
1491 // for the current device, but a pending or future device change would make
1492 // the record track configuration valid.
1493 if (mRecordBufferConverter->initCheck() != NO_ERROR) {
1494 ALOGE("RecordTrack unable to create record buffer converter");
1495 return;
1496 }
1497
Andy Hung6ae58432016-02-16 18:32:24 -08001498 mServerProxy = new AudioRecordServerProxy(mCblk, mBuffer, frameCount,
Andy Hung3f0c9022016-01-15 17:49:46 -08001499 mFrameSize, !isExternalTrack());
Andy Hung3f0c9022016-01-15 17:49:46 -08001500
Andy Hung97a893e2015-03-29 01:03:07 -07001501 mResamplerBufferProvider = new ResamplerBufferProvider(this);
Glenn Kastenc263ca02014-06-04 20:31:46 -07001502
1503 if (flags & IAudioFlinger::TRACK_FAST) {
1504 ALOG_ASSERT(thread->mFastTrackAvail);
1505 thread->mFastTrackAvail = false;
1506 }
Eric Laurent81784c32012-11-19 14:55:58 -08001507}
1508
1509AudioFlinger::RecordThread::RecordTrack::~RecordTrack()
1510{
1511 ALOGV("%s", __func__);
Andy Hung97a893e2015-03-29 01:03:07 -07001512 delete mRecordBufferConverter;
Glenn Kasten6dd62fb2013-12-05 16:35:58 -08001513 delete mResamplerBufferProvider;
Eric Laurent81784c32012-11-19 14:55:58 -08001514}
1515
Andy Hung97a893e2015-03-29 01:03:07 -07001516status_t AudioFlinger::RecordThread::RecordTrack::initCheck() const
1517{
1518 status_t status = TrackBase::initCheck();
1519 if (status == NO_ERROR && mServerProxy == 0) {
1520 status = BAD_VALUE;
1521 }
1522 return status;
1523}
1524
Eric Laurent81784c32012-11-19 14:55:58 -08001525// AudioBufferProvider interface
Glenn Kastend79072e2016-01-06 08:41:20 -08001526status_t AudioFlinger::RecordThread::RecordTrack::getNextBuffer(AudioBufferProvider::Buffer* buffer)
Eric Laurent81784c32012-11-19 14:55:58 -08001527{
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001528 ServerProxy::Buffer buf;
1529 buf.mFrameCount = buffer->frameCount;
1530 status_t status = mServerProxy->obtainBuffer(&buf);
1531 buffer->frameCount = buf.mFrameCount;
1532 buffer->raw = buf.mRaw;
1533 if (buf.mFrameCount == 0) {
1534 // FIXME also wake futex so that overrun is noticed more quickly
Glenn Kasten96f60d82013-07-12 10:21:18 -07001535 (void) android_atomic_or(CBLK_OVERRUN, &mCblk->mFlags);
Eric Laurent81784c32012-11-19 14:55:58 -08001536 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001537 return status;
Eric Laurent81784c32012-11-19 14:55:58 -08001538}
1539
1540status_t AudioFlinger::RecordThread::RecordTrack::start(AudioSystem::sync_event_t event,
Glenn Kastend848eb42016-03-08 13:42:11 -08001541 audio_session_t triggerSession)
Eric Laurent81784c32012-11-19 14:55:58 -08001542{
1543 sp<ThreadBase> thread = mThread.promote();
1544 if (thread != 0) {
1545 RecordThread *recordThread = (RecordThread *)thread.get();
1546 return recordThread->start(this, event, triggerSession);
1547 } else {
1548 return BAD_VALUE;
1549 }
1550}
1551
1552void AudioFlinger::RecordThread::RecordTrack::stop()
1553{
1554 sp<ThreadBase> thread = mThread.promote();
1555 if (thread != 0) {
1556 RecordThread *recordThread = (RecordThread *)thread.get();
Eric Laurent83b88082014-06-20 18:31:16 -07001557 if (recordThread->stop(this) && isExternalTrack()) {
Glenn Kastend848eb42016-03-08 13:42:11 -08001558 AudioSystem::stopInput(mThreadIoHandle, mSessionId);
Eric Laurent81784c32012-11-19 14:55:58 -08001559 }
1560 }
1561}
1562
1563void AudioFlinger::RecordThread::RecordTrack::destroy()
1564{
1565 // see comments at AudioFlinger::PlaybackThread::Track::destroy()
1566 sp<RecordTrack> keep(this);
1567 {
Eric Laurentaaa44472014-09-12 17:41:50 -07001568 if (isExternalTrack()) {
1569 if (mState == ACTIVE || mState == RESUMING) {
Glenn Kastend848eb42016-03-08 13:42:11 -08001570 AudioSystem::stopInput(mThreadIoHandle, mSessionId);
Eric Laurentaaa44472014-09-12 17:41:50 -07001571 }
Glenn Kastend848eb42016-03-08 13:42:11 -08001572 AudioSystem::releaseInput(mThreadIoHandle, mSessionId);
Eric Laurentaaa44472014-09-12 17:41:50 -07001573 }
Eric Laurent81784c32012-11-19 14:55:58 -08001574 sp<ThreadBase> thread = mThread.promote();
1575 if (thread != 0) {
Eric Laurent81784c32012-11-19 14:55:58 -08001576 Mutex::Autolock _l(thread->mLock);
1577 RecordThread *recordThread = (RecordThread *) thread.get();
1578 recordThread->destroyTrack_l(this);
1579 }
1580 }
1581}
1582
Eric Laurent9a54bc22013-09-09 09:08:44 -07001583void AudioFlinger::RecordThread::RecordTrack::invalidate()
1584{
1585 // FIXME should use proxy, and needs work
1586 audio_track_cblk_t* cblk = mCblk;
1587 android_atomic_or(CBLK_INVALID, &cblk->mFlags);
1588 android_atomic_release_store(0x40000000, &cblk->mFutex);
1589 // client is not in server, so FUTEX_WAKE is needed instead of FUTEX_WAKE_PRIVATE
Elliott Hughesee499292014-05-21 17:55:51 -07001590 (void) syscall(__NR_futex, &cblk->mFutex, FUTEX_WAKE, INT_MAX);
Eric Laurent9a54bc22013-09-09 09:08:44 -07001591}
1592
Eric Laurent81784c32012-11-19 14:55:58 -08001593
1594/*static*/ void AudioFlinger::RecordThread::RecordTrack::appendDumpHeader(String8& result)
1595{
Glenn Kasten6e6704c2014-07-03 10:20:00 -07001596 result.append(" Active Client Fmt Chn mask Session S Server fCount SRate\n");
Eric Laurent81784c32012-11-19 14:55:58 -08001597}
1598
Marco Nelissenb2208842014-02-07 14:00:50 -08001599void AudioFlinger::RecordThread::RecordTrack::dump(char* buffer, size_t size, bool active)
Eric Laurent81784c32012-11-19 14:55:58 -08001600{
Glenn Kasten6e6704c2014-07-03 10:20:00 -07001601 snprintf(buffer, size, " %6s %6u %3u %08X %7u %1d %08X %6zu %5u\n",
Marco Nelissenb2208842014-02-07 14:00:50 -08001602 active ? "yes" : "no",
Eric Laurent81784c32012-11-19 14:55:58 -08001603 (mClient == 0) ? getpid_cached : mClient->pid(),
1604 mFormat,
1605 mChannelMask,
1606 mSessionId,
Eric Laurent81784c32012-11-19 14:55:58 -08001607 mState,
Glenn Kastenf20e1d82013-07-12 09:45:18 -07001608 mCblk->mServer,
Glenn Kasten6dd62fb2013-12-05 16:35:58 -08001609 mFrameCount,
Glenn Kasten6e6704c2014-07-03 10:20:00 -07001610 mSampleRate);
Glenn Kasten6dd62fb2013-12-05 16:35:58 -08001611
Eric Laurent81784c32012-11-19 14:55:58 -08001612}
1613
Glenn Kasten25f4aa82014-02-07 10:50:43 -08001614void AudioFlinger::RecordThread::RecordTrack::handleSyncStartEvent(const sp<SyncEvent>& event)
1615{
1616 if (event == mSyncStartEvent) {
1617 ssize_t framesToDrop = 0;
1618 sp<ThreadBase> threadBase = mThread.promote();
1619 if (threadBase != 0) {
1620 // TODO: use actual buffer filling status instead of 2 buffers when info is available
1621 // from audio HAL
1622 framesToDrop = threadBase->mFrameCount * 2;
1623 }
1624 mFramesToDrop = framesToDrop;
1625 }
1626}
1627
1628void AudioFlinger::RecordThread::RecordTrack::clearSyncStartEvent()
1629{
1630 if (mSyncStartEvent != 0) {
1631 mSyncStartEvent->cancel();
1632 mSyncStartEvent.clear();
1633 }
1634 mFramesToDrop = 0;
1635}
1636
Andy Hung3f0c9022016-01-15 17:49:46 -08001637void AudioFlinger::RecordThread::RecordTrack::updateTrackFrameInfo(
1638 int64_t trackFramesReleased, int64_t sourceFramesRead,
1639 uint32_t halSampleRate, const ExtendedTimestamp &timestamp)
1640{
1641 ExtendedTimestamp local = timestamp;
1642
1643 // Convert HAL frames to server-side track frames at track sample rate.
1644 // We use trackFramesReleased and sourceFramesRead as an anchor point.
1645 for (int i = ExtendedTimestamp::LOCATION_SERVER; i < ExtendedTimestamp::LOCATION_MAX; ++i) {
1646 if (local.mTimeNs[i] != 0) {
1647 const int64_t relativeServerFrames = local.mPosition[i] - sourceFramesRead;
1648 const int64_t relativeTrackFrames = relativeServerFrames
1649 * mSampleRate / halSampleRate; // TODO: potential computation overflow
1650 local.mPosition[i] = relativeTrackFrames + trackFramesReleased;
1651 }
1652 }
Andy Hung6ae58432016-02-16 18:32:24 -08001653 mServerProxy->setTimestamp(local);
Andy Hung3f0c9022016-01-15 17:49:46 -08001654}
Eric Laurent83b88082014-06-20 18:31:16 -07001655
1656AudioFlinger::RecordThread::PatchRecord::PatchRecord(RecordThread *recordThread,
1657 uint32_t sampleRate,
1658 audio_channel_mask_t channelMask,
1659 audio_format_t format,
1660 size_t frameCount,
1661 void *buffer,
1662 IAudioFlinger::track_flags_t flags)
1663 : RecordTrack(recordThread, NULL, sampleRate, format, channelMask, frameCount,
Glenn Kastend848eb42016-03-08 13:42:11 -08001664 buffer, AUDIO_SESSION_NONE, getuid(), flags, TYPE_PATCH),
Eric Laurent83b88082014-06-20 18:31:16 -07001665 mProxy(new ClientProxy(mCblk, mBuffer, frameCount, mFrameSize, false, true))
1666{
1667 uint64_t mixBufferNs = ((uint64_t)2 * recordThread->frameCount() * 1000000000) /
1668 recordThread->sampleRate();
1669 mPeerTimeout.tv_sec = mixBufferNs / 1000000000;
1670 mPeerTimeout.tv_nsec = (int) (mixBufferNs % 1000000000);
1671
1672 ALOGV("PatchRecord %p sampleRate %d mPeerTimeout %d.%03d sec",
1673 this, sampleRate,
1674 (int)mPeerTimeout.tv_sec,
1675 (int)(mPeerTimeout.tv_nsec / 1000000));
1676}
1677
1678AudioFlinger::RecordThread::PatchRecord::~PatchRecord()
1679{
1680}
1681
1682// AudioBufferProvider interface
1683status_t AudioFlinger::RecordThread::PatchRecord::getNextBuffer(
Glenn Kastend79072e2016-01-06 08:41:20 -08001684 AudioBufferProvider::Buffer* buffer)
Eric Laurent83b88082014-06-20 18:31:16 -07001685{
1686 ALOG_ASSERT(mPeerProxy != 0, "PatchRecord::getNextBuffer() called without peer proxy");
1687 Proxy::Buffer buf;
1688 buf.mFrameCount = buffer->frameCount;
1689 status_t status = mPeerProxy->obtainBuffer(&buf, &mPeerTimeout);
1690 ALOGV_IF(status != NO_ERROR,
1691 "PatchRecord() %p mPeerProxy->obtainBuffer status %d", this, status);
Eric Laurentc2730ba2014-07-20 15:47:07 -07001692 buffer->frameCount = buf.mFrameCount;
Eric Laurent83b88082014-06-20 18:31:16 -07001693 if (buf.mFrameCount == 0) {
1694 return WOULD_BLOCK;
1695 }
Glenn Kastend79072e2016-01-06 08:41:20 -08001696 status = RecordTrack::getNextBuffer(buffer);
Eric Laurent83b88082014-06-20 18:31:16 -07001697 return status;
1698}
1699
1700void AudioFlinger::RecordThread::PatchRecord::releaseBuffer(AudioBufferProvider::Buffer* buffer)
1701{
1702 ALOG_ASSERT(mPeerProxy != 0, "PatchRecord::releaseBuffer() called without peer proxy");
1703 Proxy::Buffer buf;
1704 buf.mFrameCount = buffer->frameCount;
1705 buf.mRaw = buffer->raw;
1706 mPeerProxy->releaseBuffer(&buf);
1707 TrackBase::releaseBuffer(buffer);
1708}
1709
1710status_t AudioFlinger::RecordThread::PatchRecord::obtainBuffer(Proxy::Buffer* buffer,
1711 const struct timespec *timeOut)
1712{
1713 return mProxy->obtainBuffer(buffer, timeOut);
1714}
1715
1716void AudioFlinger::RecordThread::PatchRecord::releaseBuffer(Proxy::Buffer* buffer)
1717{
1718 mProxy->releaseBuffer(buffer);
1719}
1720
Glenn Kasten63238ef2015-03-02 15:50:29 -08001721} // namespace android