Send update messages from mixer to SpircManager

This commit is contained in:
Daniel Romero 2017-02-01 21:32:14 +01:00
parent c8ee08663d
commit 134239d298
3 changed files with 41 additions and 18 deletions

View file

@ -1,10 +1,13 @@
use std::borrow::Cow; use std::borrow::Cow;
use spirc::UpdateMessageSender;
use self::softmixer::SoftMixer; use self::softmixer::SoftMixer;
pub mod softmixer; pub mod softmixer;
pub trait Mixer { pub trait Mixer {
fn init(&self); fn init(&mut self, UpdateMessageSender);
fn start(&self); fn start(&self);
fn stop(&self); fn stop(&self);
fn set_volume(&self, volume: u16); fn set_volume(&self, volume: u16);

View file

@ -1,22 +1,29 @@
use super::Mixer;
use super::AudioFilter;
use std::borrow::Cow; use std::borrow::Cow;
use std::sync::{Arc, RwLock}; use std::sync::{Arc, RwLock};
use spirc::UpdateMessageSender;
use spirc::UpdateMessage;
use super::Mixer;
use super::AudioFilter;
pub struct SoftMixer { pub struct SoftMixer {
volume: Arc<RwLock<u16>> volume: Arc<RwLock<u16>>,
tx: Option<UpdateMessageSender>
} }
impl SoftMixer { impl SoftMixer {
pub fn new() -> SoftMixer { pub fn new() -> SoftMixer {
SoftMixer { SoftMixer {
volume: Arc::new(RwLock::new(0xFFFF)) volume: Arc::new(RwLock::new(0xFFFF)),
tx: None
} }
} }
} }
impl Mixer for SoftMixer { impl Mixer for SoftMixer {
fn init(&self) { fn init(&mut self, tx: UpdateMessageSender) {
self.tx = Some(tx);
} }
fn start(&self) { fn start(&self) {
} }
@ -27,6 +34,8 @@ 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;
let tx = self.tx.as_ref().expect("SoftMixer not initialized");
tx.send(UpdateMessage).unwrap();
} }
fn get_audio_filter(&self) -> Option<Box<AudioFilter + Send>> { fn get_audio_filter(&self) -> Option<Box<AudioFilter + Send>> {
let vol = self.volume.clone(); let vol = self.volume.clone();

View file

@ -68,6 +68,11 @@ implement_sender!(name => MercuryResponseSender,
with => SpircMessage, with => SpircMessage,
variant => MercuryMsg); variant => MercuryMsg);
implement_sender!(name => UpdateMessageSender,
wrap => UpdateMessage,
with => SpircMessage,
variant => UpdateMsg);
impl SpircManager { impl SpircManager {
pub fn new(session: Session, player: Player, mixer: Box<Mixer + Send>) -> SpircManager { pub fn new(session: Session, player: Player, mixer: Box<Mixer + Send>) -> SpircManager {
let ident = session.device_id().to_owned(); let ident = session.device_id().to_owned();
@ -110,6 +115,10 @@ impl SpircManager {
internal.session.mercury_sub(internal.uri(), mercury_response_sender); internal.session.mercury_sub(internal.uri(), mercury_response_sender);
let update_message_sender = UpdateMessageSender::create(tx.clone());
internal.mixer.init(update_message_sender);
internal.notify(true, None); internal.notify(true, None);
// Use a weak pointer to avoid creating an Rc cycle between the player and the // Use a weak pointer to avoid creating an Rc cycle between the player and the
@ -140,7 +149,9 @@ impl SpircManager {
self.0.lock().unwrap().handle(frame); self.0.lock().unwrap().handle(frame);
} }
_ => {} SpircMessage::UpdateMsg(_) => {
self.0.lock().unwrap().notify(false, None);
}
} }
} }
} }