Get all track key objects into a consistent state

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=195421908
This commit is contained in:
olly 2018-05-04 08:50:46 -07:00 committed by Oliver Woodman
parent b8206a70e6
commit 0edc832d67
7 changed files with 131 additions and 77 deletions

View File

@ -294,9 +294,9 @@ public class DownloadActivity extends Activity {
trackFormats.add(DUMMY_FORMAT); trackFormats.add(DUMMY_FORMAT);
} else { } else {
HlsMasterPlaylist masterPlaylist = (HlsMasterPlaylist) playlist; HlsMasterPlaylist masterPlaylist = (HlsMasterPlaylist) playlist;
addRepresentationItems(masterPlaylist.variants, RenditionKey.GROUP_VARIANTS); addRepresentationItems(masterPlaylist.variants, RenditionKey.TYPE_VARIANT);
addRepresentationItems(masterPlaylist.audios, RenditionKey.GROUP_AUDIOS); addRepresentationItems(masterPlaylist.audios, RenditionKey.TYPE_AUDIO);
addRepresentationItems(masterPlaylist.subtitles, RenditionKey.GROUP_SUBTITLES); addRepresentationItems(masterPlaylist.subtitles, RenditionKey.TYPE_SUBTITLE);
} }
} }

View File

@ -40,6 +40,43 @@ public final class RepresentationKey implements Parcelable, Comparable<Represent
return periodIndex + "." + adaptationSetIndex + "." + representationIndex; return periodIndex + "." + adaptationSetIndex + "." + representationIndex;
} }
@Override
public boolean equals(@Nullable Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
RepresentationKey that = (RepresentationKey) o;
return periodIndex == that.periodIndex
&& adaptationSetIndex == that.adaptationSetIndex
&& representationIndex == that.representationIndex;
}
@Override
public int hashCode() {
int result = periodIndex;
result = 31 * result + adaptationSetIndex;
result = 31 * result + representationIndex;
return result;
}
// Comparable implementation.
@Override
public int compareTo(@NonNull RepresentationKey o) {
int result = periodIndex - o.periodIndex;
if (result == 0) {
result = adaptationSetIndex - o.adaptationSetIndex;
if (result == 0) {
result = representationIndex - o.representationIndex;
}
}
return result;
}
// Parcelable implementation. // Parcelable implementation.
@Override @Override
@ -67,41 +104,4 @@ public final class RepresentationKey implements Parcelable, Comparable<Represent
} }
}; };
// Comparable implementation.
@Override
public int compareTo(@NonNull RepresentationKey o) {
int result = periodIndex - o.periodIndex;
if (result == 0) {
result = adaptationSetIndex - o.adaptationSetIndex;
if (result == 0) {
result = representationIndex - o.representationIndex;
}
}
return result;
}
@Override
public boolean equals(@Nullable Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
RepresentationKey that = (RepresentationKey) o;
return periodIndex == that.periodIndex
&& adaptationSetIndex == that.adaptationSetIndex
&& representationIndex == that.representationIndex;
}
@Override
public int hashCode() {
int result = periodIndex;
result = 31 * result + adaptationSetIndex;
result = 31 * result + representationIndex;
return result;
}
} }

View File

@ -68,7 +68,7 @@ public final class HlsDownloadAction extends SegmentDownloadAction<RenditionKey>
@Override @Override
protected void writeKey(DataOutputStream output, RenditionKey key) throws IOException { protected void writeKey(DataOutputStream output, RenditionKey key) throws IOException {
output.writeInt(key.renditionGroup); output.writeInt(key.type);
output.writeInt(key.trackIndex); output.writeInt(key.trackIndex);
} }

View File

@ -119,9 +119,9 @@ public final class HlsMasterPlaylist extends HlsPlaylist<HlsMasterPlaylist> {
return new HlsMasterPlaylist( return new HlsMasterPlaylist(
baseUri, baseUri,
tags, tags,
copyRenditionsList(variants, RenditionKey.GROUP_VARIANTS, renditionKeys), copyRenditionsList(variants, RenditionKey.TYPE_VARIANT, renditionKeys),
copyRenditionsList(audios, RenditionKey.GROUP_AUDIOS, renditionKeys), copyRenditionsList(audios, RenditionKey.TYPE_AUDIO, renditionKeys),
copyRenditionsList(subtitles, RenditionKey.GROUP_SUBTITLES, renditionKeys), copyRenditionsList(subtitles, RenditionKey.TYPE_SUBTITLE, renditionKeys),
muxedAudioFormat, muxedAudioFormat,
muxedCaptionFormats); muxedCaptionFormats);
} }
@ -140,13 +140,13 @@ public final class HlsMasterPlaylist extends HlsPlaylist<HlsMasterPlaylist> {
} }
private static List<HlsUrl> copyRenditionsList( private static List<HlsUrl> copyRenditionsList(
List<HlsUrl> renditions, int renditionGroup, List<RenditionKey> renditionKeys) { List<HlsUrl> renditions, int renditionType, List<RenditionKey> renditionKeys) {
List<HlsUrl> copiedRenditions = new ArrayList<>(renditionKeys.size()); List<HlsUrl> copiedRenditions = new ArrayList<>(renditionKeys.size());
for (int i = 0; i < renditions.size(); i++) { for (int i = 0; i < renditions.size(); i++) {
HlsUrl rendition = renditions.get(i); HlsUrl rendition = renditions.get(i);
for (int j = 0; j < renditionKeys.size(); j++) { for (int j = 0; j < renditionKeys.size(); j++) {
RenditionKey renditionKey = renditionKeys.get(j); RenditionKey renditionKey = renditionKeys.get(j);
if (renditionKey.renditionGroup == renditionGroup && renditionKey.trackIndex == i) { if (renditionKey.type == renditionType && renditionKey.trackIndex == i) {
copiedRenditions.add(rendition); copiedRenditions.add(rendition);
break; break;
} }

View File

@ -17,23 +17,68 @@ package com.google.android.exoplayer2.source.hls.playlist;
import android.os.Parcel; import android.os.Parcel;
import android.os.Parcelable; import android.os.Parcelable;
import android.support.annotation.IntDef;
import android.support.annotation.NonNull; import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
/** Uniquely identifies a rendition in an {@link HlsMasterPlaylist}. */ /** Uniquely identifies a rendition in an {@link HlsMasterPlaylist}. */
public final class RenditionKey implements Parcelable, Comparable<RenditionKey> { public final class RenditionKey implements Parcelable, Comparable<RenditionKey> {
public static final int GROUP_VARIANTS = 0; /** Types of rendition. */
public static final int GROUP_AUDIOS = 1; @Retention(RetentionPolicy.SOURCE)
public static final int GROUP_SUBTITLES = 2; @IntDef({TYPE_VARIANT, TYPE_AUDIO, TYPE_SUBTITLE})
public @interface Type {}
public final int renditionGroup; public static final int TYPE_VARIANT = 0;
public static final int TYPE_AUDIO = 1;
public static final int TYPE_SUBTITLE = 2;
public final @Type int type;
public final int trackIndex; public final int trackIndex;
public RenditionKey(int renditionGroup, int trackIndex) { public RenditionKey(@Type int type, int trackIndex) {
this.renditionGroup = renditionGroup; this.type = type;
this.trackIndex = trackIndex; this.trackIndex = trackIndex;
} }
@Override
public String toString() {
return type + "." + trackIndex;
}
@Override
public boolean equals(@Nullable Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
RenditionKey that = (RenditionKey) o;
return type == that.type && trackIndex == that.trackIndex;
}
@Override
public int hashCode() {
int result = type;
result = 31 * result + trackIndex;
return result;
}
// Comparable implementation.
@Override
public int compareTo(@NonNull RenditionKey other) {
int result = type - other.type;
if (result == 0) {
result = trackIndex - other.trackIndex;
}
return result;
}
// Parcelable implementation. // Parcelable implementation.
@Override @Override
@ -43,7 +88,7 @@ public final class RenditionKey implements Parcelable, Comparable<RenditionKey>
@Override @Override
public void writeToParcel(Parcel dest, int flags) { public void writeToParcel(Parcel dest, int flags) {
dest.writeInt(renditionGroup); dest.writeInt(type);
dest.writeInt(trackIndex); dest.writeInt(trackIndex);
} }
@ -59,15 +104,4 @@ public final class RenditionKey implements Parcelable, Comparable<RenditionKey>
return new RenditionKey[size]; return new RenditionKey[size];
} }
}; };
// Comparable implementation.
@Override
public int compareTo(@NonNull RenditionKey other) {
int result = renditionGroup - other.renditionGroup;
if (result == 0) {
result = trackIndex - other.trackIndex;
}
return result;
}
} }

View File

@ -190,7 +190,7 @@ public class HlsDownloaderTest {
private static ArrayList<RenditionKey> getKeys(int... variantIndices) { private static ArrayList<RenditionKey> getKeys(int... variantIndices) {
ArrayList<RenditionKey> renditionKeys = new ArrayList<>(); ArrayList<RenditionKey> renditionKeys = new ArrayList<>();
for (int variantIndex : variantIndices) { for (int variantIndex : variantIndices) {
renditionKeys.add(new RenditionKey(RenditionKey.GROUP_VARIANTS, variantIndex)); renditionKeys.add(new RenditionKey(RenditionKey.TYPE_VARIANT, variantIndex));
} }
return renditionKeys; return renditionKeys;
} }

View File

@ -18,6 +18,7 @@ package com.google.android.exoplayer2.source.smoothstreaming.manifest;
import android.os.Parcel; import android.os.Parcel;
import android.os.Parcelable; import android.os.Parcelable;
import android.support.annotation.NonNull; import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
/** /**
* Uniquely identifies a track in a {@link SsManifest}. * Uniquely identifies a track in a {@link SsManifest}.
@ -37,6 +38,37 @@ public final class TrackKey implements Parcelable, Comparable<TrackKey> {
return streamElementIndex + "." + trackIndex; return streamElementIndex + "." + trackIndex;
} }
@Override
public boolean equals(@Nullable Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
TrackKey that = (TrackKey) o;
return streamElementIndex == that.streamElementIndex && trackIndex == that.trackIndex;
}
@Override
public int hashCode() {
int result = streamElementIndex;
result = 31 * result + trackIndex;
return result;
}
// Comparable implementation.
@Override
public int compareTo(@NonNull TrackKey o) {
int result = streamElementIndex - o.streamElementIndex;
if (result == 0) {
result = trackIndex - o.trackIndex;
}
return result;
}
// Parcelable implementation. // Parcelable implementation.
@Override @Override
@ -61,16 +93,4 @@ public final class TrackKey implements Parcelable, Comparable<TrackKey> {
return new TrackKey[size]; return new TrackKey[size];
} }
}; };
// Comparable implementation.
@Override
public int compareTo(@NonNull TrackKey o) {
int result = streamElementIndex - o.streamElementIndex;
if (result == 0) {
result = trackIndex - o.trackIndex;
}
return result;
}
} }