diff --git a/RELEASENOTES.md b/RELEASENOTES.md index 17a8fb55d5..17e8649509 100644 --- a/RELEASENOTES.md +++ b/RELEASENOTES.md @@ -168,6 +168,7 @@ [#6725](https://github.com/google/ExoPlayer/issues/6725), [#7066](https://github.com/google/ExoPlayer/issues/7066)). * Downloads and caching: + * Add builder in `DownloadRequest`. * Support passing an `Executor` to `DefaultDownloaderFactory` on which data downloads are performed. * Parallelize and merge downloads in `SegmentDownloader` to improve diff --git a/library/core/src/main/java/com/google/android/exoplayer2/offline/ActionFile.java b/library/core/src/main/java/com/google/android/exoplayer2/offline/ActionFile.java index 0807241b1e..2f7db22326 100644 --- a/library/core/src/main/java/com/google/android/exoplayer2/offline/ActionFile.java +++ b/library/core/src/main/java/com/google/android/exoplayer2/offline/ActionFile.java @@ -140,10 +140,13 @@ import java.util.List; // Remove actions are not supported anymore. throw new UnsupportedRequestException(); } - // keySetId and mimeType were not supported. Set keySetId to null and try to infer the mime - // type from the download type. - return new DownloadRequest( - id, uri, inferMimeType(downloadType), keys, /* keySetId= */ null, customCacheKey, data); + + return new DownloadRequest.Builder(id, uri) + .setMimeType(inferMimeType(downloadType)) + .setStreamKeys(keys) + .setCustomCacheKey(customCacheKey) + .setData(data) + .build(); } private static StreamKey readKey(String type, int version, DataInputStream input) 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 65701e5b22..d9a060fe2d 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 @@ -434,14 +434,15 @@ 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= */ keySetId.length > 0 ? keySetId : null, - /* customCacheKey= */ cursor.getString(COLUMN_INDEX_CUSTOM_CACHE_KEY), - /* data= */ cursor.getBlob(COLUMN_INDEX_DATA)); + new DownloadRequest.Builder( + /* id= */ cursor.getString(COLUMN_INDEX_ID), + /* uri= */ Uri.parse(cursor.getString(COLUMN_INDEX_URI))) + .setMimeType(cursor.getString(COLUMN_INDEX_MIME_TYPE)) + .setStreamKeys(decodeStreamKeys(cursor.getString(COLUMN_INDEX_STREAM_KEYS))) + .setKeySetId(keySetId.length > 0 ? keySetId : null) + .setCustomCacheKey(cursor.getString(COLUMN_INDEX_CUSTOM_CACHE_KEY)) + .setData(cursor.getBlob(COLUMN_INDEX_DATA)) + .build(); DownloadProgress downloadProgress = new DownloadProgress(); downloadProgress.bytesDownloaded = cursor.getLong(COLUMN_INDEX_BYTES_DOWNLOADED); downloadProgress.percentDownloaded = cursor.getFloat(COLUMN_INDEX_PERCENT_DOWNLOADED); @@ -485,14 +486,13 @@ public final class DefaultDownloadIndex implements WritableDownloadIndex { * 13 bytes_downloaded integer */ DownloadRequest request = - new DownloadRequest( - /* id= */ cursor.getString(0), - /* uri= */ Uri.parse(cursor.getString(2)), - /* mimeType= */ inferMimeType(cursor.getString(1)), - /* streamKeys= */ decodeStreamKeys(cursor.getString(3)), - /* keySetId= */ null, - /* customCacheKey= */ cursor.getString(4), - /* data= */ cursor.getBlob(5)); + new DownloadRequest.Builder( + /* id= */ cursor.getString(0), /* uri= */ Uri.parse(cursor.getString(2))) + .setMimeType(inferMimeType(cursor.getString(1))) + .setStreamKeys(decodeStreamKeys(cursor.getString(3))) + .setCustomCacheKey(cursor.getString(4)) + .setData(cursor.getBlob(5)) + .build(); DownloadProgress downloadProgress = new DownloadProgress(); downloadProgress.bytesDownloaded = cursor.getLong(13); downloadProgress.percentDownloaded = cursor.getFloat(12); diff --git a/library/core/src/main/java/com/google/android/exoplayer2/offline/DownloadHelper.java b/library/core/src/main/java/com/google/android/exoplayer2/offline/DownloadHelper.java index 2d373b6fb3..909a56e8f6 100644 --- a/library/core/src/main/java/com/google/android/exoplayer2/offline/DownloadHelper.java +++ b/library/core/src/main/java/com/google/android/exoplayer2/offline/DownloadHelper.java @@ -745,14 +745,11 @@ public final class DownloadHelper { public DownloadRequest getDownloadRequest(String id, @Nullable byte[] data) { if (mediaSource == null) { // TODO: add support for DRM (keySetId) [Internal ref: b/158980798] - return new DownloadRequest( - id, - playbackProperties.uri, - playbackProperties.mimeType, - /* streamKeys= */ Collections.emptyList(), - /* keySetId= */ null, - playbackProperties.customCacheKey, - data); + return new DownloadRequest.Builder(id, playbackProperties.uri) + .setMimeType(playbackProperties.mimeType) + .setCustomCacheKey(playbackProperties.customCacheKey) + .setData(data) + .build(); } assertPreparedWithMedia(); List streamKeys = new ArrayList<>(); @@ -767,14 +764,12 @@ public final class DownloadHelper { streamKeys.addAll(mediaPreparer.mediaPeriods[periodIndex].getStreamKeys(allSelections)); } // TODO: add support for DRM (keySetId) [Internal ref: b/158980798] - return new DownloadRequest( - id, - playbackProperties.uri, - playbackProperties.mimeType, - streamKeys, - /* keySetId= */ null, - playbackProperties.customCacheKey, - data); + return new DownloadRequest.Builder(id, playbackProperties.uri) + .setMimeType(playbackProperties.mimeType) + .setStreamKeys(streamKeys) + .setCustomCacheKey(playbackProperties.customCacheKey) + .setData(data) + .build(); } // Initialization of array of Lists. 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 af0f329834..60c2289f2b 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 @@ -24,6 +24,7 @@ import androidx.annotation.Nullable; import com.google.android.exoplayer2.C; import com.google.android.exoplayer2.util.Assertions; import com.google.android.exoplayer2.util.Util; +import com.google.common.collect.ImmutableList; import java.io.IOException; import java.util.ArrayList; import java.util.Arrays; @@ -36,6 +37,64 @@ public final class DownloadRequest implements Parcelable { /** Thrown when the encoded request data belongs to an unsupported request type. */ public static class UnsupportedRequestException extends IOException {} + /** A builder for download requests. */ + public static class Builder { + private final String id; + private final Uri uri; + @Nullable private String mimeType; + @Nullable private List streamKeys; + @Nullable private byte[] keySetId; + @Nullable private String customCacheKey; + @Nullable private byte[] data; + + /** Creates a new instance with the specified id and uri. */ + public Builder(String id, Uri uri) { + this.id = id; + this.uri = uri; + } + + /** Sets the {@link DownloadRequest#mimeType}. */ + public Builder setMimeType(@Nullable String mimeType) { + this.mimeType = mimeType; + return this; + } + + /** Sets the {@link DownloadRequest#streamKeys}. */ + public Builder setStreamKeys(@Nullable List streamKeys) { + this.streamKeys = streamKeys; + return this; + } + + /** Sets the {@link DownloadRequest#keySetId}. */ + public Builder setKeySetId(@Nullable byte[] keySetId) { + this.keySetId = keySetId; + return this; + } + + /** Sets the {@link DownloadRequest#customCacheKey}. */ + public Builder setCustomCacheKey(@Nullable String customCacheKey) { + this.customCacheKey = customCacheKey; + return this; + } + + /** Sets the {@link DownloadRequest#data}. */ + public Builder setData(@Nullable byte[] data) { + this.data = data; + return this; + } + + public DownloadRequest build() { + return new DownloadRequest( + id, + uri, + mimeType, + streamKeys != null ? streamKeys : ImmutableList.of(), + keySetId, + customCacheKey, + data); + } + } + /** The unique content id. */ public final String id; /** The uri being downloaded. */ @@ -66,7 +125,7 @@ public final class DownloadRequest implements Parcelable { * @param customCacheKey See {@link #customCacheKey}. * @param data See {@link #data}. */ - public DownloadRequest( + private DownloadRequest( String id, Uri uri, @Nullable String mimeType, diff --git a/library/core/src/test/java/com/google/android/exoplayer2/offline/ActionFileTest.java b/library/core/src/test/java/com/google/android/exoplayer2/offline/ActionFileTest.java index ffc34cac30..ad2685bad0 100644 --- a/library/core/src/test/java/com/google/android/exoplayer2/offline/ActionFileTest.java +++ b/library/core/src/test/java/com/google/android/exoplayer2/offline/ActionFileTest.java @@ -26,7 +26,6 @@ import com.google.android.exoplayer2.util.Util; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; -import java.util.Collections; import org.junit.After; import org.junit.Assert; import org.junit.Before; @@ -126,13 +125,9 @@ public class ActionFileTest { } private static DownloadRequest buildExpectedRequest(Uri uri, byte[] data) { - return new DownloadRequest( - /* id= */ uri.toString(), - uri, - /* mimeType= */ MimeTypes.VIDEO_UNKNOWN, - /* streamKeys= */ Collections.emptyList(), - /* keySetId= */ null, - /* customCacheKey= */ null, - data); + return new DownloadRequest.Builder(/* id= */ uri.toString(), uri) + .setMimeType(MimeTypes.VIDEO_UNKNOWN) + .setData(data) + .build(); } } diff --git a/library/core/src/test/java/com/google/android/exoplayer2/offline/ActionFileUpgradeUtilTest.java b/library/core/src/test/java/com/google/android/exoplayer2/offline/ActionFileUpgradeUtilTest.java index df45f88401..1eea7c0d80 100644 --- a/library/core/src/test/java/com/google/android/exoplayer2/offline/ActionFileUpgradeUtilTest.java +++ b/library/core/src/test/java/com/google/android/exoplayer2/offline/ActionFileUpgradeUtilTest.java @@ -66,23 +66,18 @@ public class ActionFileUpgradeUtilTest { output.write(actionFileBytes); } DownloadRequest expectedRequest1 = - new DownloadRequest( - /* id= */ "http://www.test.com/1/video.mp4", - Uri.parse("http://www.test.com/1/video.mp4"), - /* mimeType= */ MimeTypes.VIDEO_UNKNOWN, - /* streamKeys= */ ImmutableList.of(), - /* keySetId= */ null, - /* customCacheKey= */ null, - /* data= */ null); + new DownloadRequest.Builder( + /* id= */ "http://www.test.com/1/video.mp4", + Uri.parse("http://www.test.com/1/video.mp4")) + .setMimeType(MimeTypes.VIDEO_UNKNOWN) + .build(); DownloadRequest expectedRequest2 = - new DownloadRequest( - /* id= */ "customCacheKey", - Uri.parse("http://www.test.com/2/video.mp4"), - /* mimeType= */ MimeTypes.VIDEO_UNKNOWN, - /* streamKeys= */ ImmutableList.of(), - /* keySetId= */ null, - /* customCacheKey= */ "customCacheKey", - /* data= */ new byte[] {0, 1, 2, 3}); + new DownloadRequest.Builder( + /* id= */ "customCacheKey", Uri.parse("http://www.test.com/2/video.mp4")) + .setMimeType(MimeTypes.VIDEO_UNKNOWN) + .setCustomCacheKey("customCacheKey") + .setData(new byte[] {0, 1, 2, 3}) + .build(); ActionFileUpgradeUtil.upgradeAndDelete( tempFile, @@ -106,25 +101,22 @@ public class ActionFileUpgradeUtilTest { output.write(actionFileBytes); } DownloadRequest expectedRequest1 = - new DownloadRequest( - /* id= */ "http://www.test.com/1/manifest.mpd", - Uri.parse("http://www.test.com/1/manifest.mpd"), - MimeTypes.APPLICATION_MPD, - /* streamKeys= */ ImmutableList.of(), - /* keySetId= */ null, - /* customCacheKey= */ null, - /* data= */ null); + new DownloadRequest.Builder( + /* id= */ "http://www.test.com/1/manifest.mpd", + Uri.parse("http://www.test.com/1/manifest.mpd")) + .setMimeType(MimeTypes.APPLICATION_MPD) + .build(); DownloadRequest expectedRequest2 = - new DownloadRequest( - /* id= */ "http://www.test.com/2/manifest.mpd", - Uri.parse("http://www.test.com/2/manifest.mpd"), - MimeTypes.APPLICATION_MPD, - ImmutableList.of( - new StreamKey(/* groupIndex= */ 0, /* trackIndex= */ 0), - new StreamKey(/* groupIndex= */ 1, /* trackIndex= */ 1)), - /* keySetId= */ null, - /* customCacheKey= */ null, - /* data= */ new byte[] {0, 1, 2, 3}); + new DownloadRequest.Builder( + /* id= */ "http://www.test.com/2/manifest.mpd", + Uri.parse("http://www.test.com/2/manifest.mpd")) + .setMimeType(MimeTypes.APPLICATION_MPD) + .setStreamKeys( + ImmutableList.of( + new StreamKey(/* groupIndex= */ 0, /* trackIndex= */ 0), + new StreamKey(/* groupIndex= */ 1, /* trackIndex= */ 1))) + .setData(new byte[] {0, 1, 2, 3}) + .build(); ActionFileUpgradeUtil.upgradeAndDelete( tempFile, @@ -148,25 +140,22 @@ public class ActionFileUpgradeUtilTest { output.write(actionFileBytes); } DownloadRequest expectedRequest1 = - new DownloadRequest( - /* id= */ "http://www.test.com/1/manifest.m3u8", - Uri.parse("http://www.test.com/1/manifest.m3u8"), - MimeTypes.APPLICATION_M3U8, - /* streamKeys= */ ImmutableList.of(), - /* keySetId= */ null, - /* customCacheKey= */ null, - /* data= */ null); + new DownloadRequest.Builder( + /* id= */ "http://www.test.com/1/manifest.m3u8", + Uri.parse("http://www.test.com/1/manifest.m3u8")) + .setMimeType(MimeTypes.APPLICATION_M3U8) + .build(); DownloadRequest expectedRequest2 = - new DownloadRequest( - /* id= */ "http://www.test.com/2/manifest.m3u8", - Uri.parse("http://www.test.com/2/manifest.m3u8"), - MimeTypes.APPLICATION_M3U8, - ImmutableList.of( - new StreamKey(/* groupIndex= */ 0, /* trackIndex= */ 0), - new StreamKey(/* groupIndex= */ 1, /* trackIndex= */ 1)), - /* keySetId= */ null, - /* customCacheKey= */ null, - /* data= */ new byte[] {0, 1, 2, 3}); + new DownloadRequest.Builder( + /* id= */ "http://www.test.com/2/manifest.m3u8", + Uri.parse("http://www.test.com/2/manifest.m3u8")) + .setMimeType(MimeTypes.APPLICATION_M3U8) + .setStreamKeys( + ImmutableList.of( + new StreamKey(/* groupIndex= */ 0, /* trackIndex= */ 0), + new StreamKey(/* groupIndex= */ 1, /* trackIndex= */ 1))) + .setData(new byte[] {0, 1, 2, 3}) + .build(); ActionFileUpgradeUtil.upgradeAndDelete( tempFile, @@ -190,25 +179,22 @@ public class ActionFileUpgradeUtilTest { output.write(actionFileBytes); } DownloadRequest expectedRequest1 = - new DownloadRequest( - /* id= */ "http://www.test.com/1/video.ism/manifest", - Uri.parse("http://www.test.com/1/video.ism/manifest"), - MimeTypes.APPLICATION_SS, - /* streamKeys= */ ImmutableList.of(), - /* keySetId= */ null, - /* customCacheKey= */ null, - /* data= */ null); + new DownloadRequest.Builder( + /* id= */ "http://www.test.com/1/video.ism/manifest", + Uri.parse("http://www.test.com/1/video.ism/manifest")) + .setMimeType(MimeTypes.APPLICATION_SS) + .build(); DownloadRequest expectedRequest2 = - new DownloadRequest( - /* id= */ "http://www.test.com/2/video.ism/manifest", - Uri.parse("http://www.test.com/2/video.ism/manifest"), - MimeTypes.APPLICATION_SS, - ImmutableList.of( - new StreamKey(/* groupIndex= */ 0, /* trackIndex= */ 0), - new StreamKey(/* groupIndex= */ 1, /* trackIndex= */ 1)), - /* keySetId= */ null, - /* customCacheKey= */ null, - /* data= */ new byte[] {0, 1, 2, 3}); + new DownloadRequest.Builder( + /* id= */ "http://www.test.com/2/video.ism/manifest", + Uri.parse("http://www.test.com/2/video.ism/manifest")) + .setMimeType(MimeTypes.APPLICATION_SS) + .setStreamKeys( + ImmutableList.of( + new StreamKey(/* groupIndex= */ 0, /* trackIndex= */ 0), + new StreamKey(/* groupIndex= */ 1, /* trackIndex= */ 1))) + .setData(new byte[] {0, 1, 2, 3}) + .build(); ActionFileUpgradeUtil.upgradeAndDelete( tempFile, @@ -225,16 +211,15 @@ public class ActionFileUpgradeUtilTest { @Test public void mergeRequest_nonExistingDownload_createsNewDownload() throws IOException { DownloadRequest request = - new DownloadRequest( - /* id= */ "id", - Uri.parse("https://www.test.com/download"), - /* mimeType= */ null, - ImmutableList.of( - new StreamKey(/* periodIndex= */ 0, /* groupIndex= */ 1, /* trackIndex= */ 2), - new StreamKey(/* periodIndex= */ 3, /* groupIndex= */ 4, /* trackIndex= */ 5)), - /* keySetId= */ new byte[] {1, 2, 3, 4}, - /* customCacheKey= */ "key123", - /* data= */ new byte[] {1, 2, 3, 4}); + new DownloadRequest.Builder(/* id= */ "id", Uri.parse("https://www.test.com/download")) + .setStreamKeys( + ImmutableList.of( + new StreamKey(/* periodIndex= */ 0, /* groupIndex= */ 1, /* trackIndex= */ 2), + new StreamKey(/* periodIndex= */ 3, /* groupIndex= */ 4, /* trackIndex= */ 5))) + .setKeySetId(new byte[] {1, 2, 3, 4}) + .setCustomCacheKey("key123") + .setData(new byte[] {1, 2, 3, 4}) + .build(); ActionFileUpgradeUtil.mergeRequest( request, downloadIndex, /* addNewDownloadAsCompleted= */ false, NOW_MS); @@ -249,23 +234,20 @@ public class ActionFileUpgradeUtilTest { StreamKey streamKey2 = new StreamKey(/* periodIndex= */ 0, /* groupIndex= */ 1, /* trackIndex= */ 2); DownloadRequest request1 = - new DownloadRequest( - /* id= */ "id", - Uri.parse("https://www.test.com/download1"), - /* mimeType= */ null, - ImmutableList.of(streamKey1), - /* keySetId= */ new byte[] {1, 2, 3, 4}, - /* customCacheKey= */ "key123", - /* data= */ new byte[] {1, 2, 3, 4}); + new DownloadRequest.Builder(/* id= */ "id", Uri.parse("https://www.test.com/download1")) + .setStreamKeys(ImmutableList.of(streamKey1)) + .setKeySetId(new byte[] {1, 2, 3, 4}) + .setCustomCacheKey("key123") + .setData(new byte[] {1, 2, 3, 4}) + .build(); DownloadRequest request2 = - new DownloadRequest( - /* id= */ "id", - Uri.parse("https://www.test.com/download2"), - /* mimeType= */ MimeTypes.APPLICATION_MP4, - ImmutableList.of(streamKey2), - /* keySetId= */ new byte[] {5, 4, 3, 2, 1}, - /* customCacheKey= */ "key345", - /* data= */ new byte[] {5, 4, 3, 2, 1}); + new DownloadRequest.Builder(/* id= */ "id", Uri.parse("https://www.test.com/download2")) + .setMimeType(MimeTypes.APPLICATION_MP4) + .setStreamKeys(ImmutableList.of(streamKey2)) + .setKeySetId(new byte[] {5, 4, 3, 2, 1}) + .setCustomCacheKey("key345") + .setData(new byte[] {5, 4, 3, 2, 1}) + .build(); ActionFileUpgradeUtil.mergeRequest( request1, downloadIndex, /* addNewDownloadAsCompleted= */ false, NOW_MS); @@ -290,23 +272,19 @@ public class ActionFileUpgradeUtilTest { StreamKey streamKey2 = new StreamKey(/* periodIndex= */ 0, /* groupIndex= */ 1, /* trackIndex= */ 2); DownloadRequest request1 = - new DownloadRequest( - /* id= */ "id1", - Uri.parse("https://www.test.com/download1"), - /* mimeType= */ null, - ImmutableList.of(streamKey1), - /* keySetId= */ new byte[] {1, 2, 3, 4}, - /* customCacheKey= */ "key123", - /* data= */ new byte[] {1, 2, 3, 4}); + new DownloadRequest.Builder(/* id= */ "id1", Uri.parse("https://www.test.com/download1")) + .setStreamKeys(ImmutableList.of(streamKey1)) + .setKeySetId(new byte[] {1, 2, 3, 4}) + .setCustomCacheKey("key123") + .setData(new byte[] {1, 2, 3, 4}) + .build(); DownloadRequest request2 = - new DownloadRequest( - /* id= */ "id2", - Uri.parse("https://www.test.com/download2"), - /* mimeType= */ null, - ImmutableList.of(streamKey2), - /* keySetId= */ new byte[] {5, 4, 3, 2, 1}, - /* customCacheKey= */ "key123", - /* data= */ new byte[] {5, 4, 3, 2, 1}); + new DownloadRequest.Builder(/* id= */ "id2", Uri.parse("https://www.test.com/download2")) + .setStreamKeys(ImmutableList.of(streamKey2)) + .setKeySetId(new byte[] {5, 4, 3, 2, 1}) + .setCustomCacheKey("key456") + .setData(new byte[] {5, 4, 3, 2, 1}) + .build(); ActionFileUpgradeUtil.mergeRequest( request1, downloadIndex, /* addNewDownloadAsCompleted= */ false, NOW_MS); diff --git a/library/core/src/test/java/com/google/android/exoplayer2/offline/DefaultDownloadIndexTest.java b/library/core/src/test/java/com/google/android/exoplayer2/offline/DefaultDownloadIndexTest.java index 313ee86413..c8b2f24056 100644 --- a/library/core/src/test/java/com/google/android/exoplayer2/offline/DefaultDownloadIndexTest.java +++ b/library/core/src/test/java/com/google/android/exoplayer2/offline/DefaultDownloadIndexTest.java @@ -385,15 +385,15 @@ public class DefaultDownloadIndexTest { private static Download createDownload( String uri, String mimeType, List streamKeys, @Nullable String customCacheKey) { + DownloadRequest downloadRequest = + new DownloadRequest.Builder(uri, Uri.parse(uri)) + .setMimeType(mimeType) + .setStreamKeys(streamKeys) + .setCustomCacheKey(customCacheKey) + .setData(new byte[] {0, 1, 2, 3}) + .build(); return new Download( - new DownloadRequest( - uri, - Uri.parse(uri), - mimeType, - streamKeys, - /* keySetId= */ null, - customCacheKey, - /* data= */ new byte[] {0, 1, 2, 3}), + downloadRequest, /* state= */ STATE_STOPPED, /* startTimeMs= */ 1, /* updateTimeMs= */ 2, diff --git a/library/core/src/test/java/com/google/android/exoplayer2/offline/DefaultDownloaderFactoryTest.java b/library/core/src/test/java/com/google/android/exoplayer2/offline/DefaultDownloaderFactoryTest.java index d3789c530e..9cf52ce568 100644 --- a/library/core/src/test/java/com/google/android/exoplayer2/offline/DefaultDownloaderFactoryTest.java +++ b/library/core/src/test/java/com/google/android/exoplayer2/offline/DefaultDownloaderFactoryTest.java @@ -22,7 +22,6 @@ import androidx.test.ext.junit.runners.AndroidJUnit4; import com.google.android.exoplayer2.upstream.DummyDataSource; import com.google.android.exoplayer2.upstream.cache.Cache; import com.google.android.exoplayer2.upstream.cache.CacheDataSource; -import java.util.Collections; import org.junit.Test; import org.junit.runner.RunWith; import org.mockito.Mockito; @@ -42,14 +41,8 @@ public final class DefaultDownloaderFactoryTest { Downloader downloader = factory.createDownloader( - new DownloadRequest( - /* id= */ "id", - Uri.parse("https://www.test.com/download"), - /* mimeType= */ null, - /* streamKeys= */ Collections.emptyList(), - /* keySetId= */ null, - /* customCacheKey= */ null, - /* data= */ null)); + new DownloadRequest.Builder(/* id= */ "id", Uri.parse("https://www.test.com/download")) + .build()); assertThat(downloader).isInstanceOf(ProgressiveDownloader.class); } } diff --git a/library/core/src/test/java/com/google/android/exoplayer2/offline/DownloadManagerTest.java b/library/core/src/test/java/com/google/android/exoplayer2/offline/DownloadManagerTest.java index 329b80dfea..d5959584ad 100644 --- a/library/core/src/test/java/com/google/android/exoplayer2/offline/DownloadManagerTest.java +++ b/library/core/src/test/java/com/google/android/exoplayer2/offline/DownloadManagerTest.java @@ -16,6 +16,7 @@ package com.google.android.exoplayer2.offline; import static com.google.common.truth.Truth.assertThat; +import static java.util.Arrays.asList; import android.net.Uri; import androidx.annotation.GuardedBy; @@ -33,7 +34,6 @@ import com.google.android.exoplayer2.util.Assertions; import com.google.android.exoplayer2.util.ConditionVariable; import java.io.IOException; import java.util.ArrayList; -import java.util.Arrays; import java.util.List; import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.atomic.AtomicReference; @@ -785,14 +785,9 @@ public class DownloadManagerTest { } private static DownloadRequest createDownloadRequest(String id, StreamKey... keys) { - return new DownloadRequest( - id, - Uri.parse("http://abc.com/ " + id), - /* mimeType= */ null, - Arrays.asList(keys), - /* keySetId= */ null, - /* customCacheKey= */ null, - /* data= */ null); + return new DownloadRequest.Builder(id, Uri.parse("http://abc.com/ " + id)) + .setStreamKeys(asList(keys)) + .build(); } // Internal methods. diff --git a/library/core/src/test/java/com/google/android/exoplayer2/offline/DownloadRequestTest.java b/library/core/src/test/java/com/google/android/exoplayer2/offline/DownloadRequestTest.java index 0256215809..c4a101e946 100644 --- a/library/core/src/test/java/com/google/android/exoplayer2/offline/DownloadRequestTest.java +++ b/library/core/src/test/java/com/google/android/exoplayer2/offline/DownloadRequestTest.java @@ -16,14 +16,13 @@ package com.google.android.exoplayer2.offline; import static com.google.common.truth.Truth.assertThat; +import static java.util.Arrays.asList; import static org.junit.Assert.fail; import android.net.Uri; import android.os.Parcel; import androidx.test.ext.junit.runners.AndroidJUnit4; import java.util.ArrayList; -import java.util.Collections; -import java.util.List; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; @@ -43,24 +42,10 @@ public class DownloadRequestTest { @Test public void mergeRequests_withDifferentIds_fails() { - DownloadRequest request1 = - new DownloadRequest( - /* id= */ "id1", - uri1, - /* mimeType= */ null, - /* streamKeys= */ Collections.emptyList(), - /* keySetId= */ null, - /* customCacheKey= */ null, - /* data= */ null); - DownloadRequest request2 = - new DownloadRequest( - /* id= */ "id2", - uri2, - /* mimeType= */ null, - /* streamKeys= */ Collections.emptyList(), - /* keySetId= */ null, - /* customCacheKey= */ null, - /* data= */ null); + + DownloadRequest request1 = new DownloadRequest.Builder(/* id= */ "id1", uri1).build(); + DownloadRequest request2 = new DownloadRequest.Builder(/* id= */ "id2", uri2).build(); + try { request1.copyWithMergedRequest(request2); fail(); @@ -114,23 +99,17 @@ public class DownloadRequestTest { byte[] data2 = new byte[] {9, 10, 11}; DownloadRequest request1 = - new DownloadRequest( - /* id= */ "id1", - uri1, - /* mimeType= */ null, - /* streamKeys= */ Collections.emptyList(), - keySetId1, - /* customCacheKey= */ "key1", - data1); + new DownloadRequest.Builder(/* id= */ "id1", uri1) + .setKeySetId(keySetId1) + .setCustomCacheKey("key1") + .setData(data1) + .build(); DownloadRequest request2 = - new DownloadRequest( - /* id= */ "id1", - uri2, - /* mimeType= */ null, - /* streamKeys= */ Collections.emptyList(), - keySetId2, - /* customCacheKey= */ "key2", - data2); + new DownloadRequest.Builder(/* id= */ "id1", uri2) + .setKeySetId(keySetId2) + .setCustomCacheKey("key2") + .setData(data2) + .build(); // uri, keySetId, customCacheKey and data should be from the request being merged. DownloadRequest mergedRequest = request1.copyWithMergedRequest(request2); @@ -152,14 +131,12 @@ public class DownloadRequestTest { streamKeys.add(new StreamKey(1, 2, 3)); streamKeys.add(new StreamKey(4, 5, 6)); DownloadRequest requestToParcel = - new DownloadRequest( - /* id= */ "id", - Uri.parse("https://abc.def/ghi"), - /* mimeType= */ null, - streamKeys, - /* keySetId= */ new byte[] {1, 2, 3, 4, 5}, - /* customCacheKey= */ "key", - /* data= */ new byte[] {1, 2, 3, 4, 5}); + new DownloadRequest.Builder("id", Uri.parse("https://abc.def/ghi")) + .setStreamKeys(streamKeys) + .setKeySetId(new byte[] {1, 2, 3, 4, 5}) + .setCustomCacheKey("key") + .setData(new byte[] {1, 2, 3, 4, 5}) + .build(); Parcel parcel = Parcel.obtain(); requestToParcel.writeToParcel(parcel, 0); parcel.setDataPosition(0); @@ -217,19 +194,6 @@ public class DownloadRequestTest { } private static DownloadRequest createRequest(Uri uri, StreamKey... keys) { - return new DownloadRequest( - uri.toString(), - uri, - /* mimeType= */ null, - toList(keys), - /* keySetId= */ null, - /* customCacheKey= */ null, - /* data= */ null); - } - - private static List toList(StreamKey... keys) { - ArrayList keysList = new ArrayList<>(); - Collections.addAll(keysList, keys); - return keysList; + return new DownloadRequest.Builder(uri.toString(), uri).setStreamKeys(asList(keys)).build(); } } diff --git a/library/dash/src/test/java/com/google/android/exoplayer2/source/dash/offline/DashDownloaderTest.java b/library/dash/src/test/java/com/google/android/exoplayer2/source/dash/offline/DashDownloaderTest.java index b5a2bf3057..d835b85725 100644 --- a/library/dash/src/test/java/com/google/android/exoplayer2/source/dash/offline/DashDownloaderTest.java +++ b/library/dash/src/test/java/com/google/android/exoplayer2/source/dash/offline/DashDownloaderTest.java @@ -92,14 +92,12 @@ public class DashDownloaderTest { Downloader downloader = factory.createDownloader( - new DownloadRequest( - "id", - Uri.parse("https://www.test.com/download"), - MimeTypes.APPLICATION_MPD, - Collections.singletonList(new StreamKey(/* groupIndex= */ 0, /* trackIndex= */ 0)), - /* keySetId= */ null, - /* customCacheKey= */ null, - /* data= */ null)); + new DownloadRequest.Builder(/* id= */ "id", Uri.parse("https://www.test.com/download")) + .setMimeType(MimeTypes.APPLICATION_MPD) + .setStreamKeys( + Collections.singletonList( + new StreamKey(/* groupIndex= */ 0, /* trackIndex= */ 0))) + .build()); assertThat(downloader).isInstanceOf(DashDownloader.class); } diff --git a/library/dash/src/test/java/com/google/android/exoplayer2/source/dash/offline/DownloadManagerDashTest.java b/library/dash/src/test/java/com/google/android/exoplayer2/source/dash/offline/DownloadManagerDashTest.java index 14319c5755..2993bb4442 100644 --- a/library/dash/src/test/java/com/google/android/exoplayer2/source/dash/offline/DownloadManagerDashTest.java +++ b/library/dash/src/test/java/com/google/android/exoplayer2/source/dash/offline/DownloadManagerDashTest.java @@ -220,14 +220,10 @@ public class DownloadManagerDashTest { private DownloadRequest getDownloadRequest(StreamKey... keys) { ArrayList keysList = new ArrayList<>(); Collections.addAll(keysList, keys); - return new DownloadRequest( - TEST_ID, - TEST_MPD_URI, - MimeTypes.APPLICATION_MPD, - keysList, - /* keySetId= */ null, - /* customCacheKey= */ null, - null); + return new DownloadRequest.Builder(TEST_ID, TEST_MPD_URI) + .setMimeType(MimeTypes.APPLICATION_MPD) + .setStreamKeys(keysList) + .build(); } private void handleRemoveAction() { diff --git a/library/dash/src/test/java/com/google/android/exoplayer2/source/dash/offline/DownloadServiceDashTest.java b/library/dash/src/test/java/com/google/android/exoplayer2/source/dash/offline/DownloadServiceDashTest.java index 3d0db421cf..6b528cdd82 100644 --- a/library/dash/src/test/java/com/google/android/exoplayer2/source/dash/offline/DownloadServiceDashTest.java +++ b/library/dash/src/test/java/com/google/android/exoplayer2/source/dash/offline/DownloadServiceDashTest.java @@ -206,14 +206,11 @@ public class DownloadServiceDashTest { ArrayList keysList = new ArrayList<>(); Collections.addAll(keysList, keys); DownloadRequest action = - new DownloadRequest( - TEST_ID, - TEST_MPD_URI, - MimeTypes.APPLICATION_MPD, - keysList, - /* keySetId= */ null, - /* customCacheKey= */ null, - null); + new DownloadRequest.Builder(TEST_ID, TEST_MPD_URI) + .setMimeType(MimeTypes.APPLICATION_MPD) + .setStreamKeys(keysList) + .build(); + testThread.runOnMainThread( () -> { Intent startIntent = diff --git a/library/hls/src/test/java/com/google/android/exoplayer2/source/hls/offline/HlsDownloaderTest.java b/library/hls/src/test/java/com/google/android/exoplayer2/source/hls/offline/HlsDownloaderTest.java index d968af51bc..986ee4deda 100644 --- a/library/hls/src/test/java/com/google/android/exoplayer2/source/hls/offline/HlsDownloaderTest.java +++ b/library/hls/src/test/java/com/google/android/exoplayer2/source/hls/offline/HlsDownloaderTest.java @@ -110,14 +110,12 @@ public class HlsDownloaderTest { Downloader downloader = factory.createDownloader( - new DownloadRequest( - "id", - Uri.parse("https://www.test.com/download"), - MimeTypes.APPLICATION_M3U8, - Collections.singletonList(new StreamKey(/* groupIndex= */ 0, /* trackIndex= */ 0)), - /* keySetId= */ null, - /* customCacheKey= */ null, - /* data= */ null)); + new DownloadRequest.Builder(/* id= */ "id", Uri.parse("https://www.test.com/download")) + .setMimeType(MimeTypes.APPLICATION_M3U8) + .setStreamKeys( + Collections.singletonList( + new StreamKey(/* groupIndex= */ 0, /* trackIndex= */ 0))) + .build()); assertThat(downloader).isInstanceOf(HlsDownloader.class); } diff --git a/library/smoothstreaming/src/test/java/com/google/android/exoplayer2/source/smoothstreaming/offline/SsDownloaderTest.java b/library/smoothstreaming/src/test/java/com/google/android/exoplayer2/source/smoothstreaming/offline/SsDownloaderTest.java index 6d1bb28009..38132b55ed 100644 --- a/library/smoothstreaming/src/test/java/com/google/android/exoplayer2/source/smoothstreaming/offline/SsDownloaderTest.java +++ b/library/smoothstreaming/src/test/java/com/google/android/exoplayer2/source/smoothstreaming/offline/SsDownloaderTest.java @@ -48,14 +48,12 @@ public final class SsDownloaderTest { Downloader downloader = factory.createDownloader( - new DownloadRequest( - "id", - Uri.parse("https://www.test.com/download"), - MimeTypes.APPLICATION_SS, - Collections.singletonList(new StreamKey(/* groupIndex= */ 0, /* trackIndex= */ 0)), - /* keySetId= */ null, - /* customCacheKey= */ null, - /* data= */ null)); + new DownloadRequest.Builder(/* id= */ "id", Uri.parse("https://www.test.com/download")) + .setMimeType(MimeTypes.APPLICATION_SS) + .setStreamKeys( + Collections.singletonList( + new StreamKey(/* groupIndex= */ 0, /* trackIndex= */ 0))) + .build()); assertThat(downloader).isInstanceOf(SsDownloader.class); } } 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 c37d0df13c..184b7a11e4 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 @@ -198,7 +198,13 @@ public final class DownloadBuilder { public Download build() { DownloadRequest request = - new DownloadRequest(id, uri, mimeType, streamKeys, keySetId, cacheKey, customMetadata); + new DownloadRequest.Builder(id, uri) + .setMimeType(mimeType) + .setStreamKeys(streamKeys) + .setKeySetId(keySetId) + .setCustomCacheKey(cacheKey) + .setData(customMetadata) + .build(); return new Download( request, state,