diff --git a/RELEASENOTES.md b/RELEASENOTES.md index d27fe26607..1dd124c27f 100644 --- a/RELEASENOTES.md +++ b/RELEASENOTES.md @@ -19,7 +19,7 @@ * Move player message-related constants from `C` to `Renderer`, to avoid having the constants class depend on player/renderer classes. * Split out `common` and `extractor` submodules. - * Add `DataSpec.Builder`. + * Add `DataSpec.Builder` and `DataSpec.customData`. * Text: * Parse `` and `` tags in WebVTT subtitles (rendering is coming later). diff --git a/library/common/src/main/java/com/google/android/exoplayer2/upstream/DataSpec.java b/library/common/src/main/java/com/google/android/exoplayer2/upstream/DataSpec.java index 29263b4323..2a4eb2f661 100644 --- a/library/common/src/main/java/com/google/android/exoplayer2/upstream/DataSpec.java +++ b/library/common/src/main/java/com/google/android/exoplayer2/upstream/DataSpec.java @@ -44,6 +44,7 @@ public final class DataSpec { private long length; @Nullable private String key; @Flags private int flags; + @Nullable private Object customData; /** Creates a new instance with default values. */ public Builder() { @@ -67,6 +68,7 @@ public final class DataSpec { length = dataSpec.length; key = dataSpec.key; flags = dataSpec.flags; + customData = dataSpec.customData; } /** @@ -168,6 +170,17 @@ public final class DataSpec { return this; } + /** + * Sets the {@link DataSpec#customData}. The default value is {@code null}. + * + * @param customData The {@link DataSpec#customData}. + * @return The builder. + */ + public Builder setCustomData(@Nullable Object customData) { + this.customData = customData; + return this; + } + /** * Builds a {@link DataSpec} with the builder's current values. * @@ -185,7 +198,8 @@ public final class DataSpec { position, length, key, - flags); + flags, + customData); } } @@ -325,6 +339,14 @@ public final class DataSpec { /** Request {@link Flags flags}. */ @Flags public final int flags; + /** + * Application specific data. + * + *

This field is intended for advanced use cases in which applications require the ability to + * attach custom data to {@link DataSpec} instances. The custom data should be immutable. + */ + @Nullable public final Object customData; + /** * Constructs an instance. * @@ -517,7 +539,8 @@ public final class DataSpec { position, length, key, - flags); + flags, + /* customData= */ null); } @SuppressWarnings("deprecation") @@ -530,7 +553,8 @@ public final class DataSpec { long position, long length, @Nullable String key, - @Flags int flags) { + @Flags int flags, + @Nullable Object customData) { // TODO: Replace this assertion with a stricter one checking "uriPositionOffset >= 0", after // validating there are no violations in ExoPlayer and 1P apps. Assertions.checkArgument(uriPositionOffset + position >= 0); @@ -546,6 +570,7 @@ public final class DataSpec { this.length = length; this.key = key; this.flags = flags; + this.customData = customData; } /** @@ -601,7 +626,8 @@ public final class DataSpec { position + offset, length, key, - flags); + flags, + customData); } } @@ -621,7 +647,8 @@ public final class DataSpec { position, length, key, - flags); + flags, + customData); } /** @@ -641,7 +668,8 @@ public final class DataSpec { position, length, key, - flags); + flags, + customData); } /** @@ -664,7 +692,8 @@ public final class DataSpec { position, length, key, - flags); + flags, + customData); } @Override diff --git a/library/common/src/test/java/com/google/android/exoplayer2/upstream/DataSpecTest.java b/library/common/src/test/java/com/google/android/exoplayer2/upstream/DataSpecTest.java index 7235cc747c..eefd4f8cb6 100644 --- a/library/common/src/test/java/com/google/android/exoplayer2/upstream/DataSpecTest.java +++ b/library/common/src/test/java/com/google/android/exoplayer2/upstream/DataSpecTest.java @@ -141,6 +141,7 @@ public class DataSpecTest { Uri uri = Uri.parse("www.google.com"); Map httpRequestHeaders = createHttpRequestHeaders(3); byte[] httpBody = new byte[] {0, 1, 2, 3}; + Object customData = new Object(); DataSpec dataSpec = new DataSpec.Builder() @@ -153,6 +154,7 @@ public class DataSpecTest { .setKey("key") .setFlags(DataSpec.FLAG_ALLOW_GZIP) .setHttpRequestHeaders(httpRequestHeaders) + .setCustomData(customData) .build(); assertThat(dataSpec.uri).isEqualTo(uri); @@ -166,6 +168,7 @@ public class DataSpecTest { assertThat(dataSpec.length).isEqualTo(5); assertThat(dataSpec.key).isEqualTo("key"); assertThat(dataSpec.flags).isEqualTo(DataSpec.FLAG_ALLOW_GZIP); + assertThat(dataSpec.customData).isEqualTo(customData); assertHttpRequestHeadersReadOnly(dataSpec); } @@ -175,6 +178,7 @@ public class DataSpecTest { Uri uri = Uri.parse("www.google.com"); Map httpRequestHeaders = createHttpRequestHeaders(3); byte[] httpBody = new byte[] {0, 1, 2, 3}; + Object customData = new Object(); DataSpec dataSpec = new DataSpec.Builder() @@ -187,6 +191,7 @@ public class DataSpecTest { .setKey("key") .setFlags(DataSpec.FLAG_ALLOW_GZIP) .setHttpRequestHeaders(httpRequestHeaders) + .setCustomData(customData) .build(); // Build upon the DataSpec. @@ -203,6 +208,7 @@ public class DataSpecTest { assertThat(dataSpec.length).isEqualTo(5); assertThat(dataSpec.key).isEqualTo("key"); assertThat(dataSpec.flags).isEqualTo(DataSpec.FLAG_ALLOW_GZIP); + assertThat(dataSpec.customData).isEqualTo(customData); assertHttpRequestHeadersReadOnly(dataSpec); } @@ -347,6 +353,7 @@ public class DataSpecTest { assertThat(dataSpec.length).isEqualTo(C.LENGTH_UNSET); assertThat(dataSpec.key).isNull(); assertThat(dataSpec.flags).isEqualTo(0); + assertThat(dataSpec.customData).isNull(); assertHttpRequestHeadersReadOnly(dataSpec); }