Audio chunk reading

This commit is contained in:
Muaz Ahmad 2024-11-29 13:32:44 +05:00
parent 71cc75feff
commit 6b0a53e96f
4 changed files with 22 additions and 3 deletions

View file

@ -3,6 +3,7 @@ use image::DynamicImage;
use crate::{ssonic::response::Song, utils::default_cover};
pub struct Metadata {
pub id: String,
pub name: String,
pub artist: Option<String>,
pub playing: bool,
@ -16,6 +17,7 @@ pub struct Metadata {
impl Metadata {
pub fn new() -> Metadata {
Metadata {
id: String::new(),
name: String::new(),
artist: None,
playing: false,
@ -32,6 +34,7 @@ impl Metadata {
}
pub fn update_metadata(&mut self, song: Song) {
self.id = song.id;
self.name = song.title;
self.artist = song.artist;
self.current_time = 0;

View file

@ -21,6 +21,7 @@ pub enum PlayerEvent {
UpdateCover(DynamicImage),
UserQuit,
PlayNext,
AddAudioChunks(Vec<u8>),
}
pub struct Player {

View file

@ -80,8 +80,8 @@ impl Player {
self.player_chan_in
.send(PlayerEvent::UpdateCover(default_cover()))?;
}
self.fetch_audio_chunk(song.id.clone())?;
self.tui_root.metadata.update_metadata(song);
self.fetch_audio_chunk(self.tui_root.metadata.id.clone())?;
Ok(())
}

View file

@ -42,7 +42,22 @@ impl APIClient {
header.insert(RANGE, range_header.parse()?);
let mut response = self.request("/stream", Some(&[("id", id)]), Some(header))?;
unimplemented!()
if !response.status().is_success() {
return Err(Box::new(APIError::StatusError(
response.status(),
"/stream",
)));
};
if response.headers()[CONTENT_TYPE].to_str()? == "application/json" {
self.validate(response, "/stream")?; // will never error if not image
} else {
eprintln!("Fetched chunk");
let mut audio_chunk = Vec::new();
response.read_to_end(&mut audio_chunk)?;
return Ok(PlayerEvent::AddAudioChunks(audio_chunk));
}
unreachable!();
}
fn get_cover_art(&mut self, cover_id: String) -> Result<PlayerEvent, Error> {
@ -65,7 +80,7 @@ impl APIClient {
)?;
return Ok(PlayerEvent::UpdateCover(cover));
}
unimplemented!()
unreachable!();
}
fn get_random(&mut self, n: i32) -> Result<PlayerEvent, Error> {