Skip to content

Commit

Permalink
Extend command GET_CURRENT_MEDIA_ITEM to more methods.
Browse files Browse the repository at this point in the history
We currently only document it for the getCurrentMediaItem(), but
the command was always meant to cover all information about the
current media item and the position therein.

To correctly hide information for controllers, we need to filter
the Timeline when bundling the PlayerInfo class if only this
command is available.

PiperOrigin-RevId: 503098124
  • Loading branch information
tonihei authored and christosts committed Jan 23, 2023
1 parent aa72b45 commit f15b752
Show file tree
Hide file tree
Showing 10 changed files with 564 additions and 62 deletions.
70 changes: 62 additions & 8 deletions libraries/common/src/main/java/androidx/media3/common/Player.java
Original file line number Diff line number Diff line change
Expand Up @@ -1639,10 +1639,28 @@ default void onMetadata(Metadata metadata) {}
int COMMAND_SET_REPEAT_MODE = 15;

/**
* Command to get the currently playing {@link MediaItem}.
* Command to get information about the currently playing {@link MediaItem}.
*
* <p>The {@link #getCurrentMediaItem()} method must only be called if this command is {@linkplain
* #isCommandAvailable(int) available}.
* <p>The following methods must only be called if this command is {@linkplain
* #isCommandAvailable(int) available}:
*
* <ul>
* <li>{@link #getCurrentMediaItem()}
* <li>{@link #isCurrentMediaItemDynamic()}
* <li>{@link #isCurrentMediaItemLive()}
* <li>{@link #isCurrentMediaItemSeekable()}
* <li>{@link #getCurrentLiveOffset()}
* <li>{@link #getDuration()}
* <li>{@link #getCurrentPosition()}
* <li>{@link #getBufferedPosition()}
* <li>{@link #getContentDuration()}
* <li>{@link #getContentPosition()}
* <li>{@link #getContentBufferedPosition()}
* <li>{@link #getTotalBufferedDuration()}
* <li>{@link #isPlayingAd()}
* <li>{@link #getCurrentAdGroupIndex()}
* <li>{@link #getCurrentAdIndexInAdGroup()}
* </ul>
*/
int COMMAND_GET_CURRENT_MEDIA_ITEM = 16;

Expand All @@ -1662,8 +1680,6 @@ default void onMetadata(Metadata metadata) {}
* <li>{@link #getPreviousMediaItemIndex()}
* <li>{@link #hasPreviousMediaItem()}
* <li>{@link #hasNextMediaItem()}
* <li>{@link #getCurrentAdGroupIndex()}
* <li>{@link #getCurrentAdIndexInAdGroup()}
* </ul>
*/
int COMMAND_GET_TIMELINE = 17;
Expand Down Expand Up @@ -2706,18 +2722,27 @@ default void onMetadata(Metadata metadata) {}
/**
* Returns the duration of the current content or ad in milliseconds, or {@link C#TIME_UNSET} if
* the duration is not known.
*
* <p>This method must only be called if {@link #COMMAND_GET_CURRENT_MEDIA_ITEM} is {@linkplain
* #getAvailableCommands() available}.
*/
long getDuration();

/**
* Returns the playback position in the current content or ad, in milliseconds, or the prospective
* position in milliseconds if the {@link #getCurrentTimeline() current timeline} is empty.
*
* <p>This method must only be called if {@link #COMMAND_GET_CURRENT_MEDIA_ITEM} is {@linkplain
* #getAvailableCommands() available}.
*/
long getCurrentPosition();

/**
* Returns an estimate of the position in the current content or ad up to which data is buffered,
* in milliseconds.
*
* <p>This method must only be called if {@link #COMMAND_GET_CURRENT_MEDIA_ITEM} is {@linkplain
* #getAvailableCommands() available}.
*/
long getBufferedPosition();

Expand All @@ -2731,6 +2756,9 @@ default void onMetadata(Metadata metadata) {}
/**
* Returns an estimate of the total buffered duration from the current position, in milliseconds.
* This includes pre-buffered data for subsequent ads and {@linkplain MediaItem media items}.
*
* <p>This method must only be called if {@link #COMMAND_GET_CURRENT_MEDIA_ITEM} is {@linkplain
* #getAvailableCommands() available}.
*/
long getTotalBufferedDuration();

Expand All @@ -2745,6 +2773,9 @@ default void onMetadata(Metadata metadata) {}
* Returns whether the current {@link MediaItem} is dynamic (may change when the {@link Timeline}
* is updated), or {@code false} if the {@link Timeline} is empty.
*
* <p>This method must only be called if {@link #COMMAND_GET_CURRENT_MEDIA_ITEM} is {@linkplain
* #getAvailableCommands() available}.
*
* @see Timeline.Window#isDynamic
*/
boolean isCurrentMediaItemDynamic();
Expand All @@ -2760,6 +2791,9 @@ default void onMetadata(Metadata metadata) {}
* Returns whether the current {@link MediaItem} is live, or {@code false} if the {@link Timeline}
* is empty.
*
* <p>This method must only be called if {@link #COMMAND_GET_CURRENT_MEDIA_ITEM} is {@linkplain
* #getAvailableCommands() available}.
*
* @see Timeline.Window#isLive()
*/
boolean isCurrentMediaItemLive();
Expand All @@ -2774,6 +2808,9 @@ default void onMetadata(Metadata metadata) {}
*
* <p>Note that this offset may rely on an accurate local time, so this method may return an
* incorrect value if the difference between system clock and server clock is unknown.
*
* <p>This method must only be called if {@link #COMMAND_GET_CURRENT_MEDIA_ITEM} is {@linkplain
* #getAvailableCommands() available}.
*/
long getCurrentLiveOffset();

Expand All @@ -2788,18 +2825,26 @@ default void onMetadata(Metadata metadata) {}
* Returns whether the current {@link MediaItem} is seekable, or {@code false} if the {@link
* Timeline} is empty.
*
* <p>This method must only be called if {@link #COMMAND_GET_CURRENT_MEDIA_ITEM} is {@linkplain
* #getAvailableCommands() available}.
*
* @see Timeline.Window#isSeekable
*/
boolean isCurrentMediaItemSeekable();

/** Returns whether the player is currently playing an ad. */
/**
* Returns whether the player is currently playing an ad.
*
* <p>This method must only be called if {@link #COMMAND_GET_CURRENT_MEDIA_ITEM} is {@linkplain
* #getAvailableCommands() available}.
*/
boolean isPlayingAd();

/**
* If {@link #isPlayingAd()} returns true, returns the index of the ad group in the period
* currently being played. Returns {@link C#INDEX_UNSET} otherwise.
*
* <p>This method must only be called if {@link #COMMAND_GET_TIMELINE} is {@linkplain
* <p>This method must only be called if {@link #COMMAND_GET_CURRENT_MEDIA_ITEM} is {@linkplain
* #getAvailableCommands() available}.
*/
int getCurrentAdGroupIndex();
Expand All @@ -2808,7 +2853,7 @@ default void onMetadata(Metadata metadata) {}
* If {@link #isPlayingAd()} returns true, returns the index of the ad in its ad group. Returns
* {@link C#INDEX_UNSET} otherwise.
*
* <p>This method must only be called if {@link #COMMAND_GET_TIMELINE} is {@linkplain
* <p>This method must only be called if {@link #COMMAND_GET_CURRENT_MEDIA_ITEM} is {@linkplain
* #getAvailableCommands() available}.
*/
int getCurrentAdIndexInAdGroup();
Expand All @@ -2817,20 +2862,29 @@ default void onMetadata(Metadata metadata) {}
* If {@link #isPlayingAd()} returns {@code true}, returns the duration of the current content in
* milliseconds, or {@link C#TIME_UNSET} if the duration is not known. If there is no ad playing,
* the returned duration is the same as that returned by {@link #getDuration()}.
*
* <p>This method must only be called if {@link #COMMAND_GET_CURRENT_MEDIA_ITEM} is {@linkplain
* #getAvailableCommands() available}.
*/
long getContentDuration();

/**
* If {@link #isPlayingAd()} returns {@code true}, returns the content position that will be
* played once all ads in the ad group have finished playing, in milliseconds. If there is no ad
* playing, the returned position is the same as that returned by {@link #getCurrentPosition()}.
*
* <p>This method must only be called if {@link #COMMAND_GET_CURRENT_MEDIA_ITEM} is {@linkplain
* #getAvailableCommands() available}.
*/
long getContentPosition();

/**
* If {@link #isPlayingAd()} returns {@code true}, returns an estimate of the content position in
* the current content up to which data is buffered, in milliseconds. If there is no ad playing,
* the returned position is the same as that returned by {@link #getBufferedPosition()}.
*
* <p>This method must only be called if {@link #COMMAND_GET_CURRENT_MEDIA_ITEM} is {@linkplain
* #getAvailableCommands() available}.
*/
long getContentBufferedPosition();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1416,6 +1416,39 @@ public final Bundle toBundle() {
return bundle;
}

/**
* Returns a {@link Bundle} containing just the specified {@link Window}.
*
* <p>The {@link #getWindow(int, Window)} windows} and {@link #getPeriod(int, Period) periods} of
* an instance restored by {@link #CREATOR} may have missing fields as described in {@link
* Window#toBundle()} and {@link Period#toBundle()}.
*
* @param windowIndex The index of the {@link Window} to include in the {@link Bundle}.
*/
@UnstableApi
public final Bundle toBundleWithOneWindowOnly(int windowIndex) {
Window window = getWindow(windowIndex, new Window(), /* defaultPositionProjectionUs= */ 0);

List<Bundle> periodBundles = new ArrayList<>();
Period period = new Period();
for (int i = window.firstPeriodIndex; i <= window.lastPeriodIndex; i++) {
getPeriod(i, period, /* setIds= */ false);
period.windowIndex = 0;
periodBundles.add(period.toBundle());
}

window.lastPeriodIndex = window.lastPeriodIndex - window.firstPeriodIndex;
window.firstPeriodIndex = 0;
Bundle windowBundle = window.toBundle();

Bundle bundle = new Bundle();
BundleUtil.putBinder(
bundle, FIELD_WINDOWS, new BundleListRetriever(ImmutableList.of(windowBundle)));
BundleUtil.putBinder(bundle, FIELD_PERIODS, new BundleListRetriever(periodBundles));
bundle.putIntArray(FIELD_SHUFFLED_WINDOW_INDICES, new int[] {0});
return bundle;
}

/**
* Object that can restore a {@link Timeline} from a {@link Bundle}.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -811,8 +811,6 @@ public void onMediaItemTransition(
if (player == null) {
return;
}
// Note: OK to omit mediaItem here, because PlayerInfo changed message will copy playerInfo
// with sessionPositionInfo, which includes current window index.
session.playerInfo = session.playerInfo.copyWithMediaItemTransitionReason(reason);
session.onPlayerInfoChangedHandler.sendPlayerInfoChangedMessage(
/* excludeTimeline= */ true, /* excludeTracks= */ true);
Expand Down
Loading

0 comments on commit f15b752

Please sign in to comment.