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

The following examples show how to use com.google.android.exoplayer2.source.MediaSource.MediaPeriodId#isAd() . 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: MediaPeriodQueue.java    From TelePlus-Android with GNU General Public License v2.0 6 votes vote down vote up
private boolean isLastInPeriod(MediaPeriodId id) {
  int adGroupCount = timeline.getPeriod(id.periodIndex, period).getAdGroupCount();
  if (adGroupCount == 0) {
    return true;
  }

  int lastAdGroupIndex = adGroupCount - 1;
  boolean isAd = id.isAd();
  if (period.getAdGroupTimeUs(lastAdGroupIndex) != C.TIME_END_OF_SOURCE) {
    // There's no postroll ad.
    return !isAd && id.endPositionUs == C.TIME_END_OF_SOURCE;
  }

  int postrollAdCount = period.getAdCountInAdGroup(lastAdGroupIndex);
  if (postrollAdCount == C.LENGTH_UNSET) {
    // We won't know if this is the last ad until we know how many postroll ads there are.
    return false;
  }

  boolean isLastAd =
      isAd && id.adGroupIndex == lastAdGroupIndex && id.adIndexInAdGroup == postrollAdCount - 1;
  return isLastAd || (!isAd && period.getFirstAdIndexToPlay(lastAdGroupIndex) == postrollAdCount);
}
 
Example 2
Source File: MediaPeriodQueue.java    From Telegram-FOSS with GNU General Public License v2.0 6 votes vote down vote up
private MediaPeriodInfo getMediaPeriodInfo(
    MediaPeriodId id, long contentPositionUs, long startPositionUs) {
  timeline.getPeriodByUid(id.periodUid, period);
  if (id.isAd()) {
    if (!period.isAdAvailable(id.adGroupIndex, id.adIndexInAdGroup)) {
      return null;
    }
    return getMediaPeriodInfoForAd(
        id.periodUid,
        id.adGroupIndex,
        id.adIndexInAdGroup,
        contentPositionUs,
        id.windowSequenceNumber);
  } else {
    return getMediaPeriodInfoForContent(id.periodUid, startPositionUs, id.windowSequenceNumber);
  }
}
 
Example 3
Source File: MediaPeriodQueue.java    From TelePlus-Android with GNU General Public License v2.0 6 votes vote down vote up
private boolean isLastInPeriod(MediaPeriodId id) {
  int adGroupCount = timeline.getPeriod(id.periodIndex, period).getAdGroupCount();
  if (adGroupCount == 0) {
    return true;
  }

  int lastAdGroupIndex = adGroupCount - 1;
  boolean isAd = id.isAd();
  if (period.getAdGroupTimeUs(lastAdGroupIndex) != C.TIME_END_OF_SOURCE) {
    // There's no postroll ad.
    return !isAd && id.endPositionUs == C.TIME_END_OF_SOURCE;
  }

  int postrollAdCount = period.getAdCountInAdGroup(lastAdGroupIndex);
  if (postrollAdCount == C.LENGTH_UNSET) {
    // We won't know if this is the last ad until we know how many postroll ads there are.
    return false;
  }

  boolean isLastAd =
      isAd && id.adGroupIndex == lastAdGroupIndex && id.adIndexInAdGroup == postrollAdCount - 1;
  return isLastAd || (!isAd && period.getFirstAdIndexToPlay(lastAdGroupIndex) == postrollAdCount);
}
 
Example 4
Source File: PlaybackInfo.java    From Telegram-FOSS with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Copied playback info with new playing position.
 *
 * @param periodId New playing media period. See {@link #periodId}.
 * @param positionUs New position. See {@link #positionUs}.
 * @param contentPositionUs New content position. See {@link #contentPositionUs}. Value is ignored
 *     if {@code periodId.isAd()} is true.
 * @param totalBufferedDurationUs New buffered duration. See {@link #totalBufferedDurationUs}.
 * @return Copied playback info with new playing position.
 */
@CheckResult
public PlaybackInfo copyWithNewPosition(
    MediaPeriodId periodId,
    long positionUs,
    long contentPositionUs,
    long totalBufferedDurationUs) {
  return new PlaybackInfo(
      timeline,
      manifest,
      periodId,
      positionUs,
      periodId.isAd() ? contentPositionUs : C.TIME_UNSET,
      playbackState,
      isLoading,
      trackGroups,
      trackSelectorResult,
      loadingMediaPeriodId,
      bufferedPositionUs,
      totalBufferedDurationUs,
      positionUs);
}
 
Example 5
Source File: PlaybackInfo.java    From Telegram with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Copied playback info with new playing position.
 *
 * @param periodId New playing media period. See {@link #periodId}.
 * @param positionUs New position. See {@link #positionUs}.
 * @param contentPositionUs New content position. See {@link #contentPositionUs}. Value is ignored
 *     if {@code periodId.isAd()} is true.
 * @param totalBufferedDurationUs New buffered duration. See {@link #totalBufferedDurationUs}.
 * @return Copied playback info with new playing position.
 */
@CheckResult
public PlaybackInfo copyWithNewPosition(
    MediaPeriodId periodId,
    long positionUs,
    long contentPositionUs,
    long totalBufferedDurationUs) {
  return new PlaybackInfo(
      timeline,
      manifest,
      periodId,
      positionUs,
      periodId.isAd() ? contentPositionUs : C.TIME_UNSET,
      playbackState,
      isLoading,
      trackGroups,
      trackSelectorResult,
      loadingMediaPeriodId,
      bufferedPositionUs,
      totalBufferedDurationUs,
      positionUs);
}
 
Example 6
Source File: PlaybackInfo.java    From MediaSDK with Apache License 2.0 6 votes vote down vote up
/**
 * Copies playback info with new playing position.
 *
 * @param periodId New playing media period. See {@link #periodId}.
 * @param positionUs New position. See {@link #positionUs}.
 * @param contentPositionUs New content position. See {@link #contentPositionUs}. Value is ignored
 *     if {@code periodId.isAd()} is true.
 * @param totalBufferedDurationUs New buffered duration. See {@link #totalBufferedDurationUs}.
 * @return Copied playback info with new playing position.
 */
@CheckResult
public PlaybackInfo copyWithNewPosition(
    MediaPeriodId periodId,
    long positionUs,
    long contentPositionUs,
    long totalBufferedDurationUs) {
  return new PlaybackInfo(
      timeline,
      periodId,
      positionUs,
      periodId.isAd() ? contentPositionUs : C.TIME_UNSET,
      playbackState,
      playbackError,
      isLoading,
      trackGroups,
      trackSelectorResult,
      loadingMediaPeriodId,
      bufferedPositionUs,
      totalBufferedDurationUs,
      positionUs);
}
 
Example 7
Source File: PlaybackInfo.java    From TelePlus-Android with GNU General Public License v2.0 6 votes vote down vote up
public PlaybackInfo fromNewPosition(
    MediaPeriodId periodId, long startPositionUs, long contentPositionUs) {
  return new PlaybackInfo(
      timeline,
      manifest,
      periodId,
      startPositionUs,
      periodId.isAd() ? contentPositionUs : C.TIME_UNSET,
      playbackState,
      isLoading,
      trackGroups,
      trackSelectorResult,
      periodId,
      startPositionUs,
      /* totalBufferedDurationUs= */ 0,
      startPositionUs);
}
 
Example 8
Source File: AdsMediaSource.java    From MediaSDK with Apache License 2.0 5 votes vote down vote up
@Override
protected @Nullable MediaPeriodId getMediaPeriodIdForChildMediaPeriodId(
    MediaPeriodId childId, MediaPeriodId mediaPeriodId) {
  // The child id for the content period is just DUMMY_CONTENT_MEDIA_PERIOD_ID. That's why we need
  // to forward the reported mediaPeriodId in this case.
  return childId.isAd() ? childId : mediaPeriodId;
}
 
Example 9
Source File: AdsMediaSource.java    From TelePlus-Android with GNU General Public License v2.0 5 votes vote down vote up
@Override
protected @Nullable MediaPeriodId getMediaPeriodIdForChildMediaPeriodId(
    MediaPeriodId childId, MediaPeriodId mediaPeriodId) {
  // The child id for the content period is just DUMMY_CONTENT_MEDIA_PERIOD_ID. That's why we need
  // to forward the reported mediaPeriodId in this case.
  return childId.isAd() ? childId : mediaPeriodId;
}
 
Example 10
Source File: AdsMediaSource.java    From TelePlus-Android with GNU General Public License v2.0 5 votes vote down vote up
@Override
protected void onChildSourceInfoRefreshed(
    MediaPeriodId mediaPeriodId,
    MediaSource mediaSource,
    Timeline timeline,
    @Nullable Object manifest) {
  if (mediaPeriodId.isAd()) {
    int adGroupIndex = mediaPeriodId.adGroupIndex;
    int adIndexInAdGroup = mediaPeriodId.adIndexInAdGroup;
    onAdSourceInfoRefreshed(mediaSource, adGroupIndex, adIndexInAdGroup, timeline);
  } else {
    onContentSourceInfoRefreshed(timeline, manifest);
  }
}
 
Example 11
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 12
Source File: AnalyticsCollector.java    From Telegram-FOSS 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 13
Source File: AdsMediaSource.java    From Telegram with GNU General Public License v2.0 5 votes vote down vote up
@Override
protected @Nullable MediaPeriodId getMediaPeriodIdForChildMediaPeriodId(
    MediaPeriodId childId, MediaPeriodId mediaPeriodId) {
  // The child id for the content period is just DUMMY_CONTENT_MEDIA_PERIOD_ID. That's why we need
  // to forward the reported mediaPeriodId in this case.
  return childId.isAd() ? childId : mediaPeriodId;
}
 
Example 14
Source File: DefaultPlaybackSessionManager.java    From MediaSDK with Apache License 2.0 5 votes vote down vote up
public SessionDescriptor(
    String sessionId, int windowIndex, @Nullable MediaPeriodId mediaPeriodId) {
  this.sessionId = sessionId;
  this.windowIndex = windowIndex;
  this.windowSequenceNumber =
      mediaPeriodId == null ? C.INDEX_UNSET : mediaPeriodId.windowSequenceNumber;
  if (mediaPeriodId != null && mediaPeriodId.isAd()) {
    this.adMediaPeriodId = mediaPeriodId;
  }
}
 
Example 15
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 16
Source File: AnalyticsCollector.java    From TelePlus-Android with GNU General Public License v2.0 5 votes vote down vote up
/** Returns a new {@link EventTime} for the specified window index and media period id. */
protected EventTime generateEventTime(int windowIndex, @Nullable MediaPeriodId mediaPeriodId) {
  Assertions.checkNotNull(player);
  long realtimeMs = clock.elapsedRealtime();
  Timeline timeline = player.getCurrentTimeline();
  long eventPositionMs;
  if (windowIndex == player.getCurrentWindowIndex()) {
    if (mediaPeriodId != null && mediaPeriodId.isAd()) {
      // This event is for an ad in the currently playing window.
      eventPositionMs =
          player.getCurrentAdGroupIndex() == mediaPeriodId.adGroupIndex
                  && player.getCurrentAdIndexInAdGroup() == mediaPeriodId.adIndexInAdGroup
              ? player.getCurrentPosition()
              : 0 /* Assume start position of 0 for a future ad. */;
    } else {
      // This event is for content in the currently playing window.
      eventPositionMs = player.getContentPosition();
    }
  } else if (windowIndex >= timeline.getWindowCount()
      || (mediaPeriodId != null && mediaPeriodId.isAd())) {
    // This event is for an unknown future window or for an ad in a future window.
    // Assume start position of zero.
    eventPositionMs = 0;
  } else {
    // This event is for content in a future window. Assume default start position.
    eventPositionMs = timeline.getWindow(windowIndex, window).getDefaultPositionMs();
  }
  return new EventTime(
      realtimeMs,
      timeline,
      windowIndex,
      mediaPeriodId,
      eventPositionMs,
      player.getCurrentPosition(),
      player.getTotalBufferedDuration());
}
 
Example 17
Source File: MediaPeriodQueue.java    From Telegram-FOSS with GNU General Public License v2.0 4 votes vote down vote up
private boolean isLastInPeriod(MediaPeriodId id) {
  return !id.isAd() && id.nextAdGroupIndex == C.INDEX_UNSET;
}
 
Example 18
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 19
Source File: AdsMediaSource.java    From MediaSDK with Apache License 2.0 4 votes vote down vote up
@Override
public MediaPeriod createPeriod(MediaPeriodId id, Allocator allocator, long startPositionUs) {
  AdPlaybackState adPlaybackState = Assertions.checkNotNull(this.adPlaybackState);
  if (adPlaybackState.adGroupCount > 0 && id.isAd()) {
    int adGroupIndex = id.adGroupIndex;
    int adIndexInAdGroup = id.adIndexInAdGroup;
    Uri adUri =
        Assertions.checkNotNull(adPlaybackState.adGroups[adGroupIndex].uris[adIndexInAdGroup]);
    if (adGroupMediaSources[adGroupIndex].length <= adIndexInAdGroup) {
      int adCount = adIndexInAdGroup + 1;
      adGroupMediaSources[adGroupIndex] =
          Arrays.copyOf(adGroupMediaSources[adGroupIndex], adCount);
      adGroupTimelines[adGroupIndex] = Arrays.copyOf(adGroupTimelines[adGroupIndex], adCount);
    }
    MediaSource mediaSource = adGroupMediaSources[adGroupIndex][adIndexInAdGroup];
    if (mediaSource == null) {
      mediaSource = adMediaSourceFactory.createMediaSource(adUri);
      adGroupMediaSources[adGroupIndex][adIndexInAdGroup] = mediaSource;
      maskingMediaPeriodByAdMediaSource.put(mediaSource, new ArrayList<>());
      prepareChildSource(id, mediaSource);
    }
    MaskingMediaPeriod maskingMediaPeriod =
        new MaskingMediaPeriod(mediaSource, id, allocator, startPositionUs);
    maskingMediaPeriod.setPrepareErrorListener(
        new AdPrepareErrorListener(adUri, adGroupIndex, adIndexInAdGroup));
    List<MaskingMediaPeriod> mediaPeriods = maskingMediaPeriodByAdMediaSource.get(mediaSource);
    if (mediaPeriods == null) {
      Object periodUid =
          Assertions.checkNotNull(adGroupTimelines[adGroupIndex][adIndexInAdGroup])
              .getUidOfPeriod(/* periodIndex= */ 0);
      MediaPeriodId adSourceMediaPeriodId = new MediaPeriodId(periodUid, id.windowSequenceNumber);
      maskingMediaPeriod.createPeriod(adSourceMediaPeriodId);
    } else {
      // Keep track of the masking media period so it can be populated with the real media period
      // when the source's info becomes available.
      mediaPeriods.add(maskingMediaPeriod);
    }
    return maskingMediaPeriod;
  } else {
    MaskingMediaPeriod mediaPeriod =
        new MaskingMediaPeriod(contentMediaSource, id, allocator, startPositionUs);
    mediaPeriod.createPeriod(id);
    return mediaPeriod;
  }
}
 
Example 20
Source File: MediaPeriodQueue.java    From MediaSDK with Apache License 2.0 4 votes vote down vote up
private boolean isLastInPeriod(MediaPeriodId id) {
  return !id.isAd() && id.nextAdGroupIndex == C.INDEX_UNSET;
}