diff --git a/extensions/ima/src/main/java/com/google/android/exoplayer2/ext/ima/SinglePeriodAdTimeline.java b/extensions/ima/src/main/java/com/google/android/exoplayer2/ext/ima/SinglePeriodAdTimeline.java index c93f1e8f28..e3eef6a2b7 100644 --- a/extensions/ima/src/main/java/com/google/android/exoplayer2/ext/ima/SinglePeriodAdTimeline.java +++ b/extensions/ima/src/main/java/com/google/android/exoplayer2/ext/ima/SinglePeriodAdTimeline.java @@ -17,14 +17,14 @@ package com.google.android.exoplayer2.ext.ima; import com.google.android.exoplayer2.C; import com.google.android.exoplayer2.Timeline; +import com.google.android.exoplayer2.source.ForwardingTimeline; import com.google.android.exoplayer2.util.Assertions; /** * A {@link Timeline} for sources that have ads. */ -public final class SinglePeriodAdTimeline extends Timeline { +public final class SinglePeriodAdTimeline extends ForwardingTimeline { - private final Timeline contentTimeline; private final long[] adGroupTimesUs; private final int[] adCounts; private final int[] adsLoadedCounts; @@ -52,9 +52,9 @@ public final class SinglePeriodAdTimeline extends Timeline { public SinglePeriodAdTimeline(Timeline contentTimeline, long[] adGroupTimesUs, int[] adCounts, int[] adsLoadedCounts, int[] adsPlayedCounts, long[][] adDurationsUs, long adResumePositionUs) { + super(contentTimeline); Assertions.checkState(contentTimeline.getPeriodCount() == 1); Assertions.checkState(contentTimeline.getWindowCount() == 1); - this.contentTimeline = contentTimeline; this.adGroupTimesUs = adGroupTimesUs; this.adCounts = adCounts; this.adsLoadedCounts = adsLoadedCounts; @@ -63,34 +63,13 @@ public final class SinglePeriodAdTimeline extends Timeline { this.adResumePositionUs = adResumePositionUs; } - @Override - public int getWindowCount() { - return 1; - } - - @Override - public Window getWindow(int windowIndex, Window window, boolean setIds, - long defaultPositionProjectionUs) { - return contentTimeline.getWindow(windowIndex, window, setIds, defaultPositionProjectionUs); - } - - @Override - public int getPeriodCount() { - return 1; - } - @Override public Period getPeriod(int periodIndex, Period period, boolean setIds) { - contentTimeline.getPeriod(periodIndex, period, setIds); + timeline.getPeriod(periodIndex, period, setIds); period.set(period.id, period.uid, period.windowIndex, period.durationUs, period.getPositionInWindowUs(), adGroupTimesUs, adCounts, adsLoadedCounts, adsPlayedCounts, adDurationsUs, adResumePositionUs); return period; } - @Override - public int getIndexOfPeriod(Object uid) { - return contentTimeline.getIndexOfPeriod(uid); - } - } diff --git a/library/core/src/main/java/com/google/android/exoplayer2/source/ClippingMediaSource.java b/library/core/src/main/java/com/google/android/exoplayer2/source/ClippingMediaSource.java index 32c4eb6c73..4caafa3110 100644 --- a/library/core/src/main/java/com/google/android/exoplayer2/source/ClippingMediaSource.java +++ b/library/core/src/main/java/com/google/android/exoplayer2/source/ClippingMediaSource.java @@ -17,7 +17,6 @@ package com.google.android.exoplayer2.source; import com.google.android.exoplayer2.C; import com.google.android.exoplayer2.ExoPlayer; -import com.google.android.exoplayer2.Player.RepeatMode; import com.google.android.exoplayer2.Timeline; import com.google.android.exoplayer2.upstream.Allocator; import com.google.android.exoplayer2.util.Assertions; @@ -128,9 +127,8 @@ public final class ClippingMediaSource implements MediaSource, MediaSource.Liste /** * Provides a clipped view of a specified timeline. */ - private static final class ClippingTimeline extends Timeline { + private static final class ClippingTimeline extends ForwardingTimeline { - private final Timeline timeline; private final long startUs; private final long endUs; @@ -143,6 +141,7 @@ public final class ClippingMediaSource implements MediaSource, MediaSource.Liste * of {@code timeline}, or {@link C#TIME_END_OF_SOURCE} to clip no samples from the end. */ public ClippingTimeline(Timeline timeline, long startUs, long endUs) { + super(timeline); Assertions.checkArgument(timeline.getWindowCount() == 1); Assertions.checkArgument(timeline.getPeriodCount() == 1); Window window = timeline.getWindow(0, new Window(), false); @@ -155,26 +154,10 @@ public final class ClippingMediaSource implements MediaSource, MediaSource.Liste } Period period = timeline.getPeriod(0, new Period()); Assertions.checkArgument(period.getPositionInWindowUs() == 0); - this.timeline = timeline; this.startUs = startUs; this.endUs = resolvedEndUs; } - @Override - public int getWindowCount() { - return 1; - } - - @Override - public int getNextWindowIndex(int windowIndex, @RepeatMode int repeatMode) { - return timeline.getNextWindowIndex(windowIndex, repeatMode); - } - - @Override - public int getPreviousWindowIndex(int windowIndex, @RepeatMode int repeatMode) { - return timeline.getPreviousWindowIndex(windowIndex, repeatMode); - } - @Override public Window getWindow(int windowIndex, Window window, boolean setIds, long defaultPositionProjectionUs) { @@ -196,11 +179,6 @@ public final class ClippingMediaSource implements MediaSource, MediaSource.Liste return window; } - @Override - public int getPeriodCount() { - return 1; - } - @Override public Period getPeriod(int periodIndex, Period period, boolean setIds) { period = timeline.getPeriod(0, period, setIds); @@ -208,11 +186,6 @@ public final class ClippingMediaSource implements MediaSource, MediaSource.Liste return period; } - @Override - public int getIndexOfPeriod(Object uid) { - return timeline.getIndexOfPeriod(uid); - } - } } diff --git a/library/core/src/main/java/com/google/android/exoplayer2/source/ForwardingTimeline.java b/library/core/src/main/java/com/google/android/exoplayer2/source/ForwardingTimeline.java new file mode 100644 index 0000000000..4203abbf39 --- /dev/null +++ b/library/core/src/main/java/com/google/android/exoplayer2/source/ForwardingTimeline.java @@ -0,0 +1,68 @@ +/* + * 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.source; + +import com.google.android.exoplayer2.Player; +import com.google.android.exoplayer2.Timeline; + +/** + * An overridable {@link Timeline} implementation forwarding all methods to another timeline. + */ +public abstract class ForwardingTimeline extends Timeline { + + protected final Timeline timeline; + + public ForwardingTimeline(Timeline timeline) { + this.timeline = timeline; + } + + @Override + public int getWindowCount() { + return timeline.getWindowCount(); + } + + @Override + public int getNextWindowIndex(int windowIndex, @Player.RepeatMode int repeatMode) { + return timeline.getNextWindowIndex(windowIndex, repeatMode); + } + + @Override + public int getPreviousWindowIndex(int windowIndex, @Player.RepeatMode int repeatMode) { + return timeline.getPreviousWindowIndex(windowIndex, repeatMode); + } + + @Override + public Window getWindow(int windowIndex, Window window, boolean setIds, + long defaultPositionProjectionUs) { + return timeline.getWindow(windowIndex, window, setIds, defaultPositionProjectionUs); + } + + @Override + public int getPeriodCount() { + return timeline.getPeriodCount(); + } + + @Override + public Period getPeriod(int periodIndex, Period period, boolean setIds) { + return timeline.getPeriod(periodIndex, period, setIds); + } + + @Override + public int getIndexOfPeriod(Object uid) { + return timeline.getIndexOfPeriod(uid); + } + +} diff --git a/library/core/src/main/java/com/google/android/exoplayer2/source/LoopingMediaSource.java b/library/core/src/main/java/com/google/android/exoplayer2/source/LoopingMediaSource.java index a6e93a92b9..1795fe8045 100644 --- a/library/core/src/main/java/com/google/android/exoplayer2/source/LoopingMediaSource.java +++ b/library/core/src/main/java/com/google/android/exoplayer2/source/LoopingMediaSource.java @@ -160,53 +160,25 @@ public final class LoopingMediaSource implements MediaSource { } - private static final class InfinitelyLoopingTimeline extends Timeline { + private static final class InfinitelyLoopingTimeline extends ForwardingTimeline { - private final Timeline childTimeline; - - public InfinitelyLoopingTimeline(Timeline childTimeline) { - this.childTimeline = childTimeline; - } - - @Override - public int getWindowCount() { - return childTimeline.getWindowCount(); + public InfinitelyLoopingTimeline(Timeline timeline) { + super(timeline); } @Override public int getNextWindowIndex(int windowIndex, @Player.RepeatMode int repeatMode) { - int childNextWindowIndex = childTimeline.getNextWindowIndex(windowIndex, repeatMode); + int childNextWindowIndex = timeline.getNextWindowIndex(windowIndex, repeatMode); return childNextWindowIndex == C.INDEX_UNSET ? 0 : childNextWindowIndex; } @Override public int getPreviousWindowIndex(int windowIndex, @Player.RepeatMode int repeatMode) { - int childPreviousWindowIndex = childTimeline.getPreviousWindowIndex(windowIndex, repeatMode); + int childPreviousWindowIndex = timeline.getPreviousWindowIndex(windowIndex, repeatMode); return childPreviousWindowIndex == C.INDEX_UNSET ? getWindowCount() - 1 : childPreviousWindowIndex; } - @Override - public Window getWindow(int windowIndex, Window window, boolean setIds, - long defaultPositionProjectionUs) { - return childTimeline.getWindow(windowIndex, window, setIds, defaultPositionProjectionUs); - } - - @Override - public int getPeriodCount() { - return childTimeline.getPeriodCount(); - } - - @Override - public Period getPeriod(int periodIndex, Period period, boolean setIds) { - return childTimeline.getPeriod(periodIndex, period, setIds); - } - - @Override - public int getIndexOfPeriod(Object uid) { - return childTimeline.getIndexOfPeriod(uid); - } - } }