Skip to content

Commit

Permalink
Add Builder pattern to HlsMediaSource.
Browse files Browse the repository at this point in the history
Add Builder pattern to HlsMediaSource and mark existing constructors as
deprecated.

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=175803853
  • Loading branch information
botaydotcom authored and ojw28 committed Nov 17, 2017
1 parent 5301d38 commit 28df0e1
Show file tree
Hide file tree
Showing 3 changed files with 141 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<HlsPlaylist> 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<HlsPlaylist> playlistParser) {
this.playlistParser = playlistParser;
return this;
}

/**
* Builds 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}.
*/
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.
*/
Expand All @@ -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,
Expand All @@ -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) {
Expand All @@ -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<HlsPlaylist> playlistParser) {
this.manifestUri = manifestUri;
this.dataSourceFactory = dataSourceFactory;
Expand Down

0 comments on commit 28df0e1

Please sign in to comment.