blob: 11eefe5c3d5098f88ce8a884c5535d7dd14248cf [file] [log] [blame]
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001/*
kjellanderb24317b2016-02-10 07:54:43 -08002 * Copyright 2011 The WebRTC project authors. All Rights Reserved.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003 *
kjellanderb24317b2016-02-10 07:54:43 -08004 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00009 */
10
ossu7bb87ee2017-01-23 04:56:25 -080011#include "webrtc/pc/webrtcsdp.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000012
kjellandera96e2d72016-02-04 23:52:28 -080013#include <ctype.h>
henrike@webrtc.org28e20752013-07-10 00:45:36 +000014#include <limits.h>
15#include <stdio.h>
kwibergd1fe2812016-04-27 06:47:29 -070016
henrike@webrtc.org28e20752013-07-10 00:45:36 +000017#include <algorithm>
kwibergd1fe2812016-04-27 06:47:29 -070018#include <memory>
henrike@webrtc.org28e20752013-07-10 00:45:36 +000019#include <string>
deadbeef67cf2c12016-04-13 10:07:16 -070020#include <unordered_map>
henrike@webrtc.org28e20752013-07-10 00:45:36 +000021#include <vector>
22
Henrik Kjellander15583c12016-02-10 10:53:12 +010023#include "webrtc/api/jsepicecandidate.h"
24#include "webrtc/api/jsepsessiondescription.h"
tfarina5237aaf2015-11-10 23:44:30 -080025#include "webrtc/base/arraysize.h"
nissec80e7412017-01-11 05:56:46 -080026#include "webrtc/base/checks.h"
buildbot@webrtc.orga09a9992014-08-13 17:26:08 +000027#include "webrtc/base/logging.h"
28#include "webrtc/base/messagedigest.h"
29#include "webrtc/base/stringutils.h"
isheriff6f8d6862016-05-26 11:24:55 -070030// for RtpExtension
31#include "webrtc/config.h"
kjellandera96e2d72016-02-04 23:52:28 -080032#include "webrtc/media/base/codec.h"
kjellandera96e2d72016-02-04 23:52:28 -080033#include "webrtc/media/base/cryptoparams.h"
kjellanderf4752772016-03-02 05:42:30 -080034#include "webrtc/media/base/mediaconstants.h"
kjellandera96e2d72016-02-04 23:52:28 -080035#include "webrtc/media/base/rtputils.h"
deadbeef953c2ce2017-01-09 14:53:41 -080036#include "webrtc/media/sctp/sctptransportinternal.h"
kjellandera96e2d72016-02-04 23:52:28 -080037#include "webrtc/p2p/base/candidate.h"
kjellanderf4752772016-03-02 05:42:30 -080038#include "webrtc/p2p/base/p2pconstants.h"
kjellandera96e2d72016-02-04 23:52:28 -080039#include "webrtc/p2p/base/port.h"
kjellander@webrtc.org9b8df252016-02-12 06:47:59 +010040#include "webrtc/pc/mediasession.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000041
42using cricket::AudioContentDescription;
43using cricket::Candidate;
44using cricket::Candidates;
45using cricket::ContentDescription;
46using cricket::ContentInfo;
47using cricket::CryptoParams;
48using cricket::DataContentDescription;
49using cricket::ICE_CANDIDATE_COMPONENT_RTP;
50using cricket::ICE_CANDIDATE_COMPONENT_RTCP;
51using cricket::kCodecParamMaxBitrate;
52using cricket::kCodecParamMaxPTime;
53using cricket::kCodecParamMaxQuantization;
54using cricket::kCodecParamMinBitrate;
55using cricket::kCodecParamMinPTime;
56using cricket::kCodecParamPTime;
57using cricket::kCodecParamSPropStereo;
buildbot@webrtc.orged97bb02014-05-07 11:15:20 +000058using cricket::kCodecParamStartBitrate;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000059using cricket::kCodecParamStereo;
60using cricket::kCodecParamUseInbandFec;
Minyue Li7100dcd2015-03-27 05:05:59 +010061using cricket::kCodecParamUseDtx;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000062using cricket::kCodecParamSctpProtocol;
63using cricket::kCodecParamSctpStreams;
henrike@webrtc.org1e09a712013-07-26 19:17:59 +000064using cricket::kCodecParamMaxAverageBitrate;
buildbot@webrtc.org5d639b32014-09-10 07:57:12 +000065using cricket::kCodecParamMaxPlaybackRate;
stefan@webrtc.org85d27942014-06-09 12:51:39 +000066using cricket::kCodecParamAssociatedPayloadType;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000067using cricket::MediaContentDescription;
68using cricket::MediaType;
isheriff6f8d6862016-05-26 11:24:55 -070069using cricket::RtpHeaderExtensions;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000070using cricket::SsrcGroup;
71using cricket::StreamParams;
72using cricket::StreamParamsVec;
73using cricket::TransportDescription;
74using cricket::TransportInfo;
75using cricket::VideoContentDescription;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000076using rtc::SocketAddress;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000077
henrike@webrtc.org28e20752013-07-10 00:45:36 +000078namespace cricket {
79class SessionDescription;
80}
81
82namespace webrtc {
83
84// Line type
85// RFC 4566
86// An SDP session description consists of a number of lines of text of
87// the form:
88// <type>=<value>
89// where <type> MUST be exactly one case-significant character.
deadbeef9d3584c2016-02-16 17:54:10 -080090static const int kLinePrefixLength = 2; // Length of <type>=
henrike@webrtc.org28e20752013-07-10 00:45:36 +000091static const char kLineTypeVersion = 'v';
92static const char kLineTypeOrigin = 'o';
93static const char kLineTypeSessionName = 's';
94static const char kLineTypeSessionInfo = 'i';
95static const char kLineTypeSessionUri = 'u';
96static const char kLineTypeSessionEmail = 'e';
97static const char kLineTypeSessionPhone = 'p';
98static const char kLineTypeSessionBandwidth = 'b';
99static const char kLineTypeTiming = 't';
100static const char kLineTypeRepeatTimes = 'r';
101static const char kLineTypeTimeZone = 'z';
102static const char kLineTypeEncryptionKey = 'k';
103static const char kLineTypeMedia = 'm';
104static const char kLineTypeConnection = 'c';
105static const char kLineTypeAttributes = 'a';
106
107// Attributes
108static const char kAttributeGroup[] = "group";
109static const char kAttributeMid[] = "mid";
deadbeef9d3584c2016-02-16 17:54:10 -0800110static const char kAttributeMsid[] = "msid";
deadbeef25ed4352016-12-12 18:37:36 -0800111static const char kAttributeBundleOnly[] = "bundle-only";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000112static const char kAttributeRtcpMux[] = "rtcp-mux";
deadbeef13871492015-12-09 12:37:51 -0800113static const char kAttributeRtcpReducedSize[] = "rtcp-rsize";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000114static const char kAttributeSsrc[] = "ssrc";
115static const char kSsrcAttributeCname[] = "cname";
116static const char kAttributeExtmap[] = "extmap";
117// draft-alvestrand-mmusic-msid-01
118// a=msid-semantic: WMS
119static const char kAttributeMsidSemantics[] = "msid-semantic";
120static const char kMediaStreamSemantic[] = "WMS";
121static const char kSsrcAttributeMsid[] = "msid";
122static const char kDefaultMsid[] = "default";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000123static const char kSsrcAttributeMslabel[] = "mslabel";
124static const char kSSrcAttributeLabel[] = "label";
125static const char kAttributeSsrcGroup[] = "ssrc-group";
126static const char kAttributeCrypto[] = "crypto";
127static const char kAttributeCandidate[] = "candidate";
128static const char kAttributeCandidateTyp[] = "typ";
129static const char kAttributeCandidateRaddr[] = "raddr";
130static const char kAttributeCandidateRport[] = "rport";
honghaiza54a0802015-12-16 18:37:23 -0800131static const char kAttributeCandidateUfrag[] = "ufrag";
132static const char kAttributeCandidatePwd[] = "pwd";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000133static const char kAttributeCandidateGeneration[] = "generation";
honghaiza0c44ea2016-03-23 16:07:48 -0700134static const char kAttributeCandidateNetworkId[] = "network-id";
honghaize1a0c942016-02-16 14:54:56 -0800135static const char kAttributeCandidateNetworkCost[] = "network-cost";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000136static const char kAttributeFingerprint[] = "fingerprint";
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +0000137static const char kAttributeSetup[] = "setup";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000138static const char kAttributeFmtp[] = "fmtp";
139static const char kAttributeRtpmap[] = "rtpmap";
wu@webrtc.org78187522013-10-07 23:32:02 +0000140static const char kAttributeSctpmap[] = "sctpmap";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000141static const char kAttributeRtcp[] = "rtcp";
142static const char kAttributeIceUfrag[] = "ice-ufrag";
143static const char kAttributeIcePwd[] = "ice-pwd";
144static const char kAttributeIceLite[] = "ice-lite";
145static const char kAttributeIceOption[] = "ice-options";
146static const char kAttributeSendOnly[] = "sendonly";
147static const char kAttributeRecvOnly[] = "recvonly";
148static const char kAttributeRtcpFb[] = "rtcp-fb";
149static const char kAttributeSendRecv[] = "sendrecv";
150static const char kAttributeInactive[] = "inactive";
jiayl@webrtc.orgddb85ab2014-09-05 16:31:56 +0000151// draft-ietf-mmusic-sctp-sdp-07
152// a=sctp-port
153static const char kAttributeSctpPort[] = "sctp-port";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000154
155// Experimental flags
156static const char kAttributeXGoogleFlag[] = "x-google-flag";
157static const char kValueConference[] = "conference";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000158
159// Candidate
160static const char kCandidateHost[] = "host";
161static const char kCandidateSrflx[] = "srflx";
Honghai Zhang7fb69db2016-03-14 11:59:18 -0700162static const char kCandidatePrflx[] = "prflx";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000163static const char kCandidateRelay[] = "relay";
mallinath@webrtc.org2d60c5e2014-08-08 22:29:20 +0000164static const char kTcpCandidateType[] = "tcptype";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000165
166static const char kSdpDelimiterEqual = '=';
167static const char kSdpDelimiterSpace = ' ';
168static const char kSdpDelimiterColon = ':';
169static const char kSdpDelimiterSemicolon = ';';
170static const char kSdpDelimiterSlash = '/';
171static const char kNewLine = '\n';
172static const char kReturn = '\r';
173static const char kLineBreak[] = "\r\n";
174
175// TODO: Generate the Session and Time description
176// instead of hardcoding.
177static const char kSessionVersion[] = "v=0";
178// RFC 4566
179static const char kSessionOriginUsername[] = "-";
180static const char kSessionOriginSessionId[] = "0";
181static const char kSessionOriginSessionVersion[] = "0";
182static const char kSessionOriginNettype[] = "IN";
183static const char kSessionOriginAddrtype[] = "IP4";
184static const char kSessionOriginAddress[] = "127.0.0.1";
185static const char kSessionName[] = "s=-";
186static const char kTimeDescription[] = "t=0 0";
187static const char kAttrGroup[] = "a=group:BUNDLE";
188static const char kConnectionNettype[] = "IN";
pthatcher@webrtc.orgc9d6d142014-10-23 23:37:22 +0000189static const char kConnectionIpv4Addrtype[] = "IP4";
190static const char kConnectionIpv6Addrtype[] = "IP6";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000191static const char kMediaTypeVideo[] = "video";
192static const char kMediaTypeAudio[] = "audio";
193static const char kMediaTypeData[] = "application";
194static const char kMediaPortRejected[] = "0";
pthatcher@webrtc.orgc9d6d142014-10-23 23:37:22 +0000195// draft-ietf-mmusic-trickle-ice-01
196// When no candidates have been gathered, set the connection
197// address to IP6 ::.
perkj@webrtc.orgd105cc82014-11-07 11:22:06 +0000198// TODO(perkj): FF can not parse IP6 ::. See http://crbug/430333
199// Use IPV4 per default.
200static const char kDummyAddress[] = "0.0.0.0";
pthatcher@webrtc.orgc9d6d142014-10-23 23:37:22 +0000201static const char kDummyPort[] = "9";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000202// RFC 3556
203static const char kApplicationSpecificMaximum[] = "AS";
204
205static const int kDefaultVideoClockrate = 90000;
206
wu@webrtc.org78187522013-10-07 23:32:02 +0000207static const char kDefaultSctpmapProtocol[] = "webrtc-datachannel";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000208
pkasting@chromium.orgd3245462015-02-23 21:28:22 +0000209// RTP payload type is in the 0-127 range. Use -1 to indicate "all" payload
210// types.
211const int kWildcardPayloadType = -1;
212
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000213struct SsrcInfo {
Peter Boström0c4e06b2015-10-07 12:23:21 +0200214 uint32_t ssrc_id;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000215 std::string cname;
deadbeef9d3584c2016-02-16 17:54:10 -0800216 std::string stream_id;
217 std::string track_id;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000218
219 // For backward compatibility.
220 // TODO(ronghuawu): Remove below 2 fields once all the clients support msid.
221 std::string label;
222 std::string mslabel;
223};
224typedef std::vector<SsrcInfo> SsrcInfoVec;
225typedef std::vector<SsrcGroup> SsrcGroupVec;
226
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000227template <class T>
228static void AddFmtpLine(const T& codec, std::string* message);
229static void BuildMediaDescription(const ContentInfo* content_info,
230 const TransportInfo* transport_info,
231 const MediaType media_type,
wu@webrtc.org4c3e9912014-07-16 21:03:13 +0000232 const std::vector<Candidate>& candidates,
deadbeef9d3584c2016-02-16 17:54:10 -0800233 bool unified_plan_sdp,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000234 std::string* message);
jiayl@webrtc.org9c16c392014-05-01 18:30:30 +0000235static void BuildSctpContentAttributes(std::string* message, int sctp_port);
deadbeef9d3584c2016-02-16 17:54:10 -0800236static void BuildRtpContentAttributes(const MediaContentDescription* media_desc,
237 const MediaType media_type,
238 bool unified_plan_sdp,
239 std::string* message);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000240static void BuildRtpMap(const MediaContentDescription* media_desc,
241 const MediaType media_type,
242 std::string* message);
243static void BuildCandidate(const std::vector<Candidate>& candidates,
honghaiza54a0802015-12-16 18:37:23 -0800244 bool include_ufrag,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000245 std::string* message);
246static void BuildIceOptions(const std::vector<std::string>& transport_options,
247 std::string* message);
pthatcher@webrtc.org3341b402015-02-13 21:14:22 +0000248static bool IsRtp(const std::string& protocol);
249static bool IsDtlsSctp(const std::string& protocol);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000250static bool ParseSessionDescription(const std::string& message, size_t* pos,
251 std::string* session_id,
252 std::string* session_version,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000253 TransportDescription* session_td,
254 RtpHeaderExtensions* session_extmaps,
255 cricket::SessionDescription* desc,
256 SdpParseError* error);
257static bool ParseGroupAttribute(const std::string& line,
258 cricket::SessionDescription* desc,
259 SdpParseError* error);
260static bool ParseMediaDescription(
261 const std::string& message,
262 const TransportDescription& session_td,
263 const RtpHeaderExtensions& session_extmaps,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000264 size_t* pos, cricket::SessionDescription* desc,
265 std::vector<JsepIceCandidate*>* candidates,
266 SdpParseError* error);
267static bool ParseContent(const std::string& message,
268 const MediaType media_type,
269 int mline_index,
270 const std::string& protocol,
deadbeef67cf2c12016-04-13 10:07:16 -0700271 const std::vector<int>& payload_types,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000272 size_t* pos,
273 std::string* content_name,
deadbeef25ed4352016-12-12 18:37:36 -0800274 bool* bundle_only,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000275 MediaContentDescription* media_desc,
276 TransportDescription* transport,
277 std::vector<JsepIceCandidate*>* candidates,
278 SdpParseError* error);
279static bool ParseSsrcAttribute(const std::string& line,
280 SsrcInfoVec* ssrc_infos,
281 SdpParseError* error);
282static bool ParseSsrcGroupAttribute(const std::string& line,
283 SsrcGroupVec* ssrc_groups,
284 SdpParseError* error);
285static bool ParseCryptoAttribute(const std::string& line,
286 MediaContentDescription* media_desc,
287 SdpParseError* error);
288static bool ParseRtpmapAttribute(const std::string& line,
289 const MediaType media_type,
deadbeef67cf2c12016-04-13 10:07:16 -0700290 const std::vector<int>& payload_types,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000291 MediaContentDescription* media_desc,
292 SdpParseError* error);
293static bool ParseFmtpAttributes(const std::string& line,
294 const MediaType media_type,
295 MediaContentDescription* media_desc,
296 SdpParseError* error);
297static bool ParseFmtpParam(const std::string& line, std::string* parameter,
298 std::string* value, SdpParseError* error);
299static bool ParseCandidate(const std::string& message, Candidate* candidate,
300 SdpParseError* error, bool is_raw);
301static bool ParseRtcpFbAttribute(const std::string& line,
302 const MediaType media_type,
303 MediaContentDescription* media_desc,
304 SdpParseError* error);
305static bool ParseIceOptions(const std::string& line,
306 std::vector<std::string>* transport_options,
307 SdpParseError* error);
308static bool ParseExtmap(const std::string& line,
isheriff6f8d6862016-05-26 11:24:55 -0700309 RtpExtension* extmap,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000310 SdpParseError* error);
311static bool ParseFingerprintAttribute(const std::string& line,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000312 rtc::SSLFingerprint** fingerprint,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000313 SdpParseError* error);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +0000314static bool ParseDtlsSetup(const std::string& line,
315 cricket::ConnectionRole* role,
316 SdpParseError* error);
deadbeef9d3584c2016-02-16 17:54:10 -0800317static bool ParseMsidAttribute(const std::string& line,
318 std::string* stream_id,
319 std::string* track_id,
320 SdpParseError* error);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000321
322// Helper functions
323
324// Below ParseFailed*** functions output the line that caused the parsing
325// failure and the detailed reason (|description|) of the failure to |error|.
326// The functions always return false so that they can be used directly in the
327// following way when error happens:
328// "return ParseFailed***(...);"
329
330// The line starting at |line_start| of |message| is the failing line.
331// The reason for the failure should be provided in the |description|.
332// An example of a description could be "unknown character".
333static bool ParseFailed(const std::string& message,
334 size_t line_start,
335 const std::string& description,
336 SdpParseError* error) {
337 // Get the first line of |message| from |line_start|.
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +0000338 std::string first_line;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000339 size_t line_end = message.find(kNewLine, line_start);
340 if (line_end != std::string::npos) {
341 if (line_end > 0 && (message.at(line_end - 1) == kReturn)) {
342 --line_end;
343 }
344 first_line = message.substr(line_start, (line_end - line_start));
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +0000345 } else {
346 first_line = message.substr(line_start);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000347 }
348
349 if (error) {
350 error->line = first_line;
351 error->description = description;
352 }
353 LOG(LS_ERROR) << "Failed to parse: \"" << first_line
354 << "\". Reason: " << description;
355 return false;
356}
357
358// |line| is the failing line. The reason for the failure should be
359// provided in the |description|.
360static bool ParseFailed(const std::string& line,
361 const std::string& description,
362 SdpParseError* error) {
363 return ParseFailed(line, 0, description, error);
364}
365
366// Parses failure where the failing SDP line isn't know or there are multiple
367// failing lines.
368static bool ParseFailed(const std::string& description,
369 SdpParseError* error) {
370 return ParseFailed("", description, error);
371}
372
373// |line| is the failing line. The failure is due to the fact that |line|
374// doesn't have |expected_fields| fields.
375static bool ParseFailedExpectFieldNum(const std::string& line,
376 int expected_fields,
377 SdpParseError* error) {
378 std::ostringstream description;
379 description << "Expects " << expected_fields << " fields.";
380 return ParseFailed(line, description.str(), error);
381}
382
383// |line| is the failing line. The failure is due to the fact that |line| has
384// less than |expected_min_fields| fields.
385static bool ParseFailedExpectMinFieldNum(const std::string& line,
386 int expected_min_fields,
387 SdpParseError* error) {
388 std::ostringstream description;
389 description << "Expects at least " << expected_min_fields << " fields.";
390 return ParseFailed(line, description.str(), error);
391}
392
393// |line| is the failing line. The failure is due to the fact that it failed to
394// get the value of |attribute|.
395static bool ParseFailedGetValue(const std::string& line,
396 const std::string& attribute,
397 SdpParseError* error) {
398 std::ostringstream description;
399 description << "Failed to get the value of attribute: " << attribute;
400 return ParseFailed(line, description.str(), error);
401}
402
403// The line starting at |line_start| of |message| is the failing line. The
404// failure is due to the line type (e.g. the "m" part of the "m-line")
405// not matching what is expected. The expected line type should be
406// provided as |line_type|.
407static bool ParseFailedExpectLine(const std::string& message,
408 size_t line_start,
409 const char line_type,
410 const std::string& line_value,
411 SdpParseError* error) {
412 std::ostringstream description;
413 description << "Expect line: " << line_type << "=" << line_value;
414 return ParseFailed(message, line_start, description.str(), error);
415}
416
417static bool AddLine(const std::string& line, std::string* message) {
418 if (!message)
419 return false;
420
421 message->append(line);
422 message->append(kLineBreak);
423 return true;
424}
425
426static bool GetLine(const std::string& message,
427 size_t* pos,
428 std::string* line) {
429 size_t line_begin = *pos;
430 size_t line_end = message.find(kNewLine, line_begin);
431 if (line_end == std::string::npos) {
432 return false;
433 }
434 // Update the new start position
435 *pos = line_end + 1;
436 if (line_end > 0 && (message.at(line_end - 1) == kReturn)) {
437 --line_end;
438 }
439 *line = message.substr(line_begin, (line_end - line_begin));
440 const char* cline = line->c_str();
441 // RFC 4566
442 // An SDP session description consists of a number of lines of text of
443 // the form:
444 // <type>=<value>
445 // where <type> MUST be exactly one case-significant character and
446 // <value> is structured text whose format depends on <type>.
447 // Whitespace MUST NOT be used on either side of the "=" sign.
decurtis@webrtc.org8af11042015-01-07 19:15:51 +0000448 if (line->length() < 3 ||
449 !islower(cline[0]) ||
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000450 cline[1] != kSdpDelimiterEqual ||
451 cline[2] == kSdpDelimiterSpace) {
452 *pos = line_begin;
453 return false;
454 }
455 return true;
456}
457
458// Init |os| to "|type|=|value|".
459static void InitLine(const char type,
460 const std::string& value,
461 std::ostringstream* os) {
462 os->str("");
463 *os << type << kSdpDelimiterEqual << value;
464}
465
466// Init |os| to "a=|attribute|".
467static void InitAttrLine(const std::string& attribute, std::ostringstream* os) {
468 InitLine(kLineTypeAttributes, attribute, os);
469}
470
471// Writes a SDP attribute line based on |attribute| and |value| to |message|.
472static void AddAttributeLine(const std::string& attribute, int value,
473 std::string* message) {
474 std::ostringstream os;
475 InitAttrLine(attribute, &os);
476 os << kSdpDelimiterColon << value;
477 AddLine(os.str(), message);
478}
479
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000480static bool IsLineType(const std::string& message,
481 const char type,
482 size_t line_start) {
483 if (message.size() < line_start + kLinePrefixLength) {
484 return false;
485 }
486 const char* cmessage = message.c_str();
487 return (cmessage[line_start] == type &&
488 cmessage[line_start + 1] == kSdpDelimiterEqual);
489}
490
491static bool IsLineType(const std::string& line,
492 const char type) {
493 return IsLineType(line, type, 0);
494}
495
496static bool GetLineWithType(const std::string& message, size_t* pos,
497 std::string* line, const char type) {
498 if (!IsLineType(message, type, *pos)) {
499 return false;
500 }
501
502 if (!GetLine(message, pos, line))
503 return false;
504
505 return true;
506}
507
508static bool HasAttribute(const std::string& line,
509 const std::string& attribute) {
510 return (line.compare(kLinePrefixLength, attribute.size(), attribute) == 0);
511}
512
Peter Boström0c4e06b2015-10-07 12:23:21 +0200513static bool AddSsrcLine(uint32_t ssrc_id,
514 const std::string& attribute,
515 const std::string& value,
516 std::string* message) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000517 // RFC 5576
518 // a=ssrc:<ssrc-id> <attribute>:<value>
519 std::ostringstream os;
520 InitAttrLine(kAttributeSsrc, &os);
521 os << kSdpDelimiterColon << ssrc_id << kSdpDelimiterSpace
522 << attribute << kSdpDelimiterColon << value;
523 return AddLine(os.str(), message);
524}
525
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000526// Get value only from <attribute>:<value>.
527static bool GetValue(const std::string& message, const std::string& attribute,
528 std::string* value, SdpParseError* error) {
529 std::string leftpart;
Donald Curtis0e07f922015-05-15 09:21:23 -0700530 if (!rtc::tokenize_first(message, kSdpDelimiterColon, &leftpart, value)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000531 return ParseFailedGetValue(message, attribute, error);
532 }
533 // The left part should end with the expected attribute.
534 if (leftpart.length() < attribute.length() ||
535 leftpart.compare(leftpart.length() - attribute.length(),
536 attribute.length(), attribute) != 0) {
537 return ParseFailedGetValue(message, attribute, error);
538 }
539 return true;
540}
541
542static bool CaseInsensitiveFind(std::string str1, std::string str2) {
543 std::transform(str1.begin(), str1.end(), str1.begin(),
544 ::tolower);
545 std::transform(str2.begin(), str2.end(), str2.begin(),
546 ::tolower);
547 return str1.find(str2) != std::string::npos;
548}
549
wu@webrtc.org5e760e72014-04-02 23:19:09 +0000550template <class T>
551static bool GetValueFromString(const std::string& line,
552 const std::string& s,
553 T* t,
554 SdpParseError* error) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000555 if (!rtc::FromString(s, t)) {
wu@webrtc.org5e760e72014-04-02 23:19:09 +0000556 std::ostringstream description;
557 description << "Invalid value: " << s << ".";
558 return ParseFailed(line, description.str(), error);
559 }
560 return true;
561}
562
pkasting@chromium.orge9facf82015-02-17 20:36:28 +0000563static bool GetPayloadTypeFromString(const std::string& line,
564 const std::string& s,
565 int* payload_type,
566 SdpParseError* error) {
567 return GetValueFromString(line, s, payload_type, error) &&
568 cricket::IsValidRtpPayloadType(*payload_type);
569}
570
Taylor Brandstetter5de6b752016-03-09 17:02:30 -0800571// |msid_stream_id| and |msid_track_id| represent the stream/track ID from the
572// "a=msid" attribute, if it exists. They are empty if the attribute does not
573// exist.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000574void CreateTracksFromSsrcInfos(const SsrcInfoVec& ssrc_infos,
Taylor Brandstetter5de6b752016-03-09 17:02:30 -0800575 const std::string& msid_stream_id,
576 const std::string& msid_track_id,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000577 StreamParamsVec* tracks) {
nisseede5da42017-01-12 05:15:36 -0800578 RTC_DCHECK(tracks != NULL);
579 RTC_DCHECK(msid_stream_id.empty() == msid_track_id.empty());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000580 for (SsrcInfoVec::const_iterator ssrc_info = ssrc_infos.begin();
581 ssrc_info != ssrc_infos.end(); ++ssrc_info) {
582 if (ssrc_info->cname.empty()) {
583 continue;
584 }
585
Taylor Brandstetter5de6b752016-03-09 17:02:30 -0800586 std::string stream_id;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000587 std::string track_id;
Taylor Brandstetter5de6b752016-03-09 17:02:30 -0800588 if (ssrc_info->stream_id.empty() && !ssrc_info->mslabel.empty()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000589 // If there's no msid and there's mslabel, we consider this is a sdp from
590 // a older version of client that doesn't support msid.
591 // In that case, we use the mslabel and label to construct the track.
Taylor Brandstetter5de6b752016-03-09 17:02:30 -0800592 stream_id = ssrc_info->mslabel;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000593 track_id = ssrc_info->label;
Taylor Brandstetter5de6b752016-03-09 17:02:30 -0800594 } else if (ssrc_info->stream_id.empty() && !msid_stream_id.empty()) {
595 // If there's no msid in the SSRC attributes, but there's a global one
596 // (from a=msid), use that. This is the case with unified plan SDP.
597 stream_id = msid_stream_id;
598 track_id = msid_track_id;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000599 } else {
Taylor Brandstetter5de6b752016-03-09 17:02:30 -0800600 stream_id = ssrc_info->stream_id;
deadbeef9d3584c2016-02-16 17:54:10 -0800601 track_id = ssrc_info->track_id;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000602 }
Taylor Brandstetter5de6b752016-03-09 17:02:30 -0800603 // If a stream/track ID wasn't populated from the SSRC attributes OR the
604 // msid attribute, use default/random values.
605 if (stream_id.empty()) {
606 stream_id = kDefaultMsid;
607 }
608 if (track_id.empty()) {
609 // TODO(ronghuawu): What should we do if the track id doesn't appear?
610 // Create random string (which will be used as track label later)?
611 track_id = rtc::CreateRandomString(8);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000612 }
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;
Taylor Brandstetter5de6b752016-03-09 17:02:30 -0800627 track->sync_label = stream_id;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000628 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 {
nissec80e7412017-01-11 05:56:46 -0800662 RTC_NOTREACHED();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000663 }
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
deadbeef9d3584c2016-02-16 17:54:10 -0800793std::string SdpSerialize(const JsepSessionDescription& jdesc,
794 bool unified_plan_sdp) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000795 const cricket::SessionDescription* desc = jdesc.description();
796 if (!desc) {
797 return "";
798 }
799
800 std::string message;
801
802 // Session Description.
803 AddLine(kSessionVersion, &message);
804 // Session Origin
805 // RFC 4566
806 // o=<username> <sess-id> <sess-version> <nettype> <addrtype>
807 // <unicast-address>
808 std::ostringstream os;
809 InitLine(kLineTypeOrigin, kSessionOriginUsername, &os);
jbauch083b73f2015-07-16 02:46:32 -0700810 const std::string& session_id = jdesc.session_id().empty() ?
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000811 kSessionOriginSessionId : jdesc.session_id();
jbauch083b73f2015-07-16 02:46:32 -0700812 const std::string& session_version = jdesc.session_version().empty() ?
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000813 kSessionOriginSessionVersion : jdesc.session_version();
814 os << " " << session_id << " " << session_version << " "
815 << kSessionOriginNettype << " " << kSessionOriginAddrtype << " "
816 << kSessionOriginAddress;
817 AddLine(os.str(), &message);
818 AddLine(kSessionName, &message);
819
820 // Time Description.
821 AddLine(kTimeDescription, &message);
822
823 // Group
824 if (desc->HasGroup(cricket::GROUP_TYPE_BUNDLE)) {
825 std::string group_line = kAttrGroup;
826 const cricket::ContentGroup* group =
827 desc->GetGroupByName(cricket::GROUP_TYPE_BUNDLE);
nisseede5da42017-01-12 05:15:36 -0800828 RTC_DCHECK(group != NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000829 const cricket::ContentNames& content_names = group->content_names();
830 for (cricket::ContentNames::const_iterator it = content_names.begin();
831 it != content_names.end(); ++it) {
832 group_line.append(" ");
833 group_line.append(*it);
834 }
835 AddLine(group_line, &message);
836 }
837
838 // MediaStream semantics
839 InitAttrLine(kAttributeMsidSemantics, &os);
840 os << kSdpDelimiterColon << " " << kMediaStreamSemantic;
wu@webrtc.org4c3e9912014-07-16 21:03:13 +0000841
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000842 std::set<std::string> media_stream_labels;
843 const ContentInfo* audio_content = GetFirstAudioContent(desc);
844 if (audio_content)
845 GetMediaStreamLabels(audio_content, &media_stream_labels);
wu@webrtc.org4c3e9912014-07-16 21:03:13 +0000846
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000847 const ContentInfo* video_content = GetFirstVideoContent(desc);
848 if (video_content)
849 GetMediaStreamLabels(video_content, &media_stream_labels);
wu@webrtc.org4c3e9912014-07-16 21:03:13 +0000850
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000851 for (std::set<std::string>::const_iterator it =
852 media_stream_labels.begin(); it != media_stream_labels.end(); ++it) {
853 os << " " << *it;
854 }
855 AddLine(os.str(), &message);
856
wu@webrtc.org4c3e9912014-07-16 21:03:13 +0000857 // Preserve the order of the media contents.
858 int mline_index = -1;
859 for (cricket::ContentInfos::const_iterator it = desc->contents().begin();
860 it != desc->contents().end(); ++it) {
861 const MediaContentDescription* mdesc =
862 static_cast<const MediaContentDescription*>(it->description);
863 std::vector<Candidate> candidates;
864 GetCandidatesByMindex(jdesc, ++mline_index, &candidates);
deadbeef9d3584c2016-02-16 17:54:10 -0800865 BuildMediaDescription(&*it, desc->GetTransportInfoByName(it->name),
866 mdesc->type(), candidates, unified_plan_sdp,
wu@webrtc.org4c3e9912014-07-16 21:03:13 +0000867 &message);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000868 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000869 return message;
870}
871
872// Serializes the passed in IceCandidateInterface to a SDP string.
873// candidate - The candidate to be serialized.
Honghai Zhang7fb69db2016-03-14 11:59:18 -0700874std::string SdpSerializeCandidate(const IceCandidateInterface& candidate) {
875 return SdpSerializeCandidate(candidate.candidate());
876}
877
878// Serializes a cricket Candidate.
879std::string SdpSerializeCandidate(const cricket::Candidate& candidate) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000880 std::string message;
Honghai Zhang7fb69db2016-03-14 11:59:18 -0700881 std::vector<cricket::Candidate> candidates(1, candidate);
honghaiza54a0802015-12-16 18:37:23 -0800882 BuildCandidate(candidates, true, &message);
wu@webrtc.orgec9f5fb2014-06-24 17:05:10 +0000883 // From WebRTC draft section 4.8.1.1 candidate-attribute will be
884 // just candidate:<candidate> not a=candidate:<blah>CRLF
nisseede5da42017-01-12 05:15:36 -0800885 RTC_DCHECK(message.find("a=") == 0);
wu@webrtc.orgec9f5fb2014-06-24 17:05:10 +0000886 message.erase(0, 2);
nisseede5da42017-01-12 05:15:36 -0800887 RTC_DCHECK(message.find(kLineBreak) == message.size() - 2);
wu@webrtc.orgec9f5fb2014-06-24 17:05:10 +0000888 message.resize(message.size() - 2);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000889 return message;
890}
891
892bool SdpDeserialize(const std::string& message,
893 JsepSessionDescription* jdesc,
894 SdpParseError* error) {
895 std::string session_id;
896 std::string session_version;
Peter Thatcher7cbd1882015-09-17 18:54:52 -0700897 TransportDescription session_td("", "");
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000898 RtpHeaderExtensions session_extmaps;
899 cricket::SessionDescription* desc = new cricket::SessionDescription();
900 std::vector<JsepIceCandidate*> candidates;
901 size_t current_pos = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000902
903 // Session Description
904 if (!ParseSessionDescription(message, &current_pos, &session_id,
deadbeefc80741f2015-10-22 13:14:45 -0700905 &session_version, &session_td, &session_extmaps,
906 desc, error)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000907 delete desc;
908 return false;
909 }
910
911 // Media Description
deadbeefc80741f2015-10-22 13:14:45 -0700912 if (!ParseMediaDescription(message, session_td, session_extmaps, &current_pos,
913 desc, &candidates, error)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000914 delete desc;
915 for (std::vector<JsepIceCandidate*>::const_iterator
916 it = candidates.begin(); it != candidates.end(); ++it) {
917 delete *it;
918 }
919 return false;
920 }
921
922 jdesc->Initialize(desc, session_id, session_version);
923
924 for (std::vector<JsepIceCandidate*>::const_iterator
925 it = candidates.begin(); it != candidates.end(); ++it) {
926 jdesc->AddCandidate(*it);
927 delete *it;
928 }
929 return true;
930}
931
932bool SdpDeserializeCandidate(const std::string& message,
933 JsepIceCandidate* jcandidate,
934 SdpParseError* error) {
nisseede5da42017-01-12 05:15:36 -0800935 RTC_DCHECK(jcandidate != NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000936 Candidate candidate;
937 if (!ParseCandidate(message, &candidate, error, true)) {
938 return false;
939 }
940 jcandidate->SetCandidate(candidate);
941 return true;
942}
943
Honghai Zhang7fb69db2016-03-14 11:59:18 -0700944bool SdpDeserializeCandidate(const std::string& transport_name,
945 const std::string& message,
946 cricket::Candidate* candidate,
947 SdpParseError* error) {
nisseede5da42017-01-12 05:15:36 -0800948 RTC_DCHECK(candidate != nullptr);
Honghai Zhang7fb69db2016-03-14 11:59:18 -0700949 if (!ParseCandidate(message, candidate, error, true)) {
950 return false;
951 }
952 candidate->set_transport_name(transport_name);
953 return true;
954}
955
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000956bool ParseCandidate(const std::string& message, Candidate* candidate,
957 SdpParseError* error, bool is_raw) {
nisseede5da42017-01-12 05:15:36 -0800958 RTC_DCHECK(candidate != NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000959
960 // Get the first line from |message|.
jiayl@webrtc.org7ec3f9f2014-08-08 23:09:15 +0000961 std::string first_line = message;
962 size_t pos = 0;
963 GetLine(message, &pos, &first_line);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000964
jiayl@webrtc.org7ec3f9f2014-08-08 23:09:15 +0000965 // Makes sure |message| contains only one line.
966 if (message.size() > first_line.size()) {
967 std::string left, right;
Donald Curtis0e07f922015-05-15 09:21:23 -0700968 if (rtc::tokenize_first(message, kNewLine, &left, &right) &&
969 !right.empty()) {
jiayl@webrtc.org7ec3f9f2014-08-08 23:09:15 +0000970 return ParseFailed(message, 0, "Expect one line only", error);
971 }
972 }
973
974 // From WebRTC draft section 4.8.1.1 candidate-attribute should be
975 // candidate:<candidate> when trickled, but we still support
976 // a=candidate:<blah>CRLF for backward compatibility and for parsing a line
977 // from the SDP.
978 if (IsLineType(first_line, kLineTypeAttributes)) {
979 first_line = first_line.substr(kLinePrefixLength);
980 }
981
982 std::string attribute_candidate;
983 std::string candidate_value;
984
985 // |first_line| must be in the form of "candidate:<value>".
Donald Curtis144d0182015-05-15 13:14:24 -0700986 if (!rtc::tokenize_first(first_line, kSdpDelimiterColon, &attribute_candidate,
987 &candidate_value) ||
jiayl@webrtc.org7ec3f9f2014-08-08 23:09:15 +0000988 attribute_candidate != kAttributeCandidate) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000989 if (is_raw) {
990 std::ostringstream description;
jiayl@webrtc.org7ec3f9f2014-08-08 23:09:15 +0000991 description << "Expect line: " << kAttributeCandidate
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000992 << ":" << "<candidate-str>";
993 return ParseFailed(first_line, 0, description.str(), error);
994 } else {
995 return ParseFailedExpectLine(first_line, 0, kLineTypeAttributes,
996 kAttributeCandidate, error);
997 }
998 }
999
1000 std::vector<std::string> fields;
jiayl@webrtc.org7ec3f9f2014-08-08 23:09:15 +00001001 rtc::split(candidate_value, kSdpDelimiterSpace, &fields);
1002
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001003 // RFC 5245
1004 // a=candidate:<foundation> <component-id> <transport> <priority>
1005 // <connection-address> <port> typ <candidate-types>
1006 // [raddr <connection-address>] [rport <port>]
1007 // *(SP extension-att-name SP extension-att-value)
1008 const size_t expected_min_fields = 8;
1009 if (fields.size() < expected_min_fields ||
1010 (fields[6] != kAttributeCandidateTyp)) {
1011 return ParseFailedExpectMinFieldNum(first_line, expected_min_fields, error);
1012 }
jbauch083b73f2015-07-16 02:46:32 -07001013 const std::string& foundation = fields[0];
jiayl@webrtc.org7ec3f9f2014-08-08 23:09:15 +00001014
wu@webrtc.org5e760e72014-04-02 23:19:09 +00001015 int component_id = 0;
1016 if (!GetValueFromString(first_line, fields[1], &component_id, error)) {
1017 return false;
1018 }
jbauch083b73f2015-07-16 02:46:32 -07001019 const std::string& transport = fields[2];
Peter Boström0c4e06b2015-10-07 12:23:21 +02001020 uint32_t priority = 0;
wu@webrtc.org5e760e72014-04-02 23:19:09 +00001021 if (!GetValueFromString(first_line, fields[3], &priority, error)) {
1022 return false;
1023 }
jbauch083b73f2015-07-16 02:46:32 -07001024 const std::string& connection_address = fields[4];
wu@webrtc.org5e760e72014-04-02 23:19:09 +00001025 int port = 0;
1026 if (!GetValueFromString(first_line, fields[5], &port, error)) {
1027 return false;
1028 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001029 SocketAddress address(connection_address, port);
1030
1031 cricket::ProtocolType protocol;
hnslb68cc752016-12-13 10:33:41 -08001032 if (!StringToProto(transport.c_str(), &protocol)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001033 return ParseFailed(first_line, "Unsupported transport type.", error);
1034 }
hnslb68cc752016-12-13 10:33:41 -08001035 switch (protocol) {
1036 case cricket::PROTO_UDP:
1037 case cricket::PROTO_TCP:
1038 case cricket::PROTO_SSLTCP:
1039 // Supported protocol.
1040 break;
1041 default:
1042 return ParseFailed(first_line, "Unsupported transport type.", error);
1043 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001044
1045 std::string candidate_type;
jbauch083b73f2015-07-16 02:46:32 -07001046 const std::string& type = fields[7];
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001047 if (type == kCandidateHost) {
1048 candidate_type = cricket::LOCAL_PORT_TYPE;
1049 } else if (type == kCandidateSrflx) {
1050 candidate_type = cricket::STUN_PORT_TYPE;
1051 } else if (type == kCandidateRelay) {
1052 candidate_type = cricket::RELAY_PORT_TYPE;
Honghai Zhang7fb69db2016-03-14 11:59:18 -07001053 } else if (type == kCandidatePrflx) {
1054 candidate_type = cricket::PRFLX_PORT_TYPE;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001055 } else {
1056 return ParseFailed(first_line, "Unsupported candidate type.", error);
1057 }
1058
1059 size_t current_position = expected_min_fields;
1060 SocketAddress related_address;
1061 // The 2 optional fields for related address
1062 // [raddr <connection-address>] [rport <port>]
1063 if (fields.size() >= (current_position + 2) &&
1064 fields[current_position] == kAttributeCandidateRaddr) {
1065 related_address.SetIP(fields[++current_position]);
1066 ++current_position;
1067 }
1068 if (fields.size() >= (current_position + 2) &&
1069 fields[current_position] == kAttributeCandidateRport) {
wu@webrtc.org5e760e72014-04-02 23:19:09 +00001070 int port = 0;
1071 if (!GetValueFromString(
1072 first_line, fields[++current_position], &port, error)) {
1073 return false;
1074 }
1075 related_address.SetPort(port);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001076 ++current_position;
1077 }
1078
mallinath@webrtc.org2d60c5e2014-08-08 22:29:20 +00001079 // If this is a TCP candidate, it has additional extension as defined in
1080 // RFC 6544.
1081 std::string tcptype;
1082 if (fields.size() >= (current_position + 2) &&
1083 fields[current_position] == kTcpCandidateType) {
1084 tcptype = fields[++current_position];
1085 ++current_position;
1086
1087 if (tcptype != cricket::TCPTYPE_ACTIVE_STR &&
1088 tcptype != cricket::TCPTYPE_PASSIVE_STR &&
1089 tcptype != cricket::TCPTYPE_SIMOPEN_STR) {
1090 return ParseFailed(first_line, "Invalid TCP candidate type.", error);
1091 }
1092
1093 if (protocol != cricket::PROTO_TCP) {
1094 return ParseFailed(first_line, "Invalid non-TCP candidate", error);
1095 }
1096 }
1097
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001098 // Extension
honghaiza54a0802015-12-16 18:37:23 -08001099 // Though non-standard, we support the ICE ufrag and pwd being signaled on
1100 // the candidate to avoid issues with confusing which generation a candidate
1101 // belongs to when trickling multiple generations at the same time.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001102 std::string username;
1103 std::string password;
Peter Boström0c4e06b2015-10-07 12:23:21 +02001104 uint32_t generation = 0;
honghaiza0c44ea2016-03-23 16:07:48 -07001105 uint16_t network_id = 0;
1106 uint16_t network_cost = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001107 for (size_t i = current_position; i + 1 < fields.size(); ++i) {
1108 // RFC 5245
1109 // *(SP extension-att-name SP extension-att-value)
1110 if (fields[i] == kAttributeCandidateGeneration) {
wu@webrtc.org5e760e72014-04-02 23:19:09 +00001111 if (!GetValueFromString(first_line, fields[++i], &generation, error)) {
1112 return false;
1113 }
honghaiza54a0802015-12-16 18:37:23 -08001114 } else if (fields[i] == kAttributeCandidateUfrag) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001115 username = fields[++i];
honghaiza54a0802015-12-16 18:37:23 -08001116 } else if (fields[i] == kAttributeCandidatePwd) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001117 password = fields[++i];
honghaiza0c44ea2016-03-23 16:07:48 -07001118 } else if (fields[i] == kAttributeCandidateNetworkId) {
1119 if (!GetValueFromString(first_line, fields[++i], &network_id, error)) {
1120 return false;
1121 }
honghaize1a0c942016-02-16 14:54:56 -08001122 } else if (fields[i] == kAttributeCandidateNetworkCost) {
1123 if (!GetValueFromString(first_line, fields[++i], &network_cost, error)) {
1124 return false;
1125 }
Honghai Zhang351d77b2016-05-20 15:08:29 -07001126 network_cost = std::min(network_cost, rtc::kNetworkCostMax);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001127 } else {
1128 // Skip the unknown extension.
1129 ++i;
1130 }
1131 }
1132
guoweis@webrtc.org61c12472015-01-15 06:53:07 +00001133 *candidate = Candidate(component_id, cricket::ProtoToString(protocol),
guoweis@webrtc.org950c5182014-12-16 23:01:31 +00001134 address, priority, username, password, candidate_type,
honghaiza0c44ea2016-03-23 16:07:48 -07001135 generation, foundation, network_id, network_cost);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001136 candidate->set_related_address(related_address);
mallinath@webrtc.org2d60c5e2014-08-08 22:29:20 +00001137 candidate->set_tcptype(tcptype);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001138 return true;
1139}
1140
1141bool ParseIceOptions(const std::string& line,
1142 std::vector<std::string>* transport_options,
1143 SdpParseError* error) {
1144 std::string ice_options;
1145 if (!GetValue(line, kAttributeIceOption, &ice_options, error)) {
1146 return false;
1147 }
1148 std::vector<std::string> fields;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001149 rtc::split(ice_options, kSdpDelimiterSpace, &fields);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001150 for (size_t i = 0; i < fields.size(); ++i) {
1151 transport_options->push_back(fields[i]);
1152 }
1153 return true;
1154}
1155
jiayl@webrtc.orgddb85ab2014-09-05 16:31:56 +00001156bool ParseSctpPort(const std::string& line,
1157 int* sctp_port,
1158 SdpParseError* error) {
1159 // draft-ietf-mmusic-sctp-sdp-07
1160 // a=sctp-port
1161 std::vector<std::string> fields;
jiayl@webrtc.orgddb85ab2014-09-05 16:31:56 +00001162 const size_t expected_min_fields = 2;
lally69f57602015-10-08 10:15:04 -07001163 rtc::split(line.substr(kLinePrefixLength), kSdpDelimiterColon, &fields);
1164 if (fields.size() < expected_min_fields) {
1165 fields.resize(0);
1166 rtc::split(line.substr(kLinePrefixLength), kSdpDelimiterSpace, &fields);
1167 }
jiayl@webrtc.orgddb85ab2014-09-05 16:31:56 +00001168 if (fields.size() < expected_min_fields) {
1169 return ParseFailedExpectMinFieldNum(line, expected_min_fields, error);
1170 }
1171 if (!rtc::FromString(fields[1], sctp_port)) {
lally69f57602015-10-08 10:15:04 -07001172 return ParseFailed(line, "Invalid sctp port value.", error);
jiayl@webrtc.orgddb85ab2014-09-05 16:31:56 +00001173 }
1174 return true;
1175}
1176
isheriff6f8d6862016-05-26 11:24:55 -07001177bool ParseExtmap(const std::string& line,
1178 RtpExtension* extmap,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001179 SdpParseError* error) {
1180 // RFC 5285
1181 // a=extmap:<value>["/"<direction>] <URI> <extensionattributes>
1182 std::vector<std::string> fields;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001183 rtc::split(line.substr(kLinePrefixLength),
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001184 kSdpDelimiterSpace, &fields);
1185 const size_t expected_min_fields = 2;
1186 if (fields.size() < expected_min_fields) {
1187 return ParseFailedExpectMinFieldNum(line, expected_min_fields, error);
1188 }
1189 std::string uri = fields[1];
1190
1191 std::string value_direction;
1192 if (!GetValue(fields[0], kAttributeExtmap, &value_direction, error)) {
1193 return false;
1194 }
1195 std::vector<std::string> sub_fields;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001196 rtc::split(value_direction, kSdpDelimiterSlash, &sub_fields);
wu@webrtc.org5e760e72014-04-02 23:19:09 +00001197 int value = 0;
1198 if (!GetValueFromString(line, sub_fields[0], &value, error)) {
1199 return false;
1200 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001201
isheriff6f8d6862016-05-26 11:24:55 -07001202 *extmap = RtpExtension(uri, value);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001203 return true;
1204}
1205
1206void BuildMediaDescription(const ContentInfo* content_info,
1207 const TransportInfo* transport_info,
1208 const MediaType media_type,
wu@webrtc.org4c3e9912014-07-16 21:03:13 +00001209 const std::vector<Candidate>& candidates,
deadbeef9d3584c2016-02-16 17:54:10 -08001210 bool unified_plan_sdp,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001211 std::string* message) {
nisseede5da42017-01-12 05:15:36 -08001212 RTC_DCHECK(message != NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001213 if (content_info == NULL || message == NULL) {
1214 return;
1215 }
1216 // TODO: Rethink if we should use sprintfn instead of stringstream.
1217 // According to the style guide, streams should only be used for logging.
1218 // http://google-styleguide.googlecode.com/svn/
1219 // trunk/cppguide.xml?showone=Streams#Streams
1220 std::ostringstream os;
1221 const MediaContentDescription* media_desc =
henrike@webrtc.org28654cb2013-07-22 21:07:49 +00001222 static_cast<const MediaContentDescription*>(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001223 content_info->description);
nisseede5da42017-01-12 05:15:36 -08001224 RTC_DCHECK(media_desc != NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001225
jiayl@webrtc.org9c16c392014-05-01 18:30:30 +00001226 int sctp_port = cricket::kSctpDefaultPort;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001227
1228 // RFC 4566
1229 // m=<media> <port> <proto> <fmt>
1230 // fmt is a list of payload type numbers that MAY be used in the session.
1231 const char* type = NULL;
1232 if (media_type == cricket::MEDIA_TYPE_AUDIO)
1233 type = kMediaTypeAudio;
1234 else if (media_type == cricket::MEDIA_TYPE_VIDEO)
1235 type = kMediaTypeVideo;
1236 else if (media_type == cricket::MEDIA_TYPE_DATA)
1237 type = kMediaTypeData;
1238 else
nissec80e7412017-01-11 05:56:46 -08001239 RTC_NOTREACHED();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001240
1241 std::string fmt;
1242 if (media_type == cricket::MEDIA_TYPE_VIDEO) {
1243 const VideoContentDescription* video_desc =
1244 static_cast<const VideoContentDescription*>(media_desc);
1245 for (std::vector<cricket::VideoCodec>::const_iterator it =
1246 video_desc->codecs().begin();
1247 it != video_desc->codecs().end(); ++it) {
1248 fmt.append(" ");
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001249 fmt.append(rtc::ToString<int>(it->id));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001250 }
1251 } else if (media_type == cricket::MEDIA_TYPE_AUDIO) {
1252 const AudioContentDescription* audio_desc =
1253 static_cast<const AudioContentDescription*>(media_desc);
1254 for (std::vector<cricket::AudioCodec>::const_iterator it =
1255 audio_desc->codecs().begin();
1256 it != audio_desc->codecs().end(); ++it) {
1257 fmt.append(" ");
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001258 fmt.append(rtc::ToString<int>(it->id));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001259 }
1260 } else if (media_type == cricket::MEDIA_TYPE_DATA) {
jiayl@webrtc.org9c16c392014-05-01 18:30:30 +00001261 const DataContentDescription* data_desc =
1262 static_cast<const DataContentDescription*>(media_desc);
pthatcher@webrtc.org3341b402015-02-13 21:14:22 +00001263 if (IsDtlsSctp(media_desc->protocol())) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001264 fmt.append(" ");
jiayl@webrtc.org9c16c392014-05-01 18:30:30 +00001265
1266 for (std::vector<cricket::DataCodec>::const_iterator it =
1267 data_desc->codecs().begin();
1268 it != data_desc->codecs().end(); ++it) {
solenberg9fa49752016-10-08 13:02:44 -07001269 if (cricket::CodecNamesEq(it->name, cricket::kGoogleSctpDataCodecName)
1270 && it->GetParam(cricket::kCodecParamPort, &sctp_port)) {
jiayl@webrtc.org9c16c392014-05-01 18:30:30 +00001271 break;
1272 }
1273 }
1274
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001275 fmt.append(rtc::ToString<int>(sctp_port));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001276 } else {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001277 for (std::vector<cricket::DataCodec>::const_iterator it =
1278 data_desc->codecs().begin();
1279 it != data_desc->codecs().end(); ++it) {
1280 fmt.append(" ");
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001281 fmt.append(rtc::ToString<int>(it->id));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001282 }
1283 }
1284 }
1285 // The fmt must never be empty. If no codecs are found, set the fmt attribute
1286 // to 0.
1287 if (fmt.empty()) {
1288 fmt = " 0";
1289 }
1290
deadbeef25ed4352016-12-12 18:37:36 -08001291 // The port number in the m line will be updated later when associated with
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001292 // the candidates.
deadbeef25ed4352016-12-12 18:37:36 -08001293 //
1294 // A port value of 0 indicates that the m= section is rejected.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001295 // RFC 3264
1296 // To reject an offered stream, the port number in the corresponding stream in
1297 // the answer MUST be set to zero.
deadbeef25ed4352016-12-12 18:37:36 -08001298 //
1299 // However, the BUNDLE draft adds a new meaning to port zero, when used along
1300 // with a=bundle-only.
1301 const std::string& port =
1302 (content_info->rejected || content_info->bundle_only) ? kMediaPortRejected
1303 : kDummyPort;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001304
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001305 rtc::SSLFingerprint* fp = (transport_info) ?
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001306 transport_info->description.identity_fingerprint.get() : NULL;
1307
wu@webrtc.org4c3e9912014-07-16 21:03:13 +00001308 // Add the m and c lines.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001309 InitLine(kLineTypeMedia, type, &os);
1310 os << " " << port << " " << media_desc->protocol() << fmt;
wu@webrtc.org4c3e9912014-07-16 21:03:13 +00001311 std::string mline = os.str();
1312 UpdateMediaDefaultDestination(candidates, mline, message);
1313
1314 // RFC 4566
1315 // b=AS:<bandwidth>
Peter Thatcherc0c3a862015-06-24 15:31:25 -07001316 if (media_desc->bandwidth() >= 1000) {
wu@webrtc.org4c3e9912014-07-16 21:03:13 +00001317 InitLine(kLineTypeSessionBandwidth, kApplicationSpecificMaximum, &os);
1318 os << kSdpDelimiterColon << (media_desc->bandwidth() / 1000);
1319 AddLine(os.str(), message);
1320 }
1321
deadbeef25ed4352016-12-12 18:37:36 -08001322 // Add the a=bundle-only line.
1323 if (content_info->bundle_only) {
1324 InitAttrLine(kAttributeBundleOnly, &os);
1325 AddLine(os.str(), message);
1326 }
1327
wu@webrtc.org4c3e9912014-07-16 21:03:13 +00001328 // Add the a=rtcp line.
pthatcher@webrtc.org3341b402015-02-13 21:14:22 +00001329 if (IsRtp(media_desc->protocol())) {
wu@webrtc.org4c3e9912014-07-16 21:03:13 +00001330 std::string rtcp_line = GetRtcpLine(candidates);
1331 if (!rtcp_line.empty()) {
1332 AddLine(rtcp_line, message);
1333 }
1334 }
1335
honghaiza54a0802015-12-16 18:37:23 -08001336 // Build the a=candidate lines. We don't include ufrag and pwd in the
1337 // candidates in the SDP to avoid redundancy.
1338 BuildCandidate(candidates, false, message);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001339
1340 // Use the transport_info to build the media level ice-ufrag and ice-pwd.
1341 if (transport_info) {
1342 // RFC 5245
1343 // ice-pwd-att = "ice-pwd" ":" password
1344 // ice-ufrag-att = "ice-ufrag" ":" ufrag
1345 // ice-ufrag
deadbeef3f7219b2015-12-28 15:17:14 -08001346 if (!transport_info->description.ice_ufrag.empty()) {
1347 InitAttrLine(kAttributeIceUfrag, &os);
1348 os << kSdpDelimiterColon << transport_info->description.ice_ufrag;
1349 AddLine(os.str(), message);
1350 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001351 // ice-pwd
deadbeef3f7219b2015-12-28 15:17:14 -08001352 if (!transport_info->description.ice_pwd.empty()) {
1353 InitAttrLine(kAttributeIcePwd, &os);
1354 os << kSdpDelimiterColon << transport_info->description.ice_pwd;
1355 AddLine(os.str(), message);
1356 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001357
1358 // draft-petithuguenin-mmusic-ice-attributes-level-03
1359 BuildIceOptions(transport_info->description.transport_options, message);
1360
1361 // RFC 4572
1362 // fingerprint-attribute =
1363 // "fingerprint" ":" hash-func SP fingerprint
1364 if (fp) {
1365 // Insert the fingerprint attribute.
1366 InitAttrLine(kAttributeFingerprint, &os);
1367 os << kSdpDelimiterColon
1368 << fp->algorithm << kSdpDelimiterSpace
1369 << fp->GetRfc4572Fingerprint();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001370 AddLine(os.str(), message);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001371
1372 // Inserting setup attribute.
1373 if (transport_info->description.connection_role !=
1374 cricket::CONNECTIONROLE_NONE) {
1375 // Making sure we are not using "passive" mode.
1376 cricket::ConnectionRole role =
1377 transport_info->description.connection_role;
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00001378 std::string dtls_role_str;
1379 VERIFY(cricket::ConnectionRoleToString(role, &dtls_role_str));
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001380 InitAttrLine(kAttributeSetup, &os);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001381 os << kSdpDelimiterColon << dtls_role_str;
1382 AddLine(os.str(), message);
1383 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001384 }
1385 }
1386
1387 // RFC 3388
1388 // mid-attribute = "a=mid:" identification-tag
1389 // identification-tag = token
1390 // Use the content name as the mid identification-tag.
1391 InitAttrLine(kAttributeMid, &os);
1392 os << kSdpDelimiterColon << content_info->name;
1393 AddLine(os.str(), message);
1394
pthatcher@webrtc.org3341b402015-02-13 21:14:22 +00001395 if (IsDtlsSctp(media_desc->protocol())) {
jiayl@webrtc.org9c16c392014-05-01 18:30:30 +00001396 BuildSctpContentAttributes(message, sctp_port);
pthatcher@webrtc.org3341b402015-02-13 21:14:22 +00001397 } else if (IsRtp(media_desc->protocol())) {
deadbeef9d3584c2016-02-16 17:54:10 -08001398 BuildRtpContentAttributes(media_desc, media_type, unified_plan_sdp,
1399 message);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001400 }
1401}
1402
jiayl@webrtc.org9c16c392014-05-01 18:30:30 +00001403void BuildSctpContentAttributes(std::string* message, int sctp_port) {
wu@webrtc.org78187522013-10-07 23:32:02 +00001404 // draft-ietf-mmusic-sctp-sdp-04
1405 // a=sctpmap:sctpmap-number protocol [streams]
lally69f57602015-10-08 10:15:04 -07001406 // TODO(lally): switch this over to mmusic-sctp-sdp-12 (or later), with
1407 // 'a=sctp-port:'
wu@webrtc.org78187522013-10-07 23:32:02 +00001408 std::ostringstream os;
1409 InitAttrLine(kAttributeSctpmap, &os);
jiayl@webrtc.org9c16c392014-05-01 18:30:30 +00001410 os << kSdpDelimiterColon << sctp_port << kSdpDelimiterSpace
wu@webrtc.org78187522013-10-07 23:32:02 +00001411 << kDefaultSctpmapProtocol << kSdpDelimiterSpace
Taylor Brandstetter1d7a6372016-08-24 13:15:27 -07001412 << cricket::kMaxSctpStreams;
wu@webrtc.org78187522013-10-07 23:32:02 +00001413 AddLine(os.str(), message);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001414}
1415
deadbeef9d3584c2016-02-16 17:54:10 -08001416// If unified_plan_sdp is true, will use "a=msid".
1417void BuildRtpContentAttributes(const MediaContentDescription* media_desc,
1418 const MediaType media_type,
1419 bool unified_plan_sdp,
1420 std::string* message) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001421 std::ostringstream os;
1422 // RFC 5285
1423 // a=extmap:<value>["/"<direction>] <URI> <extensionattributes>
1424 // The definitions MUST be either all session level or all media level. This
1425 // implementation uses all media level.
1426 for (size_t i = 0; i < media_desc->rtp_header_extensions().size(); ++i) {
1427 InitAttrLine(kAttributeExtmap, &os);
1428 os << kSdpDelimiterColon << media_desc->rtp_header_extensions()[i].id
1429 << kSdpDelimiterSpace << media_desc->rtp_header_extensions()[i].uri;
1430 AddLine(os.str(), message);
1431 }
1432
1433 // RFC 3264
1434 // a=sendrecv || a=sendonly || a=sendrecv || a=inactive
deadbeefc80741f2015-10-22 13:14:45 -07001435 switch (media_desc->direction()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001436 case cricket::MD_INACTIVE:
1437 InitAttrLine(kAttributeInactive, &os);
1438 break;
1439 case cricket::MD_SENDONLY:
1440 InitAttrLine(kAttributeSendOnly, &os);
1441 break;
1442 case cricket::MD_RECVONLY:
1443 InitAttrLine(kAttributeRecvOnly, &os);
1444 break;
1445 case cricket::MD_SENDRECV:
1446 default:
1447 InitAttrLine(kAttributeSendRecv, &os);
1448 break;
1449 }
1450 AddLine(os.str(), message);
1451
deadbeef9d3584c2016-02-16 17:54:10 -08001452 // draft-ietf-mmusic-msid-11
1453 // a=msid:<stream id> <track id>
1454 if (unified_plan_sdp && !media_desc->streams().empty()) {
1455 if (media_desc->streams().size() > 1u) {
1456 LOG(LS_WARNING) << "Trying to serialize unified plan SDP with more than "
1457 << "one track in a media section. Omitting 'a=msid'.";
1458 } else {
1459 auto track = media_desc->streams().begin();
1460 const std::string& stream_id = track->sync_label;
deadbeef9d3584c2016-02-16 17:54:10 -08001461 InitAttrLine(kAttributeMsid, &os);
1462 os << kSdpDelimiterColon << stream_id << kSdpDelimiterSpace << track->id;
1463 AddLine(os.str(), message);
1464 }
1465 }
1466
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001467 // RFC 5761
1468 // a=rtcp-mux
1469 if (media_desc->rtcp_mux()) {
1470 InitAttrLine(kAttributeRtcpMux, &os);
1471 AddLine(os.str(), message);
1472 }
1473
deadbeef13871492015-12-09 12:37:51 -08001474 // RFC 5506
1475 // a=rtcp-rsize
1476 if (media_desc->rtcp_reduced_size()) {
1477 InitAttrLine(kAttributeRtcpReducedSize, &os);
1478 AddLine(os.str(), message);
1479 }
1480
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001481 // RFC 4568
1482 // a=crypto:<tag> <crypto-suite> <key-params> [<session-params>]
1483 for (std::vector<CryptoParams>::const_iterator it =
1484 media_desc->cryptos().begin();
1485 it != media_desc->cryptos().end(); ++it) {
1486 InitAttrLine(kAttributeCrypto, &os);
1487 os << kSdpDelimiterColon << it->tag << " " << it->cipher_suite << " "
1488 << it->key_params;
1489 if (!it->session_params.empty()) {
1490 os << " " << it->session_params;
1491 }
1492 AddLine(os.str(), message);
1493 }
1494
1495 // RFC 4566
1496 // a=rtpmap:<payload type> <encoding name>/<clock rate>
1497 // [/<encodingparameters>]
1498 BuildRtpMap(media_desc, media_type, message);
1499
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001500 for (StreamParamsVec::const_iterator track = media_desc->streams().begin();
1501 track != media_desc->streams().end(); ++track) {
1502 // Require that the track belongs to a media stream,
1503 // ie the sync_label is set. This extra check is necessary since the
1504 // MediaContentDescription always contains a streamparam with an ssrc even
1505 // if no track or media stream have been created.
1506 if (track->sync_label.empty()) continue;
1507
1508 // Build the ssrc-group lines.
1509 for (size_t i = 0; i < track->ssrc_groups.size(); ++i) {
1510 // RFC 5576
1511 // a=ssrc-group:<semantics> <ssrc-id> ...
1512 if (track->ssrc_groups[i].ssrcs.empty()) {
1513 continue;
1514 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001515 InitAttrLine(kAttributeSsrcGroup, &os);
1516 os << kSdpDelimiterColon << track->ssrc_groups[i].semantics;
Peter Boström0c4e06b2015-10-07 12:23:21 +02001517 std::vector<uint32_t>::const_iterator ssrc =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001518 track->ssrc_groups[i].ssrcs.begin();
1519 for (; ssrc != track->ssrc_groups[i].ssrcs.end(); ++ssrc) {
Peter Boström0c4e06b2015-10-07 12:23:21 +02001520 os << kSdpDelimiterSpace << rtc::ToString<uint32_t>(*ssrc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001521 }
1522 AddLine(os.str(), message);
1523 }
1524 // Build the ssrc lines for each ssrc.
1525 for (size_t i = 0; i < track->ssrcs.size(); ++i) {
Peter Boström0c4e06b2015-10-07 12:23:21 +02001526 uint32_t ssrc = track->ssrcs[i];
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001527 // RFC 5576
1528 // a=ssrc:<ssrc-id> cname:<value>
1529 AddSsrcLine(ssrc, kSsrcAttributeCname,
1530 track->cname, message);
1531
1532 // draft-alvestrand-mmusic-msid-00
1533 // a=ssrc:<ssrc-id> msid:identifier [appdata]
deadbeef9d3584c2016-02-16 17:54:10 -08001534 // The appdata consists of the "id" attribute of a MediaStreamTrack,
1535 // which corresponds to the "id" attribute of StreamParams.
1536 const std::string& stream_id = track->sync_label;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001537 InitAttrLine(kAttributeSsrc, &os);
1538 os << kSdpDelimiterColon << ssrc << kSdpDelimiterSpace
deadbeef9d3584c2016-02-16 17:54:10 -08001539 << kSsrcAttributeMsid << kSdpDelimiterColon << stream_id
1540 << kSdpDelimiterSpace << track->id;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001541 AddLine(os.str(), message);
1542
deadbeef9d3584c2016-02-16 17:54:10 -08001543 // TODO(ronghuawu): Remove below code which is for backward
1544 // compatibility.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001545 // draft-alvestrand-rtcweb-mid-01
1546 // a=ssrc:<ssrc-id> mslabel:<value>
1547 // The label isn't yet defined.
1548 // a=ssrc:<ssrc-id> label:<value>
1549 AddSsrcLine(ssrc, kSsrcAttributeMslabel, track->sync_label, message);
1550 AddSsrcLine(ssrc, kSSrcAttributeLabel, track->id, message);
1551 }
1552 }
1553}
1554
1555void WriteFmtpHeader(int payload_type, std::ostringstream* os) {
1556 // fmtp header: a=fmtp:|payload_type| <parameters>
1557 // Add a=fmtp
1558 InitAttrLine(kAttributeFmtp, os);
1559 // Add :|payload_type|
1560 *os << kSdpDelimiterColon << payload_type;
1561}
1562
1563void WriteRtcpFbHeader(int payload_type, std::ostringstream* os) {
1564 // rtcp-fb header: a=rtcp-fb:|payload_type|
1565 // <parameters>/<ccm <ccm_parameters>>
1566 // Add a=rtcp-fb
1567 InitAttrLine(kAttributeRtcpFb, os);
1568 // Add :
1569 *os << kSdpDelimiterColon;
1570 if (payload_type == kWildcardPayloadType) {
1571 *os << "*";
1572 } else {
1573 *os << payload_type;
1574 }
1575}
1576
1577void WriteFmtpParameter(const std::string& parameter_name,
1578 const std::string& parameter_value,
1579 std::ostringstream* os) {
1580 // fmtp parameters: |parameter_name|=|parameter_value|
1581 *os << parameter_name << kSdpDelimiterEqual << parameter_value;
1582}
1583
1584void WriteFmtpParameters(const cricket::CodecParameterMap& parameters,
1585 std::ostringstream* os) {
1586 for (cricket::CodecParameterMap::const_iterator fmtp = parameters.begin();
1587 fmtp != parameters.end(); ++fmtp) {
hta62a216e2016-04-15 11:02:14 -07001588 // Parameters are a semicolon-separated list, no spaces.
1589 // The list is separated from the header by a space.
1590 if (fmtp == parameters.begin()) {
1591 *os << kSdpDelimiterSpace;
1592 } else {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001593 *os << kSdpDelimiterSemicolon;
1594 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001595 WriteFmtpParameter(fmtp->first, fmtp->second, os);
1596 }
1597}
1598
1599bool IsFmtpParam(const std::string& name) {
ossuaa4b0772017-01-30 07:41:18 -08001600 // RFC 4855, section 3 specifies the mapping of media format parameters to SDP
1601 // parameters. Only ptime, maxptime, channels and rate are placed outside of
1602 // the fmtp line. In WebRTC, channels and rate are already handled separately
1603 // and thus not included in the CodecParameterMap.
1604 return name != kCodecParamPTime && name != kCodecParamMaxPTime;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001605}
1606
1607// Retreives fmtp parameters from |params|, which may contain other parameters
1608// as well, and puts them in |fmtp_parameters|.
1609void GetFmtpParams(const cricket::CodecParameterMap& params,
1610 cricket::CodecParameterMap* fmtp_parameters) {
1611 for (cricket::CodecParameterMap::const_iterator iter = params.begin();
1612 iter != params.end(); ++iter) {
1613 if (IsFmtpParam(iter->first)) {
1614 (*fmtp_parameters)[iter->first] = iter->second;
1615 }
1616 }
1617}
1618
1619template <class T>
1620void AddFmtpLine(const T& codec, std::string* message) {
1621 cricket::CodecParameterMap fmtp_parameters;
1622 GetFmtpParams(codec.params, &fmtp_parameters);
1623 if (fmtp_parameters.empty()) {
1624 // No need to add an fmtp if it will have no (optional) parameters.
1625 return;
1626 }
1627 std::ostringstream os;
1628 WriteFmtpHeader(codec.id, &os);
1629 WriteFmtpParameters(fmtp_parameters, &os);
1630 AddLine(os.str(), message);
1631 return;
1632}
1633
1634template <class T>
1635void AddRtcpFbLines(const T& codec, std::string* message) {
1636 for (std::vector<cricket::FeedbackParam>::const_iterator iter =
1637 codec.feedback_params.params().begin();
1638 iter != codec.feedback_params.params().end(); ++iter) {
1639 std::ostringstream os;
1640 WriteRtcpFbHeader(codec.id, &os);
1641 os << " " << iter->id();
1642 if (!iter->param().empty()) {
1643 os << " " << iter->param();
1644 }
1645 AddLine(os.str(), message);
1646 }
1647}
1648
jiayl@webrtc.orgddb85ab2014-09-05 16:31:56 +00001649bool AddSctpDataCodec(DataContentDescription* media_desc,
1650 int sctp_port) {
solenberg9fa49752016-10-08 13:02:44 -07001651 for (const auto& codec : media_desc->codecs()) {
1652 if (cricket::CodecNamesEq(codec.name, cricket::kGoogleSctpDataCodecName)) {
1653 return ParseFailed("",
1654 "Can't have multiple sctp port attributes.",
1655 NULL);
1656 }
jiayl@webrtc.orgddb85ab2014-09-05 16:31:56 +00001657 }
1658 // Add the SCTP Port number as a pseudo-codec "port" parameter
solenberg9fa49752016-10-08 13:02:44 -07001659 cricket::DataCodec codec_port(cricket::kGoogleSctpDataCodecPlType,
deadbeef67cf2c12016-04-13 10:07:16 -07001660 cricket::kGoogleSctpDataCodecName);
jiayl@webrtc.orgddb85ab2014-09-05 16:31:56 +00001661 codec_port.SetParam(cricket::kCodecParamPort, sctp_port);
1662 LOG(INFO) << "AddSctpDataCodec: Got SCTP Port Number "
1663 << sctp_port;
1664 media_desc->AddCodec(codec_port);
1665 return true;
1666}
1667
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001668bool GetMinValue(const std::vector<int>& values, int* value) {
1669 if (values.empty()) {
1670 return false;
1671 }
1672 std::vector<int>::const_iterator found =
1673 std::min_element(values.begin(), values.end());
1674 *value = *found;
1675 return true;
1676}
1677
1678bool GetParameter(const std::string& name,
1679 const cricket::CodecParameterMap& params, int* value) {
1680 std::map<std::string, std::string>::const_iterator found =
1681 params.find(name);
1682 if (found == params.end()) {
1683 return false;
1684 }
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001685 if (!rtc::FromString(found->second, value)) {
wu@webrtc.org5e760e72014-04-02 23:19:09 +00001686 return false;
1687 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001688 return true;
1689}
1690
1691void BuildRtpMap(const MediaContentDescription* media_desc,
1692 const MediaType media_type,
1693 std::string* message) {
nisseede5da42017-01-12 05:15:36 -08001694 RTC_DCHECK(message != NULL);
1695 RTC_DCHECK(media_desc != NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001696 std::ostringstream os;
1697 if (media_type == cricket::MEDIA_TYPE_VIDEO) {
1698 const VideoContentDescription* video_desc =
1699 static_cast<const VideoContentDescription*>(media_desc);
1700 for (std::vector<cricket::VideoCodec>::const_iterator it =
1701 video_desc->codecs().begin();
1702 it != video_desc->codecs().end(); ++it) {
1703 // RFC 4566
1704 // a=rtpmap:<payload type> <encoding name>/<clock rate>
1705 // [/<encodingparameters>]
1706 if (it->id != kWildcardPayloadType) {
1707 InitAttrLine(kAttributeRtpmap, &os);
1708 os << kSdpDelimiterColon << it->id << " " << it->name
1709 << "/" << kDefaultVideoClockrate;
1710 AddLine(os.str(), message);
1711 }
1712 AddRtcpFbLines(*it, message);
1713 AddFmtpLine(*it, message);
1714 }
1715 } else if (media_type == cricket::MEDIA_TYPE_AUDIO) {
1716 const AudioContentDescription* audio_desc =
1717 static_cast<const AudioContentDescription*>(media_desc);
1718 std::vector<int> ptimes;
1719 std::vector<int> maxptimes;
1720 int max_minptime = 0;
1721 for (std::vector<cricket::AudioCodec>::const_iterator it =
1722 audio_desc->codecs().begin();
1723 it != audio_desc->codecs().end(); ++it) {
nisseede5da42017-01-12 05:15:36 -08001724 RTC_DCHECK(!it->name.empty());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001725 // RFC 4566
1726 // a=rtpmap:<payload type> <encoding name>/<clock rate>
1727 // [/<encodingparameters>]
1728 InitAttrLine(kAttributeRtpmap, &os);
1729 os << kSdpDelimiterColon << it->id << " ";
1730 os << it->name << "/" << it->clockrate;
1731 if (it->channels != 1) {
1732 os << "/" << it->channels;
1733 }
1734 AddLine(os.str(), message);
1735 AddRtcpFbLines(*it, message);
1736 AddFmtpLine(*it, message);
1737 int minptime = 0;
1738 if (GetParameter(kCodecParamMinPTime, it->params, &minptime)) {
1739 max_minptime = std::max(minptime, max_minptime);
1740 }
1741 int ptime;
1742 if (GetParameter(kCodecParamPTime, it->params, &ptime)) {
1743 ptimes.push_back(ptime);
1744 }
1745 int maxptime;
1746 if (GetParameter(kCodecParamMaxPTime, it->params, &maxptime)) {
1747 maxptimes.push_back(maxptime);
1748 }
1749 }
1750 // Populate the maxptime attribute with the smallest maxptime of all codecs
1751 // under the same m-line.
1752 int min_maxptime = INT_MAX;
1753 if (GetMinValue(maxptimes, &min_maxptime)) {
1754 AddAttributeLine(kCodecParamMaxPTime, min_maxptime, message);
1755 }
nisseede5da42017-01-12 05:15:36 -08001756 RTC_DCHECK(min_maxptime > max_minptime);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001757 // Populate the ptime attribute with the smallest ptime or the largest
1758 // minptime, whichever is the largest, for all codecs under the same m-line.
1759 int ptime = INT_MAX;
1760 if (GetMinValue(ptimes, &ptime)) {
1761 ptime = std::min(ptime, min_maxptime);
1762 ptime = std::max(ptime, max_minptime);
1763 AddAttributeLine(kCodecParamPTime, ptime, message);
1764 }
1765 } else if (media_type == cricket::MEDIA_TYPE_DATA) {
1766 const DataContentDescription* data_desc =
1767 static_cast<const DataContentDescription*>(media_desc);
1768 for (std::vector<cricket::DataCodec>::const_iterator it =
1769 data_desc->codecs().begin();
1770 it != data_desc->codecs().end(); ++it) {
1771 // RFC 4566
1772 // a=rtpmap:<payload type> <encoding name>/<clock rate>
1773 // [/<encodingparameters>]
1774 InitAttrLine(kAttributeRtpmap, &os);
1775 os << kSdpDelimiterColon << it->id << " "
1776 << it->name << "/" << it->clockrate;
1777 AddLine(os.str(), message);
1778 }
1779 }
1780}
1781
1782void BuildCandidate(const std::vector<Candidate>& candidates,
honghaiza54a0802015-12-16 18:37:23 -08001783 bool include_ufrag,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001784 std::string* message) {
1785 std::ostringstream os;
1786
1787 for (std::vector<Candidate>::const_iterator it = candidates.begin();
1788 it != candidates.end(); ++it) {
1789 // RFC 5245
1790 // a=candidate:<foundation> <component-id> <transport> <priority>
1791 // <connection-address> <port> typ <candidate-types>
1792 // [raddr <connection-address>] [rport <port>]
1793 // *(SP extension-att-name SP extension-att-value)
1794 std::string type;
1795 // Map the cricket candidate type to "host" / "srflx" / "prflx" / "relay"
1796 if (it->type() == cricket::LOCAL_PORT_TYPE) {
1797 type = kCandidateHost;
1798 } else if (it->type() == cricket::STUN_PORT_TYPE) {
1799 type = kCandidateSrflx;
1800 } else if (it->type() == cricket::RELAY_PORT_TYPE) {
1801 type = kCandidateRelay;
Honghai Zhang7fb69db2016-03-14 11:59:18 -07001802 } else if (it->type() == cricket::PRFLX_PORT_TYPE) {
1803 type = kCandidatePrflx;
1804 // Peer reflexive candidate may be signaled for being removed.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001805 } else {
nissec80e7412017-01-11 05:56:46 -08001806 RTC_NOTREACHED();
Peter Thatcher019087f2015-04-28 09:06:26 -07001807 // Never write out candidates if we don't know the type.
1808 continue;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001809 }
1810
1811 InitAttrLine(kAttributeCandidate, &os);
1812 os << kSdpDelimiterColon
mallinath@webrtc.org2d60c5e2014-08-08 22:29:20 +00001813 << it->foundation() << " "
1814 << it->component() << " "
1815 << it->protocol() << " "
1816 << it->priority() << " "
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001817 << it->address().ipaddr().ToString() << " "
1818 << it->address().PortAsString() << " "
mallinath@webrtc.org2d60c5e2014-08-08 22:29:20 +00001819 << kAttributeCandidateTyp << " "
1820 << type << " ";
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001821
1822 // Related address
1823 if (!it->related_address().IsNil()) {
1824 os << kAttributeCandidateRaddr << " "
1825 << it->related_address().ipaddr().ToString() << " "
1826 << kAttributeCandidateRport << " "
1827 << it->related_address().PortAsString() << " ";
1828 }
1829
mallinath@webrtc.org2d60c5e2014-08-08 22:29:20 +00001830 if (it->protocol() == cricket::TCP_PROTOCOL_NAME) {
mallinath@webrtc.orge999bd02014-08-13 06:05:55 +00001831 os << kTcpCandidateType << " " << it->tcptype() << " ";
mallinath@webrtc.org2d60c5e2014-08-08 22:29:20 +00001832 }
1833
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001834 // Extensions
1835 os << kAttributeCandidateGeneration << " " << it->generation();
honghaiza54a0802015-12-16 18:37:23 -08001836 if (include_ufrag && !it->username().empty()) {
1837 os << " " << kAttributeCandidateUfrag << " " << it->username();
1838 }
honghaiza0c44ea2016-03-23 16:07:48 -07001839 if (it->network_id() > 0) {
1840 os << " " << kAttributeCandidateNetworkId << " " << it->network_id();
1841 }
honghaize1a0c942016-02-16 14:54:56 -08001842 if (it->network_cost() > 0) {
1843 os << " " << kAttributeCandidateNetworkCost << " " << it->network_cost();
1844 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001845
1846 AddLine(os.str(), message);
1847 }
1848}
1849
1850void BuildIceOptions(const std::vector<std::string>& transport_options,
1851 std::string* message) {
1852 if (!transport_options.empty()) {
1853 std::ostringstream os;
1854 InitAttrLine(kAttributeIceOption, &os);
1855 os << kSdpDelimiterColon << transport_options[0];
1856 for (size_t i = 1; i < transport_options.size(); ++i) {
1857 os << kSdpDelimiterSpace << transport_options[i];
1858 }
1859 AddLine(os.str(), message);
1860 }
1861}
1862
pthatcher@webrtc.org3341b402015-02-13 21:14:22 +00001863bool IsRtp(const std::string& protocol) {
1864 return protocol.empty() ||
1865 (protocol.find(cricket::kMediaProtocolRtpPrefix) != std::string::npos);
1866}
1867
1868bool IsDtlsSctp(const std::string& protocol) {
1869 // This intentionally excludes "SCTP" and "SCTP/DTLS".
lally@webrtc.orga7470932015-02-24 20:19:21 +00001870 return protocol.find(cricket::kMediaProtocolDtlsSctp) != std::string::npos;
pthatcher@webrtc.org3341b402015-02-13 21:14:22 +00001871}
1872
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001873bool ParseSessionDescription(const std::string& message, size_t* pos,
1874 std::string* session_id,
1875 std::string* session_version,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001876 TransportDescription* session_td,
1877 RtpHeaderExtensions* session_extmaps,
1878 cricket::SessionDescription* desc,
1879 SdpParseError* error) {
1880 std::string line;
1881
deadbeefc80741f2015-10-22 13:14:45 -07001882 desc->set_msid_supported(false);
1883
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001884 // RFC 4566
1885 // v= (protocol version)
1886 if (!GetLineWithType(message, pos, &line, kLineTypeVersion)) {
1887 return ParseFailedExpectLine(message, *pos, kLineTypeVersion,
1888 std::string(), error);
1889 }
1890 // RFC 4566
1891 // o=<username> <sess-id> <sess-version> <nettype> <addrtype>
1892 // <unicast-address>
1893 if (!GetLineWithType(message, pos, &line, kLineTypeOrigin)) {
1894 return ParseFailedExpectLine(message, *pos, kLineTypeOrigin,
1895 std::string(), error);
1896 }
1897 std::vector<std::string> fields;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001898 rtc::split(line.substr(kLinePrefixLength),
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001899 kSdpDelimiterSpace, &fields);
1900 const size_t expected_fields = 6;
1901 if (fields.size() != expected_fields) {
1902 return ParseFailedExpectFieldNum(line, expected_fields, error);
1903 }
1904 *session_id = fields[1];
1905 *session_version = fields[2];
1906
1907 // RFC 4566
1908 // s= (session name)
1909 if (!GetLineWithType(message, pos, &line, kLineTypeSessionName)) {
1910 return ParseFailedExpectLine(message, *pos, kLineTypeSessionName,
1911 std::string(), error);
1912 }
1913
1914 // Optional lines
1915 // Those are the optional lines, so shouldn't return false if not present.
1916 // RFC 4566
1917 // i=* (session information)
1918 GetLineWithType(message, pos, &line, kLineTypeSessionInfo);
1919
1920 // RFC 4566
1921 // u=* (URI of description)
1922 GetLineWithType(message, pos, &line, kLineTypeSessionUri);
1923
1924 // RFC 4566
1925 // e=* (email address)
1926 GetLineWithType(message, pos, &line, kLineTypeSessionEmail);
1927
1928 // RFC 4566
1929 // p=* (phone number)
1930 GetLineWithType(message, pos, &line, kLineTypeSessionPhone);
1931
1932 // RFC 4566
1933 // c=* (connection information -- not required if included in
1934 // all media)
1935 GetLineWithType(message, pos, &line, kLineTypeConnection);
1936
1937 // RFC 4566
1938 // b=* (zero or more bandwidth information lines)
1939 while (GetLineWithType(message, pos, &line, kLineTypeSessionBandwidth)) {
1940 // By pass zero or more b lines.
1941 }
1942
1943 // RFC 4566
1944 // One or more time descriptions ("t=" and "r=" lines; see below)
1945 // t= (time the session is active)
1946 // r=* (zero or more repeat times)
1947 // Ensure there's at least one time description
1948 if (!GetLineWithType(message, pos, &line, kLineTypeTiming)) {
1949 return ParseFailedExpectLine(message, *pos, kLineTypeTiming, std::string(),
1950 error);
1951 }
1952
1953 while (GetLineWithType(message, pos, &line, kLineTypeRepeatTimes)) {
1954 // By pass zero or more r lines.
1955 }
1956
1957 // Go through the rest of the time descriptions
1958 while (GetLineWithType(message, pos, &line, kLineTypeTiming)) {
1959 while (GetLineWithType(message, pos, &line, kLineTypeRepeatTimes)) {
1960 // By pass zero or more r lines.
1961 }
1962 }
1963
1964 // RFC 4566
1965 // z=* (time zone adjustments)
1966 GetLineWithType(message, pos, &line, kLineTypeTimeZone);
1967
1968 // RFC 4566
1969 // k=* (encryption key)
1970 GetLineWithType(message, pos, &line, kLineTypeEncryptionKey);
1971
1972 // RFC 4566
1973 // a=* (zero or more session attribute lines)
1974 while (GetLineWithType(message, pos, &line, kLineTypeAttributes)) {
1975 if (HasAttribute(line, kAttributeGroup)) {
1976 if (!ParseGroupAttribute(line, desc, error)) {
1977 return false;
1978 }
1979 } else if (HasAttribute(line, kAttributeIceUfrag)) {
1980 if (!GetValue(line, kAttributeIceUfrag,
1981 &(session_td->ice_ufrag), error)) {
1982 return false;
1983 }
1984 } else if (HasAttribute(line, kAttributeIcePwd)) {
1985 if (!GetValue(line, kAttributeIcePwd, &(session_td->ice_pwd), error)) {
1986 return false;
1987 }
1988 } else if (HasAttribute(line, kAttributeIceLite)) {
1989 session_td->ice_mode = cricket::ICEMODE_LITE;
1990 } else if (HasAttribute(line, kAttributeIceOption)) {
1991 if (!ParseIceOptions(line, &(session_td->transport_options), error)) {
1992 return false;
1993 }
1994 } else if (HasAttribute(line, kAttributeFingerprint)) {
1995 if (session_td->identity_fingerprint.get()) {
1996 return ParseFailed(
1997 line,
1998 "Can't have multiple fingerprint attributes at the same level.",
1999 error);
2000 }
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002001 rtc::SSLFingerprint* fingerprint = NULL;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002002 if (!ParseFingerprintAttribute(line, &fingerprint, error)) {
2003 return false;
2004 }
2005 session_td->identity_fingerprint.reset(fingerprint);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00002006 } else if (HasAttribute(line, kAttributeSetup)) {
2007 if (!ParseDtlsSetup(line, &(session_td->connection_role), error)) {
2008 return false;
2009 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002010 } else if (HasAttribute(line, kAttributeMsidSemantics)) {
2011 std::string semantics;
2012 if (!GetValue(line, kAttributeMsidSemantics, &semantics, error)) {
2013 return false;
2014 }
deadbeefc80741f2015-10-22 13:14:45 -07002015 desc->set_msid_supported(
2016 CaseInsensitiveFind(semantics, kMediaStreamSemantic));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002017 } else if (HasAttribute(line, kAttributeExtmap)) {
isheriff6f8d6862016-05-26 11:24:55 -07002018 RtpExtension extmap;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002019 if (!ParseExtmap(line, &extmap, error)) {
2020 return false;
2021 }
2022 session_extmaps->push_back(extmap);
2023 }
2024 }
2025
2026 return true;
2027}
2028
2029bool ParseGroupAttribute(const std::string& line,
2030 cricket::SessionDescription* desc,
2031 SdpParseError* error) {
nisseede5da42017-01-12 05:15:36 -08002032 RTC_DCHECK(desc != NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002033
2034 // RFC 5888 and draft-holmberg-mmusic-sdp-bundle-negotiation-00
2035 // a=group:BUNDLE video voice
2036 std::vector<std::string> fields;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002037 rtc::split(line.substr(kLinePrefixLength),
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002038 kSdpDelimiterSpace, &fields);
2039 std::string semantics;
2040 if (!GetValue(fields[0], kAttributeGroup, &semantics, error)) {
2041 return false;
2042 }
2043 cricket::ContentGroup group(semantics);
2044 for (size_t i = 1; i < fields.size(); ++i) {
2045 group.AddContentName(fields[i]);
2046 }
2047 desc->AddGroup(group);
2048 return true;
2049}
2050
2051static bool ParseFingerprintAttribute(const std::string& line,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002052 rtc::SSLFingerprint** fingerprint,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002053 SdpParseError* error) {
2054 if (!IsLineType(line, kLineTypeAttributes) ||
2055 !HasAttribute(line, kAttributeFingerprint)) {
2056 return ParseFailedExpectLine(line, 0, kLineTypeAttributes,
2057 kAttributeFingerprint, error);
2058 }
2059
2060 std::vector<std::string> fields;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002061 rtc::split(line.substr(kLinePrefixLength),
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002062 kSdpDelimiterSpace, &fields);
2063 const size_t expected_fields = 2;
2064 if (fields.size() != expected_fields) {
2065 return ParseFailedExpectFieldNum(line, expected_fields, error);
2066 }
2067
2068 // The first field here is "fingerprint:<hash>.
2069 std::string algorithm;
2070 if (!GetValue(fields[0], kAttributeFingerprint, &algorithm, error)) {
2071 return false;
2072 }
2073
2074 // Downcase the algorithm. Note that we don't need to downcase the
2075 // fingerprint because hex_decode can handle upper-case.
2076 std::transform(algorithm.begin(), algorithm.end(), algorithm.begin(),
2077 ::tolower);
2078
2079 // The second field is the digest value. De-hexify it.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002080 *fingerprint = rtc::SSLFingerprint::CreateFromRfc4572(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002081 algorithm, fields[1]);
2082 if (!*fingerprint) {
2083 return ParseFailed(line,
2084 "Failed to create fingerprint from the digest.",
2085 error);
2086 }
2087
2088 return true;
2089}
2090
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00002091static bool ParseDtlsSetup(const std::string& line,
2092 cricket::ConnectionRole* role,
2093 SdpParseError* error) {
2094 // setup-attr = "a=setup:" role
2095 // role = "active" / "passive" / "actpass" / "holdconn"
2096 std::vector<std::string> fields;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002097 rtc::split(line.substr(kLinePrefixLength), kSdpDelimiterColon, &fields);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00002098 const size_t expected_fields = 2;
2099 if (fields.size() != expected_fields) {
2100 return ParseFailedExpectFieldNum(line, expected_fields, error);
2101 }
2102 std::string role_str = fields[1];
2103 if (!cricket::StringToConnectionRole(role_str, role)) {
2104 return ParseFailed(line, "Invalid attribute value.", error);
2105 }
2106 return true;
2107}
2108
deadbeef9d3584c2016-02-16 17:54:10 -08002109static bool ParseMsidAttribute(const std::string& line,
2110 std::string* stream_id,
2111 std::string* track_id,
2112 SdpParseError* error) {
2113 // draft-ietf-mmusic-msid-11
2114 // a=msid:<stream id> <track id>
2115 // msid-value = msid-id [ SP msid-appdata ]
2116 // msid-id = 1*64token-char ; see RFC 4566
2117 // msid-appdata = 1*64token-char ; see RFC 4566
2118 std::string field1;
2119 if (!rtc::tokenize_first(line.substr(kLinePrefixLength), kSdpDelimiterSpace,
2120 &field1, track_id)) {
2121 const size_t expected_fields = 2;
2122 return ParseFailedExpectFieldNum(line, expected_fields, error);
2123 }
2124
2125 // msid:<msid-id>
2126 if (!GetValue(field1, kAttributeMsid, stream_id, error)) {
2127 return false;
2128 }
2129 return true;
2130}
2131
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002132// RFC 3551
2133// PT encoding media type clock rate channels
2134// name (Hz)
2135// 0 PCMU A 8,000 1
2136// 1 reserved A
2137// 2 reserved A
2138// 3 GSM A 8,000 1
2139// 4 G723 A 8,000 1
2140// 5 DVI4 A 8,000 1
2141// 6 DVI4 A 16,000 1
2142// 7 LPC A 8,000 1
2143// 8 PCMA A 8,000 1
2144// 9 G722 A 8,000 1
2145// 10 L16 A 44,100 2
2146// 11 L16 A 44,100 1
2147// 12 QCELP A 8,000 1
2148// 13 CN A 8,000 1
2149// 14 MPA A 90,000 (see text)
2150// 15 G728 A 8,000 1
2151// 16 DVI4 A 11,025 1
2152// 17 DVI4 A 22,050 1
2153// 18 G729 A 8,000 1
2154struct StaticPayloadAudioCodec {
2155 const char* name;
2156 int clockrate;
Peter Kasting69558702016-01-12 16:26:35 -08002157 size_t channels;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002158};
2159static const StaticPayloadAudioCodec kStaticPayloadAudioCodecs[] = {
2160 { "PCMU", 8000, 1 },
2161 { "reserved", 0, 0 },
2162 { "reserved", 0, 0 },
2163 { "GSM", 8000, 1 },
2164 { "G723", 8000, 1 },
2165 { "DVI4", 8000, 1 },
2166 { "DVI4", 16000, 1 },
2167 { "LPC", 8000, 1 },
2168 { "PCMA", 8000, 1 },
2169 { "G722", 8000, 1 },
2170 { "L16", 44100, 2 },
2171 { "L16", 44100, 1 },
2172 { "QCELP", 8000, 1 },
2173 { "CN", 8000, 1 },
2174 { "MPA", 90000, 1 },
2175 { "G728", 8000, 1 },
2176 { "DVI4", 11025, 1 },
2177 { "DVI4", 22050, 1 },
2178 { "G729", 8000, 1 },
2179};
2180
2181void MaybeCreateStaticPayloadAudioCodecs(
2182 const std::vector<int>& fmts, AudioContentDescription* media_desc) {
2183 if (!media_desc) {
2184 return;
2185 }
deadbeef67cf2c12016-04-13 10:07:16 -07002186 RTC_DCHECK(media_desc->codecs().empty());
solenberg9fa49752016-10-08 13:02:44 -07002187 for (int payload_type : fmts) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002188 if (!media_desc->HasCodec(payload_type) &&
2189 payload_type >= 0 &&
kjellander3e33bfe2016-06-20 07:04:09 -07002190 static_cast<uint32_t>(payload_type) <
2191 arraysize(kStaticPayloadAudioCodecs)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002192 std::string encoding_name = kStaticPayloadAudioCodecs[payload_type].name;
2193 int clock_rate = kStaticPayloadAudioCodecs[payload_type].clockrate;
Peter Kasting69558702016-01-12 16:26:35 -08002194 size_t channels = kStaticPayloadAudioCodecs[payload_type].channels;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002195 media_desc->AddCodec(cricket::AudioCodec(payload_type, encoding_name,
deadbeef67cf2c12016-04-13 10:07:16 -07002196 clock_rate, 0, channels));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002197 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002198 }
2199}
2200
2201template <class C>
2202static C* ParseContentDescription(const std::string& message,
2203 const MediaType media_type,
2204 int mline_index,
2205 const std::string& protocol,
deadbeef67cf2c12016-04-13 10:07:16 -07002206 const std::vector<int>& payload_types,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002207 size_t* pos,
2208 std::string* content_name,
deadbeef25ed4352016-12-12 18:37:36 -08002209 bool* bundle_only,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002210 TransportDescription* transport,
2211 std::vector<JsepIceCandidate*>* candidates,
2212 webrtc::SdpParseError* error) {
2213 C* media_desc = new C();
2214 switch (media_type) {
2215 case cricket::MEDIA_TYPE_AUDIO:
2216 *content_name = cricket::CN_AUDIO;
2217 break;
2218 case cricket::MEDIA_TYPE_VIDEO:
2219 *content_name = cricket::CN_VIDEO;
2220 break;
2221 case cricket::MEDIA_TYPE_DATA:
2222 *content_name = cricket::CN_DATA;
2223 break;
2224 default:
nissec80e7412017-01-11 05:56:46 -08002225 RTC_NOTREACHED();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002226 break;
2227 }
deadbeef67cf2c12016-04-13 10:07:16 -07002228 if (!ParseContent(message, media_type, mline_index, protocol, payload_types,
deadbeef25ed4352016-12-12 18:37:36 -08002229 pos, content_name, bundle_only, media_desc, transport,
2230 candidates, error)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002231 delete media_desc;
2232 return NULL;
2233 }
2234 // Sort the codecs according to the m-line fmt list.
deadbeef67cf2c12016-04-13 10:07:16 -07002235 std::unordered_map<int, int> payload_type_preferences;
2236 // "size + 1" so that the lowest preference payload type has a preference of
2237 // 1, which is greater than the default (0) for payload types not in the fmt
2238 // list.
2239 int preference = static_cast<int>(payload_types.size() + 1);
2240 for (int pt : payload_types) {
2241 payload_type_preferences[pt] = preference--;
2242 }
2243 std::vector<typename C::CodecType> codecs = media_desc->codecs();
2244 std::sort(codecs.begin(), codecs.end(), [&payload_type_preferences](
2245 const typename C::CodecType& a,
2246 const typename C::CodecType& b) {
2247 return payload_type_preferences[a.id] > payload_type_preferences[b.id];
2248 });
2249 media_desc->set_codecs(codecs);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002250 return media_desc;
2251}
2252
2253bool ParseMediaDescription(const std::string& message,
2254 const TransportDescription& session_td,
2255 const RtpHeaderExtensions& session_extmaps,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002256 size_t* pos,
2257 cricket::SessionDescription* desc,
2258 std::vector<JsepIceCandidate*>* candidates,
2259 SdpParseError* error) {
nisseede5da42017-01-12 05:15:36 -08002260 RTC_DCHECK(desc != NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002261 std::string line;
2262 int mline_index = -1;
2263
2264 // Zero or more media descriptions
2265 // RFC 4566
2266 // m=<media> <port> <proto> <fmt>
2267 while (GetLineWithType(message, pos, &line, kLineTypeMedia)) {
2268 ++mline_index;
2269
2270 std::vector<std::string> fields;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002271 rtc::split(line.substr(kLinePrefixLength),
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002272 kSdpDelimiterSpace, &fields);
2273 const size_t expected_min_fields = 4;
2274 if (fields.size() < expected_min_fields) {
2275 return ParseFailedExpectMinFieldNum(line, expected_min_fields, error);
2276 }
deadbeef25ed4352016-12-12 18:37:36 -08002277 bool port_rejected = false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002278 // RFC 3264
2279 // To reject an offered stream, the port number in the corresponding stream
2280 // in the answer MUST be set to zero.
2281 if (fields[1] == kMediaPortRejected) {
deadbeef25ed4352016-12-12 18:37:36 -08002282 port_rejected = true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002283 }
2284
2285 std::string protocol = fields[2];
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002286
2287 // <fmt>
deadbeef67cf2c12016-04-13 10:07:16 -07002288 std::vector<int> payload_types;
pthatcher@webrtc.org3341b402015-02-13 21:14:22 +00002289 if (IsRtp(protocol)) {
jiayl@webrtc.orgddb85ab2014-09-05 16:31:56 +00002290 for (size_t j = 3 ; j < fields.size(); ++j) {
2291 // TODO(wu): Remove when below bug is fixed.
2292 // https://bugzilla.mozilla.org/show_bug.cgi?id=996329
pbosbb36fdf2015-07-09 07:48:14 -07002293 if (fields[j].empty() && j == fields.size() - 1) {
jiayl@webrtc.orgddb85ab2014-09-05 16:31:56 +00002294 continue;
2295 }
wu@webrtc.org36eda7c2014-04-15 20:37:30 +00002296
jiayl@webrtc.orgddb85ab2014-09-05 16:31:56 +00002297 int pl = 0;
pkasting@chromium.orge9facf82015-02-17 20:36:28 +00002298 if (!GetPayloadTypeFromString(line, fields[j], &pl, error)) {
jiayl@webrtc.orgddb85ab2014-09-05 16:31:56 +00002299 return false;
2300 }
deadbeef67cf2c12016-04-13 10:07:16 -07002301 payload_types.push_back(pl);
wu@webrtc.org5e760e72014-04-02 23:19:09 +00002302 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002303 }
2304
2305 // Make a temporary TransportDescription based on |session_td|.
2306 // Some of this gets overwritten by ParseContent.
deadbeef46eed762016-01-28 13:24:37 -08002307 TransportDescription transport(
2308 session_td.transport_options, session_td.ice_ufrag, session_td.ice_pwd,
2309 session_td.ice_mode, session_td.connection_role,
2310 session_td.identity_fingerprint.get());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002311
kwibergd1fe2812016-04-27 06:47:29 -07002312 std::unique_ptr<MediaContentDescription> content;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002313 std::string content_name;
deadbeef25ed4352016-12-12 18:37:36 -08002314 bool bundle_only = false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002315 if (HasAttribute(line, kMediaTypeVideo)) {
2316 content.reset(ParseContentDescription<VideoContentDescription>(
deadbeef67cf2c12016-04-13 10:07:16 -07002317 message, cricket::MEDIA_TYPE_VIDEO, mline_index, protocol,
deadbeef25ed4352016-12-12 18:37:36 -08002318 payload_types, pos, &content_name, &bundle_only, &transport,
2319 candidates, error));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002320 } else if (HasAttribute(line, kMediaTypeAudio)) {
2321 content.reset(ParseContentDescription<AudioContentDescription>(
deadbeef67cf2c12016-04-13 10:07:16 -07002322 message, cricket::MEDIA_TYPE_AUDIO, mline_index, protocol,
deadbeef25ed4352016-12-12 18:37:36 -08002323 payload_types, pos, &content_name, &bundle_only, &transport,
2324 candidates, error));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002325 } else if (HasAttribute(line, kMediaTypeData)) {
jiayl@webrtc.org0e527722014-09-08 21:43:43 +00002326 DataContentDescription* data_desc =
wu@webrtc.org78187522013-10-07 23:32:02 +00002327 ParseContentDescription<DataContentDescription>(
deadbeef67cf2c12016-04-13 10:07:16 -07002328 message, cricket::MEDIA_TYPE_DATA, mline_index, protocol,
deadbeef25ed4352016-12-12 18:37:36 -08002329 payload_types, pos, &content_name, &bundle_only, &transport,
2330 candidates, error);
jiayl@webrtc.org0e527722014-09-08 21:43:43 +00002331 content.reset(data_desc);
wu@webrtc.org78187522013-10-07 23:32:02 +00002332
jiayl@webrtc.orgddb85ab2014-09-05 16:31:56 +00002333 int p;
pthatcher@webrtc.org3341b402015-02-13 21:14:22 +00002334 if (data_desc && IsDtlsSctp(protocol) && rtc::FromString(fields[3], &p)) {
jiayl@webrtc.org0e527722014-09-08 21:43:43 +00002335 if (!AddSctpDataCodec(data_desc, p))
jiayl@webrtc.orgddb85ab2014-09-05 16:31:56 +00002336 return false;
wu@webrtc.org78187522013-10-07 23:32:02 +00002337 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002338 } else {
2339 LOG(LS_WARNING) << "Unsupported media type: " << line;
2340 continue;
2341 }
2342 if (!content.get()) {
2343 // ParseContentDescription returns NULL if failed.
2344 return false;
2345 }
2346
deadbeef25ed4352016-12-12 18:37:36 -08002347 bool content_rejected = false;
deadbeef12771a12017-01-03 13:53:47 -08002348 // A port of 0 is not interpreted as a rejected m= section when it's
2349 // used along with a=bundle-only.
deadbeef25ed4352016-12-12 18:37:36 -08002350 if (bundle_only) {
deadbeef25ed4352016-12-12 18:37:36 -08002351 if (!port_rejected) {
deadbeef12771a12017-01-03 13:53:47 -08002352 // Usage of bundle-only with a nonzero port is unspecified. So just
2353 // ignore bundle-only if we see this.
2354 bundle_only = false;
2355 LOG(LS_WARNING)
2356 << "a=bundle-only attribute observed with a nonzero "
2357 << "port; this usage is unspecified so the attribute is being "
2358 << "ignored.";
deadbeef25ed4352016-12-12 18:37:36 -08002359 }
2360 } else {
2361 // If not using bundle-only, interpret port 0 in the normal way; the m=
2362 // section is being rejected.
2363 content_rejected = port_rejected;
2364 }
2365
pthatcher@webrtc.org3341b402015-02-13 21:14:22 +00002366 if (IsRtp(protocol)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002367 // Set the extmap.
2368 if (!session_extmaps.empty() &&
2369 !content->rtp_header_extensions().empty()) {
2370 return ParseFailed("",
2371 "The a=extmap MUST be either all session level or "
2372 "all media level.",
2373 error);
2374 }
2375 for (size_t i = 0; i < session_extmaps.size(); ++i) {
2376 content->AddRtpHeaderExtension(session_extmaps[i]);
2377 }
2378 }
2379 content->set_protocol(protocol);
2380 desc->AddContent(content_name,
deadbeef25ed4352016-12-12 18:37:36 -08002381 IsDtlsSctp(protocol) ? cricket::NS_JINGLE_DRAFT_SCTP
2382 : cricket::NS_JINGLE_RTP,
2383 content_rejected, bundle_only, content.release());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002384 // Create TransportInfo with the media level "ice-pwd" and "ice-ufrag".
2385 TransportInfo transport_info(content_name, transport);
2386
2387 if (!desc->AddTransportInfo(transport_info)) {
2388 std::ostringstream description;
2389 description << "Failed to AddTransportInfo with content name: "
2390 << content_name;
2391 return ParseFailed("", description.str(), error);
2392 }
2393 }
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00002394
2395 size_t end_of_message = message.size();
2396 if (mline_index == -1 && *pos != end_of_message) {
2397 ParseFailed(message, *pos, "Expects m line.", error);
2398 return false;
2399 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002400 return true;
2401}
2402
2403bool VerifyCodec(const cricket::Codec& codec) {
2404 // Codec has not been populated correctly unless the name has been set. This
2405 // can happen if an SDP has an fmtp or rtcp-fb with a payload type but doesn't
2406 // have a corresponding "rtpmap" line.
htab39db842016-12-08 01:50:48 -08002407 return !codec.name.empty();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002408}
2409
2410bool VerifyAudioCodecs(const AudioContentDescription* audio_desc) {
2411 const std::vector<cricket::AudioCodec>& codecs = audio_desc->codecs();
2412 for (std::vector<cricket::AudioCodec>::const_iterator iter = codecs.begin();
2413 iter != codecs.end(); ++iter) {
2414 if (!VerifyCodec(*iter)) {
2415 return false;
2416 }
2417 }
2418 return true;
2419}
2420
2421bool VerifyVideoCodecs(const VideoContentDescription* video_desc) {
2422 const std::vector<cricket::VideoCodec>& codecs = video_desc->codecs();
2423 for (std::vector<cricket::VideoCodec>::const_iterator iter = codecs.begin();
2424 iter != codecs.end(); ++iter) {
2425 if (!VerifyCodec(*iter)) {
2426 return false;
2427 }
2428 }
2429 return true;
2430}
2431
2432void AddParameters(const cricket::CodecParameterMap& parameters,
2433 cricket::Codec* codec) {
2434 for (cricket::CodecParameterMap::const_iterator iter =
2435 parameters.begin(); iter != parameters.end(); ++iter) {
2436 codec->SetParam(iter->first, iter->second);
2437 }
2438}
2439
2440void AddFeedbackParameter(const cricket::FeedbackParam& feedback_param,
2441 cricket::Codec* codec) {
2442 codec->AddFeedbackParam(feedback_param);
2443}
2444
2445void AddFeedbackParameters(const cricket::FeedbackParams& feedback_params,
2446 cricket::Codec* codec) {
2447 for (std::vector<cricket::FeedbackParam>::const_iterator iter =
2448 feedback_params.params().begin();
2449 iter != feedback_params.params().end(); ++iter) {
2450 codec->AddFeedbackParam(*iter);
2451 }
2452}
2453
2454// Gets the current codec setting associated with |payload_type|. If there
changbin.shao@webrtc.org2d25b442015-03-16 04:14:34 +00002455// is no Codec associated with that payload type it returns an empty codec
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002456// with that payload type.
2457template <class T>
changbin.shao@webrtc.org2d25b442015-03-16 04:14:34 +00002458T GetCodecWithPayloadType(const std::vector<T>& codecs, int payload_type) {
magjedb05fa242016-11-11 04:00:16 -08002459 const T* codec = FindCodecById(codecs, payload_type);
2460 if (codec)
2461 return *codec;
2462 // Return empty codec with |payload_type|.
changbin.shao@webrtc.org2d25b442015-03-16 04:14:34 +00002463 T ret_val;
magjedb05fa242016-11-11 04:00:16 -08002464 ret_val.id = payload_type;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002465 return ret_val;
2466}
2467
2468// Updates or creates a new codec entry in the audio description.
2469template <class T, class U>
2470void AddOrReplaceCodec(MediaContentDescription* content_desc, const U& codec) {
2471 T* desc = static_cast<T*>(content_desc);
2472 std::vector<U> codecs = desc->codecs();
2473 bool found = false;
2474
2475 typename std::vector<U>::iterator iter;
2476 for (iter = codecs.begin(); iter != codecs.end(); ++iter) {
2477 if (iter->id == codec.id) {
2478 *iter = codec;
2479 found = true;
2480 break;
2481 }
2482 }
2483 if (!found) {
2484 desc->AddCodec(codec);
2485 return;
2486 }
2487 desc->set_codecs(codecs);
2488}
2489
2490// Adds or updates existing codec corresponding to |payload_type| according
2491// to |parameters|.
2492template <class T, class U>
2493void UpdateCodec(MediaContentDescription* content_desc, int payload_type,
2494 const cricket::CodecParameterMap& parameters) {
2495 // Codec might already have been populated (from rtpmap).
changbin.shao@webrtc.org2d25b442015-03-16 04:14:34 +00002496 U new_codec = GetCodecWithPayloadType(static_cast<T*>(content_desc)->codecs(),
2497 payload_type);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002498 AddParameters(parameters, &new_codec);
2499 AddOrReplaceCodec<T, U>(content_desc, new_codec);
2500}
2501
2502// Adds or updates existing codec corresponding to |payload_type| according
2503// to |feedback_param|.
2504template <class T, class U>
2505void UpdateCodec(MediaContentDescription* content_desc, int payload_type,
2506 const cricket::FeedbackParam& feedback_param) {
2507 // Codec might already have been populated (from rtpmap).
changbin.shao@webrtc.org2d25b442015-03-16 04:14:34 +00002508 U new_codec = GetCodecWithPayloadType(static_cast<T*>(content_desc)->codecs(),
2509 payload_type);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002510 AddFeedbackParameter(feedback_param, &new_codec);
2511 AddOrReplaceCodec<T, U>(content_desc, new_codec);
2512}
2513
jlmiller@webrtc.orga744a282015-02-18 21:37:46 +00002514template <class T>
2515bool PopWildcardCodec(std::vector<T>* codecs, T* wildcard_codec) {
2516 for (auto iter = codecs->begin(); iter != codecs->end(); ++iter) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002517 if (iter->id == kWildcardPayloadType) {
2518 *wildcard_codec = *iter;
2519 codecs->erase(iter);
2520 return true;
2521 }
2522 }
2523 return false;
2524}
2525
jlmiller@webrtc.orga744a282015-02-18 21:37:46 +00002526template<class T>
2527void UpdateFromWildcardCodecs(cricket::MediaContentDescriptionImpl<T>* desc) {
2528 auto codecs = desc->codecs();
2529 T wildcard_codec;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002530 if (!PopWildcardCodec(&codecs, &wildcard_codec)) {
2531 return;
2532 }
jlmiller@webrtc.orga744a282015-02-18 21:37:46 +00002533 for (auto& codec : codecs) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002534 AddFeedbackParameters(wildcard_codec.feedback_params, &codec);
2535 }
jlmiller@webrtc.orga744a282015-02-18 21:37:46 +00002536 desc->set_codecs(codecs);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002537}
2538
2539void AddAudioAttribute(const std::string& name, const std::string& value,
2540 AudioContentDescription* audio_desc) {
2541 if (value.empty()) {
2542 return;
2543 }
2544 std::vector<cricket::AudioCodec> codecs = audio_desc->codecs();
2545 for (std::vector<cricket::AudioCodec>::iterator iter = codecs.begin();
2546 iter != codecs.end(); ++iter) {
2547 iter->params[name] = value;
2548 }
2549 audio_desc->set_codecs(codecs);
2550}
2551
2552bool ParseContent(const std::string& message,
2553 const MediaType media_type,
2554 int mline_index,
2555 const std::string& protocol,
deadbeef67cf2c12016-04-13 10:07:16 -07002556 const std::vector<int>& payload_types,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002557 size_t* pos,
2558 std::string* content_name,
deadbeef25ed4352016-12-12 18:37:36 -08002559 bool* bundle_only,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002560 MediaContentDescription* media_desc,
2561 TransportDescription* transport,
2562 std::vector<JsepIceCandidate*>* candidates,
2563 SdpParseError* error) {
nisseede5da42017-01-12 05:15:36 -08002564 RTC_DCHECK(media_desc != NULL);
2565 RTC_DCHECK(content_name != NULL);
2566 RTC_DCHECK(transport != NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002567
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +00002568 if (media_type == cricket::MEDIA_TYPE_AUDIO) {
2569 MaybeCreateStaticPayloadAudioCodecs(
deadbeef67cf2c12016-04-13 10:07:16 -07002570 payload_types, static_cast<AudioContentDescription*>(media_desc));
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +00002571 }
2572
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002573 // The media level "ice-ufrag" and "ice-pwd".
2574 // The candidates before update the media level "ice-pwd" and "ice-ufrag".
2575 Candidates candidates_orig;
2576 std::string line;
2577 std::string mline_id;
2578 // Tracks created out of the ssrc attributes.
2579 StreamParamsVec tracks;
2580 SsrcInfoVec ssrc_infos;
2581 SsrcGroupVec ssrc_groups;
2582 std::string maxptime_as_string;
2583 std::string ptime_as_string;
deadbeef9d3584c2016-02-16 17:54:10 -08002584 std::string stream_id;
2585 std::string track_id;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002586
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002587 // Loop until the next m line
2588 while (!IsLineType(message, kLineTypeMedia, *pos)) {
2589 if (!GetLine(message, pos, &line)) {
2590 if (*pos >= message.size()) {
2591 break; // Done parsing
2592 } else {
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +00002593 return ParseFailed(message, *pos, "Invalid SDP line.", error);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002594 }
2595 }
2596
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002597 // RFC 4566
2598 // b=* (zero or more bandwidth information lines)
2599 if (IsLineType(line, kLineTypeSessionBandwidth)) {
2600 std::string bandwidth;
2601 if (HasAttribute(line, kApplicationSpecificMaximum)) {
2602 if (!GetValue(line, kApplicationSpecificMaximum, &bandwidth, error)) {
2603 return false;
2604 } else {
wu@webrtc.org5e760e72014-04-02 23:19:09 +00002605 int b = 0;
2606 if (!GetValueFromString(line, bandwidth, &b, error)) {
2607 return false;
2608 }
Peter Thatcherc0c3a862015-06-24 15:31:25 -07002609 // We should never use more than the default bandwidth for RTP-based
2610 // data channels. Don't allow SDP to set the bandwidth, because
2611 // that would give JS the opportunity to "break the Internet".
2612 // See: https://code.google.com/p/chromium/issues/detail?id=280726
2613 if (media_type == cricket::MEDIA_TYPE_DATA && IsRtp(protocol) &&
2614 b > cricket::kDataMaxBandwidth / 1000) {
2615 std::ostringstream description;
2616 description << "RTP-based data channels may not send more than "
2617 << cricket::kDataMaxBandwidth / 1000 << "kbps.";
2618 return ParseFailed(line, description.str(), error);
2619 }
deadbeefb2362572016-12-13 16:37:06 -08002620 // Prevent integer overflow.
2621 b = std::min(b, INT_MAX / 1000);
wu@webrtc.org5e760e72014-04-02 23:19:09 +00002622 media_desc->set_bandwidth(b * 1000);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002623 }
2624 }
2625 continue;
2626 }
2627
2628 if (!IsLineType(line, kLineTypeAttributes)) {
2629 // TODO: Handle other lines if needed.
2630 LOG(LS_INFO) << "Ignored line: " << line;
2631 continue;
2632 }
2633
2634 // Handle attributes common to SCTP and RTP.
2635 if (HasAttribute(line, kAttributeMid)) {
2636 // RFC 3388
2637 // mid-attribute = "a=mid:" identification-tag
2638 // identification-tag = token
2639 // Use the mid identification-tag as the content name.
2640 if (!GetValue(line, kAttributeMid, &mline_id, error)) {
2641 return false;
2642 }
2643 *content_name = mline_id;
deadbeef25ed4352016-12-12 18:37:36 -08002644 } else if (HasAttribute(line, kAttributeBundleOnly)) {
2645 *bundle_only = true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002646 } else if (HasAttribute(line, kAttributeCandidate)) {
2647 Candidate candidate;
2648 if (!ParseCandidate(line, &candidate, error, false)) {
2649 return false;
2650 }
deadbeef7bcdb692017-01-20 12:43:58 -08002651 // ParseCandidate will parse non-standard ufrag and password attributes,
2652 // since it's used for candidate trickling, but we only want to process
2653 // the "a=ice-ufrag"/"a=ice-pwd" values in a session description, so
2654 // strip them off at this point.
2655 candidate.set_username(std::string());
2656 candidate.set_password(std::string());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002657 candidates_orig.push_back(candidate);
2658 } else if (HasAttribute(line, kAttributeIceUfrag)) {
2659 if (!GetValue(line, kAttributeIceUfrag, &transport->ice_ufrag, error)) {
2660 return false;
2661 }
2662 } else if (HasAttribute(line, kAttributeIcePwd)) {
2663 if (!GetValue(line, kAttributeIcePwd, &transport->ice_pwd, error)) {
2664 return false;
2665 }
2666 } else if (HasAttribute(line, kAttributeIceOption)) {
2667 if (!ParseIceOptions(line, &transport->transport_options, error)) {
2668 return false;
2669 }
2670 } else if (HasAttribute(line, kAttributeFmtp)) {
2671 if (!ParseFmtpAttributes(line, media_type, media_desc, error)) {
2672 return false;
2673 }
2674 } else if (HasAttribute(line, kAttributeFingerprint)) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002675 rtc::SSLFingerprint* fingerprint = NULL;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002676
2677 if (!ParseFingerprintAttribute(line, &fingerprint, error)) {
2678 return false;
2679 }
2680 transport->identity_fingerprint.reset(fingerprint);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00002681 } else if (HasAttribute(line, kAttributeSetup)) {
2682 if (!ParseDtlsSetup(line, &(transport->connection_role), error)) {
2683 return false;
2684 }
pthatcher@webrtc.org3341b402015-02-13 21:14:22 +00002685 } else if (IsDtlsSctp(protocol) && HasAttribute(line, kAttributeSctpPort)) {
deadbeef7e146cb2016-09-28 10:04:34 -07002686 if (media_type != cricket::MEDIA_TYPE_DATA) {
2687 return ParseFailed(
2688 line, "sctp-port attribute found in non-data media description.",
2689 error);
2690 }
jiayl@webrtc.orgddb85ab2014-09-05 16:31:56 +00002691 int sctp_port;
2692 if (!ParseSctpPort(line, &sctp_port, error)) {
2693 return false;
2694 }
2695 if (!AddSctpDataCodec(static_cast<DataContentDescription*>(media_desc),
2696 sctp_port)) {
2697 return false;
2698 }
pthatcher@webrtc.org3341b402015-02-13 21:14:22 +00002699 } else if (IsRtp(protocol)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002700 //
2701 // RTP specific attrubtes
2702 //
2703 if (HasAttribute(line, kAttributeRtcpMux)) {
2704 media_desc->set_rtcp_mux(true);
deadbeef13871492015-12-09 12:37:51 -08002705 } else if (HasAttribute(line, kAttributeRtcpReducedSize)) {
2706 media_desc->set_rtcp_reduced_size(true);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002707 } else if (HasAttribute(line, kAttributeSsrcGroup)) {
2708 if (!ParseSsrcGroupAttribute(line, &ssrc_groups, error)) {
2709 return false;
2710 }
2711 } else if (HasAttribute(line, kAttributeSsrc)) {
2712 if (!ParseSsrcAttribute(line, &ssrc_infos, error)) {
2713 return false;
2714 }
2715 } else if (HasAttribute(line, kAttributeCrypto)) {
2716 if (!ParseCryptoAttribute(line, media_desc, error)) {
2717 return false;
2718 }
2719 } else if (HasAttribute(line, kAttributeRtpmap)) {
deadbeef67cf2c12016-04-13 10:07:16 -07002720 if (!ParseRtpmapAttribute(line, media_type, payload_types, media_desc,
2721 error)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002722 return false;
2723 }
2724 } else if (HasAttribute(line, kCodecParamMaxPTime)) {
2725 if (!GetValue(line, kCodecParamMaxPTime, &maxptime_as_string, error)) {
2726 return false;
2727 }
2728 } else if (HasAttribute(line, kAttributeRtcpFb)) {
2729 if (!ParseRtcpFbAttribute(line, media_type, media_desc, error)) {
2730 return false;
2731 }
2732 } else if (HasAttribute(line, kCodecParamPTime)) {
2733 if (!GetValue(line, kCodecParamPTime, &ptime_as_string, error)) {
2734 return false;
2735 }
2736 } else if (HasAttribute(line, kAttributeSendOnly)) {
2737 media_desc->set_direction(cricket::MD_SENDONLY);
2738 } else if (HasAttribute(line, kAttributeRecvOnly)) {
2739 media_desc->set_direction(cricket::MD_RECVONLY);
2740 } else if (HasAttribute(line, kAttributeInactive)) {
2741 media_desc->set_direction(cricket::MD_INACTIVE);
2742 } else if (HasAttribute(line, kAttributeSendRecv)) {
2743 media_desc->set_direction(cricket::MD_SENDRECV);
2744 } else if (HasAttribute(line, kAttributeExtmap)) {
isheriff6f8d6862016-05-26 11:24:55 -07002745 RtpExtension extmap;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002746 if (!ParseExtmap(line, &extmap, error)) {
2747 return false;
2748 }
2749 media_desc->AddRtpHeaderExtension(extmap);
2750 } else if (HasAttribute(line, kAttributeXGoogleFlag)) {
2751 // Experimental attribute. Conference mode activates more aggressive
2752 // AEC and NS settings.
2753 // TODO: expose API to set these directly.
2754 std::string flag_value;
2755 if (!GetValue(line, kAttributeXGoogleFlag, &flag_value, error)) {
2756 return false;
2757 }
2758 if (flag_value.compare(kValueConference) == 0)
2759 media_desc->set_conference_mode(true);
deadbeef9d3584c2016-02-16 17:54:10 -08002760 } else if (HasAttribute(line, kAttributeMsid)) {
2761 if (!ParseMsidAttribute(line, &stream_id, &track_id, error)) {
2762 return false;
2763 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002764 }
2765 } else {
2766 // Only parse lines that we are interested of.
2767 LOG(LS_INFO) << "Ignored line: " << line;
2768 continue;
2769 }
2770 }
2771
2772 // Create tracks from the |ssrc_infos|.
Taylor Brandstetter5de6b752016-03-09 17:02:30 -08002773 // If the stream_id/track_id for all SSRCS are identical, one StreamParams
2774 // will be created in CreateTracksFromSsrcInfos, containing all the SSRCs from
2775 // the m= section.
2776 CreateTracksFromSsrcInfos(ssrc_infos, stream_id, track_id, &tracks);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002777
2778 // Add the ssrc group to the track.
2779 for (SsrcGroupVec::iterator ssrc_group = ssrc_groups.begin();
2780 ssrc_group != ssrc_groups.end(); ++ssrc_group) {
2781 if (ssrc_group->ssrcs.empty()) {
2782 continue;
2783 }
Peter Boström0c4e06b2015-10-07 12:23:21 +02002784 uint32_t ssrc = ssrc_group->ssrcs.front();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002785 for (StreamParamsVec::iterator track = tracks.begin();
2786 track != tracks.end(); ++track) {
2787 if (track->has_ssrc(ssrc)) {
2788 track->ssrc_groups.push_back(*ssrc_group);
2789 }
2790 }
2791 }
2792
2793 // Add the new tracks to the |media_desc|.
deadbeef9d3584c2016-02-16 17:54:10 -08002794 for (StreamParams& track : tracks) {
2795 media_desc->AddStream(track);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002796 }
2797
2798 if (media_type == cricket::MEDIA_TYPE_AUDIO) {
2799 AudioContentDescription* audio_desc =
2800 static_cast<AudioContentDescription*>(media_desc);
jlmiller@webrtc.orga744a282015-02-18 21:37:46 +00002801 UpdateFromWildcardCodecs(audio_desc);
2802
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002803 // Verify audio codec ensures that no audio codec has been populated with
2804 // only fmtp.
2805 if (!VerifyAudioCodecs(audio_desc)) {
2806 return ParseFailed("Failed to parse audio codecs correctly.", error);
2807 }
2808 AddAudioAttribute(kCodecParamMaxPTime, maxptime_as_string, audio_desc);
2809 AddAudioAttribute(kCodecParamPTime, ptime_as_string, audio_desc);
2810 }
2811
2812 if (media_type == cricket::MEDIA_TYPE_VIDEO) {
jlmiller@webrtc.orga744a282015-02-18 21:37:46 +00002813 VideoContentDescription* video_desc =
2814 static_cast<VideoContentDescription*>(media_desc);
2815 UpdateFromWildcardCodecs(video_desc);
2816 // Verify video codec ensures that no video codec has been populated with
2817 // only rtcp-fb.
2818 if (!VerifyVideoCodecs(video_desc)) {
2819 return ParseFailed("Failed to parse video codecs correctly.", error);
2820 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002821 }
2822
2823 // RFC 5245
2824 // Update the candidates with the media level "ice-pwd" and "ice-ufrag".
2825 for (Candidates::iterator it = candidates_orig.begin();
2826 it != candidates_orig.end(); ++it) {
nisseede5da42017-01-12 05:15:36 -08002827 RTC_DCHECK((*it).username().empty() ||
2828 (*it).username() == transport->ice_ufrag);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002829 (*it).set_username(transport->ice_ufrag);
nisseede5da42017-01-12 05:15:36 -08002830 RTC_DCHECK((*it).password().empty());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002831 (*it).set_password(transport->ice_pwd);
2832 candidates->push_back(
2833 new JsepIceCandidate(mline_id, mline_index, *it));
2834 }
2835 return true;
2836}
2837
2838bool ParseSsrcAttribute(const std::string& line, SsrcInfoVec* ssrc_infos,
2839 SdpParseError* error) {
nisseede5da42017-01-12 05:15:36 -08002840 RTC_DCHECK(ssrc_infos != NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002841 // RFC 5576
2842 // a=ssrc:<ssrc-id> <attribute>
2843 // a=ssrc:<ssrc-id> <attribute>:<value>
2844 std::string field1, field2;
Donald Curtis144d0182015-05-15 13:14:24 -07002845 if (!rtc::tokenize_first(line.substr(kLinePrefixLength), kSdpDelimiterSpace,
2846 &field1, &field2)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002847 const size_t expected_fields = 2;
2848 return ParseFailedExpectFieldNum(line, expected_fields, error);
2849 }
2850
2851 // ssrc:<ssrc-id>
2852 std::string ssrc_id_s;
2853 if (!GetValue(field1, kAttributeSsrc, &ssrc_id_s, error)) {
2854 return false;
2855 }
Peter Boström0c4e06b2015-10-07 12:23:21 +02002856 uint32_t ssrc_id = 0;
wu@webrtc.org5e760e72014-04-02 23:19:09 +00002857 if (!GetValueFromString(line, ssrc_id_s, &ssrc_id, error)) {
2858 return false;
2859 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002860
2861 std::string attribute;
2862 std::string value;
Donald Curtis144d0182015-05-15 13:14:24 -07002863 if (!rtc::tokenize_first(field2, kSdpDelimiterColon, &attribute, &value)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002864 std::ostringstream description;
2865 description << "Failed to get the ssrc attribute value from " << field2
2866 << ". Expected format <attribute>:<value>.";
2867 return ParseFailed(line, description.str(), error);
2868 }
2869
2870 // Check if there's already an item for this |ssrc_id|. Create a new one if
2871 // there isn't.
2872 SsrcInfoVec::iterator ssrc_info = ssrc_infos->begin();
2873 for (; ssrc_info != ssrc_infos->end(); ++ssrc_info) {
2874 if (ssrc_info->ssrc_id == ssrc_id) {
2875 break;
2876 }
2877 }
2878 if (ssrc_info == ssrc_infos->end()) {
2879 SsrcInfo info;
2880 info.ssrc_id = ssrc_id;
2881 ssrc_infos->push_back(info);
2882 ssrc_info = ssrc_infos->end() - 1;
2883 }
2884
2885 // Store the info to the |ssrc_info|.
2886 if (attribute == kSsrcAttributeCname) {
2887 // RFC 5576
2888 // cname:<value>
2889 ssrc_info->cname = value;
2890 } else if (attribute == kSsrcAttributeMsid) {
2891 // draft-alvestrand-mmusic-msid-00
2892 // "msid:" identifier [ " " appdata ]
2893 std::vector<std::string> fields;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002894 rtc::split(value, kSdpDelimiterSpace, &fields);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002895 if (fields.size() < 1 || fields.size() > 2) {
2896 return ParseFailed(line,
2897 "Expected format \"msid:<identifier>[ <appdata>]\".",
2898 error);
2899 }
deadbeef9d3584c2016-02-16 17:54:10 -08002900 ssrc_info->stream_id = fields[0];
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002901 if (fields.size() == 2) {
deadbeef9d3584c2016-02-16 17:54:10 -08002902 ssrc_info->track_id = fields[1];
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002903 }
2904 } else if (attribute == kSsrcAttributeMslabel) {
2905 // draft-alvestrand-rtcweb-mid-01
2906 // mslabel:<value>
2907 ssrc_info->mslabel = value;
2908 } else if (attribute == kSSrcAttributeLabel) {
2909 // The label isn't defined.
2910 // label:<value>
2911 ssrc_info->label = value;
2912 }
2913 return true;
2914}
2915
2916bool ParseSsrcGroupAttribute(const std::string& line,
2917 SsrcGroupVec* ssrc_groups,
2918 SdpParseError* error) {
nisseede5da42017-01-12 05:15:36 -08002919 RTC_DCHECK(ssrc_groups != NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002920 // RFC 5576
2921 // a=ssrc-group:<semantics> <ssrc-id> ...
2922 std::vector<std::string> fields;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002923 rtc::split(line.substr(kLinePrefixLength),
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002924 kSdpDelimiterSpace, &fields);
2925 const size_t expected_min_fields = 2;
2926 if (fields.size() < expected_min_fields) {
2927 return ParseFailedExpectMinFieldNum(line, expected_min_fields, error);
2928 }
2929 std::string semantics;
2930 if (!GetValue(fields[0], kAttributeSsrcGroup, &semantics, error)) {
2931 return false;
2932 }
Peter Boström0c4e06b2015-10-07 12:23:21 +02002933 std::vector<uint32_t> ssrcs;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002934 for (size_t i = 1; i < fields.size(); ++i) {
Peter Boström0c4e06b2015-10-07 12:23:21 +02002935 uint32_t ssrc = 0;
wu@webrtc.org5e760e72014-04-02 23:19:09 +00002936 if (!GetValueFromString(line, fields[i], &ssrc, error)) {
2937 return false;
2938 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002939 ssrcs.push_back(ssrc);
2940 }
2941 ssrc_groups->push_back(SsrcGroup(semantics, ssrcs));
2942 return true;
2943}
2944
2945bool ParseCryptoAttribute(const std::string& line,
2946 MediaContentDescription* media_desc,
2947 SdpParseError* error) {
2948 std::vector<std::string> fields;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002949 rtc::split(line.substr(kLinePrefixLength),
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002950 kSdpDelimiterSpace, &fields);
2951 // RFC 4568
2952 // a=crypto:<tag> <crypto-suite> <key-params> [<session-params>]
2953 const size_t expected_min_fields = 3;
2954 if (fields.size() < expected_min_fields) {
2955 return ParseFailedExpectMinFieldNum(line, expected_min_fields, error);
2956 }
2957 std::string tag_value;
2958 if (!GetValue(fields[0], kAttributeCrypto, &tag_value, error)) {
2959 return false;
2960 }
wu@webrtc.org5e760e72014-04-02 23:19:09 +00002961 int tag = 0;
2962 if (!GetValueFromString(line, tag_value, &tag, error)) {
2963 return false;
2964 }
jbauch083b73f2015-07-16 02:46:32 -07002965 const std::string& crypto_suite = fields[1];
2966 const std::string& key_params = fields[2];
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002967 std::string session_params;
2968 if (fields.size() > 3) {
2969 session_params = fields[3];
2970 }
2971 media_desc->AddCrypto(CryptoParams(tag, crypto_suite, key_params,
2972 session_params));
2973 return true;
2974}
2975
2976// Updates or creates a new codec entry in the audio description with according
deadbeef67cf2c12016-04-13 10:07:16 -07002977// to |name|, |clockrate|, |bitrate|, and |channels|.
2978void UpdateCodec(int payload_type,
2979 const std::string& name,
2980 int clockrate,
2981 int bitrate,
2982 size_t channels,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002983 AudioContentDescription* audio_desc) {
2984 // Codec may already be populated with (only) optional parameters
2985 // (from an fmtp).
changbin.shao@webrtc.org2d25b442015-03-16 04:14:34 +00002986 cricket::AudioCodec codec =
2987 GetCodecWithPayloadType(audio_desc->codecs(), payload_type);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002988 codec.name = name;
2989 codec.clockrate = clockrate;
2990 codec.bitrate = bitrate;
2991 codec.channels = channels;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002992 AddOrReplaceCodec<AudioContentDescription, cricket::AudioCodec>(audio_desc,
2993 codec);
2994}
2995
2996// Updates or creates a new codec entry in the video description according to
deadbeef67cf2c12016-04-13 10:07:16 -07002997// |name|, |width|, |height|, and |framerate|.
2998void UpdateCodec(int payload_type,
2999 const std::string& name,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003000 VideoContentDescription* video_desc) {
3001 // Codec may already be populated with (only) optional parameters
3002 // (from an fmtp).
changbin.shao@webrtc.org2d25b442015-03-16 04:14:34 +00003003 cricket::VideoCodec codec =
3004 GetCodecWithPayloadType(video_desc->codecs(), payload_type);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003005 codec.name = name;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003006 AddOrReplaceCodec<VideoContentDescription, cricket::VideoCodec>(video_desc,
3007 codec);
3008}
3009
3010bool ParseRtpmapAttribute(const std::string& line,
3011 const MediaType media_type,
deadbeef67cf2c12016-04-13 10:07:16 -07003012 const std::vector<int>& payload_types,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003013 MediaContentDescription* media_desc,
3014 SdpParseError* error) {
3015 std::vector<std::string> fields;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00003016 rtc::split(line.substr(kLinePrefixLength),
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003017 kSdpDelimiterSpace, &fields);
3018 // RFC 4566
3019 // a=rtpmap:<payload type> <encoding name>/<clock rate>[/<encodingparameters>]
3020 const size_t expected_min_fields = 2;
3021 if (fields.size() < expected_min_fields) {
3022 return ParseFailedExpectMinFieldNum(line, expected_min_fields, error);
3023 }
3024 std::string payload_type_value;
3025 if (!GetValue(fields[0], kAttributeRtpmap, &payload_type_value, error)) {
3026 return false;
3027 }
wu@webrtc.org5e760e72014-04-02 23:19:09 +00003028 int payload_type = 0;
pkasting@chromium.orge9facf82015-02-17 20:36:28 +00003029 if (!GetPayloadTypeFromString(line, payload_type_value, &payload_type,
3030 error)) {
wu@webrtc.org5e760e72014-04-02 23:19:09 +00003031 return false;
3032 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003033
deadbeef67cf2c12016-04-13 10:07:16 -07003034 if (std::find(payload_types.begin(), payload_types.end(), payload_type) ==
3035 payload_types.end()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003036 LOG(LS_WARNING) << "Ignore rtpmap line that did not appear in the "
3037 << "<fmt> of the m-line: " << line;
3038 return true;
3039 }
jbauch083b73f2015-07-16 02:46:32 -07003040 const std::string& encoder = fields[1];
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003041 std::vector<std::string> codec_params;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00003042 rtc::split(encoder, '/', &codec_params);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003043 // <encoding name>/<clock rate>[/<encodingparameters>]
3044 // 2 mandatory fields
3045 if (codec_params.size() < 2 || codec_params.size() > 3) {
3046 return ParseFailed(line,
3047 "Expected format \"<encoding name>/<clock rate>"
3048 "[/<encodingparameters>]\".",
3049 error);
3050 }
jbauch083b73f2015-07-16 02:46:32 -07003051 const std::string& encoding_name = codec_params[0];
wu@webrtc.org5e760e72014-04-02 23:19:09 +00003052 int clock_rate = 0;
3053 if (!GetValueFromString(line, codec_params[1], &clock_rate, error)) {
3054 return false;
3055 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003056 if (media_type == cricket::MEDIA_TYPE_VIDEO) {
3057 VideoContentDescription* video_desc =
3058 static_cast<VideoContentDescription*>(media_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003059 UpdateCodec(payload_type, encoding_name,
deadbeef67cf2c12016-04-13 10:07:16 -07003060 video_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003061 } else if (media_type == cricket::MEDIA_TYPE_AUDIO) {
3062 // RFC 4566
3063 // For audio streams, <encoding parameters> indicates the number
3064 // of audio channels. This parameter is OPTIONAL and may be
3065 // omitted if the number of channels is one, provided that no
3066 // additional parameters are needed.
Peter Kasting69558702016-01-12 16:26:35 -08003067 size_t channels = 1;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003068 if (codec_params.size() == 3) {
wu@webrtc.org5e760e72014-04-02 23:19:09 +00003069 if (!GetValueFromString(line, codec_params[2], &channels, error)) {
3070 return false;
3071 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003072 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003073 AudioContentDescription* audio_desc =
3074 static_cast<AudioContentDescription*>(media_desc);
ossue1405ad2017-01-23 08:55:48 -08003075 UpdateCodec(payload_type, encoding_name, clock_rate, 0, channels,
deadbeef67cf2c12016-04-13 10:07:16 -07003076 audio_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003077 } else if (media_type == cricket::MEDIA_TYPE_DATA) {
3078 DataContentDescription* data_desc =
3079 static_cast<DataContentDescription*>(media_desc);
deadbeef67cf2c12016-04-13 10:07:16 -07003080 data_desc->AddCodec(cricket::DataCodec(payload_type, encoding_name));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003081 }
3082 return true;
3083}
3084
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003085bool ParseFmtpParam(const std::string& line, std::string* parameter,
3086 std::string* value, SdpParseError* error) {
Donald Curtis0e07f922015-05-15 09:21:23 -07003087 if (!rtc::tokenize_first(line, kSdpDelimiterEqual, parameter, value)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003088 ParseFailed(line, "Unable to parse fmtp parameter. \'=\' missing.", error);
3089 return false;
3090 }
3091 // a=fmtp:<payload_type> <param1>=<value1>; <param2>=<value2>; ...
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003092 return true;
3093}
3094
3095bool ParseFmtpAttributes(const std::string& line, const MediaType media_type,
3096 MediaContentDescription* media_desc,
3097 SdpParseError* error) {
3098 if (media_type != cricket::MEDIA_TYPE_AUDIO &&
3099 media_type != cricket::MEDIA_TYPE_VIDEO) {
3100 return true;
3101 }
Donald Curtis0e07f922015-05-15 09:21:23 -07003102
3103 std::string line_payload;
3104 std::string line_params;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003105
3106 // RFC 5576
3107 // a=fmtp:<format> <format specific parameters>
3108 // At least two fields, whereas the second one is any of the optional
3109 // parameters.
Donald Curtis144d0182015-05-15 13:14:24 -07003110 if (!rtc::tokenize_first(line.substr(kLinePrefixLength), kSdpDelimiterSpace,
3111 &line_payload, &line_params)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003112 ParseFailedExpectMinFieldNum(line, 2, error);
3113 return false;
3114 }
3115
Donald Curtis0e07f922015-05-15 09:21:23 -07003116 // Parse out the payload information.
pkasting@chromium.orgd3245462015-02-23 21:28:22 +00003117 std::string payload_type_str;
Donald Curtis0e07f922015-05-15 09:21:23 -07003118 if (!GetValue(line_payload, kAttributeFmtp, &payload_type_str, error)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003119 return false;
3120 }
3121
Donald Curtis0e07f922015-05-15 09:21:23 -07003122 int payload_type = 0;
Donald Curtis144d0182015-05-15 13:14:24 -07003123 if (!GetPayloadTypeFromString(line_payload, payload_type_str, &payload_type,
3124 error)) {
Donald Curtis0e07f922015-05-15 09:21:23 -07003125 return false;
3126 }
3127
3128 // Parse out format specific parameters.
3129 std::vector<std::string> fields;
3130 rtc::split(line_params, kSdpDelimiterSemicolon, &fields);
3131
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003132 cricket::CodecParameterMap codec_params;
Donald Curtis144d0182015-05-15 13:14:24 -07003133 for (auto& iter : fields) {
Donald Curtis0e07f922015-05-15 09:21:23 -07003134 if (iter.find(kSdpDelimiterEqual) == std::string::npos) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003135 // Only fmtps with equals are currently supported. Other fmtp types
3136 // should be ignored. Unknown fmtps do not constitute an error.
3137 continue;
3138 }
Donald Curtis0e07f922015-05-15 09:21:23 -07003139
3140 std::string name;
3141 std::string value;
3142 if (!ParseFmtpParam(rtc::string_trim(iter), &name, &value, error)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003143 return false;
3144 }
3145 codec_params[name] = value;
3146 }
3147
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003148 if (media_type == cricket::MEDIA_TYPE_AUDIO) {
3149 UpdateCodec<AudioContentDescription, cricket::AudioCodec>(
pkasting@chromium.orgd3245462015-02-23 21:28:22 +00003150 media_desc, payload_type, codec_params);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003151 } else if (media_type == cricket::MEDIA_TYPE_VIDEO) {
3152 UpdateCodec<VideoContentDescription, cricket::VideoCodec>(
pkasting@chromium.orgd3245462015-02-23 21:28:22 +00003153 media_desc, payload_type, codec_params);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003154 }
3155 return true;
3156}
3157
3158bool ParseRtcpFbAttribute(const std::string& line, const MediaType media_type,
3159 MediaContentDescription* media_desc,
3160 SdpParseError* error) {
3161 if (media_type != cricket::MEDIA_TYPE_AUDIO &&
3162 media_type != cricket::MEDIA_TYPE_VIDEO) {
3163 return true;
3164 }
3165 std::vector<std::string> rtcp_fb_fields;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00003166 rtc::split(line.c_str(), kSdpDelimiterSpace, &rtcp_fb_fields);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003167 if (rtcp_fb_fields.size() < 2) {
3168 return ParseFailedGetValue(line, kAttributeRtcpFb, error);
3169 }
3170 std::string payload_type_string;
3171 if (!GetValue(rtcp_fb_fields[0], kAttributeRtcpFb, &payload_type_string,
3172 error)) {
3173 return false;
3174 }
wu@webrtc.org5e760e72014-04-02 23:19:09 +00003175 int payload_type = kWildcardPayloadType;
3176 if (payload_type_string != "*") {
pkasting@chromium.orge9facf82015-02-17 20:36:28 +00003177 if (!GetPayloadTypeFromString(line, payload_type_string, &payload_type,
3178 error)) {
wu@webrtc.org5e760e72014-04-02 23:19:09 +00003179 return false;
3180 }
3181 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003182 std::string id = rtcp_fb_fields[1];
3183 std::string param = "";
3184 for (std::vector<std::string>::iterator iter = rtcp_fb_fields.begin() + 2;
3185 iter != rtcp_fb_fields.end(); ++iter) {
3186 param.append(*iter);
3187 }
3188 const cricket::FeedbackParam feedback_param(id, param);
3189
3190 if (media_type == cricket::MEDIA_TYPE_AUDIO) {
pkasting@chromium.orgd3245462015-02-23 21:28:22 +00003191 UpdateCodec<AudioContentDescription, cricket::AudioCodec>(
3192 media_desc, payload_type, feedback_param);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003193 } else if (media_type == cricket::MEDIA_TYPE_VIDEO) {
pkasting@chromium.orgd3245462015-02-23 21:28:22 +00003194 UpdateCodec<VideoContentDescription, cricket::VideoCodec>(
3195 media_desc, payload_type, feedback_param);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003196 }
3197 return true;
3198}
3199
3200} // namespace webrtc