-
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
base: main
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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. | ||
|
|
||
| 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,89 @@ 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>(); | ||||||||||
|
||||||||||
| final StreamController<VideoEvent> _eventStreamController = | |
| StreamController<VideoEvent>(); | |
| final StreamController<VideoEvent> _eventStreamController = | |
| StreamController<VideoEvent>.broadcast(); |
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.
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.