Skip to content

Commit be02f32

Browse files
committed
TimedAudioTrack: Optimize the queue trim operation.
DO NOT MERGE this change must be hand-merged into master due to directory restructuring. Don't perform the end PTS calculation for each buffer during trimming. Instead, only calculate the ending PTS of a buffer if there is no next buffer in the queue. This optimization assumes that the buffers being queued are in monotonic media time order (a fair assumption for now) and that the timestamps in the audio are contiguous (not a requirement for this API, but a reality of how it is being used right now). In the case where the audio is discontinuous on purpose, it is that this optimization will cause the system hold one extra buffer which it could have safely trimmed. It should not be much of an issue since in real life the audio is almost always contiguous, and as long as the media clock is running and the mixer is mixing, the buffer will be used up and discard as part of the normal flow anyway. Change-Id: I00061e85ee7d5651fcf80751646c7d7415894a14 Signed-off-by: John Grossman <johngro@google.com>
1 parent f609b66 commit be02f32

File tree

1 file changed

+29
-11
lines changed

1 file changed

+29
-11
lines changed

services/audioflinger/AudioFlinger.cpp

Lines changed: 29 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3936,20 +3936,38 @@ void AudioFlinger::PlaybackThread::TimedTrack::trimTimedBufferQueue_l() {
39363936

39373937
size_t trimEnd;
39383938
for (trimEnd = 0; trimEnd < mTimedBufferQueue.size(); trimEnd++) {
3939-
int64_t frameCount = mTimedBufferQueue[trimEnd].buffer()->size()
3940-
/ mCblk->frameSize;
39413939
int64_t bufEnd;
39423940

3943-
if (!mMediaTimeToSampleTransform.doReverseTransform(frameCount,
3944-
&bufEnd)) {
3945-
LOGE("Failed to convert frame count of %lld to media time duration"
3946-
" (scale factor %d/%u) in %s", frameCount,
3947-
mMediaTimeToSampleTransform.a_to_b_numer,
3948-
mMediaTimeToSampleTransform.a_to_b_denom,
3949-
__PRETTY_FUNCTION__);
3950-
break;
3941+
if ((trimEnd + 1) < mTimedBufferQueue.size()) {
3942+
// We have a next buffer. Just use its PTS as the PTS of the frame
3943+
// following the last frame in this buffer. If the stream is sparse
3944+
// (ie, there are deliberate gaps left in the stream which should be
3945+
// filled with silence by the TimedAudioTrack), then this can result
3946+
// in one extra buffer being left un-trimmed when it could have
3947+
// been. In general, this is not typical, and we would rather
3948+
// optimized away the TS calculation below for the more common case
3949+
// where PTSes are contiguous.
3950+
bufEnd = mTimedBufferQueue[trimEnd + 1].pts();
3951+
} else {
3952+
// We have no next buffer. Compute the PTS of the frame following
3953+
// the last frame in this buffer by computing the duration of of
3954+
// this frame in media time units and adding it to the PTS of the
3955+
// buffer.
3956+
int64_t frameCount = mTimedBufferQueue[trimEnd].buffer()->size()
3957+
/ mCblk->frameSize;
3958+
3959+
if (!mMediaTimeToSampleTransform.doReverseTransform(frameCount,
3960+
&bufEnd)) {
3961+
LOGE("Failed to convert frame count of %lld to media time"
3962+
" duration" " (scale factor %d/%u) in %s",
3963+
frameCount,
3964+
mMediaTimeToSampleTransform.a_to_b_numer,
3965+
mMediaTimeToSampleTransform.a_to_b_denom,
3966+
__PRETTY_FUNCTION__);
3967+
break;
3968+
}
3969+
bufEnd += mTimedBufferQueue[trimEnd].pts();
39513970
}
3952-
bufEnd += mTimedBufferQueue[trimEnd].pts();
39533971

39543972
if (bufEnd > mediaTimeNow)
39553973
break;

0 commit comments

Comments
 (0)