Stabilise DefaultDataSource.Factory, including 2 constructors

This allows apps to customise the HTTP DataSource while still supporting
non-HTTP URIs as well, as documented in our dev guide:
https://exoplayer.dev/network-stacks.html#configuring-exoplayer-to-use-a-specific-network-stack

PiperOrigin-RevId: 437209880
This commit is contained in:
ibaker 2022-03-25 10:48:41 +00:00 committed by Ian Baker
parent c87d16ca63
commit be4cbb886b

View File

@ -54,7 +54,6 @@ import java.util.Map;
* #DefaultDataSource(Context, DataSource)}. * #DefaultDataSource(Context, DataSource)}.
* </ul> * </ul>
*/ */
@UnstableApi
public final class DefaultDataSource implements DataSource { public final class DefaultDataSource implements DataSource {
/** {@link DataSource.Factory} for {@link DefaultDataSource} instances. */ /** {@link DataSource.Factory} for {@link DefaultDataSource} instances. */
@ -98,11 +97,13 @@ public final class DefaultDataSource implements DataSource {
* @param transferListener The listener that will be used. * @param transferListener The listener that will be used.
* @return This factory. * @return This factory.
*/ */
@UnstableApi
public Factory setTransferListener(@Nullable TransferListener transferListener) { public Factory setTransferListener(@Nullable TransferListener transferListener) {
this.transferListener = transferListener; this.transferListener = transferListener;
return this; return this;
} }
@UnstableApi
@Override @Override
public DefaultDataSource createDataSource() { public DefaultDataSource createDataSource() {
DefaultDataSource dataSource = DefaultDataSource dataSource =
@ -144,6 +145,7 @@ public final class DefaultDataSource implements DataSource {
* *
* @param context A context. * @param context A context.
*/ */
@UnstableApi
public DefaultDataSource(Context context, boolean allowCrossProtocolRedirects) { public DefaultDataSource(Context context, boolean allowCrossProtocolRedirects) {
this( this(
context, context,
@ -162,6 +164,7 @@ public final class DefaultDataSource implements DataSource {
* @param allowCrossProtocolRedirects Whether cross-protocol redirects (i.e. redirects from HTTP * @param allowCrossProtocolRedirects Whether cross-protocol redirects (i.e. redirects from HTTP
* to HTTPS and vice versa) are enabled when fetching remote data. * to HTTPS and vice versa) are enabled when fetching remote data.
*/ */
@UnstableApi
public DefaultDataSource( public DefaultDataSource(
Context context, @Nullable String userAgent, boolean allowCrossProtocolRedirects) { Context context, @Nullable String userAgent, boolean allowCrossProtocolRedirects) {
this( this(
@ -185,6 +188,7 @@ public final class DefaultDataSource implements DataSource {
* @param allowCrossProtocolRedirects Whether cross-protocol redirects (i.e. redirects from HTTP * @param allowCrossProtocolRedirects Whether cross-protocol redirects (i.e. redirects from HTTP
* to HTTPS and vice versa) are enabled when fetching remote data. * to HTTPS and vice versa) are enabled when fetching remote data.
*/ */
@UnstableApi
public DefaultDataSource( public DefaultDataSource(
Context context, Context context,
@Nullable String userAgent, @Nullable String userAgent,
@ -209,12 +213,14 @@ public final class DefaultDataSource implements DataSource {
* @param baseDataSource 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).
*/ */
@UnstableApi
public DefaultDataSource(Context context, DataSource baseDataSource) { public DefaultDataSource(Context context, DataSource baseDataSource) {
this.context = context.getApplicationContext(); this.context = context.getApplicationContext();
this.baseDataSource = Assertions.checkNotNull(baseDataSource); this.baseDataSource = Assertions.checkNotNull(baseDataSource);
transferListeners = new ArrayList<>(); transferListeners = new ArrayList<>();
} }
@UnstableApi
@Override @Override
public void addTransferListener(TransferListener transferListener) { public void addTransferListener(TransferListener transferListener) {
Assertions.checkNotNull(transferListener); Assertions.checkNotNull(transferListener);
@ -229,6 +235,7 @@ public final class DefaultDataSource implements DataSource {
maybeAddListenerToDataSource(rawResourceDataSource, transferListener); maybeAddListenerToDataSource(rawResourceDataSource, transferListener);
} }
@UnstableApi
@Override @Override
public long open(DataSpec dataSpec) throws IOException { public long open(DataSpec dataSpec) throws IOException {
Assertions.checkState(dataSource == null); Assertions.checkState(dataSource == null);
@ -260,22 +267,26 @@ public final class DefaultDataSource implements DataSource {
return dataSource.open(dataSpec); return dataSource.open(dataSpec);
} }
@UnstableApi
@Override @Override
public int read(byte[] buffer, int offset, int length) throws IOException { public int read(byte[] buffer, int offset, int length) throws IOException {
return Assertions.checkNotNull(dataSource).read(buffer, offset, length); return Assertions.checkNotNull(dataSource).read(buffer, offset, length);
} }
@UnstableApi
@Override @Override
@Nullable @Nullable
public Uri getUri() { public Uri getUri() {
return dataSource == null ? null : dataSource.getUri(); return dataSource == null ? null : dataSource.getUri();
} }
@UnstableApi
@Override @Override
public Map<String, List<String>> getResponseHeaders() { public Map<String, List<String>> getResponseHeaders() {
return dataSource == null ? Collections.emptyMap() : dataSource.getResponseHeaders(); return dataSource == null ? Collections.emptyMap() : dataSource.getResponseHeaders();
} }
@UnstableApi
@Override @Override
public void close() throws IOException { public void close() throws IOException {
if (dataSource != null) { if (dataSource != null) {