mirror of
https://github.com/samsonjs/media.git
synced 2026-04-27 15:07:40 +00:00
Make DefaultHttpDataSourceFactory an inner class of the built class
#exofixit PiperOrigin-RevId: 348441436
This commit is contained in:
parent
5e47c3c5e5
commit
04824dfd25
7 changed files with 210 additions and 147 deletions
|
|
@ -27,6 +27,8 @@
|
||||||
`HttpDataSource.Factory.setDefaultRequestProperties` instead.
|
`HttpDataSource.Factory.setDefaultRequestProperties` instead.
|
||||||
* Fix playback issues after seeking during an ad
|
* Fix playback issues after seeking during an ad
|
||||||
([#8349](https://github.com/google/ExoPlayer/issues/8349))
|
([#8349](https://github.com/google/ExoPlayer/issues/8349))
|
||||||
|
* Add `DefaultHttpDataSource.Factory` and deprecate
|
||||||
|
`DefaultHttpDataSourceFactory`.
|
||||||
* Track selection:
|
* Track selection:
|
||||||
* Add option to specify multiple preferred audio or text languages.
|
* Add option to specify multiple preferred audio or text languages.
|
||||||
* Forward `Timeline` and `MediaPeriodId` to `TrackSelection.Factory`.
|
* Forward `Timeline` and `MediaPeriodId` to `TrackSelection.Factory`.
|
||||||
|
|
|
||||||
|
|
@ -132,12 +132,12 @@ public final class DefaultDataSource implements DataSource {
|
||||||
boolean allowCrossProtocolRedirects) {
|
boolean allowCrossProtocolRedirects) {
|
||||||
this(
|
this(
|
||||||
context,
|
context,
|
||||||
new DefaultHttpDataSource(
|
new DefaultHttpDataSource.Factory()
|
||||||
userAgent,
|
.setUserAgent(userAgent)
|
||||||
connectTimeoutMillis,
|
.setConnectTimeoutMs(connectTimeoutMillis)
|
||||||
readTimeoutMillis,
|
.setReadTimeoutMs(readTimeoutMillis)
|
||||||
allowCrossProtocolRedirects,
|
.setAllowCrossProtocolRedirects(allowCrossProtocolRedirects)
|
||||||
/* defaultRequestProperties= */ null));
|
.createDataSource());
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
||||||
|
|
@ -59,7 +59,7 @@ public final class DefaultDataSourceFactory implements Factory {
|
||||||
*/
|
*/
|
||||||
public DefaultDataSourceFactory(
|
public DefaultDataSourceFactory(
|
||||||
Context context, String userAgent, @Nullable TransferListener listener) {
|
Context context, String userAgent, @Nullable TransferListener listener) {
|
||||||
this(context, listener, new DefaultHttpDataSourceFactory(userAgent, listener));
|
this(context, listener, new DefaultHttpDataSource.Factory().setUserAgent(userAgent));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
||||||
|
|
@ -15,6 +15,7 @@
|
||||||
*/
|
*/
|
||||||
package com.google.android.exoplayer2.upstream;
|
package com.google.android.exoplayer2.upstream;
|
||||||
|
|
||||||
|
import static com.google.android.exoplayer2.ExoPlayerLibraryInfo.DEFAULT_USER_AGENT;
|
||||||
import static java.lang.Math.max;
|
import static java.lang.Math.max;
|
||||||
import static java.lang.Math.min;
|
import static java.lang.Math.min;
|
||||||
|
|
||||||
|
|
@ -52,16 +53,148 @@ import java.util.zip.GZIPInputStream;
|
||||||
* An {@link HttpDataSource} that uses Android's {@link HttpURLConnection}.
|
* An {@link HttpDataSource} that uses Android's {@link HttpURLConnection}.
|
||||||
*
|
*
|
||||||
* <p>By default this implementation will not follow cross-protocol redirects (i.e. redirects from
|
* <p>By default this implementation will not follow cross-protocol redirects (i.e. redirects from
|
||||||
* HTTP to HTTPS or vice versa). Cross-protocol redirects can be enabled by using the {@link
|
* HTTP to HTTPS or vice versa). Cross-protocol redirects can be enabled by passing {@code true} to
|
||||||
* #DefaultHttpDataSource(String, int, int, boolean, RequestProperties)} constructor and passing
|
* {@link DefaultHttpDataSource.Factory#setAllowCrossProtocolRedirects(boolean)}.
|
||||||
* {@code true} for the {@code allowCrossProtocolRedirects} argument.
|
|
||||||
*
|
*
|
||||||
* <p>Note: HTTP request headers will be set using all parameters passed via (in order of decreasing
|
* <p>Note: HTTP request headers will be set using all parameters passed via (in order of decreasing
|
||||||
* priority) the {@code dataSpec}, {@link #setRequestProperty} and the default parameters used to
|
* priority) the {@code dataSpec}, {@link #setRequestProperty} and the default properties that can
|
||||||
* construct the instance.
|
* be passed to {@link HttpDataSource.Factory#setDefaultRequestProperties(Map)}.
|
||||||
*/
|
*/
|
||||||
public class DefaultHttpDataSource extends BaseDataSource implements HttpDataSource {
|
public class DefaultHttpDataSource extends BaseDataSource implements HttpDataSource {
|
||||||
|
|
||||||
|
/** {@link DataSource.Factory} for {@link DefaultHttpDataSource} instances. */
|
||||||
|
public static final class Factory implements HttpDataSource.Factory {
|
||||||
|
|
||||||
|
private final RequestProperties defaultRequestProperties;
|
||||||
|
|
||||||
|
@Nullable private TransferListener transferListener;
|
||||||
|
@Nullable private Predicate<String> contentTypePredicate;
|
||||||
|
private String userAgent;
|
||||||
|
private int connectTimeoutMs;
|
||||||
|
private int readTimeoutMs;
|
||||||
|
private boolean allowCrossProtocolRedirects;
|
||||||
|
|
||||||
|
/** Creates an instance. */
|
||||||
|
public Factory() {
|
||||||
|
defaultRequestProperties = new RequestProperties();
|
||||||
|
userAgent = DEFAULT_USER_AGENT;
|
||||||
|
connectTimeoutMs = DEFAULT_CONNECT_TIMEOUT_MILLIS;
|
||||||
|
readTimeoutMs = DEFAULT_READ_TIMEOUT_MILLIS;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** @deprecated Use {@link #setDefaultRequestProperties(Map)} instead. */
|
||||||
|
@Deprecated
|
||||||
|
@Override
|
||||||
|
public final RequestProperties getDefaultRequestProperties() {
|
||||||
|
return defaultRequestProperties;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public final Factory setDefaultRequestProperties(Map<String, String> defaultRequestProperties) {
|
||||||
|
this.defaultRequestProperties.clearAndSet(defaultRequestProperties);
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sets the user agent that will be used.
|
||||||
|
*
|
||||||
|
* <p>The default is {@link ExoPlayerLibraryInfo#DEFAULT_USER_AGENT}.
|
||||||
|
*
|
||||||
|
* @param userAgent The user agent that will be used.
|
||||||
|
* @return This factory.
|
||||||
|
*/
|
||||||
|
public Factory setUserAgent(String userAgent) {
|
||||||
|
this.userAgent = userAgent;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sets the connect timeout, in milliseconds.
|
||||||
|
*
|
||||||
|
* <p>The default is {@link DefaultHttpDataSource#DEFAULT_CONNECT_TIMEOUT_MILLIS}.
|
||||||
|
*
|
||||||
|
* @param connectTimeoutMs The connect timeout, in milliseconds, that will be used.
|
||||||
|
* @return This factory.
|
||||||
|
*/
|
||||||
|
public Factory setConnectTimeoutMs(int connectTimeoutMs) {
|
||||||
|
this.connectTimeoutMs = connectTimeoutMs;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sets the read timeout, in milliseconds.
|
||||||
|
*
|
||||||
|
* <p>The default is {@link DefaultHttpDataSource#DEFAULT_READ_TIMEOUT_MILLIS}.
|
||||||
|
*
|
||||||
|
* @param readTimeoutMs The connect timeout, in milliseconds, that will be used.
|
||||||
|
* @return This factory.
|
||||||
|
*/
|
||||||
|
public Factory setReadTimeoutMs(int readTimeoutMs) {
|
||||||
|
this.readTimeoutMs = readTimeoutMs;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sets whether to allow cross protocol redirects.
|
||||||
|
*
|
||||||
|
* <p>The default is {@code false}.
|
||||||
|
*
|
||||||
|
* @param allowCrossProtocolRedirects Whether to allow cross protocol redirects.
|
||||||
|
* @return This factory.
|
||||||
|
*/
|
||||||
|
public Factory setAllowCrossProtocolRedirects(boolean allowCrossProtocolRedirects) {
|
||||||
|
this.allowCrossProtocolRedirects = allowCrossProtocolRedirects;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sets a content type {@link Predicate}. If a content type is rejected by the predicate then a
|
||||||
|
* {@link HttpDataSource.InvalidContentTypeException} is thrown from {@link
|
||||||
|
* DefaultHttpDataSource#open(DataSpec)}.
|
||||||
|
*
|
||||||
|
* <p>The default is {@code null}.
|
||||||
|
*
|
||||||
|
* @param contentTypePredicate The content type {@link Predicate}, or {@code null} to clear a
|
||||||
|
* predicate that was previously set.
|
||||||
|
* @return This factory.
|
||||||
|
*/
|
||||||
|
public Factory setContentTypePredicate(@Nullable Predicate<String> contentTypePredicate) {
|
||||||
|
this.contentTypePredicate = contentTypePredicate;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sets the {@link TransferListener} that will be used.
|
||||||
|
*
|
||||||
|
* <p>The default is {@code null}.
|
||||||
|
*
|
||||||
|
* <p>See {@link DataSource#addTransferListener(TransferListener)}.
|
||||||
|
*
|
||||||
|
* @param transferListener The listener that will be used.
|
||||||
|
* @return This factory.
|
||||||
|
*/
|
||||||
|
public Factory setTransferListener(@Nullable TransferListener transferListener) {
|
||||||
|
this.transferListener = transferListener;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public DefaultHttpDataSource createDataSource() {
|
||||||
|
DefaultHttpDataSource dataSource =
|
||||||
|
new DefaultHttpDataSource(
|
||||||
|
userAgent,
|
||||||
|
connectTimeoutMs,
|
||||||
|
readTimeoutMs,
|
||||||
|
allowCrossProtocolRedirects,
|
||||||
|
defaultRequestProperties,
|
||||||
|
contentTypePredicate);
|
||||||
|
if (transferListener != null) {
|
||||||
|
dataSource.addTransferListener(transferListener);
|
||||||
|
}
|
||||||
|
return dataSource;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/** The default connection timeout, in milliseconds. */
|
/** The default connection timeout, in milliseconds. */
|
||||||
public static final int DEFAULT_CONNECT_TIMEOUT_MILLIS = 8 * 1000;
|
public static final int DEFAULT_CONNECT_TIMEOUT_MILLIS = 8 * 1000;
|
||||||
/**
|
/**
|
||||||
|
|
@ -98,7 +231,9 @@ public class DefaultHttpDataSource extends BaseDataSource implements HttpDataSou
|
||||||
private long bytesSkipped;
|
private long bytesSkipped;
|
||||||
private long bytesRead;
|
private long bytesRead;
|
||||||
|
|
||||||
/** Creates an instance. */
|
/** @deprecated Use {@link DefaultHttpDataSource.Factory} instead. */
|
||||||
|
@SuppressWarnings("deprecation")
|
||||||
|
@Deprecated
|
||||||
public DefaultHttpDataSource() {
|
public DefaultHttpDataSource() {
|
||||||
this(
|
this(
|
||||||
ExoPlayerLibraryInfo.DEFAULT_USER_AGENT,
|
ExoPlayerLibraryInfo.DEFAULT_USER_AGENT,
|
||||||
|
|
@ -106,24 +241,16 @@ public class DefaultHttpDataSource extends BaseDataSource implements HttpDataSou
|
||||||
DEFAULT_READ_TIMEOUT_MILLIS);
|
DEFAULT_READ_TIMEOUT_MILLIS);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/** @deprecated Use {@link DefaultHttpDataSource.Factory} instead. */
|
||||||
* Creates an instance.
|
@SuppressWarnings("deprecation")
|
||||||
*
|
@Deprecated
|
||||||
* @param userAgent The User-Agent string that should be used.
|
|
||||||
*/
|
|
||||||
public DefaultHttpDataSource(String userAgent) {
|
public DefaultHttpDataSource(String userAgent) {
|
||||||
this(userAgent, DEFAULT_CONNECT_TIMEOUT_MILLIS, DEFAULT_READ_TIMEOUT_MILLIS);
|
this(userAgent, DEFAULT_CONNECT_TIMEOUT_MILLIS, DEFAULT_READ_TIMEOUT_MILLIS);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/** @deprecated Use {@link DefaultHttpDataSource.Factory} instead. */
|
||||||
* Creates an instance.
|
@SuppressWarnings("deprecation")
|
||||||
*
|
@Deprecated
|
||||||
* @param userAgent The User-Agent string that should be used.
|
|
||||||
* @param connectTimeoutMillis The connection timeout, in milliseconds. A timeout of zero is
|
|
||||||
* interpreted as an infinite timeout.
|
|
||||||
* @param readTimeoutMillis The read timeout, in milliseconds. A timeout of zero is interpreted as
|
|
||||||
* an infinite timeout.
|
|
||||||
*/
|
|
||||||
public DefaultHttpDataSource(String userAgent, int connectTimeoutMillis, int readTimeoutMillis) {
|
public DefaultHttpDataSource(String userAgent, int connectTimeoutMillis, int readTimeoutMillis) {
|
||||||
this(
|
this(
|
||||||
userAgent,
|
userAgent,
|
||||||
|
|
@ -133,129 +260,45 @@ public class DefaultHttpDataSource extends BaseDataSource implements HttpDataSou
|
||||||
/* defaultRequestProperties= */ null);
|
/* defaultRequestProperties= */ null);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/** @deprecated Use {@link DefaultHttpDataSource.Factory} instead. */
|
||||||
* Creates an instance.
|
@Deprecated
|
||||||
*
|
|
||||||
* @param userAgent The User-Agent string that should be used.
|
|
||||||
* @param connectTimeoutMillis The connection timeout, in milliseconds. A timeout of zero is
|
|
||||||
* interpreted as an infinite timeout. Pass {@link #DEFAULT_CONNECT_TIMEOUT_MILLIS} to use the
|
|
||||||
* default value.
|
|
||||||
* @param readTimeoutMillis The read timeout, in milliseconds. A timeout of zero is interpreted as
|
|
||||||
* an infinite timeout. Pass {@link #DEFAULT_READ_TIMEOUT_MILLIS} to use the default value.
|
|
||||||
* @param allowCrossProtocolRedirects Whether cross-protocol redirects (i.e. redirects from HTTP
|
|
||||||
* to HTTPS and vice versa) are enabled.
|
|
||||||
* @param defaultRequestProperties The default request properties to be sent to the server as HTTP
|
|
||||||
* headers or {@code null} if not required.
|
|
||||||
*/
|
|
||||||
public DefaultHttpDataSource(
|
public DefaultHttpDataSource(
|
||||||
String userAgent,
|
String userAgent,
|
||||||
int connectTimeoutMillis,
|
int connectTimeoutMillis,
|
||||||
int readTimeoutMillis,
|
int readTimeoutMillis,
|
||||||
boolean allowCrossProtocolRedirects,
|
boolean allowCrossProtocolRedirects,
|
||||||
@Nullable RequestProperties defaultRequestProperties) {
|
@Nullable RequestProperties defaultRequestProperties) {
|
||||||
super(/* isNetwork= */ true);
|
|
||||||
this.userAgent = Assertions.checkNotEmpty(userAgent);
|
|
||||||
this.requestProperties = new RequestProperties();
|
|
||||||
this.connectTimeoutMillis = connectTimeoutMillis;
|
|
||||||
this.readTimeoutMillis = readTimeoutMillis;
|
|
||||||
this.allowCrossProtocolRedirects = allowCrossProtocolRedirects;
|
|
||||||
this.defaultRequestProperties = defaultRequestProperties;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Creates an instance.
|
|
||||||
*
|
|
||||||
* @param userAgent The User-Agent string that should be used.
|
|
||||||
* @param contentTypePredicate An optional {@link Predicate}. If a content type is rejected by the
|
|
||||||
* predicate then a {@link HttpDataSource.InvalidContentTypeException} is thrown from {@link
|
|
||||||
* #open(DataSpec)}.
|
|
||||||
* @deprecated Use {@link #DefaultHttpDataSource(String)} and {@link
|
|
||||||
* #setContentTypePredicate(Predicate)}.
|
|
||||||
*/
|
|
||||||
@SuppressWarnings("deprecation")
|
|
||||||
@Deprecated
|
|
||||||
public DefaultHttpDataSource(String userAgent, @Nullable Predicate<String> contentTypePredicate) {
|
|
||||||
this(
|
this(
|
||||||
userAgent,
|
userAgent,
|
||||||
contentTypePredicate,
|
|
||||||
DEFAULT_CONNECT_TIMEOUT_MILLIS,
|
|
||||||
DEFAULT_READ_TIMEOUT_MILLIS);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Creates an instance.
|
|
||||||
*
|
|
||||||
* @param userAgent The User-Agent string that should be used.
|
|
||||||
* @param contentTypePredicate An optional {@link Predicate}. If a content type is rejected by the
|
|
||||||
* predicate then a {@link HttpDataSource.InvalidContentTypeException} is thrown from {@link
|
|
||||||
* #open(DataSpec)}.
|
|
||||||
* @param connectTimeoutMillis The connection timeout, in milliseconds. A timeout of zero is
|
|
||||||
* interpreted as an infinite timeout.
|
|
||||||
* @param readTimeoutMillis The read timeout, in milliseconds. A timeout of zero is interpreted as
|
|
||||||
* an infinite timeout.
|
|
||||||
* @deprecated Use {@link #DefaultHttpDataSource(String, int, int)} and {@link
|
|
||||||
* #setContentTypePredicate(Predicate)}.
|
|
||||||
*/
|
|
||||||
@SuppressWarnings("deprecation")
|
|
||||||
@Deprecated
|
|
||||||
public DefaultHttpDataSource(
|
|
||||||
String userAgent,
|
|
||||||
@Nullable Predicate<String> contentTypePredicate,
|
|
||||||
int connectTimeoutMillis,
|
|
||||||
int readTimeoutMillis) {
|
|
||||||
this(
|
|
||||||
userAgent,
|
|
||||||
contentTypePredicate,
|
|
||||||
connectTimeoutMillis,
|
connectTimeoutMillis,
|
||||||
readTimeoutMillis,
|
readTimeoutMillis,
|
||||||
/* allowCrossProtocolRedirects= */ false,
|
allowCrossProtocolRedirects,
|
||||||
/* defaultRequestProperties= */ null);
|
defaultRequestProperties,
|
||||||
|
/* contentTypePredicate= */ null);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
private DefaultHttpDataSource(
|
||||||
* Creates an instance.
|
|
||||||
*
|
|
||||||
* @param userAgent The User-Agent string that should be used.
|
|
||||||
* @param contentTypePredicate An optional {@link Predicate}. If a content type is rejected by the
|
|
||||||
* predicate then a {@link HttpDataSource.InvalidContentTypeException} is thrown from {@link
|
|
||||||
* #open(DataSpec)}.
|
|
||||||
* @param connectTimeoutMillis The connection timeout, in milliseconds. A timeout of zero is
|
|
||||||
* interpreted as an infinite timeout. Pass {@link #DEFAULT_CONNECT_TIMEOUT_MILLIS} to use the
|
|
||||||
* default value.
|
|
||||||
* @param readTimeoutMillis The read timeout, in milliseconds. A timeout of zero is interpreted as
|
|
||||||
* an infinite timeout. Pass {@link #DEFAULT_READ_TIMEOUT_MILLIS} to use the default value.
|
|
||||||
* @param allowCrossProtocolRedirects Whether cross-protocol redirects (i.e. redirects from HTTP
|
|
||||||
* to HTTPS and vice versa) are enabled.
|
|
||||||
* @param defaultRequestProperties The default request properties to be sent to the server as HTTP
|
|
||||||
* headers or {@code null} if not required.
|
|
||||||
* @deprecated Use {@link #DefaultHttpDataSource(String, int, int, boolean, RequestProperties)}
|
|
||||||
* and {@link #setContentTypePredicate(Predicate)}.
|
|
||||||
*/
|
|
||||||
@Deprecated
|
|
||||||
public DefaultHttpDataSource(
|
|
||||||
String userAgent,
|
String userAgent,
|
||||||
@Nullable Predicate<String> contentTypePredicate,
|
|
||||||
int connectTimeoutMillis,
|
int connectTimeoutMillis,
|
||||||
int readTimeoutMillis,
|
int readTimeoutMillis,
|
||||||
boolean allowCrossProtocolRedirects,
|
boolean allowCrossProtocolRedirects,
|
||||||
@Nullable RequestProperties defaultRequestProperties) {
|
@Nullable RequestProperties defaultRequestProperties,
|
||||||
|
@Nullable Predicate<String> contentTypePredicate) {
|
||||||
super(/* isNetwork= */ true);
|
super(/* isNetwork= */ true);
|
||||||
this.userAgent = Assertions.checkNotEmpty(userAgent);
|
this.userAgent = Assertions.checkNotEmpty(userAgent);
|
||||||
this.contentTypePredicate = contentTypePredicate;
|
|
||||||
this.requestProperties = new RequestProperties();
|
|
||||||
this.connectTimeoutMillis = connectTimeoutMillis;
|
this.connectTimeoutMillis = connectTimeoutMillis;
|
||||||
this.readTimeoutMillis = readTimeoutMillis;
|
this.readTimeoutMillis = readTimeoutMillis;
|
||||||
this.allowCrossProtocolRedirects = allowCrossProtocolRedirects;
|
this.allowCrossProtocolRedirects = allowCrossProtocolRedirects;
|
||||||
this.defaultRequestProperties = defaultRequestProperties;
|
this.defaultRequestProperties = defaultRequestProperties;
|
||||||
|
this.contentTypePredicate = contentTypePredicate;
|
||||||
|
this.requestProperties = new RequestProperties();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Sets a content type {@link Predicate}. If a content type is rejected by the predicate then a
|
* @deprecated Use {@link DefaultHttpDataSource.Factory#setContentTypePredicate(Predicate)}
|
||||||
* {@link HttpDataSource.InvalidContentTypeException} is thrown from {@link #open(DataSpec)}.
|
* instead.
|
||||||
*
|
|
||||||
* @param contentTypePredicate The content type {@link Predicate}, or {@code null} to clear a
|
|
||||||
* predicate that was previously set.
|
|
||||||
*/
|
*/
|
||||||
|
@Deprecated
|
||||||
public void setContentTypePredicate(@Nullable Predicate<String> contentTypePredicate) {
|
public void setContentTypePredicate(@Nullable Predicate<String> contentTypePredicate) {
|
||||||
this.contentTypePredicate = contentTypePredicate;
|
this.contentTypePredicate = contentTypePredicate;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -19,10 +19,10 @@ import static com.google.android.exoplayer2.ExoPlayerLibraryInfo.DEFAULT_USER_AG
|
||||||
|
|
||||||
import androidx.annotation.Nullable;
|
import androidx.annotation.Nullable;
|
||||||
import com.google.android.exoplayer2.upstream.HttpDataSource.BaseFactory;
|
import com.google.android.exoplayer2.upstream.HttpDataSource.BaseFactory;
|
||||||
import com.google.android.exoplayer2.upstream.HttpDataSource.Factory;
|
|
||||||
import com.google.android.exoplayer2.util.Assertions;
|
import com.google.android.exoplayer2.util.Assertions;
|
||||||
|
|
||||||
/** A {@link Factory} that produces {@link DefaultHttpDataSource} instances. */
|
/** @deprecated Use {@link DefaultHttpDataSource.Factory} instead. */
|
||||||
|
@Deprecated
|
||||||
public final class DefaultHttpDataSourceFactory extends BaseFactory {
|
public final class DefaultHttpDataSourceFactory extends BaseFactory {
|
||||||
|
|
||||||
private final String userAgent;
|
private final String userAgent;
|
||||||
|
|
@ -110,6 +110,8 @@ public final class DefaultHttpDataSourceFactory extends BaseFactory {
|
||||||
this.allowCrossProtocolRedirects = allowCrossProtocolRedirects;
|
this.allowCrossProtocolRedirects = allowCrossProtocolRedirects;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Calls deprecated constructor.
|
||||||
|
@SuppressWarnings("deprecation")
|
||||||
@Override
|
@Override
|
||||||
protected DefaultHttpDataSource createDataSourceInternal(
|
protected DefaultHttpDataSource createDataSourceInternal(
|
||||||
HttpDataSource.RequestProperties defaultRequestProperties) {
|
HttpDataSource.RequestProperties defaultRequestProperties) {
|
||||||
|
|
|
||||||
|
|
@ -57,19 +57,18 @@ public class DefaultHttpDataSourceTest {
|
||||||
MockWebServer mockWebServer = new MockWebServer();
|
MockWebServer mockWebServer = new MockWebServer();
|
||||||
mockWebServer.enqueue(new MockResponse());
|
mockWebServer.enqueue(new MockResponse());
|
||||||
|
|
||||||
String propertyFromConstructor = "fromConstructor";
|
String propertyFromFactory = "fromFactory";
|
||||||
HttpDataSource.RequestProperties constructorProperties = new HttpDataSource.RequestProperties();
|
Map<String, String> defaultRequestProperties = new HashMap<>();
|
||||||
constructorProperties.set("0", propertyFromConstructor);
|
defaultRequestProperties.put("0", propertyFromFactory);
|
||||||
constructorProperties.set("1", propertyFromConstructor);
|
defaultRequestProperties.put("1", propertyFromFactory);
|
||||||
constructorProperties.set("2", propertyFromConstructor);
|
defaultRequestProperties.put("2", propertyFromFactory);
|
||||||
constructorProperties.set("4", propertyFromConstructor);
|
defaultRequestProperties.put("4", propertyFromFactory);
|
||||||
DefaultHttpDataSource dataSource =
|
DefaultHttpDataSource dataSource =
|
||||||
new DefaultHttpDataSource(
|
new DefaultHttpDataSource.Factory()
|
||||||
/* userAgent= */ "testAgent",
|
.setConnectTimeoutMs(1000)
|
||||||
/* connectTimeoutMillis= */ 1000,
|
.setReadTimeoutMs(1000)
|
||||||
/* readTimeoutMillis= */ 1000,
|
.setDefaultRequestProperties(defaultRequestProperties)
|
||||||
/* allowCrossProtocolRedirects= */ false,
|
.createDataSource();
|
||||||
constructorProperties);
|
|
||||||
|
|
||||||
String propertyFromSetter = "fromSetter";
|
String propertyFromSetter = "fromSetter";
|
||||||
dataSource.setRequestProperty("1", propertyFromSetter);
|
dataSource.setRequestProperty("1", propertyFromSetter);
|
||||||
|
|
@ -92,7 +91,7 @@ public class DefaultHttpDataSourceTest {
|
||||||
dataSource.open(dataSpec);
|
dataSource.open(dataSpec);
|
||||||
|
|
||||||
Headers headers = mockWebServer.takeRequest(10, SECONDS).getHeaders();
|
Headers headers = mockWebServer.takeRequest(10, SECONDS).getHeaders();
|
||||||
assertThat(headers.get("0")).isEqualTo(propertyFromConstructor);
|
assertThat(headers.get("0")).isEqualTo(propertyFromFactory);
|
||||||
assertThat(headers.get("1")).isEqualTo(propertyFromSetter);
|
assertThat(headers.get("1")).isEqualTo(propertyFromSetter);
|
||||||
assertThat(headers.get("2")).isEqualTo(propertyFromDataSpec);
|
assertThat(headers.get("2")).isEqualTo(propertyFromDataSpec);
|
||||||
assertThat(headers.get("3")).isEqualTo(propertyFromDataSpec);
|
assertThat(headers.get("3")).isEqualTo(propertyFromDataSpec);
|
||||||
|
|
@ -102,14 +101,12 @@ public class DefaultHttpDataSourceTest {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void open_invalidResponseCode() throws Exception {
|
public void open_invalidResponseCode() {
|
||||||
DefaultHttpDataSource defaultHttpDataSource =
|
DefaultHttpDataSource defaultHttpDataSource =
|
||||||
new DefaultHttpDataSource(
|
new DefaultHttpDataSource.Factory()
|
||||||
/* userAgent= */ "testAgent",
|
.setConnectTimeoutMs(1000)
|
||||||
/* connectTimeoutMillis= */ 1000,
|
.setReadTimeoutMs(1000)
|
||||||
/* readTimeoutMillis= */ 1000,
|
.createDataSource();
|
||||||
/* allowCrossProtocolRedirects= */ false,
|
|
||||||
/* defaultRequestProperties= */ null);
|
|
||||||
|
|
||||||
MockWebServer mockWebServer = new MockWebServer();
|
MockWebServer mockWebServer = new MockWebServer();
|
||||||
mockWebServer.enqueue(
|
mockWebServer.enqueue(
|
||||||
|
|
@ -128,4 +125,22 @@ public class DefaultHttpDataSourceTest {
|
||||||
assertThat(exception.responseCode).isEqualTo(404);
|
assertThat(exception.responseCode).isEqualTo(404);
|
||||||
assertThat(exception.responseBody).isEqualTo(TestUtil.createByteArray(1, 2, 3));
|
assertThat(exception.responseBody).isEqualTo(TestUtil.createByteArray(1, 2, 3));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void factory_setRequestPropertyAfterCreation_setsCorrectHeaders() throws Exception {
|
||||||
|
MockWebServer mockWebServer = new MockWebServer();
|
||||||
|
mockWebServer.enqueue(new MockResponse());
|
||||||
|
DataSpec dataSpec =
|
||||||
|
new DataSpec.Builder().setUri(mockWebServer.url("/test-path").toString()).build();
|
||||||
|
DefaultHttpDataSource.Factory factory = new DefaultHttpDataSource.Factory();
|
||||||
|
HttpDataSource dataSource = factory.createDataSource();
|
||||||
|
|
||||||
|
Map<String, String> defaultRequestProperties = new HashMap<>();
|
||||||
|
defaultRequestProperties.put("0", "afterCreation");
|
||||||
|
factory.setDefaultRequestProperties(defaultRequestProperties);
|
||||||
|
dataSource.open(dataSpec);
|
||||||
|
|
||||||
|
Headers headers = mockWebServer.takeRequest(10, SECONDS).getHeaders();
|
||||||
|
assertThat(headers.get("0")).isEqualTo("afterCreation");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -26,7 +26,8 @@ import com.google.android.exoplayer2.upstream.DefaultHttpDataSource;
|
||||||
/** Fake {@link MediaChunk}. */
|
/** Fake {@link MediaChunk}. */
|
||||||
public final class FakeMediaChunk extends MediaChunk {
|
public final class FakeMediaChunk extends MediaChunk {
|
||||||
|
|
||||||
private static final DataSource DATA_SOURCE = new DefaultHttpDataSource("TEST_AGENT");
|
private static final DataSource DATA_SOURCE =
|
||||||
|
new DefaultHttpDataSource.Factory().setUserAgent("TEST_AGENT").createDataSource();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates a fake media chunk.
|
* Creates a fake media chunk.
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue