Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ - (int64_t)configurePlayer:(FVPVideoPlayer *)player
// Set up the event channel.
FVPEventBridge *eventBridge = [[FVPEventBridge alloc]
initWithMessenger:messenger
channelName:[NSString stringWithFormat:@"flutter.io/videoPlayer/videoEvents%@",
channelName:[NSString stringWithFormat:@"flutter.dev/videoPlayer/videoEvents%@",
Copy link
Collaborator Author

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.

channelSuffix]];
player.eventListener = eventBridge;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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
Expand Down Expand Up @@ -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',
),
);
});
}

Expand Down Expand Up @@ -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
Expand All @@ -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',
),
};
}

Expand All @@ -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();

Choose a reason for hiding this comment

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

high

The _eventStreamController is not closed in the dispose method. This can lead to a resource leak. You should close the stream controller to release its resources when the player instance is disposed.

    await _eventStreamController.close();
    await _api.dispose();

}

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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name: video_player_avfoundation
description: iOS and macOS implementation of the video_player plugin.
repository: https://github.com/flutter/packages/tree/main/packages/video_player/video_player_avfoundation
issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+video_player%22
version: 2.8.7
version: 2.8.8

environment:
sdk: ^3.9.0
Expand Down
Loading