Skip to content

Commit 6c98f23

Browse files
toniheitianyif
authored andcommitted
Add error messages to correctness assertions in SimpleBasePlayer
Users of this class may run into these assertions when creating the State and they need to check the source code to understand why the State is invalid. Adding error messages to all our correctness assertions helps to understand the root cause more easily. PiperOrigin-RevId: 496875109
1 parent a4c3038 commit 6c98f23

File tree

1 file changed

+64
-36
lines changed

1 file changed

+64
-36
lines changed

libraries/common/src/main/java/androidx/media3/common/SimpleBasePlayer.java

Lines changed: 64 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -549,7 +549,7 @@ public Builder setTimedMetadata(Metadata timedMetadata) {
549549
public Builder setPlaylist(List<MediaItemData> playlist) {
550550
HashSet<Object> uids = new HashSet<>();
551551
for (int i = 0; i < playlist.size(); i++) {
552-
checkArgument(uids.add(playlist.get(i).uid));
552+
checkArgument(uids.add(playlist.get(i).uid), "Duplicate MediaItemData UID in playlist");
553553
}
554554
this.playlist = ImmutableList.copyOf(playlist);
555555
this.timeline = new PlaylistTimeline(this.playlist);
@@ -882,16 +882,20 @@ private State(Builder builder) {
882882
if (builder.timeline.isEmpty()) {
883883
checkArgument(
884884
builder.playbackState == Player.STATE_IDLE
885-
|| builder.playbackState == Player.STATE_ENDED);
885+
|| builder.playbackState == Player.STATE_ENDED,
886+
"Empty playlist only allowed in STATE_IDLE or STATE_ENDED");
886887
checkArgument(
887888
builder.currentAdGroupIndex == C.INDEX_UNSET
888-
&& builder.currentAdIndexInAdGroup == C.INDEX_UNSET);
889+
&& builder.currentAdIndexInAdGroup == C.INDEX_UNSET,
890+
"Ads not allowed if playlist is empty");
889891
} else {
890892
int mediaItemIndex = builder.currentMediaItemIndex;
891893
if (mediaItemIndex == C.INDEX_UNSET) {
892894
mediaItemIndex = 0; // TODO: Use shuffle order to find first index.
893895
} else {
894-
checkArgument(builder.currentMediaItemIndex < builder.timeline.getWindowCount());
896+
checkArgument(
897+
builder.currentMediaItemIndex < builder.timeline.getWindowCount(),
898+
"currentMediaItemIndex must be less than playlist.size()");
895899
}
896900
if (builder.currentAdGroupIndex != C.INDEX_UNSET) {
897901
Timeline.Period period = new Timeline.Period();
@@ -904,19 +908,25 @@ private State(Builder builder) {
904908
getPeriodIndexFromWindowPosition(
905909
builder.timeline, mediaItemIndex, contentPositionMs, window, period);
906910
builder.timeline.getPeriod(periodIndex, period);
907-
checkArgument(builder.currentAdGroupIndex < period.getAdGroupCount());
911+
checkArgument(
912+
builder.currentAdGroupIndex < period.getAdGroupCount(),
913+
"PeriodData has less ad groups than adGroupIndex");
908914
int adCountInGroup = period.getAdCountInAdGroup(builder.currentAdGroupIndex);
909915
if (adCountInGroup != C.LENGTH_UNSET) {
910-
checkArgument(builder.currentAdIndexInAdGroup < adCountInGroup);
916+
checkArgument(
917+
builder.currentAdIndexInAdGroup < adCountInGroup,
918+
"Ad group has less ads than adIndexInGroupIndex");
911919
}
912920
}
913921
}
914922
if (builder.playerError != null) {
915-
checkArgument(builder.playbackState == Player.STATE_IDLE);
923+
checkArgument(
924+
builder.playbackState == Player.STATE_IDLE, "Player error only allowed in STATE_IDLE");
916925
}
917926
if (builder.playbackState == Player.STATE_IDLE
918927
|| builder.playbackState == Player.STATE_ENDED) {
919-
checkArgument(!builder.isLoading);
928+
checkArgument(
929+
!builder.isLoading, "isLoading only allowed when not in STATE_IDLE or STATE_ENDED");
920930
}
921931
PositionSupplier contentPositionMsSupplier = builder.contentPositionMsSupplier;
922932
if (builder.contentPositionMs != null) {
@@ -1494,9 +1504,12 @@ public Builder setIsPlaceholder(boolean isPlaceholder) {
14941504
public Builder setPeriods(List<PeriodData> periods) {
14951505
int periodCount = periods.size();
14961506
for (int i = 0; i < periodCount - 1; i++) {
1497-
checkArgument(periods.get(i).durationUs != C.TIME_UNSET);
1507+
checkArgument(
1508+
periods.get(i).durationUs != C.TIME_UNSET, "Periods other than last need a duration");
14981509
for (int j = i + 1; j < periodCount; j++) {
1499-
checkArgument(!periods.get(i).uid.equals(periods.get(j).uid));
1510+
checkArgument(
1511+
!periods.get(i).uid.equals(periods.get(j).uid),
1512+
"Duplicate PeriodData UIDs in period list");
15001513
}
15011514
}
15021515
this.periods = ImmutableList.copyOf(periods);
@@ -1575,16 +1588,26 @@ public MediaItemData build() {
15751588

15761589
private MediaItemData(Builder builder) {
15771590
if (builder.liveConfiguration == null) {
1578-
checkArgument(builder.presentationStartTimeMs == C.TIME_UNSET);
1579-
checkArgument(builder.windowStartTimeMs == C.TIME_UNSET);
1580-
checkArgument(builder.elapsedRealtimeEpochOffsetMs == C.TIME_UNSET);
1591+
checkArgument(
1592+
builder.presentationStartTimeMs == C.TIME_UNSET,
1593+
"presentationStartTimeMs can only be set if liveConfiguration != null");
1594+
checkArgument(
1595+
builder.windowStartTimeMs == C.TIME_UNSET,
1596+
"windowStartTimeMs can only be set if liveConfiguration != null");
1597+
checkArgument(
1598+
builder.elapsedRealtimeEpochOffsetMs == C.TIME_UNSET,
1599+
"elapsedRealtimeEpochOffsetMs can only be set if liveConfiguration != null");
15811600
} else if (builder.presentationStartTimeMs != C.TIME_UNSET
15821601
&& builder.windowStartTimeMs != C.TIME_UNSET) {
1583-
checkArgument(builder.windowStartTimeMs >= builder.presentationStartTimeMs);
1602+
checkArgument(
1603+
builder.windowStartTimeMs >= builder.presentationStartTimeMs,
1604+
"windowStartTimeMs can't be less than presentationStartTimeMs");
15841605
}
15851606
int periodCount = builder.periods.size();
15861607
if (builder.durationUs != C.TIME_UNSET) {
1587-
checkArgument(builder.defaultPositionUs <= builder.durationUs);
1608+
checkArgument(
1609+
builder.defaultPositionUs <= builder.durationUs,
1610+
"defaultPositionUs can't be greater than durationUs");
15881611
}
15891612
this.uid = builder.uid;
15901613
this.tracks = builder.tracks;
@@ -2782,7 +2805,7 @@ protected MediaItemData getPlaceholderMediaItemData(MediaItem mediaItem) {
27822805
*/
27832806
@ForOverride
27842807
protected ListenableFuture<?> handleSetPlayWhenReady(boolean playWhenReady) {
2785-
throw new IllegalStateException();
2808+
throw new IllegalStateException("Missing implementation to handle COMMAND_PLAY_PAUSE");
27862809
}
27872810

27882811
/**
@@ -2795,7 +2818,7 @@ protected ListenableFuture<?> handleSetPlayWhenReady(boolean playWhenReady) {
27952818
*/
27962819
@ForOverride
27972820
protected ListenableFuture<?> handlePrepare() {
2798-
throw new IllegalStateException();
2821+
throw new IllegalStateException("Missing implementation to handle COMMAND_PREPARE");
27992822
}
28002823

28012824
/**
@@ -2808,7 +2831,7 @@ protected ListenableFuture<?> handlePrepare() {
28082831
*/
28092832
@ForOverride
28102833
protected ListenableFuture<?> handleStop() {
2811-
throw new IllegalStateException();
2834+
throw new IllegalStateException("Missing implementation to handle COMMAND_STOP");
28122835
}
28132836

28142837
/**
@@ -2820,7 +2843,7 @@ protected ListenableFuture<?> handleStop() {
28202843
// TODO(b/261158047): Add that this method will only be called if COMMAND_RELEASE is available.
28212844
@ForOverride
28222845
protected ListenableFuture<?> handleRelease() {
2823-
throw new IllegalStateException();
2846+
throw new IllegalStateException("Missing implementation to handle COMMAND_RELEASE");
28242847
}
28252848

28262849
/**
@@ -2834,7 +2857,7 @@ protected ListenableFuture<?> handleRelease() {
28342857
*/
28352858
@ForOverride
28362859
protected ListenableFuture<?> handleSetRepeatMode(@RepeatMode int repeatMode) {
2837-
throw new IllegalStateException();
2860+
throw new IllegalStateException("Missing implementation to handle COMMAND_SET_REPEAT_MODE");
28382861
}
28392862

28402863
/**
@@ -2848,7 +2871,7 @@ protected ListenableFuture<?> handleSetRepeatMode(@RepeatMode int repeatMode) {
28482871
*/
28492872
@ForOverride
28502873
protected ListenableFuture<?> handleSetShuffleModeEnabled(boolean shuffleModeEnabled) {
2851-
throw new IllegalStateException();
2874+
throw new IllegalStateException("Missing implementation to handle COMMAND_SET_SHUFFLE_MODE");
28522875
}
28532876

28542877
/**
@@ -2862,7 +2885,7 @@ protected ListenableFuture<?> handleSetShuffleModeEnabled(boolean shuffleModeEna
28622885
*/
28632886
@ForOverride
28642887
protected ListenableFuture<?> handleSetPlaybackParameters(PlaybackParameters playbackParameters) {
2865-
throw new IllegalStateException();
2888+
throw new IllegalStateException("Missing implementation to handle COMMAND_SET_SPEED_AND_PITCH");
28662889
}
28672890

28682891
/**
@@ -2877,7 +2900,8 @@ protected ListenableFuture<?> handleSetPlaybackParameters(PlaybackParameters pla
28772900
@ForOverride
28782901
protected ListenableFuture<?> handleSetTrackSelectionParameters(
28792902
TrackSelectionParameters trackSelectionParameters) {
2880-
throw new IllegalStateException();
2903+
throw new IllegalStateException(
2904+
"Missing implementation to handle COMMAND_SET_TRACK_SELECTION_PARAMETERS");
28812905
}
28822906

28832907
/**
@@ -2891,7 +2915,8 @@ protected ListenableFuture<?> handleSetTrackSelectionParameters(
28912915
*/
28922916
@ForOverride
28932917
protected ListenableFuture<?> handleSetPlaylistMetadata(MediaMetadata playlistMetadata) {
2894-
throw new IllegalStateException();
2918+
throw new IllegalStateException(
2919+
"Missing implementation to handle COMMAND_SET_MEDIA_ITEMS_METADATA");
28952920
}
28962921

28972922
/**
@@ -2906,7 +2931,7 @@ protected ListenableFuture<?> handleSetPlaylistMetadata(MediaMetadata playlistMe
29062931
*/
29072932
@ForOverride
29082933
protected ListenableFuture<?> handleSetVolume(@FloatRange(from = 0, to = 1.0) float volume) {
2909-
throw new IllegalStateException();
2934+
throw new IllegalStateException("Missing implementation to handle COMMAND_SET_VOLUME");
29102935
}
29112936

29122937
/**
@@ -2920,7 +2945,7 @@ protected ListenableFuture<?> handleSetVolume(@FloatRange(from = 0, to = 1.0) fl
29202945
*/
29212946
@ForOverride
29222947
protected ListenableFuture<?> handleSetDeviceVolume(@IntRange(from = 0) int deviceVolume) {
2923-
throw new IllegalStateException();
2948+
throw new IllegalStateException("Missing implementation to handle COMMAND_SET_DEVICE_VOLUME");
29242949
}
29252950

29262951
/**
@@ -2933,7 +2958,8 @@ protected ListenableFuture<?> handleSetDeviceVolume(@IntRange(from = 0) int devi
29332958
*/
29342959
@ForOverride
29352960
protected ListenableFuture<?> handleIncreaseDeviceVolume() {
2936-
throw new IllegalStateException();
2961+
throw new IllegalStateException(
2962+
"Missing implementation to handle COMMAND_ADJUST_DEVICE_VOLUME");
29372963
}
29382964

29392965
/**
@@ -2946,7 +2972,8 @@ protected ListenableFuture<?> handleIncreaseDeviceVolume() {
29462972
*/
29472973
@ForOverride
29482974
protected ListenableFuture<?> handleDecreaseDeviceVolume() {
2949-
throw new IllegalStateException();
2975+
throw new IllegalStateException(
2976+
"Missing implementation to handle COMMAND_ADJUST_DEVICE_VOLUME");
29502977
}
29512978

29522979
/**
@@ -2960,7 +2987,8 @@ protected ListenableFuture<?> handleDecreaseDeviceVolume() {
29602987
*/
29612988
@ForOverride
29622989
protected ListenableFuture<?> handleSetDeviceMuted(boolean muted) {
2963-
throw new IllegalStateException();
2990+
throw new IllegalStateException(
2991+
"Missing implementation to handle COMMAND_ADJUST_DEVICE_VOLUME");
29642992
}
29652993

29662994
/**
@@ -2975,7 +3003,7 @@ protected ListenableFuture<?> handleSetDeviceMuted(boolean muted) {
29753003
*/
29763004
@ForOverride
29773005
protected ListenableFuture<?> handleSetVideoOutput(Object videoOutput) {
2978-
throw new IllegalStateException();
3006+
throw new IllegalStateException("Missing implementation to handle COMMAND_SET_VIDEO_SURFACE");
29793007
}
29803008

29813009
/**
@@ -2992,7 +3020,7 @@ protected ListenableFuture<?> handleSetVideoOutput(Object videoOutput) {
29923020
*/
29933021
@ForOverride
29943022
protected ListenableFuture<?> handleClearVideoOutput(@Nullable Object videoOutput) {
2995-
throw new IllegalStateException();
3023+
throw new IllegalStateException("Missing implementation to handle COMMAND_SET_VIDEO_SURFACE");
29963024
}
29973025

29983026
/**
@@ -3013,7 +3041,7 @@ protected ListenableFuture<?> handleClearVideoOutput(@Nullable Object videoOutpu
30133041
@ForOverride
30143042
protected ListenableFuture<?> handleSetMediaItems(
30153043
List<MediaItem> mediaItems, int startIndex, long startPositionMs) {
3016-
throw new IllegalStateException();
3044+
throw new IllegalStateException("Missing implementation to handle COMMAND_SET_MEDIA_ITEM(S)");
30173045
}
30183046

30193047
/**
@@ -3029,7 +3057,7 @@ protected ListenableFuture<?> handleSetMediaItems(
30293057
*/
30303058
@ForOverride
30313059
protected ListenableFuture<?> handleAddMediaItems(int index, List<MediaItem> mediaItems) {
3032-
throw new IllegalStateException();
3060+
throw new IllegalStateException("Missing implementation to handle COMMAND_CHANGE_MEDIA_ITEMS");
30333061
}
30343062

30353063
/**
@@ -3049,7 +3077,7 @@ protected ListenableFuture<?> handleAddMediaItems(int index, List<MediaItem> med
30493077
*/
30503078
@ForOverride
30513079
protected ListenableFuture<?> handleMoveMediaItems(int fromIndex, int toIndex, int newIndex) {
3052-
throw new IllegalStateException();
3080+
throw new IllegalStateException("Missing implementation to handle COMMAND_CHANGE_MEDIA_ITEMS");
30533081
}
30543082

30553083
/**
@@ -3066,7 +3094,7 @@ protected ListenableFuture<?> handleMoveMediaItems(int fromIndex, int toIndex, i
30663094
*/
30673095
@ForOverride
30683096
protected ListenableFuture<?> handleRemoveMediaItems(int fromIndex, int toIndex) {
3069-
throw new IllegalStateException();
3097+
throw new IllegalStateException("Missing implementation to handle COMMAND_CHANGE_MEDIA_ITEMS");
30703098
}
30713099

30723100
/**
@@ -3087,7 +3115,7 @@ protected ListenableFuture<?> handleRemoveMediaItems(int fromIndex, int toIndex)
30873115
@ForOverride
30883116
protected ListenableFuture<?> handleSeek(
30893117
int mediaItemIndex, long positionMs, @Player.Command int seekCommand) {
3090-
throw new IllegalStateException();
3118+
throw new IllegalStateException("Missing implementation to handle one of the COMMAND_SEEK_*");
30913119
}
30923120

30933121
@RequiresNonNull("state")

0 commit comments

Comments
 (0)