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 spirc::UpdateMessageSender;
use self::softmixer::SoftMixer;
pub mod softmixer;
pub trait Mixer {
fn init(&self);
fn init(&mut self, UpdateMessageSender);
fn start(&self);
fn stop(&self);
fn set_volume(&self, volume: u16);

View file

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

View file

@ -68,6 +68,11 @@ implement_sender!(name => MercuryResponseSender,
with => SpircMessage,
variant => MercuryMsg);
implement_sender!(name => UpdateMessageSender,
wrap => UpdateMessage,
with => SpircMessage,
variant => UpdateMsg);
impl SpircManager {
pub fn new(session: Session, player: Player, mixer: Box<Mixer + Send>) -> SpircManager {
let ident = session.device_id().to_owned();
@ -110,6 +115,10 @@ impl SpircManager {
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);
// Use a weak pointer to avoid creating an Rc cycle between the player and the
@ -127,20 +136,22 @@ impl SpircManager {
for msg in rx {
match msg {
SpircMessage::MercuryMsg(pkt) => {
let data = pkt.payload.first().unwrap();
let frame = protobuf::parse_from_bytes::<protocol::spirc::Frame>(data).unwrap();
SpircMessage::MercuryMsg(pkt) => {
let data = pkt.payload.first().unwrap();
let frame = protobuf::parse_from_bytes::<protocol::spirc::Frame>(data).unwrap();
debug!("{:?} {:?} {} {} {}",
frame.get_typ(),
frame.get_device_state().get_name(),
frame.get_ident(),
frame.get_seq_nr(),
frame.get_state_update_id());
debug!("{:?} {:?} {} {} {}",
frame.get_typ(),
frame.get_device_state().get_name(),
frame.get_ident(),
frame.get_seq_nr(),
frame.get_state_update_id());
self.0.lock().unwrap().handle(frame);
}
_ => {}
self.0.lock().unwrap().handle(frame);
}
SpircMessage::UpdateMsg(_) => {
self.0.lock().unwrap().notify(false, None);
}
}
}
}