Java Code Examples for com.google.android.exoplayer2.source.MediaSource.MediaPeriodId#equals()

The following examples show how to use com.google.android.exoplayer2.source.MediaSource.MediaPeriodId#equals() . 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: AnalyticsCollector.java    From MediaSDK with Apache License 2.0 6 votes vote down vote up
/**
 * Updates the queue with a released media period. Returns whether the media period was still in
 * the queue.
 */
public boolean onMediaPeriodReleased(MediaPeriodId mediaPeriodId) {
  MediaPeriodInfo mediaPeriodInfo = mediaPeriodIdToInfo.remove(mediaPeriodId);
  if (mediaPeriodInfo == null) {
    // The media period has already been removed from the queue in resetForNewMediaSource().
    return false;
  }
  mediaPeriodInfoQueue.remove(mediaPeriodInfo);
  if (readingMediaPeriod != null && mediaPeriodId.equals(readingMediaPeriod.mediaPeriodId)) {
    readingMediaPeriod = mediaPeriodInfoQueue.isEmpty() ? null : mediaPeriodInfoQueue.get(0);
  }
  if (!mediaPeriodInfoQueue.isEmpty()) {
    lastPlayingMediaPeriod = mediaPeriodInfoQueue.get(0);
  }
  return true;
}
 
Example 2
Source File: AnalyticsCollector.java    From Telegram-FOSS with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Updates the queue with a released media period. Returns whether the media period was still in
 * the queue.
 */
public boolean onMediaPeriodReleased(MediaPeriodId mediaPeriodId) {
  MediaPeriodInfo mediaPeriodInfo = mediaPeriodIdToInfo.remove(mediaPeriodId);
  if (mediaPeriodInfo == null) {
    // The media period has already been removed from the queue in resetForNewMediaSource().
    return false;
  }
  mediaPeriodInfoQueue.remove(mediaPeriodInfo);
  if (readingMediaPeriod != null && mediaPeriodId.equals(readingMediaPeriod.mediaPeriodId)) {
    readingMediaPeriod = mediaPeriodInfoQueue.isEmpty() ? null : mediaPeriodInfoQueue.get(0);
  }
  return true;
}
 
Example 3
Source File: AnalyticsCollector.java    From Telegram with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Updates the queue with a released media period. Returns whether the media period was still in
 * the queue.
 */
public boolean onMediaPeriodReleased(MediaPeriodId mediaPeriodId) {
  MediaPeriodInfo mediaPeriodInfo = mediaPeriodIdToInfo.remove(mediaPeriodId);
  if (mediaPeriodInfo == null) {
    // The media period has already been removed from the queue in resetForNewMediaSource().
    return false;
  }
  mediaPeriodInfoQueue.remove(mediaPeriodInfo);
  if (readingMediaPeriod != null && mediaPeriodId.equals(readingMediaPeriod.mediaPeriodId)) {
    readingMediaPeriod = mediaPeriodInfoQueue.isEmpty() ? null : mediaPeriodInfoQueue.get(0);
  }
  return true;
}
 
Example 4
Source File: ExoPlayerImplInternal.java    From MediaSDK with Apache License 2.0 4 votes vote down vote up
private void seekToInternal(SeekPosition seekPosition) throws ExoPlaybackException {
  playbackInfoUpdate.incrementPendingOperationAcks(/* operationAcks= */ 1);

  MediaPeriodId periodId;
  long periodPositionUs;
  long contentPositionUs;
  boolean seekPositionAdjusted;
  Pair<Object, Long> resolvedSeekPosition =
      resolveSeekPosition(seekPosition, /* trySubsequentPeriods= */ true);
  if (resolvedSeekPosition == null) {
    // The seek position was valid for the timeline that it was performed into, but the
    // timeline has changed or is not ready and a suitable seek position could not be resolved.
    periodId = playbackInfo.getDummyFirstMediaPeriodId(shuffleModeEnabled, window, period);
    periodPositionUs = C.TIME_UNSET;
    contentPositionUs = C.TIME_UNSET;
    seekPositionAdjusted = true;
  } else {
    // Update the resolved seek position to take ads into account.
    Object periodUid = resolvedSeekPosition.first;
    contentPositionUs = resolvedSeekPosition.second;
    periodId = queue.resolveMediaPeriodIdForAds(periodUid, contentPositionUs);
    if (periodId.isAd()) {
      periodPositionUs = 0;
      seekPositionAdjusted = true;
    } else {
      periodPositionUs = resolvedSeekPosition.second;
      seekPositionAdjusted = seekPosition.windowPositionUs == C.TIME_UNSET;
    }
  }

  try {
    if (mediaSource == null || pendingPrepareCount > 0) {
      // Save seek position for later, as we are still waiting for a prepared source.
      pendingInitialSeekPosition = seekPosition;
    } else if (periodPositionUs == C.TIME_UNSET) {
      // End playback, as we didn't manage to find a valid seek position.
      setState(Player.STATE_ENDED);
      resetInternal(
          /* resetRenderers= */ false,
          /* releaseMediaSource= */ false,
          /* resetPosition= */ true,
          /* resetState= */ false,
          /* resetError= */ true);
    } else {
      // Execute the seek in the current media periods.
      long newPeriodPositionUs = periodPositionUs;
      if (periodId.equals(playbackInfo.periodId)) {
        MediaPeriodHolder playingPeriodHolder = queue.getPlayingPeriod();
        if (playingPeriodHolder != null
            && playingPeriodHolder.prepared
            && newPeriodPositionUs != 0) {
          newPeriodPositionUs =
              playingPeriodHolder.mediaPeriod.getAdjustedSeekPositionUs(
                  newPeriodPositionUs, seekParameters);
        }
        if (C.usToMs(newPeriodPositionUs) == C.usToMs(playbackInfo.positionUs)) {
          // Seek will be performed to the current position. Do nothing.
          periodPositionUs = playbackInfo.positionUs;
          return;
        }
      }
      newPeriodPositionUs = seekToPeriodPosition(periodId, newPeriodPositionUs);
      seekPositionAdjusted |= periodPositionUs != newPeriodPositionUs;
      periodPositionUs = newPeriodPositionUs;
    }
  } finally {
    playbackInfo = copyWithNewPosition(periodId, periodPositionUs, contentPositionUs);
    if (seekPositionAdjusted) {
      playbackInfoUpdate.setPositionDiscontinuity(Player.DISCONTINUITY_REASON_SEEK_ADJUSTMENT);
    }
  }
}
 
Example 5
Source File: ExoPlayerImplInternal.java    From MediaSDK with Apache License 2.0 4 votes vote down vote up
private long seekToPeriodPosition(
    MediaPeriodId periodId, long periodPositionUs, boolean forceDisableRenderers)
    throws ExoPlaybackException {
  stopRenderers();
  rebuffering = false;
  if (playbackInfo.playbackState != Player.STATE_IDLE && !playbackInfo.timeline.isEmpty()) {
    setState(Player.STATE_BUFFERING);
  }

  // Clear the timeline, but keep the requested period if it is already prepared.
  MediaPeriodHolder oldPlayingPeriodHolder = queue.getPlayingPeriod();
  MediaPeriodHolder newPlayingPeriodHolder = oldPlayingPeriodHolder;
  while (newPlayingPeriodHolder != null) {
    if (periodId.equals(newPlayingPeriodHolder.info.id) && newPlayingPeriodHolder.prepared) {
      queue.removeAfter(newPlayingPeriodHolder);
      break;
    }
    newPlayingPeriodHolder = queue.advancePlayingPeriod();
  }

  // Disable all renderers if the period being played is changing, if the seek results in negative
  // renderer timestamps, or if forced.
  if (forceDisableRenderers
      || oldPlayingPeriodHolder != newPlayingPeriodHolder
      || (newPlayingPeriodHolder != null
          && newPlayingPeriodHolder.toRendererTime(periodPositionUs) < 0)) {
    for (Renderer renderer : enabledRenderers) {
      disableRenderer(renderer);
    }
    enabledRenderers = new Renderer[0];
    oldPlayingPeriodHolder = null;
    if (newPlayingPeriodHolder != null) {
      newPlayingPeriodHolder.setRendererOffset(/* rendererPositionOffsetUs= */ 0);
    }
  }

  // Update the holders.
  if (newPlayingPeriodHolder != null) {
    updatePlayingPeriodRenderers(oldPlayingPeriodHolder);
    if (newPlayingPeriodHolder.hasEnabledTracks) {
      periodPositionUs = newPlayingPeriodHolder.mediaPeriod.seekToUs(periodPositionUs);
      newPlayingPeriodHolder.mediaPeriod.discardBuffer(
          periodPositionUs - backBufferDurationUs, retainBackBufferFromKeyframe);
    }
    resetRendererPosition(periodPositionUs);
    maybeContinueLoading();
  } else {
    queue.clear(/* keepFrontPeriodUid= */ true);
    // New period has not been prepared.
    playbackInfo =
        playbackInfo.copyWithTrackInfo(TrackGroupArray.EMPTY, emptyTrackSelectorResult);
    resetRendererPosition(periodPositionUs);
  }

  handleLoadingMediaPeriodChanged(/* loadingTrackSelectionChanged= */ false);
  handler.sendEmptyMessage(MSG_DO_SOME_WORK);
  return periodPositionUs;
}
 
Example 6
Source File: ExoPlayerImplInternal.java    From TelePlus-Android with GNU General Public License v2.0 4 votes vote down vote up
private void seekToInternal(SeekPosition seekPosition) throws ExoPlaybackException {
  playbackInfoUpdate.incrementPendingOperationAcks(/* operationAcks= */ 1);

  MediaPeriodId periodId;
  long periodPositionUs;
  long contentPositionUs;
  boolean seekPositionAdjusted;
  Pair<Integer, Long> resolvedSeekPosition =
      resolveSeekPosition(seekPosition, /* trySubsequentPeriods= */ true);
  if (resolvedSeekPosition == null) {
    // The seek position was valid for the timeline that it was performed into, but the
    // timeline has changed or is not ready and a suitable seek position could not be resolved.
    periodId = getFirstMediaPeriodId();
    periodPositionUs = C.TIME_UNSET;
    contentPositionUs = C.TIME_UNSET;
    seekPositionAdjusted = true;
  } else {
    // Update the resolved seek position to take ads into account.
    int periodIndex = resolvedSeekPosition.first;
    contentPositionUs = resolvedSeekPosition.second;
    periodId = queue.resolveMediaPeriodIdForAds(periodIndex, contentPositionUs);
    if (periodId.isAd()) {
      periodPositionUs = 0;
      seekPositionAdjusted = true;
    } else {
      periodPositionUs = resolvedSeekPosition.second;
      seekPositionAdjusted = seekPosition.windowPositionUs == C.TIME_UNSET;
    }
  }

  try {
    if (mediaSource == null || pendingPrepareCount > 0) {
      // Save seek position for later, as we are still waiting for a prepared source.
      pendingInitialSeekPosition = seekPosition;
    } else if (periodPositionUs == C.TIME_UNSET) {
      // End playback, as we didn't manage to find a valid seek position.
      setState(Player.STATE_ENDED);
      resetInternal(
          /* releaseMediaSource= */ false, /* resetPosition= */ true, /* resetState= */ false);
    } else {
      // Execute the seek in the current media periods.
      long newPeriodPositionUs = periodPositionUs;
      if (periodId.equals(playbackInfo.periodId)) {
        MediaPeriodHolder playingPeriodHolder = queue.getPlayingPeriod();
        if (playingPeriodHolder != null && newPeriodPositionUs != 0) {
          newPeriodPositionUs =
              playingPeriodHolder.mediaPeriod.getAdjustedSeekPositionUs(
                  newPeriodPositionUs, seekParameters);
        }
        if (C.usToMs(newPeriodPositionUs) == C.usToMs(playbackInfo.positionUs)) {
          // Seek will be performed to the current position. Do nothing.
          periodPositionUs = playbackInfo.positionUs;
          return;
        }
      }
      newPeriodPositionUs = seekToPeriodPosition(periodId, newPeriodPositionUs);
      seekPositionAdjusted |= periodPositionUs != newPeriodPositionUs;
      periodPositionUs = newPeriodPositionUs;
    }
  } finally {
    playbackInfo = playbackInfo.fromNewPosition(periodId, periodPositionUs, contentPositionUs);
    if (seekPositionAdjusted) {
      playbackInfoUpdate.setPositionDiscontinuity(Player.DISCONTINUITY_REASON_SEEK_ADJUSTMENT);
    }
  }
}
 
Example 7
Source File: ExoPlayerImplInternal.java    From TelePlus-Android with GNU General Public License v2.0 4 votes vote down vote up
private long seekToPeriodPosition(
    MediaPeriodId periodId, long periodPositionUs, boolean forceDisableRenderers)
    throws ExoPlaybackException {
  stopRenderers();
  rebuffering = false;
  setState(Player.STATE_BUFFERING);

  // Clear the timeline, but keep the requested period if it is already prepared.
  MediaPeriodHolder oldPlayingPeriodHolder = queue.getPlayingPeriod();
  MediaPeriodHolder newPlayingPeriodHolder = oldPlayingPeriodHolder;
  while (newPlayingPeriodHolder != null) {
    if (periodId.equals(newPlayingPeriodHolder.info.id) && newPlayingPeriodHolder.prepared) {
      queue.removeAfter(newPlayingPeriodHolder);
      break;
    }
    newPlayingPeriodHolder = queue.advancePlayingPeriod();
  }

  // Disable all the renderers if the period being played is changing, or if forced.
  if (oldPlayingPeriodHolder != newPlayingPeriodHolder || forceDisableRenderers) {
    for (Renderer renderer : enabledRenderers) {
      disableRenderer(renderer);
    }
    enabledRenderers = new Renderer[0];
    oldPlayingPeriodHolder = null;
  }

  // Update the holders.
  if (newPlayingPeriodHolder != null) {
    updatePlayingPeriodRenderers(oldPlayingPeriodHolder);
    if (newPlayingPeriodHolder.hasEnabledTracks) {
      periodPositionUs = newPlayingPeriodHolder.mediaPeriod.seekToUs(periodPositionUs);
      newPlayingPeriodHolder.mediaPeriod.discardBuffer(
          periodPositionUs - backBufferDurationUs, retainBackBufferFromKeyframe);
    }
    resetRendererPosition(periodPositionUs);
    maybeContinueLoading();
  } else {
    queue.clear(/* keepFrontPeriodUid= */ true);
    resetRendererPosition(periodPositionUs);
  }

  updateLoadingMediaPeriodId();
  handler.sendEmptyMessage(MSG_DO_SOME_WORK);
  return periodPositionUs;
}
 
Example 8
Source File: ExoPlayerImplInternal.java    From TelePlus-Android with GNU General Public License v2.0 4 votes vote down vote up
private void seekToInternal(SeekPosition seekPosition) throws ExoPlaybackException {
  playbackInfoUpdate.incrementPendingOperationAcks(/* operationAcks= */ 1);

  MediaPeriodId periodId;
  long periodPositionUs;
  long contentPositionUs;
  boolean seekPositionAdjusted;
  Pair<Integer, Long> resolvedSeekPosition =
      resolveSeekPosition(seekPosition, /* trySubsequentPeriods= */ true);
  if (resolvedSeekPosition == null) {
    // The seek position was valid for the timeline that it was performed into, but the
    // timeline has changed or is not ready and a suitable seek position could not be resolved.
    periodId = getFirstMediaPeriodId();
    periodPositionUs = C.TIME_UNSET;
    contentPositionUs = C.TIME_UNSET;
    seekPositionAdjusted = true;
  } else {
    // Update the resolved seek position to take ads into account.
    int periodIndex = resolvedSeekPosition.first;
    contentPositionUs = resolvedSeekPosition.second;
    periodId = queue.resolveMediaPeriodIdForAds(periodIndex, contentPositionUs);
    if (periodId.isAd()) {
      periodPositionUs = 0;
      seekPositionAdjusted = true;
    } else {
      periodPositionUs = resolvedSeekPosition.second;
      seekPositionAdjusted = seekPosition.windowPositionUs == C.TIME_UNSET;
    }
  }

  try {
    if (mediaSource == null || pendingPrepareCount > 0) {
      // Save seek position for later, as we are still waiting for a prepared source.
      pendingInitialSeekPosition = seekPosition;
    } else if (periodPositionUs == C.TIME_UNSET) {
      // End playback, as we didn't manage to find a valid seek position.
      setState(Player.STATE_ENDED);
      resetInternal(
          /* releaseMediaSource= */ false, /* resetPosition= */ true, /* resetState= */ false);
    } else {
      // Execute the seek in the current media periods.
      long newPeriodPositionUs = periodPositionUs;
      if (periodId.equals(playbackInfo.periodId)) {
        MediaPeriodHolder playingPeriodHolder = queue.getPlayingPeriod();
        if (playingPeriodHolder != null && newPeriodPositionUs != 0) {
          newPeriodPositionUs =
              playingPeriodHolder.mediaPeriod.getAdjustedSeekPositionUs(
                  newPeriodPositionUs, seekParameters);
        }
        if (C.usToMs(newPeriodPositionUs) == C.usToMs(playbackInfo.positionUs)) {
          // Seek will be performed to the current position. Do nothing.
          periodPositionUs = playbackInfo.positionUs;
          return;
        }
      }
      newPeriodPositionUs = seekToPeriodPosition(periodId, newPeriodPositionUs);
      seekPositionAdjusted |= periodPositionUs != newPeriodPositionUs;
      periodPositionUs = newPeriodPositionUs;
    }
  } finally {
    playbackInfo = playbackInfo.fromNewPosition(periodId, periodPositionUs, contentPositionUs);
    if (seekPositionAdjusted) {
      playbackInfoUpdate.setPositionDiscontinuity(Player.DISCONTINUITY_REASON_SEEK_ADJUSTMENT);
    }
  }
}
 
Example 9
Source File: ExoPlayerImplInternal.java    From TelePlus-Android with GNU General Public License v2.0 4 votes vote down vote up
private long seekToPeriodPosition(
    MediaPeriodId periodId, long periodPositionUs, boolean forceDisableRenderers)
    throws ExoPlaybackException {
  stopRenderers();
  rebuffering = false;
  setState(Player.STATE_BUFFERING);

  // Clear the timeline, but keep the requested period if it is already prepared.
  MediaPeriodHolder oldPlayingPeriodHolder = queue.getPlayingPeriod();
  MediaPeriodHolder newPlayingPeriodHolder = oldPlayingPeriodHolder;
  while (newPlayingPeriodHolder != null) {
    if (periodId.equals(newPlayingPeriodHolder.info.id) && newPlayingPeriodHolder.prepared) {
      queue.removeAfter(newPlayingPeriodHolder);
      break;
    }
    newPlayingPeriodHolder = queue.advancePlayingPeriod();
  }

  // Disable all the renderers if the period being played is changing, or if forced.
  if (oldPlayingPeriodHolder != newPlayingPeriodHolder || forceDisableRenderers) {
    for (Renderer renderer : enabledRenderers) {
      disableRenderer(renderer);
    }
    enabledRenderers = new Renderer[0];
    oldPlayingPeriodHolder = null;
  }

  // Update the holders.
  if (newPlayingPeriodHolder != null) {
    updatePlayingPeriodRenderers(oldPlayingPeriodHolder);
    if (newPlayingPeriodHolder.hasEnabledTracks) {
      periodPositionUs = newPlayingPeriodHolder.mediaPeriod.seekToUs(periodPositionUs);
      newPlayingPeriodHolder.mediaPeriod.discardBuffer(
          periodPositionUs - backBufferDurationUs, retainBackBufferFromKeyframe);
    }
    resetRendererPosition(periodPositionUs);
    maybeContinueLoading();
  } else {
    queue.clear(/* keepFrontPeriodUid= */ true);
    resetRendererPosition(periodPositionUs);
  }

  updateLoadingMediaPeriodId();
  handler.sendEmptyMessage(MSG_DO_SOME_WORK);
  return periodPositionUs;
}
 
Example 10
Source File: ExoPlayerImplInternal.java    From Telegram-FOSS with GNU General Public License v2.0 4 votes vote down vote up
private void seekToInternal(SeekPosition seekPosition) throws ExoPlaybackException {
  playbackInfoUpdate.incrementPendingOperationAcks(/* operationAcks= */ 1);

  MediaPeriodId periodId;
  long periodPositionUs;
  long contentPositionUs;
  boolean seekPositionAdjusted;
  Pair<Object, Long> resolvedSeekPosition =
      resolveSeekPosition(seekPosition, /* trySubsequentPeriods= */ true);
  if (resolvedSeekPosition == null) {
    // The seek position was valid for the timeline that it was performed into, but the
    // timeline has changed or is not ready and a suitable seek position could not be resolved.
    periodId = playbackInfo.getDummyFirstMediaPeriodId(shuffleModeEnabled, window);
    periodPositionUs = C.TIME_UNSET;
    contentPositionUs = C.TIME_UNSET;
    seekPositionAdjusted = true;
  } else {
    // Update the resolved seek position to take ads into account.
    Object periodUid = resolvedSeekPosition.first;
    contentPositionUs = resolvedSeekPosition.second;
    periodId = queue.resolveMediaPeriodIdForAds(periodUid, contentPositionUs);
    if (periodId.isAd()) {
      periodPositionUs = 0;
      seekPositionAdjusted = true;
    } else {
      periodPositionUs = resolvedSeekPosition.second;
      seekPositionAdjusted = seekPosition.windowPositionUs == C.TIME_UNSET;
    }
  }

  try {
    if (mediaSource == null || pendingPrepareCount > 0) {
      // Save seek position for later, as we are still waiting for a prepared source.
      pendingInitialSeekPosition = seekPosition;
    } else if (periodPositionUs == C.TIME_UNSET) {
      // End playback, as we didn't manage to find a valid seek position.
      setState(Player.STATE_ENDED);
      resetInternal(
          /* resetRenderers= */ false,
          /* releaseMediaSource= */ false,
          /* resetPosition= */ true,
          /* resetState= */ false);
    } else {
      // Execute the seek in the current media periods.
      long newPeriodPositionUs = periodPositionUs;
      if (periodId.equals(playbackInfo.periodId)) {
        MediaPeriodHolder playingPeriodHolder = queue.getPlayingPeriod();
        if (playingPeriodHolder != null && newPeriodPositionUs != 0) {
          newPeriodPositionUs =
              playingPeriodHolder.mediaPeriod.getAdjustedSeekPositionUs(
                  newPeriodPositionUs, seekParameters);
        }
        if (C.usToMs(newPeriodPositionUs) == C.usToMs(playbackInfo.positionUs)) {
          // Seek will be performed to the current position. Do nothing.
          periodPositionUs = playbackInfo.positionUs;
          return;
        }
      }
      newPeriodPositionUs = seekToPeriodPosition(periodId, newPeriodPositionUs);
      seekPositionAdjusted |= periodPositionUs != newPeriodPositionUs;
      periodPositionUs = newPeriodPositionUs;
    }
  } finally {
    playbackInfo =
        playbackInfo.copyWithNewPosition(
            periodId, periodPositionUs, contentPositionUs, getTotalBufferedDurationUs());
    if (seekPositionAdjusted) {
      playbackInfoUpdate.setPositionDiscontinuity(Player.DISCONTINUITY_REASON_SEEK_ADJUSTMENT);
    }
  }
}
 
Example 11
Source File: ExoPlayerImplInternal.java    From Telegram-FOSS with GNU General Public License v2.0 4 votes vote down vote up
private long seekToPeriodPosition(
    MediaPeriodId periodId, long periodPositionUs, boolean forceDisableRenderers)
    throws ExoPlaybackException {
  stopRenderers();
  rebuffering = false;
  setState(Player.STATE_BUFFERING);

  // Clear the timeline, but keep the requested period if it is already prepared.
  MediaPeriodHolder oldPlayingPeriodHolder = queue.getPlayingPeriod();
  MediaPeriodHolder newPlayingPeriodHolder = oldPlayingPeriodHolder;
  while (newPlayingPeriodHolder != null) {
    if (periodId.equals(newPlayingPeriodHolder.info.id) && newPlayingPeriodHolder.prepared) {
      queue.removeAfter(newPlayingPeriodHolder);
      break;
    }
    newPlayingPeriodHolder = queue.advancePlayingPeriod();
  }

  // Disable all renderers if the period being played is changing, if the seek results in negative
  // renderer timestamps, or if forced.
  if (forceDisableRenderers
      || oldPlayingPeriodHolder != newPlayingPeriodHolder
      || (newPlayingPeriodHolder != null
          && newPlayingPeriodHolder.toRendererTime(periodPositionUs) < 0)) {
    for (Renderer renderer : enabledRenderers) {
      disableRenderer(renderer);
    }
    enabledRenderers = new Renderer[0];
    oldPlayingPeriodHolder = null;
    if (newPlayingPeriodHolder != null) {
      newPlayingPeriodHolder.setRendererOffset(/* rendererPositionOffsetUs= */ 0);
    }
  }

  // Update the holders.
  if (newPlayingPeriodHolder != null) {
    updatePlayingPeriodRenderers(oldPlayingPeriodHolder);
    if (newPlayingPeriodHolder.hasEnabledTracks) {
      periodPositionUs = newPlayingPeriodHolder.mediaPeriod.seekToUs(periodPositionUs);
      newPlayingPeriodHolder.mediaPeriod.discardBuffer(
          periodPositionUs - backBufferDurationUs, retainBackBufferFromKeyframe);
    }
    resetRendererPosition(periodPositionUs);
    maybeContinueLoading();
  } else {
    queue.clear(/* keepFrontPeriodUid= */ true);
    // New period has not been prepared.
    playbackInfo =
        playbackInfo.copyWithTrackInfo(TrackGroupArray.EMPTY, emptyTrackSelectorResult);
    resetRendererPosition(periodPositionUs);
  }

  handleLoadingMediaPeriodChanged(/* loadingTrackSelectionChanged= */ false);
  handler.sendEmptyMessage(MSG_DO_SOME_WORK);
  return periodPositionUs;
}
 
Example 12
Source File: ExoPlayerImplInternal.java    From Telegram with GNU General Public License v2.0 4 votes vote down vote up
private void seekToInternal(SeekPosition seekPosition) throws ExoPlaybackException {
  playbackInfoUpdate.incrementPendingOperationAcks(/* operationAcks= */ 1);

  MediaPeriodId periodId;
  long periodPositionUs;
  long contentPositionUs;
  boolean seekPositionAdjusted;
  Pair<Object, Long> resolvedSeekPosition =
      resolveSeekPosition(seekPosition, /* trySubsequentPeriods= */ true);
  if (resolvedSeekPosition == null) {
    // The seek position was valid for the timeline that it was performed into, but the
    // timeline has changed or is not ready and a suitable seek position could not be resolved.
    periodId = playbackInfo.getDummyFirstMediaPeriodId(shuffleModeEnabled, window);
    periodPositionUs = C.TIME_UNSET;
    contentPositionUs = C.TIME_UNSET;
    seekPositionAdjusted = true;
  } else {
    // Update the resolved seek position to take ads into account.
    Object periodUid = resolvedSeekPosition.first;
    contentPositionUs = resolvedSeekPosition.second;
    periodId = queue.resolveMediaPeriodIdForAds(periodUid, contentPositionUs);
    if (periodId.isAd()) {
      periodPositionUs = 0;
      seekPositionAdjusted = true;
    } else {
      periodPositionUs = resolvedSeekPosition.second;
      seekPositionAdjusted = seekPosition.windowPositionUs == C.TIME_UNSET;
    }
  }

  try {
    if (mediaSource == null || pendingPrepareCount > 0) {
      // Save seek position for later, as we are still waiting for a prepared source.
      pendingInitialSeekPosition = seekPosition;
    } else if (periodPositionUs == C.TIME_UNSET) {
      // End playback, as we didn't manage to find a valid seek position.
      setState(Player.STATE_ENDED);
      resetInternal(
          /* resetRenderers= */ false,
          /* releaseMediaSource= */ false,
          /* resetPosition= */ true,
          /* resetState= */ false);
    } else {
      // Execute the seek in the current media periods.
      long newPeriodPositionUs = periodPositionUs;
      if (periodId.equals(playbackInfo.periodId)) {
        MediaPeriodHolder playingPeriodHolder = queue.getPlayingPeriod();
        if (playingPeriodHolder != null && newPeriodPositionUs != 0) {
          newPeriodPositionUs =
              playingPeriodHolder.mediaPeriod.getAdjustedSeekPositionUs(
                  newPeriodPositionUs, seekParameters);
        }
        if (C.usToMs(newPeriodPositionUs) == C.usToMs(playbackInfo.positionUs)) {
          // Seek will be performed to the current position. Do nothing.
          periodPositionUs = playbackInfo.positionUs;
          return;
        }
      }
      newPeriodPositionUs = seekToPeriodPosition(periodId, newPeriodPositionUs);
      seekPositionAdjusted |= periodPositionUs != newPeriodPositionUs;
      periodPositionUs = newPeriodPositionUs;
    }
  } finally {
    playbackInfo =
        playbackInfo.copyWithNewPosition(
            periodId, periodPositionUs, contentPositionUs, getTotalBufferedDurationUs());
    if (seekPositionAdjusted) {
      playbackInfoUpdate.setPositionDiscontinuity(Player.DISCONTINUITY_REASON_SEEK_ADJUSTMENT);
    }
  }
}
 
Example 13
Source File: ExoPlayerImplInternal.java    From Telegram with GNU General Public License v2.0 4 votes vote down vote up
private long seekToPeriodPosition(
    MediaPeriodId periodId, long periodPositionUs, boolean forceDisableRenderers)
    throws ExoPlaybackException {
  stopRenderers();
  rebuffering = false;
  setState(Player.STATE_BUFFERING);

  // Clear the timeline, but keep the requested period if it is already prepared.
  MediaPeriodHolder oldPlayingPeriodHolder = queue.getPlayingPeriod();
  MediaPeriodHolder newPlayingPeriodHolder = oldPlayingPeriodHolder;
  while (newPlayingPeriodHolder != null) {
    if (periodId.equals(newPlayingPeriodHolder.info.id) && newPlayingPeriodHolder.prepared) {
      queue.removeAfter(newPlayingPeriodHolder);
      break;
    }
    newPlayingPeriodHolder = queue.advancePlayingPeriod();
  }

  // Disable all renderers if the period being played is changing, if the seek results in negative
  // renderer timestamps, or if forced.
  if (forceDisableRenderers
      || oldPlayingPeriodHolder != newPlayingPeriodHolder
      || (newPlayingPeriodHolder != null
          && newPlayingPeriodHolder.toRendererTime(periodPositionUs) < 0)) {
    for (Renderer renderer : enabledRenderers) {
      disableRenderer(renderer);
    }
    enabledRenderers = new Renderer[0];
    oldPlayingPeriodHolder = null;
    if (newPlayingPeriodHolder != null) {
      newPlayingPeriodHolder.setRendererOffset(/* rendererPositionOffsetUs= */ 0);
    }
  }

  // Update the holders.
  if (newPlayingPeriodHolder != null) {
    updatePlayingPeriodRenderers(oldPlayingPeriodHolder);
    if (newPlayingPeriodHolder.hasEnabledTracks) {
      periodPositionUs = newPlayingPeriodHolder.mediaPeriod.seekToUs(periodPositionUs);
      newPlayingPeriodHolder.mediaPeriod.discardBuffer(
          periodPositionUs - backBufferDurationUs, retainBackBufferFromKeyframe);
    }
    resetRendererPosition(periodPositionUs);
    maybeContinueLoading();
  } else {
    queue.clear(/* keepFrontPeriodUid= */ true);
    // New period has not been prepared.
    playbackInfo =
        playbackInfo.copyWithTrackInfo(TrackGroupArray.EMPTY, emptyTrackSelectorResult);
    resetRendererPosition(periodPositionUs);
  }

  handleLoadingMediaPeriodChanged(/* loadingTrackSelectionChanged= */ false);
  handler.sendEmptyMessage(MSG_DO_SOME_WORK);
  return periodPositionUs;
}