blob: 76765aa4be30eb3e0180bcb52fd15501c839eace [file] [log] [blame]
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001/*
2 * libjingle
3 * Copyright 2011, Google Inc.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright notice,
9 * this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright notice,
11 * this list of conditions and the following disclaimer in the documentation
12 * and/or other materials provided with the distribution.
13 * 3. The name of the author may not be used to endorse or promote products
14 * derived from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
19 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28#include <set>
29#include <string>
30#include <vector>
31
32#include "talk/app/webrtc/jsepsessiondescription.h"
33#include "talk/app/webrtc/webrtcsdp.h"
34#include "talk/base/gunit.h"
35#include "talk/base/logging.h"
36#include "talk/base/messagedigest.h"
37#include "talk/base/scoped_ptr.h"
38#include "talk/base/sslfingerprint.h"
39#include "talk/base/stringencode.h"
40#include "talk/base/stringutils.h"
41#include "talk/media/base/constants.h"
42#include "talk/p2p/base/constants.h"
43#include "talk/session/media/mediasession.h"
44
45using cricket::AudioCodec;
46using cricket::AudioContentDescription;
47using cricket::Candidate;
48using cricket::ContentInfo;
49using cricket::CryptoParams;
50using cricket::ContentGroup;
51using cricket::DataCodec;
52using cricket::DataContentDescription;
53using cricket::ICE_CANDIDATE_COMPONENT_RTCP;
54using cricket::ICE_CANDIDATE_COMPONENT_RTP;
55using cricket::kFecSsrcGroupSemantics;
56using cricket::LOCAL_PORT_TYPE;
57using cricket::NS_JINGLE_DRAFT_SCTP;
58using cricket::NS_JINGLE_ICE_UDP;
59using cricket::NS_JINGLE_RTP;
60using cricket::RtpHeaderExtension;
61using cricket::RELAY_PORT_TYPE;
62using cricket::SessionDescription;
63using cricket::StreamParams;
64using cricket::STUN_PORT_TYPE;
65using cricket::TransportDescription;
66using cricket::TransportInfo;
67using cricket::VideoCodec;
68using cricket::VideoContentDescription;
69using webrtc::IceCandidateCollection;
70using webrtc::IceCandidateInterface;
71using webrtc::JsepIceCandidate;
72using webrtc::JsepSessionDescription;
73using webrtc::SdpParseError;
74using webrtc::SessionDescriptionInterface;
75
76typedef std::vector<AudioCodec> AudioCodecs;
77typedef std::vector<Candidate> Candidates;
78
wu@webrtc.org78187522013-10-07 23:32:02 +000079static const uint32 kDefaultSctpPort = 5000;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000080static const char kSessionTime[] = "t=0 0\r\n";
81static const uint32 kCandidatePriority = 2130706432U; // pref = 1.0
82static const char kCandidateUfragVoice[] = "ufrag_voice";
83static const char kCandidatePwdVoice[] = "pwd_voice";
84static const char kAttributeIcePwdVoice[] = "a=ice-pwd:pwd_voice\r\n";
85static const char kCandidateUfragVideo[] = "ufrag_video";
86static const char kCandidatePwdVideo[] = "pwd_video";
87static const char kCandidateUfragData[] = "ufrag_data";
88static const char kCandidatePwdData[] = "pwd_data";
89static const char kAttributeIcePwdVideo[] = "a=ice-pwd:pwd_video\r\n";
90static const uint32 kCandidateGeneration = 2;
91static const char kCandidateFoundation1[] = "a0+B/1";
92static const char kCandidateFoundation2[] = "a0+B/2";
93static const char kCandidateFoundation3[] = "a0+B/3";
94static const char kCandidateFoundation4[] = "a0+B/4";
95static const char kAttributeCryptoVoice[] =
96 "a=crypto:1 AES_CM_128_HMAC_SHA1_32 "
97 "inline:NzB4d1BINUAvLEw6UzF3WSJ+PSdFcGdUJShpX1Zj|2^20|1:32 "
98 "dummy_session_params\r\n";
99static const char kAttributeCryptoVideo[] =
100 "a=crypto:1 AES_CM_128_HMAC_SHA1_80 "
101 "inline:d0RmdmcmVCspeEc3QGZiNWpVLFJhQX1cfHAwJSoj|2^20|1:32\r\n";
102static const char kFingerprint[] = "a=fingerprint:sha-1 "
103 "4A:AD:B9:B1:3F:82:18:3B:54:02:12:DF:3E:5D:49:6B:19:E5:7C:AB\r\n";
104static const int kExtmapId = 1;
105static const char kExtmapUri[] = "http://example.com/082005/ext.htm#ttime";
106static const char kExtmap[] =
107 "a=extmap:1 http://example.com/082005/ext.htm#ttime\r\n";
108static const char kExtmapWithDirectionAndAttribute[] =
109 "a=extmap:1/sendrecv http://example.com/082005/ext.htm#ttime a1 a2\r\n";
110
111static const uint8 kIdentityDigest[] = {0x4A, 0xAD, 0xB9, 0xB1,
112 0x3F, 0x82, 0x18, 0x3B,
113 0x54, 0x02, 0x12, 0xDF,
114 0x3E, 0x5D, 0x49, 0x6B,
115 0x19, 0xE5, 0x7C, 0xAB};
116
117struct CodecParams {
118 int max_ptime;
119 int ptime;
120 int min_ptime;
121 int sprop_stereo;
122 int stereo;
123 int useinband;
henrike@webrtc.org1e09a712013-07-26 19:17:59 +0000124 int maxaveragebitrate;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000125};
126
127// Reference sdp string
128static const char kSdpFullString[] =
129 "v=0\r\n"
130 "o=- 18446744069414584320 18446462598732840960 IN IP4 127.0.0.1\r\n"
131 "s=-\r\n"
132 "t=0 0\r\n"
133 "a=msid-semantic: WMS local_stream_1 local_stream_2\r\n"
134 "m=audio 2345 RTP/SAVPF 111 103 104\r\n"
135 "c=IN IP4 74.125.127.126\r\n"
136 "a=rtcp:2347 IN IP4 74.125.127.126\r\n"
137 "a=candidate:a0+B/1 1 udp 2130706432 192.168.1.5 1234 typ host "
138 "generation 2\r\n"
139 "a=candidate:a0+B/1 2 udp 2130706432 192.168.1.5 1235 typ host "
140 "generation 2\r\n"
141 "a=candidate:a0+B/2 1 udp 2130706432 ::1 1238 typ host "
142 "generation 2\r\n"
143 "a=candidate:a0+B/2 2 udp 2130706432 ::1 1239 typ host "
144 "generation 2\r\n"
145 "a=candidate:a0+B/3 1 udp 2130706432 74.125.127.126 2345 typ srflx "
146 "raddr 192.168.1.5 rport 2346 "
147 "generation 2\r\n"
148 "a=candidate:a0+B/3 2 udp 2130706432 74.125.127.126 2347 typ srflx "
149 "raddr 192.168.1.5 rport 2348 "
150 "generation 2\r\n"
151 "a=ice-ufrag:ufrag_voice\r\na=ice-pwd:pwd_voice\r\n"
152 "a=mid:audio_content_name\r\n"
153 "a=sendrecv\r\n"
154 "a=rtcp-mux\r\n"
155 "a=crypto:1 AES_CM_128_HMAC_SHA1_32 "
156 "inline:NzB4d1BINUAvLEw6UzF3WSJ+PSdFcGdUJShpX1Zj|2^20|1:32 "
157 "dummy_session_params\r\n"
158 "a=rtpmap:111 opus/48000/2\r\n"
159 "a=rtpmap:103 ISAC/16000\r\n"
160 "a=rtpmap:104 CELT/32000/2\r\n"
161 "a=ssrc:1 cname:stream_1_cname\r\n"
162 "a=ssrc:1 msid:local_stream_1 audio_track_id_1\r\n"
163 "a=ssrc:1 mslabel:local_stream_1\r\n"
164 "a=ssrc:1 label:audio_track_id_1\r\n"
165 "a=ssrc:4 cname:stream_2_cname\r\n"
166 "a=ssrc:4 msid:local_stream_2 audio_track_id_2\r\n"
167 "a=ssrc:4 mslabel:local_stream_2\r\n"
168 "a=ssrc:4 label:audio_track_id_2\r\n"
169 "m=video 3457 RTP/SAVPF 120\r\n"
170 "c=IN IP4 74.125.224.39\r\n"
171 "a=rtcp:3456 IN IP4 74.125.224.39\r\n"
172 "a=candidate:a0+B/1 2 udp 2130706432 192.168.1.5 1236 typ host "
173 "generation 2\r\n"
174 "a=candidate:a0+B/1 1 udp 2130706432 192.168.1.5 1237 typ host "
175 "generation 2\r\n"
176 "a=candidate:a0+B/2 2 udp 2130706432 ::1 1240 typ host "
177 "generation 2\r\n"
178 "a=candidate:a0+B/2 1 udp 2130706432 ::1 1241 typ host "
179 "generation 2\r\n"
180 "a=candidate:a0+B/4 2 udp 2130706432 74.125.224.39 3456 typ relay "
181 "generation 2\r\n"
182 "a=candidate:a0+B/4 1 udp 2130706432 74.125.224.39 3457 typ relay "
183 "generation 2\r\n"
184 "a=ice-ufrag:ufrag_video\r\na=ice-pwd:pwd_video\r\n"
185 "a=mid:video_content_name\r\n"
186 "a=sendrecv\r\n"
187 "a=crypto:1 AES_CM_128_HMAC_SHA1_80 "
188 "inline:d0RmdmcmVCspeEc3QGZiNWpVLFJhQX1cfHAwJSoj|2^20|1:32\r\n"
189 "a=rtpmap:120 VP8/90000\r\n"
190 "a=ssrc:2 cname:stream_1_cname\r\n"
191 "a=ssrc:2 msid:local_stream_1 video_track_id_1\r\n"
192 "a=ssrc:2 mslabel:local_stream_1\r\n"
193 "a=ssrc:2 label:video_track_id_1\r\n"
194 "a=ssrc:3 cname:stream_1_cname\r\n"
195 "a=ssrc:3 msid:local_stream_1 video_track_id_2\r\n"
196 "a=ssrc:3 mslabel:local_stream_1\r\n"
197 "a=ssrc:3 label:video_track_id_2\r\n"
198 "a=ssrc-group:FEC 5 6\r\n"
199 "a=ssrc:5 cname:stream_2_cname\r\n"
200 "a=ssrc:5 msid:local_stream_2 video_track_id_3\r\n"
201 "a=ssrc:5 mslabel:local_stream_2\r\n"
202 "a=ssrc:5 label:video_track_id_3\r\n"
203 "a=ssrc:6 cname:stream_2_cname\r\n"
204 "a=ssrc:6 msid:local_stream_2 video_track_id_3\r\n"
205 "a=ssrc:6 mslabel:local_stream_2\r\n"
206 "a=ssrc:6 label:video_track_id_3\r\n";
207
208// SDP reference string without the candidates.
209static const char kSdpString[] =
210 "v=0\r\n"
211 "o=- 18446744069414584320 18446462598732840960 IN IP4 127.0.0.1\r\n"
212 "s=-\r\n"
213 "t=0 0\r\n"
214 "a=msid-semantic: WMS local_stream_1 local_stream_2\r\n"
215 "m=audio 1 RTP/SAVPF 111 103 104\r\n"
216 "c=IN IP4 0.0.0.0\r\n"
217 "a=rtcp:1 IN IP4 0.0.0.0\r\n"
218 "a=ice-ufrag:ufrag_voice\r\na=ice-pwd:pwd_voice\r\n"
219 "a=mid:audio_content_name\r\n"
220 "a=sendrecv\r\n"
221 "a=rtcp-mux\r\n"
222 "a=crypto:1 AES_CM_128_HMAC_SHA1_32 "
223 "inline:NzB4d1BINUAvLEw6UzF3WSJ+PSdFcGdUJShpX1Zj|2^20|1:32 "
224 "dummy_session_params\r\n"
225 "a=rtpmap:111 opus/48000/2\r\n"
226 "a=rtpmap:103 ISAC/16000\r\n"
227 "a=rtpmap:104 CELT/32000/2\r\n"
228 "a=ssrc:1 cname:stream_1_cname\r\n"
229 "a=ssrc:1 msid:local_stream_1 audio_track_id_1\r\n"
230 "a=ssrc:1 mslabel:local_stream_1\r\n"
231 "a=ssrc:1 label:audio_track_id_1\r\n"
232 "a=ssrc:4 cname:stream_2_cname\r\n"
233 "a=ssrc:4 msid:local_stream_2 audio_track_id_2\r\n"
234 "a=ssrc:4 mslabel:local_stream_2\r\n"
235 "a=ssrc:4 label:audio_track_id_2\r\n"
236 "m=video 1 RTP/SAVPF 120\r\n"
237 "c=IN IP4 0.0.0.0\r\n"
238 "a=rtcp:1 IN IP4 0.0.0.0\r\n"
239 "a=ice-ufrag:ufrag_video\r\na=ice-pwd:pwd_video\r\n"
240 "a=mid:video_content_name\r\n"
241 "a=sendrecv\r\n"
242 "a=crypto:1 AES_CM_128_HMAC_SHA1_80 "
243 "inline:d0RmdmcmVCspeEc3QGZiNWpVLFJhQX1cfHAwJSoj|2^20|1:32\r\n"
244 "a=rtpmap:120 VP8/90000\r\n"
245 "a=ssrc:2 cname:stream_1_cname\r\n"
246 "a=ssrc:2 msid:local_stream_1 video_track_id_1\r\n"
247 "a=ssrc:2 mslabel:local_stream_1\r\n"
248 "a=ssrc:2 label:video_track_id_1\r\n"
249 "a=ssrc:3 cname:stream_1_cname\r\n"
250 "a=ssrc:3 msid:local_stream_1 video_track_id_2\r\n"
251 "a=ssrc:3 mslabel:local_stream_1\r\n"
252 "a=ssrc:3 label:video_track_id_2\r\n"
253 "a=ssrc-group:FEC 5 6\r\n"
254 "a=ssrc:5 cname:stream_2_cname\r\n"
255 "a=ssrc:5 msid:local_stream_2 video_track_id_3\r\n"
256 "a=ssrc:5 mslabel:local_stream_2\r\n"
257 "a=ssrc:5 label:video_track_id_3\r\n"
258 "a=ssrc:6 cname:stream_2_cname\r\n"
259 "a=ssrc:6 msid:local_stream_2 video_track_id_3\r\n"
260 "a=ssrc:6 mslabel:local_stream_2\r\n"
261 "a=ssrc:6 label:video_track_id_3\r\n";
262
263static const char kSdpRtpDataChannelString[] =
264 "m=application 1 RTP/SAVPF 101\r\n"
265 "c=IN IP4 0.0.0.0\r\n"
266 "a=rtcp:1 IN IP4 0.0.0.0\r\n"
267 "a=ice-ufrag:ufrag_data\r\n"
268 "a=ice-pwd:pwd_data\r\n"
269 "a=mid:data_content_name\r\n"
270 "a=sendrecv\r\n"
271 "a=crypto:1 AES_CM_128_HMAC_SHA1_80 "
272 "inline:FvLcvU2P3ZWmQxgPAgcDu7Zl9vftYElFOjEzhWs5\r\n"
273 "a=rtpmap:101 google-data/90000\r\n"
274 "a=ssrc:10 cname:data_channel_cname\r\n"
275 "a=ssrc:10 msid:data_channel data_channeld0\r\n"
276 "a=ssrc:10 mslabel:data_channel\r\n"
277 "a=ssrc:10 label:data_channeld0\r\n";
278
279static const char kSdpSctpDataChannelString[] =
280 "m=application 1 DTLS/SCTP 5000\r\n"
281 "c=IN IP4 0.0.0.0\r\n"
282 "a=ice-ufrag:ufrag_data\r\n"
283 "a=ice-pwd:pwd_data\r\n"
284 "a=mid:data_content_name\r\n"
wu@webrtc.org97077a32013-10-25 21:18:33 +0000285 "a=sctpmap:5000 webrtc-datachannel 1024\r\n";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000286
287static const char kSdpSctpDataChannelWithCandidatesString[] =
288 "m=application 2345 DTLS/SCTP 5000\r\n"
289 "c=IN IP4 74.125.127.126\r\n"
290 "a=candidate:a0+B/1 1 udp 2130706432 192.168.1.5 1234 typ host "
291 "generation 2\r\n"
292 "a=candidate:a0+B/2 1 udp 2130706432 ::1 1238 typ host "
293 "generation 2\r\n"
294 "a=candidate:a0+B/3 1 udp 2130706432 74.125.127.126 2345 typ srflx "
295 "raddr 192.168.1.5 rport 2346 "
296 "generation 2\r\n"
297 "a=ice-ufrag:ufrag_data\r\n"
298 "a=ice-pwd:pwd_data\r\n"
299 "a=mid:data_content_name\r\n"
wu@webrtc.org97077a32013-10-25 21:18:33 +0000300 "a=sctpmap:5000 webrtc-datachannel 1024\r\n";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000301
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +0000302 static const char kSdpConferenceString[] =
303 "v=0\r\n"
304 "o=- 18446744069414584320 18446462598732840960 IN IP4 127.0.0.1\r\n"
305 "s=-\r\n"
306 "t=0 0\r\n"
307 "a=msid-semantic: WMS\r\n"
308 "m=audio 1 RTP/SAVPF 111 103 104\r\n"
309 "c=IN IP4 0.0.0.0\r\n"
310 "a=x-google-flag:conference\r\n"
311 "m=video 1 RTP/SAVPF 120\r\n"
312 "c=IN IP4 0.0.0.0\r\n"
313 "a=x-google-flag:conference\r\n";
314
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000315
316// One candidate reference string as per W3c spec.
317// candidate:<blah> not a=candidate:<blah>CRLF
318static const char kRawCandidate[] =
319 "candidate:a0+B/1 1 udp 2130706432 192.168.1.5 1234 typ host generation 2";
320// One candidate reference string.
321static const char kSdpOneCandidate[] =
322 "a=candidate:a0+B/1 1 udp 2130706432 192.168.1.5 1234 typ host "
323 "generation 2\r\n";
324
325// One candidate reference string.
326static const char kSdpOneCandidateOldFormat[] =
327 "a=candidate:a0+B/1 1 udp 2130706432 192.168.1.5 1234 typ host network_name"
328 " eth0 username user_rtp password password_rtp generation 2\r\n";
329
330// Session id and version
331static const char kSessionId[] = "18446744069414584320";
332static const char kSessionVersion[] = "18446462598732840960";
333
334// Ice options
335static const char kIceOption1[] = "iceoption1";
336static const char kIceOption2[] = "iceoption2";
337static const char kIceOption3[] = "iceoption3";
338
339// Content name
340static const char kAudioContentName[] = "audio_content_name";
341static const char kVideoContentName[] = "video_content_name";
342static const char kDataContentName[] = "data_content_name";
343
344// MediaStream 1
345static const char kStreamLabel1[] = "local_stream_1";
346static const char kStream1Cname[] = "stream_1_cname";
347static const char kAudioTrackId1[] = "audio_track_id_1";
348static const uint32 kAudioTrack1Ssrc = 1;
349static const char kVideoTrackId1[] = "video_track_id_1";
350static const uint32 kVideoTrack1Ssrc = 2;
351static const char kVideoTrackId2[] = "video_track_id_2";
352static const uint32 kVideoTrack2Ssrc = 3;
353
354// MediaStream 2
355static const char kStreamLabel2[] = "local_stream_2";
356static const char kStream2Cname[] = "stream_2_cname";
357static const char kAudioTrackId2[] = "audio_track_id_2";
358static const uint32 kAudioTrack2Ssrc = 4;
359static const char kVideoTrackId3[] = "video_track_id_3";
360static const uint32 kVideoTrack3Ssrc = 5;
361static const uint32 kVideoTrack4Ssrc = 6;
362
363// DataChannel
364static const char kDataChannelLabel[] = "data_channel";
365static const char kDataChannelMsid[] = "data_channeld0";
366static const char kDataChannelCname[] = "data_channel_cname";
367static const uint32 kDataChannelSsrc = 10;
368
369// Candidate
370static const char kDummyMid[] = "dummy_mid";
371static const int kDummyIndex = 123;
372
373// Misc
374static const char kDummyString[] = "dummy";
375
376// Helper functions
377
378static bool SdpDeserialize(const std::string& message,
379 JsepSessionDescription* jdesc) {
380 return webrtc::SdpDeserialize(message, jdesc, NULL);
381}
382
383static bool SdpDeserializeCandidate(const std::string& message,
384 JsepIceCandidate* candidate) {
385 return webrtc::SdpDeserializeCandidate(message, candidate, NULL);
386}
387
388// Add some extra |newlines| to the |message| after |line|.
389static void InjectAfter(const std::string& line,
390 const std::string& newlines,
391 std::string* message) {
392 const std::string tmp = line + newlines;
393 talk_base::replace_substrs(line.c_str(), line.length(),
394 tmp.c_str(), tmp.length(), message);
395}
396
397static void Replace(const std::string& line,
398 const std::string& newlines,
399 std::string* message) {
400 talk_base::replace_substrs(line.c_str(), line.length(),
401 newlines.c_str(), newlines.length(), message);
402}
403
404static void ReplaceAndTryToParse(const char* search, const char* replace) {
405 JsepSessionDescription desc(kDummyString);
406 std::string sdp = kSdpFullString;
407 Replace(search, replace, &sdp);
408 SdpParseError error;
409 bool ret = webrtc::SdpDeserialize(sdp, &desc, &error);
410 EXPECT_FALSE(ret);
411 EXPECT_NE(std::string::npos, error.line.find(replace));
412}
413
414static void ReplaceDirection(cricket::MediaContentDirection direction,
415 std::string* message) {
416 std::string new_direction;
417 switch (direction) {
418 case cricket::MD_INACTIVE:
419 new_direction = "a=inactive";
420 break;
421 case cricket::MD_SENDONLY:
422 new_direction = "a=sendonly";
423 break;
424 case cricket::MD_RECVONLY:
425 new_direction = "a=recvonly";
426 break;
427 case cricket::MD_SENDRECV:
428 default:
429 new_direction = "a=sendrecv";
430 break;
431 }
432 Replace("a=sendrecv", new_direction, message);
433}
434
435static void ReplaceRejected(bool audio_rejected, bool video_rejected,
436 std::string* message) {
437 if (audio_rejected) {
438 Replace("m=audio 2345", "m=audio 0", message);
439 }
440 if (video_rejected) {
441 Replace("m=video 3457", "m=video 0", message);
442 }
443}
444
445// WebRtcSdpTest
446
447class WebRtcSdpTest : public testing::Test {
448 public:
449 WebRtcSdpTest()
450 : jdesc_(kDummyString) {
451 // AudioContentDescription
452 audio_desc_ = CreateAudioContentDescription();
453 AudioCodec opus(111, "opus", 48000, 0, 2, 3);
454 audio_desc_->AddCodec(opus);
455 audio_desc_->AddCodec(AudioCodec(103, "ISAC", 16000, 32000, 1, 2));
456 audio_desc_->AddCodec(AudioCodec(104, "CELT", 32000, 0, 2, 1));
457 desc_.AddContent(kAudioContentName, NS_JINGLE_RTP, audio_desc_);
458
459 // VideoContentDescription
460 talk_base::scoped_ptr<VideoContentDescription> video(
461 new VideoContentDescription());
462 video_desc_ = video.get();
463 StreamParams video_stream1;
464 video_stream1.id = kVideoTrackId1;
465 video_stream1.cname = kStream1Cname;
466 video_stream1.sync_label = kStreamLabel1;
467 video_stream1.ssrcs.push_back(kVideoTrack1Ssrc);
468 video->AddStream(video_stream1);
469 StreamParams video_stream2;
470 video_stream2.id = kVideoTrackId2;
471 video_stream2.cname = kStream1Cname;
472 video_stream2.sync_label = kStreamLabel1;
473 video_stream2.ssrcs.push_back(kVideoTrack2Ssrc);
474 video->AddStream(video_stream2);
475 StreamParams video_stream3;
476 video_stream3.id = kVideoTrackId3;
477 video_stream3.cname = kStream2Cname;
478 video_stream3.sync_label = kStreamLabel2;
479 video_stream3.ssrcs.push_back(kVideoTrack3Ssrc);
480 video_stream3.ssrcs.push_back(kVideoTrack4Ssrc);
481 cricket::SsrcGroup ssrc_group(kFecSsrcGroupSemantics, video_stream3.ssrcs);
482 video_stream3.ssrc_groups.push_back(ssrc_group);
483 video->AddStream(video_stream3);
484 video->AddCrypto(CryptoParams(1, "AES_CM_128_HMAC_SHA1_80",
485 "inline:d0RmdmcmVCspeEc3QGZiNWpVLFJhQX1cfHAwJSoj|2^20|1:32", ""));
486 video->set_protocol(cricket::kMediaProtocolSavpf);
487 video->AddCodec(VideoCodec(
488 120,
489 JsepSessionDescription::kDefaultVideoCodecName,
490 JsepSessionDescription::kMaxVideoCodecWidth,
491 JsepSessionDescription::kMaxVideoCodecHeight,
492 JsepSessionDescription::kDefaultVideoCodecFramerate,
493 JsepSessionDescription::kDefaultVideoCodecPreference));
494
495 desc_.AddContent(kVideoContentName, NS_JINGLE_RTP,
496 video.release());
497
498 // TransportInfo
499 EXPECT_TRUE(desc_.AddTransportInfo(
500 TransportInfo(kAudioContentName,
501 TransportDescription(NS_JINGLE_ICE_UDP,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000502 kCandidateUfragVoice,
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +0000503 kCandidatePwdVoice))));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000504 EXPECT_TRUE(desc_.AddTransportInfo(
505 TransportInfo(kVideoContentName,
506 TransportDescription(NS_JINGLE_ICE_UDP,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000507 kCandidateUfragVideo,
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +0000508 kCandidatePwdVideo))));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000509
510 // v4 host
511 int port = 1234;
512 talk_base::SocketAddress address("192.168.1.5", port++);
513 Candidate candidate1(
514 "", ICE_CANDIDATE_COMPONENT_RTP, "udp", address, kCandidatePriority,
515 "", "", LOCAL_PORT_TYPE,
516 "", kCandidateGeneration, kCandidateFoundation1);
517 address.SetPort(port++);
518 Candidate candidate2(
519 "", ICE_CANDIDATE_COMPONENT_RTCP, "udp", address, kCandidatePriority,
520 "", "", LOCAL_PORT_TYPE,
521 "", kCandidateGeneration, kCandidateFoundation1);
522 address.SetPort(port++);
523 Candidate candidate3(
524 "", ICE_CANDIDATE_COMPONENT_RTCP, "udp", address, kCandidatePriority,
525 "", "", LOCAL_PORT_TYPE,
526 "", kCandidateGeneration, kCandidateFoundation1);
527 address.SetPort(port++);
528 Candidate candidate4(
529 "", ICE_CANDIDATE_COMPONENT_RTP, "udp", address, kCandidatePriority,
530 "", "", LOCAL_PORT_TYPE,
531 "", kCandidateGeneration, kCandidateFoundation1);
532
533 // v6 host
534 talk_base::SocketAddress v6_address("::1", port++);
535 cricket::Candidate candidate5(
536 "", cricket::ICE_CANDIDATE_COMPONENT_RTP,
537 "udp", v6_address, kCandidatePriority,
538 "", "", cricket::LOCAL_PORT_TYPE,
539 "", kCandidateGeneration, kCandidateFoundation2);
540 v6_address.SetPort(port++);
541 cricket::Candidate candidate6(
542 "", cricket::ICE_CANDIDATE_COMPONENT_RTCP,
543 "udp", v6_address, kCandidatePriority,
544 "", "", cricket::LOCAL_PORT_TYPE,
545 "", kCandidateGeneration, kCandidateFoundation2);
546 v6_address.SetPort(port++);
547 cricket::Candidate candidate7(
548 "", cricket::ICE_CANDIDATE_COMPONENT_RTCP,
549 "udp", v6_address, kCandidatePriority,
550 "", "", cricket::LOCAL_PORT_TYPE,
551 "", kCandidateGeneration, kCandidateFoundation2);
552 v6_address.SetPort(port++);
553 cricket::Candidate candidate8(
554 "", cricket::ICE_CANDIDATE_COMPONENT_RTP,
555 "udp", v6_address, kCandidatePriority,
556 "", "", cricket::LOCAL_PORT_TYPE,
557 "", kCandidateGeneration, kCandidateFoundation2);
558
559 // stun
560 int port_stun = 2345;
561 talk_base::SocketAddress address_stun("74.125.127.126", port_stun++);
562 talk_base::SocketAddress rel_address_stun("192.168.1.5", port_stun++);
563 cricket::Candidate candidate9
564 ("", cricket::ICE_CANDIDATE_COMPONENT_RTP,
565 "udp", address_stun, kCandidatePriority,
566 "", "", STUN_PORT_TYPE,
567 "", kCandidateGeneration, kCandidateFoundation3);
568 candidate9.set_related_address(rel_address_stun);
569
570 address_stun.SetPort(port_stun++);
571 rel_address_stun.SetPort(port_stun++);
572 cricket::Candidate candidate10(
573 "", cricket::ICE_CANDIDATE_COMPONENT_RTCP,
574 "udp", address_stun, kCandidatePriority,
575 "", "", STUN_PORT_TYPE,
576 "", kCandidateGeneration, kCandidateFoundation3);
577 candidate10.set_related_address(rel_address_stun);
578
579 // relay
580 int port_relay = 3456;
581 talk_base::SocketAddress address_relay("74.125.224.39", port_relay++);
582 cricket::Candidate candidate11(
583 "", cricket::ICE_CANDIDATE_COMPONENT_RTCP,
584 "udp", address_relay, kCandidatePriority,
585 "", "",
586 cricket::RELAY_PORT_TYPE, "",
587 kCandidateGeneration, kCandidateFoundation4);
588 address_relay.SetPort(port_relay++);
589 cricket::Candidate candidate12(
590 "", cricket::ICE_CANDIDATE_COMPONENT_RTP,
591 "udp", address_relay, kCandidatePriority,
592 "", "",
593 RELAY_PORT_TYPE, "",
594 kCandidateGeneration, kCandidateFoundation4);
595
596 // voice
597 candidates_.push_back(candidate1);
598 candidates_.push_back(candidate2);
599 candidates_.push_back(candidate5);
600 candidates_.push_back(candidate6);
601 candidates_.push_back(candidate9);
602 candidates_.push_back(candidate10);
603
604 // video
605 candidates_.push_back(candidate3);
606 candidates_.push_back(candidate4);
607 candidates_.push_back(candidate7);
608 candidates_.push_back(candidate8);
609 candidates_.push_back(candidate11);
610 candidates_.push_back(candidate12);
611
612 jcandidate_.reset(new JsepIceCandidate(std::string("audio_content_name"),
613 0, candidate1));
614
615 // Set up JsepSessionDescription.
616 jdesc_.Initialize(desc_.Copy(), kSessionId, kSessionVersion);
617 std::string mline_id;
618 int mline_index = 0;
619 for (size_t i = 0; i< candidates_.size(); ++i) {
620 // In this test, the audio m line index will be 0, and the video m line
621 // will be 1.
622 bool is_video = (i > 5);
623 mline_id = is_video ? "video_content_name" : "audio_content_name";
624 mline_index = is_video ? 1 : 0;
625 JsepIceCandidate jice(mline_id,
626 mline_index,
627 candidates_.at(i));
628 jdesc_.AddCandidate(&jice);
629 }
630 }
631
632 AudioContentDescription* CreateAudioContentDescription() {
633 AudioContentDescription* audio = new AudioContentDescription();
634 audio->set_rtcp_mux(true);
635 StreamParams audio_stream1;
636 audio_stream1.id = kAudioTrackId1;
637 audio_stream1.cname = kStream1Cname;
638 audio_stream1.sync_label = kStreamLabel1;
639 audio_stream1.ssrcs.push_back(kAudioTrack1Ssrc);
640 audio->AddStream(audio_stream1);
641 StreamParams audio_stream2;
642 audio_stream2.id = kAudioTrackId2;
643 audio_stream2.cname = kStream2Cname;
644 audio_stream2.sync_label = kStreamLabel2;
645 audio_stream2.ssrcs.push_back(kAudioTrack2Ssrc);
646 audio->AddStream(audio_stream2);
647 audio->AddCrypto(CryptoParams(1, "AES_CM_128_HMAC_SHA1_32",
648 "inline:NzB4d1BINUAvLEw6UzF3WSJ+PSdFcGdUJShpX1Zj|2^20|1:32",
649 "dummy_session_params"));
650 audio->set_protocol(cricket::kMediaProtocolSavpf);
651 return audio;
652 }
653
654 template <class MCD>
655 void CompareMediaContentDescription(const MCD* cd1,
656 const MCD* cd2) {
657 // type
658 EXPECT_EQ(cd1->type(), cd1->type());
659
660 // content direction
661 EXPECT_EQ(cd1->direction(), cd2->direction());
662
663 // rtcp_mux
664 EXPECT_EQ(cd1->rtcp_mux(), cd2->rtcp_mux());
665
666 // cryptos
667 EXPECT_EQ(cd1->cryptos().size(), cd2->cryptos().size());
668 if (cd1->cryptos().size() != cd2->cryptos().size()) {
669 ADD_FAILURE();
670 return;
671 }
672 for (size_t i = 0; i< cd1->cryptos().size(); ++i) {
673 const CryptoParams c1 = cd1->cryptos().at(i);
674 const CryptoParams c2 = cd2->cryptos().at(i);
675 EXPECT_TRUE(c1.Matches(c2));
676 EXPECT_EQ(c1.key_params, c2.key_params);
677 EXPECT_EQ(c1.session_params, c2.session_params);
678 }
679 // protocol
680 EXPECT_EQ(cd1->protocol(), cd2->protocol());
681
682 // codecs
683 EXPECT_EQ(cd1->codecs(), cd2->codecs());
684
685 // bandwidth
686 EXPECT_EQ(cd1->bandwidth(), cd2->bandwidth());
687
688 // streams
689 EXPECT_EQ(cd1->streams(), cd2->streams());
690
691 // extmap
692 ASSERT_EQ(cd1->rtp_header_extensions().size(),
693 cd2->rtp_header_extensions().size());
694 for (size_t i = 0; i< cd1->rtp_header_extensions().size(); ++i) {
695 const RtpHeaderExtension ext1 = cd1->rtp_header_extensions().at(i);
696 const RtpHeaderExtension ext2 = cd2->rtp_header_extensions().at(i);
697 EXPECT_EQ(ext1.uri, ext2.uri);
698 EXPECT_EQ(ext1.id, ext2.id);
699 }
700
701 // buffered mode latency
702 EXPECT_EQ(cd1->buffered_mode_latency(), cd2->buffered_mode_latency());
703 }
704
705
706 void CompareSessionDescription(const SessionDescription& desc1,
707 const SessionDescription& desc2) {
708 // Compare content descriptions.
709 if (desc1.contents().size() != desc2.contents().size()) {
710 ADD_FAILURE();
711 return;
712 }
713 for (size_t i = 0 ; i < desc1.contents().size(); ++i) {
714 const cricket::ContentInfo& c1 = desc1.contents().at(i);
715 const cricket::ContentInfo& c2 = desc2.contents().at(i);
716 // content name
717 EXPECT_EQ(c1.name, c2.name);
718 // content type
719 // Note, ASSERT will return from the function, but will not stop the test.
720 ASSERT_EQ(c1.type, c2.type);
721
722 ASSERT_EQ(IsAudioContent(&c1), IsAudioContent(&c2));
723 if (IsAudioContent(&c1)) {
724 const AudioContentDescription* acd1 =
725 static_cast<const AudioContentDescription*>(c1.description);
726 const AudioContentDescription* acd2 =
727 static_cast<const AudioContentDescription*>(c2.description);
728 CompareMediaContentDescription<AudioContentDescription>(acd1, acd2);
729 }
730
731 ASSERT_EQ(IsVideoContent(&c1), IsVideoContent(&c2));
732 if (IsVideoContent(&c1)) {
733 const VideoContentDescription* vcd1 =
734 static_cast<const VideoContentDescription*>(c1.description);
735 const VideoContentDescription* vcd2 =
736 static_cast<const VideoContentDescription*>(c2.description);
737 CompareMediaContentDescription<VideoContentDescription>(vcd1, vcd2);
738 }
739
740 ASSERT_EQ(IsDataContent(&c1), IsDataContent(&c2));
741 if (IsDataContent(&c1)) {
742 const DataContentDescription* dcd1 =
743 static_cast<const DataContentDescription*>(c1.description);
744 const DataContentDescription* dcd2 =
745 static_cast<const DataContentDescription*>(c2.description);
746 CompareMediaContentDescription<DataContentDescription>(dcd1, dcd2);
747 }
748 }
749
750 // group
751 const cricket::ContentGroups groups1 = desc1.groups();
752 const cricket::ContentGroups groups2 = desc2.groups();
753 EXPECT_EQ(groups1.size(), groups1.size());
754 if (groups1.size() != groups2.size()) {
755 ADD_FAILURE();
756 return;
757 }
758 for (size_t i = 0; i < groups1.size(); ++i) {
759 const cricket::ContentGroup group1 = groups1.at(i);
760 const cricket::ContentGroup group2 = groups2.at(i);
761 EXPECT_EQ(group1.semantics(), group2.semantics());
762 const cricket::ContentNames names1 = group1.content_names();
763 const cricket::ContentNames names2 = group2.content_names();
764 EXPECT_EQ(names1.size(), names2.size());
765 if (names1.size() != names2.size()) {
766 ADD_FAILURE();
767 return;
768 }
769 cricket::ContentNames::const_iterator iter1 = names1.begin();
770 cricket::ContentNames::const_iterator iter2 = names2.begin();
771 while (iter1 != names1.end()) {
772 EXPECT_EQ(*iter1++, *iter2++);
773 }
774 }
775
776 // transport info
777 const cricket::TransportInfos transports1 = desc1.transport_infos();
778 const cricket::TransportInfos transports2 = desc2.transport_infos();
779 EXPECT_EQ(transports1.size(), transports2.size());
780 if (transports1.size() != transports2.size()) {
781 ADD_FAILURE();
782 return;
783 }
784 for (size_t i = 0; i < transports1.size(); ++i) {
785 const cricket::TransportInfo transport1 = transports1.at(i);
786 const cricket::TransportInfo transport2 = transports2.at(i);
787 EXPECT_EQ(transport1.content_name, transport2.content_name);
788 EXPECT_EQ(transport1.description.transport_type,
789 transport2.description.transport_type);
790 EXPECT_EQ(transport1.description.ice_ufrag,
791 transport2.description.ice_ufrag);
792 EXPECT_EQ(transport1.description.ice_pwd,
793 transport2.description.ice_pwd);
794 if (transport1.description.identity_fingerprint) {
795 EXPECT_EQ(*transport1.description.identity_fingerprint,
796 *transport2.description.identity_fingerprint);
797 } else {
798 EXPECT_EQ(transport1.description.identity_fingerprint.get(),
799 transport2.description.identity_fingerprint.get());
800 }
801 EXPECT_EQ(transport1.description.transport_options,
802 transport2.description.transport_options);
803 EXPECT_TRUE(CompareCandidates(transport1.description.candidates,
804 transport2.description.candidates));
805 }
806 }
807
808 bool CompareCandidates(const Candidates& cs1, const Candidates& cs2) {
809 EXPECT_EQ(cs1.size(), cs2.size());
810 if (cs1.size() != cs2.size())
811 return false;
812 for (size_t i = 0; i< cs1.size(); ++i) {
813 const Candidate c1 = cs1.at(i);
814 const Candidate c2 = cs2.at(i);
815 EXPECT_TRUE(c1.IsEquivalent(c2));
816 }
817 return true;
818 }
819
820 bool CompareSessionDescription(
821 const JsepSessionDescription& desc1,
822 const JsepSessionDescription& desc2) {
823 EXPECT_EQ(desc1.session_id(), desc2.session_id());
824 EXPECT_EQ(desc1.session_version(), desc2.session_version());
825 CompareSessionDescription(*desc1.description(), *desc2.description());
826 if (desc1.number_of_mediasections() != desc2.number_of_mediasections())
827 return false;
828 for (size_t i = 0; i < desc1.number_of_mediasections(); ++i) {
829 const IceCandidateCollection* cc1 = desc1.candidates(i);
830 const IceCandidateCollection* cc2 = desc2.candidates(i);
831 if (cc1->count() != cc2->count())
832 return false;
833 for (size_t j = 0; j < cc1->count(); ++j) {
834 const IceCandidateInterface* c1 = cc1->at(j);
835 const IceCandidateInterface* c2 = cc2->at(j);
836 EXPECT_EQ(c1->sdp_mid(), c2->sdp_mid());
837 EXPECT_EQ(c1->sdp_mline_index(), c2->sdp_mline_index());
838 EXPECT_TRUE(c1->candidate().IsEquivalent(c2->candidate()));
839 }
840 }
841 return true;
842 }
843
844 // Disable the ice-ufrag and ice-pwd in given |sdp| message by replacing
845 // them with invalid keywords so that the parser will just ignore them.
846 bool RemoveCandidateUfragPwd(std::string* sdp) {
847 const char ice_ufrag[] = "a=ice-ufrag";
848 const char ice_ufragx[] = "a=xice-ufrag";
849 const char ice_pwd[] = "a=ice-pwd";
850 const char ice_pwdx[] = "a=xice-pwd";
851 talk_base::replace_substrs(ice_ufrag, strlen(ice_ufrag),
852 ice_ufragx, strlen(ice_ufragx), sdp);
853 talk_base::replace_substrs(ice_pwd, strlen(ice_pwd),
854 ice_pwdx, strlen(ice_pwdx), sdp);
855 return true;
856 }
857
858 // Update the candidates in |jdesc| to use the given |ufrag| and |pwd|.
859 bool UpdateCandidateUfragPwd(JsepSessionDescription* jdesc, int mline_index,
860 const std::string& ufrag, const std::string& pwd) {
861 std::string content_name;
862 if (mline_index == 0) {
863 content_name = kAudioContentName;
864 } else if (mline_index == 1) {
865 content_name = kVideoContentName;
866 } else {
867 ASSERT(false);
868 }
869 TransportInfo transport_info(
870 content_name, TransportDescription(NS_JINGLE_ICE_UDP,
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +0000871 ufrag, pwd));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000872 SessionDescription* desc =
873 const_cast<SessionDescription*>(jdesc->description());
874 desc->RemoveTransportInfoByName(content_name);
875 EXPECT_TRUE(desc->AddTransportInfo(transport_info));
876 for (size_t i = 0; i < jdesc_.number_of_mediasections(); ++i) {
877 const IceCandidateCollection* cc = jdesc_.candidates(i);
878 for (size_t j = 0; j < cc->count(); ++j) {
879 if (cc->at(j)->sdp_mline_index() == mline_index) {
880 const_cast<Candidate&>(cc->at(j)->candidate()).set_username(
881 ufrag);
882 const_cast<Candidate&>(cc->at(j)->candidate()).set_password(
883 pwd);
884 }
885 }
886 }
887 return true;
888 }
889
890 void AddIceOptions(const std::string& content_name,
891 const std::vector<std::string>& transport_options) {
892 ASSERT_TRUE(desc_.GetTransportInfoByName(content_name) != NULL);
893 cricket::TransportInfo transport_info =
894 *(desc_.GetTransportInfoByName(content_name));
895 desc_.RemoveTransportInfoByName(content_name);
896 transport_info.description.transport_options = transport_options;
897 desc_.AddTransportInfo(transport_info);
898 }
899
900 void AddFingerprint() {
901 desc_.RemoveTransportInfoByName(kAudioContentName);
902 desc_.RemoveTransportInfoByName(kVideoContentName);
903 talk_base::SSLFingerprint fingerprint(talk_base::DIGEST_SHA_1,
904 kIdentityDigest,
905 sizeof(kIdentityDigest));
906 EXPECT_TRUE(desc_.AddTransportInfo(
907 TransportInfo(kAudioContentName,
908 TransportDescription(NS_JINGLE_ICE_UDP,
909 std::vector<std::string>(),
910 kCandidateUfragVoice,
911 kCandidatePwdVoice,
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +0000912 cricket::ICEMODE_FULL,
913 cricket::CONNECTIONROLE_NONE,
914 &fingerprint, Candidates()))));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000915 EXPECT_TRUE(desc_.AddTransportInfo(
916 TransportInfo(kVideoContentName,
917 TransportDescription(NS_JINGLE_ICE_UDP,
918 std::vector<std::string>(),
919 kCandidateUfragVideo,
920 kCandidatePwdVideo,
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +0000921 cricket::ICEMODE_FULL,
922 cricket::CONNECTIONROLE_NONE,
923 &fingerprint, Candidates()))));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000924 }
925
926 void AddExtmap() {
927 audio_desc_ = static_cast<AudioContentDescription*>(
928 audio_desc_->Copy());
929 video_desc_ = static_cast<VideoContentDescription*>(
930 video_desc_->Copy());
931 audio_desc_->AddRtpHeaderExtension(
932 RtpHeaderExtension(kExtmapUri, kExtmapId));
933 video_desc_->AddRtpHeaderExtension(
934 RtpHeaderExtension(kExtmapUri, kExtmapId));
935 desc_.RemoveContentByName(kAudioContentName);
936 desc_.RemoveContentByName(kVideoContentName);
937 desc_.AddContent(kAudioContentName, NS_JINGLE_RTP, audio_desc_);
938 desc_.AddContent(kVideoContentName, NS_JINGLE_RTP, video_desc_);
939 }
940
941 void RemoveCryptos() {
942 audio_desc_->set_cryptos(std::vector<CryptoParams>());
943 video_desc_->set_cryptos(std::vector<CryptoParams>());
944 }
945
946 bool TestSerializeDirection(cricket::MediaContentDirection direction) {
947 audio_desc_->set_direction(direction);
948 video_desc_->set_direction(direction);
949 std::string new_sdp = kSdpFullString;
950 ReplaceDirection(direction, &new_sdp);
951
952 if (!jdesc_.Initialize(desc_.Copy(),
953 jdesc_.session_id(),
954 jdesc_.session_version())) {
955 return false;
956 }
957 std::string message = webrtc::SdpSerialize(jdesc_);
958 EXPECT_EQ(new_sdp, message);
959 return true;
960 }
961
962 bool TestSerializeRejected(bool audio_rejected, bool video_rejected) {
963 audio_desc_ = static_cast<AudioContentDescription*>(
964 audio_desc_->Copy());
965 video_desc_ = static_cast<VideoContentDescription*>(
966 video_desc_->Copy());
967 desc_.RemoveContentByName(kAudioContentName);
968 desc_.RemoveContentByName(kVideoContentName);
969 desc_.AddContent(kAudioContentName, NS_JINGLE_RTP, audio_rejected,
970 audio_desc_);
971 desc_.AddContent(kVideoContentName, NS_JINGLE_RTP, video_rejected,
972 video_desc_);
973 std::string new_sdp = kSdpFullString;
974 ReplaceRejected(audio_rejected, video_rejected, &new_sdp);
975
976 if (!jdesc_.Initialize(desc_.Copy(),
977 jdesc_.session_id(),
978 jdesc_.session_version())) {
979 return false;
980 }
981 std::string message = webrtc::SdpSerialize(jdesc_);
982 EXPECT_EQ(new_sdp, message);
983 return true;
984 }
985
986 void AddSctpDataChannel() {
987 talk_base::scoped_ptr<DataContentDescription> data(
988 new DataContentDescription());
989 data_desc_ = data.get();
990 data_desc_->set_protocol(cricket::kMediaProtocolDtlsSctp);
wu@webrtc.org78187522013-10-07 23:32:02 +0000991 DataCodec codec(cricket::kGoogleSctpDataCodecId,
992 cricket::kGoogleSctpDataCodecName, 0);
993 codec.SetParam(cricket::kCodecParamPort, kDefaultSctpPort);
994 data_desc_->AddCodec(codec);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000995 desc_.AddContent(kDataContentName, NS_JINGLE_DRAFT_SCTP, data.release());
996 EXPECT_TRUE(desc_.AddTransportInfo(
997 TransportInfo(kDataContentName,
998 TransportDescription(NS_JINGLE_ICE_UDP,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000999 kCandidateUfragData,
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001000 kCandidatePwdData))));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001001 }
1002
1003 void AddRtpDataChannel() {
1004 talk_base::scoped_ptr<DataContentDescription> data(
1005 new DataContentDescription());
1006 data_desc_ = data.get();
1007
1008 data_desc_->AddCodec(DataCodec(101, "google-data", 1));
1009 StreamParams data_stream;
1010 data_stream.id = kDataChannelMsid;
1011 data_stream.cname = kDataChannelCname;
1012 data_stream.sync_label = kDataChannelLabel;
1013 data_stream.ssrcs.push_back(kDataChannelSsrc);
1014 data_desc_->AddStream(data_stream);
1015 data_desc_->AddCrypto(CryptoParams(
1016 1, "AES_CM_128_HMAC_SHA1_80",
1017 "inline:FvLcvU2P3ZWmQxgPAgcDu7Zl9vftYElFOjEzhWs5", ""));
1018 data_desc_->set_protocol(cricket::kMediaProtocolSavpf);
1019 desc_.AddContent(kDataContentName, NS_JINGLE_RTP, data.release());
1020 EXPECT_TRUE(desc_.AddTransportInfo(
1021 TransportInfo(kDataContentName,
1022 TransportDescription(NS_JINGLE_ICE_UDP,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001023 kCandidateUfragData,
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001024 kCandidatePwdData))));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001025 }
1026
1027 bool TestDeserializeDirection(cricket::MediaContentDirection direction) {
1028 std::string new_sdp = kSdpFullString;
1029 ReplaceDirection(direction, &new_sdp);
1030 JsepSessionDescription new_jdesc(kDummyString);
1031
1032 EXPECT_TRUE(SdpDeserialize(new_sdp, &new_jdesc));
1033
1034 audio_desc_->set_direction(direction);
1035 video_desc_->set_direction(direction);
1036 if (!jdesc_.Initialize(desc_.Copy(),
1037 jdesc_.session_id(),
1038 jdesc_.session_version())) {
1039 return false;
1040 }
1041 EXPECT_TRUE(CompareSessionDescription(jdesc_, new_jdesc));
1042 return true;
1043 }
1044
1045 bool TestDeserializeRejected(bool audio_rejected, bool video_rejected) {
1046 std::string new_sdp = kSdpFullString;
1047 ReplaceRejected(audio_rejected, video_rejected, &new_sdp);
1048 JsepSessionDescription new_jdesc(JsepSessionDescription::kOffer);
1049
1050 EXPECT_TRUE(SdpDeserialize(new_sdp, &new_jdesc));
1051 audio_desc_ = static_cast<AudioContentDescription*>(
1052 audio_desc_->Copy());
1053 video_desc_ = static_cast<VideoContentDescription*>(
1054 video_desc_->Copy());
1055 desc_.RemoveContentByName(kAudioContentName);
1056 desc_.RemoveContentByName(kVideoContentName);
1057 desc_.AddContent(kAudioContentName, NS_JINGLE_RTP, audio_rejected,
1058 audio_desc_);
1059 desc_.AddContent(kVideoContentName, NS_JINGLE_RTP, video_rejected,
1060 video_desc_);
1061 if (!jdesc_.Initialize(desc_.Copy(),
1062 jdesc_.session_id(),
1063 jdesc_.session_version())) {
1064 return false;
1065 }
1066 EXPECT_TRUE(CompareSessionDescription(jdesc_, new_jdesc));
1067 return true;
1068 }
1069
1070 void TestDeserializeExtmap(bool session_level, bool media_level) {
1071 AddExtmap();
1072 JsepSessionDescription new_jdesc("dummy");
1073 ASSERT_TRUE(new_jdesc.Initialize(desc_.Copy(),
1074 jdesc_.session_id(),
1075 jdesc_.session_version()));
1076 JsepSessionDescription jdesc_with_extmap("dummy");
1077 std::string sdp_with_extmap = kSdpString;
1078 if (session_level) {
1079 InjectAfter(kSessionTime, kExtmapWithDirectionAndAttribute,
1080 &sdp_with_extmap);
1081 }
1082 if (media_level) {
1083 InjectAfter(kAttributeIcePwdVoice, kExtmapWithDirectionAndAttribute,
1084 &sdp_with_extmap);
1085 InjectAfter(kAttributeIcePwdVideo, kExtmapWithDirectionAndAttribute,
1086 &sdp_with_extmap);
1087 }
1088 // The extmap can't be present at the same time in both session level and
1089 // media level.
1090 if (session_level && media_level) {
1091 SdpParseError error;
1092 EXPECT_FALSE(webrtc::SdpDeserialize(sdp_with_extmap,
1093 &jdesc_with_extmap, &error));
1094 EXPECT_NE(std::string::npos, error.description.find("a=extmap"));
1095 } else {
1096 EXPECT_TRUE(SdpDeserialize(sdp_with_extmap, &jdesc_with_extmap));
1097 EXPECT_TRUE(CompareSessionDescription(jdesc_with_extmap, new_jdesc));
1098 }
1099 }
1100
1101 void VerifyCodecParameter(const cricket::CodecParameterMap& params,
1102 const std::string& name, int expected_value) {
1103 cricket::CodecParameterMap::const_iterator found = params.find(name);
1104 ASSERT_TRUE(found != params.end());
1105 EXPECT_EQ(found->second, talk_base::ToString<int>(expected_value));
1106 }
1107
1108 void TestDeserializeCodecParams(const CodecParams& params,
1109 JsepSessionDescription* jdesc_output) {
1110 std::string sdp =
1111 "v=0\r\n"
1112 "o=- 18446744069414584320 18446462598732840960 IN IP4 127.0.0.1\r\n"
1113 "s=-\r\n"
1114 "t=0 0\r\n"
1115 // Include semantics for WebRTC Media Streams since it is supported by
1116 // this parser, and will be added to the SDP when serializing a session
1117 // description.
1118 "a=msid-semantic: WMS\r\n"
1119 // Pl type 111 preferred.
1120 "m=audio 1 RTP/SAVPF 111 104 103 102\r\n"
1121 // Pltype 111 listed before 103 and 104 in the map.
1122 "a=rtpmap:111 opus/48000/2\r\n"
1123 // Pltype 103 listed before 104.
1124 "a=rtpmap:103 ISAC/16000\r\n"
1125 "a=rtpmap:104 CELT/32000/2\r\n"
1126 "a=rtpmap:102 ISAC/32000/1\r\n"
mallinath@webrtc.orga5506692013-08-12 21:18:15 +00001127 "a=fmtp:111 0-15,66,70\r\n"
1128 "a=fmtp:111 ";
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001129 std::ostringstream os;
mallinath@webrtc.orga5506692013-08-12 21:18:15 +00001130 os << "minptime=" << params.min_ptime
1131 << "; stereo=" << params.stereo
1132 << "; sprop-stereo=" << params.sprop_stereo
1133 << "; useinbandfec=" << params.useinband
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001134 << " maxaveragebitrate=" << params.maxaveragebitrate << "\r\n"
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001135 << "a=ptime:" << params.ptime << "\r\n"
1136 << "a=maxptime:" << params.max_ptime << "\r\n";
1137 sdp += os.str();
1138
1139 // Deserialize
1140 SdpParseError error;
1141 EXPECT_TRUE(webrtc::SdpDeserialize(sdp, jdesc_output, &error));
1142
1143 const ContentInfo* ac = GetFirstAudioContent(jdesc_output->description());
1144 ASSERT_TRUE(ac != NULL);
1145 const AudioContentDescription* acd =
1146 static_cast<const AudioContentDescription*>(ac->description);
1147 ASSERT_FALSE(acd->codecs().empty());
1148 cricket::AudioCodec opus = acd->codecs()[0];
1149 EXPECT_EQ("opus", opus.name);
1150 EXPECT_EQ(111, opus.id);
1151 VerifyCodecParameter(opus.params, "minptime", params.min_ptime);
1152 VerifyCodecParameter(opus.params, "stereo", params.stereo);
1153 VerifyCodecParameter(opus.params, "sprop-stereo", params.sprop_stereo);
1154 VerifyCodecParameter(opus.params, "useinbandfec", params.useinband);
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001155 VerifyCodecParameter(opus.params, "maxaveragebitrate",
1156 params.maxaveragebitrate);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001157 for (size_t i = 0; i < acd->codecs().size(); ++i) {
1158 cricket::AudioCodec codec = acd->codecs()[i];
1159 VerifyCodecParameter(codec.params, "ptime", params.ptime);
1160 VerifyCodecParameter(codec.params, "maxptime", params.max_ptime);
1161 if (codec.name == "ISAC") {
1162 if (codec.clockrate == 16000) {
1163 EXPECT_EQ(32000, codec.bitrate);
1164 } else {
1165 EXPECT_EQ(56000, codec.bitrate);
1166 }
1167 }
1168 }
1169 }
1170
1171 void TestDeserializeRtcpFb(JsepSessionDescription* jdesc_output,
1172 bool use_wildcard) {
1173 std::string sdp =
1174 "v=0\r\n"
1175 "o=- 18446744069414584320 18446462598732840960 IN IP4 127.0.0.1\r\n"
1176 "s=-\r\n"
1177 "t=0 0\r\n"
1178 // Include semantics for WebRTC Media Streams since it is supported by
1179 // this parser, and will be added to the SDP when serializing a session
1180 // description.
1181 "a=msid-semantic: WMS\r\n"
1182 "m=audio 1 RTP/SAVPF 111\r\n"
1183 "a=rtpmap:111 opus/48000/2\r\n"
1184 "a=rtcp-fb:111 nack\r\n"
1185 "m=video 3457 RTP/SAVPF 101\r\n"
1186 "a=rtpmap:101 VP8/90000\r\n"
1187 "a=rtcp-fb:101 nack\r\n"
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +00001188 "a=rtcp-fb:101 nack pli\r\n"
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001189 "a=rtcp-fb:101 goog-remb\r\n"
1190 "a=rtcp-fb:101 ccm fir\r\n";
1191 std::ostringstream os;
1192 os << "a=rtcp-fb:" << (use_wildcard ? "*" : "101") << " ccm fir\r\n";
1193 sdp += os.str();
1194 // Deserialize
1195 SdpParseError error;
1196 EXPECT_TRUE(webrtc::SdpDeserialize(sdp, jdesc_output, &error));
1197 const ContentInfo* ac = GetFirstAudioContent(jdesc_output->description());
1198 ASSERT_TRUE(ac != NULL);
1199 const AudioContentDescription* acd =
1200 static_cast<const AudioContentDescription*>(ac->description);
1201 ASSERT_FALSE(acd->codecs().empty());
1202 cricket::AudioCodec opus = acd->codecs()[0];
1203 EXPECT_EQ(111, opus.id);
1204 EXPECT_TRUE(opus.HasFeedbackParam(
1205 cricket::FeedbackParam(cricket::kRtcpFbParamNack,
1206 cricket::kParamValueEmpty)));
1207
1208 const ContentInfo* vc = GetFirstVideoContent(jdesc_output->description());
1209 ASSERT_TRUE(vc != NULL);
1210 const VideoContentDescription* vcd =
1211 static_cast<const VideoContentDescription*>(vc->description);
1212 ASSERT_FALSE(vcd->codecs().empty());
1213 cricket::VideoCodec vp8 = vcd->codecs()[0];
1214 EXPECT_STREQ(webrtc::JsepSessionDescription::kDefaultVideoCodecName,
1215 vp8.name.c_str());
1216 EXPECT_EQ(101, vp8.id);
1217 EXPECT_TRUE(vp8.HasFeedbackParam(
1218 cricket::FeedbackParam(cricket::kRtcpFbParamNack,
1219 cricket::kParamValueEmpty)));
1220 EXPECT_TRUE(vp8.HasFeedbackParam(
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +00001221 cricket::FeedbackParam(cricket::kRtcpFbParamNack,
1222 cricket::kRtcpFbNackParamPli)));
1223 EXPECT_TRUE(vp8.HasFeedbackParam(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001224 cricket::FeedbackParam(cricket::kRtcpFbParamRemb,
1225 cricket::kParamValueEmpty)));
1226 EXPECT_TRUE(vp8.HasFeedbackParam(
1227 cricket::FeedbackParam(cricket::kRtcpFbParamCcm,
1228 cricket::kRtcpFbCcmParamFir)));
1229 }
1230
1231 // Two SDP messages can mean the same thing but be different strings, e.g.
1232 // some of the lines can be serialized in different order.
1233 // However, a deserialized description can be compared field by field and has
1234 // no order. If deserializer has already been tested, serializing then
1235 // deserializing and comparing JsepSessionDescription will test
1236 // the serializer sufficiently.
1237 void TestSerialize(const JsepSessionDescription& jdesc) {
1238 std::string message = webrtc::SdpSerialize(jdesc);
1239 JsepSessionDescription jdesc_output_des(kDummyString);
1240 SdpParseError error;
1241 EXPECT_TRUE(webrtc::SdpDeserialize(message, &jdesc_output_des, &error));
1242 EXPECT_TRUE(CompareSessionDescription(jdesc, jdesc_output_des));
1243 }
1244
1245 protected:
1246 SessionDescription desc_;
1247 AudioContentDescription* audio_desc_;
1248 VideoContentDescription* video_desc_;
1249 DataContentDescription* data_desc_;
1250 Candidates candidates_;
1251 talk_base::scoped_ptr<IceCandidateInterface> jcandidate_;
1252 JsepSessionDescription jdesc_;
1253};
1254
1255void TestMismatch(const std::string& string1, const std::string& string2) {
1256 int position = 0;
1257 for (size_t i = 0; i < string1.length() && i < string2.length(); ++i) {
1258 if (string1.c_str()[i] != string2.c_str()[i]) {
henrike@webrtc.org28654cb2013-07-22 21:07:49 +00001259 position = static_cast<int>(i);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001260 break;
1261 }
1262 }
1263 EXPECT_EQ(0, position) << "Strings mismatch at the " << position
1264 << " character\n"
1265 << " 1: " << string1.substr(position, 20) << "\n"
1266 << " 2: " << string2.substr(position, 20) << "\n";
1267}
1268
1269std::string GetLine(const std::string& message,
1270 const std::string& session_description_name) {
1271 size_t start = message.find(session_description_name);
1272 if (std::string::npos == start) {
1273 return "";
1274 }
1275 size_t stop = message.find("\r\n", start);
1276 if (std::string::npos == stop) {
1277 return "";
1278 }
1279 if (stop <= start) {
1280 return "";
1281 }
1282 return message.substr(start, stop - start);
1283}
1284
1285TEST_F(WebRtcSdpTest, SerializeSessionDescription) {
1286 // SessionDescription with desc and candidates.
1287 std::string message = webrtc::SdpSerialize(jdesc_);
1288 TestMismatch(std::string(kSdpFullString), message);
1289}
1290
1291TEST_F(WebRtcSdpTest, SerializeSessionDescriptionEmpty) {
1292 JsepSessionDescription jdesc_empty(kDummyString);
1293 EXPECT_EQ("", webrtc::SdpSerialize(jdesc_empty));
1294}
1295
1296// This tests serialization of SDP with a=crypto and a=fingerprint, as would be
1297// the case in a DTLS offer.
1298TEST_F(WebRtcSdpTest, SerializeSessionDescriptionWithFingerprint) {
1299 AddFingerprint();
1300 JsepSessionDescription jdesc_with_fingerprint(kDummyString);
1301 ASSERT_TRUE(jdesc_with_fingerprint.Initialize(desc_.Copy(),
1302 kSessionId, kSessionVersion));
1303 std::string message = webrtc::SdpSerialize(jdesc_with_fingerprint);
1304
1305 std::string sdp_with_fingerprint = kSdpString;
1306 InjectAfter(kAttributeIcePwdVoice,
1307 kFingerprint, &sdp_with_fingerprint);
1308 InjectAfter(kAttributeIcePwdVideo,
1309 kFingerprint, &sdp_with_fingerprint);
1310
1311 EXPECT_EQ(sdp_with_fingerprint, message);
1312}
1313
1314// This tests serialization of SDP with a=fingerprint with no a=crypto, as would
1315// be the case in a DTLS answer.
1316TEST_F(WebRtcSdpTest, SerializeSessionDescriptionWithFingerprintNoCryptos) {
1317 AddFingerprint();
1318 RemoveCryptos();
1319 JsepSessionDescription jdesc_with_fingerprint(kDummyString);
1320 ASSERT_TRUE(jdesc_with_fingerprint.Initialize(desc_.Copy(),
1321 kSessionId, kSessionVersion));
1322 std::string message = webrtc::SdpSerialize(jdesc_with_fingerprint);
1323
1324 std::string sdp_with_fingerprint = kSdpString;
1325 Replace(kAttributeCryptoVoice, "", &sdp_with_fingerprint);
1326 Replace(kAttributeCryptoVideo, "", &sdp_with_fingerprint);
1327 InjectAfter(kAttributeIcePwdVoice,
1328 kFingerprint, &sdp_with_fingerprint);
1329 InjectAfter(kAttributeIcePwdVideo,
1330 kFingerprint, &sdp_with_fingerprint);
1331
1332 EXPECT_EQ(sdp_with_fingerprint, message);
1333}
1334
1335TEST_F(WebRtcSdpTest, SerializeSessionDescriptionWithoutCandidates) {
1336 // JsepSessionDescription with desc but without candidates.
1337 JsepSessionDescription jdesc_no_candidates(kDummyString);
1338 ASSERT_TRUE(jdesc_no_candidates.Initialize(desc_.Copy(),
1339 kSessionId, kSessionVersion));
1340 std::string message = webrtc::SdpSerialize(jdesc_no_candidates);
1341 EXPECT_EQ(std::string(kSdpString), message);
1342}
1343
1344TEST_F(WebRtcSdpTest, SerializeSessionDescriptionWithBundle) {
1345 ContentGroup group(cricket::GROUP_TYPE_BUNDLE);
1346 group.AddContentName(kAudioContentName);
1347 group.AddContentName(kVideoContentName);
1348 desc_.AddGroup(group);
1349 ASSERT_TRUE(jdesc_.Initialize(desc_.Copy(),
1350 jdesc_.session_id(),
1351 jdesc_.session_version()));
1352 std::string message = webrtc::SdpSerialize(jdesc_);
1353 std::string sdp_with_bundle = kSdpFullString;
1354 InjectAfter(kSessionTime,
1355 "a=group:BUNDLE audio_content_name video_content_name\r\n",
1356 &sdp_with_bundle);
1357 EXPECT_EQ(sdp_with_bundle, message);
1358}
1359
1360TEST_F(WebRtcSdpTest, SerializeSessionDescriptionWithBandwidth) {
1361 VideoContentDescription* vcd = static_cast<VideoContentDescription*>(
1362 GetFirstVideoContent(&desc_)->description);
1363 vcd->set_bandwidth(100 * 1000);
1364 AudioContentDescription* acd = static_cast<AudioContentDescription*>(
1365 GetFirstAudioContent(&desc_)->description);
1366 acd->set_bandwidth(50 * 1000);
1367 ASSERT_TRUE(jdesc_.Initialize(desc_.Copy(),
1368 jdesc_.session_id(),
1369 jdesc_.session_version()));
1370 std::string message = webrtc::SdpSerialize(jdesc_);
1371 std::string sdp_with_bandwidth = kSdpFullString;
1372 InjectAfter("a=mid:video_content_name\r\na=sendrecv\r\n",
1373 "b=AS:100\r\n",
1374 &sdp_with_bandwidth);
1375 InjectAfter("a=mid:audio_content_name\r\na=sendrecv\r\n",
1376 "b=AS:50\r\n",
1377 &sdp_with_bandwidth);
1378 EXPECT_EQ(sdp_with_bandwidth, message);
1379}
1380
1381TEST_F(WebRtcSdpTest, SerializeSessionDescriptionWithIceOptions) {
1382 std::vector<std::string> transport_options;
1383 transport_options.push_back(kIceOption1);
1384 transport_options.push_back(kIceOption3);
1385 AddIceOptions(kAudioContentName, transport_options);
1386 transport_options.clear();
1387 transport_options.push_back(kIceOption2);
1388 transport_options.push_back(kIceOption3);
1389 AddIceOptions(kVideoContentName, transport_options);
1390 ASSERT_TRUE(jdesc_.Initialize(desc_.Copy(),
1391 jdesc_.session_id(),
1392 jdesc_.session_version()));
1393 std::string message = webrtc::SdpSerialize(jdesc_);
1394 std::string sdp_with_ice_options = kSdpFullString;
1395 InjectAfter(kAttributeIcePwdVoice,
1396 "a=ice-options:iceoption1 iceoption3\r\n",
1397 &sdp_with_ice_options);
1398 InjectAfter(kAttributeIcePwdVideo,
1399 "a=ice-options:iceoption2 iceoption3\r\n",
1400 &sdp_with_ice_options);
1401 EXPECT_EQ(sdp_with_ice_options, message);
1402}
1403
1404TEST_F(WebRtcSdpTest, SerializeSessionDescriptionWithRecvOnlyContent) {
1405 EXPECT_TRUE(TestSerializeDirection(cricket::MD_RECVONLY));
1406}
1407
1408TEST_F(WebRtcSdpTest, SerializeSessionDescriptionWithSendOnlyContent) {
1409 EXPECT_TRUE(TestSerializeDirection(cricket::MD_SENDONLY));
1410}
1411
1412TEST_F(WebRtcSdpTest, SerializeSessionDescriptionWithInactiveContent) {
1413 EXPECT_TRUE(TestSerializeDirection(cricket::MD_INACTIVE));
1414}
1415
1416TEST_F(WebRtcSdpTest, SerializeSessionDescriptionWithAudioRejected) {
1417 EXPECT_TRUE(TestSerializeRejected(true, false));
1418}
1419
1420TEST_F(WebRtcSdpTest, SerializeSessionDescriptionWithVideoRejected) {
1421 EXPECT_TRUE(TestSerializeRejected(false, true));
1422}
1423
1424TEST_F(WebRtcSdpTest, SerializeSessionDescriptionWithAudioVideoRejected) {
1425 EXPECT_TRUE(TestSerializeRejected(true, true));
1426}
1427
1428TEST_F(WebRtcSdpTest, SerializeSessionDescriptionWithRtpDataChannel) {
1429 AddRtpDataChannel();
1430 JsepSessionDescription jsep_desc(kDummyString);
1431
1432 ASSERT_TRUE(jsep_desc.Initialize(desc_.Copy(), kSessionId, kSessionVersion));
1433 std::string message = webrtc::SdpSerialize(jsep_desc);
1434
1435 std::string expected_sdp = kSdpString;
1436 expected_sdp.append(kSdpRtpDataChannelString);
1437 EXPECT_EQ(expected_sdp, message);
1438}
1439
1440TEST_F(WebRtcSdpTest, SerializeSessionDescriptionWithSctpDataChannel) {
1441 AddSctpDataChannel();
1442 JsepSessionDescription jsep_desc(kDummyString);
1443
1444 ASSERT_TRUE(jsep_desc.Initialize(desc_.Copy(), kSessionId, kSessionVersion));
1445 std::string message = webrtc::SdpSerialize(jsep_desc);
1446
1447 std::string expected_sdp = kSdpString;
1448 expected_sdp.append(kSdpSctpDataChannelString);
1449 EXPECT_EQ(message, expected_sdp);
1450}
1451
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001452TEST_F(WebRtcSdpTest, SerializeSessionDescriptionWithDataChannelAndBandwidth) {
1453 AddRtpDataChannel();
1454 data_desc_->set_bandwidth(100*1000);
1455 JsepSessionDescription jsep_desc(kDummyString);
1456
1457 ASSERT_TRUE(jsep_desc.Initialize(desc_.Copy(), kSessionId, kSessionVersion));
1458 std::string message = webrtc::SdpSerialize(jsep_desc);
1459
1460 std::string expected_sdp = kSdpString;
1461 expected_sdp.append(kSdpRtpDataChannelString);
1462 // We want to test that serializing data content ignores bandwidth
1463 // settings (it should always be the default). Thus, we don't do
1464 // the following:
sergeyu@chromium.orga59696b2013-09-13 23:48:58 +00001465 // TODO(pthatcher): We need to temporarily allow the SDP to control
1466 // this for backwards-compatibility. Once we don't need that any
1467 // more, remove this.
1468 InjectAfter("a=mid:data_content_name\r\na=sendrecv\r\n",
1469 "b=AS:100\r\n",
1470 &expected_sdp);
1471 EXPECT_EQ(expected_sdp, message);
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001472}
1473
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001474TEST_F(WebRtcSdpTest, SerializeSessionDescriptionWithExtmap) {
1475 AddExtmap();
1476 JsepSessionDescription desc_with_extmap("dummy");
1477 ASSERT_TRUE(desc_with_extmap.Initialize(desc_.Copy(),
1478 kSessionId, kSessionVersion));
1479 std::string message = webrtc::SdpSerialize(desc_with_extmap);
1480
1481 std::string sdp_with_extmap = kSdpString;
1482 InjectAfter("a=mid:audio_content_name\r\n",
1483 kExtmap, &sdp_with_extmap);
1484 InjectAfter("a=mid:video_content_name\r\n",
1485 kExtmap, &sdp_with_extmap);
1486
1487 EXPECT_EQ(sdp_with_extmap, message);
1488}
1489
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +00001490TEST_F(WebRtcSdpTest, SerializeSessionDescriptionWithBufferLatency) {
1491 VideoContentDescription* vcd = static_cast<VideoContentDescription*>(
1492 GetFirstVideoContent(&desc_)->description);
1493 vcd->set_buffered_mode_latency(128);
1494
1495 ASSERT_TRUE(jdesc_.Initialize(desc_.Copy(),
1496 jdesc_.session_id(),
1497 jdesc_.session_version()));
1498 std::string message = webrtc::SdpSerialize(jdesc_);
1499 std::string sdp_with_buffer_latency = kSdpFullString;
1500 InjectAfter("a=rtpmap:120 VP8/90000\r\n",
1501 "a=x-google-buffer-latency:128\r\n",
1502 &sdp_with_buffer_latency);
1503 EXPECT_EQ(sdp_with_buffer_latency, message);
1504}
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001505
1506TEST_F(WebRtcSdpTest, SerializeCandidates) {
1507 std::string message = webrtc::SdpSerializeCandidate(*jcandidate_);
1508 EXPECT_EQ(std::string(kSdpOneCandidate), message);
1509}
1510
1511TEST_F(WebRtcSdpTest, DeserializeSessionDescription) {
1512 JsepSessionDescription jdesc(kDummyString);
1513 // Deserialize
1514 EXPECT_TRUE(SdpDeserialize(kSdpFullString, &jdesc));
1515 // Verify
1516 EXPECT_TRUE(CompareSessionDescription(jdesc_, jdesc));
1517}
1518
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001519TEST_F(WebRtcSdpTest, DeserializeSessionDescriptionWithoutMline) {
1520 JsepSessionDescription jdesc(kDummyString);
1521 const char kSdpWithoutMline[] =
1522 "v=0\r\n"
1523 "o=- 18446744069414584320 18446462598732840960 IN IP4 127.0.0.1\r\n"
1524 "s=-\r\n"
1525 "t=0 0\r\n"
1526 "a=msid-semantic: WMS local_stream_1 local_stream_2\r\n";
1527 // Deserialize
1528 EXPECT_TRUE(SdpDeserialize(kSdpWithoutMline, &jdesc));
1529 EXPECT_EQ(0u, jdesc.description()->contents().size());
1530}
1531
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001532TEST_F(WebRtcSdpTest, DeserializeSessionDescriptionWithoutCarriageReturn) {
1533 JsepSessionDescription jdesc(kDummyString);
1534 std::string sdp_without_carriage_return = kSdpFullString;
1535 Replace("\r\n", "\n", &sdp_without_carriage_return);
1536 // Deserialize
1537 EXPECT_TRUE(SdpDeserialize(sdp_without_carriage_return, &jdesc));
1538 // Verify
1539 EXPECT_TRUE(CompareSessionDescription(jdesc_, jdesc));
1540}
1541
1542TEST_F(WebRtcSdpTest, DeserializeSessionDescriptionWithoutCandidates) {
1543 // SessionDescription with desc but without candidates.
1544 JsepSessionDescription jdesc_no_candidates(kDummyString);
1545 ASSERT_TRUE(jdesc_no_candidates.Initialize(desc_.Copy(),
1546 kSessionId, kSessionVersion));
1547 JsepSessionDescription new_jdesc(kDummyString);
1548 EXPECT_TRUE(SdpDeserialize(kSdpString, &new_jdesc));
1549 EXPECT_TRUE(CompareSessionDescription(jdesc_no_candidates, new_jdesc));
1550}
1551
1552TEST_F(WebRtcSdpTest, DeserializeSessionDescriptionWithoutRtpmap) {
1553 static const char kSdpNoRtpmapString[] =
1554 "v=0\r\n"
1555 "o=- 11 22 IN IP4 127.0.0.1\r\n"
1556 "s=-\r\n"
1557 "t=0 0\r\n"
1558 "m=audio 49232 RTP/AVP 0 18 103\r\n"
1559 // Codec that doesn't appear in the m= line will be ignored.
1560 "a=rtpmap:104 CELT/32000/2\r\n"
1561 // The rtpmap line for static payload codec is optional.
1562 "a=rtpmap:18 G729/16000\r\n"
1563 "a=rtpmap:103 ISAC/16000\r\n";
1564
1565 JsepSessionDescription jdesc(kDummyString);
1566 EXPECT_TRUE(SdpDeserialize(kSdpNoRtpmapString, &jdesc));
1567 cricket::AudioContentDescription* audio =
1568 static_cast<AudioContentDescription*>(
1569 jdesc.description()->GetContentDescriptionByName(cricket::CN_AUDIO));
1570 AudioCodecs ref_codecs;
1571 // The codecs in the AudioContentDescription will be sorted by preference.
1572 ref_codecs.push_back(AudioCodec(0, "PCMU", 8000, 0, 1, 3));
1573 ref_codecs.push_back(AudioCodec(18, "G729", 16000, 0, 1, 2));
1574 ref_codecs.push_back(AudioCodec(103, "ISAC", 16000, 32000, 1, 1));
1575 EXPECT_EQ(ref_codecs, audio->codecs());
1576}
1577
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +00001578TEST_F(WebRtcSdpTest, DeserializeSessionDescriptionWithoutRtpmapButWithFmtp) {
1579 static const char kSdpNoRtpmapString[] =
1580 "v=0\r\n"
1581 "o=- 11 22 IN IP4 127.0.0.1\r\n"
1582 "s=-\r\n"
1583 "t=0 0\r\n"
1584 "m=audio 49232 RTP/AVP 18 103\r\n"
1585 "a=fmtp:18 annexb=yes\r\n"
1586 "a=rtpmap:103 ISAC/16000\r\n";
1587
1588 JsepSessionDescription jdesc(kDummyString);
1589 EXPECT_TRUE(SdpDeserialize(kSdpNoRtpmapString, &jdesc));
1590 cricket::AudioContentDescription* audio =
1591 static_cast<AudioContentDescription*>(
1592 jdesc.description()->GetContentDescriptionByName(cricket::CN_AUDIO));
1593
1594 cricket::AudioCodec g729 = audio->codecs()[0];
1595 EXPECT_EQ("G729", g729.name);
1596 EXPECT_EQ(8000, g729.clockrate);
1597 EXPECT_EQ(18, g729.id);
1598 cricket::CodecParameterMap::iterator found =
1599 g729.params.find("annexb");
1600 ASSERT_TRUE(found != g729.params.end());
1601 EXPECT_EQ(found->second, "yes");
1602
1603 cricket::AudioCodec isac = audio->codecs()[1];
1604 EXPECT_EQ("ISAC", isac.name);
1605 EXPECT_EQ(103, isac.id);
1606 EXPECT_EQ(16000, isac.clockrate);
1607}
1608
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001609// Ensure that we can deserialize SDP with a=fingerprint properly.
1610TEST_F(WebRtcSdpTest, DeserializeJsepSessionDescriptionWithFingerprint) {
1611 // Add a DTLS a=fingerprint attribute to our session description.
1612 AddFingerprint();
1613 JsepSessionDescription new_jdesc(kDummyString);
1614 ASSERT_TRUE(new_jdesc.Initialize(desc_.Copy(),
1615 jdesc_.session_id(),
1616 jdesc_.session_version()));
1617
1618 JsepSessionDescription jdesc_with_fingerprint(kDummyString);
1619 std::string sdp_with_fingerprint = kSdpString;
1620 InjectAfter(kAttributeIcePwdVoice, kFingerprint, &sdp_with_fingerprint);
1621 InjectAfter(kAttributeIcePwdVideo, kFingerprint, &sdp_with_fingerprint);
1622 EXPECT_TRUE(SdpDeserialize(sdp_with_fingerprint, &jdesc_with_fingerprint));
1623 EXPECT_TRUE(CompareSessionDescription(jdesc_with_fingerprint, new_jdesc));
1624}
1625
1626TEST_F(WebRtcSdpTest, DeserializeSessionDescriptionWithBundle) {
1627 JsepSessionDescription jdesc_with_bundle(kDummyString);
1628 std::string sdp_with_bundle = kSdpFullString;
1629 InjectAfter(kSessionTime,
1630 "a=group:BUNDLE audio_content_name video_content_name\r\n",
1631 &sdp_with_bundle);
1632 EXPECT_TRUE(SdpDeserialize(sdp_with_bundle, &jdesc_with_bundle));
1633 ContentGroup group(cricket::GROUP_TYPE_BUNDLE);
1634 group.AddContentName(kAudioContentName);
1635 group.AddContentName(kVideoContentName);
1636 desc_.AddGroup(group);
1637 ASSERT_TRUE(jdesc_.Initialize(desc_.Copy(),
1638 jdesc_.session_id(),
1639 jdesc_.session_version()));
1640 EXPECT_TRUE(CompareSessionDescription(jdesc_, jdesc_with_bundle));
1641}
1642
1643TEST_F(WebRtcSdpTest, DeserializeSessionDescriptionWithBandwidth) {
1644 JsepSessionDescription jdesc_with_bandwidth(kDummyString);
1645 std::string sdp_with_bandwidth = kSdpFullString;
1646 InjectAfter("a=mid:video_content_name\r\na=sendrecv\r\n",
1647 "b=AS:100\r\n",
1648 &sdp_with_bandwidth);
1649 InjectAfter("a=mid:audio_content_name\r\na=sendrecv\r\n",
1650 "b=AS:50\r\n",
1651 &sdp_with_bandwidth);
1652 EXPECT_TRUE(
1653 SdpDeserialize(sdp_with_bandwidth, &jdesc_with_bandwidth));
1654 VideoContentDescription* vcd = static_cast<VideoContentDescription*>(
1655 GetFirstVideoContent(&desc_)->description);
1656 vcd->set_bandwidth(100 * 1000);
1657 AudioContentDescription* acd = static_cast<AudioContentDescription*>(
1658 GetFirstAudioContent(&desc_)->description);
1659 acd->set_bandwidth(50 * 1000);
1660 ASSERT_TRUE(jdesc_.Initialize(desc_.Copy(),
1661 jdesc_.session_id(),
1662 jdesc_.session_version()));
1663 EXPECT_TRUE(CompareSessionDescription(jdesc_, jdesc_with_bandwidth));
1664}
1665
1666TEST_F(WebRtcSdpTest, DeserializeSessionDescriptionWithIceOptions) {
1667 JsepSessionDescription jdesc_with_ice_options(kDummyString);
1668 std::string sdp_with_ice_options = kSdpFullString;
1669 InjectAfter(kSessionTime,
1670 "a=ice-options:iceoption3\r\n",
1671 &sdp_with_ice_options);
1672 InjectAfter(kAttributeIcePwdVoice,
1673 "a=ice-options:iceoption1\r\n",
1674 &sdp_with_ice_options);
1675 InjectAfter(kAttributeIcePwdVideo,
1676 "a=ice-options:iceoption2\r\n",
1677 &sdp_with_ice_options);
1678 EXPECT_TRUE(SdpDeserialize(sdp_with_ice_options, &jdesc_with_ice_options));
1679 std::vector<std::string> transport_options;
1680 transport_options.push_back(kIceOption3);
1681 transport_options.push_back(kIceOption1);
1682 AddIceOptions(kAudioContentName, transport_options);
1683 transport_options.clear();
1684 transport_options.push_back(kIceOption3);
1685 transport_options.push_back(kIceOption2);
1686 AddIceOptions(kVideoContentName, transport_options);
1687 ASSERT_TRUE(jdesc_.Initialize(desc_.Copy(),
1688 jdesc_.session_id(),
1689 jdesc_.session_version()));
1690 EXPECT_TRUE(CompareSessionDescription(jdesc_, jdesc_with_ice_options));
1691}
1692
1693TEST_F(WebRtcSdpTest, DeserializeSessionDescriptionWithUfragPwd) {
1694 // Remove the original ice-ufrag and ice-pwd
1695 JsepSessionDescription jdesc_with_ufrag_pwd(kDummyString);
1696 std::string sdp_with_ufrag_pwd = kSdpFullString;
1697 EXPECT_TRUE(RemoveCandidateUfragPwd(&sdp_with_ufrag_pwd));
1698 // Add session level ufrag and pwd
1699 InjectAfter(kSessionTime,
1700 "a=ice-pwd:session+level+icepwd\r\n"
1701 "a=ice-ufrag:session+level+iceufrag\r\n",
1702 &sdp_with_ufrag_pwd);
1703 // Add media level ufrag and pwd for audio
1704 InjectAfter("a=mid:audio_content_name\r\n",
1705 "a=ice-pwd:media+level+icepwd\r\na=ice-ufrag:media+level+iceufrag\r\n",
1706 &sdp_with_ufrag_pwd);
1707 // Update the candidate ufrag and pwd to the expected ones.
1708 EXPECT_TRUE(UpdateCandidateUfragPwd(&jdesc_, 0,
1709 "media+level+iceufrag", "media+level+icepwd"));
1710 EXPECT_TRUE(UpdateCandidateUfragPwd(&jdesc_, 1,
1711 "session+level+iceufrag", "session+level+icepwd"));
1712 EXPECT_TRUE(SdpDeserialize(sdp_with_ufrag_pwd, &jdesc_with_ufrag_pwd));
1713 EXPECT_TRUE(CompareSessionDescription(jdesc_, jdesc_with_ufrag_pwd));
1714}
1715
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +00001716TEST_F(WebRtcSdpTest, DeserializeSessionDescriptionWithBufferLatency) {
1717 JsepSessionDescription jdesc_with_buffer_latency(kDummyString);
1718 std::string sdp_with_buffer_latency = kSdpFullString;
1719 InjectAfter("a=rtpmap:120 VP8/90000\r\n",
1720 "a=x-google-buffer-latency:128\r\n",
1721 &sdp_with_buffer_latency);
1722
1723 EXPECT_TRUE(
1724 SdpDeserialize(sdp_with_buffer_latency, &jdesc_with_buffer_latency));
1725 VideoContentDescription* vcd = static_cast<VideoContentDescription*>(
1726 GetFirstVideoContent(&desc_)->description);
1727 vcd->set_buffered_mode_latency(128);
1728 ASSERT_TRUE(jdesc_.Initialize(desc_.Copy(),
1729 jdesc_.session_id(),
1730 jdesc_.session_version()));
1731 EXPECT_TRUE(CompareSessionDescription(jdesc_, jdesc_with_buffer_latency));
1732}
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001733
1734TEST_F(WebRtcSdpTest, DeserializeSessionDescriptionWithRecvOnlyContent) {
1735 EXPECT_TRUE(TestDeserializeDirection(cricket::MD_RECVONLY));
1736}
1737
1738TEST_F(WebRtcSdpTest, DeserializeSessionDescriptionWithSendOnlyContent) {
1739 EXPECT_TRUE(TestDeserializeDirection(cricket::MD_SENDONLY));
1740}
1741
1742TEST_F(WebRtcSdpTest, DeserializeSessionDescriptionWithInactiveContent) {
1743 EXPECT_TRUE(TestDeserializeDirection(cricket::MD_INACTIVE));
1744}
1745
1746TEST_F(WebRtcSdpTest, DeserializeSessionDescriptionWithRejectedAudio) {
1747 EXPECT_TRUE(TestDeserializeRejected(true, false));
1748}
1749
1750TEST_F(WebRtcSdpTest, DeserializeSessionDescriptionWithRejectedVideo) {
1751 EXPECT_TRUE(TestDeserializeRejected(false, true));
1752}
1753
1754TEST_F(WebRtcSdpTest, DeserializeSessionDescriptionWithRejectedAudioVideo) {
1755 EXPECT_TRUE(TestDeserializeRejected(true, true));
1756}
1757
1758// Tests that we can still handle the sdp uses mslabel and label instead of
1759// msid for backward compatibility.
1760TEST_F(WebRtcSdpTest, DeserializeSessionDescriptionWithoutMsid) {
1761 JsepSessionDescription jdesc(kDummyString);
1762 std::string sdp_without_msid = kSdpFullString;
1763 Replace("msid", "xmsid", &sdp_without_msid);
1764 // Deserialize
1765 EXPECT_TRUE(SdpDeserialize(sdp_without_msid, &jdesc));
1766 // Verify
1767 EXPECT_TRUE(CompareSessionDescription(jdesc_, jdesc));
1768}
1769
1770TEST_F(WebRtcSdpTest, DeserializeCandidate) {
1771 JsepIceCandidate jcandidate(kDummyMid, kDummyIndex);
1772
1773 std::string sdp = kSdpOneCandidate;
1774 EXPECT_TRUE(SdpDeserializeCandidate(sdp, &jcandidate));
1775 EXPECT_EQ(kDummyMid, jcandidate.sdp_mid());
1776 EXPECT_EQ(kDummyIndex, jcandidate.sdp_mline_index());
1777 EXPECT_TRUE(jcandidate.candidate().IsEquivalent(jcandidate_->candidate()));
1778
1779 // Candidate line without generation extension.
1780 sdp = kSdpOneCandidate;
1781 Replace(" generation 2", "", &sdp);
1782 EXPECT_TRUE(SdpDeserializeCandidate(sdp, &jcandidate));
1783 EXPECT_EQ(kDummyMid, jcandidate.sdp_mid());
1784 EXPECT_EQ(kDummyIndex, jcandidate.sdp_mline_index());
1785 Candidate expected = jcandidate_->candidate();
1786 expected.set_generation(0);
1787 EXPECT_TRUE(jcandidate.candidate().IsEquivalent(expected));
1788
1789 // Multiple candidate lines.
1790 // Only the first line will be deserialized. The rest will be ignored.
1791 sdp = kSdpOneCandidate;
1792 sdp.append("a=candidate:1 2 tcp 1234 192.168.1.100 5678 typ host\r\n");
1793 EXPECT_TRUE(SdpDeserializeCandidate(sdp, &jcandidate));
1794 EXPECT_EQ(kDummyMid, jcandidate.sdp_mid());
1795 EXPECT_EQ(kDummyIndex, jcandidate.sdp_mline_index());
1796 EXPECT_TRUE(jcandidate.candidate().IsEquivalent(jcandidate_->candidate()));
1797}
1798
1799// This test verifies the deserialization of candidate-attribute
1800// as per RFC 5245. Candiate-attribute will be of the format
1801// candidate:<blah>. This format will be used when candidates
1802// are trickled.
1803TEST_F(WebRtcSdpTest, DeserializeRawCandidateAttribute) {
1804 JsepIceCandidate jcandidate(kDummyMid, kDummyIndex);
1805
1806 std::string candidate_attribute = kRawCandidate;
1807 EXPECT_TRUE(SdpDeserializeCandidate(candidate_attribute, &jcandidate));
1808 EXPECT_EQ(kDummyMid, jcandidate.sdp_mid());
1809 EXPECT_EQ(kDummyIndex, jcandidate.sdp_mline_index());
1810 EXPECT_TRUE(jcandidate.candidate().IsEquivalent(jcandidate_->candidate()));
1811 EXPECT_EQ(2u, jcandidate.candidate().generation());
1812
1813 // Candidate line without generation extension.
1814 candidate_attribute = kRawCandidate;
1815 Replace(" generation 2", "", &candidate_attribute);
1816 EXPECT_TRUE(SdpDeserializeCandidate(candidate_attribute, &jcandidate));
1817 EXPECT_EQ(kDummyMid, jcandidate.sdp_mid());
1818 EXPECT_EQ(kDummyIndex, jcandidate.sdp_mline_index());
1819 Candidate expected = jcandidate_->candidate();
1820 expected.set_generation(0);
1821 EXPECT_TRUE(jcandidate.candidate().IsEquivalent(expected));
1822
1823 // Candidate line without candidate:
1824 candidate_attribute = kRawCandidate;
1825 Replace("candidate:", "", &candidate_attribute);
1826 EXPECT_FALSE(SdpDeserializeCandidate(candidate_attribute, &jcandidate));
1827
1828 // Concatenating additional candidate. Expecting deserialization to fail.
1829 candidate_attribute = kRawCandidate;
1830 candidate_attribute.append("candidate:1 2 udp 1234 192.168.1.1 typ host");
1831 EXPECT_FALSE(SdpDeserializeCandidate(candidate_attribute, &jcandidate));
1832}
1833
1834TEST_F(WebRtcSdpTest, DeserializeSdpWithRtpDataChannels) {
1835 AddRtpDataChannel();
1836 JsepSessionDescription jdesc(kDummyString);
1837 ASSERT_TRUE(jdesc.Initialize(desc_.Copy(), kSessionId, kSessionVersion));
1838
1839 std::string sdp_with_data = kSdpString;
1840 sdp_with_data.append(kSdpRtpDataChannelString);
1841 JsepSessionDescription jdesc_output(kDummyString);
1842
1843 // Deserialize
1844 EXPECT_TRUE(SdpDeserialize(sdp_with_data, &jdesc_output));
1845 // Verify
1846 EXPECT_TRUE(CompareSessionDescription(jdesc, jdesc_output));
1847}
1848
1849TEST_F(WebRtcSdpTest, DeserializeSdpWithSctpDataChannels) {
1850 AddSctpDataChannel();
1851 JsepSessionDescription jdesc(kDummyString);
1852 ASSERT_TRUE(jdesc.Initialize(desc_.Copy(), kSessionId, kSessionVersion));
1853
1854 std::string sdp_with_data = kSdpString;
1855 sdp_with_data.append(kSdpSctpDataChannelString);
1856 JsepSessionDescription jdesc_output(kDummyString);
1857
1858 EXPECT_TRUE(SdpDeserialize(sdp_with_data, &jdesc_output));
1859 EXPECT_TRUE(CompareSessionDescription(jdesc, jdesc_output));
1860}
1861
henrike@webrtc.org571df2d2014-02-19 23:04:26 +00001862// For crbug/344475.
1863TEST_F(WebRtcSdpTest, DeserializeSdpWithCorruptedSctpDataChannels) {
1864 std::string sdp_with_data = kSdpString;
1865 sdp_with_data.append(kSdpSctpDataChannelString);
1866 // Remove the "\n" at the end.
1867 sdp_with_data = sdp_with_data.substr(0, sdp_with_data.size() - 1);
1868 JsepSessionDescription jdesc_output(kDummyString);
1869
1870 EXPECT_FALSE(SdpDeserialize(sdp_with_data, &jdesc_output));
1871 // No crash is a pass.
1872}
1873
wu@webrtc.org78187522013-10-07 23:32:02 +00001874TEST_F(WebRtcSdpTest, DeserializeSdpWithSctpDataChannelAndNewPort) {
1875 AddSctpDataChannel();
1876 const uint16 kUnusualSctpPort = 9556;
1877 char default_portstr[16];
1878 char unusual_portstr[16];
1879 talk_base::sprintfn(default_portstr, sizeof(default_portstr), "%d",
1880 kDefaultSctpPort);
1881 talk_base::sprintfn(unusual_portstr, sizeof(unusual_portstr), "%d",
1882 kUnusualSctpPort);
1883
1884 JsepSessionDescription jdesc(kDummyString);
1885 // take our pre-built session description and change the SCTP port.
1886 cricket::SessionDescription* mutant = desc_.Copy();
1887 DataContentDescription* dcdesc = static_cast<DataContentDescription*>(
1888 mutant->GetContentDescriptionByName(kDataContentName));
1889 std::vector<cricket::DataCodec> codecs(dcdesc->codecs());
1890 EXPECT_EQ(codecs.size(), 1UL);
1891 EXPECT_EQ(codecs[0].id, cricket::kGoogleSctpDataCodecId);
1892 codecs[0].SetParam(cricket::kCodecParamPort, kUnusualSctpPort);
1893
1894 // note: mutant's owned by jdesc now.
1895 ASSERT_TRUE(jdesc.Initialize(mutant, kSessionId, kSessionVersion));
1896 mutant = NULL;
1897
1898 std::string sdp_with_data = kSdpString;
1899 sdp_with_data.append(kSdpSctpDataChannelString);
1900 talk_base::replace_substrs(default_portstr, strlen(default_portstr),
1901 unusual_portstr, strlen(unusual_portstr),
1902 &sdp_with_data);
1903 JsepSessionDescription jdesc_output(kDummyString);
1904
1905 EXPECT_TRUE(SdpDeserialize(sdp_with_data, &jdesc_output));
1906 EXPECT_TRUE(CompareSessionDescription(jdesc, jdesc_output));
1907}
1908
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001909TEST_F(WebRtcSdpTest, DeserializeSdpWithRtpDataChannelsAndBandwidth) {
1910 AddRtpDataChannel();
1911 JsepSessionDescription jdesc(kDummyString);
1912 // We want to test that deserializing data content ignores bandwidth
1913 // settings (it should always be the default). Thus, we don't do
1914 // the following:
sergeyu@chromium.orga59696b2013-09-13 23:48:58 +00001915 // TODO(pthatcher): We need to temporarily allow the SDP to control
1916 // this for backwards-compatibility. Once we don't need that any
1917 // more, remove this.
1918 DataContentDescription* dcd = static_cast<DataContentDescription*>(
1919 GetFirstDataContent(&desc_)->description);
1920 dcd->set_bandwidth(100 * 1000);
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001921 ASSERT_TRUE(jdesc.Initialize(desc_.Copy(), kSessionId, kSessionVersion));
1922
1923 std::string sdp_with_bandwidth = kSdpString;
1924 sdp_with_bandwidth.append(kSdpRtpDataChannelString);
1925 InjectAfter("a=mid:data_content_name\r\n",
1926 "b=AS:100\r\n",
1927 &sdp_with_bandwidth);
1928 JsepSessionDescription jdesc_with_bandwidth(kDummyString);
1929
1930 EXPECT_TRUE(
1931 SdpDeserialize(sdp_with_bandwidth, &jdesc_with_bandwidth));
1932 EXPECT_TRUE(CompareSessionDescription(jdesc, jdesc_with_bandwidth));
1933}
1934
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001935TEST_F(WebRtcSdpTest, DeserializeSessionDescriptionWithSessionLevelExtmap) {
1936 TestDeserializeExtmap(true, false);
1937}
1938
1939TEST_F(WebRtcSdpTest, DeserializeSessionDescriptionWithMediaLevelExtmap) {
1940 TestDeserializeExtmap(false, true);
1941}
1942
1943TEST_F(WebRtcSdpTest, DeserializeSessionDescriptionWithInvalidExtmap) {
1944 TestDeserializeExtmap(true, true);
1945}
1946
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +00001947TEST_F(WebRtcSdpTest, DeserializeSessionDescriptionWithoutEndLineBreak) {
1948 JsepSessionDescription jdesc(kDummyString);
1949 std::string sdp = kSdpFullString;
1950 sdp = sdp.substr(0, sdp.size() - 2); // Remove \r\n at the end.
1951 // Deserialize
1952 SdpParseError error;
1953 EXPECT_FALSE(webrtc::SdpDeserialize(sdp, &jdesc, &error));
1954 const std::string lastline = "a=ssrc:6 label:video_track_id_3";
1955 EXPECT_EQ(lastline, error.line);
1956 EXPECT_EQ("Invalid SDP line.", error.description);
1957}
1958
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001959TEST_F(WebRtcSdpTest, DeserializeCandidateWithDifferentTransport) {
1960 JsepIceCandidate jcandidate(kDummyMid, kDummyIndex);
1961 std::string new_sdp = kSdpOneCandidate;
1962 Replace("udp", "unsupported_transport", &new_sdp);
1963 EXPECT_FALSE(SdpDeserializeCandidate(new_sdp, &jcandidate));
1964 new_sdp = kSdpOneCandidate;
1965 Replace("udp", "uDP", &new_sdp);
1966 EXPECT_TRUE(SdpDeserializeCandidate(new_sdp, &jcandidate));
1967 EXPECT_EQ(kDummyMid, jcandidate.sdp_mid());
1968 EXPECT_EQ(kDummyIndex, jcandidate.sdp_mline_index());
1969 EXPECT_TRUE(jcandidate.candidate().IsEquivalent(jcandidate_->candidate()));
1970}
1971
1972TEST_F(WebRtcSdpTest, DeserializeCandidateOldFormat) {
1973 JsepIceCandidate jcandidate(kDummyMid, kDummyIndex);
1974 EXPECT_TRUE(SdpDeserializeCandidate(kSdpOneCandidateOldFormat,&jcandidate));
1975 EXPECT_EQ(kDummyMid, jcandidate.sdp_mid());
1976 EXPECT_EQ(kDummyIndex, jcandidate.sdp_mline_index());
1977 Candidate ref_candidate = jcandidate_->candidate();
1978 ref_candidate.set_username("user_rtp");
1979 ref_candidate.set_password("password_rtp");
1980 EXPECT_TRUE(jcandidate.candidate().IsEquivalent(ref_candidate));
1981}
1982
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +00001983TEST_F(WebRtcSdpTest, DeserializeSdpWithConferenceFlag) {
1984 JsepSessionDescription jdesc(kDummyString);
1985
1986 // Deserialize
1987 EXPECT_TRUE(SdpDeserialize(kSdpConferenceString, &jdesc));
1988
1989 // Verify
1990 cricket::AudioContentDescription* audio =
1991 static_cast<AudioContentDescription*>(
1992 jdesc.description()->GetContentDescriptionByName(cricket::CN_AUDIO));
1993 EXPECT_TRUE(audio->conference_mode());
1994
1995 cricket::VideoContentDescription* video =
1996 static_cast<VideoContentDescription*>(
1997 jdesc.description()->GetContentDescriptionByName(cricket::CN_VIDEO));
1998 EXPECT_TRUE(video->conference_mode());
1999}
2000
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002001TEST_F(WebRtcSdpTest, DeserializeBrokenSdp) {
2002 const char kSdpDestroyer[] = "!@#$%^&";
2003 const char kSdpInvalidLine1[] = " =candidate";
2004 const char kSdpInvalidLine2[] = "a+candidate";
2005 const char kSdpInvalidLine3[] = "a= candidate";
2006 // Broken fingerprint.
2007 const char kSdpInvalidLine4[] = "a=fingerprint:sha-1 "
2008 "4AAD:B9:B1:3F:82:18:3B:54:02:12:DF:3E:5D:49:6B:19:E5:7C:AB";
2009 // Extra field.
2010 const char kSdpInvalidLine5[] = "a=fingerprint:sha-1 "
2011 "4A:AD:B9:B1:3F:82:18:3B:54:02:12:DF:3E:5D:49:6B:19:E5:7C:AB XXX";
2012 // Missing space.
2013 const char kSdpInvalidLine6[] = "a=fingerprint:sha-1"
2014 "4A:AD:B9:B1:3F:82:18:3B:54:02:12:DF:3E:5D:49:6B:19:E5:7C:AB";
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +00002015 // MD5 is not allowed in fingerprints.
2016 const char kSdpInvalidLine7[] = "a=fingerprint:md5 "
2017 "4A:AD:B9:B1:3F:82:18:3B:54:02:12:DF:3E:5D:49:6B";
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002018
2019 // Broken session description
2020 ReplaceAndTryToParse("v=", kSdpDestroyer);
2021 ReplaceAndTryToParse("o=", kSdpDestroyer);
2022 ReplaceAndTryToParse("s=-", kSdpDestroyer);
2023 // Broken time description
2024 ReplaceAndTryToParse("t=", kSdpDestroyer);
2025
2026 // Broken media description
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00002027 ReplaceAndTryToParse("m=audio", "c=IN IP4 74.125.224.39");
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002028 ReplaceAndTryToParse("m=video", kSdpDestroyer);
2029
2030 // Invalid lines
2031 ReplaceAndTryToParse("a=candidate", kSdpInvalidLine1);
2032 ReplaceAndTryToParse("a=candidate", kSdpInvalidLine2);
2033 ReplaceAndTryToParse("a=candidate", kSdpInvalidLine3);
2034
2035 // Bogus fingerprint replacing a=sendrev. We selected this attribute
2036 // because it's orthogonal to what we are replacing and hence
2037 // safe.
2038 ReplaceAndTryToParse("a=sendrecv", kSdpInvalidLine4);
2039 ReplaceAndTryToParse("a=sendrecv", kSdpInvalidLine5);
2040 ReplaceAndTryToParse("a=sendrecv", kSdpInvalidLine6);
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +00002041 ReplaceAndTryToParse("a=sendrecv", kSdpInvalidLine7);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002042}
2043
2044TEST_F(WebRtcSdpTest, DeserializeSdpWithReorderedPltypes) {
2045 JsepSessionDescription jdesc_output(kDummyString);
2046
2047 const char kSdpWithReorderedPlTypesString[] =
2048 "v=0\r\n"
2049 "o=- 18446744069414584320 18446462598732840960 IN IP4 127.0.0.1\r\n"
2050 "s=-\r\n"
2051 "t=0 0\r\n"
2052 "m=audio 1 RTP/SAVPF 104 103\r\n" // Pl type 104 preferred.
2053 "a=rtpmap:111 opus/48000/2\r\n" // Pltype 111 listed before 103 and 104
2054 // in the map.
2055 "a=rtpmap:103 ISAC/16000\r\n" // Pltype 103 listed before 104 in the map.
2056 "a=rtpmap:104 CELT/32000/2\r\n";
2057
2058 // Deserialize
2059 EXPECT_TRUE(SdpDeserialize(kSdpWithReorderedPlTypesString, &jdesc_output));
2060
2061 const ContentInfo* ac = GetFirstAudioContent(jdesc_output.description());
2062 ASSERT_TRUE(ac != NULL);
2063 const AudioContentDescription* acd =
2064 static_cast<const AudioContentDescription*>(ac->description);
2065 ASSERT_FALSE(acd->codecs().empty());
2066 EXPECT_EQ("CELT", acd->codecs()[0].name);
2067 EXPECT_EQ(104, acd->codecs()[0].id);
2068}
2069
2070TEST_F(WebRtcSdpTest, DeserializeSerializeCodecParams) {
2071 JsepSessionDescription jdesc_output(kDummyString);
2072 CodecParams params;
2073 params.max_ptime = 40;
2074 params.ptime = 30;
2075 params.min_ptime = 10;
2076 params.sprop_stereo = 1;
2077 params.stereo = 1;
2078 params.useinband = 1;
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00002079 params.maxaveragebitrate = 128000;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002080 TestDeserializeCodecParams(params, &jdesc_output);
2081 TestSerialize(jdesc_output);
2082}
2083
2084TEST_F(WebRtcSdpTest, DeserializeSerializeRtcpFb) {
2085 const bool kUseWildcard = false;
2086 JsepSessionDescription jdesc_output(kDummyString);
2087 TestDeserializeRtcpFb(&jdesc_output, kUseWildcard);
2088 TestSerialize(jdesc_output);
2089}
2090
2091TEST_F(WebRtcSdpTest, DeserializeSerializeRtcpFbWildcard) {
2092 const bool kUseWildcard = true;
2093 JsepSessionDescription jdesc_output(kDummyString);
2094 TestDeserializeRtcpFb(&jdesc_output, kUseWildcard);
2095 TestSerialize(jdesc_output);
2096}
2097
2098TEST_F(WebRtcSdpTest, DeserializeVideoFmtp) {
2099 JsepSessionDescription jdesc_output(kDummyString);
2100
2101 const char kSdpWithFmtpString[] =
2102 "v=0\r\n"
2103 "o=- 18446744069414584320 18446462598732840960 IN IP4 127.0.0.1\r\n"
2104 "s=-\r\n"
2105 "t=0 0\r\n"
2106 "m=video 3457 RTP/SAVPF 120\r\n"
2107 "a=rtpmap:120 VP8/90000\r\n"
2108 "a=fmtp:120 x-google-min-bitrate=10; x-google-max-quantization=40\r\n";
2109
2110 // Deserialize
2111 SdpParseError error;
2112 EXPECT_TRUE(webrtc::SdpDeserialize(kSdpWithFmtpString, &jdesc_output,
2113 &error));
2114
2115 const ContentInfo* vc = GetFirstVideoContent(jdesc_output.description());
2116 ASSERT_TRUE(vc != NULL);
2117 const VideoContentDescription* vcd =
2118 static_cast<const VideoContentDescription*>(vc->description);
2119 ASSERT_FALSE(vcd->codecs().empty());
2120 cricket::VideoCodec vp8 = vcd->codecs()[0];
2121 EXPECT_EQ("VP8", vp8.name);
2122 EXPECT_EQ(120, vp8.id);
2123 cricket::CodecParameterMap::iterator found =
2124 vp8.params.find("x-google-min-bitrate");
2125 ASSERT_TRUE(found != vp8.params.end());
2126 EXPECT_EQ(found->second, "10");
2127 found = vp8.params.find("x-google-max-quantization");
2128 ASSERT_TRUE(found != vp8.params.end());
2129 EXPECT_EQ(found->second, "40");
2130}
2131
2132TEST_F(WebRtcSdpTest, SerializeVideoFmtp) {
2133 VideoContentDescription* vcd = static_cast<VideoContentDescription*>(
2134 GetFirstVideoContent(&desc_)->description);
2135
2136 cricket::VideoCodecs codecs = vcd->codecs();
2137 codecs[0].params["x-google-min-bitrate"] = "10";
2138 vcd->set_codecs(codecs);
2139
2140 ASSERT_TRUE(jdesc_.Initialize(desc_.Copy(),
2141 jdesc_.session_id(),
2142 jdesc_.session_version()));
2143 std::string message = webrtc::SdpSerialize(jdesc_);
2144 std::string sdp_with_fmtp = kSdpFullString;
2145 InjectAfter("a=rtpmap:120 VP8/90000\r\n",
2146 "a=fmtp:120 x-google-min-bitrate=10\r\n",
2147 &sdp_with_fmtp);
2148 EXPECT_EQ(sdp_with_fmtp, message);
2149}
2150
2151TEST_F(WebRtcSdpTest, DeserializeSdpWithIceLite) {
2152 JsepSessionDescription jdesc_with_icelite(kDummyString);
2153 std::string sdp_with_icelite = kSdpFullString;
2154 EXPECT_TRUE(SdpDeserialize(sdp_with_icelite, &jdesc_with_icelite));
2155 cricket::SessionDescription* desc = jdesc_with_icelite.description();
2156 const cricket::TransportInfo* tinfo1 =
2157 desc->GetTransportInfoByName("audio_content_name");
2158 EXPECT_EQ(cricket::ICEMODE_FULL, tinfo1->description.ice_mode);
2159 const cricket::TransportInfo* tinfo2 =
2160 desc->GetTransportInfoByName("video_content_name");
2161 EXPECT_EQ(cricket::ICEMODE_FULL, tinfo2->description.ice_mode);
2162 InjectAfter(kSessionTime,
2163 "a=ice-lite\r\n",
2164 &sdp_with_icelite);
2165 EXPECT_TRUE(SdpDeserialize(sdp_with_icelite, &jdesc_with_icelite));
2166 desc = jdesc_with_icelite.description();
2167 const cricket::TransportInfo* atinfo =
2168 desc->GetTransportInfoByName("audio_content_name");
2169 EXPECT_EQ(cricket::ICEMODE_LITE, atinfo->description.ice_mode);
2170 const cricket::TransportInfo* vtinfo =
2171 desc->GetTransportInfoByName("video_content_name");
2172 EXPECT_EQ(cricket::ICEMODE_LITE, vtinfo->description.ice_mode);
2173}
2174
2175// Verifies that the candidates in the input SDP are parsed and serialized
2176// correctly in the output SDP.
2177TEST_F(WebRtcSdpTest, RoundTripSdpWithSctpDataChannelsWithCandidates) {
2178 std::string sdp_with_data = kSdpString;
2179 sdp_with_data.append(kSdpSctpDataChannelWithCandidatesString);
2180 JsepSessionDescription jdesc_output(kDummyString);
2181
2182 EXPECT_TRUE(SdpDeserialize(sdp_with_data, &jdesc_output));
2183 EXPECT_EQ(sdp_with_data, webrtc::SdpSerialize(jdesc_output));
2184}
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00002185
2186TEST_F(WebRtcSdpTest, SerializeDtlsSetupAttribute) {
2187 AddFingerprint();
2188 TransportInfo audio_transport_info =
2189 *(desc_.GetTransportInfoByName(kAudioContentName));
2190 EXPECT_EQ(cricket::CONNECTIONROLE_NONE,
2191 audio_transport_info.description.connection_role);
2192 audio_transport_info.description.connection_role =
2193 cricket::CONNECTIONROLE_ACTIVE;
2194
2195 TransportInfo video_transport_info =
2196 *(desc_.GetTransportInfoByName(kVideoContentName));
2197 EXPECT_EQ(cricket::CONNECTIONROLE_NONE,
2198 video_transport_info.description.connection_role);
2199 video_transport_info.description.connection_role =
2200 cricket::CONNECTIONROLE_ACTIVE;
2201
2202 desc_.RemoveTransportInfoByName(kAudioContentName);
2203 desc_.RemoveTransportInfoByName(kVideoContentName);
2204
2205 desc_.AddTransportInfo(audio_transport_info);
2206 desc_.AddTransportInfo(video_transport_info);
2207
2208 ASSERT_TRUE(jdesc_.Initialize(desc_.Copy(),
2209 jdesc_.session_id(),
2210 jdesc_.session_version()));
2211 std::string message = webrtc::SdpSerialize(jdesc_);
2212 std::string sdp_with_dtlssetup = kSdpFullString;
2213
2214 // Fingerprint attribute is necessary to add DTLS setup attribute.
2215 InjectAfter(kAttributeIcePwdVoice,
2216 kFingerprint, &sdp_with_dtlssetup);
2217 InjectAfter(kAttributeIcePwdVideo,
2218 kFingerprint, &sdp_with_dtlssetup);
2219 // Now adding |setup| attribute.
2220 InjectAfter(kFingerprint,
2221 "a=setup:active\r\n", &sdp_with_dtlssetup);
2222 EXPECT_EQ(sdp_with_dtlssetup, message);
2223}
2224
2225TEST_F(WebRtcSdpTest, DeserializeDtlsSetupAttribute) {
2226 JsepSessionDescription jdesc_with_dtlssetup(kDummyString);
2227 std::string sdp_with_dtlssetup = kSdpFullString;
2228 InjectAfter(kSessionTime,
2229 "a=setup:actpass\r\n",
2230 &sdp_with_dtlssetup);
2231 EXPECT_TRUE(SdpDeserialize(sdp_with_dtlssetup, &jdesc_with_dtlssetup));
2232 cricket::SessionDescription* desc = jdesc_with_dtlssetup.description();
2233 const cricket::TransportInfo* atinfo =
2234 desc->GetTransportInfoByName("audio_content_name");
2235 EXPECT_EQ(cricket::CONNECTIONROLE_ACTPASS,
2236 atinfo->description.connection_role);
2237 const cricket::TransportInfo* vtinfo =
2238 desc->GetTransportInfoByName("video_content_name");
2239 EXPECT_EQ(cricket::CONNECTIONROLE_ACTPASS,
2240 vtinfo->description.connection_role);
2241}