Make TrackGroup and TrackGroupArray parcelable

Issue: #3915

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=188047131
This commit is contained in:
olly 2018-03-06 10:57:43 -08:00 committed by Oliver Woodman
parent f81dc44112
commit b7c9912a3d
4 changed files with 181 additions and 9 deletions

View File

@ -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}.
* <p>
* 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.
*
* <p>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<TrackGroup> CREATOR =
new Parcelable.Creator<TrackGroup>() {
@Override
public TrackGroup createFromParcel(Parcel in) {
return new TrackGroup(in);
}
@Override
public TrackGroup[] newArray(int size) {
return new TrackGroup[size];
}
};
}

View File

@ -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<TrackGroupArray> CREATOR =
new Parcelable.Creator<TrackGroupArray>() {
@Override
public TrackGroupArray createFromParcel(Parcel in) {
return new TrackGroupArray(in);
}
@Override
public TrackGroupArray[] newArray(int size) {
return new TrackGroupArray[size];
}
};
}

View File

@ -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();
}
}

View File

@ -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();
}
}