[video_player_android] Add video track selection support#11475
[video_player_android] Add video track selection support#11475nateshmbhat wants to merge 1 commit intoflutter:mainfrom
Conversation
Implements getVideoTracks() and selectVideoTrack() methods for video track (quality) selection using ExoPlayer. Android breakout PR for flutter#10688.
There was a problem hiding this comment.
Code Review
This pull request introduces video track selection capabilities to the Android video player plugin. Key changes include adding getVideoTracks(), selectVideoTrack(), and enableAutoVideoQuality() methods to the VideoPlayerInstanceApi and their implementations in VideoPlayer.java and android_video_player.dart. A workaround is implemented in selectVideoTrack to handle video dimension changes by temporarily disabling and re-enabling the video track type to force a renderer reset. New Pigeon messages and data structures are introduced to support video track information exchange, and ExoPlayerEventListener is updated to notify about video track changes. Comprehensive unit tests for the new video track functionalities have also been added. The review comment points out a potential crash risk in the postDelayed workaround within VideoPlayer.java if the player is disposed during the 150ms delay, as the current trackSelector == null guard is ineffective. It recommends using a cancellable callback mechanism or an isDisposed flag for robust handling.
| new android.os.Handler(android.os.Looper.getMainLooper()) | ||
| .postDelayed( | ||
| () -> { | ||
| // Guard against player disposal during the delay | ||
| if (trackSelector == null) { | ||
| return; | ||
| } | ||
|
|
||
| trackSelector.setParameters( | ||
| trackSelector | ||
| .buildUponParameters() | ||
| .setTrackTypeDisabled(C.TRACK_TYPE_VIDEO, false) | ||
| .setOverrideForType(override) | ||
| .build()); | ||
|
|
||
| // Restore playback state | ||
| exoPlayer.seekTo(currentPosition); | ||
| if (wasPlaying) { | ||
| exoPlayer.play(); | ||
| } | ||
| }, | ||
| 150); |
There was a problem hiding this comment.
The postDelayed workaround for dimension changes introduces a potential crash risk. If the VideoPlayer is disposed (and the ExoPlayer released) during the 150ms delay, the calls to exoPlayer.seekTo() and exoPlayer.play() inside the delayed callback will throw an IllegalStateException because the player has been released.
The current guard if (trackSelector == null) (line 414) is likely ineffective because trackSelector is a final field in this class and is not nullified during the dispose() call in the current implementation of this plugin.
Recommendation:
Use a mechanism to cancel the pending callback or a robust way to check if the player has been disposed. For example, you could use a member Handler and call handler.removeCallbacksAndMessages(null) in dispose(), or maintain an isDisposed boolean flag that is set to true in dispose() and checked at the beginning of the delayed callback.
Summary
Android breakout PR for #10688.
getVideoTracks()andselectVideoTrack()methods using ExoPlayer'sTrackSelectionOverrideonVideoTrackChangedevent callback for track change notificationsDependency Chain
This PR is second in a series of breakout PRs:
video_player_platform_interface([video_player_platform_interface] Add video track selection support #11474) - pendingvideo_player_android(this PR)video_player_avfoundation(pending)video_player+video_player_web(pending - original PR [video_player] : Add video track selection support for Android and iOS #10688 updated)Note: This PR depends on
video_player_platform_interface6.7.0 being published first.Test Plan
getVideoTracks()andselectVideoTrack()inVideoPlayerTest.javaonVideoTrackChangedcallback inVideoPlayerEventCallbacksTest.javaAndroidVideoPlayeroverrides