Repository navigation
Conversation
There was a problem hiding this comment.
Code Review
This pull request introduces VideoAssetProvider and VideoPlayerPlugin.setVideoAssetProvider to the Android video player plugin, enabling custom video asset resolution for URIs. It also adds unit tests to verify this behavior and bumps the package version to 2.13.0. The reviewer recommended marking the static videoAssetProvider field as volatile to ensure thread visibility and prevent stale reads.
| private final VideoPlayerOptions sharedOptions = new VideoPlayerOptions(); | ||
| private long nextPlayerIdentifier = 1; | ||
|
|
||
| private static @Nullable VideoAssetProvider videoAssetProvider; |
There was a problem hiding this comment.
Since videoAssetProvider is a static field that can be set from any thread (e.g., during background initialization or dependency injection) and read on the main thread, it should be marked as volatile to ensure thread visibility and prevent stale reads.
| private static @Nullable VideoAssetProvider videoAssetProvider; | |
| private static volatile @Nullable VideoAssetProvider videoAssetProvider; |
|
Adding @mboetger to review for whether allowing modification of ExoPlayer internals like this is something we want to expose; generally we minimize native hook points as they can significantly reduce our ability to make non-breaking updates in the future.
Adding @bparrishMines to guide the design here if this does move forward. We traditionally do not consider our native plugin code to be a public API surface, and instead consider it equivalent to lib/src/. When we deviate from that we do so in limited and clearly documented ways, and @bparrishMines can point to examples of what that would look like. |
|
I am not a fan on exposing the internal implementation as a public extension point. |
Unfortunately I'm pretty much out of ideas on how to support offline playback on Android then, without making the whole download mechanism a feature of the player itself - which I think is even more unacceptable. One approach that came to mind, was to use CreationOptions, something like this: But this approach fails to the fact that SimpleCache is a single-instance-per-directory, so the very same plugin that does the download needs to own the cache for playback. The last thing that would also make some sense: Edit: Would that be acceptable instead? |
|
In https://pub.dev/packages/webview_flutter_android#external-native-api Plugins are instanced because multiple could exist in an add-to-app Flutter app, so an instance method is recommended.
|
|
This pull request is not mergeable in its current state, likely because of a merge conflict. Pre-submit CI jobs were not triggered. Pushing a new commit to this branch that resolves the issue will result in pre-submit jobs being scheduled. |
|
[from triage] @mboetger what does the Android team think of the proposed approach above? Thanks! |
|
@a1rwulf You stated that this is only and issue on Android, right? Do you know how this is handled on iOS? Also, would it help if |
|
@bparrishMines Yes, this only affects Android. The reason is where each platform’s downloader puts the data. iOS: Android: A CacheDataSource over the same SimpleCache instance. SimpleCache allows only one instance per directory, so the player can’t open its own over the download directory. It has to use the downloader’s instance. Both live in the player’s MediaSource.Factory and MediaItem, which is why I ended up needing a hook into how video_player_android builds its VideoAsset. On iOS the downloader’s output is already something the player understands. On addMediaItem + flutter_cache_manager: I don’t think it helps, for two reasons: When the item is added isn’t the problem. The problem is which DataSource reads it. A VideoAsset added after initialization would still be read through the plugin’s own data source factory, which knows nothing about the download cache. So the real need on Android is narrow: let playback for a given URL read through a Cache that something else owns, using a known set of stream keys. That’s why I asked about the shared-package route (in the style of flutter_plugin_android_lifecycle) holding the Cache. video_player_android would only need to accept that cache and the stream keys, not expose VideoAsset as an extension point. I’m happy to move to an instance-based registration following the WebViewFlutterAndroidExternalApi pattern (one documented class, provider registered per engine via the plugin instance rather than a static). Identifier-keyed lookup on its own wouldn’t be enough here, though: the player id is assigned in the same call that builds and prepares the ExoPlayer, so by the time an id exists, playback has already started loading from the network. The cache has to be chosen before preparation, which is why the hook is keyed by URL at creation time. |
|
@mboetger this is still waiting on your feedback |
VideoPlayerAndroidExternalApi.setMediaConfigProvider(binding) { request ->
val downloadRequest =
downloadTracker.getDownloadRequest(Uri.parse(request.uri))
?: return@setMediaConfigProvider null
VideoPlayerAndroidExternalApi.MediaConfig(
mediaItem = downloadRequest.toMediaItem(),
mediaSourceFactory = DefaultMediaSourceFactory(request.context)
.setDataSourceFactory(cacheDataSourceFactory),
)
}A few implementation details if you go this route:
|
…g media configuration Adds a documented native API, modeled on WebViewFlutterAndroidExternalApi, that lets other packages supply the Media3 MediaItem and MediaSource.Factory for a player. A package registers a MediaConfigProvider for its engine with setMediaConfigProvider(binding, provider); the provider is asked for every player created in that engine, and returning null keeps the plugin's own handling. This is what an offline-download package needs to play content from a Media3 download cache. - Providers are looked up when a player is created, so registration works regardless of plugin attachment order. They stay registered until the registering package removes them. - The request carries the context, URI, the MIME type implied by the format hint, a read-only view of the HTTP headers, and the user agent. Its factory is hidden from Java so fields can be added later. - Creating a player now releases the surface, event channel, and ExoPlayer if construction fails, which a provider's configuration can cause. - media3-exoplayer becomes an api dependency because MediaItem and MediaSource.Factory are part of the public signature.
fa57d78 to
73926a5
Compare
|
Hi @a1rwulf, thank you for your contribution! This pull request has been converted to a draft because it does not appear to follow our pull request template:
How to fix this
Click here for the raw Markdown of the Pre-Review Checklist to copy and paste## Pre-Review Checklist
- [ ] I read the [Contributor Guide] and followed the process outlined there for submitting PRs.
- [ ] I read the [AI contribution guidelines] and understand my responsibilities, or I am not using AI tools.
- [ ] I read the [Tree Hygiene] page, which explains my responsibilities.
- [ ] I read and followed the [relevant style guides] and ran [the auto-formatter].
- [ ] I signed the [CLA].
- [ ] The title of the PR starts with the name of the package surrounded by square brackets, e.g. `[shared_preferences]`
- [ ] I [linked to at least one issue that this PR fixes] in the description above.
- [ ] I followed [the version and CHANGELOG instructions], using [semantic versioning] and the [repository CHANGELOG style], or I have commented below to indicate which documented exception this PR falls under[^1].
- [ ] I updated/added any relevant documentation (doc comments with `///`).
- [ ] I added new tests to check the change I am making, or I have commented below to indicate which [test exemption] this PR falls under[^1].
- [ ] All existing and new tests are passing.
If you need help, consider asking for advice on the #hackers-new channel on [Discord].
**Note**: The Flutter team is currently trialing the use of [Gemini Code Assist for GitHub](https://developers.google.com/gemini-code-assist/docs/review-github-code). 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.
[^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.
<!-- Links -->
[Contributor Guide]: https://github.com/flutter/packages/blob/main/CONTRIBUTING.md
[AI contribution guidelines]: https://github.com/flutter/flutter/blob/master/CONTRIBUTING.ai.md#pull-requests
[Tree Hygiene]: https://github.com/flutter/flutter/blob/master/docs/contributing/Tree-hygiene.md
[relevant style guides]: https://github.com/flutter/packages/blob/main/CONTRIBUTING.md#style
[the auto-formatter]: https://github.com/flutter/packages/blob/main/script/tool/README.md#format-code
[CLA]: https://cla.developers.google.com/
[Discord]: https://github.com/flutter/flutter/blob/master/docs/contributing/Chat.md
[linked to at least one issue that this PR fixes]: https://github.com/flutter/flutter/blob/master/docs/contributing/Tree-hygiene.md#overview
[the version and CHANGELOG instructions]: https://github.com/flutter/flutter/blob/master/docs/ecosystem/contributing/README.md#version-and-changelog-updates
[semantic versioning]: https://dart.dev/tools/pub/versioning#semantic-versions
[repository CHANGELOG style]: https://github.com/flutter/flutter/blob/master/docs/ecosystem/contributing/README.md#changelog-style
[test exemption]: https://github.com/flutter/flutter/blob/master/docs/contributing/Tree-hygiene.md#tests |
@mboetger Thanks, I reworked the PR along those lines:
|
There was a problem hiding this comment.
Code Review
This pull request introduces VideoPlayerAndroidExternalApi to allow Android applications and packages to supply custom Media3 MediaItem and MediaSource.Factory configurations. It also adds resource cleanup logic to release ExoPlayer resources if player construction fails, refactors StreamingFormat to associate MIME types directly, and includes comprehensive unit and integration tests. Feedback on the changes highlights a potential NullPointerException in VideoPlayerPlugin.kt when wrapping options.httpHeaders with Collections.unmodifiableMap without a null check.
| uri, | ||
| streamingFormat.mimeType, | ||
| // Read-only, so that a provider cannot change the headers of the fallback below. | ||
| Collections.unmodifiableMap(options.httpHeaders), |
There was a problem hiding this comment.
options.httpHeaders can be null at runtime (for example, if no headers are provided or if the map is omitted). Passing a null map to Collections.unmodifiableMap will throw a NullPointerException. To prevent this, safely handle the nullability of options.httpHeaders by defaulting to an empty map.
| Collections.unmodifiableMap(options.httpHeaders), | |
| Collections.unmodifiableMap(options.httpHeaders ?: emptyMap()), |
There was a problem hiding this comment.
CreationOptions.httpHeaders is non-nullable end to end: it's a required non-null Map<String, String> in the Pigeon definition (defaulting to const {} on the Dart side), and the generated Kotlin decoder casts it with as Map<String, String>, so it can't be null here. ?: emptyMap() would just be a no-op the compiler warns about.
Adds
VideoPlayerAndroidExternalApi, a documented native API modeled onWebViewFlutterAndroidExternalApi, that lets another package supply the Media3MediaItemandMediaSource.Factoryfor a player. A package registers aMediaConfigProviderfor its engine withsetMediaConfigProvider(binding, provider); it is asked for every player created in that engine, and returningnullkeeps the plugin's own handling.VideoAssetstays internal.onDetachedFromEngine.media3-exoplayeris now anapidependency, sinceMediaItemandMediaSource.Factoryare in the public signature.This lets a companion package play HLS downloads from a Media3 download cache (provider: https://github.com/a1rwulf/video_player_offline/blob/video-player-external-api/packages/video_player_offline_android/android/src/main/java/io/flutter/plugins/videoplayeroffline/OfflineMediaConfigProvider.java); offline playback was verified on an Android emulator in airplane mode.
Companion package: https://github.com/a1rwulf/video_player_offline/tree/video-player-external-api
Fixes flutter/flutter#59759
Pre-Review Checklist
[shared_preferences]///).If you need help, consider asking for advice on the #hackers-new channel on Discord.
Note: The Flutter team is currently trialing the use of Gemini Code Assist for GitHub. Comments from the
gemini-code-assistbot 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
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