Apply passthrough workarounds only on platform API versions 21/22.

This commit is contained in:
Oliver Woodman 2015-05-08 17:02:23 +01:00
parent 02ea6f8937
commit de5bce3400

View File

@ -423,9 +423,11 @@ public final class AudioTrack {
return RESULT_BUFFER_CONSUMED; return RESULT_BUFFER_CONSUMED;
} }
// As a workaround for an issue where an an AC-3 audio track continues to play data written // As a workaround for an issue on platform API versions 21/22 where an an AC-3 audio track
// while it is paused, stop writing so its buffer empties. See [Internal: b/18899620]. // continues to play data written while it is paused, stop writing so its buffer empties. See
if (isAc3 && audioTrack.getPlayState() == android.media.AudioTrack.PLAYSTATE_PAUSED) { // [Internal: b/18899620].
if (Util.SDK_INT <= 22 && isAc3
&& audioTrack.getPlayState() == android.media.AudioTrack.PLAYSTATE_PAUSED) {
return 0; return 0;
} }
@ -739,7 +741,7 @@ public final class AudioTrack {
private static class AudioTrackUtil { private static class AudioTrackUtil {
protected android.media.AudioTrack audioTrack; protected android.media.AudioTrack audioTrack;
private boolean enablePassthroughWorkaround; private boolean isPassthrough;
private int sampleRate; private int sampleRate;
private long lastRawPlaybackHeadPosition; private long lastRawPlaybackHeadPosition;
private long rawPlaybackHeadWrapCount; private long rawPlaybackHeadWrapCount;
@ -749,14 +751,11 @@ public final class AudioTrack {
* Reconfigures the audio track utility helper to use the specified {@code audioTrack}. * Reconfigures the audio track utility helper to use the specified {@code audioTrack}.
* *
* @param audioTrack The audio track to wrap. * @param audioTrack The audio track to wrap.
* @param enablePassthroughWorkaround Whether to work around an issue where the playback head * @param isPassthrough Whether the audio track is used for passthrough (e.g. AC-3) playback.
* position jumps back to zero on a paused passthrough/direct audio track. See
* [Internal: b/19187573].
*/ */
public void reconfigure(android.media.AudioTrack audioTrack, public void reconfigure(android.media.AudioTrack audioTrack, boolean isPassthrough) {
boolean enablePassthroughWorkaround) {
this.audioTrack = audioTrack; this.audioTrack = audioTrack;
this.enablePassthroughWorkaround = enablePassthroughWorkaround; this.isPassthrough = isPassthrough;
lastRawPlaybackHeadPosition = 0; lastRawPlaybackHeadPosition = 0;
rawPlaybackHeadWrapCount = 0; rawPlaybackHeadWrapCount = 0;
passthroughWorkaroundPauseOffset = 0; passthroughWorkaroundPauseOffset = 0;
@ -767,14 +766,14 @@ public final class AudioTrack {
/** /**
* Returns whether the audio track should behave as though it has pending data. This is to work * Returns whether the audio track should behave as though it has pending data. This is to work
* around an issue where AC-3 audio tracks can't be paused, so we empty their buffers when * around an issue on platform API versions 21/22 where AC-3 audio tracks can't be paused, so we
* paused. In this case, they should still behave as if they have pending data, otherwise * empty their buffers when paused. In this case, they should still behave as if they have
* writing will never resume. * pending data, otherwise writing will never resume.
* *
* @see #handleBuffer * @see #handleBuffer
*/ */
public boolean overrideHasPendingData() { public boolean overrideHasPendingData() {
return enablePassthroughWorkaround return Util.SDK_INT <= 22 && isPassthrough
&& audioTrack.getPlayState() == android.media.AudioTrack.PLAYSTATE_PAUSED && audioTrack.getPlayState() == android.media.AudioTrack.PLAYSTATE_PAUSED
&& audioTrack.getPlaybackHeadPosition() == 0; && audioTrack.getPlaybackHeadPosition() == 0;
} }
@ -790,7 +789,9 @@ public final class AudioTrack {
*/ */
public long getPlaybackHeadPosition() { public long getPlaybackHeadPosition() {
long rawPlaybackHeadPosition = 0xFFFFFFFFL & audioTrack.getPlaybackHeadPosition(); long rawPlaybackHeadPosition = 0xFFFFFFFFL & audioTrack.getPlaybackHeadPosition();
if (enablePassthroughWorkaround) { if (Util.SDK_INT <= 22 && isPassthrough) {
// Work around an issue on platform API versions 21/22 where the playback head position
// jumps back to zero on paused passthrough/direct audio tracks. See [Internal: b/19187573].
if (audioTrack.getPlayState() == android.media.AudioTrack.PLAYSTATE_PAUSED if (audioTrack.getPlayState() == android.media.AudioTrack.PLAYSTATE_PAUSED
&& rawPlaybackHeadPosition == 0) { && rawPlaybackHeadPosition == 0) {
passthroughWorkaroundPauseOffset = lastRawPlaybackHeadPosition; passthroughWorkaroundPauseOffset = lastRawPlaybackHeadPosition;
@ -868,9 +869,8 @@ public final class AudioTrack {
} }
@Override @Override
public void reconfigure(android.media.AudioTrack audioTrack, public void reconfigure(android.media.AudioTrack audioTrack, boolean isPassthrough) {
boolean enablePassthroughWorkaround) { super.reconfigure(audioTrack, isPassthrough);
super.reconfigure(audioTrack, enablePassthroughWorkaround);
rawTimestampFramePositionWrapCount = 0; rawTimestampFramePositionWrapCount = 0;
lastRawTimestampFramePosition = 0; lastRawTimestampFramePosition = 0;
lastTimestampFramePosition = 0; lastTimestampFramePosition = 0;