Commit ec3ba648 by olly Committed by Ian Baker

Simplify TracksInfo API

isTypeSupportedOrEmpty is very specific and a little hard to
understand unless you know the one thing it's useful for. This
commit replaces it with isTypeSupported, which can be used in
conjunction with the recently added containsType method.

PiperOrigin-RevId: 429312712
parent c7ea8114
...@@ -223,12 +223,12 @@ import java.util.ArrayList; ...@@ -223,12 +223,12 @@ import java.util.ArrayList;
if (currentPlayer != localPlayer || tracksInfo == lastSeenTrackGroupInfo) { if (currentPlayer != localPlayer || tracksInfo == lastSeenTrackGroupInfo) {
return; return;
} }
if (!tracksInfo.isTypeSupportedOrEmpty( if (tracksInfo.containsType(C.TRACK_TYPE_VIDEO)
C.TRACK_TYPE_VIDEO, /* allowExceedsCapabilities= */ true)) { && !tracksInfo.isTypeSupported(C.TRACK_TYPE_VIDEO, /* allowExceedsCapabilities= */ true)) {
listener.onUnsupportedTrack(C.TRACK_TYPE_VIDEO); listener.onUnsupportedTrack(C.TRACK_TYPE_VIDEO);
} }
if (!tracksInfo.isTypeSupportedOrEmpty( if (tracksInfo.containsType(C.TRACK_TYPE_AUDIO)
C.TRACK_TYPE_AUDIO, /* allowExceedsCapabilities= */ true)) { && !tracksInfo.isTypeSupported(C.TRACK_TYPE_AUDIO, /* allowExceedsCapabilities= */ true)) {
listener.onUnsupportedTrack(C.TRACK_TYPE_AUDIO); listener.onUnsupportedTrack(C.TRACK_TYPE_AUDIO);
} }
lastSeenTrackGroupInfo = tracksInfo; lastSeenTrackGroupInfo = tracksInfo;
......
...@@ -466,12 +466,14 @@ public class PlayerActivity extends AppCompatActivity ...@@ -466,12 +466,14 @@ public class PlayerActivity extends AppCompatActivity
if (tracksInfo == lastSeenTracksInfo) { if (tracksInfo == lastSeenTracksInfo) {
return; return;
} }
if (!tracksInfo.isTypeSupportedOrEmpty( if (tracksInfo.containsType(C.TRACK_TYPE_VIDEO)
C.TRACK_TYPE_VIDEO, /* allowExceedsCapabilities= */ true)) { && !tracksInfo.isTypeSupported(
C.TRACK_TYPE_VIDEO, /* allowExceedsCapabilities= */ true)) {
showToast(R.string.error_unsupported_video); showToast(R.string.error_unsupported_video);
} }
if (!tracksInfo.isTypeSupportedOrEmpty( if (tracksInfo.containsType(C.TRACK_TYPE_AUDIO)
C.TRACK_TYPE_AUDIO, /* allowExceedsCapabilities= */ true)) { && !tracksInfo.isTypeSupported(
C.TRACK_TYPE_AUDIO, /* allowExceedsCapabilities= */ true)) {
showToast(R.string.error_unsupported_audio); showToast(R.string.error_unsupported_audio);
} }
lastSeenTracksInfo = tracksInfo; lastSeenTracksInfo = tracksInfo;
......
...@@ -284,7 +284,7 @@ public final class TracksInfo implements Bundleable { ...@@ -284,7 +284,7 @@ public final class TracksInfo implements Bundleable {
} }
/** Returns true if there are tracks of type {@code trackType}, and false otherwise. */ /** Returns true if there are tracks of type {@code trackType}, and false otherwise. */
public boolean hasTracksOfType(@C.TrackType int trackType) { public boolean containsType(@C.TrackType int trackType) {
for (int i = 0; i < trackGroupInfos.size(); i++) { for (int i = 0; i < trackGroupInfos.size(); i++) {
if (trackGroupInfos.get(i).getTrackType() == trackType) { if (trackGroupInfos.get(i).getTrackType() == trackType) {
return true; return true;
...@@ -295,16 +295,15 @@ public final class TracksInfo implements Bundleable { ...@@ -295,16 +295,15 @@ public final class TracksInfo implements Bundleable {
/** /**
* Returns true if at least one track of type {@code trackType} is {@link * Returns true if at least one track of type {@code trackType} is {@link
* TrackGroupInfo#isTrackSupported(int) supported} or if there are no tracks of this type. * TrackGroupInfo#isTrackSupported(int) supported}.
*/ */
public boolean isTypeSupportedOrEmpty(@C.TrackType int trackType) { public boolean isTypeSupported(@C.TrackType int trackType) {
return isTypeSupportedOrEmpty(trackType, /* allowExceedsCapabilities= */ false); return isTypeSupported(trackType, /* allowExceedsCapabilities= */ false);
} }
/** /**
* Returns true if at least one track of type {@code trackType} is {@link * Returns true if at least one track of type {@code trackType} is {@link
* TrackGroupInfo#isTrackSupported(int, boolean) supported} or if there are no tracks of this * TrackGroupInfo#isTrackSupported(int, boolean) supported}.
* type.
* *
* @param allowExceedsCapabilities Whether to consider the track as supported if it has a * @param allowExceedsCapabilities Whether to consider the track as supported if it has a
* supported {@link Format#sampleMimeType MIME type}, but otherwise exceeds the advertised * supported {@link Format#sampleMimeType MIME type}, but otherwise exceeds the advertised
...@@ -312,19 +311,29 @@ public final class TracksInfo implements Bundleable { ...@@ -312,19 +311,29 @@ public final class TracksInfo implements Bundleable {
* decoder whose maximum advertised resolution is exceeded by the resolution of the track. * decoder whose maximum advertised resolution is exceeded by the resolution of the track.
* Such tracks may be playable in some cases. * Such tracks may be playable in some cases.
*/ */
public boolean isTypeSupportedOrEmpty( public boolean isTypeSupported(@C.TrackType int trackType, boolean allowExceedsCapabilities) {
@C.TrackType int trackType, boolean allowExceedsCapabilities) {
boolean supported = true;
for (int i = 0; i < trackGroupInfos.size(); i++) { for (int i = 0; i < trackGroupInfos.size(); i++) {
if (trackGroupInfos.get(i).getTrackType() == trackType) { if (trackGroupInfos.get(i).getTrackType() == trackType) {
if (trackGroupInfos.get(i).isSupported(allowExceedsCapabilities)) { if (trackGroupInfos.get(i).isSupported(allowExceedsCapabilities)) {
return true; return true;
} else {
supported = false;
} }
} }
} }
return supported; return false;
}
/** @deprecated Use {@link #containsType(int)} and {@link #isTypeSupported(int)}. */
@Deprecated
@SuppressWarnings("deprecation")
public boolean isTypeSupportedOrEmpty(@C.TrackType int trackType) {
return isTypeSupportedOrEmpty(trackType, /* allowExceedsCapabilities= */ false);
}
/** @deprecated Use {@link #containsType(int)} and {@link #isTypeSupported(int, boolean)}. */
@Deprecated
public boolean isTypeSupportedOrEmpty(
@C.TrackType int trackType, boolean allowExceedsCapabilities) {
return !containsType(trackType) || isTypeSupported(trackType, allowExceedsCapabilities);
} }
/** Returns true if at least one track of the type {@code trackType} is selected for playback. */ /** Returns true if at least one track of the type {@code trackType} is selected for playback. */
......
...@@ -62,12 +62,10 @@ public class TracksInfoTest { ...@@ -62,12 +62,10 @@ public class TracksInfoTest {
public void tracksInfoGetters_withoutTrack_returnExpectedValues() { public void tracksInfoGetters_withoutTrack_returnExpectedValues() {
TracksInfo tracksInfo = new TracksInfo(ImmutableList.of()); TracksInfo tracksInfo = new TracksInfo(ImmutableList.of());
assertThat(tracksInfo.hasTracksOfType(C.TRACK_TYPE_AUDIO)).isFalse(); assertThat(tracksInfo.containsType(C.TRACK_TYPE_AUDIO)).isFalse();
assertThat(tracksInfo.isTypeSupportedOrEmpty(C.TRACK_TYPE_AUDIO)).isTrue(); assertThat(tracksInfo.isTypeSupported(C.TRACK_TYPE_AUDIO)).isFalse();
assertThat( assertThat(tracksInfo.isTypeSupported(C.TRACK_TYPE_AUDIO, /* allowExceedsCapabilities= */ true))
tracksInfo.isTypeSupportedOrEmpty( .isFalse();
C.TRACK_TYPE_AUDIO, /* allowExceedsCapabilities= */ true))
.isTrue();
assertThat(tracksInfo.isTypeSelected(C.TRACK_TYPE_AUDIO)).isFalse(); assertThat(tracksInfo.isTypeSelected(C.TRACK_TYPE_AUDIO)).isFalse();
ImmutableList<TrackGroupInfo> trackGroupInfos = tracksInfo.getTrackGroupInfos(); ImmutableList<TrackGroupInfo> trackGroupInfos = tracksInfo.getTrackGroupInfos();
assertThat(trackGroupInfos).isEmpty(); assertThat(trackGroupInfos).isEmpty();
...@@ -99,24 +97,18 @@ public class TracksInfoTest { ...@@ -99,24 +97,18 @@ public class TracksInfoTest {
/* tracksSelected= */ new boolean[] {false, true}); /* tracksSelected= */ new boolean[] {false, true});
TracksInfo tracksInfo = new TracksInfo(ImmutableList.of(trackGroupInfo0, trackGroupInfo1)); TracksInfo tracksInfo = new TracksInfo(ImmutableList.of(trackGroupInfo0, trackGroupInfo1));
assertThat(tracksInfo.hasTracksOfType(C.TRACK_TYPE_AUDIO)).isTrue(); assertThat(tracksInfo.containsType(C.TRACK_TYPE_AUDIO)).isTrue();
assertThat(tracksInfo.hasTracksOfType(C.TRACK_TYPE_VIDEO)).isTrue(); assertThat(tracksInfo.containsType(C.TRACK_TYPE_VIDEO)).isTrue();
assertThat(tracksInfo.hasTracksOfType(C.TRACK_TYPE_TEXT)).isFalse(); assertThat(tracksInfo.containsType(C.TRACK_TYPE_TEXT)).isFalse();
assertThat(tracksInfo.isTypeSupportedOrEmpty(C.TRACK_TYPE_AUDIO)).isFalse(); assertThat(tracksInfo.isTypeSupported(C.TRACK_TYPE_AUDIO)).isFalse();
assertThat(tracksInfo.isTypeSupportedOrEmpty(C.TRACK_TYPE_VIDEO)).isTrue(); assertThat(tracksInfo.isTypeSupported(C.TRACK_TYPE_VIDEO)).isTrue();
assertThat(tracksInfo.isTypeSupportedOrEmpty(C.TRACK_TYPE_TEXT)).isTrue(); assertThat(tracksInfo.isTypeSupported(C.TRACK_TYPE_TEXT)).isFalse();
assertThat( assertThat(tracksInfo.isTypeSupported(C.TRACK_TYPE_AUDIO, /* allowExceedsCapabilities= */ true))
tracksInfo.isTypeSupportedOrEmpty(
C.TRACK_TYPE_AUDIO, /* allowExceedsCapabilities= */ true))
.isTrue();
assertThat(
tracksInfo.isTypeSupportedOrEmpty(
C.TRACK_TYPE_VIDEO, /* allowExceedsCapabilities= */ true))
.isTrue(); .isTrue();
assertThat( assertThat(tracksInfo.isTypeSupported(C.TRACK_TYPE_VIDEO, /* allowExceedsCapabilities= */ true))
tracksInfo.isTypeSupportedOrEmpty(
C.TRACK_TYPE_TEXT, /* allowExceedsCapabilities= */ true))
.isTrue(); .isTrue();
assertThat(tracksInfo.isTypeSupported(C.TRACK_TYPE_TEXT, /* allowExceedsCapabilities= */ true))
.isFalse();
assertThat(tracksInfo.isTypeSelected(C.TRACK_TYPE_AUDIO)).isFalse(); assertThat(tracksInfo.isTypeSelected(C.TRACK_TYPE_AUDIO)).isFalse();
assertThat(tracksInfo.isTypeSelected(C.TRACK_TYPE_VIDEO)).isTrue(); assertThat(tracksInfo.isTypeSelected(C.TRACK_TYPE_VIDEO)).isTrue();
ImmutableList<TrackGroupInfo> trackGroupInfos = tracksInfo.getTrackGroupInfos(); ImmutableList<TrackGroupInfo> trackGroupInfos = tracksInfo.getTrackGroupInfos();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment