Search in sources :

Example 76 with TrackGroupArray

use of com.google.android.exoplayer2.source.TrackGroupArray in project ExoPlayer by google.

the class DefaultTrackSelector method selectAdaptiveVideoTrack.

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;
}
Also used : TrackGroup(com.google.android.exoplayer2.source.TrackGroup) Point(android.graphics.Point)

Example 77 with TrackGroupArray

use of com.google.android.exoplayer2.source.TrackGroupArray in project ExoPlayer by google.

the class DefaultTrackSelector method selectFixedVideoTrack.

private static TrackSelection selectFixedVideoTrack(TrackGroupArray groups, int[][] formatSupport, int maxVideoWidth, int maxVideoHeight, int maxVideoBitrate, int viewportWidth, int viewportHeight, boolean orientationMayChange, boolean exceedConstraintsIfNecessary, boolean exceedRendererCapabilitiesIfNecessary) {
    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, viewportWidth, viewportHeight, orientationMayChange);
        int[] trackFormatSupport = formatSupport[groupIndex];
        for (int trackIndex = 0; trackIndex < trackGroup.length; trackIndex++) {
            if (isSupported(trackFormatSupport[trackIndex], exceedRendererCapabilitiesIfNecessary)) {
                Format format = trackGroup.getFormat(trackIndex);
                boolean isWithinConstraints = selectedTrackIndices.contains(trackIndex) && (format.width == Format.NO_VALUE || format.width <= maxVideoWidth) && (format.height == Format.NO_VALUE || format.height <= maxVideoHeight) && (format.bitrate == Format.NO_VALUE || format.bitrate <= maxVideoBitrate);
                if (!isWithinConstraints && !exceedConstraintsIfNecessary) {
                    // Track should not be selected.
                    continue;
                }
                int trackScore = isWithinConstraints ? 2 : 1;
                if (isSupported(trackFormatSupport[trackIndex], false)) {
                    trackScore += WITHIN_RENDERER_CAPABILITIES_BONUS;
                }
                boolean selectTrack = trackScore > selectedTrackScore;
                if (trackScore == selectedTrackScore) {
                    // 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 comparisonResult;
                    int formatPixelCount = format.getPixelCount();
                    if (formatPixelCount != selectedPixelCount) {
                        comparisonResult = compareFormatValues(format.getPixelCount(), selectedPixelCount);
                    } else {
                        comparisonResult = compareFormatValues(format.bitrate, selectedBitrate);
                    }
                    selectTrack = 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);
}
Also used : Format(com.google.android.exoplayer2.Format) TrackGroup(com.google.android.exoplayer2.source.TrackGroup) Point(android.graphics.Point)

Example 78 with TrackGroupArray

use of com.google.android.exoplayer2.source.TrackGroupArray in project ExoPlayer by google.

the class MappingTrackSelector method maybeConfigureRenderersForTunneling.

/**
   * Determines whether tunneling should be enabled, replacing {@link RendererConfiguration}s in
   * {@code rendererConfigurations} with configurations that enable tunneling on the appropriate
   * renderers if so.
   *
   * @param rendererCapabilities The {@link RendererCapabilities} of the renderers for which
   *     {@link TrackSelection}s are to be generated.
   * @param rendererTrackGroupArrays An array of {@link TrackGroupArray}s where each entry
   *     corresponds to the renderer of equal index in {@code renderers}.
   * @param rendererFormatSupports Maps every available track to a specific level of support as
   *     defined by the renderer {@code FORMAT_*} constants.
   * @param rendererConfigurations The renderer configurations. Configurations may be replaced with
   *     ones that enable tunneling as a result of this call.
   * @param trackSelections The renderer track selections.
   * @param tunnelingAudioSessionId The audio session id to use when tunneling, or
   *     {@link C#AUDIO_SESSION_ID_UNSET} if tunneling should not be enabled.
   */
private static void maybeConfigureRenderersForTunneling(RendererCapabilities[] rendererCapabilities, TrackGroupArray[] rendererTrackGroupArrays, int[][][] rendererFormatSupports, RendererConfiguration[] rendererConfigurations, TrackSelection[] trackSelections, int tunnelingAudioSessionId) {
    if (tunnelingAudioSessionId == C.AUDIO_SESSION_ID_UNSET) {
        return;
    }
    // Check whether we can enable tunneling. To enable tunneling we require exactly one audio and
    // one video renderer to support tunneling and have a selection.
    int tunnelingAudioRendererIndex = -1;
    int tunnelingVideoRendererIndex = -1;
    boolean enableTunneling = true;
    for (int i = 0; i < rendererCapabilities.length; i++) {
        int rendererType = rendererCapabilities[i].getTrackType();
        TrackSelection trackSelection = trackSelections[i];
        if ((rendererType == C.TRACK_TYPE_AUDIO || rendererType == C.TRACK_TYPE_VIDEO) && trackSelection != null) {
            if (rendererSupportsTunneling(rendererFormatSupports[i], rendererTrackGroupArrays[i], trackSelection)) {
                if (rendererType == C.TRACK_TYPE_AUDIO) {
                    if (tunnelingAudioRendererIndex != -1) {
                        enableTunneling = false;
                        break;
                    } else {
                        tunnelingAudioRendererIndex = i;
                    }
                } else {
                    if (tunnelingVideoRendererIndex != -1) {
                        enableTunneling = false;
                        break;
                    } else {
                        tunnelingVideoRendererIndex = i;
                    }
                }
            }
        }
    }
    enableTunneling &= tunnelingAudioRendererIndex != -1 && tunnelingVideoRendererIndex != -1;
    if (enableTunneling) {
        RendererConfiguration tunnelingRendererConfiguration = new RendererConfiguration(tunnelingAudioSessionId);
        rendererConfigurations[tunnelingAudioRendererIndex] = tunnelingRendererConfiguration;
        rendererConfigurations[tunnelingVideoRendererIndex] = tunnelingRendererConfiguration;
    }
}
Also used : RendererConfiguration(com.google.android.exoplayer2.RendererConfiguration)

Example 79 with TrackGroupArray

use of com.google.android.exoplayer2.source.TrackGroupArray in project ExoPlayer by google.

the class MappingTrackSelector method selectTracks.

// TrackSelector implementation.
@Override
public final TrackSelectorResult selectTracks(RendererCapabilities[] rendererCapabilities, TrackGroupArray trackGroups) throws ExoPlaybackException {
    // Structures into which data will be written during the selection. The extra item at the end
    // of each array is to store data associated with track groups that cannot be associated with
    // any renderer.
    int[] rendererTrackGroupCounts = new int[rendererCapabilities.length + 1];
    TrackGroup[][] rendererTrackGroups = new TrackGroup[rendererCapabilities.length + 1][];
    int[][][] rendererFormatSupports = new int[rendererCapabilities.length + 1][][];
    for (int i = 0; i < rendererTrackGroups.length; i++) {
        rendererTrackGroups[i] = new TrackGroup[trackGroups.length];
        rendererFormatSupports[i] = new int[trackGroups.length][];
    }
    // Determine the extent to which each renderer supports mixed mimeType adaptation.
    int[] mixedMimeTypeAdaptationSupport = getMixedMimeTypeAdaptationSupport(rendererCapabilities);
    // renderer provides for each track in the group.
    for (int groupIndex = 0; groupIndex < trackGroups.length; groupIndex++) {
        TrackGroup group = trackGroups.get(groupIndex);
        // Associate the group to a preferred renderer.
        int rendererIndex = findRenderer(rendererCapabilities, group);
        // Evaluate the support that the renderer provides for each track in the group.
        int[] rendererFormatSupport = rendererIndex == rendererCapabilities.length ? new int[group.length] : getFormatSupport(rendererCapabilities[rendererIndex], group);
        // Stash the results.
        int rendererTrackGroupCount = rendererTrackGroupCounts[rendererIndex];
        rendererTrackGroups[rendererIndex][rendererTrackGroupCount] = group;
        rendererFormatSupports[rendererIndex][rendererTrackGroupCount] = rendererFormatSupport;
        rendererTrackGroupCounts[rendererIndex]++;
    }
    // Create a track group array for each renderer, and trim each rendererFormatSupports entry.
    TrackGroupArray[] rendererTrackGroupArrays = new TrackGroupArray[rendererCapabilities.length];
    int[] rendererTrackTypes = new int[rendererCapabilities.length];
    for (int i = 0; i < rendererCapabilities.length; i++) {
        int rendererTrackGroupCount = rendererTrackGroupCounts[i];
        rendererTrackGroupArrays[i] = new TrackGroupArray(Arrays.copyOf(rendererTrackGroups[i], rendererTrackGroupCount));
        rendererFormatSupports[i] = Arrays.copyOf(rendererFormatSupports[i], rendererTrackGroupCount);
        rendererTrackTypes[i] = rendererCapabilities[i].getTrackType();
    }
    // Create a track group array for track groups not associated with a renderer.
    int unassociatedTrackGroupCount = rendererTrackGroupCounts[rendererCapabilities.length];
    TrackGroupArray unassociatedTrackGroupArray = new TrackGroupArray(Arrays.copyOf(rendererTrackGroups[rendererCapabilities.length], unassociatedTrackGroupCount));
    TrackSelection[] trackSelections = selectTracks(rendererCapabilities, rendererTrackGroupArrays, rendererFormatSupports);
    // Apply track disabling and overriding.
    for (int i = 0; i < rendererCapabilities.length; i++) {
        if (rendererDisabledFlags.get(i)) {
            trackSelections[i] = null;
        } else {
            TrackGroupArray rendererTrackGroup = rendererTrackGroupArrays[i];
            Map<TrackGroupArray, SelectionOverride> overrides = selectionOverrides.get(i);
            SelectionOverride override = overrides == null ? null : overrides.get(rendererTrackGroup);
            if (override != null) {
                trackSelections[i] = override.createTrackSelection(rendererTrackGroup);
            }
        }
    }
    // Package up the track information and selections.
    MappedTrackInfo mappedTrackInfo = new MappedTrackInfo(rendererTrackTypes, rendererTrackGroupArrays, mixedMimeTypeAdaptationSupport, rendererFormatSupports, unassociatedTrackGroupArray);
    // Initialize the renderer configurations to the default configuration for all renderers with
    // selections, and null otherwise.
    RendererConfiguration[] rendererConfigurations = new RendererConfiguration[rendererCapabilities.length];
    for (int i = 0; i < rendererCapabilities.length; i++) {
        rendererConfigurations[i] = trackSelections[i] != null ? RendererConfiguration.DEFAULT : null;
    }
    // Configure audio and video renderers to use tunneling if appropriate.
    maybeConfigureRenderersForTunneling(rendererCapabilities, rendererTrackGroupArrays, rendererFormatSupports, rendererConfigurations, trackSelections, tunnelingAudioSessionId);
    return new TrackSelectorResult(trackGroups, new TrackSelectionArray(trackSelections), mappedTrackInfo, rendererConfigurations);
}
Also used : TrackGroupArray(com.google.android.exoplayer2.source.TrackGroupArray) TrackGroup(com.google.android.exoplayer2.source.TrackGroup) RendererConfiguration(com.google.android.exoplayer2.RendererConfiguration)

Example 80 with TrackGroupArray

use of com.google.android.exoplayer2.source.TrackGroupArray in project HybridMediaPlayer by mkaflowski.

the class CastPlayer method updateTracksAndSelections.

/**
 * Updates the internal tracks and selection and returns whether they have changed.
 */
private boolean updateTracksAndSelections() {
    if (remoteMediaClient == null) {
        // There is no session. We leave the state of the player as it is now.
        return false;
    }
    MediaStatus mediaStatus = getMediaStatus();
    MediaInfo mediaInfo = mediaStatus != null ? mediaStatus.getMediaInfo() : null;
    List<MediaTrack> castMediaTracks = mediaInfo != null ? mediaInfo.getMediaTracks() : null;
    if (castMediaTracks == null || castMediaTracks.isEmpty()) {
        boolean hasChanged = !currentTrackGroups.isEmpty();
        currentTrackGroups = TrackGroupArray.EMPTY;
        currentTrackSelection = EMPTY_TRACK_SELECTION_ARRAY;
        return hasChanged;
    }
    long[] activeTrackIds = mediaStatus.getActiveTrackIds();
    if (activeTrackIds == null) {
        activeTrackIds = EMPTY_TRACK_ID_ARRAY;
    }
    TrackGroup[] trackGroups = new TrackGroup[castMediaTracks.size()];
    TrackSelection[] trackSelections = new TrackSelection[RENDERER_COUNT];
    for (int i = 0; i < castMediaTracks.size(); i++) {
        MediaTrack mediaTrack = castMediaTracks.get(i);
        trackGroups[i] = new TrackGroup(CastUtils.mediaTrackToFormat(mediaTrack));
        long id = mediaTrack.getId();
        int trackType = MimeTypes.getTrackType(mediaTrack.getContentType());
        int rendererIndex = getRendererIndexForTrackType(trackType);
        if (isTrackActive(id, activeTrackIds) && rendererIndex != C.INDEX_UNSET && trackSelections[rendererIndex] == null) {
            trackSelections[rendererIndex] = new FixedTrackSelection(trackGroups[i], 0);
        }
    }
    TrackGroupArray newTrackGroups = new TrackGroupArray(trackGroups);
    TrackSelectionArray newTrackSelections = new TrackSelectionArray(trackSelections);
    if (!newTrackGroups.equals(currentTrackGroups) || !newTrackSelections.equals(currentTrackSelection)) {
        currentTrackSelection = new TrackSelectionArray(trackSelections);
        currentTrackGroups = new TrackGroupArray(trackGroups);
        return true;
    }
    return false;
}
Also used : TrackGroupArray(com.google.android.exoplayer2.source.TrackGroupArray) TrackSelectionArray(com.google.android.exoplayer2.trackselection.TrackSelectionArray) MediaTrack(com.google.android.gms.cast.MediaTrack) MediaInfo(com.google.android.gms.cast.MediaInfo) TrackGroup(com.google.android.exoplayer2.source.TrackGroup) TrackSelection(com.google.android.exoplayer2.trackselection.TrackSelection) FixedTrackSelection(com.google.android.exoplayer2.trackselection.FixedTrackSelection) FixedTrackSelection(com.google.android.exoplayer2.trackselection.FixedTrackSelection) MediaStatus(com.google.android.gms.cast.MediaStatus)

Aggregations

TrackGroupArray (com.google.android.exoplayer2.source.TrackGroupArray)126 Test (org.junit.Test)92 Format (com.google.android.exoplayer2.Format)67 TrackGroup (com.google.android.exoplayer2.source.TrackGroup)50 RendererCapabilities (com.google.android.exoplayer2.RendererCapabilities)36 Nullable (androidx.annotation.Nullable)18 FakeMediaSource (com.google.android.exoplayer2.testutil.FakeMediaSource)17 FakeTimeline (com.google.android.exoplayer2.testutil.FakeTimeline)17 TransferListener (com.google.android.exoplayer2.upstream.TransferListener)14 DrmSessionManager (com.google.android.exoplayer2.drm.DrmSessionManager)13 MediaSource (com.google.android.exoplayer2.source.MediaSource)13 MediaPeriodId (com.google.android.exoplayer2.source.MediaSource.MediaPeriodId)13 FakeMediaPeriod (com.google.android.exoplayer2.testutil.FakeMediaPeriod)13 TestExoPlayerBuilder (com.google.android.exoplayer2.testutil.TestExoPlayerBuilder)13 Allocator (com.google.android.exoplayer2.upstream.Allocator)12 ArrayList (java.util.ArrayList)11 ClippingMediaSource (com.google.android.exoplayer2.source.ClippingMediaSource)9 CompositeMediaSource (com.google.android.exoplayer2.source.CompositeMediaSource)9 ConcatenatingMediaSource (com.google.android.exoplayer2.source.ConcatenatingMediaSource)9 MaskingMediaSource (com.google.android.exoplayer2.source.MaskingMediaSource)9