1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
// Changed by Alakazam Labs for sew-str0m; the fork's patches are listed in its README.
use serde::{Deserialize, Serialize};
use std::collections::HashSet;
use std::ops::RangeInclusive;
use crate::packet::H264ProfileLevel;
use crate::packet::H265ProfileTierLevel;
use crate::packet::H266ProfileTierLevel;
use crate::rtp_::Pt;
use super::codec::{Codec, CodecSpec};
use super::format_params::FormatParams;
/// Preferred ranges for dynamic payload type allocation.
pub(crate) const PREFERED_RANGES: &[RangeInclusive<usize>] = &[
// Payload identifiers 96–127 are used for payloads defined dynamically during a session.
96..=127,
// "unassigned" ranged. note that RTCP packet type 207 (XR, Extended Reports) would be
// indistinguishable from RTP payload types 79 with the marker bit set
80..=95,
77..=78,
// reserved because RTCP packet types 200–204 would otherwise be indistinguishable
// from RTP payload types 72–76
// 72..77,
// lol range.
35..=71,
];
pub(crate) trait Claimed {
fn assert_claim_once(&mut self, pt: Pt) -> bool;
fn is_claimed(&self, pt: Pt) -> bool;
fn find_unclaimed(
&self,
ranges: &[RangeInclusive<usize>],
unlocked: &HashSet<Pt>,
) -> Option<Pt>;
}
impl Claimed for [bool; 128] {
fn assert_claim_once(&mut self, pt: Pt) -> bool {
let idx = *pt as usize;
if self[idx] {
warn!("Ignore already claimed PT {}", pt);
return false;
}
self[idx] = true;
true
}
fn is_claimed(&self, pt: Pt) -> bool {
let idx = *pt as usize;
self[idx]
}
fn find_unclaimed(
&self,
ranges: &[RangeInclusive<usize>],
unlocked: &HashSet<Pt>,
) -> Option<Pt> {
for range in ranges {
for i in range.clone() {
if !self[i] && !unlocked.contains(&(i as u8).into()) {
let pt: Pt = (i as u8).into();
return Some(pt);
}
}
}
// Failed to find unclaimed PT.
None
}
}
/// Group of parameters for a payload type (PT).
///
/// In the SDP a payload type has a number of associated parameters. See example below:
///
/// ```text
/// a=rtpmap:96 H264/90000
/// a=rtcp-fb:96 goog-remb
/// a=rtcp-fb:96 transport-cc
/// a=rtcp-fb:96 ccm fir
/// a=rtcp-fb:96 nack
/// a=rtcp-fb:96 nack pli
/// a=fmtp:96 level-asymmetry-allowed=1;packetization-mode=0;profile-level-id=42001f
/// ```
#[derive(Clone, Copy, Debug, Serialize, Deserialize)]
pub struct PayloadParams {
/// The payload type that groups these parameters.
pub(crate) pt: Pt,
/// Whether these parameters are repairing some other set of parameters.
/// This is used to, via PT, separate RTX resend streams from the main stream.
pub(crate) resend: Option<Pt>,
/// The codec with settings for this group of parameters.
pub(crate) spec: CodecSpec,
/// Whether the payload use the TWCC feedback mechanic.
pub(crate) fb_transport_cc: bool,
/// Whether the payload uses NACK to request resends.
pub(crate) fb_nack: bool,
/// Whether the payload uses the PLI (Picture Loss Indication) mechanic.
pub(crate) fb_pli: bool,
/// Whether the payload uses the FIR (Full Intra Request) mechanic.
pub(crate) fb_fir: bool,
/// Whether the payload uses the REMB (Receiver Estimated Maximum Bitrate) mechanic.
pub(crate) fb_remb: bool,
/// The RFC 2198 RED payload type that wraps this primary codec, if negotiated.
/// Like `resend`, this links a secondary PT to this primary via SDP.
pub(crate) red: Option<Pt>,
/// Whether the payload is locked by negotiation or can still be debated.
///
/// If we make an OFFER or ANSWER and the direction is sendrecv/recvonly, the parameters are locked
/// can't be further changed. If we make an OFFER for a sendonly, the parameters are only proposed
/// and don't lock.
pub(crate) locked: bool,
/// Whether these parameters were locked without RTX of our own (`resend` was `None` when
/// they locked), so a remote RTX payload type is never adopted for them, even after locking.
#[serde(default)]
pub(crate) rtx_declined: bool,
}
// we don't want to compare "locked"
impl PartialEq for PayloadParams {
fn eq(&self, other: &Self) -> bool {
self.pt == other.pt
&& self.resend == other.resend
&& self.spec == other.spec
&& self.fb_transport_cc == other.fb_transport_cc
&& self.fb_nack == other.fb_nack
&& self.fb_pli == other.fb_pli
&& self.fb_fir == other.fb_fir
&& self.fb_remb == other.fb_remb
&& self.red == other.red
}
}
impl Eq for PayloadParams {}
impl PayloadParams {
/// Maximum match score.
///
/// This is returned for an exact `CodecSpec` match in `match_score()`. It is also used as the
/// starting point for codec-specific scoring in this file:
/// - Opus/H.264/H.265 decrement from a perfect match.
/// - VP9/AV1 return the max score when their relevant fmtp parameters match.
/// - VP8 currently only matches via the exact `CodecSpec` equality fast-path (no fuzzy scorer).
const EXACT_MATCH_SCORE: usize = 100;
/// Score used for H.265 profile-only compatibility matches.
///
/// Some endpoints (notably browsers) may signal H.265 using only `profile-id` without
/// tier/level information; when we can only validate profile equality we return this lower
/// score so full ProfileTierLevel matches win when available.
const PROFILE_ONLY_MATCH_SCORE: usize = 90;
/// Creates new payload params.
///
/// * `pt` is the payload type RTP mapping in the session.
/// * `resend` is the payload type used for (RTX) resend channel.
/// * `spec` configures details about the codec.
pub fn new(pt: Pt, resend: Option<Pt>, spec: CodecSpec) -> Self {
let is_video = spec.codec.is_video();
PayloadParams {
pt,
resend,
spec,
// Both audio and video use TWCC
fb_transport_cc: true,
// Only true for video.
fb_fir: is_video,
fb_nack: is_video,
fb_pli: is_video,
fb_remb: is_video,
red: None,
locked: false,
rtx_declined: false,
}
}
/// Creates minimal payload params for SSRC 0 BWE probes.
///
/// These probes don't carry real media, only padding for bandwidth estimation.
/// Uses 90kHz clock rate (video rate) and enables only transport_cc feedback.
pub(crate) fn new_probe(pt: Pt) -> Self {
use super::codec::Codec;
use super::format_params::FormatParams;
use crate::rtp_::Frequency;
PayloadParams {
pt,
resend: None,
spec: CodecSpec {
codec: Codec::Null,
clock_rate: Frequency::NINETY_KHZ,
channels: None,
format: FormatParams::default(),
},
fb_transport_cc: true,
fb_nack: false,
fb_pli: false,
fb_fir: false,
fb_remb: false,
red: None,
locked: false,
rtx_declined: false,
}
}
/// The payload type that groups these parameters.
pub fn pt(&self) -> Pt {
self.pt
}
/// Whether these parameters are repairing some other set of parameters.
/// This is used to, via PT, separate RTX resend streams from the main stream.
pub fn resend(&self) -> Option<Pt> {
self.resend
}
/// Sets the RTX resend payload type. Use `None` to disable RTX, in which
/// case retransmissions (if NACK is enabled) are sent in-band on the main SSRC.
pub fn set_resend(&mut self, resend: Option<Pt>) {
self.resend = resend;
}
/// The codec with settings for this group of parameters.
pub fn spec(&self) -> CodecSpec {
self.spec
}
/// Sets whether the payload use the TWCC feedback mechanic.
pub fn set_fb_transport_cc(&mut self, fb_transport_cc: bool) {
self.fb_transport_cc = fb_transport_cc
}
/// Whether the payload use the TWCC feedback mechanic.
pub fn fb_transport_cc(&self) -> bool {
self.fb_transport_cc
}
/// Sets whether the payload uses NACK to request resends.
pub fn set_fb_nack(&mut self, fb_nack: bool) {
self.fb_nack = fb_nack
}
/// Whether the payload uses NACK to request resends.
pub fn fb_nack(&self) -> bool {
self.fb_nack
}
/// Set whether the payload uses the PLI (Picture Loss Indication) mechanic.
pub fn set_fb_pli(&mut self, fb_pli: bool) {
self.fb_pli = fb_pli
}
/// Whether the payload uses the PLI (Picture Loss Indication) mechanic.
pub fn fb_pli(&self) -> bool {
self.fb_pli
}
/// Set whether the payload uses the FIR (Full Intra Request) mechanic.
pub fn set_fb_fir(&mut self, fb_fir: bool) {
self.fb_fir = fb_fir
}
/// Whether the payload uses the FIR (Full Intra Request) mechanic.
pub fn fb_fir(&self) -> bool {
self.fb_fir
}
/// Set whether the payload uses the REMB (Receiver Estimated Maximum Bitrate) mechanic.
pub fn set_fb_remb(&mut self, fb_remb: bool) {
self.fb_remb = fb_remb
}
/// Whether the payload uses the REMB (Receiver Estimated Maximum Bitrate) mechanic.
pub fn fb_remb(&self) -> bool {
self.fb_remb
}
/// The RFC 2198 RED payload type wrapping this primary codec, if negotiated.
pub fn red(&self) -> Option<Pt> {
self.red
}
pub(crate) fn match_score(&self, o: &PayloadParams) -> Option<usize> {
// we don't want to compare PT
let c0 = self.spec;
let c1 = o.spec;
if c0 == c1 && c0.codec != Codec::AmrWb {
// Exact match
return Some(Self::EXACT_MATCH_SCORE);
}
// Attempt fuzzy matching, since we don't have an exact match
if c0.codec != c1.codec || (c0.codec == Codec::Unknown && c1.codec == Codec::Unknown) {
// Codecs must match
return None;
}
if c0.clock_rate != c1.clock_rate {
// Clock rates must match
return None;
}
// Telephone events match by clock rate, regardless of channel count.
if c0.codec.is_tele() {
return Some(Self::EXACT_MATCH_SCORE);
}
// AMR-WB defaults an omitted channel count to mono, so it must match
// before the generic channel comparison below.
if c0.codec == Codec::AmrWb {
return Self::match_amr_wb_score(c0, c1);
}
if c0.channels != c1.channels {
// Channels must match
return None;
}
if c0.codec.is_audio() && c0.codec == Codec::Opus {
return Some(Self::match_opus_score(c0, c1));
}
if c0.codec == Codec::H264 {
return Self::match_h264_score(c0, c1);
}
if c0.codec == Codec::H265 {
return Self::match_h265_score(c0, c1);
}
if c0.codec == Codec::H266 {
return Self::match_h266_score(c0, c1);
}
if c0.codec == Codec::Vp9 {
return Self::match_vp9_score(c0, c1);
}
if c0.codec == Codec::Av1 {
return Self::match_av1_score(c0, c1);
}
// TODO: Fuzzy matching for any other audio codecs
// TODO: Fuzzy matching for video
None
}
fn match_opus_score(c0: CodecSpec, c1: CodecSpec) -> usize {
let mut score: usize = Self::EXACT_MATCH_SCORE;
// If neither value is specified both sides should assume the default value, 3.
let either_p_time_specified =
c0.format.min_p_time.is_some() || c1.format.min_p_time.is_some();
if either_p_time_specified && c0.format.min_p_time != c1.format.min_p_time {
score = score.saturating_sub(1);
}
// If neither value is specified both sides should assume FEC is not used as this is
// the default.
let either_fec_specified =
c0.format.use_inband_fec.is_some() || c1.format.use_inband_fec.is_some();
if either_fec_specified && c0.format.use_inband_fec != c1.format.use_inband_fec {
score = score.saturating_sub(2);
}
// If neither value is specified both sides should assume DTX is not used as this is
// the default.
let either_dtx_specified = c0.format.use_dtx.is_some() || c1.format.use_dtx.is_some();
if either_dtx_specified && c0.format.use_dtx != c1.format.use_dtx {
score = score.saturating_sub(4);
}
// If neither value is specified both sides should assume mono for stereo
// and the sprop-stereo hint defaults to false.
let c0_stereo = c0.format.stereo.unwrap_or(false);
let c1_stereo = c1.format.stereo.unwrap_or(false);
if c0_stereo != c1_stereo {
score = score.saturating_sub(8);
}
let c0_sprop_stereo = c0.format.sprop_stereo.unwrap_or(false);
let c1_sprop_stereo = c1.format.sprop_stereo.unwrap_or(false);
if c0_sprop_stereo != c1_sprop_stereo {
score = score.saturating_sub(8);
}
score
}
fn match_vp9_score(c0: CodecSpec, c1: CodecSpec) -> Option<usize> {
// Default profile_id is 0. https://datatracker.ietf.org/doc/html/draft-ietf-payload-vp9-16#section-6
let c0_profile_id = c0.format.profile_id.unwrap_or(0);
let c1_profile_id = c1.format.profile_id.unwrap_or(0);
if c0_profile_id != c1_profile_id {
return None;
}
Some(Self::EXACT_MATCH_SCORE)
}
fn match_av1_score(c0: CodecSpec, c1: CodecSpec) -> Option<usize> {
// TODO: consider media direction for a proper less or equal matching
// The AV1 stream sent by either the offerer or the answerer MUST be
// encoded with a profile, level and tier, lesser or equal to the values
// of the level-idx, profile and tier declared in the SDP by the receiving
// agent.
// https://aomediacodec.github.io/av1-rtp-spec/#723-usage-with-the-sdp-offeranswer-model
// Default values: profile = 0, level-idx = 5, tier = 0
// https://aomediacodec.github.io/av1-rtp-spec/#72-sdp-parameters
let c0_profile = c0.format.profile.unwrap_or(0);
let c1_profile = c1.format.profile.unwrap_or(0);
if c0_profile != c1_profile {
return None;
}
let c0_level_idx = c0.format.level_idx.unwrap_or(5);
let c1_level_idx = c1.format.level_idx.unwrap_or(5);
if c0_level_idx != c1_level_idx {
return None;
}
let c0_tier = c0.format.tier.unwrap_or(0);
let c1_tier = c1.format.tier.unwrap_or(0);
if c0_tier != c1_tier {
return None;
}
Some(Self::EXACT_MATCH_SCORE)
}
fn match_amr_wb_score(c0: CodecSpec, c1: CodecSpec) -> Option<usize> {
// Only mono is implemented, and octet-aligned and bandwidth-efficient
// payloads are not interchangeable.
if c0.channels.unwrap_or(1) != 1 || c1.channels.unwrap_or(1) != 1 {
return None;
}
if c0.format.octet_align.unwrap_or(false) != c1.format.octet_align.unwrap_or(false) {
return None;
}
// A mode-set binds both peers (RFC 4867 §8.3.1), so prefer unrestricted
// payload types when the remote offers both.
match (c0.format.mode_set, c1.format.mode_set) {
(None, None) => Some(Self::EXACT_MATCH_SCORE),
(Some(a), Some(b)) if a != b => None,
_ => Some(Self::EXACT_MATCH_SCORE - 1),
}
}
pub(crate) fn match_h264_score(c0: CodecSpec, c1: CodecSpec) -> Option<usize> {
// Default packetization mode is 0. https://www.rfc-editor.org/rfc/rfc6184#section-6.2
let c0_packetization_mode = c0.format.packetization_mode.unwrap_or(0);
let c1_packetization_mode = c1.format.packetization_mode.unwrap_or(0);
if c0_packetization_mode != c1_packetization_mode {
return None;
}
let c0_profile_level = c0
.format
.profile_level_id
.map(|l| l.try_into().ok())
.unwrap_or(Some(H264ProfileLevel::FALLBACK))?;
let c1_profile_level = c1
.format
.profile_level_id
.map(|l| l.try_into().ok())
.unwrap_or(Some(H264ProfileLevel::FALLBACK))?;
// RFC 6184 Section 8.2.2: Profiles must match exactly.
if c0_profile_level.profile() != c1_profile_level.profile() {
return None;
}
// RFC 6184 Section 8.2.2: the level is a negotiable (downgradable)
// parameter, so a level mismatch in either direction is allowed and
// only penalizes the match score rather than rejecting the payload.
//
// NOTE: this only affects *matching*. Unlike H.265/H.266, `update_param`
// does not narrow the answered H.264 level to `min(local, remote)`, so
// when `level-asymmetry-allowed` is absent we may answer with a level
// higher than the offer. See the note in `update_param`.
let level_difference: usize = c0_profile_level
.level()
.ordinal()
.abs_diff(c1_profile_level.level().ordinal());
Some(Self::EXACT_MATCH_SCORE.saturating_sub(level_difference))
}
/// Match H.265 codec specifications and return compatibility score.
///
/// # Matching Rules — Full PTL (both sides have profile+tier+level)
///
/// - **Profiles** must match exactly (no cross-profile compatibility)
/// - **Tiers** must match exactly (Main vs High tier are distinct)
/// - **Levels** penalize score by `|c0_level - c1_level|` but never reject.
/// Both H.264 and H.265 tolerate level mismatches in either direction at
/// match time. For H.265, `update_param` additionally narrows the
/// negotiated level to `min(local, remote)` afterward; H.264 does not.
///
/// # Matching Rules — Profile-only (at least one side lacks tier+level)
///
/// - **Profiles** must match; missing profile info falls back to
/// `FALLBACK.profile()` (not rejected).
/// - **Tiers and levels** are not checked (insufficient information).
/// - Returns a lower score (90) so full-PTL matches are preferred.
///
/// # Returns
///
/// - `Some(100)` for exact full-PTL match
/// - `Some(100 - level_gap)` for profile/tier match with level difference
/// - `Some(90)` for profile-only match (when tier/level unavailable)
/// - `None` for profile or tier mismatch
pub(crate) fn match_h265_score(c0: CodecSpec, c1: CodecSpec) -> Option<usize> {
match (
c0.format.h265_profile_tier_level,
c1.format.h265_profile_tier_level,
) {
(Some(c0_ptl), Some(c1_ptl)) => {
// Both have full PTL - strict matching
// Profiles must match exactly.
// RFC 7798 §7.2.2: profile-id is a configuration parameter
// that MUST be used symmetrically in offer/answer.
// https://www.rfc-editor.org/rfc/rfc7798#section-7.2.2
if c0_ptl.profile() != c1_ptl.profile() {
return None;
}
// Tiers must match exactly.
// RFC 7798 §7.2.2: tier-flag is a configuration parameter
// that MUST be used symmetrically in offer/answer.
// https://www.rfc-editor.org/rfc/rfc7798#section-7.2.2
if c0_ptl.tier() != c1_ptl.tier() {
return None;
}
// Level difference penalizes score but never causes rejection.
// Actual level narrowing to min(local, remote) is done in update_param.
let level_difference: usize =
c0_ptl.level().ordinal().abs_diff(c1_ptl.level().ordinal());
// Pure scoring without artificial floor - let caller decide policy
Some(Self::EXACT_MATCH_SCORE.saturating_sub(level_difference))
}
_ => {
// At least one side has only profile-id (not full PTL).
// Match on profile only, using FALLBACK for missing values.
// Get profile from PTL if present, otherwise from profile_id field
let c0_profile = c0
.format
.h265_profile_tier_level
.map(|ptl| ptl.profile().to_id())
.or_else(|| c0.format.profile_id.map(|p| p as u8))
.unwrap_or(H265ProfileTierLevel::FALLBACK.profile().to_id());
let c1_profile = c1
.format
.h265_profile_tier_level
.map(|ptl| ptl.profile().to_id())
.or_else(|| c1.format.profile_id.map(|p| p as u8))
.unwrap_or(H265ProfileTierLevel::FALLBACK.profile().to_id());
// Profiles must match
if c0_profile != c1_profile {
return None;
}
// When only profile is specified, we can't verify tier/level compatibility.
// Return a lower score to prefer exact PTL matches when available.
Some(Self::PROFILE_ONLY_MATCH_SCORE)
}
}
}
pub(crate) fn match_h266_score(c0: CodecSpec, c1: CodecSpec) -> Option<usize> {
match (
c0.format.h266_profile_tier_level,
c1.format.h266_profile_tier_level,
) {
(Some(c0_ptl), Some(c1_ptl)) => {
// Both have full PTL - strict matching
// Profiles must match exactly.
// RFC 9328 §7.2: profile-id is a configuration parameter
// that MUST be used symmetrically in offer/answer.
if c0_ptl.profile() != c1_ptl.profile() {
return None;
}
// Tiers must match exactly.
// RFC 9328 §7.2: tier-flag is a configuration parameter
// that MUST be used symmetrically in offer/answer.
if c0_ptl.tier() != c1_ptl.tier() {
return None;
}
// Level difference penalizes score but never causes rejection.
let level_difference: usize =
c0_ptl.level().ordinal().abs_diff(c1_ptl.level().ordinal());
Some(Self::EXACT_MATCH_SCORE.saturating_sub(level_difference))
}
_ => {
// At least one side has no full PTL (e.g. a GStreamer offer
// with a bare rtpmap and no fmtp). Match on profile only,
// using FALLBACK for missing values.
let c0_profile = c0
.format
.h266_profile_tier_level
.map(|ptl| ptl.profile().to_id())
.or_else(|| c0.format.profile_id.map(|p| p as u8))
.unwrap_or(H266ProfileTierLevel::FALLBACK.profile().to_id());
let c1_profile = c1
.format
.h266_profile_tier_level
.map(|ptl| ptl.profile().to_id())
.or_else(|| c1.format.profile_id.map(|p| p as u8))
.unwrap_or(H266ProfileTierLevel::FALLBACK.profile().to_id());
// Profiles must match
if c0_profile != c1_profile {
return None;
}
Some(Self::PROFILE_ONLY_MATCH_SCORE)
}
}
}
pub(crate) fn update_param(
&mut self,
remote_pts: &[PayloadParams],
claimed: &mut [bool; 128],
local_is_controlling: bool,
unlocked: &mut HashSet<Pt>,
) {
let Some((first, _)) = remote_pts
.iter()
// One unrestricted AMR-WB payload can match several local mode sets.
// Leave the other alternatives unlocked once its PT has been claimed.
.filter(|p| {
self.spec.codec != Codec::AmrWb
|| self.locked
|| local_is_controlling
|| !claimed.is_claimed(p.pt)
})
.filter_map(|p| self.match_score(p).map(|s| (p, s)))
.max_by_key(|(_, s)| *s)
else {
return;
};
let original_spec = self.spec;
// The first negotiated m-line determines TWCC support for this PT.
if !self.locked {
self.fb_transport_cc &= first.fb_transport_cc();
}
if self.spec.codec.is_tele() {
let local = self
.spec
.format
.telephone_event_max
.unwrap_or(FormatParams::DEFAULT_TELEPHONE_EVENT_MAX);
let remote = first
.spec
.format
.telephone_event_max
.unwrap_or(FormatParams::DEFAULT_TELEPHONE_EVENT_MAX);
self.spec.format.telephone_event_max = Some(local.min(remote));
}
// Mirror the remote's H.265 fmtp shape: echo back only the params they offered.
if self.spec.codec == Codec::H265 && first.spec.codec == Codec::H265 {
if let Some(remote_ptl) = first.spec.format.h265_profile_tier_level {
// Narrow level to min(local, remote) so the negotiated level
// never exceeds either side's capability.
let negotiated_ptl =
if let Some(local_ptl) = self.spec.format.h265_profile_tier_level {
remote_ptl.with_level(std::cmp::min(local_ptl.level(), remote_ptl.level()))
} else {
remote_ptl
};
self.spec.format.h265_profile_tier_level = Some(negotiated_ptl);
// Avoid also serializing `profile-id` via the VP9 `profile_id` field.
self.spec.format.profile_id = None;
} else if let Some(profile_id) = first.spec.format.profile_id {
// Remote only offered `profile-id`.
self.spec.format.h265_profile_tier_level = None;
self.spec.format.profile_id = Some(profile_id);
} else {
// No remote H.265 fmtp, omit ours as well.
self.spec.format.h265_profile_tier_level = None;
self.spec.format.profile_id = None;
}
// Adopt remote's sprop-max-don-diff so the depacketizer knows whether
// incoming packets contain DONL fields (RFC 7798 §7.1).
// The packetizer also uses this to decide whether to emit DONL.
if first.spec.format.sprop_max_don_diff.is_some() {
self.spec.format.sprop_max_don_diff = first.spec.format.sprop_max_don_diff;
}
}
// Same negotiation for H.266 (RFC 9328 §7.2, offer/answer model §7.3.2).
if self.spec.codec == Codec::H266 && first.spec.codec == Codec::H266 {
if let Some(remote_ptl) = first.spec.format.h266_profile_tier_level {
// Narrow level to min(local, remote) so the negotiated level
// never exceeds either side's capability.
let negotiated_ptl =
if let Some(local_ptl) = self.spec.format.h266_profile_tier_level {
remote_ptl.with_level(std::cmp::min(local_ptl.level(), remote_ptl.level()))
} else {
remote_ptl
};
self.spec.format.h266_profile_tier_level = Some(negotiated_ptl);
// Avoid also serializing `profile-id` via the VP9 `profile_id` field.
self.spec.format.profile_id = None;
} else if let Some(profile_id) = first.spec.format.profile_id {
// Remote only offered `profile-id`.
self.spec.format.h266_profile_tier_level = None;
self.spec.format.profile_id = Some(profile_id);
} else {
// No remote H.266 fmtp, omit ours as well.
self.spec.format.h266_profile_tier_level = None;
self.spec.format.profile_id = None;
}
// Adopt remote's sprop-max-don-diff (RFC 9328 §7.2) — enables
// DONL on both packetizer and depacketizer.
if first.spec.format.sprop_max_don_diff.is_some() {
self.spec.format.sprop_max_don_diff = first.spec.format.sprop_max_don_diff;
}
}
// KNOWN LIMITATION: H.264 has no equivalent level-narrowing pass here.
// `match_h264_score` treats the level as negotiable, but we keep our
// locally configured `profile-level-id` in the answer instead of
// narrowing it to `min(local, remote)`. Per RFC 6184 §8.2.2, when
// `level-asymmetry-allowed` is not set the answer should use the lower
// level.
// RFC 4867 §8.3.1: an answer repeats the offer's octet-align and channels,
// and a mode-set from either side applies to both directions.
if self.spec.codec == Codec::AmrWb {
let remote = first.spec;
self.spec.channels = remote.channels;
self.spec.format.octet_align = remote.format.octet_align;
self.spec.format.mode_set = self.spec.format.mode_set.or(remote.format.mode_set);
}
let mut remote_pt = first.pt;
let mut remote_rtx = first.resend;
// The RTX payload type (RFC 4588 §8.6) is a media format, and for unicast
// the answerer selects formats (RFC 3264 §6.1, Unicast Streams) "from
// amongst those listed in the offer". So if we didn't offer RTX for this
// codec, don't adopt the remote's RTX PT. Once locked, that holds only for parameters
// locked without RTX of our own: a locked `resend` of `None` that came from the remote
// is checked against the remote's RTX like any other locked value.
if self.resend.is_none() && (!self.locked || self.rtx_declined) {
remote_rtx = None;
}
if self.locked {
if self.spec != original_spec {
warn!("Ignore remote codec parameters for locked PT {}", self.pt);
self.spec = original_spec;
}
// This can happen if the incoming PTs are suggestions (send-direction) rather than demanded
// (receive-direction). We only want to warn if we get receive direction changes.
if local_is_controlling {
return;
}
// Just verify it's still the same. We should validate this in apply_offer/answer instead
// of ever seeing this error message.
if self.pt != remote_pt {
warn!("Ignore remote PT change {} => {}", self.pt, remote_pt);
}
if self.resend != remote_rtx {
warn!(
"Ignore remote PT RTX change {:?} => {:?}",
self.resend, remote_rtx
);
}
if self.red != first.red {
warn!(
"Ignore remote PT RED change {:?} => {:?}",
self.red, first.red
);
}
} else {
// Before locking, check if the remote PT conflicts with an already locked PT.
// If we're receiving, we control what PTs to use,
// so we can remap the conflicting PT.
if local_is_controlling && claimed.is_claimed(remote_pt) {
// Prefer our configured PT, then fall back to any free PT.
let new_pt = (!claimed.is_claimed(self.pt))
.then_some(self.pt)
.or_else(|| claimed.find_unclaimed(PREFERED_RANGES, unlocked));
if let Some(new_pt) = new_pt {
debug!(
"Remapped conflicting PT {} => {} for codec {:?}",
remote_pt, new_pt, self.spec.codec
);
remote_pt = new_pt;
unlocked.remove(&new_pt);
} else {
warn!("Exhausted PT ranges; ignoring remote payload");
return;
}
}
// Lock down the PT
if !claimed.assert_claim_once(remote_pt) {
return;
}
self.pt = remote_pt;
// Check if RTX PT also conflicts
if local_is_controlling {
if let Some(rtx) = remote_rtx {
if claimed.is_claimed(rtx) {
let new_rtx = self
.resend
.filter(|pt| !claimed.is_claimed(*pt))
.or_else(|| claimed.find_unclaimed(PREFERED_RANGES, unlocked));
if let Some(new_rtx) = new_rtx {
debug!("Remapped conflicting RTX PT {:?} => {}", rtx, new_rtx);
remote_rtx = Some(new_rtx);
unlocked.remove(&new_rtx);
} else {
warn!("Exhausted PT ranges; ignoring remote RTX");
remote_rtx = None;
}
}
}
}
// Remember whether we lock without RTX of our own (see `rtx_declined`).
self.rtx_declined = self.resend.is_none();
// Lock down the RTX PT
self.resend = remote_rtx;
if let Some(rtx) = remote_rtx {
if !claimed.assert_claim_once(rtx) {
self.resend = None;
}
}
// RED is opt-in on our side and audio-only: keep it only if the matched remote also
// offers RED (`and`) and our codec is an audio codec. A RED PT can still collide (two
// codecs mapped to the same PT), so resolve any collision before claiming it. The
// collision check runs regardless of who controls PT allocation: only the resolution
// differs, and claiming the same PT twice would panic.
let mut remote_red = self
.red
.and(first.red)
.filter(|_| self.spec.codec.is_audio());
if let Some(red) = remote_red {
if claimed.is_claimed(red) {
if local_is_controlling {
// We control PT allocation: remap the conflicting RED to a free PT.
if let Some(new_red) = claimed.find_unclaimed(PREFERED_RANGES, unlocked) {
debug!("Remapped conflicting RED PT {:?} => {}", red, new_red);
remote_red = Some(new_red);
unlocked.remove(&new_red);
} else {
// RED is opt-in; drop it rather than panic if no PT is free.
debug!("No free PT for RED; dropping it");
remote_red = None;
}
} else {
// The remote dictated the PTs and mapped a RED PT that is already taken.
// We can't reassign PTs the remote controls, and RED is opt-in, so drop it
// rather than claim the same PT twice (which would panic).
debug!("Conflicting remote RED PT {:?}; dropping it", red);
remote_red = None;
}
}
}
self.red = remote_red;
if let Some(red) = remote_red {
claimed.assert_claim_once(red);
}
// The answerer removes rtcp-fb attributes it does not support, and per
// RFC 4585 §4.2 "both offerer and answerer MUST only use feedback
// mechanisms negotiated in this way". So intersect our local `fb_nack`
// with what the remote signaled (`a=rtcp-fb <pt> nack`). Runs once at
// lock time, when `self.fb_nack` still holds the local desired value.
self.fb_nack = self.fb_nack && first.fb_nack;
// This is now locked.
self.locked = true;
}
}
}
#[cfg(test)]
mod test {
use crate::rtp_::Frequency;
use super::*;
use crate::format::{CodecSpec, FormatParams};
#[test]
fn payload_params_red_defaults_none() {
let spec = CodecSpec {
codec: Codec::Opus,
clock_rate: Frequency::NINETY_KHZ,
channels: Some(2),
format: FormatParams::default(),
};
let p = PayloadParams::new(Pt::new_with_value(111), None, spec);
assert_eq!(p.red(), None);
}
#[test]
fn red_pt_remapped_on_conflict() {
use std::collections::HashSet;
// When we control PT allocation (the remote is send-only) and the RED PT we would adopt
// from the remote is already taken, RED is remapped to a free PT rather than colliding
// or being dropped.
let spec = CodecSpec {
codec: Codec::Opus,
clock_rate: Frequency::FORTY_EIGHT_KHZ,
channels: Some(2),
format: FormatParams::default(),
};
let red_pt = Pt::new_with_value(63);
let mut local = PayloadParams::new(Pt::new_with_value(111), None, spec);
local.red = Some(red_pt);
let mut remote = PayloadParams::new(Pt::new_with_value(111), None, spec);
remote.red = Some(red_pt);
// Pre-claim the RED PT as if another payload already holds it.
let mut claimed = [false; 128];
claimed[*red_pt as usize] = true;
let mut unlocked = HashSet::new();
// local_is_controlling = true: we allocate PTs and may remap the conflict.
local.update_param(&[remote], &mut claimed, true, &mut unlocked);
let got = local.red.expect("RED is kept, remapped to a free PT");
assert_ne!(
got, red_pt,
"RED PT must be remapped away from the conflict"
);
assert!(
claimed.is_claimed(got),
"the remapped RED PT must be claimed"
);
}
mod h264 {
use super::*;
fn h264_codec_spec(
level_asymmetry_allowed: Option<bool>,
packetization_mode: Option<u8>,
profile_level_id: Option<u32>,
) -> CodecSpec {
CodecSpec {
codec: Codec::H264,
clock_rate: Frequency::NINETY_KHZ,
channels: None,
format: FormatParams {
level_asymmetry_allowed,
packetization_mode,
profile_level_id,
..Default::default()
},
}
}
#[test]
fn test_h264_profile_matching() {
struct Case {
c0: CodecSpec,
c1: CodecSpec,
must_match: bool,
msg: &'static str,
}
let cases = [
Case {
c0: h264_codec_spec(None, None, Some(0x42E01F)),
c1: h264_codec_spec(None, None, Some(0x4DA01F)),
must_match: true,
msg: "0x42A01F and 0x4DF01F should match, they are both constrained baseline subprofile",
},
Case {
c0: h264_codec_spec(None, None, Some(0x42E01F)),
c1: h264_codec_spec(None, Some(1), Some(0x4DA01F)),
must_match: false,
msg: "0x42A01F and 0x4DF01F with differing packetization modes should not match",
},
Case {
c0: h264_codec_spec(None, Some(0), Some(0x422000)),
c1: h264_codec_spec(None, None, Some(0x42B00A)),
must_match: true,
msg: "0x424000 and 0x42B00A should match because they are both the baseline subprofile \
and the level idc of 0x42F01F will be adjusted to Level1B because the constraint \
set 3 flag is set",
},
];
for Case {
c0,
c1,
must_match,
msg,
} in cases.into_iter()
{
let matched = PayloadParams::match_h264_score(c0, c1).is_some();
assert_eq!(matched, must_match, "{msg}\nc0: {c0:#?}\nc1: {c1:#?}");
}
}
#[test]
fn test_h264_level_matching_rfc_compliant() {
struct Case {
c0: CodecSpec,
c1: CodecSpec,
expected: Option<usize>,
msg: &'static str,
}
let cases = [
// Test 1: Same profile, same level -> should match
Case {
c0: h264_codec_spec(None, None, Some(0x42e028)), // CB L4.0
c1: h264_codec_spec(None, None, Some(0x42e028)), // CB L4.0
expected: Some(PayloadParams::EXACT_MATCH_SCORE),
msg: "Same profile (CB) and same level (4.0) should match",
},
// Test 2: Same profile, offered level lower -> should match (RFC 6184)
Case {
c0: h264_codec_spec(None, None, Some(0x42e028)), // CB L4.0 (configured)
c1: h264_codec_spec(None, None, Some(0x42e01f)), // CB L3.1 (offered)
expected: Some(PayloadParams::EXACT_MATCH_SCORE - 2),
msg: "Same profile (CB), offered level 3.1 < configured 4.0 should match per RFC 6184",
},
// Test 3: Same profile, offered level higher -> should match (RFC 6184)
Case {
c0: h264_codec_spec(None, None, Some(0x42e01f)), // CB L3.1 (configured)
c1: h264_codec_spec(None, None, Some(0x42e028)), // CB L4.0 (offered)
expected: Some(PayloadParams::EXACT_MATCH_SCORE - 2),
msg: "Same profile (CB), offered level 4.0 > configured 3.1 should match, \
the level is negotiable per RFC 6184",
},
// Test 4: Different profiles -> should NOT match
Case {
c0: h264_codec_spec(None, None, Some(0x42e028)), // CB L4.0
c1: h264_codec_spec(None, None, Some(0x4d0028)), // Main L4.0
expected: None,
msg: "Different profiles (CB vs Main) should NOT match even with same level",
},
// Test 5: Main profile, offered lower level -> should match
Case {
c0: h264_codec_spec(None, None, Some(0x4d002a)), // Main L4.2 (configured)
c1: h264_codec_spec(None, None, Some(0x4d0028)), // Main L4.0 (offered)
expected: Some(PayloadParams::EXACT_MATCH_SCORE - 2),
msg: "Same profile (Main), offered level 4.0 < configured 4.2 should match",
},
// Test 6: High profile, multiple levels down -> should match
Case {
c0: h264_codec_spec(None, None, Some(0x640033)), // High L5.1 (configured)
c1: h264_codec_spec(None, None, Some(0x64001f)), // High L3.1 (offered)
expected: Some(PayloadParams::EXACT_MATCH_SCORE - 6),
msg: "Same profile (High), offered level 3.1 < configured 5.1 should match",
},
// Test 7: Baseline (not CB) with Level1B as offered
Case {
c0: h264_codec_spec(None, None, Some(0x42001f)), // Baseline L3.1
c1: h264_codec_spec(None, None, Some(0x420000)), // Baseline Level1B
expected: Some(PayloadParams::EXACT_MATCH_SCORE - 8),
msg: "Same profile (Baseline), offered level 1B < configured 3.1 should match",
},
// Test 8: Baseline (not CB) offered lower level > should match with (special Level1B case)
Case {
c0: h264_codec_spec(None, None, Some(0x420000)), // Baseline Level1B
c1: h264_codec_spec(None, None, Some(0x42000a)), // Baseline L1
expected: Some(PayloadParams::EXACT_MATCH_SCORE - 1),
msg: "Same profile (Baseline), offered level 1 < configured 1B should match",
},
// Test 9: Baseline (not CB) offered higher level -> should match (special Level1B case)
Case {
c0: h264_codec_spec(None, None, Some(0x42000a)), // Baseline L1
c1: h264_codec_spec(None, None, Some(0x420000)), // Baseline Level1B
expected: Some(PayloadParams::EXACT_MATCH_SCORE - 1),
msg: "Same profile (Baseline), offered level 1B > configured 1 should match",
},
// Test 10: Offered Constrained Baseline level 5.2 is higher than
// the default configured 3.1. Must match.
Case {
c0: h264_codec_spec(Some(true), Some(1), Some(0x42e01f)), // CB L3.1 (configured)
c1: h264_codec_spec(Some(true), Some(1), Some(0x42e034)), // CB L5.2 (offered)
expected: Some(PayloadParams::EXACT_MATCH_SCORE - 7),
msg: "Same profile (CB), offered level 5.2 > configured 3.1 should match",
},
// Test 11: Constrained High (profile-iop 0x0C) must be recognized
// and match a configured Constrained High.
Case {
c0: h264_codec_spec(Some(true), Some(1), Some(0x640c1f)), // CH L3.1 (configured)
c1: h264_codec_spec(Some(true), Some(1), Some(0x640c34)), // CH L5.2 (offered)
expected: Some(PayloadParams::EXACT_MATCH_SCORE - 7),
msg: "Same profile (Constrained High), offered level 5.2 should match",
},
// Test 12: Constrained High is not the same profile as High.
Case {
c0: h264_codec_spec(None, None, Some(0x64001f)), // High L3.1
c1: h264_codec_spec(None, None, Some(0x640c1f)), // Constrained High L3.1
expected: None,
msg: "Different profiles (High vs Constrained High) should NOT match",
},
];
for Case {
c0,
c1,
expected,
msg,
} in cases.into_iter()
{
assert_eq!(
PayloadParams::match_h264_score(c0, c1),
expected,
"{msg}\nc0: {c0:#?}\nc1: {c1:#?}"
);
}
}
}
mod h265 {
use super::*;
use crate::packet::Packetizer;
fn h265_codec_spec(profile_id: u8, tier_flag: u8, level_id: u8) -> CodecSpec {
CodecSpec {
codec: Codec::H265,
clock_rate: Frequency::NINETY_KHZ,
channels: None,
format: FormatParams {
h265_profile_tier_level: Some(
H265ProfileTierLevel::new(profile_id, tier_flag, level_id).unwrap(),
),
..Default::default()
},
}
}
fn h265_codec_spec_profile_only(profile_id: u32) -> CodecSpec {
CodecSpec {
codec: Codec::H265,
clock_rate: Frequency::NINETY_KHZ,
channels: None,
format: FormatParams {
profile_id: Some(profile_id),
..Default::default()
},
}
}
/// Test basic H.265 profile/tier/level matching scenarios.
/// Verifies that full ProfileTierLevel parameters are matched correctly.
#[test]
fn test_profile_tier_level_matching() {
struct Case {
c0: CodecSpec,
c1: CodecSpec,
must_match: bool,
msg: &'static str,
}
let cases = [
// Same profile, tier, level -> should match
Case {
c0: h265_codec_spec(1, 0, 93), // Main, Main tier, Level 3.1
c1: h265_codec_spec(1, 0, 93),
must_match: true,
msg: "Same profile (Main), tier (Main), level (3.1) should match",
},
// Same profile and tier, offered level lower -> should match
Case {
c0: h265_codec_spec(1, 0, 120), // Main, Main tier, Level 4.0
c1: h265_codec_spec(1, 0, 93), // Main, Main tier, Level 3.1
must_match: true,
msg: "Same profile/tier, offered level 3.1 < configured 4.0 should match",
},
// Same profile and tier, offered level higher -> should still match (with penalty)
Case {
c0: h265_codec_spec(1, 0, 93), // Main, Main tier, Level 3.1
c1: h265_codec_spec(1, 0, 120), // Main, Main tier, Level 4.0
must_match: true,
msg: "Same profile/tier, offered level 4.0 > configured 3.1 should match (penalized)",
},
// Different profiles -> should NOT match
Case {
c0: h265_codec_spec(1, 0, 93), // Main, Main tier, Level 3.1
c1: h265_codec_spec(2, 0, 93), // Main10, Main tier, Level 3.1
must_match: false,
msg: "Different profiles (Main vs Main10) should NOT match",
},
// Different tiers -> should NOT match
Case {
c0: h265_codec_spec(1, 0, 93), // Main, Main tier, Level 3.1
c1: h265_codec_spec(1, 1, 93), // Main, High tier, Level 3.1
must_match: false,
msg: "Different tiers (Main vs High) should NOT match",
},
// Main10 profile, same tier, lower level -> should match
Case {
c0: h265_codec_spec(2, 0, 153), // Main10, Main tier, Level 5.1
c1: h265_codec_spec(2, 0, 120), // Main10, Main tier, Level 4.0
must_match: true,
msg: "Main10 profile, offered level 4.0 < configured 5.1 should match",
},
// High tier, multiple levels down -> should match
Case {
c0: h265_codec_spec(1, 1, 153), // Main, High tier, Level 5.1
c1: h265_codec_spec(1, 1, 93), // Main, High tier, Level 3.1
must_match: true,
msg: "High tier, offered level 3.1 < configured 5.1 should match",
},
];
for Case {
c0,
c1,
must_match,
msg,
} in cases.into_iter()
{
let matched = PayloadParams::match_h265_score(c0, c1).is_some();
assert_eq!(matched, must_match, "{msg}\nc0: {c0:#?}\nc1: {c1:#?}");
}
}
/// Test H.265 level matching with exact scores.
/// Verifies that the score decrements based on level difference.
#[test]
fn test_level_matching_scores() {
struct Case {
c0: CodecSpec,
c1: CodecSpec,
expected: Option<usize>,
msg: &'static str,
}
let cases = [
// Exact match -> score 100
Case {
c0: h265_codec_spec(1, 0, 93), // Main, Main tier, Level 3.1
c1: h265_codec_spec(1, 0, 93),
expected: Some(PayloadParams::EXACT_MATCH_SCORE),
msg: "Exact match should return score 100",
},
// One level down -> score 100 - 1
Case {
c0: h265_codec_spec(1, 0, 120), // Main, Main tier, Level 4.0
c1: h265_codec_spec(1, 0, 93), // Main, Main tier, Level 3.1
expected: Some(PayloadParams::EXACT_MATCH_SCORE - 1),
msg: "One level difference should return score 99",
},
// Two levels down -> score 100 - 2
Case {
c0: h265_codec_spec(1, 0, 123), // Main, Main tier, Level 4.1
c1: h265_codec_spec(1, 0, 93), // Main, Main tier, Level 3.1
expected: Some(PayloadParams::EXACT_MATCH_SCORE - 2),
msg: "Two level difference should return score 98",
},
// Multiple levels down
Case {
c0: h265_codec_spec(1, 0, 153), // Main, Main tier, Level 5.1
c1: h265_codec_spec(1, 0, 93), // Main, Main tier, Level 3.1
expected: Some(PayloadParams::EXACT_MATCH_SCORE - 4),
msg: "Four level difference (5.1 to 3.1) should return score 96",
},
// Offered level higher -> still matches, penalized by 1 ordinal
Case {
c0: h265_codec_spec(1, 0, 93), // Main, Main tier, Level 3.1
c1: h265_codec_spec(1, 0, 120), // Main, Main tier, Level 4.0
expected: Some(PayloadParams::EXACT_MATCH_SCORE - 1),
msg: "Offered level one higher than configured should return score 99",
},
// Different profiles -> None
Case {
c0: h265_codec_spec(1, 0, 93), // Main, Main tier, Level 3.1
c1: h265_codec_spec(2, 0, 93), // Main10, Main tier, Level 3.1
expected: None,
msg: "Different profiles should return None",
},
// Different tiers -> None
Case {
c0: h265_codec_spec(1, 0, 93), // Main, Main tier, Level 3.1
c1: h265_codec_spec(1, 1, 93), // Main, High tier, Level 3.1
expected: None,
msg: "Different tiers should return None",
},
];
for Case {
c0,
c1,
expected,
msg,
} in cases.into_iter()
{
assert_eq!(
PayloadParams::match_h265_score(c0, c1),
expected,
"{msg}\nc0: {c0:#?}\nc1: {c1:#?}"
);
}
}
/// Test H.265 profile-only matching (for Chrome compatibility).
/// When only profile-id is provided without tier/level, match on profile only.
#[test]
fn test_profile_only_matching() {
struct Case {
c0: CodecSpec,
c1: CodecSpec,
expected: Option<usize>,
msg: &'static str,
}
let cases = [
// Both have profile-only, same profile -> score 90
Case {
c0: h265_codec_spec_profile_only(1), // Main
c1: h265_codec_spec_profile_only(1), // Main
expected: Some(PayloadParams::PROFILE_ONLY_MATCH_SCORE),
msg: "Profile-only match (Main) should return score 90",
},
// Both have profile-only, different profiles -> None
Case {
c0: h265_codec_spec_profile_only(1), // Main
c1: h265_codec_spec_profile_only(2), // Main10
expected: None,
msg: "Profile-only with different profiles should return None",
},
// One has full PTL, other has profile-only, same profile -> score 90
Case {
c0: h265_codec_spec(1, 0, 93), // Main, Main tier, Level 3.1
c1: h265_codec_spec_profile_only(1), // Main
expected: Some(PayloadParams::PROFILE_ONLY_MATCH_SCORE),
msg: "Mixed PTL and profile-only with matching profile should return score 90",
},
// One has full PTL, other has profile-only, different profiles -> None
Case {
c0: h265_codec_spec(1, 0, 93), // Main, Main tier, Level 3.1
c1: h265_codec_spec_profile_only(2), // Main10
expected: None,
msg: "Mixed PTL and profile-only with different profiles should return None",
},
// Reverse: profile-only vs full PTL, same profile -> score 90
Case {
c0: h265_codec_spec_profile_only(1), // Main
c1: h265_codec_spec(1, 0, 93), // Main, Main tier, Level 3.1
expected: Some(PayloadParams::PROFILE_ONLY_MATCH_SCORE),
msg: "Profile-only vs full PTL with matching profile should return score 90",
},
];
for Case {
c0,
c1,
expected,
msg,
} in cases.into_iter()
{
assert_eq!(
PayloadParams::match_h265_score(c0, c1),
expected,
"{msg}\nc0: {c0:#?}\nc1: {c1:#?}"
);
}
}
/// Test negative cases for H.265 matching to ensure proper rejection.
#[test]
fn test_negative_cases() {
struct Case {
c0: CodecSpec,
c1: CodecSpec,
msg: &'static str,
}
let cases = [
// Profile mismatch with full PTL
Case {
c0: h265_codec_spec(1, 0, 93), // Main, Main tier, Level 3.1
c1: h265_codec_spec(2, 0, 93), // Main10, Main tier, Level 3.1
msg: "Profile mismatch (Main vs Main10) must not match",
},
// Tier mismatch
Case {
c0: h265_codec_spec(1, 0, 93), // Main, Main tier, Level 3.1
c1: h265_codec_spec(1, 1, 93), // Main, High tier, Level 3.1
msg: "Tier mismatch (Main vs High) must not match",
},
// All three different (profile+tier mismatch rejects)
Case {
c0: h265_codec_spec(1, 0, 93), // Main, Main tier, Level 3.1
c1: h265_codec_spec(2, 1, 153), // Main10, High tier, Level 5.1
msg: "Profile and tier different must not match",
},
// Profile-only mismatch
Case {
c0: h265_codec_spec_profile_only(1), // Main
c1: h265_codec_spec_profile_only(2), // Main10
msg: "Profile-only mismatch must not match",
},
// Mixed full PTL vs profile-only with different profiles
Case {
c0: h265_codec_spec(2, 0, 93), // Main10, Main tier, Level 3.1
c1: h265_codec_spec_profile_only(1), // Main
msg: "Full PTL (Main10) vs profile-only (Main) must not match",
},
];
for Case { c0, c1, msg } in cases.into_iter() {
assert_eq!(
PayloadParams::match_h265_score(c0, c1),
None,
"{msg}\nc0: {c0:#?}\nc1: {c1:#?}"
);
}
}
/// Test that sprop-max-don-diff is adopted from remote SDP during negotiation,
/// and that packetizer/depacketizer are correctly initialized with DONL enabled.
#[test]
fn test_sprop_max_don_diff_negotiation_enables_donl() {
use crate::packet::{CodecDepacketizer, CodecPacketizer};
// Local config: no sprop-max-don-diff (default)
let local_spec = h265_codec_spec(1, 0, 93);
let mut local_params = PayloadParams::new(
Pt::new_with_value(102),
Some(Pt::new_with_value(103)),
local_spec,
);
assert_eq!(
local_params.spec.format.sprop_max_don_diff, None,
"Local should have no sprop-max-don-diff initially"
);
// Remote offers H.265 with sprop-max-don-diff=32
let mut remote_spec = h265_codec_spec(1, 0, 93);
remote_spec.format.sprop_max_don_diff = Some(32);
let remote_params = PayloadParams::new(
Pt::new_with_value(102),
Some(Pt::new_with_value(103)),
remote_spec,
);
// Simulate SDP negotiation (update_param merges remote into local)
let mut claimed = [false; 128];
let mut unlocked = std::collections::HashSet::new();
local_params.update_param(
&[remote_params],
&mut claimed,
false, // remote is controlling (we're answering)
&mut unlocked,
);
// Verify sprop-max-don-diff was adopted from remote
assert_eq!(
local_params.spec.format.sprop_max_don_diff,
Some(32),
"sprop-max-don-diff should be adopted from remote after negotiation"
);
// Verify packetizer is initialized with DONL enabled
let donl_enabled = local_params.spec.format.sprop_max_don_diff.unwrap_or(0) > 0;
assert!(
donl_enabled,
"DONL should be enabled when sprop-max-don-diff > 0"
);
let mut pack = CodecPacketizer::new(
local_params.spec.codec,
crate::format::Vp9PacketizerMode::default(),
);
if let CodecPacketizer::H265(ref mut h265) = pack {
h265.with_donl(donl_enabled);
}
// Packetize a NAL — should include DONL field (2 extra bytes)
let nalu = vec![0x02, 0x01, 0xAA, 0xBB];
let packets = pack.packetize(1200, &nalu).unwrap();
assert_eq!(packets.len(), 1);
assert_eq!(
packets[0].len(),
nalu.len() + 2, // +2 for DONL
"Packetized output should include 2-byte DONL field"
);
// Verify depacketizer is initialized with DONL enabled
let mut depack: CodecDepacketizer = local_params.spec.codec.into();
if let CodecDepacketizer::H265(ref mut h265) = depack {
h265.with_donl(donl_enabled);
}
// Depacketize the packet — should correctly strip DONL
use crate::packet::{CodecExtra, Depacketizer};
let mut out = Vec::new();
let mut extra = CodecExtra::None;
if let CodecDepacketizer::H265(ref mut h265) = depack {
h265.depacketize(&packets[0], &mut out, &mut extra).unwrap();
}
// Output should be Annex-B: start code + original NAL
assert_eq!(&out[0..4], &[0x00, 0x00, 0x00, 0x01]);
assert_eq!(
&out[4..],
&nalu[..],
"Depacketized NAL should match original"
);
}
/// Test that sprop-max-don-diff=0 from remote does NOT enable DONL.
#[test]
fn test_sprop_max_don_diff_zero_disables_donl() {
let local_spec = h265_codec_spec(1, 0, 93);
let mut local_params = PayloadParams::new(
Pt::new_with_value(102),
Some(Pt::new_with_value(103)),
local_spec,
);
// Remote offers with sprop-max-don-diff=0 (no reordering)
let mut remote_spec = h265_codec_spec(1, 0, 93);
remote_spec.format.sprop_max_don_diff = Some(0);
let remote_params = PayloadParams::new(
Pt::new_with_value(102),
Some(Pt::new_with_value(103)),
remote_spec,
);
let mut claimed = [false; 128];
let mut unlocked = std::collections::HashSet::new();
local_params.update_param(&[remote_params], &mut claimed, false, &mut unlocked);
assert_eq!(local_params.spec.format.sprop_max_don_diff, Some(0));
let donl_enabled = local_params.spec.format.sprop_max_don_diff.unwrap_or(0) > 0;
assert!(
!donl_enabled,
"DONL should NOT be enabled when sprop-max-don-diff=0"
);
// Packetize without DONL — packet should be same size as NAL
let nalu = vec![0x02, 0x01, 0xAA, 0xBB];
let mut pack = crate::packet::CodecPacketizer::new(
local_params.spec.codec,
crate::format::Vp9PacketizerMode::default(),
);
// Don't call with_donl(true) since donl_enabled is false
let packets = pack.packetize(1200, &nalu).unwrap();
assert_eq!(
packets[0].len(),
nalu.len(),
"No DONL field when sprop-max-don-diff=0"
);
}
/// Test that missing sprop-max-don-diff from remote preserves local value.
#[test]
fn test_sprop_max_don_diff_absent_preserves_local() {
// Local has sprop-max-don-diff=16
let mut local_spec = h265_codec_spec(1, 0, 93);
local_spec.format.sprop_max_don_diff = Some(16);
let mut local_params = PayloadParams::new(
Pt::new_with_value(102),
Some(Pt::new_with_value(103)),
local_spec,
);
// Remote offers without sprop-max-don-diff
let remote_spec = h265_codec_spec(1, 0, 93);
assert!(remote_spec.format.sprop_max_don_diff.is_none());
let remote_params = PayloadParams::new(
Pt::new_with_value(102),
Some(Pt::new_with_value(103)),
remote_spec,
);
let mut claimed = [false; 128];
let mut unlocked = std::collections::HashSet::new();
local_params.update_param(&[remote_params], &mut claimed, false, &mut unlocked);
// Local value should be preserved (remote didn't override)
assert_eq!(
local_params.spec.format.sprop_max_don_diff,
Some(16),
"Local sprop-max-don-diff should be preserved when remote doesn't specify it"
);
}
}
mod h266 {
use super::*;
use crate::packet::H266ProfileTierLevel;
use crate::packet::Packetizer;
fn h266_codec_spec(profile_id: u8, tier_flag: u8, level_id: u8) -> CodecSpec {
CodecSpec {
codec: Codec::H266,
clock_rate: Frequency::NINETY_KHZ,
channels: None,
format: FormatParams {
h266_profile_tier_level: Some(
H266ProfileTierLevel::new(profile_id, tier_flag, level_id).unwrap(),
),
..Default::default()
},
}
}
fn h266_codec_spec_profile_only(profile_id: u32) -> CodecSpec {
CodecSpec {
codec: Codec::H266,
clock_rate: Frequency::NINETY_KHZ,
channels: None,
format: FormatParams {
profile_id: Some(profile_id),
..Default::default()
},
}
}
/// Test basic H.266 profile/tier/level matching scenarios.
/// Verifies that full ProfileTierLevel parameters are matched correctly.
#[test]
fn test_profile_tier_level_matching() {
struct Case {
c0: CodecSpec,
c1: CodecSpec,
must_match: bool,
msg: &'static str,
}
let cases = [
// Same profile, tier, level -> should match
Case {
c0: h266_codec_spec(1, 0, 51), // Main, Main tier, Level 3.1
c1: h266_codec_spec(1, 0, 51),
must_match: true,
msg: "Same profile (Main), tier (Main), level (3.1) should match",
},
// Same profile and tier, offered level lower -> should match
Case {
c0: h266_codec_spec(1, 0, 64), // Main, Main tier, Level 4.0
c1: h266_codec_spec(1, 0, 51), // Main, Main tier, Level 3.1
must_match: true,
msg: "Same profile/tier, offered level 3.1 < configured 4.0 should match",
},
// Same profile and tier, offered level higher -> should still match (with penalty)
Case {
c0: h266_codec_spec(1, 0, 51), // Main, Main tier, Level 3.1
c1: h266_codec_spec(1, 0, 64), // Main, Main tier, Level 4.0
must_match: true,
msg: "Same profile/tier, offered level 4.0 > configured 3.1 should match (penalized)",
},
// Different profiles -> should NOT match
Case {
c0: h266_codec_spec(1, 0, 51), // Main, Main tier, Level 3.1
c1: h266_codec_spec(33, 0, 51), // Main10, Main tier, Level 3.1
must_match: false,
msg: "Different profiles (Main vs Main10) should NOT match",
},
// Different tiers -> should NOT match
Case {
c0: h266_codec_spec(1, 0, 51), // Main, Main tier, Level 3.1
c1: h266_codec_spec(1, 1, 51), // Main, High tier, Level 3.1
must_match: false,
msg: "Different tiers (Main vs High) should NOT match",
},
// Main10 profile, same tier, lower level -> should match
Case {
c0: h266_codec_spec(33, 0, 83), // Main10, Main tier, Level 5.1
c1: h266_codec_spec(33, 0, 64), // Main10, Main tier, Level 4.0
must_match: true,
msg: "Main10 profile, offered level 4.0 < configured 5.1 should match",
},
// High tier, multiple levels down -> should match
Case {
c0: h266_codec_spec(1, 1, 83), // Main, High tier, Level 5.1
c1: h266_codec_spec(1, 1, 51), // Main, High tier, Level 3.1
must_match: true,
msg: "High tier, offered level 3.1 < configured 5.1 should match",
},
];
for Case {
c0,
c1,
must_match,
msg,
} in cases.into_iter()
{
let matched = PayloadParams::match_h266_score(c0, c1).is_some();
assert_eq!(matched, must_match, "{msg}\nc0: {c0:#?}\nc1: {c1:#?}");
}
}
/// Test H.266 level matching with exact scores.
/// Verifies that the score decrements based on level difference.
#[test]
fn test_level_matching_scores() {
struct Case {
c0: CodecSpec,
c1: CodecSpec,
expected: Option<usize>,
msg: &'static str,
}
let cases = [
// Exact match -> score 100
Case {
c0: h266_codec_spec(1, 0, 51), // Main, Main tier, Level 3.1
c1: h266_codec_spec(1, 0, 51),
expected: Some(PayloadParams::EXACT_MATCH_SCORE),
msg: "Exact match should return score 100",
},
// One level down -> score 100 - 1
Case {
c0: h266_codec_spec(1, 0, 64), // Main, Main tier, Level 4.0
c1: h266_codec_spec(1, 0, 51), // Main, Main tier, Level 3.1
expected: Some(PayloadParams::EXACT_MATCH_SCORE - 1),
msg: "One level difference should return score 99",
},
// Two levels down -> score 100 - 2
Case {
c0: h266_codec_spec(1, 0, 67), // Main, Main tier, Level 4.1
c1: h266_codec_spec(1, 0, 51), // Main, Main tier, Level 3.1
expected: Some(PayloadParams::EXACT_MATCH_SCORE - 2),
msg: "Two level difference should return score 98",
},
// Multiple levels down
Case {
c0: h266_codec_spec(1, 0, 83), // Main, Main tier, Level 5.1
c1: h266_codec_spec(1, 0, 51), // Main, Main tier, Level 3.1
expected: Some(PayloadParams::EXACT_MATCH_SCORE - 4),
msg: "Four level difference (5.1 to 3.1) should return score 96",
},
// Offered level higher -> still matches, penalized by 1 ordinal
Case {
c0: h266_codec_spec(1, 0, 51), // Main, Main tier, Level 3.1
c1: h266_codec_spec(1, 0, 64), // Main, Main tier, Level 4.0
expected: Some(PayloadParams::EXACT_MATCH_SCORE - 1),
msg: "Offered level one higher than configured should return score 99",
},
// Different profiles -> None
Case {
c0: h266_codec_spec(1, 0, 51), // Main, Main tier, Level 3.1
c1: h266_codec_spec(33, 0, 51), // Main10, Main tier, Level 3.1
expected: None,
msg: "Different profiles should return None",
},
// Different tiers -> None
Case {
c0: h266_codec_spec(1, 0, 51), // Main, Main tier, Level 3.1
c1: h266_codec_spec(1, 1, 51), // Main, High tier, Level 3.1
expected: None,
msg: "Different tiers should return None",
},
];
for Case {
c0,
c1,
expected,
msg,
} in cases.into_iter()
{
assert_eq!(
PayloadParams::match_h266_score(c0, c1),
expected,
"{msg}\nc0: {c0:#?}\nc1: {c1:#?}"
);
}
}
/// Test H.266 profile-only matching (for Chrome compatibility).
/// When only profile-id is provided without tier/level, match on profile only.
#[test]
fn test_profile_only_matching() {
struct Case {
c0: CodecSpec,
c1: CodecSpec,
expected: Option<usize>,
msg: &'static str,
}
let cases = [
// Both have profile-only, same profile -> score 90
Case {
c0: h266_codec_spec_profile_only(1), // Main
c1: h266_codec_spec_profile_only(1), // Main
expected: Some(PayloadParams::PROFILE_ONLY_MATCH_SCORE),
msg: "Profile-only match (Main) should return score 90",
},
// Both have profile-only, different profiles -> None
Case {
c0: h266_codec_spec_profile_only(1), // Main
c1: h266_codec_spec_profile_only(33), // Main10
expected: None,
msg: "Profile-only with different profiles should return None",
},
// One has full PTL, other has profile-only, same profile -> score 90
Case {
c0: h266_codec_spec(1, 0, 51), // Main, Main tier, Level 3.1
c1: h266_codec_spec_profile_only(1), // Main
expected: Some(PayloadParams::PROFILE_ONLY_MATCH_SCORE),
msg: "Mixed PTL and profile-only with matching profile should return score 90",
},
// One has full PTL, other has profile-only, different profiles -> None
Case {
c0: h266_codec_spec(1, 0, 51), // Main, Main tier, Level 3.1
c1: h266_codec_spec_profile_only(33), // Main10
expected: None,
msg: "Mixed PTL and profile-only with different profiles should return None",
},
// Reverse: profile-only vs full PTL, same profile -> score 90
Case {
c0: h266_codec_spec_profile_only(1), // Main
c1: h266_codec_spec(1, 0, 51), // Main, Main tier, Level 3.1
expected: Some(PayloadParams::PROFILE_ONLY_MATCH_SCORE),
msg: "Profile-only vs full PTL with matching profile should return score 90",
},
];
for Case {
c0,
c1,
expected,
msg,
} in cases.into_iter()
{
assert_eq!(
PayloadParams::match_h266_score(c0, c1),
expected,
"{msg}\nc0: {c0:#?}\nc1: {c1:#?}"
);
}
}
/// Test negative cases for H.266 matching to ensure proper rejection.
#[test]
fn test_negative_cases() {
struct Case {
c0: CodecSpec,
c1: CodecSpec,
msg: &'static str,
}
let cases = [
// Profile mismatch with full PTL
Case {
c0: h266_codec_spec(1, 0, 51), // Main, Main tier, Level 3.1
c1: h266_codec_spec(33, 0, 51), // Main10, Main tier, Level 3.1
msg: "Profile mismatch (Main vs Main10) must not match",
},
// Tier mismatch
Case {
c0: h266_codec_spec(1, 0, 51), // Main, Main tier, Level 3.1
c1: h266_codec_spec(1, 1, 51), // Main, High tier, Level 3.1
msg: "Tier mismatch (Main vs High) must not match",
},
// All three different (profile+tier mismatch rejects)
Case {
c0: h266_codec_spec(1, 0, 51), // Main, Main tier, Level 3.1
c1: h266_codec_spec(33, 1, 83), // Main10, High tier, Level 5.1
msg: "Profile and tier different must not match",
},
// Profile-only mismatch
Case {
c0: h266_codec_spec_profile_only(1), // Main
c1: h266_codec_spec_profile_only(33), // Main10
msg: "Profile-only mismatch must not match",
},
// Mixed full PTL vs profile-only with different profiles
Case {
c0: h266_codec_spec(33, 0, 51), // Main10, Main tier, Level 3.1
c1: h266_codec_spec_profile_only(1), // Main
msg: "Full PTL (Main10) vs profile-only (Main) must not match",
},
];
for Case { c0, c1, msg } in cases.into_iter() {
assert_eq!(
PayloadParams::match_h266_score(c0, c1),
None,
"{msg}\nc0: {c0:#?}\nc1: {c1:#?}"
);
}
}
/// Test that sprop-max-don-diff is adopted from remote SDP during negotiation,
/// and that packetizer/depacketizer are correctly initialized with DONL enabled.
#[test]
fn test_sprop_max_don_diff_negotiation_enables_donl() {
use crate::packet::{CodecDepacketizer, CodecPacketizer};
// Local config: no sprop-max-don-diff (default)
let local_spec = h266_codec_spec(1, 0, 51);
let mut local_params = PayloadParams::new(
Pt::new_with_value(102),
Some(Pt::new_with_value(103)),
local_spec,
);
assert_eq!(
local_params.spec.format.sprop_max_don_diff, None,
"Local should have no sprop-max-don-diff initially"
);
// Remote offers H.266 with sprop-max-don-diff=32
let mut remote_spec = h266_codec_spec(1, 0, 51);
remote_spec.format.sprop_max_don_diff = Some(32);
let remote_params = PayloadParams::new(
Pt::new_with_value(102),
Some(Pt::new_with_value(103)),
remote_spec,
);
// Simulate SDP negotiation (update_param merges remote into local)
let mut claimed = [false; 128];
let mut unlocked = std::collections::HashSet::new();
local_params.update_param(
&[remote_params],
&mut claimed,
false, // remote is controlling (we're answering)
&mut unlocked,
);
// Verify sprop-max-don-diff was adopted from remote
assert_eq!(
local_params.spec.format.sprop_max_don_diff,
Some(32),
"sprop-max-don-diff should be adopted from remote after negotiation"
);
// Verify packetizer is initialized with DONL enabled
let donl_enabled = local_params.spec.format.sprop_max_don_diff.unwrap_or(0) > 0;
assert!(
donl_enabled,
"DONL should be enabled when sprop-max-don-diff > 0"
);
let mut pack = CodecPacketizer::new(
local_params.spec.codec,
crate::format::Vp9PacketizerMode::default(),
);
if let CodecPacketizer::H266(ref mut h266) = pack {
h266.with_donl(donl_enabled);
}
// Packetize a NAL — should include DONL field (2 extra bytes)
let nalu = vec![0x00, 0x09, 0xAA, 0xBB];
let packets = pack.packetize(1200, &nalu).unwrap();
assert_eq!(packets.len(), 1);
assert_eq!(
packets[0].len(),
nalu.len() + 2, // +2 for DONL
"Packetized output should include 2-byte DONL field"
);
// Verify depacketizer is initialized with DONL enabled
let mut depack: CodecDepacketizer = local_params.spec.codec.into();
if let CodecDepacketizer::H266(ref mut h266) = depack {
h266.with_donl(donl_enabled);
}
// Depacketize the packet — should correctly strip DONL
use crate::packet::{CodecExtra, Depacketizer};
let mut out = Vec::new();
let mut extra = CodecExtra::None;
if let CodecDepacketizer::H266(ref mut h266) = depack {
h266.depacketize(&packets[0], &mut out, &mut extra).unwrap();
}
// Output should be Annex-B: start code + original NAL
assert_eq!(&out[0..4], &[0x00, 0x00, 0x00, 0x01]);
assert_eq!(
&out[4..],
&nalu[..],
"Depacketized NAL should match original"
);
}
/// Test that missing sprop-max-don-diff from remote preserves local value.
#[test]
fn test_sprop_max_don_diff_absent_preserves_local() {
// Local has sprop-max-don-diff=16
let mut local_spec = h266_codec_spec(1, 0, 51);
local_spec.format.sprop_max_don_diff = Some(16);
let mut local_params = PayloadParams::new(
Pt::new_with_value(102),
Some(Pt::new_with_value(103)),
local_spec,
);
// Remote offers without sprop-max-don-diff
let remote_spec = h266_codec_spec(1, 0, 51);
assert!(remote_spec.format.sprop_max_don_diff.is_none());
let remote_params = PayloadParams::new(
Pt::new_with_value(102),
Some(Pt::new_with_value(103)),
remote_spec,
);
let mut claimed = [false; 128];
let mut unlocked = std::collections::HashSet::new();
local_params.update_param(&[remote_params], &mut claimed, false, &mut unlocked);
// Local value should be preserved (remote didn't override)
assert_eq!(
local_params.spec.format.sprop_max_don_diff,
Some(16),
"Local sprop-max-don-diff should be preserved when remote doesn't specify it"
);
}
/// Test that sprop-max-don-diff=0 from remote does NOT enable DONL.
#[test]
fn test_sprop_max_don_diff_zero_disables_donl() {
let local_spec = h266_codec_spec(1, 0, 51);
let mut local_params = PayloadParams::new(
Pt::new_with_value(102),
Some(Pt::new_with_value(103)),
local_spec,
);
// Remote offers with sprop-max-don-diff=0 (no reordering)
let mut remote_spec = h266_codec_spec(1, 0, 51);
remote_spec.format.sprop_max_don_diff = Some(0);
let remote_params = PayloadParams::new(
Pt::new_with_value(102),
Some(Pt::new_with_value(103)),
remote_spec,
);
let mut claimed = [false; 128];
let mut unlocked = std::collections::HashSet::new();
local_params.update_param(&[remote_params], &mut claimed, false, &mut unlocked);
assert_eq!(local_params.spec.format.sprop_max_don_diff, Some(0));
let donl_enabled = local_params.spec.format.sprop_max_don_diff.unwrap_or(0) > 0;
assert!(
!donl_enabled,
"DONL should NOT be enabled when sprop-max-don-diff=0"
);
// Packetize without DONL — packet should be same size as NAL
let nalu = vec![0x00, 0x09, 0xAA, 0xBB];
let mut pack = crate::packet::CodecPacketizer::new(
local_params.spec.codec,
crate::format::Vp9PacketizerMode::default(),
);
// Don't enable DONL since donl_enabled is false
let packets = pack.packetize(1200, &nalu).unwrap();
assert_eq!(
packets[0].len(),
nalu.len(),
"No DONL field when sprop-max-don-diff=0"
);
}
}
mod amr_wb {
use super::*;
fn amr_wb(channels: Option<u8>, format: FormatParams) -> PayloadParams {
let spec = CodecSpec {
codec: Codec::AmrWb,
clock_rate: Frequency::SIXTEEN_KHZ,
channels,
format,
};
PayloadParams::new(122.into(), None, spec)
}
fn octet_aligned() -> FormatParams {
FormatParams {
octet_align: Some(true),
..Default::default()
}
}
#[test]
fn matching() {
let local = amr_wb(Some(1), octet_aligned());
let score = |remote: PayloadParams| local.match_score(&remote);
assert!(score(local).is_some());
assert!(score(amr_wb(None, octet_aligned())).is_some());
assert!(score(amr_wb(Some(1), FormatParams::default())).is_none());
let bandwidth_efficient = FormatParams {
octet_align: Some(false),
..Default::default()
};
let default = amr_wb(None, FormatParams::default());
assert!(
default
.match_score(&amr_wb(None, bandwidth_efficient))
.is_some()
);
let modes = |mode_set| FormatParams {
mode_set: Some(mode_set),
..octet_aligned()
};
let restricted = amr_wb(Some(1), modes(0b111));
assert!(score(restricted).is_some_and(|s| Some(s) < score(local)));
assert!(restricted.match_score(&amr_wb(None, modes(0b11))).is_none());
let stereo = amr_wb(Some(2), octet_aligned());
assert!(stereo.match_score(&stereo).is_none());
}
#[test]
fn update_param_mirrors_layout_channels_and_mode_set() {
let mut local = amr_wb(Some(1), FormatParams::default());
let remote_format = FormatParams {
octet_align: Some(false),
mode_set: Some(0b111),
..Default::default()
};
let remote = amr_wb(None, remote_format);
local.update_param(&[remote], &mut [false; 128], false, &mut HashSet::new());
assert_eq!(local.spec.channels, None);
assert_eq!(local.spec.format, remote_format);
}
}
}