Skip to content

Commit 2ef3d18

Browse files
authored
[video_player] Convert player classes to Swift (flutter#13197)
Converts `VideoPlayer` and `TextureBasedVideoPlayer` to Swift. Also converts `AVAssetTrackUtils` since it simplifies calling from Swift, and that's a very simple conversion, as well as the `AV*` class wrappers, in order to replace deprecated methods with newer, Swift-friendlier alternatives. As part of the conversion, adopts Swift Pigeon for the instance-level APIs, so that all of the Pigeon generation in Swift-based (and combines the Pigeon definition files now that they can share a single config). Also adopts Pigeon's new `async` support to make handling async code simpler, so does require some test changes for the API surface change. Intentionally left to later PRs, to limit scope: - The remaining utility files in the `video_player_avfoundation_objc` subtarget (although these should be relatively simple) - The code in `video_player_avfoundation_ios` and `video_player_avfoundation_macos` - Switching the manual event channel code in FVPEventBridge to Pigeon's Swift event channel support As part of the conversion, the minimum Flutter version has been changed to 3.47, which allows dropping versions of iOS and macOS that are no longer supported in 3.47, and thus avoiding the need to convert legacy codepaths that only existing for those no-longer-supported versions. Updates CI's iOS version to target for deprecation checks to iOS 15, rather than 14, to allow using things that require iOS 15 without failing that check (which overrides the deployment version, but is intended to *raise* it, not lower it). Also replaces a number of methods that are deprecated in iOS 16, but for which the replacements were added in iOS 15. Part of flutter#119105 ## 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 18f9c64 commit 2ef3d18

40 files changed

Lines changed: 3097 additions & 3955 deletions

‎packages/video_player/video_player/CHANGELOG.md‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
## NEXT
2+
3+
* Updates README to reflect currently supported OS versions for the latest
4+
versions of the endorsed iOS and macOS platform implementations.
5+
* Applications built with older versions of Flutter will continue to
6+
use compatible versions of the platform implementations.
7+
18
## 2.14.1
29

310
* Fixes late position updates after a controller is disposed.

‎packages/video_player/video_player/README.md‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@
66

77
A Flutter plugin for iOS, Android and Web for playing back video on a Widget surface.
88

9-
| | Android | iOS | macOS | Web |
10-
|-------------|---------|-------|--------|-------|
11-
| **Support** | SDK 24+ | 13.0+ | 10.15+ | Any\* |
9+
| | Android | iOS | macOS | Web |
10+
|-------------|---------|-------|-------|-------|
11+
| **Support** | SDK 24+ | 15.0+ | 12.0+ | Any\* |
1212

1313
![The example app running in iOS](https://github.com/flutter/packages/blob/main/packages/video_player/video_player/doc/demo_ipod.gif?raw=true)
1414

‎packages/video_player/video_player_avfoundation/CHANGELOG.md‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
## 2.12.2
2+
3+
* Converts native player implementation to Swift.
4+
* Updates minimum supported SDK version to Flutter 3.47/Dart 3.13.
5+
16
## 2.12.1
27

38
* Fixes a crash when a video track reports a zero `minFrameDuration`.

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

Lines changed: 46 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -37,47 +37,45 @@ import video_player_avfoundation_objc
3737
}
3838
}
3939

40-
final class TestAsset: NSObject, FVPAVAsset {
41-
let duration: CMTime
42-
let tracks: [AVAssetTrack]?
40+
final class TestAsset: WrappedAVAsset {
41+
private let duration: CMTime
42+
private let tracks: [AVAssetTrack]
43+
var continuation: CheckedContinuation<Void, Never>?
4344

44-
var loadedTracksAsynchronously = false
45+
var loadedValue = false
4546

46-
init(duration: CMTime = CMTime.zero, tracks: [AVAssetTrack]? = nil) {
47+
init(
48+
duration: CMTime = CMTime.zero,
49+
tracks: [AVAssetTrack] = [],
50+
continuation: CheckedContinuation<Void, Never>? = nil
51+
) {
4752
self.duration = duration
4853
self.tracks = tracks
49-
super.init()
50-
}
51-
52-
func statusOfValue(forKey key: String, error outError: NSErrorPointer) -> AVKeyValueStatus {
53-
return tracks == nil ? .loading : .loaded
54-
}
55-
56-
func loadValuesAsynchronously(forKeys keys: [String], completionHandler handler: (() -> Void)?) {
57-
handler?()
54+
self.continuation = continuation
5855
}
5956

60-
@available(macOS 12.0, iOS 15.0, *)
61-
func loadTracks(
62-
withMediaType mediaType: AVMediaType,
63-
completionHandler: @escaping ([AVAssetTrack]?, Error?) -> Void
64-
) {
65-
loadedTracksAsynchronously = true
66-
completionHandler(tracks, nil)
67-
}
68-
69-
func tracks(withMediaType mediaType: AVMediaType) -> [AVAssetTrack] {
70-
return tracks ?? []
57+
func load<T>(_ property: AVAsyncProperty<AVAsset, T>, isolation: isolated (any Actor)?)
58+
async throws -> T
59+
{
60+
loadedValue = true
61+
continuation?.resume()
62+
continuation = nil
63+
if let tracks = tracks as? T {
64+
return tracks
65+
}
66+
if let duration = duration as? T {
67+
return duration
68+
}
69+
fatalError("Unimplemented property in stub")
7170
}
7271
}
7372

74-
final class StubPlayerItem: NSObject, FVPAVPlayerItem {
75-
let asset: FVPAVAsset
73+
final class StubPlayerItem: WrappedAVPlayerItem {
74+
let asset: WrappedAVAsset
7675
var videoComposition: AVVideoComposition?
7776

78-
init(asset: FVPAVAsset = TestAsset()) {
77+
init(asset: WrappedAVAsset = TestAsset()) {
7978
self.asset = asset
80-
super.init()
8179
}
8280
}
8381

@@ -148,16 +146,15 @@ final class StubAssetProvider: NSObject, FVPAssetProvider {
148146
}
149147
}
150148

151-
final class TestPixelBufferSource: NSObject, FVPPixelBufferSource {
149+
final class TestPixelBufferSource: PixelBufferSource {
152150
var pixelBuffer: CVPixelBuffer?
153151
let videoOutput: AVPlayerItemVideoOutput
154152

155-
override init() {
153+
init() {
156154
videoOutput = AVPlayerItemVideoOutput(pixelBufferAttributes: [
157155
kCVPixelBufferPixelFormatTypeKey as String: kCVPixelFormatType_32BGRA,
158156
kCVPixelBufferIOSurfacePropertiesKey as String: [:] as [String: String],
159157
])
160-
super.init()
161158
}
162159

163160
func itemTime(forHostTime hostTimeInSeconds: CFTimeInterval) -> CMTime {
@@ -180,14 +177,14 @@ final class TestPixelBufferSource: NSObject, FVPPixelBufferSource {
180177
}
181178

182179
#if os(iOS)
183-
final class TestAudioSession: NSObject, FVPAVAudioSession {
180+
final class TestAudioSession: WrappedAVAudioSession {
184181
var category: AVAudioSession.Category = .ambient
185182
var categoryOptions: AVAudioSession.CategoryOptions = []
186183
private(set) var setCategoryCalled = false
187184

188185
func setCategory(
189186
_ category: AVAudioSession.Category,
190-
with options: AVAudioSession.CategoryOptions
187+
options: AVAudioSession.CategoryOptions
191188
) throws {
192189
setCategoryCalled = true
193190
self.category = category
@@ -196,24 +193,25 @@ final class TestPixelBufferSource: NSObject, FVPPixelBufferSource {
196193
}
197194
#endif
198195

199-
final class StubFVPAVFactory: NSObject, FVPAVFactory {
196+
final class StubAVFactory: NSObject, WrappedAVObjectFactory {
200197
let player: AVPlayer
201-
let playerItem: FVPAVPlayerItem
202-
let pixelBufferSource: FVPPixelBufferSource?
198+
let playerItem: WrappedAVPlayerItem
199+
let pixelBufferSource: PixelBufferSource?
203200
private(set) var lastOutputSettings: [String: Any]?
204201
#if os(iOS)
205-
var audioSession: FVPAVAudioSession
202+
var audioSession: WrappedAVAudioSession
206203
#endif
207204

208205
init(
209206
player: AVPlayer? = nil,
210-
playerItem: FVPAVPlayerItem? = nil,
211-
pixelBufferSource: FVPPixelBufferSource? = nil
207+
playerItem: WrappedAVPlayerItem? = nil,
208+
pixelBufferSource: PixelBufferSource? = nil,
209+
assetURL: String? = nil,
212210
) {
213-
let dummyURL = URL(string: "https://flutter.dev")!
211+
let url = URL(string: assetURL ?? "https://flutter.dev")!
214212
self.player =
215213
player
216-
?? AVPlayer(playerItem: AVPlayerItem(url: dummyURL))
214+
?? AVPlayer(playerItem: AVPlayerItem(url: url))
217215
self.playerItem = playerItem ?? StubPlayerItem()
218216
self.pixelBufferSource = pixelBufferSource
219217
#if os(iOS)
@@ -222,25 +220,25 @@ final class StubFVPAVFactory: NSObject, FVPAVFactory {
222220
super.init()
223221
}
224222

225-
func urlAsset(with url: URL, options: [String: Any]?) -> FVPAVAsset {
223+
func urlAsset(with url: URL, options: [String: Any]?) -> WrappedAVAsset {
226224
return playerItem.asset
227225
}
228226

229-
func playerItem(with asset: FVPAVAsset) -> FVPAVPlayerItem {
227+
func playerItem(with asset: WrappedAVAsset) -> WrappedAVPlayerItem {
230228
return playerItem
231229
}
232230

233-
func player(with playerItem: FVPAVPlayerItem) -> AVPlayer {
231+
func player(with playerItem: WrappedAVPlayerItem) -> AVPlayer {
234232
return self.player
235233
}
236234

237-
func videoOutput(outputSettings: [String: Any]) -> FVPPixelBufferSource {
235+
func videoOutput(outputSettings: [String: Any]) -> PixelBufferSource {
238236
lastOutputSettings = outputSettings
239237
return pixelBufferSource ?? TestPixelBufferSource()
240238
}
241239

242240
#if os(iOS)
243-
func sharedAudioSession() -> FVPAVAudioSession {
241+
func sharedAudioSession() -> WrappedAVAudioSession {
244242
return audioSession
245243
}
246244
#endif
@@ -286,7 +284,7 @@ final class StubEventListener: NSObject, FVPVideoEventListener {
286284
}
287285
func videoPlayerDidSetPlaying(_ playing: Bool) {}
288286
func videoPlayerDidStartBuffering() {}
289-
func videoPlayerDidUpdateBufferRegions(_ regions: [[NSNumber]]!) {}
287+
func videoPlayerDidUpdateBufferRegions(_ regions: [[NSNumber]]) {}
290288
func videoPlayerWasDisposed() {}
291289
}
292290

0 commit comments

Comments
 (0)