[domistream] Video packetizer: tests first, FEC blocks split evenly, larger blocks first (RFC 5052 9.1)

This commit is contained in:
2026-12-04 15:47:15 +01:00
parent 28750eee81
commit 90c49c4041
+15 -4
View File
@@ -438,16 +438,18 @@ fn fec_plan_properties() {
assert!((1..=MAX_FEC_BLOCKS).contains(&plan.len()), "{ctx}: {} blocks", plan.len()); assert!((1..=MAX_FEC_BLOCKS).contains(&plan.len()), "{ctx}: {} blocks", plan.len());
assert_eq!(plan.iter().map(|b| usize::from(b.data_shards)).sum::<usize>(), n, "{ctx}"); assert_eq!(plan.iter().map(|b| usize::from(b.data_shards)).sum::<usize>(), n, "{ctx}");
let first_k = plan[0].data_shards; let first_k = plan[0].data_shards;
// Even split (RFC 5052 9.1, as a BSD-2 host does it, PROTOCOL.md 5.3): sizes differ by at most one,
// the larger blocks first.
let q = n / plan.len();
let r = n % plan.len();
for (i, b) in plan.iter().enumerate() { for (i, b) in plan.iter().enumerate() {
let (k, m, p) = let (k, m, p) =
(usize::from(b.data_shards), usize::from(b.parity_shards), usize::from(b.fec_percentage)); (usize::from(b.data_shards), usize::from(b.parity_shards), usize::from(b.fec_percentage));
assert!(k >= 1 && k + m <= 255, "{ctx}: k {k} m {m}"); assert!(k >= 1 && k + m <= 255, "{ctx}: k {k} m {m}");
assert_eq!(m, parity_count(k, b.fec_percentage).unwrap(), "{ctx}: m follows from the percentage"); assert_eq!(m, parity_count(k, b.fec_percentage).unwrap(), "{ctx}: m follows from the percentage");
if i + 1 < plan.len() { let want = if i < r { q + 1 } else { q };
assert_eq!(b.data_shards, first_k, "{ctx}: every block but the last carries the same k"); assert_eq!(usize::from(b.data_shards), want, "{ctx}: block {i} of an even split");
} else {
assert!(b.data_shards <= first_k, "{ctx}"); assert!(b.data_shards <= first_k, "{ctx}");
}
let t = target(k, pct, min); let t = target(k, pct, min);
assert_eq!( assert_eq!(
b.reduced, b.reduced,
@@ -480,6 +482,15 @@ fn fec_plan_properties() {
); );
} }
#[test]
fn blocks_split_evenly_larger_first() {
// 217 shards at 255 %: at most 72 data shards fit a block (72 + 184 = 256 would not), so 4 blocks, split
// 55, 54, 54, 54 (RFC 5052 9.1), not 55, 55, 55, 52.
let plan = plan_fec_blocks(217, 255, 0).unwrap();
let ks: Vec<u16> = plan.iter().map(|b| b.data_shards).collect();
assert_eq!(ks, vec![55, 54, 54, 54]);
}
#[test] #[test]
fn minimum_parity_raises_the_written_percentage() { fn minimum_parity_raises_the_written_percentage() {
// One data shard at 0 % with at least 2 parity packets: 101 % is the smallest percentage giving // One data shard at 0 % with at least 2 parity packets: 101 % is the smallest percentage giving