Use stream_editor in player

This commit is contained in:
Daniel Romero 2017-01-25 22:56:06 +01:00
parent 6df2af0ac9
commit 636de3fe71
4 changed files with 17 additions and 9 deletions

View file

@ -129,7 +129,7 @@ fn setup(args: &[String]) -> (Session, Player, Box<Mixer + Send>) {
let mixer = mixer::find(&mixer_name).unwrap(); let mixer = mixer::find(&mixer_name).unwrap();
let device_name = matches.opt_str("device"); let device_name = matches.opt_str("device");
let player = Player::new(session.clone(), move || { let player = Player::new(session.clone(), mixer.get_stream_editor(), move || {
(backend)(device_name.as_ref().map(AsRef::as_ref)) (backend)(device_name.as_ref().map(AsRef::as_ref))
}); });

View file

@ -8,7 +8,7 @@ pub trait Mixer {
fn stop(&self); fn stop(&self);
fn set_volume(&self, volume: u16); fn set_volume(&self, volume: u16);
fn volume(&self) -> u16; fn volume(&self) -> u16;
fn get_stream_editor(&self) -> Option<Box<StreamEditor>> fn get_stream_editor(&self) -> Option<Box<StreamEditor + Send>>
{ {
None None
} }

View file

@ -28,14 +28,14 @@ impl Mixer for SoftMixer {
fn set_volume(&self, volume: u16) { fn set_volume(&self, volume: u16) {
*self.volume.write().unwrap() = volume; *self.volume.write().unwrap() = volume;
} }
fn get_stream_editor(&self) -> Option<Box<StreamEditor>> { fn get_stream_editor(&self) -> Option<Box<StreamEditor + Send>> {
let vol = self.volume.clone(); let vol = self.volume.clone();
Some(Box::new(SoftVolumeApplier { get_volume: Box::new(move || *vol.read().unwrap() ) })) Some(Box::new(SoftVolumeApplier { get_volume: Box::new(move || *vol.read().unwrap() ) }))
} }
} }
struct SoftVolumeApplier { struct SoftVolumeApplier {
get_volume: Box<Fn() -> u16> get_volume: Box<Fn() -> u16 + Send>
} }
impl StreamEditor for SoftVolumeApplier { impl StreamEditor for SoftVolumeApplier {

View file

@ -9,6 +9,7 @@ use audio_decrypt::AudioDecrypt;
use audio_backend::Sink; use audio_backend::Sink;
use metadata::{FileFormat, Track, TrackRef}; use metadata::{FileFormat, Track, TrackRef};
use session::{Bitrate, Session}; use session::{Bitrate, Session};
use mixer::StreamEditor;
use util::{self, ReadSeek, SpotifyId, Subfile}; use util::{self, ReadSeek, SpotifyId, Subfile};
pub use spirc::PlayStatus; pub use spirc::PlayStatus;
@ -71,7 +72,7 @@ enum PlayerCommand {
} }
impl Player { impl Player {
pub fn new<F>(session: Session, sink_builder: F) -> Player pub fn new<F>(session: Session, stream_editor: Option<Box<StreamEditor + Send>>, sink_builder: F) -> Player
where F: FnOnce() -> Box<Sink> + Send + 'static { where F: FnOnce() -> Box<Sink> + Send + 'static {
let (cmd_tx, cmd_rx) = mpsc::channel(); let (cmd_tx, cmd_rx) = mpsc::channel();
@ -92,7 +93,7 @@ impl Player {
observers: observers.clone(), observers: observers.clone(),
}; };
thread::spawn(move || internal.run(sink_builder())); thread::spawn(move || internal.run(sink_builder(), stream_editor));
Player { Player {
commands: cmd_tx, commands: cmd_tx,
@ -138,6 +139,10 @@ impl Player {
} }
} }
fn borrow_data<'a>(data: &'a [i16]) -> Cow<'a, [i16]> {
Cow::Borrowed(&data)
}
fn find_available_alternative<'a>(session: &Session, track: &'a Track) -> Option<Cow<'a, Track>> { fn find_available_alternative<'a>(session: &Session, track: &'a Track) -> Option<Cow<'a, Track>> {
if track.available { if track.available {
Some(Cow::Borrowed(track)) Some(Cow::Borrowed(track))
@ -205,7 +210,7 @@ fn run_onstop(session: &Session) {
} }
impl PlayerInternal { impl PlayerInternal {
fn run(self, mut sink: Box<Sink>) { fn run(self, mut sink: Box<Sink>, stream_editor: Option<Box<StreamEditor + Send>>) {
let mut decoder = None; let mut decoder = None;
loop { loop {
@ -340,8 +345,11 @@ impl PlayerInternal {
match packet { match packet {
Some(Ok(packet)) => { Some(Ok(packet)) => {
//let buffer = mixer.apply_volume(&packet.data); let buffer = if let Some(ref editor) = stream_editor {
let buffer = Cow::Borrowed(&packet.data); editor.modify_stream(&packet.data)
} else {
borrow_data(&packet.data)
};
sink.write(&buffer).unwrap(); sink.write(&buffer).unwrap();
self.update(|state| { self.update(|state| {