Replace onViewportSizeChanged with onSurfaceSizeChanged in AnalyticsListener.

This allows the AnalyticsCollector to register itself as a VideoListener to
get these updates automatically instead of relying on the user to provide
updates.

The ViewportSizeReporter was amended to do the pixel to dp conversion itself.

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=199796532
This commit is contained in:
tonihei 2018-06-08 08:26:31 -07:00 committed by Oliver Woodman
parent 448ce43ddf
commit fcb9ca7b81
6 changed files with 42 additions and 35 deletions

View File

@ -188,6 +188,7 @@ public class SimpleExoPlayer implements ExoPlayer, Player.VideoComponent, Player
analyticsCollector = analyticsCollectorFactory.createAnalyticsCollector(player, clock);
addListener(analyticsCollector);
videoDebugListeners.add(analyticsCollector);
videoListeners.add(analyticsCollector);
audioDebugListeners.add(analyticsCollector);
addMetadataOutput(analyticsCollector);
if (drmSessionManager instanceof DefaultDrmSessionManager) {
@ -1045,8 +1046,12 @@ public class SimpleExoPlayer implements ExoPlayer, Player.VideoComponent, Player
public void onVideoSizeChanged(int width, int height, int unappliedRotationDegrees,
float pixelWidthHeightRatio) {
for (com.google.android.exoplayer2.video.VideoListener videoListener : videoListeners) {
videoListener.onVideoSizeChanged(width, height, unappliedRotationDegrees,
pixelWidthHeightRatio);
// Prevent duplicate notification if a listener is both a VideoRendererDebugListener and
// VideoListener as they have the same method signature.
if (!videoDebugListeners.contains(videoListener)) {
videoListener.onVideoSizeChanged(
width, height, unappliedRotationDegrees, pixelWidthHeightRatio);
}
}
for (VideoRendererEventListener videoDebugListener : videoDebugListeners) {
videoDebugListener.onVideoSizeChanged(width, height, unappliedRotationDegrees,

View File

@ -38,6 +38,7 @@ import com.google.android.exoplayer2.trackselection.TrackSelectionArray;
import com.google.android.exoplayer2.upstream.BandwidthMeter;
import com.google.android.exoplayer2.util.Assertions;
import com.google.android.exoplayer2.util.Clock;
import com.google.android.exoplayer2.video.VideoListener;
import com.google.android.exoplayer2.video.VideoRendererEventListener;
import java.io.IOException;
import java.util.ArrayList;
@ -58,7 +59,8 @@ public class AnalyticsCollector
VideoRendererEventListener,
MediaSourceEventListener,
BandwidthMeter.EventListener,
DefaultDrmSessionEventListener {
DefaultDrmSessionEventListener,
VideoListener {
/** Factory for an analytics collector. */
public static class Factory {
@ -145,19 +147,6 @@ public class AnalyticsCollector
}
}
/**
* Notify analytics collector that the viewport size changed.
*
* @param width The new width of the viewport in device-independent pixels (dp).
* @param height The new height of the viewport in device-independent pixels (dp).
*/
public final void notifyViewportSizeChanged(int width, int height) {
EventTime eventTime = generatePlayingMediaPeriodEventTime();
for (AnalyticsListener listener : listeners) {
listener.onViewportSizeChange(eventTime, width, height);
}
}
/**
* Resets the analytics collector for a new media source. Should be called before the player is
* prepared with a new media source.
@ -284,6 +273,16 @@ public class AnalyticsCollector
}
}
@Override
public final void onVideoDisabled(DecoderCounters counters) {
// The renderers are disabled after we changed the playing media period on the playback thread
// but before this change is reported to the app thread.
EventTime eventTime = generateLastReportedPlayingMediaPeriodEventTime();
for (AnalyticsListener listener : listeners) {
listener.onDecoderDisabled(eventTime, C.TRACK_TYPE_VIDEO, counters);
}
}
@Override
public final void onRenderedFirstFrame(Surface surface) {
EventTime eventTime = generateReadingMediaPeriodEventTime();
@ -292,13 +291,18 @@ public class AnalyticsCollector
}
}
// VideoListener implementation.
@Override
public final void onVideoDisabled(DecoderCounters counters) {
// The renderers are disabled after we changed the playing media period on the playback thread
// but before this change is reported to the app thread.
EventTime eventTime = generateLastReportedPlayingMediaPeriodEventTime();
public final void onRenderedFirstFrame() {
// Do nothing. Already reported in VideoRendererEventListener.onRenderedFirstFrame.
}
@Override
public void onSurfaceSizeChanged(int width, int height) {
EventTime eventTime = generateReadingMediaPeriodEventTime();
for (AnalyticsListener listener : listeners) {
listener.onDecoderDisabled(eventTime, C.TRACK_TYPE_VIDEO, counters);
listener.onSurfaceSizeChanged(eventTime, width, height);
}
}

View File

@ -304,13 +304,15 @@ public interface AnalyticsListener {
EventTime eventTime, int totalLoadTimeMs, long totalBytesLoaded, long bitrateEstimate);
/**
* Called when the viewport size of the output surface changed.
* Called when the output surface size changed.
*
* @param eventTime The event time.
* @param width The width of the viewport in device-independent pixels (dp).
* @param height The height of the viewport in device-independent pixels (dp).
* @param width The surface width in pixels. May be {@link C#LENGTH_UNSET} if unknown, or 0 if the
* video is not rendered onto a surface.
* @param height The surface height in pixels. May be {@link C#LENGTH_UNSET} if unknown, or 0 if
* the video is not rendered onto a surface.
*/
void onViewportSizeChange(EventTime eventTime, int width, int height);
void onSurfaceSizeChanged(EventTime eventTime, int width, int height);
/**
* Called when there is {@link Metadata} associated with the current playback time.

View File

@ -107,7 +107,7 @@ public abstract class DefaultAnalyticsListener implements AnalyticsListener {
EventTime eventTime, int totalLoadTimeMs, long totalBytesLoaded, long bitrateEstimate) {}
@Override
public void onViewportSizeChange(EventTime eventTime, int width, int height) {}
public void onSurfaceSizeChanged(EventTime eventTime, int width, int height) {}
@Override
public void onMetadata(EventTime eventTime, Metadata metadata) {}

View File

@ -363,8 +363,8 @@ public class EventLogger implements AnalyticsListener {
}
@Override
public void onViewportSizeChange(EventTime eventTime, int width, int height) {
logd(eventTime, "viewportSizeChanged", width + ", " + height);
public void onSurfaceSizeChanged(EventTime eventTime, int width, int height) {
logd(eventTime, "surfaceSizeChanged", width + ", " + height);
}
@Override

View File

@ -93,7 +93,7 @@ public final class AnalyticsCollectorTest {
private static final int EVENT_MEDIA_PERIOD_RELEASED = 18;
private static final int EVENT_READING_STARTED = 19;
private static final int EVENT_BANDWIDTH_ESTIMATE = 20;
private static final int EVENT_VIEWPORT_SIZE_CHANGED = 21;
private static final int EVENT_SURFACE_SIZE_CHANGED = 21;
private static final int EVENT_METADATA = 23;
private static final int EVENT_DECODER_ENABLED = 24;
private static final int EVENT_DECODER_INIT = 25;
@ -670,9 +670,6 @@ public final class AnalyticsCollectorTest {
new PlayerRunnable() {
@Override
public void run(SimpleExoPlayer player) {
player
.getAnalyticsCollector()
.notifyViewportSizeChanged(/* width= */ 320, /* height= */ 240);
player.getAnalyticsCollector().notifySeekStarted();
}
})
@ -683,7 +680,6 @@ public final class AnalyticsCollectorTest {
assertThat(listener.getEvents(EVENT_SEEK_STARTED)).containsExactly(PERIOD_0);
assertThat(listener.getEvents(EVENT_SEEK_PROCESSED)).containsExactly(PERIOD_0);
assertThat(listener.getEvents(EVENT_VIEWPORT_SIZE_CHANGED)).containsExactly(PERIOD_0);
}
private static TestAnalyticsListener runAnalyticsTest(MediaSource mediaSource) throws Exception {
@ -1014,8 +1010,8 @@ public final class AnalyticsCollectorTest {
}
@Override
public void onViewportSizeChange(EventTime eventTime, int width, int height) {
reportedEvents.add(new ReportedEvent(EVENT_VIEWPORT_SIZE_CHANGED, eventTime));
public void onSurfaceSizeChanged(EventTime eventTime, int width, int height) {
reportedEvents.add(new ReportedEvent(EVENT_SURFACE_SIZE_CHANGED, eventTime));
}
@Override