libre_url2audio_lib/
lib.rs

1mod url_source;
2mod player_engine;
3mod pulseaudio;
4
5use std::{sync::{Arc, RwLock}, thread::sleep, time::Duration};
6
7use crossbeam_channel::{unbounded, Receiver, Sender};
8
9use crate::player_engine::{PlayerActions, PlayerEngine, PlayerState, PlayerStatus};
10
11pub struct Player {
12    inner_player: Arc<RwLock<PlayerEngine>>,
13    tx: Sender<PlayerActions>,
14    rx: Receiver<PlayerActions>,
15    rx_status: Receiver<PlayerStatus>,
16    tx_status: Sender<PlayerStatus>,
17    state: Arc<RwLock<PlayerState>>,
18}
19
20impl Player {
21    pub fn new() -> Self {
22        let (tx, rx) = unbounded(); 
23        let (tx_status, rx_status) = unbounded(); 
24        let mut to_ret = Player {
25            inner_player: Arc::new(
26                              RwLock::new(
27                                  PlayerEngine::new(
28                                      tx.clone(),
29                                      rx.clone(),
30                                      tx_status.clone(),
31                                      // rx_status.clone()
32                                      ))),
33            tx,
34            rx,
35            rx_status,
36            tx_status,
37            state: Arc::new(RwLock::new(PlayerState {
38                playing: true,
39                duration: 0.0,
40                position: 0.0,
41            }))
42        };
43        to_ret.inner_thread();
44        to_ret
45
46    }
47    
48    pub fn open(&mut self, src: &str) {
49        let _ = self.tx.send(PlayerActions::Open(src.to_string()));
50
51    }
52
53    pub fn inner_thread(&mut self) {
54        let player = self.inner_player.clone();
55
56        // let _ = self.tx.send(PlayerActions::Close);
57        let _ = std::thread::spawn(move || {
58            let mut p = player.write().unwrap();
59            let result = p.start(); //(&path);
60            println!("Res: {:#?}", result);
61        });
62
63        let rx1 = self.rx_status.clone();
64        let s = self.state.clone();
65        let _ = std::thread::spawn(move || {
66            loop {
67                let a = rx1.try_recv();
68
69                match a {
70                    Ok(a) => {
71                        let mut state = s.write().unwrap();
72                        match a {
73                            PlayerStatus::SendPlaying(playing) => {
74                                state.playing = playing;
75                            }
76                            PlayerStatus::SendDuration(duration) => {
77                                state.duration = duration;
78                            }
79                            PlayerStatus::SendPosition(position) => {
80                                state.position = position;
81                            }
82                        }
83                    },
84                    Err(_) => { 
85                    },
86                }
87                sleep(Duration::from_millis(50));
88            }
89        });
90    }
91
92    pub fn play(&self) {
93        let _ = self.tx.send(PlayerActions::Resume);
94    }
95
96    pub fn pause(&self) {
97        let _ = self.tx.send(PlayerActions::Pause);
98    }
99
100    pub fn toggle_play(&self) {
101    }
102
103    /// seek to time from the beginning.
104    /// `time` is in seconds
105    pub fn seek(&self,time: f64) {
106        let _ = self.tx.send(PlayerActions::Seek(time));
107    }
108
109    /// seek to time relative from current position
110    pub fn seek_relative(&self, dt: f64) {
111        let new_pos = self.current_position() + dt;
112        let _ = self.tx.send(PlayerActions::Seek(new_pos));
113
114    }
115
116    pub fn current_position(&self) -> f64 {
117        self.state.read().unwrap().position
118    }
119
120    pub fn duration(&self) -> f64 {
121        self.state.read().unwrap().duration
122    }
123
124    
125
126}