Java Code Examples for com.google.android.exoplayer2.source.TrackGroup#getFormat()

The following examples show how to use com.google.android.exoplayer2.source.TrackGroup#getFormat() . 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: BaseTrackSelection.java    From Telegram with GNU General Public License v2.0 6 votes vote down vote up
/**
 * @param group The {@link TrackGroup}. Must not be null.
 * @param tracks The indices of the selected tracks within the {@link TrackGroup}. Must not be
 *     null or empty. May be in any order.
 */
public BaseTrackSelection(TrackGroup group, int... tracks) {
  Assertions.checkState(tracks.length > 0);
  this.group = Assertions.checkNotNull(group);
  this.length = tracks.length;
  // Set the formats, sorted in order of decreasing bandwidth.
  formats = new Format[length];
  for (int i = 0; i < tracks.length; i++) {
    formats[i] = group.getFormat(tracks[i]);
  }
  Arrays.sort(formats, new DecreasingBandwidthComparator());
  // Set the format indices in the same order.
  this.tracks = new int[length];
  for (int i = 0; i < length; i++) {
    this.tracks[i] = group.indexOf(formats[i]);
  }
  blacklistUntilTimes = new long[length];
}
 
Example 2
Source File: BaseTrackSelection.java    From Telegram-FOSS with GNU General Public License v2.0 6 votes vote down vote up
/**
 * @param group The {@link TrackGroup}. Must not be null.
 * @param tracks The indices of the selected tracks within the {@link TrackGroup}. Must not be
 *     null or empty. May be in any order.
 */
public BaseTrackSelection(TrackGroup group, int... tracks) {
  Assertions.checkState(tracks.length > 0);
  this.group = Assertions.checkNotNull(group);
  this.length = tracks.length;
  // Set the formats, sorted in order of decreasing bandwidth.
  formats = new Format[length];
  for (int i = 0; i < tracks.length; i++) {
    formats[i] = group.getFormat(tracks[i]);
  }
  Arrays.sort(formats, new DecreasingBandwidthComparator());
  // Set the format indices in the same order.
  this.tracks = new int[length];
  for (int i = 0; i < length; i++) {
    this.tracks[i] = group.indexOf(formats[i]);
  }
  blacklistUntilTimes = new long[length];
}
 
Example 3
Source File: ExoPlayerSubtitleTrackSelector.java    From no-player with Apache License 2.0 6 votes vote down vote up
public List<PlayerSubtitleTrack> getSubtitleTracks(RendererTypeRequester rendererTypeRequester) {
    TrackGroupArray trackGroups = trackSelector.trackGroups(TEXT, rendererTypeRequester);

    List<PlayerSubtitleTrack> subtitleTracks = new ArrayList<>();

    for (int groupIndex = 0; groupIndex < trackGroups.length; groupIndex++) {
        TrackGroup trackGroup = trackGroups.get(groupIndex);

        for (int formatIndex = 0; formatIndex < trackGroup.length; formatIndex++) {
            Format format = trackGroup.getFormat(formatIndex);
            PlayerSubtitleTrack playerSubtitleTrack = new PlayerSubtitleTrack(
                    groupIndex,
                    formatIndex,
                    format.id,
                    format.language,
                    format.sampleMimeType,
                    format.channelCount,
                    format.bitrate
            );
            subtitleTracks.add(playerSubtitleTrack);
        }
    }

    return subtitleTracks;
}
 
Example 4
Source File: BaseTrackSelection.java    From TelePlus-Android with GNU General Public License v2.0 6 votes vote down vote up
/**
 * @param group The {@link TrackGroup}. Must not be null.
 * @param tracks The indices of the selected tracks within the {@link TrackGroup}. Must not be
 *     null or empty. May be in any order.
 */
public BaseTrackSelection(TrackGroup group, int... tracks) {
  Assertions.checkState(tracks.length > 0);
  this.group = Assertions.checkNotNull(group);
  this.length = tracks.length;
  // Set the formats, sorted in order of decreasing bandwidth.
  formats = new Format[length];
  for (int i = 0; i < tracks.length; i++) {
    formats[i] = group.getFormat(tracks[i]);
  }
  Arrays.sort(formats, new DecreasingBandwidthComparator());
  // Set the format indices in the same order.
  this.tracks = new int[length];
  for (int i = 0; i < length; i++) {
    this.tracks[i] = group.indexOf(formats[i]);
  }
  blacklistUntilTimes = new long[length];
}
 
Example 5
Source File: DefaultTrackSelector.java    From TelePlus-Android with GNU General Public License v2.0 5 votes vote down vote up
private static int[] getAdaptiveAudioTracks(TrackGroup group, int[] formatSupport,
    boolean allowMixedMimeTypes) {
  int selectedConfigurationTrackCount = 0;
  AudioConfigurationTuple selectedConfiguration = null;
  HashSet<AudioConfigurationTuple> seenConfigurationTuples = new HashSet<>();
  for (int i = 0; i < group.length; i++) {
    Format format = group.getFormat(i);
    AudioConfigurationTuple configuration = new AudioConfigurationTuple(
        format.channelCount, format.sampleRate,
        allowMixedMimeTypes ? null : format.sampleMimeType);
    if (seenConfigurationTuples.add(configuration)) {
      int configurationCount = getAdaptiveAudioTrackCount(group, formatSupport, configuration);
      if (configurationCount > selectedConfigurationTrackCount) {
        selectedConfiguration = configuration;
        selectedConfigurationTrackCount = configurationCount;
      }
    }
  }

  if (selectedConfigurationTrackCount > 1) {
    int[] adaptiveIndices = new int[selectedConfigurationTrackCount];
    int index = 0;
    for (int i = 0; i < group.length; i++) {
      if (isSupportedAdaptiveAudioTrack(
          group.getFormat(i), formatSupport[i], Assertions.checkNotNull(selectedConfiguration))) {
        adaptiveIndices[index++] = i;
      }
    }
    return adaptiveIndices;
  }
  return NO_TRACKS;
}
 
Example 6
Source File: DefaultTrackSelector.java    From K-Sonic with MIT License 5 votes vote down vote up
private static int[] getAdaptiveTracksForGroup(TrackGroup group, int[] formatSupport,
    boolean allowMixedMimeTypes, int requiredAdaptiveSupport, int maxVideoWidth,
    int maxVideoHeight, int maxVideoBitrate, int viewportWidth, int viewportHeight,
    boolean orientationMayChange) {
  if (group.length < 2) {
    return NO_TRACKS;
  }

  List<Integer> selectedTrackIndices = getViewportFilteredTrackIndices(group, viewportWidth,
      viewportHeight, orientationMayChange);
  if (selectedTrackIndices.size() < 2) {
    return NO_TRACKS;
  }

  String selectedMimeType = null;
  if (!allowMixedMimeTypes) {
    // Select the mime type for which we have the most adaptive tracks.
    HashSet<String> seenMimeTypes = new HashSet<>();
    int selectedMimeTypeTrackCount = 0;
    for (int i = 0; i < selectedTrackIndices.size(); i++) {
      int trackIndex = selectedTrackIndices.get(i);
      String sampleMimeType = group.getFormat(trackIndex).sampleMimeType;
      if (seenMimeTypes.add(sampleMimeType)) {
        int countForMimeType = getAdaptiveTrackCountForMimeType(group, formatSupport,
            requiredAdaptiveSupport, sampleMimeType, maxVideoWidth, maxVideoHeight,
            maxVideoBitrate, selectedTrackIndices);
        if (countForMimeType > selectedMimeTypeTrackCount) {
          selectedMimeType = sampleMimeType;
          selectedMimeTypeTrackCount = countForMimeType;
        }
      }
    }
  }

  // Filter by the selected mime type.
  filterAdaptiveTrackCountForMimeType(group, formatSupport, requiredAdaptiveSupport,
      selectedMimeType, maxVideoWidth, maxVideoHeight, maxVideoBitrate, selectedTrackIndices);

  return selectedTrackIndices.size() < 2 ? NO_TRACKS : Util.toArray(selectedTrackIndices);
}
 
Example 7
Source File: ReactExoplayerView.java    From react-native-video with MIT License 5 votes vote down vote up
private WritableArray getVideoTrackInfo() {
    WritableArray videoTracks = Arguments.createArray();

    MappingTrackSelector.MappedTrackInfo info = trackSelector.getCurrentMappedTrackInfo();
    int index = getTrackRendererIndex(C.TRACK_TYPE_VIDEO);
    if (info == null || index == C.INDEX_UNSET) {
        return videoTracks;
    }

    TrackGroupArray groups = info.getTrackGroups(index);
    for (int i = 0; i < groups.length; ++i) {
        TrackGroup group = groups.get(i);

        for (int trackIndex = 0; trackIndex < group.length; trackIndex++) {
            Format format = group.getFormat(trackIndex);
            WritableMap videoTrack = Arguments.createMap();
            videoTrack.putInt("width", format.width == Format.NO_VALUE ? 0 : format.width);
            videoTrack.putInt("height",format.height == Format.NO_VALUE ? 0 : format.height);
            videoTrack.putInt("bitrate", format.bitrate == Format.NO_VALUE ? 0 : format.bitrate);
            videoTrack.putString("codecs", format.codecs != null ? format.codecs : "");
            videoTrack.putString("trackId",
                    format.id == null ? String.valueOf(trackIndex) : format.id);
            videoTracks.pushMap(videoTrack);
        }
    }
    return videoTracks;
}
 
Example 8
Source File: DefaultTrackSelector.java    From TelePlus-Android with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Called by {@link #selectAllTracks(MappedTrackInfo, int[][][], int[], Parameters)} to create a
 * {@link TrackSelection} for a renderer whose type is neither video, audio or text.
 *
 * @param trackType The type of the renderer.
 * @param groups The {@link TrackGroupArray} mapped to the renderer.
 * @param formatSupport The result of {@link RendererCapabilities#supportsFormat} for each mapped
 *     track, indexed by track group index and track index (in that order).
 * @param params The selector's current constraint parameters.
 * @return The {@link TrackSelection} for the renderer, or null if no selection was made.
 * @throws ExoPlaybackException If an error occurs while selecting the tracks.
 */
protected @Nullable TrackSelection selectOtherTrack(
    int trackType, TrackGroupArray groups, int[][] formatSupport, Parameters params)
    throws ExoPlaybackException {
  TrackGroup selectedGroup = null;
  int selectedTrackIndex = 0;
  int selectedTrackScore = 0;
  for (int groupIndex = 0; groupIndex < groups.length; groupIndex++) {
    TrackGroup trackGroup = groups.get(groupIndex);
    int[] trackFormatSupport = formatSupport[groupIndex];
    for (int trackIndex = 0; trackIndex < trackGroup.length; trackIndex++) {
      if (isSupported(trackFormatSupport[trackIndex],
          params.exceedRendererCapabilitiesIfNecessary)) {
        Format format = trackGroup.getFormat(trackIndex);
        boolean isDefault = (format.selectionFlags & C.SELECTION_FLAG_DEFAULT) != 0;
        int trackScore = isDefault ? 2 : 1;
        if (isSupported(trackFormatSupport[trackIndex], false)) {
          trackScore += WITHIN_RENDERER_CAPABILITIES_BONUS;
        }
        if (trackScore > selectedTrackScore) {
          selectedGroup = trackGroup;
          selectedTrackIndex = trackIndex;
          selectedTrackScore = trackScore;
        }
      }
    }
  }
  return selectedGroup == null ? null
      : new FixedTrackSelection(selectedGroup, selectedTrackIndex);
}
 
Example 9
Source File: DefaultTrackSelector.java    From TelePlus-Android with GNU General Public License v2.0 5 votes vote down vote up
private static int[] getAdaptiveVideoTracksForGroup(TrackGroup group, int[] formatSupport,
    boolean allowMixedMimeTypes, int requiredAdaptiveSupport, int maxVideoWidth,
    int maxVideoHeight, int maxVideoBitrate, int viewportWidth, int viewportHeight,
    boolean viewportOrientationMayChange) {
  if (group.length < 2) {
    return NO_TRACKS;
  }

  List<Integer> selectedTrackIndices = getViewportFilteredTrackIndices(group, viewportWidth,
      viewportHeight, viewportOrientationMayChange);
  if (selectedTrackIndices.size() < 2) {
    return NO_TRACKS;
  }

  String selectedMimeType = null;
  if (!allowMixedMimeTypes) {
    // Select the mime type for which we have the most adaptive tracks.
    HashSet<@NullableType String> seenMimeTypes = new HashSet<>();
    int selectedMimeTypeTrackCount = 0;
    for (int i = 0; i < selectedTrackIndices.size(); i++) {
      int trackIndex = selectedTrackIndices.get(i);
      String sampleMimeType = group.getFormat(trackIndex).sampleMimeType;
      if (seenMimeTypes.add(sampleMimeType)) {
        int countForMimeType = getAdaptiveVideoTrackCountForMimeType(group, formatSupport,
            requiredAdaptiveSupport, sampleMimeType, maxVideoWidth, maxVideoHeight,
            maxVideoBitrate, selectedTrackIndices);
        if (countForMimeType > selectedMimeTypeTrackCount) {
          selectedMimeType = sampleMimeType;
          selectedMimeTypeTrackCount = countForMimeType;
        }
      }
    }
  }

  // Filter by the selected mime type.
  filterAdaptiveVideoTrackCountForMimeType(group, formatSupport, requiredAdaptiveSupport,
      selectedMimeType, maxVideoWidth, maxVideoHeight, maxVideoBitrate, selectedTrackIndices);

  return selectedTrackIndices.size() < 2 ? NO_TRACKS : Util.toArray(selectedTrackIndices);
}
 
Example 10
Source File: DefaultTrackSelector.java    From K-Sonic with MIT License 5 votes vote down vote up
protected TrackSelection selectOtherTrack(int trackType, TrackGroupArray groups,
    int[][] formatSupport, boolean exceedRendererCapabilitiesIfNecessary) {
  TrackGroup selectedGroup = null;
  int selectedTrackIndex = 0;
  int selectedTrackScore = 0;
  for (int groupIndex = 0; groupIndex < groups.length; groupIndex++) {
    TrackGroup trackGroup = groups.get(groupIndex);
    int[] trackFormatSupport = formatSupport[groupIndex];
    for (int trackIndex = 0; trackIndex < trackGroup.length; trackIndex++) {
      if (isSupported(trackFormatSupport[trackIndex], exceedRendererCapabilitiesIfNecessary)) {
        Format format = trackGroup.getFormat(trackIndex);
        boolean isDefault = (format.selectionFlags & C.SELECTION_FLAG_DEFAULT) != 0;
        int trackScore = isDefault ? 2 : 1;
        if (isSupported(trackFormatSupport[trackIndex], false)) {
          trackScore += WITHIN_RENDERER_CAPABILITIES_BONUS;
        }
        if (trackScore > selectedTrackScore) {
          selectedGroup = trackGroup;
          selectedTrackIndex = trackIndex;
          selectedTrackScore = trackScore;
        }
      }
    }
  }
  return selectedGroup == null ? null
      : new FixedTrackSelection(selectedGroup, selectedTrackIndex);
}
 
Example 11
Source File: DefaultTrackSelector.java    From MediaSDK with Apache License 2.0 5 votes vote down vote up
/**
 * Called by {@link #selectAllTracks(MappedTrackInfo, int[][][], int[], Parameters)} to create a
 * {@link TrackSelection} for a renderer whose type is neither video, audio or text.
 *
 * @param trackType The type of the renderer.
 * @param groups The {@link TrackGroupArray} mapped to the renderer.
 * @param formatSupport The {@link Capabilities} for each mapped track, indexed by renderer, track
 *     group and track (in that order).
 * @param params The selector's current constraint parameters.
 * @return The {@link TrackSelection} for the renderer, or null if no selection was made.
 * @throws ExoPlaybackException If an error occurs while selecting the tracks.
 */
@Nullable
protected TrackSelection.Definition selectOtherTrack(
    int trackType, TrackGroupArray groups, @Capabilities int[][] formatSupport, Parameters params)
    throws ExoPlaybackException {
  TrackGroup selectedGroup = null;
  int selectedTrackIndex = 0;
  int selectedTrackScore = 0;
  for (int groupIndex = 0; groupIndex < groups.length; groupIndex++) {
    TrackGroup trackGroup = groups.get(groupIndex);
    @Capabilities int[] trackFormatSupport = formatSupport[groupIndex];
    for (int trackIndex = 0; trackIndex < trackGroup.length; trackIndex++) {
      if (isSupported(trackFormatSupport[trackIndex],
          params.exceedRendererCapabilitiesIfNecessary)) {
        Format format = trackGroup.getFormat(trackIndex);
        boolean isDefault = (format.selectionFlags & C.SELECTION_FLAG_DEFAULT) != 0;
        int trackScore = isDefault ? 2 : 1;
        if (isSupported(trackFormatSupport[trackIndex], false)) {
          trackScore += WITHIN_RENDERER_CAPABILITIES_BONUS;
        }
        if (trackScore > selectedTrackScore) {
          selectedGroup = trackGroup;
          selectedTrackIndex = trackIndex;
          selectedTrackScore = trackScore;
        }
      }
    }
  }
  return selectedGroup == null
      ? null
      : new TrackSelection.Definition(selectedGroup, selectedTrackIndex);
}
 
Example 12
Source File: DefaultTrackSelector.java    From MediaSDK with Apache License 2.0 5 votes vote down vote up
/**
 * Called by {@link #selectAllTracks(MappedTrackInfo, int[][][], int[], Parameters)} to create a
 * {@link TrackSelection} for a text renderer.
 *
 * @param groups The {@link TrackGroupArray} mapped to the renderer.
 * @param formatSupport The {@link Capabilities} for each mapped track, indexed by renderer, track
 *     group and track (in that order).
 * @param params The selector's current constraint parameters.
 * @param selectedAudioLanguage The language of the selected audio track. May be null if the
 *     selected text track declares no language or no text track was selected.
 * @return The {@link TrackSelection.Definition} and corresponding {@link TextTrackScore}, or null
 *     if no selection was made.
 * @throws ExoPlaybackException If an error occurs while selecting the tracks.
 */
@Nullable
protected Pair<TrackSelection.Definition, TextTrackScore> selectTextTrack(
    TrackGroupArray groups,
    @Capabilities int[][] formatSupport,
    Parameters params,
    @Nullable String selectedAudioLanguage)
    throws ExoPlaybackException {
  TrackGroup selectedGroup = null;
  int selectedTrackIndex = C.INDEX_UNSET;
  TextTrackScore selectedTrackScore = null;
  for (int groupIndex = 0; groupIndex < groups.length; groupIndex++) {
    TrackGroup trackGroup = groups.get(groupIndex);
    @Capabilities int[] trackFormatSupport = formatSupport[groupIndex];
    for (int trackIndex = 0; trackIndex < trackGroup.length; trackIndex++) {
      if (isSupported(trackFormatSupport[trackIndex],
          params.exceedRendererCapabilitiesIfNecessary)) {
        Format format = trackGroup.getFormat(trackIndex);
        TextTrackScore trackScore =
            new TextTrackScore(
                format, params, trackFormatSupport[trackIndex], selectedAudioLanguage);
        if (trackScore.isWithinConstraints
            && (selectedTrackScore == null || trackScore.compareTo(selectedTrackScore) > 0)) {
          selectedGroup = trackGroup;
          selectedTrackIndex = trackIndex;
          selectedTrackScore = trackScore;
        }
      }
    }
  }
  return selectedGroup == null
      ? null
      : Pair.create(
          new TrackSelection.Definition(selectedGroup, selectedTrackIndex),
          Assertions.checkNotNull(selectedTrackScore));
}
 
Example 13
Source File: DefaultTrackSelector.java    From Telegram with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Called by {@link #selectAllTracks(MappedTrackInfo, int[][][], int[], Parameters)} to create a
 * {@link TrackSelection} for an audio renderer.
 *
 * @param groups The {@link TrackGroupArray} mapped to the renderer.
 * @param formatSupports The result of {@link RendererCapabilities#supportsFormat} for each mapped
 *     track, indexed by track group index and track index (in that order).
 * @param mixedMimeTypeAdaptationSupports The result of {@link
 *     RendererCapabilities#supportsMixedMimeTypeAdaptation()} for the renderer.
 * @param params The selector's current constraint parameters.
 * @param enableAdaptiveTrackSelection Whether adaptive track selection is allowed.
 * @return The {@link TrackSelection.Definition} and corresponding {@link AudioTrackScore}, or
 *     null if no selection was made.
 * @throws ExoPlaybackException If an error occurs while selecting the tracks.
 */
@SuppressWarnings("unused")
@Nullable
protected Pair<TrackSelection.Definition, AudioTrackScore> selectAudioTrack(
    TrackGroupArray groups,
    int[][] formatSupports,
    int mixedMimeTypeAdaptationSupports,
    Parameters params,
    boolean enableAdaptiveTrackSelection)
    throws ExoPlaybackException {
  int selectedTrackIndex = C.INDEX_UNSET;
  int selectedGroupIndex = C.INDEX_UNSET;
  AudioTrackScore selectedTrackScore = null;
  for (int groupIndex = 0; groupIndex < groups.length; groupIndex++) {
    TrackGroup trackGroup = groups.get(groupIndex);
    int[] trackFormatSupport = formatSupports[groupIndex];
    for (int trackIndex = 0; trackIndex < trackGroup.length; trackIndex++) {
      if (isSupported(trackFormatSupport[trackIndex],
          params.exceedRendererCapabilitiesIfNecessary)) {
        Format format = trackGroup.getFormat(trackIndex);
        AudioTrackScore trackScore =
            new AudioTrackScore(format, params, trackFormatSupport[trackIndex]);
        if (!trackScore.isWithinConstraints && !params.exceedAudioConstraintsIfNecessary) {
          // Track should not be selected.
          continue;
        }
        if (selectedTrackScore == null || trackScore.compareTo(selectedTrackScore) > 0) {
          selectedGroupIndex = groupIndex;
          selectedTrackIndex = trackIndex;
          selectedTrackScore = trackScore;
        }
      }
    }
  }

  if (selectedGroupIndex == C.INDEX_UNSET) {
    return null;
  }

  TrackGroup selectedGroup = groups.get(selectedGroupIndex);

  TrackSelection.Definition definition = null;
  if (!params.forceHighestSupportedBitrate
      && !params.forceLowestBitrate
      && enableAdaptiveTrackSelection) {
    // If the group of the track with the highest score allows it, try to enable adaptation.
    int[] adaptiveTracks =
        getAdaptiveAudioTracks(
            selectedGroup,
            formatSupports[selectedGroupIndex],
            params.maxAudioBitrate,
            params.allowAudioMixedMimeTypeAdaptiveness,
            params.allowAudioMixedSampleRateAdaptiveness,
            params.allowAudioMixedChannelCountAdaptiveness);
    if (adaptiveTracks.length > 0) {
      definition = new TrackSelection.Definition(selectedGroup, adaptiveTracks);
    }
  }
  if (definition == null) {
    // We didn't make an adaptive selection, so make a fixed one instead.
    definition = new TrackSelection.Definition(selectedGroup, selectedTrackIndex);
  }

  return Pair.create(definition, Assertions.checkNotNull(selectedTrackScore));
}
 
Example 14
Source File: DefaultTrackSelector.java    From TelePlus-Android with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Called by {@link #selectAllTracks(MappedTrackInfo, int[][][], int[], Parameters)} to create a
 * {@link TrackSelection} for an audio renderer.
 *
 * @param groups The {@link TrackGroupArray} mapped to the renderer.
 * @param formatSupports The result of {@link RendererCapabilities#supportsFormat} for each mapped
 *     track, indexed by track group index and track index (in that order).
 * @param mixedMimeTypeAdaptationSupports The result of {@link
 *     RendererCapabilities#supportsMixedMimeTypeAdaptation()} for the renderer.
 * @param params The selector's current constraint parameters.
 * @param adaptiveTrackSelectionFactory A factory for generating adaptive track selections, or
 *     null if a fixed track selection is required.
 * @return The {@link TrackSelection} for the renderer, or null if no selection was made.
 * @throws ExoPlaybackException If an error occurs while selecting the tracks.
 */
protected @Nullable TrackSelection selectAudioTrack(
    TrackGroupArray groups,
    int[][] formatSupports,
    int mixedMimeTypeAdaptationSupports,
    Parameters params,
    @Nullable TrackSelection.Factory adaptiveTrackSelectionFactory)
    throws ExoPlaybackException {
  int selectedTrackIndex = C.INDEX_UNSET;
  int selectedGroupIndex = C.INDEX_UNSET;
  AudioTrackScore selectedTrackScore = null;
  for (int groupIndex = 0; groupIndex < groups.length; groupIndex++) {
    TrackGroup trackGroup = groups.get(groupIndex);
    int[] trackFormatSupport = formatSupports[groupIndex];
    for (int trackIndex = 0; trackIndex < trackGroup.length; trackIndex++) {
      if (isSupported(trackFormatSupport[trackIndex],
          params.exceedRendererCapabilitiesIfNecessary)) {
        Format format = trackGroup.getFormat(trackIndex);
        AudioTrackScore trackScore =
            new AudioTrackScore(format, params, trackFormatSupport[trackIndex]);
        if (selectedTrackScore == null || trackScore.compareTo(selectedTrackScore) > 0) {
          selectedGroupIndex = groupIndex;
          selectedTrackIndex = trackIndex;
          selectedTrackScore = trackScore;
        }
      }
    }
  }

  if (selectedGroupIndex == C.INDEX_UNSET) {
    return null;
  }

  TrackGroup selectedGroup = groups.get(selectedGroupIndex);
  if (!params.forceLowestBitrate && adaptiveTrackSelectionFactory != null) {
    // If the group of the track with the highest score allows it, try to enable adaptation.
    int[] adaptiveTracks =
        getAdaptiveAudioTracks(
            selectedGroup, formatSupports[selectedGroupIndex], params.allowMixedMimeAdaptiveness);
    if (adaptiveTracks.length > 0) {
      return adaptiveTrackSelectionFactory
          .createTrackSelection(selectedGroup, getBandwidthMeter(), adaptiveTracks);
    }
  }
  return new FixedTrackSelection(selectedGroup, selectedTrackIndex);
}
 
Example 15
Source File: DefaultTrackSelector.java    From Telegram-FOSS with GNU General Public License v2.0 4 votes vote down vote up
@Nullable
private static TrackSelection.Definition selectFixedVideoTrack(
    TrackGroupArray groups, int[][] formatSupports, Parameters params) {
  TrackGroup selectedGroup = null;
  int selectedTrackIndex = 0;
  int selectedTrackScore = 0;
  int selectedBitrate = Format.NO_VALUE;
  int selectedPixelCount = Format.NO_VALUE;
  for (int groupIndex = 0; groupIndex < groups.length; groupIndex++) {
    TrackGroup trackGroup = groups.get(groupIndex);
    List<Integer> selectedTrackIndices = getViewportFilteredTrackIndices(trackGroup,
        params.viewportWidth, params.viewportHeight, params.viewportOrientationMayChange);
    int[] trackFormatSupport = formatSupports[groupIndex];
    for (int trackIndex = 0; trackIndex < trackGroup.length; trackIndex++) {
      if (isSupported(trackFormatSupport[trackIndex],
          params.exceedRendererCapabilitiesIfNecessary)) {
        Format format = trackGroup.getFormat(trackIndex);
        boolean isWithinConstraints =
            selectedTrackIndices.contains(trackIndex)
                && (format.width == Format.NO_VALUE || format.width <= params.maxVideoWidth)
                && (format.height == Format.NO_VALUE || format.height <= params.maxVideoHeight)
                && (format.frameRate == Format.NO_VALUE
                    || format.frameRate <= params.maxVideoFrameRate)
                && (format.bitrate == Format.NO_VALUE
                    || format.bitrate <= params.maxVideoBitrate);
        if (!isWithinConstraints && !params.exceedVideoConstraintsIfNecessary) {
          // Track should not be selected.
          continue;
        }
        int trackScore = isWithinConstraints ? 2 : 1;
        boolean isWithinCapabilities = isSupported(trackFormatSupport[trackIndex], false);
        if (isWithinCapabilities) {
          trackScore += WITHIN_RENDERER_CAPABILITIES_BONUS;
        }
        boolean selectTrack = trackScore > selectedTrackScore;
        if (trackScore == selectedTrackScore) {
          int bitrateComparison = compareFormatValues(format.bitrate, selectedBitrate);
          if (params.forceLowestBitrate && bitrateComparison != 0) {
            // Use bitrate as a tie breaker, preferring the lower bitrate.
            selectTrack = bitrateComparison < 0;
          } else {
            // Use the pixel count as a tie breaker (or bitrate if pixel counts are tied). If
            // we're within constraints prefer a higher pixel count (or bitrate), else prefer a
            // lower count (or bitrate). If still tied then prefer the first track (i.e. the one
            // that's already selected).
            int formatPixelCount = format.getPixelCount();
            int comparisonResult = formatPixelCount != selectedPixelCount
                ? compareFormatValues(formatPixelCount, selectedPixelCount)
                : compareFormatValues(format.bitrate, selectedBitrate);
            selectTrack = isWithinCapabilities && isWithinConstraints
                ? comparisonResult > 0 : comparisonResult < 0;
          }
        }
        if (selectTrack) {
          selectedGroup = trackGroup;
          selectedTrackIndex = trackIndex;
          selectedTrackScore = trackScore;
          selectedBitrate = format.bitrate;
          selectedPixelCount = format.getPixelCount();
        }
      }
    }
  }
  return selectedGroup == null
      ? null
      : new TrackSelection.Definition(selectedGroup, selectedTrackIndex);
}
 
Example 16
Source File: DefaultTrackSelector.java    From TelePlus-Android with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Called by {@link #selectAllTracks(MappedTrackInfo, int[][][], int[], Parameters)} to create a
 * {@link TrackSelection} for a text renderer.
 *
 * @param groups The {@link TrackGroupArray} mapped to the renderer.
 * @param formatSupport The result of {@link RendererCapabilities#supportsFormat} for each mapped
 *     track, indexed by track group index and track index (in that order).
 * @param params The selector's current constraint parameters.
 * @return The {@link TrackSelection} for the renderer, or null if no selection was made.
 * @throws ExoPlaybackException If an error occurs while selecting the tracks.
 */
protected @Nullable TrackSelection selectTextTrack(
    TrackGroupArray groups, int[][] formatSupport, Parameters params)
    throws ExoPlaybackException {
  TrackGroup selectedGroup = null;
  int selectedTrackIndex = 0;
  int selectedTrackScore = 0;
  for (int groupIndex = 0; groupIndex < groups.length; groupIndex++) {
    TrackGroup trackGroup = groups.get(groupIndex);
    int[] trackFormatSupport = formatSupport[groupIndex];
    for (int trackIndex = 0; trackIndex < trackGroup.length; trackIndex++) {
      if (isSupported(trackFormatSupport[trackIndex],
          params.exceedRendererCapabilitiesIfNecessary)) {
        Format format = trackGroup.getFormat(trackIndex);
        int maskedSelectionFlags =
            format.selectionFlags & ~params.disabledTextTrackSelectionFlags;
        boolean isDefault = (maskedSelectionFlags & C.SELECTION_FLAG_DEFAULT) != 0;
        boolean isForced = (maskedSelectionFlags & C.SELECTION_FLAG_FORCED) != 0;
        int trackScore;
        boolean preferredLanguageFound = formatHasLanguage(format, params.preferredTextLanguage);
        if (preferredLanguageFound
            || (params.selectUndeterminedTextLanguage && formatHasNoLanguage(format))) {
          if (isDefault) {
            trackScore = 8;
          } else if (!isForced) {
            // Prefer non-forced to forced if a preferred text language has been specified. Where
            // both are provided the non-forced track will usually contain the forced subtitles as
            // a subset.
            trackScore = 6;
          } else {
            trackScore = 4;
          }
          trackScore += preferredLanguageFound ? 1 : 0;
        } else if (isDefault) {
          trackScore = 3;
        } else if (isForced) {
          if (formatHasLanguage(format, params.preferredAudioLanguage)) {
            trackScore = 2;
          } else {
            trackScore = 1;
          }
        } else {
          // Track should not be selected.
          continue;
        }
        if (isSupported(trackFormatSupport[trackIndex], false)) {
          trackScore += WITHIN_RENDERER_CAPABILITIES_BONUS;
        }
        if (trackScore > selectedTrackScore) {
          selectedGroup = trackGroup;
          selectedTrackIndex = trackIndex;
          selectedTrackScore = trackScore;
        }
      }
    }
  }
  return selectedGroup == null ? null
      : new FixedTrackSelection(selectedGroup, selectedTrackIndex);
}
 
Example 17
Source File: DashMediaPeriod.java    From Telegram-FOSS with GNU General Public License v2.0 4 votes vote down vote up
private ChunkSampleStream<DashChunkSource> buildSampleStream(TrackGroupInfo trackGroupInfo,
    TrackSelection selection, long positionUs) {
  int embeddedTrackCount = 0;
  boolean enableEventMessageTrack =
      trackGroupInfo.embeddedEventMessageTrackGroupIndex != C.INDEX_UNSET;
  TrackGroup embeddedEventMessageTrackGroup = null;
  if (enableEventMessageTrack) {
    embeddedEventMessageTrackGroup =
        trackGroups.get(trackGroupInfo.embeddedEventMessageTrackGroupIndex);
    embeddedTrackCount++;
  }
  boolean enableCea608Tracks = trackGroupInfo.embeddedCea608TrackGroupIndex != C.INDEX_UNSET;
  TrackGroup embeddedCea608TrackGroup = null;
  if (enableCea608Tracks) {
    embeddedCea608TrackGroup = trackGroups.get(trackGroupInfo.embeddedCea608TrackGroupIndex);
    embeddedTrackCount += embeddedCea608TrackGroup.length;
  }

  Format[] embeddedTrackFormats = new Format[embeddedTrackCount];
  int[] embeddedTrackTypes = new int[embeddedTrackCount];
  embeddedTrackCount = 0;
  if (enableEventMessageTrack) {
    embeddedTrackFormats[embeddedTrackCount] = embeddedEventMessageTrackGroup.getFormat(0);
    embeddedTrackTypes[embeddedTrackCount] = C.TRACK_TYPE_METADATA;
    embeddedTrackCount++;
  }
  List<Format> embeddedCea608TrackFormats = new ArrayList<>();
  if (enableCea608Tracks) {
    for (int i = 0; i < embeddedCea608TrackGroup.length; i++) {
      embeddedTrackFormats[embeddedTrackCount] = embeddedCea608TrackGroup.getFormat(i);
      embeddedTrackTypes[embeddedTrackCount] = C.TRACK_TYPE_TEXT;
      embeddedCea608TrackFormats.add(embeddedTrackFormats[embeddedTrackCount]);
      embeddedTrackCount++;
    }
  }

  PlayerTrackEmsgHandler trackPlayerEmsgHandler =
      manifest.dynamic && enableEventMessageTrack
          ? playerEmsgHandler.newPlayerTrackEmsgHandler()
          : null;
  DashChunkSource chunkSource =
      chunkSourceFactory.createDashChunkSource(
          manifestLoaderErrorThrower,
          manifest,
          periodIndex,
          trackGroupInfo.adaptationSetIndices,
          selection,
          trackGroupInfo.trackType,
          elapsedRealtimeOffsetMs,
          enableEventMessageTrack,
          embeddedCea608TrackFormats,
          trackPlayerEmsgHandler,
          transferListener);
  ChunkSampleStream<DashChunkSource> stream =
      new ChunkSampleStream<>(
          trackGroupInfo.trackType,
          embeddedTrackTypes,
          embeddedTrackFormats,
          chunkSource,
          this,
          allocator,
          positionUs,
          loadErrorHandlingPolicy,
          eventDispatcher);
  synchronized (this) {
    // The map is also accessed on the loading thread so synchronize access.
    trackEmsgHandlerBySampleStream.put(stream, trackPlayerEmsgHandler);
  }
  return stream;
}
 
Example 18
Source File: DefaultTrackSelector.java    From Telegram with GNU General Public License v2.0 4 votes vote down vote up
@Nullable
private static TrackSelection.Definition selectFixedVideoTrack(
    TrackGroupArray groups, int[][] formatSupports, Parameters params) {
  TrackGroup selectedGroup = null;
  int selectedTrackIndex = 0;
  int selectedTrackScore = 0;
  int selectedBitrate = Format.NO_VALUE;
  int selectedPixelCount = Format.NO_VALUE;
  for (int groupIndex = 0; groupIndex < groups.length; groupIndex++) {
    TrackGroup trackGroup = groups.get(groupIndex);
    List<Integer> selectedTrackIndices = getViewportFilteredTrackIndices(trackGroup,
        params.viewportWidth, params.viewportHeight, params.viewportOrientationMayChange);
    int[] trackFormatSupport = formatSupports[groupIndex];
    for (int trackIndex = 0; trackIndex < trackGroup.length; trackIndex++) {
      if (isSupported(trackFormatSupport[trackIndex],
          params.exceedRendererCapabilitiesIfNecessary)) {
        Format format = trackGroup.getFormat(trackIndex);
        boolean isWithinConstraints =
            selectedTrackIndices.contains(trackIndex)
                && (format.width == Format.NO_VALUE || format.width <= params.maxVideoWidth)
                && (format.height == Format.NO_VALUE || format.height <= params.maxVideoHeight)
                && (format.frameRate == Format.NO_VALUE
                    || format.frameRate <= params.maxVideoFrameRate)
                && (format.bitrate == Format.NO_VALUE
                    || format.bitrate <= params.maxVideoBitrate);
        if (!isWithinConstraints && !params.exceedVideoConstraintsIfNecessary) {
          // Track should not be selected.
          continue;
        }
        int trackScore = isWithinConstraints ? 2 : 1;
        boolean isWithinCapabilities = isSupported(trackFormatSupport[trackIndex], false);
        if (isWithinCapabilities) {
          trackScore += WITHIN_RENDERER_CAPABILITIES_BONUS;
        }
        boolean selectTrack = trackScore > selectedTrackScore;
        if (trackScore == selectedTrackScore) {
          int bitrateComparison = compareFormatValues(format.bitrate, selectedBitrate);
          if (params.forceLowestBitrate && bitrateComparison != 0) {
            // Use bitrate as a tie breaker, preferring the lower bitrate.
            selectTrack = bitrateComparison < 0;
          } else {
            // Use the pixel count as a tie breaker (or bitrate if pixel counts are tied). If
            // we're within constraints prefer a higher pixel count (or bitrate), else prefer a
            // lower count (or bitrate). If still tied then prefer the first track (i.e. the one
            // that's already selected).
            int formatPixelCount = format.getPixelCount();
            int comparisonResult = formatPixelCount != selectedPixelCount
                ? compareFormatValues(formatPixelCount, selectedPixelCount)
                : compareFormatValues(format.bitrate, selectedBitrate);
            selectTrack = isWithinCapabilities && isWithinConstraints
                ? comparisonResult > 0 : comparisonResult < 0;
          }
        }
        if (selectTrack) {
          selectedGroup = trackGroup;
          selectedTrackIndex = trackIndex;
          selectedTrackScore = trackScore;
          selectedBitrate = format.bitrate;
          selectedPixelCount = format.getPixelCount();
        }
      }
    }
  }
  return selectedGroup == null
      ? null
      : new TrackSelection.Definition(selectedGroup, selectedTrackIndex);
}
 
Example 19
Source File: DefaultTrackSelector.java    From TelePlus-Android with GNU General Public License v2.0 4 votes vote down vote up
private static @Nullable TrackSelection selectFixedVideoTrack(
    TrackGroupArray groups, int[][] formatSupports, Parameters params) {
  TrackGroup selectedGroup = null;
  int selectedTrackIndex = 0;
  int selectedTrackScore = 0;
  int selectedBitrate = Format.NO_VALUE;
  int selectedPixelCount = Format.NO_VALUE;
  for (int groupIndex = 0; groupIndex < groups.length; groupIndex++) {
    TrackGroup trackGroup = groups.get(groupIndex);
    List<Integer> selectedTrackIndices = getViewportFilteredTrackIndices(trackGroup,
        params.viewportWidth, params.viewportHeight, params.viewportOrientationMayChange);
    int[] trackFormatSupport = formatSupports[groupIndex];
    for (int trackIndex = 0; trackIndex < trackGroup.length; trackIndex++) {
      if (isSupported(trackFormatSupport[trackIndex],
          params.exceedRendererCapabilitiesIfNecessary)) {
        Format format = trackGroup.getFormat(trackIndex);
        boolean isWithinConstraints = selectedTrackIndices.contains(trackIndex)
            && (format.width == Format.NO_VALUE || format.width <= params.maxVideoWidth)
            && (format.height == Format.NO_VALUE || format.height <= params.maxVideoHeight)
            && (format.bitrate == Format.NO_VALUE || format.bitrate <= params.maxVideoBitrate);
        if (!isWithinConstraints && !params.exceedVideoConstraintsIfNecessary) {
          // Track should not be selected.
          continue;
        }
        int trackScore = isWithinConstraints ? 2 : 1;
        boolean isWithinCapabilities = isSupported(trackFormatSupport[trackIndex], false);
        if (isWithinCapabilities) {
          trackScore += WITHIN_RENDERER_CAPABILITIES_BONUS;
        }
        boolean selectTrack = trackScore > selectedTrackScore;
        if (trackScore == selectedTrackScore) {
          if (params.forceLowestBitrate) {
            // Use bitrate as a tie breaker, preferring the lower bitrate.
            selectTrack = compareFormatValues(format.bitrate, selectedBitrate) < 0;
          } else {
            // Use the pixel count as a tie breaker (or bitrate if pixel counts are tied). If
            // we're within constraints prefer a higher pixel count (or bitrate), else prefer a
            // lower count (or bitrate). If still tied then prefer the first track (i.e. the one
            // that's already selected).
            int formatPixelCount = format.getPixelCount();
            int comparisonResult = formatPixelCount != selectedPixelCount
                ? compareFormatValues(formatPixelCount, selectedPixelCount)
                : compareFormatValues(format.bitrate, selectedBitrate);
            selectTrack = isWithinCapabilities && isWithinConstraints
                ? comparisonResult > 0 : comparisonResult < 0;
          }
        }
        if (selectTrack) {
          selectedGroup = trackGroup;
          selectedTrackIndex = trackIndex;
          selectedTrackScore = trackScore;
          selectedBitrate = format.bitrate;
          selectedPixelCount = format.getPixelCount();
        }
      }
    }
  }
  return selectedGroup == null ? null
      : new FixedTrackSelection(selectedGroup, selectedTrackIndex);
}
 
Example 20
Source File: DefaultTrackSelector.java    From MediaSDK with Apache License 2.0 4 votes vote down vote up
@Nullable
private static TrackSelection.Definition selectFixedVideoTrack(
    TrackGroupArray groups, @Capabilities int[][] formatSupports, Parameters params) {
  TrackGroup selectedGroup = null;
  int selectedTrackIndex = 0;
  int selectedTrackScore = 0;
  int selectedBitrate = Format.NO_VALUE;
  int selectedPixelCount = Format.NO_VALUE;
  for (int groupIndex = 0; groupIndex < groups.length; groupIndex++) {
    TrackGroup trackGroup = groups.get(groupIndex);
    List<Integer> selectedTrackIndices = getViewportFilteredTrackIndices(trackGroup,
        params.viewportWidth, params.viewportHeight, params.viewportOrientationMayChange);
    @Capabilities int[] trackFormatSupport = formatSupports[groupIndex];
    for (int trackIndex = 0; trackIndex < trackGroup.length; trackIndex++) {
      if (isSupported(trackFormatSupport[trackIndex],
          params.exceedRendererCapabilitiesIfNecessary)) {
        Format format = trackGroup.getFormat(trackIndex);
        boolean isWithinConstraints =
            selectedTrackIndices.contains(trackIndex)
                && (format.width == Format.NO_VALUE || format.width <= params.maxVideoWidth)
                && (format.height == Format.NO_VALUE || format.height <= params.maxVideoHeight)
                && (format.frameRate == Format.NO_VALUE
                    || format.frameRate <= params.maxVideoFrameRate)
                && (format.bitrate == Format.NO_VALUE
                    || format.bitrate <= params.maxVideoBitrate);
        if (!isWithinConstraints && !params.exceedVideoConstraintsIfNecessary) {
          // Track should not be selected.
          continue;
        }
        int trackScore = isWithinConstraints ? 2 : 1;
        boolean isWithinCapabilities = isSupported(trackFormatSupport[trackIndex], false);
        if (isWithinCapabilities) {
          trackScore += WITHIN_RENDERER_CAPABILITIES_BONUS;
        }
        boolean selectTrack = trackScore > selectedTrackScore;
        if (trackScore == selectedTrackScore) {
          int bitrateComparison = compareFormatValues(format.bitrate, selectedBitrate);
          if (params.forceLowestBitrate && bitrateComparison != 0) {
            // Use bitrate as a tie breaker, preferring the lower bitrate.
            selectTrack = bitrateComparison < 0;
          } else {
            // Use the pixel count as a tie breaker (or bitrate if pixel counts are tied). If
            // we're within constraints prefer a higher pixel count (or bitrate), else prefer a
            // lower count (or bitrate). If still tied then prefer the first track (i.e. the one
            // that's already selected).
            int formatPixelCount = format.getPixelCount();
            int comparisonResult = formatPixelCount != selectedPixelCount
                ? compareFormatValues(formatPixelCount, selectedPixelCount)
                : compareFormatValues(format.bitrate, selectedBitrate);
            selectTrack = isWithinCapabilities && isWithinConstraints
                ? comparisonResult > 0 : comparisonResult < 0;
          }
        }
        if (selectTrack) {
          selectedGroup = trackGroup;
          selectedTrackIndex = trackIndex;
          selectedTrackScore = trackScore;
          selectedBitrate = format.bitrate;
          selectedPixelCount = format.getPixelCount();
        }
      }
    }
  }
  return selectedGroup == null
      ? null
      : new TrackSelection.Definition(selectedGroup, selectedTrackIndex);
}