diff --git a/exoplayer2-ext-icy/build.gradle b/exoplayer2-ext-icy/build.gradle index 0931fcf..d922e37 100644 --- a/exoplayer2-ext-icy/build.gradle +++ b/exoplayer2-ext-icy/build.gradle @@ -54,6 +54,7 @@ android { dependencies { // Runtime dependencies implementation 'com.google.android.exoplayer:exoplayer-core:2.8.4' + implementation 'com.google.android.exoplayer:extension-okhttp:2.8.4' // Test dependencies testImplementation 'junit:junit:4.12' diff --git a/exoplayer2-ext-icy/src/main/java/saschpe/exoplayer2/ext/icy/IcyHttpDataSource.java b/exoplayer2-ext-icy/src/main/java/saschpe/exoplayer2/ext/icy/IcyHttpDataSource.java index e625e45..5bbe5e9 100644 --- a/exoplayer2-ext-icy/src/main/java/saschpe/exoplayer2/ext/icy/IcyHttpDataSource.java +++ b/exoplayer2-ext-icy/src/main/java/saschpe/exoplayer2/ext/icy/IcyHttpDataSource.java @@ -4,9 +4,9 @@ import android.support.annotation.Nullable; import android.util.Log; +import com.google.android.exoplayer2.ext.okhttp.OkHttpDataSource; import com.google.android.exoplayer2.upstream.DataSource; import com.google.android.exoplayer2.upstream.DataSpec; -import com.google.android.exoplayer2.upstream.DefaultHttpDataSource; import com.google.android.exoplayer2.upstream.TransferListener; import com.google.android.exoplayer2.util.Predicate; @@ -14,11 +14,14 @@ import java.util.List; import java.util.Map; +import okhttp3.CacheControl; +import okhttp3.Call; + /** * https://cast.readme.io/v1.0/docs/icy * http://www.smackfu.com/stuff/programming/shoutcast.html */ -public final class IcyHttpDataSource extends DefaultHttpDataSource { +public final class IcyHttpDataSource extends OkHttpDataSource { private static final String TAG = IcyHttpDataSource.class.getSimpleName(); private static final String REQUEST_HEADER_ICY_METAINT_KEY = "Icy-MetaData"; @@ -49,14 +52,13 @@ public interface IcyMetadataListener { } private IcyHttpDataSource( - @NonNull final String userAgent, + @NonNull Call.Factory callFactory, + @Nullable final String userAgent, @Nullable final Predicate contentTypePredicate, - @Nullable final TransferListener listener, - final int connectTimeoutMillis, - final int readTimeoutMillis, - final boolean allowCrossProtocolRedirects, + @Nullable final TransferListener listener, + @Nullable CacheControl cacheControl, @Nullable final RequestProperties defaultRequestProperties) { - super(userAgent, contentTypePredicate, listener, connectTimeoutMillis, readTimeoutMillis, allowCrossProtocolRedirects, defaultRequestProperties); + super(callFactory, userAgent, contentTypePredicate, listener, cacheControl, defaultRequestProperties); defaultRequestProperties.set(REQUEST_HEADER_ICY_METAINT_KEY, REQUEST_HEADER_ICY_METAINT_VALUE); // See class Builder } @@ -202,18 +204,22 @@ private IcyMetadata parseMetadata(final String metaDataString) { } public final static class Builder { + private Call.Factory callFactory; private String userAgent; private Predicate contentTypePredicate; - private TransferListener listener; - private int connectTimeoutMillis; - private int readTimeoutMillis; - private boolean allowCrossProtocolRedirects; + private TransferListener listener; + private CacheControl cacheControl; private RequestProperties defaultRequestProperties = new RequestProperties(); private IcyHeadersListener icyHeadersListener; private IcyMetadataListener icyMetadataListener; - public Builder(@NonNull final String userAgent) { + public Builder(@NonNull Call.Factory callFactory) { + this.callFactory = callFactory; + } + + public Builder setUserAgent(@NonNull final String userAgent) { this.userAgent = userAgent; + return this; } public Builder setContentTypePredicate(@NonNull final Predicate contentTypePredicate) { @@ -226,18 +232,8 @@ public Builder setTransferListener(@NonNull final TransferListener * The exact contents isn't specified and implementation specific. It's therefore up to the * user to figure what format a given stream returns. */ diff --git a/exoplayer2-ext-icy/src/main/java/saschpe/exoplayer2/ext/icy/IcyHttpDataSourceFactory.java b/exoplayer2-ext-icy/src/main/java/saschpe/exoplayer2/ext/icy/IcyHttpDataSourceFactory.java index 454da17..47840bc 100644 --- a/exoplayer2-ext-icy/src/main/java/saschpe/exoplayer2/ext/icy/IcyHttpDataSourceFactory.java +++ b/exoplayer2-ext-icy/src/main/java/saschpe/exoplayer2/ext/icy/IcyHttpDataSourceFactory.java @@ -2,17 +2,24 @@ import android.support.annotation.NonNull; +import com.google.android.exoplayer2.ext.okhttp.OkHttpDataSource; import com.google.android.exoplayer2.upstream.DataSource; import com.google.android.exoplayer2.upstream.HttpDataSource; import com.google.android.exoplayer2.upstream.TransferListener; +import com.google.android.exoplayer2.util.Predicate; -/** A {@link HttpDataSource.Factory} that produces {@link IcyHttpDataSource} instances. */ -public final class IcyHttpDataSourceFactory extends HttpDataSource.BaseFactory { +import okhttp3.CacheControl; +import okhttp3.Call; + +/** + * A {@link HttpDataSource.Factory} that produces {@link IcyHttpDataSource} instances. + */ +public final class IcyHttpDataSourceFactory extends OkHttpDataSource.BaseFactory { + private Call.Factory callFactory; private String userAgent; + private Predicate contentTypePredicate; private TransferListener listener; - private int connectTimeoutMillis; - private int readTimeoutMillis; - private boolean allowCrossProtocolRedirects; + private CacheControl cacheControl; private IcyHttpDataSource.IcyHeadersListener icyHeadersListener; private IcyHttpDataSource.IcyMetadataListener icyMetadataListener; @@ -26,41 +33,30 @@ private IcyHttpDataSourceFactory() { public final static class Builder { private final IcyHttpDataSourceFactory factory; - /** - * Sets {@link - * IcyHttpDataSource#DEFAULT_CONNECT_TIMEOUT_MILLIS} as the connection timeout, {@link - * IcyHttpDataSource#DEFAULT_READ_TIMEOUT_MILLIS} as the read timeout and disables - * cross-protocol redirects. - * - * @param userAgent The user agent - */ - public Builder(@NonNull final String userAgent) { + public Builder(@NonNull Call.Factory callFactory) { // Apply defaults factory = new IcyHttpDataSourceFactory(); - factory.userAgent = userAgent; + factory.callFactory = callFactory; factory.listener = null; - factory.connectTimeoutMillis = IcyHttpDataSource.DEFAULT_CONNECT_TIMEOUT_MILLIS; - factory.readTimeoutMillis = IcyHttpDataSource.DEFAULT_READ_TIMEOUT_MILLIS; - factory.allowCrossProtocolRedirects = false; } - public Builder setTransferListener(@NonNull final TransferListener listener) { - factory.listener = listener; + public Builder setUserAgent(@NonNull final String userAgent) { + factory.userAgent = userAgent; return this; } - public Builder setConnectTimeoutMillis(@NonNull final int connectTimeoutMillis) { - factory.connectTimeoutMillis = connectTimeoutMillis; + public Builder setContentTypePredicate(@NonNull final Predicate contentTypePredicate) { + factory.contentTypePredicate = contentTypePredicate; return this; } - public Builder setReadTimeoutMillis(@NonNull final int readTimeoutMillis) { - factory.readTimeoutMillis = readTimeoutMillis; + public Builder setTransferListener(@NonNull final TransferListener listener) { + factory.listener = listener; return this; } - public Builder setAllowCrossProtocolRedirects(@NonNull final boolean allowCrossProtocolRedirects) { - factory.allowCrossProtocolRedirects = allowCrossProtocolRedirects; + public Builder setCacheControl(@NonNull final CacheControl cacheControl) { + factory.cacheControl = cacheControl; return this; } @@ -81,11 +77,11 @@ public IcyHttpDataSourceFactory build() { @Override protected IcyHttpDataSource createDataSourceInternal(@NonNull HttpDataSource.RequestProperties defaultRequestProperties) { - return new IcyHttpDataSource.Builder(userAgent) + return new IcyHttpDataSource.Builder(callFactory) + .setUserAgent(userAgent) + .setContentTypePredicate(contentTypePredicate) .setTransferListener(listener) - .setConnectTimeoutMillis(connectTimeoutMillis) - .setReadTimeoutMillis(readTimeoutMillis) - .setAllowCrossProtocolRedirects(allowCrossProtocolRedirects) + .setCacheControl(cacheControl) .setDefaultRequestProperties(defaultRequestProperties) .setIcyHeadersListener(icyHeadersListener) .setIcyMetadataListener(icyMetadataListener)