Commit cf393983 by samrobinson Committed by Ian Baker

Adjust `FakeSampleStream#readData` logic.

Once EOS has been read, that will be returned every time readData is called.
EOS needs to be an item in the items.

PiperOrigin-RevId: 290715513
parent 6923316c
...@@ -146,7 +146,7 @@ public class MetadataRendererTest { ...@@ -146,7 +146,7 @@ public class MetadataRendererTest {
new FakeSampleStream( new FakeSampleStream(
EMSG_FORMAT, EMSG_FORMAT,
/* eventDispatcher= */ null, /* eventDispatcher= */ null,
Arrays.asList(new FakeSampleStreamItem(input)), Arrays.asList(new FakeSampleStreamItem(input), FakeSampleStreamItem.END_OF_STREAM_ITEM),
0), 0),
/* offsetUs= */ 0L); /* offsetUs= */ 0L);
renderer.render(/* positionUs= */ 0, /* elapsedRealtimeUs= */ 0); // Read the format renderer.render(/* positionUs= */ 0, /* elapsedRealtimeUs= */ 0); // Read the format
......
...@@ -28,7 +28,6 @@ import com.google.android.exoplayer2.source.MediaSourceEventListener.EventDispat ...@@ -28,7 +28,6 @@ import com.google.android.exoplayer2.source.MediaSourceEventListener.EventDispat
import com.google.android.exoplayer2.source.SampleStream; import com.google.android.exoplayer2.source.SampleStream;
import com.google.android.exoplayer2.source.TrackGroup; import com.google.android.exoplayer2.source.TrackGroup;
import com.google.android.exoplayer2.source.TrackGroupArray; import com.google.android.exoplayer2.source.TrackGroupArray;
import com.google.android.exoplayer2.testutil.FakeSampleStream.FakeSampleStreamItem;
import com.google.android.exoplayer2.trackselection.TrackSelection; import com.google.android.exoplayer2.trackselection.TrackSelection;
import com.google.android.exoplayer2.upstream.DataSpec; import com.google.android.exoplayer2.upstream.DataSpec;
import com.google.android.exoplayer2.util.Util; import com.google.android.exoplayer2.util.Util;
...@@ -244,7 +243,10 @@ public class FakeMediaPeriod implements MediaPeriod { ...@@ -244,7 +243,10 @@ public class FakeMediaPeriod implements MediaPeriod {
protected SampleStream createSampleStream( protected SampleStream createSampleStream(
TrackSelection selection, EventDispatcher eventDispatcher) { TrackSelection selection, EventDispatcher eventDispatcher) {
return new FakeSampleStream( return new FakeSampleStream(
selection.getSelectedFormat(), eventDispatcher, /* shouldOutputSample= */ true); selection.getSelectedFormat(),
eventDispatcher,
FakeSampleStream.SINGLE_SAMPLE_THEN_END_OF_STREAM,
/* timeUsIncrement= */ 0);
} }
/** /**
...@@ -259,7 +261,7 @@ public class FakeMediaPeriod implements MediaPeriod { ...@@ -259,7 +261,7 @@ public class FakeMediaPeriod implements MediaPeriod {
// When seeking back to 0, queue our single sample at time 0 again. // When seeking back to 0, queue our single sample at time 0 again.
((FakeSampleStream) sampleStream) ((FakeSampleStream) sampleStream)
.resetSampleStreamItems( .resetSampleStreamItems(
Collections.singletonList(new FakeSampleStreamItem(new byte[] {0})), /* timeUs= */ 0); FakeSampleStream.SINGLE_SAMPLE_THEN_END_OF_STREAM, /* timeUs= */ 0);
} }
} }
......
...@@ -24,6 +24,7 @@ import com.google.android.exoplayer2.source.MediaSourceEventListener.EventDispat ...@@ -24,6 +24,7 @@ import com.google.android.exoplayer2.source.MediaSourceEventListener.EventDispat
import com.google.android.exoplayer2.source.SampleStream; import com.google.android.exoplayer2.source.SampleStream;
import java.io.IOException; import java.io.IOException;
import java.util.ArrayDeque; import java.util.ArrayDeque;
import java.util.Arrays;
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
...@@ -40,6 +41,14 @@ public final class FakeSampleStream implements SampleStream { ...@@ -40,6 +41,14 @@ public final class FakeSampleStream implements SampleStream {
int flags; int flags;
/** /**
* Item that designates the end of stream has been reached.
*
* <p>When this item is read, readData will repeatedly return end of stream.
*/
public static final FakeSampleStreamItem END_OF_STREAM_ITEM =
new FakeSampleStreamItem(new byte[] {}, C.BUFFER_FLAG_END_OF_STREAM);
/**
* Item that, when {@link #readData(FormatHolder, DecoderInputBuffer, boolean)} is called, will * Item that, when {@link #readData(FormatHolder, DecoderInputBuffer, boolean)} is called, will
* return {@link C#RESULT_FORMAT_READ} with the new format. * return {@link C#RESULT_FORMAT_READ} with the new format.
* *
...@@ -72,6 +81,11 @@ public final class FakeSampleStream implements SampleStream { ...@@ -72,6 +81,11 @@ public final class FakeSampleStream implements SampleStream {
} }
} }
/** List for use when a single sample is to be output, followed by the end of stream. */
public static final List<FakeSampleStreamItem> SINGLE_SAMPLE_THEN_END_OF_STREAM =
Arrays.asList(
new FakeSampleStreamItem(new byte[] {0}), FakeSampleStreamItem.END_OF_STREAM_ITEM);
private final ArrayDeque<FakeSampleStreamItem> fakeSampleStreamItems; private final ArrayDeque<FakeSampleStreamItem> fakeSampleStreamItems;
private final int timeUsIncrement; private final int timeUsIncrement;
...@@ -80,6 +94,7 @@ public final class FakeSampleStream implements SampleStream { ...@@ -80,6 +94,7 @@ public final class FakeSampleStream implements SampleStream {
private Format format; private Format format;
private int timeUs; private int timeUs;
private boolean readFormat; private boolean readFormat;
private boolean readEOSBuffer;
/** /**
* Creates fake sample stream which outputs the given {@link Format}, optionally one sample with * Creates fake sample stream which outputs the given {@link Format}, optionally one sample with
...@@ -95,8 +110,8 @@ public final class FakeSampleStream implements SampleStream { ...@@ -95,8 +110,8 @@ public final class FakeSampleStream implements SampleStream {
format, format,
eventDispatcher, eventDispatcher,
shouldOutputSample shouldOutputSample
? Collections.singletonList(new FakeSampleStreamItem(new byte[] {0})) ? SINGLE_SAMPLE_THEN_END_OF_STREAM
: Collections.emptyList(), : Collections.singletonList(FakeSampleStreamItem.END_OF_STREAM_ITEM),
/* timeUsIncrement= */ 0); /* timeUsIncrement= */ 0);
} }
...@@ -107,7 +122,8 @@ public final class FakeSampleStream implements SampleStream { ...@@ -107,7 +122,8 @@ public final class FakeSampleStream implements SampleStream {
* @param format The {@link Format} to output. * @param format The {@link Format} to output.
* @param eventDispatcher An {@link EventDispatcher} to notify of read events. * @param eventDispatcher An {@link EventDispatcher} to notify of read events.
* @param fakeSampleStreamItems The list of {@link FakeSampleStreamItem items} to customize the * @param fakeSampleStreamItems The list of {@link FakeSampleStreamItem items} to customize the
* return values of {@link #readData(FormatHolder, DecoderInputBuffer, boolean)}. * return values of {@link #readData(FormatHolder, DecoderInputBuffer, boolean)}. Note that
* once an EOS buffer has been read, that will return every time readData is called.
* @param timeUsIncrement The time each sample should increase by, in microseconds. * @param timeUsIncrement The time each sample should increase by, in microseconds.
*/ */
public FakeSampleStream( public FakeSampleStream(
...@@ -131,6 +147,7 @@ public final class FakeSampleStream implements SampleStream { ...@@ -131,6 +147,7 @@ public final class FakeSampleStream implements SampleStream {
this.fakeSampleStreamItems.clear(); this.fakeSampleStreamItems.clear();
this.fakeSampleStreamItems.addAll(fakeSampleStreamItems); this.fakeSampleStreamItems.addAll(fakeSampleStreamItems);
this.timeUs = timeUs; this.timeUs = timeUs;
readEOSBuffer = false;
} }
/** /**
...@@ -156,6 +173,11 @@ public final class FakeSampleStream implements SampleStream { ...@@ -156,6 +173,11 @@ public final class FakeSampleStream implements SampleStream {
notifyEventDispatcher(formatHolder); notifyEventDispatcher(formatHolder);
return C.RESULT_FORMAT_READ; return C.RESULT_FORMAT_READ;
} }
// Once an EOS buffer has been read, send EOS every time.
if (readEOSBuffer) {
buffer.setFlags(C.BUFFER_FLAG_END_OF_STREAM);
return C.RESULT_BUFFER_READ;
}
if (!fakeSampleStreamItems.isEmpty()) { if (!fakeSampleStreamItems.isEmpty()) {
FakeSampleStreamItem fakeSampleStreamItem = fakeSampleStreamItems.remove(); FakeSampleStreamItem fakeSampleStreamItem = fakeSampleStreamItems.remove();
if (fakeSampleStreamItem.format != null) { if (fakeSampleStreamItem.format != null) {
...@@ -172,12 +194,12 @@ public final class FakeSampleStream implements SampleStream { ...@@ -172,12 +194,12 @@ public final class FakeSampleStream implements SampleStream {
buffer.data.put(sampleData); buffer.data.put(sampleData);
if (fakeSampleStreamItem.flags != 0) { if (fakeSampleStreamItem.flags != 0) {
buffer.setFlags(fakeSampleStreamItem.flags); buffer.setFlags(fakeSampleStreamItem.flags);
readEOSBuffer = buffer.isEndOfStream();
} }
return C.RESULT_BUFFER_READ; return C.RESULT_BUFFER_READ;
} }
} }
buffer.setFlags(C.BUFFER_FLAG_END_OF_STREAM); return C.RESULT_NOTHING_READ;
return C.RESULT_BUFFER_READ;
} }
@Override @Override
......
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