From 33a2da5d267d953fdc91988bc14ce9b0710b3d59 Mon Sep 17 00:00:00 2001 From: Eskil Q Date: Sat, 2 Jan 2021 15:34:31 +0100 Subject: make opus encoder only yield frame when there is data to send --- mumd/src/audio.rs | 24 +++++++++++++++--------- 1 file changed, 15 insertions(+), 9 deletions(-) (limited to 'mumd/src') diff --git a/mumd/src/audio.rs b/mumd/src/audio.rs index d4ef4d6..6d69e36 100644 --- a/mumd/src/audio.rs +++ b/mumd/src/audio.rs @@ -707,17 +707,23 @@ impl Stream for OpusEncoder return Poll::Ready(None); } let opus_frame_size = (s.frame_size * s.sample_rate / 400) as usize; - while s.input_buffer.len() < opus_frame_size { - match S::poll_next(Pin::new(&mut s.stream), cx) { - Poll::Ready(Some(v)) => { - s.input_buffer.push(v.to_sample::()); - } - Poll::Ready(None) => { - s.exhausted = true; - return Poll::Ready(None); + loop { + while s.input_buffer.len() < opus_frame_size { + match S::poll_next(Pin::new(&mut s.stream), cx) { + Poll::Ready(Some(v)) => { + s.input_buffer.push(v.to_sample::()); + } + Poll::Ready(None) => { + s.exhausted = true; + return Poll::Ready(None); + } + Poll::Pending => return Poll::Pending, } - Poll::Pending => return Poll::Pending, } + if s.input_buffer.iter().any(|&e| e != 0.0) { + break; + } + s.input_buffer.clear(); } let encoded = s.encoder.encode_vec_float(&s.input_buffer, opus_frame_size).unwrap(); -- cgit v1.2.1