blob: 73df354dd5db773037c71fdeae4d0788bf3ba0f9 [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öglundb6b29e02018-06-21 16:58:01 +020025#include "api/mediatypes.h"
Patrik Höglunde2d6a062017-10-05 14:53:33 +020026#include "api/candidate.h"
Patrik Höglund7aee3d52017-11-15 13:15:17 +010027#include "api/cryptoparams.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020028#include "api/jsepicecandidate.h"
29#include "api/jsepsessiondescription.h"
isheriff6f8d6862016-05-26 11:24:55 -070030// for RtpExtension
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020031#include "api/rtpparameters.h"
32#include "media/base/codec.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020033#include "media/base/mediaconstants.h"
34#include "media/base/rtputils.h"
35#include "media/sctp/sctptransportinternal.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020036#include "p2p/base/p2pconstants.h"
37#include "p2p/base/port.h"
38#include "pc/mediasession.h"
39#include "rtc_base/arraysize.h"
40#include "rtc_base/checks.h"
41#include "rtc_base/logging.h"
42#include "rtc_base/messagedigest.h"
43#include "rtc_base/stringutils.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000044
45using cricket::AudioContentDescription;
46using cricket::Candidate;
47using cricket::Candidates;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000048using cricket::ContentInfo;
49using cricket::CryptoParams;
50using cricket::DataContentDescription;
51using cricket::ICE_CANDIDATE_COMPONENT_RTP;
52using cricket::ICE_CANDIDATE_COMPONENT_RTCP;
53using cricket::kCodecParamMaxBitrate;
54using cricket::kCodecParamMaxPTime;
55using cricket::kCodecParamMaxQuantization;
56using cricket::kCodecParamMinBitrate;
57using cricket::kCodecParamMinPTime;
58using cricket::kCodecParamPTime;
59using cricket::kCodecParamSPropStereo;
buildbot@webrtc.orged97bb02014-05-07 11:15:20 +000060using cricket::kCodecParamStartBitrate;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000061using cricket::kCodecParamStereo;
62using cricket::kCodecParamUseInbandFec;
Minyue Li7100dcd2015-03-27 05:05:59 +010063using cricket::kCodecParamUseDtx;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000064using cricket::kCodecParamSctpProtocol;
65using cricket::kCodecParamSctpStreams;
henrike@webrtc.org1e09a712013-07-26 19:17:59 +000066using cricket::kCodecParamMaxAverageBitrate;
buildbot@webrtc.org5d639b32014-09-10 07:57:12 +000067using cricket::kCodecParamMaxPlaybackRate;
stefan@webrtc.org85d27942014-06-09 12:51:39 +000068using cricket::kCodecParamAssociatedPayloadType;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000069using cricket::MediaContentDescription;
70using cricket::MediaType;
isheriff6f8d6862016-05-26 11:24:55 -070071using cricket::RtpHeaderExtensions;
Steve Anton5adfafd2017-12-20 16:34:00 -080072using cricket::MediaProtocolType;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000073using cricket::SsrcGroup;
74using cricket::StreamParams;
75using cricket::StreamParamsVec;
76using cricket::TransportDescription;
77using cricket::TransportInfo;
78using cricket::VideoContentDescription;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000079using rtc::SocketAddress;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000080
henrike@webrtc.org28e20752013-07-10 00:45:36 +000081namespace cricket {
82class SessionDescription;
83}
84
deadbeef90f1e1e2017-02-10 12:35:05 -080085// TODO(deadbeef): Switch to using anonymous namespace rather than declaring
86// everything "static".
henrike@webrtc.org28e20752013-07-10 00:45:36 +000087namespace webrtc {
88
89// Line type
90// RFC 4566
91// An SDP session description consists of a number of lines of text of
92// the form:
93// <type>=<value>
94// where <type> MUST be exactly one case-significant character.
deadbeef9d3584c2016-02-16 17:54:10 -080095static const int kLinePrefixLength = 2; // Length of <type>=
henrike@webrtc.org28e20752013-07-10 00:45:36 +000096static const char kLineTypeVersion = 'v';
97static const char kLineTypeOrigin = 'o';
98static const char kLineTypeSessionName = 's';
99static const char kLineTypeSessionInfo = 'i';
100static const char kLineTypeSessionUri = 'u';
101static const char kLineTypeSessionEmail = 'e';
102static const char kLineTypeSessionPhone = 'p';
103static const char kLineTypeSessionBandwidth = 'b';
104static const char kLineTypeTiming = 't';
105static const char kLineTypeRepeatTimes = 'r';
106static const char kLineTypeTimeZone = 'z';
107static const char kLineTypeEncryptionKey = 'k';
108static const char kLineTypeMedia = 'm';
109static const char kLineTypeConnection = 'c';
110static const char kLineTypeAttributes = 'a';
111
112// Attributes
113static const char kAttributeGroup[] = "group";
114static const char kAttributeMid[] = "mid";
deadbeef9d3584c2016-02-16 17:54:10 -0800115static const char kAttributeMsid[] = "msid";
deadbeef25ed4352016-12-12 18:37:36 -0800116static const char kAttributeBundleOnly[] = "bundle-only";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000117static const char kAttributeRtcpMux[] = "rtcp-mux";
deadbeef13871492015-12-09 12:37:51 -0800118static const char kAttributeRtcpReducedSize[] = "rtcp-rsize";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000119static const char kAttributeSsrc[] = "ssrc";
120static const char kSsrcAttributeCname[] = "cname";
121static const char kAttributeExtmap[] = "extmap";
122// draft-alvestrand-mmusic-msid-01
123// a=msid-semantic: WMS
Seth Hampson5b4f0752018-04-02 16:31:36 -0700124// This is a legacy field supported only for Plan B semantics.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000125static const char kAttributeMsidSemantics[] = "msid-semantic";
126static const char kMediaStreamSemantic[] = "WMS";
127static const char kSsrcAttributeMsid[] = "msid";
128static const char kDefaultMsid[] = "default";
Seth Hampson5b4f0752018-04-02 16:31:36 -0700129static const char kNoStreamMsid[] = "-";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000130static const char kSsrcAttributeMslabel[] = "mslabel";
131static const char kSSrcAttributeLabel[] = "label";
132static const char kAttributeSsrcGroup[] = "ssrc-group";
133static const char kAttributeCrypto[] = "crypto";
134static const char kAttributeCandidate[] = "candidate";
135static const char kAttributeCandidateTyp[] = "typ";
136static const char kAttributeCandidateRaddr[] = "raddr";
137static const char kAttributeCandidateRport[] = "rport";
honghaiza54a0802015-12-16 18:37:23 -0800138static const char kAttributeCandidateUfrag[] = "ufrag";
139static const char kAttributeCandidatePwd[] = "pwd";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000140static const char kAttributeCandidateGeneration[] = "generation";
honghaiza0c44ea2016-03-23 16:07:48 -0700141static const char kAttributeCandidateNetworkId[] = "network-id";
honghaize1a0c942016-02-16 14:54:56 -0800142static const char kAttributeCandidateNetworkCost[] = "network-cost";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000143static const char kAttributeFingerprint[] = "fingerprint";
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +0000144static const char kAttributeSetup[] = "setup";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000145static const char kAttributeFmtp[] = "fmtp";
146static const char kAttributeRtpmap[] = "rtpmap";
wu@webrtc.org78187522013-10-07 23:32:02 +0000147static const char kAttributeSctpmap[] = "sctpmap";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000148static const char kAttributeRtcp[] = "rtcp";
149static const char kAttributeIceUfrag[] = "ice-ufrag";
150static const char kAttributeIcePwd[] = "ice-pwd";
151static const char kAttributeIceLite[] = "ice-lite";
152static const char kAttributeIceOption[] = "ice-options";
153static const char kAttributeSendOnly[] = "sendonly";
154static const char kAttributeRecvOnly[] = "recvonly";
155static const char kAttributeRtcpFb[] = "rtcp-fb";
156static const char kAttributeSendRecv[] = "sendrecv";
157static const char kAttributeInactive[] = "inactive";
jiayl@webrtc.orgddb85ab2014-09-05 16:31:56 +0000158// draft-ietf-mmusic-sctp-sdp-07
159// a=sctp-port
160static const char kAttributeSctpPort[] = "sctp-port";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000161
162// Experimental flags
163static const char kAttributeXGoogleFlag[] = "x-google-flag";
164static const char kValueConference[] = "conference";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000165
166// Candidate
167static const char kCandidateHost[] = "host";
168static const char kCandidateSrflx[] = "srflx";
Honghai Zhang7fb69db2016-03-14 11:59:18 -0700169static const char kCandidatePrflx[] = "prflx";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000170static const char kCandidateRelay[] = "relay";
mallinath@webrtc.org2d60c5e2014-08-08 22:29:20 +0000171static const char kTcpCandidateType[] = "tcptype";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000172
173static const char kSdpDelimiterEqual = '=';
174static const char kSdpDelimiterSpace = ' ';
175static const char kSdpDelimiterColon = ':';
176static const char kSdpDelimiterSemicolon = ';';
177static const char kSdpDelimiterSlash = '/';
178static const char kNewLine = '\n';
179static const char kReturn = '\r';
180static const char kLineBreak[] = "\r\n";
181
Steve Anton36b29d12017-10-30 09:57:42 -0700182// TODO(deadbeef): Generate the Session and Time description
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000183// instead of hardcoding.
184static const char kSessionVersion[] = "v=0";
185// RFC 4566
186static const char kSessionOriginUsername[] = "-";
187static const char kSessionOriginSessionId[] = "0";
188static const char kSessionOriginSessionVersion[] = "0";
189static const char kSessionOriginNettype[] = "IN";
190static const char kSessionOriginAddrtype[] = "IP4";
191static const char kSessionOriginAddress[] = "127.0.0.1";
192static const char kSessionName[] = "s=-";
193static const char kTimeDescription[] = "t=0 0";
194static const char kAttrGroup[] = "a=group:BUNDLE";
195static const char kConnectionNettype[] = "IN";
pthatcher@webrtc.orgc9d6d142014-10-23 23:37:22 +0000196static const char kConnectionIpv4Addrtype[] = "IP4";
197static const char kConnectionIpv6Addrtype[] = "IP6";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000198static const char kMediaTypeVideo[] = "video";
199static const char kMediaTypeAudio[] = "audio";
200static const char kMediaTypeData[] = "application";
201static const char kMediaPortRejected[] = "0";
pthatcher@webrtc.orgc9d6d142014-10-23 23:37:22 +0000202// draft-ietf-mmusic-trickle-ice-01
203// When no candidates have been gathered, set the connection
204// address to IP6 ::.
perkj@webrtc.orgd105cc82014-11-07 11:22:06 +0000205// TODO(perkj): FF can not parse IP6 ::. See http://crbug/430333
206// Use IPV4 per default.
207static const char kDummyAddress[] = "0.0.0.0";
pthatcher@webrtc.orgc9d6d142014-10-23 23:37:22 +0000208static const char kDummyPort[] = "9";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000209// RFC 3556
210static const char kApplicationSpecificMaximum[] = "AS";
211
wu@webrtc.org78187522013-10-07 23:32:02 +0000212static const char kDefaultSctpmapProtocol[] = "webrtc-datachannel";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000213
pkasting@chromium.orgd3245462015-02-23 21:28:22 +0000214// RTP payload type is in the 0-127 range. Use -1 to indicate "all" payload
215// types.
216const int kWildcardPayloadType = -1;
217
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000218struct SsrcInfo {
Peter Boström0c4e06b2015-10-07 12:23:21 +0200219 uint32_t ssrc_id;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000220 std::string cname;
deadbeef9d3584c2016-02-16 17:54:10 -0800221 std::string stream_id;
222 std::string track_id;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000223
224 // For backward compatibility.
225 // TODO(ronghuawu): Remove below 2 fields once all the clients support msid.
226 std::string label;
227 std::string mslabel;
228};
229typedef std::vector<SsrcInfo> SsrcInfoVec;
230typedef std::vector<SsrcGroup> SsrcGroupVec;
231
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000232template <class T>
233static void AddFmtpLine(const T& codec, std::string* message);
234static void BuildMediaDescription(const ContentInfo* content_info,
235 const TransportInfo* transport_info,
Patrik Höglundb6b29e02018-06-21 16:58:01 +0200236 const cricket::MediaType media_type,
wu@webrtc.org4c3e9912014-07-16 21:03:13 +0000237 const std::vector<Candidate>& candidates,
Steve Antone831b8c2018-02-01 12:22:16 -0800238 int msid_signaling,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000239 std::string* message);
zstein4b2e0822017-02-17 19:48:38 -0800240static void BuildSctpContentAttributes(std::string* message,
241 int sctp_port,
242 bool use_sctpmap);
deadbeef9d3584c2016-02-16 17:54:10 -0800243static void BuildRtpContentAttributes(const MediaContentDescription* media_desc,
Patrik Höglundb6b29e02018-06-21 16:58:01 +0200244 const cricket::MediaType media_type,
Steve Antone831b8c2018-02-01 12:22:16 -0800245 int msid_signaling,
deadbeef9d3584c2016-02-16 17:54:10 -0800246 std::string* message);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000247static void BuildRtpMap(const MediaContentDescription* media_desc,
Patrik Höglundb6b29e02018-06-21 16:58:01 +0200248 const cricket::MediaType media_type,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000249 std::string* message);
250static void BuildCandidate(const std::vector<Candidate>& candidates,
honghaiza54a0802015-12-16 18:37:23 -0800251 bool include_ufrag,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000252 std::string* message);
253static void BuildIceOptions(const std::vector<std::string>& transport_options,
254 std::string* message);
pthatcher@webrtc.org3341b402015-02-13 21:14:22 +0000255static bool IsRtp(const std::string& protocol);
256static bool IsDtlsSctp(const std::string& protocol);
zhihuang38989e52017-03-21 11:04:53 -0700257static bool ParseSessionDescription(const std::string& message,
258 size_t* pos,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000259 std::string* session_id,
260 std::string* session_version,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000261 TransportDescription* session_td,
262 RtpHeaderExtensions* session_extmaps,
zhihuang38989e52017-03-21 11:04:53 -0700263 rtc::SocketAddress* connection_addr,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000264 cricket::SessionDescription* desc,
265 SdpParseError* error);
266static bool ParseGroupAttribute(const std::string& line,
267 cricket::SessionDescription* desc,
268 SdpParseError* error);
269static bool ParseMediaDescription(
270 const std::string& message,
271 const TransportDescription& session_td,
272 const RtpHeaderExtensions& session_extmaps,
zhihuang38989e52017-03-21 11:04:53 -0700273 size_t* pos,
274 const rtc::SocketAddress& session_connection_addr,
275 cricket::SessionDescription* desc,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000276 std::vector<JsepIceCandidate*>* candidates,
277 SdpParseError* error);
278static bool ParseContent(const std::string& message,
Patrik Höglundb6b29e02018-06-21 16:58:01 +0200279 const cricket::MediaType media_type,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000280 int mline_index,
281 const std::string& protocol,
deadbeef67cf2c12016-04-13 10:07:16 -0700282 const std::vector<int>& payload_types,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000283 size_t* pos,
284 std::string* content_name,
deadbeef25ed4352016-12-12 18:37:36 -0800285 bool* bundle_only,
Steve Antone831b8c2018-02-01 12:22:16 -0800286 int* msid_signaling,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000287 MediaContentDescription* media_desc,
288 TransportDescription* transport,
289 std::vector<JsepIceCandidate*>* candidates,
290 SdpParseError* error);
291static bool ParseSsrcAttribute(const std::string& line,
292 SsrcInfoVec* ssrc_infos,
Steve Antone831b8c2018-02-01 12:22:16 -0800293 int* msid_signaling,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000294 SdpParseError* error);
295static bool ParseSsrcGroupAttribute(const std::string& line,
296 SsrcGroupVec* ssrc_groups,
297 SdpParseError* error);
298static bool ParseCryptoAttribute(const std::string& line,
299 MediaContentDescription* media_desc,
300 SdpParseError* error);
301static bool ParseRtpmapAttribute(const std::string& line,
Patrik Höglundb6b29e02018-06-21 16:58:01 +0200302 const cricket::MediaType media_type,
deadbeef67cf2c12016-04-13 10:07:16 -0700303 const std::vector<int>& payload_types,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000304 MediaContentDescription* media_desc,
305 SdpParseError* error);
306static bool ParseFmtpAttributes(const std::string& line,
Patrik Höglundb6b29e02018-06-21 16:58:01 +0200307 const cricket::MediaType media_type,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000308 MediaContentDescription* media_desc,
309 SdpParseError* error);
Yves Gerey665174f2018-06-19 15:03:05 +0200310static bool ParseFmtpParam(const std::string& line,
311 std::string* parameter,
312 std::string* value,
313 SdpParseError* error);
314static bool ParseCandidate(const std::string& message,
315 Candidate* candidate,
316 SdpParseError* error,
317 bool is_raw);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000318static bool ParseRtcpFbAttribute(const std::string& line,
Patrik Höglundb6b29e02018-06-21 16:58:01 +0200319 const cricket::MediaType media_type,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000320 MediaContentDescription* media_desc,
321 SdpParseError* error);
322static bool ParseIceOptions(const std::string& line,
323 std::vector<std::string>* transport_options,
324 SdpParseError* error);
325static bool ParseExtmap(const std::string& line,
isheriff6f8d6862016-05-26 11:24:55 -0700326 RtpExtension* extmap,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000327 SdpParseError* error);
328static bool ParseFingerprintAttribute(const std::string& line,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000329 rtc::SSLFingerprint** fingerprint,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000330 SdpParseError* error);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +0000331static bool ParseDtlsSetup(const std::string& line,
332 cricket::ConnectionRole* role,
333 SdpParseError* error);
deadbeef9d3584c2016-02-16 17:54:10 -0800334static bool ParseMsidAttribute(const std::string& line,
Seth Hampson5b4f0752018-04-02 16:31:36 -0700335 std::vector<std::string>* stream_ids,
deadbeef9d3584c2016-02-16 17:54:10 -0800336 std::string* track_id,
337 SdpParseError* error);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000338
339// Helper functions
340
341// Below ParseFailed*** functions output the line that caused the parsing
342// failure and the detailed reason (|description|) of the failure to |error|.
343// The functions always return false so that they can be used directly in the
344// following way when error happens:
345// "return ParseFailed***(...);"
346
347// The line starting at |line_start| of |message| is the failing line.
348// The reason for the failure should be provided in the |description|.
349// An example of a description could be "unknown character".
350static bool ParseFailed(const std::string& message,
351 size_t line_start,
352 const std::string& description,
353 SdpParseError* error) {
354 // Get the first line of |message| from |line_start|.
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +0000355 std::string first_line;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000356 size_t line_end = message.find(kNewLine, line_start);
357 if (line_end != std::string::npos) {
358 if (line_end > 0 && (message.at(line_end - 1) == kReturn)) {
359 --line_end;
360 }
361 first_line = message.substr(line_start, (line_end - line_start));
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +0000362 } else {
363 first_line = message.substr(line_start);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000364 }
365
366 if (error) {
367 error->line = first_line;
368 error->description = description;
369 }
Mirko Bonadei675513b2017-11-09 11:09:25 +0100370 RTC_LOG(LS_ERROR) << "Failed to parse: \"" << first_line
371 << "\". Reason: " << description;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000372 return false;
373}
374
375// |line| is the failing line. The reason for the failure should be
376// provided in the |description|.
377static bool ParseFailed(const std::string& line,
378 const std::string& description,
379 SdpParseError* error) {
380 return ParseFailed(line, 0, description, error);
381}
382
383// Parses failure where the failing SDP line isn't know or there are multiple
384// failing lines.
Yves Gerey665174f2018-06-19 15:03:05 +0200385static bool ParseFailed(const std::string& description, SdpParseError* error) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000386 return ParseFailed("", description, error);
387}
388
389// |line| is the failing line. The failure is due to the fact that |line|
390// doesn't have |expected_fields| fields.
391static bool ParseFailedExpectFieldNum(const std::string& line,
392 int expected_fields,
393 SdpParseError* error) {
394 std::ostringstream description;
395 description << "Expects " << expected_fields << " fields.";
396 return ParseFailed(line, description.str(), error);
397}
398
399// |line| is the failing line. The failure is due to the fact that |line| has
400// less than |expected_min_fields| fields.
401static bool ParseFailedExpectMinFieldNum(const std::string& line,
402 int expected_min_fields,
403 SdpParseError* error) {
404 std::ostringstream description;
405 description << "Expects at least " << expected_min_fields << " fields.";
406 return ParseFailed(line, description.str(), error);
407}
408
409// |line| is the failing line. The failure is due to the fact that it failed to
410// get the value of |attribute|.
411static bool ParseFailedGetValue(const std::string& line,
412 const std::string& attribute,
413 SdpParseError* error) {
414 std::ostringstream description;
415 description << "Failed to get the value of attribute: " << attribute;
416 return ParseFailed(line, description.str(), error);
417}
418
419// The line starting at |line_start| of |message| is the failing line. The
420// failure is due to the line type (e.g. the "m" part of the "m-line")
421// not matching what is expected. The expected line type should be
422// provided as |line_type|.
423static bool ParseFailedExpectLine(const std::string& message,
424 size_t line_start,
425 const char line_type,
426 const std::string& line_value,
427 SdpParseError* error) {
428 std::ostringstream description;
429 description << "Expect line: " << line_type << "=" << line_value;
430 return ParseFailed(message, line_start, description.str(), error);
431}
432
433static bool AddLine(const std::string& line, std::string* message) {
434 if (!message)
435 return false;
436
437 message->append(line);
438 message->append(kLineBreak);
439 return true;
440}
441
442static bool GetLine(const std::string& message,
443 size_t* pos,
444 std::string* line) {
445 size_t line_begin = *pos;
446 size_t line_end = message.find(kNewLine, line_begin);
447 if (line_end == std::string::npos) {
448 return false;
449 }
450 // Update the new start position
451 *pos = line_end + 1;
452 if (line_end > 0 && (message.at(line_end - 1) == kReturn)) {
453 --line_end;
454 }
455 *line = message.substr(line_begin, (line_end - line_begin));
456 const char* cline = line->c_str();
457 // RFC 4566
458 // An SDP session description consists of a number of lines of text of
459 // the form:
460 // <type>=<value>
461 // where <type> MUST be exactly one case-significant character and
462 // <value> is structured text whose format depends on <type>.
463 // Whitespace MUST NOT be used on either side of the "=" sign.
Taylor Brandstetter93a7b242018-04-16 10:45:24 -0700464 //
465 // However, an exception to the whitespace rule is made for "s=", since
466 // RFC4566 also says:
467 //
468 // If a session has no meaningful name, the value "s= " SHOULD be used
469 // (i.e., a single space as the session name).
470 if (line->length() < 3 || !islower(cline[0]) ||
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000471 cline[1] != kSdpDelimiterEqual ||
Taylor Brandstetter93a7b242018-04-16 10:45:24 -0700472 (cline[0] != kLineTypeSessionName && cline[2] == kSdpDelimiterSpace)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000473 *pos = line_begin;
474 return false;
475 }
476 return true;
477}
478
479// Init |os| to "|type|=|value|".
480static void InitLine(const char type,
481 const std::string& value,
482 std::ostringstream* os) {
483 os->str("");
484 *os << type << kSdpDelimiterEqual << value;
485}
486
487// Init |os| to "a=|attribute|".
488static void InitAttrLine(const std::string& attribute, std::ostringstream* os) {
489 InitLine(kLineTypeAttributes, attribute, os);
490}
491
492// Writes a SDP attribute line based on |attribute| and |value| to |message|.
Yves Gerey665174f2018-06-19 15:03:05 +0200493static void AddAttributeLine(const std::string& attribute,
494 int value,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000495 std::string* message) {
496 std::ostringstream os;
497 InitAttrLine(attribute, &os);
498 os << kSdpDelimiterColon << value;
499 AddLine(os.str(), message);
500}
501
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000502static bool IsLineType(const std::string& message,
503 const char type,
504 size_t line_start) {
505 if (message.size() < line_start + kLinePrefixLength) {
506 return false;
507 }
508 const char* cmessage = message.c_str();
509 return (cmessage[line_start] == type &&
510 cmessage[line_start + 1] == kSdpDelimiterEqual);
511}
512
Yves Gerey665174f2018-06-19 15:03:05 +0200513static bool IsLineType(const std::string& line, const char type) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000514 return IsLineType(line, type, 0);
515}
516
Yves Gerey665174f2018-06-19 15:03:05 +0200517static bool GetLineWithType(const std::string& message,
518 size_t* pos,
519 std::string* line,
520 const char type) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000521 if (!IsLineType(message, type, *pos)) {
522 return false;
523 }
524
525 if (!GetLine(message, pos, line))
526 return false;
527
528 return true;
529}
530
531static bool HasAttribute(const std::string& line,
532 const std::string& attribute) {
533 return (line.compare(kLinePrefixLength, attribute.size(), attribute) == 0);
534}
535
Peter Boström0c4e06b2015-10-07 12:23:21 +0200536static bool AddSsrcLine(uint32_t ssrc_id,
537 const std::string& attribute,
538 const std::string& value,
539 std::string* message) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000540 // RFC 5576
541 // a=ssrc:<ssrc-id> <attribute>:<value>
542 std::ostringstream os;
543 InitAttrLine(kAttributeSsrc, &os);
Yves Gerey665174f2018-06-19 15:03:05 +0200544 os << kSdpDelimiterColon << ssrc_id << kSdpDelimiterSpace << attribute
545 << kSdpDelimiterColon << value;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000546 return AddLine(os.str(), message);
547}
548
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000549// Get value only from <attribute>:<value>.
Yves Gerey665174f2018-06-19 15:03:05 +0200550static bool GetValue(const std::string& message,
551 const std::string& attribute,
552 std::string* value,
553 SdpParseError* error) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000554 std::string leftpart;
Donald Curtis0e07f922015-05-15 09:21:23 -0700555 if (!rtc::tokenize_first(message, kSdpDelimiterColon, &leftpart, value)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000556 return ParseFailedGetValue(message, attribute, error);
557 }
558 // The left part should end with the expected attribute.
559 if (leftpart.length() < attribute.length() ||
560 leftpart.compare(leftpart.length() - attribute.length(),
561 attribute.length(), attribute) != 0) {
562 return ParseFailedGetValue(message, attribute, error);
563 }
564 return true;
565}
566
567static bool CaseInsensitiveFind(std::string str1, std::string str2) {
Yves Gerey665174f2018-06-19 15:03:05 +0200568 std::transform(str1.begin(), str1.end(), str1.begin(), ::tolower);
569 std::transform(str2.begin(), str2.end(), str2.begin(), ::tolower);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000570 return str1.find(str2) != std::string::npos;
571}
572
wu@webrtc.org5e760e72014-04-02 23:19:09 +0000573template <class T>
574static bool GetValueFromString(const std::string& line,
575 const std::string& s,
576 T* t,
577 SdpParseError* error) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000578 if (!rtc::FromString(s, t)) {
wu@webrtc.org5e760e72014-04-02 23:19:09 +0000579 std::ostringstream description;
580 description << "Invalid value: " << s << ".";
581 return ParseFailed(line, description.str(), error);
582 }
583 return true;
584}
585
pkasting@chromium.orge9facf82015-02-17 20:36:28 +0000586static bool GetPayloadTypeFromString(const std::string& line,
587 const std::string& s,
588 int* payload_type,
589 SdpParseError* error) {
590 return GetValueFromString(line, s, payload_type, error) &&
Yves Gerey665174f2018-06-19 15:03:05 +0200591 cricket::IsValidRtpPayloadType(*payload_type);
pkasting@chromium.orge9facf82015-02-17 20:36:28 +0000592}
593
Seth Hampson5897a6e2018-04-03 11:16:33 -0700594// Creates a StreamParams track in the case when no SSRC lines are signaled.
595// This is a track that does not contain SSRCs and only contains
596// stream_ids/track_id if it's signaled with a=msid lines.
597void CreateTrackWithNoSsrcs(const std::vector<std::string>& msid_stream_ids,
598 const std::string& msid_track_id,
599 StreamParamsVec* tracks) {
600 StreamParams track;
601 if (msid_track_id.empty() || msid_stream_ids.empty()) {
602 // We only create an unsignaled track if a=msid lines were signaled.
603 return;
604 }
605 track.set_stream_ids(msid_stream_ids);
606 track.id = msid_track_id;
607 tracks->push_back(track);
608}
609
610// Creates the StreamParams tracks, for the case when SSRC lines are signaled.
611// |msid_stream_ids| and |msid_track_id| represent the stream/track ID from the
Taylor Brandstetter5de6b752016-03-09 17:02:30 -0800612// "a=msid" attribute, if it exists. They are empty if the attribute does not
Seth Hampson5897a6e2018-04-03 11:16:33 -0700613// exist. We prioritize getting stream_ids/track_ids signaled in a=msid lines.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000614void CreateTracksFromSsrcInfos(const SsrcInfoVec& ssrc_infos,
Seth Hampson5b4f0752018-04-02 16:31:36 -0700615 const std::vector<std::string>& msid_stream_ids,
Taylor Brandstetter5de6b752016-03-09 17:02:30 -0800616 const std::string& msid_track_id,
Seth Hampson5b4f0752018-04-02 16:31:36 -0700617 StreamParamsVec* tracks,
618 int msid_signaling) {
nisseede5da42017-01-12 05:15:36 -0800619 RTC_DCHECK(tracks != NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000620 for (SsrcInfoVec::const_iterator ssrc_info = ssrc_infos.begin();
621 ssrc_info != ssrc_infos.end(); ++ssrc_info) {
622 if (ssrc_info->cname.empty()) {
623 continue;
624 }
625
Seth Hampson5b4f0752018-04-02 16:31:36 -0700626 std::vector<std::string> stream_ids;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000627 std::string track_id;
Seth Hampson5b4f0752018-04-02 16:31:36 -0700628 if (msid_signaling & cricket::kMsidSignalingMediaSection) {
629 // This is the case with Unified Plan SDP msid signaling.
630 stream_ids = msid_stream_ids;
Tomas Gunnarsson191bf5c2018-03-30 10:44:43 +0000631 track_id = msid_track_id;
Seth Hampson5b4f0752018-04-02 16:31:36 -0700632 } else if (msid_signaling & cricket::kMsidSignalingSsrcAttribute) {
633 // This is the case with Plan B SDP msid signaling.
634 stream_ids.push_back(ssrc_info->stream_id);
Tomas Gunnarsson191bf5c2018-03-30 10:44:43 +0000635 track_id = ssrc_info->track_id;
Seth Hampson5b4f0752018-04-02 16:31:36 -0700636 } else if (!ssrc_info->mslabel.empty()) {
637 // Since there's no a=msid or a=ssrc msid signaling, this is a sdp from
638 // an older version of client that doesn't support msid.
639 // In that case, we use the mslabel and label to construct the track.
640 stream_ids.push_back(ssrc_info->mslabel);
641 track_id = ssrc_info->label;
642 } else {
643 // Since no media streams isn't supported with older SDP signaling, we
644 // use a default a stream id.
645 stream_ids.push_back(kDefaultMsid);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000646 }
Seth Hampson5b4f0752018-04-02 16:31:36 -0700647 // If a track ID wasn't populated from the SSRC attributes OR the
Taylor Brandstetter5de6b752016-03-09 17:02:30 -0800648 // msid attribute, use default/random values.
Taylor Brandstetter5de6b752016-03-09 17:02:30 -0800649 if (track_id.empty()) {
650 // TODO(ronghuawu): What should we do if the track id doesn't appear?
651 // Create random string (which will be used as track label later)?
652 track_id = rtc::CreateRandomString(8);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000653 }
654
655 StreamParamsVec::iterator track = tracks->begin();
656 for (; track != tracks->end(); ++track) {
657 if (track->id == track_id) {
658 break;
659 }
660 }
661 if (track == tracks->end()) {
662 // If we don't find an existing track, create a new one.
663 tracks->push_back(StreamParams());
664 track = tracks->end() - 1;
665 }
666 track->add_ssrc(ssrc_info->ssrc_id);
667 track->cname = ssrc_info->cname;
Seth Hampson5b4f0752018-04-02 16:31:36 -0700668 track->set_stream_ids(stream_ids);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000669 track->id = track_id;
670 }
671}
672
Seth Hampson845e8782018-03-02 11:34:10 -0800673void GetMediaStreamIds(const ContentInfo* content,
674 std::set<std::string>* labels) {
Steve Antonb1c1de12017-12-21 15:14:30 -0800675 for (const StreamParams& stream_params :
676 content->media_description()->streams()) {
Seth Hampson845e8782018-03-02 11:34:10 -0800677 for (const std::string& stream_id : stream_params.stream_ids()) {
678 labels->insert(stream_id);
Steve Anton5a26a3a2018-02-28 11:38:47 -0800679 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000680 }
681}
682
683// RFC 5245
684// It is RECOMMENDED that default candidates be chosen based on the
685// likelihood of those candidates to work with the peer that is being
686// contacted. It is RECOMMENDED that relayed > reflexive > host.
687static const int kPreferenceUnknown = 0;
688static const int kPreferenceHost = 1;
689static const int kPreferenceReflexive = 2;
690static const int kPreferenceRelayed = 3;
691
692static int GetCandidatePreferenceFromType(const std::string& type) {
693 int preference = kPreferenceUnknown;
694 if (type == cricket::LOCAL_PORT_TYPE) {
695 preference = kPreferenceHost;
696 } else if (type == cricket::STUN_PORT_TYPE) {
697 preference = kPreferenceReflexive;
698 } else if (type == cricket::RELAY_PORT_TYPE) {
699 preference = kPreferenceRelayed;
700 } else {
nissec80e7412017-01-11 05:56:46 -0800701 RTC_NOTREACHED();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000702 }
703 return preference;
704}
705
guoweis@webrtc.org57ac2c82015-02-06 00:45:13 +0000706// Get ip and port of the default destination from the |candidates| with the
707// given value of |component_id|. The default candidate should be the one most
708// likely to work, typically IPv4 relay.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000709// RFC 5245
710// The value of |component_id| currently supported are 1 (RTP) and 2 (RTCP).
Steve Anton36b29d12017-10-30 09:57:42 -0700711// TODO(deadbeef): Decide the default destination in webrtcsession and
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000712// pass it down via SessionDescription.
Yves Gerey665174f2018-06-19 15:03:05 +0200713static void GetDefaultDestination(const std::vector<Candidate>& candidates,
714 int component_id,
715 std::string* port,
716 std::string* ip,
717 std::string* addr_type) {
perkj@webrtc.orgd105cc82014-11-07 11:22:06 +0000718 *addr_type = kConnectionIpv4Addrtype;
pthatcher@webrtc.orgc9d6d142014-10-23 23:37:22 +0000719 *port = kDummyPort;
720 *ip = kDummyAddress;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000721 int current_preference = kPreferenceUnknown;
guoweis@webrtc.org57ac2c82015-02-06 00:45:13 +0000722 int current_family = AF_UNSPEC;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000723 for (std::vector<Candidate>::const_iterator it = candidates.begin();
724 it != candidates.end(); ++it) {
725 if (it->component() != component_id) {
726 continue;
727 }
guoweis@webrtc.org57ac2c82015-02-06 00:45:13 +0000728 // Default destination should be UDP only.
729 if (it->protocol() != cricket::UDP_PROTOCOL_NAME) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000730 continue;
731 }
guoweis@webrtc.org57ac2c82015-02-06 00:45:13 +0000732 const int preference = GetCandidatePreferenceFromType(it->type());
733 const int family = it->address().ipaddr().family();
734 // See if this candidate is more preferable then the current one if it's the
735 // same family. Or if the current family is IPv4 already so we could safely
736 // ignore all IPv6 ones. WebRTC bug 4269.
737 // http://code.google.com/p/webrtc/issues/detail?id=4269
738 if ((preference <= current_preference && current_family == family) ||
739 (current_family == AF_INET && family == AF_INET6)) {
740 continue;
741 }
pthatcher@webrtc.orgc9d6d142014-10-23 23:37:22 +0000742 if (family == AF_INET) {
743 addr_type->assign(kConnectionIpv4Addrtype);
744 } else if (family == AF_INET6) {
745 addr_type->assign(kConnectionIpv6Addrtype);
746 }
guoweis@webrtc.org57ac2c82015-02-06 00:45:13 +0000747 current_preference = preference;
748 current_family = family;
749 *port = it->address().PortAsString();
750 *ip = it->address().ipaddr().ToString();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000751 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000752}
753
wu@webrtc.org4c3e9912014-07-16 21:03:13 +0000754// Gets "a=rtcp" line if found default RTCP candidate from |candidates|.
755static std::string GetRtcpLine(const std::vector<Candidate>& candidates) {
pthatcher@webrtc.orgc9d6d142014-10-23 23:37:22 +0000756 std::string rtcp_line, rtcp_port, rtcp_ip, addr_type;
Yves Gerey665174f2018-06-19 15:03:05 +0200757 GetDefaultDestination(candidates, ICE_CANDIDATE_COMPONENT_RTCP, &rtcp_port,
758 &rtcp_ip, &addr_type);
pthatcher@webrtc.orgc9d6d142014-10-23 23:37:22 +0000759 // Found default RTCP candidate.
760 // RFC 5245
761 // If the agent is utilizing RTCP, it MUST encode the RTCP candidate
762 // using the a=rtcp attribute as defined in RFC 3605.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000763
pthatcher@webrtc.orgc9d6d142014-10-23 23:37:22 +0000764 // RFC 3605
765 // rtcp-attribute = "a=rtcp:" port [nettype space addrtype space
766 // connection-address] CRLF
767 std::ostringstream os;
768 InitAttrLine(kAttributeRtcp, &os);
Yves Gerey665174f2018-06-19 15:03:05 +0200769 os << kSdpDelimiterColon << rtcp_port << " " << kConnectionNettype << " "
770 << addr_type << " " << rtcp_ip;
pthatcher@webrtc.orgc9d6d142014-10-23 23:37:22 +0000771 rtcp_line = os.str();
wu@webrtc.org4c3e9912014-07-16 21:03:13 +0000772 return rtcp_line;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000773}
774
775// Get candidates according to the mline index from SessionDescriptionInterface.
776static void GetCandidatesByMindex(const SessionDescriptionInterface& desci,
777 int mline_index,
778 std::vector<Candidate>* candidates) {
779 if (!candidates) {
780 return;
781 }
782 const IceCandidateCollection* cc = desci.candidates(mline_index);
783 for (size_t i = 0; i < cc->count(); ++i) {
784 const IceCandidateInterface* candidate = cc->at(i);
785 candidates->push_back(candidate->candidate());
786 }
787}
788
deadbeef90f1e1e2017-02-10 12:35:05 -0800789static bool IsValidPort(int port) {
790 return port >= 0 && port <= 65535;
791}
792
Steve Antone831b8c2018-02-01 12:22:16 -0800793std::string SdpSerialize(const JsepSessionDescription& jdesc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000794 const cricket::SessionDescription* desc = jdesc.description();
795 if (!desc) {
796 return "";
797 }
798
799 std::string message;
800
801 // Session Description.
802 AddLine(kSessionVersion, &message);
803 // Session Origin
804 // RFC 4566
805 // o=<username> <sess-id> <sess-version> <nettype> <addrtype>
806 // <unicast-address>
807 std::ostringstream os;
808 InitLine(kLineTypeOrigin, kSessionOriginUsername, &os);
Yves Gerey665174f2018-06-19 15:03:05 +0200809 const std::string& session_id =
810 jdesc.session_id().empty() ? kSessionOriginSessionId : jdesc.session_id();
811 const std::string& session_version = jdesc.session_version().empty()
812 ? kSessionOriginSessionVersion
813 : jdesc.session_version();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000814 os << " " << session_id << " " << session_version << " "
815 << kSessionOriginNettype << " " << kSessionOriginAddrtype << " "
816 << kSessionOriginAddress;
817 AddLine(os.str(), &message);
818 AddLine(kSessionName, &message);
819
820 // Time Description.
821 AddLine(kTimeDescription, &message);
822
823 // Group
824 if (desc->HasGroup(cricket::GROUP_TYPE_BUNDLE)) {
825 std::string group_line = kAttrGroup;
826 const cricket::ContentGroup* group =
827 desc->GetGroupByName(cricket::GROUP_TYPE_BUNDLE);
nisseede5da42017-01-12 05:15:36 -0800828 RTC_DCHECK(group != NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000829 const cricket::ContentNames& content_names = group->content_names();
830 for (cricket::ContentNames::const_iterator it = content_names.begin();
831 it != content_names.end(); ++it) {
832 group_line.append(" ");
833 group_line.append(*it);
834 }
835 AddLine(group_line, &message);
836 }
837
838 // MediaStream semantics
839 InitAttrLine(kAttributeMsidSemantics, &os);
840 os << kSdpDelimiterColon << " " << kMediaStreamSemantic;
wu@webrtc.org4c3e9912014-07-16 21:03:13 +0000841
Seth Hampson845e8782018-03-02 11:34:10 -0800842 std::set<std::string> media_stream_ids;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000843 const ContentInfo* audio_content = GetFirstAudioContent(desc);
844 if (audio_content)
Seth Hampson845e8782018-03-02 11:34:10 -0800845 GetMediaStreamIds(audio_content, &media_stream_ids);
wu@webrtc.org4c3e9912014-07-16 21:03:13 +0000846
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000847 const ContentInfo* video_content = GetFirstVideoContent(desc);
848 if (video_content)
Seth Hampson845e8782018-03-02 11:34:10 -0800849 GetMediaStreamIds(video_content, &media_stream_ids);
wu@webrtc.org4c3e9912014-07-16 21:03:13 +0000850
Seth Hampson845e8782018-03-02 11:34:10 -0800851 for (std::set<std::string>::const_iterator it = media_stream_ids.begin();
852 it != media_stream_ids.end(); ++it) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000853 os << " " << *it;
854 }
855 AddLine(os.str(), &message);
856
Taylor Brandstetter2f65ec52018-05-24 11:37:28 -0700857 // a=ice-lite
858 //
859 // TODO(deadbeef): It's weird that we need to iterate TransportInfos for
860 // this, when it's a session-level attribute. It really should be moved to a
861 // session-level structure like SessionDescription.
862 for (const cricket::TransportInfo& transport : desc->transport_infos()) {
863 if (transport.description.ice_mode == cricket::ICEMODE_LITE) {
864 InitAttrLine(kAttributeIceLite, &os);
865 AddLine(os.str(), &message);
866 break;
867 }
868 }
869
wu@webrtc.org4c3e9912014-07-16 21:03:13 +0000870 // Preserve the order of the media contents.
871 int mline_index = -1;
872 for (cricket::ContentInfos::const_iterator it = desc->contents().begin();
873 it != desc->contents().end(); ++it) {
Steve Antonb1c1de12017-12-21 15:14:30 -0800874 const MediaContentDescription* mdesc = it->media_description();
wu@webrtc.org4c3e9912014-07-16 21:03:13 +0000875 std::vector<Candidate> candidates;
876 GetCandidatesByMindex(jdesc, ++mline_index, &candidates);
deadbeef9d3584c2016-02-16 17:54:10 -0800877 BuildMediaDescription(&*it, desc->GetTransportInfoByName(it->name),
Steve Antone831b8c2018-02-01 12:22:16 -0800878 mdesc->type(), candidates, desc->msid_signaling(),
wu@webrtc.org4c3e9912014-07-16 21:03:13 +0000879 &message);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000880 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000881 return message;
882}
883
884// Serializes the passed in IceCandidateInterface to a SDP string.
885// candidate - The candidate to be serialized.
Honghai Zhang7fb69db2016-03-14 11:59:18 -0700886std::string SdpSerializeCandidate(const IceCandidateInterface& candidate) {
887 return SdpSerializeCandidate(candidate.candidate());
888}
889
890// Serializes a cricket Candidate.
891std::string SdpSerializeCandidate(const cricket::Candidate& candidate) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000892 std::string message;
Honghai Zhang7fb69db2016-03-14 11:59:18 -0700893 std::vector<cricket::Candidate> candidates(1, candidate);
honghaiza54a0802015-12-16 18:37:23 -0800894 BuildCandidate(candidates, true, &message);
wu@webrtc.orgec9f5fb2014-06-24 17:05:10 +0000895 // From WebRTC draft section 4.8.1.1 candidate-attribute will be
896 // just candidate:<candidate> not a=candidate:<blah>CRLF
nisseede5da42017-01-12 05:15:36 -0800897 RTC_DCHECK(message.find("a=") == 0);
wu@webrtc.orgec9f5fb2014-06-24 17:05:10 +0000898 message.erase(0, 2);
nisseede5da42017-01-12 05:15:36 -0800899 RTC_DCHECK(message.find(kLineBreak) == message.size() - 2);
wu@webrtc.orgec9f5fb2014-06-24 17:05:10 +0000900 message.resize(message.size() - 2);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000901 return message;
902}
903
904bool SdpDeserialize(const std::string& message,
905 JsepSessionDescription* jdesc,
906 SdpParseError* error) {
907 std::string session_id;
908 std::string session_version;
Peter Thatcher7cbd1882015-09-17 18:54:52 -0700909 TransportDescription session_td("", "");
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000910 RtpHeaderExtensions session_extmaps;
zhihuang38989e52017-03-21 11:04:53 -0700911 rtc::SocketAddress session_connection_addr;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000912 cricket::SessionDescription* desc = new cricket::SessionDescription();
913 std::vector<JsepIceCandidate*> candidates;
914 size_t current_pos = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000915
916 // Session Description
917 if (!ParseSessionDescription(message, &current_pos, &session_id,
deadbeefc80741f2015-10-22 13:14:45 -0700918 &session_version, &session_td, &session_extmaps,
zhihuang38989e52017-03-21 11:04:53 -0700919 &session_connection_addr, desc, error)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000920 delete desc;
921 return false;
922 }
923
924 // Media Description
deadbeefc80741f2015-10-22 13:14:45 -0700925 if (!ParseMediaDescription(message, session_td, session_extmaps, &current_pos,
zhihuang38989e52017-03-21 11:04:53 -0700926 session_connection_addr, desc, &candidates,
927 error)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000928 delete desc;
Yves Gerey665174f2018-06-19 15:03:05 +0200929 for (std::vector<JsepIceCandidate*>::const_iterator it = candidates.begin();
930 it != candidates.end(); ++it) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000931 delete *it;
932 }
933 return false;
934 }
935
936 jdesc->Initialize(desc, session_id, session_version);
937
Yves Gerey665174f2018-06-19 15:03:05 +0200938 for (std::vector<JsepIceCandidate*>::const_iterator it = candidates.begin();
939 it != candidates.end(); ++it) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000940 jdesc->AddCandidate(*it);
941 delete *it;
942 }
943 return true;
944}
945
946bool SdpDeserializeCandidate(const std::string& message,
947 JsepIceCandidate* jcandidate,
948 SdpParseError* error) {
nisseede5da42017-01-12 05:15:36 -0800949 RTC_DCHECK(jcandidate != NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000950 Candidate candidate;
951 if (!ParseCandidate(message, &candidate, error, true)) {
952 return false;
953 }
954 jcandidate->SetCandidate(candidate);
955 return true;
956}
957
Honghai Zhang7fb69db2016-03-14 11:59:18 -0700958bool SdpDeserializeCandidate(const std::string& transport_name,
959 const std::string& message,
960 cricket::Candidate* candidate,
961 SdpParseError* error) {
nisseede5da42017-01-12 05:15:36 -0800962 RTC_DCHECK(candidate != nullptr);
Honghai Zhang7fb69db2016-03-14 11:59:18 -0700963 if (!ParseCandidate(message, candidate, error, true)) {
964 return false;
965 }
966 candidate->set_transport_name(transport_name);
967 return true;
968}
969
Yves Gerey665174f2018-06-19 15:03:05 +0200970bool ParseCandidate(const std::string& message,
971 Candidate* candidate,
972 SdpParseError* error,
973 bool is_raw) {
nisseede5da42017-01-12 05:15:36 -0800974 RTC_DCHECK(candidate != NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000975
976 // Get the first line from |message|.
jiayl@webrtc.org7ec3f9f2014-08-08 23:09:15 +0000977 std::string first_line = message;
978 size_t pos = 0;
979 GetLine(message, &pos, &first_line);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000980
jiayl@webrtc.org7ec3f9f2014-08-08 23:09:15 +0000981 // Makes sure |message| contains only one line.
982 if (message.size() > first_line.size()) {
983 std::string left, right;
Donald Curtis0e07f922015-05-15 09:21:23 -0700984 if (rtc::tokenize_first(message, kNewLine, &left, &right) &&
985 !right.empty()) {
jiayl@webrtc.org7ec3f9f2014-08-08 23:09:15 +0000986 return ParseFailed(message, 0, "Expect one line only", error);
987 }
988 }
989
990 // From WebRTC draft section 4.8.1.1 candidate-attribute should be
991 // candidate:<candidate> when trickled, but we still support
992 // a=candidate:<blah>CRLF for backward compatibility and for parsing a line
993 // from the SDP.
994 if (IsLineType(first_line, kLineTypeAttributes)) {
995 first_line = first_line.substr(kLinePrefixLength);
996 }
997
998 std::string attribute_candidate;
999 std::string candidate_value;
1000
1001 // |first_line| must be in the form of "candidate:<value>".
Donald Curtis144d0182015-05-15 13:14:24 -07001002 if (!rtc::tokenize_first(first_line, kSdpDelimiterColon, &attribute_candidate,
1003 &candidate_value) ||
jiayl@webrtc.org7ec3f9f2014-08-08 23:09:15 +00001004 attribute_candidate != kAttributeCandidate) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001005 if (is_raw) {
1006 std::ostringstream description;
Yves Gerey665174f2018-06-19 15:03:05 +02001007 description << "Expect line: " << kAttributeCandidate << ":"
1008 << "<candidate-str>";
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001009 return ParseFailed(first_line, 0, description.str(), error);
1010 } else {
1011 return ParseFailedExpectLine(first_line, 0, kLineTypeAttributes,
1012 kAttributeCandidate, error);
1013 }
1014 }
1015
1016 std::vector<std::string> fields;
jiayl@webrtc.org7ec3f9f2014-08-08 23:09:15 +00001017 rtc::split(candidate_value, kSdpDelimiterSpace, &fields);
1018
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001019 // RFC 5245
1020 // a=candidate:<foundation> <component-id> <transport> <priority>
1021 // <connection-address> <port> typ <candidate-types>
1022 // [raddr <connection-address>] [rport <port>]
1023 // *(SP extension-att-name SP extension-att-value)
1024 const size_t expected_min_fields = 8;
1025 if (fields.size() < expected_min_fields ||
1026 (fields[6] != kAttributeCandidateTyp)) {
1027 return ParseFailedExpectMinFieldNum(first_line, expected_min_fields, error);
1028 }
jbauch083b73f2015-07-16 02:46:32 -07001029 const std::string& foundation = fields[0];
jiayl@webrtc.org7ec3f9f2014-08-08 23:09:15 +00001030
wu@webrtc.org5e760e72014-04-02 23:19:09 +00001031 int component_id = 0;
1032 if (!GetValueFromString(first_line, fields[1], &component_id, error)) {
1033 return false;
1034 }
jbauch083b73f2015-07-16 02:46:32 -07001035 const std::string& transport = fields[2];
Peter Boström0c4e06b2015-10-07 12:23:21 +02001036 uint32_t priority = 0;
wu@webrtc.org5e760e72014-04-02 23:19:09 +00001037 if (!GetValueFromString(first_line, fields[3], &priority, error)) {
1038 return false;
1039 }
jbauch083b73f2015-07-16 02:46:32 -07001040 const std::string& connection_address = fields[4];
wu@webrtc.org5e760e72014-04-02 23:19:09 +00001041 int port = 0;
1042 if (!GetValueFromString(first_line, fields[5], &port, error)) {
1043 return false;
1044 }
deadbeef90f1e1e2017-02-10 12:35:05 -08001045 if (!IsValidPort(port)) {
1046 return ParseFailed(first_line, "Invalid port number.", error);
1047 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001048 SocketAddress address(connection_address, port);
1049
1050 cricket::ProtocolType protocol;
hnslb68cc752016-12-13 10:33:41 -08001051 if (!StringToProto(transport.c_str(), &protocol)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001052 return ParseFailed(first_line, "Unsupported transport type.", error);
1053 }
hnslb68cc752016-12-13 10:33:41 -08001054 switch (protocol) {
1055 case cricket::PROTO_UDP:
1056 case cricket::PROTO_TCP:
1057 case cricket::PROTO_SSLTCP:
1058 // Supported protocol.
1059 break;
1060 default:
1061 return ParseFailed(first_line, "Unsupported transport type.", error);
1062 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001063
1064 std::string candidate_type;
jbauch083b73f2015-07-16 02:46:32 -07001065 const std::string& type = fields[7];
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001066 if (type == kCandidateHost) {
1067 candidate_type = cricket::LOCAL_PORT_TYPE;
1068 } else if (type == kCandidateSrflx) {
1069 candidate_type = cricket::STUN_PORT_TYPE;
1070 } else if (type == kCandidateRelay) {
1071 candidate_type = cricket::RELAY_PORT_TYPE;
Honghai Zhang7fb69db2016-03-14 11:59:18 -07001072 } else if (type == kCandidatePrflx) {
1073 candidate_type = cricket::PRFLX_PORT_TYPE;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001074 } else {
1075 return ParseFailed(first_line, "Unsupported candidate type.", error);
1076 }
1077
1078 size_t current_position = expected_min_fields;
1079 SocketAddress related_address;
1080 // The 2 optional fields for related address
1081 // [raddr <connection-address>] [rport <port>]
1082 if (fields.size() >= (current_position + 2) &&
1083 fields[current_position] == kAttributeCandidateRaddr) {
1084 related_address.SetIP(fields[++current_position]);
1085 ++current_position;
1086 }
1087 if (fields.size() >= (current_position + 2) &&
1088 fields[current_position] == kAttributeCandidateRport) {
wu@webrtc.org5e760e72014-04-02 23:19:09 +00001089 int port = 0;
Yves Gerey665174f2018-06-19 15:03:05 +02001090 if (!GetValueFromString(first_line, fields[++current_position], &port,
1091 error)) {
wu@webrtc.org5e760e72014-04-02 23:19:09 +00001092 return false;
1093 }
deadbeef90f1e1e2017-02-10 12:35:05 -08001094 if (!IsValidPort(port)) {
1095 return ParseFailed(first_line, "Invalid port number.", error);
1096 }
wu@webrtc.org5e760e72014-04-02 23:19:09 +00001097 related_address.SetPort(port);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001098 ++current_position;
1099 }
1100
mallinath@webrtc.org2d60c5e2014-08-08 22:29:20 +00001101 // If this is a TCP candidate, it has additional extension as defined in
1102 // RFC 6544.
1103 std::string tcptype;
1104 if (fields.size() >= (current_position + 2) &&
1105 fields[current_position] == kTcpCandidateType) {
1106 tcptype = fields[++current_position];
1107 ++current_position;
1108
1109 if (tcptype != cricket::TCPTYPE_ACTIVE_STR &&
1110 tcptype != cricket::TCPTYPE_PASSIVE_STR &&
1111 tcptype != cricket::TCPTYPE_SIMOPEN_STR) {
1112 return ParseFailed(first_line, "Invalid TCP candidate type.", error);
1113 }
1114
1115 if (protocol != cricket::PROTO_TCP) {
1116 return ParseFailed(first_line, "Invalid non-TCP candidate", error);
1117 }
1118 }
1119
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001120 // Extension
honghaiza54a0802015-12-16 18:37:23 -08001121 // Though non-standard, we support the ICE ufrag and pwd being signaled on
1122 // the candidate to avoid issues with confusing which generation a candidate
1123 // belongs to when trickling multiple generations at the same time.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001124 std::string username;
1125 std::string password;
Peter Boström0c4e06b2015-10-07 12:23:21 +02001126 uint32_t generation = 0;
honghaiza0c44ea2016-03-23 16:07:48 -07001127 uint16_t network_id = 0;
1128 uint16_t network_cost = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001129 for (size_t i = current_position; i + 1 < fields.size(); ++i) {
1130 // RFC 5245
1131 // *(SP extension-att-name SP extension-att-value)
1132 if (fields[i] == kAttributeCandidateGeneration) {
wu@webrtc.org5e760e72014-04-02 23:19:09 +00001133 if (!GetValueFromString(first_line, fields[++i], &generation, error)) {
1134 return false;
1135 }
honghaiza54a0802015-12-16 18:37:23 -08001136 } else if (fields[i] == kAttributeCandidateUfrag) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001137 username = fields[++i];
honghaiza54a0802015-12-16 18:37:23 -08001138 } else if (fields[i] == kAttributeCandidatePwd) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001139 password = fields[++i];
honghaiza0c44ea2016-03-23 16:07:48 -07001140 } else if (fields[i] == kAttributeCandidateNetworkId) {
1141 if (!GetValueFromString(first_line, fields[++i], &network_id, error)) {
1142 return false;
1143 }
honghaize1a0c942016-02-16 14:54:56 -08001144 } else if (fields[i] == kAttributeCandidateNetworkCost) {
1145 if (!GetValueFromString(first_line, fields[++i], &network_cost, error)) {
1146 return false;
1147 }
Honghai Zhang351d77b2016-05-20 15:08:29 -07001148 network_cost = std::min(network_cost, rtc::kNetworkCostMax);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001149 } else {
1150 // Skip the unknown extension.
1151 ++i;
1152 }
1153 }
1154
guoweis@webrtc.org61c12472015-01-15 06:53:07 +00001155 *candidate = Candidate(component_id, cricket::ProtoToString(protocol),
guoweis@webrtc.org950c5182014-12-16 23:01:31 +00001156 address, priority, username, password, candidate_type,
honghaiza0c44ea2016-03-23 16:07:48 -07001157 generation, foundation, network_id, network_cost);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001158 candidate->set_related_address(related_address);
mallinath@webrtc.org2d60c5e2014-08-08 22:29:20 +00001159 candidate->set_tcptype(tcptype);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001160 return true;
1161}
1162
1163bool ParseIceOptions(const std::string& line,
1164 std::vector<std::string>* transport_options,
1165 SdpParseError* error) {
1166 std::string ice_options;
1167 if (!GetValue(line, kAttributeIceOption, &ice_options, error)) {
1168 return false;
1169 }
1170 std::vector<std::string> fields;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001171 rtc::split(ice_options, kSdpDelimiterSpace, &fields);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001172 for (size_t i = 0; i < fields.size(); ++i) {
1173 transport_options->push_back(fields[i]);
1174 }
1175 return true;
1176}
1177
jiayl@webrtc.orgddb85ab2014-09-05 16:31:56 +00001178bool ParseSctpPort(const std::string& line,
1179 int* sctp_port,
1180 SdpParseError* error) {
1181 // draft-ietf-mmusic-sctp-sdp-07
1182 // a=sctp-port
1183 std::vector<std::string> fields;
jiayl@webrtc.orgddb85ab2014-09-05 16:31:56 +00001184 const size_t expected_min_fields = 2;
lally69f57602015-10-08 10:15:04 -07001185 rtc::split(line.substr(kLinePrefixLength), kSdpDelimiterColon, &fields);
1186 if (fields.size() < expected_min_fields) {
1187 fields.resize(0);
1188 rtc::split(line.substr(kLinePrefixLength), kSdpDelimiterSpace, &fields);
1189 }
jiayl@webrtc.orgddb85ab2014-09-05 16:31:56 +00001190 if (fields.size() < expected_min_fields) {
1191 return ParseFailedExpectMinFieldNum(line, expected_min_fields, error);
1192 }
1193 if (!rtc::FromString(fields[1], sctp_port)) {
lally69f57602015-10-08 10:15:04 -07001194 return ParseFailed(line, "Invalid sctp port value.", error);
jiayl@webrtc.orgddb85ab2014-09-05 16:31:56 +00001195 }
1196 return true;
1197}
1198
isheriff6f8d6862016-05-26 11:24:55 -07001199bool ParseExtmap(const std::string& line,
1200 RtpExtension* extmap,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001201 SdpParseError* error) {
1202 // RFC 5285
1203 // a=extmap:<value>["/"<direction>] <URI> <extensionattributes>
1204 std::vector<std::string> fields;
Yves Gerey665174f2018-06-19 15:03:05 +02001205 rtc::split(line.substr(kLinePrefixLength), kSdpDelimiterSpace, &fields);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001206 const size_t expected_min_fields = 2;
1207 if (fields.size() < expected_min_fields) {
1208 return ParseFailedExpectMinFieldNum(line, expected_min_fields, error);
1209 }
1210 std::string uri = fields[1];
1211
1212 std::string value_direction;
1213 if (!GetValue(fields[0], kAttributeExtmap, &value_direction, error)) {
1214 return false;
1215 }
1216 std::vector<std::string> sub_fields;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001217 rtc::split(value_direction, kSdpDelimiterSlash, &sub_fields);
wu@webrtc.org5e760e72014-04-02 23:19:09 +00001218 int value = 0;
1219 if (!GetValueFromString(line, sub_fields[0], &value, error)) {
1220 return false;
1221 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001222
jbauch5869f502017-06-29 12:31:36 -07001223 bool encrypted = false;
1224 if (uri == RtpExtension::kEncryptHeaderExtensionsUri) {
1225 // RFC 6904
Steve Anton36b29d12017-10-30 09:57:42 -07001226 // a=extmap:<value["/"<direction>] urn:ietf:params:rtp-hdrext:encrypt <URI>
1227 // <extensionattributes>
jbauch5869f502017-06-29 12:31:36 -07001228 const size_t expected_min_fields_encrypted = expected_min_fields + 1;
1229 if (fields.size() < expected_min_fields_encrypted) {
1230 return ParseFailedExpectMinFieldNum(line, expected_min_fields_encrypted,
Yves Gerey665174f2018-06-19 15:03:05 +02001231 error);
jbauch5869f502017-06-29 12:31:36 -07001232 }
1233
1234 encrypted = true;
1235 uri = fields[2];
1236 if (uri == RtpExtension::kEncryptHeaderExtensionsUri) {
1237 return ParseFailed(line, "Recursive encrypted header.", error);
1238 }
1239 }
1240
1241 *extmap = RtpExtension(uri, value, encrypted);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001242 return true;
1243}
1244
1245void BuildMediaDescription(const ContentInfo* content_info,
1246 const TransportInfo* transport_info,
Patrik Höglundb6b29e02018-06-21 16:58:01 +02001247 const cricket::MediaType media_type,
wu@webrtc.org4c3e9912014-07-16 21:03:13 +00001248 const std::vector<Candidate>& candidates,
Steve Antone831b8c2018-02-01 12:22:16 -08001249 int msid_signaling,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001250 std::string* message) {
nisseede5da42017-01-12 05:15:36 -08001251 RTC_DCHECK(message != NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001252 if (content_info == NULL || message == NULL) {
1253 return;
1254 }
Steve Anton36b29d12017-10-30 09:57:42 -07001255 // TODO(deadbeef): Rethink if we should use sprintfn instead of stringstream.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001256 // According to the style guide, streams should only be used for logging.
1257 // http://google-styleguide.googlecode.com/svn/
1258 // trunk/cppguide.xml?showone=Streams#Streams
1259 std::ostringstream os;
Steve Antonb1c1de12017-12-21 15:14:30 -08001260 const MediaContentDescription* media_desc = content_info->media_description();
1261 RTC_DCHECK(media_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001262
jiayl@webrtc.org9c16c392014-05-01 18:30:30 +00001263 int sctp_port = cricket::kSctpDefaultPort;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001264
1265 // RFC 4566
1266 // m=<media> <port> <proto> <fmt>
1267 // fmt is a list of payload type numbers that MAY be used in the session.
1268 const char* type = NULL;
1269 if (media_type == cricket::MEDIA_TYPE_AUDIO)
1270 type = kMediaTypeAudio;
1271 else if (media_type == cricket::MEDIA_TYPE_VIDEO)
1272 type = kMediaTypeVideo;
1273 else if (media_type == cricket::MEDIA_TYPE_DATA)
1274 type = kMediaTypeData;
1275 else
nissec80e7412017-01-11 05:56:46 -08001276 RTC_NOTREACHED();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001277
1278 std::string fmt;
1279 if (media_type == cricket::MEDIA_TYPE_VIDEO) {
Steve Antonb1c1de12017-12-21 15:14:30 -08001280 const VideoContentDescription* video_desc = media_desc->as_video();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001281 for (std::vector<cricket::VideoCodec>::const_iterator it =
1282 video_desc->codecs().begin();
1283 it != video_desc->codecs().end(); ++it) {
1284 fmt.append(" ");
Jonas Olsson6b1985d2018-07-05 11:59:48 +02001285 fmt.append(rtc::ToString(it->id));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001286 }
1287 } else if (media_type == cricket::MEDIA_TYPE_AUDIO) {
Steve Antonb1c1de12017-12-21 15:14:30 -08001288 const AudioContentDescription* audio_desc = media_desc->as_audio();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001289 for (std::vector<cricket::AudioCodec>::const_iterator it =
1290 audio_desc->codecs().begin();
1291 it != audio_desc->codecs().end(); ++it) {
1292 fmt.append(" ");
Jonas Olsson6b1985d2018-07-05 11:59:48 +02001293 fmt.append(rtc::ToString(it->id));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001294 }
1295 } else if (media_type == cricket::MEDIA_TYPE_DATA) {
Steve Antonb1c1de12017-12-21 15:14:30 -08001296 const DataContentDescription* data_desc = media_desc->as_data();
pthatcher@webrtc.org3341b402015-02-13 21:14:22 +00001297 if (IsDtlsSctp(media_desc->protocol())) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001298 fmt.append(" ");
jiayl@webrtc.org9c16c392014-05-01 18:30:30 +00001299
zstein4b2e0822017-02-17 19:48:38 -08001300 if (data_desc->use_sctpmap()) {
1301 for (std::vector<cricket::DataCodec>::const_iterator it =
1302 data_desc->codecs().begin();
1303 it != data_desc->codecs().end(); ++it) {
1304 if (cricket::CodecNamesEq(it->name,
1305 cricket::kGoogleSctpDataCodecName) &&
1306 it->GetParam(cricket::kCodecParamPort, &sctp_port)) {
1307 break;
1308 }
jiayl@webrtc.org9c16c392014-05-01 18:30:30 +00001309 }
jiayl@webrtc.org9c16c392014-05-01 18:30:30 +00001310
Jonas Olsson6b1985d2018-07-05 11:59:48 +02001311 fmt.append(rtc::ToString(sctp_port));
zstein4b2e0822017-02-17 19:48:38 -08001312 } else {
1313 fmt.append(kDefaultSctpmapProtocol);
1314 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001315 } else {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001316 for (std::vector<cricket::DataCodec>::const_iterator it =
Yves Gerey665174f2018-06-19 15:03:05 +02001317 data_desc->codecs().begin();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001318 it != data_desc->codecs().end(); ++it) {
1319 fmt.append(" ");
Jonas Olsson6b1985d2018-07-05 11:59:48 +02001320 fmt.append(rtc::ToString(it->id));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001321 }
1322 }
1323 }
1324 // The fmt must never be empty. If no codecs are found, set the fmt attribute
1325 // to 0.
1326 if (fmt.empty()) {
1327 fmt = " 0";
1328 }
1329
deadbeef25ed4352016-12-12 18:37:36 -08001330 // The port number in the m line will be updated later when associated with
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001331 // the candidates.
deadbeef25ed4352016-12-12 18:37:36 -08001332 //
1333 // A port value of 0 indicates that the m= section is rejected.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001334 // RFC 3264
1335 // To reject an offered stream, the port number in the corresponding stream in
1336 // the answer MUST be set to zero.
deadbeef25ed4352016-12-12 18:37:36 -08001337 //
1338 // However, the BUNDLE draft adds a new meaning to port zero, when used along
1339 // with a=bundle-only.
zhihuang38989e52017-03-21 11:04:53 -07001340 std::string port = kDummyPort;
1341 if (content_info->rejected || content_info->bundle_only) {
1342 port = kMediaPortRejected;
1343 } else if (!media_desc->connection_address().IsNil()) {
1344 port = rtc::ToString(media_desc->connection_address().port());
1345 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001346
Yves Gerey665174f2018-06-19 15:03:05 +02001347 rtc::SSLFingerprint* fp =
1348 (transport_info) ? transport_info->description.identity_fingerprint.get()
1349 : NULL;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001350
wu@webrtc.org4c3e9912014-07-16 21:03:13 +00001351 // Add the m and c lines.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001352 InitLine(kLineTypeMedia, type, &os);
1353 os << " " << port << " " << media_desc->protocol() << fmt;
zhihuang38989e52017-03-21 11:04:53 -07001354 AddLine(os.str(), message);
1355
1356 InitLine(kLineTypeConnection, kConnectionNettype, &os);
1357 if (media_desc->connection_address().IsNil()) {
1358 os << " " << kConnectionIpv4Addrtype << " " << kDummyAddress;
1359 } else if (media_desc->connection_address().family() == AF_INET) {
1360 os << " " << kConnectionIpv4Addrtype << " "
1361 << media_desc->connection_address().ipaddr().ToString();
1362 } else {
1363 os << " " << kConnectionIpv6Addrtype << " "
1364 << media_desc->connection_address().ipaddr().ToString();
1365 }
1366 AddLine(os.str(), message);
wu@webrtc.org4c3e9912014-07-16 21:03:13 +00001367
1368 // RFC 4566
1369 // b=AS:<bandwidth>
Peter Thatcherc0c3a862015-06-24 15:31:25 -07001370 if (media_desc->bandwidth() >= 1000) {
wu@webrtc.org4c3e9912014-07-16 21:03:13 +00001371 InitLine(kLineTypeSessionBandwidth, kApplicationSpecificMaximum, &os);
1372 os << kSdpDelimiterColon << (media_desc->bandwidth() / 1000);
1373 AddLine(os.str(), message);
1374 }
1375
deadbeef25ed4352016-12-12 18:37:36 -08001376 // Add the a=bundle-only line.
1377 if (content_info->bundle_only) {
1378 InitAttrLine(kAttributeBundleOnly, &os);
1379 AddLine(os.str(), message);
1380 }
1381
wu@webrtc.org4c3e9912014-07-16 21:03:13 +00001382 // Add the a=rtcp line.
pthatcher@webrtc.org3341b402015-02-13 21:14:22 +00001383 if (IsRtp(media_desc->protocol())) {
wu@webrtc.org4c3e9912014-07-16 21:03:13 +00001384 std::string rtcp_line = GetRtcpLine(candidates);
1385 if (!rtcp_line.empty()) {
1386 AddLine(rtcp_line, message);
1387 }
1388 }
1389
honghaiza54a0802015-12-16 18:37:23 -08001390 // Build the a=candidate lines. We don't include ufrag and pwd in the
1391 // candidates in the SDP to avoid redundancy.
1392 BuildCandidate(candidates, false, message);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001393
1394 // Use the transport_info to build the media level ice-ufrag and ice-pwd.
1395 if (transport_info) {
1396 // RFC 5245
1397 // ice-pwd-att = "ice-pwd" ":" password
1398 // ice-ufrag-att = "ice-ufrag" ":" ufrag
1399 // ice-ufrag
deadbeef3f7219b2015-12-28 15:17:14 -08001400 if (!transport_info->description.ice_ufrag.empty()) {
1401 InitAttrLine(kAttributeIceUfrag, &os);
1402 os << kSdpDelimiterColon << transport_info->description.ice_ufrag;
1403 AddLine(os.str(), message);
1404 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001405 // ice-pwd
deadbeef3f7219b2015-12-28 15:17:14 -08001406 if (!transport_info->description.ice_pwd.empty()) {
1407 InitAttrLine(kAttributeIcePwd, &os);
1408 os << kSdpDelimiterColon << transport_info->description.ice_pwd;
1409 AddLine(os.str(), message);
1410 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001411
1412 // draft-petithuguenin-mmusic-ice-attributes-level-03
1413 BuildIceOptions(transport_info->description.transport_options, message);
1414
1415 // RFC 4572
1416 // fingerprint-attribute =
1417 // "fingerprint" ":" hash-func SP fingerprint
1418 if (fp) {
1419 // Insert the fingerprint attribute.
1420 InitAttrLine(kAttributeFingerprint, &os);
Yves Gerey665174f2018-06-19 15:03:05 +02001421 os << kSdpDelimiterColon << fp->algorithm << kSdpDelimiterSpace
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001422 << fp->GetRfc4572Fingerprint();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001423 AddLine(os.str(), message);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001424
1425 // Inserting setup attribute.
1426 if (transport_info->description.connection_role !=
Yves Gerey665174f2018-06-19 15:03:05 +02001427 cricket::CONNECTIONROLE_NONE) {
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001428 // Making sure we are not using "passive" mode.
1429 cricket::ConnectionRole role =
1430 transport_info->description.connection_role;
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00001431 std::string dtls_role_str;
nissec16fa5e2017-02-07 07:18:43 -08001432 const bool success =
1433 cricket::ConnectionRoleToString(role, &dtls_role_str);
1434 RTC_DCHECK(success);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001435 InitAttrLine(kAttributeSetup, &os);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001436 os << kSdpDelimiterColon << dtls_role_str;
1437 AddLine(os.str(), message);
1438 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001439 }
1440 }
1441
1442 // RFC 3388
1443 // mid-attribute = "a=mid:" identification-tag
1444 // identification-tag = token
1445 // Use the content name as the mid identification-tag.
1446 InitAttrLine(kAttributeMid, &os);
1447 os << kSdpDelimiterColon << content_info->name;
1448 AddLine(os.str(), message);
1449
pthatcher@webrtc.org3341b402015-02-13 21:14:22 +00001450 if (IsDtlsSctp(media_desc->protocol())) {
Steve Antonb1c1de12017-12-21 15:14:30 -08001451 const DataContentDescription* data_desc = media_desc->as_data();
zstein4b2e0822017-02-17 19:48:38 -08001452 bool use_sctpmap = data_desc->use_sctpmap();
1453 BuildSctpContentAttributes(message, sctp_port, use_sctpmap);
pthatcher@webrtc.org3341b402015-02-13 21:14:22 +00001454 } else if (IsRtp(media_desc->protocol())) {
Steve Antone831b8c2018-02-01 12:22:16 -08001455 BuildRtpContentAttributes(media_desc, media_type, msid_signaling, message);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001456 }
1457}
1458
zstein4b2e0822017-02-17 19:48:38 -08001459void BuildSctpContentAttributes(std::string* message,
1460 int sctp_port,
1461 bool use_sctpmap) {
wu@webrtc.org78187522013-10-07 23:32:02 +00001462 std::ostringstream os;
zstein4b2e0822017-02-17 19:48:38 -08001463 if (use_sctpmap) {
1464 // draft-ietf-mmusic-sctp-sdp-04
1465 // a=sctpmap:sctpmap-number protocol [streams]
1466 InitAttrLine(kAttributeSctpmap, &os);
1467 os << kSdpDelimiterColon << sctp_port << kSdpDelimiterSpace
1468 << kDefaultSctpmapProtocol << kSdpDelimiterSpace
1469 << cricket::kMaxSctpStreams;
1470 } else {
1471 // draft-ietf-mmusic-sctp-sdp-23
1472 // a=sctp-port:<port>
1473 InitAttrLine(kAttributeSctpPort, &os);
1474 os << kSdpDelimiterColon << sctp_port;
1475 // TODO(zstein): emit max-message-size here
1476 }
wu@webrtc.org78187522013-10-07 23:32:02 +00001477 AddLine(os.str(), message);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001478}
1479
deadbeef9d3584c2016-02-16 17:54:10 -08001480void BuildRtpContentAttributes(const MediaContentDescription* media_desc,
Patrik Höglundb6b29e02018-06-21 16:58:01 +02001481 const cricket::MediaType media_type,
Steve Antone831b8c2018-02-01 12:22:16 -08001482 int msid_signaling,
deadbeef9d3584c2016-02-16 17:54:10 -08001483 std::string* message) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001484 std::ostringstream os;
1485 // RFC 5285
1486 // a=extmap:<value>["/"<direction>] <URI> <extensionattributes>
1487 // The definitions MUST be either all session level or all media level. This
1488 // implementation uses all media level.
1489 for (size_t i = 0; i < media_desc->rtp_header_extensions().size(); ++i) {
jbauch5869f502017-06-29 12:31:36 -07001490 const RtpExtension& extension = media_desc->rtp_header_extensions()[i];
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001491 InitAttrLine(kAttributeExtmap, &os);
jbauch5869f502017-06-29 12:31:36 -07001492 os << kSdpDelimiterColon << extension.id;
1493 if (extension.encrypt) {
1494 os << kSdpDelimiterSpace << RtpExtension::kEncryptHeaderExtensionsUri;
1495 }
1496 os << kSdpDelimiterSpace << extension.uri;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001497 AddLine(os.str(), message);
1498 }
1499
1500 // RFC 3264
1501 // a=sendrecv || a=sendonly || a=sendrecv || a=inactive
deadbeefc80741f2015-10-22 13:14:45 -07001502 switch (media_desc->direction()) {
Steve Anton4e70a722017-11-28 14:57:10 -08001503 case RtpTransceiverDirection::kInactive:
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001504 InitAttrLine(kAttributeInactive, &os);
1505 break;
Steve Anton4e70a722017-11-28 14:57:10 -08001506 case RtpTransceiverDirection::kSendOnly:
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001507 InitAttrLine(kAttributeSendOnly, &os);
1508 break;
Steve Anton4e70a722017-11-28 14:57:10 -08001509 case RtpTransceiverDirection::kRecvOnly:
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001510 InitAttrLine(kAttributeRecvOnly, &os);
1511 break;
Steve Anton4e70a722017-11-28 14:57:10 -08001512 case RtpTransceiverDirection::kSendRecv:
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001513 default:
1514 InitAttrLine(kAttributeSendRecv, &os);
1515 break;
1516 }
1517 AddLine(os.str(), message);
1518
Seth Hampson5b4f0752018-04-02 16:31:36 -07001519 // Specified in https://datatracker.ietf.org/doc/draft-ietf-mmusic-msid/16/
1520 // a=msid:<msid-id> <msid-appdata>
1521 // The msid-id is a 1*64 token char representing the media stream id, and the
1522 // msid-appdata is a 1*64 token char representing the track id. There is a
1523 // line for every media stream, with a special msid-id value of "-"
1524 // representing no streams. The value of "msid-appdata" MUST be identical for
1525 // all lines.
Steve Antone831b8c2018-02-01 12:22:16 -08001526 if (msid_signaling & cricket::kMsidSignalingMediaSection) {
1527 const StreamParamsVec& streams = media_desc->streams();
1528 if (streams.size() == 1u) {
1529 const StreamParams& track = streams[0];
Seth Hampson5b4f0752018-04-02 16:31:36 -07001530 std::vector<std::string> stream_ids = track.stream_ids();
1531 if (stream_ids.empty()) {
1532 stream_ids.push_back(kNoStreamMsid);
1533 }
1534 for (const std::string& stream_id : stream_ids) {
1535 InitAttrLine(kAttributeMsid, &os);
1536 os << kSdpDelimiterColon << stream_id << kSdpDelimiterSpace << track.id;
1537 AddLine(os.str(), message);
1538 }
Steve Antone831b8c2018-02-01 12:22:16 -08001539 } else if (streams.size() > 1u) {
1540 RTC_LOG(LS_WARNING)
1541 << "Trying to serialize Unified Plan SDP with more than "
Jonas Olsson45cc8902018-02-13 10:37:07 +01001542 "one track in a media section. Omitting 'a=msid'.";
deadbeef9d3584c2016-02-16 17:54:10 -08001543 }
1544 }
1545
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001546 // RFC 5761
1547 // a=rtcp-mux
1548 if (media_desc->rtcp_mux()) {
1549 InitAttrLine(kAttributeRtcpMux, &os);
1550 AddLine(os.str(), message);
1551 }
1552
deadbeef13871492015-12-09 12:37:51 -08001553 // RFC 5506
1554 // a=rtcp-rsize
1555 if (media_desc->rtcp_reduced_size()) {
1556 InitAttrLine(kAttributeRtcpReducedSize, &os);
1557 AddLine(os.str(), message);
1558 }
1559
deadbeefd45aea82017-09-16 01:24:29 -07001560 if (media_desc->conference_mode()) {
1561 InitAttrLine(kAttributeXGoogleFlag, &os);
1562 os << kSdpDelimiterColon << kValueConference;
1563 AddLine(os.str(), message);
1564 }
1565
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001566 // RFC 4568
1567 // a=crypto:<tag> <crypto-suite> <key-params> [<session-params>]
1568 for (std::vector<CryptoParams>::const_iterator it =
1569 media_desc->cryptos().begin();
1570 it != media_desc->cryptos().end(); ++it) {
1571 InitAttrLine(kAttributeCrypto, &os);
1572 os << kSdpDelimiterColon << it->tag << " " << it->cipher_suite << " "
1573 << it->key_params;
1574 if (!it->session_params.empty()) {
1575 os << " " << it->session_params;
1576 }
1577 AddLine(os.str(), message);
1578 }
1579
1580 // RFC 4566
1581 // a=rtpmap:<payload type> <encoding name>/<clock rate>
1582 // [/<encodingparameters>]
1583 BuildRtpMap(media_desc, media_type, message);
1584
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001585 for (StreamParamsVec::const_iterator track = media_desc->streams().begin();
1586 track != media_desc->streams().end(); ++track) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001587 // Build the ssrc-group lines.
1588 for (size_t i = 0; i < track->ssrc_groups.size(); ++i) {
1589 // RFC 5576
1590 // a=ssrc-group:<semantics> <ssrc-id> ...
1591 if (track->ssrc_groups[i].ssrcs.empty()) {
1592 continue;
1593 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001594 InitAttrLine(kAttributeSsrcGroup, &os);
1595 os << kSdpDelimiterColon << track->ssrc_groups[i].semantics;
Peter Boström0c4e06b2015-10-07 12:23:21 +02001596 std::vector<uint32_t>::const_iterator ssrc =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001597 track->ssrc_groups[i].ssrcs.begin();
1598 for (; ssrc != track->ssrc_groups[i].ssrcs.end(); ++ssrc) {
Jonas Olsson6b1985d2018-07-05 11:59:48 +02001599 os << kSdpDelimiterSpace << rtc::ToString(*ssrc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001600 }
1601 AddLine(os.str(), message);
1602 }
1603 // Build the ssrc lines for each ssrc.
1604 for (size_t i = 0; i < track->ssrcs.size(); ++i) {
Peter Boström0c4e06b2015-10-07 12:23:21 +02001605 uint32_t ssrc = track->ssrcs[i];
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001606 // RFC 5576
1607 // a=ssrc:<ssrc-id> cname:<value>
Yves Gerey665174f2018-06-19 15:03:05 +02001608 AddSsrcLine(ssrc, kSsrcAttributeCname, track->cname, message);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001609
Steve Antone831b8c2018-02-01 12:22:16 -08001610 if (msid_signaling & cricket::kMsidSignalingSsrcAttribute) {
1611 // draft-alvestrand-mmusic-msid-00
1612 // a=ssrc:<ssrc-id> msid:identifier [appdata]
1613 // The appdata consists of the "id" attribute of a MediaStreamTrack,
1614 // which corresponds to the "id" attribute of StreamParams.
Seth Hampson5b4f0752018-04-02 16:31:36 -07001615 // Since a=ssrc msid signaling is used in Plan B SDP semantics, and
1616 // multiple stream ids are not supported for Plan B, we are only adding
1617 // a line for the first media stream id here.
Seth Hampson845e8782018-03-02 11:34:10 -08001618 const std::string& stream_id = track->first_stream_id();
Steve Antone831b8c2018-02-01 12:22:16 -08001619 InitAttrLine(kAttributeSsrc, &os);
1620 os << kSdpDelimiterColon << ssrc << kSdpDelimiterSpace
1621 << kSsrcAttributeMsid << kSdpDelimiterColon << stream_id
1622 << kSdpDelimiterSpace << track->id;
1623 AddLine(os.str(), message);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001624
Steve Antone831b8c2018-02-01 12:22:16 -08001625 // TODO(ronghuawu): Remove below code which is for backward
1626 // compatibility.
1627 // draft-alvestrand-rtcweb-mid-01
1628 // a=ssrc:<ssrc-id> mslabel:<value>
1629 // The label isn't yet defined.
1630 // a=ssrc:<ssrc-id> label:<value>
Seth Hampson5b4f0752018-04-02 16:31:36 -07001631 AddSsrcLine(ssrc, kSsrcAttributeMslabel, stream_id, message);
Steve Antone831b8c2018-02-01 12:22:16 -08001632 AddSsrcLine(ssrc, kSSrcAttributeLabel, track->id, message);
1633 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001634 }
1635 }
1636}
1637
1638void WriteFmtpHeader(int payload_type, std::ostringstream* os) {
1639 // fmtp header: a=fmtp:|payload_type| <parameters>
1640 // Add a=fmtp
1641 InitAttrLine(kAttributeFmtp, os);
1642 // Add :|payload_type|
1643 *os << kSdpDelimiterColon << payload_type;
1644}
1645
1646void WriteRtcpFbHeader(int payload_type, std::ostringstream* os) {
1647 // rtcp-fb header: a=rtcp-fb:|payload_type|
1648 // <parameters>/<ccm <ccm_parameters>>
1649 // Add a=rtcp-fb
1650 InitAttrLine(kAttributeRtcpFb, os);
1651 // Add :
1652 *os << kSdpDelimiterColon;
1653 if (payload_type == kWildcardPayloadType) {
1654 *os << "*";
1655 } else {
1656 *os << payload_type;
1657 }
1658}
1659
1660void WriteFmtpParameter(const std::string& parameter_name,
1661 const std::string& parameter_value,
1662 std::ostringstream* os) {
1663 // fmtp parameters: |parameter_name|=|parameter_value|
1664 *os << parameter_name << kSdpDelimiterEqual << parameter_value;
1665}
1666
1667void WriteFmtpParameters(const cricket::CodecParameterMap& parameters,
1668 std::ostringstream* os) {
1669 for (cricket::CodecParameterMap::const_iterator fmtp = parameters.begin();
1670 fmtp != parameters.end(); ++fmtp) {
hta62a216e2016-04-15 11:02:14 -07001671 // Parameters are a semicolon-separated list, no spaces.
1672 // The list is separated from the header by a space.
1673 if (fmtp == parameters.begin()) {
1674 *os << kSdpDelimiterSpace;
1675 } else {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001676 *os << kSdpDelimiterSemicolon;
1677 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001678 WriteFmtpParameter(fmtp->first, fmtp->second, os);
1679 }
1680}
1681
1682bool IsFmtpParam(const std::string& name) {
ossuaa4b0772017-01-30 07:41:18 -08001683 // RFC 4855, section 3 specifies the mapping of media format parameters to SDP
1684 // parameters. Only ptime, maxptime, channels and rate are placed outside of
1685 // the fmtp line. In WebRTC, channels and rate are already handled separately
1686 // and thus not included in the CodecParameterMap.
1687 return name != kCodecParamPTime && name != kCodecParamMaxPTime;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001688}
1689
1690// Retreives fmtp parameters from |params|, which may contain other parameters
1691// as well, and puts them in |fmtp_parameters|.
1692void GetFmtpParams(const cricket::CodecParameterMap& params,
1693 cricket::CodecParameterMap* fmtp_parameters) {
1694 for (cricket::CodecParameterMap::const_iterator iter = params.begin();
1695 iter != params.end(); ++iter) {
1696 if (IsFmtpParam(iter->first)) {
1697 (*fmtp_parameters)[iter->first] = iter->second;
1698 }
1699 }
1700}
1701
1702template <class T>
1703void AddFmtpLine(const T& codec, std::string* message) {
1704 cricket::CodecParameterMap fmtp_parameters;
1705 GetFmtpParams(codec.params, &fmtp_parameters);
1706 if (fmtp_parameters.empty()) {
1707 // No need to add an fmtp if it will have no (optional) parameters.
1708 return;
1709 }
1710 std::ostringstream os;
1711 WriteFmtpHeader(codec.id, &os);
1712 WriteFmtpParameters(fmtp_parameters, &os);
1713 AddLine(os.str(), message);
1714 return;
1715}
1716
1717template <class T>
1718void AddRtcpFbLines(const T& codec, std::string* message) {
1719 for (std::vector<cricket::FeedbackParam>::const_iterator iter =
1720 codec.feedback_params.params().begin();
1721 iter != codec.feedback_params.params().end(); ++iter) {
1722 std::ostringstream os;
1723 WriteRtcpFbHeader(codec.id, &os);
1724 os << " " << iter->id();
1725 if (!iter->param().empty()) {
1726 os << " " << iter->param();
1727 }
1728 AddLine(os.str(), message);
1729 }
1730}
1731
Yves Gerey665174f2018-06-19 15:03:05 +02001732bool AddSctpDataCodec(DataContentDescription* media_desc, int sctp_port) {
solenberg9fa49752016-10-08 13:02:44 -07001733 for (const auto& codec : media_desc->codecs()) {
1734 if (cricket::CodecNamesEq(codec.name, cricket::kGoogleSctpDataCodecName)) {
Yves Gerey665174f2018-06-19 15:03:05 +02001735 return ParseFailed("", "Can't have multiple sctp port attributes.", NULL);
solenberg9fa49752016-10-08 13:02:44 -07001736 }
jiayl@webrtc.orgddb85ab2014-09-05 16:31:56 +00001737 }
1738 // Add the SCTP Port number as a pseudo-codec "port" parameter
solenberg9fa49752016-10-08 13:02:44 -07001739 cricket::DataCodec codec_port(cricket::kGoogleSctpDataCodecPlType,
deadbeef67cf2c12016-04-13 10:07:16 -07001740 cricket::kGoogleSctpDataCodecName);
jiayl@webrtc.orgddb85ab2014-09-05 16:31:56 +00001741 codec_port.SetParam(cricket::kCodecParamPort, sctp_port);
Mirko Bonadei675513b2017-11-09 11:09:25 +01001742 RTC_LOG(INFO) << "AddSctpDataCodec: Got SCTP Port Number " << sctp_port;
jiayl@webrtc.orgddb85ab2014-09-05 16:31:56 +00001743 media_desc->AddCodec(codec_port);
1744 return true;
1745}
1746
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001747bool GetMinValue(const std::vector<int>& values, int* value) {
1748 if (values.empty()) {
1749 return false;
1750 }
1751 std::vector<int>::const_iterator found =
1752 std::min_element(values.begin(), values.end());
1753 *value = *found;
1754 return true;
1755}
1756
1757bool GetParameter(const std::string& name,
Yves Gerey665174f2018-06-19 15:03:05 +02001758 const cricket::CodecParameterMap& params,
1759 int* value) {
1760 std::map<std::string, std::string>::const_iterator found = params.find(name);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001761 if (found == params.end()) {
1762 return false;
1763 }
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001764 if (!rtc::FromString(found->second, value)) {
wu@webrtc.org5e760e72014-04-02 23:19:09 +00001765 return false;
1766 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001767 return true;
1768}
1769
1770void BuildRtpMap(const MediaContentDescription* media_desc,
Patrik Höglundb6b29e02018-06-21 16:58:01 +02001771 const cricket::MediaType media_type,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001772 std::string* message) {
nisseede5da42017-01-12 05:15:36 -08001773 RTC_DCHECK(message != NULL);
1774 RTC_DCHECK(media_desc != NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001775 std::ostringstream os;
1776 if (media_type == cricket::MEDIA_TYPE_VIDEO) {
Steve Antonb1c1de12017-12-21 15:14:30 -08001777 const VideoContentDescription* video_desc = media_desc->as_video();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001778 for (std::vector<cricket::VideoCodec>::const_iterator it =
1779 video_desc->codecs().begin();
1780 it != video_desc->codecs().end(); ++it) {
1781 // RFC 4566
1782 // a=rtpmap:<payload type> <encoding name>/<clock rate>
1783 // [/<encodingparameters>]
1784 if (it->id != kWildcardPayloadType) {
1785 InitAttrLine(kAttributeRtpmap, &os);
deadbeefe814a0d2017-02-25 18:15:09 -08001786 os << kSdpDelimiterColon << it->id << " " << it->name << "/"
1787 << cricket::kVideoCodecClockrate;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001788 AddLine(os.str(), message);
1789 }
1790 AddRtcpFbLines(*it, message);
1791 AddFmtpLine(*it, message);
1792 }
1793 } else if (media_type == cricket::MEDIA_TYPE_AUDIO) {
Steve Antonb1c1de12017-12-21 15:14:30 -08001794 const AudioContentDescription* audio_desc = media_desc->as_audio();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001795 std::vector<int> ptimes;
1796 std::vector<int> maxptimes;
1797 int max_minptime = 0;
1798 for (std::vector<cricket::AudioCodec>::const_iterator it =
1799 audio_desc->codecs().begin();
1800 it != audio_desc->codecs().end(); ++it) {
nisseede5da42017-01-12 05:15:36 -08001801 RTC_DCHECK(!it->name.empty());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001802 // RFC 4566
1803 // a=rtpmap:<payload type> <encoding name>/<clock rate>
1804 // [/<encodingparameters>]
1805 InitAttrLine(kAttributeRtpmap, &os);
1806 os << kSdpDelimiterColon << it->id << " ";
1807 os << it->name << "/" << it->clockrate;
1808 if (it->channels != 1) {
1809 os << "/" << it->channels;
1810 }
1811 AddLine(os.str(), message);
1812 AddRtcpFbLines(*it, message);
1813 AddFmtpLine(*it, message);
1814 int minptime = 0;
1815 if (GetParameter(kCodecParamMinPTime, it->params, &minptime)) {
1816 max_minptime = std::max(minptime, max_minptime);
1817 }
1818 int ptime;
1819 if (GetParameter(kCodecParamPTime, it->params, &ptime)) {
1820 ptimes.push_back(ptime);
1821 }
1822 int maxptime;
1823 if (GetParameter(kCodecParamMaxPTime, it->params, &maxptime)) {
1824 maxptimes.push_back(maxptime);
1825 }
1826 }
1827 // Populate the maxptime attribute with the smallest maxptime of all codecs
1828 // under the same m-line.
1829 int min_maxptime = INT_MAX;
1830 if (GetMinValue(maxptimes, &min_maxptime)) {
1831 AddAttributeLine(kCodecParamMaxPTime, min_maxptime, message);
1832 }
nisseede5da42017-01-12 05:15:36 -08001833 RTC_DCHECK(min_maxptime > max_minptime);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001834 // Populate the ptime attribute with the smallest ptime or the largest
1835 // minptime, whichever is the largest, for all codecs under the same m-line.
1836 int ptime = INT_MAX;
1837 if (GetMinValue(ptimes, &ptime)) {
1838 ptime = std::min(ptime, min_maxptime);
1839 ptime = std::max(ptime, max_minptime);
1840 AddAttributeLine(kCodecParamPTime, ptime, message);
1841 }
1842 } else if (media_type == cricket::MEDIA_TYPE_DATA) {
Steve Antonb1c1de12017-12-21 15:14:30 -08001843 const DataContentDescription* data_desc = media_desc->as_data();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001844 for (std::vector<cricket::DataCodec>::const_iterator it =
Yves Gerey665174f2018-06-19 15:03:05 +02001845 data_desc->codecs().begin();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001846 it != data_desc->codecs().end(); ++it) {
1847 // RFC 4566
1848 // a=rtpmap:<payload type> <encoding name>/<clock rate>
1849 // [/<encodingparameters>]
1850 InitAttrLine(kAttributeRtpmap, &os);
Yves Gerey665174f2018-06-19 15:03:05 +02001851 os << kSdpDelimiterColon << it->id << " " << it->name << "/"
1852 << it->clockrate;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001853 AddLine(os.str(), message);
1854 }
1855 }
1856}
1857
1858void BuildCandidate(const std::vector<Candidate>& candidates,
honghaiza54a0802015-12-16 18:37:23 -08001859 bool include_ufrag,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001860 std::string* message) {
1861 std::ostringstream os;
1862
1863 for (std::vector<Candidate>::const_iterator it = candidates.begin();
1864 it != candidates.end(); ++it) {
1865 // RFC 5245
1866 // a=candidate:<foundation> <component-id> <transport> <priority>
1867 // <connection-address> <port> typ <candidate-types>
1868 // [raddr <connection-address>] [rport <port>]
1869 // *(SP extension-att-name SP extension-att-value)
1870 std::string type;
1871 // Map the cricket candidate type to "host" / "srflx" / "prflx" / "relay"
1872 if (it->type() == cricket::LOCAL_PORT_TYPE) {
1873 type = kCandidateHost;
1874 } else if (it->type() == cricket::STUN_PORT_TYPE) {
1875 type = kCandidateSrflx;
1876 } else if (it->type() == cricket::RELAY_PORT_TYPE) {
1877 type = kCandidateRelay;
Honghai Zhang7fb69db2016-03-14 11:59:18 -07001878 } else if (it->type() == cricket::PRFLX_PORT_TYPE) {
1879 type = kCandidatePrflx;
1880 // Peer reflexive candidate may be signaled for being removed.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001881 } else {
nissec80e7412017-01-11 05:56:46 -08001882 RTC_NOTREACHED();
Peter Thatcher019087f2015-04-28 09:06:26 -07001883 // Never write out candidates if we don't know the type.
1884 continue;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001885 }
1886
1887 InitAttrLine(kAttributeCandidate, &os);
Yves Gerey665174f2018-06-19 15:03:05 +02001888 os << kSdpDelimiterColon << it->foundation() << " " << it->component()
1889 << " " << it->protocol() << " " << it->priority() << " "
Zach Steinb336c272018-08-09 01:16:13 -07001890 << (it->address().ipaddr().IsNil() ? it->address().hostname()
1891 : it->address().ipaddr().ToString())
1892 << " " << it->address().PortAsString() << " " << kAttributeCandidateTyp
1893 << " " << type << " ";
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001894
1895 // Related address
1896 if (!it->related_address().IsNil()) {
1897 os << kAttributeCandidateRaddr << " "
1898 << it->related_address().ipaddr().ToString() << " "
1899 << kAttributeCandidateRport << " "
1900 << it->related_address().PortAsString() << " ";
1901 }
1902
mallinath@webrtc.org2d60c5e2014-08-08 22:29:20 +00001903 if (it->protocol() == cricket::TCP_PROTOCOL_NAME) {
mallinath@webrtc.orge999bd02014-08-13 06:05:55 +00001904 os << kTcpCandidateType << " " << it->tcptype() << " ";
mallinath@webrtc.org2d60c5e2014-08-08 22:29:20 +00001905 }
1906
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001907 // Extensions
1908 os << kAttributeCandidateGeneration << " " << it->generation();
honghaiza54a0802015-12-16 18:37:23 -08001909 if (include_ufrag && !it->username().empty()) {
1910 os << " " << kAttributeCandidateUfrag << " " << it->username();
1911 }
honghaiza0c44ea2016-03-23 16:07:48 -07001912 if (it->network_id() > 0) {
1913 os << " " << kAttributeCandidateNetworkId << " " << it->network_id();
1914 }
honghaize1a0c942016-02-16 14:54:56 -08001915 if (it->network_cost() > 0) {
1916 os << " " << kAttributeCandidateNetworkCost << " " << it->network_cost();
1917 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001918
1919 AddLine(os.str(), message);
1920 }
1921}
1922
1923void BuildIceOptions(const std::vector<std::string>& transport_options,
1924 std::string* message) {
1925 if (!transport_options.empty()) {
1926 std::ostringstream os;
1927 InitAttrLine(kAttributeIceOption, &os);
1928 os << kSdpDelimiterColon << transport_options[0];
1929 for (size_t i = 1; i < transport_options.size(); ++i) {
1930 os << kSdpDelimiterSpace << transport_options[i];
1931 }
1932 AddLine(os.str(), message);
1933 }
1934}
1935
pthatcher@webrtc.org3341b402015-02-13 21:14:22 +00001936bool IsRtp(const std::string& protocol) {
1937 return protocol.empty() ||
Yves Gerey665174f2018-06-19 15:03:05 +02001938 (protocol.find(cricket::kMediaProtocolRtpPrefix) != std::string::npos);
pthatcher@webrtc.org3341b402015-02-13 21:14:22 +00001939}
1940
1941bool IsDtlsSctp(const std::string& protocol) {
1942 // This intentionally excludes "SCTP" and "SCTP/DTLS".
lally@webrtc.orga7470932015-02-24 20:19:21 +00001943 return protocol.find(cricket::kMediaProtocolDtlsSctp) != std::string::npos;
pthatcher@webrtc.org3341b402015-02-13 21:14:22 +00001944}
1945
zhihuang38989e52017-03-21 11:04:53 -07001946bool ParseConnectionData(const std::string& line,
1947 rtc::SocketAddress* addr,
1948 SdpParseError* error) {
1949 // Parse the line from left to right.
1950 std::string token;
1951 std::string rightpart;
1952 // RFC 4566
1953 // c=<nettype> <addrtype> <connection-address>
1954 // Skip the "c="
1955 if (!rtc::tokenize_first(line, kSdpDelimiterEqual, &token, &rightpart)) {
1956 return ParseFailed(line, "Failed to parse the network type.", error);
1957 }
1958
1959 // Extract and verify the <nettype>
1960 if (!rtc::tokenize_first(rightpart, kSdpDelimiterSpace, &token, &rightpart) ||
1961 token != kConnectionNettype) {
1962 return ParseFailed(line,
1963 "Failed to parse the connection data. The network type "
1964 "is not currently supported.",
1965 error);
1966 }
1967
1968 // Extract the "<addrtype>" and "<connection-address>".
1969 if (!rtc::tokenize_first(rightpart, kSdpDelimiterSpace, &token, &rightpart)) {
1970 return ParseFailed(line, "Failed to parse the address type.", error);
1971 }
1972
1973 // The rightpart part should be the IP address without the slash which is used
1974 // for multicast.
1975 if (rightpart.find('/') != std::string::npos) {
1976 return ParseFailed(line,
1977 "Failed to parse the connection data. Multicast is not "
1978 "currently supported.",
1979 error);
1980 }
1981 addr->SetIP(rightpart);
1982
1983 // Verify that the addrtype matches the type of the parsed address.
1984 if ((addr->family() == AF_INET && token != "IP4") ||
1985 (addr->family() == AF_INET6 && token != "IP6")) {
1986 addr->Clear();
1987 return ParseFailed(
1988 line,
1989 "Failed to parse the connection data. The address type is mismatching.",
1990 error);
1991 }
1992 return true;
1993}
1994
1995bool ParseSessionDescription(const std::string& message,
1996 size_t* pos,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001997 std::string* session_id,
1998 std::string* session_version,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001999 TransportDescription* session_td,
2000 RtpHeaderExtensions* session_extmaps,
zhihuang38989e52017-03-21 11:04:53 -07002001 rtc::SocketAddress* connection_addr,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002002 cricket::SessionDescription* desc,
2003 SdpParseError* error) {
2004 std::string line;
2005
deadbeefc80741f2015-10-22 13:14:45 -07002006 desc->set_msid_supported(false);
2007
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002008 // RFC 4566
2009 // v= (protocol version)
2010 if (!GetLineWithType(message, pos, &line, kLineTypeVersion)) {
Yves Gerey665174f2018-06-19 15:03:05 +02002011 return ParseFailedExpectLine(message, *pos, kLineTypeVersion, std::string(),
2012 error);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002013 }
2014 // RFC 4566
2015 // o=<username> <sess-id> <sess-version> <nettype> <addrtype>
2016 // <unicast-address>
2017 if (!GetLineWithType(message, pos, &line, kLineTypeOrigin)) {
Yves Gerey665174f2018-06-19 15:03:05 +02002018 return ParseFailedExpectLine(message, *pos, kLineTypeOrigin, std::string(),
2019 error);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002020 }
2021 std::vector<std::string> fields;
Yves Gerey665174f2018-06-19 15:03:05 +02002022 rtc::split(line.substr(kLinePrefixLength), kSdpDelimiterSpace, &fields);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002023 const size_t expected_fields = 6;
2024 if (fields.size() != expected_fields) {
2025 return ParseFailedExpectFieldNum(line, expected_fields, error);
2026 }
2027 *session_id = fields[1];
2028 *session_version = fields[2];
2029
2030 // RFC 4566
2031 // s= (session name)
2032 if (!GetLineWithType(message, pos, &line, kLineTypeSessionName)) {
2033 return ParseFailedExpectLine(message, *pos, kLineTypeSessionName,
2034 std::string(), error);
2035 }
2036
Danil Chapovalov66cadcc2018-06-19 16:47:43 +02002037 // absl::optional lines
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002038 // Those are the optional lines, so shouldn't return false if not present.
2039 // RFC 4566
2040 // i=* (session information)
2041 GetLineWithType(message, pos, &line, kLineTypeSessionInfo);
2042
2043 // RFC 4566
2044 // u=* (URI of description)
2045 GetLineWithType(message, pos, &line, kLineTypeSessionUri);
2046
2047 // RFC 4566
2048 // e=* (email address)
2049 GetLineWithType(message, pos, &line, kLineTypeSessionEmail);
2050
2051 // RFC 4566
2052 // p=* (phone number)
2053 GetLineWithType(message, pos, &line, kLineTypeSessionPhone);
2054
2055 // RFC 4566
2056 // c=* (connection information -- not required if included in
2057 // all media)
zhihuang38989e52017-03-21 11:04:53 -07002058 if (GetLineWithType(message, pos, &line, kLineTypeConnection)) {
2059 if (!ParseConnectionData(line, connection_addr, error)) {
2060 return false;
2061 }
2062 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002063
2064 // RFC 4566
2065 // b=* (zero or more bandwidth information lines)
2066 while (GetLineWithType(message, pos, &line, kLineTypeSessionBandwidth)) {
2067 // By pass zero or more b lines.
2068 }
2069
2070 // RFC 4566
2071 // One or more time descriptions ("t=" and "r=" lines; see below)
2072 // t= (time the session is active)
2073 // r=* (zero or more repeat times)
2074 // Ensure there's at least one time description
2075 if (!GetLineWithType(message, pos, &line, kLineTypeTiming)) {
2076 return ParseFailedExpectLine(message, *pos, kLineTypeTiming, std::string(),
2077 error);
2078 }
2079
2080 while (GetLineWithType(message, pos, &line, kLineTypeRepeatTimes)) {
2081 // By pass zero or more r lines.
2082 }
2083
2084 // Go through the rest of the time descriptions
2085 while (GetLineWithType(message, pos, &line, kLineTypeTiming)) {
2086 while (GetLineWithType(message, pos, &line, kLineTypeRepeatTimes)) {
2087 // By pass zero or more r lines.
2088 }
2089 }
2090
2091 // RFC 4566
2092 // z=* (time zone adjustments)
2093 GetLineWithType(message, pos, &line, kLineTypeTimeZone);
2094
2095 // RFC 4566
2096 // k=* (encryption key)
2097 GetLineWithType(message, pos, &line, kLineTypeEncryptionKey);
2098
2099 // RFC 4566
2100 // a=* (zero or more session attribute lines)
2101 while (GetLineWithType(message, pos, &line, kLineTypeAttributes)) {
2102 if (HasAttribute(line, kAttributeGroup)) {
2103 if (!ParseGroupAttribute(line, desc, error)) {
2104 return false;
2105 }
2106 } else if (HasAttribute(line, kAttributeIceUfrag)) {
Yves Gerey665174f2018-06-19 15:03:05 +02002107 if (!GetValue(line, kAttributeIceUfrag, &(session_td->ice_ufrag),
2108 error)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002109 return false;
2110 }
2111 } else if (HasAttribute(line, kAttributeIcePwd)) {
2112 if (!GetValue(line, kAttributeIcePwd, &(session_td->ice_pwd), error)) {
2113 return false;
2114 }
2115 } else if (HasAttribute(line, kAttributeIceLite)) {
2116 session_td->ice_mode = cricket::ICEMODE_LITE;
2117 } else if (HasAttribute(line, kAttributeIceOption)) {
2118 if (!ParseIceOptions(line, &(session_td->transport_options), error)) {
2119 return false;
2120 }
2121 } else if (HasAttribute(line, kAttributeFingerprint)) {
2122 if (session_td->identity_fingerprint.get()) {
2123 return ParseFailed(
2124 line,
2125 "Can't have multiple fingerprint attributes at the same level.",
2126 error);
2127 }
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002128 rtc::SSLFingerprint* fingerprint = NULL;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002129 if (!ParseFingerprintAttribute(line, &fingerprint, error)) {
2130 return false;
2131 }
2132 session_td->identity_fingerprint.reset(fingerprint);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00002133 } else if (HasAttribute(line, kAttributeSetup)) {
2134 if (!ParseDtlsSetup(line, &(session_td->connection_role), error)) {
2135 return false;
2136 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002137 } else if (HasAttribute(line, kAttributeMsidSemantics)) {
2138 std::string semantics;
2139 if (!GetValue(line, kAttributeMsidSemantics, &semantics, error)) {
2140 return false;
2141 }
deadbeefc80741f2015-10-22 13:14:45 -07002142 desc->set_msid_supported(
2143 CaseInsensitiveFind(semantics, kMediaStreamSemantic));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002144 } else if (HasAttribute(line, kAttributeExtmap)) {
isheriff6f8d6862016-05-26 11:24:55 -07002145 RtpExtension extmap;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002146 if (!ParseExtmap(line, &extmap, error)) {
2147 return false;
2148 }
2149 session_extmaps->push_back(extmap);
2150 }
2151 }
2152
2153 return true;
2154}
2155
2156bool ParseGroupAttribute(const std::string& line,
2157 cricket::SessionDescription* desc,
2158 SdpParseError* error) {
nisseede5da42017-01-12 05:15:36 -08002159 RTC_DCHECK(desc != NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002160
2161 // RFC 5888 and draft-holmberg-mmusic-sdp-bundle-negotiation-00
2162 // a=group:BUNDLE video voice
2163 std::vector<std::string> fields;
Yves Gerey665174f2018-06-19 15:03:05 +02002164 rtc::split(line.substr(kLinePrefixLength), kSdpDelimiterSpace, &fields);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002165 std::string semantics;
2166 if (!GetValue(fields[0], kAttributeGroup, &semantics, error)) {
2167 return false;
2168 }
2169 cricket::ContentGroup group(semantics);
2170 for (size_t i = 1; i < fields.size(); ++i) {
2171 group.AddContentName(fields[i]);
2172 }
2173 desc->AddGroup(group);
2174 return true;
2175}
2176
2177static bool ParseFingerprintAttribute(const std::string& line,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002178 rtc::SSLFingerprint** fingerprint,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002179 SdpParseError* error) {
2180 if (!IsLineType(line, kLineTypeAttributes) ||
2181 !HasAttribute(line, kAttributeFingerprint)) {
2182 return ParseFailedExpectLine(line, 0, kLineTypeAttributes,
2183 kAttributeFingerprint, error);
2184 }
2185
2186 std::vector<std::string> fields;
Yves Gerey665174f2018-06-19 15:03:05 +02002187 rtc::split(line.substr(kLinePrefixLength), kSdpDelimiterSpace, &fields);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002188 const size_t expected_fields = 2;
2189 if (fields.size() != expected_fields) {
2190 return ParseFailedExpectFieldNum(line, expected_fields, error);
2191 }
2192
2193 // The first field here is "fingerprint:<hash>.
2194 std::string algorithm;
2195 if (!GetValue(fields[0], kAttributeFingerprint, &algorithm, error)) {
2196 return false;
2197 }
2198
2199 // Downcase the algorithm. Note that we don't need to downcase the
2200 // fingerprint because hex_decode can handle upper-case.
2201 std::transform(algorithm.begin(), algorithm.end(), algorithm.begin(),
2202 ::tolower);
2203
2204 // The second field is the digest value. De-hexify it.
Yves Gerey665174f2018-06-19 15:03:05 +02002205 *fingerprint = rtc::SSLFingerprint::CreateFromRfc4572(algorithm, fields[1]);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002206 if (!*fingerprint) {
Yves Gerey665174f2018-06-19 15:03:05 +02002207 return ParseFailed(line, "Failed to create fingerprint from the digest.",
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002208 error);
2209 }
2210
2211 return true;
2212}
2213
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00002214static bool ParseDtlsSetup(const std::string& line,
2215 cricket::ConnectionRole* role,
2216 SdpParseError* error) {
2217 // setup-attr = "a=setup:" role
2218 // role = "active" / "passive" / "actpass" / "holdconn"
2219 std::vector<std::string> fields;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002220 rtc::split(line.substr(kLinePrefixLength), kSdpDelimiterColon, &fields);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00002221 const size_t expected_fields = 2;
2222 if (fields.size() != expected_fields) {
2223 return ParseFailedExpectFieldNum(line, expected_fields, error);
2224 }
2225 std::string role_str = fields[1];
2226 if (!cricket::StringToConnectionRole(role_str, role)) {
2227 return ParseFailed(line, "Invalid attribute value.", error);
2228 }
2229 return true;
2230}
2231
deadbeef9d3584c2016-02-16 17:54:10 -08002232static bool ParseMsidAttribute(const std::string& line,
Seth Hampson5b4f0752018-04-02 16:31:36 -07002233 std::vector<std::string>* stream_ids,
deadbeef9d3584c2016-02-16 17:54:10 -08002234 std::string* track_id,
2235 SdpParseError* error) {
Seth Hampson5b4f0752018-04-02 16:31:36 -07002236 // https://datatracker.ietf.org/doc/draft-ietf-mmusic-msid/16/
deadbeef9d3584c2016-02-16 17:54:10 -08002237 // a=msid:<stream id> <track id>
2238 // msid-value = msid-id [ SP msid-appdata ]
2239 // msid-id = 1*64token-char ; see RFC 4566
2240 // msid-appdata = 1*64token-char ; see RFC 4566
2241 std::string field1;
Seth Hampson5b4f0752018-04-02 16:31:36 -07002242 std::string new_stream_id;
2243 std::string new_track_id;
deadbeef9d3584c2016-02-16 17:54:10 -08002244 if (!rtc::tokenize_first(line.substr(kLinePrefixLength), kSdpDelimiterSpace,
Seth Hampson5b4f0752018-04-02 16:31:36 -07002245 &field1, &new_track_id)) {
deadbeef9d3584c2016-02-16 17:54:10 -08002246 const size_t expected_fields = 2;
2247 return ParseFailedExpectFieldNum(line, expected_fields, error);
2248 }
2249
Seth Hampson5b4f0752018-04-02 16:31:36 -07002250 if (new_track_id.empty()) {
deadbeefa4549d62017-02-10 17:26:22 -08002251 return ParseFailed(line, "Missing track ID in msid attribute.", error);
2252 }
Seth Hampson5b4f0752018-04-02 16:31:36 -07002253 // All track ids should be the same within an m section in a Unified Plan SDP.
2254 if (!track_id->empty() && new_track_id.compare(*track_id) != 0) {
2255 return ParseFailed(
2256 line, "Two different track IDs in msid attribute in one m= section",
2257 error);
2258 }
2259 *track_id = new_track_id;
deadbeefa4549d62017-02-10 17:26:22 -08002260
deadbeef9d3584c2016-02-16 17:54:10 -08002261 // msid:<msid-id>
Seth Hampson5b4f0752018-04-02 16:31:36 -07002262 if (!GetValue(field1, kAttributeMsid, &new_stream_id, error)) {
deadbeef9d3584c2016-02-16 17:54:10 -08002263 return false;
2264 }
Seth Hampson5b4f0752018-04-02 16:31:36 -07002265 if (new_stream_id.empty()) {
deadbeefa4549d62017-02-10 17:26:22 -08002266 return ParseFailed(line, "Missing stream ID in msid attribute.", error);
2267 }
Seth Hampson5b4f0752018-04-02 16:31:36 -07002268 // The special value "-" indicates "no MediaStream".
2269 if (new_stream_id.compare(kNoStreamMsid) != 0) {
2270 stream_ids->push_back(new_stream_id);
2271 }
deadbeef9d3584c2016-02-16 17:54:10 -08002272 return true;
2273}
2274
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002275// RFC 3551
2276// PT encoding media type clock rate channels
2277// name (Hz)
2278// 0 PCMU A 8,000 1
2279// 1 reserved A
2280// 2 reserved A
2281// 3 GSM A 8,000 1
2282// 4 G723 A 8,000 1
2283// 5 DVI4 A 8,000 1
2284// 6 DVI4 A 16,000 1
2285// 7 LPC A 8,000 1
2286// 8 PCMA A 8,000 1
2287// 9 G722 A 8,000 1
2288// 10 L16 A 44,100 2
2289// 11 L16 A 44,100 1
2290// 12 QCELP A 8,000 1
2291// 13 CN A 8,000 1
2292// 14 MPA A 90,000 (see text)
2293// 15 G728 A 8,000 1
2294// 16 DVI4 A 11,025 1
2295// 17 DVI4 A 22,050 1
2296// 18 G729 A 8,000 1
2297struct StaticPayloadAudioCodec {
2298 const char* name;
2299 int clockrate;
Peter Kasting69558702016-01-12 16:26:35 -08002300 size_t channels;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002301};
2302static const StaticPayloadAudioCodec kStaticPayloadAudioCodecs[] = {
Yves Gerey665174f2018-06-19 15:03:05 +02002303 {"PCMU", 8000, 1}, {"reserved", 0, 0}, {"reserved", 0, 0},
2304 {"GSM", 8000, 1}, {"G723", 8000, 1}, {"DVI4", 8000, 1},
2305 {"DVI4", 16000, 1}, {"LPC", 8000, 1}, {"PCMA", 8000, 1},
2306 {"G722", 8000, 1}, {"L16", 44100, 2}, {"L16", 44100, 1},
2307 {"QCELP", 8000, 1}, {"CN", 8000, 1}, {"MPA", 90000, 1},
2308 {"G728", 8000, 1}, {"DVI4", 11025, 1}, {"DVI4", 22050, 1},
2309 {"G729", 8000, 1},
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002310};
2311
Yves Gerey665174f2018-06-19 15:03:05 +02002312void MaybeCreateStaticPayloadAudioCodecs(const std::vector<int>& fmts,
2313 AudioContentDescription* media_desc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002314 if (!media_desc) {
2315 return;
2316 }
deadbeef67cf2c12016-04-13 10:07:16 -07002317 RTC_DCHECK(media_desc->codecs().empty());
solenberg9fa49752016-10-08 13:02:44 -07002318 for (int payload_type : fmts) {
Yves Gerey665174f2018-06-19 15:03:05 +02002319 if (!media_desc->HasCodec(payload_type) && payload_type >= 0 &&
kjellander3e33bfe2016-06-20 07:04:09 -07002320 static_cast<uint32_t>(payload_type) <
2321 arraysize(kStaticPayloadAudioCodecs)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002322 std::string encoding_name = kStaticPayloadAudioCodecs[payload_type].name;
2323 int clock_rate = kStaticPayloadAudioCodecs[payload_type].clockrate;
Peter Kasting69558702016-01-12 16:26:35 -08002324 size_t channels = kStaticPayloadAudioCodecs[payload_type].channels;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002325 media_desc->AddCodec(cricket::AudioCodec(payload_type, encoding_name,
deadbeef67cf2c12016-04-13 10:07:16 -07002326 clock_rate, 0, channels));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002327 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002328 }
2329}
2330
2331template <class C>
2332static C* ParseContentDescription(const std::string& message,
Patrik Höglundb6b29e02018-06-21 16:58:01 +02002333 const cricket::MediaType media_type,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002334 int mline_index,
2335 const std::string& protocol,
deadbeef67cf2c12016-04-13 10:07:16 -07002336 const std::vector<int>& payload_types,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002337 size_t* pos,
2338 std::string* content_name,
deadbeef25ed4352016-12-12 18:37:36 -08002339 bool* bundle_only,
Steve Antone831b8c2018-02-01 12:22:16 -08002340 int* msid_signaling,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002341 TransportDescription* transport,
2342 std::vector<JsepIceCandidate*>* candidates,
2343 webrtc::SdpParseError* error) {
2344 C* media_desc = new C();
2345 switch (media_type) {
2346 case cricket::MEDIA_TYPE_AUDIO:
2347 *content_name = cricket::CN_AUDIO;
2348 break;
2349 case cricket::MEDIA_TYPE_VIDEO:
2350 *content_name = cricket::CN_VIDEO;
2351 break;
2352 case cricket::MEDIA_TYPE_DATA:
2353 *content_name = cricket::CN_DATA;
2354 break;
2355 default:
nissec80e7412017-01-11 05:56:46 -08002356 RTC_NOTREACHED();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002357 break;
2358 }
deadbeef67cf2c12016-04-13 10:07:16 -07002359 if (!ParseContent(message, media_type, mline_index, protocol, payload_types,
Steve Antone831b8c2018-02-01 12:22:16 -08002360 pos, content_name, bundle_only, msid_signaling, media_desc,
2361 transport, candidates, error)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002362 delete media_desc;
zhihuang38989e52017-03-21 11:04:53 -07002363 return nullptr;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002364 }
2365 // Sort the codecs according to the m-line fmt list.
deadbeef67cf2c12016-04-13 10:07:16 -07002366 std::unordered_map<int, int> payload_type_preferences;
2367 // "size + 1" so that the lowest preference payload type has a preference of
2368 // 1, which is greater than the default (0) for payload types not in the fmt
2369 // list.
2370 int preference = static_cast<int>(payload_types.size() + 1);
2371 for (int pt : payload_types) {
2372 payload_type_preferences[pt] = preference--;
2373 }
2374 std::vector<typename C::CodecType> codecs = media_desc->codecs();
Yves Gerey665174f2018-06-19 15:03:05 +02002375 std::sort(codecs.begin(), codecs.end(),
2376 [&payload_type_preferences](const typename C::CodecType& a,
2377 const typename C::CodecType& b) {
2378 return payload_type_preferences[a.id] >
2379 payload_type_preferences[b.id];
2380 });
deadbeef67cf2c12016-04-13 10:07:16 -07002381 media_desc->set_codecs(codecs);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002382 return media_desc;
2383}
2384
2385bool ParseMediaDescription(const std::string& message,
2386 const TransportDescription& session_td,
2387 const RtpHeaderExtensions& session_extmaps,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002388 size_t* pos,
zhihuang38989e52017-03-21 11:04:53 -07002389 const rtc::SocketAddress& session_connection_addr,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002390 cricket::SessionDescription* desc,
2391 std::vector<JsepIceCandidate*>* candidates,
2392 SdpParseError* error) {
nisseede5da42017-01-12 05:15:36 -08002393 RTC_DCHECK(desc != NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002394 std::string line;
2395 int mline_index = -1;
Steve Antone831b8c2018-02-01 12:22:16 -08002396 int msid_signaling = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002397
2398 // Zero or more media descriptions
2399 // RFC 4566
2400 // m=<media> <port> <proto> <fmt>
2401 while (GetLineWithType(message, pos, &line, kLineTypeMedia)) {
2402 ++mline_index;
2403
2404 std::vector<std::string> fields;
Yves Gerey665174f2018-06-19 15:03:05 +02002405 rtc::split(line.substr(kLinePrefixLength), kSdpDelimiterSpace, &fields);
zstein4b2e0822017-02-17 19:48:38 -08002406
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002407 const size_t expected_min_fields = 4;
2408 if (fields.size() < expected_min_fields) {
2409 return ParseFailedExpectMinFieldNum(line, expected_min_fields, error);
2410 }
deadbeef25ed4352016-12-12 18:37:36 -08002411 bool port_rejected = false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002412 // RFC 3264
2413 // To reject an offered stream, the port number in the corresponding stream
2414 // in the answer MUST be set to zero.
2415 if (fields[1] == kMediaPortRejected) {
deadbeef25ed4352016-12-12 18:37:36 -08002416 port_rejected = true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002417 }
2418
zhihuang38989e52017-03-21 11:04:53 -07002419 int port = 0;
2420 if (!rtc::FromString<int>(fields[1], &port) || !IsValidPort(port)) {
2421 return ParseFailed(line, "The port number is invalid", error);
2422 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002423 std::string protocol = fields[2];
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002424
2425 // <fmt>
deadbeef67cf2c12016-04-13 10:07:16 -07002426 std::vector<int> payload_types;
pthatcher@webrtc.org3341b402015-02-13 21:14:22 +00002427 if (IsRtp(protocol)) {
Yves Gerey665174f2018-06-19 15:03:05 +02002428 for (size_t j = 3; j < fields.size(); ++j) {
jiayl@webrtc.orgddb85ab2014-09-05 16:31:56 +00002429 // TODO(wu): Remove when below bug is fixed.
2430 // https://bugzilla.mozilla.org/show_bug.cgi?id=996329
pbosbb36fdf2015-07-09 07:48:14 -07002431 if (fields[j].empty() && j == fields.size() - 1) {
jiayl@webrtc.orgddb85ab2014-09-05 16:31:56 +00002432 continue;
2433 }
wu@webrtc.org36eda7c2014-04-15 20:37:30 +00002434
jiayl@webrtc.orgddb85ab2014-09-05 16:31:56 +00002435 int pl = 0;
pkasting@chromium.orge9facf82015-02-17 20:36:28 +00002436 if (!GetPayloadTypeFromString(line, fields[j], &pl, error)) {
jiayl@webrtc.orgddb85ab2014-09-05 16:31:56 +00002437 return false;
2438 }
deadbeef67cf2c12016-04-13 10:07:16 -07002439 payload_types.push_back(pl);
wu@webrtc.org5e760e72014-04-02 23:19:09 +00002440 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002441 }
2442
2443 // Make a temporary TransportDescription based on |session_td|.
2444 // Some of this gets overwritten by ParseContent.
deadbeef46eed762016-01-28 13:24:37 -08002445 TransportDescription transport(
2446 session_td.transport_options, session_td.ice_ufrag, session_td.ice_pwd,
2447 session_td.ice_mode, session_td.connection_role,
2448 session_td.identity_fingerprint.get());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002449
kwibergd1fe2812016-04-27 06:47:29 -07002450 std::unique_ptr<MediaContentDescription> content;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002451 std::string content_name;
deadbeef25ed4352016-12-12 18:37:36 -08002452 bool bundle_only = false;
Steve Antone831b8c2018-02-01 12:22:16 -08002453 int section_msid_signaling = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002454 if (HasAttribute(line, kMediaTypeVideo)) {
2455 content.reset(ParseContentDescription<VideoContentDescription>(
deadbeef67cf2c12016-04-13 10:07:16 -07002456 message, cricket::MEDIA_TYPE_VIDEO, mline_index, protocol,
Steve Antone831b8c2018-02-01 12:22:16 -08002457 payload_types, pos, &content_name, &bundle_only,
2458 &section_msid_signaling, &transport, candidates, error));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002459 } else if (HasAttribute(line, kMediaTypeAudio)) {
2460 content.reset(ParseContentDescription<AudioContentDescription>(
deadbeef67cf2c12016-04-13 10:07:16 -07002461 message, cricket::MEDIA_TYPE_AUDIO, mline_index, protocol,
Steve Antone831b8c2018-02-01 12:22:16 -08002462 payload_types, pos, &content_name, &bundle_only,
2463 &section_msid_signaling, &transport, candidates, error));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002464 } else if (HasAttribute(line, kMediaTypeData)) {
jiayl@webrtc.org0e527722014-09-08 21:43:43 +00002465 DataContentDescription* data_desc =
wu@webrtc.org78187522013-10-07 23:32:02 +00002466 ParseContentDescription<DataContentDescription>(
deadbeef67cf2c12016-04-13 10:07:16 -07002467 message, cricket::MEDIA_TYPE_DATA, mline_index, protocol,
Steve Antone831b8c2018-02-01 12:22:16 -08002468 payload_types, pos, &content_name, &bundle_only,
2469 &section_msid_signaling, &transport, candidates, error);
jiayl@webrtc.org0e527722014-09-08 21:43:43 +00002470 content.reset(data_desc);
wu@webrtc.org78187522013-10-07 23:32:02 +00002471
zstein4b2e0822017-02-17 19:48:38 -08002472 if (data_desc && IsDtlsSctp(protocol)) {
2473 int p;
2474 if (rtc::FromString(fields[3], &p)) {
2475 if (!AddSctpDataCodec(data_desc, p)) {
2476 return false;
2477 }
2478 } else if (fields[3] == kDefaultSctpmapProtocol) {
2479 data_desc->set_use_sctpmap(false);
2480 }
wu@webrtc.org78187522013-10-07 23:32:02 +00002481 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002482 } else {
Mirko Bonadei675513b2017-11-09 11:09:25 +01002483 RTC_LOG(LS_WARNING) << "Unsupported media type: " << line;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002484 continue;
2485 }
2486 if (!content.get()) {
2487 // ParseContentDescription returns NULL if failed.
2488 return false;
2489 }
2490
Steve Antone831b8c2018-02-01 12:22:16 -08002491 msid_signaling |= section_msid_signaling;
2492
deadbeef25ed4352016-12-12 18:37:36 -08002493 bool content_rejected = false;
deadbeef12771a12017-01-03 13:53:47 -08002494 // A port of 0 is not interpreted as a rejected m= section when it's
2495 // used along with a=bundle-only.
deadbeef25ed4352016-12-12 18:37:36 -08002496 if (bundle_only) {
deadbeef25ed4352016-12-12 18:37:36 -08002497 if (!port_rejected) {
deadbeef12771a12017-01-03 13:53:47 -08002498 // Usage of bundle-only with a nonzero port is unspecified. So just
2499 // ignore bundle-only if we see this.
2500 bundle_only = false;
Mirko Bonadei675513b2017-11-09 11:09:25 +01002501 RTC_LOG(LS_WARNING)
deadbeef12771a12017-01-03 13:53:47 -08002502 << "a=bundle-only attribute observed with a nonzero "
Jonas Olsson45cc8902018-02-13 10:37:07 +01002503 "port; this usage is unspecified so the attribute is being "
2504 "ignored.";
deadbeef25ed4352016-12-12 18:37:36 -08002505 }
2506 } else {
2507 // If not using bundle-only, interpret port 0 in the normal way; the m=
2508 // section is being rejected.
2509 content_rejected = port_rejected;
2510 }
2511
pthatcher@webrtc.org3341b402015-02-13 21:14:22 +00002512 if (IsRtp(protocol)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002513 // Set the extmap.
2514 if (!session_extmaps.empty() &&
2515 !content->rtp_header_extensions().empty()) {
2516 return ParseFailed("",
2517 "The a=extmap MUST be either all session level or "
2518 "all media level.",
2519 error);
2520 }
2521 for (size_t i = 0; i < session_extmaps.size(); ++i) {
2522 content->AddRtpHeaderExtension(session_extmaps[i]);
2523 }
2524 }
2525 content->set_protocol(protocol);
zhihuang38989e52017-03-21 11:04:53 -07002526
2527 // Use the session level connection address if the media level addresses are
2528 // not specified.
2529 rtc::SocketAddress address;
2530 address = content->connection_address().IsNil()
2531 ? session_connection_addr
2532 : content->connection_address();
2533 address.SetPort(port);
2534 content->set_connection_address(address);
2535
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002536 desc->AddContent(content_name,
Steve Anton5adfafd2017-12-20 16:34:00 -08002537 IsDtlsSctp(protocol) ? MediaProtocolType::kSctp
2538 : MediaProtocolType::kRtp,
deadbeef25ed4352016-12-12 18:37:36 -08002539 content_rejected, bundle_only, content.release());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002540 // Create TransportInfo with the media level "ice-pwd" and "ice-ufrag".
2541 TransportInfo transport_info(content_name, transport);
2542
2543 if (!desc->AddTransportInfo(transport_info)) {
2544 std::ostringstream description;
2545 description << "Failed to AddTransportInfo with content name: "
2546 << content_name;
2547 return ParseFailed("", description.str(), error);
2548 }
2549 }
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00002550
Steve Antone831b8c2018-02-01 12:22:16 -08002551 desc->set_msid_signaling(msid_signaling);
2552
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00002553 size_t end_of_message = message.size();
2554 if (mline_index == -1 && *pos != end_of_message) {
2555 ParseFailed(message, *pos, "Expects m line.", error);
2556 return false;
2557 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002558 return true;
2559}
2560
2561bool VerifyCodec(const cricket::Codec& codec) {
2562 // Codec has not been populated correctly unless the name has been set. This
2563 // can happen if an SDP has an fmtp or rtcp-fb with a payload type but doesn't
2564 // have a corresponding "rtpmap" line.
htab39db842016-12-08 01:50:48 -08002565 return !codec.name.empty();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002566}
2567
2568bool VerifyAudioCodecs(const AudioContentDescription* audio_desc) {
2569 const std::vector<cricket::AudioCodec>& codecs = audio_desc->codecs();
2570 for (std::vector<cricket::AudioCodec>::const_iterator iter = codecs.begin();
2571 iter != codecs.end(); ++iter) {
2572 if (!VerifyCodec(*iter)) {
2573 return false;
2574 }
2575 }
2576 return true;
2577}
2578
2579bool VerifyVideoCodecs(const VideoContentDescription* video_desc) {
2580 const std::vector<cricket::VideoCodec>& codecs = video_desc->codecs();
2581 for (std::vector<cricket::VideoCodec>::const_iterator iter = codecs.begin();
2582 iter != codecs.end(); ++iter) {
2583 if (!VerifyCodec(*iter)) {
2584 return false;
2585 }
2586 }
2587 return true;
2588}
2589
2590void AddParameters(const cricket::CodecParameterMap& parameters,
2591 cricket::Codec* codec) {
Yves Gerey665174f2018-06-19 15:03:05 +02002592 for (cricket::CodecParameterMap::const_iterator iter = parameters.begin();
2593 iter != parameters.end(); ++iter) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002594 codec->SetParam(iter->first, iter->second);
2595 }
2596}
2597
2598void AddFeedbackParameter(const cricket::FeedbackParam& feedback_param,
2599 cricket::Codec* codec) {
2600 codec->AddFeedbackParam(feedback_param);
2601}
2602
2603void AddFeedbackParameters(const cricket::FeedbackParams& feedback_params,
2604 cricket::Codec* codec) {
2605 for (std::vector<cricket::FeedbackParam>::const_iterator iter =
2606 feedback_params.params().begin();
2607 iter != feedback_params.params().end(); ++iter) {
2608 codec->AddFeedbackParam(*iter);
2609 }
2610}
2611
2612// Gets the current codec setting associated with |payload_type|. If there
changbin.shao@webrtc.org2d25b442015-03-16 04:14:34 +00002613// is no Codec associated with that payload type it returns an empty codec
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002614// with that payload type.
2615template <class T>
changbin.shao@webrtc.org2d25b442015-03-16 04:14:34 +00002616T GetCodecWithPayloadType(const std::vector<T>& codecs, int payload_type) {
magjedb05fa242016-11-11 04:00:16 -08002617 const T* codec = FindCodecById(codecs, payload_type);
2618 if (codec)
2619 return *codec;
2620 // Return empty codec with |payload_type|.
changbin.shao@webrtc.org2d25b442015-03-16 04:14:34 +00002621 T ret_val;
magjedb05fa242016-11-11 04:00:16 -08002622 ret_val.id = payload_type;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002623 return ret_val;
2624}
2625
2626// Updates or creates a new codec entry in the audio description.
2627template <class T, class U>
2628void AddOrReplaceCodec(MediaContentDescription* content_desc, const U& codec) {
2629 T* desc = static_cast<T*>(content_desc);
2630 std::vector<U> codecs = desc->codecs();
2631 bool found = false;
2632
2633 typename std::vector<U>::iterator iter;
2634 for (iter = codecs.begin(); iter != codecs.end(); ++iter) {
2635 if (iter->id == codec.id) {
2636 *iter = codec;
2637 found = true;
2638 break;
2639 }
2640 }
2641 if (!found) {
2642 desc->AddCodec(codec);
2643 return;
2644 }
2645 desc->set_codecs(codecs);
2646}
2647
2648// Adds or updates existing codec corresponding to |payload_type| according
2649// to |parameters|.
2650template <class T, class U>
Yves Gerey665174f2018-06-19 15:03:05 +02002651void UpdateCodec(MediaContentDescription* content_desc,
2652 int payload_type,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002653 const cricket::CodecParameterMap& parameters) {
2654 // Codec might already have been populated (from rtpmap).
changbin.shao@webrtc.org2d25b442015-03-16 04:14:34 +00002655 U new_codec = GetCodecWithPayloadType(static_cast<T*>(content_desc)->codecs(),
2656 payload_type);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002657 AddParameters(parameters, &new_codec);
2658 AddOrReplaceCodec<T, U>(content_desc, new_codec);
2659}
2660
2661// Adds or updates existing codec corresponding to |payload_type| according
2662// to |feedback_param|.
2663template <class T, class U>
Yves Gerey665174f2018-06-19 15:03:05 +02002664void UpdateCodec(MediaContentDescription* content_desc,
2665 int payload_type,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002666 const cricket::FeedbackParam& feedback_param) {
2667 // Codec might already have been populated (from rtpmap).
changbin.shao@webrtc.org2d25b442015-03-16 04:14:34 +00002668 U new_codec = GetCodecWithPayloadType(static_cast<T*>(content_desc)->codecs(),
2669 payload_type);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002670 AddFeedbackParameter(feedback_param, &new_codec);
2671 AddOrReplaceCodec<T, U>(content_desc, new_codec);
2672}
2673
jlmiller@webrtc.orga744a282015-02-18 21:37:46 +00002674template <class T>
2675bool PopWildcardCodec(std::vector<T>* codecs, T* wildcard_codec) {
2676 for (auto iter = codecs->begin(); iter != codecs->end(); ++iter) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002677 if (iter->id == kWildcardPayloadType) {
2678 *wildcard_codec = *iter;
2679 codecs->erase(iter);
2680 return true;
2681 }
2682 }
2683 return false;
2684}
2685
Yves Gerey665174f2018-06-19 15:03:05 +02002686template <class T>
jlmiller@webrtc.orga744a282015-02-18 21:37:46 +00002687void UpdateFromWildcardCodecs(cricket::MediaContentDescriptionImpl<T>* desc) {
2688 auto codecs = desc->codecs();
2689 T wildcard_codec;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002690 if (!PopWildcardCodec(&codecs, &wildcard_codec)) {
2691 return;
2692 }
jlmiller@webrtc.orga744a282015-02-18 21:37:46 +00002693 for (auto& codec : codecs) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002694 AddFeedbackParameters(wildcard_codec.feedback_params, &codec);
2695 }
jlmiller@webrtc.orga744a282015-02-18 21:37:46 +00002696 desc->set_codecs(codecs);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002697}
2698
Yves Gerey665174f2018-06-19 15:03:05 +02002699void AddAudioAttribute(const std::string& name,
2700 const std::string& value,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002701 AudioContentDescription* audio_desc) {
2702 if (value.empty()) {
2703 return;
2704 }
2705 std::vector<cricket::AudioCodec> codecs = audio_desc->codecs();
2706 for (std::vector<cricket::AudioCodec>::iterator iter = codecs.begin();
2707 iter != codecs.end(); ++iter) {
2708 iter->params[name] = value;
2709 }
2710 audio_desc->set_codecs(codecs);
2711}
2712
2713bool ParseContent(const std::string& message,
Patrik Höglundb6b29e02018-06-21 16:58:01 +02002714 const cricket::MediaType media_type,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002715 int mline_index,
2716 const std::string& protocol,
deadbeef67cf2c12016-04-13 10:07:16 -07002717 const std::vector<int>& payload_types,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002718 size_t* pos,
2719 std::string* content_name,
deadbeef25ed4352016-12-12 18:37:36 -08002720 bool* bundle_only,
Steve Antone831b8c2018-02-01 12:22:16 -08002721 int* msid_signaling,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002722 MediaContentDescription* media_desc,
2723 TransportDescription* transport,
2724 std::vector<JsepIceCandidate*>* candidates,
2725 SdpParseError* error) {
nisseede5da42017-01-12 05:15:36 -08002726 RTC_DCHECK(media_desc != NULL);
2727 RTC_DCHECK(content_name != NULL);
2728 RTC_DCHECK(transport != NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002729
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +00002730 if (media_type == cricket::MEDIA_TYPE_AUDIO) {
Steve Antonb1c1de12017-12-21 15:14:30 -08002731 MaybeCreateStaticPayloadAudioCodecs(payload_types, media_desc->as_audio());
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +00002732 }
2733
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002734 // The media level "ice-ufrag" and "ice-pwd".
2735 // The candidates before update the media level "ice-pwd" and "ice-ufrag".
2736 Candidates candidates_orig;
2737 std::string line;
2738 std::string mline_id;
2739 // Tracks created out of the ssrc attributes.
2740 StreamParamsVec tracks;
2741 SsrcInfoVec ssrc_infos;
2742 SsrcGroupVec ssrc_groups;
2743 std::string maxptime_as_string;
2744 std::string ptime_as_string;
Seth Hampson5b4f0752018-04-02 16:31:36 -07002745 std::vector<std::string> stream_ids;
deadbeef9d3584c2016-02-16 17:54:10 -08002746 std::string track_id;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002747
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002748 // Loop until the next m line
2749 while (!IsLineType(message, kLineTypeMedia, *pos)) {
2750 if (!GetLine(message, pos, &line)) {
2751 if (*pos >= message.size()) {
2752 break; // Done parsing
2753 } else {
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +00002754 return ParseFailed(message, *pos, "Invalid SDP line.", error);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002755 }
2756 }
2757
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002758 // RFC 4566
2759 // b=* (zero or more bandwidth information lines)
2760 if (IsLineType(line, kLineTypeSessionBandwidth)) {
2761 std::string bandwidth;
2762 if (HasAttribute(line, kApplicationSpecificMaximum)) {
2763 if (!GetValue(line, kApplicationSpecificMaximum, &bandwidth, error)) {
2764 return false;
2765 } else {
wu@webrtc.org5e760e72014-04-02 23:19:09 +00002766 int b = 0;
2767 if (!GetValueFromString(line, bandwidth, &b, error)) {
2768 return false;
2769 }
deadbeef3e8016e2017-08-03 17:49:30 -07002770 // TODO(deadbeef): Historically, applications may be setting a value
2771 // of -1 to mean "unset any previously set bandwidth limit", even
2772 // though ommitting the "b=AS" entirely will do just that. Once we've
2773 // transitioned applications to doing the right thing, it would be
2774 // better to treat this as a hard error instead of just ignoring it.
2775 if (b == -1) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01002776 RTC_LOG(LS_WARNING)
2777 << "Ignoring \"b=AS:-1\"; will be treated as \"no "
2778 "bandwidth limit\".";
deadbeef3e8016e2017-08-03 17:49:30 -07002779 continue;
2780 }
deadbeefbc88c6b2017-08-02 11:26:34 -07002781 if (b < 0) {
2782 return ParseFailed(line, "b=AS value can't be negative.", error);
2783 }
Peter Thatcherc0c3a862015-06-24 15:31:25 -07002784 // We should never use more than the default bandwidth for RTP-based
2785 // data channels. Don't allow SDP to set the bandwidth, because
2786 // that would give JS the opportunity to "break the Internet".
2787 // See: https://code.google.com/p/chromium/issues/detail?id=280726
2788 if (media_type == cricket::MEDIA_TYPE_DATA && IsRtp(protocol) &&
2789 b > cricket::kDataMaxBandwidth / 1000) {
2790 std::ostringstream description;
2791 description << "RTP-based data channels may not send more than "
2792 << cricket::kDataMaxBandwidth / 1000 << "kbps.";
2793 return ParseFailed(line, description.str(), error);
2794 }
deadbeefb2362572016-12-13 16:37:06 -08002795 // Prevent integer overflow.
2796 b = std::min(b, INT_MAX / 1000);
wu@webrtc.org5e760e72014-04-02 23:19:09 +00002797 media_desc->set_bandwidth(b * 1000);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002798 }
2799 }
2800 continue;
2801 }
2802
zhihuang38989e52017-03-21 11:04:53 -07002803 // Parse the media level connection data.
2804 if (IsLineType(line, kLineTypeConnection)) {
2805 rtc::SocketAddress addr;
2806 if (!ParseConnectionData(line, &addr, error)) {
2807 return false;
2808 }
2809 media_desc->set_connection_address(addr);
2810 continue;
2811 }
2812
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002813 if (!IsLineType(line, kLineTypeAttributes)) {
Steve Anton36b29d12017-10-30 09:57:42 -07002814 // TODO(deadbeef): Handle other lines if needed.
Mirko Bonadei675513b2017-11-09 11:09:25 +01002815 RTC_LOG(LS_INFO) << "Ignored line: " << line;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002816 continue;
2817 }
2818
2819 // Handle attributes common to SCTP and RTP.
2820 if (HasAttribute(line, kAttributeMid)) {
2821 // RFC 3388
2822 // mid-attribute = "a=mid:" identification-tag
2823 // identification-tag = token
2824 // Use the mid identification-tag as the content name.
2825 if (!GetValue(line, kAttributeMid, &mline_id, error)) {
2826 return false;
2827 }
2828 *content_name = mline_id;
deadbeef25ed4352016-12-12 18:37:36 -08002829 } else if (HasAttribute(line, kAttributeBundleOnly)) {
2830 *bundle_only = true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002831 } else if (HasAttribute(line, kAttributeCandidate)) {
2832 Candidate candidate;
2833 if (!ParseCandidate(line, &candidate, error, false)) {
2834 return false;
2835 }
deadbeef7bcdb692017-01-20 12:43:58 -08002836 // ParseCandidate will parse non-standard ufrag and password attributes,
2837 // since it's used for candidate trickling, but we only want to process
2838 // the "a=ice-ufrag"/"a=ice-pwd" values in a session description, so
2839 // strip them off at this point.
2840 candidate.set_username(std::string());
2841 candidate.set_password(std::string());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002842 candidates_orig.push_back(candidate);
2843 } else if (HasAttribute(line, kAttributeIceUfrag)) {
2844 if (!GetValue(line, kAttributeIceUfrag, &transport->ice_ufrag, error)) {
2845 return false;
2846 }
2847 } else if (HasAttribute(line, kAttributeIcePwd)) {
2848 if (!GetValue(line, kAttributeIcePwd, &transport->ice_pwd, error)) {
2849 return false;
2850 }
2851 } else if (HasAttribute(line, kAttributeIceOption)) {
2852 if (!ParseIceOptions(line, &transport->transport_options, error)) {
2853 return false;
2854 }
2855 } else if (HasAttribute(line, kAttributeFmtp)) {
2856 if (!ParseFmtpAttributes(line, media_type, media_desc, error)) {
2857 return false;
2858 }
2859 } else if (HasAttribute(line, kAttributeFingerprint)) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002860 rtc::SSLFingerprint* fingerprint = NULL;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002861
2862 if (!ParseFingerprintAttribute(line, &fingerprint, error)) {
2863 return false;
2864 }
2865 transport->identity_fingerprint.reset(fingerprint);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00002866 } else if (HasAttribute(line, kAttributeSetup)) {
2867 if (!ParseDtlsSetup(line, &(transport->connection_role), error)) {
2868 return false;
2869 }
pthatcher@webrtc.org3341b402015-02-13 21:14:22 +00002870 } else if (IsDtlsSctp(protocol) && HasAttribute(line, kAttributeSctpPort)) {
deadbeef7e146cb2016-09-28 10:04:34 -07002871 if (media_type != cricket::MEDIA_TYPE_DATA) {
2872 return ParseFailed(
2873 line, "sctp-port attribute found in non-data media description.",
2874 error);
2875 }
jiayl@webrtc.orgddb85ab2014-09-05 16:31:56 +00002876 int sctp_port;
2877 if (!ParseSctpPort(line, &sctp_port, error)) {
2878 return false;
2879 }
Steve Antonb1c1de12017-12-21 15:14:30 -08002880 if (!AddSctpDataCodec(media_desc->as_data(), sctp_port)) {
jiayl@webrtc.orgddb85ab2014-09-05 16:31:56 +00002881 return false;
2882 }
pthatcher@webrtc.org3341b402015-02-13 21:14:22 +00002883 } else if (IsRtp(protocol)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002884 //
2885 // RTP specific attrubtes
2886 //
2887 if (HasAttribute(line, kAttributeRtcpMux)) {
2888 media_desc->set_rtcp_mux(true);
deadbeef13871492015-12-09 12:37:51 -08002889 } else if (HasAttribute(line, kAttributeRtcpReducedSize)) {
2890 media_desc->set_rtcp_reduced_size(true);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002891 } else if (HasAttribute(line, kAttributeSsrcGroup)) {
2892 if (!ParseSsrcGroupAttribute(line, &ssrc_groups, error)) {
2893 return false;
2894 }
2895 } else if (HasAttribute(line, kAttributeSsrc)) {
Steve Antone831b8c2018-02-01 12:22:16 -08002896 if (!ParseSsrcAttribute(line, &ssrc_infos, msid_signaling, error)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002897 return false;
2898 }
2899 } else if (HasAttribute(line, kAttributeCrypto)) {
2900 if (!ParseCryptoAttribute(line, media_desc, error)) {
2901 return false;
2902 }
2903 } else if (HasAttribute(line, kAttributeRtpmap)) {
deadbeef67cf2c12016-04-13 10:07:16 -07002904 if (!ParseRtpmapAttribute(line, media_type, payload_types, media_desc,
2905 error)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002906 return false;
2907 }
2908 } else if (HasAttribute(line, kCodecParamMaxPTime)) {
2909 if (!GetValue(line, kCodecParamMaxPTime, &maxptime_as_string, error)) {
2910 return false;
2911 }
2912 } else if (HasAttribute(line, kAttributeRtcpFb)) {
2913 if (!ParseRtcpFbAttribute(line, media_type, media_desc, error)) {
2914 return false;
2915 }
2916 } else if (HasAttribute(line, kCodecParamPTime)) {
2917 if (!GetValue(line, kCodecParamPTime, &ptime_as_string, error)) {
2918 return false;
2919 }
2920 } else if (HasAttribute(line, kAttributeSendOnly)) {
Steve Anton4e70a722017-11-28 14:57:10 -08002921 media_desc->set_direction(RtpTransceiverDirection::kSendOnly);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002922 } else if (HasAttribute(line, kAttributeRecvOnly)) {
Steve Anton4e70a722017-11-28 14:57:10 -08002923 media_desc->set_direction(RtpTransceiverDirection::kRecvOnly);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002924 } else if (HasAttribute(line, kAttributeInactive)) {
Steve Anton4e70a722017-11-28 14:57:10 -08002925 media_desc->set_direction(RtpTransceiverDirection::kInactive);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002926 } else if (HasAttribute(line, kAttributeSendRecv)) {
Steve Anton4e70a722017-11-28 14:57:10 -08002927 media_desc->set_direction(RtpTransceiverDirection::kSendRecv);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002928 } else if (HasAttribute(line, kAttributeExtmap)) {
isheriff6f8d6862016-05-26 11:24:55 -07002929 RtpExtension extmap;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002930 if (!ParseExtmap(line, &extmap, error)) {
2931 return false;
2932 }
2933 media_desc->AddRtpHeaderExtension(extmap);
2934 } else if (HasAttribute(line, kAttributeXGoogleFlag)) {
2935 // Experimental attribute. Conference mode activates more aggressive
2936 // AEC and NS settings.
Steve Anton36b29d12017-10-30 09:57:42 -07002937 // TODO(deadbeef): expose API to set these directly.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002938 std::string flag_value;
2939 if (!GetValue(line, kAttributeXGoogleFlag, &flag_value, error)) {
2940 return false;
2941 }
2942 if (flag_value.compare(kValueConference) == 0)
2943 media_desc->set_conference_mode(true);
deadbeef9d3584c2016-02-16 17:54:10 -08002944 } else if (HasAttribute(line, kAttributeMsid)) {
Seth Hampson5b4f0752018-04-02 16:31:36 -07002945 if (!ParseMsidAttribute(line, &stream_ids, &track_id, error)) {
deadbeef9d3584c2016-02-16 17:54:10 -08002946 return false;
2947 }
Steve Antone831b8c2018-02-01 12:22:16 -08002948 *msid_signaling |= cricket::kMsidSignalingMediaSection;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002949 }
2950 } else {
2951 // Only parse lines that we are interested of.
Mirko Bonadei675513b2017-11-09 11:09:25 +01002952 RTC_LOG(LS_INFO) << "Ignored line: " << line;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002953 continue;
2954 }
2955 }
2956
2957 // Create tracks from the |ssrc_infos|.
Taylor Brandstetter5de6b752016-03-09 17:02:30 -08002958 // If the stream_id/track_id for all SSRCS are identical, one StreamParams
2959 // will be created in CreateTracksFromSsrcInfos, containing all the SSRCs from
2960 // the m= section.
Seth Hampson5897a6e2018-04-03 11:16:33 -07002961 if (!ssrc_infos.empty()) {
2962 CreateTracksFromSsrcInfos(ssrc_infos, stream_ids, track_id, &tracks,
2963 *msid_signaling);
2964 } else if (media_type != cricket::MEDIA_TYPE_DATA &&
2965 (*msid_signaling & cricket::kMsidSignalingMediaSection)) {
2966 // If the stream_ids/track_id was signaled but SSRCs were unsignaled we
2967 // still create a track. This isn't done for data media types because
2968 // StreamParams aren't used for SCTP streams, and RTP data channels don't
2969 // support unsignaled SSRCs.
2970 CreateTrackWithNoSsrcs(stream_ids, track_id, &tracks);
2971 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002972
2973 // Add the ssrc group to the track.
2974 for (SsrcGroupVec::iterator ssrc_group = ssrc_groups.begin();
2975 ssrc_group != ssrc_groups.end(); ++ssrc_group) {
2976 if (ssrc_group->ssrcs.empty()) {
2977 continue;
2978 }
Peter Boström0c4e06b2015-10-07 12:23:21 +02002979 uint32_t ssrc = ssrc_group->ssrcs.front();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002980 for (StreamParamsVec::iterator track = tracks.begin();
2981 track != tracks.end(); ++track) {
2982 if (track->has_ssrc(ssrc)) {
2983 track->ssrc_groups.push_back(*ssrc_group);
2984 }
2985 }
2986 }
2987
2988 // Add the new tracks to the |media_desc|.
deadbeef9d3584c2016-02-16 17:54:10 -08002989 for (StreamParams& track : tracks) {
2990 media_desc->AddStream(track);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002991 }
2992
2993 if (media_type == cricket::MEDIA_TYPE_AUDIO) {
Steve Antonb1c1de12017-12-21 15:14:30 -08002994 AudioContentDescription* audio_desc = media_desc->as_audio();
jlmiller@webrtc.orga744a282015-02-18 21:37:46 +00002995 UpdateFromWildcardCodecs(audio_desc);
2996
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002997 // Verify audio codec ensures that no audio codec has been populated with
2998 // only fmtp.
2999 if (!VerifyAudioCodecs(audio_desc)) {
3000 return ParseFailed("Failed to parse audio codecs correctly.", error);
3001 }
3002 AddAudioAttribute(kCodecParamMaxPTime, maxptime_as_string, audio_desc);
3003 AddAudioAttribute(kCodecParamPTime, ptime_as_string, audio_desc);
3004 }
3005
3006 if (media_type == cricket::MEDIA_TYPE_VIDEO) {
Steve Antonb1c1de12017-12-21 15:14:30 -08003007 VideoContentDescription* video_desc = media_desc->as_video();
jlmiller@webrtc.orga744a282015-02-18 21:37:46 +00003008 UpdateFromWildcardCodecs(video_desc);
3009 // Verify video codec ensures that no video codec has been populated with
3010 // only rtcp-fb.
3011 if (!VerifyVideoCodecs(video_desc)) {
3012 return ParseFailed("Failed to parse video codecs correctly.", error);
3013 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003014 }
3015
3016 // RFC 5245
3017 // Update the candidates with the media level "ice-pwd" and "ice-ufrag".
3018 for (Candidates::iterator it = candidates_orig.begin();
3019 it != candidates_orig.end(); ++it) {
nisseede5da42017-01-12 05:15:36 -08003020 RTC_DCHECK((*it).username().empty() ||
3021 (*it).username() == transport->ice_ufrag);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003022 (*it).set_username(transport->ice_ufrag);
nisseede5da42017-01-12 05:15:36 -08003023 RTC_DCHECK((*it).password().empty());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003024 (*it).set_password(transport->ice_pwd);
Yves Gerey665174f2018-06-19 15:03:05 +02003025 candidates->push_back(new JsepIceCandidate(mline_id, mline_index, *it));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003026 }
3027 return true;
3028}
3029
Steve Antone831b8c2018-02-01 12:22:16 -08003030bool ParseSsrcAttribute(const std::string& line,
3031 SsrcInfoVec* ssrc_infos,
3032 int* msid_signaling,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003033 SdpParseError* error) {
nisseede5da42017-01-12 05:15:36 -08003034 RTC_DCHECK(ssrc_infos != NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003035 // RFC 5576
3036 // a=ssrc:<ssrc-id> <attribute>
3037 // a=ssrc:<ssrc-id> <attribute>:<value>
3038 std::string field1, field2;
Donald Curtis144d0182015-05-15 13:14:24 -07003039 if (!rtc::tokenize_first(line.substr(kLinePrefixLength), kSdpDelimiterSpace,
3040 &field1, &field2)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003041 const size_t expected_fields = 2;
3042 return ParseFailedExpectFieldNum(line, expected_fields, error);
3043 }
3044
3045 // ssrc:<ssrc-id>
3046 std::string ssrc_id_s;
3047 if (!GetValue(field1, kAttributeSsrc, &ssrc_id_s, error)) {
3048 return false;
3049 }
Peter Boström0c4e06b2015-10-07 12:23:21 +02003050 uint32_t ssrc_id = 0;
wu@webrtc.org5e760e72014-04-02 23:19:09 +00003051 if (!GetValueFromString(line, ssrc_id_s, &ssrc_id, error)) {
3052 return false;
3053 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003054
3055 std::string attribute;
3056 std::string value;
Donald Curtis144d0182015-05-15 13:14:24 -07003057 if (!rtc::tokenize_first(field2, kSdpDelimiterColon, &attribute, &value)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003058 std::ostringstream description;
3059 description << "Failed to get the ssrc attribute value from " << field2
3060 << ". Expected format <attribute>:<value>.";
3061 return ParseFailed(line, description.str(), error);
3062 }
3063
3064 // Check if there's already an item for this |ssrc_id|. Create a new one if
3065 // there isn't.
3066 SsrcInfoVec::iterator ssrc_info = ssrc_infos->begin();
3067 for (; ssrc_info != ssrc_infos->end(); ++ssrc_info) {
3068 if (ssrc_info->ssrc_id == ssrc_id) {
3069 break;
3070 }
3071 }
3072 if (ssrc_info == ssrc_infos->end()) {
3073 SsrcInfo info;
3074 info.ssrc_id = ssrc_id;
3075 ssrc_infos->push_back(info);
3076 ssrc_info = ssrc_infos->end() - 1;
3077 }
3078
3079 // Store the info to the |ssrc_info|.
3080 if (attribute == kSsrcAttributeCname) {
3081 // RFC 5576
3082 // cname:<value>
3083 ssrc_info->cname = value;
3084 } else if (attribute == kSsrcAttributeMsid) {
3085 // draft-alvestrand-mmusic-msid-00
Seth Hampson5b4f0752018-04-02 16:31:36 -07003086 // msid:identifier [appdata]
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003087 std::vector<std::string> fields;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00003088 rtc::split(value, kSdpDelimiterSpace, &fields);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003089 if (fields.size() < 1 || fields.size() > 2) {
Yves Gerey665174f2018-06-19 15:03:05 +02003090 return ParseFailed(
3091 line, "Expected format \"msid:<identifier>[ <appdata>]\".", error);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003092 }
deadbeef9d3584c2016-02-16 17:54:10 -08003093 ssrc_info->stream_id = fields[0];
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003094 if (fields.size() == 2) {
deadbeef9d3584c2016-02-16 17:54:10 -08003095 ssrc_info->track_id = fields[1];
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003096 }
Steve Antone831b8c2018-02-01 12:22:16 -08003097 *msid_signaling |= cricket::kMsidSignalingSsrcAttribute;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003098 } else if (attribute == kSsrcAttributeMslabel) {
3099 // draft-alvestrand-rtcweb-mid-01
3100 // mslabel:<value>
3101 ssrc_info->mslabel = value;
3102 } else if (attribute == kSSrcAttributeLabel) {
3103 // The label isn't defined.
3104 // label:<value>
3105 ssrc_info->label = value;
3106 }
3107 return true;
3108}
3109
3110bool ParseSsrcGroupAttribute(const std::string& line,
3111 SsrcGroupVec* ssrc_groups,
3112 SdpParseError* error) {
nisseede5da42017-01-12 05:15:36 -08003113 RTC_DCHECK(ssrc_groups != NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003114 // RFC 5576
3115 // a=ssrc-group:<semantics> <ssrc-id> ...
3116 std::vector<std::string> fields;
Yves Gerey665174f2018-06-19 15:03:05 +02003117 rtc::split(line.substr(kLinePrefixLength), kSdpDelimiterSpace, &fields);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003118 const size_t expected_min_fields = 2;
3119 if (fields.size() < expected_min_fields) {
3120 return ParseFailedExpectMinFieldNum(line, expected_min_fields, error);
3121 }
3122 std::string semantics;
3123 if (!GetValue(fields[0], kAttributeSsrcGroup, &semantics, error)) {
3124 return false;
3125 }
Peter Boström0c4e06b2015-10-07 12:23:21 +02003126 std::vector<uint32_t> ssrcs;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003127 for (size_t i = 1; i < fields.size(); ++i) {
Peter Boström0c4e06b2015-10-07 12:23:21 +02003128 uint32_t ssrc = 0;
wu@webrtc.org5e760e72014-04-02 23:19:09 +00003129 if (!GetValueFromString(line, fields[i], &ssrc, error)) {
3130 return false;
3131 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003132 ssrcs.push_back(ssrc);
3133 }
3134 ssrc_groups->push_back(SsrcGroup(semantics, ssrcs));
3135 return true;
3136}
3137
3138bool ParseCryptoAttribute(const std::string& line,
3139 MediaContentDescription* media_desc,
3140 SdpParseError* error) {
3141 std::vector<std::string> fields;
Yves Gerey665174f2018-06-19 15:03:05 +02003142 rtc::split(line.substr(kLinePrefixLength), kSdpDelimiterSpace, &fields);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003143 // RFC 4568
3144 // a=crypto:<tag> <crypto-suite> <key-params> [<session-params>]
3145 const size_t expected_min_fields = 3;
3146 if (fields.size() < expected_min_fields) {
3147 return ParseFailedExpectMinFieldNum(line, expected_min_fields, error);
3148 }
3149 std::string tag_value;
3150 if (!GetValue(fields[0], kAttributeCrypto, &tag_value, error)) {
3151 return false;
3152 }
wu@webrtc.org5e760e72014-04-02 23:19:09 +00003153 int tag = 0;
3154 if (!GetValueFromString(line, tag_value, &tag, error)) {
3155 return false;
3156 }
jbauch083b73f2015-07-16 02:46:32 -07003157 const std::string& crypto_suite = fields[1];
3158 const std::string& key_params = fields[2];
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003159 std::string session_params;
3160 if (fields.size() > 3) {
3161 session_params = fields[3];
3162 }
Yves Gerey665174f2018-06-19 15:03:05 +02003163 media_desc->AddCrypto(
3164 CryptoParams(tag, crypto_suite, key_params, session_params));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003165 return true;
3166}
3167
3168// Updates or creates a new codec entry in the audio description with according
deadbeef67cf2c12016-04-13 10:07:16 -07003169// to |name|, |clockrate|, |bitrate|, and |channels|.
3170void UpdateCodec(int payload_type,
3171 const std::string& name,
3172 int clockrate,
3173 int bitrate,
3174 size_t channels,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003175 AudioContentDescription* audio_desc) {
3176 // Codec may already be populated with (only) optional parameters
3177 // (from an fmtp).
changbin.shao@webrtc.org2d25b442015-03-16 04:14:34 +00003178 cricket::AudioCodec codec =
3179 GetCodecWithPayloadType(audio_desc->codecs(), payload_type);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003180 codec.name = name;
3181 codec.clockrate = clockrate;
3182 codec.bitrate = bitrate;
3183 codec.channels = channels;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003184 AddOrReplaceCodec<AudioContentDescription, cricket::AudioCodec>(audio_desc,
3185 codec);
3186}
3187
3188// Updates or creates a new codec entry in the video description according to
deadbeef67cf2c12016-04-13 10:07:16 -07003189// |name|, |width|, |height|, and |framerate|.
3190void UpdateCodec(int payload_type,
3191 const std::string& name,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003192 VideoContentDescription* video_desc) {
3193 // Codec may already be populated with (only) optional parameters
3194 // (from an fmtp).
changbin.shao@webrtc.org2d25b442015-03-16 04:14:34 +00003195 cricket::VideoCodec codec =
3196 GetCodecWithPayloadType(video_desc->codecs(), payload_type);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003197 codec.name = name;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003198 AddOrReplaceCodec<VideoContentDescription, cricket::VideoCodec>(video_desc,
3199 codec);
3200}
3201
3202bool ParseRtpmapAttribute(const std::string& line,
Patrik Höglundb6b29e02018-06-21 16:58:01 +02003203 const cricket::MediaType media_type,
deadbeef67cf2c12016-04-13 10:07:16 -07003204 const std::vector<int>& payload_types,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003205 MediaContentDescription* media_desc,
3206 SdpParseError* error) {
3207 std::vector<std::string> fields;
Yves Gerey665174f2018-06-19 15:03:05 +02003208 rtc::split(line.substr(kLinePrefixLength), kSdpDelimiterSpace, &fields);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003209 // RFC 4566
3210 // a=rtpmap:<payload type> <encoding name>/<clock rate>[/<encodingparameters>]
3211 const size_t expected_min_fields = 2;
3212 if (fields.size() < expected_min_fields) {
3213 return ParseFailedExpectMinFieldNum(line, expected_min_fields, error);
3214 }
3215 std::string payload_type_value;
3216 if (!GetValue(fields[0], kAttributeRtpmap, &payload_type_value, error)) {
3217 return false;
3218 }
wu@webrtc.org5e760e72014-04-02 23:19:09 +00003219 int payload_type = 0;
pkasting@chromium.orge9facf82015-02-17 20:36:28 +00003220 if (!GetPayloadTypeFromString(line, payload_type_value, &payload_type,
3221 error)) {
wu@webrtc.org5e760e72014-04-02 23:19:09 +00003222 return false;
3223 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003224
deadbeef67cf2c12016-04-13 10:07:16 -07003225 if (std::find(payload_types.begin(), payload_types.end(), payload_type) ==
3226 payload_types.end()) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01003227 RTC_LOG(LS_WARNING) << "Ignore rtpmap line that did not appear in the "
Jonas Olsson45cc8902018-02-13 10:37:07 +01003228 "<fmt> of the m-line: "
3229 << line;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003230 return true;
3231 }
jbauch083b73f2015-07-16 02:46:32 -07003232 const std::string& encoder = fields[1];
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003233 std::vector<std::string> codec_params;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00003234 rtc::split(encoder, '/', &codec_params);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003235 // <encoding name>/<clock rate>[/<encodingparameters>]
3236 // 2 mandatory fields
3237 if (codec_params.size() < 2 || codec_params.size() > 3) {
3238 return ParseFailed(line,
3239 "Expected format \"<encoding name>/<clock rate>"
3240 "[/<encodingparameters>]\".",
3241 error);
3242 }
jbauch083b73f2015-07-16 02:46:32 -07003243 const std::string& encoding_name = codec_params[0];
wu@webrtc.org5e760e72014-04-02 23:19:09 +00003244 int clock_rate = 0;
3245 if (!GetValueFromString(line, codec_params[1], &clock_rate, error)) {
3246 return false;
3247 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003248 if (media_type == cricket::MEDIA_TYPE_VIDEO) {
Steve Antonb1c1de12017-12-21 15:14:30 -08003249 VideoContentDescription* video_desc = media_desc->as_video();
Yves Gerey665174f2018-06-19 15:03:05 +02003250 UpdateCodec(payload_type, encoding_name, video_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003251 } else if (media_type == cricket::MEDIA_TYPE_AUDIO) {
3252 // RFC 4566
3253 // For audio streams, <encoding parameters> indicates the number
3254 // of audio channels. This parameter is OPTIONAL and may be
3255 // omitted if the number of channels is one, provided that no
3256 // additional parameters are needed.
Peter Kasting69558702016-01-12 16:26:35 -08003257 size_t channels = 1;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003258 if (codec_params.size() == 3) {
wu@webrtc.org5e760e72014-04-02 23:19:09 +00003259 if (!GetValueFromString(line, codec_params[2], &channels, error)) {
3260 return false;
3261 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003262 }
Steve Antonb1c1de12017-12-21 15:14:30 -08003263 AudioContentDescription* audio_desc = media_desc->as_audio();
ossue1405ad2017-01-23 08:55:48 -08003264 UpdateCodec(payload_type, encoding_name, clock_rate, 0, channels,
deadbeef67cf2c12016-04-13 10:07:16 -07003265 audio_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003266 } else if (media_type == cricket::MEDIA_TYPE_DATA) {
Steve Antonb1c1de12017-12-21 15:14:30 -08003267 DataContentDescription* data_desc = media_desc->as_data();
deadbeef67cf2c12016-04-13 10:07:16 -07003268 data_desc->AddCodec(cricket::DataCodec(payload_type, encoding_name));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003269 }
3270 return true;
3271}
3272
Yves Gerey665174f2018-06-19 15:03:05 +02003273bool ParseFmtpParam(const std::string& line,
3274 std::string* parameter,
3275 std::string* value,
3276 SdpParseError* error) {
Donald Curtis0e07f922015-05-15 09:21:23 -07003277 if (!rtc::tokenize_first(line, kSdpDelimiterEqual, parameter, value)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003278 ParseFailed(line, "Unable to parse fmtp parameter. \'=\' missing.", error);
3279 return false;
3280 }
3281 // a=fmtp:<payload_type> <param1>=<value1>; <param2>=<value2>; ...
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003282 return true;
3283}
3284
Yves Gerey665174f2018-06-19 15:03:05 +02003285bool ParseFmtpAttributes(const std::string& line,
Patrik Höglundb6b29e02018-06-21 16:58:01 +02003286 const cricket::MediaType media_type,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003287 MediaContentDescription* media_desc,
3288 SdpParseError* error) {
3289 if (media_type != cricket::MEDIA_TYPE_AUDIO &&
3290 media_type != cricket::MEDIA_TYPE_VIDEO) {
3291 return true;
3292 }
Donald Curtis0e07f922015-05-15 09:21:23 -07003293
3294 std::string line_payload;
3295 std::string line_params;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003296
3297 // RFC 5576
3298 // a=fmtp:<format> <format specific parameters>
3299 // At least two fields, whereas the second one is any of the optional
3300 // parameters.
Donald Curtis144d0182015-05-15 13:14:24 -07003301 if (!rtc::tokenize_first(line.substr(kLinePrefixLength), kSdpDelimiterSpace,
3302 &line_payload, &line_params)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003303 ParseFailedExpectMinFieldNum(line, 2, error);
3304 return false;
3305 }
3306
Donald Curtis0e07f922015-05-15 09:21:23 -07003307 // Parse out the payload information.
pkasting@chromium.orgd3245462015-02-23 21:28:22 +00003308 std::string payload_type_str;
Donald Curtis0e07f922015-05-15 09:21:23 -07003309 if (!GetValue(line_payload, kAttributeFmtp, &payload_type_str, error)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003310 return false;
3311 }
3312
Donald Curtis0e07f922015-05-15 09:21:23 -07003313 int payload_type = 0;
Donald Curtis144d0182015-05-15 13:14:24 -07003314 if (!GetPayloadTypeFromString(line_payload, payload_type_str, &payload_type,
3315 error)) {
Donald Curtis0e07f922015-05-15 09:21:23 -07003316 return false;
3317 }
3318
3319 // Parse out format specific parameters.
3320 std::vector<std::string> fields;
3321 rtc::split(line_params, kSdpDelimiterSemicolon, &fields);
3322
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003323 cricket::CodecParameterMap codec_params;
Donald Curtis144d0182015-05-15 13:14:24 -07003324 for (auto& iter : fields) {
Donald Curtis0e07f922015-05-15 09:21:23 -07003325 if (iter.find(kSdpDelimiterEqual) == std::string::npos) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003326 // Only fmtps with equals are currently supported. Other fmtp types
3327 // should be ignored. Unknown fmtps do not constitute an error.
3328 continue;
3329 }
Donald Curtis0e07f922015-05-15 09:21:23 -07003330
3331 std::string name;
3332 std::string value;
3333 if (!ParseFmtpParam(rtc::string_trim(iter), &name, &value, error)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003334 return false;
3335 }
3336 codec_params[name] = value;
3337 }
3338
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003339 if (media_type == cricket::MEDIA_TYPE_AUDIO) {
3340 UpdateCodec<AudioContentDescription, cricket::AudioCodec>(
pkasting@chromium.orgd3245462015-02-23 21:28:22 +00003341 media_desc, payload_type, codec_params);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003342 } else if (media_type == cricket::MEDIA_TYPE_VIDEO) {
3343 UpdateCodec<VideoContentDescription, cricket::VideoCodec>(
pkasting@chromium.orgd3245462015-02-23 21:28:22 +00003344 media_desc, payload_type, codec_params);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003345 }
3346 return true;
3347}
3348
Yves Gerey665174f2018-06-19 15:03:05 +02003349bool ParseRtcpFbAttribute(const std::string& line,
Patrik Höglundb6b29e02018-06-21 16:58:01 +02003350 const cricket::MediaType media_type,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003351 MediaContentDescription* media_desc,
3352 SdpParseError* error) {
3353 if (media_type != cricket::MEDIA_TYPE_AUDIO &&
3354 media_type != cricket::MEDIA_TYPE_VIDEO) {
3355 return true;
3356 }
3357 std::vector<std::string> rtcp_fb_fields;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00003358 rtc::split(line.c_str(), kSdpDelimiterSpace, &rtcp_fb_fields);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003359 if (rtcp_fb_fields.size() < 2) {
3360 return ParseFailedGetValue(line, kAttributeRtcpFb, error);
3361 }
3362 std::string payload_type_string;
3363 if (!GetValue(rtcp_fb_fields[0], kAttributeRtcpFb, &payload_type_string,
3364 error)) {
3365 return false;
3366 }
wu@webrtc.org5e760e72014-04-02 23:19:09 +00003367 int payload_type = kWildcardPayloadType;
3368 if (payload_type_string != "*") {
pkasting@chromium.orge9facf82015-02-17 20:36:28 +00003369 if (!GetPayloadTypeFromString(line, payload_type_string, &payload_type,
3370 error)) {
wu@webrtc.org5e760e72014-04-02 23:19:09 +00003371 return false;
3372 }
3373 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003374 std::string id = rtcp_fb_fields[1];
3375 std::string param = "";
3376 for (std::vector<std::string>::iterator iter = rtcp_fb_fields.begin() + 2;
3377 iter != rtcp_fb_fields.end(); ++iter) {
3378 param.append(*iter);
3379 }
3380 const cricket::FeedbackParam feedback_param(id, param);
3381
3382 if (media_type == cricket::MEDIA_TYPE_AUDIO) {
pkasting@chromium.orgd3245462015-02-23 21:28:22 +00003383 UpdateCodec<AudioContentDescription, cricket::AudioCodec>(
3384 media_desc, payload_type, feedback_param);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003385 } else if (media_type == cricket::MEDIA_TYPE_VIDEO) {
pkasting@chromium.orgd3245462015-02-23 21:28:22 +00003386 UpdateCodec<VideoContentDescription, cricket::VideoCodec>(
3387 media_desc, payload_type, feedback_param);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003388 }
3389 return true;
3390}
3391
3392} // namespace webrtc