mirror of
https://github.com/samsonjs/media.git
synced 2026-04-11 12:15:47 +00:00
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
This commit is contained in:
parent
6923316cfa
commit
cf3939838b
3 changed files with 33 additions and 9 deletions
|
|
@ -146,7 +146,7 @@ public class MetadataRendererTest {
|
|||
new FakeSampleStream(
|
||||
EMSG_FORMAT,
|
||||
/* eventDispatcher= */ null,
|
||||
Arrays.asList(new FakeSampleStreamItem(input)),
|
||||
Arrays.asList(new FakeSampleStreamItem(input), FakeSampleStreamItem.END_OF_STREAM_ITEM),
|
||||
0),
|
||||
/* offsetUs= */ 0L);
|
||||
renderer.render(/* positionUs= */ 0, /* elapsedRealtimeUs= */ 0); // Read the format
|
||||
|
|
|
|||
|
|
@ -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.TrackGroup;
|
||||
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.upstream.DataSpec;
|
||||
import com.google.android.exoplayer2.util.Util;
|
||||
|
|
@ -244,7 +243,10 @@ public class FakeMediaPeriod implements MediaPeriod {
|
|||
protected SampleStream createSampleStream(
|
||||
TrackSelection selection, EventDispatcher eventDispatcher) {
|
||||
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 {
|
|||
// When seeking back to 0, queue our single sample at time 0 again.
|
||||
((FakeSampleStream) sampleStream)
|
||||
.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
|
|||
import com.google.android.exoplayer2.source.SampleStream;
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayDeque;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
|
|
@ -39,6 +40,14 @@ public final class FakeSampleStream implements SampleStream {
|
|||
@Nullable byte[] sampleData;
|
||||
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
|
||||
* return {@link C#RESULT_FORMAT_READ} with the new format.
|
||||
|
|
@ -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 int timeUsIncrement;
|
||||
|
||||
|
|
@ -80,6 +94,7 @@ public final class FakeSampleStream implements SampleStream {
|
|||
private Format format;
|
||||
private int timeUs;
|
||||
private boolean readFormat;
|
||||
private boolean readEOSBuffer;
|
||||
|
||||
/**
|
||||
* Creates fake sample stream which outputs the given {@link Format}, optionally one sample with
|
||||
|
|
@ -95,8 +110,8 @@ public final class FakeSampleStream implements SampleStream {
|
|||
format,
|
||||
eventDispatcher,
|
||||
shouldOutputSample
|
||||
? Collections.singletonList(new FakeSampleStreamItem(new byte[] {0}))
|
||||
: Collections.emptyList(),
|
||||
? SINGLE_SAMPLE_THEN_END_OF_STREAM
|
||||
: Collections.singletonList(FakeSampleStreamItem.END_OF_STREAM_ITEM),
|
||||
/* timeUsIncrement= */ 0);
|
||||
}
|
||||
|
||||
|
|
@ -107,7 +122,8 @@ public final class FakeSampleStream implements SampleStream {
|
|||
* @param format The {@link Format} to output.
|
||||
* @param eventDispatcher An {@link EventDispatcher} to notify of read events.
|
||||
* @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.
|
||||
*/
|
||||
public FakeSampleStream(
|
||||
|
|
@ -131,6 +147,7 @@ public final class FakeSampleStream implements SampleStream {
|
|||
this.fakeSampleStreamItems.clear();
|
||||
this.fakeSampleStreamItems.addAll(fakeSampleStreamItems);
|
||||
this.timeUs = timeUs;
|
||||
readEOSBuffer = false;
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -156,6 +173,11 @@ public final class FakeSampleStream implements SampleStream {
|
|||
notifyEventDispatcher(formatHolder);
|
||||
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()) {
|
||||
FakeSampleStreamItem fakeSampleStreamItem = fakeSampleStreamItems.remove();
|
||||
if (fakeSampleStreamItem.format != null) {
|
||||
|
|
@ -172,12 +194,12 @@ public final class FakeSampleStream implements SampleStream {
|
|||
buffer.data.put(sampleData);
|
||||
if (fakeSampleStreamItem.flags != 0) {
|
||||
buffer.setFlags(fakeSampleStreamItem.flags);
|
||||
readEOSBuffer = buffer.isEndOfStream();
|
||||
}
|
||||
return C.RESULT_BUFFER_READ;
|
||||
}
|
||||
}
|
||||
buffer.setFlags(C.BUFFER_FLAG_END_OF_STREAM);
|
||||
return C.RESULT_BUFFER_READ;
|
||||
return C.RESULT_NOTHING_READ;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
|||
Loading…
Reference in a new issue