Java Code Examples for com.google.android.exoplayer2.Timeline#isEmpty()

The following examples show how to use com.google.android.exoplayer2.Timeline#isEmpty() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: CustomizeControlView.java    From bcm-android with GNU General Public License v3.0 6 votes vote down vote up
private void updateNavigation() {
    if (!isVisible() || !isAttachedToWindow) {
        return;
    }
    Timeline timeline = player != null ? player.getCurrentTimeline() : null;
    boolean haveNonEmptyTimeline = timeline != null && !timeline.isEmpty();
    boolean isSeekable = false;
    boolean enablePrevious = false;
    boolean enableNext = false;
    if (haveNonEmptyTimeline && !player.isPlayingAd()) {
        int windowIndex = player.getCurrentWindowIndex();
        timeline.getWindow(windowIndex, window);
        isSeekable = window.isSeekable;
        enablePrevious =
                isSeekable || !window.isDynamic || player.getPreviousWindowIndex() != C.INDEX_UNSET;
        enableNext = window.isDynamic || player.getNextWindowIndex() != C.INDEX_UNSET;
    }
    setButtonEnabled(enablePrevious, previousButton);
    setButtonEnabled(enableNext, nextButton);
    setButtonEnabled(fastForwardMs > 0 && isSeekable, fastForwardButton);
    setButtonEnabled(rewindMs > 0 && isSeekable, rewindButton);
    if (timeBar != null) {
        timeBar.setEnabled(isSeekable);
    }
}
 
Example 2
Source File: PlaybackControlView.java    From K-Sonic with MIT License 6 votes vote down vote up
private void updateNavigation() {
  if (!isVisible() || !isAttachedToWindow) {
    return;
  }
  Timeline currentTimeline = player != null ? player.getCurrentTimeline() : null;
  boolean haveNonEmptyTimeline = currentTimeline != null && !currentTimeline.isEmpty();
  boolean isSeekable = false;
  boolean enablePrevious = false;
  boolean enableNext = false;
  if (haveNonEmptyTimeline) {
    int currentWindowIndex = player.getCurrentWindowIndex();
    currentTimeline.getWindow(currentWindowIndex, currentWindow);
    isSeekable = currentWindow.isSeekable;
    enablePrevious = currentWindowIndex > 0 || isSeekable || !currentWindow.isDynamic;
    enableNext = (currentWindowIndex < currentTimeline.getWindowCount() - 1)
        || currentWindow.isDynamic;
  }
  setButtonEnabled(enablePrevious , previousButton);
  setButtonEnabled(enableNext, nextButton);
  setButtonEnabled(fastForwardMs > 0 && isSeekable, fastForwardButton);
  setButtonEnabled(rewindMs > 0 && isSeekable, rewindButton);
  if (progressBar != null) {
    progressBar.setEnabled(isSeekable);
  }
}
 
Example 3
Source File: GSYExo2MediaPlayer.java    From GSYVideoPlayer with Apache License 2.0 6 votes vote down vote up
/**
 * 下一集
 */
public void next() {
    if (mInternalPlayer == null) {
        return;
    }
    Timeline timeline = mInternalPlayer.getCurrentTimeline();
    if (timeline.isEmpty()) {
        return;
    }
    int windowIndex = mInternalPlayer.getCurrentWindowIndex();
    int nextWindowIndex = mInternalPlayer.getNextWindowIndex();
    if (nextWindowIndex != C.INDEX_UNSET) {
        mInternalPlayer.seekTo(nextWindowIndex, C.TIME_UNSET);
    } else if (timeline.getWindow(windowIndex, window, false).isDynamic) {
        mInternalPlayer.seekTo(windowIndex, C.TIME_UNSET);
    }
}
 
Example 4
Source File: CustomizeControlView.java    From bcm-android with GNU General Public License v3.0 6 votes vote down vote up
private void seekToTimeBarPosition(long positionMs) {
    int windowIndex;
    Timeline timeline = player.getCurrentTimeline();
    if (multiWindowTimeBar && !timeline.isEmpty()) {
        int windowCount = timeline.getWindowCount();
        windowIndex = 0;
        while (true) {
            long windowDurationMs = timeline.getWindow(windowIndex, window).getDurationMs();
            if (positionMs < windowDurationMs) {
                break;
            } else if (windowIndex == windowCount - 1) {
                // Seeking past the end of the last window should seek to the end of the timeline.
                positionMs = windowDurationMs;
                break;
            }
            positionMs -= windowDurationMs;
            windowIndex++;
        }
    } else {
        windowIndex = player.getCurrentWindowIndex();
    }
    seekTo(windowIndex, positionMs);
}
 
Example 5
Source File: GSYExo2MediaPlayer.java    From GSYVideoPlayer with Apache License 2.0 6 votes vote down vote up
/**
 * 上一集
 */
public void previous() {
    if (mInternalPlayer == null) {
        return;
    }
    Timeline timeline = mInternalPlayer.getCurrentTimeline();
    if (timeline.isEmpty()) {
        return;
    }
    int windowIndex = mInternalPlayer.getCurrentWindowIndex();
    timeline.getWindow(windowIndex, window);
    int previousWindowIndex = mInternalPlayer.getPreviousWindowIndex();
    if (previousWindowIndex != C.INDEX_UNSET
            && (mInternalPlayer.getCurrentPosition() <= MAX_POSITION_FOR_SEEK_TO_PREVIOUS
            || (window.isDynamic && !window.isSeekable))) {
        mInternalPlayer.seekTo(previousWindowIndex, C.TIME_UNSET);
    } else {
        mInternalPlayer.seekTo(0);
    }
}
 
Example 6
Source File: ExoVideoPlaybackControlView.java    From ExoVideoView with Apache License 2.0 6 votes vote down vote up
private void seekToTimeBarPosition(long positionMs) {
    int windowIndex;
    Timeline timeline = player.getCurrentTimeline();
    if (multiWindowTimeBar && !timeline.isEmpty()) {
        int windowCount = timeline.getWindowCount();
        windowIndex = 0;
        while (true) {
            long windowDurationMs = timeline.getWindow(windowIndex, window).getDurationMs();
            if (positionMs < windowDurationMs) {
                break;
            } else if (windowIndex == windowCount - 1) {
                // Seeking past the end of the last window should seek to the end of the timeline.
                positionMs = windowDurationMs;
                break;
            }
            positionMs -= windowDurationMs;
            windowIndex++;
        }
    } else {
        windowIndex = player.getCurrentWindowIndex();
    }
    seekTo(windowIndex, positionMs);
}
 
Example 7
Source File: PlaybackControlView.java    From K-Sonic with MIT License 5 votes vote down vote up
private void next() {
  Timeline currentTimeline = player.getCurrentTimeline();
  if (currentTimeline.isEmpty()) {
    return;
  }
  int currentWindowIndex = player.getCurrentWindowIndex();
  if (currentWindowIndex < currentTimeline.getWindowCount() - 1) {
    seekTo(currentWindowIndex + 1, C.TIME_UNSET);
  } else if (currentTimeline.getWindow(currentWindowIndex, currentWindow, false).isDynamic) {
    seekTo(currentWindowIndex, C.TIME_UNSET);
  }
}
 
Example 8
Source File: PlaybackControlView.java    From K-Sonic with MIT License 5 votes vote down vote up
private void previous() {
  Timeline currentTimeline = player.getCurrentTimeline();
  if (currentTimeline.isEmpty()) {
    return;
  }
  int currentWindowIndex = player.getCurrentWindowIndex();
  currentTimeline.getWindow(currentWindowIndex, currentWindow);
  if (currentWindowIndex > 0 && (player.getCurrentPosition() <= MAX_POSITION_FOR_SEEK_TO_PREVIOUS
      || (currentWindow.isDynamic && !currentWindow.isSeekable))) {
    seekTo(currentWindowIndex - 1, C.TIME_UNSET);
  } else {
    seekTo(0);
  }
}
 
Example 9
Source File: ExoVideoPlaybackControlView.java    From ExoVideoView with Apache License 2.0 5 votes vote down vote up
private void next() {
    Timeline timeline = player.getCurrentTimeline();
    if (timeline.isEmpty()) {
        return;
    }
    int windowIndex = player.getCurrentWindowIndex();
    int nextWindowIndex = player.getNextWindowIndex();
    if (nextWindowIndex != C.INDEX_UNSET) {
        seekTo(nextWindowIndex, C.TIME_UNSET);
    } else if (timeline.getWindow(windowIndex, window, false).isDynamic) {
        seekTo(windowIndex, C.TIME_UNSET);
    }
}
 
Example 10
Source File: ExoVideoPlaybackControlView.java    From ExoVideoView with Apache License 2.0 5 votes vote down vote up
private void updateNavigation() {
    if (!isVisible() || !isAttachedToWindow) {
        return;
    }
    Timeline timeline = player != null ? player.getCurrentTimeline() : null;
    boolean haveNonEmptyTimeline = timeline != null && !timeline.isEmpty();
    boolean isSeekable = false;
    boolean enablePrevious = false;
    boolean enableNext = false;
    if (haveNonEmptyTimeline && !player.isPlayingAd()) {
        int windowIndex = player.getCurrentWindowIndex();
        timeline.getWindow(windowIndex, window);
        isSeekable = window.isSeekable;
        enablePrevious = isSeekable || !window.isDynamic
                || player.getPreviousWindowIndex() != C.INDEX_UNSET;
        enableNext = window.isDynamic || player.getNextWindowIndex() != C.INDEX_UNSET;
    }
    setButtonEnabled(enablePrevious, previousButton);
    setButtonEnabled(enableNext, nextButton);
    setButtonEnabled(fastForwardMs > 0 && isSeekable, fastForwardButton);
    setButtonEnabled(rewindMs > 0 && isSeekable, rewindButton);
    if (timeBar != null) {
        timeBar.setEnabled(isSeekable && !isHls);
    }
    if (timeBarLandscape != null) {
        timeBarLandscape.setEnabled(isSeekable && !isHls);
    }
}
 
Example 11
Source File: VideoGesture.java    From ExoVideoView with Apache License 2.0 5 votes vote down vote up
private boolean canSeek() {

        Player player = playerAccessor.attachPlayer();

        Timeline timeline = player != null ? player.getCurrentTimeline() : null;
        boolean haveNonEmptyTimeline = timeline != null && !timeline.isEmpty();
        boolean isSeekable = false;
        if (haveNonEmptyTimeline) {
            int windowIndex = player.getCurrentWindowIndex();
            timeline.getWindow(windowIndex, window);
            isSeekable = window.isSeekable;
        }

        return isSeekable;
    }
 
Example 12
Source File: AnalyticsCollector.java    From MediaSDK with Apache License 2.0 5 votes vote down vote up
/** Returns a new {@link EventTime} for the specified timeline, window and media period id. */
@RequiresNonNull("player")
protected EventTime generateEventTime(
    Timeline timeline, int windowIndex, @Nullable MediaPeriodId mediaPeriodId) {
  if (timeline.isEmpty()) {
    // Ensure media period id is only reported together with a valid timeline.
    mediaPeriodId = null;
  }
  long realtimeMs = clock.elapsedRealtime();
  long eventPositionMs;
  boolean isInCurrentWindow =
      timeline == player.getCurrentTimeline() && windowIndex == player.getCurrentWindowIndex();
  if (mediaPeriodId != null && mediaPeriodId.isAd()) {
    boolean isCurrentAd =
        isInCurrentWindow
            && player.getCurrentAdGroupIndex() == mediaPeriodId.adGroupIndex
            && player.getCurrentAdIndexInAdGroup() == mediaPeriodId.adIndexInAdGroup;
    // Assume start position of 0 for future ads.
    eventPositionMs = isCurrentAd ? player.getCurrentPosition() : 0;
  } else if (isInCurrentWindow) {
    eventPositionMs = player.getContentPosition();
  } else {
    // Assume default start position for future content windows. If timeline is not available yet,
    // assume start position of 0.
    eventPositionMs =
        timeline.isEmpty() ? 0 : timeline.getWindow(windowIndex, window).getDefaultPositionMs();
  }
  return new EventTime(
      realtimeMs,
      timeline,
      windowIndex,
      mediaPeriodId,
      eventPositionMs,
      player.getCurrentPosition(),
      player.getTotalBufferedDuration());
}
 
Example 13
Source File: AnalyticsCollector.java    From TelePlus-Android with GNU General Public License v2.0 5 votes vote down vote up
private WindowAndMediaPeriodId updateMediaPeriodToNewTimeline(
    WindowAndMediaPeriodId mediaPeriod, Timeline newTimeline) {
  if (newTimeline.isEmpty() || timeline.isEmpty()) {
    return mediaPeriod;
  }
  Object uid = timeline.getUidOfPeriod(mediaPeriod.mediaPeriodId.periodIndex);
  int newPeriodIndex = newTimeline.getIndexOfPeriod(uid);
  if (newPeriodIndex == C.INDEX_UNSET) {
    return mediaPeriod;
  }
  int newWindowIndex = newTimeline.getPeriod(newPeriodIndex, period).windowIndex;
  return new WindowAndMediaPeriodId(
      newWindowIndex, mediaPeriod.mediaPeriodId.copyWithPeriodIndex(newPeriodIndex));
}
 
Example 14
Source File: ConcatenatingMediaSource.java    From TelePlus-Android with GNU General Public License v2.0 5 votes vote down vote up
private void updateMediaSourceInternal(MediaSourceHolder mediaSourceHolder, Timeline timeline) {
  if (mediaSourceHolder == null) {
    throw new IllegalArgumentException();
  }
  DeferredTimeline deferredTimeline = mediaSourceHolder.timeline;
  if (deferredTimeline.getTimeline() == timeline) {
    return;
  }
  int windowOffsetUpdate = timeline.getWindowCount() - deferredTimeline.getWindowCount();
  int periodOffsetUpdate = timeline.getPeriodCount() - deferredTimeline.getPeriodCount();
  if (windowOffsetUpdate != 0 || periodOffsetUpdate != 0) {
    correctOffsets(
        mediaSourceHolder.childIndex + 1,
        /* childIndexUpdate= */ 0,
        windowOffsetUpdate,
        periodOffsetUpdate);
  }
  mediaSourceHolder.timeline = deferredTimeline.cloneWithNewTimeline(timeline);
  if (!mediaSourceHolder.isPrepared && !timeline.isEmpty()) {
    timeline.getWindow(/* windowIndex= */ 0, window);
    long defaultPeriodPositionUs =
        window.getPositionInFirstPeriodUs() + window.getDefaultPositionUs();
    for (int i = 0; i < mediaSourceHolder.activeMediaPeriods.size(); i++) {
      DeferredMediaPeriod deferredMediaPeriod = mediaSourceHolder.activeMediaPeriods.get(i);
      deferredMediaPeriod.setDefaultPreparePositionUs(defaultPeriodPositionUs);
      MediaPeriodId idInSource =
          deferredMediaPeriod.id.copyWithPeriodIndex(
              deferredMediaPeriod.id.periodIndex - mediaSourceHolder.firstPeriodIndexInChild);
      deferredMediaPeriod.createPeriod(idInSource);
    }
    mediaSourceHolder.isPrepared = true;
  }
  scheduleListenerNotification(/* actionOnCompletion= */ null);
}
 
Example 15
Source File: AnalyticsCollector.java    From TelePlus-Android with GNU General Public License v2.0 5 votes vote down vote up
private WindowAndMediaPeriodId updateMediaPeriodToNewTimeline(
    WindowAndMediaPeriodId mediaPeriod, Timeline newTimeline) {
  if (newTimeline.isEmpty() || timeline.isEmpty()) {
    return mediaPeriod;
  }
  Object uid = timeline.getUidOfPeriod(mediaPeriod.mediaPeriodId.periodIndex);
  int newPeriodIndex = newTimeline.getIndexOfPeriod(uid);
  if (newPeriodIndex == C.INDEX_UNSET) {
    return mediaPeriod;
  }
  int newWindowIndex = newTimeline.getPeriod(newPeriodIndex, period).windowIndex;
  return new WindowAndMediaPeriodId(
      newWindowIndex, mediaPeriod.mediaPeriodId.copyWithPeriodIndex(newPeriodIndex));
}
 
Example 16
Source File: AnalyticsCollector.java    From Telegram with GNU General Public License v2.0 5 votes vote down vote up
/** Returns a new {@link EventTime} for the specified timeline, window and media period id. */
@RequiresNonNull("player")
protected EventTime generateEventTime(
    Timeline timeline, int windowIndex, @Nullable MediaPeriodId mediaPeriodId) {
  if (timeline.isEmpty()) {
    // Ensure media period id is only reported together with a valid timeline.
    mediaPeriodId = null;
  }
  long realtimeMs = clock.elapsedRealtime();
  long eventPositionMs;
  boolean isInCurrentWindow =
      timeline == player.getCurrentTimeline() && windowIndex == player.getCurrentWindowIndex();
  if (mediaPeriodId != null && mediaPeriodId.isAd()) {
    boolean isCurrentAd =
        isInCurrentWindow
            && player.getCurrentAdGroupIndex() == mediaPeriodId.adGroupIndex
            && player.getCurrentAdIndexInAdGroup() == mediaPeriodId.adIndexInAdGroup;
    // Assume start position of 0 for future ads.
    eventPositionMs = isCurrentAd ? player.getCurrentPosition() : 0;
  } else if (isInCurrentWindow) {
    eventPositionMs = player.getContentPosition();
  } else {
    // Assume default start position for future content windows. If timeline is not available yet,
    // assume start position of 0.
    eventPositionMs =
        timeline.isEmpty() ? 0 : timeline.getWindow(windowIndex, window).getDefaultPositionMs();
  }
  return new EventTime(
      realtimeMs,
      timeline,
      windowIndex,
      mediaPeriodId,
      eventPositionMs,
      player.getCurrentPosition(),
      player.getTotalBufferedDuration());
}
 
Example 17
Source File: CustomizeControlView.java    From bcm-android with GNU General Public License v3.0 5 votes vote down vote up
private void next() {
    Timeline timeline = player.getCurrentTimeline();
    if (timeline.isEmpty()) {
        return;
    }
    int windowIndex = player.getCurrentWindowIndex();
    int nextWindowIndex = player.getNextWindowIndex();
    if (nextWindowIndex != C.INDEX_UNSET) {
        seekTo(nextWindowIndex, C.TIME_UNSET);
    } else if (timeline.getWindow(windowIndex, window, false).isDynamic) {
        seekTo(windowIndex, C.TIME_UNSET);
    }
}
 
Example 18
Source File: ConcatenatingMediaSource.java    From Telegram-FOSS with GNU General Public License v2.0 4 votes vote down vote up
private void updateMediaSourceInternal(MediaSourceHolder mediaSourceHolder, Timeline timeline) {
  if (mediaSourceHolder == null) {
    throw new IllegalArgumentException();
  }
  DeferredTimeline deferredTimeline = mediaSourceHolder.timeline;
  if (deferredTimeline.getTimeline() == timeline) {
    return;
  }
  int windowOffsetUpdate = timeline.getWindowCount() - deferredTimeline.getWindowCount();
  int periodOffsetUpdate = timeline.getPeriodCount() - deferredTimeline.getPeriodCount();
  if (windowOffsetUpdate != 0 || periodOffsetUpdate != 0) {
    correctOffsets(
        mediaSourceHolder.childIndex + 1,
        /* childIndexUpdate= */ 0,
        windowOffsetUpdate,
        periodOffsetUpdate);
  }
  if (mediaSourceHolder.isPrepared) {
    mediaSourceHolder.timeline = deferredTimeline.cloneWithUpdatedTimeline(timeline);
  } else if (timeline.isEmpty()) {
    mediaSourceHolder.timeline =
        DeferredTimeline.createWithRealTimeline(timeline, DeferredTimeline.DUMMY_ID);
  } else {
    // We should have at most one deferred media period for the DummyTimeline because the duration
    // is unset and we don't load beyond periods with unset duration. We need to figure out how to
    // handle the prepare positions of multiple deferred media periods, should that ever change.
    Assertions.checkState(mediaSourceHolder.activeMediaPeriods.size() <= 1);
    DeferredMediaPeriod deferredMediaPeriod =
        mediaSourceHolder.activeMediaPeriods.isEmpty()
            ? null
            : mediaSourceHolder.activeMediaPeriods.get(0);
    // Determine first period and the start position.
    // This will be:
    //  1. The default window start position if no deferred period has been created yet.
    //  2. The non-zero prepare position of the deferred period under the assumption that this is
    //     a non-zero initial seek position in the window.
    //  3. The default window start position if the deferred period has a prepare position of zero
    //     under the assumption that the prepare position of zero was used because it's the
    //     default position of the DummyTimeline window. Note that this will override an
    //     intentional seek to zero for a window with a non-zero default position. This is
    //     unlikely to be a problem as a non-zero default position usually only occurs for live
    //     playbacks and seeking to zero in a live window would cause BehindLiveWindowExceptions
    //     anyway.
    timeline.getWindow(/* windowIndex= */ 0, window);
    long windowStartPositionUs = window.getDefaultPositionUs();
    if (deferredMediaPeriod != null) {
      long periodPreparePositionUs = deferredMediaPeriod.getPreparePositionUs();
      if (periodPreparePositionUs != 0) {
        windowStartPositionUs = periodPreparePositionUs;
      }
    }
    Pair<Object, Long> periodPosition =
        timeline.getPeriodPosition(window, period, /* windowIndex= */ 0, windowStartPositionUs);
    Object periodUid = periodPosition.first;
    long periodPositionUs = periodPosition.second;
    mediaSourceHolder.timeline = DeferredTimeline.createWithRealTimeline(timeline, periodUid);
    if (deferredMediaPeriod != null) {
      deferredMediaPeriod.overridePreparePositionUs(periodPositionUs);
      MediaPeriodId idInSource =
          deferredMediaPeriod.id.copyWithPeriodUid(
              getChildPeriodUid(mediaSourceHolder, deferredMediaPeriod.id.periodUid));
      deferredMediaPeriod.createPeriod(idInSource);
    }
  }
  mediaSourceHolder.isPrepared = true;
  scheduleTimelineUpdate();
}
 
Example 19
Source File: ConcatenatingMediaSource.java    From Telegram with GNU General Public License v2.0 4 votes vote down vote up
private void updateMediaSourceInternal(MediaSourceHolder mediaSourceHolder, Timeline timeline) {
  if (mediaSourceHolder == null) {
    throw new IllegalArgumentException();
  }
  DeferredTimeline deferredTimeline = mediaSourceHolder.timeline;
  if (deferredTimeline.getTimeline() == timeline) {
    return;
  }
  int windowOffsetUpdate = timeline.getWindowCount() - deferredTimeline.getWindowCount();
  int periodOffsetUpdate = timeline.getPeriodCount() - deferredTimeline.getPeriodCount();
  if (windowOffsetUpdate != 0 || periodOffsetUpdate != 0) {
    correctOffsets(
        mediaSourceHolder.childIndex + 1,
        /* childIndexUpdate= */ 0,
        windowOffsetUpdate,
        periodOffsetUpdate);
  }
  if (mediaSourceHolder.isPrepared) {
    mediaSourceHolder.timeline = deferredTimeline.cloneWithUpdatedTimeline(timeline);
  } else if (timeline.isEmpty()) {
    mediaSourceHolder.timeline =
        DeferredTimeline.createWithRealTimeline(timeline, DeferredTimeline.DUMMY_ID);
  } else {
    // We should have at most one deferred media period for the DummyTimeline because the duration
    // is unset and we don't load beyond periods with unset duration. We need to figure out how to
    // handle the prepare positions of multiple deferred media periods, should that ever change.
    Assertions.checkState(mediaSourceHolder.activeMediaPeriods.size() <= 1);
    DeferredMediaPeriod deferredMediaPeriod =
        mediaSourceHolder.activeMediaPeriods.isEmpty()
            ? null
            : mediaSourceHolder.activeMediaPeriods.get(0);
    // Determine first period and the start position.
    // This will be:
    //  1. The default window start position if no deferred period has been created yet.
    //  2. The non-zero prepare position of the deferred period under the assumption that this is
    //     a non-zero initial seek position in the window.
    //  3. The default window start position if the deferred period has a prepare position of zero
    //     under the assumption that the prepare position of zero was used because it's the
    //     default position of the DummyTimeline window. Note that this will override an
    //     intentional seek to zero for a window with a non-zero default position. This is
    //     unlikely to be a problem as a non-zero default position usually only occurs for live
    //     playbacks and seeking to zero in a live window would cause BehindLiveWindowExceptions
    //     anyway.
    timeline.getWindow(/* windowIndex= */ 0, window);
    long windowStartPositionUs = window.getDefaultPositionUs();
    if (deferredMediaPeriod != null) {
      long periodPreparePositionUs = deferredMediaPeriod.getPreparePositionUs();
      if (periodPreparePositionUs != 0) {
        windowStartPositionUs = periodPreparePositionUs;
      }
    }
    Pair<Object, Long> periodPosition =
        timeline.getPeriodPosition(window, period, /* windowIndex= */ 0, windowStartPositionUs);
    Object periodUid = periodPosition.first;
    long periodPositionUs = periodPosition.second;
    mediaSourceHolder.timeline = DeferredTimeline.createWithRealTimeline(timeline, periodUid);
    if (deferredMediaPeriod != null) {
      deferredMediaPeriod.overridePreparePositionUs(periodPositionUs);
      MediaPeriodId idInSource =
          deferredMediaPeriod.id.copyWithPeriodUid(
              getChildPeriodUid(mediaSourceHolder, deferredMediaPeriod.id.periodUid));
      deferredMediaPeriod.createPeriod(idInSource);
    }
  }
  mediaSourceHolder.isPrepared = true;
  scheduleTimelineUpdate();
}
 
Example 20
Source File: MaskingMediaSource.java    From MediaSDK with Apache License 2.0 4 votes vote down vote up
@Override
protected void onChildSourceInfoRefreshed(
    Void id, MediaSource mediaSource, Timeline newTimeline) {
  if (isPrepared) {
    timeline = timeline.cloneWithUpdatedTimeline(newTimeline);
  } else if (newTimeline.isEmpty()) {
    timeline =
        MaskingTimeline.createWithRealTimeline(
            newTimeline, Window.SINGLE_WINDOW_UID, MaskingTimeline.DUMMY_EXTERNAL_PERIOD_UID);
  } else {
    // Determine first period and the start position.
    // This will be:
    //  1. The default window start position if no deferred period has been created yet.
    //  2. The non-zero prepare position of the deferred period under the assumption that this is
    //     a non-zero initial seek position in the window.
    //  3. The default window start position if the deferred period has a prepare position of zero
    //     under the assumption that the prepare position of zero was used because it's the
    //     default position of the DummyTimeline window. Note that this will override an
    //     intentional seek to zero for a window with a non-zero default position. This is
    //     unlikely to be a problem as a non-zero default position usually only occurs for live
    //     playbacks and seeking to zero in a live window would cause BehindLiveWindowExceptions
    //     anyway.
    newTimeline.getWindow(/* windowIndex= */ 0, window);
    long windowStartPositionUs = window.getDefaultPositionUs();
    if (unpreparedMaskingMediaPeriod != null) {
      long periodPreparePositionUs = unpreparedMaskingMediaPeriod.getPreparePositionUs();
      if (periodPreparePositionUs != 0) {
        windowStartPositionUs = periodPreparePositionUs;
      }
    }
    Object windowUid = window.uid;
    Pair<Object, Long> periodPosition =
        newTimeline.getPeriodPosition(
            window, period, /* windowIndex= */ 0, windowStartPositionUs);
    Object periodUid = periodPosition.first;
    long periodPositionUs = periodPosition.second;
    timeline = MaskingTimeline.createWithRealTimeline(newTimeline, windowUid, periodUid);
    if (unpreparedMaskingMediaPeriod != null) {
      MaskingMediaPeriod maskingPeriod = unpreparedMaskingMediaPeriod;
      maskingPeriod.overridePreparePositionUs(periodPositionUs);
      MediaPeriodId idInSource =
          maskingPeriod.id.copyWithPeriodUid(getInternalPeriodUid(maskingPeriod.id.periodUid));
      maskingPeriod.createPeriod(idInSource);
    }
  }
  isPrepared = true;
  refreshSourceInfo(this.timeline);
}