Commit 675756d3 by andrewlewis Committed by Oliver Woodman

Create MediaPeriods based on an identifier not an index

This will allow MediaSources to provide MediaPeriods that correspond to ad
breaks in a timeline period rather than content for a timeline period, in a
future change.

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=160267841
parent d4059ecc
...@@ -159,7 +159,8 @@ public final class ImaAdsMediaSource implements MediaSource { ...@@ -159,7 +159,8 @@ public final class ImaAdsMediaSource implements MediaSource {
} }
@Override @Override
public MediaPeriod createPeriod(int index, Allocator allocator) { public MediaPeriod createPeriod(MediaPeriodId id, Allocator allocator) {
int index = id.periodIndex;
if (timeline.isPeriodAd(index)) { if (timeline.isPeriodAd(index)) {
int adBreakIndex = timeline.getAdBreakIndex(index); int adBreakIndex = timeline.getAdBreakIndex(index);
int adIndexInAdBreak = timeline.getAdIndexInAdBreak(index); int adIndexInAdBreak = timeline.getAdIndexInAdBreak(index);
...@@ -174,13 +175,15 @@ public final class ImaAdsMediaSource implements MediaSource { ...@@ -174,13 +175,15 @@ public final class ImaAdsMediaSource implements MediaSource {
} }
MediaSource adBreakMediaSource = adBreakMediaSources[adBreakIndex][adIndexInAdBreak]; MediaSource adBreakMediaSource = adBreakMediaSources[adBreakIndex][adIndexInAdBreak];
MediaPeriod adBreakMediaPeriod = adBreakMediaSource.createPeriod(0, allocator); MediaPeriod adBreakMediaPeriod =
adBreakMediaSource.createPeriod(new MediaPeriodId(0), allocator);
mediaSourceByMediaPeriod.put(adBreakMediaPeriod, adBreakMediaSource); mediaSourceByMediaPeriod.put(adBreakMediaPeriod, adBreakMediaSource);
return adBreakMediaPeriod; return adBreakMediaPeriod;
} else { } else {
long startUs = timeline.getContentStartTimeUs(index); long startUs = timeline.getContentStartTimeUs(index);
long endUs = timeline.getContentEndTimeUs(index); long endUs = timeline.getContentEndTimeUs(index);
MediaPeriod contentMediaPeriod = contentMediaSource.createPeriod(0, allocator); MediaPeriod contentMediaPeriod =
contentMediaSource.createPeriod(new MediaPeriodId(0), allocator);
ClippingMediaPeriod clippingPeriod = new ClippingMediaPeriod(contentMediaPeriod, true); ClippingMediaPeriod clippingPeriod = new ClippingMediaPeriod(contentMediaPeriod, true);
clippingPeriod.setClipping(startUs, endUs == C.TIME_UNSET ? C.TIME_END_OF_SOURCE : endUs); clippingPeriod.setClipping(startUs, endUs == C.TIME_UNSET ? C.TIME_END_OF_SOURCE : endUs);
mediaSourceByMediaPeriod.put(contentMediaPeriod, contentMediaSource); mediaSourceByMediaPeriod.put(contentMediaPeriod, contentMediaSource);
...@@ -445,7 +448,7 @@ public final class ImaAdsMediaSource implements MediaSource { ...@@ -445,7 +448,7 @@ public final class ImaAdsMediaSource implements MediaSource {
} }
public void setMediaSource(MediaSource mediaSource) { public void setMediaSource(MediaSource mediaSource) {
mediaPeriod = mediaSource.createPeriod(index, allocator); mediaPeriod = mediaSource.createPeriod(new MediaPeriodId(index), allocator);
if (callback != null) { if (callback != null) {
mediaPeriod.prepare(this, positionUs); mediaPeriod.prepare(this, positionUs);
} }
......
...@@ -535,8 +535,8 @@ public final class ExoPlayerTest extends TestCase { ...@@ -535,8 +535,8 @@ public final class ExoPlayerTest extends TestCase {
} }
@Override @Override
public MediaPeriod createPeriod(int index, Allocator allocator) { public MediaPeriod createPeriod(MediaPeriodId id, Allocator allocator) {
Assertions.checkIndex(index, 0, timeline.getPeriodCount()); Assertions.checkIndex(id.periodIndex, 0, timeline.getPeriodCount());
assertTrue(preparedSource); assertTrue(preparedSource);
assertFalse(releasedSource); assertFalse(releasedSource);
FakeMediaPeriod mediaPeriod = new FakeMediaPeriod(trackGroupArray); FakeMediaPeriod mediaPeriod = new FakeMediaPeriod(trackGroupArray);
......
...@@ -98,7 +98,7 @@ public class TimelineTest extends TestCase { ...@@ -98,7 +98,7 @@ public class TimelineTest extends TestCase {
} }
@Override @Override
public MediaPeriod createPeriod(int index, Allocator allocator) { public MediaPeriod createPeriod(MediaPeriodId id, Allocator allocator) {
return null; return null;
} }
......
...@@ -26,6 +26,7 @@ import android.util.Pair; ...@@ -26,6 +26,7 @@ import android.util.Pair;
import com.google.android.exoplayer2.ExoPlayer.ExoPlayerMessage; import com.google.android.exoplayer2.ExoPlayer.ExoPlayerMessage;
import com.google.android.exoplayer2.source.MediaPeriod; import com.google.android.exoplayer2.source.MediaPeriod;
import com.google.android.exoplayer2.source.MediaSource; import com.google.android.exoplayer2.source.MediaSource;
import com.google.android.exoplayer2.source.MediaSource.MediaPeriodId;
import com.google.android.exoplayer2.source.SampleStream; import com.google.android.exoplayer2.source.SampleStream;
import com.google.android.exoplayer2.trackselection.TrackSelection; import com.google.android.exoplayer2.trackselection.TrackSelection;
import com.google.android.exoplayer2.trackselection.TrackSelectionArray; import com.google.android.exoplayer2.trackselection.TrackSelectionArray;
...@@ -1543,7 +1544,8 @@ import java.io.IOException; ...@@ -1543,7 +1544,8 @@ import java.io.IOException;
this.startPositionUs = startPositionUs; this.startPositionUs = startPositionUs;
sampleStreams = new SampleStream[renderers.length]; sampleStreams = new SampleStream[renderers.length];
mayRetainStreamFlags = new boolean[renderers.length]; mayRetainStreamFlags = new boolean[renderers.length];
mediaPeriod = mediaSource.createPeriod(periodIndex, loadControl.getAllocator()); mediaPeriod = mediaSource.createPeriod(new MediaPeriodId(periodIndex),
loadControl.getAllocator());
} }
public long toRendererTime(long periodTimeUs) { public long toRendererTime(long periodTimeUs) {
......
...@@ -91,9 +91,9 @@ public final class ClippingMediaSource implements MediaSource, MediaSource.Liste ...@@ -91,9 +91,9 @@ public final class ClippingMediaSource implements MediaSource, MediaSource.Liste
} }
@Override @Override
public MediaPeriod createPeriod(int index, Allocator allocator) { public MediaPeriod createPeriod(MediaPeriodId id, Allocator allocator) {
ClippingMediaPeriod mediaPeriod = new ClippingMediaPeriod( ClippingMediaPeriod mediaPeriod = new ClippingMediaPeriod(
mediaSource.createPeriod(index, allocator), enableInitialDiscontinuity); mediaSource.createPeriod(id, allocator), enableInitialDiscontinuity);
mediaPeriods.add(mediaPeriod); mediaPeriods.add(mediaPeriod);
mediaPeriod.setClipping(clippingTimeline.startUs, clippingTimeline.endUs); mediaPeriod.setClipping(clippingTimeline.startUs, clippingTimeline.endUs);
return mediaPeriod; return mediaPeriod;
......
...@@ -91,11 +91,11 @@ public final class ConcatenatingMediaSource implements MediaSource { ...@@ -91,11 +91,11 @@ public final class ConcatenatingMediaSource implements MediaSource {
} }
@Override @Override
public MediaPeriod createPeriod(int index, Allocator allocator) { public MediaPeriod createPeriod(MediaPeriodId id, Allocator allocator) {
int sourceIndex = timeline.getChildIndexByPeriodIndex(index); int sourceIndex = timeline.getChildIndexByPeriodIndex(id.periodIndex);
int periodIndexInSource = index - timeline.getFirstPeriodIndexInChild(sourceIndex); MediaPeriodId periodIdInSource =
MediaPeriod mediaPeriod = new MediaPeriodId(id.periodIndex - timeline.getFirstPeriodIndexInChild(sourceIndex));
mediaSources[sourceIndex].createPeriod(periodIndexInSource, allocator); MediaPeriod mediaPeriod = mediaSources[sourceIndex].createPeriod(periodIdInSource, allocator);
sourceIndexByMediaPeriod.put(mediaPeriod, sourceIndex); sourceIndexByMediaPeriod.put(mediaPeriod, sourceIndex);
return mediaPeriod; return mediaPeriod;
} }
......
...@@ -165,8 +165,8 @@ public final class ExtractorMediaSource implements MediaSource, MediaSource.List ...@@ -165,8 +165,8 @@ public final class ExtractorMediaSource implements MediaSource, MediaSource.List
} }
@Override @Override
public MediaPeriod createPeriod(int index, Allocator allocator) { public MediaPeriod createPeriod(MediaPeriodId id, Allocator allocator) {
Assertions.checkArgument(index == 0); Assertions.checkArgument(id.periodIndex == 0);
return new ExtractorMediaPeriod(uri, dataSourceFactory.createDataSource(), return new ExtractorMediaPeriod(uri, dataSourceFactory.createDataSource(),
extractorsFactory.createExtractors(), minLoadableRetryCount, eventHandler, eventListener, extractorsFactory.createExtractors(), minLoadableRetryCount, eventHandler, eventListener,
this, allocator, customCacheKey, continueLoadingCheckIntervalBytes); this, allocator, customCacheKey, continueLoadingCheckIntervalBytes);
......
...@@ -76,10 +76,10 @@ public final class LoopingMediaSource implements MediaSource { ...@@ -76,10 +76,10 @@ public final class LoopingMediaSource implements MediaSource {
} }
@Override @Override
public MediaPeriod createPeriod(int index, Allocator allocator) { public MediaPeriod createPeriod(MediaPeriodId id, Allocator allocator) {
return loopCount != Integer.MAX_VALUE return loopCount != Integer.MAX_VALUE
? childSource.createPeriod(index % childPeriodCount, allocator) ? childSource.createPeriod(new MediaPeriodId(id.periodIndex % childPeriodCount), allocator)
: childSource.createPeriod(index, allocator); : childSource.createPeriod(id, allocator);
} }
@Override @Override
......
...@@ -42,6 +42,27 @@ public interface MediaSource { ...@@ -42,6 +42,27 @@ public interface MediaSource {
} }
/** /**
* Identifier for a {@link MediaPeriod}.
*/
final class MediaPeriodId {
/**
* The timeline period index.
*/
public final int periodIndex;
/**
* Creates a media period identifier for the specified period in the timeline.
*
* @param periodIndex The timeline period index.
*/
public MediaPeriodId(int periodIndex) {
this.periodIndex = periodIndex;
}
}
/**
* Starts preparation of the source. * Starts preparation of the source.
* *
* @param player The player for which this source is being prepared. * @param player The player for which this source is being prepared.
...@@ -59,15 +80,15 @@ public interface MediaSource { ...@@ -59,15 +80,15 @@ public interface MediaSource {
void maybeThrowSourceInfoRefreshError() throws IOException; void maybeThrowSourceInfoRefreshError() throws IOException;
/** /**
* Returns a new {@link MediaPeriod} corresponding to the period at the specified {@code index}. * Returns a new {@link MediaPeriod} identified by {@code periodId}. This method may be called
* This method may be called multiple times with the same index without an intervening call to * multiple times with the same period identifier without an intervening call to
* {@link #releasePeriod(MediaPeriod)}. * {@link #releasePeriod(MediaPeriod)}.
* *
* @param index The index of the period. * @param id The identifier of the period.
* @param allocator An {@link Allocator} from which to obtain media buffer allocations. * @param allocator An {@link Allocator} from which to obtain media buffer allocations.
* @return A new {@link MediaPeriod}. * @return A new {@link MediaPeriod}.
*/ */
MediaPeriod createPeriod(int index, Allocator allocator); MediaPeriod createPeriod(MediaPeriodId id, Allocator allocator);
/** /**
* Releases the period. * Releases the period.
......
...@@ -116,10 +116,10 @@ public final class MergingMediaSource implements MediaSource { ...@@ -116,10 +116,10 @@ public final class MergingMediaSource implements MediaSource {
} }
@Override @Override
public MediaPeriod createPeriod(int index, Allocator allocator) { public MediaPeriod createPeriod(MediaPeriodId id, Allocator allocator) {
MediaPeriod[] periods = new MediaPeriod[mediaSources.length]; MediaPeriod[] periods = new MediaPeriod[mediaSources.length];
for (int i = 0; i < periods.length; i++) { for (int i = 0; i < periods.length; i++) {
periods[i] = mediaSources[i].createPeriod(index, allocator); periods[i] = mediaSources[i].createPeriod(id, allocator);
} }
return new MergingMediaPeriod(periods); return new MergingMediaPeriod(periods);
} }
......
...@@ -95,8 +95,8 @@ public final class SingleSampleMediaSource implements MediaSource { ...@@ -95,8 +95,8 @@ public final class SingleSampleMediaSource implements MediaSource {
} }
@Override @Override
public MediaPeriod createPeriod(int index, Allocator allocator) { public MediaPeriod createPeriod(MediaPeriodId id, Allocator allocator) {
Assertions.checkArgument(index == 0); Assertions.checkArgument(id.periodIndex == 0);
return new SingleSampleMediaPeriod(uri, dataSourceFactory, format, minLoadableRetryCount, return new SingleSampleMediaPeriod(uri, dataSourceFactory, format, minLoadableRetryCount,
eventHandler, eventListener, eventSourceId); eventHandler, eventListener, eventSourceId);
} }
......
...@@ -281,7 +281,8 @@ public final class DashMediaSource implements MediaSource { ...@@ -281,7 +281,8 @@ public final class DashMediaSource implements MediaSource {
} }
@Override @Override
public MediaPeriod createPeriod(int periodIndex, Allocator allocator) { public MediaPeriod createPeriod(MediaPeriodId periodId, Allocator allocator) {
int periodIndex = periodId.periodIndex;
EventDispatcher periodEventDispatcher = eventDispatcher.copyWithMediaTimeOffsetMs( EventDispatcher periodEventDispatcher = eventDispatcher.copyWithMediaTimeOffsetMs(
manifest.getPeriod(periodIndex).startMs); manifest.getPeriod(periodIndex).startMs);
DashMediaPeriod mediaPeriod = new DashMediaPeriod(firstPeriodId + periodIndex, manifest, DashMediaPeriod mediaPeriod = new DashMediaPeriod(firstPeriodId + periodIndex, manifest,
......
...@@ -88,8 +88,8 @@ public final class HlsMediaSource implements MediaSource, ...@@ -88,8 +88,8 @@ public final class HlsMediaSource implements MediaSource,
} }
@Override @Override
public MediaPeriod createPeriod(int index, Allocator allocator) { public MediaPeriod createPeriod(MediaPeriodId id, Allocator allocator) {
Assertions.checkArgument(index == 0); Assertions.checkArgument(id.periodIndex == 0);
return new HlsMediaPeriod(playlistTracker, dataSourceFactory, minLoadableRetryCount, return new HlsMediaPeriod(playlistTracker, dataSourceFactory, minLoadableRetryCount,
eventDispatcher, allocator); eventDispatcher, allocator);
} }
......
...@@ -222,8 +222,8 @@ public final class SsMediaSource implements MediaSource, ...@@ -222,8 +222,8 @@ public final class SsMediaSource implements MediaSource,
} }
@Override @Override
public MediaPeriod createPeriod(int index, Allocator allocator) { public MediaPeriod createPeriod(MediaPeriodId id, Allocator allocator) {
Assertions.checkArgument(index == 0); Assertions.checkArgument(id.periodIndex == 0);
SsMediaPeriod period = new SsMediaPeriod(manifest, chunkSourceFactory, minLoadableRetryCount, SsMediaPeriod period = new SsMediaPeriod(manifest, chunkSourceFactory, minLoadableRetryCount,
eventDispatcher, manifestLoaderErrorThrower, allocator); eventDispatcher, manifestLoaderErrorThrower, allocator);
mediaPeriods.add(period); mediaPeriods.add(period);
......
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