Skip to content

Commit

Permalink
Split media1/media3 conversion methods out of MediaUtils
Browse files Browse the repository at this point in the history
Android Studio removed some nested imports, but I think the extra
qualification at the usage site is actually mostly helpful, so I'm
leaving it as-is.

PiperOrigin-RevId: 578518880
  • Loading branch information
icbaker authored and copybara-github committed Nov 1, 2023
1 parent 27caeb8 commit 72b7019
Show file tree
Hide file tree
Showing 20 changed files with 2,564 additions and 2,450 deletions.
1,366 changes: 1,366 additions & 0 deletions libraries/session/src/main/java/androidx/media3/session/LegacyConversions.java

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ public ListenableFuture<LibraryResult<MediaItem>> getLibraryRoot(@Nullable Libra
// Already connected with the given extras.
result.set(LibraryResult.ofItem(createRootMediaItem(browserCompat), null));
} else {
Bundle rootHints = MediaUtils.convertToRootHints(params);
Bundle rootHints = LegacyConversions.convertToRootHints(params);
MediaBrowserCompat newBrowser =
new MediaBrowserCompat(
getContext(),
Expand Down Expand Up @@ -193,7 +193,8 @@ public ListenableFuture<LibraryResult<MediaItem>> getItem(String mediaId) {
public void onItemLoaded(MediaBrowserCompat.MediaItem item) {
if (item != null) {
result.set(
LibraryResult.ofItem(MediaUtils.convertToMediaItem(item), /* params= */ null));
LibraryResult.ofItem(
LegacyConversions.convertToMediaItem(item), /* params= */ null));
} else {
result.set(LibraryResult.ofError(RESULT_ERROR_BAD_VALUE));
}
Expand Down Expand Up @@ -278,7 +279,8 @@ public void onSearchResult(
String query, Bundle extrasSent, List<MediaBrowserCompat.MediaItem> items) {
future.set(
LibraryResult.ofItemList(
MediaUtils.convertBrowserItemListToMediaItemList(items), /* params= */ null));
LegacyConversions.convertBrowserItemListToMediaItemList(items),
/* params= */ null));
}

@Override
Expand Down Expand Up @@ -342,7 +344,7 @@ public void onConnected() {
result.set(
LibraryResult.ofItem(
createRootMediaItem(browserCompat),
MediaUtils.convertToLibraryParams(context, browserCompat.getExtras())));
LegacyConversions.convertToLibraryParams(context, browserCompat.getExtras())));
}
}

Expand Down Expand Up @@ -414,7 +416,7 @@ private void onChildrenLoadedInternal(
}

LibraryParams params =
MediaUtils.convertToLibraryParams(
LegacyConversions.convertToLibraryParams(
context, browserCompat.getNotifyChildrenChangedOptions());
getInstance()
.notifyBrowserListener(
Expand Down Expand Up @@ -486,7 +488,8 @@ private void onChildrenLoadedInternal(
// - New API: Extra from MediaLibraryService to MediaBrowser
future.set(
LibraryResult.ofItemList(
MediaUtils.convertBrowserItemListToMediaItemList(children), /* params= */ null));
LegacyConversions.convertBrowserItemListToMediaItemList(children),
/* params= */ null));
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,7 @@ private void dispatchRemoteSessionTaskWithPlayerCommandAndWaitForFuture(RemoteSe
ListenableFuture<SessionResult> future =
dispatchRemoteSessionTask(iSession, task, /* addToPendingMaskingOperations= */ true);
try {
MediaUtils.getFutureResult(future, /* timeoutMs= */ 3_000);
LegacyConversions.getFutureResult(future, /* timeoutMs= */ 3_000);
} catch (ExecutionException e) {
// Never happens because future.setException will not be called.
throw new IllegalStateException(e);
Expand Down Expand Up @@ -1937,8 +1937,8 @@ private void setMediaItemsInternal(
List<Window> windows = new ArrayList<>();
List<Period> periods = new ArrayList<>();
for (int i = 0; i < mediaItems.size(); i++) {
windows.add(MediaUtils.convertToWindow(mediaItems.get(i), i));
periods.add(MediaUtils.convertToPeriod(i));
windows.add(LegacyConversions.convertToWindow(mediaItems.get(i), i));
periods.add(LegacyConversions.convertToPeriod(i));
}

Timeline newTimeline = createMaskingTimeline(windows, periods);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -503,14 +503,18 @@ public ListenableFuture<SessionResult> setRating(String mediaId, Rating rating)
String currentMediaItemMediaId =
legacyPlayerInfo.mediaMetadataCompat.getString(MediaMetadataCompat.METADATA_KEY_MEDIA_ID);
if (mediaId.equals(currentMediaItemMediaId)) {
controllerCompat.getTransportControls().setRating(MediaUtils.convertToRatingCompat(rating));
controllerCompat
.getTransportControls()
.setRating(LegacyConversions.convertToRatingCompat(rating));
}
return Futures.immediateFuture(new SessionResult(SessionResult.RESULT_SUCCESS));
}

@Override
public ListenableFuture<SessionResult> setRating(Rating rating) {
controllerCompat.getTransportControls().setRating(MediaUtils.convertToRatingCompat(rating));
controllerCompat
.getTransportControls()
.setRating(LegacyConversions.convertToRatingCompat(rating));
return Futures.immediateFuture(new SessionResult(SessionResult.RESULT_SUCCESS));
}

Expand Down Expand Up @@ -936,7 +940,7 @@ public void setRepeatMode(@Player.RepeatMode int repeatMode) {

controllerCompat
.getTransportControls()
.setRepeatMode(MediaUtils.convertToPlaybackStateCompatRepeatMode(repeatMode));
.setRepeatMode(LegacyConversions.convertToPlaybackStateCompatRepeatMode(repeatMode));
}

@Override
Expand All @@ -962,7 +966,8 @@ public void setShuffleModeEnabled(boolean shuffleModeEnabled) {

controllerCompat
.getTransportControls()
.setShuffleMode(MediaUtils.convertToPlaybackStateCompatShuffleMode(shuffleModeEnabled));
.setShuffleMode(
LegacyConversions.convertToPlaybackStateCompatShuffleMode(shuffleModeEnabled));
}

@Override
Expand Down Expand Up @@ -1469,7 +1474,7 @@ private void handleBitmapFuturesAllCompletedAndAddQueueItems(
}
}
controllerCompat.addQueueItem(
MediaUtils.convertToMediaDescriptionCompat(mediaItems.get(i), bitmap),
LegacyConversions.convertToMediaDescriptionCompat(mediaItems.get(i), bitmap),
/* index= */ startIndex + i);
}
}
Expand Down Expand Up @@ -1584,7 +1589,7 @@ private void updateControllerInfo(
if (!MediaUtils.areEqualError(
oldLegacyPlayerInfo.playbackStateCompat, newLegacyPlayerInfo.playbackStateCompat)) {
PlaybackException error =
MediaUtils.convertToPlaybackException(newLegacyPlayerInfo.playbackStateCompat);
LegacyConversions.convertToPlaybackException(newLegacyPlayerInfo.playbackStateCompat);
listeners.queueEvent(
Player.EVENT_PLAYER_ERROR, (listener) -> listener.onPlayerErrorChanged(error));
if (error != null) {
Expand Down Expand Up @@ -1899,18 +1904,20 @@ private static ControllerInfo buildNewControllerInfo(
long oldActiveQueueId = getActiveQueueId(oldLegacyPlayerInfo.playbackStateCompat);
long newActiveQueueId = getActiveQueueId(newLegacyPlayerInfo.playbackStateCompat);
boolean isCurrentActiveQueueIdChanged = (oldActiveQueueId != newActiveQueueId) || initialUpdate;
long durationMs = MediaUtils.convertToDurationMs(newLegacyPlayerInfo.mediaMetadataCompat);
long durationMs =
LegacyConversions.convertToDurationMs(newLegacyPlayerInfo.mediaMetadataCompat);
if (isMetadataCompatChanged || isCurrentActiveQueueIdChanged || isQueueChanged) {
currentMediaItemIndex = findQueueItemIndex(newLegacyPlayerInfo.queue, newActiveQueueId);
boolean hasMediaMetadataCompat = newLegacyPlayerInfo.mediaMetadataCompat != null;
if (hasMediaMetadataCompat && isMetadataCompatChanged) {
mediaMetadata =
MediaUtils.convertToMediaMetadata(newLegacyPlayerInfo.mediaMetadataCompat, ratingType);
LegacyConversions.convertToMediaMetadata(
newLegacyPlayerInfo.mediaMetadataCompat, ratingType);
} else if (!hasMediaMetadataCompat && isCurrentActiveQueueIdChanged) {
mediaMetadata =
(currentMediaItemIndex == C.INDEX_UNSET)
? MediaMetadata.EMPTY
: MediaUtils.convertToMediaMetadata(
: LegacyConversions.convertToMediaMetadata(
newLegacyPlayerInfo.queue.get(currentMediaItemIndex).getDescription(),
ratingType);
} else {
Expand All @@ -1924,7 +1931,8 @@ private static ControllerInfo buildNewControllerInfo(
+ " the active queue id and current Timeline should have currently playing"
+ " MediaItem.");
MediaItem fakeMediaItem =
MediaUtils.convertToMediaItem(newLegacyPlayerInfo.mediaMetadataCompat, ratingType);
LegacyConversions.convertToMediaItem(
newLegacyPlayerInfo.mediaMetadataCompat, ratingType);
currentTimeline = currentTimeline.copyWithFakeMediaItem(fakeMediaItem, durationMs);
currentMediaItemIndex = currentTimeline.getWindowCount() - 1;
} else {
Expand All @@ -1938,7 +1946,7 @@ private static ControllerInfo buildNewControllerInfo(
currentTimeline = currentTimeline.copyWithClearedFakeMediaItem();
if (hasMediaMetadataCompat) {
MediaItem mediaItem =
MediaUtils.convertToMediaItem(
LegacyConversions.convertToMediaItem(
checkNotNull(currentTimeline.getMediaItemAt(currentMediaItemIndex)).mediaId,
newLegacyPlayerInfo.mediaMetadataCompat,
ratingType);
Expand All @@ -1960,14 +1968,16 @@ private static ControllerInfo buildNewControllerInfo(
playlistMetadata =
oldLegacyPlayerInfo.queueTitle == newLegacyPlayerInfo.queueTitle
? oldControllerInfo.playerInfo.playlistMetadata
: MediaUtils.convertToMediaMetadata(newLegacyPlayerInfo.queueTitle);
repeatMode = MediaUtils.convertToRepeatMode(newLegacyPlayerInfo.repeatMode);
shuffleModeEnabled = MediaUtils.convertToShuffleModeEnabled(newLegacyPlayerInfo.shuffleMode);
: LegacyConversions.convertToMediaMetadata(newLegacyPlayerInfo.queueTitle);
repeatMode = LegacyConversions.convertToRepeatMode(newLegacyPlayerInfo.repeatMode);
shuffleModeEnabled =
LegacyConversions.convertToShuffleModeEnabled(newLegacyPlayerInfo.shuffleMode);
if (oldLegacyPlayerInfo.playbackStateCompat != newLegacyPlayerInfo.playbackStateCompat) {
availableSessionCommands =
MediaUtils.convertToSessionCommands(
LegacyConversions.convertToSessionCommands(
newLegacyPlayerInfo.playbackStateCompat, isSessionReady);
customLayout = MediaUtils.convertToCustomLayout(newLegacyPlayerInfo.playbackStateCompat);
customLayout =
LegacyConversions.convertToCustomLayout(newLegacyPlayerInfo.playbackStateCompat);
} else {
availableSessionCommands = oldControllerInfo.availableSessionCommands;
customLayout = oldControllerInfo.customLayout;
Expand All @@ -1980,53 +1990,58 @@ private static ControllerInfo buildNewControllerInfo(
? newLegacyPlayerInfo.playbackInfoCompat.getVolumeControl()
: VolumeProviderCompat.VOLUME_CONTROL_FIXED;
availablePlayerCommands =
MediaUtils.convertToPlayerCommands(
LegacyConversions.convertToPlayerCommands(
newLegacyPlayerInfo.playbackStateCompat,
volumeControlType,
sessionFlags,
isSessionReady);

PlaybackException playerError =
MediaUtils.convertToPlaybackException(newLegacyPlayerInfo.playbackStateCompat);
LegacyConversions.convertToPlaybackException(newLegacyPlayerInfo.playbackStateCompat);

long currentPositionMs =
MediaUtils.convertToCurrentPositionMs(
LegacyConversions.convertToCurrentPositionMs(
newLegacyPlayerInfo.playbackStateCompat,
newLegacyPlayerInfo.mediaMetadataCompat,
timeDiffMs);
long bufferedPositionMs =
MediaUtils.convertToBufferedPositionMs(
LegacyConversions.convertToBufferedPositionMs(
newLegacyPlayerInfo.playbackStateCompat,
newLegacyPlayerInfo.mediaMetadataCompat,
timeDiffMs);
int bufferedPercentage =
MediaUtils.convertToBufferedPercentage(
LegacyConversions.convertToBufferedPercentage(
newLegacyPlayerInfo.playbackStateCompat,
newLegacyPlayerInfo.mediaMetadataCompat,
timeDiffMs);
long totalBufferedDurationMs =
MediaUtils.convertToTotalBufferedDurationMs(
LegacyConversions.convertToTotalBufferedDurationMs(
newLegacyPlayerInfo.playbackStateCompat,
newLegacyPlayerInfo.mediaMetadataCompat,
timeDiffMs);
boolean isPlayingAd = MediaUtils.convertToIsPlayingAd(newLegacyPlayerInfo.mediaMetadataCompat);
boolean isPlayingAd =
LegacyConversions.convertToIsPlayingAd(newLegacyPlayerInfo.mediaMetadataCompat);
PlaybackParameters playbackParameters =
MediaUtils.convertToPlaybackParameters(newLegacyPlayerInfo.playbackStateCompat);
LegacyConversions.convertToPlaybackParameters(newLegacyPlayerInfo.playbackStateCompat);
AudioAttributes audioAttributes =
MediaUtils.convertToAudioAttributes(newLegacyPlayerInfo.playbackInfoCompat);
LegacyConversions.convertToAudioAttributes(newLegacyPlayerInfo.playbackInfoCompat);
boolean playWhenReady =
MediaUtils.convertToPlayWhenReady(newLegacyPlayerInfo.playbackStateCompat);
LegacyConversions.convertToPlayWhenReady(newLegacyPlayerInfo.playbackStateCompat);
@Player.State
int playbackState =
MediaUtils.convertToPlaybackState(
LegacyConversions.convertToPlaybackState(
newLegacyPlayerInfo.playbackStateCompat,
newLegacyPlayerInfo.mediaMetadataCompat,
timeDiffMs);
boolean isPlaying = MediaUtils.convertToIsPlaying(newLegacyPlayerInfo.playbackStateCompat);
boolean isPlaying =
LegacyConversions.convertToIsPlaying(newLegacyPlayerInfo.playbackStateCompat);
DeviceInfo deviceInfo =
MediaUtils.convertToDeviceInfo(newLegacyPlayerInfo.playbackInfoCompat, routingControllerId);
int deviceVolume = MediaUtils.convertToDeviceVolume(newLegacyPlayerInfo.playbackInfoCompat);
boolean deviceMuted = MediaUtils.convertToIsDeviceMuted(newLegacyPlayerInfo.playbackInfoCompat);
LegacyConversions.convertToDeviceInfo(
newLegacyPlayerInfo.playbackInfoCompat, routingControllerId);
int deviceVolume =
LegacyConversions.convertToDeviceVolume(newLegacyPlayerInfo.playbackInfoCompat);
boolean deviceMuted =
LegacyConversions.convertToIsDeviceMuted(newLegacyPlayerInfo.playbackInfoCompat);
long seekBackIncrementMs = oldControllerInfo.playerInfo.seekBackIncrementMs;
long seekForwardIncrementMs = oldControllerInfo.playerInfo.seekForwardIncrementMs;

Expand Down Expand Up @@ -2100,12 +2115,12 @@ private static ControllerInfo buildNewControllerInfo(
} else if (oldCurrentMediaItem.equals(newControllerInfo.playerInfo.getCurrentMediaItem())) {
// Current item is the same.
long oldCurrentPosition =
MediaUtils.convertToCurrentPositionMs(
LegacyConversions.convertToCurrentPositionMs(
oldLegacyPlayerInfo.playbackStateCompat,
oldLegacyPlayerInfo.mediaMetadataCompat,
timeDiffMs);
long newCurrentPosition =
MediaUtils.convertToCurrentPositionMs(
LegacyConversions.convertToCurrentPositionMs(
newLegacyPlayerInfo.playbackStateCompat,
newLegacyPlayerInfo.mediaMetadataCompat,
timeDiffMs);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ public BrowserRoot onGetRoot(
}
@Nullable
LibraryParams params =
MediaUtils.convertToLibraryParams(librarySessionImpl.getContext(), rootHints);
LegacyConversions.convertToLibraryParams(librarySessionImpl.getContext(), rootHints);
AtomicReference<ListenableFuture<LibraryResult<MediaItem>>> futureReference =
new AtomicReference<>();
ConditionVariable haveFuture = new ConditionVariable();
Expand All @@ -118,7 +118,9 @@ public BrowserRoot onGetRoot(
if (result != null && result.resultCode == RESULT_SUCCESS && result.value != null) {
@Nullable
Bundle extras =
result.params != null ? MediaUtils.convertToRootHints(result.params) : new Bundle();
result.params != null
? LegacyConversions.convertToRootHints(result.params)
: new Bundle();
boolean isSearchSessionCommandAvailable =
getConnectedControllersManager()
.isSessionCommandAvailable(controller, SessionCommand.COMMAND_CODE_LIBRARY_SEARCH);
Expand Down Expand Up @@ -157,7 +159,7 @@ public void onSubscribe(String id, Bundle option) {
}
@Nullable
LibraryParams params =
MediaUtils.convertToLibraryParams(librarySessionImpl.getContext(), option);
LegacyConversions.convertToLibraryParams(librarySessionImpl.getContext(), option);
ignoreFuture(librarySessionImpl.onSubscribeOnHandler(controller, id, params));
});
}
Expand Down Expand Up @@ -224,7 +226,8 @@ public void onLoadChildren(
// Requesting the list of children through pagination.
@Nullable
LibraryParams params =
MediaUtils.convertToLibraryParams(librarySessionImpl.getContext(), options);
LegacyConversions.convertToLibraryParams(
librarySessionImpl.getContext(), options);
ListenableFuture<LibraryResult<ImmutableList<MediaItem>>> future =
librarySessionImpl.onGetChildrenOnHandler(
controller, parentId, page, pageSize, params);
Expand Down Expand Up @@ -315,7 +318,7 @@ public void onSearch(
cb.registerSearchRequest(controller, query, extras, result);
@Nullable
LibraryParams params =
MediaUtils.convertToLibraryParams(librarySessionImpl.getContext(), extras);
LegacyConversions.convertToLibraryParams(librarySessionImpl.getContext(), extras);
ignoreFuture(librarySessionImpl.onSearchOnHandler(controller, query, params));
// Actual search result will be sent by notifySearchResultChanged().
});
Expand Down Expand Up @@ -483,7 +486,7 @@ private void handleBitmapFuturesAllCompletedAndSetOutputFuture(
Log.d(TAG, "Failed to get bitmap", e);
}
}
outputMediaItems.add(MediaUtils.convertToBrowserItem(mediaItems.get(i), bitmap));
outputMediaItems.add(LegacyConversions.convertToBrowserItem(mediaItems.get(i), bitmap));
}
outputFuture.set(outputMediaItems);
}
Expand All @@ -510,7 +513,8 @@ private static <T> void cancelAllFutures(List<@NullableType ListenableFuture<T>>
MediaItem mediaItem = result.value;
MediaMetadata metadata = mediaItem.mediaMetadata;
if (metadata.artworkData == null) {
outputFuture.set(MediaUtils.convertToBrowserItem(mediaItem, /* artworkBitmap= */ null));
outputFuture.set(
LegacyConversions.convertToBrowserItem(mediaItem, /* artworkBitmap= */ null));
return outputFuture;
}

Expand All @@ -531,7 +535,7 @@ private static <T> void cancelAllFutures(List<@NullableType ListenableFuture<T>>
} catch (CancellationException | ExecutionException e) {
Log.d(TAG, "failed to get bitmap", e);
}
outputFuture.set(MediaUtils.convertToBrowserItem(mediaItem, bitmap));
outputFuture.set(LegacyConversions.convertToBrowserItem(mediaItem, bitmap));
},
MoreExecutors.directExecutor());
return outputFuture;
Expand Down Expand Up @@ -634,7 +638,7 @@ public void onSearchResultChanged(
}
@Nullable
LibraryParams libraryParams =
MediaUtils.convertToLibraryParams(
LegacyConversions.convertToLibraryParams(
librarySessionImpl.getContext(), request.extras);
ListenableFuture<LibraryResult<ImmutableList<MediaItem>>> future =
librarySessionImpl.onGetSearchResultOnHandler(
Expand Down
Loading

0 comments on commit 72b7019

Please sign in to comment.