blob: be1fe47ca32ee97fc1859b7d661c631ad9c690ef [file] [log] [blame]
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001/*
2 * libjingle
jlmiller@webrtc.org5f93d0a2015-01-20 21:36:13 +00003 * Copyright 2011 Google Inc.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00004 *
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 "talk/app/webrtc/webrtcsdp.h"
29
30#include <limits.h>
31#include <stdio.h>
32#include <algorithm>
33#include <string>
34#include <vector>
decurtis@webrtc.org8af11042015-01-07 19:15:51 +000035#include <ctype.h>
henrike@webrtc.org28e20752013-07-10 00:45:36 +000036
37#include "talk/app/webrtc/jsepicecandidate.h"
38#include "talk/app/webrtc/jsepsessiondescription.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000039#include "talk/media/base/codec.h"
40#include "talk/media/base/constants.h"
41#include "talk/media/base/cryptoparams.h"
pkasting@chromium.orge9facf82015-02-17 20:36:28 +000042#include "talk/media/base/rtputils.h"
mallinath@webrtc.org1112c302013-09-23 20:34:45 +000043#include "talk/media/sctp/sctpdataengine.h"
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000044#include "webrtc/p2p/base/candidate.h"
45#include "webrtc/p2p/base/constants.h"
46#include "webrtc/p2p/base/port.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000047#include "talk/session/media/mediasession.h"
buildbot@webrtc.orga09a9992014-08-13 17:26:08 +000048#include "webrtc/base/common.h"
49#include "webrtc/base/logging.h"
50#include "webrtc/base/messagedigest.h"
51#include "webrtc/base/stringutils.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000052
53using cricket::AudioContentDescription;
54using cricket::Candidate;
55using cricket::Candidates;
56using cricket::ContentDescription;
57using cricket::ContentInfo;
58using cricket::CryptoParams;
59using cricket::DataContentDescription;
60using cricket::ICE_CANDIDATE_COMPONENT_RTP;
61using cricket::ICE_CANDIDATE_COMPONENT_RTCP;
62using cricket::kCodecParamMaxBitrate;
63using cricket::kCodecParamMaxPTime;
64using cricket::kCodecParamMaxQuantization;
65using cricket::kCodecParamMinBitrate;
66using cricket::kCodecParamMinPTime;
67using cricket::kCodecParamPTime;
68using cricket::kCodecParamSPropStereo;
buildbot@webrtc.orged97bb02014-05-07 11:15:20 +000069using cricket::kCodecParamStartBitrate;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000070using cricket::kCodecParamStereo;
71using cricket::kCodecParamUseInbandFec;
72using cricket::kCodecParamSctpProtocol;
73using cricket::kCodecParamSctpStreams;
henrike@webrtc.org1e09a712013-07-26 19:17:59 +000074using cricket::kCodecParamMaxAverageBitrate;
buildbot@webrtc.org5d639b32014-09-10 07:57:12 +000075using cricket::kCodecParamMaxPlaybackRate;
stefan@webrtc.org85d27942014-06-09 12:51:39 +000076using cricket::kCodecParamAssociatedPayloadType;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000077using cricket::MediaContentDescription;
78using cricket::MediaType;
79using cricket::NS_JINGLE_ICE_UDP;
80using cricket::RtpHeaderExtension;
81using cricket::SsrcGroup;
82using cricket::StreamParams;
83using cricket::StreamParamsVec;
84using cricket::TransportDescription;
85using cricket::TransportInfo;
86using cricket::VideoContentDescription;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000087using rtc::SocketAddress;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000088
89typedef std::vector<RtpHeaderExtension> RtpHeaderExtensions;
90
91namespace cricket {
92class SessionDescription;
93}
94
95namespace webrtc {
96
97// Line type
98// RFC 4566
99// An SDP session description consists of a number of lines of text of
100// the form:
101// <type>=<value>
102// where <type> MUST be exactly one case-significant character.
103static const int kLinePrefixLength = 2; // Lenght of <type>=
104static const char kLineTypeVersion = 'v';
105static const char kLineTypeOrigin = 'o';
106static const char kLineTypeSessionName = 's';
107static const char kLineTypeSessionInfo = 'i';
108static const char kLineTypeSessionUri = 'u';
109static const char kLineTypeSessionEmail = 'e';
110static const char kLineTypeSessionPhone = 'p';
111static const char kLineTypeSessionBandwidth = 'b';
112static const char kLineTypeTiming = 't';
113static const char kLineTypeRepeatTimes = 'r';
114static const char kLineTypeTimeZone = 'z';
115static const char kLineTypeEncryptionKey = 'k';
116static const char kLineTypeMedia = 'm';
117static const char kLineTypeConnection = 'c';
118static const char kLineTypeAttributes = 'a';
119
120// Attributes
121static const char kAttributeGroup[] = "group";
122static const char kAttributeMid[] = "mid";
123static const char kAttributeRtcpMux[] = "rtcp-mux";
124static const char kAttributeSsrc[] = "ssrc";
125static const char kSsrcAttributeCname[] = "cname";
126static const char kAttributeExtmap[] = "extmap";
127// draft-alvestrand-mmusic-msid-01
128// a=msid-semantic: WMS
129static const char kAttributeMsidSemantics[] = "msid-semantic";
130static const char kMediaStreamSemantic[] = "WMS";
131static const char kSsrcAttributeMsid[] = "msid";
132static const char kDefaultMsid[] = "default";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000133static const char kSsrcAttributeMslabel[] = "mslabel";
134static const char kSSrcAttributeLabel[] = "label";
135static const char kAttributeSsrcGroup[] = "ssrc-group";
136static const char kAttributeCrypto[] = "crypto";
137static const char kAttributeCandidate[] = "candidate";
138static const char kAttributeCandidateTyp[] = "typ";
139static const char kAttributeCandidateRaddr[] = "raddr";
140static const char kAttributeCandidateRport[] = "rport";
141static const char kAttributeCandidateUsername[] = "username";
142static const char kAttributeCandidatePassword[] = "password";
143static const char kAttributeCandidateGeneration[] = "generation";
144static const char kAttributeFingerprint[] = "fingerprint";
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +0000145static const char kAttributeSetup[] = "setup";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000146static const char kAttributeFmtp[] = "fmtp";
147static const char kAttributeRtpmap[] = "rtpmap";
wu@webrtc.org78187522013-10-07 23:32:02 +0000148static const char kAttributeSctpmap[] = "sctpmap";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000149static const char kAttributeRtcp[] = "rtcp";
150static const char kAttributeIceUfrag[] = "ice-ufrag";
151static const char kAttributeIcePwd[] = "ice-pwd";
152static const char kAttributeIceLite[] = "ice-lite";
153static const char kAttributeIceOption[] = "ice-options";
154static const char kAttributeSendOnly[] = "sendonly";
155static const char kAttributeRecvOnly[] = "recvonly";
156static const char kAttributeRtcpFb[] = "rtcp-fb";
157static const char kAttributeSendRecv[] = "sendrecv";
158static const char kAttributeInactive[] = "inactive";
jiayl@webrtc.orgddb85ab2014-09-05 16:31:56 +0000159// draft-ietf-mmusic-sctp-sdp-07
160// a=sctp-port
161static const char kAttributeSctpPort[] = "sctp-port";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000162
163// Experimental flags
164static const char kAttributeXGoogleFlag[] = "x-google-flag";
165static const char kValueConference[] = "conference";
166static const char kAttributeXGoogleBufferLatency[] =
167 "x-google-buffer-latency";
168
169// Candidate
170static const char kCandidateHost[] = "host";
171static const char kCandidateSrflx[] = "srflx";
172// TODO: How to map the prflx with circket candidate type
173// static const char kCandidatePrflx[] = "prflx";
174static const char kCandidateRelay[] = "relay";
mallinath@webrtc.org2d60c5e2014-08-08 22:29:20 +0000175static const char kTcpCandidateType[] = "tcptype";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000176
177static const char kSdpDelimiterEqual = '=';
178static const char kSdpDelimiterSpace = ' ';
179static const char kSdpDelimiterColon = ':';
180static const char kSdpDelimiterSemicolon = ';';
181static const char kSdpDelimiterSlash = '/';
182static const char kNewLine = '\n';
183static const char kReturn = '\r';
184static const char kLineBreak[] = "\r\n";
185
186// TODO: Generate the Session and Time description
187// instead of hardcoding.
188static const char kSessionVersion[] = "v=0";
189// RFC 4566
190static const char kSessionOriginUsername[] = "-";
191static const char kSessionOriginSessionId[] = "0";
192static const char kSessionOriginSessionVersion[] = "0";
193static const char kSessionOriginNettype[] = "IN";
194static const char kSessionOriginAddrtype[] = "IP4";
195static const char kSessionOriginAddress[] = "127.0.0.1";
196static const char kSessionName[] = "s=-";
197static const char kTimeDescription[] = "t=0 0";
198static const char kAttrGroup[] = "a=group:BUNDLE";
199static const char kConnectionNettype[] = "IN";
pthatcher@webrtc.orgc9d6d142014-10-23 23:37:22 +0000200static const char kConnectionIpv4Addrtype[] = "IP4";
201static const char kConnectionIpv6Addrtype[] = "IP6";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000202static const char kMediaTypeVideo[] = "video";
203static const char kMediaTypeAudio[] = "audio";
204static const char kMediaTypeData[] = "application";
205static const char kMediaPortRejected[] = "0";
pthatcher@webrtc.orgc9d6d142014-10-23 23:37:22 +0000206// draft-ietf-mmusic-trickle-ice-01
207// When no candidates have been gathered, set the connection
208// address to IP6 ::.
perkj@webrtc.orgd105cc82014-11-07 11:22:06 +0000209// TODO(perkj): FF can not parse IP6 ::. See http://crbug/430333
210// Use IPV4 per default.
211static const char kDummyAddress[] = "0.0.0.0";
pthatcher@webrtc.orgc9d6d142014-10-23 23:37:22 +0000212static const char kDummyPort[] = "9";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000213// RFC 3556
214static const char kApplicationSpecificMaximum[] = "AS";
215
216static const int kDefaultVideoClockrate = 90000;
217
218// ISAC special-case.
219static const char kIsacCodecName[] = "ISAC"; // From webrtcvoiceengine.cc
220static const int kIsacWbDefaultRate = 32000; // From acm_common_defs.h
221static const int kIsacSwbDefaultRate = 56000; // From acm_common_defs.h
222
wu@webrtc.org78187522013-10-07 23:32:02 +0000223static const char kDefaultSctpmapProtocol[] = "webrtc-datachannel";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000224
pkasting@chromium.orgd3245462015-02-23 21:28:22 +0000225// RTP payload type is in the 0-127 range. Use -1 to indicate "all" payload
226// types.
227const int kWildcardPayloadType = -1;
228
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000229struct SsrcInfo {
230 SsrcInfo()
231 : msid_identifier(kDefaultMsid),
232 // TODO(ronghuawu): What should we do if the appdata doesn't appear?
233 // Create random string (which will be used as track label later)?
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000234 msid_appdata(rtc::CreateRandomString(8)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000235 }
236 uint32 ssrc_id;
237 std::string cname;
238 std::string msid_identifier;
239 std::string msid_appdata;
240
241 // For backward compatibility.
242 // TODO(ronghuawu): Remove below 2 fields once all the clients support msid.
243 std::string label;
244 std::string mslabel;
245};
246typedef std::vector<SsrcInfo> SsrcInfoVec;
247typedef std::vector<SsrcGroup> SsrcGroupVec;
248
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000249template <class T>
250static void AddFmtpLine(const T& codec, std::string* message);
251static void BuildMediaDescription(const ContentInfo* content_info,
252 const TransportInfo* transport_info,
253 const MediaType media_type,
wu@webrtc.org4c3e9912014-07-16 21:03:13 +0000254 const std::vector<Candidate>& candidates,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000255 std::string* message);
jiayl@webrtc.org9c16c392014-05-01 18:30:30 +0000256static void BuildSctpContentAttributes(std::string* message, int sctp_port);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000257static void BuildRtpContentAttributes(
258 const MediaContentDescription* media_desc,
259 const MediaType media_type,
260 std::string* message);
261static void BuildRtpMap(const MediaContentDescription* media_desc,
262 const MediaType media_type,
263 std::string* message);
264static void BuildCandidate(const std::vector<Candidate>& candidates,
265 std::string* message);
266static void BuildIceOptions(const std::vector<std::string>& transport_options,
267 std::string* message);
pthatcher@webrtc.org3341b402015-02-13 21:14:22 +0000268static bool IsRtp(const std::string& protocol);
269static bool IsDtlsSctp(const std::string& protocol);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000270static bool ParseSessionDescription(const std::string& message, size_t* pos,
271 std::string* session_id,
272 std::string* session_version,
273 bool* supports_msid,
274 TransportDescription* session_td,
275 RtpHeaderExtensions* session_extmaps,
276 cricket::SessionDescription* desc,
277 SdpParseError* error);
278static bool ParseGroupAttribute(const std::string& line,
279 cricket::SessionDescription* desc,
280 SdpParseError* error);
281static bool ParseMediaDescription(
282 const std::string& message,
283 const TransportDescription& session_td,
284 const RtpHeaderExtensions& session_extmaps,
285 bool supports_msid,
286 size_t* pos, cricket::SessionDescription* desc,
287 std::vector<JsepIceCandidate*>* candidates,
288 SdpParseError* error);
289static bool ParseContent(const std::string& message,
290 const MediaType media_type,
291 int mline_index,
292 const std::string& protocol,
293 const std::vector<int>& codec_preference,
294 size_t* pos,
295 std::string* content_name,
296 MediaContentDescription* media_desc,
297 TransportDescription* transport,
298 std::vector<JsepIceCandidate*>* candidates,
299 SdpParseError* error);
300static bool ParseSsrcAttribute(const std::string& line,
301 SsrcInfoVec* ssrc_infos,
302 SdpParseError* error);
303static bool ParseSsrcGroupAttribute(const std::string& line,
304 SsrcGroupVec* ssrc_groups,
305 SdpParseError* error);
306static bool ParseCryptoAttribute(const std::string& line,
307 MediaContentDescription* media_desc,
308 SdpParseError* error);
309static bool ParseRtpmapAttribute(const std::string& line,
310 const MediaType media_type,
311 const std::vector<int>& codec_preference,
312 MediaContentDescription* media_desc,
313 SdpParseError* error);
314static bool ParseFmtpAttributes(const std::string& line,
315 const MediaType media_type,
316 MediaContentDescription* media_desc,
317 SdpParseError* error);
318static bool ParseFmtpParam(const std::string& line, std::string* parameter,
319 std::string* value, SdpParseError* error);
320static bool ParseCandidate(const std::string& message, Candidate* candidate,
321 SdpParseError* error, bool is_raw);
322static bool ParseRtcpFbAttribute(const std::string& line,
323 const MediaType media_type,
324 MediaContentDescription* media_desc,
325 SdpParseError* error);
326static bool ParseIceOptions(const std::string& line,
327 std::vector<std::string>* transport_options,
328 SdpParseError* error);
329static bool ParseExtmap(const std::string& line,
330 RtpHeaderExtension* extmap,
331 SdpParseError* error);
332static bool ParseFingerprintAttribute(const std::string& line,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000333 rtc::SSLFingerprint** fingerprint,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000334 SdpParseError* error);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +0000335static bool ParseDtlsSetup(const std::string& line,
336 cricket::ConnectionRole* role,
337 SdpParseError* error);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000338
339// Helper functions
340
341// Below ParseFailed*** functions output the line that caused the parsing
342// failure and the detailed reason (|description|) of the failure to |error|.
343// The functions always return false so that they can be used directly in the
344// following way when error happens:
345// "return ParseFailed***(...);"
346
347// The line starting at |line_start| of |message| is the failing line.
348// The reason for the failure should be provided in the |description|.
349// An example of a description could be "unknown character".
350static bool ParseFailed(const std::string& message,
351 size_t line_start,
352 const std::string& description,
353 SdpParseError* error) {
354 // Get the first line of |message| from |line_start|.
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +0000355 std::string first_line;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000356 size_t line_end = message.find(kNewLine, line_start);
357 if (line_end != std::string::npos) {
358 if (line_end > 0 && (message.at(line_end - 1) == kReturn)) {
359 --line_end;
360 }
361 first_line = message.substr(line_start, (line_end - line_start));
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +0000362 } else {
363 first_line = message.substr(line_start);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000364 }
365
366 if (error) {
367 error->line = first_line;
368 error->description = description;
369 }
370 LOG(LS_ERROR) << "Failed to parse: \"" << first_line
371 << "\". Reason: " << description;
372 return false;
373}
374
375// |line| is the failing line. The reason for the failure should be
376// provided in the |description|.
377static bool ParseFailed(const std::string& line,
378 const std::string& description,
379 SdpParseError* error) {
380 return ParseFailed(line, 0, description, error);
381}
382
383// Parses failure where the failing SDP line isn't know or there are multiple
384// failing lines.
385static bool ParseFailed(const std::string& description,
386 SdpParseError* error) {
387 return ParseFailed("", description, error);
388}
389
390// |line| is the failing line. The failure is due to the fact that |line|
391// doesn't have |expected_fields| fields.
392static bool ParseFailedExpectFieldNum(const std::string& line,
393 int expected_fields,
394 SdpParseError* error) {
395 std::ostringstream description;
396 description << "Expects " << expected_fields << " fields.";
397 return ParseFailed(line, description.str(), error);
398}
399
400// |line| is the failing line. The failure is due to the fact that |line| has
401// less than |expected_min_fields| fields.
402static bool ParseFailedExpectMinFieldNum(const std::string& line,
403 int expected_min_fields,
404 SdpParseError* error) {
405 std::ostringstream description;
406 description << "Expects at least " << expected_min_fields << " fields.";
407 return ParseFailed(line, description.str(), error);
408}
409
410// |line| is the failing line. The failure is due to the fact that it failed to
411// get the value of |attribute|.
412static bool ParseFailedGetValue(const std::string& line,
413 const std::string& attribute,
414 SdpParseError* error) {
415 std::ostringstream description;
416 description << "Failed to get the value of attribute: " << attribute;
417 return ParseFailed(line, description.str(), error);
418}
419
420// The line starting at |line_start| of |message| is the failing line. The
421// failure is due to the line type (e.g. the "m" part of the "m-line")
422// not matching what is expected. The expected line type should be
423// provided as |line_type|.
424static bool ParseFailedExpectLine(const std::string& message,
425 size_t line_start,
426 const char line_type,
427 const std::string& line_value,
428 SdpParseError* error) {
429 std::ostringstream description;
430 description << "Expect line: " << line_type << "=" << line_value;
431 return ParseFailed(message, line_start, description.str(), error);
432}
433
434static bool AddLine(const std::string& line, std::string* message) {
435 if (!message)
436 return false;
437
438 message->append(line);
439 message->append(kLineBreak);
440 return true;
441}
442
443static bool GetLine(const std::string& message,
444 size_t* pos,
445 std::string* line) {
446 size_t line_begin = *pos;
447 size_t line_end = message.find(kNewLine, line_begin);
448 if (line_end == std::string::npos) {
449 return false;
450 }
451 // Update the new start position
452 *pos = line_end + 1;
453 if (line_end > 0 && (message.at(line_end - 1) == kReturn)) {
454 --line_end;
455 }
456 *line = message.substr(line_begin, (line_end - line_begin));
457 const char* cline = line->c_str();
458 // RFC 4566
459 // An SDP session description consists of a number of lines of text of
460 // the form:
461 // <type>=<value>
462 // where <type> MUST be exactly one case-significant character and
463 // <value> is structured text whose format depends on <type>.
464 // Whitespace MUST NOT be used on either side of the "=" sign.
decurtis@webrtc.org8af11042015-01-07 19:15:51 +0000465 if (line->length() < 3 ||
466 !islower(cline[0]) ||
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000467 cline[1] != kSdpDelimiterEqual ||
468 cline[2] == kSdpDelimiterSpace) {
469 *pos = line_begin;
470 return false;
471 }
472 return true;
473}
474
475// Init |os| to "|type|=|value|".
476static void InitLine(const char type,
477 const std::string& value,
478 std::ostringstream* os) {
479 os->str("");
480 *os << type << kSdpDelimiterEqual << value;
481}
482
483// Init |os| to "a=|attribute|".
484static void InitAttrLine(const std::string& attribute, std::ostringstream* os) {
485 InitLine(kLineTypeAttributes, attribute, os);
486}
487
488// Writes a SDP attribute line based on |attribute| and |value| to |message|.
489static void AddAttributeLine(const std::string& attribute, int value,
490 std::string* message) {
491 std::ostringstream os;
492 InitAttrLine(attribute, &os);
493 os << kSdpDelimiterColon << value;
494 AddLine(os.str(), message);
495}
496
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000497static bool IsLineType(const std::string& message,
498 const char type,
499 size_t line_start) {
500 if (message.size() < line_start + kLinePrefixLength) {
501 return false;
502 }
503 const char* cmessage = message.c_str();
504 return (cmessage[line_start] == type &&
505 cmessage[line_start + 1] == kSdpDelimiterEqual);
506}
507
508static bool IsLineType(const std::string& line,
509 const char type) {
510 return IsLineType(line, type, 0);
511}
512
513static bool GetLineWithType(const std::string& message, size_t* pos,
514 std::string* line, const char type) {
515 if (!IsLineType(message, type, *pos)) {
516 return false;
517 }
518
519 if (!GetLine(message, pos, line))
520 return false;
521
522 return true;
523}
524
525static bool HasAttribute(const std::string& line,
526 const std::string& attribute) {
527 return (line.compare(kLinePrefixLength, attribute.size(), attribute) == 0);
528}
529
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000530static bool AddSsrcLine(uint32 ssrc_id, const std::string& attribute,
531 const std::string& value, std::string* message) {
532 // RFC 5576
533 // a=ssrc:<ssrc-id> <attribute>:<value>
534 std::ostringstream os;
535 InitAttrLine(kAttributeSsrc, &os);
536 os << kSdpDelimiterColon << ssrc_id << kSdpDelimiterSpace
537 << attribute << kSdpDelimiterColon << value;
538 return AddLine(os.str(), message);
539}
540
541// Split the message into two parts by the first delimiter.
542static bool SplitByDelimiter(const std::string& message,
543 const char delimiter,
544 std::string* field1,
545 std::string* field2) {
546 // Find the first delimiter
547 size_t pos = message.find(delimiter);
548 if (pos == std::string::npos) {
549 return false;
550 }
551 *field1 = message.substr(0, pos);
552 // The rest is the value.
553 *field2 = message.substr(pos + 1);
554 return true;
555}
556
557// Get value only from <attribute>:<value>.
558static bool GetValue(const std::string& message, const std::string& attribute,
559 std::string* value, SdpParseError* error) {
560 std::string leftpart;
561 if (!SplitByDelimiter(message, kSdpDelimiterColon, &leftpart, value)) {
562 return ParseFailedGetValue(message, attribute, error);
563 }
564 // The left part should end with the expected attribute.
565 if (leftpart.length() < attribute.length() ||
566 leftpart.compare(leftpart.length() - attribute.length(),
567 attribute.length(), attribute) != 0) {
568 return ParseFailedGetValue(message, attribute, error);
569 }
570 return true;
571}
572
573static bool CaseInsensitiveFind(std::string str1, std::string str2) {
574 std::transform(str1.begin(), str1.end(), str1.begin(),
575 ::tolower);
576 std::transform(str2.begin(), str2.end(), str2.begin(),
577 ::tolower);
578 return str1.find(str2) != std::string::npos;
579}
580
wu@webrtc.org5e760e72014-04-02 23:19:09 +0000581template <class T>
582static bool GetValueFromString(const std::string& line,
583 const std::string& s,
584 T* t,
585 SdpParseError* error) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000586 if (!rtc::FromString(s, t)) {
wu@webrtc.org5e760e72014-04-02 23:19:09 +0000587 std::ostringstream description;
588 description << "Invalid value: " << s << ".";
589 return ParseFailed(line, description.str(), error);
590 }
591 return true;
592}
593
pkasting@chromium.orge9facf82015-02-17 20:36:28 +0000594static bool GetPayloadTypeFromString(const std::string& line,
595 const std::string& s,
596 int* payload_type,
597 SdpParseError* error) {
598 return GetValueFromString(line, s, payload_type, error) &&
599 cricket::IsValidRtpPayloadType(*payload_type);
600}
601
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000602void CreateTracksFromSsrcInfos(const SsrcInfoVec& ssrc_infos,
603 StreamParamsVec* tracks) {
604 ASSERT(tracks != NULL);
605 for (SsrcInfoVec::const_iterator ssrc_info = ssrc_infos.begin();
606 ssrc_info != ssrc_infos.end(); ++ssrc_info) {
607 if (ssrc_info->cname.empty()) {
608 continue;
609 }
610
611 std::string sync_label;
612 std::string track_id;
613 if (ssrc_info->msid_identifier == kDefaultMsid &&
614 !ssrc_info->mslabel.empty()) {
615 // If there's no msid and there's mslabel, we consider this is a sdp from
616 // a older version of client that doesn't support msid.
617 // In that case, we use the mslabel and label to construct the track.
618 sync_label = ssrc_info->mslabel;
619 track_id = ssrc_info->label;
620 } else {
621 sync_label = ssrc_info->msid_identifier;
622 // The appdata consists of the "id" attribute of a MediaStreamTrack, which
623 // is corresponding to the "id" attribute of StreamParams.
624 track_id = ssrc_info->msid_appdata;
625 }
626 if (sync_label.empty() || track_id.empty()) {
627 ASSERT(false);
628 continue;
629 }
630
631 StreamParamsVec::iterator track = tracks->begin();
632 for (; track != tracks->end(); ++track) {
633 if (track->id == track_id) {
634 break;
635 }
636 }
637 if (track == tracks->end()) {
638 // If we don't find an existing track, create a new one.
639 tracks->push_back(StreamParams());
640 track = tracks->end() - 1;
641 }
642 track->add_ssrc(ssrc_info->ssrc_id);
643 track->cname = ssrc_info->cname;
644 track->sync_label = sync_label;
645 track->id = track_id;
646 }
647}
648
649void GetMediaStreamLabels(const ContentInfo* content,
650 std::set<std::string>* labels) {
651 const MediaContentDescription* media_desc =
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000652 static_cast<const MediaContentDescription*>(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000653 content->description);
654 const cricket::StreamParamsVec& streams = media_desc->streams();
655 for (cricket::StreamParamsVec::const_iterator it = streams.begin();
656 it != streams.end(); ++it) {
657 labels->insert(it->sync_label);
658 }
659}
660
661// RFC 5245
662// It is RECOMMENDED that default candidates be chosen based on the
663// likelihood of those candidates to work with the peer that is being
664// contacted. It is RECOMMENDED that relayed > reflexive > host.
665static const int kPreferenceUnknown = 0;
666static const int kPreferenceHost = 1;
667static const int kPreferenceReflexive = 2;
668static const int kPreferenceRelayed = 3;
669
670static int GetCandidatePreferenceFromType(const std::string& type) {
671 int preference = kPreferenceUnknown;
672 if (type == cricket::LOCAL_PORT_TYPE) {
673 preference = kPreferenceHost;
674 } else if (type == cricket::STUN_PORT_TYPE) {
675 preference = kPreferenceReflexive;
676 } else if (type == cricket::RELAY_PORT_TYPE) {
677 preference = kPreferenceRelayed;
678 } else {
679 ASSERT(false);
680 }
681 return preference;
682}
683
guoweis@webrtc.org57ac2c82015-02-06 00:45:13 +0000684// Get ip and port of the default destination from the |candidates| with the
685// given value of |component_id|. The default candidate should be the one most
686// likely to work, typically IPv4 relay.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000687// RFC 5245
688// The value of |component_id| currently supported are 1 (RTP) and 2 (RTCP).
689// TODO: Decide the default destination in webrtcsession and
690// pass it down via SessionDescription.
pthatcher@webrtc.orgc9d6d142014-10-23 23:37:22 +0000691static void GetDefaultDestination(
692 const std::vector<Candidate>& candidates,
693 int component_id, std::string* port,
694 std::string* ip, std::string* addr_type) {
perkj@webrtc.orgd105cc82014-11-07 11:22:06 +0000695 *addr_type = kConnectionIpv4Addrtype;
pthatcher@webrtc.orgc9d6d142014-10-23 23:37:22 +0000696 *port = kDummyPort;
697 *ip = kDummyAddress;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000698 int current_preference = kPreferenceUnknown;
guoweis@webrtc.org57ac2c82015-02-06 00:45:13 +0000699 int current_family = AF_UNSPEC;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000700 for (std::vector<Candidate>::const_iterator it = candidates.begin();
701 it != candidates.end(); ++it) {
702 if (it->component() != component_id) {
703 continue;
704 }
guoweis@webrtc.org57ac2c82015-02-06 00:45:13 +0000705 // Default destination should be UDP only.
706 if (it->protocol() != cricket::UDP_PROTOCOL_NAME) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000707 continue;
708 }
guoweis@webrtc.org57ac2c82015-02-06 00:45:13 +0000709 const int preference = GetCandidatePreferenceFromType(it->type());
710 const int family = it->address().ipaddr().family();
711 // See if this candidate is more preferable then the current one if it's the
712 // same family. Or if the current family is IPv4 already so we could safely
713 // ignore all IPv6 ones. WebRTC bug 4269.
714 // http://code.google.com/p/webrtc/issues/detail?id=4269
715 if ((preference <= current_preference && current_family == family) ||
716 (current_family == AF_INET && family == AF_INET6)) {
717 continue;
718 }
pthatcher@webrtc.orgc9d6d142014-10-23 23:37:22 +0000719 if (family == AF_INET) {
720 addr_type->assign(kConnectionIpv4Addrtype);
721 } else if (family == AF_INET6) {
722 addr_type->assign(kConnectionIpv6Addrtype);
723 }
guoweis@webrtc.org57ac2c82015-02-06 00:45:13 +0000724 current_preference = preference;
725 current_family = family;
726 *port = it->address().PortAsString();
727 *ip = it->address().ipaddr().ToString();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000728 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000729}
730
wu@webrtc.org4c3e9912014-07-16 21:03:13 +0000731// Update |mline|'s default destination and append a c line after it.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000732static void UpdateMediaDefaultDestination(
wu@webrtc.org4c3e9912014-07-16 21:03:13 +0000733 const std::vector<Candidate>& candidates,
734 const std::string mline,
735 std::string* message) {
736 std::string new_lines;
737 AddLine(mline, &new_lines);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000738 // RFC 4566
739 // m=<media> <port> <proto> <fmt> ...
740 std::vector<std::string> fields;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000741 rtc::split(mline, kSdpDelimiterSpace, &fields);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000742 if (fields.size() < 3) {
743 return;
744 }
745
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000746 std::ostringstream os;
pthatcher@webrtc.orgc9d6d142014-10-23 23:37:22 +0000747 std::string rtp_port, rtp_ip, addr_type;
748 GetDefaultDestination(candidates, ICE_CANDIDATE_COMPONENT_RTP,
749 &rtp_port, &rtp_ip, &addr_type);
750 // Found default RTP candidate.
751 // RFC 5245
752 // The default candidates are added to the SDP as the default
753 // destination for media. For streams based on RTP, this is done by
754 // placing the IP address and port of the RTP candidate into the c and m
755 // lines, respectively.
756 // Update the port in the m line.
757 // If this is a m-line with port equal to 0, we don't change it.
758 if (fields[1] != kMediaPortRejected) {
759 new_lines.replace(fields[0].size() + 1,
760 fields[1].size(),
761 rtp_port);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000762 }
pthatcher@webrtc.orgc9d6d142014-10-23 23:37:22 +0000763 // Add the c line.
764 // RFC 4566
765 // c=<nettype> <addrtype> <connection-address>
766 InitLine(kLineTypeConnection, kConnectionNettype, &os);
767 os << " " << addr_type << " " << rtp_ip;
768 AddLine(os.str(), &new_lines);
wu@webrtc.org4c3e9912014-07-16 21:03:13 +0000769 message->append(new_lines);
770}
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000771
wu@webrtc.org4c3e9912014-07-16 21:03:13 +0000772// Gets "a=rtcp" line if found default RTCP candidate from |candidates|.
773static std::string GetRtcpLine(const std::vector<Candidate>& candidates) {
pthatcher@webrtc.orgc9d6d142014-10-23 23:37:22 +0000774 std::string rtcp_line, rtcp_port, rtcp_ip, addr_type;
775 GetDefaultDestination(candidates, ICE_CANDIDATE_COMPONENT_RTCP,
776 &rtcp_port, &rtcp_ip, &addr_type);
777 // Found default RTCP candidate.
778 // RFC 5245
779 // If the agent is utilizing RTCP, it MUST encode the RTCP candidate
780 // using the a=rtcp attribute as defined in RFC 3605.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000781
pthatcher@webrtc.orgc9d6d142014-10-23 23:37:22 +0000782 // RFC 3605
783 // rtcp-attribute = "a=rtcp:" port [nettype space addrtype space
784 // connection-address] CRLF
785 std::ostringstream os;
786 InitAttrLine(kAttributeRtcp, &os);
787 os << kSdpDelimiterColon
788 << rtcp_port << " "
789 << kConnectionNettype << " "
790 << addr_type << " "
791 << rtcp_ip;
792 rtcp_line = os.str();
wu@webrtc.org4c3e9912014-07-16 21:03:13 +0000793 return rtcp_line;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000794}
795
796// Get candidates according to the mline index from SessionDescriptionInterface.
797static void GetCandidatesByMindex(const SessionDescriptionInterface& desci,
798 int mline_index,
799 std::vector<Candidate>* candidates) {
800 if (!candidates) {
801 return;
802 }
803 const IceCandidateCollection* cc = desci.candidates(mline_index);
804 for (size_t i = 0; i < cc->count(); ++i) {
805 const IceCandidateInterface* candidate = cc->at(i);
806 candidates->push_back(candidate->candidate());
807 }
808}
809
810std::string SdpSerialize(const JsepSessionDescription& jdesc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000811 const cricket::SessionDescription* desc = jdesc.description();
812 if (!desc) {
813 return "";
814 }
815
816 std::string message;
817
818 // Session Description.
819 AddLine(kSessionVersion, &message);
820 // Session Origin
821 // RFC 4566
822 // o=<username> <sess-id> <sess-version> <nettype> <addrtype>
823 // <unicast-address>
824 std::ostringstream os;
825 InitLine(kLineTypeOrigin, kSessionOriginUsername, &os);
826 const std::string session_id = jdesc.session_id().empty() ?
827 kSessionOriginSessionId : jdesc.session_id();
828 const std::string session_version = jdesc.session_version().empty() ?
829 kSessionOriginSessionVersion : jdesc.session_version();
830 os << " " << session_id << " " << session_version << " "
831 << kSessionOriginNettype << " " << kSessionOriginAddrtype << " "
832 << kSessionOriginAddress;
833 AddLine(os.str(), &message);
834 AddLine(kSessionName, &message);
835
836 // Time Description.
837 AddLine(kTimeDescription, &message);
838
839 // Group
840 if (desc->HasGroup(cricket::GROUP_TYPE_BUNDLE)) {
841 std::string group_line = kAttrGroup;
842 const cricket::ContentGroup* group =
843 desc->GetGroupByName(cricket::GROUP_TYPE_BUNDLE);
844 ASSERT(group != NULL);
845 const cricket::ContentNames& content_names = group->content_names();
846 for (cricket::ContentNames::const_iterator it = content_names.begin();
847 it != content_names.end(); ++it) {
848 group_line.append(" ");
849 group_line.append(*it);
850 }
851 AddLine(group_line, &message);
852 }
853
854 // MediaStream semantics
855 InitAttrLine(kAttributeMsidSemantics, &os);
856 os << kSdpDelimiterColon << " " << kMediaStreamSemantic;
wu@webrtc.org4c3e9912014-07-16 21:03:13 +0000857
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000858 std::set<std::string> media_stream_labels;
859 const ContentInfo* audio_content = GetFirstAudioContent(desc);
860 if (audio_content)
861 GetMediaStreamLabels(audio_content, &media_stream_labels);
wu@webrtc.org4c3e9912014-07-16 21:03:13 +0000862
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000863 const ContentInfo* video_content = GetFirstVideoContent(desc);
864 if (video_content)
865 GetMediaStreamLabels(video_content, &media_stream_labels);
wu@webrtc.org4c3e9912014-07-16 21:03:13 +0000866
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000867 for (std::set<std::string>::const_iterator it =
868 media_stream_labels.begin(); it != media_stream_labels.end(); ++it) {
869 os << " " << *it;
870 }
871 AddLine(os.str(), &message);
872
wu@webrtc.org4c3e9912014-07-16 21:03:13 +0000873 // Preserve the order of the media contents.
874 int mline_index = -1;
875 for (cricket::ContentInfos::const_iterator it = desc->contents().begin();
876 it != desc->contents().end(); ++it) {
877 const MediaContentDescription* mdesc =
878 static_cast<const MediaContentDescription*>(it->description);
879 std::vector<Candidate> candidates;
880 GetCandidatesByMindex(jdesc, ++mline_index, &candidates);
881 BuildMediaDescription(&*it,
882 desc->GetTransportInfoByName(it->name),
883 mdesc->type(),
884 candidates,
885 &message);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000886 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000887 return message;
888}
889
890// Serializes the passed in IceCandidateInterface to a SDP string.
891// candidate - The candidate to be serialized.
892std::string SdpSerializeCandidate(
893 const IceCandidateInterface& candidate) {
894 std::string message;
895 std::vector<cricket::Candidate> candidates;
896 candidates.push_back(candidate.candidate());
897 BuildCandidate(candidates, &message);
wu@webrtc.orgec9f5fb2014-06-24 17:05:10 +0000898 // From WebRTC draft section 4.8.1.1 candidate-attribute will be
899 // just candidate:<candidate> not a=candidate:<blah>CRLF
900 ASSERT(message.find("a=") == 0);
901 message.erase(0, 2);
902 ASSERT(message.find(kLineBreak) == message.size() - 2);
903 message.resize(message.size() - 2);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000904 return message;
905}
906
907bool SdpDeserialize(const std::string& message,
908 JsepSessionDescription* jdesc,
909 SdpParseError* error) {
910 std::string session_id;
911 std::string session_version;
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +0000912 TransportDescription session_td(NS_JINGLE_ICE_UDP,
913 std::string(), std::string());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000914 RtpHeaderExtensions session_extmaps;
915 cricket::SessionDescription* desc = new cricket::SessionDescription();
916 std::vector<JsepIceCandidate*> candidates;
917 size_t current_pos = 0;
918 bool supports_msid = false;
919
920 // Session Description
921 if (!ParseSessionDescription(message, &current_pos, &session_id,
922 &session_version, &supports_msid, &session_td,
923 &session_extmaps, desc, error)) {
924 delete desc;
925 return false;
926 }
927
928 // Media Description
929 if (!ParseMediaDescription(message, session_td, session_extmaps,
930 supports_msid, &current_pos, desc, &candidates,
931 error)) {
932 delete desc;
933 for (std::vector<JsepIceCandidate*>::const_iterator
934 it = candidates.begin(); it != candidates.end(); ++it) {
935 delete *it;
936 }
937 return false;
938 }
939
940 jdesc->Initialize(desc, session_id, session_version);
941
942 for (std::vector<JsepIceCandidate*>::const_iterator
943 it = candidates.begin(); it != candidates.end(); ++it) {
944 jdesc->AddCandidate(*it);
945 delete *it;
946 }
947 return true;
948}
949
950bool SdpDeserializeCandidate(const std::string& message,
951 JsepIceCandidate* jcandidate,
952 SdpParseError* error) {
953 ASSERT(jcandidate != NULL);
954 Candidate candidate;
955 if (!ParseCandidate(message, &candidate, error, true)) {
956 return false;
957 }
958 jcandidate->SetCandidate(candidate);
959 return true;
960}
961
962bool ParseCandidate(const std::string& message, Candidate* candidate,
963 SdpParseError* error, bool is_raw) {
964 ASSERT(candidate != NULL);
965
966 // Get the first line from |message|.
jiayl@webrtc.org7ec3f9f2014-08-08 23:09:15 +0000967 std::string first_line = message;
968 size_t pos = 0;
969 GetLine(message, &pos, &first_line);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000970
jiayl@webrtc.org7ec3f9f2014-08-08 23:09:15 +0000971 // Makes sure |message| contains only one line.
972 if (message.size() > first_line.size()) {
973 std::string left, right;
974 if (SplitByDelimiter(message, kNewLine, &left, &right) && !right.empty()) {
975 return ParseFailed(message, 0, "Expect one line only", error);
976 }
977 }
978
979 // From WebRTC draft section 4.8.1.1 candidate-attribute should be
980 // candidate:<candidate> when trickled, but we still support
981 // a=candidate:<blah>CRLF for backward compatibility and for parsing a line
982 // from the SDP.
983 if (IsLineType(first_line, kLineTypeAttributes)) {
984 first_line = first_line.substr(kLinePrefixLength);
985 }
986
987 std::string attribute_candidate;
988 std::string candidate_value;
989
990 // |first_line| must be in the form of "candidate:<value>".
991 if (!SplitByDelimiter(first_line, kSdpDelimiterColon,
992 &attribute_candidate, &candidate_value) ||
993 attribute_candidate != kAttributeCandidate) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000994 if (is_raw) {
995 std::ostringstream description;
jiayl@webrtc.org7ec3f9f2014-08-08 23:09:15 +0000996 description << "Expect line: " << kAttributeCandidate
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000997 << ":" << "<candidate-str>";
998 return ParseFailed(first_line, 0, description.str(), error);
999 } else {
1000 return ParseFailedExpectLine(first_line, 0, kLineTypeAttributes,
1001 kAttributeCandidate, error);
1002 }
1003 }
1004
1005 std::vector<std::string> fields;
jiayl@webrtc.org7ec3f9f2014-08-08 23:09:15 +00001006 rtc::split(candidate_value, kSdpDelimiterSpace, &fields);
1007
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001008 // RFC 5245
1009 // a=candidate:<foundation> <component-id> <transport> <priority>
1010 // <connection-address> <port> typ <candidate-types>
1011 // [raddr <connection-address>] [rport <port>]
1012 // *(SP extension-att-name SP extension-att-value)
1013 const size_t expected_min_fields = 8;
1014 if (fields.size() < expected_min_fields ||
1015 (fields[6] != kAttributeCandidateTyp)) {
1016 return ParseFailedExpectMinFieldNum(first_line, expected_min_fields, error);
1017 }
jiayl@webrtc.org7ec3f9f2014-08-08 23:09:15 +00001018 std::string foundation = fields[0];
1019
wu@webrtc.org5e760e72014-04-02 23:19:09 +00001020 int component_id = 0;
1021 if (!GetValueFromString(first_line, fields[1], &component_id, error)) {
1022 return false;
1023 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001024 const std::string transport = fields[2];
wu@webrtc.org5e760e72014-04-02 23:19:09 +00001025 uint32 priority = 0;
1026 if (!GetValueFromString(first_line, fields[3], &priority, error)) {
1027 return false;
1028 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001029 const std::string connection_address = fields[4];
wu@webrtc.org5e760e72014-04-02 23:19:09 +00001030 int port = 0;
1031 if (!GetValueFromString(first_line, fields[5], &port, error)) {
1032 return false;
1033 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001034 SocketAddress address(connection_address, port);
1035
1036 cricket::ProtocolType protocol;
1037 if (!StringToProto(transport.c_str(), &protocol)) {
1038 return ParseFailed(first_line, "Unsupported transport type.", error);
1039 }
1040
1041 std::string candidate_type;
1042 const std::string type = fields[7];
1043 if (type == kCandidateHost) {
1044 candidate_type = cricket::LOCAL_PORT_TYPE;
1045 } else if (type == kCandidateSrflx) {
1046 candidate_type = cricket::STUN_PORT_TYPE;
1047 } else if (type == kCandidateRelay) {
1048 candidate_type = cricket::RELAY_PORT_TYPE;
1049 } else {
1050 return ParseFailed(first_line, "Unsupported candidate type.", error);
1051 }
1052
1053 size_t current_position = expected_min_fields;
1054 SocketAddress related_address;
1055 // The 2 optional fields for related address
1056 // [raddr <connection-address>] [rport <port>]
1057 if (fields.size() >= (current_position + 2) &&
1058 fields[current_position] == kAttributeCandidateRaddr) {
1059 related_address.SetIP(fields[++current_position]);
1060 ++current_position;
1061 }
1062 if (fields.size() >= (current_position + 2) &&
1063 fields[current_position] == kAttributeCandidateRport) {
wu@webrtc.org5e760e72014-04-02 23:19:09 +00001064 int port = 0;
1065 if (!GetValueFromString(
1066 first_line, fields[++current_position], &port, error)) {
1067 return false;
1068 }
1069 related_address.SetPort(port);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001070 ++current_position;
1071 }
1072
mallinath@webrtc.org2d60c5e2014-08-08 22:29:20 +00001073 // If this is a TCP candidate, it has additional extension as defined in
1074 // RFC 6544.
1075 std::string tcptype;
1076 if (fields.size() >= (current_position + 2) &&
1077 fields[current_position] == kTcpCandidateType) {
1078 tcptype = fields[++current_position];
1079 ++current_position;
1080
1081 if (tcptype != cricket::TCPTYPE_ACTIVE_STR &&
1082 tcptype != cricket::TCPTYPE_PASSIVE_STR &&
1083 tcptype != cricket::TCPTYPE_SIMOPEN_STR) {
1084 return ParseFailed(first_line, "Invalid TCP candidate type.", error);
1085 }
1086
1087 if (protocol != cricket::PROTO_TCP) {
1088 return ParseFailed(first_line, "Invalid non-TCP candidate", error);
1089 }
1090 }
1091
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001092 // Extension
1093 // Empty string as the candidate username and password.
1094 // Will be updated later with the ice-ufrag and ice-pwd.
1095 // TODO: Remove the username/password extension, which is currently
1096 // kept for backwards compatibility.
1097 std::string username;
1098 std::string password;
1099 uint32 generation = 0;
1100 for (size_t i = current_position; i + 1 < fields.size(); ++i) {
1101 // RFC 5245
1102 // *(SP extension-att-name SP extension-att-value)
1103 if (fields[i] == kAttributeCandidateGeneration) {
wu@webrtc.org5e760e72014-04-02 23:19:09 +00001104 if (!GetValueFromString(first_line, fields[++i], &generation, error)) {
1105 return false;
1106 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001107 } else if (fields[i] == kAttributeCandidateUsername) {
1108 username = fields[++i];
1109 } else if (fields[i] == kAttributeCandidatePassword) {
1110 password = fields[++i];
1111 } else {
1112 // Skip the unknown extension.
1113 ++i;
1114 }
1115 }
1116
guoweis@webrtc.org61c12472015-01-15 06:53:07 +00001117 *candidate = Candidate(component_id, cricket::ProtoToString(protocol),
guoweis@webrtc.org950c5182014-12-16 23:01:31 +00001118 address, priority, username, password, candidate_type,
1119 generation, foundation);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001120 candidate->set_related_address(related_address);
mallinath@webrtc.org2d60c5e2014-08-08 22:29:20 +00001121 candidate->set_tcptype(tcptype);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001122 return true;
1123}
1124
1125bool ParseIceOptions(const std::string& line,
1126 std::vector<std::string>* transport_options,
1127 SdpParseError* error) {
1128 std::string ice_options;
1129 if (!GetValue(line, kAttributeIceOption, &ice_options, error)) {
1130 return false;
1131 }
1132 std::vector<std::string> fields;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001133 rtc::split(ice_options, kSdpDelimiterSpace, &fields);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001134 for (size_t i = 0; i < fields.size(); ++i) {
1135 transport_options->push_back(fields[i]);
1136 }
1137 return true;
1138}
1139
jiayl@webrtc.orgddb85ab2014-09-05 16:31:56 +00001140bool ParseSctpPort(const std::string& line,
1141 int* sctp_port,
1142 SdpParseError* error) {
1143 // draft-ietf-mmusic-sctp-sdp-07
1144 // a=sctp-port
1145 std::vector<std::string> fields;
1146 rtc::split(line.substr(kLinePrefixLength),
1147 kSdpDelimiterSpace, &fields);
1148 const size_t expected_min_fields = 2;
1149 if (fields.size() < expected_min_fields) {
1150 return ParseFailedExpectMinFieldNum(line, expected_min_fields, error);
1151 }
1152 if (!rtc::FromString(fields[1], sctp_port)) {
1153 return ParseFailed(line,
1154 "Invalid sctp port value.",
1155 error);
1156 }
1157 return true;
1158}
1159
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001160bool ParseExtmap(const std::string& line, RtpHeaderExtension* extmap,
1161 SdpParseError* error) {
1162 // RFC 5285
1163 // a=extmap:<value>["/"<direction>] <URI> <extensionattributes>
1164 std::vector<std::string> fields;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001165 rtc::split(line.substr(kLinePrefixLength),
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001166 kSdpDelimiterSpace, &fields);
1167 const size_t expected_min_fields = 2;
1168 if (fields.size() < expected_min_fields) {
1169 return ParseFailedExpectMinFieldNum(line, expected_min_fields, error);
1170 }
1171 std::string uri = fields[1];
1172
1173 std::string value_direction;
1174 if (!GetValue(fields[0], kAttributeExtmap, &value_direction, error)) {
1175 return false;
1176 }
1177 std::vector<std::string> sub_fields;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001178 rtc::split(value_direction, kSdpDelimiterSlash, &sub_fields);
wu@webrtc.org5e760e72014-04-02 23:19:09 +00001179 int value = 0;
1180 if (!GetValueFromString(line, sub_fields[0], &value, error)) {
1181 return false;
1182 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001183
1184 *extmap = RtpHeaderExtension(uri, value);
1185 return true;
1186}
1187
1188void BuildMediaDescription(const ContentInfo* content_info,
1189 const TransportInfo* transport_info,
1190 const MediaType media_type,
wu@webrtc.org4c3e9912014-07-16 21:03:13 +00001191 const std::vector<Candidate>& candidates,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001192 std::string* message) {
1193 ASSERT(message != NULL);
1194 if (content_info == NULL || message == NULL) {
1195 return;
1196 }
1197 // TODO: Rethink if we should use sprintfn instead of stringstream.
1198 // According to the style guide, streams should only be used for logging.
1199 // http://google-styleguide.googlecode.com/svn/
1200 // trunk/cppguide.xml?showone=Streams#Streams
1201 std::ostringstream os;
1202 const MediaContentDescription* media_desc =
henrike@webrtc.org28654cb2013-07-22 21:07:49 +00001203 static_cast<const MediaContentDescription*>(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001204 content_info->description);
1205 ASSERT(media_desc != NULL);
1206
jiayl@webrtc.org9c16c392014-05-01 18:30:30 +00001207 int sctp_port = cricket::kSctpDefaultPort;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001208
1209 // RFC 4566
1210 // m=<media> <port> <proto> <fmt>
1211 // fmt is a list of payload type numbers that MAY be used in the session.
1212 const char* type = NULL;
1213 if (media_type == cricket::MEDIA_TYPE_AUDIO)
1214 type = kMediaTypeAudio;
1215 else if (media_type == cricket::MEDIA_TYPE_VIDEO)
1216 type = kMediaTypeVideo;
1217 else if (media_type == cricket::MEDIA_TYPE_DATA)
1218 type = kMediaTypeData;
1219 else
1220 ASSERT(false);
1221
1222 std::string fmt;
1223 if (media_type == cricket::MEDIA_TYPE_VIDEO) {
1224 const VideoContentDescription* video_desc =
1225 static_cast<const VideoContentDescription*>(media_desc);
1226 for (std::vector<cricket::VideoCodec>::const_iterator it =
1227 video_desc->codecs().begin();
1228 it != video_desc->codecs().end(); ++it) {
1229 fmt.append(" ");
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001230 fmt.append(rtc::ToString<int>(it->id));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001231 }
1232 } else if (media_type == cricket::MEDIA_TYPE_AUDIO) {
1233 const AudioContentDescription* audio_desc =
1234 static_cast<const AudioContentDescription*>(media_desc);
1235 for (std::vector<cricket::AudioCodec>::const_iterator it =
1236 audio_desc->codecs().begin();
1237 it != audio_desc->codecs().end(); ++it) {
1238 fmt.append(" ");
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001239 fmt.append(rtc::ToString<int>(it->id));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001240 }
1241 } else if (media_type == cricket::MEDIA_TYPE_DATA) {
jiayl@webrtc.org9c16c392014-05-01 18:30:30 +00001242 const DataContentDescription* data_desc =
1243 static_cast<const DataContentDescription*>(media_desc);
pthatcher@webrtc.org3341b402015-02-13 21:14:22 +00001244 if (IsDtlsSctp(media_desc->protocol())) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001245 fmt.append(" ");
jiayl@webrtc.org9c16c392014-05-01 18:30:30 +00001246
1247 for (std::vector<cricket::DataCodec>::const_iterator it =
1248 data_desc->codecs().begin();
1249 it != data_desc->codecs().end(); ++it) {
1250 if (it->id == cricket::kGoogleSctpDataCodecId &&
1251 it->GetParam(cricket::kCodecParamPort, &sctp_port)) {
1252 break;
1253 }
1254 }
1255
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001256 fmt.append(rtc::ToString<int>(sctp_port));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001257 } else {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001258 for (std::vector<cricket::DataCodec>::const_iterator it =
1259 data_desc->codecs().begin();
1260 it != data_desc->codecs().end(); ++it) {
1261 fmt.append(" ");
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001262 fmt.append(rtc::ToString<int>(it->id));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001263 }
1264 }
1265 }
1266 // The fmt must never be empty. If no codecs are found, set the fmt attribute
1267 // to 0.
1268 if (fmt.empty()) {
1269 fmt = " 0";
1270 }
1271
1272 // The port number in the m line will be updated later when associate with
1273 // the candidates.
1274 // RFC 3264
1275 // To reject an offered stream, the port number in the corresponding stream in
1276 // the answer MUST be set to zero.
1277 const std::string port = content_info->rejected ?
pthatcher@webrtc.orgc9d6d142014-10-23 23:37:22 +00001278 kMediaPortRejected : kDummyPort;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001279
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001280 rtc::SSLFingerprint* fp = (transport_info) ?
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001281 transport_info->description.identity_fingerprint.get() : NULL;
1282
wu@webrtc.org4c3e9912014-07-16 21:03:13 +00001283 // Add the m and c lines.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001284 InitLine(kLineTypeMedia, type, &os);
1285 os << " " << port << " " << media_desc->protocol() << fmt;
wu@webrtc.org4c3e9912014-07-16 21:03:13 +00001286 std::string mline = os.str();
1287 UpdateMediaDefaultDestination(candidates, mline, message);
1288
1289 // RFC 4566
1290 // b=AS:<bandwidth>
1291 // We should always use the default bandwidth for RTP-based data
1292 // channels. Don't allow SDP to set the bandwidth, because that
1293 // would give JS the opportunity to "break the Internet".
1294 // TODO(pthatcher): But we need to temporarily allow the SDP to control
1295 // this for backwards-compatibility. Once we don't need that any
1296 // more, remove this.
1297 bool support_dc_sdp_bandwidth_temporarily = true;
1298 if (media_desc->bandwidth() >= 1000 &&
1299 (media_type != cricket::MEDIA_TYPE_DATA ||
1300 support_dc_sdp_bandwidth_temporarily)) {
1301 InitLine(kLineTypeSessionBandwidth, kApplicationSpecificMaximum, &os);
1302 os << kSdpDelimiterColon << (media_desc->bandwidth() / 1000);
1303 AddLine(os.str(), message);
1304 }
1305
1306 // Add the a=rtcp line.
pthatcher@webrtc.org3341b402015-02-13 21:14:22 +00001307 if (IsRtp(media_desc->protocol())) {
wu@webrtc.org4c3e9912014-07-16 21:03:13 +00001308 std::string rtcp_line = GetRtcpLine(candidates);
1309 if (!rtcp_line.empty()) {
1310 AddLine(rtcp_line, message);
1311 }
1312 }
1313
1314 // Build the a=candidate lines.
1315 BuildCandidate(candidates, message);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001316
1317 // Use the transport_info to build the media level ice-ufrag and ice-pwd.
1318 if (transport_info) {
1319 // RFC 5245
1320 // ice-pwd-att = "ice-pwd" ":" password
1321 // ice-ufrag-att = "ice-ufrag" ":" ufrag
1322 // ice-ufrag
1323 InitAttrLine(kAttributeIceUfrag, &os);
1324 os << kSdpDelimiterColon << transport_info->description.ice_ufrag;
1325 AddLine(os.str(), message);
1326 // ice-pwd
1327 InitAttrLine(kAttributeIcePwd, &os);
1328 os << kSdpDelimiterColon << transport_info->description.ice_pwd;
1329 AddLine(os.str(), message);
1330
1331 // draft-petithuguenin-mmusic-ice-attributes-level-03
1332 BuildIceOptions(transport_info->description.transport_options, message);
1333
1334 // RFC 4572
1335 // fingerprint-attribute =
1336 // "fingerprint" ":" hash-func SP fingerprint
1337 if (fp) {
1338 // Insert the fingerprint attribute.
1339 InitAttrLine(kAttributeFingerprint, &os);
1340 os << kSdpDelimiterColon
1341 << fp->algorithm << kSdpDelimiterSpace
1342 << fp->GetRfc4572Fingerprint();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001343 AddLine(os.str(), message);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001344
1345 // Inserting setup attribute.
1346 if (transport_info->description.connection_role !=
1347 cricket::CONNECTIONROLE_NONE) {
1348 // Making sure we are not using "passive" mode.
1349 cricket::ConnectionRole role =
1350 transport_info->description.connection_role;
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00001351 std::string dtls_role_str;
1352 VERIFY(cricket::ConnectionRoleToString(role, &dtls_role_str));
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001353 InitAttrLine(kAttributeSetup, &os);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001354 os << kSdpDelimiterColon << dtls_role_str;
1355 AddLine(os.str(), message);
1356 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001357 }
1358 }
1359
1360 // RFC 3388
1361 // mid-attribute = "a=mid:" identification-tag
1362 // identification-tag = token
1363 // Use the content name as the mid identification-tag.
1364 InitAttrLine(kAttributeMid, &os);
1365 os << kSdpDelimiterColon << content_info->name;
1366 AddLine(os.str(), message);
1367
pthatcher@webrtc.org3341b402015-02-13 21:14:22 +00001368 if (IsDtlsSctp(media_desc->protocol())) {
jiayl@webrtc.org9c16c392014-05-01 18:30:30 +00001369 BuildSctpContentAttributes(message, sctp_port);
pthatcher@webrtc.org3341b402015-02-13 21:14:22 +00001370 } else if (IsRtp(media_desc->protocol())) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001371 BuildRtpContentAttributes(media_desc, media_type, message);
1372 }
1373}
1374
jiayl@webrtc.org9c16c392014-05-01 18:30:30 +00001375void BuildSctpContentAttributes(std::string* message, int sctp_port) {
wu@webrtc.org78187522013-10-07 23:32:02 +00001376 // draft-ietf-mmusic-sctp-sdp-04
1377 // a=sctpmap:sctpmap-number protocol [streams]
1378 std::ostringstream os;
1379 InitAttrLine(kAttributeSctpmap, &os);
jiayl@webrtc.org9c16c392014-05-01 18:30:30 +00001380 os << kSdpDelimiterColon << sctp_port << kSdpDelimiterSpace
wu@webrtc.org78187522013-10-07 23:32:02 +00001381 << kDefaultSctpmapProtocol << kSdpDelimiterSpace
1382 << (cricket::kMaxSctpSid + 1);
1383 AddLine(os.str(), message);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001384}
1385
1386void BuildRtpContentAttributes(
1387 const MediaContentDescription* media_desc,
1388 const MediaType media_type,
1389 std::string* message) {
1390 std::ostringstream os;
1391 // RFC 5285
1392 // a=extmap:<value>["/"<direction>] <URI> <extensionattributes>
1393 // The definitions MUST be either all session level or all media level. This
1394 // implementation uses all media level.
1395 for (size_t i = 0; i < media_desc->rtp_header_extensions().size(); ++i) {
1396 InitAttrLine(kAttributeExtmap, &os);
1397 os << kSdpDelimiterColon << media_desc->rtp_header_extensions()[i].id
1398 << kSdpDelimiterSpace << media_desc->rtp_header_extensions()[i].uri;
1399 AddLine(os.str(), message);
1400 }
1401
1402 // RFC 3264
1403 // a=sendrecv || a=sendonly || a=sendrecv || a=inactive
1404
1405 cricket::MediaContentDirection direction = media_desc->direction();
1406 if (media_desc->streams().empty() && direction == cricket::MD_SENDRECV) {
1407 direction = cricket::MD_RECVONLY;
1408 }
1409
1410 switch (direction) {
1411 case cricket::MD_INACTIVE:
1412 InitAttrLine(kAttributeInactive, &os);
1413 break;
1414 case cricket::MD_SENDONLY:
1415 InitAttrLine(kAttributeSendOnly, &os);
1416 break;
1417 case cricket::MD_RECVONLY:
1418 InitAttrLine(kAttributeRecvOnly, &os);
1419 break;
1420 case cricket::MD_SENDRECV:
1421 default:
1422 InitAttrLine(kAttributeSendRecv, &os);
1423 break;
1424 }
1425 AddLine(os.str(), message);
1426
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001427 // RFC 5761
1428 // a=rtcp-mux
1429 if (media_desc->rtcp_mux()) {
1430 InitAttrLine(kAttributeRtcpMux, &os);
1431 AddLine(os.str(), message);
1432 }
1433
1434 // RFC 4568
1435 // a=crypto:<tag> <crypto-suite> <key-params> [<session-params>]
1436 for (std::vector<CryptoParams>::const_iterator it =
1437 media_desc->cryptos().begin();
1438 it != media_desc->cryptos().end(); ++it) {
1439 InitAttrLine(kAttributeCrypto, &os);
1440 os << kSdpDelimiterColon << it->tag << " " << it->cipher_suite << " "
1441 << it->key_params;
1442 if (!it->session_params.empty()) {
1443 os << " " << it->session_params;
1444 }
1445 AddLine(os.str(), message);
1446 }
1447
1448 // RFC 4566
1449 // a=rtpmap:<payload type> <encoding name>/<clock rate>
1450 // [/<encodingparameters>]
1451 BuildRtpMap(media_desc, media_type, message);
1452
1453 // Specify latency for buffered mode.
1454 // a=x-google-buffer-latency:<value>
1455 if (media_desc->buffered_mode_latency() != cricket::kBufferedModeDisabled) {
1456 std::ostringstream os;
1457 InitAttrLine(kAttributeXGoogleBufferLatency, &os);
1458 os << kSdpDelimiterColon << media_desc->buffered_mode_latency();
1459 AddLine(os.str(), message);
1460 }
1461
1462 for (StreamParamsVec::const_iterator track = media_desc->streams().begin();
1463 track != media_desc->streams().end(); ++track) {
1464 // Require that the track belongs to a media stream,
1465 // ie the sync_label is set. This extra check is necessary since the
1466 // MediaContentDescription always contains a streamparam with an ssrc even
1467 // if no track or media stream have been created.
1468 if (track->sync_label.empty()) continue;
1469
1470 // Build the ssrc-group lines.
1471 for (size_t i = 0; i < track->ssrc_groups.size(); ++i) {
1472 // RFC 5576
1473 // a=ssrc-group:<semantics> <ssrc-id> ...
1474 if (track->ssrc_groups[i].ssrcs.empty()) {
1475 continue;
1476 }
1477 std::ostringstream os;
1478 InitAttrLine(kAttributeSsrcGroup, &os);
1479 os << kSdpDelimiterColon << track->ssrc_groups[i].semantics;
1480 std::vector<uint32>::const_iterator ssrc =
1481 track->ssrc_groups[i].ssrcs.begin();
1482 for (; ssrc != track->ssrc_groups[i].ssrcs.end(); ++ssrc) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001483 os << kSdpDelimiterSpace << rtc::ToString<uint32>(*ssrc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001484 }
1485 AddLine(os.str(), message);
1486 }
1487 // Build the ssrc lines for each ssrc.
1488 for (size_t i = 0; i < track->ssrcs.size(); ++i) {
1489 uint32 ssrc = track->ssrcs[i];
1490 // RFC 5576
1491 // a=ssrc:<ssrc-id> cname:<value>
1492 AddSsrcLine(ssrc, kSsrcAttributeCname,
1493 track->cname, message);
1494
1495 // draft-alvestrand-mmusic-msid-00
1496 // a=ssrc:<ssrc-id> msid:identifier [appdata]
1497 // The appdata consists of the "id" attribute of a MediaStreamTrack, which
1498 // is corresponding to the "name" attribute of StreamParams.
1499 std::string appdata = track->id;
1500 std::ostringstream os;
1501 InitAttrLine(kAttributeSsrc, &os);
1502 os << kSdpDelimiterColon << ssrc << kSdpDelimiterSpace
1503 << kSsrcAttributeMsid << kSdpDelimiterColon << track->sync_label
1504 << kSdpDelimiterSpace << appdata;
1505 AddLine(os.str(), message);
1506
1507 // TODO(ronghuawu): Remove below code which is for backward compatibility.
1508 // draft-alvestrand-rtcweb-mid-01
1509 // a=ssrc:<ssrc-id> mslabel:<value>
1510 // The label isn't yet defined.
1511 // a=ssrc:<ssrc-id> label:<value>
1512 AddSsrcLine(ssrc, kSsrcAttributeMslabel, track->sync_label, message);
1513 AddSsrcLine(ssrc, kSSrcAttributeLabel, track->id, message);
1514 }
1515 }
1516}
1517
1518void WriteFmtpHeader(int payload_type, std::ostringstream* os) {
1519 // fmtp header: a=fmtp:|payload_type| <parameters>
1520 // Add a=fmtp
1521 InitAttrLine(kAttributeFmtp, os);
1522 // Add :|payload_type|
1523 *os << kSdpDelimiterColon << payload_type;
1524}
1525
1526void WriteRtcpFbHeader(int payload_type, std::ostringstream* os) {
1527 // rtcp-fb header: a=rtcp-fb:|payload_type|
1528 // <parameters>/<ccm <ccm_parameters>>
1529 // Add a=rtcp-fb
1530 InitAttrLine(kAttributeRtcpFb, os);
1531 // Add :
1532 *os << kSdpDelimiterColon;
1533 if (payload_type == kWildcardPayloadType) {
1534 *os << "*";
1535 } else {
1536 *os << payload_type;
1537 }
1538}
1539
1540void WriteFmtpParameter(const std::string& parameter_name,
1541 const std::string& parameter_value,
1542 std::ostringstream* os) {
1543 // fmtp parameters: |parameter_name|=|parameter_value|
1544 *os << parameter_name << kSdpDelimiterEqual << parameter_value;
1545}
1546
1547void WriteFmtpParameters(const cricket::CodecParameterMap& parameters,
1548 std::ostringstream* os) {
1549 for (cricket::CodecParameterMap::const_iterator fmtp = parameters.begin();
1550 fmtp != parameters.end(); ++fmtp) {
1551 // Each new parameter, except the first one starts with ";" and " ".
1552 if (fmtp != parameters.begin()) {
1553 *os << kSdpDelimiterSemicolon;
1554 }
1555 *os << kSdpDelimiterSpace;
1556 WriteFmtpParameter(fmtp->first, fmtp->second, os);
1557 }
1558}
1559
1560bool IsFmtpParam(const std::string& name) {
1561 const char* kFmtpParams[] = {
1562 kCodecParamMinPTime, kCodecParamSPropStereo,
buildbot@webrtc.orged97bb02014-05-07 11:15:20 +00001563 kCodecParamStereo, kCodecParamUseInbandFec, kCodecParamStartBitrate,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001564 kCodecParamMaxBitrate, kCodecParamMinBitrate, kCodecParamMaxQuantization,
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001565 kCodecParamSctpProtocol, kCodecParamSctpStreams,
buildbot@webrtc.org5d639b32014-09-10 07:57:12 +00001566 kCodecParamMaxAverageBitrate, kCodecParamMaxPlaybackRate,
1567 kCodecParamAssociatedPayloadType
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001568 };
1569 for (size_t i = 0; i < ARRAY_SIZE(kFmtpParams); ++i) {
1570 if (_stricmp(name.c_str(), kFmtpParams[i]) == 0) {
1571 return true;
1572 }
1573 }
1574 return false;
1575}
1576
1577// Retreives fmtp parameters from |params|, which may contain other parameters
1578// as well, and puts them in |fmtp_parameters|.
1579void GetFmtpParams(const cricket::CodecParameterMap& params,
1580 cricket::CodecParameterMap* fmtp_parameters) {
1581 for (cricket::CodecParameterMap::const_iterator iter = params.begin();
1582 iter != params.end(); ++iter) {
1583 if (IsFmtpParam(iter->first)) {
1584 (*fmtp_parameters)[iter->first] = iter->second;
1585 }
1586 }
1587}
1588
1589template <class T>
1590void AddFmtpLine(const T& codec, std::string* message) {
1591 cricket::CodecParameterMap fmtp_parameters;
1592 GetFmtpParams(codec.params, &fmtp_parameters);
1593 if (fmtp_parameters.empty()) {
1594 // No need to add an fmtp if it will have no (optional) parameters.
1595 return;
1596 }
1597 std::ostringstream os;
1598 WriteFmtpHeader(codec.id, &os);
1599 WriteFmtpParameters(fmtp_parameters, &os);
1600 AddLine(os.str(), message);
1601 return;
1602}
1603
1604template <class T>
1605void AddRtcpFbLines(const T& codec, std::string* message) {
1606 for (std::vector<cricket::FeedbackParam>::const_iterator iter =
1607 codec.feedback_params.params().begin();
1608 iter != codec.feedback_params.params().end(); ++iter) {
1609 std::ostringstream os;
1610 WriteRtcpFbHeader(codec.id, &os);
1611 os << " " << iter->id();
1612 if (!iter->param().empty()) {
1613 os << " " << iter->param();
1614 }
1615 AddLine(os.str(), message);
1616 }
1617}
1618
jiayl@webrtc.orgddb85ab2014-09-05 16:31:56 +00001619bool AddSctpDataCodec(DataContentDescription* media_desc,
1620 int sctp_port) {
1621 if (media_desc->HasCodec(cricket::kGoogleSctpDataCodecId)) {
1622 return ParseFailed("",
1623 "Can't have multiple sctp port attributes.",
1624 NULL);
1625 }
1626 // Add the SCTP Port number as a pseudo-codec "port" parameter
1627 cricket::DataCodec codec_port(
1628 cricket::kGoogleSctpDataCodecId, cricket::kGoogleSctpDataCodecName,
1629 0);
1630 codec_port.SetParam(cricket::kCodecParamPort, sctp_port);
1631 LOG(INFO) << "AddSctpDataCodec: Got SCTP Port Number "
1632 << sctp_port;
1633 media_desc->AddCodec(codec_port);
1634 return true;
1635}
1636
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001637bool GetMinValue(const std::vector<int>& values, int* value) {
1638 if (values.empty()) {
1639 return false;
1640 }
1641 std::vector<int>::const_iterator found =
1642 std::min_element(values.begin(), values.end());
1643 *value = *found;
1644 return true;
1645}
1646
1647bool GetParameter(const std::string& name,
1648 const cricket::CodecParameterMap& params, int* value) {
1649 std::map<std::string, std::string>::const_iterator found =
1650 params.find(name);
1651 if (found == params.end()) {
1652 return false;
1653 }
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001654 if (!rtc::FromString(found->second, value)) {
wu@webrtc.org5e760e72014-04-02 23:19:09 +00001655 return false;
1656 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001657 return true;
1658}
1659
1660void BuildRtpMap(const MediaContentDescription* media_desc,
1661 const MediaType media_type,
1662 std::string* message) {
1663 ASSERT(message != NULL);
1664 ASSERT(media_desc != NULL);
1665 std::ostringstream os;
1666 if (media_type == cricket::MEDIA_TYPE_VIDEO) {
1667 const VideoContentDescription* video_desc =
1668 static_cast<const VideoContentDescription*>(media_desc);
1669 for (std::vector<cricket::VideoCodec>::const_iterator it =
1670 video_desc->codecs().begin();
1671 it != video_desc->codecs().end(); ++it) {
1672 // RFC 4566
1673 // a=rtpmap:<payload type> <encoding name>/<clock rate>
1674 // [/<encodingparameters>]
1675 if (it->id != kWildcardPayloadType) {
1676 InitAttrLine(kAttributeRtpmap, &os);
1677 os << kSdpDelimiterColon << it->id << " " << it->name
1678 << "/" << kDefaultVideoClockrate;
1679 AddLine(os.str(), message);
1680 }
1681 AddRtcpFbLines(*it, message);
1682 AddFmtpLine(*it, message);
1683 }
1684 } else if (media_type == cricket::MEDIA_TYPE_AUDIO) {
1685 const AudioContentDescription* audio_desc =
1686 static_cast<const AudioContentDescription*>(media_desc);
1687 std::vector<int> ptimes;
1688 std::vector<int> maxptimes;
1689 int max_minptime = 0;
1690 for (std::vector<cricket::AudioCodec>::const_iterator it =
1691 audio_desc->codecs().begin();
1692 it != audio_desc->codecs().end(); ++it) {
1693 ASSERT(!it->name.empty());
1694 // RFC 4566
1695 // a=rtpmap:<payload type> <encoding name>/<clock rate>
1696 // [/<encodingparameters>]
1697 InitAttrLine(kAttributeRtpmap, &os);
1698 os << kSdpDelimiterColon << it->id << " ";
1699 os << it->name << "/" << it->clockrate;
1700 if (it->channels != 1) {
1701 os << "/" << it->channels;
1702 }
1703 AddLine(os.str(), message);
1704 AddRtcpFbLines(*it, message);
1705 AddFmtpLine(*it, message);
1706 int minptime = 0;
1707 if (GetParameter(kCodecParamMinPTime, it->params, &minptime)) {
1708 max_minptime = std::max(minptime, max_minptime);
1709 }
1710 int ptime;
1711 if (GetParameter(kCodecParamPTime, it->params, &ptime)) {
1712 ptimes.push_back(ptime);
1713 }
1714 int maxptime;
1715 if (GetParameter(kCodecParamMaxPTime, it->params, &maxptime)) {
1716 maxptimes.push_back(maxptime);
1717 }
1718 }
1719 // Populate the maxptime attribute with the smallest maxptime of all codecs
1720 // under the same m-line.
1721 int min_maxptime = INT_MAX;
1722 if (GetMinValue(maxptimes, &min_maxptime)) {
1723 AddAttributeLine(kCodecParamMaxPTime, min_maxptime, message);
1724 }
1725 ASSERT(min_maxptime > max_minptime);
1726 // Populate the ptime attribute with the smallest ptime or the largest
1727 // minptime, whichever is the largest, for all codecs under the same m-line.
1728 int ptime = INT_MAX;
1729 if (GetMinValue(ptimes, &ptime)) {
1730 ptime = std::min(ptime, min_maxptime);
1731 ptime = std::max(ptime, max_minptime);
1732 AddAttributeLine(kCodecParamPTime, ptime, message);
1733 }
1734 } else if (media_type == cricket::MEDIA_TYPE_DATA) {
1735 const DataContentDescription* data_desc =
1736 static_cast<const DataContentDescription*>(media_desc);
1737 for (std::vector<cricket::DataCodec>::const_iterator it =
1738 data_desc->codecs().begin();
1739 it != data_desc->codecs().end(); ++it) {
1740 // RFC 4566
1741 // a=rtpmap:<payload type> <encoding name>/<clock rate>
1742 // [/<encodingparameters>]
1743 InitAttrLine(kAttributeRtpmap, &os);
1744 os << kSdpDelimiterColon << it->id << " "
1745 << it->name << "/" << it->clockrate;
1746 AddLine(os.str(), message);
1747 }
1748 }
1749}
1750
1751void BuildCandidate(const std::vector<Candidate>& candidates,
1752 std::string* message) {
1753 std::ostringstream os;
1754
1755 for (std::vector<Candidate>::const_iterator it = candidates.begin();
1756 it != candidates.end(); ++it) {
1757 // RFC 5245
1758 // a=candidate:<foundation> <component-id> <transport> <priority>
1759 // <connection-address> <port> typ <candidate-types>
1760 // [raddr <connection-address>] [rport <port>]
1761 // *(SP extension-att-name SP extension-att-value)
1762 std::string type;
1763 // Map the cricket candidate type to "host" / "srflx" / "prflx" / "relay"
1764 if (it->type() == cricket::LOCAL_PORT_TYPE) {
1765 type = kCandidateHost;
1766 } else if (it->type() == cricket::STUN_PORT_TYPE) {
1767 type = kCandidateSrflx;
1768 } else if (it->type() == cricket::RELAY_PORT_TYPE) {
1769 type = kCandidateRelay;
1770 } else {
1771 ASSERT(false);
1772 }
1773
1774 InitAttrLine(kAttributeCandidate, &os);
1775 os << kSdpDelimiterColon
mallinath@webrtc.org2d60c5e2014-08-08 22:29:20 +00001776 << it->foundation() << " "
1777 << it->component() << " "
1778 << it->protocol() << " "
1779 << it->priority() << " "
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001780 << it->address().ipaddr().ToString() << " "
1781 << it->address().PortAsString() << " "
mallinath@webrtc.org2d60c5e2014-08-08 22:29:20 +00001782 << kAttributeCandidateTyp << " "
1783 << type << " ";
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001784
1785 // Related address
1786 if (!it->related_address().IsNil()) {
1787 os << kAttributeCandidateRaddr << " "
1788 << it->related_address().ipaddr().ToString() << " "
1789 << kAttributeCandidateRport << " "
1790 << it->related_address().PortAsString() << " ";
1791 }
1792
mallinath@webrtc.org2d60c5e2014-08-08 22:29:20 +00001793 if (it->protocol() == cricket::TCP_PROTOCOL_NAME) {
mallinath@webrtc.orge999bd02014-08-13 06:05:55 +00001794 os << kTcpCandidateType << " " << it->tcptype() << " ";
mallinath@webrtc.org2d60c5e2014-08-08 22:29:20 +00001795 }
1796
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001797 // Extensions
1798 os << kAttributeCandidateGeneration << " " << it->generation();
1799
1800 AddLine(os.str(), message);
1801 }
1802}
1803
1804void BuildIceOptions(const std::vector<std::string>& transport_options,
1805 std::string* message) {
1806 if (!transport_options.empty()) {
1807 std::ostringstream os;
1808 InitAttrLine(kAttributeIceOption, &os);
1809 os << kSdpDelimiterColon << transport_options[0];
1810 for (size_t i = 1; i < transport_options.size(); ++i) {
1811 os << kSdpDelimiterSpace << transport_options[i];
1812 }
1813 AddLine(os.str(), message);
1814 }
1815}
1816
pthatcher@webrtc.org3341b402015-02-13 21:14:22 +00001817bool IsRtp(const std::string& protocol) {
1818 return protocol.empty() ||
1819 (protocol.find(cricket::kMediaProtocolRtpPrefix) != std::string::npos);
1820}
1821
1822bool IsDtlsSctp(const std::string& protocol) {
1823 // This intentionally excludes "SCTP" and "SCTP/DTLS".
lally@webrtc.orga7470932015-02-24 20:19:21 +00001824 return protocol.find(cricket::kMediaProtocolDtlsSctp) != std::string::npos;
pthatcher@webrtc.org3341b402015-02-13 21:14:22 +00001825}
1826
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001827bool ParseSessionDescription(const std::string& message, size_t* pos,
1828 std::string* session_id,
1829 std::string* session_version,
1830 bool* supports_msid,
1831 TransportDescription* session_td,
1832 RtpHeaderExtensions* session_extmaps,
1833 cricket::SessionDescription* desc,
1834 SdpParseError* error) {
1835 std::string line;
1836
1837 // RFC 4566
1838 // v= (protocol version)
1839 if (!GetLineWithType(message, pos, &line, kLineTypeVersion)) {
1840 return ParseFailedExpectLine(message, *pos, kLineTypeVersion,
1841 std::string(), error);
1842 }
1843 // RFC 4566
1844 // o=<username> <sess-id> <sess-version> <nettype> <addrtype>
1845 // <unicast-address>
1846 if (!GetLineWithType(message, pos, &line, kLineTypeOrigin)) {
1847 return ParseFailedExpectLine(message, *pos, kLineTypeOrigin,
1848 std::string(), error);
1849 }
1850 std::vector<std::string> fields;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001851 rtc::split(line.substr(kLinePrefixLength),
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001852 kSdpDelimiterSpace, &fields);
1853 const size_t expected_fields = 6;
1854 if (fields.size() != expected_fields) {
1855 return ParseFailedExpectFieldNum(line, expected_fields, error);
1856 }
1857 *session_id = fields[1];
1858 *session_version = fields[2];
1859
1860 // RFC 4566
1861 // s= (session name)
1862 if (!GetLineWithType(message, pos, &line, kLineTypeSessionName)) {
1863 return ParseFailedExpectLine(message, *pos, kLineTypeSessionName,
1864 std::string(), error);
1865 }
1866
1867 // Optional lines
1868 // Those are the optional lines, so shouldn't return false if not present.
1869 // RFC 4566
1870 // i=* (session information)
1871 GetLineWithType(message, pos, &line, kLineTypeSessionInfo);
1872
1873 // RFC 4566
1874 // u=* (URI of description)
1875 GetLineWithType(message, pos, &line, kLineTypeSessionUri);
1876
1877 // RFC 4566
1878 // e=* (email address)
1879 GetLineWithType(message, pos, &line, kLineTypeSessionEmail);
1880
1881 // RFC 4566
1882 // p=* (phone number)
1883 GetLineWithType(message, pos, &line, kLineTypeSessionPhone);
1884
1885 // RFC 4566
1886 // c=* (connection information -- not required if included in
1887 // all media)
1888 GetLineWithType(message, pos, &line, kLineTypeConnection);
1889
1890 // RFC 4566
1891 // b=* (zero or more bandwidth information lines)
1892 while (GetLineWithType(message, pos, &line, kLineTypeSessionBandwidth)) {
1893 // By pass zero or more b lines.
1894 }
1895
1896 // RFC 4566
1897 // One or more time descriptions ("t=" and "r=" lines; see below)
1898 // t= (time the session is active)
1899 // r=* (zero or more repeat times)
1900 // Ensure there's at least one time description
1901 if (!GetLineWithType(message, pos, &line, kLineTypeTiming)) {
1902 return ParseFailedExpectLine(message, *pos, kLineTypeTiming, std::string(),
1903 error);
1904 }
1905
1906 while (GetLineWithType(message, pos, &line, kLineTypeRepeatTimes)) {
1907 // By pass zero or more r lines.
1908 }
1909
1910 // Go through the rest of the time descriptions
1911 while (GetLineWithType(message, pos, &line, kLineTypeTiming)) {
1912 while (GetLineWithType(message, pos, &line, kLineTypeRepeatTimes)) {
1913 // By pass zero or more r lines.
1914 }
1915 }
1916
1917 // RFC 4566
1918 // z=* (time zone adjustments)
1919 GetLineWithType(message, pos, &line, kLineTypeTimeZone);
1920
1921 // RFC 4566
1922 // k=* (encryption key)
1923 GetLineWithType(message, pos, &line, kLineTypeEncryptionKey);
1924
1925 // RFC 4566
1926 // a=* (zero or more session attribute lines)
1927 while (GetLineWithType(message, pos, &line, kLineTypeAttributes)) {
1928 if (HasAttribute(line, kAttributeGroup)) {
1929 if (!ParseGroupAttribute(line, desc, error)) {
1930 return false;
1931 }
1932 } else if (HasAttribute(line, kAttributeIceUfrag)) {
1933 if (!GetValue(line, kAttributeIceUfrag,
1934 &(session_td->ice_ufrag), error)) {
1935 return false;
1936 }
1937 } else if (HasAttribute(line, kAttributeIcePwd)) {
1938 if (!GetValue(line, kAttributeIcePwd, &(session_td->ice_pwd), error)) {
1939 return false;
1940 }
1941 } else if (HasAttribute(line, kAttributeIceLite)) {
1942 session_td->ice_mode = cricket::ICEMODE_LITE;
1943 } else if (HasAttribute(line, kAttributeIceOption)) {
1944 if (!ParseIceOptions(line, &(session_td->transport_options), error)) {
1945 return false;
1946 }
1947 } else if (HasAttribute(line, kAttributeFingerprint)) {
1948 if (session_td->identity_fingerprint.get()) {
1949 return ParseFailed(
1950 line,
1951 "Can't have multiple fingerprint attributes at the same level.",
1952 error);
1953 }
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001954 rtc::SSLFingerprint* fingerprint = NULL;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001955 if (!ParseFingerprintAttribute(line, &fingerprint, error)) {
1956 return false;
1957 }
1958 session_td->identity_fingerprint.reset(fingerprint);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001959 } else if (HasAttribute(line, kAttributeSetup)) {
1960 if (!ParseDtlsSetup(line, &(session_td->connection_role), error)) {
1961 return false;
1962 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001963 } else if (HasAttribute(line, kAttributeMsidSemantics)) {
1964 std::string semantics;
1965 if (!GetValue(line, kAttributeMsidSemantics, &semantics, error)) {
1966 return false;
1967 }
1968 *supports_msid = CaseInsensitiveFind(semantics, kMediaStreamSemantic);
1969 } else if (HasAttribute(line, kAttributeExtmap)) {
1970 RtpHeaderExtension extmap;
1971 if (!ParseExtmap(line, &extmap, error)) {
1972 return false;
1973 }
1974 session_extmaps->push_back(extmap);
1975 }
1976 }
1977
1978 return true;
1979}
1980
1981bool ParseGroupAttribute(const std::string& line,
1982 cricket::SessionDescription* desc,
1983 SdpParseError* error) {
1984 ASSERT(desc != NULL);
1985
1986 // RFC 5888 and draft-holmberg-mmusic-sdp-bundle-negotiation-00
1987 // a=group:BUNDLE video voice
1988 std::vector<std::string> fields;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001989 rtc::split(line.substr(kLinePrefixLength),
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001990 kSdpDelimiterSpace, &fields);
1991 std::string semantics;
1992 if (!GetValue(fields[0], kAttributeGroup, &semantics, error)) {
1993 return false;
1994 }
1995 cricket::ContentGroup group(semantics);
1996 for (size_t i = 1; i < fields.size(); ++i) {
1997 group.AddContentName(fields[i]);
1998 }
1999 desc->AddGroup(group);
2000 return true;
2001}
2002
2003static bool ParseFingerprintAttribute(const std::string& line,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002004 rtc::SSLFingerprint** fingerprint,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002005 SdpParseError* error) {
2006 if (!IsLineType(line, kLineTypeAttributes) ||
2007 !HasAttribute(line, kAttributeFingerprint)) {
2008 return ParseFailedExpectLine(line, 0, kLineTypeAttributes,
2009 kAttributeFingerprint, error);
2010 }
2011
2012 std::vector<std::string> fields;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002013 rtc::split(line.substr(kLinePrefixLength),
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002014 kSdpDelimiterSpace, &fields);
2015 const size_t expected_fields = 2;
2016 if (fields.size() != expected_fields) {
2017 return ParseFailedExpectFieldNum(line, expected_fields, error);
2018 }
2019
2020 // The first field here is "fingerprint:<hash>.
2021 std::string algorithm;
2022 if (!GetValue(fields[0], kAttributeFingerprint, &algorithm, error)) {
2023 return false;
2024 }
2025
2026 // Downcase the algorithm. Note that we don't need to downcase the
2027 // fingerprint because hex_decode can handle upper-case.
2028 std::transform(algorithm.begin(), algorithm.end(), algorithm.begin(),
2029 ::tolower);
2030
2031 // The second field is the digest value. De-hexify it.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002032 *fingerprint = rtc::SSLFingerprint::CreateFromRfc4572(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002033 algorithm, fields[1]);
2034 if (!*fingerprint) {
2035 return ParseFailed(line,
2036 "Failed to create fingerprint from the digest.",
2037 error);
2038 }
2039
2040 return true;
2041}
2042
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00002043static bool ParseDtlsSetup(const std::string& line,
2044 cricket::ConnectionRole* role,
2045 SdpParseError* error) {
2046 // setup-attr = "a=setup:" role
2047 // role = "active" / "passive" / "actpass" / "holdconn"
2048 std::vector<std::string> fields;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002049 rtc::split(line.substr(kLinePrefixLength), kSdpDelimiterColon, &fields);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00002050 const size_t expected_fields = 2;
2051 if (fields.size() != expected_fields) {
2052 return ParseFailedExpectFieldNum(line, expected_fields, error);
2053 }
2054 std::string role_str = fields[1];
2055 if (!cricket::StringToConnectionRole(role_str, role)) {
2056 return ParseFailed(line, "Invalid attribute value.", error);
2057 }
2058 return true;
2059}
2060
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002061// RFC 3551
2062// PT encoding media type clock rate channels
2063// name (Hz)
2064// 0 PCMU A 8,000 1
2065// 1 reserved A
2066// 2 reserved A
2067// 3 GSM A 8,000 1
2068// 4 G723 A 8,000 1
2069// 5 DVI4 A 8,000 1
2070// 6 DVI4 A 16,000 1
2071// 7 LPC A 8,000 1
2072// 8 PCMA A 8,000 1
2073// 9 G722 A 8,000 1
2074// 10 L16 A 44,100 2
2075// 11 L16 A 44,100 1
2076// 12 QCELP A 8,000 1
2077// 13 CN A 8,000 1
2078// 14 MPA A 90,000 (see text)
2079// 15 G728 A 8,000 1
2080// 16 DVI4 A 11,025 1
2081// 17 DVI4 A 22,050 1
2082// 18 G729 A 8,000 1
2083struct StaticPayloadAudioCodec {
2084 const char* name;
2085 int clockrate;
2086 int channels;
2087};
2088static const StaticPayloadAudioCodec kStaticPayloadAudioCodecs[] = {
2089 { "PCMU", 8000, 1 },
2090 { "reserved", 0, 0 },
2091 { "reserved", 0, 0 },
2092 { "GSM", 8000, 1 },
2093 { "G723", 8000, 1 },
2094 { "DVI4", 8000, 1 },
2095 { "DVI4", 16000, 1 },
2096 { "LPC", 8000, 1 },
2097 { "PCMA", 8000, 1 },
2098 { "G722", 8000, 1 },
2099 { "L16", 44100, 2 },
2100 { "L16", 44100, 1 },
2101 { "QCELP", 8000, 1 },
2102 { "CN", 8000, 1 },
2103 { "MPA", 90000, 1 },
2104 { "G728", 8000, 1 },
2105 { "DVI4", 11025, 1 },
2106 { "DVI4", 22050, 1 },
2107 { "G729", 8000, 1 },
2108};
2109
2110void MaybeCreateStaticPayloadAudioCodecs(
2111 const std::vector<int>& fmts, AudioContentDescription* media_desc) {
2112 if (!media_desc) {
2113 return;
2114 }
henrike@webrtc.org28654cb2013-07-22 21:07:49 +00002115 int preference = static_cast<int>(fmts.size());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002116 std::vector<int>::const_iterator it = fmts.begin();
2117 bool add_new_codec = false;
2118 for (; it != fmts.end(); ++it) {
2119 int payload_type = *it;
2120 if (!media_desc->HasCodec(payload_type) &&
2121 payload_type >= 0 &&
2122 payload_type < ARRAY_SIZE(kStaticPayloadAudioCodecs)) {
2123 std::string encoding_name = kStaticPayloadAudioCodecs[payload_type].name;
2124 int clock_rate = kStaticPayloadAudioCodecs[payload_type].clockrate;
2125 int channels = kStaticPayloadAudioCodecs[payload_type].channels;
2126 media_desc->AddCodec(cricket::AudioCodec(payload_type, encoding_name,
2127 clock_rate, 0, channels,
2128 preference));
2129 add_new_codec = true;
2130 }
2131 --preference;
2132 }
2133 if (add_new_codec) {
2134 media_desc->SortCodecs();
2135 }
2136}
2137
2138template <class C>
2139static C* ParseContentDescription(const std::string& message,
2140 const MediaType media_type,
2141 int mline_index,
2142 const std::string& protocol,
2143 const std::vector<int>& codec_preference,
2144 size_t* pos,
2145 std::string* content_name,
2146 TransportDescription* transport,
2147 std::vector<JsepIceCandidate*>* candidates,
2148 webrtc::SdpParseError* error) {
2149 C* media_desc = new C();
2150 switch (media_type) {
2151 case cricket::MEDIA_TYPE_AUDIO:
2152 *content_name = cricket::CN_AUDIO;
2153 break;
2154 case cricket::MEDIA_TYPE_VIDEO:
2155 *content_name = cricket::CN_VIDEO;
2156 break;
2157 case cricket::MEDIA_TYPE_DATA:
2158 *content_name = cricket::CN_DATA;
2159 break;
2160 default:
2161 ASSERT(false);
2162 break;
2163 }
2164 if (!ParseContent(message, media_type, mline_index, protocol,
2165 codec_preference, pos, content_name,
2166 media_desc, transport, candidates, error)) {
2167 delete media_desc;
2168 return NULL;
2169 }
2170 // Sort the codecs according to the m-line fmt list.
2171 media_desc->SortCodecs();
2172 return media_desc;
2173}
2174
2175bool ParseMediaDescription(const std::string& message,
2176 const TransportDescription& session_td,
2177 const RtpHeaderExtensions& session_extmaps,
2178 bool supports_msid,
2179 size_t* pos,
2180 cricket::SessionDescription* desc,
2181 std::vector<JsepIceCandidate*>* candidates,
2182 SdpParseError* error) {
2183 ASSERT(desc != NULL);
2184 std::string line;
2185 int mline_index = -1;
2186
2187 // Zero or more media descriptions
2188 // RFC 4566
2189 // m=<media> <port> <proto> <fmt>
2190 while (GetLineWithType(message, pos, &line, kLineTypeMedia)) {
2191 ++mline_index;
2192
2193 std::vector<std::string> fields;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002194 rtc::split(line.substr(kLinePrefixLength),
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002195 kSdpDelimiterSpace, &fields);
2196 const size_t expected_min_fields = 4;
2197 if (fields.size() < expected_min_fields) {
2198 return ParseFailedExpectMinFieldNum(line, expected_min_fields, error);
2199 }
2200 bool rejected = false;
2201 // RFC 3264
2202 // To reject an offered stream, the port number in the corresponding stream
2203 // in the answer MUST be set to zero.
2204 if (fields[1] == kMediaPortRejected) {
2205 rejected = true;
2206 }
2207
2208 std::string protocol = fields[2];
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002209
2210 // <fmt>
2211 std::vector<int> codec_preference;
pthatcher@webrtc.org3341b402015-02-13 21:14:22 +00002212 if (IsRtp(protocol)) {
jiayl@webrtc.orgddb85ab2014-09-05 16:31:56 +00002213 for (size_t j = 3 ; j < fields.size(); ++j) {
2214 // TODO(wu): Remove when below bug is fixed.
2215 // https://bugzilla.mozilla.org/show_bug.cgi?id=996329
2216 if (fields[j] == "" && j == fields.size() - 1) {
2217 continue;
2218 }
wu@webrtc.org36eda7c2014-04-15 20:37:30 +00002219
jiayl@webrtc.orgddb85ab2014-09-05 16:31:56 +00002220 int pl = 0;
pkasting@chromium.orge9facf82015-02-17 20:36:28 +00002221 if (!GetPayloadTypeFromString(line, fields[j], &pl, error)) {
jiayl@webrtc.orgddb85ab2014-09-05 16:31:56 +00002222 return false;
2223 }
2224 codec_preference.push_back(pl);
wu@webrtc.org5e760e72014-04-02 23:19:09 +00002225 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002226 }
2227
2228 // Make a temporary TransportDescription based on |session_td|.
2229 // Some of this gets overwritten by ParseContent.
2230 TransportDescription transport(NS_JINGLE_ICE_UDP,
2231 session_td.transport_options,
2232 session_td.ice_ufrag,
2233 session_td.ice_pwd,
2234 session_td.ice_mode,
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00002235 session_td.connection_role,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002236 session_td.identity_fingerprint.get(),
2237 Candidates());
2238
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002239 rtc::scoped_ptr<MediaContentDescription> content;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002240 std::string content_name;
2241 if (HasAttribute(line, kMediaTypeVideo)) {
2242 content.reset(ParseContentDescription<VideoContentDescription>(
2243 message, cricket::MEDIA_TYPE_VIDEO, mline_index, protocol,
2244 codec_preference, pos, &content_name,
2245 &transport, candidates, error));
2246 } else if (HasAttribute(line, kMediaTypeAudio)) {
2247 content.reset(ParseContentDescription<AudioContentDescription>(
2248 message, cricket::MEDIA_TYPE_AUDIO, mline_index, protocol,
2249 codec_preference, pos, &content_name,
2250 &transport, candidates, error));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002251 } else if (HasAttribute(line, kMediaTypeData)) {
jiayl@webrtc.org0e527722014-09-08 21:43:43 +00002252 DataContentDescription* data_desc =
wu@webrtc.org78187522013-10-07 23:32:02 +00002253 ParseContentDescription<DataContentDescription>(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002254 message, cricket::MEDIA_TYPE_DATA, mline_index, protocol,
2255 codec_preference, pos, &content_name,
wu@webrtc.org78187522013-10-07 23:32:02 +00002256 &transport, candidates, error);
jiayl@webrtc.org0e527722014-09-08 21:43:43 +00002257 content.reset(data_desc);
wu@webrtc.org78187522013-10-07 23:32:02 +00002258
jiayl@webrtc.orgddb85ab2014-09-05 16:31:56 +00002259 int p;
pthatcher@webrtc.org3341b402015-02-13 21:14:22 +00002260 if (data_desc && IsDtlsSctp(protocol) && rtc::FromString(fields[3], &p)) {
jiayl@webrtc.org0e527722014-09-08 21:43:43 +00002261 if (!AddSctpDataCodec(data_desc, p))
jiayl@webrtc.orgddb85ab2014-09-05 16:31:56 +00002262 return false;
wu@webrtc.org78187522013-10-07 23:32:02 +00002263 }
2264
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00002265 // We should always use the default bandwidth for RTP-based data
2266 // channels. Don't allow SDP to set the bandwidth, because that
2267 // would give JS the opportunity to "break the Internet".
sergeyu@chromium.orga59696b2013-09-13 23:48:58 +00002268 // TODO(pthatcher): But we need to temporarily allow the SDP to control
2269 // this for backwards-compatibility. Once we don't need that any
2270 // more, remove this.
2271 bool support_dc_sdp_bandwidth_temporarily = true;
wu@webrtc.org967bfff2013-09-19 05:49:50 +00002272 if (content.get() && !support_dc_sdp_bandwidth_temporarily) {
sergeyu@chromium.orga59696b2013-09-13 23:48:58 +00002273 content->set_bandwidth(cricket::kAutoBandwidth);
2274 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002275 } else {
2276 LOG(LS_WARNING) << "Unsupported media type: " << line;
2277 continue;
2278 }
2279 if (!content.get()) {
2280 // ParseContentDescription returns NULL if failed.
2281 return false;
2282 }
2283
pthatcher@webrtc.org3341b402015-02-13 21:14:22 +00002284 if (IsRtp(protocol)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002285 // Make sure to set the media direction correctly. If the direction is not
2286 // MD_RECVONLY or Inactive and no streams are parsed,
2287 // a default MediaStream will be created to prepare for receiving media.
2288 if (supports_msid && content->streams().empty() &&
2289 content->direction() == cricket::MD_SENDRECV) {
2290 content->set_direction(cricket::MD_RECVONLY);
2291 }
2292
2293 // Set the extmap.
2294 if (!session_extmaps.empty() &&
2295 !content->rtp_header_extensions().empty()) {
2296 return ParseFailed("",
2297 "The a=extmap MUST be either all session level or "
2298 "all media level.",
2299 error);
2300 }
2301 for (size_t i = 0; i < session_extmaps.size(); ++i) {
2302 content->AddRtpHeaderExtension(session_extmaps[i]);
2303 }
2304 }
2305 content->set_protocol(protocol);
2306 desc->AddContent(content_name,
pthatcher@webrtc.org3341b402015-02-13 21:14:22 +00002307 IsDtlsSctp(protocol) ? cricket::NS_JINGLE_DRAFT_SCTP :
2308 cricket::NS_JINGLE_RTP,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002309 rejected,
2310 content.release());
2311 // Create TransportInfo with the media level "ice-pwd" and "ice-ufrag".
2312 TransportInfo transport_info(content_name, transport);
2313
2314 if (!desc->AddTransportInfo(transport_info)) {
2315 std::ostringstream description;
2316 description << "Failed to AddTransportInfo with content name: "
2317 << content_name;
2318 return ParseFailed("", description.str(), error);
2319 }
2320 }
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00002321
2322 size_t end_of_message = message.size();
2323 if (mline_index == -1 && *pos != end_of_message) {
2324 ParseFailed(message, *pos, "Expects m line.", error);
2325 return false;
2326 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002327 return true;
2328}
2329
2330bool VerifyCodec(const cricket::Codec& codec) {
2331 // Codec has not been populated correctly unless the name has been set. This
2332 // can happen if an SDP has an fmtp or rtcp-fb with a payload type but doesn't
2333 // have a corresponding "rtpmap" line.
2334 cricket::Codec default_codec;
2335 return default_codec.name != codec.name;
2336}
2337
2338bool VerifyAudioCodecs(const AudioContentDescription* audio_desc) {
2339 const std::vector<cricket::AudioCodec>& codecs = audio_desc->codecs();
2340 for (std::vector<cricket::AudioCodec>::const_iterator iter = codecs.begin();
2341 iter != codecs.end(); ++iter) {
2342 if (!VerifyCodec(*iter)) {
2343 return false;
2344 }
2345 }
2346 return true;
2347}
2348
2349bool VerifyVideoCodecs(const VideoContentDescription* video_desc) {
2350 const std::vector<cricket::VideoCodec>& codecs = video_desc->codecs();
2351 for (std::vector<cricket::VideoCodec>::const_iterator iter = codecs.begin();
2352 iter != codecs.end(); ++iter) {
2353 if (!VerifyCodec(*iter)) {
2354 return false;
2355 }
2356 }
2357 return true;
2358}
2359
2360void AddParameters(const cricket::CodecParameterMap& parameters,
2361 cricket::Codec* codec) {
2362 for (cricket::CodecParameterMap::const_iterator iter =
2363 parameters.begin(); iter != parameters.end(); ++iter) {
2364 codec->SetParam(iter->first, iter->second);
2365 }
2366}
2367
2368void AddFeedbackParameter(const cricket::FeedbackParam& feedback_param,
2369 cricket::Codec* codec) {
2370 codec->AddFeedbackParam(feedback_param);
2371}
2372
2373void AddFeedbackParameters(const cricket::FeedbackParams& feedback_params,
2374 cricket::Codec* codec) {
2375 for (std::vector<cricket::FeedbackParam>::const_iterator iter =
2376 feedback_params.params().begin();
2377 iter != feedback_params.params().end(); ++iter) {
2378 codec->AddFeedbackParam(*iter);
2379 }
2380}
2381
2382// Gets the current codec setting associated with |payload_type|. If there
changbin.shao@webrtc.org2d25b442015-03-16 04:14:34 +00002383// is no Codec associated with that payload type it returns an empty codec
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002384// with that payload type.
2385template <class T>
changbin.shao@webrtc.org2d25b442015-03-16 04:14:34 +00002386T GetCodecWithPayloadType(const std::vector<T>& codecs, int payload_type) {
2387 T ret_val;
2388 if (!FindCodecById(codecs, payload_type, &ret_val)) {
2389 ret_val.id = payload_type;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002390 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002391 return ret_val;
2392}
2393
2394// Updates or creates a new codec entry in the audio description.
2395template <class T, class U>
2396void AddOrReplaceCodec(MediaContentDescription* content_desc, const U& codec) {
2397 T* desc = static_cast<T*>(content_desc);
2398 std::vector<U> codecs = desc->codecs();
2399 bool found = false;
2400
2401 typename std::vector<U>::iterator iter;
2402 for (iter = codecs.begin(); iter != codecs.end(); ++iter) {
2403 if (iter->id == codec.id) {
2404 *iter = codec;
2405 found = true;
2406 break;
2407 }
2408 }
2409 if (!found) {
2410 desc->AddCodec(codec);
2411 return;
2412 }
2413 desc->set_codecs(codecs);
2414}
2415
2416// Adds or updates existing codec corresponding to |payload_type| according
2417// to |parameters|.
2418template <class T, class U>
2419void UpdateCodec(MediaContentDescription* content_desc, int payload_type,
2420 const cricket::CodecParameterMap& parameters) {
2421 // Codec might already have been populated (from rtpmap).
changbin.shao@webrtc.org2d25b442015-03-16 04:14:34 +00002422 U new_codec = GetCodecWithPayloadType(static_cast<T*>(content_desc)->codecs(),
2423 payload_type);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002424 AddParameters(parameters, &new_codec);
2425 AddOrReplaceCodec<T, U>(content_desc, new_codec);
2426}
2427
2428// Adds or updates existing codec corresponding to |payload_type| according
2429// to |feedback_param|.
2430template <class T, class U>
2431void UpdateCodec(MediaContentDescription* content_desc, int payload_type,
2432 const cricket::FeedbackParam& feedback_param) {
2433 // Codec might already have been populated (from rtpmap).
changbin.shao@webrtc.org2d25b442015-03-16 04:14:34 +00002434 U new_codec = GetCodecWithPayloadType(static_cast<T*>(content_desc)->codecs(),
2435 payload_type);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002436 AddFeedbackParameter(feedback_param, &new_codec);
2437 AddOrReplaceCodec<T, U>(content_desc, new_codec);
2438}
2439
jlmiller@webrtc.orga744a282015-02-18 21:37:46 +00002440template <class T>
2441bool PopWildcardCodec(std::vector<T>* codecs, T* wildcard_codec) {
2442 for (auto iter = codecs->begin(); iter != codecs->end(); ++iter) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002443 if (iter->id == kWildcardPayloadType) {
2444 *wildcard_codec = *iter;
2445 codecs->erase(iter);
2446 return true;
2447 }
2448 }
2449 return false;
2450}
2451
jlmiller@webrtc.orga744a282015-02-18 21:37:46 +00002452template<class T>
2453void UpdateFromWildcardCodecs(cricket::MediaContentDescriptionImpl<T>* desc) {
2454 auto codecs = desc->codecs();
2455 T wildcard_codec;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002456 if (!PopWildcardCodec(&codecs, &wildcard_codec)) {
2457 return;
2458 }
jlmiller@webrtc.orga744a282015-02-18 21:37:46 +00002459 for (auto& codec : codecs) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002460 AddFeedbackParameters(wildcard_codec.feedback_params, &codec);
2461 }
jlmiller@webrtc.orga744a282015-02-18 21:37:46 +00002462 desc->set_codecs(codecs);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002463}
2464
2465void AddAudioAttribute(const std::string& name, const std::string& value,
2466 AudioContentDescription* audio_desc) {
2467 if (value.empty()) {
2468 return;
2469 }
2470 std::vector<cricket::AudioCodec> codecs = audio_desc->codecs();
2471 for (std::vector<cricket::AudioCodec>::iterator iter = codecs.begin();
2472 iter != codecs.end(); ++iter) {
2473 iter->params[name] = value;
2474 }
2475 audio_desc->set_codecs(codecs);
2476}
2477
2478bool ParseContent(const std::string& message,
2479 const MediaType media_type,
2480 int mline_index,
2481 const std::string& protocol,
2482 const std::vector<int>& codec_preference,
2483 size_t* pos,
2484 std::string* content_name,
2485 MediaContentDescription* media_desc,
2486 TransportDescription* transport,
2487 std::vector<JsepIceCandidate*>* candidates,
2488 SdpParseError* error) {
2489 ASSERT(media_desc != NULL);
2490 ASSERT(content_name != NULL);
2491 ASSERT(transport != NULL);
2492
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +00002493 if (media_type == cricket::MEDIA_TYPE_AUDIO) {
2494 MaybeCreateStaticPayloadAudioCodecs(
2495 codec_preference, static_cast<AudioContentDescription*>(media_desc));
2496 }
2497
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002498 // The media level "ice-ufrag" and "ice-pwd".
2499 // The candidates before update the media level "ice-pwd" and "ice-ufrag".
2500 Candidates candidates_orig;
2501 std::string line;
2502 std::string mline_id;
2503 // Tracks created out of the ssrc attributes.
2504 StreamParamsVec tracks;
2505 SsrcInfoVec ssrc_infos;
2506 SsrcGroupVec ssrc_groups;
2507 std::string maxptime_as_string;
2508 std::string ptime_as_string;
2509
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002510 // Loop until the next m line
2511 while (!IsLineType(message, kLineTypeMedia, *pos)) {
2512 if (!GetLine(message, pos, &line)) {
2513 if (*pos >= message.size()) {
2514 break; // Done parsing
2515 } else {
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +00002516 return ParseFailed(message, *pos, "Invalid SDP line.", error);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002517 }
2518 }
2519
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002520 // RFC 4566
2521 // b=* (zero or more bandwidth information lines)
2522 if (IsLineType(line, kLineTypeSessionBandwidth)) {
2523 std::string bandwidth;
2524 if (HasAttribute(line, kApplicationSpecificMaximum)) {
2525 if (!GetValue(line, kApplicationSpecificMaximum, &bandwidth, error)) {
2526 return false;
2527 } else {
wu@webrtc.org5e760e72014-04-02 23:19:09 +00002528 int b = 0;
2529 if (!GetValueFromString(line, bandwidth, &b, error)) {
2530 return false;
2531 }
2532 media_desc->set_bandwidth(b * 1000);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002533 }
2534 }
2535 continue;
2536 }
2537
2538 if (!IsLineType(line, kLineTypeAttributes)) {
2539 // TODO: Handle other lines if needed.
2540 LOG(LS_INFO) << "Ignored line: " << line;
2541 continue;
2542 }
2543
2544 // Handle attributes common to SCTP and RTP.
2545 if (HasAttribute(line, kAttributeMid)) {
2546 // RFC 3388
2547 // mid-attribute = "a=mid:" identification-tag
2548 // identification-tag = token
2549 // Use the mid identification-tag as the content name.
2550 if (!GetValue(line, kAttributeMid, &mline_id, error)) {
2551 return false;
2552 }
2553 *content_name = mline_id;
2554 } else if (HasAttribute(line, kAttributeCandidate)) {
2555 Candidate candidate;
2556 if (!ParseCandidate(line, &candidate, error, false)) {
2557 return false;
2558 }
2559 candidates_orig.push_back(candidate);
2560 } else if (HasAttribute(line, kAttributeIceUfrag)) {
2561 if (!GetValue(line, kAttributeIceUfrag, &transport->ice_ufrag, error)) {
2562 return false;
2563 }
2564 } else if (HasAttribute(line, kAttributeIcePwd)) {
2565 if (!GetValue(line, kAttributeIcePwd, &transport->ice_pwd, error)) {
2566 return false;
2567 }
2568 } else if (HasAttribute(line, kAttributeIceOption)) {
2569 if (!ParseIceOptions(line, &transport->transport_options, error)) {
2570 return false;
2571 }
2572 } else if (HasAttribute(line, kAttributeFmtp)) {
2573 if (!ParseFmtpAttributes(line, media_type, media_desc, error)) {
2574 return false;
2575 }
2576 } else if (HasAttribute(line, kAttributeFingerprint)) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002577 rtc::SSLFingerprint* fingerprint = NULL;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002578
2579 if (!ParseFingerprintAttribute(line, &fingerprint, error)) {
2580 return false;
2581 }
2582 transport->identity_fingerprint.reset(fingerprint);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00002583 } else if (HasAttribute(line, kAttributeSetup)) {
2584 if (!ParseDtlsSetup(line, &(transport->connection_role), error)) {
2585 return false;
2586 }
pthatcher@webrtc.org3341b402015-02-13 21:14:22 +00002587 } else if (IsDtlsSctp(protocol) && HasAttribute(line, kAttributeSctpPort)) {
jiayl@webrtc.orgddb85ab2014-09-05 16:31:56 +00002588 int sctp_port;
2589 if (!ParseSctpPort(line, &sctp_port, error)) {
2590 return false;
2591 }
2592 if (!AddSctpDataCodec(static_cast<DataContentDescription*>(media_desc),
2593 sctp_port)) {
2594 return false;
2595 }
pthatcher@webrtc.org3341b402015-02-13 21:14:22 +00002596 } else if (IsRtp(protocol)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002597 //
2598 // RTP specific attrubtes
2599 //
2600 if (HasAttribute(line, kAttributeRtcpMux)) {
2601 media_desc->set_rtcp_mux(true);
2602 } else if (HasAttribute(line, kAttributeSsrcGroup)) {
2603 if (!ParseSsrcGroupAttribute(line, &ssrc_groups, error)) {
2604 return false;
2605 }
2606 } else if (HasAttribute(line, kAttributeSsrc)) {
2607 if (!ParseSsrcAttribute(line, &ssrc_infos, error)) {
2608 return false;
2609 }
2610 } else if (HasAttribute(line, kAttributeCrypto)) {
2611 if (!ParseCryptoAttribute(line, media_desc, error)) {
2612 return false;
2613 }
2614 } else if (HasAttribute(line, kAttributeRtpmap)) {
2615 if (!ParseRtpmapAttribute(line, media_type, codec_preference,
2616 media_desc, error)) {
2617 return false;
2618 }
2619 } else if (HasAttribute(line, kCodecParamMaxPTime)) {
2620 if (!GetValue(line, kCodecParamMaxPTime, &maxptime_as_string, error)) {
2621 return false;
2622 }
2623 } else if (HasAttribute(line, kAttributeRtcpFb)) {
2624 if (!ParseRtcpFbAttribute(line, media_type, media_desc, error)) {
2625 return false;
2626 }
2627 } else if (HasAttribute(line, kCodecParamPTime)) {
2628 if (!GetValue(line, kCodecParamPTime, &ptime_as_string, error)) {
2629 return false;
2630 }
2631 } else if (HasAttribute(line, kAttributeSendOnly)) {
2632 media_desc->set_direction(cricket::MD_SENDONLY);
2633 } else if (HasAttribute(line, kAttributeRecvOnly)) {
2634 media_desc->set_direction(cricket::MD_RECVONLY);
2635 } else if (HasAttribute(line, kAttributeInactive)) {
2636 media_desc->set_direction(cricket::MD_INACTIVE);
2637 } else if (HasAttribute(line, kAttributeSendRecv)) {
2638 media_desc->set_direction(cricket::MD_SENDRECV);
2639 } else if (HasAttribute(line, kAttributeExtmap)) {
2640 RtpHeaderExtension extmap;
2641 if (!ParseExtmap(line, &extmap, error)) {
2642 return false;
2643 }
2644 media_desc->AddRtpHeaderExtension(extmap);
2645 } else if (HasAttribute(line, kAttributeXGoogleFlag)) {
2646 // Experimental attribute. Conference mode activates more aggressive
2647 // AEC and NS settings.
2648 // TODO: expose API to set these directly.
2649 std::string flag_value;
2650 if (!GetValue(line, kAttributeXGoogleFlag, &flag_value, error)) {
2651 return false;
2652 }
2653 if (flag_value.compare(kValueConference) == 0)
2654 media_desc->set_conference_mode(true);
2655 } else if (HasAttribute(line, kAttributeXGoogleBufferLatency)) {
2656 // Experimental attribute.
2657 // TODO: expose API to set this directly.
2658 std::string flag_value;
2659 if (!GetValue(line, kAttributeXGoogleBufferLatency, &flag_value,
2660 error)) {
2661 return false;
2662 }
2663 int buffer_latency = 0;
wu@webrtc.org5e760e72014-04-02 23:19:09 +00002664 if (!GetValueFromString(line, flag_value, &buffer_latency, error)) {
2665 return false;
2666 }
2667 if (buffer_latency < 0) {
2668 return ParseFailed(line, "Buffer latency less than 0.", error);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002669 }
2670 media_desc->set_buffered_mode_latency(buffer_latency);
2671 }
2672 } else {
2673 // Only parse lines that we are interested of.
2674 LOG(LS_INFO) << "Ignored line: " << line;
2675 continue;
2676 }
2677 }
2678
2679 // Create tracks from the |ssrc_infos|.
2680 CreateTracksFromSsrcInfos(ssrc_infos, &tracks);
2681
2682 // Add the ssrc group to the track.
2683 for (SsrcGroupVec::iterator ssrc_group = ssrc_groups.begin();
2684 ssrc_group != ssrc_groups.end(); ++ssrc_group) {
2685 if (ssrc_group->ssrcs.empty()) {
2686 continue;
2687 }
2688 uint32 ssrc = ssrc_group->ssrcs.front();
2689 for (StreamParamsVec::iterator track = tracks.begin();
2690 track != tracks.end(); ++track) {
2691 if (track->has_ssrc(ssrc)) {
2692 track->ssrc_groups.push_back(*ssrc_group);
2693 }
2694 }
2695 }
2696
2697 // Add the new tracks to the |media_desc|.
2698 for (StreamParamsVec::iterator track = tracks.begin();
2699 track != tracks.end(); ++track) {
2700 media_desc->AddStream(*track);
2701 }
2702
2703 if (media_type == cricket::MEDIA_TYPE_AUDIO) {
2704 AudioContentDescription* audio_desc =
2705 static_cast<AudioContentDescription*>(media_desc);
jlmiller@webrtc.orga744a282015-02-18 21:37:46 +00002706 UpdateFromWildcardCodecs(audio_desc);
2707
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002708 // Verify audio codec ensures that no audio codec has been populated with
2709 // only fmtp.
2710 if (!VerifyAudioCodecs(audio_desc)) {
2711 return ParseFailed("Failed to parse audio codecs correctly.", error);
2712 }
2713 AddAudioAttribute(kCodecParamMaxPTime, maxptime_as_string, audio_desc);
2714 AddAudioAttribute(kCodecParamPTime, ptime_as_string, audio_desc);
2715 }
2716
2717 if (media_type == cricket::MEDIA_TYPE_VIDEO) {
jlmiller@webrtc.orga744a282015-02-18 21:37:46 +00002718 VideoContentDescription* video_desc =
2719 static_cast<VideoContentDescription*>(media_desc);
2720 UpdateFromWildcardCodecs(video_desc);
2721 // Verify video codec ensures that no video codec has been populated with
2722 // only rtcp-fb.
2723 if (!VerifyVideoCodecs(video_desc)) {
2724 return ParseFailed("Failed to parse video codecs correctly.", error);
2725 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002726 }
2727
2728 // RFC 5245
2729 // Update the candidates with the media level "ice-pwd" and "ice-ufrag".
2730 for (Candidates::iterator it = candidates_orig.begin();
2731 it != candidates_orig.end(); ++it) {
2732 ASSERT((*it).username().empty());
2733 (*it).set_username(transport->ice_ufrag);
2734 ASSERT((*it).password().empty());
2735 (*it).set_password(transport->ice_pwd);
2736 candidates->push_back(
2737 new JsepIceCandidate(mline_id, mline_index, *it));
2738 }
2739 return true;
2740}
2741
2742bool ParseSsrcAttribute(const std::string& line, SsrcInfoVec* ssrc_infos,
2743 SdpParseError* error) {
2744 ASSERT(ssrc_infos != NULL);
2745 // RFC 5576
2746 // a=ssrc:<ssrc-id> <attribute>
2747 // a=ssrc:<ssrc-id> <attribute>:<value>
2748 std::string field1, field2;
2749 if (!SplitByDelimiter(line.substr(kLinePrefixLength),
2750 kSdpDelimiterSpace,
2751 &field1,
2752 &field2)) {
2753 const size_t expected_fields = 2;
2754 return ParseFailedExpectFieldNum(line, expected_fields, error);
2755 }
2756
2757 // ssrc:<ssrc-id>
2758 std::string ssrc_id_s;
2759 if (!GetValue(field1, kAttributeSsrc, &ssrc_id_s, error)) {
2760 return false;
2761 }
wu@webrtc.org5e760e72014-04-02 23:19:09 +00002762 uint32 ssrc_id = 0;
2763 if (!GetValueFromString(line, ssrc_id_s, &ssrc_id, error)) {
2764 return false;
2765 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002766
2767 std::string attribute;
2768 std::string value;
2769 if (!SplitByDelimiter(field2, kSdpDelimiterColon,
2770 &attribute, &value)) {
2771 std::ostringstream description;
2772 description << "Failed to get the ssrc attribute value from " << field2
2773 << ". Expected format <attribute>:<value>.";
2774 return ParseFailed(line, description.str(), error);
2775 }
2776
2777 // Check if there's already an item for this |ssrc_id|. Create a new one if
2778 // there isn't.
2779 SsrcInfoVec::iterator ssrc_info = ssrc_infos->begin();
2780 for (; ssrc_info != ssrc_infos->end(); ++ssrc_info) {
2781 if (ssrc_info->ssrc_id == ssrc_id) {
2782 break;
2783 }
2784 }
2785 if (ssrc_info == ssrc_infos->end()) {
2786 SsrcInfo info;
2787 info.ssrc_id = ssrc_id;
2788 ssrc_infos->push_back(info);
2789 ssrc_info = ssrc_infos->end() - 1;
2790 }
2791
2792 // Store the info to the |ssrc_info|.
2793 if (attribute == kSsrcAttributeCname) {
2794 // RFC 5576
2795 // cname:<value>
2796 ssrc_info->cname = value;
2797 } else if (attribute == kSsrcAttributeMsid) {
2798 // draft-alvestrand-mmusic-msid-00
2799 // "msid:" identifier [ " " appdata ]
2800 std::vector<std::string> fields;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002801 rtc::split(value, kSdpDelimiterSpace, &fields);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002802 if (fields.size() < 1 || fields.size() > 2) {
2803 return ParseFailed(line,
2804 "Expected format \"msid:<identifier>[ <appdata>]\".",
2805 error);
2806 }
2807 ssrc_info->msid_identifier = fields[0];
2808 if (fields.size() == 2) {
2809 ssrc_info->msid_appdata = fields[1];
2810 }
2811 } else if (attribute == kSsrcAttributeMslabel) {
2812 // draft-alvestrand-rtcweb-mid-01
2813 // mslabel:<value>
2814 ssrc_info->mslabel = value;
2815 } else if (attribute == kSSrcAttributeLabel) {
2816 // The label isn't defined.
2817 // label:<value>
2818 ssrc_info->label = value;
2819 }
2820 return true;
2821}
2822
2823bool ParseSsrcGroupAttribute(const std::string& line,
2824 SsrcGroupVec* ssrc_groups,
2825 SdpParseError* error) {
2826 ASSERT(ssrc_groups != NULL);
2827 // RFC 5576
2828 // a=ssrc-group:<semantics> <ssrc-id> ...
2829 std::vector<std::string> fields;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002830 rtc::split(line.substr(kLinePrefixLength),
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002831 kSdpDelimiterSpace, &fields);
2832 const size_t expected_min_fields = 2;
2833 if (fields.size() < expected_min_fields) {
2834 return ParseFailedExpectMinFieldNum(line, expected_min_fields, error);
2835 }
2836 std::string semantics;
2837 if (!GetValue(fields[0], kAttributeSsrcGroup, &semantics, error)) {
2838 return false;
2839 }
2840 std::vector<uint32> ssrcs;
2841 for (size_t i = 1; i < fields.size(); ++i) {
wu@webrtc.org5e760e72014-04-02 23:19:09 +00002842 uint32 ssrc = 0;
2843 if (!GetValueFromString(line, fields[i], &ssrc, error)) {
2844 return false;
2845 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002846 ssrcs.push_back(ssrc);
2847 }
2848 ssrc_groups->push_back(SsrcGroup(semantics, ssrcs));
2849 return true;
2850}
2851
2852bool ParseCryptoAttribute(const std::string& line,
2853 MediaContentDescription* media_desc,
2854 SdpParseError* error) {
2855 std::vector<std::string> fields;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002856 rtc::split(line.substr(kLinePrefixLength),
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002857 kSdpDelimiterSpace, &fields);
2858 // RFC 4568
2859 // a=crypto:<tag> <crypto-suite> <key-params> [<session-params>]
2860 const size_t expected_min_fields = 3;
2861 if (fields.size() < expected_min_fields) {
2862 return ParseFailedExpectMinFieldNum(line, expected_min_fields, error);
2863 }
2864 std::string tag_value;
2865 if (!GetValue(fields[0], kAttributeCrypto, &tag_value, error)) {
2866 return false;
2867 }
wu@webrtc.org5e760e72014-04-02 23:19:09 +00002868 int tag = 0;
2869 if (!GetValueFromString(line, tag_value, &tag, error)) {
2870 return false;
2871 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002872 const std::string crypto_suite = fields[1];
2873 const std::string key_params = fields[2];
2874 std::string session_params;
2875 if (fields.size() > 3) {
2876 session_params = fields[3];
2877 }
2878 media_desc->AddCrypto(CryptoParams(tag, crypto_suite, key_params,
2879 session_params));
2880 return true;
2881}
2882
2883// Updates or creates a new codec entry in the audio description with according
2884// to |name|, |clockrate|, |bitrate|, |channels| and |preference|.
2885void UpdateCodec(int payload_type, const std::string& name, int clockrate,
2886 int bitrate, int channels, int preference,
2887 AudioContentDescription* audio_desc) {
2888 // Codec may already be populated with (only) optional parameters
2889 // (from an fmtp).
changbin.shao@webrtc.org2d25b442015-03-16 04:14:34 +00002890 cricket::AudioCodec codec =
2891 GetCodecWithPayloadType(audio_desc->codecs(), payload_type);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002892 codec.name = name;
2893 codec.clockrate = clockrate;
2894 codec.bitrate = bitrate;
2895 codec.channels = channels;
2896 codec.preference = preference;
2897 AddOrReplaceCodec<AudioContentDescription, cricket::AudioCodec>(audio_desc,
2898 codec);
2899}
2900
2901// Updates or creates a new codec entry in the video description according to
2902// |name|, |width|, |height|, |framerate| and |preference|.
2903void UpdateCodec(int payload_type, const std::string& name, int width,
2904 int height, int framerate, int preference,
2905 VideoContentDescription* video_desc) {
2906 // Codec may already be populated with (only) optional parameters
2907 // (from an fmtp).
changbin.shao@webrtc.org2d25b442015-03-16 04:14:34 +00002908 cricket::VideoCodec codec =
2909 GetCodecWithPayloadType(video_desc->codecs(), payload_type);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002910 codec.name = name;
2911 codec.width = width;
2912 codec.height = height;
2913 codec.framerate = framerate;
2914 codec.preference = preference;
2915 AddOrReplaceCodec<VideoContentDescription, cricket::VideoCodec>(video_desc,
2916 codec);
2917}
2918
2919bool ParseRtpmapAttribute(const std::string& line,
2920 const MediaType media_type,
2921 const std::vector<int>& codec_preference,
2922 MediaContentDescription* media_desc,
2923 SdpParseError* error) {
2924 std::vector<std::string> fields;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002925 rtc::split(line.substr(kLinePrefixLength),
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002926 kSdpDelimiterSpace, &fields);
2927 // RFC 4566
2928 // a=rtpmap:<payload type> <encoding name>/<clock rate>[/<encodingparameters>]
2929 const size_t expected_min_fields = 2;
2930 if (fields.size() < expected_min_fields) {
2931 return ParseFailedExpectMinFieldNum(line, expected_min_fields, error);
2932 }
2933 std::string payload_type_value;
2934 if (!GetValue(fields[0], kAttributeRtpmap, &payload_type_value, error)) {
2935 return false;
2936 }
wu@webrtc.org5e760e72014-04-02 23:19:09 +00002937 int payload_type = 0;
pkasting@chromium.orge9facf82015-02-17 20:36:28 +00002938 if (!GetPayloadTypeFromString(line, payload_type_value, &payload_type,
2939 error)) {
wu@webrtc.org5e760e72014-04-02 23:19:09 +00002940 return false;
2941 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002942
2943 // Set the preference order depending on the order of the pl type in the
2944 // <fmt> of the m-line.
2945 const int preference = codec_preference.end() -
2946 std::find(codec_preference.begin(), codec_preference.end(),
2947 payload_type);
2948 if (preference == 0) {
2949 LOG(LS_WARNING) << "Ignore rtpmap line that did not appear in the "
2950 << "<fmt> of the m-line: " << line;
2951 return true;
2952 }
2953 const std::string encoder = fields[1];
2954 std::vector<std::string> codec_params;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002955 rtc::split(encoder, '/', &codec_params);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002956 // <encoding name>/<clock rate>[/<encodingparameters>]
2957 // 2 mandatory fields
2958 if (codec_params.size() < 2 || codec_params.size() > 3) {
2959 return ParseFailed(line,
2960 "Expected format \"<encoding name>/<clock rate>"
2961 "[/<encodingparameters>]\".",
2962 error);
2963 }
2964 const std::string encoding_name = codec_params[0];
wu@webrtc.org5e760e72014-04-02 23:19:09 +00002965 int clock_rate = 0;
2966 if (!GetValueFromString(line, codec_params[1], &clock_rate, error)) {
2967 return false;
2968 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002969 if (media_type == cricket::MEDIA_TYPE_VIDEO) {
2970 VideoContentDescription* video_desc =
2971 static_cast<VideoContentDescription*>(media_desc);
2972 // TODO: We will send resolution in SDP. For now use
2973 // JsepSessionDescription::kMaxVideoCodecWidth and kMaxVideoCodecHeight.
2974 UpdateCodec(payload_type, encoding_name,
2975 JsepSessionDescription::kMaxVideoCodecWidth,
2976 JsepSessionDescription::kMaxVideoCodecHeight,
2977 JsepSessionDescription::kDefaultVideoCodecFramerate,
2978 preference, video_desc);
2979 } else if (media_type == cricket::MEDIA_TYPE_AUDIO) {
2980 // RFC 4566
2981 // For audio streams, <encoding parameters> indicates the number
2982 // of audio channels. This parameter is OPTIONAL and may be
2983 // omitted if the number of channels is one, provided that no
2984 // additional parameters are needed.
2985 int channels = 1;
2986 if (codec_params.size() == 3) {
wu@webrtc.org5e760e72014-04-02 23:19:09 +00002987 if (!GetValueFromString(line, codec_params[2], &channels, error)) {
2988 return false;
2989 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002990 }
2991 int bitrate = 0;
2992 // The default behavior for ISAC (bitrate == 0) in webrtcvoiceengine.cc
2993 // (specifically FindWebRtcCodec) is bandwidth-adaptive variable bitrate.
2994 // The bandwidth adaptation doesn't always work well, so this code
2995 // sets a fixed target bitrate instead.
2996 if (_stricmp(encoding_name.c_str(), kIsacCodecName) == 0) {
2997 if (clock_rate <= 16000) {
2998 bitrate = kIsacWbDefaultRate;
2999 } else {
3000 bitrate = kIsacSwbDefaultRate;
3001 }
3002 }
3003 AudioContentDescription* audio_desc =
3004 static_cast<AudioContentDescription*>(media_desc);
3005 UpdateCodec(payload_type, encoding_name, clock_rate, bitrate, channels,
3006 preference, audio_desc);
3007 } else if (media_type == cricket::MEDIA_TYPE_DATA) {
3008 DataContentDescription* data_desc =
3009 static_cast<DataContentDescription*>(media_desc);
3010 data_desc->AddCodec(cricket::DataCodec(payload_type, encoding_name,
3011 preference));
3012 }
3013 return true;
3014}
3015
3016void PruneRight(const char delimiter, std::string* message) {
3017 size_t trailing = message->find(delimiter);
3018 if (trailing != std::string::npos) {
3019 *message = message->substr(0, trailing);
3020 }
3021}
3022
3023bool ParseFmtpParam(const std::string& line, std::string* parameter,
3024 std::string* value, SdpParseError* error) {
3025 if (!SplitByDelimiter(line, kSdpDelimiterEqual, parameter, value)) {
3026 ParseFailed(line, "Unable to parse fmtp parameter. \'=\' missing.", error);
3027 return false;
3028 }
3029 // a=fmtp:<payload_type> <param1>=<value1>; <param2>=<value2>; ...
3030 // When parsing the values the trailing ";" gets picked up. Remove them.
3031 PruneRight(kSdpDelimiterSemicolon, value);
3032 return true;
3033}
3034
3035bool ParseFmtpAttributes(const std::string& line, const MediaType media_type,
3036 MediaContentDescription* media_desc,
3037 SdpParseError* error) {
3038 if (media_type != cricket::MEDIA_TYPE_AUDIO &&
3039 media_type != cricket::MEDIA_TYPE_VIDEO) {
3040 return true;
3041 }
3042 std::vector<std::string> fields;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00003043 rtc::split(line.substr(kLinePrefixLength),
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003044 kSdpDelimiterSpace, &fields);
3045
3046 // RFC 5576
3047 // a=fmtp:<format> <format specific parameters>
3048 // At least two fields, whereas the second one is any of the optional
3049 // parameters.
3050 if (fields.size() < 2) {
3051 ParseFailedExpectMinFieldNum(line, 2, error);
3052 return false;
3053 }
3054
pkasting@chromium.orgd3245462015-02-23 21:28:22 +00003055 std::string payload_type_str;
3056 if (!GetValue(fields[0], kAttributeFmtp, &payload_type_str, error)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003057 return false;
3058 }
3059
3060 cricket::CodecParameterMap codec_params;
3061 for (std::vector<std::string>::const_iterator iter = fields.begin() + 1;
3062 iter != fields.end(); ++iter) {
3063 std::string name;
3064 std::string value;
3065 if (iter->find(kSdpDelimiterEqual) == std::string::npos) {
3066 // Only fmtps with equals are currently supported. Other fmtp types
3067 // should be ignored. Unknown fmtps do not constitute an error.
3068 continue;
3069 }
3070 if (!ParseFmtpParam(*iter, &name, &value, error)) {
3071 return false;
3072 }
3073 codec_params[name] = value;
3074 }
3075
pkasting@chromium.orgd3245462015-02-23 21:28:22 +00003076 int payload_type = 0;
3077 if (!GetPayloadTypeFromString(line, payload_type_str, &payload_type, error)) {
wu@webrtc.org5e760e72014-04-02 23:19:09 +00003078 return false;
3079 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003080 if (media_type == cricket::MEDIA_TYPE_AUDIO) {
3081 UpdateCodec<AudioContentDescription, cricket::AudioCodec>(
pkasting@chromium.orgd3245462015-02-23 21:28:22 +00003082 media_desc, payload_type, codec_params);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003083 } else if (media_type == cricket::MEDIA_TYPE_VIDEO) {
3084 UpdateCodec<VideoContentDescription, cricket::VideoCodec>(
pkasting@chromium.orgd3245462015-02-23 21:28:22 +00003085 media_desc, payload_type, codec_params);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003086 }
3087 return true;
3088}
3089
3090bool ParseRtcpFbAttribute(const std::string& line, const MediaType media_type,
3091 MediaContentDescription* media_desc,
3092 SdpParseError* error) {
3093 if (media_type != cricket::MEDIA_TYPE_AUDIO &&
3094 media_type != cricket::MEDIA_TYPE_VIDEO) {
3095 return true;
3096 }
3097 std::vector<std::string> rtcp_fb_fields;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00003098 rtc::split(line.c_str(), kSdpDelimiterSpace, &rtcp_fb_fields);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003099 if (rtcp_fb_fields.size() < 2) {
3100 return ParseFailedGetValue(line, kAttributeRtcpFb, error);
3101 }
3102 std::string payload_type_string;
3103 if (!GetValue(rtcp_fb_fields[0], kAttributeRtcpFb, &payload_type_string,
3104 error)) {
3105 return false;
3106 }
wu@webrtc.org5e760e72014-04-02 23:19:09 +00003107 int payload_type = kWildcardPayloadType;
3108 if (payload_type_string != "*") {
pkasting@chromium.orge9facf82015-02-17 20:36:28 +00003109 if (!GetPayloadTypeFromString(line, payload_type_string, &payload_type,
3110 error)) {
wu@webrtc.org5e760e72014-04-02 23:19:09 +00003111 return false;
3112 }
3113 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003114 std::string id = rtcp_fb_fields[1];
3115 std::string param = "";
3116 for (std::vector<std::string>::iterator iter = rtcp_fb_fields.begin() + 2;
3117 iter != rtcp_fb_fields.end(); ++iter) {
3118 param.append(*iter);
3119 }
3120 const cricket::FeedbackParam feedback_param(id, param);
3121
3122 if (media_type == cricket::MEDIA_TYPE_AUDIO) {
pkasting@chromium.orgd3245462015-02-23 21:28:22 +00003123 UpdateCodec<AudioContentDescription, cricket::AudioCodec>(
3124 media_desc, payload_type, feedback_param);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003125 } else if (media_type == cricket::MEDIA_TYPE_VIDEO) {
pkasting@chromium.orgd3245462015-02-23 21:28:22 +00003126 UpdateCodec<VideoContentDescription, cricket::VideoCodec>(
3127 media_desc, payload_type, feedback_param);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003128 }
3129 return true;
3130}
3131
3132} // namespace webrtc