blob: f92baa18732947faac6a350be1a5947697eb7be9 [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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#include "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>
Steve Anton36b29d12017-10-30 09:57:42 -070018#include <map>
kwibergd1fe2812016-04-27 06:47:29 -070019#include <memory>
Steve Anton36b29d12017-10-30 09:57:42 -070020#include <set>
henrike@webrtc.org28e20752013-07-10 00:45:36 +000021#include <string>
deadbeef67cf2c12016-04-13 10:07:16 -070022#include <unordered_map>
henrike@webrtc.org28e20752013-07-10 00:45:36 +000023#include <vector>
24
Patrik Höglunde2d6a062017-10-05 14:53:33 +020025#include "api/candidate.h"
Patrik Höglund7aee3d52017-11-15 13:15:17 +010026#include "api/cryptoparams.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020027#include "api/jsepicecandidate.h"
28#include "api/jsepsessiondescription.h"
isheriff6f8d6862016-05-26 11:24:55 -070029// for RtpExtension
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020030#include "api/rtpparameters.h"
31#include "media/base/codec.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020032#include "media/base/mediaconstants.h"
33#include "media/base/rtputils.h"
34#include "media/sctp/sctptransportinternal.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020035#include "p2p/base/p2pconstants.h"
36#include "p2p/base/port.h"
37#include "pc/mediasession.h"
38#include "rtc_base/arraysize.h"
39#include "rtc_base/checks.h"
40#include "rtc_base/logging.h"
41#include "rtc_base/messagedigest.h"
42#include "rtc_base/stringutils.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000043
44using cricket::AudioContentDescription;
45using cricket::Candidate;
46using cricket::Candidates;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000047using cricket::ContentInfo;
48using cricket::CryptoParams;
49using cricket::DataContentDescription;
50using cricket::ICE_CANDIDATE_COMPONENT_RTP;
51using cricket::ICE_CANDIDATE_COMPONENT_RTCP;
52using cricket::kCodecParamMaxBitrate;
53using cricket::kCodecParamMaxPTime;
54using cricket::kCodecParamMaxQuantization;
55using cricket::kCodecParamMinBitrate;
56using cricket::kCodecParamMinPTime;
57using cricket::kCodecParamPTime;
58using cricket::kCodecParamSPropStereo;
buildbot@webrtc.orged97bb02014-05-07 11:15:20 +000059using cricket::kCodecParamStartBitrate;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000060using cricket::kCodecParamStereo;
61using cricket::kCodecParamUseInbandFec;
Minyue Li7100dcd2015-03-27 05:05:59 +010062using cricket::kCodecParamUseDtx;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000063using cricket::kCodecParamSctpProtocol;
64using cricket::kCodecParamSctpStreams;
henrike@webrtc.org1e09a712013-07-26 19:17:59 +000065using cricket::kCodecParamMaxAverageBitrate;
buildbot@webrtc.org5d639b32014-09-10 07:57:12 +000066using cricket::kCodecParamMaxPlaybackRate;
stefan@webrtc.org85d27942014-06-09 12:51:39 +000067using cricket::kCodecParamAssociatedPayloadType;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000068using cricket::MediaContentDescription;
69using cricket::MediaType;
isheriff6f8d6862016-05-26 11:24:55 -070070using cricket::RtpHeaderExtensions;
Steve Anton5adfafd2017-12-20 16:34:00 -080071using cricket::MediaProtocolType;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000072using cricket::SsrcGroup;
73using cricket::StreamParams;
74using cricket::StreamParamsVec;
75using cricket::TransportDescription;
76using cricket::TransportInfo;
77using cricket::VideoContentDescription;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000078using rtc::SocketAddress;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000079
henrike@webrtc.org28e20752013-07-10 00:45:36 +000080namespace cricket {
81class SessionDescription;
82}
83
deadbeef90f1e1e2017-02-10 12:35:05 -080084// TODO(deadbeef): Switch to using anonymous namespace rather than declaring
85// everything "static".
henrike@webrtc.org28e20752013-07-10 00:45:36 +000086namespace webrtc {
87
88// Line type
89// RFC 4566
90// An SDP session description consists of a number of lines of text of
91// the form:
92// <type>=<value>
93// where <type> MUST be exactly one case-significant character.
deadbeef9d3584c2016-02-16 17:54:10 -080094static const int kLinePrefixLength = 2; // Length of <type>=
henrike@webrtc.org28e20752013-07-10 00:45:36 +000095static const char kLineTypeVersion = 'v';
96static const char kLineTypeOrigin = 'o';
97static const char kLineTypeSessionName = 's';
98static const char kLineTypeSessionInfo = 'i';
99static const char kLineTypeSessionUri = 'u';
100static const char kLineTypeSessionEmail = 'e';
101static const char kLineTypeSessionPhone = 'p';
102static const char kLineTypeSessionBandwidth = 'b';
103static const char kLineTypeTiming = 't';
104static const char kLineTypeRepeatTimes = 'r';
105static const char kLineTypeTimeZone = 'z';
106static const char kLineTypeEncryptionKey = 'k';
107static const char kLineTypeMedia = 'm';
108static const char kLineTypeConnection = 'c';
109static const char kLineTypeAttributes = 'a';
110
111// Attributes
112static const char kAttributeGroup[] = "group";
113static const char kAttributeMid[] = "mid";
deadbeef9d3584c2016-02-16 17:54:10 -0800114static const char kAttributeMsid[] = "msid";
deadbeef25ed4352016-12-12 18:37:36 -0800115static const char kAttributeBundleOnly[] = "bundle-only";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000116static const char kAttributeRtcpMux[] = "rtcp-mux";
deadbeef13871492015-12-09 12:37:51 -0800117static const char kAttributeRtcpReducedSize[] = "rtcp-rsize";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000118static const char kAttributeSsrc[] = "ssrc";
119static const char kSsrcAttributeCname[] = "cname";
120static const char kAttributeExtmap[] = "extmap";
121// draft-alvestrand-mmusic-msid-01
122// a=msid-semantic: WMS
Seth Hampson5b4f0752018-04-02 16:31:36 -0700123// This is a legacy field supported only for Plan B semantics.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000124static const char kAttributeMsidSemantics[] = "msid-semantic";
125static const char kMediaStreamSemantic[] = "WMS";
126static const char kSsrcAttributeMsid[] = "msid";
127static const char kDefaultMsid[] = "default";
Seth Hampson5b4f0752018-04-02 16:31:36 -0700128static const char kNoStreamMsid[] = "-";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000129static const char kSsrcAttributeMslabel[] = "mslabel";
130static const char kSSrcAttributeLabel[] = "label";
131static const char kAttributeSsrcGroup[] = "ssrc-group";
132static const char kAttributeCrypto[] = "crypto";
133static const char kAttributeCandidate[] = "candidate";
134static const char kAttributeCandidateTyp[] = "typ";
135static const char kAttributeCandidateRaddr[] = "raddr";
136static const char kAttributeCandidateRport[] = "rport";
honghaiza54a0802015-12-16 18:37:23 -0800137static const char kAttributeCandidateUfrag[] = "ufrag";
138static const char kAttributeCandidatePwd[] = "pwd";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000139static const char kAttributeCandidateGeneration[] = "generation";
honghaiza0c44ea2016-03-23 16:07:48 -0700140static const char kAttributeCandidateNetworkId[] = "network-id";
honghaize1a0c942016-02-16 14:54:56 -0800141static const char kAttributeCandidateNetworkCost[] = "network-cost";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000142static const char kAttributeFingerprint[] = "fingerprint";
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +0000143static const char kAttributeSetup[] = "setup";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000144static const char kAttributeFmtp[] = "fmtp";
145static const char kAttributeRtpmap[] = "rtpmap";
wu@webrtc.org78187522013-10-07 23:32:02 +0000146static const char kAttributeSctpmap[] = "sctpmap";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000147static const char kAttributeRtcp[] = "rtcp";
148static const char kAttributeIceUfrag[] = "ice-ufrag";
149static const char kAttributeIcePwd[] = "ice-pwd";
150static const char kAttributeIceLite[] = "ice-lite";
151static const char kAttributeIceOption[] = "ice-options";
152static const char kAttributeSendOnly[] = "sendonly";
153static const char kAttributeRecvOnly[] = "recvonly";
154static const char kAttributeRtcpFb[] = "rtcp-fb";
155static const char kAttributeSendRecv[] = "sendrecv";
156static const char kAttributeInactive[] = "inactive";
jiayl@webrtc.orgddb85ab2014-09-05 16:31:56 +0000157// draft-ietf-mmusic-sctp-sdp-07
158// a=sctp-port
159static const char kAttributeSctpPort[] = "sctp-port";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000160
161// Experimental flags
162static const char kAttributeXGoogleFlag[] = "x-google-flag";
163static const char kValueConference[] = "conference";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000164
165// Candidate
166static const char kCandidateHost[] = "host";
167static const char kCandidateSrflx[] = "srflx";
Honghai Zhang7fb69db2016-03-14 11:59:18 -0700168static const char kCandidatePrflx[] = "prflx";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000169static const char kCandidateRelay[] = "relay";
mallinath@webrtc.org2d60c5e2014-08-08 22:29:20 +0000170static const char kTcpCandidateType[] = "tcptype";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000171
172static const char kSdpDelimiterEqual = '=';
173static const char kSdpDelimiterSpace = ' ';
174static const char kSdpDelimiterColon = ':';
175static const char kSdpDelimiterSemicolon = ';';
176static const char kSdpDelimiterSlash = '/';
177static const char kNewLine = '\n';
178static const char kReturn = '\r';
179static const char kLineBreak[] = "\r\n";
180
Steve Anton36b29d12017-10-30 09:57:42 -0700181// TODO(deadbeef): Generate the Session and Time description
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000182// instead of hardcoding.
183static const char kSessionVersion[] = "v=0";
184// RFC 4566
185static const char kSessionOriginUsername[] = "-";
186static const char kSessionOriginSessionId[] = "0";
187static const char kSessionOriginSessionVersion[] = "0";
188static const char kSessionOriginNettype[] = "IN";
189static const char kSessionOriginAddrtype[] = "IP4";
190static const char kSessionOriginAddress[] = "127.0.0.1";
191static const char kSessionName[] = "s=-";
192static const char kTimeDescription[] = "t=0 0";
193static const char kAttrGroup[] = "a=group:BUNDLE";
194static const char kConnectionNettype[] = "IN";
pthatcher@webrtc.orgc9d6d142014-10-23 23:37:22 +0000195static const char kConnectionIpv4Addrtype[] = "IP4";
196static const char kConnectionIpv6Addrtype[] = "IP6";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000197static const char kMediaTypeVideo[] = "video";
198static const char kMediaTypeAudio[] = "audio";
199static const char kMediaTypeData[] = "application";
200static const char kMediaPortRejected[] = "0";
pthatcher@webrtc.orgc9d6d142014-10-23 23:37:22 +0000201// draft-ietf-mmusic-trickle-ice-01
202// When no candidates have been gathered, set the connection
203// address to IP6 ::.
perkj@webrtc.orgd105cc82014-11-07 11:22:06 +0000204// TODO(perkj): FF can not parse IP6 ::. See http://crbug/430333
205// Use IPV4 per default.
206static const char kDummyAddress[] = "0.0.0.0";
pthatcher@webrtc.orgc9d6d142014-10-23 23:37:22 +0000207static const char kDummyPort[] = "9";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000208// RFC 3556
209static const char kApplicationSpecificMaximum[] = "AS";
210
wu@webrtc.org78187522013-10-07 23:32:02 +0000211static const char kDefaultSctpmapProtocol[] = "webrtc-datachannel";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000212
pkasting@chromium.orgd3245462015-02-23 21:28:22 +0000213// RTP payload type is in the 0-127 range. Use -1 to indicate "all" payload
214// types.
215const int kWildcardPayloadType = -1;
216
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000217struct SsrcInfo {
Peter Boström0c4e06b2015-10-07 12:23:21 +0200218 uint32_t ssrc_id;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000219 std::string cname;
deadbeef9d3584c2016-02-16 17:54:10 -0800220 std::string stream_id;
221 std::string track_id;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000222
223 // For backward compatibility.
224 // TODO(ronghuawu): Remove below 2 fields once all the clients support msid.
225 std::string label;
226 std::string mslabel;
227};
228typedef std::vector<SsrcInfo> SsrcInfoVec;
229typedef std::vector<SsrcGroup> SsrcGroupVec;
230
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000231template <class T>
232static void AddFmtpLine(const T& codec, std::string* message);
233static void BuildMediaDescription(const ContentInfo* content_info,
234 const TransportInfo* transport_info,
235 const MediaType media_type,
wu@webrtc.org4c3e9912014-07-16 21:03:13 +0000236 const std::vector<Candidate>& candidates,
Steve Antone831b8c2018-02-01 12:22:16 -0800237 int msid_signaling,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000238 std::string* message);
zstein4b2e0822017-02-17 19:48:38 -0800239static void BuildSctpContentAttributes(std::string* message,
240 int sctp_port,
241 bool use_sctpmap);
deadbeef9d3584c2016-02-16 17:54:10 -0800242static void BuildRtpContentAttributes(const MediaContentDescription* media_desc,
243 const MediaType media_type,
Steve Antone831b8c2018-02-01 12:22:16 -0800244 int msid_signaling,
deadbeef9d3584c2016-02-16 17:54:10 -0800245 std::string* message);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000246static void BuildRtpMap(const MediaContentDescription* media_desc,
247 const MediaType media_type,
248 std::string* message);
249static void BuildCandidate(const std::vector<Candidate>& candidates,
honghaiza54a0802015-12-16 18:37:23 -0800250 bool include_ufrag,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000251 std::string* message);
252static void BuildIceOptions(const std::vector<std::string>& transport_options,
253 std::string* message);
pthatcher@webrtc.org3341b402015-02-13 21:14:22 +0000254static bool IsRtp(const std::string& protocol);
255static bool IsDtlsSctp(const std::string& protocol);
zhihuang38989e52017-03-21 11:04:53 -0700256static bool ParseSessionDescription(const std::string& message,
257 size_t* pos,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000258 std::string* session_id,
259 std::string* session_version,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000260 TransportDescription* session_td,
261 RtpHeaderExtensions* session_extmaps,
zhihuang38989e52017-03-21 11:04:53 -0700262 rtc::SocketAddress* connection_addr,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000263 cricket::SessionDescription* desc,
264 SdpParseError* error);
265static bool ParseGroupAttribute(const std::string& line,
266 cricket::SessionDescription* desc,
267 SdpParseError* error);
268static bool ParseMediaDescription(
269 const std::string& message,
270 const TransportDescription& session_td,
271 const RtpHeaderExtensions& session_extmaps,
zhihuang38989e52017-03-21 11:04:53 -0700272 size_t* pos,
273 const rtc::SocketAddress& session_connection_addr,
274 cricket::SessionDescription* desc,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000275 std::vector<JsepIceCandidate*>* candidates,
276 SdpParseError* error);
277static bool ParseContent(const std::string& message,
278 const MediaType media_type,
279 int mline_index,
280 const std::string& protocol,
deadbeef67cf2c12016-04-13 10:07:16 -0700281 const std::vector<int>& payload_types,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000282 size_t* pos,
283 std::string* content_name,
deadbeef25ed4352016-12-12 18:37:36 -0800284 bool* bundle_only,
Steve Antone831b8c2018-02-01 12:22:16 -0800285 int* msid_signaling,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000286 MediaContentDescription* media_desc,
287 TransportDescription* transport,
288 std::vector<JsepIceCandidate*>* candidates,
289 SdpParseError* error);
290static bool ParseSsrcAttribute(const std::string& line,
291 SsrcInfoVec* ssrc_infos,
Steve Antone831b8c2018-02-01 12:22:16 -0800292 int* msid_signaling,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000293 SdpParseError* error);
294static bool ParseSsrcGroupAttribute(const std::string& line,
295 SsrcGroupVec* ssrc_groups,
296 SdpParseError* error);
297static bool ParseCryptoAttribute(const std::string& line,
298 MediaContentDescription* media_desc,
299 SdpParseError* error);
300static bool ParseRtpmapAttribute(const std::string& line,
301 const MediaType media_type,
deadbeef67cf2c12016-04-13 10:07:16 -0700302 const std::vector<int>& payload_types,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000303 MediaContentDescription* media_desc,
304 SdpParseError* error);
305static bool ParseFmtpAttributes(const std::string& line,
306 const MediaType media_type,
307 MediaContentDescription* media_desc,
308 SdpParseError* error);
309static bool ParseFmtpParam(const std::string& line, std::string* parameter,
310 std::string* value, SdpParseError* error);
311static bool ParseCandidate(const std::string& message, Candidate* candidate,
312 SdpParseError* error, bool is_raw);
313static bool ParseRtcpFbAttribute(const std::string& line,
314 const MediaType media_type,
315 MediaContentDescription* media_desc,
316 SdpParseError* error);
317static bool ParseIceOptions(const std::string& line,
318 std::vector<std::string>* transport_options,
319 SdpParseError* error);
320static bool ParseExtmap(const std::string& line,
isheriff6f8d6862016-05-26 11:24:55 -0700321 RtpExtension* extmap,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000322 SdpParseError* error);
323static bool ParseFingerprintAttribute(const std::string& line,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000324 rtc::SSLFingerprint** fingerprint,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000325 SdpParseError* error);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +0000326static bool ParseDtlsSetup(const std::string& line,
327 cricket::ConnectionRole* role,
328 SdpParseError* error);
deadbeef9d3584c2016-02-16 17:54:10 -0800329static bool ParseMsidAttribute(const std::string& line,
Seth Hampson5b4f0752018-04-02 16:31:36 -0700330 std::vector<std::string>* stream_ids,
deadbeef9d3584c2016-02-16 17:54:10 -0800331 std::string* track_id,
332 SdpParseError* error);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000333
334// Helper functions
335
336// Below ParseFailed*** functions output the line that caused the parsing
337// failure and the detailed reason (|description|) of the failure to |error|.
338// The functions always return false so that they can be used directly in the
339// following way when error happens:
340// "return ParseFailed***(...);"
341
342// The line starting at |line_start| of |message| is the failing line.
343// The reason for the failure should be provided in the |description|.
344// An example of a description could be "unknown character".
345static bool ParseFailed(const std::string& message,
346 size_t line_start,
347 const std::string& description,
348 SdpParseError* error) {
349 // Get the first line of |message| from |line_start|.
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +0000350 std::string first_line;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000351 size_t line_end = message.find(kNewLine, line_start);
352 if (line_end != std::string::npos) {
353 if (line_end > 0 && (message.at(line_end - 1) == kReturn)) {
354 --line_end;
355 }
356 first_line = message.substr(line_start, (line_end - line_start));
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +0000357 } else {
358 first_line = message.substr(line_start);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000359 }
360
361 if (error) {
362 error->line = first_line;
363 error->description = description;
364 }
Mirko Bonadei675513b2017-11-09 11:09:25 +0100365 RTC_LOG(LS_ERROR) << "Failed to parse: \"" << first_line
366 << "\". Reason: " << description;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000367 return false;
368}
369
370// |line| is the failing line. The reason for the failure should be
371// provided in the |description|.
372static bool ParseFailed(const std::string& line,
373 const std::string& description,
374 SdpParseError* error) {
375 return ParseFailed(line, 0, description, error);
376}
377
378// Parses failure where the failing SDP line isn't know or there are multiple
379// failing lines.
380static bool ParseFailed(const std::string& description,
381 SdpParseError* error) {
382 return ParseFailed("", description, error);
383}
384
385// |line| is the failing line. The failure is due to the fact that |line|
386// doesn't have |expected_fields| fields.
387static bool ParseFailedExpectFieldNum(const std::string& line,
388 int expected_fields,
389 SdpParseError* error) {
390 std::ostringstream description;
391 description << "Expects " << expected_fields << " fields.";
392 return ParseFailed(line, description.str(), error);
393}
394
395// |line| is the failing line. The failure is due to the fact that |line| has
396// less than |expected_min_fields| fields.
397static bool ParseFailedExpectMinFieldNum(const std::string& line,
398 int expected_min_fields,
399 SdpParseError* error) {
400 std::ostringstream description;
401 description << "Expects at least " << expected_min_fields << " fields.";
402 return ParseFailed(line, description.str(), error);
403}
404
405// |line| is the failing line. The failure is due to the fact that it failed to
406// get the value of |attribute|.
407static bool ParseFailedGetValue(const std::string& line,
408 const std::string& attribute,
409 SdpParseError* error) {
410 std::ostringstream description;
411 description << "Failed to get the value of attribute: " << attribute;
412 return ParseFailed(line, description.str(), error);
413}
414
415// The line starting at |line_start| of |message| is the failing line. The
416// failure is due to the line type (e.g. the "m" part of the "m-line")
417// not matching what is expected. The expected line type should be
418// provided as |line_type|.
419static bool ParseFailedExpectLine(const std::string& message,
420 size_t line_start,
421 const char line_type,
422 const std::string& line_value,
423 SdpParseError* error) {
424 std::ostringstream description;
425 description << "Expect line: " << line_type << "=" << line_value;
426 return ParseFailed(message, line_start, description.str(), error);
427}
428
429static bool AddLine(const std::string& line, std::string* message) {
430 if (!message)
431 return false;
432
433 message->append(line);
434 message->append(kLineBreak);
435 return true;
436}
437
438static bool GetLine(const std::string& message,
439 size_t* pos,
440 std::string* line) {
441 size_t line_begin = *pos;
442 size_t line_end = message.find(kNewLine, line_begin);
443 if (line_end == std::string::npos) {
444 return false;
445 }
446 // Update the new start position
447 *pos = line_end + 1;
448 if (line_end > 0 && (message.at(line_end - 1) == kReturn)) {
449 --line_end;
450 }
451 *line = message.substr(line_begin, (line_end - line_begin));
452 const char* cline = line->c_str();
453 // RFC 4566
454 // An SDP session description consists of a number of lines of text of
455 // the form:
456 // <type>=<value>
457 // where <type> MUST be exactly one case-significant character and
458 // <value> is structured text whose format depends on <type>.
459 // Whitespace MUST NOT be used on either side of the "=" sign.
Taylor Brandstetter93a7b242018-04-16 10:45:24 -0700460 //
461 // However, an exception to the whitespace rule is made for "s=", since
462 // RFC4566 also says:
463 //
464 // If a session has no meaningful name, the value "s= " SHOULD be used
465 // (i.e., a single space as the session name).
466 if (line->length() < 3 || !islower(cline[0]) ||
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000467 cline[1] != kSdpDelimiterEqual ||
Taylor Brandstetter93a7b242018-04-16 10:45:24 -0700468 (cline[0] != kLineTypeSessionName && cline[2] == kSdpDelimiterSpace)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000469 *pos = line_begin;
470 return false;
471 }
472 return true;
473}
474
475// Init |os| to "|type|=|value|".
476static void InitLine(const char type,
477 const std::string& value,
478 std::ostringstream* os) {
479 os->str("");
480 *os << type << kSdpDelimiterEqual << value;
481}
482
483// Init |os| to "a=|attribute|".
484static void InitAttrLine(const std::string& attribute, std::ostringstream* os) {
485 InitLine(kLineTypeAttributes, attribute, os);
486}
487
488// Writes a SDP attribute line based on |attribute| and |value| to |message|.
489static void AddAttributeLine(const std::string& attribute, int value,
490 std::string* message) {
491 std::ostringstream os;
492 InitAttrLine(attribute, &os);
493 os << kSdpDelimiterColon << value;
494 AddLine(os.str(), message);
495}
496
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000497static bool IsLineType(const std::string& message,
498 const char type,
499 size_t line_start) {
500 if (message.size() < line_start + kLinePrefixLength) {
501 return false;
502 }
503 const char* cmessage = message.c_str();
504 return (cmessage[line_start] == type &&
505 cmessage[line_start + 1] == kSdpDelimiterEqual);
506}
507
508static bool IsLineType(const std::string& line,
509 const char type) {
510 return IsLineType(line, type, 0);
511}
512
513static bool GetLineWithType(const std::string& message, size_t* pos,
514 std::string* line, const char type) {
515 if (!IsLineType(message, type, *pos)) {
516 return false;
517 }
518
519 if (!GetLine(message, pos, line))
520 return false;
521
522 return true;
523}
524
525static bool HasAttribute(const std::string& line,
526 const std::string& attribute) {
527 return (line.compare(kLinePrefixLength, attribute.size(), attribute) == 0);
528}
529
Peter Boström0c4e06b2015-10-07 12:23:21 +0200530static bool AddSsrcLine(uint32_t ssrc_id,
531 const std::string& attribute,
532 const std::string& value,
533 std::string* message) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000534 // RFC 5576
535 // a=ssrc:<ssrc-id> <attribute>:<value>
536 std::ostringstream os;
537 InitAttrLine(kAttributeSsrc, &os);
538 os << kSdpDelimiterColon << ssrc_id << kSdpDelimiterSpace
539 << attribute << kSdpDelimiterColon << value;
540 return AddLine(os.str(), message);
541}
542
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000543// Get value only from <attribute>:<value>.
544static bool GetValue(const std::string& message, const std::string& attribute,
545 std::string* value, SdpParseError* error) {
546 std::string leftpart;
Donald Curtis0e07f922015-05-15 09:21:23 -0700547 if (!rtc::tokenize_first(message, kSdpDelimiterColon, &leftpart, value)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000548 return ParseFailedGetValue(message, attribute, error);
549 }
550 // The left part should end with the expected attribute.
551 if (leftpart.length() < attribute.length() ||
552 leftpart.compare(leftpart.length() - attribute.length(),
553 attribute.length(), attribute) != 0) {
554 return ParseFailedGetValue(message, attribute, error);
555 }
556 return true;
557}
558
559static bool CaseInsensitiveFind(std::string str1, std::string str2) {
560 std::transform(str1.begin(), str1.end(), str1.begin(),
561 ::tolower);
562 std::transform(str2.begin(), str2.end(), str2.begin(),
563 ::tolower);
564 return str1.find(str2) != std::string::npos;
565}
566
wu@webrtc.org5e760e72014-04-02 23:19:09 +0000567template <class T>
568static bool GetValueFromString(const std::string& line,
569 const std::string& s,
570 T* t,
571 SdpParseError* error) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000572 if (!rtc::FromString(s, t)) {
wu@webrtc.org5e760e72014-04-02 23:19:09 +0000573 std::ostringstream description;
574 description << "Invalid value: " << s << ".";
575 return ParseFailed(line, description.str(), error);
576 }
577 return true;
578}
579
pkasting@chromium.orge9facf82015-02-17 20:36:28 +0000580static bool GetPayloadTypeFromString(const std::string& line,
581 const std::string& s,
582 int* payload_type,
583 SdpParseError* error) {
584 return GetValueFromString(line, s, payload_type, error) &&
585 cricket::IsValidRtpPayloadType(*payload_type);
586}
587
Seth Hampson5897a6e2018-04-03 11:16:33 -0700588// Creates a StreamParams track in the case when no SSRC lines are signaled.
589// This is a track that does not contain SSRCs and only contains
590// stream_ids/track_id if it's signaled with a=msid lines.
591void CreateTrackWithNoSsrcs(const std::vector<std::string>& msid_stream_ids,
592 const std::string& msid_track_id,
593 StreamParamsVec* tracks) {
594 StreamParams track;
595 if (msid_track_id.empty() || msid_stream_ids.empty()) {
596 // We only create an unsignaled track if a=msid lines were signaled.
597 return;
598 }
599 track.set_stream_ids(msid_stream_ids);
600 track.id = msid_track_id;
601 tracks->push_back(track);
602}
603
604// Creates the StreamParams tracks, for the case when SSRC lines are signaled.
605// |msid_stream_ids| and |msid_track_id| represent the stream/track ID from the
Taylor Brandstetter5de6b752016-03-09 17:02:30 -0800606// "a=msid" attribute, if it exists. They are empty if the attribute does not
Seth Hampson5897a6e2018-04-03 11:16:33 -0700607// exist. We prioritize getting stream_ids/track_ids signaled in a=msid lines.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000608void CreateTracksFromSsrcInfos(const SsrcInfoVec& ssrc_infos,
Seth Hampson5b4f0752018-04-02 16:31:36 -0700609 const std::vector<std::string>& msid_stream_ids,
Taylor Brandstetter5de6b752016-03-09 17:02:30 -0800610 const std::string& msid_track_id,
Seth Hampson5b4f0752018-04-02 16:31:36 -0700611 StreamParamsVec* tracks,
612 int msid_signaling) {
nisseede5da42017-01-12 05:15:36 -0800613 RTC_DCHECK(tracks != NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000614 for (SsrcInfoVec::const_iterator ssrc_info = ssrc_infos.begin();
615 ssrc_info != ssrc_infos.end(); ++ssrc_info) {
616 if (ssrc_info->cname.empty()) {
617 continue;
618 }
619
Seth Hampson5b4f0752018-04-02 16:31:36 -0700620 std::vector<std::string> stream_ids;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000621 std::string track_id;
Seth Hampson5b4f0752018-04-02 16:31:36 -0700622 if (msid_signaling & cricket::kMsidSignalingMediaSection) {
623 // This is the case with Unified Plan SDP msid signaling.
624 stream_ids = msid_stream_ids;
Tomas Gunnarsson191bf5c2018-03-30 10:44:43 +0000625 track_id = msid_track_id;
Seth Hampson5b4f0752018-04-02 16:31:36 -0700626 } else if (msid_signaling & cricket::kMsidSignalingSsrcAttribute) {
627 // This is the case with Plan B SDP msid signaling.
628 stream_ids.push_back(ssrc_info->stream_id);
Tomas Gunnarsson191bf5c2018-03-30 10:44:43 +0000629 track_id = ssrc_info->track_id;
Seth Hampson5b4f0752018-04-02 16:31:36 -0700630 } else if (!ssrc_info->mslabel.empty()) {
631 // Since there's no a=msid or a=ssrc msid signaling, this is a sdp from
632 // an older version of client that doesn't support msid.
633 // In that case, we use the mslabel and label to construct the track.
634 stream_ids.push_back(ssrc_info->mslabel);
635 track_id = ssrc_info->label;
636 } else {
637 // Since no media streams isn't supported with older SDP signaling, we
638 // use a default a stream id.
639 stream_ids.push_back(kDefaultMsid);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000640 }
Seth Hampson5b4f0752018-04-02 16:31:36 -0700641 // If a track ID wasn't populated from the SSRC attributes OR the
Taylor Brandstetter5de6b752016-03-09 17:02:30 -0800642 // msid attribute, use default/random values.
Taylor Brandstetter5de6b752016-03-09 17:02:30 -0800643 if (track_id.empty()) {
644 // TODO(ronghuawu): What should we do if the track id doesn't appear?
645 // Create random string (which will be used as track label later)?
646 track_id = rtc::CreateRandomString(8);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000647 }
648
649 StreamParamsVec::iterator track = tracks->begin();
650 for (; track != tracks->end(); ++track) {
651 if (track->id == track_id) {
652 break;
653 }
654 }
655 if (track == tracks->end()) {
656 // If we don't find an existing track, create a new one.
657 tracks->push_back(StreamParams());
658 track = tracks->end() - 1;
659 }
660 track->add_ssrc(ssrc_info->ssrc_id);
661 track->cname = ssrc_info->cname;
Seth Hampson5b4f0752018-04-02 16:31:36 -0700662 track->set_stream_ids(stream_ids);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000663 track->id = track_id;
664 }
665}
666
Seth Hampson845e8782018-03-02 11:34:10 -0800667void GetMediaStreamIds(const ContentInfo* content,
668 std::set<std::string>* labels) {
Steve Antonb1c1de12017-12-21 15:14:30 -0800669 for (const StreamParams& stream_params :
670 content->media_description()->streams()) {
Seth Hampson845e8782018-03-02 11:34:10 -0800671 for (const std::string& stream_id : stream_params.stream_ids()) {
672 labels->insert(stream_id);
Steve Anton5a26a3a2018-02-28 11:38:47 -0800673 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000674 }
675}
676
677// RFC 5245
678// It is RECOMMENDED that default candidates be chosen based on the
679// likelihood of those candidates to work with the peer that is being
680// contacted. It is RECOMMENDED that relayed > reflexive > host.
681static const int kPreferenceUnknown = 0;
682static const int kPreferenceHost = 1;
683static const int kPreferenceReflexive = 2;
684static const int kPreferenceRelayed = 3;
685
686static int GetCandidatePreferenceFromType(const std::string& type) {
687 int preference = kPreferenceUnknown;
688 if (type == cricket::LOCAL_PORT_TYPE) {
689 preference = kPreferenceHost;
690 } else if (type == cricket::STUN_PORT_TYPE) {
691 preference = kPreferenceReflexive;
692 } else if (type == cricket::RELAY_PORT_TYPE) {
693 preference = kPreferenceRelayed;
694 } else {
nissec80e7412017-01-11 05:56:46 -0800695 RTC_NOTREACHED();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000696 }
697 return preference;
698}
699
guoweis@webrtc.org57ac2c82015-02-06 00:45:13 +0000700// Get ip and port of the default destination from the |candidates| with the
701// given value of |component_id|. The default candidate should be the one most
702// likely to work, typically IPv4 relay.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000703// RFC 5245
704// The value of |component_id| currently supported are 1 (RTP) and 2 (RTCP).
Steve Anton36b29d12017-10-30 09:57:42 -0700705// TODO(deadbeef): Decide the default destination in webrtcsession and
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000706// pass it down via SessionDescription.
pthatcher@webrtc.orgc9d6d142014-10-23 23:37:22 +0000707static void GetDefaultDestination(
708 const std::vector<Candidate>& candidates,
709 int component_id, std::string* port,
710 std::string* ip, std::string* addr_type) {
perkj@webrtc.orgd105cc82014-11-07 11:22:06 +0000711 *addr_type = kConnectionIpv4Addrtype;
pthatcher@webrtc.orgc9d6d142014-10-23 23:37:22 +0000712 *port = kDummyPort;
713 *ip = kDummyAddress;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000714 int current_preference = kPreferenceUnknown;
guoweis@webrtc.org57ac2c82015-02-06 00:45:13 +0000715 int current_family = AF_UNSPEC;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000716 for (std::vector<Candidate>::const_iterator it = candidates.begin();
717 it != candidates.end(); ++it) {
718 if (it->component() != component_id) {
719 continue;
720 }
guoweis@webrtc.org57ac2c82015-02-06 00:45:13 +0000721 // Default destination should be UDP only.
722 if (it->protocol() != cricket::UDP_PROTOCOL_NAME) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000723 continue;
724 }
guoweis@webrtc.org57ac2c82015-02-06 00:45:13 +0000725 const int preference = GetCandidatePreferenceFromType(it->type());
726 const int family = it->address().ipaddr().family();
727 // See if this candidate is more preferable then the current one if it's the
728 // same family. Or if the current family is IPv4 already so we could safely
729 // ignore all IPv6 ones. WebRTC bug 4269.
730 // http://code.google.com/p/webrtc/issues/detail?id=4269
731 if ((preference <= current_preference && current_family == family) ||
732 (current_family == AF_INET && family == AF_INET6)) {
733 continue;
734 }
pthatcher@webrtc.orgc9d6d142014-10-23 23:37:22 +0000735 if (family == AF_INET) {
736 addr_type->assign(kConnectionIpv4Addrtype);
737 } else if (family == AF_INET6) {
738 addr_type->assign(kConnectionIpv6Addrtype);
739 }
guoweis@webrtc.org57ac2c82015-02-06 00:45:13 +0000740 current_preference = preference;
741 current_family = family;
742 *port = it->address().PortAsString();
743 *ip = it->address().ipaddr().ToString();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000744 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000745}
746
wu@webrtc.org4c3e9912014-07-16 21:03:13 +0000747// Gets "a=rtcp" line if found default RTCP candidate from |candidates|.
748static std::string GetRtcpLine(const std::vector<Candidate>& candidates) {
pthatcher@webrtc.orgc9d6d142014-10-23 23:37:22 +0000749 std::string rtcp_line, rtcp_port, rtcp_ip, addr_type;
750 GetDefaultDestination(candidates, ICE_CANDIDATE_COMPONENT_RTCP,
751 &rtcp_port, &rtcp_ip, &addr_type);
752 // Found default RTCP candidate.
753 // RFC 5245
754 // If the agent is utilizing RTCP, it MUST encode the RTCP candidate
755 // using the a=rtcp attribute as defined in RFC 3605.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000756
pthatcher@webrtc.orgc9d6d142014-10-23 23:37:22 +0000757 // RFC 3605
758 // rtcp-attribute = "a=rtcp:" port [nettype space addrtype space
759 // connection-address] CRLF
760 std::ostringstream os;
761 InitAttrLine(kAttributeRtcp, &os);
762 os << kSdpDelimiterColon
763 << rtcp_port << " "
764 << kConnectionNettype << " "
765 << addr_type << " "
766 << rtcp_ip;
767 rtcp_line = os.str();
wu@webrtc.org4c3e9912014-07-16 21:03:13 +0000768 return rtcp_line;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000769}
770
771// Get candidates according to the mline index from SessionDescriptionInterface.
772static void GetCandidatesByMindex(const SessionDescriptionInterface& desci,
773 int mline_index,
774 std::vector<Candidate>* candidates) {
775 if (!candidates) {
776 return;
777 }
778 const IceCandidateCollection* cc = desci.candidates(mline_index);
779 for (size_t i = 0; i < cc->count(); ++i) {
780 const IceCandidateInterface* candidate = cc->at(i);
781 candidates->push_back(candidate->candidate());
782 }
783}
784
deadbeef90f1e1e2017-02-10 12:35:05 -0800785static bool IsValidPort(int port) {
786 return port >= 0 && port <= 65535;
787}
788
Steve Antone831b8c2018-02-01 12:22:16 -0800789std::string SdpSerialize(const JsepSessionDescription& jdesc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000790 const cricket::SessionDescription* desc = jdesc.description();
791 if (!desc) {
792 return "";
793 }
794
795 std::string message;
796
797 // Session Description.
798 AddLine(kSessionVersion, &message);
799 // Session Origin
800 // RFC 4566
801 // o=<username> <sess-id> <sess-version> <nettype> <addrtype>
802 // <unicast-address>
803 std::ostringstream os;
804 InitLine(kLineTypeOrigin, kSessionOriginUsername, &os);
jbauch083b73f2015-07-16 02:46:32 -0700805 const std::string& session_id = jdesc.session_id().empty() ?
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000806 kSessionOriginSessionId : jdesc.session_id();
jbauch083b73f2015-07-16 02:46:32 -0700807 const std::string& session_version = jdesc.session_version().empty() ?
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000808 kSessionOriginSessionVersion : jdesc.session_version();
809 os << " " << session_id << " " << session_version << " "
810 << kSessionOriginNettype << " " << kSessionOriginAddrtype << " "
811 << kSessionOriginAddress;
812 AddLine(os.str(), &message);
813 AddLine(kSessionName, &message);
814
815 // Time Description.
816 AddLine(kTimeDescription, &message);
817
818 // Group
819 if (desc->HasGroup(cricket::GROUP_TYPE_BUNDLE)) {
820 std::string group_line = kAttrGroup;
821 const cricket::ContentGroup* group =
822 desc->GetGroupByName(cricket::GROUP_TYPE_BUNDLE);
nisseede5da42017-01-12 05:15:36 -0800823 RTC_DCHECK(group != NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000824 const cricket::ContentNames& content_names = group->content_names();
825 for (cricket::ContentNames::const_iterator it = content_names.begin();
826 it != content_names.end(); ++it) {
827 group_line.append(" ");
828 group_line.append(*it);
829 }
830 AddLine(group_line, &message);
831 }
832
833 // MediaStream semantics
834 InitAttrLine(kAttributeMsidSemantics, &os);
835 os << kSdpDelimiterColon << " " << kMediaStreamSemantic;
wu@webrtc.org4c3e9912014-07-16 21:03:13 +0000836
Seth Hampson845e8782018-03-02 11:34:10 -0800837 std::set<std::string> media_stream_ids;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000838 const ContentInfo* audio_content = GetFirstAudioContent(desc);
839 if (audio_content)
Seth Hampson845e8782018-03-02 11:34:10 -0800840 GetMediaStreamIds(audio_content, &media_stream_ids);
wu@webrtc.org4c3e9912014-07-16 21:03:13 +0000841
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000842 const ContentInfo* video_content = GetFirstVideoContent(desc);
843 if (video_content)
Seth Hampson845e8782018-03-02 11:34:10 -0800844 GetMediaStreamIds(video_content, &media_stream_ids);
wu@webrtc.org4c3e9912014-07-16 21:03:13 +0000845
Seth Hampson845e8782018-03-02 11:34:10 -0800846 for (std::set<std::string>::const_iterator it = media_stream_ids.begin();
847 it != media_stream_ids.end(); ++it) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000848 os << " " << *it;
849 }
850 AddLine(os.str(), &message);
851
wu@webrtc.org4c3e9912014-07-16 21:03:13 +0000852 // Preserve the order of the media contents.
853 int mline_index = -1;
854 for (cricket::ContentInfos::const_iterator it = desc->contents().begin();
855 it != desc->contents().end(); ++it) {
Steve Antonb1c1de12017-12-21 15:14:30 -0800856 const MediaContentDescription* mdesc = it->media_description();
wu@webrtc.org4c3e9912014-07-16 21:03:13 +0000857 std::vector<Candidate> candidates;
858 GetCandidatesByMindex(jdesc, ++mline_index, &candidates);
deadbeef9d3584c2016-02-16 17:54:10 -0800859 BuildMediaDescription(&*it, desc->GetTransportInfoByName(it->name),
Steve Antone831b8c2018-02-01 12:22:16 -0800860 mdesc->type(), candidates, desc->msid_signaling(),
wu@webrtc.org4c3e9912014-07-16 21:03:13 +0000861 &message);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000862 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000863 return message;
864}
865
866// Serializes the passed in IceCandidateInterface to a SDP string.
867// candidate - The candidate to be serialized.
Honghai Zhang7fb69db2016-03-14 11:59:18 -0700868std::string SdpSerializeCandidate(const IceCandidateInterface& candidate) {
869 return SdpSerializeCandidate(candidate.candidate());
870}
871
872// Serializes a cricket Candidate.
873std::string SdpSerializeCandidate(const cricket::Candidate& candidate) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000874 std::string message;
Honghai Zhang7fb69db2016-03-14 11:59:18 -0700875 std::vector<cricket::Candidate> candidates(1, candidate);
honghaiza54a0802015-12-16 18:37:23 -0800876 BuildCandidate(candidates, true, &message);
wu@webrtc.orgec9f5fb2014-06-24 17:05:10 +0000877 // From WebRTC draft section 4.8.1.1 candidate-attribute will be
878 // just candidate:<candidate> not a=candidate:<blah>CRLF
nisseede5da42017-01-12 05:15:36 -0800879 RTC_DCHECK(message.find("a=") == 0);
wu@webrtc.orgec9f5fb2014-06-24 17:05:10 +0000880 message.erase(0, 2);
nisseede5da42017-01-12 05:15:36 -0800881 RTC_DCHECK(message.find(kLineBreak) == message.size() - 2);
wu@webrtc.orgec9f5fb2014-06-24 17:05:10 +0000882 message.resize(message.size() - 2);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000883 return message;
884}
885
886bool SdpDeserialize(const std::string& message,
887 JsepSessionDescription* jdesc,
888 SdpParseError* error) {
889 std::string session_id;
890 std::string session_version;
Peter Thatcher7cbd1882015-09-17 18:54:52 -0700891 TransportDescription session_td("", "");
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000892 RtpHeaderExtensions session_extmaps;
zhihuang38989e52017-03-21 11:04:53 -0700893 rtc::SocketAddress session_connection_addr;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000894 cricket::SessionDescription* desc = new cricket::SessionDescription();
895 std::vector<JsepIceCandidate*> candidates;
896 size_t current_pos = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000897
898 // Session Description
899 if (!ParseSessionDescription(message, &current_pos, &session_id,
deadbeefc80741f2015-10-22 13:14:45 -0700900 &session_version, &session_td, &session_extmaps,
zhihuang38989e52017-03-21 11:04:53 -0700901 &session_connection_addr, desc, error)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000902 delete desc;
903 return false;
904 }
905
906 // Media Description
deadbeefc80741f2015-10-22 13:14:45 -0700907 if (!ParseMediaDescription(message, session_td, session_extmaps, &current_pos,
zhihuang38989e52017-03-21 11:04:53 -0700908 session_connection_addr, desc, &candidates,
909 error)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000910 delete desc;
911 for (std::vector<JsepIceCandidate*>::const_iterator
912 it = candidates.begin(); it != candidates.end(); ++it) {
913 delete *it;
914 }
915 return false;
916 }
917
918 jdesc->Initialize(desc, session_id, session_version);
919
920 for (std::vector<JsepIceCandidate*>::const_iterator
921 it = candidates.begin(); it != candidates.end(); ++it) {
922 jdesc->AddCandidate(*it);
923 delete *it;
924 }
925 return true;
926}
927
928bool SdpDeserializeCandidate(const std::string& message,
929 JsepIceCandidate* jcandidate,
930 SdpParseError* error) {
nisseede5da42017-01-12 05:15:36 -0800931 RTC_DCHECK(jcandidate != NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000932 Candidate candidate;
933 if (!ParseCandidate(message, &candidate, error, true)) {
934 return false;
935 }
936 jcandidate->SetCandidate(candidate);
937 return true;
938}
939
Honghai Zhang7fb69db2016-03-14 11:59:18 -0700940bool SdpDeserializeCandidate(const std::string& transport_name,
941 const std::string& message,
942 cricket::Candidate* candidate,
943 SdpParseError* error) {
nisseede5da42017-01-12 05:15:36 -0800944 RTC_DCHECK(candidate != nullptr);
Honghai Zhang7fb69db2016-03-14 11:59:18 -0700945 if (!ParseCandidate(message, candidate, error, true)) {
946 return false;
947 }
948 candidate->set_transport_name(transport_name);
949 return true;
950}
951
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000952bool ParseCandidate(const std::string& message, Candidate* candidate,
953 SdpParseError* error, bool is_raw) {
nisseede5da42017-01-12 05:15:36 -0800954 RTC_DCHECK(candidate != NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000955
956 // Get the first line from |message|.
jiayl@webrtc.org7ec3f9f2014-08-08 23:09:15 +0000957 std::string first_line = message;
958 size_t pos = 0;
959 GetLine(message, &pos, &first_line);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000960
jiayl@webrtc.org7ec3f9f2014-08-08 23:09:15 +0000961 // Makes sure |message| contains only one line.
962 if (message.size() > first_line.size()) {
963 std::string left, right;
Donald Curtis0e07f922015-05-15 09:21:23 -0700964 if (rtc::tokenize_first(message, kNewLine, &left, &right) &&
965 !right.empty()) {
jiayl@webrtc.org7ec3f9f2014-08-08 23:09:15 +0000966 return ParseFailed(message, 0, "Expect one line only", error);
967 }
968 }
969
970 // From WebRTC draft section 4.8.1.1 candidate-attribute should be
971 // candidate:<candidate> when trickled, but we still support
972 // a=candidate:<blah>CRLF for backward compatibility and for parsing a line
973 // from the SDP.
974 if (IsLineType(first_line, kLineTypeAttributes)) {
975 first_line = first_line.substr(kLinePrefixLength);
976 }
977
978 std::string attribute_candidate;
979 std::string candidate_value;
980
981 // |first_line| must be in the form of "candidate:<value>".
Donald Curtis144d0182015-05-15 13:14:24 -0700982 if (!rtc::tokenize_first(first_line, kSdpDelimiterColon, &attribute_candidate,
983 &candidate_value) ||
jiayl@webrtc.org7ec3f9f2014-08-08 23:09:15 +0000984 attribute_candidate != kAttributeCandidate) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000985 if (is_raw) {
986 std::ostringstream description;
jiayl@webrtc.org7ec3f9f2014-08-08 23:09:15 +0000987 description << "Expect line: " << kAttributeCandidate
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000988 << ":" << "<candidate-str>";
989 return ParseFailed(first_line, 0, description.str(), error);
990 } else {
991 return ParseFailedExpectLine(first_line, 0, kLineTypeAttributes,
992 kAttributeCandidate, error);
993 }
994 }
995
996 std::vector<std::string> fields;
jiayl@webrtc.org7ec3f9f2014-08-08 23:09:15 +0000997 rtc::split(candidate_value, kSdpDelimiterSpace, &fields);
998
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000999 // RFC 5245
1000 // a=candidate:<foundation> <component-id> <transport> <priority>
1001 // <connection-address> <port> typ <candidate-types>
1002 // [raddr <connection-address>] [rport <port>]
1003 // *(SP extension-att-name SP extension-att-value)
1004 const size_t expected_min_fields = 8;
1005 if (fields.size() < expected_min_fields ||
1006 (fields[6] != kAttributeCandidateTyp)) {
1007 return ParseFailedExpectMinFieldNum(first_line, expected_min_fields, error);
1008 }
jbauch083b73f2015-07-16 02:46:32 -07001009 const std::string& foundation = fields[0];
jiayl@webrtc.org7ec3f9f2014-08-08 23:09:15 +00001010
wu@webrtc.org5e760e72014-04-02 23:19:09 +00001011 int component_id = 0;
1012 if (!GetValueFromString(first_line, fields[1], &component_id, error)) {
1013 return false;
1014 }
jbauch083b73f2015-07-16 02:46:32 -07001015 const std::string& transport = fields[2];
Peter Boström0c4e06b2015-10-07 12:23:21 +02001016 uint32_t priority = 0;
wu@webrtc.org5e760e72014-04-02 23:19:09 +00001017 if (!GetValueFromString(first_line, fields[3], &priority, error)) {
1018 return false;
1019 }
jbauch083b73f2015-07-16 02:46:32 -07001020 const std::string& connection_address = fields[4];
wu@webrtc.org5e760e72014-04-02 23:19:09 +00001021 int port = 0;
1022 if (!GetValueFromString(first_line, fields[5], &port, error)) {
1023 return false;
1024 }
deadbeef90f1e1e2017-02-10 12:35:05 -08001025 if (!IsValidPort(port)) {
1026 return ParseFailed(first_line, "Invalid port number.", error);
1027 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001028 SocketAddress address(connection_address, port);
1029
1030 cricket::ProtocolType protocol;
hnslb68cc752016-12-13 10:33:41 -08001031 if (!StringToProto(transport.c_str(), &protocol)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001032 return ParseFailed(first_line, "Unsupported transport type.", error);
1033 }
hnslb68cc752016-12-13 10:33:41 -08001034 switch (protocol) {
1035 case cricket::PROTO_UDP:
1036 case cricket::PROTO_TCP:
1037 case cricket::PROTO_SSLTCP:
1038 // Supported protocol.
1039 break;
1040 default:
1041 return ParseFailed(first_line, "Unsupported transport type.", error);
1042 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001043
1044 std::string candidate_type;
jbauch083b73f2015-07-16 02:46:32 -07001045 const std::string& type = fields[7];
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001046 if (type == kCandidateHost) {
1047 candidate_type = cricket::LOCAL_PORT_TYPE;
1048 } else if (type == kCandidateSrflx) {
1049 candidate_type = cricket::STUN_PORT_TYPE;
1050 } else if (type == kCandidateRelay) {
1051 candidate_type = cricket::RELAY_PORT_TYPE;
Honghai Zhang7fb69db2016-03-14 11:59:18 -07001052 } else if (type == kCandidatePrflx) {
1053 candidate_type = cricket::PRFLX_PORT_TYPE;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001054 } else {
1055 return ParseFailed(first_line, "Unsupported candidate type.", error);
1056 }
1057
1058 size_t current_position = expected_min_fields;
1059 SocketAddress related_address;
1060 // The 2 optional fields for related address
1061 // [raddr <connection-address>] [rport <port>]
1062 if (fields.size() >= (current_position + 2) &&
1063 fields[current_position] == kAttributeCandidateRaddr) {
1064 related_address.SetIP(fields[++current_position]);
1065 ++current_position;
1066 }
1067 if (fields.size() >= (current_position + 2) &&
1068 fields[current_position] == kAttributeCandidateRport) {
wu@webrtc.org5e760e72014-04-02 23:19:09 +00001069 int port = 0;
1070 if (!GetValueFromString(
1071 first_line, fields[++current_position], &port, error)) {
1072 return false;
1073 }
deadbeef90f1e1e2017-02-10 12:35:05 -08001074 if (!IsValidPort(port)) {
1075 return ParseFailed(first_line, "Invalid port number.", error);
1076 }
wu@webrtc.org5e760e72014-04-02 23:19:09 +00001077 related_address.SetPort(port);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001078 ++current_position;
1079 }
1080
mallinath@webrtc.org2d60c5e2014-08-08 22:29:20 +00001081 // If this is a TCP candidate, it has additional extension as defined in
1082 // RFC 6544.
1083 std::string tcptype;
1084 if (fields.size() >= (current_position + 2) &&
1085 fields[current_position] == kTcpCandidateType) {
1086 tcptype = fields[++current_position];
1087 ++current_position;
1088
1089 if (tcptype != cricket::TCPTYPE_ACTIVE_STR &&
1090 tcptype != cricket::TCPTYPE_PASSIVE_STR &&
1091 tcptype != cricket::TCPTYPE_SIMOPEN_STR) {
1092 return ParseFailed(first_line, "Invalid TCP candidate type.", error);
1093 }
1094
1095 if (protocol != cricket::PROTO_TCP) {
1096 return ParseFailed(first_line, "Invalid non-TCP candidate", error);
1097 }
1098 }
1099
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001100 // Extension
honghaiza54a0802015-12-16 18:37:23 -08001101 // Though non-standard, we support the ICE ufrag and pwd being signaled on
1102 // the candidate to avoid issues with confusing which generation a candidate
1103 // belongs to when trickling multiple generations at the same time.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001104 std::string username;
1105 std::string password;
Peter Boström0c4e06b2015-10-07 12:23:21 +02001106 uint32_t generation = 0;
honghaiza0c44ea2016-03-23 16:07:48 -07001107 uint16_t network_id = 0;
1108 uint16_t network_cost = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001109 for (size_t i = current_position; i + 1 < fields.size(); ++i) {
1110 // RFC 5245
1111 // *(SP extension-att-name SP extension-att-value)
1112 if (fields[i] == kAttributeCandidateGeneration) {
wu@webrtc.org5e760e72014-04-02 23:19:09 +00001113 if (!GetValueFromString(first_line, fields[++i], &generation, error)) {
1114 return false;
1115 }
honghaiza54a0802015-12-16 18:37:23 -08001116 } else if (fields[i] == kAttributeCandidateUfrag) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001117 username = fields[++i];
honghaiza54a0802015-12-16 18:37:23 -08001118 } else if (fields[i] == kAttributeCandidatePwd) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001119 password = fields[++i];
honghaiza0c44ea2016-03-23 16:07:48 -07001120 } else if (fields[i] == kAttributeCandidateNetworkId) {
1121 if (!GetValueFromString(first_line, fields[++i], &network_id, error)) {
1122 return false;
1123 }
honghaize1a0c942016-02-16 14:54:56 -08001124 } else if (fields[i] == kAttributeCandidateNetworkCost) {
1125 if (!GetValueFromString(first_line, fields[++i], &network_cost, error)) {
1126 return false;
1127 }
Honghai Zhang351d77b2016-05-20 15:08:29 -07001128 network_cost = std::min(network_cost, rtc::kNetworkCostMax);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001129 } else {
1130 // Skip the unknown extension.
1131 ++i;
1132 }
1133 }
1134
guoweis@webrtc.org61c12472015-01-15 06:53:07 +00001135 *candidate = Candidate(component_id, cricket::ProtoToString(protocol),
guoweis@webrtc.org950c5182014-12-16 23:01:31 +00001136 address, priority, username, password, candidate_type,
honghaiza0c44ea2016-03-23 16:07:48 -07001137 generation, foundation, network_id, network_cost);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001138 candidate->set_related_address(related_address);
mallinath@webrtc.org2d60c5e2014-08-08 22:29:20 +00001139 candidate->set_tcptype(tcptype);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001140 return true;
1141}
1142
1143bool ParseIceOptions(const std::string& line,
1144 std::vector<std::string>* transport_options,
1145 SdpParseError* error) {
1146 std::string ice_options;
1147 if (!GetValue(line, kAttributeIceOption, &ice_options, error)) {
1148 return false;
1149 }
1150 std::vector<std::string> fields;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001151 rtc::split(ice_options, kSdpDelimiterSpace, &fields);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001152 for (size_t i = 0; i < fields.size(); ++i) {
1153 transport_options->push_back(fields[i]);
1154 }
1155 return true;
1156}
1157
jiayl@webrtc.orgddb85ab2014-09-05 16:31:56 +00001158bool ParseSctpPort(const std::string& line,
1159 int* sctp_port,
1160 SdpParseError* error) {
1161 // draft-ietf-mmusic-sctp-sdp-07
1162 // a=sctp-port
1163 std::vector<std::string> fields;
jiayl@webrtc.orgddb85ab2014-09-05 16:31:56 +00001164 const size_t expected_min_fields = 2;
lally69f57602015-10-08 10:15:04 -07001165 rtc::split(line.substr(kLinePrefixLength), kSdpDelimiterColon, &fields);
1166 if (fields.size() < expected_min_fields) {
1167 fields.resize(0);
1168 rtc::split(line.substr(kLinePrefixLength), kSdpDelimiterSpace, &fields);
1169 }
jiayl@webrtc.orgddb85ab2014-09-05 16:31:56 +00001170 if (fields.size() < expected_min_fields) {
1171 return ParseFailedExpectMinFieldNum(line, expected_min_fields, error);
1172 }
1173 if (!rtc::FromString(fields[1], sctp_port)) {
lally69f57602015-10-08 10:15:04 -07001174 return ParseFailed(line, "Invalid sctp port value.", error);
jiayl@webrtc.orgddb85ab2014-09-05 16:31:56 +00001175 }
1176 return true;
1177}
1178
isheriff6f8d6862016-05-26 11:24:55 -07001179bool ParseExtmap(const std::string& line,
1180 RtpExtension* extmap,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001181 SdpParseError* error) {
1182 // RFC 5285
1183 // a=extmap:<value>["/"<direction>] <URI> <extensionattributes>
1184 std::vector<std::string> fields;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001185 rtc::split(line.substr(kLinePrefixLength),
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001186 kSdpDelimiterSpace, &fields);
1187 const size_t expected_min_fields = 2;
1188 if (fields.size() < expected_min_fields) {
1189 return ParseFailedExpectMinFieldNum(line, expected_min_fields, error);
1190 }
1191 std::string uri = fields[1];
1192
1193 std::string value_direction;
1194 if (!GetValue(fields[0], kAttributeExtmap, &value_direction, error)) {
1195 return false;
1196 }
1197 std::vector<std::string> sub_fields;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001198 rtc::split(value_direction, kSdpDelimiterSlash, &sub_fields);
wu@webrtc.org5e760e72014-04-02 23:19:09 +00001199 int value = 0;
1200 if (!GetValueFromString(line, sub_fields[0], &value, error)) {
1201 return false;
1202 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001203
jbauch5869f502017-06-29 12:31:36 -07001204 bool encrypted = false;
1205 if (uri == RtpExtension::kEncryptHeaderExtensionsUri) {
1206 // RFC 6904
Steve Anton36b29d12017-10-30 09:57:42 -07001207 // a=extmap:<value["/"<direction>] urn:ietf:params:rtp-hdrext:encrypt <URI>
1208 // <extensionattributes>
jbauch5869f502017-06-29 12:31:36 -07001209 const size_t expected_min_fields_encrypted = expected_min_fields + 1;
1210 if (fields.size() < expected_min_fields_encrypted) {
1211 return ParseFailedExpectMinFieldNum(line, expected_min_fields_encrypted,
1212 error);
1213 }
1214
1215 encrypted = true;
1216 uri = fields[2];
1217 if (uri == RtpExtension::kEncryptHeaderExtensionsUri) {
1218 return ParseFailed(line, "Recursive encrypted header.", error);
1219 }
1220 }
1221
1222 *extmap = RtpExtension(uri, value, encrypted);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001223 return true;
1224}
1225
1226void BuildMediaDescription(const ContentInfo* content_info,
1227 const TransportInfo* transport_info,
1228 const MediaType media_type,
wu@webrtc.org4c3e9912014-07-16 21:03:13 +00001229 const std::vector<Candidate>& candidates,
Steve Antone831b8c2018-02-01 12:22:16 -08001230 int msid_signaling,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001231 std::string* message) {
nisseede5da42017-01-12 05:15:36 -08001232 RTC_DCHECK(message != NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001233 if (content_info == NULL || message == NULL) {
1234 return;
1235 }
Steve Anton36b29d12017-10-30 09:57:42 -07001236 // TODO(deadbeef): Rethink if we should use sprintfn instead of stringstream.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001237 // According to the style guide, streams should only be used for logging.
1238 // http://google-styleguide.googlecode.com/svn/
1239 // trunk/cppguide.xml?showone=Streams#Streams
1240 std::ostringstream os;
Steve Antonb1c1de12017-12-21 15:14:30 -08001241 const MediaContentDescription* media_desc = content_info->media_description();
1242 RTC_DCHECK(media_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001243
jiayl@webrtc.org9c16c392014-05-01 18:30:30 +00001244 int sctp_port = cricket::kSctpDefaultPort;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001245
1246 // RFC 4566
1247 // m=<media> <port> <proto> <fmt>
1248 // fmt is a list of payload type numbers that MAY be used in the session.
1249 const char* type = NULL;
1250 if (media_type == cricket::MEDIA_TYPE_AUDIO)
1251 type = kMediaTypeAudio;
1252 else if (media_type == cricket::MEDIA_TYPE_VIDEO)
1253 type = kMediaTypeVideo;
1254 else if (media_type == cricket::MEDIA_TYPE_DATA)
1255 type = kMediaTypeData;
1256 else
nissec80e7412017-01-11 05:56:46 -08001257 RTC_NOTREACHED();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001258
1259 std::string fmt;
1260 if (media_type == cricket::MEDIA_TYPE_VIDEO) {
Steve Antonb1c1de12017-12-21 15:14:30 -08001261 const VideoContentDescription* video_desc = media_desc->as_video();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001262 for (std::vector<cricket::VideoCodec>::const_iterator it =
1263 video_desc->codecs().begin();
1264 it != video_desc->codecs().end(); ++it) {
1265 fmt.append(" ");
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001266 fmt.append(rtc::ToString<int>(it->id));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001267 }
1268 } else if (media_type == cricket::MEDIA_TYPE_AUDIO) {
Steve Antonb1c1de12017-12-21 15:14:30 -08001269 const AudioContentDescription* audio_desc = media_desc->as_audio();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001270 for (std::vector<cricket::AudioCodec>::const_iterator it =
1271 audio_desc->codecs().begin();
1272 it != audio_desc->codecs().end(); ++it) {
1273 fmt.append(" ");
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001274 fmt.append(rtc::ToString<int>(it->id));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001275 }
1276 } else if (media_type == cricket::MEDIA_TYPE_DATA) {
Steve Antonb1c1de12017-12-21 15:14:30 -08001277 const DataContentDescription* data_desc = media_desc->as_data();
pthatcher@webrtc.org3341b402015-02-13 21:14:22 +00001278 if (IsDtlsSctp(media_desc->protocol())) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001279 fmt.append(" ");
jiayl@webrtc.org9c16c392014-05-01 18:30:30 +00001280
zstein4b2e0822017-02-17 19:48:38 -08001281 if (data_desc->use_sctpmap()) {
1282 for (std::vector<cricket::DataCodec>::const_iterator it =
1283 data_desc->codecs().begin();
1284 it != data_desc->codecs().end(); ++it) {
1285 if (cricket::CodecNamesEq(it->name,
1286 cricket::kGoogleSctpDataCodecName) &&
1287 it->GetParam(cricket::kCodecParamPort, &sctp_port)) {
1288 break;
1289 }
jiayl@webrtc.org9c16c392014-05-01 18:30:30 +00001290 }
jiayl@webrtc.org9c16c392014-05-01 18:30:30 +00001291
zstein4b2e0822017-02-17 19:48:38 -08001292 fmt.append(rtc::ToString<int>(sctp_port));
1293 } else {
1294 fmt.append(kDefaultSctpmapProtocol);
1295 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001296 } else {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001297 for (std::vector<cricket::DataCodec>::const_iterator it =
1298 data_desc->codecs().begin();
1299 it != data_desc->codecs().end(); ++it) {
1300 fmt.append(" ");
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001301 fmt.append(rtc::ToString<int>(it->id));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001302 }
1303 }
1304 }
1305 // The fmt must never be empty. If no codecs are found, set the fmt attribute
1306 // to 0.
1307 if (fmt.empty()) {
1308 fmt = " 0";
1309 }
1310
deadbeef25ed4352016-12-12 18:37:36 -08001311 // The port number in the m line will be updated later when associated with
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001312 // the candidates.
deadbeef25ed4352016-12-12 18:37:36 -08001313 //
1314 // A port value of 0 indicates that the m= section is rejected.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001315 // RFC 3264
1316 // To reject an offered stream, the port number in the corresponding stream in
1317 // the answer MUST be set to zero.
deadbeef25ed4352016-12-12 18:37:36 -08001318 //
1319 // However, the BUNDLE draft adds a new meaning to port zero, when used along
1320 // with a=bundle-only.
zhihuang38989e52017-03-21 11:04:53 -07001321 std::string port = kDummyPort;
1322 if (content_info->rejected || content_info->bundle_only) {
1323 port = kMediaPortRejected;
1324 } else if (!media_desc->connection_address().IsNil()) {
1325 port = rtc::ToString(media_desc->connection_address().port());
1326 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001327
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001328 rtc::SSLFingerprint* fp = (transport_info) ?
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001329 transport_info->description.identity_fingerprint.get() : NULL;
1330
wu@webrtc.org4c3e9912014-07-16 21:03:13 +00001331 // Add the m and c lines.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001332 InitLine(kLineTypeMedia, type, &os);
1333 os << " " << port << " " << media_desc->protocol() << fmt;
zhihuang38989e52017-03-21 11:04:53 -07001334 AddLine(os.str(), message);
1335
1336 InitLine(kLineTypeConnection, kConnectionNettype, &os);
1337 if (media_desc->connection_address().IsNil()) {
1338 os << " " << kConnectionIpv4Addrtype << " " << kDummyAddress;
1339 } else if (media_desc->connection_address().family() == AF_INET) {
1340 os << " " << kConnectionIpv4Addrtype << " "
1341 << media_desc->connection_address().ipaddr().ToString();
1342 } else {
1343 os << " " << kConnectionIpv6Addrtype << " "
1344 << media_desc->connection_address().ipaddr().ToString();
1345 }
1346 AddLine(os.str(), message);
wu@webrtc.org4c3e9912014-07-16 21:03:13 +00001347
1348 // RFC 4566
1349 // b=AS:<bandwidth>
Peter Thatcherc0c3a862015-06-24 15:31:25 -07001350 if (media_desc->bandwidth() >= 1000) {
wu@webrtc.org4c3e9912014-07-16 21:03:13 +00001351 InitLine(kLineTypeSessionBandwidth, kApplicationSpecificMaximum, &os);
1352 os << kSdpDelimiterColon << (media_desc->bandwidth() / 1000);
1353 AddLine(os.str(), message);
1354 }
1355
deadbeef25ed4352016-12-12 18:37:36 -08001356 // Add the a=bundle-only line.
1357 if (content_info->bundle_only) {
1358 InitAttrLine(kAttributeBundleOnly, &os);
1359 AddLine(os.str(), message);
1360 }
1361
wu@webrtc.org4c3e9912014-07-16 21:03:13 +00001362 // Add the a=rtcp line.
pthatcher@webrtc.org3341b402015-02-13 21:14:22 +00001363 if (IsRtp(media_desc->protocol())) {
wu@webrtc.org4c3e9912014-07-16 21:03:13 +00001364 std::string rtcp_line = GetRtcpLine(candidates);
1365 if (!rtcp_line.empty()) {
1366 AddLine(rtcp_line, message);
1367 }
1368 }
1369
honghaiza54a0802015-12-16 18:37:23 -08001370 // Build the a=candidate lines. We don't include ufrag and pwd in the
1371 // candidates in the SDP to avoid redundancy.
1372 BuildCandidate(candidates, false, message);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001373
1374 // Use the transport_info to build the media level ice-ufrag and ice-pwd.
1375 if (transport_info) {
1376 // RFC 5245
1377 // ice-pwd-att = "ice-pwd" ":" password
1378 // ice-ufrag-att = "ice-ufrag" ":" ufrag
1379 // ice-ufrag
deadbeef3f7219b2015-12-28 15:17:14 -08001380 if (!transport_info->description.ice_ufrag.empty()) {
1381 InitAttrLine(kAttributeIceUfrag, &os);
1382 os << kSdpDelimiterColon << transport_info->description.ice_ufrag;
1383 AddLine(os.str(), message);
1384 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001385 // ice-pwd
deadbeef3f7219b2015-12-28 15:17:14 -08001386 if (!transport_info->description.ice_pwd.empty()) {
1387 InitAttrLine(kAttributeIcePwd, &os);
1388 os << kSdpDelimiterColon << transport_info->description.ice_pwd;
1389 AddLine(os.str(), message);
1390 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001391
1392 // draft-petithuguenin-mmusic-ice-attributes-level-03
1393 BuildIceOptions(transport_info->description.transport_options, message);
1394
1395 // RFC 4572
1396 // fingerprint-attribute =
1397 // "fingerprint" ":" hash-func SP fingerprint
1398 if (fp) {
1399 // Insert the fingerprint attribute.
1400 InitAttrLine(kAttributeFingerprint, &os);
1401 os << kSdpDelimiterColon
1402 << fp->algorithm << kSdpDelimiterSpace
1403 << fp->GetRfc4572Fingerprint();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001404 AddLine(os.str(), message);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001405
1406 // Inserting setup attribute.
1407 if (transport_info->description.connection_role !=
1408 cricket::CONNECTIONROLE_NONE) {
1409 // Making sure we are not using "passive" mode.
1410 cricket::ConnectionRole role =
1411 transport_info->description.connection_role;
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00001412 std::string dtls_role_str;
nissec16fa5e2017-02-07 07:18:43 -08001413 const bool success =
1414 cricket::ConnectionRoleToString(role, &dtls_role_str);
1415 RTC_DCHECK(success);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001416 InitAttrLine(kAttributeSetup, &os);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001417 os << kSdpDelimiterColon << dtls_role_str;
1418 AddLine(os.str(), message);
1419 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001420 }
1421 }
1422
1423 // RFC 3388
1424 // mid-attribute = "a=mid:" identification-tag
1425 // identification-tag = token
1426 // Use the content name as the mid identification-tag.
1427 InitAttrLine(kAttributeMid, &os);
1428 os << kSdpDelimiterColon << content_info->name;
1429 AddLine(os.str(), message);
1430
pthatcher@webrtc.org3341b402015-02-13 21:14:22 +00001431 if (IsDtlsSctp(media_desc->protocol())) {
Steve Antonb1c1de12017-12-21 15:14:30 -08001432 const DataContentDescription* data_desc = media_desc->as_data();
zstein4b2e0822017-02-17 19:48:38 -08001433 bool use_sctpmap = data_desc->use_sctpmap();
1434 BuildSctpContentAttributes(message, sctp_port, use_sctpmap);
pthatcher@webrtc.org3341b402015-02-13 21:14:22 +00001435 } else if (IsRtp(media_desc->protocol())) {
Steve Antone831b8c2018-02-01 12:22:16 -08001436 BuildRtpContentAttributes(media_desc, media_type, msid_signaling, message);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001437 }
1438}
1439
zstein4b2e0822017-02-17 19:48:38 -08001440void BuildSctpContentAttributes(std::string* message,
1441 int sctp_port,
1442 bool use_sctpmap) {
wu@webrtc.org78187522013-10-07 23:32:02 +00001443 std::ostringstream os;
zstein4b2e0822017-02-17 19:48:38 -08001444 if (use_sctpmap) {
1445 // draft-ietf-mmusic-sctp-sdp-04
1446 // a=sctpmap:sctpmap-number protocol [streams]
1447 InitAttrLine(kAttributeSctpmap, &os);
1448 os << kSdpDelimiterColon << sctp_port << kSdpDelimiterSpace
1449 << kDefaultSctpmapProtocol << kSdpDelimiterSpace
1450 << cricket::kMaxSctpStreams;
1451 } else {
1452 // draft-ietf-mmusic-sctp-sdp-23
1453 // a=sctp-port:<port>
1454 InitAttrLine(kAttributeSctpPort, &os);
1455 os << kSdpDelimiterColon << sctp_port;
1456 // TODO(zstein): emit max-message-size here
1457 }
wu@webrtc.org78187522013-10-07 23:32:02 +00001458 AddLine(os.str(), message);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001459}
1460
deadbeef9d3584c2016-02-16 17:54:10 -08001461void BuildRtpContentAttributes(const MediaContentDescription* media_desc,
1462 const MediaType media_type,
Steve Antone831b8c2018-02-01 12:22:16 -08001463 int msid_signaling,
deadbeef9d3584c2016-02-16 17:54:10 -08001464 std::string* message) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001465 std::ostringstream os;
1466 // RFC 5285
1467 // a=extmap:<value>["/"<direction>] <URI> <extensionattributes>
1468 // The definitions MUST be either all session level or all media level. This
1469 // implementation uses all media level.
1470 for (size_t i = 0; i < media_desc->rtp_header_extensions().size(); ++i) {
jbauch5869f502017-06-29 12:31:36 -07001471 const RtpExtension& extension = media_desc->rtp_header_extensions()[i];
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001472 InitAttrLine(kAttributeExtmap, &os);
jbauch5869f502017-06-29 12:31:36 -07001473 os << kSdpDelimiterColon << extension.id;
1474 if (extension.encrypt) {
1475 os << kSdpDelimiterSpace << RtpExtension::kEncryptHeaderExtensionsUri;
1476 }
1477 os << kSdpDelimiterSpace << extension.uri;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001478 AddLine(os.str(), message);
1479 }
1480
1481 // RFC 3264
1482 // a=sendrecv || a=sendonly || a=sendrecv || a=inactive
deadbeefc80741f2015-10-22 13:14:45 -07001483 switch (media_desc->direction()) {
Steve Anton4e70a722017-11-28 14:57:10 -08001484 case RtpTransceiverDirection::kInactive:
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001485 InitAttrLine(kAttributeInactive, &os);
1486 break;
Steve Anton4e70a722017-11-28 14:57:10 -08001487 case RtpTransceiverDirection::kSendOnly:
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001488 InitAttrLine(kAttributeSendOnly, &os);
1489 break;
Steve Anton4e70a722017-11-28 14:57:10 -08001490 case RtpTransceiverDirection::kRecvOnly:
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001491 InitAttrLine(kAttributeRecvOnly, &os);
1492 break;
Steve Anton4e70a722017-11-28 14:57:10 -08001493 case RtpTransceiverDirection::kSendRecv:
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001494 default:
1495 InitAttrLine(kAttributeSendRecv, &os);
1496 break;
1497 }
1498 AddLine(os.str(), message);
1499
Seth Hampson5b4f0752018-04-02 16:31:36 -07001500 // Specified in https://datatracker.ietf.org/doc/draft-ietf-mmusic-msid/16/
1501 // a=msid:<msid-id> <msid-appdata>
1502 // The msid-id is a 1*64 token char representing the media stream id, and the
1503 // msid-appdata is a 1*64 token char representing the track id. There is a
1504 // line for every media stream, with a special msid-id value of "-"
1505 // representing no streams. The value of "msid-appdata" MUST be identical for
1506 // all lines.
Steve Antone831b8c2018-02-01 12:22:16 -08001507 if (msid_signaling & cricket::kMsidSignalingMediaSection) {
1508 const StreamParamsVec& streams = media_desc->streams();
1509 if (streams.size() == 1u) {
1510 const StreamParams& track = streams[0];
Seth Hampson5b4f0752018-04-02 16:31:36 -07001511 std::vector<std::string> stream_ids = track.stream_ids();
1512 if (stream_ids.empty()) {
1513 stream_ids.push_back(kNoStreamMsid);
1514 }
1515 for (const std::string& stream_id : stream_ids) {
1516 InitAttrLine(kAttributeMsid, &os);
1517 os << kSdpDelimiterColon << stream_id << kSdpDelimiterSpace << track.id;
1518 AddLine(os.str(), message);
1519 }
Steve Antone831b8c2018-02-01 12:22:16 -08001520 } else if (streams.size() > 1u) {
1521 RTC_LOG(LS_WARNING)
1522 << "Trying to serialize Unified Plan SDP with more than "
Jonas Olsson45cc8902018-02-13 10:37:07 +01001523 "one track in a media section. Omitting 'a=msid'.";
deadbeef9d3584c2016-02-16 17:54:10 -08001524 }
1525 }
1526
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001527 // RFC 5761
1528 // a=rtcp-mux
1529 if (media_desc->rtcp_mux()) {
1530 InitAttrLine(kAttributeRtcpMux, &os);
1531 AddLine(os.str(), message);
1532 }
1533
deadbeef13871492015-12-09 12:37:51 -08001534 // RFC 5506
1535 // a=rtcp-rsize
1536 if (media_desc->rtcp_reduced_size()) {
1537 InitAttrLine(kAttributeRtcpReducedSize, &os);
1538 AddLine(os.str(), message);
1539 }
1540
deadbeefd45aea82017-09-16 01:24:29 -07001541 if (media_desc->conference_mode()) {
1542 InitAttrLine(kAttributeXGoogleFlag, &os);
1543 os << kSdpDelimiterColon << kValueConference;
1544 AddLine(os.str(), message);
1545 }
1546
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001547 // RFC 4568
1548 // a=crypto:<tag> <crypto-suite> <key-params> [<session-params>]
1549 for (std::vector<CryptoParams>::const_iterator it =
1550 media_desc->cryptos().begin();
1551 it != media_desc->cryptos().end(); ++it) {
1552 InitAttrLine(kAttributeCrypto, &os);
1553 os << kSdpDelimiterColon << it->tag << " " << it->cipher_suite << " "
1554 << it->key_params;
1555 if (!it->session_params.empty()) {
1556 os << " " << it->session_params;
1557 }
1558 AddLine(os.str(), message);
1559 }
1560
1561 // RFC 4566
1562 // a=rtpmap:<payload type> <encoding name>/<clock rate>
1563 // [/<encodingparameters>]
1564 BuildRtpMap(media_desc, media_type, message);
1565
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001566 for (StreamParamsVec::const_iterator track = media_desc->streams().begin();
1567 track != media_desc->streams().end(); ++track) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001568 // Build the ssrc-group lines.
1569 for (size_t i = 0; i < track->ssrc_groups.size(); ++i) {
1570 // RFC 5576
1571 // a=ssrc-group:<semantics> <ssrc-id> ...
1572 if (track->ssrc_groups[i].ssrcs.empty()) {
1573 continue;
1574 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001575 InitAttrLine(kAttributeSsrcGroup, &os);
1576 os << kSdpDelimiterColon << track->ssrc_groups[i].semantics;
Peter Boström0c4e06b2015-10-07 12:23:21 +02001577 std::vector<uint32_t>::const_iterator ssrc =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001578 track->ssrc_groups[i].ssrcs.begin();
1579 for (; ssrc != track->ssrc_groups[i].ssrcs.end(); ++ssrc) {
Peter Boström0c4e06b2015-10-07 12:23:21 +02001580 os << kSdpDelimiterSpace << rtc::ToString<uint32_t>(*ssrc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001581 }
1582 AddLine(os.str(), message);
1583 }
1584 // Build the ssrc lines for each ssrc.
1585 for (size_t i = 0; i < track->ssrcs.size(); ++i) {
Peter Boström0c4e06b2015-10-07 12:23:21 +02001586 uint32_t ssrc = track->ssrcs[i];
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001587 // RFC 5576
1588 // a=ssrc:<ssrc-id> cname:<value>
1589 AddSsrcLine(ssrc, kSsrcAttributeCname,
1590 track->cname, message);
1591
Steve Antone831b8c2018-02-01 12:22:16 -08001592 if (msid_signaling & cricket::kMsidSignalingSsrcAttribute) {
1593 // draft-alvestrand-mmusic-msid-00
1594 // a=ssrc:<ssrc-id> msid:identifier [appdata]
1595 // The appdata consists of the "id" attribute of a MediaStreamTrack,
1596 // which corresponds to the "id" attribute of StreamParams.
Seth Hampson5b4f0752018-04-02 16:31:36 -07001597 // Since a=ssrc msid signaling is used in Plan B SDP semantics, and
1598 // multiple stream ids are not supported for Plan B, we are only adding
1599 // a line for the first media stream id here.
Seth Hampson845e8782018-03-02 11:34:10 -08001600 const std::string& stream_id = track->first_stream_id();
Steve Antone831b8c2018-02-01 12:22:16 -08001601 InitAttrLine(kAttributeSsrc, &os);
1602 os << kSdpDelimiterColon << ssrc << kSdpDelimiterSpace
1603 << kSsrcAttributeMsid << kSdpDelimiterColon << stream_id
1604 << kSdpDelimiterSpace << track->id;
1605 AddLine(os.str(), message);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001606
Steve Antone831b8c2018-02-01 12:22:16 -08001607 // TODO(ronghuawu): Remove below code which is for backward
1608 // compatibility.
1609 // draft-alvestrand-rtcweb-mid-01
1610 // a=ssrc:<ssrc-id> mslabel:<value>
1611 // The label isn't yet defined.
1612 // a=ssrc:<ssrc-id> label:<value>
Seth Hampson5b4f0752018-04-02 16:31:36 -07001613 AddSsrcLine(ssrc, kSsrcAttributeMslabel, stream_id, message);
Steve Antone831b8c2018-02-01 12:22:16 -08001614 AddSsrcLine(ssrc, kSSrcAttributeLabel, track->id, message);
1615 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001616 }
1617 }
1618}
1619
1620void WriteFmtpHeader(int payload_type, std::ostringstream* os) {
1621 // fmtp header: a=fmtp:|payload_type| <parameters>
1622 // Add a=fmtp
1623 InitAttrLine(kAttributeFmtp, os);
1624 // Add :|payload_type|
1625 *os << kSdpDelimiterColon << payload_type;
1626}
1627
1628void WriteRtcpFbHeader(int payload_type, std::ostringstream* os) {
1629 // rtcp-fb header: a=rtcp-fb:|payload_type|
1630 // <parameters>/<ccm <ccm_parameters>>
1631 // Add a=rtcp-fb
1632 InitAttrLine(kAttributeRtcpFb, os);
1633 // Add :
1634 *os << kSdpDelimiterColon;
1635 if (payload_type == kWildcardPayloadType) {
1636 *os << "*";
1637 } else {
1638 *os << payload_type;
1639 }
1640}
1641
1642void WriteFmtpParameter(const std::string& parameter_name,
1643 const std::string& parameter_value,
1644 std::ostringstream* os) {
1645 // fmtp parameters: |parameter_name|=|parameter_value|
1646 *os << parameter_name << kSdpDelimiterEqual << parameter_value;
1647}
1648
1649void WriteFmtpParameters(const cricket::CodecParameterMap& parameters,
1650 std::ostringstream* os) {
1651 for (cricket::CodecParameterMap::const_iterator fmtp = parameters.begin();
1652 fmtp != parameters.end(); ++fmtp) {
hta62a216e2016-04-15 11:02:14 -07001653 // Parameters are a semicolon-separated list, no spaces.
1654 // The list is separated from the header by a space.
1655 if (fmtp == parameters.begin()) {
1656 *os << kSdpDelimiterSpace;
1657 } else {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001658 *os << kSdpDelimiterSemicolon;
1659 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001660 WriteFmtpParameter(fmtp->first, fmtp->second, os);
1661 }
1662}
1663
1664bool IsFmtpParam(const std::string& name) {
ossuaa4b0772017-01-30 07:41:18 -08001665 // RFC 4855, section 3 specifies the mapping of media format parameters to SDP
1666 // parameters. Only ptime, maxptime, channels and rate are placed outside of
1667 // the fmtp line. In WebRTC, channels and rate are already handled separately
1668 // and thus not included in the CodecParameterMap.
1669 return name != kCodecParamPTime && name != kCodecParamMaxPTime;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001670}
1671
1672// Retreives fmtp parameters from |params|, which may contain other parameters
1673// as well, and puts them in |fmtp_parameters|.
1674void GetFmtpParams(const cricket::CodecParameterMap& params,
1675 cricket::CodecParameterMap* fmtp_parameters) {
1676 for (cricket::CodecParameterMap::const_iterator iter = params.begin();
1677 iter != params.end(); ++iter) {
1678 if (IsFmtpParam(iter->first)) {
1679 (*fmtp_parameters)[iter->first] = iter->second;
1680 }
1681 }
1682}
1683
1684template <class T>
1685void AddFmtpLine(const T& codec, std::string* message) {
1686 cricket::CodecParameterMap fmtp_parameters;
1687 GetFmtpParams(codec.params, &fmtp_parameters);
1688 if (fmtp_parameters.empty()) {
1689 // No need to add an fmtp if it will have no (optional) parameters.
1690 return;
1691 }
1692 std::ostringstream os;
1693 WriteFmtpHeader(codec.id, &os);
1694 WriteFmtpParameters(fmtp_parameters, &os);
1695 AddLine(os.str(), message);
1696 return;
1697}
1698
1699template <class T>
1700void AddRtcpFbLines(const T& codec, std::string* message) {
1701 for (std::vector<cricket::FeedbackParam>::const_iterator iter =
1702 codec.feedback_params.params().begin();
1703 iter != codec.feedback_params.params().end(); ++iter) {
1704 std::ostringstream os;
1705 WriteRtcpFbHeader(codec.id, &os);
1706 os << " " << iter->id();
1707 if (!iter->param().empty()) {
1708 os << " " << iter->param();
1709 }
1710 AddLine(os.str(), message);
1711 }
1712}
1713
jiayl@webrtc.orgddb85ab2014-09-05 16:31:56 +00001714bool AddSctpDataCodec(DataContentDescription* media_desc,
1715 int sctp_port) {
solenberg9fa49752016-10-08 13:02:44 -07001716 for (const auto& codec : media_desc->codecs()) {
1717 if (cricket::CodecNamesEq(codec.name, cricket::kGoogleSctpDataCodecName)) {
1718 return ParseFailed("",
1719 "Can't have multiple sctp port attributes.",
1720 NULL);
1721 }
jiayl@webrtc.orgddb85ab2014-09-05 16:31:56 +00001722 }
1723 // Add the SCTP Port number as a pseudo-codec "port" parameter
solenberg9fa49752016-10-08 13:02:44 -07001724 cricket::DataCodec codec_port(cricket::kGoogleSctpDataCodecPlType,
deadbeef67cf2c12016-04-13 10:07:16 -07001725 cricket::kGoogleSctpDataCodecName);
jiayl@webrtc.orgddb85ab2014-09-05 16:31:56 +00001726 codec_port.SetParam(cricket::kCodecParamPort, sctp_port);
Mirko Bonadei675513b2017-11-09 11:09:25 +01001727 RTC_LOG(INFO) << "AddSctpDataCodec: Got SCTP Port Number " << sctp_port;
jiayl@webrtc.orgddb85ab2014-09-05 16:31:56 +00001728 media_desc->AddCodec(codec_port);
1729 return true;
1730}
1731
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001732bool GetMinValue(const std::vector<int>& values, int* value) {
1733 if (values.empty()) {
1734 return false;
1735 }
1736 std::vector<int>::const_iterator found =
1737 std::min_element(values.begin(), values.end());
1738 *value = *found;
1739 return true;
1740}
1741
1742bool GetParameter(const std::string& name,
1743 const cricket::CodecParameterMap& params, int* value) {
1744 std::map<std::string, std::string>::const_iterator found =
1745 params.find(name);
1746 if (found == params.end()) {
1747 return false;
1748 }
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001749 if (!rtc::FromString(found->second, value)) {
wu@webrtc.org5e760e72014-04-02 23:19:09 +00001750 return false;
1751 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001752 return true;
1753}
1754
1755void BuildRtpMap(const MediaContentDescription* media_desc,
1756 const MediaType media_type,
1757 std::string* message) {
nisseede5da42017-01-12 05:15:36 -08001758 RTC_DCHECK(message != NULL);
1759 RTC_DCHECK(media_desc != NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001760 std::ostringstream os;
1761 if (media_type == cricket::MEDIA_TYPE_VIDEO) {
Steve Antonb1c1de12017-12-21 15:14:30 -08001762 const VideoContentDescription* video_desc = media_desc->as_video();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001763 for (std::vector<cricket::VideoCodec>::const_iterator it =
1764 video_desc->codecs().begin();
1765 it != video_desc->codecs().end(); ++it) {
1766 // RFC 4566
1767 // a=rtpmap:<payload type> <encoding name>/<clock rate>
1768 // [/<encodingparameters>]
1769 if (it->id != kWildcardPayloadType) {
1770 InitAttrLine(kAttributeRtpmap, &os);
deadbeefe814a0d2017-02-25 18:15:09 -08001771 os << kSdpDelimiterColon << it->id << " " << it->name << "/"
1772 << cricket::kVideoCodecClockrate;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001773 AddLine(os.str(), message);
1774 }
1775 AddRtcpFbLines(*it, message);
1776 AddFmtpLine(*it, message);
1777 }
1778 } else if (media_type == cricket::MEDIA_TYPE_AUDIO) {
Steve Antonb1c1de12017-12-21 15:14:30 -08001779 const AudioContentDescription* audio_desc = media_desc->as_audio();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001780 std::vector<int> ptimes;
1781 std::vector<int> maxptimes;
1782 int max_minptime = 0;
1783 for (std::vector<cricket::AudioCodec>::const_iterator it =
1784 audio_desc->codecs().begin();
1785 it != audio_desc->codecs().end(); ++it) {
nisseede5da42017-01-12 05:15:36 -08001786 RTC_DCHECK(!it->name.empty());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001787 // RFC 4566
1788 // a=rtpmap:<payload type> <encoding name>/<clock rate>
1789 // [/<encodingparameters>]
1790 InitAttrLine(kAttributeRtpmap, &os);
1791 os << kSdpDelimiterColon << it->id << " ";
1792 os << it->name << "/" << it->clockrate;
1793 if (it->channels != 1) {
1794 os << "/" << it->channels;
1795 }
1796 AddLine(os.str(), message);
1797 AddRtcpFbLines(*it, message);
1798 AddFmtpLine(*it, message);
1799 int minptime = 0;
1800 if (GetParameter(kCodecParamMinPTime, it->params, &minptime)) {
1801 max_minptime = std::max(minptime, max_minptime);
1802 }
1803 int ptime;
1804 if (GetParameter(kCodecParamPTime, it->params, &ptime)) {
1805 ptimes.push_back(ptime);
1806 }
1807 int maxptime;
1808 if (GetParameter(kCodecParamMaxPTime, it->params, &maxptime)) {
1809 maxptimes.push_back(maxptime);
1810 }
1811 }
1812 // Populate the maxptime attribute with the smallest maxptime of all codecs
1813 // under the same m-line.
1814 int min_maxptime = INT_MAX;
1815 if (GetMinValue(maxptimes, &min_maxptime)) {
1816 AddAttributeLine(kCodecParamMaxPTime, min_maxptime, message);
1817 }
nisseede5da42017-01-12 05:15:36 -08001818 RTC_DCHECK(min_maxptime > max_minptime);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001819 // Populate the ptime attribute with the smallest ptime or the largest
1820 // minptime, whichever is the largest, for all codecs under the same m-line.
1821 int ptime = INT_MAX;
1822 if (GetMinValue(ptimes, &ptime)) {
1823 ptime = std::min(ptime, min_maxptime);
1824 ptime = std::max(ptime, max_minptime);
1825 AddAttributeLine(kCodecParamPTime, ptime, message);
1826 }
1827 } else if (media_type == cricket::MEDIA_TYPE_DATA) {
Steve Antonb1c1de12017-12-21 15:14:30 -08001828 const DataContentDescription* data_desc = media_desc->as_data();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001829 for (std::vector<cricket::DataCodec>::const_iterator it =
1830 data_desc->codecs().begin();
1831 it != data_desc->codecs().end(); ++it) {
1832 // RFC 4566
1833 // a=rtpmap:<payload type> <encoding name>/<clock rate>
1834 // [/<encodingparameters>]
1835 InitAttrLine(kAttributeRtpmap, &os);
1836 os << kSdpDelimiterColon << it->id << " "
1837 << it->name << "/" << it->clockrate;
1838 AddLine(os.str(), message);
1839 }
1840 }
1841}
1842
1843void BuildCandidate(const std::vector<Candidate>& candidates,
honghaiza54a0802015-12-16 18:37:23 -08001844 bool include_ufrag,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001845 std::string* message) {
1846 std::ostringstream os;
1847
1848 for (std::vector<Candidate>::const_iterator it = candidates.begin();
1849 it != candidates.end(); ++it) {
1850 // RFC 5245
1851 // a=candidate:<foundation> <component-id> <transport> <priority>
1852 // <connection-address> <port> typ <candidate-types>
1853 // [raddr <connection-address>] [rport <port>]
1854 // *(SP extension-att-name SP extension-att-value)
1855 std::string type;
1856 // Map the cricket candidate type to "host" / "srflx" / "prflx" / "relay"
1857 if (it->type() == cricket::LOCAL_PORT_TYPE) {
1858 type = kCandidateHost;
1859 } else if (it->type() == cricket::STUN_PORT_TYPE) {
1860 type = kCandidateSrflx;
1861 } else if (it->type() == cricket::RELAY_PORT_TYPE) {
1862 type = kCandidateRelay;
Honghai Zhang7fb69db2016-03-14 11:59:18 -07001863 } else if (it->type() == cricket::PRFLX_PORT_TYPE) {
1864 type = kCandidatePrflx;
1865 // Peer reflexive candidate may be signaled for being removed.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001866 } else {
nissec80e7412017-01-11 05:56:46 -08001867 RTC_NOTREACHED();
Peter Thatcher019087f2015-04-28 09:06:26 -07001868 // Never write out candidates if we don't know the type.
1869 continue;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001870 }
1871
1872 InitAttrLine(kAttributeCandidate, &os);
1873 os << kSdpDelimiterColon
mallinath@webrtc.org2d60c5e2014-08-08 22:29:20 +00001874 << it->foundation() << " "
1875 << it->component() << " "
1876 << it->protocol() << " "
1877 << it->priority() << " "
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001878 << it->address().ipaddr().ToString() << " "
1879 << it->address().PortAsString() << " "
mallinath@webrtc.org2d60c5e2014-08-08 22:29:20 +00001880 << kAttributeCandidateTyp << " "
1881 << type << " ";
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001882
1883 // Related address
1884 if (!it->related_address().IsNil()) {
1885 os << kAttributeCandidateRaddr << " "
1886 << it->related_address().ipaddr().ToString() << " "
1887 << kAttributeCandidateRport << " "
1888 << it->related_address().PortAsString() << " ";
1889 }
1890
mallinath@webrtc.org2d60c5e2014-08-08 22:29:20 +00001891 if (it->protocol() == cricket::TCP_PROTOCOL_NAME) {
mallinath@webrtc.orge999bd02014-08-13 06:05:55 +00001892 os << kTcpCandidateType << " " << it->tcptype() << " ";
mallinath@webrtc.org2d60c5e2014-08-08 22:29:20 +00001893 }
1894
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001895 // Extensions
1896 os << kAttributeCandidateGeneration << " " << it->generation();
honghaiza54a0802015-12-16 18:37:23 -08001897 if (include_ufrag && !it->username().empty()) {
1898 os << " " << kAttributeCandidateUfrag << " " << it->username();
1899 }
honghaiza0c44ea2016-03-23 16:07:48 -07001900 if (it->network_id() > 0) {
1901 os << " " << kAttributeCandidateNetworkId << " " << it->network_id();
1902 }
honghaize1a0c942016-02-16 14:54:56 -08001903 if (it->network_cost() > 0) {
1904 os << " " << kAttributeCandidateNetworkCost << " " << it->network_cost();
1905 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001906
1907 AddLine(os.str(), message);
1908 }
1909}
1910
1911void BuildIceOptions(const std::vector<std::string>& transport_options,
1912 std::string* message) {
1913 if (!transport_options.empty()) {
1914 std::ostringstream os;
1915 InitAttrLine(kAttributeIceOption, &os);
1916 os << kSdpDelimiterColon << transport_options[0];
1917 for (size_t i = 1; i < transport_options.size(); ++i) {
1918 os << kSdpDelimiterSpace << transport_options[i];
1919 }
1920 AddLine(os.str(), message);
1921 }
1922}
1923
pthatcher@webrtc.org3341b402015-02-13 21:14:22 +00001924bool IsRtp(const std::string& protocol) {
1925 return protocol.empty() ||
1926 (protocol.find(cricket::kMediaProtocolRtpPrefix) != std::string::npos);
1927}
1928
1929bool IsDtlsSctp(const std::string& protocol) {
1930 // This intentionally excludes "SCTP" and "SCTP/DTLS".
lally@webrtc.orga7470932015-02-24 20:19:21 +00001931 return protocol.find(cricket::kMediaProtocolDtlsSctp) != std::string::npos;
pthatcher@webrtc.org3341b402015-02-13 21:14:22 +00001932}
1933
zhihuang38989e52017-03-21 11:04:53 -07001934bool ParseConnectionData(const std::string& line,
1935 rtc::SocketAddress* addr,
1936 SdpParseError* error) {
1937 // Parse the line from left to right.
1938 std::string token;
1939 std::string rightpart;
1940 // RFC 4566
1941 // c=<nettype> <addrtype> <connection-address>
1942 // Skip the "c="
1943 if (!rtc::tokenize_first(line, kSdpDelimiterEqual, &token, &rightpart)) {
1944 return ParseFailed(line, "Failed to parse the network type.", error);
1945 }
1946
1947 // Extract and verify the <nettype>
1948 if (!rtc::tokenize_first(rightpart, kSdpDelimiterSpace, &token, &rightpart) ||
1949 token != kConnectionNettype) {
1950 return ParseFailed(line,
1951 "Failed to parse the connection data. The network type "
1952 "is not currently supported.",
1953 error);
1954 }
1955
1956 // Extract the "<addrtype>" and "<connection-address>".
1957 if (!rtc::tokenize_first(rightpart, kSdpDelimiterSpace, &token, &rightpart)) {
1958 return ParseFailed(line, "Failed to parse the address type.", error);
1959 }
1960
1961 // The rightpart part should be the IP address without the slash which is used
1962 // for multicast.
1963 if (rightpart.find('/') != std::string::npos) {
1964 return ParseFailed(line,
1965 "Failed to parse the connection data. Multicast is not "
1966 "currently supported.",
1967 error);
1968 }
1969 addr->SetIP(rightpart);
1970
1971 // Verify that the addrtype matches the type of the parsed address.
1972 if ((addr->family() == AF_INET && token != "IP4") ||
1973 (addr->family() == AF_INET6 && token != "IP6")) {
1974 addr->Clear();
1975 return ParseFailed(
1976 line,
1977 "Failed to parse the connection data. The address type is mismatching.",
1978 error);
1979 }
1980 return true;
1981}
1982
1983bool ParseSessionDescription(const std::string& message,
1984 size_t* pos,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001985 std::string* session_id,
1986 std::string* session_version,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001987 TransportDescription* session_td,
1988 RtpHeaderExtensions* session_extmaps,
zhihuang38989e52017-03-21 11:04:53 -07001989 rtc::SocketAddress* connection_addr,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001990 cricket::SessionDescription* desc,
1991 SdpParseError* error) {
1992 std::string line;
1993
deadbeefc80741f2015-10-22 13:14:45 -07001994 desc->set_msid_supported(false);
1995
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001996 // RFC 4566
1997 // v= (protocol version)
1998 if (!GetLineWithType(message, pos, &line, kLineTypeVersion)) {
1999 return ParseFailedExpectLine(message, *pos, kLineTypeVersion,
2000 std::string(), error);
2001 }
2002 // RFC 4566
2003 // o=<username> <sess-id> <sess-version> <nettype> <addrtype>
2004 // <unicast-address>
2005 if (!GetLineWithType(message, pos, &line, kLineTypeOrigin)) {
2006 return ParseFailedExpectLine(message, *pos, kLineTypeOrigin,
2007 std::string(), error);
2008 }
2009 std::vector<std::string> fields;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002010 rtc::split(line.substr(kLinePrefixLength),
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002011 kSdpDelimiterSpace, &fields);
2012 const size_t expected_fields = 6;
2013 if (fields.size() != expected_fields) {
2014 return ParseFailedExpectFieldNum(line, expected_fields, error);
2015 }
2016 *session_id = fields[1];
2017 *session_version = fields[2];
2018
2019 // RFC 4566
2020 // s= (session name)
2021 if (!GetLineWithType(message, pos, &line, kLineTypeSessionName)) {
2022 return ParseFailedExpectLine(message, *pos, kLineTypeSessionName,
2023 std::string(), error);
2024 }
2025
2026 // Optional lines
2027 // Those are the optional lines, so shouldn't return false if not present.
2028 // RFC 4566
2029 // i=* (session information)
2030 GetLineWithType(message, pos, &line, kLineTypeSessionInfo);
2031
2032 // RFC 4566
2033 // u=* (URI of description)
2034 GetLineWithType(message, pos, &line, kLineTypeSessionUri);
2035
2036 // RFC 4566
2037 // e=* (email address)
2038 GetLineWithType(message, pos, &line, kLineTypeSessionEmail);
2039
2040 // RFC 4566
2041 // p=* (phone number)
2042 GetLineWithType(message, pos, &line, kLineTypeSessionPhone);
2043
2044 // RFC 4566
2045 // c=* (connection information -- not required if included in
2046 // all media)
zhihuang38989e52017-03-21 11:04:53 -07002047 if (GetLineWithType(message, pos, &line, kLineTypeConnection)) {
2048 if (!ParseConnectionData(line, connection_addr, error)) {
2049 return false;
2050 }
2051 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002052
2053 // RFC 4566
2054 // b=* (zero or more bandwidth information lines)
2055 while (GetLineWithType(message, pos, &line, kLineTypeSessionBandwidth)) {
2056 // By pass zero or more b lines.
2057 }
2058
2059 // RFC 4566
2060 // One or more time descriptions ("t=" and "r=" lines; see below)
2061 // t= (time the session is active)
2062 // r=* (zero or more repeat times)
2063 // Ensure there's at least one time description
2064 if (!GetLineWithType(message, pos, &line, kLineTypeTiming)) {
2065 return ParseFailedExpectLine(message, *pos, kLineTypeTiming, std::string(),
2066 error);
2067 }
2068
2069 while (GetLineWithType(message, pos, &line, kLineTypeRepeatTimes)) {
2070 // By pass zero or more r lines.
2071 }
2072
2073 // Go through the rest of the time descriptions
2074 while (GetLineWithType(message, pos, &line, kLineTypeTiming)) {
2075 while (GetLineWithType(message, pos, &line, kLineTypeRepeatTimes)) {
2076 // By pass zero or more r lines.
2077 }
2078 }
2079
2080 // RFC 4566
2081 // z=* (time zone adjustments)
2082 GetLineWithType(message, pos, &line, kLineTypeTimeZone);
2083
2084 // RFC 4566
2085 // k=* (encryption key)
2086 GetLineWithType(message, pos, &line, kLineTypeEncryptionKey);
2087
2088 // RFC 4566
2089 // a=* (zero or more session attribute lines)
2090 while (GetLineWithType(message, pos, &line, kLineTypeAttributes)) {
2091 if (HasAttribute(line, kAttributeGroup)) {
2092 if (!ParseGroupAttribute(line, desc, error)) {
2093 return false;
2094 }
2095 } else if (HasAttribute(line, kAttributeIceUfrag)) {
2096 if (!GetValue(line, kAttributeIceUfrag,
2097 &(session_td->ice_ufrag), error)) {
2098 return false;
2099 }
2100 } else if (HasAttribute(line, kAttributeIcePwd)) {
2101 if (!GetValue(line, kAttributeIcePwd, &(session_td->ice_pwd), error)) {
2102 return false;
2103 }
2104 } else if (HasAttribute(line, kAttributeIceLite)) {
2105 session_td->ice_mode = cricket::ICEMODE_LITE;
2106 } else if (HasAttribute(line, kAttributeIceOption)) {
2107 if (!ParseIceOptions(line, &(session_td->transport_options), error)) {
2108 return false;
2109 }
2110 } else if (HasAttribute(line, kAttributeFingerprint)) {
2111 if (session_td->identity_fingerprint.get()) {
2112 return ParseFailed(
2113 line,
2114 "Can't have multiple fingerprint attributes at the same level.",
2115 error);
2116 }
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002117 rtc::SSLFingerprint* fingerprint = NULL;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002118 if (!ParseFingerprintAttribute(line, &fingerprint, error)) {
2119 return false;
2120 }
2121 session_td->identity_fingerprint.reset(fingerprint);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00002122 } else if (HasAttribute(line, kAttributeSetup)) {
2123 if (!ParseDtlsSetup(line, &(session_td->connection_role), error)) {
2124 return false;
2125 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002126 } else if (HasAttribute(line, kAttributeMsidSemantics)) {
2127 std::string semantics;
2128 if (!GetValue(line, kAttributeMsidSemantics, &semantics, error)) {
2129 return false;
2130 }
deadbeefc80741f2015-10-22 13:14:45 -07002131 desc->set_msid_supported(
2132 CaseInsensitiveFind(semantics, kMediaStreamSemantic));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002133 } else if (HasAttribute(line, kAttributeExtmap)) {
isheriff6f8d6862016-05-26 11:24:55 -07002134 RtpExtension extmap;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002135 if (!ParseExtmap(line, &extmap, error)) {
2136 return false;
2137 }
2138 session_extmaps->push_back(extmap);
2139 }
2140 }
2141
2142 return true;
2143}
2144
2145bool ParseGroupAttribute(const std::string& line,
2146 cricket::SessionDescription* desc,
2147 SdpParseError* error) {
nisseede5da42017-01-12 05:15:36 -08002148 RTC_DCHECK(desc != NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002149
2150 // RFC 5888 and draft-holmberg-mmusic-sdp-bundle-negotiation-00
2151 // a=group:BUNDLE video voice
2152 std::vector<std::string> fields;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002153 rtc::split(line.substr(kLinePrefixLength),
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002154 kSdpDelimiterSpace, &fields);
2155 std::string semantics;
2156 if (!GetValue(fields[0], kAttributeGroup, &semantics, error)) {
2157 return false;
2158 }
2159 cricket::ContentGroup group(semantics);
2160 for (size_t i = 1; i < fields.size(); ++i) {
2161 group.AddContentName(fields[i]);
2162 }
2163 desc->AddGroup(group);
2164 return true;
2165}
2166
2167static bool ParseFingerprintAttribute(const std::string& line,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002168 rtc::SSLFingerprint** fingerprint,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002169 SdpParseError* error) {
2170 if (!IsLineType(line, kLineTypeAttributes) ||
2171 !HasAttribute(line, kAttributeFingerprint)) {
2172 return ParseFailedExpectLine(line, 0, kLineTypeAttributes,
2173 kAttributeFingerprint, error);
2174 }
2175
2176 std::vector<std::string> fields;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002177 rtc::split(line.substr(kLinePrefixLength),
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002178 kSdpDelimiterSpace, &fields);
2179 const size_t expected_fields = 2;
2180 if (fields.size() != expected_fields) {
2181 return ParseFailedExpectFieldNum(line, expected_fields, error);
2182 }
2183
2184 // The first field here is "fingerprint:<hash>.
2185 std::string algorithm;
2186 if (!GetValue(fields[0], kAttributeFingerprint, &algorithm, error)) {
2187 return false;
2188 }
2189
2190 // Downcase the algorithm. Note that we don't need to downcase the
2191 // fingerprint because hex_decode can handle upper-case.
2192 std::transform(algorithm.begin(), algorithm.end(), algorithm.begin(),
2193 ::tolower);
2194
2195 // The second field is the digest value. De-hexify it.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002196 *fingerprint = rtc::SSLFingerprint::CreateFromRfc4572(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002197 algorithm, fields[1]);
2198 if (!*fingerprint) {
2199 return ParseFailed(line,
2200 "Failed to create fingerprint from the digest.",
2201 error);
2202 }
2203
2204 return true;
2205}
2206
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00002207static bool ParseDtlsSetup(const std::string& line,
2208 cricket::ConnectionRole* role,
2209 SdpParseError* error) {
2210 // setup-attr = "a=setup:" role
2211 // role = "active" / "passive" / "actpass" / "holdconn"
2212 std::vector<std::string> fields;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002213 rtc::split(line.substr(kLinePrefixLength), kSdpDelimiterColon, &fields);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00002214 const size_t expected_fields = 2;
2215 if (fields.size() != expected_fields) {
2216 return ParseFailedExpectFieldNum(line, expected_fields, error);
2217 }
2218 std::string role_str = fields[1];
2219 if (!cricket::StringToConnectionRole(role_str, role)) {
2220 return ParseFailed(line, "Invalid attribute value.", error);
2221 }
2222 return true;
2223}
2224
deadbeef9d3584c2016-02-16 17:54:10 -08002225static bool ParseMsidAttribute(const std::string& line,
Seth Hampson5b4f0752018-04-02 16:31:36 -07002226 std::vector<std::string>* stream_ids,
deadbeef9d3584c2016-02-16 17:54:10 -08002227 std::string* track_id,
2228 SdpParseError* error) {
Seth Hampson5b4f0752018-04-02 16:31:36 -07002229 // https://datatracker.ietf.org/doc/draft-ietf-mmusic-msid/16/
deadbeef9d3584c2016-02-16 17:54:10 -08002230 // a=msid:<stream id> <track id>
2231 // msid-value = msid-id [ SP msid-appdata ]
2232 // msid-id = 1*64token-char ; see RFC 4566
2233 // msid-appdata = 1*64token-char ; see RFC 4566
2234 std::string field1;
Seth Hampson5b4f0752018-04-02 16:31:36 -07002235 std::string new_stream_id;
2236 std::string new_track_id;
deadbeef9d3584c2016-02-16 17:54:10 -08002237 if (!rtc::tokenize_first(line.substr(kLinePrefixLength), kSdpDelimiterSpace,
Seth Hampson5b4f0752018-04-02 16:31:36 -07002238 &field1, &new_track_id)) {
deadbeef9d3584c2016-02-16 17:54:10 -08002239 const size_t expected_fields = 2;
2240 return ParseFailedExpectFieldNum(line, expected_fields, error);
2241 }
2242
Seth Hampson5b4f0752018-04-02 16:31:36 -07002243 if (new_track_id.empty()) {
deadbeefa4549d62017-02-10 17:26:22 -08002244 return ParseFailed(line, "Missing track ID in msid attribute.", error);
2245 }
Seth Hampson5b4f0752018-04-02 16:31:36 -07002246 // All track ids should be the same within an m section in a Unified Plan SDP.
2247 if (!track_id->empty() && new_track_id.compare(*track_id) != 0) {
2248 return ParseFailed(
2249 line, "Two different track IDs in msid attribute in one m= section",
2250 error);
2251 }
2252 *track_id = new_track_id;
deadbeefa4549d62017-02-10 17:26:22 -08002253
deadbeef9d3584c2016-02-16 17:54:10 -08002254 // msid:<msid-id>
Seth Hampson5b4f0752018-04-02 16:31:36 -07002255 if (!GetValue(field1, kAttributeMsid, &new_stream_id, error)) {
deadbeef9d3584c2016-02-16 17:54:10 -08002256 return false;
2257 }
Seth Hampson5b4f0752018-04-02 16:31:36 -07002258 if (new_stream_id.empty()) {
deadbeefa4549d62017-02-10 17:26:22 -08002259 return ParseFailed(line, "Missing stream ID in msid attribute.", error);
2260 }
Seth Hampson5b4f0752018-04-02 16:31:36 -07002261 // The special value "-" indicates "no MediaStream".
2262 if (new_stream_id.compare(kNoStreamMsid) != 0) {
2263 stream_ids->push_back(new_stream_id);
2264 }
deadbeef9d3584c2016-02-16 17:54:10 -08002265 return true;
2266}
2267
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002268// RFC 3551
2269// PT encoding media type clock rate channels
2270// name (Hz)
2271// 0 PCMU A 8,000 1
2272// 1 reserved A
2273// 2 reserved A
2274// 3 GSM A 8,000 1
2275// 4 G723 A 8,000 1
2276// 5 DVI4 A 8,000 1
2277// 6 DVI4 A 16,000 1
2278// 7 LPC A 8,000 1
2279// 8 PCMA A 8,000 1
2280// 9 G722 A 8,000 1
2281// 10 L16 A 44,100 2
2282// 11 L16 A 44,100 1
2283// 12 QCELP A 8,000 1
2284// 13 CN A 8,000 1
2285// 14 MPA A 90,000 (see text)
2286// 15 G728 A 8,000 1
2287// 16 DVI4 A 11,025 1
2288// 17 DVI4 A 22,050 1
2289// 18 G729 A 8,000 1
2290struct StaticPayloadAudioCodec {
2291 const char* name;
2292 int clockrate;
Peter Kasting69558702016-01-12 16:26:35 -08002293 size_t channels;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002294};
2295static const StaticPayloadAudioCodec kStaticPayloadAudioCodecs[] = {
2296 { "PCMU", 8000, 1 },
2297 { "reserved", 0, 0 },
2298 { "reserved", 0, 0 },
2299 { "GSM", 8000, 1 },
2300 { "G723", 8000, 1 },
2301 { "DVI4", 8000, 1 },
2302 { "DVI4", 16000, 1 },
2303 { "LPC", 8000, 1 },
2304 { "PCMA", 8000, 1 },
2305 { "G722", 8000, 1 },
2306 { "L16", 44100, 2 },
2307 { "L16", 44100, 1 },
2308 { "QCELP", 8000, 1 },
2309 { "CN", 8000, 1 },
2310 { "MPA", 90000, 1 },
2311 { "G728", 8000, 1 },
2312 { "DVI4", 11025, 1 },
2313 { "DVI4", 22050, 1 },
2314 { "G729", 8000, 1 },
2315};
2316
2317void MaybeCreateStaticPayloadAudioCodecs(
2318 const std::vector<int>& fmts, AudioContentDescription* media_desc) {
2319 if (!media_desc) {
2320 return;
2321 }
deadbeef67cf2c12016-04-13 10:07:16 -07002322 RTC_DCHECK(media_desc->codecs().empty());
solenberg9fa49752016-10-08 13:02:44 -07002323 for (int payload_type : fmts) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002324 if (!media_desc->HasCodec(payload_type) &&
2325 payload_type >= 0 &&
kjellander3e33bfe2016-06-20 07:04:09 -07002326 static_cast<uint32_t>(payload_type) <
2327 arraysize(kStaticPayloadAudioCodecs)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002328 std::string encoding_name = kStaticPayloadAudioCodecs[payload_type].name;
2329 int clock_rate = kStaticPayloadAudioCodecs[payload_type].clockrate;
Peter Kasting69558702016-01-12 16:26:35 -08002330 size_t channels = kStaticPayloadAudioCodecs[payload_type].channels;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002331 media_desc->AddCodec(cricket::AudioCodec(payload_type, encoding_name,
deadbeef67cf2c12016-04-13 10:07:16 -07002332 clock_rate, 0, channels));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002333 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002334 }
2335}
2336
2337template <class C>
2338static C* ParseContentDescription(const std::string& message,
2339 const MediaType media_type,
2340 int mline_index,
2341 const std::string& protocol,
deadbeef67cf2c12016-04-13 10:07:16 -07002342 const std::vector<int>& payload_types,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002343 size_t* pos,
2344 std::string* content_name,
deadbeef25ed4352016-12-12 18:37:36 -08002345 bool* bundle_only,
Steve Antone831b8c2018-02-01 12:22:16 -08002346 int* msid_signaling,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002347 TransportDescription* transport,
2348 std::vector<JsepIceCandidate*>* candidates,
2349 webrtc::SdpParseError* error) {
2350 C* media_desc = new C();
2351 switch (media_type) {
2352 case cricket::MEDIA_TYPE_AUDIO:
2353 *content_name = cricket::CN_AUDIO;
2354 break;
2355 case cricket::MEDIA_TYPE_VIDEO:
2356 *content_name = cricket::CN_VIDEO;
2357 break;
2358 case cricket::MEDIA_TYPE_DATA:
2359 *content_name = cricket::CN_DATA;
2360 break;
2361 default:
nissec80e7412017-01-11 05:56:46 -08002362 RTC_NOTREACHED();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002363 break;
2364 }
deadbeef67cf2c12016-04-13 10:07:16 -07002365 if (!ParseContent(message, media_type, mline_index, protocol, payload_types,
Steve Antone831b8c2018-02-01 12:22:16 -08002366 pos, content_name, bundle_only, msid_signaling, media_desc,
2367 transport, candidates, error)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002368 delete media_desc;
zhihuang38989e52017-03-21 11:04:53 -07002369 return nullptr;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002370 }
2371 // Sort the codecs according to the m-line fmt list.
deadbeef67cf2c12016-04-13 10:07:16 -07002372 std::unordered_map<int, int> payload_type_preferences;
2373 // "size + 1" so that the lowest preference payload type has a preference of
2374 // 1, which is greater than the default (0) for payload types not in the fmt
2375 // list.
2376 int preference = static_cast<int>(payload_types.size() + 1);
2377 for (int pt : payload_types) {
2378 payload_type_preferences[pt] = preference--;
2379 }
2380 std::vector<typename C::CodecType> codecs = media_desc->codecs();
2381 std::sort(codecs.begin(), codecs.end(), [&payload_type_preferences](
2382 const typename C::CodecType& a,
2383 const typename C::CodecType& b) {
2384 return payload_type_preferences[a.id] > payload_type_preferences[b.id];
2385 });
2386 media_desc->set_codecs(codecs);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002387 return media_desc;
2388}
2389
2390bool ParseMediaDescription(const std::string& message,
2391 const TransportDescription& session_td,
2392 const RtpHeaderExtensions& session_extmaps,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002393 size_t* pos,
zhihuang38989e52017-03-21 11:04:53 -07002394 const rtc::SocketAddress& session_connection_addr,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002395 cricket::SessionDescription* desc,
2396 std::vector<JsepIceCandidate*>* candidates,
2397 SdpParseError* error) {
nisseede5da42017-01-12 05:15:36 -08002398 RTC_DCHECK(desc != NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002399 std::string line;
2400 int mline_index = -1;
Steve Antone831b8c2018-02-01 12:22:16 -08002401 int msid_signaling = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002402
2403 // Zero or more media descriptions
2404 // RFC 4566
2405 // m=<media> <port> <proto> <fmt>
2406 while (GetLineWithType(message, pos, &line, kLineTypeMedia)) {
2407 ++mline_index;
2408
2409 std::vector<std::string> fields;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002410 rtc::split(line.substr(kLinePrefixLength),
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002411 kSdpDelimiterSpace, &fields);
zstein4b2e0822017-02-17 19:48:38 -08002412
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002413 const size_t expected_min_fields = 4;
2414 if (fields.size() < expected_min_fields) {
2415 return ParseFailedExpectMinFieldNum(line, expected_min_fields, error);
2416 }
deadbeef25ed4352016-12-12 18:37:36 -08002417 bool port_rejected = false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002418 // RFC 3264
2419 // To reject an offered stream, the port number in the corresponding stream
2420 // in the answer MUST be set to zero.
2421 if (fields[1] == kMediaPortRejected) {
deadbeef25ed4352016-12-12 18:37:36 -08002422 port_rejected = true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002423 }
2424
zhihuang38989e52017-03-21 11:04:53 -07002425 int port = 0;
2426 if (!rtc::FromString<int>(fields[1], &port) || !IsValidPort(port)) {
2427 return ParseFailed(line, "The port number is invalid", error);
2428 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002429 std::string protocol = fields[2];
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002430
2431 // <fmt>
deadbeef67cf2c12016-04-13 10:07:16 -07002432 std::vector<int> payload_types;
pthatcher@webrtc.org3341b402015-02-13 21:14:22 +00002433 if (IsRtp(protocol)) {
jiayl@webrtc.orgddb85ab2014-09-05 16:31:56 +00002434 for (size_t j = 3 ; j < fields.size(); ++j) {
2435 // TODO(wu): Remove when below bug is fixed.
2436 // https://bugzilla.mozilla.org/show_bug.cgi?id=996329
pbosbb36fdf2015-07-09 07:48:14 -07002437 if (fields[j].empty() && j == fields.size() - 1) {
jiayl@webrtc.orgddb85ab2014-09-05 16:31:56 +00002438 continue;
2439 }
wu@webrtc.org36eda7c2014-04-15 20:37:30 +00002440
jiayl@webrtc.orgddb85ab2014-09-05 16:31:56 +00002441 int pl = 0;
pkasting@chromium.orge9facf82015-02-17 20:36:28 +00002442 if (!GetPayloadTypeFromString(line, fields[j], &pl, error)) {
jiayl@webrtc.orgddb85ab2014-09-05 16:31:56 +00002443 return false;
2444 }
deadbeef67cf2c12016-04-13 10:07:16 -07002445 payload_types.push_back(pl);
wu@webrtc.org5e760e72014-04-02 23:19:09 +00002446 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002447 }
2448
2449 // Make a temporary TransportDescription based on |session_td|.
2450 // Some of this gets overwritten by ParseContent.
deadbeef46eed762016-01-28 13:24:37 -08002451 TransportDescription transport(
2452 session_td.transport_options, session_td.ice_ufrag, session_td.ice_pwd,
2453 session_td.ice_mode, session_td.connection_role,
2454 session_td.identity_fingerprint.get());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002455
kwibergd1fe2812016-04-27 06:47:29 -07002456 std::unique_ptr<MediaContentDescription> content;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002457 std::string content_name;
deadbeef25ed4352016-12-12 18:37:36 -08002458 bool bundle_only = false;
Steve Antone831b8c2018-02-01 12:22:16 -08002459 int section_msid_signaling = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002460 if (HasAttribute(line, kMediaTypeVideo)) {
2461 content.reset(ParseContentDescription<VideoContentDescription>(
deadbeef67cf2c12016-04-13 10:07:16 -07002462 message, cricket::MEDIA_TYPE_VIDEO, mline_index, protocol,
Steve Antone831b8c2018-02-01 12:22:16 -08002463 payload_types, pos, &content_name, &bundle_only,
2464 &section_msid_signaling, &transport, candidates, error));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002465 } else if (HasAttribute(line, kMediaTypeAudio)) {
2466 content.reset(ParseContentDescription<AudioContentDescription>(
deadbeef67cf2c12016-04-13 10:07:16 -07002467 message, cricket::MEDIA_TYPE_AUDIO, mline_index, protocol,
Steve Antone831b8c2018-02-01 12:22:16 -08002468 payload_types, pos, &content_name, &bundle_only,
2469 &section_msid_signaling, &transport, candidates, error));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002470 } else if (HasAttribute(line, kMediaTypeData)) {
jiayl@webrtc.org0e527722014-09-08 21:43:43 +00002471 DataContentDescription* data_desc =
wu@webrtc.org78187522013-10-07 23:32:02 +00002472 ParseContentDescription<DataContentDescription>(
deadbeef67cf2c12016-04-13 10:07:16 -07002473 message, cricket::MEDIA_TYPE_DATA, mline_index, protocol,
Steve Antone831b8c2018-02-01 12:22:16 -08002474 payload_types, pos, &content_name, &bundle_only,
2475 &section_msid_signaling, &transport, candidates, error);
jiayl@webrtc.org0e527722014-09-08 21:43:43 +00002476 content.reset(data_desc);
wu@webrtc.org78187522013-10-07 23:32:02 +00002477
zstein4b2e0822017-02-17 19:48:38 -08002478 if (data_desc && IsDtlsSctp(protocol)) {
2479 int p;
2480 if (rtc::FromString(fields[3], &p)) {
2481 if (!AddSctpDataCodec(data_desc, p)) {
2482 return false;
2483 }
2484 } else if (fields[3] == kDefaultSctpmapProtocol) {
2485 data_desc->set_use_sctpmap(false);
2486 }
wu@webrtc.org78187522013-10-07 23:32:02 +00002487 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002488 } else {
Mirko Bonadei675513b2017-11-09 11:09:25 +01002489 RTC_LOG(LS_WARNING) << "Unsupported media type: " << line;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002490 continue;
2491 }
2492 if (!content.get()) {
2493 // ParseContentDescription returns NULL if failed.
2494 return false;
2495 }
2496
Steve Antone831b8c2018-02-01 12:22:16 -08002497 msid_signaling |= section_msid_signaling;
2498
deadbeef25ed4352016-12-12 18:37:36 -08002499 bool content_rejected = false;
deadbeef12771a12017-01-03 13:53:47 -08002500 // A port of 0 is not interpreted as a rejected m= section when it's
2501 // used along with a=bundle-only.
deadbeef25ed4352016-12-12 18:37:36 -08002502 if (bundle_only) {
deadbeef25ed4352016-12-12 18:37:36 -08002503 if (!port_rejected) {
deadbeef12771a12017-01-03 13:53:47 -08002504 // Usage of bundle-only with a nonzero port is unspecified. So just
2505 // ignore bundle-only if we see this.
2506 bundle_only = false;
Mirko Bonadei675513b2017-11-09 11:09:25 +01002507 RTC_LOG(LS_WARNING)
deadbeef12771a12017-01-03 13:53:47 -08002508 << "a=bundle-only attribute observed with a nonzero "
Jonas Olsson45cc8902018-02-13 10:37:07 +01002509 "port; this usage is unspecified so the attribute is being "
2510 "ignored.";
deadbeef25ed4352016-12-12 18:37:36 -08002511 }
2512 } else {
2513 // If not using bundle-only, interpret port 0 in the normal way; the m=
2514 // section is being rejected.
2515 content_rejected = port_rejected;
2516 }
2517
pthatcher@webrtc.org3341b402015-02-13 21:14:22 +00002518 if (IsRtp(protocol)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002519 // Set the extmap.
2520 if (!session_extmaps.empty() &&
2521 !content->rtp_header_extensions().empty()) {
2522 return ParseFailed("",
2523 "The a=extmap MUST be either all session level or "
2524 "all media level.",
2525 error);
2526 }
2527 for (size_t i = 0; i < session_extmaps.size(); ++i) {
2528 content->AddRtpHeaderExtension(session_extmaps[i]);
2529 }
2530 }
2531 content->set_protocol(protocol);
zhihuang38989e52017-03-21 11:04:53 -07002532
2533 // Use the session level connection address if the media level addresses are
2534 // not specified.
2535 rtc::SocketAddress address;
2536 address = content->connection_address().IsNil()
2537 ? session_connection_addr
2538 : content->connection_address();
2539 address.SetPort(port);
2540 content->set_connection_address(address);
2541
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002542 desc->AddContent(content_name,
Steve Anton5adfafd2017-12-20 16:34:00 -08002543 IsDtlsSctp(protocol) ? MediaProtocolType::kSctp
2544 : MediaProtocolType::kRtp,
deadbeef25ed4352016-12-12 18:37:36 -08002545 content_rejected, bundle_only, content.release());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002546 // Create TransportInfo with the media level "ice-pwd" and "ice-ufrag".
2547 TransportInfo transport_info(content_name, transport);
2548
2549 if (!desc->AddTransportInfo(transport_info)) {
2550 std::ostringstream description;
2551 description << "Failed to AddTransportInfo with content name: "
2552 << content_name;
2553 return ParseFailed("", description.str(), error);
2554 }
2555 }
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00002556
Steve Antone831b8c2018-02-01 12:22:16 -08002557 desc->set_msid_signaling(msid_signaling);
2558
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00002559 size_t end_of_message = message.size();
2560 if (mline_index == -1 && *pos != end_of_message) {
2561 ParseFailed(message, *pos, "Expects m line.", error);
2562 return false;
2563 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002564 return true;
2565}
2566
2567bool VerifyCodec(const cricket::Codec& codec) {
2568 // Codec has not been populated correctly unless the name has been set. This
2569 // can happen if an SDP has an fmtp or rtcp-fb with a payload type but doesn't
2570 // have a corresponding "rtpmap" line.
htab39db842016-12-08 01:50:48 -08002571 return !codec.name.empty();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002572}
2573
2574bool VerifyAudioCodecs(const AudioContentDescription* audio_desc) {
2575 const std::vector<cricket::AudioCodec>& codecs = audio_desc->codecs();
2576 for (std::vector<cricket::AudioCodec>::const_iterator iter = codecs.begin();
2577 iter != codecs.end(); ++iter) {
2578 if (!VerifyCodec(*iter)) {
2579 return false;
2580 }
2581 }
2582 return true;
2583}
2584
2585bool VerifyVideoCodecs(const VideoContentDescription* video_desc) {
2586 const std::vector<cricket::VideoCodec>& codecs = video_desc->codecs();
2587 for (std::vector<cricket::VideoCodec>::const_iterator iter = codecs.begin();
2588 iter != codecs.end(); ++iter) {
2589 if (!VerifyCodec(*iter)) {
2590 return false;
2591 }
2592 }
2593 return true;
2594}
2595
2596void AddParameters(const cricket::CodecParameterMap& parameters,
2597 cricket::Codec* codec) {
2598 for (cricket::CodecParameterMap::const_iterator iter =
2599 parameters.begin(); iter != parameters.end(); ++iter) {
2600 codec->SetParam(iter->first, iter->second);
2601 }
2602}
2603
2604void AddFeedbackParameter(const cricket::FeedbackParam& feedback_param,
2605 cricket::Codec* codec) {
2606 codec->AddFeedbackParam(feedback_param);
2607}
2608
2609void AddFeedbackParameters(const cricket::FeedbackParams& feedback_params,
2610 cricket::Codec* codec) {
2611 for (std::vector<cricket::FeedbackParam>::const_iterator iter =
2612 feedback_params.params().begin();
2613 iter != feedback_params.params().end(); ++iter) {
2614 codec->AddFeedbackParam(*iter);
2615 }
2616}
2617
2618// Gets the current codec setting associated with |payload_type|. If there
changbin.shao@webrtc.org2d25b442015-03-16 04:14:34 +00002619// is no Codec associated with that payload type it returns an empty codec
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002620// with that payload type.
2621template <class T>
changbin.shao@webrtc.org2d25b442015-03-16 04:14:34 +00002622T GetCodecWithPayloadType(const std::vector<T>& codecs, int payload_type) {
magjedb05fa242016-11-11 04:00:16 -08002623 const T* codec = FindCodecById(codecs, payload_type);
2624 if (codec)
2625 return *codec;
2626 // Return empty codec with |payload_type|.
changbin.shao@webrtc.org2d25b442015-03-16 04:14:34 +00002627 T ret_val;
magjedb05fa242016-11-11 04:00:16 -08002628 ret_val.id = payload_type;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002629 return ret_val;
2630}
2631
2632// Updates or creates a new codec entry in the audio description.
2633template <class T, class U>
2634void AddOrReplaceCodec(MediaContentDescription* content_desc, const U& codec) {
2635 T* desc = static_cast<T*>(content_desc);
2636 std::vector<U> codecs = desc->codecs();
2637 bool found = false;
2638
2639 typename std::vector<U>::iterator iter;
2640 for (iter = codecs.begin(); iter != codecs.end(); ++iter) {
2641 if (iter->id == codec.id) {
2642 *iter = codec;
2643 found = true;
2644 break;
2645 }
2646 }
2647 if (!found) {
2648 desc->AddCodec(codec);
2649 return;
2650 }
2651 desc->set_codecs(codecs);
2652}
2653
2654// Adds or updates existing codec corresponding to |payload_type| according
2655// to |parameters|.
2656template <class T, class U>
2657void UpdateCodec(MediaContentDescription* content_desc, int payload_type,
2658 const cricket::CodecParameterMap& parameters) {
2659 // Codec might already have been populated (from rtpmap).
changbin.shao@webrtc.org2d25b442015-03-16 04:14:34 +00002660 U new_codec = GetCodecWithPayloadType(static_cast<T*>(content_desc)->codecs(),
2661 payload_type);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002662 AddParameters(parameters, &new_codec);
2663 AddOrReplaceCodec<T, U>(content_desc, new_codec);
2664}
2665
2666// Adds or updates existing codec corresponding to |payload_type| according
2667// to |feedback_param|.
2668template <class T, class U>
2669void UpdateCodec(MediaContentDescription* content_desc, int payload_type,
2670 const cricket::FeedbackParam& feedback_param) {
2671 // Codec might already have been populated (from rtpmap).
changbin.shao@webrtc.org2d25b442015-03-16 04:14:34 +00002672 U new_codec = GetCodecWithPayloadType(static_cast<T*>(content_desc)->codecs(),
2673 payload_type);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002674 AddFeedbackParameter(feedback_param, &new_codec);
2675 AddOrReplaceCodec<T, U>(content_desc, new_codec);
2676}
2677
jlmiller@webrtc.orga744a282015-02-18 21:37:46 +00002678template <class T>
2679bool PopWildcardCodec(std::vector<T>* codecs, T* wildcard_codec) {
2680 for (auto iter = codecs->begin(); iter != codecs->end(); ++iter) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002681 if (iter->id == kWildcardPayloadType) {
2682 *wildcard_codec = *iter;
2683 codecs->erase(iter);
2684 return true;
2685 }
2686 }
2687 return false;
2688}
2689
jlmiller@webrtc.orga744a282015-02-18 21:37:46 +00002690template<class T>
2691void UpdateFromWildcardCodecs(cricket::MediaContentDescriptionImpl<T>* desc) {
2692 auto codecs = desc->codecs();
2693 T wildcard_codec;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002694 if (!PopWildcardCodec(&codecs, &wildcard_codec)) {
2695 return;
2696 }
jlmiller@webrtc.orga744a282015-02-18 21:37:46 +00002697 for (auto& codec : codecs) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002698 AddFeedbackParameters(wildcard_codec.feedback_params, &codec);
2699 }
jlmiller@webrtc.orga744a282015-02-18 21:37:46 +00002700 desc->set_codecs(codecs);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002701}
2702
2703void AddAudioAttribute(const std::string& name, const std::string& value,
2704 AudioContentDescription* audio_desc) {
2705 if (value.empty()) {
2706 return;
2707 }
2708 std::vector<cricket::AudioCodec> codecs = audio_desc->codecs();
2709 for (std::vector<cricket::AudioCodec>::iterator iter = codecs.begin();
2710 iter != codecs.end(); ++iter) {
2711 iter->params[name] = value;
2712 }
2713 audio_desc->set_codecs(codecs);
2714}
2715
2716bool ParseContent(const std::string& message,
2717 const MediaType media_type,
2718 int mline_index,
2719 const std::string& protocol,
deadbeef67cf2c12016-04-13 10:07:16 -07002720 const std::vector<int>& payload_types,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002721 size_t* pos,
2722 std::string* content_name,
deadbeef25ed4352016-12-12 18:37:36 -08002723 bool* bundle_only,
Steve Antone831b8c2018-02-01 12:22:16 -08002724 int* msid_signaling,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002725 MediaContentDescription* media_desc,
2726 TransportDescription* transport,
2727 std::vector<JsepIceCandidate*>* candidates,
2728 SdpParseError* error) {
nisseede5da42017-01-12 05:15:36 -08002729 RTC_DCHECK(media_desc != NULL);
2730 RTC_DCHECK(content_name != NULL);
2731 RTC_DCHECK(transport != NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002732
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +00002733 if (media_type == cricket::MEDIA_TYPE_AUDIO) {
Steve Antonb1c1de12017-12-21 15:14:30 -08002734 MaybeCreateStaticPayloadAudioCodecs(payload_types, media_desc->as_audio());
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +00002735 }
2736
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002737 // The media level "ice-ufrag" and "ice-pwd".
2738 // The candidates before update the media level "ice-pwd" and "ice-ufrag".
2739 Candidates candidates_orig;
2740 std::string line;
2741 std::string mline_id;
2742 // Tracks created out of the ssrc attributes.
2743 StreamParamsVec tracks;
2744 SsrcInfoVec ssrc_infos;
2745 SsrcGroupVec ssrc_groups;
2746 std::string maxptime_as_string;
2747 std::string ptime_as_string;
Seth Hampson5b4f0752018-04-02 16:31:36 -07002748 std::vector<std::string> stream_ids;
deadbeef9d3584c2016-02-16 17:54:10 -08002749 std::string track_id;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002750
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002751 // Loop until the next m line
2752 while (!IsLineType(message, kLineTypeMedia, *pos)) {
2753 if (!GetLine(message, pos, &line)) {
2754 if (*pos >= message.size()) {
2755 break; // Done parsing
2756 } else {
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +00002757 return ParseFailed(message, *pos, "Invalid SDP line.", error);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002758 }
2759 }
2760
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002761 // RFC 4566
2762 // b=* (zero or more bandwidth information lines)
2763 if (IsLineType(line, kLineTypeSessionBandwidth)) {
2764 std::string bandwidth;
2765 if (HasAttribute(line, kApplicationSpecificMaximum)) {
2766 if (!GetValue(line, kApplicationSpecificMaximum, &bandwidth, error)) {
2767 return false;
2768 } else {
wu@webrtc.org5e760e72014-04-02 23:19:09 +00002769 int b = 0;
2770 if (!GetValueFromString(line, bandwidth, &b, error)) {
2771 return false;
2772 }
deadbeef3e8016e2017-08-03 17:49:30 -07002773 // TODO(deadbeef): Historically, applications may be setting a value
2774 // of -1 to mean "unset any previously set bandwidth limit", even
2775 // though ommitting the "b=AS" entirely will do just that. Once we've
2776 // transitioned applications to doing the right thing, it would be
2777 // better to treat this as a hard error instead of just ignoring it.
2778 if (b == -1) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01002779 RTC_LOG(LS_WARNING)
2780 << "Ignoring \"b=AS:-1\"; will be treated as \"no "
2781 "bandwidth limit\".";
deadbeef3e8016e2017-08-03 17:49:30 -07002782 continue;
2783 }
deadbeefbc88c6b2017-08-02 11:26:34 -07002784 if (b < 0) {
2785 return ParseFailed(line, "b=AS value can't be negative.", error);
2786 }
Peter Thatcherc0c3a862015-06-24 15:31:25 -07002787 // We should never use more than the default bandwidth for RTP-based
2788 // data channels. Don't allow SDP to set the bandwidth, because
2789 // that would give JS the opportunity to "break the Internet".
2790 // See: https://code.google.com/p/chromium/issues/detail?id=280726
2791 if (media_type == cricket::MEDIA_TYPE_DATA && IsRtp(protocol) &&
2792 b > cricket::kDataMaxBandwidth / 1000) {
2793 std::ostringstream description;
2794 description << "RTP-based data channels may not send more than "
2795 << cricket::kDataMaxBandwidth / 1000 << "kbps.";
2796 return ParseFailed(line, description.str(), error);
2797 }
deadbeefb2362572016-12-13 16:37:06 -08002798 // Prevent integer overflow.
2799 b = std::min(b, INT_MAX / 1000);
wu@webrtc.org5e760e72014-04-02 23:19:09 +00002800 media_desc->set_bandwidth(b * 1000);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002801 }
2802 }
2803 continue;
2804 }
2805
zhihuang38989e52017-03-21 11:04:53 -07002806 // Parse the media level connection data.
2807 if (IsLineType(line, kLineTypeConnection)) {
2808 rtc::SocketAddress addr;
2809 if (!ParseConnectionData(line, &addr, error)) {
2810 return false;
2811 }
2812 media_desc->set_connection_address(addr);
2813 continue;
2814 }
2815
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002816 if (!IsLineType(line, kLineTypeAttributes)) {
Steve Anton36b29d12017-10-30 09:57:42 -07002817 // TODO(deadbeef): Handle other lines if needed.
Mirko Bonadei675513b2017-11-09 11:09:25 +01002818 RTC_LOG(LS_INFO) << "Ignored line: " << line;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002819 continue;
2820 }
2821
2822 // Handle attributes common to SCTP and RTP.
2823 if (HasAttribute(line, kAttributeMid)) {
2824 // RFC 3388
2825 // mid-attribute = "a=mid:" identification-tag
2826 // identification-tag = token
2827 // Use the mid identification-tag as the content name.
2828 if (!GetValue(line, kAttributeMid, &mline_id, error)) {
2829 return false;
2830 }
2831 *content_name = mline_id;
deadbeef25ed4352016-12-12 18:37:36 -08002832 } else if (HasAttribute(line, kAttributeBundleOnly)) {
2833 *bundle_only = true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002834 } else if (HasAttribute(line, kAttributeCandidate)) {
2835 Candidate candidate;
2836 if (!ParseCandidate(line, &candidate, error, false)) {
2837 return false;
2838 }
deadbeef7bcdb692017-01-20 12:43:58 -08002839 // ParseCandidate will parse non-standard ufrag and password attributes,
2840 // since it's used for candidate trickling, but we only want to process
2841 // the "a=ice-ufrag"/"a=ice-pwd" values in a session description, so
2842 // strip them off at this point.
2843 candidate.set_username(std::string());
2844 candidate.set_password(std::string());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002845 candidates_orig.push_back(candidate);
2846 } else if (HasAttribute(line, kAttributeIceUfrag)) {
2847 if (!GetValue(line, kAttributeIceUfrag, &transport->ice_ufrag, error)) {
2848 return false;
2849 }
2850 } else if (HasAttribute(line, kAttributeIcePwd)) {
2851 if (!GetValue(line, kAttributeIcePwd, &transport->ice_pwd, error)) {
2852 return false;
2853 }
2854 } else if (HasAttribute(line, kAttributeIceOption)) {
2855 if (!ParseIceOptions(line, &transport->transport_options, error)) {
2856 return false;
2857 }
2858 } else if (HasAttribute(line, kAttributeFmtp)) {
2859 if (!ParseFmtpAttributes(line, media_type, media_desc, error)) {
2860 return false;
2861 }
2862 } else if (HasAttribute(line, kAttributeFingerprint)) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002863 rtc::SSLFingerprint* fingerprint = NULL;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002864
2865 if (!ParseFingerprintAttribute(line, &fingerprint, error)) {
2866 return false;
2867 }
2868 transport->identity_fingerprint.reset(fingerprint);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00002869 } else if (HasAttribute(line, kAttributeSetup)) {
2870 if (!ParseDtlsSetup(line, &(transport->connection_role), error)) {
2871 return false;
2872 }
pthatcher@webrtc.org3341b402015-02-13 21:14:22 +00002873 } else if (IsDtlsSctp(protocol) && HasAttribute(line, kAttributeSctpPort)) {
deadbeef7e146cb2016-09-28 10:04:34 -07002874 if (media_type != cricket::MEDIA_TYPE_DATA) {
2875 return ParseFailed(
2876 line, "sctp-port attribute found in non-data media description.",
2877 error);
2878 }
jiayl@webrtc.orgddb85ab2014-09-05 16:31:56 +00002879 int sctp_port;
2880 if (!ParseSctpPort(line, &sctp_port, error)) {
2881 return false;
2882 }
Steve Antonb1c1de12017-12-21 15:14:30 -08002883 if (!AddSctpDataCodec(media_desc->as_data(), sctp_port)) {
jiayl@webrtc.orgddb85ab2014-09-05 16:31:56 +00002884 return false;
2885 }
pthatcher@webrtc.org3341b402015-02-13 21:14:22 +00002886 } else if (IsRtp(protocol)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002887 //
2888 // RTP specific attrubtes
2889 //
2890 if (HasAttribute(line, kAttributeRtcpMux)) {
2891 media_desc->set_rtcp_mux(true);
deadbeef13871492015-12-09 12:37:51 -08002892 } else if (HasAttribute(line, kAttributeRtcpReducedSize)) {
2893 media_desc->set_rtcp_reduced_size(true);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002894 } else if (HasAttribute(line, kAttributeSsrcGroup)) {
2895 if (!ParseSsrcGroupAttribute(line, &ssrc_groups, error)) {
2896 return false;
2897 }
2898 } else if (HasAttribute(line, kAttributeSsrc)) {
Steve Antone831b8c2018-02-01 12:22:16 -08002899 if (!ParseSsrcAttribute(line, &ssrc_infos, msid_signaling, error)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002900 return false;
2901 }
2902 } else if (HasAttribute(line, kAttributeCrypto)) {
2903 if (!ParseCryptoAttribute(line, media_desc, error)) {
2904 return false;
2905 }
2906 } else if (HasAttribute(line, kAttributeRtpmap)) {
deadbeef67cf2c12016-04-13 10:07:16 -07002907 if (!ParseRtpmapAttribute(line, media_type, payload_types, media_desc,
2908 error)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002909 return false;
2910 }
2911 } else if (HasAttribute(line, kCodecParamMaxPTime)) {
2912 if (!GetValue(line, kCodecParamMaxPTime, &maxptime_as_string, error)) {
2913 return false;
2914 }
2915 } else if (HasAttribute(line, kAttributeRtcpFb)) {
2916 if (!ParseRtcpFbAttribute(line, media_type, media_desc, error)) {
2917 return false;
2918 }
2919 } else if (HasAttribute(line, kCodecParamPTime)) {
2920 if (!GetValue(line, kCodecParamPTime, &ptime_as_string, error)) {
2921 return false;
2922 }
2923 } else if (HasAttribute(line, kAttributeSendOnly)) {
Steve Anton4e70a722017-11-28 14:57:10 -08002924 media_desc->set_direction(RtpTransceiverDirection::kSendOnly);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002925 } else if (HasAttribute(line, kAttributeRecvOnly)) {
Steve Anton4e70a722017-11-28 14:57:10 -08002926 media_desc->set_direction(RtpTransceiverDirection::kRecvOnly);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002927 } else if (HasAttribute(line, kAttributeInactive)) {
Steve Anton4e70a722017-11-28 14:57:10 -08002928 media_desc->set_direction(RtpTransceiverDirection::kInactive);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002929 } else if (HasAttribute(line, kAttributeSendRecv)) {
Steve Anton4e70a722017-11-28 14:57:10 -08002930 media_desc->set_direction(RtpTransceiverDirection::kSendRecv);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002931 } else if (HasAttribute(line, kAttributeExtmap)) {
isheriff6f8d6862016-05-26 11:24:55 -07002932 RtpExtension extmap;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002933 if (!ParseExtmap(line, &extmap, error)) {
2934 return false;
2935 }
2936 media_desc->AddRtpHeaderExtension(extmap);
2937 } else if (HasAttribute(line, kAttributeXGoogleFlag)) {
2938 // Experimental attribute. Conference mode activates more aggressive
2939 // AEC and NS settings.
Steve Anton36b29d12017-10-30 09:57:42 -07002940 // TODO(deadbeef): expose API to set these directly.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002941 std::string flag_value;
2942 if (!GetValue(line, kAttributeXGoogleFlag, &flag_value, error)) {
2943 return false;
2944 }
2945 if (flag_value.compare(kValueConference) == 0)
2946 media_desc->set_conference_mode(true);
deadbeef9d3584c2016-02-16 17:54:10 -08002947 } else if (HasAttribute(line, kAttributeMsid)) {
Seth Hampson5b4f0752018-04-02 16:31:36 -07002948 if (!ParseMsidAttribute(line, &stream_ids, &track_id, error)) {
deadbeef9d3584c2016-02-16 17:54:10 -08002949 return false;
2950 }
Steve Antone831b8c2018-02-01 12:22:16 -08002951 *msid_signaling |= cricket::kMsidSignalingMediaSection;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002952 }
2953 } else {
2954 // Only parse lines that we are interested of.
Mirko Bonadei675513b2017-11-09 11:09:25 +01002955 RTC_LOG(LS_INFO) << "Ignored line: " << line;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002956 continue;
2957 }
2958 }
2959
2960 // Create tracks from the |ssrc_infos|.
Taylor Brandstetter5de6b752016-03-09 17:02:30 -08002961 // If the stream_id/track_id for all SSRCS are identical, one StreamParams
2962 // will be created in CreateTracksFromSsrcInfos, containing all the SSRCs from
2963 // the m= section.
Seth Hampson5897a6e2018-04-03 11:16:33 -07002964 if (!ssrc_infos.empty()) {
2965 CreateTracksFromSsrcInfos(ssrc_infos, stream_ids, track_id, &tracks,
2966 *msid_signaling);
2967 } else if (media_type != cricket::MEDIA_TYPE_DATA &&
2968 (*msid_signaling & cricket::kMsidSignalingMediaSection)) {
2969 // If the stream_ids/track_id was signaled but SSRCs were unsignaled we
2970 // still create a track. This isn't done for data media types because
2971 // StreamParams aren't used for SCTP streams, and RTP data channels don't
2972 // support unsignaled SSRCs.
2973 CreateTrackWithNoSsrcs(stream_ids, track_id, &tracks);
2974 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002975
2976 // Add the ssrc group to the track.
2977 for (SsrcGroupVec::iterator ssrc_group = ssrc_groups.begin();
2978 ssrc_group != ssrc_groups.end(); ++ssrc_group) {
2979 if (ssrc_group->ssrcs.empty()) {
2980 continue;
2981 }
Peter Boström0c4e06b2015-10-07 12:23:21 +02002982 uint32_t ssrc = ssrc_group->ssrcs.front();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002983 for (StreamParamsVec::iterator track = tracks.begin();
2984 track != tracks.end(); ++track) {
2985 if (track->has_ssrc(ssrc)) {
2986 track->ssrc_groups.push_back(*ssrc_group);
2987 }
2988 }
2989 }
2990
2991 // Add the new tracks to the |media_desc|.
deadbeef9d3584c2016-02-16 17:54:10 -08002992 for (StreamParams& track : tracks) {
2993 media_desc->AddStream(track);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002994 }
2995
2996 if (media_type == cricket::MEDIA_TYPE_AUDIO) {
Steve Antonb1c1de12017-12-21 15:14:30 -08002997 AudioContentDescription* audio_desc = media_desc->as_audio();
jlmiller@webrtc.orga744a282015-02-18 21:37:46 +00002998 UpdateFromWildcardCodecs(audio_desc);
2999
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003000 // Verify audio codec ensures that no audio codec has been populated with
3001 // only fmtp.
3002 if (!VerifyAudioCodecs(audio_desc)) {
3003 return ParseFailed("Failed to parse audio codecs correctly.", error);
3004 }
3005 AddAudioAttribute(kCodecParamMaxPTime, maxptime_as_string, audio_desc);
3006 AddAudioAttribute(kCodecParamPTime, ptime_as_string, audio_desc);
3007 }
3008
3009 if (media_type == cricket::MEDIA_TYPE_VIDEO) {
Steve Antonb1c1de12017-12-21 15:14:30 -08003010 VideoContentDescription* video_desc = media_desc->as_video();
jlmiller@webrtc.orga744a282015-02-18 21:37:46 +00003011 UpdateFromWildcardCodecs(video_desc);
3012 // Verify video codec ensures that no video codec has been populated with
3013 // only rtcp-fb.
3014 if (!VerifyVideoCodecs(video_desc)) {
3015 return ParseFailed("Failed to parse video codecs correctly.", error);
3016 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003017 }
3018
3019 // RFC 5245
3020 // Update the candidates with the media level "ice-pwd" and "ice-ufrag".
3021 for (Candidates::iterator it = candidates_orig.begin();
3022 it != candidates_orig.end(); ++it) {
nisseede5da42017-01-12 05:15:36 -08003023 RTC_DCHECK((*it).username().empty() ||
3024 (*it).username() == transport->ice_ufrag);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003025 (*it).set_username(transport->ice_ufrag);
nisseede5da42017-01-12 05:15:36 -08003026 RTC_DCHECK((*it).password().empty());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003027 (*it).set_password(transport->ice_pwd);
3028 candidates->push_back(
3029 new JsepIceCandidate(mline_id, mline_index, *it));
3030 }
3031 return true;
3032}
3033
Steve Antone831b8c2018-02-01 12:22:16 -08003034bool ParseSsrcAttribute(const std::string& line,
3035 SsrcInfoVec* ssrc_infos,
3036 int* msid_signaling,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003037 SdpParseError* error) {
nisseede5da42017-01-12 05:15:36 -08003038 RTC_DCHECK(ssrc_infos != NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003039 // RFC 5576
3040 // a=ssrc:<ssrc-id> <attribute>
3041 // a=ssrc:<ssrc-id> <attribute>:<value>
3042 std::string field1, field2;
Donald Curtis144d0182015-05-15 13:14:24 -07003043 if (!rtc::tokenize_first(line.substr(kLinePrefixLength), kSdpDelimiterSpace,
3044 &field1, &field2)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003045 const size_t expected_fields = 2;
3046 return ParseFailedExpectFieldNum(line, expected_fields, error);
3047 }
3048
3049 // ssrc:<ssrc-id>
3050 std::string ssrc_id_s;
3051 if (!GetValue(field1, kAttributeSsrc, &ssrc_id_s, error)) {
3052 return false;
3053 }
Peter Boström0c4e06b2015-10-07 12:23:21 +02003054 uint32_t ssrc_id = 0;
wu@webrtc.org5e760e72014-04-02 23:19:09 +00003055 if (!GetValueFromString(line, ssrc_id_s, &ssrc_id, error)) {
3056 return false;
3057 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003058
3059 std::string attribute;
3060 std::string value;
Donald Curtis144d0182015-05-15 13:14:24 -07003061 if (!rtc::tokenize_first(field2, kSdpDelimiterColon, &attribute, &value)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003062 std::ostringstream description;
3063 description << "Failed to get the ssrc attribute value from " << field2
3064 << ". Expected format <attribute>:<value>.";
3065 return ParseFailed(line, description.str(), error);
3066 }
3067
3068 // Check if there's already an item for this |ssrc_id|. Create a new one if
3069 // there isn't.
3070 SsrcInfoVec::iterator ssrc_info = ssrc_infos->begin();
3071 for (; ssrc_info != ssrc_infos->end(); ++ssrc_info) {
3072 if (ssrc_info->ssrc_id == ssrc_id) {
3073 break;
3074 }
3075 }
3076 if (ssrc_info == ssrc_infos->end()) {
3077 SsrcInfo info;
3078 info.ssrc_id = ssrc_id;
3079 ssrc_infos->push_back(info);
3080 ssrc_info = ssrc_infos->end() - 1;
3081 }
3082
3083 // Store the info to the |ssrc_info|.
3084 if (attribute == kSsrcAttributeCname) {
3085 // RFC 5576
3086 // cname:<value>
3087 ssrc_info->cname = value;
3088 } else if (attribute == kSsrcAttributeMsid) {
3089 // draft-alvestrand-mmusic-msid-00
Seth Hampson5b4f0752018-04-02 16:31:36 -07003090 // msid:identifier [appdata]
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003091 std::vector<std::string> fields;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00003092 rtc::split(value, kSdpDelimiterSpace, &fields);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003093 if (fields.size() < 1 || fields.size() > 2) {
3094 return ParseFailed(line,
3095 "Expected format \"msid:<identifier>[ <appdata>]\".",
3096 error);
3097 }
deadbeef9d3584c2016-02-16 17:54:10 -08003098 ssrc_info->stream_id = fields[0];
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003099 if (fields.size() == 2) {
deadbeef9d3584c2016-02-16 17:54:10 -08003100 ssrc_info->track_id = fields[1];
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003101 }
Steve Antone831b8c2018-02-01 12:22:16 -08003102 *msid_signaling |= cricket::kMsidSignalingSsrcAttribute;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003103 } else if (attribute == kSsrcAttributeMslabel) {
3104 // draft-alvestrand-rtcweb-mid-01
3105 // mslabel:<value>
3106 ssrc_info->mslabel = value;
3107 } else if (attribute == kSSrcAttributeLabel) {
3108 // The label isn't defined.
3109 // label:<value>
3110 ssrc_info->label = value;
3111 }
3112 return true;
3113}
3114
3115bool ParseSsrcGroupAttribute(const std::string& line,
3116 SsrcGroupVec* ssrc_groups,
3117 SdpParseError* error) {
nisseede5da42017-01-12 05:15:36 -08003118 RTC_DCHECK(ssrc_groups != NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003119 // RFC 5576
3120 // a=ssrc-group:<semantics> <ssrc-id> ...
3121 std::vector<std::string> fields;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00003122 rtc::split(line.substr(kLinePrefixLength),
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003123 kSdpDelimiterSpace, &fields);
3124 const size_t expected_min_fields = 2;
3125 if (fields.size() < expected_min_fields) {
3126 return ParseFailedExpectMinFieldNum(line, expected_min_fields, error);
3127 }
3128 std::string semantics;
3129 if (!GetValue(fields[0], kAttributeSsrcGroup, &semantics, error)) {
3130 return false;
3131 }
Peter Boström0c4e06b2015-10-07 12:23:21 +02003132 std::vector<uint32_t> ssrcs;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003133 for (size_t i = 1; i < fields.size(); ++i) {
Peter Boström0c4e06b2015-10-07 12:23:21 +02003134 uint32_t ssrc = 0;
wu@webrtc.org5e760e72014-04-02 23:19:09 +00003135 if (!GetValueFromString(line, fields[i], &ssrc, error)) {
3136 return false;
3137 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003138 ssrcs.push_back(ssrc);
3139 }
3140 ssrc_groups->push_back(SsrcGroup(semantics, ssrcs));
3141 return true;
3142}
3143
3144bool ParseCryptoAttribute(const std::string& line,
3145 MediaContentDescription* media_desc,
3146 SdpParseError* error) {
3147 std::vector<std::string> fields;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00003148 rtc::split(line.substr(kLinePrefixLength),
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003149 kSdpDelimiterSpace, &fields);
3150 // RFC 4568
3151 // a=crypto:<tag> <crypto-suite> <key-params> [<session-params>]
3152 const size_t expected_min_fields = 3;
3153 if (fields.size() < expected_min_fields) {
3154 return ParseFailedExpectMinFieldNum(line, expected_min_fields, error);
3155 }
3156 std::string tag_value;
3157 if (!GetValue(fields[0], kAttributeCrypto, &tag_value, error)) {
3158 return false;
3159 }
wu@webrtc.org5e760e72014-04-02 23:19:09 +00003160 int tag = 0;
3161 if (!GetValueFromString(line, tag_value, &tag, error)) {
3162 return false;
3163 }
jbauch083b73f2015-07-16 02:46:32 -07003164 const std::string& crypto_suite = fields[1];
3165 const std::string& key_params = fields[2];
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003166 std::string session_params;
3167 if (fields.size() > 3) {
3168 session_params = fields[3];
3169 }
3170 media_desc->AddCrypto(CryptoParams(tag, crypto_suite, key_params,
3171 session_params));
3172 return true;
3173}
3174
3175// Updates or creates a new codec entry in the audio description with according
deadbeef67cf2c12016-04-13 10:07:16 -07003176// to |name|, |clockrate|, |bitrate|, and |channels|.
3177void UpdateCodec(int payload_type,
3178 const std::string& name,
3179 int clockrate,
3180 int bitrate,
3181 size_t channels,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003182 AudioContentDescription* audio_desc) {
3183 // Codec may already be populated with (only) optional parameters
3184 // (from an fmtp).
changbin.shao@webrtc.org2d25b442015-03-16 04:14:34 +00003185 cricket::AudioCodec codec =
3186 GetCodecWithPayloadType(audio_desc->codecs(), payload_type);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003187 codec.name = name;
3188 codec.clockrate = clockrate;
3189 codec.bitrate = bitrate;
3190 codec.channels = channels;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003191 AddOrReplaceCodec<AudioContentDescription, cricket::AudioCodec>(audio_desc,
3192 codec);
3193}
3194
3195// Updates or creates a new codec entry in the video description according to
deadbeef67cf2c12016-04-13 10:07:16 -07003196// |name|, |width|, |height|, and |framerate|.
3197void UpdateCodec(int payload_type,
3198 const std::string& name,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003199 VideoContentDescription* video_desc) {
3200 // Codec may already be populated with (only) optional parameters
3201 // (from an fmtp).
changbin.shao@webrtc.org2d25b442015-03-16 04:14:34 +00003202 cricket::VideoCodec codec =
3203 GetCodecWithPayloadType(video_desc->codecs(), payload_type);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003204 codec.name = name;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003205 AddOrReplaceCodec<VideoContentDescription, cricket::VideoCodec>(video_desc,
3206 codec);
3207}
3208
3209bool ParseRtpmapAttribute(const std::string& line,
3210 const MediaType media_type,
deadbeef67cf2c12016-04-13 10:07:16 -07003211 const std::vector<int>& payload_types,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003212 MediaContentDescription* media_desc,
3213 SdpParseError* error) {
3214 std::vector<std::string> fields;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00003215 rtc::split(line.substr(kLinePrefixLength),
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003216 kSdpDelimiterSpace, &fields);
3217 // RFC 4566
3218 // a=rtpmap:<payload type> <encoding name>/<clock rate>[/<encodingparameters>]
3219 const size_t expected_min_fields = 2;
3220 if (fields.size() < expected_min_fields) {
3221 return ParseFailedExpectMinFieldNum(line, expected_min_fields, error);
3222 }
3223 std::string payload_type_value;
3224 if (!GetValue(fields[0], kAttributeRtpmap, &payload_type_value, error)) {
3225 return false;
3226 }
wu@webrtc.org5e760e72014-04-02 23:19:09 +00003227 int payload_type = 0;
pkasting@chromium.orge9facf82015-02-17 20:36:28 +00003228 if (!GetPayloadTypeFromString(line, payload_type_value, &payload_type,
3229 error)) {
wu@webrtc.org5e760e72014-04-02 23:19:09 +00003230 return false;
3231 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003232
deadbeef67cf2c12016-04-13 10:07:16 -07003233 if (std::find(payload_types.begin(), payload_types.end(), payload_type) ==
3234 payload_types.end()) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01003235 RTC_LOG(LS_WARNING) << "Ignore rtpmap line that did not appear in the "
Jonas Olsson45cc8902018-02-13 10:37:07 +01003236 "<fmt> of the m-line: "
3237 << line;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003238 return true;
3239 }
jbauch083b73f2015-07-16 02:46:32 -07003240 const std::string& encoder = fields[1];
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003241 std::vector<std::string> codec_params;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00003242 rtc::split(encoder, '/', &codec_params);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003243 // <encoding name>/<clock rate>[/<encodingparameters>]
3244 // 2 mandatory fields
3245 if (codec_params.size() < 2 || codec_params.size() > 3) {
3246 return ParseFailed(line,
3247 "Expected format \"<encoding name>/<clock rate>"
3248 "[/<encodingparameters>]\".",
3249 error);
3250 }
jbauch083b73f2015-07-16 02:46:32 -07003251 const std::string& encoding_name = codec_params[0];
wu@webrtc.org5e760e72014-04-02 23:19:09 +00003252 int clock_rate = 0;
3253 if (!GetValueFromString(line, codec_params[1], &clock_rate, error)) {
3254 return false;
3255 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003256 if (media_type == cricket::MEDIA_TYPE_VIDEO) {
Steve Antonb1c1de12017-12-21 15:14:30 -08003257 VideoContentDescription* video_desc = media_desc->as_video();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003258 UpdateCodec(payload_type, encoding_name,
deadbeef67cf2c12016-04-13 10:07:16 -07003259 video_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003260 } else if (media_type == cricket::MEDIA_TYPE_AUDIO) {
3261 // RFC 4566
3262 // For audio streams, <encoding parameters> indicates the number
3263 // of audio channels. This parameter is OPTIONAL and may be
3264 // omitted if the number of channels is one, provided that no
3265 // additional parameters are needed.
Peter Kasting69558702016-01-12 16:26:35 -08003266 size_t channels = 1;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003267 if (codec_params.size() == 3) {
wu@webrtc.org5e760e72014-04-02 23:19:09 +00003268 if (!GetValueFromString(line, codec_params[2], &channels, error)) {
3269 return false;
3270 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003271 }
Steve Antonb1c1de12017-12-21 15:14:30 -08003272 AudioContentDescription* audio_desc = media_desc->as_audio();
ossue1405ad2017-01-23 08:55:48 -08003273 UpdateCodec(payload_type, encoding_name, clock_rate, 0, channels,
deadbeef67cf2c12016-04-13 10:07:16 -07003274 audio_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003275 } else if (media_type == cricket::MEDIA_TYPE_DATA) {
Steve Antonb1c1de12017-12-21 15:14:30 -08003276 DataContentDescription* data_desc = media_desc->as_data();
deadbeef67cf2c12016-04-13 10:07:16 -07003277 data_desc->AddCodec(cricket::DataCodec(payload_type, encoding_name));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003278 }
3279 return true;
3280}
3281
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003282bool ParseFmtpParam(const std::string& line, std::string* parameter,
3283 std::string* value, SdpParseError* error) {
Donald Curtis0e07f922015-05-15 09:21:23 -07003284 if (!rtc::tokenize_first(line, kSdpDelimiterEqual, parameter, value)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003285 ParseFailed(line, "Unable to parse fmtp parameter. \'=\' missing.", error);
3286 return false;
3287 }
3288 // a=fmtp:<payload_type> <param1>=<value1>; <param2>=<value2>; ...
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003289 return true;
3290}
3291
3292bool ParseFmtpAttributes(const std::string& line, const MediaType media_type,
3293 MediaContentDescription* media_desc,
3294 SdpParseError* error) {
3295 if (media_type != cricket::MEDIA_TYPE_AUDIO &&
3296 media_type != cricket::MEDIA_TYPE_VIDEO) {
3297 return true;
3298 }
Donald Curtis0e07f922015-05-15 09:21:23 -07003299
3300 std::string line_payload;
3301 std::string line_params;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003302
3303 // RFC 5576
3304 // a=fmtp:<format> <format specific parameters>
3305 // At least two fields, whereas the second one is any of the optional
3306 // parameters.
Donald Curtis144d0182015-05-15 13:14:24 -07003307 if (!rtc::tokenize_first(line.substr(kLinePrefixLength), kSdpDelimiterSpace,
3308 &line_payload, &line_params)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003309 ParseFailedExpectMinFieldNum(line, 2, error);
3310 return false;
3311 }
3312
Donald Curtis0e07f922015-05-15 09:21:23 -07003313 // Parse out the payload information.
pkasting@chromium.orgd3245462015-02-23 21:28:22 +00003314 std::string payload_type_str;
Donald Curtis0e07f922015-05-15 09:21:23 -07003315 if (!GetValue(line_payload, kAttributeFmtp, &payload_type_str, error)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003316 return false;
3317 }
3318
Donald Curtis0e07f922015-05-15 09:21:23 -07003319 int payload_type = 0;
Donald Curtis144d0182015-05-15 13:14:24 -07003320 if (!GetPayloadTypeFromString(line_payload, payload_type_str, &payload_type,
3321 error)) {
Donald Curtis0e07f922015-05-15 09:21:23 -07003322 return false;
3323 }
3324
3325 // Parse out format specific parameters.
3326 std::vector<std::string> fields;
3327 rtc::split(line_params, kSdpDelimiterSemicolon, &fields);
3328
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003329 cricket::CodecParameterMap codec_params;
Donald Curtis144d0182015-05-15 13:14:24 -07003330 for (auto& iter : fields) {
Donald Curtis0e07f922015-05-15 09:21:23 -07003331 if (iter.find(kSdpDelimiterEqual) == std::string::npos) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003332 // Only fmtps with equals are currently supported. Other fmtp types
3333 // should be ignored. Unknown fmtps do not constitute an error.
3334 continue;
3335 }
Donald Curtis0e07f922015-05-15 09:21:23 -07003336
3337 std::string name;
3338 std::string value;
3339 if (!ParseFmtpParam(rtc::string_trim(iter), &name, &value, error)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003340 return false;
3341 }
3342 codec_params[name] = value;
3343 }
3344
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003345 if (media_type == cricket::MEDIA_TYPE_AUDIO) {
3346 UpdateCodec<AudioContentDescription, cricket::AudioCodec>(
pkasting@chromium.orgd3245462015-02-23 21:28:22 +00003347 media_desc, payload_type, codec_params);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003348 } else if (media_type == cricket::MEDIA_TYPE_VIDEO) {
3349 UpdateCodec<VideoContentDescription, cricket::VideoCodec>(
pkasting@chromium.orgd3245462015-02-23 21:28:22 +00003350 media_desc, payload_type, codec_params);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003351 }
3352 return true;
3353}
3354
3355bool ParseRtcpFbAttribute(const std::string& line, const MediaType media_type,
3356 MediaContentDescription* media_desc,
3357 SdpParseError* error) {
3358 if (media_type != cricket::MEDIA_TYPE_AUDIO &&
3359 media_type != cricket::MEDIA_TYPE_VIDEO) {
3360 return true;
3361 }
3362 std::vector<std::string> rtcp_fb_fields;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00003363 rtc::split(line.c_str(), kSdpDelimiterSpace, &rtcp_fb_fields);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003364 if (rtcp_fb_fields.size() < 2) {
3365 return ParseFailedGetValue(line, kAttributeRtcpFb, error);
3366 }
3367 std::string payload_type_string;
3368 if (!GetValue(rtcp_fb_fields[0], kAttributeRtcpFb, &payload_type_string,
3369 error)) {
3370 return false;
3371 }
wu@webrtc.org5e760e72014-04-02 23:19:09 +00003372 int payload_type = kWildcardPayloadType;
3373 if (payload_type_string != "*") {
pkasting@chromium.orge9facf82015-02-17 20:36:28 +00003374 if (!GetPayloadTypeFromString(line, payload_type_string, &payload_type,
3375 error)) {
wu@webrtc.org5e760e72014-04-02 23:19:09 +00003376 return false;
3377 }
3378 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003379 std::string id = rtcp_fb_fields[1];
3380 std::string param = "";
3381 for (std::vector<std::string>::iterator iter = rtcp_fb_fields.begin() + 2;
3382 iter != rtcp_fb_fields.end(); ++iter) {
3383 param.append(*iter);
3384 }
3385 const cricket::FeedbackParam feedback_param(id, param);
3386
3387 if (media_type == cricket::MEDIA_TYPE_AUDIO) {
pkasting@chromium.orgd3245462015-02-23 21:28:22 +00003388 UpdateCodec<AudioContentDescription, cricket::AudioCodec>(
3389 media_desc, payload_type, feedback_param);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003390 } else if (media_type == cricket::MEDIA_TYPE_VIDEO) {
pkasting@chromium.orgd3245462015-02-23 21:28:22 +00003391 UpdateCodec<VideoContentDescription, cricket::VideoCodec>(
3392 media_desc, payload_type, feedback_param);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003393 }
3394 return true;
3395}
3396
3397} // namespace webrtc