Search in sources :

Example 11 with State

use of com.google.android.exoplayer2.ext.ima.ImaServerSideAdInsertionMediaSource.AdsLoader.State in project ExoPlayer by google.

the class ImaServerSideAdInsertionMediaSourceTest method adsLoaderStateToBundle_marshallAndUnmarshalling_resultIsEqual.

@Test
public void adsLoaderStateToBundle_marshallAndUnmarshalling_resultIsEqual() {
    AdPlaybackState firstAdPlaybackState = ServerSideAdInsertionUtil.addAdGroupToAdPlaybackState(new AdPlaybackState("adsId1"), /* fromPositionUs= */
    0, /* contentResumeOffsetUs= */
    10, /* adDurationsUs...= */
    5_000_000, 10_000_000, 20_000_000);
    AdPlaybackState secondAdPlaybackState = ServerSideAdInsertionUtil.addAdGroupToAdPlaybackState(new AdPlaybackState("adsId2"), /* fromPositionUs= */
    0, /* contentResumeOffsetUs= */
    10, /* adDurationsUs...= */
    10_000_000).withPlayedAd(/* adGroupIndex= */
    0, /* adIndexInAdGroup= */
    0);
    AdPlaybackState thirdAdPlaybackState = ServerSideAdInsertionUtil.addAdGroupToAdPlaybackState(new AdPlaybackState("adsId3"), /* fromPositionUs= */
    C.TIME_END_OF_SOURCE, /* contentResumeOffsetUs= */
    10, /* adDurationsUs...= */
    10_000_000);
    thirdAdPlaybackState = ServerSideAdInsertionUtil.addAdGroupToAdPlaybackState(thirdAdPlaybackState, /* fromPositionUs= */
    0, /* contentResumeOffsetUs= */
    10, /* adDurationsUs...= */
    10_000_000).withRemovedAdGroupCount(1);
    State state = new State(ImmutableMap.<String, AdPlaybackState>builder().put("adsId1", firstAdPlaybackState).put("adsId2", secondAdPlaybackState).put("adsId3", thirdAdPlaybackState).buildOrThrow());
    assertThat(State.CREATOR.fromBundle(state.toBundle())).isEqualTo(state);
}
Also used : AdPlaybackState(com.google.android.exoplayer2.source.ads.AdPlaybackState) AdPlaybackState(com.google.android.exoplayer2.source.ads.AdPlaybackState) State(com.google.android.exoplayer2.ext.ima.ImaServerSideAdInsertionMediaSource.AdsLoader.State) Test(org.junit.Test)

Example 12 with State

use of com.google.android.exoplayer2.ext.ima.ImaServerSideAdInsertionMediaSource.AdsLoader.State in project ExoPlayer by google.

the class MediaSessionConnector method invalidateMediaSessionPlaybackState.

/**
 * Updates the playback state of the media session.
 *
 * <p>Apps normally only need to call this method when the custom actions provided by a {@link
 * CustomActionProvider} changed and the playback state needs to be updated immediately.
 */
public final void invalidateMediaSessionPlaybackState() {
    PlaybackStateCompat.Builder builder = new PlaybackStateCompat.Builder();
    @Nullable Player player = this.player;
    if (player == null) {
        builder.setActions(buildPrepareActions()).setState(PlaybackStateCompat.STATE_NONE, /* position= */
        0, /* playbackSpeed= */
        0, /* updateTime= */
        SystemClock.elapsedRealtime());
        mediaSession.setRepeatMode(PlaybackStateCompat.REPEAT_MODE_NONE);
        mediaSession.setShuffleMode(PlaybackStateCompat.SHUFFLE_MODE_NONE);
        mediaSession.setPlaybackState(builder.build());
        return;
    }
    Map<String, CustomActionProvider> currentActions = new HashMap<>();
    for (CustomActionProvider customActionProvider : customActionProviders) {
        @Nullable PlaybackStateCompat.CustomAction customAction = customActionProvider.getCustomAction(player);
        if (customAction != null) {
            currentActions.put(customAction.getAction(), customActionProvider);
            builder.addCustomAction(customAction);
        }
    }
    customActionMap = Collections.unmodifiableMap(currentActions);
    Bundle extras = new Bundle();
    @Nullable PlaybackException playbackError = player.getPlayerError();
    boolean reportError = playbackError != null || customError != null;
    int sessionPlaybackState = reportError ? PlaybackStateCompat.STATE_ERROR : getMediaSessionPlaybackState(player.getPlaybackState(), player.getPlayWhenReady());
    if (customError != null) {
        builder.setErrorMessage(customError.first, customError.second);
        if (customErrorExtras != null) {
            extras.putAll(customErrorExtras);
        }
    } else if (playbackError != null && errorMessageProvider != null) {
        Pair<Integer, String> message = errorMessageProvider.getErrorMessage(playbackError);
        builder.setErrorMessage(message.first, message.second);
    }
    long activeQueueItemId = queueNavigator != null ? queueNavigator.getActiveQueueItemId(player) : MediaSessionCompat.QueueItem.UNKNOWN_ID;
    float playbackSpeed = player.getPlaybackParameters().speed;
    extras.putFloat(EXTRAS_SPEED, playbackSpeed);
    float sessionPlaybackSpeed = player.isPlaying() ? playbackSpeed : 0f;
    @Nullable MediaItem currentMediaItem = player.getCurrentMediaItem();
    if (currentMediaItem != null && !MediaItem.DEFAULT_MEDIA_ID.equals(currentMediaItem.mediaId)) {
        extras.putString(PLAYBACK_STATE_EXTRAS_KEY_MEDIA_ID, currentMediaItem.mediaId);
    }
    builder.setActions(buildPrepareActions() | buildPlaybackActions(player)).setActiveQueueItemId(activeQueueItemId).setBufferedPosition(player.getBufferedPosition()).setState(sessionPlaybackState, player.getCurrentPosition(), sessionPlaybackSpeed, /* updateTime= */
    SystemClock.elapsedRealtime()).setExtras(extras);
    @Player.RepeatMode int repeatMode = player.getRepeatMode();
    mediaSession.setRepeatMode(repeatMode == Player.REPEAT_MODE_ONE ? PlaybackStateCompat.REPEAT_MODE_ONE : repeatMode == Player.REPEAT_MODE_ALL ? PlaybackStateCompat.REPEAT_MODE_ALL : PlaybackStateCompat.REPEAT_MODE_NONE);
    mediaSession.setShuffleMode(player.getShuffleModeEnabled() ? PlaybackStateCompat.SHUFFLE_MODE_ALL : PlaybackStateCompat.SHUFFLE_MODE_NONE);
    mediaSession.setPlaybackState(builder.build());
}
Also used : PlaybackException(com.google.android.exoplayer2.PlaybackException) Player(com.google.android.exoplayer2.Player) HashMap(java.util.HashMap) Bundle(android.os.Bundle) PlaybackStateCompat(android.support.v4.media.session.PlaybackStateCompat) MediaItem(com.google.android.exoplayer2.MediaItem) Nullable(androidx.annotation.Nullable) Pair(android.util.Pair)

Example 13 with State

use of com.google.android.exoplayer2.ext.ima.ImaServerSideAdInsertionMediaSource.AdsLoader.State in project ExoPlayer by google.

the class ExoPlayerTest method recursivePlayerChangesAreReportedInCorrectOrder.

@Test
public void recursivePlayerChangesAreReportedInCorrectOrder() throws Exception {
    // The listener stops the player as soon as it's ready (which should report a timeline and state
    // change) and sets playWhenReady to false when the timeline callback is received.
    final AtomicReference<Player> playerReference = new AtomicReference<>();
    final List<Boolean> playerListenerPlayWhenReady = new ArrayList<>();
    final List<Integer> playerListenerStates = new ArrayList<>();
    List<Integer> sequence = new ArrayList<>();
    final Player.Listener playerListener = new Player.Listener() {

        @Override
        public void onPlaybackStateChanged(@Player.State int playbackState) {
            playerListenerStates.add(playbackState);
            if (playbackState == Player.STATE_READY) {
                playerReference.get().stop(/* reset= */
                true);
                sequence.add(0);
            }
        }

        @Override
        public void onTimelineChanged(Timeline timeline, int reason) {
            if (timeline.isEmpty()) {
                playerReference.get().pause();
                sequence.add(1);
            }
        }

        @Override
        public void onPlayWhenReadyChanged(boolean playWhenReady, @Player.PlayWhenReadyChangeReason int reason) {
            playerListenerPlayWhenReady.add(playWhenReady);
            sequence.add(2);
        }
    };
    ActionSchedule actionSchedule = new ActionSchedule.Builder(TAG).executeRunnable(new PlayerRunnable() {

        @Override
        public void run(ExoPlayer player) {
            playerReference.set(player);
            player.addListener(playerListener);
        }
    }).build();
    new ExoPlayerTestRunner.Builder(context).setActionSchedule(actionSchedule).build().start().blockUntilEnded(TIMEOUT_MS);
    assertThat(playerListenerStates).containsExactly(Player.STATE_BUFFERING, Player.STATE_READY, Player.STATE_IDLE).inOrder();
    assertThat(playerListenerPlayWhenReady).containsExactly(false).inOrder();
    assertThat(sequence).containsExactly(0, 1, 2).inOrder();
}
Also used : TransferListener(com.google.android.exoplayer2.upstream.TransferListener) Listener(com.google.android.exoplayer2.Player.Listener) MediaSourceEventListener(com.google.android.exoplayer2.source.MediaSourceEventListener) AnalyticsListener(com.google.android.exoplayer2.analytics.AnalyticsListener) DrmSessionEventListener(com.google.android.exoplayer2.drm.DrmSessionEventListener) ActionSchedule(com.google.android.exoplayer2.testutil.ActionSchedule) PlayerRunnable(com.google.android.exoplayer2.testutil.ActionSchedule.PlayerRunnable) TestExoPlayerBuilder(com.google.android.exoplayer2.testutil.TestExoPlayerBuilder) ArrayList(java.util.ArrayList) AtomicReference(java.util.concurrent.atomic.AtomicReference) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Listener(com.google.android.exoplayer2.Player.Listener) NoUidTimeline(com.google.android.exoplayer2.testutil.NoUidTimeline) SinglePeriodTimeline(com.google.android.exoplayer2.source.SinglePeriodTimeline) FakeTimeline(com.google.android.exoplayer2.testutil.FakeTimeline) ArgumentMatchers.anyBoolean(org.mockito.ArgumentMatchers.anyBoolean) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Test(org.junit.Test)

Example 14 with State

use of com.google.android.exoplayer2.ext.ima.ImaServerSideAdInsertionMediaSource.AdsLoader.State in project ExoPlayer by google.

the class MediaCodecVideoRenderer method setOutput.

private void setOutput(@Nullable Object output) throws ExoPlaybackException {
    // Handle unsupported (i.e., non-Surface) outputs by clearing the surface.
    @Nullable Surface surface = output instanceof Surface ? (Surface) output : null;
    if (surface == null) {
        // Use a dummy surface if possible.
        if (dummySurface != null) {
            surface = dummySurface;
        } else {
            MediaCodecInfo codecInfo = getCodecInfo();
            if (codecInfo != null && shouldUseDummySurface(codecInfo)) {
                dummySurface = DummySurface.newInstanceV17(context, codecInfo.secure);
                surface = dummySurface;
            }
        }
    }
    // We only need to update the codec if the surface has changed.
    if (this.surface != surface) {
        this.surface = surface;
        frameReleaseHelper.onSurfaceChanged(surface);
        haveReportedFirstFrameRenderedForCurrentSurface = false;
        @State int state = getState();
        @Nullable MediaCodecAdapter codec = getCodec();
        if (codec != null) {
            if (Util.SDK_INT >= 23 && surface != null && !codecNeedsSetOutputSurfaceWorkaround) {
                setOutputSurfaceV23(codec, surface);
            } else {
                releaseCodec();
                maybeInitCodecOrBypass();
            }
        }
        if (surface != null && surface != dummySurface) {
            // If we know the video size, report it again immediately.
            maybeRenotifyVideoSizeChanged();
            // We haven't rendered to the new surface yet.
            clearRenderedFirstFrame();
            if (state == STATE_STARTED) {
                setJoiningDeadlineMs();
            }
        } else {
            // The surface has been removed.
            clearReportedVideoSize();
            clearRenderedFirstFrame();
        }
    } else if (surface != null && surface != dummySurface) {
        // The surface is set and unchanged. If we know the video size and/or have already rendered to
        // the surface, report these again immediately.
        maybeRenotifyVideoSizeChanged();
        maybeRenotifyRenderedFirstFrame();
    }
}
Also used : MediaCodecAdapter(com.google.android.exoplayer2.mediacodec.MediaCodecAdapter) MediaCodecInfo(com.google.android.exoplayer2.mediacodec.MediaCodecInfo) Nullable(androidx.annotation.Nullable) SuppressLint(android.annotation.SuppressLint) Point(android.graphics.Point) Surface(android.view.Surface)

Example 15 with State

use of com.google.android.exoplayer2.ext.ima.ImaServerSideAdInsertionMediaSource.AdsLoader.State in project ExoPlayer by google.

the class ServerSideAdInsertionUtilTest method addAdGroupToAdPlaybackState_insertsCorrectAdGroupData.

@Test
public void addAdGroupToAdPlaybackState_insertsCorrectAdGroupData() {
    AdPlaybackState state = new AdPlaybackState(ADS_ID, /* adGroupTimesUs...= */
    0, 1, C.TIME_END_OF_SOURCE).withRemovedAdGroupCount(2);
    // stream: 0-- content --4300-- ad1 --4500-- content
    // content timeline: 0-4300 - [ad1] - 4700-end
    state = addAdGroupToAdPlaybackState(state, /* fromPositionUs= */
    4300, /* contentResumeOffsetUs= */
    400, /* adDurationsUs...= */
    200);
    assertThat(state).isEqualTo(new AdPlaybackState(ADS_ID, /* adGroupTimesUs...= */
    0, 0, 4300, C.TIME_END_OF_SOURCE).withRemovedAdGroupCount(2).withAdCount(/* adGroupIndex= */
    2, /* adCount= */
    1).withIsServerSideInserted(/* adGroupIndex= */
    2, /* isServerSideInserted= */
    true).withContentResumeOffsetUs(/* adGroupIndex= */
    2, /* contentResumeOffsetUs= */
    400).withAdDurationsUs(/* adGroupIndex= */
    2, /* adDurationsUs...= */
    200));
    // stream: 0-- content --2100-- ad1 --2400-- content --4300-- ad2 --4500-- content
    // content timeline: 0-2100 - [ad1] - 2100-4000 - [ad2] - 4400-end
    state = addAdGroupToAdPlaybackState(state, /* fromPositionUs= */
    2100, /* contentResumeOffsetUs= */
    0, /* adDurationsUs...= */
    300);
    assertThat(state).isEqualTo(new AdPlaybackState(ADS_ID, /* adGroupTimesUs...= */
    0, 0, 2100, 4000, C.TIME_END_OF_SOURCE).withRemovedAdGroupCount(2).withAdCount(/* adGroupIndex= */
    2, /* adCount= */
    1).withAdCount(/* adGroupIndex= */
    3, /* adCount= */
    1).withIsServerSideInserted(/* adGroupIndex= */
    2, /* isServerSideInserted= */
    true).withIsServerSideInserted(/* adGroupIndex= */
    3, /* isServerSideInserted= */
    true).withContentResumeOffsetUs(/* adGroupIndex= */
    3, /* contentResumeOffsetUs= */
    400).withAdDurationsUs(/* adGroupIndex= */
    2, /* adDurationsUs...= */
    300).withAdDurationsUs(/* adGroupIndex= */
    3, /* adDurationsUs...= */
    200));
    // stream: 0-- ad1 --100-- content --2100-- ad2 --2400-- content --4300-- ad3 --4500-- content
    // content timeline: 0 - [ad1] - 50-2050 -[ad2] - 2050-3950 - [ad3] - 4350-end
    state = addAdGroupToAdPlaybackState(state, /* fromPositionUs= */
    0, /* contentResumeOffsetUs= */
    50, /* adDurationsUs...= */
    100);
    assertThat(state).isEqualTo(new AdPlaybackState(ADS_ID, /* adGroupTimesUs...= */
    0, 0, 0, 2050, 3950, C.TIME_END_OF_SOURCE).withRemovedAdGroupCount(2).withAdCount(/* adGroupIndex= */
    2, /* adCount= */
    1).withAdCount(/* adGroupIndex= */
    3, /* adCount= */
    1).withAdCount(/* adGroupIndex= */
    4, /* adCount= */
    1).withIsServerSideInserted(/* adGroupIndex= */
    2, /* isServerSideInserted= */
    true).withIsServerSideInserted(/* adGroupIndex= */
    3, /* isServerSideInserted= */
    true).withIsServerSideInserted(/* adGroupIndex= */
    4, /* isServerSideInserted= */
    true).withContentResumeOffsetUs(/* adGroupIndex= */
    2, /* contentResumeOffsetUs= */
    50).withContentResumeOffsetUs(/* adGroupIndex= */
    4, /* contentResumeOffsetUs= */
    400).withAdDurationsUs(/* adGroupIndex= */
    2, /* adDurationsUs...= */
    100).withAdDurationsUs(/* adGroupIndex= */
    3, /* adDurationsUs...= */
    300).withAdDurationsUs(/* adGroupIndex= */
    4, /* adDurationsUs...= */
    200));
    // stream: 0-- ad1 --100-- c --2100-- ad2 --2400-- c --4300-- ad3 --4500-- c --5000-- ad4 --6000
    // content timeline: 0 - [ad1] - 50-2050 -[ad2] - 2050-3950 - [ad3] - 4350-4850 - [ad4] - 4850
    state = addAdGroupToAdPlaybackState(state, /* fromPositionUs= */
    5000, /* contentResumeOffsetUs= */
    0, /* adDurationsUs...= */
    1000);
    assertThat(state).isEqualTo(new AdPlaybackState(ADS_ID, /* adGroupTimesUs...= */
    0, 0, 0, 2050, 3950, 4850, C.TIME_END_OF_SOURCE).withRemovedAdGroupCount(2).withAdCount(/* adGroupIndex= */
    2, /* adCount= */
    1).withAdCount(/* adGroupIndex= */
    3, /* adCount= */
    1).withAdCount(/* adGroupIndex= */
    4, /* adCount= */
    1).withAdCount(/* adGroupIndex= */
    5, /* adCount= */
    1).withIsServerSideInserted(/* adGroupIndex= */
    2, /* isServerSideInserted= */
    true).withIsServerSideInserted(/* adGroupIndex= */
    3, /* isServerSideInserted= */
    true).withIsServerSideInserted(/* adGroupIndex= */
    4, /* isServerSideInserted= */
    true).withIsServerSideInserted(/* adGroupIndex= */
    5, /* isServerSideInserted= */
    true).withContentResumeOffsetUs(/* adGroupIndex= */
    2, /* contentResumeOffsetUs= */
    50).withContentResumeOffsetUs(/* adGroupIndex= */
    4, /* contentResumeOffsetUs= */
    400).withAdDurationsUs(/* adGroupIndex= */
    2, /* adDurationsUs...= */
    100).withAdDurationsUs(/* adGroupIndex= */
    3, /* adDurationsUs...= */
    300).withAdDurationsUs(/* adGroupIndex= */
    4, /* adDurationsUs...= */
    200).withAdDurationsUs(/* adGroupIndex= */
    5, /* adDurationsUs...= */
    1000));
}
Also used : ServerSideAdInsertionUtil.addAdGroupToAdPlaybackState(com.google.android.exoplayer2.source.ads.ServerSideAdInsertionUtil.addAdGroupToAdPlaybackState) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)26 TestExoPlayerBuilder (com.google.android.exoplayer2.testutil.TestExoPlayerBuilder)10 Nullable (androidx.annotation.Nullable)9 ServerSideAdInsertionUtil.addAdGroupToAdPlaybackState (com.google.android.exoplayer2.source.ads.ServerSideAdInsertionUtil.addAdGroupToAdPlaybackState)9 ActionSchedule (com.google.android.exoplayer2.testutil.ActionSchedule)9 SinglePeriodTimeline (com.google.android.exoplayer2.source.SinglePeriodTimeline)8 PlayerRunnable (com.google.android.exoplayer2.testutil.ActionSchedule.PlayerRunnable)8 SuppressLint (android.annotation.SuppressLint)7 ExoPlayerTestRunner (com.google.android.exoplayer2.testutil.ExoPlayerTestRunner)7 FakeTimeline (com.google.android.exoplayer2.testutil.FakeTimeline)7 NoUidTimeline (com.google.android.exoplayer2.testutil.NoUidTimeline)7 AdPlaybackState (com.google.android.exoplayer2.source.ads.AdPlaybackState)6 FakeMediaSource (com.google.android.exoplayer2.testutil.FakeMediaSource)6 ArrayList (java.util.ArrayList)6 AnalyticsListener (com.google.android.exoplayer2.analytics.AnalyticsListener)5 Listener (com.google.android.exoplayer2.Player.Listener)4 MediaSource (com.google.android.exoplayer2.source.MediaSource)4 TrackSelection (com.google.android.exoplayer2.trackselection.TrackSelection)4 IOException (java.io.IOException)4 Pair (android.util.Pair)3