Search in sources :

Example 1 with Callback

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

the class DownloadHelper method onMediaPrepared.

// Initialization of array of Lists.
@SuppressWarnings("unchecked")
private void onMediaPrepared() {
    checkNotNull(mediaPreparer);
    checkNotNull(mediaPreparer.mediaPeriods);
    checkNotNull(mediaPreparer.timeline);
    int periodCount = mediaPreparer.mediaPeriods.length;
    int rendererCount = rendererCapabilities.length;
    trackSelectionsByPeriodAndRenderer = (List<ExoTrackSelection>[][]) new List<?>[periodCount][rendererCount];
    immutableTrackSelectionsByPeriodAndRenderer = (List<ExoTrackSelection>[][]) new List<?>[periodCount][rendererCount];
    for (int i = 0; i < periodCount; i++) {
        for (int j = 0; j < rendererCount; j++) {
            trackSelectionsByPeriodAndRenderer[i][j] = new ArrayList<>();
            immutableTrackSelectionsByPeriodAndRenderer[i][j] = Collections.unmodifiableList(trackSelectionsByPeriodAndRenderer[i][j]);
        }
    }
    trackGroupArrays = new TrackGroupArray[periodCount];
    mappedTrackInfos = new MappedTrackInfo[periodCount];
    for (int i = 0; i < periodCount; i++) {
        trackGroupArrays[i] = mediaPreparer.mediaPeriods[i].getTrackGroups();
        TrackSelectorResult trackSelectorResult = runTrackSelection(/* periodIndex= */
        i);
        trackSelector.onSelectionActivated(trackSelectorResult.info);
        mappedTrackInfos[i] = checkNotNull(trackSelector.getCurrentMappedTrackInfo());
    }
    setPreparedWithMedia();
    checkNotNull(callbackHandler).post(() -> checkNotNull(callback).onPrepared(this));
}
Also used : TrackSelectorResult(com.google.android.exoplayer2.trackselection.TrackSelectorResult) ExoTrackSelection(com.google.android.exoplayer2.trackselection.ExoTrackSelection) List(java.util.List) ArrayList(java.util.ArrayList)

Example 2 with Callback

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

the class CastPlayerTest method setPlaybackParameters_speedChangeNotSupported_resetOnResultCallback.

@Test
public void setPlaybackParameters_speedChangeNotSupported_resetOnResultCallback() {
    when(mockRemoteMediaClient.setPlaybackRate(eq((double) 1.234f), any())).thenReturn(mockPendingResult);
    PlaybackParameters playbackParameters = new PlaybackParameters(/* speed= */
    1.234f);
    // Change the playback speed and and capture the result callback.
    castPlayer.setPlaybackParameters(playbackParameters);
    verify(mockPendingResult).setResultCallback(setResultCallbackArgumentCaptor.capture());
    verify(mockListener).onPlaybackParametersChanged(new PlaybackParameters(/* speed= */
    1.234f));
    // The device does not support speed changes and returns unit speed to the result callback.
    when(mockMediaStatus.getPlaybackRate()).thenReturn(1.0d);
    setResultCallbackArgumentCaptor.getValue().onResult(mock(RemoteMediaClient.MediaChannelResult.class));
    assertThat(castPlayer.getPlaybackParameters()).isEqualTo(PlaybackParameters.DEFAULT);
    verify(mockListener).onPlaybackParametersChanged(PlaybackParameters.DEFAULT);
    verifyNoMoreInteractions(mockListener);
}
Also used : PlaybackParameters(com.google.android.exoplayer2.PlaybackParameters) Test(org.junit.Test)

Example 3 with Callback

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

the class CastPlayerTest method setPlaybackParameters_masksPendingState.

@Test
public void setPlaybackParameters_masksPendingState() {
    PlaybackParameters playbackParameters = new PlaybackParameters(/* speed= */
    1.234f);
    when(mockRemoteMediaClient.setPlaybackRate(eq((double) 1.234f), any())).thenReturn(mockPendingResult);
    castPlayer.setPlaybackParameters(playbackParameters);
    verify(mockPendingResult).setResultCallback(setResultCallbackArgumentCaptor.capture());
    assertThat(castPlayer.getPlaybackParameters().speed).isEqualTo(1.234f);
    verify(mockListener).onPlaybackParametersChanged(playbackParameters);
    // Simulate a status update while the update is pending that must not override the masked speed.
    when(mockMediaStatus.getPlaybackRate()).thenReturn(99.0d);
    remoteMediaClientCallback.onStatusUpdated();
    assertThat(castPlayer.getPlaybackParameters().speed).isEqualTo(1.234f);
    // Call the captured result callback when the device responds. The listener must not be called.
    when(mockMediaStatus.getPlaybackRate()).thenReturn(1.234d);
    setResultCallbackArgumentCaptor.getValue().onResult(mock(RemoteMediaClient.MediaChannelResult.class));
    assertThat(castPlayer.getPlaybackParameters().speed).isEqualTo(1.234f);
    verifyNoMoreInteractions(mockListener);
}
Also used : PlaybackParameters(com.google.android.exoplayer2.PlaybackParameters) Test(org.junit.Test)

Example 4 with Callback

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

the class SessionCallbackBuilderTest method constructor.

@Test
public void constructor() throws Exception {
    try (MediaSession session = createMediaSession(sessionPlayerConnector, new SessionCallbackBuilder(context, sessionPlayerConnector).build())) {
        assertPlayerResultSuccess(sessionPlayerConnector.setMediaItem(TestUtils.createMediaItem()));
        assertPlayerResultSuccess(sessionPlayerConnector.prepare());
        OnConnectedListener listener = (controller, allowedCommands) -> {
            List<Integer> disallowedCommandCodes = Arrays.asList(// no rating callback
            SessionCommand.COMMAND_CODE_SESSION_SET_RATING, // no media item provider
            SessionCommand.COMMAND_CODE_PLAYER_ADD_PLAYLIST_ITEM, SessionCommand.COMMAND_CODE_PLAYER_REPLACE_PLAYLIST_ITEM, // no media item provider
            SessionCommand.COMMAND_CODE_PLAYER_SET_MEDIA_ITEM, // no media item provider
            SessionCommand.COMMAND_CODE_PLAYER_SET_PLAYLIST, // no current media item
            SessionCommand.COMMAND_CODE_SESSION_REWIND, // no current media item
            SessionCommand.COMMAND_CODE_SESSION_FAST_FORWARD);
            assertDisallowedCommands(disallowedCommandCodes, allowedCommands);
        };
        try (MediaController controller = createConnectedController(session, listener, null)) {
            assertThat(controller.getPlayerState()).isEqualTo(SessionPlayer.PLAYER_STATE_PAUSED);
        }
    }
}
Also used : Context(android.content.Context) HeartRating(androidx.media2.session.HeartRating) Arrays(java.util.Arrays) Bundle(android.os.Bundle) Uri(android.net.Uri) MediaSession(androidx.media2.session.MediaSession) RunWith(org.junit.runner.RunWith) SessionResult(androidx.media2.session.SessionResult) AndroidJUnit4(androidx.test.ext.junit.runners.AndroidJUnit4) TestUtils.assertPlayerResultSuccess(com.google.android.exoplayer2.ext.media2.TestUtils.assertPlayerResultSuccess) ApplicationProvider(androidx.test.core.app.ApplicationProvider) ArrayList(java.util.ArrayList) Future(java.util.concurrent.Future) SessionCommand(androidx.media2.session.SessionCommand) ContextCompat(androidx.core.content.ContextCompat) LargeTest(androidx.test.filters.LargeTest) Before(org.junit.Before) SessionPlayer(androidx.media2.common.SessionPlayer) MediaMetadata(androidx.media2.common.MediaMetadata) Truth.assertWithMessage(com.google.common.truth.Truth.assertWithMessage) Executor(java.util.concurrent.Executor) TextUtils(android.text.TextUtils) UriMediaItem(androidx.media2.common.UriMediaItem) Test(org.junit.Test) MILLISECONDS(java.util.concurrent.TimeUnit.MILLISECONDS) Truth.assertThat(com.google.common.truth.Truth.assertThat) MediaItem(androidx.media2.common.MediaItem) Rating(androidx.media2.common.Rating) R(com.google.android.exoplayer2.ext.media2.test.R) CountDownLatch(java.util.concurrent.CountDownLatch) SessionCommandGroup(androidx.media2.session.SessionCommandGroup) List(java.util.List) Nullable(androidx.annotation.Nullable) Rule(org.junit.Rule) MediaController(androidx.media2.session.MediaController) RawResourceDataSource(com.google.android.exoplayer2.upstream.RawResourceDataSource) MediaController(androidx.media2.session.MediaController) MediaSession(androidx.media2.session.MediaSession) ArrayList(java.util.ArrayList) List(java.util.List) LargeTest(androidx.test.filters.LargeTest) Test(org.junit.Test)

Example 5 with Callback

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

the class ProgressiveMediaPeriod method maybeFinishPrepare.

private void maybeFinishPrepare() {
    if (released || prepared || !sampleQueuesBuilt || seekMap == null) {
        return;
    }
    for (SampleQueue sampleQueue : sampleQueues) {
        if (sampleQueue.getUpstreamFormat() == null) {
            return;
        }
    }
    loadCondition.close();
    int trackCount = sampleQueues.length;
    TrackGroup[] trackArray = new TrackGroup[trackCount];
    boolean[] trackIsAudioVideoFlags = new boolean[trackCount];
    for (int i = 0; i < trackCount; i++) {
        Format trackFormat = Assertions.checkNotNull(sampleQueues[i].getUpstreamFormat());
        @Nullable String mimeType = trackFormat.sampleMimeType;
        boolean isAudio = MimeTypes.isAudio(mimeType);
        boolean isAudioVideo = isAudio || MimeTypes.isVideo(mimeType);
        trackIsAudioVideoFlags[i] = isAudioVideo;
        haveAudioVideoTracks |= isAudioVideo;
        @Nullable IcyHeaders icyHeaders = this.icyHeaders;
        if (icyHeaders != null) {
            if (isAudio || sampleQueueTrackIds[i].isIcyTrack) {
                @Nullable Metadata metadata = trackFormat.metadata;
                if (metadata == null) {
                    metadata = new Metadata(icyHeaders);
                } else {
                    metadata = metadata.copyWithAppendedEntries(icyHeaders);
                }
                trackFormat = trackFormat.buildUpon().setMetadata(metadata).build();
            }
            // an average or peak bitrate of its own.
            if (isAudio && trackFormat.averageBitrate == Format.NO_VALUE && trackFormat.peakBitrate == Format.NO_VALUE && icyHeaders.bitrate != Format.NO_VALUE) {
                trackFormat = trackFormat.buildUpon().setAverageBitrate(icyHeaders.bitrate).build();
            }
        }
        trackFormat = trackFormat.copyWithCryptoType(drmSessionManager.getCryptoType(trackFormat));
        trackArray[i] = new TrackGroup(/* id= */
        Integer.toString(i), trackFormat);
    }
    trackState = new TrackState(new TrackGroupArray(trackArray), trackIsAudioVideoFlags);
    prepared = true;
    Assertions.checkNotNull(callback).onPrepared(this);
}
Also used : Metadata(com.google.android.exoplayer2.metadata.Metadata) IcyHeaders(com.google.android.exoplayer2.metadata.icy.IcyHeaders) Format(com.google.android.exoplayer2.Format) Nullable(androidx.annotation.Nullable)

Aggregations

Test (org.junit.Test)23 ArrayList (java.util.ArrayList)10 HandlerThread (android.os.HandlerThread)9 Nullable (androidx.annotation.Nullable)9 FakeTimeline (com.google.android.exoplayer2.testutil.FakeTimeline)9 HandlerWrapper (com.google.android.exoplayer2.util.HandlerWrapper)9 FakeMediaSource (com.google.android.exoplayer2.testutil.FakeMediaSource)8 PlaybackParameters (com.google.android.exoplayer2.PlaybackParameters)7 Player (com.google.android.exoplayer2.Player)6 TestExoPlayerBuilder (com.google.android.exoplayer2.testutil.TestExoPlayerBuilder)6 ApplicationProvider (androidx.test.core.app.ApplicationProvider)5 AndroidJUnit4 (androidx.test.ext.junit.runners.AndroidJUnit4)5 ExoPlayer (com.google.android.exoplayer2.ExoPlayer)5 MediaSource (com.google.android.exoplayer2.source.MediaSource)5 Truth.assertThat (com.google.common.truth.Truth.assertThat)5 AtomicReference (java.util.concurrent.atomic.AtomicReference)5 RunWith (org.junit.runner.RunWith)5 LoadEventInfo (com.google.android.exoplayer2.source.LoadEventInfo)4 ImmutableList (com.google.common.collect.ImmutableList)4 SurfaceTexture (android.graphics.SurfaceTexture)3