blob: 71ebe0d85a34492ef5c11fa08cf262908df4cb8e [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"
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000034#include "webrtc/base/gunit.h"
35#include "webrtc/base/logging.h"
36#include "webrtc/base/messagedigest.h"
37#include "webrtc/base/scoped_ptr.h"
38#include "webrtc/base/sslfingerprint.h"
39#include "webrtc/base/stringencode.h"
40#include "webrtc/base/stringutils.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000041#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
jiayl@webrtc.org9c16c392014-05-01 18:30:30 +0000302static const char kSdpConferenceString[] =
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +0000303 "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
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +0000315static const char kSdpSessionString[] =
316 "v=0\r\n"
317 "o=- 18446744069414584320 18446462598732840960 IN IP4 127.0.0.1\r\n"
318 "s=-\r\n"
319 "t=0 0\r\n"
320 "a=msid-semantic: WMS local_stream\r\n";
321
322static const char kSdpAudioString[] =
323 "m=audio 1 RTP/SAVPF 111\r\n"
324 "c=IN IP4 0.0.0.0\r\n"
325 "a=rtcp:1 IN IP4 0.0.0.0\r\n"
326 "a=ice-ufrag:ufrag_voice\r\na=ice-pwd:pwd_voice\r\n"
327 "a=mid:audio_content_name\r\n"
328 "a=sendrecv\r\n"
329 "a=rtpmap:111 opus/48000/2\r\n"
330 "a=ssrc:1 cname:stream_1_cname\r\n"
331 "a=ssrc:1 msid:local_stream audio_track_id_1\r\n"
332 "a=ssrc:1 mslabel:local_stream\r\n"
333 "a=ssrc:1 label:audio_track_id_1\r\n";
334
335static const char kSdpVideoString[] =
336 "m=video 1 RTP/SAVPF 120\r\n"
337 "c=IN IP4 0.0.0.0\r\n"
338 "a=rtcp:1 IN IP4 0.0.0.0\r\n"
339 "a=ice-ufrag:ufrag_video\r\na=ice-pwd:pwd_video\r\n"
340 "a=mid:video_content_name\r\n"
341 "a=sendrecv\r\n"
342 "a=rtpmap:120 VP8/90000\r\n"
343 "a=ssrc:2 cname:stream_1_cname\r\n"
344 "a=ssrc:2 msid:local_stream video_track_id_1\r\n"
345 "a=ssrc:2 mslabel:local_stream\r\n"
346 "a=ssrc:2 label:video_track_id_1\r\n";
347
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000348
349// One candidate reference string as per W3c spec.
350// candidate:<blah> not a=candidate:<blah>CRLF
351static const char kRawCandidate[] =
352 "candidate:a0+B/1 1 udp 2130706432 192.168.1.5 1234 typ host generation 2";
353// One candidate reference string.
354static const char kSdpOneCandidate[] =
355 "a=candidate:a0+B/1 1 udp 2130706432 192.168.1.5 1234 typ host "
356 "generation 2\r\n";
357
358// One candidate reference string.
359static const char kSdpOneCandidateOldFormat[] =
360 "a=candidate:a0+B/1 1 udp 2130706432 192.168.1.5 1234 typ host network_name"
361 " eth0 username user_rtp password password_rtp generation 2\r\n";
362
363// Session id and version
364static const char kSessionId[] = "18446744069414584320";
365static const char kSessionVersion[] = "18446462598732840960";
366
367// Ice options
368static const char kIceOption1[] = "iceoption1";
369static const char kIceOption2[] = "iceoption2";
370static const char kIceOption3[] = "iceoption3";
371
372// Content name
373static const char kAudioContentName[] = "audio_content_name";
374static const char kVideoContentName[] = "video_content_name";
375static const char kDataContentName[] = "data_content_name";
376
377// MediaStream 1
378static const char kStreamLabel1[] = "local_stream_1";
379static const char kStream1Cname[] = "stream_1_cname";
380static const char kAudioTrackId1[] = "audio_track_id_1";
381static const uint32 kAudioTrack1Ssrc = 1;
382static const char kVideoTrackId1[] = "video_track_id_1";
383static const uint32 kVideoTrack1Ssrc = 2;
384static const char kVideoTrackId2[] = "video_track_id_2";
385static const uint32 kVideoTrack2Ssrc = 3;
386
387// MediaStream 2
388static const char kStreamLabel2[] = "local_stream_2";
389static const char kStream2Cname[] = "stream_2_cname";
390static const char kAudioTrackId2[] = "audio_track_id_2";
391static const uint32 kAudioTrack2Ssrc = 4;
392static const char kVideoTrackId3[] = "video_track_id_3";
393static const uint32 kVideoTrack3Ssrc = 5;
394static const uint32 kVideoTrack4Ssrc = 6;
395
396// DataChannel
397static const char kDataChannelLabel[] = "data_channel";
398static const char kDataChannelMsid[] = "data_channeld0";
399static const char kDataChannelCname[] = "data_channel_cname";
400static const uint32 kDataChannelSsrc = 10;
401
402// Candidate
403static const char kDummyMid[] = "dummy_mid";
404static const int kDummyIndex = 123;
405
406// Misc
407static const char kDummyString[] = "dummy";
408
409// Helper functions
410
411static bool SdpDeserialize(const std::string& message,
412 JsepSessionDescription* jdesc) {
413 return webrtc::SdpDeserialize(message, jdesc, NULL);
414}
415
416static bool SdpDeserializeCandidate(const std::string& message,
417 JsepIceCandidate* candidate) {
418 return webrtc::SdpDeserializeCandidate(message, candidate, NULL);
419}
420
421// Add some extra |newlines| to the |message| after |line|.
422static void InjectAfter(const std::string& line,
423 const std::string& newlines,
424 std::string* message) {
425 const std::string tmp = line + newlines;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000426 rtc::replace_substrs(line.c_str(), line.length(),
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000427 tmp.c_str(), tmp.length(), message);
428}
429
430static void Replace(const std::string& line,
431 const std::string& newlines,
432 std::string* message) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000433 rtc::replace_substrs(line.c_str(), line.length(),
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000434 newlines.c_str(), newlines.length(), message);
435}
436
wu@webrtc.org5e760e72014-04-02 23:19:09 +0000437// Expect fail to parase |bad_sdp| and expect |bad_part| be part of the error
438// message.
439static void ExpectParseFailure(const std::string& bad_sdp,
440 const std::string& bad_part) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000441 JsepSessionDescription desc(kDummyString);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000442 SdpParseError error;
wu@webrtc.org5e760e72014-04-02 23:19:09 +0000443 bool ret = webrtc::SdpDeserialize(bad_sdp, &desc, &error);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000444 EXPECT_FALSE(ret);
wu@webrtc.org5e760e72014-04-02 23:19:09 +0000445 EXPECT_NE(std::string::npos, error.line.find(bad_part.c_str()));
446}
447
448// Expect fail to parse kSdpFullString if replace |good_part| with |bad_part|.
449static void ExpectParseFailure(const char* good_part, const char* bad_part) {
450 std::string bad_sdp = kSdpFullString;
451 Replace(good_part, bad_part, &bad_sdp);
452 ExpectParseFailure(bad_sdp, bad_part);
453}
454
455// Expect fail to parse kSdpFullString if add |newlines| after |injectpoint|.
456static void ExpectParseFailureWithNewLines(const std::string& injectpoint,
457 const std::string& newlines,
458 const std::string& bad_part) {
459 std::string bad_sdp = kSdpFullString;
460 InjectAfter(injectpoint, newlines, &bad_sdp);
461 ExpectParseFailure(bad_sdp, bad_part);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000462}
463
464static void ReplaceDirection(cricket::MediaContentDirection direction,
465 std::string* message) {
466 std::string new_direction;
467 switch (direction) {
468 case cricket::MD_INACTIVE:
469 new_direction = "a=inactive";
470 break;
471 case cricket::MD_SENDONLY:
472 new_direction = "a=sendonly";
473 break;
474 case cricket::MD_RECVONLY:
475 new_direction = "a=recvonly";
476 break;
477 case cricket::MD_SENDRECV:
478 default:
479 new_direction = "a=sendrecv";
480 break;
481 }
482 Replace("a=sendrecv", new_direction, message);
483}
484
485static void ReplaceRejected(bool audio_rejected, bool video_rejected,
486 std::string* message) {
487 if (audio_rejected) {
488 Replace("m=audio 2345", "m=audio 0", message);
489 }
490 if (video_rejected) {
491 Replace("m=video 3457", "m=video 0", message);
492 }
493}
494
495// WebRtcSdpTest
496
497class WebRtcSdpTest : public testing::Test {
498 public:
499 WebRtcSdpTest()
500 : jdesc_(kDummyString) {
501 // AudioContentDescription
502 audio_desc_ = CreateAudioContentDescription();
503 AudioCodec opus(111, "opus", 48000, 0, 2, 3);
504 audio_desc_->AddCodec(opus);
505 audio_desc_->AddCodec(AudioCodec(103, "ISAC", 16000, 32000, 1, 2));
506 audio_desc_->AddCodec(AudioCodec(104, "CELT", 32000, 0, 2, 1));
507 desc_.AddContent(kAudioContentName, NS_JINGLE_RTP, audio_desc_);
508
509 // VideoContentDescription
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000510 rtc::scoped_ptr<VideoContentDescription> video(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000511 new VideoContentDescription());
512 video_desc_ = video.get();
513 StreamParams video_stream1;
514 video_stream1.id = kVideoTrackId1;
515 video_stream1.cname = kStream1Cname;
516 video_stream1.sync_label = kStreamLabel1;
517 video_stream1.ssrcs.push_back(kVideoTrack1Ssrc);
518 video->AddStream(video_stream1);
519 StreamParams video_stream2;
520 video_stream2.id = kVideoTrackId2;
521 video_stream2.cname = kStream1Cname;
522 video_stream2.sync_label = kStreamLabel1;
523 video_stream2.ssrcs.push_back(kVideoTrack2Ssrc);
524 video->AddStream(video_stream2);
525 StreamParams video_stream3;
526 video_stream3.id = kVideoTrackId3;
527 video_stream3.cname = kStream2Cname;
528 video_stream3.sync_label = kStreamLabel2;
529 video_stream3.ssrcs.push_back(kVideoTrack3Ssrc);
530 video_stream3.ssrcs.push_back(kVideoTrack4Ssrc);
531 cricket::SsrcGroup ssrc_group(kFecSsrcGroupSemantics, video_stream3.ssrcs);
532 video_stream3.ssrc_groups.push_back(ssrc_group);
533 video->AddStream(video_stream3);
534 video->AddCrypto(CryptoParams(1, "AES_CM_128_HMAC_SHA1_80",
535 "inline:d0RmdmcmVCspeEc3QGZiNWpVLFJhQX1cfHAwJSoj|2^20|1:32", ""));
536 video->set_protocol(cricket::kMediaProtocolSavpf);
537 video->AddCodec(VideoCodec(
538 120,
539 JsepSessionDescription::kDefaultVideoCodecName,
540 JsepSessionDescription::kMaxVideoCodecWidth,
541 JsepSessionDescription::kMaxVideoCodecHeight,
542 JsepSessionDescription::kDefaultVideoCodecFramerate,
543 JsepSessionDescription::kDefaultVideoCodecPreference));
544
545 desc_.AddContent(kVideoContentName, NS_JINGLE_RTP,
546 video.release());
547
548 // TransportInfo
549 EXPECT_TRUE(desc_.AddTransportInfo(
550 TransportInfo(kAudioContentName,
551 TransportDescription(NS_JINGLE_ICE_UDP,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000552 kCandidateUfragVoice,
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +0000553 kCandidatePwdVoice))));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000554 EXPECT_TRUE(desc_.AddTransportInfo(
555 TransportInfo(kVideoContentName,
556 TransportDescription(NS_JINGLE_ICE_UDP,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000557 kCandidateUfragVideo,
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +0000558 kCandidatePwdVideo))));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000559
560 // v4 host
561 int port = 1234;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000562 rtc::SocketAddress address("192.168.1.5", port++);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000563 Candidate candidate1(
564 "", ICE_CANDIDATE_COMPONENT_RTP, "udp", address, kCandidatePriority,
565 "", "", LOCAL_PORT_TYPE,
566 "", kCandidateGeneration, kCandidateFoundation1);
567 address.SetPort(port++);
568 Candidate candidate2(
569 "", ICE_CANDIDATE_COMPONENT_RTCP, "udp", address, kCandidatePriority,
570 "", "", LOCAL_PORT_TYPE,
571 "", kCandidateGeneration, kCandidateFoundation1);
572 address.SetPort(port++);
573 Candidate candidate3(
574 "", ICE_CANDIDATE_COMPONENT_RTCP, "udp", address, kCandidatePriority,
575 "", "", LOCAL_PORT_TYPE,
576 "", kCandidateGeneration, kCandidateFoundation1);
577 address.SetPort(port++);
578 Candidate candidate4(
579 "", ICE_CANDIDATE_COMPONENT_RTP, "udp", address, kCandidatePriority,
580 "", "", LOCAL_PORT_TYPE,
581 "", kCandidateGeneration, kCandidateFoundation1);
582
583 // v6 host
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000584 rtc::SocketAddress v6_address("::1", port++);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000585 cricket::Candidate candidate5(
586 "", cricket::ICE_CANDIDATE_COMPONENT_RTP,
587 "udp", v6_address, kCandidatePriority,
588 "", "", cricket::LOCAL_PORT_TYPE,
589 "", kCandidateGeneration, kCandidateFoundation2);
590 v6_address.SetPort(port++);
591 cricket::Candidate candidate6(
592 "", cricket::ICE_CANDIDATE_COMPONENT_RTCP,
593 "udp", v6_address, kCandidatePriority,
594 "", "", cricket::LOCAL_PORT_TYPE,
595 "", kCandidateGeneration, kCandidateFoundation2);
596 v6_address.SetPort(port++);
597 cricket::Candidate candidate7(
598 "", cricket::ICE_CANDIDATE_COMPONENT_RTCP,
599 "udp", v6_address, kCandidatePriority,
600 "", "", cricket::LOCAL_PORT_TYPE,
601 "", kCandidateGeneration, kCandidateFoundation2);
602 v6_address.SetPort(port++);
603 cricket::Candidate candidate8(
604 "", cricket::ICE_CANDIDATE_COMPONENT_RTP,
605 "udp", v6_address, kCandidatePriority,
606 "", "", cricket::LOCAL_PORT_TYPE,
607 "", kCandidateGeneration, kCandidateFoundation2);
608
609 // stun
610 int port_stun = 2345;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000611 rtc::SocketAddress address_stun("74.125.127.126", port_stun++);
612 rtc::SocketAddress rel_address_stun("192.168.1.5", port_stun++);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000613 cricket::Candidate candidate9
614 ("", cricket::ICE_CANDIDATE_COMPONENT_RTP,
615 "udp", address_stun, kCandidatePriority,
616 "", "", STUN_PORT_TYPE,
617 "", kCandidateGeneration, kCandidateFoundation3);
618 candidate9.set_related_address(rel_address_stun);
619
620 address_stun.SetPort(port_stun++);
621 rel_address_stun.SetPort(port_stun++);
622 cricket::Candidate candidate10(
623 "", cricket::ICE_CANDIDATE_COMPONENT_RTCP,
624 "udp", address_stun, kCandidatePriority,
625 "", "", STUN_PORT_TYPE,
626 "", kCandidateGeneration, kCandidateFoundation3);
627 candidate10.set_related_address(rel_address_stun);
628
629 // relay
630 int port_relay = 3456;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000631 rtc::SocketAddress address_relay("74.125.224.39", port_relay++);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000632 cricket::Candidate candidate11(
633 "", cricket::ICE_CANDIDATE_COMPONENT_RTCP,
634 "udp", address_relay, kCandidatePriority,
635 "", "",
636 cricket::RELAY_PORT_TYPE, "",
637 kCandidateGeneration, kCandidateFoundation4);
638 address_relay.SetPort(port_relay++);
639 cricket::Candidate candidate12(
640 "", cricket::ICE_CANDIDATE_COMPONENT_RTP,
641 "udp", address_relay, kCandidatePriority,
642 "", "",
643 RELAY_PORT_TYPE, "",
644 kCandidateGeneration, kCandidateFoundation4);
645
646 // voice
647 candidates_.push_back(candidate1);
648 candidates_.push_back(candidate2);
649 candidates_.push_back(candidate5);
650 candidates_.push_back(candidate6);
651 candidates_.push_back(candidate9);
652 candidates_.push_back(candidate10);
653
654 // video
655 candidates_.push_back(candidate3);
656 candidates_.push_back(candidate4);
657 candidates_.push_back(candidate7);
658 candidates_.push_back(candidate8);
659 candidates_.push_back(candidate11);
660 candidates_.push_back(candidate12);
661
662 jcandidate_.reset(new JsepIceCandidate(std::string("audio_content_name"),
663 0, candidate1));
664
665 // Set up JsepSessionDescription.
666 jdesc_.Initialize(desc_.Copy(), kSessionId, kSessionVersion);
667 std::string mline_id;
668 int mline_index = 0;
669 for (size_t i = 0; i< candidates_.size(); ++i) {
670 // In this test, the audio m line index will be 0, and the video m line
671 // will be 1.
672 bool is_video = (i > 5);
673 mline_id = is_video ? "video_content_name" : "audio_content_name";
674 mline_index = is_video ? 1 : 0;
675 JsepIceCandidate jice(mline_id,
676 mline_index,
677 candidates_.at(i));
678 jdesc_.AddCandidate(&jice);
679 }
680 }
681
682 AudioContentDescription* CreateAudioContentDescription() {
683 AudioContentDescription* audio = new AudioContentDescription();
684 audio->set_rtcp_mux(true);
685 StreamParams audio_stream1;
686 audio_stream1.id = kAudioTrackId1;
687 audio_stream1.cname = kStream1Cname;
688 audio_stream1.sync_label = kStreamLabel1;
689 audio_stream1.ssrcs.push_back(kAudioTrack1Ssrc);
690 audio->AddStream(audio_stream1);
691 StreamParams audio_stream2;
692 audio_stream2.id = kAudioTrackId2;
693 audio_stream2.cname = kStream2Cname;
694 audio_stream2.sync_label = kStreamLabel2;
695 audio_stream2.ssrcs.push_back(kAudioTrack2Ssrc);
696 audio->AddStream(audio_stream2);
697 audio->AddCrypto(CryptoParams(1, "AES_CM_128_HMAC_SHA1_32",
698 "inline:NzB4d1BINUAvLEw6UzF3WSJ+PSdFcGdUJShpX1Zj|2^20|1:32",
699 "dummy_session_params"));
700 audio->set_protocol(cricket::kMediaProtocolSavpf);
701 return audio;
702 }
703
704 template <class MCD>
705 void CompareMediaContentDescription(const MCD* cd1,
706 const MCD* cd2) {
707 // type
708 EXPECT_EQ(cd1->type(), cd1->type());
709
710 // content direction
711 EXPECT_EQ(cd1->direction(), cd2->direction());
712
713 // rtcp_mux
714 EXPECT_EQ(cd1->rtcp_mux(), cd2->rtcp_mux());
715
716 // cryptos
717 EXPECT_EQ(cd1->cryptos().size(), cd2->cryptos().size());
718 if (cd1->cryptos().size() != cd2->cryptos().size()) {
719 ADD_FAILURE();
720 return;
721 }
722 for (size_t i = 0; i< cd1->cryptos().size(); ++i) {
723 const CryptoParams c1 = cd1->cryptos().at(i);
724 const CryptoParams c2 = cd2->cryptos().at(i);
725 EXPECT_TRUE(c1.Matches(c2));
726 EXPECT_EQ(c1.key_params, c2.key_params);
727 EXPECT_EQ(c1.session_params, c2.session_params);
728 }
729 // protocol
730 EXPECT_EQ(cd1->protocol(), cd2->protocol());
731
732 // codecs
733 EXPECT_EQ(cd1->codecs(), cd2->codecs());
734
735 // bandwidth
736 EXPECT_EQ(cd1->bandwidth(), cd2->bandwidth());
737
738 // streams
739 EXPECT_EQ(cd1->streams(), cd2->streams());
740
741 // extmap
742 ASSERT_EQ(cd1->rtp_header_extensions().size(),
743 cd2->rtp_header_extensions().size());
744 for (size_t i = 0; i< cd1->rtp_header_extensions().size(); ++i) {
745 const RtpHeaderExtension ext1 = cd1->rtp_header_extensions().at(i);
746 const RtpHeaderExtension ext2 = cd2->rtp_header_extensions().at(i);
747 EXPECT_EQ(ext1.uri, ext2.uri);
748 EXPECT_EQ(ext1.id, ext2.id);
749 }
750
751 // buffered mode latency
752 EXPECT_EQ(cd1->buffered_mode_latency(), cd2->buffered_mode_latency());
753 }
754
755
756 void CompareSessionDescription(const SessionDescription& desc1,
757 const SessionDescription& desc2) {
758 // Compare content descriptions.
759 if (desc1.contents().size() != desc2.contents().size()) {
760 ADD_FAILURE();
761 return;
762 }
763 for (size_t i = 0 ; i < desc1.contents().size(); ++i) {
764 const cricket::ContentInfo& c1 = desc1.contents().at(i);
765 const cricket::ContentInfo& c2 = desc2.contents().at(i);
766 // content name
767 EXPECT_EQ(c1.name, c2.name);
768 // content type
769 // Note, ASSERT will return from the function, but will not stop the test.
770 ASSERT_EQ(c1.type, c2.type);
771
772 ASSERT_EQ(IsAudioContent(&c1), IsAudioContent(&c2));
773 if (IsAudioContent(&c1)) {
774 const AudioContentDescription* acd1 =
775 static_cast<const AudioContentDescription*>(c1.description);
776 const AudioContentDescription* acd2 =
777 static_cast<const AudioContentDescription*>(c2.description);
778 CompareMediaContentDescription<AudioContentDescription>(acd1, acd2);
779 }
780
781 ASSERT_EQ(IsVideoContent(&c1), IsVideoContent(&c2));
782 if (IsVideoContent(&c1)) {
783 const VideoContentDescription* vcd1 =
784 static_cast<const VideoContentDescription*>(c1.description);
785 const VideoContentDescription* vcd2 =
786 static_cast<const VideoContentDescription*>(c2.description);
787 CompareMediaContentDescription<VideoContentDescription>(vcd1, vcd2);
788 }
789
790 ASSERT_EQ(IsDataContent(&c1), IsDataContent(&c2));
791 if (IsDataContent(&c1)) {
792 const DataContentDescription* dcd1 =
793 static_cast<const DataContentDescription*>(c1.description);
794 const DataContentDescription* dcd2 =
795 static_cast<const DataContentDescription*>(c2.description);
796 CompareMediaContentDescription<DataContentDescription>(dcd1, dcd2);
797 }
798 }
799
800 // group
801 const cricket::ContentGroups groups1 = desc1.groups();
802 const cricket::ContentGroups groups2 = desc2.groups();
803 EXPECT_EQ(groups1.size(), groups1.size());
804 if (groups1.size() != groups2.size()) {
805 ADD_FAILURE();
806 return;
807 }
808 for (size_t i = 0; i < groups1.size(); ++i) {
809 const cricket::ContentGroup group1 = groups1.at(i);
810 const cricket::ContentGroup group2 = groups2.at(i);
811 EXPECT_EQ(group1.semantics(), group2.semantics());
812 const cricket::ContentNames names1 = group1.content_names();
813 const cricket::ContentNames names2 = group2.content_names();
814 EXPECT_EQ(names1.size(), names2.size());
815 if (names1.size() != names2.size()) {
816 ADD_FAILURE();
817 return;
818 }
819 cricket::ContentNames::const_iterator iter1 = names1.begin();
820 cricket::ContentNames::const_iterator iter2 = names2.begin();
821 while (iter1 != names1.end()) {
822 EXPECT_EQ(*iter1++, *iter2++);
823 }
824 }
825
826 // transport info
827 const cricket::TransportInfos transports1 = desc1.transport_infos();
828 const cricket::TransportInfos transports2 = desc2.transport_infos();
829 EXPECT_EQ(transports1.size(), transports2.size());
830 if (transports1.size() != transports2.size()) {
831 ADD_FAILURE();
832 return;
833 }
834 for (size_t i = 0; i < transports1.size(); ++i) {
835 const cricket::TransportInfo transport1 = transports1.at(i);
836 const cricket::TransportInfo transport2 = transports2.at(i);
837 EXPECT_EQ(transport1.content_name, transport2.content_name);
838 EXPECT_EQ(transport1.description.transport_type,
839 transport2.description.transport_type);
840 EXPECT_EQ(transport1.description.ice_ufrag,
841 transport2.description.ice_ufrag);
842 EXPECT_EQ(transport1.description.ice_pwd,
843 transport2.description.ice_pwd);
844 if (transport1.description.identity_fingerprint) {
845 EXPECT_EQ(*transport1.description.identity_fingerprint,
846 *transport2.description.identity_fingerprint);
847 } else {
848 EXPECT_EQ(transport1.description.identity_fingerprint.get(),
849 transport2.description.identity_fingerprint.get());
850 }
851 EXPECT_EQ(transport1.description.transport_options,
852 transport2.description.transport_options);
853 EXPECT_TRUE(CompareCandidates(transport1.description.candidates,
854 transport2.description.candidates));
855 }
856 }
857
858 bool CompareCandidates(const Candidates& cs1, const Candidates& cs2) {
859 EXPECT_EQ(cs1.size(), cs2.size());
860 if (cs1.size() != cs2.size())
861 return false;
862 for (size_t i = 0; i< cs1.size(); ++i) {
863 const Candidate c1 = cs1.at(i);
864 const Candidate c2 = cs2.at(i);
865 EXPECT_TRUE(c1.IsEquivalent(c2));
866 }
867 return true;
868 }
869
870 bool CompareSessionDescription(
871 const JsepSessionDescription& desc1,
872 const JsepSessionDescription& desc2) {
873 EXPECT_EQ(desc1.session_id(), desc2.session_id());
874 EXPECT_EQ(desc1.session_version(), desc2.session_version());
875 CompareSessionDescription(*desc1.description(), *desc2.description());
876 if (desc1.number_of_mediasections() != desc2.number_of_mediasections())
877 return false;
878 for (size_t i = 0; i < desc1.number_of_mediasections(); ++i) {
879 const IceCandidateCollection* cc1 = desc1.candidates(i);
880 const IceCandidateCollection* cc2 = desc2.candidates(i);
881 if (cc1->count() != cc2->count())
882 return false;
883 for (size_t j = 0; j < cc1->count(); ++j) {
884 const IceCandidateInterface* c1 = cc1->at(j);
885 const IceCandidateInterface* c2 = cc2->at(j);
886 EXPECT_EQ(c1->sdp_mid(), c2->sdp_mid());
887 EXPECT_EQ(c1->sdp_mline_index(), c2->sdp_mline_index());
888 EXPECT_TRUE(c1->candidate().IsEquivalent(c2->candidate()));
889 }
890 }
891 return true;
892 }
893
894 // Disable the ice-ufrag and ice-pwd in given |sdp| message by replacing
895 // them with invalid keywords so that the parser will just ignore them.
896 bool RemoveCandidateUfragPwd(std::string* sdp) {
897 const char ice_ufrag[] = "a=ice-ufrag";
898 const char ice_ufragx[] = "a=xice-ufrag";
899 const char ice_pwd[] = "a=ice-pwd";
900 const char ice_pwdx[] = "a=xice-pwd";
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000901 rtc::replace_substrs(ice_ufrag, strlen(ice_ufrag),
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000902 ice_ufragx, strlen(ice_ufragx), sdp);
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000903 rtc::replace_substrs(ice_pwd, strlen(ice_pwd),
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000904 ice_pwdx, strlen(ice_pwdx), sdp);
905 return true;
906 }
907
908 // Update the candidates in |jdesc| to use the given |ufrag| and |pwd|.
909 bool UpdateCandidateUfragPwd(JsepSessionDescription* jdesc, int mline_index,
910 const std::string& ufrag, const std::string& pwd) {
911 std::string content_name;
912 if (mline_index == 0) {
913 content_name = kAudioContentName;
914 } else if (mline_index == 1) {
915 content_name = kVideoContentName;
916 } else {
917 ASSERT(false);
918 }
919 TransportInfo transport_info(
920 content_name, TransportDescription(NS_JINGLE_ICE_UDP,
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +0000921 ufrag, pwd));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000922 SessionDescription* desc =
923 const_cast<SessionDescription*>(jdesc->description());
924 desc->RemoveTransportInfoByName(content_name);
925 EXPECT_TRUE(desc->AddTransportInfo(transport_info));
926 for (size_t i = 0; i < jdesc_.number_of_mediasections(); ++i) {
927 const IceCandidateCollection* cc = jdesc_.candidates(i);
928 for (size_t j = 0; j < cc->count(); ++j) {
929 if (cc->at(j)->sdp_mline_index() == mline_index) {
930 const_cast<Candidate&>(cc->at(j)->candidate()).set_username(
931 ufrag);
932 const_cast<Candidate&>(cc->at(j)->candidate()).set_password(
933 pwd);
934 }
935 }
936 }
937 return true;
938 }
939
940 void AddIceOptions(const std::string& content_name,
941 const std::vector<std::string>& transport_options) {
942 ASSERT_TRUE(desc_.GetTransportInfoByName(content_name) != NULL);
943 cricket::TransportInfo transport_info =
944 *(desc_.GetTransportInfoByName(content_name));
945 desc_.RemoveTransportInfoByName(content_name);
946 transport_info.description.transport_options = transport_options;
947 desc_.AddTransportInfo(transport_info);
948 }
949
950 void AddFingerprint() {
951 desc_.RemoveTransportInfoByName(kAudioContentName);
952 desc_.RemoveTransportInfoByName(kVideoContentName);
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000953 rtc::SSLFingerprint fingerprint(rtc::DIGEST_SHA_1,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000954 kIdentityDigest,
955 sizeof(kIdentityDigest));
956 EXPECT_TRUE(desc_.AddTransportInfo(
957 TransportInfo(kAudioContentName,
958 TransportDescription(NS_JINGLE_ICE_UDP,
959 std::vector<std::string>(),
960 kCandidateUfragVoice,
961 kCandidatePwdVoice,
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +0000962 cricket::ICEMODE_FULL,
963 cricket::CONNECTIONROLE_NONE,
964 &fingerprint, Candidates()))));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000965 EXPECT_TRUE(desc_.AddTransportInfo(
966 TransportInfo(kVideoContentName,
967 TransportDescription(NS_JINGLE_ICE_UDP,
968 std::vector<std::string>(),
969 kCandidateUfragVideo,
970 kCandidatePwdVideo,
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +0000971 cricket::ICEMODE_FULL,
972 cricket::CONNECTIONROLE_NONE,
973 &fingerprint, Candidates()))));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000974 }
975
976 void AddExtmap() {
977 audio_desc_ = static_cast<AudioContentDescription*>(
978 audio_desc_->Copy());
979 video_desc_ = static_cast<VideoContentDescription*>(
980 video_desc_->Copy());
981 audio_desc_->AddRtpHeaderExtension(
982 RtpHeaderExtension(kExtmapUri, kExtmapId));
983 video_desc_->AddRtpHeaderExtension(
984 RtpHeaderExtension(kExtmapUri, kExtmapId));
985 desc_.RemoveContentByName(kAudioContentName);
986 desc_.RemoveContentByName(kVideoContentName);
987 desc_.AddContent(kAudioContentName, NS_JINGLE_RTP, audio_desc_);
988 desc_.AddContent(kVideoContentName, NS_JINGLE_RTP, video_desc_);
989 }
990
991 void RemoveCryptos() {
992 audio_desc_->set_cryptos(std::vector<CryptoParams>());
993 video_desc_->set_cryptos(std::vector<CryptoParams>());
994 }
995
996 bool TestSerializeDirection(cricket::MediaContentDirection direction) {
997 audio_desc_->set_direction(direction);
998 video_desc_->set_direction(direction);
999 std::string new_sdp = kSdpFullString;
1000 ReplaceDirection(direction, &new_sdp);
1001
1002 if (!jdesc_.Initialize(desc_.Copy(),
1003 jdesc_.session_id(),
1004 jdesc_.session_version())) {
1005 return false;
1006 }
1007 std::string message = webrtc::SdpSerialize(jdesc_);
1008 EXPECT_EQ(new_sdp, message);
1009 return true;
1010 }
1011
1012 bool TestSerializeRejected(bool audio_rejected, bool video_rejected) {
1013 audio_desc_ = static_cast<AudioContentDescription*>(
1014 audio_desc_->Copy());
1015 video_desc_ = static_cast<VideoContentDescription*>(
1016 video_desc_->Copy());
1017 desc_.RemoveContentByName(kAudioContentName);
1018 desc_.RemoveContentByName(kVideoContentName);
1019 desc_.AddContent(kAudioContentName, NS_JINGLE_RTP, audio_rejected,
1020 audio_desc_);
1021 desc_.AddContent(kVideoContentName, NS_JINGLE_RTP, video_rejected,
1022 video_desc_);
1023 std::string new_sdp = kSdpFullString;
1024 ReplaceRejected(audio_rejected, video_rejected, &new_sdp);
1025
1026 if (!jdesc_.Initialize(desc_.Copy(),
1027 jdesc_.session_id(),
1028 jdesc_.session_version())) {
1029 return false;
1030 }
1031 std::string message = webrtc::SdpSerialize(jdesc_);
1032 EXPECT_EQ(new_sdp, message);
1033 return true;
1034 }
1035
1036 void AddSctpDataChannel() {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001037 rtc::scoped_ptr<DataContentDescription> data(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001038 new DataContentDescription());
1039 data_desc_ = data.get();
1040 data_desc_->set_protocol(cricket::kMediaProtocolDtlsSctp);
wu@webrtc.org78187522013-10-07 23:32:02 +00001041 DataCodec codec(cricket::kGoogleSctpDataCodecId,
1042 cricket::kGoogleSctpDataCodecName, 0);
1043 codec.SetParam(cricket::kCodecParamPort, kDefaultSctpPort);
1044 data_desc_->AddCodec(codec);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001045 desc_.AddContent(kDataContentName, NS_JINGLE_DRAFT_SCTP, data.release());
1046 EXPECT_TRUE(desc_.AddTransportInfo(
1047 TransportInfo(kDataContentName,
1048 TransportDescription(NS_JINGLE_ICE_UDP,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001049 kCandidateUfragData,
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001050 kCandidatePwdData))));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001051 }
1052
1053 void AddRtpDataChannel() {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001054 rtc::scoped_ptr<DataContentDescription> data(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001055 new DataContentDescription());
1056 data_desc_ = data.get();
1057
1058 data_desc_->AddCodec(DataCodec(101, "google-data", 1));
1059 StreamParams data_stream;
1060 data_stream.id = kDataChannelMsid;
1061 data_stream.cname = kDataChannelCname;
1062 data_stream.sync_label = kDataChannelLabel;
1063 data_stream.ssrcs.push_back(kDataChannelSsrc);
1064 data_desc_->AddStream(data_stream);
1065 data_desc_->AddCrypto(CryptoParams(
1066 1, "AES_CM_128_HMAC_SHA1_80",
1067 "inline:FvLcvU2P3ZWmQxgPAgcDu7Zl9vftYElFOjEzhWs5", ""));
1068 data_desc_->set_protocol(cricket::kMediaProtocolSavpf);
1069 desc_.AddContent(kDataContentName, NS_JINGLE_RTP, data.release());
1070 EXPECT_TRUE(desc_.AddTransportInfo(
1071 TransportInfo(kDataContentName,
1072 TransportDescription(NS_JINGLE_ICE_UDP,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001073 kCandidateUfragData,
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001074 kCandidatePwdData))));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001075 }
1076
1077 bool TestDeserializeDirection(cricket::MediaContentDirection direction) {
1078 std::string new_sdp = kSdpFullString;
1079 ReplaceDirection(direction, &new_sdp);
1080 JsepSessionDescription new_jdesc(kDummyString);
1081
1082 EXPECT_TRUE(SdpDeserialize(new_sdp, &new_jdesc));
1083
1084 audio_desc_->set_direction(direction);
1085 video_desc_->set_direction(direction);
1086 if (!jdesc_.Initialize(desc_.Copy(),
1087 jdesc_.session_id(),
1088 jdesc_.session_version())) {
1089 return false;
1090 }
1091 EXPECT_TRUE(CompareSessionDescription(jdesc_, new_jdesc));
1092 return true;
1093 }
1094
1095 bool TestDeserializeRejected(bool audio_rejected, bool video_rejected) {
1096 std::string new_sdp = kSdpFullString;
1097 ReplaceRejected(audio_rejected, video_rejected, &new_sdp);
1098 JsepSessionDescription new_jdesc(JsepSessionDescription::kOffer);
1099
1100 EXPECT_TRUE(SdpDeserialize(new_sdp, &new_jdesc));
1101 audio_desc_ = static_cast<AudioContentDescription*>(
1102 audio_desc_->Copy());
1103 video_desc_ = static_cast<VideoContentDescription*>(
1104 video_desc_->Copy());
1105 desc_.RemoveContentByName(kAudioContentName);
1106 desc_.RemoveContentByName(kVideoContentName);
1107 desc_.AddContent(kAudioContentName, NS_JINGLE_RTP, audio_rejected,
1108 audio_desc_);
1109 desc_.AddContent(kVideoContentName, NS_JINGLE_RTP, video_rejected,
1110 video_desc_);
1111 if (!jdesc_.Initialize(desc_.Copy(),
1112 jdesc_.session_id(),
1113 jdesc_.session_version())) {
1114 return false;
1115 }
1116 EXPECT_TRUE(CompareSessionDescription(jdesc_, new_jdesc));
1117 return true;
1118 }
1119
1120 void TestDeserializeExtmap(bool session_level, bool media_level) {
1121 AddExtmap();
1122 JsepSessionDescription new_jdesc("dummy");
1123 ASSERT_TRUE(new_jdesc.Initialize(desc_.Copy(),
1124 jdesc_.session_id(),
1125 jdesc_.session_version()));
1126 JsepSessionDescription jdesc_with_extmap("dummy");
1127 std::string sdp_with_extmap = kSdpString;
1128 if (session_level) {
1129 InjectAfter(kSessionTime, kExtmapWithDirectionAndAttribute,
1130 &sdp_with_extmap);
1131 }
1132 if (media_level) {
1133 InjectAfter(kAttributeIcePwdVoice, kExtmapWithDirectionAndAttribute,
1134 &sdp_with_extmap);
1135 InjectAfter(kAttributeIcePwdVideo, kExtmapWithDirectionAndAttribute,
1136 &sdp_with_extmap);
1137 }
1138 // The extmap can't be present at the same time in both session level and
1139 // media level.
1140 if (session_level && media_level) {
1141 SdpParseError error;
1142 EXPECT_FALSE(webrtc::SdpDeserialize(sdp_with_extmap,
1143 &jdesc_with_extmap, &error));
1144 EXPECT_NE(std::string::npos, error.description.find("a=extmap"));
1145 } else {
1146 EXPECT_TRUE(SdpDeserialize(sdp_with_extmap, &jdesc_with_extmap));
1147 EXPECT_TRUE(CompareSessionDescription(jdesc_with_extmap, new_jdesc));
1148 }
1149 }
1150
1151 void VerifyCodecParameter(const cricket::CodecParameterMap& params,
1152 const std::string& name, int expected_value) {
1153 cricket::CodecParameterMap::const_iterator found = params.find(name);
1154 ASSERT_TRUE(found != params.end());
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001155 EXPECT_EQ(found->second, rtc::ToString<int>(expected_value));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001156 }
1157
1158 void TestDeserializeCodecParams(const CodecParams& params,
1159 JsepSessionDescription* jdesc_output) {
1160 std::string sdp =
1161 "v=0\r\n"
1162 "o=- 18446744069414584320 18446462598732840960 IN IP4 127.0.0.1\r\n"
1163 "s=-\r\n"
1164 "t=0 0\r\n"
1165 // Include semantics for WebRTC Media Streams since it is supported by
1166 // this parser, and will be added to the SDP when serializing a session
1167 // description.
1168 "a=msid-semantic: WMS\r\n"
1169 // Pl type 111 preferred.
1170 "m=audio 1 RTP/SAVPF 111 104 103 102\r\n"
1171 // Pltype 111 listed before 103 and 104 in the map.
1172 "a=rtpmap:111 opus/48000/2\r\n"
1173 // Pltype 103 listed before 104.
1174 "a=rtpmap:103 ISAC/16000\r\n"
1175 "a=rtpmap:104 CELT/32000/2\r\n"
1176 "a=rtpmap:102 ISAC/32000/1\r\n"
mallinath@webrtc.orga5506692013-08-12 21:18:15 +00001177 "a=fmtp:111 0-15,66,70\r\n"
1178 "a=fmtp:111 ";
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001179 std::ostringstream os;
mallinath@webrtc.orga5506692013-08-12 21:18:15 +00001180 os << "minptime=" << params.min_ptime
1181 << "; stereo=" << params.stereo
1182 << "; sprop-stereo=" << params.sprop_stereo
1183 << "; useinbandfec=" << params.useinband
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001184 << " maxaveragebitrate=" << params.maxaveragebitrate << "\r\n"
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001185 << "a=ptime:" << params.ptime << "\r\n"
1186 << "a=maxptime:" << params.max_ptime << "\r\n";
1187 sdp += os.str();
1188
stefan@webrtc.org85d27942014-06-09 12:51:39 +00001189 os.clear();
1190 os.str("");
1191 // Pl type 100 preferred.
1192 os << "m=video 1 RTP/SAVPF 99 95\r\n"
1193 << "a=rtpmap:99 VP8/90000\r\n"
1194 << "a=rtpmap:95 RTX/90000\r\n"
1195 << "a=fmtp:95 apt=99;rtx-time=1000\r\n";
1196 sdp += os.str();
1197
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001198 // Deserialize
1199 SdpParseError error;
1200 EXPECT_TRUE(webrtc::SdpDeserialize(sdp, jdesc_output, &error));
1201
1202 const ContentInfo* ac = GetFirstAudioContent(jdesc_output->description());
1203 ASSERT_TRUE(ac != NULL);
1204 const AudioContentDescription* acd =
1205 static_cast<const AudioContentDescription*>(ac->description);
1206 ASSERT_FALSE(acd->codecs().empty());
1207 cricket::AudioCodec opus = acd->codecs()[0];
1208 EXPECT_EQ("opus", opus.name);
1209 EXPECT_EQ(111, opus.id);
1210 VerifyCodecParameter(opus.params, "minptime", params.min_ptime);
1211 VerifyCodecParameter(opus.params, "stereo", params.stereo);
1212 VerifyCodecParameter(opus.params, "sprop-stereo", params.sprop_stereo);
1213 VerifyCodecParameter(opus.params, "useinbandfec", params.useinband);
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001214 VerifyCodecParameter(opus.params, "maxaveragebitrate",
1215 params.maxaveragebitrate);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001216 for (size_t i = 0; i < acd->codecs().size(); ++i) {
1217 cricket::AudioCodec codec = acd->codecs()[i];
1218 VerifyCodecParameter(codec.params, "ptime", params.ptime);
1219 VerifyCodecParameter(codec.params, "maxptime", params.max_ptime);
1220 if (codec.name == "ISAC") {
1221 if (codec.clockrate == 16000) {
1222 EXPECT_EQ(32000, codec.bitrate);
1223 } else {
1224 EXPECT_EQ(56000, codec.bitrate);
1225 }
1226 }
1227 }
stefan@webrtc.org85d27942014-06-09 12:51:39 +00001228
1229 const ContentInfo* vc = GetFirstVideoContent(jdesc_output->description());
1230 ASSERT_TRUE(vc != NULL);
1231 const VideoContentDescription* vcd =
1232 static_cast<const VideoContentDescription*>(vc->description);
1233 ASSERT_FALSE(vcd->codecs().empty());
1234 cricket::VideoCodec vp8 = vcd->codecs()[0];
1235 EXPECT_EQ("VP8", vp8.name);
1236 EXPECT_EQ(99, vp8.id);
1237 cricket::VideoCodec rtx = vcd->codecs()[1];
1238 EXPECT_EQ("RTX", rtx.name);
1239 EXPECT_EQ(95, rtx.id);
1240 VerifyCodecParameter(rtx.params, "apt", vp8.id);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001241 }
1242
1243 void TestDeserializeRtcpFb(JsepSessionDescription* jdesc_output,
1244 bool use_wildcard) {
1245 std::string sdp =
1246 "v=0\r\n"
1247 "o=- 18446744069414584320 18446462598732840960 IN IP4 127.0.0.1\r\n"
1248 "s=-\r\n"
1249 "t=0 0\r\n"
1250 // Include semantics for WebRTC Media Streams since it is supported by
1251 // this parser, and will be added to the SDP when serializing a session
1252 // description.
1253 "a=msid-semantic: WMS\r\n"
1254 "m=audio 1 RTP/SAVPF 111\r\n"
1255 "a=rtpmap:111 opus/48000/2\r\n"
1256 "a=rtcp-fb:111 nack\r\n"
1257 "m=video 3457 RTP/SAVPF 101\r\n"
1258 "a=rtpmap:101 VP8/90000\r\n"
1259 "a=rtcp-fb:101 nack\r\n"
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +00001260 "a=rtcp-fb:101 nack pli\r\n"
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001261 "a=rtcp-fb:101 goog-remb\r\n"
1262 "a=rtcp-fb:101 ccm fir\r\n";
1263 std::ostringstream os;
1264 os << "a=rtcp-fb:" << (use_wildcard ? "*" : "101") << " ccm fir\r\n";
1265 sdp += os.str();
1266 // Deserialize
1267 SdpParseError error;
1268 EXPECT_TRUE(webrtc::SdpDeserialize(sdp, jdesc_output, &error));
1269 const ContentInfo* ac = GetFirstAudioContent(jdesc_output->description());
1270 ASSERT_TRUE(ac != NULL);
1271 const AudioContentDescription* acd =
1272 static_cast<const AudioContentDescription*>(ac->description);
1273 ASSERT_FALSE(acd->codecs().empty());
1274 cricket::AudioCodec opus = acd->codecs()[0];
1275 EXPECT_EQ(111, opus.id);
1276 EXPECT_TRUE(opus.HasFeedbackParam(
1277 cricket::FeedbackParam(cricket::kRtcpFbParamNack,
1278 cricket::kParamValueEmpty)));
1279
1280 const ContentInfo* vc = GetFirstVideoContent(jdesc_output->description());
1281 ASSERT_TRUE(vc != NULL);
1282 const VideoContentDescription* vcd =
1283 static_cast<const VideoContentDescription*>(vc->description);
1284 ASSERT_FALSE(vcd->codecs().empty());
1285 cricket::VideoCodec vp8 = vcd->codecs()[0];
1286 EXPECT_STREQ(webrtc::JsepSessionDescription::kDefaultVideoCodecName,
1287 vp8.name.c_str());
1288 EXPECT_EQ(101, vp8.id);
1289 EXPECT_TRUE(vp8.HasFeedbackParam(
1290 cricket::FeedbackParam(cricket::kRtcpFbParamNack,
1291 cricket::kParamValueEmpty)));
1292 EXPECT_TRUE(vp8.HasFeedbackParam(
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +00001293 cricket::FeedbackParam(cricket::kRtcpFbParamNack,
1294 cricket::kRtcpFbNackParamPli)));
1295 EXPECT_TRUE(vp8.HasFeedbackParam(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001296 cricket::FeedbackParam(cricket::kRtcpFbParamRemb,
1297 cricket::kParamValueEmpty)));
1298 EXPECT_TRUE(vp8.HasFeedbackParam(
1299 cricket::FeedbackParam(cricket::kRtcpFbParamCcm,
1300 cricket::kRtcpFbCcmParamFir)));
1301 }
1302
1303 // Two SDP messages can mean the same thing but be different strings, e.g.
1304 // some of the lines can be serialized in different order.
1305 // However, a deserialized description can be compared field by field and has
1306 // no order. If deserializer has already been tested, serializing then
1307 // deserializing and comparing JsepSessionDescription will test
1308 // the serializer sufficiently.
1309 void TestSerialize(const JsepSessionDescription& jdesc) {
1310 std::string message = webrtc::SdpSerialize(jdesc);
1311 JsepSessionDescription jdesc_output_des(kDummyString);
1312 SdpParseError error;
1313 EXPECT_TRUE(webrtc::SdpDeserialize(message, &jdesc_output_des, &error));
1314 EXPECT_TRUE(CompareSessionDescription(jdesc, jdesc_output_des));
1315 }
1316
1317 protected:
1318 SessionDescription desc_;
1319 AudioContentDescription* audio_desc_;
1320 VideoContentDescription* video_desc_;
1321 DataContentDescription* data_desc_;
1322 Candidates candidates_;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001323 rtc::scoped_ptr<IceCandidateInterface> jcandidate_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001324 JsepSessionDescription jdesc_;
1325};
1326
1327void TestMismatch(const std::string& string1, const std::string& string2) {
1328 int position = 0;
1329 for (size_t i = 0; i < string1.length() && i < string2.length(); ++i) {
1330 if (string1.c_str()[i] != string2.c_str()[i]) {
henrike@webrtc.org28654cb2013-07-22 21:07:49 +00001331 position = static_cast<int>(i);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001332 break;
1333 }
1334 }
1335 EXPECT_EQ(0, position) << "Strings mismatch at the " << position
1336 << " character\n"
1337 << " 1: " << string1.substr(position, 20) << "\n"
1338 << " 2: " << string2.substr(position, 20) << "\n";
1339}
1340
1341std::string GetLine(const std::string& message,
1342 const std::string& session_description_name) {
1343 size_t start = message.find(session_description_name);
1344 if (std::string::npos == start) {
1345 return "";
1346 }
1347 size_t stop = message.find("\r\n", start);
1348 if (std::string::npos == stop) {
1349 return "";
1350 }
1351 if (stop <= start) {
1352 return "";
1353 }
1354 return message.substr(start, stop - start);
1355}
1356
1357TEST_F(WebRtcSdpTest, SerializeSessionDescription) {
1358 // SessionDescription with desc and candidates.
1359 std::string message = webrtc::SdpSerialize(jdesc_);
1360 TestMismatch(std::string(kSdpFullString), message);
1361}
1362
1363TEST_F(WebRtcSdpTest, SerializeSessionDescriptionEmpty) {
1364 JsepSessionDescription jdesc_empty(kDummyString);
1365 EXPECT_EQ("", webrtc::SdpSerialize(jdesc_empty));
1366}
1367
1368// This tests serialization of SDP with a=crypto and a=fingerprint, as would be
1369// the case in a DTLS offer.
1370TEST_F(WebRtcSdpTest, SerializeSessionDescriptionWithFingerprint) {
1371 AddFingerprint();
1372 JsepSessionDescription jdesc_with_fingerprint(kDummyString);
1373 ASSERT_TRUE(jdesc_with_fingerprint.Initialize(desc_.Copy(),
1374 kSessionId, kSessionVersion));
1375 std::string message = webrtc::SdpSerialize(jdesc_with_fingerprint);
1376
1377 std::string sdp_with_fingerprint = kSdpString;
1378 InjectAfter(kAttributeIcePwdVoice,
1379 kFingerprint, &sdp_with_fingerprint);
1380 InjectAfter(kAttributeIcePwdVideo,
1381 kFingerprint, &sdp_with_fingerprint);
1382
1383 EXPECT_EQ(sdp_with_fingerprint, message);
1384}
1385
1386// This tests serialization of SDP with a=fingerprint with no a=crypto, as would
1387// be the case in a DTLS answer.
1388TEST_F(WebRtcSdpTest, SerializeSessionDescriptionWithFingerprintNoCryptos) {
1389 AddFingerprint();
1390 RemoveCryptos();
1391 JsepSessionDescription jdesc_with_fingerprint(kDummyString);
1392 ASSERT_TRUE(jdesc_with_fingerprint.Initialize(desc_.Copy(),
1393 kSessionId, kSessionVersion));
1394 std::string message = webrtc::SdpSerialize(jdesc_with_fingerprint);
1395
1396 std::string sdp_with_fingerprint = kSdpString;
1397 Replace(kAttributeCryptoVoice, "", &sdp_with_fingerprint);
1398 Replace(kAttributeCryptoVideo, "", &sdp_with_fingerprint);
1399 InjectAfter(kAttributeIcePwdVoice,
1400 kFingerprint, &sdp_with_fingerprint);
1401 InjectAfter(kAttributeIcePwdVideo,
1402 kFingerprint, &sdp_with_fingerprint);
1403
1404 EXPECT_EQ(sdp_with_fingerprint, message);
1405}
1406
1407TEST_F(WebRtcSdpTest, SerializeSessionDescriptionWithoutCandidates) {
1408 // JsepSessionDescription with desc but without candidates.
1409 JsepSessionDescription jdesc_no_candidates(kDummyString);
1410 ASSERT_TRUE(jdesc_no_candidates.Initialize(desc_.Copy(),
1411 kSessionId, kSessionVersion));
1412 std::string message = webrtc::SdpSerialize(jdesc_no_candidates);
1413 EXPECT_EQ(std::string(kSdpString), message);
1414}
1415
1416TEST_F(WebRtcSdpTest, SerializeSessionDescriptionWithBundle) {
1417 ContentGroup group(cricket::GROUP_TYPE_BUNDLE);
1418 group.AddContentName(kAudioContentName);
1419 group.AddContentName(kVideoContentName);
1420 desc_.AddGroup(group);
1421 ASSERT_TRUE(jdesc_.Initialize(desc_.Copy(),
1422 jdesc_.session_id(),
1423 jdesc_.session_version()));
1424 std::string message = webrtc::SdpSerialize(jdesc_);
1425 std::string sdp_with_bundle = kSdpFullString;
1426 InjectAfter(kSessionTime,
1427 "a=group:BUNDLE audio_content_name video_content_name\r\n",
1428 &sdp_with_bundle);
1429 EXPECT_EQ(sdp_with_bundle, message);
1430}
1431
1432TEST_F(WebRtcSdpTest, SerializeSessionDescriptionWithBandwidth) {
1433 VideoContentDescription* vcd = static_cast<VideoContentDescription*>(
1434 GetFirstVideoContent(&desc_)->description);
1435 vcd->set_bandwidth(100 * 1000);
1436 AudioContentDescription* acd = static_cast<AudioContentDescription*>(
1437 GetFirstAudioContent(&desc_)->description);
1438 acd->set_bandwidth(50 * 1000);
1439 ASSERT_TRUE(jdesc_.Initialize(desc_.Copy(),
1440 jdesc_.session_id(),
1441 jdesc_.session_version()));
1442 std::string message = webrtc::SdpSerialize(jdesc_);
1443 std::string sdp_with_bandwidth = kSdpFullString;
wu@webrtc.org4c3e9912014-07-16 21:03:13 +00001444 InjectAfter("c=IN IP4 74.125.224.39\r\n",
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001445 "b=AS:100\r\n",
1446 &sdp_with_bandwidth);
wu@webrtc.org4c3e9912014-07-16 21:03:13 +00001447 InjectAfter("c=IN IP4 74.125.127.126\r\n",
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001448 "b=AS:50\r\n",
1449 &sdp_with_bandwidth);
1450 EXPECT_EQ(sdp_with_bandwidth, message);
1451}
1452
1453TEST_F(WebRtcSdpTest, SerializeSessionDescriptionWithIceOptions) {
1454 std::vector<std::string> transport_options;
1455 transport_options.push_back(kIceOption1);
1456 transport_options.push_back(kIceOption3);
1457 AddIceOptions(kAudioContentName, transport_options);
1458 transport_options.clear();
1459 transport_options.push_back(kIceOption2);
1460 transport_options.push_back(kIceOption3);
1461 AddIceOptions(kVideoContentName, transport_options);
1462 ASSERT_TRUE(jdesc_.Initialize(desc_.Copy(),
1463 jdesc_.session_id(),
1464 jdesc_.session_version()));
1465 std::string message = webrtc::SdpSerialize(jdesc_);
1466 std::string sdp_with_ice_options = kSdpFullString;
1467 InjectAfter(kAttributeIcePwdVoice,
1468 "a=ice-options:iceoption1 iceoption3\r\n",
1469 &sdp_with_ice_options);
1470 InjectAfter(kAttributeIcePwdVideo,
1471 "a=ice-options:iceoption2 iceoption3\r\n",
1472 &sdp_with_ice_options);
1473 EXPECT_EQ(sdp_with_ice_options, message);
1474}
1475
1476TEST_F(WebRtcSdpTest, SerializeSessionDescriptionWithRecvOnlyContent) {
1477 EXPECT_TRUE(TestSerializeDirection(cricket::MD_RECVONLY));
1478}
1479
1480TEST_F(WebRtcSdpTest, SerializeSessionDescriptionWithSendOnlyContent) {
1481 EXPECT_TRUE(TestSerializeDirection(cricket::MD_SENDONLY));
1482}
1483
1484TEST_F(WebRtcSdpTest, SerializeSessionDescriptionWithInactiveContent) {
1485 EXPECT_TRUE(TestSerializeDirection(cricket::MD_INACTIVE));
1486}
1487
1488TEST_F(WebRtcSdpTest, SerializeSessionDescriptionWithAudioRejected) {
1489 EXPECT_TRUE(TestSerializeRejected(true, false));
1490}
1491
1492TEST_F(WebRtcSdpTest, SerializeSessionDescriptionWithVideoRejected) {
1493 EXPECT_TRUE(TestSerializeRejected(false, true));
1494}
1495
1496TEST_F(WebRtcSdpTest, SerializeSessionDescriptionWithAudioVideoRejected) {
1497 EXPECT_TRUE(TestSerializeRejected(true, true));
1498}
1499
1500TEST_F(WebRtcSdpTest, SerializeSessionDescriptionWithRtpDataChannel) {
1501 AddRtpDataChannel();
1502 JsepSessionDescription jsep_desc(kDummyString);
1503
1504 ASSERT_TRUE(jsep_desc.Initialize(desc_.Copy(), kSessionId, kSessionVersion));
1505 std::string message = webrtc::SdpSerialize(jsep_desc);
1506
1507 std::string expected_sdp = kSdpString;
1508 expected_sdp.append(kSdpRtpDataChannelString);
1509 EXPECT_EQ(expected_sdp, message);
1510}
1511
1512TEST_F(WebRtcSdpTest, SerializeSessionDescriptionWithSctpDataChannel) {
1513 AddSctpDataChannel();
1514 JsepSessionDescription jsep_desc(kDummyString);
1515
1516 ASSERT_TRUE(jsep_desc.Initialize(desc_.Copy(), kSessionId, kSessionVersion));
1517 std::string message = webrtc::SdpSerialize(jsep_desc);
1518
1519 std::string expected_sdp = kSdpString;
1520 expected_sdp.append(kSdpSctpDataChannelString);
1521 EXPECT_EQ(message, expected_sdp);
1522}
1523
jiayl@webrtc.org9c16c392014-05-01 18:30:30 +00001524TEST_F(WebRtcSdpTest, SerializeWithSctpDataChannelAndNewPort) {
1525 AddSctpDataChannel();
1526 JsepSessionDescription jsep_desc(kDummyString);
1527
1528 ASSERT_TRUE(jsep_desc.Initialize(desc_.Copy(), kSessionId, kSessionVersion));
1529 DataContentDescription* dcdesc = static_cast<DataContentDescription*>(
1530 jsep_desc.description()->GetContentDescriptionByName(kDataContentName));
1531
1532 const int kNewPort = 1234;
1533 cricket::DataCodec codec(
1534 cricket::kGoogleSctpDataCodecId, cricket::kGoogleSctpDataCodecName, 0);
1535 codec.SetParam(cricket::kCodecParamPort, kNewPort);
1536 dcdesc->AddOrReplaceCodec(codec);
1537
1538 std::string message = webrtc::SdpSerialize(jsep_desc);
1539
1540 std::string expected_sdp = kSdpString;
1541 expected_sdp.append(kSdpSctpDataChannelString);
1542
1543 char default_portstr[16];
1544 char new_portstr[16];
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001545 rtc::sprintfn(default_portstr, sizeof(default_portstr), "%d",
jiayl@webrtc.org9c16c392014-05-01 18:30:30 +00001546 kDefaultSctpPort);
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001547 rtc::sprintfn(new_portstr, sizeof(new_portstr), "%d", kNewPort);
1548 rtc::replace_substrs(default_portstr, strlen(default_portstr),
jiayl@webrtc.org9c16c392014-05-01 18:30:30 +00001549 new_portstr, strlen(new_portstr),
1550 &expected_sdp);
1551
1552 EXPECT_EQ(expected_sdp, message);
1553}
1554
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001555TEST_F(WebRtcSdpTest, SerializeSessionDescriptionWithDataChannelAndBandwidth) {
1556 AddRtpDataChannel();
1557 data_desc_->set_bandwidth(100*1000);
1558 JsepSessionDescription jsep_desc(kDummyString);
1559
1560 ASSERT_TRUE(jsep_desc.Initialize(desc_.Copy(), kSessionId, kSessionVersion));
1561 std::string message = webrtc::SdpSerialize(jsep_desc);
1562
1563 std::string expected_sdp = kSdpString;
1564 expected_sdp.append(kSdpRtpDataChannelString);
1565 // We want to test that serializing data content ignores bandwidth
1566 // settings (it should always be the default). Thus, we don't do
1567 // the following:
sergeyu@chromium.orga59696b2013-09-13 23:48:58 +00001568 // TODO(pthatcher): We need to temporarily allow the SDP to control
1569 // this for backwards-compatibility. Once we don't need that any
1570 // more, remove this.
wu@webrtc.org4c3e9912014-07-16 21:03:13 +00001571 InjectAfter("m=application 1 RTP/SAVPF 101\r\nc=IN IP4 0.0.0.0\r\n",
sergeyu@chromium.orga59696b2013-09-13 23:48:58 +00001572 "b=AS:100\r\n",
1573 &expected_sdp);
1574 EXPECT_EQ(expected_sdp, message);
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001575}
1576
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001577TEST_F(WebRtcSdpTest, SerializeSessionDescriptionWithExtmap) {
1578 AddExtmap();
1579 JsepSessionDescription desc_with_extmap("dummy");
1580 ASSERT_TRUE(desc_with_extmap.Initialize(desc_.Copy(),
1581 kSessionId, kSessionVersion));
1582 std::string message = webrtc::SdpSerialize(desc_with_extmap);
1583
1584 std::string sdp_with_extmap = kSdpString;
1585 InjectAfter("a=mid:audio_content_name\r\n",
1586 kExtmap, &sdp_with_extmap);
1587 InjectAfter("a=mid:video_content_name\r\n",
1588 kExtmap, &sdp_with_extmap);
1589
1590 EXPECT_EQ(sdp_with_extmap, message);
1591}
1592
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +00001593TEST_F(WebRtcSdpTest, SerializeSessionDescriptionWithBufferLatency) {
1594 VideoContentDescription* vcd = static_cast<VideoContentDescription*>(
1595 GetFirstVideoContent(&desc_)->description);
1596 vcd->set_buffered_mode_latency(128);
1597
1598 ASSERT_TRUE(jdesc_.Initialize(desc_.Copy(),
1599 jdesc_.session_id(),
1600 jdesc_.session_version()));
1601 std::string message = webrtc::SdpSerialize(jdesc_);
1602 std::string sdp_with_buffer_latency = kSdpFullString;
1603 InjectAfter("a=rtpmap:120 VP8/90000\r\n",
1604 "a=x-google-buffer-latency:128\r\n",
1605 &sdp_with_buffer_latency);
1606 EXPECT_EQ(sdp_with_buffer_latency, message);
1607}
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001608
1609TEST_F(WebRtcSdpTest, SerializeCandidates) {
1610 std::string message = webrtc::SdpSerializeCandidate(*jcandidate_);
wu@webrtc.orgec9f5fb2014-06-24 17:05:10 +00001611 EXPECT_EQ(std::string(kRawCandidate), message);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001612}
1613
1614TEST_F(WebRtcSdpTest, DeserializeSessionDescription) {
1615 JsepSessionDescription jdesc(kDummyString);
1616 // Deserialize
1617 EXPECT_TRUE(SdpDeserialize(kSdpFullString, &jdesc));
1618 // Verify
1619 EXPECT_TRUE(CompareSessionDescription(jdesc_, jdesc));
1620}
1621
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001622TEST_F(WebRtcSdpTest, DeserializeSessionDescriptionWithoutMline) {
1623 JsepSessionDescription jdesc(kDummyString);
1624 const char kSdpWithoutMline[] =
1625 "v=0\r\n"
1626 "o=- 18446744069414584320 18446462598732840960 IN IP4 127.0.0.1\r\n"
1627 "s=-\r\n"
1628 "t=0 0\r\n"
1629 "a=msid-semantic: WMS local_stream_1 local_stream_2\r\n";
1630 // Deserialize
1631 EXPECT_TRUE(SdpDeserialize(kSdpWithoutMline, &jdesc));
1632 EXPECT_EQ(0u, jdesc.description()->contents().size());
1633}
1634
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001635TEST_F(WebRtcSdpTest, DeserializeSessionDescriptionWithoutCarriageReturn) {
1636 JsepSessionDescription jdesc(kDummyString);
1637 std::string sdp_without_carriage_return = kSdpFullString;
1638 Replace("\r\n", "\n", &sdp_without_carriage_return);
1639 // Deserialize
1640 EXPECT_TRUE(SdpDeserialize(sdp_without_carriage_return, &jdesc));
1641 // Verify
1642 EXPECT_TRUE(CompareSessionDescription(jdesc_, jdesc));
1643}
1644
1645TEST_F(WebRtcSdpTest, DeserializeSessionDescriptionWithoutCandidates) {
1646 // SessionDescription with desc but without candidates.
1647 JsepSessionDescription jdesc_no_candidates(kDummyString);
1648 ASSERT_TRUE(jdesc_no_candidates.Initialize(desc_.Copy(),
1649 kSessionId, kSessionVersion));
1650 JsepSessionDescription new_jdesc(kDummyString);
1651 EXPECT_TRUE(SdpDeserialize(kSdpString, &new_jdesc));
1652 EXPECT_TRUE(CompareSessionDescription(jdesc_no_candidates, new_jdesc));
1653}
1654
1655TEST_F(WebRtcSdpTest, DeserializeSessionDescriptionWithoutRtpmap) {
1656 static const char kSdpNoRtpmapString[] =
1657 "v=0\r\n"
1658 "o=- 11 22 IN IP4 127.0.0.1\r\n"
1659 "s=-\r\n"
1660 "t=0 0\r\n"
1661 "m=audio 49232 RTP/AVP 0 18 103\r\n"
1662 // Codec that doesn't appear in the m= line will be ignored.
1663 "a=rtpmap:104 CELT/32000/2\r\n"
1664 // The rtpmap line for static payload codec is optional.
1665 "a=rtpmap:18 G729/16000\r\n"
1666 "a=rtpmap:103 ISAC/16000\r\n";
1667
1668 JsepSessionDescription jdesc(kDummyString);
1669 EXPECT_TRUE(SdpDeserialize(kSdpNoRtpmapString, &jdesc));
1670 cricket::AudioContentDescription* audio =
1671 static_cast<AudioContentDescription*>(
1672 jdesc.description()->GetContentDescriptionByName(cricket::CN_AUDIO));
1673 AudioCodecs ref_codecs;
1674 // The codecs in the AudioContentDescription will be sorted by preference.
1675 ref_codecs.push_back(AudioCodec(0, "PCMU", 8000, 0, 1, 3));
1676 ref_codecs.push_back(AudioCodec(18, "G729", 16000, 0, 1, 2));
1677 ref_codecs.push_back(AudioCodec(103, "ISAC", 16000, 32000, 1, 1));
1678 EXPECT_EQ(ref_codecs, audio->codecs());
1679}
1680
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +00001681TEST_F(WebRtcSdpTest, DeserializeSessionDescriptionWithoutRtpmapButWithFmtp) {
1682 static const char kSdpNoRtpmapString[] =
1683 "v=0\r\n"
1684 "o=- 11 22 IN IP4 127.0.0.1\r\n"
1685 "s=-\r\n"
1686 "t=0 0\r\n"
1687 "m=audio 49232 RTP/AVP 18 103\r\n"
1688 "a=fmtp:18 annexb=yes\r\n"
1689 "a=rtpmap:103 ISAC/16000\r\n";
1690
1691 JsepSessionDescription jdesc(kDummyString);
1692 EXPECT_TRUE(SdpDeserialize(kSdpNoRtpmapString, &jdesc));
1693 cricket::AudioContentDescription* audio =
1694 static_cast<AudioContentDescription*>(
1695 jdesc.description()->GetContentDescriptionByName(cricket::CN_AUDIO));
1696
1697 cricket::AudioCodec g729 = audio->codecs()[0];
1698 EXPECT_EQ("G729", g729.name);
1699 EXPECT_EQ(8000, g729.clockrate);
1700 EXPECT_EQ(18, g729.id);
1701 cricket::CodecParameterMap::iterator found =
1702 g729.params.find("annexb");
1703 ASSERT_TRUE(found != g729.params.end());
1704 EXPECT_EQ(found->second, "yes");
1705
1706 cricket::AudioCodec isac = audio->codecs()[1];
1707 EXPECT_EQ("ISAC", isac.name);
1708 EXPECT_EQ(103, isac.id);
1709 EXPECT_EQ(16000, isac.clockrate);
1710}
1711
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001712// Ensure that we can deserialize SDP with a=fingerprint properly.
1713TEST_F(WebRtcSdpTest, DeserializeJsepSessionDescriptionWithFingerprint) {
1714 // Add a DTLS a=fingerprint attribute to our session description.
1715 AddFingerprint();
1716 JsepSessionDescription new_jdesc(kDummyString);
1717 ASSERT_TRUE(new_jdesc.Initialize(desc_.Copy(),
1718 jdesc_.session_id(),
1719 jdesc_.session_version()));
1720
1721 JsepSessionDescription jdesc_with_fingerprint(kDummyString);
1722 std::string sdp_with_fingerprint = kSdpString;
1723 InjectAfter(kAttributeIcePwdVoice, kFingerprint, &sdp_with_fingerprint);
1724 InjectAfter(kAttributeIcePwdVideo, kFingerprint, &sdp_with_fingerprint);
1725 EXPECT_TRUE(SdpDeserialize(sdp_with_fingerprint, &jdesc_with_fingerprint));
1726 EXPECT_TRUE(CompareSessionDescription(jdesc_with_fingerprint, new_jdesc));
1727}
1728
1729TEST_F(WebRtcSdpTest, DeserializeSessionDescriptionWithBundle) {
1730 JsepSessionDescription jdesc_with_bundle(kDummyString);
1731 std::string sdp_with_bundle = kSdpFullString;
1732 InjectAfter(kSessionTime,
1733 "a=group:BUNDLE audio_content_name video_content_name\r\n",
1734 &sdp_with_bundle);
1735 EXPECT_TRUE(SdpDeserialize(sdp_with_bundle, &jdesc_with_bundle));
1736 ContentGroup group(cricket::GROUP_TYPE_BUNDLE);
1737 group.AddContentName(kAudioContentName);
1738 group.AddContentName(kVideoContentName);
1739 desc_.AddGroup(group);
1740 ASSERT_TRUE(jdesc_.Initialize(desc_.Copy(),
1741 jdesc_.session_id(),
1742 jdesc_.session_version()));
1743 EXPECT_TRUE(CompareSessionDescription(jdesc_, jdesc_with_bundle));
1744}
1745
1746TEST_F(WebRtcSdpTest, DeserializeSessionDescriptionWithBandwidth) {
1747 JsepSessionDescription jdesc_with_bandwidth(kDummyString);
1748 std::string sdp_with_bandwidth = kSdpFullString;
1749 InjectAfter("a=mid:video_content_name\r\na=sendrecv\r\n",
1750 "b=AS:100\r\n",
1751 &sdp_with_bandwidth);
1752 InjectAfter("a=mid:audio_content_name\r\na=sendrecv\r\n",
1753 "b=AS:50\r\n",
1754 &sdp_with_bandwidth);
1755 EXPECT_TRUE(
1756 SdpDeserialize(sdp_with_bandwidth, &jdesc_with_bandwidth));
1757 VideoContentDescription* vcd = static_cast<VideoContentDescription*>(
1758 GetFirstVideoContent(&desc_)->description);
1759 vcd->set_bandwidth(100 * 1000);
1760 AudioContentDescription* acd = static_cast<AudioContentDescription*>(
1761 GetFirstAudioContent(&desc_)->description);
1762 acd->set_bandwidth(50 * 1000);
1763 ASSERT_TRUE(jdesc_.Initialize(desc_.Copy(),
1764 jdesc_.session_id(),
1765 jdesc_.session_version()));
1766 EXPECT_TRUE(CompareSessionDescription(jdesc_, jdesc_with_bandwidth));
1767}
1768
1769TEST_F(WebRtcSdpTest, DeserializeSessionDescriptionWithIceOptions) {
1770 JsepSessionDescription jdesc_with_ice_options(kDummyString);
1771 std::string sdp_with_ice_options = kSdpFullString;
1772 InjectAfter(kSessionTime,
1773 "a=ice-options:iceoption3\r\n",
1774 &sdp_with_ice_options);
1775 InjectAfter(kAttributeIcePwdVoice,
1776 "a=ice-options:iceoption1\r\n",
1777 &sdp_with_ice_options);
1778 InjectAfter(kAttributeIcePwdVideo,
1779 "a=ice-options:iceoption2\r\n",
1780 &sdp_with_ice_options);
1781 EXPECT_TRUE(SdpDeserialize(sdp_with_ice_options, &jdesc_with_ice_options));
1782 std::vector<std::string> transport_options;
1783 transport_options.push_back(kIceOption3);
1784 transport_options.push_back(kIceOption1);
1785 AddIceOptions(kAudioContentName, transport_options);
1786 transport_options.clear();
1787 transport_options.push_back(kIceOption3);
1788 transport_options.push_back(kIceOption2);
1789 AddIceOptions(kVideoContentName, transport_options);
1790 ASSERT_TRUE(jdesc_.Initialize(desc_.Copy(),
1791 jdesc_.session_id(),
1792 jdesc_.session_version()));
1793 EXPECT_TRUE(CompareSessionDescription(jdesc_, jdesc_with_ice_options));
1794}
1795
1796TEST_F(WebRtcSdpTest, DeserializeSessionDescriptionWithUfragPwd) {
1797 // Remove the original ice-ufrag and ice-pwd
1798 JsepSessionDescription jdesc_with_ufrag_pwd(kDummyString);
1799 std::string sdp_with_ufrag_pwd = kSdpFullString;
1800 EXPECT_TRUE(RemoveCandidateUfragPwd(&sdp_with_ufrag_pwd));
1801 // Add session level ufrag and pwd
1802 InjectAfter(kSessionTime,
1803 "a=ice-pwd:session+level+icepwd\r\n"
1804 "a=ice-ufrag:session+level+iceufrag\r\n",
1805 &sdp_with_ufrag_pwd);
1806 // Add media level ufrag and pwd for audio
1807 InjectAfter("a=mid:audio_content_name\r\n",
1808 "a=ice-pwd:media+level+icepwd\r\na=ice-ufrag:media+level+iceufrag\r\n",
1809 &sdp_with_ufrag_pwd);
1810 // Update the candidate ufrag and pwd to the expected ones.
1811 EXPECT_TRUE(UpdateCandidateUfragPwd(&jdesc_, 0,
1812 "media+level+iceufrag", "media+level+icepwd"));
1813 EXPECT_TRUE(UpdateCandidateUfragPwd(&jdesc_, 1,
1814 "session+level+iceufrag", "session+level+icepwd"));
1815 EXPECT_TRUE(SdpDeserialize(sdp_with_ufrag_pwd, &jdesc_with_ufrag_pwd));
1816 EXPECT_TRUE(CompareSessionDescription(jdesc_, jdesc_with_ufrag_pwd));
1817}
1818
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +00001819TEST_F(WebRtcSdpTest, DeserializeSessionDescriptionWithBufferLatency) {
1820 JsepSessionDescription jdesc_with_buffer_latency(kDummyString);
1821 std::string sdp_with_buffer_latency = kSdpFullString;
1822 InjectAfter("a=rtpmap:120 VP8/90000\r\n",
1823 "a=x-google-buffer-latency:128\r\n",
1824 &sdp_with_buffer_latency);
1825
1826 EXPECT_TRUE(
1827 SdpDeserialize(sdp_with_buffer_latency, &jdesc_with_buffer_latency));
1828 VideoContentDescription* vcd = static_cast<VideoContentDescription*>(
1829 GetFirstVideoContent(&desc_)->description);
1830 vcd->set_buffered_mode_latency(128);
1831 ASSERT_TRUE(jdesc_.Initialize(desc_.Copy(),
1832 jdesc_.session_id(),
1833 jdesc_.session_version()));
1834 EXPECT_TRUE(CompareSessionDescription(jdesc_, jdesc_with_buffer_latency));
1835}
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001836
1837TEST_F(WebRtcSdpTest, DeserializeSessionDescriptionWithRecvOnlyContent) {
1838 EXPECT_TRUE(TestDeserializeDirection(cricket::MD_RECVONLY));
1839}
1840
1841TEST_F(WebRtcSdpTest, DeserializeSessionDescriptionWithSendOnlyContent) {
1842 EXPECT_TRUE(TestDeserializeDirection(cricket::MD_SENDONLY));
1843}
1844
1845TEST_F(WebRtcSdpTest, DeserializeSessionDescriptionWithInactiveContent) {
1846 EXPECT_TRUE(TestDeserializeDirection(cricket::MD_INACTIVE));
1847}
1848
1849TEST_F(WebRtcSdpTest, DeserializeSessionDescriptionWithRejectedAudio) {
1850 EXPECT_TRUE(TestDeserializeRejected(true, false));
1851}
1852
1853TEST_F(WebRtcSdpTest, DeserializeSessionDescriptionWithRejectedVideo) {
1854 EXPECT_TRUE(TestDeserializeRejected(false, true));
1855}
1856
1857TEST_F(WebRtcSdpTest, DeserializeSessionDescriptionWithRejectedAudioVideo) {
1858 EXPECT_TRUE(TestDeserializeRejected(true, true));
1859}
1860
1861// Tests that we can still handle the sdp uses mslabel and label instead of
1862// msid for backward compatibility.
1863TEST_F(WebRtcSdpTest, DeserializeSessionDescriptionWithoutMsid) {
1864 JsepSessionDescription jdesc(kDummyString);
1865 std::string sdp_without_msid = kSdpFullString;
1866 Replace("msid", "xmsid", &sdp_without_msid);
1867 // Deserialize
1868 EXPECT_TRUE(SdpDeserialize(sdp_without_msid, &jdesc));
1869 // Verify
1870 EXPECT_TRUE(CompareSessionDescription(jdesc_, jdesc));
1871}
1872
1873TEST_F(WebRtcSdpTest, DeserializeCandidate) {
1874 JsepIceCandidate jcandidate(kDummyMid, kDummyIndex);
1875
1876 std::string sdp = kSdpOneCandidate;
1877 EXPECT_TRUE(SdpDeserializeCandidate(sdp, &jcandidate));
1878 EXPECT_EQ(kDummyMid, jcandidate.sdp_mid());
1879 EXPECT_EQ(kDummyIndex, jcandidate.sdp_mline_index());
1880 EXPECT_TRUE(jcandidate.candidate().IsEquivalent(jcandidate_->candidate()));
1881
1882 // Candidate line without generation extension.
1883 sdp = kSdpOneCandidate;
1884 Replace(" generation 2", "", &sdp);
1885 EXPECT_TRUE(SdpDeserializeCandidate(sdp, &jcandidate));
1886 EXPECT_EQ(kDummyMid, jcandidate.sdp_mid());
1887 EXPECT_EQ(kDummyIndex, jcandidate.sdp_mline_index());
1888 Candidate expected = jcandidate_->candidate();
1889 expected.set_generation(0);
1890 EXPECT_TRUE(jcandidate.candidate().IsEquivalent(expected));
1891
1892 // Multiple candidate lines.
1893 // Only the first line will be deserialized. The rest will be ignored.
1894 sdp = kSdpOneCandidate;
1895 sdp.append("a=candidate:1 2 tcp 1234 192.168.1.100 5678 typ host\r\n");
1896 EXPECT_TRUE(SdpDeserializeCandidate(sdp, &jcandidate));
1897 EXPECT_EQ(kDummyMid, jcandidate.sdp_mid());
1898 EXPECT_EQ(kDummyIndex, jcandidate.sdp_mline_index());
1899 EXPECT_TRUE(jcandidate.candidate().IsEquivalent(jcandidate_->candidate()));
1900}
1901
1902// This test verifies the deserialization of candidate-attribute
1903// as per RFC 5245. Candiate-attribute will be of the format
1904// candidate:<blah>. This format will be used when candidates
1905// are trickled.
1906TEST_F(WebRtcSdpTest, DeserializeRawCandidateAttribute) {
1907 JsepIceCandidate jcandidate(kDummyMid, kDummyIndex);
1908
1909 std::string candidate_attribute = kRawCandidate;
1910 EXPECT_TRUE(SdpDeserializeCandidate(candidate_attribute, &jcandidate));
1911 EXPECT_EQ(kDummyMid, jcandidate.sdp_mid());
1912 EXPECT_EQ(kDummyIndex, jcandidate.sdp_mline_index());
1913 EXPECT_TRUE(jcandidate.candidate().IsEquivalent(jcandidate_->candidate()));
1914 EXPECT_EQ(2u, jcandidate.candidate().generation());
1915
1916 // Candidate line without generation extension.
1917 candidate_attribute = kRawCandidate;
1918 Replace(" generation 2", "", &candidate_attribute);
1919 EXPECT_TRUE(SdpDeserializeCandidate(candidate_attribute, &jcandidate));
1920 EXPECT_EQ(kDummyMid, jcandidate.sdp_mid());
1921 EXPECT_EQ(kDummyIndex, jcandidate.sdp_mline_index());
1922 Candidate expected = jcandidate_->candidate();
1923 expected.set_generation(0);
1924 EXPECT_TRUE(jcandidate.candidate().IsEquivalent(expected));
1925
1926 // Candidate line without candidate:
1927 candidate_attribute = kRawCandidate;
1928 Replace("candidate:", "", &candidate_attribute);
1929 EXPECT_FALSE(SdpDeserializeCandidate(candidate_attribute, &jcandidate));
1930
1931 // Concatenating additional candidate. Expecting deserialization to fail.
1932 candidate_attribute = kRawCandidate;
1933 candidate_attribute.append("candidate:1 2 udp 1234 192.168.1.1 typ host");
1934 EXPECT_FALSE(SdpDeserializeCandidate(candidate_attribute, &jcandidate));
1935}
1936
1937TEST_F(WebRtcSdpTest, DeserializeSdpWithRtpDataChannels) {
1938 AddRtpDataChannel();
1939 JsepSessionDescription jdesc(kDummyString);
1940 ASSERT_TRUE(jdesc.Initialize(desc_.Copy(), kSessionId, kSessionVersion));
1941
1942 std::string sdp_with_data = kSdpString;
1943 sdp_with_data.append(kSdpRtpDataChannelString);
1944 JsepSessionDescription jdesc_output(kDummyString);
1945
1946 // Deserialize
1947 EXPECT_TRUE(SdpDeserialize(sdp_with_data, &jdesc_output));
1948 // Verify
1949 EXPECT_TRUE(CompareSessionDescription(jdesc, jdesc_output));
1950}
1951
1952TEST_F(WebRtcSdpTest, DeserializeSdpWithSctpDataChannels) {
1953 AddSctpDataChannel();
1954 JsepSessionDescription jdesc(kDummyString);
1955 ASSERT_TRUE(jdesc.Initialize(desc_.Copy(), kSessionId, kSessionVersion));
1956
1957 std::string sdp_with_data = kSdpString;
1958 sdp_with_data.append(kSdpSctpDataChannelString);
1959 JsepSessionDescription jdesc_output(kDummyString);
1960
1961 EXPECT_TRUE(SdpDeserialize(sdp_with_data, &jdesc_output));
1962 EXPECT_TRUE(CompareSessionDescription(jdesc, jdesc_output));
1963}
1964
henrike@webrtc.org571df2d2014-02-19 23:04:26 +00001965// For crbug/344475.
1966TEST_F(WebRtcSdpTest, DeserializeSdpWithCorruptedSctpDataChannels) {
1967 std::string sdp_with_data = kSdpString;
1968 sdp_with_data.append(kSdpSctpDataChannelString);
1969 // Remove the "\n" at the end.
1970 sdp_with_data = sdp_with_data.substr(0, sdp_with_data.size() - 1);
1971 JsepSessionDescription jdesc_output(kDummyString);
1972
1973 EXPECT_FALSE(SdpDeserialize(sdp_with_data, &jdesc_output));
1974 // No crash is a pass.
1975}
1976
wu@webrtc.org78187522013-10-07 23:32:02 +00001977TEST_F(WebRtcSdpTest, DeserializeSdpWithSctpDataChannelAndNewPort) {
1978 AddSctpDataChannel();
1979 const uint16 kUnusualSctpPort = 9556;
1980 char default_portstr[16];
1981 char unusual_portstr[16];
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001982 rtc::sprintfn(default_portstr, sizeof(default_portstr), "%d",
wu@webrtc.org78187522013-10-07 23:32:02 +00001983 kDefaultSctpPort);
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001984 rtc::sprintfn(unusual_portstr, sizeof(unusual_portstr), "%d",
wu@webrtc.org78187522013-10-07 23:32:02 +00001985 kUnusualSctpPort);
1986
jiayl@webrtc.org9c16c392014-05-01 18:30:30 +00001987 // First setup the expected JsepSessionDescription.
wu@webrtc.org78187522013-10-07 23:32:02 +00001988 JsepSessionDescription jdesc(kDummyString);
1989 // take our pre-built session description and change the SCTP port.
1990 cricket::SessionDescription* mutant = desc_.Copy();
1991 DataContentDescription* dcdesc = static_cast<DataContentDescription*>(
1992 mutant->GetContentDescriptionByName(kDataContentName));
1993 std::vector<cricket::DataCodec> codecs(dcdesc->codecs());
1994 EXPECT_EQ(codecs.size(), 1UL);
1995 EXPECT_EQ(codecs[0].id, cricket::kGoogleSctpDataCodecId);
1996 codecs[0].SetParam(cricket::kCodecParamPort, kUnusualSctpPort);
jiayl@webrtc.org9c16c392014-05-01 18:30:30 +00001997 dcdesc->set_codecs(codecs);
wu@webrtc.org78187522013-10-07 23:32:02 +00001998
1999 // note: mutant's owned by jdesc now.
2000 ASSERT_TRUE(jdesc.Initialize(mutant, kSessionId, kSessionVersion));
2001 mutant = NULL;
2002
jiayl@webrtc.org9c16c392014-05-01 18:30:30 +00002003 // Then get the deserialized JsepSessionDescription.
wu@webrtc.org78187522013-10-07 23:32:02 +00002004 std::string sdp_with_data = kSdpString;
2005 sdp_with_data.append(kSdpSctpDataChannelString);
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002006 rtc::replace_substrs(default_portstr, strlen(default_portstr),
wu@webrtc.org78187522013-10-07 23:32:02 +00002007 unusual_portstr, strlen(unusual_portstr),
2008 &sdp_with_data);
2009 JsepSessionDescription jdesc_output(kDummyString);
2010
2011 EXPECT_TRUE(SdpDeserialize(sdp_with_data, &jdesc_output));
2012 EXPECT_TRUE(CompareSessionDescription(jdesc, jdesc_output));
2013}
2014
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00002015TEST_F(WebRtcSdpTest, DeserializeSdpWithRtpDataChannelsAndBandwidth) {
2016 AddRtpDataChannel();
2017 JsepSessionDescription jdesc(kDummyString);
2018 // We want to test that deserializing data content ignores bandwidth
2019 // settings (it should always be the default). Thus, we don't do
2020 // the following:
sergeyu@chromium.orga59696b2013-09-13 23:48:58 +00002021 // TODO(pthatcher): We need to temporarily allow the SDP to control
2022 // this for backwards-compatibility. Once we don't need that any
2023 // more, remove this.
2024 DataContentDescription* dcd = static_cast<DataContentDescription*>(
2025 GetFirstDataContent(&desc_)->description);
2026 dcd->set_bandwidth(100 * 1000);
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00002027 ASSERT_TRUE(jdesc.Initialize(desc_.Copy(), kSessionId, kSessionVersion));
2028
2029 std::string sdp_with_bandwidth = kSdpString;
2030 sdp_with_bandwidth.append(kSdpRtpDataChannelString);
2031 InjectAfter("a=mid:data_content_name\r\n",
2032 "b=AS:100\r\n",
2033 &sdp_with_bandwidth);
2034 JsepSessionDescription jdesc_with_bandwidth(kDummyString);
2035
2036 EXPECT_TRUE(
2037 SdpDeserialize(sdp_with_bandwidth, &jdesc_with_bandwidth));
2038 EXPECT_TRUE(CompareSessionDescription(jdesc, jdesc_with_bandwidth));
2039}
2040
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002041TEST_F(WebRtcSdpTest, DeserializeSessionDescriptionWithSessionLevelExtmap) {
2042 TestDeserializeExtmap(true, false);
2043}
2044
2045TEST_F(WebRtcSdpTest, DeserializeSessionDescriptionWithMediaLevelExtmap) {
2046 TestDeserializeExtmap(false, true);
2047}
2048
2049TEST_F(WebRtcSdpTest, DeserializeSessionDescriptionWithInvalidExtmap) {
2050 TestDeserializeExtmap(true, true);
2051}
2052
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +00002053TEST_F(WebRtcSdpTest, DeserializeSessionDescriptionWithoutEndLineBreak) {
2054 JsepSessionDescription jdesc(kDummyString);
2055 std::string sdp = kSdpFullString;
2056 sdp = sdp.substr(0, sdp.size() - 2); // Remove \r\n at the end.
2057 // Deserialize
2058 SdpParseError error;
2059 EXPECT_FALSE(webrtc::SdpDeserialize(sdp, &jdesc, &error));
2060 const std::string lastline = "a=ssrc:6 label:video_track_id_3";
2061 EXPECT_EQ(lastline, error.line);
2062 EXPECT_EQ("Invalid SDP line.", error.description);
2063}
2064
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002065TEST_F(WebRtcSdpTest, DeserializeCandidateWithDifferentTransport) {
2066 JsepIceCandidate jcandidate(kDummyMid, kDummyIndex);
2067 std::string new_sdp = kSdpOneCandidate;
2068 Replace("udp", "unsupported_transport", &new_sdp);
2069 EXPECT_FALSE(SdpDeserializeCandidate(new_sdp, &jcandidate));
2070 new_sdp = kSdpOneCandidate;
2071 Replace("udp", "uDP", &new_sdp);
2072 EXPECT_TRUE(SdpDeserializeCandidate(new_sdp, &jcandidate));
2073 EXPECT_EQ(kDummyMid, jcandidate.sdp_mid());
2074 EXPECT_EQ(kDummyIndex, jcandidate.sdp_mline_index());
2075 EXPECT_TRUE(jcandidate.candidate().IsEquivalent(jcandidate_->candidate()));
2076}
2077
2078TEST_F(WebRtcSdpTest, DeserializeCandidateOldFormat) {
2079 JsepIceCandidate jcandidate(kDummyMid, kDummyIndex);
2080 EXPECT_TRUE(SdpDeserializeCandidate(kSdpOneCandidateOldFormat,&jcandidate));
2081 EXPECT_EQ(kDummyMid, jcandidate.sdp_mid());
2082 EXPECT_EQ(kDummyIndex, jcandidate.sdp_mline_index());
2083 Candidate ref_candidate = jcandidate_->candidate();
2084 ref_candidate.set_username("user_rtp");
2085 ref_candidate.set_password("password_rtp");
2086 EXPECT_TRUE(jcandidate.candidate().IsEquivalent(ref_candidate));
2087}
2088
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +00002089TEST_F(WebRtcSdpTest, DeserializeSdpWithConferenceFlag) {
2090 JsepSessionDescription jdesc(kDummyString);
2091
2092 // Deserialize
2093 EXPECT_TRUE(SdpDeserialize(kSdpConferenceString, &jdesc));
2094
2095 // Verify
2096 cricket::AudioContentDescription* audio =
2097 static_cast<AudioContentDescription*>(
2098 jdesc.description()->GetContentDescriptionByName(cricket::CN_AUDIO));
2099 EXPECT_TRUE(audio->conference_mode());
2100
2101 cricket::VideoContentDescription* video =
2102 static_cast<VideoContentDescription*>(
2103 jdesc.description()->GetContentDescriptionByName(cricket::CN_VIDEO));
2104 EXPECT_TRUE(video->conference_mode());
2105}
2106
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002107TEST_F(WebRtcSdpTest, DeserializeBrokenSdp) {
2108 const char kSdpDestroyer[] = "!@#$%^&";
2109 const char kSdpInvalidLine1[] = " =candidate";
2110 const char kSdpInvalidLine2[] = "a+candidate";
2111 const char kSdpInvalidLine3[] = "a= candidate";
2112 // Broken fingerprint.
2113 const char kSdpInvalidLine4[] = "a=fingerprint:sha-1 "
2114 "4AAD:B9:B1:3F:82:18:3B:54:02:12:DF:3E:5D:49:6B:19:E5:7C:AB";
2115 // Extra field.
2116 const char kSdpInvalidLine5[] = "a=fingerprint:sha-1 "
2117 "4A:AD:B9:B1:3F:82:18:3B:54:02:12:DF:3E:5D:49:6B:19:E5:7C:AB XXX";
2118 // Missing space.
2119 const char kSdpInvalidLine6[] = "a=fingerprint:sha-1"
2120 "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 +00002121 // MD5 is not allowed in fingerprints.
2122 const char kSdpInvalidLine7[] = "a=fingerprint:md5 "
2123 "4A:AD:B9:B1:3F:82:18:3B:54:02:12:DF:3E:5D:49:6B";
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002124
2125 // Broken session description
wu@webrtc.org5e760e72014-04-02 23:19:09 +00002126 ExpectParseFailure("v=", kSdpDestroyer);
2127 ExpectParseFailure("o=", kSdpDestroyer);
2128 ExpectParseFailure("s=-", kSdpDestroyer);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002129 // Broken time description
wu@webrtc.org5e760e72014-04-02 23:19:09 +00002130 ExpectParseFailure("t=", kSdpDestroyer);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002131
2132 // Broken media description
wu@webrtc.org5e760e72014-04-02 23:19:09 +00002133 ExpectParseFailure("m=audio", "c=IN IP4 74.125.224.39");
2134 ExpectParseFailure("m=video", kSdpDestroyer);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002135
2136 // Invalid lines
wu@webrtc.org5e760e72014-04-02 23:19:09 +00002137 ExpectParseFailure("a=candidate", kSdpInvalidLine1);
2138 ExpectParseFailure("a=candidate", kSdpInvalidLine2);
2139 ExpectParseFailure("a=candidate", kSdpInvalidLine3);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002140
2141 // Bogus fingerprint replacing a=sendrev. We selected this attribute
2142 // because it's orthogonal to what we are replacing and hence
2143 // safe.
wu@webrtc.org5e760e72014-04-02 23:19:09 +00002144 ExpectParseFailure("a=sendrecv", kSdpInvalidLine4);
2145 ExpectParseFailure("a=sendrecv", kSdpInvalidLine5);
2146 ExpectParseFailure("a=sendrecv", kSdpInvalidLine6);
2147 ExpectParseFailure("a=sendrecv", kSdpInvalidLine7);
2148}
2149
2150TEST_F(WebRtcSdpTest, DeserializeSdpWithInvalidAttributeValue) {
2151 // ssrc
2152 ExpectParseFailure("a=ssrc:1", "a=ssrc:badvalue");
2153 ExpectParseFailure("a=ssrc-group:FEC 5 6", "a=ssrc-group:FEC badvalue 6");
2154 // crypto
2155 ExpectParseFailure("a=crypto:1 ", "a=crypto:badvalue ");
2156 // rtpmap
2157 ExpectParseFailure("a=rtpmap:111 ", "a=rtpmap:badvalue ");
2158 ExpectParseFailure("opus/48000/2", "opus/badvalue/2");
2159 ExpectParseFailure("opus/48000/2", "opus/48000/badvalue");
2160 // candidate
2161 ExpectParseFailure("1 udp 2130706432", "badvalue udp 2130706432");
2162 ExpectParseFailure("1 udp 2130706432", "1 udp badvalue");
2163 ExpectParseFailure("192.168.1.5 1234", "192.168.1.5 badvalue");
2164 ExpectParseFailure("rport 2346", "rport badvalue");
2165 ExpectParseFailure("rport 2346 generation 2",
2166 "rport 2346 generation badvalue");
2167 // m line
2168 ExpectParseFailure("m=audio 2345 RTP/SAVPF 111 103 104",
2169 "m=audio 2345 RTP/SAVPF 111 badvalue 104");
2170
2171 // bandwidth
2172 ExpectParseFailureWithNewLines("a=mid:video_content_name\r\n",
2173 "b=AS:badvalue\r\n",
2174 "b=AS:badvalue");
2175 // rtcp-fb
2176 ExpectParseFailureWithNewLines("a=mid:video_content_name\r\n",
2177 "a=rtcp-fb:badvalue nack\r\n",
2178 "a=rtcp-fb:badvalue nack");
2179 // extmap
2180 ExpectParseFailureWithNewLines("a=mid:video_content_name\r\n",
2181 "a=extmap:badvalue http://example.com\r\n",
2182 "a=extmap:badvalue http://example.com");
2183 // x-google-buffer-latency
2184 ExpectParseFailureWithNewLines("a=mid:video_content_name\r\n",
2185 "a=x-google-buffer-latency:badvalue\r\n",
2186 "a=x-google-buffer-latency:badvalue");
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002187}
2188
2189TEST_F(WebRtcSdpTest, DeserializeSdpWithReorderedPltypes) {
2190 JsepSessionDescription jdesc_output(kDummyString);
2191
2192 const char kSdpWithReorderedPlTypesString[] =
2193 "v=0\r\n"
2194 "o=- 18446744069414584320 18446462598732840960 IN IP4 127.0.0.1\r\n"
2195 "s=-\r\n"
2196 "t=0 0\r\n"
2197 "m=audio 1 RTP/SAVPF 104 103\r\n" // Pl type 104 preferred.
2198 "a=rtpmap:111 opus/48000/2\r\n" // Pltype 111 listed before 103 and 104
2199 // in the map.
2200 "a=rtpmap:103 ISAC/16000\r\n" // Pltype 103 listed before 104 in the map.
2201 "a=rtpmap:104 CELT/32000/2\r\n";
2202
2203 // Deserialize
2204 EXPECT_TRUE(SdpDeserialize(kSdpWithReorderedPlTypesString, &jdesc_output));
2205
2206 const ContentInfo* ac = GetFirstAudioContent(jdesc_output.description());
2207 ASSERT_TRUE(ac != NULL);
2208 const AudioContentDescription* acd =
2209 static_cast<const AudioContentDescription*>(ac->description);
2210 ASSERT_FALSE(acd->codecs().empty());
2211 EXPECT_EQ("CELT", acd->codecs()[0].name);
2212 EXPECT_EQ(104, acd->codecs()[0].id);
2213}
2214
2215TEST_F(WebRtcSdpTest, DeserializeSerializeCodecParams) {
2216 JsepSessionDescription jdesc_output(kDummyString);
2217 CodecParams params;
2218 params.max_ptime = 40;
2219 params.ptime = 30;
2220 params.min_ptime = 10;
2221 params.sprop_stereo = 1;
2222 params.stereo = 1;
2223 params.useinband = 1;
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00002224 params.maxaveragebitrate = 128000;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002225 TestDeserializeCodecParams(params, &jdesc_output);
2226 TestSerialize(jdesc_output);
2227}
2228
2229TEST_F(WebRtcSdpTest, DeserializeSerializeRtcpFb) {
2230 const bool kUseWildcard = false;
2231 JsepSessionDescription jdesc_output(kDummyString);
2232 TestDeserializeRtcpFb(&jdesc_output, kUseWildcard);
2233 TestSerialize(jdesc_output);
2234}
2235
2236TEST_F(WebRtcSdpTest, DeserializeSerializeRtcpFbWildcard) {
2237 const bool kUseWildcard = true;
2238 JsepSessionDescription jdesc_output(kDummyString);
2239 TestDeserializeRtcpFb(&jdesc_output, kUseWildcard);
2240 TestSerialize(jdesc_output);
2241}
2242
2243TEST_F(WebRtcSdpTest, DeserializeVideoFmtp) {
2244 JsepSessionDescription jdesc_output(kDummyString);
2245
2246 const char kSdpWithFmtpString[] =
2247 "v=0\r\n"
2248 "o=- 18446744069414584320 18446462598732840960 IN IP4 127.0.0.1\r\n"
2249 "s=-\r\n"
2250 "t=0 0\r\n"
2251 "m=video 3457 RTP/SAVPF 120\r\n"
2252 "a=rtpmap:120 VP8/90000\r\n"
2253 "a=fmtp:120 x-google-min-bitrate=10; x-google-max-quantization=40\r\n";
2254
2255 // Deserialize
2256 SdpParseError error;
2257 EXPECT_TRUE(webrtc::SdpDeserialize(kSdpWithFmtpString, &jdesc_output,
2258 &error));
2259
2260 const ContentInfo* vc = GetFirstVideoContent(jdesc_output.description());
2261 ASSERT_TRUE(vc != NULL);
2262 const VideoContentDescription* vcd =
2263 static_cast<const VideoContentDescription*>(vc->description);
2264 ASSERT_FALSE(vcd->codecs().empty());
2265 cricket::VideoCodec vp8 = vcd->codecs()[0];
2266 EXPECT_EQ("VP8", vp8.name);
2267 EXPECT_EQ(120, vp8.id);
2268 cricket::CodecParameterMap::iterator found =
2269 vp8.params.find("x-google-min-bitrate");
2270 ASSERT_TRUE(found != vp8.params.end());
2271 EXPECT_EQ(found->second, "10");
2272 found = vp8.params.find("x-google-max-quantization");
2273 ASSERT_TRUE(found != vp8.params.end());
2274 EXPECT_EQ(found->second, "40");
2275}
2276
2277TEST_F(WebRtcSdpTest, SerializeVideoFmtp) {
2278 VideoContentDescription* vcd = static_cast<VideoContentDescription*>(
2279 GetFirstVideoContent(&desc_)->description);
2280
2281 cricket::VideoCodecs codecs = vcd->codecs();
2282 codecs[0].params["x-google-min-bitrate"] = "10";
2283 vcd->set_codecs(codecs);
2284
2285 ASSERT_TRUE(jdesc_.Initialize(desc_.Copy(),
2286 jdesc_.session_id(),
2287 jdesc_.session_version()));
2288 std::string message = webrtc::SdpSerialize(jdesc_);
2289 std::string sdp_with_fmtp = kSdpFullString;
2290 InjectAfter("a=rtpmap:120 VP8/90000\r\n",
2291 "a=fmtp:120 x-google-min-bitrate=10\r\n",
2292 &sdp_with_fmtp);
2293 EXPECT_EQ(sdp_with_fmtp, message);
2294}
2295
2296TEST_F(WebRtcSdpTest, DeserializeSdpWithIceLite) {
2297 JsepSessionDescription jdesc_with_icelite(kDummyString);
2298 std::string sdp_with_icelite = kSdpFullString;
2299 EXPECT_TRUE(SdpDeserialize(sdp_with_icelite, &jdesc_with_icelite));
2300 cricket::SessionDescription* desc = jdesc_with_icelite.description();
2301 const cricket::TransportInfo* tinfo1 =
2302 desc->GetTransportInfoByName("audio_content_name");
2303 EXPECT_EQ(cricket::ICEMODE_FULL, tinfo1->description.ice_mode);
2304 const cricket::TransportInfo* tinfo2 =
2305 desc->GetTransportInfoByName("video_content_name");
2306 EXPECT_EQ(cricket::ICEMODE_FULL, tinfo2->description.ice_mode);
2307 InjectAfter(kSessionTime,
2308 "a=ice-lite\r\n",
2309 &sdp_with_icelite);
2310 EXPECT_TRUE(SdpDeserialize(sdp_with_icelite, &jdesc_with_icelite));
2311 desc = jdesc_with_icelite.description();
2312 const cricket::TransportInfo* atinfo =
2313 desc->GetTransportInfoByName("audio_content_name");
2314 EXPECT_EQ(cricket::ICEMODE_LITE, atinfo->description.ice_mode);
2315 const cricket::TransportInfo* vtinfo =
2316 desc->GetTransportInfoByName("video_content_name");
2317 EXPECT_EQ(cricket::ICEMODE_LITE, vtinfo->description.ice_mode);
2318}
2319
2320// Verifies that the candidates in the input SDP are parsed and serialized
2321// correctly in the output SDP.
2322TEST_F(WebRtcSdpTest, RoundTripSdpWithSctpDataChannelsWithCandidates) {
2323 std::string sdp_with_data = kSdpString;
2324 sdp_with_data.append(kSdpSctpDataChannelWithCandidatesString);
2325 JsepSessionDescription jdesc_output(kDummyString);
2326
2327 EXPECT_TRUE(SdpDeserialize(sdp_with_data, &jdesc_output));
2328 EXPECT_EQ(sdp_with_data, webrtc::SdpSerialize(jdesc_output));
2329}
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00002330
2331TEST_F(WebRtcSdpTest, SerializeDtlsSetupAttribute) {
2332 AddFingerprint();
2333 TransportInfo audio_transport_info =
2334 *(desc_.GetTransportInfoByName(kAudioContentName));
2335 EXPECT_EQ(cricket::CONNECTIONROLE_NONE,
2336 audio_transport_info.description.connection_role);
2337 audio_transport_info.description.connection_role =
2338 cricket::CONNECTIONROLE_ACTIVE;
2339
2340 TransportInfo video_transport_info =
2341 *(desc_.GetTransportInfoByName(kVideoContentName));
2342 EXPECT_EQ(cricket::CONNECTIONROLE_NONE,
2343 video_transport_info.description.connection_role);
2344 video_transport_info.description.connection_role =
2345 cricket::CONNECTIONROLE_ACTIVE;
2346
2347 desc_.RemoveTransportInfoByName(kAudioContentName);
2348 desc_.RemoveTransportInfoByName(kVideoContentName);
2349
2350 desc_.AddTransportInfo(audio_transport_info);
2351 desc_.AddTransportInfo(video_transport_info);
2352
2353 ASSERT_TRUE(jdesc_.Initialize(desc_.Copy(),
2354 jdesc_.session_id(),
2355 jdesc_.session_version()));
2356 std::string message = webrtc::SdpSerialize(jdesc_);
2357 std::string sdp_with_dtlssetup = kSdpFullString;
2358
2359 // Fingerprint attribute is necessary to add DTLS setup attribute.
2360 InjectAfter(kAttributeIcePwdVoice,
2361 kFingerprint, &sdp_with_dtlssetup);
2362 InjectAfter(kAttributeIcePwdVideo,
2363 kFingerprint, &sdp_with_dtlssetup);
2364 // Now adding |setup| attribute.
2365 InjectAfter(kFingerprint,
2366 "a=setup:active\r\n", &sdp_with_dtlssetup);
2367 EXPECT_EQ(sdp_with_dtlssetup, message);
2368}
2369
2370TEST_F(WebRtcSdpTest, DeserializeDtlsSetupAttribute) {
2371 JsepSessionDescription jdesc_with_dtlssetup(kDummyString);
2372 std::string sdp_with_dtlssetup = kSdpFullString;
2373 InjectAfter(kSessionTime,
2374 "a=setup:actpass\r\n",
2375 &sdp_with_dtlssetup);
2376 EXPECT_TRUE(SdpDeserialize(sdp_with_dtlssetup, &jdesc_with_dtlssetup));
2377 cricket::SessionDescription* desc = jdesc_with_dtlssetup.description();
2378 const cricket::TransportInfo* atinfo =
2379 desc->GetTransportInfoByName("audio_content_name");
2380 EXPECT_EQ(cricket::CONNECTIONROLE_ACTPASS,
2381 atinfo->description.connection_role);
2382 const cricket::TransportInfo* vtinfo =
2383 desc->GetTransportInfoByName("video_content_name");
2384 EXPECT_EQ(cricket::CONNECTIONROLE_ACTPASS,
2385 vtinfo->description.connection_role);
2386}
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +00002387
2388// Verifies that the order of the serialized m-lines follows the order of the
2389// ContentInfo in SessionDescription, and vise versa for deserialization.
2390TEST_F(WebRtcSdpTest, MediaContentOrderMaintainedRoundTrip) {
2391 JsepSessionDescription jdesc(kDummyString);
2392 const std::string media_content_sdps[3] = {
2393 kSdpAudioString,
2394 kSdpVideoString,
2395 kSdpSctpDataChannelString
2396 };
2397 const cricket::MediaType media_types[3] = {
2398 cricket::MEDIA_TYPE_AUDIO,
2399 cricket::MEDIA_TYPE_VIDEO,
2400 cricket::MEDIA_TYPE_DATA
2401 };
2402
2403 // Verifies all 6 permutations.
2404 for (size_t i = 0; i < 6; ++i) {
2405 size_t media_content_in_sdp[3];
2406 // The index of the first media content.
2407 media_content_in_sdp[0] = i / 2;
2408 // The index of the second media content.
2409 media_content_in_sdp[1] = (media_content_in_sdp[0] + i % 2 + 1) % 3;
2410 // The index of the third media content.
2411 media_content_in_sdp[2] = (media_content_in_sdp[0] + (i + 1) % 2 + 1) % 3;
2412
2413 std::string sdp_string = kSdpSessionString;
2414 for (size_t i = 0; i < 3; ++i)
2415 sdp_string += media_content_sdps[media_content_in_sdp[i]];
2416
2417 EXPECT_TRUE(SdpDeserialize(sdp_string, &jdesc));
2418 cricket::SessionDescription* desc = jdesc.description();
2419 EXPECT_EQ(3u, desc->contents().size());
2420
2421 for (size_t i = 0; i < 3; ++i) {
2422 const cricket::MediaContentDescription* mdesc =
2423 static_cast<const cricket::MediaContentDescription*>(
2424 desc->contents()[i].description);
2425 EXPECT_EQ(media_types[media_content_in_sdp[i]], mdesc->type());
2426 }
2427
2428 std::string serialized_sdp = webrtc::SdpSerialize(jdesc);
2429 EXPECT_EQ(sdp_string, serialized_sdp);
2430 }
2431}