2024-10-30 07:41:17 +00:00
|
|
|
mod ffi;
|
|
|
|
|
2024-11-02 20:11:18 +00:00
|
|
|
use std::ffi::{c_char, c_int, c_void, CStr, CString};
|
2024-11-02 14:43:43 +00:00
|
|
|
|
2024-10-30 07:44:25 +00:00
|
|
|
#[link(name = "mpv")]
|
|
|
|
extern "C" {}
|
2024-10-30 07:41:17 +00:00
|
|
|
|
2024-11-02 14:43:43 +00:00
|
|
|
pub struct Error(c_int);
|
2024-10-30 07:41:17 +00:00
|
|
|
|
|
|
|
impl Error {
|
2024-11-02 14:43:43 +00:00
|
|
|
fn from_return_code(rc: c_int) -> Result<(), Self> {
|
2024-10-30 07:41:17 +00:00
|
|
|
if rc == 0 {
|
|
|
|
Ok(())
|
|
|
|
} else {
|
|
|
|
Err(Self(rc))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-11-02 15:21:13 +00:00
|
|
|
use std::fmt;
|
|
|
|
|
|
|
|
impl fmt::Display for Error {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> {
|
|
|
|
fmt::Display::fmt(
|
2024-11-02 16:05:26 +00:00
|
|
|
unsafe { CStr::from_ptr(ffi::mpv_error_string(self.0 as c_int)) }
|
2024-11-02 15:21:13 +00:00
|
|
|
.to_str()
|
|
|
|
.unwrap(),
|
|
|
|
f,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl fmt::Debug for Error {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> {
|
2024-10-30 07:41:17 +00:00
|
|
|
f.debug_tuple("Error")
|
2024-11-02 15:21:13 +00:00
|
|
|
.field(
|
2024-11-02 16:05:26 +00:00
|
|
|
&unsafe { CStr::from_ptr(ffi::mpv_error_string(self.0 as c_int)) }
|
2024-11-02 15:21:13 +00:00
|
|
|
.to_str()
|
|
|
|
.unwrap(),
|
|
|
|
)
|
2024-10-30 07:41:17 +00:00
|
|
|
.finish()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-11-02 15:21:13 +00:00
|
|
|
impl std::error::Error for Error {}
|
|
|
|
|
2024-11-02 19:57:12 +00:00
|
|
|
use event_listener::{Event, EventListener, IntoNotification};
|
2024-11-02 16:29:53 +00:00
|
|
|
use std::cell::RefCell;
|
2024-11-02 14:43:43 +00:00
|
|
|
use std::pin::Pin;
|
2024-10-30 07:41:17 +00:00
|
|
|
use std::ptr::NonNull;
|
|
|
|
|
2024-11-02 14:43:43 +00:00
|
|
|
pub struct Handle {
|
|
|
|
inner: NonNull<ffi::mpv_handle>,
|
2024-11-02 15:50:10 +00:00
|
|
|
wakeup: Pin<Box<Event>>, // the wakeup callback holds a pointer to this
|
2024-11-02 16:29:53 +00:00
|
|
|
wait_event_cell: RefCell<()>,
|
2024-11-02 14:43:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// The client API is generally fully thread-safe, unless otherwise noted.
|
2024-11-02 15:22:57 +00:00
|
|
|
//unsafe impl Send for Handle {}
|
|
|
|
//unsafe impl Sync for Handle {}
|
|
|
|
// ........but we'd rather keep this in a single thread to play better with gtk
|
2024-11-02 14:43:43 +00:00
|
|
|
|
|
|
|
impl Default for Handle {
|
|
|
|
fn default() -> Self {
|
2024-11-02 15:50:10 +00:00
|
|
|
Self::new()
|
2024-11-02 14:43:43 +00:00
|
|
|
}
|
|
|
|
}
|
2024-10-30 07:41:17 +00:00
|
|
|
|
2024-11-03 08:54:22 +00:00
|
|
|
impl fmt::Debug for Handle {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> {
|
|
|
|
f.debug_tuple("Handle").field(&self.client_name()).finish()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-10-30 07:41:17 +00:00
|
|
|
impl Handle {
|
2024-11-02 15:50:10 +00:00
|
|
|
pub fn new() -> Self {
|
2024-11-02 14:43:43 +00:00
|
|
|
let inner =
|
|
|
|
NonNull::new(unsafe { ffi::mpv_create() }).expect("could not create mpv handle");
|
|
|
|
|
2024-11-02 15:50:10 +00:00
|
|
|
let wakeup = Box::pin(Event::new());
|
2024-11-02 14:43:43 +00:00
|
|
|
|
|
|
|
extern "C" fn wakeup_callback(d: *mut c_void) {
|
2024-11-02 15:50:10 +00:00
|
|
|
let d = d as *const Event;
|
|
|
|
let wakeup = unsafe { &*d };
|
|
|
|
wakeup.notify(1_u32.relaxed()); // mpv has its own synchronization, trust it
|
2024-11-02 14:43:43 +00:00
|
|
|
}
|
|
|
|
unsafe {
|
|
|
|
ffi::mpv_set_wakeup_callback(
|
|
|
|
inner.as_ptr(),
|
|
|
|
Some(wakeup_callback),
|
2024-11-02 15:50:10 +00:00
|
|
|
std::ptr::from_ref::<Event>(&wakeup) as *mut c_void,
|
2024-11-02 14:43:43 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2024-11-02 17:25:26 +00:00
|
|
|
// set up verbose logging for now
|
2024-11-02 19:57:12 +00:00
|
|
|
Error::from_return_code(unsafe {
|
|
|
|
ffi::mpv_request_log_messages(inner.as_ptr(), c"v".as_ptr())
|
|
|
|
})
|
|
|
|
.unwrap();
|
2024-11-02 17:25:26 +00:00
|
|
|
|
2024-11-02 15:50:10 +00:00
|
|
|
// TODO: maybe we need to set something before initialization, but idk
|
|
|
|
// also wed need a builder, since "Note that you should avoid doing concurrent accesses on the uninitialized client handle."
|
2024-11-02 16:05:26 +00:00
|
|
|
Error::from_return_code(unsafe { ffi::mpv_initialize(inner.as_ptr()) })
|
|
|
|
.expect("could not initialize mpv handle");
|
2024-11-02 14:43:43 +00:00
|
|
|
|
2024-11-02 16:29:53 +00:00
|
|
|
Self {
|
|
|
|
inner,
|
|
|
|
wakeup,
|
|
|
|
wait_event_cell: RefCell::new(()),
|
|
|
|
}
|
2024-10-30 07:41:17 +00:00
|
|
|
}
|
|
|
|
|
2024-11-03 08:54:22 +00:00
|
|
|
pub fn client_name(&self) -> &str {
|
|
|
|
unsafe { CStr::from_ptr(ffi::mpv_client_name(self.inner.as_ptr())) }
|
|
|
|
.to_str()
|
|
|
|
.unwrap()
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn client_id(&self) -> i64 {
|
|
|
|
unsafe { ffi::mpv_client_id(self.inner.as_ptr()) }
|
|
|
|
}
|
|
|
|
|
2024-11-02 16:05:26 +00:00
|
|
|
pub fn set_property(&self, name: &str, value: impl FormatValue) -> Result<(), Error> {
|
2024-11-02 14:43:43 +00:00
|
|
|
// need to add zero terminator
|
|
|
|
let name = CString::new(name).expect("null bytes in property name");
|
|
|
|
value.into_data(|format, data| {
|
|
|
|
Error::from_return_code(unsafe {
|
|
|
|
ffi::mpv_set_property(self.inner.as_ptr(), name.as_ptr(), format, data)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2024-11-02 20:11:18 +00:00
|
|
|
pub fn command<'a>(&self, args: impl IntoIterator<Item = &'a str>) -> Result<(), Error> {
|
|
|
|
// add zero terminators to Everything
|
|
|
|
let args: Vec<CString> = args
|
|
|
|
.into_iter()
|
|
|
|
.map(CString::new)
|
|
|
|
.map(Result::unwrap)
|
|
|
|
.collect();
|
|
|
|
let mut args: Vec<*const c_char> = args
|
|
|
|
.iter()
|
|
|
|
.map(CString::as_c_str)
|
|
|
|
.map(CStr::as_ptr)
|
|
|
|
.collect();
|
|
|
|
// must be null terminated
|
|
|
|
args.push(std::ptr::null_mut());
|
|
|
|
let args: *mut *const c_char = args.as_mut_ptr();
|
|
|
|
|
|
|
|
Error::from_return_code(unsafe { ffi::mpv_command(self.inner.as_ptr(), args) })
|
|
|
|
}
|
|
|
|
|
2024-11-02 19:57:12 +00:00
|
|
|
pub fn tick(&self) -> Option<EventListener> {
|
|
|
|
// take listener before we drain the event queue, so we don't miss any notifications
|
|
|
|
let listener = self.wakeup.listen();
|
|
|
|
let borrowed = self
|
|
|
|
.wait_event_cell
|
|
|
|
.try_borrow_mut()
|
|
|
|
.expect("Mpv::tick is not reentrant");
|
|
|
|
|
2024-11-02 14:43:43 +00:00
|
|
|
loop {
|
|
|
|
let event = unsafe { &*ffi::mpv_wait_event(self.inner.as_ptr(), 0.0) };
|
2024-11-02 16:29:53 +00:00
|
|
|
|
2024-11-02 14:43:43 +00:00
|
|
|
match event.event_id {
|
|
|
|
ffi::mpv_event_id_MPV_EVENT_NONE => break,
|
|
|
|
|
2024-11-02 17:25:26 +00:00
|
|
|
ffi::mpv_event_id_MPV_EVENT_LOG_MESSAGE => {
|
|
|
|
let data = unsafe { &*(event.data as *mut ffi::mpv_event_log_message) };
|
|
|
|
// TODO: actual logging?
|
|
|
|
let prefix = unsafe { CStr::from_ptr(data.prefix) }.to_str().unwrap();
|
|
|
|
let level = unsafe { CStr::from_ptr(data.level) }.to_str().unwrap();
|
|
|
|
let text = unsafe { CStr::from_ptr(data.text) }.to_str().unwrap();
|
|
|
|
print!("[{prefix}] {level}: {text}");
|
2024-11-02 19:57:12 +00:00
|
|
|
}
|
2024-11-02 17:25:26 +00:00
|
|
|
|
2024-11-02 20:20:26 +00:00
|
|
|
ffi::mpv_event_id_MPV_EVENT_SHUTDOWN => {
|
|
|
|
return None; // good bye forever
|
|
|
|
}
|
|
|
|
|
2024-11-02 14:43:43 +00:00
|
|
|
11 => { /* deprecated, ignore */ }
|
|
|
|
|
|
|
|
_ => todo!("event {}", event.event_id),
|
2024-10-30 07:41:17 +00:00
|
|
|
}
|
|
|
|
}
|
2024-11-02 15:50:10 +00:00
|
|
|
|
2024-11-02 19:57:12 +00:00
|
|
|
drop(borrowed); // make sure the borrow is held until here
|
|
|
|
Some(listener)
|
2024-10-30 07:41:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-11-02 14:43:43 +00:00
|
|
|
impl Drop for Handle {
|
|
|
|
fn drop(&mut self) {
|
2024-11-02 20:15:55 +00:00
|
|
|
println!("dropping MpvHandle");
|
|
|
|
|
2024-11-02 19:57:12 +00:00
|
|
|
// let any executor ticking tasks know we're ded
|
|
|
|
self.wakeup.notify(u32::MAX.relaxed());
|
|
|
|
|
|
|
|
// just in case
|
|
|
|
unsafe {
|
|
|
|
ffi::mpv_wait_async_requests(self.inner.as_ptr());
|
|
|
|
}
|
|
|
|
// drain event queue (we're &mut so we know we have exclusive access)
|
|
|
|
self.tick();
|
|
|
|
|
2024-11-02 17:25:26 +00:00
|
|
|
unsafe {
|
|
|
|
ffi::mpv_destroy(self.inner.as_ptr());
|
|
|
|
}
|
2024-11-02 14:43:43 +00:00
|
|
|
}
|
2024-10-30 07:41:17 +00:00
|
|
|
}
|
|
|
|
|
2024-11-02 14:43:43 +00:00
|
|
|
pub trait FormatValue: Sized {
|
|
|
|
const FORMAT: ffi::mpv_format;
|
|
|
|
fn into_data<T>(self, f: impl FnOnce(ffi::mpv_format, *mut c_void) -> T) -> T;
|
|
|
|
fn from_data<T>(
|
|
|
|
format: ffi::mpv_format,
|
|
|
|
data: *mut c_void,
|
|
|
|
f: impl FnOnce(Option<Self>) -> T,
|
|
|
|
) -> T;
|
2024-10-30 07:41:17 +00:00
|
|
|
}
|
|
|
|
|
2024-11-02 14:43:43 +00:00
|
|
|
impl<'a> FormatValue for &'a str {
|
|
|
|
const FORMAT: ffi::mpv_format = ffi::mpv_format_MPV_FORMAT_STRING;
|
2024-10-30 07:41:17 +00:00
|
|
|
|
2024-11-02 14:43:43 +00:00
|
|
|
fn into_data<T>(self, f: impl FnOnce(ffi::mpv_format, *mut c_void) -> T) -> T {
|
|
|
|
let str = CString::new(self).expect("null bytes in string");
|
|
|
|
let str_ptr = str.as_ptr();
|
|
|
|
f(Self::FORMAT, (&str_ptr) as *const _ as *mut c_void)
|
|
|
|
}
|
2024-10-30 07:41:17 +00:00
|
|
|
|
2024-11-02 14:43:43 +00:00
|
|
|
fn from_data<T>(
|
|
|
|
_format: ffi::mpv_format,
|
|
|
|
_data: *mut c_void,
|
|
|
|
_f: impl FnOnce(Option<Self>) -> T,
|
|
|
|
) -> T {
|
|
|
|
todo!()
|
|
|
|
}
|
2024-10-30 07:41:17 +00:00
|
|
|
}
|
|
|
|
|
2024-11-02 14:43:43 +00:00
|
|
|
impl FormatValue for bool {
|
|
|
|
const FORMAT: ffi::mpv_format = ffi::mpv_format_MPV_FORMAT_FLAG;
|
2024-10-30 07:41:17 +00:00
|
|
|
|
2024-11-02 14:43:43 +00:00
|
|
|
fn into_data<T>(self, f: impl FnOnce(ffi::mpv_format, *mut c_void) -> T) -> T {
|
|
|
|
let flag: c_int = if self { 1 } else { 0 };
|
|
|
|
f(Self::FORMAT, (&flag) as *const c_int as *mut c_void)
|
|
|
|
}
|
2024-10-30 07:41:17 +00:00
|
|
|
|
2024-11-02 14:43:43 +00:00
|
|
|
fn from_data<T>(
|
|
|
|
_format: ffi::mpv_format,
|
|
|
|
_data: *mut c_void,
|
|
|
|
_f: impl FnOnce(Option<Self>) -> T,
|
|
|
|
) -> T {
|
|
|
|
todo!()
|
|
|
|
}
|
2024-10-30 07:41:17 +00:00
|
|
|
}
|
|
|
|
|
2024-11-02 14:43:43 +00:00
|
|
|
impl FormatValue for f64 {
|
|
|
|
const FORMAT: ffi::mpv_format = ffi::mpv_format_MPV_FORMAT_DOUBLE;
|
|
|
|
|
|
|
|
fn into_data<T>(self, f: impl FnOnce(ffi::mpv_format, *mut c_void) -> T) -> T {
|
|
|
|
f(Self::FORMAT, (&self) as *const f64 as *mut c_void)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn from_data<T>(
|
|
|
|
format: ffi::mpv_format,
|
|
|
|
data: *mut c_void,
|
|
|
|
f: impl FnOnce(Option<Self>) -> T,
|
|
|
|
) -> T {
|
|
|
|
match format {
|
|
|
|
ffi::mpv_format_MPV_FORMAT_NONE => f(None),
|
|
|
|
ffi::mpv_format_MPV_FORMAT_DOUBLE => f(Some(unsafe { *(data as *mut f64) })),
|
|
|
|
_ => panic!(),
|
|
|
|
}
|
2024-10-30 07:41:17 +00:00
|
|
|
}
|
|
|
|
}
|