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,
private BandwidthMeter bandwidthMeter;
private boolean playerNeedsSource;
private int playerPeriodIndex;
private long playerPosition;
// Activity lifecycle
......@@ -286,7 +287,7 @@ public class PlayerActivity extends Activity implements SurfaceHolder.Callback,
player.setVideoListener(this);
player.setCaptionListener(this);
player.setMetadataListener(this);
player.seekTo(playerPosition);
player.seekTo(playerPeriodIndex, playerPosition);
player.setSurface(surfaceView.getHolder().getSurface());
player.setPlayWhenReady(true);
mediaController.setMediaPlayer(new PlayerControl(player));
......@@ -406,6 +407,7 @@ public class PlayerActivity extends Activity implements SurfaceHolder.Callback,
shutterView.setVisibility(View.VISIBLE);
debugViewHelper.stop();
debugViewHelper = null;
playerPeriodIndex = player.getCurrentPeriodIndex();
playerPosition = player.getCurrentPosition();
player.release();
player = null;
......
......@@ -240,8 +240,6 @@ public interface ExoPlayer {
/**
* 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.
*/
......
......@@ -42,7 +42,7 @@ import java.util.concurrent.CopyOnWriteArraySet;
private boolean playWhenReady;
private int playbackState;
private int pendingPlayWhenReadyAcks;
private int pendingSetMediaSourceAndSeekAcks;
private int pendingSeekAcks;
private boolean isLoading;
// Playback information when there is no pending seek/set source operation.
......@@ -97,15 +97,7 @@ import java.util.concurrent.CopyOnWriteArraySet;
@Override
public void setMediaSource(MediaSource mediaSource) {
maskingPeriodIndex = 0;
maskingPositionMs = 0;
maskingDurationMs = ExoPlayer.UNKNOWN_TIME;
pendingSetMediaSourceAndSeekAcks++;
internalPlayer.setMediaSource(mediaSource);
for (EventListener listener : listeners) {
listener.onPositionDiscontinuity(0, 0);
}
}
@Override
......@@ -147,7 +139,7 @@ import java.util.concurrent.CopyOnWriteArraySet;
maskingPositionMs = positionMs;
maskingDurationMs = periodChanging ? ExoPlayer.UNKNOWN_TIME : getDuration();
pendingSetMediaSourceAndSeekAcks++;
pendingSeekAcks++;
internalPlayer.seekTo(periodIndex, positionMs * 1000);
for (EventListener listener : listeners) {
listener.onPositionDiscontinuity(periodIndex, positionMs);
......@@ -177,7 +169,7 @@ import java.util.concurrent.CopyOnWriteArraySet;
@Override
public long getDuration() {
if (pendingSetMediaSourceAndSeekAcks == 0) {
if (pendingSeekAcks == 0) {
long durationUs = playbackInfo.durationUs;
return durationUs == C.UNSET_TIME_US ? ExoPlayer.UNKNOWN_TIME : durationUs / 1000;
} else {
......@@ -187,18 +179,18 @@ import java.util.concurrent.CopyOnWriteArraySet;
@Override
public long getCurrentPosition() {
return pendingSetMediaSourceAndSeekAcks == 0 ? playbackInfo.positionUs / 1000
return pendingSeekAcks == 0 ? playbackInfo.positionUs / 1000
: maskingPositionMs;
}
@Override
public int getCurrentPeriodIndex() {
return pendingSetMediaSourceAndSeekAcks == 0 ? playbackInfo.periodIndex : maskingPeriodIndex;
return pendingSeekAcks == 0 ? playbackInfo.periodIndex : maskingPeriodIndex;
}
@Override
public long getBufferedPosition() {
if (pendingSetMediaSourceAndSeekAcks == 0) {
if (pendingSeekAcks == 0) {
long bufferedPositionUs = playbackInfo.bufferedPositionUs;
return bufferedPositionUs == C.END_OF_SOURCE_US ? getDuration() : bufferedPositionUs / 1000;
} else {
......@@ -240,14 +232,13 @@ import java.util.concurrent.CopyOnWriteArraySet;
}
break;
}
case ExoPlayerImplInternal.MSG_SET_MEDIA_SOURCE_ACK: // Fall through.
case ExoPlayerImplInternal.MSG_SEEK_ACK: {
pendingSetMediaSourceAndSeekAcks--;
pendingSeekAcks--;
break;
}
case ExoPlayerImplInternal.MSG_PERIOD_CHANGED: {
playbackInfo = (ExoPlayerImplInternal.PlaybackInfo) msg.obj;
if (pendingSetMediaSourceAndSeekAcks == 0) {
if (pendingSeekAcks == 0) {
for (EventListener listener : listeners) {
listener.onPositionDiscontinuity(playbackInfo.periodIndex, 0);
}
......
......@@ -72,10 +72,9 @@ import java.util.ArrayList;
public static final int MSG_STATE_CHANGED = 1;
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_MEDIA_SOURCE_ACK = 4;
public static final int MSG_SEEK_ACK = 5;
public static final int MSG_PERIOD_CHANGED = 6;
public static final int MSG_ERROR = 7;
public static final int MSG_SEEK_ACK = 4;
public static final int MSG_PERIOD_CHANGED = 5;
public static final int MSG_ERROR = 6;
// Internal messages
private static final int MSG_SET_MEDIA_SOURCE = 0;
......@@ -311,15 +310,11 @@ import java.util.ArrayList;
}
private void setMediaSourceInternal(MediaSource mediaSource) {
try {
resetInternal();
this.mediaSource = mediaSource;
mediaSource.prepareSource();
setState(ExoPlayer.STATE_BUFFERING);
handler.sendEmptyMessage(MSG_DO_SOME_WORK);
} finally {
eventHandler.sendEmptyMessage(MSG_SET_MEDIA_SOURCE_ACK);
}
resetInternal();
this.mediaSource = mediaSource;
mediaSource.prepareSource();
setState(ExoPlayer.STATE_BUFFERING);
handler.sendEmptyMessage(MSG_DO_SOME_WORK);
}
private void setPlayWhenReadyInternal(boolean playWhenReady) throws ExoPlaybackException {
......@@ -596,7 +591,6 @@ import java.util.ArrayList;
private Period readingPeriod;
private Period loadingPeriod;
private int pendingPeriodIndex;
private long playingPeriodEndPositionUs;
public Timeline(Renderer[] renderers) {
......@@ -651,7 +645,8 @@ import java.util.ArrayList;
|| (loadingPeriod.isFullyBuffered() && loadingPeriod.index
- (playingPeriod != null ? playingPeriod.index : 0) < MAXIMUM_BUFFER_AHEAD_SOURCES)) {
// 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) {
// Attempt to create the next period.
MediaPeriod mediaPeriod = mediaSource.createPeriod(periodIndex);
......@@ -817,7 +812,6 @@ import java.util.ArrayList;
playingPeriod = null;
readingPeriod = null;
loadingPeriod = null;
pendingPeriodIndex = periodIndex;
resetInternalPosition(seekPositionUs);
}
return seekPositionUs;
......@@ -911,14 +905,12 @@ import java.util.ArrayList;
period.release();
period = period.nextPeriod;
}
playingPeriodEndPositionUs = C.UNSET_TIME_US;
isReady = false;
isEnded = false;
playingPeriod = null;
readingPeriod = null;
loadingPeriod = null;
playingPeriodEndPositionUs = C.UNSET_TIME_US;
pendingPeriodIndex = 0;
playbackInfo = new PlaybackInfo(0);
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