Let FakeRenderer subclasses decide whether to render a sample.

This is closer to how our actual renderers look like and allows tests
to use a similar logic to show/suppress the first frame in follow-up changes.

PiperOrigin-RevId: 295557548
This commit is contained in:
tonihei 2020-02-17 12:04:29 +00:00 committed by Ian Baker
parent 56ec705275
commit d3f806fdf4
2 changed files with 44 additions and 22 deletions

View File

@ -1241,13 +1241,15 @@ public final class AnalyticsCollectorTest {
} }
@Override @Override
protected void onBufferRead() { protected boolean shouldProcessBuffer(long bufferTimeUs, long playbackPositionUs) {
if (!renderedFirstFrame) { boolean shouldProcess = super.shouldProcessBuffer(bufferTimeUs, playbackPositionUs);
if (shouldProcess && !renderedFirstFrame) {
eventDispatcher.videoSizeChanged( eventDispatcher.videoSizeChanged(
format.width, format.height, format.rotationDegrees, format.pixelWidthHeightRatio); format.width, format.height, format.rotationDegrees, format.pixelWidthHeightRatio);
eventDispatcher.renderedFirstFrame(/* surface= */ null); eventDispatcher.renderedFirstFrame(/* surface= */ null);
renderedFirstFrame = true; renderedFirstFrame = true;
} }
return shouldProcess;
} }
} }
@ -1291,11 +1293,13 @@ public final class AnalyticsCollectorTest {
} }
@Override @Override
protected void onBufferRead() { protected boolean shouldProcessBuffer(long bufferTimeUs, long playbackPositionUs) {
if (!notifiedAudioSessionId) { boolean shouldProcess = super.shouldProcessBuffer(bufferTimeUs, playbackPositionUs);
if (shouldProcess && !notifiedAudioSessionId) {
eventDispatcher.audioSessionId(/* audioSessionId= */ 1); eventDispatcher.audioSessionId(/* audioSessionId= */ 1);
notifiedAudioSessionId = true; notifiedAudioSessionId = true;
} }
return shouldProcess;
} }
} }

View File

@ -49,6 +49,7 @@ public class FakeRenderer extends BaseRenderer {
private long playbackPositionUs; private long playbackPositionUs;
private long lastSamplePositionUs; private long lastSamplePositionUs;
private boolean hasPendingBuffer;
public boolean isEnded; public boolean isEnded;
public int positionResetCount; public int positionResetCount;
@ -67,6 +68,7 @@ public class FakeRenderer extends BaseRenderer {
protected void onPositionReset(long positionUs, boolean joining) throws ExoPlaybackException { protected void onPositionReset(long positionUs, boolean joining) throws ExoPlaybackException {
playbackPositionUs = positionUs; playbackPositionUs = positionUs;
lastSamplePositionUs = Long.MIN_VALUE; lastSamplePositionUs = Long.MIN_VALUE;
hasPendingBuffer = false;
positionResetCount++; positionResetCount++;
isEnded = false; isEnded = false;
} }
@ -77,32 +79,40 @@ public class FakeRenderer extends BaseRenderer {
return; return;
} }
playbackPositionUs = positionUs; playbackPositionUs = positionUs;
while (lastSamplePositionUs < positionUs + SOURCE_READAHEAD_US) { while (true) {
FormatHolder formatHolder = getFormatHolder(); if (!hasPendingBuffer) {
buffer.clear(); FormatHolder formatHolder = getFormatHolder();
int result = readSource(formatHolder, buffer, false); buffer.clear();
if (result == C.RESULT_FORMAT_READ) { int result = readSource(formatHolder, buffer, /* formatRequired= */ false);
formatReadCount++; if (result == C.RESULT_FORMAT_READ) {
assertThat(expectedFormats).contains(formatHolder.format); formatReadCount++;
onFormatChanged(Assertions.checkNotNull(formatHolder.format)); assertThat(expectedFormats).contains(formatHolder.format);
} else if (result == C.RESULT_BUFFER_READ) { onFormatChanged(Assertions.checkNotNull(formatHolder.format));
if (buffer.isEndOfStream()) { } else if (result == C.RESULT_BUFFER_READ) {
isEnded = true; if (buffer.isEndOfStream()) {
isEnded = true;
return;
}
hasPendingBuffer = true;
} else {
Assertions.checkState(result == C.RESULT_NOTHING_READ);
return;
}
}
if (hasPendingBuffer) {
if (!shouldProcessBuffer(buffer.timeUs, positionUs)) {
return; return;
} }
lastSamplePositionUs = buffer.timeUs; lastSamplePositionUs = buffer.timeUs;
sampleBufferReadCount++; sampleBufferReadCount++;
onBufferRead(); hasPendingBuffer = false;
} else {
Assertions.checkState(result == C.RESULT_NOTHING_READ);
return;
} }
} }
} }
@Override @Override
public boolean isReady() { public boolean isReady() {
return lastSamplePositionUs >= playbackPositionUs || isSourceReady(); return lastSamplePositionUs >= playbackPositionUs || hasPendingBuffer || isSourceReady();
} }
@Override @Override
@ -121,6 +131,14 @@ public class FakeRenderer extends BaseRenderer {
/** Called when the renderer reads a new format. */ /** Called when the renderer reads a new format. */
protected void onFormatChanged(Format format) {} protected void onFormatChanged(Format format) {}
/** Called when the renderer read a sample from the buffer. */ /**
protected void onBufferRead() {} * Called before the renderer processes a buffer.
*
* @param bufferTimeUs The buffer timestamp, in microseconds.
* @param playbackPositionUs The playback position, in microseconds
* @return Whether the buffer should be processed.
*/
protected boolean shouldProcessBuffer(long bufferTimeUs, long playbackPositionUs) {
return bufferTimeUs < playbackPositionUs + SOURCE_READAHEAD_US;
}
} }