-
Notifications
You must be signed in to change notification settings - Fork 3.5k
[video_player_avfoundation] Create a Dart player instance #10490
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
Open
stuartmorgan-g
wants to merge
2
commits into
flutter:main
Choose a base branch
from
stuartmorgan-g:video-player-av-dart-player-instance
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+152
−101
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,7 @@ | ||
| ## 2.8.8 | ||
|
|
||
| * Refactors Dart internals for maintainability. | ||
|
|
||
| ## 2.8.7 | ||
|
|
||
| * Updates to Pigeon 26. | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -22,23 +22,16 @@ class AVFoundationVideoPlayer extends VideoPlayerPlatform { | |
| AVFoundationVideoPlayer({ | ||
| @visibleForTesting AVFoundationVideoPlayerApi? pluginApi, | ||
| @visibleForTesting | ||
| VideoPlayerInstanceApi Function(int playerId)? playerProvider, | ||
| VideoPlayerInstanceApi Function(int playerId)? playerApiProvider, | ||
| }) : _api = pluginApi ?? AVFoundationVideoPlayerApi(), | ||
| _playerProvider = playerProvider ?? _productionApiProvider; | ||
| _playerApiProvider = playerApiProvider ?? _productionApiProvider; | ||
|
|
||
| final AVFoundationVideoPlayerApi _api; | ||
| // A method to create VideoPlayerInstanceApi instances, which can be | ||
| // overridden for testing. | ||
| final VideoPlayerInstanceApi Function(int mapId) _playerProvider; | ||
| final VideoPlayerInstanceApi Function(int mapId) _playerApiProvider; | ||
|
|
||
| /// A map that associates player ID with a view state. | ||
| /// This is used to determine which view type to use when building a view. | ||
| @visibleForTesting | ||
| final Map<int, VideoPlayerViewState> playerViewStates = | ||
| <int, VideoPlayerViewState>{}; | ||
|
|
||
| final Map<int, VideoPlayerInstanceApi> _players = | ||
| <int, VideoPlayerInstanceApi>{}; | ||
| final Map<int, _PlayerInstance> _players = <int, _PlayerInstance>{}; | ||
|
|
||
| /// Registers this class as the default instance of [VideoPlayerPlatform]. | ||
| static void registerWith() { | ||
|
|
@@ -52,9 +45,8 @@ class AVFoundationVideoPlayer extends VideoPlayerPlatform { | |
|
|
||
| @override | ||
| Future<void> dispose(int playerId) async { | ||
| final VideoPlayerInstanceApi? player = _players.remove(playerId); | ||
| final _PlayerInstance? player = _players.remove(playerId); | ||
| await player?.dispose(); | ||
| playerViewStates.remove(playerId); | ||
| } | ||
|
|
||
| @override | ||
|
|
@@ -118,18 +110,24 @@ class AVFoundationVideoPlayer extends VideoPlayerPlatform { | |
| playerId = await _api.createForPlatformView(pigeonCreationOptions); | ||
| state = const VideoPlayerPlatformViewState(); | ||
| } | ||
| playerViewStates[playerId] = state; | ||
| ensureApiInitialized(playerId); | ||
| ensurePlayerInitialized(playerId, state); | ||
|
|
||
| return playerId; | ||
| } | ||
|
|
||
| /// Returns the API instance for [playerId], creating it if it doesn't already | ||
| /// exist. | ||
| @visibleForTesting | ||
| VideoPlayerInstanceApi ensureApiInitialized(int playerId) { | ||
| return _players.putIfAbsent(playerId, () { | ||
| return _playerProvider(playerId); | ||
| void ensurePlayerInitialized(int playerId, VideoPlayerViewState viewState) { | ||
| _players.putIfAbsent(playerId, () { | ||
| return _PlayerInstance( | ||
| _playerApiProvider(playerId), | ||
| viewState, | ||
| eventChannel: EventChannel( | ||
| // This must match the channel name used in FVPVideoPlayerPlugin.m. | ||
| 'flutter.dev/videoPlayer/videoEvents$playerId', | ||
| ), | ||
| ); | ||
| }); | ||
| } | ||
|
|
||
|
|
@@ -162,48 +160,17 @@ class AVFoundationVideoPlayer extends VideoPlayerPlatform { | |
|
|
||
| @override | ||
| Future<void> seekTo(int playerId, Duration position) { | ||
| return _playerWith(id: playerId).seekTo(position.inMilliseconds); | ||
| return _playerWith(id: playerId).seekTo(position); | ||
| } | ||
|
|
||
| @override | ||
| Future<Duration> getPosition(int playerId) async { | ||
| final int position = await _playerWith(id: playerId).getPosition(); | ||
| return Duration(milliseconds: position); | ||
| return _playerWith(id: playerId).getPosition(); | ||
| } | ||
|
|
||
| @override | ||
| Stream<VideoEvent> videoEventsFor(int playerId) { | ||
| return _eventChannelFor(playerId).receiveBroadcastStream().map(( | ||
| dynamic event, | ||
| ) { | ||
| final Map<dynamic, dynamic> map = event as Map<dynamic, dynamic>; | ||
| return switch (map['event']) { | ||
| 'initialized' => VideoEvent( | ||
| eventType: VideoEventType.initialized, | ||
| duration: Duration(milliseconds: map['duration'] as int), | ||
| size: Size( | ||
| (map['width'] as num?)?.toDouble() ?? 0.0, | ||
| (map['height'] as num?)?.toDouble() ?? 0.0, | ||
| ), | ||
| ), | ||
| 'completed' => VideoEvent(eventType: VideoEventType.completed), | ||
| 'bufferingUpdate' => VideoEvent( | ||
| buffered: (map['values'] as List<dynamic>) | ||
| .map<DurationRange>(_toDurationRange) | ||
| .toList(), | ||
| eventType: VideoEventType.bufferingUpdate, | ||
| ), | ||
| 'bufferingStart' => VideoEvent( | ||
| eventType: VideoEventType.bufferingStart, | ||
| ), | ||
| 'bufferingEnd' => VideoEvent(eventType: VideoEventType.bufferingEnd), | ||
| 'isPlayingStateUpdate' => VideoEvent( | ||
| eventType: VideoEventType.isPlayingStateUpdate, | ||
| isPlaying: map['isPlaying'] as bool, | ||
| ), | ||
| _ => VideoEvent(eventType: VideoEventType.unknown), | ||
| }; | ||
| }); | ||
| return _playerWith(id: playerId).videoEvents; | ||
| } | ||
|
|
||
| @override | ||
|
|
@@ -219,16 +186,13 @@ class AVFoundationVideoPlayer extends VideoPlayerPlatform { | |
| @override | ||
| Widget buildViewWithOptions(VideoViewOptions options) { | ||
| final int playerId = options.playerId; | ||
| final VideoPlayerViewState? viewState = playerViewStates[playerId]; | ||
| final VideoPlayerViewState viewState = _playerWith(id: playerId).viewState; | ||
|
|
||
| return switch (viewState) { | ||
| VideoPlayerTextureViewState(:final int textureId) => Texture( | ||
| textureId: textureId, | ||
| ), | ||
| VideoPlayerPlatformViewState() => _buildPlatformView(playerId), | ||
| null => throw Exception( | ||
| 'Could not find corresponding view type for playerId: $playerId', | ||
| ), | ||
| }; | ||
| } | ||
|
|
||
|
|
@@ -246,13 +210,90 @@ class AVFoundationVideoPlayer extends VideoPlayerPlatform { | |
| ); | ||
| } | ||
|
|
||
| EventChannel _eventChannelFor(int playerId) { | ||
| return EventChannel('flutter.io/videoPlayer/videoEvents$playerId'); | ||
| _PlayerInstance _playerWith({required int id}) { | ||
| final _PlayerInstance? player = _players[id]; | ||
| return player ?? (throw StateError('No active player with ID $id.')); | ||
| } | ||
| } | ||
|
|
||
| VideoPlayerInstanceApi _playerWith({required int id}) { | ||
| final VideoPlayerInstanceApi? player = _players[id]; | ||
| return player ?? (throw StateError('No active player with ID $id.')); | ||
| /// An instance of a video player, corresponding to a single player ID in | ||
| /// [AVFoundationVideoPlayer]. | ||
| class _PlayerInstance { | ||
| _PlayerInstance( | ||
| this._api, | ||
| this.viewState, { | ||
| required EventChannel eventChannel, | ||
| }) : _eventChannel = eventChannel; | ||
|
|
||
| final VideoPlayerInstanceApi _api; | ||
| final VideoPlayerViewState viewState; | ||
| final EventChannel _eventChannel; | ||
| final StreamController<VideoEvent> _eventStreamController = | ||
| StreamController<VideoEvent>.broadcast(); | ||
| StreamSubscription<dynamic>? _eventSubscription; | ||
|
|
||
| Future<void> play() => _api.play(); | ||
|
|
||
| Future<void> pause() => _api.pause(); | ||
|
|
||
| Future<void> setLooping(bool looping) => _api.setLooping(looping); | ||
|
|
||
| Future<void> setVolume(double volume) => _api.setVolume(volume); | ||
|
|
||
| Future<void> setPlaybackSpeed(double speed) => _api.setPlaybackSpeed(speed); | ||
|
|
||
| Future<void> seekTo(Duration position) { | ||
| return _api.seekTo(position.inMilliseconds); | ||
| } | ||
|
|
||
| Future<Duration> getPosition() async { | ||
| return Duration(milliseconds: await _api.getPosition()); | ||
| } | ||
|
|
||
| Stream<VideoEvent> get videoEvents { | ||
| _eventSubscription ??= _eventChannel.receiveBroadcastStream().listen( | ||
| _onStreamEvent, | ||
| onError: (Object e) { | ||
| _eventStreamController.addError(e); | ||
| }, | ||
| ); | ||
|
|
||
| return _eventStreamController.stream; | ||
| } | ||
|
|
||
| Future<void> dispose() async { | ||
| await _eventSubscription?.cancel(); | ||
| unawaited(_eventStreamController.close()); | ||
| await _api.dispose(); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| } | ||
|
|
||
| void _onStreamEvent(dynamic event) { | ||
| final Map<dynamic, dynamic> map = event as Map<dynamic, dynamic>; | ||
| // The strings here must all match the strings in FVPEventBridge.m. | ||
| _eventStreamController.add(switch (map['event']) { | ||
| 'initialized' => VideoEvent( | ||
| eventType: VideoEventType.initialized, | ||
| duration: Duration(milliseconds: map['duration'] as int), | ||
| size: Size( | ||
| (map['width'] as num?)?.toDouble() ?? 0.0, | ||
| (map['height'] as num?)?.toDouble() ?? 0.0, | ||
| ), | ||
| ), | ||
| 'completed' => VideoEvent(eventType: VideoEventType.completed), | ||
| 'bufferingUpdate' => VideoEvent( | ||
| buffered: (map['values'] as List<dynamic>) | ||
| .map<DurationRange>(_toDurationRange) | ||
| .toList(), | ||
| eventType: VideoEventType.bufferingUpdate, | ||
| ), | ||
| 'bufferingStart' => VideoEvent(eventType: VideoEventType.bufferingStart), | ||
| 'bufferingEnd' => VideoEvent(eventType: VideoEventType.bufferingEnd), | ||
| 'isPlayingStateUpdate' => VideoEvent( | ||
| eventType: VideoEventType.isPlayingStateUpdate, | ||
| isPlaying: map['isPlaying'] as bool, | ||
| ), | ||
| _ => VideoEvent(eventType: VideoEventType.unknown), | ||
| }); | ||
| } | ||
|
|
||
| DurationRange _toDurationRange(dynamic value) { | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This was just an opportunistic change; I had mass-changed flutter.io to flutter.dev in most places a while ago, and apparently missed this channel name.