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;
|
use crate::utils::Error;
|
||||||
|
|
||||||
const MAX_SAMPLE_BUFF: usize = 1_000_000;
|
|
||||||
|
|
||||||
pub struct DecoderContext {
|
pub struct DecoderContext {
|
||||||
process: Child,
|
process: Child,
|
||||||
encoded_in: Sender<Vec<u8>>,
|
encoded_in: Sender<Vec<u8>>,
|
||||||
frames_out: Receiver<Vec<u8>>,
|
frames_out: Receiver<Vec<u8>>,
|
||||||
sample_buf: VecDeque<Vec<u8>>,
|
sample_buf: VecDeque<Vec<u8>>,
|
||||||
|
input_done: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn init(stride: usize, sample_rate: u32) -> Result<DecoderContext, Error> {
|
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,
|
encoded_in,
|
||||||
frames_out,
|
frames_out,
|
||||||
sample_buf: VecDeque::new(),
|
sample_buf: VecDeque::new(),
|
||||||
|
input_done: false,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -70,11 +70,19 @@ impl DecoderContext {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn next_sample(&mut self) -> Vec<u8> {
|
pub fn next_sample(&mut self) -> (Vec<u8>, bool) {
|
||||||
if self.sample_buf.len() < 100 {
|
let curr_n_samples = self.sample_buf.len();
|
||||||
|
let mut fetch_more_file = false;
|
||||||
|
if curr_n_samples < 1000 {
|
||||||
self.fetch_samples();
|
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 {
|
pub enum AudioEvent {
|
||||||
DecodeChunk(Vec<u8>),
|
DecodeChunk(Vec<u8>),
|
||||||
DecoderInit(u32, u32),
|
DecoderInit(u32, u32),
|
||||||
|
FinalChunkRecvd,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct SoundManager {
|
pub struct SoundManager {
|
||||||
|
|
|
@ -62,8 +62,11 @@ impl SoundManager {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn push_samples(&mut self) -> Result<(), Error> {
|
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)?;
|
self.sample_in.send(frame)?;
|
||||||
|
if fetch_more {
|
||||||
|
self.player_chan.send(PlayerEvent::FetchChunk)?;
|
||||||
|
}
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -16,14 +16,13 @@ use crate::{
|
||||||
utils::Error,
|
utils::Error,
|
||||||
};
|
};
|
||||||
|
|
||||||
pub const AUDIO_FILE_BUFFER: u32 = 5_000_000; // 5MB
|
|
||||||
|
|
||||||
pub enum PlayerEvent {
|
pub enum PlayerEvent {
|
||||||
AddSongList(Vec<Song>),
|
AddSongList(Vec<Song>),
|
||||||
UpdateCover(DynamicImage),
|
UpdateCover(DynamicImage),
|
||||||
UserQuit,
|
UserQuit,
|
||||||
PlayNext,
|
PlayNext,
|
||||||
AddAudioChunk(Vec<u8>),
|
AddAudioChunk(Vec<u8>),
|
||||||
|
FetchChunk,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct Player {
|
pub struct Player {
|
||||||
|
|
|
@ -9,7 +9,7 @@ use crate::{
|
||||||
utils::{default_cover, Error},
|
utils::{default_cover, Error},
|
||||||
};
|
};
|
||||||
|
|
||||||
use super::{errors::PlayerError, Player, PlayerEvent, AUDIO_FILE_BUFFER};
|
use super::{errors::PlayerError, Player, PlayerEvent};
|
||||||
|
|
||||||
impl Player {
|
impl Player {
|
||||||
pub fn begin(&mut self) -> Result<(), Error> {
|
pub fn begin(&mut self) -> Result<(), Error> {
|
||||||
|
@ -46,7 +46,7 @@ impl Player {
|
||||||
PlayerEvent::PlayNext => self.play_next()?,
|
PlayerEvent::PlayNext => self.play_next()?,
|
||||||
PlayerEvent::UpdateCover(cover) => self.tui_root.update_cover(cover),
|
PlayerEvent::UpdateCover(cover) => self.tui_root.update_cover(cover),
|
||||||
PlayerEvent::AddAudioChunk(chunk) => self.recv_chunk(chunk)?,
|
PlayerEvent::AddAudioChunk(chunk) => self.recv_chunk(chunk)?,
|
||||||
|
PlayerEvent::FetchChunk => self.fetch_audio_chunk()?,
|
||||||
_ => unimplemented!(),
|
_ => unimplemented!(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -92,6 +92,9 @@ impl Player {
|
||||||
fn fetch_audio_chunk(&mut self) -> Result<(), Error> {
|
fn fetch_audio_chunk(&mut self) -> Result<(), Error> {
|
||||||
let start = self.tui_root.metadata.bytes_received;
|
let start = self.tui_root.metadata.bytes_received;
|
||||||
let diff = self.tui_root.metadata.size - 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;
|
let end = std::cmp::min(diff, MAX_CHUNK_SIZE) + start - 1;
|
||||||
self.api_chan.send(APIEvent::StreamSong(
|
self.api_chan.send(APIEvent::StreamSong(
|
||||||
self.tui_root.metadata.id.clone(),
|
self.tui_root.metadata.id.clone(),
|
||||||
|
|
Loading…
Reference in a new issue