Java Code Examples for com.google.android.exoplayer2.source.TrackGroupArray#get()

The following examples show how to use com.google.android.exoplayer2.source.TrackGroupArray#get() . 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: DefaultTrackSelector.java    From K-Sonic with MIT License 6 votes vote down vote up
private static TrackSelection selectAdaptiveVideoTrack(RendererCapabilities rendererCapabilities,
    TrackGroupArray groups, int[][] formatSupport, int maxVideoWidth, int maxVideoHeight,
    int maxVideoBitrate, boolean allowNonSeamlessAdaptiveness, boolean allowMixedMimeAdaptiveness,
    int viewportWidth, int viewportHeight, boolean orientationMayChange,
    TrackSelection.Factory adaptiveVideoTrackSelectionFactory) throws ExoPlaybackException {
  int requiredAdaptiveSupport = allowNonSeamlessAdaptiveness
      ? (RendererCapabilities.ADAPTIVE_NOT_SEAMLESS | RendererCapabilities.ADAPTIVE_SEAMLESS)
      : RendererCapabilities.ADAPTIVE_SEAMLESS;
  boolean allowMixedMimeTypes = allowMixedMimeAdaptiveness
      && (rendererCapabilities.supportsMixedMimeTypeAdaptation() & requiredAdaptiveSupport) != 0;
  for (int i = 0; i < groups.length; i++) {
    TrackGroup group = groups.get(i);
    int[] adaptiveTracks = getAdaptiveTracksForGroup(group, formatSupport[i],
        allowMixedMimeTypes, requiredAdaptiveSupport, maxVideoWidth, maxVideoHeight,
        maxVideoBitrate, viewportWidth, viewportHeight, orientationMayChange);
    if (adaptiveTracks.length > 0) {
      return adaptiveVideoTrackSelectionFactory.createTrackSelection(group, adaptiveTracks);
    }
  }
  return null;
}
 
Example 2
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 3
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 4
Source File: ExoPlayerTrackSelector.java    From no-player with Apache License 2.0 5 votes vote down vote up
boolean supportsTrackSwitching(TrackType trackType,
                               RendererTypeRequester rendererTypeRequester,
                               TrackGroupArray trackGroups,
                               int groupIndex) {
    Optional<Integer> audioRendererIndex = rendererTrackIndexExtractor.extract(trackType, mappedTrackInfoLength(), rendererTypeRequester);
    return audioRendererIndex.isPresent()
            && trackGroups.get(groupIndex).length > 0
            && trackInfo().getAdaptiveSupport(audioRendererIndex.get(), groupIndex, false) != RendererCapabilities.ADAPTIVE_NOT_SUPPORTED;
}
 
Example 5
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 6
Source File: ExoPlayerFragment.java    From carstream-android-auto with Apache License 2.0 5 votes vote down vote up
@Override
public void onTracksChanged(TrackGroupArray trackGroups, TrackSelectionArray trackSelections) {
    playerQueue.resetPosition(player.getCurrentWindowIndex());
    String album = null;
    String title = null;
    for (int i = 0; i < trackGroups.length; i++) {
        TrackGroup trackGroup = trackGroups.get(i);
        for (int j = 0; j < trackGroup.length; j++) {
            Metadata trackMetadata = trackGroup.getFormat(j).metadata;
            if (trackMetadata != null) {
                for (int k = 0; k < trackMetadata.length(); k++) {
                    Metadata.Entry entry = trackMetadata.get(k);
                    if (entry instanceof TextInformationFrame) {
                        TextInformationFrame textInformationFrame = (TextInformationFrame) entry;
                        String id = textInformationFrame.id;
                        if (id != null && id.equals("TALB")) {
                            album = textInformationFrame.value;
                        } else if (id != null && id.equals("TIT2")) {
                            title = textInformationFrame.value;
                        }

                    }
                }
            }
        }
    }
    if (title != null || album != null) {
        if (title != null) {
            titleView.setText(title);
        }
        if (album != null) {
            albumView.setText(album);
        }
        BroadcastFromUI.broadcastTitle(getContext(), title);
    } else {
        File file = new File(playerQueue.current());
        titleView.setText(file.getName());
        albumView.setText(file.getParent());
    }
}
 
Example 7
Source File: DefaultTrackSelector.java    From K-Sonic with MIT License 5 votes vote down vote up
protected TrackSelection selectAudioTrack(TrackGroupArray groups, int[][] formatSupport,
    String preferredAudioLanguage, 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;
        if (formatHasLanguage(format, preferredAudioLanguage)) {
          if (isDefault) {
            trackScore = 4;
          } else {
            trackScore = 3;
          }
        } else if (isDefault) {
          trackScore = 2;
        } else {
          trackScore = 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 8
Source File: DefaultTrackSelector.java    From Telegram 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 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.
 * @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,
    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);
    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 9
Source File: ExoPlayerVideoTrackSelector.java    From no-player with Apache License 2.0 5 votes vote down vote up
public List<PlayerVideoTrack> getVideoTracks(RendererTypeRequester rendererTypeRequester, ContentType contentType) {
    TrackGroupArray trackGroups = trackSelector.trackGroups(VIDEO, rendererTypeRequester);

    List<PlayerVideoTrack> videoTracks = 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);

            PlayerVideoTrack playerVideoTrack = new PlayerVideoTrack(
                    groupIndex,
                    formatIndex,
                    format.id,
                    contentType,
                    format.width,
                    format.height,
                    (int) format.frameRate,
                    format.bitrate
            );

            videoTracks.add(playerVideoTrack);
        }
    }

    return videoTracks;
}
 
Example 10
Source File: DefaultTrackSelector.java    From Telegram-FOSS 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.
 */
@Nullable
protected TrackSelection.Definition 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 TrackSelection.Definition(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-FOSS with GNU General Public License v2.0 5 votes vote down vote up
@Nullable
private static TrackSelection.Definition selectAdaptiveVideoTrack(
    TrackGroupArray groups,
    int[][] formatSupport,
    int mixedMimeTypeAdaptationSupports,
    Parameters params) {
  int requiredAdaptiveSupport =
      params.allowVideoNonSeamlessAdaptiveness
          ? (RendererCapabilities.ADAPTIVE_NOT_SEAMLESS | RendererCapabilities.ADAPTIVE_SEAMLESS)
          : RendererCapabilities.ADAPTIVE_SEAMLESS;
  boolean allowMixedMimeTypes =
      params.allowVideoMixedMimeTypeAdaptiveness
          && (mixedMimeTypeAdaptationSupports & requiredAdaptiveSupport) != 0;
  for (int i = 0; i < groups.length; i++) {
    TrackGroup group = groups.get(i);
    int[] adaptiveTracks =
        getAdaptiveVideoTracksForGroup(
            group,
            formatSupport[i],
            allowMixedMimeTypes,
            requiredAdaptiveSupport,
            params.maxVideoWidth,
            params.maxVideoHeight,
            params.maxVideoFrameRate,
            params.maxVideoBitrate,
            params.viewportWidth,
            params.viewportHeight,
            params.viewportOrientationMayChange);
    if (adaptiveTracks.length > 0) {
      return new TrackSelection.Definition(group, adaptiveTracks);
    }
  }
  return null;
}
 
Example 14
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 15
Source File: DefaultTrackSelector.java    From TelePlus-Android with GNU General Public License v2.0 4 votes vote down vote up
@Override
protected final Pair<@NullableType RendererConfiguration[], @NullableType TrackSelection[]>
    selectTracks(
        MappedTrackInfo mappedTrackInfo,
        int[][][] rendererFormatSupports,
        int[] rendererMixedMimeTypeAdaptationSupports)
        throws ExoPlaybackException {
  Parameters params = parametersReference.get();
  int rendererCount = mappedTrackInfo.getRendererCount();
  @NullableType TrackSelection[] rendererTrackSelections =
      selectAllTracks(
          mappedTrackInfo,
          rendererFormatSupports,
          rendererMixedMimeTypeAdaptationSupports,
          params);

  // Apply track disabling and overriding.
  for (int i = 0; i < rendererCount; i++) {
    if (params.getRendererDisabled(i)) {
      rendererTrackSelections[i] = null;
    } else {
      TrackGroupArray rendererTrackGroups = mappedTrackInfo.getTrackGroups(i);
      if (params.hasSelectionOverride(i, rendererTrackGroups)) {
        SelectionOverride override = params.getSelectionOverride(i, rendererTrackGroups);
        if (override == null) {
          rendererTrackSelections[i] = null;
        } else if (override.length == 1) {
          rendererTrackSelections[i] =
              new FixedTrackSelection(
                  rendererTrackGroups.get(override.groupIndex), override.tracks[0]);
        } else {
          rendererTrackSelections[i] =
              Assertions.checkNotNull(adaptiveTrackSelectionFactory)
                  .createTrackSelection(
                      rendererTrackGroups.get(override.groupIndex),
                      getBandwidthMeter(),
                      override.tracks);
        }
      }
    }
  }

  // Initialize the renderer configurations to the default configuration for all renderers with
  // selections, and null otherwise.
  @NullableType RendererConfiguration[] rendererConfigurations =
      new RendererConfiguration[rendererCount];
  for (int i = 0; i < rendererCount; i++) {
    boolean forceRendererDisabled = params.getRendererDisabled(i);
    boolean rendererEnabled =
        !forceRendererDisabled
            && (mappedTrackInfo.getRendererType(i) == C.TRACK_TYPE_NONE
                || rendererTrackSelections[i] != null);
    rendererConfigurations[i] = rendererEnabled ? RendererConfiguration.DEFAULT : null;
  }

  // Configure audio and video renderers to use tunneling if appropriate.
  maybeConfigureRenderersForTunneling(
      mappedTrackInfo,
      rendererFormatSupports,
      rendererConfigurations,
      rendererTrackSelections,
      params.tunnelingAudioSessionId);

  return Pair.create(rendererConfigurations, rendererTrackSelections);
}
 
Example 16
Source File: DefaultTrackSelector.java    From TelePlus-Android with GNU General Public License v2.0 4 votes vote down vote up
@Override
protected final Pair<@NullableType RendererConfiguration[], @NullableType TrackSelection[]>
    selectTracks(
        MappedTrackInfo mappedTrackInfo,
        int[][][] rendererFormatSupports,
        int[] rendererMixedMimeTypeAdaptationSupports)
        throws ExoPlaybackException {
  Parameters params = parametersReference.get();
  int rendererCount = mappedTrackInfo.getRendererCount();
  @NullableType TrackSelection[] rendererTrackSelections =
      selectAllTracks(
          mappedTrackInfo,
          rendererFormatSupports,
          rendererMixedMimeTypeAdaptationSupports,
          params);

  // Apply track disabling and overriding.
  for (int i = 0; i < rendererCount; i++) {
    if (params.getRendererDisabled(i)) {
      rendererTrackSelections[i] = null;
    } else {
      TrackGroupArray rendererTrackGroups = mappedTrackInfo.getTrackGroups(i);
      if (params.hasSelectionOverride(i, rendererTrackGroups)) {
        SelectionOverride override = params.getSelectionOverride(i, rendererTrackGroups);
        if (override == null) {
          rendererTrackSelections[i] = null;
        } else if (override.length == 1) {
          rendererTrackSelections[i] =
              new FixedTrackSelection(
                  rendererTrackGroups.get(override.groupIndex), override.tracks[0]);
        } else {
          rendererTrackSelections[i] =
              Assertions.checkNotNull(adaptiveTrackSelectionFactory)
                  .createTrackSelection(
                      rendererTrackGroups.get(override.groupIndex),
                      getBandwidthMeter(),
                      override.tracks);
        }
      }
    }
  }

  // Initialize the renderer configurations to the default configuration for all renderers with
  // selections, and null otherwise.
  @NullableType RendererConfiguration[] rendererConfigurations =
      new RendererConfiguration[rendererCount];
  for (int i = 0; i < rendererCount; i++) {
    boolean forceRendererDisabled = params.getRendererDisabled(i);
    boolean rendererEnabled =
        !forceRendererDisabled
            && (mappedTrackInfo.getRendererType(i) == C.TRACK_TYPE_NONE
                || rendererTrackSelections[i] != null);
    rendererConfigurations[i] = rendererEnabled ? RendererConfiguration.DEFAULT : null;
  }

  // Configure audio and video renderers to use tunneling if appropriate.
  maybeConfigureRenderersForTunneling(
      mappedTrackInfo,
      rendererFormatSupports,
      rendererConfigurations,
      rendererTrackSelections,
      params.tunnelingAudioSessionId);

  return Pair.create(rendererConfigurations, rendererTrackSelections);
}
 
Example 17
Source File: DefaultTrackSelector.java    From Telegram-FOSS 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 18
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 19
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 20
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);
}