blob: d068470086a79f8f14bc52d2b0cae097bd430715 [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);
Yves Gerey665174f2018-06-19 15:03:05 +0200309static bool ParseFmtpParam(const std::string& line,
310 std::string* parameter,
311 std::string* value,
312 SdpParseError* error);
313static bool ParseCandidate(const std::string& message,
314 Candidate* candidate,
315 SdpParseError* error,
316 bool is_raw);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000317static bool ParseRtcpFbAttribute(const std::string& line,
318 const MediaType media_type,
319 MediaContentDescription* media_desc,
320 SdpParseError* error);
321static bool ParseIceOptions(const std::string& line,
322 std::vector<std::string>* transport_options,
323 SdpParseError* error);
324static bool ParseExtmap(const std::string& line,
isheriff6f8d6862016-05-26 11:24:55 -0700325 RtpExtension* extmap,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000326 SdpParseError* error);
327static bool ParseFingerprintAttribute(const std::string& line,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000328 rtc::SSLFingerprint** fingerprint,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000329 SdpParseError* error);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +0000330static bool ParseDtlsSetup(const std::string& line,
331 cricket::ConnectionRole* role,
332 SdpParseError* error);
deadbeef9d3584c2016-02-16 17:54:10 -0800333static bool ParseMsidAttribute(const std::string& line,
Seth Hampson5b4f0752018-04-02 16:31:36 -0700334 std::vector<std::string>* stream_ids,
deadbeef9d3584c2016-02-16 17:54:10 -0800335 std::string* track_id,
336 SdpParseError* error);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000337
338// Helper functions
339
340// Below ParseFailed*** functions output the line that caused the parsing
341// failure and the detailed reason (|description|) of the failure to |error|.
342// The functions always return false so that they can be used directly in the
343// following way when error happens:
344// "return ParseFailed***(...);"
345
346// The line starting at |line_start| of |message| is the failing line.
347// The reason for the failure should be provided in the |description|.
348// An example of a description could be "unknown character".
349static bool ParseFailed(const std::string& message,
350 size_t line_start,
351 const std::string& description,
352 SdpParseError* error) {
353 // Get the first line of |message| from |line_start|.
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +0000354 std::string first_line;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000355 size_t line_end = message.find(kNewLine, line_start);
356 if (line_end != std::string::npos) {
357 if (line_end > 0 && (message.at(line_end - 1) == kReturn)) {
358 --line_end;
359 }
360 first_line = message.substr(line_start, (line_end - line_start));
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +0000361 } else {
362 first_line = message.substr(line_start);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000363 }
364
365 if (error) {
366 error->line = first_line;
367 error->description = description;
368 }
Mirko Bonadei675513b2017-11-09 11:09:25 +0100369 RTC_LOG(LS_ERROR) << "Failed to parse: \"" << first_line
370 << "\". Reason: " << description;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000371 return false;
372}
373
374// |line| is the failing line. The reason for the failure should be
375// provided in the |description|.
376static bool ParseFailed(const std::string& line,
377 const std::string& description,
378 SdpParseError* error) {
379 return ParseFailed(line, 0, description, error);
380}
381
382// Parses failure where the failing SDP line isn't know or there are multiple
383// failing lines.
Yves Gerey665174f2018-06-19 15:03:05 +0200384static bool ParseFailed(const std::string& description, SdpParseError* error) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000385 return ParseFailed("", description, error);
386}
387
388// |line| is the failing line. The failure is due to the fact that |line|
389// doesn't have |expected_fields| fields.
390static bool ParseFailedExpectFieldNum(const std::string& line,
391 int expected_fields,
392 SdpParseError* error) {
393 std::ostringstream description;
394 description << "Expects " << expected_fields << " fields.";
395 return ParseFailed(line, description.str(), error);
396}
397
398// |line| is the failing line. The failure is due to the fact that |line| has
399// less than |expected_min_fields| fields.
400static bool ParseFailedExpectMinFieldNum(const std::string& line,
401 int expected_min_fields,
402 SdpParseError* error) {
403 std::ostringstream description;
404 description << "Expects at least " << expected_min_fields << " fields.";
405 return ParseFailed(line, description.str(), error);
406}
407
408// |line| is the failing line. The failure is due to the fact that it failed to
409// get the value of |attribute|.
410static bool ParseFailedGetValue(const std::string& line,
411 const std::string& attribute,
412 SdpParseError* error) {
413 std::ostringstream description;
414 description << "Failed to get the value of attribute: " << attribute;
415 return ParseFailed(line, description.str(), error);
416}
417
418// The line starting at |line_start| of |message| is the failing line. The
419// failure is due to the line type (e.g. the "m" part of the "m-line")
420// not matching what is expected. The expected line type should be
421// provided as |line_type|.
422static bool ParseFailedExpectLine(const std::string& message,
423 size_t line_start,
424 const char line_type,
425 const std::string& line_value,
426 SdpParseError* error) {
427 std::ostringstream description;
428 description << "Expect line: " << line_type << "=" << line_value;
429 return ParseFailed(message, line_start, description.str(), error);
430}
431
432static bool AddLine(const std::string& line, std::string* message) {
433 if (!message)
434 return false;
435
436 message->append(line);
437 message->append(kLineBreak);
438 return true;
439}
440
441static bool GetLine(const std::string& message,
442 size_t* pos,
443 std::string* line) {
444 size_t line_begin = *pos;
445 size_t line_end = message.find(kNewLine, line_begin);
446 if (line_end == std::string::npos) {
447 return false;
448 }
449 // Update the new start position
450 *pos = line_end + 1;
451 if (line_end > 0 && (message.at(line_end - 1) == kReturn)) {
452 --line_end;
453 }
454 *line = message.substr(line_begin, (line_end - line_begin));
455 const char* cline = line->c_str();
456 // RFC 4566
457 // An SDP session description consists of a number of lines of text of
458 // the form:
459 // <type>=<value>
460 // where <type> MUST be exactly one case-significant character and
461 // <value> is structured text whose format depends on <type>.
462 // Whitespace MUST NOT be used on either side of the "=" sign.
Taylor Brandstetter93a7b242018-04-16 10:45:24 -0700463 //
464 // However, an exception to the whitespace rule is made for "s=", since
465 // RFC4566 also says:
466 //
467 // If a session has no meaningful name, the value "s= " SHOULD be used
468 // (i.e., a single space as the session name).
469 if (line->length() < 3 || !islower(cline[0]) ||
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000470 cline[1] != kSdpDelimiterEqual ||
Taylor Brandstetter93a7b242018-04-16 10:45:24 -0700471 (cline[0] != kLineTypeSessionName && cline[2] == kSdpDelimiterSpace)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000472 *pos = line_begin;
473 return false;
474 }
475 return true;
476}
477
478// Init |os| to "|type|=|value|".
479static void InitLine(const char type,
480 const std::string& value,
481 std::ostringstream* os) {
482 os->str("");
483 *os << type << kSdpDelimiterEqual << value;
484}
485
486// Init |os| to "a=|attribute|".
487static void InitAttrLine(const std::string& attribute, std::ostringstream* os) {
488 InitLine(kLineTypeAttributes, attribute, os);
489}
490
491// Writes a SDP attribute line based on |attribute| and |value| to |message|.
Yves Gerey665174f2018-06-19 15:03:05 +0200492static void AddAttributeLine(const std::string& attribute,
493 int value,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000494 std::string* message) {
495 std::ostringstream os;
496 InitAttrLine(attribute, &os);
497 os << kSdpDelimiterColon << value;
498 AddLine(os.str(), message);
499}
500
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000501static bool IsLineType(const std::string& message,
502 const char type,
503 size_t line_start) {
504 if (message.size() < line_start + kLinePrefixLength) {
505 return false;
506 }
507 const char* cmessage = message.c_str();
508 return (cmessage[line_start] == type &&
509 cmessage[line_start + 1] == kSdpDelimiterEqual);
510}
511
Yves Gerey665174f2018-06-19 15:03:05 +0200512static bool IsLineType(const std::string& line, const char type) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000513 return IsLineType(line, type, 0);
514}
515
Yves Gerey665174f2018-06-19 15:03:05 +0200516static bool GetLineWithType(const std::string& message,
517 size_t* pos,
518 std::string* line,
519 const char type) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000520 if (!IsLineType(message, type, *pos)) {
521 return false;
522 }
523
524 if (!GetLine(message, pos, line))
525 return false;
526
527 return true;
528}
529
530static bool HasAttribute(const std::string& line,
531 const std::string& attribute) {
532 return (line.compare(kLinePrefixLength, attribute.size(), attribute) == 0);
533}
534
Peter Boström0c4e06b2015-10-07 12:23:21 +0200535static bool AddSsrcLine(uint32_t ssrc_id,
536 const std::string& attribute,
537 const std::string& value,
538 std::string* message) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000539 // RFC 5576
540 // a=ssrc:<ssrc-id> <attribute>:<value>
541 std::ostringstream os;
542 InitAttrLine(kAttributeSsrc, &os);
Yves Gerey665174f2018-06-19 15:03:05 +0200543 os << kSdpDelimiterColon << ssrc_id << kSdpDelimiterSpace << attribute
544 << kSdpDelimiterColon << value;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000545 return AddLine(os.str(), message);
546}
547
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000548// Get value only from <attribute>:<value>.
Yves Gerey665174f2018-06-19 15:03:05 +0200549static bool GetValue(const std::string& message,
550 const std::string& attribute,
551 std::string* value,
552 SdpParseError* error) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000553 std::string leftpart;
Donald Curtis0e07f922015-05-15 09:21:23 -0700554 if (!rtc::tokenize_first(message, kSdpDelimiterColon, &leftpart, value)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000555 return ParseFailedGetValue(message, attribute, error);
556 }
557 // The left part should end with the expected attribute.
558 if (leftpart.length() < attribute.length() ||
559 leftpart.compare(leftpart.length() - attribute.length(),
560 attribute.length(), attribute) != 0) {
561 return ParseFailedGetValue(message, attribute, error);
562 }
563 return true;
564}
565
566static bool CaseInsensitiveFind(std::string str1, std::string str2) {
Yves Gerey665174f2018-06-19 15:03:05 +0200567 std::transform(str1.begin(), str1.end(), str1.begin(), ::tolower);
568 std::transform(str2.begin(), str2.end(), str2.begin(), ::tolower);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000569 return str1.find(str2) != std::string::npos;
570}
571
wu@webrtc.org5e760e72014-04-02 23:19:09 +0000572template <class T>
573static bool GetValueFromString(const std::string& line,
574 const std::string& s,
575 T* t,
576 SdpParseError* error) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000577 if (!rtc::FromString(s, t)) {
wu@webrtc.org5e760e72014-04-02 23:19:09 +0000578 std::ostringstream description;
579 description << "Invalid value: " << s << ".";
580 return ParseFailed(line, description.str(), error);
581 }
582 return true;
583}
584
pkasting@chromium.orge9facf82015-02-17 20:36:28 +0000585static bool GetPayloadTypeFromString(const std::string& line,
586 const std::string& s,
587 int* payload_type,
588 SdpParseError* error) {
589 return GetValueFromString(line, s, payload_type, error) &&
Yves Gerey665174f2018-06-19 15:03:05 +0200590 cricket::IsValidRtpPayloadType(*payload_type);
pkasting@chromium.orge9facf82015-02-17 20:36:28 +0000591}
592
Seth Hampson5897a6e2018-04-03 11:16:33 -0700593// Creates a StreamParams track in the case when no SSRC lines are signaled.
594// This is a track that does not contain SSRCs and only contains
595// stream_ids/track_id if it's signaled with a=msid lines.
596void CreateTrackWithNoSsrcs(const std::vector<std::string>& msid_stream_ids,
597 const std::string& msid_track_id,
598 StreamParamsVec* tracks) {
599 StreamParams track;
600 if (msid_track_id.empty() || msid_stream_ids.empty()) {
601 // We only create an unsignaled track if a=msid lines were signaled.
602 return;
603 }
604 track.set_stream_ids(msid_stream_ids);
605 track.id = msid_track_id;
606 tracks->push_back(track);
607}
608
609// Creates the StreamParams tracks, for the case when SSRC lines are signaled.
610// |msid_stream_ids| and |msid_track_id| represent the stream/track ID from the
Taylor Brandstetter5de6b752016-03-09 17:02:30 -0800611// "a=msid" attribute, if it exists. They are empty if the attribute does not
Seth Hampson5897a6e2018-04-03 11:16:33 -0700612// exist. We prioritize getting stream_ids/track_ids signaled in a=msid lines.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000613void CreateTracksFromSsrcInfos(const SsrcInfoVec& ssrc_infos,
Seth Hampson5b4f0752018-04-02 16:31:36 -0700614 const std::vector<std::string>& msid_stream_ids,
Taylor Brandstetter5de6b752016-03-09 17:02:30 -0800615 const std::string& msid_track_id,
Seth Hampson5b4f0752018-04-02 16:31:36 -0700616 StreamParamsVec* tracks,
617 int msid_signaling) {
nisseede5da42017-01-12 05:15:36 -0800618 RTC_DCHECK(tracks != NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000619 for (SsrcInfoVec::const_iterator ssrc_info = ssrc_infos.begin();
620 ssrc_info != ssrc_infos.end(); ++ssrc_info) {
621 if (ssrc_info->cname.empty()) {
622 continue;
623 }
624
Seth Hampson5b4f0752018-04-02 16:31:36 -0700625 std::vector<std::string> stream_ids;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000626 std::string track_id;
Seth Hampson5b4f0752018-04-02 16:31:36 -0700627 if (msid_signaling & cricket::kMsidSignalingMediaSection) {
628 // This is the case with Unified Plan SDP msid signaling.
629 stream_ids = msid_stream_ids;
Tomas Gunnarsson191bf5c2018-03-30 10:44:43 +0000630 track_id = msid_track_id;
Seth Hampson5b4f0752018-04-02 16:31:36 -0700631 } else if (msid_signaling & cricket::kMsidSignalingSsrcAttribute) {
632 // This is the case with Plan B SDP msid signaling.
633 stream_ids.push_back(ssrc_info->stream_id);
Tomas Gunnarsson191bf5c2018-03-30 10:44:43 +0000634 track_id = ssrc_info->track_id;
Seth Hampson5b4f0752018-04-02 16:31:36 -0700635 } else if (!ssrc_info->mslabel.empty()) {
636 // Since there's no a=msid or a=ssrc msid signaling, this is a sdp from
637 // an older version of client that doesn't support msid.
638 // In that case, we use the mslabel and label to construct the track.
639 stream_ids.push_back(ssrc_info->mslabel);
640 track_id = ssrc_info->label;
641 } else {
642 // Since no media streams isn't supported with older SDP signaling, we
643 // use a default a stream id.
644 stream_ids.push_back(kDefaultMsid);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000645 }
Seth Hampson5b4f0752018-04-02 16:31:36 -0700646 // If a track ID wasn't populated from the SSRC attributes OR the
Taylor Brandstetter5de6b752016-03-09 17:02:30 -0800647 // msid attribute, use default/random values.
Taylor Brandstetter5de6b752016-03-09 17:02:30 -0800648 if (track_id.empty()) {
649 // TODO(ronghuawu): What should we do if the track id doesn't appear?
650 // Create random string (which will be used as track label later)?
651 track_id = rtc::CreateRandomString(8);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000652 }
653
654 StreamParamsVec::iterator track = tracks->begin();
655 for (; track != tracks->end(); ++track) {
656 if (track->id == track_id) {
657 break;
658 }
659 }
660 if (track == tracks->end()) {
661 // If we don't find an existing track, create a new one.
662 tracks->push_back(StreamParams());
663 track = tracks->end() - 1;
664 }
665 track->add_ssrc(ssrc_info->ssrc_id);
666 track->cname = ssrc_info->cname;
Seth Hampson5b4f0752018-04-02 16:31:36 -0700667 track->set_stream_ids(stream_ids);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000668 track->id = track_id;
669 }
670}
671
Seth Hampson845e8782018-03-02 11:34:10 -0800672void GetMediaStreamIds(const ContentInfo* content,
673 std::set<std::string>* labels) {
Steve Antonb1c1de12017-12-21 15:14:30 -0800674 for (const StreamParams& stream_params :
675 content->media_description()->streams()) {
Seth Hampson845e8782018-03-02 11:34:10 -0800676 for (const std::string& stream_id : stream_params.stream_ids()) {
677 labels->insert(stream_id);
Steve Anton5a26a3a2018-02-28 11:38:47 -0800678 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000679 }
680}
681
682// RFC 5245
683// It is RECOMMENDED that default candidates be chosen based on the
684// likelihood of those candidates to work with the peer that is being
685// contacted. It is RECOMMENDED that relayed > reflexive > host.
686static const int kPreferenceUnknown = 0;
687static const int kPreferenceHost = 1;
688static const int kPreferenceReflexive = 2;
689static const int kPreferenceRelayed = 3;
690
691static int GetCandidatePreferenceFromType(const std::string& type) {
692 int preference = kPreferenceUnknown;
693 if (type == cricket::LOCAL_PORT_TYPE) {
694 preference = kPreferenceHost;
695 } else if (type == cricket::STUN_PORT_TYPE) {
696 preference = kPreferenceReflexive;
697 } else if (type == cricket::RELAY_PORT_TYPE) {
698 preference = kPreferenceRelayed;
699 } else {
nissec80e7412017-01-11 05:56:46 -0800700 RTC_NOTREACHED();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000701 }
702 return preference;
703}
704
guoweis@webrtc.org57ac2c82015-02-06 00:45:13 +0000705// Get ip and port of the default destination from the |candidates| with the
706// given value of |component_id|. The default candidate should be the one most
707// likely to work, typically IPv4 relay.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000708// RFC 5245
709// The value of |component_id| currently supported are 1 (RTP) and 2 (RTCP).
Steve Anton36b29d12017-10-30 09:57:42 -0700710// TODO(deadbeef): Decide the default destination in webrtcsession and
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000711// pass it down via SessionDescription.
Yves Gerey665174f2018-06-19 15:03:05 +0200712static void GetDefaultDestination(const std::vector<Candidate>& candidates,
713 int component_id,
714 std::string* port,
715 std::string* ip,
716 std::string* addr_type) {
perkj@webrtc.orgd105cc82014-11-07 11:22:06 +0000717 *addr_type = kConnectionIpv4Addrtype;
pthatcher@webrtc.orgc9d6d142014-10-23 23:37:22 +0000718 *port = kDummyPort;
719 *ip = kDummyAddress;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000720 int current_preference = kPreferenceUnknown;
guoweis@webrtc.org57ac2c82015-02-06 00:45:13 +0000721 int current_family = AF_UNSPEC;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000722 for (std::vector<Candidate>::const_iterator it = candidates.begin();
723 it != candidates.end(); ++it) {
724 if (it->component() != component_id) {
725 continue;
726 }
guoweis@webrtc.org57ac2c82015-02-06 00:45:13 +0000727 // Default destination should be UDP only.
728 if (it->protocol() != cricket::UDP_PROTOCOL_NAME) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000729 continue;
730 }
guoweis@webrtc.org57ac2c82015-02-06 00:45:13 +0000731 const int preference = GetCandidatePreferenceFromType(it->type());
732 const int family = it->address().ipaddr().family();
733 // See if this candidate is more preferable then the current one if it's the
734 // same family. Or if the current family is IPv4 already so we could safely
735 // ignore all IPv6 ones. WebRTC bug 4269.
736 // http://code.google.com/p/webrtc/issues/detail?id=4269
737 if ((preference <= current_preference && current_family == family) ||
738 (current_family == AF_INET && family == AF_INET6)) {
739 continue;
740 }
pthatcher@webrtc.orgc9d6d142014-10-23 23:37:22 +0000741 if (family == AF_INET) {
742 addr_type->assign(kConnectionIpv4Addrtype);
743 } else if (family == AF_INET6) {
744 addr_type->assign(kConnectionIpv6Addrtype);
745 }
guoweis@webrtc.org57ac2c82015-02-06 00:45:13 +0000746 current_preference = preference;
747 current_family = family;
748 *port = it->address().PortAsString();
749 *ip = it->address().ipaddr().ToString();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000750 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000751}
752
wu@webrtc.org4c3e9912014-07-16 21:03:13 +0000753// Gets "a=rtcp" line if found default RTCP candidate from |candidates|.
754static std::string GetRtcpLine(const std::vector<Candidate>& candidates) {
pthatcher@webrtc.orgc9d6d142014-10-23 23:37:22 +0000755 std::string rtcp_line, rtcp_port, rtcp_ip, addr_type;
Yves Gerey665174f2018-06-19 15:03:05 +0200756 GetDefaultDestination(candidates, ICE_CANDIDATE_COMPONENT_RTCP, &rtcp_port,
757 &rtcp_ip, &addr_type);
pthatcher@webrtc.orgc9d6d142014-10-23 23:37:22 +0000758 // Found default RTCP candidate.
759 // RFC 5245
760 // If the agent is utilizing RTCP, it MUST encode the RTCP candidate
761 // using the a=rtcp attribute as defined in RFC 3605.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000762
pthatcher@webrtc.orgc9d6d142014-10-23 23:37:22 +0000763 // RFC 3605
764 // rtcp-attribute = "a=rtcp:" port [nettype space addrtype space
765 // connection-address] CRLF
766 std::ostringstream os;
767 InitAttrLine(kAttributeRtcp, &os);
Yves Gerey665174f2018-06-19 15:03:05 +0200768 os << kSdpDelimiterColon << rtcp_port << " " << kConnectionNettype << " "
769 << addr_type << " " << rtcp_ip;
pthatcher@webrtc.orgc9d6d142014-10-23 23:37:22 +0000770 rtcp_line = os.str();
wu@webrtc.org4c3e9912014-07-16 21:03:13 +0000771 return rtcp_line;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000772}
773
774// Get candidates according to the mline index from SessionDescriptionInterface.
775static void GetCandidatesByMindex(const SessionDescriptionInterface& desci,
776 int mline_index,
777 std::vector<Candidate>* candidates) {
778 if (!candidates) {
779 return;
780 }
781 const IceCandidateCollection* cc = desci.candidates(mline_index);
782 for (size_t i = 0; i < cc->count(); ++i) {
783 const IceCandidateInterface* candidate = cc->at(i);
784 candidates->push_back(candidate->candidate());
785 }
786}
787
deadbeef90f1e1e2017-02-10 12:35:05 -0800788static bool IsValidPort(int port) {
789 return port >= 0 && port <= 65535;
790}
791
Steve Antone831b8c2018-02-01 12:22:16 -0800792std::string SdpSerialize(const JsepSessionDescription& jdesc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000793 const cricket::SessionDescription* desc = jdesc.description();
794 if (!desc) {
795 return "";
796 }
797
798 std::string message;
799
800 // Session Description.
801 AddLine(kSessionVersion, &message);
802 // Session Origin
803 // RFC 4566
804 // o=<username> <sess-id> <sess-version> <nettype> <addrtype>
805 // <unicast-address>
806 std::ostringstream os;
807 InitLine(kLineTypeOrigin, kSessionOriginUsername, &os);
Yves Gerey665174f2018-06-19 15:03:05 +0200808 const std::string& session_id =
809 jdesc.session_id().empty() ? kSessionOriginSessionId : jdesc.session_id();
810 const std::string& session_version = jdesc.session_version().empty()
811 ? kSessionOriginSessionVersion
812 : jdesc.session_version();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000813 os << " " << session_id << " " << session_version << " "
814 << kSessionOriginNettype << " " << kSessionOriginAddrtype << " "
815 << kSessionOriginAddress;
816 AddLine(os.str(), &message);
817 AddLine(kSessionName, &message);
818
819 // Time Description.
820 AddLine(kTimeDescription, &message);
821
822 // Group
823 if (desc->HasGroup(cricket::GROUP_TYPE_BUNDLE)) {
824 std::string group_line = kAttrGroup;
825 const cricket::ContentGroup* group =
826 desc->GetGroupByName(cricket::GROUP_TYPE_BUNDLE);
nisseede5da42017-01-12 05:15:36 -0800827 RTC_DCHECK(group != NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000828 const cricket::ContentNames& content_names = group->content_names();
829 for (cricket::ContentNames::const_iterator it = content_names.begin();
830 it != content_names.end(); ++it) {
831 group_line.append(" ");
832 group_line.append(*it);
833 }
834 AddLine(group_line, &message);
835 }
836
837 // MediaStream semantics
838 InitAttrLine(kAttributeMsidSemantics, &os);
839 os << kSdpDelimiterColon << " " << kMediaStreamSemantic;
wu@webrtc.org4c3e9912014-07-16 21:03:13 +0000840
Seth Hampson845e8782018-03-02 11:34:10 -0800841 std::set<std::string> media_stream_ids;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000842 const ContentInfo* audio_content = GetFirstAudioContent(desc);
843 if (audio_content)
Seth Hampson845e8782018-03-02 11:34:10 -0800844 GetMediaStreamIds(audio_content, &media_stream_ids);
wu@webrtc.org4c3e9912014-07-16 21:03:13 +0000845
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000846 const ContentInfo* video_content = GetFirstVideoContent(desc);
847 if (video_content)
Seth Hampson845e8782018-03-02 11:34:10 -0800848 GetMediaStreamIds(video_content, &media_stream_ids);
wu@webrtc.org4c3e9912014-07-16 21:03:13 +0000849
Seth Hampson845e8782018-03-02 11:34:10 -0800850 for (std::set<std::string>::const_iterator it = media_stream_ids.begin();
851 it != media_stream_ids.end(); ++it) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000852 os << " " << *it;
853 }
854 AddLine(os.str(), &message);
855
Taylor Brandstetter2f65ec52018-05-24 11:37:28 -0700856 // a=ice-lite
857 //
858 // TODO(deadbeef): It's weird that we need to iterate TransportInfos for
859 // this, when it's a session-level attribute. It really should be moved to a
860 // session-level structure like SessionDescription.
861 for (const cricket::TransportInfo& transport : desc->transport_infos()) {
862 if (transport.description.ice_mode == cricket::ICEMODE_LITE) {
863 InitAttrLine(kAttributeIceLite, &os);
864 AddLine(os.str(), &message);
865 break;
866 }
867 }
868
wu@webrtc.org4c3e9912014-07-16 21:03:13 +0000869 // Preserve the order of the media contents.
870 int mline_index = -1;
871 for (cricket::ContentInfos::const_iterator it = desc->contents().begin();
872 it != desc->contents().end(); ++it) {
Steve Antonb1c1de12017-12-21 15:14:30 -0800873 const MediaContentDescription* mdesc = it->media_description();
wu@webrtc.org4c3e9912014-07-16 21:03:13 +0000874 std::vector<Candidate> candidates;
875 GetCandidatesByMindex(jdesc, ++mline_index, &candidates);
deadbeef9d3584c2016-02-16 17:54:10 -0800876 BuildMediaDescription(&*it, desc->GetTransportInfoByName(it->name),
Steve Antone831b8c2018-02-01 12:22:16 -0800877 mdesc->type(), candidates, desc->msid_signaling(),
wu@webrtc.org4c3e9912014-07-16 21:03:13 +0000878 &message);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000879 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000880 return message;
881}
882
883// Serializes the passed in IceCandidateInterface to a SDP string.
884// candidate - The candidate to be serialized.
Honghai Zhang7fb69db2016-03-14 11:59:18 -0700885std::string SdpSerializeCandidate(const IceCandidateInterface& candidate) {
886 return SdpSerializeCandidate(candidate.candidate());
887}
888
889// Serializes a cricket Candidate.
890std::string SdpSerializeCandidate(const cricket::Candidate& candidate) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000891 std::string message;
Honghai Zhang7fb69db2016-03-14 11:59:18 -0700892 std::vector<cricket::Candidate> candidates(1, candidate);
honghaiza54a0802015-12-16 18:37:23 -0800893 BuildCandidate(candidates, true, &message);
wu@webrtc.orgec9f5fb2014-06-24 17:05:10 +0000894 // From WebRTC draft section 4.8.1.1 candidate-attribute will be
895 // just candidate:<candidate> not a=candidate:<blah>CRLF
nisseede5da42017-01-12 05:15:36 -0800896 RTC_DCHECK(message.find("a=") == 0);
wu@webrtc.orgec9f5fb2014-06-24 17:05:10 +0000897 message.erase(0, 2);
nisseede5da42017-01-12 05:15:36 -0800898 RTC_DCHECK(message.find(kLineBreak) == message.size() - 2);
wu@webrtc.orgec9f5fb2014-06-24 17:05:10 +0000899 message.resize(message.size() - 2);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000900 return message;
901}
902
903bool SdpDeserialize(const std::string& message,
904 JsepSessionDescription* jdesc,
905 SdpParseError* error) {
906 std::string session_id;
907 std::string session_version;
Peter Thatcher7cbd1882015-09-17 18:54:52 -0700908 TransportDescription session_td("", "");
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000909 RtpHeaderExtensions session_extmaps;
zhihuang38989e52017-03-21 11:04:53 -0700910 rtc::SocketAddress session_connection_addr;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000911 cricket::SessionDescription* desc = new cricket::SessionDescription();
912 std::vector<JsepIceCandidate*> candidates;
913 size_t current_pos = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000914
915 // Session Description
916 if (!ParseSessionDescription(message, &current_pos, &session_id,
deadbeefc80741f2015-10-22 13:14:45 -0700917 &session_version, &session_td, &session_extmaps,
zhihuang38989e52017-03-21 11:04:53 -0700918 &session_connection_addr, desc, error)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000919 delete desc;
920 return false;
921 }
922
923 // Media Description
deadbeefc80741f2015-10-22 13:14:45 -0700924 if (!ParseMediaDescription(message, session_td, session_extmaps, &current_pos,
zhihuang38989e52017-03-21 11:04:53 -0700925 session_connection_addr, desc, &candidates,
926 error)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000927 delete desc;
Yves Gerey665174f2018-06-19 15:03:05 +0200928 for (std::vector<JsepIceCandidate*>::const_iterator it = candidates.begin();
929 it != candidates.end(); ++it) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000930 delete *it;
931 }
932 return false;
933 }
934
935 jdesc->Initialize(desc, session_id, session_version);
936
Yves Gerey665174f2018-06-19 15:03:05 +0200937 for (std::vector<JsepIceCandidate*>::const_iterator it = candidates.begin();
938 it != candidates.end(); ++it) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000939 jdesc->AddCandidate(*it);
940 delete *it;
941 }
942 return true;
943}
944
945bool SdpDeserializeCandidate(const std::string& message,
946 JsepIceCandidate* jcandidate,
947 SdpParseError* error) {
nisseede5da42017-01-12 05:15:36 -0800948 RTC_DCHECK(jcandidate != NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000949 Candidate candidate;
950 if (!ParseCandidate(message, &candidate, error, true)) {
951 return false;
952 }
953 jcandidate->SetCandidate(candidate);
954 return true;
955}
956
Honghai Zhang7fb69db2016-03-14 11:59:18 -0700957bool SdpDeserializeCandidate(const std::string& transport_name,
958 const std::string& message,
959 cricket::Candidate* candidate,
960 SdpParseError* error) {
nisseede5da42017-01-12 05:15:36 -0800961 RTC_DCHECK(candidate != nullptr);
Honghai Zhang7fb69db2016-03-14 11:59:18 -0700962 if (!ParseCandidate(message, candidate, error, true)) {
963 return false;
964 }
965 candidate->set_transport_name(transport_name);
966 return true;
967}
968
Yves Gerey665174f2018-06-19 15:03:05 +0200969bool ParseCandidate(const std::string& message,
970 Candidate* candidate,
971 SdpParseError* error,
972 bool is_raw) {
nisseede5da42017-01-12 05:15:36 -0800973 RTC_DCHECK(candidate != NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000974
975 // Get the first line from |message|.
jiayl@webrtc.org7ec3f9f2014-08-08 23:09:15 +0000976 std::string first_line = message;
977 size_t pos = 0;
978 GetLine(message, &pos, &first_line);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000979
jiayl@webrtc.org7ec3f9f2014-08-08 23:09:15 +0000980 // Makes sure |message| contains only one line.
981 if (message.size() > first_line.size()) {
982 std::string left, right;
Donald Curtis0e07f922015-05-15 09:21:23 -0700983 if (rtc::tokenize_first(message, kNewLine, &left, &right) &&
984 !right.empty()) {
jiayl@webrtc.org7ec3f9f2014-08-08 23:09:15 +0000985 return ParseFailed(message, 0, "Expect one line only", error);
986 }
987 }
988
989 // From WebRTC draft section 4.8.1.1 candidate-attribute should be
990 // candidate:<candidate> when trickled, but we still support
991 // a=candidate:<blah>CRLF for backward compatibility and for parsing a line
992 // from the SDP.
993 if (IsLineType(first_line, kLineTypeAttributes)) {
994 first_line = first_line.substr(kLinePrefixLength);
995 }
996
997 std::string attribute_candidate;
998 std::string candidate_value;
999
1000 // |first_line| must be in the form of "candidate:<value>".
Donald Curtis144d0182015-05-15 13:14:24 -07001001 if (!rtc::tokenize_first(first_line, kSdpDelimiterColon, &attribute_candidate,
1002 &candidate_value) ||
jiayl@webrtc.org7ec3f9f2014-08-08 23:09:15 +00001003 attribute_candidate != kAttributeCandidate) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001004 if (is_raw) {
1005 std::ostringstream description;
Yves Gerey665174f2018-06-19 15:03:05 +02001006 description << "Expect line: " << kAttributeCandidate << ":"
1007 << "<candidate-str>";
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001008 return ParseFailed(first_line, 0, description.str(), error);
1009 } else {
1010 return ParseFailedExpectLine(first_line, 0, kLineTypeAttributes,
1011 kAttributeCandidate, error);
1012 }
1013 }
1014
1015 std::vector<std::string> fields;
jiayl@webrtc.org7ec3f9f2014-08-08 23:09:15 +00001016 rtc::split(candidate_value, kSdpDelimiterSpace, &fields);
1017
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001018 // RFC 5245
1019 // a=candidate:<foundation> <component-id> <transport> <priority>
1020 // <connection-address> <port> typ <candidate-types>
1021 // [raddr <connection-address>] [rport <port>]
1022 // *(SP extension-att-name SP extension-att-value)
1023 const size_t expected_min_fields = 8;
1024 if (fields.size() < expected_min_fields ||
1025 (fields[6] != kAttributeCandidateTyp)) {
1026 return ParseFailedExpectMinFieldNum(first_line, expected_min_fields, error);
1027 }
jbauch083b73f2015-07-16 02:46:32 -07001028 const std::string& foundation = fields[0];
jiayl@webrtc.org7ec3f9f2014-08-08 23:09:15 +00001029
wu@webrtc.org5e760e72014-04-02 23:19:09 +00001030 int component_id = 0;
1031 if (!GetValueFromString(first_line, fields[1], &component_id, error)) {
1032 return false;
1033 }
jbauch083b73f2015-07-16 02:46:32 -07001034 const std::string& transport = fields[2];
Peter Boström0c4e06b2015-10-07 12:23:21 +02001035 uint32_t priority = 0;
wu@webrtc.org5e760e72014-04-02 23:19:09 +00001036 if (!GetValueFromString(first_line, fields[3], &priority, error)) {
1037 return false;
1038 }
jbauch083b73f2015-07-16 02:46:32 -07001039 const std::string& connection_address = fields[4];
wu@webrtc.org5e760e72014-04-02 23:19:09 +00001040 int port = 0;
1041 if (!GetValueFromString(first_line, fields[5], &port, error)) {
1042 return false;
1043 }
deadbeef90f1e1e2017-02-10 12:35:05 -08001044 if (!IsValidPort(port)) {
1045 return ParseFailed(first_line, "Invalid port number.", error);
1046 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001047 SocketAddress address(connection_address, port);
1048
1049 cricket::ProtocolType protocol;
hnslb68cc752016-12-13 10:33:41 -08001050 if (!StringToProto(transport.c_str(), &protocol)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001051 return ParseFailed(first_line, "Unsupported transport type.", error);
1052 }
hnslb68cc752016-12-13 10:33:41 -08001053 switch (protocol) {
1054 case cricket::PROTO_UDP:
1055 case cricket::PROTO_TCP:
1056 case cricket::PROTO_SSLTCP:
1057 // Supported protocol.
1058 break;
1059 default:
1060 return ParseFailed(first_line, "Unsupported transport type.", error);
1061 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001062
1063 std::string candidate_type;
jbauch083b73f2015-07-16 02:46:32 -07001064 const std::string& type = fields[7];
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001065 if (type == kCandidateHost) {
1066 candidate_type = cricket::LOCAL_PORT_TYPE;
1067 } else if (type == kCandidateSrflx) {
1068 candidate_type = cricket::STUN_PORT_TYPE;
1069 } else if (type == kCandidateRelay) {
1070 candidate_type = cricket::RELAY_PORT_TYPE;
Honghai Zhang7fb69db2016-03-14 11:59:18 -07001071 } else if (type == kCandidatePrflx) {
1072 candidate_type = cricket::PRFLX_PORT_TYPE;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001073 } else {
1074 return ParseFailed(first_line, "Unsupported candidate type.", error);
1075 }
1076
1077 size_t current_position = expected_min_fields;
1078 SocketAddress related_address;
1079 // The 2 optional fields for related address
1080 // [raddr <connection-address>] [rport <port>]
1081 if (fields.size() >= (current_position + 2) &&
1082 fields[current_position] == kAttributeCandidateRaddr) {
1083 related_address.SetIP(fields[++current_position]);
1084 ++current_position;
1085 }
1086 if (fields.size() >= (current_position + 2) &&
1087 fields[current_position] == kAttributeCandidateRport) {
wu@webrtc.org5e760e72014-04-02 23:19:09 +00001088 int port = 0;
Yves Gerey665174f2018-06-19 15:03:05 +02001089 if (!GetValueFromString(first_line, fields[++current_position], &port,
1090 error)) {
wu@webrtc.org5e760e72014-04-02 23:19:09 +00001091 return false;
1092 }
deadbeef90f1e1e2017-02-10 12:35:05 -08001093 if (!IsValidPort(port)) {
1094 return ParseFailed(first_line, "Invalid port number.", error);
1095 }
wu@webrtc.org5e760e72014-04-02 23:19:09 +00001096 related_address.SetPort(port);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001097 ++current_position;
1098 }
1099
mallinath@webrtc.org2d60c5e2014-08-08 22:29:20 +00001100 // If this is a TCP candidate, it has additional extension as defined in
1101 // RFC 6544.
1102 std::string tcptype;
1103 if (fields.size() >= (current_position + 2) &&
1104 fields[current_position] == kTcpCandidateType) {
1105 tcptype = fields[++current_position];
1106 ++current_position;
1107
1108 if (tcptype != cricket::TCPTYPE_ACTIVE_STR &&
1109 tcptype != cricket::TCPTYPE_PASSIVE_STR &&
1110 tcptype != cricket::TCPTYPE_SIMOPEN_STR) {
1111 return ParseFailed(first_line, "Invalid TCP candidate type.", error);
1112 }
1113
1114 if (protocol != cricket::PROTO_TCP) {
1115 return ParseFailed(first_line, "Invalid non-TCP candidate", error);
1116 }
1117 }
1118
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001119 // Extension
honghaiza54a0802015-12-16 18:37:23 -08001120 // Though non-standard, we support the ICE ufrag and pwd being signaled on
1121 // the candidate to avoid issues with confusing which generation a candidate
1122 // belongs to when trickling multiple generations at the same time.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001123 std::string username;
1124 std::string password;
Peter Boström0c4e06b2015-10-07 12:23:21 +02001125 uint32_t generation = 0;
honghaiza0c44ea2016-03-23 16:07:48 -07001126 uint16_t network_id = 0;
1127 uint16_t network_cost = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001128 for (size_t i = current_position; i + 1 < fields.size(); ++i) {
1129 // RFC 5245
1130 // *(SP extension-att-name SP extension-att-value)
1131 if (fields[i] == kAttributeCandidateGeneration) {
wu@webrtc.org5e760e72014-04-02 23:19:09 +00001132 if (!GetValueFromString(first_line, fields[++i], &generation, error)) {
1133 return false;
1134 }
honghaiza54a0802015-12-16 18:37:23 -08001135 } else if (fields[i] == kAttributeCandidateUfrag) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001136 username = fields[++i];
honghaiza54a0802015-12-16 18:37:23 -08001137 } else if (fields[i] == kAttributeCandidatePwd) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001138 password = fields[++i];
honghaiza0c44ea2016-03-23 16:07:48 -07001139 } else if (fields[i] == kAttributeCandidateNetworkId) {
1140 if (!GetValueFromString(first_line, fields[++i], &network_id, error)) {
1141 return false;
1142 }
honghaize1a0c942016-02-16 14:54:56 -08001143 } else if (fields[i] == kAttributeCandidateNetworkCost) {
1144 if (!GetValueFromString(first_line, fields[++i], &network_cost, error)) {
1145 return false;
1146 }
Honghai Zhang351d77b2016-05-20 15:08:29 -07001147 network_cost = std::min(network_cost, rtc::kNetworkCostMax);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001148 } else {
1149 // Skip the unknown extension.
1150 ++i;
1151 }
1152 }
1153
guoweis@webrtc.org61c12472015-01-15 06:53:07 +00001154 *candidate = Candidate(component_id, cricket::ProtoToString(protocol),
guoweis@webrtc.org950c5182014-12-16 23:01:31 +00001155 address, priority, username, password, candidate_type,
honghaiza0c44ea2016-03-23 16:07:48 -07001156 generation, foundation, network_id, network_cost);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001157 candidate->set_related_address(related_address);
mallinath@webrtc.org2d60c5e2014-08-08 22:29:20 +00001158 candidate->set_tcptype(tcptype);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001159 return true;
1160}
1161
1162bool ParseIceOptions(const std::string& line,
1163 std::vector<std::string>* transport_options,
1164 SdpParseError* error) {
1165 std::string ice_options;
1166 if (!GetValue(line, kAttributeIceOption, &ice_options, error)) {
1167 return false;
1168 }
1169 std::vector<std::string> fields;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001170 rtc::split(ice_options, kSdpDelimiterSpace, &fields);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001171 for (size_t i = 0; i < fields.size(); ++i) {
1172 transport_options->push_back(fields[i]);
1173 }
1174 return true;
1175}
1176
jiayl@webrtc.orgddb85ab2014-09-05 16:31:56 +00001177bool ParseSctpPort(const std::string& line,
1178 int* sctp_port,
1179 SdpParseError* error) {
1180 // draft-ietf-mmusic-sctp-sdp-07
1181 // a=sctp-port
1182 std::vector<std::string> fields;
jiayl@webrtc.orgddb85ab2014-09-05 16:31:56 +00001183 const size_t expected_min_fields = 2;
lally69f57602015-10-08 10:15:04 -07001184 rtc::split(line.substr(kLinePrefixLength), kSdpDelimiterColon, &fields);
1185 if (fields.size() < expected_min_fields) {
1186 fields.resize(0);
1187 rtc::split(line.substr(kLinePrefixLength), kSdpDelimiterSpace, &fields);
1188 }
jiayl@webrtc.orgddb85ab2014-09-05 16:31:56 +00001189 if (fields.size() < expected_min_fields) {
1190 return ParseFailedExpectMinFieldNum(line, expected_min_fields, error);
1191 }
1192 if (!rtc::FromString(fields[1], sctp_port)) {
lally69f57602015-10-08 10:15:04 -07001193 return ParseFailed(line, "Invalid sctp port value.", error);
jiayl@webrtc.orgddb85ab2014-09-05 16:31:56 +00001194 }
1195 return true;
1196}
1197
isheriff6f8d6862016-05-26 11:24:55 -07001198bool ParseExtmap(const std::string& line,
1199 RtpExtension* extmap,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001200 SdpParseError* error) {
1201 // RFC 5285
1202 // a=extmap:<value>["/"<direction>] <URI> <extensionattributes>
1203 std::vector<std::string> fields;
Yves Gerey665174f2018-06-19 15:03:05 +02001204 rtc::split(line.substr(kLinePrefixLength), kSdpDelimiterSpace, &fields);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001205 const size_t expected_min_fields = 2;
1206 if (fields.size() < expected_min_fields) {
1207 return ParseFailedExpectMinFieldNum(line, expected_min_fields, error);
1208 }
1209 std::string uri = fields[1];
1210
1211 std::string value_direction;
1212 if (!GetValue(fields[0], kAttributeExtmap, &value_direction, error)) {
1213 return false;
1214 }
1215 std::vector<std::string> sub_fields;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001216 rtc::split(value_direction, kSdpDelimiterSlash, &sub_fields);
wu@webrtc.org5e760e72014-04-02 23:19:09 +00001217 int value = 0;
1218 if (!GetValueFromString(line, sub_fields[0], &value, error)) {
1219 return false;
1220 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001221
jbauch5869f502017-06-29 12:31:36 -07001222 bool encrypted = false;
1223 if (uri == RtpExtension::kEncryptHeaderExtensionsUri) {
1224 // RFC 6904
Steve Anton36b29d12017-10-30 09:57:42 -07001225 // a=extmap:<value["/"<direction>] urn:ietf:params:rtp-hdrext:encrypt <URI>
1226 // <extensionattributes>
jbauch5869f502017-06-29 12:31:36 -07001227 const size_t expected_min_fields_encrypted = expected_min_fields + 1;
1228 if (fields.size() < expected_min_fields_encrypted) {
1229 return ParseFailedExpectMinFieldNum(line, expected_min_fields_encrypted,
Yves Gerey665174f2018-06-19 15:03:05 +02001230 error);
jbauch5869f502017-06-29 12:31:36 -07001231 }
1232
1233 encrypted = true;
1234 uri = fields[2];
1235 if (uri == RtpExtension::kEncryptHeaderExtensionsUri) {
1236 return ParseFailed(line, "Recursive encrypted header.", error);
1237 }
1238 }
1239
1240 *extmap = RtpExtension(uri, value, encrypted);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001241 return true;
1242}
1243
1244void BuildMediaDescription(const ContentInfo* content_info,
1245 const TransportInfo* transport_info,
1246 const MediaType media_type,
wu@webrtc.org4c3e9912014-07-16 21:03:13 +00001247 const std::vector<Candidate>& candidates,
Steve Antone831b8c2018-02-01 12:22:16 -08001248 int msid_signaling,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001249 std::string* message) {
nisseede5da42017-01-12 05:15:36 -08001250 RTC_DCHECK(message != NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001251 if (content_info == NULL || message == NULL) {
1252 return;
1253 }
Steve Anton36b29d12017-10-30 09:57:42 -07001254 // TODO(deadbeef): Rethink if we should use sprintfn instead of stringstream.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001255 // According to the style guide, streams should only be used for logging.
1256 // http://google-styleguide.googlecode.com/svn/
1257 // trunk/cppguide.xml?showone=Streams#Streams
1258 std::ostringstream os;
Steve Antonb1c1de12017-12-21 15:14:30 -08001259 const MediaContentDescription* media_desc = content_info->media_description();
1260 RTC_DCHECK(media_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001261
jiayl@webrtc.org9c16c392014-05-01 18:30:30 +00001262 int sctp_port = cricket::kSctpDefaultPort;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001263
1264 // RFC 4566
1265 // m=<media> <port> <proto> <fmt>
1266 // fmt is a list of payload type numbers that MAY be used in the session.
1267 const char* type = NULL;
1268 if (media_type == cricket::MEDIA_TYPE_AUDIO)
1269 type = kMediaTypeAudio;
1270 else if (media_type == cricket::MEDIA_TYPE_VIDEO)
1271 type = kMediaTypeVideo;
1272 else if (media_type == cricket::MEDIA_TYPE_DATA)
1273 type = kMediaTypeData;
1274 else
nissec80e7412017-01-11 05:56:46 -08001275 RTC_NOTREACHED();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001276
1277 std::string fmt;
1278 if (media_type == cricket::MEDIA_TYPE_VIDEO) {
Steve Antonb1c1de12017-12-21 15:14:30 -08001279 const VideoContentDescription* video_desc = media_desc->as_video();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001280 for (std::vector<cricket::VideoCodec>::const_iterator it =
1281 video_desc->codecs().begin();
1282 it != video_desc->codecs().end(); ++it) {
1283 fmt.append(" ");
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001284 fmt.append(rtc::ToString<int>(it->id));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001285 }
1286 } else if (media_type == cricket::MEDIA_TYPE_AUDIO) {
Steve Antonb1c1de12017-12-21 15:14:30 -08001287 const AudioContentDescription* audio_desc = media_desc->as_audio();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001288 for (std::vector<cricket::AudioCodec>::const_iterator it =
1289 audio_desc->codecs().begin();
1290 it != audio_desc->codecs().end(); ++it) {
1291 fmt.append(" ");
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001292 fmt.append(rtc::ToString<int>(it->id));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001293 }
1294 } else if (media_type == cricket::MEDIA_TYPE_DATA) {
Steve Antonb1c1de12017-12-21 15:14:30 -08001295 const DataContentDescription* data_desc = media_desc->as_data();
pthatcher@webrtc.org3341b402015-02-13 21:14:22 +00001296 if (IsDtlsSctp(media_desc->protocol())) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001297 fmt.append(" ");
jiayl@webrtc.org9c16c392014-05-01 18:30:30 +00001298
zstein4b2e0822017-02-17 19:48:38 -08001299 if (data_desc->use_sctpmap()) {
1300 for (std::vector<cricket::DataCodec>::const_iterator it =
1301 data_desc->codecs().begin();
1302 it != data_desc->codecs().end(); ++it) {
1303 if (cricket::CodecNamesEq(it->name,
1304 cricket::kGoogleSctpDataCodecName) &&
1305 it->GetParam(cricket::kCodecParamPort, &sctp_port)) {
1306 break;
1307 }
jiayl@webrtc.org9c16c392014-05-01 18:30:30 +00001308 }
jiayl@webrtc.org9c16c392014-05-01 18:30:30 +00001309
zstein4b2e0822017-02-17 19:48:38 -08001310 fmt.append(rtc::ToString<int>(sctp_port));
1311 } else {
1312 fmt.append(kDefaultSctpmapProtocol);
1313 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001314 } else {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001315 for (std::vector<cricket::DataCodec>::const_iterator it =
Yves Gerey665174f2018-06-19 15:03:05 +02001316 data_desc->codecs().begin();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001317 it != data_desc->codecs().end(); ++it) {
1318 fmt.append(" ");
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001319 fmt.append(rtc::ToString<int>(it->id));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001320 }
1321 }
1322 }
1323 // The fmt must never be empty. If no codecs are found, set the fmt attribute
1324 // to 0.
1325 if (fmt.empty()) {
1326 fmt = " 0";
1327 }
1328
deadbeef25ed4352016-12-12 18:37:36 -08001329 // The port number in the m line will be updated later when associated with
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001330 // the candidates.
deadbeef25ed4352016-12-12 18:37:36 -08001331 //
1332 // A port value of 0 indicates that the m= section is rejected.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001333 // RFC 3264
1334 // To reject an offered stream, the port number in the corresponding stream in
1335 // the answer MUST be set to zero.
deadbeef25ed4352016-12-12 18:37:36 -08001336 //
1337 // However, the BUNDLE draft adds a new meaning to port zero, when used along
1338 // with a=bundle-only.
zhihuang38989e52017-03-21 11:04:53 -07001339 std::string port = kDummyPort;
1340 if (content_info->rejected || content_info->bundle_only) {
1341 port = kMediaPortRejected;
1342 } else if (!media_desc->connection_address().IsNil()) {
1343 port = rtc::ToString(media_desc->connection_address().port());
1344 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001345
Yves Gerey665174f2018-06-19 15:03:05 +02001346 rtc::SSLFingerprint* fp =
1347 (transport_info) ? transport_info->description.identity_fingerprint.get()
1348 : NULL;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001349
wu@webrtc.org4c3e9912014-07-16 21:03:13 +00001350 // Add the m and c lines.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001351 InitLine(kLineTypeMedia, type, &os);
1352 os << " " << port << " " << media_desc->protocol() << fmt;
zhihuang38989e52017-03-21 11:04:53 -07001353 AddLine(os.str(), message);
1354
1355 InitLine(kLineTypeConnection, kConnectionNettype, &os);
1356 if (media_desc->connection_address().IsNil()) {
1357 os << " " << kConnectionIpv4Addrtype << " " << kDummyAddress;
1358 } else if (media_desc->connection_address().family() == AF_INET) {
1359 os << " " << kConnectionIpv4Addrtype << " "
1360 << media_desc->connection_address().ipaddr().ToString();
1361 } else {
1362 os << " " << kConnectionIpv6Addrtype << " "
1363 << media_desc->connection_address().ipaddr().ToString();
1364 }
1365 AddLine(os.str(), message);
wu@webrtc.org4c3e9912014-07-16 21:03:13 +00001366
1367 // RFC 4566
1368 // b=AS:<bandwidth>
Peter Thatcherc0c3a862015-06-24 15:31:25 -07001369 if (media_desc->bandwidth() >= 1000) {
wu@webrtc.org4c3e9912014-07-16 21:03:13 +00001370 InitLine(kLineTypeSessionBandwidth, kApplicationSpecificMaximum, &os);
1371 os << kSdpDelimiterColon << (media_desc->bandwidth() / 1000);
1372 AddLine(os.str(), message);
1373 }
1374
deadbeef25ed4352016-12-12 18:37:36 -08001375 // Add the a=bundle-only line.
1376 if (content_info->bundle_only) {
1377 InitAttrLine(kAttributeBundleOnly, &os);
1378 AddLine(os.str(), message);
1379 }
1380
wu@webrtc.org4c3e9912014-07-16 21:03:13 +00001381 // Add the a=rtcp line.
pthatcher@webrtc.org3341b402015-02-13 21:14:22 +00001382 if (IsRtp(media_desc->protocol())) {
wu@webrtc.org4c3e9912014-07-16 21:03:13 +00001383 std::string rtcp_line = GetRtcpLine(candidates);
1384 if (!rtcp_line.empty()) {
1385 AddLine(rtcp_line, message);
1386 }
1387 }
1388
honghaiza54a0802015-12-16 18:37:23 -08001389 // Build the a=candidate lines. We don't include ufrag and pwd in the
1390 // candidates in the SDP to avoid redundancy.
1391 BuildCandidate(candidates, false, message);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001392
1393 // Use the transport_info to build the media level ice-ufrag and ice-pwd.
1394 if (transport_info) {
1395 // RFC 5245
1396 // ice-pwd-att = "ice-pwd" ":" password
1397 // ice-ufrag-att = "ice-ufrag" ":" ufrag
1398 // ice-ufrag
deadbeef3f7219b2015-12-28 15:17:14 -08001399 if (!transport_info->description.ice_ufrag.empty()) {
1400 InitAttrLine(kAttributeIceUfrag, &os);
1401 os << kSdpDelimiterColon << transport_info->description.ice_ufrag;
1402 AddLine(os.str(), message);
1403 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001404 // ice-pwd
deadbeef3f7219b2015-12-28 15:17:14 -08001405 if (!transport_info->description.ice_pwd.empty()) {
1406 InitAttrLine(kAttributeIcePwd, &os);
1407 os << kSdpDelimiterColon << transport_info->description.ice_pwd;
1408 AddLine(os.str(), message);
1409 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001410
1411 // draft-petithuguenin-mmusic-ice-attributes-level-03
1412 BuildIceOptions(transport_info->description.transport_options, message);
1413
1414 // RFC 4572
1415 // fingerprint-attribute =
1416 // "fingerprint" ":" hash-func SP fingerprint
1417 if (fp) {
1418 // Insert the fingerprint attribute.
1419 InitAttrLine(kAttributeFingerprint, &os);
Yves Gerey665174f2018-06-19 15:03:05 +02001420 os << kSdpDelimiterColon << fp->algorithm << kSdpDelimiterSpace
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001421 << fp->GetRfc4572Fingerprint();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001422 AddLine(os.str(), message);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001423
1424 // Inserting setup attribute.
1425 if (transport_info->description.connection_role !=
Yves Gerey665174f2018-06-19 15:03:05 +02001426 cricket::CONNECTIONROLE_NONE) {
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001427 // Making sure we are not using "passive" mode.
1428 cricket::ConnectionRole role =
1429 transport_info->description.connection_role;
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00001430 std::string dtls_role_str;
nissec16fa5e2017-02-07 07:18:43 -08001431 const bool success =
1432 cricket::ConnectionRoleToString(role, &dtls_role_str);
1433 RTC_DCHECK(success);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001434 InitAttrLine(kAttributeSetup, &os);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001435 os << kSdpDelimiterColon << dtls_role_str;
1436 AddLine(os.str(), message);
1437 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001438 }
1439 }
1440
1441 // RFC 3388
1442 // mid-attribute = "a=mid:" identification-tag
1443 // identification-tag = token
1444 // Use the content name as the mid identification-tag.
1445 InitAttrLine(kAttributeMid, &os);
1446 os << kSdpDelimiterColon << content_info->name;
1447 AddLine(os.str(), message);
1448
pthatcher@webrtc.org3341b402015-02-13 21:14:22 +00001449 if (IsDtlsSctp(media_desc->protocol())) {
Steve Antonb1c1de12017-12-21 15:14:30 -08001450 const DataContentDescription* data_desc = media_desc->as_data();
zstein4b2e0822017-02-17 19:48:38 -08001451 bool use_sctpmap = data_desc->use_sctpmap();
1452 BuildSctpContentAttributes(message, sctp_port, use_sctpmap);
pthatcher@webrtc.org3341b402015-02-13 21:14:22 +00001453 } else if (IsRtp(media_desc->protocol())) {
Steve Antone831b8c2018-02-01 12:22:16 -08001454 BuildRtpContentAttributes(media_desc, media_type, msid_signaling, message);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001455 }
1456}
1457
zstein4b2e0822017-02-17 19:48:38 -08001458void BuildSctpContentAttributes(std::string* message,
1459 int sctp_port,
1460 bool use_sctpmap) {
wu@webrtc.org78187522013-10-07 23:32:02 +00001461 std::ostringstream os;
zstein4b2e0822017-02-17 19:48:38 -08001462 if (use_sctpmap) {
1463 // draft-ietf-mmusic-sctp-sdp-04
1464 // a=sctpmap:sctpmap-number protocol [streams]
1465 InitAttrLine(kAttributeSctpmap, &os);
1466 os << kSdpDelimiterColon << sctp_port << kSdpDelimiterSpace
1467 << kDefaultSctpmapProtocol << kSdpDelimiterSpace
1468 << cricket::kMaxSctpStreams;
1469 } else {
1470 // draft-ietf-mmusic-sctp-sdp-23
1471 // a=sctp-port:<port>
1472 InitAttrLine(kAttributeSctpPort, &os);
1473 os << kSdpDelimiterColon << sctp_port;
1474 // TODO(zstein): emit max-message-size here
1475 }
wu@webrtc.org78187522013-10-07 23:32:02 +00001476 AddLine(os.str(), message);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001477}
1478
deadbeef9d3584c2016-02-16 17:54:10 -08001479void BuildRtpContentAttributes(const MediaContentDescription* media_desc,
1480 const MediaType media_type,
Steve Antone831b8c2018-02-01 12:22:16 -08001481 int msid_signaling,
deadbeef9d3584c2016-02-16 17:54:10 -08001482 std::string* message) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001483 std::ostringstream os;
1484 // RFC 5285
1485 // a=extmap:<value>["/"<direction>] <URI> <extensionattributes>
1486 // The definitions MUST be either all session level or all media level. This
1487 // implementation uses all media level.
1488 for (size_t i = 0; i < media_desc->rtp_header_extensions().size(); ++i) {
jbauch5869f502017-06-29 12:31:36 -07001489 const RtpExtension& extension = media_desc->rtp_header_extensions()[i];
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001490 InitAttrLine(kAttributeExtmap, &os);
jbauch5869f502017-06-29 12:31:36 -07001491 os << kSdpDelimiterColon << extension.id;
1492 if (extension.encrypt) {
1493 os << kSdpDelimiterSpace << RtpExtension::kEncryptHeaderExtensionsUri;
1494 }
1495 os << kSdpDelimiterSpace << extension.uri;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001496 AddLine(os.str(), message);
1497 }
1498
1499 // RFC 3264
1500 // a=sendrecv || a=sendonly || a=sendrecv || a=inactive
deadbeefc80741f2015-10-22 13:14:45 -07001501 switch (media_desc->direction()) {
Steve Anton4e70a722017-11-28 14:57:10 -08001502 case RtpTransceiverDirection::kInactive:
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001503 InitAttrLine(kAttributeInactive, &os);
1504 break;
Steve Anton4e70a722017-11-28 14:57:10 -08001505 case RtpTransceiverDirection::kSendOnly:
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001506 InitAttrLine(kAttributeSendOnly, &os);
1507 break;
Steve Anton4e70a722017-11-28 14:57:10 -08001508 case RtpTransceiverDirection::kRecvOnly:
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001509 InitAttrLine(kAttributeRecvOnly, &os);
1510 break;
Steve Anton4e70a722017-11-28 14:57:10 -08001511 case RtpTransceiverDirection::kSendRecv:
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001512 default:
1513 InitAttrLine(kAttributeSendRecv, &os);
1514 break;
1515 }
1516 AddLine(os.str(), message);
1517
Seth Hampson5b4f0752018-04-02 16:31:36 -07001518 // Specified in https://datatracker.ietf.org/doc/draft-ietf-mmusic-msid/16/
1519 // a=msid:<msid-id> <msid-appdata>
1520 // The msid-id is a 1*64 token char representing the media stream id, and the
1521 // msid-appdata is a 1*64 token char representing the track id. There is a
1522 // line for every media stream, with a special msid-id value of "-"
1523 // representing no streams. The value of "msid-appdata" MUST be identical for
1524 // all lines.
Steve Antone831b8c2018-02-01 12:22:16 -08001525 if (msid_signaling & cricket::kMsidSignalingMediaSection) {
1526 const StreamParamsVec& streams = media_desc->streams();
1527 if (streams.size() == 1u) {
1528 const StreamParams& track = streams[0];
Seth Hampson5b4f0752018-04-02 16:31:36 -07001529 std::vector<std::string> stream_ids = track.stream_ids();
1530 if (stream_ids.empty()) {
1531 stream_ids.push_back(kNoStreamMsid);
1532 }
1533 for (const std::string& stream_id : stream_ids) {
1534 InitAttrLine(kAttributeMsid, &os);
1535 os << kSdpDelimiterColon << stream_id << kSdpDelimiterSpace << track.id;
1536 AddLine(os.str(), message);
1537 }
Steve Antone831b8c2018-02-01 12:22:16 -08001538 } else if (streams.size() > 1u) {
1539 RTC_LOG(LS_WARNING)
1540 << "Trying to serialize Unified Plan SDP with more than "
Jonas Olsson45cc8902018-02-13 10:37:07 +01001541 "one track in a media section. Omitting 'a=msid'.";
deadbeef9d3584c2016-02-16 17:54:10 -08001542 }
1543 }
1544
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001545 // RFC 5761
1546 // a=rtcp-mux
1547 if (media_desc->rtcp_mux()) {
1548 InitAttrLine(kAttributeRtcpMux, &os);
1549 AddLine(os.str(), message);
1550 }
1551
deadbeef13871492015-12-09 12:37:51 -08001552 // RFC 5506
1553 // a=rtcp-rsize
1554 if (media_desc->rtcp_reduced_size()) {
1555 InitAttrLine(kAttributeRtcpReducedSize, &os);
1556 AddLine(os.str(), message);
1557 }
1558
deadbeefd45aea82017-09-16 01:24:29 -07001559 if (media_desc->conference_mode()) {
1560 InitAttrLine(kAttributeXGoogleFlag, &os);
1561 os << kSdpDelimiterColon << kValueConference;
1562 AddLine(os.str(), message);
1563 }
1564
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001565 // RFC 4568
1566 // a=crypto:<tag> <crypto-suite> <key-params> [<session-params>]
1567 for (std::vector<CryptoParams>::const_iterator it =
1568 media_desc->cryptos().begin();
1569 it != media_desc->cryptos().end(); ++it) {
1570 InitAttrLine(kAttributeCrypto, &os);
1571 os << kSdpDelimiterColon << it->tag << " " << it->cipher_suite << " "
1572 << it->key_params;
1573 if (!it->session_params.empty()) {
1574 os << " " << it->session_params;
1575 }
1576 AddLine(os.str(), message);
1577 }
1578
1579 // RFC 4566
1580 // a=rtpmap:<payload type> <encoding name>/<clock rate>
1581 // [/<encodingparameters>]
1582 BuildRtpMap(media_desc, media_type, message);
1583
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001584 for (StreamParamsVec::const_iterator track = media_desc->streams().begin();
1585 track != media_desc->streams().end(); ++track) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001586 // Build the ssrc-group lines.
1587 for (size_t i = 0; i < track->ssrc_groups.size(); ++i) {
1588 // RFC 5576
1589 // a=ssrc-group:<semantics> <ssrc-id> ...
1590 if (track->ssrc_groups[i].ssrcs.empty()) {
1591 continue;
1592 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001593 InitAttrLine(kAttributeSsrcGroup, &os);
1594 os << kSdpDelimiterColon << track->ssrc_groups[i].semantics;
Peter Boström0c4e06b2015-10-07 12:23:21 +02001595 std::vector<uint32_t>::const_iterator ssrc =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001596 track->ssrc_groups[i].ssrcs.begin();
1597 for (; ssrc != track->ssrc_groups[i].ssrcs.end(); ++ssrc) {
Peter Boström0c4e06b2015-10-07 12:23:21 +02001598 os << kSdpDelimiterSpace << rtc::ToString<uint32_t>(*ssrc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001599 }
1600 AddLine(os.str(), message);
1601 }
1602 // Build the ssrc lines for each ssrc.
1603 for (size_t i = 0; i < track->ssrcs.size(); ++i) {
Peter Boström0c4e06b2015-10-07 12:23:21 +02001604 uint32_t ssrc = track->ssrcs[i];
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001605 // RFC 5576
1606 // a=ssrc:<ssrc-id> cname:<value>
Yves Gerey665174f2018-06-19 15:03:05 +02001607 AddSsrcLine(ssrc, kSsrcAttributeCname, track->cname, message);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001608
Steve Antone831b8c2018-02-01 12:22:16 -08001609 if (msid_signaling & cricket::kMsidSignalingSsrcAttribute) {
1610 // draft-alvestrand-mmusic-msid-00
1611 // a=ssrc:<ssrc-id> msid:identifier [appdata]
1612 // The appdata consists of the "id" attribute of a MediaStreamTrack,
1613 // which corresponds to the "id" attribute of StreamParams.
Seth Hampson5b4f0752018-04-02 16:31:36 -07001614 // Since a=ssrc msid signaling is used in Plan B SDP semantics, and
1615 // multiple stream ids are not supported for Plan B, we are only adding
1616 // a line for the first media stream id here.
Seth Hampson845e8782018-03-02 11:34:10 -08001617 const std::string& stream_id = track->first_stream_id();
Steve Antone831b8c2018-02-01 12:22:16 -08001618 InitAttrLine(kAttributeSsrc, &os);
1619 os << kSdpDelimiterColon << ssrc << kSdpDelimiterSpace
1620 << kSsrcAttributeMsid << kSdpDelimiterColon << stream_id
1621 << kSdpDelimiterSpace << track->id;
1622 AddLine(os.str(), message);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001623
Steve Antone831b8c2018-02-01 12:22:16 -08001624 // TODO(ronghuawu): Remove below code which is for backward
1625 // compatibility.
1626 // draft-alvestrand-rtcweb-mid-01
1627 // a=ssrc:<ssrc-id> mslabel:<value>
1628 // The label isn't yet defined.
1629 // a=ssrc:<ssrc-id> label:<value>
Seth Hampson5b4f0752018-04-02 16:31:36 -07001630 AddSsrcLine(ssrc, kSsrcAttributeMslabel, stream_id, message);
Steve Antone831b8c2018-02-01 12:22:16 -08001631 AddSsrcLine(ssrc, kSSrcAttributeLabel, track->id, message);
1632 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001633 }
1634 }
1635}
1636
1637void WriteFmtpHeader(int payload_type, std::ostringstream* os) {
1638 // fmtp header: a=fmtp:|payload_type| <parameters>
1639 // Add a=fmtp
1640 InitAttrLine(kAttributeFmtp, os);
1641 // Add :|payload_type|
1642 *os << kSdpDelimiterColon << payload_type;
1643}
1644
1645void WriteRtcpFbHeader(int payload_type, std::ostringstream* os) {
1646 // rtcp-fb header: a=rtcp-fb:|payload_type|
1647 // <parameters>/<ccm <ccm_parameters>>
1648 // Add a=rtcp-fb
1649 InitAttrLine(kAttributeRtcpFb, os);
1650 // Add :
1651 *os << kSdpDelimiterColon;
1652 if (payload_type == kWildcardPayloadType) {
1653 *os << "*";
1654 } else {
1655 *os << payload_type;
1656 }
1657}
1658
1659void WriteFmtpParameter(const std::string& parameter_name,
1660 const std::string& parameter_value,
1661 std::ostringstream* os) {
1662 // fmtp parameters: |parameter_name|=|parameter_value|
1663 *os << parameter_name << kSdpDelimiterEqual << parameter_value;
1664}
1665
1666void WriteFmtpParameters(const cricket::CodecParameterMap& parameters,
1667 std::ostringstream* os) {
1668 for (cricket::CodecParameterMap::const_iterator fmtp = parameters.begin();
1669 fmtp != parameters.end(); ++fmtp) {
hta62a216e2016-04-15 11:02:14 -07001670 // Parameters are a semicolon-separated list, no spaces.
1671 // The list is separated from the header by a space.
1672 if (fmtp == parameters.begin()) {
1673 *os << kSdpDelimiterSpace;
1674 } else {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001675 *os << kSdpDelimiterSemicolon;
1676 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001677 WriteFmtpParameter(fmtp->first, fmtp->second, os);
1678 }
1679}
1680
1681bool IsFmtpParam(const std::string& name) {
ossuaa4b0772017-01-30 07:41:18 -08001682 // RFC 4855, section 3 specifies the mapping of media format parameters to SDP
1683 // parameters. Only ptime, maxptime, channels and rate are placed outside of
1684 // the fmtp line. In WebRTC, channels and rate are already handled separately
1685 // and thus not included in the CodecParameterMap.
1686 return name != kCodecParamPTime && name != kCodecParamMaxPTime;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001687}
1688
1689// Retreives fmtp parameters from |params|, which may contain other parameters
1690// as well, and puts them in |fmtp_parameters|.
1691void GetFmtpParams(const cricket::CodecParameterMap& params,
1692 cricket::CodecParameterMap* fmtp_parameters) {
1693 for (cricket::CodecParameterMap::const_iterator iter = params.begin();
1694 iter != params.end(); ++iter) {
1695 if (IsFmtpParam(iter->first)) {
1696 (*fmtp_parameters)[iter->first] = iter->second;
1697 }
1698 }
1699}
1700
1701template <class T>
1702void AddFmtpLine(const T& codec, std::string* message) {
1703 cricket::CodecParameterMap fmtp_parameters;
1704 GetFmtpParams(codec.params, &fmtp_parameters);
1705 if (fmtp_parameters.empty()) {
1706 // No need to add an fmtp if it will have no (optional) parameters.
1707 return;
1708 }
1709 std::ostringstream os;
1710 WriteFmtpHeader(codec.id, &os);
1711 WriteFmtpParameters(fmtp_parameters, &os);
1712 AddLine(os.str(), message);
1713 return;
1714}
1715
1716template <class T>
1717void AddRtcpFbLines(const T& codec, std::string* message) {
1718 for (std::vector<cricket::FeedbackParam>::const_iterator iter =
1719 codec.feedback_params.params().begin();
1720 iter != codec.feedback_params.params().end(); ++iter) {
1721 std::ostringstream os;
1722 WriteRtcpFbHeader(codec.id, &os);
1723 os << " " << iter->id();
1724 if (!iter->param().empty()) {
1725 os << " " << iter->param();
1726 }
1727 AddLine(os.str(), message);
1728 }
1729}
1730
Yves Gerey665174f2018-06-19 15:03:05 +02001731bool AddSctpDataCodec(DataContentDescription* media_desc, int sctp_port) {
solenberg9fa49752016-10-08 13:02:44 -07001732 for (const auto& codec : media_desc->codecs()) {
1733 if (cricket::CodecNamesEq(codec.name, cricket::kGoogleSctpDataCodecName)) {
Yves Gerey665174f2018-06-19 15:03:05 +02001734 return ParseFailed("", "Can't have multiple sctp port attributes.", NULL);
solenberg9fa49752016-10-08 13:02:44 -07001735 }
jiayl@webrtc.orgddb85ab2014-09-05 16:31:56 +00001736 }
1737 // Add the SCTP Port number as a pseudo-codec "port" parameter
solenberg9fa49752016-10-08 13:02:44 -07001738 cricket::DataCodec codec_port(cricket::kGoogleSctpDataCodecPlType,
deadbeef67cf2c12016-04-13 10:07:16 -07001739 cricket::kGoogleSctpDataCodecName);
jiayl@webrtc.orgddb85ab2014-09-05 16:31:56 +00001740 codec_port.SetParam(cricket::kCodecParamPort, sctp_port);
Mirko Bonadei675513b2017-11-09 11:09:25 +01001741 RTC_LOG(INFO) << "AddSctpDataCodec: Got SCTP Port Number " << sctp_port;
jiayl@webrtc.orgddb85ab2014-09-05 16:31:56 +00001742 media_desc->AddCodec(codec_port);
1743 return true;
1744}
1745
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001746bool GetMinValue(const std::vector<int>& values, int* value) {
1747 if (values.empty()) {
1748 return false;
1749 }
1750 std::vector<int>::const_iterator found =
1751 std::min_element(values.begin(), values.end());
1752 *value = *found;
1753 return true;
1754}
1755
1756bool GetParameter(const std::string& name,
Yves Gerey665174f2018-06-19 15:03:05 +02001757 const cricket::CodecParameterMap& params,
1758 int* value) {
1759 std::map<std::string, std::string>::const_iterator found = params.find(name);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001760 if (found == params.end()) {
1761 return false;
1762 }
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001763 if (!rtc::FromString(found->second, value)) {
wu@webrtc.org5e760e72014-04-02 23:19:09 +00001764 return false;
1765 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001766 return true;
1767}
1768
1769void BuildRtpMap(const MediaContentDescription* media_desc,
1770 const MediaType media_type,
1771 std::string* message) {
nisseede5da42017-01-12 05:15:36 -08001772 RTC_DCHECK(message != NULL);
1773 RTC_DCHECK(media_desc != NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001774 std::ostringstream os;
1775 if (media_type == cricket::MEDIA_TYPE_VIDEO) {
Steve Antonb1c1de12017-12-21 15:14:30 -08001776 const VideoContentDescription* video_desc = media_desc->as_video();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001777 for (std::vector<cricket::VideoCodec>::const_iterator it =
1778 video_desc->codecs().begin();
1779 it != video_desc->codecs().end(); ++it) {
1780 // RFC 4566
1781 // a=rtpmap:<payload type> <encoding name>/<clock rate>
1782 // [/<encodingparameters>]
1783 if (it->id != kWildcardPayloadType) {
1784 InitAttrLine(kAttributeRtpmap, &os);
deadbeefe814a0d2017-02-25 18:15:09 -08001785 os << kSdpDelimiterColon << it->id << " " << it->name << "/"
1786 << cricket::kVideoCodecClockrate;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001787 AddLine(os.str(), message);
1788 }
1789 AddRtcpFbLines(*it, message);
1790 AddFmtpLine(*it, message);
1791 }
1792 } else if (media_type == cricket::MEDIA_TYPE_AUDIO) {
Steve Antonb1c1de12017-12-21 15:14:30 -08001793 const AudioContentDescription* audio_desc = media_desc->as_audio();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001794 std::vector<int> ptimes;
1795 std::vector<int> maxptimes;
1796 int max_minptime = 0;
1797 for (std::vector<cricket::AudioCodec>::const_iterator it =
1798 audio_desc->codecs().begin();
1799 it != audio_desc->codecs().end(); ++it) {
nisseede5da42017-01-12 05:15:36 -08001800 RTC_DCHECK(!it->name.empty());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001801 // RFC 4566
1802 // a=rtpmap:<payload type> <encoding name>/<clock rate>
1803 // [/<encodingparameters>]
1804 InitAttrLine(kAttributeRtpmap, &os);
1805 os << kSdpDelimiterColon << it->id << " ";
1806 os << it->name << "/" << it->clockrate;
1807 if (it->channels != 1) {
1808 os << "/" << it->channels;
1809 }
1810 AddLine(os.str(), message);
1811 AddRtcpFbLines(*it, message);
1812 AddFmtpLine(*it, message);
1813 int minptime = 0;
1814 if (GetParameter(kCodecParamMinPTime, it->params, &minptime)) {
1815 max_minptime = std::max(minptime, max_minptime);
1816 }
1817 int ptime;
1818 if (GetParameter(kCodecParamPTime, it->params, &ptime)) {
1819 ptimes.push_back(ptime);
1820 }
1821 int maxptime;
1822 if (GetParameter(kCodecParamMaxPTime, it->params, &maxptime)) {
1823 maxptimes.push_back(maxptime);
1824 }
1825 }
1826 // Populate the maxptime attribute with the smallest maxptime of all codecs
1827 // under the same m-line.
1828 int min_maxptime = INT_MAX;
1829 if (GetMinValue(maxptimes, &min_maxptime)) {
1830 AddAttributeLine(kCodecParamMaxPTime, min_maxptime, message);
1831 }
nisseede5da42017-01-12 05:15:36 -08001832 RTC_DCHECK(min_maxptime > max_minptime);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001833 // Populate the ptime attribute with the smallest ptime or the largest
1834 // minptime, whichever is the largest, for all codecs under the same m-line.
1835 int ptime = INT_MAX;
1836 if (GetMinValue(ptimes, &ptime)) {
1837 ptime = std::min(ptime, min_maxptime);
1838 ptime = std::max(ptime, max_minptime);
1839 AddAttributeLine(kCodecParamPTime, ptime, message);
1840 }
1841 } else if (media_type == cricket::MEDIA_TYPE_DATA) {
Steve Antonb1c1de12017-12-21 15:14:30 -08001842 const DataContentDescription* data_desc = media_desc->as_data();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001843 for (std::vector<cricket::DataCodec>::const_iterator it =
Yves Gerey665174f2018-06-19 15:03:05 +02001844 data_desc->codecs().begin();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001845 it != data_desc->codecs().end(); ++it) {
1846 // RFC 4566
1847 // a=rtpmap:<payload type> <encoding name>/<clock rate>
1848 // [/<encodingparameters>]
1849 InitAttrLine(kAttributeRtpmap, &os);
Yves Gerey665174f2018-06-19 15:03:05 +02001850 os << kSdpDelimiterColon << it->id << " " << it->name << "/"
1851 << it->clockrate;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001852 AddLine(os.str(), message);
1853 }
1854 }
1855}
1856
1857void BuildCandidate(const std::vector<Candidate>& candidates,
honghaiza54a0802015-12-16 18:37:23 -08001858 bool include_ufrag,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001859 std::string* message) {
1860 std::ostringstream os;
1861
1862 for (std::vector<Candidate>::const_iterator it = candidates.begin();
1863 it != candidates.end(); ++it) {
1864 // RFC 5245
1865 // a=candidate:<foundation> <component-id> <transport> <priority>
1866 // <connection-address> <port> typ <candidate-types>
1867 // [raddr <connection-address>] [rport <port>]
1868 // *(SP extension-att-name SP extension-att-value)
1869 std::string type;
1870 // Map the cricket candidate type to "host" / "srflx" / "prflx" / "relay"
1871 if (it->type() == cricket::LOCAL_PORT_TYPE) {
1872 type = kCandidateHost;
1873 } else if (it->type() == cricket::STUN_PORT_TYPE) {
1874 type = kCandidateSrflx;
1875 } else if (it->type() == cricket::RELAY_PORT_TYPE) {
1876 type = kCandidateRelay;
Honghai Zhang7fb69db2016-03-14 11:59:18 -07001877 } else if (it->type() == cricket::PRFLX_PORT_TYPE) {
1878 type = kCandidatePrflx;
1879 // Peer reflexive candidate may be signaled for being removed.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001880 } else {
nissec80e7412017-01-11 05:56:46 -08001881 RTC_NOTREACHED();
Peter Thatcher019087f2015-04-28 09:06:26 -07001882 // Never write out candidates if we don't know the type.
1883 continue;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001884 }
1885
1886 InitAttrLine(kAttributeCandidate, &os);
Yves Gerey665174f2018-06-19 15:03:05 +02001887 os << kSdpDelimiterColon << it->foundation() << " " << it->component()
1888 << " " << it->protocol() << " " << it->priority() << " "
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001889 << it->address().ipaddr().ToString() << " "
Yves Gerey665174f2018-06-19 15:03:05 +02001890 << it->address().PortAsString() << " " << kAttributeCandidateTyp << " "
mallinath@webrtc.org2d60c5e2014-08-08 22:29:20 +00001891 << type << " ";
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001892
1893 // Related address
1894 if (!it->related_address().IsNil()) {
1895 os << kAttributeCandidateRaddr << " "
1896 << it->related_address().ipaddr().ToString() << " "
1897 << kAttributeCandidateRport << " "
1898 << it->related_address().PortAsString() << " ";
1899 }
1900
mallinath@webrtc.org2d60c5e2014-08-08 22:29:20 +00001901 if (it->protocol() == cricket::TCP_PROTOCOL_NAME) {
mallinath@webrtc.orge999bd02014-08-13 06:05:55 +00001902 os << kTcpCandidateType << " " << it->tcptype() << " ";
mallinath@webrtc.org2d60c5e2014-08-08 22:29:20 +00001903 }
1904
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001905 // Extensions
1906 os << kAttributeCandidateGeneration << " " << it->generation();
honghaiza54a0802015-12-16 18:37:23 -08001907 if (include_ufrag && !it->username().empty()) {
1908 os << " " << kAttributeCandidateUfrag << " " << it->username();
1909 }
honghaiza0c44ea2016-03-23 16:07:48 -07001910 if (it->network_id() > 0) {
1911 os << " " << kAttributeCandidateNetworkId << " " << it->network_id();
1912 }
honghaize1a0c942016-02-16 14:54:56 -08001913 if (it->network_cost() > 0) {
1914 os << " " << kAttributeCandidateNetworkCost << " " << it->network_cost();
1915 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001916
1917 AddLine(os.str(), message);
1918 }
1919}
1920
1921void BuildIceOptions(const std::vector<std::string>& transport_options,
1922 std::string* message) {
1923 if (!transport_options.empty()) {
1924 std::ostringstream os;
1925 InitAttrLine(kAttributeIceOption, &os);
1926 os << kSdpDelimiterColon << transport_options[0];
1927 for (size_t i = 1; i < transport_options.size(); ++i) {
1928 os << kSdpDelimiterSpace << transport_options[i];
1929 }
1930 AddLine(os.str(), message);
1931 }
1932}
1933
pthatcher@webrtc.org3341b402015-02-13 21:14:22 +00001934bool IsRtp(const std::string& protocol) {
1935 return protocol.empty() ||
Yves Gerey665174f2018-06-19 15:03:05 +02001936 (protocol.find(cricket::kMediaProtocolRtpPrefix) != std::string::npos);
pthatcher@webrtc.org3341b402015-02-13 21:14:22 +00001937}
1938
1939bool IsDtlsSctp(const std::string& protocol) {
1940 // This intentionally excludes "SCTP" and "SCTP/DTLS".
lally@webrtc.orga7470932015-02-24 20:19:21 +00001941 return protocol.find(cricket::kMediaProtocolDtlsSctp) != std::string::npos;
pthatcher@webrtc.org3341b402015-02-13 21:14:22 +00001942}
1943
zhihuang38989e52017-03-21 11:04:53 -07001944bool ParseConnectionData(const std::string& line,
1945 rtc::SocketAddress* addr,
1946 SdpParseError* error) {
1947 // Parse the line from left to right.
1948 std::string token;
1949 std::string rightpart;
1950 // RFC 4566
1951 // c=<nettype> <addrtype> <connection-address>
1952 // Skip the "c="
1953 if (!rtc::tokenize_first(line, kSdpDelimiterEqual, &token, &rightpart)) {
1954 return ParseFailed(line, "Failed to parse the network type.", error);
1955 }
1956
1957 // Extract and verify the <nettype>
1958 if (!rtc::tokenize_first(rightpart, kSdpDelimiterSpace, &token, &rightpart) ||
1959 token != kConnectionNettype) {
1960 return ParseFailed(line,
1961 "Failed to parse the connection data. The network type "
1962 "is not currently supported.",
1963 error);
1964 }
1965
1966 // Extract the "<addrtype>" and "<connection-address>".
1967 if (!rtc::tokenize_first(rightpart, kSdpDelimiterSpace, &token, &rightpart)) {
1968 return ParseFailed(line, "Failed to parse the address type.", error);
1969 }
1970
1971 // The rightpart part should be the IP address without the slash which is used
1972 // for multicast.
1973 if (rightpart.find('/') != std::string::npos) {
1974 return ParseFailed(line,
1975 "Failed to parse the connection data. Multicast is not "
1976 "currently supported.",
1977 error);
1978 }
1979 addr->SetIP(rightpart);
1980
1981 // Verify that the addrtype matches the type of the parsed address.
1982 if ((addr->family() == AF_INET && token != "IP4") ||
1983 (addr->family() == AF_INET6 && token != "IP6")) {
1984 addr->Clear();
1985 return ParseFailed(
1986 line,
1987 "Failed to parse the connection data. The address type is mismatching.",
1988 error);
1989 }
1990 return true;
1991}
1992
1993bool ParseSessionDescription(const std::string& message,
1994 size_t* pos,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001995 std::string* session_id,
1996 std::string* session_version,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001997 TransportDescription* session_td,
1998 RtpHeaderExtensions* session_extmaps,
zhihuang38989e52017-03-21 11:04:53 -07001999 rtc::SocketAddress* connection_addr,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002000 cricket::SessionDescription* desc,
2001 SdpParseError* error) {
2002 std::string line;
2003
deadbeefc80741f2015-10-22 13:14:45 -07002004 desc->set_msid_supported(false);
2005
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002006 // RFC 4566
2007 // v= (protocol version)
2008 if (!GetLineWithType(message, pos, &line, kLineTypeVersion)) {
Yves Gerey665174f2018-06-19 15:03:05 +02002009 return ParseFailedExpectLine(message, *pos, kLineTypeVersion, std::string(),
2010 error);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002011 }
2012 // RFC 4566
2013 // o=<username> <sess-id> <sess-version> <nettype> <addrtype>
2014 // <unicast-address>
2015 if (!GetLineWithType(message, pos, &line, kLineTypeOrigin)) {
Yves Gerey665174f2018-06-19 15:03:05 +02002016 return ParseFailedExpectLine(message, *pos, kLineTypeOrigin, std::string(),
2017 error);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002018 }
2019 std::vector<std::string> fields;
Yves Gerey665174f2018-06-19 15:03:05 +02002020 rtc::split(line.substr(kLinePrefixLength), kSdpDelimiterSpace, &fields);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002021 const size_t expected_fields = 6;
2022 if (fields.size() != expected_fields) {
2023 return ParseFailedExpectFieldNum(line, expected_fields, error);
2024 }
2025 *session_id = fields[1];
2026 *session_version = fields[2];
2027
2028 // RFC 4566
2029 // s= (session name)
2030 if (!GetLineWithType(message, pos, &line, kLineTypeSessionName)) {
2031 return ParseFailedExpectLine(message, *pos, kLineTypeSessionName,
2032 std::string(), error);
2033 }
2034
Danil Chapovalov66cadcc2018-06-19 16:47:43 +02002035 // absl::optional lines
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002036 // Those are the optional lines, so shouldn't return false if not present.
2037 // RFC 4566
2038 // i=* (session information)
2039 GetLineWithType(message, pos, &line, kLineTypeSessionInfo);
2040
2041 // RFC 4566
2042 // u=* (URI of description)
2043 GetLineWithType(message, pos, &line, kLineTypeSessionUri);
2044
2045 // RFC 4566
2046 // e=* (email address)
2047 GetLineWithType(message, pos, &line, kLineTypeSessionEmail);
2048
2049 // RFC 4566
2050 // p=* (phone number)
2051 GetLineWithType(message, pos, &line, kLineTypeSessionPhone);
2052
2053 // RFC 4566
2054 // c=* (connection information -- not required if included in
2055 // all media)
zhihuang38989e52017-03-21 11:04:53 -07002056 if (GetLineWithType(message, pos, &line, kLineTypeConnection)) {
2057 if (!ParseConnectionData(line, connection_addr, error)) {
2058 return false;
2059 }
2060 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002061
2062 // RFC 4566
2063 // b=* (zero or more bandwidth information lines)
2064 while (GetLineWithType(message, pos, &line, kLineTypeSessionBandwidth)) {
2065 // By pass zero or more b lines.
2066 }
2067
2068 // RFC 4566
2069 // One or more time descriptions ("t=" and "r=" lines; see below)
2070 // t= (time the session is active)
2071 // r=* (zero or more repeat times)
2072 // Ensure there's at least one time description
2073 if (!GetLineWithType(message, pos, &line, kLineTypeTiming)) {
2074 return ParseFailedExpectLine(message, *pos, kLineTypeTiming, std::string(),
2075 error);
2076 }
2077
2078 while (GetLineWithType(message, pos, &line, kLineTypeRepeatTimes)) {
2079 // By pass zero or more r lines.
2080 }
2081
2082 // Go through the rest of the time descriptions
2083 while (GetLineWithType(message, pos, &line, kLineTypeTiming)) {
2084 while (GetLineWithType(message, pos, &line, kLineTypeRepeatTimes)) {
2085 // By pass zero or more r lines.
2086 }
2087 }
2088
2089 // RFC 4566
2090 // z=* (time zone adjustments)
2091 GetLineWithType(message, pos, &line, kLineTypeTimeZone);
2092
2093 // RFC 4566
2094 // k=* (encryption key)
2095 GetLineWithType(message, pos, &line, kLineTypeEncryptionKey);
2096
2097 // RFC 4566
2098 // a=* (zero or more session attribute lines)
2099 while (GetLineWithType(message, pos, &line, kLineTypeAttributes)) {
2100 if (HasAttribute(line, kAttributeGroup)) {
2101 if (!ParseGroupAttribute(line, desc, error)) {
2102 return false;
2103 }
2104 } else if (HasAttribute(line, kAttributeIceUfrag)) {
Yves Gerey665174f2018-06-19 15:03:05 +02002105 if (!GetValue(line, kAttributeIceUfrag, &(session_td->ice_ufrag),
2106 error)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002107 return false;
2108 }
2109 } else if (HasAttribute(line, kAttributeIcePwd)) {
2110 if (!GetValue(line, kAttributeIcePwd, &(session_td->ice_pwd), error)) {
2111 return false;
2112 }
2113 } else if (HasAttribute(line, kAttributeIceLite)) {
2114 session_td->ice_mode = cricket::ICEMODE_LITE;
2115 } else if (HasAttribute(line, kAttributeIceOption)) {
2116 if (!ParseIceOptions(line, &(session_td->transport_options), error)) {
2117 return false;
2118 }
2119 } else if (HasAttribute(line, kAttributeFingerprint)) {
2120 if (session_td->identity_fingerprint.get()) {
2121 return ParseFailed(
2122 line,
2123 "Can't have multiple fingerprint attributes at the same level.",
2124 error);
2125 }
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002126 rtc::SSLFingerprint* fingerprint = NULL;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002127 if (!ParseFingerprintAttribute(line, &fingerprint, error)) {
2128 return false;
2129 }
2130 session_td->identity_fingerprint.reset(fingerprint);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00002131 } else if (HasAttribute(line, kAttributeSetup)) {
2132 if (!ParseDtlsSetup(line, &(session_td->connection_role), error)) {
2133 return false;
2134 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002135 } else if (HasAttribute(line, kAttributeMsidSemantics)) {
2136 std::string semantics;
2137 if (!GetValue(line, kAttributeMsidSemantics, &semantics, error)) {
2138 return false;
2139 }
deadbeefc80741f2015-10-22 13:14:45 -07002140 desc->set_msid_supported(
2141 CaseInsensitiveFind(semantics, kMediaStreamSemantic));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002142 } else if (HasAttribute(line, kAttributeExtmap)) {
isheriff6f8d6862016-05-26 11:24:55 -07002143 RtpExtension extmap;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002144 if (!ParseExtmap(line, &extmap, error)) {
2145 return false;
2146 }
2147 session_extmaps->push_back(extmap);
2148 }
2149 }
2150
2151 return true;
2152}
2153
2154bool ParseGroupAttribute(const std::string& line,
2155 cricket::SessionDescription* desc,
2156 SdpParseError* error) {
nisseede5da42017-01-12 05:15:36 -08002157 RTC_DCHECK(desc != NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002158
2159 // RFC 5888 and draft-holmberg-mmusic-sdp-bundle-negotiation-00
2160 // a=group:BUNDLE video voice
2161 std::vector<std::string> fields;
Yves Gerey665174f2018-06-19 15:03:05 +02002162 rtc::split(line.substr(kLinePrefixLength), kSdpDelimiterSpace, &fields);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002163 std::string semantics;
2164 if (!GetValue(fields[0], kAttributeGroup, &semantics, error)) {
2165 return false;
2166 }
2167 cricket::ContentGroup group(semantics);
2168 for (size_t i = 1; i < fields.size(); ++i) {
2169 group.AddContentName(fields[i]);
2170 }
2171 desc->AddGroup(group);
2172 return true;
2173}
2174
2175static bool ParseFingerprintAttribute(const std::string& line,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002176 rtc::SSLFingerprint** fingerprint,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002177 SdpParseError* error) {
2178 if (!IsLineType(line, kLineTypeAttributes) ||
2179 !HasAttribute(line, kAttributeFingerprint)) {
2180 return ParseFailedExpectLine(line, 0, kLineTypeAttributes,
2181 kAttributeFingerprint, error);
2182 }
2183
2184 std::vector<std::string> fields;
Yves Gerey665174f2018-06-19 15:03:05 +02002185 rtc::split(line.substr(kLinePrefixLength), kSdpDelimiterSpace, &fields);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002186 const size_t expected_fields = 2;
2187 if (fields.size() != expected_fields) {
2188 return ParseFailedExpectFieldNum(line, expected_fields, error);
2189 }
2190
2191 // The first field here is "fingerprint:<hash>.
2192 std::string algorithm;
2193 if (!GetValue(fields[0], kAttributeFingerprint, &algorithm, error)) {
2194 return false;
2195 }
2196
2197 // Downcase the algorithm. Note that we don't need to downcase the
2198 // fingerprint because hex_decode can handle upper-case.
2199 std::transform(algorithm.begin(), algorithm.end(), algorithm.begin(),
2200 ::tolower);
2201
2202 // The second field is the digest value. De-hexify it.
Yves Gerey665174f2018-06-19 15:03:05 +02002203 *fingerprint = rtc::SSLFingerprint::CreateFromRfc4572(algorithm, fields[1]);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002204 if (!*fingerprint) {
Yves Gerey665174f2018-06-19 15:03:05 +02002205 return ParseFailed(line, "Failed to create fingerprint from the digest.",
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002206 error);
2207 }
2208
2209 return true;
2210}
2211
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00002212static bool ParseDtlsSetup(const std::string& line,
2213 cricket::ConnectionRole* role,
2214 SdpParseError* error) {
2215 // setup-attr = "a=setup:" role
2216 // role = "active" / "passive" / "actpass" / "holdconn"
2217 std::vector<std::string> fields;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002218 rtc::split(line.substr(kLinePrefixLength), kSdpDelimiterColon, &fields);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00002219 const size_t expected_fields = 2;
2220 if (fields.size() != expected_fields) {
2221 return ParseFailedExpectFieldNum(line, expected_fields, error);
2222 }
2223 std::string role_str = fields[1];
2224 if (!cricket::StringToConnectionRole(role_str, role)) {
2225 return ParseFailed(line, "Invalid attribute value.", error);
2226 }
2227 return true;
2228}
2229
deadbeef9d3584c2016-02-16 17:54:10 -08002230static bool ParseMsidAttribute(const std::string& line,
Seth Hampson5b4f0752018-04-02 16:31:36 -07002231 std::vector<std::string>* stream_ids,
deadbeef9d3584c2016-02-16 17:54:10 -08002232 std::string* track_id,
2233 SdpParseError* error) {
Seth Hampson5b4f0752018-04-02 16:31:36 -07002234 // https://datatracker.ietf.org/doc/draft-ietf-mmusic-msid/16/
deadbeef9d3584c2016-02-16 17:54:10 -08002235 // a=msid:<stream id> <track id>
2236 // msid-value = msid-id [ SP msid-appdata ]
2237 // msid-id = 1*64token-char ; see RFC 4566
2238 // msid-appdata = 1*64token-char ; see RFC 4566
2239 std::string field1;
Seth Hampson5b4f0752018-04-02 16:31:36 -07002240 std::string new_stream_id;
2241 std::string new_track_id;
deadbeef9d3584c2016-02-16 17:54:10 -08002242 if (!rtc::tokenize_first(line.substr(kLinePrefixLength), kSdpDelimiterSpace,
Seth Hampson5b4f0752018-04-02 16:31:36 -07002243 &field1, &new_track_id)) {
deadbeef9d3584c2016-02-16 17:54:10 -08002244 const size_t expected_fields = 2;
2245 return ParseFailedExpectFieldNum(line, expected_fields, error);
2246 }
2247
Seth Hampson5b4f0752018-04-02 16:31:36 -07002248 if (new_track_id.empty()) {
deadbeefa4549d62017-02-10 17:26:22 -08002249 return ParseFailed(line, "Missing track ID in msid attribute.", error);
2250 }
Seth Hampson5b4f0752018-04-02 16:31:36 -07002251 // All track ids should be the same within an m section in a Unified Plan SDP.
2252 if (!track_id->empty() && new_track_id.compare(*track_id) != 0) {
2253 return ParseFailed(
2254 line, "Two different track IDs in msid attribute in one m= section",
2255 error);
2256 }
2257 *track_id = new_track_id;
deadbeefa4549d62017-02-10 17:26:22 -08002258
deadbeef9d3584c2016-02-16 17:54:10 -08002259 // msid:<msid-id>
Seth Hampson5b4f0752018-04-02 16:31:36 -07002260 if (!GetValue(field1, kAttributeMsid, &new_stream_id, error)) {
deadbeef9d3584c2016-02-16 17:54:10 -08002261 return false;
2262 }
Seth Hampson5b4f0752018-04-02 16:31:36 -07002263 if (new_stream_id.empty()) {
deadbeefa4549d62017-02-10 17:26:22 -08002264 return ParseFailed(line, "Missing stream ID in msid attribute.", error);
2265 }
Seth Hampson5b4f0752018-04-02 16:31:36 -07002266 // The special value "-" indicates "no MediaStream".
2267 if (new_stream_id.compare(kNoStreamMsid) != 0) {
2268 stream_ids->push_back(new_stream_id);
2269 }
deadbeef9d3584c2016-02-16 17:54:10 -08002270 return true;
2271}
2272
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002273// RFC 3551
2274// PT encoding media type clock rate channels
2275// name (Hz)
2276// 0 PCMU A 8,000 1
2277// 1 reserved A
2278// 2 reserved A
2279// 3 GSM A 8,000 1
2280// 4 G723 A 8,000 1
2281// 5 DVI4 A 8,000 1
2282// 6 DVI4 A 16,000 1
2283// 7 LPC A 8,000 1
2284// 8 PCMA A 8,000 1
2285// 9 G722 A 8,000 1
2286// 10 L16 A 44,100 2
2287// 11 L16 A 44,100 1
2288// 12 QCELP A 8,000 1
2289// 13 CN A 8,000 1
2290// 14 MPA A 90,000 (see text)
2291// 15 G728 A 8,000 1
2292// 16 DVI4 A 11,025 1
2293// 17 DVI4 A 22,050 1
2294// 18 G729 A 8,000 1
2295struct StaticPayloadAudioCodec {
2296 const char* name;
2297 int clockrate;
Peter Kasting69558702016-01-12 16:26:35 -08002298 size_t channels;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002299};
2300static const StaticPayloadAudioCodec kStaticPayloadAudioCodecs[] = {
Yves Gerey665174f2018-06-19 15:03:05 +02002301 {"PCMU", 8000, 1}, {"reserved", 0, 0}, {"reserved", 0, 0},
2302 {"GSM", 8000, 1}, {"G723", 8000, 1}, {"DVI4", 8000, 1},
2303 {"DVI4", 16000, 1}, {"LPC", 8000, 1}, {"PCMA", 8000, 1},
2304 {"G722", 8000, 1}, {"L16", 44100, 2}, {"L16", 44100, 1},
2305 {"QCELP", 8000, 1}, {"CN", 8000, 1}, {"MPA", 90000, 1},
2306 {"G728", 8000, 1}, {"DVI4", 11025, 1}, {"DVI4", 22050, 1},
2307 {"G729", 8000, 1},
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002308};
2309
Yves Gerey665174f2018-06-19 15:03:05 +02002310void MaybeCreateStaticPayloadAudioCodecs(const std::vector<int>& fmts,
2311 AudioContentDescription* media_desc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002312 if (!media_desc) {
2313 return;
2314 }
deadbeef67cf2c12016-04-13 10:07:16 -07002315 RTC_DCHECK(media_desc->codecs().empty());
solenberg9fa49752016-10-08 13:02:44 -07002316 for (int payload_type : fmts) {
Yves Gerey665174f2018-06-19 15:03:05 +02002317 if (!media_desc->HasCodec(payload_type) && payload_type >= 0 &&
kjellander3e33bfe2016-06-20 07:04:09 -07002318 static_cast<uint32_t>(payload_type) <
2319 arraysize(kStaticPayloadAudioCodecs)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002320 std::string encoding_name = kStaticPayloadAudioCodecs[payload_type].name;
2321 int clock_rate = kStaticPayloadAudioCodecs[payload_type].clockrate;
Peter Kasting69558702016-01-12 16:26:35 -08002322 size_t channels = kStaticPayloadAudioCodecs[payload_type].channels;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002323 media_desc->AddCodec(cricket::AudioCodec(payload_type, encoding_name,
deadbeef67cf2c12016-04-13 10:07:16 -07002324 clock_rate, 0, channels));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002325 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002326 }
2327}
2328
2329template <class C>
2330static C* ParseContentDescription(const std::string& message,
2331 const MediaType media_type,
2332 int mline_index,
2333 const std::string& protocol,
deadbeef67cf2c12016-04-13 10:07:16 -07002334 const std::vector<int>& payload_types,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002335 size_t* pos,
2336 std::string* content_name,
deadbeef25ed4352016-12-12 18:37:36 -08002337 bool* bundle_only,
Steve Antone831b8c2018-02-01 12:22:16 -08002338 int* msid_signaling,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002339 TransportDescription* transport,
2340 std::vector<JsepIceCandidate*>* candidates,
2341 webrtc::SdpParseError* error) {
2342 C* media_desc = new C();
2343 switch (media_type) {
2344 case cricket::MEDIA_TYPE_AUDIO:
2345 *content_name = cricket::CN_AUDIO;
2346 break;
2347 case cricket::MEDIA_TYPE_VIDEO:
2348 *content_name = cricket::CN_VIDEO;
2349 break;
2350 case cricket::MEDIA_TYPE_DATA:
2351 *content_name = cricket::CN_DATA;
2352 break;
2353 default:
nissec80e7412017-01-11 05:56:46 -08002354 RTC_NOTREACHED();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002355 break;
2356 }
deadbeef67cf2c12016-04-13 10:07:16 -07002357 if (!ParseContent(message, media_type, mline_index, protocol, payload_types,
Steve Antone831b8c2018-02-01 12:22:16 -08002358 pos, content_name, bundle_only, msid_signaling, media_desc,
2359 transport, candidates, error)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002360 delete media_desc;
zhihuang38989e52017-03-21 11:04:53 -07002361 return nullptr;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002362 }
2363 // Sort the codecs according to the m-line fmt list.
deadbeef67cf2c12016-04-13 10:07:16 -07002364 std::unordered_map<int, int> payload_type_preferences;
2365 // "size + 1" so that the lowest preference payload type has a preference of
2366 // 1, which is greater than the default (0) for payload types not in the fmt
2367 // list.
2368 int preference = static_cast<int>(payload_types.size() + 1);
2369 for (int pt : payload_types) {
2370 payload_type_preferences[pt] = preference--;
2371 }
2372 std::vector<typename C::CodecType> codecs = media_desc->codecs();
Yves Gerey665174f2018-06-19 15:03:05 +02002373 std::sort(codecs.begin(), codecs.end(),
2374 [&payload_type_preferences](const typename C::CodecType& a,
2375 const typename C::CodecType& b) {
2376 return payload_type_preferences[a.id] >
2377 payload_type_preferences[b.id];
2378 });
deadbeef67cf2c12016-04-13 10:07:16 -07002379 media_desc->set_codecs(codecs);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002380 return media_desc;
2381}
2382
2383bool ParseMediaDescription(const std::string& message,
2384 const TransportDescription& session_td,
2385 const RtpHeaderExtensions& session_extmaps,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002386 size_t* pos,
zhihuang38989e52017-03-21 11:04:53 -07002387 const rtc::SocketAddress& session_connection_addr,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002388 cricket::SessionDescription* desc,
2389 std::vector<JsepIceCandidate*>* candidates,
2390 SdpParseError* error) {
nisseede5da42017-01-12 05:15:36 -08002391 RTC_DCHECK(desc != NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002392 std::string line;
2393 int mline_index = -1;
Steve Antone831b8c2018-02-01 12:22:16 -08002394 int msid_signaling = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002395
2396 // Zero or more media descriptions
2397 // RFC 4566
2398 // m=<media> <port> <proto> <fmt>
2399 while (GetLineWithType(message, pos, &line, kLineTypeMedia)) {
2400 ++mline_index;
2401
2402 std::vector<std::string> fields;
Yves Gerey665174f2018-06-19 15:03:05 +02002403 rtc::split(line.substr(kLinePrefixLength), kSdpDelimiterSpace, &fields);
zstein4b2e0822017-02-17 19:48:38 -08002404
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002405 const size_t expected_min_fields = 4;
2406 if (fields.size() < expected_min_fields) {
2407 return ParseFailedExpectMinFieldNum(line, expected_min_fields, error);
2408 }
deadbeef25ed4352016-12-12 18:37:36 -08002409 bool port_rejected = false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002410 // RFC 3264
2411 // To reject an offered stream, the port number in the corresponding stream
2412 // in the answer MUST be set to zero.
2413 if (fields[1] == kMediaPortRejected) {
deadbeef25ed4352016-12-12 18:37:36 -08002414 port_rejected = true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002415 }
2416
zhihuang38989e52017-03-21 11:04:53 -07002417 int port = 0;
2418 if (!rtc::FromString<int>(fields[1], &port) || !IsValidPort(port)) {
2419 return ParseFailed(line, "The port number is invalid", error);
2420 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002421 std::string protocol = fields[2];
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002422
2423 // <fmt>
deadbeef67cf2c12016-04-13 10:07:16 -07002424 std::vector<int> payload_types;
pthatcher@webrtc.org3341b402015-02-13 21:14:22 +00002425 if (IsRtp(protocol)) {
Yves Gerey665174f2018-06-19 15:03:05 +02002426 for (size_t j = 3; j < fields.size(); ++j) {
jiayl@webrtc.orgddb85ab2014-09-05 16:31:56 +00002427 // TODO(wu): Remove when below bug is fixed.
2428 // https://bugzilla.mozilla.org/show_bug.cgi?id=996329
pbosbb36fdf2015-07-09 07:48:14 -07002429 if (fields[j].empty() && j == fields.size() - 1) {
jiayl@webrtc.orgddb85ab2014-09-05 16:31:56 +00002430 continue;
2431 }
wu@webrtc.org36eda7c2014-04-15 20:37:30 +00002432
jiayl@webrtc.orgddb85ab2014-09-05 16:31:56 +00002433 int pl = 0;
pkasting@chromium.orge9facf82015-02-17 20:36:28 +00002434 if (!GetPayloadTypeFromString(line, fields[j], &pl, error)) {
jiayl@webrtc.orgddb85ab2014-09-05 16:31:56 +00002435 return false;
2436 }
deadbeef67cf2c12016-04-13 10:07:16 -07002437 payload_types.push_back(pl);
wu@webrtc.org5e760e72014-04-02 23:19:09 +00002438 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002439 }
2440
2441 // Make a temporary TransportDescription based on |session_td|.
2442 // Some of this gets overwritten by ParseContent.
deadbeef46eed762016-01-28 13:24:37 -08002443 TransportDescription transport(
2444 session_td.transport_options, session_td.ice_ufrag, session_td.ice_pwd,
2445 session_td.ice_mode, session_td.connection_role,
2446 session_td.identity_fingerprint.get());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002447
kwibergd1fe2812016-04-27 06:47:29 -07002448 std::unique_ptr<MediaContentDescription> content;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002449 std::string content_name;
deadbeef25ed4352016-12-12 18:37:36 -08002450 bool bundle_only = false;
Steve Antone831b8c2018-02-01 12:22:16 -08002451 int section_msid_signaling = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002452 if (HasAttribute(line, kMediaTypeVideo)) {
2453 content.reset(ParseContentDescription<VideoContentDescription>(
deadbeef67cf2c12016-04-13 10:07:16 -07002454 message, cricket::MEDIA_TYPE_VIDEO, mline_index, protocol,
Steve Antone831b8c2018-02-01 12:22:16 -08002455 payload_types, pos, &content_name, &bundle_only,
2456 &section_msid_signaling, &transport, candidates, error));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002457 } else if (HasAttribute(line, kMediaTypeAudio)) {
2458 content.reset(ParseContentDescription<AudioContentDescription>(
deadbeef67cf2c12016-04-13 10:07:16 -07002459 message, cricket::MEDIA_TYPE_AUDIO, mline_index, protocol,
Steve Antone831b8c2018-02-01 12:22:16 -08002460 payload_types, pos, &content_name, &bundle_only,
2461 &section_msid_signaling, &transport, candidates, error));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002462 } else if (HasAttribute(line, kMediaTypeData)) {
jiayl@webrtc.org0e527722014-09-08 21:43:43 +00002463 DataContentDescription* data_desc =
wu@webrtc.org78187522013-10-07 23:32:02 +00002464 ParseContentDescription<DataContentDescription>(
deadbeef67cf2c12016-04-13 10:07:16 -07002465 message, cricket::MEDIA_TYPE_DATA, mline_index, protocol,
Steve Antone831b8c2018-02-01 12:22:16 -08002466 payload_types, pos, &content_name, &bundle_only,
2467 &section_msid_signaling, &transport, candidates, error);
jiayl@webrtc.org0e527722014-09-08 21:43:43 +00002468 content.reset(data_desc);
wu@webrtc.org78187522013-10-07 23:32:02 +00002469
zstein4b2e0822017-02-17 19:48:38 -08002470 if (data_desc && IsDtlsSctp(protocol)) {
2471 int p;
2472 if (rtc::FromString(fields[3], &p)) {
2473 if (!AddSctpDataCodec(data_desc, p)) {
2474 return false;
2475 }
2476 } else if (fields[3] == kDefaultSctpmapProtocol) {
2477 data_desc->set_use_sctpmap(false);
2478 }
wu@webrtc.org78187522013-10-07 23:32:02 +00002479 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002480 } else {
Mirko Bonadei675513b2017-11-09 11:09:25 +01002481 RTC_LOG(LS_WARNING) << "Unsupported media type: " << line;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002482 continue;
2483 }
2484 if (!content.get()) {
2485 // ParseContentDescription returns NULL if failed.
2486 return false;
2487 }
2488
Steve Antone831b8c2018-02-01 12:22:16 -08002489 msid_signaling |= section_msid_signaling;
2490
deadbeef25ed4352016-12-12 18:37:36 -08002491 bool content_rejected = false;
deadbeef12771a12017-01-03 13:53:47 -08002492 // A port of 0 is not interpreted as a rejected m= section when it's
2493 // used along with a=bundle-only.
deadbeef25ed4352016-12-12 18:37:36 -08002494 if (bundle_only) {
deadbeef25ed4352016-12-12 18:37:36 -08002495 if (!port_rejected) {
deadbeef12771a12017-01-03 13:53:47 -08002496 // Usage of bundle-only with a nonzero port is unspecified. So just
2497 // ignore bundle-only if we see this.
2498 bundle_only = false;
Mirko Bonadei675513b2017-11-09 11:09:25 +01002499 RTC_LOG(LS_WARNING)
deadbeef12771a12017-01-03 13:53:47 -08002500 << "a=bundle-only attribute observed with a nonzero "
Jonas Olsson45cc8902018-02-13 10:37:07 +01002501 "port; this usage is unspecified so the attribute is being "
2502 "ignored.";
deadbeef25ed4352016-12-12 18:37:36 -08002503 }
2504 } else {
2505 // If not using bundle-only, interpret port 0 in the normal way; the m=
2506 // section is being rejected.
2507 content_rejected = port_rejected;
2508 }
2509
pthatcher@webrtc.org3341b402015-02-13 21:14:22 +00002510 if (IsRtp(protocol)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002511 // Set the extmap.
2512 if (!session_extmaps.empty() &&
2513 !content->rtp_header_extensions().empty()) {
2514 return ParseFailed("",
2515 "The a=extmap MUST be either all session level or "
2516 "all media level.",
2517 error);
2518 }
2519 for (size_t i = 0; i < session_extmaps.size(); ++i) {
2520 content->AddRtpHeaderExtension(session_extmaps[i]);
2521 }
2522 }
2523 content->set_protocol(protocol);
zhihuang38989e52017-03-21 11:04:53 -07002524
2525 // Use the session level connection address if the media level addresses are
2526 // not specified.
2527 rtc::SocketAddress address;
2528 address = content->connection_address().IsNil()
2529 ? session_connection_addr
2530 : content->connection_address();
2531 address.SetPort(port);
2532 content->set_connection_address(address);
2533
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002534 desc->AddContent(content_name,
Steve Anton5adfafd2017-12-20 16:34:00 -08002535 IsDtlsSctp(protocol) ? MediaProtocolType::kSctp
2536 : MediaProtocolType::kRtp,
deadbeef25ed4352016-12-12 18:37:36 -08002537 content_rejected, bundle_only, content.release());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002538 // Create TransportInfo with the media level "ice-pwd" and "ice-ufrag".
2539 TransportInfo transport_info(content_name, transport);
2540
2541 if (!desc->AddTransportInfo(transport_info)) {
2542 std::ostringstream description;
2543 description << "Failed to AddTransportInfo with content name: "
2544 << content_name;
2545 return ParseFailed("", description.str(), error);
2546 }
2547 }
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00002548
Steve Antone831b8c2018-02-01 12:22:16 -08002549 desc->set_msid_signaling(msid_signaling);
2550
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00002551 size_t end_of_message = message.size();
2552 if (mline_index == -1 && *pos != end_of_message) {
2553 ParseFailed(message, *pos, "Expects m line.", error);
2554 return false;
2555 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002556 return true;
2557}
2558
2559bool VerifyCodec(const cricket::Codec& codec) {
2560 // Codec has not been populated correctly unless the name has been set. This
2561 // can happen if an SDP has an fmtp or rtcp-fb with a payload type but doesn't
2562 // have a corresponding "rtpmap" line.
htab39db842016-12-08 01:50:48 -08002563 return !codec.name.empty();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002564}
2565
2566bool VerifyAudioCodecs(const AudioContentDescription* audio_desc) {
2567 const std::vector<cricket::AudioCodec>& codecs = audio_desc->codecs();
2568 for (std::vector<cricket::AudioCodec>::const_iterator iter = codecs.begin();
2569 iter != codecs.end(); ++iter) {
2570 if (!VerifyCodec(*iter)) {
2571 return false;
2572 }
2573 }
2574 return true;
2575}
2576
2577bool VerifyVideoCodecs(const VideoContentDescription* video_desc) {
2578 const std::vector<cricket::VideoCodec>& codecs = video_desc->codecs();
2579 for (std::vector<cricket::VideoCodec>::const_iterator iter = codecs.begin();
2580 iter != codecs.end(); ++iter) {
2581 if (!VerifyCodec(*iter)) {
2582 return false;
2583 }
2584 }
2585 return true;
2586}
2587
2588void AddParameters(const cricket::CodecParameterMap& parameters,
2589 cricket::Codec* codec) {
Yves Gerey665174f2018-06-19 15:03:05 +02002590 for (cricket::CodecParameterMap::const_iterator iter = parameters.begin();
2591 iter != parameters.end(); ++iter) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002592 codec->SetParam(iter->first, iter->second);
2593 }
2594}
2595
2596void AddFeedbackParameter(const cricket::FeedbackParam& feedback_param,
2597 cricket::Codec* codec) {
2598 codec->AddFeedbackParam(feedback_param);
2599}
2600
2601void AddFeedbackParameters(const cricket::FeedbackParams& feedback_params,
2602 cricket::Codec* codec) {
2603 for (std::vector<cricket::FeedbackParam>::const_iterator iter =
2604 feedback_params.params().begin();
2605 iter != feedback_params.params().end(); ++iter) {
2606 codec->AddFeedbackParam(*iter);
2607 }
2608}
2609
2610// Gets the current codec setting associated with |payload_type|. If there
changbin.shao@webrtc.org2d25b442015-03-16 04:14:34 +00002611// is no Codec associated with that payload type it returns an empty codec
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002612// with that payload type.
2613template <class T>
changbin.shao@webrtc.org2d25b442015-03-16 04:14:34 +00002614T GetCodecWithPayloadType(const std::vector<T>& codecs, int payload_type) {
magjedb05fa242016-11-11 04:00:16 -08002615 const T* codec = FindCodecById(codecs, payload_type);
2616 if (codec)
2617 return *codec;
2618 // Return empty codec with |payload_type|.
changbin.shao@webrtc.org2d25b442015-03-16 04:14:34 +00002619 T ret_val;
magjedb05fa242016-11-11 04:00:16 -08002620 ret_val.id = payload_type;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002621 return ret_val;
2622}
2623
2624// Updates or creates a new codec entry in the audio description.
2625template <class T, class U>
2626void AddOrReplaceCodec(MediaContentDescription* content_desc, const U& codec) {
2627 T* desc = static_cast<T*>(content_desc);
2628 std::vector<U> codecs = desc->codecs();
2629 bool found = false;
2630
2631 typename std::vector<U>::iterator iter;
2632 for (iter = codecs.begin(); iter != codecs.end(); ++iter) {
2633 if (iter->id == codec.id) {
2634 *iter = codec;
2635 found = true;
2636 break;
2637 }
2638 }
2639 if (!found) {
2640 desc->AddCodec(codec);
2641 return;
2642 }
2643 desc->set_codecs(codecs);
2644}
2645
2646// Adds or updates existing codec corresponding to |payload_type| according
2647// to |parameters|.
2648template <class T, class U>
Yves Gerey665174f2018-06-19 15:03:05 +02002649void UpdateCodec(MediaContentDescription* content_desc,
2650 int payload_type,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002651 const cricket::CodecParameterMap& parameters) {
2652 // Codec might already have been populated (from rtpmap).
changbin.shao@webrtc.org2d25b442015-03-16 04:14:34 +00002653 U new_codec = GetCodecWithPayloadType(static_cast<T*>(content_desc)->codecs(),
2654 payload_type);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002655 AddParameters(parameters, &new_codec);
2656 AddOrReplaceCodec<T, U>(content_desc, new_codec);
2657}
2658
2659// Adds or updates existing codec corresponding to |payload_type| according
2660// to |feedback_param|.
2661template <class T, class U>
Yves Gerey665174f2018-06-19 15:03:05 +02002662void UpdateCodec(MediaContentDescription* content_desc,
2663 int payload_type,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002664 const cricket::FeedbackParam& feedback_param) {
2665 // Codec might already have been populated (from rtpmap).
changbin.shao@webrtc.org2d25b442015-03-16 04:14:34 +00002666 U new_codec = GetCodecWithPayloadType(static_cast<T*>(content_desc)->codecs(),
2667 payload_type);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002668 AddFeedbackParameter(feedback_param, &new_codec);
2669 AddOrReplaceCodec<T, U>(content_desc, new_codec);
2670}
2671
jlmiller@webrtc.orga744a282015-02-18 21:37:46 +00002672template <class T>
2673bool PopWildcardCodec(std::vector<T>* codecs, T* wildcard_codec) {
2674 for (auto iter = codecs->begin(); iter != codecs->end(); ++iter) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002675 if (iter->id == kWildcardPayloadType) {
2676 *wildcard_codec = *iter;
2677 codecs->erase(iter);
2678 return true;
2679 }
2680 }
2681 return false;
2682}
2683
Yves Gerey665174f2018-06-19 15:03:05 +02002684template <class T>
jlmiller@webrtc.orga744a282015-02-18 21:37:46 +00002685void UpdateFromWildcardCodecs(cricket::MediaContentDescriptionImpl<T>* desc) {
2686 auto codecs = desc->codecs();
2687 T wildcard_codec;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002688 if (!PopWildcardCodec(&codecs, &wildcard_codec)) {
2689 return;
2690 }
jlmiller@webrtc.orga744a282015-02-18 21:37:46 +00002691 for (auto& codec : codecs) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002692 AddFeedbackParameters(wildcard_codec.feedback_params, &codec);
2693 }
jlmiller@webrtc.orga744a282015-02-18 21:37:46 +00002694 desc->set_codecs(codecs);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002695}
2696
Yves Gerey665174f2018-06-19 15:03:05 +02002697void AddAudioAttribute(const std::string& name,
2698 const std::string& value,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002699 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);
Yves Gerey665174f2018-06-19 15:03:05 +02003023 candidates->push_back(new JsepIceCandidate(mline_id, mline_index, *it));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003024 }
3025 return true;
3026}
3027
Steve Antone831b8c2018-02-01 12:22:16 -08003028bool ParseSsrcAttribute(const std::string& line,
3029 SsrcInfoVec* ssrc_infos,
3030 int* msid_signaling,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003031 SdpParseError* error) {
nisseede5da42017-01-12 05:15:36 -08003032 RTC_DCHECK(ssrc_infos != NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003033 // RFC 5576
3034 // a=ssrc:<ssrc-id> <attribute>
3035 // a=ssrc:<ssrc-id> <attribute>:<value>
3036 std::string field1, field2;
Donald Curtis144d0182015-05-15 13:14:24 -07003037 if (!rtc::tokenize_first(line.substr(kLinePrefixLength), kSdpDelimiterSpace,
3038 &field1, &field2)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003039 const size_t expected_fields = 2;
3040 return ParseFailedExpectFieldNum(line, expected_fields, error);
3041 }
3042
3043 // ssrc:<ssrc-id>
3044 std::string ssrc_id_s;
3045 if (!GetValue(field1, kAttributeSsrc, &ssrc_id_s, error)) {
3046 return false;
3047 }
Peter Boström0c4e06b2015-10-07 12:23:21 +02003048 uint32_t ssrc_id = 0;
wu@webrtc.org5e760e72014-04-02 23:19:09 +00003049 if (!GetValueFromString(line, ssrc_id_s, &ssrc_id, error)) {
3050 return false;
3051 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003052
3053 std::string attribute;
3054 std::string value;
Donald Curtis144d0182015-05-15 13:14:24 -07003055 if (!rtc::tokenize_first(field2, kSdpDelimiterColon, &attribute, &value)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003056 std::ostringstream description;
3057 description << "Failed to get the ssrc attribute value from " << field2
3058 << ". Expected format <attribute>:<value>.";
3059 return ParseFailed(line, description.str(), error);
3060 }
3061
3062 // Check if there's already an item for this |ssrc_id|. Create a new one if
3063 // there isn't.
3064 SsrcInfoVec::iterator ssrc_info = ssrc_infos->begin();
3065 for (; ssrc_info != ssrc_infos->end(); ++ssrc_info) {
3066 if (ssrc_info->ssrc_id == ssrc_id) {
3067 break;
3068 }
3069 }
3070 if (ssrc_info == ssrc_infos->end()) {
3071 SsrcInfo info;
3072 info.ssrc_id = ssrc_id;
3073 ssrc_infos->push_back(info);
3074 ssrc_info = ssrc_infos->end() - 1;
3075 }
3076
3077 // Store the info to the |ssrc_info|.
3078 if (attribute == kSsrcAttributeCname) {
3079 // RFC 5576
3080 // cname:<value>
3081 ssrc_info->cname = value;
3082 } else if (attribute == kSsrcAttributeMsid) {
3083 // draft-alvestrand-mmusic-msid-00
Seth Hampson5b4f0752018-04-02 16:31:36 -07003084 // msid:identifier [appdata]
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003085 std::vector<std::string> fields;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00003086 rtc::split(value, kSdpDelimiterSpace, &fields);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003087 if (fields.size() < 1 || fields.size() > 2) {
Yves Gerey665174f2018-06-19 15:03:05 +02003088 return ParseFailed(
3089 line, "Expected format \"msid:<identifier>[ <appdata>]\".", error);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003090 }
deadbeef9d3584c2016-02-16 17:54:10 -08003091 ssrc_info->stream_id = fields[0];
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003092 if (fields.size() == 2) {
deadbeef9d3584c2016-02-16 17:54:10 -08003093 ssrc_info->track_id = fields[1];
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003094 }
Steve Antone831b8c2018-02-01 12:22:16 -08003095 *msid_signaling |= cricket::kMsidSignalingSsrcAttribute;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003096 } else if (attribute == kSsrcAttributeMslabel) {
3097 // draft-alvestrand-rtcweb-mid-01
3098 // mslabel:<value>
3099 ssrc_info->mslabel = value;
3100 } else if (attribute == kSSrcAttributeLabel) {
3101 // The label isn't defined.
3102 // label:<value>
3103 ssrc_info->label = value;
3104 }
3105 return true;
3106}
3107
3108bool ParseSsrcGroupAttribute(const std::string& line,
3109 SsrcGroupVec* ssrc_groups,
3110 SdpParseError* error) {
nisseede5da42017-01-12 05:15:36 -08003111 RTC_DCHECK(ssrc_groups != NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003112 // RFC 5576
3113 // a=ssrc-group:<semantics> <ssrc-id> ...
3114 std::vector<std::string> fields;
Yves Gerey665174f2018-06-19 15:03:05 +02003115 rtc::split(line.substr(kLinePrefixLength), kSdpDelimiterSpace, &fields);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003116 const size_t expected_min_fields = 2;
3117 if (fields.size() < expected_min_fields) {
3118 return ParseFailedExpectMinFieldNum(line, expected_min_fields, error);
3119 }
3120 std::string semantics;
3121 if (!GetValue(fields[0], kAttributeSsrcGroup, &semantics, error)) {
3122 return false;
3123 }
Peter Boström0c4e06b2015-10-07 12:23:21 +02003124 std::vector<uint32_t> ssrcs;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003125 for (size_t i = 1; i < fields.size(); ++i) {
Peter Boström0c4e06b2015-10-07 12:23:21 +02003126 uint32_t ssrc = 0;
wu@webrtc.org5e760e72014-04-02 23:19:09 +00003127 if (!GetValueFromString(line, fields[i], &ssrc, error)) {
3128 return false;
3129 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003130 ssrcs.push_back(ssrc);
3131 }
3132 ssrc_groups->push_back(SsrcGroup(semantics, ssrcs));
3133 return true;
3134}
3135
3136bool ParseCryptoAttribute(const std::string& line,
3137 MediaContentDescription* media_desc,
3138 SdpParseError* error) {
3139 std::vector<std::string> fields;
Yves Gerey665174f2018-06-19 15:03:05 +02003140 rtc::split(line.substr(kLinePrefixLength), kSdpDelimiterSpace, &fields);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003141 // RFC 4568
3142 // a=crypto:<tag> <crypto-suite> <key-params> [<session-params>]
3143 const size_t expected_min_fields = 3;
3144 if (fields.size() < expected_min_fields) {
3145 return ParseFailedExpectMinFieldNum(line, expected_min_fields, error);
3146 }
3147 std::string tag_value;
3148 if (!GetValue(fields[0], kAttributeCrypto, &tag_value, error)) {
3149 return false;
3150 }
wu@webrtc.org5e760e72014-04-02 23:19:09 +00003151 int tag = 0;
3152 if (!GetValueFromString(line, tag_value, &tag, error)) {
3153 return false;
3154 }
jbauch083b73f2015-07-16 02:46:32 -07003155 const std::string& crypto_suite = fields[1];
3156 const std::string& key_params = fields[2];
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003157 std::string session_params;
3158 if (fields.size() > 3) {
3159 session_params = fields[3];
3160 }
Yves Gerey665174f2018-06-19 15:03:05 +02003161 media_desc->AddCrypto(
3162 CryptoParams(tag, crypto_suite, key_params, session_params));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003163 return true;
3164}
3165
3166// Updates or creates a new codec entry in the audio description with according
deadbeef67cf2c12016-04-13 10:07:16 -07003167// to |name|, |clockrate|, |bitrate|, and |channels|.
3168void UpdateCodec(int payload_type,
3169 const std::string& name,
3170 int clockrate,
3171 int bitrate,
3172 size_t channels,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003173 AudioContentDescription* audio_desc) {
3174 // Codec may already be populated with (only) optional parameters
3175 // (from an fmtp).
changbin.shao@webrtc.org2d25b442015-03-16 04:14:34 +00003176 cricket::AudioCodec codec =
3177 GetCodecWithPayloadType(audio_desc->codecs(), payload_type);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003178 codec.name = name;
3179 codec.clockrate = clockrate;
3180 codec.bitrate = bitrate;
3181 codec.channels = channels;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003182 AddOrReplaceCodec<AudioContentDescription, cricket::AudioCodec>(audio_desc,
3183 codec);
3184}
3185
3186// Updates or creates a new codec entry in the video description according to
deadbeef67cf2c12016-04-13 10:07:16 -07003187// |name|, |width|, |height|, and |framerate|.
3188void UpdateCodec(int payload_type,
3189 const std::string& name,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003190 VideoContentDescription* video_desc) {
3191 // Codec may already be populated with (only) optional parameters
3192 // (from an fmtp).
changbin.shao@webrtc.org2d25b442015-03-16 04:14:34 +00003193 cricket::VideoCodec codec =
3194 GetCodecWithPayloadType(video_desc->codecs(), payload_type);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003195 codec.name = name;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003196 AddOrReplaceCodec<VideoContentDescription, cricket::VideoCodec>(video_desc,
3197 codec);
3198}
3199
3200bool ParseRtpmapAttribute(const std::string& line,
3201 const MediaType media_type,
deadbeef67cf2c12016-04-13 10:07:16 -07003202 const std::vector<int>& payload_types,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003203 MediaContentDescription* media_desc,
3204 SdpParseError* error) {
3205 std::vector<std::string> fields;
Yves Gerey665174f2018-06-19 15:03:05 +02003206 rtc::split(line.substr(kLinePrefixLength), kSdpDelimiterSpace, &fields);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003207 // RFC 4566
3208 // a=rtpmap:<payload type> <encoding name>/<clock rate>[/<encodingparameters>]
3209 const size_t expected_min_fields = 2;
3210 if (fields.size() < expected_min_fields) {
3211 return ParseFailedExpectMinFieldNum(line, expected_min_fields, error);
3212 }
3213 std::string payload_type_value;
3214 if (!GetValue(fields[0], kAttributeRtpmap, &payload_type_value, error)) {
3215 return false;
3216 }
wu@webrtc.org5e760e72014-04-02 23:19:09 +00003217 int payload_type = 0;
pkasting@chromium.orge9facf82015-02-17 20:36:28 +00003218 if (!GetPayloadTypeFromString(line, payload_type_value, &payload_type,
3219 error)) {
wu@webrtc.org5e760e72014-04-02 23:19:09 +00003220 return false;
3221 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003222
deadbeef67cf2c12016-04-13 10:07:16 -07003223 if (std::find(payload_types.begin(), payload_types.end(), payload_type) ==
3224 payload_types.end()) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01003225 RTC_LOG(LS_WARNING) << "Ignore rtpmap line that did not appear in the "
Jonas Olsson45cc8902018-02-13 10:37:07 +01003226 "<fmt> of the m-line: "
3227 << line;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003228 return true;
3229 }
jbauch083b73f2015-07-16 02:46:32 -07003230 const std::string& encoder = fields[1];
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003231 std::vector<std::string> codec_params;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00003232 rtc::split(encoder, '/', &codec_params);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003233 // <encoding name>/<clock rate>[/<encodingparameters>]
3234 // 2 mandatory fields
3235 if (codec_params.size() < 2 || codec_params.size() > 3) {
3236 return ParseFailed(line,
3237 "Expected format \"<encoding name>/<clock rate>"
3238 "[/<encodingparameters>]\".",
3239 error);
3240 }
jbauch083b73f2015-07-16 02:46:32 -07003241 const std::string& encoding_name = codec_params[0];
wu@webrtc.org5e760e72014-04-02 23:19:09 +00003242 int clock_rate = 0;
3243 if (!GetValueFromString(line, codec_params[1], &clock_rate, error)) {
3244 return false;
3245 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003246 if (media_type == cricket::MEDIA_TYPE_VIDEO) {
Steve Antonb1c1de12017-12-21 15:14:30 -08003247 VideoContentDescription* video_desc = media_desc->as_video();
Yves Gerey665174f2018-06-19 15:03:05 +02003248 UpdateCodec(payload_type, encoding_name, video_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003249 } else if (media_type == cricket::MEDIA_TYPE_AUDIO) {
3250 // RFC 4566
3251 // For audio streams, <encoding parameters> indicates the number
3252 // of audio channels. This parameter is OPTIONAL and may be
3253 // omitted if the number of channels is one, provided that no
3254 // additional parameters are needed.
Peter Kasting69558702016-01-12 16:26:35 -08003255 size_t channels = 1;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003256 if (codec_params.size() == 3) {
wu@webrtc.org5e760e72014-04-02 23:19:09 +00003257 if (!GetValueFromString(line, codec_params[2], &channels, error)) {
3258 return false;
3259 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003260 }
Steve Antonb1c1de12017-12-21 15:14:30 -08003261 AudioContentDescription* audio_desc = media_desc->as_audio();
ossue1405ad2017-01-23 08:55:48 -08003262 UpdateCodec(payload_type, encoding_name, clock_rate, 0, channels,
deadbeef67cf2c12016-04-13 10:07:16 -07003263 audio_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003264 } else if (media_type == cricket::MEDIA_TYPE_DATA) {
Steve Antonb1c1de12017-12-21 15:14:30 -08003265 DataContentDescription* data_desc = media_desc->as_data();
deadbeef67cf2c12016-04-13 10:07:16 -07003266 data_desc->AddCodec(cricket::DataCodec(payload_type, encoding_name));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003267 }
3268 return true;
3269}
3270
Yves Gerey665174f2018-06-19 15:03:05 +02003271bool ParseFmtpParam(const std::string& line,
3272 std::string* parameter,
3273 std::string* value,
3274 SdpParseError* error) {
Donald Curtis0e07f922015-05-15 09:21:23 -07003275 if (!rtc::tokenize_first(line, kSdpDelimiterEqual, parameter, value)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003276 ParseFailed(line, "Unable to parse fmtp parameter. \'=\' missing.", error);
3277 return false;
3278 }
3279 // a=fmtp:<payload_type> <param1>=<value1>; <param2>=<value2>; ...
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003280 return true;
3281}
3282
Yves Gerey665174f2018-06-19 15:03:05 +02003283bool ParseFmtpAttributes(const std::string& line,
3284 const MediaType media_type,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003285 MediaContentDescription* media_desc,
3286 SdpParseError* error) {
3287 if (media_type != cricket::MEDIA_TYPE_AUDIO &&
3288 media_type != cricket::MEDIA_TYPE_VIDEO) {
3289 return true;
3290 }
Donald Curtis0e07f922015-05-15 09:21:23 -07003291
3292 std::string line_payload;
3293 std::string line_params;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003294
3295 // RFC 5576
3296 // a=fmtp:<format> <format specific parameters>
3297 // At least two fields, whereas the second one is any of the optional
3298 // parameters.
Donald Curtis144d0182015-05-15 13:14:24 -07003299 if (!rtc::tokenize_first(line.substr(kLinePrefixLength), kSdpDelimiterSpace,
3300 &line_payload, &line_params)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003301 ParseFailedExpectMinFieldNum(line, 2, error);
3302 return false;
3303 }
3304
Donald Curtis0e07f922015-05-15 09:21:23 -07003305 // Parse out the payload information.
pkasting@chromium.orgd3245462015-02-23 21:28:22 +00003306 std::string payload_type_str;
Donald Curtis0e07f922015-05-15 09:21:23 -07003307 if (!GetValue(line_payload, kAttributeFmtp, &payload_type_str, error)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003308 return false;
3309 }
3310
Donald Curtis0e07f922015-05-15 09:21:23 -07003311 int payload_type = 0;
Donald Curtis144d0182015-05-15 13:14:24 -07003312 if (!GetPayloadTypeFromString(line_payload, payload_type_str, &payload_type,
3313 error)) {
Donald Curtis0e07f922015-05-15 09:21:23 -07003314 return false;
3315 }
3316
3317 // Parse out format specific parameters.
3318 std::vector<std::string> fields;
3319 rtc::split(line_params, kSdpDelimiterSemicolon, &fields);
3320
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003321 cricket::CodecParameterMap codec_params;
Donald Curtis144d0182015-05-15 13:14:24 -07003322 for (auto& iter : fields) {
Donald Curtis0e07f922015-05-15 09:21:23 -07003323 if (iter.find(kSdpDelimiterEqual) == std::string::npos) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003324 // Only fmtps with equals are currently supported. Other fmtp types
3325 // should be ignored. Unknown fmtps do not constitute an error.
3326 continue;
3327 }
Donald Curtis0e07f922015-05-15 09:21:23 -07003328
3329 std::string name;
3330 std::string value;
3331 if (!ParseFmtpParam(rtc::string_trim(iter), &name, &value, error)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003332 return false;
3333 }
3334 codec_params[name] = value;
3335 }
3336
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003337 if (media_type == cricket::MEDIA_TYPE_AUDIO) {
3338 UpdateCodec<AudioContentDescription, cricket::AudioCodec>(
pkasting@chromium.orgd3245462015-02-23 21:28:22 +00003339 media_desc, payload_type, codec_params);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003340 } else if (media_type == cricket::MEDIA_TYPE_VIDEO) {
3341 UpdateCodec<VideoContentDescription, cricket::VideoCodec>(
pkasting@chromium.orgd3245462015-02-23 21:28:22 +00003342 media_desc, payload_type, codec_params);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003343 }
3344 return true;
3345}
3346
Yves Gerey665174f2018-06-19 15:03:05 +02003347bool ParseRtcpFbAttribute(const std::string& line,
3348 const MediaType media_type,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003349 MediaContentDescription* media_desc,
3350 SdpParseError* error) {
3351 if (media_type != cricket::MEDIA_TYPE_AUDIO &&
3352 media_type != cricket::MEDIA_TYPE_VIDEO) {
3353 return true;
3354 }
3355 std::vector<std::string> rtcp_fb_fields;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00003356 rtc::split(line.c_str(), kSdpDelimiterSpace, &rtcp_fb_fields);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003357 if (rtcp_fb_fields.size() < 2) {
3358 return ParseFailedGetValue(line, kAttributeRtcpFb, error);
3359 }
3360 std::string payload_type_string;
3361 if (!GetValue(rtcp_fb_fields[0], kAttributeRtcpFb, &payload_type_string,
3362 error)) {
3363 return false;
3364 }
wu@webrtc.org5e760e72014-04-02 23:19:09 +00003365 int payload_type = kWildcardPayloadType;
3366 if (payload_type_string != "*") {
pkasting@chromium.orge9facf82015-02-17 20:36:28 +00003367 if (!GetPayloadTypeFromString(line, payload_type_string, &payload_type,
3368 error)) {
wu@webrtc.org5e760e72014-04-02 23:19:09 +00003369 return false;
3370 }
3371 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003372 std::string id = rtcp_fb_fields[1];
3373 std::string param = "";
3374 for (std::vector<std::string>::iterator iter = rtcp_fb_fields.begin() + 2;
3375 iter != rtcp_fb_fields.end(); ++iter) {
3376 param.append(*iter);
3377 }
3378 const cricket::FeedbackParam feedback_param(id, param);
3379
3380 if (media_type == cricket::MEDIA_TYPE_AUDIO) {
pkasting@chromium.orgd3245462015-02-23 21:28:22 +00003381 UpdateCodec<AudioContentDescription, cricket::AudioCodec>(
3382 media_desc, payload_type, feedback_param);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003383 } else if (media_type == cricket::MEDIA_TYPE_VIDEO) {
pkasting@chromium.orgd3245462015-02-23 21:28:22 +00003384 UpdateCodec<VideoContentDescription, cricket::VideoCodec>(
3385 media_desc, payload_type, feedback_param);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003386 }
3387 return true;
3388}
3389
3390} // namespace webrtc