Commit 98a0c5f0 by olly Committed by Oliver Woodman

Fix restoring of position.

This change restores ExoPlayer to its previous behaviour.
I think we'll want a "resetPosition" boolean parameter on
setMediaSource at some point, but no need to add it right
now. Note that this would not always reset to the start
of the source. For a live playback it will reset to the
desired position (normally somewhere slightly behind the
live edge).

Issue: #1667
-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=127549700
parent 15631e12
...@@ -137,6 +137,7 @@ public class PlayerActivity extends Activity implements SurfaceHolder.Callback, ...@@ -137,6 +137,7 @@ public class PlayerActivity extends Activity implements SurfaceHolder.Callback,
private BandwidthMeter bandwidthMeter; private BandwidthMeter bandwidthMeter;
private boolean playerNeedsSource; private boolean playerNeedsSource;
private int playerPeriodIndex;
private long playerPosition; private long playerPosition;
// Activity lifecycle // Activity lifecycle
...@@ -286,7 +287,7 @@ public class PlayerActivity extends Activity implements SurfaceHolder.Callback, ...@@ -286,7 +287,7 @@ public class PlayerActivity extends Activity implements SurfaceHolder.Callback,
player.setVideoListener(this); player.setVideoListener(this);
player.setCaptionListener(this); player.setCaptionListener(this);
player.setMetadataListener(this); player.setMetadataListener(this);
player.seekTo(playerPosition); player.seekTo(playerPeriodIndex, playerPosition);
player.setSurface(surfaceView.getHolder().getSurface()); player.setSurface(surfaceView.getHolder().getSurface());
player.setPlayWhenReady(true); player.setPlayWhenReady(true);
mediaController.setMediaPlayer(new PlayerControl(player)); mediaController.setMediaPlayer(new PlayerControl(player));
...@@ -406,6 +407,7 @@ public class PlayerActivity extends Activity implements SurfaceHolder.Callback, ...@@ -406,6 +407,7 @@ public class PlayerActivity extends Activity implements SurfaceHolder.Callback,
shutterView.setVisibility(View.VISIBLE); shutterView.setVisibility(View.VISIBLE);
debugViewHelper.stop(); debugViewHelper.stop();
debugViewHelper = null; debugViewHelper = null;
playerPeriodIndex = player.getCurrentPeriodIndex();
playerPosition = player.getCurrentPosition(); playerPosition = player.getCurrentPosition();
player.release(); player.release();
player = null; player = null;
......
...@@ -240,8 +240,6 @@ public interface ExoPlayer { ...@@ -240,8 +240,6 @@ public interface ExoPlayer {
/** /**
* Sets the {@link MediaSource} to play. * Sets the {@link MediaSource} to play.
* <p>
* The player's position will be reset to the start of the source.
* *
* @param mediaSource The {@link MediaSource} to play. * @param mediaSource The {@link MediaSource} to play.
*/ */
......
...@@ -42,7 +42,7 @@ import java.util.concurrent.CopyOnWriteArraySet; ...@@ -42,7 +42,7 @@ import java.util.concurrent.CopyOnWriteArraySet;
private boolean playWhenReady; private boolean playWhenReady;
private int playbackState; private int playbackState;
private int pendingPlayWhenReadyAcks; private int pendingPlayWhenReadyAcks;
private int pendingSetMediaSourceAndSeekAcks; private int pendingSeekAcks;
private boolean isLoading; private boolean isLoading;
// Playback information when there is no pending seek/set source operation. // Playback information when there is no pending seek/set source operation.
...@@ -97,15 +97,7 @@ import java.util.concurrent.CopyOnWriteArraySet; ...@@ -97,15 +97,7 @@ import java.util.concurrent.CopyOnWriteArraySet;
@Override @Override
public void setMediaSource(MediaSource mediaSource) { public void setMediaSource(MediaSource mediaSource) {
maskingPeriodIndex = 0;
maskingPositionMs = 0;
maskingDurationMs = ExoPlayer.UNKNOWN_TIME;
pendingSetMediaSourceAndSeekAcks++;
internalPlayer.setMediaSource(mediaSource); internalPlayer.setMediaSource(mediaSource);
for (EventListener listener : listeners) {
listener.onPositionDiscontinuity(0, 0);
}
} }
@Override @Override
...@@ -147,7 +139,7 @@ import java.util.concurrent.CopyOnWriteArraySet; ...@@ -147,7 +139,7 @@ import java.util.concurrent.CopyOnWriteArraySet;
maskingPositionMs = positionMs; maskingPositionMs = positionMs;
maskingDurationMs = periodChanging ? ExoPlayer.UNKNOWN_TIME : getDuration(); maskingDurationMs = periodChanging ? ExoPlayer.UNKNOWN_TIME : getDuration();
pendingSetMediaSourceAndSeekAcks++; pendingSeekAcks++;
internalPlayer.seekTo(periodIndex, positionMs * 1000); internalPlayer.seekTo(periodIndex, positionMs * 1000);
for (EventListener listener : listeners) { for (EventListener listener : listeners) {
listener.onPositionDiscontinuity(periodIndex, positionMs); listener.onPositionDiscontinuity(periodIndex, positionMs);
...@@ -177,7 +169,7 @@ import java.util.concurrent.CopyOnWriteArraySet; ...@@ -177,7 +169,7 @@ import java.util.concurrent.CopyOnWriteArraySet;
@Override @Override
public long getDuration() { public long getDuration() {
if (pendingSetMediaSourceAndSeekAcks == 0) { if (pendingSeekAcks == 0) {
long durationUs = playbackInfo.durationUs; long durationUs = playbackInfo.durationUs;
return durationUs == C.UNSET_TIME_US ? ExoPlayer.UNKNOWN_TIME : durationUs / 1000; return durationUs == C.UNSET_TIME_US ? ExoPlayer.UNKNOWN_TIME : durationUs / 1000;
} else { } else {
...@@ -187,18 +179,18 @@ import java.util.concurrent.CopyOnWriteArraySet; ...@@ -187,18 +179,18 @@ import java.util.concurrent.CopyOnWriteArraySet;
@Override @Override
public long getCurrentPosition() { public long getCurrentPosition() {
return pendingSetMediaSourceAndSeekAcks == 0 ? playbackInfo.positionUs / 1000 return pendingSeekAcks == 0 ? playbackInfo.positionUs / 1000
: maskingPositionMs; : maskingPositionMs;
} }
@Override @Override
public int getCurrentPeriodIndex() { public int getCurrentPeriodIndex() {
return pendingSetMediaSourceAndSeekAcks == 0 ? playbackInfo.periodIndex : maskingPeriodIndex; return pendingSeekAcks == 0 ? playbackInfo.periodIndex : maskingPeriodIndex;
} }
@Override @Override
public long getBufferedPosition() { public long getBufferedPosition() {
if (pendingSetMediaSourceAndSeekAcks == 0) { if (pendingSeekAcks == 0) {
long bufferedPositionUs = playbackInfo.bufferedPositionUs; long bufferedPositionUs = playbackInfo.bufferedPositionUs;
return bufferedPositionUs == C.END_OF_SOURCE_US ? getDuration() : bufferedPositionUs / 1000; return bufferedPositionUs == C.END_OF_SOURCE_US ? getDuration() : bufferedPositionUs / 1000;
} else { } else {
...@@ -240,14 +232,13 @@ import java.util.concurrent.CopyOnWriteArraySet; ...@@ -240,14 +232,13 @@ import java.util.concurrent.CopyOnWriteArraySet;
} }
break; break;
} }
case ExoPlayerImplInternal.MSG_SET_MEDIA_SOURCE_ACK: // Fall through.
case ExoPlayerImplInternal.MSG_SEEK_ACK: { case ExoPlayerImplInternal.MSG_SEEK_ACK: {
pendingSetMediaSourceAndSeekAcks--; pendingSeekAcks--;
break; break;
} }
case ExoPlayerImplInternal.MSG_PERIOD_CHANGED: { case ExoPlayerImplInternal.MSG_PERIOD_CHANGED: {
playbackInfo = (ExoPlayerImplInternal.PlaybackInfo) msg.obj; playbackInfo = (ExoPlayerImplInternal.PlaybackInfo) msg.obj;
if (pendingSetMediaSourceAndSeekAcks == 0) { if (pendingSeekAcks == 0) {
for (EventListener listener : listeners) { for (EventListener listener : listeners) {
listener.onPositionDiscontinuity(playbackInfo.periodIndex, 0); listener.onPositionDiscontinuity(playbackInfo.periodIndex, 0);
} }
......
...@@ -72,10 +72,9 @@ import java.util.ArrayList; ...@@ -72,10 +72,9 @@ import java.util.ArrayList;
public static final int MSG_STATE_CHANGED = 1; public static final int MSG_STATE_CHANGED = 1;
public static final int MSG_LOADING_CHANGED = 2; public static final int MSG_LOADING_CHANGED = 2;
public static final int MSG_SET_PLAY_WHEN_READY_ACK = 3; public static final int MSG_SET_PLAY_WHEN_READY_ACK = 3;
public static final int MSG_SET_MEDIA_SOURCE_ACK = 4; public static final int MSG_SEEK_ACK = 4;
public static final int MSG_SEEK_ACK = 5; public static final int MSG_PERIOD_CHANGED = 5;
public static final int MSG_PERIOD_CHANGED = 6; public static final int MSG_ERROR = 6;
public static final int MSG_ERROR = 7;
// Internal messages // Internal messages
private static final int MSG_SET_MEDIA_SOURCE = 0; private static final int MSG_SET_MEDIA_SOURCE = 0;
...@@ -311,15 +310,11 @@ import java.util.ArrayList; ...@@ -311,15 +310,11 @@ import java.util.ArrayList;
} }
private void setMediaSourceInternal(MediaSource mediaSource) { private void setMediaSourceInternal(MediaSource mediaSource) {
try {
resetInternal(); resetInternal();
this.mediaSource = mediaSource; this.mediaSource = mediaSource;
mediaSource.prepareSource(); mediaSource.prepareSource();
setState(ExoPlayer.STATE_BUFFERING); setState(ExoPlayer.STATE_BUFFERING);
handler.sendEmptyMessage(MSG_DO_SOME_WORK); handler.sendEmptyMessage(MSG_DO_SOME_WORK);
} finally {
eventHandler.sendEmptyMessage(MSG_SET_MEDIA_SOURCE_ACK);
}
} }
private void setPlayWhenReadyInternal(boolean playWhenReady) throws ExoPlaybackException { private void setPlayWhenReadyInternal(boolean playWhenReady) throws ExoPlaybackException {
...@@ -596,7 +591,6 @@ import java.util.ArrayList; ...@@ -596,7 +591,6 @@ import java.util.ArrayList;
private Period readingPeriod; private Period readingPeriod;
private Period loadingPeriod; private Period loadingPeriod;
private int pendingPeriodIndex;
private long playingPeriodEndPositionUs; private long playingPeriodEndPositionUs;
public Timeline(Renderer[] renderers) { public Timeline(Renderer[] renderers) {
...@@ -651,7 +645,8 @@ import java.util.ArrayList; ...@@ -651,7 +645,8 @@ import java.util.ArrayList;
|| (loadingPeriod.isFullyBuffered() && loadingPeriod.index || (loadingPeriod.isFullyBuffered() && loadingPeriod.index
- (playingPeriod != null ? playingPeriod.index : 0) < MAXIMUM_BUFFER_AHEAD_SOURCES)) { - (playingPeriod != null ? playingPeriod.index : 0) < MAXIMUM_BUFFER_AHEAD_SOURCES)) {
// Try and obtain the next period to start loading. // Try and obtain the next period to start loading.
int periodIndex = loadingPeriod == null ? pendingPeriodIndex : loadingPeriod.index + 1; int periodIndex = loadingPeriod == null ? playbackInfo.periodIndex
: loadingPeriod.index + 1;
if (periodCount == MediaSource.UNKNOWN_PERIOD_COUNT || periodIndex < periodCount) { if (periodCount == MediaSource.UNKNOWN_PERIOD_COUNT || periodIndex < periodCount) {
// Attempt to create the next period. // Attempt to create the next period.
MediaPeriod mediaPeriod = mediaSource.createPeriod(periodIndex); MediaPeriod mediaPeriod = mediaSource.createPeriod(periodIndex);
...@@ -817,7 +812,6 @@ import java.util.ArrayList; ...@@ -817,7 +812,6 @@ import java.util.ArrayList;
playingPeriod = null; playingPeriod = null;
readingPeriod = null; readingPeriod = null;
loadingPeriod = null; loadingPeriod = null;
pendingPeriodIndex = periodIndex;
resetInternalPosition(seekPositionUs); resetInternalPosition(seekPositionUs);
} }
return seekPositionUs; return seekPositionUs;
...@@ -911,14 +905,12 @@ import java.util.ArrayList; ...@@ -911,14 +905,12 @@ import java.util.ArrayList;
period.release(); period.release();
period = period.nextPeriod; period = period.nextPeriod;
} }
playingPeriodEndPositionUs = C.UNSET_TIME_US;
isReady = false; isReady = false;
isEnded = false; isEnded = false;
playingPeriod = null; playingPeriod = null;
readingPeriod = null; readingPeriod = null;
loadingPeriod = null; loadingPeriod = null;
playingPeriodEndPositionUs = C.UNSET_TIME_US;
pendingPeriodIndex = 0;
playbackInfo = new PlaybackInfo(0);
eventHandler.obtainMessage(MSG_PERIOD_CHANGED, playbackInfo).sendToTarget(); eventHandler.obtainMessage(MSG_PERIOD_CHANGED, playbackInfo).sendToTarget();
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment