1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
/* automatically generated by rust-bindgen 0.72.1 */
/* Run 'cargo run -p codegen' to regenerate */
#[repr(C)]
#[derive(Debug, Copy, Clone)]
pub struct MpvHandle {
_unused: [u8; 0],
}
/** Callback function type for mpv events.
@param event A JSON string representing the event.
@param userdata The user-supplied pointer passed to `mpv_wrapper_create`.*/
pub type EventCallback = ::std::option::Option<
unsafe extern "C" fn(
event: *const ::std::os::raw::c_char,
userdata: *mut ::std::os::raw::c_void,
),
>;
pub struct LibmpvWrapper {
__library: ::libloading::Library,
pub mpv_wrapper_create: unsafe extern "C" fn(
initial_options: *const ::std::os::raw::c_char,
observed_properties: *const ::std::os::raw::c_char,
event_callback: EventCallback,
event_userdata: *mut ::std::os::raw::c_void,
) -> *mut MpvHandle,
pub mpv_wrapper_destroy: unsafe extern "C" fn(handle: *mut MpvHandle),
pub mpv_wrapper_command: unsafe extern "C" fn(
handle: *mut MpvHandle,
name: *const ::std::os::raw::c_char,
args: *const ::std::os::raw::c_char,
) -> *mut ::std::os::raw::c_char,
pub mpv_wrapper_set_property: unsafe extern "C" fn(
handle: *mut MpvHandle,
name: *const ::std::os::raw::c_char,
value: *const ::std::os::raw::c_char,
) -> *mut ::std::os::raw::c_char,
pub mpv_wrapper_get_property: unsafe extern "C" fn(
handle: *mut MpvHandle,
name: *const ::std::os::raw::c_char,
format: *const ::std::os::raw::c_char,
) -> *mut ::std::os::raw::c_char,
pub mpv_wrapper_free: unsafe extern "C" fn(s: *mut ::std::os::raw::c_char),
}
impl LibmpvWrapper {
pub unsafe fn new<P>(path: P) -> Result<Self, ::libloading::Error>
where
P: AsRef<::std::ffi::OsStr>,
{
let library = unsafe { ::libloading::Library::new(path) }?;
unsafe { Self::from_library(library) }
}
pub unsafe fn from_library<L>(library: L) -> Result<Self, ::libloading::Error>
where
L: Into<::libloading::Library>,
{
let __library = library.into();
let mpv_wrapper_create = unsafe { __library.get(b"mpv_wrapper_create\0") }
.map(|sym| *sym)?;
let mpv_wrapper_destroy = unsafe { __library.get(b"mpv_wrapper_destroy\0") }
.map(|sym| *sym)?;
let mpv_wrapper_command = unsafe { __library.get(b"mpv_wrapper_command\0") }
.map(|sym| *sym)?;
let mpv_wrapper_set_property = unsafe {
__library.get(b"mpv_wrapper_set_property\0")
}
.map(|sym| *sym)?;
let mpv_wrapper_get_property = unsafe {
__library.get(b"mpv_wrapper_get_property\0")
}
.map(|sym| *sym)?;
let mpv_wrapper_free = unsafe { __library.get(b"mpv_wrapper_free\0") }
.map(|sym| *sym)?;
Ok(LibmpvWrapper {
__library,
mpv_wrapper_create,
mpv_wrapper_destroy,
mpv_wrapper_command,
mpv_wrapper_set_property,
mpv_wrapper_get_property,
mpv_wrapper_free,
})
}
/** Creates a new mpv handle.
@param initial_options A JSON string of initial mpv options (e.g., `{"idle": "yes"}`).
@param observed_properties A JSON string mapping property names to their formats (e.g., `{"pause": "flag"}`).
The format can be "string", "flag", "int64", "double", or "node".
@param event_callback A function pointer that will be called for mpv events.
@param event_userdata A user-supplied pointer that will be passed to the event_callback.
@return A pointer to the opaque mpv handle, or NULL on failure.*/
pub unsafe fn mpv_wrapper_create(
&self,
initial_options: *const ::std::os::raw::c_char,
observed_properties: *const ::std::os::raw::c_char,
event_callback: EventCallback,
event_userdata: *mut ::std::os::raw::c_void,
) -> *mut MpvHandle {
unsafe {
(self
.mpv_wrapper_create)(
initial_options,
observed_properties,
event_callback,
event_userdata,
)
}
}
/** Destroys the mpv handle and terminates the mpv core.
@param handle A valid pointer to the mpv handle (obtained from `mpv_wrapper_create`).*/
pub unsafe fn mpv_wrapper_destroy(&self, handle: *mut MpvHandle) {
unsafe { (self.mpv_wrapper_destroy)(handle) }
}
/** Executes an mpv command.
@param handle A valid pointer to the mpv handle.
@param name The name of the command (e.g., "set", "loadfile").
@param args A JSON string representing an array of arguments (e.g., `["volume", "50"]`, `["path/to/video.mp4"]`).
Pass an empty string "[]" or NULL for no arguments.
@return A JSON string representing the command result (e.g., `{"data": null}` or `{"error": "..."}`).
The caller MUST free this string using `mpv_wrapper_free_string`.*/
pub unsafe fn mpv_wrapper_command(
&self,
handle: *mut MpvHandle,
name: *const ::std::os::raw::c_char,
args: *const ::std::os::raw::c_char,
) -> *mut ::std::os::raw::c_char {
unsafe { (self.mpv_wrapper_command)(handle, name, args) }
}
/** Sets an mpv property.
@param handle A valid pointer to the mpv handle.
@param name The name of the property to set (e.g., "pause").
@param value A JSON string representing the value (e.g., "true").
@return A JSON string indicating success or failure.
The caller MUST free this string using `mpv_wrapper_free_string`.*/
pub unsafe fn mpv_wrapper_set_property(
&self,
handle: *mut MpvHandle,
name: *const ::std::os::raw::c_char,
value: *const ::std::os::raw::c_char,
) -> *mut ::std::os::raw::c_char {
unsafe { (self.mpv_wrapper_set_property)(handle, name, value) }
}
/** Gets an mpv property.
@param handle A valid pointer to the mpv handle.
@param name The name of the property to get.
@param format The format can be "string", "flag", "int64", "double", or "node".
@return A JSON string containing the property value (e.g., `{"data": true}`) or an error.
The caller MUST free this string using `mpv_wrapper_free_string`.*/
pub unsafe fn mpv_wrapper_get_property(
&self,
handle: *mut MpvHandle,
name: *const ::std::os::raw::c_char,
format: *const ::std::os::raw::c_char,
) -> *mut ::std::os::raw::c_char {
unsafe { (self.mpv_wrapper_get_property)(handle, name, format) }
}
/** Frees a C string that was returned by one of the `mpv_wrapper_*` functions.
@param s A pointer to the C string to be freed.*/
pub unsafe fn mpv_wrapper_free(&self, s: *mut ::std::os::raw::c_char) {
unsafe { (self.mpv_wrapper_free)(s) }
}
}