blob: 5518233f2f7da5371992c632df1ed36eb80db7c7 [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;
Minyue Li7100dcd2015-03-27 05:05:59 +010072using cricket::kCodecParamUseDtx;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000073using cricket::kCodecParamSctpProtocol;
74using cricket::kCodecParamSctpStreams;
henrike@webrtc.org1e09a712013-07-26 19:17:59 +000075using cricket::kCodecParamMaxAverageBitrate;
buildbot@webrtc.org5d639b32014-09-10 07:57:12 +000076using cricket::kCodecParamMaxPlaybackRate;
stefan@webrtc.org85d27942014-06-09 12:51:39 +000077using cricket::kCodecParamAssociatedPayloadType;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000078using cricket::MediaContentDescription;
79using cricket::MediaType;
minyuel5bdafd42015-08-21 15:52:48 +020080using cricket::NS_JINGLE_ICE_UDP;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000081using 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";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000167
168// Candidate
169static const char kCandidateHost[] = "host";
170static const char kCandidateSrflx[] = "srflx";
171// TODO: How to map the prflx with circket candidate type
172// static const char kCandidatePrflx[] = "prflx";
173static const char kCandidateRelay[] = "relay";
mallinath@webrtc.org2d60c5e2014-08-08 22:29:20 +0000174static const char kTcpCandidateType[] = "tcptype";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000175
176static const char kSdpDelimiterEqual = '=';
177static const char kSdpDelimiterSpace = ' ';
178static const char kSdpDelimiterColon = ':';
179static const char kSdpDelimiterSemicolon = ';';
180static const char kSdpDelimiterSlash = '/';
181static const char kNewLine = '\n';
182static const char kReturn = '\r';
183static const char kLineBreak[] = "\r\n";
184
185// TODO: Generate the Session and Time description
186// instead of hardcoding.
187static const char kSessionVersion[] = "v=0";
188// RFC 4566
189static const char kSessionOriginUsername[] = "-";
190static const char kSessionOriginSessionId[] = "0";
191static const char kSessionOriginSessionVersion[] = "0";
192static const char kSessionOriginNettype[] = "IN";
193static const char kSessionOriginAddrtype[] = "IP4";
194static const char kSessionOriginAddress[] = "127.0.0.1";
195static const char kSessionName[] = "s=-";
196static const char kTimeDescription[] = "t=0 0";
197static const char kAttrGroup[] = "a=group:BUNDLE";
198static const char kConnectionNettype[] = "IN";
pthatcher@webrtc.orgc9d6d142014-10-23 23:37:22 +0000199static const char kConnectionIpv4Addrtype[] = "IP4";
200static const char kConnectionIpv6Addrtype[] = "IP6";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000201static const char kMediaTypeVideo[] = "video";
202static const char kMediaTypeAudio[] = "audio";
203static const char kMediaTypeData[] = "application";
204static const char kMediaPortRejected[] = "0";
pthatcher@webrtc.orgc9d6d142014-10-23 23:37:22 +0000205// draft-ietf-mmusic-trickle-ice-01
206// When no candidates have been gathered, set the connection
207// address to IP6 ::.
perkj@webrtc.orgd105cc82014-11-07 11:22:06 +0000208// TODO(perkj): FF can not parse IP6 ::. See http://crbug/430333
209// Use IPV4 per default.
210static const char kDummyAddress[] = "0.0.0.0";
pthatcher@webrtc.orgc9d6d142014-10-23 23:37:22 +0000211static const char kDummyPort[] = "9";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000212// RFC 3556
213static const char kApplicationSpecificMaximum[] = "AS";
214
215static const int kDefaultVideoClockrate = 90000;
216
217// ISAC special-case.
218static const char kIsacCodecName[] = "ISAC"; // From webrtcvoiceengine.cc
219static const int kIsacWbDefaultRate = 32000; // From acm_common_defs.h
220static const int kIsacSwbDefaultRate = 56000; // From acm_common_defs.h
221
wu@webrtc.org78187522013-10-07 23:32:02 +0000222static const char kDefaultSctpmapProtocol[] = "webrtc-datachannel";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000223
pkasting@chromium.orgd3245462015-02-23 21:28:22 +0000224// RTP payload type is in the 0-127 range. Use -1 to indicate "all" payload
225// types.
226const int kWildcardPayloadType = -1;
227
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000228struct SsrcInfo {
229 SsrcInfo()
230 : msid_identifier(kDefaultMsid),
231 // TODO(ronghuawu): What should we do if the appdata doesn't appear?
232 // Create random string (which will be used as track label later)?
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000233 msid_appdata(rtc::CreateRandomString(8)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000234 }
235 uint32 ssrc_id;
236 std::string cname;
237 std::string msid_identifier;
238 std::string msid_appdata;
239
240 // For backward compatibility.
241 // TODO(ronghuawu): Remove below 2 fields once all the clients support msid.
242 std::string label;
243 std::string mslabel;
244};
245typedef std::vector<SsrcInfo> SsrcInfoVec;
246typedef std::vector<SsrcGroup> SsrcGroupVec;
247
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000248template <class T>
249static void AddFmtpLine(const T& codec, std::string* message);
250static void BuildMediaDescription(const ContentInfo* content_info,
251 const TransportInfo* transport_info,
252 const MediaType media_type,
wu@webrtc.org4c3e9912014-07-16 21:03:13 +0000253 const std::vector<Candidate>& candidates,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000254 std::string* message);
jiayl@webrtc.org9c16c392014-05-01 18:30:30 +0000255static void BuildSctpContentAttributes(std::string* message, int sctp_port);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000256static void BuildRtpContentAttributes(
257 const MediaContentDescription* media_desc,
258 const MediaType media_type,
259 std::string* message);
260static void BuildRtpMap(const MediaContentDescription* media_desc,
261 const MediaType media_type,
262 std::string* message);
263static void BuildCandidate(const std::vector<Candidate>& candidates,
264 std::string* message);
265static void BuildIceOptions(const std::vector<std::string>& transport_options,
266 std::string* message);
pthatcher@webrtc.org3341b402015-02-13 21:14:22 +0000267static bool IsRtp(const std::string& protocol);
268static bool IsDtlsSctp(const std::string& protocol);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000269static bool ParseSessionDescription(const std::string& message, size_t* pos,
270 std::string* session_id,
271 std::string* session_version,
272 bool* supports_msid,
273 TransportDescription* session_td,
274 RtpHeaderExtensions* session_extmaps,
275 cricket::SessionDescription* desc,
276 SdpParseError* error);
277static bool ParseGroupAttribute(const std::string& line,
278 cricket::SessionDescription* desc,
279 SdpParseError* error);
280static bool ParseMediaDescription(
281 const std::string& message,
282 const TransportDescription& session_td,
283 const RtpHeaderExtensions& session_extmaps,
284 bool supports_msid,
285 size_t* pos, cricket::SessionDescription* desc,
286 std::vector<JsepIceCandidate*>* candidates,
287 SdpParseError* error);
288static bool ParseContent(const std::string& message,
289 const MediaType media_type,
290 int mline_index,
291 const std::string& protocol,
292 const std::vector<int>& codec_preference,
293 size_t* pos,
294 std::string* content_name,
295 MediaContentDescription* media_desc,
296 TransportDescription* transport,
297 std::vector<JsepIceCandidate*>* candidates,
298 SdpParseError* error);
299static bool ParseSsrcAttribute(const std::string& line,
300 SsrcInfoVec* ssrc_infos,
301 SdpParseError* error);
302static bool ParseSsrcGroupAttribute(const std::string& line,
303 SsrcGroupVec* ssrc_groups,
304 SdpParseError* error);
305static bool ParseCryptoAttribute(const std::string& line,
306 MediaContentDescription* media_desc,
307 SdpParseError* error);
308static bool ParseRtpmapAttribute(const std::string& line,
309 const MediaType media_type,
310 const std::vector<int>& codec_preference,
311 MediaContentDescription* media_desc,
312 SdpParseError* error);
313static bool ParseFmtpAttributes(const std::string& line,
314 const MediaType media_type,
315 MediaContentDescription* media_desc,
316 SdpParseError* error);
317static bool ParseFmtpParam(const std::string& line, std::string* parameter,
318 std::string* value, SdpParseError* error);
319static bool ParseCandidate(const std::string& message, Candidate* candidate,
320 SdpParseError* error, bool is_raw);
321static bool ParseRtcpFbAttribute(const std::string& line,
322 const MediaType media_type,
323 MediaContentDescription* media_desc,
324 SdpParseError* error);
325static bool ParseIceOptions(const std::string& line,
326 std::vector<std::string>* transport_options,
327 SdpParseError* error);
328static bool ParseExtmap(const std::string& line,
329 RtpHeaderExtension* extmap,
330 SdpParseError* error);
331static bool ParseFingerprintAttribute(const std::string& line,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000332 rtc::SSLFingerprint** fingerprint,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000333 SdpParseError* error);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +0000334static bool ParseDtlsSetup(const std::string& line,
335 cricket::ConnectionRole* role,
336 SdpParseError* error);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000337
338// Helper functions
339
340// Below ParseFailed*** functions output the line that caused the parsing
341// failure and the detailed reason (|description|) of the failure to |error|.
342// The functions always return false so that they can be used directly in the
343// following way when error happens:
344// "return ParseFailed***(...);"
345
346// The line starting at |line_start| of |message| is the failing line.
347// The reason for the failure should be provided in the |description|.
348// An example of a description could be "unknown character".
349static bool ParseFailed(const std::string& message,
350 size_t line_start,
351 const std::string& description,
352 SdpParseError* error) {
353 // Get the first line of |message| from |line_start|.
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +0000354 std::string first_line;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000355 size_t line_end = message.find(kNewLine, line_start);
356 if (line_end != std::string::npos) {
357 if (line_end > 0 && (message.at(line_end - 1) == kReturn)) {
358 --line_end;
359 }
360 first_line = message.substr(line_start, (line_end - line_start));
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +0000361 } else {
362 first_line = message.substr(line_start);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000363 }
364
365 if (error) {
366 error->line = first_line;
367 error->description = description;
368 }
369 LOG(LS_ERROR) << "Failed to parse: \"" << first_line
370 << "\". Reason: " << description;
371 return false;
372}
373
374// |line| is the failing line. The reason for the failure should be
375// provided in the |description|.
376static bool ParseFailed(const std::string& line,
377 const std::string& description,
378 SdpParseError* error) {
379 return ParseFailed(line, 0, description, error);
380}
381
382// Parses failure where the failing SDP line isn't know or there are multiple
383// failing lines.
384static bool ParseFailed(const std::string& description,
385 SdpParseError* error) {
386 return ParseFailed("", description, error);
387}
388
389// |line| is the failing line. The failure is due to the fact that |line|
390// doesn't have |expected_fields| fields.
391static bool ParseFailedExpectFieldNum(const std::string& line,
392 int expected_fields,
393 SdpParseError* error) {
394 std::ostringstream description;
395 description << "Expects " << expected_fields << " fields.";
396 return ParseFailed(line, description.str(), error);
397}
398
399// |line| is the failing line. The failure is due to the fact that |line| has
400// less than |expected_min_fields| fields.
401static bool ParseFailedExpectMinFieldNum(const std::string& line,
402 int expected_min_fields,
403 SdpParseError* error) {
404 std::ostringstream description;
405 description << "Expects at least " << expected_min_fields << " fields.";
406 return ParseFailed(line, description.str(), error);
407}
408
409// |line| is the failing line. The failure is due to the fact that it failed to
410// get the value of |attribute|.
411static bool ParseFailedGetValue(const std::string& line,
412 const std::string& attribute,
413 SdpParseError* error) {
414 std::ostringstream description;
415 description << "Failed to get the value of attribute: " << attribute;
416 return ParseFailed(line, description.str(), error);
417}
418
419// The line starting at |line_start| of |message| is the failing line. The
420// failure is due to the line type (e.g. the "m" part of the "m-line")
421// not matching what is expected. The expected line type should be
422// provided as |line_type|.
423static bool ParseFailedExpectLine(const std::string& message,
424 size_t line_start,
425 const char line_type,
426 const std::string& line_value,
427 SdpParseError* error) {
428 std::ostringstream description;
429 description << "Expect line: " << line_type << "=" << line_value;
430 return ParseFailed(message, line_start, description.str(), error);
431}
432
433static bool AddLine(const std::string& line, std::string* message) {
434 if (!message)
435 return false;
436
437 message->append(line);
438 message->append(kLineBreak);
439 return true;
440}
441
442static bool GetLine(const std::string& message,
443 size_t* pos,
444 std::string* line) {
445 size_t line_begin = *pos;
446 size_t line_end = message.find(kNewLine, line_begin);
447 if (line_end == std::string::npos) {
448 return false;
449 }
450 // Update the new start position
451 *pos = line_end + 1;
452 if (line_end > 0 && (message.at(line_end - 1) == kReturn)) {
453 --line_end;
454 }
455 *line = message.substr(line_begin, (line_end - line_begin));
456 const char* cline = line->c_str();
457 // RFC 4566
458 // An SDP session description consists of a number of lines of text of
459 // the form:
460 // <type>=<value>
461 // where <type> MUST be exactly one case-significant character and
462 // <value> is structured text whose format depends on <type>.
463 // Whitespace MUST NOT be used on either side of the "=" sign.
decurtis@webrtc.org8af11042015-01-07 19:15:51 +0000464 if (line->length() < 3 ||
465 !islower(cline[0]) ||
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000466 cline[1] != kSdpDelimiterEqual ||
467 cline[2] == kSdpDelimiterSpace) {
468 *pos = line_begin;
469 return false;
470 }
471 return true;
472}
473
474// Init |os| to "|type|=|value|".
475static void InitLine(const char type,
476 const std::string& value,
477 std::ostringstream* os) {
478 os->str("");
479 *os << type << kSdpDelimiterEqual << value;
480}
481
482// Init |os| to "a=|attribute|".
483static void InitAttrLine(const std::string& attribute, std::ostringstream* os) {
484 InitLine(kLineTypeAttributes, attribute, os);
485}
486
487// Writes a SDP attribute line based on |attribute| and |value| to |message|.
488static void AddAttributeLine(const std::string& attribute, int value,
489 std::string* message) {
490 std::ostringstream os;
491 InitAttrLine(attribute, &os);
492 os << kSdpDelimiterColon << value;
493 AddLine(os.str(), message);
494}
495
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000496static bool IsLineType(const std::string& message,
497 const char type,
498 size_t line_start) {
499 if (message.size() < line_start + kLinePrefixLength) {
500 return false;
501 }
502 const char* cmessage = message.c_str();
503 return (cmessage[line_start] == type &&
504 cmessage[line_start + 1] == kSdpDelimiterEqual);
505}
506
507static bool IsLineType(const std::string& line,
508 const char type) {
509 return IsLineType(line, type, 0);
510}
511
512static bool GetLineWithType(const std::string& message, size_t* pos,
513 std::string* line, const char type) {
514 if (!IsLineType(message, type, *pos)) {
515 return false;
516 }
517
518 if (!GetLine(message, pos, line))
519 return false;
520
521 return true;
522}
523
524static bool HasAttribute(const std::string& line,
525 const std::string& attribute) {
526 return (line.compare(kLinePrefixLength, attribute.size(), attribute) == 0);
527}
528
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000529static bool AddSsrcLine(uint32 ssrc_id, const std::string& attribute,
530 const std::string& value, std::string* message) {
531 // RFC 5576
532 // a=ssrc:<ssrc-id> <attribute>:<value>
533 std::ostringstream os;
534 InitAttrLine(kAttributeSsrc, &os);
535 os << kSdpDelimiterColon << ssrc_id << kSdpDelimiterSpace
536 << attribute << kSdpDelimiterColon << value;
537 return AddLine(os.str(), message);
538}
539
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000540// Get value only from <attribute>:<value>.
541static bool GetValue(const std::string& message, const std::string& attribute,
542 std::string* value, SdpParseError* error) {
543 std::string leftpart;
Donald Curtis0e07f922015-05-15 09:21:23 -0700544 if (!rtc::tokenize_first(message, kSdpDelimiterColon, &leftpart, value)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000545 return ParseFailedGetValue(message, attribute, error);
546 }
547 // The left part should end with the expected attribute.
548 if (leftpart.length() < attribute.length() ||
549 leftpart.compare(leftpart.length() - attribute.length(),
550 attribute.length(), attribute) != 0) {
551 return ParseFailedGetValue(message, attribute, error);
552 }
553 return true;
554}
555
556static bool CaseInsensitiveFind(std::string str1, std::string str2) {
557 std::transform(str1.begin(), str1.end(), str1.begin(),
558 ::tolower);
559 std::transform(str2.begin(), str2.end(), str2.begin(),
560 ::tolower);
561 return str1.find(str2) != std::string::npos;
562}
563
wu@webrtc.org5e760e72014-04-02 23:19:09 +0000564template <class T>
565static bool GetValueFromString(const std::string& line,
566 const std::string& s,
567 T* t,
568 SdpParseError* error) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000569 if (!rtc::FromString(s, t)) {
wu@webrtc.org5e760e72014-04-02 23:19:09 +0000570 std::ostringstream description;
571 description << "Invalid value: " << s << ".";
572 return ParseFailed(line, description.str(), error);
573 }
574 return true;
575}
576
pkasting@chromium.orge9facf82015-02-17 20:36:28 +0000577static bool GetPayloadTypeFromString(const std::string& line,
578 const std::string& s,
579 int* payload_type,
580 SdpParseError* error) {
581 return GetValueFromString(line, s, payload_type, error) &&
582 cricket::IsValidRtpPayloadType(*payload_type);
583}
584
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000585void CreateTracksFromSsrcInfos(const SsrcInfoVec& ssrc_infos,
586 StreamParamsVec* tracks) {
587 ASSERT(tracks != NULL);
588 for (SsrcInfoVec::const_iterator ssrc_info = ssrc_infos.begin();
589 ssrc_info != ssrc_infos.end(); ++ssrc_info) {
590 if (ssrc_info->cname.empty()) {
591 continue;
592 }
593
594 std::string sync_label;
595 std::string track_id;
596 if (ssrc_info->msid_identifier == kDefaultMsid &&
597 !ssrc_info->mslabel.empty()) {
598 // If there's no msid and there's mslabel, we consider this is a sdp from
599 // a older version of client that doesn't support msid.
600 // In that case, we use the mslabel and label to construct the track.
601 sync_label = ssrc_info->mslabel;
602 track_id = ssrc_info->label;
603 } else {
604 sync_label = ssrc_info->msid_identifier;
605 // The appdata consists of the "id" attribute of a MediaStreamTrack, which
606 // is corresponding to the "id" attribute of StreamParams.
607 track_id = ssrc_info->msid_appdata;
608 }
609 if (sync_label.empty() || track_id.empty()) {
610 ASSERT(false);
611 continue;
612 }
613
614 StreamParamsVec::iterator track = tracks->begin();
615 for (; track != tracks->end(); ++track) {
616 if (track->id == track_id) {
617 break;
618 }
619 }
620 if (track == tracks->end()) {
621 // If we don't find an existing track, create a new one.
622 tracks->push_back(StreamParams());
623 track = tracks->end() - 1;
624 }
625 track->add_ssrc(ssrc_info->ssrc_id);
626 track->cname = ssrc_info->cname;
627 track->sync_label = sync_label;
628 track->id = track_id;
629 }
630}
631
632void GetMediaStreamLabels(const ContentInfo* content,
633 std::set<std::string>* labels) {
634 const MediaContentDescription* media_desc =
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000635 static_cast<const MediaContentDescription*>(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000636 content->description);
637 const cricket::StreamParamsVec& streams = media_desc->streams();
638 for (cricket::StreamParamsVec::const_iterator it = streams.begin();
639 it != streams.end(); ++it) {
640 labels->insert(it->sync_label);
641 }
642}
643
644// RFC 5245
645// It is RECOMMENDED that default candidates be chosen based on the
646// likelihood of those candidates to work with the peer that is being
647// contacted. It is RECOMMENDED that relayed > reflexive > host.
648static const int kPreferenceUnknown = 0;
649static const int kPreferenceHost = 1;
650static const int kPreferenceReflexive = 2;
651static const int kPreferenceRelayed = 3;
652
653static int GetCandidatePreferenceFromType(const std::string& type) {
654 int preference = kPreferenceUnknown;
655 if (type == cricket::LOCAL_PORT_TYPE) {
656 preference = kPreferenceHost;
657 } else if (type == cricket::STUN_PORT_TYPE) {
658 preference = kPreferenceReflexive;
659 } else if (type == cricket::RELAY_PORT_TYPE) {
660 preference = kPreferenceRelayed;
661 } else {
662 ASSERT(false);
663 }
664 return preference;
665}
666
guoweis@webrtc.org57ac2c82015-02-06 00:45:13 +0000667// Get ip and port of the default destination from the |candidates| with the
668// given value of |component_id|. The default candidate should be the one most
669// likely to work, typically IPv4 relay.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000670// RFC 5245
671// The value of |component_id| currently supported are 1 (RTP) and 2 (RTCP).
672// TODO: Decide the default destination in webrtcsession and
673// pass it down via SessionDescription.
pthatcher@webrtc.orgc9d6d142014-10-23 23:37:22 +0000674static void GetDefaultDestination(
675 const std::vector<Candidate>& candidates,
676 int component_id, std::string* port,
677 std::string* ip, std::string* addr_type) {
perkj@webrtc.orgd105cc82014-11-07 11:22:06 +0000678 *addr_type = kConnectionIpv4Addrtype;
pthatcher@webrtc.orgc9d6d142014-10-23 23:37:22 +0000679 *port = kDummyPort;
680 *ip = kDummyAddress;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000681 int current_preference = kPreferenceUnknown;
guoweis@webrtc.org57ac2c82015-02-06 00:45:13 +0000682 int current_family = AF_UNSPEC;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000683 for (std::vector<Candidate>::const_iterator it = candidates.begin();
684 it != candidates.end(); ++it) {
685 if (it->component() != component_id) {
686 continue;
687 }
guoweis@webrtc.org57ac2c82015-02-06 00:45:13 +0000688 // Default destination should be UDP only.
689 if (it->protocol() != cricket::UDP_PROTOCOL_NAME) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000690 continue;
691 }
guoweis@webrtc.org57ac2c82015-02-06 00:45:13 +0000692 const int preference = GetCandidatePreferenceFromType(it->type());
693 const int family = it->address().ipaddr().family();
694 // See if this candidate is more preferable then the current one if it's the
695 // same family. Or if the current family is IPv4 already so we could safely
696 // ignore all IPv6 ones. WebRTC bug 4269.
697 // http://code.google.com/p/webrtc/issues/detail?id=4269
698 if ((preference <= current_preference && current_family == family) ||
699 (current_family == AF_INET && family == AF_INET6)) {
700 continue;
701 }
pthatcher@webrtc.orgc9d6d142014-10-23 23:37:22 +0000702 if (family == AF_INET) {
703 addr_type->assign(kConnectionIpv4Addrtype);
704 } else if (family == AF_INET6) {
705 addr_type->assign(kConnectionIpv6Addrtype);
706 }
guoweis@webrtc.org57ac2c82015-02-06 00:45:13 +0000707 current_preference = preference;
708 current_family = family;
709 *port = it->address().PortAsString();
710 *ip = it->address().ipaddr().ToString();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000711 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000712}
713
wu@webrtc.org4c3e9912014-07-16 21:03:13 +0000714// Update |mline|'s default destination and append a c line after it.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000715static void UpdateMediaDefaultDestination(
wu@webrtc.org4c3e9912014-07-16 21:03:13 +0000716 const std::vector<Candidate>& candidates,
jbauch083b73f2015-07-16 02:46:32 -0700717 const std::string& mline,
wu@webrtc.org4c3e9912014-07-16 21:03:13 +0000718 std::string* message) {
719 std::string new_lines;
720 AddLine(mline, &new_lines);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000721 // RFC 4566
722 // m=<media> <port> <proto> <fmt> ...
723 std::vector<std::string> fields;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000724 rtc::split(mline, kSdpDelimiterSpace, &fields);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000725 if (fields.size() < 3) {
726 return;
727 }
728
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000729 std::ostringstream os;
pthatcher@webrtc.orgc9d6d142014-10-23 23:37:22 +0000730 std::string rtp_port, rtp_ip, addr_type;
731 GetDefaultDestination(candidates, ICE_CANDIDATE_COMPONENT_RTP,
732 &rtp_port, &rtp_ip, &addr_type);
733 // Found default RTP candidate.
734 // RFC 5245
735 // The default candidates are added to the SDP as the default
736 // destination for media. For streams based on RTP, this is done by
737 // placing the IP address and port of the RTP candidate into the c and m
738 // lines, respectively.
739 // Update the port in the m line.
740 // If this is a m-line with port equal to 0, we don't change it.
741 if (fields[1] != kMediaPortRejected) {
742 new_lines.replace(fields[0].size() + 1,
743 fields[1].size(),
744 rtp_port);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000745 }
pthatcher@webrtc.orgc9d6d142014-10-23 23:37:22 +0000746 // Add the c line.
747 // RFC 4566
748 // c=<nettype> <addrtype> <connection-address>
749 InitLine(kLineTypeConnection, kConnectionNettype, &os);
750 os << " " << addr_type << " " << rtp_ip;
751 AddLine(os.str(), &new_lines);
wu@webrtc.org4c3e9912014-07-16 21:03:13 +0000752 message->append(new_lines);
753}
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000754
wu@webrtc.org4c3e9912014-07-16 21:03:13 +0000755// Gets "a=rtcp" line if found default RTCP candidate from |candidates|.
756static std::string GetRtcpLine(const std::vector<Candidate>& candidates) {
pthatcher@webrtc.orgc9d6d142014-10-23 23:37:22 +0000757 std::string rtcp_line, rtcp_port, rtcp_ip, addr_type;
758 GetDefaultDestination(candidates, ICE_CANDIDATE_COMPONENT_RTCP,
759 &rtcp_port, &rtcp_ip, &addr_type);
760 // Found default RTCP candidate.
761 // RFC 5245
762 // If the agent is utilizing RTCP, it MUST encode the RTCP candidate
763 // using the a=rtcp attribute as defined in RFC 3605.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000764
pthatcher@webrtc.orgc9d6d142014-10-23 23:37:22 +0000765 // RFC 3605
766 // rtcp-attribute = "a=rtcp:" port [nettype space addrtype space
767 // connection-address] CRLF
768 std::ostringstream os;
769 InitAttrLine(kAttributeRtcp, &os);
770 os << kSdpDelimiterColon
771 << rtcp_port << " "
772 << kConnectionNettype << " "
773 << addr_type << " "
774 << rtcp_ip;
775 rtcp_line = os.str();
wu@webrtc.org4c3e9912014-07-16 21:03:13 +0000776 return rtcp_line;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000777}
778
779// Get candidates according to the mline index from SessionDescriptionInterface.
780static void GetCandidatesByMindex(const SessionDescriptionInterface& desci,
781 int mline_index,
782 std::vector<Candidate>* candidates) {
783 if (!candidates) {
784 return;
785 }
786 const IceCandidateCollection* cc = desci.candidates(mline_index);
787 for (size_t i = 0; i < cc->count(); ++i) {
788 const IceCandidateInterface* candidate = cc->at(i);
789 candidates->push_back(candidate->candidate());
790 }
791}
792
793std::string SdpSerialize(const JsepSessionDescription& jdesc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000794 const cricket::SessionDescription* desc = jdesc.description();
795 if (!desc) {
796 return "";
797 }
798
799 std::string message;
800
801 // Session Description.
802 AddLine(kSessionVersion, &message);
803 // Session Origin
804 // RFC 4566
805 // o=<username> <sess-id> <sess-version> <nettype> <addrtype>
806 // <unicast-address>
807 std::ostringstream os;
808 InitLine(kLineTypeOrigin, kSessionOriginUsername, &os);
jbauch083b73f2015-07-16 02:46:32 -0700809 const std::string& session_id = jdesc.session_id().empty() ?
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000810 kSessionOriginSessionId : jdesc.session_id();
jbauch083b73f2015-07-16 02:46:32 -0700811 const std::string& session_version = jdesc.session_version().empty() ?
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000812 kSessionOriginSessionVersion : jdesc.session_version();
813 os << " " << session_id << " " << session_version << " "
814 << kSessionOriginNettype << " " << kSessionOriginAddrtype << " "
815 << kSessionOriginAddress;
816 AddLine(os.str(), &message);
817 AddLine(kSessionName, &message);
818
819 // Time Description.
820 AddLine(kTimeDescription, &message);
821
822 // Group
823 if (desc->HasGroup(cricket::GROUP_TYPE_BUNDLE)) {
824 std::string group_line = kAttrGroup;
825 const cricket::ContentGroup* group =
826 desc->GetGroupByName(cricket::GROUP_TYPE_BUNDLE);
827 ASSERT(group != NULL);
828 const cricket::ContentNames& content_names = group->content_names();
829 for (cricket::ContentNames::const_iterator it = content_names.begin();
830 it != content_names.end(); ++it) {
831 group_line.append(" ");
832 group_line.append(*it);
833 }
834 AddLine(group_line, &message);
835 }
836
837 // MediaStream semantics
838 InitAttrLine(kAttributeMsidSemantics, &os);
839 os << kSdpDelimiterColon << " " << kMediaStreamSemantic;
wu@webrtc.org4c3e9912014-07-16 21:03:13 +0000840
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000841 std::set<std::string> media_stream_labels;
842 const ContentInfo* audio_content = GetFirstAudioContent(desc);
843 if (audio_content)
844 GetMediaStreamLabels(audio_content, &media_stream_labels);
wu@webrtc.org4c3e9912014-07-16 21:03:13 +0000845
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000846 const ContentInfo* video_content = GetFirstVideoContent(desc);
847 if (video_content)
848 GetMediaStreamLabels(video_content, &media_stream_labels);
wu@webrtc.org4c3e9912014-07-16 21:03:13 +0000849
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000850 for (std::set<std::string>::const_iterator it =
851 media_stream_labels.begin(); it != media_stream_labels.end(); ++it) {
852 os << " " << *it;
853 }
854 AddLine(os.str(), &message);
855
wu@webrtc.org4c3e9912014-07-16 21:03:13 +0000856 // Preserve the order of the media contents.
857 int mline_index = -1;
858 for (cricket::ContentInfos::const_iterator it = desc->contents().begin();
859 it != desc->contents().end(); ++it) {
860 const MediaContentDescription* mdesc =
861 static_cast<const MediaContentDescription*>(it->description);
862 std::vector<Candidate> candidates;
863 GetCandidatesByMindex(jdesc, ++mline_index, &candidates);
864 BuildMediaDescription(&*it,
865 desc->GetTransportInfoByName(it->name),
866 mdesc->type(),
867 candidates,
868 &message);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000869 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000870 return message;
871}
872
873// Serializes the passed in IceCandidateInterface to a SDP string.
874// candidate - The candidate to be serialized.
875std::string SdpSerializeCandidate(
876 const IceCandidateInterface& candidate) {
877 std::string message;
878 std::vector<cricket::Candidate> candidates;
879 candidates.push_back(candidate.candidate());
880 BuildCandidate(candidates, &message);
wu@webrtc.orgec9f5fb2014-06-24 17:05:10 +0000881 // From WebRTC draft section 4.8.1.1 candidate-attribute will be
882 // just candidate:<candidate> not a=candidate:<blah>CRLF
883 ASSERT(message.find("a=") == 0);
884 message.erase(0, 2);
885 ASSERT(message.find(kLineBreak) == message.size() - 2);
886 message.resize(message.size() - 2);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000887 return message;
888}
889
890bool SdpDeserialize(const std::string& message,
891 JsepSessionDescription* jdesc,
892 SdpParseError* error) {
893 std::string session_id;
894 std::string session_version;
minyuel5bdafd42015-08-21 15:52:48 +0200895 TransportDescription session_td(NS_JINGLE_ICE_UDP,
896 std::string(), std::string());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000897 RtpHeaderExtensions session_extmaps;
898 cricket::SessionDescription* desc = new cricket::SessionDescription();
899 std::vector<JsepIceCandidate*> candidates;
900 size_t current_pos = 0;
901 bool supports_msid = false;
902
903 // Session Description
904 if (!ParseSessionDescription(message, &current_pos, &session_id,
905 &session_version, &supports_msid, &session_td,
906 &session_extmaps, desc, error)) {
907 delete desc;
908 return false;
909 }
910
911 // Media Description
912 if (!ParseMediaDescription(message, session_td, session_extmaps,
913 supports_msid, &current_pos, desc, &candidates,
914 error)) {
915 delete desc;
916 for (std::vector<JsepIceCandidate*>::const_iterator
917 it = candidates.begin(); it != candidates.end(); ++it) {
918 delete *it;
919 }
920 return false;
921 }
922
923 jdesc->Initialize(desc, session_id, session_version);
924
925 for (std::vector<JsepIceCandidate*>::const_iterator
926 it = candidates.begin(); it != candidates.end(); ++it) {
927 jdesc->AddCandidate(*it);
928 delete *it;
929 }
930 return true;
931}
932
933bool SdpDeserializeCandidate(const std::string& message,
934 JsepIceCandidate* jcandidate,
935 SdpParseError* error) {
936 ASSERT(jcandidate != NULL);
937 Candidate candidate;
938 if (!ParseCandidate(message, &candidate, error, true)) {
939 return false;
940 }
941 jcandidate->SetCandidate(candidate);
942 return true;
943}
944
945bool ParseCandidate(const std::string& message, Candidate* candidate,
946 SdpParseError* error, bool is_raw) {
947 ASSERT(candidate != NULL);
948
949 // Get the first line from |message|.
jiayl@webrtc.org7ec3f9f2014-08-08 23:09:15 +0000950 std::string first_line = message;
951 size_t pos = 0;
952 GetLine(message, &pos, &first_line);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000953
jiayl@webrtc.org7ec3f9f2014-08-08 23:09:15 +0000954 // Makes sure |message| contains only one line.
955 if (message.size() > first_line.size()) {
956 std::string left, right;
Donald Curtis0e07f922015-05-15 09:21:23 -0700957 if (rtc::tokenize_first(message, kNewLine, &left, &right) &&
958 !right.empty()) {
jiayl@webrtc.org7ec3f9f2014-08-08 23:09:15 +0000959 return ParseFailed(message, 0, "Expect one line only", error);
960 }
961 }
962
963 // From WebRTC draft section 4.8.1.1 candidate-attribute should be
964 // candidate:<candidate> when trickled, but we still support
965 // a=candidate:<blah>CRLF for backward compatibility and for parsing a line
966 // from the SDP.
967 if (IsLineType(first_line, kLineTypeAttributes)) {
968 first_line = first_line.substr(kLinePrefixLength);
969 }
970
971 std::string attribute_candidate;
972 std::string candidate_value;
973
974 // |first_line| must be in the form of "candidate:<value>".
Donald Curtis144d0182015-05-15 13:14:24 -0700975 if (!rtc::tokenize_first(first_line, kSdpDelimiterColon, &attribute_candidate,
976 &candidate_value) ||
jiayl@webrtc.org7ec3f9f2014-08-08 23:09:15 +0000977 attribute_candidate != kAttributeCandidate) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000978 if (is_raw) {
979 std::ostringstream description;
jiayl@webrtc.org7ec3f9f2014-08-08 23:09:15 +0000980 description << "Expect line: " << kAttributeCandidate
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000981 << ":" << "<candidate-str>";
982 return ParseFailed(first_line, 0, description.str(), error);
983 } else {
984 return ParseFailedExpectLine(first_line, 0, kLineTypeAttributes,
985 kAttributeCandidate, error);
986 }
987 }
988
989 std::vector<std::string> fields;
jiayl@webrtc.org7ec3f9f2014-08-08 23:09:15 +0000990 rtc::split(candidate_value, kSdpDelimiterSpace, &fields);
991
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000992 // RFC 5245
993 // a=candidate:<foundation> <component-id> <transport> <priority>
994 // <connection-address> <port> typ <candidate-types>
995 // [raddr <connection-address>] [rport <port>]
996 // *(SP extension-att-name SP extension-att-value)
997 const size_t expected_min_fields = 8;
998 if (fields.size() < expected_min_fields ||
999 (fields[6] != kAttributeCandidateTyp)) {
1000 return ParseFailedExpectMinFieldNum(first_line, expected_min_fields, error);
1001 }
jbauch083b73f2015-07-16 02:46:32 -07001002 const std::string& foundation = fields[0];
jiayl@webrtc.org7ec3f9f2014-08-08 23:09:15 +00001003
wu@webrtc.org5e760e72014-04-02 23:19:09 +00001004 int component_id = 0;
1005 if (!GetValueFromString(first_line, fields[1], &component_id, error)) {
1006 return false;
1007 }
jbauch083b73f2015-07-16 02:46:32 -07001008 const std::string& transport = fields[2];
wu@webrtc.org5e760e72014-04-02 23:19:09 +00001009 uint32 priority = 0;
1010 if (!GetValueFromString(first_line, fields[3], &priority, error)) {
1011 return false;
1012 }
jbauch083b73f2015-07-16 02:46:32 -07001013 const std::string& connection_address = fields[4];
wu@webrtc.org5e760e72014-04-02 23:19:09 +00001014 int port = 0;
1015 if (!GetValueFromString(first_line, fields[5], &port, error)) {
1016 return false;
1017 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001018 SocketAddress address(connection_address, port);
1019
1020 cricket::ProtocolType protocol;
1021 if (!StringToProto(transport.c_str(), &protocol)) {
1022 return ParseFailed(first_line, "Unsupported transport type.", error);
1023 }
1024
1025 std::string candidate_type;
jbauch083b73f2015-07-16 02:46:32 -07001026 const std::string& type = fields[7];
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001027 if (type == kCandidateHost) {
1028 candidate_type = cricket::LOCAL_PORT_TYPE;
1029 } else if (type == kCandidateSrflx) {
1030 candidate_type = cricket::STUN_PORT_TYPE;
1031 } else if (type == kCandidateRelay) {
1032 candidate_type = cricket::RELAY_PORT_TYPE;
1033 } else {
1034 return ParseFailed(first_line, "Unsupported candidate type.", error);
1035 }
1036
1037 size_t current_position = expected_min_fields;
1038 SocketAddress related_address;
1039 // The 2 optional fields for related address
1040 // [raddr <connection-address>] [rport <port>]
1041 if (fields.size() >= (current_position + 2) &&
1042 fields[current_position] == kAttributeCandidateRaddr) {
1043 related_address.SetIP(fields[++current_position]);
1044 ++current_position;
1045 }
1046 if (fields.size() >= (current_position + 2) &&
1047 fields[current_position] == kAttributeCandidateRport) {
wu@webrtc.org5e760e72014-04-02 23:19:09 +00001048 int port = 0;
1049 if (!GetValueFromString(
1050 first_line, fields[++current_position], &port, error)) {
1051 return false;
1052 }
1053 related_address.SetPort(port);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001054 ++current_position;
1055 }
1056
mallinath@webrtc.org2d60c5e2014-08-08 22:29:20 +00001057 // If this is a TCP candidate, it has additional extension as defined in
1058 // RFC 6544.
1059 std::string tcptype;
1060 if (fields.size() >= (current_position + 2) &&
1061 fields[current_position] == kTcpCandidateType) {
1062 tcptype = fields[++current_position];
1063 ++current_position;
1064
1065 if (tcptype != cricket::TCPTYPE_ACTIVE_STR &&
1066 tcptype != cricket::TCPTYPE_PASSIVE_STR &&
1067 tcptype != cricket::TCPTYPE_SIMOPEN_STR) {
1068 return ParseFailed(first_line, "Invalid TCP candidate type.", error);
1069 }
1070
1071 if (protocol != cricket::PROTO_TCP) {
1072 return ParseFailed(first_line, "Invalid non-TCP candidate", error);
1073 }
1074 }
1075
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001076 // Extension
1077 // Empty string as the candidate username and password.
1078 // Will be updated later with the ice-ufrag and ice-pwd.
1079 // TODO: Remove the username/password extension, which is currently
1080 // kept for backwards compatibility.
1081 std::string username;
1082 std::string password;
1083 uint32 generation = 0;
1084 for (size_t i = current_position; i + 1 < fields.size(); ++i) {
1085 // RFC 5245
1086 // *(SP extension-att-name SP extension-att-value)
1087 if (fields[i] == kAttributeCandidateGeneration) {
wu@webrtc.org5e760e72014-04-02 23:19:09 +00001088 if (!GetValueFromString(first_line, fields[++i], &generation, error)) {
1089 return false;
1090 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001091 } else if (fields[i] == kAttributeCandidateUsername) {
1092 username = fields[++i];
1093 } else if (fields[i] == kAttributeCandidatePassword) {
1094 password = fields[++i];
1095 } else {
1096 // Skip the unknown extension.
1097 ++i;
1098 }
1099 }
1100
guoweis@webrtc.org61c12472015-01-15 06:53:07 +00001101 *candidate = Candidate(component_id, cricket::ProtoToString(protocol),
guoweis@webrtc.org950c5182014-12-16 23:01:31 +00001102 address, priority, username, password, candidate_type,
1103 generation, foundation);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001104 candidate->set_related_address(related_address);
mallinath@webrtc.org2d60c5e2014-08-08 22:29:20 +00001105 candidate->set_tcptype(tcptype);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001106 return true;
1107}
1108
1109bool ParseIceOptions(const std::string& line,
1110 std::vector<std::string>* transport_options,
1111 SdpParseError* error) {
1112 std::string ice_options;
1113 if (!GetValue(line, kAttributeIceOption, &ice_options, error)) {
1114 return false;
1115 }
1116 std::vector<std::string> fields;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001117 rtc::split(ice_options, kSdpDelimiterSpace, &fields);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001118 for (size_t i = 0; i < fields.size(); ++i) {
1119 transport_options->push_back(fields[i]);
1120 }
1121 return true;
1122}
1123
jiayl@webrtc.orgddb85ab2014-09-05 16:31:56 +00001124bool ParseSctpPort(const std::string& line,
1125 int* sctp_port,
1126 SdpParseError* error) {
1127 // draft-ietf-mmusic-sctp-sdp-07
1128 // a=sctp-port
1129 std::vector<std::string> fields;
1130 rtc::split(line.substr(kLinePrefixLength),
1131 kSdpDelimiterSpace, &fields);
1132 const size_t expected_min_fields = 2;
1133 if (fields.size() < expected_min_fields) {
1134 return ParseFailedExpectMinFieldNum(line, expected_min_fields, error);
1135 }
1136 if (!rtc::FromString(fields[1], sctp_port)) {
1137 return ParseFailed(line,
1138 "Invalid sctp port value.",
1139 error);
1140 }
1141 return true;
1142}
1143
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001144bool ParseExtmap(const std::string& line, RtpHeaderExtension* extmap,
1145 SdpParseError* error) {
1146 // RFC 5285
1147 // a=extmap:<value>["/"<direction>] <URI> <extensionattributes>
1148 std::vector<std::string> fields;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001149 rtc::split(line.substr(kLinePrefixLength),
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001150 kSdpDelimiterSpace, &fields);
1151 const size_t expected_min_fields = 2;
1152 if (fields.size() < expected_min_fields) {
1153 return ParseFailedExpectMinFieldNum(line, expected_min_fields, error);
1154 }
1155 std::string uri = fields[1];
1156
1157 std::string value_direction;
1158 if (!GetValue(fields[0], kAttributeExtmap, &value_direction, error)) {
1159 return false;
1160 }
1161 std::vector<std::string> sub_fields;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001162 rtc::split(value_direction, kSdpDelimiterSlash, &sub_fields);
wu@webrtc.org5e760e72014-04-02 23:19:09 +00001163 int value = 0;
1164 if (!GetValueFromString(line, sub_fields[0], &value, error)) {
1165 return false;
1166 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001167
1168 *extmap = RtpHeaderExtension(uri, value);
1169 return true;
1170}
1171
1172void BuildMediaDescription(const ContentInfo* content_info,
1173 const TransportInfo* transport_info,
1174 const MediaType media_type,
wu@webrtc.org4c3e9912014-07-16 21:03:13 +00001175 const std::vector<Candidate>& candidates,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001176 std::string* message) {
1177 ASSERT(message != NULL);
1178 if (content_info == NULL || message == NULL) {
1179 return;
1180 }
1181 // TODO: Rethink if we should use sprintfn instead of stringstream.
1182 // According to the style guide, streams should only be used for logging.
1183 // http://google-styleguide.googlecode.com/svn/
1184 // trunk/cppguide.xml?showone=Streams#Streams
1185 std::ostringstream os;
1186 const MediaContentDescription* media_desc =
henrike@webrtc.org28654cb2013-07-22 21:07:49 +00001187 static_cast<const MediaContentDescription*>(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001188 content_info->description);
1189 ASSERT(media_desc != NULL);
1190
jiayl@webrtc.org9c16c392014-05-01 18:30:30 +00001191 int sctp_port = cricket::kSctpDefaultPort;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001192
1193 // RFC 4566
1194 // m=<media> <port> <proto> <fmt>
1195 // fmt is a list of payload type numbers that MAY be used in the session.
1196 const char* type = NULL;
1197 if (media_type == cricket::MEDIA_TYPE_AUDIO)
1198 type = kMediaTypeAudio;
1199 else if (media_type == cricket::MEDIA_TYPE_VIDEO)
1200 type = kMediaTypeVideo;
1201 else if (media_type == cricket::MEDIA_TYPE_DATA)
1202 type = kMediaTypeData;
1203 else
1204 ASSERT(false);
1205
1206 std::string fmt;
1207 if (media_type == cricket::MEDIA_TYPE_VIDEO) {
1208 const VideoContentDescription* video_desc =
1209 static_cast<const VideoContentDescription*>(media_desc);
1210 for (std::vector<cricket::VideoCodec>::const_iterator it =
1211 video_desc->codecs().begin();
1212 it != video_desc->codecs().end(); ++it) {
1213 fmt.append(" ");
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001214 fmt.append(rtc::ToString<int>(it->id));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001215 }
1216 } else if (media_type == cricket::MEDIA_TYPE_AUDIO) {
1217 const AudioContentDescription* audio_desc =
1218 static_cast<const AudioContentDescription*>(media_desc);
1219 for (std::vector<cricket::AudioCodec>::const_iterator it =
1220 audio_desc->codecs().begin();
1221 it != audio_desc->codecs().end(); ++it) {
1222 fmt.append(" ");
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001223 fmt.append(rtc::ToString<int>(it->id));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001224 }
1225 } else if (media_type == cricket::MEDIA_TYPE_DATA) {
jiayl@webrtc.org9c16c392014-05-01 18:30:30 +00001226 const DataContentDescription* data_desc =
1227 static_cast<const DataContentDescription*>(media_desc);
pthatcher@webrtc.org3341b402015-02-13 21:14:22 +00001228 if (IsDtlsSctp(media_desc->protocol())) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001229 fmt.append(" ");
jiayl@webrtc.org9c16c392014-05-01 18:30:30 +00001230
1231 for (std::vector<cricket::DataCodec>::const_iterator it =
1232 data_desc->codecs().begin();
1233 it != data_desc->codecs().end(); ++it) {
1234 if (it->id == cricket::kGoogleSctpDataCodecId &&
1235 it->GetParam(cricket::kCodecParamPort, &sctp_port)) {
1236 break;
1237 }
1238 }
1239
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001240 fmt.append(rtc::ToString<int>(sctp_port));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001241 } else {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001242 for (std::vector<cricket::DataCodec>::const_iterator it =
1243 data_desc->codecs().begin();
1244 it != data_desc->codecs().end(); ++it) {
1245 fmt.append(" ");
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001246 fmt.append(rtc::ToString<int>(it->id));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001247 }
1248 }
1249 }
1250 // The fmt must never be empty. If no codecs are found, set the fmt attribute
1251 // to 0.
1252 if (fmt.empty()) {
1253 fmt = " 0";
1254 }
1255
1256 // The port number in the m line will be updated later when associate with
1257 // the candidates.
1258 // RFC 3264
1259 // To reject an offered stream, the port number in the corresponding stream in
1260 // the answer MUST be set to zero.
jbauch083b73f2015-07-16 02:46:32 -07001261 const std::string& port = content_info->rejected ?
pthatcher@webrtc.orgc9d6d142014-10-23 23:37:22 +00001262 kMediaPortRejected : kDummyPort;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001263
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001264 rtc::SSLFingerprint* fp = (transport_info) ?
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001265 transport_info->description.identity_fingerprint.get() : NULL;
1266
wu@webrtc.org4c3e9912014-07-16 21:03:13 +00001267 // Add the m and c lines.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001268 InitLine(kLineTypeMedia, type, &os);
1269 os << " " << port << " " << media_desc->protocol() << fmt;
wu@webrtc.org4c3e9912014-07-16 21:03:13 +00001270 std::string mline = os.str();
1271 UpdateMediaDefaultDestination(candidates, mline, message);
1272
1273 // RFC 4566
1274 // b=AS:<bandwidth>
Peter Thatcherc0c3a862015-06-24 15:31:25 -07001275 if (media_desc->bandwidth() >= 1000) {
wu@webrtc.org4c3e9912014-07-16 21:03:13 +00001276 InitLine(kLineTypeSessionBandwidth, kApplicationSpecificMaximum, &os);
1277 os << kSdpDelimiterColon << (media_desc->bandwidth() / 1000);
1278 AddLine(os.str(), message);
1279 }
1280
1281 // Add the a=rtcp line.
pthatcher@webrtc.org3341b402015-02-13 21:14:22 +00001282 if (IsRtp(media_desc->protocol())) {
wu@webrtc.org4c3e9912014-07-16 21:03:13 +00001283 std::string rtcp_line = GetRtcpLine(candidates);
1284 if (!rtcp_line.empty()) {
1285 AddLine(rtcp_line, message);
1286 }
1287 }
1288
1289 // Build the a=candidate lines.
1290 BuildCandidate(candidates, message);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001291
1292 // Use the transport_info to build the media level ice-ufrag and ice-pwd.
1293 if (transport_info) {
1294 // RFC 5245
1295 // ice-pwd-att = "ice-pwd" ":" password
1296 // ice-ufrag-att = "ice-ufrag" ":" ufrag
1297 // ice-ufrag
1298 InitAttrLine(kAttributeIceUfrag, &os);
1299 os << kSdpDelimiterColon << transport_info->description.ice_ufrag;
1300 AddLine(os.str(), message);
1301 // ice-pwd
1302 InitAttrLine(kAttributeIcePwd, &os);
1303 os << kSdpDelimiterColon << transport_info->description.ice_pwd;
1304 AddLine(os.str(), message);
1305
1306 // draft-petithuguenin-mmusic-ice-attributes-level-03
1307 BuildIceOptions(transport_info->description.transport_options, message);
1308
1309 // RFC 4572
1310 // fingerprint-attribute =
1311 // "fingerprint" ":" hash-func SP fingerprint
1312 if (fp) {
1313 // Insert the fingerprint attribute.
1314 InitAttrLine(kAttributeFingerprint, &os);
1315 os << kSdpDelimiterColon
1316 << fp->algorithm << kSdpDelimiterSpace
1317 << fp->GetRfc4572Fingerprint();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001318 AddLine(os.str(), message);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001319
1320 // Inserting setup attribute.
1321 if (transport_info->description.connection_role !=
1322 cricket::CONNECTIONROLE_NONE) {
1323 // Making sure we are not using "passive" mode.
1324 cricket::ConnectionRole role =
1325 transport_info->description.connection_role;
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00001326 std::string dtls_role_str;
1327 VERIFY(cricket::ConnectionRoleToString(role, &dtls_role_str));
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001328 InitAttrLine(kAttributeSetup, &os);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001329 os << kSdpDelimiterColon << dtls_role_str;
1330 AddLine(os.str(), message);
1331 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001332 }
1333 }
1334
1335 // RFC 3388
1336 // mid-attribute = "a=mid:" identification-tag
1337 // identification-tag = token
1338 // Use the content name as the mid identification-tag.
1339 InitAttrLine(kAttributeMid, &os);
1340 os << kSdpDelimiterColon << content_info->name;
1341 AddLine(os.str(), message);
1342
pthatcher@webrtc.org3341b402015-02-13 21:14:22 +00001343 if (IsDtlsSctp(media_desc->protocol())) {
jiayl@webrtc.org9c16c392014-05-01 18:30:30 +00001344 BuildSctpContentAttributes(message, sctp_port);
pthatcher@webrtc.org3341b402015-02-13 21:14:22 +00001345 } else if (IsRtp(media_desc->protocol())) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001346 BuildRtpContentAttributes(media_desc, media_type, message);
1347 }
1348}
1349
jiayl@webrtc.org9c16c392014-05-01 18:30:30 +00001350void BuildSctpContentAttributes(std::string* message, int sctp_port) {
wu@webrtc.org78187522013-10-07 23:32:02 +00001351 // draft-ietf-mmusic-sctp-sdp-04
1352 // a=sctpmap:sctpmap-number protocol [streams]
1353 std::ostringstream os;
1354 InitAttrLine(kAttributeSctpmap, &os);
jiayl@webrtc.org9c16c392014-05-01 18:30:30 +00001355 os << kSdpDelimiterColon << sctp_port << kSdpDelimiterSpace
wu@webrtc.org78187522013-10-07 23:32:02 +00001356 << kDefaultSctpmapProtocol << kSdpDelimiterSpace
1357 << (cricket::kMaxSctpSid + 1);
1358 AddLine(os.str(), message);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001359}
1360
1361void BuildRtpContentAttributes(
1362 const MediaContentDescription* media_desc,
1363 const MediaType media_type,
1364 std::string* message) {
1365 std::ostringstream os;
1366 // RFC 5285
1367 // a=extmap:<value>["/"<direction>] <URI> <extensionattributes>
1368 // The definitions MUST be either all session level or all media level. This
1369 // implementation uses all media level.
1370 for (size_t i = 0; i < media_desc->rtp_header_extensions().size(); ++i) {
1371 InitAttrLine(kAttributeExtmap, &os);
1372 os << kSdpDelimiterColon << media_desc->rtp_header_extensions()[i].id
1373 << kSdpDelimiterSpace << media_desc->rtp_header_extensions()[i].uri;
1374 AddLine(os.str(), message);
1375 }
1376
1377 // RFC 3264
1378 // a=sendrecv || a=sendonly || a=sendrecv || a=inactive
1379
1380 cricket::MediaContentDirection direction = media_desc->direction();
1381 if (media_desc->streams().empty() && direction == cricket::MD_SENDRECV) {
1382 direction = cricket::MD_RECVONLY;
1383 }
1384
1385 switch (direction) {
1386 case cricket::MD_INACTIVE:
1387 InitAttrLine(kAttributeInactive, &os);
1388 break;
1389 case cricket::MD_SENDONLY:
1390 InitAttrLine(kAttributeSendOnly, &os);
1391 break;
1392 case cricket::MD_RECVONLY:
1393 InitAttrLine(kAttributeRecvOnly, &os);
1394 break;
1395 case cricket::MD_SENDRECV:
1396 default:
1397 InitAttrLine(kAttributeSendRecv, &os);
1398 break;
1399 }
1400 AddLine(os.str(), message);
1401
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001402 // RFC 5761
1403 // a=rtcp-mux
1404 if (media_desc->rtcp_mux()) {
1405 InitAttrLine(kAttributeRtcpMux, &os);
1406 AddLine(os.str(), message);
1407 }
1408
1409 // RFC 4568
1410 // a=crypto:<tag> <crypto-suite> <key-params> [<session-params>]
1411 for (std::vector<CryptoParams>::const_iterator it =
1412 media_desc->cryptos().begin();
1413 it != media_desc->cryptos().end(); ++it) {
1414 InitAttrLine(kAttributeCrypto, &os);
1415 os << kSdpDelimiterColon << it->tag << " " << it->cipher_suite << " "
1416 << it->key_params;
1417 if (!it->session_params.empty()) {
1418 os << " " << it->session_params;
1419 }
1420 AddLine(os.str(), message);
1421 }
1422
1423 // RFC 4566
1424 // a=rtpmap:<payload type> <encoding name>/<clock rate>
1425 // [/<encodingparameters>]
1426 BuildRtpMap(media_desc, media_type, message);
1427
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001428 for (StreamParamsVec::const_iterator track = media_desc->streams().begin();
1429 track != media_desc->streams().end(); ++track) {
1430 // Require that the track belongs to a media stream,
1431 // ie the sync_label is set. This extra check is necessary since the
1432 // MediaContentDescription always contains a streamparam with an ssrc even
1433 // if no track or media stream have been created.
1434 if (track->sync_label.empty()) continue;
1435
1436 // Build the ssrc-group lines.
1437 for (size_t i = 0; i < track->ssrc_groups.size(); ++i) {
1438 // RFC 5576
1439 // a=ssrc-group:<semantics> <ssrc-id> ...
1440 if (track->ssrc_groups[i].ssrcs.empty()) {
1441 continue;
1442 }
1443 std::ostringstream os;
1444 InitAttrLine(kAttributeSsrcGroup, &os);
1445 os << kSdpDelimiterColon << track->ssrc_groups[i].semantics;
1446 std::vector<uint32>::const_iterator ssrc =
1447 track->ssrc_groups[i].ssrcs.begin();
1448 for (; ssrc != track->ssrc_groups[i].ssrcs.end(); ++ssrc) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001449 os << kSdpDelimiterSpace << rtc::ToString<uint32>(*ssrc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001450 }
1451 AddLine(os.str(), message);
1452 }
1453 // Build the ssrc lines for each ssrc.
1454 for (size_t i = 0; i < track->ssrcs.size(); ++i) {
1455 uint32 ssrc = track->ssrcs[i];
1456 // RFC 5576
1457 // a=ssrc:<ssrc-id> cname:<value>
1458 AddSsrcLine(ssrc, kSsrcAttributeCname,
1459 track->cname, message);
1460
1461 // draft-alvestrand-mmusic-msid-00
1462 // a=ssrc:<ssrc-id> msid:identifier [appdata]
1463 // The appdata consists of the "id" attribute of a MediaStreamTrack, which
1464 // is corresponding to the "name" attribute of StreamParams.
1465 std::string appdata = track->id;
1466 std::ostringstream os;
1467 InitAttrLine(kAttributeSsrc, &os);
1468 os << kSdpDelimiterColon << ssrc << kSdpDelimiterSpace
1469 << kSsrcAttributeMsid << kSdpDelimiterColon << track->sync_label
1470 << kSdpDelimiterSpace << appdata;
1471 AddLine(os.str(), message);
1472
1473 // TODO(ronghuawu): Remove below code which is for backward compatibility.
1474 // draft-alvestrand-rtcweb-mid-01
1475 // a=ssrc:<ssrc-id> mslabel:<value>
1476 // The label isn't yet defined.
1477 // a=ssrc:<ssrc-id> label:<value>
1478 AddSsrcLine(ssrc, kSsrcAttributeMslabel, track->sync_label, message);
1479 AddSsrcLine(ssrc, kSSrcAttributeLabel, track->id, message);
1480 }
1481 }
1482}
1483
1484void WriteFmtpHeader(int payload_type, std::ostringstream* os) {
1485 // fmtp header: a=fmtp:|payload_type| <parameters>
1486 // Add a=fmtp
1487 InitAttrLine(kAttributeFmtp, os);
1488 // Add :|payload_type|
1489 *os << kSdpDelimiterColon << payload_type;
1490}
1491
1492void WriteRtcpFbHeader(int payload_type, std::ostringstream* os) {
1493 // rtcp-fb header: a=rtcp-fb:|payload_type|
1494 // <parameters>/<ccm <ccm_parameters>>
1495 // Add a=rtcp-fb
1496 InitAttrLine(kAttributeRtcpFb, os);
1497 // Add :
1498 *os << kSdpDelimiterColon;
1499 if (payload_type == kWildcardPayloadType) {
1500 *os << "*";
1501 } else {
1502 *os << payload_type;
1503 }
1504}
1505
1506void WriteFmtpParameter(const std::string& parameter_name,
1507 const std::string& parameter_value,
1508 std::ostringstream* os) {
1509 // fmtp parameters: |parameter_name|=|parameter_value|
1510 *os << parameter_name << kSdpDelimiterEqual << parameter_value;
1511}
1512
1513void WriteFmtpParameters(const cricket::CodecParameterMap& parameters,
1514 std::ostringstream* os) {
1515 for (cricket::CodecParameterMap::const_iterator fmtp = parameters.begin();
1516 fmtp != parameters.end(); ++fmtp) {
1517 // Each new parameter, except the first one starts with ";" and " ".
1518 if (fmtp != parameters.begin()) {
1519 *os << kSdpDelimiterSemicolon;
1520 }
1521 *os << kSdpDelimiterSpace;
1522 WriteFmtpParameter(fmtp->first, fmtp->second, os);
1523 }
1524}
1525
1526bool IsFmtpParam(const std::string& name) {
1527 const char* kFmtpParams[] = {
1528 kCodecParamMinPTime, kCodecParamSPropStereo,
Minyue Li7100dcd2015-03-27 05:05:59 +01001529 kCodecParamStereo, kCodecParamUseInbandFec, kCodecParamUseDtx,
1530 kCodecParamStartBitrate, kCodecParamMaxBitrate, kCodecParamMinBitrate,
1531 kCodecParamMaxQuantization, kCodecParamSctpProtocol, kCodecParamSctpStreams,
buildbot@webrtc.org5d639b32014-09-10 07:57:12 +00001532 kCodecParamMaxAverageBitrate, kCodecParamMaxPlaybackRate,
1533 kCodecParamAssociatedPayloadType
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001534 };
1535 for (size_t i = 0; i < ARRAY_SIZE(kFmtpParams); ++i) {
1536 if (_stricmp(name.c_str(), kFmtpParams[i]) == 0) {
1537 return true;
1538 }
1539 }
1540 return false;
1541}
1542
1543// Retreives fmtp parameters from |params|, which may contain other parameters
1544// as well, and puts them in |fmtp_parameters|.
1545void GetFmtpParams(const cricket::CodecParameterMap& params,
1546 cricket::CodecParameterMap* fmtp_parameters) {
1547 for (cricket::CodecParameterMap::const_iterator iter = params.begin();
1548 iter != params.end(); ++iter) {
1549 if (IsFmtpParam(iter->first)) {
1550 (*fmtp_parameters)[iter->first] = iter->second;
1551 }
1552 }
1553}
1554
1555template <class T>
1556void AddFmtpLine(const T& codec, std::string* message) {
1557 cricket::CodecParameterMap fmtp_parameters;
1558 GetFmtpParams(codec.params, &fmtp_parameters);
1559 if (fmtp_parameters.empty()) {
1560 // No need to add an fmtp if it will have no (optional) parameters.
1561 return;
1562 }
1563 std::ostringstream os;
1564 WriteFmtpHeader(codec.id, &os);
1565 WriteFmtpParameters(fmtp_parameters, &os);
1566 AddLine(os.str(), message);
1567 return;
1568}
1569
1570template <class T>
1571void AddRtcpFbLines(const T& codec, std::string* message) {
1572 for (std::vector<cricket::FeedbackParam>::const_iterator iter =
1573 codec.feedback_params.params().begin();
1574 iter != codec.feedback_params.params().end(); ++iter) {
1575 std::ostringstream os;
1576 WriteRtcpFbHeader(codec.id, &os);
1577 os << " " << iter->id();
1578 if (!iter->param().empty()) {
1579 os << " " << iter->param();
1580 }
1581 AddLine(os.str(), message);
1582 }
1583}
1584
jiayl@webrtc.orgddb85ab2014-09-05 16:31:56 +00001585bool AddSctpDataCodec(DataContentDescription* media_desc,
1586 int sctp_port) {
1587 if (media_desc->HasCodec(cricket::kGoogleSctpDataCodecId)) {
1588 return ParseFailed("",
1589 "Can't have multiple sctp port attributes.",
1590 NULL);
1591 }
1592 // Add the SCTP Port number as a pseudo-codec "port" parameter
1593 cricket::DataCodec codec_port(
1594 cricket::kGoogleSctpDataCodecId, cricket::kGoogleSctpDataCodecName,
1595 0);
1596 codec_port.SetParam(cricket::kCodecParamPort, sctp_port);
1597 LOG(INFO) << "AddSctpDataCodec: Got SCTP Port Number "
1598 << sctp_port;
1599 media_desc->AddCodec(codec_port);
1600 return true;
1601}
1602
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001603bool GetMinValue(const std::vector<int>& values, int* value) {
1604 if (values.empty()) {
1605 return false;
1606 }
1607 std::vector<int>::const_iterator found =
1608 std::min_element(values.begin(), values.end());
1609 *value = *found;
1610 return true;
1611}
1612
1613bool GetParameter(const std::string& name,
1614 const cricket::CodecParameterMap& params, int* value) {
1615 std::map<std::string, std::string>::const_iterator found =
1616 params.find(name);
1617 if (found == params.end()) {
1618 return false;
1619 }
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001620 if (!rtc::FromString(found->second, value)) {
wu@webrtc.org5e760e72014-04-02 23:19:09 +00001621 return false;
1622 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001623 return true;
1624}
1625
1626void BuildRtpMap(const MediaContentDescription* media_desc,
1627 const MediaType media_type,
1628 std::string* message) {
1629 ASSERT(message != NULL);
1630 ASSERT(media_desc != NULL);
1631 std::ostringstream os;
1632 if (media_type == cricket::MEDIA_TYPE_VIDEO) {
1633 const VideoContentDescription* video_desc =
1634 static_cast<const VideoContentDescription*>(media_desc);
1635 for (std::vector<cricket::VideoCodec>::const_iterator it =
1636 video_desc->codecs().begin();
1637 it != video_desc->codecs().end(); ++it) {
1638 // RFC 4566
1639 // a=rtpmap:<payload type> <encoding name>/<clock rate>
1640 // [/<encodingparameters>]
1641 if (it->id != kWildcardPayloadType) {
1642 InitAttrLine(kAttributeRtpmap, &os);
1643 os << kSdpDelimiterColon << it->id << " " << it->name
1644 << "/" << kDefaultVideoClockrate;
1645 AddLine(os.str(), message);
1646 }
1647 AddRtcpFbLines(*it, message);
1648 AddFmtpLine(*it, message);
1649 }
1650 } else if (media_type == cricket::MEDIA_TYPE_AUDIO) {
1651 const AudioContentDescription* audio_desc =
1652 static_cast<const AudioContentDescription*>(media_desc);
1653 std::vector<int> ptimes;
1654 std::vector<int> maxptimes;
1655 int max_minptime = 0;
1656 for (std::vector<cricket::AudioCodec>::const_iterator it =
1657 audio_desc->codecs().begin();
1658 it != audio_desc->codecs().end(); ++it) {
1659 ASSERT(!it->name.empty());
1660 // RFC 4566
1661 // a=rtpmap:<payload type> <encoding name>/<clock rate>
1662 // [/<encodingparameters>]
1663 InitAttrLine(kAttributeRtpmap, &os);
1664 os << kSdpDelimiterColon << it->id << " ";
1665 os << it->name << "/" << it->clockrate;
1666 if (it->channels != 1) {
1667 os << "/" << it->channels;
1668 }
1669 AddLine(os.str(), message);
1670 AddRtcpFbLines(*it, message);
1671 AddFmtpLine(*it, message);
1672 int minptime = 0;
1673 if (GetParameter(kCodecParamMinPTime, it->params, &minptime)) {
1674 max_minptime = std::max(minptime, max_minptime);
1675 }
1676 int ptime;
1677 if (GetParameter(kCodecParamPTime, it->params, &ptime)) {
1678 ptimes.push_back(ptime);
1679 }
1680 int maxptime;
1681 if (GetParameter(kCodecParamMaxPTime, it->params, &maxptime)) {
1682 maxptimes.push_back(maxptime);
1683 }
1684 }
1685 // Populate the maxptime attribute with the smallest maxptime of all codecs
1686 // under the same m-line.
1687 int min_maxptime = INT_MAX;
1688 if (GetMinValue(maxptimes, &min_maxptime)) {
1689 AddAttributeLine(kCodecParamMaxPTime, min_maxptime, message);
1690 }
1691 ASSERT(min_maxptime > max_minptime);
1692 // Populate the ptime attribute with the smallest ptime or the largest
1693 // minptime, whichever is the largest, for all codecs under the same m-line.
1694 int ptime = INT_MAX;
1695 if (GetMinValue(ptimes, &ptime)) {
1696 ptime = std::min(ptime, min_maxptime);
1697 ptime = std::max(ptime, max_minptime);
1698 AddAttributeLine(kCodecParamPTime, ptime, message);
1699 }
1700 } else if (media_type == cricket::MEDIA_TYPE_DATA) {
1701 const DataContentDescription* data_desc =
1702 static_cast<const DataContentDescription*>(media_desc);
1703 for (std::vector<cricket::DataCodec>::const_iterator it =
1704 data_desc->codecs().begin();
1705 it != data_desc->codecs().end(); ++it) {
1706 // RFC 4566
1707 // a=rtpmap:<payload type> <encoding name>/<clock rate>
1708 // [/<encodingparameters>]
1709 InitAttrLine(kAttributeRtpmap, &os);
1710 os << kSdpDelimiterColon << it->id << " "
1711 << it->name << "/" << it->clockrate;
1712 AddLine(os.str(), message);
1713 }
1714 }
1715}
1716
1717void BuildCandidate(const std::vector<Candidate>& candidates,
1718 std::string* message) {
1719 std::ostringstream os;
1720
1721 for (std::vector<Candidate>::const_iterator it = candidates.begin();
1722 it != candidates.end(); ++it) {
1723 // RFC 5245
1724 // a=candidate:<foundation> <component-id> <transport> <priority>
1725 // <connection-address> <port> typ <candidate-types>
1726 // [raddr <connection-address>] [rport <port>]
1727 // *(SP extension-att-name SP extension-att-value)
1728 std::string type;
1729 // Map the cricket candidate type to "host" / "srflx" / "prflx" / "relay"
1730 if (it->type() == cricket::LOCAL_PORT_TYPE) {
1731 type = kCandidateHost;
1732 } else if (it->type() == cricket::STUN_PORT_TYPE) {
1733 type = kCandidateSrflx;
1734 } else if (it->type() == cricket::RELAY_PORT_TYPE) {
1735 type = kCandidateRelay;
1736 } else {
1737 ASSERT(false);
Peter Thatcher019087f2015-04-28 09:06:26 -07001738 // Never write out candidates if we don't know the type.
1739 continue;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001740 }
1741
1742 InitAttrLine(kAttributeCandidate, &os);
1743 os << kSdpDelimiterColon
mallinath@webrtc.org2d60c5e2014-08-08 22:29:20 +00001744 << it->foundation() << " "
1745 << it->component() << " "
1746 << it->protocol() << " "
1747 << it->priority() << " "
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001748 << it->address().ipaddr().ToString() << " "
1749 << it->address().PortAsString() << " "
mallinath@webrtc.org2d60c5e2014-08-08 22:29:20 +00001750 << kAttributeCandidateTyp << " "
1751 << type << " ";
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001752
1753 // Related address
1754 if (!it->related_address().IsNil()) {
1755 os << kAttributeCandidateRaddr << " "
1756 << it->related_address().ipaddr().ToString() << " "
1757 << kAttributeCandidateRport << " "
1758 << it->related_address().PortAsString() << " ";
1759 }
1760
mallinath@webrtc.org2d60c5e2014-08-08 22:29:20 +00001761 if (it->protocol() == cricket::TCP_PROTOCOL_NAME) {
mallinath@webrtc.orge999bd02014-08-13 06:05:55 +00001762 os << kTcpCandidateType << " " << it->tcptype() << " ";
mallinath@webrtc.org2d60c5e2014-08-08 22:29:20 +00001763 }
1764
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001765 // Extensions
1766 os << kAttributeCandidateGeneration << " " << it->generation();
1767
1768 AddLine(os.str(), message);
1769 }
1770}
1771
1772void BuildIceOptions(const std::vector<std::string>& transport_options,
1773 std::string* message) {
1774 if (!transport_options.empty()) {
1775 std::ostringstream os;
1776 InitAttrLine(kAttributeIceOption, &os);
1777 os << kSdpDelimiterColon << transport_options[0];
1778 for (size_t i = 1; i < transport_options.size(); ++i) {
1779 os << kSdpDelimiterSpace << transport_options[i];
1780 }
1781 AddLine(os.str(), message);
1782 }
1783}
1784
pthatcher@webrtc.org3341b402015-02-13 21:14:22 +00001785bool IsRtp(const std::string& protocol) {
1786 return protocol.empty() ||
1787 (protocol.find(cricket::kMediaProtocolRtpPrefix) != std::string::npos);
1788}
1789
1790bool IsDtlsSctp(const std::string& protocol) {
1791 // This intentionally excludes "SCTP" and "SCTP/DTLS".
lally@webrtc.orga7470932015-02-24 20:19:21 +00001792 return protocol.find(cricket::kMediaProtocolDtlsSctp) != std::string::npos;
pthatcher@webrtc.org3341b402015-02-13 21:14:22 +00001793}
1794
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001795bool ParseSessionDescription(const std::string& message, size_t* pos,
1796 std::string* session_id,
1797 std::string* session_version,
1798 bool* supports_msid,
1799 TransportDescription* session_td,
1800 RtpHeaderExtensions* session_extmaps,
1801 cricket::SessionDescription* desc,
1802 SdpParseError* error) {
1803 std::string line;
1804
1805 // RFC 4566
1806 // v= (protocol version)
1807 if (!GetLineWithType(message, pos, &line, kLineTypeVersion)) {
1808 return ParseFailedExpectLine(message, *pos, kLineTypeVersion,
1809 std::string(), error);
1810 }
1811 // RFC 4566
1812 // o=<username> <sess-id> <sess-version> <nettype> <addrtype>
1813 // <unicast-address>
1814 if (!GetLineWithType(message, pos, &line, kLineTypeOrigin)) {
1815 return ParseFailedExpectLine(message, *pos, kLineTypeOrigin,
1816 std::string(), error);
1817 }
1818 std::vector<std::string> fields;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001819 rtc::split(line.substr(kLinePrefixLength),
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001820 kSdpDelimiterSpace, &fields);
1821 const size_t expected_fields = 6;
1822 if (fields.size() != expected_fields) {
1823 return ParseFailedExpectFieldNum(line, expected_fields, error);
1824 }
1825 *session_id = fields[1];
1826 *session_version = fields[2];
1827
1828 // RFC 4566
1829 // s= (session name)
1830 if (!GetLineWithType(message, pos, &line, kLineTypeSessionName)) {
1831 return ParseFailedExpectLine(message, *pos, kLineTypeSessionName,
1832 std::string(), error);
1833 }
1834
1835 // Optional lines
1836 // Those are the optional lines, so shouldn't return false if not present.
1837 // RFC 4566
1838 // i=* (session information)
1839 GetLineWithType(message, pos, &line, kLineTypeSessionInfo);
1840
1841 // RFC 4566
1842 // u=* (URI of description)
1843 GetLineWithType(message, pos, &line, kLineTypeSessionUri);
1844
1845 // RFC 4566
1846 // e=* (email address)
1847 GetLineWithType(message, pos, &line, kLineTypeSessionEmail);
1848
1849 // RFC 4566
1850 // p=* (phone number)
1851 GetLineWithType(message, pos, &line, kLineTypeSessionPhone);
1852
1853 // RFC 4566
1854 // c=* (connection information -- not required if included in
1855 // all media)
1856 GetLineWithType(message, pos, &line, kLineTypeConnection);
1857
1858 // RFC 4566
1859 // b=* (zero or more bandwidth information lines)
1860 while (GetLineWithType(message, pos, &line, kLineTypeSessionBandwidth)) {
1861 // By pass zero or more b lines.
1862 }
1863
1864 // RFC 4566
1865 // One or more time descriptions ("t=" and "r=" lines; see below)
1866 // t= (time the session is active)
1867 // r=* (zero or more repeat times)
1868 // Ensure there's at least one time description
1869 if (!GetLineWithType(message, pos, &line, kLineTypeTiming)) {
1870 return ParseFailedExpectLine(message, *pos, kLineTypeTiming, std::string(),
1871 error);
1872 }
1873
1874 while (GetLineWithType(message, pos, &line, kLineTypeRepeatTimes)) {
1875 // By pass zero or more r lines.
1876 }
1877
1878 // Go through the rest of the time descriptions
1879 while (GetLineWithType(message, pos, &line, kLineTypeTiming)) {
1880 while (GetLineWithType(message, pos, &line, kLineTypeRepeatTimes)) {
1881 // By pass zero or more r lines.
1882 }
1883 }
1884
1885 // RFC 4566
1886 // z=* (time zone adjustments)
1887 GetLineWithType(message, pos, &line, kLineTypeTimeZone);
1888
1889 // RFC 4566
1890 // k=* (encryption key)
1891 GetLineWithType(message, pos, &line, kLineTypeEncryptionKey);
1892
1893 // RFC 4566
1894 // a=* (zero or more session attribute lines)
1895 while (GetLineWithType(message, pos, &line, kLineTypeAttributes)) {
1896 if (HasAttribute(line, kAttributeGroup)) {
1897 if (!ParseGroupAttribute(line, desc, error)) {
1898 return false;
1899 }
1900 } else if (HasAttribute(line, kAttributeIceUfrag)) {
1901 if (!GetValue(line, kAttributeIceUfrag,
1902 &(session_td->ice_ufrag), error)) {
1903 return false;
1904 }
1905 } else if (HasAttribute(line, kAttributeIcePwd)) {
1906 if (!GetValue(line, kAttributeIcePwd, &(session_td->ice_pwd), error)) {
1907 return false;
1908 }
1909 } else if (HasAttribute(line, kAttributeIceLite)) {
1910 session_td->ice_mode = cricket::ICEMODE_LITE;
1911 } else if (HasAttribute(line, kAttributeIceOption)) {
1912 if (!ParseIceOptions(line, &(session_td->transport_options), error)) {
1913 return false;
1914 }
1915 } else if (HasAttribute(line, kAttributeFingerprint)) {
1916 if (session_td->identity_fingerprint.get()) {
1917 return ParseFailed(
1918 line,
1919 "Can't have multiple fingerprint attributes at the same level.",
1920 error);
1921 }
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001922 rtc::SSLFingerprint* fingerprint = NULL;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001923 if (!ParseFingerprintAttribute(line, &fingerprint, error)) {
1924 return false;
1925 }
1926 session_td->identity_fingerprint.reset(fingerprint);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001927 } else if (HasAttribute(line, kAttributeSetup)) {
1928 if (!ParseDtlsSetup(line, &(session_td->connection_role), error)) {
1929 return false;
1930 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001931 } else if (HasAttribute(line, kAttributeMsidSemantics)) {
1932 std::string semantics;
1933 if (!GetValue(line, kAttributeMsidSemantics, &semantics, error)) {
1934 return false;
1935 }
1936 *supports_msid = CaseInsensitiveFind(semantics, kMediaStreamSemantic);
1937 } else if (HasAttribute(line, kAttributeExtmap)) {
1938 RtpHeaderExtension extmap;
1939 if (!ParseExtmap(line, &extmap, error)) {
1940 return false;
1941 }
1942 session_extmaps->push_back(extmap);
1943 }
1944 }
1945
1946 return true;
1947}
1948
1949bool ParseGroupAttribute(const std::string& line,
1950 cricket::SessionDescription* desc,
1951 SdpParseError* error) {
1952 ASSERT(desc != NULL);
1953
1954 // RFC 5888 and draft-holmberg-mmusic-sdp-bundle-negotiation-00
1955 // a=group:BUNDLE video voice
1956 std::vector<std::string> fields;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001957 rtc::split(line.substr(kLinePrefixLength),
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001958 kSdpDelimiterSpace, &fields);
1959 std::string semantics;
1960 if (!GetValue(fields[0], kAttributeGroup, &semantics, error)) {
1961 return false;
1962 }
1963 cricket::ContentGroup group(semantics);
1964 for (size_t i = 1; i < fields.size(); ++i) {
1965 group.AddContentName(fields[i]);
1966 }
1967 desc->AddGroup(group);
1968 return true;
1969}
1970
1971static bool ParseFingerprintAttribute(const std::string& line,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001972 rtc::SSLFingerprint** fingerprint,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001973 SdpParseError* error) {
1974 if (!IsLineType(line, kLineTypeAttributes) ||
1975 !HasAttribute(line, kAttributeFingerprint)) {
1976 return ParseFailedExpectLine(line, 0, kLineTypeAttributes,
1977 kAttributeFingerprint, error);
1978 }
1979
1980 std::vector<std::string> fields;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001981 rtc::split(line.substr(kLinePrefixLength),
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001982 kSdpDelimiterSpace, &fields);
1983 const size_t expected_fields = 2;
1984 if (fields.size() != expected_fields) {
1985 return ParseFailedExpectFieldNum(line, expected_fields, error);
1986 }
1987
1988 // The first field here is "fingerprint:<hash>.
1989 std::string algorithm;
1990 if (!GetValue(fields[0], kAttributeFingerprint, &algorithm, error)) {
1991 return false;
1992 }
1993
1994 // Downcase the algorithm. Note that we don't need to downcase the
1995 // fingerprint because hex_decode can handle upper-case.
1996 std::transform(algorithm.begin(), algorithm.end(), algorithm.begin(),
1997 ::tolower);
1998
1999 // The second field is the digest value. De-hexify it.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002000 *fingerprint = rtc::SSLFingerprint::CreateFromRfc4572(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002001 algorithm, fields[1]);
2002 if (!*fingerprint) {
2003 return ParseFailed(line,
2004 "Failed to create fingerprint from the digest.",
2005 error);
2006 }
2007
2008 return true;
2009}
2010
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00002011static bool ParseDtlsSetup(const std::string& line,
2012 cricket::ConnectionRole* role,
2013 SdpParseError* error) {
2014 // setup-attr = "a=setup:" role
2015 // role = "active" / "passive" / "actpass" / "holdconn"
2016 std::vector<std::string> fields;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002017 rtc::split(line.substr(kLinePrefixLength), kSdpDelimiterColon, &fields);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00002018 const size_t expected_fields = 2;
2019 if (fields.size() != expected_fields) {
2020 return ParseFailedExpectFieldNum(line, expected_fields, error);
2021 }
2022 std::string role_str = fields[1];
2023 if (!cricket::StringToConnectionRole(role_str, role)) {
2024 return ParseFailed(line, "Invalid attribute value.", error);
2025 }
2026 return true;
2027}
2028
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002029// RFC 3551
2030// PT encoding media type clock rate channels
2031// name (Hz)
2032// 0 PCMU A 8,000 1
2033// 1 reserved A
2034// 2 reserved A
2035// 3 GSM A 8,000 1
2036// 4 G723 A 8,000 1
2037// 5 DVI4 A 8,000 1
2038// 6 DVI4 A 16,000 1
2039// 7 LPC A 8,000 1
2040// 8 PCMA A 8,000 1
2041// 9 G722 A 8,000 1
2042// 10 L16 A 44,100 2
2043// 11 L16 A 44,100 1
2044// 12 QCELP A 8,000 1
2045// 13 CN A 8,000 1
2046// 14 MPA A 90,000 (see text)
2047// 15 G728 A 8,000 1
2048// 16 DVI4 A 11,025 1
2049// 17 DVI4 A 22,050 1
2050// 18 G729 A 8,000 1
2051struct StaticPayloadAudioCodec {
2052 const char* name;
2053 int clockrate;
2054 int channels;
2055};
2056static const StaticPayloadAudioCodec kStaticPayloadAudioCodecs[] = {
2057 { "PCMU", 8000, 1 },
2058 { "reserved", 0, 0 },
2059 { "reserved", 0, 0 },
2060 { "GSM", 8000, 1 },
2061 { "G723", 8000, 1 },
2062 { "DVI4", 8000, 1 },
2063 { "DVI4", 16000, 1 },
2064 { "LPC", 8000, 1 },
2065 { "PCMA", 8000, 1 },
2066 { "G722", 8000, 1 },
2067 { "L16", 44100, 2 },
2068 { "L16", 44100, 1 },
2069 { "QCELP", 8000, 1 },
2070 { "CN", 8000, 1 },
2071 { "MPA", 90000, 1 },
2072 { "G728", 8000, 1 },
2073 { "DVI4", 11025, 1 },
2074 { "DVI4", 22050, 1 },
2075 { "G729", 8000, 1 },
2076};
2077
2078void MaybeCreateStaticPayloadAudioCodecs(
2079 const std::vector<int>& fmts, AudioContentDescription* media_desc) {
2080 if (!media_desc) {
2081 return;
2082 }
henrike@webrtc.org28654cb2013-07-22 21:07:49 +00002083 int preference = static_cast<int>(fmts.size());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002084 std::vector<int>::const_iterator it = fmts.begin();
2085 bool add_new_codec = false;
2086 for (; it != fmts.end(); ++it) {
2087 int payload_type = *it;
2088 if (!media_desc->HasCodec(payload_type) &&
2089 payload_type >= 0 &&
2090 payload_type < ARRAY_SIZE(kStaticPayloadAudioCodecs)) {
2091 std::string encoding_name = kStaticPayloadAudioCodecs[payload_type].name;
2092 int clock_rate = kStaticPayloadAudioCodecs[payload_type].clockrate;
2093 int channels = kStaticPayloadAudioCodecs[payload_type].channels;
2094 media_desc->AddCodec(cricket::AudioCodec(payload_type, encoding_name,
2095 clock_rate, 0, channels,
2096 preference));
2097 add_new_codec = true;
2098 }
2099 --preference;
2100 }
2101 if (add_new_codec) {
2102 media_desc->SortCodecs();
2103 }
2104}
2105
2106template <class C>
2107static C* ParseContentDescription(const std::string& message,
2108 const MediaType media_type,
2109 int mline_index,
2110 const std::string& protocol,
2111 const std::vector<int>& codec_preference,
2112 size_t* pos,
2113 std::string* content_name,
2114 TransportDescription* transport,
2115 std::vector<JsepIceCandidate*>* candidates,
2116 webrtc::SdpParseError* error) {
2117 C* media_desc = new C();
2118 switch (media_type) {
2119 case cricket::MEDIA_TYPE_AUDIO:
2120 *content_name = cricket::CN_AUDIO;
2121 break;
2122 case cricket::MEDIA_TYPE_VIDEO:
2123 *content_name = cricket::CN_VIDEO;
2124 break;
2125 case cricket::MEDIA_TYPE_DATA:
2126 *content_name = cricket::CN_DATA;
2127 break;
2128 default:
2129 ASSERT(false);
2130 break;
2131 }
2132 if (!ParseContent(message, media_type, mline_index, protocol,
2133 codec_preference, pos, content_name,
2134 media_desc, transport, candidates, error)) {
2135 delete media_desc;
2136 return NULL;
2137 }
2138 // Sort the codecs according to the m-line fmt list.
2139 media_desc->SortCodecs();
2140 return media_desc;
2141}
2142
2143bool ParseMediaDescription(const std::string& message,
2144 const TransportDescription& session_td,
2145 const RtpHeaderExtensions& session_extmaps,
2146 bool supports_msid,
2147 size_t* pos,
2148 cricket::SessionDescription* desc,
2149 std::vector<JsepIceCandidate*>* candidates,
2150 SdpParseError* error) {
2151 ASSERT(desc != NULL);
2152 std::string line;
2153 int mline_index = -1;
2154
2155 // Zero or more media descriptions
2156 // RFC 4566
2157 // m=<media> <port> <proto> <fmt>
2158 while (GetLineWithType(message, pos, &line, kLineTypeMedia)) {
2159 ++mline_index;
2160
2161 std::vector<std::string> fields;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002162 rtc::split(line.substr(kLinePrefixLength),
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002163 kSdpDelimiterSpace, &fields);
2164 const size_t expected_min_fields = 4;
2165 if (fields.size() < expected_min_fields) {
2166 return ParseFailedExpectMinFieldNum(line, expected_min_fields, error);
2167 }
2168 bool rejected = false;
2169 // RFC 3264
2170 // To reject an offered stream, the port number in the corresponding stream
2171 // in the answer MUST be set to zero.
2172 if (fields[1] == kMediaPortRejected) {
2173 rejected = true;
2174 }
2175
2176 std::string protocol = fields[2];
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002177
2178 // <fmt>
2179 std::vector<int> codec_preference;
pthatcher@webrtc.org3341b402015-02-13 21:14:22 +00002180 if (IsRtp(protocol)) {
jiayl@webrtc.orgddb85ab2014-09-05 16:31:56 +00002181 for (size_t j = 3 ; j < fields.size(); ++j) {
2182 // TODO(wu): Remove when below bug is fixed.
2183 // https://bugzilla.mozilla.org/show_bug.cgi?id=996329
pbosbb36fdf2015-07-09 07:48:14 -07002184 if (fields[j].empty() && j == fields.size() - 1) {
jiayl@webrtc.orgddb85ab2014-09-05 16:31:56 +00002185 continue;
2186 }
wu@webrtc.org36eda7c2014-04-15 20:37:30 +00002187
jiayl@webrtc.orgddb85ab2014-09-05 16:31:56 +00002188 int pl = 0;
pkasting@chromium.orge9facf82015-02-17 20:36:28 +00002189 if (!GetPayloadTypeFromString(line, fields[j], &pl, error)) {
jiayl@webrtc.orgddb85ab2014-09-05 16:31:56 +00002190 return false;
2191 }
2192 codec_preference.push_back(pl);
wu@webrtc.org5e760e72014-04-02 23:19:09 +00002193 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002194 }
2195
2196 // Make a temporary TransportDescription based on |session_td|.
2197 // Some of this gets overwritten by ParseContent.
minyuel5bdafd42015-08-21 15:52:48 +02002198 TransportDescription transport(NS_JINGLE_ICE_UDP,
2199 session_td.transport_options,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002200 session_td.ice_ufrag,
2201 session_td.ice_pwd,
2202 session_td.ice_mode,
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00002203 session_td.connection_role,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002204 session_td.identity_fingerprint.get(),
2205 Candidates());
2206
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002207 rtc::scoped_ptr<MediaContentDescription> content;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002208 std::string content_name;
2209 if (HasAttribute(line, kMediaTypeVideo)) {
2210 content.reset(ParseContentDescription<VideoContentDescription>(
2211 message, cricket::MEDIA_TYPE_VIDEO, mline_index, protocol,
2212 codec_preference, pos, &content_name,
2213 &transport, candidates, error));
2214 } else if (HasAttribute(line, kMediaTypeAudio)) {
2215 content.reset(ParseContentDescription<AudioContentDescription>(
2216 message, cricket::MEDIA_TYPE_AUDIO, mline_index, protocol,
2217 codec_preference, pos, &content_name,
2218 &transport, candidates, error));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002219 } else if (HasAttribute(line, kMediaTypeData)) {
jiayl@webrtc.org0e527722014-09-08 21:43:43 +00002220 DataContentDescription* data_desc =
wu@webrtc.org78187522013-10-07 23:32:02 +00002221 ParseContentDescription<DataContentDescription>(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002222 message, cricket::MEDIA_TYPE_DATA, mline_index, protocol,
2223 codec_preference, pos, &content_name,
wu@webrtc.org78187522013-10-07 23:32:02 +00002224 &transport, candidates, error);
jiayl@webrtc.org0e527722014-09-08 21:43:43 +00002225 content.reset(data_desc);
wu@webrtc.org78187522013-10-07 23:32:02 +00002226
jiayl@webrtc.orgddb85ab2014-09-05 16:31:56 +00002227 int p;
pthatcher@webrtc.org3341b402015-02-13 21:14:22 +00002228 if (data_desc && IsDtlsSctp(protocol) && rtc::FromString(fields[3], &p)) {
jiayl@webrtc.org0e527722014-09-08 21:43:43 +00002229 if (!AddSctpDataCodec(data_desc, p))
jiayl@webrtc.orgddb85ab2014-09-05 16:31:56 +00002230 return false;
wu@webrtc.org78187522013-10-07 23:32:02 +00002231 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002232 } else {
2233 LOG(LS_WARNING) << "Unsupported media type: " << line;
2234 continue;
2235 }
2236 if (!content.get()) {
2237 // ParseContentDescription returns NULL if failed.
2238 return false;
2239 }
2240
pthatcher@webrtc.org3341b402015-02-13 21:14:22 +00002241 if (IsRtp(protocol)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002242 // Make sure to set the media direction correctly. If the direction is not
2243 // MD_RECVONLY or Inactive and no streams are parsed,
2244 // a default MediaStream will be created to prepare for receiving media.
2245 if (supports_msid && content->streams().empty() &&
2246 content->direction() == cricket::MD_SENDRECV) {
2247 content->set_direction(cricket::MD_RECVONLY);
2248 }
2249
2250 // Set the extmap.
2251 if (!session_extmaps.empty() &&
2252 !content->rtp_header_extensions().empty()) {
2253 return ParseFailed("",
2254 "The a=extmap MUST be either all session level or "
2255 "all media level.",
2256 error);
2257 }
2258 for (size_t i = 0; i < session_extmaps.size(); ++i) {
2259 content->AddRtpHeaderExtension(session_extmaps[i]);
2260 }
2261 }
2262 content->set_protocol(protocol);
2263 desc->AddContent(content_name,
pthatcher@webrtc.org3341b402015-02-13 21:14:22 +00002264 IsDtlsSctp(protocol) ? cricket::NS_JINGLE_DRAFT_SCTP :
2265 cricket::NS_JINGLE_RTP,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002266 rejected,
2267 content.release());
2268 // Create TransportInfo with the media level "ice-pwd" and "ice-ufrag".
2269 TransportInfo transport_info(content_name, transport);
2270
2271 if (!desc->AddTransportInfo(transport_info)) {
2272 std::ostringstream description;
2273 description << "Failed to AddTransportInfo with content name: "
2274 << content_name;
2275 return ParseFailed("", description.str(), error);
2276 }
2277 }
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00002278
2279 size_t end_of_message = message.size();
2280 if (mline_index == -1 && *pos != end_of_message) {
2281 ParseFailed(message, *pos, "Expects m line.", error);
2282 return false;
2283 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002284 return true;
2285}
2286
2287bool VerifyCodec(const cricket::Codec& codec) {
2288 // Codec has not been populated correctly unless the name has been set. This
2289 // can happen if an SDP has an fmtp or rtcp-fb with a payload type but doesn't
2290 // have a corresponding "rtpmap" line.
2291 cricket::Codec default_codec;
2292 return default_codec.name != codec.name;
2293}
2294
2295bool VerifyAudioCodecs(const AudioContentDescription* audio_desc) {
2296 const std::vector<cricket::AudioCodec>& codecs = audio_desc->codecs();
2297 for (std::vector<cricket::AudioCodec>::const_iterator iter = codecs.begin();
2298 iter != codecs.end(); ++iter) {
2299 if (!VerifyCodec(*iter)) {
2300 return false;
2301 }
2302 }
2303 return true;
2304}
2305
2306bool VerifyVideoCodecs(const VideoContentDescription* video_desc) {
2307 const std::vector<cricket::VideoCodec>& codecs = video_desc->codecs();
2308 for (std::vector<cricket::VideoCodec>::const_iterator iter = codecs.begin();
2309 iter != codecs.end(); ++iter) {
2310 if (!VerifyCodec(*iter)) {
2311 return false;
2312 }
2313 }
2314 return true;
2315}
2316
2317void AddParameters(const cricket::CodecParameterMap& parameters,
2318 cricket::Codec* codec) {
2319 for (cricket::CodecParameterMap::const_iterator iter =
2320 parameters.begin(); iter != parameters.end(); ++iter) {
2321 codec->SetParam(iter->first, iter->second);
2322 }
2323}
2324
2325void AddFeedbackParameter(const cricket::FeedbackParam& feedback_param,
2326 cricket::Codec* codec) {
2327 codec->AddFeedbackParam(feedback_param);
2328}
2329
2330void AddFeedbackParameters(const cricket::FeedbackParams& feedback_params,
2331 cricket::Codec* codec) {
2332 for (std::vector<cricket::FeedbackParam>::const_iterator iter =
2333 feedback_params.params().begin();
2334 iter != feedback_params.params().end(); ++iter) {
2335 codec->AddFeedbackParam(*iter);
2336 }
2337}
2338
2339// Gets the current codec setting associated with |payload_type|. If there
changbin.shao@webrtc.org2d25b442015-03-16 04:14:34 +00002340// is no Codec associated with that payload type it returns an empty codec
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002341// with that payload type.
2342template <class T>
changbin.shao@webrtc.org2d25b442015-03-16 04:14:34 +00002343T GetCodecWithPayloadType(const std::vector<T>& codecs, int payload_type) {
2344 T ret_val;
2345 if (!FindCodecById(codecs, payload_type, &ret_val)) {
2346 ret_val.id = payload_type;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002347 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002348 return ret_val;
2349}
2350
2351// Updates or creates a new codec entry in the audio description.
2352template <class T, class U>
2353void AddOrReplaceCodec(MediaContentDescription* content_desc, const U& codec) {
2354 T* desc = static_cast<T*>(content_desc);
2355 std::vector<U> codecs = desc->codecs();
2356 bool found = false;
2357
2358 typename std::vector<U>::iterator iter;
2359 for (iter = codecs.begin(); iter != codecs.end(); ++iter) {
2360 if (iter->id == codec.id) {
2361 *iter = codec;
2362 found = true;
2363 break;
2364 }
2365 }
2366 if (!found) {
2367 desc->AddCodec(codec);
2368 return;
2369 }
2370 desc->set_codecs(codecs);
2371}
2372
2373// Adds or updates existing codec corresponding to |payload_type| according
2374// to |parameters|.
2375template <class T, class U>
2376void UpdateCodec(MediaContentDescription* content_desc, int payload_type,
2377 const cricket::CodecParameterMap& parameters) {
2378 // Codec might already have been populated (from rtpmap).
changbin.shao@webrtc.org2d25b442015-03-16 04:14:34 +00002379 U new_codec = GetCodecWithPayloadType(static_cast<T*>(content_desc)->codecs(),
2380 payload_type);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002381 AddParameters(parameters, &new_codec);
2382 AddOrReplaceCodec<T, U>(content_desc, new_codec);
2383}
2384
2385// Adds or updates existing codec corresponding to |payload_type| according
2386// to |feedback_param|.
2387template <class T, class U>
2388void UpdateCodec(MediaContentDescription* content_desc, int payload_type,
2389 const cricket::FeedbackParam& feedback_param) {
2390 // Codec might already have been populated (from rtpmap).
changbin.shao@webrtc.org2d25b442015-03-16 04:14:34 +00002391 U new_codec = GetCodecWithPayloadType(static_cast<T*>(content_desc)->codecs(),
2392 payload_type);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002393 AddFeedbackParameter(feedback_param, &new_codec);
2394 AddOrReplaceCodec<T, U>(content_desc, new_codec);
2395}
2396
jlmiller@webrtc.orga744a282015-02-18 21:37:46 +00002397template <class T>
2398bool PopWildcardCodec(std::vector<T>* codecs, T* wildcard_codec) {
2399 for (auto iter = codecs->begin(); iter != codecs->end(); ++iter) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002400 if (iter->id == kWildcardPayloadType) {
2401 *wildcard_codec = *iter;
2402 codecs->erase(iter);
2403 return true;
2404 }
2405 }
2406 return false;
2407}
2408
jlmiller@webrtc.orga744a282015-02-18 21:37:46 +00002409template<class T>
2410void UpdateFromWildcardCodecs(cricket::MediaContentDescriptionImpl<T>* desc) {
2411 auto codecs = desc->codecs();
2412 T wildcard_codec;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002413 if (!PopWildcardCodec(&codecs, &wildcard_codec)) {
2414 return;
2415 }
jlmiller@webrtc.orga744a282015-02-18 21:37:46 +00002416 for (auto& codec : codecs) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002417 AddFeedbackParameters(wildcard_codec.feedback_params, &codec);
2418 }
jlmiller@webrtc.orga744a282015-02-18 21:37:46 +00002419 desc->set_codecs(codecs);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002420}
2421
2422void AddAudioAttribute(const std::string& name, const std::string& value,
2423 AudioContentDescription* audio_desc) {
2424 if (value.empty()) {
2425 return;
2426 }
2427 std::vector<cricket::AudioCodec> codecs = audio_desc->codecs();
2428 for (std::vector<cricket::AudioCodec>::iterator iter = codecs.begin();
2429 iter != codecs.end(); ++iter) {
2430 iter->params[name] = value;
2431 }
2432 audio_desc->set_codecs(codecs);
2433}
2434
2435bool ParseContent(const std::string& message,
2436 const MediaType media_type,
2437 int mline_index,
2438 const std::string& protocol,
2439 const std::vector<int>& codec_preference,
2440 size_t* pos,
2441 std::string* content_name,
2442 MediaContentDescription* media_desc,
2443 TransportDescription* transport,
2444 std::vector<JsepIceCandidate*>* candidates,
2445 SdpParseError* error) {
2446 ASSERT(media_desc != NULL);
2447 ASSERT(content_name != NULL);
2448 ASSERT(transport != NULL);
2449
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +00002450 if (media_type == cricket::MEDIA_TYPE_AUDIO) {
2451 MaybeCreateStaticPayloadAudioCodecs(
2452 codec_preference, static_cast<AudioContentDescription*>(media_desc));
2453 }
2454
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002455 // The media level "ice-ufrag" and "ice-pwd".
2456 // The candidates before update the media level "ice-pwd" and "ice-ufrag".
2457 Candidates candidates_orig;
2458 std::string line;
2459 std::string mline_id;
2460 // Tracks created out of the ssrc attributes.
2461 StreamParamsVec tracks;
2462 SsrcInfoVec ssrc_infos;
2463 SsrcGroupVec ssrc_groups;
2464 std::string maxptime_as_string;
2465 std::string ptime_as_string;
2466
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002467 // Loop until the next m line
2468 while (!IsLineType(message, kLineTypeMedia, *pos)) {
2469 if (!GetLine(message, pos, &line)) {
2470 if (*pos >= message.size()) {
2471 break; // Done parsing
2472 } else {
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +00002473 return ParseFailed(message, *pos, "Invalid SDP line.", error);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002474 }
2475 }
2476
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002477 // RFC 4566
2478 // b=* (zero or more bandwidth information lines)
2479 if (IsLineType(line, kLineTypeSessionBandwidth)) {
2480 std::string bandwidth;
2481 if (HasAttribute(line, kApplicationSpecificMaximum)) {
2482 if (!GetValue(line, kApplicationSpecificMaximum, &bandwidth, error)) {
2483 return false;
2484 } else {
wu@webrtc.org5e760e72014-04-02 23:19:09 +00002485 int b = 0;
2486 if (!GetValueFromString(line, bandwidth, &b, error)) {
2487 return false;
2488 }
Peter Thatcherc0c3a862015-06-24 15:31:25 -07002489 // We should never use more than the default bandwidth for RTP-based
2490 // data channels. Don't allow SDP to set the bandwidth, because
2491 // that would give JS the opportunity to "break the Internet".
2492 // See: https://code.google.com/p/chromium/issues/detail?id=280726
2493 if (media_type == cricket::MEDIA_TYPE_DATA && IsRtp(protocol) &&
2494 b > cricket::kDataMaxBandwidth / 1000) {
2495 std::ostringstream description;
2496 description << "RTP-based data channels may not send more than "
2497 << cricket::kDataMaxBandwidth / 1000 << "kbps.";
2498 return ParseFailed(line, description.str(), error);
2499 }
wu@webrtc.org5e760e72014-04-02 23:19:09 +00002500 media_desc->set_bandwidth(b * 1000);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002501 }
2502 }
2503 continue;
2504 }
2505
2506 if (!IsLineType(line, kLineTypeAttributes)) {
2507 // TODO: Handle other lines if needed.
2508 LOG(LS_INFO) << "Ignored line: " << line;
2509 continue;
2510 }
2511
2512 // Handle attributes common to SCTP and RTP.
2513 if (HasAttribute(line, kAttributeMid)) {
2514 // RFC 3388
2515 // mid-attribute = "a=mid:" identification-tag
2516 // identification-tag = token
2517 // Use the mid identification-tag as the content name.
2518 if (!GetValue(line, kAttributeMid, &mline_id, error)) {
2519 return false;
2520 }
2521 *content_name = mline_id;
2522 } else if (HasAttribute(line, kAttributeCandidate)) {
2523 Candidate candidate;
2524 if (!ParseCandidate(line, &candidate, error, false)) {
2525 return false;
2526 }
2527 candidates_orig.push_back(candidate);
2528 } else if (HasAttribute(line, kAttributeIceUfrag)) {
2529 if (!GetValue(line, kAttributeIceUfrag, &transport->ice_ufrag, error)) {
2530 return false;
2531 }
2532 } else if (HasAttribute(line, kAttributeIcePwd)) {
2533 if (!GetValue(line, kAttributeIcePwd, &transport->ice_pwd, error)) {
2534 return false;
2535 }
2536 } else if (HasAttribute(line, kAttributeIceOption)) {
2537 if (!ParseIceOptions(line, &transport->transport_options, error)) {
2538 return false;
2539 }
2540 } else if (HasAttribute(line, kAttributeFmtp)) {
2541 if (!ParseFmtpAttributes(line, media_type, media_desc, error)) {
2542 return false;
2543 }
2544 } else if (HasAttribute(line, kAttributeFingerprint)) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002545 rtc::SSLFingerprint* fingerprint = NULL;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002546
2547 if (!ParseFingerprintAttribute(line, &fingerprint, error)) {
2548 return false;
2549 }
2550 transport->identity_fingerprint.reset(fingerprint);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00002551 } else if (HasAttribute(line, kAttributeSetup)) {
2552 if (!ParseDtlsSetup(line, &(transport->connection_role), error)) {
2553 return false;
2554 }
pthatcher@webrtc.org3341b402015-02-13 21:14:22 +00002555 } else if (IsDtlsSctp(protocol) && HasAttribute(line, kAttributeSctpPort)) {
jiayl@webrtc.orgddb85ab2014-09-05 16:31:56 +00002556 int sctp_port;
2557 if (!ParseSctpPort(line, &sctp_port, error)) {
2558 return false;
2559 }
2560 if (!AddSctpDataCodec(static_cast<DataContentDescription*>(media_desc),
2561 sctp_port)) {
2562 return false;
2563 }
pthatcher@webrtc.org3341b402015-02-13 21:14:22 +00002564 } else if (IsRtp(protocol)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002565 //
2566 // RTP specific attrubtes
2567 //
2568 if (HasAttribute(line, kAttributeRtcpMux)) {
2569 media_desc->set_rtcp_mux(true);
2570 } else if (HasAttribute(line, kAttributeSsrcGroup)) {
2571 if (!ParseSsrcGroupAttribute(line, &ssrc_groups, error)) {
2572 return false;
2573 }
2574 } else if (HasAttribute(line, kAttributeSsrc)) {
2575 if (!ParseSsrcAttribute(line, &ssrc_infos, error)) {
2576 return false;
2577 }
2578 } else if (HasAttribute(line, kAttributeCrypto)) {
2579 if (!ParseCryptoAttribute(line, media_desc, error)) {
2580 return false;
2581 }
2582 } else if (HasAttribute(line, kAttributeRtpmap)) {
2583 if (!ParseRtpmapAttribute(line, media_type, codec_preference,
2584 media_desc, error)) {
2585 return false;
2586 }
2587 } else if (HasAttribute(line, kCodecParamMaxPTime)) {
2588 if (!GetValue(line, kCodecParamMaxPTime, &maxptime_as_string, error)) {
2589 return false;
2590 }
2591 } else if (HasAttribute(line, kAttributeRtcpFb)) {
2592 if (!ParseRtcpFbAttribute(line, media_type, media_desc, error)) {
2593 return false;
2594 }
2595 } else if (HasAttribute(line, kCodecParamPTime)) {
2596 if (!GetValue(line, kCodecParamPTime, &ptime_as_string, error)) {
2597 return false;
2598 }
2599 } else if (HasAttribute(line, kAttributeSendOnly)) {
2600 media_desc->set_direction(cricket::MD_SENDONLY);
2601 } else if (HasAttribute(line, kAttributeRecvOnly)) {
2602 media_desc->set_direction(cricket::MD_RECVONLY);
2603 } else if (HasAttribute(line, kAttributeInactive)) {
2604 media_desc->set_direction(cricket::MD_INACTIVE);
2605 } else if (HasAttribute(line, kAttributeSendRecv)) {
2606 media_desc->set_direction(cricket::MD_SENDRECV);
2607 } else if (HasAttribute(line, kAttributeExtmap)) {
2608 RtpHeaderExtension extmap;
2609 if (!ParseExtmap(line, &extmap, error)) {
2610 return false;
2611 }
2612 media_desc->AddRtpHeaderExtension(extmap);
2613 } else if (HasAttribute(line, kAttributeXGoogleFlag)) {
2614 // Experimental attribute. Conference mode activates more aggressive
2615 // AEC and NS settings.
2616 // TODO: expose API to set these directly.
2617 std::string flag_value;
2618 if (!GetValue(line, kAttributeXGoogleFlag, &flag_value, error)) {
2619 return false;
2620 }
2621 if (flag_value.compare(kValueConference) == 0)
2622 media_desc->set_conference_mode(true);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002623 }
2624 } else {
2625 // Only parse lines that we are interested of.
2626 LOG(LS_INFO) << "Ignored line: " << line;
2627 continue;
2628 }
2629 }
2630
2631 // Create tracks from the |ssrc_infos|.
2632 CreateTracksFromSsrcInfos(ssrc_infos, &tracks);
2633
2634 // Add the ssrc group to the track.
2635 for (SsrcGroupVec::iterator ssrc_group = ssrc_groups.begin();
2636 ssrc_group != ssrc_groups.end(); ++ssrc_group) {
2637 if (ssrc_group->ssrcs.empty()) {
2638 continue;
2639 }
2640 uint32 ssrc = ssrc_group->ssrcs.front();
2641 for (StreamParamsVec::iterator track = tracks.begin();
2642 track != tracks.end(); ++track) {
2643 if (track->has_ssrc(ssrc)) {
2644 track->ssrc_groups.push_back(*ssrc_group);
2645 }
2646 }
2647 }
2648
2649 // Add the new tracks to the |media_desc|.
2650 for (StreamParamsVec::iterator track = tracks.begin();
2651 track != tracks.end(); ++track) {
2652 media_desc->AddStream(*track);
2653 }
2654
2655 if (media_type == cricket::MEDIA_TYPE_AUDIO) {
2656 AudioContentDescription* audio_desc =
2657 static_cast<AudioContentDescription*>(media_desc);
jlmiller@webrtc.orga744a282015-02-18 21:37:46 +00002658 UpdateFromWildcardCodecs(audio_desc);
2659
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002660 // Verify audio codec ensures that no audio codec has been populated with
2661 // only fmtp.
2662 if (!VerifyAudioCodecs(audio_desc)) {
2663 return ParseFailed("Failed to parse audio codecs correctly.", error);
2664 }
2665 AddAudioAttribute(kCodecParamMaxPTime, maxptime_as_string, audio_desc);
2666 AddAudioAttribute(kCodecParamPTime, ptime_as_string, audio_desc);
2667 }
2668
2669 if (media_type == cricket::MEDIA_TYPE_VIDEO) {
jlmiller@webrtc.orga744a282015-02-18 21:37:46 +00002670 VideoContentDescription* video_desc =
2671 static_cast<VideoContentDescription*>(media_desc);
2672 UpdateFromWildcardCodecs(video_desc);
2673 // Verify video codec ensures that no video codec has been populated with
2674 // only rtcp-fb.
2675 if (!VerifyVideoCodecs(video_desc)) {
2676 return ParseFailed("Failed to parse video codecs correctly.", error);
2677 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002678 }
2679
2680 // RFC 5245
2681 // Update the candidates with the media level "ice-pwd" and "ice-ufrag".
2682 for (Candidates::iterator it = candidates_orig.begin();
2683 it != candidates_orig.end(); ++it) {
2684 ASSERT((*it).username().empty());
2685 (*it).set_username(transport->ice_ufrag);
2686 ASSERT((*it).password().empty());
2687 (*it).set_password(transport->ice_pwd);
2688 candidates->push_back(
2689 new JsepIceCandidate(mline_id, mline_index, *it));
2690 }
2691 return true;
2692}
2693
2694bool ParseSsrcAttribute(const std::string& line, SsrcInfoVec* ssrc_infos,
2695 SdpParseError* error) {
2696 ASSERT(ssrc_infos != NULL);
2697 // RFC 5576
2698 // a=ssrc:<ssrc-id> <attribute>
2699 // a=ssrc:<ssrc-id> <attribute>:<value>
2700 std::string field1, field2;
Donald Curtis144d0182015-05-15 13:14:24 -07002701 if (!rtc::tokenize_first(line.substr(kLinePrefixLength), kSdpDelimiterSpace,
2702 &field1, &field2)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002703 const size_t expected_fields = 2;
2704 return ParseFailedExpectFieldNum(line, expected_fields, error);
2705 }
2706
2707 // ssrc:<ssrc-id>
2708 std::string ssrc_id_s;
2709 if (!GetValue(field1, kAttributeSsrc, &ssrc_id_s, error)) {
2710 return false;
2711 }
wu@webrtc.org5e760e72014-04-02 23:19:09 +00002712 uint32 ssrc_id = 0;
2713 if (!GetValueFromString(line, ssrc_id_s, &ssrc_id, error)) {
2714 return false;
2715 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002716
2717 std::string attribute;
2718 std::string value;
Donald Curtis144d0182015-05-15 13:14:24 -07002719 if (!rtc::tokenize_first(field2, kSdpDelimiterColon, &attribute, &value)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002720 std::ostringstream description;
2721 description << "Failed to get the ssrc attribute value from " << field2
2722 << ". Expected format <attribute>:<value>.";
2723 return ParseFailed(line, description.str(), error);
2724 }
2725
2726 // Check if there's already an item for this |ssrc_id|. Create a new one if
2727 // there isn't.
2728 SsrcInfoVec::iterator ssrc_info = ssrc_infos->begin();
2729 for (; ssrc_info != ssrc_infos->end(); ++ssrc_info) {
2730 if (ssrc_info->ssrc_id == ssrc_id) {
2731 break;
2732 }
2733 }
2734 if (ssrc_info == ssrc_infos->end()) {
2735 SsrcInfo info;
2736 info.ssrc_id = ssrc_id;
2737 ssrc_infos->push_back(info);
2738 ssrc_info = ssrc_infos->end() - 1;
2739 }
2740
2741 // Store the info to the |ssrc_info|.
2742 if (attribute == kSsrcAttributeCname) {
2743 // RFC 5576
2744 // cname:<value>
2745 ssrc_info->cname = value;
2746 } else if (attribute == kSsrcAttributeMsid) {
2747 // draft-alvestrand-mmusic-msid-00
2748 // "msid:" identifier [ " " appdata ]
2749 std::vector<std::string> fields;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002750 rtc::split(value, kSdpDelimiterSpace, &fields);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002751 if (fields.size() < 1 || fields.size() > 2) {
2752 return ParseFailed(line,
2753 "Expected format \"msid:<identifier>[ <appdata>]\".",
2754 error);
2755 }
2756 ssrc_info->msid_identifier = fields[0];
2757 if (fields.size() == 2) {
2758 ssrc_info->msid_appdata = fields[1];
2759 }
2760 } else if (attribute == kSsrcAttributeMslabel) {
2761 // draft-alvestrand-rtcweb-mid-01
2762 // mslabel:<value>
2763 ssrc_info->mslabel = value;
2764 } else if (attribute == kSSrcAttributeLabel) {
2765 // The label isn't defined.
2766 // label:<value>
2767 ssrc_info->label = value;
2768 }
2769 return true;
2770}
2771
2772bool ParseSsrcGroupAttribute(const std::string& line,
2773 SsrcGroupVec* ssrc_groups,
2774 SdpParseError* error) {
2775 ASSERT(ssrc_groups != NULL);
2776 // RFC 5576
2777 // a=ssrc-group:<semantics> <ssrc-id> ...
2778 std::vector<std::string> fields;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002779 rtc::split(line.substr(kLinePrefixLength),
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002780 kSdpDelimiterSpace, &fields);
2781 const size_t expected_min_fields = 2;
2782 if (fields.size() < expected_min_fields) {
2783 return ParseFailedExpectMinFieldNum(line, expected_min_fields, error);
2784 }
2785 std::string semantics;
2786 if (!GetValue(fields[0], kAttributeSsrcGroup, &semantics, error)) {
2787 return false;
2788 }
2789 std::vector<uint32> ssrcs;
2790 for (size_t i = 1; i < fields.size(); ++i) {
wu@webrtc.org5e760e72014-04-02 23:19:09 +00002791 uint32 ssrc = 0;
2792 if (!GetValueFromString(line, fields[i], &ssrc, error)) {
2793 return false;
2794 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002795 ssrcs.push_back(ssrc);
2796 }
2797 ssrc_groups->push_back(SsrcGroup(semantics, ssrcs));
2798 return true;
2799}
2800
2801bool ParseCryptoAttribute(const std::string& line,
2802 MediaContentDescription* media_desc,
2803 SdpParseError* error) {
2804 std::vector<std::string> fields;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002805 rtc::split(line.substr(kLinePrefixLength),
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002806 kSdpDelimiterSpace, &fields);
2807 // RFC 4568
2808 // a=crypto:<tag> <crypto-suite> <key-params> [<session-params>]
2809 const size_t expected_min_fields = 3;
2810 if (fields.size() < expected_min_fields) {
2811 return ParseFailedExpectMinFieldNum(line, expected_min_fields, error);
2812 }
2813 std::string tag_value;
2814 if (!GetValue(fields[0], kAttributeCrypto, &tag_value, error)) {
2815 return false;
2816 }
wu@webrtc.org5e760e72014-04-02 23:19:09 +00002817 int tag = 0;
2818 if (!GetValueFromString(line, tag_value, &tag, error)) {
2819 return false;
2820 }
jbauch083b73f2015-07-16 02:46:32 -07002821 const std::string& crypto_suite = fields[1];
2822 const std::string& key_params = fields[2];
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002823 std::string session_params;
2824 if (fields.size() > 3) {
2825 session_params = fields[3];
2826 }
2827 media_desc->AddCrypto(CryptoParams(tag, crypto_suite, key_params,
2828 session_params));
2829 return true;
2830}
2831
2832// Updates or creates a new codec entry in the audio description with according
2833// to |name|, |clockrate|, |bitrate|, |channels| and |preference|.
2834void UpdateCodec(int payload_type, const std::string& name, int clockrate,
2835 int bitrate, int channels, int preference,
2836 AudioContentDescription* audio_desc) {
2837 // Codec may already be populated with (only) optional parameters
2838 // (from an fmtp).
changbin.shao@webrtc.org2d25b442015-03-16 04:14:34 +00002839 cricket::AudioCodec codec =
2840 GetCodecWithPayloadType(audio_desc->codecs(), payload_type);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002841 codec.name = name;
2842 codec.clockrate = clockrate;
2843 codec.bitrate = bitrate;
2844 codec.channels = channels;
2845 codec.preference = preference;
2846 AddOrReplaceCodec<AudioContentDescription, cricket::AudioCodec>(audio_desc,
2847 codec);
2848}
2849
2850// Updates or creates a new codec entry in the video description according to
2851// |name|, |width|, |height|, |framerate| and |preference|.
2852void UpdateCodec(int payload_type, const std::string& name, int width,
2853 int height, int framerate, int preference,
2854 VideoContentDescription* video_desc) {
2855 // Codec may already be populated with (only) optional parameters
2856 // (from an fmtp).
changbin.shao@webrtc.org2d25b442015-03-16 04:14:34 +00002857 cricket::VideoCodec codec =
2858 GetCodecWithPayloadType(video_desc->codecs(), payload_type);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002859 codec.name = name;
2860 codec.width = width;
2861 codec.height = height;
2862 codec.framerate = framerate;
2863 codec.preference = preference;
2864 AddOrReplaceCodec<VideoContentDescription, cricket::VideoCodec>(video_desc,
2865 codec);
2866}
2867
2868bool ParseRtpmapAttribute(const std::string& line,
2869 const MediaType media_type,
2870 const std::vector<int>& codec_preference,
2871 MediaContentDescription* media_desc,
2872 SdpParseError* error) {
2873 std::vector<std::string> fields;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002874 rtc::split(line.substr(kLinePrefixLength),
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002875 kSdpDelimiterSpace, &fields);
2876 // RFC 4566
2877 // a=rtpmap:<payload type> <encoding name>/<clock rate>[/<encodingparameters>]
2878 const size_t expected_min_fields = 2;
2879 if (fields.size() < expected_min_fields) {
2880 return ParseFailedExpectMinFieldNum(line, expected_min_fields, error);
2881 }
2882 std::string payload_type_value;
2883 if (!GetValue(fields[0], kAttributeRtpmap, &payload_type_value, error)) {
2884 return false;
2885 }
wu@webrtc.org5e760e72014-04-02 23:19:09 +00002886 int payload_type = 0;
pkasting@chromium.orge9facf82015-02-17 20:36:28 +00002887 if (!GetPayloadTypeFromString(line, payload_type_value, &payload_type,
2888 error)) {
wu@webrtc.org5e760e72014-04-02 23:19:09 +00002889 return false;
2890 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002891
2892 // Set the preference order depending on the order of the pl type in the
2893 // <fmt> of the m-line.
2894 const int preference = codec_preference.end() -
2895 std::find(codec_preference.begin(), codec_preference.end(),
2896 payload_type);
2897 if (preference == 0) {
2898 LOG(LS_WARNING) << "Ignore rtpmap line that did not appear in the "
2899 << "<fmt> of the m-line: " << line;
2900 return true;
2901 }
jbauch083b73f2015-07-16 02:46:32 -07002902 const std::string& encoder = fields[1];
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002903 std::vector<std::string> codec_params;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002904 rtc::split(encoder, '/', &codec_params);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002905 // <encoding name>/<clock rate>[/<encodingparameters>]
2906 // 2 mandatory fields
2907 if (codec_params.size() < 2 || codec_params.size() > 3) {
2908 return ParseFailed(line,
2909 "Expected format \"<encoding name>/<clock rate>"
2910 "[/<encodingparameters>]\".",
2911 error);
2912 }
jbauch083b73f2015-07-16 02:46:32 -07002913 const std::string& encoding_name = codec_params[0];
wu@webrtc.org5e760e72014-04-02 23:19:09 +00002914 int clock_rate = 0;
2915 if (!GetValueFromString(line, codec_params[1], &clock_rate, error)) {
2916 return false;
2917 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002918 if (media_type == cricket::MEDIA_TYPE_VIDEO) {
2919 VideoContentDescription* video_desc =
2920 static_cast<VideoContentDescription*>(media_desc);
2921 // TODO: We will send resolution in SDP. For now use
2922 // JsepSessionDescription::kMaxVideoCodecWidth and kMaxVideoCodecHeight.
2923 UpdateCodec(payload_type, encoding_name,
2924 JsepSessionDescription::kMaxVideoCodecWidth,
2925 JsepSessionDescription::kMaxVideoCodecHeight,
2926 JsepSessionDescription::kDefaultVideoCodecFramerate,
2927 preference, video_desc);
2928 } else if (media_type == cricket::MEDIA_TYPE_AUDIO) {
2929 // RFC 4566
2930 // For audio streams, <encoding parameters> indicates the number
2931 // of audio channels. This parameter is OPTIONAL and may be
2932 // omitted if the number of channels is one, provided that no
2933 // additional parameters are needed.
2934 int channels = 1;
2935 if (codec_params.size() == 3) {
wu@webrtc.org5e760e72014-04-02 23:19:09 +00002936 if (!GetValueFromString(line, codec_params[2], &channels, error)) {
2937 return false;
2938 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002939 }
2940 int bitrate = 0;
2941 // The default behavior for ISAC (bitrate == 0) in webrtcvoiceengine.cc
2942 // (specifically FindWebRtcCodec) is bandwidth-adaptive variable bitrate.
2943 // The bandwidth adaptation doesn't always work well, so this code
2944 // sets a fixed target bitrate instead.
2945 if (_stricmp(encoding_name.c_str(), kIsacCodecName) == 0) {
2946 if (clock_rate <= 16000) {
2947 bitrate = kIsacWbDefaultRate;
2948 } else {
2949 bitrate = kIsacSwbDefaultRate;
2950 }
2951 }
2952 AudioContentDescription* audio_desc =
2953 static_cast<AudioContentDescription*>(media_desc);
2954 UpdateCodec(payload_type, encoding_name, clock_rate, bitrate, channels,
2955 preference, audio_desc);
2956 } else if (media_type == cricket::MEDIA_TYPE_DATA) {
2957 DataContentDescription* data_desc =
2958 static_cast<DataContentDescription*>(media_desc);
2959 data_desc->AddCodec(cricket::DataCodec(payload_type, encoding_name,
2960 preference));
2961 }
2962 return true;
2963}
2964
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002965bool ParseFmtpParam(const std::string& line, std::string* parameter,
2966 std::string* value, SdpParseError* error) {
Donald Curtis0e07f922015-05-15 09:21:23 -07002967 if (!rtc::tokenize_first(line, kSdpDelimiterEqual, parameter, value)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002968 ParseFailed(line, "Unable to parse fmtp parameter. \'=\' missing.", error);
2969 return false;
2970 }
2971 // a=fmtp:<payload_type> <param1>=<value1>; <param2>=<value2>; ...
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002972 return true;
2973}
2974
2975bool ParseFmtpAttributes(const std::string& line, const MediaType media_type,
2976 MediaContentDescription* media_desc,
2977 SdpParseError* error) {
2978 if (media_type != cricket::MEDIA_TYPE_AUDIO &&
2979 media_type != cricket::MEDIA_TYPE_VIDEO) {
2980 return true;
2981 }
Donald Curtis0e07f922015-05-15 09:21:23 -07002982
2983 std::string line_payload;
2984 std::string line_params;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002985
2986 // RFC 5576
2987 // a=fmtp:<format> <format specific parameters>
2988 // At least two fields, whereas the second one is any of the optional
2989 // parameters.
Donald Curtis144d0182015-05-15 13:14:24 -07002990 if (!rtc::tokenize_first(line.substr(kLinePrefixLength), kSdpDelimiterSpace,
2991 &line_payload, &line_params)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002992 ParseFailedExpectMinFieldNum(line, 2, error);
2993 return false;
2994 }
2995
Donald Curtis0e07f922015-05-15 09:21:23 -07002996 // Parse out the payload information.
pkasting@chromium.orgd3245462015-02-23 21:28:22 +00002997 std::string payload_type_str;
Donald Curtis0e07f922015-05-15 09:21:23 -07002998 if (!GetValue(line_payload, kAttributeFmtp, &payload_type_str, error)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002999 return false;
3000 }
3001
Donald Curtis0e07f922015-05-15 09:21:23 -07003002 int payload_type = 0;
Donald Curtis144d0182015-05-15 13:14:24 -07003003 if (!GetPayloadTypeFromString(line_payload, payload_type_str, &payload_type,
3004 error)) {
Donald Curtis0e07f922015-05-15 09:21:23 -07003005 return false;
3006 }
3007
3008 // Parse out format specific parameters.
3009 std::vector<std::string> fields;
3010 rtc::split(line_params, kSdpDelimiterSemicolon, &fields);
3011
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003012 cricket::CodecParameterMap codec_params;
Donald Curtis144d0182015-05-15 13:14:24 -07003013 for (auto& iter : fields) {
Donald Curtis0e07f922015-05-15 09:21:23 -07003014 if (iter.find(kSdpDelimiterEqual) == std::string::npos) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003015 // Only fmtps with equals are currently supported. Other fmtp types
3016 // should be ignored. Unknown fmtps do not constitute an error.
3017 continue;
3018 }
Donald Curtis0e07f922015-05-15 09:21:23 -07003019
3020 std::string name;
3021 std::string value;
3022 if (!ParseFmtpParam(rtc::string_trim(iter), &name, &value, error)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003023 return false;
3024 }
3025 codec_params[name] = value;
3026 }
3027
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003028 if (media_type == cricket::MEDIA_TYPE_AUDIO) {
3029 UpdateCodec<AudioContentDescription, cricket::AudioCodec>(
pkasting@chromium.orgd3245462015-02-23 21:28:22 +00003030 media_desc, payload_type, codec_params);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003031 } else if (media_type == cricket::MEDIA_TYPE_VIDEO) {
3032 UpdateCodec<VideoContentDescription, cricket::VideoCodec>(
pkasting@chromium.orgd3245462015-02-23 21:28:22 +00003033 media_desc, payload_type, codec_params);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003034 }
3035 return true;
3036}
3037
3038bool ParseRtcpFbAttribute(const std::string& line, const MediaType media_type,
3039 MediaContentDescription* media_desc,
3040 SdpParseError* error) {
3041 if (media_type != cricket::MEDIA_TYPE_AUDIO &&
3042 media_type != cricket::MEDIA_TYPE_VIDEO) {
3043 return true;
3044 }
3045 std::vector<std::string> rtcp_fb_fields;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00003046 rtc::split(line.c_str(), kSdpDelimiterSpace, &rtcp_fb_fields);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003047 if (rtcp_fb_fields.size() < 2) {
3048 return ParseFailedGetValue(line, kAttributeRtcpFb, error);
3049 }
3050 std::string payload_type_string;
3051 if (!GetValue(rtcp_fb_fields[0], kAttributeRtcpFb, &payload_type_string,
3052 error)) {
3053 return false;
3054 }
wu@webrtc.org5e760e72014-04-02 23:19:09 +00003055 int payload_type = kWildcardPayloadType;
3056 if (payload_type_string != "*") {
pkasting@chromium.orge9facf82015-02-17 20:36:28 +00003057 if (!GetPayloadTypeFromString(line, payload_type_string, &payload_type,
3058 error)) {
wu@webrtc.org5e760e72014-04-02 23:19:09 +00003059 return false;
3060 }
3061 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003062 std::string id = rtcp_fb_fields[1];
3063 std::string param = "";
3064 for (std::vector<std::string>::iterator iter = rtcp_fb_fields.begin() + 2;
3065 iter != rtcp_fb_fields.end(); ++iter) {
3066 param.append(*iter);
3067 }
3068 const cricket::FeedbackParam feedback_param(id, param);
3069
3070 if (media_type == cricket::MEDIA_TYPE_AUDIO) {
pkasting@chromium.orgd3245462015-02-23 21:28:22 +00003071 UpdateCodec<AudioContentDescription, cricket::AudioCodec>(
3072 media_desc, payload_type, feedback_param);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003073 } else if (media_type == cricket::MEDIA_TYPE_VIDEO) {
pkasting@chromium.orgd3245462015-02-23 21:28:22 +00003074 UpdateCodec<VideoContentDescription, cricket::VideoCodec>(
3075 media_desc, payload_type, feedback_param);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003076 }
3077 return true;
3078}
3079
3080} // namespace webrtc