Add a builder for DownloadRequest

PiperOrigin-RevId: 324616617
This commit is contained in:
christosts 2020-08-03 17:16:49 +01:00 committed by kim-vde
parent 129ef7ccd4
commit f4287ed070
17 changed files with 259 additions and 283 deletions

View File

@ -168,6 +168,7 @@
[#6725](https://github.com/google/ExoPlayer/issues/6725), [#6725](https://github.com/google/ExoPlayer/issues/6725),
[#7066](https://github.com/google/ExoPlayer/issues/7066)). [#7066](https://github.com/google/ExoPlayer/issues/7066)).
* Downloads and caching: * Downloads and caching:
* Add builder in `DownloadRequest`.
* Support passing an `Executor` to `DefaultDownloaderFactory` on which * Support passing an `Executor` to `DefaultDownloaderFactory` on which
data downloads are performed. data downloads are performed.
* Parallelize and merge downloads in `SegmentDownloader` to improve * Parallelize and merge downloads in `SegmentDownloader` to improve

View File

@ -140,10 +140,13 @@ import java.util.List;
// Remove actions are not supported anymore. // Remove actions are not supported anymore.
throw new UnsupportedRequestException(); 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.Builder(id, uri)
return new DownloadRequest( .setMimeType(inferMimeType(downloadType))
id, uri, inferMimeType(downloadType), keys, /* keySetId= */ null, customCacheKey, data); .setStreamKeys(keys)
.setCustomCacheKey(customCacheKey)
.setData(data)
.build();
} }
private static StreamKey readKey(String type, int version, DataInputStream input) private static StreamKey readKey(String type, int version, DataInputStream input)

View File

@ -434,14 +434,15 @@ public final class DefaultDownloadIndex implements WritableDownloadIndex {
private static Download getDownloadForCurrentRow(Cursor cursor) { private static Download getDownloadForCurrentRow(Cursor cursor) {
byte[] keySetId = cursor.getBlob(COLUMN_INDEX_KEY_SET_ID); byte[] keySetId = cursor.getBlob(COLUMN_INDEX_KEY_SET_ID);
DownloadRequest request = DownloadRequest request =
new DownloadRequest( new DownloadRequest.Builder(
/* id= */ cursor.getString(COLUMN_INDEX_ID), /* id= */ cursor.getString(COLUMN_INDEX_ID),
/* uri= */ Uri.parse(cursor.getString(COLUMN_INDEX_URI)), /* uri= */ Uri.parse(cursor.getString(COLUMN_INDEX_URI)))
/* mimeType= */ cursor.getString(COLUMN_INDEX_MIME_TYPE), .setMimeType(cursor.getString(COLUMN_INDEX_MIME_TYPE))
/* streamKeys= */ decodeStreamKeys(cursor.getString(COLUMN_INDEX_STREAM_KEYS)), .setStreamKeys(decodeStreamKeys(cursor.getString(COLUMN_INDEX_STREAM_KEYS)))
/* keySetId= */ keySetId.length > 0 ? keySetId : null, .setKeySetId(keySetId.length > 0 ? keySetId : null)
/* customCacheKey= */ cursor.getString(COLUMN_INDEX_CUSTOM_CACHE_KEY), .setCustomCacheKey(cursor.getString(COLUMN_INDEX_CUSTOM_CACHE_KEY))
/* data= */ cursor.getBlob(COLUMN_INDEX_DATA)); .setData(cursor.getBlob(COLUMN_INDEX_DATA))
.build();
DownloadProgress downloadProgress = new DownloadProgress(); DownloadProgress downloadProgress = new DownloadProgress();
downloadProgress.bytesDownloaded = cursor.getLong(COLUMN_INDEX_BYTES_DOWNLOADED); downloadProgress.bytesDownloaded = cursor.getLong(COLUMN_INDEX_BYTES_DOWNLOADED);
downloadProgress.percentDownloaded = cursor.getFloat(COLUMN_INDEX_PERCENT_DOWNLOADED); downloadProgress.percentDownloaded = cursor.getFloat(COLUMN_INDEX_PERCENT_DOWNLOADED);
@ -485,14 +486,13 @@ public final class DefaultDownloadIndex implements WritableDownloadIndex {
* 13 bytes_downloaded integer * 13 bytes_downloaded integer
*/ */
DownloadRequest request = DownloadRequest request =
new DownloadRequest( new DownloadRequest.Builder(
/* id= */ cursor.getString(0), /* id= */ cursor.getString(0), /* uri= */ Uri.parse(cursor.getString(2)))
/* uri= */ Uri.parse(cursor.getString(2)), .setMimeType(inferMimeType(cursor.getString(1)))
/* mimeType= */ inferMimeType(cursor.getString(1)), .setStreamKeys(decodeStreamKeys(cursor.getString(3)))
/* streamKeys= */ decodeStreamKeys(cursor.getString(3)), .setCustomCacheKey(cursor.getString(4))
/* keySetId= */ null, .setData(cursor.getBlob(5))
/* customCacheKey= */ cursor.getString(4), .build();
/* data= */ cursor.getBlob(5));
DownloadProgress downloadProgress = new DownloadProgress(); DownloadProgress downloadProgress = new DownloadProgress();
downloadProgress.bytesDownloaded = cursor.getLong(13); downloadProgress.bytesDownloaded = cursor.getLong(13);
downloadProgress.percentDownloaded = cursor.getFloat(12); downloadProgress.percentDownloaded = cursor.getFloat(12);

View File

@ -745,14 +745,11 @@ public final class DownloadHelper {
public DownloadRequest getDownloadRequest(String id, @Nullable byte[] data) { public DownloadRequest getDownloadRequest(String id, @Nullable byte[] data) {
if (mediaSource == null) { if (mediaSource == null) {
// TODO: add support for DRM (keySetId) [Internal ref: b/158980798] // TODO: add support for DRM (keySetId) [Internal ref: b/158980798]
return new DownloadRequest( return new DownloadRequest.Builder(id, playbackProperties.uri)
id, .setMimeType(playbackProperties.mimeType)
playbackProperties.uri, .setCustomCacheKey(playbackProperties.customCacheKey)
playbackProperties.mimeType, .setData(data)
/* streamKeys= */ Collections.emptyList(), .build();
/* keySetId= */ null,
playbackProperties.customCacheKey,
data);
} }
assertPreparedWithMedia(); assertPreparedWithMedia();
List<StreamKey> streamKeys = new ArrayList<>(); List<StreamKey> streamKeys = new ArrayList<>();
@ -767,14 +764,12 @@ public final class DownloadHelper {
streamKeys.addAll(mediaPreparer.mediaPeriods[periodIndex].getStreamKeys(allSelections)); streamKeys.addAll(mediaPreparer.mediaPeriods[periodIndex].getStreamKeys(allSelections));
} }
// TODO: add support for DRM (keySetId) [Internal ref: b/158980798] // TODO: add support for DRM (keySetId) [Internal ref: b/158980798]
return new DownloadRequest( return new DownloadRequest.Builder(id, playbackProperties.uri)
id, .setMimeType(playbackProperties.mimeType)
playbackProperties.uri, .setStreamKeys(streamKeys)
playbackProperties.mimeType, .setCustomCacheKey(playbackProperties.customCacheKey)
streamKeys, .setData(data)
/* keySetId= */ null, .build();
playbackProperties.customCacheKey,
data);
} }
// Initialization of array of Lists. // Initialization of array of Lists.

View File

@ -24,6 +24,7 @@ import androidx.annotation.Nullable;
import com.google.android.exoplayer2.C; import com.google.android.exoplayer2.C;
import com.google.android.exoplayer2.util.Assertions; import com.google.android.exoplayer2.util.Assertions;
import com.google.android.exoplayer2.util.Util; import com.google.android.exoplayer2.util.Util;
import com.google.common.collect.ImmutableList;
import java.io.IOException; import java.io.IOException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; 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. */ /** Thrown when the encoded request data belongs to an unsupported request type. */
public static class UnsupportedRequestException extends IOException {} 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<StreamKey> 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<StreamKey> 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. */ /** The unique content id. */
public final String id; public final String id;
/** The uri being downloaded. */ /** The uri being downloaded. */
@ -66,7 +125,7 @@ public final class DownloadRequest implements Parcelable {
* @param customCacheKey See {@link #customCacheKey}. * @param customCacheKey See {@link #customCacheKey}.
* @param data See {@link #data}. * @param data See {@link #data}.
*/ */
public DownloadRequest( private DownloadRequest(
String id, String id,
Uri uri, Uri uri,
@Nullable String mimeType, @Nullable String mimeType,

View File

@ -26,7 +26,6 @@ import com.google.android.exoplayer2.util.Util;
import java.io.File; import java.io.File;
import java.io.FileOutputStream; import java.io.FileOutputStream;
import java.io.IOException; import java.io.IOException;
import java.util.Collections;
import org.junit.After; import org.junit.After;
import org.junit.Assert; import org.junit.Assert;
import org.junit.Before; import org.junit.Before;
@ -126,13 +125,9 @@ public class ActionFileTest {
} }
private static DownloadRequest buildExpectedRequest(Uri uri, byte[] data) { private static DownloadRequest buildExpectedRequest(Uri uri, byte[] data) {
return new DownloadRequest( return new DownloadRequest.Builder(/* id= */ uri.toString(), uri)
/* id= */ uri.toString(), .setMimeType(MimeTypes.VIDEO_UNKNOWN)
uri, .setData(data)
/* mimeType= */ MimeTypes.VIDEO_UNKNOWN, .build();
/* streamKeys= */ Collections.emptyList(),
/* keySetId= */ null,
/* customCacheKey= */ null,
data);
} }
} }

View File

@ -66,23 +66,18 @@ public class ActionFileUpgradeUtilTest {
output.write(actionFileBytes); output.write(actionFileBytes);
} }
DownloadRequest expectedRequest1 = DownloadRequest expectedRequest1 =
new DownloadRequest( new DownloadRequest.Builder(
/* id= */ "http://www.test.com/1/video.mp4", /* id= */ "http://www.test.com/1/video.mp4",
Uri.parse("http://www.test.com/1/video.mp4"), Uri.parse("http://www.test.com/1/video.mp4"))
/* mimeType= */ MimeTypes.VIDEO_UNKNOWN, .setMimeType(MimeTypes.VIDEO_UNKNOWN)
/* streamKeys= */ ImmutableList.of(), .build();
/* keySetId= */ null,
/* customCacheKey= */ null,
/* data= */ null);
DownloadRequest expectedRequest2 = DownloadRequest expectedRequest2 =
new DownloadRequest( new DownloadRequest.Builder(
/* id= */ "customCacheKey", /* id= */ "customCacheKey", Uri.parse("http://www.test.com/2/video.mp4"))
Uri.parse("http://www.test.com/2/video.mp4"), .setMimeType(MimeTypes.VIDEO_UNKNOWN)
/* mimeType= */ MimeTypes.VIDEO_UNKNOWN, .setCustomCacheKey("customCacheKey")
/* streamKeys= */ ImmutableList.of(), .setData(new byte[] {0, 1, 2, 3})
/* keySetId= */ null, .build();
/* customCacheKey= */ "customCacheKey",
/* data= */ new byte[] {0, 1, 2, 3});
ActionFileUpgradeUtil.upgradeAndDelete( ActionFileUpgradeUtil.upgradeAndDelete(
tempFile, tempFile,
@ -106,25 +101,22 @@ public class ActionFileUpgradeUtilTest {
output.write(actionFileBytes); output.write(actionFileBytes);
} }
DownloadRequest expectedRequest1 = DownloadRequest expectedRequest1 =
new DownloadRequest( new DownloadRequest.Builder(
/* id= */ "http://www.test.com/1/manifest.mpd", /* id= */ "http://www.test.com/1/manifest.mpd",
Uri.parse("http://www.test.com/1/manifest.mpd"), Uri.parse("http://www.test.com/1/manifest.mpd"))
MimeTypes.APPLICATION_MPD, .setMimeType(MimeTypes.APPLICATION_MPD)
/* streamKeys= */ ImmutableList.of(), .build();
/* keySetId= */ null,
/* customCacheKey= */ null,
/* data= */ null);
DownloadRequest expectedRequest2 = DownloadRequest expectedRequest2 =
new DownloadRequest( new DownloadRequest.Builder(
/* id= */ "http://www.test.com/2/manifest.mpd", /* id= */ "http://www.test.com/2/manifest.mpd",
Uri.parse("http://www.test.com/2/manifest.mpd"), Uri.parse("http://www.test.com/2/manifest.mpd"))
MimeTypes.APPLICATION_MPD, .setMimeType(MimeTypes.APPLICATION_MPD)
ImmutableList.of( .setStreamKeys(
new StreamKey(/* groupIndex= */ 0, /* trackIndex= */ 0), ImmutableList.of(
new StreamKey(/* groupIndex= */ 1, /* trackIndex= */ 1)), new StreamKey(/* groupIndex= */ 0, /* trackIndex= */ 0),
/* keySetId= */ null, new StreamKey(/* groupIndex= */ 1, /* trackIndex= */ 1)))
/* customCacheKey= */ null, .setData(new byte[] {0, 1, 2, 3})
/* data= */ new byte[] {0, 1, 2, 3}); .build();
ActionFileUpgradeUtil.upgradeAndDelete( ActionFileUpgradeUtil.upgradeAndDelete(
tempFile, tempFile,
@ -148,25 +140,22 @@ public class ActionFileUpgradeUtilTest {
output.write(actionFileBytes); output.write(actionFileBytes);
} }
DownloadRequest expectedRequest1 = DownloadRequest expectedRequest1 =
new DownloadRequest( new DownloadRequest.Builder(
/* id= */ "http://www.test.com/1/manifest.m3u8", /* id= */ "http://www.test.com/1/manifest.m3u8",
Uri.parse("http://www.test.com/1/manifest.m3u8"), Uri.parse("http://www.test.com/1/manifest.m3u8"))
MimeTypes.APPLICATION_M3U8, .setMimeType(MimeTypes.APPLICATION_M3U8)
/* streamKeys= */ ImmutableList.of(), .build();
/* keySetId= */ null,
/* customCacheKey= */ null,
/* data= */ null);
DownloadRequest expectedRequest2 = DownloadRequest expectedRequest2 =
new DownloadRequest( new DownloadRequest.Builder(
/* id= */ "http://www.test.com/2/manifest.m3u8", /* id= */ "http://www.test.com/2/manifest.m3u8",
Uri.parse("http://www.test.com/2/manifest.m3u8"), Uri.parse("http://www.test.com/2/manifest.m3u8"))
MimeTypes.APPLICATION_M3U8, .setMimeType(MimeTypes.APPLICATION_M3U8)
ImmutableList.of( .setStreamKeys(
new StreamKey(/* groupIndex= */ 0, /* trackIndex= */ 0), ImmutableList.of(
new StreamKey(/* groupIndex= */ 1, /* trackIndex= */ 1)), new StreamKey(/* groupIndex= */ 0, /* trackIndex= */ 0),
/* keySetId= */ null, new StreamKey(/* groupIndex= */ 1, /* trackIndex= */ 1)))
/* customCacheKey= */ null, .setData(new byte[] {0, 1, 2, 3})
/* data= */ new byte[] {0, 1, 2, 3}); .build();
ActionFileUpgradeUtil.upgradeAndDelete( ActionFileUpgradeUtil.upgradeAndDelete(
tempFile, tempFile,
@ -190,25 +179,22 @@ public class ActionFileUpgradeUtilTest {
output.write(actionFileBytes); output.write(actionFileBytes);
} }
DownloadRequest expectedRequest1 = DownloadRequest expectedRequest1 =
new DownloadRequest( new DownloadRequest.Builder(
/* id= */ "http://www.test.com/1/video.ism/manifest", /* id= */ "http://www.test.com/1/video.ism/manifest",
Uri.parse("http://www.test.com/1/video.ism/manifest"), Uri.parse("http://www.test.com/1/video.ism/manifest"))
MimeTypes.APPLICATION_SS, .setMimeType(MimeTypes.APPLICATION_SS)
/* streamKeys= */ ImmutableList.of(), .build();
/* keySetId= */ null,
/* customCacheKey= */ null,
/* data= */ null);
DownloadRequest expectedRequest2 = DownloadRequest expectedRequest2 =
new DownloadRequest( new DownloadRequest.Builder(
/* id= */ "http://www.test.com/2/video.ism/manifest", /* id= */ "http://www.test.com/2/video.ism/manifest",
Uri.parse("http://www.test.com/2/video.ism/manifest"), Uri.parse("http://www.test.com/2/video.ism/manifest"))
MimeTypes.APPLICATION_SS, .setMimeType(MimeTypes.APPLICATION_SS)
ImmutableList.of( .setStreamKeys(
new StreamKey(/* groupIndex= */ 0, /* trackIndex= */ 0), ImmutableList.of(
new StreamKey(/* groupIndex= */ 1, /* trackIndex= */ 1)), new StreamKey(/* groupIndex= */ 0, /* trackIndex= */ 0),
/* keySetId= */ null, new StreamKey(/* groupIndex= */ 1, /* trackIndex= */ 1)))
/* customCacheKey= */ null, .setData(new byte[] {0, 1, 2, 3})
/* data= */ new byte[] {0, 1, 2, 3}); .build();
ActionFileUpgradeUtil.upgradeAndDelete( ActionFileUpgradeUtil.upgradeAndDelete(
tempFile, tempFile,
@ -225,16 +211,15 @@ public class ActionFileUpgradeUtilTest {
@Test @Test
public void mergeRequest_nonExistingDownload_createsNewDownload() throws IOException { public void mergeRequest_nonExistingDownload_createsNewDownload() throws IOException {
DownloadRequest request = DownloadRequest request =
new DownloadRequest( new DownloadRequest.Builder(/* id= */ "id", Uri.parse("https://www.test.com/download"))
/* id= */ "id", .setStreamKeys(
Uri.parse("https://www.test.com/download"), ImmutableList.of(
/* mimeType= */ null, new StreamKey(/* periodIndex= */ 0, /* groupIndex= */ 1, /* trackIndex= */ 2),
ImmutableList.of( new StreamKey(/* periodIndex= */ 3, /* groupIndex= */ 4, /* trackIndex= */ 5)))
new StreamKey(/* periodIndex= */ 0, /* groupIndex= */ 1, /* trackIndex= */ 2), .setKeySetId(new byte[] {1, 2, 3, 4})
new StreamKey(/* periodIndex= */ 3, /* groupIndex= */ 4, /* trackIndex= */ 5)), .setCustomCacheKey("key123")
/* keySetId= */ new byte[] {1, 2, 3, 4}, .setData(new byte[] {1, 2, 3, 4})
/* customCacheKey= */ "key123", .build();
/* data= */ new byte[] {1, 2, 3, 4});
ActionFileUpgradeUtil.mergeRequest( ActionFileUpgradeUtil.mergeRequest(
request, downloadIndex, /* addNewDownloadAsCompleted= */ false, NOW_MS); request, downloadIndex, /* addNewDownloadAsCompleted= */ false, NOW_MS);
@ -249,23 +234,20 @@ public class ActionFileUpgradeUtilTest {
StreamKey streamKey2 = StreamKey streamKey2 =
new StreamKey(/* periodIndex= */ 0, /* groupIndex= */ 1, /* trackIndex= */ 2); new StreamKey(/* periodIndex= */ 0, /* groupIndex= */ 1, /* trackIndex= */ 2);
DownloadRequest request1 = DownloadRequest request1 =
new DownloadRequest( new DownloadRequest.Builder(/* id= */ "id", Uri.parse("https://www.test.com/download1"))
/* id= */ "id", .setStreamKeys(ImmutableList.of(streamKey1))
Uri.parse("https://www.test.com/download1"), .setKeySetId(new byte[] {1, 2, 3, 4})
/* mimeType= */ null, .setCustomCacheKey("key123")
ImmutableList.of(streamKey1), .setData(new byte[] {1, 2, 3, 4})
/* keySetId= */ new byte[] {1, 2, 3, 4}, .build();
/* customCacheKey= */ "key123",
/* data= */ new byte[] {1, 2, 3, 4});
DownloadRequest request2 = DownloadRequest request2 =
new DownloadRequest( new DownloadRequest.Builder(/* id= */ "id", Uri.parse("https://www.test.com/download2"))
/* id= */ "id", .setMimeType(MimeTypes.APPLICATION_MP4)
Uri.parse("https://www.test.com/download2"), .setStreamKeys(ImmutableList.of(streamKey2))
/* mimeType= */ MimeTypes.APPLICATION_MP4, .setKeySetId(new byte[] {5, 4, 3, 2, 1})
ImmutableList.of(streamKey2), .setCustomCacheKey("key345")
/* keySetId= */ new byte[] {5, 4, 3, 2, 1}, .setData(new byte[] {5, 4, 3, 2, 1})
/* customCacheKey= */ "key345", .build();
/* data= */ new byte[] {5, 4, 3, 2, 1});
ActionFileUpgradeUtil.mergeRequest( ActionFileUpgradeUtil.mergeRequest(
request1, downloadIndex, /* addNewDownloadAsCompleted= */ false, NOW_MS); request1, downloadIndex, /* addNewDownloadAsCompleted= */ false, NOW_MS);
@ -290,23 +272,19 @@ public class ActionFileUpgradeUtilTest {
StreamKey streamKey2 = StreamKey streamKey2 =
new StreamKey(/* periodIndex= */ 0, /* groupIndex= */ 1, /* trackIndex= */ 2); new StreamKey(/* periodIndex= */ 0, /* groupIndex= */ 1, /* trackIndex= */ 2);
DownloadRequest request1 = DownloadRequest request1 =
new DownloadRequest( new DownloadRequest.Builder(/* id= */ "id1", Uri.parse("https://www.test.com/download1"))
/* id= */ "id1", .setStreamKeys(ImmutableList.of(streamKey1))
Uri.parse("https://www.test.com/download1"), .setKeySetId(new byte[] {1, 2, 3, 4})
/* mimeType= */ null, .setCustomCacheKey("key123")
ImmutableList.of(streamKey1), .setData(new byte[] {1, 2, 3, 4})
/* keySetId= */ new byte[] {1, 2, 3, 4}, .build();
/* customCacheKey= */ "key123",
/* data= */ new byte[] {1, 2, 3, 4});
DownloadRequest request2 = DownloadRequest request2 =
new DownloadRequest( new DownloadRequest.Builder(/* id= */ "id2", Uri.parse("https://www.test.com/download2"))
/* id= */ "id2", .setStreamKeys(ImmutableList.of(streamKey2))
Uri.parse("https://www.test.com/download2"), .setKeySetId(new byte[] {5, 4, 3, 2, 1})
/* mimeType= */ null, .setCustomCacheKey("key456")
ImmutableList.of(streamKey2), .setData(new byte[] {5, 4, 3, 2, 1})
/* keySetId= */ new byte[] {5, 4, 3, 2, 1}, .build();
/* customCacheKey= */ "key123",
/* data= */ new byte[] {5, 4, 3, 2, 1});
ActionFileUpgradeUtil.mergeRequest( ActionFileUpgradeUtil.mergeRequest(
request1, downloadIndex, /* addNewDownloadAsCompleted= */ false, NOW_MS); request1, downloadIndex, /* addNewDownloadAsCompleted= */ false, NOW_MS);

View File

@ -385,15 +385,15 @@ public class DefaultDownloadIndexTest {
private static Download createDownload( private static Download createDownload(
String uri, String mimeType, List<StreamKey> streamKeys, @Nullable String customCacheKey) { String uri, String mimeType, List<StreamKey> 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( return new Download(
new DownloadRequest( downloadRequest,
uri,
Uri.parse(uri),
mimeType,
streamKeys,
/* keySetId= */ null,
customCacheKey,
/* data= */ new byte[] {0, 1, 2, 3}),
/* state= */ STATE_STOPPED, /* state= */ STATE_STOPPED,
/* startTimeMs= */ 1, /* startTimeMs= */ 1,
/* updateTimeMs= */ 2, /* updateTimeMs= */ 2,

View File

@ -22,7 +22,6 @@ import androidx.test.ext.junit.runners.AndroidJUnit4;
import com.google.android.exoplayer2.upstream.DummyDataSource; import com.google.android.exoplayer2.upstream.DummyDataSource;
import com.google.android.exoplayer2.upstream.cache.Cache; import com.google.android.exoplayer2.upstream.cache.Cache;
import com.google.android.exoplayer2.upstream.cache.CacheDataSource; import com.google.android.exoplayer2.upstream.cache.CacheDataSource;
import java.util.Collections;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.mockito.Mockito; import org.mockito.Mockito;
@ -42,14 +41,8 @@ public final class DefaultDownloaderFactoryTest {
Downloader downloader = Downloader downloader =
factory.createDownloader( factory.createDownloader(
new DownloadRequest( new DownloadRequest.Builder(/* id= */ "id", Uri.parse("https://www.test.com/download"))
/* id= */ "id", .build());
Uri.parse("https://www.test.com/download"),
/* mimeType= */ null,
/* streamKeys= */ Collections.emptyList(),
/* keySetId= */ null,
/* customCacheKey= */ null,
/* data= */ null));
assertThat(downloader).isInstanceOf(ProgressiveDownloader.class); assertThat(downloader).isInstanceOf(ProgressiveDownloader.class);
} }
} }

View File

@ -16,6 +16,7 @@
package com.google.android.exoplayer2.offline; package com.google.android.exoplayer2.offline;
import static com.google.common.truth.Truth.assertThat; import static com.google.common.truth.Truth.assertThat;
import static java.util.Arrays.asList;
import android.net.Uri; import android.net.Uri;
import androidx.annotation.GuardedBy; import androidx.annotation.GuardedBy;
@ -33,7 +34,6 @@ import com.google.android.exoplayer2.util.Assertions;
import com.google.android.exoplayer2.util.ConditionVariable; import com.google.android.exoplayer2.util.ConditionVariable;
import java.io.IOException; import java.io.IOException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays;
import java.util.List; import java.util.List;
import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicReference; import java.util.concurrent.atomic.AtomicReference;
@ -785,14 +785,9 @@ public class DownloadManagerTest {
} }
private static DownloadRequest createDownloadRequest(String id, StreamKey... keys) { private static DownloadRequest createDownloadRequest(String id, StreamKey... keys) {
return new DownloadRequest( return new DownloadRequest.Builder(id, Uri.parse("http://abc.com/ " + id))
id, .setStreamKeys(asList(keys))
Uri.parse("http://abc.com/ " + id), .build();
/* mimeType= */ null,
Arrays.asList(keys),
/* keySetId= */ null,
/* customCacheKey= */ null,
/* data= */ null);
} }
// Internal methods. // Internal methods.

View File

@ -16,14 +16,13 @@
package com.google.android.exoplayer2.offline; package com.google.android.exoplayer2.offline;
import static com.google.common.truth.Truth.assertThat; import static com.google.common.truth.Truth.assertThat;
import static java.util.Arrays.asList;
import static org.junit.Assert.fail; import static org.junit.Assert.fail;
import android.net.Uri; import android.net.Uri;
import android.os.Parcel; import android.os.Parcel;
import androidx.test.ext.junit.runners.AndroidJUnit4; import androidx.test.ext.junit.runners.AndroidJUnit4;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
@ -43,24 +42,10 @@ public class DownloadRequestTest {
@Test @Test
public void mergeRequests_withDifferentIds_fails() { public void mergeRequests_withDifferentIds_fails() {
DownloadRequest request1 =
new DownloadRequest( DownloadRequest request1 = new DownloadRequest.Builder(/* id= */ "id1", uri1).build();
/* id= */ "id1", DownloadRequest request2 = new DownloadRequest.Builder(/* id= */ "id2", uri2).build();
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);
try { try {
request1.copyWithMergedRequest(request2); request1.copyWithMergedRequest(request2);
fail(); fail();
@ -114,23 +99,17 @@ public class DownloadRequestTest {
byte[] data2 = new byte[] {9, 10, 11}; byte[] data2 = new byte[] {9, 10, 11};
DownloadRequest request1 = DownloadRequest request1 =
new DownloadRequest( new DownloadRequest.Builder(/* id= */ "id1", uri1)
/* id= */ "id1", .setKeySetId(keySetId1)
uri1, .setCustomCacheKey("key1")
/* mimeType= */ null, .setData(data1)
/* streamKeys= */ Collections.emptyList(), .build();
keySetId1,
/* customCacheKey= */ "key1",
data1);
DownloadRequest request2 = DownloadRequest request2 =
new DownloadRequest( new DownloadRequest.Builder(/* id= */ "id1", uri2)
/* id= */ "id1", .setKeySetId(keySetId2)
uri2, .setCustomCacheKey("key2")
/* mimeType= */ null, .setData(data2)
/* streamKeys= */ Collections.emptyList(), .build();
keySetId2,
/* customCacheKey= */ "key2",
data2);
// uri, keySetId, customCacheKey and data should be from the request being merged. // uri, keySetId, customCacheKey and data should be from the request being merged.
DownloadRequest mergedRequest = request1.copyWithMergedRequest(request2); DownloadRequest mergedRequest = request1.copyWithMergedRequest(request2);
@ -152,14 +131,12 @@ public class DownloadRequestTest {
streamKeys.add(new StreamKey(1, 2, 3)); streamKeys.add(new StreamKey(1, 2, 3));
streamKeys.add(new StreamKey(4, 5, 6)); streamKeys.add(new StreamKey(4, 5, 6));
DownloadRequest requestToParcel = DownloadRequest requestToParcel =
new DownloadRequest( new DownloadRequest.Builder("id", Uri.parse("https://abc.def/ghi"))
/* id= */ "id", .setStreamKeys(streamKeys)
Uri.parse("https://abc.def/ghi"), .setKeySetId(new byte[] {1, 2, 3, 4, 5})
/* mimeType= */ null, .setCustomCacheKey("key")
streamKeys, .setData(new byte[] {1, 2, 3, 4, 5})
/* keySetId= */ new byte[] {1, 2, 3, 4, 5}, .build();
/* customCacheKey= */ "key",
/* data= */ new byte[] {1, 2, 3, 4, 5});
Parcel parcel = Parcel.obtain(); Parcel parcel = Parcel.obtain();
requestToParcel.writeToParcel(parcel, 0); requestToParcel.writeToParcel(parcel, 0);
parcel.setDataPosition(0); parcel.setDataPosition(0);
@ -217,19 +194,6 @@ public class DownloadRequestTest {
} }
private static DownloadRequest createRequest(Uri uri, StreamKey... keys) { private static DownloadRequest createRequest(Uri uri, StreamKey... keys) {
return new DownloadRequest( return new DownloadRequest.Builder(uri.toString(), uri).setStreamKeys(asList(keys)).build();
uri.toString(),
uri,
/* mimeType= */ null,
toList(keys),
/* keySetId= */ null,
/* customCacheKey= */ null,
/* data= */ null);
}
private static List<StreamKey> toList(StreamKey... keys) {
ArrayList<StreamKey> keysList = new ArrayList<>();
Collections.addAll(keysList, keys);
return keysList;
} }
} }

View File

@ -92,14 +92,12 @@ public class DashDownloaderTest {
Downloader downloader = Downloader downloader =
factory.createDownloader( factory.createDownloader(
new DownloadRequest( new DownloadRequest.Builder(/* id= */ "id", Uri.parse("https://www.test.com/download"))
"id", .setMimeType(MimeTypes.APPLICATION_MPD)
Uri.parse("https://www.test.com/download"), .setStreamKeys(
MimeTypes.APPLICATION_MPD, Collections.singletonList(
Collections.singletonList(new StreamKey(/* groupIndex= */ 0, /* trackIndex= */ 0)), new StreamKey(/* groupIndex= */ 0, /* trackIndex= */ 0)))
/* keySetId= */ null, .build());
/* customCacheKey= */ null,
/* data= */ null));
assertThat(downloader).isInstanceOf(DashDownloader.class); assertThat(downloader).isInstanceOf(DashDownloader.class);
} }

View File

@ -220,14 +220,10 @@ public class DownloadManagerDashTest {
private DownloadRequest getDownloadRequest(StreamKey... keys) { private DownloadRequest getDownloadRequest(StreamKey... keys) {
ArrayList<StreamKey> keysList = new ArrayList<>(); ArrayList<StreamKey> keysList = new ArrayList<>();
Collections.addAll(keysList, keys); Collections.addAll(keysList, keys);
return new DownloadRequest( return new DownloadRequest.Builder(TEST_ID, TEST_MPD_URI)
TEST_ID, .setMimeType(MimeTypes.APPLICATION_MPD)
TEST_MPD_URI, .setStreamKeys(keysList)
MimeTypes.APPLICATION_MPD, .build();
keysList,
/* keySetId= */ null,
/* customCacheKey= */ null,
null);
} }
private void handleRemoveAction() { private void handleRemoveAction() {

View File

@ -206,14 +206,11 @@ public class DownloadServiceDashTest {
ArrayList<StreamKey> keysList = new ArrayList<>(); ArrayList<StreamKey> keysList = new ArrayList<>();
Collections.addAll(keysList, keys); Collections.addAll(keysList, keys);
DownloadRequest action = DownloadRequest action =
new DownloadRequest( new DownloadRequest.Builder(TEST_ID, TEST_MPD_URI)
TEST_ID, .setMimeType(MimeTypes.APPLICATION_MPD)
TEST_MPD_URI, .setStreamKeys(keysList)
MimeTypes.APPLICATION_MPD, .build();
keysList,
/* keySetId= */ null,
/* customCacheKey= */ null,
null);
testThread.runOnMainThread( testThread.runOnMainThread(
() -> { () -> {
Intent startIntent = Intent startIntent =

View File

@ -110,14 +110,12 @@ public class HlsDownloaderTest {
Downloader downloader = Downloader downloader =
factory.createDownloader( factory.createDownloader(
new DownloadRequest( new DownloadRequest.Builder(/* id= */ "id", Uri.parse("https://www.test.com/download"))
"id", .setMimeType(MimeTypes.APPLICATION_M3U8)
Uri.parse("https://www.test.com/download"), .setStreamKeys(
MimeTypes.APPLICATION_M3U8, Collections.singletonList(
Collections.singletonList(new StreamKey(/* groupIndex= */ 0, /* trackIndex= */ 0)), new StreamKey(/* groupIndex= */ 0, /* trackIndex= */ 0)))
/* keySetId= */ null, .build());
/* customCacheKey= */ null,
/* data= */ null));
assertThat(downloader).isInstanceOf(HlsDownloader.class); assertThat(downloader).isInstanceOf(HlsDownloader.class);
} }

View File

@ -48,14 +48,12 @@ public final class SsDownloaderTest {
Downloader downloader = Downloader downloader =
factory.createDownloader( factory.createDownloader(
new DownloadRequest( new DownloadRequest.Builder(/* id= */ "id", Uri.parse("https://www.test.com/download"))
"id", .setMimeType(MimeTypes.APPLICATION_SS)
Uri.parse("https://www.test.com/download"), .setStreamKeys(
MimeTypes.APPLICATION_SS, Collections.singletonList(
Collections.singletonList(new StreamKey(/* groupIndex= */ 0, /* trackIndex= */ 0)), new StreamKey(/* groupIndex= */ 0, /* trackIndex= */ 0)))
/* keySetId= */ null, .build());
/* customCacheKey= */ null,
/* data= */ null));
assertThat(downloader).isInstanceOf(SsDownloader.class); assertThat(downloader).isInstanceOf(SsDownloader.class);
} }
} }

View File

@ -198,7 +198,13 @@ public final class DownloadBuilder {
public Download build() { public Download build() {
DownloadRequest request = 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( return new Download(
request, request,
state, state,