diff options
Diffstat (limited to '')
-rw-r--r-- | osdep/mac/libmpv_helper.swift (renamed from osdep/macos/libmpv_helper.swift) | 119 |
1 files changed, 25 insertions, 94 deletions
diff --git a/osdep/macos/libmpv_helper.swift b/osdep/mac/libmpv_helper.swift index 8b1c697..23953eb 100644 --- a/osdep/macos/libmpv_helper.swift +++ b/osdep/mac/libmpv_helper.swift @@ -23,27 +23,14 @@ let glDummy: @convention(c) () -> Void = {} class LibmpvHelper { var log: LogHelper - var mpvHandle: OpaquePointer? + var mpv: OpaquePointer? var mpvRenderContext: OpaquePointer? - var macOptsPtr: UnsafeMutableRawPointer? - var macOpts: macos_opts = macos_opts() var fbo: GLint = 1 - let deinitLock = NSLock() - - init(_ mpv: OpaquePointer, _ mpLog: OpaquePointer?) { - mpvHandle = mpv - log = LogHelper(mpLog) - - guard let app = NSApp as? Application, - let ptr = mp_get_config_group(nil, - mp_client_get_global(mpvHandle), - app.getMacOSConf()) else - { - log.sendError("macOS config group couldn't be retrieved'") - exit(1) - } - macOptsPtr = ptr - macOpts = UnsafeMutablePointer<macos_opts>(OpaquePointer(ptr)).pointee + let uninitLock = NSLock() + + init(_ mpv: OpaquePointer, _ log: LogHelper) { + self.mpv = mpv + self.log = log } func initRender() { @@ -52,7 +39,7 @@ class LibmpvHelper { let pAddress = mpv_opengl_init_params(get_proc_address: getProcAddress, get_proc_address_ctx: nil) - MPVHelper.withUnsafeMutableRawPointers([pAddress, advanced]) { (pointers: [UnsafeMutableRawPointer?]) in + TypeHelper.withUnsafeMutableRawPointers([pAddress, advanced]) { (pointers: [UnsafeMutableRawPointer?]) in var params: [mpv_render_param] = [ mpv_render_param(type: MPV_RENDER_PARAM_API_TYPE, data: api), mpv_render_param(type: MPV_RENDER_PARAM_OPENGL_INIT_PARAMS, data: pointers[0]), @@ -60,12 +47,11 @@ class LibmpvHelper { mpv_render_param() ] - if (mpv_render_context_create(&mpvRenderContext, mpvHandle, ¶ms) < 0) { - log.sendError("Render context init has failed.") + if (mpv_render_context_create(&mpvRenderContext, mpv, ¶ms) < 0) { + log.error("Render context init has failed.") exit(1) } } - } let getProcAddress: (@convention(c) (UnsafeMutableRawPointer?, UnsafePointer<Int8>?) @@ -87,17 +73,17 @@ class LibmpvHelper { func setRenderUpdateCallback(_ callback: @escaping mpv_render_update_fn, context object: AnyObject) { if mpvRenderContext == nil { - log.sendWarning("Init mpv render context first.") + log.warning("Init mpv render context first.") } else { - mpv_render_context_set_update_callback(mpvRenderContext, callback, MPVHelper.bridge(obj: object)) + mpv_render_context_set_update_callback(mpvRenderContext, callback, TypeHelper.bridge(obj: object)) } } func setRenderControlCallback(_ callback: @escaping mp_render_cb_control_fn, context object: AnyObject) { if mpvRenderContext == nil { - log.sendWarning("Init mpv render context first.") + log.warning("Init mpv render context first.") } else { - mp_render_context_set_control_callback(mpvRenderContext, callback, MPVHelper.bridge(obj: object)) + mp_render_context_set_control_callback(mpvRenderContext, callback, TypeHelper.bridge(obj: object)) } } @@ -107,18 +93,18 @@ class LibmpvHelper { } func isRenderUpdateFrame() -> Bool { - deinitLock.lock() + uninitLock.lock() if mpvRenderContext == nil { - deinitLock.unlock() + uninitLock.unlock() return false } let flags: UInt64 = mpv_render_context_update(mpvRenderContext) - deinitLock.unlock() + uninitLock.unlock() return flags & UInt64(MPV_RENDER_UPDATE_FRAME.rawValue) > 0 } func drawRender(_ surface: NSSize, _ depth: GLint, _ ctx: CGLContextObj, skip: Bool = false) { - deinitLock.lock() + uninitLock.lock() if mpvRenderContext != nil { var i: GLint = 0 let flip: CInt = 1 @@ -134,7 +120,7 @@ class LibmpvHelper { h: Int32(surface.height), internal_format: 0) - MPVHelper.withUnsafeMutableRawPointers([data, flip, ditherDepth, skip]) { (pointers: [UnsafeMutableRawPointer?]) in + TypeHelper.withUnsafeMutableRawPointers([data, flip, ditherDepth, skip]) { (pointers: [UnsafeMutableRawPointer?]) in var params: [mpv_render_param] = [ mpv_render_param(type: MPV_RENDER_PARAM_OPENGL_FBO, data: pointers[0]), mpv_render_param(type: MPV_RENDER_PARAM_FLIP_Y, data: pointers[1]), @@ -151,13 +137,13 @@ class LibmpvHelper { if !skip { CGLFlushDrawable(ctx) } - deinitLock.unlock() + uninitLock.unlock() } func setRenderICCProfile(_ profile: NSColorSpace) { if mpvRenderContext == nil { return } guard var iccData = profile.iccProfileData else { - log.sendWarning("Invalid ICC profile data.") + log.warning("Invalid ICC profile data.") return } iccData.withUnsafeMutableBytes { (ptr: UnsafeMutableRawBufferPointer) in @@ -182,69 +168,14 @@ class LibmpvHelper { } } - func commandAsync(_ cmd: [String?], id: UInt64 = 1) { - if mpvHandle == nil { return } - var mCmd = cmd - mCmd.append(nil) - var cargs = mCmd.map { $0.flatMap { UnsafePointer<Int8>(strdup($0)) } } - mpv_command_async(mpvHandle, id, &cargs) - for ptr in cargs { free(UnsafeMutablePointer(mutating: ptr)) } - } - - // Unsafe function when called while using the render API - func command(_ cmd: String) { - if mpvHandle == nil { return } - mpv_command_string(mpvHandle, cmd) - } - - func getBoolProperty(_ name: String) -> Bool { - if mpvHandle == nil { return false } - var value = Int32() - mpv_get_property(mpvHandle, name, MPV_FORMAT_FLAG, &value) - return value > 0 - } - - func getIntProperty(_ name: String) -> Int { - if mpvHandle == nil { return 0 } - var value = Int64() - mpv_get_property(mpvHandle, name, MPV_FORMAT_INT64, &value) - return Int(value) - } - - func getStringProperty(_ name: String) -> String? { - guard let mpv = mpvHandle else { return nil } - guard let value = mpv_get_property_string(mpv, name) else { return nil } - let str = String(cString: value) - mpv_free(value) - return str - } - - func deinitRender() { + func uninit() { mpv_render_context_set_update_callback(mpvRenderContext, nil, nil) mp_render_context_set_control_callback(mpvRenderContext, nil, nil) - deinitLock.lock() + uninitLock.lock() mpv_render_context_free(mpvRenderContext) mpvRenderContext = nil - deinitLock.unlock() - } - - func deinitMPV(_ destroy: Bool = false) { - if destroy { - mpv_destroy(mpvHandle) - } - ta_free(macOptsPtr) - macOptsPtr = nil - mpvHandle = nil - } - - // *(char **) MPV_FORMAT_STRING on mpv_event_property - class func mpvStringArrayToString(_ obj: UnsafeMutableRawPointer) -> String? { - let cstr = UnsafeMutablePointer<UnsafeMutablePointer<Int8>>(OpaquePointer(obj)) - return String(cString: cstr[0]) - } - - // MPV_FORMAT_FLAG - class func mpvFlagToBool(_ obj: UnsafeMutableRawPointer) -> Bool? { - return UnsafePointer<Bool>(OpaquePointer(obj))?.pointee + mpv_destroy(mpv) + mpv = nil + uninitLock.unlock() } } |