MappingTrackSelector extends TrackSelector { @Override public final TrackSelectorResult selectTracks( RendererCapabilities[] rendererCapabilities, TrackGroupArray trackGroups, MediaPeriodId periodId, Timeline timeline) throws ExoPlaybackException { int[] rendererTrackGroupCounts = new int[rendererCapabilities.length + 1]; TrackGroup[][] rendererTrackGroups = new TrackGroup[rendererCapabilities.length + 1][]; @Capabilities 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][]; } @AdaptiveSupport int[] rendererMixedMimeTypeAdaptationSupports = getMixedMimeTypeAdaptationSupports(rendererCapabilities); for (int groupIndex = 0; groupIndex < trackGroups.length; groupIndex++) { TrackGroup group = trackGroups.get(groupIndex); boolean preferUnassociatedRenderer = MimeTypes.getTrackType(group.getFormat(0).sampleMimeType) == C.TRACK_TYPE_METADATA; int rendererIndex = findRenderer( rendererCapabilities, group, rendererTrackGroupCounts, preferUnassociatedRenderer); @Capabilities int[] rendererFormatSupport = rendererIndex == rendererCapabilities.length ? new int[group.length] : getFormatSupport(rendererCapabilities[rendererIndex], group); int rendererTrackGroupCount = rendererTrackGroupCounts[rendererIndex]; rendererTrackGroups[rendererIndex][rendererTrackGroupCount] = group; rendererFormatSupports[rendererIndex][rendererTrackGroupCount] = rendererFormatSupport; rendererTrackGroupCounts[rendererIndex]++; } 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( Util.nullSafeArrayCopy(rendererTrackGroups[i], rendererTrackGroupCount)); rendererFormatSupports[i] = Util.nullSafeArrayCopy(rendererFormatSupports[i], rendererTrackGroupCount); rendererTrackTypes[i] = rendererCapabilities[i].getTrackType(); } int unmappedTrackGroupCount = rendererTrackGroupCounts[rendererCapabilities.length]; TrackGroupArray unmappedTrackGroupArray = new TrackGroupArray( Util.nullSafeArrayCopy( rendererTrackGroups[rendererCapabilities.length], unmappedTrackGroupCount)); MappedTrackInfo mappedTrackInfo = new MappedTrackInfo( rendererTrackTypes, rendererTrackGroupArrays, rendererMixedMimeTypeAdaptationSupports, rendererFormatSupports, unmappedTrackGroupArray); Pair<@NullableType RendererConfiguration[], @NullableType TrackSelection[]> result = selectTracks( mappedTrackInfo, rendererFormatSupports, rendererMixedMimeTypeAdaptationSupports); return new TrackSelectorResult(result.first, result.second, mappedTrackInfo); }  final @Nullable MappedTrackInfo getCurrentMappedTrackInfo(); @Override final void onSelectionActivated(Object info); @Override final TrackSelectorResult selectTracks(
      RendererCapabilities[] rendererCapabilities,
      TrackGroupArray trackGroups,
      MediaPeriodId periodId,
      Timeline timeline);  }
@Test public void selectTracks_audioAndVideo_sameOrderAsRenderers_mappedToCorectRenderer() throws ExoPlaybackException { FakeMappingTrackSelector trackSelector = new FakeMappingTrackSelector(); RendererCapabilities[] rendererCapabilities = new RendererCapabilities[] {VIDEO_CAPABILITIES, AUDIO_CAPABILITIES}; TrackGroupArray trackGroups = new TrackGroupArray(VIDEO_TRACK_GROUP, AUDIO_TRACK_GROUP); trackSelector.selectTracks(rendererCapabilities, trackGroups, periodId, TIMELINE); trackSelector.assertMappedTrackGroups( 0, VIDEO_TRACK_GROUP); trackSelector.assertMappedTrackGroups( 1, AUDIO_TRACK_GROUP); }
@Test public void selectTracks_audioAndVideo_reverseOrderToRenderers_mappedToCorectRenderer() throws ExoPlaybackException { FakeMappingTrackSelector trackSelector = new FakeMappingTrackSelector(); TrackGroupArray trackGroups = new TrackGroupArray(VIDEO_TRACK_GROUP, AUDIO_TRACK_GROUP); RendererCapabilities[] reverseOrderRendererCapabilities = new RendererCapabilities[] {AUDIO_CAPABILITIES, VIDEO_CAPABILITIES}; trackSelector.selectTracks(reverseOrderRendererCapabilities, trackGroups, periodId, TIMELINE); trackSelector.assertMappedTrackGroups( 0, AUDIO_TRACK_GROUP); trackSelector.assertMappedTrackGroups( 1, VIDEO_TRACK_GROUP); }
@Test public void selectTracks_multipleVideoAndAudioTracks_mappedToSameRenderer() throws ExoPlaybackException { FakeMappingTrackSelector trackSelector = new FakeMappingTrackSelector(); TrackGroupArray trackGroups = new TrackGroupArray( VIDEO_TRACK_GROUP, AUDIO_TRACK_GROUP, AUDIO_TRACK_GROUP, VIDEO_TRACK_GROUP); RendererCapabilities[] rendererCapabilities = new RendererCapabilities[] { VIDEO_CAPABILITIES, AUDIO_CAPABILITIES, VIDEO_CAPABILITIES, AUDIO_CAPABILITIES }; trackSelector.selectTracks(rendererCapabilities, trackGroups, periodId, TIMELINE); trackSelector.assertMappedTrackGroups(0, VIDEO_TRACK_GROUP, VIDEO_TRACK_GROUP); trackSelector.assertMappedTrackGroups(1, AUDIO_TRACK_GROUP, AUDIO_TRACK_GROUP); }
@Test public void selectTracks_multipleMetadataTracks_mappedToDifferentRenderers() throws ExoPlaybackException { FakeMappingTrackSelector trackSelector = new FakeMappingTrackSelector(); TrackGroupArray trackGroups = new TrackGroupArray(VIDEO_TRACK_GROUP, METADATA_TRACK_GROUP, METADATA_TRACK_GROUP); RendererCapabilities[] rendererCapabilities = new RendererCapabilities[] { VIDEO_CAPABILITIES, METADATA_CAPABILITIES, METADATA_CAPABILITIES }; trackSelector.selectTracks(rendererCapabilities, trackGroups, periodId, TIMELINE); trackSelector.assertMappedTrackGroups(0, VIDEO_TRACK_GROUP); trackSelector.assertMappedTrackGroups(1, METADATA_TRACK_GROUP); trackSelector.assertMappedTrackGroups(2, METADATA_TRACK_GROUP); }