summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorYuval Adam <_@yuv.al>2024-06-04 20:50:46 +0200
committerYuval Adam <_@yuv.al>2024-06-04 20:50:46 +0200
commit355743d349d8f63a5d98246c0f0265812fbb2482 (patch)
treec48c313ebad2949afa96e64d8ed48e1d2b16fdf7 /src
parent482a942cd9e62fadee4ee7bc1fd7e8def8a85f71 (diff)
Initial spinner mpsc
Diffstat (limited to 'src')
-rw-r--r--src/audio/mod.rs8
-rw-r--r--src/audio/mpv.rs3
-rw-r--r--src/audio/rodio.rs7
-rw-r--r--src/main.rs13
4 files changed, 22 insertions, 9 deletions
diff --git a/src/audio/mod.rs b/src/audio/mod.rs
index 5aae19a..81cdc70 100644
--- a/src/audio/mod.rs
+++ b/src/audio/mod.rs
@@ -1,3 +1,5 @@
+use tokio::sync::mpsc;
+
mod mpv;
mod rodio;
@@ -13,10 +15,10 @@ pub fn get_backend() -> AudioBackends {
}
impl AudioBackends {
- pub async fn play(&self, url: &str) {
+ pub async fn play(&self, url: &str, tx: mpsc::Sender<String>) {
match self {
- AudioBackends::Mpv(m) => m.play(url).await,
- AudioBackends::Rodio(r) => r.play(url).await,
+ AudioBackends::Mpv(m) => m.play(url, tx).await,
+ AudioBackends::Rodio(r) => r.play(url, tx).await,
}
}
}
diff --git a/src/audio/mpv.rs b/src/audio/mpv.rs
index fde2711..06d3630 100644
--- a/src/audio/mpv.rs
+++ b/src/audio/mpv.rs
@@ -1,9 +1,10 @@
use std::process::Command;
+use tokio::sync::mpsc;
pub struct Mpv {}
impl Mpv {
- pub async fn play(&self, url: &str) {
+ pub async fn play(&self, url: &str, _tx: mpsc::Sender<String>) {
Command::new("mpv")
.args([url])
.output()
diff --git a/src/audio/rodio.rs b/src/audio/rodio.rs
index 0f9fbb2..695c6cb 100644
--- a/src/audio/rodio.rs
+++ b/src/audio/rodio.rs
@@ -6,13 +6,14 @@ use stream_download::http::HttpStream;
use stream_download::storage::bounded::BoundedStorageProvider;
use stream_download::storage::memory::MemoryStorageProvider;
use stream_download::{Settings, StreamDownload};
+use tokio::sync::mpsc;
const AUDIO_BUFFER_SECONDS: u32 = 5;
pub struct Rodio {}
impl Rodio {
- pub async fn play(&self, url: &str) {
+ pub async fn play(&self, url: &str, tx: mpsc::Sender<String>) {
let (_stream, handle) = OutputStream::try_default().unwrap();
let sink = Sink::try_new(&handle).unwrap();
@@ -48,10 +49,10 @@ impl Rodio {
// Since we requested icy metadata, the metadata interval header should be present in the
// response. This will allow us to parse the metadata within the stream
icy_headers.metadata_interval(),
- |metadata| {
+ move |metadata| {
if let Ok(md) = metadata {
if let Some(tr) = md.stream_title() {
- println!(" {tr}");
+ let _ = tx.send(tr.to_string());
}
}
},
diff --git a/src/main.rs b/src/main.rs
index 10c7d6f..19442a6 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -2,6 +2,7 @@ use crate::channels::Channel;
use channels::get_stream_url;
use inquire::{InquireError, Select};
use spinners::{Spinner, Spinners};
+use tokio::sync::mpsc;
mod audio;
mod channels;
@@ -15,6 +16,8 @@ async fn main() {
let ans: Result<Channel, InquireError> =
Select::new("Select channel from list:", channels).prompt();
+ let (tx, mut rx) = mpsc::channel(5);
+
match ans {
Ok(ch) => {
let mut sp = Spinner::new(Spinners::Dots, "Fetching channel streams...".into());
@@ -22,8 +25,14 @@ async fn main() {
let url = get_stream_url(&playlist).await.unwrap();
sp.stop_with_newline();
- let _sp = Spinner::new(Spinners::Arrow3, format!("Playing {}", ch.title));
- audio::get_backend().play(&url).await
+ sp = Spinner::new(Spinners::Arrow3, format!("Playing {}", ch.title));
+ tokio::spawn(async move {
+ while let Some(title) = rx.recv().await {
+ sp.stop_with_newline();
+ sp = Spinner::new(Spinners::Arrow3, title);
+ }
+ });
+ audio::get_backend().play(&url, tx).await
}
Err(_e) => {
println!("\nNo channel selected, exiting.");