1
0
mirror of https://github.com/yattee/yattee.git synced 2024-12-15 14:50:32 +05:30
yattee/Model/Player/Backends/MPVClient.swift

337 lines
10 KiB
Swift
Raw Normal View History

2022-02-28 02:01:17 +05:30
import CoreMedia
2022-02-17 01:53:11 +05:30
import Foundation
import Logging
#if !os(macOS)
import Siesta
import UIKit
#endif
final class MPVClient: ObservableObject {
private var logger = Logger(label: "mpv-client")
var mpv: OpaquePointer!
var mpvGL: OpaquePointer!
var queue: DispatchQueue!
2022-02-28 02:01:17 +05:30
#if os(macOS)
var layer: VideoLayer!
var link: CVDisplayLink!
#else
var glView: MPVOGLView!
#endif
2022-02-17 01:53:11 +05:30
var backend: MPVBackend!
2022-02-17 03:21:37 +05:30
var seeking = false
2022-02-28 02:01:17 +05:30
func create(frame: CGRect? = nil) {
#if !os(macOS)
2022-03-20 04:35:09 +05:30
if let frame = frame {
glView = MPVOGLView(frame: frame)
}
2022-02-28 02:01:17 +05:30
#endif
2022-02-17 01:53:11 +05:30
mpv = mpv_create()
if mpv == nil {
print("failed creating context\n")
exit(1)
}
checkError(mpv_request_log_messages(mpv, "warn"))
2022-02-28 02:01:17 +05:30
#if os(macOS)
checkError(mpv_set_option_string(mpv, "input-media-keys", "yes"))
#else
checkError(mpv_set_option_string(mpv, "hwdec", "yes"))
checkError(mpv_set_option_string(mpv, "override-display-fps", "\(UIScreen.main.maximumFramesPerSecond)"))
checkError(mpv_set_option_string(mpv, "video-sync", "display-resample"))
#endif
2022-02-17 01:53:11 +05:30
checkError(mpv_set_option_string(mpv, "vo", "libmpv"))
2022-02-28 02:01:17 +05:30
checkError(mpv_initialize(mpv))
2022-02-17 01:53:11 +05:30
let api = UnsafeMutableRawPointer(mutating: (MPV_RENDER_API_TYPE_OPENGL as NSString).utf8String)
var initParams = mpv_opengl_init_params(
2022-02-28 02:01:17 +05:30
get_proc_address: getProcAddress,
2022-02-17 01:53:11 +05:30
get_proc_address_ctx: nil,
extra_exts: nil
)
2022-02-28 02:01:17 +05:30
queue = DispatchQueue(label: "mpv", qos: .background)
2022-02-17 01:53:11 +05:30
withUnsafeMutablePointer(to: &initParams) { initParams in
var params = [
mpv_render_param(type: MPV_RENDER_PARAM_API_TYPE, data: api),
mpv_render_param(type: MPV_RENDER_PARAM_OPENGL_INIT_PARAMS, data: initParams),
mpv_render_param()
]
if mpv_render_context_create(&mpvGL, mpv, &params) < 0 {
puts("failed to initialize mpv GL context")
exit(1)
}
2022-02-28 02:01:17 +05:30
#if os(macOS)
mpv_render_context_set_update_callback(
mpvGL,
glUpdate,
UnsafeMutableRawPointer(Unmanaged.passUnretained(layer).toOpaque())
)
#else
glView.mpvGL = UnsafeMutableRawPointer(mpvGL)
mpv_render_context_set_update_callback(
mpvGL,
glUpdate(_:),
UnsafeMutableRawPointer(Unmanaged.passUnretained(glView).toOpaque())
)
#endif
2022-02-17 01:53:11 +05:30
}
queue!.async {
mpv_set_wakeup_callback(self.mpv, wakeUp, UnsafeMutableRawPointer(Unmanaged.passUnretained(self).toOpaque()))
}
}
func readEvents() {
queue?.async { [self] in
while self.mpv != nil {
let event = mpv_wait_event(self.mpv, 0)
if event!.pointee.event_id == MPV_EVENT_NONE {
break
}
backend.handle(event)
}
}
}
func loadFile(_ url: URL, audio: URL? = nil, time: CMTime? = nil, completionHandler: ((Int32) -> Void)? = nil) {
2022-02-17 01:53:11 +05:30
var args = [url.absoluteString]
var options = [String]()
2022-02-17 01:53:11 +05:30
if let time = time {
args.append("replace")
options.append("start=\(Int(time.seconds))")
}
if let audioURL = audio?.absoluteString {
options.append("audio-files-append=\"\(audioURL)\"")
2022-02-17 01:53:11 +05:30
}
args.append(options.joined(separator: ","))
2022-02-17 01:53:11 +05:30
command("loadfile", args: args, returnValueCallback: completionHandler)
}
func play() {
setFlagAsync("pause", false)
}
func pause() {
setFlagAsync("pause", true)
}
func togglePlay() {
command("cycle", args: ["pause"])
}
func stop() {
command("stop")
}
var currentTime: CMTime {
CMTime.secondsInDefaultTimescale(mpv.isNil ? -1 : getDouble("time-pos"))
2022-02-17 01:53:11 +05:30
}
var duration: CMTime {
CMTime.secondsInDefaultTimescale(mpv.isNil ? -1 : getDouble("duration"))
2022-02-17 01:53:11 +05:30
}
func seek(relative time: CMTime, completionHandler: ((Bool) -> Void)? = nil) {
2022-02-17 03:21:37 +05:30
guard !seeking else {
logger.warning("ignoring seek, another in progress")
return
}
seeking = true
command("seek", args: [String(time.seconds)]) { [weak self] _ in
self?.seeking = false
2022-02-17 01:53:11 +05:30
completionHandler?(true)
}
}
func seek(to time: CMTime, completionHandler: ((Bool) -> Void)? = nil) {
2022-02-17 03:21:37 +05:30
guard !seeking else {
logger.warning("ignoring seek, another in progress")
return
}
seeking = true
command("seek", args: [String(time.seconds), "absolute"]) { [weak self] _ in
self?.seeking = false
2022-02-17 01:53:11 +05:30
completionHandler?(true)
}
}
func setSize(_ width: Double, _ height: Double) {
2022-03-27 17:12:20 +05:30
let roundedWidth = width.rounded()
let roundedHeight = height.rounded()
guard width > 0, height > 0 else {
return
}
logger.info("setting player size to \(roundedWidth),\(roundedHeight)")
2022-02-17 01:53:11 +05:30
#if !os(macOS)
2022-03-27 17:12:20 +05:30
guard roundedWidth <= UIScreen.main.bounds.width, roundedHeight <= UIScreen.main.bounds.height else {
2022-02-17 01:53:11 +05:30
logger.info("requested size is greater than screen size, ignoring")
2022-03-27 17:12:20 +05:30
logger.info("width: \(roundedWidth) <= \(UIScreen.main.bounds.width)")
logger.info("height: \(roundedHeight) <= \(UIScreen.main.bounds.height)")
2022-02-17 01:53:11 +05:30
return
}
2022-03-27 17:12:20 +05:30
glView?.frame = CGRect(x: 0, y: 0, width: roundedWidth, height: roundedHeight)
2022-02-28 02:01:17 +05:30
#endif
2022-02-17 01:53:11 +05:30
}
func setNeedsDrawing(_ needsDrawing: Bool) {
logger.info("needs drawing: \(needsDrawing)")
2022-02-28 02:01:17 +05:30
#if !os(macOS)
glView.needsDrawing = needsDrawing
#endif
2022-02-17 01:53:11 +05:30
}
func command(
_ command: String,
args: [String?] = [],
checkForErrors: Bool = true,
returnValueCallback: ((Int32) -> Void)? = nil
) {
guard mpv != nil else {
return
}
var cargs = makeCArgs(command, args).map { $0.flatMap { UnsafePointer<CChar>(strdup($0)) } }
defer {
for ptr in cargs where ptr != nil {
free(UnsafeMutablePointer(mutating: ptr!))
}
}
logger.info("\(command) -- \(args)")
let returnValue = mpv_command(mpv, &cargs)
if checkForErrors {
checkError(returnValue)
}
if let cb = returnValueCallback {
cb(returnValue)
}
}
2022-06-08 02:50:24 +05:30
func addVideoTrack(_ url: URL) {
command("video-add", args: [url.absoluteString])
}
func setVideoToAuto() {
2022-06-08 03:35:30 +05:30
setString("video", "1")
2022-06-08 02:50:24 +05:30
}
func setVideoToNo() {
setString("video", "no")
}
var tracksCount: Int {
Int(getString("track-list/count") ?? "-1") ?? -1
}
2022-02-17 01:53:11 +05:30
private func setFlagAsync(_ name: String, _ flag: Bool) {
var data: Int = flag ? 1 : 0
mpv_set_property_async(mpv, 0, name, MPV_FORMAT_FLAG, &data)
}
2022-04-17 02:20:37 +05:30
func setDoubleAsync(_ name: String, _ value: Double) {
var data = value
mpv_set_property_async(mpv, 0, name, MPV_FORMAT_DOUBLE, &data)
}
2022-02-17 01:53:11 +05:30
private func getDouble(_ name: String) -> Double {
var data = Double()
mpv_get_property(mpv, name, MPV_FORMAT_DOUBLE, &data)
return data
}
2022-06-08 02:50:24 +05:30
private func getInt(_ name: String) -> Int {
var data = Int64()
mpv_get_property(mpv, name, MPV_FORMAT_INT64, &data)
return Int(data)
}
func getString(_ name: String) -> String? {
let cstr = mpv_get_property_string(mpv, name)
let str: String? = cstr == nil ? nil : String(cString: cstr!)
mpv_free(cstr)
return str
}
private func setString(_ name: String, _ value: String) {
mpv_set_property_string(mpv, name, value)
}
2022-02-17 01:53:11 +05:30
private func makeCArgs(_ command: String, _ args: [String?]) -> [String?] {
if !args.isEmpty, args.last == nil {
fatalError("Command do not need a nil suffix")
}
var strArgs = args
strArgs.insert(command, at: 0)
strArgs.append(nil)
return strArgs
}
func checkError(_ status: CInt) {
if status < 0 {
logger.error(.init(stringLiteral: "MPV API error: \(String(cString: mpv_error_string(status)))\n"))
}
}
}
2022-02-28 02:01:17 +05:30
#if os(macOS)
func getProcAddress(_: UnsafeMutableRawPointer?, _ name: UnsafePointer<Int8>?) -> UnsafeMutableRawPointer? {
let symbolName = CFStringCreateWithCString(kCFAllocatorDefault, name, CFStringBuiltInEncodings.ASCII.rawValue)
let identifier = CFBundleGetBundleWithIdentifier("com.apple.opengl" as CFString)
2022-02-17 01:53:11 +05:30
2022-02-28 02:01:17 +05:30
return CFBundleGetFunctionPointerForName(identifier, symbolName)
}
2022-02-17 01:53:11 +05:30
2022-02-28 02:01:17 +05:30
func glUpdate(_ ctx: UnsafeMutableRawPointer?) {
let videoLayer = unsafeBitCast(ctx, to: VideoLayer.self)
2022-02-17 01:53:11 +05:30
2022-02-28 02:01:17 +05:30
videoLayer.client?.queue?.async {
if !videoLayer.isAsynchronous {
videoLayer.display()
}
}
2022-02-17 01:53:11 +05:30
}
2022-02-28 02:01:17 +05:30
#else
func getProcAddress(_: UnsafeMutableRawPointer?, _ name: UnsafePointer<Int8>?) -> UnsafeMutableRawPointer? {
let symbolName = CFStringCreateWithCString(kCFAllocatorDefault, name, CFStringBuiltInEncodings.ASCII.rawValue)
let identifier = CFBundleGetBundleWithIdentifier("com.apple.opengles" as CFString)
2022-02-17 01:53:11 +05:30
2022-02-28 02:01:17 +05:30
return CFBundleGetFunctionPointerForName(identifier, symbolName)
2022-02-17 01:53:11 +05:30
}
2022-02-28 02:01:17 +05:30
private func glUpdate(_ ctx: UnsafeMutableRawPointer?) {
let glView = unsafeBitCast(ctx, to: MPVOGLView.self)
guard glView.needsDrawing else {
return
}
DispatchQueue.main.async {
glView.setNeedsDisplay()
}
}
#endif
2022-02-17 01:53:11 +05:30
private func wakeUp(_ context: UnsafeMutableRawPointer?) {
let client = unsafeBitCast(context, to: MPVClient.self)
client.readEvents()
}