From c50fc9885ac296799416895f851f3c715863aaba Mon Sep 17 00:00:00 2001 From: Konstantin Seiler Date: Wed, 6 Nov 2019 08:16:01 +1100 Subject: [PATCH] Adapt code for the new bitrate selection via alternatives. --- audio/src/fetch.rs | 14 +++++++++++--- playback/src/player.rs | 28 ++++++++++++++++++---------- 2 files changed, 29 insertions(+), 13 deletions(-) diff --git a/audio/src/fetch.rs b/audio/src/fetch.rs index e17473ed..e3d63dfb 100644 --- a/audio/src/fetch.rs +++ b/audio/src/fetch.rs @@ -57,6 +57,7 @@ pub struct StreamLoaderController { channel_tx: Option>, stream_shared: Option>, file_size: usize, + bytes_per_second: usize, } @@ -65,6 +66,8 @@ impl StreamLoaderController { return self.file_size; } + pub fn data_rate(&self) -> usize { return self.bytes_per_second; } + pub fn range_available(&self, range: Range) -> bool { if let Some(ref shared) = self.stream_shared { let download_status = shared.download_status.lock().unwrap(); @@ -168,6 +171,7 @@ impl StreamLoaderController { pub fn set_stream_data_rate(&mut self, bytes_per_second: usize) { // when optimising for streaming, assume a streaming rate of this many bytes per second. + self.bytes_per_second = bytes_per_second; self.send_stream_loader_command(StreamLoaderCommand::StreamDataRate(bytes_per_second)); } @@ -335,20 +339,24 @@ impl AudioFile { AudioFileOpen::Streaming(open) } - pub fn get_stream_loader_controller(&self) -> StreamLoaderController { + pub fn get_stream_loader_controller(&self, bytes_per_second: usize) -> StreamLoaderController { match self { AudioFile::Streaming(stream) => { - return StreamLoaderController { + let mut result = StreamLoaderController { channel_tx: Some(stream.stream_loader_command_tx.clone()), stream_shared: Some(stream.shared.clone()), file_size: stream.shared.file_size, - } + bytes_per_second: bytes_per_second, + }; + result.set_stream_data_rate(bytes_per_second); + return result; } AudioFile::Cached(ref file) => { return StreamLoaderController { channel_tx: None, stream_shared: None, file_size: file.metadata().unwrap().len() as usize, + bytes_per_second: bytes_per_second, } } } diff --git a/playback/src/player.rs b/playback/src/player.rs index 23121785..32500e30 100644 --- a/playback/src/player.rs +++ b/playback/src/player.rs @@ -490,12 +490,12 @@ impl PlayerInternal { } // If we're playing, ensure, that we have enough data leaded to avoid a buffer underrun. - let stream_data_rate = self.stream_data_rate(); if let Some(stream_loader_controller) = self.state.stream_loader_controller() { stream_loader_controller.set_stream_mode(); } if let PlayerState::Playing{..} = self.state { if let Some(stream_loader_controller) = self.state.stream_loader_controller() { + let stream_data_rate = stream_loader_controller.data_rate(); let wait_for_data_length = (2 * stream_loader_controller.ping_time_ms() * stream_data_rate) / 1000; stream_loader_controller.fetch_next_blocking(wait_for_data_length); } @@ -561,11 +561,22 @@ impl PlayerInternal { } } - fn stream_data_rate(&self) -> usize { - match self.config.bitrate { - Bitrate::Bitrate96 => 12 * 1024, - Bitrate::Bitrate160 => 20 * 1024, - Bitrate::Bitrate320 => 40 * 1024, + fn stream_data_rate(&self, format: FileFormat) -> usize { + match format { + FileFormat::OGG_VORBIS_96 => 12 * 1024, + FileFormat::OGG_VORBIS_160 => 20 * 1024, + FileFormat::OGG_VORBIS_320=> 40 * 1024, + FileFormat::MP3_256 => 32 * 1024, + FileFormat::MP3_320 => 40 * 1024, + FileFormat::MP3_160 => 20 * 1024, + FileFormat::MP3_96 => 12 * 1024, + FileFormat::MP3_160_ENC => 20 * 1024, + FileFormat::MP4_128_DUAL => 16 * 1024, + FileFormat::OTHER3 => 40 * 1024, // better some high guess than nothing + FileFormat::AAC_160 => 20 * 1024, + FileFormat::AAC_320 => 40 * 1024, + FileFormat::MP4_128 => 16 * 1024, + FileFormat::OTHER5 => 40 * 1024, // better some high guess than nothing } } @@ -618,10 +629,7 @@ impl PlayerInternal { let encrypted_file = encrypted_file.wait().unwrap(); - let mut stream_loader_controller = encrypted_file.get_stream_loader_controller(); - - // tell the stream loader how to optimise its strategy. - stream_loader_controller.set_stream_data_rate(self.stream_data_rate()); + let mut stream_loader_controller = encrypted_file.get_stream_loader_controller(self.stream_data_rate(*format)); if position == 0 { // No need to seek -> we stream from the beginning