com.google.android.exoplayer2.source.MediaSource.MediaPeriodId Java Examples

The following examples show how to use com.google.android.exoplayer2.source.MediaSource.MediaPeriodId. 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: AdsMediaSource.java    From Telegram-FOSS with GNU General Public License v2.0 6 votes vote down vote up
@Override
public void onPrepareError(MediaPeriodId mediaPeriodId, final IOException exception) {
  createEventDispatcher(mediaPeriodId)
      .loadError(
          new DataSpec(adUri),
          adUri,
          /* responseHeaders= */ Collections.emptyMap(),
          C.DATA_TYPE_AD,
          C.TRACK_TYPE_UNKNOWN,
          /* loadDurationMs= */ 0,
          /* bytesLoaded= */ 0,
          AdLoadException.createForAd(exception),
          /* wasCanceled= */ true);
  mainHandler.post(
      () -> adsLoader.handlePrepareError(adGroupIndex, adIndexInAdGroup, exception));
}
 
Example #2
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 #3
Source File: MediaPeriodQueue.java    From TelePlus-Android with GNU General Public License v2.0 6 votes vote down vote up
private MediaPeriodInfo getMediaPeriodInfoForAd(
    int periodIndex,
    int adGroupIndex,
    int adIndexInAdGroup,
    long contentPositionUs,
    long windowSequenceNumber) {
  MediaPeriodId id =
      new MediaPeriodId(periodIndex, adGroupIndex, adIndexInAdGroup, windowSequenceNumber);
  boolean isLastInPeriod = isLastInPeriod(id);
  boolean isLastInTimeline = isLastInTimeline(id, isLastInPeriod);
  long durationUs =
      timeline
          .getPeriod(id.periodIndex, period)
          .getAdDurationUs(id.adGroupIndex, id.adIndexInAdGroup);
  long startPositionUs =
      adIndexInAdGroup == period.getFirstAdIndexToPlay(adGroupIndex)
          ? period.getAdResumePositionUs()
          : 0;
  return new MediaPeriodInfo(
      id,
      startPositionUs,
      contentPositionUs,
      durationUs,
      isLastInPeriod,
      isLastInTimeline);
}
 
Example #4
Source File: ExoPlayerImplInternal.java    From Telegram-FOSS with GNU General Public License v2.0 6 votes vote down vote up
private void handleLoadingMediaPeriodChanged(boolean loadingTrackSelectionChanged) {
  MediaPeriodHolder loadingMediaPeriodHolder = queue.getLoadingPeriod();
  MediaPeriodId loadingMediaPeriodId =
      loadingMediaPeriodHolder == null ? playbackInfo.periodId : loadingMediaPeriodHolder.info.id;
  boolean loadingMediaPeriodChanged =
      !playbackInfo.loadingMediaPeriodId.equals(loadingMediaPeriodId);
  if (loadingMediaPeriodChanged) {
    playbackInfo = playbackInfo.copyWithLoadingMediaPeriodId(loadingMediaPeriodId);
  }
  playbackInfo.bufferedPositionUs =
      loadingMediaPeriodHolder == null
          ? playbackInfo.positionUs
          : loadingMediaPeriodHolder.getBufferedPositionUs();
  playbackInfo.totalBufferedDurationUs = getTotalBufferedDurationUs();
  if ((loadingMediaPeriodChanged || loadingTrackSelectionChanged)
      && loadingMediaPeriodHolder != null
      && loadingMediaPeriodHolder.prepared) {
    updateLoadControlTrackSelection(
        loadingMediaPeriodHolder.getTrackGroups(),
        loadingMediaPeriodHolder.getTrackSelectorResult());
  }
}
 
Example #5
Source File: ExoPlayerImplInternal.java    From Telegram with GNU General Public License v2.0 6 votes vote down vote up
private void handleLoadingMediaPeriodChanged(boolean loadingTrackSelectionChanged) {
  MediaPeriodHolder loadingMediaPeriodHolder = queue.getLoadingPeriod();
  MediaPeriodId loadingMediaPeriodId =
      loadingMediaPeriodHolder == null ? playbackInfo.periodId : loadingMediaPeriodHolder.info.id;
  boolean loadingMediaPeriodChanged =
      !playbackInfo.loadingMediaPeriodId.equals(loadingMediaPeriodId);
  if (loadingMediaPeriodChanged) {
    playbackInfo = playbackInfo.copyWithLoadingMediaPeriodId(loadingMediaPeriodId);
  }
  playbackInfo.bufferedPositionUs =
      loadingMediaPeriodHolder == null
          ? playbackInfo.positionUs
          : loadingMediaPeriodHolder.getBufferedPositionUs();
  playbackInfo.totalBufferedDurationUs = getTotalBufferedDurationUs();
  if ((loadingMediaPeriodChanged || loadingTrackSelectionChanged)
      && loadingMediaPeriodHolder != null
      && loadingMediaPeriodHolder.prepared) {
    updateLoadControlTrackSelection(
        loadingMediaPeriodHolder.getTrackGroups(),
        loadingMediaPeriodHolder.getTrackSelectorResult());
  }
}
 
Example #6
Source File: AnalyticsCollector.java    From TelePlus-Android with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Tries to find an existing media period id from the specified window index. Only returns a
 * non-null media period id if there is a unique, unambiguous match.
 */
public @Nullable MediaPeriodId tryResolveWindowIndex(int windowIndex) {
  MediaPeriodId match = null;
  if (timeline != null) {
    int timelinePeriodCount = timeline.getPeriodCount();
    for (int i = 0; i < activeMediaPeriods.size(); i++) {
      WindowAndMediaPeriodId mediaPeriod = activeMediaPeriods.get(i);
      int periodIndex = mediaPeriod.mediaPeriodId.periodIndex;
      if (periodIndex < timelinePeriodCount
          && timeline.getPeriod(periodIndex, period).windowIndex == windowIndex) {
        if (match != null) {
          // Ambiguous match.
          return null;
        }
        match = mediaPeriod.mediaPeriodId;
      }
    }
  }
  return match;
}
 
Example #7
Source File: PlaybackInfo.java    From Telegram with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Copies playback info with new loading media period.
 *
 * @param loadingMediaPeriodId New loading media period id. See {@link #loadingMediaPeriodId}.
 * @return Copied playback info with new loading media period.
 */
@CheckResult
public PlaybackInfo copyWithLoadingMediaPeriodId(MediaPeriodId loadingMediaPeriodId) {
  return new PlaybackInfo(
      timeline,
      manifest,
      periodId,
      startPositionUs,
      contentPositionUs,
      playbackState,
      isLoading,
      trackGroups,
      trackSelectorResult,
      loadingMediaPeriodId,
      bufferedPositionUs,
      totalBufferedDurationUs,
      positionUs);
}
 
Example #8
Source File: MediaPeriodQueue.java    From Telegram with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Returns new media period info based on specified {@code mediaPeriodInfo} but taking into
 * account the current timeline. This method must only be called if the period is still part of
 * the current timeline.
 *
 * @param info Media period info for a media period based on an old timeline.
 * @return The updated media period info for the current timeline.
 */
public MediaPeriodInfo getUpdatedMediaPeriodInfo(MediaPeriodInfo info) {
  MediaPeriodId id = info.id;
  boolean isLastInPeriod = isLastInPeriod(id);
  boolean isLastInTimeline = isLastInTimeline(id, isLastInPeriod);
  timeline.getPeriodByUid(info.id.periodUid, period);
  long durationUs =
      id.isAd()
          ? period.getAdDurationUs(id.adGroupIndex, id.adIndexInAdGroup)
          : (info.endPositionUs == C.TIME_UNSET || info.endPositionUs == C.TIME_END_OF_SOURCE
              ? period.getDurationUs()
              : info.endPositionUs);
  return new MediaPeriodInfo(
      id,
      info.startPositionUs,
      info.contentPositionUs,
      info.endPositionUs,
      durationUs,
      isLastInPeriod,
      isLastInTimeline);
}
 
Example #9
Source File: PlaybackInfo.java    From MediaSDK with Apache License 2.0 6 votes vote down vote up
/**
 * Copies playback info with new loading media period.
 *
 * @param loadingMediaPeriodId New loading media period id. See {@link #loadingMediaPeriodId}.
 * @return Copied playback info with new loading media period.
 */
@CheckResult
public PlaybackInfo copyWithLoadingMediaPeriodId(MediaPeriodId loadingMediaPeriodId) {
  return new PlaybackInfo(
      timeline,
      periodId,
      startPositionUs,
      contentPositionUs,
      playbackState,
      playbackError,
      isLoading,
      trackGroups,
      trackSelectorResult,
      loadingMediaPeriodId,
      bufferedPositionUs,
      totalBufferedDurationUs,
      positionUs);
}
 
Example #10
Source File: ExoPlayerImplInternal.java    From Telegram-FOSS with GNU General Public License v2.0 6 votes vote down vote up
private void seekToCurrentPosition(boolean sendDiscontinuity) throws ExoPlaybackException {
  // Renderers may have read from a period that's been removed. Seek back to the current
  // position of the playing period to make sure none of the removed period is played.
  MediaPeriodId periodId = queue.getPlayingPeriod().info.id;
  long newPositionUs =
      seekToPeriodPosition(periodId, playbackInfo.positionUs, /* forceDisableRenderers= */ true);
  if (newPositionUs != playbackInfo.positionUs) {
    playbackInfo =
        playbackInfo.copyWithNewPosition(
            periodId,
            newPositionUs,
            playbackInfo.contentPositionUs,
            getTotalBufferedDurationUs());
    if (sendDiscontinuity) {
      playbackInfoUpdate.setPositionDiscontinuity(Player.DISCONTINUITY_REASON_INTERNAL);
    }
  }
}
 
Example #11
Source File: MediaPeriodQueue.java    From TelePlus-Android with GNU General Public License v2.0 6 votes vote down vote up
private MediaPeriodInfo getUpdatedMediaPeriodInfo(MediaPeriodInfo info, MediaPeriodId newId) {
  long startPositionUs = info.startPositionUs;
  boolean isLastInPeriod = isLastInPeriod(newId);
  boolean isLastInTimeline = isLastInTimeline(newId, isLastInPeriod);
  timeline.getPeriod(newId.periodIndex, period);
  long durationUs =
      newId.isAd()
          ? period.getAdDurationUs(newId.adGroupIndex, newId.adIndexInAdGroup)
          : (newId.endPositionUs == C.TIME_END_OF_SOURCE
              ? period.getDurationUs()
              : newId.endPositionUs);
  return new MediaPeriodInfo(
      newId,
      startPositionUs,
      info.contentPositionUs,
      durationUs,
      isLastInPeriod,
      isLastInTimeline);
}
 
Example #12
Source File: MediaPeriodQueue.java    From TelePlus-Android with GNU General Public License v2.0 6 votes vote down vote up
private MediaPeriodInfo getMediaPeriodInfo(
    MediaPeriodId id, long contentPositionUs, long startPositionUs) {
  timeline.getPeriod(id.periodIndex, period);
  if (id.isAd()) {
    if (!period.isAdAvailable(id.adGroupIndex, id.adIndexInAdGroup)) {
      return null;
    }
    return getMediaPeriodInfoForAd(
        id.periodIndex,
        id.adGroupIndex,
        id.adIndexInAdGroup,
        contentPositionUs,
        id.windowSequenceNumber);
  } else {
    return getMediaPeriodInfoForContent(id.periodIndex, startPositionUs, id.windowSequenceNumber);
  }
}
 
Example #13
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 #14
Source File: AnalyticsCollector.java    From MediaSDK with Apache License 2.0 5 votes vote down vote up
private EventTime generateMediaPeriodEventTime(
    int windowIndex, @Nullable MediaPeriodId mediaPeriodId) {
  Assertions.checkNotNull(player);
  if (mediaPeriodId != null) {
    MediaPeriodInfo mediaPeriodInfo = mediaPeriodQueueTracker.getMediaPeriodInfo(mediaPeriodId);
    return mediaPeriodInfo != null
        ? generateEventTime(mediaPeriodInfo)
        : generateEventTime(Timeline.EMPTY, windowIndex, mediaPeriodId);
  }
  Timeline timeline = player.getCurrentTimeline();
  boolean windowIsInTimeline = windowIndex < timeline.getWindowCount();
  return generateEventTime(
      windowIsInTimeline ? timeline : Timeline.EMPTY, windowIndex, /* mediaPeriodId= */ null);
}
 
Example #15
Source File: DefaultPlaybackSessionManager.java    From MediaSDK with Apache License 2.0 5 votes vote down vote up
public void maybeSetWindowSequenceNumber(
    int eventWindowIndex, @Nullable MediaPeriodId eventMediaPeriodId) {
  if (windowSequenceNumber == C.INDEX_UNSET
      && eventWindowIndex == windowIndex
      && eventMediaPeriodId != null
      && !eventMediaPeriodId.isAd()) {
    // Set window sequence number for this session as soon as we have one.
    windowSequenceNumber = eventMediaPeriodId.windowSequenceNumber;
  }
}
 
Example #16
Source File: ExoPlayerImplInternal.java    From TelePlus-Android with GNU General Public License v2.0 5 votes vote down vote up
private void seekToCurrentPosition(boolean sendDiscontinuity) throws ExoPlaybackException {
  // Renderers may have read from a period that's been removed. Seek back to the current
  // position of the playing period to make sure none of the removed period is played.
  MediaPeriodId periodId = queue.getPlayingPeriod().info.id;
  long newPositionUs =
      seekToPeriodPosition(periodId, playbackInfo.positionUs, /* forceDisableRenderers= */ true);
  if (newPositionUs != playbackInfo.positionUs) {
    playbackInfo =
        playbackInfo.fromNewPosition(periodId, newPositionUs, playbackInfo.contentPositionUs);
    if (sendDiscontinuity) {
      playbackInfoUpdate.setPositionDiscontinuity(Player.DISCONTINUITY_REASON_INTERNAL);
    }
  }
}
 
Example #17
Source File: AnalyticsCollector.java    From Telegram with GNU General Public License v2.0 5 votes vote down vote up
@Override
public final void onLoadError(
    int windowIndex,
    @Nullable MediaPeriodId mediaPeriodId,
    LoadEventInfo loadEventInfo,
    MediaLoadData mediaLoadData,
    IOException error,
    boolean wasCanceled) {
  EventTime eventTime = generateMediaPeriodEventTime(windowIndex, mediaPeriodId);
  for (AnalyticsListener listener : listeners) {
    listener.onLoadError(eventTime, loadEventInfo, mediaLoadData, error, wasCanceled);
  }
}
 
Example #18
Source File: DeferredMediaPeriod.java    From Telegram with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Creates a new deferred media period.
 *
 * @param mediaSource The media source to wrap.
 * @param id The identifier used to create the deferred media period.
 * @param allocator The allocator used to create the media period.
 * @param preparePositionUs The expected start position, in microseconds.
 */
public DeferredMediaPeriod(
    MediaSource mediaSource, MediaPeriodId id, Allocator allocator, long preparePositionUs) {
  this.id = id;
  this.allocator = allocator;
  this.mediaSource = mediaSource;
  this.preparePositionUs = preparePositionUs;
  preparePositionOverrideUs = C.TIME_UNSET;
}
 
Example #19
Source File: AnalyticsCollector.java    From TelePlus-Android with GNU General Public License v2.0 5 votes vote down vote up
/** Updates the queue with a released media period. */
public void onMediaPeriodReleased(int windowIndex, MediaPeriodId mediaPeriodId) {
  WindowAndMediaPeriodId mediaPeriod = new WindowAndMediaPeriodId(windowIndex, mediaPeriodId);
  activeMediaPeriods.remove(mediaPeriod);
  if (mediaPeriod.equals(readingMediaPeriod)) {
    readingMediaPeriod = activeMediaPeriods.isEmpty() ? null : activeMediaPeriods.get(0);
  }
}
 
Example #20
Source File: MediaPeriodInfo.java    From TelePlus-Android with GNU General Public License v2.0 5 votes vote down vote up
MediaPeriodInfo(
    MediaPeriodId id,
    long startPositionUs,
    long contentPositionUs,
    long durationUs,
    boolean isLastInTimelinePeriod,
    boolean isFinal) {
  this.id = id;
  this.startPositionUs = startPositionUs;
  this.contentPositionUs = contentPositionUs;
  this.durationUs = durationUs;
  this.isLastInTimelinePeriod = isLastInTimelinePeriod;
  this.isFinal = isFinal;
}
 
Example #21
Source File: AnalyticsCollector.java    From TelePlus-Android with GNU General Public License v2.0 5 votes vote down vote up
@Override
public final void onLoadCanceled(
    int windowIndex,
    @Nullable MediaPeriodId mediaPeriodId,
    LoadEventInfo loadEventInfo,
    MediaLoadData mediaLoadData) {
  EventTime eventTime = generateEventTime(windowIndex, mediaPeriodId);
  for (AnalyticsListener listener : listeners) {
    listener.onLoadCanceled(eventTime, loadEventInfo, mediaLoadData);
  }
}
 
Example #22
Source File: MediaSourceEventListener.java    From Telegram-FOSS with GNU General Public License v2.0 5 votes vote down vote up
/** Dispatches {@link #onMediaPeriodReleased(int, MediaPeriodId)}. */
public void mediaPeriodReleased() {
  MediaPeriodId mediaPeriodId = Assertions.checkNotNull(this.mediaPeriodId);
  for (ListenerAndHandler listenerAndHandler : listenerAndHandlers) {
    final MediaSourceEventListener listener = listenerAndHandler.listener;
    postOrRun(
        listenerAndHandler.handler,
        () -> listener.onMediaPeriodReleased(windowIndex, mediaPeriodId));
  }
}
 
Example #23
Source File: AnalyticsCollector.java    From Telegram with GNU General Public License v2.0 5 votes vote down vote up
@Override
public final void onLoadCompleted(
    int windowIndex,
    @Nullable MediaPeriodId mediaPeriodId,
    LoadEventInfo loadEventInfo,
    MediaLoadData mediaLoadData) {
  EventTime eventTime = generateMediaPeriodEventTime(windowIndex, mediaPeriodId);
  for (AnalyticsListener listener : listeners) {
    listener.onLoadCompleted(eventTime, loadEventInfo, mediaLoadData);
  }
}
 
Example #24
Source File: AnalyticsCollector.java    From Telegram with GNU General Public License v2.0 5 votes vote down vote up
private EventTime generateMediaPeriodEventTime(
    int windowIndex, @Nullable MediaPeriodId mediaPeriodId) {
  Assertions.checkNotNull(player);
  if (mediaPeriodId != null) {
    MediaPeriodInfo mediaPeriodInfo = mediaPeriodQueueTracker.getMediaPeriodInfo(mediaPeriodId);
    return mediaPeriodInfo != null
        ? generateEventTime(mediaPeriodInfo)
        : generateEventTime(Timeline.EMPTY, windowIndex, mediaPeriodId);
  }
  Timeline timeline = player.getCurrentTimeline();
  boolean windowIsInTimeline = windowIndex < timeline.getWindowCount();
  return generateEventTime(
      windowIsInTimeline ? timeline : Timeline.EMPTY, windowIndex, /* mediaPeriodId= */ null);
}
 
Example #25
Source File: ExoPlayerImpl.java    From TelePlus-Android with GNU General Public License v2.0 5 votes vote down vote up
@Override
public long getDuration() {
  Timeline timeline = playbackInfo.timeline;
  if (timeline.isEmpty()) {
    return C.TIME_UNSET;
  }
  if (isPlayingAd()) {
    MediaPeriodId periodId = playbackInfo.periodId;
    timeline.getPeriod(periodId.periodIndex, period);
    long adDurationUs = period.getAdDurationUs(periodId.adGroupIndex, periodId.adIndexInAdGroup);
    return C.usToMs(adDurationUs);
  } else {
    return timeline.getWindow(getCurrentWindowIndex(), window).getDurationMs();
  }
}
 
Example #26
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 #27
Source File: ExoPlayerImpl.java    From MediaSDK with Apache License 2.0 5 votes vote down vote up
private PlaybackInfo getResetPlaybackInfo(
    boolean resetPosition,
    boolean resetState,
    boolean resetError,
    @State int playbackState) {
  if (resetPosition) {
    maskingWindowIndex = 0;
    maskingPeriodIndex = 0;
    maskingWindowPositionMs = 0;
  } else {
    maskingWindowIndex = getCurrentWindowIndex();
    maskingPeriodIndex = getCurrentPeriodIndex();
    maskingWindowPositionMs = getCurrentPosition();
  }
  // Also reset period-based PlaybackInfo positions if resetting the state.
  resetPosition = resetPosition || resetState;
  MediaPeriodId mediaPeriodId =
      resetPosition
          ? playbackInfo.getDummyFirstMediaPeriodId(shuffleModeEnabled, window, period)
          : playbackInfo.periodId;
  long startPositionUs = resetPosition ? 0 : playbackInfo.positionUs;
  long contentPositionUs = resetPosition ? C.TIME_UNSET : playbackInfo.contentPositionUs;
  return new PlaybackInfo(
      resetState ? Timeline.EMPTY : playbackInfo.timeline,
      mediaPeriodId,
      startPositionUs,
      contentPositionUs,
      playbackState,
      resetError ? null : playbackInfo.playbackError,
      /* isLoading= */ false,
      resetState ? TrackGroupArray.EMPTY : playbackInfo.trackGroups,
      resetState ? emptyTrackSelectorResult : playbackInfo.trackSelectorResult,
      mediaPeriodId,
      startPositionUs,
      /* totalBufferedDurationUs= */ 0,
      startPositionUs);
}
 
Example #28
Source File: MediaSourceEventListener.java    From Telegram with GNU General Public License v2.0 5 votes vote down vote up
/** Dispatches {@link #onMediaPeriodCreated(int, MediaPeriodId)}. */
public void mediaPeriodCreated() {
  MediaPeriodId mediaPeriodId = Assertions.checkNotNull(this.mediaPeriodId);
  for (ListenerAndHandler listenerAndHandler : listenerAndHandlers) {
    final MediaSourceEventListener listener = listenerAndHandler.listener;
    postOrRun(
        listenerAndHandler.handler,
        () -> listener.onMediaPeriodCreated(windowIndex, mediaPeriodId));
  }
}
 
Example #29
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 #30
Source File: AnalyticsCollector.java    From TelePlus-Android with GNU General Public License v2.0 5 votes vote down vote up
@Override
public final void onLoadError(
    int windowIndex,
    @Nullable MediaPeriodId mediaPeriodId,
    LoadEventInfo loadEventInfo,
    MediaLoadData mediaLoadData,
    IOException error,
    boolean wasCanceled) {
  EventTime eventTime = generateEventTime(windowIndex, mediaPeriodId);
  for (AnalyticsListener listener : listeners) {
    listener.onLoadError(eventTime, loadEventInfo, mediaLoadData, error, wasCanceled);
  }
}