Skip to content

Commit

Permalink
Clea up Android native code
Browse files Browse the repository at this point in the history
  • Loading branch information
FirentisTFW committed Dec 17, 2024
1 parent 8b203fc commit f3590a8
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
package io.flutter.plugins.videoplayer;

import android.os.Build;

import androidx.annotation.NonNull;
import androidx.annotation.OptIn;
import androidx.media3.common.Format;
Expand All @@ -14,15 +13,14 @@
import androidx.media3.common.VideoSize;
import androidx.media3.common.util.UnstableApi;
import androidx.media3.exoplayer.ExoPlayer;

import java.util.Objects;

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

private enum RotationDegrees {
ROTATE_0(0),
Expand Down Expand Up @@ -52,18 +50,18 @@ public int getDegrees() {

ExoPlayerEventListener(
ExoPlayer exoPlayer, VideoPlayerCallbacks events, Messages.PlatformVideoViewType viewType) {
this(exoPlayer, events, false, viewType);
this(exoPlayer, events, viewType, false);
}

ExoPlayerEventListener(
ExoPlayer exoPlayer,
VideoPlayerCallbacks events,
boolean initialized,
Messages.PlatformVideoViewType viewType) {
Messages.PlatformVideoViewType viewType,
boolean initialized) {
this.exoPlayer = exoPlayer;
this.events = events;
this.isInitialized = initialized;
this.viewType = viewType;
this.isInitialized = initialized;
}

private void setBuffering(boolean buffering) {
Expand All @@ -85,18 +83,20 @@ private void sendInitialized() {
}
isInitialized = true;

// FIXME Comment why this is needed
if (viewType == Messages.PlatformVideoViewType.PLATFORM_VIEW) {
sendInitializedPlatformViewApproach();
sendInitializedForPlatformViewApproach();
} else {
sendInitializedTextureApproach();
sendInitializedForTextureApproach();
}
}

@OptIn(markerClass = UnstableApi.class)
private void sendInitializedPlatformViewApproach() {
private void sendInitializedForPlatformViewApproach() {
// We can't rely on VideoSize here, because at this point it is not available - the platform
// view was not created yet. We use the video format instead.
Format videoFormat = exoPlayer.getVideoFormat();
RotationDegrees rotationCorrection = RotationDegrees.fromDegrees(videoFormat.rotationDegrees);
RotationDegrees rotationCorrection =
RotationDegrees.fromDegrees(Objects.requireNonNull(videoFormat).rotationDegrees);
int width = videoFormat.width;
int height = videoFormat.height;

Expand All @@ -111,12 +111,11 @@ private void sendInitializedPlatformViewApproach() {
events.onInitialized(width, height, exoPlayer.getDuration(), rotationCorrection.degrees);
}

private void sendInitializedTextureApproach() {
private void sendInitializedForTextureApproach() {
VideoSize videoSize = exoPlayer.getVideoSize();
int rotationCorrection = 0;
int width = videoSize.width;
int height = videoSize.height;

if (width != 0 && height != 0) {
RotationDegrees reportedRotationCorrection = RotationDegrees.ROTATE_0;

Expand Down Expand Up @@ -217,8 +216,7 @@ public void onPlaybackStateChanged(final int playbackState) {
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
// See https://exoplayer.dev/live-streaming.html#behindlivewindowexception-and-error_code_behind_live_window
exoPlayer.seekToDefaultPosition();
exoPlayer.prepare();
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ class NativeView implements PlatformView {

NativeView(@NonNull Context context, @NonNull ExoPlayer exoPlayer) {
surfaceView = new SurfaceView(context);
// The line below is needed to display the video correctly on older Android versions.
// The line below is needed to display the video correctly on older Android versions (blank
// space instead of a video).
surfaceView.setZOrderMediaOverlay(true);
exoPlayer.setVideoSurfaceView(surfaceView);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.media3.exoplayer.ExoPlayer;
import io.flutter.plugin.common.StandardMessageCodec;
import io.flutter.plugin.platform.PlatformView;
import io.flutter.plugin.platform.PlatformViewFactory;
import java.util.Objects;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ protected ExoPlayer createVideoPlayer() {

exoPlayer.addListener(
new ExoPlayerEventListener(
exoPlayer, videoPlayerEvents, wasPlayerInitialized(), getViewType()));
exoPlayer, videoPlayerEvents, getViewType(), wasPlayerInitialized()));
setAudioAttributes(exoPlayer, options.mixWithOthers);

return exoPlayer;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,11 @@

/** Android platform implementation of the VideoPlayerPlugin. */
public class VideoPlayerPlugin implements FlutterPlugin, AndroidVideoPlayerApi {
/// The next non-texture player ID, initialized to a high number to avoid collisions with
/// texture IDs (which are generated separately).
static Long nextNonTexturePlayerId = 1000000L;
/**
* The next non-texture player ID, initialized to a high number to avoid collisions with texture
* IDs (which are generated separately).
*/
private static Long nextNonTexturePlayerId = 1000000L;

private static final String TAG = "VideoPlayerPlugin";
private final LongSparseArray<VideoPlayer> videoPlayers = new LongSparseArray<>();
Expand Down Expand Up @@ -115,25 +117,19 @@ public void initialize() {
VideoPlayer videoPlayer;
if (arg.getViewType() == Messages.PlatformVideoViewType.PLATFORM_VIEW) {
id = VideoPlayerPlugin.nextNonTexturePlayerId++;
EventChannel eventChannel =
new EventChannel(flutterState.binaryMessenger, "flutter.io/videoPlayer/videoEvents" + id);

videoPlayer =
VideoPlayer.create(
flutterState.applicationContext,
VideoPlayerEventCallbacks.bindTo(eventChannel),
VideoPlayerEventCallbacks.bindTo(createEventChannel(id)),
videoAsset,
options);
} else {
TextureRegistry.SurfaceProducer handle = flutterState.textureRegistry.createSurfaceProducer();
id = handle.id();
EventChannel eventChannel =
new EventChannel(flutterState.binaryMessenger, "flutter.io/videoPlayer/videoEvents" + id);

videoPlayer =
VideoPlayerTextureApproach.create(
flutterState.applicationContext,
VideoPlayerEventCallbacks.bindTo(eventChannel),
VideoPlayerEventCallbacks.bindTo(createEventChannel(id)),
handle,
videoAsset,
options);
Expand All @@ -143,6 +139,12 @@ public void initialize() {
return id;
}

@NonNull
private EventChannel createEventChannel(long id) {
return new EventChannel(
flutterState.binaryMessenger, "flutter.io/videoPlayer/videoEvents" + id);
}

@NonNull
private VideoPlayer getPlayer(long textureId) {
VideoPlayer player = videoPlayers.get(textureId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ final class VideoPlayerTextureApproach extends VideoPlayer
@NonNull private final TextureRegistry.SurfaceProducer surfaceProducer;
@Nullable private ExoPlayerState savedStateDuring;

private static final Messages.PlatformVideoViewType viewType =
Messages.PlatformVideoViewType.TEXTURE_VIEW;
/**
* Creates a video player.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
import androidx.media3.common.PlaybackParameters;
import androidx.media3.common.Player;
import androidx.media3.exoplayer.ExoPlayer;
import io.flutter.view.TextureRegistry;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
Expand Down Expand Up @@ -188,12 +187,4 @@ public void disposeReleasesExoPlayer() {

verify(mockExoPlayer).release();
}

// TODO(matanlurey): Replace with inline calls to onSurfaceAvailable once
// available on stable; see https://github.com/flutter/flutter/issues/155131.
// This separate method only exists to scope the suppression.
@SuppressWarnings({"deprecation", "removal"})
void simulateSurfaceCreation(TextureRegistry.SurfaceProducer.Callback producerLifecycle) {
producerLifecycle.onSurfaceCreated();
}
}

0 comments on commit f3590a8

Please sign in to comment.