charcoal_client/actions/
player.rs1use async_trait::async_trait;
2use hearth_interconnect::messages::Message;
3use hearth_interconnect::worker_communication::{DWCActionType, DirectWorkerCommunication};
4
5use crate::background::processor::IPCData;
6use crate::PlayerObject;
7use nanoid::nanoid;
8use snafu::prelude::*;
9use tokio::sync::broadcast::error::SendError;
10
11#[derive(Debug, Snafu)]
12pub enum PlayerActionError {
13 #[snafu(display("Failed to send IPC request to Background thread"))]
14 FailedToSendIPCRequest { source: SendError<IPCData> },
15}
16
17#[async_trait]
18pub trait Player {
20 async fn play_from_http(&mut self, url: String) -> Result<(), PlayerActionError>;
22 async fn play_from_youtube(&mut self, url: String) -> Result<(), PlayerActionError>;
24}
25
26#[async_trait]
27impl Player for PlayerObject {
28 async fn play_from_http(&mut self, url: String) -> Result<(), PlayerActionError> {
29 self.bg_com_tx
30 .send(IPCData::new_from_main(
31 Message::DirectWorkerCommunication(DirectWorkerCommunication {
32 job_id: self.job_id.read().await.clone().unwrap(),
33 action_type: DWCActionType::PlayDirectLink,
34 play_audio_url: Some(url),
35 guild_id: self.guild_id.clone(),
36 request_id: Some(nanoid!()),
37 new_volume: None,
38 seek_position: None,
39 loop_times: None,
40 worker_id: self.worker_id.clone().read().await.clone().unwrap(),
41 voice_channel_id: None,
42 }),
43 self.tx.clone(),
44 self.guild_id.clone(),
45 ))
46 .context(FailedToSendIPCRequestSnafu)?;
47
48 Ok(())
49 }
50 async fn play_from_youtube(&mut self, url: String) -> Result<(), PlayerActionError> {
51 self.bg_com_tx
52 .send(IPCData::new_from_main(
53 Message::DirectWorkerCommunication(DirectWorkerCommunication {
54 job_id: self.job_id.clone().read().await.clone().unwrap(),
55 action_type: DWCActionType::PlayFromYoutube,
56 play_audio_url: Some(url),
57 guild_id: self.guild_id.clone(),
58 request_id: Some(nanoid!()),
59 new_volume: None,
60 seek_position: None,
61 loop_times: None,
62 worker_id: self.worker_id.clone().read().await.clone().unwrap(),
63 voice_channel_id: None,
64 }),
65 self.tx.clone(),
66 self.guild_id.clone(),
67 ))
68 .context(FailedToSendIPCRequestSnafu)?;
69
70 Ok(())
71 }
72}