Reset maxPositionOfRemovedSources in DefaultAudioMixer

If the mixer is reset without resetting maxPositionOfRemovedSources and
then reused, the value of maxPositionOfRemovedSources can be outdated,
leading to an incorrect number of bytes being output by the mixer.

PiperOrigin-RevId: 619832502
This commit is contained in:
kimvde 2024-03-28 01:19:19 -07:00 committed by Copybara-Service
parent 828f6d87f9
commit 04a3889998
3 changed files with 27 additions and 0 deletions

View File

@ -44,6 +44,8 @@
* Add support for changing between SDR and HDR input media in a sequence.
* Add support for composition-level audio effects.
* Add support for transcoding Ultra HDR images into HDR videos.
* Fix issue where the `DefaultAudioMixer` doesnt output the correct
amount of bytes after being reset and reused.
* Track Selection:
* `DefaultTrackSelector`: Prefer video tracks with a 'reasonable' frame
rate (>=10fps) over those with a lower or unset frame rate. This ensures

View File

@ -84,6 +84,7 @@ public final class DefaultAudioMixer implements AudioMixer {
// TODO(b/290002438, b/276734854): Improve buffer management & determine best default size.
private static final int DEFAULT_BUFFER_SIZE_MS = 500;
private final boolean outputSilenceWithNoSources;
private final boolean clipFloatOutput;
private final SparseArray<SourceInfo> sources;
private int nextSourceId;
@ -108,6 +109,7 @@ public final class DefaultAudioMixer implements AudioMixer {
private long maxPositionOfRemovedSources;
private DefaultAudioMixer(boolean outputSilenceWithNoSources, boolean clipFloatOutput) {
this.outputSilenceWithNoSources = outputSilenceWithNoSources;
this.clipFloatOutput = clipFloatOutput;
sources = new SparseArray<>();
outputAudioFormat = AudioFormat.NOT_SET;
@ -313,6 +315,7 @@ public final class DefaultAudioMixer implements AudioMixer {
inputLimit = C.LENGTH_UNSET;
outputPosition = 0;
endPosition = Long.MAX_VALUE;
maxPositionOfRemovedSources = outputSilenceWithNoSources ? Long.MAX_VALUE : 0;
}
private void checkStateIsConfigured() {

View File

@ -388,6 +388,28 @@ public final class DefaultAudioMixerTest {
assertThat(mixer.isEnded()).isTrue();
}
@Test
public void reset_afterRemoveSource_whenNotOutputSilenceWithNoSources_resetsSourceEndPosition()
throws Exception {
assumeFalse(outputSilenceWithNoSources);
mixer.configure(AUDIO_FORMAT_STEREO_PCM_FLOAT, /* bufferSizeMs= */ 3, /* startTimeUs= */ 0);
int sourceId = mixer.addSource(AUDIO_FORMAT_STEREO_PCM_FLOAT, /* startTimeUs= */ 0);
ByteBuffer sourceBuffer = createByteBuffer(new float[] {0.1f, 0.2f, 0.3f, 0.4f, 0.5f, 0.6f});
mixer.queueInput(sourceId, sourceBuffer);
assertThat(sourceBuffer.remaining()).isEqualTo(0);
mixer.removeSource(sourceId);
mixer.reset();
mixer.configure(AUDIO_FORMAT_STEREO_PCM_FLOAT, /* bufferSizeMs= */ 3, /* startTimeUs= */ 0);
sourceId = mixer.addSource(AUDIO_FORMAT_STEREO_PCM_FLOAT, /* startTimeUs= */ 0);
sourceBuffer = createByteBuffer(new float[] {0.1f, 0.2f});
mixer.queueInput(sourceId, sourceBuffer);
assertThat(sourceBuffer.remaining()).isEqualTo(0);
mixer.removeSource(sourceId);
assertThat(createFloatArray(mixer.getOutput())).isEqualTo(new float[] {0.1f, 0.2f});
}
@Test
public void input_whileIsEnded_isNotConsumed() throws Exception {
mixer.configure(