mirror of
https://github.com/samsonjs/media.git
synced 2026-04-08 11:45:51 +00:00
Support setting default headers on HttpDataSource.Factory's
Issue: #2166 ------------- Created by MOE: https://github.com/google/moe MOE_MIGRATED_REVID=143703258
This commit is contained in:
parent
a77bc8d457
commit
9d48d4e486
4 changed files with 92 additions and 6 deletions
|
|
@ -16,6 +16,7 @@
|
|||
package com.google.android.exoplayer2.ext.cronet;
|
||||
|
||||
import com.google.android.exoplayer2.upstream.DataSource;
|
||||
import com.google.android.exoplayer2.upstream.HttpDataSource.BaseFactory;
|
||||
import com.google.android.exoplayer2.upstream.HttpDataSource.Factory;
|
||||
import com.google.android.exoplayer2.upstream.TransferListener;
|
||||
import com.google.android.exoplayer2.util.Predicate;
|
||||
|
|
@ -25,7 +26,7 @@ import org.chromium.net.CronetEngine;
|
|||
/**
|
||||
* A {@link Factory} that produces {@link CronetDataSource}.
|
||||
*/
|
||||
public final class CronetDataSourceFactory implements Factory {
|
||||
public final class CronetDataSourceFactory extends BaseFactory {
|
||||
|
||||
/**
|
||||
* The default connection timeout, in milliseconds.
|
||||
|
|
@ -67,7 +68,7 @@ public final class CronetDataSourceFactory implements Factory {
|
|||
}
|
||||
|
||||
@Override
|
||||
public CronetDataSource createDataSource() {
|
||||
protected CronetDataSource createDataSourceInternal() {
|
||||
return new CronetDataSource(cronetEngine, executor, contentTypePredicate, transferListener,
|
||||
connectTimeoutMs, readTimeoutMs, resetTimeoutOnRedirects);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -16,6 +16,7 @@
|
|||
package com.google.android.exoplayer2.ext.okhttp;
|
||||
|
||||
import com.google.android.exoplayer2.upstream.DataSource;
|
||||
import com.google.android.exoplayer2.upstream.HttpDataSource.BaseFactory;
|
||||
import com.google.android.exoplayer2.upstream.HttpDataSource.Factory;
|
||||
import com.google.android.exoplayer2.upstream.TransferListener;
|
||||
import okhttp3.CacheControl;
|
||||
|
|
@ -24,7 +25,7 @@ import okhttp3.Call;
|
|||
/**
|
||||
* A {@link Factory} that produces {@link OkHttpDataSource}.
|
||||
*/
|
||||
public final class OkHttpDataSourceFactory implements Factory {
|
||||
public final class OkHttpDataSourceFactory extends BaseFactory {
|
||||
|
||||
private final Call.Factory callFactory;
|
||||
private final String userAgent;
|
||||
|
|
@ -58,7 +59,7 @@ public final class OkHttpDataSourceFactory implements Factory {
|
|||
}
|
||||
|
||||
@Override
|
||||
public OkHttpDataSource createDataSource() {
|
||||
protected OkHttpDataSource createDataSourceInternal() {
|
||||
return new OkHttpDataSource(callFactory, userAgent, null, listener, cacheControl);
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -15,10 +15,11 @@
|
|||
*/
|
||||
package com.google.android.exoplayer2.upstream;
|
||||
|
||||
import com.google.android.exoplayer2.upstream.HttpDataSource.BaseFactory;
|
||||
import com.google.android.exoplayer2.upstream.HttpDataSource.Factory;
|
||||
|
||||
/** A {@link Factory} that produces {@link DefaultHttpDataSource} instances. */
|
||||
public final class DefaultHttpDataSourceFactory implements Factory {
|
||||
public final class DefaultHttpDataSourceFactory extends BaseFactory {
|
||||
|
||||
private final String userAgent;
|
||||
private final TransferListener<? super DataSource> listener;
|
||||
|
|
@ -75,8 +76,9 @@ public final class DefaultHttpDataSourceFactory implements Factory {
|
|||
}
|
||||
|
||||
@Override
|
||||
public DefaultHttpDataSource createDataSource() {
|
||||
protected DefaultHttpDataSource createDataSourceInternal() {
|
||||
return new DefaultHttpDataSource(userAgent, null, listener, connectTimeoutMillis,
|
||||
readTimeoutMillis, allowCrossProtocolRedirects);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -17,11 +17,13 @@ package com.google.android.exoplayer2.upstream;
|
|||
|
||||
import android.support.annotation.IntDef;
|
||||
import android.text.TextUtils;
|
||||
import com.google.android.exoplayer2.util.Assertions;
|
||||
import com.google.android.exoplayer2.util.Predicate;
|
||||
import com.google.android.exoplayer2.util.Util;
|
||||
import java.io.IOException;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
|
|
@ -38,6 +40,86 @@ public interface HttpDataSource extends DataSource {
|
|||
@Override
|
||||
HttpDataSource createDataSource();
|
||||
|
||||
/**
|
||||
* Sets a default request header field for {@link HttpDataSource} instances subsequently
|
||||
* created by the factory. Previously created instances are not affected.
|
||||
*
|
||||
* @param name The name of the header field.
|
||||
* @param value The value of the field.
|
||||
*/
|
||||
void setDefaultRequestProperty(String name, String value);
|
||||
|
||||
/**
|
||||
* Clears a default request header field for {@link HttpDataSource} instances subsequently
|
||||
* created by the factory. Previously created instances are not affected.
|
||||
*
|
||||
* @param name The name of the header field.
|
||||
*/
|
||||
void clearDefaultRequestProperty(String name);
|
||||
|
||||
/**
|
||||
* Clears all default request header fields for all {@link HttpDataSource} instances
|
||||
* subsequently created by the factory. Previously created instances are not affected.
|
||||
*/
|
||||
void clearAllDefaultRequestProperties();
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Base implementation of {@link Factory} that sets default request properties.
|
||||
*/
|
||||
abstract class BaseFactory implements Factory {
|
||||
|
||||
private final HashMap<String, String> requestProperties;
|
||||
|
||||
public BaseFactory() {
|
||||
requestProperties = new HashMap<>();
|
||||
}
|
||||
|
||||
@Override
|
||||
public final HttpDataSource createDataSource() {
|
||||
HttpDataSource dataSource = createDataSourceInternal();
|
||||
synchronized (requestProperties) {
|
||||
for (Map.Entry<String, String> property : requestProperties.entrySet()) {
|
||||
dataSource.setRequestProperty(property.getKey(), property.getValue());
|
||||
}
|
||||
}
|
||||
return dataSource;
|
||||
}
|
||||
|
||||
@Override
|
||||
public final void setDefaultRequestProperty(String name, String value) {
|
||||
Assertions.checkNotNull(name);
|
||||
Assertions.checkNotNull(value);
|
||||
synchronized (requestProperties) {
|
||||
requestProperties.put(name, value);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public final void clearDefaultRequestProperty(String name) {
|
||||
Assertions.checkNotNull(name);
|
||||
synchronized (requestProperties) {
|
||||
requestProperties.remove(name);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public final void clearAllDefaultRequestProperties() {
|
||||
synchronized (requestProperties) {
|
||||
requestProperties.clear();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Called by {@link #createDataSource()} to create a {@link HttpDataSource} instance without
|
||||
* default request properties set. Default request properties will be set by
|
||||
* {@link #createDataSource()} before the instance is returned.
|
||||
*
|
||||
* @return A {@link HttpDataSource} instance without default request properties set.
|
||||
*/
|
||||
protected abstract HttpDataSource createDataSourceInternal();
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
|||
Loading…
Reference in a new issue