Skip to content

Commit

Permalink
Move media period end position from MediaperiodInfo to MediaPeriodId.
Browse files Browse the repository at this point in the history
The end position is needed to make the MediaPeriodId unique as multiple
content periods may only vary in this parameter.

This also simplfies some other comparisons where the end position needed to
be compared in addition to the media period id.

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=205634508
  • Loading branch information
tonihei authored and ojw28 committed Jul 23, 2018
1 parent 41345dc commit 0b3fa51
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 50 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -676,7 +676,7 @@ private long seekToPeriodPosition(
MediaPeriodHolder oldPlayingPeriodHolder = queue.getPlayingPeriod();
MediaPeriodHolder newPlayingPeriodHolder = oldPlayingPeriodHolder;
while (newPlayingPeriodHolder != null) {
if (shouldKeepPeriodHolder(periodId, periodPositionUs, newPlayingPeriodHolder)) {
if (periodId.equals(newPlayingPeriodHolder.info.id) && newPlayingPeriodHolder.prepared) {
queue.removeAfter(newPlayingPeriodHolder);
break;
}
Expand Down Expand Up @@ -712,19 +712,6 @@ private long seekToPeriodPosition(
return periodPositionUs;
}

private boolean shouldKeepPeriodHolder(
MediaPeriodId seekPeriodId, long positionUs, MediaPeriodHolder holder) {
if (seekPeriodId.equals(holder.info.id) && holder.prepared) {
playbackInfo.timeline.getPeriod(holder.info.id.periodIndex, period);
int nextAdGroupIndex = period.getAdGroupIndexAfterPositionUs(positionUs);
if (nextAdGroupIndex == C.INDEX_UNSET
|| period.getAdGroupTimeUs(nextAdGroupIndex) == holder.info.endPositionUs) {
return true;
}
}
return false;
}

private void resetRendererPosition(long periodPositionUs) throws ExoPlaybackException {
rendererPositionUs =
!queue.hasPlayingPeriod()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,13 +82,13 @@ public MediaPeriodHolder(
sampleStreams = new SampleStream[rendererCapabilities.length];
mayRetainStreamFlags = new boolean[rendererCapabilities.length];
MediaPeriod mediaPeriod = mediaSource.createPeriod(info.id, allocator);
if (info.endPositionUs != C.TIME_END_OF_SOURCE) {
if (info.id.endPositionUs != C.TIME_END_OF_SOURCE) {
mediaPeriod =
new ClippingMediaPeriod(
mediaPeriod,
/* enableInitialDiscontinuity= */ true,
/* startUs= */ 0,
info.endPositionUs);
info.id.endPositionUs);
}
this.mediaPeriod = mediaPeriod;
}
Expand Down Expand Up @@ -219,7 +219,7 @@ public long applyTrackSelection(
public void release() {
updatePeriodTrackSelectorResult(null);
try {
if (info.endPositionUs != C.TIME_END_OF_SOURCE) {
if (info.id.endPositionUs != C.TIME_END_OF_SOURCE) {
mediaSource.releasePeriod(((ClippingMediaPeriod) mediaPeriod).mediaPeriod);
} else {
mediaSource.releasePeriod(mediaPeriod);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,13 @@
public final MediaPeriodId id;
/** The start position of the media to play within the media period, in microseconds. */
public final long startPositionUs;
/**
* The end position of the media to play within the media period, in microseconds, or {@link
* C#TIME_END_OF_SOURCE} if the end position is the end of the media period.
*/
public final long endPositionUs;
/**
* If this is an ad, the position to play in the next content media period. {@link C#TIME_UNSET}
* otherwise.
*/
public final long contentPositionUs;
/**
* The duration of the media period, like {@link #endPositionUs} but with {@link
* The duration of the media period, like {@link MediaPeriodId#endPositionUs} but with {@link
* C#TIME_END_OF_SOURCE} resolved to the timeline period duration. May be {@link C#TIME_UNSET} if
* the end position is not known.
*/
Expand All @@ -55,14 +50,12 @@
MediaPeriodInfo(
MediaPeriodId id,
long startPositionUs,
long endPositionUs,
long contentPositionUs,
long durationUs,
boolean isLastInTimelinePeriod,
boolean isFinal) {
this.id = id;
this.startPositionUs = startPositionUs;
this.endPositionUs = endPositionUs;
this.contentPositionUs = contentPositionUs;
this.durationUs = durationUs;
this.isLastInTimelinePeriod = isLastInTimelinePeriod;
Expand All @@ -77,7 +70,6 @@ public MediaPeriodInfo copyWithPeriodIndex(int periodIndex) {
return new MediaPeriodInfo(
id.copyWithPeriodIndex(periodIndex),
startPositionUs,
endPositionUs,
contentPositionUs,
durationUs,
isLastInTimelinePeriod,
Expand All @@ -89,7 +81,6 @@ public MediaPeriodInfo copyWithStartPositionUs(long startPositionUs) {
return new MediaPeriodInfo(
id,
startPositionUs,
endPositionUs,
contentPositionUs,
durationUs,
isLastInTimelinePeriod,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -391,7 +391,12 @@ private MediaPeriodId resolveMediaPeriodIdForAds(
timeline.getPeriod(periodIndex, period);
int adGroupIndex = period.getAdGroupIndexForPositionUs(positionUs);
if (adGroupIndex == C.INDEX_UNSET) {
return new MediaPeriodId(periodIndex, windowSequenceNumber);
int nextAdGroupIndex = period.getAdGroupIndexAfterPositionUs(positionUs);
long endPositionUs =
nextAdGroupIndex == C.INDEX_UNSET
? C.TIME_END_OF_SOURCE
: period.getAdGroupTimeUs(nextAdGroupIndex);
return new MediaPeriodId(periodIndex, windowSequenceNumber, endPositionUs);
} else {
int adIndexInAdGroup = period.getFirstAdIndexToPlay(adGroupIndex);
return new MediaPeriodId(periodIndex, adGroupIndex, adIndexInAdGroup, windowSequenceNumber);
Expand Down Expand Up @@ -450,7 +455,6 @@ private long resolvePeriodIndexToWindowSequenceNumber(int periodIndex) {
private boolean canKeepMediaPeriodHolder(MediaPeriodHolder periodHolder, MediaPeriodInfo info) {
MediaPeriodInfo periodHolderInfo = periodHolder.info;
return periodHolderInfo.startPositionUs == info.startPositionUs
&& periodHolderInfo.endPositionUs == info.endPositionUs
&& periodHolderInfo.id.equals(info.id);
}

Expand Down Expand Up @@ -593,14 +597,14 @@ private MediaPeriodInfo getFirstMediaPeriodInfo(PlaybackInfo playbackInfo) {
mediaPeriodInfo.contentPositionUs,
currentPeriodId.windowSequenceNumber);
}
} else if (mediaPeriodInfo.endPositionUs != C.TIME_END_OF_SOURCE) {
} else if (mediaPeriodInfo.id.endPositionUs != C.TIME_END_OF_SOURCE) {
// Play the next ad group if it's available.
int nextAdGroupIndex = period.getAdGroupIndexForPositionUs(mediaPeriodInfo.endPositionUs);
int nextAdGroupIndex = period.getAdGroupIndexForPositionUs(mediaPeriodInfo.id.endPositionUs);
if (nextAdGroupIndex == C.INDEX_UNSET) {
// The next ad group can't be played. Play content from the ad group position instead.
return getMediaPeriodInfoForContent(
currentPeriodId.periodIndex,
mediaPeriodInfo.endPositionUs,
mediaPeriodInfo.id.endPositionUs,
currentPeriodId.windowSequenceNumber);
}
int adIndexInAdGroup = period.getFirstAdIndexToPlay(nextAdGroupIndex);
Expand All @@ -610,7 +614,7 @@ private MediaPeriodInfo getFirstMediaPeriodInfo(PlaybackInfo playbackInfo) {
currentPeriodId.periodIndex,
nextAdGroupIndex,
adIndexInAdGroup,
mediaPeriodInfo.endPositionUs,
mediaPeriodInfo.id.endPositionUs,
currentPeriodId.windowSequenceNumber);
} else {
// Check if the postroll ad should be played.
Expand Down Expand Up @@ -639,18 +643,18 @@ private MediaPeriodInfo getFirstMediaPeriodInfo(PlaybackInfo playbackInfo) {

private MediaPeriodInfo getUpdatedMediaPeriodInfo(MediaPeriodInfo info, MediaPeriodId newId) {
long startPositionUs = info.startPositionUs;
long endPositionUs = info.endPositionUs;
boolean isLastInPeriod = isLastInPeriod(newId, endPositionUs);
boolean isLastInPeriod = isLastInPeriod(newId);
boolean isLastInTimeline = isLastInTimeline(newId, isLastInPeriod);
timeline.getPeriod(newId.periodIndex, period);
long durationUs =
newId.isAd()
? period.getAdDurationUs(newId.adGroupIndex, newId.adIndexInAdGroup)
: (endPositionUs == C.TIME_END_OF_SOURCE ? period.getDurationUs() : endPositionUs);
: (newId.endPositionUs == C.TIME_END_OF_SOURCE
? period.getDurationUs()
: newId.endPositionUs);
return new MediaPeriodInfo(
newId,
startPositionUs,
endPositionUs,
info.contentPositionUs,
durationUs,
isLastInPeriod,
Expand Down Expand Up @@ -683,7 +687,7 @@ private MediaPeriodInfo getMediaPeriodInfoForAd(
long windowSequenceNumber) {
MediaPeriodId id =
new MediaPeriodId(periodIndex, adGroupIndex, adIndexInAdGroup, windowSequenceNumber);
boolean isLastInPeriod = isLastInPeriod(id, C.TIME_END_OF_SOURCE);
boolean isLastInPeriod = isLastInPeriod(id);
boolean isLastInTimeline = isLastInTimeline(id, isLastInPeriod);
long durationUs =
timeline
Expand All @@ -696,7 +700,6 @@ private MediaPeriodInfo getMediaPeriodInfoForAd(
return new MediaPeriodInfo(
id,
startPositionUs,
C.TIME_END_OF_SOURCE,
contentPositionUs,
durationUs,
isLastInPeriod,
Expand All @@ -705,21 +708,22 @@ private MediaPeriodInfo getMediaPeriodInfoForAd(

private MediaPeriodInfo getMediaPeriodInfoForContent(
int periodIndex, long startPositionUs, long windowSequenceNumber) {
MediaPeriodId id = new MediaPeriodId(periodIndex, windowSequenceNumber);
timeline.getPeriod(id.periodIndex, period);
int nextAdGroupIndex = period.getAdGroupIndexAfterPositionUs(startPositionUs);
long endUs =
long endPositionUs =
nextAdGroupIndex == C.INDEX_UNSET
? C.TIME_END_OF_SOURCE
: period.getAdGroupTimeUs(nextAdGroupIndex);
boolean isLastInPeriod = isLastInPeriod(id, endUs);
MediaPeriodId id = new MediaPeriodId(periodIndex, windowSequenceNumber, endPositionUs);
timeline.getPeriod(id.periodIndex, period);
boolean isLastInPeriod = isLastInPeriod(id);
boolean isLastInTimeline = isLastInTimeline(id, isLastInPeriod);
long durationUs = endUs == C.TIME_END_OF_SOURCE ? period.getDurationUs() : endUs;
long durationUs =
endPositionUs == C.TIME_END_OF_SOURCE ? period.getDurationUs() : endPositionUs;
return new MediaPeriodInfo(
id, startPositionUs, endUs, C.TIME_UNSET, durationUs, isLastInPeriod, isLastInTimeline);
id, startPositionUs, C.TIME_UNSET, durationUs, isLastInPeriod, isLastInTimeline);
}

private boolean isLastInPeriod(MediaPeriodId id, long endPositionUs) {
private boolean isLastInPeriod(MediaPeriodId id) {
int adGroupCount = timeline.getPeriod(id.periodIndex, period).getAdGroupCount();
if (adGroupCount == 0) {
return true;
Expand All @@ -729,7 +733,7 @@ private boolean isLastInPeriod(MediaPeriodId id, long endPositionUs) {
boolean isAd = id.isAd();
if (period.getAdGroupTimeUs(lastAdGroupIndex) != C.TIME_END_OF_SOURCE) {
// There's no postroll ad.
return !isAd && endPositionUs == C.TIME_END_OF_SOURCE;
return !isAd && id.endPositionUs == C.TIME_END_OF_SOURCE;
}

int postrollAdCount = period.getAdCountInAdGroup(lastAdGroupIndex);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,15 @@ final class MediaPeriodId {
*/
public final long windowSequenceNumber;

/**
* The end position of the media to play within the media period, in microseconds, or {@link
* C#TIME_END_OF_SOURCE} if the end position is the end of the media period.
*
* <p>Note that this only applies if the media period is for content (i.e., not for an ad) and
* is clipped to the position of the next ad group.
*/
public final long endPositionUs;

/**
* Creates a media period identifier for a dummy period which is not part of a buffered sequence
* of windows.
Expand All @@ -109,7 +118,20 @@ public MediaPeriodId(int periodIndex) {
* windows this media period is part of.
*/
public MediaPeriodId(int periodIndex, long windowSequenceNumber) {
this(periodIndex, C.INDEX_UNSET, C.INDEX_UNSET, windowSequenceNumber);
this(periodIndex, C.INDEX_UNSET, C.INDEX_UNSET, windowSequenceNumber, C.TIME_END_OF_SOURCE);
}

/**
* Creates a media period identifier for the specified clipped period in the timeline.
*
* @param periodIndex The timeline period index.
* @param windowSequenceNumber The sequence number of the window in the buffered sequence of
* windows this media period is part of.
* @param endPositionUs The end position of the media period within the timeline period, in
* microseconds.
*/
public MediaPeriodId(int periodIndex, long windowSequenceNumber, long endPositionUs) {
this(periodIndex, C.INDEX_UNSET, C.INDEX_UNSET, windowSequenceNumber, endPositionUs);
}

/**
Expand All @@ -124,10 +146,20 @@ public MediaPeriodId(int periodIndex, long windowSequenceNumber) {
*/
public MediaPeriodId(
int periodIndex, int adGroupIndex, int adIndexInAdGroup, long windowSequenceNumber) {
this(periodIndex, adGroupIndex, adIndexInAdGroup, windowSequenceNumber, C.TIME_END_OF_SOURCE);
}

private MediaPeriodId(
int periodIndex,
int adGroupIndex,
int adIndexInAdGroup,
long windowSequenceNumber,
long endPositionUs) {
this.periodIndex = periodIndex;
this.adGroupIndex = adGroupIndex;
this.adIndexInAdGroup = adIndexInAdGroup;
this.windowSequenceNumber = windowSequenceNumber;
this.endPositionUs = endPositionUs;
}

/**
Expand All @@ -136,7 +168,8 @@ public MediaPeriodId(
public MediaPeriodId copyWithPeriodIndex(int newPeriodIndex) {
return periodIndex == newPeriodIndex
? this
: new MediaPeriodId(newPeriodIndex, adGroupIndex, adIndexInAdGroup, windowSequenceNumber);
: new MediaPeriodId(
newPeriodIndex, adGroupIndex, adIndexInAdGroup, windowSequenceNumber, endPositionUs);
}

/**
Expand All @@ -159,7 +192,8 @@ public boolean equals(@Nullable Object obj) {
return periodIndex == periodId.periodIndex
&& adGroupIndex == periodId.adGroupIndex
&& adIndexInAdGroup == periodId.adIndexInAdGroup
&& windowSequenceNumber == periodId.windowSequenceNumber;
&& windowSequenceNumber == periodId.windowSequenceNumber
&& endPositionUs == periodId.endPositionUs;
}

@Override
Expand All @@ -169,6 +203,7 @@ public int hashCode() {
result = 31 * result + adGroupIndex;
result = 31 * result + adIndexInAdGroup;
result = 31 * result + (int) windowSequenceNumber;
result = 31 * result + (int) endPositionUs;
return result;
}

Expand Down

0 comments on commit 0b3fa51

Please sign in to comment.