Expand FakeSampleStream to allow specifying a single sample

I removed the buffer.flip() call because it seems incompatible with the way MetadataRenderer deals with the Stream - it calls flip() itself on line 126. Tests fail with flip() here, and pass without it...

PiperOrigin-RevId: 263381799
This commit is contained in:
ibaker 2019-08-14 18:46:12 +01:00 committed by Andrew Lewis
parent bdc8790896
commit 4b75d3338e

View File

@ -32,6 +32,7 @@ public final class FakeSampleStream implements SampleStream {
private final Format format; private final Format format;
@Nullable private final EventDispatcher eventDispatcher; @Nullable private final EventDispatcher eventDispatcher;
private final byte[] sampleData;
private boolean notifiedDownstreamFormat; private boolean notifiedDownstreamFormat;
private boolean readFormat; private boolean readFormat;
@ -47,9 +48,23 @@ public final class FakeSampleStream implements SampleStream {
*/ */
public FakeSampleStream( public FakeSampleStream(
Format format, @Nullable EventDispatcher eventDispatcher, boolean shouldOutputSample) { Format format, @Nullable EventDispatcher eventDispatcher, boolean shouldOutputSample) {
this(format, eventDispatcher, new byte[] {0});
readSample = !shouldOutputSample;
}
/**
* Creates fake sample stream which outputs the given {@link Format}, one sample with the provided
* bytes, then end of stream.
*
* @param format The {@link Format} to output.
* @param eventDispatcher An {@link EventDispatcher} to notify of read events.
* @param sampleData The sample data to output.
*/
public FakeSampleStream(
Format format, @Nullable EventDispatcher eventDispatcher, byte[] sampleData) {
this.format = format; this.format = format;
this.eventDispatcher = eventDispatcher; this.eventDispatcher = eventDispatcher;
readSample = !shouldOutputSample; this.sampleData = sampleData;
} }
@Override @Override
@ -58,8 +73,8 @@ public final class FakeSampleStream implements SampleStream {
} }
@Override @Override
public int readData(FormatHolder formatHolder, DecoderInputBuffer buffer, public int readData(
boolean formatRequired) { FormatHolder formatHolder, DecoderInputBuffer buffer, boolean formatRequired) {
if (eventDispatcher != null && !notifiedDownstreamFormat) { if (eventDispatcher != null && !notifiedDownstreamFormat) {
eventDispatcher.downstreamFormatChanged( eventDispatcher.downstreamFormatChanged(
C.TRACK_TYPE_UNKNOWN, C.TRACK_TYPE_UNKNOWN,
@ -75,9 +90,8 @@ public final class FakeSampleStream implements SampleStream {
return C.RESULT_FORMAT_READ; return C.RESULT_FORMAT_READ;
} else if (!readSample) { } else if (!readSample) {
buffer.timeUs = 0; buffer.timeUs = 0;
buffer.ensureSpaceForWrite(1); buffer.ensureSpaceForWrite(sampleData.length);
buffer.data.put((byte) 0); buffer.data.put(sampleData);
buffer.flip();
readSample = true; readSample = true;
return C.RESULT_BUFFER_READ; return C.RESULT_BUFFER_READ;
} else { } else {
@ -95,5 +109,4 @@ public final class FakeSampleStream implements SampleStream {
public int skipData(long positionUs) { public int skipData(long positionUs) {
return 0; return 0;
} }
} }