Commit f0b34b8f by samrobinson Committed by Ian Baker

Rollback of https://github.com/androidx/media/commit/8a4168271db6402964fa7e969d2adfc0be493fb3

*** Original commit ***

Keep AudioTrack on flush as default

***

PiperOrigin-RevId: 444327724
parent b816a3fe
......@@ -457,6 +457,18 @@ public interface AudioSink {
*/
void flush();
/**
* Flushes the sink, after which it is ready to receive buffers from a new playback position.
*
* <p>Does not release the {@link AudioTrack} held by the sink.
*
* <p>This method is experimental, and will be renamed or removed in a future release.
*
* <p>Only for experimental use as part of {@link
* MediaCodecAudioRenderer#experimentalSetEnableKeepAudioTrackOnSeek(boolean)}.
*/
void experimentalFlushWithoutAudioTrackRelease();
/** Resets the sink, releasing any resources that it currently holds. */
void reset();
}
......@@ -129,6 +129,7 @@ public abstract class DecoderAudioRenderer<
private int encoderDelay;
private int encoderPadding;
private boolean experimentalKeepAudioTrackOnSeek;
private boolean firstStreamSampleRead;
@Nullable private T decoder;
......@@ -208,6 +209,19 @@ public abstract class DecoderAudioRenderer<
audioSinkNeedsConfigure = true;
}
/**
* Sets whether to enable the experimental feature that keeps and flushes the {@link
* android.media.AudioTrack} when a seek occurs, as opposed to releasing and reinitialising. Off
* by default.
*
* <p>This method is experimental, and will be renamed or removed in a future release.
*
* @param enableKeepAudioTrackOnSeek Whether to keep the {@link android.media.AudioTrack} on seek.
*/
public void experimentalSetEnableKeepAudioTrackOnSeek(boolean enableKeepAudioTrackOnSeek) {
this.experimentalKeepAudioTrackOnSeek = enableKeepAudioTrackOnSeek;
}
@Override
@Nullable
public MediaClock getMediaClock() {
......@@ -541,7 +555,12 @@ public abstract class DecoderAudioRenderer<
@Override
protected void onPositionReset(long positionUs, boolean joining) throws ExoPlaybackException {
if (experimentalKeepAudioTrackOnSeek) {
audioSink.experimentalFlushWithoutAudioTrackRelease();
} else {
audioSink.flush();
}
currentPositionUs = positionUs;
allowFirstBufferPositionDiscontinuity = true;
allowPositionDiscontinuity = true;
......
......@@ -907,7 +907,7 @@ public final class DefaultAudioSink implements AudioSink {
// We're waiting for playout on the current audio track to finish.
return false;
}
flushAndReleaseAudioTrack();
flush();
} else {
// The current audio track can be reused for the new configuration.
configuration = pendingConfiguration;
......@@ -1034,7 +1034,7 @@ public final class DefaultAudioSink implements AudioSink {
if (audioTrackPositionTracker.isStalled(getWrittenFrames())) {
Log.w(TAG, "Resetting stalled audio track");
flushAndReleaseAudioTrack();
flush();
return true;
}
......@@ -1324,8 +1324,7 @@ public final class DefaultAudioSink implements AudioSink {
// The audio attributes are ignored in tunneling mode, so no need to reset.
return;
}
// audioAttributes change requires the audioTrack to be recreated.
flushAndReleaseAudioTrack();
flush();
}
@Override
......@@ -1338,8 +1337,7 @@ public final class DefaultAudioSink implements AudioSink {
if (this.audioSessionId != audioSessionId) {
this.audioSessionId = audioSessionId;
externalAudioSessionIdProvided = audioSessionId != C.AUDIO_SESSION_ID_UNSET;
// audioSessionId change requires the audioTrack to be recreated.
flushAndReleaseAudioTrack();
flush();
}
}
......@@ -1367,7 +1365,7 @@ public final class DefaultAudioSink implements AudioSink {
Assertions.checkState(externalAudioSessionIdProvided);
if (!tunneling) {
tunneling = true;
flushAndReleaseAudioTrack();
flush();
}
}
......@@ -1375,7 +1373,7 @@ public final class DefaultAudioSink implements AudioSink {
public void disableTunneling() {
if (tunneling) {
tunneling = false;
flushAndReleaseAudioTrack();
flush();
}
}
......@@ -1407,60 +1405,9 @@ public final class DefaultAudioSink implements AudioSink {
@Override
public void flush() {
if (!isAudioTrackInitialized()) {
return;
}
// Prior to SDK 25, AudioTrack flush does not work as intended, so it must be released and
// reinitialized. (Internal reference: b/143500232)
if (Util.SDK_INT < 25) {
flushAndReleaseAudioTrack();
return;
}
writeExceptionPendingExceptionHolder.clear();
initializationExceptionPendingExceptionHolder.clear();
if (isAudioTrackInitialized()) {
resetSinkStateForFlush();
if (audioTrackPositionTracker.isPlaying()) {
audioTrack.pause();
}
audioTrack.flush();
audioTrackPositionTracker.reset();
audioTrackPositionTracker.setAudioTrack(
audioTrack,
/* isPassthrough= */ configuration.outputMode == OUTPUT_MODE_PASSTHROUGH,
configuration.outputEncoding,
configuration.outputPcmFrameSize,
configuration.bufferSize);
startMediaTimeUsNeedsInit = true;
}
@Override
public void reset() {
flushAndReleaseAudioTrack();
for (AudioProcessor audioProcessor : toIntPcmAvailableAudioProcessors) {
audioProcessor.reset();
}
for (AudioProcessor audioProcessor : toFloatPcmAvailableAudioProcessors) {
audioProcessor.reset();
}
playing = false;
offloadDisabledUntilNextConfiguration = false;
}
// Internal methods.
private void flushAndReleaseAudioTrack() {
if (!isAudioTrackInitialized()) {
return;
}
writeExceptionPendingExceptionHolder.clear();
initializationExceptionPendingExceptionHolder.clear();
resetSinkStateForFlush();
if (audioTrackPositionTracker.isPlaying()) {
audioTrack.pause();
}
......@@ -1496,6 +1443,57 @@ public final class DefaultAudioSink implements AudioSink {
}
}.start();
}
writeExceptionPendingExceptionHolder.clear();
initializationExceptionPendingExceptionHolder.clear();
}
@Override
public void experimentalFlushWithoutAudioTrackRelease() {
// Prior to SDK 25, AudioTrack flush does not work as intended, and therefore it must be
// released and reinitialized. (Internal reference: b/143500232)
if (Util.SDK_INT < 25) {
flush();
return;
}
writeExceptionPendingExceptionHolder.clear();
initializationExceptionPendingExceptionHolder.clear();
if (!isAudioTrackInitialized()) {
return;
}
resetSinkStateForFlush();
if (audioTrackPositionTracker.isPlaying()) {
audioTrack.pause();
}
audioTrack.flush();
audioTrackPositionTracker.reset();
audioTrackPositionTracker.setAudioTrack(
audioTrack,
/* isPassthrough= */ configuration.outputMode == OUTPUT_MODE_PASSTHROUGH,
configuration.outputEncoding,
configuration.outputPcmFrameSize,
configuration.bufferSize);
startMediaTimeUsNeedsInit = true;
}
@Override
public void reset() {
flush();
for (AudioProcessor audioProcessor : toIntPcmAvailableAudioProcessors) {
audioProcessor.reset();
}
for (AudioProcessor audioProcessor : toFloatPcmAvailableAudioProcessors) {
audioProcessor.reset();
}
playing = false;
offloadDisabledUntilNextConfiguration = false;
}
// Internal methods.
private void resetSinkStateForFlush() {
submittedPcmBytes = 0;
......
......@@ -164,6 +164,11 @@ public class ForwardingAudioSink implements AudioSink {
}
@Override
public void experimentalFlushWithoutAudioTrackRelease() {
sink.experimentalFlushWithoutAudioTrackRelease();
}
@Override
public void reset() {
sink.reset();
}
......
......@@ -109,6 +109,8 @@ public class MediaCodecAudioRenderer extends MediaCodecRenderer implements Media
private boolean allowPositionDiscontinuity;
private boolean audioSinkNeedsConfigure;
private boolean experimentalKeepAudioTrackOnSeek;
@Nullable private WakeupListener wakeupListener;
/**
......@@ -264,6 +266,19 @@ public class MediaCodecAudioRenderer extends MediaCodecRenderer implements Media
return TAG;
}
/**
* Sets whether to enable the experimental feature that keeps and flushes the {@link
* android.media.AudioTrack} when a seek occurs, as opposed to releasing and reinitialising. Off
* by default.
*
* <p>This method is experimental, and will be renamed or removed in a future release.
*
* @param enableKeepAudioTrackOnSeek Whether to keep the {@link android.media.AudioTrack} on seek.
*/
public void experimentalSetEnableKeepAudioTrackOnSeek(boolean enableKeepAudioTrackOnSeek) {
this.experimentalKeepAudioTrackOnSeek = enableKeepAudioTrackOnSeek;
}
@Override
protected @Capabilities int supportsFormat(MediaCodecSelector mediaCodecSelector, Format format)
throws DecoderQueryException {
......@@ -516,7 +531,11 @@ public class MediaCodecAudioRenderer extends MediaCodecRenderer implements Media
@Override
protected void onPositionReset(long positionUs, boolean joining) throws ExoPlaybackException {
super.onPositionReset(positionUs, joining);
if (experimentalKeepAudioTrackOnSeek) {
audioSink.experimentalFlushWithoutAudioTrackRelease();
} else {
audioSink.flush();
}
currentPositionUs = positionUs;
allowFirstBufferPositionDiscontinuity = true;
......
......@@ -265,15 +265,15 @@ public final class DefaultAudioSinkTest {
}
@Test
public void handleBuffer_afterFlush_doesntThrow() throws Exception {
// This is demonstrating that no Exceptions are thrown as a result of handling a buffer after a
// flush.
public void handlesBufferAfterExperimentalFlush() throws Exception {
// This is demonstrating that no Exceptions are thrown as a result of handling a buffer after an
// experimental flush.
configureDefaultAudioSink(CHANNEL_COUNT_STEREO);
defaultAudioSink.handleBuffer(
createDefaultSilenceBuffer(), /* presentationTimeUs= */ 0, /* encodedAccessUnitCount= */ 1);
// After the flush we can successfully queue more input.
defaultAudioSink.flush();
// After the experimental flush we can successfully queue more input.
defaultAudioSink.experimentalFlushWithoutAudioTrackRelease();
defaultAudioSink.handleBuffer(
createDefaultSilenceBuffer(),
/* presentationTimeUs= */ 5_000,
......@@ -281,13 +281,13 @@ public final class DefaultAudioSinkTest {
}
@Test
public void getCurrentPosition_afterFlush_returnsUnset() throws Exception {
public void getCurrentPosition_returnsUnset_afterExperimentalFlush() throws Exception {
configureDefaultAudioSink(CHANNEL_COUNT_STEREO);
defaultAudioSink.handleBuffer(
createDefaultSilenceBuffer(),
/* presentationTimeUs= */ 5 * C.MICROS_PER_SECOND,
/* encodedAccessUnitCount= */ 1);
defaultAudioSink.flush();
defaultAudioSink.experimentalFlushWithoutAudioTrackRelease();
assertThat(defaultAudioSink.getCurrentPositionUs(/* sourceEnded= */ false))
.isEqualTo(CURRENT_POSITION_NOT_SET);
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment