From 55644de7b35421997198c9dec4a8bba5dfb8dd8d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gustav=20S=C3=B6rn=C3=A4s?= Date: Tue, 5 Jan 2021 12:47:04 +0100 Subject: add voice stream type --- mumd/src/audio.rs | 47 ++++++++++++++++++++++++++--------------------- 1 file changed, 26 insertions(+), 21 deletions(-) (limited to 'mumd/src/audio.rs') diff --git a/mumd/src/audio.rs b/mumd/src/audio.rs index 680433c..40cdcb2 100644 --- a/mumd/src/audio.rs +++ b/mumd/src/audio.rs @@ -2,6 +2,7 @@ pub mod input; pub mod output; use crate::audio::output::SaturatingAdd; +use crate::network::VoiceStreamType; use cpal::traits::{DeviceTrait, HostTrait, StreamTrait}; use cpal::{SampleFormat, SampleRate, StreamConfig}; @@ -82,7 +83,7 @@ pub struct Audio { user_volumes: Arc>>, - client_streams: Arc>>, + client_streams: Arc>>, sounds: HashMap>, play_sounds: Arc>>, @@ -291,8 +292,8 @@ impl Audio { .collect(); } - pub fn decode_packet(&self, session_id: u32, payload: VoicePacketPayload) { - match self.client_streams.lock().unwrap().entry(session_id) { + pub fn decode_packet(&self, stream_type: VoiceStreamType, session_id: u32, payload: VoicePacketPayload) { + match self.client_streams.lock().unwrap().entry((stream_type, session_id)) { Entry::Occupied(mut entry) => { entry .get_mut() @@ -305,29 +306,33 @@ impl Audio { } pub fn add_client(&self, session_id: u32) { - match self.client_streams.lock().unwrap().entry(session_id) { - Entry::Occupied(_) => { - warn!("Session id {} already exists", session_id); - } - Entry::Vacant(entry) => { - entry.insert(output::ClientStream::new( - self.output_config.sample_rate.0, - self.output_config.channels, - )); + for stream_type in [VoiceStreamType::TCP, VoiceStreamType::UDP].iter() { + match self.client_streams.lock().unwrap().entry((*stream_type, session_id)) { + Entry::Occupied(_) => { + warn!("Session id {} already exists", session_id); + } + Entry::Vacant(entry) => { + entry.insert(output::ClientStream::new( + self.output_config.sample_rate.0, + self.output_config.channels, + )); + } } } } pub fn remove_client(&self, session_id: u32) { - match self.client_streams.lock().unwrap().entry(session_id) { - Entry::Occupied(entry) => { - entry.remove(); - } - Entry::Vacant(_) => { - warn!( - "Tried to remove session id {} that doesn't exist", - session_id - ); + for stream_type in [VoiceStreamType::TCP, VoiceStreamType::UDP].iter() { + match self.client_streams.lock().unwrap().entry((*stream_type, session_id)) { + Entry::Occupied(entry) => { + entry.remove(); + } + Entry::Vacant(_) => { + warn!( + "Tried to remove session id {} that doesn't exist", + session_id + ); + } } } } -- cgit v1.2.1 From ab038b58b4440804cdfded56167ce72b599d87c8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gustav=20S=C3=B6rn=C3=A4s?= Date: Tue, 5 Jan 2021 17:08:48 +0100 Subject: yikes --- mumd/src/audio.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'mumd/src/audio.rs') diff --git a/mumd/src/audio.rs b/mumd/src/audio.rs index 40cdcb2..4f9b73c 100644 --- a/mumd/src/audio.rs +++ b/mumd/src/audio.rs @@ -337,7 +337,7 @@ impl Audio { } } - pub fn take_receiver(&mut self) -> Arc> + Unpin>>> { + pub fn input_receiver(&self) -> Arc> + Unpin>>> { Arc::clone(&self.input_channel_receiver) } -- cgit v1.2.1 From b15e010a6bebc7b7c6b8afb1b51f2673d0695e06 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gustav=20S=C3=B6rn=C3=A4s?= Date: Tue, 5 Jan 2021 20:02:32 +0100 Subject: tokio mutex --- mumd/src/audio.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'mumd/src/audio.rs') diff --git a/mumd/src/audio.rs b/mumd/src/audio.rs index 4f9b73c..3f03e61 100644 --- a/mumd/src/audio.rs +++ b/mumd/src/audio.rs @@ -76,7 +76,7 @@ pub struct Audio { _output_stream: cpal::Stream, _input_stream: cpal::Stream, - input_channel_receiver: Arc> + Unpin>>>, + input_channel_receiver: Arc> + Unpin>>>, input_volume_sender: watch::Sender, output_volume_sender: watch::Sender, @@ -227,7 +227,7 @@ impl Audio { _output_stream: output_stream, _input_stream: input_stream, input_volume_sender, - input_channel_receiver: Arc::new(Mutex::new(Box::new(opus_stream))), + input_channel_receiver: Arc::new(tokio::sync::Mutex::new(Box::new(opus_stream))), client_streams, sounds: HashMap::new(), output_volume_sender, @@ -337,7 +337,7 @@ impl Audio { } } - pub fn input_receiver(&self) -> Arc> + Unpin>>> { + pub fn input_receiver(&self) -> Arc> + Unpin>>> { Arc::clone(&self.input_channel_receiver) } -- cgit v1.2.1 From 92d5b21bf0f910f219c473002f83ba93ddcbee6d Mon Sep 17 00:00:00 2001 From: Eskil Q Date: Wed, 6 Jan 2021 23:50:09 +0100 Subject: fix deadlock --- mumd/src/audio.rs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) (limited to 'mumd/src/audio.rs') diff --git a/mumd/src/audio.rs b/mumd/src/audio.rs index 3f03e61..bdc8377 100644 --- a/mumd/src/audio.rs +++ b/mumd/src/audio.rs @@ -31,7 +31,7 @@ use std::{ }; use strum::IntoEnumIterator; use strum_macros::EnumIter; -use tokio::sync::watch; +use tokio::sync::{watch}; const SAMPLE_RATE: u32 = 48000; @@ -132,11 +132,11 @@ impl Audio { let err_fn = |err| error!("An error occurred on the output audio stream: {}", err); - let user_volumes = Arc::new(Mutex::new(HashMap::new())); + let user_volumes = Arc::new(std::sync::Mutex::new(HashMap::new())); let (output_volume_sender, output_volume_receiver) = watch::channel::(output_volume); - let play_sounds = Arc::new(Mutex::new(VecDeque::new())); + let play_sounds = Arc::new(std::sync::Mutex::new(VecDeque::new())); - let client_streams = Arc::new(Mutex::new(HashMap::new())); + let client_streams = Arc::new(std::sync::Mutex::new(HashMap::new())); let output_stream = match output_supported_sample_format { SampleFormat::F32 => output_device.build_output_stream( &output_config, @@ -292,7 +292,7 @@ impl Audio { .collect(); } - pub fn decode_packet(&self, stream_type: VoiceStreamType, session_id: u32, payload: VoicePacketPayload) { + pub fn decode_packet_payload(&self, stream_type: VoiceStreamType, session_id: u32, payload: VoicePacketPayload) { match self.client_streams.lock().unwrap().entry((stream_type, session_id)) { Entry::Occupied(mut entry) => { entry -- cgit v1.2.1 From 8b042801d090e1a17ca72ddb559d92ccbbb41091 Mon Sep 17 00:00:00 2001 From: Eskil Q Date: Thu, 7 Jan 2021 12:02:43 +0100 Subject: update according to feedback --- mumd/src/audio.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'mumd/src/audio.rs') diff --git a/mumd/src/audio.rs b/mumd/src/audio.rs index bdc8377..598dde6 100644 --- a/mumd/src/audio.rs +++ b/mumd/src/audio.rs @@ -31,7 +31,7 @@ use std::{ }; use strum::IntoEnumIterator; use strum_macros::EnumIter; -use tokio::sync::{watch}; +use tokio::sync::watch; const SAMPLE_RATE: u32 = 48000; -- cgit v1.2.1