From 28df0e133b909832feca142e13f63bcd5fa21121 Mon Sep 17 00:00:00 2001 From: hoangtc Date: Wed, 15 Nov 2017 03:08:11 -0800 Subject: [PATCH] Add Builder pattern to HlsMediaSource. Add Builder pattern to HlsMediaSource and mark existing constructors as deprecated. ------------- Created by MOE: https://github.com/google/moe MOE_MIGRATED_REVID=175803853 --- .../exoplayer2/castdemo/PlayerManager.java | 4 +- .../exoplayer2/demo/PlayerActivity.java | 5 +- .../exoplayer2/source/hls/HlsMediaSource.java | 136 +++++++++++++++++- 3 files changed, 141 insertions(+), 4 deletions(-) diff --git a/demos/cast/src/main/java/com/google/android/exoplayer2/castdemo/PlayerManager.java b/demos/cast/src/main/java/com/google/android/exoplayer2/castdemo/PlayerManager.java index a10692bca7e..1dfe153c972 100644 --- a/demos/cast/src/main/java/com/google/android/exoplayer2/castdemo/PlayerManager.java +++ b/demos/cast/src/main/java/com/google/android/exoplayer2/castdemo/PlayerManager.java @@ -167,7 +167,9 @@ private static MediaSource buildMediaSource(DemoUtil.Sample sample) { new DefaultDashChunkSource.Factory(DATA_SOURCE_FACTORY)) .build(); case DemoUtil.MIME_TYPE_HLS: - return new HlsMediaSource(uri, DATA_SOURCE_FACTORY, null, null); + return HlsMediaSource.Builder + .forDataSource(uri, DATA_SOURCE_FACTORY) + .build(); case DemoUtil.MIME_TYPE_VIDEO_MP4: return new ExtractorMediaSource(uri, DATA_SOURCE_FACTORY, new DefaultExtractorsFactory(), null, null); diff --git a/demos/main/src/main/java/com/google/android/exoplayer2/demo/PlayerActivity.java b/demos/main/src/main/java/com/google/android/exoplayer2/demo/PlayerActivity.java index 65e1c0e0831..3d669c94775 100644 --- a/demos/main/src/main/java/com/google/android/exoplayer2/demo/PlayerActivity.java +++ b/demos/main/src/main/java/com/google/android/exoplayer2/demo/PlayerActivity.java @@ -374,7 +374,10 @@ private MediaSource buildMediaSource(Uri uri, String overrideExtension) { .setEventListener(mainHandler, eventLogger) .build(); case C.TYPE_HLS: - return new HlsMediaSource(uri, mediaDataSourceFactory, mainHandler, eventLogger); + return HlsMediaSource.Builder + .forDataSource(uri, mediaDataSourceFactory) + .setEventListener(mainHandler, eventLogger) + .build(); case C.TYPE_OTHER: return new ExtractorMediaSource(uri, mediaDataSourceFactory, new DefaultExtractorsFactory(), mainHandler, eventLogger); diff --git a/library/hls/src/main/java/com/google/android/exoplayer2/source/hls/HlsMediaSource.java b/library/hls/src/main/java/com/google/android/exoplayer2/source/hls/HlsMediaSource.java index 21b27e655de..3f28981f0e5 100644 --- a/library/hls/src/main/java/com/google/android/exoplayer2/source/hls/HlsMediaSource.java +++ b/library/hls/src/main/java/com/google/android/exoplayer2/source/hls/HlsMediaSource.java @@ -47,6 +47,132 @@ public final class HlsMediaSource implements MediaSource, ExoPlayerLibraryInfo.registerModule("goog.exo.hls"); } + /** + * Builder for {@link HlsMediaSource}. Each builder instance can only be used once. + */ + public static final class Builder { + + private final Uri manifestUri; + private final HlsDataSourceFactory hlsDataSourceFactory; + + private HlsExtractorFactory extractorFactory; + private ParsingLoadable.Parser playlistParser; + private AdaptiveMediaSourceEventListener eventListener; + private Handler eventHandler; + private int minLoadableRetryCount; + private boolean isBuildCalled; + + /** + * Creates a {@link Builder} for a {@link HlsMediaSource} with a loadable manifest Uri and + * 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 DefaultHlsDataSourceFactory} to build {@link DataSource}s for manifests, + * segments and keys. + * @return A new builder. + */ + public static Builder forDataSource(Uri manifestUri, DataSource.Factory dataSourceFactory) { + return new Builder(manifestUri, new DefaultHlsDataSourceFactory(dataSourceFactory)); + } + + /** + * Creates a {@link Builder} for a {@link HlsMediaSource} with a loadable manifest Uri and + * a {@link HlsDataSourceFactory}. + * + * @param manifestUri The {@link Uri} of the HLS manifest. + * @param dataSourceFactory An {@link HlsDataSourceFactory} for {@link DataSource}s for + * manifests, segments and keys. + * @return A new builder. + */ + public static Builder forHlsDataSource(Uri manifestUri, + HlsDataSourceFactory dataSourceFactory) { + return new Builder(manifestUri, dataSourceFactory); + } + + private Builder(Uri manifestUri, HlsDataSourceFactory hlsDataSourceFactory) { + this.manifestUri = manifestUri; + this.hlsDataSourceFactory = hlsDataSourceFactory; + + minLoadableRetryCount = DEFAULT_MIN_LOADABLE_RETRY_COUNT; + } + + /** + * Sets the factory for {@link Extractor}s for the segments. Default value is + * {@link HlsExtractorFactory#DEFAULT}. + * + * @param extractorFactory An {@link HlsExtractorFactory} for {@link Extractor}s for the + * segments. + * @return This builder. + */ + public Builder setExtractorFactory(HlsExtractorFactory extractorFactory) { + this.extractorFactory = extractorFactory; + return this; + } + + /** + * Sets the minimum number of times to retry if a loading error occurs. The default value is + * {@link #DEFAULT_MIN_LOADABLE_RETRY_COUNT}. + * + * @param minLoadableRetryCount The minimum number of times loads must be retried before + * errors are propagated. + * @return This builder. + */ + public Builder setMinLoadableRetryCount(int minLoadableRetryCount) { + this.minLoadableRetryCount = minLoadableRetryCount; + return this; + } + + /** + * Sets the listener to respond to adaptive {@link MediaSource} events and the handler to + * deliver these events. + * + * @param eventHandler A handler for events. + * @param eventListener A listener of events. + * @return This builder. + */ + public Builder setEventListener(Handler eventHandler, + AdaptiveMediaSourceEventListener eventListener) { + this.eventHandler = eventHandler; + this.eventListener = eventListener; + return this; + } + + /** + * Sets the parser to parse HLS playlists. The default is an instance of + * {@link HlsPlaylistParser}. + * + * @param playlistParser A {@link ParsingLoadable.Parser} for HLS playlists. + * @return This builder. + */ + public Builder setPlaylistParser(ParsingLoadable.Parser playlistParser) { + this.playlistParser = playlistParser; + return this; + } + + /** + * Builds a new {@link HlsMediaSource} using the current parameters. + *

+ * After this call, the builder should not be re-used. + * + * @return The newly built {@link HlsMediaSource}. + */ + public HlsMediaSource build() { + Assertions.checkArgument((eventListener == null) == (eventHandler == null)); + Assertions.checkState(!isBuildCalled); + isBuildCalled = true; + if (extractorFactory == null) { + extractorFactory = HlsExtractorFactory.DEFAULT; + } + if (playlistParser == null) { + playlistParser = new HlsPlaylistParser(); + } + return new HlsMediaSource(manifestUri, hlsDataSourceFactory, extractorFactory, + minLoadableRetryCount, eventHandler, eventListener, playlistParser); + } + + } + /** * The default minimum number of times to retry loading data prior to failing. */ @@ -69,7 +195,9 @@ public final class HlsMediaSource implements MediaSource, * @param eventHandler A handler for events. May be null if delivery of events is not required. * @param eventListener An {@link AdaptiveMediaSourceEventListener}. May be null if delivery of * events is not required. + * @deprecated Use {@link Builder} instead. */ + @Deprecated public HlsMediaSource(Uri manifestUri, DataSource.Factory dataSourceFactory, Handler eventHandler, AdaptiveMediaSourceEventListener eventListener) { this(manifestUri, dataSourceFactory, DEFAULT_MIN_LOADABLE_RETRY_COUNT, eventHandler, @@ -85,7 +213,9 @@ public HlsMediaSource(Uri manifestUri, DataSource.Factory dataSourceFactory, Han * @param eventHandler A handler for events. May be null if delivery of events is not required. * @param eventListener An {@link AdaptiveMediaSourceEventListener}. May be null if delivery of * events is not required. + * @deprecated Use {@link Builder} instead. */ + @Deprecated public HlsMediaSource(Uri manifestUri, DataSource.Factory dataSourceFactory, int minLoadableRetryCount, Handler eventHandler, AdaptiveMediaSourceEventListener eventListener) { @@ -105,10 +235,12 @@ public HlsMediaSource(Uri manifestUri, DataSource.Factory dataSourceFactory, * @param eventListener An {@link AdaptiveMediaSourceEventListener}. May be null if delivery of * events is not required. * @param playlistParser A {@link ParsingLoadable.Parser} for HLS playlists. + * @deprecated Use {@link Builder} instead. */ + @Deprecated public HlsMediaSource(Uri manifestUri, HlsDataSourceFactory dataSourceFactory, - HlsExtractorFactory extractorFactory, int minLoadableRetryCount, Handler eventHandler, - AdaptiveMediaSourceEventListener eventListener, + HlsExtractorFactory extractorFactory, int minLoadableRetryCount, Handler eventHandler, + AdaptiveMediaSourceEventListener eventListener, ParsingLoadable.Parser playlistParser) { this.manifestUri = manifestUri; this.dataSourceFactory = dataSourceFactory;