Skip to content

Move Player.Listener impl, remove @VisibleForTesting isInitialized. #6922

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Jun 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
// Copyright 2013 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

package io.flutter.plugins.videoplayer;

import androidx.annotation.NonNull;
import androidx.media3.common.PlaybackException;
import androidx.media3.common.Player;
import androidx.media3.common.VideoSize;
import androidx.media3.exoplayer.ExoPlayer;

final class ExoPlayerEventListener implements Player.Listener {
private final ExoPlayer exoPlayer;
private final VideoPlayerCallbacks events;
private boolean isBuffering = false;
private boolean isInitialized = false;

ExoPlayerEventListener(ExoPlayer exoPlayer, VideoPlayerCallbacks events) {
this.exoPlayer = exoPlayer;
this.events = events;
}

private void setBuffering(boolean buffering) {
if (isBuffering == buffering) {
return;
}
isBuffering = buffering;
if (buffering) {
events.onBufferingStart();
} else {
events.onBufferingEnd();
}
}

@SuppressWarnings("SuspiciousNameCombination")
private void sendInitialized() {
if (isInitialized) {
return;
}
isInitialized = true;
VideoSize videoSize = exoPlayer.getVideoSize();
int rotationCorrection = 0;
int width = videoSize.width;
int height = videoSize.height;
if (width != 0 && height != 0) {
int rotationDegrees = videoSize.unappliedRotationDegrees;
// Switch the width/height if video was taken in portrait mode
if (rotationDegrees == 90 || rotationDegrees == 270) {
width = videoSize.height;
height = videoSize.width;
}
// Rotating the video with ExoPlayer does not seem to be possible with a Surface,
// so inform the Flutter code that the widget needs to be rotated to prevent
// upside-down playback for videos with rotationDegrees of 180 (other orientations work
// correctly without correction).
if (rotationDegrees == 180) {
rotationCorrection = rotationDegrees;
}
}
events.onInitialized(width, height, exoPlayer.getDuration(), rotationCorrection);
}

@Override
public void onPlaybackStateChanged(final int playbackState) {
switch (playbackState) {
case Player.STATE_BUFFERING:
setBuffering(true);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a slight behavior change, right?

Before we would always invoke videoPlayerEvents.onBufferingUpdate(exoPlayer.getBufferedPosition()):

setBuffering(true);
sendBufferingUpdate();

but now we only invoke it if we pass the check at the start of the slightly modified setBuffering.

I don't know enough about video_player to know if that case (we are in the case of this switch Player.STATE_BUFFERING while isBuffering is already true) can happen/matters, so feel free to ignore if it isn't important.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not only was this a good catch, but you caught a bug missed in the previous refactor.

I've fixed this and added a test to catch regressions.

events.onBufferingUpdate(exoPlayer.getBufferedPosition());
break;
case Player.STATE_READY:
sendInitialized();
break;
case Player.STATE_ENDED:
events.onCompleted();
break;
case Player.STATE_IDLE:
break;
}
if (playbackState != Player.STATE_BUFFERING) {
setBuffering(false);
}
}

@Override
public void onPlayerError(@NonNull final PlaybackException error) {
setBuffering(false);
if (error.errorCode == PlaybackException.ERROR_CODE_BEHIND_LIVE_WINDOW) {
// See https://exoplayer.dev/live-streaming.html#behindlivewindowexception-and-error_code_behind_live_window
exoPlayer.seekToDefaultPosition();
exoPlayer.prepare();
} else {
events.onError("VideoError", "Video player had error " + error, null);
}
}

@Override
public void onIsPlayingChanged(boolean isPlaying) {
events.onIsPlayingStateUpdate(isPlaying);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,7 @@
import androidx.media3.common.C;
import androidx.media3.common.MediaItem;
import androidx.media3.common.MimeTypes;
import androidx.media3.common.PlaybackException;
import androidx.media3.common.PlaybackParameters;
import androidx.media3.common.Player;
import androidx.media3.common.Player.Listener;
import androidx.media3.common.VideoSize;
import androidx.media3.common.util.UnstableApi;
import androidx.media3.datasource.DataSource;
import androidx.media3.datasource.DefaultDataSource;
Expand All @@ -47,8 +43,6 @@ final class VideoPlayer {

private static final String USER_AGENT = "User-Agent";

@VisibleForTesting boolean isInitialized = false;

private final VideoPlayerOptions options;

private final DefaultHttpDataSource.Factory httpDataSourceFactory;
Expand Down Expand Up @@ -116,61 +110,7 @@ private void setUpVideoPlayer(ExoPlayer exoPlayer) {
surface = new Surface(textureEntry.surfaceTexture());
exoPlayer.setVideoSurface(surface);
setAudioAttributes(exoPlayer, options.mixWithOthers);

// Avoids synthetic accessor.
VideoPlayerCallbacks events = this.videoPlayerEvents;

exoPlayer.addListener(
new Listener() {
private boolean isBuffering = false;

public void setBuffering(boolean buffering) {
if (isBuffering == buffering) {
return;
}
isBuffering = buffering;
if (buffering) {
events.onBufferingStart();
} else {
events.onBufferingEnd();
}
}

@Override
public void onPlaybackStateChanged(final int playbackState) {
if (playbackState == Player.STATE_BUFFERING) {
setBuffering(true);
sendBufferingUpdate();
} else if (playbackState == Player.STATE_READY) {
if (!isInitialized) {
isInitialized = true;
sendInitialized();
}
} else if (playbackState == Player.STATE_ENDED) {
events.onCompleted();
}
if (playbackState != Player.STATE_BUFFERING) {
setBuffering(false);
}
}

@Override
public void onPlayerError(@NonNull final PlaybackException error) {
setBuffering(false);
if (error.errorCode == PlaybackException.ERROR_CODE_BEHIND_LIVE_WINDOW) {
// See https://exoplayer.dev/live-streaming.html#behindlivewindowexception-and-error_code_behind_live_window
exoPlayer.seekToDefaultPosition();
exoPlayer.prepare();
} else {
events.onError("VideoError", "Video player had error " + error, null);
}
}

@Override
public void onIsPlayingChanged(boolean isPlaying) {
events.onIsPlayingStateUpdate(isPlaying);
}
});
exoPlayer.addListener(new ExoPlayerEventListener(exoPlayer, videoPlayerEvents));
}

void sendBufferingUpdate() {
Expand Down Expand Up @@ -216,38 +156,7 @@ long getPosition() {
return exoPlayer.getCurrentPosition();
}

@SuppressWarnings("SuspiciousNameCombination")
@VisibleForTesting
void sendInitialized() {
if (!isInitialized) {
return;
}
VideoSize videoSize = exoPlayer.getVideoSize();
int rotationCorrection = 0;
int width = videoSize.width;
int height = videoSize.height;
if (width != 0 && height != 0) {
int rotationDegrees = videoSize.unappliedRotationDegrees;
// Switch the width/height if video was taken in portrait mode
if (rotationDegrees == 90 || rotationDegrees == 270) {
width = videoSize.height;
height = videoSize.width;
}
// Rotating the video with ExoPlayer does not seem to be possible with a Surface,
// so inform the Flutter code that the widget needs to be rotated to prevent
// upside-down playback for videos with rotationDegrees of 180 (other orientations work
// correctly without correction).
if (rotationDegrees == 180) {
rotationCorrection = rotationDegrees;
}
}
videoPlayerEvents.onInitialized(width, height, exoPlayer.getDuration(), rotationCorrection);
}

void dispose() {
if (isInitialized) {
exoPlayer.stop();
}
textureEntry.release();
if (surface != null) {
surface.release();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@
import androidx.annotation.Nullable;
import androidx.annotation.VisibleForTesting;
import io.flutter.plugin.common.EventChannel;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

final class VideoPlayerEventCallbacks implements VideoPlayerCallbacks {
Expand Down Expand Up @@ -66,7 +68,10 @@ public void onBufferingStart() {
public void onBufferingUpdate(long bufferedPosition) {
// iOS supports a list of buffered ranges, so we send as a list with a single range.
Map<String, Object> event = new HashMap<>();
event.put("values", Collections.singletonList(bufferedPosition));
event.put("event", "bufferingUpdate");

List<? extends Number> range = Arrays.asList(0, bufferedPosition);
event.put("values", Collections.singletonList(range));
eventSink.success(event);
}

Expand Down
Loading