Skip to content

Commit

Permalink
minor cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
leshow committed Mar 1, 2021
1 parent 9d77fef commit 6a33eb4
Show file tree
Hide file tree
Showing 27 changed files with 166 additions and 206 deletions.
59 changes: 23 additions & 36 deletions audio/src/fetch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -182,59 +182,52 @@ 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());
}
}

pub struct AudioFileStreaming {
read_file: fs::File,

position: u64,

stream_loader_command_tx: mpsc::UnboundedSender<StreamLoaderCommand>,

shared: Arc<AudioFileShared>,
}

Expand Down Expand Up @@ -332,10 +325,7 @@ impl AudioFile {
}

pub fn is_cached(&self) -> bool {
match self {
AudioFile::Cached { .. } => true,
_ => false,
}
matches!(self, AudioFile::Cached { .. })
}
}

Expand All @@ -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(),
Expand Down Expand Up @@ -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,
})
}
}
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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(),
}
Expand Down
10 changes: 5 additions & 5 deletions audio/src/lewton_decoder.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand All @@ -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))),
Expand Down
6 changes: 3 additions & 3 deletions audio/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#![allow(clippy::unused_io_amount)]
#![allow(clippy::unused_io_amount, clippy::too_many_arguments)]

#[macro_use]
extern crate log;
Expand Down Expand Up @@ -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)
}
}

Expand Down
14 changes: 7 additions & 7 deletions audio/src/passthrough_decoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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>(
Expand Down Expand Up @@ -65,7 +65,7 @@ where
)
.unwrap();

return Ok(*stream_serial);
Ok(*stream_serial)
}

pub struct PassthroughDecoder<R: Read + Seek> {
Expand All @@ -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,
});
})
}
}

Expand All @@ -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())),
}
}

Expand Down Expand Up @@ -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));
}
Expand Down
23 changes: 10 additions & 13 deletions audio/src/range_set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}

Expand All @@ -50,19 +47,19 @@ impl RangeSet {
}

pub fn is_empty(&self) -> bool {
return self.ranges.is_empty();
self.ranges.is_empty()
}

pub fn len(&self) -> usize {
self.ranges.iter().map(|r| r.length).sum()
}

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 {
Expand All @@ -73,7 +70,7 @@ impl RangeSet {
return true;
}
}
return false;
false
}

pub fn contained_length_from_value(&self, value: usize) -> usize {
Expand All @@ -84,7 +81,7 @@ impl RangeSet {
return range.end() - value;
}
}
return 0;
0
}

#[allow(dead_code)]
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -240,6 +237,6 @@ impl RangeSet {
}
}

return result;
result
}
}
23 changes: 10 additions & 13 deletions connect/src/discovery.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,34 +5,31 @@ use futures_core::Stream;
use hmac::{Hmac, Mac, NewMac};
use hyper::service::{make_service_fn, service_fn};
use hyper::{Body, Method, Request, Response, StatusCode};
use num_bigint::BigUint;
use serde_json::json;
use sha1::{Digest, Sha1};
use tokio::sync::{mpsc, oneshot};

use std::borrow::Cow;
use std::convert::Infallible;
use std::net::{Ipv4Addr, SocketAddr};
use std::task::{Context, Poll};

#[cfg(feature = "with-dns-sd")]
use dns_sd::DNSService;

#[cfg(not(feature = "with-dns-sd"))]
use libmdns;

use num_bigint::BigUint;
use rand;
use std::collections::BTreeMap;
use std::io;
use std::pin::Pin;
use std::sync::Arc;
use url;

use librespot_core::authentication::Credentials;
use librespot_core::config::ConnectConfig;
use librespot_core::diffie_hellman::{DH_GENERATOR, DH_PRIME};
use librespot_core::util;

use std::borrow::Cow;
use std::collections::BTreeMap;
use std::convert::Infallible;
use std::io;
use std::net::{Ipv4Addr, SocketAddr};
use std::pin::Pin;
use std::sync::Arc;
use std::task::{Context, Poll};

type HmacSha1 = Hmac<Sha1>;

#[derive(Clone)]
Expand Down
Loading

0 comments on commit 6a33eb4

Please sign in to comment.