Search in sources :

Example 6 with TrackGroupArray

use of androidx.media3.common.TrackGroupArray in project media by androidx.

the class DefaultTrackSelector method selectOtherTrack.

// Generic track selection methods.
/**
 * Called by {@link #selectAllTracks(MappedTrackInfo, int[][][], int[], Parameters)} to create a
 * {@link ExoTrackSelection} 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 track group and
 *     track (in that order).
 * @param params The selector's current constraint parameters.
 * @return The {@link ExoTrackSelection} for the renderer, or null if no selection was made.
 * @throws ExoPlaybackException If an error occurs while selecting the tracks.
 */
@Nullable
protected ExoTrackSelection.Definition selectOtherTrack(int trackType, TrackGroupArray groups, @Capabilities int[][] formatSupport, Parameters params) throws ExoPlaybackException {
    @Nullable TrackGroup selectedGroup = null;
    int selectedTrackIndex = 0;
    @Nullable OtherTrackScore 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);
                OtherTrackScore trackScore = new OtherTrackScore(format, trackFormatSupport[trackIndex]);
                if (selectedTrackScore == null || trackScore.compareTo(selectedTrackScore) > 0) {
                    selectedGroup = trackGroup;
                    selectedTrackIndex = trackIndex;
                    selectedTrackScore = trackScore;
                }
            }
        }
    }
    return selectedGroup == null ? null : new ExoTrackSelection.Definition(selectedGroup, selectedTrackIndex);
}
Also used : Format(androidx.media3.common.Format) TrackGroup(androidx.media3.common.TrackGroup) RendererCapabilities(androidx.media3.exoplayer.RendererCapabilities) Capabilities(androidx.media3.exoplayer.RendererCapabilities.Capabilities) Nullable(androidx.annotation.Nullable) SuppressLint(android.annotation.SuppressLint) Point(android.graphics.Point) Nullable(androidx.annotation.Nullable)

Example 7 with TrackGroupArray

use of androidx.media3.common.TrackGroupArray in project media by androidx.

the class DefaultTrackSelector method getApplicableOverrides.

/**
 * Returns applicable overrides. Mapping from track type to a pair of override and renderer index
 * for this override.
 */
private SparseArray<Pair<TrackSelectionOverride, Integer>> getApplicableOverrides(MappedTrackInfo mappedTrackInfo, Parameters params) {
    SparseArray<Pair<TrackSelectionOverride, Integer>> applicableOverrides = new SparseArray<>();
    // Iterate through all existing track groups to ensure only overrides for those groups are used.
    int rendererCount = mappedTrackInfo.getRendererCount();
    for (int rendererIndex = 0; rendererIndex < rendererCount; rendererIndex++) {
        TrackGroupArray rendererTrackGroups = mappedTrackInfo.getTrackGroups(rendererIndex);
        for (int j = 0; j < rendererTrackGroups.length; j++) {
            maybeUpdateApplicableOverrides(applicableOverrides, params.trackSelectionOverrides.getOverride(rendererTrackGroups.get(j)), rendererIndex);
        }
    }
    // Also iterate unmapped groups to see if they have overrides.
    TrackGroupArray unmappedGroups = mappedTrackInfo.getUnmappedTrackGroups();
    for (int i = 0; i < unmappedGroups.length; i++) {
        maybeUpdateApplicableOverrides(applicableOverrides, params.trackSelectionOverrides.getOverride(unmappedGroups.get(i)), /* rendererIndex= */
        C.INDEX_UNSET);
    }
    return applicableOverrides;
}
Also used : SparseArray(android.util.SparseArray) TrackGroupArray(androidx.media3.common.TrackGroupArray) SuppressLint(android.annotation.SuppressLint) Point(android.graphics.Point) Pair(android.util.Pair)

Example 8 with TrackGroupArray

use of androidx.media3.common.TrackGroupArray in project media by androidx.

the class ExoPlayerTest method setPlaybackSpeedBeforePreparationCompletesSucceeds.

@Test
public void setPlaybackSpeedBeforePreparationCompletesSucceeds() throws Exception {
    // Test that no exception is thrown when playback parameters are updated between creating a
    // period and preparation of the period completing.
    final CountDownLatch createPeriodCalledCountDownLatch = new CountDownLatch(1);
    final FakeMediaPeriod[] fakeMediaPeriodHolder = new FakeMediaPeriod[1];
    MediaSource mediaSource = new FakeMediaSource(new FakeTimeline(), ExoPlayerTestRunner.VIDEO_FORMAT) {

        @Override
        protected MediaPeriod createMediaPeriod(MediaPeriodId id, TrackGroupArray trackGroupArray, Allocator allocator, MediaSourceEventListener.EventDispatcher mediaSourceEventDispatcher, DrmSessionManager drmSessionManager, DrmSessionEventListener.EventDispatcher drmEventDispatcher, @Nullable TransferListener transferListener) {
            // Defer completing preparation of the period until playback parameters have been set.
            fakeMediaPeriodHolder[0] = new FakeMediaPeriod(trackGroupArray, allocator, TimelineWindowDefinition.DEFAULT_WINDOW_OFFSET_IN_FIRST_PERIOD_US, mediaSourceEventDispatcher, drmSessionManager, drmEventDispatcher, /* deferOnPrepared= */
            true);
            createPeriodCalledCountDownLatch.countDown();
            return fakeMediaPeriodHolder[0];
        }
    };
    ActionSchedule actionSchedule = new ActionSchedule.Builder(TAG).waitForPlaybackState(Player.STATE_BUFFERING).executeRunnable(new PlayerRunnable() {

        @Override
        public void run(ExoPlayer player) {
            try {
                player.getClock().onThreadBlocked();
                createPeriodCalledCountDownLatch.await();
            } catch (InterruptedException e) {
                throw new IllegalStateException(e);
            }
        }
    }).setPlaybackParameters(new PlaybackParameters(/* speed= */
    2f)).executeRunnable(() -> fakeMediaPeriodHolder[0].setPreparationComplete()).build();
    new ExoPlayerTestRunner.Builder(context).setMediaSources(mediaSource).setActionSchedule(actionSchedule).build().start().blockUntilEnded(TIMEOUT_MS);
}
Also used : TransferListener(androidx.media3.datasource.TransferListener) Allocator(androidx.media3.exoplayer.upstream.Allocator) FakeMediaSource(androidx.media3.test.utils.FakeMediaSource) ActionSchedule(androidx.media3.test.utils.ActionSchedule) PlayerRunnable(androidx.media3.test.utils.ActionSchedule.PlayerRunnable) DrmSessionManager(androidx.media3.exoplayer.drm.DrmSessionManager) TrackGroupArray(androidx.media3.common.TrackGroupArray) CountDownLatch(java.util.concurrent.CountDownLatch) FakeMediaPeriod(androidx.media3.test.utils.FakeMediaPeriod) CompositeMediaSource(androidx.media3.exoplayer.source.CompositeMediaSource) ClippingMediaSource(androidx.media3.exoplayer.source.ClippingMediaSource) FakeMediaSource(androidx.media3.test.utils.FakeMediaSource) MaskingMediaSource(androidx.media3.exoplayer.source.MaskingMediaSource) ServerSideAdInsertionMediaSource(androidx.media3.exoplayer.source.ads.ServerSideAdInsertionMediaSource) FakeAdaptiveMediaSource(androidx.media3.test.utils.FakeAdaptiveMediaSource) ConcatenatingMediaSource(androidx.media3.exoplayer.source.ConcatenatingMediaSource) MediaSource(androidx.media3.exoplayer.source.MediaSource) FakeTimeline(androidx.media3.test.utils.FakeTimeline) MediaPeriodId(androidx.media3.exoplayer.source.MediaSource.MediaPeriodId) ExoPlayerTestRunner(androidx.media3.test.utils.ExoPlayerTestRunner) Nullable(androidx.annotation.Nullable) PlaybackParameters(androidx.media3.common.PlaybackParameters) Test(org.junit.Test)

Example 9 with TrackGroupArray

use of androidx.media3.common.TrackGroupArray in project media by androidx.

the class ExoPlayerTest method mediaPeriodMaybeThrowPrepareError_isNotThrownUntilPlaybackReachedFailingItem.

@Test
public void mediaPeriodMaybeThrowPrepareError_isNotThrownUntilPlaybackReachedFailingItem() throws Exception {
    ExoPlayer player = new TestExoPlayerBuilder(context).build();
    Timeline timeline = new FakeTimeline();
    player.addMediaSource(new FakeMediaSource(timeline, ExoPlayerTestRunner.VIDEO_FORMAT));
    player.addMediaSource(new FakeMediaSource(timeline, ExoPlayerTestRunner.VIDEO_FORMAT) {

        @Override
        protected MediaPeriod createMediaPeriod(MediaPeriodId id, TrackGroupArray trackGroupArray, Allocator allocator, MediaSourceEventListener.EventDispatcher mediaSourceEventDispatcher, DrmSessionManager drmSessionManager, DrmSessionEventListener.EventDispatcher drmEventDispatcher, @Nullable TransferListener transferListener) {
            return new FakeMediaPeriod(trackGroupArray, allocator, /* singleSampleTimeUs= */
            0, mediaSourceEventDispatcher, DrmSessionManager.DRM_UNSUPPORTED, drmEventDispatcher, /* deferOnPrepared= */
            true) {

                @Override
                public void maybeThrowPrepareError() throws IOException {
                    throw new IOException();
                }
            };
        }
    });
    player.prepare();
    player.play();
    ExoPlaybackException error = TestPlayerRunHelper.runUntilError(player);
    Object period1Uid = player.getCurrentTimeline().getPeriod(/* periodIndex= */
    1, new Timeline.Period(), /* setIds= */
    true).uid;
    assertThat(error.mediaPeriodId.periodUid).isEqualTo(period1Uid);
    assertThat(player.getCurrentMediaItemIndex()).isEqualTo(1);
}
Also used : TransferListener(androidx.media3.datasource.TransferListener) Allocator(androidx.media3.exoplayer.upstream.Allocator) FakeMediaSource(androidx.media3.test.utils.FakeMediaSource) DrmSessionManager(androidx.media3.exoplayer.drm.DrmSessionManager) TrackGroupArray(androidx.media3.common.TrackGroupArray) FakeMediaPeriod(androidx.media3.test.utils.FakeMediaPeriod) MediaPeriod(androidx.media3.exoplayer.source.MediaPeriod) IOException(java.io.IOException) DrmSessionEventListener(androidx.media3.exoplayer.drm.DrmSessionEventListener) FakeMediaPeriod(androidx.media3.test.utils.FakeMediaPeriod) Timeline(androidx.media3.common.Timeline) NoUidTimeline(androidx.media3.test.utils.NoUidTimeline) FakeTimeline(androidx.media3.test.utils.FakeTimeline) SinglePeriodTimeline(androidx.media3.exoplayer.source.SinglePeriodTimeline) MediaSourceEventListener(androidx.media3.exoplayer.source.MediaSourceEventListener) FakeTimeline(androidx.media3.test.utils.FakeTimeline) MediaPeriodId(androidx.media3.exoplayer.source.MediaSource.MediaPeriodId) FakeMediaPeriod(androidx.media3.test.utils.FakeMediaPeriod) MediaPeriod(androidx.media3.exoplayer.source.MediaPeriod) TestExoPlayerBuilder(androidx.media3.test.utils.TestExoPlayerBuilder) Test(org.junit.Test)

Example 10 with TrackGroupArray

use of androidx.media3.common.TrackGroupArray in project media by androidx.

the class ExoPlayerTest method playSinglePeriodTimeline.

/**
 * Tests playback of a source that exposes a single period.
 */
@Test
public void playSinglePeriodTimeline() throws Exception {
    Timeline timeline = new FakeTimeline();
    FakeRenderer renderer = new FakeRenderer(C.TRACK_TYPE_VIDEO);
    ExoPlayer player = new TestExoPlayerBuilder(context).setRenderers(renderer).build();
    Player.Listener mockListener = mock(Player.Listener.class);
    player.addListener(mockListener);
    player.setMediaSource(new FakeMediaSource(timeline, ExoPlayerTestRunner.VIDEO_FORMAT));
    player.prepare();
    player.play();
    runUntilPlaybackState(player, Player.STATE_ENDED);
    InOrder inOrder = Mockito.inOrder(mockListener);
    inOrder.verify(mockListener).onTimelineChanged(argThat(noUid(placeholderTimeline)), eq(Player.TIMELINE_CHANGE_REASON_PLAYLIST_CHANGED));
    inOrder.verify(mockListener).onTimelineChanged(argThat(noUid(timeline)), eq(Player.TIMELINE_CHANGE_REASON_SOURCE_UPDATE));
    inOrder.verify(mockListener).onTracksChanged(eq(new TrackGroupArray(new TrackGroup(ExoPlayerTestRunner.VIDEO_FORMAT))), any());
    inOrder.verify(mockListener, never()).onPositionDiscontinuity(anyInt());
    inOrder.verify(mockListener, never()).onPositionDiscontinuity(any(), any(), anyInt());
    assertThat(renderer.getFormatsRead()).containsExactly(ExoPlayerTestRunner.VIDEO_FORMAT);
    assertThat(renderer.sampleBufferReadCount).isEqualTo(1);
    assertThat(renderer.isEnded).isTrue();
}
Also used : FakeRenderer(androidx.media3.test.utils.FakeRenderer) Listener(androidx.media3.common.Player.Listener) Timeline(androidx.media3.common.Timeline) NoUidTimeline(androidx.media3.test.utils.NoUidTimeline) FakeTimeline(androidx.media3.test.utils.FakeTimeline) SinglePeriodTimeline(androidx.media3.exoplayer.source.SinglePeriodTimeline) Player(androidx.media3.common.Player) InOrder(org.mockito.InOrder) FakeMediaSource(androidx.media3.test.utils.FakeMediaSource) FakeTimeline(androidx.media3.test.utils.FakeTimeline) TrackGroup(androidx.media3.common.TrackGroup) TrackGroupArray(androidx.media3.common.TrackGroupArray) TestExoPlayerBuilder(androidx.media3.test.utils.TestExoPlayerBuilder) Test(org.junit.Test)

Aggregations

TrackGroupArray (androidx.media3.common.TrackGroupArray)117 Test (org.junit.Test)92 Format (androidx.media3.common.Format)60 TrackGroup (androidx.media3.common.TrackGroup)40 RendererCapabilities (androidx.media3.exoplayer.RendererCapabilities)36 Nullable (androidx.annotation.Nullable)18 FakeMediaSource (androidx.media3.test.utils.FakeMediaSource)17 FakeTimeline (androidx.media3.test.utils.FakeTimeline)17 TransferListener (androidx.media3.datasource.TransferListener)14 DrmSessionManager (androidx.media3.exoplayer.drm.DrmSessionManager)13 MediaPeriodId (androidx.media3.exoplayer.source.MediaSource.MediaPeriodId)13 FakeMediaPeriod (androidx.media3.test.utils.FakeMediaPeriod)13 TestExoPlayerBuilder (androidx.media3.test.utils.TestExoPlayerBuilder)13 Allocator (androidx.media3.exoplayer.upstream.Allocator)12 MediaSource (androidx.media3.exoplayer.source.MediaSource)10 MediaItem (androidx.media3.common.MediaItem)9 ClippingMediaSource (androidx.media3.exoplayer.source.ClippingMediaSource)9 CompositeMediaSource (androidx.media3.exoplayer.source.CompositeMediaSource)9 ConcatenatingMediaSource (androidx.media3.exoplayer.source.ConcatenatingMediaSource)9 MaskingMediaSource (androidx.media3.exoplayer.source.MaskingMediaSource)9