about summary refs log tree commit diff stats
path: root/libmpv2/examples/protocol.rs
diff options
context:
space:
mode:
authorBenedikt Peetz <benedikt.peetz@b-peetz.de>2024-08-21 10:49:23 +0200
committerBenedikt Peetz <benedikt.peetz@b-peetz.de>2024-08-21 11:28:43 +0200
commit1debeb77f7986de1b659dcfdc442de6415e1d9f5 (patch)
tree4df3e7c3f6a2d1ec116e4088c5ace7f143a8b05f /libmpv2/examples/protocol.rs
downloadyt-1debeb77f7986de1b659dcfdc442de6415e1d9f5.tar.gz
yt-1debeb77f7986de1b659dcfdc442de6415e1d9f5.zip
chore: Initial Commit
This repository was migrated out of my nixos-config.
Diffstat (limited to 'libmpv2/examples/protocol.rs')
-rw-r--r--libmpv2/examples/protocol.rs87
1 files changed, 87 insertions, 0 deletions
diff --git a/libmpv2/examples/protocol.rs b/libmpv2/examples/protocol.rs
new file mode 100644
index 0000000..46702d6
--- /dev/null
+++ b/libmpv2/examples/protocol.rs
@@ -0,0 +1,87 @@
+// yt - A fully featured command line YouTube client
+//
+// Copyright (C) 2024 Benedikt Peetz <benedikt.peetz@b-peetz.de>
+// SPDX-License-Identifier: GPL-3.0-or-later
+//
+// This file is part of Yt.
+//
+// You should have received a copy of the License along with this program.
+// If not, see <https://www.gnu.org/licenses/gpl-3.0.txt>.
+
+use std::{
+    env,
+    fs::File,
+    io::{Read, Seek, SeekFrom},
+    mem, thread,
+    time::Duration,
+};
+
+#[cfg(all(not(test), not(feature = "protocols")))]
+compile_error!("The feature `protocols` needs to be enabled for this example`");
+
+#[cfg(feature = "protocols")]
+fn main() {
+    use libmpv2::{protocol::*, *};
+
+    let path = format!(
+        "filereader://{}",
+        env::args()
+            .nth(1)
+            .expect("Expected path to local media as argument, found nil.")
+    );
+
+    let protocol = unsafe {
+        Protocol::new(
+            "filereader".into(),
+            (),
+            open,
+            close,
+            read,
+            Some(seek),
+            Some(size),
+        )
+    };
+
+    let mpv = Mpv::new().unwrap();
+    mpv.set_property("volume", 25).unwrap();
+
+    let proto_ctx = mpv.create_protocol_context();
+    proto_ctx.register(protocol).unwrap();
+
+    mpv.command("loadfile", &[&path, "append-play"]).unwrap();
+
+    thread::sleep(Duration::from_secs(10));
+
+    mpv.command("seek", &["15"]).unwrap();
+
+    thread::sleep(Duration::from_secs(5));
+}
+
+fn open(_: &mut (), uri: &str) -> File {
+    // Open the file, and strip the `filereader://` part
+    let ret = File::open(&uri[13..]).unwrap();
+
+    println!("Opened file[{}], ready for orders o7", &uri[13..]);
+    ret
+}
+
+fn close(_: Box<File>) {
+    println!("Closing file, bye bye~~");
+}
+
+fn read(cookie: &mut File, buf: &mut [i8]) -> i64 {
+    unsafe {
+        let forbidden_magic = mem::transmute::<&mut [i8], &mut [u8]>(buf);
+
+        cookie.read(forbidden_magic).unwrap() as _
+    }
+}
+
+fn seek(cookie: &mut File, offset: i64) -> i64 {
+    println!("Seeking to byte {}", offset);
+    cookie.seek(SeekFrom::Start(offset as u64)).unwrap() as _
+}
+
+fn size(cookie: &mut File) -> i64 {
+    cookie.metadata().unwrap().len() as _
+}