aboutsummaryrefslogtreecommitdiffstats
path: root/mumd/src/audio.rs
diff options
context:
space:
mode:
authorEskil Q <eskilq@kth.se>2021-01-02 15:34:31 +0100
committerEskil Q <eskilq@kth.se>2021-01-02 15:34:31 +0100
commit33a2da5d267d953fdc91988bc14ce9b0710b3d59 (patch)
tree88a1d5652790468d53e4076e2fc68e3acd097357 /mumd/src/audio.rs
parent6e07c2bc4bba206e15bbe8838a322a5c506be9a1 (diff)
downloadmum-33a2da5d267d953fdc91988bc14ce9b0710b3d59.tar.gz
make opus encoder only yield frame when there is data to send
Diffstat (limited to 'mumd/src/audio.rs')
-rw-r--r--mumd/src/audio.rs24
1 files changed, 15 insertions, 9 deletions
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<S, I> Stream for OpusEncoder<S>
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::<f32>());
- }
- 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::<f32>());
+ }
+ 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();