1 /*
2  *  Copyright 2012 The WebRTC project authors. All Rights Reserved.
3  *
4  *  Use of this source code is governed by a BSD-style license
5  *  that can be found in the LICENSE file in the root of the source
6  *  tree. An additional intellectual property rights grant can be found
7  *  in the file PATENTS.  All contributing project authors may
8  *  be found in the AUTHORS file in the root of the source tree.
9  */
10 
11 #include "api/peer_connection_interface.h"
12 
13 #include <limits.h>
14 #include <stdint.h>
15 #include <string.h>
16 
17 #include <memory>
18 #include <string>
19 #include <utility>
20 #include <vector>
21 
22 #include "absl/strings/str_replace.h"
23 #include "absl/types/optional.h"
24 #include "api/audio/audio_mixer.h"
25 #include "api/audio_codecs/audio_decoder_factory.h"
26 #include "api/audio_codecs/audio_encoder_factory.h"
27 #include "api/audio_codecs/builtin_audio_decoder_factory.h"
28 #include "api/audio_codecs/builtin_audio_encoder_factory.h"
29 #include "api/call/call_factory_interface.h"
30 #include "api/create_peerconnection_factory.h"
31 #include "api/data_channel_interface.h"
32 #include "api/jsep.h"
33 #include "api/jsep_session_description.h"
34 #include "api/media_stream_interface.h"
35 #include "api/media_types.h"
36 #include "api/rtc_error.h"
37 #include "api/rtc_event_log/rtc_event_log.h"
38 #include "api/rtc_event_log/rtc_event_log_factory.h"
39 #include "api/rtc_event_log_output.h"
40 #include "api/rtc_event_log_output_file.h"
41 #include "api/rtp_receiver_interface.h"
42 #include "api/rtp_sender_interface.h"
43 #include "api/rtp_transceiver_interface.h"
44 #include "api/scoped_refptr.h"
45 #include "api/task_queue/default_task_queue_factory.h"
46 #include "api/transport/field_trial_based_config.h"
47 #include "api/video_codecs/builtin_video_decoder_factory.h"
48 #include "api/video_codecs/builtin_video_encoder_factory.h"
49 #include "api/video_codecs/video_decoder_factory.h"
50 #include "api/video_codecs/video_encoder_factory.h"
51 #include "media/base/codec.h"
52 #include "media/base/media_config.h"
53 #include "media/base/media_engine.h"
54 #include "media/base/stream_params.h"
55 #include "media/engine/webrtc_media_engine.h"
56 #include "media/engine/webrtc_media_engine_defaults.h"
57 #include "media/sctp/sctp_transport_internal.h"
58 #include "modules/audio_device/include/audio_device.h"
59 #include "modules/audio_processing/include/audio_processing.h"
60 #include "p2p/base/fake_port_allocator.h"
61 #include "p2p/base/p2p_constants.h"
62 #include "p2p/base/port.h"
63 #include "p2p/base/port_allocator.h"
64 #include "p2p/base/transport_description.h"
65 #include "p2p/base/transport_info.h"
66 #include "pc/audio_track.h"
67 #include "pc/media_session.h"
68 #include "pc/media_stream.h"
69 #include "pc/peer_connection.h"
70 #include "pc/peer_connection_factory.h"
71 #include "pc/rtc_stats_collector.h"
72 #include "pc/rtp_sender.h"
73 #include "pc/session_description.h"
74 #include "pc/stream_collection.h"
75 #include "pc/test/fake_audio_capture_module.h"
76 #include "pc/test/fake_rtc_certificate_generator.h"
77 #include "pc/test/fake_video_track_source.h"
78 #include "pc/test/mock_peer_connection_observers.h"
79 #include "pc/test/test_sdp_strings.h"
80 #include "pc/video_track.h"
81 #include "rtc_base/checks.h"
82 #include "rtc_base/copy_on_write_buffer.h"
83 #include "rtc_base/gunit.h"
84 #include "rtc_base/ref_counted_object.h"
85 #include "rtc_base/rtc_certificate_generator.h"
86 #include "rtc_base/socket_address.h"
87 #include "rtc_base/thread.h"
88 #include "rtc_base/time_utils.h"
89 #include "rtc_base/virtual_socket_server.h"
90 #include "test/gmock.h"
91 #include "test/gtest.h"
92 #include "test/testsupport/file_utils.h"
93 
94 #ifdef WEBRTC_ANDROID
95 #include "pc/test/android_test_initializer.h"
96 #endif
97 
98 namespace webrtc {
99 namespace {
100 
101 static const char kStreamId1[] = "local_stream_1";
102 static const char kStreamId2[] = "local_stream_2";
103 static const char kStreamId3[] = "local_stream_3";
104 static const int kDefaultStunPort = 3478;
105 static const char kStunAddressOnly[] = "stun:address";
106 static const char kStunInvalidPort[] = "stun:address:-1";
107 static const char kStunAddressPortAndMore1[] = "stun:address:port:more";
108 static const char kStunAddressPortAndMore2[] = "stun:address:port more";
109 static const char kTurnIceServerUri[] = "turn:turn.example.org";
110 static const char kTurnUsername[] = "user";
111 static const char kTurnPassword[] = "password";
112 static const char kTurnHostname[] = "turn.example.org";
113 static const uint32_t kTimeout = 10000U;
114 
115 static const char kStreams[][8] = {"stream1", "stream2"};
116 static const char kAudioTracks[][32] = {"audiotrack0", "audiotrack1"};
117 static const char kVideoTracks[][32] = {"videotrack0", "videotrack1"};
118 
119 static const char kRecvonly[] = "recvonly";
120 static const char kSendrecv[] = "sendrecv";
121 
122 // Reference SDP with a MediaStream with label "stream1" and audio track with
123 // id "audio_1" and a video track with id "video_1;
124 static const char kSdpStringWithStream1PlanB[] =
125     "v=0\r\n"
126     "o=- 0 0 IN IP4 127.0.0.1\r\n"
127     "s=-\r\n"
128     "t=0 0\r\n"
129     "m=audio 1 RTP/AVPF 103\r\n"
130     "a=ice-ufrag:e5785931\r\n"
131     "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
132     "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
133     "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
134     "a=mid:audio\r\n"
135     "a=sendrecv\r\n"
136     "a=rtcp-mux\r\n"
137     "a=rtpmap:103 ISAC/16000\r\n"
138     "a=ssrc:1 cname:stream1\r\n"
139     "a=ssrc:1 mslabel:stream1\r\n"
140     "a=ssrc:1 label:audiotrack0\r\n"
141     "m=video 1 RTP/AVPF 120\r\n"
142     "a=ice-ufrag:e5785931\r\n"
143     "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
144     "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
145     "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
146     "a=mid:video\r\n"
147     "a=sendrecv\r\n"
148     "a=rtcp-mux\r\n"
149     "a=rtpmap:120 VP8/90000\r\n"
150     "a=ssrc:2 cname:stream1\r\n"
151     "a=ssrc:2 mslabel:stream1\r\n"
152     "a=ssrc:2 label:videotrack0\r\n";
153 // Same string as above but with the MID changed to the Unified Plan default.
154 // This is needed so that this SDP can be used as an answer for a Unified Plan
155 // offer.
156 static const char kSdpStringWithStream1UnifiedPlan[] =
157     "v=0\r\n"
158     "o=- 0 0 IN IP4 127.0.0.1\r\n"
159     "s=-\r\n"
160     "t=0 0\r\n"
161     "m=audio 1 RTP/AVPF 103\r\n"
162     "a=ice-ufrag:e5785931\r\n"
163     "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
164     "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
165     "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
166     "a=mid:0\r\n"
167     "a=sendrecv\r\n"
168     "a=rtcp-mux\r\n"
169     "a=rtpmap:103 ISAC/16000\r\n"
170     "a=ssrc:1 cname:stream1\r\n"
171     "a=ssrc:1 mslabel:stream1\r\n"
172     "a=ssrc:1 label:audiotrack0\r\n"
173     "m=video 1 RTP/AVPF 120\r\n"
174     "a=ice-ufrag:e5785931\r\n"
175     "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
176     "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
177     "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
178     "a=mid:1\r\n"
179     "a=sendrecv\r\n"
180     "a=rtcp-mux\r\n"
181     "a=rtpmap:120 VP8/90000\r\n"
182     "a=ssrc:2 cname:stream1\r\n"
183     "a=ssrc:2 mslabel:stream1\r\n"
184     "a=ssrc:2 label:videotrack0\r\n";
185 
186 // Reference SDP with a MediaStream with label "stream1" and audio track with
187 // id "audio_1";
188 static const char kSdpStringWithStream1AudioTrackOnly[] =
189     "v=0\r\n"
190     "o=- 0 0 IN IP4 127.0.0.1\r\n"
191     "s=-\r\n"
192     "t=0 0\r\n"
193     "m=audio 1 RTP/AVPF 103\r\n"
194     "a=ice-ufrag:e5785931\r\n"
195     "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
196     "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
197     "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
198     "a=mid:audio\r\n"
199     "a=sendrecv\r\n"
200     "a=rtpmap:103 ISAC/16000\r\n"
201     "a=ssrc:1 cname:stream1\r\n"
202     "a=ssrc:1 mslabel:stream1\r\n"
203     "a=ssrc:1 label:audiotrack0\r\n"
204     "a=rtcp-mux\r\n";
205 
206 // Reference SDP with two MediaStreams with label "stream1" and "stream2. Each
207 // MediaStreams have one audio track and one video track.
208 // This uses MSID.
209 static const char kSdpStringWithStream1And2PlanB[] =
210     "v=0\r\n"
211     "o=- 0 0 IN IP4 127.0.0.1\r\n"
212     "s=-\r\n"
213     "t=0 0\r\n"
214     "a=msid-semantic: WMS stream1 stream2\r\n"
215     "m=audio 1 RTP/AVPF 103\r\n"
216     "a=ice-ufrag:e5785931\r\n"
217     "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
218     "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
219     "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
220     "a=mid:audio\r\n"
221     "a=sendrecv\r\n"
222     "a=rtcp-mux\r\n"
223     "a=rtpmap:103 ISAC/16000\r\n"
224     "a=ssrc:1 cname:stream1\r\n"
225     "a=ssrc:1 msid:stream1 audiotrack0\r\n"
226     "a=ssrc:3 cname:stream2\r\n"
227     "a=ssrc:3 msid:stream2 audiotrack1\r\n"
228     "m=video 1 RTP/AVPF 120\r\n"
229     "a=ice-ufrag:e5785931\r\n"
230     "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
231     "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
232     "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
233     "a=mid:video\r\n"
234     "a=sendrecv\r\n"
235     "a=rtcp-mux\r\n"
236     "a=rtpmap:120 VP8/0\r\n"
237     "a=ssrc:2 cname:stream1\r\n"
238     "a=ssrc:2 msid:stream1 videotrack0\r\n"
239     "a=ssrc:4 cname:stream2\r\n"
240     "a=ssrc:4 msid:stream2 videotrack1\r\n";
241 static const char kSdpStringWithStream1And2UnifiedPlan[] =
242     "v=0\r\n"
243     "o=- 0 0 IN IP4 127.0.0.1\r\n"
244     "s=-\r\n"
245     "t=0 0\r\n"
246     "a=msid-semantic: WMS stream1 stream2\r\n"
247     "m=audio 1 RTP/AVPF 103\r\n"
248     "a=ice-ufrag:e5785931\r\n"
249     "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
250     "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
251     "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
252     "a=mid:0\r\n"
253     "a=sendrecv\r\n"
254     "a=rtcp-mux\r\n"
255     "a=rtpmap:103 ISAC/16000\r\n"
256     "a=ssrc:1 cname:stream1\r\n"
257     "a=ssrc:1 msid:stream1 audiotrack0\r\n"
258     "m=video 1 RTP/AVPF 120\r\n"
259     "a=ice-ufrag:e5785931\r\n"
260     "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
261     "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
262     "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
263     "a=mid:1\r\n"
264     "a=sendrecv\r\n"
265     "a=rtcp-mux\r\n"
266     "a=rtpmap:120 VP8/0\r\n"
267     "a=ssrc:2 cname:stream1\r\n"
268     "a=ssrc:2 msid:stream1 videotrack0\r\n"
269     "m=audio 1 RTP/AVPF 103\r\n"
270     "a=ice-ufrag:e5785931\r\n"
271     "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
272     "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
273     "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
274     "a=mid:2\r\n"
275     "a=sendrecv\r\n"
276     "a=rtcp-mux\r\n"
277     "a=rtpmap:103 ISAC/16000\r\n"
278     "a=ssrc:3 cname:stream2\r\n"
279     "a=ssrc:3 msid:stream2 audiotrack1\r\n"
280     "m=video 1 RTP/AVPF 120\r\n"
281     "a=ice-ufrag:e5785931\r\n"
282     "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
283     "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
284     "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
285     "a=mid:3\r\n"
286     "a=sendrecv\r\n"
287     "a=rtcp-mux\r\n"
288     "a=rtpmap:120 VP8/0\r\n"
289     "a=ssrc:4 cname:stream2\r\n"
290     "a=ssrc:4 msid:stream2 videotrack1\r\n";
291 
292 // Reference SDP without MediaStreams. Msid is not supported.
293 static const char kSdpStringWithoutStreams[] =
294     "v=0\r\n"
295     "o=- 0 0 IN IP4 127.0.0.1\r\n"
296     "s=-\r\n"
297     "t=0 0\r\n"
298     "m=audio 1 RTP/AVPF 103\r\n"
299     "a=ice-ufrag:e5785931\r\n"
300     "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
301     "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
302     "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
303     "a=mid:audio\r\n"
304     "a=sendrecv\r\n"
305     "a=rtcp-mux\r\n"
306     "a=rtpmap:103 ISAC/16000\r\n"
307     "m=video 1 RTP/AVPF 120\r\n"
308     "a=ice-ufrag:e5785931\r\n"
309     "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
310     "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
311     "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
312     "a=mid:video\r\n"
313     "a=sendrecv\r\n"
314     "a=rtcp-mux\r\n"
315     "a=rtpmap:120 VP8/90000\r\n";
316 
317 // Reference SDP without MediaStreams. Msid is supported.
318 static const char kSdpStringWithMsidWithoutStreams[] =
319     "v=0\r\n"
320     "o=- 0 0 IN IP4 127.0.0.1\r\n"
321     "s=-\r\n"
322     "t=0 0\r\n"
323     "a=msid-semantic: WMS\r\n"
324     "m=audio 1 RTP/AVPF 103\r\n"
325     "a=ice-ufrag:e5785931\r\n"
326     "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
327     "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
328     "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
329     "a=mid:audio\r\n"
330     "a=sendrecv\r\n"
331     "a=rtcp-mux\r\n"
332     "a=rtpmap:103 ISAC/16000\r\n"
333     "m=video 1 RTP/AVPF 120\r\n"
334     "a=ice-ufrag:e5785931\r\n"
335     "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
336     "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
337     "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
338     "a=mid:video\r\n"
339     "a=sendrecv\r\n"
340     "a=rtcp-mux\r\n"
341     "a=rtpmap:120 VP8/90000\r\n";
342 
343 // Reference SDP without MediaStreams and audio only.
344 static const char kSdpStringWithoutStreamsAudioOnly[] =
345     "v=0\r\n"
346     "o=- 0 0 IN IP4 127.0.0.1\r\n"
347     "s=-\r\n"
348     "t=0 0\r\n"
349     "m=audio 1 RTP/AVPF 103\r\n"
350     "a=ice-ufrag:e5785931\r\n"
351     "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
352     "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
353     "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
354     "a=mid:audio\r\n"
355     "a=sendrecv\r\n"
356     "a=rtcp-mux\r\n"
357     "a=rtpmap:103 ISAC/16000\r\n";
358 
359 // Reference SENDONLY SDP without MediaStreams. Msid is not supported.
360 static const char kSdpStringSendOnlyWithoutStreams[] =
361     "v=0\r\n"
362     "o=- 0 0 IN IP4 127.0.0.1\r\n"
363     "s=-\r\n"
364     "t=0 0\r\n"
365     "m=audio 1 RTP/AVPF 103\r\n"
366     "a=ice-ufrag:e5785931\r\n"
367     "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
368     "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
369     "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
370     "a=mid:audio\r\n"
371     "a=sendrecv\r\n"
372     "a=sendonly\r\n"
373     "a=rtcp-mux\r\n"
374     "a=rtpmap:103 ISAC/16000\r\n"
375     "m=video 1 RTP/AVPF 120\r\n"
376     "a=ice-ufrag:e5785931\r\n"
377     "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
378     "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
379     "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
380     "a=mid:video\r\n"
381     "a=sendrecv\r\n"
382     "a=sendonly\r\n"
383     "a=rtcp-mux\r\n"
384     "a=rtpmap:120 VP8/90000\r\n";
385 
386 static const char kSdpStringInit[] =
387     "v=0\r\n"
388     "o=- 0 0 IN IP4 127.0.0.1\r\n"
389     "s=-\r\n"
390     "t=0 0\r\n"
391     "a=msid-semantic: WMS\r\n";
392 
393 static const char kSdpStringAudio[] =
394     "m=audio 1 RTP/AVPF 103\r\n"
395     "a=ice-ufrag:e5785931\r\n"
396     "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
397     "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
398     "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
399     "a=mid:audio\r\n"
400     "a=sendrecv\r\n"
401     "a=rtcp-mux\r\n"
402     "a=rtpmap:103 ISAC/16000\r\n";
403 
404 static const char kSdpStringVideo[] =
405     "m=video 1 RTP/AVPF 120\r\n"
406     "a=ice-ufrag:e5785931\r\n"
407     "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
408     "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
409     "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
410     "a=mid:video\r\n"
411     "a=sendrecv\r\n"
412     "a=rtcp-mux\r\n"
413     "a=rtpmap:120 VP8/90000\r\n";
414 
415 static const char kSdpStringMs1Audio0[] =
416     "a=ssrc:1 cname:stream1\r\n"
417     "a=ssrc:1 msid:stream1 audiotrack0\r\n";
418 
419 static const char kSdpStringMs1Video0[] =
420     "a=ssrc:2 cname:stream1\r\n"
421     "a=ssrc:2 msid:stream1 videotrack0\r\n";
422 
423 static const char kSdpStringMs1Audio1[] =
424     "a=ssrc:3 cname:stream1\r\n"
425     "a=ssrc:3 msid:stream1 audiotrack1\r\n";
426 
427 static const char kSdpStringMs1Video1[] =
428     "a=ssrc:4 cname:stream1\r\n"
429     "a=ssrc:4 msid:stream1 videotrack1\r\n";
430 
431 static const char kDtlsSdesFallbackSdp[] =
432     "v=0\r\n"
433     "o=xxxxxx 7 2 IN IP4 0.0.0.0\r\n"
434     "s=-\r\n"
435     "c=IN IP4 0.0.0.0\r\n"
436     "t=0 0\r\n"
437     "a=group:BUNDLE audio\r\n"
438     "a=msid-semantic: WMS\r\n"
439     "m=audio 1 RTP/SAVPF 0\r\n"
440     "a=sendrecv\r\n"
441     "a=rtcp-mux\r\n"
442     "a=mid:audio\r\n"
443     "a=ssrc:1 cname:stream1\r\n"
444     "a=ssrc:1 mslabel:stream1\r\n"
445     "a=ssrc:1 label:audiotrack0\r\n"
446     "a=ice-ufrag:e5785931\r\n"
447     "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
448     "a=rtpmap:0 pcmu/8000\r\n"
449     "a=fingerprint:sha-1 "
450     "4A:AD:B9:B1:3F:82:18:3B:54:02:12:DF:3E:5D:49:6B:19:E5:7C:AB\r\n"
451     "a=setup:actpass\r\n"
452     "a=crypto:0 AES_CM_128_HMAC_SHA1_80 "
453     "inline:NzB4d1BINUAvLEw6UzF3WSJ+PSdFcGdUJShpX1Zj|2^20|1:32 "
454     "dummy_session_params\r\n";
455 
456 class RtcEventLogOutputNull final : public RtcEventLogOutput {
457  public:
IsActive() const458   bool IsActive() const override { return true; }
Write(const std::string & output)459   bool Write(const std::string& output) override { return true; }
460 };
461 
462 using ::cricket::StreamParams;
463 using ::testing::Exactly;
464 using ::testing::Values;
465 
466 using RTCConfiguration = PeerConnectionInterface::RTCConfiguration;
467 using RTCOfferAnswerOptions = PeerConnectionInterface::RTCOfferAnswerOptions;
468 
469 // Gets the first ssrc of given content type from the ContentInfo.
GetFirstSsrc(const cricket::ContentInfo * content_info,int * ssrc)470 bool GetFirstSsrc(const cricket::ContentInfo* content_info, int* ssrc) {
471   if (!content_info || !ssrc) {
472     return false;
473   }
474   const cricket::MediaContentDescription* media_desc =
475       content_info->media_description();
476   if (!media_desc || media_desc->streams().empty()) {
477     return false;
478   }
479   *ssrc = media_desc->streams().begin()->first_ssrc();
480   return true;
481 }
482 
483 // Get the ufrags out of an SDP blob. Useful for testing ICE restart
484 // behavior.
GetUfrags(const webrtc::SessionDescriptionInterface * desc)485 std::vector<std::string> GetUfrags(
486     const webrtc::SessionDescriptionInterface* desc) {
487   std::vector<std::string> ufrags;
488   for (const cricket::TransportInfo& info :
489        desc->description()->transport_infos()) {
490     ufrags.push_back(info.description.ice_ufrag);
491   }
492   return ufrags;
493 }
494 
SetSsrcToZero(std::string * sdp)495 void SetSsrcToZero(std::string* sdp) {
496   const char kSdpSsrcAtribute[] = "a=ssrc:";
497   const char kSdpSsrcAtributeZero[] = "a=ssrc:0";
498   size_t ssrc_pos = 0;
499   while ((ssrc_pos = sdp->find(kSdpSsrcAtribute, ssrc_pos)) !=
500          std::string::npos) {
501     size_t end_ssrc = sdp->find(" ", ssrc_pos);
502     sdp->replace(ssrc_pos, end_ssrc - ssrc_pos, kSdpSsrcAtributeZero);
503     ssrc_pos = end_ssrc;
504   }
505 }
506 
507 // Check if |streams| contains the specified track.
ContainsTrack(const std::vector<cricket::StreamParams> & streams,const std::string & stream_id,const std::string & track_id)508 bool ContainsTrack(const std::vector<cricket::StreamParams>& streams,
509                    const std::string& stream_id,
510                    const std::string& track_id) {
511   for (const cricket::StreamParams& params : streams) {
512     if (params.first_stream_id() == stream_id && params.id == track_id) {
513       return true;
514     }
515   }
516   return false;
517 }
518 
519 // Check if |senders| contains the specified sender, by id.
ContainsSender(const std::vector<rtc::scoped_refptr<RtpSenderInterface>> & senders,const std::string & id)520 bool ContainsSender(
521     const std::vector<rtc::scoped_refptr<RtpSenderInterface>>& senders,
522     const std::string& id) {
523   for (const auto& sender : senders) {
524     if (sender->id() == id) {
525       return true;
526     }
527   }
528   return false;
529 }
530 
531 // Check if |senders| contains the specified sender, by id and stream id.
ContainsSender(const std::vector<rtc::scoped_refptr<RtpSenderInterface>> & senders,const std::string & id,const std::string & stream_id)532 bool ContainsSender(
533     const std::vector<rtc::scoped_refptr<RtpSenderInterface>>& senders,
534     const std::string& id,
535     const std::string& stream_id) {
536   for (const auto& sender : senders) {
537     if (sender->id() == id && sender->stream_ids()[0] == stream_id) {
538       return true;
539     }
540   }
541   return false;
542 }
543 
544 // Create a collection of streams.
545 // CreateStreamCollection(1) creates a collection that
546 // correspond to kSdpStringWithStream1.
547 // CreateStreamCollection(2) correspond to kSdpStringWithStream1And2.
CreateStreamCollection(int number_of_streams,int tracks_per_stream)548 rtc::scoped_refptr<StreamCollection> CreateStreamCollection(
549     int number_of_streams,
550     int tracks_per_stream) {
551   rtc::scoped_refptr<StreamCollection> local_collection(
552       StreamCollection::Create());
553 
554   for (int i = 0; i < number_of_streams; ++i) {
555     rtc::scoped_refptr<webrtc::MediaStreamInterface> stream(
556         webrtc::MediaStream::Create(kStreams[i]));
557 
558     for (int j = 0; j < tracks_per_stream; ++j) {
559       // Add a local audio track.
560       rtc::scoped_refptr<webrtc::AudioTrackInterface> audio_track(
561           webrtc::AudioTrack::Create(kAudioTracks[i * tracks_per_stream + j],
562                                      nullptr));
563       stream->AddTrack(audio_track);
564 
565       // Add a local video track.
566       rtc::scoped_refptr<webrtc::VideoTrackInterface> video_track(
567           webrtc::VideoTrack::Create(kVideoTracks[i * tracks_per_stream + j],
568                                      webrtc::FakeVideoTrackSource::Create(),
569                                      rtc::Thread::Current()));
570       stream->AddTrack(video_track);
571     }
572 
573     local_collection->AddStream(stream);
574   }
575   return local_collection;
576 }
577 
578 // Check equality of StreamCollections.
CompareStreamCollections(StreamCollectionInterface * s1,StreamCollectionInterface * s2)579 bool CompareStreamCollections(StreamCollectionInterface* s1,
580                               StreamCollectionInterface* s2) {
581   if (s1 == nullptr || s2 == nullptr || s1->count() != s2->count()) {
582     return false;
583   }
584 
585   for (size_t i = 0; i != s1->count(); ++i) {
586     if (s1->at(i)->id() != s2->at(i)->id()) {
587       return false;
588     }
589     webrtc::AudioTrackVector audio_tracks1 = s1->at(i)->GetAudioTracks();
590     webrtc::AudioTrackVector audio_tracks2 = s2->at(i)->GetAudioTracks();
591     webrtc::VideoTrackVector video_tracks1 = s1->at(i)->GetVideoTracks();
592     webrtc::VideoTrackVector video_tracks2 = s2->at(i)->GetVideoTracks();
593 
594     if (audio_tracks1.size() != audio_tracks2.size()) {
595       return false;
596     }
597     for (size_t j = 0; j != audio_tracks1.size(); ++j) {
598       if (audio_tracks1[j]->id() != audio_tracks2[j]->id()) {
599         return false;
600       }
601     }
602     if (video_tracks1.size() != video_tracks2.size()) {
603       return false;
604     }
605     for (size_t j = 0; j != video_tracks1.size(); ++j) {
606       if (video_tracks1[j]->id() != video_tracks2[j]->id()) {
607         return false;
608       }
609     }
610   }
611   return true;
612 }
613 
614 // Helper class to test Observer.
615 class MockTrackObserver : public ObserverInterface {
616  public:
MockTrackObserver(NotifierInterface * notifier)617   explicit MockTrackObserver(NotifierInterface* notifier)
618       : notifier_(notifier) {
619     notifier_->RegisterObserver(this);
620   }
621 
~MockTrackObserver()622   ~MockTrackObserver() { Unregister(); }
623 
Unregister()624   void Unregister() {
625     if (notifier_) {
626       notifier_->UnregisterObserver(this);
627       notifier_ = nullptr;
628     }
629   }
630 
631   MOCK_METHOD(void, OnChanged, (), (override));
632 
633  private:
634   NotifierInterface* notifier_;
635 };
636 
637 // The PeerConnectionMediaConfig tests below verify that configuration and
638 // constraints are propagated into the PeerConnection's MediaConfig. These
639 // settings are intended for MediaChannel constructors, but that is not
640 // exercised by these unittest.
641 class PeerConnectionFactoryForTest : public webrtc::PeerConnectionFactory {
642  public:
643   static rtc::scoped_refptr<PeerConnectionFactoryForTest>
CreatePeerConnectionFactoryForTest()644   CreatePeerConnectionFactoryForTest() {
645     PeerConnectionFactoryDependencies dependencies;
646     dependencies.worker_thread = rtc::Thread::Current();
647     dependencies.network_thread = rtc::Thread::Current();
648     dependencies.signaling_thread = rtc::Thread::Current();
649     dependencies.task_queue_factory = CreateDefaultTaskQueueFactory();
650     dependencies.trials = std::make_unique<FieldTrialBasedConfig>();
651     cricket::MediaEngineDependencies media_deps;
652     media_deps.task_queue_factory = dependencies.task_queue_factory.get();
653     // Use fake audio device module since we're only testing the interface
654     // level, and using a real one could make tests flaky when run in parallel.
655     media_deps.adm = FakeAudioCaptureModule::Create();
656     SetMediaEngineDefaults(&media_deps);
657     media_deps.trials = dependencies.trials.get();
658     dependencies.media_engine =
659         cricket::CreateMediaEngine(std::move(media_deps));
660     dependencies.call_factory = webrtc::CreateCallFactory();
661     dependencies.event_log_factory = std::make_unique<RtcEventLogFactory>(
662         dependencies.task_queue_factory.get());
663 
664     return new rtc::RefCountedObject<PeerConnectionFactoryForTest>(
665         std::move(dependencies));
666   }
667 
668   using PeerConnectionFactory::PeerConnectionFactory;
669 
670  private:
671   rtc::scoped_refptr<FakeAudioCaptureModule> fake_audio_capture_module_;
672 };
673 
674 // TODO(steveanton): Convert to use the new PeerConnectionWrapper.
675 class PeerConnectionInterfaceBaseTest : public ::testing::Test {
676  protected:
PeerConnectionInterfaceBaseTest(SdpSemantics sdp_semantics)677   explicit PeerConnectionInterfaceBaseTest(SdpSemantics sdp_semantics)
678       : vss_(new rtc::VirtualSocketServer()),
679         main_(vss_.get()),
680         sdp_semantics_(sdp_semantics) {
681 #ifdef WEBRTC_ANDROID
682     webrtc::InitializeAndroidObjects();
683 #endif
684   }
685 
SetUp()686   void SetUp() override {
687     // Use fake audio capture module since we're only testing the interface
688     // level, and using a real one could make tests flaky when run in parallel.
689     fake_audio_capture_module_ = FakeAudioCaptureModule::Create();
690     pc_factory_ = webrtc::CreatePeerConnectionFactory(
691         rtc::Thread::Current(), rtc::Thread::Current(), rtc::Thread::Current(),
692         rtc::scoped_refptr<webrtc::AudioDeviceModule>(
693             fake_audio_capture_module_),
694         webrtc::CreateBuiltinAudioEncoderFactory(),
695         webrtc::CreateBuiltinAudioDecoderFactory(),
696         webrtc::CreateBuiltinVideoEncoderFactory(),
697         webrtc::CreateBuiltinVideoDecoderFactory(), nullptr /* audio_mixer */,
698         nullptr /* audio_processing */);
699     ASSERT_TRUE(pc_factory_);
700     pc_factory_for_test_ =
701         PeerConnectionFactoryForTest::CreatePeerConnectionFactoryForTest();
702   }
703 
TearDown()704   void TearDown() override {
705     if (pc_)
706       pc_->Close();
707   }
708 
CreatePeerConnection()709   void CreatePeerConnection() {
710     CreatePeerConnection(PeerConnectionInterface::RTCConfiguration());
711   }
712 
713   // DTLS does not work in a loopback call, so is disabled for most of the
714   // tests in this file.
CreatePeerConnectionWithoutDtls()715   void CreatePeerConnectionWithoutDtls() {
716     RTCConfiguration config;
717     config.enable_dtls_srtp = false;
718 
719     CreatePeerConnection(config);
720   }
721 
CreatePeerConnectionWithIceTransportsType(PeerConnectionInterface::IceTransportsType type)722   void CreatePeerConnectionWithIceTransportsType(
723       PeerConnectionInterface::IceTransportsType type) {
724     PeerConnectionInterface::RTCConfiguration config;
725     config.type = type;
726     return CreatePeerConnection(config);
727   }
728 
CreatePeerConnectionWithIceServer(const std::string & uri,const std::string & username,const std::string & password)729   void CreatePeerConnectionWithIceServer(const std::string& uri,
730                                          const std::string& username,
731                                          const std::string& password) {
732     PeerConnectionInterface::RTCConfiguration config;
733     PeerConnectionInterface::IceServer server;
734     server.uri = uri;
735     server.username = username;
736     server.password = password;
737     config.servers.push_back(server);
738     CreatePeerConnection(config);
739   }
740 
CreatePeerConnection(const RTCConfiguration & config)741   void CreatePeerConnection(const RTCConfiguration& config) {
742     if (pc_) {
743       pc_->Close();
744       pc_ = nullptr;
745     }
746     std::unique_ptr<cricket::FakePortAllocator> port_allocator(
747         new cricket::FakePortAllocator(rtc::Thread::Current(), nullptr));
748     port_allocator_ = port_allocator.get();
749 
750     // Create certificate generator unless DTLS constraint is explicitly set to
751     // false.
752     std::unique_ptr<rtc::RTCCertificateGeneratorInterface> cert_generator;
753 
754     if (config.enable_dtls_srtp.value_or(true)) {
755       fake_certificate_generator_ = new FakeRTCCertificateGenerator();
756       cert_generator.reset(fake_certificate_generator_);
757     }
758     RTCConfiguration modified_config = config;
759     modified_config.sdp_semantics = sdp_semantics_;
760     pc_ = pc_factory_->CreatePeerConnection(
761         modified_config, std::move(port_allocator), std::move(cert_generator),
762         &observer_);
763     ASSERT_TRUE(pc_.get() != NULL);
764     observer_.SetPeerConnectionInterface(pc_.get());
765     EXPECT_EQ(PeerConnectionInterface::kStable, observer_.state_);
766   }
767 
CreatePeerConnectionExpectFail(const std::string & uri)768   void CreatePeerConnectionExpectFail(const std::string& uri) {
769     PeerConnectionInterface::RTCConfiguration config;
770     PeerConnectionInterface::IceServer server;
771     server.uri = uri;
772     config.servers.push_back(server);
773     config.sdp_semantics = sdp_semantics_;
774     rtc::scoped_refptr<PeerConnectionInterface> pc =
775         pc_factory_->CreatePeerConnection(config, nullptr, nullptr, &observer_);
776     EXPECT_EQ(nullptr, pc);
777   }
778 
CreatePeerConnectionExpectFail(PeerConnectionInterface::RTCConfiguration config)779   void CreatePeerConnectionExpectFail(
780       PeerConnectionInterface::RTCConfiguration config) {
781     PeerConnectionInterface::IceServer server;
782     server.uri = kTurnIceServerUri;
783     server.password = kTurnPassword;
784     config.servers.push_back(server);
785     config.sdp_semantics = sdp_semantics_;
786     rtc::scoped_refptr<PeerConnectionInterface> pc =
787         pc_factory_->CreatePeerConnection(config, nullptr, nullptr, &observer_);
788     EXPECT_EQ(nullptr, pc);
789   }
790 
CreatePeerConnectionWithDifferentConfigurations()791   void CreatePeerConnectionWithDifferentConfigurations() {
792     CreatePeerConnectionWithIceServer(kStunAddressOnly, "", "");
793     EXPECT_EQ(1u, port_allocator_->stun_servers().size());
794     EXPECT_EQ(0u, port_allocator_->turn_servers().size());
795     EXPECT_EQ("address", port_allocator_->stun_servers().begin()->hostname());
796     EXPECT_EQ(kDefaultStunPort,
797               port_allocator_->stun_servers().begin()->port());
798 
799     CreatePeerConnectionExpectFail(kStunInvalidPort);
800     CreatePeerConnectionExpectFail(kStunAddressPortAndMore1);
801     CreatePeerConnectionExpectFail(kStunAddressPortAndMore2);
802 
803     CreatePeerConnectionWithIceServer(kTurnIceServerUri, kTurnUsername,
804                                       kTurnPassword);
805     EXPECT_EQ(0u, port_allocator_->stun_servers().size());
806     EXPECT_EQ(1u, port_allocator_->turn_servers().size());
807     EXPECT_EQ(kTurnUsername,
808               port_allocator_->turn_servers()[0].credentials.username);
809     EXPECT_EQ(kTurnPassword,
810               port_allocator_->turn_servers()[0].credentials.password);
811     EXPECT_EQ(kTurnHostname,
812               port_allocator_->turn_servers()[0].ports[0].address.hostname());
813   }
814 
ReleasePeerConnection()815   void ReleasePeerConnection() {
816     pc_ = NULL;
817     observer_.SetPeerConnectionInterface(NULL);
818   }
819 
CreateVideoTrack(const std::string & label)820   rtc::scoped_refptr<VideoTrackInterface> CreateVideoTrack(
821       const std::string& label) {
822     return pc_factory_->CreateVideoTrack(label, FakeVideoTrackSource::Create());
823   }
824 
AddVideoTrack(const std::string & track_label,const std::vector<std::string> & stream_ids={})825   void AddVideoTrack(const std::string& track_label,
826                      const std::vector<std::string>& stream_ids = {}) {
827     auto sender_or_error =
828         pc_->AddTrack(CreateVideoTrack(track_label), stream_ids);
829     ASSERT_EQ(RTCErrorType::NONE, sender_or_error.error().type());
830   }
831 
AddVideoStream(const std::string & label)832   void AddVideoStream(const std::string& label) {
833     rtc::scoped_refptr<MediaStreamInterface> stream(
834         pc_factory_->CreateLocalMediaStream(label));
835     stream->AddTrack(CreateVideoTrack(label + "v0"));
836     ASSERT_TRUE(pc_->AddStream(stream));
837   }
838 
CreateAudioTrack(const std::string & label)839   rtc::scoped_refptr<AudioTrackInterface> CreateAudioTrack(
840       const std::string& label) {
841     return pc_factory_->CreateAudioTrack(label, nullptr);
842   }
843 
AddAudioTrack(const std::string & track_label,const std::vector<std::string> & stream_ids={})844   void AddAudioTrack(const std::string& track_label,
845                      const std::vector<std::string>& stream_ids = {}) {
846     auto sender_or_error =
847         pc_->AddTrack(CreateAudioTrack(track_label), stream_ids);
848     ASSERT_EQ(RTCErrorType::NONE, sender_or_error.error().type());
849   }
850 
AddAudioStream(const std::string & label)851   void AddAudioStream(const std::string& label) {
852     rtc::scoped_refptr<MediaStreamInterface> stream(
853         pc_factory_->CreateLocalMediaStream(label));
854     stream->AddTrack(CreateAudioTrack(label + "a0"));
855     ASSERT_TRUE(pc_->AddStream(stream));
856   }
857 
AddAudioVideoStream(const std::string & stream_id,const std::string & audio_track_label,const std::string & video_track_label)858   void AddAudioVideoStream(const std::string& stream_id,
859                            const std::string& audio_track_label,
860                            const std::string& video_track_label) {
861     // Create a local stream.
862     rtc::scoped_refptr<MediaStreamInterface> stream(
863         pc_factory_->CreateLocalMediaStream(stream_id));
864     stream->AddTrack(CreateAudioTrack(audio_track_label));
865     stream->AddTrack(CreateVideoTrack(video_track_label));
866     ASSERT_TRUE(pc_->AddStream(stream));
867   }
868 
GetFirstReceiverOfType(cricket::MediaType media_type)869   rtc::scoped_refptr<RtpReceiverInterface> GetFirstReceiverOfType(
870       cricket::MediaType media_type) {
871     for (auto receiver : pc_->GetReceivers()) {
872       if (receiver->media_type() == media_type) {
873         return receiver;
874       }
875     }
876     return nullptr;
877   }
878 
DoCreateOfferAnswer(std::unique_ptr<SessionDescriptionInterface> * desc,const RTCOfferAnswerOptions * options,bool offer)879   bool DoCreateOfferAnswer(std::unique_ptr<SessionDescriptionInterface>* desc,
880                            const RTCOfferAnswerOptions* options,
881                            bool offer) {
882     rtc::scoped_refptr<MockCreateSessionDescriptionObserver> observer(
883         new rtc::RefCountedObject<MockCreateSessionDescriptionObserver>());
884     if (offer) {
885       pc_->CreateOffer(observer, options ? *options : RTCOfferAnswerOptions());
886     } else {
887       pc_->CreateAnswer(observer, options ? *options : RTCOfferAnswerOptions());
888     }
889     EXPECT_EQ_WAIT(true, observer->called(), kTimeout);
890     *desc = observer->MoveDescription();
891     return observer->result();
892   }
893 
DoCreateOffer(std::unique_ptr<SessionDescriptionInterface> * desc,const RTCOfferAnswerOptions * options)894   bool DoCreateOffer(std::unique_ptr<SessionDescriptionInterface>* desc,
895                      const RTCOfferAnswerOptions* options) {
896     return DoCreateOfferAnswer(desc, options, true);
897   }
898 
DoCreateAnswer(std::unique_ptr<SessionDescriptionInterface> * desc,const RTCOfferAnswerOptions * options)899   bool DoCreateAnswer(std::unique_ptr<SessionDescriptionInterface>* desc,
900                       const RTCOfferAnswerOptions* options) {
901     return DoCreateOfferAnswer(desc, options, false);
902   }
903 
DoSetSessionDescription(std::unique_ptr<SessionDescriptionInterface> desc,bool local)904   bool DoSetSessionDescription(
905       std::unique_ptr<SessionDescriptionInterface> desc,
906       bool local) {
907     rtc::scoped_refptr<MockSetSessionDescriptionObserver> observer(
908         new rtc::RefCountedObject<MockSetSessionDescriptionObserver>());
909     if (local) {
910       pc_->SetLocalDescription(observer, desc.release());
911     } else {
912       pc_->SetRemoteDescription(observer, desc.release());
913     }
914     if (pc_->signaling_state() != PeerConnectionInterface::kClosed) {
915       EXPECT_EQ_WAIT(true, observer->called(), kTimeout);
916     }
917     return observer->result();
918   }
919 
DoSetLocalDescription(std::unique_ptr<SessionDescriptionInterface> desc)920   bool DoSetLocalDescription(
921       std::unique_ptr<SessionDescriptionInterface> desc) {
922     return DoSetSessionDescription(std::move(desc), true);
923   }
924 
DoSetRemoteDescription(std::unique_ptr<SessionDescriptionInterface> desc)925   bool DoSetRemoteDescription(
926       std::unique_ptr<SessionDescriptionInterface> desc) {
927     return DoSetSessionDescription(std::move(desc), false);
928   }
929 
930   // Calls PeerConnection::GetStats and check the return value.
931   // It does not verify the values in the StatReports since a RTCP packet might
932   // be required.
DoGetStats(MediaStreamTrackInterface * track)933   bool DoGetStats(MediaStreamTrackInterface* track) {
934     rtc::scoped_refptr<MockStatsObserver> observer(
935         new rtc::RefCountedObject<MockStatsObserver>());
936     if (!pc_->GetStats(observer, track,
937                        PeerConnectionInterface::kStatsOutputLevelStandard))
938       return false;
939     EXPECT_TRUE_WAIT(observer->called(), kTimeout);
940     return observer->called();
941   }
942 
943   // Call the standards-compliant GetStats function.
DoGetRTCStats()944   bool DoGetRTCStats() {
945     rtc::scoped_refptr<webrtc::MockRTCStatsCollectorCallback> callback(
946         new rtc::RefCountedObject<webrtc::MockRTCStatsCollectorCallback>());
947     pc_->GetStats(callback);
948     EXPECT_TRUE_WAIT(callback->called(), kTimeout);
949     return callback->called();
950   }
951 
InitiateCall()952   void InitiateCall() {
953     CreatePeerConnectionWithoutDtls();
954     // Create a local stream with audio&video tracks.
955     if (sdp_semantics_ == SdpSemantics::kPlanB) {
956       AddAudioVideoStream(kStreamId1, "audio_track", "video_track");
957     } else {
958       // Unified Plan does not support AddStream, so just add an audio and video
959       // track.
960       AddAudioTrack(kAudioTracks[0], {kStreamId1});
961       AddVideoTrack(kVideoTracks[0], {kStreamId1});
962     }
963     CreateOfferReceiveAnswer();
964   }
965 
966   // Verify that RTP Header extensions has been negotiated for audio and video.
VerifyRemoteRtpHeaderExtensions()967   void VerifyRemoteRtpHeaderExtensions() {
968     const cricket::MediaContentDescription* desc =
969         cricket::GetFirstAudioContentDescription(
970             pc_->remote_description()->description());
971     ASSERT_TRUE(desc != NULL);
972     EXPECT_GT(desc->rtp_header_extensions().size(), 0u);
973 
974     desc = cricket::GetFirstVideoContentDescription(
975         pc_->remote_description()->description());
976     ASSERT_TRUE(desc != NULL);
977     EXPECT_GT(desc->rtp_header_extensions().size(), 0u);
978   }
979 
CreateOfferAsRemoteDescription()980   void CreateOfferAsRemoteDescription() {
981     std::unique_ptr<SessionDescriptionInterface> offer;
982     ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
983     std::string sdp;
984     EXPECT_TRUE(offer->ToString(&sdp));
985     std::unique_ptr<SessionDescriptionInterface> remote_offer(
986         webrtc::CreateSessionDescription(SdpType::kOffer, sdp));
987     EXPECT_TRUE(DoSetRemoteDescription(std::move(remote_offer)));
988     EXPECT_EQ(PeerConnectionInterface::kHaveRemoteOffer, observer_.state_);
989   }
990 
CreateAndSetRemoteOffer(const std::string & sdp)991   void CreateAndSetRemoteOffer(const std::string& sdp) {
992     std::unique_ptr<SessionDescriptionInterface> remote_offer(
993         webrtc::CreateSessionDescription(SdpType::kOffer, sdp));
994     EXPECT_TRUE(DoSetRemoteDescription(std::move(remote_offer)));
995     EXPECT_EQ(PeerConnectionInterface::kHaveRemoteOffer, observer_.state_);
996   }
997 
CreateAnswerAsLocalDescription()998   void CreateAnswerAsLocalDescription() {
999     std::unique_ptr<SessionDescriptionInterface> answer;
1000     ASSERT_TRUE(DoCreateAnswer(&answer, nullptr));
1001 
1002     // TODO(perkj): Currently SetLocalDescription fails if any parameters in an
1003     // audio codec change, even if the parameter has nothing to do with
1004     // receiving. Not all parameters are serialized to SDP.
1005     // Since CreatePrAnswerAsLocalDescription serialize/deserialize
1006     // the SessionDescription, it is necessary to do that here to in order to
1007     // get ReceiveOfferCreatePrAnswerAndAnswer and RenegotiateAudioOnly to pass.
1008     // https://code.google.com/p/webrtc/issues/detail?id=1356
1009     std::string sdp;
1010     EXPECT_TRUE(answer->ToString(&sdp));
1011     std::unique_ptr<SessionDescriptionInterface> new_answer(
1012         webrtc::CreateSessionDescription(SdpType::kAnswer, sdp));
1013     EXPECT_TRUE(DoSetLocalDescription(std::move(new_answer)));
1014     EXPECT_EQ(PeerConnectionInterface::kStable, observer_.state_);
1015   }
1016 
CreatePrAnswerAsLocalDescription()1017   void CreatePrAnswerAsLocalDescription() {
1018     std::unique_ptr<SessionDescriptionInterface> answer;
1019     ASSERT_TRUE(DoCreateAnswer(&answer, nullptr));
1020 
1021     std::string sdp;
1022     EXPECT_TRUE(answer->ToString(&sdp));
1023     std::unique_ptr<SessionDescriptionInterface> pr_answer(
1024         webrtc::CreateSessionDescription(SdpType::kPrAnswer, sdp));
1025     EXPECT_TRUE(DoSetLocalDescription(std::move(pr_answer)));
1026     EXPECT_EQ(PeerConnectionInterface::kHaveLocalPrAnswer, observer_.state_);
1027   }
1028 
CreateOfferReceiveAnswer()1029   void CreateOfferReceiveAnswer() {
1030     CreateOfferAsLocalDescription();
1031     std::string sdp;
1032     EXPECT_TRUE(pc_->local_description()->ToString(&sdp));
1033     CreateAnswerAsRemoteDescription(sdp);
1034   }
1035 
CreateOfferAsLocalDescription()1036   void CreateOfferAsLocalDescription() {
1037     std::unique_ptr<SessionDescriptionInterface> offer;
1038     ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
1039     // TODO(perkj): Currently SetLocalDescription fails if any parameters in an
1040     // audio codec change, even if the parameter has nothing to do with
1041     // receiving. Not all parameters are serialized to SDP.
1042     // Since CreatePrAnswerAsLocalDescription serialize/deserialize
1043     // the SessionDescription, it is necessary to do that here to in order to
1044     // get ReceiveOfferCreatePrAnswerAndAnswer and RenegotiateAudioOnly to pass.
1045     // https://code.google.com/p/webrtc/issues/detail?id=1356
1046     std::string sdp;
1047     EXPECT_TRUE(offer->ToString(&sdp));
1048     std::unique_ptr<SessionDescriptionInterface> new_offer(
1049         webrtc::CreateSessionDescription(SdpType::kOffer, sdp));
1050 
1051     EXPECT_TRUE(DoSetLocalDescription(std::move(new_offer)));
1052     EXPECT_EQ(PeerConnectionInterface::kHaveLocalOffer, observer_.state_);
1053     // Wait for the ice_complete message, so that SDP will have candidates.
1054     EXPECT_TRUE_WAIT(observer_.ice_gathering_complete_, kTimeout);
1055   }
1056 
CreateAnswerAsRemoteDescription(const std::string & sdp)1057   void CreateAnswerAsRemoteDescription(const std::string& sdp) {
1058     std::unique_ptr<SessionDescriptionInterface> answer(
1059         webrtc::CreateSessionDescription(SdpType::kAnswer, sdp));
1060     ASSERT_TRUE(answer);
1061     EXPECT_TRUE(DoSetRemoteDescription(std::move(answer)));
1062     EXPECT_EQ(PeerConnectionInterface::kStable, observer_.state_);
1063   }
1064 
CreatePrAnswerAndAnswerAsRemoteDescription(const std::string & sdp)1065   void CreatePrAnswerAndAnswerAsRemoteDescription(const std::string& sdp) {
1066     std::unique_ptr<SessionDescriptionInterface> pr_answer(
1067         webrtc::CreateSessionDescription(SdpType::kPrAnswer, sdp));
1068     ASSERT_TRUE(pr_answer);
1069     EXPECT_TRUE(DoSetRemoteDescription(std::move(pr_answer)));
1070     EXPECT_EQ(PeerConnectionInterface::kHaveRemotePrAnswer, observer_.state_);
1071     std::unique_ptr<SessionDescriptionInterface> answer(
1072         webrtc::CreateSessionDescription(SdpType::kAnswer, sdp));
1073     ASSERT_TRUE(answer);
1074     EXPECT_TRUE(DoSetRemoteDescription(std::move(answer)));
1075     EXPECT_EQ(PeerConnectionInterface::kStable, observer_.state_);
1076   }
1077 
1078   // Waits until a remote stream with the given id is signaled. This helper
1079   // function will verify both OnAddTrack and OnAddStream (Plan B only) are
1080   // called with the given stream id and expected number of tracks.
WaitAndVerifyOnAddStream(const std::string & stream_id,int expected_num_tracks)1081   void WaitAndVerifyOnAddStream(const std::string& stream_id,
1082                                 int expected_num_tracks) {
1083     // Verify that both OnAddStream and OnAddTrack are called.
1084     EXPECT_EQ_WAIT(stream_id, observer_.GetLastAddedStreamId(), kTimeout);
1085     EXPECT_EQ_WAIT(expected_num_tracks,
1086                    observer_.CountAddTrackEventsForStream(stream_id), kTimeout);
1087   }
1088 
1089   // Creates an offer and applies it as a local session description.
1090   // Creates an answer with the same SDP an the offer but removes all lines
1091   // that start with a:ssrc"
CreateOfferReceiveAnswerWithoutSsrc()1092   void CreateOfferReceiveAnswerWithoutSsrc() {
1093     CreateOfferAsLocalDescription();
1094     std::string sdp;
1095     EXPECT_TRUE(pc_->local_description()->ToString(&sdp));
1096     SetSsrcToZero(&sdp);
1097     CreateAnswerAsRemoteDescription(sdp);
1098   }
1099 
1100   // This function creates a MediaStream with label kStreams[0] and
1101   // |number_of_audio_tracks| and |number_of_video_tracks| tracks and the
1102   // corresponding SessionDescriptionInterface. The SessionDescriptionInterface
1103   // is returned and the MediaStream is stored in
1104   // |reference_collection_|
1105   std::unique_ptr<SessionDescriptionInterface>
CreateSessionDescriptionAndReference(size_t number_of_audio_tracks,size_t number_of_video_tracks)1106   CreateSessionDescriptionAndReference(size_t number_of_audio_tracks,
1107                                        size_t number_of_video_tracks) {
1108     EXPECT_LE(number_of_audio_tracks, 2u);
1109     EXPECT_LE(number_of_video_tracks, 2u);
1110 
1111     reference_collection_ = StreamCollection::Create();
1112     std::string sdp_ms1 = std::string(kSdpStringInit);
1113 
1114     std::string mediastream_id = kStreams[0];
1115 
1116     rtc::scoped_refptr<webrtc::MediaStreamInterface> stream(
1117         webrtc::MediaStream::Create(mediastream_id));
1118     reference_collection_->AddStream(stream);
1119 
1120     if (number_of_audio_tracks > 0) {
1121       sdp_ms1 += std::string(kSdpStringAudio);
1122       sdp_ms1 += std::string(kSdpStringMs1Audio0);
1123       AddAudioTrack(kAudioTracks[0], stream);
1124     }
1125     if (number_of_audio_tracks > 1) {
1126       sdp_ms1 += kSdpStringMs1Audio1;
1127       AddAudioTrack(kAudioTracks[1], stream);
1128     }
1129 
1130     if (number_of_video_tracks > 0) {
1131       sdp_ms1 += std::string(kSdpStringVideo);
1132       sdp_ms1 += std::string(kSdpStringMs1Video0);
1133       AddVideoTrack(kVideoTracks[0], stream);
1134     }
1135     if (number_of_video_tracks > 1) {
1136       sdp_ms1 += kSdpStringMs1Video1;
1137       AddVideoTrack(kVideoTracks[1], stream);
1138     }
1139 
1140     return std::unique_ptr<SessionDescriptionInterface>(
1141         webrtc::CreateSessionDescription(SdpType::kOffer, sdp_ms1));
1142   }
1143 
AddAudioTrack(const std::string & track_id,MediaStreamInterface * stream)1144   void AddAudioTrack(const std::string& track_id,
1145                      MediaStreamInterface* stream) {
1146     rtc::scoped_refptr<webrtc::AudioTrackInterface> audio_track(
1147         webrtc::AudioTrack::Create(track_id, nullptr));
1148     ASSERT_TRUE(stream->AddTrack(audio_track));
1149   }
1150 
AddVideoTrack(const std::string & track_id,MediaStreamInterface * stream)1151   void AddVideoTrack(const std::string& track_id,
1152                      MediaStreamInterface* stream) {
1153     rtc::scoped_refptr<webrtc::VideoTrackInterface> video_track(
1154         webrtc::VideoTrack::Create(track_id,
1155                                    webrtc::FakeVideoTrackSource::Create(),
1156                                    rtc::Thread::Current()));
1157     ASSERT_TRUE(stream->AddTrack(video_track));
1158   }
1159 
CreateOfferWithOneAudioTrack()1160   std::unique_ptr<SessionDescriptionInterface> CreateOfferWithOneAudioTrack() {
1161     CreatePeerConnectionWithoutDtls();
1162     AddAudioTrack(kAudioTracks[0]);
1163     std::unique_ptr<SessionDescriptionInterface> offer;
1164     EXPECT_TRUE(DoCreateOffer(&offer, nullptr));
1165     return offer;
1166   }
1167 
CreateOfferWithOneAudioStream()1168   std::unique_ptr<SessionDescriptionInterface> CreateOfferWithOneAudioStream() {
1169     CreatePeerConnectionWithoutDtls();
1170     AddAudioStream(kStreamId1);
1171     std::unique_ptr<SessionDescriptionInterface> offer;
1172     EXPECT_TRUE(DoCreateOffer(&offer, nullptr));
1173     return offer;
1174   }
1175 
CreateAnswerWithOneAudioTrack()1176   std::unique_ptr<SessionDescriptionInterface> CreateAnswerWithOneAudioTrack() {
1177     EXPECT_TRUE(DoSetRemoteDescription(CreateOfferWithOneAudioTrack()));
1178     std::unique_ptr<SessionDescriptionInterface> answer;
1179     EXPECT_TRUE(DoCreateAnswer(&answer, nullptr));
1180     return answer;
1181   }
1182 
1183   std::unique_ptr<SessionDescriptionInterface>
CreateAnswerWithOneAudioStream()1184   CreateAnswerWithOneAudioStream() {
1185     EXPECT_TRUE(DoSetRemoteDescription(CreateOfferWithOneAudioStream()));
1186     std::unique_ptr<SessionDescriptionInterface> answer;
1187     EXPECT_TRUE(DoCreateAnswer(&answer, nullptr));
1188     return answer;
1189   }
1190 
GetFirstAudioStreamCname(const SessionDescriptionInterface * desc)1191   const std::string& GetFirstAudioStreamCname(
1192       const SessionDescriptionInterface* desc) {
1193     const cricket::AudioContentDescription* audio_desc =
1194         cricket::GetFirstAudioContentDescription(desc->description());
1195     return audio_desc->streams()[0].cname;
1196   }
1197 
CreateOfferWithOptions(const RTCOfferAnswerOptions & offer_answer_options)1198   std::unique_ptr<SessionDescriptionInterface> CreateOfferWithOptions(
1199       const RTCOfferAnswerOptions& offer_answer_options) {
1200     RTC_DCHECK(pc_);
1201     rtc::scoped_refptr<MockCreateSessionDescriptionObserver> observer(
1202         new rtc::RefCountedObject<MockCreateSessionDescriptionObserver>());
1203     pc_->CreateOffer(observer, offer_answer_options);
1204     EXPECT_EQ_WAIT(true, observer->called(), kTimeout);
1205     return observer->MoveDescription();
1206   }
1207 
CreateOfferWithOptionsAsRemoteDescription(std::unique_ptr<SessionDescriptionInterface> * desc,const RTCOfferAnswerOptions & offer_answer_options)1208   void CreateOfferWithOptionsAsRemoteDescription(
1209       std::unique_ptr<SessionDescriptionInterface>* desc,
1210       const RTCOfferAnswerOptions& offer_answer_options) {
1211     *desc = CreateOfferWithOptions(offer_answer_options);
1212     ASSERT_TRUE(desc != nullptr);
1213     std::string sdp;
1214     EXPECT_TRUE((*desc)->ToString(&sdp));
1215     std::unique_ptr<SessionDescriptionInterface> remote_offer(
1216         webrtc::CreateSessionDescription(SdpType::kOffer, sdp));
1217     EXPECT_TRUE(DoSetRemoteDescription(std::move(remote_offer)));
1218     EXPECT_EQ(PeerConnectionInterface::kHaveRemoteOffer, observer_.state_);
1219   }
1220 
CreateOfferWithOptionsAsLocalDescription(std::unique_ptr<SessionDescriptionInterface> * desc,const RTCOfferAnswerOptions & offer_answer_options)1221   void CreateOfferWithOptionsAsLocalDescription(
1222       std::unique_ptr<SessionDescriptionInterface>* desc,
1223       const RTCOfferAnswerOptions& offer_answer_options) {
1224     *desc = CreateOfferWithOptions(offer_answer_options);
1225     ASSERT_TRUE(desc != nullptr);
1226     std::string sdp;
1227     EXPECT_TRUE((*desc)->ToString(&sdp));
1228     std::unique_ptr<SessionDescriptionInterface> new_offer(
1229         webrtc::CreateSessionDescription(SdpType::kOffer, sdp));
1230 
1231     EXPECT_TRUE(DoSetLocalDescription(std::move(new_offer)));
1232     EXPECT_EQ(PeerConnectionInterface::kHaveLocalOffer, observer_.state_);
1233   }
1234 
HasCNCodecs(const cricket::ContentInfo * content)1235   bool HasCNCodecs(const cricket::ContentInfo* content) {
1236     RTC_DCHECK(content);
1237     RTC_DCHECK(content->media_description());
1238     for (const cricket::AudioCodec& codec :
1239          content->media_description()->as_audio()->codecs()) {
1240       if (codec.name == "CN") {
1241         return true;
1242       }
1243     }
1244     return false;
1245   }
1246 
GetSdpStringWithStream1() const1247   const char* GetSdpStringWithStream1() const {
1248     if (sdp_semantics_ == SdpSemantics::kPlanB) {
1249       return kSdpStringWithStream1PlanB;
1250     } else {
1251       return kSdpStringWithStream1UnifiedPlan;
1252     }
1253   }
1254 
GetSdpStringWithStream1And2() const1255   const char* GetSdpStringWithStream1And2() const {
1256     if (sdp_semantics_ == SdpSemantics::kPlanB) {
1257       return kSdpStringWithStream1And2PlanB;
1258     } else {
1259       return kSdpStringWithStream1And2UnifiedPlan;
1260     }
1261   }
1262 
1263   std::unique_ptr<rtc::VirtualSocketServer> vss_;
1264   rtc::AutoSocketServerThread main_;
1265   rtc::scoped_refptr<FakeAudioCaptureModule> fake_audio_capture_module_;
1266   cricket::FakePortAllocator* port_allocator_ = nullptr;
1267   FakeRTCCertificateGenerator* fake_certificate_generator_ = nullptr;
1268   rtc::scoped_refptr<webrtc::PeerConnectionFactoryInterface> pc_factory_;
1269   rtc::scoped_refptr<PeerConnectionFactoryForTest> pc_factory_for_test_;
1270   rtc::scoped_refptr<PeerConnectionInterface> pc_;
1271   MockPeerConnectionObserver observer_;
1272   rtc::scoped_refptr<StreamCollection> reference_collection_;
1273   const SdpSemantics sdp_semantics_;
1274 };
1275 
1276 class PeerConnectionInterfaceTest
1277     : public PeerConnectionInterfaceBaseTest,
1278       public ::testing::WithParamInterface<SdpSemantics> {
1279  protected:
PeerConnectionInterfaceTest()1280   PeerConnectionInterfaceTest() : PeerConnectionInterfaceBaseTest(GetParam()) {}
1281 };
1282 
1283 class PeerConnectionInterfaceTestPlanB
1284     : public PeerConnectionInterfaceBaseTest {
1285  protected:
PeerConnectionInterfaceTestPlanB()1286   PeerConnectionInterfaceTestPlanB()
1287       : PeerConnectionInterfaceBaseTest(SdpSemantics::kPlanB) {}
1288 };
1289 
1290 // Generate different CNAMEs when PeerConnections are created.
1291 // The CNAMEs are expected to be generated randomly. It is possible
1292 // that the test fails, though the possibility is very low.
TEST_P(PeerConnectionInterfaceTest,CnameGenerationInOffer)1293 TEST_P(PeerConnectionInterfaceTest, CnameGenerationInOffer) {
1294   std::unique_ptr<SessionDescriptionInterface> offer1 =
1295       CreateOfferWithOneAudioTrack();
1296   std::unique_ptr<SessionDescriptionInterface> offer2 =
1297       CreateOfferWithOneAudioTrack();
1298   EXPECT_NE(GetFirstAudioStreamCname(offer1.get()),
1299             GetFirstAudioStreamCname(offer2.get()));
1300 }
1301 
TEST_P(PeerConnectionInterfaceTest,CnameGenerationInAnswer)1302 TEST_P(PeerConnectionInterfaceTest, CnameGenerationInAnswer) {
1303   std::unique_ptr<SessionDescriptionInterface> answer1 =
1304       CreateAnswerWithOneAudioTrack();
1305   std::unique_ptr<SessionDescriptionInterface> answer2 =
1306       CreateAnswerWithOneAudioTrack();
1307   EXPECT_NE(GetFirstAudioStreamCname(answer1.get()),
1308             GetFirstAudioStreamCname(answer2.get()));
1309 }
1310 
TEST_P(PeerConnectionInterfaceTest,CreatePeerConnectionWithDifferentConfigurations)1311 TEST_P(PeerConnectionInterfaceTest,
1312        CreatePeerConnectionWithDifferentConfigurations) {
1313   CreatePeerConnectionWithDifferentConfigurations();
1314 }
1315 
TEST_P(PeerConnectionInterfaceTest,CreatePeerConnectionWithDifferentIceTransportsTypes)1316 TEST_P(PeerConnectionInterfaceTest,
1317        CreatePeerConnectionWithDifferentIceTransportsTypes) {
1318   CreatePeerConnectionWithIceTransportsType(PeerConnectionInterface::kNone);
1319   EXPECT_EQ(cricket::CF_NONE, port_allocator_->candidate_filter());
1320   CreatePeerConnectionWithIceTransportsType(PeerConnectionInterface::kRelay);
1321   EXPECT_EQ(cricket::CF_RELAY, port_allocator_->candidate_filter());
1322   CreatePeerConnectionWithIceTransportsType(PeerConnectionInterface::kNoHost);
1323   EXPECT_EQ(cricket::CF_ALL & ~cricket::CF_HOST,
1324             port_allocator_->candidate_filter());
1325   CreatePeerConnectionWithIceTransportsType(PeerConnectionInterface::kAll);
1326   EXPECT_EQ(cricket::CF_ALL, port_allocator_->candidate_filter());
1327 }
1328 
1329 // Test that when a PeerConnection is created with a nonzero candidate pool
1330 // size, the pooled PortAllocatorSession is created with all the attributes
1331 // in the RTCConfiguration.
TEST_P(PeerConnectionInterfaceTest,CreatePeerConnectionWithPooledCandidates)1332 TEST_P(PeerConnectionInterfaceTest, CreatePeerConnectionWithPooledCandidates) {
1333   PeerConnectionInterface::RTCConfiguration config;
1334   PeerConnectionInterface::IceServer server;
1335   server.uri = kStunAddressOnly;
1336   config.servers.push_back(server);
1337   config.type = PeerConnectionInterface::kRelay;
1338   config.disable_ipv6 = true;
1339   config.tcp_candidate_policy =
1340       PeerConnectionInterface::kTcpCandidatePolicyDisabled;
1341   config.candidate_network_policy =
1342       PeerConnectionInterface::kCandidateNetworkPolicyLowCost;
1343   config.ice_candidate_pool_size = 1;
1344   CreatePeerConnection(config);
1345 
1346   const cricket::FakePortAllocatorSession* session =
1347       static_cast<const cricket::FakePortAllocatorSession*>(
1348           port_allocator_->GetPooledSession());
1349   ASSERT_NE(nullptr, session);
1350   EXPECT_EQ(1UL, session->stun_servers().size());
1351   EXPECT_EQ(0U, session->flags() & cricket::PORTALLOCATOR_ENABLE_IPV6);
1352   EXPECT_LT(0U, session->flags() & cricket::PORTALLOCATOR_DISABLE_TCP);
1353   EXPECT_LT(0U,
1354             session->flags() & cricket::PORTALLOCATOR_DISABLE_COSTLY_NETWORKS);
1355 }
1356 
1357 // Test that network-related RTCConfiguration members are applied to the
1358 // PortAllocator when CreatePeerConnection is called. Specifically:
1359 // - disable_ipv6_on_wifi
1360 // - max_ipv6_networks
1361 // - tcp_candidate_policy
1362 // - candidate_network_policy
1363 // - prune_turn_ports
1364 //
1365 // Note that the candidate filter (RTCConfiguration::type) is already tested
1366 // above.
TEST_P(PeerConnectionInterfaceTest,CreatePeerConnectionAppliesNetworkConfigToPortAllocator)1367 TEST_P(PeerConnectionInterfaceTest,
1368        CreatePeerConnectionAppliesNetworkConfigToPortAllocator) {
1369   // Create fake port allocator.
1370   std::unique_ptr<cricket::FakePortAllocator> port_allocator(
1371       new cricket::FakePortAllocator(rtc::Thread::Current(), nullptr));
1372   cricket::FakePortAllocator* raw_port_allocator = port_allocator.get();
1373 
1374   // Create RTCConfiguration with some network-related fields relevant to
1375   // PortAllocator populated.
1376   PeerConnectionInterface::RTCConfiguration config;
1377   config.disable_ipv6_on_wifi = true;
1378   config.max_ipv6_networks = 10;
1379   config.tcp_candidate_policy =
1380       PeerConnectionInterface::kTcpCandidatePolicyDisabled;
1381   config.candidate_network_policy =
1382       PeerConnectionInterface::kCandidateNetworkPolicyLowCost;
1383   config.prune_turn_ports = true;
1384 
1385   // Create the PC factory and PC with the above config.
1386   rtc::scoped_refptr<webrtc::PeerConnectionFactoryInterface> pc_factory(
1387       webrtc::CreatePeerConnectionFactory(
1388           rtc::Thread::Current(), rtc::Thread::Current(),
1389           rtc::Thread::Current(), fake_audio_capture_module_,
1390           webrtc::CreateBuiltinAudioEncoderFactory(),
1391           webrtc::CreateBuiltinAudioDecoderFactory(),
1392           webrtc::CreateBuiltinVideoEncoderFactory(),
1393           webrtc::CreateBuiltinVideoDecoderFactory(), nullptr /* audio_mixer */,
1394           nullptr /* audio_processing */));
1395   rtc::scoped_refptr<PeerConnectionInterface> pc(
1396       pc_factory->CreatePeerConnection(config, std::move(port_allocator),
1397                                        nullptr, &observer_));
1398   EXPECT_TRUE(pc.get());
1399   observer_.SetPeerConnectionInterface(pc.get());
1400 
1401   // Now validate that the config fields set above were applied to the
1402   // PortAllocator, as flags or otherwise.
1403   EXPECT_FALSE(raw_port_allocator->flags() &
1404                cricket::PORTALLOCATOR_ENABLE_IPV6_ON_WIFI);
1405   EXPECT_EQ(10, raw_port_allocator->max_ipv6_networks());
1406   EXPECT_TRUE(raw_port_allocator->flags() & cricket::PORTALLOCATOR_DISABLE_TCP);
1407   EXPECT_TRUE(raw_port_allocator->flags() &
1408               cricket::PORTALLOCATOR_DISABLE_COSTLY_NETWORKS);
1409   EXPECT_EQ(webrtc::PRUNE_BASED_ON_PRIORITY,
1410             raw_port_allocator->turn_port_prune_policy());
1411 }
1412 
1413 // Check that GetConfiguration returns the configuration the PeerConnection was
1414 // constructed with, before SetConfiguration is called.
TEST_P(PeerConnectionInterfaceTest,GetConfigurationAfterCreatePeerConnection)1415 TEST_P(PeerConnectionInterfaceTest, GetConfigurationAfterCreatePeerConnection) {
1416   PeerConnectionInterface::RTCConfiguration config;
1417   config.type = PeerConnectionInterface::kRelay;
1418   CreatePeerConnection(config);
1419 
1420   PeerConnectionInterface::RTCConfiguration returned_config =
1421       pc_->GetConfiguration();
1422   EXPECT_EQ(PeerConnectionInterface::kRelay, returned_config.type);
1423 }
1424 
1425 // Check that GetConfiguration returns the last configuration passed into
1426 // SetConfiguration.
TEST_P(PeerConnectionInterfaceTest,GetConfigurationAfterSetConfiguration)1427 TEST_P(PeerConnectionInterfaceTest, GetConfigurationAfterSetConfiguration) {
1428   PeerConnectionInterface::RTCConfiguration starting_config;
1429   starting_config.bundle_policy =
1430       webrtc::PeerConnection::kBundlePolicyMaxBundle;
1431   CreatePeerConnection(starting_config);
1432 
1433   PeerConnectionInterface::RTCConfiguration config = pc_->GetConfiguration();
1434   config.type = PeerConnectionInterface::kRelay;
1435   EXPECT_TRUE(pc_->SetConfiguration(config).ok());
1436 
1437   PeerConnectionInterface::RTCConfiguration returned_config =
1438       pc_->GetConfiguration();
1439   EXPECT_EQ(PeerConnectionInterface::kRelay, returned_config.type);
1440 }
1441 
TEST_P(PeerConnectionInterfaceTest,SetConfigurationFailsAfterClose)1442 TEST_P(PeerConnectionInterfaceTest, SetConfigurationFailsAfterClose) {
1443   CreatePeerConnection();
1444 
1445   pc_->Close();
1446 
1447   EXPECT_FALSE(
1448       pc_->SetConfiguration(PeerConnectionInterface::RTCConfiguration()).ok());
1449 }
1450 
TEST_F(PeerConnectionInterfaceTestPlanB,AddStreams)1451 TEST_F(PeerConnectionInterfaceTestPlanB, AddStreams) {
1452   CreatePeerConnectionWithoutDtls();
1453   AddVideoStream(kStreamId1);
1454   AddAudioStream(kStreamId2);
1455   ASSERT_EQ(2u, pc_->local_streams()->count());
1456 
1457   // Test we can add multiple local streams to one peerconnection.
1458   rtc::scoped_refptr<MediaStreamInterface> stream(
1459       pc_factory_->CreateLocalMediaStream(kStreamId3));
1460   rtc::scoped_refptr<AudioTrackInterface> audio_track(
1461       pc_factory_->CreateAudioTrack(kStreamId3,
1462                                     static_cast<AudioSourceInterface*>(NULL)));
1463   stream->AddTrack(audio_track.get());
1464   EXPECT_TRUE(pc_->AddStream(stream));
1465   EXPECT_EQ(3u, pc_->local_streams()->count());
1466 
1467   // Remove the third stream.
1468   pc_->RemoveStream(pc_->local_streams()->at(2));
1469   EXPECT_EQ(2u, pc_->local_streams()->count());
1470 
1471   // Remove the second stream.
1472   pc_->RemoveStream(pc_->local_streams()->at(1));
1473   EXPECT_EQ(1u, pc_->local_streams()->count());
1474 
1475   // Remove the first stream.
1476   pc_->RemoveStream(pc_->local_streams()->at(0));
1477   EXPECT_EQ(0u, pc_->local_streams()->count());
1478 }
1479 
1480 // Test that the created offer includes streams we added.
1481 // Don't run under Unified Plan since the stream API is not available.
TEST_F(PeerConnectionInterfaceTestPlanB,AddedStreamsPresentInOffer)1482 TEST_F(PeerConnectionInterfaceTestPlanB, AddedStreamsPresentInOffer) {
1483   CreatePeerConnectionWithoutDtls();
1484   AddAudioVideoStream(kStreamId1, "audio_track", "video_track");
1485   std::unique_ptr<SessionDescriptionInterface> offer;
1486   ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
1487 
1488   const cricket::AudioContentDescription* audio_desc =
1489       cricket::GetFirstAudioContentDescription(offer->description());
1490   EXPECT_TRUE(ContainsTrack(audio_desc->streams(), kStreamId1, "audio_track"));
1491 
1492   const cricket::VideoContentDescription* video_desc =
1493       cricket::GetFirstVideoContentDescription(offer->description());
1494   EXPECT_TRUE(ContainsTrack(video_desc->streams(), kStreamId1, "video_track"));
1495 
1496   // Add another stream and ensure the offer includes both the old and new
1497   // streams.
1498   AddAudioVideoStream(kStreamId2, "audio_track2", "video_track2");
1499   ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
1500 
1501   audio_desc = cricket::GetFirstAudioContentDescription(offer->description());
1502   EXPECT_TRUE(ContainsTrack(audio_desc->streams(), kStreamId1, "audio_track"));
1503   EXPECT_TRUE(ContainsTrack(audio_desc->streams(), kStreamId2, "audio_track2"));
1504 
1505   video_desc = cricket::GetFirstVideoContentDescription(offer->description());
1506   EXPECT_TRUE(ContainsTrack(video_desc->streams(), kStreamId1, "video_track"));
1507   EXPECT_TRUE(ContainsTrack(video_desc->streams(), kStreamId2, "video_track2"));
1508 }
1509 
1510 // Don't run under Unified Plan since the stream API is not available.
TEST_F(PeerConnectionInterfaceTestPlanB,RemoveStream)1511 TEST_F(PeerConnectionInterfaceTestPlanB, RemoveStream) {
1512   CreatePeerConnectionWithoutDtls();
1513   AddVideoStream(kStreamId1);
1514   ASSERT_EQ(1u, pc_->local_streams()->count());
1515   pc_->RemoveStream(pc_->local_streams()->at(0));
1516   EXPECT_EQ(0u, pc_->local_streams()->count());
1517 }
1518 
1519 // Test for AddTrack and RemoveTrack methods.
1520 // Tests that the created offer includes tracks we added,
1521 // and that the RtpSenders are created correctly.
1522 // Also tests that RemoveTrack removes the tracks from subsequent offers.
1523 // Only tested with Plan B since Unified Plan is covered in more detail by tests
1524 // in peerconnection_jsep_unittests.cc
TEST_F(PeerConnectionInterfaceTestPlanB,AddTrackRemoveTrack)1525 TEST_F(PeerConnectionInterfaceTestPlanB, AddTrackRemoveTrack) {
1526   CreatePeerConnectionWithoutDtls();
1527   rtc::scoped_refptr<AudioTrackInterface> audio_track(
1528       CreateAudioTrack("audio_track"));
1529   rtc::scoped_refptr<VideoTrackInterface> video_track(
1530       CreateVideoTrack("video_track"));
1531   auto audio_sender = pc_->AddTrack(audio_track, {kStreamId1}).MoveValue();
1532   auto video_sender = pc_->AddTrack(video_track, {kStreamId1}).MoveValue();
1533   EXPECT_EQ(1UL, audio_sender->stream_ids().size());
1534   EXPECT_EQ(kStreamId1, audio_sender->stream_ids()[0]);
1535   EXPECT_EQ("audio_track", audio_sender->id());
1536   EXPECT_EQ(audio_track, audio_sender->track());
1537   EXPECT_EQ(1UL, video_sender->stream_ids().size());
1538   EXPECT_EQ(kStreamId1, video_sender->stream_ids()[0]);
1539   EXPECT_EQ("video_track", video_sender->id());
1540   EXPECT_EQ(video_track, video_sender->track());
1541 
1542   // Now create an offer and check for the senders.
1543   std::unique_ptr<SessionDescriptionInterface> offer;
1544   ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
1545 
1546   const cricket::ContentInfo* audio_content =
1547       cricket::GetFirstAudioContent(offer->description());
1548   EXPECT_TRUE(ContainsTrack(audio_content->media_description()->streams(),
1549                             kStreamId1, "audio_track"));
1550 
1551   const cricket::ContentInfo* video_content =
1552       cricket::GetFirstVideoContent(offer->description());
1553   EXPECT_TRUE(ContainsTrack(video_content->media_description()->streams(),
1554                             kStreamId1, "video_track"));
1555 
1556   EXPECT_TRUE(DoSetLocalDescription(std::move(offer)));
1557 
1558   // Now try removing the tracks.
1559   EXPECT_TRUE(pc_->RemoveTrack(audio_sender));
1560   EXPECT_TRUE(pc_->RemoveTrack(video_sender));
1561 
1562   // Create a new offer and ensure it doesn't contain the removed senders.
1563   ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
1564 
1565   audio_content = cricket::GetFirstAudioContent(offer->description());
1566   EXPECT_FALSE(ContainsTrack(audio_content->media_description()->streams(),
1567                              kStreamId1, "audio_track"));
1568 
1569   video_content = cricket::GetFirstVideoContent(offer->description());
1570   EXPECT_FALSE(ContainsTrack(video_content->media_description()->streams(),
1571                              kStreamId1, "video_track"));
1572 
1573   EXPECT_TRUE(DoSetLocalDescription(std::move(offer)));
1574 
1575   // Calling RemoveTrack on a sender no longer attached to a PeerConnection
1576   // should return false.
1577   EXPECT_FALSE(pc_->RemoveTrack(audio_sender));
1578   EXPECT_FALSE(pc_->RemoveTrack(video_sender));
1579 }
1580 
1581 // Test creating senders without a stream specified,
1582 // expecting a random stream ID to be generated.
TEST_P(PeerConnectionInterfaceTest,AddTrackWithoutStream)1583 TEST_P(PeerConnectionInterfaceTest, AddTrackWithoutStream) {
1584   CreatePeerConnectionWithoutDtls();
1585   rtc::scoped_refptr<AudioTrackInterface> audio_track(
1586       CreateAudioTrack("audio_track"));
1587   rtc::scoped_refptr<VideoTrackInterface> video_track(
1588       CreateVideoTrack("video_track"));
1589   auto audio_sender =
1590       pc_->AddTrack(audio_track, std::vector<std::string>()).MoveValue();
1591   auto video_sender =
1592       pc_->AddTrack(video_track, std::vector<std::string>()).MoveValue();
1593   EXPECT_EQ("audio_track", audio_sender->id());
1594   EXPECT_EQ(audio_track, audio_sender->track());
1595   EXPECT_EQ("video_track", video_sender->id());
1596   EXPECT_EQ(video_track, video_sender->track());
1597   if (sdp_semantics_ == SdpSemantics::kPlanB) {
1598     // If the ID is truly a random GUID, it should be infinitely unlikely they
1599     // will be the same.
1600     EXPECT_NE(video_sender->stream_ids(), audio_sender->stream_ids());
1601   } else {
1602     // We allows creating tracks without stream ids under Unified Plan
1603     // semantics.
1604     EXPECT_EQ(0u, video_sender->stream_ids().size());
1605     EXPECT_EQ(0u, audio_sender->stream_ids().size());
1606   }
1607 }
1608 
1609 // Test that we can call GetStats() after AddTrack but before connecting
1610 // the PeerConnection to a peer.
TEST_P(PeerConnectionInterfaceTest,AddTrackBeforeConnecting)1611 TEST_P(PeerConnectionInterfaceTest, AddTrackBeforeConnecting) {
1612   CreatePeerConnectionWithoutDtls();
1613   rtc::scoped_refptr<AudioTrackInterface> audio_track(
1614       CreateAudioTrack("audio_track"));
1615   rtc::scoped_refptr<VideoTrackInterface> video_track(
1616       CreateVideoTrack("video_track"));
1617   auto audio_sender = pc_->AddTrack(audio_track, std::vector<std::string>());
1618   auto video_sender = pc_->AddTrack(video_track, std::vector<std::string>());
1619   EXPECT_TRUE(DoGetStats(nullptr));
1620 }
1621 
TEST_P(PeerConnectionInterfaceTest,AttachmentIdIsSetOnAddTrack)1622 TEST_P(PeerConnectionInterfaceTest, AttachmentIdIsSetOnAddTrack) {
1623   CreatePeerConnectionWithoutDtls();
1624   rtc::scoped_refptr<AudioTrackInterface> audio_track(
1625       CreateAudioTrack("audio_track"));
1626   rtc::scoped_refptr<VideoTrackInterface> video_track(
1627       CreateVideoTrack("video_track"));
1628   auto audio_sender = pc_->AddTrack(audio_track, std::vector<std::string>());
1629   ASSERT_TRUE(audio_sender.ok());
1630   auto* audio_sender_proxy =
1631       static_cast<RtpSenderProxyWithInternal<RtpSenderInternal>*>(
1632           audio_sender.value().get());
1633   EXPECT_NE(0, audio_sender_proxy->internal()->AttachmentId());
1634 
1635   auto video_sender = pc_->AddTrack(video_track, std::vector<std::string>());
1636   ASSERT_TRUE(video_sender.ok());
1637   auto* video_sender_proxy =
1638       static_cast<RtpSenderProxyWithInternal<RtpSenderInternal>*>(
1639           video_sender.value().get());
1640   EXPECT_NE(0, video_sender_proxy->internal()->AttachmentId());
1641 }
1642 
1643 // Don't run under Unified Plan since the stream API is not available.
TEST_F(PeerConnectionInterfaceTestPlanB,AttachmentIdIsSetOnAddStream)1644 TEST_F(PeerConnectionInterfaceTestPlanB, AttachmentIdIsSetOnAddStream) {
1645   CreatePeerConnectionWithoutDtls();
1646   AddVideoStream(kStreamId1);
1647   auto senders = pc_->GetSenders();
1648   ASSERT_EQ(1u, senders.size());
1649   auto* sender_proxy =
1650       static_cast<RtpSenderProxyWithInternal<RtpSenderInternal>*>(
1651           senders[0].get());
1652   EXPECT_NE(0, sender_proxy->internal()->AttachmentId());
1653 }
1654 
TEST_P(PeerConnectionInterfaceTest,CreateOfferReceiveAnswer)1655 TEST_P(PeerConnectionInterfaceTest, CreateOfferReceiveAnswer) {
1656   InitiateCall();
1657   WaitAndVerifyOnAddStream(kStreamId1, 2);
1658   VerifyRemoteRtpHeaderExtensions();
1659 }
1660 
TEST_P(PeerConnectionInterfaceTest,CreateOfferReceivePrAnswerAndAnswer)1661 TEST_P(PeerConnectionInterfaceTest, CreateOfferReceivePrAnswerAndAnswer) {
1662   CreatePeerConnectionWithoutDtls();
1663   AddVideoTrack(kVideoTracks[0], {kStreamId1});
1664   CreateOfferAsLocalDescription();
1665   std::string offer;
1666   EXPECT_TRUE(pc_->local_description()->ToString(&offer));
1667   CreatePrAnswerAndAnswerAsRemoteDescription(offer);
1668   WaitAndVerifyOnAddStream(kStreamId1, 1);
1669 }
1670 
TEST_P(PeerConnectionInterfaceTest,ReceiveOfferCreateAnswer)1671 TEST_P(PeerConnectionInterfaceTest, ReceiveOfferCreateAnswer) {
1672   CreatePeerConnectionWithoutDtls();
1673   AddVideoTrack(kVideoTracks[0], {kStreamId1});
1674 
1675   CreateOfferAsRemoteDescription();
1676   CreateAnswerAsLocalDescription();
1677 
1678   WaitAndVerifyOnAddStream(kStreamId1, 1);
1679 }
1680 
TEST_P(PeerConnectionInterfaceTest,ReceiveOfferCreatePrAnswerAndAnswer)1681 TEST_P(PeerConnectionInterfaceTest, ReceiveOfferCreatePrAnswerAndAnswer) {
1682   CreatePeerConnectionWithoutDtls();
1683   AddVideoTrack(kVideoTracks[0], {kStreamId1});
1684 
1685   CreateOfferAsRemoteDescription();
1686   CreatePrAnswerAsLocalDescription();
1687   CreateAnswerAsLocalDescription();
1688 
1689   WaitAndVerifyOnAddStream(kStreamId1, 1);
1690 }
1691 
1692 // Don't run under Unified Plan since the stream API is not available.
TEST_F(PeerConnectionInterfaceTestPlanB,Renegotiate)1693 TEST_F(PeerConnectionInterfaceTestPlanB, Renegotiate) {
1694   InitiateCall();
1695   ASSERT_EQ(1u, pc_->remote_streams()->count());
1696   pc_->RemoveStream(pc_->local_streams()->at(0));
1697   CreateOfferReceiveAnswer();
1698   EXPECT_EQ(0u, pc_->remote_streams()->count());
1699   AddVideoStream(kStreamId1);
1700   CreateOfferReceiveAnswer();
1701 }
1702 
1703 // Tests that after negotiating an audio only call, the respondent can perform a
1704 // renegotiation that removes the audio stream.
TEST_F(PeerConnectionInterfaceTestPlanB,RenegotiateAudioOnly)1705 TEST_F(PeerConnectionInterfaceTestPlanB, RenegotiateAudioOnly) {
1706   CreatePeerConnectionWithoutDtls();
1707   AddAudioStream(kStreamId1);
1708   CreateOfferAsRemoteDescription();
1709   CreateAnswerAsLocalDescription();
1710 
1711   ASSERT_EQ(1u, pc_->remote_streams()->count());
1712   pc_->RemoveStream(pc_->local_streams()->at(0));
1713   CreateOfferReceiveAnswer();
1714   EXPECT_EQ(0u, pc_->remote_streams()->count());
1715 }
1716 
1717 // Test that candidates are generated and that we can parse our own candidates.
TEST_P(PeerConnectionInterfaceTest,IceCandidates)1718 TEST_P(PeerConnectionInterfaceTest, IceCandidates) {
1719   CreatePeerConnectionWithoutDtls();
1720 
1721   EXPECT_FALSE(pc_->AddIceCandidate(observer_.last_candidate()));
1722   // SetRemoteDescription takes ownership of offer.
1723   std::unique_ptr<SessionDescriptionInterface> offer;
1724   AddVideoTrack(kVideoTracks[0]);
1725   EXPECT_TRUE(DoCreateOffer(&offer, nullptr));
1726   EXPECT_TRUE(DoSetRemoteDescription(std::move(offer)));
1727 
1728   // SetLocalDescription takes ownership of answer.
1729   std::unique_ptr<SessionDescriptionInterface> answer;
1730   EXPECT_TRUE(DoCreateAnswer(&answer, nullptr));
1731   EXPECT_TRUE(DoSetLocalDescription(std::move(answer)));
1732 
1733   EXPECT_TRUE_WAIT(observer_.last_candidate() != nullptr, kTimeout);
1734   EXPECT_TRUE_WAIT(observer_.ice_gathering_complete_, kTimeout);
1735 
1736   EXPECT_TRUE(pc_->AddIceCandidate(observer_.last_candidate()));
1737 }
1738 
1739 // Test that CreateOffer and CreateAnswer will fail if the track labels are
1740 // not unique.
TEST_F(PeerConnectionInterfaceTestPlanB,CreateOfferAnswerWithInvalidStream)1741 TEST_F(PeerConnectionInterfaceTestPlanB, CreateOfferAnswerWithInvalidStream) {
1742   CreatePeerConnectionWithoutDtls();
1743   // Create a regular offer for the CreateAnswer test later.
1744   std::unique_ptr<SessionDescriptionInterface> offer;
1745   EXPECT_TRUE(DoCreateOffer(&offer, nullptr));
1746   EXPECT_TRUE(offer);
1747   offer.reset();
1748 
1749   // Create a local stream with audio&video tracks having same label.
1750   AddAudioTrack("track_label", {kStreamId1});
1751   AddVideoTrack("track_label", {kStreamId1});
1752 
1753   // Test CreateOffer
1754   EXPECT_FALSE(DoCreateOffer(&offer, nullptr));
1755 
1756   // Test CreateAnswer
1757   std::unique_ptr<SessionDescriptionInterface> answer;
1758   EXPECT_FALSE(DoCreateAnswer(&answer, nullptr));
1759 }
1760 
1761 // Test that we will get different SSRCs for each tracks in the offer and answer
1762 // we created.
TEST_P(PeerConnectionInterfaceTest,SsrcInOfferAnswer)1763 TEST_P(PeerConnectionInterfaceTest, SsrcInOfferAnswer) {
1764   CreatePeerConnectionWithoutDtls();
1765   // Create a local stream with audio&video tracks having different labels.
1766   AddAudioTrack(kAudioTracks[0], {kStreamId1});
1767   AddVideoTrack(kVideoTracks[0], {kStreamId1});
1768 
1769   // Test CreateOffer
1770   std::unique_ptr<SessionDescriptionInterface> offer;
1771   ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
1772   int audio_ssrc = 0;
1773   int video_ssrc = 0;
1774   EXPECT_TRUE(
1775       GetFirstSsrc(GetFirstAudioContent(offer->description()), &audio_ssrc));
1776   EXPECT_TRUE(
1777       GetFirstSsrc(GetFirstVideoContent(offer->description()), &video_ssrc));
1778   EXPECT_NE(audio_ssrc, video_ssrc);
1779 
1780   // Test CreateAnswer
1781   EXPECT_TRUE(DoSetRemoteDescription(std::move(offer)));
1782   std::unique_ptr<SessionDescriptionInterface> answer;
1783   ASSERT_TRUE(DoCreateAnswer(&answer, nullptr));
1784   audio_ssrc = 0;
1785   video_ssrc = 0;
1786   EXPECT_TRUE(
1787       GetFirstSsrc(GetFirstAudioContent(answer->description()), &audio_ssrc));
1788   EXPECT_TRUE(
1789       GetFirstSsrc(GetFirstVideoContent(answer->description()), &video_ssrc));
1790   EXPECT_NE(audio_ssrc, video_ssrc);
1791 }
1792 
1793 // Test that it's possible to call AddTrack on a MediaStream after adding
1794 // the stream to a PeerConnection.
1795 // TODO(deadbeef): Remove this test once this behavior is no longer supported.
1796 // Don't run under Unified Plan since the stream API is not available.
TEST_F(PeerConnectionInterfaceTestPlanB,AddTrackAfterAddStream)1797 TEST_F(PeerConnectionInterfaceTestPlanB, AddTrackAfterAddStream) {
1798   CreatePeerConnectionWithoutDtls();
1799   // Create audio stream and add to PeerConnection.
1800   AddAudioStream(kStreamId1);
1801   MediaStreamInterface* stream = pc_->local_streams()->at(0);
1802 
1803   // Add video track to the audio-only stream.
1804   rtc::scoped_refptr<VideoTrackInterface> video_track(
1805       CreateVideoTrack("video_label"));
1806   stream->AddTrack(video_track.get());
1807 
1808   std::unique_ptr<SessionDescriptionInterface> offer;
1809   ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
1810 
1811   const cricket::MediaContentDescription* video_desc =
1812       cricket::GetFirstVideoContentDescription(offer->description());
1813   EXPECT_TRUE(video_desc != nullptr);
1814 }
1815 
1816 // Test that it's possible to call RemoveTrack on a MediaStream after adding
1817 // the stream to a PeerConnection.
1818 // TODO(deadbeef): Remove this test once this behavior is no longer supported.
1819 // Don't run under Unified Plan since the stream API is not available.
TEST_F(PeerConnectionInterfaceTestPlanB,RemoveTrackAfterAddStream)1820 TEST_F(PeerConnectionInterfaceTestPlanB, RemoveTrackAfterAddStream) {
1821   CreatePeerConnectionWithoutDtls();
1822   // Create audio/video stream and add to PeerConnection.
1823   AddAudioVideoStream(kStreamId1, "audio_label", "video_label");
1824   MediaStreamInterface* stream = pc_->local_streams()->at(0);
1825 
1826   // Remove the video track.
1827   stream->RemoveTrack(stream->GetVideoTracks()[0]);
1828 
1829   std::unique_ptr<SessionDescriptionInterface> offer;
1830   ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
1831 
1832   const cricket::MediaContentDescription* video_desc =
1833       cricket::GetFirstVideoContentDescription(offer->description());
1834   EXPECT_TRUE(video_desc == nullptr);
1835 }
1836 
1837 // Test creating a sender with a stream ID, and ensure the ID is populated
1838 // in the offer.
1839 // Don't run under Unified Plan since the stream API is not available.
TEST_F(PeerConnectionInterfaceTestPlanB,CreateSenderWithStream)1840 TEST_F(PeerConnectionInterfaceTestPlanB, CreateSenderWithStream) {
1841   CreatePeerConnectionWithoutDtls();
1842   pc_->CreateSender("video", kStreamId1);
1843 
1844   std::unique_ptr<SessionDescriptionInterface> offer;
1845   ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
1846 
1847   const cricket::MediaContentDescription* video_desc =
1848       cricket::GetFirstVideoContentDescription(offer->description());
1849   ASSERT_TRUE(video_desc != nullptr);
1850   ASSERT_EQ(1u, video_desc->streams().size());
1851   EXPECT_EQ(kStreamId1, video_desc->streams()[0].first_stream_id());
1852 }
1853 
1854 // Test that we can specify a certain track that we want statistics about.
TEST_P(PeerConnectionInterfaceTest,GetStatsForSpecificTrack)1855 TEST_P(PeerConnectionInterfaceTest, GetStatsForSpecificTrack) {
1856   InitiateCall();
1857   ASSERT_LT(0u, pc_->GetSenders().size());
1858   ASSERT_LT(0u, pc_->GetReceivers().size());
1859   rtc::scoped_refptr<MediaStreamTrackInterface> remote_audio =
1860       pc_->GetReceivers()[0]->track();
1861   EXPECT_TRUE(DoGetStats(remote_audio));
1862 
1863   // Remove the stream. Since we are sending to our selves the local
1864   // and the remote stream is the same.
1865   pc_->RemoveTrack(pc_->GetSenders()[0]);
1866   // Do a re-negotiation.
1867   CreateOfferReceiveAnswer();
1868 
1869   // Test that we still can get statistics for the old track. Even if it is not
1870   // sent any longer.
1871   EXPECT_TRUE(DoGetStats(remote_audio));
1872 }
1873 
1874 // Test that we can get stats on a video track.
TEST_P(PeerConnectionInterfaceTest,GetStatsForVideoTrack)1875 TEST_P(PeerConnectionInterfaceTest, GetStatsForVideoTrack) {
1876   InitiateCall();
1877   auto video_receiver = GetFirstReceiverOfType(cricket::MEDIA_TYPE_VIDEO);
1878   ASSERT_TRUE(video_receiver);
1879   EXPECT_TRUE(DoGetStats(video_receiver->track()));
1880 }
1881 
1882 // Test that we don't get statistics for an invalid track.
TEST_P(PeerConnectionInterfaceTest,GetStatsForInvalidTrack)1883 TEST_P(PeerConnectionInterfaceTest, GetStatsForInvalidTrack) {
1884   InitiateCall();
1885   rtc::scoped_refptr<AudioTrackInterface> unknown_audio_track(
1886       pc_factory_->CreateAudioTrack("unknown track", NULL));
1887   EXPECT_FALSE(DoGetStats(unknown_audio_track));
1888 }
1889 
TEST_P(PeerConnectionInterfaceTest,GetRTCStatsBeforeAndAfterCalling)1890 TEST_P(PeerConnectionInterfaceTest, GetRTCStatsBeforeAndAfterCalling) {
1891   CreatePeerConnectionWithoutDtls();
1892   EXPECT_TRUE(DoGetRTCStats());
1893   // Clearing stats cache is needed now, but should be temporary.
1894   // https://bugs.chromium.org/p/webrtc/issues/detail?id=8693
1895   pc_->ClearStatsCache();
1896   AddAudioTrack(kAudioTracks[0], {kStreamId1});
1897   AddVideoTrack(kVideoTracks[0], {kStreamId1});
1898   EXPECT_TRUE(DoGetRTCStats());
1899   pc_->ClearStatsCache();
1900   CreateOfferReceiveAnswer();
1901   EXPECT_TRUE(DoGetRTCStats());
1902 }
1903 
1904 // This test setup two RTP data channels in loop back.
TEST_P(PeerConnectionInterfaceTest,TestDataChannel)1905 TEST_P(PeerConnectionInterfaceTest, TestDataChannel) {
1906   RTCConfiguration config;
1907   config.enable_rtp_data_channel = true;
1908   config.enable_dtls_srtp = false;
1909   CreatePeerConnection(config);
1910   rtc::scoped_refptr<DataChannelInterface> data1 =
1911       pc_->CreateDataChannel("test1", NULL);
1912   rtc::scoped_refptr<DataChannelInterface> data2 =
1913       pc_->CreateDataChannel("test2", NULL);
1914   ASSERT_TRUE(data1 != NULL);
1915   std::unique_ptr<MockDataChannelObserver> observer1(
1916       new MockDataChannelObserver(data1));
1917   std::unique_ptr<MockDataChannelObserver> observer2(
1918       new MockDataChannelObserver(data2));
1919 
1920   EXPECT_EQ(DataChannelInterface::kConnecting, data1->state());
1921   EXPECT_EQ(DataChannelInterface::kConnecting, data2->state());
1922   std::string data_to_send1 = "testing testing";
1923   std::string data_to_send2 = "testing something else";
1924   EXPECT_FALSE(data1->Send(DataBuffer(data_to_send1)));
1925 
1926   CreateOfferReceiveAnswer();
1927   EXPECT_TRUE_WAIT(observer1->IsOpen(), kTimeout);
1928   EXPECT_TRUE_WAIT(observer2->IsOpen(), kTimeout);
1929 
1930   EXPECT_EQ(DataChannelInterface::kOpen, data1->state());
1931   EXPECT_EQ(DataChannelInterface::kOpen, data2->state());
1932   EXPECT_TRUE(data1->Send(DataBuffer(data_to_send1)));
1933   EXPECT_TRUE(data2->Send(DataBuffer(data_to_send2)));
1934 
1935   EXPECT_EQ_WAIT(data_to_send1, observer1->last_message(), kTimeout);
1936   EXPECT_EQ_WAIT(data_to_send2, observer2->last_message(), kTimeout);
1937 
1938   data1->Close();
1939   EXPECT_EQ(DataChannelInterface::kClosing, data1->state());
1940   CreateOfferReceiveAnswer();
1941   EXPECT_FALSE(observer1->IsOpen());
1942   EXPECT_EQ(DataChannelInterface::kClosed, data1->state());
1943   EXPECT_TRUE(observer2->IsOpen());
1944 
1945   data_to_send2 = "testing something else again";
1946   EXPECT_TRUE(data2->Send(DataBuffer(data_to_send2)));
1947 
1948   EXPECT_EQ_WAIT(data_to_send2, observer2->last_message(), kTimeout);
1949 }
1950 
1951 // This test verifies that sendnig binary data over RTP data channels should
1952 // fail.
TEST_P(PeerConnectionInterfaceTest,TestSendBinaryOnRtpDataChannel)1953 TEST_P(PeerConnectionInterfaceTest, TestSendBinaryOnRtpDataChannel) {
1954   RTCConfiguration config;
1955   config.enable_rtp_data_channel = true;
1956   config.enable_dtls_srtp = false;
1957   CreatePeerConnection(config);
1958   rtc::scoped_refptr<DataChannelInterface> data1 =
1959       pc_->CreateDataChannel("test1", NULL);
1960   rtc::scoped_refptr<DataChannelInterface> data2 =
1961       pc_->CreateDataChannel("test2", NULL);
1962   ASSERT_TRUE(data1 != NULL);
1963   std::unique_ptr<MockDataChannelObserver> observer1(
1964       new MockDataChannelObserver(data1));
1965   std::unique_ptr<MockDataChannelObserver> observer2(
1966       new MockDataChannelObserver(data2));
1967 
1968   EXPECT_EQ(DataChannelInterface::kConnecting, data1->state());
1969   EXPECT_EQ(DataChannelInterface::kConnecting, data2->state());
1970 
1971   CreateOfferReceiveAnswer();
1972   EXPECT_TRUE_WAIT(observer1->IsOpen(), kTimeout);
1973   EXPECT_TRUE_WAIT(observer2->IsOpen(), kTimeout);
1974 
1975   EXPECT_EQ(DataChannelInterface::kOpen, data1->state());
1976   EXPECT_EQ(DataChannelInterface::kOpen, data2->state());
1977 
1978   rtc::CopyOnWriteBuffer buffer("test", 4);
1979   EXPECT_FALSE(data1->Send(DataBuffer(buffer, true)));
1980 }
1981 
1982 // This test setup a RTP data channels in loop back and test that a channel is
1983 // opened even if the remote end answer with a zero SSRC.
TEST_P(PeerConnectionInterfaceTest,TestSendOnlyDataChannel)1984 TEST_P(PeerConnectionInterfaceTest, TestSendOnlyDataChannel) {
1985   RTCConfiguration config;
1986   config.enable_rtp_data_channel = true;
1987   config.enable_dtls_srtp = false;
1988   CreatePeerConnection(config);
1989   rtc::scoped_refptr<DataChannelInterface> data1 =
1990       pc_->CreateDataChannel("test1", NULL);
1991   std::unique_ptr<MockDataChannelObserver> observer1(
1992       new MockDataChannelObserver(data1));
1993 
1994   CreateOfferReceiveAnswerWithoutSsrc();
1995 
1996   EXPECT_TRUE_WAIT(observer1->IsOpen(), kTimeout);
1997 
1998   data1->Close();
1999   EXPECT_EQ(DataChannelInterface::kClosing, data1->state());
2000   CreateOfferReceiveAnswerWithoutSsrc();
2001   EXPECT_EQ(DataChannelInterface::kClosed, data1->state());
2002   EXPECT_FALSE(observer1->IsOpen());
2003 }
2004 
2005 // This test that if a data channel is added in an answer a receive only channel
2006 // channel is created.
TEST_P(PeerConnectionInterfaceTest,TestReceiveOnlyDataChannel)2007 TEST_P(PeerConnectionInterfaceTest, TestReceiveOnlyDataChannel) {
2008   RTCConfiguration config;
2009   config.enable_rtp_data_channel = true;
2010   config.enable_dtls_srtp = false;
2011 
2012   CreatePeerConnection(config);
2013 
2014   std::string offer_label = "offer_channel";
2015   rtc::scoped_refptr<DataChannelInterface> offer_channel =
2016       pc_->CreateDataChannel(offer_label, NULL);
2017 
2018   CreateOfferAsLocalDescription();
2019 
2020   // Replace the data channel label in the offer and apply it as an answer.
2021   std::string receive_label = "answer_channel";
2022   std::string sdp;
2023   EXPECT_TRUE(pc_->local_description()->ToString(&sdp));
2024   absl::StrReplaceAll({{offer_label, receive_label}}, &sdp);
2025   CreateAnswerAsRemoteDescription(sdp);
2026 
2027   // Verify that a new incoming data channel has been created and that
2028   // it is open but can't we written to.
2029   ASSERT_TRUE(observer_.last_datachannel_ != NULL);
2030   DataChannelInterface* received_channel = observer_.last_datachannel_;
2031   EXPECT_EQ(DataChannelInterface::kConnecting, received_channel->state());
2032   EXPECT_EQ(receive_label, received_channel->label());
2033   EXPECT_FALSE(received_channel->Send(DataBuffer("something")));
2034 
2035   // Verify that the channel we initially offered has been rejected.
2036   EXPECT_EQ(DataChannelInterface::kClosed, offer_channel->state());
2037 
2038   // Do another offer / answer exchange and verify that the data channel is
2039   // opened.
2040   CreateOfferReceiveAnswer();
2041   EXPECT_EQ_WAIT(DataChannelInterface::kOpen, received_channel->state(),
2042                  kTimeout);
2043 }
2044 
2045 // This test that no data channel is returned if a reliable channel is
2046 // requested.
2047 // TODO(perkj): Remove this test once reliable channels are implemented.
TEST_P(PeerConnectionInterfaceTest,CreateReliableRtpDataChannelShouldFail)2048 TEST_P(PeerConnectionInterfaceTest, CreateReliableRtpDataChannelShouldFail) {
2049   RTCConfiguration rtc_config;
2050   rtc_config.enable_rtp_data_channel = true;
2051   CreatePeerConnection(rtc_config);
2052 
2053   std::string label = "test";
2054   webrtc::DataChannelInit config;
2055   config.reliable = true;
2056   rtc::scoped_refptr<DataChannelInterface> channel =
2057       pc_->CreateDataChannel(label, &config);
2058   EXPECT_TRUE(channel == NULL);
2059 }
2060 
2061 // Verifies that duplicated label is not allowed for RTP data channel.
TEST_P(PeerConnectionInterfaceTest,RtpDuplicatedLabelNotAllowed)2062 TEST_P(PeerConnectionInterfaceTest, RtpDuplicatedLabelNotAllowed) {
2063   RTCConfiguration config;
2064   config.enable_rtp_data_channel = true;
2065   CreatePeerConnection(config);
2066 
2067   std::string label = "test";
2068   rtc::scoped_refptr<DataChannelInterface> channel =
2069       pc_->CreateDataChannel(label, nullptr);
2070   EXPECT_NE(channel, nullptr);
2071 
2072   rtc::scoped_refptr<DataChannelInterface> dup_channel =
2073       pc_->CreateDataChannel(label, nullptr);
2074   EXPECT_EQ(dup_channel, nullptr);
2075 }
2076 
2077 // This tests that a SCTP data channel is returned using different
2078 // DataChannelInit configurations.
TEST_P(PeerConnectionInterfaceTest,CreateSctpDataChannel)2079 TEST_P(PeerConnectionInterfaceTest, CreateSctpDataChannel) {
2080   RTCConfiguration rtc_config;
2081   rtc_config.enable_dtls_srtp = true;
2082   CreatePeerConnection(rtc_config);
2083 
2084   webrtc::DataChannelInit config;
2085   rtc::scoped_refptr<DataChannelInterface> channel =
2086       pc_->CreateDataChannel("1", &config);
2087   EXPECT_TRUE(channel != NULL);
2088   EXPECT_TRUE(channel->reliable());
2089   EXPECT_TRUE(observer_.renegotiation_needed_);
2090   observer_.renegotiation_needed_ = false;
2091 
2092   config.ordered = false;
2093   channel = pc_->CreateDataChannel("2", &config);
2094   EXPECT_TRUE(channel != NULL);
2095   EXPECT_TRUE(channel->reliable());
2096   EXPECT_FALSE(observer_.renegotiation_needed_);
2097 
2098   config.ordered = true;
2099   config.maxRetransmits = 0;
2100   channel = pc_->CreateDataChannel("3", &config);
2101   EXPECT_TRUE(channel != NULL);
2102   EXPECT_FALSE(channel->reliable());
2103   EXPECT_FALSE(observer_.renegotiation_needed_);
2104 
2105   config.maxRetransmits = absl::nullopt;
2106   config.maxRetransmitTime = 0;
2107   channel = pc_->CreateDataChannel("4", &config);
2108   EXPECT_TRUE(channel != NULL);
2109   EXPECT_FALSE(channel->reliable());
2110   EXPECT_FALSE(observer_.renegotiation_needed_);
2111 }
2112 
2113 // For backwards compatibility, we want people who "unset" maxRetransmits
2114 // and maxRetransmitTime by setting them to -1 to get what they want.
TEST_P(PeerConnectionInterfaceTest,CreateSctpDataChannelWithMinusOne)2115 TEST_P(PeerConnectionInterfaceTest, CreateSctpDataChannelWithMinusOne) {
2116   RTCConfiguration rtc_config;
2117   rtc_config.enable_dtls_srtp = true;
2118   CreatePeerConnection(rtc_config);
2119 
2120   webrtc::DataChannelInit config;
2121   config.maxRetransmitTime = -1;
2122   config.maxRetransmits = -1;
2123   rtc::scoped_refptr<DataChannelInterface> channel =
2124       pc_->CreateDataChannel("1", &config);
2125   EXPECT_TRUE(channel != NULL);
2126 }
2127 
2128 // This tests that no data channel is returned if both maxRetransmits and
2129 // maxRetransmitTime are set for SCTP data channels.
TEST_P(PeerConnectionInterfaceTest,CreateSctpDataChannelShouldFailForInvalidConfig)2130 TEST_P(PeerConnectionInterfaceTest,
2131        CreateSctpDataChannelShouldFailForInvalidConfig) {
2132   RTCConfiguration rtc_config;
2133   rtc_config.enable_dtls_srtp = true;
2134   CreatePeerConnection(rtc_config);
2135 
2136   std::string label = "test";
2137   webrtc::DataChannelInit config;
2138   config.maxRetransmits = 0;
2139   config.maxRetransmitTime = 0;
2140 
2141   rtc::scoped_refptr<DataChannelInterface> channel =
2142       pc_->CreateDataChannel(label, &config);
2143   EXPECT_TRUE(channel == NULL);
2144 }
2145 
2146 // The test verifies that creating a SCTP data channel with an id already in use
2147 // or out of range should fail.
TEST_P(PeerConnectionInterfaceTest,CreateSctpDataChannelWithInvalidIdShouldFail)2148 TEST_P(PeerConnectionInterfaceTest,
2149        CreateSctpDataChannelWithInvalidIdShouldFail) {
2150   RTCConfiguration rtc_config;
2151   rtc_config.enable_dtls_srtp = true;
2152   CreatePeerConnection(rtc_config);
2153 
2154   webrtc::DataChannelInit config;
2155   rtc::scoped_refptr<DataChannelInterface> channel;
2156 
2157   config.id = 1;
2158   config.negotiated = true;
2159   channel = pc_->CreateDataChannel("1", &config);
2160   EXPECT_TRUE(channel != NULL);
2161   EXPECT_EQ(1, channel->id());
2162 
2163   channel = pc_->CreateDataChannel("x", &config);
2164   EXPECT_TRUE(channel == NULL);
2165 
2166   config.id = cricket::kMaxSctpSid;
2167   config.negotiated = true;
2168   channel = pc_->CreateDataChannel("max", &config);
2169   EXPECT_TRUE(channel != NULL);
2170   EXPECT_EQ(config.id, channel->id());
2171 
2172   config.id = cricket::kMaxSctpSid + 1;
2173   config.negotiated = true;
2174   channel = pc_->CreateDataChannel("x", &config);
2175   EXPECT_TRUE(channel == NULL);
2176 }
2177 
2178 // Verifies that duplicated label is allowed for SCTP data channel.
TEST_P(PeerConnectionInterfaceTest,SctpDuplicatedLabelAllowed)2179 TEST_P(PeerConnectionInterfaceTest, SctpDuplicatedLabelAllowed) {
2180   RTCConfiguration rtc_config;
2181   rtc_config.enable_dtls_srtp = true;
2182   CreatePeerConnection(rtc_config);
2183 
2184   std::string label = "test";
2185   rtc::scoped_refptr<DataChannelInterface> channel =
2186       pc_->CreateDataChannel(label, nullptr);
2187   EXPECT_NE(channel, nullptr);
2188 
2189   rtc::scoped_refptr<DataChannelInterface> dup_channel =
2190       pc_->CreateDataChannel(label, nullptr);
2191   EXPECT_NE(dup_channel, nullptr);
2192 }
2193 
2194 // This test verifies that OnRenegotiationNeeded is fired for every new RTP
2195 // DataChannel.
TEST_P(PeerConnectionInterfaceTest,RenegotiationNeededForNewRtpDataChannel)2196 TEST_P(PeerConnectionInterfaceTest, RenegotiationNeededForNewRtpDataChannel) {
2197   RTCConfiguration rtc_config;
2198   rtc_config.enable_rtp_data_channel = true;
2199   rtc_config.enable_dtls_srtp = false;
2200   CreatePeerConnection(rtc_config);
2201 
2202   rtc::scoped_refptr<DataChannelInterface> dc1 =
2203       pc_->CreateDataChannel("test1", NULL);
2204   EXPECT_TRUE(observer_.renegotiation_needed_);
2205   observer_.renegotiation_needed_ = false;
2206 
2207   CreateOfferReceiveAnswer();
2208 
2209   rtc::scoped_refptr<DataChannelInterface> dc2 =
2210       pc_->CreateDataChannel("test2", NULL);
2211   EXPECT_EQ(observer_.renegotiation_needed_,
2212             GetParam() == SdpSemantics::kPlanB);
2213 }
2214 
2215 // This test that a data channel closes when a PeerConnection is deleted/closed.
TEST_P(PeerConnectionInterfaceTest,DataChannelCloseWhenPeerConnectionClose)2216 TEST_P(PeerConnectionInterfaceTest, DataChannelCloseWhenPeerConnectionClose) {
2217   RTCConfiguration rtc_config;
2218   rtc_config.enable_rtp_data_channel = true;
2219   rtc_config.enable_dtls_srtp = false;
2220   CreatePeerConnection(rtc_config);
2221 
2222   rtc::scoped_refptr<DataChannelInterface> data1 =
2223       pc_->CreateDataChannel("test1", NULL);
2224   rtc::scoped_refptr<DataChannelInterface> data2 =
2225       pc_->CreateDataChannel("test2", NULL);
2226   ASSERT_TRUE(data1 != NULL);
2227   std::unique_ptr<MockDataChannelObserver> observer1(
2228       new MockDataChannelObserver(data1));
2229   std::unique_ptr<MockDataChannelObserver> observer2(
2230       new MockDataChannelObserver(data2));
2231 
2232   CreateOfferReceiveAnswer();
2233   EXPECT_TRUE_WAIT(observer1->IsOpen(), kTimeout);
2234   EXPECT_TRUE_WAIT(observer2->IsOpen(), kTimeout);
2235 
2236   ReleasePeerConnection();
2237   EXPECT_EQ(DataChannelInterface::kClosed, data1->state());
2238   EXPECT_EQ(DataChannelInterface::kClosed, data2->state());
2239 }
2240 
2241 // This tests that RTP data channels can be rejected in an answer.
TEST_P(PeerConnectionInterfaceTest,TestRejectRtpDataChannelInAnswer)2242 TEST_P(PeerConnectionInterfaceTest, TestRejectRtpDataChannelInAnswer) {
2243   RTCConfiguration rtc_config;
2244   rtc_config.enable_rtp_data_channel = true;
2245   rtc_config.enable_dtls_srtp = false;
2246   CreatePeerConnection(rtc_config);
2247 
2248   rtc::scoped_refptr<DataChannelInterface> offer_channel(
2249       pc_->CreateDataChannel("offer_channel", NULL));
2250 
2251   CreateOfferAsLocalDescription();
2252 
2253   // Create an answer where the m-line for data channels are rejected.
2254   std::string sdp;
2255   EXPECT_TRUE(pc_->local_description()->ToString(&sdp));
2256   std::unique_ptr<SessionDescriptionInterface> answer(
2257       webrtc::CreateSessionDescription(SdpType::kAnswer, sdp));
2258   ASSERT_TRUE(answer);
2259   cricket::ContentInfo* data_info =
2260       cricket::GetFirstDataContent(answer->description());
2261   data_info->rejected = true;
2262 
2263   DoSetRemoteDescription(std::move(answer));
2264   EXPECT_EQ(DataChannelInterface::kClosed, offer_channel->state());
2265 }
2266 
2267 #ifdef WEBRTC_HAVE_SCTP
2268 // This tests that SCTP data channels can be rejected in an answer.
TEST_P(PeerConnectionInterfaceTest,TestRejectSctpDataChannelInAnswer)2269 TEST_P(PeerConnectionInterfaceTest, TestRejectSctpDataChannelInAnswer)
2270 #else
2271 TEST_P(PeerConnectionInterfaceTest, DISABLED_TestRejectSctpDataChannelInAnswer)
2272 #endif
2273 {
2274   RTCConfiguration rtc_config;
2275   CreatePeerConnection(rtc_config);
2276 
2277   rtc::scoped_refptr<DataChannelInterface> offer_channel(
2278       pc_->CreateDataChannel("offer_channel", NULL));
2279 
2280   CreateOfferAsLocalDescription();
2281 
2282   // Create an answer where the m-line for data channels are rejected.
2283   std::string sdp;
2284   EXPECT_TRUE(pc_->local_description()->ToString(&sdp));
2285   std::unique_ptr<SessionDescriptionInterface> answer(
2286       webrtc::CreateSessionDescription(SdpType::kAnswer, sdp));
2287   ASSERT_TRUE(answer);
2288   cricket::ContentInfo* data_info =
2289       cricket::GetFirstDataContent(answer->description());
2290   data_info->rejected = true;
2291 
2292   DoSetRemoteDescription(std::move(answer));
2293   EXPECT_EQ(DataChannelInterface::kClosed, offer_channel->state());
2294 }
2295 
2296 // Test that we can create a session description from an SDP string from
2297 // FireFox, use it as a remote session description, generate an answer and use
2298 // the answer as a local description.
TEST_P(PeerConnectionInterfaceTest,ReceiveFireFoxOffer)2299 TEST_P(PeerConnectionInterfaceTest, ReceiveFireFoxOffer) {
2300   RTCConfiguration rtc_config;
2301   rtc_config.enable_dtls_srtp = true;
2302   CreatePeerConnection(rtc_config);
2303   AddAudioTrack("audio_label");
2304   AddVideoTrack("video_label");
2305   std::unique_ptr<SessionDescriptionInterface> desc(
2306       webrtc::CreateSessionDescription(SdpType::kOffer,
2307                                        webrtc::kFireFoxSdpOffer, nullptr));
2308   EXPECT_TRUE(DoSetSessionDescription(std::move(desc), false));
2309   CreateAnswerAsLocalDescription();
2310   ASSERT_TRUE(pc_->local_description() != NULL);
2311   ASSERT_TRUE(pc_->remote_description() != NULL);
2312 
2313   const cricket::ContentInfo* content =
2314       cricket::GetFirstAudioContent(pc_->local_description()->description());
2315   ASSERT_TRUE(content != NULL);
2316   EXPECT_FALSE(content->rejected);
2317 
2318   content =
2319       cricket::GetFirstVideoContent(pc_->local_description()->description());
2320   ASSERT_TRUE(content != NULL);
2321   EXPECT_FALSE(content->rejected);
2322 #ifdef WEBRTC_HAVE_SCTP
2323   content =
2324       cricket::GetFirstDataContent(pc_->local_description()->description());
2325   ASSERT_TRUE(content != NULL);
2326   EXPECT_FALSE(content->rejected);
2327 #endif
2328 }
2329 
2330 // Test that fallback from DTLS to SDES is not supported.
2331 // The fallback was previously supported but was removed to simplify the code
2332 // and because it's non-standard.
TEST_P(PeerConnectionInterfaceTest,DtlsSdesFallbackNotSupported)2333 TEST_P(PeerConnectionInterfaceTest, DtlsSdesFallbackNotSupported) {
2334   RTCConfiguration rtc_config;
2335   rtc_config.enable_dtls_srtp = true;
2336   CreatePeerConnection(rtc_config);
2337   // Wait for fake certificate to be generated. Previously, this is what caused
2338   // the "a=crypto" lines to be rejected.
2339   AddAudioTrack("audio_label");
2340   AddVideoTrack("video_label");
2341   ASSERT_NE(nullptr, fake_certificate_generator_);
2342   EXPECT_EQ_WAIT(1, fake_certificate_generator_->generated_certificates(),
2343                  kTimeout);
2344   std::unique_ptr<SessionDescriptionInterface> desc(
2345       webrtc::CreateSessionDescription(SdpType::kOffer, kDtlsSdesFallbackSdp,
2346                                        nullptr));
2347   EXPECT_FALSE(DoSetSessionDescription(std::move(desc), /*local=*/false));
2348 }
2349 
2350 // Test that we can create an audio only offer and receive an answer with a
2351 // limited set of audio codecs and receive an updated offer with more audio
2352 // codecs, where the added codecs are not supported.
TEST_P(PeerConnectionInterfaceTest,ReceiveUpdatedAudioOfferWithBadCodecs)2353 TEST_P(PeerConnectionInterfaceTest, ReceiveUpdatedAudioOfferWithBadCodecs) {
2354   CreatePeerConnectionWithoutDtls();
2355   AddAudioTrack("audio_label");
2356   CreateOfferAsLocalDescription();
2357 
2358   const char* answer_sdp =
2359       (sdp_semantics_ == SdpSemantics::kPlanB ? webrtc::kAudioSdpPlanB
2360                                               : webrtc::kAudioSdpUnifiedPlan);
2361   std::unique_ptr<SessionDescriptionInterface> answer(
2362       webrtc::CreateSessionDescription(SdpType::kAnswer, answer_sdp, nullptr));
2363   EXPECT_TRUE(DoSetSessionDescription(std::move(answer), false));
2364 
2365   const char* reoffer_sdp =
2366       (sdp_semantics_ == SdpSemantics::kPlanB
2367            ? webrtc::kAudioSdpWithUnsupportedCodecsPlanB
2368            : webrtc::kAudioSdpWithUnsupportedCodecsUnifiedPlan);
2369   std::unique_ptr<SessionDescriptionInterface> updated_offer(
2370       webrtc::CreateSessionDescription(SdpType::kOffer, reoffer_sdp, nullptr));
2371   EXPECT_TRUE(DoSetSessionDescription(std::move(updated_offer), false));
2372   CreateAnswerAsLocalDescription();
2373 }
2374 
2375 // Test that if we're receiving (but not sending) a track, subsequent offers
2376 // will have m-lines with a=recvonly.
TEST_P(PeerConnectionInterfaceTest,CreateSubsequentRecvOnlyOffer)2377 TEST_P(PeerConnectionInterfaceTest, CreateSubsequentRecvOnlyOffer) {
2378   RTCConfiguration rtc_config;
2379   rtc_config.enable_dtls_srtp = true;
2380   CreatePeerConnection(rtc_config);
2381   CreateAndSetRemoteOffer(GetSdpStringWithStream1());
2382   CreateAnswerAsLocalDescription();
2383 
2384   // At this point we should be receiving stream 1, but not sending anything.
2385   // A new offer should be recvonly.
2386   std::unique_ptr<SessionDescriptionInterface> offer;
2387   DoCreateOffer(&offer, nullptr);
2388 
2389   const cricket::ContentInfo* video_content =
2390       cricket::GetFirstVideoContent(offer->description());
2391   ASSERT_EQ(RtpTransceiverDirection::kRecvOnly,
2392             video_content->media_description()->direction());
2393 
2394   const cricket::ContentInfo* audio_content =
2395       cricket::GetFirstAudioContent(offer->description());
2396   ASSERT_EQ(RtpTransceiverDirection::kRecvOnly,
2397             audio_content->media_description()->direction());
2398 }
2399 
2400 // Test that if we're receiving (but not sending) a track, and the
2401 // offerToReceiveVideo/offerToReceiveAudio constraints are explicitly set to
2402 // false, the generated m-lines will be a=inactive.
TEST_P(PeerConnectionInterfaceTest,CreateSubsequentInactiveOffer)2403 TEST_P(PeerConnectionInterfaceTest, CreateSubsequentInactiveOffer) {
2404   RTCConfiguration rtc_config;
2405   rtc_config.enable_dtls_srtp = true;
2406   CreatePeerConnection(rtc_config);
2407   CreateAndSetRemoteOffer(GetSdpStringWithStream1());
2408   CreateAnswerAsLocalDescription();
2409 
2410   // At this point we should be receiving stream 1, but not sending anything.
2411   // A new offer would be recvonly, but we'll set the "no receive" constraints
2412   // to make it inactive.
2413   std::unique_ptr<SessionDescriptionInterface> offer;
2414   RTCOfferAnswerOptions options;
2415   options.offer_to_receive_audio = 0;
2416   options.offer_to_receive_video = 0;
2417   DoCreateOffer(&offer, &options);
2418 
2419   const cricket::ContentInfo* video_content =
2420       cricket::GetFirstVideoContent(offer->description());
2421   ASSERT_EQ(RtpTransceiverDirection::kInactive,
2422             video_content->media_description()->direction());
2423 
2424   const cricket::ContentInfo* audio_content =
2425       cricket::GetFirstAudioContent(offer->description());
2426   ASSERT_EQ(RtpTransceiverDirection::kInactive,
2427             audio_content->media_description()->direction());
2428 }
2429 
2430 // Test that we can use SetConfiguration to change the ICE servers of the
2431 // PortAllocator.
TEST_P(PeerConnectionInterfaceTest,SetConfigurationChangesIceServers)2432 TEST_P(PeerConnectionInterfaceTest, SetConfigurationChangesIceServers) {
2433   CreatePeerConnection();
2434 
2435   PeerConnectionInterface::RTCConfiguration config = pc_->GetConfiguration();
2436   PeerConnectionInterface::IceServer server;
2437   server.uri = "stun:test_hostname";
2438   config.servers.push_back(server);
2439   EXPECT_TRUE(pc_->SetConfiguration(config).ok());
2440 
2441   EXPECT_EQ(1u, port_allocator_->stun_servers().size());
2442   EXPECT_EQ("test_hostname",
2443             port_allocator_->stun_servers().begin()->hostname());
2444 }
2445 
TEST_P(PeerConnectionInterfaceTest,SetConfigurationChangesCandidateFilter)2446 TEST_P(PeerConnectionInterfaceTest, SetConfigurationChangesCandidateFilter) {
2447   CreatePeerConnection();
2448   PeerConnectionInterface::RTCConfiguration config = pc_->GetConfiguration();
2449   config.type = PeerConnectionInterface::kRelay;
2450   EXPECT_TRUE(pc_->SetConfiguration(config).ok());
2451   EXPECT_EQ(cricket::CF_RELAY, port_allocator_->candidate_filter());
2452 }
2453 
TEST_P(PeerConnectionInterfaceTest,SetConfigurationChangesPruneTurnPortsFlag)2454 TEST_P(PeerConnectionInterfaceTest, SetConfigurationChangesPruneTurnPortsFlag) {
2455   PeerConnectionInterface::RTCConfiguration config;
2456   config.prune_turn_ports = false;
2457   CreatePeerConnection(config);
2458   config = pc_->GetConfiguration();
2459   EXPECT_EQ(webrtc::NO_PRUNE, port_allocator_->turn_port_prune_policy());
2460 
2461   config.prune_turn_ports = true;
2462   EXPECT_TRUE(pc_->SetConfiguration(config).ok());
2463   EXPECT_EQ(webrtc::PRUNE_BASED_ON_PRIORITY,
2464             port_allocator_->turn_port_prune_policy());
2465 }
2466 
2467 // Test that the ice check interval can be changed. This does not verify that
2468 // the setting makes it all the way to P2PTransportChannel, as that would
2469 // require a very complex set of mocks.
TEST_P(PeerConnectionInterfaceTest,SetConfigurationChangesIceCheckInterval)2470 TEST_P(PeerConnectionInterfaceTest, SetConfigurationChangesIceCheckInterval) {
2471   PeerConnectionInterface::RTCConfiguration config;
2472   config.ice_check_min_interval = absl::nullopt;
2473   CreatePeerConnection(config);
2474   config = pc_->GetConfiguration();
2475   config.ice_check_min_interval = 100;
2476   EXPECT_TRUE(pc_->SetConfiguration(config).ok());
2477   config = pc_->GetConfiguration();
2478   EXPECT_EQ(config.ice_check_min_interval, 100);
2479 }
2480 
TEST_P(PeerConnectionInterfaceTest,SetConfigurationChangesSurfaceIceCandidatesOnIceTransportTypeChanged)2481 TEST_P(PeerConnectionInterfaceTest,
2482        SetConfigurationChangesSurfaceIceCandidatesOnIceTransportTypeChanged) {
2483   PeerConnectionInterface::RTCConfiguration config;
2484   config.surface_ice_candidates_on_ice_transport_type_changed = false;
2485   CreatePeerConnection(config);
2486   config = pc_->GetConfiguration();
2487   EXPECT_FALSE(config.surface_ice_candidates_on_ice_transport_type_changed);
2488 
2489   config.surface_ice_candidates_on_ice_transport_type_changed = true;
2490   EXPECT_TRUE(pc_->SetConfiguration(config).ok());
2491   config = pc_->GetConfiguration();
2492   EXPECT_TRUE(config.surface_ice_candidates_on_ice_transport_type_changed);
2493 }
2494 
2495 // Test that when SetConfiguration changes both the pool size and other
2496 // attributes, the pooled session is created with the updated attributes.
TEST_P(PeerConnectionInterfaceTest,SetConfigurationCreatesPooledSessionCorrectly)2497 TEST_P(PeerConnectionInterfaceTest,
2498        SetConfigurationCreatesPooledSessionCorrectly) {
2499   CreatePeerConnection();
2500   PeerConnectionInterface::RTCConfiguration config = pc_->GetConfiguration();
2501   config.ice_candidate_pool_size = 1;
2502   PeerConnectionInterface::IceServer server;
2503   server.uri = kStunAddressOnly;
2504   config.servers.push_back(server);
2505   config.type = PeerConnectionInterface::kRelay;
2506   EXPECT_TRUE(pc_->SetConfiguration(config).ok());
2507 
2508   const cricket::FakePortAllocatorSession* session =
2509       static_cast<const cricket::FakePortAllocatorSession*>(
2510           port_allocator_->GetPooledSession());
2511   ASSERT_NE(nullptr, session);
2512   EXPECT_EQ(1UL, session->stun_servers().size());
2513 }
2514 
2515 // Test that after SetLocalDescription, changing the pool size is not allowed,
2516 // and an invalid modification error is returned.
TEST_P(PeerConnectionInterfaceTest,CantChangePoolSizeAfterSetLocalDescription)2517 TEST_P(PeerConnectionInterfaceTest,
2518        CantChangePoolSizeAfterSetLocalDescription) {
2519   CreatePeerConnection();
2520   // Start by setting a size of 1.
2521   PeerConnectionInterface::RTCConfiguration config = pc_->GetConfiguration();
2522   config.ice_candidate_pool_size = 1;
2523   EXPECT_TRUE(pc_->SetConfiguration(config).ok());
2524 
2525   // Set remote offer; can still change pool size at this point.
2526   CreateOfferAsRemoteDescription();
2527   config.ice_candidate_pool_size = 2;
2528   EXPECT_TRUE(pc_->SetConfiguration(config).ok());
2529 
2530   // Set local answer; now it's too late.
2531   CreateAnswerAsLocalDescription();
2532   config.ice_candidate_pool_size = 3;
2533   RTCError error = pc_->SetConfiguration(config);
2534   EXPECT_EQ(RTCErrorType::INVALID_MODIFICATION, error.type());
2535 }
2536 
2537 // Test that after setting an answer, extra pooled sessions are discarded. The
2538 // ICE candidate pool is only intended to be used for the first offer/answer.
TEST_P(PeerConnectionInterfaceTest,ExtraPooledSessionsDiscardedAfterApplyingAnswer)2539 TEST_P(PeerConnectionInterfaceTest,
2540        ExtraPooledSessionsDiscardedAfterApplyingAnswer) {
2541   CreatePeerConnection();
2542 
2543   // Set a larger-than-necessary size.
2544   PeerConnectionInterface::RTCConfiguration config = pc_->GetConfiguration();
2545   config.ice_candidate_pool_size = 4;
2546   EXPECT_TRUE(pc_->SetConfiguration(config).ok());
2547 
2548   // Do offer/answer.
2549   CreateOfferAsRemoteDescription();
2550   CreateAnswerAsLocalDescription();
2551 
2552   // Expect no pooled sessions to be left.
2553   const cricket::PortAllocatorSession* session =
2554       port_allocator_->GetPooledSession();
2555   EXPECT_EQ(nullptr, session);
2556 }
2557 
2558 // After Close is called, pooled candidates should be discarded so as to not
2559 // waste network resources.
TEST_P(PeerConnectionInterfaceTest,PooledSessionsDiscardedAfterClose)2560 TEST_P(PeerConnectionInterfaceTest, PooledSessionsDiscardedAfterClose) {
2561   CreatePeerConnection();
2562 
2563   PeerConnectionInterface::RTCConfiguration config = pc_->GetConfiguration();
2564   config.ice_candidate_pool_size = 3;
2565   EXPECT_TRUE(pc_->SetConfiguration(config).ok());
2566   pc_->Close();
2567 
2568   // Expect no pooled sessions to be left.
2569   const cricket::PortAllocatorSession* session =
2570       port_allocator_->GetPooledSession();
2571   EXPECT_EQ(nullptr, session);
2572 }
2573 
2574 // Test that SetConfiguration returns an invalid modification error if
2575 // modifying a field in the configuration that isn't allowed to be modified.
TEST_P(PeerConnectionInterfaceTest,SetConfigurationReturnsInvalidModificationError)2576 TEST_P(PeerConnectionInterfaceTest,
2577        SetConfigurationReturnsInvalidModificationError) {
2578   PeerConnectionInterface::RTCConfiguration config;
2579   config.bundle_policy = PeerConnectionInterface::kBundlePolicyBalanced;
2580   config.rtcp_mux_policy = PeerConnectionInterface::kRtcpMuxPolicyNegotiate;
2581   config.continual_gathering_policy = PeerConnectionInterface::GATHER_ONCE;
2582   CreatePeerConnection(config);
2583 
2584   PeerConnectionInterface::RTCConfiguration modified_config =
2585       pc_->GetConfiguration();
2586   modified_config.bundle_policy =
2587       PeerConnectionInterface::kBundlePolicyMaxBundle;
2588   RTCError error = pc_->SetConfiguration(modified_config);
2589   EXPECT_EQ(RTCErrorType::INVALID_MODIFICATION, error.type());
2590 
2591   modified_config = pc_->GetConfiguration();
2592   modified_config.rtcp_mux_policy =
2593       PeerConnectionInterface::kRtcpMuxPolicyRequire;
2594   error = pc_->SetConfiguration(modified_config);
2595   EXPECT_EQ(RTCErrorType::INVALID_MODIFICATION, error.type());
2596 
2597   modified_config = pc_->GetConfiguration();
2598   modified_config.continual_gathering_policy =
2599       PeerConnectionInterface::GATHER_CONTINUALLY;
2600   error = pc_->SetConfiguration(modified_config);
2601   EXPECT_EQ(RTCErrorType::INVALID_MODIFICATION, error.type());
2602 }
2603 
2604 // Test that SetConfiguration returns a range error if the candidate pool size
2605 // is negative or larger than allowed by the spec.
TEST_P(PeerConnectionInterfaceTest,SetConfigurationReturnsRangeErrorForBadCandidatePoolSize)2606 TEST_P(PeerConnectionInterfaceTest,
2607        SetConfigurationReturnsRangeErrorForBadCandidatePoolSize) {
2608   PeerConnectionInterface::RTCConfiguration config;
2609   CreatePeerConnection(config);
2610   config = pc_->GetConfiguration();
2611 
2612   config.ice_candidate_pool_size = -1;
2613   RTCError error = pc_->SetConfiguration(config);
2614   EXPECT_EQ(RTCErrorType::INVALID_RANGE, error.type());
2615 
2616   config.ice_candidate_pool_size = INT_MAX;
2617   error = pc_->SetConfiguration(config);
2618   EXPECT_EQ(RTCErrorType::INVALID_RANGE, error.type());
2619 }
2620 
2621 // Test that SetConfiguration returns a syntax error if parsing an ICE server
2622 // URL failed.
TEST_P(PeerConnectionInterfaceTest,SetConfigurationReturnsSyntaxErrorFromBadIceUrls)2623 TEST_P(PeerConnectionInterfaceTest,
2624        SetConfigurationReturnsSyntaxErrorFromBadIceUrls) {
2625   PeerConnectionInterface::RTCConfiguration config;
2626   CreatePeerConnection(config);
2627   config = pc_->GetConfiguration();
2628 
2629   PeerConnectionInterface::IceServer bad_server;
2630   bad_server.uri = "stunn:www.example.com";
2631   config.servers.push_back(bad_server);
2632   RTCError error = pc_->SetConfiguration(config);
2633   EXPECT_EQ(RTCErrorType::SYNTAX_ERROR, error.type());
2634 }
2635 
2636 // Test that SetConfiguration returns an invalid parameter error if a TURN
2637 // IceServer is missing a username or password.
TEST_P(PeerConnectionInterfaceTest,SetConfigurationReturnsInvalidParameterIfCredentialsMissing)2638 TEST_P(PeerConnectionInterfaceTest,
2639        SetConfigurationReturnsInvalidParameterIfCredentialsMissing) {
2640   PeerConnectionInterface::RTCConfiguration config;
2641   CreatePeerConnection(config);
2642   config = pc_->GetConfiguration();
2643 
2644   PeerConnectionInterface::IceServer bad_server;
2645   bad_server.uri = "turn:www.example.com";
2646   // Missing password.
2647   bad_server.username = "foo";
2648   config.servers.push_back(bad_server);
2649   RTCError error;
2650   EXPECT_EQ(pc_->SetConfiguration(config).type(),
2651             RTCErrorType::INVALID_PARAMETER);
2652 }
2653 
2654 // Test that PeerConnection::Close changes the states to closed and all remote
2655 // tracks change state to ended.
TEST_P(PeerConnectionInterfaceTest,CloseAndTestStreamsAndStates)2656 TEST_P(PeerConnectionInterfaceTest, CloseAndTestStreamsAndStates) {
2657   // Initialize a PeerConnection and negotiate local and remote session
2658   // description.
2659   InitiateCall();
2660 
2661   // With Plan B, verify the stream count. The analog with Unified Plan is the
2662   // RtpTransceiver count.
2663   if (sdp_semantics_ == SdpSemantics::kPlanB) {
2664     ASSERT_EQ(1u, pc_->local_streams()->count());
2665     ASSERT_EQ(1u, pc_->remote_streams()->count());
2666   } else {
2667     ASSERT_EQ(2u, pc_->GetTransceivers().size());
2668   }
2669 
2670   pc_->Close();
2671 
2672   EXPECT_EQ(PeerConnectionInterface::kClosed, pc_->signaling_state());
2673   EXPECT_EQ(PeerConnectionInterface::kIceConnectionClosed,
2674             pc_->ice_connection_state());
2675   EXPECT_EQ(PeerConnectionInterface::kIceGatheringComplete,
2676             pc_->ice_gathering_state());
2677 
2678   if (sdp_semantics_ == SdpSemantics::kPlanB) {
2679     EXPECT_EQ(1u, pc_->local_streams()->count());
2680     EXPECT_EQ(1u, pc_->remote_streams()->count());
2681   } else {
2682     // Verify that the RtpTransceivers are still returned.
2683     EXPECT_EQ(2u, pc_->GetTransceivers().size());
2684   }
2685 
2686   auto audio_receiver = GetFirstReceiverOfType(cricket::MEDIA_TYPE_AUDIO);
2687   auto video_receiver = GetFirstReceiverOfType(cricket::MEDIA_TYPE_VIDEO);
2688   if (sdp_semantics_ == SdpSemantics::kPlanB) {
2689     ASSERT_TRUE(audio_receiver);
2690     ASSERT_TRUE(video_receiver);
2691     // Track state may be updated asynchronously.
2692     EXPECT_EQ_WAIT(MediaStreamTrackInterface::kEnded,
2693                    audio_receiver->track()->state(), kTimeout);
2694     EXPECT_EQ_WAIT(MediaStreamTrackInterface::kEnded,
2695                    video_receiver->track()->state(), kTimeout);
2696   } else {
2697     ASSERT_FALSE(audio_receiver);
2698     ASSERT_FALSE(video_receiver);
2699   }
2700 }
2701 
2702 // Test that PeerConnection methods fails gracefully after
2703 // PeerConnection::Close has been called.
2704 // Don't run under Unified Plan since the stream API is not available.
TEST_F(PeerConnectionInterfaceTestPlanB,CloseAndTestMethods)2705 TEST_F(PeerConnectionInterfaceTestPlanB, CloseAndTestMethods) {
2706   CreatePeerConnectionWithoutDtls();
2707   AddAudioVideoStream(kStreamId1, "audio_label", "video_label");
2708   CreateOfferAsRemoteDescription();
2709   CreateAnswerAsLocalDescription();
2710 
2711   ASSERT_EQ(1u, pc_->local_streams()->count());
2712   rtc::scoped_refptr<MediaStreamInterface> local_stream =
2713       pc_->local_streams()->at(0);
2714 
2715   pc_->Close();
2716 
2717   pc_->RemoveStream(local_stream);
2718   EXPECT_FALSE(pc_->AddStream(local_stream));
2719 
2720   EXPECT_TRUE(pc_->CreateDataChannel("test", NULL) == NULL);
2721 
2722   EXPECT_TRUE(pc_->local_description() != NULL);
2723   EXPECT_TRUE(pc_->remote_description() != NULL);
2724 
2725   std::unique_ptr<SessionDescriptionInterface> offer;
2726   EXPECT_FALSE(DoCreateOffer(&offer, nullptr));
2727   std::unique_ptr<SessionDescriptionInterface> answer;
2728   EXPECT_FALSE(DoCreateAnswer(&answer, nullptr));
2729 
2730   std::string sdp;
2731   ASSERT_TRUE(pc_->remote_description()->ToString(&sdp));
2732   std::unique_ptr<SessionDescriptionInterface> remote_offer(
2733       webrtc::CreateSessionDescription(SdpType::kOffer, sdp));
2734   EXPECT_FALSE(DoSetRemoteDescription(std::move(remote_offer)));
2735 
2736   ASSERT_TRUE(pc_->local_description()->ToString(&sdp));
2737   std::unique_ptr<SessionDescriptionInterface> local_offer(
2738       webrtc::CreateSessionDescription(SdpType::kOffer, sdp));
2739   EXPECT_FALSE(DoSetLocalDescription(std::move(local_offer)));
2740 }
2741 
2742 // Test that GetStats can still be called after PeerConnection::Close.
TEST_P(PeerConnectionInterfaceTest,CloseAndGetStats)2743 TEST_P(PeerConnectionInterfaceTest, CloseAndGetStats) {
2744   InitiateCall();
2745   pc_->Close();
2746   DoGetStats(NULL);
2747 }
2748 
2749 // NOTE: The series of tests below come from what used to be
2750 // mediastreamsignaling_unittest.cc, and are mostly aimed at testing that
2751 // setting a remote or local description has the expected effects.
2752 
2753 // This test verifies that the remote MediaStreams corresponding to a received
2754 // SDP string is created. In this test the two separate MediaStreams are
2755 // signaled.
TEST_P(PeerConnectionInterfaceTest,UpdateRemoteStreams)2756 TEST_P(PeerConnectionInterfaceTest, UpdateRemoteStreams) {
2757   RTCConfiguration config;
2758   config.enable_dtls_srtp = true;
2759   CreatePeerConnection(config);
2760   CreateAndSetRemoteOffer(GetSdpStringWithStream1());
2761 
2762   rtc::scoped_refptr<StreamCollection> reference(CreateStreamCollection(1, 1));
2763   EXPECT_TRUE(
2764       CompareStreamCollections(observer_.remote_streams(), reference.get()));
2765   MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2766   EXPECT_TRUE(remote_stream->GetVideoTracks()[0]->GetSource() != nullptr);
2767 
2768   // Create a session description based on another SDP with another
2769   // MediaStream.
2770   CreateAndSetRemoteOffer(GetSdpStringWithStream1And2());
2771 
2772   rtc::scoped_refptr<StreamCollection> reference2(CreateStreamCollection(2, 1));
2773   EXPECT_TRUE(
2774       CompareStreamCollections(observer_.remote_streams(), reference2.get()));
2775 }
2776 
2777 // This test verifies that when remote tracks are added/removed from SDP, the
2778 // created remote streams are updated appropriately.
2779 // Don't run under Unified Plan since this test uses Plan B SDP to test Plan B
2780 // specific behavior.
TEST_F(PeerConnectionInterfaceTestPlanB,AddRemoveTrackFromExistingRemoteMediaStream)2781 TEST_F(PeerConnectionInterfaceTestPlanB,
2782        AddRemoveTrackFromExistingRemoteMediaStream) {
2783   RTCConfiguration config;
2784   config.enable_dtls_srtp = true;
2785   CreatePeerConnection(config);
2786   std::unique_ptr<SessionDescriptionInterface> desc_ms1 =
2787       CreateSessionDescriptionAndReference(1, 1);
2788   EXPECT_TRUE(DoSetRemoteDescription(std::move(desc_ms1)));
2789   EXPECT_TRUE(CompareStreamCollections(observer_.remote_streams(),
2790                                        reference_collection_));
2791 
2792   // Add extra audio and video tracks to the same MediaStream.
2793   std::unique_ptr<SessionDescriptionInterface> desc_ms1_two_tracks =
2794       CreateSessionDescriptionAndReference(2, 2);
2795   EXPECT_TRUE(DoSetRemoteDescription(std::move(desc_ms1_two_tracks)));
2796   EXPECT_TRUE(CompareStreamCollections(observer_.remote_streams(),
2797                                        reference_collection_));
2798   rtc::scoped_refptr<AudioTrackInterface> audio_track2 =
2799       observer_.remote_streams()->at(0)->GetAudioTracks()[1];
2800   EXPECT_EQ(webrtc::MediaStreamTrackInterface::kLive, audio_track2->state());
2801   rtc::scoped_refptr<VideoTrackInterface> video_track2 =
2802       observer_.remote_streams()->at(0)->GetVideoTracks()[1];
2803   EXPECT_EQ(webrtc::MediaStreamTrackInterface::kLive, video_track2->state());
2804 
2805   // Remove the extra audio and video tracks.
2806   std::unique_ptr<SessionDescriptionInterface> desc_ms2 =
2807       CreateSessionDescriptionAndReference(1, 1);
2808   MockTrackObserver audio_track_observer(audio_track2);
2809   MockTrackObserver video_track_observer(video_track2);
2810 
2811   EXPECT_CALL(audio_track_observer, OnChanged()).Times(Exactly(1));
2812   EXPECT_CALL(video_track_observer, OnChanged()).Times(Exactly(1));
2813   EXPECT_TRUE(DoSetRemoteDescription(std::move(desc_ms2)));
2814   EXPECT_TRUE(CompareStreamCollections(observer_.remote_streams(),
2815                                        reference_collection_));
2816   // Track state may be updated asynchronously.
2817   EXPECT_EQ_WAIT(webrtc::MediaStreamTrackInterface::kEnded,
2818                  audio_track2->state(), kTimeout);
2819   EXPECT_EQ_WAIT(webrtc::MediaStreamTrackInterface::kEnded,
2820                  video_track2->state(), kTimeout);
2821 }
2822 
2823 // This tests that remote tracks are ended if a local session description is set
2824 // that rejects the media content type.
TEST_P(PeerConnectionInterfaceTest,RejectMediaContent)2825 TEST_P(PeerConnectionInterfaceTest, RejectMediaContent) {
2826   RTCConfiguration config;
2827   config.enable_dtls_srtp = true;
2828   CreatePeerConnection(config);
2829   // First create and set a remote offer, then reject its video content in our
2830   // answer.
2831   CreateAndSetRemoteOffer(kSdpStringWithStream1PlanB);
2832   auto audio_receiver = GetFirstReceiverOfType(cricket::MEDIA_TYPE_AUDIO);
2833   ASSERT_TRUE(audio_receiver);
2834   auto video_receiver = GetFirstReceiverOfType(cricket::MEDIA_TYPE_VIDEO);
2835   ASSERT_TRUE(video_receiver);
2836 
2837   rtc::scoped_refptr<MediaStreamTrackInterface> remote_audio =
2838       audio_receiver->track();
2839   EXPECT_EQ(webrtc::MediaStreamTrackInterface::kLive, remote_audio->state());
2840   rtc::scoped_refptr<MediaStreamTrackInterface> remote_video =
2841       video_receiver->track();
2842   EXPECT_EQ(MediaStreamTrackInterface::kLive, remote_video->state());
2843 
2844   std::unique_ptr<SessionDescriptionInterface> local_answer;
2845   EXPECT_TRUE(DoCreateAnswer(&local_answer, nullptr));
2846   cricket::ContentInfo* video_info =
2847       local_answer->description()->GetContentByName("video");
2848   video_info->rejected = true;
2849   EXPECT_TRUE(DoSetLocalDescription(std::move(local_answer)));
2850   EXPECT_EQ(MediaStreamTrackInterface::kEnded, remote_video->state());
2851   EXPECT_EQ(MediaStreamTrackInterface::kLive, remote_audio->state());
2852 
2853   // Now create an offer where we reject both video and audio.
2854   std::unique_ptr<SessionDescriptionInterface> local_offer;
2855   EXPECT_TRUE(DoCreateOffer(&local_offer, nullptr));
2856   video_info = local_offer->description()->GetContentByName("video");
2857   ASSERT_TRUE(video_info != nullptr);
2858   video_info->rejected = true;
2859   cricket::ContentInfo* audio_info =
2860       local_offer->description()->GetContentByName("audio");
2861   ASSERT_TRUE(audio_info != nullptr);
2862   audio_info->rejected = true;
2863   EXPECT_TRUE(DoSetLocalDescription(std::move(local_offer)));
2864   // Track state may be updated asynchronously.
2865   EXPECT_EQ_WAIT(MediaStreamTrackInterface::kEnded, remote_audio->state(),
2866                  kTimeout);
2867   EXPECT_EQ_WAIT(MediaStreamTrackInterface::kEnded, remote_video->state(),
2868                  kTimeout);
2869 }
2870 
2871 // This tests that we won't crash if the remote track has been removed outside
2872 // of PeerConnection and then PeerConnection tries to reject the track.
2873 // Don't run under Unified Plan since the stream API is not available.
TEST_F(PeerConnectionInterfaceTestPlanB,RemoveTrackThenRejectMediaContent)2874 TEST_F(PeerConnectionInterfaceTestPlanB, RemoveTrackThenRejectMediaContent) {
2875   RTCConfiguration config;
2876   config.enable_dtls_srtp = true;
2877   CreatePeerConnection(config);
2878   CreateAndSetRemoteOffer(GetSdpStringWithStream1());
2879   MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2880   remote_stream->RemoveTrack(remote_stream->GetVideoTracks()[0]);
2881   remote_stream->RemoveTrack(remote_stream->GetAudioTracks()[0]);
2882 
2883   std::unique_ptr<SessionDescriptionInterface> local_answer(
2884       webrtc::CreateSessionDescription(SdpType::kAnswer,
2885                                        GetSdpStringWithStream1(), nullptr));
2886   cricket::ContentInfo* video_info =
2887       local_answer->description()->GetContentByName("video");
2888   video_info->rejected = true;
2889   cricket::ContentInfo* audio_info =
2890       local_answer->description()->GetContentByName("audio");
2891   audio_info->rejected = true;
2892   EXPECT_TRUE(DoSetLocalDescription(std::move(local_answer)));
2893 
2894   // No crash is a pass.
2895 }
2896 
2897 // This tests that if a recvonly remote description is set, no remote streams
2898 // will be created, even if the description contains SSRCs/MSIDs.
2899 // See: https://code.google.com/p/webrtc/issues/detail?id=5054
TEST_P(PeerConnectionInterfaceTest,RecvonlyDescriptionDoesntCreateStream)2900 TEST_P(PeerConnectionInterfaceTest, RecvonlyDescriptionDoesntCreateStream) {
2901   RTCConfiguration config;
2902   config.enable_dtls_srtp = true;
2903   CreatePeerConnection(config);
2904 
2905   std::string recvonly_offer = GetSdpStringWithStream1();
2906   absl::StrReplaceAll({{kSendrecv, kRecvonly}}, &recvonly_offer);
2907   CreateAndSetRemoteOffer(recvonly_offer);
2908 
2909   EXPECT_EQ(0u, observer_.remote_streams()->count());
2910 }
2911 
2912 // This tests that a default MediaStream is created if a remote session
2913 // description doesn't contain any streams and no MSID support.
2914 // It also tests that the default stream is updated if a video m-line is added
2915 // in a subsequent session description.
2916 // Don't run under Unified Plan since this behavior is Plan B specific.
TEST_F(PeerConnectionInterfaceTestPlanB,SdpWithoutMsidCreatesDefaultStream)2917 TEST_F(PeerConnectionInterfaceTestPlanB, SdpWithoutMsidCreatesDefaultStream) {
2918   RTCConfiguration config;
2919   config.enable_dtls_srtp = true;
2920   CreatePeerConnection(config);
2921   CreateAndSetRemoteOffer(kSdpStringWithoutStreamsAudioOnly);
2922 
2923   ASSERT_EQ(1u, observer_.remote_streams()->count());
2924   MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2925 
2926   EXPECT_EQ(1u, remote_stream->GetAudioTracks().size());
2927   EXPECT_EQ(0u, remote_stream->GetVideoTracks().size());
2928   EXPECT_EQ("default", remote_stream->id());
2929 
2930   CreateAndSetRemoteOffer(kSdpStringWithoutStreams);
2931   ASSERT_EQ(1u, observer_.remote_streams()->count());
2932   ASSERT_EQ(1u, remote_stream->GetAudioTracks().size());
2933   EXPECT_EQ("defaulta0", remote_stream->GetAudioTracks()[0]->id());
2934   EXPECT_EQ(MediaStreamTrackInterface::kLive,
2935             remote_stream->GetAudioTracks()[0]->state());
2936   ASSERT_EQ(1u, remote_stream->GetVideoTracks().size());
2937   EXPECT_EQ("defaultv0", remote_stream->GetVideoTracks()[0]->id());
2938   EXPECT_EQ(MediaStreamTrackInterface::kLive,
2939             remote_stream->GetVideoTracks()[0]->state());
2940 }
2941 
2942 // This tests that a default MediaStream is created if a remote session
2943 // description doesn't contain any streams and media direction is send only.
2944 // Don't run under Unified Plan since this behavior is Plan B specific.
TEST_F(PeerConnectionInterfaceTestPlanB,SendOnlySdpWithoutMsidCreatesDefaultStream)2945 TEST_F(PeerConnectionInterfaceTestPlanB,
2946        SendOnlySdpWithoutMsidCreatesDefaultStream) {
2947   RTCConfiguration config;
2948   config.enable_dtls_srtp = true;
2949   CreatePeerConnection(config);
2950   CreateAndSetRemoteOffer(kSdpStringSendOnlyWithoutStreams);
2951 
2952   ASSERT_EQ(1u, observer_.remote_streams()->count());
2953   MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2954 
2955   EXPECT_EQ(1u, remote_stream->GetAudioTracks().size());
2956   EXPECT_EQ(1u, remote_stream->GetVideoTracks().size());
2957   EXPECT_EQ("default", remote_stream->id());
2958 }
2959 
2960 // This tests that it won't crash when PeerConnection tries to remove
2961 // a remote track that as already been removed from the MediaStream.
2962 // Don't run under Unified Plan since this behavior is Plan B specific.
TEST_F(PeerConnectionInterfaceTestPlanB,RemoveAlreadyGoneRemoteStream)2963 TEST_F(PeerConnectionInterfaceTestPlanB, RemoveAlreadyGoneRemoteStream) {
2964   RTCConfiguration config;
2965   config.enable_dtls_srtp = true;
2966   CreatePeerConnection(config);
2967   CreateAndSetRemoteOffer(GetSdpStringWithStream1());
2968   MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2969   remote_stream->RemoveTrack(remote_stream->GetAudioTracks()[0]);
2970   remote_stream->RemoveTrack(remote_stream->GetVideoTracks()[0]);
2971 
2972   CreateAndSetRemoteOffer(kSdpStringWithoutStreams);
2973 
2974   // No crash is a pass.
2975 }
2976 
2977 // This tests that a default MediaStream is created if the remote session
2978 // description doesn't contain any streams and don't contain an indication if
2979 // MSID is supported.
2980 // Don't run under Unified Plan since this behavior is Plan B specific.
TEST_F(PeerConnectionInterfaceTestPlanB,SdpWithoutMsidAndStreamsCreatesDefaultStream)2981 TEST_F(PeerConnectionInterfaceTestPlanB,
2982        SdpWithoutMsidAndStreamsCreatesDefaultStream) {
2983   RTCConfiguration config;
2984   config.enable_dtls_srtp = true;
2985   CreatePeerConnection(config);
2986   CreateAndSetRemoteOffer(kSdpStringWithoutStreams);
2987 
2988   ASSERT_EQ(1u, observer_.remote_streams()->count());
2989   MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2990   EXPECT_EQ(1u, remote_stream->GetAudioTracks().size());
2991   EXPECT_EQ(1u, remote_stream->GetVideoTracks().size());
2992 }
2993 
2994 // This tests that a default MediaStream is not created if the remote session
2995 // description doesn't contain any streams but does support MSID.
2996 // Don't run under Unified Plan since this behavior is Plan B specific.
TEST_F(PeerConnectionInterfaceTestPlanB,SdpWithMsidDontCreatesDefaultStream)2997 TEST_F(PeerConnectionInterfaceTestPlanB, SdpWithMsidDontCreatesDefaultStream) {
2998   RTCConfiguration config;
2999   config.enable_dtls_srtp = true;
3000   CreatePeerConnection(config);
3001   CreateAndSetRemoteOffer(kSdpStringWithMsidWithoutStreams);
3002   EXPECT_EQ(0u, observer_.remote_streams()->count());
3003 }
3004 
3005 // This tests that when setting a new description, the old default tracks are
3006 // not destroyed and recreated.
3007 // See: https://bugs.chromium.org/p/webrtc/issues/detail?id=5250
3008 // Don't run under Unified Plan since this behavior is Plan B specific.
TEST_F(PeerConnectionInterfaceTestPlanB,DefaultTracksNotDestroyedAndRecreated)3009 TEST_F(PeerConnectionInterfaceTestPlanB,
3010        DefaultTracksNotDestroyedAndRecreated) {
3011   RTCConfiguration config;
3012   config.enable_dtls_srtp = true;
3013   CreatePeerConnection(config);
3014   CreateAndSetRemoteOffer(kSdpStringWithoutStreamsAudioOnly);
3015 
3016   ASSERT_EQ(1u, observer_.remote_streams()->count());
3017   MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
3018   ASSERT_EQ(1u, remote_stream->GetAudioTracks().size());
3019 
3020   // Set the track to "disabled", then set a new description and ensure the
3021   // track is still disabled, which ensures it hasn't been recreated.
3022   remote_stream->GetAudioTracks()[0]->set_enabled(false);
3023   CreateAndSetRemoteOffer(kSdpStringWithoutStreamsAudioOnly);
3024   ASSERT_EQ(1u, remote_stream->GetAudioTracks().size());
3025   EXPECT_FALSE(remote_stream->GetAudioTracks()[0]->enabled());
3026 }
3027 
3028 // This tests that a default MediaStream is not created if a remote session
3029 // description is updated to not have any MediaStreams.
3030 // Don't run under Unified Plan since this behavior is Plan B specific.
TEST_F(PeerConnectionInterfaceTestPlanB,VerifyDefaultStreamIsNotCreated)3031 TEST_F(PeerConnectionInterfaceTestPlanB, VerifyDefaultStreamIsNotCreated) {
3032   RTCConfiguration config;
3033   config.enable_dtls_srtp = true;
3034   CreatePeerConnection(config);
3035   CreateAndSetRemoteOffer(GetSdpStringWithStream1());
3036   rtc::scoped_refptr<StreamCollection> reference(CreateStreamCollection(1, 1));
3037   EXPECT_TRUE(
3038       CompareStreamCollections(observer_.remote_streams(), reference.get()));
3039 
3040   CreateAndSetRemoteOffer(kSdpStringWithoutStreams);
3041   EXPECT_EQ(0u, observer_.remote_streams()->count());
3042 }
3043 
3044 // This tests that a default MediaStream is created if a remote SDP comes from
3045 // an endpoint that doesn't signal SSRCs, but signals media stream IDs.
TEST_F(PeerConnectionInterfaceTestPlanB,SdpWithMsidWithoutSsrcCreatesDefaultStream)3046 TEST_F(PeerConnectionInterfaceTestPlanB,
3047        SdpWithMsidWithoutSsrcCreatesDefaultStream) {
3048   RTCConfiguration config;
3049   config.enable_dtls_srtp = true;
3050   CreatePeerConnection(config);
3051   std::string sdp_string = kSdpStringWithoutStreamsAudioOnly;
3052   // Add a=msid lines to simulate a Unified Plan endpoint that only
3053   // signals stream IDs with a=msid lines.
3054   sdp_string.append("a=msid:audio_stream_id audio_track_id\n");
3055 
3056   CreateAndSetRemoteOffer(sdp_string);
3057 
3058   ASSERT_EQ(1u, observer_.remote_streams()->count());
3059   MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
3060   EXPECT_EQ("default", remote_stream->id());
3061   ASSERT_EQ(1u, remote_stream->GetAudioTracks().size());
3062 }
3063 
3064 // This tests that when a Plan B endpoint receives an SDP that signals no media
3065 // stream IDs indicated by the special character "-" in the a=msid line, that
3066 // a default stream ID will be used for the MediaStream ID. This can occur
3067 // when a Unified Plan endpoint signals no media stream IDs, but signals both
3068 // a=ssrc msid and a=msid lines for interop signaling with Plan B.
TEST_F(PeerConnectionInterfaceTestPlanB,SdpWithEmptyMsidAndSsrcCreatesDefaultStreamId)3069 TEST_F(PeerConnectionInterfaceTestPlanB,
3070        SdpWithEmptyMsidAndSsrcCreatesDefaultStreamId) {
3071   RTCConfiguration config;
3072   config.enable_dtls_srtp = true;
3073   CreatePeerConnection(config);
3074   // Add a a=msid line to the SDP. This is prioritized when parsing the SDP, so
3075   // the sender's stream ID will be interpreted as no stream IDs.
3076   std::string sdp_string = kSdpStringWithStream1AudioTrackOnly;
3077   sdp_string.append("a=msid:- audiotrack0\n");
3078 
3079   CreateAndSetRemoteOffer(sdp_string);
3080 
3081   ASSERT_EQ(1u, observer_.remote_streams()->count());
3082   // Because SSRCs are signaled the track ID will be what was signaled in the
3083   // a=msid line.
3084   EXPECT_EQ("audiotrack0", observer_.last_added_track_label_);
3085   MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
3086   EXPECT_EQ("default", remote_stream->id());
3087   ASSERT_EQ(1u, remote_stream->GetAudioTracks().size());
3088 
3089   // Previously a bug ocurred when setting the remote description a second time.
3090   // This is because we checked equality of the remote StreamParams stream ID
3091   // (empty), and the previously set stream ID for the remote sender
3092   // ("default"). This cause a track to be removed, then added, when really
3093   // nothing should occur because it is the same track.
3094   CreateAndSetRemoteOffer(sdp_string);
3095   EXPECT_EQ(0u, observer_.remove_track_events_.size());
3096   EXPECT_EQ(1u, observer_.add_track_events_.size());
3097   EXPECT_EQ("audiotrack0", observer_.last_added_track_label_);
3098   remote_stream = observer_.remote_streams()->at(0);
3099   EXPECT_EQ("default", remote_stream->id());
3100   ASSERT_EQ(1u, remote_stream->GetAudioTracks().size());
3101 }
3102 
3103 // This tests that an RtpSender is created when the local description is set
3104 // after adding a local stream.
3105 // TODO(deadbeef): This test and the one below it need to be updated when
3106 // an RtpSender's lifetime isn't determined by when a local description is set.
3107 // Don't run under Unified Plan since this behavior is Plan B specific.
TEST_F(PeerConnectionInterfaceTestPlanB,LocalDescriptionChanged)3108 TEST_F(PeerConnectionInterfaceTestPlanB, LocalDescriptionChanged) {
3109   RTCConfiguration config;
3110   config.enable_dtls_srtp = true;
3111   CreatePeerConnection(config);
3112 
3113   // Create an offer with 1 stream with 2 tracks of each type.
3114   rtc::scoped_refptr<StreamCollection> stream_collection =
3115       CreateStreamCollection(1, 2);
3116   pc_->AddStream(stream_collection->at(0));
3117   std::unique_ptr<SessionDescriptionInterface> offer;
3118   ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
3119   EXPECT_TRUE(DoSetLocalDescription(std::move(offer)));
3120 
3121   auto senders = pc_->GetSenders();
3122   EXPECT_EQ(4u, senders.size());
3123   EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
3124   EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
3125   EXPECT_TRUE(ContainsSender(senders, kAudioTracks[1]));
3126   EXPECT_TRUE(ContainsSender(senders, kVideoTracks[1]));
3127 
3128   // Remove an audio and video track.
3129   pc_->RemoveStream(stream_collection->at(0));
3130   stream_collection = CreateStreamCollection(1, 1);
3131   pc_->AddStream(stream_collection->at(0));
3132   ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
3133   EXPECT_TRUE(DoSetLocalDescription(std::move(offer)));
3134 
3135   senders = pc_->GetSenders();
3136   EXPECT_EQ(2u, senders.size());
3137   EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
3138   EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
3139   EXPECT_FALSE(ContainsSender(senders, kAudioTracks[1]));
3140   EXPECT_FALSE(ContainsSender(senders, kVideoTracks[1]));
3141 }
3142 
3143 // This tests that an RtpSender is created when the local description is set
3144 // before adding a local stream.
3145 // Don't run under Unified Plan since this behavior is Plan B specific.
TEST_F(PeerConnectionInterfaceTestPlanB,AddLocalStreamAfterLocalDescriptionChanged)3146 TEST_F(PeerConnectionInterfaceTestPlanB,
3147        AddLocalStreamAfterLocalDescriptionChanged) {
3148   RTCConfiguration config;
3149   config.enable_dtls_srtp = true;
3150   CreatePeerConnection(config);
3151 
3152   rtc::scoped_refptr<StreamCollection> stream_collection =
3153       CreateStreamCollection(1, 2);
3154   // Add a stream to create the offer, but remove it afterwards.
3155   pc_->AddStream(stream_collection->at(0));
3156   std::unique_ptr<SessionDescriptionInterface> offer;
3157   ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
3158   pc_->RemoveStream(stream_collection->at(0));
3159 
3160   EXPECT_TRUE(DoSetLocalDescription(std::move(offer)));
3161   auto senders = pc_->GetSenders();
3162   EXPECT_EQ(0u, senders.size());
3163 
3164   pc_->AddStream(stream_collection->at(0));
3165   senders = pc_->GetSenders();
3166   EXPECT_EQ(4u, senders.size());
3167   EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
3168   EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
3169   EXPECT_TRUE(ContainsSender(senders, kAudioTracks[1]));
3170   EXPECT_TRUE(ContainsSender(senders, kVideoTracks[1]));
3171 }
3172 
3173 // This tests that the expected behavior occurs if the SSRC on a local track is
3174 // changed when SetLocalDescription is called.
TEST_P(PeerConnectionInterfaceTest,ChangeSsrcOnTrackInLocalSessionDescription)3175 TEST_P(PeerConnectionInterfaceTest,
3176        ChangeSsrcOnTrackInLocalSessionDescription) {
3177   RTCConfiguration config;
3178   config.enable_dtls_srtp = true;
3179   CreatePeerConnection(config);
3180 
3181   AddAudioTrack(kAudioTracks[0]);
3182   AddVideoTrack(kVideoTracks[0]);
3183   std::unique_ptr<SessionDescriptionInterface> offer;
3184   ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
3185   // Grab a copy of the offer before it gets passed into the PC.
3186   std::unique_ptr<SessionDescriptionInterface> modified_offer =
3187       webrtc::CreateSessionDescription(
3188           webrtc::SdpType::kOffer, offer->session_id(),
3189           offer->session_version(), offer->description()->Clone());
3190   EXPECT_TRUE(DoSetLocalDescription(std::move(offer)));
3191 
3192   auto senders = pc_->GetSenders();
3193   EXPECT_EQ(2u, senders.size());
3194   EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
3195   EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
3196 
3197   // Change the ssrc of the audio and video track.
3198   cricket::MediaContentDescription* desc =
3199       cricket::GetFirstAudioContentDescription(modified_offer->description());
3200   ASSERT_TRUE(desc != NULL);
3201   for (StreamParams& stream : desc->mutable_streams()) {
3202     for (unsigned int& ssrc : stream.ssrcs) {
3203       ++ssrc;
3204     }
3205   }
3206 
3207   desc =
3208       cricket::GetFirstVideoContentDescription(modified_offer->description());
3209   ASSERT_TRUE(desc != NULL);
3210   for (StreamParams& stream : desc->mutable_streams()) {
3211     for (unsigned int& ssrc : stream.ssrcs) {
3212       ++ssrc;
3213     }
3214   }
3215 
3216   EXPECT_TRUE(DoSetLocalDescription(std::move(modified_offer)));
3217   senders = pc_->GetSenders();
3218   EXPECT_EQ(2u, senders.size());
3219   EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
3220   EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
3221   // TODO(deadbeef): Once RtpSenders expose parameters, check that the SSRC
3222   // changed.
3223 }
3224 
3225 // This tests that the expected behavior occurs if a new session description is
3226 // set with the same tracks, but on a different MediaStream.
3227 // Don't run under Unified Plan since the stream API is not available.
TEST_F(PeerConnectionInterfaceTestPlanB,SignalSameTracksInSeparateMediaStream)3228 TEST_F(PeerConnectionInterfaceTestPlanB,
3229        SignalSameTracksInSeparateMediaStream) {
3230   RTCConfiguration config;
3231   config.enable_dtls_srtp = true;
3232   CreatePeerConnection(config);
3233 
3234   rtc::scoped_refptr<StreamCollection> stream_collection =
3235       CreateStreamCollection(2, 1);
3236   pc_->AddStream(stream_collection->at(0));
3237   std::unique_ptr<SessionDescriptionInterface> offer;
3238   ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
3239   EXPECT_TRUE(DoSetLocalDescription(std::move(offer)));
3240 
3241   auto senders = pc_->GetSenders();
3242   EXPECT_EQ(2u, senders.size());
3243   EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0], kStreams[0]));
3244   EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0], kStreams[0]));
3245 
3246   // Add a new MediaStream but with the same tracks as in the first stream.
3247   rtc::scoped_refptr<webrtc::MediaStreamInterface> stream_1(
3248       webrtc::MediaStream::Create(kStreams[1]));
3249   stream_1->AddTrack(stream_collection->at(0)->GetVideoTracks()[0]);
3250   stream_1->AddTrack(stream_collection->at(0)->GetAudioTracks()[0]);
3251   pc_->AddStream(stream_1);
3252 
3253   ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
3254   EXPECT_TRUE(DoSetLocalDescription(std::move(offer)));
3255 
3256   auto new_senders = pc_->GetSenders();
3257   // Should be the same senders as before, but with updated stream id.
3258   // Note that this behavior is subject to change in the future.
3259   // We may decide the PC should ignore existing tracks in AddStream.
3260   EXPECT_EQ(senders, new_senders);
3261   EXPECT_TRUE(ContainsSender(new_senders, kAudioTracks[0], kStreams[1]));
3262   EXPECT_TRUE(ContainsSender(new_senders, kVideoTracks[0], kStreams[1]));
3263 }
3264 
3265 // This tests that PeerConnectionObserver::OnAddTrack is correctly called.
TEST_P(PeerConnectionInterfaceTest,OnAddTrackCallback)3266 TEST_P(PeerConnectionInterfaceTest, OnAddTrackCallback) {
3267   RTCConfiguration config;
3268   config.enable_dtls_srtp = true;
3269   CreatePeerConnection(config);
3270   CreateAndSetRemoteOffer(kSdpStringWithStream1AudioTrackOnly);
3271   EXPECT_EQ(observer_.num_added_tracks_, 1);
3272   EXPECT_EQ(observer_.last_added_track_label_, kAudioTracks[0]);
3273 
3274   // Create and set the updated remote SDP.
3275   CreateAndSetRemoteOffer(kSdpStringWithStream1PlanB);
3276   EXPECT_EQ(observer_.num_added_tracks_, 2);
3277   EXPECT_EQ(observer_.last_added_track_label_, kVideoTracks[0]);
3278 }
3279 
3280 // Test that when SetConfiguration is called and the configuration is
3281 // changing, the next offer causes an ICE restart.
TEST_P(PeerConnectionInterfaceTest,SetConfigurationCausingIceRestart)3282 TEST_P(PeerConnectionInterfaceTest, SetConfigurationCausingIceRestart) {
3283   PeerConnectionInterface::RTCConfiguration config;
3284   config.type = PeerConnectionInterface::kRelay;
3285   CreatePeerConnection(config);
3286   config = pc_->GetConfiguration();
3287   AddAudioTrack(kAudioTracks[0], {kStreamId1});
3288   AddVideoTrack(kVideoTracks[0], {kStreamId1});
3289 
3290   // Do initial offer/answer so there's something to restart.
3291   CreateOfferAsLocalDescription();
3292   CreateAnswerAsRemoteDescription(GetSdpStringWithStream1());
3293 
3294   // Grab the ufrags.
3295   std::vector<std::string> initial_ufrags = GetUfrags(pc_->local_description());
3296 
3297   // Change ICE policy, which should trigger an ICE restart on the next offer.
3298   config.type = PeerConnectionInterface::kAll;
3299   EXPECT_TRUE(pc_->SetConfiguration(config).ok());
3300   CreateOfferAsLocalDescription();
3301 
3302   // Grab the new ufrags.
3303   std::vector<std::string> subsequent_ufrags =
3304       GetUfrags(pc_->local_description());
3305 
3306   // Sanity check.
3307   EXPECT_EQ(initial_ufrags.size(), subsequent_ufrags.size());
3308   // Check that each ufrag is different.
3309   for (int i = 0; i < static_cast<int>(initial_ufrags.size()); ++i) {
3310     EXPECT_NE(initial_ufrags[i], subsequent_ufrags[i]);
3311   }
3312 }
3313 
3314 // Test that when SetConfiguration is called and the configuration *isn't*
3315 // changing, the next offer does *not* cause an ICE restart.
TEST_P(PeerConnectionInterfaceTest,SetConfigurationNotCausingIceRestart)3316 TEST_P(PeerConnectionInterfaceTest, SetConfigurationNotCausingIceRestart) {
3317   PeerConnectionInterface::RTCConfiguration config;
3318   config.type = PeerConnectionInterface::kRelay;
3319   CreatePeerConnection(config);
3320   config = pc_->GetConfiguration();
3321   AddAudioTrack(kAudioTracks[0]);
3322   AddVideoTrack(kVideoTracks[0]);
3323 
3324   // Do initial offer/answer so there's something to restart.
3325   CreateOfferAsLocalDescription();
3326   CreateAnswerAsRemoteDescription(GetSdpStringWithStream1());
3327 
3328   // Grab the ufrags.
3329   std::vector<std::string> initial_ufrags = GetUfrags(pc_->local_description());
3330 
3331   // Call SetConfiguration with a config identical to what the PC was
3332   // constructed with.
3333   EXPECT_TRUE(pc_->SetConfiguration(config).ok());
3334   CreateOfferAsLocalDescription();
3335 
3336   // Grab the new ufrags.
3337   std::vector<std::string> subsequent_ufrags =
3338       GetUfrags(pc_->local_description());
3339 
3340   EXPECT_EQ(initial_ufrags, subsequent_ufrags);
3341 }
3342 
3343 // Test for a weird corner case scenario:
3344 // 1. Audio/video session established.
3345 // 2. SetConfiguration changes ICE config; ICE restart needed.
3346 // 3. ICE restart initiated by remote peer, but only for one m= section.
3347 // 4. Next createOffer should initiate an ICE restart, but only for the other
3348 //    m= section; it would be pointless to do an ICE restart for the m= section
3349 //    that was already restarted.
TEST_P(PeerConnectionInterfaceTest,SetConfigurationCausingPartialIceRestart)3350 TEST_P(PeerConnectionInterfaceTest, SetConfigurationCausingPartialIceRestart) {
3351   PeerConnectionInterface::RTCConfiguration config;
3352   config.type = PeerConnectionInterface::kRelay;
3353   CreatePeerConnection(config);
3354   config = pc_->GetConfiguration();
3355   AddAudioTrack(kAudioTracks[0], {kStreamId1});
3356   AddVideoTrack(kVideoTracks[0], {kStreamId1});
3357 
3358   // Do initial offer/answer so there's something to restart.
3359   CreateOfferAsLocalDescription();
3360   CreateAnswerAsRemoteDescription(GetSdpStringWithStream1());
3361 
3362   // Change ICE policy, which should set the "needs-ice-restart" flag.
3363   config.type = PeerConnectionInterface::kAll;
3364   EXPECT_TRUE(pc_->SetConfiguration(config).ok());
3365 
3366   // Do ICE restart for the first m= section, initiated by remote peer.
3367   std::unique_ptr<webrtc::SessionDescriptionInterface> remote_offer(
3368       webrtc::CreateSessionDescription(SdpType::kOffer,
3369                                        GetSdpStringWithStream1(), nullptr));
3370   ASSERT_TRUE(remote_offer);
3371   remote_offer->description()->transport_infos()[0].description.ice_ufrag =
3372       "modified";
3373   EXPECT_TRUE(DoSetRemoteDescription(std::move(remote_offer)));
3374   CreateAnswerAsLocalDescription();
3375 
3376   // Grab the ufrags.
3377   std::vector<std::string> initial_ufrags = GetUfrags(pc_->local_description());
3378   ASSERT_EQ(2U, initial_ufrags.size());
3379 
3380   // Create offer and grab the new ufrags.
3381   CreateOfferAsLocalDescription();
3382   std::vector<std::string> subsequent_ufrags =
3383       GetUfrags(pc_->local_description());
3384   ASSERT_EQ(2U, subsequent_ufrags.size());
3385 
3386   // Ensure that only the ufrag for the second m= section changed.
3387   EXPECT_EQ(initial_ufrags[0], subsequent_ufrags[0]);
3388   EXPECT_NE(initial_ufrags[1], subsequent_ufrags[1]);
3389 }
3390 
3391 // Tests that the methods to return current/pending descriptions work as
3392 // expected at different points in the offer/answer exchange. This test does
3393 // one offer/answer exchange as the offerer, then another as the answerer.
TEST_P(PeerConnectionInterfaceTest,CurrentAndPendingDescriptions)3394 TEST_P(PeerConnectionInterfaceTest, CurrentAndPendingDescriptions) {
3395   // This disables DTLS so we can apply an answer to ourselves.
3396   CreatePeerConnection();
3397 
3398   // Create initial local offer and get SDP (which will also be used as
3399   // answer/pranswer);
3400   std::unique_ptr<SessionDescriptionInterface> local_offer;
3401   ASSERT_TRUE(DoCreateOffer(&local_offer, nullptr));
3402   std::string sdp;
3403   EXPECT_TRUE(local_offer->ToString(&sdp));
3404 
3405   // Set local offer.
3406   SessionDescriptionInterface* local_offer_ptr = local_offer.get();
3407   EXPECT_TRUE(DoSetLocalDescription(std::move(local_offer)));
3408   EXPECT_EQ(local_offer_ptr, pc_->pending_local_description());
3409   EXPECT_EQ(nullptr, pc_->pending_remote_description());
3410   EXPECT_EQ(nullptr, pc_->current_local_description());
3411   EXPECT_EQ(nullptr, pc_->current_remote_description());
3412 
3413   // Set remote pranswer.
3414   std::unique_ptr<SessionDescriptionInterface> remote_pranswer(
3415       webrtc::CreateSessionDescription(SdpType::kPrAnswer, sdp));
3416   SessionDescriptionInterface* remote_pranswer_ptr = remote_pranswer.get();
3417   EXPECT_TRUE(DoSetRemoteDescription(std::move(remote_pranswer)));
3418   EXPECT_EQ(local_offer_ptr, pc_->pending_local_description());
3419   EXPECT_EQ(remote_pranswer_ptr, pc_->pending_remote_description());
3420   EXPECT_EQ(nullptr, pc_->current_local_description());
3421   EXPECT_EQ(nullptr, pc_->current_remote_description());
3422 
3423   // Set remote answer.
3424   std::unique_ptr<SessionDescriptionInterface> remote_answer(
3425       webrtc::CreateSessionDescription(SdpType::kAnswer, sdp));
3426   SessionDescriptionInterface* remote_answer_ptr = remote_answer.get();
3427   EXPECT_TRUE(DoSetRemoteDescription(std::move(remote_answer)));
3428   EXPECT_EQ(nullptr, pc_->pending_local_description());
3429   EXPECT_EQ(nullptr, pc_->pending_remote_description());
3430   EXPECT_EQ(local_offer_ptr, pc_->current_local_description());
3431   EXPECT_EQ(remote_answer_ptr, pc_->current_remote_description());
3432 
3433   // Set remote offer.
3434   std::unique_ptr<SessionDescriptionInterface> remote_offer(
3435       webrtc::CreateSessionDescription(SdpType::kOffer, sdp));
3436   SessionDescriptionInterface* remote_offer_ptr = remote_offer.get();
3437   EXPECT_TRUE(DoSetRemoteDescription(std::move(remote_offer)));
3438   EXPECT_EQ(remote_offer_ptr, pc_->pending_remote_description());
3439   EXPECT_EQ(nullptr, pc_->pending_local_description());
3440   EXPECT_EQ(local_offer_ptr, pc_->current_local_description());
3441   EXPECT_EQ(remote_answer_ptr, pc_->current_remote_description());
3442 
3443   // Set local pranswer.
3444   std::unique_ptr<SessionDescriptionInterface> local_pranswer(
3445       webrtc::CreateSessionDescription(SdpType::kPrAnswer, sdp));
3446   SessionDescriptionInterface* local_pranswer_ptr = local_pranswer.get();
3447   EXPECT_TRUE(DoSetLocalDescription(std::move(local_pranswer)));
3448   EXPECT_EQ(remote_offer_ptr, pc_->pending_remote_description());
3449   EXPECT_EQ(local_pranswer_ptr, pc_->pending_local_description());
3450   EXPECT_EQ(local_offer_ptr, pc_->current_local_description());
3451   EXPECT_EQ(remote_answer_ptr, pc_->current_remote_description());
3452 
3453   // Set local answer.
3454   std::unique_ptr<SessionDescriptionInterface> local_answer(
3455       webrtc::CreateSessionDescription(SdpType::kAnswer, sdp));
3456   SessionDescriptionInterface* local_answer_ptr = local_answer.get();
3457   EXPECT_TRUE(DoSetLocalDescription(std::move(local_answer)));
3458   EXPECT_EQ(nullptr, pc_->pending_remote_description());
3459   EXPECT_EQ(nullptr, pc_->pending_local_description());
3460   EXPECT_EQ(remote_offer_ptr, pc_->current_remote_description());
3461   EXPECT_EQ(local_answer_ptr, pc_->current_local_description());
3462 }
3463 
3464 // Tests that it won't crash when calling StartRtcEventLog or StopRtcEventLog
3465 // after the PeerConnection is closed.
3466 // This version tests the StartRtcEventLog version that receives an object
3467 // of type |RtcEventLogOutput|.
TEST_P(PeerConnectionInterfaceTest,StartAndStopLoggingToOutputAfterPeerConnectionClosed)3468 TEST_P(PeerConnectionInterfaceTest,
3469        StartAndStopLoggingToOutputAfterPeerConnectionClosed) {
3470   CreatePeerConnection();
3471   // The RtcEventLog will be reset when the PeerConnection is closed.
3472   pc_->Close();
3473 
3474   EXPECT_FALSE(
3475       pc_->StartRtcEventLog(std::make_unique<webrtc::RtcEventLogOutputNull>(),
3476                             webrtc::RtcEventLog::kImmediateOutput));
3477   pc_->StopRtcEventLog();
3478 }
3479 
3480 // Test that generated offers/answers include "ice-option:trickle".
TEST_P(PeerConnectionInterfaceTest,OffersAndAnswersHaveTrickleIceOption)3481 TEST_P(PeerConnectionInterfaceTest, OffersAndAnswersHaveTrickleIceOption) {
3482   CreatePeerConnection();
3483 
3484   // First, create an offer with audio/video.
3485   RTCOfferAnswerOptions options;
3486   options.offer_to_receive_audio = 1;
3487   options.offer_to_receive_video = 1;
3488   std::unique_ptr<SessionDescriptionInterface> offer;
3489   ASSERT_TRUE(DoCreateOffer(&offer, &options));
3490   cricket::SessionDescription* desc = offer->description();
3491   ASSERT_EQ(2u, desc->transport_infos().size());
3492   EXPECT_TRUE(desc->transport_infos()[0].description.HasOption("trickle"));
3493   EXPECT_TRUE(desc->transport_infos()[1].description.HasOption("trickle"));
3494 
3495   // Apply the offer as a remote description, then create an answer.
3496   EXPECT_FALSE(pc_->can_trickle_ice_candidates());
3497   EXPECT_TRUE(DoSetRemoteDescription(std::move(offer)));
3498   ASSERT_TRUE(pc_->can_trickle_ice_candidates());
3499   EXPECT_TRUE(*(pc_->can_trickle_ice_candidates()));
3500   std::unique_ptr<SessionDescriptionInterface> answer;
3501   ASSERT_TRUE(DoCreateAnswer(&answer, &options));
3502   desc = answer->description();
3503   ASSERT_EQ(2u, desc->transport_infos().size());
3504   EXPECT_TRUE(desc->transport_infos()[0].description.HasOption("trickle"));
3505   EXPECT_TRUE(desc->transport_infos()[1].description.HasOption("trickle"));
3506 }
3507 
3508 // Test that ICE renomination isn't offered if it's not enabled in the PC's
3509 // RTCConfiguration.
TEST_P(PeerConnectionInterfaceTest,IceRenominationNotOffered)3510 TEST_P(PeerConnectionInterfaceTest, IceRenominationNotOffered) {
3511   PeerConnectionInterface::RTCConfiguration config;
3512   config.enable_ice_renomination = false;
3513   CreatePeerConnection(config);
3514   AddAudioTrack("foo");
3515 
3516   std::unique_ptr<SessionDescriptionInterface> offer;
3517   ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
3518   cricket::SessionDescription* desc = offer->description();
3519   EXPECT_EQ(1u, desc->transport_infos().size());
3520   EXPECT_FALSE(
3521       desc->transport_infos()[0].description.GetIceParameters().renomination);
3522 }
3523 
3524 // Test that the ICE renomination option is present in generated offers/answers
3525 // if it's enabled in the PC's RTCConfiguration.
TEST_P(PeerConnectionInterfaceTest,IceRenominationOptionInOfferAndAnswer)3526 TEST_P(PeerConnectionInterfaceTest, IceRenominationOptionInOfferAndAnswer) {
3527   PeerConnectionInterface::RTCConfiguration config;
3528   config.enable_ice_renomination = true;
3529   CreatePeerConnection(config);
3530   AddAudioTrack("foo");
3531 
3532   std::unique_ptr<SessionDescriptionInterface> offer;
3533   ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
3534   cricket::SessionDescription* desc = offer->description();
3535   EXPECT_EQ(1u, desc->transport_infos().size());
3536   EXPECT_TRUE(
3537       desc->transport_infos()[0].description.GetIceParameters().renomination);
3538 
3539   // Set the offer as a remote description, then create an answer and ensure it
3540   // has the renomination flag too.
3541   EXPECT_TRUE(DoSetRemoteDescription(std::move(offer)));
3542   std::unique_ptr<SessionDescriptionInterface> answer;
3543   ASSERT_TRUE(DoCreateAnswer(&answer, nullptr));
3544   desc = answer->description();
3545   EXPECT_EQ(1u, desc->transport_infos().size());
3546   EXPECT_TRUE(
3547       desc->transport_infos()[0].description.GetIceParameters().renomination);
3548 }
3549 
3550 // Test that if CreateOffer is called with the deprecated "offer to receive
3551 // audio/video" constraints, they're processed and result in an offer with
3552 // audio/video sections just as if RTCOfferAnswerOptions had been used.
TEST_P(PeerConnectionInterfaceTest,CreateOfferWithOfferToReceiveConstraints)3553 TEST_P(PeerConnectionInterfaceTest, CreateOfferWithOfferToReceiveConstraints) {
3554   CreatePeerConnection();
3555 
3556   RTCOfferAnswerOptions options;
3557   options.offer_to_receive_audio = 1;
3558   options.offer_to_receive_video = 1;
3559   std::unique_ptr<SessionDescriptionInterface> offer;
3560   ASSERT_TRUE(DoCreateOffer(&offer, &options));
3561 
3562   cricket::SessionDescription* desc = offer->description();
3563   const cricket::ContentInfo* audio = cricket::GetFirstAudioContent(desc);
3564   const cricket::ContentInfo* video = cricket::GetFirstVideoContent(desc);
3565   ASSERT_NE(nullptr, audio);
3566   ASSERT_NE(nullptr, video);
3567   EXPECT_FALSE(audio->rejected);
3568   EXPECT_FALSE(video->rejected);
3569 }
3570 
3571 // Test that if CreateAnswer is called with the deprecated "offer to receive
3572 // audio/video" constraints, they're processed and can be used to reject an
3573 // offered m= section just as can be done with RTCOfferAnswerOptions;
3574 // Don't run under Unified Plan since this behavior is not supported.
TEST_F(PeerConnectionInterfaceTestPlanB,CreateAnswerWithOfferToReceiveConstraints)3575 TEST_F(PeerConnectionInterfaceTestPlanB,
3576        CreateAnswerWithOfferToReceiveConstraints) {
3577   CreatePeerConnection();
3578 
3579   // First, create an offer with audio/video and apply it as a remote
3580   // description.
3581   RTCOfferAnswerOptions options;
3582   options.offer_to_receive_audio = 1;
3583   options.offer_to_receive_video = 1;
3584   std::unique_ptr<SessionDescriptionInterface> offer;
3585   ASSERT_TRUE(DoCreateOffer(&offer, &options));
3586   EXPECT_TRUE(DoSetRemoteDescription(std::move(offer)));
3587 
3588   // Now create answer that rejects audio/video.
3589   options.offer_to_receive_audio = 0;
3590   options.offer_to_receive_video = 0;
3591   std::unique_ptr<SessionDescriptionInterface> answer;
3592   ASSERT_TRUE(DoCreateAnswer(&answer, &options));
3593 
3594   cricket::SessionDescription* desc = answer->description();
3595   const cricket::ContentInfo* audio = cricket::GetFirstAudioContent(desc);
3596   const cricket::ContentInfo* video = cricket::GetFirstVideoContent(desc);
3597   ASSERT_NE(nullptr, audio);
3598   ASSERT_NE(nullptr, video);
3599   EXPECT_TRUE(audio->rejected);
3600   EXPECT_TRUE(video->rejected);
3601 }
3602 
3603 // Test that negotiation can succeed with a data channel only, and with the max
3604 // bundle policy. Previously there was a bug that prevented this.
3605 #ifdef WEBRTC_HAVE_SCTP
TEST_P(PeerConnectionInterfaceTest,DataChannelOnlyOfferWithMaxBundlePolicy)3606 TEST_P(PeerConnectionInterfaceTest, DataChannelOnlyOfferWithMaxBundlePolicy) {
3607 #else
3608 TEST_P(PeerConnectionInterfaceTest,
3609        DISABLED_DataChannelOnlyOfferWithMaxBundlePolicy) {
3610 #endif  // WEBRTC_HAVE_SCTP
3611   PeerConnectionInterface::RTCConfiguration config;
3612   config.bundle_policy = PeerConnectionInterface::kBundlePolicyMaxBundle;
3613   CreatePeerConnection(config);
3614 
3615   // First, create an offer with only a data channel and apply it as a remote
3616   // description.
3617   pc_->CreateDataChannel("test", nullptr);
3618   std::unique_ptr<SessionDescriptionInterface> offer;
3619   ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
3620   EXPECT_TRUE(DoSetRemoteDescription(std::move(offer)));
3621 
3622   // Create and set answer as well.
3623   std::unique_ptr<SessionDescriptionInterface> answer;
3624   ASSERT_TRUE(DoCreateAnswer(&answer, nullptr));
3625   EXPECT_TRUE(DoSetLocalDescription(std::move(answer)));
3626 }
3627 
3628 TEST_P(PeerConnectionInterfaceTest, SetBitrateWithoutMinSucceeds) {
3629   CreatePeerConnection();
3630   BitrateSettings bitrate;
3631   bitrate.start_bitrate_bps = 100000;
3632   EXPECT_TRUE(pc_->SetBitrate(bitrate).ok());
3633 }
3634 
3635 TEST_P(PeerConnectionInterfaceTest, SetBitrateNegativeMinFails) {
3636   CreatePeerConnection();
3637   BitrateSettings bitrate;
3638   bitrate.min_bitrate_bps = -1;
3639   EXPECT_FALSE(pc_->SetBitrate(bitrate).ok());
3640 }
3641 
3642 TEST_P(PeerConnectionInterfaceTest, SetBitrateCurrentLessThanMinFails) {
3643   CreatePeerConnection();
3644   BitrateSettings bitrate;
3645   bitrate.min_bitrate_bps = 5;
3646   bitrate.start_bitrate_bps = 3;
3647   EXPECT_FALSE(pc_->SetBitrate(bitrate).ok());
3648 }
3649 
3650 TEST_P(PeerConnectionInterfaceTest, SetBitrateCurrentNegativeFails) {
3651   CreatePeerConnection();
3652   BitrateSettings bitrate;
3653   bitrate.start_bitrate_bps = -1;
3654   EXPECT_FALSE(pc_->SetBitrate(bitrate).ok());
3655 }
3656 
3657 TEST_P(PeerConnectionInterfaceTest, SetBitrateMaxLessThanCurrentFails) {
3658   CreatePeerConnection();
3659   BitrateSettings bitrate;
3660   bitrate.start_bitrate_bps = 10;
3661   bitrate.max_bitrate_bps = 8;
3662   EXPECT_FALSE(pc_->SetBitrate(bitrate).ok());
3663 }
3664 
3665 TEST_P(PeerConnectionInterfaceTest, SetBitrateMaxLessThanMinFails) {
3666   CreatePeerConnection();
3667   BitrateSettings bitrate;
3668   bitrate.min_bitrate_bps = 10;
3669   bitrate.max_bitrate_bps = 8;
3670   EXPECT_FALSE(pc_->SetBitrate(bitrate).ok());
3671 }
3672 
3673 TEST_P(PeerConnectionInterfaceTest, SetBitrateMaxNegativeFails) {
3674   CreatePeerConnection();
3675   BitrateSettings bitrate;
3676   bitrate.max_bitrate_bps = -1;
3677   EXPECT_FALSE(pc_->SetBitrate(bitrate).ok());
3678 }
3679 
3680 // The current bitrate from BitrateSettings is currently clamped
3681 // by Call's BitrateConstraints, which comes from the SDP or a default value.
3682 // This test checks that a call to SetBitrate with a current bitrate that will
3683 // be clamped succeeds.
3684 TEST_P(PeerConnectionInterfaceTest, SetBitrateCurrentLessThanImplicitMin) {
3685   CreatePeerConnection();
3686   BitrateSettings bitrate;
3687   bitrate.start_bitrate_bps = 1;
3688   EXPECT_TRUE(pc_->SetBitrate(bitrate).ok());
3689 }
3690 
3691 // The following tests verify that the offer can be created correctly.
3692 TEST_P(PeerConnectionInterfaceTest,
3693        CreateOfferFailsWithInvalidOfferToReceiveAudio) {
3694   RTCOfferAnswerOptions rtc_options;
3695 
3696   // Setting offer_to_receive_audio to a value lower than kUndefined or greater
3697   // than kMaxOfferToReceiveMedia should be treated as invalid.
3698   rtc_options.offer_to_receive_audio = RTCOfferAnswerOptions::kUndefined - 1;
3699   CreatePeerConnection();
3700   EXPECT_FALSE(CreateOfferWithOptions(rtc_options));
3701 
3702   rtc_options.offer_to_receive_audio =
3703       RTCOfferAnswerOptions::kMaxOfferToReceiveMedia + 1;
3704   EXPECT_FALSE(CreateOfferWithOptions(rtc_options));
3705 }
3706 
3707 TEST_P(PeerConnectionInterfaceTest,
3708        CreateOfferFailsWithInvalidOfferToReceiveVideo) {
3709   RTCOfferAnswerOptions rtc_options;
3710 
3711   // Setting offer_to_receive_video to a value lower than kUndefined or greater
3712   // than kMaxOfferToReceiveMedia should be treated as invalid.
3713   rtc_options.offer_to_receive_video = RTCOfferAnswerOptions::kUndefined - 1;
3714   CreatePeerConnection();
3715   EXPECT_FALSE(CreateOfferWithOptions(rtc_options));
3716 
3717   rtc_options.offer_to_receive_video =
3718       RTCOfferAnswerOptions::kMaxOfferToReceiveMedia + 1;
3719   EXPECT_FALSE(CreateOfferWithOptions(rtc_options));
3720 }
3721 
3722 // Test that the audio and video content will be added to an offer if both
3723 // |offer_to_receive_audio| and |offer_to_receive_video| options are 1.
3724 TEST_P(PeerConnectionInterfaceTest, CreateOfferWithAudioVideoOptions) {
3725   RTCOfferAnswerOptions rtc_options;
3726   rtc_options.offer_to_receive_audio = 1;
3727   rtc_options.offer_to_receive_video = 1;
3728 
3729   std::unique_ptr<SessionDescriptionInterface> offer;
3730   CreatePeerConnection();
3731   offer = CreateOfferWithOptions(rtc_options);
3732   ASSERT_TRUE(offer);
3733   EXPECT_NE(nullptr, GetFirstAudioContent(offer->description()));
3734   EXPECT_NE(nullptr, GetFirstVideoContent(offer->description()));
3735 }
3736 
3737 // Test that only audio content will be added to the offer if only
3738 // |offer_to_receive_audio| options is 1.
3739 TEST_P(PeerConnectionInterfaceTest, CreateOfferWithAudioOnlyOptions) {
3740   RTCOfferAnswerOptions rtc_options;
3741   rtc_options.offer_to_receive_audio = 1;
3742   rtc_options.offer_to_receive_video = 0;
3743 
3744   std::unique_ptr<SessionDescriptionInterface> offer;
3745   CreatePeerConnection();
3746   offer = CreateOfferWithOptions(rtc_options);
3747   ASSERT_TRUE(offer);
3748   EXPECT_NE(nullptr, GetFirstAudioContent(offer->description()));
3749   EXPECT_EQ(nullptr, GetFirstVideoContent(offer->description()));
3750 }
3751 
3752 // Test that only video content will be added if only |offer_to_receive_video|
3753 // options is 1.
3754 TEST_P(PeerConnectionInterfaceTest, CreateOfferWithVideoOnlyOptions) {
3755   RTCOfferAnswerOptions rtc_options;
3756   rtc_options.offer_to_receive_audio = 0;
3757   rtc_options.offer_to_receive_video = 1;
3758 
3759   std::unique_ptr<SessionDescriptionInterface> offer;
3760   CreatePeerConnection();
3761   offer = CreateOfferWithOptions(rtc_options);
3762   ASSERT_TRUE(offer);
3763   EXPECT_EQ(nullptr, GetFirstAudioContent(offer->description()));
3764   EXPECT_NE(nullptr, GetFirstVideoContent(offer->description()));
3765 }
3766 
3767 // Test that no media content will be added to the offer if using default
3768 // RTCOfferAnswerOptions.
3769 TEST_P(PeerConnectionInterfaceTest, CreateOfferWithDefaultOfferAnswerOptions) {
3770   RTCOfferAnswerOptions rtc_options;
3771 
3772   std::unique_ptr<SessionDescriptionInterface> offer;
3773   CreatePeerConnection();
3774   offer = CreateOfferWithOptions(rtc_options);
3775   ASSERT_TRUE(offer);
3776   EXPECT_EQ(nullptr, GetFirstAudioContent(offer->description()));
3777   EXPECT_EQ(nullptr, GetFirstVideoContent(offer->description()));
3778 }
3779 
3780 // Test that if |ice_restart| is true, the ufrag/pwd will change, otherwise
3781 // ufrag/pwd will be the same in the new offer.
3782 TEST_P(PeerConnectionInterfaceTest, CreateOfferWithIceRestart) {
3783   CreatePeerConnection();
3784 
3785   RTCOfferAnswerOptions rtc_options;
3786   rtc_options.ice_restart = false;
3787   rtc_options.offer_to_receive_audio = 1;
3788 
3789   std::unique_ptr<SessionDescriptionInterface> offer;
3790   CreateOfferWithOptionsAsLocalDescription(&offer, rtc_options);
3791   std::string mid = cricket::GetFirstAudioContent(offer->description())->name;
3792   auto ufrag1 =
3793       offer->description()->GetTransportInfoByName(mid)->description.ice_ufrag;
3794   auto pwd1 =
3795       offer->description()->GetTransportInfoByName(mid)->description.ice_pwd;
3796 
3797   // |ice_restart| is false, the ufrag/pwd shouldn't change.
3798   CreateOfferWithOptionsAsLocalDescription(&offer, rtc_options);
3799   auto ufrag2 =
3800       offer->description()->GetTransportInfoByName(mid)->description.ice_ufrag;
3801   auto pwd2 =
3802       offer->description()->GetTransportInfoByName(mid)->description.ice_pwd;
3803 
3804   // |ice_restart| is true, the ufrag/pwd should change.
3805   rtc_options.ice_restart = true;
3806   CreateOfferWithOptionsAsLocalDescription(&offer, rtc_options);
3807   auto ufrag3 =
3808       offer->description()->GetTransportInfoByName(mid)->description.ice_ufrag;
3809   auto pwd3 =
3810       offer->description()->GetTransportInfoByName(mid)->description.ice_pwd;
3811 
3812   EXPECT_EQ(ufrag1, ufrag2);
3813   EXPECT_EQ(pwd1, pwd2);
3814   EXPECT_NE(ufrag2, ufrag3);
3815   EXPECT_NE(pwd2, pwd3);
3816 }
3817 
3818 // Test that if |use_rtp_mux| is true, the bundling will be enabled in the
3819 // offer; if it is false, there won't be any bundle group in the offer.
3820 TEST_P(PeerConnectionInterfaceTest, CreateOfferWithRtpMux) {
3821   RTCOfferAnswerOptions rtc_options;
3822   rtc_options.offer_to_receive_audio = 1;
3823   rtc_options.offer_to_receive_video = 1;
3824 
3825   std::unique_ptr<SessionDescriptionInterface> offer;
3826   CreatePeerConnection();
3827 
3828   rtc_options.use_rtp_mux = true;
3829   offer = CreateOfferWithOptions(rtc_options);
3830   ASSERT_TRUE(offer);
3831   EXPECT_NE(nullptr, GetFirstAudioContent(offer->description()));
3832   EXPECT_NE(nullptr, GetFirstVideoContent(offer->description()));
3833   EXPECT_TRUE(offer->description()->HasGroup(cricket::GROUP_TYPE_BUNDLE));
3834 
3835   rtc_options.use_rtp_mux = false;
3836   offer = CreateOfferWithOptions(rtc_options);
3837   ASSERT_TRUE(offer);
3838   EXPECT_NE(nullptr, GetFirstAudioContent(offer->description()));
3839   EXPECT_NE(nullptr, GetFirstVideoContent(offer->description()));
3840   EXPECT_FALSE(offer->description()->HasGroup(cricket::GROUP_TYPE_BUNDLE));
3841 }
3842 
3843 // This test ensures OnRenegotiationNeeded is called when we add track with
3844 // MediaStream -> AddTrack in the same way it is called when we add track with
3845 // PeerConnection -> AddTrack.
3846 // The test can be removed once addStream is rewritten in terms of addTrack
3847 // https://bugs.chromium.org/p/webrtc/issues/detail?id=7815
3848 // Don't run under Unified Plan since the stream API is not available.
3849 TEST_F(PeerConnectionInterfaceTestPlanB,
3850        MediaStreamAddTrackRemoveTrackRenegotiate) {
3851   CreatePeerConnectionWithoutDtls();
3852   rtc::scoped_refptr<MediaStreamInterface> stream(
3853       pc_factory_->CreateLocalMediaStream(kStreamId1));
3854   pc_->AddStream(stream);
3855   rtc::scoped_refptr<AudioTrackInterface> audio_track(
3856       CreateAudioTrack("audio_track"));
3857   rtc::scoped_refptr<VideoTrackInterface> video_track(
3858       CreateVideoTrack("video_track"));
3859   stream->AddTrack(audio_track);
3860   EXPECT_TRUE_WAIT(observer_.renegotiation_needed_, kTimeout);
3861   observer_.renegotiation_needed_ = false;
3862 
3863   CreateOfferReceiveAnswer();
3864   stream->AddTrack(video_track);
3865   EXPECT_TRUE_WAIT(observer_.renegotiation_needed_, kTimeout);
3866   observer_.renegotiation_needed_ = false;
3867 
3868   CreateOfferReceiveAnswer();
3869   stream->RemoveTrack(audio_track);
3870   EXPECT_TRUE_WAIT(observer_.renegotiation_needed_, kTimeout);
3871   observer_.renegotiation_needed_ = false;
3872 
3873   CreateOfferReceiveAnswer();
3874   stream->RemoveTrack(video_track);
3875   EXPECT_TRUE_WAIT(observer_.renegotiation_needed_, kTimeout);
3876   observer_.renegotiation_needed_ = false;
3877 }
3878 
3879 // Tests that an error is returned if a description is applied that has fewer
3880 // media sections than the existing description.
3881 TEST_P(PeerConnectionInterfaceTest,
3882        MediaSectionCountEnforcedForSubsequentOffer) {
3883   CreatePeerConnection();
3884   AddAudioTrack("audio_label");
3885   AddVideoTrack("video_label");
3886 
3887   std::unique_ptr<SessionDescriptionInterface> offer;
3888   ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
3889   EXPECT_TRUE(DoSetRemoteDescription(std::move(offer)));
3890 
3891   // A remote offer with fewer media sections should be rejected.
3892   ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
3893   offer->description()->contents().pop_back();
3894   offer->description()->contents().pop_back();
3895   ASSERT_TRUE(offer->description()->contents().empty());
3896   EXPECT_FALSE(DoSetRemoteDescription(std::move(offer)));
3897 
3898   std::unique_ptr<SessionDescriptionInterface> answer;
3899   ASSERT_TRUE(DoCreateAnswer(&answer, nullptr));
3900   EXPECT_TRUE(DoSetLocalDescription(std::move(answer)));
3901 
3902   // A subsequent local offer with fewer media sections should be rejected.
3903   ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
3904   offer->description()->contents().pop_back();
3905   offer->description()->contents().pop_back();
3906   ASSERT_TRUE(offer->description()->contents().empty());
3907   EXPECT_FALSE(DoSetLocalDescription(std::move(offer)));
3908 }
3909 
3910 TEST_P(PeerConnectionInterfaceTest, ExtmapAllowMixedIsConfigurable) {
3911   RTCConfiguration config;
3912   // Default behavior is true.
3913   CreatePeerConnection(config);
3914   std::unique_ptr<SessionDescriptionInterface> offer;
3915   ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
3916   EXPECT_TRUE(offer->description()->extmap_allow_mixed());
3917   // Possible to set to false.
3918   config.offer_extmap_allow_mixed = false;
3919   CreatePeerConnection(config);
3920   offer = nullptr;
3921   ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
3922   EXPECT_FALSE(offer->description()->extmap_allow_mixed());
3923 }
3924 
3925 INSTANTIATE_TEST_SUITE_P(PeerConnectionInterfaceTest,
3926                          PeerConnectionInterfaceTest,
3927                          Values(SdpSemantics::kPlanB,
3928                                 SdpSemantics::kUnifiedPlan));
3929 
3930 class PeerConnectionMediaConfigTest : public ::testing::Test {
3931  protected:
3932   void SetUp() override {
3933     pcf_ = PeerConnectionFactoryForTest::CreatePeerConnectionFactoryForTest();
3934   }
3935   const cricket::MediaConfig TestCreatePeerConnection(
3936       const RTCConfiguration& config) {
3937     rtc::scoped_refptr<PeerConnectionInterface> pc(
3938         pcf_->CreatePeerConnection(config, nullptr, nullptr, &observer_));
3939     EXPECT_TRUE(pc.get());
3940     observer_.SetPeerConnectionInterface(pc.get());
3941     return pc->GetConfiguration().media_config;
3942   }
3943 
3944   rtc::scoped_refptr<PeerConnectionFactoryForTest> pcf_;
3945   MockPeerConnectionObserver observer_;
3946 };
3947 
3948 // This sanity check validates the test infrastructure itself.
3949 TEST_F(PeerConnectionMediaConfigTest, TestCreateAndClose) {
3950   PeerConnectionInterface::RTCConfiguration config;
3951   rtc::scoped_refptr<PeerConnectionInterface> pc(
3952       pcf_->CreatePeerConnection(config, nullptr, nullptr, &observer_));
3953   EXPECT_TRUE(pc.get());
3954   observer_.SetPeerConnectionInterface(pc.get());  // Required.
3955   pc->Close();                                     // No abort -> ok.
3956   SUCCEED();
3957 }
3958 
3959 // This test verifies the default behaviour with no constraints and a
3960 // default RTCConfiguration.
3961 TEST_F(PeerConnectionMediaConfigTest, TestDefaults) {
3962   PeerConnectionInterface::RTCConfiguration config;
3963 
3964   const cricket::MediaConfig& media_config = TestCreatePeerConnection(config);
3965 
3966   EXPECT_FALSE(media_config.enable_dscp);
3967   EXPECT_TRUE(media_config.video.enable_cpu_adaptation);
3968   EXPECT_TRUE(media_config.video.enable_prerenderer_smoothing);
3969   EXPECT_FALSE(media_config.video.suspend_below_min_bitrate);
3970   EXPECT_FALSE(media_config.video.experiment_cpu_load_estimator);
3971 }
3972 
3973 // This test verifies that the enable_prerenderer_smoothing flag is
3974 // propagated from RTCConfiguration to the PeerConnection.
3975 TEST_F(PeerConnectionMediaConfigTest, TestDisablePrerendererSmoothingTrue) {
3976   PeerConnectionInterface::RTCConfiguration config;
3977 
3978   config.set_prerenderer_smoothing(false);
3979   const cricket::MediaConfig& media_config = TestCreatePeerConnection(config);
3980 
3981   EXPECT_FALSE(media_config.video.enable_prerenderer_smoothing);
3982 }
3983 
3984 // This test verifies that the experiment_cpu_load_estimator flag is
3985 // propagated from RTCConfiguration to the PeerConnection.
3986 TEST_F(PeerConnectionMediaConfigTest, TestEnableExperimentCpuLoadEstimator) {
3987   PeerConnectionInterface::RTCConfiguration config;
3988 
3989   config.set_experiment_cpu_load_estimator(true);
3990   const cricket::MediaConfig& media_config = TestCreatePeerConnection(config);
3991 
3992   EXPECT_TRUE(media_config.video.experiment_cpu_load_estimator);
3993 }
3994 
3995 // Tests a few random fields being different.
3996 TEST(RTCConfigurationTest, ComparisonOperators) {
3997   PeerConnectionInterface::RTCConfiguration a;
3998   PeerConnectionInterface::RTCConfiguration b;
3999   EXPECT_EQ(a, b);
4000 
4001   PeerConnectionInterface::RTCConfiguration c;
4002   c.servers.push_back(PeerConnectionInterface::IceServer());
4003   EXPECT_NE(a, c);
4004 
4005   PeerConnectionInterface::RTCConfiguration d;
4006   d.type = PeerConnectionInterface::kRelay;
4007   EXPECT_NE(a, d);
4008 
4009   PeerConnectionInterface::RTCConfiguration e;
4010   e.audio_jitter_buffer_max_packets = 5;
4011   EXPECT_NE(a, e);
4012 
4013   PeerConnectionInterface::RTCConfiguration f;
4014   f.ice_connection_receiving_timeout = 1337;
4015   EXPECT_NE(a, f);
4016 
4017   PeerConnectionInterface::RTCConfiguration g;
4018   g.disable_ipv6 = true;
4019   EXPECT_NE(a, g);
4020 
4021   PeerConnectionInterface::RTCConfiguration h(
4022       PeerConnectionInterface::RTCConfigurationType::kAggressive);
4023   EXPECT_NE(a, h);
4024 }
4025 
4026 }  // namespace
4027 }  // namespace webrtc
4028