Commit 5b0e971f by bachinger Committed by Andrew Lewis

Make SingleSampleMediaSource provide a media item

This is part of go/exoplayer-playlist-retrieval which aims for all MediaSources to associate the media item to the corresponding window. Media items need to be added to the timeline which then assigns it to the window.mediaItem attribute.

PiperOrigin-RevId: 312065023
parent ef615754
...@@ -22,7 +22,6 @@ import android.util.SparseArray; ...@@ -22,7 +22,6 @@ import android.util.SparseArray;
import androidx.annotation.Nullable; import androidx.annotation.Nullable;
import com.google.android.exoplayer2.C; import com.google.android.exoplayer2.C;
import com.google.android.exoplayer2.ExoPlayerLibraryInfo; import com.google.android.exoplayer2.ExoPlayerLibraryInfo;
import com.google.android.exoplayer2.Format;
import com.google.android.exoplayer2.MediaItem; import com.google.android.exoplayer2.MediaItem;
import com.google.android.exoplayer2.drm.DefaultDrmSessionManager; import com.google.android.exoplayer2.drm.DefaultDrmSessionManager;
import com.google.android.exoplayer2.drm.DrmSessionManager; import com.google.android.exoplayer2.drm.DrmSessionManager;
...@@ -299,16 +298,9 @@ public final class DefaultMediaSourceFactory implements MediaSourceFactory { ...@@ -299,16 +298,9 @@ public final class DefaultMediaSourceFactory implements MediaSourceFactory {
SingleSampleMediaSource.Factory singleSampleSourceFactory = SingleSampleMediaSource.Factory singleSampleSourceFactory =
new SingleSampleMediaSource.Factory(dataSourceFactory); new SingleSampleMediaSource.Factory(dataSourceFactory);
for (int i = 0; i < subtitles.size(); i++) { for (int i = 0; i < subtitles.size(); i++) {
MediaItem.Subtitle subtitle = subtitles.get(i);
Format subtitleFormat =
new Format.Builder()
.setSampleMimeType(subtitle.mimeType)
.setLanguage(subtitle.language)
.setSelectionFlags(subtitle.selectionFlags)
.build();
mediaSources[i + 1] = mediaSources[i + 1] =
singleSampleSourceFactory.createMediaSource( singleSampleSourceFactory.createMediaSource(
subtitle.uri, subtitleFormat, /* durationUs= */ C.TIME_UNSET); subtitles.get(i), /* durationUs= */ C.TIME_UNSET);
} }
mediaSource = new MergingMediaSource(mediaSources); mediaSource = new MergingMediaSource(mediaSources);
} }
......
...@@ -15,10 +15,15 @@ ...@@ -15,10 +15,15 @@
*/ */
package com.google.android.exoplayer2.source; package com.google.android.exoplayer2.source;
import static com.google.android.exoplayer2.util.Assertions.checkNotNull;
import static com.google.android.exoplayer2.util.Util.castNonNull;
import android.net.Uri; import android.net.Uri;
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.Format; import com.google.android.exoplayer2.Format;
import com.google.android.exoplayer2.MediaItem;
import com.google.android.exoplayer2.Timeline; import com.google.android.exoplayer2.Timeline;
import com.google.android.exoplayer2.upstream.Allocator; import com.google.android.exoplayer2.upstream.Allocator;
import com.google.android.exoplayer2.upstream.DataSource; import com.google.android.exoplayer2.upstream.DataSource;
...@@ -26,8 +31,8 @@ import com.google.android.exoplayer2.upstream.DataSpec; ...@@ -26,8 +31,8 @@ import com.google.android.exoplayer2.upstream.DataSpec;
import com.google.android.exoplayer2.upstream.DefaultLoadErrorHandlingPolicy; import com.google.android.exoplayer2.upstream.DefaultLoadErrorHandlingPolicy;
import com.google.android.exoplayer2.upstream.LoadErrorHandlingPolicy; import com.google.android.exoplayer2.upstream.LoadErrorHandlingPolicy;
import com.google.android.exoplayer2.upstream.TransferListener; import com.google.android.exoplayer2.upstream.TransferListener;
import com.google.android.exoplayer2.util.Assertions;
import java.io.IOException; import java.io.IOException;
import java.util.Collections;
/** /**
* Loads data at a given {@link Uri} as a single sample belonging to a single {@link MediaPeriod}. * Loads data at a given {@link Uri} as a single sample belonging to a single {@link MediaPeriod}.
...@@ -60,6 +65,7 @@ public final class SingleSampleMediaSource extends BaseMediaSource { ...@@ -60,6 +65,7 @@ public final class SingleSampleMediaSource extends BaseMediaSource {
private LoadErrorHandlingPolicy loadErrorHandlingPolicy; private LoadErrorHandlingPolicy loadErrorHandlingPolicy;
private boolean treatLoadErrorsAsEndOfStream; private boolean treatLoadErrorsAsEndOfStream;
@Nullable private Object tag; @Nullable private Object tag;
@Nullable private String trackId;
/** /**
* Creates a factory for {@link SingleSampleMediaSource}s. * Creates a factory for {@link SingleSampleMediaSource}s.
...@@ -68,7 +74,7 @@ public final class SingleSampleMediaSource extends BaseMediaSource { ...@@ -68,7 +74,7 @@ public final class SingleSampleMediaSource extends BaseMediaSource {
* be obtained. * be obtained.
*/ */
public Factory(DataSource.Factory dataSourceFactory) { public Factory(DataSource.Factory dataSourceFactory) {
this.dataSourceFactory = Assertions.checkNotNull(dataSourceFactory); this.dataSourceFactory = checkNotNull(dataSourceFactory);
loadErrorHandlingPolicy = new DefaultLoadErrorHandlingPolicy(); loadErrorHandlingPolicy = new DefaultLoadErrorHandlingPolicy();
} }
...@@ -78,7 +84,6 @@ public final class SingleSampleMediaSource extends BaseMediaSource { ...@@ -78,7 +84,6 @@ public final class SingleSampleMediaSource extends BaseMediaSource {
* *
* @param tag A tag for the media source. * @param tag A tag for the media source.
* @return This factory, for convenience. * @return This factory, for convenience.
* @throws IllegalStateException If one of the {@code create} methods has already been called.
*/ */
public Factory setTag(@Nullable Object tag) { public Factory setTag(@Nullable Object tag) {
this.tag = tag; this.tag = tag;
...@@ -86,6 +91,17 @@ public final class SingleSampleMediaSource extends BaseMediaSource { ...@@ -86,6 +91,17 @@ public final class SingleSampleMediaSource extends BaseMediaSource {
} }
/** /**
* Sets an optional track id to be used.
*
* @param trackId An optional track id.
* @return This factory, for convenience.
*/
public Factory setTrackId(@Nullable String trackId) {
this.trackId = trackId;
return this;
}
/**
* Sets the minimum number of times to retry if a loading error occurs. See {@link * Sets the minimum number of times to retry if a loading error occurs. See {@link
* #setLoadErrorHandlingPolicy} for the default value. * #setLoadErrorHandlingPolicy} for the default value.
* *
...@@ -95,7 +111,6 @@ public final class SingleSampleMediaSource extends BaseMediaSource { ...@@ -95,7 +111,6 @@ public final class SingleSampleMediaSource extends BaseMediaSource {
* *
* @param minLoadableRetryCount The minimum number of times to retry if a loading error occurs. * @param minLoadableRetryCount The minimum number of times to retry if a loading error occurs.
* @return This factory, for convenience. * @return This factory, for convenience.
* @throws IllegalStateException If one of the {@code create} methods has already been called.
* @deprecated Use {@link #setLoadErrorHandlingPolicy(LoadErrorHandlingPolicy)} instead. * @deprecated Use {@link #setLoadErrorHandlingPolicy(LoadErrorHandlingPolicy)} instead.
*/ */
@Deprecated @Deprecated
...@@ -111,7 +126,6 @@ public final class SingleSampleMediaSource extends BaseMediaSource { ...@@ -111,7 +126,6 @@ public final class SingleSampleMediaSource extends BaseMediaSource {
* *
* @param loadErrorHandlingPolicy A {@link LoadErrorHandlingPolicy}. * @param loadErrorHandlingPolicy A {@link LoadErrorHandlingPolicy}.
* @return This factory, for convenience. * @return This factory, for convenience.
* @throws IllegalStateException If one of the {@code create} methods has already been called.
*/ */
public Factory setLoadErrorHandlingPolicy( public Factory setLoadErrorHandlingPolicy(
@Nullable LoadErrorHandlingPolicy loadErrorHandlingPolicy) { @Nullable LoadErrorHandlingPolicy loadErrorHandlingPolicy) {
...@@ -130,7 +144,6 @@ public final class SingleSampleMediaSource extends BaseMediaSource { ...@@ -130,7 +144,6 @@ public final class SingleSampleMediaSource extends BaseMediaSource {
* streams, treating them as ended instead. If false, load errors will be propagated * streams, treating them as ended instead. If false, load errors will be propagated
* normally by {@link SampleStream#maybeThrowError()}. * normally by {@link SampleStream#maybeThrowError()}.
* @return This factory, for convenience. * @return This factory, for convenience.
* @throws IllegalStateException If one of the {@code create} methods has already been called.
*/ */
public Factory setTreatLoadErrorsAsEndOfStream(boolean treatLoadErrorsAsEndOfStream) { public Factory setTreatLoadErrorsAsEndOfStream(boolean treatLoadErrorsAsEndOfStream) {
this.treatLoadErrorsAsEndOfStream = treatLoadErrorsAsEndOfStream; this.treatLoadErrorsAsEndOfStream = treatLoadErrorsAsEndOfStream;
...@@ -140,40 +153,34 @@ public final class SingleSampleMediaSource extends BaseMediaSource { ...@@ -140,40 +153,34 @@ public final class SingleSampleMediaSource extends BaseMediaSource {
/** /**
* Returns a new {@link SingleSampleMediaSource} using the current parameters. * Returns a new {@link SingleSampleMediaSource} using the current parameters.
* *
* @param uri The {@link Uri}. * @param subtitle The {@link MediaItem.Subtitle}.
* @param format The {@link Format} of the media stream.
* @param durationUs The duration of the media stream in microseconds. * @param durationUs The duration of the media stream in microseconds.
* @return The new {@link SingleSampleMediaSource}. * @return The new {@link SingleSampleMediaSource}.
*/ */
public SingleSampleMediaSource createMediaSource(Uri uri, Format format, long durationUs) { public SingleSampleMediaSource createMediaSource(MediaItem.Subtitle subtitle, long durationUs) {
return new SingleSampleMediaSource( return new SingleSampleMediaSource(
uri, trackId,
subtitle,
dataSourceFactory, dataSourceFactory,
format,
durationUs, durationUs,
loadErrorHandlingPolicy, loadErrorHandlingPolicy,
treatLoadErrorsAsEndOfStream, treatLoadErrorsAsEndOfStream,
tag); tag);
} }
/** /** @deprecated Use {@link #createMediaSource(MediaItem.Subtitle, long)} instead. */
* @deprecated Use {@link #createMediaSource(Uri, Format, long)} and {@link
* #addEventListener(Handler, MediaSourceEventListener)} instead.
*/
@Deprecated @Deprecated
public SingleSampleMediaSource createMediaSource( public SingleSampleMediaSource createMediaSource(Uri uri, Format format, long durationUs) {
Uri uri, return new SingleSampleMediaSource(
Format format, format.id == null ? trackId : format.id,
long durationUs, new MediaItem.Subtitle(
@Nullable Handler eventHandler, uri, checkNotNull(format.sampleMimeType), format.language, format.selectionFlags),
@Nullable MediaSourceEventListener eventListener) { dataSourceFactory,
SingleSampleMediaSource mediaSource = createMediaSource(uri, format, durationUs); durationUs,
if (eventHandler != null && eventListener != null) { loadErrorHandlingPolicy,
mediaSource.addEventListener(eventHandler, eventListener); treatLoadErrorsAsEndOfStream,
} tag);
return mediaSource;
} }
} }
private final DataSpec dataSpec; private final DataSpec dataSpec;
...@@ -183,18 +190,11 @@ public final class SingleSampleMediaSource extends BaseMediaSource { ...@@ -183,18 +190,11 @@ public final class SingleSampleMediaSource extends BaseMediaSource {
private final LoadErrorHandlingPolicy loadErrorHandlingPolicy; private final LoadErrorHandlingPolicy loadErrorHandlingPolicy;
private final boolean treatLoadErrorsAsEndOfStream; private final boolean treatLoadErrorsAsEndOfStream;
private final Timeline timeline; private final Timeline timeline;
@Nullable private final Object tag; private final MediaItem mediaItem;
@Nullable private TransferListener transferListener; @Nullable private TransferListener transferListener;
/** /** @deprecated Use {@link Factory} instead. */
* @param uri The {@link Uri} of the media stream.
* @param dataSourceFactory The factory from which the {@link DataSource} to read the media will
* be obtained.
* @param format The {@link Format} associated with the output track.
* @param durationUs The duration of the media stream in microseconds.
* @deprecated Use {@link Factory} instead.
*/
@Deprecated @Deprecated
@SuppressWarnings("deprecation") @SuppressWarnings("deprecation")
public SingleSampleMediaSource( public SingleSampleMediaSource(
...@@ -207,15 +207,8 @@ public final class SingleSampleMediaSource extends BaseMediaSource { ...@@ -207,15 +207,8 @@ public final class SingleSampleMediaSource extends BaseMediaSource {
DefaultLoadErrorHandlingPolicy.DEFAULT_MIN_LOADABLE_RETRY_COUNT); DefaultLoadErrorHandlingPolicy.DEFAULT_MIN_LOADABLE_RETRY_COUNT);
} }
/** /** @deprecated Use {@link Factory} instead. */
* @param uri The {@link Uri} of the media stream. @SuppressWarnings("deprecation")
* @param dataSourceFactory The factory from which the {@link DataSource} to read the media will
* be obtained.
* @param format The {@link Format} associated with the output track.
* @param durationUs The duration of the media stream in microseconds.
* @param minLoadableRetryCount The minimum number of times to retry if a loading error occurs.
* @deprecated Use {@link Factory} instead.
*/
@Deprecated @Deprecated
public SingleSampleMediaSource( public SingleSampleMediaSource(
Uri uri, Uri uri,
...@@ -228,28 +221,15 @@ public final class SingleSampleMediaSource extends BaseMediaSource { ...@@ -228,28 +221,15 @@ public final class SingleSampleMediaSource extends BaseMediaSource {
dataSourceFactory, dataSourceFactory,
format, format,
durationUs, durationUs,
new DefaultLoadErrorHandlingPolicy(minLoadableRetryCount), minLoadableRetryCount,
/* treatLoadErrorsAsEndOfStream= */ false, /* eventHandler= */ null,
/* tag= */ null); /* eventListener= */ null,
/* ignored */ C.INDEX_UNSET,
/* treatLoadErrorsAsEndOfStream= */ false);
} }
/** /** @deprecated Use {@link Factory} instead. */
* @param uri The {@link Uri} of the media stream.
* @param dataSourceFactory The factory from which the {@link DataSource} to read the media will
* be obtained.
* @param format The {@link Format} associated with the output track.
* @param durationUs The duration of the media stream in microseconds.
* @param minLoadableRetryCount The minimum number of times to retry if a loading error occurs.
* @param eventHandler A handler for events. May be null if delivery of events is not required.
* @param eventListener A listener of events. May be null if delivery of events is not required.
* @param eventSourceId An identifier that gets passed to {@code eventListener} methods.
* @param treatLoadErrorsAsEndOfStream If true, load errors will not be propagated by sample
* streams, treating them as ended instead. If false, load errors will be propagated normally
* by {@link SampleStream#maybeThrowError()}.
* @deprecated Use {@link Factory} instead.
*/
@Deprecated @Deprecated
@SuppressWarnings("deprecation")
public SingleSampleMediaSource( public SingleSampleMediaSource(
Uri uri, Uri uri,
DataSource.Factory dataSourceFactory, DataSource.Factory dataSourceFactory,
...@@ -261,9 +241,10 @@ public final class SingleSampleMediaSource extends BaseMediaSource { ...@@ -261,9 +241,10 @@ public final class SingleSampleMediaSource extends BaseMediaSource {
int eventSourceId, int eventSourceId,
boolean treatLoadErrorsAsEndOfStream) { boolean treatLoadErrorsAsEndOfStream) {
this( this(
uri, /* trackId= */ null,
new MediaItem.Subtitle(
uri, checkNotNull(format.sampleMimeType), format.language, format.selectionFlags),
dataSourceFactory, dataSourceFactory,
format,
durationUs, durationUs,
new DefaultLoadErrorHandlingPolicy(minLoadableRetryCount), new DefaultLoadErrorHandlingPolicy(minLoadableRetryCount),
treatLoadErrorsAsEndOfStream, treatLoadErrorsAsEndOfStream,
...@@ -274,20 +255,33 @@ public final class SingleSampleMediaSource extends BaseMediaSource { ...@@ -274,20 +255,33 @@ public final class SingleSampleMediaSource extends BaseMediaSource {
} }
private SingleSampleMediaSource( private SingleSampleMediaSource(
Uri uri, @Nullable String trackId,
MediaItem.Subtitle subtitle,
DataSource.Factory dataSourceFactory, DataSource.Factory dataSourceFactory,
Format format,
long durationUs, long durationUs,
LoadErrorHandlingPolicy loadErrorHandlingPolicy, LoadErrorHandlingPolicy loadErrorHandlingPolicy,
boolean treatLoadErrorsAsEndOfStream, boolean treatLoadErrorsAsEndOfStream,
@Nullable Object tag) { @Nullable Object tag) {
this.dataSourceFactory = dataSourceFactory; this.dataSourceFactory = dataSourceFactory;
this.format = format;
this.durationUs = durationUs; this.durationUs = durationUs;
this.loadErrorHandlingPolicy = loadErrorHandlingPolicy; this.loadErrorHandlingPolicy = loadErrorHandlingPolicy;
this.treatLoadErrorsAsEndOfStream = treatLoadErrorsAsEndOfStream; this.treatLoadErrorsAsEndOfStream = treatLoadErrorsAsEndOfStream;
this.tag = tag; mediaItem =
dataSpec = new DataSpec.Builder().setUri(uri).setFlags(DataSpec.FLAG_ALLOW_GZIP).build(); new MediaItem.Builder()
.setUri(Uri.EMPTY)
.setMediaId(subtitle.uri.toString())
.setSubtitles(Collections.singletonList(subtitle))
.setTag(tag)
.build();
format =
new Format.Builder()
.setId(trackId)
.setSampleMimeType(subtitle.mimeType)
.setLanguage(subtitle.language)
.setSelectionFlags(subtitle.selectionFlags)
.build();
dataSpec =
new DataSpec.Builder().setUri(subtitle.uri).setFlags(DataSpec.FLAG_ALLOW_GZIP).build();
timeline = timeline =
new SinglePeriodTimeline( new SinglePeriodTimeline(
durationUs, durationUs,
...@@ -295,7 +289,7 @@ public final class SingleSampleMediaSource extends BaseMediaSource { ...@@ -295,7 +289,7 @@ public final class SingleSampleMediaSource extends BaseMediaSource {
/* isDynamic= */ false, /* isDynamic= */ false,
/* isLive= */ false, /* isLive= */ false,
/* manifest= */ null, /* manifest= */ null,
tag); mediaItem);
} }
// MediaSource implementation. // MediaSource implementation.
...@@ -303,7 +297,12 @@ public final class SingleSampleMediaSource extends BaseMediaSource { ...@@ -303,7 +297,12 @@ public final class SingleSampleMediaSource extends BaseMediaSource {
@Override @Override
@Nullable @Nullable
public Object getTag() { public Object getTag() {
return tag; return castNonNull(mediaItem.playbackProperties).tag;
}
// TODO(bachinger) Add @Override annotation once the method is defined by MediaSource.
public MediaItem getMediaItem() {
return mediaItem;
} }
@Override @Override
...@@ -352,7 +351,7 @@ public final class SingleSampleMediaSource extends BaseMediaSource { ...@@ -352,7 +351,7 @@ public final class SingleSampleMediaSource extends BaseMediaSource {
private final int eventSourceId; private final int eventSourceId;
public EventListenerWrapper(EventListener eventListener, int eventSourceId) { public EventListenerWrapper(EventListener eventListener, int eventSourceId) {
this.eventListener = Assertions.checkNotNull(eventListener); this.eventListener = checkNotNull(eventListener);
this.eventSourceId = eventSourceId; this.eventSourceId = eventSourceId;
} }
......
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