Commit eb84b144 by tonihei Committed by Oliver Woodman

Move window index and media period id out of MediaLoadData again.

This gives the MediaSourceEventListener API a consistent look when new methods are
added which only have a window index and media period id.

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=190450270
parent dfe4bfba
......@@ -22,6 +22,7 @@ import com.google.android.exoplayer2.ExoPlayer;
import com.google.android.exoplayer2.Timeline;
import com.google.android.exoplayer2.source.MediaSourceEventListener.MediaLoadData;
import com.google.android.exoplayer2.util.Assertions;
import com.google.android.exoplayer2.util.Util;
import java.io.IOException;
import java.util.HashMap;
......@@ -176,8 +177,8 @@ public abstract class CompositeMediaSource<T> extends BaseMediaSource {
private final class ForwardingEventListener implements MediaSourceEventListener {
private final EventDispatcher eventDispatcher;
private final @Nullable T id;
private EventDispatcher eventDispatcher;
public ForwardingEventListener(@Nullable T id) {
this.eventDispatcher = createEventDispatcher(/* mediaPeriodId= */ null);
......@@ -185,72 +186,96 @@ public abstract class CompositeMediaSource<T> extends BaseMediaSource {
}
@Override
public void onLoadStarted(LoadEventInfo loadEventData, MediaLoadData mediaLoadData) {
MediaLoadData correctedMediaLoadData = correctMediaLoadData(mediaLoadData);
if (correctedMediaLoadData != null) {
eventDispatcher.loadStarted(loadEventData, correctedMediaLoadData);
public void onLoadStarted(
int windowIndex,
@Nullable MediaPeriodId mediaPeriodId,
LoadEventInfo loadEventData,
MediaLoadData mediaLoadData) {
if (maybeUpdateEventDispatcher(windowIndex, mediaPeriodId)) {
eventDispatcher.loadStarted(loadEventData, maybeUpdateMediaLoadData(mediaLoadData));
}
}
@Override
public void onLoadCompleted(LoadEventInfo loadEventData, MediaLoadData mediaLoadData) {
MediaLoadData correctedMediaLoadData = correctMediaLoadData(mediaLoadData);
if (correctedMediaLoadData != null) {
eventDispatcher.loadCompleted(loadEventData, correctedMediaLoadData);
public void onLoadCompleted(
int windowIndex,
@Nullable MediaPeriodId mediaPeriodId,
LoadEventInfo loadEventData,
MediaLoadData mediaLoadData) {
if (maybeUpdateEventDispatcher(windowIndex, mediaPeriodId)) {
eventDispatcher.loadCompleted(loadEventData, maybeUpdateMediaLoadData(mediaLoadData));
}
}
@Override
public void onLoadCanceled(LoadEventInfo loadEventData, MediaLoadData mediaLoadData) {
MediaLoadData correctedMediaLoadData = correctMediaLoadData(mediaLoadData);
if (correctedMediaLoadData != null) {
eventDispatcher.loadCanceled(loadEventData, correctedMediaLoadData);
public void onLoadCanceled(
int windowIndex,
@Nullable MediaPeriodId mediaPeriodId,
LoadEventInfo loadEventData,
MediaLoadData mediaLoadData) {
if (maybeUpdateEventDispatcher(windowIndex, mediaPeriodId)) {
eventDispatcher.loadCanceled(loadEventData, maybeUpdateMediaLoadData(mediaLoadData));
}
}
@Override
public void onLoadError(
int windowIndex,
@Nullable MediaPeriodId mediaPeriodId,
LoadEventInfo loadEventData,
MediaLoadData mediaLoadData,
IOException error,
boolean wasCanceled) {
MediaLoadData correctedMediaLoadData = correctMediaLoadData(mediaLoadData);
if (correctedMediaLoadData != null) {
eventDispatcher.loadError(loadEventData, correctedMediaLoadData, error, wasCanceled);
if (maybeUpdateEventDispatcher(windowIndex, mediaPeriodId)) {
eventDispatcher.loadError(
loadEventData, maybeUpdateMediaLoadData(mediaLoadData), error, wasCanceled);
}
}
@Override
public void onUpstreamDiscarded(MediaLoadData mediaLoadData) {
MediaLoadData correctedMediaLoadData = correctMediaLoadData(mediaLoadData);
if (correctedMediaLoadData != null) {
eventDispatcher.upstreamDiscarded(correctedMediaLoadData);
public void onUpstreamDiscarded(
int windowIndex, @Nullable MediaPeriodId mediaPeriodId, MediaLoadData mediaLoadData) {
if (maybeUpdateEventDispatcher(windowIndex, mediaPeriodId)) {
eventDispatcher.upstreamDiscarded(maybeUpdateMediaLoadData(mediaLoadData));
}
}
@Override
public void onDownstreamFormatChanged(MediaLoadData mediaLoadData) {
MediaLoadData correctedMediaLoadData = correctMediaLoadData(mediaLoadData);
if (correctedMediaLoadData != null) {
eventDispatcher.downstreamFormatChanged(correctedMediaLoadData);
public void onDownstreamFormatChanged(
int windowIndex, @Nullable MediaPeriodId mediaPeriodId, MediaLoadData mediaLoadData) {
if (maybeUpdateEventDispatcher(windowIndex, mediaPeriodId)) {
eventDispatcher.downstreamFormatChanged(maybeUpdateMediaLoadData(mediaLoadData));
}
}
private @Nullable MediaLoadData correctMediaLoadData(MediaLoadData mediaLoadData) {
/** Updates the event dispatcher and returns whether the event should be dispatched. */
private boolean maybeUpdateEventDispatcher(
int childWindowIndex, @Nullable MediaPeriodId childMediaPeriodId) {
MediaPeriodId mediaPeriodId = null;
if (mediaLoadData.mediaPeriodId != null) {
mediaPeriodId = getMediaPeriodIdForChildMediaPeriodId(id, mediaLoadData.mediaPeriodId);
if (childMediaPeriodId != null) {
mediaPeriodId = getMediaPeriodIdForChildMediaPeriodId(id, childMediaPeriodId);
if (mediaPeriodId == null) {
// Media period not found. Ignore event.
return null;
return false;
}
}
int windowIndex = getWindowIndexForChildWindowIndex(id, mediaLoadData.windowIndex);
int windowIndex = getWindowIndexForChildWindowIndex(id, childWindowIndex);
if (eventDispatcher.windowIndex != windowIndex
|| !Util.areEqual(eventDispatcher.mediaPeriodId, mediaPeriodId)) {
eventDispatcher =
createEventDispatcher(windowIndex, mediaPeriodId, /* mediaTimeOffsetMs= */ 0);
}
return true;
}
private MediaLoadData maybeUpdateMediaLoadData(MediaLoadData mediaLoadData) {
long mediaStartTimeMs = getMediaTimeForChildMediaTime(id, mediaLoadData.mediaStartTimeMs);
long mediaEndTimeMs = getMediaTimeForChildMediaTime(id, mediaLoadData.mediaEndTimeMs);
if (mediaStartTimeMs == mediaLoadData.mediaStartTimeMs
&& mediaEndTimeMs == mediaLoadData.mediaEndTimeMs) {
return mediaLoadData;
}
return new MediaLoadData(
windowIndex,
mediaPeriodId,
mediaLoadData.dataType,
mediaLoadData.trackType,
mediaLoadData.trackFormat,
......
......@@ -15,6 +15,8 @@
*/
package com.google.android.exoplayer2.source;
import android.support.annotation.Nullable;
import com.google.android.exoplayer2.source.MediaSource.MediaPeriodId;
import java.io.IOException;
/**
......@@ -24,22 +26,36 @@ import java.io.IOException;
public abstract class DefaultMediaSourceEventListener implements MediaSourceEventListener {
@Override
public void onLoadStarted(LoadEventInfo loadEventInfo, MediaLoadData mediaLoadData) {
public void onLoadStarted(
int windowIndex,
@Nullable MediaPeriodId mediaPeriodId,
LoadEventInfo loadEventInfo,
MediaLoadData mediaLoadData) {
// Do nothing.
}
@Override
public void onLoadCompleted(LoadEventInfo loadEventInfo, MediaLoadData mediaLoadData) {
public void onLoadCompleted(
int windowIndex,
@Nullable MediaPeriodId mediaPeriodId,
LoadEventInfo loadEventInfo,
MediaLoadData mediaLoadData) {
// Do nothing.
}
@Override
public void onLoadCanceled(LoadEventInfo loadEventInfo, MediaLoadData mediaLoadData) {
public void onLoadCanceled(
int windowIndex,
@Nullable MediaPeriodId mediaPeriodId,
LoadEventInfo loadEventInfo,
MediaLoadData mediaLoadData) {
// Do nothing.
}
@Override
public void onLoadError(
int windowIndex,
@Nullable MediaPeriodId mediaPeriodId,
LoadEventInfo loadEventInfo,
MediaLoadData mediaLoadData,
IOException error,
......@@ -48,12 +64,14 @@ public abstract class DefaultMediaSourceEventListener implements MediaSourceEven
}
@Override
public void onUpstreamDiscarded(MediaLoadData mediaLoadData) {
public void onUpstreamDiscarded(
int windowIndex, @Nullable MediaPeriodId mediaPeriodId, MediaLoadData mediaLoadData) {
// Do nothing.
}
@Override
public void onDownstreamFormatChanged(MediaLoadData mediaLoadData) {
public void onDownstreamFormatChanged(
int windowIndex, @Nullable MediaPeriodId mediaPeriodId, MediaLoadData mediaLoadData) {
// Do nothing.
}
}
......@@ -394,6 +394,8 @@ public final class ExtractorMediaSource extends BaseMediaSource
@Override
public void onLoadError(
int windowIndex,
@Nullable MediaPeriodId mediaPeriodId,
LoadEventInfo loadEventInfo,
MediaLoadData mediaLoadData,
IOException error,
......
......@@ -296,6 +296,8 @@ public final class SingleSampleMediaSource extends BaseMediaSource {
@Override
public void onLoadError(
int windowIndex,
@Nullable MediaPeriodId mediaPeriodId,
LoadEventInfo loadEventInfo,
MediaLoadData mediaLoadData,
IOException error,
......
......@@ -16,6 +16,7 @@
package com.google.android.exoplayer2.util;
import android.os.SystemClock;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.Surface;
import com.google.android.exoplayer2.C;
......@@ -39,6 +40,7 @@ import com.google.android.exoplayer2.metadata.id3.PrivFrame;
import com.google.android.exoplayer2.metadata.id3.TextInformationFrame;
import com.google.android.exoplayer2.metadata.id3.UrlLinkFrame;
import com.google.android.exoplayer2.metadata.scte35.SpliceCommand;
import com.google.android.exoplayer2.source.MediaSource.MediaPeriodId;
import com.google.android.exoplayer2.source.MediaSourceEventListener;
import com.google.android.exoplayer2.source.TrackGroup;
import com.google.android.exoplayer2.source.TrackGroupArray;
......@@ -361,12 +363,18 @@ public class EventLogger
// MediaSourceEventListener
@Override
public void onLoadStarted(LoadEventInfo loadEventInfo, MediaLoadData mediaLoadData) {
public void onLoadStarted(
int windowIndex,
@Nullable MediaPeriodId mediaPeriodId,
LoadEventInfo loadEventInfo,
MediaLoadData mediaLoadData) {
// Do nothing.
}
@Override
public void onLoadError(
int windowIndex,
@Nullable MediaPeriodId mediaPeriodId,
LoadEventInfo loadEventInfo,
MediaLoadData mediaLoadData,
IOException error,
......@@ -375,22 +383,32 @@ public class EventLogger
}
@Override
public void onLoadCanceled(LoadEventInfo loadEventInfo, MediaLoadData mediaLoadData) {
public void onLoadCanceled(
int windowIndex,
@Nullable MediaPeriodId mediaPeriodId,
LoadEventInfo loadEventInfo,
MediaLoadData mediaLoadData) {
// Do nothing.
}
@Override
public void onLoadCompleted(LoadEventInfo loadEventInfo, MediaLoadData mediaLoadData) {
public void onLoadCompleted(
int windowIndex,
@Nullable MediaPeriodId mediaPeriodId,
LoadEventInfo loadEventInfo,
MediaLoadData mediaLoadData) {
// Do nothing.
}
@Override
public void onUpstreamDiscarded(MediaLoadData mediaLoadData) {
public void onUpstreamDiscarded(
int windowIndex, @Nullable MediaPeriodId mediaPeriodId, MediaLoadData mediaLoadData) {
// Do nothing.
}
@Override
public void onDownstreamFormatChanged(MediaLoadData mediaLoadData) {
public void onDownstreamFormatChanged(
int windowIndex, @Nullable MediaPeriodId mediaPeriodId, MediaLoadData mediaLoadData) {
// Do nothing.
}
......
......@@ -19,6 +19,7 @@ import static com.google.common.truth.Truth.assertThat;
import static org.junit.Assert.fail;
import android.os.Handler;
import android.support.annotation.Nullable;
import com.google.android.exoplayer2.C;
import com.google.android.exoplayer2.Player;
import com.google.android.exoplayer2.Timeline;
......@@ -271,8 +272,6 @@ public final class ClippingMediaSourceTest {
EventDispatcher eventDispatcher) {
eventDispatcher.downstreamFormatChanged(
new MediaLoadData(
/* windowIndex= */ 0,
id,
C.DATA_TYPE_MEDIA,
C.TRACK_TYPE_UNKNOWN,
/* trackFormat= */ null,
......@@ -297,7 +296,10 @@ public final class ClippingMediaSourceTest {
new Handler(),
new DefaultMediaSourceEventListener() {
@Override
public void onDownstreamFormatChanged(MediaLoadData mediaLoadData) {
public void onDownstreamFormatChanged(
int windowIndex,
@Nullable MediaPeriodId mediaPeriodId,
MediaLoadData mediaLoadData) {
reportedMediaLoadData[0] = mediaLoadData;
}
});
......
......@@ -200,8 +200,6 @@ public class FakeMediaSource extends BaseMediaSource {
if (!timeline.isEmpty()) {
MediaLoadData mediaLoadData =
new MediaLoadData(
/* windowIndex= */ 0,
/* mediaPeriodId= */ null,
C.DATA_TYPE_MANIFEST,
C.TRACK_TYPE_UNKNOWN,
/* trackFormat= */ null,
......
......@@ -24,6 +24,8 @@ import android.os.Handler;
import android.os.HandlerThread;
import android.os.Looper;
import android.os.Message;
import android.support.annotation.Nullable;
import android.util.Pair;
import com.google.android.exoplayer2.ExoPlaybackException;
import com.google.android.exoplayer2.PlayerMessage;
import com.google.android.exoplayer2.Timeline;
......@@ -41,7 +43,7 @@ import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.concurrent.CopyOnWriteArraySet;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.TimeUnit;
......@@ -59,7 +61,7 @@ public class MediaSourceTestRunner {
private final Allocator allocator;
private final LinkedBlockingDeque<Timeline> timelines;
private final CopyOnWriteArraySet<MediaLoadData> completedLoads;
private final CopyOnWriteArrayList<Pair<Integer, MediaPeriodId>> completedLoads;
private Timeline timeline;
/**
......@@ -76,7 +78,7 @@ public class MediaSourceTestRunner {
player = new EventHandlingExoPlayer(playbackLooper);
mediaSourceListener = new MediaSourceListener();
timelines = new LinkedBlockingDeque<>();
completedLoads = new CopyOnWriteArraySet<>();
completedLoads = new CopyOnWriteArrayList<>();
mediaSource.addEventListener(playbackHandler, mediaSourceListener);
}
......@@ -294,15 +296,15 @@ public class MediaSourceTestRunner {
/**
* Asserts that the media source reported completed loads via {@link
* MediaSourceEventListener#onLoadCompleted(LoadEventInfo, MediaLoadData)} for each specified
* window index and a null period id. Also asserts that no other loads with media period id null
* are reported.
* MediaSourceEventListener#onLoadCompleted(int, MediaPeriodId, LoadEventInfo, MediaLoadData)} for
* each specified window index and a null period id. Also asserts that no other loads with media
* period id null are reported.
*/
public void assertCompletedManifestLoads(Integer... windowIndices) {
List<Integer> expectedWindowIndices = new ArrayList<>(Arrays.asList(windowIndices));
for (MediaLoadData mediaLoadData : completedLoads) {
if (mediaLoadData.mediaPeriodId == null) {
boolean loadExpected = expectedWindowIndices.remove((Integer) mediaLoadData.windowIndex);
for (Pair<Integer, MediaPeriodId> windowIndexAndMediaPeriodId : completedLoads) {
if (windowIndexAndMediaPeriodId.second == null) {
boolean loadExpected = expectedWindowIndices.remove(windowIndexAndMediaPeriodId.first);
assertThat(loadExpected).isTrue();
}
}
......@@ -313,19 +315,20 @@ public class MediaSourceTestRunner {
/**
* Asserts that the media source reported completed loads via {@link
* MediaSourceEventListener#onLoadCompleted(LoadEventInfo, MediaLoadData)} for each specified
* media period id, and asserts that the associated window index matches the one in the last known
* timeline returned from {@link #prepareSource()}, {@link #assertTimelineChange()} or {@link
* #assertTimelineChangeBlocking()}.
* MediaSourceEventListener#onLoadCompleted(int, MediaPeriodId, LoadEventInfo, MediaLoadData)} for
* each specified media period id, and asserts that the associated window index matches the one in
* the last known timeline returned from {@link #prepareSource()}, {@link #assertTimelineChange()}
* or {@link #assertTimelineChangeBlocking()}.
*/
public void assertCompletedMediaPeriodLoads(MediaPeriodId... mediaPeriodIds) {
Timeline.Period period = new Timeline.Period();
HashSet<MediaPeriodId> expectedLoads = new HashSet<>(Arrays.asList(mediaPeriodIds));
for (MediaLoadData mediaLoadData : completedLoads) {
if (expectedLoads.remove(mediaLoadData.mediaPeriodId)) {
assertThat(mediaLoadData.windowIndex)
.isEqualTo(
timeline.getPeriod(mediaLoadData.mediaPeriodId.periodIndex, period).windowIndex);
for (Pair<Integer, MediaPeriodId> windowIndexAndMediaPeriodId : completedLoads) {
int windowIndex = windowIndexAndMediaPeriodId.first;
MediaPeriodId mediaPeriodId = windowIndexAndMediaPeriodId.second;
if (expectedLoads.remove(mediaPeriodId)) {
assertThat(windowIndex)
.isEqualTo(timeline.getPeriod(mediaPeriodId.periodIndex, period).windowIndex);
}
}
assertWithMessage("Not all expected media source loads have been completed.")
......@@ -352,23 +355,37 @@ public class MediaSourceTestRunner {
// MediaSourceEventListener methods.
@Override
public void onLoadStarted(LoadEventInfo loadEventInfo, MediaLoadData mediaLoadData) {
public void onLoadStarted(
int windowIndex,
@Nullable MediaPeriodId mediaPeriodId,
LoadEventInfo loadEventInfo,
MediaLoadData mediaLoadData) {
Assertions.checkState(Looper.myLooper() == playbackThread.getLooper());
}
@Override
public void onLoadCompleted(LoadEventInfo loadEventInfo, MediaLoadData mediaLoadData) {
public void onLoadCompleted(
int windowIndex,
@Nullable MediaPeriodId mediaPeriodId,
LoadEventInfo loadEventInfo,
MediaLoadData mediaLoadData) {
Assertions.checkState(Looper.myLooper() == playbackThread.getLooper());
completedLoads.add(mediaLoadData);
completedLoads.add(Pair.create(windowIndex, mediaPeriodId));
}
@Override
public void onLoadCanceled(LoadEventInfo loadEventInfo, MediaLoadData mediaLoadData) {
public void onLoadCanceled(
int windowIndex,
@Nullable MediaPeriodId mediaPeriodId,
LoadEventInfo loadEventInfo,
MediaLoadData mediaLoadData) {
Assertions.checkState(Looper.myLooper() == playbackThread.getLooper());
}
@Override
public void onLoadError(
int windowIndex,
@Nullable MediaPeriodId mediaPeriodId,
LoadEventInfo loadEventInfo,
MediaLoadData mediaLoadData,
IOException error,
......@@ -377,12 +394,14 @@ public class MediaSourceTestRunner {
}
@Override
public void onUpstreamDiscarded(MediaLoadData mediaLoadData) {
public void onUpstreamDiscarded(
int windowIndex, @Nullable MediaPeriodId mediaPeriodId, MediaLoadData mediaLoadData) {
Assertions.checkState(Looper.myLooper() == playbackThread.getLooper());
}
@Override
public void onDownstreamFormatChanged(MediaLoadData mediaLoadData) {
public void onDownstreamFormatChanged(
int windowIndex, @Nullable MediaPeriodId mediaPeriodId, MediaLoadData mediaLoadData) {
Assertions.checkState(Looper.myLooper() == playbackThread.getLooper());
}
}
......
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