Commit a8b851ce by tonihei Committed by Oliver Woodman

Add BasePlayer to avoid code duplication for common convenience methods.

A lot of methods just forward to other methods and there is no conceivable
way a player should implement it another way. Moving these methods to a
base player class allows to remove duplicated code across our player
implementations.

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=215374192
parent 8b2d9984
......@@ -18,6 +18,7 @@ package com.google.android.exoplayer2.ext.cast;
import android.os.Looper;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import com.google.android.exoplayer2.BasePlayer;
import com.google.android.exoplayer2.C;
import com.google.android.exoplayer2.ExoPlaybackException;
import com.google.android.exoplayer2.PlaybackParameters;
......@@ -31,7 +32,6 @@ import com.google.android.exoplayer2.trackselection.TrackSelectionArray;
import com.google.android.exoplayer2.util.Assertions;
import com.google.android.exoplayer2.util.Log;
import com.google.android.exoplayer2.util.MimeTypes;
import com.google.android.exoplayer2.util.Util;
import com.google.android.gms.cast.CastStatusCodes;
import com.google.android.gms.cast.MediaInfo;
import com.google.android.gms.cast.MediaQueueItem;
......@@ -62,7 +62,7 @@ import java.util.concurrent.CopyOnWriteArraySet;
*
* <p>Methods should be called on the application's main thread.
*/
public final class CastPlayer implements Player {
public final class CastPlayer extends BasePlayer {
private static final String TAG = "CastPlayer";
......@@ -78,7 +78,6 @@ public final class CastPlayer implements Player {
private final CastContext castContext;
// TODO: Allow custom implementations of CastTimelineTracker.
private final CastTimelineTracker timelineTracker;
private final Timeline.Window window;
private final Timeline.Period period;
private RemoteMediaClient remoteMediaClient;
......@@ -111,7 +110,6 @@ public final class CastPlayer implements Player {
public CastPlayer(CastContext castContext) {
this.castContext = castContext;
timelineTracker = new CastTimelineTracker();
window = new Timeline.Window();
period = new Timeline.Period();
statusListener = new StatusListener();
seekResultCallback = new SeekResultCallback();
......@@ -325,21 +323,6 @@ public final class CastPlayer implements Player {
}
@Override
public void seekToDefaultPosition() {
seekTo(0);
}
@Override
public void seekToDefaultPosition(int windowIndex) {
seekTo(windowIndex, 0);
}
@Override
public void seekTo(long positionMs) {
seekTo(getCurrentWindowIndex(), positionMs);
}
@Override
public void seekTo(int windowIndex, long positionMs) {
MediaStatus mediaStatus = getMediaStatus();
// We assume the default position is 0. There is no support for seeking to the default position
......@@ -366,32 +349,6 @@ public final class CastPlayer implements Player {
}
@Override
public boolean hasPrevious() {
return getPreviousWindowIndex() != C.INDEX_UNSET;
}
@Override
public void previous() {
int previousWindowIndex = getPreviousWindowIndex();
if (previousWindowIndex != C.INDEX_UNSET) {
seekToDefaultPosition(previousWindowIndex);
}
}
@Override
public boolean hasNext() {
return getNextWindowIndex() != C.INDEX_UNSET;
}
@Override
public void next() {
int nextWindowIndex = getPreviousWindowIndex();
if (nextWindowIndex != C.INDEX_UNSET) {
seekToDefaultPosition(nextWindowIndex);
}
}
@Override
public void setPlaybackParameters(@Nullable PlaybackParameters playbackParameters) {
// Unsupported by the RemoteMediaClient API. Do nothing.
}
......@@ -402,11 +359,6 @@ public final class CastPlayer implements Player {
}
@Override
public void stop() {
stop(/* reset= */ false);
}
@Override
public void stop(boolean reset) {
playbackState = STATE_IDLE;
if (remoteMediaClient != null) {
......@@ -495,32 +447,11 @@ public final class CastPlayer implements Player {
return pendingSeekWindowIndex != C.INDEX_UNSET ? pendingSeekWindowIndex : currentWindowIndex;
}
@Override
public int getNextWindowIndex() {
return currentTimeline.isEmpty() ? C.INDEX_UNSET
: currentTimeline.getNextWindowIndex(getCurrentWindowIndex(), repeatMode, false);
}
@Override
public int getPreviousWindowIndex() {
return currentTimeline.isEmpty() ? C.INDEX_UNSET
: currentTimeline.getPreviousWindowIndex(getCurrentWindowIndex(), repeatMode, false);
}
@Override
public @Nullable Object getCurrentTag() {
int windowIndex = getCurrentWindowIndex();
return windowIndex >= currentTimeline.getWindowCount()
? null
: currentTimeline.getWindow(windowIndex, window, /* setTag= */ true).tag;
}
// TODO: Fill the cast timeline information with ProgressListener's duration updates.
// See [Internal: b/65152553].
@Override
public long getDuration() {
return currentTimeline.isEmpty() ? C.TIME_UNSET
: currentTimeline.getWindow(getCurrentWindowIndex(), window).getDurationMs();
return getContentDuration();
}
@Override
......@@ -538,15 +469,6 @@ public final class CastPlayer implements Player {
}
@Override
public int getBufferedPercentage() {
long position = getBufferedPosition();
long duration = getDuration();
return position == C.TIME_UNSET || duration == C.TIME_UNSET
? 0
: duration == 0 ? 100 : Util.constrainValue((int) ((position * 100) / duration), 0, 100);
}
@Override
public long getTotalBufferedDuration() {
long bufferedPosition = getBufferedPosition();
long currentPosition = getCurrentPosition();
......@@ -556,18 +478,6 @@ public final class CastPlayer implements Player {
}
@Override
public boolean isCurrentWindowDynamic() {
return !currentTimeline.isEmpty()
&& currentTimeline.getWindow(getCurrentWindowIndex(), window).isDynamic;
}
@Override
public boolean isCurrentWindowSeekable() {
return !currentTimeline.isEmpty()
&& currentTimeline.getWindow(getCurrentWindowIndex(), window).isSeekable;
}
@Override
public boolean isPlayingAd() {
return false;
}
......@@ -583,11 +493,6 @@ public final class CastPlayer implements Player {
}
@Override
public long getContentDuration() {
return getDuration();
}
@Override
public boolean isLoading() {
return false;
}
......
......@@ -26,7 +26,6 @@ import java.util.ArrayList;
/* package */ final class FakePlayer extends StubExoPlayer {
private final ArrayList<Player.EventListener> listeners;
private final Timeline.Window window;
private final Timeline.Period period;
private final Timeline timeline;
......@@ -41,7 +40,6 @@ import java.util.ArrayList;
public FakePlayer() {
listeners = new ArrayList<>();
window = new Timeline.Window();
period = new Timeline.Period();
state = Player.STATE_IDLE;
playWhenReady = true;
......@@ -152,16 +150,6 @@ import java.util.ArrayList;
}
@Override
public int getNextWindowIndex() {
return C.INDEX_UNSET;
}
@Override
public int getPreviousWindowIndex() {
return C.INDEX_UNSET;
}
@Override
public long getDuration() {
if (timeline.isEmpty()) {
return C.INDEX_UNSET;
......
/*
* Copyright (C) 2018 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 android.support.annotation.Nullable;
import com.google.android.exoplayer2.util.Util;
/** Abstract base {@link Player} which implements common implementation independent methods. */
public abstract class BasePlayer implements Player {
protected final Timeline.Window window;
public BasePlayer() {
window = new Timeline.Window();
}
@Override
public final void seekToDefaultPosition() {
seekToDefaultPosition(getCurrentWindowIndex());
}
@Override
public final void seekToDefaultPosition(int windowIndex) {
seekTo(windowIndex, /* positionMs= */ C.TIME_UNSET);
}
@Override
public final void seekTo(long positionMs) {
seekTo(getCurrentWindowIndex(), positionMs);
}
@Override
public final boolean hasPrevious() {
return getPreviousWindowIndex() != C.INDEX_UNSET;
}
@Override
public final void previous() {
int previousWindowIndex = getPreviousWindowIndex();
if (previousWindowIndex != C.INDEX_UNSET) {
seekToDefaultPosition(previousWindowIndex);
}
}
@Override
public final boolean hasNext() {
return getNextWindowIndex() != C.INDEX_UNSET;
}
@Override
public final void next() {
int nextWindowIndex = getPreviousWindowIndex();
if (nextWindowIndex != C.INDEX_UNSET) {
seekToDefaultPosition(nextWindowIndex);
}
}
@Override
public final void stop() {
stop(/* reset= */ false);
}
@Override
public final int getNextWindowIndex() {
Timeline timeline = getCurrentTimeline();
return timeline.isEmpty()
? C.INDEX_UNSET
: timeline.getNextWindowIndex(
getCurrentWindowIndex(), getRepeatMode(), getShuffleModeEnabled());
}
@Override
public final int getPreviousWindowIndex() {
Timeline timeline = getCurrentTimeline();
return timeline.isEmpty()
? C.INDEX_UNSET
: timeline.getPreviousWindowIndex(
getCurrentWindowIndex(), getRepeatMode(), getShuffleModeEnabled());
}
@Override
@Nullable
public final Object getCurrentTag() {
int windowIndex = getCurrentWindowIndex();
Timeline timeline = getCurrentTimeline();
return windowIndex >= timeline.getWindowCount()
? null
: timeline.getWindow(windowIndex, window, /* setTag= */ true).tag;
}
@Override
public final int getBufferedPercentage() {
long position = getBufferedPosition();
long duration = getDuration();
return position == C.TIME_UNSET || duration == C.TIME_UNSET
? 0
: duration == 0 ? 100 : Util.constrainValue((int) ((position * 100) / duration), 0, 100);
}
@Override
public final boolean isCurrentWindowDynamic() {
Timeline timeline = getCurrentTimeline();
return !timeline.isEmpty() && timeline.getWindow(getCurrentWindowIndex(), window).isDynamic;
}
@Override
public final boolean isCurrentWindowSeekable() {
Timeline timeline = getCurrentTimeline();
return !timeline.isEmpty() && timeline.getWindow(getCurrentWindowIndex(), window).isSeekable;
}
@Override
public final long getContentDuration() {
Timeline timeline = getCurrentTimeline();
return timeline.isEmpty()
? C.TIME_UNSET
: timeline.getWindow(getCurrentWindowIndex(), window).getDurationMs();
}
}
......@@ -40,10 +40,8 @@ import java.util.List;
import java.util.Set;
import java.util.concurrent.CopyOnWriteArraySet;
/**
* An {@link ExoPlayer} implementation. Instances can be obtained from {@link ExoPlayerFactory}.
*/
/* package */ final class ExoPlayerImpl implements ExoPlayer {
/** An {@link ExoPlayer} implementation. Instances can be obtained from {@link ExoPlayerFactory}. */
/* package */ final class ExoPlayerImpl extends BasePlayer implements ExoPlayer {
private static final String TAG = "ExoPlayerImpl";
......@@ -62,7 +60,6 @@ import java.util.concurrent.CopyOnWriteArraySet;
private final ExoPlayerImplInternal internalPlayer;
private final Handler internalPlayerHandler;
private final CopyOnWriteArraySet<Player.EventListener> listeners;
private final Timeline.Window window;
private final Timeline.Period period;
private final ArrayDeque<PlaybackInfoUpdate> pendingPlaybackInfoUpdates;
......@@ -119,7 +116,6 @@ import java.util.concurrent.CopyOnWriteArraySet;
new RendererConfiguration[renderers.length],
new TrackSelection[renderers.length],
null);
window = new Timeline.Window();
period = new Timeline.Period();
playbackParameters = PlaybackParameters.DEFAULT;
seekParameters = SeekParameters.DEFAULT;
......@@ -295,21 +291,6 @@ import java.util.concurrent.CopyOnWriteArraySet;
}
@Override
public void seekToDefaultPosition() {
seekToDefaultPosition(getCurrentWindowIndex());
}
@Override
public void seekToDefaultPosition(int windowIndex) {
seekTo(windowIndex, C.TIME_UNSET);
}
@Override
public void seekTo(long positionMs) {
seekTo(getCurrentWindowIndex(), positionMs);
}
@Override
public void seekTo(int windowIndex, long positionMs) {
Timeline timeline = playbackInfo.timeline;
if (windowIndex < 0 || (!timeline.isEmpty() && windowIndex >= timeline.getWindowCount())) {
......@@ -350,32 +331,6 @@ import java.util.concurrent.CopyOnWriteArraySet;
}
@Override
public boolean hasPrevious() {
return getPreviousWindowIndex() != C.INDEX_UNSET;
}
@Override
public void previous() {
int previousWindowIndex = getPreviousWindowIndex();
if (previousWindowIndex != C.INDEX_UNSET) {
seekToDefaultPosition(previousWindowIndex);
}
}
@Override
public boolean hasNext() {
return getNextWindowIndex() != C.INDEX_UNSET;
}
@Override
public void next() {
int nextWindowIndex = getPreviousWindowIndex();
if (nextWindowIndex != C.INDEX_UNSET) {
seekToDefaultPosition(nextWindowIndex);
}
}
@Override
public void setPlaybackParameters(@Nullable PlaybackParameters playbackParameters) {
if (playbackParameters == null) {
playbackParameters = PlaybackParameters.DEFAULT;
......@@ -405,19 +360,6 @@ import java.util.concurrent.CopyOnWriteArraySet;
}
@Override
public @Nullable Object getCurrentTag() {
int windowIndex = getCurrentWindowIndex();
return windowIndex >= playbackInfo.timeline.getWindowCount()
? null
: playbackInfo.timeline.getWindow(windowIndex, window, /* setTag= */ true).tag;
}
@Override
public void stop() {
stop(/* reset= */ false);
}
@Override
public void stop(boolean reset) {
if (reset) {
playbackError = null;
......@@ -522,20 +464,6 @@ import java.util.concurrent.CopyOnWriteArraySet;
}
@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() {
if (isPlayingAd()) {
MediaPeriodId periodId = playbackInfo.periodId;
......@@ -568,32 +496,11 @@ import java.util.concurrent.CopyOnWriteArraySet;
}
@Override
public int getBufferedPercentage() {
long position = getBufferedPosition();
long duration = getDuration();
return position == C.TIME_UNSET || duration == C.TIME_UNSET
? 0
: (duration == 0 ? 100 : Util.constrainValue((int) ((position * 100) / duration), 0, 100));
}
@Override
public long getTotalBufferedDuration() {
return Math.max(0, C.usToMs(playbackInfo.totalBufferedDurationUs));
}
@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;
}
@Override
public boolean isPlayingAd() {
return !shouldMaskPosition() && playbackInfo.periodId.isAd();
}
......@@ -609,13 +516,6 @@ import java.util.concurrent.CopyOnWriteArraySet;
}
@Override
public long getContentDuration() {
return playbackInfo.timeline.isEmpty()
? C.TIME_UNSET
: playbackInfo.timeline.getWindow(getCurrentWindowIndex(), window).getDurationMs();
}
@Override
public long getContentPosition() {
if (isPlayingAd()) {
playbackInfo.timeline.getPeriodByUid(playbackInfo.periodId.periodUid, period);
......
......@@ -64,7 +64,7 @@ import java.util.concurrent.CopyOnWriteArraySet;
* be obtained from {@link ExoPlayerFactory}.
*/
@TargetApi(16)
public class SimpleExoPlayer
public class SimpleExoPlayer extends BasePlayer
implements ExoPlayer, Player.AudioComponent, Player.VideoComponent, Player.TextComponent {
/** @deprecated Use {@link com.google.android.exoplayer2.video.VideoListener}. */
......@@ -928,27 +928,6 @@ public class SimpleExoPlayer
}
@Override
public void seekToDefaultPosition() {
verifyApplicationThread();
analyticsCollector.notifySeekStarted();
player.seekToDefaultPosition();
}
@Override
public void seekToDefaultPosition(int windowIndex) {
verifyApplicationThread();
analyticsCollector.notifySeekStarted();
player.seekToDefaultPosition(windowIndex);
}
@Override
public void seekTo(long positionMs) {
verifyApplicationThread();
analyticsCollector.notifySeekStarted();
player.seekTo(positionMs);
}
@Override
public void seekTo(int windowIndex, long positionMs) {
verifyApplicationThread();
analyticsCollector.notifySeekStarted();
......@@ -956,36 +935,6 @@ public class SimpleExoPlayer
}
@Override
public boolean hasPrevious() {
verifyApplicationThread();
return getPreviousWindowIndex() != C.INDEX_UNSET;
}
@Override
public void previous() {
verifyApplicationThread();
if (hasPrevious()) {
analyticsCollector.notifySeekStarted();
player.previous();
}
}
@Override
public boolean hasNext() {
verifyApplicationThread();
return getNextWindowIndex() != C.INDEX_UNSET;
}
@Override
public void next() {
verifyApplicationThread();
if (hasNext()) {
analyticsCollector.notifySeekStarted();
player.next();
}
}
@Override
public void setPlaybackParameters(@Nullable PlaybackParameters playbackParameters) {
verifyApplicationThread();
player.setPlaybackParameters(playbackParameters);
......@@ -1010,17 +959,6 @@ public class SimpleExoPlayer
}
@Override
public @Nullable Object getCurrentTag() {
verifyApplicationThread();
return player.getCurrentTag();
}
@Override
public void stop() {
stop(/* reset= */ false);
}
@Override
public void stop(boolean reset) {
verifyApplicationThread();
player.stop(reset);
......@@ -1123,18 +1061,6 @@ public class SimpleExoPlayer
}
@Override
public int getNextWindowIndex() {
verifyApplicationThread();
return player.getNextWindowIndex();
}
@Override
public int getPreviousWindowIndex() {
verifyApplicationThread();
return player.getPreviousWindowIndex();
}
@Override
public long getDuration() {
verifyApplicationThread();
return player.getDuration();
......@@ -1153,30 +1079,12 @@ public class SimpleExoPlayer
}
@Override
public int getBufferedPercentage() {
verifyApplicationThread();
return player.getBufferedPercentage();
}
@Override
public long getTotalBufferedDuration() {
verifyApplicationThread();
return player.getTotalBufferedDuration();
}
@Override
public boolean isCurrentWindowDynamic() {
verifyApplicationThread();
return player.isCurrentWindowDynamic();
}
@Override
public boolean isCurrentWindowSeekable() {
verifyApplicationThread();
return player.isCurrentWindowSeekable();
}
@Override
public boolean isPlayingAd() {
verifyApplicationThread();
return player.isPlayingAd();
......@@ -1195,12 +1103,6 @@ public class SimpleExoPlayer
}
@Override
public long getContentDuration() {
verifyApplicationThread();
return player.getContentDuration();
}
@Override
public long getContentPosition() {
verifyApplicationThread();
return player.getContentPosition();
......
......@@ -16,7 +16,7 @@
package com.google.android.exoplayer2.testutil;
import android.os.Looper;
import android.support.annotation.Nullable;
import com.google.android.exoplayer2.BasePlayer;
import com.google.android.exoplayer2.ExoPlaybackException;
import com.google.android.exoplayer2.ExoPlayer;
import com.google.android.exoplayer2.PlaybackParameters;
......@@ -32,7 +32,7 @@ import com.google.android.exoplayer2.trackselection.TrackSelectionArray;
* An abstract {@link ExoPlayer} implementation that throws {@link UnsupportedOperationException}
* from every method.
*/
public abstract class StubExoPlayer implements ExoPlayer {
public abstract class StubExoPlayer extends BasePlayer implements ExoPlayer {
@Override
public AudioComponent getAudioComponent() {
......@@ -130,46 +130,11 @@ public abstract class StubExoPlayer implements ExoPlayer {
}
@Override
public void seekToDefaultPosition() {
throw new UnsupportedOperationException();
}
@Override
public void seekToDefaultPosition(int windowIndex) {
throw new UnsupportedOperationException();
}
@Override
public void seekTo(long positionMs) {
throw new UnsupportedOperationException();
}
@Override
public void seekTo(int windowIndex, long positionMs) {
throw new UnsupportedOperationException();
}
@Override
public boolean hasPrevious() {
throw new UnsupportedOperationException();
}
@Override
public void previous() {
throw new UnsupportedOperationException();
}
@Override
public boolean hasNext() {
throw new UnsupportedOperationException();
}
@Override
public void next() {
throw new UnsupportedOperationException();
}
@Override
public void setPlaybackParameters(PlaybackParameters playbackParameters) {
throw new UnsupportedOperationException();
}
......@@ -190,16 +155,6 @@ public abstract class StubExoPlayer implements ExoPlayer {
}
@Override
public @Nullable Object getCurrentTag() {
throw new UnsupportedOperationException();
}
@Override
public void stop() {
throw new UnsupportedOperationException();
}
@Override
public void stop(boolean resetStateAndPosition) {
throw new UnsupportedOperationException();
}
......@@ -269,16 +224,6 @@ public abstract class StubExoPlayer implements ExoPlayer {
}
@Override
public int getNextWindowIndex() {
throw new UnsupportedOperationException();
}
@Override
public int getPreviousWindowIndex() {
throw new UnsupportedOperationException();
}
@Override
public long getDuration() {
throw new UnsupportedOperationException();
}
......@@ -294,26 +239,11 @@ public abstract class StubExoPlayer implements ExoPlayer {
}
@Override
public int getBufferedPercentage() {
throw new UnsupportedOperationException();
}
@Override
public long getTotalBufferedDuration() {
throw new UnsupportedOperationException();
}
@Override
public boolean isCurrentWindowDynamic() {
throw new UnsupportedOperationException();
}
@Override
public boolean isCurrentWindowSeekable() {
throw new UnsupportedOperationException();
}
@Override
public boolean isPlayingAd() {
throw new UnsupportedOperationException();
}
......@@ -329,11 +259,6 @@ public abstract class StubExoPlayer implements ExoPlayer {
}
@Override
public long getContentDuration() {
throw new UnsupportedOperationException();
}
@Override
public long getContentPosition() {
throw new UnsupportedOperationException();
}
......
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