EventSampleStream implements SampleStream { @Override public int readData(FormatHolder formatHolder, DecoderInputBuffer buffer, boolean formatRequired) { if (formatRequired || !isFormatSentDownstream) { formatHolder.format = upstreamFormat; isFormatSentDownstream = true; return C.RESULT_FORMAT_READ; } if (currentIndex == eventTimesUs.length) { if (!eventStreamAppendable) { buffer.setFlags(C.BUFFER_FLAG_END_OF_STREAM); return C.RESULT_BUFFER_READ; } else { return C.RESULT_NOTHING_READ; } } int sampleIndex = currentIndex++; byte[] serializedEvent = eventMessageEncoder.encode(eventStream.events[sampleIndex]); if (serializedEvent != null) { buffer.ensureSpaceForWrite(serializedEvent.length); buffer.data.put(serializedEvent); buffer.timeUs = eventTimesUs[sampleIndex]; buffer.setFlags(C.BUFFER_FLAG_KEY_FRAME); return C.RESULT_BUFFER_READ; } else { return C.RESULT_NOTHING_READ; } } EventSampleStream(
      EventStream eventStream, Format upstreamFormat, boolean eventStreamAppendable); String eventStreamId(); void updateEventStream(EventStream eventStream, boolean eventStreamAppendable); void seekToUs(long positionUs); @Override boolean isReady(); @Override void maybeThrowError(); @Override int readData(FormatHolder formatHolder, DecoderInputBuffer buffer,
      boolean formatRequired); @Override int skipData(long positionUs);  }
@Test public void testReadDataReturnFormatForFirstRead() { EventStream eventStream = new EventStream(SCHEME_ID, VALUE, TIME_SCALE, new long[0], new EventMessage[0]); EventSampleStream sampleStream = new EventSampleStream(eventStream, FORMAT, false); int result = readData(sampleStream); assertThat(result).isEqualTo(C.RESULT_FORMAT_READ); assertThat(formatHolder.format).isEqualTo(FORMAT); }
@Test public void testReadDataOutOfBoundReturnEndOfStreamAfterFormatForNonDynamicEventSampleStream() { EventStream eventStream = new EventStream(SCHEME_ID, VALUE, TIME_SCALE, new long[0], new EventMessage[0]); EventSampleStream sampleStream = new EventSampleStream(eventStream, FORMAT, false); readData(sampleStream); int result = readData(sampleStream); assertThat(result).isEqualTo(C.RESULT_BUFFER_READ); assertThat(inputBuffer.isEndOfStream()).isTrue(); }
@Test public void testReadDataOutOfBoundReturnEndOfStreamAfterFormatForDynamicEventSampleStream() { EventStream eventStream = new EventStream(SCHEME_ID, VALUE, TIME_SCALE, new long[0], new EventMessage[0]); EventSampleStream sampleStream = new EventSampleStream(eventStream, FORMAT, true); readData(sampleStream); int result = readData(sampleStream); assertThat(result).isEqualTo(C.RESULT_NOTHING_READ); }
@Test public void testReadDataReturnDataAfterFormat() { long presentationTimeUs = 1000000; EventMessage eventMessage = newEventMessageWithId(1); EventStream eventStream = new EventStream(SCHEME_ID, VALUE, TIME_SCALE, new long[] {presentationTimeUs}, new EventMessage[] {eventMessage}); EventSampleStream sampleStream = new EventSampleStream(eventStream, FORMAT, false); readData(sampleStream); int result = readData(sampleStream); assertThat(result).isEqualTo(C.RESULT_BUFFER_READ); assertThat(inputBuffer.data.array()) .isEqualTo(getEncodedMessage(eventMessage)); }