From 8ce3d4dc729f53c3f44649ae790d7f6b3a63d5da Mon Sep 17 00:00:00 2001 From: bachinger Date: Mon, 25 Jul 2022 12:29:44 +0000 Subject: [PATCH] Exclude Metadata from Format when bundling from TrackGroup #minor-release PiperOrigin-RevId: 463062454 --- .../java/androidx/media3/common/Format.java | 17 +++++++++++++---- .../java/androidx/media3/common/TrackGroup.java | 9 ++++++--- .../java/androidx/media3/common/FormatTest.java | 12 +++++++++++- 3 files changed, 30 insertions(+), 8 deletions(-) diff --git a/libraries/common/src/main/java/androidx/media3/common/Format.java b/libraries/common/src/main/java/androidx/media3/common/Format.java index b90881e6369..bb712e24723 100644 --- a/libraries/common/src/main/java/androidx/media3/common/Format.java +++ b/libraries/common/src/main/java/androidx/media3/common/Format.java @@ -1547,6 +1547,15 @@ public static String toLogString(@Nullable Format format) { @UnstableApi @Override public Bundle toBundle() { + return toBundle(/* excludeMetadata= */ false); + } + + /** + * Returns a {@link Bundle} representing the information stored in this object. If {@code + * excludeMetadata} is true, {@linkplain Format#metadata metadata} is excluded. + */ + @UnstableApi + public Bundle toBundle(boolean excludeMetadata) { Bundle bundle = new Bundle(); bundle.putString(keyForField(FIELD_ID), id); bundle.putString(keyForField(FIELD_LABEL), label); @@ -1556,10 +1565,10 @@ public Bundle toBundle() { bundle.putInt(keyForField(FIELD_AVERAGE_BITRATE), averageBitrate); bundle.putInt(keyForField(FIELD_PEAK_BITRATE), peakBitrate); bundle.putString(keyForField(FIELD_CODECS), codecs); - // Metadata is currently not Bundleable because Metadata.Entry is an Interface, - // which would be difficult to unbundle in a backward compatible way. - // The entries are additionally of limited usefulness to remote processes. - bundle.putParcelable(keyForField(FIELD_METADATA), metadata); + if (!excludeMetadata) { + // TODO (internal ref: b/239701618) + bundle.putParcelable(keyForField(FIELD_METADATA), metadata); + } // Container specific. bundle.putString(keyForField(FIELD_CONTAINER_MIME_TYPE), containerMimeType); // Sample specific. diff --git a/libraries/common/src/main/java/androidx/media3/common/TrackGroup.java b/libraries/common/src/main/java/androidx/media3/common/TrackGroup.java index 6cb679368a4..ce934111d56 100644 --- a/libraries/common/src/main/java/androidx/media3/common/TrackGroup.java +++ b/libraries/common/src/main/java/androidx/media3/common/TrackGroup.java @@ -26,11 +26,11 @@ import androidx.media3.common.util.Log; import androidx.media3.common.util.UnstableApi; import com.google.common.collect.ImmutableList; -import com.google.common.collect.Lists; import java.lang.annotation.Documented; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; +import java.util.ArrayList; import java.util.Arrays; import java.util.List; @@ -179,8 +179,11 @@ public boolean equals(@Nullable Object obj) { @Override public Bundle toBundle() { Bundle bundle = new Bundle(); - bundle.putParcelableArrayList( - keyForField(FIELD_FORMATS), BundleableUtil.toBundleArrayList(Lists.newArrayList(formats))); + ArrayList arrayList = new ArrayList<>(formats.length); + for (Format format : formats) { + arrayList.add(format.toBundle(/* excludeMetadata= */ true)); + } + bundle.putParcelableArrayList(keyForField(FIELD_FORMATS), arrayList); bundle.putString(keyForField(FIELD_ID), id); return bundle; } diff --git a/libraries/common/src/test/java/androidx/media3/common/FormatTest.java b/libraries/common/src/test/java/androidx/media3/common/FormatTest.java index 6e184def744..ab656935ff6 100644 --- a/libraries/common/src/test/java/androidx/media3/common/FormatTest.java +++ b/libraries/common/src/test/java/androidx/media3/common/FormatTest.java @@ -20,6 +20,7 @@ import static androidx.media3.common.MimeTypes.VIDEO_WEBM; import static com.google.common.truth.Truth.assertThat; +import android.os.Bundle; import androidx.media3.test.utils.FakeMetadataEntry; import androidx.test.ext.junit.runners.AndroidJUnit4; import java.util.ArrayList; @@ -46,6 +47,16 @@ public void roundTripViaBundle_ofParameters_yieldsEqualInstance() { assertThat(formatFromBundle).isEqualTo(formatToBundle); } + @Test + public void roundTripViaBundle_excludeMetadata_hasMetadataExcluded() { + Format format = createTestFormat(); + + Bundle bundleWithMetadataExcluded = format.toBundle(/* excludeMetadata= */ true); + + Format formatWithMetadataExcluded = Format.CREATOR.fromBundle(bundleWithMetadataExcluded); + assertThat(formatWithMetadataExcluded).isEqualTo(format.buildUpon().setMetadata(null).build()); + } + private static Format createTestFormat() { byte[] initData1 = new byte[] {1, 2, 3}; byte[] initData2 = new byte[] {4, 5, 6}; @@ -60,7 +71,6 @@ private static Format createTestFormat() { DrmInitData drmInitData = new DrmInitData(drmData1, drmData2); byte[] projectionData = new byte[] {1, 2, 3}; - Metadata metadata = new Metadata(new FakeMetadataEntry("id1"), new FakeMetadataEntry("id2")); ColorInfo colorInfo =