🐛 fix(calling): camera video starts sharp and 1080p no longer stalls (#65)
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com> Co-authored-by: Daniel Däschle <30751516+danieldaeschle@users.noreply.github.com>
@@ -530,7 +530,10 @@ fn signal_rtp_line(
530
530
.get(payload)
531
531
.is_some_and(|encoding| encoding.to_ascii_lowercase().starts_with("opus/"));
532
532
let cleaned = if video {
533
value.replace(";sps-pps-idr-in-keyframe=1", "")
533
without_fmtp_parameter(
534
&value.replace(";sps-pps-idr-in-keyframe=1", ""),
535
H264_SEND_START[0].0,
536
)
534
537
} else if is_opus {
535
538
with_fmtp_parameters(value, &OPUS_RECEIVE_FULLBAND)
536
539
} else {
@@ -805,6 +808,58 @@ fn with_opus_send_quality(fmtp: &str) -> String {
805
808
with_fmtp_parameters(fmtp, &OPUS_SEND_QUALITY)
806
809
}
807
810
811
/// libwebrtc starts its bandwidth estimate at 300 kbit/s, which opens a 720p camera at 320x180.
812
const H264_SEND_START: [(&str, &str); 1] = [("x-google-start-bitrate", "1500")];
813
const H264_PROFILE_LEVEL_ID: &str = "profile-level-id=";
814
const H264_LEVEL_4_0: u8 = 0x28;
815
const FULL_HD_MAX_FS: u32 = 8160;
816
const FULL_HD_MAX_MBPS: u32 = 244_800;
817
818
/// Only touches H264 lines, the sole codec here that carries a `profile-level-id`.
819
fn with_h264_send_quality(fmtp: &str) -> String {
820
if !fmtp.contains(H264_PROFILE_LEVEL_ID) {
821
return fmtp.to_owned();
822
}
823
with_fmtp_parameters(&with_full_hd_level(fmtp), &H264_SEND_START)
824
}
825
826
/// Teams answers level 3.1 even when its `max-fs` and `max-mbps` allow 1080p, and the encoder
827
/// stalls at 1080p on that level. The explicit limits are what the peer can really receive.
828
fn with_full_hd_level(fmtp: &str) -> String {
829
let limit = |name: &str| {
830
fmtp.split([' ', ';'])
831
.find_map(|parameter| parameter.strip_prefix(name)?.strip_prefix('='))
832
.and_then(|value| value.parse::<u32>().ok())
833
};
834
let allows_full_hd = limit("max-fs").is_some_and(|max_fs| max_fs >= FULL_HD_MAX_FS)
835
&& limit("max-mbps").is_some_and(|max_mbps| max_mbps >= FULL_HD_MAX_MBPS);
836
let level = fmtp
837
.find(H264_PROFILE_LEVEL_ID)
838
.map(|start| start + H264_PROFILE_LEVEL_ID.len() + 4)
839
.map(|start| start..start + 2)
840
.filter(|range| {
841
fmtp.get(range.clone())
842
.and_then(|level| u8::from_str_radix(level, 16).ok())
843
.is_some_and(|level| level < H264_LEVEL_4_0)
844
});
845
let mut raised = fmtp.to_owned();
846
if let Some(range) = level.filter(|_| allows_full_hd) {
847
raised.replace_range(range, &format!("{H264_LEVEL_4_0:02x}"));
848
}
849
raised
850
}
851
852
fn without_fmtp_parameter(fmtp: &str, name: &str) -> String {
853
let Some((payload, parameters)) = fmtp.split_once(' ') else {
854
return fmtp.to_owned();
855
};
856
let kept: Vec<&str> = parameters
857
.split(';')
858
.filter(|parameter| parameter.split('=').next() != Some(name))
859
.collect();
860
format!("{payload} {}", kept.join(";"))
861
}
862
808
863
fn with_fmtp_parameters(fmtp: &str, parameters: &[(&str, &str)]) -> String {
809
864
let mut value = fmtp.to_owned();
810
865
for &(name, setting) in parameters {
@@ -900,7 +955,7 @@ fn browser_rtp_line(
900
955
if is_opus {
901
956
with_opus_send_quality(&cleaned)
902
957
} else {
903
cleaned
958
with_h264_send_quality(&cleaned)
904
959
},
905
960
));
906
961
}
@@ -1348,6 +1403,7 @@ fn browser_rtp_from_teams(
1348
1403
"rtcp" => attributes.push(Attribute::new("rtcp", BROWSER_RTCP)),
1349
1404
"rtpmap" => attributes.push(Attribute::new("rtpmap", browser_rtpmap(value))),
1350
1405
"extmap" => attributes.push(Attribute::new("extmap", browser_extmap(value))),
1406
"fmtp" => attributes.push(Attribute::new("fmtp", with_h264_send_quality(value))),
1351
1407
"rtcp-fb" => match value.strip_prefix("* ") {
1352
1408
Some(kind) => expanded_feedback.extend(
1353
1409
source
@@ -1415,6 +1471,28 @@ mod tests {
1415
1471
}
1416
1472
}
1417
1473
1474
#[test]
1475
fn h264_sends_start_high_and_use_the_level_the_peer_limits_allow() {
1476
let full_hd = "98 packetization-mode=1;profile-level-id=42e01f;max-fs=8160;max-mbps=244800";
1477
assert_eq!(
1478
with_h264_send_quality(full_hd),
1479
"98 packetization-mode=1;profile-level-id=42e028;max-fs=8160;max-mbps=244800;x-google-start-bitrate=1500"
1480
);
1481
let hd = "98 profile-level-id=42e01f;max-fs=3600;max-mbps=108000";
1482
assert_eq!(
1483
with_h264_send_quality(hd),
1484
"98 profile-level-id=42e01f;max-fs=3600;max-mbps=108000;x-google-start-bitrate=1500"
1485
);
1486
assert_eq!(with_h264_send_quality("99 apt=98"), "99 apt=98");
1487
assert_eq!(
1488
without_fmtp_parameter(
1489
"98 profile-level-id=42e01f;x-google-start-bitrate=1500",
1490
"x-google-start-bitrate"
1491
),
1492
"98 profile-level-id=42e01f"
1493
);
1494
}
1495
1418
1496
#[test]
1419
1497
fn rejected_lines_stay_out_of_the_bundle() {
1420
1498
let lines = [("9", Some("0")), ("0", Some("1")), ("9", Some("2"))];
@@ -530,7 +530,10 @@ fn signal_rtp_line(530
.get(payload)530
.get(payload)531
.is_some_and(|encoding| encoding.to_ascii_lowercase().starts_with("opus/"));531
.is_some_and(|encoding| encoding.to_ascii_lowercase().starts_with("opus/"));532
let cleaned = if video {532
let cleaned = if video {533
value.replace(";sps-pps-idr-in-keyframe=1", "")533
without_fmtp_parameter(534
&value.replace(";sps-pps-idr-in-keyframe=1", ""),535
H264_SEND_START[0].0,536
)534
} else if is_opus {537
} else if is_opus {535
with_fmtp_parameters(value, &OPUS_RECEIVE_FULLBAND)538
with_fmtp_parameters(value, &OPUS_RECEIVE_FULLBAND)536
} else {539
} else {@@ -805,6 +808,58 @@ fn with_opus_send_quality(fmtp: &str) -> String {805
with_fmtp_parameters(fmtp, &OPUS_SEND_QUALITY)808
with_fmtp_parameters(fmtp, &OPUS_SEND_QUALITY)806
}809
}807
810
811
/// libwebrtc starts its bandwidth estimate at 300 kbit/s, which opens a 720p camera at 320x180.812
const H264_SEND_START: [(&str, &str); 1] = [("x-google-start-bitrate", "1500")];813
const H264_PROFILE_LEVEL_ID: &str = "profile-level-id=";814
const H264_LEVEL_4_0: u8 = 0x28;815
const FULL_HD_MAX_FS: u32 = 8160;816
const FULL_HD_MAX_MBPS: u32 = 244_800;817
818
/// Only touches H264 lines, the sole codec here that carries a `profile-level-id`.819
fn with_h264_send_quality(fmtp: &str) -> String {820
if !fmtp.contains(H264_PROFILE_LEVEL_ID) {821
return fmtp.to_owned();822
}823
with_fmtp_parameters(&with_full_hd_level(fmtp), &H264_SEND_START)824
}825
826
/// Teams answers level 3.1 even when its `max-fs` and `max-mbps` allow 1080p, and the encoder827
/// stalls at 1080p on that level. The explicit limits are what the peer can really receive.828
fn with_full_hd_level(fmtp: &str) -> String {829
let limit = |name: &str| {830
fmtp.split([' ', ';'])831
.find_map(|parameter| parameter.strip_prefix(name)?.strip_prefix('='))832
.and_then(|value| value.parse::<u32>().ok())833
};834
let allows_full_hd = limit("max-fs").is_some_and(|max_fs| max_fs >= FULL_HD_MAX_FS)835
&& limit("max-mbps").is_some_and(|max_mbps| max_mbps >= FULL_HD_MAX_MBPS);836
let level = fmtp837
.find(H264_PROFILE_LEVEL_ID)838
.map(|start| start + H264_PROFILE_LEVEL_ID.len() + 4)839
.map(|start| start..start + 2)840
.filter(|range| {841
fmtp.get(range.clone())842
.and_then(|level| u8::from_str_radix(level, 16).ok())843
.is_some_and(|level| level < H264_LEVEL_4_0)844
});845
let mut raised = fmtp.to_owned();846
if let Some(range) = level.filter(|_| allows_full_hd) {847
raised.replace_range(range, &format!("{H264_LEVEL_4_0:02x}"));848
}849
raised850
}851
852
fn without_fmtp_parameter(fmtp: &str, name: &str) -> String {853
let Some((payload, parameters)) = fmtp.split_once(' ') else {854
return fmtp.to_owned();855
};856
let kept: Vec<&str> = parameters857
.split(';')858
.filter(|parameter| parameter.split('=').next() != Some(name))859
.collect();860
format!("{payload} {}", kept.join(";"))861
}862
808
fn with_fmtp_parameters(fmtp: &str, parameters: &[(&str, &str)]) -> String {863
fn with_fmtp_parameters(fmtp: &str, parameters: &[(&str, &str)]) -> String {809
let mut value = fmtp.to_owned();864
let mut value = fmtp.to_owned();810
for &(name, setting) in parameters {865
for &(name, setting) in parameters {@@ -900,7 +955,7 @@ fn browser_rtp_line(900
if is_opus {955
if is_opus {901
with_opus_send_quality(&cleaned)956
with_opus_send_quality(&cleaned)902
} else {957
} else {903
cleaned958
with_h264_send_quality(&cleaned)904
},959
},905
));960
));906
}961
}@@ -1348,6 +1403,7 @@ fn browser_rtp_from_teams(1348
"rtcp" => attributes.push(Attribute::new("rtcp", BROWSER_RTCP)),1403
"rtcp" => attributes.push(Attribute::new("rtcp", BROWSER_RTCP)),1349
"rtpmap" => attributes.push(Attribute::new("rtpmap", browser_rtpmap(value))),1404
"rtpmap" => attributes.push(Attribute::new("rtpmap", browser_rtpmap(value))),1350
"extmap" => attributes.push(Attribute::new("extmap", browser_extmap(value))),1405
"extmap" => attributes.push(Attribute::new("extmap", browser_extmap(value))),1406
"fmtp" => attributes.push(Attribute::new("fmtp", with_h264_send_quality(value))),1351
"rtcp-fb" => match value.strip_prefix("* ") {1407
"rtcp-fb" => match value.strip_prefix("* ") {1352
Some(kind) => expanded_feedback.extend(1408
Some(kind) => expanded_feedback.extend(1353
source1409
source@@ -1415,6 +1471,28 @@ mod tests {1415
}1471
}1416
}1472
}1417
1473
1474
#[test]1475
fn h264_sends_start_high_and_use_the_level_the_peer_limits_allow() {1476
let full_hd = "98 packetization-mode=1;profile-level-id=42e01f;max-fs=8160;max-mbps=244800";1477
assert_eq!(1478
with_h264_send_quality(full_hd),1479
"98 packetization-mode=1;profile-level-id=42e028;max-fs=8160;max-mbps=244800;x-google-start-bitrate=1500"1480
);1481
let hd = "98 profile-level-id=42e01f;max-fs=3600;max-mbps=108000";1482
assert_eq!(1483
with_h264_send_quality(hd),1484
"98 profile-level-id=42e01f;max-fs=3600;max-mbps=108000;x-google-start-bitrate=1500"1485
);1486
assert_eq!(with_h264_send_quality("99 apt=98"), "99 apt=98");1487
assert_eq!(1488
without_fmtp_parameter(1489
"98 profile-level-id=42e01f;x-google-start-bitrate=1500",1490
"x-google-start-bitrate"1491
),1492
"98 profile-level-id=42e01f"1493
);1494
}1495
1418
#[test]1496
#[test]1419
fn rejected_lines_stay_out_of_the_bundle() {1497
fn rejected_lines_stay_out_of_the_bundle() {1420
let lines = [("9", Some("0")), ("0", Some("1")), ("9", Some("2"))];1498
let lines = [("9", Some("0")), ("0", Some("1")), ("9", Some("2"))];