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
|
/*
* Copyright 2012 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.
*/
#ifndef PC_TEST_INTEGRATION_TEST_HELPERS_H_
#define PC_TEST_INTEGRATION_TEST_HELPERS_H_
#include <limits.h>
#include <stdint.h>
#include <stdio.h>
#include <algorithm>
#include <functional>
#include <limits>
#include <list>
#include <map>
#include <memory>
#include <set>
#include <string>
#include <utility>
#include <vector>
#include "absl/algorithm/container.h"
#include "absl/memory/memory.h"
#include "absl/strings/string_view.h"
#include "absl/types/optional.h"
#include "api/audio_options.h"
#include "api/call/call_factory_interface.h"
#include "api/candidate.h"
#include "api/crypto/crypto_options.h"
#include "api/data_channel_interface.h"
#include "api/field_trials_view.h"
#include "api/ice_transport_interface.h"
#include "api/jsep.h"
#include "api/media_stream_interface.h"
#include "api/media_types.h"
#include "api/peer_connection_interface.h"
#include "api/rtc_error.h"
#include "api/rtc_event_log/rtc_event_log_factory.h"
#include "api/rtc_event_log/rtc_event_log_factory_interface.h"
#include "api/rtc_event_log_output.h"
#include "api/rtp_receiver_interface.h"
#include "api/rtp_sender_interface.h"
#include "api/rtp_transceiver_interface.h"
#include "api/scoped_refptr.h"
#include "api/stats/rtc_stats.h"
#include "api/stats/rtc_stats_report.h"
#include "api/stats/rtcstats_objects.h"
#include "api/task_queue/default_task_queue_factory.h"
#include "api/task_queue/pending_task_safety_flag.h"
#include "api/task_queue/task_queue_factory.h"
#include "api/transport/field_trial_based_config.h"
#include "api/uma_metrics.h"
#include "api/units/time_delta.h"
#include "api/video/video_rotation.h"
#include "api/video_codecs/sdp_video_format.h"
#include "api/video_codecs/video_decoder_factory.h"
#include "api/video_codecs/video_encoder_factory.h"
#include "call/call.h"
#include "logging/rtc_event_log/fake_rtc_event_log_factory.h"
#include "media/base/media_engine.h"
#include "media/base/stream_params.h"
#include "media/engine/fake_webrtc_video_engine.h"
#include "media/engine/webrtc_media_engine.h"
#include "media/engine/webrtc_media_engine_defaults.h"
#include "modules/audio_device/include/audio_device.h"
#include "modules/audio_processing/include/audio_processing.h"
#include "modules/audio_processing/test/audio_processing_builder_for_testing.h"
#include "p2p/base/fake_ice_transport.h"
#include "p2p/base/ice_transport_internal.h"
#include "p2p/base/mock_async_resolver.h"
#include "p2p/base/p2p_constants.h"
#include "p2p/base/port.h"
#include "p2p/base/port_allocator.h"
#include "p2p/base/port_interface.h"
#include "p2p/base/test_stun_server.h"
#include "p2p/base/test_turn_customizer.h"
#include "p2p/base/test_turn_server.h"
#include "p2p/client/basic_port_allocator.h"
#include "pc/dtmf_sender.h"
#include "pc/local_audio_source.h"
#include "pc/media_session.h"
#include "pc/peer_connection.h"
#include "pc/peer_connection_factory.h"
#include "pc/peer_connection_proxy.h"
#include "pc/rtp_media_utils.h"
#include "pc/session_description.h"
#include "pc/test/fake_audio_capture_module.h"
#include "pc/test/fake_periodic_video_source.h"
#include "pc/test/fake_periodic_video_track_source.h"
#include "pc/test/fake_rtc_certificate_generator.h"
#include "pc/test/fake_video_track_renderer.h"
#include "pc/test/mock_peer_connection_observers.h"
#include "pc/video_track_source.h"
#include "rtc_base/checks.h"
#include "rtc_base/event.h"
#include "rtc_base/fake_clock.h"
#include "rtc_base/fake_mdns_responder.h"
#include "rtc_base/fake_network.h"
#include "rtc_base/firewall_socket_server.h"
#include "rtc_base/gunit.h"
#include "rtc_base/helpers.h"
#include "rtc_base/ip_address.h"
#include "rtc_base/logging.h"
#include "rtc_base/mdns_responder_interface.h"
#include "rtc_base/numerics/safe_conversions.h"
#include "rtc_base/rtc_certificate_generator.h"
#include "rtc_base/socket_address.h"
#include "rtc_base/ssl_stream_adapter.h"
#include "rtc_base/task_queue_for_test.h"
#include "rtc_base/task_utils/repeating_task.h"
#include "rtc_base/test_certificate_verifier.h"
#include "rtc_base/thread.h"
#include "rtc_base/thread_annotations.h"
#include "rtc_base/time_utils.h"
#include "rtc_base/virtual_socket_server.h"
#include "system_wrappers/include/metrics.h"
#include "test/gmock.h"
#include "test/scoped_key_value_config.h"
namespace webrtc {
using ::cricket::ContentInfo;
using ::cricket::StreamParams;
using ::rtc::SocketAddress;
using ::testing::_;
using ::testing::Combine;
using ::testing::Contains;
using ::testing::DoAll;
using ::testing::ElementsAre;
using ::testing::NiceMock;
using ::testing::Return;
using ::testing::SetArgPointee;
using ::testing::UnorderedElementsAreArray;
using ::testing::Values;
using RTCConfiguration = PeerConnectionInterface::RTCConfiguration;
static const int kDefaultTimeout = 10000;
static const int kMaxWaitForStatsMs = 3000;
static const int kMaxWaitForActivationMs = 5000;
static const int kMaxWaitForFramesMs = 10000;
// Default number of audio/video frames to wait for before considering a test
// successful.
static const int kDefaultExpectedAudioFrameCount = 3;
static const int kDefaultExpectedVideoFrameCount = 3;
static const char kDataChannelLabel[] = "data_channel";
// SRTP cipher name negotiated by the tests. This must be updated if the
// default changes.
static const int kDefaultSrtpCryptoSuite = rtc::kSrtpAes128CmSha1_80;
static const int kDefaultSrtpCryptoSuiteGcm = rtc::kSrtpAeadAes256Gcm;
static const SocketAddress kDefaultLocalAddress("192.168.1.1", 0);
// Helper function for constructing offer/answer options to initiate an ICE
// restart.
PeerConnectionInterface::RTCOfferAnswerOptions IceRestartOfferAnswerOptions();
// Remove all stream information (SSRCs, track IDs, etc.) and "msid-semantic"
// attribute from received SDP, simulating a legacy endpoint.
void RemoveSsrcsAndMsids(cricket::SessionDescription* desc);
// Removes all stream information besides the stream ids, simulating an
// endpoint that only signals a=msid lines to convey stream_ids.
void RemoveSsrcsAndKeepMsids(cricket::SessionDescription* desc);
int FindFirstMediaStatsIndexByKind(
const std::string& kind,
const std::vector<const webrtc::RTCInboundRTPStreamStats*>& inbound_rtps);
class TaskQueueMetronome : public webrtc::Metronome {
public:
explicit TaskQueueMetronome(TimeDelta tick_period);
~TaskQueueMetronome() override;
// webrtc::Metronome implementation.
void RequestCallOnNextTick(absl::AnyInvocable<void() &&> callback) override;
TimeDelta TickPeriod() const override;
private:
const TimeDelta tick_period_;
SequenceChecker sequence_checker_;
std::vector<absl::AnyInvocable<void() &&>> callbacks_;
ScopedTaskSafetyDetached safety_;
};
class SignalingMessageReceiver {
public:
virtual void ReceiveSdpMessage(SdpType type, const std::string& msg) = 0;
virtual void ReceiveIceMessage(const std::string& sdp_mid,
int sdp_mline_index,
const std::string& msg) = 0;
protected:
SignalingMessageReceiver() {}
virtual ~SignalingMessageReceiver() {}
};
class MockRtpReceiverObserver : public webrtc::RtpReceiverObserverInterface {
public:
explicit MockRtpReceiverObserver(cricket::MediaType media_type)
: expected_media_type_(media_type) {}
void OnFirstPacketReceived(cricket::MediaType media_type) override {
ASSERT_EQ(expected_media_type_, media_type);
first_packet_received_ = true;
}
bool first_packet_received() const { return first_packet_received_; }
virtual ~MockRtpReceiverObserver() {}
private:
bool first_packet_received_ = false;
cricket::MediaType expected_media_type_;
};
// Helper class that wraps a peer connection, observes it, and can accept
// signaling messages from another wrapper.
//
// Uses a fake network, fake A/V capture, and optionally fake
// encoders/decoders, though they aren't used by default since they don't
// advertise support of any codecs.
// TODO(steveanton): See how this could become a subclass of
// PeerConnectionWrapper defined in peerconnectionwrapper.h.
class PeerConnectionIntegrationWrapper : public webrtc::PeerConnectionObserver,
public SignalingMessageReceiver {
public:
webrtc::PeerConnectionFactoryInterface* pc_factory() const {
return peer_connection_factory_.get();
}
webrtc::PeerConnectionInterface* pc() const { return peer_connection_.get(); }
// If a signaling message receiver is set (via ConnectFakeSignaling), this
// will set the whole offer/answer exchange in motion. Just need to wait for
// the signaling state to reach "stable".
void CreateAndSetAndSignalOffer() {
auto offer = CreateOfferAndWait();
ASSERT_NE(nullptr, offer);
EXPECT_TRUE(SetLocalDescriptionAndSendSdpMessage(std::move(offer)));
}
// Sets the options to be used when CreateAndSetAndSignalOffer is called, or
// when a remote offer is received (via fake signaling) and an answer is
// generated. By default, uses default options.
void SetOfferAnswerOptions(
const PeerConnectionInterface::RTCOfferAnswerOptions& options) {
offer_answer_options_ = options;
}
// Set a callback to be invoked when SDP is received via the fake signaling
// channel, which provides an opportunity to munge (modify) the SDP. This is
// used to test SDP being applied that a PeerConnection would normally not
// generate, but a non-JSEP endpoint might.
void SetReceivedSdpMunger(
std::function<void(cricket::SessionDescription*)> munger) {
received_sdp_munger_ = std::move(munger);
}
// Similar to the above, but this is run on SDP immediately after it's
// generated.
void SetGeneratedSdpMunger(
std::function<void(cricket::SessionDescription*)> munger) {
generated_sdp_munger_ = std::move(munger);
}
// Set a callback to be invoked when a remote offer is received via the fake
// signaling channel. This provides an opportunity to change the
// PeerConnection state before an answer is created and sent to the caller.
void SetRemoteOfferHandler(std::function<void()> handler) {
remote_offer_handler_ = std::move(handler);
}
void SetRemoteAsyncResolver(rtc::MockAsyncResolver* resolver) {
remote_async_resolver_ = resolver;
}
// Every ICE connection state in order that has been seen by the observer.
std::vector<PeerConnectionInterface::IceConnectionState>
ice_connection_state_history() const {
return ice_connection_state_history_;
}
void clear_ice_connection_state_history() {
ice_connection_state_history_.clear();
}
// Every standardized ICE connection state in order that has been seen by the
// observer.
std::vector<PeerConnectionInterface::IceConnectionState>
standardized_ice_connection_state_history() const {
return standardized_ice_connection_state_history_;
}
// Every PeerConnection state in order that has been seen by the observer.
std::vector<PeerConnectionInterface::PeerConnectionState>
peer_connection_state_history() const {
return peer_connection_state_history_;
}
// Every ICE gathering state in order that has been seen by the observer.
std::vector<PeerConnectionInterface::IceGatheringState>
ice_gathering_state_history() const {
return ice_gathering_state_history_;
}
std::vector<cricket::CandidatePairChangeEvent>
ice_candidate_pair_change_history() const {
return ice_candidate_pair_change_history_;
}
// Every PeerConnection signaling state in order that has been seen by the
// observer.
std::vector<PeerConnectionInterface::SignalingState>
peer_connection_signaling_state_history() const {
return peer_connection_signaling_state_history_;
}
void AddAudioVideoTracks() {
AddAudioTrack();
AddVideoTrack();
}
rtc::scoped_refptr<RtpSenderInterface> AddAudioTrack() {
return AddTrack(CreateLocalAudioTrack());
}
rtc::scoped_refptr<RtpSenderInterface> AddVideoTrack() {
return AddTrack(CreateLocalVideoTrack());
}
rtc::scoped_refptr<webrtc::AudioTrackInterface> CreateLocalAudioTrack() {
cricket::AudioOptions options;
// Disable highpass filter so that we can get all the test audio frames.
options.highpass_filter = false;
rtc::scoped_refptr<webrtc::AudioSourceInterface> source =
peer_connection_factory_->CreateAudioSource(options);
// TODO(perkj): Test audio source when it is implemented. Currently audio
// always use the default input.
return peer_connection_factory_->CreateAudioTrack(rtc::CreateRandomUuid(),
source.get());
}
rtc::scoped_refptr<webrtc::VideoTrackInterface> CreateLocalVideoTrack() {
webrtc::FakePeriodicVideoSource::Config config;
config.timestamp_offset_ms = rtc::TimeMillis();
return CreateLocalVideoTrackInternal(config);
}
rtc::scoped_refptr<webrtc::VideoTrackInterface>
CreateLocalVideoTrackWithConfig(
webrtc::FakePeriodicVideoSource::Config config) {
return CreateLocalVideoTrackInternal(config);
}
rtc::scoped_refptr<webrtc::VideoTrackInterface>
CreateLocalVideoTrackWithRotation(webrtc::VideoRotation rotation) {
webrtc::FakePeriodicVideoSource::Config config;
config.rotation = rotation;
config.timestamp_offset_ms = rtc::TimeMillis();
return CreateLocalVideoTrackInternal(config);
}
rtc::scoped_refptr<RtpSenderInterface> AddTrack(
rtc::scoped_refptr<MediaStreamTrackInterface> track,
const std::vector<std::string>& stream_ids = {}) {
EXPECT_TRUE(track);
if (!track) {
return nullptr;
}
auto result = pc()->AddTrack(track, stream_ids);
EXPECT_EQ(RTCErrorType::NONE, result.error().type());
if (result.ok()) {
return result.MoveValue();
} else {
return nullptr;
}
}
std::vector<rtc::scoped_refptr<RtpReceiverInterface>> GetReceiversOfType(
cricket::MediaType media_type) {
std::vector<rtc::scoped_refptr<RtpReceiverInterface>> receivers;
for (const auto& receiver : pc()->GetReceivers()) {
if (receiver->media_type() == media_type) {
receivers.push_back(receiver);
}
}
return receivers;
}
rtc::scoped_refptr<RtpTransceiverInterface> GetFirstTransceiverOfType(
cricket::MediaType media_type) {
for (auto transceiver : pc()->GetTransceivers()) {
if (transceiver->receiver()->media_type() == media_type) {
return transceiver;
}
}
return nullptr;
}
bool SignalingStateStable() {
return pc()->signaling_state() == webrtc::PeerConnectionInterface::kStable;
}
bool IceGatheringStateComplete() {
return pc()->ice_gathering_state() ==
webrtc::PeerConnectionInterface::kIceGatheringComplete;
}
void CreateDataChannel() { CreateDataChannel(nullptr); }
void CreateDataChannel(const webrtc::DataChannelInit* init) {
CreateDataChannel(kDataChannelLabel, init);
}
void CreateDataChannel(const std::string& label,
const webrtc::DataChannelInit* init) {
auto data_channel_or_error = pc()->CreateDataChannelOrError(label, init);
ASSERT_TRUE(data_channel_or_error.ok());
data_channels_.push_back(data_channel_or_error.MoveValue());
ASSERT_TRUE(data_channels_.back().get() != nullptr);
data_observers_.push_back(
std::make_unique<MockDataChannelObserver>(data_channels_.back().get()));
}
// Return the last observed data channel.
DataChannelInterface* data_channel() {
if (data_channels_.size() == 0) {
return nullptr;
}
return data_channels_.back().get();
}
// Return all data channels.
std::vector<rtc::scoped_refptr<DataChannelInterface>>& data_channels() {
return data_channels_;
}
const MockDataChannelObserver* data_observer() const {
if (data_observers_.size() == 0) {
return nullptr;
}
return data_observers_.back().get();
}
std::vector<std::unique_ptr<MockDataChannelObserver>>& data_observers() {
return data_observers_;
}
int audio_frames_received() const {
return fake_audio_capture_module_->frames_received();
}
// Takes minimum of video frames received for each track.
//
// Can be used like:
// EXPECT_GE(expected_frames, min_video_frames_received_per_track());
//
// To ensure that all video tracks received at least a certain number of
// frames.
int min_video_frames_received_per_track() const {
int min_frames = INT_MAX;
if (fake_video_renderers_.empty()) {
return 0;
}
for (const auto& pair : fake_video_renderers_) {
min_frames = std::min(min_frames, pair.second->num_rendered_frames());
}
return min_frames;
}
// Returns a MockStatsObserver in a state after stats gathering finished,
// which can be used to access the gathered stats.
rtc::scoped_refptr<MockStatsObserver> OldGetStatsForTrack(
webrtc::MediaStreamTrackInterface* track) {
auto observer = rtc::make_ref_counted<MockStatsObserver>();
EXPECT_TRUE(peer_connection_->GetStats(
observer.get(), nullptr,
PeerConnectionInterface::kStatsOutputLevelStandard));
EXPECT_TRUE_WAIT(observer->called(), kDefaultTimeout);
return observer;
}
// Version that doesn't take a track "filter", and gathers all stats.
rtc::scoped_refptr<MockStatsObserver> OldGetStats() {
return OldGetStatsForTrack(nullptr);
}
// Synchronously gets stats and returns them. If it times out, fails the test
// and returns null.
rtc::scoped_refptr<const webrtc::RTCStatsReport> NewGetStats() {
auto callback =
rtc::make_ref_counted<webrtc::MockRTCStatsCollectorCallback>();
peer_connection_->GetStats(callback.get());
EXPECT_TRUE_WAIT(callback->called(), kDefaultTimeout);
return callback->report();
}
int rendered_width() {
EXPECT_FALSE(fake_video_renderers_.empty());
return fake_video_renderers_.empty()
? 0
: fake_video_renderers_.begin()->second->width();
}
int rendered_height() {
EXPECT_FALSE(fake_video_renderers_.empty());
return fake_video_renderers_.empty()
? 0
: fake_video_renderers_.begin()->second->height();
}
double rendered_aspect_ratio() {
if (rendered_height() == 0) {
return 0.0;
}
return static_cast<double>(rendered_width()) / rendered_height();
}
webrtc::VideoRotation rendered_rotation() {
EXPECT_FALSE(fake_video_renderers_.empty());
return fake_video_renderers_.empty()
? webrtc::kVideoRotation_0
: fake_video_renderers_.begin()->second->rotation();
}
int local_rendered_width() {
return local_video_renderer_ ? local_video_renderer_->width() : 0;
}
int local_rendered_height() {
return local_video_renderer_ ? local_video_renderer_->height() : 0;
}
double local_rendered_aspect_ratio() {
if (local_rendered_height() == 0) {
return 0.0;
}
return static_cast<double>(local_rendered_width()) /
local_rendered_height();
}
size_t number_of_remote_streams() {
if (!pc()) {
return 0;
}
return pc()->remote_streams()->count();
}
StreamCollectionInterface* remote_streams() const {
if (!pc()) {
ADD_FAILURE();
return nullptr;
}
return pc()->remote_streams().get();
}
StreamCollectionInterface* local_streams() {
if (!pc()) {
ADD_FAILURE();
return nullptr;
}
return pc()->local_streams().get();
}
webrtc::PeerConnectionInterface::SignalingState signaling_state() {
return pc()->signaling_state();
}
webrtc::PeerConnectionInterface::IceConnectionState ice_connection_state() {
return pc()->ice_connection_state();
}
webrtc::PeerConnectionInterface::IceConnectionState
standardized_ice_connection_state() {
return pc()->standardized_ice_connection_state();
}
webrtc::PeerConnectionInterface::IceGatheringState ice_gathering_state() {
return pc()->ice_gathering_state();
}
// Returns a MockRtpReceiverObserver for each RtpReceiver returned by
// GetReceivers. They're updated automatically when a remote offer/answer
// from the fake signaling channel is applied, or when
// ResetRtpReceiverObservers below is called.
const std::vector<std::unique_ptr<MockRtpReceiverObserver>>&
rtp_receiver_observers() {
return rtp_receiver_observers_;
}
void ResetRtpReceiverObservers() {
rtp_receiver_observers_.clear();
for (const rtc::scoped_refptr<RtpReceiverInterface>& receiver :
pc()->GetReceivers()) {
std::unique_ptr<MockRtpReceiverObserver> observer(
new MockRtpReceiverObserver(receiver->media_type()));
receiver->SetObserver(observer.get());
rtp_receiver_observers_.push_back(std::move(observer));
}
}
rtc::FakeNetworkManager* network_manager() const {
return fake_network_manager_.get();
}
cricket::PortAllocator* port_allocator() const { return port_allocator_; }
webrtc::FakeRtcEventLogFactory* event_log_factory() const {
return event_log_factory_;
}
const cricket::Candidate& last_candidate_gathered() const {
return last_candidate_gathered_;
}
const cricket::IceCandidateErrorEvent& error_event() const {
return error_event_;
}
// Sets the mDNS responder for the owned fake network manager and keeps a
// reference to the responder.
void SetMdnsResponder(
std::unique_ptr<webrtc::FakeMdnsResponder> mdns_responder) {
RTC_DCHECK(mdns_responder != nullptr);
mdns_responder_ = mdns_responder.get();
network_manager()->set_mdns_responder(std::move(mdns_responder));
}
// Returns null on failure.
std::unique_ptr<SessionDescriptionInterface> CreateOfferAndWait() {
auto observer =
rtc::make_ref_counted<MockCreateSessionDescriptionObserver>();
pc()->CreateOffer(observer.get(), offer_answer_options_);
return WaitForDescriptionFromObserver(observer.get());
}
bool Rollback() {
return SetRemoteDescription(
webrtc::CreateSessionDescription(SdpType::kRollback, ""));
}
// Functions for querying stats.
void StartWatchingDelayStats() {
// Get the baseline numbers for audio_packets and audio_delay.
auto received_stats = NewGetStats();
auto rtp_stats =
received_stats->GetStatsOfType<webrtc::RTCInboundRTPStreamStats>()[0];
ASSERT_TRUE(rtp_stats->relative_packet_arrival_delay.is_defined());
ASSERT_TRUE(rtp_stats->packets_received.is_defined());
ASSERT_TRUE(rtp_stats->track_id.is_defined());
rtp_stats_id_ = rtp_stats->id();
audio_packets_stat_ = *rtp_stats->packets_received;
audio_delay_stat_ = *rtp_stats->relative_packet_arrival_delay;
audio_samples_stat_ = *rtp_stats->total_samples_received;
audio_concealed_stat_ = *rtp_stats->concealed_samples;
}
void UpdateDelayStats(std::string tag, int desc_size) {
auto report = NewGetStats();
auto rtp_stats =
report->GetAs<webrtc::RTCInboundRTPStreamStats>(rtp_stats_id_);
ASSERT_TRUE(rtp_stats);
auto delta_packets = *rtp_stats->packets_received - audio_packets_stat_;
auto delta_rpad =
*rtp_stats->relative_packet_arrival_delay - audio_delay_stat_;
auto recent_delay = delta_packets > 0 ? delta_rpad / delta_packets : -1;
// The purpose of these checks is to sound the alarm early if we introduce
// serious regressions. The numbers are not acceptable for production, but
// occur on slow bots.
//
// An average relative packet arrival delay over the renegotiation of
// > 100 ms indicates that something is dramatically wrong, and will impact
// quality for sure.
// Worst bots:
// linux_x86_dbg at 0.206
#if !defined(NDEBUG)
EXPECT_GT(0.25, recent_delay) << tag << " size " << desc_size;
#else
EXPECT_GT(0.1, recent_delay) << tag << " size " << desc_size;
#endif
auto delta_samples =
*rtp_stats->total_samples_received - audio_samples_stat_;
auto delta_concealed =
*rtp_stats->concealed_samples - audio_concealed_stat_;
// These limits should be adjusted down as we improve:
//
// Concealing more than 4000 samples during a renegotiation is unacceptable.
// But some bots are slow.
// Worst bots:
// linux_more_configs bot at conceal count 5184
// android_arm_rel at conceal count 9241
// linux_x86_dbg at 15174
#if !defined(NDEBUG)
EXPECT_GT(18000U, delta_concealed) << "Concealed " << delta_concealed
<< " of " << delta_samples << " samples";
#else
EXPECT_GT(15000U, delta_concealed) << "Concealed " << delta_concealed
<< " of " << delta_samples << " samples";
#endif
// Concealing more than 20% of samples during a renegotiation is
// unacceptable.
// Worst bots:
// Nondebug: Linux32 Release at conceal rate 0.606597 (CI run)
// Debug: linux_x86_dbg bot at conceal rate 0.854
if (delta_samples > 0) {
#if !defined(NDEBUG)
EXPECT_LT(1.0 * delta_concealed / delta_samples, 0.95)
<< "Concealed " << delta_concealed << " of " << delta_samples
<< " samples";
#else
EXPECT_LT(1.0 * delta_concealed / delta_samples, 0.7)
<< "Concealed " << delta_concealed << " of " << delta_samples
<< " samples";
#endif
}
// Increment trailing counters
audio_packets_stat_ = *rtp_stats->packets_received;
audio_delay_stat_ = *rtp_stats->relative_packet_arrival_delay;
audio_samples_stat_ = *rtp_stats->total_samples_received;
audio_concealed_stat_ = *rtp_stats->concealed_samples;
}
// Sets number of candidates expected
void ExpectCandidates(int candidate_count) {
candidates_expected_ = candidate_count;
}
private:
// Constructor used by friend class PeerConnectionIntegrationBaseTest.
explicit PeerConnectionIntegrationWrapper(const std::string& debug_name)
: debug_name_(debug_name) {}
bool Init(const PeerConnectionFactory::Options* options,
const PeerConnectionInterface::RTCConfiguration* config,
webrtc::PeerConnectionDependencies dependencies,
rtc::SocketServer* socket_server,
rtc::Thread* network_thread,
rtc::Thread* worker_thread,
std::unique_ptr<webrtc::FakeRtcEventLogFactory> event_log_factory,
bool reset_encoder_factory,
bool reset_decoder_factory,
bool create_media_engine) {
// There's an error in this test code if Init ends up being called twice.
RTC_DCHECK(!peer_connection_);
RTC_DCHECK(!peer_connection_factory_);
fake_network_manager_.reset(new rtc::FakeNetworkManager());
fake_network_manager_->AddInterface(kDefaultLocalAddress);
std::unique_ptr<cricket::PortAllocator> port_allocator(
new cricket::BasicPortAllocator(
fake_network_manager_.get(),
std::make_unique<rtc::BasicPacketSocketFactory>(socket_server)));
port_allocator_ = port_allocator.get();
fake_audio_capture_module_ = FakeAudioCaptureModule::Create();
if (!fake_audio_capture_module_) {
return false;
}
rtc::Thread* const signaling_thread = rtc::Thread::Current();
webrtc::PeerConnectionFactoryDependencies pc_factory_dependencies;
pc_factory_dependencies.network_thread = network_thread;
pc_factory_dependencies.worker_thread = worker_thread;
pc_factory_dependencies.signaling_thread = signaling_thread;
pc_factory_dependencies.task_queue_factory =
webrtc::CreateDefaultTaskQueueFactory();
pc_factory_dependencies.trials = std::make_unique<FieldTrialBasedConfig>();
pc_factory_dependencies.metronome =
std::make_unique<TaskQueueMetronome>(TimeDelta::Millis(8));
cricket::MediaEngineDependencies media_deps;
media_deps.task_queue_factory =
pc_factory_dependencies.task_queue_factory.get();
media_deps.adm = fake_audio_capture_module_;
webrtc::SetMediaEngineDefaults(&media_deps);
if (reset_encoder_factory) {
media_deps.video_encoder_factory.reset();
}
if (reset_decoder_factory) {
media_deps.video_decoder_factory.reset();
}
if (!media_deps.audio_processing) {
// If the standard Creation method for APM returns a null pointer, instead
// use the builder for testing to create an APM object.
media_deps.audio_processing = AudioProcessingBuilderForTesting().Create();
}
media_deps.trials = pc_factory_dependencies.trials.get();
if (create_media_engine) {
pc_factory_dependencies.media_engine =
cricket::CreateMediaEngine(std::move(media_deps));
}
pc_factory_dependencies.call_factory = webrtc::CreateCallFactory();
if (event_log_factory) {
event_log_factory_ = event_log_factory.get();
pc_factory_dependencies.event_log_factory = std::move(event_log_factory);
} else {
pc_factory_dependencies.event_log_factory =
std::make_unique<webrtc::RtcEventLogFactory>(
pc_factory_dependencies.task_queue_factory.get());
}
peer_connection_factory_ = webrtc::CreateModularPeerConnectionFactory(
std::move(pc_factory_dependencies));
if (!peer_connection_factory_) {
return false;
}
if (options) {
peer_connection_factory_->SetOptions(*options);
}
if (config) {
sdp_semantics_ = config->sdp_semantics;
}
dependencies.allocator = std::move(port_allocator);
peer_connection_ = CreatePeerConnection(config, std::move(dependencies));
return peer_connection_.get() != nullptr;
}
rtc::scoped_refptr<webrtc::PeerConnectionInterface> CreatePeerConnection(
const PeerConnectionInterface::RTCConfiguration* config,
webrtc::PeerConnectionDependencies dependencies) {
PeerConnectionInterface::RTCConfiguration modified_config;
modified_config.sdp_semantics = sdp_semantics_;
// If `config` is null, this will result in a default configuration being
// used.
if (config) {
modified_config = *config;
}
// Disable resolution adaptation; we don't want it interfering with the
// test results.
// TODO(deadbeef): Do something more robust. Since we're testing for aspect
// ratios and not specific resolutions, is this even necessary?
modified_config.set_cpu_adaptation(false);
dependencies.observer = this;
auto peer_connection_or_error =
peer_connection_factory_->CreatePeerConnectionOrError(
modified_config, std::move(dependencies));
return peer_connection_or_error.ok() ? peer_connection_or_error.MoveValue()
: nullptr;
}
void set_signaling_message_receiver(
SignalingMessageReceiver* signaling_message_receiver) {
signaling_message_receiver_ = signaling_message_receiver;
}
void set_signaling_delay_ms(int delay_ms) { signaling_delay_ms_ = delay_ms; }
void set_signal_ice_candidates(bool signal) {
signal_ice_candidates_ = signal;
}
rtc::scoped_refptr<webrtc::VideoTrackInterface> CreateLocalVideoTrackInternal(
webrtc::FakePeriodicVideoSource::Config config) {
// Set max frame rate to 10fps to reduce the risk of test flakiness.
// TODO(deadbeef): Do something more robust.
config.frame_interval_ms = 100;
video_track_sources_.emplace_back(
rtc::make_ref_counted<webrtc::FakePeriodicVideoTrackSource>(
config, false /* remote */));
rtc::scoped_refptr<webrtc::VideoTrackInterface> track(
peer_connection_factory_->CreateVideoTrack(
rtc::CreateRandomUuid(), video_track_sources_.back().get()));
if (!local_video_renderer_) {
local_video_renderer_.reset(
new webrtc::FakeVideoTrackRenderer(track.get()));
}
return track;
}
void HandleIncomingOffer(const std::string& msg) {
RTC_LOG(LS_INFO) << debug_name_ << ": HandleIncomingOffer";
std::unique_ptr<SessionDescriptionInterface> desc =
webrtc::CreateSessionDescription(SdpType::kOffer, msg);
if (received_sdp_munger_) {
received_sdp_munger_(desc->description());
}
EXPECT_TRUE(SetRemoteDescription(std::move(desc)));
// Setting a remote description may have changed the number of receivers,
// so reset the receiver observers.
ResetRtpReceiverObservers();
if (remote_offer_handler_) {
remote_offer_handler_();
}
auto answer = CreateAnswer();
ASSERT_NE(nullptr, answer);
EXPECT_TRUE(SetLocalDescriptionAndSendSdpMessage(std::move(answer)));
}
void HandleIncomingAnswer(const std::string& msg) {
RTC_LOG(LS_INFO) << debug_name_ << ": HandleIncomingAnswer";
std::unique_ptr<SessionDescriptionInterface> desc =
webrtc::CreateSessionDescription(SdpType::kAnswer, msg);
if (received_sdp_munger_) {
received_sdp_munger_(desc->description());
}
EXPECT_TRUE(SetRemoteDescription(std::move(desc)));
// Set the RtpReceiverObserver after receivers are created.
ResetRtpReceiverObservers();
}
// Returns null on failure.
std::unique_ptr<SessionDescriptionInterface> CreateAnswer() {
auto observer =
rtc::make_ref_counted<MockCreateSessionDescriptionObserver>();
pc()->CreateAnswer(observer.get(), offer_answer_options_);
return WaitForDescriptionFromObserver(observer.get());
}
std::unique_ptr<SessionDescriptionInterface> WaitForDescriptionFromObserver(
MockCreateSessionDescriptionObserver* observer) {
EXPECT_EQ_WAIT(true, observer->called(), kDefaultTimeout);
if (!observer->result()) {
return nullptr;
}
auto description = observer->MoveDescription();
if (generated_sdp_munger_) {
generated_sdp_munger_(description->description());
}
return description;
}
// Setting the local description and sending the SDP message over the fake
// signaling channel are combined into the same method because the SDP
// message needs to be sent as soon as SetLocalDescription finishes, without
// waiting for the observer to be called. This ensures that ICE candidates
// don't outrace the description.
bool SetLocalDescriptionAndSendSdpMessage(
std::unique_ptr<SessionDescriptionInterface> desc) {
auto observer = rtc::make_ref_counted<MockSetSessionDescriptionObserver>();
RTC_LOG(LS_INFO) << debug_name_ << ": SetLocalDescriptionAndSendSdpMessage";
SdpType type = desc->GetType();
std::string sdp;
EXPECT_TRUE(desc->ToString(&sdp));
RTC_LOG(LS_INFO) << debug_name_ << ": local SDP contents=\n" << sdp;
pc()->SetLocalDescription(observer.get(), desc.release());
RemoveUnusedVideoRenderers();
// As mentioned above, we need to send the message immediately after
// SetLocalDescription.
SendSdpMessage(type, sdp);
EXPECT_TRUE_WAIT(observer->called(), kDefaultTimeout);
return true;
}
bool SetRemoteDescription(std::unique_ptr<SessionDescriptionInterface> desc) {
auto observer = rtc::make_ref_counted<MockSetSessionDescriptionObserver>();
RTC_LOG(LS_INFO) << debug_name_ << ": SetRemoteDescription";
pc()->SetRemoteDescription(observer.get(), desc.release());
RemoveUnusedVideoRenderers();
EXPECT_TRUE_WAIT(observer->called(), kDefaultTimeout);
return observer->result();
}
// This is a work around to remove unused fake_video_renderers from
// transceivers that have either stopped or are no longer receiving.
void RemoveUnusedVideoRenderers() {
if (sdp_semantics_ != SdpSemantics::kUnifiedPlan) {
return;
}
auto transceivers = pc()->GetTransceivers();
std::set<std::string> active_renderers;
for (auto& transceiver : transceivers) {
// Note - we don't check for direction here. This function is called
// before direction is set, and in that case, we should not remove
// the renderer.
if (transceiver->receiver()->media_type() == cricket::MEDIA_TYPE_VIDEO) {
active_renderers.insert(transceiver->receiver()->track()->id());
}
}
for (auto it = fake_video_renderers_.begin();
it != fake_video_renderers_.end();) {
// Remove fake video renderers belonging to any non-active transceivers.
if (!active_renderers.count(it->first)) {
it = fake_video_renderers_.erase(it);
} else {
it++;
}
}
}
// Simulate sending a blob of SDP with delay `signaling_delay_ms_` (0 by
// default).
void SendSdpMessage(SdpType type, const std::string& msg) {
if (signaling_delay_ms_ == 0) {
RelaySdpMessageIfReceiverExists(type, msg);
} else {
rtc::Thread::Current()->PostDelayedTask(
SafeTask(task_safety_.flag(),
[this, type, msg] {
RelaySdpMessageIfReceiverExists(type, msg);
}),
TimeDelta::Millis(signaling_delay_ms_));
}
}
void RelaySdpMessageIfReceiverExists(SdpType type, const std::string& msg) {
if (signaling_message_receiver_) {
signaling_message_receiver_->ReceiveSdpMessage(type, msg);
}
}
// Simulate trickling an ICE candidate with delay `signaling_delay_ms_` (0 by
// default).
void SendIceMessage(const std::string& sdp_mid,
int sdp_mline_index,
const std::string& msg) {
if (signaling_delay_ms_ == 0) {
RelayIceMessageIfReceiverExists(sdp_mid, sdp_mline_index, msg);
} else {
rtc::Thread::Current()->PostDelayedTask(
SafeTask(task_safety_.flag(),
[this, sdp_mid, sdp_mline_index, msg] {
RelayIceMessageIfReceiverExists(sdp_mid, sdp_mline_index,
msg);
}),
TimeDelta::Millis(signaling_delay_ms_));
}
}
void RelayIceMessageIfReceiverExists(const std::string& sdp_mid,
int sdp_mline_index,
const std::string& msg) {
if (signaling_message_receiver_) {
signaling_message_receiver_->ReceiveIceMessage(sdp_mid, sdp_mline_index,
msg);
}
}
// SignalingMessageReceiver callbacks.
void ReceiveSdpMessage(SdpType type, const std::string& msg) override {
if (type == SdpType::kOffer) {
HandleIncomingOffer(msg);
} else {
HandleIncomingAnswer(msg);
}
}
void ReceiveIceMessage(const std::string& sdp_mid,
int sdp_mline_index,
const std::string& msg) override {
RTC_LOG(LS_INFO) << debug_name_ << ": ReceiveIceMessage";
absl::optional<RTCError> result;
pc()->AddIceCandidate(absl::WrapUnique(webrtc::CreateIceCandidate(
sdp_mid, sdp_mline_index, msg, nullptr)),
[&result](RTCError r) { result = r; });
EXPECT_TRUE_WAIT(result.has_value(), kDefaultTimeout);
EXPECT_TRUE(result.value().ok());
}
// PeerConnectionObserver callbacks.
void OnSignalingChange(
webrtc::PeerConnectionInterface::SignalingState new_state) override {
EXPECT_EQ(pc()->signaling_state(), new_state);
peer_connection_signaling_state_history_.push_back(new_state);
}
void OnAddTrack(rtc::scoped_refptr<RtpReceiverInterface> receiver,
const std::vector<rtc::scoped_refptr<MediaStreamInterface>>&
streams) override {
if (receiver->media_type() == cricket::MEDIA_TYPE_VIDEO) {
rtc::scoped_refptr<VideoTrackInterface> video_track(
static_cast<VideoTrackInterface*>(receiver->track().get()));
ASSERT_TRUE(fake_video_renderers_.find(video_track->id()) ==
fake_video_renderers_.end());
fake_video_renderers_[video_track->id()] =
std::make_unique<FakeVideoTrackRenderer>(video_track.get());
}
}
void OnRemoveTrack(
rtc::scoped_refptr<RtpReceiverInterface> receiver) override {
if (receiver->media_type() == cricket::MEDIA_TYPE_VIDEO) {
auto it = fake_video_renderers_.find(receiver->track()->id());
if (it != fake_video_renderers_.end()) {
fake_video_renderers_.erase(it);
} else {
RTC_LOG(LS_ERROR) << "OnRemoveTrack called for non-active renderer";
}
}
}
void OnRenegotiationNeeded() override {}
void OnIceConnectionChange(
webrtc::PeerConnectionInterface::IceConnectionState new_state) override {
EXPECT_EQ(pc()->ice_connection_state(), new_state);
ice_connection_state_history_.push_back(new_state);
}
void OnStandardizedIceConnectionChange(
webrtc::PeerConnectionInterface::IceConnectionState new_state) override {
standardized_ice_connection_state_history_.push_back(new_state);
}
void OnConnectionChange(
webrtc::PeerConnectionInterface::PeerConnectionState new_state) override {
peer_connection_state_history_.push_back(new_state);
}
void OnIceGatheringChange(
webrtc::PeerConnectionInterface::IceGatheringState new_state) override {
EXPECT_EQ(pc()->ice_gathering_state(), new_state);
ice_gathering_state_history_.push_back(new_state);
}
void OnIceSelectedCandidatePairChanged(
const cricket::CandidatePairChangeEvent& event) {
ice_candidate_pair_change_history_.push_back(event);
}
void OnIceCandidate(const webrtc::IceCandidateInterface* candidate) override {
RTC_LOG(LS_INFO) << debug_name_ << ": OnIceCandidate";
if (remote_async_resolver_) {
const auto& local_candidate = candidate->candidate();
if (local_candidate.address().IsUnresolvedIP()) {
RTC_DCHECK(local_candidate.type() == cricket::LOCAL_PORT_TYPE);
rtc::SocketAddress resolved_addr(local_candidate.address());
const auto resolved_ip = mdns_responder_->GetMappedAddressForName(
local_candidate.address().hostname());
RTC_DCHECK(!resolved_ip.IsNil());
resolved_addr.SetResolvedIP(resolved_ip);
EXPECT_CALL(*remote_async_resolver_, GetResolvedAddress(_, _))
.WillOnce(DoAll(SetArgPointee<1>(resolved_addr), Return(true)));
EXPECT_CALL(*remote_async_resolver_, Destroy(_));
}
}
// Check if we expected to have a candidate.
EXPECT_GT(candidates_expected_, 1);
candidates_expected_--;
std::string ice_sdp;
EXPECT_TRUE(candidate->ToString(&ice_sdp));
if (signaling_message_receiver_ == nullptr || !signal_ice_candidates_) {
// Remote party may be deleted.
return;
}
SendIceMessage(candidate->sdp_mid(), candidate->sdp_mline_index(), ice_sdp);
last_candidate_gathered_ = candidate->candidate();
}
void OnIceCandidateError(const std::string& address,
int port,
const std::string& url,
int error_code,
const std::string& error_text) override {
error_event_ = cricket::IceCandidateErrorEvent(address, port, url,
error_code, error_text);
}
void OnDataChannel(
rtc::scoped_refptr<DataChannelInterface> data_channel) override {
RTC_LOG(LS_INFO) << debug_name_ << ": OnDataChannel";
data_channels_.push_back(data_channel);
data_observers_.push_back(
std::make_unique<MockDataChannelObserver>(data_channel.get()));
}
std::string debug_name_;
std::unique_ptr<rtc::FakeNetworkManager> fake_network_manager_;
// Reference to the mDNS responder owned by `fake_network_manager_` after set.
webrtc::FakeMdnsResponder* mdns_responder_ = nullptr;
rtc::scoped_refptr<webrtc::PeerConnectionInterface> peer_connection_;
rtc::scoped_refptr<webrtc::PeerConnectionFactoryInterface>
peer_connection_factory_;
cricket::PortAllocator* port_allocator_;
// Needed to keep track of number of frames sent.
rtc::scoped_refptr<FakeAudioCaptureModule> fake_audio_capture_module_;
// Needed to keep track of number of frames received.
std::map<std::string, std::unique_ptr<webrtc::FakeVideoTrackRenderer>>
fake_video_renderers_;
// Needed to ensure frames aren't received for removed tracks.
std::vector<std::unique_ptr<webrtc::FakeVideoTrackRenderer>>
removed_fake_video_renderers_;
// For remote peer communication.
SignalingMessageReceiver* signaling_message_receiver_ = nullptr;
int signaling_delay_ms_ = 0;
bool signal_ice_candidates_ = true;
cricket::Candidate last_candidate_gathered_;
cricket::IceCandidateErrorEvent error_event_;
// Store references to the video sources we've created, so that we can stop
// them, if required.
std::vector<rtc::scoped_refptr<webrtc::VideoTrackSource>>
video_track_sources_;
// `local_video_renderer_` attached to the first created local video track.
std::unique_ptr<webrtc::FakeVideoTrackRenderer> local_video_renderer_;
SdpSemantics sdp_semantics_;
PeerConnectionInterface::RTCOfferAnswerOptions offer_answer_options_;
std::function<void(cricket::SessionDescription*)> received_sdp_munger_;
std::function<void(cricket::SessionDescription*)> generated_sdp_munger_;
std::function<void()> remote_offer_handler_;
rtc::MockAsyncResolver* remote_async_resolver_ = nullptr;
// All data channels either created or observed on this peerconnection
std::vector<rtc::scoped_refptr<DataChannelInterface>> data_channels_;
std::vector<std::unique_ptr<MockDataChannelObserver>> data_observers_;
std::vector<std::unique_ptr<MockRtpReceiverObserver>> rtp_receiver_observers_;
std::vector<PeerConnectionInterface::IceConnectionState>
ice_connection_state_history_;
std::vector<PeerConnectionInterface::IceConnectionState>
standardized_ice_connection_state_history_;
std::vector<PeerConnectionInterface::PeerConnectionState>
peer_connection_state_history_;
std::vector<PeerConnectionInterface::IceGatheringState>
ice_gathering_state_history_;
std::vector<cricket::CandidatePairChangeEvent>
ice_candidate_pair_change_history_;
std::vector<PeerConnectionInterface::SignalingState>
peer_connection_signaling_state_history_;
webrtc::FakeRtcEventLogFactory* event_log_factory_;
// Number of ICE candidates expected. The default is no limit.
int candidates_expected_ = std::numeric_limits<int>::max();
// Variables for tracking delay stats on an audio track
int audio_packets_stat_ = 0;
double audio_delay_stat_ = 0.0;
uint64_t audio_samples_stat_ = 0;
uint64_t audio_concealed_stat_ = 0;
std::string rtp_stats_id_;
ScopedTaskSafety task_safety_;
friend class PeerConnectionIntegrationBaseTest;
};
class MockRtcEventLogOutput : public webrtc::RtcEventLogOutput {
public:
virtual ~MockRtcEventLogOutput() = default;
MOCK_METHOD(bool, IsActive, (), (const, override));
MOCK_METHOD(bool, Write, (absl::string_view), (override));
};
// This helper object is used for both specifying how many audio/video frames
// are expected to be received for a caller/callee. It provides helper functions
// to specify these expectations. The object initially starts in a state of no
// expectations.
class MediaExpectations {
public:
enum ExpectFrames {
kExpectSomeFrames,
kExpectNoFrames,
kNoExpectation,
};
void ExpectBidirectionalAudioAndVideo() {
ExpectBidirectionalAudio();
ExpectBidirectionalVideo();
}
void ExpectBidirectionalAudio() {
CallerExpectsSomeAudio();
CalleeExpectsSomeAudio();
}
void ExpectNoAudio() {
CallerExpectsNoAudio();
CalleeExpectsNoAudio();
}
void ExpectBidirectionalVideo() {
CallerExpectsSomeVideo();
CalleeExpectsSomeVideo();
}
void ExpectNoVideo() {
CallerExpectsNoVideo();
CalleeExpectsNoVideo();
}
void CallerExpectsSomeAudioAndVideo() {
CallerExpectsSomeAudio();
CallerExpectsSomeVideo();
}
void CalleeExpectsSomeAudioAndVideo() {
CalleeExpectsSomeAudio();
CalleeExpectsSomeVideo();
}
// Caller's audio functions.
void CallerExpectsSomeAudio(
int expected_audio_frames = kDefaultExpectedAudioFrameCount) {
caller_audio_expectation_ = kExpectSomeFrames;
caller_audio_frames_expected_ = expected_audio_frames;
}
void CallerExpectsNoAudio() {
caller_audio_expectation_ = kExpectNoFrames;
caller_audio_frames_expected_ = 0;
}
// Caller's video functions.
void CallerExpectsSomeVideo(
int expected_video_frames = kDefaultExpectedVideoFrameCount) {
caller_video_expectation_ = kExpectSomeFrames;
caller_video_frames_expected_ = expected_video_frames;
}
void CallerExpectsNoVideo() {
caller_video_expectation_ = kExpectNoFrames;
caller_video_frames_expected_ = 0;
}
// Callee's audio functions.
void CalleeExpectsSomeAudio(
int expected_audio_frames = kDefaultExpectedAudioFrameCount) {
callee_audio_expectation_ = kExpectSomeFrames;
callee_audio_frames_expected_ = expected_audio_frames;
}
void CalleeExpectsNoAudio() {
callee_audio_expectation_ = kExpectNoFrames;
callee_audio_frames_expected_ = 0;
}
// Callee's video functions.
void CalleeExpectsSomeVideo(
int expected_video_frames = kDefaultExpectedVideoFrameCount) {
callee_video_expectation_ = kExpectSomeFrames;
callee_video_frames_expected_ = expected_video_frames;
}
void CalleeExpectsNoVideo() {
callee_video_expectation_ = kExpectNoFrames;
callee_video_frames_expected_ = 0;
}
ExpectFrames caller_audio_expectation_ = kNoExpectation;
ExpectFrames caller_video_expectation_ = kNoExpectation;
ExpectFrames callee_audio_expectation_ = kNoExpectation;
ExpectFrames callee_video_expectation_ = kNoExpectation;
int caller_audio_frames_expected_ = 0;
int caller_video_frames_expected_ = 0;
int callee_audio_frames_expected_ = 0;
int callee_video_frames_expected_ = 0;
};
class MockIceTransport : public webrtc::IceTransportInterface {
public:
MockIceTransport(const std::string& name, int component)
: internal_(std::make_unique<cricket::FakeIceTransport>(
name,
component,
nullptr /* network_thread */)) {}
~MockIceTransport() = default;
cricket::IceTransportInternal* internal() { return internal_.get(); }
private:
std::unique_ptr<cricket::FakeIceTransport> internal_;
};
class MockIceTransportFactory : public IceTransportFactory {
public:
~MockIceTransportFactory() override = default;
rtc::scoped_refptr<IceTransportInterface> CreateIceTransport(
const std::string& transport_name,
int component,
IceTransportInit init) {
RecordIceTransportCreated();
return rtc::make_ref_counted<MockIceTransport>(transport_name, component);
}
MOCK_METHOD(void, RecordIceTransportCreated, ());
};
// Tests two PeerConnections connecting to each other end-to-end, using a
// virtual network, fake A/V capture and fake encoder/decoders. The
// PeerConnections share the threads/socket servers, but use separate versions
// of everything else (including "PeerConnectionFactory"s).
class PeerConnectionIntegrationBaseTest : public ::testing::Test {
public:
PeerConnectionIntegrationBaseTest(
SdpSemantics sdp_semantics,
absl::optional<std::string> field_trials = absl::nullopt)
: sdp_semantics_(sdp_semantics),
ss_(new rtc::VirtualSocketServer()),
fss_(new rtc::FirewallSocketServer(ss_.get())),
network_thread_(new rtc::Thread(fss_.get())),
worker_thread_(rtc::Thread::Create()),
// TODO(bugs.webrtc.org/10335): Pass optional ScopedKeyValueConfig.
field_trials_(new test::ScopedKeyValueConfig(
field_trials.has_value() ? *field_trials : "")) {
network_thread_->SetName("PCNetworkThread", this);
worker_thread_->SetName("PCWorkerThread", this);
RTC_CHECK(network_thread_->Start());
RTC_CHECK(worker_thread_->Start());
webrtc::metrics::Reset();
}
~PeerConnectionIntegrationBaseTest() {
// The PeerConnections should be deleted before the TurnCustomizers.
// A TurnPort is created with a raw pointer to a TurnCustomizer. The
// TurnPort has the same lifetime as the PeerConnection, so it's expected
// that the TurnCustomizer outlives the life of the PeerConnection or else
// when Send() is called it will hit a seg fault.
if (caller_) {
caller_->set_signaling_message_receiver(nullptr);
caller_->pc()->Close();
delete SetCallerPcWrapperAndReturnCurrent(nullptr);
}
if (callee_) {
callee_->set_signaling_message_receiver(nullptr);
callee_->pc()->Close();
delete SetCalleePcWrapperAndReturnCurrent(nullptr);
}
// If turn servers were created for the test they need to be destroyed on
// the network thread.
SendTask(network_thread(), [this] {
turn_servers_.clear();
turn_customizers_.clear();
});
}
bool SignalingStateStable() {
return caller_->SignalingStateStable() && callee_->SignalingStateStable();
}
bool DtlsConnected() {
// TODO(deadbeef): kIceConnectionConnected currently means both ICE and DTLS
// are connected. This is an important distinction. Once we have separate
// ICE and DTLS state, this check needs to use the DTLS state.
return (callee()->ice_connection_state() ==
webrtc::PeerConnectionInterface::kIceConnectionConnected ||
callee()->ice_connection_state() ==
webrtc::PeerConnectionInterface::kIceConnectionCompleted) &&
(caller()->ice_connection_state() ==
webrtc::PeerConnectionInterface::kIceConnectionConnected ||
caller()->ice_connection_state() ==
webrtc::PeerConnectionInterface::kIceConnectionCompleted);
}
// When `event_log_factory` is null, the default implementation of the event
// log factory will be used.
std::unique_ptr<PeerConnectionIntegrationWrapper> CreatePeerConnectionWrapper(
const std::string& debug_name,
const PeerConnectionFactory::Options* options,
const RTCConfiguration* config,
webrtc::PeerConnectionDependencies dependencies,
std::unique_ptr<webrtc::FakeRtcEventLogFactory> event_log_factory,
bool reset_encoder_factory,
bool reset_decoder_factory,
bool create_media_engine = true) {
RTCConfiguration modified_config;
if (config) {
modified_config = *config;
}
modified_config.sdp_semantics = sdp_semantics_;
if (!dependencies.cert_generator) {
dependencies.cert_generator =
std::make_unique<FakeRTCCertificateGenerator>();
}
std::unique_ptr<PeerConnectionIntegrationWrapper> client(
new PeerConnectionIntegrationWrapper(debug_name));
if (!client->Init(options, &modified_config, std::move(dependencies),
fss_.get(), network_thread_.get(), worker_thread_.get(),
std::move(event_log_factory), reset_encoder_factory,
reset_decoder_factory, create_media_engine)) {
return nullptr;
}
return client;
}
std::unique_ptr<PeerConnectionIntegrationWrapper>
CreatePeerConnectionWrapperWithFakeRtcEventLog(
const std::string& debug_name,
const PeerConnectionFactory::Options* options,
const RTCConfiguration* config,
webrtc::PeerConnectionDependencies dependencies) {
return CreatePeerConnectionWrapper(
debug_name, options, config, std::move(dependencies),
std::make_unique<webrtc::FakeRtcEventLogFactory>(),
/*reset_encoder_factory=*/false,
/*reset_decoder_factory=*/false);
}
bool CreatePeerConnectionWrappers() {
return CreatePeerConnectionWrappersWithConfig(
PeerConnectionInterface::RTCConfiguration(),
PeerConnectionInterface::RTCConfiguration());
}
bool CreatePeerConnectionWrappersWithSdpSemantics(
SdpSemantics caller_semantics,
SdpSemantics callee_semantics) {
// Can't specify the sdp_semantics in the passed-in configuration since it
// will be overwritten by CreatePeerConnectionWrapper with whatever is
// stored in sdp_semantics_. So get around this by modifying the instance
// variable before calling CreatePeerConnectionWrapper for the caller and
// callee PeerConnections.
SdpSemantics original_semantics = sdp_semantics_;
sdp_semantics_ = caller_semantics;
caller_ = CreatePeerConnectionWrapper(
"Caller", nullptr, nullptr, webrtc::PeerConnectionDependencies(nullptr),
nullptr,
/*reset_encoder_factory=*/false,
/*reset_decoder_factory=*/false);
sdp_semantics_ = callee_semantics;
callee_ = CreatePeerConnectionWrapper(
"Callee", nullptr, nullptr, webrtc::PeerConnectionDependencies(nullptr),
nullptr,
/*reset_encoder_factory=*/false,
/*reset_decoder_factory=*/false);
sdp_semantics_ = original_semantics;
return caller_ && callee_;
}
bool CreatePeerConnectionWrappersWithConfig(
const PeerConnectionInterface::RTCConfiguration& caller_config,
const PeerConnectionInterface::RTCConfiguration& callee_config) {
caller_ = CreatePeerConnectionWrapper(
"Caller", nullptr, &caller_config,
webrtc::PeerConnectionDependencies(nullptr), nullptr,
/*reset_encoder_factory=*/false,
/*reset_decoder_factory=*/false);
callee_ = CreatePeerConnectionWrapper(
"Callee", nullptr, &callee_config,
webrtc::PeerConnectionDependencies(nullptr), nullptr,
/*reset_encoder_factory=*/false,
/*reset_decoder_factory=*/false);
return caller_ && callee_;
}
bool CreatePeerConnectionWrappersWithConfigAndDeps(
const PeerConnectionInterface::RTCConfiguration& caller_config,
webrtc::PeerConnectionDependencies caller_dependencies,
const PeerConnectionInterface::RTCConfiguration& callee_config,
webrtc::PeerConnectionDependencies callee_dependencies) {
caller_ =
CreatePeerConnectionWrapper("Caller", nullptr, &caller_config,
std::move(caller_dependencies), nullptr,
/*reset_encoder_factory=*/false,
/*reset_decoder_factory=*/false);
callee_ =
CreatePeerConnectionWrapper("Callee", nullptr, &callee_config,
std::move(callee_dependencies), nullptr,
/*reset_encoder_factory=*/false,
/*reset_decoder_factory=*/false);
return caller_ && callee_;
}
bool CreatePeerConnectionWrappersWithOptions(
const PeerConnectionFactory::Options& caller_options,
const PeerConnectionFactory::Options& callee_options) {
caller_ = CreatePeerConnectionWrapper(
"Caller", &caller_options, nullptr,
webrtc::PeerConnectionDependencies(nullptr), nullptr,
/*reset_encoder_factory=*/false,
/*reset_decoder_factory=*/false);
callee_ = CreatePeerConnectionWrapper(
"Callee", &callee_options, nullptr,
webrtc::PeerConnectionDependencies(nullptr), nullptr,
/*reset_encoder_factory=*/false,
/*reset_decoder_factory=*/false);
return caller_ && callee_;
}
bool CreatePeerConnectionWrappersWithFakeRtcEventLog() {
PeerConnectionInterface::RTCConfiguration default_config;
caller_ = CreatePeerConnectionWrapperWithFakeRtcEventLog(
"Caller", nullptr, &default_config,
webrtc::PeerConnectionDependencies(nullptr));
callee_ = CreatePeerConnectionWrapperWithFakeRtcEventLog(
"Callee", nullptr, &default_config,
webrtc::PeerConnectionDependencies(nullptr));
return caller_ && callee_;
}
std::unique_ptr<PeerConnectionIntegrationWrapper>
CreatePeerConnectionWrapperWithAlternateKey() {
std::unique_ptr<FakeRTCCertificateGenerator> cert_generator(
new FakeRTCCertificateGenerator());
cert_generator->use_alternate_key();
webrtc::PeerConnectionDependencies dependencies(nullptr);
dependencies.cert_generator = std::move(cert_generator);
return CreatePeerConnectionWrapper("New Peer", nullptr, nullptr,
std::move(dependencies), nullptr,
/*reset_encoder_factory=*/false,
/*reset_decoder_factory=*/false);
}
bool CreateOneDirectionalPeerConnectionWrappers(bool caller_to_callee) {
caller_ = CreatePeerConnectionWrapper(
"Caller", nullptr, nullptr, webrtc::PeerConnectionDependencies(nullptr),
nullptr,
/*reset_encoder_factory=*/!caller_to_callee,
/*reset_decoder_factory=*/caller_to_callee);
callee_ = CreatePeerConnectionWrapper(
"Callee", nullptr, nullptr, webrtc::PeerConnectionDependencies(nullptr),
nullptr,
/*reset_encoder_factory=*/caller_to_callee,
/*reset_decoder_factory=*/!caller_to_callee);
return caller_ && callee_;
}
bool CreatePeerConnectionWrappersWithoutMediaEngine() {
caller_ = CreatePeerConnectionWrapper(
"Caller", nullptr, nullptr, webrtc::PeerConnectionDependencies(nullptr),
nullptr,
/*reset_encoder_factory=*/false,
/*reset_decoder_factory=*/false,
/*create_media_engine=*/false);
callee_ = CreatePeerConnectionWrapper(
"Callee", nullptr, nullptr, webrtc::PeerConnectionDependencies(nullptr),
nullptr,
/*reset_encoder_factory=*/false,
/*reset_decoder_factory=*/false,
/*create_media_engine=*/false);
return caller_ && callee_;
}
cricket::TestTurnServer* CreateTurnServer(
rtc::SocketAddress internal_address,
rtc::SocketAddress external_address,
cricket::ProtocolType type = cricket::ProtocolType::PROTO_UDP,
const std::string& common_name = "test turn server") {
rtc::Thread* thread = network_thread();
rtc::SocketFactory* socket_factory = fss_.get();
std::unique_ptr<cricket::TestTurnServer> turn_server;
SendTask(network_thread(), [&] {
turn_server = std::make_unique<cricket::TestTurnServer>(
thread, socket_factory, internal_address, external_address, type,
/*ignore_bad_certs=*/true, common_name);
});
turn_servers_.push_back(std::move(turn_server));
// Interactions with the turn server should be done on the network thread.
return turn_servers_.back().get();
}
cricket::TestTurnCustomizer* CreateTurnCustomizer() {
std::unique_ptr<cricket::TestTurnCustomizer> turn_customizer;
SendTask(network_thread(), [&] {
turn_customizer = std::make_unique<cricket::TestTurnCustomizer>();
});
turn_customizers_.push_back(std::move(turn_customizer));
// Interactions with the turn customizer should be done on the network
// thread.
return turn_customizers_.back().get();
}
// Checks that the function counters for a TestTurnCustomizer are greater than
// 0.
void ExpectTurnCustomizerCountersIncremented(
cricket::TestTurnCustomizer* turn_customizer) {
SendTask(network_thread(), [turn_customizer] {
EXPECT_GT(turn_customizer->allow_channel_data_cnt_, 0u);
EXPECT_GT(turn_customizer->modify_cnt_, 0u);
});
}
// Once called, SDP blobs and ICE candidates will be automatically signaled
// between PeerConnections.
void ConnectFakeSignaling() {
caller_->set_signaling_message_receiver(callee_.get());
callee_->set_signaling_message_receiver(caller_.get());
}
// Once called, SDP blobs will be automatically signaled between
// PeerConnections. Note that ICE candidates will not be signaled unless they
// are in the exchanged SDP blobs.
void ConnectFakeSignalingForSdpOnly() {
ConnectFakeSignaling();
SetSignalIceCandidates(false);
}
void SetSignalingDelayMs(int delay_ms) {
caller_->set_signaling_delay_ms(delay_ms);
callee_->set_signaling_delay_ms(delay_ms);
}
void SetSignalIceCandidates(bool signal) {
caller_->set_signal_ice_candidates(signal);
callee_->set_signal_ice_candidates(signal);
}
// Messages may get lost on the unreliable DataChannel, so we send multiple
// times to avoid test flakiness.
void SendRtpDataWithRetries(webrtc::DataChannelInterface* dc,
const std::string& data,
int retries) {
for (int i = 0; i < retries; ++i) {
dc->Send(DataBuffer(data));
}
}
rtc::Thread* network_thread() { return network_thread_.get(); }
rtc::VirtualSocketServer* virtual_socket_server() { return ss_.get(); }
PeerConnectionIntegrationWrapper* caller() { return caller_.get(); }
// Destroy peerconnections.
// This can be used to ensure that all pointers to on-stack mocks
// get dropped before exit.
void DestroyPeerConnections() {
if (caller_) {
caller_->pc()->Close();
}
if (callee_) {
callee_->pc()->Close();
}
caller_.reset();
callee_.reset();
}
// Set the `caller_` to the `wrapper` passed in and return the
// original `caller_`.
PeerConnectionIntegrationWrapper* SetCallerPcWrapperAndReturnCurrent(
PeerConnectionIntegrationWrapper* wrapper) {
PeerConnectionIntegrationWrapper* old = caller_.release();
caller_.reset(wrapper);
return old;
}
PeerConnectionIntegrationWrapper* callee() { return callee_.get(); }
// Set the `callee_` to the `wrapper` passed in and return the
// original `callee_`.
PeerConnectionIntegrationWrapper* SetCalleePcWrapperAndReturnCurrent(
PeerConnectionIntegrationWrapper* wrapper) {
PeerConnectionIntegrationWrapper* old = callee_.release();
callee_.reset(wrapper);
return old;
}
void SetPortAllocatorFlags(uint32_t caller_flags, uint32_t callee_flags) {
SendTask(network_thread(), [this, caller_flags] {
caller()->port_allocator()->set_flags(caller_flags);
});
SendTask(network_thread(), [this, callee_flags] {
callee()->port_allocator()->set_flags(callee_flags);
});
}
rtc::FirewallSocketServer* firewall() const { return fss_.get(); }
// Expects the provided number of new frames to be received within
// kMaxWaitForFramesMs. The new expected frames are specified in
// `media_expectations`. Returns false if any of the expectations were
// not met.
bool ExpectNewFrames(const MediaExpectations& media_expectations) {
// Make sure there are no bogus tracks confusing the issue.
caller()->RemoveUnusedVideoRenderers();
callee()->RemoveUnusedVideoRenderers();
// First initialize the expected frame counts based upon the current
// frame count.
int total_caller_audio_frames_expected = caller()->audio_frames_received();
if (media_expectations.caller_audio_expectation_ ==
MediaExpectations::kExpectSomeFrames) {
total_caller_audio_frames_expected +=
media_expectations.caller_audio_frames_expected_;
}
int total_caller_video_frames_expected =
caller()->min_video_frames_received_per_track();
if (media_expectations.caller_video_expectation_ ==
MediaExpectations::kExpectSomeFrames) {
total_caller_video_frames_expected +=
media_expectations.caller_video_frames_expected_;
}
int total_callee_audio_frames_expected = callee()->audio_frames_received();
if (media_expectations.callee_audio_expectation_ ==
MediaExpectations::kExpectSomeFrames) {
total_callee_audio_frames_expected +=
media_expectations.callee_audio_frames_expected_;
}
int total_callee_video_frames_expected =
callee()->min_video_frames_received_per_track();
if (media_expectations.callee_video_expectation_ ==
MediaExpectations::kExpectSomeFrames) {
total_callee_video_frames_expected +=
media_expectations.callee_video_frames_expected_;
}
// Wait for the expected frames.
EXPECT_TRUE_WAIT(caller()->audio_frames_received() >=
total_caller_audio_frames_expected &&
caller()->min_video_frames_received_per_track() >=
total_caller_video_frames_expected &&
callee()->audio_frames_received() >=
total_callee_audio_frames_expected &&
callee()->min_video_frames_received_per_track() >=
total_callee_video_frames_expected,
kMaxWaitForFramesMs);
bool expectations_correct =
caller()->audio_frames_received() >=
total_caller_audio_frames_expected &&
caller()->min_video_frames_received_per_track() >=
total_caller_video_frames_expected &&
callee()->audio_frames_received() >=
total_callee_audio_frames_expected &&
callee()->min_video_frames_received_per_track() >=
total_callee_video_frames_expected;
// After the combined wait, print out a more detailed message upon
// failure.
EXPECT_GE(caller()->audio_frames_received(),
total_caller_audio_frames_expected);
EXPECT_GE(caller()->min_video_frames_received_per_track(),
total_caller_video_frames_expected);
EXPECT_GE(callee()->audio_frames_received(),
total_callee_audio_frames_expected);
EXPECT_GE(callee()->min_video_frames_received_per_track(),
total_callee_video_frames_expected);
// We want to make sure nothing unexpected was received.
if (media_expectations.caller_audio_expectation_ ==
MediaExpectations::kExpectNoFrames) {
EXPECT_EQ(caller()->audio_frames_received(),
total_caller_audio_frames_expected);
if (caller()->audio_frames_received() !=
total_caller_audio_frames_expected) {
expectations_correct = false;
}
}
if (media_expectations.caller_video_expectation_ ==
MediaExpectations::kExpectNoFrames) {
EXPECT_EQ(caller()->min_video_frames_received_per_track(),
total_caller_video_frames_expected);
if (caller()->min_video_frames_received_per_track() !=
total_caller_video_frames_expected) {
expectations_correct = false;
}
}
if (media_expectations.callee_audio_expectation_ ==
MediaExpectations::kExpectNoFrames) {
EXPECT_EQ(callee()->audio_frames_received(),
total_callee_audio_frames_expected);
if (callee()->audio_frames_received() !=
total_callee_audio_frames_expected) {
expectations_correct = false;
}
}
if (media_expectations.callee_video_expectation_ ==
MediaExpectations::kExpectNoFrames) {
EXPECT_EQ(callee()->min_video_frames_received_per_track(),
total_callee_video_frames_expected);
if (callee()->min_video_frames_received_per_track() !=
total_callee_video_frames_expected) {
expectations_correct = false;
}
}
return expectations_correct;
}
void ClosePeerConnections() {
if (caller())
caller()->pc()->Close();
if (callee())
callee()->pc()->Close();
}
void TestNegotiatedCipherSuite(
const PeerConnectionFactory::Options& caller_options,
const PeerConnectionFactory::Options& callee_options,
int expected_cipher_suite) {
ASSERT_TRUE(CreatePeerConnectionWrappersWithOptions(caller_options,
callee_options));
ConnectFakeSignaling();
caller()->AddAudioVideoTracks();
callee()->AddAudioVideoTracks();
caller()->CreateAndSetAndSignalOffer();
ASSERT_TRUE_WAIT(DtlsConnected(), kDefaultTimeout);
EXPECT_EQ_WAIT(rtc::SrtpCryptoSuiteToName(expected_cipher_suite),
caller()->OldGetStats()->SrtpCipher(), kDefaultTimeout);
// TODO(bugs.webrtc.org/9456): Fix it.
EXPECT_METRIC_EQ(1, webrtc::metrics::NumEvents(
"WebRTC.PeerConnection.SrtpCryptoSuite.Audio",
expected_cipher_suite));
}
void TestGcmNegotiationUsesCipherSuite(bool local_gcm_enabled,
bool remote_gcm_enabled,
bool aes_ctr_enabled,
int expected_cipher_suite) {
PeerConnectionFactory::Options caller_options;
caller_options.crypto_options.srtp.enable_gcm_crypto_suites =
local_gcm_enabled;
caller_options.crypto_options.srtp.enable_aes128_sha1_80_crypto_cipher =
aes_ctr_enabled;
PeerConnectionFactory::Options callee_options;
callee_options.crypto_options.srtp.enable_gcm_crypto_suites =
remote_gcm_enabled;
callee_options.crypto_options.srtp.enable_aes128_sha1_80_crypto_cipher =
aes_ctr_enabled;
TestNegotiatedCipherSuite(caller_options, callee_options,
expected_cipher_suite);
}
const FieldTrialsView& trials() const { return *field_trials_.get(); }
protected:
SdpSemantics sdp_semantics_;
private:
rtc::AutoThread main_thread_; // Used as the signal thread by most tests.
// `ss_` is used by `network_thread_` so it must be destroyed later.
std::unique_ptr<rtc::VirtualSocketServer> ss_;
std::unique_ptr<rtc::FirewallSocketServer> fss_;
// `network_thread_` and `worker_thread_` are used by both
// `caller_` and `callee_` so they must be destroyed
// later.
std::unique_ptr<rtc::Thread> network_thread_;
std::unique_ptr<rtc::Thread> worker_thread_;
// The turn servers and turn customizers should be accessed & deleted on the
// network thread to avoid a race with the socket read/write that occurs
// on the network thread.
std::vector<std::unique_ptr<cricket::TestTurnServer>> turn_servers_;
std::vector<std::unique_ptr<cricket::TestTurnCustomizer>> turn_customizers_;
std::unique_ptr<PeerConnectionIntegrationWrapper> caller_;
std::unique_ptr<PeerConnectionIntegrationWrapper> callee_;
std::unique_ptr<FieldTrialsView> field_trials_;
};
} // namespace webrtc
#endif // PC_TEST_INTEGRATION_TEST_HELPERS_H_
|