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
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
|
/*
* Copyright (c) 2008 The WebRTC project authors. All Rights Reserved.
*
* Use of this source code is governed by a BSD-style license
* that can be found in the LICENSE file in the root of the source
* tree. An additional intellectual property rights grant can be found
* in the file PATENTS. All contributing project authors may
* be found in the AUTHORS file in the root of the source tree.
*/
#include "media/engine/webrtc_voice_engine.h"
#include <memory>
#include <utility>
#include "absl/memory/memory.h"
#include "absl/strings/match.h"
#include "absl/types/optional.h"
#include "api/audio_codecs/builtin_audio_decoder_factory.h"
#include "api/audio_codecs/builtin_audio_encoder_factory.h"
#include "api/environment/environment.h"
#include "api/environment/environment_factory.h"
#include "api/media_types.h"
#include "api/rtp_parameters.h"
#include "api/scoped_refptr.h"
#include "api/task_queue/default_task_queue_factory.h"
#include "api/transport/field_trial_based_config.h"
#include "call/call.h"
#include "media/base/codec.h"
#include "media/base/fake_media_engine.h"
#include "media/base/fake_network_interface.h"
#include "media/base/fake_rtp.h"
#include "media/base/media_channel.h"
#include "media/base/media_constants.h"
#include "media/engine/fake_webrtc_call.h"
#include "modules/audio_device/include/mock_audio_device.h"
#include "modules/audio_mixer/audio_mixer_impl.h"
#include "modules/audio_processing/include/mock_audio_processing.h"
#include "modules/rtp_rtcp/source/rtp_header_extensions.h"
#include "modules/rtp_rtcp/source/rtp_packet_received.h"
#include "rtc_base/arraysize.h"
#include "rtc_base/byte_order.h"
#include "rtc_base/numerics/safe_conversions.h"
#include "test/gtest.h"
#include "test/mock_audio_decoder_factory.h"
#include "test/mock_audio_encoder_factory.h"
#include "test/scoped_key_value_config.h"
namespace {
using ::testing::_;
using ::testing::ContainerEq;
using ::testing::Contains;
using ::testing::Field;
using ::testing::IsEmpty;
using ::testing::Return;
using ::testing::ReturnPointee;
using ::testing::SaveArg;
using ::testing::StrictMock;
using ::testing::UnorderedElementsAreArray;
using ::webrtc::BitrateConstraints;
using ::webrtc::Call;
using ::webrtc::CallConfig;
using ::webrtc::CreateEnvironment;
using ::webrtc::Environment;
constexpr uint32_t kMaxUnsignaledRecvStreams = 4;
const cricket::AudioCodec kPcmuCodec =
cricket::CreateAudioCodec(0, "PCMU", 8000, 1);
const cricket::AudioCodec kOpusCodec =
cricket::CreateAudioCodec(111, "opus", 48000, 2);
const cricket::AudioCodec kG722CodecVoE =
cricket::CreateAudioCodec(9, "G722", 16000, 1);
const cricket::AudioCodec kG722CodecSdp =
cricket::CreateAudioCodec(9, "G722", 8000, 1);
const cricket::AudioCodec kCn8000Codec =
cricket::CreateAudioCodec(13, "CN", 8000, 1);
const cricket::AudioCodec kCn16000Codec =
cricket::CreateAudioCodec(105, "CN", 16000, 1);
const cricket::AudioCodec kRed48000Codec =
cricket::CreateAudioCodec(112, "RED", 48000, 2);
const cricket::AudioCodec kTelephoneEventCodec1 =
cricket::CreateAudioCodec(106, "telephone-event", 8000, 1);
const cricket::AudioCodec kTelephoneEventCodec2 =
cricket::CreateAudioCodec(107, "telephone-event", 32000, 1);
const uint32_t kSsrc0 = 0;
const uint32_t kSsrc1 = 1;
const uint32_t kSsrcX = 0x99;
const uint32_t kSsrcY = 0x17;
const uint32_t kSsrcZ = 0x42;
const uint32_t kSsrcW = 0x02;
const uint32_t kSsrcs4[] = {11, 200, 30, 44};
constexpr int kRtpHistoryMs = 5000;
constexpr webrtc::AudioProcessing::Config::GainController1::Mode
kDefaultAgcMode =
#if defined(WEBRTC_IOS) || defined(WEBRTC_ANDROID)
webrtc::AudioProcessing::Config::GainController1::kFixedDigital;
#else
webrtc::AudioProcessing::Config::GainController1::kAdaptiveAnalog;
#endif
constexpr webrtc::AudioProcessing::Config::NoiseSuppression::Level
kDefaultNsLevel =
webrtc::AudioProcessing::Config::NoiseSuppression::Level::kHigh;
void AdmSetupExpectations(webrtc::test::MockAudioDeviceModule* adm) {
RTC_DCHECK(adm);
// Setup.
EXPECT_CALL(*adm, Init()).WillOnce(Return(0));
EXPECT_CALL(*adm, RegisterAudioCallback(_)).WillOnce(Return(0));
#if defined(WEBRTC_WIN)
EXPECT_CALL(
*adm,
SetPlayoutDevice(
::testing::Matcher<webrtc::AudioDeviceModule::WindowsDeviceType>(
webrtc::AudioDeviceModule::kDefaultCommunicationDevice)))
.WillOnce(Return(0));
#else
EXPECT_CALL(*adm, SetPlayoutDevice(0)).WillOnce(Return(0));
#endif // #if defined(WEBRTC_WIN)
EXPECT_CALL(*adm, InitSpeaker()).WillOnce(Return(0));
EXPECT_CALL(*adm, StereoPlayoutIsAvailable(::testing::_)).WillOnce(Return(0));
EXPECT_CALL(*adm, SetStereoPlayout(false)).WillOnce(Return(0));
#if defined(WEBRTC_WIN)
EXPECT_CALL(
*adm,
SetRecordingDevice(
::testing::Matcher<webrtc::AudioDeviceModule::WindowsDeviceType>(
webrtc::AudioDeviceModule::kDefaultCommunicationDevice)))
.WillOnce(Return(0));
#else
EXPECT_CALL(*adm, SetRecordingDevice(0)).WillOnce(Return(0));
#endif // #if defined(WEBRTC_WIN)
EXPECT_CALL(*adm, InitMicrophone()).WillOnce(Return(0));
EXPECT_CALL(*adm, StereoRecordingIsAvailable(::testing::_))
.WillOnce(Return(0));
EXPECT_CALL(*adm, SetStereoRecording(false)).WillOnce(Return(0));
EXPECT_CALL(*adm, BuiltInAECIsAvailable()).WillOnce(Return(false));
EXPECT_CALL(*adm, BuiltInAGCIsAvailable()).WillOnce(Return(false));
EXPECT_CALL(*adm, BuiltInNSIsAvailable()).WillOnce(Return(false));
// Teardown.
EXPECT_CALL(*adm, StopPlayout()).WillOnce(Return(0));
EXPECT_CALL(*adm, StopRecording()).WillOnce(Return(0));
EXPECT_CALL(*adm, RegisterAudioCallback(nullptr)).WillOnce(Return(0));
EXPECT_CALL(*adm, Terminate()).WillOnce(Return(0));
}
} // namespace
// Tests that our stub library "works".
TEST(WebRtcVoiceEngineTestStubLibrary, StartupShutdown) {
for (bool use_null_apm : {false, true}) {
std::unique_ptr<webrtc::TaskQueueFactory> task_queue_factory =
webrtc::CreateDefaultTaskQueueFactory();
rtc::scoped_refptr<webrtc::test::MockAudioDeviceModule> adm =
webrtc::test::MockAudioDeviceModule::CreateStrict();
AdmSetupExpectations(adm.get());
rtc::scoped_refptr<StrictMock<webrtc::test::MockAudioProcessing>> apm =
use_null_apm ? nullptr
: rtc::make_ref_counted<
StrictMock<webrtc::test::MockAudioProcessing>>();
webrtc::AudioProcessing::Config apm_config;
if (!use_null_apm) {
EXPECT_CALL(*apm, GetConfig()).WillRepeatedly(ReturnPointee(&apm_config));
EXPECT_CALL(*apm, ApplyConfig(_)).WillRepeatedly(SaveArg<0>(&apm_config));
EXPECT_CALL(*apm, DetachAecDump());
}
{
webrtc::FieldTrialBasedConfig trials;
cricket::WebRtcVoiceEngine engine(
task_queue_factory.get(), adm.get(),
webrtc::MockAudioEncoderFactory::CreateUnusedFactory(),
webrtc::MockAudioDecoderFactory::CreateUnusedFactory(), nullptr, apm,
nullptr, trials);
engine.Init();
}
}
}
class FakeAudioSink : public webrtc::AudioSinkInterface {
public:
void OnData(const Data& audio) override {}
};
class FakeAudioSource : public cricket::AudioSource {
void SetSink(Sink* sink) override {}
};
class WebRtcVoiceEngineTestFake : public ::testing::TestWithParam<bool> {
public:
WebRtcVoiceEngineTestFake()
: use_null_apm_(GetParam()),
task_queue_factory_(webrtc::CreateDefaultTaskQueueFactory()),
adm_(webrtc::test::MockAudioDeviceModule::CreateStrict()),
apm_(use_null_apm_
? nullptr
: rtc::make_ref_counted<
StrictMock<webrtc::test::MockAudioProcessing>>()),
call_(&field_trials_) {
// AudioDeviceModule.
AdmSetupExpectations(adm_.get());
if (!use_null_apm_) {
// AudioProcessing.
EXPECT_CALL(*apm_, GetConfig())
.WillRepeatedly(ReturnPointee(&apm_config_));
EXPECT_CALL(*apm_, ApplyConfig(_))
.WillRepeatedly(SaveArg<0>(&apm_config_));
EXPECT_CALL(*apm_, DetachAecDump());
}
// Default Options.
// TODO(kwiberg): We should use mock factories here, but a bunch of
// the tests here probe the specific set of codecs provided by the builtin
// factories. Those tests should probably be moved elsewhere.
auto encoder_factory = webrtc::CreateBuiltinAudioEncoderFactory();
auto decoder_factory = webrtc::CreateBuiltinAudioDecoderFactory();
engine_.reset(new cricket::WebRtcVoiceEngine(
task_queue_factory_.get(), adm_.get(), encoder_factory, decoder_factory,
nullptr, apm_, nullptr, field_trials_));
engine_->Init();
send_parameters_.codecs.push_back(kPcmuCodec);
recv_parameters_.codecs.push_back(kPcmuCodec);
if (!use_null_apm_) {
// Default Options.
VerifyEchoCancellationSettings(/*enabled=*/true);
EXPECT_TRUE(IsHighPassFilterEnabled());
EXPECT_TRUE(apm_config_.noise_suppression.enabled);
EXPECT_EQ(apm_config_.noise_suppression.level, kDefaultNsLevel);
VerifyGainControlEnabledCorrectly();
VerifyGainControlDefaultSettings();
}
}
bool SetupChannel() {
send_channel_ = engine_->CreateSendChannel(
&call_, cricket::MediaConfig(), cricket::AudioOptions(),
webrtc::CryptoOptions(), webrtc::AudioCodecPairId::Create());
receive_channel_ = engine_->CreateReceiveChannel(
&call_, cricket::MediaConfig(), cricket::AudioOptions(),
webrtc::CryptoOptions(), webrtc::AudioCodecPairId::Create());
send_channel_->SetSsrcListChangedCallback(
[receive_channel =
receive_channel_.get()](const std::set<uint32_t>& choices) {
receive_channel->ChooseReceiverReportSsrc(choices);
});
send_channel_->SetSendCodecChangedCallback(
[receive_channel = receive_channel_.get(),
send_channel = send_channel_.get()]() {
receive_channel->SetReceiveNackEnabled(
send_channel->SendCodecHasNack());
receive_channel->SetReceiveNonSenderRttEnabled(
send_channel->SenderNonSenderRttEnabled());
});
return true;
}
bool SetupRecvStream() {
if (!SetupChannel()) {
return false;
}
return AddRecvStream(kSsrcX);
}
bool SetupSendStream() {
return SetupSendStream(cricket::StreamParams::CreateLegacy(kSsrcX));
}
bool SetupSendStream(const cricket::StreamParams& sp) {
if (!SetupChannel()) {
return false;
}
if (!send_channel_->AddSendStream(sp)) {
return false;
}
if (!use_null_apm_) {
EXPECT_CALL(*apm_, set_output_will_be_muted(false));
}
return send_channel_->SetAudioSend(kSsrcX, true, nullptr, &fake_source_);
}
bool AddRecvStream(uint32_t ssrc) {
EXPECT_TRUE(receive_channel_);
return receive_channel_->AddRecvStream(
cricket::StreamParams::CreateLegacy(ssrc));
}
void SetupForMultiSendStream() {
EXPECT_TRUE(SetupSendStream());
// Remove stream added in Setup.
EXPECT_TRUE(call_.GetAudioSendStream(kSsrcX));
EXPECT_TRUE(send_channel_->RemoveSendStream(kSsrcX));
// Verify the channel does not exist.
EXPECT_FALSE(call_.GetAudioSendStream(kSsrcX));
}
void DeliverPacket(const void* data, int len) {
webrtc::RtpPacketReceived packet;
packet.Parse(reinterpret_cast<const uint8_t*>(data), len);
receive_channel_->OnPacketReceived(packet);
rtc::Thread::Current()->ProcessMessages(0);
}
const cricket::FakeAudioSendStream& GetSendStream(uint32_t ssrc) {
const auto* send_stream = call_.GetAudioSendStream(ssrc);
EXPECT_TRUE(send_stream);
return *send_stream;
}
const cricket::FakeAudioReceiveStream& GetRecvStream(uint32_t ssrc) {
const auto* recv_stream = call_.GetAudioReceiveStream(ssrc);
EXPECT_TRUE(recv_stream);
return *recv_stream;
}
const webrtc::AudioSendStream::Config& GetSendStreamConfig(uint32_t ssrc) {
return GetSendStream(ssrc).GetConfig();
}
const webrtc::AudioReceiveStreamInterface::Config& GetRecvStreamConfig(
uint32_t ssrc) {
return GetRecvStream(ssrc).GetConfig();
}
void SetSend(bool enable) {
ASSERT_TRUE(send_channel_);
if (enable) {
EXPECT_CALL(*adm_, RecordingIsInitialized())
.Times(::testing::AtMost(1))
.WillOnce(Return(false));
EXPECT_CALL(*adm_, Recording())
.Times(::testing::AtMost(1))
.WillOnce(Return(false));
EXPECT_CALL(*adm_, InitRecording())
.Times(::testing::AtMost(1))
.WillOnce(Return(0));
}
send_channel_->SetSend(enable);
}
void SetSenderParameters(const cricket::AudioSenderParameter& params) {
ASSERT_TRUE(send_channel_);
EXPECT_TRUE(send_channel_->SetSenderParameters(params));
}
void SetAudioSend(uint32_t ssrc,
bool enable,
cricket::AudioSource* source,
const cricket::AudioOptions* options = nullptr) {
ASSERT_TRUE(send_channel_);
if (!use_null_apm_) {
EXPECT_CALL(*apm_, set_output_will_be_muted(!enable));
}
EXPECT_TRUE(send_channel_->SetAudioSend(ssrc, enable, options, source));
}
void TestInsertDtmf(uint32_t ssrc,
bool caller,
const cricket::AudioCodec& codec) {
EXPECT_TRUE(SetupChannel());
if (caller) {
// If this is a caller, local description will be applied and add the
// send stream.
EXPECT_TRUE(send_channel_->AddSendStream(
cricket::StreamParams::CreateLegacy(kSsrcX)));
}
// Test we can only InsertDtmf when the other side supports telephone-event.
SetSenderParameters(send_parameters_);
SetSend(true);
EXPECT_FALSE(send_channel_->CanInsertDtmf());
EXPECT_FALSE(send_channel_->InsertDtmf(ssrc, 1, 111));
send_parameters_.codecs.push_back(codec);
SetSenderParameters(send_parameters_);
EXPECT_TRUE(send_channel_->CanInsertDtmf());
if (!caller) {
// If this is callee, there's no active send channel yet.
EXPECT_FALSE(send_channel_->InsertDtmf(ssrc, 2, 123));
EXPECT_TRUE(send_channel_->AddSendStream(
cricket::StreamParams::CreateLegacy(kSsrcX)));
}
// Check we fail if the ssrc is invalid.
EXPECT_FALSE(send_channel_->InsertDtmf(-1, 1, 111));
// Test send.
cricket::FakeAudioSendStream::TelephoneEvent telephone_event =
GetSendStream(kSsrcX).GetLatestTelephoneEvent();
EXPECT_EQ(-1, telephone_event.payload_type);
EXPECT_TRUE(send_channel_->InsertDtmf(ssrc, 2, 123));
telephone_event = GetSendStream(kSsrcX).GetLatestTelephoneEvent();
EXPECT_EQ(codec.id, telephone_event.payload_type);
EXPECT_EQ(codec.clockrate, telephone_event.payload_frequency);
EXPECT_EQ(2, telephone_event.event_code);
EXPECT_EQ(123, telephone_event.duration_ms);
}
void TestExtmapAllowMixedCaller(bool extmap_allow_mixed) {
// For a caller, the answer will be applied in set remote description
// where SetSenderParameters() is called.
EXPECT_TRUE(SetupChannel());
EXPECT_TRUE(send_channel_->AddSendStream(
cricket::StreamParams::CreateLegacy(kSsrcX)));
send_parameters_.extmap_allow_mixed = extmap_allow_mixed;
SetSenderParameters(send_parameters_);
const webrtc::AudioSendStream::Config& config = GetSendStreamConfig(kSsrcX);
EXPECT_EQ(extmap_allow_mixed, config.rtp.extmap_allow_mixed);
}
void TestExtmapAllowMixedCallee(bool extmap_allow_mixed) {
// For a callee, the answer will be applied in set local description
// where SetExtmapAllowMixed() and AddSendStream() are called.
EXPECT_TRUE(SetupChannel());
send_channel_->SetExtmapAllowMixed(extmap_allow_mixed);
EXPECT_TRUE(send_channel_->AddSendStream(
cricket::StreamParams::CreateLegacy(kSsrcX)));
const webrtc::AudioSendStream::Config& config = GetSendStreamConfig(kSsrcX);
EXPECT_EQ(extmap_allow_mixed, config.rtp.extmap_allow_mixed);
}
// Test that send bandwidth is set correctly.
// `codec` is the codec under test.
// `max_bitrate` is a parameter to set to SetMaxSendBandwidth().
// `expected_result` is the expected result from SetMaxSendBandwidth().
// `expected_bitrate` is the expected audio bitrate afterward.
void TestMaxSendBandwidth(const cricket::AudioCodec& codec,
int max_bitrate,
bool expected_result,
int expected_bitrate) {
cricket::AudioSenderParameter parameters;
parameters.codecs.push_back(codec);
parameters.max_bandwidth_bps = max_bitrate;
if (expected_result) {
SetSenderParameters(parameters);
} else {
EXPECT_FALSE(send_channel_->SetSenderParameters(parameters));
}
EXPECT_EQ(expected_bitrate, GetCodecBitrate(kSsrcX));
}
// Sets the per-stream maximum bitrate limit for the specified SSRC.
bool SetMaxBitrateForStream(int32_t ssrc, int bitrate) {
webrtc::RtpParameters parameters =
send_channel_->GetRtpSendParameters(ssrc);
EXPECT_EQ(1UL, parameters.encodings.size());
parameters.encodings[0].max_bitrate_bps = bitrate;
return send_channel_->SetRtpSendParameters(ssrc, parameters).ok();
}
void SetGlobalMaxBitrate(const cricket::AudioCodec& codec, int bitrate) {
cricket::AudioSenderParameter send_parameters;
send_parameters.codecs.push_back(codec);
send_parameters.max_bandwidth_bps = bitrate;
SetSenderParameters(send_parameters);
}
void CheckSendCodecBitrate(int32_t ssrc,
const char expected_name[],
int expected_bitrate) {
const auto& spec = GetSendStreamConfig(ssrc).send_codec_spec;
EXPECT_EQ(expected_name, spec->format.name);
EXPECT_EQ(expected_bitrate, spec->target_bitrate_bps);
}
absl::optional<int> GetCodecBitrate(int32_t ssrc) {
return GetSendStreamConfig(ssrc).send_codec_spec->target_bitrate_bps;
}
int GetMaxBitrate(int32_t ssrc) {
return GetSendStreamConfig(ssrc).max_bitrate_bps;
}
const absl::optional<std::string>& GetAudioNetworkAdaptorConfig(
int32_t ssrc) {
return GetSendStreamConfig(ssrc).audio_network_adaptor_config;
}
void SetAndExpectMaxBitrate(const cricket::AudioCodec& codec,
int global_max,
int stream_max,
bool expected_result,
int expected_codec_bitrate) {
// Clear the bitrate limit from the previous test case.
EXPECT_TRUE(SetMaxBitrateForStream(kSsrcX, -1));
// Attempt to set the requested bitrate limits.
SetGlobalMaxBitrate(codec, global_max);
EXPECT_EQ(expected_result, SetMaxBitrateForStream(kSsrcX, stream_max));
// Verify that reading back the parameters gives results
// consistent with the Set() result.
webrtc::RtpParameters resulting_parameters =
send_channel_->GetRtpSendParameters(kSsrcX);
EXPECT_EQ(1UL, resulting_parameters.encodings.size());
EXPECT_EQ(expected_result ? stream_max : -1,
resulting_parameters.encodings[0].max_bitrate_bps);
// Verify that the codec settings have the expected bitrate.
EXPECT_EQ(expected_codec_bitrate, GetCodecBitrate(kSsrcX));
EXPECT_EQ(expected_codec_bitrate, GetMaxBitrate(kSsrcX));
}
void SetSendCodecsShouldWorkForBitrates(const char* min_bitrate_kbps,
int expected_min_bitrate_bps,
const char* start_bitrate_kbps,
int expected_start_bitrate_bps,
const char* max_bitrate_kbps,
int expected_max_bitrate_bps) {
EXPECT_TRUE(SetupSendStream());
auto& codecs = send_parameters_.codecs;
codecs.clear();
codecs.push_back(kOpusCodec);
codecs[0].params[cricket::kCodecParamMinBitrate] = min_bitrate_kbps;
codecs[0].params[cricket::kCodecParamStartBitrate] = start_bitrate_kbps;
codecs[0].params[cricket::kCodecParamMaxBitrate] = max_bitrate_kbps;
EXPECT_CALL(*call_.GetMockTransportControllerSend(),
SetSdpBitrateParameters(
AllOf(Field(&BitrateConstraints::min_bitrate_bps,
expected_min_bitrate_bps),
Field(&BitrateConstraints::start_bitrate_bps,
expected_start_bitrate_bps),
Field(&BitrateConstraints::max_bitrate_bps,
expected_max_bitrate_bps))));
SetSenderParameters(send_parameters_);
}
void TestSetSendRtpHeaderExtensions(const std::string& ext) {
EXPECT_TRUE(SetupSendStream());
// Ensure extensions are off by default.
EXPECT_EQ(0u, GetSendStreamConfig(kSsrcX).rtp.extensions.size());
// Ensure unknown extensions won't cause an error.
send_parameters_.extensions.push_back(
webrtc::RtpExtension("urn:ietf:params:unknownextention", 1));
SetSenderParameters(send_parameters_);
EXPECT_EQ(0u, GetSendStreamConfig(kSsrcX).rtp.extensions.size());
// Ensure extensions stay off with an empty list of headers.
send_parameters_.extensions.clear();
SetSenderParameters(send_parameters_);
EXPECT_EQ(0u, GetSendStreamConfig(kSsrcX).rtp.extensions.size());
// Ensure extension is set properly.
const int id = 1;
send_parameters_.extensions.push_back(webrtc::RtpExtension(ext, id));
SetSenderParameters(send_parameters_);
EXPECT_EQ(1u, GetSendStreamConfig(kSsrcX).rtp.extensions.size());
EXPECT_EQ(ext, GetSendStreamConfig(kSsrcX).rtp.extensions[0].uri);
EXPECT_EQ(id, GetSendStreamConfig(kSsrcX).rtp.extensions[0].id);
// Ensure extension is set properly on new stream.
EXPECT_TRUE(send_channel_->AddSendStream(
cricket::StreamParams::CreateLegacy(kSsrcY)));
EXPECT_NE(call_.GetAudioSendStream(kSsrcX),
call_.GetAudioSendStream(kSsrcY));
EXPECT_EQ(1u, GetSendStreamConfig(kSsrcY).rtp.extensions.size());
EXPECT_EQ(ext, GetSendStreamConfig(kSsrcY).rtp.extensions[0].uri);
EXPECT_EQ(id, GetSendStreamConfig(kSsrcY).rtp.extensions[0].id);
// Ensure all extensions go back off with an empty list.
send_parameters_.codecs.push_back(kPcmuCodec);
send_parameters_.extensions.clear();
SetSenderParameters(send_parameters_);
EXPECT_EQ(0u, GetSendStreamConfig(kSsrcX).rtp.extensions.size());
EXPECT_EQ(0u, GetSendStreamConfig(kSsrcY).rtp.extensions.size());
}
void TestSetRecvRtpHeaderExtensions(const std::string& ext) {
EXPECT_TRUE(SetupRecvStream());
// Ensure extensions are off by default.
EXPECT_THAT(
receive_channel_->GetRtpReceiverParameters(kSsrcX).header_extensions,
IsEmpty());
// Ensure unknown extensions won't cause an error.
recv_parameters_.extensions.push_back(
webrtc::RtpExtension("urn:ietf:params:unknownextention", 1));
EXPECT_TRUE(receive_channel_->SetReceiverParameters(recv_parameters_));
EXPECT_THAT(
receive_channel_->GetRtpReceiverParameters(kSsrcX).header_extensions,
IsEmpty());
// Ensure extensions stay off with an empty list of headers.
recv_parameters_.extensions.clear();
EXPECT_TRUE(receive_channel_->SetReceiverParameters(recv_parameters_));
EXPECT_THAT(
receive_channel_->GetRtpReceiverParameters(kSsrcX).header_extensions,
IsEmpty());
// Ensure extension is set properly.
const int id = 2;
recv_parameters_.extensions.push_back(webrtc::RtpExtension(ext, id));
EXPECT_TRUE(receive_channel_->SetReceiverParameters(recv_parameters_));
EXPECT_EQ(
receive_channel_->GetRtpReceiverParameters(kSsrcX).header_extensions,
recv_parameters_.extensions);
// Ensure extension is set properly on new stream.
EXPECT_TRUE(AddRecvStream(kSsrcY));
EXPECT_EQ(
receive_channel_->GetRtpReceiverParameters(kSsrcY).header_extensions,
recv_parameters_.extensions);
// Ensure all extensions go back off with an empty list.
recv_parameters_.extensions.clear();
EXPECT_TRUE(receive_channel_->SetReceiverParameters(recv_parameters_));
EXPECT_THAT(
receive_channel_->GetRtpReceiverParameters(kSsrcX).header_extensions,
IsEmpty());
EXPECT_THAT(
receive_channel_->GetRtpReceiverParameters(kSsrcY).header_extensions,
IsEmpty());
}
webrtc::AudioSendStream::Stats GetAudioSendStreamStats() const {
webrtc::AudioSendStream::Stats stats;
stats.local_ssrc = 12;
stats.payload_bytes_sent = 345;
stats.header_and_padding_bytes_sent = 56;
stats.packets_sent = 678;
stats.packets_lost = 9012;
stats.fraction_lost = 34.56f;
stats.codec_name = "codec_name_send";
stats.codec_payload_type = 0;
stats.jitter_ms = 12;
stats.rtt_ms = 345;
stats.audio_level = 678;
stats.apm_statistics.delay_median_ms = 234;
stats.apm_statistics.delay_standard_deviation_ms = 567;
stats.apm_statistics.echo_return_loss = 890;
stats.apm_statistics.echo_return_loss_enhancement = 1234;
stats.apm_statistics.residual_echo_likelihood = 0.432f;
stats.apm_statistics.residual_echo_likelihood_recent_max = 0.6f;
stats.ana_statistics.bitrate_action_counter = 321;
stats.ana_statistics.channel_action_counter = 432;
stats.ana_statistics.dtx_action_counter = 543;
stats.ana_statistics.fec_action_counter = 654;
stats.ana_statistics.frame_length_increase_counter = 765;
stats.ana_statistics.frame_length_decrease_counter = 876;
stats.ana_statistics.uplink_packet_loss_fraction = 987.0;
return stats;
}
void SetAudioSendStreamStats() {
for (auto* s : call_.GetAudioSendStreams()) {
s->SetStats(GetAudioSendStreamStats());
}
}
void VerifyVoiceSenderInfo(const cricket::VoiceSenderInfo& info,
bool is_sending) {
const auto stats = GetAudioSendStreamStats();
EXPECT_EQ(info.ssrc(), stats.local_ssrc);
EXPECT_EQ(info.payload_bytes_sent, stats.payload_bytes_sent);
EXPECT_EQ(info.header_and_padding_bytes_sent,
stats.header_and_padding_bytes_sent);
EXPECT_EQ(info.packets_sent, stats.packets_sent);
EXPECT_EQ(info.packets_lost, stats.packets_lost);
EXPECT_EQ(info.fraction_lost, stats.fraction_lost);
EXPECT_EQ(info.codec_name, stats.codec_name);
EXPECT_EQ(info.codec_payload_type, stats.codec_payload_type);
EXPECT_EQ(info.jitter_ms, stats.jitter_ms);
EXPECT_EQ(info.rtt_ms, stats.rtt_ms);
EXPECT_EQ(info.audio_level, stats.audio_level);
EXPECT_EQ(info.apm_statistics.delay_median_ms,
stats.apm_statistics.delay_median_ms);
EXPECT_EQ(info.apm_statistics.delay_standard_deviation_ms,
stats.apm_statistics.delay_standard_deviation_ms);
EXPECT_EQ(info.apm_statistics.echo_return_loss,
stats.apm_statistics.echo_return_loss);
EXPECT_EQ(info.apm_statistics.echo_return_loss_enhancement,
stats.apm_statistics.echo_return_loss_enhancement);
EXPECT_EQ(info.apm_statistics.residual_echo_likelihood,
stats.apm_statistics.residual_echo_likelihood);
EXPECT_EQ(info.apm_statistics.residual_echo_likelihood_recent_max,
stats.apm_statistics.residual_echo_likelihood_recent_max);
EXPECT_EQ(info.ana_statistics.bitrate_action_counter,
stats.ana_statistics.bitrate_action_counter);
EXPECT_EQ(info.ana_statistics.channel_action_counter,
stats.ana_statistics.channel_action_counter);
EXPECT_EQ(info.ana_statistics.dtx_action_counter,
stats.ana_statistics.dtx_action_counter);
EXPECT_EQ(info.ana_statistics.fec_action_counter,
stats.ana_statistics.fec_action_counter);
EXPECT_EQ(info.ana_statistics.frame_length_increase_counter,
stats.ana_statistics.frame_length_increase_counter);
EXPECT_EQ(info.ana_statistics.frame_length_decrease_counter,
stats.ana_statistics.frame_length_decrease_counter);
EXPECT_EQ(info.ana_statistics.uplink_packet_loss_fraction,
stats.ana_statistics.uplink_packet_loss_fraction);
}
webrtc::AudioReceiveStreamInterface::Stats GetAudioReceiveStreamStats()
const {
webrtc::AudioReceiveStreamInterface::Stats stats;
stats.remote_ssrc = 123;
stats.payload_bytes_received = 456;
stats.header_and_padding_bytes_received = 67;
stats.packets_received = 768;
stats.packets_lost = 101;
stats.codec_name = "codec_name_recv";
stats.codec_payload_type = 0;
stats.jitter_ms = 901;
stats.jitter_buffer_ms = 234;
stats.jitter_buffer_preferred_ms = 567;
stats.delay_estimate_ms = 890;
stats.audio_level = 1234;
stats.total_samples_received = 5678901;
stats.concealed_samples = 234;
stats.concealment_events = 12;
stats.jitter_buffer_delay_seconds = 34;
stats.jitter_buffer_emitted_count = 77;
stats.expand_rate = 5.67f;
stats.speech_expand_rate = 8.90f;
stats.secondary_decoded_rate = 1.23f;
stats.secondary_discarded_rate = 0.12f;
stats.accelerate_rate = 4.56f;
stats.preemptive_expand_rate = 7.89f;
stats.decoding_calls_to_silence_generator = 12;
stats.decoding_calls_to_neteq = 345;
stats.decoding_normal = 67890;
stats.decoding_plc = 1234;
stats.decoding_codec_plc = 1236;
stats.decoding_cng = 5678;
stats.decoding_plc_cng = 9012;
stats.decoding_muted_output = 3456;
stats.capture_start_ntp_time_ms = 7890;
return stats;
}
void SetAudioReceiveStreamStats() {
for (auto* s : call_.GetAudioReceiveStreams()) {
s->SetStats(GetAudioReceiveStreamStats());
}
}
void VerifyVoiceReceiverInfo(const cricket::VoiceReceiverInfo& info) {
const auto stats = GetAudioReceiveStreamStats();
EXPECT_EQ(info.ssrc(), stats.remote_ssrc);
EXPECT_EQ(info.payload_bytes_received, stats.payload_bytes_received);
EXPECT_EQ(info.header_and_padding_bytes_received,
stats.header_and_padding_bytes_received);
EXPECT_EQ(rtc::checked_cast<unsigned int>(info.packets_received),
stats.packets_received);
EXPECT_EQ(info.packets_lost, stats.packets_lost);
EXPECT_EQ(info.codec_name, stats.codec_name);
EXPECT_EQ(info.codec_payload_type, stats.codec_payload_type);
EXPECT_EQ(rtc::checked_cast<unsigned int>(info.jitter_ms), stats.jitter_ms);
EXPECT_EQ(rtc::checked_cast<unsigned int>(info.jitter_buffer_ms),
stats.jitter_buffer_ms);
EXPECT_EQ(rtc::checked_cast<unsigned int>(info.jitter_buffer_preferred_ms),
stats.jitter_buffer_preferred_ms);
EXPECT_EQ(rtc::checked_cast<unsigned int>(info.delay_estimate_ms),
stats.delay_estimate_ms);
EXPECT_EQ(info.audio_level, stats.audio_level);
EXPECT_EQ(info.total_samples_received, stats.total_samples_received);
EXPECT_EQ(info.concealed_samples, stats.concealed_samples);
EXPECT_EQ(info.concealment_events, stats.concealment_events);
EXPECT_EQ(info.jitter_buffer_delay_seconds,
stats.jitter_buffer_delay_seconds);
EXPECT_EQ(info.jitter_buffer_emitted_count,
stats.jitter_buffer_emitted_count);
EXPECT_EQ(info.expand_rate, stats.expand_rate);
EXPECT_EQ(info.speech_expand_rate, stats.speech_expand_rate);
EXPECT_EQ(info.secondary_decoded_rate, stats.secondary_decoded_rate);
EXPECT_EQ(info.secondary_discarded_rate, stats.secondary_discarded_rate);
EXPECT_EQ(info.accelerate_rate, stats.accelerate_rate);
EXPECT_EQ(info.preemptive_expand_rate, stats.preemptive_expand_rate);
EXPECT_EQ(info.decoding_calls_to_silence_generator,
stats.decoding_calls_to_silence_generator);
EXPECT_EQ(info.decoding_calls_to_neteq, stats.decoding_calls_to_neteq);
EXPECT_EQ(info.decoding_normal, stats.decoding_normal);
EXPECT_EQ(info.decoding_plc, stats.decoding_plc);
EXPECT_EQ(info.decoding_codec_plc, stats.decoding_codec_plc);
EXPECT_EQ(info.decoding_cng, stats.decoding_cng);
EXPECT_EQ(info.decoding_plc_cng, stats.decoding_plc_cng);
EXPECT_EQ(info.decoding_muted_output, stats.decoding_muted_output);
EXPECT_EQ(info.capture_start_ntp_time_ms, stats.capture_start_ntp_time_ms);
}
void VerifyVoiceSendRecvCodecs(
const cricket::VoiceMediaSendInfo& send_info,
const cricket::VoiceMediaReceiveInfo& receive_info) const {
EXPECT_EQ(send_parameters_.codecs.size(), send_info.send_codecs.size());
for (const cricket::AudioCodec& codec : send_parameters_.codecs) {
ASSERT_EQ(send_info.send_codecs.count(codec.id), 1U);
EXPECT_EQ(send_info.send_codecs.find(codec.id)->second,
codec.ToCodecParameters());
}
EXPECT_EQ(recv_parameters_.codecs.size(),
receive_info.receive_codecs.size());
for (const cricket::AudioCodec& codec : recv_parameters_.codecs) {
ASSERT_EQ(receive_info.receive_codecs.count(codec.id), 1U);
EXPECT_EQ(receive_info.receive_codecs.find(codec.id)->second,
codec.ToCodecParameters());
}
}
void VerifyGainControlEnabledCorrectly() {
EXPECT_TRUE(apm_config_.gain_controller1.enabled);
EXPECT_EQ(kDefaultAgcMode, apm_config_.gain_controller1.mode);
}
void VerifyGainControlDefaultSettings() {
EXPECT_EQ(3, apm_config_.gain_controller1.target_level_dbfs);
EXPECT_EQ(9, apm_config_.gain_controller1.compression_gain_db);
EXPECT_TRUE(apm_config_.gain_controller1.enable_limiter);
}
void VerifyEchoCancellationSettings(bool enabled) {
constexpr bool kDefaultUseAecm =
#if defined(WEBRTC_ANDROID)
true;
#else
false;
#endif
EXPECT_EQ(apm_config_.echo_canceller.enabled, enabled);
EXPECT_EQ(apm_config_.echo_canceller.mobile_mode, kDefaultUseAecm);
}
bool IsHighPassFilterEnabled() {
return apm_config_.high_pass_filter.enabled;
}
cricket::WebRtcVoiceSendChannel* SendImplFromPointer(
cricket::VoiceMediaSendChannelInterface* channel) {
return static_cast<cricket::WebRtcVoiceSendChannel*>(channel);
}
cricket::WebRtcVoiceSendChannel* SendImpl() {
return SendImplFromPointer(send_channel_.get());
}
cricket::WebRtcVoiceReceiveChannel* ReceiveImpl() {
return static_cast<cricket::WebRtcVoiceReceiveChannel*>(
receive_channel_.get());
}
protected:
rtc::AutoThread main_thread_;
const bool use_null_apm_;
webrtc::test::ScopedKeyValueConfig field_trials_;
std::unique_ptr<webrtc::TaskQueueFactory> task_queue_factory_;
rtc::scoped_refptr<webrtc::test::MockAudioDeviceModule> adm_;
rtc::scoped_refptr<StrictMock<webrtc::test::MockAudioProcessing>> apm_;
cricket::FakeCall call_;
std::unique_ptr<cricket::WebRtcVoiceEngine> engine_;
std::unique_ptr<cricket::VoiceMediaSendChannelInterface> send_channel_;
std::unique_ptr<cricket::VoiceMediaReceiveChannelInterface> receive_channel_;
cricket::AudioSenderParameter send_parameters_;
cricket::AudioReceiverParameters recv_parameters_;
FakeAudioSource fake_source_;
webrtc::AudioProcessing::Config apm_config_;
};
INSTANTIATE_TEST_SUITE_P(TestBothWithAndWithoutNullApm,
WebRtcVoiceEngineTestFake,
::testing::Values(false, true));
// Tests that we can create and destroy a channel.
TEST_P(WebRtcVoiceEngineTestFake, CreateMediaChannel) {
EXPECT_TRUE(SetupChannel());
}
// Test that we can add a send stream and that it has the correct defaults.
TEST_P(WebRtcVoiceEngineTestFake, CreateSendStream) {
EXPECT_TRUE(SetupChannel());
EXPECT_TRUE(send_channel_->AddSendStream(
cricket::StreamParams::CreateLegacy(kSsrcX)));
const webrtc::AudioSendStream::Config& config = GetSendStreamConfig(kSsrcX);
EXPECT_EQ(kSsrcX, config.rtp.ssrc);
EXPECT_EQ("", config.rtp.c_name);
EXPECT_EQ(0u, config.rtp.extensions.size());
EXPECT_EQ(SendImpl()->transport(), config.send_transport);
}
// Test that we can add a receive stream and that it has the correct defaults.
TEST_P(WebRtcVoiceEngineTestFake, CreateRecvStream) {
EXPECT_TRUE(SetupChannel());
EXPECT_TRUE(AddRecvStream(kSsrcX));
const webrtc::AudioReceiveStreamInterface::Config& config =
GetRecvStreamConfig(kSsrcX);
EXPECT_EQ(kSsrcX, config.rtp.remote_ssrc);
EXPECT_EQ(0xFA17FA17, config.rtp.local_ssrc);
EXPECT_EQ(ReceiveImpl()->transport(), config.rtcp_send_transport);
EXPECT_EQ("", config.sync_group);
}
TEST_P(WebRtcVoiceEngineTestFake, OpusSupportsTransportCc) {
const std::vector<cricket::AudioCodec>& codecs = engine_->send_codecs();
bool opus_found = false;
for (const cricket::AudioCodec& codec : codecs) {
if (codec.name == "opus") {
EXPECT_TRUE(HasTransportCc(codec));
opus_found = true;
}
}
EXPECT_TRUE(opus_found);
}
// Test that we set our inbound codecs properly, including changing PT.
TEST_P(WebRtcVoiceEngineTestFake, SetRecvCodecs) {
EXPECT_TRUE(SetupChannel());
cricket::AudioReceiverParameters parameters;
parameters.codecs.push_back(kOpusCodec);
parameters.codecs.push_back(kPcmuCodec);
parameters.codecs.push_back(kTelephoneEventCodec1);
parameters.codecs.push_back(kTelephoneEventCodec2);
parameters.codecs[0].id = 106; // collide with existing CN 32k
parameters.codecs[2].id = 126;
EXPECT_TRUE(receive_channel_->SetReceiverParameters(parameters));
EXPECT_TRUE(AddRecvStream(kSsrcX));
EXPECT_THAT(GetRecvStreamConfig(kSsrcX).decoder_map,
(ContainerEq<std::map<int, webrtc::SdpAudioFormat>>(
{{0, {"PCMU", 8000, 1}},
{106, {"OPUS", 48000, 2}},
{126, {"telephone-event", 8000, 1}},
{107, {"telephone-event", 32000, 1}}})));
}
// Test that we fail to set an unknown inbound codec.
TEST_P(WebRtcVoiceEngineTestFake, SetRecvCodecsUnsupportedCodec) {
EXPECT_TRUE(SetupChannel());
cricket::AudioReceiverParameters parameters;
parameters.codecs.push_back(kOpusCodec);
parameters.codecs.push_back(cricket::CreateAudioCodec(127, "XYZ", 32000, 1));
EXPECT_FALSE(receive_channel_->SetReceiverParameters(parameters));
}
// Test that we fail if we have duplicate types in the inbound list.
TEST_P(WebRtcVoiceEngineTestFake, SetRecvCodecsDuplicatePayloadType) {
EXPECT_TRUE(SetupChannel());
cricket::AudioReceiverParameters parameters;
parameters.codecs.push_back(kOpusCodec);
parameters.codecs.push_back(kCn16000Codec);
parameters.codecs[1].id = kOpusCodec.id;
EXPECT_FALSE(receive_channel_->SetReceiverParameters(parameters));
}
// Test that we can decode OPUS without stereo parameters.
TEST_P(WebRtcVoiceEngineTestFake, SetRecvCodecsWithOpusNoStereo) {
EXPECT_TRUE(SetupChannel());
cricket::AudioReceiverParameters parameters;
parameters.codecs.push_back(kPcmuCodec);
parameters.codecs.push_back(kOpusCodec);
EXPECT_TRUE(receive_channel_->SetReceiverParameters(parameters));
EXPECT_TRUE(AddRecvStream(kSsrcX));
EXPECT_THAT(GetRecvStreamConfig(kSsrcX).decoder_map,
(ContainerEq<std::map<int, webrtc::SdpAudioFormat>>(
{{0, {"PCMU", 8000, 1}}, {111, {"opus", 48000, 2}}})));
}
// Test that we can decode OPUS with stereo = 0.
TEST_P(WebRtcVoiceEngineTestFake, SetRecvCodecsWithOpus0Stereo) {
EXPECT_TRUE(SetupChannel());
cricket::AudioReceiverParameters parameters;
parameters.codecs.push_back(kPcmuCodec);
parameters.codecs.push_back(kOpusCodec);
parameters.codecs[1].params["stereo"] = "0";
EXPECT_TRUE(receive_channel_->SetReceiverParameters(parameters));
EXPECT_TRUE(AddRecvStream(kSsrcX));
EXPECT_THAT(GetRecvStreamConfig(kSsrcX).decoder_map,
(ContainerEq<std::map<int, webrtc::SdpAudioFormat>>(
{{0, {"PCMU", 8000, 1}},
{111, {"opus", 48000, 2, {{"stereo", "0"}}}}})));
}
// Test that we can decode OPUS with stereo = 1.
TEST_P(WebRtcVoiceEngineTestFake, SetRecvCodecsWithOpus1Stereo) {
EXPECT_TRUE(SetupChannel());
cricket::AudioReceiverParameters parameters;
parameters.codecs.push_back(kPcmuCodec);
parameters.codecs.push_back(kOpusCodec);
parameters.codecs[1].params["stereo"] = "1";
EXPECT_TRUE(receive_channel_->SetReceiverParameters(parameters));
EXPECT_TRUE(AddRecvStream(kSsrcX));
EXPECT_THAT(GetRecvStreamConfig(kSsrcX).decoder_map,
(ContainerEq<std::map<int, webrtc::SdpAudioFormat>>(
{{0, {"PCMU", 8000, 1}},
{111, {"opus", 48000, 2, {{"stereo", "1"}}}}})));
}
// Test that changes to recv codecs are applied to all streams.
TEST_P(WebRtcVoiceEngineTestFake, SetRecvCodecsWithMultipleStreams) {
EXPECT_TRUE(SetupChannel());
cricket::AudioReceiverParameters parameters;
parameters.codecs.push_back(kOpusCodec);
parameters.codecs.push_back(kPcmuCodec);
parameters.codecs.push_back(kTelephoneEventCodec1);
parameters.codecs.push_back(kTelephoneEventCodec2);
parameters.codecs[0].id = 106; // collide with existing CN 32k
parameters.codecs[2].id = 126;
EXPECT_TRUE(receive_channel_->SetReceiverParameters(parameters));
for (const auto& ssrc : {kSsrcX, kSsrcY}) {
EXPECT_TRUE(AddRecvStream(ssrc));
EXPECT_THAT(GetRecvStreamConfig(ssrc).decoder_map,
(ContainerEq<std::map<int, webrtc::SdpAudioFormat>>(
{{0, {"PCMU", 8000, 1}},
{106, {"OPUS", 48000, 2}},
{126, {"telephone-event", 8000, 1}},
{107, {"telephone-event", 32000, 1}}})));
}
}
TEST_P(WebRtcVoiceEngineTestFake, SetRecvCodecsAfterAddingStreams) {
EXPECT_TRUE(SetupRecvStream());
cricket::AudioReceiverParameters parameters;
parameters.codecs.push_back(kOpusCodec);
parameters.codecs[0].id = 106; // collide with existing CN 32k
EXPECT_TRUE(receive_channel_->SetReceiverParameters(parameters));
const auto& dm = GetRecvStreamConfig(kSsrcX).decoder_map;
ASSERT_EQ(1u, dm.count(106));
EXPECT_EQ(webrtc::SdpAudioFormat("opus", 48000, 2), dm.at(106));
}
// Test that we can apply the same set of codecs again while playing.
TEST_P(WebRtcVoiceEngineTestFake, SetRecvCodecsWhilePlaying) {
EXPECT_TRUE(SetupRecvStream());
cricket::AudioReceiverParameters parameters;
parameters.codecs.push_back(kPcmuCodec);
parameters.codecs.push_back(kCn16000Codec);
EXPECT_TRUE(receive_channel_->SetReceiverParameters(parameters));
receive_channel_->SetPlayout(true);
EXPECT_TRUE(receive_channel_->SetReceiverParameters(parameters));
// Remapping a payload type to a different codec should fail.
parameters.codecs[0] = kOpusCodec;
parameters.codecs[0].id = kPcmuCodec.id;
EXPECT_FALSE(receive_channel_->SetReceiverParameters(parameters));
EXPECT_TRUE(GetRecvStream(kSsrcX).started());
}
// Test that we can add a codec while playing.
TEST_P(WebRtcVoiceEngineTestFake, AddRecvCodecsWhilePlaying) {
EXPECT_TRUE(SetupRecvStream());
cricket::AudioReceiverParameters parameters;
parameters.codecs.push_back(kPcmuCodec);
parameters.codecs.push_back(kCn16000Codec);
EXPECT_TRUE(receive_channel_->SetReceiverParameters(parameters));
receive_channel_->SetPlayout(true);
parameters.codecs.push_back(kOpusCodec);
EXPECT_TRUE(receive_channel_->SetReceiverParameters(parameters));
EXPECT_TRUE(GetRecvStream(kSsrcX).started());
}
// Test that we accept adding the same codec with a different payload type.
// See: https://bugs.chromium.org/p/webrtc/issues/detail?id=5847
TEST_P(WebRtcVoiceEngineTestFake, ChangeRecvCodecPayloadType) {
EXPECT_TRUE(SetupRecvStream());
cricket::AudioReceiverParameters parameters;
parameters.codecs.push_back(kOpusCodec);
EXPECT_TRUE(receive_channel_->SetReceiverParameters(parameters));
++parameters.codecs[0].id;
EXPECT_TRUE(receive_channel_->SetReceiverParameters(parameters));
}
// Test that we do allow setting Opus/Red by default.
TEST_P(WebRtcVoiceEngineTestFake, RecvRedDefault) {
EXPECT_TRUE(SetupRecvStream());
cricket::AudioReceiverParameters parameters;
parameters.codecs.push_back(kOpusCodec);
parameters.codecs.push_back(kRed48000Codec);
parameters.codecs[1].params[""] = "111/111";
EXPECT_TRUE(receive_channel_->SetReceiverParameters(parameters));
EXPECT_THAT(GetRecvStreamConfig(kSsrcX).decoder_map,
(ContainerEq<std::map<int, webrtc::SdpAudioFormat>>(
{{111, {"opus", 48000, 2}},
{112, {"red", 48000, 2, {{"", "111/111"}}}}})));
}
TEST_P(WebRtcVoiceEngineTestFake, SetSendBandwidthAuto) {
EXPECT_TRUE(SetupSendStream());
// Test that when autobw is enabled, bitrate is kept as the default
// value. autobw is enabled for the following tests because the target
// bitrate is <= 0.
// PCMU, default bitrate == 64000.
TestMaxSendBandwidth(kPcmuCodec, -1, true, 64000);
// opus, default bitrate == 32000 in mono.
TestMaxSendBandwidth(kOpusCodec, -1, true, 32000);
}
TEST_P(WebRtcVoiceEngineTestFake, SetMaxSendBandwidthMultiRateAsCaller) {
EXPECT_TRUE(SetupSendStream());
// opus, default bitrate == 64000.
TestMaxSendBandwidth(kOpusCodec, 96000, true, 96000);
TestMaxSendBandwidth(kOpusCodec, 48000, true, 48000);
// Rates above the max (510000) should be capped.
TestMaxSendBandwidth(kOpusCodec, 600000, true, 510000);
}
TEST_P(WebRtcVoiceEngineTestFake, SetMaxSendBandwidthFixedRateAsCaller) {
EXPECT_TRUE(SetupSendStream());
// Test that we can only set a maximum bitrate for a fixed-rate codec
// if it's bigger than the fixed rate.
// PCMU, fixed bitrate == 64000.
TestMaxSendBandwidth(kPcmuCodec, 0, true, 64000);
TestMaxSendBandwidth(kPcmuCodec, 1, false, 64000);
TestMaxSendBandwidth(kPcmuCodec, 128000, true, 64000);
TestMaxSendBandwidth(kPcmuCodec, 32000, false, 64000);
TestMaxSendBandwidth(kPcmuCodec, 64000, true, 64000);
TestMaxSendBandwidth(kPcmuCodec, 63999, false, 64000);
TestMaxSendBandwidth(kPcmuCodec, 64001, true, 64000);
}
TEST_P(WebRtcVoiceEngineTestFake, SetMaxSendBandwidthMultiRateAsCallee) {
EXPECT_TRUE(SetupChannel());
const int kDesiredBitrate = 128000;
cricket::AudioSenderParameter parameters;
parameters.codecs = engine_->send_codecs();
parameters.max_bandwidth_bps = kDesiredBitrate;
SetSenderParameters(parameters);
EXPECT_TRUE(send_channel_->AddSendStream(
cricket::StreamParams::CreateLegacy(kSsrcX)));
EXPECT_EQ(kDesiredBitrate, GetCodecBitrate(kSsrcX));
}
// Test that bitrate cannot be set for CBR codecs.
// Bitrate is ignored if it is higher than the fixed bitrate.
// Bitrate less then the fixed bitrate is an error.
TEST_P(WebRtcVoiceEngineTestFake, SetMaxSendBandwidthCbr) {
EXPECT_TRUE(SetupSendStream());
// PCMU, default bitrate == 64000.
SetSenderParameters(send_parameters_);
EXPECT_EQ(64000, GetCodecBitrate(kSsrcX));
send_parameters_.max_bandwidth_bps = 128000;
SetSenderParameters(send_parameters_);
EXPECT_EQ(64000, GetCodecBitrate(kSsrcX));
send_parameters_.max_bandwidth_bps = 128;
EXPECT_FALSE(send_channel_->SetSenderParameters(send_parameters_));
EXPECT_EQ(64000, GetCodecBitrate(kSsrcX));
}
// Test that the per-stream bitrate limit and the global
// bitrate limit both apply.
TEST_P(WebRtcVoiceEngineTestFake, SetMaxBitratePerStream) {
EXPECT_TRUE(SetupSendStream());
// opus, default bitrate == 32000.
SetAndExpectMaxBitrate(kOpusCodec, 0, 0, true, 32000);
SetAndExpectMaxBitrate(kOpusCodec, 48000, 0, true, 48000);
SetAndExpectMaxBitrate(kOpusCodec, 48000, 64000, true, 48000);
SetAndExpectMaxBitrate(kOpusCodec, 64000, 48000, true, 48000);
// CBR codecs allow both maximums to exceed the bitrate.
SetAndExpectMaxBitrate(kPcmuCodec, 0, 0, true, 64000);
SetAndExpectMaxBitrate(kPcmuCodec, 64001, 0, true, 64000);
SetAndExpectMaxBitrate(kPcmuCodec, 0, 64001, true, 64000);
SetAndExpectMaxBitrate(kPcmuCodec, 64001, 64001, true, 64000);
// CBR codecs don't allow per stream maximums to be too low.
SetAndExpectMaxBitrate(kPcmuCodec, 0, 63999, false, 64000);
SetAndExpectMaxBitrate(kPcmuCodec, 64001, 63999, false, 64000);
}
// Test that an attempt to set RtpParameters for a stream that does not exist
// fails.
TEST_P(WebRtcVoiceEngineTestFake, CannotSetMaxBitrateForNonexistentStream) {
EXPECT_TRUE(SetupChannel());
webrtc::RtpParameters nonexistent_parameters =
send_channel_->GetRtpSendParameters(kSsrcX);
EXPECT_EQ(0u, nonexistent_parameters.encodings.size());
nonexistent_parameters.encodings.push_back(webrtc::RtpEncodingParameters());
EXPECT_FALSE(
send_channel_->SetRtpSendParameters(kSsrcX, nonexistent_parameters).ok());
}
TEST_P(WebRtcVoiceEngineTestFake,
CannotSetRtpSendParametersWithIncorrectNumberOfEncodings) {
// This test verifies that setting RtpParameters succeeds only if
// the structure contains exactly one encoding.
// TODO(skvlad): Update this test when we start supporting setting parameters
// for each encoding individually.
EXPECT_TRUE(SetupSendStream());
webrtc::RtpParameters parameters =
send_channel_->GetRtpSendParameters(kSsrcX);
// Two or more encodings should result in failure.
parameters.encodings.push_back(webrtc::RtpEncodingParameters());
EXPECT_FALSE(send_channel_->SetRtpSendParameters(kSsrcX, parameters).ok());
// Zero encodings should also fail.
parameters.encodings.clear();
EXPECT_FALSE(send_channel_->SetRtpSendParameters(kSsrcX, parameters).ok());
}
// Changing the SSRC through RtpParameters is not allowed.
TEST_P(WebRtcVoiceEngineTestFake, CannotSetSsrcInRtpSendParameters) {
EXPECT_TRUE(SetupSendStream());
webrtc::RtpParameters parameters =
send_channel_->GetRtpSendParameters(kSsrcX);
parameters.encodings[0].ssrc = 0xdeadbeef;
EXPECT_FALSE(send_channel_->SetRtpSendParameters(kSsrcX, parameters).ok());
}
// Test that a stream will not be sending if its encoding is made
// inactive through SetRtpSendParameters.
TEST_P(WebRtcVoiceEngineTestFake, SetRtpParametersEncodingsActive) {
EXPECT_TRUE(SetupSendStream());
SetSend(true);
EXPECT_TRUE(GetSendStream(kSsrcX).IsSending());
// Get current parameters and change "active" to false.
webrtc::RtpParameters parameters =
send_channel_->GetRtpSendParameters(kSsrcX);
ASSERT_EQ(1u, parameters.encodings.size());
ASSERT_TRUE(parameters.encodings[0].active);
parameters.encodings[0].active = false;
EXPECT_TRUE(send_channel_->SetRtpSendParameters(kSsrcX, parameters).ok());
EXPECT_FALSE(GetSendStream(kSsrcX).IsSending());
// Now change it back to active and verify we resume sending.
// This should occur even when other parameters are updated.
parameters.encodings[0].active = true;
parameters.encodings[0].max_bitrate_bps = absl::optional<int>(6000);
EXPECT_TRUE(send_channel_->SetRtpSendParameters(kSsrcX, parameters).ok());
EXPECT_TRUE(GetSendStream(kSsrcX).IsSending());
}
TEST_P(WebRtcVoiceEngineTestFake, SetRtpParametersAdaptivePtime) {
EXPECT_TRUE(SetupSendStream());
// Get current parameters and change "adaptive_ptime" to true.
webrtc::RtpParameters parameters =
send_channel_->GetRtpSendParameters(kSsrcX);
ASSERT_EQ(1u, parameters.encodings.size());
ASSERT_FALSE(parameters.encodings[0].adaptive_ptime);
parameters.encodings[0].adaptive_ptime = true;
EXPECT_TRUE(send_channel_->SetRtpSendParameters(kSsrcX, parameters).ok());
EXPECT_TRUE(GetAudioNetworkAdaptorConfig(kSsrcX));
EXPECT_EQ(16000, GetSendStreamConfig(kSsrcX).min_bitrate_bps);
parameters.encodings[0].adaptive_ptime = false;
EXPECT_TRUE(send_channel_->SetRtpSendParameters(kSsrcX, parameters).ok());
EXPECT_FALSE(GetAudioNetworkAdaptorConfig(kSsrcX));
EXPECT_EQ(32000, GetSendStreamConfig(kSsrcX).min_bitrate_bps);
}
TEST_P(WebRtcVoiceEngineTestFake,
DisablingAdaptivePtimeDoesNotRemoveAudioNetworkAdaptorFromOptions) {
EXPECT_TRUE(SetupSendStream());
send_parameters_.options.audio_network_adaptor = true;
send_parameters_.options.audio_network_adaptor_config = {"1234"};
SetSenderParameters(send_parameters_);
EXPECT_EQ(send_parameters_.options.audio_network_adaptor_config,
GetAudioNetworkAdaptorConfig(kSsrcX));
webrtc::RtpParameters parameters =
send_channel_->GetRtpSendParameters(kSsrcX);
parameters.encodings[0].adaptive_ptime = false;
EXPECT_TRUE(send_channel_->SetRtpSendParameters(kSsrcX, parameters).ok());
EXPECT_EQ(send_parameters_.options.audio_network_adaptor_config,
GetAudioNetworkAdaptorConfig(kSsrcX));
}
TEST_P(WebRtcVoiceEngineTestFake, AdaptivePtimeFieldTrial) {
webrtc::test::ScopedKeyValueConfig override_field_trials(
field_trials_, "WebRTC-Audio-AdaptivePtime/enabled:true/");
EXPECT_TRUE(SetupSendStream());
EXPECT_TRUE(GetAudioNetworkAdaptorConfig(kSsrcX));
}
// Test that SetRtpSendParameters configures the correct encoding channel for
// each SSRC.
TEST_P(WebRtcVoiceEngineTestFake, RtpParametersArePerStream) {
SetupForMultiSendStream();
// Create send streams.
for (uint32_t ssrc : kSsrcs4) {
EXPECT_TRUE(send_channel_->AddSendStream(
cricket::StreamParams::CreateLegacy(ssrc)));
}
// Configure one stream to be limited by the stream config, another to be
// limited by the global max, and the third one with no per-stream limit
// (still subject to the global limit).
SetGlobalMaxBitrate(kOpusCodec, 32000);
EXPECT_TRUE(SetMaxBitrateForStream(kSsrcs4[0], 24000));
EXPECT_TRUE(SetMaxBitrateForStream(kSsrcs4[1], 48000));
EXPECT_TRUE(SetMaxBitrateForStream(kSsrcs4[2], -1));
EXPECT_EQ(24000, GetCodecBitrate(kSsrcs4[0]));
EXPECT_EQ(32000, GetCodecBitrate(kSsrcs4[1]));
EXPECT_EQ(32000, GetCodecBitrate(kSsrcs4[2]));
// Remove the global cap; the streams should switch to their respective
// maximums (or remain unchanged if there was no other limit on them.)
SetGlobalMaxBitrate(kOpusCodec, -1);
EXPECT_EQ(24000, GetCodecBitrate(kSsrcs4[0]));
EXPECT_EQ(48000, GetCodecBitrate(kSsrcs4[1]));
EXPECT_EQ(32000, GetCodecBitrate(kSsrcs4[2]));
}
// Test that GetRtpSendParameters returns the currently configured codecs.
TEST_P(WebRtcVoiceEngineTestFake, GetRtpSendParametersCodecs) {
EXPECT_TRUE(SetupSendStream());
cricket::AudioSenderParameter parameters;
parameters.codecs.push_back(kOpusCodec);
parameters.codecs.push_back(kPcmuCodec);
SetSenderParameters(parameters);
webrtc::RtpParameters rtp_parameters =
send_channel_->GetRtpSendParameters(kSsrcX);
ASSERT_EQ(2u, rtp_parameters.codecs.size());
EXPECT_EQ(kOpusCodec.ToCodecParameters(), rtp_parameters.codecs[0]);
EXPECT_EQ(kPcmuCodec.ToCodecParameters(), rtp_parameters.codecs[1]);
}
// Test that GetRtpSendParameters returns the currently configured RTCP CNAME.
TEST_P(WebRtcVoiceEngineTestFake, GetRtpSendParametersRtcpCname) {
cricket::StreamParams params = cricket::StreamParams::CreateLegacy(kSsrcX);
params.cname = "rtcpcname";
EXPECT_TRUE(SetupSendStream(params));
webrtc::RtpParameters rtp_parameters =
send_channel_->GetRtpSendParameters(kSsrcX);
EXPECT_STREQ("rtcpcname", rtp_parameters.rtcp.cname.c_str());
}
TEST_P(WebRtcVoiceEngineTestFake,
DetectRtpSendParameterHeaderExtensionsChange) {
EXPECT_TRUE(SetupSendStream());
webrtc::RtpParameters rtp_parameters =
send_channel_->GetRtpSendParameters(kSsrcX);
rtp_parameters.header_extensions.emplace_back();
EXPECT_NE(0u, rtp_parameters.header_extensions.size());
webrtc::RTCError result =
send_channel_->SetRtpSendParameters(kSsrcX, rtp_parameters);
EXPECT_EQ(webrtc::RTCErrorType::INVALID_MODIFICATION, result.type());
}
// Test that GetRtpSendParameters returns an SSRC.
TEST_P(WebRtcVoiceEngineTestFake, GetRtpSendParametersSsrc) {
EXPECT_TRUE(SetupSendStream());
webrtc::RtpParameters rtp_parameters =
send_channel_->GetRtpSendParameters(kSsrcX);
ASSERT_EQ(1u, rtp_parameters.encodings.size());
EXPECT_EQ(kSsrcX, rtp_parameters.encodings[0].ssrc);
}
// Test that if we set/get parameters multiple times, we get the same results.
TEST_P(WebRtcVoiceEngineTestFake, SetAndGetRtpSendParameters) {
EXPECT_TRUE(SetupSendStream());
cricket::AudioSenderParameter parameters;
parameters.codecs.push_back(kOpusCodec);
parameters.codecs.push_back(kPcmuCodec);
SetSenderParameters(parameters);
webrtc::RtpParameters initial_params =
send_channel_->GetRtpSendParameters(kSsrcX);
// We should be able to set the params we just got.
EXPECT_TRUE(send_channel_->SetRtpSendParameters(kSsrcX, initial_params).ok());
// ... And this shouldn't change the params returned by GetRtpSendParameters.
webrtc::RtpParameters new_params =
send_channel_->GetRtpSendParameters(kSsrcX);
EXPECT_EQ(initial_params, send_channel_->GetRtpSendParameters(kSsrcX));
}
// Test that we remove the codec from RTP parameters if it's not negotiated
// anymore.
TEST_P(WebRtcVoiceEngineTestFake,
SetSendParametersRemovesSelectedCodecFromRtpParameters) {
EXPECT_TRUE(SetupSendStream());
cricket::AudioSenderParameter parameters;
parameters.codecs.push_back(kOpusCodec);
parameters.codecs.push_back(kPcmuCodec);
SetSenderParameters(parameters);
webrtc::RtpParameters initial_params =
send_channel_->GetRtpSendParameters(kSsrcX);
webrtc::RtpCodec opus_rtp_codec;
opus_rtp_codec.name = "opus";
opus_rtp_codec.kind = cricket::MEDIA_TYPE_AUDIO;
opus_rtp_codec.num_channels = 2;
opus_rtp_codec.clock_rate = 48000;
initial_params.encodings[0].codec = opus_rtp_codec;
// We should be able to set the params with the opus codec that has been
// negotiated.
EXPECT_TRUE(send_channel_->SetRtpSendParameters(kSsrcX, initial_params).ok());
parameters.codecs.clear();
parameters.codecs.push_back(kPcmuCodec);
SetSenderParameters(parameters);
// Since Opus is no longer negotiated, the RTP parameters should not have a
// forced codec anymore.
webrtc::RtpParameters new_params =
send_channel_->GetRtpSendParameters(kSsrcX);
EXPECT_EQ(new_params.encodings[0].codec, absl::nullopt);
}
// Test that max_bitrate_bps in send stream config gets updated correctly when
// SetRtpSendParameters is called.
TEST_P(WebRtcVoiceEngineTestFake, SetRtpSendParameterUpdatesMaxBitrate) {
EXPECT_TRUE(SetupSendStream());
cricket::AudioSenderParameter send_parameters;
send_parameters.codecs.push_back(kOpusCodec);
SetSenderParameters(send_parameters);
webrtc::RtpParameters rtp_parameters =
send_channel_->GetRtpSendParameters(kSsrcX);
// Expect empty on parameters.encodings[0].max_bitrate_bps;
EXPECT_FALSE(rtp_parameters.encodings[0].max_bitrate_bps);
constexpr int kMaxBitrateBps = 6000;
rtp_parameters.encodings[0].max_bitrate_bps = kMaxBitrateBps;
EXPECT_TRUE(send_channel_->SetRtpSendParameters(kSsrcX, rtp_parameters).ok());
const int max_bitrate = GetSendStreamConfig(kSsrcX).max_bitrate_bps;
EXPECT_EQ(max_bitrate, kMaxBitrateBps);
}
// Tests that when RTCRtpEncodingParameters.bitrate_priority gets set to
// a value <= 0, setting the parameters returns false.
TEST_P(WebRtcVoiceEngineTestFake, SetRtpSendParameterInvalidBitratePriority) {
EXPECT_TRUE(SetupSendStream());
webrtc::RtpParameters rtp_parameters =
send_channel_->GetRtpSendParameters(kSsrcX);
EXPECT_EQ(1UL, rtp_parameters.encodings.size());
EXPECT_EQ(webrtc::kDefaultBitratePriority,
rtp_parameters.encodings[0].bitrate_priority);
rtp_parameters.encodings[0].bitrate_priority = 0;
EXPECT_FALSE(
send_channel_->SetRtpSendParameters(kSsrcX, rtp_parameters).ok());
rtp_parameters.encodings[0].bitrate_priority = -1.0;
EXPECT_FALSE(
send_channel_->SetRtpSendParameters(kSsrcX, rtp_parameters).ok());
}
// Test that the bitrate_priority in the send stream config gets updated when
// SetRtpSendParameters is set for the VoiceMediaChannel.
TEST_P(WebRtcVoiceEngineTestFake, SetRtpSendParameterUpdatesBitratePriority) {
EXPECT_TRUE(SetupSendStream());
webrtc::RtpParameters rtp_parameters =
send_channel_->GetRtpSendParameters(kSsrcX);
EXPECT_EQ(1UL, rtp_parameters.encodings.size());
EXPECT_EQ(webrtc::kDefaultBitratePriority,
rtp_parameters.encodings[0].bitrate_priority);
double new_bitrate_priority = 2.0;
rtp_parameters.encodings[0].bitrate_priority = new_bitrate_priority;
EXPECT_TRUE(send_channel_->SetRtpSendParameters(kSsrcX, rtp_parameters).ok());
// The priority should get set for both the audio channel's rtp parameters
// and the audio send stream's audio config.
EXPECT_EQ(new_bitrate_priority, send_channel_->GetRtpSendParameters(kSsrcX)
.encodings[0]
.bitrate_priority);
EXPECT_EQ(new_bitrate_priority, GetSendStreamConfig(kSsrcX).bitrate_priority);
}
// Test that GetRtpReceiverParameters returns the currently configured codecs.
TEST_P(WebRtcVoiceEngineTestFake, GetRtpReceiveParametersCodecs) {
EXPECT_TRUE(SetupRecvStream());
cricket::AudioReceiverParameters parameters;
parameters.codecs.push_back(kOpusCodec);
parameters.codecs.push_back(kPcmuCodec);
EXPECT_TRUE(receive_channel_->SetReceiverParameters(parameters));
webrtc::RtpParameters rtp_parameters =
receive_channel_->GetRtpReceiverParameters(kSsrcX);
ASSERT_EQ(2u, rtp_parameters.codecs.size());
EXPECT_EQ(kOpusCodec.ToCodecParameters(), rtp_parameters.codecs[0]);
EXPECT_EQ(kPcmuCodec.ToCodecParameters(), rtp_parameters.codecs[1]);
}
// Test that GetRtpReceiverParameters returns an SSRC.
TEST_P(WebRtcVoiceEngineTestFake, GetRtpReceiveParametersSsrc) {
EXPECT_TRUE(SetupRecvStream());
webrtc::RtpParameters rtp_parameters =
receive_channel_->GetRtpReceiverParameters(kSsrcX);
ASSERT_EQ(1u, rtp_parameters.encodings.size());
EXPECT_EQ(kSsrcX, rtp_parameters.encodings[0].ssrc);
}
// Test that if we set/get parameters multiple times, we get the same results.
TEST_P(WebRtcVoiceEngineTestFake, SetAndGetRtpReceiveParameters) {
EXPECT_TRUE(SetupRecvStream());
cricket::AudioReceiverParameters parameters;
parameters.codecs.push_back(kOpusCodec);
parameters.codecs.push_back(kPcmuCodec);
EXPECT_TRUE(receive_channel_->SetReceiverParameters(parameters));
webrtc::RtpParameters initial_params =
receive_channel_->GetRtpReceiverParameters(kSsrcX);
// ... And this shouldn't change the params returned by
// GetRtpReceiverParameters.
webrtc::RtpParameters new_params =
receive_channel_->GetRtpReceiverParameters(kSsrcX);
EXPECT_EQ(initial_params, receive_channel_->GetRtpReceiverParameters(kSsrcX));
}
// Test that GetRtpReceiverParameters returns parameters correctly when SSRCs
// aren't signaled. It should return an empty "RtpEncodingParameters" when
// configured to receive an unsignaled stream and no packets have been received
// yet, and start returning the SSRC once a packet has been received.
TEST_P(WebRtcVoiceEngineTestFake, GetRtpReceiveParametersWithUnsignaledSsrc) {
ASSERT_TRUE(SetupChannel());
// Call necessary methods to configure receiving a default stream as
// soon as it arrives.
cricket::AudioReceiverParameters parameters;
parameters.codecs.push_back(kOpusCodec);
parameters.codecs.push_back(kPcmuCodec);
EXPECT_TRUE(receive_channel_->SetReceiverParameters(parameters));
// Call GetDefaultRtpReceiveParameters before configured to receive an
// unsignaled stream. Should return nothing.
EXPECT_EQ(webrtc::RtpParameters(),
receive_channel_->GetDefaultRtpReceiveParameters());
// Set a sink for an unsignaled stream.
std::unique_ptr<FakeAudioSink> fake_sink(new FakeAudioSink());
receive_channel_->SetDefaultRawAudioSink(std::move(fake_sink));
// Call GetDefaultRtpReceiveParameters before the SSRC is known.
webrtc::RtpParameters rtp_parameters =
receive_channel_->GetDefaultRtpReceiveParameters();
ASSERT_EQ(1u, rtp_parameters.encodings.size());
EXPECT_FALSE(rtp_parameters.encodings[0].ssrc);
// Receive PCMU packet (SSRC=1).
DeliverPacket(kPcmuFrame, sizeof(kPcmuFrame));
// The `ssrc` member should still be unset.
rtp_parameters = receive_channel_->GetDefaultRtpReceiveParameters();
ASSERT_EQ(1u, rtp_parameters.encodings.size());
EXPECT_FALSE(rtp_parameters.encodings[0].ssrc);
}
TEST_P(WebRtcVoiceEngineTestFake, OnPacketReceivedIdentifiesExtensions) {
ASSERT_TRUE(SetupChannel());
cricket::AudioReceiverParameters parameters = recv_parameters_;
parameters.extensions.push_back(
RtpExtension(RtpExtension::kAudioLevelUri, /*id=*/1));
ASSERT_TRUE(receive_channel_->SetReceiverParameters(parameters));
webrtc::RtpHeaderExtensionMap extension_map(parameters.extensions);
webrtc::RtpPacketReceived reference_packet(&extension_map);
constexpr uint8_t kAudioLevel = 123;
reference_packet.SetExtension<webrtc::AudioLevel>(/*voice_activity=*/true,
kAudioLevel);
// Create a packet without the extension map but with the same content.
webrtc::RtpPacketReceived received_packet;
ASSERT_TRUE(received_packet.Parse(reference_packet.Buffer()));
receive_channel_->OnPacketReceived(received_packet);
rtc::Thread::Current()->ProcessMessages(0);
bool voice_activity;
uint8_t audio_level;
EXPECT_TRUE(call_.last_received_rtp_packet().GetExtension<webrtc::AudioLevel>(
&voice_activity, &audio_level));
EXPECT_EQ(audio_level, kAudioLevel);
}
// Test that we apply codecs properly.
TEST_P(WebRtcVoiceEngineTestFake, SetSendCodecs) {
EXPECT_TRUE(SetupSendStream());
cricket::AudioSenderParameter parameters;
parameters.codecs.push_back(kOpusCodec);
parameters.codecs.push_back(kPcmuCodec);
parameters.codecs.push_back(kCn8000Codec);
parameters.codecs[0].id = 96;
parameters.codecs[0].bitrate = 22000;
SetSenderParameters(parameters);
const auto& send_codec_spec = *GetSendStreamConfig(kSsrcX).send_codec_spec;
EXPECT_EQ(96, send_codec_spec.payload_type);
EXPECT_EQ(22000, send_codec_spec.target_bitrate_bps);
EXPECT_STRCASEEQ("OPUS", send_codec_spec.format.name.c_str());
EXPECT_NE(send_codec_spec.format.clockrate_hz, 8000);
EXPECT_EQ(absl::nullopt, send_codec_spec.cng_payload_type);
EXPECT_FALSE(send_channel_->CanInsertDtmf());
}
// Test that we use Opus/Red by default when it is
// listed as the first codec and there is an fmtp line.
TEST_P(WebRtcVoiceEngineTestFake, SetSendCodecsRed) {
EXPECT_TRUE(SetupSendStream());
cricket::AudioSenderParameter parameters;
parameters.codecs.push_back(kRed48000Codec);
parameters.codecs[0].params[""] = "111/111";
parameters.codecs.push_back(kOpusCodec);
SetSenderParameters(parameters);
const auto& send_codec_spec = *GetSendStreamConfig(kSsrcX).send_codec_spec;
EXPECT_EQ(111, send_codec_spec.payload_type);
EXPECT_STRCASEEQ("opus", send_codec_spec.format.name.c_str());
EXPECT_EQ(112, send_codec_spec.red_payload_type);
}
// Test that we do not use Opus/Red by default when it is
// listed as the first codec but there is no fmtp line.
TEST_P(WebRtcVoiceEngineTestFake, SetSendCodecsRedNoFmtp) {
EXPECT_TRUE(SetupSendStream());
cricket::AudioSenderParameter parameters;
parameters.codecs.push_back(kRed48000Codec);
parameters.codecs.push_back(kOpusCodec);
SetSenderParameters(parameters);
const auto& send_codec_spec = *GetSendStreamConfig(kSsrcX).send_codec_spec;
EXPECT_EQ(111, send_codec_spec.payload_type);
EXPECT_STRCASEEQ("opus", send_codec_spec.format.name.c_str());
EXPECT_EQ(absl::nullopt, send_codec_spec.red_payload_type);
}
// Test that we do not use Opus/Red by default.
TEST_P(WebRtcVoiceEngineTestFake, SetSendCodecsRedDefault) {
EXPECT_TRUE(SetupSendStream());
cricket::AudioSenderParameter parameters;
parameters.codecs.push_back(kOpusCodec);
parameters.codecs.push_back(kRed48000Codec);
parameters.codecs[1].params[""] = "111/111";
SetSenderParameters(parameters);
const auto& send_codec_spec = *GetSendStreamConfig(kSsrcX).send_codec_spec;
EXPECT_EQ(111, send_codec_spec.payload_type);
EXPECT_STRCASEEQ("opus", send_codec_spec.format.name.c_str());
EXPECT_EQ(absl::nullopt, send_codec_spec.red_payload_type);
}
// Test that the RED fmtp line must match the payload type.
TEST_P(WebRtcVoiceEngineTestFake, SetSendCodecsRedFmtpMismatch) {
EXPECT_TRUE(SetupSendStream());
cricket::AudioSenderParameter parameters;
parameters.codecs.push_back(kRed48000Codec);
parameters.codecs[0].params[""] = "8/8";
parameters.codecs.push_back(kOpusCodec);
SetSenderParameters(parameters);
const auto& send_codec_spec = *GetSendStreamConfig(kSsrcX).send_codec_spec;
EXPECT_EQ(111, send_codec_spec.payload_type);
EXPECT_STRCASEEQ("opus", send_codec_spec.format.name.c_str());
EXPECT_EQ(absl::nullopt, send_codec_spec.red_payload_type);
}
// Test that the RED fmtp line must show 2..32 payloads.
TEST_P(WebRtcVoiceEngineTestFake, SetSendCodecsRedFmtpAmountOfRedundancy) {
EXPECT_TRUE(SetupSendStream());
cricket::AudioSenderParameter parameters;
parameters.codecs.push_back(kRed48000Codec);
parameters.codecs[0].params[""] = "111";
parameters.codecs.push_back(kOpusCodec);
SetSenderParameters(parameters);
const auto& send_codec_spec = *GetSendStreamConfig(kSsrcX).send_codec_spec;
EXPECT_EQ(111, send_codec_spec.payload_type);
EXPECT_STRCASEEQ("opus", send_codec_spec.format.name.c_str());
EXPECT_EQ(absl::nullopt, send_codec_spec.red_payload_type);
for (int i = 1; i < 32; i++) {
parameters.codecs[0].params[""] += "/111";
SetSenderParameters(parameters);
const auto& send_codec_spec2 = *GetSendStreamConfig(kSsrcX).send_codec_spec;
EXPECT_EQ(111, send_codec_spec2.payload_type);
EXPECT_STRCASEEQ("opus", send_codec_spec2.format.name.c_str());
EXPECT_EQ(112, send_codec_spec2.red_payload_type);
}
parameters.codecs[0].params[""] += "/111";
SetSenderParameters(parameters);
const auto& send_codec_spec3 = *GetSendStreamConfig(kSsrcX).send_codec_spec;
EXPECT_EQ(111, send_codec_spec3.payload_type);
EXPECT_STRCASEEQ("opus", send_codec_spec3.format.name.c_str());
EXPECT_EQ(absl::nullopt, send_codec_spec3.red_payload_type);
}
// Test that WebRtcVoiceEngine reconfigures, rather than recreates its
// AudioSendStream.
TEST_P(WebRtcVoiceEngineTestFake, DontRecreateSendStream) {
EXPECT_TRUE(SetupSendStream());
cricket::AudioSenderParameter parameters;
parameters.codecs.push_back(kOpusCodec);
parameters.codecs.push_back(kPcmuCodec);
parameters.codecs.push_back(kCn8000Codec);
parameters.codecs[0].id = 96;
parameters.codecs[0].bitrate = 48000;
const int initial_num = call_.GetNumCreatedSendStreams();
SetSenderParameters(parameters);
EXPECT_EQ(initial_num, call_.GetNumCreatedSendStreams());
// Calling SetSendCodec again with same codec which is already set.
// In this case media channel shouldn't send codec to VoE.
SetSenderParameters(parameters);
EXPECT_EQ(initial_num, call_.GetNumCreatedSendStreams());
}
// TODO(ossu): Revisit if these tests need to be here, now that these kinds of
// tests should be available in AudioEncoderOpusTest.
// Test that if clockrate is not 48000 for opus, we do not have a send codec.
TEST_P(WebRtcVoiceEngineTestFake, SetSendCodecOpusBadClockrate) {
EXPECT_TRUE(SetupSendStream());
cricket::AudioSenderParameter parameters;
parameters.codecs.push_back(kOpusCodec);
parameters.codecs[0].bitrate = 0;
parameters.codecs[0].clockrate = 50000;
EXPECT_TRUE(send_channel_->SetSenderParameters(parameters));
EXPECT_EQ(send_channel_->GetSendCodec(), absl::nullopt);
}
// Test that if channels=0 for opus, we do not have a send codec.
TEST_P(WebRtcVoiceEngineTestFake, SetSendCodecOpusBad0ChannelsNoStereo) {
EXPECT_TRUE(SetupSendStream());
cricket::AudioSenderParameter parameters;
parameters.codecs.push_back(kOpusCodec);
parameters.codecs[0].bitrate = 0;
parameters.codecs[0].channels = 0;
EXPECT_TRUE(send_channel_->SetSenderParameters(parameters));
EXPECT_EQ(send_channel_->GetSendCodec(), absl::nullopt);
}
// Test that if channels=0 for opus, we do not have a send codec.
TEST_P(WebRtcVoiceEngineTestFake, SetSendCodecOpusBad0Channels1Stereo) {
EXPECT_TRUE(SetupSendStream());
cricket::AudioSenderParameter parameters;
parameters.codecs.push_back(kOpusCodec);
parameters.codecs[0].bitrate = 0;
parameters.codecs[0].channels = 0;
parameters.codecs[0].params["stereo"] = "1";
EXPECT_TRUE(send_channel_->SetSenderParameters(parameters));
EXPECT_EQ(send_channel_->GetSendCodec(), absl::nullopt);
}
// Test that if channel is 1 for opus and there's no stereo, we do not have a
// send codec.
TEST_P(WebRtcVoiceEngineTestFake, SetSendCodecOpus1ChannelNoStereo) {
EXPECT_TRUE(SetupSendStream());
cricket::AudioSenderParameter parameters;
parameters.codecs.push_back(kOpusCodec);
parameters.codecs[0].bitrate = 0;
parameters.codecs[0].channels = 1;
EXPECT_TRUE(send_channel_->SetSenderParameters(parameters));
EXPECT_EQ(send_channel_->GetSendCodec(), absl::nullopt);
}
// Test that if channel is 1 for opus and stereo=0, we do not have a send codec.
TEST_P(WebRtcVoiceEngineTestFake, SetSendCodecOpusBad1Channel0Stereo) {
EXPECT_TRUE(SetupSendStream());
cricket::AudioSenderParameter parameters;
parameters.codecs.push_back(kOpusCodec);
parameters.codecs[0].bitrate = 0;
parameters.codecs[0].channels = 1;
parameters.codecs[0].params["stereo"] = "0";
EXPECT_TRUE(send_channel_->SetSenderParameters(parameters));
EXPECT_EQ(send_channel_->GetSendCodec(), absl::nullopt);
}
// Test that if channel is 1 for opus and stereo=1, we do not have a send codec.
TEST_P(WebRtcVoiceEngineTestFake, SetSendCodecOpusBad1Channel1Stereo) {
EXPECT_TRUE(SetupSendStream());
cricket::AudioSenderParameter parameters;
parameters.codecs.push_back(kOpusCodec);
parameters.codecs[0].bitrate = 0;
parameters.codecs[0].channels = 1;
parameters.codecs[0].params["stereo"] = "1";
EXPECT_TRUE(send_channel_->SetSenderParameters(parameters));
EXPECT_EQ(send_channel_->GetSendCodec(), absl::nullopt);
}
// Test that with bitrate=0 and no stereo, bitrate is 32000.
TEST_P(WebRtcVoiceEngineTestFake, SetSendCodecOpusGood0BitrateNoStereo) {
EXPECT_TRUE(SetupSendStream());
cricket::AudioSenderParameter parameters;
parameters.codecs.push_back(kOpusCodec);
parameters.codecs[0].bitrate = 0;
SetSenderParameters(parameters);
CheckSendCodecBitrate(kSsrcX, "opus", 32000);
}
// Test that with bitrate=0 and stereo=0, bitrate is 32000.
TEST_P(WebRtcVoiceEngineTestFake, SetSendCodecOpusGood0Bitrate0Stereo) {
EXPECT_TRUE(SetupSendStream());
cricket::AudioSenderParameter parameters;
parameters.codecs.push_back(kOpusCodec);
parameters.codecs[0].bitrate = 0;
parameters.codecs[0].params["stereo"] = "0";
SetSenderParameters(parameters);
CheckSendCodecBitrate(kSsrcX, "opus", 32000);
}
// Test that with bitrate=invalid and stereo=0, bitrate is 32000.
TEST_P(WebRtcVoiceEngineTestFake, SetSendCodecOpusGoodXBitrate0Stereo) {
EXPECT_TRUE(SetupSendStream());
cricket::AudioSenderParameter parameters;
parameters.codecs.push_back(kOpusCodec);
parameters.codecs[0].params["stereo"] = "0";
// bitrate that's out of the range between 6000 and 510000 will be clamped.
parameters.codecs[0].bitrate = 5999;
SetSenderParameters(parameters);
CheckSendCodecBitrate(kSsrcX, "opus", 6000);
parameters.codecs[0].bitrate = 510001;
SetSenderParameters(parameters);
CheckSendCodecBitrate(kSsrcX, "opus", 510000);
}
// Test that with bitrate=0 and stereo=1, bitrate is 64000.
TEST_P(WebRtcVoiceEngineTestFake, SetSendCodecOpusGood0Bitrate1Stereo) {
EXPECT_TRUE(SetupSendStream());
cricket::AudioSenderParameter parameters;
parameters.codecs.push_back(kOpusCodec);
parameters.codecs[0].bitrate = 0;
parameters.codecs[0].params["stereo"] = "1";
SetSenderParameters(parameters);
CheckSendCodecBitrate(kSsrcX, "opus", 64000);
}
// Test that with bitrate=invalid and stereo=1, bitrate is 64000.
TEST_P(WebRtcVoiceEngineTestFake, SetSendCodecOpusGoodXBitrate1Stereo) {
EXPECT_TRUE(SetupSendStream());
cricket::AudioSenderParameter parameters;
parameters.codecs.push_back(kOpusCodec);
parameters.codecs[0].params["stereo"] = "1";
// bitrate that's out of the range between 6000 and 510000 will be clamped.
parameters.codecs[0].bitrate = 5999;
SetSenderParameters(parameters);
CheckSendCodecBitrate(kSsrcX, "opus", 6000);
parameters.codecs[0].bitrate = 510001;
SetSenderParameters(parameters);
CheckSendCodecBitrate(kSsrcX, "opus", 510000);
}
// Test that with bitrate=N and stereo unset, bitrate is N.
TEST_P(WebRtcVoiceEngineTestFake, SetSendCodecOpusGoodNBitrateNoStereo) {
EXPECT_TRUE(SetupSendStream());
cricket::AudioSenderParameter parameters;
parameters.codecs.push_back(kOpusCodec);
parameters.codecs[0].bitrate = 96000;
SetSenderParameters(parameters);
const auto& spec = *GetSendStreamConfig(kSsrcX).send_codec_spec;
EXPECT_EQ(111, spec.payload_type);
EXPECT_EQ(96000, spec.target_bitrate_bps);
EXPECT_EQ("opus", spec.format.name);
EXPECT_EQ(2u, spec.format.num_channels);
EXPECT_EQ(48000, spec.format.clockrate_hz);
}
// Test that with bitrate=N and stereo=0, bitrate is N.
TEST_P(WebRtcVoiceEngineTestFake, SetSendCodecOpusGoodNBitrate0Stereo) {
EXPECT_TRUE(SetupSendStream());
cricket::AudioSenderParameter parameters;
parameters.codecs.push_back(kOpusCodec);
parameters.codecs[0].bitrate = 30000;
parameters.codecs[0].params["stereo"] = "0";
SetSenderParameters(parameters);
CheckSendCodecBitrate(kSsrcX, "opus", 30000);
}
// Test that with bitrate=N and without any parameters, bitrate is N.
TEST_P(WebRtcVoiceEngineTestFake, SetSendCodecOpusGoodNBitrateNoParameters) {
EXPECT_TRUE(SetupSendStream());
cricket::AudioSenderParameter parameters;
parameters.codecs.push_back(kOpusCodec);
parameters.codecs[0].bitrate = 30000;
SetSenderParameters(parameters);
CheckSendCodecBitrate(kSsrcX, "opus", 30000);
}
// Test that with bitrate=N and stereo=1, bitrate is N.
TEST_P(WebRtcVoiceEngineTestFake, SetSendCodecOpusGoodNBitrate1Stereo) {
EXPECT_TRUE(SetupSendStream());
cricket::AudioSenderParameter parameters;
parameters.codecs.push_back(kOpusCodec);
parameters.codecs[0].bitrate = 30000;
parameters.codecs[0].params["stereo"] = "1";
SetSenderParameters(parameters);
CheckSendCodecBitrate(kSsrcX, "opus", 30000);
}
TEST_P(WebRtcVoiceEngineTestFake, SetSendCodecsWithBitrates) {
SetSendCodecsShouldWorkForBitrates("100", 100000, "150", 150000, "200",
200000);
}
TEST_P(WebRtcVoiceEngineTestFake, SetSendCodecsWithHighMaxBitrate) {
SetSendCodecsShouldWorkForBitrates("", 0, "", -1, "10000", 10000000);
}
TEST_P(WebRtcVoiceEngineTestFake,
SetSendCodecsWithoutBitratesUsesCorrectDefaults) {
SetSendCodecsShouldWorkForBitrates("", 0, "", -1, "", -1);
}
TEST_P(WebRtcVoiceEngineTestFake, SetSendCodecsCapsMinAndStartBitrate) {
SetSendCodecsShouldWorkForBitrates("-1", 0, "-100", -1, "", -1);
}
TEST_P(WebRtcVoiceEngineTestFake, SetMaxSendBandwidthForAudioDoesntAffectBwe) {
SetSendCodecsShouldWorkForBitrates("100", 100000, "150", 150000, "200",
200000);
send_parameters_.max_bandwidth_bps = 100000;
// Setting max bitrate should keep previous min bitrate
// Setting max bitrate should not reset start bitrate.
EXPECT_CALL(*call_.GetMockTransportControllerSend(),
SetSdpBitrateParameters(
AllOf(Field(&BitrateConstraints::min_bitrate_bps, 100000),
Field(&BitrateConstraints::start_bitrate_bps, -1),
Field(&BitrateConstraints::max_bitrate_bps, 200000))));
SetSenderParameters(send_parameters_);
}
// Test that we can enable NACK with opus as callee.
TEST_P(WebRtcVoiceEngineTestFake, SetSendCodecEnableNackAsCallee) {
EXPECT_TRUE(SetupRecvStream());
cricket::AudioSenderParameter parameters;
parameters.codecs.push_back(kOpusCodec);
parameters.codecs[0].AddFeedbackParam(cricket::FeedbackParam(
cricket::kRtcpFbParamNack, cricket::kParamValueEmpty));
EXPECT_EQ(0, GetRecvStreamConfig(kSsrcX).rtp.nack.rtp_history_ms);
SetSenderParameters(parameters);
// NACK should be enabled even with no send stream.
EXPECT_EQ(kRtpHistoryMs, GetRecvStreamConfig(kSsrcX).rtp.nack.rtp_history_ms);
EXPECT_TRUE(send_channel_->AddSendStream(
cricket::StreamParams::CreateLegacy(kSsrcX)));
}
// Test that we can enable NACK on receive streams.
TEST_P(WebRtcVoiceEngineTestFake, SetSendCodecEnableNackRecvStreams) {
EXPECT_TRUE(SetupSendStream());
EXPECT_TRUE(AddRecvStream(kSsrcY));
cricket::AudioSenderParameter parameters;
parameters.codecs.push_back(kOpusCodec);
parameters.codecs[0].AddFeedbackParam(cricket::FeedbackParam(
cricket::kRtcpFbParamNack, cricket::kParamValueEmpty));
EXPECT_EQ(0, GetRecvStreamConfig(kSsrcY).rtp.nack.rtp_history_ms);
SetSenderParameters(parameters);
EXPECT_EQ(kRtpHistoryMs, GetRecvStreamConfig(kSsrcY).rtp.nack.rtp_history_ms);
}
// Test that we can disable NACK on receive streams.
TEST_P(WebRtcVoiceEngineTestFake, SetSendCodecDisableNackRecvStreams) {
EXPECT_TRUE(SetupSendStream());
EXPECT_TRUE(AddRecvStream(kSsrcY));
cricket::AudioSenderParameter parameters;
parameters.codecs.push_back(kOpusCodec);
parameters.codecs[0].AddFeedbackParam(cricket::FeedbackParam(
cricket::kRtcpFbParamNack, cricket::kParamValueEmpty));
SetSenderParameters(parameters);
EXPECT_EQ(kRtpHistoryMs, GetRecvStreamConfig(kSsrcY).rtp.nack.rtp_history_ms);
parameters.codecs.clear();
parameters.codecs.push_back(kOpusCodec);
SetSenderParameters(parameters);
EXPECT_EQ(0, GetRecvStreamConfig(kSsrcY).rtp.nack.rtp_history_ms);
}
// Test that NACK is enabled on a new receive stream.
TEST_P(WebRtcVoiceEngineTestFake, AddRecvStreamEnableNack) {
EXPECT_TRUE(SetupSendStream());
cricket::AudioSenderParameter parameters;
parameters.codecs.push_back(kOpusCodec);
parameters.codecs.push_back(kCn16000Codec);
parameters.codecs[0].AddFeedbackParam(cricket::FeedbackParam(
cricket::kRtcpFbParamNack, cricket::kParamValueEmpty));
SetSenderParameters(parameters);
EXPECT_TRUE(AddRecvStream(kSsrcY));
EXPECT_EQ(kRtpHistoryMs, GetRecvStreamConfig(kSsrcY).rtp.nack.rtp_history_ms);
EXPECT_TRUE(AddRecvStream(kSsrcZ));
EXPECT_EQ(kRtpHistoryMs, GetRecvStreamConfig(kSsrcZ).rtp.nack.rtp_history_ms);
}
// Test that we can switch back and forth between Opus and PCMU with CN.
TEST_P(WebRtcVoiceEngineTestFake, SetSendCodecsOpusPcmuSwitching) {
EXPECT_TRUE(SetupSendStream());
cricket::AudioSenderParameter opus_parameters;
opus_parameters.codecs.push_back(kOpusCodec);
SetSenderParameters(opus_parameters);
{
const auto& spec = *GetSendStreamConfig(kSsrcX).send_codec_spec;
EXPECT_EQ(111, spec.payload_type);
EXPECT_STRCASEEQ("opus", spec.format.name.c_str());
}
cricket::AudioSenderParameter pcmu_parameters;
pcmu_parameters.codecs.push_back(kPcmuCodec);
pcmu_parameters.codecs.push_back(kCn16000Codec);
pcmu_parameters.codecs.push_back(kOpusCodec);
SetSenderParameters(pcmu_parameters);
{
const auto& spec = *GetSendStreamConfig(kSsrcX).send_codec_spec;
EXPECT_EQ(0, spec.payload_type);
EXPECT_STRCASEEQ("PCMU", spec.format.name.c_str());
}
SetSenderParameters(opus_parameters);
{
const auto& spec = *GetSendStreamConfig(kSsrcX).send_codec_spec;
EXPECT_EQ(111, spec.payload_type);
EXPECT_STRCASEEQ("opus", spec.format.name.c_str());
}
}
// Test that we handle various ways of specifying bitrate.
TEST_P(WebRtcVoiceEngineTestFake, SetSendCodecsBitrate) {
EXPECT_TRUE(SetupSendStream());
cricket::AudioSenderParameter parameters;
parameters.codecs.push_back(kPcmuCodec);
SetSenderParameters(parameters);
{
const auto& spec = *GetSendStreamConfig(kSsrcX).send_codec_spec;
EXPECT_EQ(0, spec.payload_type);
EXPECT_STRCASEEQ("PCMU", spec.format.name.c_str());
EXPECT_EQ(64000, spec.target_bitrate_bps);
}
parameters.codecs[0].bitrate = 0; // bitrate == default
SetSenderParameters(parameters);
{
const auto& spec = *GetSendStreamConfig(kSsrcX).send_codec_spec;
EXPECT_EQ(0, spec.payload_type);
EXPECT_STREQ("PCMU", spec.format.name.c_str());
EXPECT_EQ(64000, spec.target_bitrate_bps);
}
parameters.codecs[0] = kOpusCodec;
parameters.codecs[0].bitrate = 0; // bitrate == default
SetSenderParameters(parameters);
{
const auto& spec = *GetSendStreamConfig(kSsrcX).send_codec_spec;
EXPECT_EQ(111, spec.payload_type);
EXPECT_STREQ("opus", spec.format.name.c_str());
EXPECT_EQ(32000, spec.target_bitrate_bps);
}
}
// Test that we do not fail if no codecs are specified.
TEST_P(WebRtcVoiceEngineTestFake, SetSendCodecsNoCodecs) {
EXPECT_TRUE(SetupSendStream());
cricket::AudioSenderParameter parameters;
EXPECT_TRUE(send_channel_->SetSenderParameters(parameters));
EXPECT_EQ(send_channel_->GetSendCodec(), absl::nullopt);
}
// Test that we can set send codecs even with telephone-event codec as the first
// one on the list.
TEST_P(WebRtcVoiceEngineTestFake, SetSendCodecsDTMFOnTop) {
EXPECT_TRUE(SetupSendStream());
cricket::AudioSenderParameter parameters;
parameters.codecs.push_back(kTelephoneEventCodec1);
parameters.codecs.push_back(kOpusCodec);
parameters.codecs.push_back(kPcmuCodec);
parameters.codecs[0].id = 98; // DTMF
parameters.codecs[1].id = 96;
SetSenderParameters(parameters);
const auto& spec = *GetSendStreamConfig(kSsrcX).send_codec_spec;
EXPECT_EQ(96, spec.payload_type);
EXPECT_STRCASEEQ("OPUS", spec.format.name.c_str());
SetSend(true);
EXPECT_TRUE(send_channel_->CanInsertDtmf());
}
// Test that CanInsertDtmf() is governed by the send flag
TEST_P(WebRtcVoiceEngineTestFake, DTMFControlledBySendFlag) {
EXPECT_TRUE(SetupSendStream());
cricket::AudioSenderParameter parameters;
parameters.codecs.push_back(kTelephoneEventCodec1);
parameters.codecs.push_back(kPcmuCodec);
parameters.codecs[0].id = 98; // DTMF
parameters.codecs[1].id = 96;
SetSenderParameters(parameters);
EXPECT_FALSE(send_channel_->CanInsertDtmf());
SetSend(true);
EXPECT_TRUE(send_channel_->CanInsertDtmf());
SetSend(false);
EXPECT_FALSE(send_channel_->CanInsertDtmf());
}
// Test that payload type range is limited for telephone-event codec.
TEST_P(WebRtcVoiceEngineTestFake, SetSendCodecsDTMFPayloadTypeOutOfRange) {
EXPECT_TRUE(SetupSendStream());
cricket::AudioSenderParameter parameters;
parameters.codecs.push_back(kTelephoneEventCodec2);
parameters.codecs.push_back(kOpusCodec);
parameters.codecs[0].id = 0; // DTMF
parameters.codecs[1].id = 96;
SetSenderParameters(parameters);
SetSend(true);
EXPECT_TRUE(send_channel_->CanInsertDtmf());
parameters.codecs[0].id = 128; // DTMF
EXPECT_FALSE(send_channel_->SetSenderParameters(parameters));
EXPECT_FALSE(send_channel_->CanInsertDtmf());
parameters.codecs[0].id = 127;
SetSenderParameters(parameters);
EXPECT_TRUE(send_channel_->CanInsertDtmf());
parameters.codecs[0].id = -1; // DTMF
EXPECT_FALSE(send_channel_->SetSenderParameters(parameters));
EXPECT_FALSE(send_channel_->CanInsertDtmf());
}
// Test that we can set send codecs even with CN codec as the first
// one on the list.
TEST_P(WebRtcVoiceEngineTestFake, SetSendCodecsCNOnTop) {
EXPECT_TRUE(SetupSendStream());
cricket::AudioSenderParameter parameters;
parameters.codecs.push_back(kCn8000Codec);
parameters.codecs.push_back(kPcmuCodec);
parameters.codecs[0].id = 98; // narrowband CN
SetSenderParameters(parameters);
const auto& send_codec_spec = *GetSendStreamConfig(kSsrcX).send_codec_spec;
EXPECT_EQ(0, send_codec_spec.payload_type);
EXPECT_STRCASEEQ("PCMU", send_codec_spec.format.name.c_str());
EXPECT_EQ(98, send_codec_spec.cng_payload_type);
}
// Test that we set VAD and DTMF types correctly as caller.
TEST_P(WebRtcVoiceEngineTestFake, SetSendCodecsCNandDTMFAsCaller) {
EXPECT_TRUE(SetupSendStream());
cricket::AudioSenderParameter parameters;
parameters.codecs.push_back(kPcmuCodec);
parameters.codecs.push_back(kCn16000Codec);
parameters.codecs.push_back(kCn8000Codec);
parameters.codecs.push_back(kTelephoneEventCodec1);
parameters.codecs[0].id = 96;
parameters.codecs[2].id = 97; // narrowband CN
parameters.codecs[3].id = 98; // DTMF
SetSenderParameters(parameters);
const auto& send_codec_spec = *GetSendStreamConfig(kSsrcX).send_codec_spec;
EXPECT_EQ(96, send_codec_spec.payload_type);
EXPECT_STRCASEEQ("PCMU", send_codec_spec.format.name.c_str());
EXPECT_EQ(1u, send_codec_spec.format.num_channels);
EXPECT_EQ(97, send_codec_spec.cng_payload_type);
SetSend(true);
EXPECT_TRUE(send_channel_->CanInsertDtmf());
}
// Test that we set VAD and DTMF types correctly as callee.
TEST_P(WebRtcVoiceEngineTestFake, SetSendCodecsCNandDTMFAsCallee) {
EXPECT_TRUE(SetupChannel());
cricket::AudioSenderParameter parameters;
parameters.codecs.push_back(kPcmuCodec);
parameters.codecs.push_back(kCn16000Codec);
parameters.codecs.push_back(kCn8000Codec);
parameters.codecs.push_back(kTelephoneEventCodec2);
parameters.codecs[0].id = 96;
parameters.codecs[2].id = 97; // narrowband CN
parameters.codecs[3].id = 98; // DTMF
SetSenderParameters(parameters);
EXPECT_TRUE(send_channel_->AddSendStream(
cricket::StreamParams::CreateLegacy(kSsrcX)));
const auto& send_codec_spec = *GetSendStreamConfig(kSsrcX).send_codec_spec;
EXPECT_EQ(96, send_codec_spec.payload_type);
EXPECT_STRCASEEQ("PCMU", send_codec_spec.format.name.c_str());
EXPECT_EQ(1u, send_codec_spec.format.num_channels);
EXPECT_EQ(97, send_codec_spec.cng_payload_type);
SetSend(true);
EXPECT_TRUE(send_channel_->CanInsertDtmf());
}
// Test that we only apply VAD if we have a CN codec that matches the
// send codec clockrate.
TEST_P(WebRtcVoiceEngineTestFake, SetSendCodecsCNNoMatch) {
EXPECT_TRUE(SetupSendStream());
cricket::AudioSenderParameter parameters;
// Set PCMU(8K) and CN(16K). VAD should not be activated.
parameters.codecs.push_back(kPcmuCodec);
parameters.codecs.push_back(kCn16000Codec);
parameters.codecs[1].id = 97;
SetSenderParameters(parameters);
{
const auto& send_codec_spec = *GetSendStreamConfig(kSsrcX).send_codec_spec;
EXPECT_STRCASEEQ("PCMU", send_codec_spec.format.name.c_str());
EXPECT_EQ(absl::nullopt, send_codec_spec.cng_payload_type);
}
// Set PCMU(8K) and CN(8K). VAD should be activated.
parameters.codecs[1] = kCn8000Codec;
SetSenderParameters(parameters);
{
const auto& send_codec_spec = *GetSendStreamConfig(kSsrcX).send_codec_spec;
EXPECT_STRCASEEQ("PCMU", send_codec_spec.format.name.c_str());
EXPECT_EQ(1u, send_codec_spec.format.num_channels);
EXPECT_EQ(13, send_codec_spec.cng_payload_type);
}
// Set OPUS(48K) and CN(8K). VAD should not be activated.
parameters.codecs[0] = kOpusCodec;
SetSenderParameters(parameters);
{
const auto& send_codec_spec = *GetSendStreamConfig(kSsrcX).send_codec_spec;
EXPECT_STRCASEEQ("OPUS", send_codec_spec.format.name.c_str());
EXPECT_EQ(absl::nullopt, send_codec_spec.cng_payload_type);
}
}
// Test that we perform case-insensitive matching of codec names.
TEST_P(WebRtcVoiceEngineTestFake, SetSendCodecsCaseInsensitive) {
EXPECT_TRUE(SetupSendStream());
cricket::AudioSenderParameter parameters;
parameters.codecs.push_back(kPcmuCodec);
parameters.codecs.push_back(kCn16000Codec);
parameters.codecs.push_back(kCn8000Codec);
parameters.codecs.push_back(kTelephoneEventCodec1);
parameters.codecs[0].name = "PcMu";
parameters.codecs[0].id = 96;
parameters.codecs[2].id = 97; // narrowband CN
parameters.codecs[3].id = 98; // DTMF
SetSenderParameters(parameters);
const auto& send_codec_spec = *GetSendStreamConfig(kSsrcX).send_codec_spec;
EXPECT_EQ(96, send_codec_spec.payload_type);
EXPECT_STRCASEEQ("PCMU", send_codec_spec.format.name.c_str());
EXPECT_EQ(1u, send_codec_spec.format.num_channels);
EXPECT_EQ(97, send_codec_spec.cng_payload_type);
SetSend(true);
EXPECT_TRUE(send_channel_->CanInsertDtmf());
}
TEST_P(WebRtcVoiceEngineTestFake,
SupportsTransportSequenceNumberHeaderExtension) {
const std::vector<webrtc::RtpExtension> header_extensions =
GetDefaultEnabledRtpHeaderExtensions(*engine_);
EXPECT_THAT(header_extensions,
Contains(::testing::Field(
"uri", &RtpExtension::uri,
webrtc::RtpExtension::kTransportSequenceNumberUri)));
}
// Test support for audio level header extension.
TEST_P(WebRtcVoiceEngineTestFake, SendAudioLevelHeaderExtensions) {
TestSetSendRtpHeaderExtensions(webrtc::RtpExtension::kAudioLevelUri);
}
TEST_P(WebRtcVoiceEngineTestFake, RecvAudioLevelHeaderExtensions) {
TestSetRecvRtpHeaderExtensions(webrtc::RtpExtension::kAudioLevelUri);
}
// Test support for transport sequence number header extension.
TEST_P(WebRtcVoiceEngineTestFake, SendTransportSequenceNumberHeaderExtensions) {
TestSetSendRtpHeaderExtensions(
webrtc::RtpExtension::kTransportSequenceNumberUri);
}
TEST_P(WebRtcVoiceEngineTestFake, RecvTransportSequenceNumberHeaderExtensions) {
TestSetRecvRtpHeaderExtensions(
webrtc::RtpExtension::kTransportSequenceNumberUri);
}
// Test that we can create a channel and start sending on it.
TEST_P(WebRtcVoiceEngineTestFake, Send) {
EXPECT_TRUE(SetupSendStream());
SetSenderParameters(send_parameters_);
SetSend(true);
EXPECT_TRUE(GetSendStream(kSsrcX).IsSending());
SetSend(false);
EXPECT_FALSE(GetSendStream(kSsrcX).IsSending());
}
// Test that a channel is muted/unmuted.
TEST_P(WebRtcVoiceEngineTestFake, SendStateMuteUnmute) {
EXPECT_TRUE(SetupSendStream());
SetSenderParameters(send_parameters_);
EXPECT_FALSE(GetSendStream(kSsrcX).muted());
SetAudioSend(kSsrcX, true, nullptr);
EXPECT_FALSE(GetSendStream(kSsrcX).muted());
SetAudioSend(kSsrcX, false, nullptr);
EXPECT_TRUE(GetSendStream(kSsrcX).muted());
}
// Test that SetSenderParameters() does not alter a stream's send state.
TEST_P(WebRtcVoiceEngineTestFake, SendStateWhenStreamsAreRecreated) {
EXPECT_TRUE(SetupSendStream());
EXPECT_FALSE(GetSendStream(kSsrcX).IsSending());
// Turn on sending.
SetSend(true);
EXPECT_TRUE(GetSendStream(kSsrcX).IsSending());
// Changing RTP header extensions will recreate the AudioSendStream.
send_parameters_.extensions.push_back(
webrtc::RtpExtension(webrtc::RtpExtension::kAudioLevelUri, 12));
SetSenderParameters(send_parameters_);
EXPECT_TRUE(GetSendStream(kSsrcX).IsSending());
// Turn off sending.
SetSend(false);
EXPECT_FALSE(GetSendStream(kSsrcX).IsSending());
// Changing RTP header extensions will recreate the AudioSendStream.
send_parameters_.extensions.clear();
SetSenderParameters(send_parameters_);
EXPECT_FALSE(GetSendStream(kSsrcX).IsSending());
}
// Test that we can create a channel and start playing out on it.
TEST_P(WebRtcVoiceEngineTestFake, Playout) {
EXPECT_TRUE(SetupRecvStream());
EXPECT_TRUE(receive_channel_->SetReceiverParameters(recv_parameters_));
receive_channel_->SetPlayout(true);
EXPECT_TRUE(GetRecvStream(kSsrcX).started());
receive_channel_->SetPlayout(false);
EXPECT_FALSE(GetRecvStream(kSsrcX).started());
}
// Test that we can add and remove send streams.
TEST_P(WebRtcVoiceEngineTestFake, CreateAndDeleteMultipleSendStreams) {
SetupForMultiSendStream();
// Set the global state for sending.
SetSend(true);
for (uint32_t ssrc : kSsrcs4) {
EXPECT_TRUE(send_channel_->AddSendStream(
cricket::StreamParams::CreateLegacy(ssrc)));
SetAudioSend(ssrc, true, &fake_source_);
// Verify that we are in a sending state for all the created streams.
EXPECT_TRUE(GetSendStream(ssrc).IsSending());
}
EXPECT_EQ(arraysize(kSsrcs4), call_.GetAudioSendStreams().size());
// Delete the send streams.
for (uint32_t ssrc : kSsrcs4) {
EXPECT_TRUE(send_channel_->RemoveSendStream(ssrc));
EXPECT_FALSE(call_.GetAudioSendStream(ssrc));
EXPECT_FALSE(send_channel_->RemoveSendStream(ssrc));
}
EXPECT_EQ(0u, call_.GetAudioSendStreams().size());
}
// Test SetSendCodecs correctly configure the codecs in all send streams.
TEST_P(WebRtcVoiceEngineTestFake, SetSendCodecsWithMultipleSendStreams) {
SetupForMultiSendStream();
// Create send streams.
for (uint32_t ssrc : kSsrcs4) {
EXPECT_TRUE(send_channel_->AddSendStream(
cricket::StreamParams::CreateLegacy(ssrc)));
}
cricket::AudioSenderParameter parameters;
// Set PCMU and CN(8K). VAD should be activated.
parameters.codecs.push_back(kPcmuCodec);
parameters.codecs.push_back(kCn8000Codec);
parameters.codecs[1].id = 97;
SetSenderParameters(parameters);
// Verify PCMU and VAD are corrected configured on all send channels.
for (uint32_t ssrc : kSsrcs4) {
ASSERT_TRUE(call_.GetAudioSendStream(ssrc) != nullptr);
const auto& send_codec_spec =
*call_.GetAudioSendStream(ssrc)->GetConfig().send_codec_spec;
EXPECT_STRCASEEQ("PCMU", send_codec_spec.format.name.c_str());
EXPECT_EQ(1u, send_codec_spec.format.num_channels);
EXPECT_EQ(97, send_codec_spec.cng_payload_type);
}
// Change to PCMU(8K) and CN(16K).
parameters.codecs[0] = kPcmuCodec;
parameters.codecs[1] = kCn16000Codec;
SetSenderParameters(parameters);
for (uint32_t ssrc : kSsrcs4) {
ASSERT_TRUE(call_.GetAudioSendStream(ssrc) != nullptr);
const auto& send_codec_spec =
*call_.GetAudioSendStream(ssrc)->GetConfig().send_codec_spec;
EXPECT_STRCASEEQ("PCMU", send_codec_spec.format.name.c_str());
EXPECT_EQ(absl::nullopt, send_codec_spec.cng_payload_type);
}
}
// Test we can SetSend on all send streams correctly.
TEST_P(WebRtcVoiceEngineTestFake, SetSendWithMultipleSendStreams) {
SetupForMultiSendStream();
// Create the send channels and they should be a "not sending" date.
for (uint32_t ssrc : kSsrcs4) {
EXPECT_TRUE(send_channel_->AddSendStream(
cricket::StreamParams::CreateLegacy(ssrc)));
SetAudioSend(ssrc, true, &fake_source_);
EXPECT_FALSE(GetSendStream(ssrc).IsSending());
}
// Set the global state for starting sending.
SetSend(true);
for (uint32_t ssrc : kSsrcs4) {
// Verify that we are in a sending state for all the send streams.
EXPECT_TRUE(GetSendStream(ssrc).IsSending());
}
// Set the global state for stopping sending.
SetSend(false);
for (uint32_t ssrc : kSsrcs4) {
// Verify that we are in a stop state for all the send streams.
EXPECT_FALSE(GetSendStream(ssrc).IsSending());
}
}
// Test we can set the correct statistics on all send streams.
TEST_P(WebRtcVoiceEngineTestFake, GetStatsWithMultipleSendStreams) {
SetupForMultiSendStream();
// Create send streams.
for (uint32_t ssrc : kSsrcs4) {
EXPECT_TRUE(send_channel_->AddSendStream(
cricket::StreamParams::CreateLegacy(ssrc)));
}
// Create a receive stream to check that none of the send streams end up in
// the receive stream stats.
EXPECT_TRUE(AddRecvStream(kSsrcY));
// We need send codec to be set to get all stats.
SetSenderParameters(send_parameters_);
EXPECT_TRUE(receive_channel_->SetReceiverParameters(recv_parameters_));
SetAudioSendStreamStats();
SetAudioReceiveStreamStats();
// Check stats for the added streams.
{
EXPECT_CALL(*adm_, GetPlayoutUnderrunCount()).WillOnce(Return(0));
cricket::VoiceMediaSendInfo send_info;
cricket::VoiceMediaReceiveInfo receive_info;
EXPECT_EQ(true, send_channel_->GetStats(&send_info));
EXPECT_EQ(true, receive_channel_->GetStats(
&receive_info, /*get_and_clear_legacy_stats=*/true));
// We have added 4 send streams. We should see empty stats for all.
EXPECT_EQ(static_cast<size_t>(arraysize(kSsrcs4)),
send_info.senders.size());
for (const auto& sender : send_info.senders) {
VerifyVoiceSenderInfo(sender, false);
}
VerifyVoiceSendRecvCodecs(send_info, receive_info);
// We have added one receive stream. We should see empty stats.
EXPECT_EQ(receive_info.receivers.size(), 1u);
EXPECT_EQ(receive_info.receivers[0].ssrc(), 123u);
}
// Remove the kSsrcY stream. No receiver stats.
{
cricket::VoiceMediaReceiveInfo receive_info;
cricket::VoiceMediaSendInfo send_info;
EXPECT_TRUE(receive_channel_->RemoveRecvStream(kSsrcY));
EXPECT_CALL(*adm_, GetPlayoutUnderrunCount()).WillOnce(Return(0));
EXPECT_EQ(true, send_channel_->GetStats(&send_info));
EXPECT_EQ(true, receive_channel_->GetStats(
&receive_info, /*get_and_clear_legacy_stats=*/true));
EXPECT_EQ(static_cast<size_t>(arraysize(kSsrcs4)),
send_info.senders.size());
EXPECT_EQ(0u, receive_info.receivers.size());
}
// Deliver a new packet - a default receive stream should be created and we
// should see stats again.
{
cricket::VoiceMediaSendInfo send_info;
cricket::VoiceMediaReceiveInfo receive_info;
DeliverPacket(kPcmuFrame, sizeof(kPcmuFrame));
SetAudioReceiveStreamStats();
EXPECT_CALL(*adm_, GetPlayoutUnderrunCount()).WillOnce(Return(0));
EXPECT_EQ(true, send_channel_->GetStats(&send_info));
EXPECT_EQ(true, receive_channel_->GetStats(
&receive_info, /*get_and_clear_legacy_stats=*/true));
EXPECT_EQ(static_cast<size_t>(arraysize(kSsrcs4)),
send_info.senders.size());
EXPECT_EQ(1u, receive_info.receivers.size());
VerifyVoiceReceiverInfo(receive_info.receivers[0]);
VerifyVoiceSendRecvCodecs(send_info, receive_info);
}
}
// Test that we can add and remove receive streams, and do proper send/playout.
// We can receive on multiple streams while sending one stream.
TEST_P(WebRtcVoiceEngineTestFake, PlayoutWithMultipleStreams) {
EXPECT_TRUE(SetupSendStream());
// Start playout without a receive stream.
SetSenderParameters(send_parameters_);
receive_channel_->SetPlayout(true);
// Adding another stream should enable playout on the new stream only.
EXPECT_TRUE(AddRecvStream(kSsrcY));
SetSend(true);
EXPECT_TRUE(GetSendStream(kSsrcX).IsSending());
// Make sure only the new stream is played out.
EXPECT_TRUE(GetRecvStream(kSsrcY).started());
// Adding yet another stream should have stream 2 and 3 enabled for playout.
EXPECT_TRUE(AddRecvStream(kSsrcZ));
EXPECT_TRUE(GetRecvStream(kSsrcY).started());
EXPECT_TRUE(GetRecvStream(kSsrcZ).started());
// Stop sending.
SetSend(false);
EXPECT_FALSE(GetSendStream(kSsrcX).IsSending());
// Stop playout.
receive_channel_->SetPlayout(false);
EXPECT_FALSE(GetRecvStream(kSsrcY).started());
EXPECT_FALSE(GetRecvStream(kSsrcZ).started());
// Restart playout and make sure recv streams are played out.
receive_channel_->SetPlayout(true);
EXPECT_TRUE(GetRecvStream(kSsrcY).started());
EXPECT_TRUE(GetRecvStream(kSsrcZ).started());
// Now remove the recv streams.
EXPECT_TRUE(receive_channel_->RemoveRecvStream(kSsrcZ));
EXPECT_TRUE(receive_channel_->RemoveRecvStream(kSsrcY));
}
TEST_P(WebRtcVoiceEngineTestFake, SetAudioNetworkAdaptorViaOptions) {
EXPECT_TRUE(SetupSendStream());
send_parameters_.options.audio_network_adaptor = true;
send_parameters_.options.audio_network_adaptor_config = {"1234"};
SetSenderParameters(send_parameters_);
EXPECT_EQ(send_parameters_.options.audio_network_adaptor_config,
GetAudioNetworkAdaptorConfig(kSsrcX));
}
TEST_P(WebRtcVoiceEngineTestFake, AudioSendResetAudioNetworkAdaptor) {
EXPECT_TRUE(SetupSendStream());
send_parameters_.options.audio_network_adaptor = true;
send_parameters_.options.audio_network_adaptor_config = {"1234"};
SetSenderParameters(send_parameters_);
EXPECT_EQ(send_parameters_.options.audio_network_adaptor_config,
GetAudioNetworkAdaptorConfig(kSsrcX));
cricket::AudioOptions options;
options.audio_network_adaptor = false;
SetAudioSend(kSsrcX, true, nullptr, &options);
EXPECT_EQ(absl::nullopt, GetAudioNetworkAdaptorConfig(kSsrcX));
}
TEST_P(WebRtcVoiceEngineTestFake, AudioNetworkAdaptorNotGetOverridden) {
EXPECT_TRUE(SetupSendStream());
send_parameters_.options.audio_network_adaptor = true;
send_parameters_.options.audio_network_adaptor_config = {"1234"};
SetSenderParameters(send_parameters_);
EXPECT_EQ(send_parameters_.options.audio_network_adaptor_config,
GetAudioNetworkAdaptorConfig(kSsrcX));
const int initial_num = call_.GetNumCreatedSendStreams();
cricket::AudioOptions options;
options.audio_network_adaptor = absl::nullopt;
// Unvalued `options.audio_network_adaptor` should not reset audio network
// adaptor.
SetAudioSend(kSsrcX, true, nullptr, &options);
// AudioSendStream not expected to be recreated.
EXPECT_EQ(initial_num, call_.GetNumCreatedSendStreams());
EXPECT_EQ(send_parameters_.options.audio_network_adaptor_config,
GetAudioNetworkAdaptorConfig(kSsrcX));
}
// Test that we can set the outgoing SSRC properly.
// SSRC is set in SetupSendStream() by calling AddSendStream.
TEST_P(WebRtcVoiceEngineTestFake, SetSendSsrc) {
EXPECT_TRUE(SetupSendStream());
EXPECT_TRUE(call_.GetAudioSendStream(kSsrcX));
}
TEST_P(WebRtcVoiceEngineTestFake, GetStats) {
// Setup. We need send codec to be set to get all stats.
EXPECT_TRUE(SetupSendStream());
// SetupSendStream adds a send stream with kSsrcX, so the receive
// stream has to use a different SSRC.
EXPECT_TRUE(AddRecvStream(kSsrcY));
SetSenderParameters(send_parameters_);
EXPECT_TRUE(receive_channel_->SetReceiverParameters(recv_parameters_));
SetAudioSendStreamStats();
// Check stats for the added streams.
{
EXPECT_CALL(*adm_, GetPlayoutUnderrunCount()).WillOnce(Return(0));
cricket::VoiceMediaSendInfo send_info;
cricket::VoiceMediaReceiveInfo receive_info;
EXPECT_EQ(true, send_channel_->GetStats(&send_info));
EXPECT_EQ(true, receive_channel_->GetStats(
&receive_info, /*get_and_clear_legacy_stats=*/true));
// We have added one send stream. We should see the stats we've set.
EXPECT_EQ(1u, send_info.senders.size());
VerifyVoiceSenderInfo(send_info.senders[0], false);
// We have added one receive stream. We should see empty stats.
EXPECT_EQ(receive_info.receivers.size(), 1u);
EXPECT_EQ(receive_info.receivers[0].ssrc(), 0u);
}
// Start sending - this affects some reported stats.
{
SetSend(true);
EXPECT_CALL(*adm_, GetPlayoutUnderrunCount()).WillOnce(Return(0));
cricket::VoiceMediaSendInfo send_info;
cricket::VoiceMediaReceiveInfo receive_info;
SetAudioReceiveStreamStats();
EXPECT_EQ(true, send_channel_->GetStats(&send_info));
EXPECT_EQ(true, receive_channel_->GetStats(
&receive_info, /*get_and_clear_legacy_stats=*/true));
VerifyVoiceSenderInfo(send_info.senders[0], true);
VerifyVoiceSendRecvCodecs(send_info, receive_info);
}
// Remove the kSsrcY stream. No receiver stats.
{
EXPECT_TRUE(receive_channel_->RemoveRecvStream(kSsrcY));
EXPECT_CALL(*adm_, GetPlayoutUnderrunCount()).WillOnce(Return(0));
cricket::VoiceMediaSendInfo send_info;
cricket::VoiceMediaReceiveInfo receive_info;
EXPECT_EQ(true, send_channel_->GetStats(&send_info));
EXPECT_EQ(true, receive_channel_->GetStats(
&receive_info, /*get_and_clear_legacy_stats=*/true));
EXPECT_EQ(1u, send_info.senders.size());
EXPECT_EQ(0u, receive_info.receivers.size());
}
// Deliver a new packet - a default receive stream should be created and we
// should see stats again.
{
DeliverPacket(kPcmuFrame, sizeof(kPcmuFrame));
SetAudioReceiveStreamStats();
EXPECT_CALL(*adm_, GetPlayoutUnderrunCount()).WillOnce(Return(0));
cricket::VoiceMediaSendInfo send_info;
cricket::VoiceMediaReceiveInfo receive_info;
EXPECT_EQ(true, send_channel_->GetStats(&send_info));
EXPECT_EQ(true, receive_channel_->GetStats(
&receive_info, /*get_and_clear_legacy_stats=*/true));
EXPECT_EQ(1u, send_info.senders.size());
EXPECT_EQ(1u, receive_info.receivers.size());
VerifyVoiceReceiverInfo(receive_info.receivers[0]);
VerifyVoiceSendRecvCodecs(send_info, receive_info);
}
}
// Test that we can set the outgoing SSRC properly with multiple streams.
// SSRC is set in SetupSendStream() by calling AddSendStream.
TEST_P(WebRtcVoiceEngineTestFake, SetSendSsrcWithMultipleStreams) {
EXPECT_TRUE(SetupSendStream());
EXPECT_TRUE(call_.GetAudioSendStream(kSsrcX));
EXPECT_TRUE(AddRecvStream(kSsrcY));
EXPECT_EQ(kSsrcX, GetRecvStreamConfig(kSsrcY).rtp.local_ssrc);
}
// Test that the local SSRC is the same on sending and receiving channels if the
// receive channel is created before the send channel.
TEST_P(WebRtcVoiceEngineTestFake, SetSendSsrcAfterCreatingReceiveChannel) {
EXPECT_TRUE(SetupChannel());
EXPECT_TRUE(AddRecvStream(kSsrcY));
EXPECT_TRUE(send_channel_->AddSendStream(
cricket::StreamParams::CreateLegacy(kSsrcX)));
EXPECT_TRUE(call_.GetAudioSendStream(kSsrcX));
EXPECT_EQ(kSsrcX, GetRecvStreamConfig(kSsrcY).rtp.local_ssrc);
}
// Test that we can properly receive packets.
TEST_P(WebRtcVoiceEngineTestFake, Recv) {
EXPECT_TRUE(SetupChannel());
EXPECT_TRUE(AddRecvStream(1));
DeliverPacket(kPcmuFrame, sizeof(kPcmuFrame));
EXPECT_TRUE(
GetRecvStream(1).VerifyLastPacket(kPcmuFrame, sizeof(kPcmuFrame)));
}
// Test that we can properly receive packets on multiple streams.
TEST_P(WebRtcVoiceEngineTestFake, RecvWithMultipleStreams) {
EXPECT_TRUE(SetupChannel());
const uint32_t ssrc1 = 1;
const uint32_t ssrc2 = 2;
const uint32_t ssrc3 = 3;
EXPECT_TRUE(AddRecvStream(ssrc1));
EXPECT_TRUE(AddRecvStream(ssrc2));
EXPECT_TRUE(AddRecvStream(ssrc3));
// Create packets with the right SSRCs.
unsigned char packets[4][sizeof(kPcmuFrame)];
for (size_t i = 0; i < arraysize(packets); ++i) {
memcpy(packets[i], kPcmuFrame, sizeof(kPcmuFrame));
rtc::SetBE32(packets[i] + 8, static_cast<uint32_t>(i));
}
const cricket::FakeAudioReceiveStream& s1 = GetRecvStream(ssrc1);
const cricket::FakeAudioReceiveStream& s2 = GetRecvStream(ssrc2);
const cricket::FakeAudioReceiveStream& s3 = GetRecvStream(ssrc3);
EXPECT_EQ(s1.received_packets(), 0);
EXPECT_EQ(s2.received_packets(), 0);
EXPECT_EQ(s3.received_packets(), 0);
DeliverPacket(packets[0], sizeof(packets[0]));
EXPECT_EQ(s1.received_packets(), 0);
EXPECT_EQ(s2.received_packets(), 0);
EXPECT_EQ(s3.received_packets(), 0);
DeliverPacket(packets[1], sizeof(packets[1]));
EXPECT_EQ(s1.received_packets(), 1);
EXPECT_TRUE(s1.VerifyLastPacket(packets[1], sizeof(packets[1])));
EXPECT_EQ(s2.received_packets(), 0);
EXPECT_EQ(s3.received_packets(), 0);
DeliverPacket(packets[2], sizeof(packets[2]));
EXPECT_EQ(s1.received_packets(), 1);
EXPECT_EQ(s2.received_packets(), 1);
EXPECT_TRUE(s2.VerifyLastPacket(packets[2], sizeof(packets[2])));
EXPECT_EQ(s3.received_packets(), 0);
DeliverPacket(packets[3], sizeof(packets[3]));
EXPECT_EQ(s1.received_packets(), 1);
EXPECT_EQ(s2.received_packets(), 1);
EXPECT_EQ(s3.received_packets(), 1);
EXPECT_TRUE(s3.VerifyLastPacket(packets[3], sizeof(packets[3])));
EXPECT_TRUE(receive_channel_->RemoveRecvStream(ssrc3));
EXPECT_TRUE(receive_channel_->RemoveRecvStream(ssrc2));
EXPECT_TRUE(receive_channel_->RemoveRecvStream(ssrc1));
}
// Test that receiving on an unsignaled stream works (a stream is created).
TEST_P(WebRtcVoiceEngineTestFake, RecvUnsignaled) {
EXPECT_TRUE(SetupChannel());
EXPECT_EQ(0u, call_.GetAudioReceiveStreams().size());
DeliverPacket(kPcmuFrame, sizeof(kPcmuFrame));
EXPECT_EQ(1u, call_.GetAudioReceiveStreams().size());
EXPECT_TRUE(
GetRecvStream(kSsrc1).VerifyLastPacket(kPcmuFrame, sizeof(kPcmuFrame)));
}
// Tests that when we add a stream without SSRCs, but contains a stream_id
// that it is stored and its stream id is later used when the first packet
// arrives to properly create a receive stream with a sync label.
TEST_P(WebRtcVoiceEngineTestFake, RecvUnsignaledSsrcWithSignaledStreamId) {
const char kSyncLabel[] = "sync_label";
EXPECT_TRUE(SetupChannel());
cricket::StreamParams unsignaled_stream;
unsignaled_stream.set_stream_ids({kSyncLabel});
ASSERT_TRUE(receive_channel_->AddRecvStream(unsignaled_stream));
// The stream shouldn't have been created at this point because it doesn't
// have any SSRCs.
EXPECT_EQ(0u, call_.GetAudioReceiveStreams().size());
DeliverPacket(kPcmuFrame, sizeof(kPcmuFrame));
EXPECT_EQ(1u, call_.GetAudioReceiveStreams().size());
EXPECT_TRUE(
GetRecvStream(kSsrc1).VerifyLastPacket(kPcmuFrame, sizeof(kPcmuFrame)));
EXPECT_EQ(kSyncLabel, GetRecvStream(kSsrc1).GetConfig().sync_group);
// Remset the unsignaled stream to clear the cached parameters. If a new
// default unsignaled receive stream is created it will not have a sync group.
receive_channel_->ResetUnsignaledRecvStream();
receive_channel_->RemoveRecvStream(kSsrc1);
DeliverPacket(kPcmuFrame, sizeof(kPcmuFrame));
EXPECT_EQ(1u, call_.GetAudioReceiveStreams().size());
EXPECT_TRUE(
GetRecvStream(kSsrc1).VerifyLastPacket(kPcmuFrame, sizeof(kPcmuFrame)));
EXPECT_TRUE(GetRecvStream(kSsrc1).GetConfig().sync_group.empty());
}
TEST_P(WebRtcVoiceEngineTestFake,
ResetUnsignaledRecvStreamDeletesAllDefaultStreams) {
ASSERT_TRUE(SetupChannel());
// No receive streams to start with.
ASSERT_TRUE(call_.GetAudioReceiveStreams().empty());
// Deliver a couple packets with unsignaled SSRCs.
unsigned char packet[sizeof(kPcmuFrame)];
memcpy(packet, kPcmuFrame, sizeof(kPcmuFrame));
rtc::SetBE32(&packet[8], 0x1234);
DeliverPacket(packet, sizeof(packet));
rtc::SetBE32(&packet[8], 0x5678);
DeliverPacket(packet, sizeof(packet));
// Verify that the receive streams were created.
const auto& receivers1 = call_.GetAudioReceiveStreams();
ASSERT_EQ(receivers1.size(), 2u);
// Should remove all default streams.
receive_channel_->ResetUnsignaledRecvStream();
const auto& receivers2 = call_.GetAudioReceiveStreams();
EXPECT_EQ(0u, receivers2.size());
}
// Test that receiving N unsignaled stream works (streams will be created), and
// that packets are forwarded to them all.
TEST_P(WebRtcVoiceEngineTestFake, RecvMultipleUnsignaled) {
EXPECT_TRUE(SetupChannel());
unsigned char packet[sizeof(kPcmuFrame)];
memcpy(packet, kPcmuFrame, sizeof(kPcmuFrame));
// Note that SSRC = 0 is not supported.
for (uint32_t ssrc = 1; ssrc < (1 + kMaxUnsignaledRecvStreams); ++ssrc) {
rtc::SetBE32(&packet[8], ssrc);
DeliverPacket(packet, sizeof(packet));
// Verify we have one new stream for each loop iteration.
EXPECT_EQ(ssrc, call_.GetAudioReceiveStreams().size());
EXPECT_EQ(1, GetRecvStream(ssrc).received_packets());
EXPECT_TRUE(GetRecvStream(ssrc).VerifyLastPacket(packet, sizeof(packet)));
}
// Sending on the same SSRCs again should not create new streams.
for (uint32_t ssrc = 1; ssrc < (1 + kMaxUnsignaledRecvStreams); ++ssrc) {
rtc::SetBE32(&packet[8], ssrc);
DeliverPacket(packet, sizeof(packet));
EXPECT_EQ(kMaxUnsignaledRecvStreams, call_.GetAudioReceiveStreams().size());
EXPECT_EQ(2, GetRecvStream(ssrc).received_packets());
EXPECT_TRUE(GetRecvStream(ssrc).VerifyLastPacket(packet, sizeof(packet)));
}
// Send on another SSRC, the oldest unsignaled stream (SSRC=1) is replaced.
constexpr uint32_t kAnotherSsrc = 667;
rtc::SetBE32(&packet[8], kAnotherSsrc);
DeliverPacket(packet, sizeof(packet));
const auto& streams = call_.GetAudioReceiveStreams();
EXPECT_EQ(kMaxUnsignaledRecvStreams, streams.size());
size_t i = 0;
for (uint32_t ssrc = 2; ssrc < (1 + kMaxUnsignaledRecvStreams); ++ssrc, ++i) {
EXPECT_EQ(ssrc, streams[i]->GetConfig().rtp.remote_ssrc);
EXPECT_EQ(2, streams[i]->received_packets());
}
EXPECT_EQ(kAnotherSsrc, streams[i]->GetConfig().rtp.remote_ssrc);
EXPECT_EQ(1, streams[i]->received_packets());
// Sanity check that we've checked all streams.
EXPECT_EQ(kMaxUnsignaledRecvStreams, (i + 1));
}
// Test that a default channel is created even after a signaled stream has been
// added, and that this stream will get any packets for unknown SSRCs.
TEST_P(WebRtcVoiceEngineTestFake, RecvUnsignaledAfterSignaled) {
EXPECT_TRUE(SetupChannel());
unsigned char packet[sizeof(kPcmuFrame)];
memcpy(packet, kPcmuFrame, sizeof(kPcmuFrame));
// Add a known stream, send packet and verify we got it.
const uint32_t signaled_ssrc = 1;
rtc::SetBE32(&packet[8], signaled_ssrc);
EXPECT_TRUE(AddRecvStream(signaled_ssrc));
DeliverPacket(packet, sizeof(packet));
EXPECT_TRUE(
GetRecvStream(signaled_ssrc).VerifyLastPacket(packet, sizeof(packet)));
EXPECT_EQ(1u, call_.GetAudioReceiveStreams().size());
// Note that the first unknown SSRC cannot be 0, because we only support
// creating receive streams for SSRC!=0.
const uint32_t unsignaled_ssrc = 7011;
rtc::SetBE32(&packet[8], unsignaled_ssrc);
DeliverPacket(packet, sizeof(packet));
EXPECT_TRUE(
GetRecvStream(unsignaled_ssrc).VerifyLastPacket(packet, sizeof(packet)));
EXPECT_EQ(2u, call_.GetAudioReceiveStreams().size());
DeliverPacket(packet, sizeof(packet));
EXPECT_EQ(2, GetRecvStream(unsignaled_ssrc).received_packets());
rtc::SetBE32(&packet[8], signaled_ssrc);
DeliverPacket(packet, sizeof(packet));
EXPECT_EQ(2, GetRecvStream(signaled_ssrc).received_packets());
EXPECT_EQ(2u, call_.GetAudioReceiveStreams().size());
}
// Two tests to verify that adding a receive stream with the same SSRC as a
// previously added unsignaled stream will only recreate underlying stream
// objects if the stream parameters have changed.
TEST_P(WebRtcVoiceEngineTestFake, AddRecvStreamAfterUnsignaled_NoRecreate) {
EXPECT_TRUE(SetupChannel());
// Spawn unsignaled stream with SSRC=1.
DeliverPacket(kPcmuFrame, sizeof(kPcmuFrame));
EXPECT_EQ(1u, call_.GetAudioReceiveStreams().size());
EXPECT_TRUE(
GetRecvStream(1).VerifyLastPacket(kPcmuFrame, sizeof(kPcmuFrame)));
// Verify that the underlying stream object in Call is not recreated when a
// stream with SSRC=1 is added.
const auto& streams = call_.GetAudioReceiveStreams();
EXPECT_EQ(1u, streams.size());
int audio_receive_stream_id = streams.front()->id();
EXPECT_TRUE(AddRecvStream(1));
EXPECT_EQ(1u, streams.size());
EXPECT_EQ(audio_receive_stream_id, streams.front()->id());
}
TEST_P(WebRtcVoiceEngineTestFake, AddRecvStreamAfterUnsignaled_Updates) {
EXPECT_TRUE(SetupChannel());
// Spawn unsignaled stream with SSRC=1.
DeliverPacket(kPcmuFrame, sizeof(kPcmuFrame));
EXPECT_EQ(1u, call_.GetAudioReceiveStreams().size());
EXPECT_TRUE(
GetRecvStream(1).VerifyLastPacket(kPcmuFrame, sizeof(kPcmuFrame)));
// Verify that the underlying stream object in Call gets updated when a
// stream with SSRC=1 is added, and which has changed stream parameters.
const auto& streams = call_.GetAudioReceiveStreams();
EXPECT_EQ(1u, streams.size());
// The sync_group id should be empty.
EXPECT_TRUE(streams.front()->GetConfig().sync_group.empty());
const std::string new_stream_id("stream_id");
int audio_receive_stream_id = streams.front()->id();
cricket::StreamParams stream_params;
stream_params.ssrcs.push_back(1);
stream_params.set_stream_ids({new_stream_id});
EXPECT_TRUE(receive_channel_->AddRecvStream(stream_params));
EXPECT_EQ(1u, streams.size());
// The audio receive stream should not have been recreated.
EXPECT_EQ(audio_receive_stream_id, streams.front()->id());
// The sync_group id should now match with the new stream params.
EXPECT_EQ(new_stream_id, streams.front()->GetConfig().sync_group);
}
// Test that AddRecvStream creates new stream.
TEST_P(WebRtcVoiceEngineTestFake, AddRecvStream) {
EXPECT_TRUE(SetupRecvStream());
EXPECT_TRUE(AddRecvStream(1));
}
// Test that after adding a recv stream, we do not decode more codecs than
// those previously passed into SetRecvCodecs.
TEST_P(WebRtcVoiceEngineTestFake, AddRecvStreamUnsupportedCodec) {
EXPECT_TRUE(SetupSendStream());
cricket::AudioReceiverParameters parameters;
parameters.codecs.push_back(kOpusCodec);
parameters.codecs.push_back(kPcmuCodec);
EXPECT_TRUE(receive_channel_->SetReceiverParameters(parameters));
EXPECT_TRUE(AddRecvStream(kSsrcX));
EXPECT_THAT(GetRecvStreamConfig(kSsrcX).decoder_map,
(ContainerEq<std::map<int, webrtc::SdpAudioFormat>>(
{{0, {"PCMU", 8000, 1}}, {111, {"OPUS", 48000, 2}}})));
}
// Test that we properly clean up any streams that were added, even if
// not explicitly removed.
TEST_P(WebRtcVoiceEngineTestFake, StreamCleanup) {
EXPECT_TRUE(SetupSendStream());
SetSenderParameters(send_parameters_);
EXPECT_TRUE(AddRecvStream(1));
EXPECT_TRUE(AddRecvStream(2));
EXPECT_EQ(1u, call_.GetAudioSendStreams().size());
EXPECT_EQ(2u, call_.GetAudioReceiveStreams().size());
send_channel_.reset();
receive_channel_.reset();
EXPECT_EQ(0u, call_.GetAudioSendStreams().size());
EXPECT_EQ(0u, call_.GetAudioReceiveStreams().size());
}
TEST_P(WebRtcVoiceEngineTestFake, TestAddRecvStreamSuccessWithZeroSsrc) {
EXPECT_TRUE(SetupSendStream());
EXPECT_TRUE(AddRecvStream(0));
}
TEST_P(WebRtcVoiceEngineTestFake, TestAddRecvStreamFailWithSameSsrc) {
EXPECT_TRUE(SetupChannel());
EXPECT_TRUE(AddRecvStream(1));
EXPECT_FALSE(AddRecvStream(1));
}
// Test the InsertDtmf on default send stream as caller.
TEST_P(WebRtcVoiceEngineTestFake, InsertDtmfOnDefaultSendStreamAsCaller) {
TestInsertDtmf(0, true, kTelephoneEventCodec1);
}
// Test the InsertDtmf on default send stream as callee
TEST_P(WebRtcVoiceEngineTestFake, InsertDtmfOnDefaultSendStreamAsCallee) {
TestInsertDtmf(0, false, kTelephoneEventCodec2);
}
// Test the InsertDtmf on specified send stream as caller.
TEST_P(WebRtcVoiceEngineTestFake, InsertDtmfOnSendStreamAsCaller) {
TestInsertDtmf(kSsrcX, true, kTelephoneEventCodec2);
}
// Test the InsertDtmf on specified send stream as callee.
TEST_P(WebRtcVoiceEngineTestFake, InsertDtmfOnSendStreamAsCallee) {
TestInsertDtmf(kSsrcX, false, kTelephoneEventCodec1);
}
// Test propagation of extmap allow mixed setting.
TEST_P(WebRtcVoiceEngineTestFake, SetExtmapAllowMixedAsCaller) {
TestExtmapAllowMixedCaller(/*extmap_allow_mixed=*/true);
}
TEST_P(WebRtcVoiceEngineTestFake, SetExtmapAllowMixedDisabledAsCaller) {
TestExtmapAllowMixedCaller(/*extmap_allow_mixed=*/false);
}
TEST_P(WebRtcVoiceEngineTestFake, SetExtmapAllowMixedAsCallee) {
TestExtmapAllowMixedCallee(/*extmap_allow_mixed=*/true);
}
TEST_P(WebRtcVoiceEngineTestFake, SetExtmapAllowMixedDisabledAsCallee) {
TestExtmapAllowMixedCallee(/*extmap_allow_mixed=*/false);
}
TEST_P(WebRtcVoiceEngineTestFake, SetAudioOptions) {
EXPECT_TRUE(SetupSendStream());
EXPECT_TRUE(AddRecvStream(kSsrcY));
EXPECT_CALL(*adm_, BuiltInAECIsAvailable())
.Times(8)
.WillRepeatedly(Return(false));
EXPECT_CALL(*adm_, BuiltInAGCIsAvailable())
.Times(4)
.WillRepeatedly(Return(false));
EXPECT_CALL(*adm_, BuiltInNSIsAvailable())
.Times(2)
.WillRepeatedly(Return(false));
EXPECT_EQ(200u, GetRecvStreamConfig(kSsrcY).jitter_buffer_max_packets);
EXPECT_FALSE(GetRecvStreamConfig(kSsrcY).jitter_buffer_fast_accelerate);
// Nothing set in AudioOptions, so everything should be as default.
send_parameters_.options = cricket::AudioOptions();
SetSenderParameters(send_parameters_);
if (!use_null_apm_) {
VerifyEchoCancellationSettings(/*enabled=*/true);
EXPECT_TRUE(IsHighPassFilterEnabled());
}
EXPECT_EQ(200u, GetRecvStreamConfig(kSsrcY).jitter_buffer_max_packets);
EXPECT_FALSE(GetRecvStreamConfig(kSsrcY).jitter_buffer_fast_accelerate);
// Turn echo cancellation off
send_parameters_.options.echo_cancellation = false;
SetSenderParameters(send_parameters_);
if (!use_null_apm_) {
VerifyEchoCancellationSettings(/*enabled=*/false);
}
// Turn echo cancellation back on, with settings, and make sure
// nothing else changed.
send_parameters_.options.echo_cancellation = true;
SetSenderParameters(send_parameters_);
if (!use_null_apm_) {
VerifyEchoCancellationSettings(/*enabled=*/true);
}
// Turn off echo cancellation and delay agnostic aec.
send_parameters_.options.echo_cancellation = false;
SetSenderParameters(send_parameters_);
if (!use_null_apm_) {
VerifyEchoCancellationSettings(/*enabled=*/false);
}
// Restore AEC to be on to work with the following tests.
send_parameters_.options.echo_cancellation = true;
SetSenderParameters(send_parameters_);
// Turn off AGC
send_parameters_.options.auto_gain_control = false;
SetSenderParameters(send_parameters_);
if (!use_null_apm_) {
VerifyEchoCancellationSettings(/*enabled=*/true);
EXPECT_FALSE(apm_config_.gain_controller1.enabled);
}
// Turn AGC back on
send_parameters_.options.auto_gain_control = true;
SetSenderParameters(send_parameters_);
if (!use_null_apm_) {
VerifyEchoCancellationSettings(/*enabled=*/true);
EXPECT_TRUE(apm_config_.gain_controller1.enabled);
}
// Turn off other options.
send_parameters_.options.noise_suppression = false;
send_parameters_.options.highpass_filter = false;
send_parameters_.options.stereo_swapping = true;
SetSenderParameters(send_parameters_);
if (!use_null_apm_) {
VerifyEchoCancellationSettings(/*enabled=*/true);
EXPECT_FALSE(IsHighPassFilterEnabled());
EXPECT_TRUE(apm_config_.gain_controller1.enabled);
EXPECT_FALSE(apm_config_.noise_suppression.enabled);
EXPECT_EQ(apm_config_.noise_suppression.level, kDefaultNsLevel);
}
// Set options again to ensure it has no impact.
SetSenderParameters(send_parameters_);
if (!use_null_apm_) {
VerifyEchoCancellationSettings(/*enabled=*/true);
EXPECT_TRUE(apm_config_.gain_controller1.enabled);
EXPECT_FALSE(apm_config_.noise_suppression.enabled);
EXPECT_EQ(apm_config_.noise_suppression.level, kDefaultNsLevel);
}
}
TEST_P(WebRtcVoiceEngineTestFake, InitRecordingOnSend) {
EXPECT_CALL(*adm_, RecordingIsInitialized()).WillOnce(Return(false));
EXPECT_CALL(*adm_, Recording()).WillOnce(Return(false));
EXPECT_CALL(*adm_, InitRecording()).Times(1);
std::unique_ptr<cricket::VoiceMediaSendChannelInterface> send_channel(
engine_->CreateSendChannel(
&call_, cricket::MediaConfig(), cricket::AudioOptions(),
webrtc::CryptoOptions(), webrtc::AudioCodecPairId::Create()));
send_channel->SetSend(true);
}
TEST_P(WebRtcVoiceEngineTestFake, SkipInitRecordingOnSend) {
EXPECT_CALL(*adm_, RecordingIsInitialized()).Times(0);
EXPECT_CALL(*adm_, Recording()).Times(0);
EXPECT_CALL(*adm_, InitRecording()).Times(0);
cricket::AudioOptions options;
options.init_recording_on_send = false;
std::unique_ptr<cricket::VoiceMediaSendChannelInterface> send_channel(
engine_->CreateSendChannel(&call_, cricket::MediaConfig(), options,
webrtc::CryptoOptions(),
webrtc::AudioCodecPairId::Create()));
send_channel->SetSend(true);
}
TEST_P(WebRtcVoiceEngineTestFake, SetOptionOverridesViaChannels) {
EXPECT_TRUE(SetupSendStream());
EXPECT_CALL(*adm_, BuiltInAECIsAvailable())
.Times(use_null_apm_ ? 4 : 8)
.WillRepeatedly(Return(false));
EXPECT_CALL(*adm_, BuiltInAGCIsAvailable())
.Times(use_null_apm_ ? 7 : 8)
.WillRepeatedly(Return(false));
EXPECT_CALL(*adm_, BuiltInNSIsAvailable())
.Times(use_null_apm_ ? 5 : 8)
.WillRepeatedly(Return(false));
EXPECT_CALL(*adm_, RecordingIsInitialized())
.Times(2)
.WillRepeatedly(Return(false));
EXPECT_CALL(*adm_, Recording()).Times(2).WillRepeatedly(Return(false));
EXPECT_CALL(*adm_, InitRecording()).Times(2).WillRepeatedly(Return(0));
std::unique_ptr<cricket::VoiceMediaSendChannelInterface> send_channel1(
engine_->CreateSendChannel(
&call_, cricket::MediaConfig(), cricket::AudioOptions(),
webrtc::CryptoOptions(), webrtc::AudioCodecPairId::Create()));
std::unique_ptr<cricket::VoiceMediaSendChannelInterface> send_channel2(
engine_->CreateSendChannel(
&call_, cricket::MediaConfig(), cricket::AudioOptions(),
webrtc::CryptoOptions(), webrtc::AudioCodecPairId::Create()));
// Have to add a stream to make SetSend work.
cricket::StreamParams stream1;
stream1.ssrcs.push_back(1);
send_channel1->AddSendStream(stream1);
cricket::StreamParams stream2;
stream2.ssrcs.push_back(2);
send_channel2->AddSendStream(stream2);
// AEC and AGC and NS
cricket::AudioSenderParameter parameters_options_all = send_parameters_;
parameters_options_all.options.echo_cancellation = true;
parameters_options_all.options.auto_gain_control = true;
parameters_options_all.options.noise_suppression = true;
EXPECT_TRUE(send_channel1->SetSenderParameters(parameters_options_all));
if (!use_null_apm_) {
VerifyEchoCancellationSettings(/*enabled=*/true);
VerifyGainControlEnabledCorrectly();
EXPECT_TRUE(apm_config_.noise_suppression.enabled);
EXPECT_EQ(apm_config_.noise_suppression.level, kDefaultNsLevel);
EXPECT_EQ(parameters_options_all.options,
SendImplFromPointer(send_channel1.get())->options());
EXPECT_TRUE(send_channel2->SetSenderParameters(parameters_options_all));
VerifyEchoCancellationSettings(/*enabled=*/true);
VerifyGainControlEnabledCorrectly();
EXPECT_EQ(parameters_options_all.options,
SendImplFromPointer(send_channel2.get())->options());
}
// unset NS
cricket::AudioSenderParameter parameters_options_no_ns = send_parameters_;
parameters_options_no_ns.options.noise_suppression = false;
EXPECT_TRUE(send_channel1->SetSenderParameters(parameters_options_no_ns));
cricket::AudioOptions expected_options = parameters_options_all.options;
if (!use_null_apm_) {
VerifyEchoCancellationSettings(/*enabled=*/true);
EXPECT_FALSE(apm_config_.noise_suppression.enabled);
EXPECT_EQ(apm_config_.noise_suppression.level, kDefaultNsLevel);
VerifyGainControlEnabledCorrectly();
expected_options.echo_cancellation = true;
expected_options.auto_gain_control = true;
expected_options.noise_suppression = false;
EXPECT_EQ(expected_options,
SendImplFromPointer(send_channel1.get())->options());
}
// unset AGC
cricket::AudioSenderParameter parameters_options_no_agc = send_parameters_;
parameters_options_no_agc.options.auto_gain_control = false;
EXPECT_TRUE(send_channel2->SetSenderParameters(parameters_options_no_agc));
if (!use_null_apm_) {
VerifyEchoCancellationSettings(/*enabled=*/true);
EXPECT_FALSE(apm_config_.gain_controller1.enabled);
EXPECT_TRUE(apm_config_.noise_suppression.enabled);
EXPECT_EQ(apm_config_.noise_suppression.level, kDefaultNsLevel);
expected_options.echo_cancellation = true;
expected_options.auto_gain_control = false;
expected_options.noise_suppression = true;
EXPECT_EQ(expected_options,
SendImplFromPointer(send_channel2.get())->options());
}
EXPECT_TRUE(send_channel_->SetSenderParameters(parameters_options_all));
if (!use_null_apm_) {
VerifyEchoCancellationSettings(/*enabled=*/true);
VerifyGainControlEnabledCorrectly();
EXPECT_TRUE(apm_config_.noise_suppression.enabled);
EXPECT_EQ(apm_config_.noise_suppression.level, kDefaultNsLevel);
}
send_channel1->SetSend(true);
if (!use_null_apm_) {
VerifyEchoCancellationSettings(/*enabled=*/true);
VerifyGainControlEnabledCorrectly();
EXPECT_FALSE(apm_config_.noise_suppression.enabled);
EXPECT_EQ(apm_config_.noise_suppression.level, kDefaultNsLevel);
}
send_channel2->SetSend(true);
if (!use_null_apm_) {
VerifyEchoCancellationSettings(/*enabled=*/true);
EXPECT_FALSE(apm_config_.gain_controller1.enabled);
EXPECT_TRUE(apm_config_.noise_suppression.enabled);
EXPECT_EQ(apm_config_.noise_suppression.level, kDefaultNsLevel);
}
// Make sure settings take effect while we are sending.
cricket::AudioSenderParameter parameters_options_no_agc_nor_ns =
send_parameters_;
parameters_options_no_agc_nor_ns.options.auto_gain_control = false;
parameters_options_no_agc_nor_ns.options.noise_suppression = false;
EXPECT_TRUE(
send_channel2->SetSenderParameters(parameters_options_no_agc_nor_ns));
if (!use_null_apm_) {
VerifyEchoCancellationSettings(/*enabled=*/true);
EXPECT_FALSE(apm_config_.gain_controller1.enabled);
EXPECT_FALSE(apm_config_.noise_suppression.enabled);
EXPECT_EQ(apm_config_.noise_suppression.level, kDefaultNsLevel);
expected_options.echo_cancellation = true;
expected_options.auto_gain_control = false;
expected_options.noise_suppression = false;
EXPECT_EQ(expected_options,
SendImplFromPointer(send_channel2.get())->options());
}
}
// This test verifies DSCP settings are properly applied on voice media channel.
TEST_P(WebRtcVoiceEngineTestFake, TestSetDscpOptions) {
EXPECT_TRUE(SetupSendStream());
cricket::FakeNetworkInterface network_interface;
cricket::MediaConfig config;
std::unique_ptr<cricket::VoiceMediaSendChannelInterface> channel;
webrtc::RtpParameters parameters;
channel = engine_->CreateSendChannel(&call_, config, cricket::AudioOptions(),
webrtc::CryptoOptions(),
webrtc::AudioCodecPairId::Create());
channel->SetInterface(&network_interface);
// Default value when DSCP is disabled should be DSCP_DEFAULT.
EXPECT_EQ(rtc::DSCP_DEFAULT, network_interface.dscp());
channel->SetInterface(nullptr);
config.enable_dscp = true;
channel = engine_->CreateSendChannel(&call_, config, cricket::AudioOptions(),
webrtc::CryptoOptions(),
webrtc::AudioCodecPairId::Create());
channel->SetInterface(&network_interface);
EXPECT_EQ(rtc::DSCP_DEFAULT, network_interface.dscp());
// Create a send stream to configure
EXPECT_TRUE(
channel->AddSendStream(cricket::StreamParams::CreateLegacy(kSsrcZ)));
parameters = channel->GetRtpSendParameters(kSsrcZ);
ASSERT_FALSE(parameters.encodings.empty());
// Various priorities map to various dscp values.
parameters.encodings[0].network_priority = webrtc::Priority::kHigh;
ASSERT_TRUE(channel->SetRtpSendParameters(kSsrcZ, parameters, nullptr).ok());
EXPECT_EQ(rtc::DSCP_EF, network_interface.dscp());
parameters.encodings[0].network_priority = webrtc::Priority::kVeryLow;
ASSERT_TRUE(channel->SetRtpSendParameters(kSsrcZ, parameters, nullptr).ok());
EXPECT_EQ(rtc::DSCP_CS1, network_interface.dscp());
// Packets should also self-identify their dscp in PacketOptions.
const uint8_t kData[10] = {0};
EXPECT_TRUE(SendImplFromPointer(channel.get())->transport()->SendRtcp(kData));
EXPECT_EQ(rtc::DSCP_CS1, network_interface.options().dscp);
channel->SetInterface(nullptr);
// Verify that setting the option to false resets the
// DiffServCodePoint.
config.enable_dscp = false;
channel = engine_->CreateSendChannel(&call_, config, cricket::AudioOptions(),
webrtc::CryptoOptions(),
webrtc::AudioCodecPairId::Create());
channel->SetInterface(&network_interface);
// Default value when DSCP is disabled should be DSCP_DEFAULT.
EXPECT_EQ(rtc::DSCP_DEFAULT, network_interface.dscp());
channel->SetInterface(nullptr);
}
TEST_P(WebRtcVoiceEngineTestFake, SetOutputVolume) {
EXPECT_TRUE(SetupChannel());
EXPECT_FALSE(receive_channel_->SetOutputVolume(kSsrcY, 0.5));
cricket::StreamParams stream;
stream.ssrcs.push_back(kSsrcY);
EXPECT_TRUE(receive_channel_->AddRecvStream(stream));
EXPECT_DOUBLE_EQ(1, GetRecvStream(kSsrcY).gain());
EXPECT_TRUE(receive_channel_->SetOutputVolume(kSsrcY, 3));
EXPECT_DOUBLE_EQ(3, GetRecvStream(kSsrcY).gain());
}
TEST_P(WebRtcVoiceEngineTestFake, SetOutputVolumeUnsignaledRecvStream) {
EXPECT_TRUE(SetupChannel());
// Spawn an unsignaled stream by sending a packet - gain should be 1.
DeliverPacket(kPcmuFrame, sizeof(kPcmuFrame));
EXPECT_DOUBLE_EQ(1, GetRecvStream(kSsrc1).gain());
// Should remember the volume "2" which will be set on new unsignaled streams,
// and also set the gain to 2 on existing unsignaled streams.
EXPECT_TRUE(receive_channel_->SetDefaultOutputVolume(2));
EXPECT_DOUBLE_EQ(2, GetRecvStream(kSsrc1).gain());
// Spawn an unsignaled stream by sending a packet - gain should be 2.
unsigned char pcmuFrame2[sizeof(kPcmuFrame)];
memcpy(pcmuFrame2, kPcmuFrame, sizeof(kPcmuFrame));
rtc::SetBE32(&pcmuFrame2[8], kSsrcX);
DeliverPacket(pcmuFrame2, sizeof(pcmuFrame2));
EXPECT_DOUBLE_EQ(2, GetRecvStream(kSsrcX).gain());
// Setting gain for all unsignaled streams.
EXPECT_TRUE(receive_channel_->SetDefaultOutputVolume(3));
if (kMaxUnsignaledRecvStreams > 1) {
EXPECT_DOUBLE_EQ(3, GetRecvStream(kSsrc1).gain());
}
EXPECT_DOUBLE_EQ(3, GetRecvStream(kSsrcX).gain());
// Setting gain on an individual stream affects only that.
EXPECT_TRUE(receive_channel_->SetOutputVolume(kSsrcX, 4));
if (kMaxUnsignaledRecvStreams > 1) {
EXPECT_DOUBLE_EQ(3, GetRecvStream(kSsrc1).gain());
}
EXPECT_DOUBLE_EQ(4, GetRecvStream(kSsrcX).gain());
}
TEST_P(WebRtcVoiceEngineTestFake, BaseMinimumPlayoutDelayMs) {
EXPECT_TRUE(SetupChannel());
EXPECT_FALSE(receive_channel_->SetBaseMinimumPlayoutDelayMs(kSsrcY, 200));
EXPECT_FALSE(
receive_channel_->GetBaseMinimumPlayoutDelayMs(kSsrcY).has_value());
cricket::StreamParams stream;
stream.ssrcs.push_back(kSsrcY);
EXPECT_TRUE(receive_channel_->AddRecvStream(stream));
EXPECT_EQ(0, GetRecvStream(kSsrcY).base_mininum_playout_delay_ms());
EXPECT_TRUE(receive_channel_->SetBaseMinimumPlayoutDelayMs(kSsrcY, 300));
EXPECT_EQ(300, GetRecvStream(kSsrcY).base_mininum_playout_delay_ms());
}
TEST_P(WebRtcVoiceEngineTestFake,
BaseMinimumPlayoutDelayMsUnsignaledRecvStream) {
// Here base minimum delay is abbreviated to delay in comments for shortness.
EXPECT_TRUE(SetupChannel());
// Spawn an unsignaled stream by sending a packet - delay should be 0.
DeliverPacket(kPcmuFrame, sizeof(kPcmuFrame));
EXPECT_EQ(
0, receive_channel_->GetBaseMinimumPlayoutDelayMs(kSsrc1).value_or(-1));
// Check that it doesn't provide default values for unknown ssrc.
EXPECT_FALSE(
receive_channel_->GetBaseMinimumPlayoutDelayMs(kSsrcY).has_value());
// Check that default value for unsignaled streams is 0.
EXPECT_EQ(
0, receive_channel_->GetBaseMinimumPlayoutDelayMs(kSsrc0).value_or(-1));
// Should remember the delay 100 which will be set on new unsignaled streams,
// and also set the delay to 100 on existing unsignaled streams.
EXPECT_TRUE(receive_channel_->SetBaseMinimumPlayoutDelayMs(kSsrc0, 100));
EXPECT_EQ(
100, receive_channel_->GetBaseMinimumPlayoutDelayMs(kSsrc0).value_or(-1));
// Check that it doesn't provide default values for unknown ssrc.
EXPECT_FALSE(
receive_channel_->GetBaseMinimumPlayoutDelayMs(kSsrcY).has_value());
// Spawn an unsignaled stream by sending a packet - delay should be 100.
unsigned char pcmuFrame2[sizeof(kPcmuFrame)];
memcpy(pcmuFrame2, kPcmuFrame, sizeof(kPcmuFrame));
rtc::SetBE32(&pcmuFrame2[8], kSsrcX);
DeliverPacket(pcmuFrame2, sizeof(pcmuFrame2));
EXPECT_EQ(
100, receive_channel_->GetBaseMinimumPlayoutDelayMs(kSsrcX).value_or(-1));
// Setting delay with SSRC=0 should affect all unsignaled streams.
EXPECT_TRUE(receive_channel_->SetBaseMinimumPlayoutDelayMs(kSsrc0, 300));
if (kMaxUnsignaledRecvStreams > 1) {
EXPECT_EQ(
300,
receive_channel_->GetBaseMinimumPlayoutDelayMs(kSsrc1).value_or(-1));
}
EXPECT_EQ(
300, receive_channel_->GetBaseMinimumPlayoutDelayMs(kSsrcX).value_or(-1));
// Setting delay on an individual stream affects only that.
EXPECT_TRUE(receive_channel_->SetBaseMinimumPlayoutDelayMs(kSsrcX, 400));
if (kMaxUnsignaledRecvStreams > 1) {
EXPECT_EQ(
300,
receive_channel_->GetBaseMinimumPlayoutDelayMs(kSsrc1).value_or(-1));
}
EXPECT_EQ(
400, receive_channel_->GetBaseMinimumPlayoutDelayMs(kSsrcX).value_or(-1));
EXPECT_EQ(
300, receive_channel_->GetBaseMinimumPlayoutDelayMs(kSsrc0).value_or(-1));
// Check that it doesn't provide default values for unknown ssrc.
EXPECT_FALSE(
receive_channel_->GetBaseMinimumPlayoutDelayMs(kSsrcY).has_value());
}
TEST_P(WebRtcVoiceEngineTestFake, SetsSyncGroupFromStreamId) {
const uint32_t kAudioSsrc = 123;
const std::string kStreamId = "AvSyncLabel";
EXPECT_TRUE(SetupSendStream());
cricket::StreamParams sp = cricket::StreamParams::CreateLegacy(kAudioSsrc);
sp.set_stream_ids({kStreamId});
// Creating two channels to make sure that sync label is set properly for both
// the default voice channel and following ones.
EXPECT_TRUE(receive_channel_->AddRecvStream(sp));
sp.ssrcs[0] += 1;
EXPECT_TRUE(receive_channel_->AddRecvStream(sp));
ASSERT_EQ(2u, call_.GetAudioReceiveStreams().size());
EXPECT_EQ(kStreamId,
call_.GetAudioReceiveStream(kAudioSsrc)->GetConfig().sync_group)
<< "SyncGroup should be set based on stream id";
EXPECT_EQ(kStreamId,
call_.GetAudioReceiveStream(kAudioSsrc + 1)->GetConfig().sync_group)
<< "SyncGroup should be set based on stream id";
}
// TODO(solenberg): Remove, once recv streams are configured through Call.
// (This is then covered by TestSetRecvRtpHeaderExtensions.)
TEST_P(WebRtcVoiceEngineTestFake, ConfiguresAudioReceiveStreamRtpExtensions) {
// Test that setting the header extensions results in the expected state
// changes on an associated Call.
std::vector<uint32_t> ssrcs;
ssrcs.push_back(223);
ssrcs.push_back(224);
EXPECT_TRUE(SetupSendStream());
SetSenderParameters(send_parameters_);
for (uint32_t ssrc : ssrcs) {
EXPECT_TRUE(receive_channel_->AddRecvStream(
cricket::StreamParams::CreateLegacy(ssrc)));
}
EXPECT_EQ(2u, call_.GetAudioReceiveStreams().size());
for (uint32_t ssrc : ssrcs) {
EXPECT_THAT(
receive_channel_->GetRtpReceiverParameters(ssrc).header_extensions,
IsEmpty());
}
// Set up receive extensions.
const std::vector<webrtc::RtpExtension> header_extensions =
GetDefaultEnabledRtpHeaderExtensions(*engine_);
cricket::AudioReceiverParameters recv_parameters;
recv_parameters.extensions = header_extensions;
receive_channel_->SetReceiverParameters(recv_parameters);
EXPECT_EQ(2u, call_.GetAudioReceiveStreams().size());
for (uint32_t ssrc : ssrcs) {
EXPECT_THAT(
receive_channel_->GetRtpReceiverParameters(ssrc).header_extensions,
testing::UnorderedElementsAreArray(header_extensions));
}
// Disable receive extensions.
receive_channel_->SetReceiverParameters(cricket::AudioReceiverParameters());
for (uint32_t ssrc : ssrcs) {
EXPECT_THAT(
receive_channel_->GetRtpReceiverParameters(ssrc).header_extensions,
IsEmpty());
}
}
TEST_P(WebRtcVoiceEngineTestFake, DeliverAudioPacket_Call) {
// Test that packets are forwarded to the Call when configured accordingly.
const uint32_t kAudioSsrc = 1;
rtc::CopyOnWriteBuffer kPcmuPacket(kPcmuFrame, sizeof(kPcmuFrame));
static const unsigned char kRtcp[] = {
0x80, 0xc9, 0x00, 0x01, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00,
0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
rtc::CopyOnWriteBuffer kRtcpPacket(kRtcp, sizeof(kRtcp));
EXPECT_TRUE(SetupSendStream());
cricket::VoiceMediaReceiveChannelInterface* media_channel = ReceiveImpl();
SetSenderParameters(send_parameters_);
EXPECT_TRUE(media_channel->AddRecvStream(
cricket::StreamParams::CreateLegacy(kAudioSsrc)));
EXPECT_EQ(1u, call_.GetAudioReceiveStreams().size());
const cricket::FakeAudioReceiveStream* s =
call_.GetAudioReceiveStream(kAudioSsrc);
EXPECT_EQ(0, s->received_packets());
webrtc::RtpPacketReceived parsed_packet;
RTC_CHECK(parsed_packet.Parse(kPcmuPacket));
receive_channel_->OnPacketReceived(parsed_packet);
rtc::Thread::Current()->ProcessMessages(0);
EXPECT_EQ(1, s->received_packets());
}
// All receive channels should be associated with the first send channel,
// since they do not send RTCP SR.
TEST_P(WebRtcVoiceEngineTestFake, AssociateFirstSendChannel_SendCreatedFirst) {
EXPECT_TRUE(SetupSendStream());
EXPECT_TRUE(AddRecvStream(kSsrcY));
EXPECT_EQ(kSsrcX, GetRecvStreamConfig(kSsrcY).rtp.local_ssrc);
EXPECT_TRUE(send_channel_->AddSendStream(
cricket::StreamParams::CreateLegacy(kSsrcZ)));
EXPECT_EQ(kSsrcX, GetRecvStreamConfig(kSsrcY).rtp.local_ssrc);
EXPECT_TRUE(AddRecvStream(kSsrcW));
EXPECT_EQ(kSsrcX, GetRecvStreamConfig(kSsrcW).rtp.local_ssrc);
}
TEST_P(WebRtcVoiceEngineTestFake, AssociateFirstSendChannel_RecvCreatedFirst) {
EXPECT_TRUE(SetupRecvStream());
EXPECT_EQ(0xFA17FA17u, GetRecvStreamConfig(kSsrcX).rtp.local_ssrc);
EXPECT_TRUE(send_channel_->AddSendStream(
cricket::StreamParams::CreateLegacy(kSsrcY)));
EXPECT_EQ(kSsrcY, GetRecvStreamConfig(kSsrcX).rtp.local_ssrc);
EXPECT_TRUE(AddRecvStream(kSsrcZ));
EXPECT_EQ(kSsrcY, GetRecvStreamConfig(kSsrcZ).rtp.local_ssrc);
EXPECT_TRUE(send_channel_->AddSendStream(
cricket::StreamParams::CreateLegacy(kSsrcW)));
EXPECT_EQ(kSsrcY, GetRecvStreamConfig(kSsrcX).rtp.local_ssrc);
EXPECT_EQ(kSsrcY, GetRecvStreamConfig(kSsrcZ).rtp.local_ssrc);
}
TEST_P(WebRtcVoiceEngineTestFake, SetRawAudioSink) {
EXPECT_TRUE(SetupChannel());
std::unique_ptr<FakeAudioSink> fake_sink_1(new FakeAudioSink());
std::unique_ptr<FakeAudioSink> fake_sink_2(new FakeAudioSink());
// Setting the sink before a recv stream exists should do nothing.
receive_channel_->SetRawAudioSink(kSsrcX, std::move(fake_sink_1));
EXPECT_TRUE(AddRecvStream(kSsrcX));
EXPECT_EQ(nullptr, GetRecvStream(kSsrcX).sink());
// Now try actually setting the sink.
receive_channel_->SetRawAudioSink(kSsrcX, std::move(fake_sink_2));
EXPECT_NE(nullptr, GetRecvStream(kSsrcX).sink());
// Now try resetting it.
receive_channel_->SetRawAudioSink(kSsrcX, nullptr);
EXPECT_EQ(nullptr, GetRecvStream(kSsrcX).sink());
}
TEST_P(WebRtcVoiceEngineTestFake, SetRawAudioSinkUnsignaledRecvStream) {
EXPECT_TRUE(SetupChannel());
std::unique_ptr<FakeAudioSink> fake_sink_1(new FakeAudioSink());
std::unique_ptr<FakeAudioSink> fake_sink_2(new FakeAudioSink());
std::unique_ptr<FakeAudioSink> fake_sink_3(new FakeAudioSink());
std::unique_ptr<FakeAudioSink> fake_sink_4(new FakeAudioSink());
// Should be able to set a default sink even when no stream exists.
receive_channel_->SetDefaultRawAudioSink(std::move(fake_sink_1));
// Spawn an unsignaled stream by sending a packet - it should be assigned the
// default sink.
DeliverPacket(kPcmuFrame, sizeof(kPcmuFrame));
EXPECT_NE(nullptr, GetRecvStream(kSsrc1).sink());
// Try resetting the default sink.
receive_channel_->SetDefaultRawAudioSink(nullptr);
EXPECT_EQ(nullptr, GetRecvStream(kSsrc1).sink());
// Try setting the default sink while the default stream exists.
receive_channel_->SetDefaultRawAudioSink(std::move(fake_sink_2));
EXPECT_NE(nullptr, GetRecvStream(kSsrc1).sink());
// If we remove and add a default stream, it should get the same sink.
EXPECT_TRUE(receive_channel_->RemoveRecvStream(kSsrc1));
DeliverPacket(kPcmuFrame, sizeof(kPcmuFrame));
EXPECT_NE(nullptr, GetRecvStream(kSsrc1).sink());
// Spawn another unsignaled stream - it should be assigned the default sink
// and the previous unsignaled stream should lose it.
unsigned char pcmuFrame2[sizeof(kPcmuFrame)];
memcpy(pcmuFrame2, kPcmuFrame, sizeof(kPcmuFrame));
rtc::SetBE32(&pcmuFrame2[8], kSsrcX);
DeliverPacket(pcmuFrame2, sizeof(pcmuFrame2));
if (kMaxUnsignaledRecvStreams > 1) {
EXPECT_EQ(nullptr, GetRecvStream(kSsrc1).sink());
}
EXPECT_NE(nullptr, GetRecvStream(kSsrcX).sink());
// Reset the default sink - the second unsignaled stream should lose it.
receive_channel_->SetDefaultRawAudioSink(nullptr);
if (kMaxUnsignaledRecvStreams > 1) {
EXPECT_EQ(nullptr, GetRecvStream(kSsrc1).sink());
}
EXPECT_EQ(nullptr, GetRecvStream(kSsrcX).sink());
// Try setting the default sink while two streams exists.
receive_channel_->SetDefaultRawAudioSink(std::move(fake_sink_3));
if (kMaxUnsignaledRecvStreams > 1) {
EXPECT_EQ(nullptr, GetRecvStream(kSsrc1).sink());
}
EXPECT_NE(nullptr, GetRecvStream(kSsrcX).sink());
// Try setting the sink for the first unsignaled stream using its known SSRC.
receive_channel_->SetRawAudioSink(kSsrc1, std::move(fake_sink_4));
if (kMaxUnsignaledRecvStreams > 1) {
EXPECT_NE(nullptr, GetRecvStream(kSsrc1).sink());
}
EXPECT_NE(nullptr, GetRecvStream(kSsrcX).sink());
if (kMaxUnsignaledRecvStreams > 1) {
EXPECT_NE(GetRecvStream(kSsrc1).sink(), GetRecvStream(kSsrcX).sink());
}
}
// Test that, just like the video channel, the voice channel communicates the
// network state to the call.
TEST_P(WebRtcVoiceEngineTestFake, OnReadyToSendSignalsNetworkState) {
EXPECT_TRUE(SetupChannel());
EXPECT_EQ(webrtc::kNetworkUp,
call_.GetNetworkState(webrtc::MediaType::AUDIO));
EXPECT_EQ(webrtc::kNetworkUp,
call_.GetNetworkState(webrtc::MediaType::VIDEO));
send_channel_->OnReadyToSend(false);
EXPECT_EQ(webrtc::kNetworkDown,
call_.GetNetworkState(webrtc::MediaType::AUDIO));
EXPECT_EQ(webrtc::kNetworkUp,
call_.GetNetworkState(webrtc::MediaType::VIDEO));
send_channel_->OnReadyToSend(true);
EXPECT_EQ(webrtc::kNetworkUp,
call_.GetNetworkState(webrtc::MediaType::AUDIO));
EXPECT_EQ(webrtc::kNetworkUp,
call_.GetNetworkState(webrtc::MediaType::VIDEO));
}
// Test that playout is still started after changing parameters
TEST_P(WebRtcVoiceEngineTestFake, PreservePlayoutWhenRecreateRecvStream) {
SetupRecvStream();
receive_channel_->SetPlayout(true);
EXPECT_TRUE(GetRecvStream(kSsrcX).started());
// Changing RTP header extensions will recreate the
// AudioReceiveStreamInterface.
cricket::AudioReceiverParameters parameters;
parameters.extensions.push_back(
webrtc::RtpExtension(webrtc::RtpExtension::kAudioLevelUri, 12));
receive_channel_->SetReceiverParameters(parameters);
EXPECT_TRUE(GetRecvStream(kSsrcX).started());
}
// Tests when GetSources is called with non-existing ssrc, it will return an
// empty list of RtpSource without crashing.
TEST_P(WebRtcVoiceEngineTestFake, GetSourcesWithNonExistingSsrc) {
// Setup an recv stream with `kSsrcX`.
SetupRecvStream();
cricket::WebRtcVoiceReceiveChannel* media_channel = ReceiveImpl();
// Call GetSources with `kSsrcY` which doesn't exist.
std::vector<webrtc::RtpSource> sources = media_channel->GetSources(kSsrcY);
EXPECT_EQ(0u, sources.size());
}
// Tests that the library initializes and shuts down properly.
TEST(WebRtcVoiceEngineTest, StartupShutdown) {
rtc::AutoThread main_thread;
for (bool use_null_apm : {false, true}) {
// If the VoiceEngine wants to gather available codecs early, that's fine
// but we never want it to create a decoder at this stage.
Environment env = CreateEnvironment();
rtc::scoped_refptr<webrtc::test::MockAudioDeviceModule> adm =
webrtc::test::MockAudioDeviceModule::CreateNice();
rtc::scoped_refptr<webrtc::AudioProcessing> apm =
use_null_apm ? nullptr : webrtc::AudioProcessingBuilder().Create();
cricket::WebRtcVoiceEngine engine(
&env.task_queue_factory(), adm.get(),
webrtc::MockAudioEncoderFactory::CreateUnusedFactory(),
webrtc::MockAudioDecoderFactory::CreateUnusedFactory(), nullptr, apm,
nullptr, env.field_trials());
engine.Init();
std::unique_ptr<Call> call = Call::Create(CallConfig(env));
std::unique_ptr<cricket::VoiceMediaSendChannelInterface> send_channel =
engine.CreateSendChannel(
call.get(), cricket::MediaConfig(), cricket::AudioOptions(),
webrtc::CryptoOptions(), webrtc::AudioCodecPairId::Create());
EXPECT_TRUE(send_channel);
std::unique_ptr<cricket::VoiceMediaReceiveChannelInterface>
receive_channel = engine.CreateReceiveChannel(
call.get(), cricket::MediaConfig(), cricket::AudioOptions(),
webrtc::CryptoOptions(), webrtc::AudioCodecPairId::Create());
EXPECT_TRUE(receive_channel);
}
}
// Tests that reference counting on the external ADM is correct.
TEST(WebRtcVoiceEngineTest, StartupShutdownWithExternalADM) {
rtc::AutoThread main_thread;
for (bool use_null_apm : {false, true}) {
Environment env = CreateEnvironment();
auto adm = rtc::make_ref_counted<
::testing::NiceMock<webrtc::test::MockAudioDeviceModule>>();
{
rtc::scoped_refptr<webrtc::AudioProcessing> apm =
use_null_apm ? nullptr : webrtc::AudioProcessingBuilder().Create();
cricket::WebRtcVoiceEngine engine(
&env.task_queue_factory(), adm.get(),
webrtc::MockAudioEncoderFactory::CreateUnusedFactory(),
webrtc::MockAudioDecoderFactory::CreateUnusedFactory(), nullptr, apm,
nullptr, env.field_trials());
engine.Init();
std::unique_ptr<Call> call = Call::Create(CallConfig(env));
std::unique_ptr<cricket::VoiceMediaSendChannelInterface> send_channel =
engine.CreateSendChannel(
call.get(), cricket::MediaConfig(), cricket::AudioOptions(),
webrtc::CryptoOptions(), webrtc::AudioCodecPairId::Create());
EXPECT_TRUE(send_channel);
std::unique_ptr<cricket::VoiceMediaReceiveChannelInterface>
receive_channel = engine.CreateReceiveChannel(
call.get(), cricket::MediaConfig(), cricket::AudioOptions(),
webrtc::CryptoOptions(), webrtc::AudioCodecPairId::Create());
EXPECT_TRUE(receive_channel);
}
// The engine/channel should have dropped their references.
EXPECT_EQ(adm.release()->Release(),
rtc::RefCountReleaseStatus::kDroppedLastRef);
}
}
// Verify the payload id of common audio codecs, including CN and G722.
TEST(WebRtcVoiceEngineTest, HasCorrectPayloadTypeMapping) {
for (bool use_null_apm : {false, true}) {
std::unique_ptr<webrtc::TaskQueueFactory> task_queue_factory =
webrtc::CreateDefaultTaskQueueFactory();
// TODO(ossu): Why are the payload types of codecs with non-static payload
// type assignments checked here? It shouldn't really matter.
rtc::scoped_refptr<webrtc::test::MockAudioDeviceModule> adm =
webrtc::test::MockAudioDeviceModule::CreateNice();
rtc::scoped_refptr<webrtc::AudioProcessing> apm =
use_null_apm ? nullptr : webrtc::AudioProcessingBuilder().Create();
webrtc::FieldTrialBasedConfig field_trials;
cricket::WebRtcVoiceEngine engine(
task_queue_factory.get(), adm.get(),
webrtc::MockAudioEncoderFactory::CreateUnusedFactory(),
webrtc::MockAudioDecoderFactory::CreateUnusedFactory(), nullptr, apm,
nullptr, field_trials);
engine.Init();
for (const cricket::AudioCodec& codec : engine.send_codecs()) {
auto is_codec = [&codec](const char* name, int clockrate = 0) {
return absl::EqualsIgnoreCase(codec.name, name) &&
(clockrate == 0 || codec.clockrate == clockrate);
};
if (is_codec("CN", 16000)) {
EXPECT_EQ(105, codec.id);
} else if (is_codec("CN", 32000)) {
EXPECT_EQ(106, codec.id);
} else if (is_codec("G722", 8000)) {
EXPECT_EQ(9, codec.id);
} else if (is_codec("telephone-event", 8000)) {
EXPECT_EQ(126, codec.id);
// TODO(solenberg): 16k, 32k, 48k DTMF should be dynamically assigned.
// Remove these checks once both send and receive side assigns payload
// types dynamically.
} else if (is_codec("telephone-event", 16000)) {
EXPECT_EQ(113, codec.id);
} else if (is_codec("telephone-event", 32000)) {
EXPECT_EQ(112, codec.id);
} else if (is_codec("telephone-event", 48000)) {
EXPECT_EQ(110, codec.id);
} else if (is_codec("opus")) {
EXPECT_EQ(111, codec.id);
ASSERT_TRUE(codec.params.find("minptime") != codec.params.end());
EXPECT_EQ("10", codec.params.find("minptime")->second);
ASSERT_TRUE(codec.params.find("useinbandfec") != codec.params.end());
EXPECT_EQ("1", codec.params.find("useinbandfec")->second);
}
}
}
}
// Tests that VoE supports at least 32 channels
TEST(WebRtcVoiceEngineTest, Has32Channels) {
rtc::AutoThread main_thread;
for (bool use_null_apm : {false, true}) {
Environment env = CreateEnvironment();
rtc::scoped_refptr<webrtc::test::MockAudioDeviceModule> adm =
webrtc::test::MockAudioDeviceModule::CreateNice();
rtc::scoped_refptr<webrtc::AudioProcessing> apm =
use_null_apm ? nullptr : webrtc::AudioProcessingBuilder().Create();
cricket::WebRtcVoiceEngine engine(
&env.task_queue_factory(), adm.get(),
webrtc::MockAudioEncoderFactory::CreateUnusedFactory(),
webrtc::MockAudioDecoderFactory::CreateUnusedFactory(), nullptr, apm,
nullptr, env.field_trials());
engine.Init();
std::unique_ptr<Call> call = Call::Create(CallConfig(env));
std::vector<std::unique_ptr<cricket::VoiceMediaSendChannelInterface>>
channels;
while (channels.size() < 32) {
std::unique_ptr<cricket::VoiceMediaSendChannelInterface> channel =
engine.CreateSendChannel(
call.get(), cricket::MediaConfig(), cricket::AudioOptions(),
webrtc::CryptoOptions(), webrtc::AudioCodecPairId::Create());
if (!channel)
break;
channels.emplace_back(std::move(channel));
}
EXPECT_EQ(channels.size(), 32u);
}
}
// Test that we set our preferred codecs properly.
TEST(WebRtcVoiceEngineTest, SetRecvCodecs) {
rtc::AutoThread main_thread;
for (bool use_null_apm : {false, true}) {
Environment env = CreateEnvironment();
// TODO(ossu): I'm not sure of the intent of this test. It's either:
// - Check that our builtin codecs are usable by Channel.
// - The codecs provided by the engine is usable by Channel.
// It does not check that the codecs in the RecvParameters are actually
// what we sent in - though it's probably reasonable to expect so, if
// SetReceiverParameters returns true.
// I think it will become clear once audio decoder injection is completed.
rtc::scoped_refptr<webrtc::test::MockAudioDeviceModule> adm =
webrtc::test::MockAudioDeviceModule::CreateNice();
rtc::scoped_refptr<webrtc::AudioProcessing> apm =
use_null_apm ? nullptr : webrtc::AudioProcessingBuilder().Create();
cricket::WebRtcVoiceEngine engine(
&env.task_queue_factory(), adm.get(),
webrtc::MockAudioEncoderFactory::CreateUnusedFactory(),
webrtc::CreateBuiltinAudioDecoderFactory(), nullptr, apm, nullptr,
env.field_trials());
engine.Init();
std::unique_ptr<Call> call = Call::Create(CallConfig(env));
cricket::WebRtcVoiceReceiveChannel channel(
&engine, cricket::MediaConfig(), cricket::AudioOptions(),
webrtc::CryptoOptions(), call.get(),
webrtc::AudioCodecPairId::Create());
cricket::AudioReceiverParameters parameters;
parameters.codecs = engine.recv_codecs();
EXPECT_TRUE(channel.SetReceiverParameters(parameters));
}
}
TEST(WebRtcVoiceEngineTest, SetRtpSendParametersMaxBitrate) {
rtc::AutoThread main_thread;
Environment env = CreateEnvironment();
rtc::scoped_refptr<webrtc::test::MockAudioDeviceModule> adm =
webrtc::test::MockAudioDeviceModule::CreateNice();
FakeAudioSource source;
cricket::WebRtcVoiceEngine engine(&env.task_queue_factory(), adm.get(),
webrtc::CreateBuiltinAudioEncoderFactory(),
webrtc::CreateBuiltinAudioDecoderFactory(),
nullptr, nullptr, nullptr,
env.field_trials());
engine.Init();
CallConfig call_config(env);
{
webrtc::AudioState::Config config;
config.audio_mixer = webrtc::AudioMixerImpl::Create();
config.audio_device_module =
webrtc::test::MockAudioDeviceModule::CreateNice();
call_config.audio_state = webrtc::AudioState::Create(config);
}
std::unique_ptr<Call> call = Call::Create(call_config);
cricket::WebRtcVoiceSendChannel channel(
&engine, cricket::MediaConfig(), cricket::AudioOptions(),
webrtc::CryptoOptions(), call.get(), webrtc::AudioCodecPairId::Create());
{
cricket::AudioSenderParameter params;
params.codecs.push_back(cricket::CreateAudioCodec(1, "opus", 48000, 2));
params.extensions.push_back(webrtc::RtpExtension(
webrtc::RtpExtension::kTransportSequenceNumberUri, 1));
EXPECT_TRUE(channel.SetSenderParameters(params));
}
constexpr int kSsrc = 1234;
{
cricket::StreamParams params;
params.add_ssrc(kSsrc);
channel.AddSendStream(params);
}
channel.SetAudioSend(kSsrc, true, nullptr, &source);
channel.SetSend(true);
webrtc::RtpParameters params = channel.GetRtpSendParameters(kSsrc);
for (int max_bitrate : {-10, -1, 0, 10000}) {
params.encodings[0].max_bitrate_bps = max_bitrate;
channel.SetRtpSendParameters(
kSsrc, params, [](webrtc::RTCError error) { EXPECT_TRUE(error.ok()); });
}
}
TEST(WebRtcVoiceEngineTest, CollectRecvCodecs) {
for (bool use_null_apm : {false, true}) {
std::vector<webrtc::AudioCodecSpec> specs;
webrtc::AudioCodecSpec spec1{{"codec1", 48000, 2, {{"param1", "value1"}}},
{48000, 2, 16000, 10000, 20000}};
spec1.info.allow_comfort_noise = false;
spec1.info.supports_network_adaption = true;
specs.push_back(spec1);
webrtc::AudioCodecSpec spec2{{"codec2", 32000, 1}, {32000, 1, 32000}};
spec2.info.allow_comfort_noise = false;
specs.push_back(spec2);
specs.push_back(webrtc::AudioCodecSpec{
{"codec3", 16000, 1, {{"param1", "value1b"}, {"param2", "value2"}}},
{16000, 1, 13300}});
specs.push_back(
webrtc::AudioCodecSpec{{"codec4", 8000, 1}, {8000, 1, 64000}});
specs.push_back(
webrtc::AudioCodecSpec{{"codec5", 8000, 2}, {8000, 1, 64000}});
std::unique_ptr<webrtc::TaskQueueFactory> task_queue_factory =
webrtc::CreateDefaultTaskQueueFactory();
rtc::scoped_refptr<webrtc::MockAudioEncoderFactory> unused_encoder_factory =
webrtc::MockAudioEncoderFactory::CreateUnusedFactory();
rtc::scoped_refptr<webrtc::MockAudioDecoderFactory> mock_decoder_factory =
rtc::make_ref_counted<webrtc::MockAudioDecoderFactory>();
EXPECT_CALL(*mock_decoder_factory.get(), GetSupportedDecoders())
.WillOnce(Return(specs));
rtc::scoped_refptr<webrtc::test::MockAudioDeviceModule> adm =
webrtc::test::MockAudioDeviceModule::CreateNice();
rtc::scoped_refptr<webrtc::AudioProcessing> apm =
use_null_apm ? nullptr : webrtc::AudioProcessingBuilder().Create();
webrtc::FieldTrialBasedConfig field_trials;
cricket::WebRtcVoiceEngine engine(
task_queue_factory.get(), adm.get(), unused_encoder_factory,
mock_decoder_factory, nullptr, apm, nullptr, field_trials);
engine.Init();
auto codecs = engine.recv_codecs();
EXPECT_EQ(11u, codecs.size());
// Rather than just ASSERTing that there are enough codecs, ensure that we
// can check the actual values safely, to provide better test results.
auto get_codec = [&codecs](size_t index) -> const cricket::AudioCodec& {
static const cricket::AudioCodec missing_codec =
cricket::CreateAudioCodec(0, "<missing>", 0, 0);
if (codecs.size() > index)
return codecs[index];
return missing_codec;
};
// Ensure the general codecs are generated first and in order.
for (size_t i = 0; i != specs.size(); ++i) {
EXPECT_EQ(specs[i].format.name, get_codec(i).name);
EXPECT_EQ(specs[i].format.clockrate_hz, get_codec(i).clockrate);
EXPECT_EQ(specs[i].format.num_channels, get_codec(i).channels);
EXPECT_EQ(specs[i].format.parameters, get_codec(i).params);
}
// Find the index of a codec, or -1 if not found, so that we can easily
// check supplementary codecs are ordered after the general codecs.
auto find_codec = [&codecs](const webrtc::SdpAudioFormat& format) -> int {
for (size_t i = 0; i != codecs.size(); ++i) {
const cricket::AudioCodec& codec = codecs[i];
if (absl::EqualsIgnoreCase(codec.name, format.name) &&
codec.clockrate == format.clockrate_hz &&
codec.channels == format.num_channels) {
return rtc::checked_cast<int>(i);
}
}
return -1;
};
// Ensure all supplementary codecs are generated last. Their internal
// ordering is not important. Without this cast, the comparison turned
// unsigned and, thus, failed for -1.
const int num_specs = static_cast<int>(specs.size());
EXPECT_GE(find_codec({"cn", 8000, 1}), num_specs);
EXPECT_GE(find_codec({"cn", 16000, 1}), num_specs);
EXPECT_EQ(find_codec({"cn", 32000, 1}), -1);
EXPECT_GE(find_codec({"telephone-event", 8000, 1}), num_specs);
EXPECT_GE(find_codec({"telephone-event", 16000, 1}), num_specs);
EXPECT_GE(find_codec({"telephone-event", 32000, 1}), num_specs);
EXPECT_GE(find_codec({"telephone-event", 48000, 1}), num_specs);
}
}
|