Commit 2480e0a4 by tonihei Committed by Ian Baker

Add option to align durations of MergingMediaSource.

Without this feature it's impossible to nicely merge multiple sources
with different durations if these durations are not known exactly
before the start of playback.

Issue: #8422
PiperOrigin-RevId: 350567625
parent 1ef06b8d
...@@ -42,6 +42,9 @@ ...@@ -42,6 +42,9 @@
* Populate codecs string for H.264/AVC in MP4, Matroska and FLV streams to * Populate codecs string for H.264/AVC in MP4, Matroska and FLV streams to
allow decoder capability checks based on codec profile/level allow decoder capability checks based on codec profile/level
([#8393](https://github.com/google/ExoPlayer/issues/8393)). ([#8393](https://github.com/google/ExoPlayer/issues/8393)).
* Add option to `MergingMediaSource` to clip the durations of all sources
to have the same length
([#8422](https://github.com/google/ExoPlayer/issues/8422)).
* Track selection: * Track selection:
* Allow parallel adaptation for video and audio * Allow parallel adaptation for video and audio
([#5111](https://github.com/google/ExoPlayer/issues/5111)). ([#5111](https://github.com/google/ExoPlayer/issues/5111)).
......
...@@ -16,7 +16,7 @@ ...@@ -16,7 +16,7 @@
package com.google.android.exoplayer2.source; package com.google.android.exoplayer2.source;
import static com.google.common.truth.Truth.assertThat; import static com.google.common.truth.Truth.assertThat;
import static org.junit.Assert.fail; import static org.junit.Assert.assertThrows;
import androidx.test.ext.junit.runners.AndroidJUnit4; import androidx.test.ext.junit.runners.AndroidJUnit4;
import com.google.android.exoplayer2.C; import com.google.android.exoplayer2.C;
...@@ -35,35 +35,65 @@ import org.junit.runner.RunWith; ...@@ -35,35 +35,65 @@ import org.junit.runner.RunWith;
public class MergingMediaSourceTest { public class MergingMediaSourceTest {
@Test @Test
public void mergingDynamicTimelines() throws IOException { public void prepare_withoutDurationClipping_usesTimelineOfFirstSource() throws IOException {
FakeTimeline firstTimeline = FakeTimeline timeline1 =
new FakeTimeline(new TimelineWindowDefinition(true, true, C.TIME_UNSET)); new FakeTimeline(
FakeTimeline secondTimeline = new TimelineWindowDefinition(
new FakeTimeline(new TimelineWindowDefinition(true, true, C.TIME_UNSET)); /* isSeekable= */ true, /* isDynamic= */ true, /* durationUs= */ 30));
testMergingMediaSourcePrepare(firstTimeline, secondTimeline); FakeTimeline timeline2 =
new FakeTimeline(
new TimelineWindowDefinition(
/* isSeekable= */ true, /* isDynamic= */ true, /* durationUs= */ C.TIME_UNSET));
FakeTimeline timeline3 =
new FakeTimeline(
new TimelineWindowDefinition(
/* isSeekable= */ true, /* isDynamic= */ true, /* durationUs= */ 10));
Timeline mergedTimeline =
prepareMergingMediaSource(/* clipDurations= */ false, timeline1, timeline2, timeline3);
assertThat(mergedTimeline).isEqualTo(timeline1);
} }
@Test @Test
public void mergingStaticTimelines() throws IOException { public void prepare_withDurationClipping_usesDurationOfShortestSource() throws IOException {
FakeTimeline firstTimeline = new FakeTimeline(new TimelineWindowDefinition(true, false, 20)); FakeTimeline timeline1 =
FakeTimeline secondTimeline = new FakeTimeline(new TimelineWindowDefinition(true, false, 10)); new FakeTimeline(
testMergingMediaSourcePrepare(firstTimeline, secondTimeline); new TimelineWindowDefinition(
/* isSeekable= */ true, /* isDynamic= */ true, /* durationUs= */ 30));
FakeTimeline timeline2 =
new FakeTimeline(
new TimelineWindowDefinition(
/* isSeekable= */ true, /* isDynamic= */ true, /* durationUs= */ C.TIME_UNSET));
FakeTimeline timeline3 =
new FakeTimeline(
new TimelineWindowDefinition(
/* isSeekable= */ true, /* isDynamic= */ true, /* durationUs= */ 10));
Timeline mergedTimeline =
prepareMergingMediaSource(/* clipDurations= */ true, timeline1, timeline2, timeline3);
assertThat(mergedTimeline).isEqualTo(timeline3);
} }
@Test @Test
public void mergingTimelinesWithDifferentPeriodCounts() throws IOException { public void prepare_differentPeriodCounts_fails() throws IOException {
FakeTimeline firstTimeline = new FakeTimeline(new TimelineWindowDefinition(1, null)); FakeTimeline firstTimeline =
FakeTimeline secondTimeline = new FakeTimeline(new TimelineWindowDefinition(2, null)); new FakeTimeline(new TimelineWindowDefinition(/* periodCount= */ 1, /* id= */ 1));
try { FakeTimeline secondTimeline =
testMergingMediaSourcePrepare(firstTimeline, secondTimeline); new FakeTimeline(new TimelineWindowDefinition(/* periodCount= */ 2, /* id= */ 2));
fail("Expected merging to fail.");
} catch (IllegalMergeException e) { IllegalMergeException exception =
assertThat(e.reason).isEqualTo(IllegalMergeException.REASON_PERIOD_COUNT_MISMATCH); assertThrows(
} IllegalMergeException.class,
() ->
prepareMergingMediaSource(
/* clipDurations= */ false, firstTimeline, secondTimeline));
assertThat(exception.reason).isEqualTo(IllegalMergeException.REASON_PERIOD_COUNT_MISMATCH);
} }
@Test @Test
public void mergingMediaSourcePeriodCreation() throws Exception { public void createPeriod_createsChildPeriods() throws Exception {
FakeMediaSource[] mediaSources = new FakeMediaSource[2]; FakeMediaSource[] mediaSources = new FakeMediaSource[2];
for (int i = 0; i < mediaSources.length; i++) { for (int i = 0; i < mediaSources.length; i++) {
mediaSources[i] = new FakeMediaSource(new FakeTimeline(/* windowCount= */ 2)); mediaSources[i] = new FakeMediaSource(new FakeTimeline(/* windowCount= */ 2));
...@@ -83,24 +113,26 @@ public class MergingMediaSourceTest { ...@@ -83,24 +113,26 @@ public class MergingMediaSourceTest {
} }
/** /**
* Wraps the specified timelines in a {@link MergingMediaSource}, prepares it and checks that it * Wraps the specified timelines in a {@link MergingMediaSource}, prepares it and returns the
* forwards the first of the wrapped timelines. * merged timeline.
*/ */
private static void testMergingMediaSourcePrepare(Timeline... timelines) throws IOException { private static Timeline prepareMergingMediaSource(boolean clipDurations, Timeline... timelines)
throws IOException {
FakeMediaSource[] mediaSources = new FakeMediaSource[timelines.length]; FakeMediaSource[] mediaSources = new FakeMediaSource[timelines.length];
for (int i = 0; i < timelines.length; i++) { for (int i = 0; i < timelines.length; i++) {
mediaSources[i] = new FakeMediaSource(timelines[i]); mediaSources[i] = new FakeMediaSource(timelines[i]);
} }
MergingMediaSource mergingMediaSource = new MergingMediaSource(mediaSources); MergingMediaSource mergingMediaSource =
MediaSourceTestRunner testRunner = new MediaSourceTestRunner(mergingMediaSource, null); new MergingMediaSource(/* adjustPeriodTimeOffsets= */ false, clipDurations, mediaSources);
MediaSourceTestRunner testRunner =
new MediaSourceTestRunner(mergingMediaSource, /* allocator= */ null);
try { try {
Timeline timeline = testRunner.prepareSource(); Timeline timeline = testRunner.prepareSource();
// The merged timeline should always be the one from the first child.
assertThat(timeline).isEqualTo(timelines[0]);
testRunner.releaseSource(); testRunner.releaseSource();
for (FakeMediaSource mediaSource : mediaSources) { for (FakeMediaSource mediaSource : mediaSources) {
mediaSource.assertReleased(); mediaSource.assertReleased();
} }
return timeline;
} finally { } finally {
testRunner.release(); testRunner.release();
} }
......
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