Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ tracing-subscriber = "0.3.23"
serde = "1.0.229"
serde_json = "1.0.151"
itertools = "0.15.0"
libmpv2 = { version = "5.0.3", git = "https://github.com/Stremio/libmpv2-rs" }
libmpv2 = { version = "5.0.3", git = "https://github.com/Stremio/libmpv2-rs", rev = "b0c168c7f0efabac85d4961db086340c9d1382b9" }
image = "0.25.10"
chrono = "0.4.45"
anyhow = "1.0.104"
Expand Down
115 changes: 107 additions & 8 deletions src/app/video/imp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,13 @@ use libmpv2::{
mpv_end_file_reason,
render::{OpenGLInitParams, RenderContext, RenderParam, RenderParamApiType},
};
use std::{cell::RefCell, env, os::raw::c_void, sync::OnceLock};
use std::{
cell::{Cell, RefCell},
collections::VecDeque,
env,
os::raw::c_void,
sync::OnceLock,
};
use tracing::error;

use crate::spawn_local;
Expand All @@ -25,11 +31,28 @@ enum EventCallback {
Events,
}

type Submit = Box<dyn FnOnce(&Mpv, u64) -> libmpv2::Result<()>>;

struct Control {
name: String,
submit: Submit,
}

impl Control {
fn is_subtitle(&self) -> bool {
matches!(self.name.as_str(), "sub-add" | "sub-remove" | "sid")
}
}

#[derive(Properties)]
#[properties(wrapper_type = super::Video)]
pub struct Video {
mpv: RefCell<Mpv>,
render_context: RefCell<Option<RenderContext>>,
controls: RefCell<VecDeque<Control>>,
pending_control: RefCell<Option<(u64, String)>>,
pending_subtitle: Cell<Option<u64>>,
next_request: Cell<u64>,
}

impl Default for Video {
Expand Down Expand Up @@ -62,6 +85,10 @@ impl Default for Video {
Self {
mpv: RefCell::new(mpv),
render_context: Default::default(),
controls: Default::default(),
pending_control: Default::default(),
pending_subtitle: Default::default(),
next_request: Default::default(),
}
}
}
Expand All @@ -82,22 +109,86 @@ impl Video {
}
}

pub fn send_command(&self, name: &str, args: &[&str]) {
if let Err(e) = self.mpv.borrow().command(name, args) {
error!("Failed to send command {name}: {e}");
fn dispatch(&self) {
while self.pending_control.borrow().is_none() {
let control = {
let mut controls = self.controls.borrow_mut();
let subtitle_pending = self.pending_subtitle.get().is_some();
controls
.iter()
.position(|control| !subtitle_pending || !control.is_subtitle())
.and_then(|index| controls.remove(index))
};
let Some(control) = control else {
break;
};

let id = self.next_request.get();
self.next_request.set(id + 1);

match (control.submit)(&self.mpv.borrow(), id) {
Ok(()) if control.name == "sub-add" => self.pending_subtitle.set(Some(id)),
Ok(()) => *self.pending_control.borrow_mut() = Some((id, control.name)),
Err(e) => error!("Failed to send {}: {e}", control.name),
}
}
}

fn enqueue(&self, control: Control) {
self.controls.borrow_mut().push_back(control);
self.dispatch();
}

fn on_reply(&self, id: u64, result: libmpv2::Result<()>) {
let name = if self.pending_subtitle.get() == Some(id) {
self.pending_subtitle.set(None);
Some("sub-add".to_owned())
} else {
self.pending_control
.borrow_mut()
.take_if(|(pending, _)| *pending == id)
.map(|(_, name)| name)
};

if let (Some(name), Err(e)) = (name, result) {
error!("Failed to send {name}: {e}");
}

self.dispatch();
}

pub fn send_command(&self, name: String, args: Vec<String>) {
if matches!(name.as_str(), "loadfile" | "stop") {
self.controls
.borrow_mut()
.retain(|control| !control.is_subtitle());
if let Some(id) = self.pending_subtitle.get() {
self.mpv.borrow().abort_async_command(id);
}
}

let command = name.clone();
self.enqueue(Control {
name,
submit: Box::new(move |mpv, id| {
let args: Vec<_> = args.iter().map(String::as_str).collect();
mpv.command_async(&command, &args, id)
}),
});
}

pub fn observe_property(&self, name: &str, format: Format) {
if let Err(e) = self.mpv.borrow().observe_property(name, format, 0) {
error!("Failed to observe property {name}: {e}");
}
}

pub fn set_property<T: SetData>(&self, name: &str, value: T) {
if let Err(e) = self.mpv.borrow().set_property(name, value) {
error!("Failed to set property {name}: {e}");
}
pub fn set_property<T: SetData + 'static>(&self, name: &str, value: T) {
let property = name.to_owned();
self.enqueue(Control {
name: name.to_owned(),
submit: Box::new(move |mpv, id| mpv.set_property_async(&property, value, id)),
});
}
}

Expand Down Expand Up @@ -181,6 +272,14 @@ impl WidgetImpl for Video {
object.emit_by_name::<()>("playback-ended", &[&reason]);
video.unobserve_properties();
}
Event::CommandReply {
reply_userdata,
result,
}
| Event::SetPropertyReply {
reply_userdata,
result,
} => video.on_reply(reply_userdata, result),
_ => {}
});
}
Expand Down
8 changes: 2 additions & 6 deletions src/app/video/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ mod imp;

use adw::subclass::prelude::ObjectSubclassIsExt;
use gtk::glib::{self, Variant, closure_local, object::ObjectExt};
use itertools::Itertools;
use libmpv2::Format;
use serde_json::{Number, Value};
use tracing::warn;
Expand Down Expand Up @@ -68,10 +67,7 @@ impl Video {
}

pub fn send_mpv_command(&self, name: String, args: Vec<String>) {
let widget = self.imp();

let args = args.iter().map(String::as_ref).collect_vec();
widget.send_command(&name, &args);
self.imp().send_command(name, args);
}

pub fn observe_mpv_property(&self, name: String) {
Expand Down Expand Up @@ -107,7 +103,7 @@ impl Video {
}
name if STRING_PROPERTIES.contains(&name) => {
if let Some(value) = value.as_str() {
widget.set_property(name, value);
widget.set_property(name, value.to_owned());
}
}
name => warn!("Failed to set property {name}: Unsupported"),
Expand Down
Loading