Skip to content

Commit 23a05b6

Browse files
authored
[video_player] Fix crash regression with async calls on iOS/macOS (flutter#13383)
Because Pigeon closures have references to the bridge object that serves as the API handler, it's not safe for it to have an `unowned` reference to the player instance that it delegates to. This changes it to a weak reference, and uses dummy return values as necessary for cases where the player has already been destroyed by the time the async call reaches the bridge. Also includes a unit test simulating that situation, which crashes without the fix. Fixes flutter#195056 ## Pre-Review Checklist [^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.
1 parent 9919b95 commit 23a05b6

4 files changed

Lines changed: 54 additions & 15 deletions

File tree

‎packages/video_player/video_player_avfoundation/CHANGELOG.md‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 2.12.3
2+
3+
* Fixes a potential crash when a player is disposed while an async method is in progress.
4+
15
## 2.12.2
26

37
* Converts native player implementation to Swift.

‎packages/video_player/video_player_avfoundation/darwin/RunnerTests/VideoPlayerTests.swift‎

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -935,6 +935,41 @@ private let hlsAudioTestURI =
935935
#expect(!frameDurationIsValid(CMTime.indefinite))
936936
}
937937

938+
@Test func apiBridgeHandlesPlayerDestruction() async throws {
939+
weak var weakPlayer: VideoPlayer?
940+
var apiBridge: VideoPlayerInstanceBridge?
941+
942+
let videoPlayerPlugin = try createInitializedPlugin(avFactory: StubAVFactory())
943+
944+
let identifier = try await videoPlayerPlugin.createPlatformViewPlayer(
945+
options: CreationOptions(uri: mp4TestURI, httpHeaders: [:]))
946+
947+
// Simulation a temporary lifetime extension of the bridge, which can happen if an
948+
// async Pigeon closure is executing when `dispose()` is called.
949+
autoreleasepool {
950+
let player = videoPlayerPlugin.playersByIdentifier[identifier]
951+
weakPlayer = player
952+
apiBridge = player?.hostApiBridge
953+
954+
player?.dispose()
955+
}
956+
957+
// Wait for the weak pointer to be invalidated, indicating that the player has been deallocated.
958+
let checkInterval = 0.1
959+
let maxTries = Int64(30 / checkInterval)
960+
for _ in 1...maxTries {
961+
if weakPlayer == nil {
962+
break
963+
}
964+
try await Task.sleep(nanoseconds: UInt64(checkInterval * 1_000_000_000))
965+
}
966+
967+
#expect(weakPlayer == nil)
968+
#expect(apiBridge != nil)
969+
// Calling the bridge should return a placeholder value rather than crash.
970+
#expect(try apiBridge?.getPosition() == 0)
971+
}
972+
938973
// MARK: - Helper Methods
939974

940975
/// Creates a plugin with the given dependencies, and default stubs for any that aren't provided,

‎packages/video_player/video_player_avfoundation/darwin/video_player_avfoundation/Sources/video_player_avfoundation/VideoPlayer.swift‎

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -25,81 +25,81 @@ import CoreMedia
2525
// is fixed, by making VideoPlayerInstanceApi a @MainActor protocol and returning to
2626
// having VideoPlayer conform to it.
2727
class VideoPlayerInstanceBridge: VideoPlayerInstanceApi {
28-
private unowned let player: VideoPlayer
28+
private weak var player: VideoPlayer?
2929

3030
init(player: VideoPlayer) {
3131
self.player = player
3232
}
3333

3434
func setLooping(_ looping: Bool) throws {
3535
MainActor.assumeIsolated {
36-
player.setLooping(looping)
36+
player?.setLooping(looping)
3737
}
3838
}
3939

4040
func setVolume(_ volume: Double) throws {
4141
MainActor.assumeIsolated {
42-
player.setVolume(volume)
42+
player?.setVolume(volume)
4343
}
4444
}
4545

4646
func setPlaybackSpeed(_ speed: Double) throws {
4747
MainActor.assumeIsolated {
48-
player.setPlaybackSpeed(speed)
48+
player?.setPlaybackSpeed(speed)
4949
}
5050
}
5151

5252
func play() throws {
5353
MainActor.assumeIsolated {
54-
player.play()
54+
player?.play()
5555
}
5656
}
5757

5858
func getPosition() throws -> Int64 {
5959
MainActor.assumeIsolated {
60-
return player.getPosition()
60+
return player?.getPosition() ?? 0
6161
}
6262
}
6363

6464
func seek(to position: Int64) async throws {
65-
try await player.seek(to: position)
65+
try await player?.seek(to: position)
6666
}
6767

6868
func pause() throws {
6969
MainActor.assumeIsolated {
70-
player.pause()
70+
player?.pause()
7171
}
7272
}
7373

7474
func dispose() throws {
7575
MainActor.assumeIsolated {
76-
player.dispose()
76+
player?.dispose()
7777
}
7878
}
7979

8080
func getAudioTracks() async throws -> [MediaSelectionAudioTrackData] {
81-
return try await player.getAudioTracks()
81+
return try await player?.getAudioTracks() ?? []
8282
}
8383

8484
func selectAudioTrack(atIndex trackIndex: Int64) async throws {
85-
return try await player.selectAudioTrack(atIndex: trackIndex)
85+
try await player?.selectAudioTrack(atIndex: trackIndex)
8686
}
8787

8888
func getVideoTracks() async throws -> NativeVideoTrackData {
89-
return try await player.getVideoTracks()
89+
return try await player?.getVideoTracks() ?? NativeVideoTrackData()
9090
}
9191

9292
func selectVideoTrack(withBitrate bitrate: Int64) throws {
9393
MainActor.assumeIsolated {
94-
player.selectVideoTrack(withBitrate: bitrate)
94+
player?.selectVideoTrack(withBitrate: bitrate)
9595
}
9696
}
9797

9898
func setPreventsDisplaySleepDuringVideoPlayback(_ preventsDisplaySleepDuringVideoPlayback: Bool)
9999
throws
100100
{
101101
MainActor.assumeIsolated {
102-
player.setPreventsDisplaySleepDuringVideoPlayback(preventsDisplaySleepDuringVideoPlayback)
102+
player?.setPreventsDisplaySleepDuringVideoPlayback(preventsDisplaySleepDuringVideoPlayback)
103103
}
104104
}
105105

‎packages/video_player/video_player_avfoundation/pubspec.yaml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ name: video_player_avfoundation
22
description: iOS and macOS implementation of the video_player plugin.
33
repository: https://github.com/flutter/packages/tree/main/packages/video_player/video_player_avfoundation
44
issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+video_player%22
5-
version: 2.12.2
5+
version: 2.12.3
66

77
environment:
88
sdk: ^3.13.0

0 commit comments

Comments
 (0)