Commit 7d3e5f2a by krocard Committed by Ian Baker

Move MediaPeriodId in common minimalistically

This is needed to move ExoPlayerException in common.
The follow up cl moves MediaPeriodId completely.

#player-to-common

PiperOrigin-RevId: 346133091
parent 734565a8
/*
* Copyright 2020 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.android.exoplayer2.source;
import androidx.annotation.Nullable;
import com.google.android.exoplayer2.C;
import com.google.android.exoplayer2.Timeline;
/**
* Identifies a specific playback of a {@link Timeline.Period}.
*
* <p>A {@link Timeline.Period} can be played multiple times, for example if it is repeated. Each
* instances of this class identifies a specific playback of a {@link Timeline.Period}.
*
* <p>In ExoPlayer's implementation, {@link MediaPeriodId} identifies a {@code MediaPeriod}.
*/
// TODO(b/172315872) Should be final, but subclassed in MediaSource for backward-compatibility.
public class MediaPeriodId {
/** The unique id of the timeline period. */
public final Object periodUid;
/**
* If the media period is in an ad group, the index of the ad group in the period. {@link
* C#INDEX_UNSET} otherwise.
*/
public final int adGroupIndex;
/**
* If the media period is in an ad group, the index of the ad in its ad group in the period.
* {@link C#INDEX_UNSET} otherwise.
*/
public final int adIndexInAdGroup;
/**
* The sequence number of the window in the buffered sequence of windows this media period is part
* of. {@link C#INDEX_UNSET} if the media period id is not part of a buffered sequence of windows.
*/
public final long windowSequenceNumber;
/**
* The index of the next ad group to which the media period's content is clipped, or {@link
* C#INDEX_UNSET} if there is no following ad group or if this media period is an ad.
*/
public final int nextAdGroupIndex;
/**
* Creates a media period identifier for a period which is not part of a buffered sequence of
* windows.
*
* @param periodUid The unique id of the timeline period.
*/
public MediaPeriodId(Object periodUid) {
this(periodUid, /* windowSequenceNumber= */ C.INDEX_UNSET);
}
/**
* Creates a media period identifier for the specified period in the timeline.
*
* @param periodUid The unique id of the timeline period.
* @param windowSequenceNumber The sequence number of the window in the buffered sequence of
* windows this media period is part of.
*/
public MediaPeriodId(Object periodUid, long windowSequenceNumber) {
this(
periodUid,
/* adGroupIndex= */ C.INDEX_UNSET,
/* adIndexInAdGroup= */ C.INDEX_UNSET,
windowSequenceNumber,
/* nextAdGroupIndex= */ C.INDEX_UNSET);
}
/**
* Creates a media period identifier for the specified clipped period in the timeline.
*
* @param periodUid The unique id of the timeline period.
* @param windowSequenceNumber The sequence number of the window in the buffered sequence of
* windows this media period is part of.
* @param nextAdGroupIndex The index of the next ad group to which the media period's content is
* clipped.
*/
public MediaPeriodId(Object periodUid, long windowSequenceNumber, int nextAdGroupIndex) {
this(
periodUid,
/* adGroupIndex= */ C.INDEX_UNSET,
/* adIndexInAdGroup= */ C.INDEX_UNSET,
windowSequenceNumber,
nextAdGroupIndex);
}
/**
* Creates a media period identifier that identifies an ad within an ad group at the specified
* timeline period.
*
* @param periodUid The unique id of the timeline period that contains the ad group.
* @param adGroupIndex The index of the ad group.
* @param adIndexInAdGroup The index of the ad in the ad group.
* @param windowSequenceNumber The sequence number of the window in the buffered sequence of
* windows this media period is part of.
*/
public MediaPeriodId(
Object periodUid, int adGroupIndex, int adIndexInAdGroup, long windowSequenceNumber) {
this(
periodUid,
adGroupIndex,
adIndexInAdGroup,
windowSequenceNumber,
/* nextAdGroupIndex= */ C.INDEX_UNSET);
}
/** Copy constructor for inheritance. */
// TODO(b/172315872) Delete when client have migrated from MediaSource.MediaPeriodId
protected MediaPeriodId(MediaPeriodId mediaPeriodId) {
this.periodUid = mediaPeriodId.periodUid;
this.adGroupIndex = mediaPeriodId.adGroupIndex;
this.adIndexInAdGroup = mediaPeriodId.adIndexInAdGroup;
this.windowSequenceNumber = mediaPeriodId.windowSequenceNumber;
this.nextAdGroupIndex = mediaPeriodId.nextAdGroupIndex;
}
private MediaPeriodId(
Object periodUid,
int adGroupIndex,
int adIndexInAdGroup,
long windowSequenceNumber,
int nextAdGroupIndex) {
this.periodUid = periodUid;
this.adGroupIndex = adGroupIndex;
this.adIndexInAdGroup = adIndexInAdGroup;
this.windowSequenceNumber = windowSequenceNumber;
this.nextAdGroupIndex = nextAdGroupIndex;
}
/** Returns a copy of this period identifier but with {@code newPeriodUid} as its period uid. */
public MediaPeriodId copyWithPeriodUid(Object newPeriodUid) {
return periodUid.equals(newPeriodUid)
? this
: new MediaPeriodId(
newPeriodUid, adGroupIndex, adIndexInAdGroup, windowSequenceNumber, nextAdGroupIndex);
}
/** Returns whether this period identifier identifies an ad in an ad group in a period. */
public boolean isAd() {
return adGroupIndex != C.INDEX_UNSET;
}
@Override
public boolean equals(@Nullable Object obj) {
if (this == obj) {
return true;
}
if (!(obj instanceof MediaPeriodId)) {
return false;
}
MediaPeriodId periodId = (MediaPeriodId) obj;
return periodUid.equals(periodId.periodUid)
&& adGroupIndex == periodId.adGroupIndex
&& adIndexInAdGroup == periodId.adIndexInAdGroup
&& windowSequenceNumber == periodId.windowSequenceNumber
&& nextAdGroupIndex == periodId.nextAdGroupIndex;
}
@Override
public int hashCode() {
int result = 17;
result = 31 * result + periodUid.hashCode();
result = 31 * result + adGroupIndex;
result = 31 * result + adIndexInAdGroup;
result = 31 * result + (int) windowSequenceNumber;
result = 31 * result + nextAdGroupIndex;
return result;
}
}
...@@ -21,6 +21,7 @@ import androidx.annotation.CheckResult; ...@@ -21,6 +21,7 @@ import androidx.annotation.CheckResult;
import androidx.annotation.IntDef; import androidx.annotation.IntDef;
import androidx.annotation.Nullable; import androidx.annotation.Nullable;
import com.google.android.exoplayer2.C.FormatSupport; import com.google.android.exoplayer2.C.FormatSupport;
import com.google.android.exoplayer2.source.MediaPeriodId;
import com.google.android.exoplayer2.source.MediaSource; import com.google.android.exoplayer2.source.MediaSource;
import com.google.android.exoplayer2.util.Assertions; import com.google.android.exoplayer2.util.Assertions;
import java.io.IOException; import java.io.IOException;
...@@ -133,11 +134,8 @@ public final class ExoPlaybackException extends Exception { ...@@ -133,11 +134,8 @@ public final class ExoPlaybackException extends Exception {
/** The value of {@link SystemClock#elapsedRealtime()} when this exception was created. */ /** The value of {@link SystemClock#elapsedRealtime()} when this exception was created. */
public final long timestampMs; public final long timestampMs;
/** /** The {@link MediaPeriodId} of the media associated with this error, or null if undetermined. */
* The {@link MediaSource.MediaPeriodId} of the media associated with this error, or null if @Nullable public final MediaPeriodId mediaPeriodId;
* undetermined.
*/
@Nullable public final MediaSource.MediaPeriodId mediaPeriodId;
/** /**
* Whether the error may be recoverable. * Whether the error may be recoverable.
...@@ -334,7 +332,7 @@ public final class ExoPlaybackException extends Exception { ...@@ -334,7 +332,7 @@ public final class ExoPlaybackException extends Exception {
int rendererIndex, int rendererIndex,
@Nullable Format rendererFormat, @Nullable Format rendererFormat,
@FormatSupport int rendererFormatSupport, @FormatSupport int rendererFormatSupport,
@Nullable MediaSource.MediaPeriodId mediaPeriodId, @Nullable MediaPeriodId mediaPeriodId,
@TimeoutOperation int timeoutOperation, @TimeoutOperation int timeoutOperation,
long timestampMs, long timestampMs,
boolean isRecoverable) { boolean isRecoverable) {
...@@ -402,14 +400,13 @@ public final class ExoPlaybackException extends Exception { ...@@ -402,14 +400,13 @@ public final class ExoPlaybackException extends Exception {
} }
/** /**
* Returns a copy of this exception with the provided {@link MediaSource.MediaPeriodId}. * Returns a copy of this exception with the provided {@link MediaPeriodId}.
* *
* @param mediaPeriodId The {@link MediaSource.MediaPeriodId}. * @param mediaPeriodId The {@link MediaPeriodId}.
* @return The copied exception. * @return The copied exception.
*/ */
@CheckResult @CheckResult
/* package */ ExoPlaybackException copyWithMediaPeriodId( /* package */ ExoPlaybackException copyWithMediaPeriodId(@Nullable MediaPeriodId mediaPeriodId) {
@Nullable MediaSource.MediaPeriodId mediaPeriodId) {
return new ExoPlaybackException( return new ExoPlaybackException(
getMessage(), getMessage(),
cause, cause,
......
...@@ -646,7 +646,7 @@ public class AnalyticsCollector ...@@ -646,7 +646,7 @@ public class AnalyticsCollector
public final void onPlayerError(ExoPlaybackException error) { public final void onPlayerError(ExoPlaybackException error) {
EventTime eventTime = EventTime eventTime =
error.mediaPeriodId != null error.mediaPeriodId != null
? generateEventTime(error.mediaPeriodId) ? generateEventTime(new MediaPeriodId(error.mediaPeriodId))
: generateCurrentPlayerMediaPeriodEventTime(); : generateCurrentPlayerMediaPeriodEventTime();
sendEvent( sendEvent(
eventTime, eventTime,
......
...@@ -17,7 +17,6 @@ package com.google.android.exoplayer2.source; ...@@ -17,7 +17,6 @@ package com.google.android.exoplayer2.source;
import android.os.Handler; import android.os.Handler;
import androidx.annotation.Nullable; import androidx.annotation.Nullable;
import com.google.android.exoplayer2.C;
import com.google.android.exoplayer2.MediaItem; import com.google.android.exoplayer2.MediaItem;
import com.google.android.exoplayer2.Timeline; import com.google.android.exoplayer2.Timeline;
import com.google.android.exoplayer2.drm.DrmSessionEventListener; import com.google.android.exoplayer2.drm.DrmSessionEventListener;
...@@ -61,155 +60,52 @@ public interface MediaSource { ...@@ -61,155 +60,52 @@ public interface MediaSource {
void onSourceInfoRefreshed(MediaSource source, Timeline timeline); void onSourceInfoRefreshed(MediaSource source, Timeline timeline);
} }
/** Identifier for a {@link MediaPeriod}. */ // TODO(b/172315872) Delete when all clients have been migrated to base class.
final class MediaPeriodId {
/** The unique id of the timeline period. */
public final Object periodUid;
/**
* If the media period is in an ad group, the index of the ad group in the period.
* {@link C#INDEX_UNSET} otherwise.
*/
public final int adGroupIndex;
/**
* If the media period is in an ad group, the index of the ad in its ad group in the period.
* {@link C#INDEX_UNSET} otherwise.
*/
public final int adIndexInAdGroup;
/** /**
* The sequence number of the window in the buffered sequence of windows this media period is * Identifier for a {@link MediaPeriod}.
* part of. {@link C#INDEX_UNSET} if the media period id is not part of a buffered sequence of
* windows.
*/
public final long windowSequenceNumber;
/**
* The index of the next ad group to which the media period's content is clipped, or {@link
* C#INDEX_UNSET} if there is no following ad group or if this media period is an ad.
*/
public final int nextAdGroupIndex;
/**
* Creates a media period identifier for a period which is not part of a buffered sequence of
* windows.
* *
* @param periodUid The unique id of the timeline period. * <p>Extends for backward-compatibility {@link
* com.google.android.exoplayer2.source.MediaPeriodId}.
*/ */
final class MediaPeriodId extends com.google.android.exoplayer2.source.MediaPeriodId {
/** See {@link com.google.android.exoplayer2.source.MediaPeriodId#MediaPeriodId(Object)}. */
public MediaPeriodId(Object periodUid) { public MediaPeriodId(Object periodUid) {
this(periodUid, /* windowSequenceNumber= */ C.INDEX_UNSET); super(periodUid);
} }
/** /**
* Creates a media period identifier for the specified period in the timeline. * See {@link com.google.android.exoplayer2.source.MediaPeriodId#MediaPeriodId(Object, long)}.
*
* @param periodUid The unique id of the timeline period.
* @param windowSequenceNumber The sequence number of the window in the buffered sequence of
* windows this media period is part of.
*/ */
public MediaPeriodId(Object periodUid, long windowSequenceNumber) { public MediaPeriodId(Object periodUid, long windowSequenceNumber) {
this( super(periodUid, windowSequenceNumber);
periodUid,
/* adGroupIndex= */ C.INDEX_UNSET,
/* adIndexInAdGroup= */ C.INDEX_UNSET,
windowSequenceNumber,
/* nextAdGroupIndex= */ C.INDEX_UNSET);
} }
/** /**
* Creates a media period identifier for the specified clipped period in the timeline. * See {@link com.google.android.exoplayer2.source.MediaPeriodId#MediaPeriodId(Object, long,
* * int)}.
* @param periodUid The unique id of the timeline period.
* @param windowSequenceNumber The sequence number of the window in the buffered sequence of
* windows this media period is part of.
* @param nextAdGroupIndex The index of the next ad group to which the media period's content is
* clipped.
*/ */
public MediaPeriodId(Object periodUid, long windowSequenceNumber, int nextAdGroupIndex) { public MediaPeriodId(Object periodUid, long windowSequenceNumber, int nextAdGroupIndex) {
this( super(periodUid, windowSequenceNumber, nextAdGroupIndex);
periodUid,
/* adGroupIndex= */ C.INDEX_UNSET,
/* adIndexInAdGroup= */ C.INDEX_UNSET,
windowSequenceNumber,
nextAdGroupIndex);
} }
/** /**
* Creates a media period identifier that identifies an ad within an ad group at the specified * See {@link com.google.android.exoplayer2.source.MediaPeriodId#MediaPeriodId(Object, int, int,
* timeline period. * long)}.
*
* @param periodUid The unique id of the timeline period that contains the ad group.
* @param adGroupIndex The index of the ad group.
* @param adIndexInAdGroup The index of the ad in the ad group.
* @param windowSequenceNumber The sequence number of the window in the buffered sequence of
* windows this media period is part of.
*/ */
public MediaPeriodId( public MediaPeriodId(
Object periodUid, int adGroupIndex, int adIndexInAdGroup, long windowSequenceNumber) { Object periodUid, int adGroupIndex, int adIndexInAdGroup, long windowSequenceNumber) {
this( super(periodUid, adGroupIndex, adIndexInAdGroup, windowSequenceNumber);
periodUid,
adGroupIndex,
adIndexInAdGroup,
windowSequenceNumber,
/* nextAdGroupIndex= */ C.INDEX_UNSET);
} }
private MediaPeriodId( /** Wraps an {@link com.google.android.exoplayer2.source.MediaPeriodId} into a MediaPeriodId. */
Object periodUid, public MediaPeriodId(com.google.android.exoplayer2.source.MediaPeriodId mediaPeriodId) {
int adGroupIndex, super(mediaPeriodId);
int adIndexInAdGroup,
long windowSequenceNumber,
int nextAdGroupIndex) {
this.periodUid = periodUid;
this.adGroupIndex = adGroupIndex;
this.adIndexInAdGroup = adIndexInAdGroup;
this.windowSequenceNumber = windowSequenceNumber;
this.nextAdGroupIndex = nextAdGroupIndex;
} }
/** Returns a copy of this period identifier but with {@code newPeriodUid} as its period uid. */ /** See {@link com.google.android.exoplayer2.source.MediaPeriodId#copyWithPeriodUid(Object)}. */
public MediaPeriodId copyWithPeriodUid(Object newPeriodUid) { public MediaPeriodId copyWithPeriodUid(Object newPeriodUid) {
return periodUid.equals(newPeriodUid) return new MediaPeriodId(super.copyWithPeriodUid(newPeriodUid));
? this
: new MediaPeriodId(
newPeriodUid, adGroupIndex, adIndexInAdGroup, windowSequenceNumber, nextAdGroupIndex);
}
/**
* Returns whether this period identifier identifies an ad in an ad group in a period.
*/
public boolean isAd() {
return adGroupIndex != C.INDEX_UNSET;
}
@Override
public boolean equals(@Nullable Object obj) {
if (this == obj) {
return true;
}
if (obj == null || getClass() != obj.getClass()) {
return false;
}
MediaPeriodId periodId = (MediaPeriodId) obj;
return periodUid.equals(periodId.periodUid)
&& adGroupIndex == periodId.adGroupIndex
&& adIndexInAdGroup == periodId.adIndexInAdGroup
&& windowSequenceNumber == periodId.windowSequenceNumber
&& nextAdGroupIndex == periodId.nextAdGroupIndex;
}
@Override
public int hashCode() {
int result = 17;
result = 31 * result + periodUid.hashCode();
result = 31 * result + adGroupIndex;
result = 31 * result + adIndexInAdGroup;
result = 31 * result + (int) windowSequenceNumber;
result = 31 * result + nextAdGroupIndex;
return result;
} }
} }
......
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