Java Code Examples for com.google.android.exoplayer2.C#msToUs()

The following examples show how to use com.google.android.exoplayer2.C#msToUs() . 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: DashDownloader.java    From Telegram-FOSS with GNU General Public License v2.0 6 votes vote down vote up
@Override
protected List<Segment> getSegments(
    DataSource dataSource, DashManifest manifest, boolean allowIncompleteList)
    throws InterruptedException, IOException {
  ArrayList<Segment> segments = new ArrayList<>();
  for (int i = 0; i < manifest.getPeriodCount(); i++) {
    Period period = manifest.getPeriod(i);
    long periodStartUs = C.msToUs(period.startMs);
    long periodDurationUs = manifest.getPeriodDurationUs(i);
    List<AdaptationSet> adaptationSets = period.adaptationSets;
    for (int j = 0; j < adaptationSets.size(); j++) {
      addSegmentsForAdaptationSet(
          dataSource,
          adaptationSets.get(j),
          periodStartUs,
          periodDurationUs,
          allowIncompleteList,
          segments);
    }
  }
  return segments;
}
 
Example 2
Source File: SimpleDecoderVideoRenderer.java    From MediaSDK with Apache License 2.0 6 votes vote down vote up
/**
 * Renders the specified output buffer.
 *
 * <p>The implementation of this method takes ownership of the output buffer and is responsible
 * for calling {@link VideoDecoderOutputBuffer#release()} either immediately or in the future.
 *
 * @param outputBuffer {@link VideoDecoderOutputBuffer} to render.
 * @param presentationTimeUs Presentation time in microseconds.
 * @param outputFormat Output {@link Format}.
 * @throws VideoDecoderException If an error occurs when rendering the output buffer.
 */
protected void renderOutputBuffer(
    VideoDecoderOutputBuffer outputBuffer, long presentationTimeUs, Format outputFormat)
    throws VideoDecoderException {
  lastRenderTimeUs = C.msToUs(SystemClock.elapsedRealtime() * 1000);
  int bufferMode = outputBuffer.mode;
  boolean renderSurface = bufferMode == C.VIDEO_OUTPUT_MODE_SURFACE_YUV && surface != null;
  boolean renderYuv = bufferMode == C.VIDEO_OUTPUT_MODE_YUV && outputBufferRenderer != null;
  if (!renderYuv && !renderSurface) {
    dropOutputBuffer(outputBuffer);
  } else {
    maybeNotifyVideoSizeChanged(outputBuffer.width, outputBuffer.height);
    if (renderYuv) {
      outputBufferRenderer.setOutputBuffer(outputBuffer);
    } else {
      renderOutputBufferToSurface(outputBuffer, surface);
    }
    consecutiveDroppedFrameCount = 0;
    decoderCounters.renderedOutputBufferCount++;
    maybeNotifyRenderedFirstFrame();
  }
}
 
Example 3
Source File: MlltSeeker.java    From Telegram with GNU General Public License v2.0 5 votes vote down vote up
private MlltSeeker(long[] referencePositions, long[] referenceTimesMs) {
  this.referencePositions = referencePositions;
  this.referenceTimesMs = referenceTimesMs;
  // Use the last reference point as the duration, as extrapolating variable bitrate at the end of
  // the stream may give a large error.
  durationUs = C.msToUs(referenceTimesMs[referenceTimesMs.length - 1]);
}
 
Example 4
Source File: StandaloneMediaClock.java    From Telegram-FOSS with GNU General Public License v2.0 5 votes vote down vote up
@Override
public long getPositionUs() {
  long positionUs = baseUs;
  if (started) {
    long elapsedSinceBaseMs = clock.elapsedRealtime() - baseElapsedMs;
    if (playbackParameters.speed == 1f) {
      positionUs += C.msToUs(elapsedSinceBaseMs);
    } else {
      positionUs += playbackParameters.getMediaTimeUsForPlayoutTimeMs(elapsedSinceBaseMs);
    }
  }
  return positionUs;
}
 
Example 5
Source File: DefaultDashChunkSource.java    From Telegram-FOSS with GNU General Public License v2.0 5 votes vote down vote up
public long getFirstAvailableSegmentNum(
    DashManifest manifest, int periodIndex, long nowUnixTimeUs) {
  if (getSegmentCount() == DashSegmentIndex.INDEX_UNBOUNDED
      && manifest.timeShiftBufferDepthMs != C.TIME_UNSET) {
    // The index is itself unbounded. We need to use the current time to calculate the range of
    // available segments.
    long liveEdgeTimeUs = nowUnixTimeUs - C.msToUs(manifest.availabilityStartTimeMs);
    long periodStartUs = C.msToUs(manifest.getPeriod(periodIndex).startMs);
    long liveEdgeTimeInPeriodUs = liveEdgeTimeUs - periodStartUs;
    long bufferDepthUs = C.msToUs(manifest.timeShiftBufferDepthMs);
    return Math.max(
        getFirstSegmentNum(), getSegmentNum(liveEdgeTimeInPeriodUs - bufferDepthUs));
  }
  return getFirstSegmentNum();
}
 
Example 6
Source File: StandaloneMediaClock.java    From TelePlus-Android with GNU General Public License v2.0 5 votes vote down vote up
@Override
public long getPositionUs() {
  long positionUs = baseUs;
  if (started) {
    long elapsedSinceBaseMs = clock.elapsedRealtime() - baseElapsedMs;
    if (playbackParameters.speed == 1f) {
      positionUs += C.msToUs(elapsedSinceBaseMs);
    } else {
      positionUs += playbackParameters.getMediaTimeUsForPlayoutTimeMs(elapsedSinceBaseMs);
    }
  }
  return positionUs;
}
 
Example 7
Source File: MlltSeeker.java    From Telegram with GNU General Public License v2.0 5 votes vote down vote up
@Override
public SeekPoints getSeekPoints(long timeUs) {
  timeUs = Util.constrainValue(timeUs, 0, durationUs);
  Pair<Long, Long> timeMsAndPosition =
      linearlyInterpolate(C.usToMs(timeUs), referenceTimesMs, referencePositions);
  timeUs = C.msToUs(timeMsAndPosition.first);
  long position = timeMsAndPosition.second;
  return new SeekPoints(new SeekPoint(timeUs, position));
}
 
Example 8
Source File: DefaultDashChunkSource.java    From Telegram with GNU General Public License v2.0 5 votes vote down vote up
public long getLastAvailableSegmentNum(
    DashManifest manifest, int periodIndex, long nowUnixTimeUs) {
  int availableSegmentCount = getSegmentCount();
  if (availableSegmentCount == DashSegmentIndex.INDEX_UNBOUNDED) {
    // The index is itself unbounded. We need to use the current time to calculate the range of
    // available segments.
    long liveEdgeTimeUs = nowUnixTimeUs - C.msToUs(manifest.availabilityStartTimeMs);
    long periodStartUs = C.msToUs(manifest.getPeriod(periodIndex).startMs);
    long liveEdgeTimeInPeriodUs = liveEdgeTimeUs - periodStartUs;
    // getSegmentNum(liveEdgeTimeInPeriodUs) will not be completed yet, so subtract one to get
    // the index of the last completed segment.
    return getSegmentNum(liveEdgeTimeInPeriodUs) - 1;
  }
  return getFirstSegmentNum() + availableSegmentCount - 1;
}
 
Example 9
Source File: StandaloneMediaClock.java    From MediaSDK with Apache License 2.0 5 votes vote down vote up
@Override
public long getPositionUs() {
  long positionUs = baseUs;
  if (started) {
    long elapsedSinceBaseMs = clock.elapsedRealtime() - baseElapsedMs;
    if (playbackParameters.speed == 1f) {
      positionUs += C.msToUs(elapsedSinceBaseMs);
    } else {
      positionUs += playbackParameters.getMediaTimeUsForPlayoutTimeMs(elapsedSinceBaseMs);
    }
  }
  return positionUs;
}
 
Example 10
Source File: DefaultDashChunkSource.java    From MediaSDK with Apache License 2.0 5 votes vote down vote up
public long getLastAvailableSegmentNum(
    DashManifest manifest, int periodIndex, long nowUnixTimeUs) {
  int availableSegmentCount = getSegmentCount();
  if (availableSegmentCount == DashSegmentIndex.INDEX_UNBOUNDED) {
    // The index is itself unbounded. We need to use the current time to calculate the range of
    // available segments.
    long liveEdgeTimeUs = nowUnixTimeUs - C.msToUs(manifest.availabilityStartTimeMs);
    long periodStartUs = C.msToUs(manifest.getPeriod(periodIndex).startMs);
    long liveEdgeTimeInPeriodUs = liveEdgeTimeUs - periodStartUs;
    // getSegmentNum(liveEdgeTimeInPeriodUs) will not be completed yet, so subtract one to get
    // the index of the last completed segment.
    return getSegmentNum(liveEdgeTimeInPeriodUs) - 1;
  }
  return getFirstSegmentNum() + availableSegmentCount - 1;
}
 
Example 11
Source File: MlltSeeker.java    From Telegram-FOSS with GNU General Public License v2.0 5 votes vote down vote up
private MlltSeeker(long[] referencePositions, long[] referenceTimesMs) {
  this.referencePositions = referencePositions;
  this.referenceTimesMs = referenceTimesMs;
  // Use the last reference point as the duration, as extrapolating variable bitrate at the end of
  // the stream may give a large error.
  durationUs = C.msToUs(referenceTimesMs[referenceTimesMs.length - 1]);
}
 
Example 12
Source File: DefaultDashChunkSource.java    From Telegram-FOSS with GNU General Public License v2.0 5 votes vote down vote up
public long getLastAvailableSegmentNum(
    DashManifest manifest, int periodIndex, long nowUnixTimeUs) {
  int availableSegmentCount = getSegmentCount();
  if (availableSegmentCount == DashSegmentIndex.INDEX_UNBOUNDED) {
    // The index is itself unbounded. We need to use the current time to calculate the range of
    // available segments.
    long liveEdgeTimeUs = nowUnixTimeUs - C.msToUs(manifest.availabilityStartTimeMs);
    long periodStartUs = C.msToUs(manifest.getPeriod(periodIndex).startMs);
    long liveEdgeTimeInPeriodUs = liveEdgeTimeUs - periodStartUs;
    // getSegmentNum(liveEdgeTimeInPeriodUs) will not be completed yet, so subtract one to get
    // the index of the last completed segment.
    return getSegmentNum(liveEdgeTimeInPeriodUs) - 1;
  }
  return getFirstSegmentNum() + availableSegmentCount - 1;
}
 
Example 13
Source File: DashMediaSource.java    From K-Sonic with MIT License 5 votes vote down vote up
private long getNowUnixTimeUs() {
  if (elapsedRealtimeOffsetMs != 0) {
    return C.msToUs(SystemClock.elapsedRealtime() + elapsedRealtimeOffsetMs);
  } else {
    return C.msToUs(System.currentTimeMillis());
  }
}
 
Example 14
Source File: MlltSeeker.java    From Telegram-FOSS with GNU General Public License v2.0 4 votes vote down vote up
@Override
public long getTimeUs(long position) {
  Pair<Long, Long> positionAndTimeMs =
      linearlyInterpolate(position, referencePositions, referenceTimesMs);
  return C.msToUs(positionAndTimeMs.second);
}
 
Example 15
Source File: MlltSeeker.java    From Telegram with GNU General Public License v2.0 4 votes vote down vote up
@Override
public long getTimeUs(long position) {
  Pair<Long, Long> positionAndTimeMs =
      linearlyInterpolate(position, referencePositions, referenceTimesMs);
  return C.msToUs(positionAndTimeMs.second);
}
 
Example 16
Source File: DashManifest.java    From TelePlus-Android with GNU General Public License v2.0 4 votes vote down vote up
public final long getPeriodDurationUs(int index) {
  return C.msToUs(getPeriodDurationMs(index));
}
 
Example 17
Source File: DashMediaSource.java    From K-Sonic with MIT License 4 votes vote down vote up
private void processManifest(boolean scheduleRefresh) {
  // Update any periods.
  for (int i = 0; i < periodsById.size(); i++) {
    int id = periodsById.keyAt(i);
    if (id >= firstPeriodId) {
      periodsById.valueAt(i).updateManifest(manifest, id - firstPeriodId);
    } else {
      // This period has been removed from the manifest so it doesn't need to be updated.
    }
  }
  // Update the window.
  boolean windowChangingImplicitly = false;
  int lastPeriodIndex = manifest.getPeriodCount() - 1;
  PeriodSeekInfo firstPeriodSeekInfo = PeriodSeekInfo.createPeriodSeekInfo(manifest.getPeriod(0),
      manifest.getPeriodDurationUs(0));
  PeriodSeekInfo lastPeriodSeekInfo = PeriodSeekInfo.createPeriodSeekInfo(
      manifest.getPeriod(lastPeriodIndex), manifest.getPeriodDurationUs(lastPeriodIndex));
  // Get the period-relative start/end times.
  long currentStartTimeUs = firstPeriodSeekInfo.availableStartTimeUs;
  long currentEndTimeUs = lastPeriodSeekInfo.availableEndTimeUs;
  if (manifest.dynamic && !lastPeriodSeekInfo.isIndexExplicit) {
    // The manifest describes an incomplete live stream. Update the start/end times to reflect the
    // live stream duration and the manifest's time shift buffer depth.
    long liveStreamDurationUs = getNowUnixTimeUs() - C.msToUs(manifest.availabilityStartTime);
    long liveStreamEndPositionInLastPeriodUs = liveStreamDurationUs
        - C.msToUs(manifest.getPeriod(lastPeriodIndex).startMs);
    currentEndTimeUs = Math.min(liveStreamEndPositionInLastPeriodUs, currentEndTimeUs);
    if (manifest.timeShiftBufferDepth != C.TIME_UNSET) {
      long timeShiftBufferDepthUs = C.msToUs(manifest.timeShiftBufferDepth);
      long offsetInPeriodUs = currentEndTimeUs - timeShiftBufferDepthUs;
      int periodIndex = lastPeriodIndex;
      while (offsetInPeriodUs < 0 && periodIndex > 0) {
        offsetInPeriodUs += manifest.getPeriodDurationUs(--periodIndex);
      }
      if (periodIndex == 0) {
        currentStartTimeUs = Math.max(currentStartTimeUs, offsetInPeriodUs);
      } else {
        // The time shift buffer starts after the earliest period.
        // TODO: Does this ever happen?
        currentStartTimeUs = manifest.getPeriodDurationUs(0);
      }
    }
    windowChangingImplicitly = true;
  }
  long windowDurationUs = currentEndTimeUs - currentStartTimeUs;
  for (int i = 0; i < manifest.getPeriodCount() - 1; i++) {
    windowDurationUs += manifest.getPeriodDurationUs(i);
  }
  long windowDefaultStartPositionUs = 0;
  if (manifest.dynamic) {
    long presentationDelayForManifestMs = livePresentationDelayMs;
    if (presentationDelayForManifestMs == DEFAULT_LIVE_PRESENTATION_DELAY_PREFER_MANIFEST_MS) {
      presentationDelayForManifestMs = manifest.suggestedPresentationDelay != C.TIME_UNSET
          ? manifest.suggestedPresentationDelay : DEFAULT_LIVE_PRESENTATION_DELAY_FIXED_MS;
    }
    // Snap the default position to the start of the segment containing it.
    windowDefaultStartPositionUs = windowDurationUs - C.msToUs(presentationDelayForManifestMs);
    if (windowDefaultStartPositionUs < MIN_LIVE_DEFAULT_START_POSITION_US) {
      // The default start position is too close to the start of the live window. Set it to the
      // minimum default start position provided the window is at least twice as big. Else set
      // it to the middle of the window.
      windowDefaultStartPositionUs = Math.min(MIN_LIVE_DEFAULT_START_POSITION_US,
          windowDurationUs / 2);
    }
  }
  long windowStartTimeMs = manifest.availabilityStartTime
      + manifest.getPeriod(0).startMs + C.usToMs(currentStartTimeUs);
  DashTimeline timeline = new DashTimeline(manifest.availabilityStartTime, windowStartTimeMs,
      firstPeriodId, currentStartTimeUs, windowDurationUs, windowDefaultStartPositionUs,
      manifest);
  sourceListener.onSourceInfoRefreshed(timeline, manifest);

  if (!sideloadedManifest) {
    // Remove any pending simulated refresh.
    handler.removeCallbacks(simulateManifestRefreshRunnable);
    // If the window is changing implicitly, post a simulated manifest refresh to update it.
    if (windowChangingImplicitly) {
      handler.postDelayed(simulateManifestRefreshRunnable, NOTIFY_MANIFEST_INTERVAL_MS);
    }
    // Schedule an explicit refresh if needed.
    if (scheduleRefresh) {
      scheduleManifestRefresh();
    }
  }
}
 
Example 18
Source File: DashManifest.java    From TelePlus-Android with GNU General Public License v2.0 4 votes vote down vote up
public final long getPeriodDurationUs(int index) {
  return C.msToUs(getPeriodDurationMs(index));
}
 
Example 19
Source File: MlltSeeker.java    From MediaSDK with Apache License 2.0 4 votes vote down vote up
@Override
public long getTimeUs(long position) {
  Pair<Long, Long> positionAndTimeMs =
      linearlyInterpolate(position, referencePositions, referenceTimesMs);
  return C.msToUs(positionAndTimeMs.second);
}
 
Example 20
Source File: DashManifest.java    From Telegram with GNU General Public License v2.0 4 votes vote down vote up
public final long getPeriodDurationUs(int index) {
  return C.msToUs(getPeriodDurationMs(index));
}