Skip to content
Permalink
Browse files

Add API wrapper for MPV hooks

  • Loading branch information...
lhc70000 committed Feb 28, 2019
1 parent b6edcf3 commit 4473128783f404cd8e036ab28aa6f37e432264b3
Showing with 46 additions and 0 deletions.
  1. +4 −0 iina.xcodeproj/project.pbxproj
  2. +20 −0 iina/MPVController.swift
  3. +22 −0 iina/MPVHook.swift
@@ -249,6 +249,7 @@
E3B3DD5720FDB8A600325184 /* PreferenceWindowController.xib in Resources */ = {isa = PBXBuildFile; fileRef = E3B3DD5920FDB8A600325184 /* PreferenceWindowController.xib */; };
E3BA79EE2131443A00529D99 /* OpenURLWindowController.swift in Sources */ = {isa = PBXBuildFile; fileRef = E3BA79EC2131443A00529D99 /* OpenURLWindowController.swift */; };
E3C12F6B201F281F00297964 /* FirstRunManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = E3C12F6A201F281F00297964 /* FirstRunManager.swift */; };
E3CB258F222799B800A62C47 /* MPVHook.swift in Sources */ = {isa = PBXBuildFile; fileRef = E3CB258E222799B800A62C47 /* MPVHook.swift */; };
E3CB75BB1FD998F5004DB10A /* Regex.swift in Sources */ = {isa = PBXBuildFile; fileRef = 84A886F21E26CA24008755BB /* Regex.swift */; };
E3CB75BD1FDACB82004DB10A /* SavedFilter.swift in Sources */ = {isa = PBXBuildFile; fileRef = E3CB75BC1FDACB82004DB10A /* SavedFilter.swift */; };
E3DBD239218B057A00B3AFBF /* Translators.json in Resources */ = {isa = PBXBuildFile; fileRef = E3DBD238218B057A00B3AFBF /* Translators.json */; };
@@ -1168,6 +1169,7 @@
E3B3DD5B20FDB8A900325184 /* zh-Hans */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = "zh-Hans"; path = "zh-Hans.lproj/PreferenceWindowController.strings"; sourceTree = "<group>"; };
E3BA79EC2131443A00529D99 /* OpenURLWindowController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = OpenURLWindowController.swift; sourceTree = "<group>"; };
E3C12F6A201F281F00297964 /* FirstRunManager.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FirstRunManager.swift; sourceTree = "<group>"; };
E3CB258E222799B800A62C47 /* MPVHook.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = MPVHook.swift; sourceTree = "<group>"; };
E3CB75BC1FDACB82004DB10A /* SavedFilter.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SavedFilter.swift; sourceTree = "<group>"; };
E3DBD238218B057A00B3AFBF /* Translators.json */ = {isa = PBXFileReference; indentWidth = 4; lastKnownFileType = text.json; path = Translators.json; sourceTree = "<group>"; tabWidth = 4; };
E3DBD23A218B199700B3AFBF /* Translators.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Translators.swift; sourceTree = "<group>"; };
@@ -1598,6 +1600,7 @@
847644071D48B413004F6DF5 /* MPVOption.swift */,
844DE1BE1D3E2B9900272589 /* MPVEvent.swift */,
84F7258E1D486185000DEF1B /* MPVProperty.swift */,
E3CB258E222799B800A62C47 /* MPVHook.swift */,
84EB1F041D2F5C5B004FA5A1 /* AppData.swift */,
84A0BA8F1D2F8D4100BC8DA1 /* IINAError.swift */,
84AABE8A1DBF634600D138FD /* CharEncoding.swift */,
@@ -2319,6 +2322,7 @@
84FBCB381EEACDDD0076C77C /* FFmpegController.m in Sources */,
84879A981E0FFC7E0004F894 /* PrefUIViewController.swift in Sources */,
84C6D3641EB276E9009BF721 /* PlaybackHistory.swift in Sources */,
E3CB258F222799B800A62C47 /* MPVHook.swift in Sources */,
843FFD4D1D5DAA01001F3A44 /* RoundedTextFieldCell.swift in Sources */,
8407D1401E3A684C0043895D /* ViewLayer.swift in Sources */,
84A0BA9B1D2FAB4100BC8DA1 /* Parameter.swift in Sources */,
@@ -54,6 +54,9 @@ class MPVController: NSObject {

var fileLoaded: Bool = false

private var hooks: [UInt64: () -> Void] = [:]
private var hookCounter: UInt64 = 1

let observeProperties: [String: mpv_format] = [
MPVProperty.trackList: MPV_FORMAT_NONE,
MPVProperty.vf: MPV_FORMAT_NONE,
@@ -529,6 +532,14 @@ class MPVController: NSObject {
return parsed!
}

// MARK: - Hooks
func addHook(_ name: MPVHook, priority: Int32 = 0, hook: @escaping () -> Void) {
mpv_hook_add(mpv, hookCounter, name.rawValue, priority)
hooks[hookCounter] = hook
hookCounter += 1
}

// MARK: - Events
// Read event and handle it async
@@ -567,6 +578,15 @@ class MPVController: NSObject {
let text = String(cString: (msg?.pointee.text)!)
Logger.log("mpv log: [\(prefix)] \(level): \(text)", level: .warning, subsystem: .general, appendNewlineAtTheEnd: false)

case MPV_EVENT_HOOK:
let userData = event.pointee.reply_userdata
let hookEvent = event.pointee.data.bindMemory(to: mpv_event_hook.self, capacity: 1).pointee
let hookID = hookEvent.id
if let hook = hooks[userData] {
hook()
}
mpv_hook_continue(mpv, hookID)

case MPV_EVENT_PROPERTY_CHANGE:
let dataOpaquePtr = OpaquePointer(event.pointee.data)
if let property = UnsafePointer<mpv_event_property>(dataOpaquePtr)?.pointee {
@@ -0,0 +1,22 @@
//
// MPVHook.swift
// iina
//
// Created by Collider LI on 28/2/2019.
// Copyright © 2019 lhc. All rights reserved.
//
import Foundation

struct MPVHook: RawRepresentable {
typealias RawValue = String
var rawValue: RawValue

init(_ string: String) { self.rawValue = string }
init?(rawValue: RawValue) { self.rawValue = rawValue }

static let onLoad = MPVHook("on_load")
static let onLoadFail = MPVHook("on_load_fail")
static let onPreLoaded = MPVHook("on_preloaded")
static let onUnLoad = MPVHook("on_unload")
}

0 comments on commit 4473128

Please sign in to comment.
You can’t perform that action at this time.