Skip to content

[video_player] Move Android buffer updates to Dart #9771

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

Conversation

stuartmorgan-g
Copy link
Contributor

Since ExoPlayer does not generate events for changes to the buffer position, the video_player_android synthesizes them via polling. This moves the logic to generate these events from Java to Dart, to reduce the amount of logic on the Java side.

This does not attempt to fix the structural problem that the workaround was piggy-backed on the fact that the app-facing package polls frequently for the playback position, generating buffer update events when that polling happens rather than via any independent internal polling system. This approach is not only fragile, as it depends on logic defined in another package, but already doesn't work in the case of a paused video. However, this refactoring will make it possible to change this behavior in Dart code in a follow-up focused on that behavioral change. This change should be behavior-neutral, except that it adds the simple improvement of not creating events if the buffer position hasn't changed, so that clients aren't getting a stream of useless buffer position events during playback.

This version will also be more efficient from a platform channel standpoint, since instead of sending a separate message with the buffer position, it's returned in the same call as the playback position.

To support synthesizing events on the Dart side, this adds a custom stream controller in Dart, instead of creating it directly from the EventChannel stream; this change will make future changes to a non-method-channel approach (e.g., FFI callbacks) straightforward. As this adds non-trivial additional per-player-instance state on the Dart side, this adds a Dart player instance class, and consolidates existing state into that class, including passthrough methods for the API calls, making it clearer which state is plugin level vs player level.

Part of flutter/flutter#172763

Pre-Review Checklist

Note: The Flutter team is currently trialing the use of Gemini Code Assist for GitHub. Comments from the gemini-code-assist bot should not be taken as authoritative feedback from the Flutter team. If you find its comments useful you can update your code accordingly, but if you are unsure or disagree with the feedback, please feel free to wait for a Flutter team member's review for guidance on which automated comments should be addressed.

Footnotes

  1. Regular contributors who have demonstrated familiarity with the repository guidelines only need to comment if the PR is not auto-exempted by repo tooling. 2 3

Copy link

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request is a nice refactoring that moves the buffer update logic from the native Android side to Dart. This simplifies the native code and improves efficiency by bundling playback and buffer position updates into a single platform channel call. The introduction of the _PlayerInstance class is a great structural improvement for managing per-player state in Dart.

I have a couple of suggestions for minor improvements: one to align a test method name with Java conventions, and another to remove what appears to be dead code following the refactoring. Overall, this is a solid change.

Comment on lines +341 to +344
'bufferingUpdate' => VideoEvent(
eventType: VideoEventType.bufferingUpdate,
buffered: _bufferRangeForPosition(map['position'] as int),
),

Choose a reason for hiding this comment

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

medium

This bufferingUpdate case appears to be unreachable. With the refactoring in this PR, the logic for sending buffer updates has been moved from the native Java code to the getPosition method in Dart. The native side should no longer be sending 'bufferingUpdate' events, making this case dead code. It can be safely removed.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is not correct; the Java code still sends this when the player enters STATE_BUFFERING.

),
_ => VideoEvent(eventType: VideoEventType.unknown),
};
});
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This switch is moved almost unchanged to the player instance class below.


// Turns a single buffer position, which is what ExoPlayer reports, into the
// DurationRange array expected by [VideoEventType.bufferingUpdate].
List<DurationRange> _bufferRangeForPosition(int milliseconds) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This was extracted from the switch to allow it to be used in _updateBufferingState as well.

// that the app-facing package polls getPosition frequently, which makes
// this fragile (for instance, as of writing, this won't be called while
// the video is paused). It should instead be called on its own timer,
// independent of higher-level package logic.
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I've expanded on the explanation relative to the version removed from VideoPlayer.java, but it's still fundamentally the same hack as the Java version was for now (to minimize behavioral changes in this PR).

@@ -50,7 +60,12 @@ abstract class VideoPlayerInstanceApi {
void setVolume(double volume);
void setPlaybackSpeed(double speed);
void play();
int getPosition();
void seekTo(int position);
Copy link
Contributor

Choose a reason for hiding this comment

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

Can we specify that the position is also in milliseconds here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good call; added docs to all of these methods.

Comment on lines 60 to 61
void setVolume(double volume);
void setPlaybackSpeed(double speed);
Copy link
Contributor

Choose a reason for hiding this comment

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

I think it might help to provide what that max/min bounds of these values would be or at least specify the unit.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Added.

VideoViewType.platformView => const _VideoPlayerPlatformViewState(),
};
final String eventChannelName =
'flutter.io/videoPlayer/videoEvents$playerId';
Copy link
Contributor

Choose a reason for hiding this comment

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

Can we make the magic string a constant outside of this method?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Extracted, and added a comment about the value it has to match on the other side.

@stuartmorgan-g stuartmorgan-g added the autosubmit Merge PR when tree becomes green via auto submit App label Aug 15, 2025
@auto-submit auto-submit bot merged commit 5c52c55 into flutter:main Aug 15, 2025
80 checks passed
engine-flutter-autoroll added a commit to engine-flutter-autoroll/flutter that referenced this pull request Aug 15, 2025
github-merge-queue bot pushed a commit to flutter/flutter that referenced this pull request Aug 15, 2025
flutter/packages@09533b7...5c52c55

2025-08-15 [email protected] [video_player] Move Android buffer
updates to Dart (flutter/packages#9771)
2025-08-15 [email protected] [webview_flutter] Add support for
payment requests on Android (flutter/packages#9679)
2025-08-15 [email protected]
[vector_graphics_compiler] Set the m4_10 (Z scale) value to 1 when
constructing an AffineMatrix from an SVG matrix (flutter/packages#9813)
2025-08-15 [email protected] [url_launcher_ios] Fix test button text to
work on iOS 26 (flutter/packages#9766)
2025-08-15 [email protected] [video_player] Simplify native iOS
code (flutter/packages#9800)
2025-08-14 [email protected] [image_picker] Add the ability to
pick multiple videos - platform_interface (flutter/packages#9804)

If this roll has caused a breakage, revert this CL and stop the roller
using the controls here:
https://autoroll.skia.org/r/flutter-packages-flutter-autoroll
Please CC [email protected] on the revert to ensure that a
human
is aware of the problem.

To file a bug in Flutter:
https://github.com/flutter/flutter/issues/new/choose

To report a problem with the AutoRoller itself, please file a bug:
https://issues.skia.org/issues/new?component=1389291&template=1850622

Documentation for the AutoRoller is here:
https://skia.googlesource.com/buildbot/+doc/main/autoroll/README.md
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
autosubmit Merge PR when tree becomes green via auto submit App p: video_player platform-android
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants