Commit fb73a9df by bachinger Committed by Oliver Woodman

Make media item of Timeline.Window non-null

This change makes the media item of Timeline.Window non-null by providing a fallback media item in window.set(...). After having migrated all media sources this should not be needed anymore, but a fallback makes it more safe than just making the mediaItem argument of window.set(...) non-null (which is done in a following CL in this chain of CLs).

PiperOrigin-RevId: 314527983
parent 7c33e257
...@@ -15,6 +15,7 @@ ...@@ -15,6 +15,7 @@
*/ */
package com.google.android.exoplayer2; package com.google.android.exoplayer2;
import android.net.Uri;
import android.os.SystemClock; import android.os.SystemClock;
import android.util.Pair; import android.util.Pair;
import androidx.annotation.Nullable; import androidx.annotation.Nullable;
...@@ -123,6 +124,12 @@ public abstract class Timeline { ...@@ -123,6 +124,12 @@ public abstract class Timeline {
*/ */
public static final Object SINGLE_WINDOW_UID = new Object(); public static final Object SINGLE_WINDOW_UID = new Object();
private static final MediaItem DUMMY_MEDIA_ITEM =
new MediaItem.Builder()
.setMediaId("com.google.android.exoplayer2.Timeline")
.setUri(Uri.EMPTY)
.build();
/** /**
* A unique identifier for the window. Single-window {@link Timeline Timelines} must use {@link * A unique identifier for the window. Single-window {@link Timeline Timelines} must use {@link
* #SINGLE_WINDOW_UID}. * #SINGLE_WINDOW_UID}.
...@@ -133,7 +140,7 @@ public abstract class Timeline { ...@@ -133,7 +140,7 @@ public abstract class Timeline {
@Deprecated @Nullable public Object tag; @Deprecated @Nullable public Object tag;
/** The {@link MediaItem} associated to the window. Not necessarily unique. */ /** The {@link MediaItem} associated to the window. Not necessarily unique. */
@Nullable public MediaItem mediaItem; public MediaItem mediaItem;
/** The manifest of the window. May be {@code null}. */ /** The manifest of the window. May be {@code null}. */
@Nullable public Object manifest; @Nullable public Object manifest;
...@@ -215,13 +222,13 @@ public abstract class Timeline { ...@@ -215,13 +222,13 @@ public abstract class Timeline {
/** Creates window. */ /** Creates window. */
public Window() { public Window() {
uid = SINGLE_WINDOW_UID; uid = SINGLE_WINDOW_UID;
mediaItem = DUMMY_MEDIA_ITEM;
} }
/** /**
* @deprecated Use {@link #set(Object, MediaItem, Object, long, long, long, boolean, boolean, * @deprecated Use {@link #set(Object, MediaItem, Object, long, long, long, boolean, boolean,
* boolean, long, long, int, int, long)} instead. * boolean, long, long, int, int, long)} instead.
*/ */
@SuppressWarnings("deprecation")
@Deprecated @Deprecated
public Window set( public Window set(
Object uid, Object uid,
...@@ -240,7 +247,7 @@ public abstract class Timeline { ...@@ -240,7 +247,7 @@ public abstract class Timeline {
long positionInFirstPeriodUs) { long positionInFirstPeriodUs) {
set( set(
uid, uid,
/* mediaItem= */ null, DUMMY_MEDIA_ITEM.buildUpon().setTag(tag).build(),
manifest, manifest,
presentationStartTimeMs, presentationStartTimeMs,
windowStartTimeMs, windowStartTimeMs,
...@@ -253,7 +260,6 @@ public abstract class Timeline { ...@@ -253,7 +260,6 @@ public abstract class Timeline {
firstPeriodIndex, firstPeriodIndex,
lastPeriodIndex, lastPeriodIndex,
positionInFirstPeriodUs); positionInFirstPeriodUs);
this.tag = tag;
return this; return this;
} }
...@@ -275,7 +281,7 @@ public abstract class Timeline { ...@@ -275,7 +281,7 @@ public abstract class Timeline {
int lastPeriodIndex, int lastPeriodIndex,
long positionInFirstPeriodUs) { long positionInFirstPeriodUs) {
this.uid = uid; this.uid = uid;
this.mediaItem = mediaItem; this.mediaItem = mediaItem != null ? mediaItem : DUMMY_MEDIA_ITEM;
this.tag = this.tag =
mediaItem != null && mediaItem.playbackProperties != null mediaItem != null && mediaItem.playbackProperties != null
? mediaItem.playbackProperties.tag ? mediaItem.playbackProperties.tag
...@@ -356,6 +362,7 @@ public abstract class Timeline { ...@@ -356,6 +362,7 @@ public abstract class Timeline {
return Util.getNowUnixTimeMs(elapsedRealtimeEpochOffsetMs); return Util.getNowUnixTimeMs(elapsedRealtimeEpochOffsetMs);
} }
// Provide backward compatibility for tag.
@Override @Override
public boolean equals(@Nullable Object obj) { public boolean equals(@Nullable Object obj) {
if (this == obj) { if (this == obj) {
...@@ -366,7 +373,6 @@ public abstract class Timeline { ...@@ -366,7 +373,6 @@ public abstract class Timeline {
} }
Window that = (Window) obj; Window that = (Window) obj;
return Util.areEqual(uid, that.uid) return Util.areEqual(uid, that.uid)
&& Util.areEqual(tag, that.tag)
&& Util.areEqual(mediaItem, that.mediaItem) && Util.areEqual(mediaItem, that.mediaItem)
&& Util.areEqual(manifest, that.manifest) && Util.areEqual(manifest, that.manifest)
&& presentationStartTimeMs == that.presentationStartTimeMs && presentationStartTimeMs == that.presentationStartTimeMs
...@@ -383,12 +389,12 @@ public abstract class Timeline { ...@@ -383,12 +389,12 @@ public abstract class Timeline {
&& positionInFirstPeriodUs == that.positionInFirstPeriodUs; && positionInFirstPeriodUs == that.positionInFirstPeriodUs;
} }
// Provide backward compatibility for tag.
@Override @Override
public int hashCode() { public int hashCode() {
int result = 7; int result = 7;
result = 31 * result + uid.hashCode(); result = 31 * result + uid.hashCode();
result = 31 * result + (tag == null ? 0 : tag.hashCode()); result = 31 * result + mediaItem.hashCode();
result = 31 * result + (mediaItem == null ? 0 : mediaItem.hashCode());
result = 31 * result + (manifest == null ? 0 : manifest.hashCode()); result = 31 * result + (manifest == null ? 0 : manifest.hashCode());
result = 31 * result + (int) (presentationStartTimeMs ^ (presentationStartTimeMs >>> 32)); result = 31 * result + (int) (presentationStartTimeMs ^ (presentationStartTimeMs >>> 32));
result = 31 * result + (int) (windowStartTimeMs ^ (windowStartTimeMs >>> 32)); result = 31 * result + (int) (windowStartTimeMs ^ (windowStartTimeMs >>> 32));
...@@ -688,7 +694,7 @@ public abstract class Timeline { ...@@ -688,7 +694,7 @@ public abstract class Timeline {
result = 31 * result + windowIndex; result = 31 * result + windowIndex;
result = 31 * result + (int) (durationUs ^ (durationUs >>> 32)); result = 31 * result + (int) (durationUs ^ (durationUs >>> 32));
result = 31 * result + (int) (positionInWindowUs ^ (positionInWindowUs >>> 32)); result = 31 * result + (int) (positionInWindowUs ^ (positionInWindowUs >>> 32));
result = 31 * result + (adPlaybackState == null ? 0 : adPlaybackState.hashCode()); result = 31 * result + adPlaybackState.hashCode();
return result; return result;
} }
} }
......
...@@ -17,6 +17,7 @@ package com.google.android.exoplayer2; ...@@ -17,6 +17,7 @@ package com.google.android.exoplayer2;
import static com.google.common.truth.Truth.assertThat; import static com.google.common.truth.Truth.assertThat;
import android.net.Uri;
import androidx.annotation.Nullable; import androidx.annotation.Nullable;
import androidx.test.ext.junit.runners.AndroidJUnit4; import androidx.test.ext.junit.runners.AndroidJUnit4;
import com.google.android.exoplayer2.testutil.FakeTimeline; import com.google.android.exoplayer2.testutil.FakeTimeline;
...@@ -62,7 +63,6 @@ public class TimelineTest { ...@@ -62,7 +63,6 @@ public class TimelineTest {
TimelineAsserts.assertNextWindowIndices(timeline, Player.REPEAT_MODE_ALL, false, 0); TimelineAsserts.assertNextWindowIndices(timeline, Player.REPEAT_MODE_ALL, false, 0);
} }
@SuppressWarnings("deprecation") // Tests the deprecated window.tag property.
@Test @Test
public void windowEquals() { public void windowEquals() {
MediaItem mediaItem = new MediaItem.Builder().setUri("uri").setTag(new Object()).build(); MediaItem mediaItem = new MediaItem.Builder().setUri("uri").setTag(new Object()).build();
...@@ -74,10 +74,6 @@ public class TimelineTest { ...@@ -74,10 +74,6 @@ public class TimelineTest {
assertThat(window).isNotEqualTo(otherWindow); assertThat(window).isNotEqualTo(otherWindow);
otherWindow = new Timeline.Window(); otherWindow = new Timeline.Window();
otherWindow.tag = mediaItem.playbackProperties.tag;
assertThat(window).isNotEqualTo(otherWindow);
otherWindow = new Timeline.Window();
otherWindow.manifest = new Object(); otherWindow.manifest = new Object();
assertThat(window).isNotEqualTo(otherWindow); assertThat(window).isNotEqualTo(otherWindow);
...@@ -148,7 +144,15 @@ public class TimelineTest { ...@@ -148,7 +144,15 @@ public class TimelineTest {
@SuppressWarnings("deprecation") @SuppressWarnings("deprecation")
@Test @Test
public void windowSet_withTag() { public void windowSet_withTag() {
Timeline.Window window = populateWindow(/* mediaItem= */ null, new Object()); Object tag = new Object();
Timeline.Window window =
populateWindow(
new MediaItem.Builder()
.setMediaId("com.google.android.exoplayer2.Timeline")
.setUri(Uri.EMPTY)
.setTag(tag)
.build(),
tag);
Timeline.Window otherWindow = new Timeline.Window(); Timeline.Window otherWindow = new Timeline.Window();
otherWindow = otherWindow =
otherWindow.set( otherWindow.set(
......
...@@ -108,7 +108,7 @@ public final class SinglePeriodTimelineTest { ...@@ -108,7 +108,7 @@ public final class SinglePeriodTimelineTest {
} }
@Test @Test
public void setNullMediaItem_returnsNullMediaItem_butUsesDefaultUid() { public void setNullMediaItem_returnsFallbackMediaItem_butUsesDefaultUid() {
SinglePeriodTimeline timeline = SinglePeriodTimeline timeline =
new SinglePeriodTimeline( new SinglePeriodTimeline(
/* durationUs= */ C.TIME_UNSET, /* durationUs= */ C.TIME_UNSET,
...@@ -118,7 +118,8 @@ public final class SinglePeriodTimelineTest { ...@@ -118,7 +118,8 @@ public final class SinglePeriodTimelineTest {
/* manifest= */ null, /* manifest= */ null,
/* mediaItem= */ null); /* mediaItem= */ null);
assertThat(timeline.getWindow(/* windowIndex= */ 0, window).mediaItem).isNull(); assertThat(timeline.getWindow(/* windowIndex= */ 0, window).mediaItem.mediaId)
.isEqualTo("com.google.android.exoplayer2.Timeline");
assertThat(timeline.getPeriod(/* periodIndex= */ 0, period, /* setIds= */ false).id).isNull(); assertThat(timeline.getPeriod(/* periodIndex= */ 0, period, /* setIds= */ false).id).isNull();
assertThat(timeline.getPeriod(/* periodIndex= */ 0, period, /* setIds= */ true).id).isNull(); assertThat(timeline.getPeriod(/* periodIndex= */ 0, period, /* setIds= */ true).id).isNull();
assertThat(timeline.getPeriod(/* periodIndex= */ 0, period, /* setIds= */ false).uid).isNull(); assertThat(timeline.getPeriod(/* periodIndex= */ 0, period, /* setIds= */ false).uid).isNull();
......
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