From b4bbf5e7770d3510034ba3f6410b4daa5fc9dc51 Mon Sep 17 00:00:00 2001 From: Aron Heinecke Date: Tue, 11 May 2021 00:17:26 +0200 Subject: [PATCH] Fix audio encoder Signed-off-by: Aron Heinecke --- src/discord.rs | 2 +- src/main.rs | 21 +++++++++++---------- 2 files changed, 12 insertions(+), 11 deletions(-) diff --git a/src/discord.rs b/src/discord.rs index 2260902..38b67e2 100644 --- a/src/discord.rs +++ b/src/discord.rs @@ -394,7 +394,7 @@ impl VoiceEventHandler for Receiver { let time = std::time::Instant::now(); let mut lock = self.sink.lock().await; let dur = time.elapsed(); - if dur > Duration::from_nanos(500) { + if dur.as_millis() > 1 { eprintln!("Akquiring lock took {}ms",dur.as_millis()); } if let Some(buffer) = lock.get_mut(&packet.ssrc) { diff --git a/src/main.rs b/src/main.rs index 81c62b0..708ccd0 100644 --- a/src/main.rs +++ b/src/main.rs @@ -153,7 +153,12 @@ async fn main() -> Result<()> { // a2t.set_volume(config.volume); // a2t.set_playing(true); // } - + let encoder = audiopus::coder::Encoder::new( + audiopus::SampleRate::Hz48000, + audiopus::Channels::Stereo, + audiopus::Application::Voip) + .expect("Can't construct encoder!"); + let encoder = Arc::new(Mutex::new(encoder)); let mut interval = tokio::time::interval(Duration::from_millis(TICK_TIME)); // tokio::spawn(async { @@ -207,14 +212,13 @@ async fn main() -> Result<()> { //eprintln!("Tick took {}ms",dur.as_millis()); } let start = std::time::Instant::now(); - if let Some(processed) = process_audio(&voice_buffer).await { + if let Some(processed) = process_audio(&voice_buffer,&encoder).await { con.send_audio(processed)?; let dur = start.elapsed(); if dur >= Duration::from_millis(1) { eprintln!("Audio pipeline took {}ms",dur.as_millis()); } } - } _ = tokio::signal::ctrl_c() => { break; } r = events => { @@ -233,7 +237,7 @@ async fn main() -> Result<()> { -async fn process_audio(voice_buffer: &AudioBufferDiscord) -> Option { +async fn process_audio(voice_buffer: &AudioBufferDiscord, encoder: &Arc>) -> Option { let mut buffer_map; { let mut lock = voice_buffer.lock().await; @@ -243,6 +247,7 @@ async fn process_audio(voice_buffer: &AudioBufferDiscord) -> Option { return None; } let mut encoded = [0; 1024]; + let encoder_c = encoder.clone(); let res = task::spawn_blocking(move || { let start = std::time::Instant::now(); let mut data: Vec = Vec::with_capacity(STEREO_20MS); @@ -258,13 +263,9 @@ async fn process_audio(voice_buffer: &AudioBufferDiscord) -> Option { } } - let encoder = audiopus::coder::Encoder::new( - audiopus::SampleRate::Hz48000, - audiopus::Channels::Stereo, - audiopus::Application::Voip) - .expect("Can't construct encoder!"); - let length = match encoder.encode(&data, &mut encoded) { + let lock = encoder_c.try_lock().expect("Can't reach encoder!"); + let length = match lock.encode(&data, &mut encoded) { Err(e) => {eprintln!("Failed to encode voice: {}",e); return None;}, Ok(size) => size, };