blob: de45c099881bd994480dfe1dd2bb0b7f5093eff9 [file] [log] [blame]
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001/*
2 * libjingle
jlmiller@webrtc.org5f93d0a2015-01-20 21:36:13 +00003 * Copyright 2011 Google Inc.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00004 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright notice,
9 * this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright notice,
11 * this list of conditions and the following disclaimer in the documentation
12 * and/or other materials provided with the distribution.
13 * 3. The name of the author may not be used to endorse or promote products
14 * derived from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
19 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28#include "talk/app/webrtc/webrtcsdp.h"
29
30#include <limits.h>
31#include <stdio.h>
32#include <algorithm>
33#include <string>
34#include <vector>
decurtis@webrtc.org8af11042015-01-07 19:15:51 +000035#include <ctype.h>
henrike@webrtc.org28e20752013-07-10 00:45:36 +000036
37#include "talk/app/webrtc/jsepicecandidate.h"
38#include "talk/app/webrtc/jsepsessiondescription.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000039#include "talk/media/base/codec.h"
40#include "talk/media/base/constants.h"
41#include "talk/media/base/cryptoparams.h"
pkasting@chromium.orge9facf82015-02-17 20:36:28 +000042#include "talk/media/base/rtputils.h"
mallinath@webrtc.org1112c302013-09-23 20:34:45 +000043#include "talk/media/sctp/sctpdataengine.h"
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000044#include "webrtc/p2p/base/candidate.h"
45#include "webrtc/p2p/base/constants.h"
46#include "webrtc/p2p/base/port.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000047#include "talk/session/media/mediasession.h"
buildbot@webrtc.orga09a9992014-08-13 17:26:08 +000048#include "webrtc/base/common.h"
49#include "webrtc/base/logging.h"
50#include "webrtc/base/messagedigest.h"
51#include "webrtc/base/stringutils.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000052
53using cricket::AudioContentDescription;
54using cricket::Candidate;
55using cricket::Candidates;
56using cricket::ContentDescription;
57using cricket::ContentInfo;
58using cricket::CryptoParams;
59using cricket::DataContentDescription;
60using cricket::ICE_CANDIDATE_COMPONENT_RTP;
61using cricket::ICE_CANDIDATE_COMPONENT_RTCP;
62using cricket::kCodecParamMaxBitrate;
63using cricket::kCodecParamMaxPTime;
64using cricket::kCodecParamMaxQuantization;
65using cricket::kCodecParamMinBitrate;
66using cricket::kCodecParamMinPTime;
67using cricket::kCodecParamPTime;
68using cricket::kCodecParamSPropStereo;
buildbot@webrtc.orged97bb02014-05-07 11:15:20 +000069using cricket::kCodecParamStartBitrate;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000070using cricket::kCodecParamStereo;
71using cricket::kCodecParamUseInbandFec;
72using cricket::kCodecParamSctpProtocol;
73using cricket::kCodecParamSctpStreams;
henrike@webrtc.org1e09a712013-07-26 19:17:59 +000074using cricket::kCodecParamMaxAverageBitrate;
buildbot@webrtc.org5d639b32014-09-10 07:57:12 +000075using cricket::kCodecParamMaxPlaybackRate;
stefan@webrtc.org85d27942014-06-09 12:51:39 +000076using cricket::kCodecParamAssociatedPayloadType;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000077using cricket::kWildcardPayloadType;
78using cricket::MediaContentDescription;
79using cricket::MediaType;
80using cricket::NS_JINGLE_ICE_UDP;
81using cricket::RtpHeaderExtension;
82using cricket::SsrcGroup;
83using cricket::StreamParams;
84using cricket::StreamParamsVec;
85using cricket::TransportDescription;
86using cricket::TransportInfo;
87using cricket::VideoContentDescription;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000088using rtc::SocketAddress;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000089
90typedef std::vector<RtpHeaderExtension> RtpHeaderExtensions;
91
92namespace cricket {
93class SessionDescription;
94}
95
96namespace webrtc {
97
98// Line type
99// RFC 4566
100// An SDP session description consists of a number of lines of text of
101// the form:
102// <type>=<value>
103// where <type> MUST be exactly one case-significant character.
104static const int kLinePrefixLength = 2; // Lenght of <type>=
105static const char kLineTypeVersion = 'v';
106static const char kLineTypeOrigin = 'o';
107static const char kLineTypeSessionName = 's';
108static const char kLineTypeSessionInfo = 'i';
109static const char kLineTypeSessionUri = 'u';
110static const char kLineTypeSessionEmail = 'e';
111static const char kLineTypeSessionPhone = 'p';
112static const char kLineTypeSessionBandwidth = 'b';
113static const char kLineTypeTiming = 't';
114static const char kLineTypeRepeatTimes = 'r';
115static const char kLineTypeTimeZone = 'z';
116static const char kLineTypeEncryptionKey = 'k';
117static const char kLineTypeMedia = 'm';
118static const char kLineTypeConnection = 'c';
119static const char kLineTypeAttributes = 'a';
120
121// Attributes
122static const char kAttributeGroup[] = "group";
123static const char kAttributeMid[] = "mid";
124static const char kAttributeRtcpMux[] = "rtcp-mux";
125static const char kAttributeSsrc[] = "ssrc";
126static const char kSsrcAttributeCname[] = "cname";
127static const char kAttributeExtmap[] = "extmap";
128// draft-alvestrand-mmusic-msid-01
129// a=msid-semantic: WMS
130static const char kAttributeMsidSemantics[] = "msid-semantic";
131static const char kMediaStreamSemantic[] = "WMS";
132static const char kSsrcAttributeMsid[] = "msid";
133static const char kDefaultMsid[] = "default";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000134static const char kSsrcAttributeMslabel[] = "mslabel";
135static const char kSSrcAttributeLabel[] = "label";
136static const char kAttributeSsrcGroup[] = "ssrc-group";
137static const char kAttributeCrypto[] = "crypto";
138static const char kAttributeCandidate[] = "candidate";
139static const char kAttributeCandidateTyp[] = "typ";
140static const char kAttributeCandidateRaddr[] = "raddr";
141static const char kAttributeCandidateRport[] = "rport";
142static const char kAttributeCandidateUsername[] = "username";
143static const char kAttributeCandidatePassword[] = "password";
144static const char kAttributeCandidateGeneration[] = "generation";
145static const char kAttributeFingerprint[] = "fingerprint";
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +0000146static const char kAttributeSetup[] = "setup";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000147static const char kAttributeFmtp[] = "fmtp";
148static const char kAttributeRtpmap[] = "rtpmap";
wu@webrtc.org78187522013-10-07 23:32:02 +0000149static const char kAttributeSctpmap[] = "sctpmap";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000150static const char kAttributeRtcp[] = "rtcp";
151static const char kAttributeIceUfrag[] = "ice-ufrag";
152static const char kAttributeIcePwd[] = "ice-pwd";
153static const char kAttributeIceLite[] = "ice-lite";
154static const char kAttributeIceOption[] = "ice-options";
155static const char kAttributeSendOnly[] = "sendonly";
156static const char kAttributeRecvOnly[] = "recvonly";
157static const char kAttributeRtcpFb[] = "rtcp-fb";
158static const char kAttributeSendRecv[] = "sendrecv";
159static const char kAttributeInactive[] = "inactive";
jiayl@webrtc.orgddb85ab2014-09-05 16:31:56 +0000160// draft-ietf-mmusic-sctp-sdp-07
161// a=sctp-port
162static const char kAttributeSctpPort[] = "sctp-port";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000163
164// Experimental flags
165static const char kAttributeXGoogleFlag[] = "x-google-flag";
166static const char kValueConference[] = "conference";
167static const char kAttributeXGoogleBufferLatency[] =
168 "x-google-buffer-latency";
169
170// Candidate
171static const char kCandidateHost[] = "host";
172static const char kCandidateSrflx[] = "srflx";
173// TODO: How to map the prflx with circket candidate type
174// static const char kCandidatePrflx[] = "prflx";
175static const char kCandidateRelay[] = "relay";
mallinath@webrtc.org2d60c5e2014-08-08 22:29:20 +0000176static const char kTcpCandidateType[] = "tcptype";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000177
178static const char kSdpDelimiterEqual = '=';
179static const char kSdpDelimiterSpace = ' ';
180static const char kSdpDelimiterColon = ':';
181static const char kSdpDelimiterSemicolon = ';';
182static const char kSdpDelimiterSlash = '/';
183static const char kNewLine = '\n';
184static const char kReturn = '\r';
185static const char kLineBreak[] = "\r\n";
186
187// TODO: Generate the Session and Time description
188// instead of hardcoding.
189static const char kSessionVersion[] = "v=0";
190// RFC 4566
191static const char kSessionOriginUsername[] = "-";
192static const char kSessionOriginSessionId[] = "0";
193static const char kSessionOriginSessionVersion[] = "0";
194static const char kSessionOriginNettype[] = "IN";
195static const char kSessionOriginAddrtype[] = "IP4";
196static const char kSessionOriginAddress[] = "127.0.0.1";
197static const char kSessionName[] = "s=-";
198static const char kTimeDescription[] = "t=0 0";
199static const char kAttrGroup[] = "a=group:BUNDLE";
200static const char kConnectionNettype[] = "IN";
pthatcher@webrtc.orgc9d6d142014-10-23 23:37:22 +0000201static const char kConnectionIpv4Addrtype[] = "IP4";
202static const char kConnectionIpv6Addrtype[] = "IP6";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000203static const char kMediaTypeVideo[] = "video";
204static const char kMediaTypeAudio[] = "audio";
205static const char kMediaTypeData[] = "application";
206static const char kMediaPortRejected[] = "0";
pthatcher@webrtc.orgc9d6d142014-10-23 23:37:22 +0000207// draft-ietf-mmusic-trickle-ice-01
208// When no candidates have been gathered, set the connection
209// address to IP6 ::.
perkj@webrtc.orgd105cc82014-11-07 11:22:06 +0000210// TODO(perkj): FF can not parse IP6 ::. See http://crbug/430333
211// Use IPV4 per default.
212static const char kDummyAddress[] = "0.0.0.0";
pthatcher@webrtc.orgc9d6d142014-10-23 23:37:22 +0000213static const char kDummyPort[] = "9";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000214// RFC 3556
215static const char kApplicationSpecificMaximum[] = "AS";
216
217static const int kDefaultVideoClockrate = 90000;
218
219// ISAC special-case.
220static const char kIsacCodecName[] = "ISAC"; // From webrtcvoiceengine.cc
221static const int kIsacWbDefaultRate = 32000; // From acm_common_defs.h
222static const int kIsacSwbDefaultRate = 56000; // From acm_common_defs.h
223
wu@webrtc.org78187522013-10-07 23:32:02 +0000224static const char kDefaultSctpmapProtocol[] = "webrtc-datachannel";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000225
226struct SsrcInfo {
227 SsrcInfo()
228 : msid_identifier(kDefaultMsid),
229 // TODO(ronghuawu): What should we do if the appdata doesn't appear?
230 // Create random string (which will be used as track label later)?
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000231 msid_appdata(rtc::CreateRandomString(8)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000232 }
233 uint32 ssrc_id;
234 std::string cname;
235 std::string msid_identifier;
236 std::string msid_appdata;
237
238 // For backward compatibility.
239 // TODO(ronghuawu): Remove below 2 fields once all the clients support msid.
240 std::string label;
241 std::string mslabel;
242};
243typedef std::vector<SsrcInfo> SsrcInfoVec;
244typedef std::vector<SsrcGroup> SsrcGroupVec;
245
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000246template <class T>
247static void AddFmtpLine(const T& codec, std::string* message);
248static void BuildMediaDescription(const ContentInfo* content_info,
249 const TransportInfo* transport_info,
250 const MediaType media_type,
wu@webrtc.org4c3e9912014-07-16 21:03:13 +0000251 const std::vector<Candidate>& candidates,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000252 std::string* message);
jiayl@webrtc.org9c16c392014-05-01 18:30:30 +0000253static void BuildSctpContentAttributes(std::string* message, int sctp_port);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000254static void BuildRtpContentAttributes(
255 const MediaContentDescription* media_desc,
256 const MediaType media_type,
257 std::string* message);
258static void BuildRtpMap(const MediaContentDescription* media_desc,
259 const MediaType media_type,
260 std::string* message);
261static void BuildCandidate(const std::vector<Candidate>& candidates,
262 std::string* message);
263static void BuildIceOptions(const std::vector<std::string>& transport_options,
264 std::string* message);
pthatcher@webrtc.org3341b402015-02-13 21:14:22 +0000265static bool IsRtp(const std::string& protocol);
266static bool IsDtlsSctp(const std::string& protocol);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000267static bool ParseSessionDescription(const std::string& message, size_t* pos,
268 std::string* session_id,
269 std::string* session_version,
270 bool* supports_msid,
271 TransportDescription* session_td,
272 RtpHeaderExtensions* session_extmaps,
273 cricket::SessionDescription* desc,
274 SdpParseError* error);
275static bool ParseGroupAttribute(const std::string& line,
276 cricket::SessionDescription* desc,
277 SdpParseError* error);
278static bool ParseMediaDescription(
279 const std::string& message,
280 const TransportDescription& session_td,
281 const RtpHeaderExtensions& session_extmaps,
282 bool supports_msid,
283 size_t* pos, cricket::SessionDescription* desc,
284 std::vector<JsepIceCandidate*>* candidates,
285 SdpParseError* error);
286static bool ParseContent(const std::string& message,
287 const MediaType media_type,
288 int mline_index,
289 const std::string& protocol,
290 const std::vector<int>& codec_preference,
291 size_t* pos,
292 std::string* content_name,
293 MediaContentDescription* media_desc,
294 TransportDescription* transport,
295 std::vector<JsepIceCandidate*>* candidates,
296 SdpParseError* error);
297static bool ParseSsrcAttribute(const std::string& line,
298 SsrcInfoVec* ssrc_infos,
299 SdpParseError* error);
300static bool ParseSsrcGroupAttribute(const std::string& line,
301 SsrcGroupVec* ssrc_groups,
302 SdpParseError* error);
303static bool ParseCryptoAttribute(const std::string& line,
304 MediaContentDescription* media_desc,
305 SdpParseError* error);
306static bool ParseRtpmapAttribute(const std::string& line,
307 const MediaType media_type,
308 const std::vector<int>& codec_preference,
309 MediaContentDescription* media_desc,
310 SdpParseError* error);
311static bool ParseFmtpAttributes(const std::string& line,
312 const MediaType media_type,
313 MediaContentDescription* media_desc,
314 SdpParseError* error);
315static bool ParseFmtpParam(const std::string& line, std::string* parameter,
316 std::string* value, SdpParseError* error);
317static bool ParseCandidate(const std::string& message, Candidate* candidate,
318 SdpParseError* error, bool is_raw);
319static bool ParseRtcpFbAttribute(const std::string& line,
320 const MediaType media_type,
321 MediaContentDescription* media_desc,
322 SdpParseError* error);
323static bool ParseIceOptions(const std::string& line,
324 std::vector<std::string>* transport_options,
325 SdpParseError* error);
326static bool ParseExtmap(const std::string& line,
327 RtpHeaderExtension* extmap,
328 SdpParseError* error);
329static bool ParseFingerprintAttribute(const std::string& line,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000330 rtc::SSLFingerprint** fingerprint,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000331 SdpParseError* error);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +0000332static bool ParseDtlsSetup(const std::string& line,
333 cricket::ConnectionRole* role,
334 SdpParseError* error);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000335
336// Helper functions
337
338// Below ParseFailed*** functions output the line that caused the parsing
339// failure and the detailed reason (|description|) of the failure to |error|.
340// The functions always return false so that they can be used directly in the
341// following way when error happens:
342// "return ParseFailed***(...);"
343
344// The line starting at |line_start| of |message| is the failing line.
345// The reason for the failure should be provided in the |description|.
346// An example of a description could be "unknown character".
347static bool ParseFailed(const std::string& message,
348 size_t line_start,
349 const std::string& description,
350 SdpParseError* error) {
351 // Get the first line of |message| from |line_start|.
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +0000352 std::string first_line;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000353 size_t line_end = message.find(kNewLine, line_start);
354 if (line_end != std::string::npos) {
355 if (line_end > 0 && (message.at(line_end - 1) == kReturn)) {
356 --line_end;
357 }
358 first_line = message.substr(line_start, (line_end - line_start));
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +0000359 } else {
360 first_line = message.substr(line_start);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000361 }
362
363 if (error) {
364 error->line = first_line;
365 error->description = description;
366 }
367 LOG(LS_ERROR) << "Failed to parse: \"" << first_line
368 << "\". Reason: " << description;
369 return false;
370}
371
372// |line| is the failing line. The reason for the failure should be
373// provided in the |description|.
374static bool ParseFailed(const std::string& line,
375 const std::string& description,
376 SdpParseError* error) {
377 return ParseFailed(line, 0, description, error);
378}
379
380// Parses failure where the failing SDP line isn't know or there are multiple
381// failing lines.
382static bool ParseFailed(const std::string& description,
383 SdpParseError* error) {
384 return ParseFailed("", description, error);
385}
386
387// |line| is the failing line. The failure is due to the fact that |line|
388// doesn't have |expected_fields| fields.
389static bool ParseFailedExpectFieldNum(const std::string& line,
390 int expected_fields,
391 SdpParseError* error) {
392 std::ostringstream description;
393 description << "Expects " << expected_fields << " fields.";
394 return ParseFailed(line, description.str(), error);
395}
396
397// |line| is the failing line. The failure is due to the fact that |line| has
398// less than |expected_min_fields| fields.
399static bool ParseFailedExpectMinFieldNum(const std::string& line,
400 int expected_min_fields,
401 SdpParseError* error) {
402 std::ostringstream description;
403 description << "Expects at least " << expected_min_fields << " fields.";
404 return ParseFailed(line, description.str(), error);
405}
406
407// |line| is the failing line. The failure is due to the fact that it failed to
408// get the value of |attribute|.
409static bool ParseFailedGetValue(const std::string& line,
410 const std::string& attribute,
411 SdpParseError* error) {
412 std::ostringstream description;
413 description << "Failed to get the value of attribute: " << attribute;
414 return ParseFailed(line, description.str(), error);
415}
416
417// The line starting at |line_start| of |message| is the failing line. The
418// failure is due to the line type (e.g. the "m" part of the "m-line")
419// not matching what is expected. The expected line type should be
420// provided as |line_type|.
421static bool ParseFailedExpectLine(const std::string& message,
422 size_t line_start,
423 const char line_type,
424 const std::string& line_value,
425 SdpParseError* error) {
426 std::ostringstream description;
427 description << "Expect line: " << line_type << "=" << line_value;
428 return ParseFailed(message, line_start, description.str(), error);
429}
430
431static bool AddLine(const std::string& line, std::string* message) {
432 if (!message)
433 return false;
434
435 message->append(line);
436 message->append(kLineBreak);
437 return true;
438}
439
440static bool GetLine(const std::string& message,
441 size_t* pos,
442 std::string* line) {
443 size_t line_begin = *pos;
444 size_t line_end = message.find(kNewLine, line_begin);
445 if (line_end == std::string::npos) {
446 return false;
447 }
448 // Update the new start position
449 *pos = line_end + 1;
450 if (line_end > 0 && (message.at(line_end - 1) == kReturn)) {
451 --line_end;
452 }
453 *line = message.substr(line_begin, (line_end - line_begin));
454 const char* cline = line->c_str();
455 // RFC 4566
456 // An SDP session description consists of a number of lines of text of
457 // the form:
458 // <type>=<value>
459 // where <type> MUST be exactly one case-significant character and
460 // <value> is structured text whose format depends on <type>.
461 // Whitespace MUST NOT be used on either side of the "=" sign.
decurtis@webrtc.org8af11042015-01-07 19:15:51 +0000462 if (line->length() < 3 ||
463 !islower(cline[0]) ||
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000464 cline[1] != kSdpDelimiterEqual ||
465 cline[2] == kSdpDelimiterSpace) {
466 *pos = line_begin;
467 return false;
468 }
469 return true;
470}
471
472// Init |os| to "|type|=|value|".
473static void InitLine(const char type,
474 const std::string& value,
475 std::ostringstream* os) {
476 os->str("");
477 *os << type << kSdpDelimiterEqual << value;
478}
479
480// Init |os| to "a=|attribute|".
481static void InitAttrLine(const std::string& attribute, std::ostringstream* os) {
482 InitLine(kLineTypeAttributes, attribute, os);
483}
484
485// Writes a SDP attribute line based on |attribute| and |value| to |message|.
486static void AddAttributeLine(const std::string& attribute, int value,
487 std::string* message) {
488 std::ostringstream os;
489 InitAttrLine(attribute, &os);
490 os << kSdpDelimiterColon << value;
491 AddLine(os.str(), message);
492}
493
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000494static bool IsLineType(const std::string& message,
495 const char type,
496 size_t line_start) {
497 if (message.size() < line_start + kLinePrefixLength) {
498 return false;
499 }
500 const char* cmessage = message.c_str();
501 return (cmessage[line_start] == type &&
502 cmessage[line_start + 1] == kSdpDelimiterEqual);
503}
504
505static bool IsLineType(const std::string& line,
506 const char type) {
507 return IsLineType(line, type, 0);
508}
509
510static bool GetLineWithType(const std::string& message, size_t* pos,
511 std::string* line, const char type) {
512 if (!IsLineType(message, type, *pos)) {
513 return false;
514 }
515
516 if (!GetLine(message, pos, line))
517 return false;
518
519 return true;
520}
521
522static bool HasAttribute(const std::string& line,
523 const std::string& attribute) {
524 return (line.compare(kLinePrefixLength, attribute.size(), attribute) == 0);
525}
526
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000527static bool AddSsrcLine(uint32 ssrc_id, const std::string& attribute,
528 const std::string& value, std::string* message) {
529 // RFC 5576
530 // a=ssrc:<ssrc-id> <attribute>:<value>
531 std::ostringstream os;
532 InitAttrLine(kAttributeSsrc, &os);
533 os << kSdpDelimiterColon << ssrc_id << kSdpDelimiterSpace
534 << attribute << kSdpDelimiterColon << value;
535 return AddLine(os.str(), message);
536}
537
538// Split the message into two parts by the first delimiter.
539static bool SplitByDelimiter(const std::string& message,
540 const char delimiter,
541 std::string* field1,
542 std::string* field2) {
543 // Find the first delimiter
544 size_t pos = message.find(delimiter);
545 if (pos == std::string::npos) {
546 return false;
547 }
548 *field1 = message.substr(0, pos);
549 // The rest is the value.
550 *field2 = message.substr(pos + 1);
551 return true;
552}
553
554// Get value only from <attribute>:<value>.
555static bool GetValue(const std::string& message, const std::string& attribute,
556 std::string* value, SdpParseError* error) {
557 std::string leftpart;
558 if (!SplitByDelimiter(message, kSdpDelimiterColon, &leftpart, value)) {
559 return ParseFailedGetValue(message, attribute, error);
560 }
561 // The left part should end with the expected attribute.
562 if (leftpart.length() < attribute.length() ||
563 leftpart.compare(leftpart.length() - attribute.length(),
564 attribute.length(), attribute) != 0) {
565 return ParseFailedGetValue(message, attribute, error);
566 }
567 return true;
568}
569
570static bool CaseInsensitiveFind(std::string str1, std::string str2) {
571 std::transform(str1.begin(), str1.end(), str1.begin(),
572 ::tolower);
573 std::transform(str2.begin(), str2.end(), str2.begin(),
574 ::tolower);
575 return str1.find(str2) != std::string::npos;
576}
577
wu@webrtc.org5e760e72014-04-02 23:19:09 +0000578template <class T>
579static bool GetValueFromString(const std::string& line,
580 const std::string& s,
581 T* t,
582 SdpParseError* error) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000583 if (!rtc::FromString(s, t)) {
wu@webrtc.org5e760e72014-04-02 23:19:09 +0000584 std::ostringstream description;
585 description << "Invalid value: " << s << ".";
586 return ParseFailed(line, description.str(), error);
587 }
588 return true;
589}
590
pkasting@chromium.orge9facf82015-02-17 20:36:28 +0000591static bool GetPayloadTypeFromString(const std::string& line,
592 const std::string& s,
593 int* payload_type,
594 SdpParseError* error) {
595 return GetValueFromString(line, s, payload_type, error) &&
596 cricket::IsValidRtpPayloadType(*payload_type);
597}
598
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000599void CreateTracksFromSsrcInfos(const SsrcInfoVec& ssrc_infos,
600 StreamParamsVec* tracks) {
601 ASSERT(tracks != NULL);
602 for (SsrcInfoVec::const_iterator ssrc_info = ssrc_infos.begin();
603 ssrc_info != ssrc_infos.end(); ++ssrc_info) {
604 if (ssrc_info->cname.empty()) {
605 continue;
606 }
607
608 std::string sync_label;
609 std::string track_id;
610 if (ssrc_info->msid_identifier == kDefaultMsid &&
611 !ssrc_info->mslabel.empty()) {
612 // If there's no msid and there's mslabel, we consider this is a sdp from
613 // a older version of client that doesn't support msid.
614 // In that case, we use the mslabel and label to construct the track.
615 sync_label = ssrc_info->mslabel;
616 track_id = ssrc_info->label;
617 } else {
618 sync_label = ssrc_info->msid_identifier;
619 // The appdata consists of the "id" attribute of a MediaStreamTrack, which
620 // is corresponding to the "id" attribute of StreamParams.
621 track_id = ssrc_info->msid_appdata;
622 }
623 if (sync_label.empty() || track_id.empty()) {
624 ASSERT(false);
625 continue;
626 }
627
628 StreamParamsVec::iterator track = tracks->begin();
629 for (; track != tracks->end(); ++track) {
630 if (track->id == track_id) {
631 break;
632 }
633 }
634 if (track == tracks->end()) {
635 // If we don't find an existing track, create a new one.
636 tracks->push_back(StreamParams());
637 track = tracks->end() - 1;
638 }
639 track->add_ssrc(ssrc_info->ssrc_id);
640 track->cname = ssrc_info->cname;
641 track->sync_label = sync_label;
642 track->id = track_id;
643 }
644}
645
646void GetMediaStreamLabels(const ContentInfo* content,
647 std::set<std::string>* labels) {
648 const MediaContentDescription* media_desc =
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000649 static_cast<const MediaContentDescription*>(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000650 content->description);
651 const cricket::StreamParamsVec& streams = media_desc->streams();
652 for (cricket::StreamParamsVec::const_iterator it = streams.begin();
653 it != streams.end(); ++it) {
654 labels->insert(it->sync_label);
655 }
656}
657
658// RFC 5245
659// It is RECOMMENDED that default candidates be chosen based on the
660// likelihood of those candidates to work with the peer that is being
661// contacted. It is RECOMMENDED that relayed > reflexive > host.
662static const int kPreferenceUnknown = 0;
663static const int kPreferenceHost = 1;
664static const int kPreferenceReflexive = 2;
665static const int kPreferenceRelayed = 3;
666
667static int GetCandidatePreferenceFromType(const std::string& type) {
668 int preference = kPreferenceUnknown;
669 if (type == cricket::LOCAL_PORT_TYPE) {
670 preference = kPreferenceHost;
671 } else if (type == cricket::STUN_PORT_TYPE) {
672 preference = kPreferenceReflexive;
673 } else if (type == cricket::RELAY_PORT_TYPE) {
674 preference = kPreferenceRelayed;
675 } else {
676 ASSERT(false);
677 }
678 return preference;
679}
680
guoweis@webrtc.org57ac2c82015-02-06 00:45:13 +0000681// Get ip and port of the default destination from the |candidates| with the
682// given value of |component_id|. The default candidate should be the one most
683// likely to work, typically IPv4 relay.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000684// RFC 5245
685// The value of |component_id| currently supported are 1 (RTP) and 2 (RTCP).
686// TODO: Decide the default destination in webrtcsession and
687// pass it down via SessionDescription.
pthatcher@webrtc.orgc9d6d142014-10-23 23:37:22 +0000688static void GetDefaultDestination(
689 const std::vector<Candidate>& candidates,
690 int component_id, std::string* port,
691 std::string* ip, std::string* addr_type) {
perkj@webrtc.orgd105cc82014-11-07 11:22:06 +0000692 *addr_type = kConnectionIpv4Addrtype;
pthatcher@webrtc.orgc9d6d142014-10-23 23:37:22 +0000693 *port = kDummyPort;
694 *ip = kDummyAddress;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000695 int current_preference = kPreferenceUnknown;
guoweis@webrtc.org57ac2c82015-02-06 00:45:13 +0000696 int current_family = AF_UNSPEC;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000697 for (std::vector<Candidate>::const_iterator it = candidates.begin();
698 it != candidates.end(); ++it) {
699 if (it->component() != component_id) {
700 continue;
701 }
guoweis@webrtc.org57ac2c82015-02-06 00:45:13 +0000702 // Default destination should be UDP only.
703 if (it->protocol() != cricket::UDP_PROTOCOL_NAME) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000704 continue;
705 }
guoweis@webrtc.org57ac2c82015-02-06 00:45:13 +0000706 const int preference = GetCandidatePreferenceFromType(it->type());
707 const int family = it->address().ipaddr().family();
708 // See if this candidate is more preferable then the current one if it's the
709 // same family. Or if the current family is IPv4 already so we could safely
710 // ignore all IPv6 ones. WebRTC bug 4269.
711 // http://code.google.com/p/webrtc/issues/detail?id=4269
712 if ((preference <= current_preference && current_family == family) ||
713 (current_family == AF_INET && family == AF_INET6)) {
714 continue;
715 }
pthatcher@webrtc.orgc9d6d142014-10-23 23:37:22 +0000716 if (family == AF_INET) {
717 addr_type->assign(kConnectionIpv4Addrtype);
718 } else if (family == AF_INET6) {
719 addr_type->assign(kConnectionIpv6Addrtype);
720 }
guoweis@webrtc.org57ac2c82015-02-06 00:45:13 +0000721 current_preference = preference;
722 current_family = family;
723 *port = it->address().PortAsString();
724 *ip = it->address().ipaddr().ToString();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000725 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000726}
727
wu@webrtc.org4c3e9912014-07-16 21:03:13 +0000728// Update |mline|'s default destination and append a c line after it.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000729static void UpdateMediaDefaultDestination(
wu@webrtc.org4c3e9912014-07-16 21:03:13 +0000730 const std::vector<Candidate>& candidates,
731 const std::string mline,
732 std::string* message) {
733 std::string new_lines;
734 AddLine(mline, &new_lines);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000735 // RFC 4566
736 // m=<media> <port> <proto> <fmt> ...
737 std::vector<std::string> fields;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000738 rtc::split(mline, kSdpDelimiterSpace, &fields);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000739 if (fields.size() < 3) {
740 return;
741 }
742
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000743 std::ostringstream os;
pthatcher@webrtc.orgc9d6d142014-10-23 23:37:22 +0000744 std::string rtp_port, rtp_ip, addr_type;
745 GetDefaultDestination(candidates, ICE_CANDIDATE_COMPONENT_RTP,
746 &rtp_port, &rtp_ip, &addr_type);
747 // Found default RTP candidate.
748 // RFC 5245
749 // The default candidates are added to the SDP as the default
750 // destination for media. For streams based on RTP, this is done by
751 // placing the IP address and port of the RTP candidate into the c and m
752 // lines, respectively.
753 // Update the port in the m line.
754 // If this is a m-line with port equal to 0, we don't change it.
755 if (fields[1] != kMediaPortRejected) {
756 new_lines.replace(fields[0].size() + 1,
757 fields[1].size(),
758 rtp_port);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000759 }
pthatcher@webrtc.orgc9d6d142014-10-23 23:37:22 +0000760 // Add the c line.
761 // RFC 4566
762 // c=<nettype> <addrtype> <connection-address>
763 InitLine(kLineTypeConnection, kConnectionNettype, &os);
764 os << " " << addr_type << " " << rtp_ip;
765 AddLine(os.str(), &new_lines);
wu@webrtc.org4c3e9912014-07-16 21:03:13 +0000766 message->append(new_lines);
767}
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000768
wu@webrtc.org4c3e9912014-07-16 21:03:13 +0000769// Gets "a=rtcp" line if found default RTCP candidate from |candidates|.
770static std::string GetRtcpLine(const std::vector<Candidate>& candidates) {
pthatcher@webrtc.orgc9d6d142014-10-23 23:37:22 +0000771 std::string rtcp_line, rtcp_port, rtcp_ip, addr_type;
772 GetDefaultDestination(candidates, ICE_CANDIDATE_COMPONENT_RTCP,
773 &rtcp_port, &rtcp_ip, &addr_type);
774 // Found default RTCP candidate.
775 // RFC 5245
776 // If the agent is utilizing RTCP, it MUST encode the RTCP candidate
777 // using the a=rtcp attribute as defined in RFC 3605.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000778
pthatcher@webrtc.orgc9d6d142014-10-23 23:37:22 +0000779 // RFC 3605
780 // rtcp-attribute = "a=rtcp:" port [nettype space addrtype space
781 // connection-address] CRLF
782 std::ostringstream os;
783 InitAttrLine(kAttributeRtcp, &os);
784 os << kSdpDelimiterColon
785 << rtcp_port << " "
786 << kConnectionNettype << " "
787 << addr_type << " "
788 << rtcp_ip;
789 rtcp_line = os.str();
wu@webrtc.org4c3e9912014-07-16 21:03:13 +0000790 return rtcp_line;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000791}
792
793// Get candidates according to the mline index from SessionDescriptionInterface.
794static void GetCandidatesByMindex(const SessionDescriptionInterface& desci,
795 int mline_index,
796 std::vector<Candidate>* candidates) {
797 if (!candidates) {
798 return;
799 }
800 const IceCandidateCollection* cc = desci.candidates(mline_index);
801 for (size_t i = 0; i < cc->count(); ++i) {
802 const IceCandidateInterface* candidate = cc->at(i);
803 candidates->push_back(candidate->candidate());
804 }
805}
806
807std::string SdpSerialize(const JsepSessionDescription& jdesc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000808 const cricket::SessionDescription* desc = jdesc.description();
809 if (!desc) {
810 return "";
811 }
812
813 std::string message;
814
815 // Session Description.
816 AddLine(kSessionVersion, &message);
817 // Session Origin
818 // RFC 4566
819 // o=<username> <sess-id> <sess-version> <nettype> <addrtype>
820 // <unicast-address>
821 std::ostringstream os;
822 InitLine(kLineTypeOrigin, kSessionOriginUsername, &os);
823 const std::string session_id = jdesc.session_id().empty() ?
824 kSessionOriginSessionId : jdesc.session_id();
825 const std::string session_version = jdesc.session_version().empty() ?
826 kSessionOriginSessionVersion : jdesc.session_version();
827 os << " " << session_id << " " << session_version << " "
828 << kSessionOriginNettype << " " << kSessionOriginAddrtype << " "
829 << kSessionOriginAddress;
830 AddLine(os.str(), &message);
831 AddLine(kSessionName, &message);
832
833 // Time Description.
834 AddLine(kTimeDescription, &message);
835
836 // Group
837 if (desc->HasGroup(cricket::GROUP_TYPE_BUNDLE)) {
838 std::string group_line = kAttrGroup;
839 const cricket::ContentGroup* group =
840 desc->GetGroupByName(cricket::GROUP_TYPE_BUNDLE);
841 ASSERT(group != NULL);
842 const cricket::ContentNames& content_names = group->content_names();
843 for (cricket::ContentNames::const_iterator it = content_names.begin();
844 it != content_names.end(); ++it) {
845 group_line.append(" ");
846 group_line.append(*it);
847 }
848 AddLine(group_line, &message);
849 }
850
851 // MediaStream semantics
852 InitAttrLine(kAttributeMsidSemantics, &os);
853 os << kSdpDelimiterColon << " " << kMediaStreamSemantic;
wu@webrtc.org4c3e9912014-07-16 21:03:13 +0000854
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000855 std::set<std::string> media_stream_labels;
856 const ContentInfo* audio_content = GetFirstAudioContent(desc);
857 if (audio_content)
858 GetMediaStreamLabels(audio_content, &media_stream_labels);
wu@webrtc.org4c3e9912014-07-16 21:03:13 +0000859
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000860 const ContentInfo* video_content = GetFirstVideoContent(desc);
861 if (video_content)
862 GetMediaStreamLabels(video_content, &media_stream_labels);
wu@webrtc.org4c3e9912014-07-16 21:03:13 +0000863
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000864 for (std::set<std::string>::const_iterator it =
865 media_stream_labels.begin(); it != media_stream_labels.end(); ++it) {
866 os << " " << *it;
867 }
868 AddLine(os.str(), &message);
869
wu@webrtc.org4c3e9912014-07-16 21:03:13 +0000870 // Preserve the order of the media contents.
871 int mline_index = -1;
872 for (cricket::ContentInfos::const_iterator it = desc->contents().begin();
873 it != desc->contents().end(); ++it) {
874 const MediaContentDescription* mdesc =
875 static_cast<const MediaContentDescription*>(it->description);
876 std::vector<Candidate> candidates;
877 GetCandidatesByMindex(jdesc, ++mline_index, &candidates);
878 BuildMediaDescription(&*it,
879 desc->GetTransportInfoByName(it->name),
880 mdesc->type(),
881 candidates,
882 &message);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000883 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000884 return message;
885}
886
887// Serializes the passed in IceCandidateInterface to a SDP string.
888// candidate - The candidate to be serialized.
889std::string SdpSerializeCandidate(
890 const IceCandidateInterface& candidate) {
891 std::string message;
892 std::vector<cricket::Candidate> candidates;
893 candidates.push_back(candidate.candidate());
894 BuildCandidate(candidates, &message);
wu@webrtc.orgec9f5fb2014-06-24 17:05:10 +0000895 // From WebRTC draft section 4.8.1.1 candidate-attribute will be
896 // just candidate:<candidate> not a=candidate:<blah>CRLF
897 ASSERT(message.find("a=") == 0);
898 message.erase(0, 2);
899 ASSERT(message.find(kLineBreak) == message.size() - 2);
900 message.resize(message.size() - 2);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000901 return message;
902}
903
904bool SdpDeserialize(const std::string& message,
905 JsepSessionDescription* jdesc,
906 SdpParseError* error) {
907 std::string session_id;
908 std::string session_version;
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +0000909 TransportDescription session_td(NS_JINGLE_ICE_UDP,
910 std::string(), std::string());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000911 RtpHeaderExtensions session_extmaps;
912 cricket::SessionDescription* desc = new cricket::SessionDescription();
913 std::vector<JsepIceCandidate*> candidates;
914 size_t current_pos = 0;
915 bool supports_msid = false;
916
917 // Session Description
918 if (!ParseSessionDescription(message, &current_pos, &session_id,
919 &session_version, &supports_msid, &session_td,
920 &session_extmaps, desc, error)) {
921 delete desc;
922 return false;
923 }
924
925 // Media Description
926 if (!ParseMediaDescription(message, session_td, session_extmaps,
927 supports_msid, &current_pos, desc, &candidates,
928 error)) {
929 delete desc;
930 for (std::vector<JsepIceCandidate*>::const_iterator
931 it = candidates.begin(); it != candidates.end(); ++it) {
932 delete *it;
933 }
934 return false;
935 }
936
937 jdesc->Initialize(desc, session_id, session_version);
938
939 for (std::vector<JsepIceCandidate*>::const_iterator
940 it = candidates.begin(); it != candidates.end(); ++it) {
941 jdesc->AddCandidate(*it);
942 delete *it;
943 }
944 return true;
945}
946
947bool SdpDeserializeCandidate(const std::string& message,
948 JsepIceCandidate* jcandidate,
949 SdpParseError* error) {
950 ASSERT(jcandidate != NULL);
951 Candidate candidate;
952 if (!ParseCandidate(message, &candidate, error, true)) {
953 return false;
954 }
955 jcandidate->SetCandidate(candidate);
956 return true;
957}
958
959bool ParseCandidate(const std::string& message, Candidate* candidate,
960 SdpParseError* error, bool is_raw) {
961 ASSERT(candidate != NULL);
962
963 // Get the first line from |message|.
jiayl@webrtc.org7ec3f9f2014-08-08 23:09:15 +0000964 std::string first_line = message;
965 size_t pos = 0;
966 GetLine(message, &pos, &first_line);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000967
jiayl@webrtc.org7ec3f9f2014-08-08 23:09:15 +0000968 // Makes sure |message| contains only one line.
969 if (message.size() > first_line.size()) {
970 std::string left, right;
971 if (SplitByDelimiter(message, kNewLine, &left, &right) && !right.empty()) {
972 return ParseFailed(message, 0, "Expect one line only", error);
973 }
974 }
975
976 // From WebRTC draft section 4.8.1.1 candidate-attribute should be
977 // candidate:<candidate> when trickled, but we still support
978 // a=candidate:<blah>CRLF for backward compatibility and for parsing a line
979 // from the SDP.
980 if (IsLineType(first_line, kLineTypeAttributes)) {
981 first_line = first_line.substr(kLinePrefixLength);
982 }
983
984 std::string attribute_candidate;
985 std::string candidate_value;
986
987 // |first_line| must be in the form of "candidate:<value>".
988 if (!SplitByDelimiter(first_line, kSdpDelimiterColon,
989 &attribute_candidate, &candidate_value) ||
990 attribute_candidate != kAttributeCandidate) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000991 if (is_raw) {
992 std::ostringstream description;
jiayl@webrtc.org7ec3f9f2014-08-08 23:09:15 +0000993 description << "Expect line: " << kAttributeCandidate
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000994 << ":" << "<candidate-str>";
995 return ParseFailed(first_line, 0, description.str(), error);
996 } else {
997 return ParseFailedExpectLine(first_line, 0, kLineTypeAttributes,
998 kAttributeCandidate, error);
999 }
1000 }
1001
1002 std::vector<std::string> fields;
jiayl@webrtc.org7ec3f9f2014-08-08 23:09:15 +00001003 rtc::split(candidate_value, kSdpDelimiterSpace, &fields);
1004
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001005 // RFC 5245
1006 // a=candidate:<foundation> <component-id> <transport> <priority>
1007 // <connection-address> <port> typ <candidate-types>
1008 // [raddr <connection-address>] [rport <port>]
1009 // *(SP extension-att-name SP extension-att-value)
1010 const size_t expected_min_fields = 8;
1011 if (fields.size() < expected_min_fields ||
1012 (fields[6] != kAttributeCandidateTyp)) {
1013 return ParseFailedExpectMinFieldNum(first_line, expected_min_fields, error);
1014 }
jiayl@webrtc.org7ec3f9f2014-08-08 23:09:15 +00001015 std::string foundation = fields[0];
1016
wu@webrtc.org5e760e72014-04-02 23:19:09 +00001017 int component_id = 0;
1018 if (!GetValueFromString(first_line, fields[1], &component_id, error)) {
1019 return false;
1020 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001021 const std::string transport = fields[2];
wu@webrtc.org5e760e72014-04-02 23:19:09 +00001022 uint32 priority = 0;
1023 if (!GetValueFromString(first_line, fields[3], &priority, error)) {
1024 return false;
1025 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001026 const std::string connection_address = fields[4];
wu@webrtc.org5e760e72014-04-02 23:19:09 +00001027 int port = 0;
1028 if (!GetValueFromString(first_line, fields[5], &port, error)) {
1029 return false;
1030 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001031 SocketAddress address(connection_address, port);
1032
1033 cricket::ProtocolType protocol;
1034 if (!StringToProto(transport.c_str(), &protocol)) {
1035 return ParseFailed(first_line, "Unsupported transport type.", error);
1036 }
1037
1038 std::string candidate_type;
1039 const std::string type = fields[7];
1040 if (type == kCandidateHost) {
1041 candidate_type = cricket::LOCAL_PORT_TYPE;
1042 } else if (type == kCandidateSrflx) {
1043 candidate_type = cricket::STUN_PORT_TYPE;
1044 } else if (type == kCandidateRelay) {
1045 candidate_type = cricket::RELAY_PORT_TYPE;
1046 } else {
1047 return ParseFailed(first_line, "Unsupported candidate type.", error);
1048 }
1049
1050 size_t current_position = expected_min_fields;
1051 SocketAddress related_address;
1052 // The 2 optional fields for related address
1053 // [raddr <connection-address>] [rport <port>]
1054 if (fields.size() >= (current_position + 2) &&
1055 fields[current_position] == kAttributeCandidateRaddr) {
1056 related_address.SetIP(fields[++current_position]);
1057 ++current_position;
1058 }
1059 if (fields.size() >= (current_position + 2) &&
1060 fields[current_position] == kAttributeCandidateRport) {
wu@webrtc.org5e760e72014-04-02 23:19:09 +00001061 int port = 0;
1062 if (!GetValueFromString(
1063 first_line, fields[++current_position], &port, error)) {
1064 return false;
1065 }
1066 related_address.SetPort(port);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001067 ++current_position;
1068 }
1069
mallinath@webrtc.org2d60c5e2014-08-08 22:29:20 +00001070 // If this is a TCP candidate, it has additional extension as defined in
1071 // RFC 6544.
1072 std::string tcptype;
1073 if (fields.size() >= (current_position + 2) &&
1074 fields[current_position] == kTcpCandidateType) {
1075 tcptype = fields[++current_position];
1076 ++current_position;
1077
1078 if (tcptype != cricket::TCPTYPE_ACTIVE_STR &&
1079 tcptype != cricket::TCPTYPE_PASSIVE_STR &&
1080 tcptype != cricket::TCPTYPE_SIMOPEN_STR) {
1081 return ParseFailed(first_line, "Invalid TCP candidate type.", error);
1082 }
1083
1084 if (protocol != cricket::PROTO_TCP) {
1085 return ParseFailed(first_line, "Invalid non-TCP candidate", error);
1086 }
1087 }
1088
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001089 // Extension
1090 // Empty string as the candidate username and password.
1091 // Will be updated later with the ice-ufrag and ice-pwd.
1092 // TODO: Remove the username/password extension, which is currently
1093 // kept for backwards compatibility.
1094 std::string username;
1095 std::string password;
1096 uint32 generation = 0;
1097 for (size_t i = current_position; i + 1 < fields.size(); ++i) {
1098 // RFC 5245
1099 // *(SP extension-att-name SP extension-att-value)
1100 if (fields[i] == kAttributeCandidateGeneration) {
wu@webrtc.org5e760e72014-04-02 23:19:09 +00001101 if (!GetValueFromString(first_line, fields[++i], &generation, error)) {
1102 return false;
1103 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001104 } else if (fields[i] == kAttributeCandidateUsername) {
1105 username = fields[++i];
1106 } else if (fields[i] == kAttributeCandidatePassword) {
1107 password = fields[++i];
1108 } else {
1109 // Skip the unknown extension.
1110 ++i;
1111 }
1112 }
1113
guoweis@webrtc.org61c12472015-01-15 06:53:07 +00001114 *candidate = Candidate(component_id, cricket::ProtoToString(protocol),
guoweis@webrtc.org950c5182014-12-16 23:01:31 +00001115 address, priority, username, password, candidate_type,
1116 generation, foundation);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001117 candidate->set_related_address(related_address);
mallinath@webrtc.org2d60c5e2014-08-08 22:29:20 +00001118 candidate->set_tcptype(tcptype);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001119 return true;
1120}
1121
1122bool ParseIceOptions(const std::string& line,
1123 std::vector<std::string>* transport_options,
1124 SdpParseError* error) {
1125 std::string ice_options;
1126 if (!GetValue(line, kAttributeIceOption, &ice_options, error)) {
1127 return false;
1128 }
1129 std::vector<std::string> fields;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001130 rtc::split(ice_options, kSdpDelimiterSpace, &fields);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001131 for (size_t i = 0; i < fields.size(); ++i) {
1132 transport_options->push_back(fields[i]);
1133 }
1134 return true;
1135}
1136
jiayl@webrtc.orgddb85ab2014-09-05 16:31:56 +00001137bool ParseSctpPort(const std::string& line,
1138 int* sctp_port,
1139 SdpParseError* error) {
1140 // draft-ietf-mmusic-sctp-sdp-07
1141 // a=sctp-port
1142 std::vector<std::string> fields;
1143 rtc::split(line.substr(kLinePrefixLength),
1144 kSdpDelimiterSpace, &fields);
1145 const size_t expected_min_fields = 2;
1146 if (fields.size() < expected_min_fields) {
1147 return ParseFailedExpectMinFieldNum(line, expected_min_fields, error);
1148 }
1149 if (!rtc::FromString(fields[1], sctp_port)) {
1150 return ParseFailed(line,
1151 "Invalid sctp port value.",
1152 error);
1153 }
1154 return true;
1155}
1156
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001157bool ParseExtmap(const std::string& line, RtpHeaderExtension* extmap,
1158 SdpParseError* error) {
1159 // RFC 5285
1160 // a=extmap:<value>["/"<direction>] <URI> <extensionattributes>
1161 std::vector<std::string> fields;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001162 rtc::split(line.substr(kLinePrefixLength),
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001163 kSdpDelimiterSpace, &fields);
1164 const size_t expected_min_fields = 2;
1165 if (fields.size() < expected_min_fields) {
1166 return ParseFailedExpectMinFieldNum(line, expected_min_fields, error);
1167 }
1168 std::string uri = fields[1];
1169
1170 std::string value_direction;
1171 if (!GetValue(fields[0], kAttributeExtmap, &value_direction, error)) {
1172 return false;
1173 }
1174 std::vector<std::string> sub_fields;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001175 rtc::split(value_direction, kSdpDelimiterSlash, &sub_fields);
wu@webrtc.org5e760e72014-04-02 23:19:09 +00001176 int value = 0;
1177 if (!GetValueFromString(line, sub_fields[0], &value, error)) {
1178 return false;
1179 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001180
1181 *extmap = RtpHeaderExtension(uri, value);
1182 return true;
1183}
1184
1185void BuildMediaDescription(const ContentInfo* content_info,
1186 const TransportInfo* transport_info,
1187 const MediaType media_type,
wu@webrtc.org4c3e9912014-07-16 21:03:13 +00001188 const std::vector<Candidate>& candidates,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001189 std::string* message) {
1190 ASSERT(message != NULL);
1191 if (content_info == NULL || message == NULL) {
1192 return;
1193 }
1194 // TODO: Rethink if we should use sprintfn instead of stringstream.
1195 // According to the style guide, streams should only be used for logging.
1196 // http://google-styleguide.googlecode.com/svn/
1197 // trunk/cppguide.xml?showone=Streams#Streams
1198 std::ostringstream os;
1199 const MediaContentDescription* media_desc =
henrike@webrtc.org28654cb2013-07-22 21:07:49 +00001200 static_cast<const MediaContentDescription*>(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001201 content_info->description);
1202 ASSERT(media_desc != NULL);
1203
jiayl@webrtc.org9c16c392014-05-01 18:30:30 +00001204 int sctp_port = cricket::kSctpDefaultPort;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001205
1206 // RFC 4566
1207 // m=<media> <port> <proto> <fmt>
1208 // fmt is a list of payload type numbers that MAY be used in the session.
1209 const char* type = NULL;
1210 if (media_type == cricket::MEDIA_TYPE_AUDIO)
1211 type = kMediaTypeAudio;
1212 else if (media_type == cricket::MEDIA_TYPE_VIDEO)
1213 type = kMediaTypeVideo;
1214 else if (media_type == cricket::MEDIA_TYPE_DATA)
1215 type = kMediaTypeData;
1216 else
1217 ASSERT(false);
1218
1219 std::string fmt;
1220 if (media_type == cricket::MEDIA_TYPE_VIDEO) {
1221 const VideoContentDescription* video_desc =
1222 static_cast<const VideoContentDescription*>(media_desc);
1223 for (std::vector<cricket::VideoCodec>::const_iterator it =
1224 video_desc->codecs().begin();
1225 it != video_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_AUDIO) {
1230 const AudioContentDescription* audio_desc =
1231 static_cast<const AudioContentDescription*>(media_desc);
1232 for (std::vector<cricket::AudioCodec>::const_iterator it =
1233 audio_desc->codecs().begin();
1234 it != audio_desc->codecs().end(); ++it) {
1235 fmt.append(" ");
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001236 fmt.append(rtc::ToString<int>(it->id));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001237 }
1238 } else if (media_type == cricket::MEDIA_TYPE_DATA) {
jiayl@webrtc.org9c16c392014-05-01 18:30:30 +00001239 const DataContentDescription* data_desc =
1240 static_cast<const DataContentDescription*>(media_desc);
pthatcher@webrtc.org3341b402015-02-13 21:14:22 +00001241 if (IsDtlsSctp(media_desc->protocol())) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001242 fmt.append(" ");
jiayl@webrtc.org9c16c392014-05-01 18:30:30 +00001243
1244 for (std::vector<cricket::DataCodec>::const_iterator it =
1245 data_desc->codecs().begin();
1246 it != data_desc->codecs().end(); ++it) {
1247 if (it->id == cricket::kGoogleSctpDataCodecId &&
1248 it->GetParam(cricket::kCodecParamPort, &sctp_port)) {
1249 break;
1250 }
1251 }
1252
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001253 fmt.append(rtc::ToString<int>(sctp_port));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001254 } else {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001255 for (std::vector<cricket::DataCodec>::const_iterator it =
1256 data_desc->codecs().begin();
1257 it != data_desc->codecs().end(); ++it) {
1258 fmt.append(" ");
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001259 fmt.append(rtc::ToString<int>(it->id));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001260 }
1261 }
1262 }
1263 // The fmt must never be empty. If no codecs are found, set the fmt attribute
1264 // to 0.
1265 if (fmt.empty()) {
1266 fmt = " 0";
1267 }
1268
1269 // The port number in the m line will be updated later when associate with
1270 // the candidates.
1271 // RFC 3264
1272 // To reject an offered stream, the port number in the corresponding stream in
1273 // the answer MUST be set to zero.
1274 const std::string port = content_info->rejected ?
pthatcher@webrtc.orgc9d6d142014-10-23 23:37:22 +00001275 kMediaPortRejected : kDummyPort;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001276
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001277 rtc::SSLFingerprint* fp = (transport_info) ?
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001278 transport_info->description.identity_fingerprint.get() : NULL;
1279
wu@webrtc.org4c3e9912014-07-16 21:03:13 +00001280 // Add the m and c lines.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001281 InitLine(kLineTypeMedia, type, &os);
1282 os << " " << port << " " << media_desc->protocol() << fmt;
wu@webrtc.org4c3e9912014-07-16 21:03:13 +00001283 std::string mline = os.str();
1284 UpdateMediaDefaultDestination(candidates, mline, message);
1285
1286 // RFC 4566
1287 // b=AS:<bandwidth>
1288 // We should always use the default bandwidth for RTP-based data
1289 // channels. Don't allow SDP to set the bandwidth, because that
1290 // would give JS the opportunity to "break the Internet".
1291 // TODO(pthatcher): But we need to temporarily allow the SDP to control
1292 // this for backwards-compatibility. Once we don't need that any
1293 // more, remove this.
1294 bool support_dc_sdp_bandwidth_temporarily = true;
1295 if (media_desc->bandwidth() >= 1000 &&
1296 (media_type != cricket::MEDIA_TYPE_DATA ||
1297 support_dc_sdp_bandwidth_temporarily)) {
1298 InitLine(kLineTypeSessionBandwidth, kApplicationSpecificMaximum, &os);
1299 os << kSdpDelimiterColon << (media_desc->bandwidth() / 1000);
1300 AddLine(os.str(), message);
1301 }
1302
1303 // Add the a=rtcp line.
pthatcher@webrtc.org3341b402015-02-13 21:14:22 +00001304 if (IsRtp(media_desc->protocol())) {
wu@webrtc.org4c3e9912014-07-16 21:03:13 +00001305 std::string rtcp_line = GetRtcpLine(candidates);
1306 if (!rtcp_line.empty()) {
1307 AddLine(rtcp_line, message);
1308 }
1309 }
1310
1311 // Build the a=candidate lines.
1312 BuildCandidate(candidates, message);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001313
1314 // Use the transport_info to build the media level ice-ufrag and ice-pwd.
1315 if (transport_info) {
1316 // RFC 5245
1317 // ice-pwd-att = "ice-pwd" ":" password
1318 // ice-ufrag-att = "ice-ufrag" ":" ufrag
1319 // ice-ufrag
1320 InitAttrLine(kAttributeIceUfrag, &os);
1321 os << kSdpDelimiterColon << transport_info->description.ice_ufrag;
1322 AddLine(os.str(), message);
1323 // ice-pwd
1324 InitAttrLine(kAttributeIcePwd, &os);
1325 os << kSdpDelimiterColon << transport_info->description.ice_pwd;
1326 AddLine(os.str(), message);
1327
1328 // draft-petithuguenin-mmusic-ice-attributes-level-03
1329 BuildIceOptions(transport_info->description.transport_options, message);
1330
1331 // RFC 4572
1332 // fingerprint-attribute =
1333 // "fingerprint" ":" hash-func SP fingerprint
1334 if (fp) {
1335 // Insert the fingerprint attribute.
1336 InitAttrLine(kAttributeFingerprint, &os);
1337 os << kSdpDelimiterColon
1338 << fp->algorithm << kSdpDelimiterSpace
1339 << fp->GetRfc4572Fingerprint();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001340 AddLine(os.str(), message);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001341
1342 // Inserting setup attribute.
1343 if (transport_info->description.connection_role !=
1344 cricket::CONNECTIONROLE_NONE) {
1345 // Making sure we are not using "passive" mode.
1346 cricket::ConnectionRole role =
1347 transport_info->description.connection_role;
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00001348 std::string dtls_role_str;
1349 VERIFY(cricket::ConnectionRoleToString(role, &dtls_role_str));
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001350 InitAttrLine(kAttributeSetup, &os);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001351 os << kSdpDelimiterColon << dtls_role_str;
1352 AddLine(os.str(), message);
1353 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001354 }
1355 }
1356
1357 // RFC 3388
1358 // mid-attribute = "a=mid:" identification-tag
1359 // identification-tag = token
1360 // Use the content name as the mid identification-tag.
1361 InitAttrLine(kAttributeMid, &os);
1362 os << kSdpDelimiterColon << content_info->name;
1363 AddLine(os.str(), message);
1364
pthatcher@webrtc.org3341b402015-02-13 21:14:22 +00001365 if (IsDtlsSctp(media_desc->protocol())) {
jiayl@webrtc.org9c16c392014-05-01 18:30:30 +00001366 BuildSctpContentAttributes(message, sctp_port);
pthatcher@webrtc.org3341b402015-02-13 21:14:22 +00001367 } else if (IsRtp(media_desc->protocol())) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001368 BuildRtpContentAttributes(media_desc, media_type, message);
1369 }
1370}
1371
jiayl@webrtc.org9c16c392014-05-01 18:30:30 +00001372void BuildSctpContentAttributes(std::string* message, int sctp_port) {
wu@webrtc.org78187522013-10-07 23:32:02 +00001373 // draft-ietf-mmusic-sctp-sdp-04
1374 // a=sctpmap:sctpmap-number protocol [streams]
1375 std::ostringstream os;
1376 InitAttrLine(kAttributeSctpmap, &os);
jiayl@webrtc.org9c16c392014-05-01 18:30:30 +00001377 os << kSdpDelimiterColon << sctp_port << kSdpDelimiterSpace
wu@webrtc.org78187522013-10-07 23:32:02 +00001378 << kDefaultSctpmapProtocol << kSdpDelimiterSpace
1379 << (cricket::kMaxSctpSid + 1);
1380 AddLine(os.str(), message);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001381}
1382
1383void BuildRtpContentAttributes(
1384 const MediaContentDescription* media_desc,
1385 const MediaType media_type,
1386 std::string* message) {
1387 std::ostringstream os;
1388 // RFC 5285
1389 // a=extmap:<value>["/"<direction>] <URI> <extensionattributes>
1390 // The definitions MUST be either all session level or all media level. This
1391 // implementation uses all media level.
1392 for (size_t i = 0; i < media_desc->rtp_header_extensions().size(); ++i) {
1393 InitAttrLine(kAttributeExtmap, &os);
1394 os << kSdpDelimiterColon << media_desc->rtp_header_extensions()[i].id
1395 << kSdpDelimiterSpace << media_desc->rtp_header_extensions()[i].uri;
1396 AddLine(os.str(), message);
1397 }
1398
1399 // RFC 3264
1400 // a=sendrecv || a=sendonly || a=sendrecv || a=inactive
1401
1402 cricket::MediaContentDirection direction = media_desc->direction();
1403 if (media_desc->streams().empty() && direction == cricket::MD_SENDRECV) {
1404 direction = cricket::MD_RECVONLY;
1405 }
1406
1407 switch (direction) {
1408 case cricket::MD_INACTIVE:
1409 InitAttrLine(kAttributeInactive, &os);
1410 break;
1411 case cricket::MD_SENDONLY:
1412 InitAttrLine(kAttributeSendOnly, &os);
1413 break;
1414 case cricket::MD_RECVONLY:
1415 InitAttrLine(kAttributeRecvOnly, &os);
1416 break;
1417 case cricket::MD_SENDRECV:
1418 default:
1419 InitAttrLine(kAttributeSendRecv, &os);
1420 break;
1421 }
1422 AddLine(os.str(), message);
1423
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001424 // RFC 5761
1425 // a=rtcp-mux
1426 if (media_desc->rtcp_mux()) {
1427 InitAttrLine(kAttributeRtcpMux, &os);
1428 AddLine(os.str(), message);
1429 }
1430
1431 // RFC 4568
1432 // a=crypto:<tag> <crypto-suite> <key-params> [<session-params>]
1433 for (std::vector<CryptoParams>::const_iterator it =
1434 media_desc->cryptos().begin();
1435 it != media_desc->cryptos().end(); ++it) {
1436 InitAttrLine(kAttributeCrypto, &os);
1437 os << kSdpDelimiterColon << it->tag << " " << it->cipher_suite << " "
1438 << it->key_params;
1439 if (!it->session_params.empty()) {
1440 os << " " << it->session_params;
1441 }
1442 AddLine(os.str(), message);
1443 }
1444
1445 // RFC 4566
1446 // a=rtpmap:<payload type> <encoding name>/<clock rate>
1447 // [/<encodingparameters>]
1448 BuildRtpMap(media_desc, media_type, message);
1449
1450 // Specify latency for buffered mode.
1451 // a=x-google-buffer-latency:<value>
1452 if (media_desc->buffered_mode_latency() != cricket::kBufferedModeDisabled) {
1453 std::ostringstream os;
1454 InitAttrLine(kAttributeXGoogleBufferLatency, &os);
1455 os << kSdpDelimiterColon << media_desc->buffered_mode_latency();
1456 AddLine(os.str(), message);
1457 }
1458
1459 for (StreamParamsVec::const_iterator track = media_desc->streams().begin();
1460 track != media_desc->streams().end(); ++track) {
1461 // Require that the track belongs to a media stream,
1462 // ie the sync_label is set. This extra check is necessary since the
1463 // MediaContentDescription always contains a streamparam with an ssrc even
1464 // if no track or media stream have been created.
1465 if (track->sync_label.empty()) continue;
1466
1467 // Build the ssrc-group lines.
1468 for (size_t i = 0; i < track->ssrc_groups.size(); ++i) {
1469 // RFC 5576
1470 // a=ssrc-group:<semantics> <ssrc-id> ...
1471 if (track->ssrc_groups[i].ssrcs.empty()) {
1472 continue;
1473 }
1474 std::ostringstream os;
1475 InitAttrLine(kAttributeSsrcGroup, &os);
1476 os << kSdpDelimiterColon << track->ssrc_groups[i].semantics;
1477 std::vector<uint32>::const_iterator ssrc =
1478 track->ssrc_groups[i].ssrcs.begin();
1479 for (; ssrc != track->ssrc_groups[i].ssrcs.end(); ++ssrc) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001480 os << kSdpDelimiterSpace << rtc::ToString<uint32>(*ssrc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001481 }
1482 AddLine(os.str(), message);
1483 }
1484 // Build the ssrc lines for each ssrc.
1485 for (size_t i = 0; i < track->ssrcs.size(); ++i) {
1486 uint32 ssrc = track->ssrcs[i];
1487 // RFC 5576
1488 // a=ssrc:<ssrc-id> cname:<value>
1489 AddSsrcLine(ssrc, kSsrcAttributeCname,
1490 track->cname, message);
1491
1492 // draft-alvestrand-mmusic-msid-00
1493 // a=ssrc:<ssrc-id> msid:identifier [appdata]
1494 // The appdata consists of the "id" attribute of a MediaStreamTrack, which
1495 // is corresponding to the "name" attribute of StreamParams.
1496 std::string appdata = track->id;
1497 std::ostringstream os;
1498 InitAttrLine(kAttributeSsrc, &os);
1499 os << kSdpDelimiterColon << ssrc << kSdpDelimiterSpace
1500 << kSsrcAttributeMsid << kSdpDelimiterColon << track->sync_label
1501 << kSdpDelimiterSpace << appdata;
1502 AddLine(os.str(), message);
1503
1504 // TODO(ronghuawu): Remove below code which is for backward compatibility.
1505 // draft-alvestrand-rtcweb-mid-01
1506 // a=ssrc:<ssrc-id> mslabel:<value>
1507 // The label isn't yet defined.
1508 // a=ssrc:<ssrc-id> label:<value>
1509 AddSsrcLine(ssrc, kSsrcAttributeMslabel, track->sync_label, message);
1510 AddSsrcLine(ssrc, kSSrcAttributeLabel, track->id, message);
1511 }
1512 }
1513}
1514
1515void WriteFmtpHeader(int payload_type, std::ostringstream* os) {
1516 // fmtp header: a=fmtp:|payload_type| <parameters>
1517 // Add a=fmtp
1518 InitAttrLine(kAttributeFmtp, os);
1519 // Add :|payload_type|
1520 *os << kSdpDelimiterColon << payload_type;
1521}
1522
1523void WriteRtcpFbHeader(int payload_type, std::ostringstream* os) {
1524 // rtcp-fb header: a=rtcp-fb:|payload_type|
1525 // <parameters>/<ccm <ccm_parameters>>
1526 // Add a=rtcp-fb
1527 InitAttrLine(kAttributeRtcpFb, os);
1528 // Add :
1529 *os << kSdpDelimiterColon;
1530 if (payload_type == kWildcardPayloadType) {
1531 *os << "*";
1532 } else {
1533 *os << payload_type;
1534 }
1535}
1536
1537void WriteFmtpParameter(const std::string& parameter_name,
1538 const std::string& parameter_value,
1539 std::ostringstream* os) {
1540 // fmtp parameters: |parameter_name|=|parameter_value|
1541 *os << parameter_name << kSdpDelimiterEqual << parameter_value;
1542}
1543
1544void WriteFmtpParameters(const cricket::CodecParameterMap& parameters,
1545 std::ostringstream* os) {
1546 for (cricket::CodecParameterMap::const_iterator fmtp = parameters.begin();
1547 fmtp != parameters.end(); ++fmtp) {
1548 // Each new parameter, except the first one starts with ";" and " ".
1549 if (fmtp != parameters.begin()) {
1550 *os << kSdpDelimiterSemicolon;
1551 }
1552 *os << kSdpDelimiterSpace;
1553 WriteFmtpParameter(fmtp->first, fmtp->second, os);
1554 }
1555}
1556
1557bool IsFmtpParam(const std::string& name) {
1558 const char* kFmtpParams[] = {
1559 kCodecParamMinPTime, kCodecParamSPropStereo,
buildbot@webrtc.orged97bb02014-05-07 11:15:20 +00001560 kCodecParamStereo, kCodecParamUseInbandFec, kCodecParamStartBitrate,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001561 kCodecParamMaxBitrate, kCodecParamMinBitrate, kCodecParamMaxQuantization,
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001562 kCodecParamSctpProtocol, kCodecParamSctpStreams,
buildbot@webrtc.org5d639b32014-09-10 07:57:12 +00001563 kCodecParamMaxAverageBitrate, kCodecParamMaxPlaybackRate,
1564 kCodecParamAssociatedPayloadType
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001565 };
1566 for (size_t i = 0; i < ARRAY_SIZE(kFmtpParams); ++i) {
1567 if (_stricmp(name.c_str(), kFmtpParams[i]) == 0) {
1568 return true;
1569 }
1570 }
1571 return false;
1572}
1573
1574// Retreives fmtp parameters from |params|, which may contain other parameters
1575// as well, and puts them in |fmtp_parameters|.
1576void GetFmtpParams(const cricket::CodecParameterMap& params,
1577 cricket::CodecParameterMap* fmtp_parameters) {
1578 for (cricket::CodecParameterMap::const_iterator iter = params.begin();
1579 iter != params.end(); ++iter) {
1580 if (IsFmtpParam(iter->first)) {
1581 (*fmtp_parameters)[iter->first] = iter->second;
1582 }
1583 }
1584}
1585
1586template <class T>
1587void AddFmtpLine(const T& codec, std::string* message) {
1588 cricket::CodecParameterMap fmtp_parameters;
1589 GetFmtpParams(codec.params, &fmtp_parameters);
1590 if (fmtp_parameters.empty()) {
1591 // No need to add an fmtp if it will have no (optional) parameters.
1592 return;
1593 }
1594 std::ostringstream os;
1595 WriteFmtpHeader(codec.id, &os);
1596 WriteFmtpParameters(fmtp_parameters, &os);
1597 AddLine(os.str(), message);
1598 return;
1599}
1600
1601template <class T>
1602void AddRtcpFbLines(const T& codec, std::string* message) {
1603 for (std::vector<cricket::FeedbackParam>::const_iterator iter =
1604 codec.feedback_params.params().begin();
1605 iter != codec.feedback_params.params().end(); ++iter) {
1606 std::ostringstream os;
1607 WriteRtcpFbHeader(codec.id, &os);
1608 os << " " << iter->id();
1609 if (!iter->param().empty()) {
1610 os << " " << iter->param();
1611 }
1612 AddLine(os.str(), message);
1613 }
1614}
1615
jiayl@webrtc.orgddb85ab2014-09-05 16:31:56 +00001616bool AddSctpDataCodec(DataContentDescription* media_desc,
1617 int sctp_port) {
1618 if (media_desc->HasCodec(cricket::kGoogleSctpDataCodecId)) {
1619 return ParseFailed("",
1620 "Can't have multiple sctp port attributes.",
1621 NULL);
1622 }
1623 // Add the SCTP Port number as a pseudo-codec "port" parameter
1624 cricket::DataCodec codec_port(
1625 cricket::kGoogleSctpDataCodecId, cricket::kGoogleSctpDataCodecName,
1626 0);
1627 codec_port.SetParam(cricket::kCodecParamPort, sctp_port);
1628 LOG(INFO) << "AddSctpDataCodec: Got SCTP Port Number "
1629 << sctp_port;
1630 media_desc->AddCodec(codec_port);
1631 return true;
1632}
1633
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001634bool GetMinValue(const std::vector<int>& values, int* value) {
1635 if (values.empty()) {
1636 return false;
1637 }
1638 std::vector<int>::const_iterator found =
1639 std::min_element(values.begin(), values.end());
1640 *value = *found;
1641 return true;
1642}
1643
1644bool GetParameter(const std::string& name,
1645 const cricket::CodecParameterMap& params, int* value) {
1646 std::map<std::string, std::string>::const_iterator found =
1647 params.find(name);
1648 if (found == params.end()) {
1649 return false;
1650 }
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001651 if (!rtc::FromString(found->second, value)) {
wu@webrtc.org5e760e72014-04-02 23:19:09 +00001652 return false;
1653 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001654 return true;
1655}
1656
1657void BuildRtpMap(const MediaContentDescription* media_desc,
1658 const MediaType media_type,
1659 std::string* message) {
1660 ASSERT(message != NULL);
1661 ASSERT(media_desc != NULL);
1662 std::ostringstream os;
1663 if (media_type == cricket::MEDIA_TYPE_VIDEO) {
1664 const VideoContentDescription* video_desc =
1665 static_cast<const VideoContentDescription*>(media_desc);
1666 for (std::vector<cricket::VideoCodec>::const_iterator it =
1667 video_desc->codecs().begin();
1668 it != video_desc->codecs().end(); ++it) {
1669 // RFC 4566
1670 // a=rtpmap:<payload type> <encoding name>/<clock rate>
1671 // [/<encodingparameters>]
1672 if (it->id != kWildcardPayloadType) {
1673 InitAttrLine(kAttributeRtpmap, &os);
1674 os << kSdpDelimiterColon << it->id << " " << it->name
1675 << "/" << kDefaultVideoClockrate;
1676 AddLine(os.str(), message);
1677 }
1678 AddRtcpFbLines(*it, message);
1679 AddFmtpLine(*it, message);
1680 }
1681 } else if (media_type == cricket::MEDIA_TYPE_AUDIO) {
1682 const AudioContentDescription* audio_desc =
1683 static_cast<const AudioContentDescription*>(media_desc);
1684 std::vector<int> ptimes;
1685 std::vector<int> maxptimes;
1686 int max_minptime = 0;
1687 for (std::vector<cricket::AudioCodec>::const_iterator it =
1688 audio_desc->codecs().begin();
1689 it != audio_desc->codecs().end(); ++it) {
1690 ASSERT(!it->name.empty());
1691 // RFC 4566
1692 // a=rtpmap:<payload type> <encoding name>/<clock rate>
1693 // [/<encodingparameters>]
1694 InitAttrLine(kAttributeRtpmap, &os);
1695 os << kSdpDelimiterColon << it->id << " ";
1696 os << it->name << "/" << it->clockrate;
1697 if (it->channels != 1) {
1698 os << "/" << it->channels;
1699 }
1700 AddLine(os.str(), message);
1701 AddRtcpFbLines(*it, message);
1702 AddFmtpLine(*it, message);
1703 int minptime = 0;
1704 if (GetParameter(kCodecParamMinPTime, it->params, &minptime)) {
1705 max_minptime = std::max(minptime, max_minptime);
1706 }
1707 int ptime;
1708 if (GetParameter(kCodecParamPTime, it->params, &ptime)) {
1709 ptimes.push_back(ptime);
1710 }
1711 int maxptime;
1712 if (GetParameter(kCodecParamMaxPTime, it->params, &maxptime)) {
1713 maxptimes.push_back(maxptime);
1714 }
1715 }
1716 // Populate the maxptime attribute with the smallest maxptime of all codecs
1717 // under the same m-line.
1718 int min_maxptime = INT_MAX;
1719 if (GetMinValue(maxptimes, &min_maxptime)) {
1720 AddAttributeLine(kCodecParamMaxPTime, min_maxptime, message);
1721 }
1722 ASSERT(min_maxptime > max_minptime);
1723 // Populate the ptime attribute with the smallest ptime or the largest
1724 // minptime, whichever is the largest, for all codecs under the same m-line.
1725 int ptime = INT_MAX;
1726 if (GetMinValue(ptimes, &ptime)) {
1727 ptime = std::min(ptime, min_maxptime);
1728 ptime = std::max(ptime, max_minptime);
1729 AddAttributeLine(kCodecParamPTime, ptime, message);
1730 }
1731 } else if (media_type == cricket::MEDIA_TYPE_DATA) {
1732 const DataContentDescription* data_desc =
1733 static_cast<const DataContentDescription*>(media_desc);
1734 for (std::vector<cricket::DataCodec>::const_iterator it =
1735 data_desc->codecs().begin();
1736 it != data_desc->codecs().end(); ++it) {
1737 // RFC 4566
1738 // a=rtpmap:<payload type> <encoding name>/<clock rate>
1739 // [/<encodingparameters>]
1740 InitAttrLine(kAttributeRtpmap, &os);
1741 os << kSdpDelimiterColon << it->id << " "
1742 << it->name << "/" << it->clockrate;
1743 AddLine(os.str(), message);
1744 }
1745 }
1746}
1747
1748void BuildCandidate(const std::vector<Candidate>& candidates,
1749 std::string* message) {
1750 std::ostringstream os;
1751
1752 for (std::vector<Candidate>::const_iterator it = candidates.begin();
1753 it != candidates.end(); ++it) {
1754 // RFC 5245
1755 // a=candidate:<foundation> <component-id> <transport> <priority>
1756 // <connection-address> <port> typ <candidate-types>
1757 // [raddr <connection-address>] [rport <port>]
1758 // *(SP extension-att-name SP extension-att-value)
1759 std::string type;
1760 // Map the cricket candidate type to "host" / "srflx" / "prflx" / "relay"
1761 if (it->type() == cricket::LOCAL_PORT_TYPE) {
1762 type = kCandidateHost;
1763 } else if (it->type() == cricket::STUN_PORT_TYPE) {
1764 type = kCandidateSrflx;
1765 } else if (it->type() == cricket::RELAY_PORT_TYPE) {
1766 type = kCandidateRelay;
1767 } else {
1768 ASSERT(false);
1769 }
1770
1771 InitAttrLine(kAttributeCandidate, &os);
1772 os << kSdpDelimiterColon
mallinath@webrtc.org2d60c5e2014-08-08 22:29:20 +00001773 << it->foundation() << " "
1774 << it->component() << " "
1775 << it->protocol() << " "
1776 << it->priority() << " "
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001777 << it->address().ipaddr().ToString() << " "
1778 << it->address().PortAsString() << " "
mallinath@webrtc.org2d60c5e2014-08-08 22:29:20 +00001779 << kAttributeCandidateTyp << " "
1780 << type << " ";
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001781
1782 // Related address
1783 if (!it->related_address().IsNil()) {
1784 os << kAttributeCandidateRaddr << " "
1785 << it->related_address().ipaddr().ToString() << " "
1786 << kAttributeCandidateRport << " "
1787 << it->related_address().PortAsString() << " ";
1788 }
1789
mallinath@webrtc.org2d60c5e2014-08-08 22:29:20 +00001790 if (it->protocol() == cricket::TCP_PROTOCOL_NAME) {
mallinath@webrtc.orge999bd02014-08-13 06:05:55 +00001791 os << kTcpCandidateType << " " << it->tcptype() << " ";
mallinath@webrtc.org2d60c5e2014-08-08 22:29:20 +00001792 }
1793
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001794 // Extensions
1795 os << kAttributeCandidateGeneration << " " << it->generation();
1796
1797 AddLine(os.str(), message);
1798 }
1799}
1800
1801void BuildIceOptions(const std::vector<std::string>& transport_options,
1802 std::string* message) {
1803 if (!transport_options.empty()) {
1804 std::ostringstream os;
1805 InitAttrLine(kAttributeIceOption, &os);
1806 os << kSdpDelimiterColon << transport_options[0];
1807 for (size_t i = 1; i < transport_options.size(); ++i) {
1808 os << kSdpDelimiterSpace << transport_options[i];
1809 }
1810 AddLine(os.str(), message);
1811 }
1812}
1813
pthatcher@webrtc.org3341b402015-02-13 21:14:22 +00001814bool IsRtp(const std::string& protocol) {
1815 return protocol.empty() ||
1816 (protocol.find(cricket::kMediaProtocolRtpPrefix) != std::string::npos);
1817}
1818
1819bool IsDtlsSctp(const std::string& protocol) {
1820 // This intentionally excludes "SCTP" and "SCTP/DTLS".
1821 return protocol.find(cricket::kMediaProtocolDtlsSctp) != std::string::npos;
1822}
1823
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001824bool ParseSessionDescription(const std::string& message, size_t* pos,
1825 std::string* session_id,
1826 std::string* session_version,
1827 bool* supports_msid,
1828 TransportDescription* session_td,
1829 RtpHeaderExtensions* session_extmaps,
1830 cricket::SessionDescription* desc,
1831 SdpParseError* error) {
1832 std::string line;
1833
1834 // RFC 4566
1835 // v= (protocol version)
1836 if (!GetLineWithType(message, pos, &line, kLineTypeVersion)) {
1837 return ParseFailedExpectLine(message, *pos, kLineTypeVersion,
1838 std::string(), error);
1839 }
1840 // RFC 4566
1841 // o=<username> <sess-id> <sess-version> <nettype> <addrtype>
1842 // <unicast-address>
1843 if (!GetLineWithType(message, pos, &line, kLineTypeOrigin)) {
1844 return ParseFailedExpectLine(message, *pos, kLineTypeOrigin,
1845 std::string(), error);
1846 }
1847 std::vector<std::string> fields;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001848 rtc::split(line.substr(kLinePrefixLength),
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001849 kSdpDelimiterSpace, &fields);
1850 const size_t expected_fields = 6;
1851 if (fields.size() != expected_fields) {
1852 return ParseFailedExpectFieldNum(line, expected_fields, error);
1853 }
1854 *session_id = fields[1];
1855 *session_version = fields[2];
1856
1857 // RFC 4566
1858 // s= (session name)
1859 if (!GetLineWithType(message, pos, &line, kLineTypeSessionName)) {
1860 return ParseFailedExpectLine(message, *pos, kLineTypeSessionName,
1861 std::string(), error);
1862 }
1863
1864 // Optional lines
1865 // Those are the optional lines, so shouldn't return false if not present.
1866 // RFC 4566
1867 // i=* (session information)
1868 GetLineWithType(message, pos, &line, kLineTypeSessionInfo);
1869
1870 // RFC 4566
1871 // u=* (URI of description)
1872 GetLineWithType(message, pos, &line, kLineTypeSessionUri);
1873
1874 // RFC 4566
1875 // e=* (email address)
1876 GetLineWithType(message, pos, &line, kLineTypeSessionEmail);
1877
1878 // RFC 4566
1879 // p=* (phone number)
1880 GetLineWithType(message, pos, &line, kLineTypeSessionPhone);
1881
1882 // RFC 4566
1883 // c=* (connection information -- not required if included in
1884 // all media)
1885 GetLineWithType(message, pos, &line, kLineTypeConnection);
1886
1887 // RFC 4566
1888 // b=* (zero or more bandwidth information lines)
1889 while (GetLineWithType(message, pos, &line, kLineTypeSessionBandwidth)) {
1890 // By pass zero or more b lines.
1891 }
1892
1893 // RFC 4566
1894 // One or more time descriptions ("t=" and "r=" lines; see below)
1895 // t= (time the session is active)
1896 // r=* (zero or more repeat times)
1897 // Ensure there's at least one time description
1898 if (!GetLineWithType(message, pos, &line, kLineTypeTiming)) {
1899 return ParseFailedExpectLine(message, *pos, kLineTypeTiming, std::string(),
1900 error);
1901 }
1902
1903 while (GetLineWithType(message, pos, &line, kLineTypeRepeatTimes)) {
1904 // By pass zero or more r lines.
1905 }
1906
1907 // Go through the rest of the time descriptions
1908 while (GetLineWithType(message, pos, &line, kLineTypeTiming)) {
1909 while (GetLineWithType(message, pos, &line, kLineTypeRepeatTimes)) {
1910 // By pass zero or more r lines.
1911 }
1912 }
1913
1914 // RFC 4566
1915 // z=* (time zone adjustments)
1916 GetLineWithType(message, pos, &line, kLineTypeTimeZone);
1917
1918 // RFC 4566
1919 // k=* (encryption key)
1920 GetLineWithType(message, pos, &line, kLineTypeEncryptionKey);
1921
1922 // RFC 4566
1923 // a=* (zero or more session attribute lines)
1924 while (GetLineWithType(message, pos, &line, kLineTypeAttributes)) {
1925 if (HasAttribute(line, kAttributeGroup)) {
1926 if (!ParseGroupAttribute(line, desc, error)) {
1927 return false;
1928 }
1929 } else if (HasAttribute(line, kAttributeIceUfrag)) {
1930 if (!GetValue(line, kAttributeIceUfrag,
1931 &(session_td->ice_ufrag), error)) {
1932 return false;
1933 }
1934 } else if (HasAttribute(line, kAttributeIcePwd)) {
1935 if (!GetValue(line, kAttributeIcePwd, &(session_td->ice_pwd), error)) {
1936 return false;
1937 }
1938 } else if (HasAttribute(line, kAttributeIceLite)) {
1939 session_td->ice_mode = cricket::ICEMODE_LITE;
1940 } else if (HasAttribute(line, kAttributeIceOption)) {
1941 if (!ParseIceOptions(line, &(session_td->transport_options), error)) {
1942 return false;
1943 }
1944 } else if (HasAttribute(line, kAttributeFingerprint)) {
1945 if (session_td->identity_fingerprint.get()) {
1946 return ParseFailed(
1947 line,
1948 "Can't have multiple fingerprint attributes at the same level.",
1949 error);
1950 }
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001951 rtc::SSLFingerprint* fingerprint = NULL;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001952 if (!ParseFingerprintAttribute(line, &fingerprint, error)) {
1953 return false;
1954 }
1955 session_td->identity_fingerprint.reset(fingerprint);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001956 } else if (HasAttribute(line, kAttributeSetup)) {
1957 if (!ParseDtlsSetup(line, &(session_td->connection_role), error)) {
1958 return false;
1959 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001960 } else if (HasAttribute(line, kAttributeMsidSemantics)) {
1961 std::string semantics;
1962 if (!GetValue(line, kAttributeMsidSemantics, &semantics, error)) {
1963 return false;
1964 }
1965 *supports_msid = CaseInsensitiveFind(semantics, kMediaStreamSemantic);
1966 } else if (HasAttribute(line, kAttributeExtmap)) {
1967 RtpHeaderExtension extmap;
1968 if (!ParseExtmap(line, &extmap, error)) {
1969 return false;
1970 }
1971 session_extmaps->push_back(extmap);
1972 }
1973 }
1974
1975 return true;
1976}
1977
1978bool ParseGroupAttribute(const std::string& line,
1979 cricket::SessionDescription* desc,
1980 SdpParseError* error) {
1981 ASSERT(desc != NULL);
1982
1983 // RFC 5888 and draft-holmberg-mmusic-sdp-bundle-negotiation-00
1984 // a=group:BUNDLE video voice
1985 std::vector<std::string> fields;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001986 rtc::split(line.substr(kLinePrefixLength),
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001987 kSdpDelimiterSpace, &fields);
1988 std::string semantics;
1989 if (!GetValue(fields[0], kAttributeGroup, &semantics, error)) {
1990 return false;
1991 }
1992 cricket::ContentGroup group(semantics);
1993 for (size_t i = 1; i < fields.size(); ++i) {
1994 group.AddContentName(fields[i]);
1995 }
1996 desc->AddGroup(group);
1997 return true;
1998}
1999
2000static bool ParseFingerprintAttribute(const std::string& line,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002001 rtc::SSLFingerprint** fingerprint,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002002 SdpParseError* error) {
2003 if (!IsLineType(line, kLineTypeAttributes) ||
2004 !HasAttribute(line, kAttributeFingerprint)) {
2005 return ParseFailedExpectLine(line, 0, kLineTypeAttributes,
2006 kAttributeFingerprint, error);
2007 }
2008
2009 std::vector<std::string> fields;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002010 rtc::split(line.substr(kLinePrefixLength),
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002011 kSdpDelimiterSpace, &fields);
2012 const size_t expected_fields = 2;
2013 if (fields.size() != expected_fields) {
2014 return ParseFailedExpectFieldNum(line, expected_fields, error);
2015 }
2016
2017 // The first field here is "fingerprint:<hash>.
2018 std::string algorithm;
2019 if (!GetValue(fields[0], kAttributeFingerprint, &algorithm, error)) {
2020 return false;
2021 }
2022
2023 // Downcase the algorithm. Note that we don't need to downcase the
2024 // fingerprint because hex_decode can handle upper-case.
2025 std::transform(algorithm.begin(), algorithm.end(), algorithm.begin(),
2026 ::tolower);
2027
2028 // The second field is the digest value. De-hexify it.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002029 *fingerprint = rtc::SSLFingerprint::CreateFromRfc4572(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002030 algorithm, fields[1]);
2031 if (!*fingerprint) {
2032 return ParseFailed(line,
2033 "Failed to create fingerprint from the digest.",
2034 error);
2035 }
2036
2037 return true;
2038}
2039
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00002040static bool ParseDtlsSetup(const std::string& line,
2041 cricket::ConnectionRole* role,
2042 SdpParseError* error) {
2043 // setup-attr = "a=setup:" role
2044 // role = "active" / "passive" / "actpass" / "holdconn"
2045 std::vector<std::string> fields;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002046 rtc::split(line.substr(kLinePrefixLength), kSdpDelimiterColon, &fields);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00002047 const size_t expected_fields = 2;
2048 if (fields.size() != expected_fields) {
2049 return ParseFailedExpectFieldNum(line, expected_fields, error);
2050 }
2051 std::string role_str = fields[1];
2052 if (!cricket::StringToConnectionRole(role_str, role)) {
2053 return ParseFailed(line, "Invalid attribute value.", error);
2054 }
2055 return true;
2056}
2057
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002058// RFC 3551
2059// PT encoding media type clock rate channels
2060// name (Hz)
2061// 0 PCMU A 8,000 1
2062// 1 reserved A
2063// 2 reserved A
2064// 3 GSM A 8,000 1
2065// 4 G723 A 8,000 1
2066// 5 DVI4 A 8,000 1
2067// 6 DVI4 A 16,000 1
2068// 7 LPC A 8,000 1
2069// 8 PCMA A 8,000 1
2070// 9 G722 A 8,000 1
2071// 10 L16 A 44,100 2
2072// 11 L16 A 44,100 1
2073// 12 QCELP A 8,000 1
2074// 13 CN A 8,000 1
2075// 14 MPA A 90,000 (see text)
2076// 15 G728 A 8,000 1
2077// 16 DVI4 A 11,025 1
2078// 17 DVI4 A 22,050 1
2079// 18 G729 A 8,000 1
2080struct StaticPayloadAudioCodec {
2081 const char* name;
2082 int clockrate;
2083 int channels;
2084};
2085static const StaticPayloadAudioCodec kStaticPayloadAudioCodecs[] = {
2086 { "PCMU", 8000, 1 },
2087 { "reserved", 0, 0 },
2088 { "reserved", 0, 0 },
2089 { "GSM", 8000, 1 },
2090 { "G723", 8000, 1 },
2091 { "DVI4", 8000, 1 },
2092 { "DVI4", 16000, 1 },
2093 { "LPC", 8000, 1 },
2094 { "PCMA", 8000, 1 },
2095 { "G722", 8000, 1 },
2096 { "L16", 44100, 2 },
2097 { "L16", 44100, 1 },
2098 { "QCELP", 8000, 1 },
2099 { "CN", 8000, 1 },
2100 { "MPA", 90000, 1 },
2101 { "G728", 8000, 1 },
2102 { "DVI4", 11025, 1 },
2103 { "DVI4", 22050, 1 },
2104 { "G729", 8000, 1 },
2105};
2106
2107void MaybeCreateStaticPayloadAudioCodecs(
2108 const std::vector<int>& fmts, AudioContentDescription* media_desc) {
2109 if (!media_desc) {
2110 return;
2111 }
henrike@webrtc.org28654cb2013-07-22 21:07:49 +00002112 int preference = static_cast<int>(fmts.size());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002113 std::vector<int>::const_iterator it = fmts.begin();
2114 bool add_new_codec = false;
2115 for (; it != fmts.end(); ++it) {
2116 int payload_type = *it;
2117 if (!media_desc->HasCodec(payload_type) &&
2118 payload_type >= 0 &&
2119 payload_type < ARRAY_SIZE(kStaticPayloadAudioCodecs)) {
2120 std::string encoding_name = kStaticPayloadAudioCodecs[payload_type].name;
2121 int clock_rate = kStaticPayloadAudioCodecs[payload_type].clockrate;
2122 int channels = kStaticPayloadAudioCodecs[payload_type].channels;
2123 media_desc->AddCodec(cricket::AudioCodec(payload_type, encoding_name,
2124 clock_rate, 0, channels,
2125 preference));
2126 add_new_codec = true;
2127 }
2128 --preference;
2129 }
2130 if (add_new_codec) {
2131 media_desc->SortCodecs();
2132 }
2133}
2134
2135template <class C>
2136static C* ParseContentDescription(const std::string& message,
2137 const MediaType media_type,
2138 int mline_index,
2139 const std::string& protocol,
2140 const std::vector<int>& codec_preference,
2141 size_t* pos,
2142 std::string* content_name,
2143 TransportDescription* transport,
2144 std::vector<JsepIceCandidate*>* candidates,
2145 webrtc::SdpParseError* error) {
2146 C* media_desc = new C();
2147 switch (media_type) {
2148 case cricket::MEDIA_TYPE_AUDIO:
2149 *content_name = cricket::CN_AUDIO;
2150 break;
2151 case cricket::MEDIA_TYPE_VIDEO:
2152 *content_name = cricket::CN_VIDEO;
2153 break;
2154 case cricket::MEDIA_TYPE_DATA:
2155 *content_name = cricket::CN_DATA;
2156 break;
2157 default:
2158 ASSERT(false);
2159 break;
2160 }
2161 if (!ParseContent(message, media_type, mline_index, protocol,
2162 codec_preference, pos, content_name,
2163 media_desc, transport, candidates, error)) {
2164 delete media_desc;
2165 return NULL;
2166 }
2167 // Sort the codecs according to the m-line fmt list.
2168 media_desc->SortCodecs();
2169 return media_desc;
2170}
2171
2172bool ParseMediaDescription(const std::string& message,
2173 const TransportDescription& session_td,
2174 const RtpHeaderExtensions& session_extmaps,
2175 bool supports_msid,
2176 size_t* pos,
2177 cricket::SessionDescription* desc,
2178 std::vector<JsepIceCandidate*>* candidates,
2179 SdpParseError* error) {
2180 ASSERT(desc != NULL);
2181 std::string line;
2182 int mline_index = -1;
2183
2184 // Zero or more media descriptions
2185 // RFC 4566
2186 // m=<media> <port> <proto> <fmt>
2187 while (GetLineWithType(message, pos, &line, kLineTypeMedia)) {
2188 ++mline_index;
2189
2190 std::vector<std::string> fields;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002191 rtc::split(line.substr(kLinePrefixLength),
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002192 kSdpDelimiterSpace, &fields);
2193 const size_t expected_min_fields = 4;
2194 if (fields.size() < expected_min_fields) {
2195 return ParseFailedExpectMinFieldNum(line, expected_min_fields, error);
2196 }
2197 bool rejected = false;
2198 // RFC 3264
2199 // To reject an offered stream, the port number in the corresponding stream
2200 // in the answer MUST be set to zero.
2201 if (fields[1] == kMediaPortRejected) {
2202 rejected = true;
2203 }
2204
2205 std::string protocol = fields[2];
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002206
2207 // <fmt>
2208 std::vector<int> codec_preference;
pthatcher@webrtc.org3341b402015-02-13 21:14:22 +00002209 if (IsRtp(protocol)) {
jiayl@webrtc.orgddb85ab2014-09-05 16:31:56 +00002210 for (size_t j = 3 ; j < fields.size(); ++j) {
2211 // TODO(wu): Remove when below bug is fixed.
2212 // https://bugzilla.mozilla.org/show_bug.cgi?id=996329
2213 if (fields[j] == "" && j == fields.size() - 1) {
2214 continue;
2215 }
wu@webrtc.org36eda7c2014-04-15 20:37:30 +00002216
jiayl@webrtc.orgddb85ab2014-09-05 16:31:56 +00002217 int pl = 0;
pkasting@chromium.orge9facf82015-02-17 20:36:28 +00002218 if (!GetPayloadTypeFromString(line, fields[j], &pl, error)) {
jiayl@webrtc.orgddb85ab2014-09-05 16:31:56 +00002219 return false;
2220 }
2221 codec_preference.push_back(pl);
wu@webrtc.org5e760e72014-04-02 23:19:09 +00002222 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002223 }
2224
2225 // Make a temporary TransportDescription based on |session_td|.
2226 // Some of this gets overwritten by ParseContent.
2227 TransportDescription transport(NS_JINGLE_ICE_UDP,
2228 session_td.transport_options,
2229 session_td.ice_ufrag,
2230 session_td.ice_pwd,
2231 session_td.ice_mode,
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00002232 session_td.connection_role,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002233 session_td.identity_fingerprint.get(),
2234 Candidates());
2235
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002236 rtc::scoped_ptr<MediaContentDescription> content;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002237 std::string content_name;
2238 if (HasAttribute(line, kMediaTypeVideo)) {
2239 content.reset(ParseContentDescription<VideoContentDescription>(
2240 message, cricket::MEDIA_TYPE_VIDEO, mline_index, protocol,
2241 codec_preference, pos, &content_name,
2242 &transport, candidates, error));
2243 } else if (HasAttribute(line, kMediaTypeAudio)) {
2244 content.reset(ParseContentDescription<AudioContentDescription>(
2245 message, cricket::MEDIA_TYPE_AUDIO, mline_index, protocol,
2246 codec_preference, pos, &content_name,
2247 &transport, candidates, error));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002248 } else if (HasAttribute(line, kMediaTypeData)) {
jiayl@webrtc.org0e527722014-09-08 21:43:43 +00002249 DataContentDescription* data_desc =
wu@webrtc.org78187522013-10-07 23:32:02 +00002250 ParseContentDescription<DataContentDescription>(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002251 message, cricket::MEDIA_TYPE_DATA, mline_index, protocol,
2252 codec_preference, pos, &content_name,
wu@webrtc.org78187522013-10-07 23:32:02 +00002253 &transport, candidates, error);
jiayl@webrtc.org0e527722014-09-08 21:43:43 +00002254 content.reset(data_desc);
wu@webrtc.org78187522013-10-07 23:32:02 +00002255
jiayl@webrtc.orgddb85ab2014-09-05 16:31:56 +00002256 int p;
pthatcher@webrtc.org3341b402015-02-13 21:14:22 +00002257 if (data_desc && IsDtlsSctp(protocol) && rtc::FromString(fields[3], &p)) {
jiayl@webrtc.org0e527722014-09-08 21:43:43 +00002258 if (!AddSctpDataCodec(data_desc, p))
jiayl@webrtc.orgddb85ab2014-09-05 16:31:56 +00002259 return false;
wu@webrtc.org78187522013-10-07 23:32:02 +00002260 }
2261
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00002262 // We should always use the default bandwidth for RTP-based data
2263 // channels. Don't allow SDP to set the bandwidth, because that
2264 // would give JS the opportunity to "break the Internet".
sergeyu@chromium.orga59696b2013-09-13 23:48:58 +00002265 // TODO(pthatcher): But we need to temporarily allow the SDP to control
2266 // this for backwards-compatibility. Once we don't need that any
2267 // more, remove this.
2268 bool support_dc_sdp_bandwidth_temporarily = true;
wu@webrtc.org967bfff2013-09-19 05:49:50 +00002269 if (content.get() && !support_dc_sdp_bandwidth_temporarily) {
sergeyu@chromium.orga59696b2013-09-13 23:48:58 +00002270 content->set_bandwidth(cricket::kAutoBandwidth);
2271 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002272 } else {
2273 LOG(LS_WARNING) << "Unsupported media type: " << line;
2274 continue;
2275 }
2276 if (!content.get()) {
2277 // ParseContentDescription returns NULL if failed.
2278 return false;
2279 }
2280
pthatcher@webrtc.org3341b402015-02-13 21:14:22 +00002281 if (IsRtp(protocol)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002282 // Make sure to set the media direction correctly. If the direction is not
2283 // MD_RECVONLY or Inactive and no streams are parsed,
2284 // a default MediaStream will be created to prepare for receiving media.
2285 if (supports_msid && content->streams().empty() &&
2286 content->direction() == cricket::MD_SENDRECV) {
2287 content->set_direction(cricket::MD_RECVONLY);
2288 }
2289
2290 // Set the extmap.
2291 if (!session_extmaps.empty() &&
2292 !content->rtp_header_extensions().empty()) {
2293 return ParseFailed("",
2294 "The a=extmap MUST be either all session level or "
2295 "all media level.",
2296 error);
2297 }
2298 for (size_t i = 0; i < session_extmaps.size(); ++i) {
2299 content->AddRtpHeaderExtension(session_extmaps[i]);
2300 }
2301 }
2302 content->set_protocol(protocol);
2303 desc->AddContent(content_name,
pthatcher@webrtc.org3341b402015-02-13 21:14:22 +00002304 IsDtlsSctp(protocol) ? cricket::NS_JINGLE_DRAFT_SCTP :
2305 cricket::NS_JINGLE_RTP,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002306 rejected,
2307 content.release());
2308 // Create TransportInfo with the media level "ice-pwd" and "ice-ufrag".
2309 TransportInfo transport_info(content_name, transport);
2310
2311 if (!desc->AddTransportInfo(transport_info)) {
2312 std::ostringstream description;
2313 description << "Failed to AddTransportInfo with content name: "
2314 << content_name;
2315 return ParseFailed("", description.str(), error);
2316 }
2317 }
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00002318
2319 size_t end_of_message = message.size();
2320 if (mline_index == -1 && *pos != end_of_message) {
2321 ParseFailed(message, *pos, "Expects m line.", error);
2322 return false;
2323 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002324 return true;
2325}
2326
2327bool VerifyCodec(const cricket::Codec& codec) {
2328 // Codec has not been populated correctly unless the name has been set. This
2329 // can happen if an SDP has an fmtp or rtcp-fb with a payload type but doesn't
2330 // have a corresponding "rtpmap" line.
2331 cricket::Codec default_codec;
2332 return default_codec.name != codec.name;
2333}
2334
2335bool VerifyAudioCodecs(const AudioContentDescription* audio_desc) {
2336 const std::vector<cricket::AudioCodec>& codecs = audio_desc->codecs();
2337 for (std::vector<cricket::AudioCodec>::const_iterator iter = codecs.begin();
2338 iter != codecs.end(); ++iter) {
2339 if (!VerifyCodec(*iter)) {
2340 return false;
2341 }
2342 }
2343 return true;
2344}
2345
2346bool VerifyVideoCodecs(const VideoContentDescription* video_desc) {
2347 const std::vector<cricket::VideoCodec>& codecs = video_desc->codecs();
2348 for (std::vector<cricket::VideoCodec>::const_iterator iter = codecs.begin();
2349 iter != codecs.end(); ++iter) {
2350 if (!VerifyCodec(*iter)) {
2351 return false;
2352 }
2353 }
2354 return true;
2355}
2356
2357void AddParameters(const cricket::CodecParameterMap& parameters,
2358 cricket::Codec* codec) {
2359 for (cricket::CodecParameterMap::const_iterator iter =
2360 parameters.begin(); iter != parameters.end(); ++iter) {
2361 codec->SetParam(iter->first, iter->second);
2362 }
2363}
2364
2365void AddFeedbackParameter(const cricket::FeedbackParam& feedback_param,
2366 cricket::Codec* codec) {
2367 codec->AddFeedbackParam(feedback_param);
2368}
2369
2370void AddFeedbackParameters(const cricket::FeedbackParams& feedback_params,
2371 cricket::Codec* codec) {
2372 for (std::vector<cricket::FeedbackParam>::const_iterator iter =
2373 feedback_params.params().begin();
2374 iter != feedback_params.params().end(); ++iter) {
2375 codec->AddFeedbackParam(*iter);
2376 }
2377}
2378
2379// Gets the current codec setting associated with |payload_type|. If there
2380// is no AudioCodec associated with that payload type it returns an empty codec
2381// with that payload type.
2382template <class T>
2383T GetCodec(const std::vector<T>& codecs, int payload_type) {
2384 for (typename std::vector<T>::const_iterator codec = codecs.begin();
2385 codec != codecs.end(); ++codec) {
2386 if (codec->id == payload_type) {
2387 return *codec;
2388 }
2389 }
2390 T ret_val = T();
2391 ret_val.id = payload_type;
2392 return ret_val;
2393}
2394
2395// Updates or creates a new codec entry in the audio description.
2396template <class T, class U>
2397void AddOrReplaceCodec(MediaContentDescription* content_desc, const U& codec) {
2398 T* desc = static_cast<T*>(content_desc);
2399 std::vector<U> codecs = desc->codecs();
2400 bool found = false;
2401
2402 typename std::vector<U>::iterator iter;
2403 for (iter = codecs.begin(); iter != codecs.end(); ++iter) {
2404 if (iter->id == codec.id) {
2405 *iter = codec;
2406 found = true;
2407 break;
2408 }
2409 }
2410 if (!found) {
2411 desc->AddCodec(codec);
2412 return;
2413 }
2414 desc->set_codecs(codecs);
2415}
2416
2417// Adds or updates existing codec corresponding to |payload_type| according
2418// to |parameters|.
2419template <class T, class U>
2420void UpdateCodec(MediaContentDescription* content_desc, int payload_type,
2421 const cricket::CodecParameterMap& parameters) {
2422 // Codec might already have been populated (from rtpmap).
2423 U new_codec = GetCodec(static_cast<T*>(content_desc)->codecs(), payload_type);
2424 AddParameters(parameters, &new_codec);
2425 AddOrReplaceCodec<T, U>(content_desc, new_codec);
2426}
2427
2428// Adds or updates existing codec corresponding to |payload_type| according
2429// to |feedback_param|.
2430template <class T, class U>
2431void UpdateCodec(MediaContentDescription* content_desc, int payload_type,
2432 const cricket::FeedbackParam& feedback_param) {
2433 // Codec might already have been populated (from rtpmap).
2434 U new_codec = GetCodec(static_cast<T*>(content_desc)->codecs(), payload_type);
2435 AddFeedbackParameter(feedback_param, &new_codec);
2436 AddOrReplaceCodec<T, U>(content_desc, new_codec);
2437}
2438
2439bool PopWildcardCodec(std::vector<cricket::VideoCodec>* codecs,
2440 cricket::VideoCodec* wildcard_codec) {
2441 for (std::vector<cricket::VideoCodec>::iterator iter = codecs->begin();
2442 iter != codecs->end(); ++iter) {
2443 if (iter->id == kWildcardPayloadType) {
2444 *wildcard_codec = *iter;
2445 codecs->erase(iter);
2446 return true;
2447 }
2448 }
2449 return false;
2450}
2451
2452void UpdateFromWildcardVideoCodecs(VideoContentDescription* video_desc) {
2453 std::vector<cricket::VideoCodec> codecs = video_desc->codecs();
2454 cricket::VideoCodec wildcard_codec;
2455 if (!PopWildcardCodec(&codecs, &wildcard_codec)) {
2456 return;
2457 }
2458 for (std::vector<cricket::VideoCodec>::iterator iter = codecs.begin();
2459 iter != codecs.end(); ++iter) {
2460 cricket::VideoCodec& codec = *iter;
2461 AddFeedbackParameters(wildcard_codec.feedback_params, &codec);
2462 }
2463 video_desc->set_codecs(codecs);
2464}
2465
2466void AddAudioAttribute(const std::string& name, const std::string& value,
2467 AudioContentDescription* audio_desc) {
2468 if (value.empty()) {
2469 return;
2470 }
2471 std::vector<cricket::AudioCodec> codecs = audio_desc->codecs();
2472 for (std::vector<cricket::AudioCodec>::iterator iter = codecs.begin();
2473 iter != codecs.end(); ++iter) {
2474 iter->params[name] = value;
2475 }
2476 audio_desc->set_codecs(codecs);
2477}
2478
2479bool ParseContent(const std::string& message,
2480 const MediaType media_type,
2481 int mline_index,
2482 const std::string& protocol,
2483 const std::vector<int>& codec_preference,
2484 size_t* pos,
2485 std::string* content_name,
2486 MediaContentDescription* media_desc,
2487 TransportDescription* transport,
2488 std::vector<JsepIceCandidate*>* candidates,
2489 SdpParseError* error) {
2490 ASSERT(media_desc != NULL);
2491 ASSERT(content_name != NULL);
2492 ASSERT(transport != NULL);
2493
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +00002494 if (media_type == cricket::MEDIA_TYPE_AUDIO) {
2495 MaybeCreateStaticPayloadAudioCodecs(
2496 codec_preference, static_cast<AudioContentDescription*>(media_desc));
2497 }
2498
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002499 // The media level "ice-ufrag" and "ice-pwd".
2500 // The candidates before update the media level "ice-pwd" and "ice-ufrag".
2501 Candidates candidates_orig;
2502 std::string line;
2503 std::string mline_id;
2504 // Tracks created out of the ssrc attributes.
2505 StreamParamsVec tracks;
2506 SsrcInfoVec ssrc_infos;
2507 SsrcGroupVec ssrc_groups;
2508 std::string maxptime_as_string;
2509 std::string ptime_as_string;
2510
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002511 // Loop until the next m line
2512 while (!IsLineType(message, kLineTypeMedia, *pos)) {
2513 if (!GetLine(message, pos, &line)) {
2514 if (*pos >= message.size()) {
2515 break; // Done parsing
2516 } else {
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +00002517 return ParseFailed(message, *pos, "Invalid SDP line.", error);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002518 }
2519 }
2520
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002521 // RFC 4566
2522 // b=* (zero or more bandwidth information lines)
2523 if (IsLineType(line, kLineTypeSessionBandwidth)) {
2524 std::string bandwidth;
2525 if (HasAttribute(line, kApplicationSpecificMaximum)) {
2526 if (!GetValue(line, kApplicationSpecificMaximum, &bandwidth, error)) {
2527 return false;
2528 } else {
wu@webrtc.org5e760e72014-04-02 23:19:09 +00002529 int b = 0;
2530 if (!GetValueFromString(line, bandwidth, &b, error)) {
2531 return false;
2532 }
2533 media_desc->set_bandwidth(b * 1000);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002534 }
2535 }
2536 continue;
2537 }
2538
2539 if (!IsLineType(line, kLineTypeAttributes)) {
2540 // TODO: Handle other lines if needed.
2541 LOG(LS_INFO) << "Ignored line: " << line;
2542 continue;
2543 }
2544
2545 // Handle attributes common to SCTP and RTP.
2546 if (HasAttribute(line, kAttributeMid)) {
2547 // RFC 3388
2548 // mid-attribute = "a=mid:" identification-tag
2549 // identification-tag = token
2550 // Use the mid identification-tag as the content name.
2551 if (!GetValue(line, kAttributeMid, &mline_id, error)) {
2552 return false;
2553 }
2554 *content_name = mline_id;
2555 } else if (HasAttribute(line, kAttributeCandidate)) {
2556 Candidate candidate;
2557 if (!ParseCandidate(line, &candidate, error, false)) {
2558 return false;
2559 }
2560 candidates_orig.push_back(candidate);
2561 } else if (HasAttribute(line, kAttributeIceUfrag)) {
2562 if (!GetValue(line, kAttributeIceUfrag, &transport->ice_ufrag, error)) {
2563 return false;
2564 }
2565 } else if (HasAttribute(line, kAttributeIcePwd)) {
2566 if (!GetValue(line, kAttributeIcePwd, &transport->ice_pwd, error)) {
2567 return false;
2568 }
2569 } else if (HasAttribute(line, kAttributeIceOption)) {
2570 if (!ParseIceOptions(line, &transport->transport_options, error)) {
2571 return false;
2572 }
2573 } else if (HasAttribute(line, kAttributeFmtp)) {
2574 if (!ParseFmtpAttributes(line, media_type, media_desc, error)) {
2575 return false;
2576 }
2577 } else if (HasAttribute(line, kAttributeFingerprint)) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002578 rtc::SSLFingerprint* fingerprint = NULL;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002579
2580 if (!ParseFingerprintAttribute(line, &fingerprint, error)) {
2581 return false;
2582 }
2583 transport->identity_fingerprint.reset(fingerprint);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00002584 } else if (HasAttribute(line, kAttributeSetup)) {
2585 if (!ParseDtlsSetup(line, &(transport->connection_role), error)) {
2586 return false;
2587 }
pthatcher@webrtc.org3341b402015-02-13 21:14:22 +00002588 } else if (IsDtlsSctp(protocol) && HasAttribute(line, kAttributeSctpPort)) {
jiayl@webrtc.orgddb85ab2014-09-05 16:31:56 +00002589 int sctp_port;
2590 if (!ParseSctpPort(line, &sctp_port, error)) {
2591 return false;
2592 }
2593 if (!AddSctpDataCodec(static_cast<DataContentDescription*>(media_desc),
2594 sctp_port)) {
2595 return false;
2596 }
pthatcher@webrtc.org3341b402015-02-13 21:14:22 +00002597 } else if (IsRtp(protocol)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002598 //
2599 // RTP specific attrubtes
2600 //
2601 if (HasAttribute(line, kAttributeRtcpMux)) {
2602 media_desc->set_rtcp_mux(true);
2603 } else if (HasAttribute(line, kAttributeSsrcGroup)) {
2604 if (!ParseSsrcGroupAttribute(line, &ssrc_groups, error)) {
2605 return false;
2606 }
2607 } else if (HasAttribute(line, kAttributeSsrc)) {
2608 if (!ParseSsrcAttribute(line, &ssrc_infos, error)) {
2609 return false;
2610 }
2611 } else if (HasAttribute(line, kAttributeCrypto)) {
2612 if (!ParseCryptoAttribute(line, media_desc, error)) {
2613 return false;
2614 }
2615 } else if (HasAttribute(line, kAttributeRtpmap)) {
2616 if (!ParseRtpmapAttribute(line, media_type, codec_preference,
2617 media_desc, error)) {
2618 return false;
2619 }
2620 } else if (HasAttribute(line, kCodecParamMaxPTime)) {
2621 if (!GetValue(line, kCodecParamMaxPTime, &maxptime_as_string, error)) {
2622 return false;
2623 }
2624 } else if (HasAttribute(line, kAttributeRtcpFb)) {
2625 if (!ParseRtcpFbAttribute(line, media_type, media_desc, error)) {
2626 return false;
2627 }
2628 } else if (HasAttribute(line, kCodecParamPTime)) {
2629 if (!GetValue(line, kCodecParamPTime, &ptime_as_string, error)) {
2630 return false;
2631 }
2632 } else if (HasAttribute(line, kAttributeSendOnly)) {
2633 media_desc->set_direction(cricket::MD_SENDONLY);
2634 } else if (HasAttribute(line, kAttributeRecvOnly)) {
2635 media_desc->set_direction(cricket::MD_RECVONLY);
2636 } else if (HasAttribute(line, kAttributeInactive)) {
2637 media_desc->set_direction(cricket::MD_INACTIVE);
2638 } else if (HasAttribute(line, kAttributeSendRecv)) {
2639 media_desc->set_direction(cricket::MD_SENDRECV);
2640 } else if (HasAttribute(line, kAttributeExtmap)) {
2641 RtpHeaderExtension extmap;
2642 if (!ParseExtmap(line, &extmap, error)) {
2643 return false;
2644 }
2645 media_desc->AddRtpHeaderExtension(extmap);
2646 } else if (HasAttribute(line, kAttributeXGoogleFlag)) {
2647 // Experimental attribute. Conference mode activates more aggressive
2648 // AEC and NS settings.
2649 // TODO: expose API to set these directly.
2650 std::string flag_value;
2651 if (!GetValue(line, kAttributeXGoogleFlag, &flag_value, error)) {
2652 return false;
2653 }
2654 if (flag_value.compare(kValueConference) == 0)
2655 media_desc->set_conference_mode(true);
2656 } else if (HasAttribute(line, kAttributeXGoogleBufferLatency)) {
2657 // Experimental attribute.
2658 // TODO: expose API to set this directly.
2659 std::string flag_value;
2660 if (!GetValue(line, kAttributeXGoogleBufferLatency, &flag_value,
2661 error)) {
2662 return false;
2663 }
2664 int buffer_latency = 0;
wu@webrtc.org5e760e72014-04-02 23:19:09 +00002665 if (!GetValueFromString(line, flag_value, &buffer_latency, error)) {
2666 return false;
2667 }
2668 if (buffer_latency < 0) {
2669 return ParseFailed(line, "Buffer latency less than 0.", error);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002670 }
2671 media_desc->set_buffered_mode_latency(buffer_latency);
2672 }
2673 } else {
2674 // Only parse lines that we are interested of.
2675 LOG(LS_INFO) << "Ignored line: " << line;
2676 continue;
2677 }
2678 }
2679
2680 // Create tracks from the |ssrc_infos|.
2681 CreateTracksFromSsrcInfos(ssrc_infos, &tracks);
2682
2683 // Add the ssrc group to the track.
2684 for (SsrcGroupVec::iterator ssrc_group = ssrc_groups.begin();
2685 ssrc_group != ssrc_groups.end(); ++ssrc_group) {
2686 if (ssrc_group->ssrcs.empty()) {
2687 continue;
2688 }
2689 uint32 ssrc = ssrc_group->ssrcs.front();
2690 for (StreamParamsVec::iterator track = tracks.begin();
2691 track != tracks.end(); ++track) {
2692 if (track->has_ssrc(ssrc)) {
2693 track->ssrc_groups.push_back(*ssrc_group);
2694 }
2695 }
2696 }
2697
2698 // Add the new tracks to the |media_desc|.
2699 for (StreamParamsVec::iterator track = tracks.begin();
2700 track != tracks.end(); ++track) {
2701 media_desc->AddStream(*track);
2702 }
2703
2704 if (media_type == cricket::MEDIA_TYPE_AUDIO) {
2705 AudioContentDescription* audio_desc =
2706 static_cast<AudioContentDescription*>(media_desc);
2707 // Verify audio codec ensures that no audio codec has been populated with
2708 // only fmtp.
2709 if (!VerifyAudioCodecs(audio_desc)) {
2710 return ParseFailed("Failed to parse audio codecs correctly.", error);
2711 }
2712 AddAudioAttribute(kCodecParamMaxPTime, maxptime_as_string, audio_desc);
2713 AddAudioAttribute(kCodecParamPTime, ptime_as_string, audio_desc);
2714 }
2715
2716 if (media_type == cricket::MEDIA_TYPE_VIDEO) {
2717 VideoContentDescription* video_desc =
2718 static_cast<VideoContentDescription*>(media_desc);
2719 UpdateFromWildcardVideoCodecs(video_desc);
2720 // Verify video codec ensures that no video codec has been populated with
2721 // only rtcp-fb.
2722 if (!VerifyVideoCodecs(video_desc)) {
2723 return ParseFailed("Failed to parse video codecs correctly.", error);
2724 }
2725 }
2726
2727 // RFC 5245
2728 // Update the candidates with the media level "ice-pwd" and "ice-ufrag".
2729 for (Candidates::iterator it = candidates_orig.begin();
2730 it != candidates_orig.end(); ++it) {
2731 ASSERT((*it).username().empty());
2732 (*it).set_username(transport->ice_ufrag);
2733 ASSERT((*it).password().empty());
2734 (*it).set_password(transport->ice_pwd);
2735 candidates->push_back(
2736 new JsepIceCandidate(mline_id, mline_index, *it));
2737 }
2738 return true;
2739}
2740
2741bool ParseSsrcAttribute(const std::string& line, SsrcInfoVec* ssrc_infos,
2742 SdpParseError* error) {
2743 ASSERT(ssrc_infos != NULL);
2744 // RFC 5576
2745 // a=ssrc:<ssrc-id> <attribute>
2746 // a=ssrc:<ssrc-id> <attribute>:<value>
2747 std::string field1, field2;
2748 if (!SplitByDelimiter(line.substr(kLinePrefixLength),
2749 kSdpDelimiterSpace,
2750 &field1,
2751 &field2)) {
2752 const size_t expected_fields = 2;
2753 return ParseFailedExpectFieldNum(line, expected_fields, error);
2754 }
2755
2756 // ssrc:<ssrc-id>
2757 std::string ssrc_id_s;
2758 if (!GetValue(field1, kAttributeSsrc, &ssrc_id_s, error)) {
2759 return false;
2760 }
wu@webrtc.org5e760e72014-04-02 23:19:09 +00002761 uint32 ssrc_id = 0;
2762 if (!GetValueFromString(line, ssrc_id_s, &ssrc_id, error)) {
2763 return false;
2764 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002765
2766 std::string attribute;
2767 std::string value;
2768 if (!SplitByDelimiter(field2, kSdpDelimiterColon,
2769 &attribute, &value)) {
2770 std::ostringstream description;
2771 description << "Failed to get the ssrc attribute value from " << field2
2772 << ". Expected format <attribute>:<value>.";
2773 return ParseFailed(line, description.str(), error);
2774 }
2775
2776 // Check if there's already an item for this |ssrc_id|. Create a new one if
2777 // there isn't.
2778 SsrcInfoVec::iterator ssrc_info = ssrc_infos->begin();
2779 for (; ssrc_info != ssrc_infos->end(); ++ssrc_info) {
2780 if (ssrc_info->ssrc_id == ssrc_id) {
2781 break;
2782 }
2783 }
2784 if (ssrc_info == ssrc_infos->end()) {
2785 SsrcInfo info;
2786 info.ssrc_id = ssrc_id;
2787 ssrc_infos->push_back(info);
2788 ssrc_info = ssrc_infos->end() - 1;
2789 }
2790
2791 // Store the info to the |ssrc_info|.
2792 if (attribute == kSsrcAttributeCname) {
2793 // RFC 5576
2794 // cname:<value>
2795 ssrc_info->cname = value;
2796 } else if (attribute == kSsrcAttributeMsid) {
2797 // draft-alvestrand-mmusic-msid-00
2798 // "msid:" identifier [ " " appdata ]
2799 std::vector<std::string> fields;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002800 rtc::split(value, kSdpDelimiterSpace, &fields);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002801 if (fields.size() < 1 || fields.size() > 2) {
2802 return ParseFailed(line,
2803 "Expected format \"msid:<identifier>[ <appdata>]\".",
2804 error);
2805 }
2806 ssrc_info->msid_identifier = fields[0];
2807 if (fields.size() == 2) {
2808 ssrc_info->msid_appdata = fields[1];
2809 }
2810 } else if (attribute == kSsrcAttributeMslabel) {
2811 // draft-alvestrand-rtcweb-mid-01
2812 // mslabel:<value>
2813 ssrc_info->mslabel = value;
2814 } else if (attribute == kSSrcAttributeLabel) {
2815 // The label isn't defined.
2816 // label:<value>
2817 ssrc_info->label = value;
2818 }
2819 return true;
2820}
2821
2822bool ParseSsrcGroupAttribute(const std::string& line,
2823 SsrcGroupVec* ssrc_groups,
2824 SdpParseError* error) {
2825 ASSERT(ssrc_groups != NULL);
2826 // RFC 5576
2827 // a=ssrc-group:<semantics> <ssrc-id> ...
2828 std::vector<std::string> fields;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002829 rtc::split(line.substr(kLinePrefixLength),
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002830 kSdpDelimiterSpace, &fields);
2831 const size_t expected_min_fields = 2;
2832 if (fields.size() < expected_min_fields) {
2833 return ParseFailedExpectMinFieldNum(line, expected_min_fields, error);
2834 }
2835 std::string semantics;
2836 if (!GetValue(fields[0], kAttributeSsrcGroup, &semantics, error)) {
2837 return false;
2838 }
2839 std::vector<uint32> ssrcs;
2840 for (size_t i = 1; i < fields.size(); ++i) {
wu@webrtc.org5e760e72014-04-02 23:19:09 +00002841 uint32 ssrc = 0;
2842 if (!GetValueFromString(line, fields[i], &ssrc, error)) {
2843 return false;
2844 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002845 ssrcs.push_back(ssrc);
2846 }
2847 ssrc_groups->push_back(SsrcGroup(semantics, ssrcs));
2848 return true;
2849}
2850
2851bool ParseCryptoAttribute(const std::string& line,
2852 MediaContentDescription* media_desc,
2853 SdpParseError* error) {
2854 std::vector<std::string> fields;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002855 rtc::split(line.substr(kLinePrefixLength),
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002856 kSdpDelimiterSpace, &fields);
2857 // RFC 4568
2858 // a=crypto:<tag> <crypto-suite> <key-params> [<session-params>]
2859 const size_t expected_min_fields = 3;
2860 if (fields.size() < expected_min_fields) {
2861 return ParseFailedExpectMinFieldNum(line, expected_min_fields, error);
2862 }
2863 std::string tag_value;
2864 if (!GetValue(fields[0], kAttributeCrypto, &tag_value, error)) {
2865 return false;
2866 }
wu@webrtc.org5e760e72014-04-02 23:19:09 +00002867 int tag = 0;
2868 if (!GetValueFromString(line, tag_value, &tag, error)) {
2869 return false;
2870 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002871 const std::string crypto_suite = fields[1];
2872 const std::string key_params = fields[2];
2873 std::string session_params;
2874 if (fields.size() > 3) {
2875 session_params = fields[3];
2876 }
2877 media_desc->AddCrypto(CryptoParams(tag, crypto_suite, key_params,
2878 session_params));
2879 return true;
2880}
2881
2882// Updates or creates a new codec entry in the audio description with according
2883// to |name|, |clockrate|, |bitrate|, |channels| and |preference|.
2884void UpdateCodec(int payload_type, const std::string& name, int clockrate,
2885 int bitrate, int channels, int preference,
2886 AudioContentDescription* audio_desc) {
2887 // Codec may already be populated with (only) optional parameters
2888 // (from an fmtp).
2889 cricket::AudioCodec codec = GetCodec(audio_desc->codecs(), payload_type);
2890 codec.name = name;
2891 codec.clockrate = clockrate;
2892 codec.bitrate = bitrate;
2893 codec.channels = channels;
2894 codec.preference = preference;
2895 AddOrReplaceCodec<AudioContentDescription, cricket::AudioCodec>(audio_desc,
2896 codec);
2897}
2898
2899// Updates or creates a new codec entry in the video description according to
2900// |name|, |width|, |height|, |framerate| and |preference|.
2901void UpdateCodec(int payload_type, const std::string& name, int width,
2902 int height, int framerate, int preference,
2903 VideoContentDescription* video_desc) {
2904 // Codec may already be populated with (only) optional parameters
2905 // (from an fmtp).
2906 cricket::VideoCodec codec = GetCodec(video_desc->codecs(), payload_type);
2907 codec.name = name;
2908 codec.width = width;
2909 codec.height = height;
2910 codec.framerate = framerate;
2911 codec.preference = preference;
2912 AddOrReplaceCodec<VideoContentDescription, cricket::VideoCodec>(video_desc,
2913 codec);
2914}
2915
2916bool ParseRtpmapAttribute(const std::string& line,
2917 const MediaType media_type,
2918 const std::vector<int>& codec_preference,
2919 MediaContentDescription* media_desc,
2920 SdpParseError* error) {
2921 std::vector<std::string> fields;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002922 rtc::split(line.substr(kLinePrefixLength),
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002923 kSdpDelimiterSpace, &fields);
2924 // RFC 4566
2925 // a=rtpmap:<payload type> <encoding name>/<clock rate>[/<encodingparameters>]
2926 const size_t expected_min_fields = 2;
2927 if (fields.size() < expected_min_fields) {
2928 return ParseFailedExpectMinFieldNum(line, expected_min_fields, error);
2929 }
2930 std::string payload_type_value;
2931 if (!GetValue(fields[0], kAttributeRtpmap, &payload_type_value, error)) {
2932 return false;
2933 }
wu@webrtc.org5e760e72014-04-02 23:19:09 +00002934 int payload_type = 0;
pkasting@chromium.orge9facf82015-02-17 20:36:28 +00002935 if (!GetPayloadTypeFromString(line, payload_type_value, &payload_type,
2936 error)) {
wu@webrtc.org5e760e72014-04-02 23:19:09 +00002937 return false;
2938 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002939
2940 // Set the preference order depending on the order of the pl type in the
2941 // <fmt> of the m-line.
2942 const int preference = codec_preference.end() -
2943 std::find(codec_preference.begin(), codec_preference.end(),
2944 payload_type);
2945 if (preference == 0) {
2946 LOG(LS_WARNING) << "Ignore rtpmap line that did not appear in the "
2947 << "<fmt> of the m-line: " << line;
2948 return true;
2949 }
2950 const std::string encoder = fields[1];
2951 std::vector<std::string> codec_params;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002952 rtc::split(encoder, '/', &codec_params);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002953 // <encoding name>/<clock rate>[/<encodingparameters>]
2954 // 2 mandatory fields
2955 if (codec_params.size() < 2 || codec_params.size() > 3) {
2956 return ParseFailed(line,
2957 "Expected format \"<encoding name>/<clock rate>"
2958 "[/<encodingparameters>]\".",
2959 error);
2960 }
2961 const std::string encoding_name = codec_params[0];
wu@webrtc.org5e760e72014-04-02 23:19:09 +00002962 int clock_rate = 0;
2963 if (!GetValueFromString(line, codec_params[1], &clock_rate, error)) {
2964 return false;
2965 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002966 if (media_type == cricket::MEDIA_TYPE_VIDEO) {
2967 VideoContentDescription* video_desc =
2968 static_cast<VideoContentDescription*>(media_desc);
2969 // TODO: We will send resolution in SDP. For now use
2970 // JsepSessionDescription::kMaxVideoCodecWidth and kMaxVideoCodecHeight.
2971 UpdateCodec(payload_type, encoding_name,
2972 JsepSessionDescription::kMaxVideoCodecWidth,
2973 JsepSessionDescription::kMaxVideoCodecHeight,
2974 JsepSessionDescription::kDefaultVideoCodecFramerate,
2975 preference, video_desc);
2976 } else if (media_type == cricket::MEDIA_TYPE_AUDIO) {
2977 // RFC 4566
2978 // For audio streams, <encoding parameters> indicates the number
2979 // of audio channels. This parameter is OPTIONAL and may be
2980 // omitted if the number of channels is one, provided that no
2981 // additional parameters are needed.
2982 int channels = 1;
2983 if (codec_params.size() == 3) {
wu@webrtc.org5e760e72014-04-02 23:19:09 +00002984 if (!GetValueFromString(line, codec_params[2], &channels, error)) {
2985 return false;
2986 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002987 }
2988 int bitrate = 0;
2989 // The default behavior for ISAC (bitrate == 0) in webrtcvoiceengine.cc
2990 // (specifically FindWebRtcCodec) is bandwidth-adaptive variable bitrate.
2991 // The bandwidth adaptation doesn't always work well, so this code
2992 // sets a fixed target bitrate instead.
2993 if (_stricmp(encoding_name.c_str(), kIsacCodecName) == 0) {
2994 if (clock_rate <= 16000) {
2995 bitrate = kIsacWbDefaultRate;
2996 } else {
2997 bitrate = kIsacSwbDefaultRate;
2998 }
2999 }
3000 AudioContentDescription* audio_desc =
3001 static_cast<AudioContentDescription*>(media_desc);
3002 UpdateCodec(payload_type, encoding_name, clock_rate, bitrate, channels,
3003 preference, audio_desc);
3004 } else if (media_type == cricket::MEDIA_TYPE_DATA) {
3005 DataContentDescription* data_desc =
3006 static_cast<DataContentDescription*>(media_desc);
3007 data_desc->AddCodec(cricket::DataCodec(payload_type, encoding_name,
3008 preference));
3009 }
3010 return true;
3011}
3012
3013void PruneRight(const char delimiter, std::string* message) {
3014 size_t trailing = message->find(delimiter);
3015 if (trailing != std::string::npos) {
3016 *message = message->substr(0, trailing);
3017 }
3018}
3019
3020bool ParseFmtpParam(const std::string& line, std::string* parameter,
3021 std::string* value, SdpParseError* error) {
3022 if (!SplitByDelimiter(line, kSdpDelimiterEqual, parameter, value)) {
3023 ParseFailed(line, "Unable to parse fmtp parameter. \'=\' missing.", error);
3024 return false;
3025 }
3026 // a=fmtp:<payload_type> <param1>=<value1>; <param2>=<value2>; ...
3027 // When parsing the values the trailing ";" gets picked up. Remove them.
3028 PruneRight(kSdpDelimiterSemicolon, value);
3029 return true;
3030}
3031
3032bool ParseFmtpAttributes(const std::string& line, const MediaType media_type,
3033 MediaContentDescription* media_desc,
3034 SdpParseError* error) {
3035 if (media_type != cricket::MEDIA_TYPE_AUDIO &&
3036 media_type != cricket::MEDIA_TYPE_VIDEO) {
3037 return true;
3038 }
3039 std::vector<std::string> fields;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00003040 rtc::split(line.substr(kLinePrefixLength),
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003041 kSdpDelimiterSpace, &fields);
3042
3043 // RFC 5576
3044 // a=fmtp:<format> <format specific parameters>
3045 // At least two fields, whereas the second one is any of the optional
3046 // parameters.
3047 if (fields.size() < 2) {
3048 ParseFailedExpectMinFieldNum(line, 2, error);
3049 return false;
3050 }
3051
3052 std::string payload_type;
3053 if (!GetValue(fields[0], kAttributeFmtp, &payload_type, error)) {
3054 return false;
3055 }
3056
3057 cricket::CodecParameterMap codec_params;
3058 for (std::vector<std::string>::const_iterator iter = fields.begin() + 1;
3059 iter != fields.end(); ++iter) {
3060 std::string name;
3061 std::string value;
3062 if (iter->find(kSdpDelimiterEqual) == std::string::npos) {
3063 // Only fmtps with equals are currently supported. Other fmtp types
3064 // should be ignored. Unknown fmtps do not constitute an error.
3065 continue;
3066 }
3067 if (!ParseFmtpParam(*iter, &name, &value, error)) {
3068 return false;
3069 }
3070 codec_params[name] = value;
3071 }
3072
wu@webrtc.org5e760e72014-04-02 23:19:09 +00003073 int int_payload_type = 0;
pkasting@chromium.orge9facf82015-02-17 20:36:28 +00003074 if (!GetPayloadTypeFromString(line, payload_type, &int_payload_type, error)) {
wu@webrtc.org5e760e72014-04-02 23:19:09 +00003075 return false;
3076 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003077 if (media_type == cricket::MEDIA_TYPE_AUDIO) {
3078 UpdateCodec<AudioContentDescription, cricket::AudioCodec>(
3079 media_desc, int_payload_type, codec_params);
3080 } else if (media_type == cricket::MEDIA_TYPE_VIDEO) {
3081 UpdateCodec<VideoContentDescription, cricket::VideoCodec>(
3082 media_desc, int_payload_type, codec_params);
3083 }
3084 return true;
3085}
3086
3087bool ParseRtcpFbAttribute(const std::string& line, const MediaType media_type,
3088 MediaContentDescription* media_desc,
3089 SdpParseError* error) {
3090 if (media_type != cricket::MEDIA_TYPE_AUDIO &&
3091 media_type != cricket::MEDIA_TYPE_VIDEO) {
3092 return true;
3093 }
3094 std::vector<std::string> rtcp_fb_fields;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00003095 rtc::split(line.c_str(), kSdpDelimiterSpace, &rtcp_fb_fields);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003096 if (rtcp_fb_fields.size() < 2) {
3097 return ParseFailedGetValue(line, kAttributeRtcpFb, error);
3098 }
3099 std::string payload_type_string;
3100 if (!GetValue(rtcp_fb_fields[0], kAttributeRtcpFb, &payload_type_string,
3101 error)) {
3102 return false;
3103 }
wu@webrtc.org5e760e72014-04-02 23:19:09 +00003104 int payload_type = kWildcardPayloadType;
3105 if (payload_type_string != "*") {
pkasting@chromium.orge9facf82015-02-17 20:36:28 +00003106 if (!GetPayloadTypeFromString(line, payload_type_string, &payload_type,
3107 error)) {
wu@webrtc.org5e760e72014-04-02 23:19:09 +00003108 return false;
3109 }
3110 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003111 std::string id = rtcp_fb_fields[1];
3112 std::string param = "";
3113 for (std::vector<std::string>::iterator iter = rtcp_fb_fields.begin() + 2;
3114 iter != rtcp_fb_fields.end(); ++iter) {
3115 param.append(*iter);
3116 }
3117 const cricket::FeedbackParam feedback_param(id, param);
3118
3119 if (media_type == cricket::MEDIA_TYPE_AUDIO) {
3120 UpdateCodec<AudioContentDescription, cricket::AudioCodec>(media_desc,
3121 payload_type,
3122 feedback_param);
3123 } else if (media_type == cricket::MEDIA_TYPE_VIDEO) {
3124 UpdateCodec<VideoContentDescription, cricket::VideoCodec>(media_desc,
3125 payload_type,
3126 feedback_param);
3127 }
3128 return true;
3129}
3130
3131} // namespace webrtc