mirror of
https://github.com/samsonjs/media.git
synced 2026-04-27 15:07:40 +00:00
Clean up DefaultDataSources and add factory for okhttp DataSources.
------------- Created by MOE: https://github.com/google/moe MOE_MIGRATED_REVID=123991352
This commit is contained in:
parent
c622483f79
commit
e684e42994
3 changed files with 79 additions and 21 deletions
|
|
@ -0,0 +1,65 @@
|
||||||
|
/*
|
||||||
|
* Copyright (C) 2014 The Android Open Source Project
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
package com.google.android.exoplayer.ext.okhttp;
|
||||||
|
|
||||||
|
import com.google.android.exoplayer.upstream.DataSourceFactory;
|
||||||
|
import com.google.android.exoplayer.upstream.DefaultDataSource;
|
||||||
|
import com.google.android.exoplayer.upstream.TransferListener;
|
||||||
|
import com.google.android.exoplayer.util.Predicate;
|
||||||
|
|
||||||
|
import android.content.Context;
|
||||||
|
|
||||||
|
import okhttp3.CacheControl;
|
||||||
|
import okhttp3.OkHttpClient;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A {@link DataSourceFactory} that produces {@link DefaultDataSource} instances that delegate to
|
||||||
|
* {@link OkHttpDataSource}s for non-file/asset/content URIs.
|
||||||
|
*/
|
||||||
|
public final class DefaultOkHttpDataSourceFactory implements DataSourceFactory {
|
||||||
|
|
||||||
|
private final Context context;
|
||||||
|
private final OkHttpClient client;
|
||||||
|
private final String userAgent;
|
||||||
|
private final Predicate<String> contentTypePredicate;
|
||||||
|
private final CacheControl cacheControl;
|
||||||
|
|
||||||
|
public DefaultOkHttpDataSourceFactory(Context context, OkHttpClient client, String userAgent,
|
||||||
|
Predicate<String> contentTypePredicate) {
|
||||||
|
this(context, client, userAgent, contentTypePredicate, null);
|
||||||
|
}
|
||||||
|
|
||||||
|
public DefaultOkHttpDataSourceFactory(Context context, OkHttpClient client, String userAgent,
|
||||||
|
Predicate<String> contentTypePredicate, CacheControl cacheControl) {
|
||||||
|
this.context = context.getApplicationContext();
|
||||||
|
this.client = client;
|
||||||
|
this.userAgent = userAgent;
|
||||||
|
this.contentTypePredicate = contentTypePredicate;
|
||||||
|
this.cacheControl = cacheControl;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public DefaultDataSource createDataSource() {
|
||||||
|
return createDataSource(null);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public DefaultDataSource createDataSource(TransferListener listener) {
|
||||||
|
return new DefaultDataSource(context, listener,
|
||||||
|
new OkHttpDataSource(client, userAgent, contentTypePredicate, listener, cacheControl));
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
@ -27,12 +27,15 @@ import java.io.IOException;
|
||||||
* A {@link DataSource} that supports multiple URI schemes. The supported schemes are:
|
* A {@link DataSource} that supports multiple URI schemes. The supported schemes are:
|
||||||
*
|
*
|
||||||
* <ul>
|
* <ul>
|
||||||
* <li>http(s): For fetching data over HTTP and HTTPS (e.g. https://www.something.com/media.mp4).
|
|
||||||
* <li>file: For fetching data from a local file (e.g. file:///path/to/media/media.mp4, or just
|
* <li>file: For fetching data from a local file (e.g. file:///path/to/media/media.mp4, or just
|
||||||
* /path/to/media/media.mp4 because the implementation assumes that a URI without a scheme is a
|
* /path/to/media/media.mp4 because the implementation assumes that a URI without a scheme is a
|
||||||
* local file URI).
|
* local file URI).
|
||||||
* <li>asset: For fetching data from an asset in the application's apk (e.g. asset:///media.mp4).
|
* <li>asset: For fetching data from an asset in the application's apk (e.g. asset:///media.mp4).
|
||||||
* <li>content: For fetching data from a content URI (e.g. content://authority/path/123).
|
* <li>content: For fetching data from a content URI (e.g. content://authority/path/123).
|
||||||
|
* <li>http(s): For fetching data over HTTP and HTTPS (e.g. https://www.something.com/media.mp4), if
|
||||||
|
* constructed using {@link #DefaultDataSource(Context, TransferListener, String, boolean)}, or
|
||||||
|
* any other schemes supported by a base data source if constructed using
|
||||||
|
* {@link #DefaultDataSource(Context, TransferListener, DataSource)}.
|
||||||
* </ul>
|
* </ul>
|
||||||
*/
|
*/
|
||||||
public final class DefaultDataSource implements DataSource {
|
public final class DefaultDataSource implements DataSource {
|
||||||
|
|
@ -40,7 +43,7 @@ public final class DefaultDataSource implements DataSource {
|
||||||
private static final String SCHEME_ASSET = "asset";
|
private static final String SCHEME_ASSET = "asset";
|
||||||
private static final String SCHEME_CONTENT = "content";
|
private static final String SCHEME_CONTENT = "content";
|
||||||
|
|
||||||
private final DataSource defaultDataSource;
|
private final DataSource baseDataSource;
|
||||||
private final DataSource fileDataSource;
|
private final DataSource fileDataSource;
|
||||||
private final DataSource assetDataSource;
|
private final DataSource assetDataSource;
|
||||||
private final DataSource contentDataSource;
|
private final DataSource contentDataSource;
|
||||||
|
|
@ -70,12 +73,11 @@ public final class DefaultDataSource implements DataSource {
|
||||||
*
|
*
|
||||||
* @param context A context.
|
* @param context A context.
|
||||||
* @param listener An optional {@link TransferListener}.
|
* @param listener An optional {@link TransferListener}.
|
||||||
* @param defaultDataSource A {@link DataSource} to use for URI schemes other than file, asset and
|
* @param baseDataSource A {@link DataSource} to use for URI schemes other than file, asset and
|
||||||
* content. This {@link DataSource} should normally support at least http(s).
|
* content. This {@link DataSource} should normally support at least http(s).
|
||||||
*/
|
*/
|
||||||
public DefaultDataSource(Context context, TransferListener listener,
|
public DefaultDataSource(Context context, TransferListener listener, DataSource baseDataSource) {
|
||||||
DataSource defaultDataSource) {
|
this.baseDataSource = Assertions.checkNotNull(baseDataSource);
|
||||||
this.defaultDataSource = Assertions.checkNotNull(defaultDataSource);
|
|
||||||
this.fileDataSource = new FileDataSource(listener);
|
this.fileDataSource = new FileDataSource(listener);
|
||||||
this.assetDataSource = new AssetDataSource(context, listener);
|
this.assetDataSource = new AssetDataSource(context, listener);
|
||||||
this.contentDataSource = new ContentDataSource(context, listener);
|
this.contentDataSource = new ContentDataSource(context, listener);
|
||||||
|
|
@ -97,7 +99,7 @@ public final class DefaultDataSource implements DataSource {
|
||||||
} else if (SCHEME_CONTENT.equals(scheme)) {
|
} else if (SCHEME_CONTENT.equals(scheme)) {
|
||||||
dataSource = contentDataSource;
|
dataSource = contentDataSource;
|
||||||
} else {
|
} else {
|
||||||
dataSource = defaultDataSource;
|
dataSource = baseDataSource;
|
||||||
}
|
}
|
||||||
// Open the source and return.
|
// Open the source and return.
|
||||||
return dataSource.open(dataSpec);
|
return dataSource.open(dataSpec);
|
||||||
|
|
|
||||||
|
|
@ -18,14 +18,14 @@ package com.google.android.exoplayer.upstream;
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A {@link DataSourceFactory} that produces {@link DefaultDataSource} instances.
|
* A {@link DataSourceFactory} that produces {@link DefaultDataSource} instances that delegate to
|
||||||
|
* {@link DefaultHttpDataSource}s for non-file/asset/content URIs.
|
||||||
*/
|
*/
|
||||||
public final class DefaultDataSourceFactory implements DataSourceFactory {
|
public final class DefaultDataSourceFactory implements DataSourceFactory {
|
||||||
|
|
||||||
private final Context context;
|
private final Context context;
|
||||||
private final String userAgent;
|
private final String userAgent;
|
||||||
private final boolean allowCrossProtocolRedirects;
|
private final boolean allowCrossProtocolRedirects;
|
||||||
private final DataSource httpDataSource;
|
|
||||||
|
|
||||||
public DefaultDataSourceFactory(Context context, String userAgent) {
|
public DefaultDataSourceFactory(Context context, String userAgent) {
|
||||||
this(context, userAgent, false);
|
this(context, userAgent, false);
|
||||||
|
|
@ -36,25 +36,16 @@ public final class DefaultDataSourceFactory implements DataSourceFactory {
|
||||||
this.context = context.getApplicationContext();
|
this.context = context.getApplicationContext();
|
||||||
this.userAgent = userAgent;
|
this.userAgent = userAgent;
|
||||||
this.allowCrossProtocolRedirects = allowCrossProtocolRedirects;
|
this.allowCrossProtocolRedirects = allowCrossProtocolRedirects;
|
||||||
this.httpDataSource = null;
|
|
||||||
}
|
|
||||||
|
|
||||||
public DefaultDataSourceFactory(Context context, DataSource httpDataSource) {
|
|
||||||
this.context = context.getApplicationContext();
|
|
||||||
this.httpDataSource = httpDataSource;
|
|
||||||
this.userAgent = null;
|
|
||||||
this.allowCrossProtocolRedirects = false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public DataSource createDataSource() {
|
public DefaultDataSource createDataSource() {
|
||||||
return createDataSource(null);
|
return createDataSource(null);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public DataSource createDataSource(TransferListener listener) {
|
public DefaultDataSource createDataSource(TransferListener listener) {
|
||||||
return httpDataSource != null ? new DefaultDataSource(context, listener, httpDataSource)
|
return new DefaultDataSource(context, listener, userAgent, allowCrossProtocolRedirects);
|
||||||
: new DefaultDataSource(context, listener, userAgent, allowCrossProtocolRedirects);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue