Skip to content

Commit

Permalink
Fixes for retries
Browse files Browse the repository at this point in the history
- Fix issue in ExoPlayerImpl where the timeline was null'd
  but onTimelineChanged was not fired.
- Add the ability to not reset the timeline. This is useful
  for retries where you know the timeline will be the same
  as it was previously.

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=135797577
  • Loading branch information
ojw28 committed Oct 11, 2016
1 parent 83107cc commit 29f3eb5
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,9 @@ public void onPositionDiscontinuity() {

@Override
public void onTimelineChanged(Timeline timeline, Object manifest) {
if (timeline == null) {
return;
}
int periodCount = timeline.getPeriodCount();
int windowCount = timeline.getWindowCount();
Log.d(TAG, "sourceInfo [periodCount=" + periodCount + ", windowCount=" + windowCount);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ public class PlayerActivity extends Activity implements OnClickListener, ExoPlay
}

private Handler mainHandler;
private Timeline.Window window;
private EventLogger eventLogger;
private SimpleExoPlayerView simpleExoPlayerView;
private LinearLayout debugRootView;
Expand All @@ -115,7 +116,7 @@ public class PlayerActivity extends Activity implements OnClickListener, ExoPlay
private boolean playerNeedsSource;

private boolean shouldAutoPlay;
private boolean shouldRestorePosition;
private boolean isTimelineStatic;
private int playerWindow;
private long playerPosition;

Expand All @@ -127,6 +128,7 @@ public void onCreate(Bundle savedInstanceState) {
shouldAutoPlay = true;
mediaDataSourceFactory = buildDataSourceFactory(true);
mainHandler = new Handler();
window = new Timeline.Window();
if (CookieHandler.getDefault() != DEFAULT_COOKIE_MANAGER) {
CookieHandler.setDefault(DEFAULT_COOKIE_MANAGER);
}
Expand All @@ -147,7 +149,7 @@ public void onCreate(Bundle savedInstanceState) {
@Override
public void onNewIntent(Intent intent) {
releasePlayer();
shouldRestorePosition = false;
isTimelineStatic = false;
setIntent(intent);
}

Expand Down Expand Up @@ -262,7 +264,7 @@ private void initializePlayer() {
player.setVideoDebugListener(eventLogger);
player.setId3Output(eventLogger);
simpleExoPlayerView.setPlayer(player);
if (shouldRestorePosition) {
if (isTimelineStatic) {
if (playerPosition == C.TIME_UNSET) {
player.seekToDefaultPosition(playerWindow);
} else {
Expand Down Expand Up @@ -305,7 +307,7 @@ private void initializePlayer() {
}
MediaSource mediaSource = mediaSources.length == 1 ? mediaSources[0]
: new ConcatenatingMediaSource(mediaSources);
player.prepare(mediaSource, !shouldRestorePosition);
player.prepare(mediaSource, !isTimelineStatic, !isTimelineStatic);
playerNeedsSource = false;
updateButtonVisibilities();
}
Expand Down Expand Up @@ -348,15 +350,11 @@ private void releasePlayer() {
debugViewHelper.stop();
debugViewHelper = null;
shouldAutoPlay = player.getPlayWhenReady();
shouldRestorePosition = false;
playerWindow = player.getCurrentWindowIndex();
playerPosition = C.TIME_UNSET;
Timeline timeline = player.getCurrentTimeline();
if (timeline != null) {
playerWindow = player.getCurrentWindowIndex();
Timeline.Window window = timeline.getWindow(playerWindow, new Timeline.Window());
if (!window.isDynamic) {
shouldRestorePosition = true;
playerPosition = window.isSeekable ? player.getCurrentPosition() : C.TIME_UNSET;
}
if (timeline != null && timeline.getWindow(playerWindow, window).isSeekable) {
playerPosition = player.getCurrentPosition();
}
player.release();
player = null;
Expand Down Expand Up @@ -412,7 +410,8 @@ public void onPositionDiscontinuity() {

@Override
public void onTimelineChanged(Timeline timeline, Object manifest) {
// Do nothing.
isTimelineStatic = timeline != null && timeline.getWindowCount() > 0
&& !timeline.getWindow(timeline.getWindowCount() - 1, window).isDynamic;
}

@Override
Expand Down Expand Up @@ -501,7 +500,7 @@ private void updateButtonVisibilities() {
button.setText(label);
button.setTag(i);
button.setOnClickListener(this);
debugRootView.addView(button);
debugRootView.addView(button, debugRootView.getChildCount() - 1);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,8 +130,8 @@ interface EventListener {
/**
* Called when timeline and/or manifest has been refreshed.
*
* @param timeline The latest timeline.
* @param manifest The latest manifest.
* @param timeline The latest timeline, or null if the timeline is being cleared.
* @param manifest The latest manifest, or null if the manifest is being cleared.
*/
void onTimelineChanged(Timeline timeline, Object manifest);

Expand Down Expand Up @@ -247,7 +247,7 @@ public ExoPlayerMessage(ExoPlayerComponent target, int messageType, Object messa

/**
* Prepares the player to play the provided {@link MediaSource}. Equivalent to
* {@code prepare(mediaSource, true)}.
* {@code prepare(mediaSource, true, true)}.
*/
void prepare(MediaSource mediaSource);

Expand All @@ -259,8 +259,11 @@ public ExoPlayerMessage(ExoPlayerComponent target, int messageType, Object messa
* @param resetPosition Whether the playback position should be reset to the default position in
* the first {@link Timeline.Window}. If false, playback will start from the position defined
* by {@link #getCurrentWindowIndex()} and {@link #getCurrentPosition()}.
* @param resetTimeline Whether the timeline and manifest should be reset. Should be true unless
* the player is being prepared to play the same media as it was playing previously (e.g. if
* playback failed and is being retried).
*/
void prepare(MediaSource mediaSource, boolean resetPosition);
void prepare(MediaSource mediaSource, boolean resetPosition, boolean resetTimeline);

/**
* Sets whether playback should proceed when {@link #getPlaybackState()} == {@link #STATE_READY}.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,12 +101,18 @@ public int getPlaybackState() {

@Override
public void prepare(MediaSource mediaSource) {
prepare(mediaSource, true);
prepare(mediaSource, true, true);
}

@Override
public void prepare(MediaSource mediaSource, boolean resetPosition) {
timeline = null;
public void prepare(MediaSource mediaSource, boolean resetPosition, boolean resetTimeline) {
if (resetTimeline && (timeline != null || manifest != null)) {
timeline = null;
manifest = null;
for (EventListener listener : listeners) {
listener.onTimelineChanged(null, null);
}
}
internalPlayer.prepare(mediaSource, resetPosition);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -420,8 +420,8 @@ public void prepare(MediaSource mediaSource) {
}

@Override
public void prepare(MediaSource mediaSource, boolean resetPosition) {
player.prepare(mediaSource, resetPosition);
public void prepare(MediaSource mediaSource, boolean resetPosition, boolean resetTimeline) {
player.prepare(mediaSource, resetPosition, resetTimeline);
}

@Override
Expand Down

0 comments on commit 29f3eb5

Please # to comment.