Trigger fetching more file
This commit is contained in:
parent
0ca063e927
commit
d0c5a8f61f
5 changed files with 24 additions and 10 deletions
|
@ -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<Vec<u8>>,
|
||||
frames_out: Receiver<Vec<u8>>,
|
||||
sample_buf: VecDeque<Vec<u8>>,
|
||||
input_done: bool,
|
||||
}
|
||||
|
||||
pub fn init(stride: usize, sample_rate: u32) -> Result<DecoderContext, Error> {
|
||||
|
@ -51,6 +50,7 @@ pub fn init(stride: usize, sample_rate: u32) -> Result<DecoderContext, Error> {
|
|||
encoded_in,
|
||||
frames_out,
|
||||
sample_buf: VecDeque::new(),
|
||||
input_done: false,
|
||||
})
|
||||
}
|
||||
|
||||
|
@ -70,11 +70,19 @@ impl DecoderContext {
|
|||
}
|
||||
}
|
||||
|
||||
pub fn next_sample(&mut self) -> Vec<u8> {
|
||||
if self.sample_buf.len() < 100 {
|
||||
pub fn next_sample(&mut self) -> (Vec<u8>, 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,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -15,6 +15,7 @@ use crate::{config::Settings, player::PlayerEvent, utils::Error};
|
|||
pub enum AudioEvent {
|
||||
DecodeChunk(Vec<u8>),
|
||||
DecoderInit(u32, u32),
|
||||
FinalChunkRecvd,
|
||||
}
|
||||
|
||||
pub struct SoundManager {
|
||||
|
|
|
@ -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(())
|
||||
}
|
||||
|
||||
|
|
|
@ -16,14 +16,13 @@ use crate::{
|
|||
utils::Error,
|
||||
};
|
||||
|
||||
pub const AUDIO_FILE_BUFFER: u32 = 5_000_000; // 5MB
|
||||
|
||||
pub enum PlayerEvent {
|
||||
AddSongList(Vec<Song>),
|
||||
UpdateCover(DynamicImage),
|
||||
UserQuit,
|
||||
PlayNext,
|
||||
AddAudioChunk(Vec<u8>),
|
||||
FetchChunk,
|
||||
}
|
||||
|
||||
pub struct Player {
|
||||
|
|
|
@ -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(),
|
||||
|
|
Loading…
Reference in a new issue