Java Code Examples for com.google.android.exoplayer2.C#INDEX_UNSET

The following examples show how to use com.google.android.exoplayer2.C#INDEX_UNSET . 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: CodecSpecificDataUtil.java    From K-Sonic with MIT License 6 votes vote down vote up
/**
 * Splits an array of NAL units.
 * <p>
 * If the input consists of NAL start code delimited units, then the returned array consists of
 * the split NAL units, each of which is still prefixed with the NAL start code. For any other
 * input, null is returned.
 *
 * @param data An array of data.
 * @return The individual NAL units, or null if the input did not consist of NAL start code
 *     delimited units.
 */
public static byte[][] splitNalUnits(byte[] data) {
  if (!isNalStartCode(data, 0)) {
    // data does not consist of NAL start code delimited units.
    return null;
  }
  List<Integer> starts = new ArrayList<>();
  int nalUnitIndex = 0;
  do {
    starts.add(nalUnitIndex);
    nalUnitIndex = findNalStartCode(data, nalUnitIndex + NAL_START_CODE.length);
  } while (nalUnitIndex != C.INDEX_UNSET);
  byte[][] split = new byte[starts.size()][];
  for (int i = 0; i < starts.size(); i++) {
    int startIndex = starts.get(i);
    int endIndex = i < starts.size() - 1 ? starts.get(i + 1) : data.length;
    byte[] nal = new byte[endIndex - startIndex];
    System.arraycopy(data, startIndex, nal, 0, nal.length);
    split[i] = nal;
  }
  return split;
}
 
Example 2
Source File: HlsChunkSource.java    From Telegram with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Called when a playlist load encounters an error.
 *
 * @param playlistUrl The {@link Uri} of the playlist whose load encountered an error.
 * @param blacklistDurationMs The duration for which the playlist should be blacklisted. Or {@link
 *     C#TIME_UNSET} if the playlist should not be blacklisted.
 * @return True if blacklisting did not encounter errors. False otherwise.
 */
public boolean onPlaylistError(Uri playlistUrl, long blacklistDurationMs) {
  int trackGroupIndex = C.INDEX_UNSET;
  for (int i = 0; i < playlistUrls.length; i++) {
    if (playlistUrls[i].equals(playlistUrl)) {
      trackGroupIndex = i;
      break;
    }
  }
  if (trackGroupIndex == C.INDEX_UNSET) {
    return true;
  }
  int trackSelectionIndex = trackSelection.indexOf(trackGroupIndex);
  if (trackSelectionIndex == C.INDEX_UNSET) {
    return true;
  }
  seenExpectedPlaylistError |= playlistUrl.equals(expectedPlaylistUrl);
  return blacklistDurationMs == C.TIME_UNSET
      || trackSelection.blacklist(trackSelectionIndex, blacklistDurationMs);
}
 
Example 3
Source File: DownloadManager.java    From Telegram-FOSS with GNU General Public License v2.0 6 votes vote down vote up
private Download putDownload(Download download) {
  // Downloads in terminal states shouldn't be in the downloads list.
  Assertions.checkState(download.state != STATE_COMPLETED && download.state != STATE_FAILED);
  int changedIndex = getDownloadIndex(download.request.id);
  if (changedIndex == C.INDEX_UNSET) {
    downloads.add(download);
    Collections.sort(downloads, InternalHandler::compareStartTimes);
  } else {
    boolean needsSort = download.startTimeMs != downloads.get(changedIndex).startTimeMs;
    downloads.set(changedIndex, download);
    if (needsSort) {
      Collections.sort(downloads, InternalHandler::compareStartTimes);
    }
  }
  try {
    downloadIndex.putDownload(download);
  } catch (IOException e) {
    Log.e(TAG, "Failed to update index.", e);
  }
  DownloadUpdate update =
      new DownloadUpdate(download, /* isRemove= */ false, new ArrayList<>(downloads));
  mainHandler.obtainMessage(MSG_DOWNLOAD_UPDATE, update).sendToTarget();
  return download;
}
 
Example 4
Source File: Mp4Extractor.java    From TelePlus-Android with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Returns the index of the track that contains the next sample to be read, or {@link
 * C#INDEX_UNSET} if no samples remain.
 *
 * <p>The preferred choice is the sample with the smallest offset not requiring a source reload,
 * or if not available the sample with the smallest overall offset to avoid subsequent source
 * reloads.
 *
 * <p>To deal with poor sample interleaving, we also check whether the required memory to catch up
 * with the next logical sample (based on sample time) exceeds {@link
 * #MAXIMUM_READ_AHEAD_BYTES_STREAM}. If this is the case, we continue with this sample even
 * though it may require a source reload.
 */
private int getTrackIndexOfNextReadSample(long inputPosition) {
  long preferredSkipAmount = Long.MAX_VALUE;
  boolean preferredRequiresReload = true;
  int preferredTrackIndex = C.INDEX_UNSET;
  long preferredAccumulatedBytes = Long.MAX_VALUE;
  long minAccumulatedBytes = Long.MAX_VALUE;
  boolean minAccumulatedBytesRequiresReload = true;
  int minAccumulatedBytesTrackIndex = C.INDEX_UNSET;
  for (int trackIndex = 0; trackIndex < tracks.length; trackIndex++) {
    Mp4Track track = tracks[trackIndex];
    int sampleIndex = track.sampleIndex;
    if (sampleIndex == track.sampleTable.sampleCount) {
      continue;
    }
    long sampleOffset = track.sampleTable.offsets[sampleIndex];
    long sampleAccumulatedBytes = accumulatedSampleSizes[trackIndex][sampleIndex];
    long skipAmount = sampleOffset - inputPosition;
    boolean requiresReload = skipAmount < 0 || skipAmount >= RELOAD_MINIMUM_SEEK_DISTANCE;
    if ((!requiresReload && preferredRequiresReload)
        || (requiresReload == preferredRequiresReload && skipAmount < preferredSkipAmount)) {
      preferredRequiresReload = requiresReload;
      preferredSkipAmount = skipAmount;
      preferredTrackIndex = trackIndex;
      preferredAccumulatedBytes = sampleAccumulatedBytes;
    }
    if (sampleAccumulatedBytes < minAccumulatedBytes) {
      minAccumulatedBytes = sampleAccumulatedBytes;
      minAccumulatedBytesRequiresReload = requiresReload;
      minAccumulatedBytesTrackIndex = trackIndex;
    }
  }
  return minAccumulatedBytes == Long.MAX_VALUE
          || !minAccumulatedBytesRequiresReload
          || preferredAccumulatedBytes < minAccumulatedBytes + MAXIMUM_READ_AHEAD_BYTES_STREAM
      ? preferredTrackIndex
      : minAccumulatedBytesTrackIndex;
}
 
Example 5
Source File: TrackSampleTable.java    From TelePlus-Android with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Returns the sample index of the closest synchronization sample at or after the given timestamp,
 * if one is available.
 *
 * @param timeUs Timestamp adjacent to which to find a synchronization sample.
 * @return index Index of the synchronization sample, or {@link C#INDEX_UNSET} if none.
 */
public int getIndexOfLaterOrEqualSynchronizationSample(long timeUs) {
  int startIndex = Util.binarySearchCeil(timestampsUs, timeUs, true, false);
  for (int i = startIndex; i < timestampsUs.length; i++) {
    if ((flags[i] & C.BUFFER_FLAG_KEY_FRAME) != 0) {
      return i;
    }
  }
  return C.INDEX_UNSET;
}
 
Example 6
Source File: ExoVideoPlaybackControlView.java    From ExoVideoView with Apache License 2.0 5 votes vote down vote up
private void next() {
    Timeline timeline = player.getCurrentTimeline();
    if (timeline.isEmpty()) {
        return;
    }
    int windowIndex = player.getCurrentWindowIndex();
    int nextWindowIndex = player.getNextWindowIndex();
    if (nextWindowIndex != C.INDEX_UNSET) {
        seekTo(nextWindowIndex, C.TIME_UNSET);
    } else if (timeline.getWindow(windowIndex, window, false).isDynamic) {
        seekTo(windowIndex, C.TIME_UNSET);
    }
}
 
Example 7
Source File: DashUtil.java    From Telegram-FOSS with GNU General Public License v2.0 5 votes vote down vote up
private static @Nullable Representation getFirstRepresentation(Period period, int type) {
  int index = period.getAdaptationSetIndex(type);
  if (index == C.INDEX_UNSET) {
    return null;
  }
  List<Representation> representations = period.adaptationSets.get(index).representations;
  return representations.isEmpty() ? null : representations.get(0);
}
 
Example 8
Source File: BaseTrackSelection.java    From TelePlus-Android with GNU General Public License v2.0 5 votes vote down vote up
@Override
@SuppressWarnings("ReferenceEquality")
public final int indexOf(Format format) {
  for (int i = 0; i < length; i++) {
    if (formats[i] == format) {
      return i;
    }
  }
  return C.INDEX_UNSET;
}
 
Example 9
Source File: Ac3Util.java    From Telegram with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Returns the offset relative to the buffer's position of the start of a TrueHD syncframe, or
 * {@link C#INDEX_UNSET} if no syncframe was found. The buffer's position is not modified.
 *
 * @param buffer The {@link ByteBuffer} within which to find a syncframe.
 * @return The offset relative to the buffer's position of the start of a TrueHD syncframe, or
 *     {@link C#INDEX_UNSET} if no syncframe was found.
 */
public static int findTrueHdSyncframeOffset(ByteBuffer buffer) {
  int startIndex = buffer.position();
  int endIndex = buffer.limit() - TRUEHD_SYNCFRAME_PREFIX_LENGTH;
  for (int i = startIndex; i <= endIndex; i++) {
    // The syncword ends 0xBA for TrueHD or 0xBB for MLP.
    if ((buffer.getInt(i + 4) & 0xFEFFFFFF) == 0xBA6F72F8) {
      return i - startIndex;
    }
  }
  return C.INDEX_UNSET;
}
 
Example 10
Source File: CodecSpecificDataUtil.java    From TelePlus-Android with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Finds the next occurrence of the NAL start code from a given index.
 *
 * @param data The data in which to search.
 * @param index The first index to test.
 * @return The index of the first byte of the found start code, or {@link C#INDEX_UNSET}.
 */
private static int findNalStartCode(byte[] data, int index) {
  int endIndex = data.length - NAL_START_CODE.length;
  for (int i = index; i <= endIndex; i++) {
    if (isNalStartCode(data, i)) {
      return i;
    }
  }
  return C.INDEX_UNSET;
}
 
Example 11
Source File: BaseTrackSelection.java    From Telegram with GNU General Public License v2.0 5 votes vote down vote up
@Override
@SuppressWarnings("ReferenceEquality")
public final int indexOf(Format format) {
  for (int i = 0; i < length; i++) {
    if (formats[i] == format) {
      return i;
    }
  }
  return C.INDEX_UNSET;
}
 
Example 12
Source File: DashMediaSource.java    From Telegram-FOSS with GNU General Public License v2.0 4 votes vote down vote up
private long getAdjustedWindowDefaultStartPositionUs(long defaultPositionProjectionUs) {
  long windowDefaultStartPositionUs = this.windowDefaultStartPositionUs;
  if (!manifest.dynamic) {
    return windowDefaultStartPositionUs;
  }
  if (defaultPositionProjectionUs > 0) {
    windowDefaultStartPositionUs += defaultPositionProjectionUs;
    if (windowDefaultStartPositionUs > windowDurationUs) {
      // The projection takes us beyond the end of the live window.
      return C.TIME_UNSET;
    }
  }
  // Attempt to snap to the start of the corresponding video segment.
  int periodIndex = 0;
  long defaultStartPositionInPeriodUs = offsetInFirstPeriodUs + windowDefaultStartPositionUs;
  long periodDurationUs = manifest.getPeriodDurationUs(periodIndex);
  while (periodIndex < manifest.getPeriodCount() - 1
      && defaultStartPositionInPeriodUs >= periodDurationUs) {
    defaultStartPositionInPeriodUs -= periodDurationUs;
    periodIndex++;
    periodDurationUs = manifest.getPeriodDurationUs(periodIndex);
  }
  com.google.android.exoplayer2.source.dash.manifest.Period period =
      manifest.getPeriod(periodIndex);
  int videoAdaptationSetIndex = period.getAdaptationSetIndex(C.TRACK_TYPE_VIDEO);
  if (videoAdaptationSetIndex == C.INDEX_UNSET) {
    // No video adaptation set for snapping.
    return windowDefaultStartPositionUs;
  }
  // If there are multiple video adaptation sets with unaligned segments, the initial time may
  // not correspond to the start of a segment in both, but this is an edge case.
  DashSegmentIndex snapIndex = period.adaptationSets.get(videoAdaptationSetIndex)
      .representations.get(0).getIndex();
  if (snapIndex == null || snapIndex.getSegmentCount(periodDurationUs) == 0) {
    // Video adaptation set does not include a non-empty index for snapping.
    return windowDefaultStartPositionUs;
  }
  long segmentNum = snapIndex.getSegmentNum(defaultStartPositionInPeriodUs, periodDurationUs);
  return windowDefaultStartPositionUs + snapIndex.getTimeUs(segmentNum)
      - defaultStartPositionInPeriodUs;
}
 
Example 13
Source File: PlayerActivity.java    From leafpicrevived with GNU General Public License v3.0 4 votes vote down vote up
private void clearResumePosition() {
    resumeWindow = C.INDEX_UNSET;
    resumePosition = C.TIME_UNSET;
}
 
Example 14
Source File: ShuffleOrder.java    From TelePlus-Android with GNU General Public License v2.0 4 votes vote down vote up
@Override
public int getFirstIndex() {
  return length > 0 ? 0 : C.INDEX_UNSET;
}
 
Example 15
Source File: ShuffleOrder.java    From TelePlus-Android with GNU General Public License v2.0 4 votes vote down vote up
@Override
public int getLastIndex() {
  return shuffled.length > 0 ? shuffled[shuffled.length - 1] : C.INDEX_UNSET;
}
 
Example 16
Source File: VideoActivity.java    From evercam-android with GNU Affero General Public License v3.0 4 votes vote down vote up
private void clearResumePosition() {
    resumeWindow = C.INDEX_UNSET;
    resumePosition = C.TIME_UNSET;
}
 
Example 17
Source File: MediaSource.java    From TelePlus-Android with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Returns whether this period identifier identifies an ad in an ad group in a period.
 */
public boolean isAd() {
  return adGroupIndex != C.INDEX_UNSET;
}
 
Example 18
Source File: ReactExoplayerView.java    From react-native-video with MIT License 4 votes vote down vote up
private void clearResumePosition() {
    resumeWindow = C.INDEX_UNSET;
    resumePosition = C.TIME_UNSET;
}
 
Example 19
Source File: ShuffleOrder.java    From TelePlus-Android with GNU General Public License v2.0 4 votes vote down vote up
@Override
public int getNextIndex(int index) {
  int shuffledIndex = indexInShuffled[index];
  return ++shuffledIndex < shuffled.length ? shuffled[shuffledIndex] : C.INDEX_UNSET;
}
 
Example 20
Source File: AdPlaybackState.java    From Telegram with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Returns the index of the ad group at or before {@code positionUs}, if that ad group is
 * unplayed. Returns {@link C#INDEX_UNSET} if the ad group at or before {@code positionUs} has no
 * ads remaining to be played, or if there is no such ad group.
 *
 * @param positionUs The position at or before which to find an ad group, in microseconds, or
 *     {@link C#TIME_END_OF_SOURCE} for the end of the stream (in which case the index of any
 *     unplayed postroll ad group will be returned).
 * @return The index of the ad group, or {@link C#INDEX_UNSET}.
 */
public int getAdGroupIndexForPositionUs(long positionUs) {
  // Use a linear search as the array elements may not be increasing due to TIME_END_OF_SOURCE.
  // In practice we expect there to be few ad groups so the search shouldn't be expensive.
  int index = adGroupTimesUs.length - 1;
  while (index >= 0 && isPositionBeforeAdGroup(positionUs, index)) {
    index--;
  }
  return index >= 0 && adGroups[index].hasUnplayedAds() ? index : C.INDEX_UNSET;
}