mirror of
https://github.com/librespot-org/librespot.git
synced 2025-10-03 09:49:31 +02:00
minor cleanup
This commit is contained in:
parent
9d77fef008
commit
6a33eb4efa
27 changed files with 172 additions and 212 deletions
|
@ -138,19 +138,19 @@ impl StreamLoaderController {
|
|||
})
|
||||
}
|
||||
|
||||
fn send_stream_loader_command(&mut self, command: StreamLoaderCommand) {
|
||||
if let Some(ref mut channel) = self.channel_tx {
|
||||
fn send_stream_loader_command(&self, command: StreamLoaderCommand) {
|
||||
if let Some(ref channel) = self.channel_tx {
|
||||
// ignore the error in case the channel has been closed already.
|
||||
let _ = channel.send(command);
|
||||
}
|
||||
}
|
||||
|
||||
pub fn fetch(&mut self, range: Range) {
|
||||
pub fn fetch(&self, range: Range) {
|
||||
// signal the stream loader to fetch a range of the file
|
||||
self.send_stream_loader_command(StreamLoaderCommand::Fetch(range));
|
||||
}
|
||||
|
||||
pub fn fetch_blocking(&mut self, mut range: Range) {
|
||||
pub fn fetch_blocking(&self, mut range: Range) {
|
||||
// signal the stream loader to tech a range of the file and block until it is loaded.
|
||||
|
||||
// ensure the range is within the file's bounds.
|
||||
|
@ -182,47 +182,43 @@ impl StreamLoaderController {
|
|||
{
|
||||
// For some reason, the requested range is neither downloaded nor requested.
|
||||
// This could be due to a network error. Request it again.
|
||||
// We can't use self.fetch here because self can't be borrowed mutably, so we access the channel directly.
|
||||
if let Some(ref mut channel) = self.channel_tx {
|
||||
// ignore the error in case the channel has been closed already.
|
||||
let _ = channel.send(StreamLoaderCommand::Fetch(range));
|
||||
}
|
||||
self.fetch(range);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn fetch_next(&mut self, length: usize) {
|
||||
pub fn fetch_next(&self, length: usize) {
|
||||
if let Some(ref shared) = self.stream_shared {
|
||||
let range = Range {
|
||||
start: shared.read_position.load(atomic::Ordering::Relaxed),
|
||||
length: length,
|
||||
length,
|
||||
};
|
||||
self.fetch(range)
|
||||
}
|
||||
}
|
||||
|
||||
pub fn fetch_next_blocking(&mut self, length: usize) {
|
||||
pub fn fetch_next_blocking(&self, length: usize) {
|
||||
if let Some(ref shared) = self.stream_shared {
|
||||
let range = Range {
|
||||
start: shared.read_position.load(atomic::Ordering::Relaxed),
|
||||
length: length,
|
||||
length,
|
||||
};
|
||||
self.fetch_blocking(range);
|
||||
}
|
||||
}
|
||||
|
||||
pub fn set_random_access_mode(&mut self) {
|
||||
pub fn set_random_access_mode(&self) {
|
||||
// optimise download strategy for random access
|
||||
self.send_stream_loader_command(StreamLoaderCommand::RandomAccessMode());
|
||||
}
|
||||
|
||||
pub fn set_stream_mode(&mut self) {
|
||||
pub fn set_stream_mode(&self) {
|
||||
// optimise download strategy for streaming
|
||||
self.send_stream_loader_command(StreamLoaderCommand::StreamMode());
|
||||
}
|
||||
|
||||
pub fn close(&mut self) {
|
||||
pub fn close(&self) {
|
||||
// terminate stream loading and don't load any more data for this file.
|
||||
self.send_stream_loader_command(StreamLoaderCommand::Close());
|
||||
}
|
||||
|
@ -230,11 +226,8 @@ impl StreamLoaderController {
|
|||
|
||||
pub struct AudioFileStreaming {
|
||||
read_file: fs::File,
|
||||
|
||||
position: u64,
|
||||
|
||||
stream_loader_command_tx: mpsc::UnboundedSender<StreamLoaderCommand>,
|
||||
|
||||
shared: Arc<AudioFileShared>,
|
||||
}
|
||||
|
||||
|
@ -332,10 +325,7 @@ impl AudioFile {
|
|||
}
|
||||
|
||||
pub fn is_cached(&self) -> bool {
|
||||
match self {
|
||||
AudioFile::Cached { .. } => true,
|
||||
_ => false,
|
||||
}
|
||||
matches!(self, AudioFile::Cached { .. })
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -359,7 +349,7 @@ impl AudioFileStreaming {
|
|||
let size = BigEndian::read_u32(&data) as usize * 4;
|
||||
|
||||
let shared = Arc::new(AudioFileShared {
|
||||
file_id: file_id,
|
||||
file_id,
|
||||
file_size: size,
|
||||
stream_data_rate: streaming_data_rate,
|
||||
cond: Condvar::new(),
|
||||
|
@ -396,11 +386,10 @@ impl AudioFileStreaming {
|
|||
|
||||
session.spawn(fetcher);
|
||||
Ok(AudioFileStreaming {
|
||||
read_file: read_file,
|
||||
read_file,
|
||||
position: 0,
|
||||
//seek: seek_tx,
|
||||
stream_loader_command_tx: stream_loader_command_tx,
|
||||
shared: shared,
|
||||
stream_loader_command_tx,
|
||||
shared,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
@ -486,7 +475,7 @@ async fn audio_file_fetch_receive_data(
|
|||
let data_size = data.len();
|
||||
let _ = file_data_tx.send(ReceivedData::Data(PartialFileData {
|
||||
offset: data_offset,
|
||||
data: data,
|
||||
data,
|
||||
}));
|
||||
data_offset += data_size;
|
||||
if request_length < data_size {
|
||||
|
@ -728,14 +717,12 @@ impl AudioFileFetch {
|
|||
));
|
||||
|
||||
AudioFileFetch {
|
||||
session: session,
|
||||
shared: shared,
|
||||
session,
|
||||
shared,
|
||||
output: Some(output),
|
||||
|
||||
file_data_tx: file_data_tx,
|
||||
file_data_rx: file_data_rx,
|
||||
|
||||
stream_loader_command_rx: stream_loader_command_rx,
|
||||
file_data_tx,
|
||||
file_data_rx,
|
||||
stream_loader_command_rx,
|
||||
complete_tx: Some(complete_tx),
|
||||
network_response_times_ms: Vec::new(),
|
||||
}
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
use super::{AudioDecoder, AudioError, AudioPacket};
|
||||
|
||||
use lewton::inside_ogg::OggStreamReader;
|
||||
|
||||
use super::{AudioDecoder, AudioError, AudioPacket};
|
||||
use std::error;
|
||||
use std::fmt;
|
||||
use std::io::{Read, Seek};
|
||||
|
@ -24,16 +25,15 @@ where
|
|||
fn seek(&mut self, ms: i64) -> Result<(), AudioError> {
|
||||
let absgp = ms * 44100 / 1000;
|
||||
match self.0.seek_absgp_pg(absgp as u64) {
|
||||
Ok(_) => return Ok(()),
|
||||
Err(err) => return Err(AudioError::VorbisError(err.into())),
|
||||
Ok(_) => Ok(()),
|
||||
Err(err) => Err(AudioError::VorbisError(err.into())),
|
||||
}
|
||||
}
|
||||
|
||||
fn next_packet(&mut self) -> Result<Option<AudioPacket>, AudioError> {
|
||||
use lewton::audio::AudioReadError::AudioIsHeader;
|
||||
use lewton::OggReadError::NoCapturePatternFound;
|
||||
use lewton::VorbisError::BadAudio;
|
||||
use lewton::VorbisError::OggError;
|
||||
use lewton::VorbisError::{BadAudio, OggError};
|
||||
loop {
|
||||
match self.0.read_dec_packet_itl() {
|
||||
Ok(Some(packet)) => return Ok(Some(AudioPacket::Samples(packet))),
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
#![allow(clippy::unused_io_amount)]
|
||||
#![allow(clippy::unused_io_amount, clippy::too_many_arguments)]
|
||||
|
||||
#[macro_use]
|
||||
extern crate log;
|
||||
|
@ -85,13 +85,13 @@ impl fmt::Display for AudioError {
|
|||
|
||||
impl From<VorbisError> for AudioError {
|
||||
fn from(err: VorbisError) -> AudioError {
|
||||
AudioError::VorbisError(VorbisError::from(err))
|
||||
AudioError::VorbisError(err)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<PassthroughError> for AudioError {
|
||||
fn from(err: PassthroughError) -> AudioError {
|
||||
AudioError::PassthroughError(PassthroughError::from(err))
|
||||
AudioError::PassthroughError(err)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -27,7 +27,7 @@ fn write_headers<T: Read + Seek>(
|
|||
|
||||
// remove un-needed packets
|
||||
rdr.delete_unread_packets();
|
||||
return Ok(stream_serial);
|
||||
Ok(stream_serial)
|
||||
}
|
||||
|
||||
fn get_header<T>(
|
||||
|
@ -65,7 +65,7 @@ where
|
|||
)
|
||||
.unwrap();
|
||||
|
||||
return Ok(*stream_serial);
|
||||
Ok(*stream_serial)
|
||||
}
|
||||
|
||||
pub struct PassthroughDecoder<R: Read + Seek> {
|
||||
|
@ -87,13 +87,13 @@ impl<R: Read + Seek> PassthroughDecoder<R> {
|
|||
let stream_serial = write_headers(&mut rdr, &mut wtr)?;
|
||||
info!("Starting passthrough track with serial {}", stream_serial);
|
||||
|
||||
return Ok(PassthroughDecoder {
|
||||
Ok(PassthroughDecoder {
|
||||
rdr,
|
||||
wtr,
|
||||
lastgp_page: Some(0),
|
||||
absgp_page: 0,
|
||||
stream_serial,
|
||||
});
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -107,8 +107,8 @@ impl<R: Read + Seek> AudioDecoder for PassthroughDecoder<R> {
|
|||
|
||||
// hard-coded to 44.1 kHz
|
||||
match self.rdr.seek_absgp(None, (ms * 44100 / 1000) as u64) {
|
||||
Ok(_) => return Ok(()),
|
||||
Err(err) => return Err(AudioError::PassthroughError(err.into())),
|
||||
Ok(_) => Ok(()),
|
||||
Err(err) => Err(AudioError::PassthroughError(err.into())),
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -164,7 +164,7 @@ impl<R: Read + Seek> AudioDecoder for PassthroughDecoder<R> {
|
|||
|
||||
let data = self.wtr.inner_mut();
|
||||
|
||||
if data.len() > 0 {
|
||||
if !data.is_empty() {
|
||||
let result = AudioPacket::OggData(std::mem::take(data));
|
||||
return Ok(Some(result));
|
||||
}
|
||||
|
|
|
@ -16,14 +16,11 @@ impl fmt::Display for Range {
|
|||
|
||||
impl Range {
|
||||
pub fn new(start: usize, length: usize) -> Range {
|
||||
return Range {
|
||||
start: start,
|
||||
length: length,
|
||||
};
|
||||
Range { start, length }
|
||||
}
|
||||
|
||||
pub fn end(&self) -> usize {
|
||||
return self.start + self.length;
|
||||
self.start + self.length
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -50,7 +47,7 @@ impl RangeSet {
|
|||
}
|
||||
|
||||
pub fn is_empty(&self) -> bool {
|
||||
return self.ranges.is_empty();
|
||||
self.ranges.is_empty()
|
||||
}
|
||||
|
||||
pub fn len(&self) -> usize {
|
||||
|
@ -58,11 +55,11 @@ impl RangeSet {
|
|||
}
|
||||
|
||||
pub fn get_range(&self, index: usize) -> Range {
|
||||
return self.ranges[index].clone();
|
||||
self.ranges[index]
|
||||
}
|
||||
|
||||
pub fn iter(&self) -> Iter<Range> {
|
||||
return self.ranges.iter();
|
||||
self.ranges.iter()
|
||||
}
|
||||
|
||||
pub fn contains(&self, value: usize) -> bool {
|
||||
|
@ -73,7 +70,7 @@ impl RangeSet {
|
|||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
false
|
||||
}
|
||||
|
||||
pub fn contained_length_from_value(&self, value: usize) -> usize {
|
||||
|
@ -84,7 +81,7 @@ impl RangeSet {
|
|||
return range.end() - value;
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
0
|
||||
}
|
||||
|
||||
#[allow(dead_code)]
|
||||
|
@ -144,7 +141,7 @@ impl RangeSet {
|
|||
pub fn union(&self, other: &RangeSet) -> RangeSet {
|
||||
let mut result = self.clone();
|
||||
result.add_range_set(other);
|
||||
return result;
|
||||
result
|
||||
}
|
||||
|
||||
pub fn subtract_range(&mut self, range: &Range) {
|
||||
|
@ -204,7 +201,7 @@ impl RangeSet {
|
|||
pub fn minus(&self, other: &RangeSet) -> RangeSet {
|
||||
let mut result = self.clone();
|
||||
result.subtract_range_set(other);
|
||||
return result;
|
||||
result
|
||||
}
|
||||
|
||||
pub fn intersection(&self, other: &RangeSet) -> RangeSet {
|
||||
|
@ -240,6 +237,6 @@ impl RangeSet {
|
|||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
result
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue