blob: eeb6a32391e74f1a0e5a58316046bbe70323988c [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"
mallinath@webrtc.org1112c302013-09-23 20:34:45 +000042#include "talk/media/sctp/sctpdataengine.h"
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000043#include "webrtc/p2p/base/candidate.h"
44#include "webrtc/p2p/base/constants.h"
45#include "webrtc/p2p/base/port.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000046#include "talk/session/media/mediasession.h"
buildbot@webrtc.orga09a9992014-08-13 17:26:08 +000047#include "webrtc/base/common.h"
48#include "webrtc/base/logging.h"
49#include "webrtc/base/messagedigest.h"
50#include "webrtc/base/stringutils.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000051
52using cricket::AudioContentDescription;
53using cricket::Candidate;
54using cricket::Candidates;
55using cricket::ContentDescription;
56using cricket::ContentInfo;
57using cricket::CryptoParams;
58using cricket::DataContentDescription;
59using cricket::ICE_CANDIDATE_COMPONENT_RTP;
60using cricket::ICE_CANDIDATE_COMPONENT_RTCP;
61using cricket::kCodecParamMaxBitrate;
62using cricket::kCodecParamMaxPTime;
63using cricket::kCodecParamMaxQuantization;
64using cricket::kCodecParamMinBitrate;
65using cricket::kCodecParamMinPTime;
66using cricket::kCodecParamPTime;
67using cricket::kCodecParamSPropStereo;
buildbot@webrtc.orged97bb02014-05-07 11:15:20 +000068using cricket::kCodecParamStartBitrate;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000069using cricket::kCodecParamStereo;
70using cricket::kCodecParamUseInbandFec;
71using cricket::kCodecParamSctpProtocol;
72using cricket::kCodecParamSctpStreams;
henrike@webrtc.org1e09a712013-07-26 19:17:59 +000073using cricket::kCodecParamMaxAverageBitrate;
buildbot@webrtc.org5d639b32014-09-10 07:57:12 +000074using cricket::kCodecParamMaxPlaybackRate;
stefan@webrtc.org85d27942014-06-09 12:51:39 +000075using cricket::kCodecParamAssociatedPayloadType;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000076using cricket::kWildcardPayloadType;
77using 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
225struct SsrcInfo {
226 SsrcInfo()
227 : msid_identifier(kDefaultMsid),
228 // TODO(ronghuawu): What should we do if the appdata doesn't appear?
229 // Create random string (which will be used as track label later)?
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000230 msid_appdata(rtc::CreateRandomString(8)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000231 }
232 uint32 ssrc_id;
233 std::string cname;
234 std::string msid_identifier;
235 std::string msid_appdata;
236
237 // For backward compatibility.
238 // TODO(ronghuawu): Remove below 2 fields once all the clients support msid.
239 std::string label;
240 std::string mslabel;
241};
242typedef std::vector<SsrcInfo> SsrcInfoVec;
243typedef std::vector<SsrcGroup> SsrcGroupVec;
244
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000245template <class T>
246static void AddFmtpLine(const T& codec, std::string* message);
247static void BuildMediaDescription(const ContentInfo* content_info,
248 const TransportInfo* transport_info,
249 const MediaType media_type,
wu@webrtc.org4c3e9912014-07-16 21:03:13 +0000250 const std::vector<Candidate>& candidates,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000251 std::string* message);
jiayl@webrtc.org9c16c392014-05-01 18:30:30 +0000252static void BuildSctpContentAttributes(std::string* message, int sctp_port);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000253static void BuildRtpContentAttributes(
254 const MediaContentDescription* media_desc,
255 const MediaType media_type,
256 std::string* message);
257static void BuildRtpMap(const MediaContentDescription* media_desc,
258 const MediaType media_type,
259 std::string* message);
260static void BuildCandidate(const std::vector<Candidate>& candidates,
261 std::string* message);
262static void BuildIceOptions(const std::vector<std::string>& transport_options,
263 std::string* message);
pthatcher@webrtc.org3341b402015-02-13 21:14:22 +0000264static bool IsRtp(const std::string& protocol);
265static bool IsDtlsSctp(const std::string& protocol);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000266static bool ParseSessionDescription(const std::string& message, size_t* pos,
267 std::string* session_id,
268 std::string* session_version,
269 bool* supports_msid,
270 TransportDescription* session_td,
271 RtpHeaderExtensions* session_extmaps,
272 cricket::SessionDescription* desc,
273 SdpParseError* error);
274static bool ParseGroupAttribute(const std::string& line,
275 cricket::SessionDescription* desc,
276 SdpParseError* error);
277static bool ParseMediaDescription(
278 const std::string& message,
279 const TransportDescription& session_td,
280 const RtpHeaderExtensions& session_extmaps,
281 bool supports_msid,
282 size_t* pos, cricket::SessionDescription* desc,
283 std::vector<JsepIceCandidate*>* candidates,
284 SdpParseError* error);
285static bool ParseContent(const std::string& message,
286 const MediaType media_type,
287 int mline_index,
288 const std::string& protocol,
289 const std::vector<int>& codec_preference,
290 size_t* pos,
291 std::string* content_name,
292 MediaContentDescription* media_desc,
293 TransportDescription* transport,
294 std::vector<JsepIceCandidate*>* candidates,
295 SdpParseError* error);
296static bool ParseSsrcAttribute(const std::string& line,
297 SsrcInfoVec* ssrc_infos,
298 SdpParseError* error);
299static bool ParseSsrcGroupAttribute(const std::string& line,
300 SsrcGroupVec* ssrc_groups,
301 SdpParseError* error);
302static bool ParseCryptoAttribute(const std::string& line,
303 MediaContentDescription* media_desc,
304 SdpParseError* error);
305static bool ParseRtpmapAttribute(const std::string& line,
306 const MediaType media_type,
307 const std::vector<int>& codec_preference,
308 MediaContentDescription* media_desc,
309 SdpParseError* error);
310static bool ParseFmtpAttributes(const std::string& line,
311 const MediaType media_type,
312 MediaContentDescription* media_desc,
313 SdpParseError* error);
314static bool ParseFmtpParam(const std::string& line, std::string* parameter,
315 std::string* value, SdpParseError* error);
316static bool ParseCandidate(const std::string& message, Candidate* candidate,
317 SdpParseError* error, bool is_raw);
318static bool ParseRtcpFbAttribute(const std::string& line,
319 const MediaType media_type,
320 MediaContentDescription* media_desc,
321 SdpParseError* error);
322static bool ParseIceOptions(const std::string& line,
323 std::vector<std::string>* transport_options,
324 SdpParseError* error);
325static bool ParseExtmap(const std::string& line,
326 RtpHeaderExtension* extmap,
327 SdpParseError* error);
328static bool ParseFingerprintAttribute(const std::string& line,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000329 rtc::SSLFingerprint** fingerprint,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000330 SdpParseError* error);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +0000331static bool ParseDtlsSetup(const std::string& line,
332 cricket::ConnectionRole* role,
333 SdpParseError* error);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000334
335// Helper functions
336
337// Below ParseFailed*** functions output the line that caused the parsing
338// failure and the detailed reason (|description|) of the failure to |error|.
339// The functions always return false so that they can be used directly in the
340// following way when error happens:
341// "return ParseFailed***(...);"
342
343// The line starting at |line_start| of |message| is the failing line.
344// The reason for the failure should be provided in the |description|.
345// An example of a description could be "unknown character".
346static bool ParseFailed(const std::string& message,
347 size_t line_start,
348 const std::string& description,
349 SdpParseError* error) {
350 // Get the first line of |message| from |line_start|.
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +0000351 std::string first_line;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000352 size_t line_end = message.find(kNewLine, line_start);
353 if (line_end != std::string::npos) {
354 if (line_end > 0 && (message.at(line_end - 1) == kReturn)) {
355 --line_end;
356 }
357 first_line = message.substr(line_start, (line_end - line_start));
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +0000358 } else {
359 first_line = message.substr(line_start);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000360 }
361
362 if (error) {
363 error->line = first_line;
364 error->description = description;
365 }
366 LOG(LS_ERROR) << "Failed to parse: \"" << first_line
367 << "\". Reason: " << description;
368 return false;
369}
370
371// |line| is the failing line. The reason for the failure should be
372// provided in the |description|.
373static bool ParseFailed(const std::string& line,
374 const std::string& description,
375 SdpParseError* error) {
376 return ParseFailed(line, 0, description, error);
377}
378
379// Parses failure where the failing SDP line isn't know or there are multiple
380// failing lines.
381static bool ParseFailed(const std::string& description,
382 SdpParseError* error) {
383 return ParseFailed("", description, error);
384}
385
386// |line| is the failing line. The failure is due to the fact that |line|
387// doesn't have |expected_fields| fields.
388static bool ParseFailedExpectFieldNum(const std::string& line,
389 int expected_fields,
390 SdpParseError* error) {
391 std::ostringstream description;
392 description << "Expects " << expected_fields << " fields.";
393 return ParseFailed(line, description.str(), error);
394}
395
396// |line| is the failing line. The failure is due to the fact that |line| has
397// less than |expected_min_fields| fields.
398static bool ParseFailedExpectMinFieldNum(const std::string& line,
399 int expected_min_fields,
400 SdpParseError* error) {
401 std::ostringstream description;
402 description << "Expects at least " << expected_min_fields << " fields.";
403 return ParseFailed(line, description.str(), error);
404}
405
406// |line| is the failing line. The failure is due to the fact that it failed to
407// get the value of |attribute|.
408static bool ParseFailedGetValue(const std::string& line,
409 const std::string& attribute,
410 SdpParseError* error) {
411 std::ostringstream description;
412 description << "Failed to get the value of attribute: " << attribute;
413 return ParseFailed(line, description.str(), error);
414}
415
416// The line starting at |line_start| of |message| is the failing line. The
417// failure is due to the line type (e.g. the "m" part of the "m-line")
418// not matching what is expected. The expected line type should be
419// provided as |line_type|.
420static bool ParseFailedExpectLine(const std::string& message,
421 size_t line_start,
422 const char line_type,
423 const std::string& line_value,
424 SdpParseError* error) {
425 std::ostringstream description;
426 description << "Expect line: " << line_type << "=" << line_value;
427 return ParseFailed(message, line_start, description.str(), error);
428}
429
430static bool AddLine(const std::string& line, std::string* message) {
431 if (!message)
432 return false;
433
434 message->append(line);
435 message->append(kLineBreak);
436 return true;
437}
438
439static bool GetLine(const std::string& message,
440 size_t* pos,
441 std::string* line) {
442 size_t line_begin = *pos;
443 size_t line_end = message.find(kNewLine, line_begin);
444 if (line_end == std::string::npos) {
445 return false;
446 }
447 // Update the new start position
448 *pos = line_end + 1;
449 if (line_end > 0 && (message.at(line_end - 1) == kReturn)) {
450 --line_end;
451 }
452 *line = message.substr(line_begin, (line_end - line_begin));
453 const char* cline = line->c_str();
454 // RFC 4566
455 // An SDP session description consists of a number of lines of text of
456 // the form:
457 // <type>=<value>
458 // where <type> MUST be exactly one case-significant character and
459 // <value> is structured text whose format depends on <type>.
460 // Whitespace MUST NOT be used on either side of the "=" sign.
decurtis@webrtc.org8af11042015-01-07 19:15:51 +0000461 if (line->length() < 3 ||
462 !islower(cline[0]) ||
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000463 cline[1] != kSdpDelimiterEqual ||
464 cline[2] == kSdpDelimiterSpace) {
465 *pos = line_begin;
466 return false;
467 }
468 return true;
469}
470
471// Init |os| to "|type|=|value|".
472static void InitLine(const char type,
473 const std::string& value,
474 std::ostringstream* os) {
475 os->str("");
476 *os << type << kSdpDelimiterEqual << value;
477}
478
479// Init |os| to "a=|attribute|".
480static void InitAttrLine(const std::string& attribute, std::ostringstream* os) {
481 InitLine(kLineTypeAttributes, attribute, os);
482}
483
484// Writes a SDP attribute line based on |attribute| and |value| to |message|.
485static void AddAttributeLine(const std::string& attribute, int value,
486 std::string* message) {
487 std::ostringstream os;
488 InitAttrLine(attribute, &os);
489 os << kSdpDelimiterColon << value;
490 AddLine(os.str(), message);
491}
492
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000493static bool IsLineType(const std::string& message,
494 const char type,
495 size_t line_start) {
496 if (message.size() < line_start + kLinePrefixLength) {
497 return false;
498 }
499 const char* cmessage = message.c_str();
500 return (cmessage[line_start] == type &&
501 cmessage[line_start + 1] == kSdpDelimiterEqual);
502}
503
504static bool IsLineType(const std::string& line,
505 const char type) {
506 return IsLineType(line, type, 0);
507}
508
509static bool GetLineWithType(const std::string& message, size_t* pos,
510 std::string* line, const char type) {
511 if (!IsLineType(message, type, *pos)) {
512 return false;
513 }
514
515 if (!GetLine(message, pos, line))
516 return false;
517
518 return true;
519}
520
521static bool HasAttribute(const std::string& line,
522 const std::string& attribute) {
523 return (line.compare(kLinePrefixLength, attribute.size(), attribute) == 0);
524}
525
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000526static bool AddSsrcLine(uint32 ssrc_id, const std::string& attribute,
527 const std::string& value, std::string* message) {
528 // RFC 5576
529 // a=ssrc:<ssrc-id> <attribute>:<value>
530 std::ostringstream os;
531 InitAttrLine(kAttributeSsrc, &os);
532 os << kSdpDelimiterColon << ssrc_id << kSdpDelimiterSpace
533 << attribute << kSdpDelimiterColon << value;
534 return AddLine(os.str(), message);
535}
536
537// Split the message into two parts by the first delimiter.
538static bool SplitByDelimiter(const std::string& message,
539 const char delimiter,
540 std::string* field1,
541 std::string* field2) {
542 // Find the first delimiter
543 size_t pos = message.find(delimiter);
544 if (pos == std::string::npos) {
545 return false;
546 }
547 *field1 = message.substr(0, pos);
548 // The rest is the value.
549 *field2 = message.substr(pos + 1);
550 return true;
551}
552
553// Get value only from <attribute>:<value>.
554static bool GetValue(const std::string& message, const std::string& attribute,
555 std::string* value, SdpParseError* error) {
556 std::string leftpart;
557 if (!SplitByDelimiter(message, kSdpDelimiterColon, &leftpart, value)) {
558 return ParseFailedGetValue(message, attribute, error);
559 }
560 // The left part should end with the expected attribute.
561 if (leftpart.length() < attribute.length() ||
562 leftpart.compare(leftpart.length() - attribute.length(),
563 attribute.length(), attribute) != 0) {
564 return ParseFailedGetValue(message, attribute, error);
565 }
566 return true;
567}
568
569static bool CaseInsensitiveFind(std::string str1, std::string str2) {
570 std::transform(str1.begin(), str1.end(), str1.begin(),
571 ::tolower);
572 std::transform(str2.begin(), str2.end(), str2.begin(),
573 ::tolower);
574 return str1.find(str2) != std::string::npos;
575}
576
wu@webrtc.org5e760e72014-04-02 23:19:09 +0000577template <class T>
578static bool GetValueFromString(const std::string& line,
579 const std::string& s,
580 T* t,
581 SdpParseError* error) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000582 if (!rtc::FromString(s, t)) {
wu@webrtc.org5e760e72014-04-02 23:19:09 +0000583 std::ostringstream description;
584 description << "Invalid value: " << s << ".";
585 return ParseFailed(line, description.str(), error);
586 }
587 return true;
588}
589
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000590void CreateTracksFromSsrcInfos(const SsrcInfoVec& ssrc_infos,
591 StreamParamsVec* tracks) {
592 ASSERT(tracks != NULL);
593 for (SsrcInfoVec::const_iterator ssrc_info = ssrc_infos.begin();
594 ssrc_info != ssrc_infos.end(); ++ssrc_info) {
595 if (ssrc_info->cname.empty()) {
596 continue;
597 }
598
599 std::string sync_label;
600 std::string track_id;
601 if (ssrc_info->msid_identifier == kDefaultMsid &&
602 !ssrc_info->mslabel.empty()) {
603 // If there's no msid and there's mslabel, we consider this is a sdp from
604 // a older version of client that doesn't support msid.
605 // In that case, we use the mslabel and label to construct the track.
606 sync_label = ssrc_info->mslabel;
607 track_id = ssrc_info->label;
608 } else {
609 sync_label = ssrc_info->msid_identifier;
610 // The appdata consists of the "id" attribute of a MediaStreamTrack, which
611 // is corresponding to the "id" attribute of StreamParams.
612 track_id = ssrc_info->msid_appdata;
613 }
614 if (sync_label.empty() || track_id.empty()) {
615 ASSERT(false);
616 continue;
617 }
618
619 StreamParamsVec::iterator track = tracks->begin();
620 for (; track != tracks->end(); ++track) {
621 if (track->id == track_id) {
622 break;
623 }
624 }
625 if (track == tracks->end()) {
626 // If we don't find an existing track, create a new one.
627 tracks->push_back(StreamParams());
628 track = tracks->end() - 1;
629 }
630 track->add_ssrc(ssrc_info->ssrc_id);
631 track->cname = ssrc_info->cname;
632 track->sync_label = sync_label;
633 track->id = track_id;
634 }
635}
636
637void GetMediaStreamLabels(const ContentInfo* content,
638 std::set<std::string>* labels) {
639 const MediaContentDescription* media_desc =
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000640 static_cast<const MediaContentDescription*>(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000641 content->description);
642 const cricket::StreamParamsVec& streams = media_desc->streams();
643 for (cricket::StreamParamsVec::const_iterator it = streams.begin();
644 it != streams.end(); ++it) {
645 labels->insert(it->sync_label);
646 }
647}
648
649// RFC 5245
650// It is RECOMMENDED that default candidates be chosen based on the
651// likelihood of those candidates to work with the peer that is being
652// contacted. It is RECOMMENDED that relayed > reflexive > host.
653static const int kPreferenceUnknown = 0;
654static const int kPreferenceHost = 1;
655static const int kPreferenceReflexive = 2;
656static const int kPreferenceRelayed = 3;
657
658static int GetCandidatePreferenceFromType(const std::string& type) {
659 int preference = kPreferenceUnknown;
660 if (type == cricket::LOCAL_PORT_TYPE) {
661 preference = kPreferenceHost;
662 } else if (type == cricket::STUN_PORT_TYPE) {
663 preference = kPreferenceReflexive;
664 } else if (type == cricket::RELAY_PORT_TYPE) {
665 preference = kPreferenceRelayed;
666 } else {
667 ASSERT(false);
668 }
669 return preference;
670}
671
guoweis@webrtc.org57ac2c82015-02-06 00:45:13 +0000672// Get ip and port of the default destination from the |candidates| with the
673// given value of |component_id|. The default candidate should be the one most
674// likely to work, typically IPv4 relay.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000675// RFC 5245
676// The value of |component_id| currently supported are 1 (RTP) and 2 (RTCP).
677// TODO: Decide the default destination in webrtcsession and
678// pass it down via SessionDescription.
pthatcher@webrtc.orgc9d6d142014-10-23 23:37:22 +0000679static void GetDefaultDestination(
680 const std::vector<Candidate>& candidates,
681 int component_id, std::string* port,
682 std::string* ip, std::string* addr_type) {
perkj@webrtc.orgd105cc82014-11-07 11:22:06 +0000683 *addr_type = kConnectionIpv4Addrtype;
pthatcher@webrtc.orgc9d6d142014-10-23 23:37:22 +0000684 *port = kDummyPort;
685 *ip = kDummyAddress;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000686 int current_preference = kPreferenceUnknown;
guoweis@webrtc.org57ac2c82015-02-06 00:45:13 +0000687 int current_family = AF_UNSPEC;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000688 for (std::vector<Candidate>::const_iterator it = candidates.begin();
689 it != candidates.end(); ++it) {
690 if (it->component() != component_id) {
691 continue;
692 }
guoweis@webrtc.org57ac2c82015-02-06 00:45:13 +0000693 // Default destination should be UDP only.
694 if (it->protocol() != cricket::UDP_PROTOCOL_NAME) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000695 continue;
696 }
guoweis@webrtc.org57ac2c82015-02-06 00:45:13 +0000697 const int preference = GetCandidatePreferenceFromType(it->type());
698 const int family = it->address().ipaddr().family();
699 // See if this candidate is more preferable then the current one if it's the
700 // same family. Or if the current family is IPv4 already so we could safely
701 // ignore all IPv6 ones. WebRTC bug 4269.
702 // http://code.google.com/p/webrtc/issues/detail?id=4269
703 if ((preference <= current_preference && current_family == family) ||
704 (current_family == AF_INET && family == AF_INET6)) {
705 continue;
706 }
pthatcher@webrtc.orgc9d6d142014-10-23 23:37:22 +0000707 if (family == AF_INET) {
708 addr_type->assign(kConnectionIpv4Addrtype);
709 } else if (family == AF_INET6) {
710 addr_type->assign(kConnectionIpv6Addrtype);
711 }
guoweis@webrtc.org57ac2c82015-02-06 00:45:13 +0000712 current_preference = preference;
713 current_family = family;
714 *port = it->address().PortAsString();
715 *ip = it->address().ipaddr().ToString();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000716 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000717}
718
wu@webrtc.org4c3e9912014-07-16 21:03:13 +0000719// Update |mline|'s default destination and append a c line after it.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000720static void UpdateMediaDefaultDestination(
wu@webrtc.org4c3e9912014-07-16 21:03:13 +0000721 const std::vector<Candidate>& candidates,
722 const std::string mline,
723 std::string* message) {
724 std::string new_lines;
725 AddLine(mline, &new_lines);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000726 // RFC 4566
727 // m=<media> <port> <proto> <fmt> ...
728 std::vector<std::string> fields;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000729 rtc::split(mline, kSdpDelimiterSpace, &fields);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000730 if (fields.size() < 3) {
731 return;
732 }
733
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000734 std::ostringstream os;
pthatcher@webrtc.orgc9d6d142014-10-23 23:37:22 +0000735 std::string rtp_port, rtp_ip, addr_type;
736 GetDefaultDestination(candidates, ICE_CANDIDATE_COMPONENT_RTP,
737 &rtp_port, &rtp_ip, &addr_type);
738 // Found default RTP candidate.
739 // RFC 5245
740 // The default candidates are added to the SDP as the default
741 // destination for media. For streams based on RTP, this is done by
742 // placing the IP address and port of the RTP candidate into the c and m
743 // lines, respectively.
744 // Update the port in the m line.
745 // If this is a m-line with port equal to 0, we don't change it.
746 if (fields[1] != kMediaPortRejected) {
747 new_lines.replace(fields[0].size() + 1,
748 fields[1].size(),
749 rtp_port);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000750 }
pthatcher@webrtc.orgc9d6d142014-10-23 23:37:22 +0000751 // Add the c line.
752 // RFC 4566
753 // c=<nettype> <addrtype> <connection-address>
754 InitLine(kLineTypeConnection, kConnectionNettype, &os);
755 os << " " << addr_type << " " << rtp_ip;
756 AddLine(os.str(), &new_lines);
wu@webrtc.org4c3e9912014-07-16 21:03:13 +0000757 message->append(new_lines);
758}
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000759
wu@webrtc.org4c3e9912014-07-16 21:03:13 +0000760// Gets "a=rtcp" line if found default RTCP candidate from |candidates|.
761static std::string GetRtcpLine(const std::vector<Candidate>& candidates) {
pthatcher@webrtc.orgc9d6d142014-10-23 23:37:22 +0000762 std::string rtcp_line, rtcp_port, rtcp_ip, addr_type;
763 GetDefaultDestination(candidates, ICE_CANDIDATE_COMPONENT_RTCP,
764 &rtcp_port, &rtcp_ip, &addr_type);
765 // Found default RTCP candidate.
766 // RFC 5245
767 // If the agent is utilizing RTCP, it MUST encode the RTCP candidate
768 // using the a=rtcp attribute as defined in RFC 3605.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000769
pthatcher@webrtc.orgc9d6d142014-10-23 23:37:22 +0000770 // RFC 3605
771 // rtcp-attribute = "a=rtcp:" port [nettype space addrtype space
772 // connection-address] CRLF
773 std::ostringstream os;
774 InitAttrLine(kAttributeRtcp, &os);
775 os << kSdpDelimiterColon
776 << rtcp_port << " "
777 << kConnectionNettype << " "
778 << addr_type << " "
779 << rtcp_ip;
780 rtcp_line = os.str();
wu@webrtc.org4c3e9912014-07-16 21:03:13 +0000781 return rtcp_line;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000782}
783
784// Get candidates according to the mline index from SessionDescriptionInterface.
785static void GetCandidatesByMindex(const SessionDescriptionInterface& desci,
786 int mline_index,
787 std::vector<Candidate>* candidates) {
788 if (!candidates) {
789 return;
790 }
791 const IceCandidateCollection* cc = desci.candidates(mline_index);
792 for (size_t i = 0; i < cc->count(); ++i) {
793 const IceCandidateInterface* candidate = cc->at(i);
794 candidates->push_back(candidate->candidate());
795 }
796}
797
798std::string SdpSerialize(const JsepSessionDescription& jdesc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000799 const cricket::SessionDescription* desc = jdesc.description();
800 if (!desc) {
801 return "";
802 }
803
804 std::string message;
805
806 // Session Description.
807 AddLine(kSessionVersion, &message);
808 // Session Origin
809 // RFC 4566
810 // o=<username> <sess-id> <sess-version> <nettype> <addrtype>
811 // <unicast-address>
812 std::ostringstream os;
813 InitLine(kLineTypeOrigin, kSessionOriginUsername, &os);
814 const std::string session_id = jdesc.session_id().empty() ?
815 kSessionOriginSessionId : jdesc.session_id();
816 const std::string session_version = jdesc.session_version().empty() ?
817 kSessionOriginSessionVersion : jdesc.session_version();
818 os << " " << session_id << " " << session_version << " "
819 << kSessionOriginNettype << " " << kSessionOriginAddrtype << " "
820 << kSessionOriginAddress;
821 AddLine(os.str(), &message);
822 AddLine(kSessionName, &message);
823
824 // Time Description.
825 AddLine(kTimeDescription, &message);
826
827 // Group
828 if (desc->HasGroup(cricket::GROUP_TYPE_BUNDLE)) {
829 std::string group_line = kAttrGroup;
830 const cricket::ContentGroup* group =
831 desc->GetGroupByName(cricket::GROUP_TYPE_BUNDLE);
832 ASSERT(group != NULL);
833 const cricket::ContentNames& content_names = group->content_names();
834 for (cricket::ContentNames::const_iterator it = content_names.begin();
835 it != content_names.end(); ++it) {
836 group_line.append(" ");
837 group_line.append(*it);
838 }
839 AddLine(group_line, &message);
840 }
841
842 // MediaStream semantics
843 InitAttrLine(kAttributeMsidSemantics, &os);
844 os << kSdpDelimiterColon << " " << kMediaStreamSemantic;
wu@webrtc.org4c3e9912014-07-16 21:03:13 +0000845
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000846 std::set<std::string> media_stream_labels;
847 const ContentInfo* audio_content = GetFirstAudioContent(desc);
848 if (audio_content)
849 GetMediaStreamLabels(audio_content, &media_stream_labels);
wu@webrtc.org4c3e9912014-07-16 21:03:13 +0000850
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000851 const ContentInfo* video_content = GetFirstVideoContent(desc);
852 if (video_content)
853 GetMediaStreamLabels(video_content, &media_stream_labels);
wu@webrtc.org4c3e9912014-07-16 21:03:13 +0000854
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000855 for (std::set<std::string>::const_iterator it =
856 media_stream_labels.begin(); it != media_stream_labels.end(); ++it) {
857 os << " " << *it;
858 }
859 AddLine(os.str(), &message);
860
wu@webrtc.org4c3e9912014-07-16 21:03:13 +0000861 // Preserve the order of the media contents.
862 int mline_index = -1;
863 for (cricket::ContentInfos::const_iterator it = desc->contents().begin();
864 it != desc->contents().end(); ++it) {
865 const MediaContentDescription* mdesc =
866 static_cast<const MediaContentDescription*>(it->description);
867 std::vector<Candidate> candidates;
868 GetCandidatesByMindex(jdesc, ++mline_index, &candidates);
869 BuildMediaDescription(&*it,
870 desc->GetTransportInfoByName(it->name),
871 mdesc->type(),
872 candidates,
873 &message);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000874 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000875 return message;
876}
877
878// Serializes the passed in IceCandidateInterface to a SDP string.
879// candidate - The candidate to be serialized.
880std::string SdpSerializeCandidate(
881 const IceCandidateInterface& candidate) {
882 std::string message;
883 std::vector<cricket::Candidate> candidates;
884 candidates.push_back(candidate.candidate());
885 BuildCandidate(candidates, &message);
wu@webrtc.orgec9f5fb2014-06-24 17:05:10 +0000886 // From WebRTC draft section 4.8.1.1 candidate-attribute will be
887 // just candidate:<candidate> not a=candidate:<blah>CRLF
888 ASSERT(message.find("a=") == 0);
889 message.erase(0, 2);
890 ASSERT(message.find(kLineBreak) == message.size() - 2);
891 message.resize(message.size() - 2);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000892 return message;
893}
894
895bool SdpDeserialize(const std::string& message,
896 JsepSessionDescription* jdesc,
897 SdpParseError* error) {
898 std::string session_id;
899 std::string session_version;
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +0000900 TransportDescription session_td(NS_JINGLE_ICE_UDP,
901 std::string(), std::string());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000902 RtpHeaderExtensions session_extmaps;
903 cricket::SessionDescription* desc = new cricket::SessionDescription();
904 std::vector<JsepIceCandidate*> candidates;
905 size_t current_pos = 0;
906 bool supports_msid = false;
907
908 // Session Description
909 if (!ParseSessionDescription(message, &current_pos, &session_id,
910 &session_version, &supports_msid, &session_td,
911 &session_extmaps, desc, error)) {
912 delete desc;
913 return false;
914 }
915
916 // Media Description
917 if (!ParseMediaDescription(message, session_td, session_extmaps,
918 supports_msid, &current_pos, desc, &candidates,
919 error)) {
920 delete desc;
921 for (std::vector<JsepIceCandidate*>::const_iterator
922 it = candidates.begin(); it != candidates.end(); ++it) {
923 delete *it;
924 }
925 return false;
926 }
927
928 jdesc->Initialize(desc, session_id, session_version);
929
930 for (std::vector<JsepIceCandidate*>::const_iterator
931 it = candidates.begin(); it != candidates.end(); ++it) {
932 jdesc->AddCandidate(*it);
933 delete *it;
934 }
935 return true;
936}
937
938bool SdpDeserializeCandidate(const std::string& message,
939 JsepIceCandidate* jcandidate,
940 SdpParseError* error) {
941 ASSERT(jcandidate != NULL);
942 Candidate candidate;
943 if (!ParseCandidate(message, &candidate, error, true)) {
944 return false;
945 }
946 jcandidate->SetCandidate(candidate);
947 return true;
948}
949
950bool ParseCandidate(const std::string& message, Candidate* candidate,
951 SdpParseError* error, bool is_raw) {
952 ASSERT(candidate != NULL);
953
954 // Get the first line from |message|.
jiayl@webrtc.org7ec3f9f2014-08-08 23:09:15 +0000955 std::string first_line = message;
956 size_t pos = 0;
957 GetLine(message, &pos, &first_line);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000958
jiayl@webrtc.org7ec3f9f2014-08-08 23:09:15 +0000959 // Makes sure |message| contains only one line.
960 if (message.size() > first_line.size()) {
961 std::string left, right;
962 if (SplitByDelimiter(message, kNewLine, &left, &right) && !right.empty()) {
963 return ParseFailed(message, 0, "Expect one line only", error);
964 }
965 }
966
967 // From WebRTC draft section 4.8.1.1 candidate-attribute should be
968 // candidate:<candidate> when trickled, but we still support
969 // a=candidate:<blah>CRLF for backward compatibility and for parsing a line
970 // from the SDP.
971 if (IsLineType(first_line, kLineTypeAttributes)) {
972 first_line = first_line.substr(kLinePrefixLength);
973 }
974
975 std::string attribute_candidate;
976 std::string candidate_value;
977
978 // |first_line| must be in the form of "candidate:<value>".
979 if (!SplitByDelimiter(first_line, kSdpDelimiterColon,
980 &attribute_candidate, &candidate_value) ||
981 attribute_candidate != kAttributeCandidate) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000982 if (is_raw) {
983 std::ostringstream description;
jiayl@webrtc.org7ec3f9f2014-08-08 23:09:15 +0000984 description << "Expect line: " << kAttributeCandidate
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000985 << ":" << "<candidate-str>";
986 return ParseFailed(first_line, 0, description.str(), error);
987 } else {
988 return ParseFailedExpectLine(first_line, 0, kLineTypeAttributes,
989 kAttributeCandidate, error);
990 }
991 }
992
993 std::vector<std::string> fields;
jiayl@webrtc.org7ec3f9f2014-08-08 23:09:15 +0000994 rtc::split(candidate_value, kSdpDelimiterSpace, &fields);
995
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000996 // RFC 5245
997 // a=candidate:<foundation> <component-id> <transport> <priority>
998 // <connection-address> <port> typ <candidate-types>
999 // [raddr <connection-address>] [rport <port>]
1000 // *(SP extension-att-name SP extension-att-value)
1001 const size_t expected_min_fields = 8;
1002 if (fields.size() < expected_min_fields ||
1003 (fields[6] != kAttributeCandidateTyp)) {
1004 return ParseFailedExpectMinFieldNum(first_line, expected_min_fields, error);
1005 }
jiayl@webrtc.org7ec3f9f2014-08-08 23:09:15 +00001006 std::string foundation = fields[0];
1007
wu@webrtc.org5e760e72014-04-02 23:19:09 +00001008 int component_id = 0;
1009 if (!GetValueFromString(first_line, fields[1], &component_id, error)) {
1010 return false;
1011 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001012 const std::string transport = fields[2];
wu@webrtc.org5e760e72014-04-02 23:19:09 +00001013 uint32 priority = 0;
1014 if (!GetValueFromString(first_line, fields[3], &priority, error)) {
1015 return false;
1016 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001017 const std::string connection_address = fields[4];
wu@webrtc.org5e760e72014-04-02 23:19:09 +00001018 int port = 0;
1019 if (!GetValueFromString(first_line, fields[5], &port, error)) {
1020 return false;
1021 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001022 SocketAddress address(connection_address, port);
1023
1024 cricket::ProtocolType protocol;
1025 if (!StringToProto(transport.c_str(), &protocol)) {
1026 return ParseFailed(first_line, "Unsupported transport type.", error);
1027 }
1028
1029 std::string candidate_type;
1030 const std::string type = fields[7];
1031 if (type == kCandidateHost) {
1032 candidate_type = cricket::LOCAL_PORT_TYPE;
1033 } else if (type == kCandidateSrflx) {
1034 candidate_type = cricket::STUN_PORT_TYPE;
1035 } else if (type == kCandidateRelay) {
1036 candidate_type = cricket::RELAY_PORT_TYPE;
1037 } else {
1038 return ParseFailed(first_line, "Unsupported candidate type.", error);
1039 }
1040
1041 size_t current_position = expected_min_fields;
1042 SocketAddress related_address;
1043 // The 2 optional fields for related address
1044 // [raddr <connection-address>] [rport <port>]
1045 if (fields.size() >= (current_position + 2) &&
1046 fields[current_position] == kAttributeCandidateRaddr) {
1047 related_address.SetIP(fields[++current_position]);
1048 ++current_position;
1049 }
1050 if (fields.size() >= (current_position + 2) &&
1051 fields[current_position] == kAttributeCandidateRport) {
wu@webrtc.org5e760e72014-04-02 23:19:09 +00001052 int port = 0;
1053 if (!GetValueFromString(
1054 first_line, fields[++current_position], &port, error)) {
1055 return false;
1056 }
1057 related_address.SetPort(port);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001058 ++current_position;
1059 }
1060
mallinath@webrtc.org2d60c5e2014-08-08 22:29:20 +00001061 // If this is a TCP candidate, it has additional extension as defined in
1062 // RFC 6544.
1063 std::string tcptype;
1064 if (fields.size() >= (current_position + 2) &&
1065 fields[current_position] == kTcpCandidateType) {
1066 tcptype = fields[++current_position];
1067 ++current_position;
1068
1069 if (tcptype != cricket::TCPTYPE_ACTIVE_STR &&
1070 tcptype != cricket::TCPTYPE_PASSIVE_STR &&
1071 tcptype != cricket::TCPTYPE_SIMOPEN_STR) {
1072 return ParseFailed(first_line, "Invalid TCP candidate type.", error);
1073 }
1074
1075 if (protocol != cricket::PROTO_TCP) {
1076 return ParseFailed(first_line, "Invalid non-TCP candidate", error);
1077 }
1078 }
1079
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001080 // Extension
1081 // Empty string as the candidate username and password.
1082 // Will be updated later with the ice-ufrag and ice-pwd.
1083 // TODO: Remove the username/password extension, which is currently
1084 // kept for backwards compatibility.
1085 std::string username;
1086 std::string password;
1087 uint32 generation = 0;
1088 for (size_t i = current_position; i + 1 < fields.size(); ++i) {
1089 // RFC 5245
1090 // *(SP extension-att-name SP extension-att-value)
1091 if (fields[i] == kAttributeCandidateGeneration) {
wu@webrtc.org5e760e72014-04-02 23:19:09 +00001092 if (!GetValueFromString(first_line, fields[++i], &generation, error)) {
1093 return false;
1094 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001095 } else if (fields[i] == kAttributeCandidateUsername) {
1096 username = fields[++i];
1097 } else if (fields[i] == kAttributeCandidatePassword) {
1098 password = fields[++i];
1099 } else {
1100 // Skip the unknown extension.
1101 ++i;
1102 }
1103 }
1104
guoweis@webrtc.org61c12472015-01-15 06:53:07 +00001105 *candidate = Candidate(component_id, cricket::ProtoToString(protocol),
guoweis@webrtc.org950c5182014-12-16 23:01:31 +00001106 address, priority, username, password, candidate_type,
1107 generation, foundation);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001108 candidate->set_related_address(related_address);
mallinath@webrtc.org2d60c5e2014-08-08 22:29:20 +00001109 candidate->set_tcptype(tcptype);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001110 return true;
1111}
1112
1113bool ParseIceOptions(const std::string& line,
1114 std::vector<std::string>* transport_options,
1115 SdpParseError* error) {
1116 std::string ice_options;
1117 if (!GetValue(line, kAttributeIceOption, &ice_options, error)) {
1118 return false;
1119 }
1120 std::vector<std::string> fields;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001121 rtc::split(ice_options, kSdpDelimiterSpace, &fields);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001122 for (size_t i = 0; i < fields.size(); ++i) {
1123 transport_options->push_back(fields[i]);
1124 }
1125 return true;
1126}
1127
jiayl@webrtc.orgddb85ab2014-09-05 16:31:56 +00001128bool ParseSctpPort(const std::string& line,
1129 int* sctp_port,
1130 SdpParseError* error) {
1131 // draft-ietf-mmusic-sctp-sdp-07
1132 // a=sctp-port
1133 std::vector<std::string> fields;
1134 rtc::split(line.substr(kLinePrefixLength),
1135 kSdpDelimiterSpace, &fields);
1136 const size_t expected_min_fields = 2;
1137 if (fields.size() < expected_min_fields) {
1138 return ParseFailedExpectMinFieldNum(line, expected_min_fields, error);
1139 }
1140 if (!rtc::FromString(fields[1], sctp_port)) {
1141 return ParseFailed(line,
1142 "Invalid sctp port value.",
1143 error);
1144 }
1145 return true;
1146}
1147
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001148bool ParseExtmap(const std::string& line, RtpHeaderExtension* extmap,
1149 SdpParseError* error) {
1150 // RFC 5285
1151 // a=extmap:<value>["/"<direction>] <URI> <extensionattributes>
1152 std::vector<std::string> fields;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001153 rtc::split(line.substr(kLinePrefixLength),
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001154 kSdpDelimiterSpace, &fields);
1155 const size_t expected_min_fields = 2;
1156 if (fields.size() < expected_min_fields) {
1157 return ParseFailedExpectMinFieldNum(line, expected_min_fields, error);
1158 }
1159 std::string uri = fields[1];
1160
1161 std::string value_direction;
1162 if (!GetValue(fields[0], kAttributeExtmap, &value_direction, error)) {
1163 return false;
1164 }
1165 std::vector<std::string> sub_fields;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001166 rtc::split(value_direction, kSdpDelimiterSlash, &sub_fields);
wu@webrtc.org5e760e72014-04-02 23:19:09 +00001167 int value = 0;
1168 if (!GetValueFromString(line, sub_fields[0], &value, error)) {
1169 return false;
1170 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001171
1172 *extmap = RtpHeaderExtension(uri, value);
1173 return true;
1174}
1175
1176void BuildMediaDescription(const ContentInfo* content_info,
1177 const TransportInfo* transport_info,
1178 const MediaType media_type,
wu@webrtc.org4c3e9912014-07-16 21:03:13 +00001179 const std::vector<Candidate>& candidates,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001180 std::string* message) {
1181 ASSERT(message != NULL);
1182 if (content_info == NULL || message == NULL) {
1183 return;
1184 }
1185 // TODO: Rethink if we should use sprintfn instead of stringstream.
1186 // According to the style guide, streams should only be used for logging.
1187 // http://google-styleguide.googlecode.com/svn/
1188 // trunk/cppguide.xml?showone=Streams#Streams
1189 std::ostringstream os;
1190 const MediaContentDescription* media_desc =
henrike@webrtc.org28654cb2013-07-22 21:07:49 +00001191 static_cast<const MediaContentDescription*>(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001192 content_info->description);
1193 ASSERT(media_desc != NULL);
1194
jiayl@webrtc.org9c16c392014-05-01 18:30:30 +00001195 int sctp_port = cricket::kSctpDefaultPort;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001196
1197 // RFC 4566
1198 // m=<media> <port> <proto> <fmt>
1199 // fmt is a list of payload type numbers that MAY be used in the session.
1200 const char* type = NULL;
1201 if (media_type == cricket::MEDIA_TYPE_AUDIO)
1202 type = kMediaTypeAudio;
1203 else if (media_type == cricket::MEDIA_TYPE_VIDEO)
1204 type = kMediaTypeVideo;
1205 else if (media_type == cricket::MEDIA_TYPE_DATA)
1206 type = kMediaTypeData;
1207 else
1208 ASSERT(false);
1209
1210 std::string fmt;
1211 if (media_type == cricket::MEDIA_TYPE_VIDEO) {
1212 const VideoContentDescription* video_desc =
1213 static_cast<const VideoContentDescription*>(media_desc);
1214 for (std::vector<cricket::VideoCodec>::const_iterator it =
1215 video_desc->codecs().begin();
1216 it != video_desc->codecs().end(); ++it) {
1217 fmt.append(" ");
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001218 fmt.append(rtc::ToString<int>(it->id));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001219 }
1220 } else if (media_type == cricket::MEDIA_TYPE_AUDIO) {
1221 const AudioContentDescription* audio_desc =
1222 static_cast<const AudioContentDescription*>(media_desc);
1223 for (std::vector<cricket::AudioCodec>::const_iterator it =
1224 audio_desc->codecs().begin();
1225 it != audio_desc->codecs().end(); ++it) {
1226 fmt.append(" ");
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001227 fmt.append(rtc::ToString<int>(it->id));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001228 }
1229 } else if (media_type == cricket::MEDIA_TYPE_DATA) {
jiayl@webrtc.org9c16c392014-05-01 18:30:30 +00001230 const DataContentDescription* data_desc =
1231 static_cast<const DataContentDescription*>(media_desc);
pthatcher@webrtc.org3341b402015-02-13 21:14:22 +00001232 if (IsDtlsSctp(media_desc->protocol())) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001233 fmt.append(" ");
jiayl@webrtc.org9c16c392014-05-01 18:30:30 +00001234
1235 for (std::vector<cricket::DataCodec>::const_iterator it =
1236 data_desc->codecs().begin();
1237 it != data_desc->codecs().end(); ++it) {
1238 if (it->id == cricket::kGoogleSctpDataCodecId &&
1239 it->GetParam(cricket::kCodecParamPort, &sctp_port)) {
1240 break;
1241 }
1242 }
1243
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001244 fmt.append(rtc::ToString<int>(sctp_port));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001245 } else {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001246 for (std::vector<cricket::DataCodec>::const_iterator it =
1247 data_desc->codecs().begin();
1248 it != data_desc->codecs().end(); ++it) {
1249 fmt.append(" ");
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001250 fmt.append(rtc::ToString<int>(it->id));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001251 }
1252 }
1253 }
1254 // The fmt must never be empty. If no codecs are found, set the fmt attribute
1255 // to 0.
1256 if (fmt.empty()) {
1257 fmt = " 0";
1258 }
1259
1260 // The port number in the m line will be updated later when associate with
1261 // the candidates.
1262 // RFC 3264
1263 // To reject an offered stream, the port number in the corresponding stream in
1264 // the answer MUST be set to zero.
1265 const std::string port = content_info->rejected ?
pthatcher@webrtc.orgc9d6d142014-10-23 23:37:22 +00001266 kMediaPortRejected : kDummyPort;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001267
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001268 rtc::SSLFingerprint* fp = (transport_info) ?
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001269 transport_info->description.identity_fingerprint.get() : NULL;
1270
wu@webrtc.org4c3e9912014-07-16 21:03:13 +00001271 // Add the m and c lines.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001272 InitLine(kLineTypeMedia, type, &os);
1273 os << " " << port << " " << media_desc->protocol() << fmt;
wu@webrtc.org4c3e9912014-07-16 21:03:13 +00001274 std::string mline = os.str();
1275 UpdateMediaDefaultDestination(candidates, mline, message);
1276
1277 // RFC 4566
1278 // b=AS:<bandwidth>
1279 // We should always use the default bandwidth for RTP-based data
1280 // channels. Don't allow SDP to set the bandwidth, because that
1281 // would give JS the opportunity to "break the Internet".
1282 // TODO(pthatcher): But we need to temporarily allow the SDP to control
1283 // this for backwards-compatibility. Once we don't need that any
1284 // more, remove this.
1285 bool support_dc_sdp_bandwidth_temporarily = true;
1286 if (media_desc->bandwidth() >= 1000 &&
1287 (media_type != cricket::MEDIA_TYPE_DATA ||
1288 support_dc_sdp_bandwidth_temporarily)) {
1289 InitLine(kLineTypeSessionBandwidth, kApplicationSpecificMaximum, &os);
1290 os << kSdpDelimiterColon << (media_desc->bandwidth() / 1000);
1291 AddLine(os.str(), message);
1292 }
1293
1294 // Add the a=rtcp line.
pthatcher@webrtc.org3341b402015-02-13 21:14:22 +00001295 if (IsRtp(media_desc->protocol())) {
wu@webrtc.org4c3e9912014-07-16 21:03:13 +00001296 std::string rtcp_line = GetRtcpLine(candidates);
1297 if (!rtcp_line.empty()) {
1298 AddLine(rtcp_line, message);
1299 }
1300 }
1301
1302 // Build the a=candidate lines.
1303 BuildCandidate(candidates, message);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001304
1305 // Use the transport_info to build the media level ice-ufrag and ice-pwd.
1306 if (transport_info) {
1307 // RFC 5245
1308 // ice-pwd-att = "ice-pwd" ":" password
1309 // ice-ufrag-att = "ice-ufrag" ":" ufrag
1310 // ice-ufrag
1311 InitAttrLine(kAttributeIceUfrag, &os);
1312 os << kSdpDelimiterColon << transport_info->description.ice_ufrag;
1313 AddLine(os.str(), message);
1314 // ice-pwd
1315 InitAttrLine(kAttributeIcePwd, &os);
1316 os << kSdpDelimiterColon << transport_info->description.ice_pwd;
1317 AddLine(os.str(), message);
1318
1319 // draft-petithuguenin-mmusic-ice-attributes-level-03
1320 BuildIceOptions(transport_info->description.transport_options, message);
1321
1322 // RFC 4572
1323 // fingerprint-attribute =
1324 // "fingerprint" ":" hash-func SP fingerprint
1325 if (fp) {
1326 // Insert the fingerprint attribute.
1327 InitAttrLine(kAttributeFingerprint, &os);
1328 os << kSdpDelimiterColon
1329 << fp->algorithm << kSdpDelimiterSpace
1330 << fp->GetRfc4572Fingerprint();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001331 AddLine(os.str(), message);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001332
1333 // Inserting setup attribute.
1334 if (transport_info->description.connection_role !=
1335 cricket::CONNECTIONROLE_NONE) {
1336 // Making sure we are not using "passive" mode.
1337 cricket::ConnectionRole role =
1338 transport_info->description.connection_role;
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00001339 std::string dtls_role_str;
1340 VERIFY(cricket::ConnectionRoleToString(role, &dtls_role_str));
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001341 InitAttrLine(kAttributeSetup, &os);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001342 os << kSdpDelimiterColon << dtls_role_str;
1343 AddLine(os.str(), message);
1344 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001345 }
1346 }
1347
1348 // RFC 3388
1349 // mid-attribute = "a=mid:" identification-tag
1350 // identification-tag = token
1351 // Use the content name as the mid identification-tag.
1352 InitAttrLine(kAttributeMid, &os);
1353 os << kSdpDelimiterColon << content_info->name;
1354 AddLine(os.str(), message);
1355
pthatcher@webrtc.org3341b402015-02-13 21:14:22 +00001356 if (IsDtlsSctp(media_desc->protocol())) {
jiayl@webrtc.org9c16c392014-05-01 18:30:30 +00001357 BuildSctpContentAttributes(message, sctp_port);
pthatcher@webrtc.org3341b402015-02-13 21:14:22 +00001358 } else if (IsRtp(media_desc->protocol())) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001359 BuildRtpContentAttributes(media_desc, media_type, message);
1360 }
1361}
1362
jiayl@webrtc.org9c16c392014-05-01 18:30:30 +00001363void BuildSctpContentAttributes(std::string* message, int sctp_port) {
wu@webrtc.org78187522013-10-07 23:32:02 +00001364 // draft-ietf-mmusic-sctp-sdp-04
1365 // a=sctpmap:sctpmap-number protocol [streams]
1366 std::ostringstream os;
1367 InitAttrLine(kAttributeSctpmap, &os);
jiayl@webrtc.org9c16c392014-05-01 18:30:30 +00001368 os << kSdpDelimiterColon << sctp_port << kSdpDelimiterSpace
wu@webrtc.org78187522013-10-07 23:32:02 +00001369 << kDefaultSctpmapProtocol << kSdpDelimiterSpace
1370 << (cricket::kMaxSctpSid + 1);
1371 AddLine(os.str(), message);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001372}
1373
1374void BuildRtpContentAttributes(
1375 const MediaContentDescription* media_desc,
1376 const MediaType media_type,
1377 std::string* message) {
1378 std::ostringstream os;
1379 // RFC 5285
1380 // a=extmap:<value>["/"<direction>] <URI> <extensionattributes>
1381 // The definitions MUST be either all session level or all media level. This
1382 // implementation uses all media level.
1383 for (size_t i = 0; i < media_desc->rtp_header_extensions().size(); ++i) {
1384 InitAttrLine(kAttributeExtmap, &os);
1385 os << kSdpDelimiterColon << media_desc->rtp_header_extensions()[i].id
1386 << kSdpDelimiterSpace << media_desc->rtp_header_extensions()[i].uri;
1387 AddLine(os.str(), message);
1388 }
1389
1390 // RFC 3264
1391 // a=sendrecv || a=sendonly || a=sendrecv || a=inactive
1392
1393 cricket::MediaContentDirection direction = media_desc->direction();
1394 if (media_desc->streams().empty() && direction == cricket::MD_SENDRECV) {
1395 direction = cricket::MD_RECVONLY;
1396 }
1397
1398 switch (direction) {
1399 case cricket::MD_INACTIVE:
1400 InitAttrLine(kAttributeInactive, &os);
1401 break;
1402 case cricket::MD_SENDONLY:
1403 InitAttrLine(kAttributeSendOnly, &os);
1404 break;
1405 case cricket::MD_RECVONLY:
1406 InitAttrLine(kAttributeRecvOnly, &os);
1407 break;
1408 case cricket::MD_SENDRECV:
1409 default:
1410 InitAttrLine(kAttributeSendRecv, &os);
1411 break;
1412 }
1413 AddLine(os.str(), message);
1414
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001415 // RFC 5761
1416 // a=rtcp-mux
1417 if (media_desc->rtcp_mux()) {
1418 InitAttrLine(kAttributeRtcpMux, &os);
1419 AddLine(os.str(), message);
1420 }
1421
1422 // RFC 4568
1423 // a=crypto:<tag> <crypto-suite> <key-params> [<session-params>]
1424 for (std::vector<CryptoParams>::const_iterator it =
1425 media_desc->cryptos().begin();
1426 it != media_desc->cryptos().end(); ++it) {
1427 InitAttrLine(kAttributeCrypto, &os);
1428 os << kSdpDelimiterColon << it->tag << " " << it->cipher_suite << " "
1429 << it->key_params;
1430 if (!it->session_params.empty()) {
1431 os << " " << it->session_params;
1432 }
1433 AddLine(os.str(), message);
1434 }
1435
1436 // RFC 4566
1437 // a=rtpmap:<payload type> <encoding name>/<clock rate>
1438 // [/<encodingparameters>]
1439 BuildRtpMap(media_desc, media_type, message);
1440
1441 // Specify latency for buffered mode.
1442 // a=x-google-buffer-latency:<value>
1443 if (media_desc->buffered_mode_latency() != cricket::kBufferedModeDisabled) {
1444 std::ostringstream os;
1445 InitAttrLine(kAttributeXGoogleBufferLatency, &os);
1446 os << kSdpDelimiterColon << media_desc->buffered_mode_latency();
1447 AddLine(os.str(), message);
1448 }
1449
1450 for (StreamParamsVec::const_iterator track = media_desc->streams().begin();
1451 track != media_desc->streams().end(); ++track) {
1452 // Require that the track belongs to a media stream,
1453 // ie the sync_label is set. This extra check is necessary since the
1454 // MediaContentDescription always contains a streamparam with an ssrc even
1455 // if no track or media stream have been created.
1456 if (track->sync_label.empty()) continue;
1457
1458 // Build the ssrc-group lines.
1459 for (size_t i = 0; i < track->ssrc_groups.size(); ++i) {
1460 // RFC 5576
1461 // a=ssrc-group:<semantics> <ssrc-id> ...
1462 if (track->ssrc_groups[i].ssrcs.empty()) {
1463 continue;
1464 }
1465 std::ostringstream os;
1466 InitAttrLine(kAttributeSsrcGroup, &os);
1467 os << kSdpDelimiterColon << track->ssrc_groups[i].semantics;
1468 std::vector<uint32>::const_iterator ssrc =
1469 track->ssrc_groups[i].ssrcs.begin();
1470 for (; ssrc != track->ssrc_groups[i].ssrcs.end(); ++ssrc) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001471 os << kSdpDelimiterSpace << rtc::ToString<uint32>(*ssrc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001472 }
1473 AddLine(os.str(), message);
1474 }
1475 // Build the ssrc lines for each ssrc.
1476 for (size_t i = 0; i < track->ssrcs.size(); ++i) {
1477 uint32 ssrc = track->ssrcs[i];
1478 // RFC 5576
1479 // a=ssrc:<ssrc-id> cname:<value>
1480 AddSsrcLine(ssrc, kSsrcAttributeCname,
1481 track->cname, message);
1482
1483 // draft-alvestrand-mmusic-msid-00
1484 // a=ssrc:<ssrc-id> msid:identifier [appdata]
1485 // The appdata consists of the "id" attribute of a MediaStreamTrack, which
1486 // is corresponding to the "name" attribute of StreamParams.
1487 std::string appdata = track->id;
1488 std::ostringstream os;
1489 InitAttrLine(kAttributeSsrc, &os);
1490 os << kSdpDelimiterColon << ssrc << kSdpDelimiterSpace
1491 << kSsrcAttributeMsid << kSdpDelimiterColon << track->sync_label
1492 << kSdpDelimiterSpace << appdata;
1493 AddLine(os.str(), message);
1494
1495 // TODO(ronghuawu): Remove below code which is for backward compatibility.
1496 // draft-alvestrand-rtcweb-mid-01
1497 // a=ssrc:<ssrc-id> mslabel:<value>
1498 // The label isn't yet defined.
1499 // a=ssrc:<ssrc-id> label:<value>
1500 AddSsrcLine(ssrc, kSsrcAttributeMslabel, track->sync_label, message);
1501 AddSsrcLine(ssrc, kSSrcAttributeLabel, track->id, message);
1502 }
1503 }
1504}
1505
1506void WriteFmtpHeader(int payload_type, std::ostringstream* os) {
1507 // fmtp header: a=fmtp:|payload_type| <parameters>
1508 // Add a=fmtp
1509 InitAttrLine(kAttributeFmtp, os);
1510 // Add :|payload_type|
1511 *os << kSdpDelimiterColon << payload_type;
1512}
1513
1514void WriteRtcpFbHeader(int payload_type, std::ostringstream* os) {
1515 // rtcp-fb header: a=rtcp-fb:|payload_type|
1516 // <parameters>/<ccm <ccm_parameters>>
1517 // Add a=rtcp-fb
1518 InitAttrLine(kAttributeRtcpFb, os);
1519 // Add :
1520 *os << kSdpDelimiterColon;
1521 if (payload_type == kWildcardPayloadType) {
1522 *os << "*";
1523 } else {
1524 *os << payload_type;
1525 }
1526}
1527
1528void WriteFmtpParameter(const std::string& parameter_name,
1529 const std::string& parameter_value,
1530 std::ostringstream* os) {
1531 // fmtp parameters: |parameter_name|=|parameter_value|
1532 *os << parameter_name << kSdpDelimiterEqual << parameter_value;
1533}
1534
1535void WriteFmtpParameters(const cricket::CodecParameterMap& parameters,
1536 std::ostringstream* os) {
1537 for (cricket::CodecParameterMap::const_iterator fmtp = parameters.begin();
1538 fmtp != parameters.end(); ++fmtp) {
1539 // Each new parameter, except the first one starts with ";" and " ".
1540 if (fmtp != parameters.begin()) {
1541 *os << kSdpDelimiterSemicolon;
1542 }
1543 *os << kSdpDelimiterSpace;
1544 WriteFmtpParameter(fmtp->first, fmtp->second, os);
1545 }
1546}
1547
1548bool IsFmtpParam(const std::string& name) {
1549 const char* kFmtpParams[] = {
1550 kCodecParamMinPTime, kCodecParamSPropStereo,
buildbot@webrtc.orged97bb02014-05-07 11:15:20 +00001551 kCodecParamStereo, kCodecParamUseInbandFec, kCodecParamStartBitrate,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001552 kCodecParamMaxBitrate, kCodecParamMinBitrate, kCodecParamMaxQuantization,
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001553 kCodecParamSctpProtocol, kCodecParamSctpStreams,
buildbot@webrtc.org5d639b32014-09-10 07:57:12 +00001554 kCodecParamMaxAverageBitrate, kCodecParamMaxPlaybackRate,
1555 kCodecParamAssociatedPayloadType
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001556 };
1557 for (size_t i = 0; i < ARRAY_SIZE(kFmtpParams); ++i) {
1558 if (_stricmp(name.c_str(), kFmtpParams[i]) == 0) {
1559 return true;
1560 }
1561 }
1562 return false;
1563}
1564
1565// Retreives fmtp parameters from |params|, which may contain other parameters
1566// as well, and puts them in |fmtp_parameters|.
1567void GetFmtpParams(const cricket::CodecParameterMap& params,
1568 cricket::CodecParameterMap* fmtp_parameters) {
1569 for (cricket::CodecParameterMap::const_iterator iter = params.begin();
1570 iter != params.end(); ++iter) {
1571 if (IsFmtpParam(iter->first)) {
1572 (*fmtp_parameters)[iter->first] = iter->second;
1573 }
1574 }
1575}
1576
1577template <class T>
1578void AddFmtpLine(const T& codec, std::string* message) {
1579 cricket::CodecParameterMap fmtp_parameters;
1580 GetFmtpParams(codec.params, &fmtp_parameters);
1581 if (fmtp_parameters.empty()) {
1582 // No need to add an fmtp if it will have no (optional) parameters.
1583 return;
1584 }
1585 std::ostringstream os;
1586 WriteFmtpHeader(codec.id, &os);
1587 WriteFmtpParameters(fmtp_parameters, &os);
1588 AddLine(os.str(), message);
1589 return;
1590}
1591
1592template <class T>
1593void AddRtcpFbLines(const T& codec, std::string* message) {
1594 for (std::vector<cricket::FeedbackParam>::const_iterator iter =
1595 codec.feedback_params.params().begin();
1596 iter != codec.feedback_params.params().end(); ++iter) {
1597 std::ostringstream os;
1598 WriteRtcpFbHeader(codec.id, &os);
1599 os << " " << iter->id();
1600 if (!iter->param().empty()) {
1601 os << " " << iter->param();
1602 }
1603 AddLine(os.str(), message);
1604 }
1605}
1606
jiayl@webrtc.orgddb85ab2014-09-05 16:31:56 +00001607bool AddSctpDataCodec(DataContentDescription* media_desc,
1608 int sctp_port) {
1609 if (media_desc->HasCodec(cricket::kGoogleSctpDataCodecId)) {
1610 return ParseFailed("",
1611 "Can't have multiple sctp port attributes.",
1612 NULL);
1613 }
1614 // Add the SCTP Port number as a pseudo-codec "port" parameter
1615 cricket::DataCodec codec_port(
1616 cricket::kGoogleSctpDataCodecId, cricket::kGoogleSctpDataCodecName,
1617 0);
1618 codec_port.SetParam(cricket::kCodecParamPort, sctp_port);
1619 LOG(INFO) << "AddSctpDataCodec: Got SCTP Port Number "
1620 << sctp_port;
1621 media_desc->AddCodec(codec_port);
1622 return true;
1623}
1624
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001625bool GetMinValue(const std::vector<int>& values, int* value) {
1626 if (values.empty()) {
1627 return false;
1628 }
1629 std::vector<int>::const_iterator found =
1630 std::min_element(values.begin(), values.end());
1631 *value = *found;
1632 return true;
1633}
1634
1635bool GetParameter(const std::string& name,
1636 const cricket::CodecParameterMap& params, int* value) {
1637 std::map<std::string, std::string>::const_iterator found =
1638 params.find(name);
1639 if (found == params.end()) {
1640 return false;
1641 }
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001642 if (!rtc::FromString(found->second, value)) {
wu@webrtc.org5e760e72014-04-02 23:19:09 +00001643 return false;
1644 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001645 return true;
1646}
1647
1648void BuildRtpMap(const MediaContentDescription* media_desc,
1649 const MediaType media_type,
1650 std::string* message) {
1651 ASSERT(message != NULL);
1652 ASSERT(media_desc != NULL);
1653 std::ostringstream os;
1654 if (media_type == cricket::MEDIA_TYPE_VIDEO) {
1655 const VideoContentDescription* video_desc =
1656 static_cast<const VideoContentDescription*>(media_desc);
1657 for (std::vector<cricket::VideoCodec>::const_iterator it =
1658 video_desc->codecs().begin();
1659 it != video_desc->codecs().end(); ++it) {
1660 // RFC 4566
1661 // a=rtpmap:<payload type> <encoding name>/<clock rate>
1662 // [/<encodingparameters>]
1663 if (it->id != kWildcardPayloadType) {
1664 InitAttrLine(kAttributeRtpmap, &os);
1665 os << kSdpDelimiterColon << it->id << " " << it->name
1666 << "/" << kDefaultVideoClockrate;
1667 AddLine(os.str(), message);
1668 }
1669 AddRtcpFbLines(*it, message);
1670 AddFmtpLine(*it, message);
1671 }
1672 } else if (media_type == cricket::MEDIA_TYPE_AUDIO) {
1673 const AudioContentDescription* audio_desc =
1674 static_cast<const AudioContentDescription*>(media_desc);
1675 std::vector<int> ptimes;
1676 std::vector<int> maxptimes;
1677 int max_minptime = 0;
1678 for (std::vector<cricket::AudioCodec>::const_iterator it =
1679 audio_desc->codecs().begin();
1680 it != audio_desc->codecs().end(); ++it) {
1681 ASSERT(!it->name.empty());
1682 // RFC 4566
1683 // a=rtpmap:<payload type> <encoding name>/<clock rate>
1684 // [/<encodingparameters>]
1685 InitAttrLine(kAttributeRtpmap, &os);
1686 os << kSdpDelimiterColon << it->id << " ";
1687 os << it->name << "/" << it->clockrate;
1688 if (it->channels != 1) {
1689 os << "/" << it->channels;
1690 }
1691 AddLine(os.str(), message);
1692 AddRtcpFbLines(*it, message);
1693 AddFmtpLine(*it, message);
1694 int minptime = 0;
1695 if (GetParameter(kCodecParamMinPTime, it->params, &minptime)) {
1696 max_minptime = std::max(minptime, max_minptime);
1697 }
1698 int ptime;
1699 if (GetParameter(kCodecParamPTime, it->params, &ptime)) {
1700 ptimes.push_back(ptime);
1701 }
1702 int maxptime;
1703 if (GetParameter(kCodecParamMaxPTime, it->params, &maxptime)) {
1704 maxptimes.push_back(maxptime);
1705 }
1706 }
1707 // Populate the maxptime attribute with the smallest maxptime of all codecs
1708 // under the same m-line.
1709 int min_maxptime = INT_MAX;
1710 if (GetMinValue(maxptimes, &min_maxptime)) {
1711 AddAttributeLine(kCodecParamMaxPTime, min_maxptime, message);
1712 }
1713 ASSERT(min_maxptime > max_minptime);
1714 // Populate the ptime attribute with the smallest ptime or the largest
1715 // minptime, whichever is the largest, for all codecs under the same m-line.
1716 int ptime = INT_MAX;
1717 if (GetMinValue(ptimes, &ptime)) {
1718 ptime = std::min(ptime, min_maxptime);
1719 ptime = std::max(ptime, max_minptime);
1720 AddAttributeLine(kCodecParamPTime, ptime, message);
1721 }
1722 } else if (media_type == cricket::MEDIA_TYPE_DATA) {
1723 const DataContentDescription* data_desc =
1724 static_cast<const DataContentDescription*>(media_desc);
1725 for (std::vector<cricket::DataCodec>::const_iterator it =
1726 data_desc->codecs().begin();
1727 it != data_desc->codecs().end(); ++it) {
1728 // RFC 4566
1729 // a=rtpmap:<payload type> <encoding name>/<clock rate>
1730 // [/<encodingparameters>]
1731 InitAttrLine(kAttributeRtpmap, &os);
1732 os << kSdpDelimiterColon << it->id << " "
1733 << it->name << "/" << it->clockrate;
1734 AddLine(os.str(), message);
1735 }
1736 }
1737}
1738
1739void BuildCandidate(const std::vector<Candidate>& candidates,
1740 std::string* message) {
1741 std::ostringstream os;
1742
1743 for (std::vector<Candidate>::const_iterator it = candidates.begin();
1744 it != candidates.end(); ++it) {
1745 // RFC 5245
1746 // a=candidate:<foundation> <component-id> <transport> <priority>
1747 // <connection-address> <port> typ <candidate-types>
1748 // [raddr <connection-address>] [rport <port>]
1749 // *(SP extension-att-name SP extension-att-value)
1750 std::string type;
1751 // Map the cricket candidate type to "host" / "srflx" / "prflx" / "relay"
1752 if (it->type() == cricket::LOCAL_PORT_TYPE) {
1753 type = kCandidateHost;
1754 } else if (it->type() == cricket::STUN_PORT_TYPE) {
1755 type = kCandidateSrflx;
1756 } else if (it->type() == cricket::RELAY_PORT_TYPE) {
1757 type = kCandidateRelay;
1758 } else {
1759 ASSERT(false);
1760 }
1761
1762 InitAttrLine(kAttributeCandidate, &os);
1763 os << kSdpDelimiterColon
mallinath@webrtc.org2d60c5e2014-08-08 22:29:20 +00001764 << it->foundation() << " "
1765 << it->component() << " "
1766 << it->protocol() << " "
1767 << it->priority() << " "
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001768 << it->address().ipaddr().ToString() << " "
1769 << it->address().PortAsString() << " "
mallinath@webrtc.org2d60c5e2014-08-08 22:29:20 +00001770 << kAttributeCandidateTyp << " "
1771 << type << " ";
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001772
1773 // Related address
1774 if (!it->related_address().IsNil()) {
1775 os << kAttributeCandidateRaddr << " "
1776 << it->related_address().ipaddr().ToString() << " "
1777 << kAttributeCandidateRport << " "
1778 << it->related_address().PortAsString() << " ";
1779 }
1780
mallinath@webrtc.org2d60c5e2014-08-08 22:29:20 +00001781 if (it->protocol() == cricket::TCP_PROTOCOL_NAME) {
mallinath@webrtc.orge999bd02014-08-13 06:05:55 +00001782 os << kTcpCandidateType << " " << it->tcptype() << " ";
mallinath@webrtc.org2d60c5e2014-08-08 22:29:20 +00001783 }
1784
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001785 // Extensions
1786 os << kAttributeCandidateGeneration << " " << it->generation();
1787
1788 AddLine(os.str(), message);
1789 }
1790}
1791
1792void BuildIceOptions(const std::vector<std::string>& transport_options,
1793 std::string* message) {
1794 if (!transport_options.empty()) {
1795 std::ostringstream os;
1796 InitAttrLine(kAttributeIceOption, &os);
1797 os << kSdpDelimiterColon << transport_options[0];
1798 for (size_t i = 1; i < transport_options.size(); ++i) {
1799 os << kSdpDelimiterSpace << transport_options[i];
1800 }
1801 AddLine(os.str(), message);
1802 }
1803}
1804
pthatcher@webrtc.org3341b402015-02-13 21:14:22 +00001805bool IsRtp(const std::string& protocol) {
1806 return protocol.empty() ||
1807 (protocol.find(cricket::kMediaProtocolRtpPrefix) != std::string::npos);
1808}
1809
1810bool IsDtlsSctp(const std::string& protocol) {
1811 // This intentionally excludes "SCTP" and "SCTP/DTLS".
1812 return protocol.find(cricket::kMediaProtocolDtlsSctp) != std::string::npos;
1813}
1814
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001815bool ParseSessionDescription(const std::string& message, size_t* pos,
1816 std::string* session_id,
1817 std::string* session_version,
1818 bool* supports_msid,
1819 TransportDescription* session_td,
1820 RtpHeaderExtensions* session_extmaps,
1821 cricket::SessionDescription* desc,
1822 SdpParseError* error) {
1823 std::string line;
1824
1825 // RFC 4566
1826 // v= (protocol version)
1827 if (!GetLineWithType(message, pos, &line, kLineTypeVersion)) {
1828 return ParseFailedExpectLine(message, *pos, kLineTypeVersion,
1829 std::string(), error);
1830 }
1831 // RFC 4566
1832 // o=<username> <sess-id> <sess-version> <nettype> <addrtype>
1833 // <unicast-address>
1834 if (!GetLineWithType(message, pos, &line, kLineTypeOrigin)) {
1835 return ParseFailedExpectLine(message, *pos, kLineTypeOrigin,
1836 std::string(), error);
1837 }
1838 std::vector<std::string> fields;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001839 rtc::split(line.substr(kLinePrefixLength),
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001840 kSdpDelimiterSpace, &fields);
1841 const size_t expected_fields = 6;
1842 if (fields.size() != expected_fields) {
1843 return ParseFailedExpectFieldNum(line, expected_fields, error);
1844 }
1845 *session_id = fields[1];
1846 *session_version = fields[2];
1847
1848 // RFC 4566
1849 // s= (session name)
1850 if (!GetLineWithType(message, pos, &line, kLineTypeSessionName)) {
1851 return ParseFailedExpectLine(message, *pos, kLineTypeSessionName,
1852 std::string(), error);
1853 }
1854
1855 // Optional lines
1856 // Those are the optional lines, so shouldn't return false if not present.
1857 // RFC 4566
1858 // i=* (session information)
1859 GetLineWithType(message, pos, &line, kLineTypeSessionInfo);
1860
1861 // RFC 4566
1862 // u=* (URI of description)
1863 GetLineWithType(message, pos, &line, kLineTypeSessionUri);
1864
1865 // RFC 4566
1866 // e=* (email address)
1867 GetLineWithType(message, pos, &line, kLineTypeSessionEmail);
1868
1869 // RFC 4566
1870 // p=* (phone number)
1871 GetLineWithType(message, pos, &line, kLineTypeSessionPhone);
1872
1873 // RFC 4566
1874 // c=* (connection information -- not required if included in
1875 // all media)
1876 GetLineWithType(message, pos, &line, kLineTypeConnection);
1877
1878 // RFC 4566
1879 // b=* (zero or more bandwidth information lines)
1880 while (GetLineWithType(message, pos, &line, kLineTypeSessionBandwidth)) {
1881 // By pass zero or more b lines.
1882 }
1883
1884 // RFC 4566
1885 // One or more time descriptions ("t=" and "r=" lines; see below)
1886 // t= (time the session is active)
1887 // r=* (zero or more repeat times)
1888 // Ensure there's at least one time description
1889 if (!GetLineWithType(message, pos, &line, kLineTypeTiming)) {
1890 return ParseFailedExpectLine(message, *pos, kLineTypeTiming, std::string(),
1891 error);
1892 }
1893
1894 while (GetLineWithType(message, pos, &line, kLineTypeRepeatTimes)) {
1895 // By pass zero or more r lines.
1896 }
1897
1898 // Go through the rest of the time descriptions
1899 while (GetLineWithType(message, pos, &line, kLineTypeTiming)) {
1900 while (GetLineWithType(message, pos, &line, kLineTypeRepeatTimes)) {
1901 // By pass zero or more r lines.
1902 }
1903 }
1904
1905 // RFC 4566
1906 // z=* (time zone adjustments)
1907 GetLineWithType(message, pos, &line, kLineTypeTimeZone);
1908
1909 // RFC 4566
1910 // k=* (encryption key)
1911 GetLineWithType(message, pos, &line, kLineTypeEncryptionKey);
1912
1913 // RFC 4566
1914 // a=* (zero or more session attribute lines)
1915 while (GetLineWithType(message, pos, &line, kLineTypeAttributes)) {
1916 if (HasAttribute(line, kAttributeGroup)) {
1917 if (!ParseGroupAttribute(line, desc, error)) {
1918 return false;
1919 }
1920 } else if (HasAttribute(line, kAttributeIceUfrag)) {
1921 if (!GetValue(line, kAttributeIceUfrag,
1922 &(session_td->ice_ufrag), error)) {
1923 return false;
1924 }
1925 } else if (HasAttribute(line, kAttributeIcePwd)) {
1926 if (!GetValue(line, kAttributeIcePwd, &(session_td->ice_pwd), error)) {
1927 return false;
1928 }
1929 } else if (HasAttribute(line, kAttributeIceLite)) {
1930 session_td->ice_mode = cricket::ICEMODE_LITE;
1931 } else if (HasAttribute(line, kAttributeIceOption)) {
1932 if (!ParseIceOptions(line, &(session_td->transport_options), error)) {
1933 return false;
1934 }
1935 } else if (HasAttribute(line, kAttributeFingerprint)) {
1936 if (session_td->identity_fingerprint.get()) {
1937 return ParseFailed(
1938 line,
1939 "Can't have multiple fingerprint attributes at the same level.",
1940 error);
1941 }
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001942 rtc::SSLFingerprint* fingerprint = NULL;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001943 if (!ParseFingerprintAttribute(line, &fingerprint, error)) {
1944 return false;
1945 }
1946 session_td->identity_fingerprint.reset(fingerprint);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001947 } else if (HasAttribute(line, kAttributeSetup)) {
1948 if (!ParseDtlsSetup(line, &(session_td->connection_role), error)) {
1949 return false;
1950 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001951 } else if (HasAttribute(line, kAttributeMsidSemantics)) {
1952 std::string semantics;
1953 if (!GetValue(line, kAttributeMsidSemantics, &semantics, error)) {
1954 return false;
1955 }
1956 *supports_msid = CaseInsensitiveFind(semantics, kMediaStreamSemantic);
1957 } else if (HasAttribute(line, kAttributeExtmap)) {
1958 RtpHeaderExtension extmap;
1959 if (!ParseExtmap(line, &extmap, error)) {
1960 return false;
1961 }
1962 session_extmaps->push_back(extmap);
1963 }
1964 }
1965
1966 return true;
1967}
1968
1969bool ParseGroupAttribute(const std::string& line,
1970 cricket::SessionDescription* desc,
1971 SdpParseError* error) {
1972 ASSERT(desc != NULL);
1973
1974 // RFC 5888 and draft-holmberg-mmusic-sdp-bundle-negotiation-00
1975 // a=group:BUNDLE video voice
1976 std::vector<std::string> fields;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001977 rtc::split(line.substr(kLinePrefixLength),
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001978 kSdpDelimiterSpace, &fields);
1979 std::string semantics;
1980 if (!GetValue(fields[0], kAttributeGroup, &semantics, error)) {
1981 return false;
1982 }
1983 cricket::ContentGroup group(semantics);
1984 for (size_t i = 1; i < fields.size(); ++i) {
1985 group.AddContentName(fields[i]);
1986 }
1987 desc->AddGroup(group);
1988 return true;
1989}
1990
1991static bool ParseFingerprintAttribute(const std::string& line,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001992 rtc::SSLFingerprint** fingerprint,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001993 SdpParseError* error) {
1994 if (!IsLineType(line, kLineTypeAttributes) ||
1995 !HasAttribute(line, kAttributeFingerprint)) {
1996 return ParseFailedExpectLine(line, 0, kLineTypeAttributes,
1997 kAttributeFingerprint, error);
1998 }
1999
2000 std::vector<std::string> fields;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002001 rtc::split(line.substr(kLinePrefixLength),
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002002 kSdpDelimiterSpace, &fields);
2003 const size_t expected_fields = 2;
2004 if (fields.size() != expected_fields) {
2005 return ParseFailedExpectFieldNum(line, expected_fields, error);
2006 }
2007
2008 // The first field here is "fingerprint:<hash>.
2009 std::string algorithm;
2010 if (!GetValue(fields[0], kAttributeFingerprint, &algorithm, error)) {
2011 return false;
2012 }
2013
2014 // Downcase the algorithm. Note that we don't need to downcase the
2015 // fingerprint because hex_decode can handle upper-case.
2016 std::transform(algorithm.begin(), algorithm.end(), algorithm.begin(),
2017 ::tolower);
2018
2019 // The second field is the digest value. De-hexify it.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002020 *fingerprint = rtc::SSLFingerprint::CreateFromRfc4572(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002021 algorithm, fields[1]);
2022 if (!*fingerprint) {
2023 return ParseFailed(line,
2024 "Failed to create fingerprint from the digest.",
2025 error);
2026 }
2027
2028 return true;
2029}
2030
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00002031static bool ParseDtlsSetup(const std::string& line,
2032 cricket::ConnectionRole* role,
2033 SdpParseError* error) {
2034 // setup-attr = "a=setup:" role
2035 // role = "active" / "passive" / "actpass" / "holdconn"
2036 std::vector<std::string> fields;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002037 rtc::split(line.substr(kLinePrefixLength), kSdpDelimiterColon, &fields);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00002038 const size_t expected_fields = 2;
2039 if (fields.size() != expected_fields) {
2040 return ParseFailedExpectFieldNum(line, expected_fields, error);
2041 }
2042 std::string role_str = fields[1];
2043 if (!cricket::StringToConnectionRole(role_str, role)) {
2044 return ParseFailed(line, "Invalid attribute value.", error);
2045 }
2046 return true;
2047}
2048
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002049// RFC 3551
2050// PT encoding media type clock rate channels
2051// name (Hz)
2052// 0 PCMU A 8,000 1
2053// 1 reserved A
2054// 2 reserved A
2055// 3 GSM A 8,000 1
2056// 4 G723 A 8,000 1
2057// 5 DVI4 A 8,000 1
2058// 6 DVI4 A 16,000 1
2059// 7 LPC A 8,000 1
2060// 8 PCMA A 8,000 1
2061// 9 G722 A 8,000 1
2062// 10 L16 A 44,100 2
2063// 11 L16 A 44,100 1
2064// 12 QCELP A 8,000 1
2065// 13 CN A 8,000 1
2066// 14 MPA A 90,000 (see text)
2067// 15 G728 A 8,000 1
2068// 16 DVI4 A 11,025 1
2069// 17 DVI4 A 22,050 1
2070// 18 G729 A 8,000 1
2071struct StaticPayloadAudioCodec {
2072 const char* name;
2073 int clockrate;
2074 int channels;
2075};
2076static const StaticPayloadAudioCodec kStaticPayloadAudioCodecs[] = {
2077 { "PCMU", 8000, 1 },
2078 { "reserved", 0, 0 },
2079 { "reserved", 0, 0 },
2080 { "GSM", 8000, 1 },
2081 { "G723", 8000, 1 },
2082 { "DVI4", 8000, 1 },
2083 { "DVI4", 16000, 1 },
2084 { "LPC", 8000, 1 },
2085 { "PCMA", 8000, 1 },
2086 { "G722", 8000, 1 },
2087 { "L16", 44100, 2 },
2088 { "L16", 44100, 1 },
2089 { "QCELP", 8000, 1 },
2090 { "CN", 8000, 1 },
2091 { "MPA", 90000, 1 },
2092 { "G728", 8000, 1 },
2093 { "DVI4", 11025, 1 },
2094 { "DVI4", 22050, 1 },
2095 { "G729", 8000, 1 },
2096};
2097
2098void MaybeCreateStaticPayloadAudioCodecs(
2099 const std::vector<int>& fmts, AudioContentDescription* media_desc) {
2100 if (!media_desc) {
2101 return;
2102 }
henrike@webrtc.org28654cb2013-07-22 21:07:49 +00002103 int preference = static_cast<int>(fmts.size());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002104 std::vector<int>::const_iterator it = fmts.begin();
2105 bool add_new_codec = false;
2106 for (; it != fmts.end(); ++it) {
2107 int payload_type = *it;
2108 if (!media_desc->HasCodec(payload_type) &&
2109 payload_type >= 0 &&
2110 payload_type < ARRAY_SIZE(kStaticPayloadAudioCodecs)) {
2111 std::string encoding_name = kStaticPayloadAudioCodecs[payload_type].name;
2112 int clock_rate = kStaticPayloadAudioCodecs[payload_type].clockrate;
2113 int channels = kStaticPayloadAudioCodecs[payload_type].channels;
2114 media_desc->AddCodec(cricket::AudioCodec(payload_type, encoding_name,
2115 clock_rate, 0, channels,
2116 preference));
2117 add_new_codec = true;
2118 }
2119 --preference;
2120 }
2121 if (add_new_codec) {
2122 media_desc->SortCodecs();
2123 }
2124}
2125
2126template <class C>
2127static C* ParseContentDescription(const std::string& message,
2128 const MediaType media_type,
2129 int mline_index,
2130 const std::string& protocol,
2131 const std::vector<int>& codec_preference,
2132 size_t* pos,
2133 std::string* content_name,
2134 TransportDescription* transport,
2135 std::vector<JsepIceCandidate*>* candidates,
2136 webrtc::SdpParseError* error) {
2137 C* media_desc = new C();
2138 switch (media_type) {
2139 case cricket::MEDIA_TYPE_AUDIO:
2140 *content_name = cricket::CN_AUDIO;
2141 break;
2142 case cricket::MEDIA_TYPE_VIDEO:
2143 *content_name = cricket::CN_VIDEO;
2144 break;
2145 case cricket::MEDIA_TYPE_DATA:
2146 *content_name = cricket::CN_DATA;
2147 break;
2148 default:
2149 ASSERT(false);
2150 break;
2151 }
2152 if (!ParseContent(message, media_type, mline_index, protocol,
2153 codec_preference, pos, content_name,
2154 media_desc, transport, candidates, error)) {
2155 delete media_desc;
2156 return NULL;
2157 }
2158 // Sort the codecs according to the m-line fmt list.
2159 media_desc->SortCodecs();
2160 return media_desc;
2161}
2162
2163bool ParseMediaDescription(const std::string& message,
2164 const TransportDescription& session_td,
2165 const RtpHeaderExtensions& session_extmaps,
2166 bool supports_msid,
2167 size_t* pos,
2168 cricket::SessionDescription* desc,
2169 std::vector<JsepIceCandidate*>* candidates,
2170 SdpParseError* error) {
2171 ASSERT(desc != NULL);
2172 std::string line;
2173 int mline_index = -1;
2174
2175 // Zero or more media descriptions
2176 // RFC 4566
2177 // m=<media> <port> <proto> <fmt>
2178 while (GetLineWithType(message, pos, &line, kLineTypeMedia)) {
2179 ++mline_index;
2180
2181 std::vector<std::string> fields;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002182 rtc::split(line.substr(kLinePrefixLength),
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002183 kSdpDelimiterSpace, &fields);
2184 const size_t expected_min_fields = 4;
2185 if (fields.size() < expected_min_fields) {
2186 return ParseFailedExpectMinFieldNum(line, expected_min_fields, error);
2187 }
2188 bool rejected = false;
2189 // RFC 3264
2190 // To reject an offered stream, the port number in the corresponding stream
2191 // in the answer MUST be set to zero.
2192 if (fields[1] == kMediaPortRejected) {
2193 rejected = true;
2194 }
2195
2196 std::string protocol = fields[2];
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002197
2198 // <fmt>
2199 std::vector<int> codec_preference;
pthatcher@webrtc.org3341b402015-02-13 21:14:22 +00002200 if (IsRtp(protocol)) {
jiayl@webrtc.orgddb85ab2014-09-05 16:31:56 +00002201 for (size_t j = 3 ; j < fields.size(); ++j) {
2202 // TODO(wu): Remove when below bug is fixed.
2203 // https://bugzilla.mozilla.org/show_bug.cgi?id=996329
2204 if (fields[j] == "" && j == fields.size() - 1) {
2205 continue;
2206 }
wu@webrtc.org36eda7c2014-04-15 20:37:30 +00002207
jiayl@webrtc.orgddb85ab2014-09-05 16:31:56 +00002208 int pl = 0;
2209 if (!GetValueFromString(line, fields[j], &pl, error)) {
2210 return false;
2211 }
2212 codec_preference.push_back(pl);
wu@webrtc.org5e760e72014-04-02 23:19:09 +00002213 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002214 }
2215
2216 // Make a temporary TransportDescription based on |session_td|.
2217 // Some of this gets overwritten by ParseContent.
2218 TransportDescription transport(NS_JINGLE_ICE_UDP,
2219 session_td.transport_options,
2220 session_td.ice_ufrag,
2221 session_td.ice_pwd,
2222 session_td.ice_mode,
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00002223 session_td.connection_role,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002224 session_td.identity_fingerprint.get(),
2225 Candidates());
2226
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002227 rtc::scoped_ptr<MediaContentDescription> content;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002228 std::string content_name;
2229 if (HasAttribute(line, kMediaTypeVideo)) {
2230 content.reset(ParseContentDescription<VideoContentDescription>(
2231 message, cricket::MEDIA_TYPE_VIDEO, mline_index, protocol,
2232 codec_preference, pos, &content_name,
2233 &transport, candidates, error));
2234 } else if (HasAttribute(line, kMediaTypeAudio)) {
2235 content.reset(ParseContentDescription<AudioContentDescription>(
2236 message, cricket::MEDIA_TYPE_AUDIO, mline_index, protocol,
2237 codec_preference, pos, &content_name,
2238 &transport, candidates, error));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002239 } else if (HasAttribute(line, kMediaTypeData)) {
jiayl@webrtc.org0e527722014-09-08 21:43:43 +00002240 DataContentDescription* data_desc =
wu@webrtc.org78187522013-10-07 23:32:02 +00002241 ParseContentDescription<DataContentDescription>(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002242 message, cricket::MEDIA_TYPE_DATA, mline_index, protocol,
2243 codec_preference, pos, &content_name,
wu@webrtc.org78187522013-10-07 23:32:02 +00002244 &transport, candidates, error);
jiayl@webrtc.org0e527722014-09-08 21:43:43 +00002245 content.reset(data_desc);
wu@webrtc.org78187522013-10-07 23:32:02 +00002246
jiayl@webrtc.orgddb85ab2014-09-05 16:31:56 +00002247 int p;
pthatcher@webrtc.org3341b402015-02-13 21:14:22 +00002248 if (data_desc && IsDtlsSctp(protocol) && rtc::FromString(fields[3], &p)) {
jiayl@webrtc.org0e527722014-09-08 21:43:43 +00002249 if (!AddSctpDataCodec(data_desc, p))
jiayl@webrtc.orgddb85ab2014-09-05 16:31:56 +00002250 return false;
wu@webrtc.org78187522013-10-07 23:32:02 +00002251 }
2252
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00002253 // We should always use the default bandwidth for RTP-based data
2254 // channels. Don't allow SDP to set the bandwidth, because that
2255 // would give JS the opportunity to "break the Internet".
sergeyu@chromium.orga59696b2013-09-13 23:48:58 +00002256 // TODO(pthatcher): But we need to temporarily allow the SDP to control
2257 // this for backwards-compatibility. Once we don't need that any
2258 // more, remove this.
2259 bool support_dc_sdp_bandwidth_temporarily = true;
wu@webrtc.org967bfff2013-09-19 05:49:50 +00002260 if (content.get() && !support_dc_sdp_bandwidth_temporarily) {
sergeyu@chromium.orga59696b2013-09-13 23:48:58 +00002261 content->set_bandwidth(cricket::kAutoBandwidth);
2262 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002263 } else {
2264 LOG(LS_WARNING) << "Unsupported media type: " << line;
2265 continue;
2266 }
2267 if (!content.get()) {
2268 // ParseContentDescription returns NULL if failed.
2269 return false;
2270 }
2271
pthatcher@webrtc.org3341b402015-02-13 21:14:22 +00002272 if (IsRtp(protocol)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002273 // Make sure to set the media direction correctly. If the direction is not
2274 // MD_RECVONLY or Inactive and no streams are parsed,
2275 // a default MediaStream will be created to prepare for receiving media.
2276 if (supports_msid && content->streams().empty() &&
2277 content->direction() == cricket::MD_SENDRECV) {
2278 content->set_direction(cricket::MD_RECVONLY);
2279 }
2280
2281 // Set the extmap.
2282 if (!session_extmaps.empty() &&
2283 !content->rtp_header_extensions().empty()) {
2284 return ParseFailed("",
2285 "The a=extmap MUST be either all session level or "
2286 "all media level.",
2287 error);
2288 }
2289 for (size_t i = 0; i < session_extmaps.size(); ++i) {
2290 content->AddRtpHeaderExtension(session_extmaps[i]);
2291 }
2292 }
2293 content->set_protocol(protocol);
2294 desc->AddContent(content_name,
pthatcher@webrtc.org3341b402015-02-13 21:14:22 +00002295 IsDtlsSctp(protocol) ? cricket::NS_JINGLE_DRAFT_SCTP :
2296 cricket::NS_JINGLE_RTP,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002297 rejected,
2298 content.release());
2299 // Create TransportInfo with the media level "ice-pwd" and "ice-ufrag".
2300 TransportInfo transport_info(content_name, transport);
2301
2302 if (!desc->AddTransportInfo(transport_info)) {
2303 std::ostringstream description;
2304 description << "Failed to AddTransportInfo with content name: "
2305 << content_name;
2306 return ParseFailed("", description.str(), error);
2307 }
2308 }
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00002309
2310 size_t end_of_message = message.size();
2311 if (mline_index == -1 && *pos != end_of_message) {
2312 ParseFailed(message, *pos, "Expects m line.", error);
2313 return false;
2314 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002315 return true;
2316}
2317
2318bool VerifyCodec(const cricket::Codec& codec) {
2319 // Codec has not been populated correctly unless the name has been set. This
2320 // can happen if an SDP has an fmtp or rtcp-fb with a payload type but doesn't
2321 // have a corresponding "rtpmap" line.
2322 cricket::Codec default_codec;
2323 return default_codec.name != codec.name;
2324}
2325
2326bool VerifyAudioCodecs(const AudioContentDescription* audio_desc) {
2327 const std::vector<cricket::AudioCodec>& codecs = audio_desc->codecs();
2328 for (std::vector<cricket::AudioCodec>::const_iterator iter = codecs.begin();
2329 iter != codecs.end(); ++iter) {
2330 if (!VerifyCodec(*iter)) {
2331 return false;
2332 }
2333 }
2334 return true;
2335}
2336
2337bool VerifyVideoCodecs(const VideoContentDescription* video_desc) {
2338 const std::vector<cricket::VideoCodec>& codecs = video_desc->codecs();
2339 for (std::vector<cricket::VideoCodec>::const_iterator iter = codecs.begin();
2340 iter != codecs.end(); ++iter) {
2341 if (!VerifyCodec(*iter)) {
2342 return false;
2343 }
2344 }
2345 return true;
2346}
2347
2348void AddParameters(const cricket::CodecParameterMap& parameters,
2349 cricket::Codec* codec) {
2350 for (cricket::CodecParameterMap::const_iterator iter =
2351 parameters.begin(); iter != parameters.end(); ++iter) {
2352 codec->SetParam(iter->first, iter->second);
2353 }
2354}
2355
2356void AddFeedbackParameter(const cricket::FeedbackParam& feedback_param,
2357 cricket::Codec* codec) {
2358 codec->AddFeedbackParam(feedback_param);
2359}
2360
2361void AddFeedbackParameters(const cricket::FeedbackParams& feedback_params,
2362 cricket::Codec* codec) {
2363 for (std::vector<cricket::FeedbackParam>::const_iterator iter =
2364 feedback_params.params().begin();
2365 iter != feedback_params.params().end(); ++iter) {
2366 codec->AddFeedbackParam(*iter);
2367 }
2368}
2369
2370// Gets the current codec setting associated with |payload_type|. If there
2371// is no AudioCodec associated with that payload type it returns an empty codec
2372// with that payload type.
2373template <class T>
2374T GetCodec(const std::vector<T>& codecs, int payload_type) {
2375 for (typename std::vector<T>::const_iterator codec = codecs.begin();
2376 codec != codecs.end(); ++codec) {
2377 if (codec->id == payload_type) {
2378 return *codec;
2379 }
2380 }
2381 T ret_val = T();
2382 ret_val.id = payload_type;
2383 return ret_val;
2384}
2385
2386// Updates or creates a new codec entry in the audio description.
2387template <class T, class U>
2388void AddOrReplaceCodec(MediaContentDescription* content_desc, const U& codec) {
2389 T* desc = static_cast<T*>(content_desc);
2390 std::vector<U> codecs = desc->codecs();
2391 bool found = false;
2392
2393 typename std::vector<U>::iterator iter;
2394 for (iter = codecs.begin(); iter != codecs.end(); ++iter) {
2395 if (iter->id == codec.id) {
2396 *iter = codec;
2397 found = true;
2398 break;
2399 }
2400 }
2401 if (!found) {
2402 desc->AddCodec(codec);
2403 return;
2404 }
2405 desc->set_codecs(codecs);
2406}
2407
2408// Adds or updates existing codec corresponding to |payload_type| according
2409// to |parameters|.
2410template <class T, class U>
2411void UpdateCodec(MediaContentDescription* content_desc, int payload_type,
2412 const cricket::CodecParameterMap& parameters) {
2413 // Codec might already have been populated (from rtpmap).
2414 U new_codec = GetCodec(static_cast<T*>(content_desc)->codecs(), payload_type);
2415 AddParameters(parameters, &new_codec);
2416 AddOrReplaceCodec<T, U>(content_desc, new_codec);
2417}
2418
2419// Adds or updates existing codec corresponding to |payload_type| according
2420// to |feedback_param|.
2421template <class T, class U>
2422void UpdateCodec(MediaContentDescription* content_desc, int payload_type,
2423 const cricket::FeedbackParam& feedback_param) {
2424 // Codec might already have been populated (from rtpmap).
2425 U new_codec = GetCodec(static_cast<T*>(content_desc)->codecs(), payload_type);
2426 AddFeedbackParameter(feedback_param, &new_codec);
2427 AddOrReplaceCodec<T, U>(content_desc, new_codec);
2428}
2429
2430bool PopWildcardCodec(std::vector<cricket::VideoCodec>* codecs,
2431 cricket::VideoCodec* wildcard_codec) {
2432 for (std::vector<cricket::VideoCodec>::iterator iter = codecs->begin();
2433 iter != codecs->end(); ++iter) {
2434 if (iter->id == kWildcardPayloadType) {
2435 *wildcard_codec = *iter;
2436 codecs->erase(iter);
2437 return true;
2438 }
2439 }
2440 return false;
2441}
2442
2443void UpdateFromWildcardVideoCodecs(VideoContentDescription* video_desc) {
2444 std::vector<cricket::VideoCodec> codecs = video_desc->codecs();
2445 cricket::VideoCodec wildcard_codec;
2446 if (!PopWildcardCodec(&codecs, &wildcard_codec)) {
2447 return;
2448 }
2449 for (std::vector<cricket::VideoCodec>::iterator iter = codecs.begin();
2450 iter != codecs.end(); ++iter) {
2451 cricket::VideoCodec& codec = *iter;
2452 AddFeedbackParameters(wildcard_codec.feedback_params, &codec);
2453 }
2454 video_desc->set_codecs(codecs);
2455}
2456
2457void AddAudioAttribute(const std::string& name, const std::string& value,
2458 AudioContentDescription* audio_desc) {
2459 if (value.empty()) {
2460 return;
2461 }
2462 std::vector<cricket::AudioCodec> codecs = audio_desc->codecs();
2463 for (std::vector<cricket::AudioCodec>::iterator iter = codecs.begin();
2464 iter != codecs.end(); ++iter) {
2465 iter->params[name] = value;
2466 }
2467 audio_desc->set_codecs(codecs);
2468}
2469
2470bool ParseContent(const std::string& message,
2471 const MediaType media_type,
2472 int mline_index,
2473 const std::string& protocol,
2474 const std::vector<int>& codec_preference,
2475 size_t* pos,
2476 std::string* content_name,
2477 MediaContentDescription* media_desc,
2478 TransportDescription* transport,
2479 std::vector<JsepIceCandidate*>* candidates,
2480 SdpParseError* error) {
2481 ASSERT(media_desc != NULL);
2482 ASSERT(content_name != NULL);
2483 ASSERT(transport != NULL);
2484
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +00002485 if (media_type == cricket::MEDIA_TYPE_AUDIO) {
2486 MaybeCreateStaticPayloadAudioCodecs(
2487 codec_preference, static_cast<AudioContentDescription*>(media_desc));
2488 }
2489
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002490 // The media level "ice-ufrag" and "ice-pwd".
2491 // The candidates before update the media level "ice-pwd" and "ice-ufrag".
2492 Candidates candidates_orig;
2493 std::string line;
2494 std::string mline_id;
2495 // Tracks created out of the ssrc attributes.
2496 StreamParamsVec tracks;
2497 SsrcInfoVec ssrc_infos;
2498 SsrcGroupVec ssrc_groups;
2499 std::string maxptime_as_string;
2500 std::string ptime_as_string;
2501
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002502 // Loop until the next m line
2503 while (!IsLineType(message, kLineTypeMedia, *pos)) {
2504 if (!GetLine(message, pos, &line)) {
2505 if (*pos >= message.size()) {
2506 break; // Done parsing
2507 } else {
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +00002508 return ParseFailed(message, *pos, "Invalid SDP line.", error);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002509 }
2510 }
2511
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002512 // RFC 4566
2513 // b=* (zero or more bandwidth information lines)
2514 if (IsLineType(line, kLineTypeSessionBandwidth)) {
2515 std::string bandwidth;
2516 if (HasAttribute(line, kApplicationSpecificMaximum)) {
2517 if (!GetValue(line, kApplicationSpecificMaximum, &bandwidth, error)) {
2518 return false;
2519 } else {
wu@webrtc.org5e760e72014-04-02 23:19:09 +00002520 int b = 0;
2521 if (!GetValueFromString(line, bandwidth, &b, error)) {
2522 return false;
2523 }
2524 media_desc->set_bandwidth(b * 1000);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002525 }
2526 }
2527 continue;
2528 }
2529
2530 if (!IsLineType(line, kLineTypeAttributes)) {
2531 // TODO: Handle other lines if needed.
2532 LOG(LS_INFO) << "Ignored line: " << line;
2533 continue;
2534 }
2535
2536 // Handle attributes common to SCTP and RTP.
2537 if (HasAttribute(line, kAttributeMid)) {
2538 // RFC 3388
2539 // mid-attribute = "a=mid:" identification-tag
2540 // identification-tag = token
2541 // Use the mid identification-tag as the content name.
2542 if (!GetValue(line, kAttributeMid, &mline_id, error)) {
2543 return false;
2544 }
2545 *content_name = mline_id;
2546 } else if (HasAttribute(line, kAttributeCandidate)) {
2547 Candidate candidate;
2548 if (!ParseCandidate(line, &candidate, error, false)) {
2549 return false;
2550 }
2551 candidates_orig.push_back(candidate);
2552 } else if (HasAttribute(line, kAttributeIceUfrag)) {
2553 if (!GetValue(line, kAttributeIceUfrag, &transport->ice_ufrag, error)) {
2554 return false;
2555 }
2556 } else if (HasAttribute(line, kAttributeIcePwd)) {
2557 if (!GetValue(line, kAttributeIcePwd, &transport->ice_pwd, error)) {
2558 return false;
2559 }
2560 } else if (HasAttribute(line, kAttributeIceOption)) {
2561 if (!ParseIceOptions(line, &transport->transport_options, error)) {
2562 return false;
2563 }
2564 } else if (HasAttribute(line, kAttributeFmtp)) {
2565 if (!ParseFmtpAttributes(line, media_type, media_desc, error)) {
2566 return false;
2567 }
2568 } else if (HasAttribute(line, kAttributeFingerprint)) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002569 rtc::SSLFingerprint* fingerprint = NULL;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002570
2571 if (!ParseFingerprintAttribute(line, &fingerprint, error)) {
2572 return false;
2573 }
2574 transport->identity_fingerprint.reset(fingerprint);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00002575 } else if (HasAttribute(line, kAttributeSetup)) {
2576 if (!ParseDtlsSetup(line, &(transport->connection_role), error)) {
2577 return false;
2578 }
pthatcher@webrtc.org3341b402015-02-13 21:14:22 +00002579 } else if (IsDtlsSctp(protocol) && HasAttribute(line, kAttributeSctpPort)) {
jiayl@webrtc.orgddb85ab2014-09-05 16:31:56 +00002580 int sctp_port;
2581 if (!ParseSctpPort(line, &sctp_port, error)) {
2582 return false;
2583 }
2584 if (!AddSctpDataCodec(static_cast<DataContentDescription*>(media_desc),
2585 sctp_port)) {
2586 return false;
2587 }
pthatcher@webrtc.org3341b402015-02-13 21:14:22 +00002588 } else if (IsRtp(protocol)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002589 //
2590 // RTP specific attrubtes
2591 //
2592 if (HasAttribute(line, kAttributeRtcpMux)) {
2593 media_desc->set_rtcp_mux(true);
2594 } else if (HasAttribute(line, kAttributeSsrcGroup)) {
2595 if (!ParseSsrcGroupAttribute(line, &ssrc_groups, error)) {
2596 return false;
2597 }
2598 } else if (HasAttribute(line, kAttributeSsrc)) {
2599 if (!ParseSsrcAttribute(line, &ssrc_infos, error)) {
2600 return false;
2601 }
2602 } else if (HasAttribute(line, kAttributeCrypto)) {
2603 if (!ParseCryptoAttribute(line, media_desc, error)) {
2604 return false;
2605 }
2606 } else if (HasAttribute(line, kAttributeRtpmap)) {
2607 if (!ParseRtpmapAttribute(line, media_type, codec_preference,
2608 media_desc, error)) {
2609 return false;
2610 }
2611 } else if (HasAttribute(line, kCodecParamMaxPTime)) {
2612 if (!GetValue(line, kCodecParamMaxPTime, &maxptime_as_string, error)) {
2613 return false;
2614 }
2615 } else if (HasAttribute(line, kAttributeRtcpFb)) {
2616 if (!ParseRtcpFbAttribute(line, media_type, media_desc, error)) {
2617 return false;
2618 }
2619 } else if (HasAttribute(line, kCodecParamPTime)) {
2620 if (!GetValue(line, kCodecParamPTime, &ptime_as_string, error)) {
2621 return false;
2622 }
2623 } else if (HasAttribute(line, kAttributeSendOnly)) {
2624 media_desc->set_direction(cricket::MD_SENDONLY);
2625 } else if (HasAttribute(line, kAttributeRecvOnly)) {
2626 media_desc->set_direction(cricket::MD_RECVONLY);
2627 } else if (HasAttribute(line, kAttributeInactive)) {
2628 media_desc->set_direction(cricket::MD_INACTIVE);
2629 } else if (HasAttribute(line, kAttributeSendRecv)) {
2630 media_desc->set_direction(cricket::MD_SENDRECV);
2631 } else if (HasAttribute(line, kAttributeExtmap)) {
2632 RtpHeaderExtension extmap;
2633 if (!ParseExtmap(line, &extmap, error)) {
2634 return false;
2635 }
2636 media_desc->AddRtpHeaderExtension(extmap);
2637 } else if (HasAttribute(line, kAttributeXGoogleFlag)) {
2638 // Experimental attribute. Conference mode activates more aggressive
2639 // AEC and NS settings.
2640 // TODO: expose API to set these directly.
2641 std::string flag_value;
2642 if (!GetValue(line, kAttributeXGoogleFlag, &flag_value, error)) {
2643 return false;
2644 }
2645 if (flag_value.compare(kValueConference) == 0)
2646 media_desc->set_conference_mode(true);
2647 } else if (HasAttribute(line, kAttributeXGoogleBufferLatency)) {
2648 // Experimental attribute.
2649 // TODO: expose API to set this directly.
2650 std::string flag_value;
2651 if (!GetValue(line, kAttributeXGoogleBufferLatency, &flag_value,
2652 error)) {
2653 return false;
2654 }
2655 int buffer_latency = 0;
wu@webrtc.org5e760e72014-04-02 23:19:09 +00002656 if (!GetValueFromString(line, flag_value, &buffer_latency, error)) {
2657 return false;
2658 }
2659 if (buffer_latency < 0) {
2660 return ParseFailed(line, "Buffer latency less than 0.", error);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002661 }
2662 media_desc->set_buffered_mode_latency(buffer_latency);
2663 }
2664 } else {
2665 // Only parse lines that we are interested of.
2666 LOG(LS_INFO) << "Ignored line: " << line;
2667 continue;
2668 }
2669 }
2670
2671 // Create tracks from the |ssrc_infos|.
2672 CreateTracksFromSsrcInfos(ssrc_infos, &tracks);
2673
2674 // Add the ssrc group to the track.
2675 for (SsrcGroupVec::iterator ssrc_group = ssrc_groups.begin();
2676 ssrc_group != ssrc_groups.end(); ++ssrc_group) {
2677 if (ssrc_group->ssrcs.empty()) {
2678 continue;
2679 }
2680 uint32 ssrc = ssrc_group->ssrcs.front();
2681 for (StreamParamsVec::iterator track = tracks.begin();
2682 track != tracks.end(); ++track) {
2683 if (track->has_ssrc(ssrc)) {
2684 track->ssrc_groups.push_back(*ssrc_group);
2685 }
2686 }
2687 }
2688
2689 // Add the new tracks to the |media_desc|.
2690 for (StreamParamsVec::iterator track = tracks.begin();
2691 track != tracks.end(); ++track) {
2692 media_desc->AddStream(*track);
2693 }
2694
2695 if (media_type == cricket::MEDIA_TYPE_AUDIO) {
2696 AudioContentDescription* audio_desc =
2697 static_cast<AudioContentDescription*>(media_desc);
2698 // Verify audio codec ensures that no audio codec has been populated with
2699 // only fmtp.
2700 if (!VerifyAudioCodecs(audio_desc)) {
2701 return ParseFailed("Failed to parse audio codecs correctly.", error);
2702 }
2703 AddAudioAttribute(kCodecParamMaxPTime, maxptime_as_string, audio_desc);
2704 AddAudioAttribute(kCodecParamPTime, ptime_as_string, audio_desc);
2705 }
2706
2707 if (media_type == cricket::MEDIA_TYPE_VIDEO) {
2708 VideoContentDescription* video_desc =
2709 static_cast<VideoContentDescription*>(media_desc);
2710 UpdateFromWildcardVideoCodecs(video_desc);
2711 // Verify video codec ensures that no video codec has been populated with
2712 // only rtcp-fb.
2713 if (!VerifyVideoCodecs(video_desc)) {
2714 return ParseFailed("Failed to parse video codecs correctly.", error);
2715 }
2716 }
2717
2718 // RFC 5245
2719 // Update the candidates with the media level "ice-pwd" and "ice-ufrag".
2720 for (Candidates::iterator it = candidates_orig.begin();
2721 it != candidates_orig.end(); ++it) {
2722 ASSERT((*it).username().empty());
2723 (*it).set_username(transport->ice_ufrag);
2724 ASSERT((*it).password().empty());
2725 (*it).set_password(transport->ice_pwd);
2726 candidates->push_back(
2727 new JsepIceCandidate(mline_id, mline_index, *it));
2728 }
2729 return true;
2730}
2731
2732bool ParseSsrcAttribute(const std::string& line, SsrcInfoVec* ssrc_infos,
2733 SdpParseError* error) {
2734 ASSERT(ssrc_infos != NULL);
2735 // RFC 5576
2736 // a=ssrc:<ssrc-id> <attribute>
2737 // a=ssrc:<ssrc-id> <attribute>:<value>
2738 std::string field1, field2;
2739 if (!SplitByDelimiter(line.substr(kLinePrefixLength),
2740 kSdpDelimiterSpace,
2741 &field1,
2742 &field2)) {
2743 const size_t expected_fields = 2;
2744 return ParseFailedExpectFieldNum(line, expected_fields, error);
2745 }
2746
2747 // ssrc:<ssrc-id>
2748 std::string ssrc_id_s;
2749 if (!GetValue(field1, kAttributeSsrc, &ssrc_id_s, error)) {
2750 return false;
2751 }
wu@webrtc.org5e760e72014-04-02 23:19:09 +00002752 uint32 ssrc_id = 0;
2753 if (!GetValueFromString(line, ssrc_id_s, &ssrc_id, error)) {
2754 return false;
2755 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002756
2757 std::string attribute;
2758 std::string value;
2759 if (!SplitByDelimiter(field2, kSdpDelimiterColon,
2760 &attribute, &value)) {
2761 std::ostringstream description;
2762 description << "Failed to get the ssrc attribute value from " << field2
2763 << ". Expected format <attribute>:<value>.";
2764 return ParseFailed(line, description.str(), error);
2765 }
2766
2767 // Check if there's already an item for this |ssrc_id|. Create a new one if
2768 // there isn't.
2769 SsrcInfoVec::iterator ssrc_info = ssrc_infos->begin();
2770 for (; ssrc_info != ssrc_infos->end(); ++ssrc_info) {
2771 if (ssrc_info->ssrc_id == ssrc_id) {
2772 break;
2773 }
2774 }
2775 if (ssrc_info == ssrc_infos->end()) {
2776 SsrcInfo info;
2777 info.ssrc_id = ssrc_id;
2778 ssrc_infos->push_back(info);
2779 ssrc_info = ssrc_infos->end() - 1;
2780 }
2781
2782 // Store the info to the |ssrc_info|.
2783 if (attribute == kSsrcAttributeCname) {
2784 // RFC 5576
2785 // cname:<value>
2786 ssrc_info->cname = value;
2787 } else if (attribute == kSsrcAttributeMsid) {
2788 // draft-alvestrand-mmusic-msid-00
2789 // "msid:" identifier [ " " appdata ]
2790 std::vector<std::string> fields;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002791 rtc::split(value, kSdpDelimiterSpace, &fields);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002792 if (fields.size() < 1 || fields.size() > 2) {
2793 return ParseFailed(line,
2794 "Expected format \"msid:<identifier>[ <appdata>]\".",
2795 error);
2796 }
2797 ssrc_info->msid_identifier = fields[0];
2798 if (fields.size() == 2) {
2799 ssrc_info->msid_appdata = fields[1];
2800 }
2801 } else if (attribute == kSsrcAttributeMslabel) {
2802 // draft-alvestrand-rtcweb-mid-01
2803 // mslabel:<value>
2804 ssrc_info->mslabel = value;
2805 } else if (attribute == kSSrcAttributeLabel) {
2806 // The label isn't defined.
2807 // label:<value>
2808 ssrc_info->label = value;
2809 }
2810 return true;
2811}
2812
2813bool ParseSsrcGroupAttribute(const std::string& line,
2814 SsrcGroupVec* ssrc_groups,
2815 SdpParseError* error) {
2816 ASSERT(ssrc_groups != NULL);
2817 // RFC 5576
2818 // a=ssrc-group:<semantics> <ssrc-id> ...
2819 std::vector<std::string> fields;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002820 rtc::split(line.substr(kLinePrefixLength),
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002821 kSdpDelimiterSpace, &fields);
2822 const size_t expected_min_fields = 2;
2823 if (fields.size() < expected_min_fields) {
2824 return ParseFailedExpectMinFieldNum(line, expected_min_fields, error);
2825 }
2826 std::string semantics;
2827 if (!GetValue(fields[0], kAttributeSsrcGroup, &semantics, error)) {
2828 return false;
2829 }
2830 std::vector<uint32> ssrcs;
2831 for (size_t i = 1; i < fields.size(); ++i) {
wu@webrtc.org5e760e72014-04-02 23:19:09 +00002832 uint32 ssrc = 0;
2833 if (!GetValueFromString(line, fields[i], &ssrc, error)) {
2834 return false;
2835 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002836 ssrcs.push_back(ssrc);
2837 }
2838 ssrc_groups->push_back(SsrcGroup(semantics, ssrcs));
2839 return true;
2840}
2841
2842bool ParseCryptoAttribute(const std::string& line,
2843 MediaContentDescription* media_desc,
2844 SdpParseError* error) {
2845 std::vector<std::string> fields;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002846 rtc::split(line.substr(kLinePrefixLength),
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002847 kSdpDelimiterSpace, &fields);
2848 // RFC 4568
2849 // a=crypto:<tag> <crypto-suite> <key-params> [<session-params>]
2850 const size_t expected_min_fields = 3;
2851 if (fields.size() < expected_min_fields) {
2852 return ParseFailedExpectMinFieldNum(line, expected_min_fields, error);
2853 }
2854 std::string tag_value;
2855 if (!GetValue(fields[0], kAttributeCrypto, &tag_value, error)) {
2856 return false;
2857 }
wu@webrtc.org5e760e72014-04-02 23:19:09 +00002858 int tag = 0;
2859 if (!GetValueFromString(line, tag_value, &tag, error)) {
2860 return false;
2861 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002862 const std::string crypto_suite = fields[1];
2863 const std::string key_params = fields[2];
2864 std::string session_params;
2865 if (fields.size() > 3) {
2866 session_params = fields[3];
2867 }
2868 media_desc->AddCrypto(CryptoParams(tag, crypto_suite, key_params,
2869 session_params));
2870 return true;
2871}
2872
2873// Updates or creates a new codec entry in the audio description with according
2874// to |name|, |clockrate|, |bitrate|, |channels| and |preference|.
2875void UpdateCodec(int payload_type, const std::string& name, int clockrate,
2876 int bitrate, int channels, int preference,
2877 AudioContentDescription* audio_desc) {
2878 // Codec may already be populated with (only) optional parameters
2879 // (from an fmtp).
2880 cricket::AudioCodec codec = GetCodec(audio_desc->codecs(), payload_type);
2881 codec.name = name;
2882 codec.clockrate = clockrate;
2883 codec.bitrate = bitrate;
2884 codec.channels = channels;
2885 codec.preference = preference;
2886 AddOrReplaceCodec<AudioContentDescription, cricket::AudioCodec>(audio_desc,
2887 codec);
2888}
2889
2890// Updates or creates a new codec entry in the video description according to
2891// |name|, |width|, |height|, |framerate| and |preference|.
2892void UpdateCodec(int payload_type, const std::string& name, int width,
2893 int height, int framerate, int preference,
2894 VideoContentDescription* video_desc) {
2895 // Codec may already be populated with (only) optional parameters
2896 // (from an fmtp).
2897 cricket::VideoCodec codec = GetCodec(video_desc->codecs(), payload_type);
2898 codec.name = name;
2899 codec.width = width;
2900 codec.height = height;
2901 codec.framerate = framerate;
2902 codec.preference = preference;
2903 AddOrReplaceCodec<VideoContentDescription, cricket::VideoCodec>(video_desc,
2904 codec);
2905}
2906
2907bool ParseRtpmapAttribute(const std::string& line,
2908 const MediaType media_type,
2909 const std::vector<int>& codec_preference,
2910 MediaContentDescription* media_desc,
2911 SdpParseError* error) {
2912 std::vector<std::string> fields;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002913 rtc::split(line.substr(kLinePrefixLength),
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002914 kSdpDelimiterSpace, &fields);
2915 // RFC 4566
2916 // a=rtpmap:<payload type> <encoding name>/<clock rate>[/<encodingparameters>]
2917 const size_t expected_min_fields = 2;
2918 if (fields.size() < expected_min_fields) {
2919 return ParseFailedExpectMinFieldNum(line, expected_min_fields, error);
2920 }
2921 std::string payload_type_value;
2922 if (!GetValue(fields[0], kAttributeRtpmap, &payload_type_value, error)) {
2923 return false;
2924 }
wu@webrtc.org5e760e72014-04-02 23:19:09 +00002925 int payload_type = 0;
2926 if (!GetValueFromString(line, payload_type_value, &payload_type, error)) {
2927 return false;
2928 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002929
2930 // Set the preference order depending on the order of the pl type in the
2931 // <fmt> of the m-line.
2932 const int preference = codec_preference.end() -
2933 std::find(codec_preference.begin(), codec_preference.end(),
2934 payload_type);
2935 if (preference == 0) {
2936 LOG(LS_WARNING) << "Ignore rtpmap line that did not appear in the "
2937 << "<fmt> of the m-line: " << line;
2938 return true;
2939 }
2940 const std::string encoder = fields[1];
2941 std::vector<std::string> codec_params;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002942 rtc::split(encoder, '/', &codec_params);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002943 // <encoding name>/<clock rate>[/<encodingparameters>]
2944 // 2 mandatory fields
2945 if (codec_params.size() < 2 || codec_params.size() > 3) {
2946 return ParseFailed(line,
2947 "Expected format \"<encoding name>/<clock rate>"
2948 "[/<encodingparameters>]\".",
2949 error);
2950 }
2951 const std::string encoding_name = codec_params[0];
wu@webrtc.org5e760e72014-04-02 23:19:09 +00002952 int clock_rate = 0;
2953 if (!GetValueFromString(line, codec_params[1], &clock_rate, error)) {
2954 return false;
2955 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002956 if (media_type == cricket::MEDIA_TYPE_VIDEO) {
2957 VideoContentDescription* video_desc =
2958 static_cast<VideoContentDescription*>(media_desc);
2959 // TODO: We will send resolution in SDP. For now use
2960 // JsepSessionDescription::kMaxVideoCodecWidth and kMaxVideoCodecHeight.
2961 UpdateCodec(payload_type, encoding_name,
2962 JsepSessionDescription::kMaxVideoCodecWidth,
2963 JsepSessionDescription::kMaxVideoCodecHeight,
2964 JsepSessionDescription::kDefaultVideoCodecFramerate,
2965 preference, video_desc);
2966 } else if (media_type == cricket::MEDIA_TYPE_AUDIO) {
2967 // RFC 4566
2968 // For audio streams, <encoding parameters> indicates the number
2969 // of audio channels. This parameter is OPTIONAL and may be
2970 // omitted if the number of channels is one, provided that no
2971 // additional parameters are needed.
2972 int channels = 1;
2973 if (codec_params.size() == 3) {
wu@webrtc.org5e760e72014-04-02 23:19:09 +00002974 if (!GetValueFromString(line, codec_params[2], &channels, error)) {
2975 return false;
2976 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002977 }
2978 int bitrate = 0;
2979 // The default behavior for ISAC (bitrate == 0) in webrtcvoiceengine.cc
2980 // (specifically FindWebRtcCodec) is bandwidth-adaptive variable bitrate.
2981 // The bandwidth adaptation doesn't always work well, so this code
2982 // sets a fixed target bitrate instead.
2983 if (_stricmp(encoding_name.c_str(), kIsacCodecName) == 0) {
2984 if (clock_rate <= 16000) {
2985 bitrate = kIsacWbDefaultRate;
2986 } else {
2987 bitrate = kIsacSwbDefaultRate;
2988 }
2989 }
2990 AudioContentDescription* audio_desc =
2991 static_cast<AudioContentDescription*>(media_desc);
2992 UpdateCodec(payload_type, encoding_name, clock_rate, bitrate, channels,
2993 preference, audio_desc);
2994 } else if (media_type == cricket::MEDIA_TYPE_DATA) {
2995 DataContentDescription* data_desc =
2996 static_cast<DataContentDescription*>(media_desc);
2997 data_desc->AddCodec(cricket::DataCodec(payload_type, encoding_name,
2998 preference));
2999 }
3000 return true;
3001}
3002
3003void PruneRight(const char delimiter, std::string* message) {
3004 size_t trailing = message->find(delimiter);
3005 if (trailing != std::string::npos) {
3006 *message = message->substr(0, trailing);
3007 }
3008}
3009
3010bool ParseFmtpParam(const std::string& line, std::string* parameter,
3011 std::string* value, SdpParseError* error) {
3012 if (!SplitByDelimiter(line, kSdpDelimiterEqual, parameter, value)) {
3013 ParseFailed(line, "Unable to parse fmtp parameter. \'=\' missing.", error);
3014 return false;
3015 }
3016 // a=fmtp:<payload_type> <param1>=<value1>; <param2>=<value2>; ...
3017 // When parsing the values the trailing ";" gets picked up. Remove them.
3018 PruneRight(kSdpDelimiterSemicolon, value);
3019 return true;
3020}
3021
3022bool ParseFmtpAttributes(const std::string& line, const MediaType media_type,
3023 MediaContentDescription* media_desc,
3024 SdpParseError* error) {
3025 if (media_type != cricket::MEDIA_TYPE_AUDIO &&
3026 media_type != cricket::MEDIA_TYPE_VIDEO) {
3027 return true;
3028 }
3029 std::vector<std::string> fields;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00003030 rtc::split(line.substr(kLinePrefixLength),
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003031 kSdpDelimiterSpace, &fields);
3032
3033 // RFC 5576
3034 // a=fmtp:<format> <format specific parameters>
3035 // At least two fields, whereas the second one is any of the optional
3036 // parameters.
3037 if (fields.size() < 2) {
3038 ParseFailedExpectMinFieldNum(line, 2, error);
3039 return false;
3040 }
3041
3042 std::string payload_type;
3043 if (!GetValue(fields[0], kAttributeFmtp, &payload_type, error)) {
3044 return false;
3045 }
3046
3047 cricket::CodecParameterMap codec_params;
3048 for (std::vector<std::string>::const_iterator iter = fields.begin() + 1;
3049 iter != fields.end(); ++iter) {
3050 std::string name;
3051 std::string value;
3052 if (iter->find(kSdpDelimiterEqual) == std::string::npos) {
3053 // Only fmtps with equals are currently supported. Other fmtp types
3054 // should be ignored. Unknown fmtps do not constitute an error.
3055 continue;
3056 }
3057 if (!ParseFmtpParam(*iter, &name, &value, error)) {
3058 return false;
3059 }
3060 codec_params[name] = value;
3061 }
3062
wu@webrtc.org5e760e72014-04-02 23:19:09 +00003063 int int_payload_type = 0;
3064 if (!GetValueFromString(line, payload_type, &int_payload_type, error)) {
3065 return false;
3066 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003067 if (media_type == cricket::MEDIA_TYPE_AUDIO) {
3068 UpdateCodec<AudioContentDescription, cricket::AudioCodec>(
3069 media_desc, int_payload_type, codec_params);
3070 } else if (media_type == cricket::MEDIA_TYPE_VIDEO) {
3071 UpdateCodec<VideoContentDescription, cricket::VideoCodec>(
3072 media_desc, int_payload_type, codec_params);
3073 }
3074 return true;
3075}
3076
3077bool ParseRtcpFbAttribute(const std::string& line, const MediaType media_type,
3078 MediaContentDescription* media_desc,
3079 SdpParseError* error) {
3080 if (media_type != cricket::MEDIA_TYPE_AUDIO &&
3081 media_type != cricket::MEDIA_TYPE_VIDEO) {
3082 return true;
3083 }
3084 std::vector<std::string> rtcp_fb_fields;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00003085 rtc::split(line.c_str(), kSdpDelimiterSpace, &rtcp_fb_fields);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003086 if (rtcp_fb_fields.size() < 2) {
3087 return ParseFailedGetValue(line, kAttributeRtcpFb, error);
3088 }
3089 std::string payload_type_string;
3090 if (!GetValue(rtcp_fb_fields[0], kAttributeRtcpFb, &payload_type_string,
3091 error)) {
3092 return false;
3093 }
wu@webrtc.org5e760e72014-04-02 23:19:09 +00003094 int payload_type = kWildcardPayloadType;
3095 if (payload_type_string != "*") {
3096 if (!GetValueFromString(line, payload_type_string, &payload_type, error)) {
3097 return false;
3098 }
3099 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003100 std::string id = rtcp_fb_fields[1];
3101 std::string param = "";
3102 for (std::vector<std::string>::iterator iter = rtcp_fb_fields.begin() + 2;
3103 iter != rtcp_fb_fields.end(); ++iter) {
3104 param.append(*iter);
3105 }
3106 const cricket::FeedbackParam feedback_param(id, param);
3107
3108 if (media_type == cricket::MEDIA_TYPE_AUDIO) {
3109 UpdateCodec<AudioContentDescription, cricket::AudioCodec>(media_desc,
3110 payload_type,
3111 feedback_param);
3112 } else if (media_type == cricket::MEDIA_TYPE_VIDEO) {
3113 UpdateCodec<VideoContentDescription, cricket::VideoCodec>(media_desc,
3114 payload_type,
3115 feedback_param);
3116 }
3117 return true;
3118}
3119
3120} // namespace webrtc