mirror of
https://github.com/androidx/media.git
synced 2025-04-30 06:46:50 +08:00
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:
parent
56ec705275
commit
d3f806fdf4
@ -1241,13 +1241,15 @@ public final class AnalyticsCollectorTest {
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onBufferRead() {
|
||||
if (!renderedFirstFrame) {
|
||||
protected boolean shouldProcessBuffer(long bufferTimeUs, long playbackPositionUs) {
|
||||
boolean shouldProcess = super.shouldProcessBuffer(bufferTimeUs, playbackPositionUs);
|
||||
if (shouldProcess && !renderedFirstFrame) {
|
||||
eventDispatcher.videoSizeChanged(
|
||||
format.width, format.height, format.rotationDegrees, format.pixelWidthHeightRatio);
|
||||
eventDispatcher.renderedFirstFrame(/* surface= */ null);
|
||||
renderedFirstFrame = true;
|
||||
}
|
||||
return shouldProcess;
|
||||
}
|
||||
}
|
||||
|
||||
@ -1291,11 +1293,13 @@ public final class AnalyticsCollectorTest {
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onBufferRead() {
|
||||
if (!notifiedAudioSessionId) {
|
||||
protected boolean shouldProcessBuffer(long bufferTimeUs, long playbackPositionUs) {
|
||||
boolean shouldProcess = super.shouldProcessBuffer(bufferTimeUs, playbackPositionUs);
|
||||
if (shouldProcess && !notifiedAudioSessionId) {
|
||||
eventDispatcher.audioSessionId(/* audioSessionId= */ 1);
|
||||
notifiedAudioSessionId = true;
|
||||
}
|
||||
return shouldProcess;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -49,6 +49,7 @@ public class FakeRenderer extends BaseRenderer {
|
||||
|
||||
private long playbackPositionUs;
|
||||
private long lastSamplePositionUs;
|
||||
private boolean hasPendingBuffer;
|
||||
|
||||
public boolean isEnded;
|
||||
public int positionResetCount;
|
||||
@ -67,6 +68,7 @@ public class FakeRenderer extends BaseRenderer {
|
||||
protected void onPositionReset(long positionUs, boolean joining) throws ExoPlaybackException {
|
||||
playbackPositionUs = positionUs;
|
||||
lastSamplePositionUs = Long.MIN_VALUE;
|
||||
hasPendingBuffer = false;
|
||||
positionResetCount++;
|
||||
isEnded = false;
|
||||
}
|
||||
@ -77,32 +79,40 @@ public class FakeRenderer extends BaseRenderer {
|
||||
return;
|
||||
}
|
||||
playbackPositionUs = positionUs;
|
||||
while (lastSamplePositionUs < positionUs + SOURCE_READAHEAD_US) {
|
||||
FormatHolder formatHolder = getFormatHolder();
|
||||
buffer.clear();
|
||||
int result = readSource(formatHolder, buffer, false);
|
||||
if (result == C.RESULT_FORMAT_READ) {
|
||||
formatReadCount++;
|
||||
assertThat(expectedFormats).contains(formatHolder.format);
|
||||
onFormatChanged(Assertions.checkNotNull(formatHolder.format));
|
||||
} else if (result == C.RESULT_BUFFER_READ) {
|
||||
if (buffer.isEndOfStream()) {
|
||||
isEnded = true;
|
||||
while (true) {
|
||||
if (!hasPendingBuffer) {
|
||||
FormatHolder formatHolder = getFormatHolder();
|
||||
buffer.clear();
|
||||
int result = readSource(formatHolder, buffer, /* formatRequired= */ false);
|
||||
if (result == C.RESULT_FORMAT_READ) {
|
||||
formatReadCount++;
|
||||
assertThat(expectedFormats).contains(formatHolder.format);
|
||||
onFormatChanged(Assertions.checkNotNull(formatHolder.format));
|
||||
} else if (result == C.RESULT_BUFFER_READ) {
|
||||
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;
|
||||
}
|
||||
lastSamplePositionUs = buffer.timeUs;
|
||||
sampleBufferReadCount++;
|
||||
onBufferRead();
|
||||
} else {
|
||||
Assertions.checkState(result == C.RESULT_NOTHING_READ);
|
||||
return;
|
||||
hasPendingBuffer = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isReady() {
|
||||
return lastSamplePositionUs >= playbackPositionUs || isSourceReady();
|
||||
return lastSamplePositionUs >= playbackPositionUs || hasPendingBuffer || isSourceReady();
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -121,6 +131,14 @@ public class FakeRenderer extends BaseRenderer {
|
||||
/** Called when the renderer reads a new 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;
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user