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
This commit is contained in:
parent
265dd079e9
commit
f22025cc5e
@ -223,12 +223,12 @@ import java.util.ArrayList;
|
||||
if (currentPlayer != localPlayer || tracksInfo == lastSeenTrackGroupInfo) {
|
||||
return;
|
||||
}
|
||||
if (!tracksInfo.isTypeSupportedOrEmpty(
|
||||
C.TRACK_TYPE_VIDEO, /* allowExceedsCapabilities= */ true)) {
|
||||
if (tracksInfo.containsType(C.TRACK_TYPE_VIDEO)
|
||||
&& !tracksInfo.isTypeSupported(C.TRACK_TYPE_VIDEO, /* allowExceedsCapabilities= */ true)) {
|
||||
listener.onUnsupportedTrack(C.TRACK_TYPE_VIDEO);
|
||||
}
|
||||
if (!tracksInfo.isTypeSupportedOrEmpty(
|
||||
C.TRACK_TYPE_AUDIO, /* allowExceedsCapabilities= */ true)) {
|
||||
if (tracksInfo.containsType(C.TRACK_TYPE_AUDIO)
|
||||
&& !tracksInfo.isTypeSupported(C.TRACK_TYPE_AUDIO, /* allowExceedsCapabilities= */ true)) {
|
||||
listener.onUnsupportedTrack(C.TRACK_TYPE_AUDIO);
|
||||
}
|
||||
lastSeenTrackGroupInfo = tracksInfo;
|
||||
|
@ -466,12 +466,14 @@ public class PlayerActivity extends AppCompatActivity
|
||||
if (tracksInfo == lastSeenTracksInfo) {
|
||||
return;
|
||||
}
|
||||
if (!tracksInfo.isTypeSupportedOrEmpty(
|
||||
C.TRACK_TYPE_VIDEO, /* allowExceedsCapabilities= */ true)) {
|
||||
if (tracksInfo.containsType(C.TRACK_TYPE_VIDEO)
|
||||
&& !tracksInfo.isTypeSupported(
|
||||
C.TRACK_TYPE_VIDEO, /* allowExceedsCapabilities= */ true)) {
|
||||
showToast(R.string.error_unsupported_video);
|
||||
}
|
||||
if (!tracksInfo.isTypeSupportedOrEmpty(
|
||||
C.TRACK_TYPE_AUDIO, /* allowExceedsCapabilities= */ true)) {
|
||||
if (tracksInfo.containsType(C.TRACK_TYPE_AUDIO)
|
||||
&& !tracksInfo.isTypeSupported(
|
||||
C.TRACK_TYPE_AUDIO, /* allowExceedsCapabilities= */ true)) {
|
||||
showToast(R.string.error_unsupported_audio);
|
||||
}
|
||||
lastSeenTracksInfo = tracksInfo;
|
||||
|
@ -288,7 +288,7 @@ public final class TracksInfo implements Bundleable {
|
||||
}
|
||||
|
||||
/** 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++) {
|
||||
if (trackGroupInfos.get(i).getTrackType() == trackType) {
|
||||
return true;
|
||||
@ -299,16 +299,15 @@ public final class TracksInfo implements Bundleable {
|
||||
|
||||
/**
|
||||
* 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) {
|
||||
return isTypeSupportedOrEmpty(trackType, /* allowExceedsCapabilities= */ false);
|
||||
public boolean isTypeSupported(@C.TrackType int trackType) {
|
||||
return isTypeSupported(trackType, /* allowExceedsCapabilities= */ false);
|
||||
}
|
||||
|
||||
/**
|
||||
* 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
|
||||
* type.
|
||||
* TrackGroupInfo#isTrackSupported(int, boolean) supported}.
|
||||
*
|
||||
* @param allowExceedsCapabilities Whether to consider the track as supported if it has a
|
||||
* supported {@link Format#sampleMimeType MIME type}, but otherwise exceeds the advertised
|
||||
@ -316,19 +315,31 @@ public final class TracksInfo implements Bundleable {
|
||||
* decoder whose maximum advertised resolution is exceeded by the resolution of the track.
|
||||
* Such tracks may be playable in some cases.
|
||||
*/
|
||||
public boolean isTypeSupportedOrEmpty(
|
||||
@C.TrackType int trackType, boolean allowExceedsCapabilities) {
|
||||
boolean supported = true;
|
||||
public boolean isTypeSupported(@C.TrackType int trackType, boolean allowExceedsCapabilities) {
|
||||
for (int i = 0; i < trackGroupInfos.size(); i++) {
|
||||
if (trackGroupInfos.get(i).getTrackType() == trackType) {
|
||||
if (trackGroupInfos.get(i).isSupported(allowExceedsCapabilities)) {
|
||||
return true;
|
||||
} else {
|
||||
supported = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
return supported;
|
||||
return false;
|
||||
}
|
||||
|
||||
/** @deprecated Use {@link #containsType(int)} and {@link #isTypeSupported(int)}. */
|
||||
@Deprecated
|
||||
@UnstableApi
|
||||
@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
|
||||
@UnstableApi
|
||||
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. */
|
||||
|
@ -61,12 +61,10 @@ public class TracksInfoTest {
|
||||
public void tracksInfoGetters_withoutTrack_returnExpectedValues() {
|
||||
TracksInfo tracksInfo = new TracksInfo(ImmutableList.of());
|
||||
|
||||
assertThat(tracksInfo.hasTracksOfType(C.TRACK_TYPE_AUDIO)).isFalse();
|
||||
assertThat(tracksInfo.isTypeSupportedOrEmpty(C.TRACK_TYPE_AUDIO)).isTrue();
|
||||
assertThat(
|
||||
tracksInfo.isTypeSupportedOrEmpty(
|
||||
C.TRACK_TYPE_AUDIO, /* allowExceedsCapabilities= */ true))
|
||||
.isTrue();
|
||||
assertThat(tracksInfo.containsType(C.TRACK_TYPE_AUDIO)).isFalse();
|
||||
assertThat(tracksInfo.isTypeSupported(C.TRACK_TYPE_AUDIO)).isFalse();
|
||||
assertThat(tracksInfo.isTypeSupported(C.TRACK_TYPE_AUDIO, /* allowExceedsCapabilities= */ true))
|
||||
.isFalse();
|
||||
assertThat(tracksInfo.isTypeSelected(C.TRACK_TYPE_AUDIO)).isFalse();
|
||||
ImmutableList<TrackGroupInfo> trackGroupInfos = tracksInfo.getTrackGroupInfos();
|
||||
assertThat(trackGroupInfos).isEmpty();
|
||||
@ -98,24 +96,18 @@ public class TracksInfoTest {
|
||||
/* tracksSelected= */ new boolean[] {false, true});
|
||||
TracksInfo tracksInfo = new TracksInfo(ImmutableList.of(trackGroupInfo0, trackGroupInfo1));
|
||||
|
||||
assertThat(tracksInfo.hasTracksOfType(C.TRACK_TYPE_AUDIO)).isTrue();
|
||||
assertThat(tracksInfo.hasTracksOfType(C.TRACK_TYPE_VIDEO)).isTrue();
|
||||
assertThat(tracksInfo.hasTracksOfType(C.TRACK_TYPE_TEXT)).isFalse();
|
||||
assertThat(tracksInfo.isTypeSupportedOrEmpty(C.TRACK_TYPE_AUDIO)).isFalse();
|
||||
assertThat(tracksInfo.isTypeSupportedOrEmpty(C.TRACK_TYPE_VIDEO)).isTrue();
|
||||
assertThat(tracksInfo.isTypeSupportedOrEmpty(C.TRACK_TYPE_TEXT)).isTrue();
|
||||
assertThat(
|
||||
tracksInfo.isTypeSupportedOrEmpty(
|
||||
C.TRACK_TYPE_AUDIO, /* allowExceedsCapabilities= */ true))
|
||||
assertThat(tracksInfo.containsType(C.TRACK_TYPE_AUDIO)).isTrue();
|
||||
assertThat(tracksInfo.containsType(C.TRACK_TYPE_VIDEO)).isTrue();
|
||||
assertThat(tracksInfo.containsType(C.TRACK_TYPE_TEXT)).isFalse();
|
||||
assertThat(tracksInfo.isTypeSupported(C.TRACK_TYPE_AUDIO)).isFalse();
|
||||
assertThat(tracksInfo.isTypeSupported(C.TRACK_TYPE_VIDEO)).isTrue();
|
||||
assertThat(tracksInfo.isTypeSupported(C.TRACK_TYPE_TEXT)).isFalse();
|
||||
assertThat(tracksInfo.isTypeSupported(C.TRACK_TYPE_AUDIO, /* allowExceedsCapabilities= */ true))
|
||||
.isTrue();
|
||||
assertThat(
|
||||
tracksInfo.isTypeSupportedOrEmpty(
|
||||
C.TRACK_TYPE_VIDEO, /* allowExceedsCapabilities= */ true))
|
||||
.isTrue();
|
||||
assertThat(
|
||||
tracksInfo.isTypeSupportedOrEmpty(
|
||||
C.TRACK_TYPE_TEXT, /* allowExceedsCapabilities= */ true))
|
||||
assertThat(tracksInfo.isTypeSupported(C.TRACK_TYPE_VIDEO, /* allowExceedsCapabilities= */ true))
|
||||
.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_VIDEO)).isTrue();
|
||||
ImmutableList<TrackGroupInfo> trackGroupInfos = tracksInfo.getTrackGroupInfos();
|
||||
|
Loading…
x
Reference in New Issue
Block a user