From d0c5a8f61f8cfa935b1697fb77c91ad4db60d12c Mon Sep 17 00:00:00 2001 From: Muaz Ahmad Date: Mon, 9 Dec 2024 14:51:19 +0500 Subject: [PATCH] Trigger fetching more file --- src/audio/codec.rs | 18 +++++++++++++----- src/audio/mod.rs | 1 + src/audio/sound_mgr.rs | 5 ++++- src/player/mod.rs | 3 +-- src/player/player.rs | 7 +++++-- 5 files changed, 24 insertions(+), 10 deletions(-) diff --git a/src/audio/codec.rs b/src/audio/codec.rs index 7bca5b8..d809eaf 100644 --- a/src/audio/codec.rs +++ b/src/audio/codec.rs @@ -8,13 +8,12 @@ use std::{ use crate::utils::Error; -const MAX_SAMPLE_BUFF: usize = 1_000_000; - pub struct DecoderContext { process: Child, encoded_in: Sender>, frames_out: Receiver>, sample_buf: VecDeque>, + input_done: bool, } pub fn init(stride: usize, sample_rate: u32) -> Result { @@ -51,6 +50,7 @@ pub fn init(stride: usize, sample_rate: u32) -> Result { encoded_in, frames_out, sample_buf: VecDeque::new(), + input_done: false, }) } @@ -70,11 +70,19 @@ impl DecoderContext { } } - pub fn next_sample(&mut self) -> Vec { - if self.sample_buf.len() < 100 { + pub fn next_sample(&mut self) -> (Vec, bool) { + let curr_n_samples = self.sample_buf.len(); + let mut fetch_more_file = false; + if curr_n_samples < 1000 { self.fetch_samples(); + if self.sample_buf.len() == curr_n_samples && !self.input_done { + fetch_more_file = true; + } } - self.sample_buf.pop_front().unwrap_or(vec![0; 1920]) + return ( + self.sample_buf.pop_front().unwrap_or(vec![0; 1920]), + fetch_more_file, + ); } } diff --git a/src/audio/mod.rs b/src/audio/mod.rs index c243131..7746ee5 100644 --- a/src/audio/mod.rs +++ b/src/audio/mod.rs @@ -15,6 +15,7 @@ use crate::{config::Settings, player::PlayerEvent, utils::Error}; pub enum AudioEvent { DecodeChunk(Vec), DecoderInit(u32, u32), + FinalChunkRecvd, } pub struct SoundManager { diff --git a/src/audio/sound_mgr.rs b/src/audio/sound_mgr.rs index dcb97be..3f7d8ac 100644 --- a/src/audio/sound_mgr.rs +++ b/src/audio/sound_mgr.rs @@ -62,8 +62,11 @@ impl SoundManager { } fn push_samples(&mut self) -> Result<(), Error> { - let frame = self.decoder_context.next_sample(); + let (frame, fetch_more) = self.decoder_context.next_sample(); self.sample_in.send(frame)?; + if fetch_more { + self.player_chan.send(PlayerEvent::FetchChunk)?; + } Ok(()) } diff --git a/src/player/mod.rs b/src/player/mod.rs index b9ae20f..32dd3c8 100644 --- a/src/player/mod.rs +++ b/src/player/mod.rs @@ -16,14 +16,13 @@ use crate::{ utils::Error, }; -pub const AUDIO_FILE_BUFFER: u32 = 5_000_000; // 5MB - pub enum PlayerEvent { AddSongList(Vec), UpdateCover(DynamicImage), UserQuit, PlayNext, AddAudioChunk(Vec), + FetchChunk, } pub struct Player { diff --git a/src/player/player.rs b/src/player/player.rs index 9bd60cb..8450e32 100644 --- a/src/player/player.rs +++ b/src/player/player.rs @@ -9,7 +9,7 @@ use crate::{ utils::{default_cover, Error}, }; -use super::{errors::PlayerError, Player, PlayerEvent, AUDIO_FILE_BUFFER}; +use super::{errors::PlayerError, Player, PlayerEvent}; impl Player { pub fn begin(&mut self) -> Result<(), Error> { @@ -46,7 +46,7 @@ impl Player { PlayerEvent::PlayNext => self.play_next()?, PlayerEvent::UpdateCover(cover) => self.tui_root.update_cover(cover), PlayerEvent::AddAudioChunk(chunk) => self.recv_chunk(chunk)?, - + PlayerEvent::FetchChunk => self.fetch_audio_chunk()?, _ => unimplemented!(), } } @@ -92,6 +92,9 @@ impl Player { fn fetch_audio_chunk(&mut self) -> Result<(), Error> { let start = self.tui_root.metadata.bytes_received; let diff = self.tui_root.metadata.size - self.tui_root.metadata.bytes_received; + if diff <= MAX_CHUNK_SIZE { + self.audio_chan.send(AudioEvent::FinalChunkRecvd)?; + } let end = std::cmp::min(diff, MAX_CHUNK_SIZE) + start - 1; self.api_chan.send(APIEvent::StreamSong( self.tui_root.metadata.id.clone(),