Skip to content

Commit fef0f54

Browse files
committed
chore: update demo
1 parent d92a235 commit fef0f54

File tree

5 files changed

+62
-12
lines changed

5 files changed

+62
-12
lines changed

.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,4 +118,6 @@ temp/
118118
dist/
119119
*.log
120120
*.xcframework
121-
Package.resolved
121+
Package.resolved
122+
buildServer.json
123+
.vscode

Demo/Demo-iOS/Demo-iOS/Player/OpenGL/MPVViewController.swift

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,24 @@ import Foundation
22
import GLKit
33
import Libmpv
44

5+
/**
6+
Warning: OpenGL rendering can not play 10bit videos correctly on iOS/tvOS.
7+
https://github.com/mpv-player/mpv/issues/7846
8+
*/
59
class MPVViewController: GLKViewController {
610
var mpv: OpaquePointer!
711
var mpvGL: OpaquePointer!
812
var playDelegate: MPVPlayerDelegate?
913
var queue: DispatchQueue = DispatchQueue(label: "mpv", qos: .userInteractive)
1014
private var defaultFBO: GLint = -1
1115
var playUrl: URL?
12-
16+
var isSimulator: Bool {
17+
#if targetEnvironment(simulator)
18+
return true
19+
#else
20+
return false
21+
#endif
22+
}
1323

1424
override func viewDidLoad() {
1525
super.viewDidLoad()
@@ -55,7 +65,7 @@ class MPVViewController: GLKViewController {
5565
#endif
5666
checkError(mpv_set_option_string(mpv, "subs-match-os-language", "yes"))
5767
checkError(mpv_set_option_string(mpv, "subs-fallback", "yes"))
58-
checkError(mpv_set_option_string(mpv, "hwdec", machine == "x86_64" ? "no" : "auto-safe"))
68+
checkError(mpv_set_option_string(mpv, "hwdec", isSimulator ? "no" : "videotoolbox"))
5969
checkError(mpv_set_option_string(mpv, "vo", "libmpv"))
6070

6171
checkError(mpv_initialize(mpv))
@@ -176,7 +186,9 @@ class MPVViewController: GLKViewController {
176186

177187

178188
func readEvents() {
179-
queue.async { [self] in
189+
queue.async { [weak self] in
190+
guard let self else { return }
191+
180192
while self.mpv != nil {
181193
let event = mpv_wait_event(self.mpv, 0)
182194
if event!.pointee.event_id == MPV_EVENT_NONE {

Demo/Demo-macOS/Demo-macOS/Player/Metal/MPVMetalViewController.swift

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -114,12 +114,12 @@ final class MPVMetalViewController: NSViewController {
114114
mpv_observe_property(mpv, 0, MPVProperty.videoParamsColormatrix, MPV_FORMAT_STRING)
115115
mpv_observe_property(mpv, 0, MPVProperty.pausedForCache, MPV_FORMAT_FLAG)
116116
mpv_set_wakeup_callback(self.mpv, { (ctx) in
117-
let client = unsafeBitCast(ctx, to: MPVMetalViewController.self)
118-
client.readEvents()
119-
}, UnsafeMutableRawPointer(Unmanaged.passUnretained(self).toOpaque()))
117+
guard let client = ctx else { return }
118+
let viewController = Unmanaged<MPVMetalViewController>.fromOpaque(client).takeUnretainedValue()
119+
viewController.readEvents()
120+
}, Unmanaged.passRetained(self).toOpaque())
120121
}
121122

122-
123123
func loadFile(
124124
_ url: URL,
125125
time: Double? = nil
@@ -214,7 +214,9 @@ final class MPVMetalViewController: NSViewController {
214214
}
215215

216216
func readEvents() {
217-
queue.async { [self] in
217+
queue.async { [weak self] in
218+
guard let self else { return }
219+
218220
while self.mpv != nil {
219221
let event = mpv_wait_event(self.mpv, 0)
220222
if event?.pointee.event_id == MPV_EVENT_NONE {
@@ -261,6 +263,24 @@ final class MPVMetalViewController: NSViewController {
261263
}
262264
}
263265

266+
deinit {
267+
NotificationCenter.default.removeObserver(self)
268+
269+
if mpv != nil {
270+
mpv_set_wakeup_callback(mpv, nil, nil)
271+
272+
// Wait for any pending queue operations to complete
273+
queue.sync {
274+
if self.mpv != nil {
275+
mpv_terminate_destroy(self.mpv)
276+
self.mpv = nil
277+
}
278+
}
279+
280+
// Release the retained self from wakeup callback
281+
Unmanaged.passUnretained(self).release()
282+
}
283+
}
264284

265285
private func checkError(_ status: CInt) {
266286
if status < 0 {

Demo/Demo-macOS/Demo-macOS/Player/OpenGL/MPVOGLView.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,9 @@ final class MPVOGLView: NSOpenGLView {
172172

173173

174174
func readEvents() {
175-
queue.async { [self] in
175+
queue.async { [weak self] in
176+
guard let self else { return }
177+
176178
while self.mpv != nil {
177179
let event = mpv_wait_event(self.mpv, 0)
178180
if event!.pointee.event_id == MPV_EVENT_NONE {

Demo/Demo-tvOS/Demo-tvOS/Player/OpenGL/MPVViewController.swift

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@ import Foundation
22
import GLKit
33
import Libmpv
44

5+
/**
6+
Warning: OpenGL rendering can not play 10bit videos correctly on iOS/tvOS.
7+
https://github.com/mpv-player/mpv/issues/7846
8+
*/
59
class MPVViewController: GLKViewController {
610
var mpv: OpaquePointer!
711
var mpvGL: OpaquePointer!
@@ -10,6 +14,14 @@ class MPVViewController: GLKViewController {
1014
private var defaultFBO: GLint = -1
1115
var playUrl: URL?
1216

17+
var isSimulator: Bool {
18+
#if targetEnvironment(simulator)
19+
return true
20+
#else
21+
return false
22+
#endif
23+
}
24+
1325

1426
override func viewDidLoad() {
1527
super.viewDidLoad()
@@ -55,7 +67,7 @@ class MPVViewController: GLKViewController {
5567
#endif
5668
checkError(mpv_set_option_string(mpv, "subs-match-os-language", "yes"))
5769
checkError(mpv_set_option_string(mpv, "subs-fallback", "yes"))
58-
checkError(mpv_set_option_string(mpv, "hwdec", machine == "x86_64" ? "no" : "auto-safe"))
70+
checkError(mpv_set_option_string(mpv, "hwdec", isSimulator ? "no" : "videotoolbox"))
5971
checkError(mpv_set_option_string(mpv, "vo", "libmpv"))
6072

6173
checkError(mpv_initialize(mpv))
@@ -176,7 +188,9 @@ class MPVViewController: GLKViewController {
176188

177189

178190
func readEvents() {
179-
queue.async { [self] in
191+
queue.async { [weak self] in
192+
guard let self else { return }
193+
180194
while self.mpv != nil {
181195
let event = mpv_wait_event(self.mpv, 0)
182196
if event!.pointee.event_id == MPV_EVENT_NONE {

0 commit comments

Comments
 (0)