From ee11d9d6fb1e62ff760f6ddd8c32318fa5c1573a Mon Sep 17 00:00:00 2001 From: tonihei Date: Tue, 26 May 2020 09:45:36 +0100 Subject: [PATCH] Make manifest loads on timeline refresh optional in FakeMediaSource. Every timeline refresh currently assumes that it corresponds to a manifest refresh and issues the respective load events. However, there are other timeline updates that don't have a manifest refresh (e.g. ad state updates)and thus shouldn't issue these events. PiperOrigin-RevId: 313150489 --- .../analytics/AnalyticsCollectorTest.java | 9 ++---- .../exoplayer2/testutil/FakeMediaSource.java | 30 ++++++++++++++----- 2 files changed, 24 insertions(+), 15 deletions(-) diff --git a/library/core/src/test/java/com/google/android/exoplayer2/analytics/AnalyticsCollectorTest.java b/library/core/src/test/java/com/google/android/exoplayer2/analytics/AnalyticsCollectorTest.java index 891d7f28bb..5721212567 100644 --- a/library/core/src/test/java/com/google/android/exoplayer2/analytics/AnalyticsCollectorTest.java +++ b/library/core/src/test/java/com/google/android/exoplayer2/analytics/AnalyticsCollectorTest.java @@ -849,7 +849,8 @@ public final class AnalyticsCollectorTest { /* isSeekable= */ true, /* isDynamic= */ false, /* durationUs =*/ 10 * C.MICROS_PER_SECOND, - adPlaybackState.get()))); + adPlaybackState.get())), + /* sendManifestLoadEvents= */ false); } } }); @@ -956,25 +957,19 @@ public final class AnalyticsCollectorTest { assertThat(listener.getEvents(EVENT_LOAD_STARTED)) .containsExactly( WINDOW_0 /* content manifest */, - WINDOW_0 /* preroll manifest */, prerollAd, contentAfterPreroll, - WINDOW_0 /* midroll manifest */, midrollAd, contentAfterMidroll, - WINDOW_0 /* postroll manifest */, postrollAd, contentAfterPostroll); assertThat(listener.getEvents(EVENT_LOAD_COMPLETED)) .containsExactly( WINDOW_0 /* content manifest */, - WINDOW_0 /* preroll manifest */, prerollAd, contentAfterPreroll, - WINDOW_0 /* midroll manifest */, midrollAd, contentAfterMidroll, - WINDOW_0 /* postroll manifest */, postrollAd, contentAfterPostroll); assertThat(listener.getEvents(EVENT_DOWNSTREAM_FORMAT_CHANGED)) diff --git a/testutils/src/main/java/com/google/android/exoplayer2/testutil/FakeMediaSource.java b/testutils/src/main/java/com/google/android/exoplayer2/testutil/FakeMediaSource.java index cb55127c22..70b67e9fc1 100644 --- a/testutils/src/main/java/com/google/android/exoplayer2/testutil/FakeMediaSource.java +++ b/testutils/src/main/java/com/google/android/exoplayer2/testutil/FakeMediaSource.java @@ -40,9 +40,9 @@ import com.google.android.exoplayer2.upstream.DataSpec; import com.google.android.exoplayer2.upstream.TransferListener; import com.google.android.exoplayer2.util.Assertions; import com.google.android.exoplayer2.util.Util; +import com.google.common.collect.ImmutableMap; import java.io.IOException; import java.util.ArrayList; -import java.util.Collections; import java.util.List; import org.checkerframework.checker.nullness.qual.MonotonicNonNull; @@ -159,7 +159,7 @@ public class FakeMediaSource extends BaseMediaSource { releasedSource = false; sourceInfoRefreshHandler = Util.createHandler(); if (timeline != null) { - finishSourcePreparation(); + finishSourcePreparation(/* sendManifestLoadEvents= */ true); } } @@ -209,15 +209,29 @@ public class FakeMediaSource extends BaseMediaSource { /** * Sets a new timeline. If the source is already prepared, this triggers a source info refresh * message being sent to the listener. + * + * @param newTimeline The new {@link Timeline}. */ - public synchronized void setNewSourceInfo(final Timeline newTimeline) { + public void setNewSourceInfo(Timeline newTimeline) { + setNewSourceInfo(newTimeline, /* sendManifestLoadEvents= */ true); + } + + /** + * Sets a new timeline. If the source is already prepared, this triggers a source info refresh + * message being sent to the listener. + * + * @param newTimeline The new {@link Timeline}. + * @param sendManifestLoadEvents Whether to treat this as a manifest refresh and send manifest + * load events to listeners. + */ + public synchronized void setNewSourceInfo(Timeline newTimeline, boolean sendManifestLoadEvents) { if (sourceInfoRefreshHandler != null) { sourceInfoRefreshHandler.post( () -> { assertThat(releasedSource).isFalse(); assertThat(preparedSource).isTrue(); timeline = newTimeline; - finishSourcePreparation(); + finishSourcePreparation(sendManifestLoadEvents); }); } else { timeline = newTimeline; @@ -270,9 +284,9 @@ public class FakeMediaSource extends BaseMediaSource { trackGroupArray, drmSessionManager, eventDispatcher, /* deferOnPrepared= */ false); } - private void finishSourcePreparation() { + private void finishSourcePreparation(boolean sendManifestLoadEvents) { refreshSourceInfo(Assertions.checkStateNotNull(timeline)); - if (!timeline.isEmpty()) { + if (!timeline.isEmpty() && sendManifestLoadEvents) { MediaLoadData mediaLoadData = new MediaLoadData( C.DATA_TYPE_MANIFEST, @@ -290,7 +304,7 @@ public class FakeMediaSource extends BaseMediaSource { loadTaskId, FAKE_DATA_SPEC, FAKE_DATA_SPEC.uri, - /* responseHeaders= */ Collections.emptyMap(), + /* responseHeaders= */ ImmutableMap.of(), elapsedRealTimeMs, /* loadDurationMs= */ 0, /* bytesLoaded= */ 0), @@ -300,7 +314,7 @@ public class FakeMediaSource extends BaseMediaSource { loadTaskId, FAKE_DATA_SPEC, FAKE_DATA_SPEC.uri, - /* responseHeaders= */ Collections.emptyMap(), + /* responseHeaders= */ ImmutableMap.of(), elapsedRealTimeMs, /* loadDurationMs= */ 0, /* bytesLoaded= */ MANIFEST_LOAD_BYTES),