Commit 42b612a4 by andrewlewis Committed by Oliver Woodman

Make HlsMediaSource.Builder a factory for HlsMediaSources

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=178384204
parent 4c71d636
...@@ -375,10 +375,8 @@ public class PlayerActivity extends Activity implements OnClickListener, ...@@ -375,10 +375,8 @@ public class PlayerActivity extends Activity implements OnClickListener,
buildDataSourceFactory(false)) buildDataSourceFactory(false))
.createMediaSource(uri, mainHandler, eventLogger); .createMediaSource(uri, mainHandler, eventLogger);
case C.TYPE_HLS: case C.TYPE_HLS:
return HlsMediaSource.Builder return new HlsMediaSource.Factory(mediaDataSourceFactory)
.forDataSource(uri, mediaDataSourceFactory) .createMediaSource(uri, mainHandler, eventLogger);
.setEventListener(mainHandler, eventLogger)
.build();
case C.TYPE_OTHER: case C.TYPE_OTHER:
return new ExtractorMediaSource.Builder(uri, mediaDataSourceFactory) return new ExtractorMediaSource.Builder(uri, mediaDataSourceFactory)
.setEventListener(mainHandler, eventLogger) .setEventListener(mainHandler, eventLogger)
......
...@@ -17,6 +17,7 @@ package com.google.android.exoplayer2.source.hls; ...@@ -17,6 +17,7 @@ package com.google.android.exoplayer2.source.hls;
import android.net.Uri; import android.net.Uri;
import android.os.Handler; import android.os.Handler;
import android.support.annotation.Nullable;
import com.google.android.exoplayer2.C; import com.google.android.exoplayer2.C;
import com.google.android.exoplayer2.ExoPlayer; import com.google.android.exoplayer2.ExoPlayer;
import com.google.android.exoplayer2.ExoPlayerLibraryInfo; import com.google.android.exoplayer2.ExoPlayerLibraryInfo;
...@@ -26,6 +27,7 @@ import com.google.android.exoplayer2.source.MediaSource; ...@@ -26,6 +27,7 @@ import com.google.android.exoplayer2.source.MediaSource;
import com.google.android.exoplayer2.source.MediaSourceEventListener; import com.google.android.exoplayer2.source.MediaSourceEventListener;
import com.google.android.exoplayer2.source.MediaSourceEventListener.EventDispatcher; import com.google.android.exoplayer2.source.MediaSourceEventListener.EventDispatcher;
import com.google.android.exoplayer2.source.SinglePeriodTimeline; import com.google.android.exoplayer2.source.SinglePeriodTimeline;
import com.google.android.exoplayer2.source.ads.AdsMediaSource;
import com.google.android.exoplayer2.source.hls.playlist.HlsMediaPlaylist; import com.google.android.exoplayer2.source.hls.playlist.HlsMediaPlaylist;
import com.google.android.exoplayer2.source.hls.playlist.HlsPlaylist; import com.google.android.exoplayer2.source.hls.playlist.HlsPlaylist;
import com.google.android.exoplayer2.source.hls.playlist.HlsPlaylistParser; import com.google.android.exoplayer2.source.hls.playlist.HlsPlaylistParser;
...@@ -47,66 +49,51 @@ public final class HlsMediaSource implements MediaSource, ...@@ -47,66 +49,51 @@ public final class HlsMediaSource implements MediaSource,
ExoPlayerLibraryInfo.registerModule("goog.exo.hls"); ExoPlayerLibraryInfo.registerModule("goog.exo.hls");
} }
/** /** Factory for {@link HlsMediaSource}s. */
* Builder for {@link HlsMediaSource}. Each builder instance can only be used once. public static final class Factory implements AdsMediaSource.MediaSourceFactory {
*/
public static final class Builder {
private final Uri manifestUri;
private final HlsDataSourceFactory hlsDataSourceFactory; private final HlsDataSourceFactory hlsDataSourceFactory;
private HlsExtractorFactory extractorFactory; private HlsExtractorFactory extractorFactory;
private ParsingLoadable.Parser<HlsPlaylist> playlistParser; private @Nullable ParsingLoadable.Parser<HlsPlaylist> playlistParser;
private MediaSourceEventListener eventListener;
private Handler eventHandler;
private int minLoadableRetryCount; private int minLoadableRetryCount;
private boolean isBuildCalled; private boolean isCreateCalled;
/** /**
* Creates a {@link Builder} for a {@link HlsMediaSource} with a loadable manifest Uri and * Creates a new factory for {@link HlsMediaSource}s.
* a {@link DataSource.Factory}.
* *
* @param manifestUri The {@link Uri} of the HLS manifest. * @param dataSourceFactory A data source factory that will be wrapped by a {@link
* @param dataSourceFactory A data source factory that will be wrapped by a * DefaultHlsDataSourceFactory} to create {@link DataSource}s for manifests, segments and
* {@link DefaultHlsDataSourceFactory} to build {@link DataSource}s for manifests, * keys.
* segments and keys.
* @return A new builder.
*/ */
public static Builder forDataSource(Uri manifestUri, DataSource.Factory dataSourceFactory) { public Factory(DataSource.Factory dataSourceFactory) {
return new Builder(manifestUri, new DefaultHlsDataSourceFactory(dataSourceFactory)); this(new DefaultHlsDataSourceFactory(dataSourceFactory));
} }
/** /**
* Creates a {@link Builder} for a {@link HlsMediaSource} with a loadable manifest Uri and * Creates a new factory for {@link HlsMediaSource}s.
* a {@link HlsDataSourceFactory}.
* *
* @param manifestUri The {@link Uri} of the HLS manifest. * @param hlsDataSourceFactory An {@link HlsDataSourceFactory} for {@link DataSource}s for
* @param dataSourceFactory An {@link HlsDataSourceFactory} for {@link DataSource}s for
* manifests, segments and keys. * manifests, segments and keys.
* @return A new builder.
*/ */
public static Builder forHlsDataSource(Uri manifestUri, public Factory(HlsDataSourceFactory hlsDataSourceFactory) {
HlsDataSourceFactory dataSourceFactory) { this.hlsDataSourceFactory = Assertions.checkNotNull(hlsDataSourceFactory);
return new Builder(manifestUri, dataSourceFactory); extractorFactory = HlsExtractorFactory.DEFAULT;
}
private Builder(Uri manifestUri, HlsDataSourceFactory hlsDataSourceFactory) {
this.manifestUri = manifestUri;
this.hlsDataSourceFactory = hlsDataSourceFactory;
minLoadableRetryCount = DEFAULT_MIN_LOADABLE_RETRY_COUNT; minLoadableRetryCount = DEFAULT_MIN_LOADABLE_RETRY_COUNT;
} }
/** /**
* Sets the factory for {@link Extractor}s for the segments. Default value is * Sets the factory for {@link Extractor}s for the segments. The default value is {@link
* {@link HlsExtractorFactory#DEFAULT}. * HlsExtractorFactory#DEFAULT}.
* *
* @param extractorFactory An {@link HlsExtractorFactory} for {@link Extractor}s for the * @param extractorFactory An {@link HlsExtractorFactory} for {@link Extractor}s for the
* segments. * segments.
* @return This builder. * @return This factory, for convenience.
* @throws IllegalStateException If one of the {@code create} methods has already been called.
*/ */
public Builder setExtractorFactory(HlsExtractorFactory extractorFactory) { public Factory setExtractorFactory(HlsExtractorFactory extractorFactory) {
this.extractorFactory = extractorFactory; Assertions.checkState(!isCreateCalled);
this.extractorFactory = Assertions.checkNotNull(extractorFactory);
return this; return this;
} }
...@@ -114,63 +101,71 @@ public final class HlsMediaSource implements MediaSource, ...@@ -114,63 +101,71 @@ public final class HlsMediaSource implements MediaSource,
* Sets the minimum number of times to retry if a loading error occurs. The default value is * Sets the minimum number of times to retry if a loading error occurs. The default value is
* {@link #DEFAULT_MIN_LOADABLE_RETRY_COUNT}. * {@link #DEFAULT_MIN_LOADABLE_RETRY_COUNT}.
* *
* @param minLoadableRetryCount The minimum number of times loads must be retried before * @param minLoadableRetryCount The minimum number of times to retry if a loading error occurs.
* errors are propagated. * @return This factory, for convenience.
* @return This builder. * @throws IllegalStateException If one of the {@code create} methods has already been called.
*/ */
public Builder setMinLoadableRetryCount(int minLoadableRetryCount) { public Factory setMinLoadableRetryCount(int minLoadableRetryCount) {
Assertions.checkState(!isCreateCalled);
this.minLoadableRetryCount = minLoadableRetryCount; this.minLoadableRetryCount = minLoadableRetryCount;
return this; return this;
} }
/** /**
* Sets the listener to respond to adaptive {@link MediaSource} events and the handler to * Sets the parser to parse HLS playlists. The default is an instance of {@link
* deliver these events. * HlsPlaylistParser}.
* *
* @param eventHandler A handler for events. * @param playlistParser A {@link ParsingLoadable.Parser} for HLS playlists.
* @param eventListener A listener of events. * @return This factory, for convenience.
* @return This builder. * @throws IllegalStateException If one of the {@code create} methods has already been called.
*/ */
public Builder setEventListener(Handler eventHandler, public Factory setPlaylistParser(ParsingLoadable.Parser<HlsPlaylist> playlistParser) {
MediaSourceEventListener eventListener) { Assertions.checkState(!isCreateCalled);
this.eventHandler = eventHandler; this.playlistParser = Assertions.checkNotNull(playlistParser);
this.eventListener = eventListener;
return this; return this;
} }
/** /**
* Sets the parser to parse HLS playlists. The default is an instance of * Returns a new {@link HlsMediaSource} using the current parameters. Media source events will
* {@link HlsPlaylistParser}. * not be delivered.
* *
* @param playlistParser A {@link ParsingLoadable.Parser} for HLS playlists. * @return The new {@link HlsMediaSource}.
* @return This builder.
*/ */
public Builder setPlaylistParser(ParsingLoadable.Parser<HlsPlaylist> playlistParser) { public MediaSource createMediaSource(Uri playlistUri) {
this.playlistParser = playlistParser; return createMediaSource(playlistUri, null, null);
return this;
} }
/** /**
* Builds a new {@link HlsMediaSource} using the current parameters. * Returns a new {@link HlsMediaSource} using the current parameters.
* <p>
* After this call, the builder should not be re-used.
* *
* @return The newly built {@link HlsMediaSource}. * @param playlistUri The playlist {@link Uri}.
* @param eventHandler A handler for events.
* @param eventListener A listener of events.
* @return The new {@link HlsMediaSource}.
*/ */
public HlsMediaSource build() { @Override
Assertions.checkArgument((eventListener == null) == (eventHandler == null)); public MediaSource createMediaSource(
Assertions.checkState(!isBuildCalled); Uri playlistUri,
isBuildCalled = true; @Nullable Handler eventHandler,
if (extractorFactory == null) { @Nullable MediaSourceEventListener eventListener) {
extractorFactory = HlsExtractorFactory.DEFAULT; isCreateCalled = true;
}
if (playlistParser == null) { if (playlistParser == null) {
playlistParser = new HlsPlaylistParser(); playlistParser = new HlsPlaylistParser();
} }
return new HlsMediaSource(manifestUri, hlsDataSourceFactory, extractorFactory, return new HlsMediaSource(
minLoadableRetryCount, eventHandler, eventListener, playlistParser); playlistUri,
hlsDataSourceFactory,
extractorFactory,
minLoadableRetryCount,
eventHandler,
eventListener,
playlistParser);
} }
@Override
public int[] getSupportedTypes() {
return new int[] {C.TYPE_HLS};
}
} }
/** /**
...@@ -195,7 +190,7 @@ public final class HlsMediaSource implements MediaSource, ...@@ -195,7 +190,7 @@ public final class HlsMediaSource implements MediaSource,
* @param eventHandler A handler for events. May be null if delivery of events is not required. * @param eventHandler A handler for events. May be null if delivery of events is not required.
* @param eventListener An {@link MediaSourceEventListener}. May be null if delivery of events is * @param eventListener An {@link MediaSourceEventListener}. May be null if delivery of events is
* not required. * not required.
* @deprecated Use {@link Builder} instead. * @deprecated Use {@link Factory} instead.
*/ */
@Deprecated @Deprecated
public HlsMediaSource(Uri manifestUri, DataSource.Factory dataSourceFactory, Handler eventHandler, public HlsMediaSource(Uri manifestUri, DataSource.Factory dataSourceFactory, Handler eventHandler,
...@@ -213,7 +208,7 @@ public final class HlsMediaSource implements MediaSource, ...@@ -213,7 +208,7 @@ public final class HlsMediaSource implements MediaSource,
* @param eventHandler A handler for events. May be null if delivery of events is not required. * @param eventHandler A handler for events. May be null if delivery of events is not required.
* @param eventListener An {@link MediaSourceEventListener}. May be null if delivery of events is * @param eventListener An {@link MediaSourceEventListener}. May be null if delivery of events is
* not required. * not required.
* @deprecated Use {@link Builder} instead. * @deprecated Use {@link Factory} instead.
*/ */
@Deprecated @Deprecated
public HlsMediaSource(Uri manifestUri, DataSource.Factory dataSourceFactory, public HlsMediaSource(Uri manifestUri, DataSource.Factory dataSourceFactory,
...@@ -234,7 +229,7 @@ public final class HlsMediaSource implements MediaSource, ...@@ -234,7 +229,7 @@ public final class HlsMediaSource implements MediaSource,
* @param eventListener An {@link MediaSourceEventListener}. May be null if delivery of events is * @param eventListener An {@link MediaSourceEventListener}. May be null if delivery of events is
* not required. * not required.
* @param playlistParser A {@link ParsingLoadable.Parser} for HLS playlists. * @param playlistParser A {@link ParsingLoadable.Parser} for HLS playlists.
* @deprecated Use {@link Builder} instead. * @deprecated Use {@link Factory} instead.
*/ */
@Deprecated @Deprecated
public HlsMediaSource(Uri manifestUri, HlsDataSourceFactory dataSourceFactory, public HlsMediaSource(Uri manifestUri, HlsDataSourceFactory dataSourceFactory,
......
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