diff --git a/library/core/proguard-rules.txt b/library/core/proguard-rules.txt index 128f227896..c599a93e5a 100644 --- a/library/core/proguard-rules.txt +++ b/library/core/proguard-rules.txt @@ -51,15 +51,15 @@ # Constructors accessed via reflection in DefaultDownloaderFactory -dontnote com.google.android.exoplayer2.source.dash.offline.DashDownloader -keepclassmembers class com.google.android.exoplayer2.source.dash.offline.DashDownloader { - (android.net.Uri, java.util.List, com.google.android.exoplayer2.upstream.cache.CacheDataSource$Factory, java.util.concurrent.Executor); + (com.google.android.exoplayer2.MediaItem, com.google.android.exoplayer2.upstream.cache.CacheDataSource$Factory, java.util.concurrent.Executor); } -dontnote com.google.android.exoplayer2.source.hls.offline.HlsDownloader -keepclassmembers class com.google.android.exoplayer2.source.hls.offline.HlsDownloader { - (android.net.Uri, java.util.List, com.google.android.exoplayer2.upstream.cache.CacheDataSource$Factory, java.util.concurrent.Executor); + (com.google.android.exoplayer2.MediaItem, com.google.android.exoplayer2.upstream.cache.CacheDataSource$Factory, java.util.concurrent.Executor); } -dontnote com.google.android.exoplayer2.source.smoothstreaming.offline.SsDownloader -keepclassmembers class com.google.android.exoplayer2.source.smoothstreaming.offline.SsDownloader { - (android.net.Uri, java.util.List, com.google.android.exoplayer2.upstream.cache.CacheDataSource$Factory, java.util.concurrent.Executor); + (com.google.android.exoplayer2.MediaItem, com.google.android.exoplayer2.upstream.cache.CacheDataSource$Factory, java.util.concurrent.Executor); } # Constructors accessed via reflection in DefaultMediaSourceFactory diff --git a/library/core/src/main/java/com/google/android/exoplayer2/offline/DefaultDownloadIndex.java b/library/core/src/main/java/com/google/android/exoplayer2/offline/DefaultDownloadIndex.java index 90bf57cd47..65701e5b22 100644 --- a/library/core/src/main/java/com/google/android/exoplayer2/offline/DefaultDownloadIndex.java +++ b/library/core/src/main/java/com/google/android/exoplayer2/offline/DefaultDownloadIndex.java @@ -304,6 +304,8 @@ public final class DefaultDownloadIndex implements WritableDownloadIndex { } private void putDownloadInternal(Download download, SQLiteDatabase database) { + byte[] keySetId = + download.request.keySetId == null ? Util.EMPTY_BYTE_ARRAY : download.request.keySetId; ContentValues values = new ContentValues(); values.put(COLUMN_ID, download.request.id); values.put(COLUMN_MIME_TYPE, download.request.mimeType); @@ -319,7 +321,7 @@ public final class DefaultDownloadIndex implements WritableDownloadIndex { values.put(COLUMN_FAILURE_REASON, download.failureReason); values.put(COLUMN_PERCENT_DOWNLOADED, download.getPercentDownloaded()); values.put(COLUMN_BYTES_DOWNLOADED, download.getBytesDownloaded()); - values.put(COLUMN_KEY_SET_ID, download.request.keySetId); + values.put(COLUMN_KEY_SET_ID, keySetId); database.replaceOrThrow(tableName, /* nullColumnHack= */ null, values); } @@ -430,13 +432,14 @@ public final class DefaultDownloadIndex implements WritableDownloadIndex { } private static Download getDownloadForCurrentRow(Cursor cursor) { + byte[] keySetId = cursor.getBlob(COLUMN_INDEX_KEY_SET_ID); DownloadRequest request = new DownloadRequest( /* id= */ cursor.getString(COLUMN_INDEX_ID), /* uri= */ Uri.parse(cursor.getString(COLUMN_INDEX_URI)), /* mimeType= */ cursor.getString(COLUMN_INDEX_MIME_TYPE), /* streamKeys= */ decodeStreamKeys(cursor.getString(COLUMN_INDEX_STREAM_KEYS)), - /* keySetId= */ cursor.getBlob(COLUMN_INDEX_KEY_SET_ID), + /* keySetId= */ keySetId.length > 0 ? keySetId : null, /* customCacheKey= */ cursor.getString(COLUMN_INDEX_CUSTOM_CACHE_KEY), /* data= */ cursor.getBlob(COLUMN_INDEX_DATA)); DownloadProgress downloadProgress = new DownloadProgress(); diff --git a/library/core/src/main/java/com/google/android/exoplayer2/offline/DefaultDownloaderFactory.java b/library/core/src/main/java/com/google/android/exoplayer2/offline/DefaultDownloaderFactory.java index 786d60b545..194450a56a 100644 --- a/library/core/src/main/java/com/google/android/exoplayer2/offline/DefaultDownloaderFactory.java +++ b/library/core/src/main/java/com/google/android/exoplayer2/offline/DefaultDownloaderFactory.java @@ -15,7 +15,6 @@ */ package com.google.android.exoplayer2.offline; -import android.net.Uri; import android.util.SparseArray; import androidx.annotation.Nullable; import com.google.android.exoplayer2.C; @@ -24,7 +23,6 @@ import com.google.android.exoplayer2.upstream.cache.CacheDataSource; import com.google.android.exoplayer2.util.Assertions; import com.google.android.exoplayer2.util.Util; import java.lang.reflect.Constructor; -import java.util.List; import java.util.concurrent.Executor; /** @@ -95,9 +93,15 @@ public class DefaultDownloaderFactory implements DownloaderFactory { if (constructor == null) { throw new IllegalStateException("Module missing for content type " + contentType); } + MediaItem mediaItem = + new MediaItem.Builder() + .setUri(request.uri) + .setStreamKeys(request.streamKeys) + .setCustomCacheKey(request.customCacheKey) + .setDrmKeySetId(request.keySetId) + .build(); try { - return constructor.newInstance( - request.uri, request.streamKeys, cacheDataSourceFactory, executor); + return constructor.newInstance(mediaItem, cacheDataSourceFactory, executor); } catch (Exception e) { throw new IllegalStateException( "Failed to instantiate downloader for content type " + contentType); @@ -140,7 +144,7 @@ public class DefaultDownloaderFactory implements DownloaderFactory { try { return clazz .asSubclass(Downloader.class) - .getConstructor(Uri.class, List.class, CacheDataSource.Factory.class, Executor.class); + .getConstructor(MediaItem.class, CacheDataSource.Factory.class, Executor.class); } catch (NoSuchMethodException e) { // The downloader is present, but the expected constructor is missing. throw new IllegalStateException("Downloader constructor missing", e); diff --git a/library/core/src/main/java/com/google/android/exoplayer2/offline/DownloadRequest.java b/library/core/src/main/java/com/google/android/exoplayer2/offline/DownloadRequest.java index cecb76983b..b13e6ee767 100644 --- a/library/core/src/main/java/com/google/android/exoplayer2/offline/DownloadRequest.java +++ b/library/core/src/main/java/com/google/android/exoplayer2/offline/DownloadRequest.java @@ -48,11 +48,8 @@ public final class DownloadRequest implements Parcelable { @Nullable public final String mimeType; /** Stream keys to be downloaded. If empty, all streams will be downloaded. */ public final List streamKeys; - /** - * The key set id of the offline licence if the content is protected with DRM, or empty if no - * license is needed. - */ - public final byte[] keySetId; + /** The key set id of the offline licence if the content is protected with DRM. */ + @Nullable public final byte[] keySetId; /** * Custom key for cache indexing, or null. Must be null for DASH, HLS and SmoothStreaming * downloads. @@ -88,8 +85,7 @@ public final class DownloadRequest implements Parcelable { ArrayList mutableKeys = new ArrayList<>(streamKeys); Collections.sort(mutableKeys); this.streamKeys = Collections.unmodifiableList(mutableKeys); - this.keySetId = - keySetId != null ? Arrays.copyOf(keySetId, keySetId.length) : Util.EMPTY_BYTE_ARRAY; + this.keySetId = keySetId != null ? Arrays.copyOf(keySetId, keySetId.length) : null; this.customCacheKey = customCacheKey; this.data = data != null ? Arrays.copyOf(data, data.length) : Util.EMPTY_BYTE_ARRAY; } @@ -104,7 +100,7 @@ public final class DownloadRequest implements Parcelable { mutableStreamKeys.add(in.readParcelable(StreamKey.class.getClassLoader())); } streamKeys = Collections.unmodifiableList(mutableStreamKeys); - keySetId = castNonNull(in.createByteArray()); + keySetId = in.createByteArray(); customCacheKey = in.readString(); data = castNonNull(in.createByteArray()); } diff --git a/testutils/src/main/java/com/google/android/exoplayer2/testutil/DownloadBuilder.java b/testutils/src/main/java/com/google/android/exoplayer2/testutil/DownloadBuilder.java index cc5565a69c..c37d0df13c 100644 --- a/testutils/src/main/java/com/google/android/exoplayer2/testutil/DownloadBuilder.java +++ b/testutils/src/main/java/com/google/android/exoplayer2/testutil/DownloadBuilder.java @@ -41,7 +41,7 @@ public final class DownloadBuilder { private Uri uri; @Nullable private String mimeType; private List streamKeys; - private byte[] keySetId; + @Nullable private byte[] keySetId; @Nullable private String cacheKey; private byte[] customMetadata; @@ -63,7 +63,7 @@ public final class DownloadBuilder { Uri.parse("uri"), /* mimeType= */ null, /* streamKeys= */ Collections.emptyList(), - /* keySetId= */ new byte[0], + /* keySetId= */ null, /* cacheKey= */ null, /* customMetadata= */ new byte[0]); } @@ -90,7 +90,7 @@ public final class DownloadBuilder { Uri uri, @Nullable String mimeType, List streamKeys, - byte[] keySetId, + @Nullable byte[] keySetId, @Nullable String cacheKey, byte[] customMetadata) { this.id = id;