Add buffer limit to audio fetch

This commit is contained in:
Muaz Ahmad 2024-12-03 11:56:50 +05:00
parent 60b5f2ed60
commit f2cea03e27
4 changed files with 15 additions and 3 deletions

4
Cargo.lock generated
View file

@ -1167,9 +1167,9 @@ dependencies = [
[[package]] [[package]]
name = "libloading" name = "libloading"
version = "0.8.5" version = "0.8.6"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4979f22fdb869068da03c9f7528f8297c6fd2606bc3a4affe42e6a823fdb8da4" checksum = "fc2f4eb4bc735547cfed7c0a4922cbd04a4655978c09b54f1f7b228750664c34"
dependencies = [ dependencies = [
"cfg-if", "cfg-if",
"windows-targets", "windows-targets",

View file

@ -12,6 +12,7 @@ pub struct Metadata {
pub cover: DynamicImage, pub cover: DynamicImage,
pub size: u32, pub size: u32,
pub bytes_received: u32, pub bytes_received: u32,
pub bytes_handled: u32,
} }
impl Metadata { impl Metadata {
@ -26,6 +27,7 @@ impl Metadata {
cover: default_cover(), cover: default_cover(),
size: 0, size: 0,
bytes_received: 0, bytes_received: 0,
bytes_handled: 0,
} }
} }

View file

@ -16,6 +16,8 @@ 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),

View file

@ -9,7 +9,7 @@ use crate::{
utils::{default_cover, Error}, utils::{default_cover, Error},
}; };
use super::{errors::PlayerError, Player, PlayerEvent}; use super::{errors::PlayerError, Player, PlayerEvent, AUDIO_FILE_BUFFER};
impl Player { impl Player {
pub fn begin(&mut self) -> Result<(), Error> { pub fn begin(&mut self) -> Result<(), Error> {
@ -100,6 +100,14 @@ impl Player {
} }
fn recv_chunk(&mut self, chunk: Vec<u8>) -> Result<(), Error> { fn recv_chunk(&mut self, chunk: Vec<u8>) -> Result<(), Error> {
if self.tui_root.metadata.bytes_received - self.tui_root.metadata.bytes_handled
> AUDIO_FILE_BUFFER
{
// requeue the new chunk since we don't need it decoded just yet. Mostly for massive hr+ files
self.player_chan_in
.send(PlayerEvent::AddAudioChunk(chunk))?;
return Ok(());
}
self.tui_root.metadata.bytes_received += chunk.len() as u32; self.tui_root.metadata.bytes_received += chunk.len() as u32;
self.audio_chan.send(AudioEvent::DecodeChunk(chunk))?; self.audio_chan.send(AudioEvent::DecodeChunk(chunk))?;
if self.tui_root.metadata.bytes_pending() { if self.tui_root.metadata.bytes_pending() {