Search in sources :

Example 26 with Timeline

use of com.google.android.exoplayer2.Timeline in project ExoPlayer by google.

the class DefaultPlaybackSessionManager method updateCurrentSession.

@RequiresNonNull("listener")
private void updateCurrentSession(EventTime eventTime) {
    if (eventTime.timeline.isEmpty()) {
        // Clear current session if the Timeline is empty.
        currentSessionId = null;
        return;
    }
    @Nullable SessionDescriptor previousSessionDescriptor = sessions.get(currentSessionId);
    SessionDescriptor currentSessionDescriptor = getOrAddSession(eventTime.windowIndex, eventTime.mediaPeriodId);
    currentSessionId = currentSessionDescriptor.sessionId;
    updateSessions(eventTime);
    if (eventTime.mediaPeriodId != null && eventTime.mediaPeriodId.isAd() && (previousSessionDescriptor == null || previousSessionDescriptor.windowSequenceNumber != eventTime.mediaPeriodId.windowSequenceNumber || previousSessionDescriptor.adMediaPeriodId == null || previousSessionDescriptor.adMediaPeriodId.adGroupIndex != eventTime.mediaPeriodId.adGroupIndex || previousSessionDescriptor.adMediaPeriodId.adIndexInAdGroup != eventTime.mediaPeriodId.adIndexInAdGroup)) {
        // New ad playback started. Find corresponding content session and notify ad playback started.
        MediaPeriodId contentMediaPeriodId = new MediaPeriodId(eventTime.mediaPeriodId.periodUid, eventTime.mediaPeriodId.windowSequenceNumber);
        SessionDescriptor contentSession = getOrAddSession(eventTime.windowIndex, contentMediaPeriodId);
        listener.onAdPlaybackStarted(eventTime, contentSession.sessionId, currentSessionDescriptor.sessionId);
    }
}
Also used : MediaPeriodId(com.google.android.exoplayer2.source.MediaSource.MediaPeriodId) Nullable(androidx.annotation.Nullable) RequiresNonNull(org.checkerframework.checker.nullness.qual.RequiresNonNull)

Example 27 with Timeline

use of com.google.android.exoplayer2.Timeline in project ExoPlayer by google.

the class DefaultPlaybackSessionManager method updateSessionsWithTimelineChange.

@Override
public synchronized void updateSessionsWithTimelineChange(EventTime eventTime) {
    Assertions.checkNotNull(listener);
    Timeline previousTimeline = currentTimeline;
    currentTimeline = eventTime.timeline;
    Iterator<SessionDescriptor> iterator = sessions.values().iterator();
    while (iterator.hasNext()) {
        SessionDescriptor session = iterator.next();
        if (!session.tryResolvingToNewTimeline(previousTimeline, currentTimeline) || session.isFinishedAtEventTime(eventTime)) {
            iterator.remove();
            if (session.isCreated) {
                if (session.sessionId.equals(currentSessionId)) {
                    currentSessionId = null;
                }
                listener.onSessionFinished(eventTime, session.sessionId, /* automaticTransitionToNextPlayback= */
                false);
            }
        }
    }
    updateCurrentSession(eventTime);
}
Also used : Timeline(com.google.android.exoplayer2.Timeline)

Example 28 with Timeline

use of com.google.android.exoplayer2.Timeline in project ExoPlayer by google.

the class CastPlayerTest method updateTimeLine.

private void updateTimeLine(List<MediaItem> mediaItems, int[] mediaQueueItemIds, int currentItemId, int[] streamTypes, long[] durationsMs, long positionMs) {
    // Set up mocks to allow the player to update the timeline.
    List<MediaQueueItem> queueItems = new ArrayList<>();
    for (int i = 0; i < mediaQueueItemIds.length; i++) {
        MediaItem mediaItem = mediaItems.get(i);
        int mediaQueueItemId = mediaQueueItemIds[i];
        int streamType = streamTypes[i];
        long durationMs = durationsMs[i];
        MediaInfo.Builder mediaInfoBuilder = new MediaInfo.Builder(mediaItem.localConfiguration.uri.toString()).setStreamType(streamType).setContentType(mediaItem.localConfiguration.mimeType);
        if (durationMs != C.TIME_UNSET) {
            mediaInfoBuilder.setStreamDuration(durationMs);
        }
        MediaInfo mediaInfo = mediaInfoBuilder.build();
        MediaQueueItem mediaQueueItem = mock(MediaQueueItem.class);
        when(mediaQueueItem.getItemId()).thenReturn(mediaQueueItemId);
        when(mediaQueueItem.getMedia()).thenReturn(mediaInfo);
        queueItems.add(mediaQueueItem);
        if (mediaQueueItemId == currentItemId) {
            when(mockRemoteMediaClient.getCurrentItem()).thenReturn(mediaQueueItem);
            when(mockMediaStatus.getMediaInfo()).thenReturn(mediaInfo);
        }
    }
    if (positionMs != C.TIME_UNSET) {
        when(mockRemoteMediaClient.getApproximateStreamPosition()).thenReturn(positionMs);
    }
    when(mockMediaQueue.getItemIds()).thenReturn(mediaQueueItemIds);
    when(mockMediaStatus.getQueueItems()).thenReturn(queueItems);
    when(mockMediaStatus.getCurrentItemId()).thenReturn(currentItemId == C.INDEX_UNSET ? 0 : currentItemId);
    // Call listener to update the timeline of the player.
    remoteMediaClientCallback.onStatusUpdated();
}
Also used : MediaInfo(com.google.android.gms.cast.MediaInfo) MediaItem(com.google.android.exoplayer2.MediaItem) ArrayList(java.util.ArrayList) MediaQueueItem(com.google.android.gms.cast.MediaQueueItem)

Example 29 with Timeline

use of com.google.android.exoplayer2.Timeline in project ExoPlayer by google.

the class CastPlayerTest method autoTransition_notifiesMediaItemTransition.

@Test
public void autoTransition_notifiesMediaItemTransition() {
    int[] mediaQueueItemIds = new int[] { 1, 2 };
    // When the remote Cast player transitions to an item that wasn't played before, the media state
    // delivers the duration for that media item which updates the timeline accordingly.
    List<MediaItem> mediaItems = createMediaItems(mediaQueueItemIds);
    castPlayer.addMediaItems(mediaItems);
    updateTimeLine(mediaItems, mediaQueueItemIds, /* currentItemId= */
    1);
    updateTimeLine(mediaItems, mediaQueueItemIds, /* currentItemId= */
    2);
    InOrder inOrder = Mockito.inOrder(mockListener);
    inOrder.verify(mockListener).onMediaItemTransition(any(), eq(Player.MEDIA_ITEM_TRANSITION_REASON_PLAYLIST_CHANGED));
    inOrder.verify(mockListener).onMediaItemTransition(mediaItemCaptor.capture(), eq(Player.MEDIA_ITEM_TRANSITION_REASON_AUTO));
    inOrder.verify(mockListener, never()).onMediaItemTransition(any(), anyInt());
    assertThat(mediaItemCaptor.getValue().localConfiguration.tag).isEqualTo(2);
}
Also used : InOrder(org.mockito.InOrder) MediaItem(com.google.android.exoplayer2.MediaItem) Test(org.junit.Test)

Example 30 with Timeline

use of com.google.android.exoplayer2.Timeline in project ExoPlayer by google.

the class CastPlayer method getCurrentPositionInfo.

private PositionInfo getCurrentPositionInfo() {
    Timeline currentTimeline = getCurrentTimeline();
    @Nullable Object newPeriodUid = null;
    @Nullable Object newWindowUid = null;
    @Nullable MediaItem newMediaItem = null;
    if (!currentTimeline.isEmpty()) {
        newPeriodUid = currentTimeline.getPeriod(getCurrentPeriodIndex(), period, /* setIds= */
        true).uid;
        newWindowUid = currentTimeline.getWindow(period.windowIndex, window).uid;
        newMediaItem = window.mediaItem;
    }
    return new PositionInfo(newWindowUid, getCurrentMediaItemIndex(), newMediaItem, newPeriodUid, getCurrentPeriodIndex(), getCurrentPosition(), getContentPosition(), /* adGroupIndex= */
    C.INDEX_UNSET, /* adIndexInAdGroup= */
    C.INDEX_UNSET);
}
Also used : Timeline(com.google.android.exoplayer2.Timeline) MediaItem(com.google.android.exoplayer2.MediaItem) Nullable(androidx.annotation.Nullable)

Aggregations

Test (org.junit.Test)347 FakeTimeline (com.google.android.exoplayer2.testutil.FakeTimeline)246 Timeline (com.google.android.exoplayer2.Timeline)163 FakeMediaSource (com.google.android.exoplayer2.testutil.FakeMediaSource)140 SinglePeriodTimeline (com.google.android.exoplayer2.source.SinglePeriodTimeline)125 NoUidTimeline (com.google.android.exoplayer2.testutil.NoUidTimeline)110 TestExoPlayerBuilder (com.google.android.exoplayer2.testutil.TestExoPlayerBuilder)109 TimelineWindowDefinition (com.google.android.exoplayer2.testutil.FakeTimeline.TimelineWindowDefinition)89 MediaPeriodId (com.google.android.exoplayer2.source.MediaSource.MediaPeriodId)85 TrackGroupArray (com.google.android.exoplayer2.source.TrackGroupArray)79 ActionSchedule (com.google.android.exoplayer2.testutil.ActionSchedule)74 ExoPlayerTestRunner (com.google.android.exoplayer2.testutil.ExoPlayerTestRunner)60 Format (com.google.android.exoplayer2.Format)49 MediaSource (com.google.android.exoplayer2.source.MediaSource)47 AdPlaybackState (com.google.android.exoplayer2.source.ads.AdPlaybackState)44 ConcatenatingMediaSource (com.google.android.exoplayer2.source.ConcatenatingMediaSource)38 PlayerRunnable (com.google.android.exoplayer2.testutil.ActionSchedule.PlayerRunnable)36 EventTime (com.google.android.exoplayer2.analytics.AnalyticsListener.EventTime)35 MediaItem (com.google.android.exoplayer2.MediaItem)34 RendererCapabilities (com.google.android.exoplayer2.RendererCapabilities)33