Commit 28df0e13 by hoangtc Committed by Oliver Woodman

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
parent 5301d381
...@@ -167,7 +167,9 @@ import com.google.android.gms.cast.framework.CastContext; ...@@ -167,7 +167,9 @@ import com.google.android.gms.cast.framework.CastContext;
new DefaultDashChunkSource.Factory(DATA_SOURCE_FACTORY)) new DefaultDashChunkSource.Factory(DATA_SOURCE_FACTORY))
.build(); .build();
case DemoUtil.MIME_TYPE_HLS: 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: case DemoUtil.MIME_TYPE_VIDEO_MP4:
return new ExtractorMediaSource(uri, DATA_SOURCE_FACTORY, new DefaultExtractorsFactory(), return new ExtractorMediaSource(uri, DATA_SOURCE_FACTORY, new DefaultExtractorsFactory(),
null, null); null, null);
......
...@@ -374,7 +374,10 @@ public class PlayerActivity extends Activity implements OnClickListener, ...@@ -374,7 +374,10 @@ public class PlayerActivity extends Activity implements OnClickListener,
.setEventListener(mainHandler, eventLogger) .setEventListener(mainHandler, eventLogger)
.build(); .build();
case C.TYPE_HLS: 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: case C.TYPE_OTHER:
return new ExtractorMediaSource(uri, mediaDataSourceFactory, new DefaultExtractorsFactory(), return new ExtractorMediaSource(uri, mediaDataSourceFactory, new DefaultExtractorsFactory(),
mainHandler, eventLogger); mainHandler, eventLogger);
......
...@@ -48,6 +48,132 @@ public final class HlsMediaSource implements MediaSource, ...@@ -48,6 +48,132 @@ public final class HlsMediaSource implements MediaSource,
} }
/** /**
* 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. * The default minimum number of times to retry loading data prior to failing.
*/ */
public static final int DEFAULT_MIN_LOADABLE_RETRY_COUNT = 3; public static final int DEFAULT_MIN_LOADABLE_RETRY_COUNT = 3;
...@@ -69,7 +195,9 @@ public final class HlsMediaSource implements MediaSource, ...@@ -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 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 * @param eventListener An {@link AdaptiveMediaSourceEventListener}. May be null if delivery of
* events is not required. * events is not required.
* @deprecated Use {@link Builder} instead.
*/ */
@Deprecated
public HlsMediaSource(Uri manifestUri, DataSource.Factory dataSourceFactory, Handler eventHandler, public HlsMediaSource(Uri manifestUri, DataSource.Factory dataSourceFactory, Handler eventHandler,
AdaptiveMediaSourceEventListener eventListener) { AdaptiveMediaSourceEventListener eventListener) {
this(manifestUri, dataSourceFactory, DEFAULT_MIN_LOADABLE_RETRY_COUNT, eventHandler, this(manifestUri, dataSourceFactory, DEFAULT_MIN_LOADABLE_RETRY_COUNT, eventHandler,
...@@ -85,7 +213,9 @@ public final class HlsMediaSource implements MediaSource, ...@@ -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 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 * @param eventListener An {@link AdaptiveMediaSourceEventListener}. May be null if delivery of
* events is not required. * events is not required.
* @deprecated Use {@link Builder} instead.
*/ */
@Deprecated
public HlsMediaSource(Uri manifestUri, DataSource.Factory dataSourceFactory, public HlsMediaSource(Uri manifestUri, DataSource.Factory dataSourceFactory,
int minLoadableRetryCount, Handler eventHandler, int minLoadableRetryCount, Handler eventHandler,
AdaptiveMediaSourceEventListener eventListener) { AdaptiveMediaSourceEventListener eventListener) {
...@@ -105,7 +235,9 @@ public final class HlsMediaSource implements MediaSource, ...@@ -105,7 +235,9 @@ public final class HlsMediaSource implements MediaSource,
* @param eventListener An {@link AdaptiveMediaSourceEventListener}. May be null if delivery of * @param eventListener An {@link AdaptiveMediaSourceEventListener}. May be null if delivery of
* events is not required. * events is 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
public HlsMediaSource(Uri manifestUri, HlsDataSourceFactory dataSourceFactory, public HlsMediaSource(Uri manifestUri, HlsDataSourceFactory dataSourceFactory,
HlsExtractorFactory extractorFactory, int minLoadableRetryCount, Handler eventHandler, HlsExtractorFactory extractorFactory, int minLoadableRetryCount, Handler eventHandler,
AdaptiveMediaSourceEventListener eventListener, AdaptiveMediaSourceEventListener eventListener,
......
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