BlueGit

ict rusty-teams

feat(call): ask Teams for fullband Opus

- Offer adds maxplaybackrate=48000;sprop-maxcapturerate=48000 to the Opus fmtp
- Deviation: the Teams web client offer leaves these out (RFC default 48 kHz); Teams answered with 16000, so we state fullband explicitly
- Diagnostics log the far-end energy share above 8 kHz every 10 s to prove the received bandwidth

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

metis Daniel Däschle · pushed by metis

Authored 2026-11-11 · 2 weeks ago

Hash
ddfbb9a31929a721f81dda92a10f041ece7829a2
Parent
f77d4e96
5 files changed +105 −2
crates/calling/src/band_meter.rs added +81 −0
@@ -0,0 +1,81 @@
1 use crate::audio::SAMPLE_RATE;
2
3 const SPLIT_HERTZ: f32 = 8000.;
4 const QUALITY: f32 = std::f32::consts::FRAC_1_SQRT_2;
5 const SILENCE_ENERGY: f64 = 1e6;
6
7 pub struct HighBandMeter {
8 coefficients: [f32; 5],
9 history: [f32; 4],
10 total_energy: f64,
11 high_energy: f64,
12 }
13
14 impl Default for HighBandMeter {
15 fn default() -> Self {
16 let omega = 2. * std::f32::consts::PI * SPLIT_HERTZ / SAMPLE_RATE as f32;
17 let alpha = omega.sin() / (2. * QUALITY);
18 let cosine = omega.cos();
19 let a0 = 1. + alpha;
20 let b0 = (1. + cosine) / 2. / a0;
21 HighBandMeter {
22 coefficients: [b0, -(1. + cosine) / a0, b0, -2. * cosine / a0, (1. - alpha) / a0],
23 history: [0.; 4],
24 total_energy: 0.,
25 high_energy: 0.,
26 }
27 }
28 }
29
30 impl HighBandMeter {
31 pub fn add(&mut self, frame: &[i16]) {
32 let [b0, b1, b2, a1, a2] = self.coefficients;
33 for &sample in frame {
34 let input = f32::from(sample);
35 let [x1, x2, y1, y2] = self.history;
36 let output = b0 * input + b1 * x1 + b2 * x2 - a1 * y1 - a2 * y2;
37 self.history = [input, x1, output, y1];
38 self.total_energy += f64::from(input * input);
39 self.high_energy += f64::from(output * output);
40 }
41 }
42
43 pub fn take_share(&mut self) -> Option<f64> {
44 let share = (self.total_energy > SILENCE_ENERGY).then(|| self.high_energy / self.total_energy);
45 self.total_energy = 0.;
46 self.high_energy = 0.;
47 share
48 }
49 }
50
51 #[cfg(test)]
52 mod tests {
53 use super::*;
54
55 fn sine(hertz: f32) -> Vec<i16> {
56 (0..SAMPLE_RATE as usize)
57 .map(|index| ((2. * std::f32::consts::PI * hertz * index as f32 / SAMPLE_RATE as f32).sin() * 8000.) as i16)
58 .collect()
59 }
60
61 #[test]
62 fn wideband_speech_has_nothing_above_eight_kilohertz() {
63 let mut meter = HighBandMeter::default();
64 meter.add(&sine(1000.));
65 assert!(meter.take_share().unwrap() < 0.01);
66 }
67
68 #[test]
69 fn fullband_content_shows_up_above_eight_kilohertz() {
70 let mut meter = HighBandMeter::default();
71 meter.add(&sine(14000.));
72 assert!(meter.take_share().unwrap() > 0.9);
73 }
74
75 #[test]
76 fn silence_reports_nothing() {
77 let mut meter = HighBandMeter::default();
78 meter.add(&[0; 4800]);
79 assert_eq!(meter.take_share(), None);
80 }
81 }
@@ -0,0 +1,81 @@
1use crate::audio::SAMPLE_RATE;
2
3const SPLIT_HERTZ: f32 = 8000.;
4const QUALITY: f32 = std::f32::consts::FRAC_1_SQRT_2;
5const SILENCE_ENERGY: f64 = 1e6;
6
7pub struct HighBandMeter {
8 coefficients: [f32; 5],
9 history: [f32; 4],
10 total_energy: f64,
11 high_energy: f64,
12}
13
14impl Default for HighBandMeter {
15 fn default() -> Self {
16 let omega = 2. * std::f32::consts::PI * SPLIT_HERTZ / SAMPLE_RATE as f32;
17 let alpha = omega.sin() / (2. * QUALITY);
18 let cosine = omega.cos();
19 let a0 = 1. + alpha;
20 let b0 = (1. + cosine) / 2. / a0;
21 HighBandMeter {
22 coefficients: [b0, -(1. + cosine) / a0, b0, -2. * cosine / a0, (1. - alpha) / a0],
23 history: [0.; 4],
24 total_energy: 0.,
25 high_energy: 0.,
26 }
27 }
28}
29
30impl HighBandMeter {
31 pub fn add(&mut self, frame: &[i16]) {
32 let [b0, b1, b2, a1, a2] = self.coefficients;
33 for &sample in frame {
34 let input = f32::from(sample);
35 let [x1, x2, y1, y2] = self.history;
36 let output = b0 * input + b1 * x1 + b2 * x2 - a1 * y1 - a2 * y2;
37 self.history = [input, x1, output, y1];
38 self.total_energy += f64::from(input * input);
39 self.high_energy += f64::from(output * output);
40 }
41 }
42
43 pub fn take_share(&mut self) -> Option<f64> {
44 let share = (self.total_energy > SILENCE_ENERGY).then(|| self.high_energy / self.total_energy);
45 self.total_energy = 0.;
46 self.high_energy = 0.;
47 share
48 }
49}
50
51#[cfg(test)]
52mod tests {
53 use super::*;
54
55 fn sine(hertz: f32) -> Vec<i16> {
56 (0..SAMPLE_RATE as usize)
57 .map(|index| ((2. * std::f32::consts::PI * hertz * index as f32 / SAMPLE_RATE as f32).sin() * 8000.) as i16)
58 .collect()
59 }
60
61 #[test]
62 fn wideband_speech_has_nothing_above_eight_kilohertz() {
63 let mut meter = HighBandMeter::default();
64 meter.add(&sine(1000.));
65 assert!(meter.take_share().unwrap() < 0.01);
66 }
67
68 #[test]
69 fn fullband_content_shows_up_above_eight_kilohertz() {
70 let mut meter = HighBandMeter::default();
71 meter.add(&sine(14000.));
72 assert!(meter.take_share().unwrap() > 0.9);
73 }
74
75 #[test]
76 fn silence_reports_nothing() {
77 let mut meter = HighBandMeter::default();
78 meter.add(&[0; 4800]);
79 assert_eq!(meter.take_share(), None);
80 }
81}
crates/calling/src/lib.rs +1 −0
@@ -3,6 +3,7 @@ pub mod audio;
3 3 pub mod audio_io;
4 4 pub mod awake;
5 5 pub mod background;
6 pub mod band_meter;
6 7 pub mod call;
7 8 pub mod candidates;
8 9 pub mod callee_gate;
@@ -3,6 +3,7 @@ pub mod audio;
3pub mod audio_io;
3pub mod audio_io;
4pub mod awake;
4pub mod awake;
5pub mod background;
5pub mod background;
6pub mod band_meter;
6pub mod call;
7pub mod call;
7pub mod candidates;
8pub mod candidates;
8pub mod callee_gate;
9pub mod callee_gate;
crates/calling/src/sdp.rs +11 −1
@@ -445,8 +445,11 @@ fn signal_rtp_line(
445 445 "fmtp" => {
446 446 let payload = value.split(' ').next().unwrap_or_default();
447 447 let position = source.formats.iter().position(|format| format == payload).unwrap_or(usize::MAX);
448 let is_opus = rtpmap.get(payload).is_some_and(|encoding| encoding.to_ascii_lowercase().starts_with("opus/"));
448 449 let cleaned = if video {
449 450 value.replace(";sps-pps-idr-in-keyframe=1", "")
451 } else if is_opus {
452 with_fmtp_parameters(value, &OPUS_RECEIVE_FULLBAND)
450 453 } else {
451 454 value.to_owned()
452 455 };
@@ -648,10 +651,16 @@ fn transport_attributes(teams_line: &Media, session: &Session) -> Vec<Attribute>
648 651
649 652 const OPUS_SEND_QUALITY: [(&str, &str); 2] = [("maxaveragebitrate", "40000"), ("maxplaybackrate", "48000")];
650 653
654 const OPUS_RECEIVE_FULLBAND: [(&str, &str); 2] = [("maxplaybackrate", "48000"), ("sprop-maxcapturerate", "48000")];
655
651 656 /// libwebrtc sizes its Opus encoder from these remote fmtp parameters; without them it picks its own bitrate.
652 657 fn with_opus_send_quality(fmtp: &str) -> String {
658 with_fmtp_parameters(fmtp, &OPUS_SEND_QUALITY)
659 }
660
661 fn with_fmtp_parameters(fmtp: &str, parameters: &[(&str, &str)]) -> String {
653 662 let mut value = fmtp.to_owned();
654 for (name, setting) in OPUS_SEND_QUALITY {
663 for &(name, setting) in parameters {
655 664 if value.split([' ', ';']).any(|parameter| parameter.split('=').next() == Some(name)) {
656 665 continue;
657 666 }
@@ -1118,6 +1127,7 @@ mod tests {
1118 1127 assert!(text.contains("a=x-signaling-fb:* x-message app recv:dsh\n"));
1119 1128 assert!(text.contains("a=x-ssrc-range:42-42\n"));
1120 1129 assert!(text.contains("a=rtpmap:63 RED/8000\n"));
1130 assert!(text.contains("a=fmtp:111 minptime=10;useinbandfec=1;maxplaybackrate=48000;sprop-maxcapturerate=48000\n"));
1121 1131 assert!(text.contains("a=rtcp:1234\n"));
1122 1132 assert!(text.contains("a=extmap:2 http:\\\\www.webrtc.org\\experiments\\rtp-hdrext\\abs-send-time\n"));
1123 1133 assert!(text.contains("a=ssrc:42 msid:stream 0\n"));
@@ -445,8 +445,11 @@ fn signal_rtp_line(
445 "fmtp" => {
445 "fmtp" => {
446 let payload = value.split(' ').next().unwrap_or_default();
446 let payload = value.split(' ').next().unwrap_or_default();
447 let position = source.formats.iter().position(|format| format == payload).unwrap_or(usize::MAX);
447 let position = source.formats.iter().position(|format| format == payload).unwrap_or(usize::MAX);
448 let is_opus = rtpmap.get(payload).is_some_and(|encoding| encoding.to_ascii_lowercase().starts_with("opus/"));
448 let cleaned = if video {
449 let cleaned = if video {
449 value.replace(";sps-pps-idr-in-keyframe=1", "")
450 value.replace(";sps-pps-idr-in-keyframe=1", "")
451 } else if is_opus {
452 with_fmtp_parameters(value, &OPUS_RECEIVE_FULLBAND)
450 } else {
453 } else {
451 value.to_owned()
454 value.to_owned()
452 };
455 };
@@ -648,10 +651,16 @@ fn transport_attributes(teams_line: &Media, session: &Session) -> Vec<Attribute>
648
651
649const OPUS_SEND_QUALITY: [(&str, &str); 2] = [("maxaveragebitrate", "40000"), ("maxplaybackrate", "48000")];
652const OPUS_SEND_QUALITY: [(&str, &str); 2] = [("maxaveragebitrate", "40000"), ("maxplaybackrate", "48000")];
650
653
654const OPUS_RECEIVE_FULLBAND: [(&str, &str); 2] = [("maxplaybackrate", "48000"), ("sprop-maxcapturerate", "48000")];
655
651/// libwebrtc sizes its Opus encoder from these remote fmtp parameters; without them it picks its own bitrate.
656/// libwebrtc sizes its Opus encoder from these remote fmtp parameters; without them it picks its own bitrate.
652fn with_opus_send_quality(fmtp: &str) -> String {
657fn with_opus_send_quality(fmtp: &str) -> String {
658 with_fmtp_parameters(fmtp, &OPUS_SEND_QUALITY)
659}
660
661fn with_fmtp_parameters(fmtp: &str, parameters: &[(&str, &str)]) -> String {
653 let mut value = fmtp.to_owned();
662 let mut value = fmtp.to_owned();
654 for (name, setting) in OPUS_SEND_QUALITY {
663 for &(name, setting) in parameters {
655 if value.split([' ', ';']).any(|parameter| parameter.split('=').next() == Some(name)) {
664 if value.split([' ', ';']).any(|parameter| parameter.split('=').next() == Some(name)) {
656 continue;
665 continue;
657 }
666 }
@@ -1118,6 +1127,7 @@ mod tests {
1118 assert!(text.contains("a=x-signaling-fb:* x-message app recv:dsh\n"));
1127 assert!(text.contains("a=x-signaling-fb:* x-message app recv:dsh\n"));
1119 assert!(text.contains("a=x-ssrc-range:42-42\n"));
1128 assert!(text.contains("a=x-ssrc-range:42-42\n"));
1120 assert!(text.contains("a=rtpmap:63 RED/8000\n"));
1129 assert!(text.contains("a=rtpmap:63 RED/8000\n"));
1130 assert!(text.contains("a=fmtp:111 minptime=10;useinbandfec=1;maxplaybackrate=48000;sprop-maxcapturerate=48000\n"));
1121 assert!(text.contains("a=rtcp:1234\n"));
1131 assert!(text.contains("a=rtcp:1234\n"));
1122 assert!(text.contains("a=extmap:2 http:\\\\www.webrtc.org\\experiments\\rtp-hdrext\\abs-send-time\n"));
1132 assert!(text.contains("a=extmap:2 http:\\\\www.webrtc.org\\experiments\\rtp-hdrext\\abs-send-time\n"));
1123 assert!(text.contains("a=ssrc:42 msid:stream 0\n"));
1133 assert!(text.contains("a=ssrc:42 msid:stream 0\n"));
crates/calling/src/software_audio.rs +11 −0
@@ -10,6 +10,7 @@ use libwebrtc::prelude::*;
10 10 use tokio::task::JoinHandle;
11 11
12 12 use crate::audio::{FRAME_SAMPLES, SAMPLE_RATE, SineGenerator};
13 use crate::band_meter::HighBandMeter;
13 14 use crate::audio_io::AudioMode;
14 15 use crate::capture::Capture;
15 16 use crate::devices::DeviceChoice;
@@ -24,6 +25,7 @@ use crate::wav_dump::StageDumps;
24 25 const SOURCE_QUEUE_MS: u32 = 100;
25 26 const FRAME_PERIOD: Duration = Duration::from_millis(10);
26 27 const MAX_LAG: Duration = Duration::from_millis(100);
28 const BAND_REPORT_PERIOD: Duration = Duration::from_secs(10);
27 29 const SYSTEM_TONE_HZ: f32 = 660.0;
28 30 const SYNTHETIC_AMPLITUDE: f32 = 0.3;
29 31
@@ -239,8 +241,17 @@ impl Drop for SoftwareAudio {
239 241
240 242 async fn feed_far_end(remote: RtcAudioTrack, queue: SampleQueue) {
241 243 let mut stream = NativeAudioStream::new(remote, SAMPLE_RATE as i32, 1);
244 let mut band_meter = HighBandMeter::default();
245 let mut next_report = Instant::now() + BAND_REPORT_PERIOD;
242 246 while let Some(frame) = stream.next().await {
243 247 queue.push(&frame.data);
248 band_meter.add(&frame.data);
249 if Instant::now() >= next_report {
250 next_report += BAND_REPORT_PERIOD;
251 if let Some(share) = band_meter.take_share() {
252 eprintln!("calls: far-end audio energy above 8 kHz: {:.2}%", share * 100.);
253 }
254 }
244 255 }
245 256 }
246 257
@@ -10,6 +10,7 @@ use libwebrtc::prelude::*;
10use tokio::task::JoinHandle;
10use tokio::task::JoinHandle;
11
11
12use crate::audio::{FRAME_SAMPLES, SAMPLE_RATE, SineGenerator};
12use crate::audio::{FRAME_SAMPLES, SAMPLE_RATE, SineGenerator};
13use crate::band_meter::HighBandMeter;
13use crate::audio_io::AudioMode;
14use crate::audio_io::AudioMode;
14use crate::capture::Capture;
15use crate::capture::Capture;
15use crate::devices::DeviceChoice;
16use crate::devices::DeviceChoice;
@@ -24,6 +25,7 @@ use crate::wav_dump::StageDumps;
24const SOURCE_QUEUE_MS: u32 = 100;
25const SOURCE_QUEUE_MS: u32 = 100;
25const FRAME_PERIOD: Duration = Duration::from_millis(10);
26const FRAME_PERIOD: Duration = Duration::from_millis(10);
26const MAX_LAG: Duration = Duration::from_millis(100);
27const MAX_LAG: Duration = Duration::from_millis(100);
28const BAND_REPORT_PERIOD: Duration = Duration::from_secs(10);
27const SYSTEM_TONE_HZ: f32 = 660.0;
29const SYSTEM_TONE_HZ: f32 = 660.0;
28const SYNTHETIC_AMPLITUDE: f32 = 0.3;
30const SYNTHETIC_AMPLITUDE: f32 = 0.3;
29
31
@@ -239,8 +241,17 @@ impl Drop for SoftwareAudio {
239
241
240async fn feed_far_end(remote: RtcAudioTrack, queue: SampleQueue) {
242async fn feed_far_end(remote: RtcAudioTrack, queue: SampleQueue) {
241 let mut stream = NativeAudioStream::new(remote, SAMPLE_RATE as i32, 1);
243 let mut stream = NativeAudioStream::new(remote, SAMPLE_RATE as i32, 1);
244 let mut band_meter = HighBandMeter::default();
245 let mut next_report = Instant::now() + BAND_REPORT_PERIOD;
242 while let Some(frame) = stream.next().await {
246 while let Some(frame) = stream.next().await {
243 queue.push(&frame.data);
247 queue.push(&frame.data);
248 band_meter.add(&frame.data);
249 if Instant::now() >= next_report {
250 next_report += BAND_REPORT_PERIOD;
251 if let Some(share) = band_meter.take_share() {
252 eprintln!("calls: far-end audio energy above 8 kHz: {:.2}%", share * 100.);
253 }
254 }
244 }
255 }
245}
256}
246
257
crates/calling/tests/fixtures/teams_offer.sdp +1 −1
@@ -19,7 +19,7 @@ a=rtpmap:8 PCMA/8000
19 19 a=rtpmap:13 CN/8000
20 20 a=rtpmap:110 telephone-event/48000
21 21 a=rtpmap:126 telephone-event/8000
22 a=fmtp:111 minptime=10;useinbandfec=1
22 a=fmtp:111 minptime=10;useinbandfec=1;maxplaybackrate=48000;sprop-maxcapturerate=48000
23 23 a=fmtp:97 111/111
24 24 a=rtcp:1234
25 25 a=rtcp-fb:111 transport-cc
@@ -19,7 +19,7 @@ a=rtpmap:8 PCMA/8000
19a=rtpmap:13 CN/8000
19a=rtpmap:13 CN/8000
20a=rtpmap:110 telephone-event/48000
20a=rtpmap:110 telephone-event/48000
21a=rtpmap:126 telephone-event/8000
21a=rtpmap:126 telephone-event/8000
22a=fmtp:111 minptime=10;useinbandfec=1
22a=fmtp:111 minptime=10;useinbandfec=1;maxplaybackrate=48000;sprop-maxcapturerate=48000
23a=fmtp:97 111/111
23a=fmtp:97 111/111
24a=rtcp:1234
24a=rtcp:1234
25a=rtcp-fb:111 transport-cc
25a=rtcp-fb:111 transport-cc