Make StreamKey Bundleable and remove deprecated trackIndex

#minor-release

PiperOrigin-RevId: 538809105
This commit is contained in:
jbibik 2023-06-08 16:33:41 +00:00 committed by Tofunmi Adigun-Hameed
parent 276f2f1fe6
commit 28b8fb706a
5 changed files with 81 additions and 19 deletions

View File

@ -1215,7 +1215,8 @@ public final class MediaItem implements Bundleable {
bundle.putBundle(FIELD_ADS_CONFIGURATION, adsConfiguration.toBundle()); bundle.putBundle(FIELD_ADS_CONFIGURATION, adsConfiguration.toBundle());
} }
if (!streamKeys.isEmpty()) { if (!streamKeys.isEmpty()) {
bundle.putParcelableArrayList(FIELD_STREAM_KEYS, new ArrayList<>(streamKeys)); bundle.putParcelableArrayList(
FIELD_STREAM_KEYS, BundleableUtil.toBundleArrayList(streamKeys));
} }
if (customCacheKey != null) { if (customCacheKey != null) {
bundle.putString(FIELD_CUSTOM_CACHE_KEY, customCacheKey); bundle.putString(FIELD_CUSTOM_CACHE_KEY, customCacheKey);
@ -1239,9 +1240,11 @@ public final class MediaItem implements Bundleable {
@Nullable Bundle adsBundle = bundle.getBundle(FIELD_ADS_CONFIGURATION); @Nullable Bundle adsBundle = bundle.getBundle(FIELD_ADS_CONFIGURATION);
AdsConfiguration adsConfiguration = AdsConfiguration adsConfiguration =
adsBundle == null ? null : AdsConfiguration.CREATOR.fromBundle(adsBundle); adsBundle == null ? null : AdsConfiguration.CREATOR.fromBundle(adsBundle);
@Nullable List<StreamKey> streamKeysList = bundle.getParcelableArrayList(FIELD_STREAM_KEYS); @Nullable List<Bundle> streamKeysBundles = bundle.getParcelableArrayList(FIELD_STREAM_KEYS);
List<StreamKey> streamKeys = List<StreamKey> streamKeys =
streamKeysList == null ? ImmutableList.of() : ImmutableList.copyOf(streamKeysList); streamKeysBundles == null
? ImmutableList.of()
: BundleableUtil.fromBundleList(StreamKey::fromBundle, streamKeysBundles);
@Nullable @Nullable
List<Bundle> subtitleBundles = bundle.getParcelableArrayList(FIELD_SUBTITLE_CONFIGURATION); List<Bundle> subtitleBundles = bundle.getParcelableArrayList(FIELD_SUBTITLE_CONFIGURATION);
ImmutableList<SubtitleConfiguration> subtitleConfiguration = ImmutableList<SubtitleConfiguration> subtitleConfiguration =

View File

@ -15,10 +15,12 @@
*/ */
package androidx.media3.common; package androidx.media3.common;
import android.os.Bundle;
import android.os.Parcel; import android.os.Parcel;
import android.os.Parcelable; import android.os.Parcelable;
import androidx.annotation.Nullable; import androidx.annotation.Nullable;
import androidx.media3.common.util.UnstableApi; import androidx.media3.common.util.UnstableApi;
import androidx.media3.common.util.Util;
/** /**
* A key for a subset of media that can be separately loaded (a "stream"). * A key for a subset of media that can be separately loaded (a "stream").
@ -35,7 +37,7 @@ import androidx.media3.common.util.UnstableApi;
* particular track selection. * particular track selection.
*/ */
@UnstableApi @UnstableApi
public final class StreamKey implements Comparable<StreamKey>, Parcelable { public final class StreamKey implements Comparable<StreamKey>, Parcelable, Bundleable {
/** The period index. */ /** The period index. */
public final int periodIndex; public final int periodIndex;
@ -44,11 +46,6 @@ public final class StreamKey implements Comparable<StreamKey>, Parcelable {
/** The stream index. */ /** The stream index. */
public final int streamIndex; public final int streamIndex;
/**
* @deprecated Use {@link #streamIndex}.
*/
@Deprecated public final int trackIndex;
/** /**
* Creates an instance with {@link #periodIndex} set to 0. * Creates an instance with {@link #periodIndex} set to 0.
* *
@ -60,26 +57,22 @@ public final class StreamKey implements Comparable<StreamKey>, Parcelable {
} }
/** /**
* Creates an instance. * Creates an instance of {@link StreamKey} using 3 indices.
* *
* @param periodIndex The period index. * @param periodIndex The period index.
* @param groupIndex The group index. * @param groupIndex The group index.
* @param streamIndex The stream index. * @param streamIndex The stream index.
*/ */
@SuppressWarnings("deprecation")
public StreamKey(int periodIndex, int groupIndex, int streamIndex) { public StreamKey(int periodIndex, int groupIndex, int streamIndex) {
this.periodIndex = periodIndex; this.periodIndex = periodIndex;
this.groupIndex = groupIndex; this.groupIndex = groupIndex;
this.streamIndex = streamIndex; this.streamIndex = streamIndex;
trackIndex = streamIndex;
} }
@SuppressWarnings("deprecation")
/* package */ StreamKey(Parcel in) { /* package */ StreamKey(Parcel in) {
periodIndex = in.readInt(); periodIndex = in.readInt();
groupIndex = in.readInt(); groupIndex = in.readInt();
streamIndex = in.readInt(); streamIndex = in.readInt();
trackIndex = streamIndex;
} }
@Override @Override
@ -151,4 +144,36 @@ public final class StreamKey implements Comparable<StreamKey>, Parcelable {
return new StreamKey[size]; return new StreamKey[size];
} }
}; };
// Bundleable implementation.
private static final String FIELD_PERIOD_INDEX = Util.intToStringMaxRadix(0);
private static final String FIELD_GROUP_INDEX = Util.intToStringMaxRadix(1);
private static final String FIELD_STREAM_INDEX = Util.intToStringMaxRadix(2);
@Override
public Bundle toBundle() {
Bundle bundle = new Bundle();
if (periodIndex != 0) {
bundle.putInt(FIELD_PERIOD_INDEX, periodIndex);
}
if (groupIndex != 0) {
bundle.putInt(FIELD_GROUP_INDEX, groupIndex);
}
if (streamIndex != 0) {
bundle.putInt(FIELD_STREAM_INDEX, streamIndex);
}
return bundle;
}
/**
* Constructs an instance of {@link StreamKey} from a {@link Bundle} produced by {@link
* #toBundle()}.
*/
public static StreamKey fromBundle(Bundle bundle) {
return new StreamKey(
bundle.getInt(FIELD_PERIOD_INDEX, /* defaultValue= */ 0),
bundle.getInt(FIELD_GROUP_INDEX, /* defaultValue= */ 0),
bundle.getInt(FIELD_STREAM_INDEX, /* defaultValue= */ 0));
}
} }

View File

@ -17,6 +17,7 @@ package androidx.media3.common;
import static com.google.common.truth.Truth.assertThat; import static com.google.common.truth.Truth.assertThat;
import android.os.Bundle;
import android.os.Parcel; import android.os.Parcel;
import androidx.test.ext.junit.runners.AndroidJUnit4; import androidx.test.ext.junit.runners.AndroidJUnit4;
import org.junit.Test; import org.junit.Test;
@ -28,7 +29,8 @@ public class StreamKeyTest {
@Test @Test
public void parcelable() { public void parcelable() {
StreamKey streamKeyToParcel = new StreamKey(1, 2, 3); StreamKey streamKeyToParcel =
new StreamKey(/* periodIndex= */ 1, /* groupIndex= */ 2, /* streamIndex= */ 3);
Parcel parcel = Parcel.obtain(); Parcel parcel = Parcel.obtain();
streamKeyToParcel.writeToParcel(parcel, 0); streamKeyToParcel.writeToParcel(parcel, 0);
parcel.setDataPosition(0); parcel.setDataPosition(0);
@ -38,4 +40,36 @@ public class StreamKeyTest {
parcel.recycle(); parcel.recycle();
} }
@Test
public void roundTripViaBundle_withDefaultPeriodIndex_yieldsEqualInstance() {
StreamKey originalStreamKey = new StreamKey(/* groupIndex= */ 1, /* streamIndex= */ 2);
StreamKey streamKeyFromBundle = StreamKey.fromBundle(originalStreamKey.toBundle());
assertThat(originalStreamKey).isEqualTo(streamKeyFromBundle);
}
@Test
public void roundTripViaBundle_toBundleSkipsDefaultValues_fromBundleRestoresThem() {
StreamKey originalStreamKey = new StreamKey(/* groupIndex= */ 0, /* streamIndex= */ 0);
Bundle streamKeyBundle = originalStreamKey.toBundle();
assertThat(streamKeyBundle.keySet()).isEmpty();
StreamKey streamKeyFromBundle = StreamKey.fromBundle(streamKeyBundle);
assertThat(originalStreamKey).isEqualTo(streamKeyFromBundle);
}
@Test
public void roundTripViaBundle_yieldsEqualInstance() {
StreamKey originalStreamKey =
new StreamKey(/* periodIndex= */ 10, /* groupIndex= */ 11, /* streamIndex= */ 12);
StreamKey streamKeyFromBundle = StreamKey.fromBundle(originalStreamKey.toBundle());
assertThat(originalStreamKey).isEqualTo(streamKeyFromBundle);
}
} }

View File

@ -102,8 +102,8 @@ public class DefaultDownloadIndexTest {
.setStartTimeMs(10) .setStartTimeMs(10)
.setUpdateTimeMs(20) .setUpdateTimeMs(20)
.setStreamKeys( .setStreamKeys(
new StreamKey(/* periodIndex= */ 0, /* groupIndex= */ 1, /* trackIndex= */ 2), new StreamKey(/* periodIndex= */ 0, /* groupIndex= */ 1, /* streamIndex= */ 2),
new StreamKey(/* periodIndex= */ 3, /* groupIndex= */ 4, /* trackIndex= */ 5)) new StreamKey(/* periodIndex= */ 3, /* groupIndex= */ 4, /* streamIndex= */ 5))
.setCustomMetadata(new byte[] {0, 1, 2, 3, 7, 8, 9, 10}) .setCustomMetadata(new byte[] {0, 1, 2, 3, 7, 8, 9, 10})
.setKeySetId(new byte[] {0, 1, 2, 3}) .setKeySetId(new byte[] {0, 1, 2, 3})
.build(); .build();

View File

@ -302,13 +302,13 @@ public class DownloadManagerTest {
@Test @Test
public void downloads_withSameIdsAndDifferentStreamKeys_areMerged() throws Throwable { public void downloads_withSameIdsAndDifferentStreamKeys_areMerged() throws Throwable {
StreamKey streamKey1 = new StreamKey(/* groupIndex= */ 0, /* trackIndex= */ 0); StreamKey streamKey1 = new StreamKey(/* groupIndex= */ 0, /* streamIndex= */ 0);
postDownloadRequest(ID1, streamKey1); postDownloadRequest(ID1, streamKey1);
FakeDownloader downloader0 = getDownloaderAt(0); FakeDownloader downloader0 = getDownloaderAt(0);
downloader0.assertId(ID1); downloader0.assertId(ID1);
downloader0.assertDownloadStarted(); downloader0.assertDownloadStarted();
StreamKey streamKey2 = new StreamKey(/* groupIndex= */ 1, /* trackIndex= */ 1); StreamKey streamKey2 = new StreamKey(/* groupIndex= */ 1, /* streamIndex= */ 1);
postDownloadRequest(ID1, streamKey2); postDownloadRequest(ID1, streamKey2);
// The request for streamKey2 will cause the downloader for streamKey1 to be canceled and // The request for streamKey2 will cause the downloader for streamKey1 to be canceled and
// replaced with a new downloader for both keys. // replaced with a new downloader for both keys.