Add reason to onPositionDiscontinuity.

This allows listeners to easily determine the source of the discontinuity.
Reasons can be period transitions, seeks, and internal reasons.

Listeners still using the deprecated ExoPlayer.EventListener interface were
updated to Player.EventListener.

GitHub: #3252

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=168549612
This commit is contained in:
tonihei 2017-09-13 09:25:46 -07:00 committed by Oliver Woodman
parent 872cbec9e1
commit 7d59383cc4
17 changed files with 60 additions and 24 deletions

View File

@ -105,8 +105,8 @@ import java.util.Locale;
} }
@Override @Override
public void onPositionDiscontinuity() { public void onPositionDiscontinuity(@Player.DiscontinuityReason int reason) {
Log.d(TAG, "positionDiscontinuity"); Log.d(TAG, "positionDiscontinuity [" + getDiscontinuityReasonString(reason) + "]");
} }
@Override @Override
@ -484,4 +484,17 @@ import java.util.Locale;
return "?"; return "?";
} }
} }
private static String getDiscontinuityReasonString(@Player.DiscontinuityReason int reason) {
switch (reason) {
case Player.DISCONTINUITY_REASON_PERIOD_TRANSITION:
return "PERIOD_TRANSITION";
case Player.DISCONTINUITY_REASON_SEEK:
return "SEEK";
case Player.DISCONTINUITY_REASON_INTERNAL:
return "INTERNAL";
default:
return "?";
}
}
} }

View File

@ -510,7 +510,7 @@ public class PlayerActivity extends Activity implements OnClickListener, EventLi
} }
@Override @Override
public void onPositionDiscontinuity() { public void onPositionDiscontinuity(@Player.DiscontinuityReason int reason) {
if (inErrorState) { if (inErrorState) {
// This will only occur if the user has performed a seek whilst in the error state. Update the // This will only occur if the user has performed a seek whilst in the error state. Update the
// resume position so that if the user then retries, playback will resume from the position to // resume position so that if the user then retries, playback will resume from the position to

View File

@ -336,7 +336,7 @@ public final class CastPlayer implements Player {
pendingSeekWindowIndex = windowIndex; pendingSeekWindowIndex = windowIndex;
pendingSeekPositionMs = positionMs; pendingSeekPositionMs = positionMs;
for (EventListener listener : listeners) { for (EventListener listener : listeners) {
listener.onPositionDiscontinuity(); listener.onPositionDiscontinuity(Player.DISCONTINUITY_REASON_SEEK);
} }
} }
} }
@ -539,7 +539,7 @@ public final class CastPlayer implements Player {
if (this.currentWindowIndex != currentWindowIndex) { if (this.currentWindowIndex != currentWindowIndex) {
this.currentWindowIndex = currentWindowIndex; this.currentWindowIndex = currentWindowIndex;
for (EventListener listener : listeners) { for (EventListener listener : listeners) {
listener.onPositionDiscontinuity(); listener.onPositionDiscontinuity(DISCONTINUITY_REASON_PERIOD_TRANSITION);
} }
} }
if (updateTracksAndSelections()) { if (updateTracksAndSelections()) {

View File

@ -100,7 +100,7 @@ public class FlacPlaybackTest extends InstrumentationTestCase {
} }
@Override @Override
public void onPositionDiscontinuity() { public void onPositionDiscontinuity(@Player.DiscontinuityReason int reason) {
// Do nothing. // Do nothing.
} }

View File

@ -583,7 +583,7 @@ public final class ImaAdsLoader implements Player.EventListener, VideoAdPlayer,
} }
@Override @Override
public void onPositionDiscontinuity() { public void onPositionDiscontinuity(@Player.DiscontinuityReason int reason) {
if (adsManager == null) { if (adsManager == null) {
return; return;
} }

View File

@ -32,6 +32,7 @@ import com.google.android.exoplayer2.ExoPlaybackException;
import com.google.android.exoplayer2.ExoPlayerLibraryInfo; import com.google.android.exoplayer2.ExoPlayerLibraryInfo;
import com.google.android.exoplayer2.PlaybackParameters; import com.google.android.exoplayer2.PlaybackParameters;
import com.google.android.exoplayer2.Player; import com.google.android.exoplayer2.Player;
import com.google.android.exoplayer2.Player.DiscontinuityReason;
import com.google.android.exoplayer2.SimpleExoPlayer; import com.google.android.exoplayer2.SimpleExoPlayer;
import com.google.android.exoplayer2.Timeline; import com.google.android.exoplayer2.Timeline;
import com.google.android.exoplayer2.source.TrackGroupArray; import com.google.android.exoplayer2.source.TrackGroupArray;
@ -278,7 +279,7 @@ public final class LeanbackPlayerAdapter extends PlayerAdapter {
} }
@Override @Override
public void onPositionDiscontinuity() { public void onPositionDiscontinuity(@DiscontinuityReason int reason) {
Callback callback = getCallback(); Callback callback = getCallback();
callback.onCurrentPositionChanged(LeanbackPlayerAdapter.this); callback.onCurrentPositionChanged(LeanbackPlayerAdapter.this);
callback.onBufferedPositionChanged(LeanbackPlayerAdapter.this); callback.onBufferedPositionChanged(LeanbackPlayerAdapter.this);

View File

@ -686,7 +686,7 @@ public final class MediaSessionConnector {
} }
@Override @Override
public void onPositionDiscontinuity() { public void onPositionDiscontinuity(@Player.DiscontinuityReason int reason) {
if (currentWindowIndex != player.getCurrentWindowIndex()) { if (currentWindowIndex != player.getCurrentWindowIndex()) {
if (queueNavigator != null) { if (queueNavigator != null) {
queueNavigator.onCurrentWindowIndexChanged(player); queueNavigator.onCurrentWindowIndexChanged(player);

View File

@ -100,7 +100,7 @@ public class OpusPlaybackTest extends InstrumentationTestCase {
} }
@Override @Override
public void onPositionDiscontinuity() { public void onPositionDiscontinuity(@Player.DiscontinuityReason int reason) {
// Do nothing. // Do nothing.
} }

View File

@ -132,7 +132,7 @@ public class VpxPlaybackTest extends InstrumentationTestCase {
} }
@Override @Override
public void onPositionDiscontinuity() { public void onPositionDiscontinuity(@Player.DiscontinuityReason int reason) {
// Do nothing. // Do nothing.
} }

View File

@ -257,7 +257,7 @@ import java.util.concurrent.CopyOnWriteArraySet;
maskingWindowPositionMs = positionMs; maskingWindowPositionMs = positionMs;
internalPlayer.seekTo(timeline, windowIndex, C.msToUs(positionMs)); internalPlayer.seekTo(timeline, windowIndex, C.msToUs(positionMs));
for (Player.EventListener listener : listeners) { for (Player.EventListener listener : listeners) {
listener.onPositionDiscontinuity(); listener.onPositionDiscontinuity(DISCONTINUITY_REASON_SEEK);
} }
} }
} }
@ -484,7 +484,7 @@ import java.util.concurrent.CopyOnWriteArraySet;
} }
if (msg.arg1 != 0) { if (msg.arg1 != 0) {
for (Player.EventListener listener : listeners) { for (Player.EventListener listener : listeners) {
listener.onPositionDiscontinuity(); listener.onPositionDiscontinuity(DISCONTINUITY_REASON_SEEK);
} }
} }
} }
@ -494,7 +494,7 @@ import java.util.concurrent.CopyOnWriteArraySet;
if (pendingSeekAcks == 0) { if (pendingSeekAcks == 0) {
playbackInfo = (ExoPlayerImplInternal.PlaybackInfo) msg.obj; playbackInfo = (ExoPlayerImplInternal.PlaybackInfo) msg.obj;
for (Player.EventListener listener : listeners) { for (Player.EventListener listener : listeners) {
listener.onPositionDiscontinuity(); listener.onPositionDiscontinuity(DISCONTINUITY_REASON_PERIOD_TRANSITION);
} }
} }
break; break;

View File

@ -55,7 +55,7 @@ public interface Player {
* Note that if the timeline has changed then a position discontinuity may also have occurred. * Note that if the timeline has changed then a position discontinuity may also have occurred.
* For example, the current period index may have changed as a result of periods being added or * For example, the current period index may have changed as a result of periods being added or
* removed from the timeline. This will <em>not</em> be reported via a separate call to * removed from the timeline. This will <em>not</em> be reported via a separate call to
* {@link #onPositionDiscontinuity()}. * {@link #onPositionDiscontinuity(int)}.
* *
* @param timeline The latest timeline. Never null, but may be empty. * @param timeline The latest timeline. Never null, but may be empty.
* @param manifest The latest manifest. May be null. * @param manifest The latest manifest. May be null.
@ -119,8 +119,10 @@ public interface Player {
* <p> * <p>
* When a position discontinuity occurs as a result of a change to the timeline this method is * When a position discontinuity occurs as a result of a change to the timeline this method is
* <em>not</em> called. {@link #onTimelineChanged(Timeline, Object)} is called in this case. * <em>not</em> called. {@link #onTimelineChanged(Timeline, Object)} is called in this case.
*
* @param reason The {@link DiscontinuityReason} responsible for the discontinuity.
*/ */
void onPositionDiscontinuity(); void onPositionDiscontinuity(@DiscontinuityReason int reason);
/** /**
* Called when the current playback parameters change. The playback parameters may change due to * Called when the current playback parameters change. The playback parameters may change due to
@ -172,6 +174,27 @@ public interface Player {
*/ */
int REPEAT_MODE_ALL = 2; int REPEAT_MODE_ALL = 2;
/**
* Reasons for position discontinuities.
*/
@Retention(RetentionPolicy.SOURCE)
@IntDef({DISCONTINUITY_REASON_PERIOD_TRANSITION, DISCONTINUITY_REASON_SEEK,
DISCONTINUITY_REASON_INTERNAL})
public @interface DiscontinuityReason {}
/**
* Automatic playback transition from one period in the timeline to the next. The period index may
* be the same as it was before the discontinuity in case the current period is repeated.
*/
int DISCONTINUITY_REASON_PERIOD_TRANSITION = 0;
/**
* Seek within the current period or to another period.
*/
int DISCONTINUITY_REASON_SEEK = 1;
/**
* Discontinuity introduced internally by the source.
*/
int DISCONTINUITY_REASON_INTERNAL = 2;
/** /**
* Register a listener to receive events from the player. The listener's methods will be called on * Register a listener to receive events from the player. The listener's methods will be called on
* the thread that was used to construct the player. However, if the thread used to construct the * the thread that was used to construct the player. However, if the thread used to construct the

View File

@ -99,7 +99,7 @@ public final class DebugTextViewHelper implements Runnable, Player.EventListener
} }
@Override @Override
public void onPositionDiscontinuity() { public void onPositionDiscontinuity(@Player.DiscontinuityReason int reason) {
updateAndPost(); updateAndPost();
} }

View File

@ -1090,7 +1090,7 @@ public class PlaybackControlView extends FrameLayout {
} }
@Override @Override
public void onPositionDiscontinuity() { public void onPositionDiscontinuity(@Player.DiscontinuityReason int reason) {
updateNavigation(); updateNavigation();
updateProgress(); updateProgress();
} }
@ -1150,4 +1150,3 @@ public class PlaybackControlView extends FrameLayout {
} }
} }

View File

@ -927,7 +927,7 @@ public final class SimpleExoPlayerView extends FrameLayout {
} }
@Override @Override
public void onPositionDiscontinuity() { public void onPositionDiscontinuity(@Player.DiscontinuityReason int reason) {
// Do nothing. // Do nothing.
} }

View File

@ -368,7 +368,7 @@ public abstract class Action {
final ActionNode nextAction) { final ActionNode nextAction) {
player.addListener(new PlayerListener() { player.addListener(new PlayerListener() {
@Override @Override
public void onPositionDiscontinuity() { public void onPositionDiscontinuity(@Player.DiscontinuityReason int reason) {
player.removeListener(this); player.removeListener(this);
nextAction.schedule(player, trackSelector, surface, handler); nextAction.schedule(player, trackSelector, surface, handler);
} }
@ -445,7 +445,7 @@ public abstract class Action {
} }
@Override @Override
public void onPositionDiscontinuity() { public void onPositionDiscontinuity(@Player.DiscontinuityReason int reason) {
} }

View File

@ -248,7 +248,7 @@ public abstract class ExoHostedTest implements HostedTest, Player.EventListener,
} }
@Override @Override
public final void onPositionDiscontinuity() { public final void onPositionDiscontinuity(@Player.DiscontinuityReason int reason) {
// Do nothing. // Do nothing.
} }

View File

@ -374,7 +374,7 @@ public final class ExoPlayerTestRunner implements Player.EventListener {
} }
@Override @Override
public void onPositionDiscontinuity() { public void onPositionDiscontinuity(@Player.DiscontinuityReason int reason) {
positionDiscontinuityCount++; positionDiscontinuityCount++;
periodIndices.add(player.getCurrentPeriodIndex()); periodIndices.add(player.getCurrentPeriodIndex());
} }