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
This commit is contained in:
hoangtc 2017-11-15 03:08:11 -08:00 committed by Oliver Woodman
parent 5301d38136
commit 28df0e133b
3 changed files with 141 additions and 4 deletions

View File

@ -167,7 +167,9 @@ import com.google.android.gms.cast.framework.CastContext;
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);

View File

@ -374,7 +374,10 @@ public class PlayerActivity extends Activity implements OnClickListener,
.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);

View File

@ -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.
*/
@ -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 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,
int minLoadableRetryCount, Handler eventHandler,
AdaptiveMediaSourceEventListener eventListener) {
@ -105,7 +235,9 @@ public final class HlsMediaSource implements MediaSource,
* @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,