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:
andrewlewis 2016-06-03 12:01:07 -07:00 committed by Oliver Woodman
parent c622483f79
commit e684e42994
3 changed files with 79 additions and 21 deletions

View File

@ -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));
}
}

View File

@ -27,12 +27,15 @@ import java.io.IOException;
* A {@link DataSource} that supports multiple URI schemes. The supported schemes are:
*
* <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
* /path/to/media/media.mp4 because the implementation assumes that a URI without a scheme is a
* local file URI).
* <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>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>
*/
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_CONTENT = "content";
private final DataSource defaultDataSource;
private final DataSource baseDataSource;
private final DataSource fileDataSource;
private final DataSource assetDataSource;
private final DataSource contentDataSource;
@ -70,12 +73,11 @@ public final class DefaultDataSource implements DataSource {
*
* @param context A context.
* @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).
*/
public DefaultDataSource(Context context, TransferListener listener,
DataSource defaultDataSource) {
this.defaultDataSource = Assertions.checkNotNull(defaultDataSource);
public DefaultDataSource(Context context, TransferListener listener, DataSource baseDataSource) {
this.baseDataSource = Assertions.checkNotNull(baseDataSource);
this.fileDataSource = new FileDataSource(listener);
this.assetDataSource = new AssetDataSource(context, listener);
this.contentDataSource = new ContentDataSource(context, listener);
@ -97,7 +99,7 @@ public final class DefaultDataSource implements DataSource {
} else if (SCHEME_CONTENT.equals(scheme)) {
dataSource = contentDataSource;
} else {
dataSource = defaultDataSource;
dataSource = baseDataSource;
}
// Open the source and return.
return dataSource.open(dataSpec);

View File

@ -18,14 +18,14 @@ package com.google.android.exoplayer.upstream;
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 {
private final Context context;
private final String userAgent;
private final boolean allowCrossProtocolRedirects;
private final DataSource httpDataSource;
public DefaultDataSourceFactory(Context context, String userAgent) {
this(context, userAgent, false);
@ -36,25 +36,16 @@ public final class DefaultDataSourceFactory implements DataSourceFactory {
this.context = context.getApplicationContext();
this.userAgent = userAgent;
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
public DataSource createDataSource() {
public DefaultDataSource createDataSource() {
return createDataSource(null);
}
@Override
public DataSource createDataSource(TransferListener listener) {
return httpDataSource != null ? new DefaultDataSource(context, listener, httpDataSource)
: new DefaultDataSource(context, listener, userAgent, allowCrossProtocolRedirects);
public DefaultDataSource createDataSource(TransferListener listener) {
return new DefaultDataSource(context, listener, userAgent, allowCrossProtocolRedirects);
}
}