Skip to content

Commit

Permalink
Added the ability to specify a uri scheme in the MediaSourceProvider
Browse files Browse the repository at this point in the history
  • Loading branch information
brianwernick committed May 7, 2018
1 parent 336b081 commit 1b3d652
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,8 @@ public static class Data {
public static volatile DataSourceFactoryProvider dataSourceFactoryProvider;
@Nullable
public static volatile LoadControl loadControl;
@NonNull
public static volatile MediaSourceProvider mediaSourceProvider = new MediaSourceProvider();

static {
instantiateRendererClasses();
Expand All @@ -146,9 +148,9 @@ private static void instantiateRendererClasses() {

private static void instantiateSourceProviders() {
// Adds the HLS, SmoothStream, and MPEG Dash registrations
sourceTypeBuilders.add(new MediaSourceProvider.SourceTypeBuilder(new HlsMediaSourceBuilder(), ".m3u8", ".*\\.m3u8.*"));
sourceTypeBuilders.add(new MediaSourceProvider.SourceTypeBuilder(new DashMediaSourceBuilder(), ".mpd", ".*\\.mpd.*"));
sourceTypeBuilders.add(new MediaSourceProvider.SourceTypeBuilder(new SsMediaSourceBuilder(), ".ism", ".*\\.ism.*"));
sourceTypeBuilders.add(new MediaSourceProvider.SourceTypeBuilder(new HlsMediaSourceBuilder(), null, ".m3u8", ".*\\.m3u8.*"));
sourceTypeBuilders.add(new MediaSourceProvider.SourceTypeBuilder(new DashMediaSourceBuilder(), null, ".mpd", ".*\\.mpd.*"));
sourceTypeBuilders.add(new MediaSourceProvider.SourceTypeBuilder(new SsMediaSourceBuilder(), null,".ism", ".*\\.ism.*"));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@
import com.devbrackets.android.exomedia.core.listener.InternalErrorListener;
import com.devbrackets.android.exomedia.core.listener.MetadataListener;
import com.devbrackets.android.exomedia.core.renderer.RendererProvider;
import com.devbrackets.android.exomedia.core.source.MediaSourceProvider;
import com.devbrackets.android.exomedia.listener.OnBufferUpdateListener;
import com.devbrackets.android.exomedia.util.Repeater;
import com.google.android.exoplayer2.C;
Expand Down Expand Up @@ -78,7 +77,6 @@
import com.google.android.exoplayer2.video.VideoRendererEventListener;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.UUID;
Expand Down Expand Up @@ -120,9 +118,7 @@ public class ExoMediaPlayer extends Player.DefaultEventListener {
@Nullable
private MediaSource mediaSource;
@NonNull
private List<Renderer> renderers = new LinkedList<>();
@NonNull
private MediaSourceProvider mediaSourceProvider = new MediaSourceProvider();
private List<Renderer> renderers;
@NonNull
private DefaultBandwidthMeter bandwidthMeter = new DefaultBandwidthMeter();

Expand Down Expand Up @@ -192,7 +188,7 @@ public void setDrmCallback(@Nullable MediaDrmCallback drmCallback) {
}

public void setUri(@Nullable Uri uri) {
setMediaSource(uri != null ? mediaSourceProvider.generate(context, mainHandler, uri, bandwidthMeter) : null);
setMediaSource(uri != null ? ExoMedia.Data.mediaSourceProvider.generate(context, mainHandler, uri, bandwidthMeter) : null);
}

public void setMediaSource(@Nullable MediaSource source) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,28 +31,62 @@ public class MediaSourceProvider {

@NonNull
public MediaSource generate(@NonNull Context context, @NonNull Handler handler, @NonNull Uri uri, @Nullable TransferListener<? super DataSource> transferListener ) {
String extension = MediaSourceUtil.getExtension(uri);

// Searches for a registered builder
SourceTypeBuilder sourceTypeBuilder = findByExtension(extension);
if (sourceTypeBuilder == null) {
sourceTypeBuilder = findByLooseComparison(uri);
}
SourceTypeBuilder sourceTypeBuilder = findByProviders(uri);

// If a registered builder wasn't found then use the default
MediaSourceBuilder builder = sourceTypeBuilder != null ? sourceTypeBuilder.builder : new DefaultMediaSourceBuilder();
return builder.build(context, uri, userAgent, handler, transferListener);
}

@Nullable
protected static SourceTypeBuilder findByExtension(@Nullable String extension) {
protected static SourceTypeBuilder findByProviders(@NonNull Uri uri) {
// Uri Scheme (e.g. rtsp)
SourceTypeBuilder sourceTypeBuilder = findByScheme(uri);
if (sourceTypeBuilder != null) {
return sourceTypeBuilder;
}

// Extension
sourceTypeBuilder = findByExtension(uri);
if (sourceTypeBuilder != null) {
return sourceTypeBuilder;
}

// Regex
sourceTypeBuilder = findByLooseComparison(uri);
if (sourceTypeBuilder != null) {
return sourceTypeBuilder;
}

return null;
}

@Nullable
protected static SourceTypeBuilder findByScheme(@NonNull Uri uri) {
String scheme = uri.getScheme();
if (scheme == null || scheme.isEmpty()) {
return null;
}

for (SourceTypeBuilder builder : ExoMedia.Data.sourceTypeBuilders) {
if (builder.uriScheme != null && builder.uriScheme.equalsIgnoreCase(scheme)) {
return builder;
}
}

return null;
}

@Nullable
protected static SourceTypeBuilder findByExtension(@NonNull Uri uri) {
String extension = MediaSourceUtil.getExtension(uri);
if (extension == null || extension.isEmpty()) {
return null;
}

for (SourceTypeBuilder sourceTypeBuilder : ExoMedia.Data.sourceTypeBuilders) {
if (sourceTypeBuilder.extension.equalsIgnoreCase(extension)) {
return sourceTypeBuilder;
for (SourceTypeBuilder builder : ExoMedia.Data.sourceTypeBuilders) {
if (builder.extension != null && builder.extension.equalsIgnoreCase(extension)) {
return builder;
}
}

Expand All @@ -61,9 +95,9 @@ protected static SourceTypeBuilder findByExtension(@Nullable String extension) {

@Nullable
protected static SourceTypeBuilder findByLooseComparison(@NonNull Uri uri) {
for (SourceTypeBuilder sourceTypeBuilder : ExoMedia.Data.sourceTypeBuilders) {
if (sourceTypeBuilder.looseComparisonRegex != null && uri.toString().matches(sourceTypeBuilder.looseComparisonRegex)) {
return sourceTypeBuilder;
for (SourceTypeBuilder builder : ExoMedia.Data.sourceTypeBuilders) {
if (builder.looseComparisonRegex != null && uri.toString().matches(builder.looseComparisonRegex)) {
return builder;
}
}

Expand All @@ -73,13 +107,24 @@ protected static SourceTypeBuilder findByLooseComparison(@NonNull Uri uri) {
public static class SourceTypeBuilder {
@NonNull
public final MediaSourceBuilder builder;
@NonNull
@Nullable
public final String extension;
@Nullable
public final String uriScheme;
@Nullable
public final String looseComparisonRegex;

/**
* @deprecated Use {@link #SourceTypeBuilder(MediaSourceBuilder, String, String, String)}
*/
@Deprecated
public SourceTypeBuilder(@NonNull MediaSourceBuilder builder, @NonNull String extension, @Nullable String looseComparisonRegex) {
this(builder, null, extension, looseComparisonRegex);
}

public SourceTypeBuilder(@NonNull MediaSourceBuilder builder, @Nullable String uriScheme, @Nullable String extension, @Nullable String looseComparisonRegex) {
this.builder = builder;
this.uriScheme = uriScheme;
this.extension = extension;
this.looseComparisonRegex = looseComparisonRegex;
}
Expand Down

0 comments on commit 1b3d652

Please sign in to comment.