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,10 +79,11 @@ public class FakeRenderer extends BaseRenderer {
return; return;
} }
playbackPositionUs = positionUs; playbackPositionUs = positionUs;
while (lastSamplePositionUs < positionUs + SOURCE_READAHEAD_US) { while (true) {
if (!hasPendingBuffer) {
FormatHolder formatHolder = getFormatHolder(); FormatHolder formatHolder = getFormatHolder();
buffer.clear(); buffer.clear();
int result = readSource(formatHolder, buffer, false); int result = readSource(formatHolder, buffer, /* formatRequired= */ false);
if (result == C.RESULT_FORMAT_READ) { if (result == C.RESULT_FORMAT_READ) {
formatReadCount++; formatReadCount++;
assertThat(expectedFormats).contains(formatHolder.format); assertThat(expectedFormats).contains(formatHolder.format);
@ -90,19 +93,26 @@ public class FakeRenderer extends BaseRenderer {
isEnded = true; isEnded = true;
return; return;
} }
lastSamplePositionUs = buffer.timeUs; hasPendingBuffer = true;
sampleBufferReadCount++;
onBufferRead();
} else { } else {
Assertions.checkState(result == C.RESULT_NOTHING_READ); Assertions.checkState(result == C.RESULT_NOTHING_READ);
return; return;
} }
} }
if (hasPendingBuffer) {
if (!shouldProcessBuffer(buffer.timeUs, positionUs)) {
return;
}
lastSamplePositionUs = buffer.timeUs;
sampleBufferReadCount++;
hasPendingBuffer = false;
}
}
} }
@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;
}
} }