From 199b983d42e0cf9c3f3eed68f286cd587cef27ca Mon Sep 17 00:00:00 2001 From: olly Date: Tue, 31 Oct 2017 06:56:47 -0700 Subject: [PATCH] Split PlaybackInfo into its own file ------------- Created by MOE: https://github.com/google/moe MOE_MIGRATED_REVID=174030901 --- .../android/exoplayer2/ExoPlayerImpl.java | 44 +++++------ .../exoplayer2/ExoPlayerImplInternal.java | 61 --------------- .../exoplayer2/MediaPeriodInfoSequence.java | 1 - .../android/exoplayer2/PlaybackInfo.java | 78 +++++++++++++++++++ 4 files changed, 98 insertions(+), 86 deletions(-) create mode 100644 library/core/src/main/java/com/google/android/exoplayer2/PlaybackInfo.java diff --git a/library/core/src/main/java/com/google/android/exoplayer2/ExoPlayerImpl.java b/library/core/src/main/java/com/google/android/exoplayer2/ExoPlayerImpl.java index 92f5d0c844..d8c6184d3f 100644 --- a/library/core/src/main/java/com/google/android/exoplayer2/ExoPlayerImpl.java +++ b/library/core/src/main/java/com/google/android/exoplayer2/ExoPlayerImpl.java @@ -21,7 +21,6 @@ import android.os.Looper; import android.os.Message; import android.support.annotation.Nullable; import android.util.Log; -import com.google.android.exoplayer2.ExoPlayerImplInternal.PlaybackInfo; import com.google.android.exoplayer2.source.MediaSource; import com.google.android.exoplayer2.source.MediaSource.MediaPeriodId; import com.google.android.exoplayer2.source.TrackGroupArray; @@ -57,8 +56,6 @@ import java.util.concurrent.CopyOnWriteArraySet; private int pendingSeekAcks; private int pendingPrepareAcks; private boolean isLoading; - private Timeline timeline; - private Object manifest; private TrackGroupArray trackGroups; private TrackSelectionArray trackSelections; private PlaybackParameters playbackParameters; @@ -91,7 +88,6 @@ import java.util.concurrent.CopyOnWriteArraySet; this.playbackState = Player.STATE_IDLE; this.listeners = new CopyOnWriteArraySet<>(); emptyTrackSelections = new TrackSelectionArray(new TrackSelection[renderers.length]); - timeline = Timeline.EMPTY; window = new Timeline.Window(); period = new Timeline.Period(); trackGroups = TrackGroupArray.EMPTY; @@ -104,7 +100,7 @@ import java.util.concurrent.CopyOnWriteArraySet; ExoPlayerImpl.this.handleEvent(msg); } }; - playbackInfo = new ExoPlayerImplInternal.PlaybackInfo(timeline, manifest, 0, 0); + playbackInfo = new PlaybackInfo(Timeline.EMPTY, null, 0, 0); internalPlayer = new ExoPlayerImplInternal(renderers, trackSelector, loadControl, playWhenReady, repeatMode, shuffleModeEnabled, eventHandler, this); } @@ -146,11 +142,10 @@ import java.util.concurrent.CopyOnWriteArraySet; maskingWindowPositionMs = 0; } if (resetState) { - if (!timeline.isEmpty() || manifest != null) { - timeline = Timeline.EMPTY; - manifest = null; + if (!playbackInfo.timeline.isEmpty() || playbackInfo.manifest != null) { + playbackInfo = playbackInfo.copyWithTimeline(Timeline.EMPTY, null); for (Player.EventListener listener : listeners) { - listener.onTimelineChanged(timeline, manifest); + listener.onTimelineChanged(playbackInfo.timeline, playbackInfo.manifest); } } if (tracksSelected) { @@ -237,6 +232,7 @@ import java.util.concurrent.CopyOnWriteArraySet; @Override public void seekTo(int windowIndex, long positionMs) { + Timeline timeline = playbackInfo.timeline; if (windowIndex < 0 || (!timeline.isEmpty() && windowIndex >= timeline.getWindowCount())) { throw new IllegalSeekPositionException(timeline, windowIndex, positionMs); } @@ -334,24 +330,27 @@ import java.util.concurrent.CopyOnWriteArraySet; if (shouldMaskPosition()) { return maskingWindowIndex; } else { - return timeline.getPeriod(playbackInfo.periodId.periodIndex, period).windowIndex; + return playbackInfo.timeline.getPeriod(playbackInfo.periodId.periodIndex, period).windowIndex; } } @Override public int getNextWindowIndex() { + Timeline timeline = playbackInfo.timeline; return timeline.isEmpty() ? C.INDEX_UNSET : timeline.getNextWindowIndex(getCurrentWindowIndex(), repeatMode, shuffleModeEnabled); } @Override public int getPreviousWindowIndex() { + Timeline timeline = playbackInfo.timeline; return timeline.isEmpty() ? C.INDEX_UNSET : timeline.getPreviousWindowIndex(getCurrentWindowIndex(), repeatMode, shuffleModeEnabled); } @Override public long getDuration() { + Timeline timeline = playbackInfo.timeline; if (timeline.isEmpty()) { return C.TIME_UNSET; } @@ -386,9 +385,6 @@ import java.util.concurrent.CopyOnWriteArraySet; @Override public int getBufferedPercentage() { - if (timeline.isEmpty()) { - return 0; - } long position = getBufferedPosition(); long duration = getDuration(); return position == C.TIME_UNSET || duration == C.TIME_UNSET ? 0 @@ -397,11 +393,13 @@ import java.util.concurrent.CopyOnWriteArraySet; @Override public boolean isCurrentWindowDynamic() { + Timeline timeline = playbackInfo.timeline; return !timeline.isEmpty() && timeline.getWindow(getCurrentWindowIndex(), window).isDynamic; } @Override public boolean isCurrentWindowSeekable() { + Timeline timeline = playbackInfo.timeline; return !timeline.isEmpty() && timeline.getWindow(getCurrentWindowIndex(), window).isSeekable; } @@ -423,7 +421,7 @@ import java.util.concurrent.CopyOnWriteArraySet; @Override public long getContentPosition() { if (isPlayingAd()) { - timeline.getPeriod(playbackInfo.periodId.periodIndex, period); + playbackInfo.timeline.getPeriod(playbackInfo.periodId.periodIndex, period); return period.getPositionInWindowMs() + C.usToMs(playbackInfo.contentPositionUs); } else { return getCurrentPosition(); @@ -452,12 +450,12 @@ import java.util.concurrent.CopyOnWriteArraySet; @Override public Timeline getCurrentTimeline() { - return timeline; + return playbackInfo.timeline; } @Override public Object getCurrentManifest() { - return manifest; + return playbackInfo.manifest; } // Not private so it can be called from an inner class without going through a thunk method. @@ -533,12 +531,10 @@ import java.util.concurrent.CopyOnWriteArraySet; pendingPrepareAcks -= prepareAcks; pendingSeekAcks -= seekAcks; if (pendingPrepareAcks == 0 && pendingSeekAcks == 0) { + boolean timelineOrManifestChanged = this.playbackInfo.timeline != playbackInfo.timeline + || this.playbackInfo.manifest != playbackInfo.manifest; this.playbackInfo = playbackInfo; - boolean timelineOrManifestChanged = timeline != playbackInfo.timeline - || manifest != playbackInfo.manifest; - timeline = playbackInfo.timeline; - manifest = playbackInfo.manifest; - if (timeline.isEmpty()) { + if (playbackInfo.timeline.isEmpty()) { // Update the masking variables, which are used when the timeline is empty. maskingPeriodIndex = 0; maskingWindowIndex = 0; @@ -546,7 +542,7 @@ import java.util.concurrent.CopyOnWriteArraySet; } if (timelineOrManifestChanged) { for (Player.EventListener listener : listeners) { - listener.onTimelineChanged(timeline, manifest); + listener.onTimelineChanged(playbackInfo.timeline, playbackInfo.manifest); } } if (positionDiscontinuity) { @@ -567,14 +563,14 @@ import java.util.concurrent.CopyOnWriteArraySet; private long playbackInfoPositionUsToWindowPositionMs(long positionUs) { long positionMs = C.usToMs(positionUs); if (!playbackInfo.periodId.isAd()) { - timeline.getPeriod(playbackInfo.periodId.periodIndex, period); + playbackInfo.timeline.getPeriod(playbackInfo.periodId.periodIndex, period); positionMs += period.getPositionInWindowMs(); } return positionMs; } private boolean shouldMaskPosition() { - return timeline.isEmpty() || pendingSeekAcks > 0 || pendingPrepareAcks > 0; + return playbackInfo.timeline.isEmpty() || pendingSeekAcks > 0 || pendingPrepareAcks > 0; } } diff --git a/library/core/src/main/java/com/google/android/exoplayer2/ExoPlayerImplInternal.java b/library/core/src/main/java/com/google/android/exoplayer2/ExoPlayerImplInternal.java index fec3b3c65a..b79f5c70a3 100644 --- a/library/core/src/main/java/com/google/android/exoplayer2/ExoPlayerImplInternal.java +++ b/library/core/src/main/java/com/google/android/exoplayer2/ExoPlayerImplInternal.java @@ -48,67 +48,6 @@ import java.io.IOException; /* package */ final class ExoPlayerImplInternal implements Handler.Callback, MediaPeriod.Callback, TrackSelector.InvalidationListener, MediaSource.Listener { - /** - * Playback position information which is read on the application's thread by - * {@link ExoPlayerImpl} and read/written internally on the player's thread. - */ - public static final class PlaybackInfo { - - public final Timeline timeline; - public final Object manifest; - public final MediaPeriodId periodId; - public final long startPositionUs; - public final long contentPositionUs; - - public volatile long positionUs; - public volatile long bufferedPositionUs; - - public PlaybackInfo(Timeline timeline, Object manifest, int periodIndex, long startPositionUs) { - this(timeline, manifest, new MediaPeriodId(periodIndex), startPositionUs, C.TIME_UNSET); - } - - public PlaybackInfo(Timeline timeline, Object manifest, MediaPeriodId periodId, - long startPositionUs, long contentPositionUs) { - this.timeline = timeline; - this.manifest = manifest; - this.periodId = periodId; - this.startPositionUs = startPositionUs; - this.contentPositionUs = contentPositionUs; - positionUs = startPositionUs; - bufferedPositionUs = startPositionUs; - } - - public PlaybackInfo fromNewPosition(int periodIndex, long startPositionUs, - long contentPositionUs) { - return fromNewPosition(new MediaPeriodId(periodIndex), startPositionUs, contentPositionUs); - } - - public PlaybackInfo fromNewPosition(MediaPeriodId periodId, long startPositionUs, - long contentPositionUs) { - return new PlaybackInfo(timeline, manifest, periodId, startPositionUs, contentPositionUs); - } - - public PlaybackInfo copyWithPeriodIndex(int periodIndex) { - PlaybackInfo playbackInfo = new PlaybackInfo(timeline, manifest, - periodId.copyWithPeriodIndex(periodIndex), startPositionUs, contentPositionUs); - copyMutablePositions(this, playbackInfo); - return playbackInfo; - } - - public PlaybackInfo copyWithTimeline(Timeline timeline, Object manifest) { - PlaybackInfo playbackInfo = new PlaybackInfo(timeline, manifest, periodId, startPositionUs, - contentPositionUs); - copyMutablePositions(this, playbackInfo); - return playbackInfo; - } - - private static void copyMutablePositions(PlaybackInfo from, PlaybackInfo to) { - to.positionUs = from.positionUs; - to.bufferedPositionUs = from.bufferedPositionUs; - } - - } - private static final String TAG = "ExoPlayerImplInternal"; // External messages diff --git a/library/core/src/main/java/com/google/android/exoplayer2/MediaPeriodInfoSequence.java b/library/core/src/main/java/com/google/android/exoplayer2/MediaPeriodInfoSequence.java index 6fd0d48e57..6cb76e5471 100644 --- a/library/core/src/main/java/com/google/android/exoplayer2/MediaPeriodInfoSequence.java +++ b/library/core/src/main/java/com/google/android/exoplayer2/MediaPeriodInfoSequence.java @@ -16,7 +16,6 @@ package com.google.android.exoplayer2; import android.util.Pair; -import com.google.android.exoplayer2.ExoPlayerImplInternal.PlaybackInfo; import com.google.android.exoplayer2.Player.RepeatMode; import com.google.android.exoplayer2.source.MediaPeriod; import com.google.android.exoplayer2.source.MediaSource.MediaPeriodId; diff --git a/library/core/src/main/java/com/google/android/exoplayer2/PlaybackInfo.java b/library/core/src/main/java/com/google/android/exoplayer2/PlaybackInfo.java new file mode 100644 index 0000000000..a2ffa43c4b --- /dev/null +++ b/library/core/src/main/java/com/google/android/exoplayer2/PlaybackInfo.java @@ -0,0 +1,78 @@ +/* + * Copyright (C) 2017 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + package com.google.android.exoplayer2; + +import com.google.android.exoplayer2.source.MediaSource.MediaPeriodId; + +/** + * Information about an ongoing playback. + */ +/* package */ final class PlaybackInfo { + + public final Timeline timeline; + public final Object manifest; + public final MediaPeriodId periodId; + public final long startPositionUs; + public final long contentPositionUs; + + public volatile long positionUs; + public volatile long bufferedPositionUs; + + public PlaybackInfo(Timeline timeline, Object manifest, int periodIndex, long startPositionUs) { + this(timeline, manifest, new MediaPeriodId(periodIndex), startPositionUs, C.TIME_UNSET); + } + + public PlaybackInfo(Timeline timeline, Object manifest, MediaPeriodId periodId, + long startPositionUs, long contentPositionUs) { + this.timeline = timeline; + this.manifest = manifest; + this.periodId = periodId; + this.startPositionUs = startPositionUs; + this.contentPositionUs = contentPositionUs; + positionUs = startPositionUs; + bufferedPositionUs = startPositionUs; + } + + public PlaybackInfo fromNewPosition(int periodIndex, long startPositionUs, + long contentPositionUs) { + return fromNewPosition(new MediaPeriodId(periodIndex), startPositionUs, contentPositionUs); + } + + public PlaybackInfo fromNewPosition(MediaPeriodId periodId, long startPositionUs, + long contentPositionUs) { + return new PlaybackInfo(timeline, manifest, periodId, startPositionUs, contentPositionUs); + } + + public PlaybackInfo copyWithPeriodIndex(int periodIndex) { + PlaybackInfo playbackInfo = new PlaybackInfo(timeline, manifest, + periodId.copyWithPeriodIndex(periodIndex), startPositionUs, contentPositionUs); + copyMutablePositions(this, playbackInfo); + return playbackInfo; + } + + public PlaybackInfo copyWithTimeline(Timeline timeline, Object manifest) { + PlaybackInfo playbackInfo = new PlaybackInfo(timeline, manifest, periodId, startPositionUs, + contentPositionUs); + copyMutablePositions(this, playbackInfo); + return playbackInfo; + } + + private static void copyMutablePositions(PlaybackInfo from, PlaybackInfo to) { + to.positionUs = from.positionUs; + to.bufferedPositionUs = from.bufferedPositionUs; + } + +}