/** * Populates a {@link Period} with data for the period at the specified index. Does not populate * {@link Period#id} and {@link Period#uid}. * * @param periodIndex The index of the period. * @param period The {@link Period} to populate. Must not be null. * @return The populated {@link Period}, for convenience. */ public final Period getPeriod(int periodIndex, Period period) { return getPeriod(periodIndex, period, false); }
/** * Asserts that the durations of the periods in the {@link Timeline} and the durations in the * given sequence are equal. */ public static void assertPeriodDurations(Timeline timeline, long... durationsUs) { int periodCount = timeline.getPeriodCount(); assertThat(periodCount).isEqualTo(durationsUs.length); Period period = new Period(); for (int i = 0; i < periodCount; i++) { assertThat(timeline.getPeriod(i, period).durationUs).isEqualTo(durationsUs[i]); } }
@Override public Period getPeriod(int periodIndex, Period period, boolean setIds) { return timeline.getPeriod(periodIndex, period, setIds); }
/** Asserts that periods' {@link Period#getAdGroupCount()} are set correctly. */ public static void assertAdGroupCounts(Timeline timeline, int... expectedAdGroupCounts) { Period period = new Period(); for (int i = 0; i < timeline.getPeriodCount(); i++) { timeline.getPeriod(i, period); assertThat(period.getAdGroupCount()).isEqualTo(expectedAdGroupCounts[i]); } } }
@Override public Period getPeriod(int periodIndex, Period period, boolean setIds) { timeline.getPeriod(periodIndex, period, setIds); if (Util.areEqual(period.uid, replacedId)) { period.uid = DUMMY_ID; } return period; }
/** * Asserts that the durations of the periods in the {@link Timeline} and the durations in the * given sequence are equal. */ public static void assertPeriodDurations(Timeline timeline, long... durationsUs) { int periodCount = timeline.getPeriodCount(); assertThat(periodCount).isEqualTo(durationsUs.length); Period period = new Period(); for (int i = 0; i < periodCount; i++) { assertThat(timeline.getPeriod(i, period).durationUs).isEqualTo(durationsUs[i]); } }
/** * Populates a {@link Period} with data for the period with the specified unique identifier. * * @param periodUid The unique identifier of the period. * @param period The {@link Period} to populate. Must not be null. * @return The populated {@link Period}, for convenience. */ public Period getPeriodByUid(Object periodUid, Period period) { return getPeriod(getIndexOfPeriod(periodUid), period, /* setIds= */ true); }
/** Asserts that periods' {@link Period#getAdGroupCount()} are set correctly. */ public static void assertAdGroupCounts(Timeline timeline, int... expectedAdGroupCounts) { Period period = new Period(); for (int i = 0; i < timeline.getPeriodCount(); i++) { timeline.getPeriod(i, period); assertThat(period.getAdGroupCount()).isEqualTo(expectedAdGroupCounts[i]); } } }
private static long[][] getAdDurations(Timeline[][] adTimelines, Timeline.Period period) { long[][] adDurations = new long[adTimelines.length][]; for (int i = 0; i < adTimelines.length; i++) { adDurations[i] = new long[adTimelines[i].length]; for (int j = 0; j < adTimelines[i].length; j++) { adDurations[i][j] = adTimelines[i][j] == null ? C.TIME_UNSET : adTimelines[i][j].getPeriod(/* periodIndex= */ 0, period).getDurationUs(); } } return adDurations; }
@Test public void testNoClipping() throws IOException { Timeline timeline = new SinglePeriodTimeline(TEST_PERIOD_DURATION_US, true, false); Timeline clippedTimeline = getClippedTimeline(timeline, 0, TEST_PERIOD_DURATION_US); assertThat(clippedTimeline.getWindowCount()).isEqualTo(1); assertThat(clippedTimeline.getPeriodCount()).isEqualTo(1); assertThat(clippedTimeline.getWindow(0, window).getDurationUs()) .isEqualTo(TEST_PERIOD_DURATION_US); assertThat(clippedTimeline.getPeriod(0, period).getDurationUs()) .isEqualTo(TEST_PERIOD_DURATION_US); }
private MediaPeriodInfo updateMediaPeriodInfoToNewTimeline( MediaPeriodInfo info, Timeline newTimeline) { int newPeriodIndex = newTimeline.getIndexOfPeriod(info.mediaPeriodId.periodUid); if (newPeriodIndex == C.INDEX_UNSET) { // Media period is not yet or no longer available in the new timeline. Keep it as it is. return info; } int newWindowIndex = newTimeline.getPeriod(newPeriodIndex, period).windowIndex; return new MediaPeriodInfo(info.mediaPeriodId, newTimeline, newWindowIndex); } }
@Override public Period getPeriod(int periodIndex, Period period, boolean setIds) { timeline.getPeriod(periodIndex, period, setIds); period.set( period.id, period.uid, period.windowIndex, period.durationUs, period.getPositionInWindowUs(), adPlaybackState); return period; }
@Override public Period getPeriod(int periodIndex, Period period, boolean setIds) { timeline.getPeriod(/* periodIndex= */ 0, period, setIds); long positionInClippedWindowUs = period.getPositionInWindowUs() - startUs; long periodDurationUs = durationUs == C.TIME_UNSET ? C.TIME_UNSET : durationUs - positionInClippedWindowUs; return period.set( period.id, period.uid, /* windowIndex= */ 0, periodDurationUs, positionInClippedWindowUs); } }
private boolean isLastInTimeline(MediaPeriodId id, boolean isLastMediaPeriodInPeriod) { int periodIndex = timeline.getIndexOfPeriod(id.periodUid); int windowIndex = timeline.getPeriod(periodIndex, period).windowIndex; return !timeline.getWindow(windowIndex, window).isDynamic && timeline.isLastPeriod(periodIndex, period, window, repeatMode, shuffleModeEnabled) && isLastMediaPeriodInPeriod; } }
@Override public MediaPeriod createPeriod(MediaPeriodId id, Allocator allocator, long startPositionUs) { assertThat(preparedSource).isTrue(); assertThat(releasedSource).isFalse(); int periodIndex = timeline.getIndexOfPeriod(id.periodUid); Assertions.checkArgument(periodIndex != C.INDEX_UNSET); Period period = timeline.getPeriod(periodIndex, new Period()); EventDispatcher eventDispatcher = createEventDispatcher(period.windowIndex, id, period.getPositionInWindowMs()); FakeMediaPeriod mediaPeriod = createFakeMediaPeriod(id, trackGroupArray, allocator, eventDispatcher, transferListener); activeMediaPeriods.add(mediaPeriod); createdMediaPeriods.add(id); return mediaPeriod; }
@Override public final Period getPeriod(int periodIndex, Period period, boolean setIds) { int childIndex = getChildIndexByPeriodIndex(periodIndex); int firstWindowIndexInChild = getFirstWindowIndexByChildIndex(childIndex); int firstPeriodIndexInChild = getFirstPeriodIndexByChildIndex(childIndex); getTimelineByChildIndex(childIndex).getPeriod(periodIndex - firstPeriodIndexInChild, period, setIds); period.windowIndex += firstWindowIndexInChild; if (setIds) { period.uid = getConcatenatedUid(getChildUidByChildIndex(childIndex), period.uid); } return period; }
@Test public void testClippingStartAndEnd() throws IOException { Timeline timeline = new SinglePeriodTimeline(TEST_PERIOD_DURATION_US, true, false); Timeline clippedTimeline = getClippedTimeline( timeline, TEST_CLIP_AMOUNT_US, TEST_PERIOD_DURATION_US - TEST_CLIP_AMOUNT_US * 2); assertThat(clippedTimeline.getWindow(0, window).getDurationUs()) .isEqualTo(TEST_PERIOD_DURATION_US - TEST_CLIP_AMOUNT_US * 3); assertThat(clippedTimeline.getPeriod(0, period).getDurationUs()) .isEqualTo(TEST_PERIOD_DURATION_US - TEST_CLIP_AMOUNT_US * 2); }
@Test public void testClippingStart() throws IOException { Timeline timeline = new SinglePeriodTimeline(TEST_PERIOD_DURATION_US, true, false); Timeline clippedTimeline = getClippedTimeline(timeline, TEST_CLIP_AMOUNT_US, TEST_PERIOD_DURATION_US); assertThat(clippedTimeline.getWindow(0, window).getDurationUs()) .isEqualTo(TEST_PERIOD_DURATION_US - TEST_CLIP_AMOUNT_US); assertThat(clippedTimeline.getPeriod(0, period).getDurationUs()) .isEqualTo(TEST_PERIOD_DURATION_US); }
@Test public void testClippingEnd() throws IOException { Timeline timeline = new SinglePeriodTimeline(TEST_PERIOD_DURATION_US, true, false); Timeline clippedTimeline = getClippedTimeline(timeline, 0, TEST_PERIOD_DURATION_US - TEST_CLIP_AMOUNT_US); assertThat(clippedTimeline.getWindow(0, window).getDurationUs()) .isEqualTo(TEST_PERIOD_DURATION_US - TEST_CLIP_AMOUNT_US); assertThat(clippedTimeline.getPeriod(0, period).getDurationUs()) .isEqualTo(TEST_PERIOD_DURATION_US - TEST_CLIP_AMOUNT_US); }
@Override public long getDuration() { if (timeline.isEmpty()) { return C.INDEX_UNSET; } if (isPlayingAd()) { long adDurationUs = timeline.getPeriod(0, period).getAdDurationUs(adGroupIndex, adIndexInAdGroup); return C.usToMs(adDurationUs); } else { return timeline.getWindow(getCurrentWindowIndex(), window).getDurationMs(); } }