blob: 2c3472891776256cd88d5cf27edeffccb7cf6185 [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.
decurtis@webrtc.org8af11042015-01-07 19:15:51 +0000460 if (line->length() < 3 ||
461 !islower(cline[0]) ||
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000462 cline[1] != kSdpDelimiterEqual ||
463 cline[2] == kSdpDelimiterSpace) {
464 *pos = line_begin;
465 return false;
466 }
467 return true;
468}
469
470// Init |os| to "|type|=|value|".
471static void InitLine(const char type,
472 const std::string& value,
473 std::ostringstream* os) {
474 os->str("");
475 *os << type << kSdpDelimiterEqual << value;
476}
477
478// Init |os| to "a=|attribute|".
479static void InitAttrLine(const std::string& attribute, std::ostringstream* os) {
480 InitLine(kLineTypeAttributes, attribute, os);
481}
482
483// Writes a SDP attribute line based on |attribute| and |value| to |message|.
484static void AddAttributeLine(const std::string& attribute, int value,
485 std::string* message) {
486 std::ostringstream os;
487 InitAttrLine(attribute, &os);
488 os << kSdpDelimiterColon << value;
489 AddLine(os.str(), message);
490}
491
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000492static bool IsLineType(const std::string& message,
493 const char type,
494 size_t line_start) {
495 if (message.size() < line_start + kLinePrefixLength) {
496 return false;
497 }
498 const char* cmessage = message.c_str();
499 return (cmessage[line_start] == type &&
500 cmessage[line_start + 1] == kSdpDelimiterEqual);
501}
502
503static bool IsLineType(const std::string& line,
504 const char type) {
505 return IsLineType(line, type, 0);
506}
507
508static bool GetLineWithType(const std::string& message, size_t* pos,
509 std::string* line, const char type) {
510 if (!IsLineType(message, type, *pos)) {
511 return false;
512 }
513
514 if (!GetLine(message, pos, line))
515 return false;
516
517 return true;
518}
519
520static bool HasAttribute(const std::string& line,
521 const std::string& attribute) {
522 return (line.compare(kLinePrefixLength, attribute.size(), attribute) == 0);
523}
524
Peter Boström0c4e06b2015-10-07 12:23:21 +0200525static bool AddSsrcLine(uint32_t ssrc_id,
526 const std::string& attribute,
527 const std::string& value,
528 std::string* message) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000529 // RFC 5576
530 // a=ssrc:<ssrc-id> <attribute>:<value>
531 std::ostringstream os;
532 InitAttrLine(kAttributeSsrc, &os);
533 os << kSdpDelimiterColon << ssrc_id << kSdpDelimiterSpace
534 << attribute << kSdpDelimiterColon << value;
535 return AddLine(os.str(), message);
536}
537
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000538// Get value only from <attribute>:<value>.
539static bool GetValue(const std::string& message, const std::string& attribute,
540 std::string* value, SdpParseError* error) {
541 std::string leftpart;
Donald Curtis0e07f922015-05-15 09:21:23 -0700542 if (!rtc::tokenize_first(message, kSdpDelimiterColon, &leftpart, value)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000543 return ParseFailedGetValue(message, attribute, error);
544 }
545 // The left part should end with the expected attribute.
546 if (leftpart.length() < attribute.length() ||
547 leftpart.compare(leftpart.length() - attribute.length(),
548 attribute.length(), attribute) != 0) {
549 return ParseFailedGetValue(message, attribute, error);
550 }
551 return true;
552}
553
554static bool CaseInsensitiveFind(std::string str1, std::string str2) {
555 std::transform(str1.begin(), str1.end(), str1.begin(),
556 ::tolower);
557 std::transform(str2.begin(), str2.end(), str2.begin(),
558 ::tolower);
559 return str1.find(str2) != std::string::npos;
560}
561
wu@webrtc.org5e760e72014-04-02 23:19:09 +0000562template <class T>
563static bool GetValueFromString(const std::string& line,
564 const std::string& s,
565 T* t,
566 SdpParseError* error) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000567 if (!rtc::FromString(s, t)) {
wu@webrtc.org5e760e72014-04-02 23:19:09 +0000568 std::ostringstream description;
569 description << "Invalid value: " << s << ".";
570 return ParseFailed(line, description.str(), error);
571 }
572 return true;
573}
574
pkasting@chromium.orge9facf82015-02-17 20:36:28 +0000575static bool GetPayloadTypeFromString(const std::string& line,
576 const std::string& s,
577 int* payload_type,
578 SdpParseError* error) {
579 return GetValueFromString(line, s, payload_type, error) &&
580 cricket::IsValidRtpPayloadType(*payload_type);
581}
582
Seth Hampson5897a6e2018-04-03 11:16:33 -0700583// Creates a StreamParams track in the case when no SSRC lines are signaled.
584// This is a track that does not contain SSRCs and only contains
585// stream_ids/track_id if it's signaled with a=msid lines.
586void CreateTrackWithNoSsrcs(const std::vector<std::string>& msid_stream_ids,
587 const std::string& msid_track_id,
588 StreamParamsVec* tracks) {
589 StreamParams track;
590 if (msid_track_id.empty() || msid_stream_ids.empty()) {
591 // We only create an unsignaled track if a=msid lines were signaled.
592 return;
593 }
594 track.set_stream_ids(msid_stream_ids);
595 track.id = msid_track_id;
596 tracks->push_back(track);
597}
598
599// Creates the StreamParams tracks, for the case when SSRC lines are signaled.
600// |msid_stream_ids| and |msid_track_id| represent the stream/track ID from the
Taylor Brandstetter5de6b752016-03-09 17:02:30 -0800601// "a=msid" attribute, if it exists. They are empty if the attribute does not
Seth Hampson5897a6e2018-04-03 11:16:33 -0700602// exist. We prioritize getting stream_ids/track_ids signaled in a=msid lines.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000603void CreateTracksFromSsrcInfos(const SsrcInfoVec& ssrc_infos,
Seth Hampson5b4f0752018-04-02 16:31:36 -0700604 const std::vector<std::string>& msid_stream_ids,
Taylor Brandstetter5de6b752016-03-09 17:02:30 -0800605 const std::string& msid_track_id,
Seth Hampson5b4f0752018-04-02 16:31:36 -0700606 StreamParamsVec* tracks,
607 int msid_signaling) {
nisseede5da42017-01-12 05:15:36 -0800608 RTC_DCHECK(tracks != NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000609 for (SsrcInfoVec::const_iterator ssrc_info = ssrc_infos.begin();
610 ssrc_info != ssrc_infos.end(); ++ssrc_info) {
611 if (ssrc_info->cname.empty()) {
612 continue;
613 }
614
Seth Hampson5b4f0752018-04-02 16:31:36 -0700615 std::vector<std::string> stream_ids;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000616 std::string track_id;
Seth Hampson5b4f0752018-04-02 16:31:36 -0700617 if (msid_signaling & cricket::kMsidSignalingMediaSection) {
618 // This is the case with Unified Plan SDP msid signaling.
619 stream_ids = msid_stream_ids;
Tomas Gunnarsson191bf5c2018-03-30 10:44:43 +0000620 track_id = msid_track_id;
Seth Hampson5b4f0752018-04-02 16:31:36 -0700621 } else if (msid_signaling & cricket::kMsidSignalingSsrcAttribute) {
622 // This is the case with Plan B SDP msid signaling.
623 stream_ids.push_back(ssrc_info->stream_id);
Tomas Gunnarsson191bf5c2018-03-30 10:44:43 +0000624 track_id = ssrc_info->track_id;
Seth Hampson5b4f0752018-04-02 16:31:36 -0700625 } else if (!ssrc_info->mslabel.empty()) {
626 // Since there's no a=msid or a=ssrc msid signaling, this is a sdp from
627 // an older version of client that doesn't support msid.
628 // In that case, we use the mslabel and label to construct the track.
629 stream_ids.push_back(ssrc_info->mslabel);
630 track_id = ssrc_info->label;
631 } else {
632 // Since no media streams isn't supported with older SDP signaling, we
633 // use a default a stream id.
634 stream_ids.push_back(kDefaultMsid);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000635 }
Seth Hampson5b4f0752018-04-02 16:31:36 -0700636 // If a track ID wasn't populated from the SSRC attributes OR the
Taylor Brandstetter5de6b752016-03-09 17:02:30 -0800637 // msid attribute, use default/random values.
Taylor Brandstetter5de6b752016-03-09 17:02:30 -0800638 if (track_id.empty()) {
639 // TODO(ronghuawu): What should we do if the track id doesn't appear?
640 // Create random string (which will be used as track label later)?
641 track_id = rtc::CreateRandomString(8);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000642 }
643
644 StreamParamsVec::iterator track = tracks->begin();
645 for (; track != tracks->end(); ++track) {
646 if (track->id == track_id) {
647 break;
648 }
649 }
650 if (track == tracks->end()) {
651 // If we don't find an existing track, create a new one.
652 tracks->push_back(StreamParams());
653 track = tracks->end() - 1;
654 }
655 track->add_ssrc(ssrc_info->ssrc_id);
656 track->cname = ssrc_info->cname;
Seth Hampson5b4f0752018-04-02 16:31:36 -0700657 track->set_stream_ids(stream_ids);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000658 track->id = track_id;
659 }
660}
661
Seth Hampson845e8782018-03-02 11:34:10 -0800662void GetMediaStreamIds(const ContentInfo* content,
663 std::set<std::string>* labels) {
Steve Antonb1c1de12017-12-21 15:14:30 -0800664 for (const StreamParams& stream_params :
665 content->media_description()->streams()) {
Seth Hampson845e8782018-03-02 11:34:10 -0800666 for (const std::string& stream_id : stream_params.stream_ids()) {
667 labels->insert(stream_id);
Steve Anton5a26a3a2018-02-28 11:38:47 -0800668 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000669 }
670}
671
672// RFC 5245
673// It is RECOMMENDED that default candidates be chosen based on the
674// likelihood of those candidates to work with the peer that is being
675// contacted. It is RECOMMENDED that relayed > reflexive > host.
676static const int kPreferenceUnknown = 0;
677static const int kPreferenceHost = 1;
678static const int kPreferenceReflexive = 2;
679static const int kPreferenceRelayed = 3;
680
681static int GetCandidatePreferenceFromType(const std::string& type) {
682 int preference = kPreferenceUnknown;
683 if (type == cricket::LOCAL_PORT_TYPE) {
684 preference = kPreferenceHost;
685 } else if (type == cricket::STUN_PORT_TYPE) {
686 preference = kPreferenceReflexive;
687 } else if (type == cricket::RELAY_PORT_TYPE) {
688 preference = kPreferenceRelayed;
689 } else {
nissec80e7412017-01-11 05:56:46 -0800690 RTC_NOTREACHED();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000691 }
692 return preference;
693}
694
guoweis@webrtc.org57ac2c82015-02-06 00:45:13 +0000695// Get ip and port of the default destination from the |candidates| with the
696// given value of |component_id|. The default candidate should be the one most
697// likely to work, typically IPv4 relay.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000698// RFC 5245
699// The value of |component_id| currently supported are 1 (RTP) and 2 (RTCP).
Steve Anton36b29d12017-10-30 09:57:42 -0700700// TODO(deadbeef): Decide the default destination in webrtcsession and
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000701// pass it down via SessionDescription.
pthatcher@webrtc.orgc9d6d142014-10-23 23:37:22 +0000702static void GetDefaultDestination(
703 const std::vector<Candidate>& candidates,
704 int component_id, std::string* port,
705 std::string* ip, std::string* addr_type) {
perkj@webrtc.orgd105cc82014-11-07 11:22:06 +0000706 *addr_type = kConnectionIpv4Addrtype;
pthatcher@webrtc.orgc9d6d142014-10-23 23:37:22 +0000707 *port = kDummyPort;
708 *ip = kDummyAddress;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000709 int current_preference = kPreferenceUnknown;
guoweis@webrtc.org57ac2c82015-02-06 00:45:13 +0000710 int current_family = AF_UNSPEC;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000711 for (std::vector<Candidate>::const_iterator it = candidates.begin();
712 it != candidates.end(); ++it) {
713 if (it->component() != component_id) {
714 continue;
715 }
guoweis@webrtc.org57ac2c82015-02-06 00:45:13 +0000716 // Default destination should be UDP only.
717 if (it->protocol() != cricket::UDP_PROTOCOL_NAME) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000718 continue;
719 }
guoweis@webrtc.org57ac2c82015-02-06 00:45:13 +0000720 const int preference = GetCandidatePreferenceFromType(it->type());
721 const int family = it->address().ipaddr().family();
722 // See if this candidate is more preferable then the current one if it's the
723 // same family. Or if the current family is IPv4 already so we could safely
724 // ignore all IPv6 ones. WebRTC bug 4269.
725 // http://code.google.com/p/webrtc/issues/detail?id=4269
726 if ((preference <= current_preference && current_family == family) ||
727 (current_family == AF_INET && family == AF_INET6)) {
728 continue;
729 }
pthatcher@webrtc.orgc9d6d142014-10-23 23:37:22 +0000730 if (family == AF_INET) {
731 addr_type->assign(kConnectionIpv4Addrtype);
732 } else if (family == AF_INET6) {
733 addr_type->assign(kConnectionIpv6Addrtype);
734 }
guoweis@webrtc.org57ac2c82015-02-06 00:45:13 +0000735 current_preference = preference;
736 current_family = family;
737 *port = it->address().PortAsString();
738 *ip = it->address().ipaddr().ToString();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000739 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000740}
741
wu@webrtc.org4c3e9912014-07-16 21:03:13 +0000742// Gets "a=rtcp" line if found default RTCP candidate from |candidates|.
743static std::string GetRtcpLine(const std::vector<Candidate>& candidates) {
pthatcher@webrtc.orgc9d6d142014-10-23 23:37:22 +0000744 std::string rtcp_line, rtcp_port, rtcp_ip, addr_type;
745 GetDefaultDestination(candidates, ICE_CANDIDATE_COMPONENT_RTCP,
746 &rtcp_port, &rtcp_ip, &addr_type);
747 // Found default RTCP candidate.
748 // RFC 5245
749 // If the agent is utilizing RTCP, it MUST encode the RTCP candidate
750 // using the a=rtcp attribute as defined in RFC 3605.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000751
pthatcher@webrtc.orgc9d6d142014-10-23 23:37:22 +0000752 // RFC 3605
753 // rtcp-attribute = "a=rtcp:" port [nettype space addrtype space
754 // connection-address] CRLF
755 std::ostringstream os;
756 InitAttrLine(kAttributeRtcp, &os);
757 os << kSdpDelimiterColon
758 << rtcp_port << " "
759 << kConnectionNettype << " "
760 << addr_type << " "
761 << rtcp_ip;
762 rtcp_line = os.str();
wu@webrtc.org4c3e9912014-07-16 21:03:13 +0000763 return rtcp_line;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000764}
765
766// Get candidates according to the mline index from SessionDescriptionInterface.
767static void GetCandidatesByMindex(const SessionDescriptionInterface& desci,
768 int mline_index,
769 std::vector<Candidate>* candidates) {
770 if (!candidates) {
771 return;
772 }
773 const IceCandidateCollection* cc = desci.candidates(mline_index);
774 for (size_t i = 0; i < cc->count(); ++i) {
775 const IceCandidateInterface* candidate = cc->at(i);
776 candidates->push_back(candidate->candidate());
777 }
778}
779
deadbeef90f1e1e2017-02-10 12:35:05 -0800780static bool IsValidPort(int port) {
781 return port >= 0 && port <= 65535;
782}
783
Steve Antone831b8c2018-02-01 12:22:16 -0800784std::string SdpSerialize(const JsepSessionDescription& jdesc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000785 const cricket::SessionDescription* desc = jdesc.description();
786 if (!desc) {
787 return "";
788 }
789
790 std::string message;
791
792 // Session Description.
793 AddLine(kSessionVersion, &message);
794 // Session Origin
795 // RFC 4566
796 // o=<username> <sess-id> <sess-version> <nettype> <addrtype>
797 // <unicast-address>
798 std::ostringstream os;
799 InitLine(kLineTypeOrigin, kSessionOriginUsername, &os);
jbauch083b73f2015-07-16 02:46:32 -0700800 const std::string& session_id = jdesc.session_id().empty() ?
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000801 kSessionOriginSessionId : jdesc.session_id();
jbauch083b73f2015-07-16 02:46:32 -0700802 const std::string& session_version = jdesc.session_version().empty() ?
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000803 kSessionOriginSessionVersion : jdesc.session_version();
804 os << " " << session_id << " " << session_version << " "
805 << kSessionOriginNettype << " " << kSessionOriginAddrtype << " "
806 << kSessionOriginAddress;
807 AddLine(os.str(), &message);
808 AddLine(kSessionName, &message);
809
810 // Time Description.
811 AddLine(kTimeDescription, &message);
812
813 // Group
814 if (desc->HasGroup(cricket::GROUP_TYPE_BUNDLE)) {
815 std::string group_line = kAttrGroup;
816 const cricket::ContentGroup* group =
817 desc->GetGroupByName(cricket::GROUP_TYPE_BUNDLE);
nisseede5da42017-01-12 05:15:36 -0800818 RTC_DCHECK(group != NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000819 const cricket::ContentNames& content_names = group->content_names();
820 for (cricket::ContentNames::const_iterator it = content_names.begin();
821 it != content_names.end(); ++it) {
822 group_line.append(" ");
823 group_line.append(*it);
824 }
825 AddLine(group_line, &message);
826 }
827
828 // MediaStream semantics
829 InitAttrLine(kAttributeMsidSemantics, &os);
830 os << kSdpDelimiterColon << " " << kMediaStreamSemantic;
wu@webrtc.org4c3e9912014-07-16 21:03:13 +0000831
Seth Hampson845e8782018-03-02 11:34:10 -0800832 std::set<std::string> media_stream_ids;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000833 const ContentInfo* audio_content = GetFirstAudioContent(desc);
834 if (audio_content)
Seth Hampson845e8782018-03-02 11:34:10 -0800835 GetMediaStreamIds(audio_content, &media_stream_ids);
wu@webrtc.org4c3e9912014-07-16 21:03:13 +0000836
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000837 const ContentInfo* video_content = GetFirstVideoContent(desc);
838 if (video_content)
Seth Hampson845e8782018-03-02 11:34:10 -0800839 GetMediaStreamIds(video_content, &media_stream_ids);
wu@webrtc.org4c3e9912014-07-16 21:03:13 +0000840
Seth Hampson845e8782018-03-02 11:34:10 -0800841 for (std::set<std::string>::const_iterator it = media_stream_ids.begin();
842 it != media_stream_ids.end(); ++it) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000843 os << " " << *it;
844 }
845 AddLine(os.str(), &message);
846
wu@webrtc.org4c3e9912014-07-16 21:03:13 +0000847 // Preserve the order of the media contents.
848 int mline_index = -1;
849 for (cricket::ContentInfos::const_iterator it = desc->contents().begin();
850 it != desc->contents().end(); ++it) {
Steve Antonb1c1de12017-12-21 15:14:30 -0800851 const MediaContentDescription* mdesc = it->media_description();
wu@webrtc.org4c3e9912014-07-16 21:03:13 +0000852 std::vector<Candidate> candidates;
853 GetCandidatesByMindex(jdesc, ++mline_index, &candidates);
deadbeef9d3584c2016-02-16 17:54:10 -0800854 BuildMediaDescription(&*it, desc->GetTransportInfoByName(it->name),
Steve Antone831b8c2018-02-01 12:22:16 -0800855 mdesc->type(), candidates, desc->msid_signaling(),
wu@webrtc.org4c3e9912014-07-16 21:03:13 +0000856 &message);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000857 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000858 return message;
859}
860
861// Serializes the passed in IceCandidateInterface to a SDP string.
862// candidate - The candidate to be serialized.
Honghai Zhang7fb69db2016-03-14 11:59:18 -0700863std::string SdpSerializeCandidate(const IceCandidateInterface& candidate) {
864 return SdpSerializeCandidate(candidate.candidate());
865}
866
867// Serializes a cricket Candidate.
868std::string SdpSerializeCandidate(const cricket::Candidate& candidate) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000869 std::string message;
Honghai Zhang7fb69db2016-03-14 11:59:18 -0700870 std::vector<cricket::Candidate> candidates(1, candidate);
honghaiza54a0802015-12-16 18:37:23 -0800871 BuildCandidate(candidates, true, &message);
wu@webrtc.orgec9f5fb2014-06-24 17:05:10 +0000872 // From WebRTC draft section 4.8.1.1 candidate-attribute will be
873 // just candidate:<candidate> not a=candidate:<blah>CRLF
nisseede5da42017-01-12 05:15:36 -0800874 RTC_DCHECK(message.find("a=") == 0);
wu@webrtc.orgec9f5fb2014-06-24 17:05:10 +0000875 message.erase(0, 2);
nisseede5da42017-01-12 05:15:36 -0800876 RTC_DCHECK(message.find(kLineBreak) == message.size() - 2);
wu@webrtc.orgec9f5fb2014-06-24 17:05:10 +0000877 message.resize(message.size() - 2);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000878 return message;
879}
880
881bool SdpDeserialize(const std::string& message,
882 JsepSessionDescription* jdesc,
883 SdpParseError* error) {
884 std::string session_id;
885 std::string session_version;
Peter Thatcher7cbd1882015-09-17 18:54:52 -0700886 TransportDescription session_td("", "");
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000887 RtpHeaderExtensions session_extmaps;
zhihuang38989e52017-03-21 11:04:53 -0700888 rtc::SocketAddress session_connection_addr;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000889 cricket::SessionDescription* desc = new cricket::SessionDescription();
890 std::vector<JsepIceCandidate*> candidates;
891 size_t current_pos = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000892
893 // Session Description
894 if (!ParseSessionDescription(message, &current_pos, &session_id,
deadbeefc80741f2015-10-22 13:14:45 -0700895 &session_version, &session_td, &session_extmaps,
zhihuang38989e52017-03-21 11:04:53 -0700896 &session_connection_addr, desc, error)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000897 delete desc;
898 return false;
899 }
900
901 // Media Description
deadbeefc80741f2015-10-22 13:14:45 -0700902 if (!ParseMediaDescription(message, session_td, session_extmaps, &current_pos,
zhihuang38989e52017-03-21 11:04:53 -0700903 session_connection_addr, desc, &candidates,
904 error)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000905 delete desc;
906 for (std::vector<JsepIceCandidate*>::const_iterator
907 it = candidates.begin(); it != candidates.end(); ++it) {
908 delete *it;
909 }
910 return false;
911 }
912
913 jdesc->Initialize(desc, session_id, session_version);
914
915 for (std::vector<JsepIceCandidate*>::const_iterator
916 it = candidates.begin(); it != candidates.end(); ++it) {
917 jdesc->AddCandidate(*it);
918 delete *it;
919 }
920 return true;
921}
922
923bool SdpDeserializeCandidate(const std::string& message,
924 JsepIceCandidate* jcandidate,
925 SdpParseError* error) {
nisseede5da42017-01-12 05:15:36 -0800926 RTC_DCHECK(jcandidate != NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000927 Candidate candidate;
928 if (!ParseCandidate(message, &candidate, error, true)) {
929 return false;
930 }
931 jcandidate->SetCandidate(candidate);
932 return true;
933}
934
Honghai Zhang7fb69db2016-03-14 11:59:18 -0700935bool SdpDeserializeCandidate(const std::string& transport_name,
936 const std::string& message,
937 cricket::Candidate* candidate,
938 SdpParseError* error) {
nisseede5da42017-01-12 05:15:36 -0800939 RTC_DCHECK(candidate != nullptr);
Honghai Zhang7fb69db2016-03-14 11:59:18 -0700940 if (!ParseCandidate(message, candidate, error, true)) {
941 return false;
942 }
943 candidate->set_transport_name(transport_name);
944 return true;
945}
946
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000947bool ParseCandidate(const std::string& message, Candidate* candidate,
948 SdpParseError* error, bool is_raw) {
nisseede5da42017-01-12 05:15:36 -0800949 RTC_DCHECK(candidate != NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000950
951 // Get the first line from |message|.
jiayl@webrtc.org7ec3f9f2014-08-08 23:09:15 +0000952 std::string first_line = message;
953 size_t pos = 0;
954 GetLine(message, &pos, &first_line);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000955
jiayl@webrtc.org7ec3f9f2014-08-08 23:09:15 +0000956 // Makes sure |message| contains only one line.
957 if (message.size() > first_line.size()) {
958 std::string left, right;
Donald Curtis0e07f922015-05-15 09:21:23 -0700959 if (rtc::tokenize_first(message, kNewLine, &left, &right) &&
960 !right.empty()) {
jiayl@webrtc.org7ec3f9f2014-08-08 23:09:15 +0000961 return ParseFailed(message, 0, "Expect one line only", error);
962 }
963 }
964
965 // From WebRTC draft section 4.8.1.1 candidate-attribute should be
966 // candidate:<candidate> when trickled, but we still support
967 // a=candidate:<blah>CRLF for backward compatibility and for parsing a line
968 // from the SDP.
969 if (IsLineType(first_line, kLineTypeAttributes)) {
970 first_line = first_line.substr(kLinePrefixLength);
971 }
972
973 std::string attribute_candidate;
974 std::string candidate_value;
975
976 // |first_line| must be in the form of "candidate:<value>".
Donald Curtis144d0182015-05-15 13:14:24 -0700977 if (!rtc::tokenize_first(first_line, kSdpDelimiterColon, &attribute_candidate,
978 &candidate_value) ||
jiayl@webrtc.org7ec3f9f2014-08-08 23:09:15 +0000979 attribute_candidate != kAttributeCandidate) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000980 if (is_raw) {
981 std::ostringstream description;
jiayl@webrtc.org7ec3f9f2014-08-08 23:09:15 +0000982 description << "Expect line: " << kAttributeCandidate
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000983 << ":" << "<candidate-str>";
984 return ParseFailed(first_line, 0, description.str(), error);
985 } else {
986 return ParseFailedExpectLine(first_line, 0, kLineTypeAttributes,
987 kAttributeCandidate, error);
988 }
989 }
990
991 std::vector<std::string> fields;
jiayl@webrtc.org7ec3f9f2014-08-08 23:09:15 +0000992 rtc::split(candidate_value, kSdpDelimiterSpace, &fields);
993
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000994 // RFC 5245
995 // a=candidate:<foundation> <component-id> <transport> <priority>
996 // <connection-address> <port> typ <candidate-types>
997 // [raddr <connection-address>] [rport <port>]
998 // *(SP extension-att-name SP extension-att-value)
999 const size_t expected_min_fields = 8;
1000 if (fields.size() < expected_min_fields ||
1001 (fields[6] != kAttributeCandidateTyp)) {
1002 return ParseFailedExpectMinFieldNum(first_line, expected_min_fields, error);
1003 }
jbauch083b73f2015-07-16 02:46:32 -07001004 const std::string& foundation = fields[0];
jiayl@webrtc.org7ec3f9f2014-08-08 23:09:15 +00001005
wu@webrtc.org5e760e72014-04-02 23:19:09 +00001006 int component_id = 0;
1007 if (!GetValueFromString(first_line, fields[1], &component_id, error)) {
1008 return false;
1009 }
jbauch083b73f2015-07-16 02:46:32 -07001010 const std::string& transport = fields[2];
Peter Boström0c4e06b2015-10-07 12:23:21 +02001011 uint32_t priority = 0;
wu@webrtc.org5e760e72014-04-02 23:19:09 +00001012 if (!GetValueFromString(first_line, fields[3], &priority, error)) {
1013 return false;
1014 }
jbauch083b73f2015-07-16 02:46:32 -07001015 const std::string& connection_address = fields[4];
wu@webrtc.org5e760e72014-04-02 23:19:09 +00001016 int port = 0;
1017 if (!GetValueFromString(first_line, fields[5], &port, error)) {
1018 return false;
1019 }
deadbeef90f1e1e2017-02-10 12:35:05 -08001020 if (!IsValidPort(port)) {
1021 return ParseFailed(first_line, "Invalid port number.", error);
1022 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001023 SocketAddress address(connection_address, port);
1024
1025 cricket::ProtocolType protocol;
hnslb68cc752016-12-13 10:33:41 -08001026 if (!StringToProto(transport.c_str(), &protocol)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001027 return ParseFailed(first_line, "Unsupported transport type.", error);
1028 }
hnslb68cc752016-12-13 10:33:41 -08001029 switch (protocol) {
1030 case cricket::PROTO_UDP:
1031 case cricket::PROTO_TCP:
1032 case cricket::PROTO_SSLTCP:
1033 // Supported protocol.
1034 break;
1035 default:
1036 return ParseFailed(first_line, "Unsupported transport type.", error);
1037 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001038
1039 std::string candidate_type;
jbauch083b73f2015-07-16 02:46:32 -07001040 const std::string& type = fields[7];
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001041 if (type == kCandidateHost) {
1042 candidate_type = cricket::LOCAL_PORT_TYPE;
1043 } else if (type == kCandidateSrflx) {
1044 candidate_type = cricket::STUN_PORT_TYPE;
1045 } else if (type == kCandidateRelay) {
1046 candidate_type = cricket::RELAY_PORT_TYPE;
Honghai Zhang7fb69db2016-03-14 11:59:18 -07001047 } else if (type == kCandidatePrflx) {
1048 candidate_type = cricket::PRFLX_PORT_TYPE;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001049 } else {
1050 return ParseFailed(first_line, "Unsupported candidate type.", error);
1051 }
1052
1053 size_t current_position = expected_min_fields;
1054 SocketAddress related_address;
1055 // The 2 optional fields for related address
1056 // [raddr <connection-address>] [rport <port>]
1057 if (fields.size() >= (current_position + 2) &&
1058 fields[current_position] == kAttributeCandidateRaddr) {
1059 related_address.SetIP(fields[++current_position]);
1060 ++current_position;
1061 }
1062 if (fields.size() >= (current_position + 2) &&
1063 fields[current_position] == kAttributeCandidateRport) {
wu@webrtc.org5e760e72014-04-02 23:19:09 +00001064 int port = 0;
1065 if (!GetValueFromString(
1066 first_line, fields[++current_position], &port, error)) {
1067 return false;
1068 }
deadbeef90f1e1e2017-02-10 12:35:05 -08001069 if (!IsValidPort(port)) {
1070 return ParseFailed(first_line, "Invalid port number.", error);
1071 }
wu@webrtc.org5e760e72014-04-02 23:19:09 +00001072 related_address.SetPort(port);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001073 ++current_position;
1074 }
1075
mallinath@webrtc.org2d60c5e2014-08-08 22:29:20 +00001076 // If this is a TCP candidate, it has additional extension as defined in
1077 // RFC 6544.
1078 std::string tcptype;
1079 if (fields.size() >= (current_position + 2) &&
1080 fields[current_position] == kTcpCandidateType) {
1081 tcptype = fields[++current_position];
1082 ++current_position;
1083
1084 if (tcptype != cricket::TCPTYPE_ACTIVE_STR &&
1085 tcptype != cricket::TCPTYPE_PASSIVE_STR &&
1086 tcptype != cricket::TCPTYPE_SIMOPEN_STR) {
1087 return ParseFailed(first_line, "Invalid TCP candidate type.", error);
1088 }
1089
1090 if (protocol != cricket::PROTO_TCP) {
1091 return ParseFailed(first_line, "Invalid non-TCP candidate", error);
1092 }
1093 }
1094
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001095 // Extension
honghaiza54a0802015-12-16 18:37:23 -08001096 // Though non-standard, we support the ICE ufrag and pwd being signaled on
1097 // the candidate to avoid issues with confusing which generation a candidate
1098 // belongs to when trickling multiple generations at the same time.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001099 std::string username;
1100 std::string password;
Peter Boström0c4e06b2015-10-07 12:23:21 +02001101 uint32_t generation = 0;
honghaiza0c44ea2016-03-23 16:07:48 -07001102 uint16_t network_id = 0;
1103 uint16_t network_cost = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001104 for (size_t i = current_position; i + 1 < fields.size(); ++i) {
1105 // RFC 5245
1106 // *(SP extension-att-name SP extension-att-value)
1107 if (fields[i] == kAttributeCandidateGeneration) {
wu@webrtc.org5e760e72014-04-02 23:19:09 +00001108 if (!GetValueFromString(first_line, fields[++i], &generation, error)) {
1109 return false;
1110 }
honghaiza54a0802015-12-16 18:37:23 -08001111 } else if (fields[i] == kAttributeCandidateUfrag) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001112 username = fields[++i];
honghaiza54a0802015-12-16 18:37:23 -08001113 } else if (fields[i] == kAttributeCandidatePwd) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001114 password = fields[++i];
honghaiza0c44ea2016-03-23 16:07:48 -07001115 } else if (fields[i] == kAttributeCandidateNetworkId) {
1116 if (!GetValueFromString(first_line, fields[++i], &network_id, error)) {
1117 return false;
1118 }
honghaize1a0c942016-02-16 14:54:56 -08001119 } else if (fields[i] == kAttributeCandidateNetworkCost) {
1120 if (!GetValueFromString(first_line, fields[++i], &network_cost, error)) {
1121 return false;
1122 }
Honghai Zhang351d77b2016-05-20 15:08:29 -07001123 network_cost = std::min(network_cost, rtc::kNetworkCostMax);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001124 } else {
1125 // Skip the unknown extension.
1126 ++i;
1127 }
1128 }
1129
guoweis@webrtc.org61c12472015-01-15 06:53:07 +00001130 *candidate = Candidate(component_id, cricket::ProtoToString(protocol),
guoweis@webrtc.org950c5182014-12-16 23:01:31 +00001131 address, priority, username, password, candidate_type,
honghaiza0c44ea2016-03-23 16:07:48 -07001132 generation, foundation, network_id, network_cost);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001133 candidate->set_related_address(related_address);
mallinath@webrtc.org2d60c5e2014-08-08 22:29:20 +00001134 candidate->set_tcptype(tcptype);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001135 return true;
1136}
1137
1138bool ParseIceOptions(const std::string& line,
1139 std::vector<std::string>* transport_options,
1140 SdpParseError* error) {
1141 std::string ice_options;
1142 if (!GetValue(line, kAttributeIceOption, &ice_options, error)) {
1143 return false;
1144 }
1145 std::vector<std::string> fields;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001146 rtc::split(ice_options, kSdpDelimiterSpace, &fields);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001147 for (size_t i = 0; i < fields.size(); ++i) {
1148 transport_options->push_back(fields[i]);
1149 }
1150 return true;
1151}
1152
jiayl@webrtc.orgddb85ab2014-09-05 16:31:56 +00001153bool ParseSctpPort(const std::string& line,
1154 int* sctp_port,
1155 SdpParseError* error) {
1156 // draft-ietf-mmusic-sctp-sdp-07
1157 // a=sctp-port
1158 std::vector<std::string> fields;
jiayl@webrtc.orgddb85ab2014-09-05 16:31:56 +00001159 const size_t expected_min_fields = 2;
lally69f57602015-10-08 10:15:04 -07001160 rtc::split(line.substr(kLinePrefixLength), kSdpDelimiterColon, &fields);
1161 if (fields.size() < expected_min_fields) {
1162 fields.resize(0);
1163 rtc::split(line.substr(kLinePrefixLength), kSdpDelimiterSpace, &fields);
1164 }
jiayl@webrtc.orgddb85ab2014-09-05 16:31:56 +00001165 if (fields.size() < expected_min_fields) {
1166 return ParseFailedExpectMinFieldNum(line, expected_min_fields, error);
1167 }
1168 if (!rtc::FromString(fields[1], sctp_port)) {
lally69f57602015-10-08 10:15:04 -07001169 return ParseFailed(line, "Invalid sctp port value.", error);
jiayl@webrtc.orgddb85ab2014-09-05 16:31:56 +00001170 }
1171 return true;
1172}
1173
isheriff6f8d6862016-05-26 11:24:55 -07001174bool ParseExtmap(const std::string& line,
1175 RtpExtension* extmap,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001176 SdpParseError* error) {
1177 // RFC 5285
1178 // a=extmap:<value>["/"<direction>] <URI> <extensionattributes>
1179 std::vector<std::string> fields;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001180 rtc::split(line.substr(kLinePrefixLength),
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001181 kSdpDelimiterSpace, &fields);
1182 const size_t expected_min_fields = 2;
1183 if (fields.size() < expected_min_fields) {
1184 return ParseFailedExpectMinFieldNum(line, expected_min_fields, error);
1185 }
1186 std::string uri = fields[1];
1187
1188 std::string value_direction;
1189 if (!GetValue(fields[0], kAttributeExtmap, &value_direction, error)) {
1190 return false;
1191 }
1192 std::vector<std::string> sub_fields;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001193 rtc::split(value_direction, kSdpDelimiterSlash, &sub_fields);
wu@webrtc.org5e760e72014-04-02 23:19:09 +00001194 int value = 0;
1195 if (!GetValueFromString(line, sub_fields[0], &value, error)) {
1196 return false;
1197 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001198
jbauch5869f502017-06-29 12:31:36 -07001199 bool encrypted = false;
1200 if (uri == RtpExtension::kEncryptHeaderExtensionsUri) {
1201 // RFC 6904
Steve Anton36b29d12017-10-30 09:57:42 -07001202 // a=extmap:<value["/"<direction>] urn:ietf:params:rtp-hdrext:encrypt <URI>
1203 // <extensionattributes>
jbauch5869f502017-06-29 12:31:36 -07001204 const size_t expected_min_fields_encrypted = expected_min_fields + 1;
1205 if (fields.size() < expected_min_fields_encrypted) {
1206 return ParseFailedExpectMinFieldNum(line, expected_min_fields_encrypted,
1207 error);
1208 }
1209
1210 encrypted = true;
1211 uri = fields[2];
1212 if (uri == RtpExtension::kEncryptHeaderExtensionsUri) {
1213 return ParseFailed(line, "Recursive encrypted header.", error);
1214 }
1215 }
1216
1217 *extmap = RtpExtension(uri, value, encrypted);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001218 return true;
1219}
1220
1221void BuildMediaDescription(const ContentInfo* content_info,
1222 const TransportInfo* transport_info,
1223 const MediaType media_type,
wu@webrtc.org4c3e9912014-07-16 21:03:13 +00001224 const std::vector<Candidate>& candidates,
Steve Antone831b8c2018-02-01 12:22:16 -08001225 int msid_signaling,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001226 std::string* message) {
nisseede5da42017-01-12 05:15:36 -08001227 RTC_DCHECK(message != NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001228 if (content_info == NULL || message == NULL) {
1229 return;
1230 }
Steve Anton36b29d12017-10-30 09:57:42 -07001231 // TODO(deadbeef): Rethink if we should use sprintfn instead of stringstream.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001232 // According to the style guide, streams should only be used for logging.
1233 // http://google-styleguide.googlecode.com/svn/
1234 // trunk/cppguide.xml?showone=Streams#Streams
1235 std::ostringstream os;
Steve Antonb1c1de12017-12-21 15:14:30 -08001236 const MediaContentDescription* media_desc = content_info->media_description();
1237 RTC_DCHECK(media_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001238
jiayl@webrtc.org9c16c392014-05-01 18:30:30 +00001239 int sctp_port = cricket::kSctpDefaultPort;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001240
1241 // RFC 4566
1242 // m=<media> <port> <proto> <fmt>
1243 // fmt is a list of payload type numbers that MAY be used in the session.
1244 const char* type = NULL;
1245 if (media_type == cricket::MEDIA_TYPE_AUDIO)
1246 type = kMediaTypeAudio;
1247 else if (media_type == cricket::MEDIA_TYPE_VIDEO)
1248 type = kMediaTypeVideo;
1249 else if (media_type == cricket::MEDIA_TYPE_DATA)
1250 type = kMediaTypeData;
1251 else
nissec80e7412017-01-11 05:56:46 -08001252 RTC_NOTREACHED();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001253
1254 std::string fmt;
1255 if (media_type == cricket::MEDIA_TYPE_VIDEO) {
Steve Antonb1c1de12017-12-21 15:14:30 -08001256 const VideoContentDescription* video_desc = media_desc->as_video();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001257 for (std::vector<cricket::VideoCodec>::const_iterator it =
1258 video_desc->codecs().begin();
1259 it != video_desc->codecs().end(); ++it) {
1260 fmt.append(" ");
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001261 fmt.append(rtc::ToString<int>(it->id));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001262 }
1263 } else if (media_type == cricket::MEDIA_TYPE_AUDIO) {
Steve Antonb1c1de12017-12-21 15:14:30 -08001264 const AudioContentDescription* audio_desc = media_desc->as_audio();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001265 for (std::vector<cricket::AudioCodec>::const_iterator it =
1266 audio_desc->codecs().begin();
1267 it != audio_desc->codecs().end(); ++it) {
1268 fmt.append(" ");
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001269 fmt.append(rtc::ToString<int>(it->id));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001270 }
1271 } else if (media_type == cricket::MEDIA_TYPE_DATA) {
Steve Antonb1c1de12017-12-21 15:14:30 -08001272 const DataContentDescription* data_desc = media_desc->as_data();
pthatcher@webrtc.org3341b402015-02-13 21:14:22 +00001273 if (IsDtlsSctp(media_desc->protocol())) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001274 fmt.append(" ");
jiayl@webrtc.org9c16c392014-05-01 18:30:30 +00001275
zstein4b2e0822017-02-17 19:48:38 -08001276 if (data_desc->use_sctpmap()) {
1277 for (std::vector<cricket::DataCodec>::const_iterator it =
1278 data_desc->codecs().begin();
1279 it != data_desc->codecs().end(); ++it) {
1280 if (cricket::CodecNamesEq(it->name,
1281 cricket::kGoogleSctpDataCodecName) &&
1282 it->GetParam(cricket::kCodecParamPort, &sctp_port)) {
1283 break;
1284 }
jiayl@webrtc.org9c16c392014-05-01 18:30:30 +00001285 }
jiayl@webrtc.org9c16c392014-05-01 18:30:30 +00001286
zstein4b2e0822017-02-17 19:48:38 -08001287 fmt.append(rtc::ToString<int>(sctp_port));
1288 } else {
1289 fmt.append(kDefaultSctpmapProtocol);
1290 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001291 } else {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001292 for (std::vector<cricket::DataCodec>::const_iterator it =
1293 data_desc->codecs().begin();
1294 it != data_desc->codecs().end(); ++it) {
1295 fmt.append(" ");
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001296 fmt.append(rtc::ToString<int>(it->id));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001297 }
1298 }
1299 }
1300 // The fmt must never be empty. If no codecs are found, set the fmt attribute
1301 // to 0.
1302 if (fmt.empty()) {
1303 fmt = " 0";
1304 }
1305
deadbeef25ed4352016-12-12 18:37:36 -08001306 // The port number in the m line will be updated later when associated with
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001307 // the candidates.
deadbeef25ed4352016-12-12 18:37:36 -08001308 //
1309 // A port value of 0 indicates that the m= section is rejected.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001310 // RFC 3264
1311 // To reject an offered stream, the port number in the corresponding stream in
1312 // the answer MUST be set to zero.
deadbeef25ed4352016-12-12 18:37:36 -08001313 //
1314 // However, the BUNDLE draft adds a new meaning to port zero, when used along
1315 // with a=bundle-only.
zhihuang38989e52017-03-21 11:04:53 -07001316 std::string port = kDummyPort;
1317 if (content_info->rejected || content_info->bundle_only) {
1318 port = kMediaPortRejected;
1319 } else if (!media_desc->connection_address().IsNil()) {
1320 port = rtc::ToString(media_desc->connection_address().port());
1321 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001322
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001323 rtc::SSLFingerprint* fp = (transport_info) ?
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001324 transport_info->description.identity_fingerprint.get() : NULL;
1325
wu@webrtc.org4c3e9912014-07-16 21:03:13 +00001326 // Add the m and c lines.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001327 InitLine(kLineTypeMedia, type, &os);
1328 os << " " << port << " " << media_desc->protocol() << fmt;
zhihuang38989e52017-03-21 11:04:53 -07001329 AddLine(os.str(), message);
1330
1331 InitLine(kLineTypeConnection, kConnectionNettype, &os);
1332 if (media_desc->connection_address().IsNil()) {
1333 os << " " << kConnectionIpv4Addrtype << " " << kDummyAddress;
1334 } else if (media_desc->connection_address().family() == AF_INET) {
1335 os << " " << kConnectionIpv4Addrtype << " "
1336 << media_desc->connection_address().ipaddr().ToString();
1337 } else {
1338 os << " " << kConnectionIpv6Addrtype << " "
1339 << media_desc->connection_address().ipaddr().ToString();
1340 }
1341 AddLine(os.str(), message);
wu@webrtc.org4c3e9912014-07-16 21:03:13 +00001342
1343 // RFC 4566
1344 // b=AS:<bandwidth>
Peter Thatcherc0c3a862015-06-24 15:31:25 -07001345 if (media_desc->bandwidth() >= 1000) {
wu@webrtc.org4c3e9912014-07-16 21:03:13 +00001346 InitLine(kLineTypeSessionBandwidth, kApplicationSpecificMaximum, &os);
1347 os << kSdpDelimiterColon << (media_desc->bandwidth() / 1000);
1348 AddLine(os.str(), message);
1349 }
1350
deadbeef25ed4352016-12-12 18:37:36 -08001351 // Add the a=bundle-only line.
1352 if (content_info->bundle_only) {
1353 InitAttrLine(kAttributeBundleOnly, &os);
1354 AddLine(os.str(), message);
1355 }
1356
wu@webrtc.org4c3e9912014-07-16 21:03:13 +00001357 // Add the a=rtcp line.
pthatcher@webrtc.org3341b402015-02-13 21:14:22 +00001358 if (IsRtp(media_desc->protocol())) {
wu@webrtc.org4c3e9912014-07-16 21:03:13 +00001359 std::string rtcp_line = GetRtcpLine(candidates);
1360 if (!rtcp_line.empty()) {
1361 AddLine(rtcp_line, message);
1362 }
1363 }
1364
honghaiza54a0802015-12-16 18:37:23 -08001365 // Build the a=candidate lines. We don't include ufrag and pwd in the
1366 // candidates in the SDP to avoid redundancy.
1367 BuildCandidate(candidates, false, message);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001368
1369 // Use the transport_info to build the media level ice-ufrag and ice-pwd.
1370 if (transport_info) {
1371 // RFC 5245
1372 // ice-pwd-att = "ice-pwd" ":" password
1373 // ice-ufrag-att = "ice-ufrag" ":" ufrag
1374 // ice-ufrag
deadbeef3f7219b2015-12-28 15:17:14 -08001375 if (!transport_info->description.ice_ufrag.empty()) {
1376 InitAttrLine(kAttributeIceUfrag, &os);
1377 os << kSdpDelimiterColon << transport_info->description.ice_ufrag;
1378 AddLine(os.str(), message);
1379 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001380 // ice-pwd
deadbeef3f7219b2015-12-28 15:17:14 -08001381 if (!transport_info->description.ice_pwd.empty()) {
1382 InitAttrLine(kAttributeIcePwd, &os);
1383 os << kSdpDelimiterColon << transport_info->description.ice_pwd;
1384 AddLine(os.str(), message);
1385 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001386
1387 // draft-petithuguenin-mmusic-ice-attributes-level-03
1388 BuildIceOptions(transport_info->description.transport_options, message);
1389
1390 // RFC 4572
1391 // fingerprint-attribute =
1392 // "fingerprint" ":" hash-func SP fingerprint
1393 if (fp) {
1394 // Insert the fingerprint attribute.
1395 InitAttrLine(kAttributeFingerprint, &os);
1396 os << kSdpDelimiterColon
1397 << fp->algorithm << kSdpDelimiterSpace
1398 << fp->GetRfc4572Fingerprint();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001399 AddLine(os.str(), message);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001400
1401 // Inserting setup attribute.
1402 if (transport_info->description.connection_role !=
1403 cricket::CONNECTIONROLE_NONE) {
1404 // Making sure we are not using "passive" mode.
1405 cricket::ConnectionRole role =
1406 transport_info->description.connection_role;
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00001407 std::string dtls_role_str;
nissec16fa5e2017-02-07 07:18:43 -08001408 const bool success =
1409 cricket::ConnectionRoleToString(role, &dtls_role_str);
1410 RTC_DCHECK(success);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001411 InitAttrLine(kAttributeSetup, &os);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001412 os << kSdpDelimiterColon << dtls_role_str;
1413 AddLine(os.str(), message);
1414 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001415 }
1416 }
1417
1418 // RFC 3388
1419 // mid-attribute = "a=mid:" identification-tag
1420 // identification-tag = token
1421 // Use the content name as the mid identification-tag.
1422 InitAttrLine(kAttributeMid, &os);
1423 os << kSdpDelimiterColon << content_info->name;
1424 AddLine(os.str(), message);
1425
pthatcher@webrtc.org3341b402015-02-13 21:14:22 +00001426 if (IsDtlsSctp(media_desc->protocol())) {
Steve Antonb1c1de12017-12-21 15:14:30 -08001427 const DataContentDescription* data_desc = media_desc->as_data();
zstein4b2e0822017-02-17 19:48:38 -08001428 bool use_sctpmap = data_desc->use_sctpmap();
1429 BuildSctpContentAttributes(message, sctp_port, use_sctpmap);
pthatcher@webrtc.org3341b402015-02-13 21:14:22 +00001430 } else if (IsRtp(media_desc->protocol())) {
Steve Antone831b8c2018-02-01 12:22:16 -08001431 BuildRtpContentAttributes(media_desc, media_type, msid_signaling, message);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001432 }
1433}
1434
zstein4b2e0822017-02-17 19:48:38 -08001435void BuildSctpContentAttributes(std::string* message,
1436 int sctp_port,
1437 bool use_sctpmap) {
wu@webrtc.org78187522013-10-07 23:32:02 +00001438 std::ostringstream os;
zstein4b2e0822017-02-17 19:48:38 -08001439 if (use_sctpmap) {
1440 // draft-ietf-mmusic-sctp-sdp-04
1441 // a=sctpmap:sctpmap-number protocol [streams]
1442 InitAttrLine(kAttributeSctpmap, &os);
1443 os << kSdpDelimiterColon << sctp_port << kSdpDelimiterSpace
1444 << kDefaultSctpmapProtocol << kSdpDelimiterSpace
1445 << cricket::kMaxSctpStreams;
1446 } else {
1447 // draft-ietf-mmusic-sctp-sdp-23
1448 // a=sctp-port:<port>
1449 InitAttrLine(kAttributeSctpPort, &os);
1450 os << kSdpDelimiterColon << sctp_port;
1451 // TODO(zstein): emit max-message-size here
1452 }
wu@webrtc.org78187522013-10-07 23:32:02 +00001453 AddLine(os.str(), message);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001454}
1455
deadbeef9d3584c2016-02-16 17:54:10 -08001456void BuildRtpContentAttributes(const MediaContentDescription* media_desc,
1457 const MediaType media_type,
Steve Antone831b8c2018-02-01 12:22:16 -08001458 int msid_signaling,
deadbeef9d3584c2016-02-16 17:54:10 -08001459 std::string* message) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001460 std::ostringstream os;
1461 // RFC 5285
1462 // a=extmap:<value>["/"<direction>] <URI> <extensionattributes>
1463 // The definitions MUST be either all session level or all media level. This
1464 // implementation uses all media level.
1465 for (size_t i = 0; i < media_desc->rtp_header_extensions().size(); ++i) {
jbauch5869f502017-06-29 12:31:36 -07001466 const RtpExtension& extension = media_desc->rtp_header_extensions()[i];
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001467 InitAttrLine(kAttributeExtmap, &os);
jbauch5869f502017-06-29 12:31:36 -07001468 os << kSdpDelimiterColon << extension.id;
1469 if (extension.encrypt) {
1470 os << kSdpDelimiterSpace << RtpExtension::kEncryptHeaderExtensionsUri;
1471 }
1472 os << kSdpDelimiterSpace << extension.uri;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001473 AddLine(os.str(), message);
1474 }
1475
1476 // RFC 3264
1477 // a=sendrecv || a=sendonly || a=sendrecv || a=inactive
deadbeefc80741f2015-10-22 13:14:45 -07001478 switch (media_desc->direction()) {
Steve Anton4e70a722017-11-28 14:57:10 -08001479 case RtpTransceiverDirection::kInactive:
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001480 InitAttrLine(kAttributeInactive, &os);
1481 break;
Steve Anton4e70a722017-11-28 14:57:10 -08001482 case RtpTransceiverDirection::kSendOnly:
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001483 InitAttrLine(kAttributeSendOnly, &os);
1484 break;
Steve Anton4e70a722017-11-28 14:57:10 -08001485 case RtpTransceiverDirection::kRecvOnly:
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001486 InitAttrLine(kAttributeRecvOnly, &os);
1487 break;
Steve Anton4e70a722017-11-28 14:57:10 -08001488 case RtpTransceiverDirection::kSendRecv:
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001489 default:
1490 InitAttrLine(kAttributeSendRecv, &os);
1491 break;
1492 }
1493 AddLine(os.str(), message);
1494
Seth Hampson5b4f0752018-04-02 16:31:36 -07001495 // Specified in https://datatracker.ietf.org/doc/draft-ietf-mmusic-msid/16/
1496 // a=msid:<msid-id> <msid-appdata>
1497 // The msid-id is a 1*64 token char representing the media stream id, and the
1498 // msid-appdata is a 1*64 token char representing the track id. There is a
1499 // line for every media stream, with a special msid-id value of "-"
1500 // representing no streams. The value of "msid-appdata" MUST be identical for
1501 // all lines.
Steve Antone831b8c2018-02-01 12:22:16 -08001502 if (msid_signaling & cricket::kMsidSignalingMediaSection) {
1503 const StreamParamsVec& streams = media_desc->streams();
1504 if (streams.size() == 1u) {
1505 const StreamParams& track = streams[0];
Seth Hampson5b4f0752018-04-02 16:31:36 -07001506 std::vector<std::string> stream_ids = track.stream_ids();
1507 if (stream_ids.empty()) {
1508 stream_ids.push_back(kNoStreamMsid);
1509 }
1510 for (const std::string& stream_id : stream_ids) {
1511 InitAttrLine(kAttributeMsid, &os);
1512 os << kSdpDelimiterColon << stream_id << kSdpDelimiterSpace << track.id;
1513 AddLine(os.str(), message);
1514 }
Steve Antone831b8c2018-02-01 12:22:16 -08001515 } else if (streams.size() > 1u) {
1516 RTC_LOG(LS_WARNING)
1517 << "Trying to serialize Unified Plan SDP with more than "
Jonas Olsson45cc8902018-02-13 10:37:07 +01001518 "one track in a media section. Omitting 'a=msid'.";
deadbeef9d3584c2016-02-16 17:54:10 -08001519 }
1520 }
1521
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001522 // RFC 5761
1523 // a=rtcp-mux
1524 if (media_desc->rtcp_mux()) {
1525 InitAttrLine(kAttributeRtcpMux, &os);
1526 AddLine(os.str(), message);
1527 }
1528
deadbeef13871492015-12-09 12:37:51 -08001529 // RFC 5506
1530 // a=rtcp-rsize
1531 if (media_desc->rtcp_reduced_size()) {
1532 InitAttrLine(kAttributeRtcpReducedSize, &os);
1533 AddLine(os.str(), message);
1534 }
1535
deadbeefd45aea82017-09-16 01:24:29 -07001536 if (media_desc->conference_mode()) {
1537 InitAttrLine(kAttributeXGoogleFlag, &os);
1538 os << kSdpDelimiterColon << kValueConference;
1539 AddLine(os.str(), message);
1540 }
1541
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001542 // RFC 4568
1543 // a=crypto:<tag> <crypto-suite> <key-params> [<session-params>]
1544 for (std::vector<CryptoParams>::const_iterator it =
1545 media_desc->cryptos().begin();
1546 it != media_desc->cryptos().end(); ++it) {
1547 InitAttrLine(kAttributeCrypto, &os);
1548 os << kSdpDelimiterColon << it->tag << " " << it->cipher_suite << " "
1549 << it->key_params;
1550 if (!it->session_params.empty()) {
1551 os << " " << it->session_params;
1552 }
1553 AddLine(os.str(), message);
1554 }
1555
1556 // RFC 4566
1557 // a=rtpmap:<payload type> <encoding name>/<clock rate>
1558 // [/<encodingparameters>]
1559 BuildRtpMap(media_desc, media_type, message);
1560
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001561 for (StreamParamsVec::const_iterator track = media_desc->streams().begin();
1562 track != media_desc->streams().end(); ++track) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001563 // Build the ssrc-group lines.
1564 for (size_t i = 0; i < track->ssrc_groups.size(); ++i) {
1565 // RFC 5576
1566 // a=ssrc-group:<semantics> <ssrc-id> ...
1567 if (track->ssrc_groups[i].ssrcs.empty()) {
1568 continue;
1569 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001570 InitAttrLine(kAttributeSsrcGroup, &os);
1571 os << kSdpDelimiterColon << track->ssrc_groups[i].semantics;
Peter Boström0c4e06b2015-10-07 12:23:21 +02001572 std::vector<uint32_t>::const_iterator ssrc =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001573 track->ssrc_groups[i].ssrcs.begin();
1574 for (; ssrc != track->ssrc_groups[i].ssrcs.end(); ++ssrc) {
Peter Boström0c4e06b2015-10-07 12:23:21 +02001575 os << kSdpDelimiterSpace << rtc::ToString<uint32_t>(*ssrc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001576 }
1577 AddLine(os.str(), message);
1578 }
1579 // Build the ssrc lines for each ssrc.
1580 for (size_t i = 0; i < track->ssrcs.size(); ++i) {
Peter Boström0c4e06b2015-10-07 12:23:21 +02001581 uint32_t ssrc = track->ssrcs[i];
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001582 // RFC 5576
1583 // a=ssrc:<ssrc-id> cname:<value>
1584 AddSsrcLine(ssrc, kSsrcAttributeCname,
1585 track->cname, message);
1586
Steve Antone831b8c2018-02-01 12:22:16 -08001587 if (msid_signaling & cricket::kMsidSignalingSsrcAttribute) {
1588 // draft-alvestrand-mmusic-msid-00
1589 // a=ssrc:<ssrc-id> msid:identifier [appdata]
1590 // The appdata consists of the "id" attribute of a MediaStreamTrack,
1591 // which corresponds to the "id" attribute of StreamParams.
Seth Hampson5b4f0752018-04-02 16:31:36 -07001592 // Since a=ssrc msid signaling is used in Plan B SDP semantics, and
1593 // multiple stream ids are not supported for Plan B, we are only adding
1594 // a line for the first media stream id here.
Seth Hampson845e8782018-03-02 11:34:10 -08001595 const std::string& stream_id = track->first_stream_id();
Steve Antone831b8c2018-02-01 12:22:16 -08001596 InitAttrLine(kAttributeSsrc, &os);
1597 os << kSdpDelimiterColon << ssrc << kSdpDelimiterSpace
1598 << kSsrcAttributeMsid << kSdpDelimiterColon << stream_id
1599 << kSdpDelimiterSpace << track->id;
1600 AddLine(os.str(), message);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001601
Steve Antone831b8c2018-02-01 12:22:16 -08001602 // TODO(ronghuawu): Remove below code which is for backward
1603 // compatibility.
1604 // draft-alvestrand-rtcweb-mid-01
1605 // a=ssrc:<ssrc-id> mslabel:<value>
1606 // The label isn't yet defined.
1607 // a=ssrc:<ssrc-id> label:<value>
Seth Hampson5b4f0752018-04-02 16:31:36 -07001608 AddSsrcLine(ssrc, kSsrcAttributeMslabel, stream_id, message);
Steve Antone831b8c2018-02-01 12:22:16 -08001609 AddSsrcLine(ssrc, kSSrcAttributeLabel, track->id, message);
1610 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001611 }
1612 }
1613}
1614
1615void WriteFmtpHeader(int payload_type, std::ostringstream* os) {
1616 // fmtp header: a=fmtp:|payload_type| <parameters>
1617 // Add a=fmtp
1618 InitAttrLine(kAttributeFmtp, os);
1619 // Add :|payload_type|
1620 *os << kSdpDelimiterColon << payload_type;
1621}
1622
1623void WriteRtcpFbHeader(int payload_type, std::ostringstream* os) {
1624 // rtcp-fb header: a=rtcp-fb:|payload_type|
1625 // <parameters>/<ccm <ccm_parameters>>
1626 // Add a=rtcp-fb
1627 InitAttrLine(kAttributeRtcpFb, os);
1628 // Add :
1629 *os << kSdpDelimiterColon;
1630 if (payload_type == kWildcardPayloadType) {
1631 *os << "*";
1632 } else {
1633 *os << payload_type;
1634 }
1635}
1636
1637void WriteFmtpParameter(const std::string& parameter_name,
1638 const std::string& parameter_value,
1639 std::ostringstream* os) {
1640 // fmtp parameters: |parameter_name|=|parameter_value|
1641 *os << parameter_name << kSdpDelimiterEqual << parameter_value;
1642}
1643
1644void WriteFmtpParameters(const cricket::CodecParameterMap& parameters,
1645 std::ostringstream* os) {
1646 for (cricket::CodecParameterMap::const_iterator fmtp = parameters.begin();
1647 fmtp != parameters.end(); ++fmtp) {
hta62a216e2016-04-15 11:02:14 -07001648 // Parameters are a semicolon-separated list, no spaces.
1649 // The list is separated from the header by a space.
1650 if (fmtp == parameters.begin()) {
1651 *os << kSdpDelimiterSpace;
1652 } else {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001653 *os << kSdpDelimiterSemicolon;
1654 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001655 WriteFmtpParameter(fmtp->first, fmtp->second, os);
1656 }
1657}
1658
1659bool IsFmtpParam(const std::string& name) {
ossuaa4b0772017-01-30 07:41:18 -08001660 // RFC 4855, section 3 specifies the mapping of media format parameters to SDP
1661 // parameters. Only ptime, maxptime, channels and rate are placed outside of
1662 // the fmtp line. In WebRTC, channels and rate are already handled separately
1663 // and thus not included in the CodecParameterMap.
1664 return name != kCodecParamPTime && name != kCodecParamMaxPTime;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001665}
1666
1667// Retreives fmtp parameters from |params|, which may contain other parameters
1668// as well, and puts them in |fmtp_parameters|.
1669void GetFmtpParams(const cricket::CodecParameterMap& params,
1670 cricket::CodecParameterMap* fmtp_parameters) {
1671 for (cricket::CodecParameterMap::const_iterator iter = params.begin();
1672 iter != params.end(); ++iter) {
1673 if (IsFmtpParam(iter->first)) {
1674 (*fmtp_parameters)[iter->first] = iter->second;
1675 }
1676 }
1677}
1678
1679template <class T>
1680void AddFmtpLine(const T& codec, std::string* message) {
1681 cricket::CodecParameterMap fmtp_parameters;
1682 GetFmtpParams(codec.params, &fmtp_parameters);
1683 if (fmtp_parameters.empty()) {
1684 // No need to add an fmtp if it will have no (optional) parameters.
1685 return;
1686 }
1687 std::ostringstream os;
1688 WriteFmtpHeader(codec.id, &os);
1689 WriteFmtpParameters(fmtp_parameters, &os);
1690 AddLine(os.str(), message);
1691 return;
1692}
1693
1694template <class T>
1695void AddRtcpFbLines(const T& codec, std::string* message) {
1696 for (std::vector<cricket::FeedbackParam>::const_iterator iter =
1697 codec.feedback_params.params().begin();
1698 iter != codec.feedback_params.params().end(); ++iter) {
1699 std::ostringstream os;
1700 WriteRtcpFbHeader(codec.id, &os);
1701 os << " " << iter->id();
1702 if (!iter->param().empty()) {
1703 os << " " << iter->param();
1704 }
1705 AddLine(os.str(), message);
1706 }
1707}
1708
jiayl@webrtc.orgddb85ab2014-09-05 16:31:56 +00001709bool AddSctpDataCodec(DataContentDescription* media_desc,
1710 int sctp_port) {
solenberg9fa49752016-10-08 13:02:44 -07001711 for (const auto& codec : media_desc->codecs()) {
1712 if (cricket::CodecNamesEq(codec.name, cricket::kGoogleSctpDataCodecName)) {
1713 return ParseFailed("",
1714 "Can't have multiple sctp port attributes.",
1715 NULL);
1716 }
jiayl@webrtc.orgddb85ab2014-09-05 16:31:56 +00001717 }
1718 // Add the SCTP Port number as a pseudo-codec "port" parameter
solenberg9fa49752016-10-08 13:02:44 -07001719 cricket::DataCodec codec_port(cricket::kGoogleSctpDataCodecPlType,
deadbeef67cf2c12016-04-13 10:07:16 -07001720 cricket::kGoogleSctpDataCodecName);
jiayl@webrtc.orgddb85ab2014-09-05 16:31:56 +00001721 codec_port.SetParam(cricket::kCodecParamPort, sctp_port);
Mirko Bonadei675513b2017-11-09 11:09:25 +01001722 RTC_LOG(INFO) << "AddSctpDataCodec: Got SCTP Port Number " << sctp_port;
jiayl@webrtc.orgddb85ab2014-09-05 16:31:56 +00001723 media_desc->AddCodec(codec_port);
1724 return true;
1725}
1726
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001727bool GetMinValue(const std::vector<int>& values, int* value) {
1728 if (values.empty()) {
1729 return false;
1730 }
1731 std::vector<int>::const_iterator found =
1732 std::min_element(values.begin(), values.end());
1733 *value = *found;
1734 return true;
1735}
1736
1737bool GetParameter(const std::string& name,
1738 const cricket::CodecParameterMap& params, int* value) {
1739 std::map<std::string, std::string>::const_iterator found =
1740 params.find(name);
1741 if (found == params.end()) {
1742 return false;
1743 }
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001744 if (!rtc::FromString(found->second, value)) {
wu@webrtc.org5e760e72014-04-02 23:19:09 +00001745 return false;
1746 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001747 return true;
1748}
1749
1750void BuildRtpMap(const MediaContentDescription* media_desc,
1751 const MediaType media_type,
1752 std::string* message) {
nisseede5da42017-01-12 05:15:36 -08001753 RTC_DCHECK(message != NULL);
1754 RTC_DCHECK(media_desc != NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001755 std::ostringstream os;
1756 if (media_type == cricket::MEDIA_TYPE_VIDEO) {
Steve Antonb1c1de12017-12-21 15:14:30 -08001757 const VideoContentDescription* video_desc = media_desc->as_video();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001758 for (std::vector<cricket::VideoCodec>::const_iterator it =
1759 video_desc->codecs().begin();
1760 it != video_desc->codecs().end(); ++it) {
1761 // RFC 4566
1762 // a=rtpmap:<payload type> <encoding name>/<clock rate>
1763 // [/<encodingparameters>]
1764 if (it->id != kWildcardPayloadType) {
1765 InitAttrLine(kAttributeRtpmap, &os);
deadbeefe814a0d2017-02-25 18:15:09 -08001766 os << kSdpDelimiterColon << it->id << " " << it->name << "/"
1767 << cricket::kVideoCodecClockrate;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001768 AddLine(os.str(), message);
1769 }
1770 AddRtcpFbLines(*it, message);
1771 AddFmtpLine(*it, message);
1772 }
1773 } else if (media_type == cricket::MEDIA_TYPE_AUDIO) {
Steve Antonb1c1de12017-12-21 15:14:30 -08001774 const AudioContentDescription* audio_desc = media_desc->as_audio();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001775 std::vector<int> ptimes;
1776 std::vector<int> maxptimes;
1777 int max_minptime = 0;
1778 for (std::vector<cricket::AudioCodec>::const_iterator it =
1779 audio_desc->codecs().begin();
1780 it != audio_desc->codecs().end(); ++it) {
nisseede5da42017-01-12 05:15:36 -08001781 RTC_DCHECK(!it->name.empty());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001782 // RFC 4566
1783 // a=rtpmap:<payload type> <encoding name>/<clock rate>
1784 // [/<encodingparameters>]
1785 InitAttrLine(kAttributeRtpmap, &os);
1786 os << kSdpDelimiterColon << it->id << " ";
1787 os << it->name << "/" << it->clockrate;
1788 if (it->channels != 1) {
1789 os << "/" << it->channels;
1790 }
1791 AddLine(os.str(), message);
1792 AddRtcpFbLines(*it, message);
1793 AddFmtpLine(*it, message);
1794 int minptime = 0;
1795 if (GetParameter(kCodecParamMinPTime, it->params, &minptime)) {
1796 max_minptime = std::max(minptime, max_minptime);
1797 }
1798 int ptime;
1799 if (GetParameter(kCodecParamPTime, it->params, &ptime)) {
1800 ptimes.push_back(ptime);
1801 }
1802 int maxptime;
1803 if (GetParameter(kCodecParamMaxPTime, it->params, &maxptime)) {
1804 maxptimes.push_back(maxptime);
1805 }
1806 }
1807 // Populate the maxptime attribute with the smallest maxptime of all codecs
1808 // under the same m-line.
1809 int min_maxptime = INT_MAX;
1810 if (GetMinValue(maxptimes, &min_maxptime)) {
1811 AddAttributeLine(kCodecParamMaxPTime, min_maxptime, message);
1812 }
nisseede5da42017-01-12 05:15:36 -08001813 RTC_DCHECK(min_maxptime > max_minptime);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001814 // Populate the ptime attribute with the smallest ptime or the largest
1815 // minptime, whichever is the largest, for all codecs under the same m-line.
1816 int ptime = INT_MAX;
1817 if (GetMinValue(ptimes, &ptime)) {
1818 ptime = std::min(ptime, min_maxptime);
1819 ptime = std::max(ptime, max_minptime);
1820 AddAttributeLine(kCodecParamPTime, ptime, message);
1821 }
1822 } else if (media_type == cricket::MEDIA_TYPE_DATA) {
Steve Antonb1c1de12017-12-21 15:14:30 -08001823 const DataContentDescription* data_desc = media_desc->as_data();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001824 for (std::vector<cricket::DataCodec>::const_iterator it =
1825 data_desc->codecs().begin();
1826 it != data_desc->codecs().end(); ++it) {
1827 // RFC 4566
1828 // a=rtpmap:<payload type> <encoding name>/<clock rate>
1829 // [/<encodingparameters>]
1830 InitAttrLine(kAttributeRtpmap, &os);
1831 os << kSdpDelimiterColon << it->id << " "
1832 << it->name << "/" << it->clockrate;
1833 AddLine(os.str(), message);
1834 }
1835 }
1836}
1837
1838void BuildCandidate(const std::vector<Candidate>& candidates,
honghaiza54a0802015-12-16 18:37:23 -08001839 bool include_ufrag,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001840 std::string* message) {
1841 std::ostringstream os;
1842
1843 for (std::vector<Candidate>::const_iterator it = candidates.begin();
1844 it != candidates.end(); ++it) {
1845 // RFC 5245
1846 // a=candidate:<foundation> <component-id> <transport> <priority>
1847 // <connection-address> <port> typ <candidate-types>
1848 // [raddr <connection-address>] [rport <port>]
1849 // *(SP extension-att-name SP extension-att-value)
1850 std::string type;
1851 // Map the cricket candidate type to "host" / "srflx" / "prflx" / "relay"
1852 if (it->type() == cricket::LOCAL_PORT_TYPE) {
1853 type = kCandidateHost;
1854 } else if (it->type() == cricket::STUN_PORT_TYPE) {
1855 type = kCandidateSrflx;
1856 } else if (it->type() == cricket::RELAY_PORT_TYPE) {
1857 type = kCandidateRelay;
Honghai Zhang7fb69db2016-03-14 11:59:18 -07001858 } else if (it->type() == cricket::PRFLX_PORT_TYPE) {
1859 type = kCandidatePrflx;
1860 // Peer reflexive candidate may be signaled for being removed.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001861 } else {
nissec80e7412017-01-11 05:56:46 -08001862 RTC_NOTREACHED();
Peter Thatcher019087f2015-04-28 09:06:26 -07001863 // Never write out candidates if we don't know the type.
1864 continue;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001865 }
1866
1867 InitAttrLine(kAttributeCandidate, &os);
1868 os << kSdpDelimiterColon
mallinath@webrtc.org2d60c5e2014-08-08 22:29:20 +00001869 << it->foundation() << " "
1870 << it->component() << " "
1871 << it->protocol() << " "
1872 << it->priority() << " "
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001873 << it->address().ipaddr().ToString() << " "
1874 << it->address().PortAsString() << " "
mallinath@webrtc.org2d60c5e2014-08-08 22:29:20 +00001875 << kAttributeCandidateTyp << " "
1876 << type << " ";
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001877
1878 // Related address
1879 if (!it->related_address().IsNil()) {
1880 os << kAttributeCandidateRaddr << " "
1881 << it->related_address().ipaddr().ToString() << " "
1882 << kAttributeCandidateRport << " "
1883 << it->related_address().PortAsString() << " ";
1884 }
1885
mallinath@webrtc.org2d60c5e2014-08-08 22:29:20 +00001886 if (it->protocol() == cricket::TCP_PROTOCOL_NAME) {
mallinath@webrtc.orge999bd02014-08-13 06:05:55 +00001887 os << kTcpCandidateType << " " << it->tcptype() << " ";
mallinath@webrtc.org2d60c5e2014-08-08 22:29:20 +00001888 }
1889
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001890 // Extensions
1891 os << kAttributeCandidateGeneration << " " << it->generation();
honghaiza54a0802015-12-16 18:37:23 -08001892 if (include_ufrag && !it->username().empty()) {
1893 os << " " << kAttributeCandidateUfrag << " " << it->username();
1894 }
honghaiza0c44ea2016-03-23 16:07:48 -07001895 if (it->network_id() > 0) {
1896 os << " " << kAttributeCandidateNetworkId << " " << it->network_id();
1897 }
honghaize1a0c942016-02-16 14:54:56 -08001898 if (it->network_cost() > 0) {
1899 os << " " << kAttributeCandidateNetworkCost << " " << it->network_cost();
1900 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001901
1902 AddLine(os.str(), message);
1903 }
1904}
1905
1906void BuildIceOptions(const std::vector<std::string>& transport_options,
1907 std::string* message) {
1908 if (!transport_options.empty()) {
1909 std::ostringstream os;
1910 InitAttrLine(kAttributeIceOption, &os);
1911 os << kSdpDelimiterColon << transport_options[0];
1912 for (size_t i = 1; i < transport_options.size(); ++i) {
1913 os << kSdpDelimiterSpace << transport_options[i];
1914 }
1915 AddLine(os.str(), message);
1916 }
1917}
1918
pthatcher@webrtc.org3341b402015-02-13 21:14:22 +00001919bool IsRtp(const std::string& protocol) {
1920 return protocol.empty() ||
1921 (protocol.find(cricket::kMediaProtocolRtpPrefix) != std::string::npos);
1922}
1923
1924bool IsDtlsSctp(const std::string& protocol) {
1925 // This intentionally excludes "SCTP" and "SCTP/DTLS".
lally@webrtc.orga7470932015-02-24 20:19:21 +00001926 return protocol.find(cricket::kMediaProtocolDtlsSctp) != std::string::npos;
pthatcher@webrtc.org3341b402015-02-13 21:14:22 +00001927}
1928
zhihuang38989e52017-03-21 11:04:53 -07001929bool ParseConnectionData(const std::string& line,
1930 rtc::SocketAddress* addr,
1931 SdpParseError* error) {
1932 // Parse the line from left to right.
1933 std::string token;
1934 std::string rightpart;
1935 // RFC 4566
1936 // c=<nettype> <addrtype> <connection-address>
1937 // Skip the "c="
1938 if (!rtc::tokenize_first(line, kSdpDelimiterEqual, &token, &rightpart)) {
1939 return ParseFailed(line, "Failed to parse the network type.", error);
1940 }
1941
1942 // Extract and verify the <nettype>
1943 if (!rtc::tokenize_first(rightpart, kSdpDelimiterSpace, &token, &rightpart) ||
1944 token != kConnectionNettype) {
1945 return ParseFailed(line,
1946 "Failed to parse the connection data. The network type "
1947 "is not currently supported.",
1948 error);
1949 }
1950
1951 // Extract the "<addrtype>" and "<connection-address>".
1952 if (!rtc::tokenize_first(rightpart, kSdpDelimiterSpace, &token, &rightpart)) {
1953 return ParseFailed(line, "Failed to parse the address type.", error);
1954 }
1955
1956 // The rightpart part should be the IP address without the slash which is used
1957 // for multicast.
1958 if (rightpart.find('/') != std::string::npos) {
1959 return ParseFailed(line,
1960 "Failed to parse the connection data. Multicast is not "
1961 "currently supported.",
1962 error);
1963 }
1964 addr->SetIP(rightpart);
1965
1966 // Verify that the addrtype matches the type of the parsed address.
1967 if ((addr->family() == AF_INET && token != "IP4") ||
1968 (addr->family() == AF_INET6 && token != "IP6")) {
1969 addr->Clear();
1970 return ParseFailed(
1971 line,
1972 "Failed to parse the connection data. The address type is mismatching.",
1973 error);
1974 }
1975 return true;
1976}
1977
1978bool ParseSessionDescription(const std::string& message,
1979 size_t* pos,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001980 std::string* session_id,
1981 std::string* session_version,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001982 TransportDescription* session_td,
1983 RtpHeaderExtensions* session_extmaps,
zhihuang38989e52017-03-21 11:04:53 -07001984 rtc::SocketAddress* connection_addr,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001985 cricket::SessionDescription* desc,
1986 SdpParseError* error) {
1987 std::string line;
1988
deadbeefc80741f2015-10-22 13:14:45 -07001989 desc->set_msid_supported(false);
1990
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001991 // RFC 4566
1992 // v= (protocol version)
1993 if (!GetLineWithType(message, pos, &line, kLineTypeVersion)) {
1994 return ParseFailedExpectLine(message, *pos, kLineTypeVersion,
1995 std::string(), error);
1996 }
1997 // RFC 4566
1998 // o=<username> <sess-id> <sess-version> <nettype> <addrtype>
1999 // <unicast-address>
2000 if (!GetLineWithType(message, pos, &line, kLineTypeOrigin)) {
2001 return ParseFailedExpectLine(message, *pos, kLineTypeOrigin,
2002 std::string(), error);
2003 }
2004 std::vector<std::string> fields;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002005 rtc::split(line.substr(kLinePrefixLength),
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002006 kSdpDelimiterSpace, &fields);
2007 const size_t expected_fields = 6;
2008 if (fields.size() != expected_fields) {
2009 return ParseFailedExpectFieldNum(line, expected_fields, error);
2010 }
2011 *session_id = fields[1];
2012 *session_version = fields[2];
2013
2014 // RFC 4566
2015 // s= (session name)
2016 if (!GetLineWithType(message, pos, &line, kLineTypeSessionName)) {
2017 return ParseFailedExpectLine(message, *pos, kLineTypeSessionName,
2018 std::string(), error);
2019 }
2020
2021 // Optional lines
2022 // Those are the optional lines, so shouldn't return false if not present.
2023 // RFC 4566
2024 // i=* (session information)
2025 GetLineWithType(message, pos, &line, kLineTypeSessionInfo);
2026
2027 // RFC 4566
2028 // u=* (URI of description)
2029 GetLineWithType(message, pos, &line, kLineTypeSessionUri);
2030
2031 // RFC 4566
2032 // e=* (email address)
2033 GetLineWithType(message, pos, &line, kLineTypeSessionEmail);
2034
2035 // RFC 4566
2036 // p=* (phone number)
2037 GetLineWithType(message, pos, &line, kLineTypeSessionPhone);
2038
2039 // RFC 4566
2040 // c=* (connection information -- not required if included in
2041 // all media)
zhihuang38989e52017-03-21 11:04:53 -07002042 if (GetLineWithType(message, pos, &line, kLineTypeConnection)) {
2043 if (!ParseConnectionData(line, connection_addr, error)) {
2044 return false;
2045 }
2046 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002047
2048 // RFC 4566
2049 // b=* (zero or more bandwidth information lines)
2050 while (GetLineWithType(message, pos, &line, kLineTypeSessionBandwidth)) {
2051 // By pass zero or more b lines.
2052 }
2053
2054 // RFC 4566
2055 // One or more time descriptions ("t=" and "r=" lines; see below)
2056 // t= (time the session is active)
2057 // r=* (zero or more repeat times)
2058 // Ensure there's at least one time description
2059 if (!GetLineWithType(message, pos, &line, kLineTypeTiming)) {
2060 return ParseFailedExpectLine(message, *pos, kLineTypeTiming, std::string(),
2061 error);
2062 }
2063
2064 while (GetLineWithType(message, pos, &line, kLineTypeRepeatTimes)) {
2065 // By pass zero or more r lines.
2066 }
2067
2068 // Go through the rest of the time descriptions
2069 while (GetLineWithType(message, pos, &line, kLineTypeTiming)) {
2070 while (GetLineWithType(message, pos, &line, kLineTypeRepeatTimes)) {
2071 // By pass zero or more r lines.
2072 }
2073 }
2074
2075 // RFC 4566
2076 // z=* (time zone adjustments)
2077 GetLineWithType(message, pos, &line, kLineTypeTimeZone);
2078
2079 // RFC 4566
2080 // k=* (encryption key)
2081 GetLineWithType(message, pos, &line, kLineTypeEncryptionKey);
2082
2083 // RFC 4566
2084 // a=* (zero or more session attribute lines)
2085 while (GetLineWithType(message, pos, &line, kLineTypeAttributes)) {
2086 if (HasAttribute(line, kAttributeGroup)) {
2087 if (!ParseGroupAttribute(line, desc, error)) {
2088 return false;
2089 }
2090 } else if (HasAttribute(line, kAttributeIceUfrag)) {
2091 if (!GetValue(line, kAttributeIceUfrag,
2092 &(session_td->ice_ufrag), error)) {
2093 return false;
2094 }
2095 } else if (HasAttribute(line, kAttributeIcePwd)) {
2096 if (!GetValue(line, kAttributeIcePwd, &(session_td->ice_pwd), error)) {
2097 return false;
2098 }
2099 } else if (HasAttribute(line, kAttributeIceLite)) {
2100 session_td->ice_mode = cricket::ICEMODE_LITE;
2101 } else if (HasAttribute(line, kAttributeIceOption)) {
2102 if (!ParseIceOptions(line, &(session_td->transport_options), error)) {
2103 return false;
2104 }
2105 } else if (HasAttribute(line, kAttributeFingerprint)) {
2106 if (session_td->identity_fingerprint.get()) {
2107 return ParseFailed(
2108 line,
2109 "Can't have multiple fingerprint attributes at the same level.",
2110 error);
2111 }
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002112 rtc::SSLFingerprint* fingerprint = NULL;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002113 if (!ParseFingerprintAttribute(line, &fingerprint, error)) {
2114 return false;
2115 }
2116 session_td->identity_fingerprint.reset(fingerprint);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00002117 } else if (HasAttribute(line, kAttributeSetup)) {
2118 if (!ParseDtlsSetup(line, &(session_td->connection_role), error)) {
2119 return false;
2120 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002121 } else if (HasAttribute(line, kAttributeMsidSemantics)) {
2122 std::string semantics;
2123 if (!GetValue(line, kAttributeMsidSemantics, &semantics, error)) {
2124 return false;
2125 }
deadbeefc80741f2015-10-22 13:14:45 -07002126 desc->set_msid_supported(
2127 CaseInsensitiveFind(semantics, kMediaStreamSemantic));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002128 } else if (HasAttribute(line, kAttributeExtmap)) {
isheriff6f8d6862016-05-26 11:24:55 -07002129 RtpExtension extmap;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002130 if (!ParseExtmap(line, &extmap, error)) {
2131 return false;
2132 }
2133 session_extmaps->push_back(extmap);
2134 }
2135 }
2136
2137 return true;
2138}
2139
2140bool ParseGroupAttribute(const std::string& line,
2141 cricket::SessionDescription* desc,
2142 SdpParseError* error) {
nisseede5da42017-01-12 05:15:36 -08002143 RTC_DCHECK(desc != NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002144
2145 // RFC 5888 and draft-holmberg-mmusic-sdp-bundle-negotiation-00
2146 // a=group:BUNDLE video voice
2147 std::vector<std::string> fields;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002148 rtc::split(line.substr(kLinePrefixLength),
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002149 kSdpDelimiterSpace, &fields);
2150 std::string semantics;
2151 if (!GetValue(fields[0], kAttributeGroup, &semantics, error)) {
2152 return false;
2153 }
2154 cricket::ContentGroup group(semantics);
2155 for (size_t i = 1; i < fields.size(); ++i) {
2156 group.AddContentName(fields[i]);
2157 }
2158 desc->AddGroup(group);
2159 return true;
2160}
2161
2162static bool ParseFingerprintAttribute(const std::string& line,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002163 rtc::SSLFingerprint** fingerprint,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002164 SdpParseError* error) {
2165 if (!IsLineType(line, kLineTypeAttributes) ||
2166 !HasAttribute(line, kAttributeFingerprint)) {
2167 return ParseFailedExpectLine(line, 0, kLineTypeAttributes,
2168 kAttributeFingerprint, error);
2169 }
2170
2171 std::vector<std::string> fields;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002172 rtc::split(line.substr(kLinePrefixLength),
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002173 kSdpDelimiterSpace, &fields);
2174 const size_t expected_fields = 2;
2175 if (fields.size() != expected_fields) {
2176 return ParseFailedExpectFieldNum(line, expected_fields, error);
2177 }
2178
2179 // The first field here is "fingerprint:<hash>.
2180 std::string algorithm;
2181 if (!GetValue(fields[0], kAttributeFingerprint, &algorithm, error)) {
2182 return false;
2183 }
2184
2185 // Downcase the algorithm. Note that we don't need to downcase the
2186 // fingerprint because hex_decode can handle upper-case.
2187 std::transform(algorithm.begin(), algorithm.end(), algorithm.begin(),
2188 ::tolower);
2189
2190 // The second field is the digest value. De-hexify it.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002191 *fingerprint = rtc::SSLFingerprint::CreateFromRfc4572(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002192 algorithm, fields[1]);
2193 if (!*fingerprint) {
2194 return ParseFailed(line,
2195 "Failed to create fingerprint from the digest.",
2196 error);
2197 }
2198
2199 return true;
2200}
2201
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00002202static bool ParseDtlsSetup(const std::string& line,
2203 cricket::ConnectionRole* role,
2204 SdpParseError* error) {
2205 // setup-attr = "a=setup:" role
2206 // role = "active" / "passive" / "actpass" / "holdconn"
2207 std::vector<std::string> fields;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002208 rtc::split(line.substr(kLinePrefixLength), kSdpDelimiterColon, &fields);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00002209 const size_t expected_fields = 2;
2210 if (fields.size() != expected_fields) {
2211 return ParseFailedExpectFieldNum(line, expected_fields, error);
2212 }
2213 std::string role_str = fields[1];
2214 if (!cricket::StringToConnectionRole(role_str, role)) {
2215 return ParseFailed(line, "Invalid attribute value.", error);
2216 }
2217 return true;
2218}
2219
deadbeef9d3584c2016-02-16 17:54:10 -08002220static bool ParseMsidAttribute(const std::string& line,
Seth Hampson5b4f0752018-04-02 16:31:36 -07002221 std::vector<std::string>* stream_ids,
deadbeef9d3584c2016-02-16 17:54:10 -08002222 std::string* track_id,
2223 SdpParseError* error) {
Seth Hampson5b4f0752018-04-02 16:31:36 -07002224 // https://datatracker.ietf.org/doc/draft-ietf-mmusic-msid/16/
deadbeef9d3584c2016-02-16 17:54:10 -08002225 // a=msid:<stream id> <track id>
2226 // msid-value = msid-id [ SP msid-appdata ]
2227 // msid-id = 1*64token-char ; see RFC 4566
2228 // msid-appdata = 1*64token-char ; see RFC 4566
2229 std::string field1;
Seth Hampson5b4f0752018-04-02 16:31:36 -07002230 std::string new_stream_id;
2231 std::string new_track_id;
deadbeef9d3584c2016-02-16 17:54:10 -08002232 if (!rtc::tokenize_first(line.substr(kLinePrefixLength), kSdpDelimiterSpace,
Seth Hampson5b4f0752018-04-02 16:31:36 -07002233 &field1, &new_track_id)) {
deadbeef9d3584c2016-02-16 17:54:10 -08002234 const size_t expected_fields = 2;
2235 return ParseFailedExpectFieldNum(line, expected_fields, error);
2236 }
2237
Seth Hampson5b4f0752018-04-02 16:31:36 -07002238 if (new_track_id.empty()) {
deadbeefa4549d62017-02-10 17:26:22 -08002239 return ParseFailed(line, "Missing track ID in msid attribute.", error);
2240 }
Seth Hampson5b4f0752018-04-02 16:31:36 -07002241 // All track ids should be the same within an m section in a Unified Plan SDP.
2242 if (!track_id->empty() && new_track_id.compare(*track_id) != 0) {
2243 return ParseFailed(
2244 line, "Two different track IDs in msid attribute in one m= section",
2245 error);
2246 }
2247 *track_id = new_track_id;
deadbeefa4549d62017-02-10 17:26:22 -08002248
deadbeef9d3584c2016-02-16 17:54:10 -08002249 // msid:<msid-id>
Seth Hampson5b4f0752018-04-02 16:31:36 -07002250 if (!GetValue(field1, kAttributeMsid, &new_stream_id, error)) {
deadbeef9d3584c2016-02-16 17:54:10 -08002251 return false;
2252 }
Seth Hampson5b4f0752018-04-02 16:31:36 -07002253 if (new_stream_id.empty()) {
deadbeefa4549d62017-02-10 17:26:22 -08002254 return ParseFailed(line, "Missing stream ID in msid attribute.", error);
2255 }
Seth Hampson5b4f0752018-04-02 16:31:36 -07002256 // The special value "-" indicates "no MediaStream".
2257 if (new_stream_id.compare(kNoStreamMsid) != 0) {
2258 stream_ids->push_back(new_stream_id);
2259 }
deadbeef9d3584c2016-02-16 17:54:10 -08002260 return true;
2261}
2262
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002263// RFC 3551
2264// PT encoding media type clock rate channels
2265// name (Hz)
2266// 0 PCMU A 8,000 1
2267// 1 reserved A
2268// 2 reserved A
2269// 3 GSM A 8,000 1
2270// 4 G723 A 8,000 1
2271// 5 DVI4 A 8,000 1
2272// 6 DVI4 A 16,000 1
2273// 7 LPC A 8,000 1
2274// 8 PCMA A 8,000 1
2275// 9 G722 A 8,000 1
2276// 10 L16 A 44,100 2
2277// 11 L16 A 44,100 1
2278// 12 QCELP A 8,000 1
2279// 13 CN A 8,000 1
2280// 14 MPA A 90,000 (see text)
2281// 15 G728 A 8,000 1
2282// 16 DVI4 A 11,025 1
2283// 17 DVI4 A 22,050 1
2284// 18 G729 A 8,000 1
2285struct StaticPayloadAudioCodec {
2286 const char* name;
2287 int clockrate;
Peter Kasting69558702016-01-12 16:26:35 -08002288 size_t channels;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002289};
2290static const StaticPayloadAudioCodec kStaticPayloadAudioCodecs[] = {
2291 { "PCMU", 8000, 1 },
2292 { "reserved", 0, 0 },
2293 { "reserved", 0, 0 },
2294 { "GSM", 8000, 1 },
2295 { "G723", 8000, 1 },
2296 { "DVI4", 8000, 1 },
2297 { "DVI4", 16000, 1 },
2298 { "LPC", 8000, 1 },
2299 { "PCMA", 8000, 1 },
2300 { "G722", 8000, 1 },
2301 { "L16", 44100, 2 },
2302 { "L16", 44100, 1 },
2303 { "QCELP", 8000, 1 },
2304 { "CN", 8000, 1 },
2305 { "MPA", 90000, 1 },
2306 { "G728", 8000, 1 },
2307 { "DVI4", 11025, 1 },
2308 { "DVI4", 22050, 1 },
2309 { "G729", 8000, 1 },
2310};
2311
2312void MaybeCreateStaticPayloadAudioCodecs(
2313 const std::vector<int>& fmts, AudioContentDescription* media_desc) {
2314 if (!media_desc) {
2315 return;
2316 }
deadbeef67cf2c12016-04-13 10:07:16 -07002317 RTC_DCHECK(media_desc->codecs().empty());
solenberg9fa49752016-10-08 13:02:44 -07002318 for (int payload_type : fmts) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002319 if (!media_desc->HasCodec(payload_type) &&
2320 payload_type >= 0 &&
kjellander3e33bfe2016-06-20 07:04:09 -07002321 static_cast<uint32_t>(payload_type) <
2322 arraysize(kStaticPayloadAudioCodecs)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002323 std::string encoding_name = kStaticPayloadAudioCodecs[payload_type].name;
2324 int clock_rate = kStaticPayloadAudioCodecs[payload_type].clockrate;
Peter Kasting69558702016-01-12 16:26:35 -08002325 size_t channels = kStaticPayloadAudioCodecs[payload_type].channels;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002326 media_desc->AddCodec(cricket::AudioCodec(payload_type, encoding_name,
deadbeef67cf2c12016-04-13 10:07:16 -07002327 clock_rate, 0, channels));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002328 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002329 }
2330}
2331
2332template <class C>
2333static C* ParseContentDescription(const std::string& message,
2334 const MediaType media_type,
2335 int mline_index,
2336 const std::string& protocol,
deadbeef67cf2c12016-04-13 10:07:16 -07002337 const std::vector<int>& payload_types,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002338 size_t* pos,
2339 std::string* content_name,
deadbeef25ed4352016-12-12 18:37:36 -08002340 bool* bundle_only,
Steve Antone831b8c2018-02-01 12:22:16 -08002341 int* msid_signaling,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002342 TransportDescription* transport,
2343 std::vector<JsepIceCandidate*>* candidates,
2344 webrtc::SdpParseError* error) {
2345 C* media_desc = new C();
2346 switch (media_type) {
2347 case cricket::MEDIA_TYPE_AUDIO:
2348 *content_name = cricket::CN_AUDIO;
2349 break;
2350 case cricket::MEDIA_TYPE_VIDEO:
2351 *content_name = cricket::CN_VIDEO;
2352 break;
2353 case cricket::MEDIA_TYPE_DATA:
2354 *content_name = cricket::CN_DATA;
2355 break;
2356 default:
nissec80e7412017-01-11 05:56:46 -08002357 RTC_NOTREACHED();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002358 break;
2359 }
deadbeef67cf2c12016-04-13 10:07:16 -07002360 if (!ParseContent(message, media_type, mline_index, protocol, payload_types,
Steve Antone831b8c2018-02-01 12:22:16 -08002361 pos, content_name, bundle_only, msid_signaling, media_desc,
2362 transport, candidates, error)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002363 delete media_desc;
zhihuang38989e52017-03-21 11:04:53 -07002364 return nullptr;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002365 }
2366 // Sort the codecs according to the m-line fmt list.
deadbeef67cf2c12016-04-13 10:07:16 -07002367 std::unordered_map<int, int> payload_type_preferences;
2368 // "size + 1" so that the lowest preference payload type has a preference of
2369 // 1, which is greater than the default (0) for payload types not in the fmt
2370 // list.
2371 int preference = static_cast<int>(payload_types.size() + 1);
2372 for (int pt : payload_types) {
2373 payload_type_preferences[pt] = preference--;
2374 }
2375 std::vector<typename C::CodecType> codecs = media_desc->codecs();
2376 std::sort(codecs.begin(), codecs.end(), [&payload_type_preferences](
2377 const typename C::CodecType& a,
2378 const typename C::CodecType& b) {
2379 return payload_type_preferences[a.id] > payload_type_preferences[b.id];
2380 });
2381 media_desc->set_codecs(codecs);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002382 return media_desc;
2383}
2384
2385bool ParseMediaDescription(const std::string& message,
2386 const TransportDescription& session_td,
2387 const RtpHeaderExtensions& session_extmaps,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002388 size_t* pos,
zhihuang38989e52017-03-21 11:04:53 -07002389 const rtc::SocketAddress& session_connection_addr,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002390 cricket::SessionDescription* desc,
2391 std::vector<JsepIceCandidate*>* candidates,
2392 SdpParseError* error) {
nisseede5da42017-01-12 05:15:36 -08002393 RTC_DCHECK(desc != NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002394 std::string line;
2395 int mline_index = -1;
Steve Antone831b8c2018-02-01 12:22:16 -08002396 int msid_signaling = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002397
2398 // Zero or more media descriptions
2399 // RFC 4566
2400 // m=<media> <port> <proto> <fmt>
2401 while (GetLineWithType(message, pos, &line, kLineTypeMedia)) {
2402 ++mline_index;
2403
2404 std::vector<std::string> fields;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002405 rtc::split(line.substr(kLinePrefixLength),
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002406 kSdpDelimiterSpace, &fields);
zstein4b2e0822017-02-17 19:48:38 -08002407
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002408 const size_t expected_min_fields = 4;
2409 if (fields.size() < expected_min_fields) {
2410 return ParseFailedExpectMinFieldNum(line, expected_min_fields, error);
2411 }
deadbeef25ed4352016-12-12 18:37:36 -08002412 bool port_rejected = false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002413 // RFC 3264
2414 // To reject an offered stream, the port number in the corresponding stream
2415 // in the answer MUST be set to zero.
2416 if (fields[1] == kMediaPortRejected) {
deadbeef25ed4352016-12-12 18:37:36 -08002417 port_rejected = true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002418 }
2419
zhihuang38989e52017-03-21 11:04:53 -07002420 int port = 0;
2421 if (!rtc::FromString<int>(fields[1], &port) || !IsValidPort(port)) {
2422 return ParseFailed(line, "The port number is invalid", error);
2423 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002424 std::string protocol = fields[2];
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002425
2426 // <fmt>
deadbeef67cf2c12016-04-13 10:07:16 -07002427 std::vector<int> payload_types;
pthatcher@webrtc.org3341b402015-02-13 21:14:22 +00002428 if (IsRtp(protocol)) {
jiayl@webrtc.orgddb85ab2014-09-05 16:31:56 +00002429 for (size_t j = 3 ; j < fields.size(); ++j) {
2430 // TODO(wu): Remove when below bug is fixed.
2431 // https://bugzilla.mozilla.org/show_bug.cgi?id=996329
pbosbb36fdf2015-07-09 07:48:14 -07002432 if (fields[j].empty() && j == fields.size() - 1) {
jiayl@webrtc.orgddb85ab2014-09-05 16:31:56 +00002433 continue;
2434 }
wu@webrtc.org36eda7c2014-04-15 20:37:30 +00002435
jiayl@webrtc.orgddb85ab2014-09-05 16:31:56 +00002436 int pl = 0;
pkasting@chromium.orge9facf82015-02-17 20:36:28 +00002437 if (!GetPayloadTypeFromString(line, fields[j], &pl, error)) {
jiayl@webrtc.orgddb85ab2014-09-05 16:31:56 +00002438 return false;
2439 }
deadbeef67cf2c12016-04-13 10:07:16 -07002440 payload_types.push_back(pl);
wu@webrtc.org5e760e72014-04-02 23:19:09 +00002441 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002442 }
2443
2444 // Make a temporary TransportDescription based on |session_td|.
2445 // Some of this gets overwritten by ParseContent.
deadbeef46eed762016-01-28 13:24:37 -08002446 TransportDescription transport(
2447 session_td.transport_options, session_td.ice_ufrag, session_td.ice_pwd,
2448 session_td.ice_mode, session_td.connection_role,
2449 session_td.identity_fingerprint.get());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002450
kwibergd1fe2812016-04-27 06:47:29 -07002451 std::unique_ptr<MediaContentDescription> content;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002452 std::string content_name;
deadbeef25ed4352016-12-12 18:37:36 -08002453 bool bundle_only = false;
Steve Antone831b8c2018-02-01 12:22:16 -08002454 int section_msid_signaling = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002455 if (HasAttribute(line, kMediaTypeVideo)) {
2456 content.reset(ParseContentDescription<VideoContentDescription>(
deadbeef67cf2c12016-04-13 10:07:16 -07002457 message, cricket::MEDIA_TYPE_VIDEO, mline_index, protocol,
Steve Antone831b8c2018-02-01 12:22:16 -08002458 payload_types, pos, &content_name, &bundle_only,
2459 &section_msid_signaling, &transport, candidates, error));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002460 } else if (HasAttribute(line, kMediaTypeAudio)) {
2461 content.reset(ParseContentDescription<AudioContentDescription>(
deadbeef67cf2c12016-04-13 10:07:16 -07002462 message, cricket::MEDIA_TYPE_AUDIO, 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, kMediaTypeData)) {
jiayl@webrtc.org0e527722014-09-08 21:43:43 +00002466 DataContentDescription* data_desc =
wu@webrtc.org78187522013-10-07 23:32:02 +00002467 ParseContentDescription<DataContentDescription>(
deadbeef67cf2c12016-04-13 10:07:16 -07002468 message, cricket::MEDIA_TYPE_DATA, mline_index, protocol,
Steve Antone831b8c2018-02-01 12:22:16 -08002469 payload_types, pos, &content_name, &bundle_only,
2470 &section_msid_signaling, &transport, candidates, error);
jiayl@webrtc.org0e527722014-09-08 21:43:43 +00002471 content.reset(data_desc);
wu@webrtc.org78187522013-10-07 23:32:02 +00002472
zstein4b2e0822017-02-17 19:48:38 -08002473 if (data_desc && IsDtlsSctp(protocol)) {
2474 int p;
2475 if (rtc::FromString(fields[3], &p)) {
2476 if (!AddSctpDataCodec(data_desc, p)) {
2477 return false;
2478 }
2479 } else if (fields[3] == kDefaultSctpmapProtocol) {
2480 data_desc->set_use_sctpmap(false);
2481 }
wu@webrtc.org78187522013-10-07 23:32:02 +00002482 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002483 } else {
Mirko Bonadei675513b2017-11-09 11:09:25 +01002484 RTC_LOG(LS_WARNING) << "Unsupported media type: " << line;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002485 continue;
2486 }
2487 if (!content.get()) {
2488 // ParseContentDescription returns NULL if failed.
2489 return false;
2490 }
2491
Steve Antone831b8c2018-02-01 12:22:16 -08002492 msid_signaling |= section_msid_signaling;
2493
deadbeef25ed4352016-12-12 18:37:36 -08002494 bool content_rejected = false;
deadbeef12771a12017-01-03 13:53:47 -08002495 // A port of 0 is not interpreted as a rejected m= section when it's
2496 // used along with a=bundle-only.
deadbeef25ed4352016-12-12 18:37:36 -08002497 if (bundle_only) {
deadbeef25ed4352016-12-12 18:37:36 -08002498 if (!port_rejected) {
deadbeef12771a12017-01-03 13:53:47 -08002499 // Usage of bundle-only with a nonzero port is unspecified. So just
2500 // ignore bundle-only if we see this.
2501 bundle_only = false;
Mirko Bonadei675513b2017-11-09 11:09:25 +01002502 RTC_LOG(LS_WARNING)
deadbeef12771a12017-01-03 13:53:47 -08002503 << "a=bundle-only attribute observed with a nonzero "
Jonas Olsson45cc8902018-02-13 10:37:07 +01002504 "port; this usage is unspecified so the attribute is being "
2505 "ignored.";
deadbeef25ed4352016-12-12 18:37:36 -08002506 }
2507 } else {
2508 // If not using bundle-only, interpret port 0 in the normal way; the m=
2509 // section is being rejected.
2510 content_rejected = port_rejected;
2511 }
2512
pthatcher@webrtc.org3341b402015-02-13 21:14:22 +00002513 if (IsRtp(protocol)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002514 // Set the extmap.
2515 if (!session_extmaps.empty() &&
2516 !content->rtp_header_extensions().empty()) {
2517 return ParseFailed("",
2518 "The a=extmap MUST be either all session level or "
2519 "all media level.",
2520 error);
2521 }
2522 for (size_t i = 0; i < session_extmaps.size(); ++i) {
2523 content->AddRtpHeaderExtension(session_extmaps[i]);
2524 }
2525 }
2526 content->set_protocol(protocol);
zhihuang38989e52017-03-21 11:04:53 -07002527
2528 // Use the session level connection address if the media level addresses are
2529 // not specified.
2530 rtc::SocketAddress address;
2531 address = content->connection_address().IsNil()
2532 ? session_connection_addr
2533 : content->connection_address();
2534 address.SetPort(port);
2535 content->set_connection_address(address);
2536
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002537 desc->AddContent(content_name,
Steve Anton5adfafd2017-12-20 16:34:00 -08002538 IsDtlsSctp(protocol) ? MediaProtocolType::kSctp
2539 : MediaProtocolType::kRtp,
deadbeef25ed4352016-12-12 18:37:36 -08002540 content_rejected, bundle_only, content.release());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002541 // Create TransportInfo with the media level "ice-pwd" and "ice-ufrag".
2542 TransportInfo transport_info(content_name, transport);
2543
2544 if (!desc->AddTransportInfo(transport_info)) {
2545 std::ostringstream description;
2546 description << "Failed to AddTransportInfo with content name: "
2547 << content_name;
2548 return ParseFailed("", description.str(), error);
2549 }
2550 }
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00002551
Steve Antone831b8c2018-02-01 12:22:16 -08002552 desc->set_msid_signaling(msid_signaling);
2553
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00002554 size_t end_of_message = message.size();
2555 if (mline_index == -1 && *pos != end_of_message) {
2556 ParseFailed(message, *pos, "Expects m line.", error);
2557 return false;
2558 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002559 return true;
2560}
2561
2562bool VerifyCodec(const cricket::Codec& codec) {
2563 // Codec has not been populated correctly unless the name has been set. This
2564 // can happen if an SDP has an fmtp or rtcp-fb with a payload type but doesn't
2565 // have a corresponding "rtpmap" line.
htab39db842016-12-08 01:50:48 -08002566 return !codec.name.empty();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002567}
2568
2569bool VerifyAudioCodecs(const AudioContentDescription* audio_desc) {
2570 const std::vector<cricket::AudioCodec>& codecs = audio_desc->codecs();
2571 for (std::vector<cricket::AudioCodec>::const_iterator iter = codecs.begin();
2572 iter != codecs.end(); ++iter) {
2573 if (!VerifyCodec(*iter)) {
2574 return false;
2575 }
2576 }
2577 return true;
2578}
2579
2580bool VerifyVideoCodecs(const VideoContentDescription* video_desc) {
2581 const std::vector<cricket::VideoCodec>& codecs = video_desc->codecs();
2582 for (std::vector<cricket::VideoCodec>::const_iterator iter = codecs.begin();
2583 iter != codecs.end(); ++iter) {
2584 if (!VerifyCodec(*iter)) {
2585 return false;
2586 }
2587 }
2588 return true;
2589}
2590
2591void AddParameters(const cricket::CodecParameterMap& parameters,
2592 cricket::Codec* codec) {
2593 for (cricket::CodecParameterMap::const_iterator iter =
2594 parameters.begin(); iter != parameters.end(); ++iter) {
2595 codec->SetParam(iter->first, iter->second);
2596 }
2597}
2598
2599void AddFeedbackParameter(const cricket::FeedbackParam& feedback_param,
2600 cricket::Codec* codec) {
2601 codec->AddFeedbackParam(feedback_param);
2602}
2603
2604void AddFeedbackParameters(const cricket::FeedbackParams& feedback_params,
2605 cricket::Codec* codec) {
2606 for (std::vector<cricket::FeedbackParam>::const_iterator iter =
2607 feedback_params.params().begin();
2608 iter != feedback_params.params().end(); ++iter) {
2609 codec->AddFeedbackParam(*iter);
2610 }
2611}
2612
2613// Gets the current codec setting associated with |payload_type|. If there
changbin.shao@webrtc.org2d25b442015-03-16 04:14:34 +00002614// is no Codec associated with that payload type it returns an empty codec
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002615// with that payload type.
2616template <class T>
changbin.shao@webrtc.org2d25b442015-03-16 04:14:34 +00002617T GetCodecWithPayloadType(const std::vector<T>& codecs, int payload_type) {
magjedb05fa242016-11-11 04:00:16 -08002618 const T* codec = FindCodecById(codecs, payload_type);
2619 if (codec)
2620 return *codec;
2621 // Return empty codec with |payload_type|.
changbin.shao@webrtc.org2d25b442015-03-16 04:14:34 +00002622 T ret_val;
magjedb05fa242016-11-11 04:00:16 -08002623 ret_val.id = payload_type;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002624 return ret_val;
2625}
2626
2627// Updates or creates a new codec entry in the audio description.
2628template <class T, class U>
2629void AddOrReplaceCodec(MediaContentDescription* content_desc, const U& codec) {
2630 T* desc = static_cast<T*>(content_desc);
2631 std::vector<U> codecs = desc->codecs();
2632 bool found = false;
2633
2634 typename std::vector<U>::iterator iter;
2635 for (iter = codecs.begin(); iter != codecs.end(); ++iter) {
2636 if (iter->id == codec.id) {
2637 *iter = codec;
2638 found = true;
2639 break;
2640 }
2641 }
2642 if (!found) {
2643 desc->AddCodec(codec);
2644 return;
2645 }
2646 desc->set_codecs(codecs);
2647}
2648
2649// Adds or updates existing codec corresponding to |payload_type| according
2650// to |parameters|.
2651template <class T, class U>
2652void UpdateCodec(MediaContentDescription* content_desc, int payload_type,
2653 const cricket::CodecParameterMap& parameters) {
2654 // Codec might already have been populated (from rtpmap).
changbin.shao@webrtc.org2d25b442015-03-16 04:14:34 +00002655 U new_codec = GetCodecWithPayloadType(static_cast<T*>(content_desc)->codecs(),
2656 payload_type);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002657 AddParameters(parameters, &new_codec);
2658 AddOrReplaceCodec<T, U>(content_desc, new_codec);
2659}
2660
2661// Adds or updates existing codec corresponding to |payload_type| according
2662// to |feedback_param|.
2663template <class T, class U>
2664void UpdateCodec(MediaContentDescription* content_desc, int payload_type,
2665 const cricket::FeedbackParam& feedback_param) {
2666 // Codec might already have been populated (from rtpmap).
changbin.shao@webrtc.org2d25b442015-03-16 04:14:34 +00002667 U new_codec = GetCodecWithPayloadType(static_cast<T*>(content_desc)->codecs(),
2668 payload_type);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002669 AddFeedbackParameter(feedback_param, &new_codec);
2670 AddOrReplaceCodec<T, U>(content_desc, new_codec);
2671}
2672
jlmiller@webrtc.orga744a282015-02-18 21:37:46 +00002673template <class T>
2674bool PopWildcardCodec(std::vector<T>* codecs, T* wildcard_codec) {
2675 for (auto iter = codecs->begin(); iter != codecs->end(); ++iter) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002676 if (iter->id == kWildcardPayloadType) {
2677 *wildcard_codec = *iter;
2678 codecs->erase(iter);
2679 return true;
2680 }
2681 }
2682 return false;
2683}
2684
jlmiller@webrtc.orga744a282015-02-18 21:37:46 +00002685template<class T>
2686void UpdateFromWildcardCodecs(cricket::MediaContentDescriptionImpl<T>* desc) {
2687 auto codecs = desc->codecs();
2688 T wildcard_codec;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002689 if (!PopWildcardCodec(&codecs, &wildcard_codec)) {
2690 return;
2691 }
jlmiller@webrtc.orga744a282015-02-18 21:37:46 +00002692 for (auto& codec : codecs) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002693 AddFeedbackParameters(wildcard_codec.feedback_params, &codec);
2694 }
jlmiller@webrtc.orga744a282015-02-18 21:37:46 +00002695 desc->set_codecs(codecs);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002696}
2697
2698void AddAudioAttribute(const std::string& name, const std::string& value,
2699 AudioContentDescription* audio_desc) {
2700 if (value.empty()) {
2701 return;
2702 }
2703 std::vector<cricket::AudioCodec> codecs = audio_desc->codecs();
2704 for (std::vector<cricket::AudioCodec>::iterator iter = codecs.begin();
2705 iter != codecs.end(); ++iter) {
2706 iter->params[name] = value;
2707 }
2708 audio_desc->set_codecs(codecs);
2709}
2710
2711bool ParseContent(const std::string& message,
2712 const MediaType media_type,
2713 int mline_index,
2714 const std::string& protocol,
deadbeef67cf2c12016-04-13 10:07:16 -07002715 const std::vector<int>& payload_types,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002716 size_t* pos,
2717 std::string* content_name,
deadbeef25ed4352016-12-12 18:37:36 -08002718 bool* bundle_only,
Steve Antone831b8c2018-02-01 12:22:16 -08002719 int* msid_signaling,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002720 MediaContentDescription* media_desc,
2721 TransportDescription* transport,
2722 std::vector<JsepIceCandidate*>* candidates,
2723 SdpParseError* error) {
nisseede5da42017-01-12 05:15:36 -08002724 RTC_DCHECK(media_desc != NULL);
2725 RTC_DCHECK(content_name != NULL);
2726 RTC_DCHECK(transport != NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002727
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +00002728 if (media_type == cricket::MEDIA_TYPE_AUDIO) {
Steve Antonb1c1de12017-12-21 15:14:30 -08002729 MaybeCreateStaticPayloadAudioCodecs(payload_types, media_desc->as_audio());
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +00002730 }
2731
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002732 // The media level "ice-ufrag" and "ice-pwd".
2733 // The candidates before update the media level "ice-pwd" and "ice-ufrag".
2734 Candidates candidates_orig;
2735 std::string line;
2736 std::string mline_id;
2737 // Tracks created out of the ssrc attributes.
2738 StreamParamsVec tracks;
2739 SsrcInfoVec ssrc_infos;
2740 SsrcGroupVec ssrc_groups;
2741 std::string maxptime_as_string;
2742 std::string ptime_as_string;
Seth Hampson5b4f0752018-04-02 16:31:36 -07002743 std::vector<std::string> stream_ids;
deadbeef9d3584c2016-02-16 17:54:10 -08002744 std::string track_id;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002745
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002746 // Loop until the next m line
2747 while (!IsLineType(message, kLineTypeMedia, *pos)) {
2748 if (!GetLine(message, pos, &line)) {
2749 if (*pos >= message.size()) {
2750 break; // Done parsing
2751 } else {
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +00002752 return ParseFailed(message, *pos, "Invalid SDP line.", error);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002753 }
2754 }
2755
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002756 // RFC 4566
2757 // b=* (zero or more bandwidth information lines)
2758 if (IsLineType(line, kLineTypeSessionBandwidth)) {
2759 std::string bandwidth;
2760 if (HasAttribute(line, kApplicationSpecificMaximum)) {
2761 if (!GetValue(line, kApplicationSpecificMaximum, &bandwidth, error)) {
2762 return false;
2763 } else {
wu@webrtc.org5e760e72014-04-02 23:19:09 +00002764 int b = 0;
2765 if (!GetValueFromString(line, bandwidth, &b, error)) {
2766 return false;
2767 }
deadbeef3e8016e2017-08-03 17:49:30 -07002768 // TODO(deadbeef): Historically, applications may be setting a value
2769 // of -1 to mean "unset any previously set bandwidth limit", even
2770 // though ommitting the "b=AS" entirely will do just that. Once we've
2771 // transitioned applications to doing the right thing, it would be
2772 // better to treat this as a hard error instead of just ignoring it.
2773 if (b == -1) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01002774 RTC_LOG(LS_WARNING)
2775 << "Ignoring \"b=AS:-1\"; will be treated as \"no "
2776 "bandwidth limit\".";
deadbeef3e8016e2017-08-03 17:49:30 -07002777 continue;
2778 }
deadbeefbc88c6b2017-08-02 11:26:34 -07002779 if (b < 0) {
2780 return ParseFailed(line, "b=AS value can't be negative.", error);
2781 }
Peter Thatcherc0c3a862015-06-24 15:31:25 -07002782 // We should never use more than the default bandwidth for RTP-based
2783 // data channels. Don't allow SDP to set the bandwidth, because
2784 // that would give JS the opportunity to "break the Internet".
2785 // See: https://code.google.com/p/chromium/issues/detail?id=280726
2786 if (media_type == cricket::MEDIA_TYPE_DATA && IsRtp(protocol) &&
2787 b > cricket::kDataMaxBandwidth / 1000) {
2788 std::ostringstream description;
2789 description << "RTP-based data channels may not send more than "
2790 << cricket::kDataMaxBandwidth / 1000 << "kbps.";
2791 return ParseFailed(line, description.str(), error);
2792 }
deadbeefb2362572016-12-13 16:37:06 -08002793 // Prevent integer overflow.
2794 b = std::min(b, INT_MAX / 1000);
wu@webrtc.org5e760e72014-04-02 23:19:09 +00002795 media_desc->set_bandwidth(b * 1000);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002796 }
2797 }
2798 continue;
2799 }
2800
zhihuang38989e52017-03-21 11:04:53 -07002801 // Parse the media level connection data.
2802 if (IsLineType(line, kLineTypeConnection)) {
2803 rtc::SocketAddress addr;
2804 if (!ParseConnectionData(line, &addr, error)) {
2805 return false;
2806 }
2807 media_desc->set_connection_address(addr);
2808 continue;
2809 }
2810
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002811 if (!IsLineType(line, kLineTypeAttributes)) {
Steve Anton36b29d12017-10-30 09:57:42 -07002812 // TODO(deadbeef): Handle other lines if needed.
Mirko Bonadei675513b2017-11-09 11:09:25 +01002813 RTC_LOG(LS_INFO) << "Ignored line: " << line;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002814 continue;
2815 }
2816
2817 // Handle attributes common to SCTP and RTP.
2818 if (HasAttribute(line, kAttributeMid)) {
2819 // RFC 3388
2820 // mid-attribute = "a=mid:" identification-tag
2821 // identification-tag = token
2822 // Use the mid identification-tag as the content name.
2823 if (!GetValue(line, kAttributeMid, &mline_id, error)) {
2824 return false;
2825 }
2826 *content_name = mline_id;
deadbeef25ed4352016-12-12 18:37:36 -08002827 } else if (HasAttribute(line, kAttributeBundleOnly)) {
2828 *bundle_only = true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002829 } else if (HasAttribute(line, kAttributeCandidate)) {
2830 Candidate candidate;
2831 if (!ParseCandidate(line, &candidate, error, false)) {
2832 return false;
2833 }
deadbeef7bcdb692017-01-20 12:43:58 -08002834 // ParseCandidate will parse non-standard ufrag and password attributes,
2835 // since it's used for candidate trickling, but we only want to process
2836 // the "a=ice-ufrag"/"a=ice-pwd" values in a session description, so
2837 // strip them off at this point.
2838 candidate.set_username(std::string());
2839 candidate.set_password(std::string());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002840 candidates_orig.push_back(candidate);
2841 } else if (HasAttribute(line, kAttributeIceUfrag)) {
2842 if (!GetValue(line, kAttributeIceUfrag, &transport->ice_ufrag, error)) {
2843 return false;
2844 }
2845 } else if (HasAttribute(line, kAttributeIcePwd)) {
2846 if (!GetValue(line, kAttributeIcePwd, &transport->ice_pwd, error)) {
2847 return false;
2848 }
2849 } else if (HasAttribute(line, kAttributeIceOption)) {
2850 if (!ParseIceOptions(line, &transport->transport_options, error)) {
2851 return false;
2852 }
2853 } else if (HasAttribute(line, kAttributeFmtp)) {
2854 if (!ParseFmtpAttributes(line, media_type, media_desc, error)) {
2855 return false;
2856 }
2857 } else if (HasAttribute(line, kAttributeFingerprint)) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002858 rtc::SSLFingerprint* fingerprint = NULL;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002859
2860 if (!ParseFingerprintAttribute(line, &fingerprint, error)) {
2861 return false;
2862 }
2863 transport->identity_fingerprint.reset(fingerprint);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00002864 } else if (HasAttribute(line, kAttributeSetup)) {
2865 if (!ParseDtlsSetup(line, &(transport->connection_role), error)) {
2866 return false;
2867 }
pthatcher@webrtc.org3341b402015-02-13 21:14:22 +00002868 } else if (IsDtlsSctp(protocol) && HasAttribute(line, kAttributeSctpPort)) {
deadbeef7e146cb2016-09-28 10:04:34 -07002869 if (media_type != cricket::MEDIA_TYPE_DATA) {
2870 return ParseFailed(
2871 line, "sctp-port attribute found in non-data media description.",
2872 error);
2873 }
jiayl@webrtc.orgddb85ab2014-09-05 16:31:56 +00002874 int sctp_port;
2875 if (!ParseSctpPort(line, &sctp_port, error)) {
2876 return false;
2877 }
Steve Antonb1c1de12017-12-21 15:14:30 -08002878 if (!AddSctpDataCodec(media_desc->as_data(), sctp_port)) {
jiayl@webrtc.orgddb85ab2014-09-05 16:31:56 +00002879 return false;
2880 }
pthatcher@webrtc.org3341b402015-02-13 21:14:22 +00002881 } else if (IsRtp(protocol)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002882 //
2883 // RTP specific attrubtes
2884 //
2885 if (HasAttribute(line, kAttributeRtcpMux)) {
2886 media_desc->set_rtcp_mux(true);
deadbeef13871492015-12-09 12:37:51 -08002887 } else if (HasAttribute(line, kAttributeRtcpReducedSize)) {
2888 media_desc->set_rtcp_reduced_size(true);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002889 } else if (HasAttribute(line, kAttributeSsrcGroup)) {
2890 if (!ParseSsrcGroupAttribute(line, &ssrc_groups, error)) {
2891 return false;
2892 }
2893 } else if (HasAttribute(line, kAttributeSsrc)) {
Steve Antone831b8c2018-02-01 12:22:16 -08002894 if (!ParseSsrcAttribute(line, &ssrc_infos, msid_signaling, error)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002895 return false;
2896 }
2897 } else if (HasAttribute(line, kAttributeCrypto)) {
2898 if (!ParseCryptoAttribute(line, media_desc, error)) {
2899 return false;
2900 }
2901 } else if (HasAttribute(line, kAttributeRtpmap)) {
deadbeef67cf2c12016-04-13 10:07:16 -07002902 if (!ParseRtpmapAttribute(line, media_type, payload_types, media_desc,
2903 error)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002904 return false;
2905 }
2906 } else if (HasAttribute(line, kCodecParamMaxPTime)) {
2907 if (!GetValue(line, kCodecParamMaxPTime, &maxptime_as_string, error)) {
2908 return false;
2909 }
2910 } else if (HasAttribute(line, kAttributeRtcpFb)) {
2911 if (!ParseRtcpFbAttribute(line, media_type, media_desc, error)) {
2912 return false;
2913 }
2914 } else if (HasAttribute(line, kCodecParamPTime)) {
2915 if (!GetValue(line, kCodecParamPTime, &ptime_as_string, error)) {
2916 return false;
2917 }
2918 } else if (HasAttribute(line, kAttributeSendOnly)) {
Steve Anton4e70a722017-11-28 14:57:10 -08002919 media_desc->set_direction(RtpTransceiverDirection::kSendOnly);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002920 } else if (HasAttribute(line, kAttributeRecvOnly)) {
Steve Anton4e70a722017-11-28 14:57:10 -08002921 media_desc->set_direction(RtpTransceiverDirection::kRecvOnly);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002922 } else if (HasAttribute(line, kAttributeInactive)) {
Steve Anton4e70a722017-11-28 14:57:10 -08002923 media_desc->set_direction(RtpTransceiverDirection::kInactive);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002924 } else if (HasAttribute(line, kAttributeSendRecv)) {
Steve Anton4e70a722017-11-28 14:57:10 -08002925 media_desc->set_direction(RtpTransceiverDirection::kSendRecv);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002926 } else if (HasAttribute(line, kAttributeExtmap)) {
isheriff6f8d6862016-05-26 11:24:55 -07002927 RtpExtension extmap;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002928 if (!ParseExtmap(line, &extmap, error)) {
2929 return false;
2930 }
2931 media_desc->AddRtpHeaderExtension(extmap);
2932 } else if (HasAttribute(line, kAttributeXGoogleFlag)) {
2933 // Experimental attribute. Conference mode activates more aggressive
2934 // AEC and NS settings.
Steve Anton36b29d12017-10-30 09:57:42 -07002935 // TODO(deadbeef): expose API to set these directly.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002936 std::string flag_value;
2937 if (!GetValue(line, kAttributeXGoogleFlag, &flag_value, error)) {
2938 return false;
2939 }
2940 if (flag_value.compare(kValueConference) == 0)
2941 media_desc->set_conference_mode(true);
deadbeef9d3584c2016-02-16 17:54:10 -08002942 } else if (HasAttribute(line, kAttributeMsid)) {
Seth Hampson5b4f0752018-04-02 16:31:36 -07002943 if (!ParseMsidAttribute(line, &stream_ids, &track_id, error)) {
deadbeef9d3584c2016-02-16 17:54:10 -08002944 return false;
2945 }
Steve Antone831b8c2018-02-01 12:22:16 -08002946 *msid_signaling |= cricket::kMsidSignalingMediaSection;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002947 }
2948 } else {
2949 // Only parse lines that we are interested of.
Mirko Bonadei675513b2017-11-09 11:09:25 +01002950 RTC_LOG(LS_INFO) << "Ignored line: " << line;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002951 continue;
2952 }
2953 }
2954
2955 // Create tracks from the |ssrc_infos|.
Taylor Brandstetter5de6b752016-03-09 17:02:30 -08002956 // If the stream_id/track_id for all SSRCS are identical, one StreamParams
2957 // will be created in CreateTracksFromSsrcInfos, containing all the SSRCs from
2958 // the m= section.
Seth Hampson5897a6e2018-04-03 11:16:33 -07002959 if (!ssrc_infos.empty()) {
2960 CreateTracksFromSsrcInfos(ssrc_infos, stream_ids, track_id, &tracks,
2961 *msid_signaling);
2962 } else if (media_type != cricket::MEDIA_TYPE_DATA &&
2963 (*msid_signaling & cricket::kMsidSignalingMediaSection)) {
2964 // If the stream_ids/track_id was signaled but SSRCs were unsignaled we
2965 // still create a track. This isn't done for data media types because
2966 // StreamParams aren't used for SCTP streams, and RTP data channels don't
2967 // support unsignaled SSRCs.
2968 CreateTrackWithNoSsrcs(stream_ids, track_id, &tracks);
2969 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002970
2971 // Add the ssrc group to the track.
2972 for (SsrcGroupVec::iterator ssrc_group = ssrc_groups.begin();
2973 ssrc_group != ssrc_groups.end(); ++ssrc_group) {
2974 if (ssrc_group->ssrcs.empty()) {
2975 continue;
2976 }
Peter Boström0c4e06b2015-10-07 12:23:21 +02002977 uint32_t ssrc = ssrc_group->ssrcs.front();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002978 for (StreamParamsVec::iterator track = tracks.begin();
2979 track != tracks.end(); ++track) {
2980 if (track->has_ssrc(ssrc)) {
2981 track->ssrc_groups.push_back(*ssrc_group);
2982 }
2983 }
2984 }
2985
2986 // Add the new tracks to the |media_desc|.
deadbeef9d3584c2016-02-16 17:54:10 -08002987 for (StreamParams& track : tracks) {
2988 media_desc->AddStream(track);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002989 }
2990
2991 if (media_type == cricket::MEDIA_TYPE_AUDIO) {
Steve Antonb1c1de12017-12-21 15:14:30 -08002992 AudioContentDescription* audio_desc = media_desc->as_audio();
jlmiller@webrtc.orga744a282015-02-18 21:37:46 +00002993 UpdateFromWildcardCodecs(audio_desc);
2994
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002995 // Verify audio codec ensures that no audio codec has been populated with
2996 // only fmtp.
2997 if (!VerifyAudioCodecs(audio_desc)) {
2998 return ParseFailed("Failed to parse audio codecs correctly.", error);
2999 }
3000 AddAudioAttribute(kCodecParamMaxPTime, maxptime_as_string, audio_desc);
3001 AddAudioAttribute(kCodecParamPTime, ptime_as_string, audio_desc);
3002 }
3003
3004 if (media_type == cricket::MEDIA_TYPE_VIDEO) {
Steve Antonb1c1de12017-12-21 15:14:30 -08003005 VideoContentDescription* video_desc = media_desc->as_video();
jlmiller@webrtc.orga744a282015-02-18 21:37:46 +00003006 UpdateFromWildcardCodecs(video_desc);
3007 // Verify video codec ensures that no video codec has been populated with
3008 // only rtcp-fb.
3009 if (!VerifyVideoCodecs(video_desc)) {
3010 return ParseFailed("Failed to parse video codecs correctly.", error);
3011 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003012 }
3013
3014 // RFC 5245
3015 // Update the candidates with the media level "ice-pwd" and "ice-ufrag".
3016 for (Candidates::iterator it = candidates_orig.begin();
3017 it != candidates_orig.end(); ++it) {
nisseede5da42017-01-12 05:15:36 -08003018 RTC_DCHECK((*it).username().empty() ||
3019 (*it).username() == transport->ice_ufrag);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003020 (*it).set_username(transport->ice_ufrag);
nisseede5da42017-01-12 05:15:36 -08003021 RTC_DCHECK((*it).password().empty());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003022 (*it).set_password(transport->ice_pwd);
3023 candidates->push_back(
3024 new JsepIceCandidate(mline_id, mline_index, *it));
3025 }
3026 return true;
3027}
3028
Steve Antone831b8c2018-02-01 12:22:16 -08003029bool ParseSsrcAttribute(const std::string& line,
3030 SsrcInfoVec* ssrc_infos,
3031 int* msid_signaling,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003032 SdpParseError* error) {
nisseede5da42017-01-12 05:15:36 -08003033 RTC_DCHECK(ssrc_infos != NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003034 // RFC 5576
3035 // a=ssrc:<ssrc-id> <attribute>
3036 // a=ssrc:<ssrc-id> <attribute>:<value>
3037 std::string field1, field2;
Donald Curtis144d0182015-05-15 13:14:24 -07003038 if (!rtc::tokenize_first(line.substr(kLinePrefixLength), kSdpDelimiterSpace,
3039 &field1, &field2)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003040 const size_t expected_fields = 2;
3041 return ParseFailedExpectFieldNum(line, expected_fields, error);
3042 }
3043
3044 // ssrc:<ssrc-id>
3045 std::string ssrc_id_s;
3046 if (!GetValue(field1, kAttributeSsrc, &ssrc_id_s, error)) {
3047 return false;
3048 }
Peter Boström0c4e06b2015-10-07 12:23:21 +02003049 uint32_t ssrc_id = 0;
wu@webrtc.org5e760e72014-04-02 23:19:09 +00003050 if (!GetValueFromString(line, ssrc_id_s, &ssrc_id, error)) {
3051 return false;
3052 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003053
3054 std::string attribute;
3055 std::string value;
Donald Curtis144d0182015-05-15 13:14:24 -07003056 if (!rtc::tokenize_first(field2, kSdpDelimiterColon, &attribute, &value)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003057 std::ostringstream description;
3058 description << "Failed to get the ssrc attribute value from " << field2
3059 << ". Expected format <attribute>:<value>.";
3060 return ParseFailed(line, description.str(), error);
3061 }
3062
3063 // Check if there's already an item for this |ssrc_id|. Create a new one if
3064 // there isn't.
3065 SsrcInfoVec::iterator ssrc_info = ssrc_infos->begin();
3066 for (; ssrc_info != ssrc_infos->end(); ++ssrc_info) {
3067 if (ssrc_info->ssrc_id == ssrc_id) {
3068 break;
3069 }
3070 }
3071 if (ssrc_info == ssrc_infos->end()) {
3072 SsrcInfo info;
3073 info.ssrc_id = ssrc_id;
3074 ssrc_infos->push_back(info);
3075 ssrc_info = ssrc_infos->end() - 1;
3076 }
3077
3078 // Store the info to the |ssrc_info|.
3079 if (attribute == kSsrcAttributeCname) {
3080 // RFC 5576
3081 // cname:<value>
3082 ssrc_info->cname = value;
3083 } else if (attribute == kSsrcAttributeMsid) {
3084 // draft-alvestrand-mmusic-msid-00
Seth Hampson5b4f0752018-04-02 16:31:36 -07003085 // msid:identifier [appdata]
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003086 std::vector<std::string> fields;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00003087 rtc::split(value, kSdpDelimiterSpace, &fields);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003088 if (fields.size() < 1 || fields.size() > 2) {
3089 return ParseFailed(line,
3090 "Expected format \"msid:<identifier>[ <appdata>]\".",
3091 error);
3092 }
deadbeef9d3584c2016-02-16 17:54:10 -08003093 ssrc_info->stream_id = fields[0];
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003094 if (fields.size() == 2) {
deadbeef9d3584c2016-02-16 17:54:10 -08003095 ssrc_info->track_id = fields[1];
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003096 }
Steve Antone831b8c2018-02-01 12:22:16 -08003097 *msid_signaling |= cricket::kMsidSignalingSsrcAttribute;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003098 } else if (attribute == kSsrcAttributeMslabel) {
3099 // draft-alvestrand-rtcweb-mid-01
3100 // mslabel:<value>
3101 ssrc_info->mslabel = value;
3102 } else if (attribute == kSSrcAttributeLabel) {
3103 // The label isn't defined.
3104 // label:<value>
3105 ssrc_info->label = value;
3106 }
3107 return true;
3108}
3109
3110bool ParseSsrcGroupAttribute(const std::string& line,
3111 SsrcGroupVec* ssrc_groups,
3112 SdpParseError* error) {
nisseede5da42017-01-12 05:15:36 -08003113 RTC_DCHECK(ssrc_groups != NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003114 // RFC 5576
3115 // a=ssrc-group:<semantics> <ssrc-id> ...
3116 std::vector<std::string> fields;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00003117 rtc::split(line.substr(kLinePrefixLength),
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003118 kSdpDelimiterSpace, &fields);
3119 const size_t expected_min_fields = 2;
3120 if (fields.size() < expected_min_fields) {
3121 return ParseFailedExpectMinFieldNum(line, expected_min_fields, error);
3122 }
3123 std::string semantics;
3124 if (!GetValue(fields[0], kAttributeSsrcGroup, &semantics, error)) {
3125 return false;
3126 }
Peter Boström0c4e06b2015-10-07 12:23:21 +02003127 std::vector<uint32_t> ssrcs;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003128 for (size_t i = 1; i < fields.size(); ++i) {
Peter Boström0c4e06b2015-10-07 12:23:21 +02003129 uint32_t ssrc = 0;
wu@webrtc.org5e760e72014-04-02 23:19:09 +00003130 if (!GetValueFromString(line, fields[i], &ssrc, error)) {
3131 return false;
3132 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003133 ssrcs.push_back(ssrc);
3134 }
3135 ssrc_groups->push_back(SsrcGroup(semantics, ssrcs));
3136 return true;
3137}
3138
3139bool ParseCryptoAttribute(const std::string& line,
3140 MediaContentDescription* media_desc,
3141 SdpParseError* error) {
3142 std::vector<std::string> fields;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00003143 rtc::split(line.substr(kLinePrefixLength),
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003144 kSdpDelimiterSpace, &fields);
3145 // RFC 4568
3146 // a=crypto:<tag> <crypto-suite> <key-params> [<session-params>]
3147 const size_t expected_min_fields = 3;
3148 if (fields.size() < expected_min_fields) {
3149 return ParseFailedExpectMinFieldNum(line, expected_min_fields, error);
3150 }
3151 std::string tag_value;
3152 if (!GetValue(fields[0], kAttributeCrypto, &tag_value, error)) {
3153 return false;
3154 }
wu@webrtc.org5e760e72014-04-02 23:19:09 +00003155 int tag = 0;
3156 if (!GetValueFromString(line, tag_value, &tag, error)) {
3157 return false;
3158 }
jbauch083b73f2015-07-16 02:46:32 -07003159 const std::string& crypto_suite = fields[1];
3160 const std::string& key_params = fields[2];
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003161 std::string session_params;
3162 if (fields.size() > 3) {
3163 session_params = fields[3];
3164 }
3165 media_desc->AddCrypto(CryptoParams(tag, crypto_suite, key_params,
3166 session_params));
3167 return true;
3168}
3169
3170// Updates or creates a new codec entry in the audio description with according
deadbeef67cf2c12016-04-13 10:07:16 -07003171// to |name|, |clockrate|, |bitrate|, and |channels|.
3172void UpdateCodec(int payload_type,
3173 const std::string& name,
3174 int clockrate,
3175 int bitrate,
3176 size_t channels,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003177 AudioContentDescription* audio_desc) {
3178 // Codec may already be populated with (only) optional parameters
3179 // (from an fmtp).
changbin.shao@webrtc.org2d25b442015-03-16 04:14:34 +00003180 cricket::AudioCodec codec =
3181 GetCodecWithPayloadType(audio_desc->codecs(), payload_type);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003182 codec.name = name;
3183 codec.clockrate = clockrate;
3184 codec.bitrate = bitrate;
3185 codec.channels = channels;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003186 AddOrReplaceCodec<AudioContentDescription, cricket::AudioCodec>(audio_desc,
3187 codec);
3188}
3189
3190// Updates or creates a new codec entry in the video description according to
deadbeef67cf2c12016-04-13 10:07:16 -07003191// |name|, |width|, |height|, and |framerate|.
3192void UpdateCodec(int payload_type,
3193 const std::string& name,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003194 VideoContentDescription* video_desc) {
3195 // Codec may already be populated with (only) optional parameters
3196 // (from an fmtp).
changbin.shao@webrtc.org2d25b442015-03-16 04:14:34 +00003197 cricket::VideoCodec codec =
3198 GetCodecWithPayloadType(video_desc->codecs(), payload_type);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003199 codec.name = name;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003200 AddOrReplaceCodec<VideoContentDescription, cricket::VideoCodec>(video_desc,
3201 codec);
3202}
3203
3204bool ParseRtpmapAttribute(const std::string& line,
3205 const MediaType media_type,
deadbeef67cf2c12016-04-13 10:07:16 -07003206 const std::vector<int>& payload_types,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003207 MediaContentDescription* media_desc,
3208 SdpParseError* error) {
3209 std::vector<std::string> fields;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00003210 rtc::split(line.substr(kLinePrefixLength),
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003211 kSdpDelimiterSpace, &fields);
3212 // RFC 4566
3213 // a=rtpmap:<payload type> <encoding name>/<clock rate>[/<encodingparameters>]
3214 const size_t expected_min_fields = 2;
3215 if (fields.size() < expected_min_fields) {
3216 return ParseFailedExpectMinFieldNum(line, expected_min_fields, error);
3217 }
3218 std::string payload_type_value;
3219 if (!GetValue(fields[0], kAttributeRtpmap, &payload_type_value, error)) {
3220 return false;
3221 }
wu@webrtc.org5e760e72014-04-02 23:19:09 +00003222 int payload_type = 0;
pkasting@chromium.orge9facf82015-02-17 20:36:28 +00003223 if (!GetPayloadTypeFromString(line, payload_type_value, &payload_type,
3224 error)) {
wu@webrtc.org5e760e72014-04-02 23:19:09 +00003225 return false;
3226 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003227
deadbeef67cf2c12016-04-13 10:07:16 -07003228 if (std::find(payload_types.begin(), payload_types.end(), payload_type) ==
3229 payload_types.end()) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01003230 RTC_LOG(LS_WARNING) << "Ignore rtpmap line that did not appear in the "
Jonas Olsson45cc8902018-02-13 10:37:07 +01003231 "<fmt> of the m-line: "
3232 << line;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003233 return true;
3234 }
jbauch083b73f2015-07-16 02:46:32 -07003235 const std::string& encoder = fields[1];
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003236 std::vector<std::string> codec_params;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00003237 rtc::split(encoder, '/', &codec_params);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003238 // <encoding name>/<clock rate>[/<encodingparameters>]
3239 // 2 mandatory fields
3240 if (codec_params.size() < 2 || codec_params.size() > 3) {
3241 return ParseFailed(line,
3242 "Expected format \"<encoding name>/<clock rate>"
3243 "[/<encodingparameters>]\".",
3244 error);
3245 }
jbauch083b73f2015-07-16 02:46:32 -07003246 const std::string& encoding_name = codec_params[0];
wu@webrtc.org5e760e72014-04-02 23:19:09 +00003247 int clock_rate = 0;
3248 if (!GetValueFromString(line, codec_params[1], &clock_rate, error)) {
3249 return false;
3250 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003251 if (media_type == cricket::MEDIA_TYPE_VIDEO) {
Steve Antonb1c1de12017-12-21 15:14:30 -08003252 VideoContentDescription* video_desc = media_desc->as_video();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003253 UpdateCodec(payload_type, encoding_name,
deadbeef67cf2c12016-04-13 10:07:16 -07003254 video_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003255 } else if (media_type == cricket::MEDIA_TYPE_AUDIO) {
3256 // RFC 4566
3257 // For audio streams, <encoding parameters> indicates the number
3258 // of audio channels. This parameter is OPTIONAL and may be
3259 // omitted if the number of channels is one, provided that no
3260 // additional parameters are needed.
Peter Kasting69558702016-01-12 16:26:35 -08003261 size_t channels = 1;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003262 if (codec_params.size() == 3) {
wu@webrtc.org5e760e72014-04-02 23:19:09 +00003263 if (!GetValueFromString(line, codec_params[2], &channels, error)) {
3264 return false;
3265 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003266 }
Steve Antonb1c1de12017-12-21 15:14:30 -08003267 AudioContentDescription* audio_desc = media_desc->as_audio();
ossue1405ad2017-01-23 08:55:48 -08003268 UpdateCodec(payload_type, encoding_name, clock_rate, 0, channels,
deadbeef67cf2c12016-04-13 10:07:16 -07003269 audio_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003270 } else if (media_type == cricket::MEDIA_TYPE_DATA) {
Steve Antonb1c1de12017-12-21 15:14:30 -08003271 DataContentDescription* data_desc = media_desc->as_data();
deadbeef67cf2c12016-04-13 10:07:16 -07003272 data_desc->AddCodec(cricket::DataCodec(payload_type, encoding_name));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003273 }
3274 return true;
3275}
3276
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003277bool ParseFmtpParam(const std::string& line, std::string* parameter,
3278 std::string* value, SdpParseError* error) {
Donald Curtis0e07f922015-05-15 09:21:23 -07003279 if (!rtc::tokenize_first(line, kSdpDelimiterEqual, parameter, value)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003280 ParseFailed(line, "Unable to parse fmtp parameter. \'=\' missing.", error);
3281 return false;
3282 }
3283 // a=fmtp:<payload_type> <param1>=<value1>; <param2>=<value2>; ...
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003284 return true;
3285}
3286
3287bool ParseFmtpAttributes(const std::string& line, const MediaType media_type,
3288 MediaContentDescription* media_desc,
3289 SdpParseError* error) {
3290 if (media_type != cricket::MEDIA_TYPE_AUDIO &&
3291 media_type != cricket::MEDIA_TYPE_VIDEO) {
3292 return true;
3293 }
Donald Curtis0e07f922015-05-15 09:21:23 -07003294
3295 std::string line_payload;
3296 std::string line_params;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003297
3298 // RFC 5576
3299 // a=fmtp:<format> <format specific parameters>
3300 // At least two fields, whereas the second one is any of the optional
3301 // parameters.
Donald Curtis144d0182015-05-15 13:14:24 -07003302 if (!rtc::tokenize_first(line.substr(kLinePrefixLength), kSdpDelimiterSpace,
3303 &line_payload, &line_params)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003304 ParseFailedExpectMinFieldNum(line, 2, error);
3305 return false;
3306 }
3307
Donald Curtis0e07f922015-05-15 09:21:23 -07003308 // Parse out the payload information.
pkasting@chromium.orgd3245462015-02-23 21:28:22 +00003309 std::string payload_type_str;
Donald Curtis0e07f922015-05-15 09:21:23 -07003310 if (!GetValue(line_payload, kAttributeFmtp, &payload_type_str, error)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003311 return false;
3312 }
3313
Donald Curtis0e07f922015-05-15 09:21:23 -07003314 int payload_type = 0;
Donald Curtis144d0182015-05-15 13:14:24 -07003315 if (!GetPayloadTypeFromString(line_payload, payload_type_str, &payload_type,
3316 error)) {
Donald Curtis0e07f922015-05-15 09:21:23 -07003317 return false;
3318 }
3319
3320 // Parse out format specific parameters.
3321 std::vector<std::string> fields;
3322 rtc::split(line_params, kSdpDelimiterSemicolon, &fields);
3323
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003324 cricket::CodecParameterMap codec_params;
Donald Curtis144d0182015-05-15 13:14:24 -07003325 for (auto& iter : fields) {
Donald Curtis0e07f922015-05-15 09:21:23 -07003326 if (iter.find(kSdpDelimiterEqual) == std::string::npos) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003327 // Only fmtps with equals are currently supported. Other fmtp types
3328 // should be ignored. Unknown fmtps do not constitute an error.
3329 continue;
3330 }
Donald Curtis0e07f922015-05-15 09:21:23 -07003331
3332 std::string name;
3333 std::string value;
3334 if (!ParseFmtpParam(rtc::string_trim(iter), &name, &value, error)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003335 return false;
3336 }
3337 codec_params[name] = value;
3338 }
3339
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003340 if (media_type == cricket::MEDIA_TYPE_AUDIO) {
3341 UpdateCodec<AudioContentDescription, cricket::AudioCodec>(
pkasting@chromium.orgd3245462015-02-23 21:28:22 +00003342 media_desc, payload_type, codec_params);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003343 } else if (media_type == cricket::MEDIA_TYPE_VIDEO) {
3344 UpdateCodec<VideoContentDescription, cricket::VideoCodec>(
pkasting@chromium.orgd3245462015-02-23 21:28:22 +00003345 media_desc, payload_type, codec_params);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003346 }
3347 return true;
3348}
3349
3350bool ParseRtcpFbAttribute(const std::string& line, const MediaType media_type,
3351 MediaContentDescription* media_desc,
3352 SdpParseError* error) {
3353 if (media_type != cricket::MEDIA_TYPE_AUDIO &&
3354 media_type != cricket::MEDIA_TYPE_VIDEO) {
3355 return true;
3356 }
3357 std::vector<std::string> rtcp_fb_fields;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00003358 rtc::split(line.c_str(), kSdpDelimiterSpace, &rtcp_fb_fields);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003359 if (rtcp_fb_fields.size() < 2) {
3360 return ParseFailedGetValue(line, kAttributeRtcpFb, error);
3361 }
3362 std::string payload_type_string;
3363 if (!GetValue(rtcp_fb_fields[0], kAttributeRtcpFb, &payload_type_string,
3364 error)) {
3365 return false;
3366 }
wu@webrtc.org5e760e72014-04-02 23:19:09 +00003367 int payload_type = kWildcardPayloadType;
3368 if (payload_type_string != "*") {
pkasting@chromium.orge9facf82015-02-17 20:36:28 +00003369 if (!GetPayloadTypeFromString(line, payload_type_string, &payload_type,
3370 error)) {
wu@webrtc.org5e760e72014-04-02 23:19:09 +00003371 return false;
3372 }
3373 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003374 std::string id = rtcp_fb_fields[1];
3375 std::string param = "";
3376 for (std::vector<std::string>::iterator iter = rtcp_fb_fields.begin() + 2;
3377 iter != rtcp_fb_fields.end(); ++iter) {
3378 param.append(*iter);
3379 }
3380 const cricket::FeedbackParam feedback_param(id, param);
3381
3382 if (media_type == cricket::MEDIA_TYPE_AUDIO) {
pkasting@chromium.orgd3245462015-02-23 21:28:22 +00003383 UpdateCodec<AudioContentDescription, cricket::AudioCodec>(
3384 media_desc, payload_type, feedback_param);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003385 } else if (media_type == cricket::MEDIA_TYPE_VIDEO) {
pkasting@chromium.orgd3245462015-02-23 21:28:22 +00003386 UpdateCodec<VideoContentDescription, cricket::VideoCodec>(
3387 media_desc, payload_type, feedback_param);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003388 }
3389 return true;
3390}
3391
3392} // namespace webrtc