public class

FakeTrackSelector

extends DefaultTrackSelector

Gradle dependencies

compile group: 'androidx.media3', name: 'media3-test-utils', version: '1.5.0-alpha01'

  • groupId: androidx.media3
  • artifactId: media3-test-utils
  • version: 1.5.0-alpha01

Artifact androidx.media3:media3-test-utils:1.5.0-alpha01 it located at Google repository (https://maven.google.com/)

Overview

A fake MappingTrackSelector that returns FakeTrackSelections.

Summary

Fields
from DefaultTrackSelectorcontext, SELECTION_ELIGIBILITY_ADAPTIVE, SELECTION_ELIGIBILITY_FIXED, SELECTION_ELIGIBILITY_NO
Constructors
publicFakeTrackSelector()

publicFakeTrackSelector(boolean mayReuseTrackSelection)

Methods
public java.util.List<FakeTrackSelection>getAllTrackSelections()

Returns list of all FakeTrackSelections that this track selector has made so far.

protected ExoTrackSelection.DefinitionselectAllTracks(MappingTrackSelector.MappedTrackInfo mappedTrackInfo, int[][][] rendererFormatSupports[][][], int[] rendererMixedMimeTypeAdaptationSupports[], DefaultTrackSelector.Parameters params)

Called from DefaultTrackSelector to make a track selection for each renderer, prior to overrides and disabled flags being applied.

from DefaultTrackSelectorbuildUponParameters, getFormatLanguageScore, getParameters, getRendererCapabilitiesListener, isSetParametersSupported, isSupported, normalizeUndeterminedLanguageToNull, onRendererCapabilitiesChanged, release, selectAudioTrack, selectImageTrack, selectOtherTrack, selectTextTrack, selectTracks, selectVideoTrack, setAudioAttributes, setParameters, setParameters, setParameters
from MappingTrackSelectorgetCurrentMappedTrackInfo, onSelectionActivated, selectTracks
from TrackSelectorgetBandwidthMeter, init, invalidate, invalidateForRendererCapabilitiesChange
from java.lang.Objectclone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait

Constructors

public FakeTrackSelector()

public FakeTrackSelector(boolean mayReuseTrackSelection)

Parameters:

mayReuseTrackSelection: Whether this FakeTrackSelector will reuse ExoTrackSelections during track selection, when it finds previously-selected track selection using the same TrackGroup.

Methods

protected ExoTrackSelection.Definition selectAllTracks(MappingTrackSelector.MappedTrackInfo mappedTrackInfo, int[][][] rendererFormatSupports[][][], int[] rendererMixedMimeTypeAdaptationSupports[], DefaultTrackSelector.Parameters params)

Called from DefaultTrackSelector to make a track selection for each renderer, prior to overrides and disabled flags being applied.

The implementation should not account for overrides and disabled flags. Track selections generated by this method will be overridden to account for these properties.

If selection parameters include TrackSelectionParameters.audioOffloadPreferences with TrackSelectionParameters.AudioOffloadPreferences.audioOffloadMode set to TrackSelectionParameters.AudioOffloadPreferences.AUDIO_OFFLOAD_MODE_REQUIRED then only audio tracks will be selected. If no audio track is supported in offload, then no track will be selected.

Parameters:

mappedTrackInfo: Mapped track information.
rendererFormatSupports: The RendererCapabilities.Capabilities for each mapped track, indexed by renderer, track group and track (in that order).
rendererMixedMimeTypeAdaptationSupports: The RendererCapabilities.AdaptiveSupport for mixed MIME type adaptation for the renderer.
params: The parameters to use for the track selection.

Returns:

The ExoTrackSelection.Definitions for the renderers. A null entry indicates no selection was made.

public java.util.List<FakeTrackSelection> getAllTrackSelections()

Returns list of all FakeTrackSelections that this track selector has made so far.

Source

/*
 * Copyright (C) 2017 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package androidx.media3.test.utils;

import androidx.media3.common.Timeline;
import androidx.media3.common.TrackGroup;
import androidx.media3.common.util.NullableType;
import androidx.media3.common.util.UnstableApi;
import androidx.media3.exoplayer.RendererCapabilities.AdaptiveSupport;
import androidx.media3.exoplayer.RendererCapabilities.Capabilities;
import androidx.media3.exoplayer.source.MediaSource.MediaPeriodId;
import androidx.media3.exoplayer.source.TrackGroupArray;
import androidx.media3.exoplayer.trackselection.DefaultTrackSelector;
import androidx.media3.exoplayer.trackselection.ExoTrackSelection;
import androidx.media3.exoplayer.trackselection.MappingTrackSelector;
import androidx.media3.exoplayer.upstream.BandwidthMeter;
import androidx.test.core.app.ApplicationProvider;
import java.util.ArrayList;
import java.util.List;

/** A fake {@link MappingTrackSelector} that returns {@link FakeTrackSelection}s. */
@UnstableApi
public class FakeTrackSelector extends DefaultTrackSelector {

  private final FakeTrackSelectionFactory fakeTrackSelectionFactory;

  public FakeTrackSelector() {
    this(/* mayReuseTrackSelection= */ false);
  }

  /**
   * @param mayReuseTrackSelection Whether this {@link FakeTrackSelector} will reuse {@link
   *     ExoTrackSelection}s during track selection, when it finds previously-selected track
   *     selection using the same {@link TrackGroup}.
   */
  public FakeTrackSelector(boolean mayReuseTrackSelection) {
    this(new FakeTrackSelectionFactory(mayReuseTrackSelection));
  }

  private FakeTrackSelector(FakeTrackSelectionFactory fakeTrackSelectionFactory) {
    super(ApplicationProvider.getApplicationContext(), fakeTrackSelectionFactory);
    this.fakeTrackSelectionFactory = fakeTrackSelectionFactory;
  }

  @Override
  protected ExoTrackSelection.@NullableType Definition[] selectAllTracks(
      MappedTrackInfo mappedTrackInfo,
      @Capabilities int[][][] rendererFormatSupports,
      @AdaptiveSupport int[] rendererMixedMimeTypeAdaptationSupports,
      Parameters params) {
    int rendererCount = mappedTrackInfo.getRendererCount();
    ExoTrackSelection.@NullableType Definition[] definitions =
        new ExoTrackSelection.Definition[rendererCount];
    for (int i = 0; i < rendererCount; i++) {
      TrackGroupArray trackGroupArray = mappedTrackInfo.getTrackGroups(i);
      boolean hasTracks = trackGroupArray.length > 0;
      definitions[i] =
          hasTracks
              ? new ExoTrackSelection.Definition(trackGroupArray.get(0), /* tracks...= */ 0)
              : null;
    }
    return definitions;
  }

  /** Returns list of all {@link FakeTrackSelection}s that this track selector has made so far. */
  public List<FakeTrackSelection> getAllTrackSelections() {
    return fakeTrackSelectionFactory.trackSelections;
  }

  private static class FakeTrackSelectionFactory implements ExoTrackSelection.Factory {

    private final List<FakeTrackSelection> trackSelections;
    private final boolean mayReuseTrackSelection;

    public FakeTrackSelectionFactory(boolean mayReuseTrackSelection) {
      this.mayReuseTrackSelection = mayReuseTrackSelection;
      trackSelections = new ArrayList<>();
    }

    @Override
    public ExoTrackSelection[] createTrackSelections(
        ExoTrackSelection.@NullableType Definition[] definitions,
        BandwidthMeter bandwidthMeter,
        MediaPeriodId mediaPeriodId,
        Timeline timeline) {
      ExoTrackSelection[] selections = new ExoTrackSelection[definitions.length];
      for (int i = 0; i < definitions.length; i++) {
        ExoTrackSelection.Definition definition = definitions[i];
        if (definition != null) {
          selections[i] = createTrackSelection(definition.group);
        }
      }
      return selections;
    }

    private ExoTrackSelection createTrackSelection(TrackGroup trackGroup) {
      if (mayReuseTrackSelection) {
        for (FakeTrackSelection trackSelection : trackSelections) {
          if (trackSelection.getTrackGroup().equals(trackGroup)) {
            return trackSelection;
          }
        }
      }
      FakeTrackSelection trackSelection = new FakeTrackSelection(trackGroup);
      trackSelections.add(trackSelection);
      return trackSelection;
    }
  }
}