Listening for end file events but not sending them yet.
This commit is contained in:
parent
4dc352714c
commit
9621787959
@ -50,12 +50,17 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
|||||||
sleep(Duration::from_secs(1)).await;
|
sleep(Duration::from_secs(1)).await;
|
||||||
|
|
||||||
let mut mpvctl = MpvCtl::connect("/tmp/recordwatchsocket").await?;
|
let mut mpvctl = MpvCtl::connect("/tmp/recordwatchsocket").await?;
|
||||||
|
let mut end_file_listener = mpvctl.listen(&["end-file"])?;
|
||||||
let client_name = mpvctl.get_client_name().await?;
|
let client_name = mpvctl.get_client_name().await?;
|
||||||
let _ = mpvctl
|
let _ = mpvctl
|
||||||
.play_video("/home/talexander/Downloads/test.mp4")
|
.play_video("/home/talexander/Downloads/test.mp4")
|
||||||
.await?;
|
.await?;
|
||||||
|
|
||||||
sleep(Duration::from_secs(5)).await;
|
while let Some(evt) = end_file_listener.recv().await {
|
||||||
|
println!("end file event {}", evt);
|
||||||
|
}
|
||||||
|
|
||||||
|
sleep(Duration::from_secs(50)).await;
|
||||||
let client_name = mpvctl.get_client_name().await?;
|
let client_name = mpvctl.get_client_name().await?;
|
||||||
|
|
||||||
println!("done {}", client_name);
|
println!("done {}", client_name);
|
||||||
|
@ -11,14 +11,17 @@ use std::{collections::BTreeMap, path::Path};
|
|||||||
use tokio::io::{AsyncReadExt, AsyncWriteExt};
|
use tokio::io::{AsyncReadExt, AsyncWriteExt};
|
||||||
use tokio::net::unix::OwnedWriteHalf;
|
use tokio::net::unix::OwnedWriteHalf;
|
||||||
use tokio::net::UnixStream;
|
use tokio::net::UnixStream;
|
||||||
|
use tokio::sync::mpsc;
|
||||||
use tokio::sync::oneshot;
|
use tokio::sync::oneshot;
|
||||||
|
|
||||||
type CommandDb = Arc<Mutex<HashMap<u64, oneshot::Sender<serde_json::Value>>>>;
|
type CommandDb = Arc<Mutex<HashMap<u64, oneshot::Sender<serde_json::Value>>>>;
|
||||||
|
type EventDb = Arc<Mutex<HashMap<String, Vec<mpsc::Sender<serde_json::Value>>>>>;
|
||||||
|
|
||||||
pub struct MpvCtl {
|
pub struct MpvCtl {
|
||||||
socket: OwnedWriteHalf,
|
socket: OwnedWriteHalf,
|
||||||
next_request_id: u64,
|
next_request_id: u64,
|
||||||
in_flight_requests: CommandDb,
|
in_flight_requests: CommandDb,
|
||||||
|
event_listeners: EventDb,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl MpvCtl {
|
impl MpvCtl {
|
||||||
@ -46,9 +49,32 @@ impl MpvCtl {
|
|||||||
socket: socket_write,
|
socket: socket_write,
|
||||||
next_request_id: 0,
|
next_request_id: 0,
|
||||||
in_flight_requests: db,
|
in_flight_requests: db,
|
||||||
|
event_listeners: Arc::new(Mutex::new(HashMap::new())),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn listen(
|
||||||
|
&mut self,
|
||||||
|
events: &[&str],
|
||||||
|
) -> Result<mpsc::Receiver<serde_json::Value>, Box<dyn std::error::Error>> {
|
||||||
|
let (tx, rx): (
|
||||||
|
tokio::sync::mpsc::Sender<serde_json::Value>,
|
||||||
|
tokio::sync::mpsc::Receiver<serde_json::Value>,
|
||||||
|
) = mpsc::channel(10);
|
||||||
|
|
||||||
|
{
|
||||||
|
let mut db_handle = self.event_listeners.lock().unwrap();
|
||||||
|
for evt in events {
|
||||||
|
(*db_handle)
|
||||||
|
.entry(evt.to_string())
|
||||||
|
.or_insert_with(Vec::new)
|
||||||
|
.push(tx.clone());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(rx)
|
||||||
|
}
|
||||||
|
|
||||||
async fn read_loop(
|
async fn read_loop(
|
||||||
framed_read: &mut MpvFramed,
|
framed_read: &mut MpvFramed,
|
||||||
db: &CommandDb,
|
db: &CommandDb,
|
||||||
|
Loading…
x
Reference in New Issue
Block a user