Skip to content

Commit

Permalink
Make DashMediaSource.Builder a factory for DashMediaSources
Browse files Browse the repository at this point in the history
This is in preparation for supporting non-extractor MediaSources for ads in
AdsMediaSource.

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=178377627
  • Loading branch information
andrewlewis authored and ojw28 committed Dec 12, 2017
1 parent 3e9c86f commit a0d42b5
Show file tree
Hide file tree
Showing 4 changed files with 135 additions and 124 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -371,11 +371,10 @@ private MediaSource buildMediaSource(Uri uri, String overrideExtension) {
.setEventListener(mainHandler, eventLogger)
.build();
case C.TYPE_DASH:
return DashMediaSource.Builder
.forManifestUri(uri, buildDataSourceFactory(false),
new DefaultDashChunkSource.Factory(mediaDataSourceFactory))
.setEventListener(mainHandler, eventLogger)
.build();
return new DashMediaSource.Factory(
new DefaultDashChunkSource.Factory(mediaDataSourceFactory),
buildDataSourceFactory(false))
.createMediaSource(uri, mainHandler, eventLogger);
case C.TYPE_HLS:
return HlsMediaSource.Builder
.forDataSource(uri, mediaDataSourceFactory)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,31 @@
*/
public final class AdsMediaSource implements MediaSource {

/** Factory for creating {@link MediaSource}s to play ad media. */
public interface MediaSourceFactory {

/**
* Creates a new {@link MediaSource} for loading the ad media with the specified {@code uri}.
*
* @param uri The URI of the media or manifest to play.
* @param handler A handler for listener events. May be null if delivery of events is not
* required.
* @param listener A listener for events. May be null if delivery of events is not required.
* @return The new media source.
*/
MediaSource createMediaSource(
Uri uri, @Nullable Handler handler, @Nullable MediaSourceEventListener listener);

/**
* Returns the content types supported by media sources created by this factory. Each element
* should be one of {@link C#TYPE_DASH}, {@link C#TYPE_SS}, {@link C#TYPE_HLS} or {@link
* C#TYPE_OTHER}.
*
* @return The content types supported by media sources created by this factory.
*/
int[] getSupportedTypes();
}

/** Listener for ads media source events. */
public interface EventListener extends MediaSourceEventListener {

Expand Down Expand Up @@ -77,7 +102,7 @@ public interface EventListener extends MediaSourceEventListener {
@Nullable private final EventListener eventListener;
private final Handler mainHandler;
private final ComponentListener componentListener;
private final AdMediaSourceFactory adMediaSourceFactory;
private final MediaSourceFactory adMediaSourceFactory;
private final Map<MediaSource, List<DeferredMediaPeriod>> deferredMediaPeriodByAdMediaSource;
private final Timeline.Period period;

Expand Down Expand Up @@ -138,7 +163,7 @@ public AdsMediaSource(
this.eventListener = eventListener;
mainHandler = new Handler(Looper.getMainLooper());
componentListener = new ComponentListener();
adMediaSourceFactory = new ExtractorAdMediaSourceFactory(dataSourceFactory);
adMediaSourceFactory = new ExtractorMediaSourceFactory(dataSourceFactory);
deferredMediaPeriodByAdMediaSource = new HashMap<>();
period = new Timeline.Period();
adGroupMediaSources = new MediaSource[0][];
Expand Down Expand Up @@ -186,7 +211,7 @@ public MediaPeriod createPeriod(MediaPeriodId id, Allocator allocator) {
if (adGroupMediaSources[adGroupIndex].length <= adIndexInAdGroup) {
Uri adUri = adPlaybackState.adUris[id.adGroupIndex][id.adIndexInAdGroup];
final MediaSource adMediaSource =
adMediaSourceFactory.createAdMediaSource(adUri, eventHandler, eventListener);
adMediaSourceFactory.createMediaSource(adUri, eventHandler, eventListener);
int oldAdCount = adGroupMediaSources[id.adGroupIndex].length;
if (adIndexInAdGroup >= oldAdCount) {
int adCount = adIndexInAdGroup + 1;
Expand Down Expand Up @@ -371,44 +396,16 @@ public void run() {

}

/**
* Factory for {@link MediaSource}s for loading ad media.
*/
private interface AdMediaSourceFactory {

/**
* Creates a new {@link MediaSource} for loading the ad media with the specified {@code uri}.
*
* @param uri The URI of the ad.
* @param handler A handler for listener events. May be null if delivery of events is not
* required.
* @param listener A listener for events. May be null if delivery of events is not required.
* @return The new media source.
*/
MediaSource createAdMediaSource(
Uri uri, @Nullable Handler handler, @Nullable MediaSourceEventListener listener);

/**
* Returns the content types supported by media sources created by this factory. Each element
* should be one of {@link C#TYPE_DASH}, {@link C#TYPE_SS}, {@link C#TYPE_HLS} or
* {@link C#TYPE_OTHER}.
*
* @return The content types supported by the factory.
*/
int[] getSupportedTypes();

}

private static final class ExtractorAdMediaSourceFactory implements AdMediaSourceFactory {
private static final class ExtractorMediaSourceFactory implements MediaSourceFactory {

private final DataSource.Factory dataSourceFactory;

public ExtractorAdMediaSourceFactory(DataSource.Factory dataSourceFactory) {
public ExtractorMediaSourceFactory(DataSource.Factory dataSourceFactory) {
this.dataSourceFactory = dataSourceFactory;
}

@Override
public MediaSource createAdMediaSource(
public MediaSource createMediaSource(
Uri uri, @Nullable Handler handler, @Nullable MediaSourceEventListener listener) {
return new ExtractorMediaSource.Builder(uri, dataSourceFactory)
.setEventListener(handler, listener)
Expand Down
Loading

0 comments on commit a0d42b5

Please sign in to comment.