use of androidx.media3.common.Player.PositionInfo in project media by androidx.
the class SessionPositionInfo method fromBundle.
private static SessionPositionInfo fromBundle(Bundle bundle) {
PositionInfo positionInfo = BundleableUtil.fromNullableBundle(PositionInfo.CREATOR, bundle.getBundle(keyForField(FIELD_POSITION_INFO)), /* defaultValue= */
DEFAULT_POSITION_INFO);
boolean isPlayingAd = bundle.getBoolean(keyForField(FIELD_IS_PLAYING_AD), /* defaultValue= */
false);
long eventTimeMs = bundle.getLong(keyForField(FIELD_EVENT_TIME_MS), /* defaultValue= */
C.TIME_UNSET);
long durationMs = bundle.getLong(keyForField(FIELD_DURATION_MS), /* defaultValue= */
C.TIME_UNSET);
long bufferedPositionMs = bundle.getLong(keyForField(FIELD_BUFFERED_POSITION_MS), /* defaultValue= */
C.TIME_UNSET);
int bufferedPercentage = bundle.getInt(keyForField(FIELD_BUFFERED_PERCENTAGE), /* defaultValue= */
0);
long totalBufferedDurationMs = bundle.getLong(keyForField(FIELD_TOTAL_BUFFERED_DURATION_MS), /* defaultValue= */
0);
long currentLiveOffsetMs = bundle.getLong(keyForField(FIELD_CURRENT_LIVE_OFFSET_MS), /* defaultValue= */
C.TIME_UNSET);
long contentDurationMs = bundle.getLong(keyForField(FIELD_CONTENT_DURATION_MS), /* defaultValue= */
C.TIME_UNSET);
long contentBufferedPositionMs = bundle.getLong(keyForField(FIELD_CONTENT_BUFFERED_POSITION_MS), /* defaultValue= */
C.TIME_UNSET);
return new SessionPositionInfo(positionInfo, isPlayingAd, eventTimeMs, durationMs, bufferedPositionMs, bufferedPercentage, totalBufferedDurationMs, currentLiveOffsetMs, contentDurationMs, contentBufferedPositionMs);
}
use of androidx.media3.common.Player.PositionInfo in project media by androidx.
the class MediaControllerListenerTest method onPositionDiscontinuity_updatesGetters.
@Test
public void onPositionDiscontinuity_updatesGetters() throws Exception {
long testCurrentPositionMs = 11;
long testContentPositionMs = 33;
long testDurationMs = 200;
long testBufferedPositionMs = 100;
int testBufferedPercentage = 50;
long testTotalBufferedDurationMs = 120;
long testCurrentLiveOffsetMs = 10;
long testContentDurationMs = 300;
long testContentBufferedPositionMs = 240;
boolean testIsPlayingAd = true;
int testCurrentAdGroupIndex = 33;
int testCurrentAdIndexInAdGroup = 11;
PositionInfo newPositionInfo = new PositionInfo(/* windowUid= */
null, /* mediaItemIndex= */
C.INDEX_UNSET, /* mediaItem= */
null, /* periodUid= */
null, /* periodIndex= */
C.INDEX_UNSET, testCurrentPositionMs, testContentPositionMs, testCurrentAdGroupIndex, testCurrentAdIndexInAdGroup);
MediaController controller = controllerTestRule.createController(remoteSession.getToken());
CountDownLatch latch = new CountDownLatch(1);
AtomicLong currentPositionMsRef = new AtomicLong();
AtomicLong contentPositionMsRef = new AtomicLong();
AtomicLong durationMsRef = new AtomicLong();
AtomicLong bufferedPositionMsRef = new AtomicLong();
AtomicInteger bufferedPercentageRef = new AtomicInteger();
AtomicLong totalBufferedDurationMsRef = new AtomicLong();
AtomicLong currentLiveOffsetMsRef = new AtomicLong();
AtomicLong contentDurationMsRef = new AtomicLong();
AtomicLong contentBufferedPositionMsRef = new AtomicLong();
AtomicBoolean isPlayingAdRef = new AtomicBoolean();
AtomicInteger currentAdGroupIndexRef = new AtomicInteger();
AtomicInteger currentAdIndexInAdGroupRef = new AtomicInteger();
Player.Listener listener = new Player.Listener() {
@Override
public void onPositionDiscontinuity(PositionInfo oldPosition, PositionInfo newPosition, @DiscontinuityReason int reason) {
currentPositionMsRef.set(controller.getCurrentPosition());
contentPositionMsRef.set(controller.getContentPosition());
durationMsRef.set(controller.getDuration());
bufferedPositionMsRef.set(controller.getBufferedPosition());
bufferedPercentageRef.set(controller.getBufferedPercentage());
totalBufferedDurationMsRef.set(controller.getTotalBufferedDuration());
currentLiveOffsetMsRef.set(controller.getCurrentLiveOffset());
contentDurationMsRef.set(controller.getContentDuration());
contentBufferedPositionMsRef.set(controller.getContentBufferedPosition());
isPlayingAdRef.set(controller.isPlayingAd());
currentAdGroupIndexRef.set(controller.getCurrentAdGroupIndex());
currentAdIndexInAdGroupRef.set(controller.getCurrentAdIndexInAdGroup());
latch.countDown();
}
};
threadTestRule.getHandler().postAndSync(() -> controller.addListener(listener));
RemoteMockPlayer remoteMockPlayer = remoteSession.getMockPlayer();
remoteMockPlayer.setCurrentPosition(testCurrentPositionMs);
remoteMockPlayer.setContentPosition(testContentPositionMs);
remoteMockPlayer.setDuration(testDurationMs);
remoteMockPlayer.setBufferedPosition(testBufferedPositionMs);
remoteMockPlayer.setBufferedPercentage(testBufferedPercentage);
remoteMockPlayer.setTotalBufferedDuration(testTotalBufferedDurationMs);
remoteMockPlayer.setCurrentLiveOffset(testCurrentLiveOffsetMs);
remoteMockPlayer.setContentDuration(testContentDurationMs);
remoteMockPlayer.setContentBufferedPosition(testContentBufferedPositionMs);
remoteMockPlayer.setIsPlayingAd(testIsPlayingAd);
remoteMockPlayer.setCurrentAdGroupIndex(testCurrentAdGroupIndex);
remoteMockPlayer.setCurrentAdIndexInAdGroup(testCurrentAdIndexInAdGroup);
remoteMockPlayer.notifyPositionDiscontinuity(/* oldPositionInfo= */
SessionPositionInfo.DEFAULT_POSITION_INFO, newPositionInfo, Player.DISCONTINUITY_REASON_INTERNAL);
assertThat(latch.await(TIMEOUT_MS, MILLISECONDS)).isTrue();
assertThat(currentPositionMsRef.get()).isEqualTo(testCurrentPositionMs);
assertThat(contentPositionMsRef.get()).isEqualTo(testContentPositionMs);
assertThat(durationMsRef.get()).isEqualTo(testDurationMs);
assertThat(bufferedPositionMsRef.get()).isEqualTo(testBufferedPositionMs);
assertThat(bufferedPercentageRef.get()).isEqualTo(testBufferedPercentage);
assertThat(totalBufferedDurationMsRef.get()).isEqualTo(testTotalBufferedDurationMs);
assertThat(currentLiveOffsetMsRef.get()).isEqualTo(testCurrentLiveOffsetMs);
assertThat(contentDurationMsRef.get()).isEqualTo(testContentDurationMs);
assertThat(contentBufferedPositionMsRef.get()).isEqualTo(testContentBufferedPositionMs);
assertThat(isPlayingAdRef.get()).isEqualTo(testIsPlayingAd);
assertThat(currentAdGroupIndexRef.get()).isEqualTo(testCurrentAdGroupIndex);
assertThat(currentAdIndexInAdGroupRef.get()).isEqualTo(testCurrentAdIndexInAdGroup);
}
use of androidx.media3.common.Player.PositionInfo in project media by androidx.
the class MediaControllerMediaSessionCompatCallbackAggregationTest method getters_withValidQueueAndQueueIdAndMetadata.
@Test
public void getters_withValidQueueAndQueueIdAndMetadata() throws Exception {
int testSize = 3;
List<MediaItem> testMediaItems = MediaTestUtils.createMediaItems(testSize);
List<QueueItem> testQueue = MediaUtils.convertToQueueItemList(testMediaItems);
int testMediaItemIndex = 1;
MediaMetadataCompat testMediaMetadataCompat = createMediaMetadataCompat();
@RatingCompat.Style int testRatingType = RatingCompat.RATING_HEART;
MediaMetadata testMediaMetadata = MediaUtils.convertToMediaMetadata(testMediaMetadataCompat, testRatingType);
MediaItem testCurrentMediaItem = new MediaItem.Builder().setMediaId(testMediaItems.get(testMediaItemIndex).mediaId).setMediaMetadata(testMediaMetadata).build();
Events testEvents = new Events(new FlagSet.Builder().addAll(EVENT_MEDIA_ITEM_TRANSITION, EVENT_MEDIA_METADATA_CHANGED, EVENT_POSITION_DISCONTINUITY, EVENT_TIMELINE_CHANGED).build());
MediaController controller = controllerTestRule.createController(session.getSessionToken());
CountDownLatch latch = new CountDownLatch(5);
AtomicReference<Timeline> timelineRef = new AtomicReference<>();
AtomicInteger timelineChangeReasonRef = new AtomicInteger();
AtomicReference<MediaItem> mediaItemRef = new AtomicReference<>();
AtomicInteger mediaItemTransitionReasonRef = new AtomicInteger();
AtomicReference<PositionInfo> positionInfoRef = new AtomicReference<>();
AtomicInteger discontinuityReasonRef = new AtomicInteger();
AtomicReference<MediaMetadata> metadataRef = new AtomicReference<>();
AtomicReference<Events> eventsRef = new AtomicReference<>();
Player.Listener listener = new Player.Listener() {
@Override
public void onTimelineChanged(Timeline timeline, @Player.TimelineChangeReason int reason) {
timelineRef.set(timeline);
timelineChangeReasonRef.set(reason);
latch.countDown();
}
@Override
public void onMediaItemTransition(@Nullable MediaItem mediaItem, @Player.MediaItemTransitionReason int reason) {
mediaItemRef.set(mediaItem);
mediaItemTransitionReasonRef.set(reason);
latch.countDown();
}
@Override
public void onPositionDiscontinuity(PositionInfo oldPosition, PositionInfo newPosition, @Player.DiscontinuityReason int reason) {
positionInfoRef.set(newPosition);
discontinuityReasonRef.set(reason);
latch.countDown();
}
@Override
public void onMediaMetadataChanged(MediaMetadata mediaMetadata) {
metadataRef.set(mediaMetadata);
latch.countDown();
}
@Override
public void onEvents(Player player, Events events) {
eventsRef.set(events);
latch.countDown();
}
};
threadTestRule.getHandler().postAndSync(() -> controller.addListener(listener));
session.setQueue(testQueue);
session.setPlaybackState(new PlaybackStateCompat.Builder().setActiveQueueItemId(testQueue.get(testMediaItemIndex).getQueueId()).build());
session.setMetadata(testMediaMetadataCompat);
session.setRatingType(testRatingType);
assertThat(latch.await(TIMEOUT_MS, MILLISECONDS)).isTrue();
assertThat(mediaItemRef.get()).isEqualTo(testCurrentMediaItem);
for (int i = 0; i < timelineRef.get().getWindowCount(); i++) {
assertThat(timelineRef.get().getWindow(i, new Window()).mediaItem).isEqualTo(i == testMediaItemIndex ? testCurrentMediaItem : testMediaItems.get(i));
}
assertThat(timelineChangeReasonRef.get()).isEqualTo(TIMELINE_CHANGE_REASON_PLAYLIST_CHANGED);
assertThat(mediaItemTransitionReasonRef.get()).isEqualTo(Player.MEDIA_ITEM_TRANSITION_REASON_PLAYLIST_CHANGED);
assertThat(positionInfoRef.get().mediaItemIndex).isEqualTo(testMediaItemIndex);
assertThat(discontinuityReasonRef.get()).isEqualTo(DISCONTINUITY_REASON_AUTO_TRANSITION);
assertThat(metadataRef.get()).isEqualTo(testMediaMetadata);
assertThat(eventsRef.get()).isEqualTo(testEvents);
Timeline currentTimeline = threadTestRule.getHandler().postAndSync(controller::getCurrentTimeline);
assertThat(currentTimeline).isEqualTo(timelineRef.get());
int currentMediaItemIndex = threadTestRule.getHandler().postAndSync(controller::getCurrentMediaItemIndex);
assertThat(currentMediaItemIndex).isEqualTo(testMediaItemIndex);
MediaItem currentMediaItem = threadTestRule.getHandler().postAndSync(controller::getCurrentMediaItem);
assertThat(currentMediaItem).isEqualTo(testCurrentMediaItem);
MediaMetadata currentMediaMetadata = threadTestRule.getHandler().postAndSync(controller::getMediaMetadata);
assertThat(currentMediaMetadata).isEqualTo(currentMediaItem.mediaMetadata);
}
use of androidx.media3.common.Player.PositionInfo in project media by androidx.
the class MediaControllerMediaSessionCompatCallbackAggregationTest method getters_withValidQueueAndQueueIdWithoutMetadata.
@Test
public void getters_withValidQueueAndQueueIdWithoutMetadata() throws Exception {
int testSize = 3;
List<MediaItem> testMediaItems = MediaTestUtils.createMediaItems(testSize);
List<QueueItem> testQueue = MediaUtils.convertToQueueItemList(testMediaItems);
@RatingCompat.Style int testRatingType = RatingCompat.RATING_HEART;
Events testEvents = new Events(new FlagSet.Builder().addAll(EVENT_MEDIA_ITEM_TRANSITION, EVENT_POSITION_DISCONTINUITY, EVENT_TIMELINE_CHANGED).build());
int testMediaItemIndex = 1;
MediaController controller = controllerTestRule.createController(session.getSessionToken());
CountDownLatch latch = new CountDownLatch(4);
AtomicReference<Timeline> timelineRef = new AtomicReference<>();
AtomicInteger timelineChangeReasonRef = new AtomicInteger();
AtomicReference<MediaItem> mediaItemRef = new AtomicReference<>();
AtomicInteger mediaItemTransitionReasonRef = new AtomicInteger();
AtomicReference<PositionInfo> positionInfoRef = new AtomicReference<>();
AtomicInteger discontinuityReasonRef = new AtomicInteger();
AtomicReference<Events> eventsRef = new AtomicReference<>();
Player.Listener listener = new Player.Listener() {
@Override
public void onTimelineChanged(Timeline timeline, @Player.TimelineChangeReason int reason) {
timelineRef.set(timeline);
timelineChangeReasonRef.set(reason);
latch.countDown();
}
@Override
public void onMediaItemTransition(@Nullable MediaItem mediaItem, @Player.MediaItemTransitionReason int reason) {
mediaItemRef.set(mediaItem);
mediaItemTransitionReasonRef.set(reason);
latch.countDown();
}
@Override
public void onPositionDiscontinuity(PositionInfo oldPosition, PositionInfo newPosition, @Player.DiscontinuityReason int reason) {
positionInfoRef.set(newPosition);
discontinuityReasonRef.set(reason);
latch.countDown();
}
@Override
public void onEvents(Player player, Events events) {
eventsRef.set(events);
latch.countDown();
}
};
threadTestRule.getHandler().postAndSync(() -> controller.addListener(listener));
session.setQueue(testQueue);
session.setPlaybackState(new PlaybackStateCompat.Builder().setActiveQueueItemId(testQueue.get(testMediaItemIndex).getQueueId()).build());
session.setRatingType(testRatingType);
assertThat(latch.await(TIMEOUT_MS, MILLISECONDS)).isTrue();
assertTimelineEqualsToMediaItems(timelineRef.get(), testMediaItems);
assertThat(timelineChangeReasonRef.get()).isEqualTo(TIMELINE_CHANGE_REASON_PLAYLIST_CHANGED);
assertThat(mediaItemRef.get()).isEqualTo(testMediaItems.get(testMediaItemIndex));
assertThat(mediaItemTransitionReasonRef.get()).isEqualTo(Player.MEDIA_ITEM_TRANSITION_REASON_PLAYLIST_CHANGED);
assertThat(positionInfoRef.get().mediaItemIndex).isEqualTo(testMediaItemIndex);
assertThat(discontinuityReasonRef.get()).isEqualTo(DISCONTINUITY_REASON_AUTO_TRANSITION);
assertThat(eventsRef.get()).isEqualTo(testEvents);
Timeline currentTimeline = threadTestRule.getHandler().postAndSync(controller::getCurrentTimeline);
assertThat(currentTimeline).isEqualTo(timelineRef.get());
int currentMediaItemIndex = threadTestRule.getHandler().postAndSync(controller::getCurrentMediaItemIndex);
assertThat(currentMediaItemIndex).isEqualTo(testMediaItemIndex);
MediaItem currentMediaItem = threadTestRule.getHandler().postAndSync(controller::getCurrentMediaItem);
assertThat(currentMediaItem).isEqualTo(testMediaItems.get(testMediaItemIndex));
MediaMetadata currentMediaMetadata = threadTestRule.getHandler().postAndSync(controller::getMediaMetadata);
assertThat(currentMediaMetadata).isEqualTo(currentMediaItem.mediaMetadata);
}
use of androidx.media3.common.Player.PositionInfo in project media by androidx.
the class MediaControllerStateMaskingTest method seekToPreviousMediaItem.
@Test
public void seekToPreviousMediaItem() throws Exception {
int initialMediaItemIndex = 1;
String firstMediaId = "firstMediaId";
String secondMediaId = "secondMediaId";
String thirdMediaId = "thirdMediaId";
String testCurrentMediaId = firstMediaId;
int testMediaItemIndex = 0;
Bundle playerConfig = new RemoteMediaSession.MockPlayerConfigBuilder().setTimeline(MediaTestUtils.createTimeline(MediaTestUtils.createMediaItems(firstMediaId, secondMediaId, thirdMediaId))).setCurrentMediaItemIndex(initialMediaItemIndex).setCurrentPeriodIndex(initialMediaItemIndex).build();
remoteSession.setPlayer(playerConfig);
MediaController controller = controllerTestRule.createController(remoteSession.getToken());
CountDownLatch latch = new CountDownLatch(3);
AtomicReference<MediaItem> newMediaItemRef = new AtomicReference<>();
AtomicReference<PositionInfo> oldPositionInfoRef = new AtomicReference<>();
AtomicReference<PositionInfo> newPositionInfoRef = new AtomicReference<>();
AtomicReference<Player.Events> onEventsRef = new AtomicReference<>();
Player.Listener listener = new Player.Listener() {
@Override
public void onMediaItemTransition(@Nullable MediaItem mediaItem, int reason) {
newMediaItemRef.set(mediaItem);
latch.countDown();
}
@Override
public void onPositionDiscontinuity(PositionInfo oldPosition, PositionInfo newPosition, int reason) {
oldPositionInfoRef.set(oldPosition);
newPositionInfoRef.set(newPosition);
latch.countDown();
}
@Override
public void onEvents(Player player, Player.Events events) {
onEventsRef.set(events);
latch.countDown();
}
};
threadTestRule.getHandler().postAndSync(() -> controller.addListener(listener));
AtomicInteger currentMediaItemIndexRef = new AtomicInteger();
threadTestRule.getHandler().postAndSync(() -> {
controller.seekToPreviousMediaItem();
currentMediaItemIndexRef.set(controller.getCurrentMediaItemIndex());
});
assertThat(latch.await(TIMEOUT_MS, MILLISECONDS)).isTrue();
assertThat(newMediaItemRef.get().mediaId).isEqualTo(testCurrentMediaId);
assertThat(oldPositionInfoRef.get().mediaItemIndex).isEqualTo(initialMediaItemIndex);
assertThat(newPositionInfoRef.get().mediaItemIndex).isEqualTo(testMediaItemIndex);
assertThat(onEventsRef.get().contains(Player.EVENT_MEDIA_ITEM_TRANSITION)).isTrue();
assertThat(onEventsRef.get().contains(Player.EVENT_POSITION_DISCONTINUITY)).isTrue();
assertThat(currentMediaItemIndexRef.get()).isEqualTo(testMediaItemIndex);
}
Aggregations