diff --git a/library/core/src/main/java/com/google/android/exoplayer2/source/TrackGroup.java b/library/core/src/main/java/com/google/android/exoplayer2/source/TrackGroup.java index 06410d5426..2e5b259a88 100644 --- a/library/core/src/main/java/com/google/android/exoplayer2/source/TrackGroup.java +++ b/library/core/src/main/java/com/google/android/exoplayer2/source/TrackGroup.java @@ -15,6 +15,8 @@ */ package com.google.android.exoplayer2.source; +import android.os.Parcel; +import android.os.Parcelable; import com.google.android.exoplayer2.C; import com.google.android.exoplayer2.Format; import com.google.android.exoplayer2.util.Assertions; @@ -24,12 +26,12 @@ import java.util.Arrays; // does not apply. /** * Defines a group of tracks exposed by a {@link MediaPeriod}. - *

- * A {@link MediaPeriod} is only able to provide one {@link SampleStream} corresponding to a group - * at any given time, however this {@link SampleStream} may adapt between multiple tracks within the - * group. + * + *

A {@link MediaPeriod} is only able to provide one {@link SampleStream} corresponding to a + * group at any given time, however this {@link SampleStream} may adapt between multiple tracks + * within the group. */ -public final class TrackGroup { +public final class TrackGroup implements Parcelable { /** * The number of tracks in the group. @@ -50,6 +52,14 @@ public final class TrackGroup { this.length = formats.length; } + /* package */ TrackGroup(Parcel in) { + length = in.readInt(); + formats = new Format[length]; + for (int i = 0; i < length; i++) { + formats[i] = in.readParcelable(Format.class.getClassLoader()); + } + } + /** * Returns the format of the track at a given index. * @@ -97,4 +107,32 @@ public final class TrackGroup { return length == other.length && Arrays.equals(formats, other.formats); } + // Parcelable implementation. + + @Override + public int describeContents() { + return 0; + } + + @Override + public void writeToParcel(Parcel dest, int flags) { + dest.writeInt(length); + for (int i = 0; i < length; i++) { + dest.writeParcelable(formats[i], 0); + } + } + + public static final Parcelable.Creator CREATOR = + new Parcelable.Creator() { + + @Override + public TrackGroup createFromParcel(Parcel in) { + return new TrackGroup(in); + } + + @Override + public TrackGroup[] newArray(int size) { + return new TrackGroup[size]; + } + }; } diff --git a/library/core/src/main/java/com/google/android/exoplayer2/source/TrackGroupArray.java b/library/core/src/main/java/com/google/android/exoplayer2/source/TrackGroupArray.java index fb28da581c..72afa3463e 100644 --- a/library/core/src/main/java/com/google/android/exoplayer2/source/TrackGroupArray.java +++ b/library/core/src/main/java/com/google/android/exoplayer2/source/TrackGroupArray.java @@ -15,13 +15,13 @@ */ package com.google.android.exoplayer2.source; +import android.os.Parcel; +import android.os.Parcelable; import com.google.android.exoplayer2.C; import java.util.Arrays; -/** - * An array of {@link TrackGroup}s exposed by a {@link MediaPeriod}. - */ -public final class TrackGroupArray { +/** An array of {@link TrackGroup}s exposed by a {@link MediaPeriod}. */ +public final class TrackGroupArray implements Parcelable { /** * The empty array. @@ -46,6 +46,14 @@ public final class TrackGroupArray { this.length = trackGroups.length; } + /* package */ TrackGroupArray(Parcel in) { + length = in.readInt(); + trackGroups = new TrackGroup[length]; + for (int i = 0; i < length; i++) { + trackGroups[i] = in.readParcelable(TrackGroup.class.getClassLoader()); + } + } + /** * Returns the group at a given index. * @@ -101,4 +109,32 @@ public final class TrackGroupArray { return length == other.length && Arrays.equals(trackGroups, other.trackGroups); } + // Parcelable implementation. + + @Override + public int describeContents() { + return 0; + } + + @Override + public void writeToParcel(Parcel dest, int flags) { + dest.writeInt(length); + for (int i = 0; i < length; i++) { + dest.writeParcelable(trackGroups[i], 0); + } + } + + public static final Parcelable.Creator CREATOR = + new Parcelable.Creator() { + + @Override + public TrackGroupArray createFromParcel(Parcel in) { + return new TrackGroupArray(in); + } + + @Override + public TrackGroupArray[] newArray(int size) { + return new TrackGroupArray[size]; + } + }; } diff --git a/library/core/src/test/java/com/google/android/exoplayer2/source/TrackGroupArrayTest.java b/library/core/src/test/java/com/google/android/exoplayer2/source/TrackGroupArrayTest.java new file mode 100644 index 0000000000..86778a0fa0 --- /dev/null +++ b/library/core/src/test/java/com/google/android/exoplayer2/source/TrackGroupArrayTest.java @@ -0,0 +1,51 @@ +/* + * Copyright (C) 2018 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.google.android.exoplayer2.source; + +import static com.google.common.truth.Truth.assertThat; + +import android.os.Parcel; +import com.google.android.exoplayer2.Format; +import com.google.android.exoplayer2.util.MimeTypes; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.robolectric.RobolectricTestRunner; + +/** Unit test for {@link TrackGroupArray}. */ +@RunWith(RobolectricTestRunner.class) +public final class TrackGroupArrayTest { + + @Test + public void testParcelable() { + Format format1 = Format.createSampleFormat("1", MimeTypes.VIDEO_H264, 0); + Format format2 = Format.createSampleFormat("2", MimeTypes.AUDIO_AAC, 0); + Format format3 = Format.createSampleFormat("3", MimeTypes.VIDEO_H264, 0); + + TrackGroup trackGroup1 = new TrackGroup(format1, format2); + TrackGroup trackGroup2 = new TrackGroup(format3); + + TrackGroupArray trackGroupArrayToParcel = new TrackGroupArray(trackGroup1, trackGroup2); + + Parcel parcel = Parcel.obtain(); + trackGroupArrayToParcel.writeToParcel(parcel, 0); + parcel.setDataPosition(0); + + TrackGroupArray trackGroupArrayFromParcel = TrackGroupArray.CREATOR.createFromParcel(parcel); + assertThat(trackGroupArrayFromParcel).isEqualTo(trackGroupArrayToParcel); + + parcel.recycle(); + } +} diff --git a/library/core/src/test/java/com/google/android/exoplayer2/source/TrackGroupTest.java b/library/core/src/test/java/com/google/android/exoplayer2/source/TrackGroupTest.java new file mode 100644 index 0000000000..1900f3c586 --- /dev/null +++ b/library/core/src/test/java/com/google/android/exoplayer2/source/TrackGroupTest.java @@ -0,0 +1,47 @@ +/* + * Copyright (C) 2018 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.google.android.exoplayer2.source; + +import static com.google.common.truth.Truth.assertThat; + +import android.os.Parcel; +import com.google.android.exoplayer2.Format; +import com.google.android.exoplayer2.util.MimeTypes; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.robolectric.RobolectricTestRunner; + +/** Unit test for {@link TrackGroup}. */ +@RunWith(RobolectricTestRunner.class) +public final class TrackGroupTest { + + @Test + public void testParcelable() { + Format format1 = Format.createSampleFormat("1", MimeTypes.VIDEO_H264, 0); + Format format2 = Format.createSampleFormat("2", MimeTypes.AUDIO_AAC, 0); + + TrackGroup trackGroupToParcel = new TrackGroup(format1, format2); + + Parcel parcel = Parcel.obtain(); + trackGroupToParcel.writeToParcel(parcel, 0); + parcel.setDataPosition(0); + + TrackGroup trackGroupFromParcel = TrackGroup.CREATOR.createFromParcel(parcel); + assertThat(trackGroupFromParcel).isEqualTo(trackGroupToParcel); + + parcel.recycle(); + } +}