Skip to content

[video_player_android] Platform view support #8466

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
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
60 commits
Select commit Hold shift + click to select a range
15a0d09
Add platform interface changes
FirentisTFW Jan 21, 2025
25520fc
Add platform view support for Android
FirentisTFW Jan 21, 2025
28d38c5
Rearrange native unit tests after merge
FirentisTFW Jan 21, 2025
0d3a753
Remove a todo in web implementation
FirentisTFW Jan 21, 2025
ea16d63
Set initial value of nextNonTexturePlayerId to Long.MAX_VALUE
FirentisTFW Jan 21, 2025
25425ca
Use player provider instead of modifiable list
FirentisTFW Jan 21, 2025
ce2aa51
Remove empty line
FirentisTFW Jan 21, 2025
8aeca10
Check player view states in Dart unit tests
FirentisTFW Jan 21, 2025
209f34f
Restore previous formatting for comments
FirentisTFW Jan 21, 2025
42cb659
Add missing licenses
FirentisTFW Jan 21, 2025
690ec54
Format files
FirentisTFW Jan 21, 2025
6db9e6d
Fix linting issues
FirentisTFW Jan 21, 2025
277bff7
Rename textureId to playerId in platform interace methods
FirentisTFW Jan 21, 2025
5aca5db
Rename textureId to playerId in Android Dart files
FirentisTFW Jan 21, 2025
751d529
Rename textureId to playerId in Android native code
FirentisTFW Jan 21, 2025
e6f856b
Add an ignore to UI test (it times out on Firebase Test Lab)
FirentisTFW Jan 21, 2025
b752b4c
Merge branch 'main' into feature/video-player-android-platform-view-s…
FirentisTFW Jan 28, 2025
8fc1fa0
Bump platform interface dependency to 6.3.0
FirentisTFW Jan 28, 2025
b745dcc
Remove resolved todo and fix dartdocs
FirentisTFW Jan 28, 2025
773cfc1
Add missing import
FirentisTFW Jan 28, 2025
89dfcea
Format test file
FirentisTFW Jan 28, 2025
f59463a
Add more context to class names for native views
FirentisTFW Jan 28, 2025
650cc21
Add missing docs for Android
FirentisTFW Jan 28, 2025
18463e7
Format file
FirentisTFW Jan 28, 2025
11647d0
Remove redundant async keyword
FirentisTFW Jan 29, 2025
8f9ccd8
Depend on platform interface 6.3.0
FirentisTFW Feb 10, 2025
0c2e19b
Add more details to javadocs for players
FirentisTFW Feb 10, 2025
17d5c6c
Rename method for clarity
FirentisTFW Feb 10, 2025
10640da
Remove "///" from a javadoc
FirentisTFW Feb 11, 2025
7917c0c
Add unit tests for NativeVideoView and NativeVideoViewFactory
FirentisTFW Feb 19, 2025
06b20a1
Add unit tests for VideoPlayerPlugin.create method
FirentisTFW Feb 19, 2025
1e51f8c
Add empty line to separate concepts
FirentisTFW Feb 19, 2025
e2a5afa
Clean up newly added tests
FirentisTFW Feb 19, 2025
2692564
Sort imports
FirentisTFW Feb 19, 2025
f5258df
Add missing licenses
FirentisTFW Feb 19, 2025
1bbe2c2
Split ExoPlayerEventListener into two classes, for platform view and …
FirentisTFW Feb 24, 2025
f43e9ba
Make class final
FirentisTFW Feb 24, 2025
22c9241
Use "PlatformView" instead of "NativeView" everywhere
FirentisTFW Feb 24, 2025
df3959d
Rename TextureBased to Texture, move texture player to a separate pac…
FirentisTFW Feb 24, 2025
4575c4c
Make VideoPlayer abstract
FirentisTFW Feb 24, 2025
eae1eb5
Clean up analysis
FirentisTFW Feb 24, 2025
c39b14c
Rename nextNonTexturePlayerId to nextPlatformViewPlayerId
FirentisTFW Feb 24, 2025
8d3279d
Split tests for exo player event listeners
FirentisTFW Feb 24, 2025
2862323
Update implementation comment
FirentisTFW Feb 24, 2025
314bf82
Use widget instead of a method
FirentisTFW Feb 24, 2025
feca35d
Move VideoPlayerProvider to PlatformVideoViewFactory
FirentisTFW Feb 24, 2025
7956b19
Comment on swapping width and height
FirentisTFW Feb 25, 2025
70ed428
Remove redundant abstract class method
FirentisTFW Feb 25, 2025
ce63f14
Handle SurfaceView on Android 9 correctly, document workarounds
FirentisTFW Feb 25, 2025
81a71a3
Test output, not internal state
FirentisTFW Feb 25, 2025
41891d2
Remove overriden == operator as it's not needed anymore
FirentisTFW Feb 25, 2025
6bf34e7
Clean up code
FirentisTFW Feb 25, 2025
dc08e2d
Format code
FirentisTFW Feb 25, 2025
0681400
Use custom test class in VideoPlayerTest
FirentisTFW Feb 25, 2025
b0f282d
Add missing licenses
FirentisTFW Feb 25, 2025
6c17ebc
Add a todo to decouple identifiers in the future
FirentisTFW Feb 26, 2025
9895676
Format file
FirentisTFW Feb 26, 2025
da1b78d
Resolve lint issues
FirentisTFW Feb 26, 2025
ae4bfae
Inline method
FirentisTFW Feb 26, 2025
371c462
Format file
FirentisTFW Feb 26, 2025
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
3 changes: 2 additions & 1 deletion packages/video_player/video_player_android/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
## NEXT
## 2.8.0

* Adds support for platform views as an optional way of displaying a video.
* Suppresses deprecation and removal warnings for
`TextureRegistry.SurfaceProducer.onSurfaceDestroyed`.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,18 @@

package io.flutter.plugins.videoplayer;

import android.os.Build;
import androidx.annotation.NonNull;
import androidx.annotation.OptIn;
import androidx.media3.common.Format;
import androidx.media3.common.PlaybackException;
import androidx.media3.common.Player;
import androidx.media3.common.VideoSize;
import androidx.media3.exoplayer.ExoPlayer;
import java.util.Objects;

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

private enum RotationDegrees {
protected enum RotationDegrees {
ROTATE_0(0),
ROTATE_90(90),
ROTATE_180(180),
Expand All @@ -46,11 +41,8 @@ public int getDegrees() {
}
}

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

ExoPlayerEventListener(ExoPlayer exoPlayer, VideoPlayerCallbacks events, boolean initialized) {
public ExoPlayerEventListener(
@NonNull ExoPlayer exoPlayer, @NonNull VideoPlayerCallbacks events, boolean initialized) {
this.exoPlayer = exoPlayer;
this.events = events;
this.isInitialized = initialized;
Expand All @@ -68,90 +60,7 @@ private void setBuffering(boolean buffering) {
}
}

@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) {
RotationDegrees reportedRotationCorrection = RotationDegrees.ROTATE_0;

if (Build.VERSION.SDK_INT <= 21) {
// On API 21 and below, Exoplayer may not internally handle rotation correction
// and reports it through VideoSize.unappliedRotationDegrees. We may apply it to
// fix the case of upside-down playback.
try {
reportedRotationCorrection =
RotationDegrees.fromDegrees(videoSize.unappliedRotationDegrees);
rotationCorrection =
getRotationCorrectionFromUnappliedRotation(reportedRotationCorrection);
} catch (IllegalArgumentException e) {
// Unapplied rotation other than 0, 90, 180, 270 reported by VideoSize. Because this is unexpected,
// we apply no rotation correction.
reportedRotationCorrection = RotationDegrees.ROTATE_0;
rotationCorrection = 0;
}
}
// TODO(camsim99): Replace this with a call to `handlesCropAndRotation` when it is
// available in stable. https://github.com/flutter/flutter/issues/157198
else if (Build.VERSION.SDK_INT < 29) {
// When the SurfaceTexture backend for Impeller is used, the preview should already
// be correctly rotated.
rotationCorrection = 0;
} else {
// The video's Format also provides a rotation correction that may be used to
// correct the rotation, so we try to use that to correct the video rotation
// when the ImageReader backend for Impeller is used.
rotationCorrection = getRotationCorrectionFromFormat(exoPlayer);

try {
reportedRotationCorrection = RotationDegrees.fromDegrees(rotationCorrection);
} catch (IllegalArgumentException e) {
// Rotation correction other than 0, 90, 180, 270 reported by Format. Because this is unexpected,
// we apply no rotation correction.
reportedRotationCorrection = RotationDegrees.ROTATE_0;
rotationCorrection = 0;
}
}

// Switch the width/height if video was taken in portrait mode and a rotation
// correction was detected.
if (reportedRotationCorrection == RotationDegrees.ROTATE_90
|| reportedRotationCorrection == RotationDegrees.ROTATE_270) {
width = videoSize.height;
height = videoSize.width;
}
}
events.onInitialized(width, height, exoPlayer.getDuration(), rotationCorrection);
}

private int getRotationCorrectionFromUnappliedRotation(RotationDegrees unappliedRotationDegrees) {
int rotationCorrection = 0;

// 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 unappliedRotationDegrees of 180 (other orientations
// work correctly without correction).
if (unappliedRotationDegrees == RotationDegrees.ROTATE_180) {
rotationCorrection = unappliedRotationDegrees.getDegrees();
}

return rotationCorrection;
}

@OptIn(markerClass = androidx.media3.common.util.UnstableApi.class)
// A video's Format and its rotation degrees are unstable because they are not guaranteed
// the same implementation across API versions. It is possible that this logic may need
// revisiting should the implementation change across versions of the Exoplayer API.
private int getRotationCorrectionFromFormat(ExoPlayer exoPlayer) {
Format videoFormat = Objects.requireNonNull(exoPlayer.getVideoFormat());
return videoFormat.rotationDegrees;
}
protected abstract void sendInitialized();

@Override
public void onPlaybackStateChanged(final int playbackState) {
Expand All @@ -161,6 +70,10 @@ public void onPlaybackStateChanged(final int playbackState) {
events.onBufferingUpdate(exoPlayer.getBufferedPosition());
break;
case Player.STATE_READY:
if (isInitialized) {
return;
}
isInitialized = true;
sendInitialized();
break;
case Player.STATE_ENDED:
Expand All @@ -178,7 +91,8 @@ 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 @@ -4,6 +4,7 @@

package io.flutter.plugins.videoplayer;

import androidx.annotation.NonNull;
import androidx.media3.common.PlaybackParameters;
import androidx.media3.exoplayer.ExoPlayer;

Expand All @@ -15,7 +16,7 @@
* is reclaimed. Upon <em>resume</em>, the player will need to be recreated, but start again at the
* previous point (and settings).
*/
final class ExoPlayerState {
public final class ExoPlayerState {
/**
* Saves a representation of the current state of the player at the current point in time.
*
Expand All @@ -24,12 +25,13 @@ final class ExoPlayerState {
* @param exoPlayer the active player instance.
* @return an opaque object representing the state.
*/
static ExoPlayerState save(ExoPlayer exoPlayer) {
@NonNull
public static ExoPlayerState save(@NonNull ExoPlayer exoPlayer) {
return new ExoPlayerState(
/*position=*/ exoPlayer.getCurrentPosition(),
/*repeatMode=*/ exoPlayer.getRepeatMode(),
/*volume=*/ exoPlayer.getVolume(),
/*playbackParameters=*/ exoPlayer.getPlaybackParameters());
/* position= */ exoPlayer.getCurrentPosition(),
/* repeatMode= */ exoPlayer.getRepeatMode(),
/* volume= */ exoPlayer.getVolume(),
/* playbackParameters= */ exoPlayer.getPlaybackParameters());
}

private ExoPlayerState(
Expand Down Expand Up @@ -60,7 +62,7 @@ private ExoPlayerState(
*
* @param exoPlayer the new player instance to reflect the state back to.
*/
void restore(ExoPlayer exoPlayer) {
public void restore(@NonNull ExoPlayer exoPlayer) {
exoPlayer.seekTo(position);
exoPlayer.setRepeatMode(repeatMode);
exoPlayer.setVolume(volume);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ final class HttpVideoAsset extends VideoAsset {

@NonNull
@Override
MediaItem getMediaItem() {
public MediaItem getMediaItem() {
MediaItem.Builder builder = new MediaItem.Builder().setUri(assetUrl);
String mimeType = null;
switch (streamingFormat) {
Expand All @@ -57,8 +57,9 @@ MediaItem getMediaItem() {
return builder.build();
}

@NonNull
@Override
MediaSource.Factory getMediaSourceFactory(Context context) {
public MediaSource.Factory getMediaSourceFactory(@NonNull Context context) {
return getMediaSourceFactory(context, new DefaultHttpDataSource.Factory());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,13 @@ final class LocalVideoAsset extends VideoAsset {

@NonNull
@Override
MediaItem getMediaItem() {
public MediaItem getMediaItem() {
return new MediaItem.Builder().setUri(assetUrl).build();
}

@NonNull
@Override
MediaSource.Factory getMediaSourceFactory(Context context) {
public MediaSource.Factory getMediaSourceFactory(@NonNull Context context) {
return new DefaultMediaSourceFactory(context);
}
}
Loading