blob: 70a06343fd79528888aa8fc704810517ce614f60 [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(" ");
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001285 fmt.append(rtc::ToString<int>(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(" ");
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001293 fmt.append(rtc::ToString<int>(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
zstein4b2e0822017-02-17 19:48:38 -08001311 fmt.append(rtc::ToString<int>(sctp_port));
1312 } 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(" ");
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001320 fmt.append(rtc::ToString<int>(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) {
Peter Boström0c4e06b2015-10-07 12:23:21 +02001599 os << kSdpDelimiterSpace << rtc::ToString<uint32_t>(*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() << " "
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001890 << it->address().ipaddr().ToString() << " "
Yves Gerey665174f2018-06-19 15:03:05 +02001891 << it->address().PortAsString() << " " << kAttributeCandidateTyp << " "
mallinath@webrtc.org2d60c5e2014-08-08 22:29:20 +00001892 << type << " ";
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001893
1894 // Related address
1895 if (!it->related_address().IsNil()) {
1896 os << kAttributeCandidateRaddr << " "
1897 << it->related_address().ipaddr().ToString() << " "
1898 << kAttributeCandidateRport << " "
1899 << it->related_address().PortAsString() << " ";
1900 }
1901
mallinath@webrtc.org2d60c5e2014-08-08 22:29:20 +00001902 if (it->protocol() == cricket::TCP_PROTOCOL_NAME) {
mallinath@webrtc.orge999bd02014-08-13 06:05:55 +00001903 os << kTcpCandidateType << " " << it->tcptype() << " ";
mallinath@webrtc.org2d60c5e2014-08-08 22:29:20 +00001904 }
1905
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001906 // Extensions
1907 os << kAttributeCandidateGeneration << " " << it->generation();
honghaiza54a0802015-12-16 18:37:23 -08001908 if (include_ufrag && !it->username().empty()) {
1909 os << " " << kAttributeCandidateUfrag << " " << it->username();
1910 }
honghaiza0c44ea2016-03-23 16:07:48 -07001911 if (it->network_id() > 0) {
1912 os << " " << kAttributeCandidateNetworkId << " " << it->network_id();
1913 }
honghaize1a0c942016-02-16 14:54:56 -08001914 if (it->network_cost() > 0) {
1915 os << " " << kAttributeCandidateNetworkCost << " " << it->network_cost();
1916 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001917
1918 AddLine(os.str(), message);
1919 }
1920}
1921
1922void BuildIceOptions(const std::vector<std::string>& transport_options,
1923 std::string* message) {
1924 if (!transport_options.empty()) {
1925 std::ostringstream os;
1926 InitAttrLine(kAttributeIceOption, &os);
1927 os << kSdpDelimiterColon << transport_options[0];
1928 for (size_t i = 1; i < transport_options.size(); ++i) {
1929 os << kSdpDelimiterSpace << transport_options[i];
1930 }
1931 AddLine(os.str(), message);
1932 }
1933}
1934
pthatcher@webrtc.org3341b402015-02-13 21:14:22 +00001935bool IsRtp(const std::string& protocol) {
1936 return protocol.empty() ||
Yves Gerey665174f2018-06-19 15:03:05 +02001937 (protocol.find(cricket::kMediaProtocolRtpPrefix) != std::string::npos);
pthatcher@webrtc.org3341b402015-02-13 21:14:22 +00001938}
1939
1940bool IsDtlsSctp(const std::string& protocol) {
1941 // This intentionally excludes "SCTP" and "SCTP/DTLS".
lally@webrtc.orga7470932015-02-24 20:19:21 +00001942 return protocol.find(cricket::kMediaProtocolDtlsSctp) != std::string::npos;
pthatcher@webrtc.org3341b402015-02-13 21:14:22 +00001943}
1944
zhihuang38989e52017-03-21 11:04:53 -07001945bool ParseConnectionData(const std::string& line,
1946 rtc::SocketAddress* addr,
1947 SdpParseError* error) {
1948 // Parse the line from left to right.
1949 std::string token;
1950 std::string rightpart;
1951 // RFC 4566
1952 // c=<nettype> <addrtype> <connection-address>
1953 // Skip the "c="
1954 if (!rtc::tokenize_first(line, kSdpDelimiterEqual, &token, &rightpart)) {
1955 return ParseFailed(line, "Failed to parse the network type.", error);
1956 }
1957
1958 // Extract and verify the <nettype>
1959 if (!rtc::tokenize_first(rightpart, kSdpDelimiterSpace, &token, &rightpart) ||
1960 token != kConnectionNettype) {
1961 return ParseFailed(line,
1962 "Failed to parse the connection data. The network type "
1963 "is not currently supported.",
1964 error);
1965 }
1966
1967 // Extract the "<addrtype>" and "<connection-address>".
1968 if (!rtc::tokenize_first(rightpart, kSdpDelimiterSpace, &token, &rightpart)) {
1969 return ParseFailed(line, "Failed to parse the address type.", error);
1970 }
1971
1972 // The rightpart part should be the IP address without the slash which is used
1973 // for multicast.
1974 if (rightpart.find('/') != std::string::npos) {
1975 return ParseFailed(line,
1976 "Failed to parse the connection data. Multicast is not "
1977 "currently supported.",
1978 error);
1979 }
1980 addr->SetIP(rightpart);
1981
1982 // Verify that the addrtype matches the type of the parsed address.
1983 if ((addr->family() == AF_INET && token != "IP4") ||
1984 (addr->family() == AF_INET6 && token != "IP6")) {
1985 addr->Clear();
1986 return ParseFailed(
1987 line,
1988 "Failed to parse the connection data. The address type is mismatching.",
1989 error);
1990 }
1991 return true;
1992}
1993
1994bool ParseSessionDescription(const std::string& message,
1995 size_t* pos,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001996 std::string* session_id,
1997 std::string* session_version,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001998 TransportDescription* session_td,
1999 RtpHeaderExtensions* session_extmaps,
zhihuang38989e52017-03-21 11:04:53 -07002000 rtc::SocketAddress* connection_addr,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002001 cricket::SessionDescription* desc,
2002 SdpParseError* error) {
2003 std::string line;
2004
deadbeefc80741f2015-10-22 13:14:45 -07002005 desc->set_msid_supported(false);
2006
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002007 // RFC 4566
2008 // v= (protocol version)
2009 if (!GetLineWithType(message, pos, &line, kLineTypeVersion)) {
Yves Gerey665174f2018-06-19 15:03:05 +02002010 return ParseFailedExpectLine(message, *pos, kLineTypeVersion, std::string(),
2011 error);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002012 }
2013 // RFC 4566
2014 // o=<username> <sess-id> <sess-version> <nettype> <addrtype>
2015 // <unicast-address>
2016 if (!GetLineWithType(message, pos, &line, kLineTypeOrigin)) {
Yves Gerey665174f2018-06-19 15:03:05 +02002017 return ParseFailedExpectLine(message, *pos, kLineTypeOrigin, std::string(),
2018 error);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002019 }
2020 std::vector<std::string> fields;
Yves Gerey665174f2018-06-19 15:03:05 +02002021 rtc::split(line.substr(kLinePrefixLength), kSdpDelimiterSpace, &fields);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002022 const size_t expected_fields = 6;
2023 if (fields.size() != expected_fields) {
2024 return ParseFailedExpectFieldNum(line, expected_fields, error);
2025 }
2026 *session_id = fields[1];
2027 *session_version = fields[2];
2028
2029 // RFC 4566
2030 // s= (session name)
2031 if (!GetLineWithType(message, pos, &line, kLineTypeSessionName)) {
2032 return ParseFailedExpectLine(message, *pos, kLineTypeSessionName,
2033 std::string(), error);
2034 }
2035
Danil Chapovalov66cadcc2018-06-19 16:47:43 +02002036 // absl::optional lines
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002037 // Those are the optional lines, so shouldn't return false if not present.
2038 // RFC 4566
2039 // i=* (session information)
2040 GetLineWithType(message, pos, &line, kLineTypeSessionInfo);
2041
2042 // RFC 4566
2043 // u=* (URI of description)
2044 GetLineWithType(message, pos, &line, kLineTypeSessionUri);
2045
2046 // RFC 4566
2047 // e=* (email address)
2048 GetLineWithType(message, pos, &line, kLineTypeSessionEmail);
2049
2050 // RFC 4566
2051 // p=* (phone number)
2052 GetLineWithType(message, pos, &line, kLineTypeSessionPhone);
2053
2054 // RFC 4566
2055 // c=* (connection information -- not required if included in
2056 // all media)
zhihuang38989e52017-03-21 11:04:53 -07002057 if (GetLineWithType(message, pos, &line, kLineTypeConnection)) {
2058 if (!ParseConnectionData(line, connection_addr, error)) {
2059 return false;
2060 }
2061 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002062
2063 // RFC 4566
2064 // b=* (zero or more bandwidth information lines)
2065 while (GetLineWithType(message, pos, &line, kLineTypeSessionBandwidth)) {
2066 // By pass zero or more b lines.
2067 }
2068
2069 // RFC 4566
2070 // One or more time descriptions ("t=" and "r=" lines; see below)
2071 // t= (time the session is active)
2072 // r=* (zero or more repeat times)
2073 // Ensure there's at least one time description
2074 if (!GetLineWithType(message, pos, &line, kLineTypeTiming)) {
2075 return ParseFailedExpectLine(message, *pos, kLineTypeTiming, std::string(),
2076 error);
2077 }
2078
2079 while (GetLineWithType(message, pos, &line, kLineTypeRepeatTimes)) {
2080 // By pass zero or more r lines.
2081 }
2082
2083 // Go through the rest of the time descriptions
2084 while (GetLineWithType(message, pos, &line, kLineTypeTiming)) {
2085 while (GetLineWithType(message, pos, &line, kLineTypeRepeatTimes)) {
2086 // By pass zero or more r lines.
2087 }
2088 }
2089
2090 // RFC 4566
2091 // z=* (time zone adjustments)
2092 GetLineWithType(message, pos, &line, kLineTypeTimeZone);
2093
2094 // RFC 4566
2095 // k=* (encryption key)
2096 GetLineWithType(message, pos, &line, kLineTypeEncryptionKey);
2097
2098 // RFC 4566
2099 // a=* (zero or more session attribute lines)
2100 while (GetLineWithType(message, pos, &line, kLineTypeAttributes)) {
2101 if (HasAttribute(line, kAttributeGroup)) {
2102 if (!ParseGroupAttribute(line, desc, error)) {
2103 return false;
2104 }
2105 } else if (HasAttribute(line, kAttributeIceUfrag)) {
Yves Gerey665174f2018-06-19 15:03:05 +02002106 if (!GetValue(line, kAttributeIceUfrag, &(session_td->ice_ufrag),
2107 error)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002108 return false;
2109 }
2110 } else if (HasAttribute(line, kAttributeIcePwd)) {
2111 if (!GetValue(line, kAttributeIcePwd, &(session_td->ice_pwd), error)) {
2112 return false;
2113 }
2114 } else if (HasAttribute(line, kAttributeIceLite)) {
2115 session_td->ice_mode = cricket::ICEMODE_LITE;
2116 } else if (HasAttribute(line, kAttributeIceOption)) {
2117 if (!ParseIceOptions(line, &(session_td->transport_options), error)) {
2118 return false;
2119 }
2120 } else if (HasAttribute(line, kAttributeFingerprint)) {
2121 if (session_td->identity_fingerprint.get()) {
2122 return ParseFailed(
2123 line,
2124 "Can't have multiple fingerprint attributes at the same level.",
2125 error);
2126 }
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002127 rtc::SSLFingerprint* fingerprint = NULL;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002128 if (!ParseFingerprintAttribute(line, &fingerprint, error)) {
2129 return false;
2130 }
2131 session_td->identity_fingerprint.reset(fingerprint);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00002132 } else if (HasAttribute(line, kAttributeSetup)) {
2133 if (!ParseDtlsSetup(line, &(session_td->connection_role), error)) {
2134 return false;
2135 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002136 } else if (HasAttribute(line, kAttributeMsidSemantics)) {
2137 std::string semantics;
2138 if (!GetValue(line, kAttributeMsidSemantics, &semantics, error)) {
2139 return false;
2140 }
deadbeefc80741f2015-10-22 13:14:45 -07002141 desc->set_msid_supported(
2142 CaseInsensitiveFind(semantics, kMediaStreamSemantic));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002143 } else if (HasAttribute(line, kAttributeExtmap)) {
isheriff6f8d6862016-05-26 11:24:55 -07002144 RtpExtension extmap;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002145 if (!ParseExtmap(line, &extmap, error)) {
2146 return false;
2147 }
2148 session_extmaps->push_back(extmap);
2149 }
2150 }
2151
2152 return true;
2153}
2154
2155bool ParseGroupAttribute(const std::string& line,
2156 cricket::SessionDescription* desc,
2157 SdpParseError* error) {
nisseede5da42017-01-12 05:15:36 -08002158 RTC_DCHECK(desc != NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002159
2160 // RFC 5888 and draft-holmberg-mmusic-sdp-bundle-negotiation-00
2161 // a=group:BUNDLE video voice
2162 std::vector<std::string> fields;
Yves Gerey665174f2018-06-19 15:03:05 +02002163 rtc::split(line.substr(kLinePrefixLength), kSdpDelimiterSpace, &fields);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002164 std::string semantics;
2165 if (!GetValue(fields[0], kAttributeGroup, &semantics, error)) {
2166 return false;
2167 }
2168 cricket::ContentGroup group(semantics);
2169 for (size_t i = 1; i < fields.size(); ++i) {
2170 group.AddContentName(fields[i]);
2171 }
2172 desc->AddGroup(group);
2173 return true;
2174}
2175
2176static bool ParseFingerprintAttribute(const std::string& line,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002177 rtc::SSLFingerprint** fingerprint,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002178 SdpParseError* error) {
2179 if (!IsLineType(line, kLineTypeAttributes) ||
2180 !HasAttribute(line, kAttributeFingerprint)) {
2181 return ParseFailedExpectLine(line, 0, kLineTypeAttributes,
2182 kAttributeFingerprint, error);
2183 }
2184
2185 std::vector<std::string> fields;
Yves Gerey665174f2018-06-19 15:03:05 +02002186 rtc::split(line.substr(kLinePrefixLength), kSdpDelimiterSpace, &fields);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002187 const size_t expected_fields = 2;
2188 if (fields.size() != expected_fields) {
2189 return ParseFailedExpectFieldNum(line, expected_fields, error);
2190 }
2191
2192 // The first field here is "fingerprint:<hash>.
2193 std::string algorithm;
2194 if (!GetValue(fields[0], kAttributeFingerprint, &algorithm, error)) {
2195 return false;
2196 }
2197
2198 // Downcase the algorithm. Note that we don't need to downcase the
2199 // fingerprint because hex_decode can handle upper-case.
2200 std::transform(algorithm.begin(), algorithm.end(), algorithm.begin(),
2201 ::tolower);
2202
2203 // The second field is the digest value. De-hexify it.
Yves Gerey665174f2018-06-19 15:03:05 +02002204 *fingerprint = rtc::SSLFingerprint::CreateFromRfc4572(algorithm, fields[1]);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002205 if (!*fingerprint) {
Yves Gerey665174f2018-06-19 15:03:05 +02002206 return ParseFailed(line, "Failed to create fingerprint from the digest.",
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002207 error);
2208 }
2209
2210 return true;
2211}
2212
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00002213static bool ParseDtlsSetup(const std::string& line,
2214 cricket::ConnectionRole* role,
2215 SdpParseError* error) {
2216 // setup-attr = "a=setup:" role
2217 // role = "active" / "passive" / "actpass" / "holdconn"
2218 std::vector<std::string> fields;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002219 rtc::split(line.substr(kLinePrefixLength), kSdpDelimiterColon, &fields);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00002220 const size_t expected_fields = 2;
2221 if (fields.size() != expected_fields) {
2222 return ParseFailedExpectFieldNum(line, expected_fields, error);
2223 }
2224 std::string role_str = fields[1];
2225 if (!cricket::StringToConnectionRole(role_str, role)) {
2226 return ParseFailed(line, "Invalid attribute value.", error);
2227 }
2228 return true;
2229}
2230
deadbeef9d3584c2016-02-16 17:54:10 -08002231static bool ParseMsidAttribute(const std::string& line,
Seth Hampson5b4f0752018-04-02 16:31:36 -07002232 std::vector<std::string>* stream_ids,
deadbeef9d3584c2016-02-16 17:54:10 -08002233 std::string* track_id,
2234 SdpParseError* error) {
Seth Hampson5b4f0752018-04-02 16:31:36 -07002235 // https://datatracker.ietf.org/doc/draft-ietf-mmusic-msid/16/
deadbeef9d3584c2016-02-16 17:54:10 -08002236 // a=msid:<stream id> <track id>
2237 // msid-value = msid-id [ SP msid-appdata ]
2238 // msid-id = 1*64token-char ; see RFC 4566
2239 // msid-appdata = 1*64token-char ; see RFC 4566
2240 std::string field1;
Seth Hampson5b4f0752018-04-02 16:31:36 -07002241 std::string new_stream_id;
2242 std::string new_track_id;
deadbeef9d3584c2016-02-16 17:54:10 -08002243 if (!rtc::tokenize_first(line.substr(kLinePrefixLength), kSdpDelimiterSpace,
Seth Hampson5b4f0752018-04-02 16:31:36 -07002244 &field1, &new_track_id)) {
deadbeef9d3584c2016-02-16 17:54:10 -08002245 const size_t expected_fields = 2;
2246 return ParseFailedExpectFieldNum(line, expected_fields, error);
2247 }
2248
Seth Hampson5b4f0752018-04-02 16:31:36 -07002249 if (new_track_id.empty()) {
deadbeefa4549d62017-02-10 17:26:22 -08002250 return ParseFailed(line, "Missing track ID in msid attribute.", error);
2251 }
Seth Hampson5b4f0752018-04-02 16:31:36 -07002252 // All track ids should be the same within an m section in a Unified Plan SDP.
2253 if (!track_id->empty() && new_track_id.compare(*track_id) != 0) {
2254 return ParseFailed(
2255 line, "Two different track IDs in msid attribute in one m= section",
2256 error);
2257 }
2258 *track_id = new_track_id;
deadbeefa4549d62017-02-10 17:26:22 -08002259
deadbeef9d3584c2016-02-16 17:54:10 -08002260 // msid:<msid-id>
Seth Hampson5b4f0752018-04-02 16:31:36 -07002261 if (!GetValue(field1, kAttributeMsid, &new_stream_id, error)) {
deadbeef9d3584c2016-02-16 17:54:10 -08002262 return false;
2263 }
Seth Hampson5b4f0752018-04-02 16:31:36 -07002264 if (new_stream_id.empty()) {
deadbeefa4549d62017-02-10 17:26:22 -08002265 return ParseFailed(line, "Missing stream ID in msid attribute.", error);
2266 }
Seth Hampson5b4f0752018-04-02 16:31:36 -07002267 // The special value "-" indicates "no MediaStream".
2268 if (new_stream_id.compare(kNoStreamMsid) != 0) {
2269 stream_ids->push_back(new_stream_id);
2270 }
deadbeef9d3584c2016-02-16 17:54:10 -08002271 return true;
2272}
2273
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002274// RFC 3551
2275// PT encoding media type clock rate channels
2276// name (Hz)
2277// 0 PCMU A 8,000 1
2278// 1 reserved A
2279// 2 reserved A
2280// 3 GSM A 8,000 1
2281// 4 G723 A 8,000 1
2282// 5 DVI4 A 8,000 1
2283// 6 DVI4 A 16,000 1
2284// 7 LPC A 8,000 1
2285// 8 PCMA A 8,000 1
2286// 9 G722 A 8,000 1
2287// 10 L16 A 44,100 2
2288// 11 L16 A 44,100 1
2289// 12 QCELP A 8,000 1
2290// 13 CN A 8,000 1
2291// 14 MPA A 90,000 (see text)
2292// 15 G728 A 8,000 1
2293// 16 DVI4 A 11,025 1
2294// 17 DVI4 A 22,050 1
2295// 18 G729 A 8,000 1
2296struct StaticPayloadAudioCodec {
2297 const char* name;
2298 int clockrate;
Peter Kasting69558702016-01-12 16:26:35 -08002299 size_t channels;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002300};
2301static const StaticPayloadAudioCodec kStaticPayloadAudioCodecs[] = {
Yves Gerey665174f2018-06-19 15:03:05 +02002302 {"PCMU", 8000, 1}, {"reserved", 0, 0}, {"reserved", 0, 0},
2303 {"GSM", 8000, 1}, {"G723", 8000, 1}, {"DVI4", 8000, 1},
2304 {"DVI4", 16000, 1}, {"LPC", 8000, 1}, {"PCMA", 8000, 1},
2305 {"G722", 8000, 1}, {"L16", 44100, 2}, {"L16", 44100, 1},
2306 {"QCELP", 8000, 1}, {"CN", 8000, 1}, {"MPA", 90000, 1},
2307 {"G728", 8000, 1}, {"DVI4", 11025, 1}, {"DVI4", 22050, 1},
2308 {"G729", 8000, 1},
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002309};
2310
Yves Gerey665174f2018-06-19 15:03:05 +02002311void MaybeCreateStaticPayloadAudioCodecs(const std::vector<int>& fmts,
2312 AudioContentDescription* media_desc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002313 if (!media_desc) {
2314 return;
2315 }
deadbeef67cf2c12016-04-13 10:07:16 -07002316 RTC_DCHECK(media_desc->codecs().empty());
solenberg9fa49752016-10-08 13:02:44 -07002317 for (int payload_type : fmts) {
Yves Gerey665174f2018-06-19 15:03:05 +02002318 if (!media_desc->HasCodec(payload_type) && payload_type >= 0 &&
kjellander3e33bfe2016-06-20 07:04:09 -07002319 static_cast<uint32_t>(payload_type) <
2320 arraysize(kStaticPayloadAudioCodecs)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002321 std::string encoding_name = kStaticPayloadAudioCodecs[payload_type].name;
2322 int clock_rate = kStaticPayloadAudioCodecs[payload_type].clockrate;
Peter Kasting69558702016-01-12 16:26:35 -08002323 size_t channels = kStaticPayloadAudioCodecs[payload_type].channels;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002324 media_desc->AddCodec(cricket::AudioCodec(payload_type, encoding_name,
deadbeef67cf2c12016-04-13 10:07:16 -07002325 clock_rate, 0, channels));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002326 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002327 }
2328}
2329
2330template <class C>
2331static C* ParseContentDescription(const std::string& message,
Patrik Höglundb6b29e02018-06-21 16:58:01 +02002332 const cricket::MediaType media_type,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002333 int mline_index,
2334 const std::string& protocol,
deadbeef67cf2c12016-04-13 10:07:16 -07002335 const std::vector<int>& payload_types,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002336 size_t* pos,
2337 std::string* content_name,
deadbeef25ed4352016-12-12 18:37:36 -08002338 bool* bundle_only,
Steve Antone831b8c2018-02-01 12:22:16 -08002339 int* msid_signaling,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002340 TransportDescription* transport,
2341 std::vector<JsepIceCandidate*>* candidates,
2342 webrtc::SdpParseError* error) {
2343 C* media_desc = new C();
2344 switch (media_type) {
2345 case cricket::MEDIA_TYPE_AUDIO:
2346 *content_name = cricket::CN_AUDIO;
2347 break;
2348 case cricket::MEDIA_TYPE_VIDEO:
2349 *content_name = cricket::CN_VIDEO;
2350 break;
2351 case cricket::MEDIA_TYPE_DATA:
2352 *content_name = cricket::CN_DATA;
2353 break;
2354 default:
nissec80e7412017-01-11 05:56:46 -08002355 RTC_NOTREACHED();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002356 break;
2357 }
deadbeef67cf2c12016-04-13 10:07:16 -07002358 if (!ParseContent(message, media_type, mline_index, protocol, payload_types,
Steve Antone831b8c2018-02-01 12:22:16 -08002359 pos, content_name, bundle_only, msid_signaling, media_desc,
2360 transport, candidates, error)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002361 delete media_desc;
zhihuang38989e52017-03-21 11:04:53 -07002362 return nullptr;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002363 }
2364 // Sort the codecs according to the m-line fmt list.
deadbeef67cf2c12016-04-13 10:07:16 -07002365 std::unordered_map<int, int> payload_type_preferences;
2366 // "size + 1" so that the lowest preference payload type has a preference of
2367 // 1, which is greater than the default (0) for payload types not in the fmt
2368 // list.
2369 int preference = static_cast<int>(payload_types.size() + 1);
2370 for (int pt : payload_types) {
2371 payload_type_preferences[pt] = preference--;
2372 }
2373 std::vector<typename C::CodecType> codecs = media_desc->codecs();
Yves Gerey665174f2018-06-19 15:03:05 +02002374 std::sort(codecs.begin(), codecs.end(),
2375 [&payload_type_preferences](const typename C::CodecType& a,
2376 const typename C::CodecType& b) {
2377 return payload_type_preferences[a.id] >
2378 payload_type_preferences[b.id];
2379 });
deadbeef67cf2c12016-04-13 10:07:16 -07002380 media_desc->set_codecs(codecs);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002381 return media_desc;
2382}
2383
2384bool ParseMediaDescription(const std::string& message,
2385 const TransportDescription& session_td,
2386 const RtpHeaderExtensions& session_extmaps,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002387 size_t* pos,
zhihuang38989e52017-03-21 11:04:53 -07002388 const rtc::SocketAddress& session_connection_addr,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002389 cricket::SessionDescription* desc,
2390 std::vector<JsepIceCandidate*>* candidates,
2391 SdpParseError* error) {
nisseede5da42017-01-12 05:15:36 -08002392 RTC_DCHECK(desc != NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002393 std::string line;
2394 int mline_index = -1;
Steve Antone831b8c2018-02-01 12:22:16 -08002395 int msid_signaling = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002396
2397 // Zero or more media descriptions
2398 // RFC 4566
2399 // m=<media> <port> <proto> <fmt>
2400 while (GetLineWithType(message, pos, &line, kLineTypeMedia)) {
2401 ++mline_index;
2402
2403 std::vector<std::string> fields;
Yves Gerey665174f2018-06-19 15:03:05 +02002404 rtc::split(line.substr(kLinePrefixLength), kSdpDelimiterSpace, &fields);
zstein4b2e0822017-02-17 19:48:38 -08002405
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002406 const size_t expected_min_fields = 4;
2407 if (fields.size() < expected_min_fields) {
2408 return ParseFailedExpectMinFieldNum(line, expected_min_fields, error);
2409 }
deadbeef25ed4352016-12-12 18:37:36 -08002410 bool port_rejected = false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002411 // RFC 3264
2412 // To reject an offered stream, the port number in the corresponding stream
2413 // in the answer MUST be set to zero.
2414 if (fields[1] == kMediaPortRejected) {
deadbeef25ed4352016-12-12 18:37:36 -08002415 port_rejected = true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002416 }
2417
zhihuang38989e52017-03-21 11:04:53 -07002418 int port = 0;
2419 if (!rtc::FromString<int>(fields[1], &port) || !IsValidPort(port)) {
2420 return ParseFailed(line, "The port number is invalid", error);
2421 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002422 std::string protocol = fields[2];
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002423
2424 // <fmt>
deadbeef67cf2c12016-04-13 10:07:16 -07002425 std::vector<int> payload_types;
pthatcher@webrtc.org3341b402015-02-13 21:14:22 +00002426 if (IsRtp(protocol)) {
Yves Gerey665174f2018-06-19 15:03:05 +02002427 for (size_t j = 3; j < fields.size(); ++j) {
jiayl@webrtc.orgddb85ab2014-09-05 16:31:56 +00002428 // TODO(wu): Remove when below bug is fixed.
2429 // https://bugzilla.mozilla.org/show_bug.cgi?id=996329
pbosbb36fdf2015-07-09 07:48:14 -07002430 if (fields[j].empty() && j == fields.size() - 1) {
jiayl@webrtc.orgddb85ab2014-09-05 16:31:56 +00002431 continue;
2432 }
wu@webrtc.org36eda7c2014-04-15 20:37:30 +00002433
jiayl@webrtc.orgddb85ab2014-09-05 16:31:56 +00002434 int pl = 0;
pkasting@chromium.orge9facf82015-02-17 20:36:28 +00002435 if (!GetPayloadTypeFromString(line, fields[j], &pl, error)) {
jiayl@webrtc.orgddb85ab2014-09-05 16:31:56 +00002436 return false;
2437 }
deadbeef67cf2c12016-04-13 10:07:16 -07002438 payload_types.push_back(pl);
wu@webrtc.org5e760e72014-04-02 23:19:09 +00002439 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002440 }
2441
2442 // Make a temporary TransportDescription based on |session_td|.
2443 // Some of this gets overwritten by ParseContent.
deadbeef46eed762016-01-28 13:24:37 -08002444 TransportDescription transport(
2445 session_td.transport_options, session_td.ice_ufrag, session_td.ice_pwd,
2446 session_td.ice_mode, session_td.connection_role,
2447 session_td.identity_fingerprint.get());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002448
kwibergd1fe2812016-04-27 06:47:29 -07002449 std::unique_ptr<MediaContentDescription> content;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002450 std::string content_name;
deadbeef25ed4352016-12-12 18:37:36 -08002451 bool bundle_only = false;
Steve Antone831b8c2018-02-01 12:22:16 -08002452 int section_msid_signaling = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002453 if (HasAttribute(line, kMediaTypeVideo)) {
2454 content.reset(ParseContentDescription<VideoContentDescription>(
deadbeef67cf2c12016-04-13 10:07:16 -07002455 message, cricket::MEDIA_TYPE_VIDEO, mline_index, protocol,
Steve Antone831b8c2018-02-01 12:22:16 -08002456 payload_types, pos, &content_name, &bundle_only,
2457 &section_msid_signaling, &transport, candidates, error));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002458 } else if (HasAttribute(line, kMediaTypeAudio)) {
2459 content.reset(ParseContentDescription<AudioContentDescription>(
deadbeef67cf2c12016-04-13 10:07:16 -07002460 message, cricket::MEDIA_TYPE_AUDIO, mline_index, protocol,
Steve Antone831b8c2018-02-01 12:22:16 -08002461 payload_types, pos, &content_name, &bundle_only,
2462 &section_msid_signaling, &transport, candidates, error));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002463 } else if (HasAttribute(line, kMediaTypeData)) {
jiayl@webrtc.org0e527722014-09-08 21:43:43 +00002464 DataContentDescription* data_desc =
wu@webrtc.org78187522013-10-07 23:32:02 +00002465 ParseContentDescription<DataContentDescription>(
deadbeef67cf2c12016-04-13 10:07:16 -07002466 message, cricket::MEDIA_TYPE_DATA, mline_index, protocol,
Steve Antone831b8c2018-02-01 12:22:16 -08002467 payload_types, pos, &content_name, &bundle_only,
2468 &section_msid_signaling, &transport, candidates, error);
jiayl@webrtc.org0e527722014-09-08 21:43:43 +00002469 content.reset(data_desc);
wu@webrtc.org78187522013-10-07 23:32:02 +00002470
zstein4b2e0822017-02-17 19:48:38 -08002471 if (data_desc && IsDtlsSctp(protocol)) {
2472 int p;
2473 if (rtc::FromString(fields[3], &p)) {
2474 if (!AddSctpDataCodec(data_desc, p)) {
2475 return false;
2476 }
2477 } else if (fields[3] == kDefaultSctpmapProtocol) {
2478 data_desc->set_use_sctpmap(false);
2479 }
wu@webrtc.org78187522013-10-07 23:32:02 +00002480 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002481 } else {
Mirko Bonadei675513b2017-11-09 11:09:25 +01002482 RTC_LOG(LS_WARNING) << "Unsupported media type: " << line;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002483 continue;
2484 }
2485 if (!content.get()) {
2486 // ParseContentDescription returns NULL if failed.
2487 return false;
2488 }
2489
Steve Antone831b8c2018-02-01 12:22:16 -08002490 msid_signaling |= section_msid_signaling;
2491
deadbeef25ed4352016-12-12 18:37:36 -08002492 bool content_rejected = false;
deadbeef12771a12017-01-03 13:53:47 -08002493 // A port of 0 is not interpreted as a rejected m= section when it's
2494 // used along with a=bundle-only.
deadbeef25ed4352016-12-12 18:37:36 -08002495 if (bundle_only) {
deadbeef25ed4352016-12-12 18:37:36 -08002496 if (!port_rejected) {
deadbeef12771a12017-01-03 13:53:47 -08002497 // Usage of bundle-only with a nonzero port is unspecified. So just
2498 // ignore bundle-only if we see this.
2499 bundle_only = false;
Mirko Bonadei675513b2017-11-09 11:09:25 +01002500 RTC_LOG(LS_WARNING)
deadbeef12771a12017-01-03 13:53:47 -08002501 << "a=bundle-only attribute observed with a nonzero "
Jonas Olsson45cc8902018-02-13 10:37:07 +01002502 "port; this usage is unspecified so the attribute is being "
2503 "ignored.";
deadbeef25ed4352016-12-12 18:37:36 -08002504 }
2505 } else {
2506 // If not using bundle-only, interpret port 0 in the normal way; the m=
2507 // section is being rejected.
2508 content_rejected = port_rejected;
2509 }
2510
pthatcher@webrtc.org3341b402015-02-13 21:14:22 +00002511 if (IsRtp(protocol)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002512 // Set the extmap.
2513 if (!session_extmaps.empty() &&
2514 !content->rtp_header_extensions().empty()) {
2515 return ParseFailed("",
2516 "The a=extmap MUST be either all session level or "
2517 "all media level.",
2518 error);
2519 }
2520 for (size_t i = 0; i < session_extmaps.size(); ++i) {
2521 content->AddRtpHeaderExtension(session_extmaps[i]);
2522 }
2523 }
2524 content->set_protocol(protocol);
zhihuang38989e52017-03-21 11:04:53 -07002525
2526 // Use the session level connection address if the media level addresses are
2527 // not specified.
2528 rtc::SocketAddress address;
2529 address = content->connection_address().IsNil()
2530 ? session_connection_addr
2531 : content->connection_address();
2532 address.SetPort(port);
2533 content->set_connection_address(address);
2534
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002535 desc->AddContent(content_name,
Steve Anton5adfafd2017-12-20 16:34:00 -08002536 IsDtlsSctp(protocol) ? MediaProtocolType::kSctp
2537 : MediaProtocolType::kRtp,
deadbeef25ed4352016-12-12 18:37:36 -08002538 content_rejected, bundle_only, content.release());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002539 // Create TransportInfo with the media level "ice-pwd" and "ice-ufrag".
2540 TransportInfo transport_info(content_name, transport);
2541
2542 if (!desc->AddTransportInfo(transport_info)) {
2543 std::ostringstream description;
2544 description << "Failed to AddTransportInfo with content name: "
2545 << content_name;
2546 return ParseFailed("", description.str(), error);
2547 }
2548 }
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00002549
Steve Antone831b8c2018-02-01 12:22:16 -08002550 desc->set_msid_signaling(msid_signaling);
2551
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00002552 size_t end_of_message = message.size();
2553 if (mline_index == -1 && *pos != end_of_message) {
2554 ParseFailed(message, *pos, "Expects m line.", error);
2555 return false;
2556 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002557 return true;
2558}
2559
2560bool VerifyCodec(const cricket::Codec& codec) {
2561 // Codec has not been populated correctly unless the name has been set. This
2562 // can happen if an SDP has an fmtp or rtcp-fb with a payload type but doesn't
2563 // have a corresponding "rtpmap" line.
htab39db842016-12-08 01:50:48 -08002564 return !codec.name.empty();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002565}
2566
2567bool VerifyAudioCodecs(const AudioContentDescription* audio_desc) {
2568 const std::vector<cricket::AudioCodec>& codecs = audio_desc->codecs();
2569 for (std::vector<cricket::AudioCodec>::const_iterator iter = codecs.begin();
2570 iter != codecs.end(); ++iter) {
2571 if (!VerifyCodec(*iter)) {
2572 return false;
2573 }
2574 }
2575 return true;
2576}
2577
2578bool VerifyVideoCodecs(const VideoContentDescription* video_desc) {
2579 const std::vector<cricket::VideoCodec>& codecs = video_desc->codecs();
2580 for (std::vector<cricket::VideoCodec>::const_iterator iter = codecs.begin();
2581 iter != codecs.end(); ++iter) {
2582 if (!VerifyCodec(*iter)) {
2583 return false;
2584 }
2585 }
2586 return true;
2587}
2588
2589void AddParameters(const cricket::CodecParameterMap& parameters,
2590 cricket::Codec* codec) {
Yves Gerey665174f2018-06-19 15:03:05 +02002591 for (cricket::CodecParameterMap::const_iterator iter = parameters.begin();
2592 iter != parameters.end(); ++iter) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002593 codec->SetParam(iter->first, iter->second);
2594 }
2595}
2596
2597void AddFeedbackParameter(const cricket::FeedbackParam& feedback_param,
2598 cricket::Codec* codec) {
2599 codec->AddFeedbackParam(feedback_param);
2600}
2601
2602void AddFeedbackParameters(const cricket::FeedbackParams& feedback_params,
2603 cricket::Codec* codec) {
2604 for (std::vector<cricket::FeedbackParam>::const_iterator iter =
2605 feedback_params.params().begin();
2606 iter != feedback_params.params().end(); ++iter) {
2607 codec->AddFeedbackParam(*iter);
2608 }
2609}
2610
2611// Gets the current codec setting associated with |payload_type|. If there
changbin.shao@webrtc.org2d25b442015-03-16 04:14:34 +00002612// is no Codec associated with that payload type it returns an empty codec
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002613// with that payload type.
2614template <class T>
changbin.shao@webrtc.org2d25b442015-03-16 04:14:34 +00002615T GetCodecWithPayloadType(const std::vector<T>& codecs, int payload_type) {
magjedb05fa242016-11-11 04:00:16 -08002616 const T* codec = FindCodecById(codecs, payload_type);
2617 if (codec)
2618 return *codec;
2619 // Return empty codec with |payload_type|.
changbin.shao@webrtc.org2d25b442015-03-16 04:14:34 +00002620 T ret_val;
magjedb05fa242016-11-11 04:00:16 -08002621 ret_val.id = payload_type;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002622 return ret_val;
2623}
2624
2625// Updates or creates a new codec entry in the audio description.
2626template <class T, class U>
2627void AddOrReplaceCodec(MediaContentDescription* content_desc, const U& codec) {
2628 T* desc = static_cast<T*>(content_desc);
2629 std::vector<U> codecs = desc->codecs();
2630 bool found = false;
2631
2632 typename std::vector<U>::iterator iter;
2633 for (iter = codecs.begin(); iter != codecs.end(); ++iter) {
2634 if (iter->id == codec.id) {
2635 *iter = codec;
2636 found = true;
2637 break;
2638 }
2639 }
2640 if (!found) {
2641 desc->AddCodec(codec);
2642 return;
2643 }
2644 desc->set_codecs(codecs);
2645}
2646
2647// Adds or updates existing codec corresponding to |payload_type| according
2648// to |parameters|.
2649template <class T, class U>
Yves Gerey665174f2018-06-19 15:03:05 +02002650void UpdateCodec(MediaContentDescription* content_desc,
2651 int payload_type,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002652 const cricket::CodecParameterMap& parameters) {
2653 // Codec might already have been populated (from rtpmap).
changbin.shao@webrtc.org2d25b442015-03-16 04:14:34 +00002654 U new_codec = GetCodecWithPayloadType(static_cast<T*>(content_desc)->codecs(),
2655 payload_type);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002656 AddParameters(parameters, &new_codec);
2657 AddOrReplaceCodec<T, U>(content_desc, new_codec);
2658}
2659
2660// Adds or updates existing codec corresponding to |payload_type| according
2661// to |feedback_param|.
2662template <class T, class U>
Yves Gerey665174f2018-06-19 15:03:05 +02002663void UpdateCodec(MediaContentDescription* content_desc,
2664 int payload_type,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002665 const cricket::FeedbackParam& feedback_param) {
2666 // Codec might already have been populated (from rtpmap).
changbin.shao@webrtc.org2d25b442015-03-16 04:14:34 +00002667 U new_codec = GetCodecWithPayloadType(static_cast<T*>(content_desc)->codecs(),
2668 payload_type);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002669 AddFeedbackParameter(feedback_param, &new_codec);
2670 AddOrReplaceCodec<T, U>(content_desc, new_codec);
2671}
2672
jlmiller@webrtc.orga744a282015-02-18 21:37:46 +00002673template <class T>
2674bool PopWildcardCodec(std::vector<T>* codecs, T* wildcard_codec) {
2675 for (auto iter = codecs->begin(); iter != codecs->end(); ++iter) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002676 if (iter->id == kWildcardPayloadType) {
2677 *wildcard_codec = *iter;
2678 codecs->erase(iter);
2679 return true;
2680 }
2681 }
2682 return false;
2683}
2684
Yves Gerey665174f2018-06-19 15:03:05 +02002685template <class T>
jlmiller@webrtc.orga744a282015-02-18 21:37:46 +00002686void UpdateFromWildcardCodecs(cricket::MediaContentDescriptionImpl<T>* desc) {
2687 auto codecs = desc->codecs();
2688 T wildcard_codec;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002689 if (!PopWildcardCodec(&codecs, &wildcard_codec)) {
2690 return;
2691 }
jlmiller@webrtc.orga744a282015-02-18 21:37:46 +00002692 for (auto& codec : codecs) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002693 AddFeedbackParameters(wildcard_codec.feedback_params, &codec);
2694 }
jlmiller@webrtc.orga744a282015-02-18 21:37:46 +00002695 desc->set_codecs(codecs);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002696}
2697
Yves Gerey665174f2018-06-19 15:03:05 +02002698void AddAudioAttribute(const std::string& name,
2699 const std::string& value,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002700 AudioContentDescription* audio_desc) {
2701 if (value.empty()) {
2702 return;
2703 }
2704 std::vector<cricket::AudioCodec> codecs = audio_desc->codecs();
2705 for (std::vector<cricket::AudioCodec>::iterator iter = codecs.begin();
2706 iter != codecs.end(); ++iter) {
2707 iter->params[name] = value;
2708 }
2709 audio_desc->set_codecs(codecs);
2710}
2711
2712bool ParseContent(const std::string& message,
Patrik Höglundb6b29e02018-06-21 16:58:01 +02002713 const cricket::MediaType media_type,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002714 int mline_index,
2715 const std::string& protocol,
deadbeef67cf2c12016-04-13 10:07:16 -07002716 const std::vector<int>& payload_types,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002717 size_t* pos,
2718 std::string* content_name,
deadbeef25ed4352016-12-12 18:37:36 -08002719 bool* bundle_only,
Steve Antone831b8c2018-02-01 12:22:16 -08002720 int* msid_signaling,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002721 MediaContentDescription* media_desc,
2722 TransportDescription* transport,
2723 std::vector<JsepIceCandidate*>* candidates,
2724 SdpParseError* error) {
nisseede5da42017-01-12 05:15:36 -08002725 RTC_DCHECK(media_desc != NULL);
2726 RTC_DCHECK(content_name != NULL);
2727 RTC_DCHECK(transport != NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002728
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +00002729 if (media_type == cricket::MEDIA_TYPE_AUDIO) {
Steve Antonb1c1de12017-12-21 15:14:30 -08002730 MaybeCreateStaticPayloadAudioCodecs(payload_types, media_desc->as_audio());
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +00002731 }
2732
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002733 // The media level "ice-ufrag" and "ice-pwd".
2734 // The candidates before update the media level "ice-pwd" and "ice-ufrag".
2735 Candidates candidates_orig;
2736 std::string line;
2737 std::string mline_id;
2738 // Tracks created out of the ssrc attributes.
2739 StreamParamsVec tracks;
2740 SsrcInfoVec ssrc_infos;
2741 SsrcGroupVec ssrc_groups;
2742 std::string maxptime_as_string;
2743 std::string ptime_as_string;
Seth Hampson5b4f0752018-04-02 16:31:36 -07002744 std::vector<std::string> stream_ids;
deadbeef9d3584c2016-02-16 17:54:10 -08002745 std::string track_id;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002746
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002747 // Loop until the next m line
2748 while (!IsLineType(message, kLineTypeMedia, *pos)) {
2749 if (!GetLine(message, pos, &line)) {
2750 if (*pos >= message.size()) {
2751 break; // Done parsing
2752 } else {
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +00002753 return ParseFailed(message, *pos, "Invalid SDP line.", error);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002754 }
2755 }
2756
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002757 // RFC 4566
2758 // b=* (zero or more bandwidth information lines)
2759 if (IsLineType(line, kLineTypeSessionBandwidth)) {
2760 std::string bandwidth;
2761 if (HasAttribute(line, kApplicationSpecificMaximum)) {
2762 if (!GetValue(line, kApplicationSpecificMaximum, &bandwidth, error)) {
2763 return false;
2764 } else {
wu@webrtc.org5e760e72014-04-02 23:19:09 +00002765 int b = 0;
2766 if (!GetValueFromString(line, bandwidth, &b, error)) {
2767 return false;
2768 }
deadbeef3e8016e2017-08-03 17:49:30 -07002769 // TODO(deadbeef): Historically, applications may be setting a value
2770 // of -1 to mean "unset any previously set bandwidth limit", even
2771 // though ommitting the "b=AS" entirely will do just that. Once we've
2772 // transitioned applications to doing the right thing, it would be
2773 // better to treat this as a hard error instead of just ignoring it.
2774 if (b == -1) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01002775 RTC_LOG(LS_WARNING)
2776 << "Ignoring \"b=AS:-1\"; will be treated as \"no "
2777 "bandwidth limit\".";
deadbeef3e8016e2017-08-03 17:49:30 -07002778 continue;
2779 }
deadbeefbc88c6b2017-08-02 11:26:34 -07002780 if (b < 0) {
2781 return ParseFailed(line, "b=AS value can't be negative.", error);
2782 }
Peter Thatcherc0c3a862015-06-24 15:31:25 -07002783 // We should never use more than the default bandwidth for RTP-based
2784 // data channels. Don't allow SDP to set the bandwidth, because
2785 // that would give JS the opportunity to "break the Internet".
2786 // See: https://code.google.com/p/chromium/issues/detail?id=280726
2787 if (media_type == cricket::MEDIA_TYPE_DATA && IsRtp(protocol) &&
2788 b > cricket::kDataMaxBandwidth / 1000) {
2789 std::ostringstream description;
2790 description << "RTP-based data channels may not send more than "
2791 << cricket::kDataMaxBandwidth / 1000 << "kbps.";
2792 return ParseFailed(line, description.str(), error);
2793 }
deadbeefb2362572016-12-13 16:37:06 -08002794 // Prevent integer overflow.
2795 b = std::min(b, INT_MAX / 1000);
wu@webrtc.org5e760e72014-04-02 23:19:09 +00002796 media_desc->set_bandwidth(b * 1000);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002797 }
2798 }
2799 continue;
2800 }
2801
zhihuang38989e52017-03-21 11:04:53 -07002802 // Parse the media level connection data.
2803 if (IsLineType(line, kLineTypeConnection)) {
2804 rtc::SocketAddress addr;
2805 if (!ParseConnectionData(line, &addr, error)) {
2806 return false;
2807 }
2808 media_desc->set_connection_address(addr);
2809 continue;
2810 }
2811
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002812 if (!IsLineType(line, kLineTypeAttributes)) {
Steve Anton36b29d12017-10-30 09:57:42 -07002813 // TODO(deadbeef): Handle other lines if needed.
Mirko Bonadei675513b2017-11-09 11:09:25 +01002814 RTC_LOG(LS_INFO) << "Ignored line: " << line;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002815 continue;
2816 }
2817
2818 // Handle attributes common to SCTP and RTP.
2819 if (HasAttribute(line, kAttributeMid)) {
2820 // RFC 3388
2821 // mid-attribute = "a=mid:" identification-tag
2822 // identification-tag = token
2823 // Use the mid identification-tag as the content name.
2824 if (!GetValue(line, kAttributeMid, &mline_id, error)) {
2825 return false;
2826 }
2827 *content_name = mline_id;
deadbeef25ed4352016-12-12 18:37:36 -08002828 } else if (HasAttribute(line, kAttributeBundleOnly)) {
2829 *bundle_only = true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002830 } else if (HasAttribute(line, kAttributeCandidate)) {
2831 Candidate candidate;
2832 if (!ParseCandidate(line, &candidate, error, false)) {
2833 return false;
2834 }
deadbeef7bcdb692017-01-20 12:43:58 -08002835 // ParseCandidate will parse non-standard ufrag and password attributes,
2836 // since it's used for candidate trickling, but we only want to process
2837 // the "a=ice-ufrag"/"a=ice-pwd" values in a session description, so
2838 // strip them off at this point.
2839 candidate.set_username(std::string());
2840 candidate.set_password(std::string());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002841 candidates_orig.push_back(candidate);
2842 } else if (HasAttribute(line, kAttributeIceUfrag)) {
2843 if (!GetValue(line, kAttributeIceUfrag, &transport->ice_ufrag, error)) {
2844 return false;
2845 }
2846 } else if (HasAttribute(line, kAttributeIcePwd)) {
2847 if (!GetValue(line, kAttributeIcePwd, &transport->ice_pwd, error)) {
2848 return false;
2849 }
2850 } else if (HasAttribute(line, kAttributeIceOption)) {
2851 if (!ParseIceOptions(line, &transport->transport_options, error)) {
2852 return false;
2853 }
2854 } else if (HasAttribute(line, kAttributeFmtp)) {
2855 if (!ParseFmtpAttributes(line, media_type, media_desc, error)) {
2856 return false;
2857 }
2858 } else if (HasAttribute(line, kAttributeFingerprint)) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002859 rtc::SSLFingerprint* fingerprint = NULL;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002860
2861 if (!ParseFingerprintAttribute(line, &fingerprint, error)) {
2862 return false;
2863 }
2864 transport->identity_fingerprint.reset(fingerprint);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00002865 } else if (HasAttribute(line, kAttributeSetup)) {
2866 if (!ParseDtlsSetup(line, &(transport->connection_role), error)) {
2867 return false;
2868 }
pthatcher@webrtc.org3341b402015-02-13 21:14:22 +00002869 } else if (IsDtlsSctp(protocol) && HasAttribute(line, kAttributeSctpPort)) {
deadbeef7e146cb2016-09-28 10:04:34 -07002870 if (media_type != cricket::MEDIA_TYPE_DATA) {
2871 return ParseFailed(
2872 line, "sctp-port attribute found in non-data media description.",
2873 error);
2874 }
jiayl@webrtc.orgddb85ab2014-09-05 16:31:56 +00002875 int sctp_port;
2876 if (!ParseSctpPort(line, &sctp_port, error)) {
2877 return false;
2878 }
Steve Antonb1c1de12017-12-21 15:14:30 -08002879 if (!AddSctpDataCodec(media_desc->as_data(), sctp_port)) {
jiayl@webrtc.orgddb85ab2014-09-05 16:31:56 +00002880 return false;
2881 }
pthatcher@webrtc.org3341b402015-02-13 21:14:22 +00002882 } else if (IsRtp(protocol)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002883 //
2884 // RTP specific attrubtes
2885 //
2886 if (HasAttribute(line, kAttributeRtcpMux)) {
2887 media_desc->set_rtcp_mux(true);
deadbeef13871492015-12-09 12:37:51 -08002888 } else if (HasAttribute(line, kAttributeRtcpReducedSize)) {
2889 media_desc->set_rtcp_reduced_size(true);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002890 } else if (HasAttribute(line, kAttributeSsrcGroup)) {
2891 if (!ParseSsrcGroupAttribute(line, &ssrc_groups, error)) {
2892 return false;
2893 }
2894 } else if (HasAttribute(line, kAttributeSsrc)) {
Steve Antone831b8c2018-02-01 12:22:16 -08002895 if (!ParseSsrcAttribute(line, &ssrc_infos, msid_signaling, error)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002896 return false;
2897 }
2898 } else if (HasAttribute(line, kAttributeCrypto)) {
2899 if (!ParseCryptoAttribute(line, media_desc, error)) {
2900 return false;
2901 }
2902 } else if (HasAttribute(line, kAttributeRtpmap)) {
deadbeef67cf2c12016-04-13 10:07:16 -07002903 if (!ParseRtpmapAttribute(line, media_type, payload_types, media_desc,
2904 error)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002905 return false;
2906 }
2907 } else if (HasAttribute(line, kCodecParamMaxPTime)) {
2908 if (!GetValue(line, kCodecParamMaxPTime, &maxptime_as_string, error)) {
2909 return false;
2910 }
2911 } else if (HasAttribute(line, kAttributeRtcpFb)) {
2912 if (!ParseRtcpFbAttribute(line, media_type, media_desc, error)) {
2913 return false;
2914 }
2915 } else if (HasAttribute(line, kCodecParamPTime)) {
2916 if (!GetValue(line, kCodecParamPTime, &ptime_as_string, error)) {
2917 return false;
2918 }
2919 } else if (HasAttribute(line, kAttributeSendOnly)) {
Steve Anton4e70a722017-11-28 14:57:10 -08002920 media_desc->set_direction(RtpTransceiverDirection::kSendOnly);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002921 } else if (HasAttribute(line, kAttributeRecvOnly)) {
Steve Anton4e70a722017-11-28 14:57:10 -08002922 media_desc->set_direction(RtpTransceiverDirection::kRecvOnly);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002923 } else if (HasAttribute(line, kAttributeInactive)) {
Steve Anton4e70a722017-11-28 14:57:10 -08002924 media_desc->set_direction(RtpTransceiverDirection::kInactive);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002925 } else if (HasAttribute(line, kAttributeSendRecv)) {
Steve Anton4e70a722017-11-28 14:57:10 -08002926 media_desc->set_direction(RtpTransceiverDirection::kSendRecv);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002927 } else if (HasAttribute(line, kAttributeExtmap)) {
isheriff6f8d6862016-05-26 11:24:55 -07002928 RtpExtension extmap;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002929 if (!ParseExtmap(line, &extmap, error)) {
2930 return false;
2931 }
2932 media_desc->AddRtpHeaderExtension(extmap);
2933 } else if (HasAttribute(line, kAttributeXGoogleFlag)) {
2934 // Experimental attribute. Conference mode activates more aggressive
2935 // AEC and NS settings.
Steve Anton36b29d12017-10-30 09:57:42 -07002936 // TODO(deadbeef): expose API to set these directly.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002937 std::string flag_value;
2938 if (!GetValue(line, kAttributeXGoogleFlag, &flag_value, error)) {
2939 return false;
2940 }
2941 if (flag_value.compare(kValueConference) == 0)
2942 media_desc->set_conference_mode(true);
deadbeef9d3584c2016-02-16 17:54:10 -08002943 } else if (HasAttribute(line, kAttributeMsid)) {
Seth Hampson5b4f0752018-04-02 16:31:36 -07002944 if (!ParseMsidAttribute(line, &stream_ids, &track_id, error)) {
deadbeef9d3584c2016-02-16 17:54:10 -08002945 return false;
2946 }
Steve Antone831b8c2018-02-01 12:22:16 -08002947 *msid_signaling |= cricket::kMsidSignalingMediaSection;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002948 }
2949 } else {
2950 // Only parse lines that we are interested of.
Mirko Bonadei675513b2017-11-09 11:09:25 +01002951 RTC_LOG(LS_INFO) << "Ignored line: " << line;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002952 continue;
2953 }
2954 }
2955
2956 // Create tracks from the |ssrc_infos|.
Taylor Brandstetter5de6b752016-03-09 17:02:30 -08002957 // If the stream_id/track_id for all SSRCS are identical, one StreamParams
2958 // will be created in CreateTracksFromSsrcInfos, containing all the SSRCs from
2959 // the m= section.
Seth Hampson5897a6e2018-04-03 11:16:33 -07002960 if (!ssrc_infos.empty()) {
2961 CreateTracksFromSsrcInfos(ssrc_infos, stream_ids, track_id, &tracks,
2962 *msid_signaling);
2963 } else if (media_type != cricket::MEDIA_TYPE_DATA &&
2964 (*msid_signaling & cricket::kMsidSignalingMediaSection)) {
2965 // If the stream_ids/track_id was signaled but SSRCs were unsignaled we
2966 // still create a track. This isn't done for data media types because
2967 // StreamParams aren't used for SCTP streams, and RTP data channels don't
2968 // support unsignaled SSRCs.
2969 CreateTrackWithNoSsrcs(stream_ids, track_id, &tracks);
2970 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002971
2972 // Add the ssrc group to the track.
2973 for (SsrcGroupVec::iterator ssrc_group = ssrc_groups.begin();
2974 ssrc_group != ssrc_groups.end(); ++ssrc_group) {
2975 if (ssrc_group->ssrcs.empty()) {
2976 continue;
2977 }
Peter Boström0c4e06b2015-10-07 12:23:21 +02002978 uint32_t ssrc = ssrc_group->ssrcs.front();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002979 for (StreamParamsVec::iterator track = tracks.begin();
2980 track != tracks.end(); ++track) {
2981 if (track->has_ssrc(ssrc)) {
2982 track->ssrc_groups.push_back(*ssrc_group);
2983 }
2984 }
2985 }
2986
2987 // Add the new tracks to the |media_desc|.
deadbeef9d3584c2016-02-16 17:54:10 -08002988 for (StreamParams& track : tracks) {
2989 media_desc->AddStream(track);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002990 }
2991
2992 if (media_type == cricket::MEDIA_TYPE_AUDIO) {
Steve Antonb1c1de12017-12-21 15:14:30 -08002993 AudioContentDescription* audio_desc = media_desc->as_audio();
jlmiller@webrtc.orga744a282015-02-18 21:37:46 +00002994 UpdateFromWildcardCodecs(audio_desc);
2995
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002996 // Verify audio codec ensures that no audio codec has been populated with
2997 // only fmtp.
2998 if (!VerifyAudioCodecs(audio_desc)) {
2999 return ParseFailed("Failed to parse audio codecs correctly.", error);
3000 }
3001 AddAudioAttribute(kCodecParamMaxPTime, maxptime_as_string, audio_desc);
3002 AddAudioAttribute(kCodecParamPTime, ptime_as_string, audio_desc);
3003 }
3004
3005 if (media_type == cricket::MEDIA_TYPE_VIDEO) {
Steve Antonb1c1de12017-12-21 15:14:30 -08003006 VideoContentDescription* video_desc = media_desc->as_video();
jlmiller@webrtc.orga744a282015-02-18 21:37:46 +00003007 UpdateFromWildcardCodecs(video_desc);
3008 // Verify video codec ensures that no video codec has been populated with
3009 // only rtcp-fb.
3010 if (!VerifyVideoCodecs(video_desc)) {
3011 return ParseFailed("Failed to parse video codecs correctly.", error);
3012 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003013 }
3014
3015 // RFC 5245
3016 // Update the candidates with the media level "ice-pwd" and "ice-ufrag".
3017 for (Candidates::iterator it = candidates_orig.begin();
3018 it != candidates_orig.end(); ++it) {
nisseede5da42017-01-12 05:15:36 -08003019 RTC_DCHECK((*it).username().empty() ||
3020 (*it).username() == transport->ice_ufrag);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003021 (*it).set_username(transport->ice_ufrag);
nisseede5da42017-01-12 05:15:36 -08003022 RTC_DCHECK((*it).password().empty());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003023 (*it).set_password(transport->ice_pwd);
Yves Gerey665174f2018-06-19 15:03:05 +02003024 candidates->push_back(new JsepIceCandidate(mline_id, mline_index, *it));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003025 }
3026 return true;
3027}
3028
Steve Antone831b8c2018-02-01 12:22:16 -08003029bool ParseSsrcAttribute(const std::string& line,
3030 SsrcInfoVec* ssrc_infos,
3031 int* msid_signaling,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003032 SdpParseError* error) {
nisseede5da42017-01-12 05:15:36 -08003033 RTC_DCHECK(ssrc_infos != NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003034 // RFC 5576
3035 // a=ssrc:<ssrc-id> <attribute>
3036 // a=ssrc:<ssrc-id> <attribute>:<value>
3037 std::string field1, field2;
Donald Curtis144d0182015-05-15 13:14:24 -07003038 if (!rtc::tokenize_first(line.substr(kLinePrefixLength), kSdpDelimiterSpace,
3039 &field1, &field2)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003040 const size_t expected_fields = 2;
3041 return ParseFailedExpectFieldNum(line, expected_fields, error);
3042 }
3043
3044 // ssrc:<ssrc-id>
3045 std::string ssrc_id_s;
3046 if (!GetValue(field1, kAttributeSsrc, &ssrc_id_s, error)) {
3047 return false;
3048 }
Peter Boström0c4e06b2015-10-07 12:23:21 +02003049 uint32_t ssrc_id = 0;
wu@webrtc.org5e760e72014-04-02 23:19:09 +00003050 if (!GetValueFromString(line, ssrc_id_s, &ssrc_id, error)) {
3051 return false;
3052 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003053
3054 std::string attribute;
3055 std::string value;
Donald Curtis144d0182015-05-15 13:14:24 -07003056 if (!rtc::tokenize_first(field2, kSdpDelimiterColon, &attribute, &value)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003057 std::ostringstream description;
3058 description << "Failed to get the ssrc attribute value from " << field2
3059 << ". Expected format <attribute>:<value>.";
3060 return ParseFailed(line, description.str(), error);
3061 }
3062
3063 // Check if there's already an item for this |ssrc_id|. Create a new one if
3064 // there isn't.
3065 SsrcInfoVec::iterator ssrc_info = ssrc_infos->begin();
3066 for (; ssrc_info != ssrc_infos->end(); ++ssrc_info) {
3067 if (ssrc_info->ssrc_id == ssrc_id) {
3068 break;
3069 }
3070 }
3071 if (ssrc_info == ssrc_infos->end()) {
3072 SsrcInfo info;
3073 info.ssrc_id = ssrc_id;
3074 ssrc_infos->push_back(info);
3075 ssrc_info = ssrc_infos->end() - 1;
3076 }
3077
3078 // Store the info to the |ssrc_info|.
3079 if (attribute == kSsrcAttributeCname) {
3080 // RFC 5576
3081 // cname:<value>
3082 ssrc_info->cname = value;
3083 } else if (attribute == kSsrcAttributeMsid) {
3084 // draft-alvestrand-mmusic-msid-00
Seth Hampson5b4f0752018-04-02 16:31:36 -07003085 // msid:identifier [appdata]
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003086 std::vector<std::string> fields;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00003087 rtc::split(value, kSdpDelimiterSpace, &fields);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003088 if (fields.size() < 1 || fields.size() > 2) {
Yves Gerey665174f2018-06-19 15:03:05 +02003089 return ParseFailed(
3090 line, "Expected format \"msid:<identifier>[ <appdata>]\".", error);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003091 }
deadbeef9d3584c2016-02-16 17:54:10 -08003092 ssrc_info->stream_id = fields[0];
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003093 if (fields.size() == 2) {
deadbeef9d3584c2016-02-16 17:54:10 -08003094 ssrc_info->track_id = fields[1];
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003095 }
Steve Antone831b8c2018-02-01 12:22:16 -08003096 *msid_signaling |= cricket::kMsidSignalingSsrcAttribute;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003097 } else if (attribute == kSsrcAttributeMslabel) {
3098 // draft-alvestrand-rtcweb-mid-01
3099 // mslabel:<value>
3100 ssrc_info->mslabel = value;
3101 } else if (attribute == kSSrcAttributeLabel) {
3102 // The label isn't defined.
3103 // label:<value>
3104 ssrc_info->label = value;
3105 }
3106 return true;
3107}
3108
3109bool ParseSsrcGroupAttribute(const std::string& line,
3110 SsrcGroupVec* ssrc_groups,
3111 SdpParseError* error) {
nisseede5da42017-01-12 05:15:36 -08003112 RTC_DCHECK(ssrc_groups != NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003113 // RFC 5576
3114 // a=ssrc-group:<semantics> <ssrc-id> ...
3115 std::vector<std::string> fields;
Yves Gerey665174f2018-06-19 15:03:05 +02003116 rtc::split(line.substr(kLinePrefixLength), kSdpDelimiterSpace, &fields);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003117 const size_t expected_min_fields = 2;
3118 if (fields.size() < expected_min_fields) {
3119 return ParseFailedExpectMinFieldNum(line, expected_min_fields, error);
3120 }
3121 std::string semantics;
3122 if (!GetValue(fields[0], kAttributeSsrcGroup, &semantics, error)) {
3123 return false;
3124 }
Peter Boström0c4e06b2015-10-07 12:23:21 +02003125 std::vector<uint32_t> ssrcs;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003126 for (size_t i = 1; i < fields.size(); ++i) {
Peter Boström0c4e06b2015-10-07 12:23:21 +02003127 uint32_t ssrc = 0;
wu@webrtc.org5e760e72014-04-02 23:19:09 +00003128 if (!GetValueFromString(line, fields[i], &ssrc, error)) {
3129 return false;
3130 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003131 ssrcs.push_back(ssrc);
3132 }
3133 ssrc_groups->push_back(SsrcGroup(semantics, ssrcs));
3134 return true;
3135}
3136
3137bool ParseCryptoAttribute(const std::string& line,
3138 MediaContentDescription* media_desc,
3139 SdpParseError* error) {
3140 std::vector<std::string> fields;
Yves Gerey665174f2018-06-19 15:03:05 +02003141 rtc::split(line.substr(kLinePrefixLength), kSdpDelimiterSpace, &fields);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003142 // RFC 4568
3143 // a=crypto:<tag> <crypto-suite> <key-params> [<session-params>]
3144 const size_t expected_min_fields = 3;
3145 if (fields.size() < expected_min_fields) {
3146 return ParseFailedExpectMinFieldNum(line, expected_min_fields, error);
3147 }
3148 std::string tag_value;
3149 if (!GetValue(fields[0], kAttributeCrypto, &tag_value, error)) {
3150 return false;
3151 }
wu@webrtc.org5e760e72014-04-02 23:19:09 +00003152 int tag = 0;
3153 if (!GetValueFromString(line, tag_value, &tag, error)) {
3154 return false;
3155 }
jbauch083b73f2015-07-16 02:46:32 -07003156 const std::string& crypto_suite = fields[1];
3157 const std::string& key_params = fields[2];
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003158 std::string session_params;
3159 if (fields.size() > 3) {
3160 session_params = fields[3];
3161 }
Yves Gerey665174f2018-06-19 15:03:05 +02003162 media_desc->AddCrypto(
3163 CryptoParams(tag, crypto_suite, key_params, session_params));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003164 return true;
3165}
3166
3167// Updates or creates a new codec entry in the audio description with according
deadbeef67cf2c12016-04-13 10:07:16 -07003168// to |name|, |clockrate|, |bitrate|, and |channels|.
3169void UpdateCodec(int payload_type,
3170 const std::string& name,
3171 int clockrate,
3172 int bitrate,
3173 size_t channels,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003174 AudioContentDescription* audio_desc) {
3175 // Codec may already be populated with (only) optional parameters
3176 // (from an fmtp).
changbin.shao@webrtc.org2d25b442015-03-16 04:14:34 +00003177 cricket::AudioCodec codec =
3178 GetCodecWithPayloadType(audio_desc->codecs(), payload_type);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003179 codec.name = name;
3180 codec.clockrate = clockrate;
3181 codec.bitrate = bitrate;
3182 codec.channels = channels;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003183 AddOrReplaceCodec<AudioContentDescription, cricket::AudioCodec>(audio_desc,
3184 codec);
3185}
3186
3187// Updates or creates a new codec entry in the video description according to
deadbeef67cf2c12016-04-13 10:07:16 -07003188// |name|, |width|, |height|, and |framerate|.
3189void UpdateCodec(int payload_type,
3190 const std::string& name,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003191 VideoContentDescription* video_desc) {
3192 // Codec may already be populated with (only) optional parameters
3193 // (from an fmtp).
changbin.shao@webrtc.org2d25b442015-03-16 04:14:34 +00003194 cricket::VideoCodec codec =
3195 GetCodecWithPayloadType(video_desc->codecs(), payload_type);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003196 codec.name = name;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003197 AddOrReplaceCodec<VideoContentDescription, cricket::VideoCodec>(video_desc,
3198 codec);
3199}
3200
3201bool ParseRtpmapAttribute(const std::string& line,
Patrik Höglundb6b29e02018-06-21 16:58:01 +02003202 const cricket::MediaType media_type,
deadbeef67cf2c12016-04-13 10:07:16 -07003203 const std::vector<int>& payload_types,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003204 MediaContentDescription* media_desc,
3205 SdpParseError* error) {
3206 std::vector<std::string> fields;
Yves Gerey665174f2018-06-19 15:03:05 +02003207 rtc::split(line.substr(kLinePrefixLength), kSdpDelimiterSpace, &fields);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003208 // RFC 4566
3209 // a=rtpmap:<payload type> <encoding name>/<clock rate>[/<encodingparameters>]
3210 const size_t expected_min_fields = 2;
3211 if (fields.size() < expected_min_fields) {
3212 return ParseFailedExpectMinFieldNum(line, expected_min_fields, error);
3213 }
3214 std::string payload_type_value;
3215 if (!GetValue(fields[0], kAttributeRtpmap, &payload_type_value, error)) {
3216 return false;
3217 }
wu@webrtc.org5e760e72014-04-02 23:19:09 +00003218 int payload_type = 0;
pkasting@chromium.orge9facf82015-02-17 20:36:28 +00003219 if (!GetPayloadTypeFromString(line, payload_type_value, &payload_type,
3220 error)) {
wu@webrtc.org5e760e72014-04-02 23:19:09 +00003221 return false;
3222 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003223
deadbeef67cf2c12016-04-13 10:07:16 -07003224 if (std::find(payload_types.begin(), payload_types.end(), payload_type) ==
3225 payload_types.end()) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01003226 RTC_LOG(LS_WARNING) << "Ignore rtpmap line that did not appear in the "
Jonas Olsson45cc8902018-02-13 10:37:07 +01003227 "<fmt> of the m-line: "
3228 << line;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003229 return true;
3230 }
jbauch083b73f2015-07-16 02:46:32 -07003231 const std::string& encoder = fields[1];
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003232 std::vector<std::string> codec_params;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00003233 rtc::split(encoder, '/', &codec_params);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003234 // <encoding name>/<clock rate>[/<encodingparameters>]
3235 // 2 mandatory fields
3236 if (codec_params.size() < 2 || codec_params.size() > 3) {
3237 return ParseFailed(line,
3238 "Expected format \"<encoding name>/<clock rate>"
3239 "[/<encodingparameters>]\".",
3240 error);
3241 }
jbauch083b73f2015-07-16 02:46:32 -07003242 const std::string& encoding_name = codec_params[0];
wu@webrtc.org5e760e72014-04-02 23:19:09 +00003243 int clock_rate = 0;
3244 if (!GetValueFromString(line, codec_params[1], &clock_rate, error)) {
3245 return false;
3246 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003247 if (media_type == cricket::MEDIA_TYPE_VIDEO) {
Steve Antonb1c1de12017-12-21 15:14:30 -08003248 VideoContentDescription* video_desc = media_desc->as_video();
Yves Gerey665174f2018-06-19 15:03:05 +02003249 UpdateCodec(payload_type, encoding_name, video_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003250 } else if (media_type == cricket::MEDIA_TYPE_AUDIO) {
3251 // RFC 4566
3252 // For audio streams, <encoding parameters> indicates the number
3253 // of audio channels. This parameter is OPTIONAL and may be
3254 // omitted if the number of channels is one, provided that no
3255 // additional parameters are needed.
Peter Kasting69558702016-01-12 16:26:35 -08003256 size_t channels = 1;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003257 if (codec_params.size() == 3) {
wu@webrtc.org5e760e72014-04-02 23:19:09 +00003258 if (!GetValueFromString(line, codec_params[2], &channels, error)) {
3259 return false;
3260 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003261 }
Steve Antonb1c1de12017-12-21 15:14:30 -08003262 AudioContentDescription* audio_desc = media_desc->as_audio();
ossue1405ad2017-01-23 08:55:48 -08003263 UpdateCodec(payload_type, encoding_name, clock_rate, 0, channels,
deadbeef67cf2c12016-04-13 10:07:16 -07003264 audio_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003265 } else if (media_type == cricket::MEDIA_TYPE_DATA) {
Steve Antonb1c1de12017-12-21 15:14:30 -08003266 DataContentDescription* data_desc = media_desc->as_data();
deadbeef67cf2c12016-04-13 10:07:16 -07003267 data_desc->AddCodec(cricket::DataCodec(payload_type, encoding_name));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003268 }
3269 return true;
3270}
3271
Yves Gerey665174f2018-06-19 15:03:05 +02003272bool ParseFmtpParam(const std::string& line,
3273 std::string* parameter,
3274 std::string* value,
3275 SdpParseError* error) {
Donald Curtis0e07f922015-05-15 09:21:23 -07003276 if (!rtc::tokenize_first(line, kSdpDelimiterEqual, parameter, value)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003277 ParseFailed(line, "Unable to parse fmtp parameter. \'=\' missing.", error);
3278 return false;
3279 }
3280 // a=fmtp:<payload_type> <param1>=<value1>; <param2>=<value2>; ...
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003281 return true;
3282}
3283
Yves Gerey665174f2018-06-19 15:03:05 +02003284bool ParseFmtpAttributes(const std::string& line,
Patrik Höglundb6b29e02018-06-21 16:58:01 +02003285 const cricket::MediaType media_type,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003286 MediaContentDescription* media_desc,
3287 SdpParseError* error) {
3288 if (media_type != cricket::MEDIA_TYPE_AUDIO &&
3289 media_type != cricket::MEDIA_TYPE_VIDEO) {
3290 return true;
3291 }
Donald Curtis0e07f922015-05-15 09:21:23 -07003292
3293 std::string line_payload;
3294 std::string line_params;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003295
3296 // RFC 5576
3297 // a=fmtp:<format> <format specific parameters>
3298 // At least two fields, whereas the second one is any of the optional
3299 // parameters.
Donald Curtis144d0182015-05-15 13:14:24 -07003300 if (!rtc::tokenize_first(line.substr(kLinePrefixLength), kSdpDelimiterSpace,
3301 &line_payload, &line_params)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003302 ParseFailedExpectMinFieldNum(line, 2, error);
3303 return false;
3304 }
3305
Donald Curtis0e07f922015-05-15 09:21:23 -07003306 // Parse out the payload information.
pkasting@chromium.orgd3245462015-02-23 21:28:22 +00003307 std::string payload_type_str;
Donald Curtis0e07f922015-05-15 09:21:23 -07003308 if (!GetValue(line_payload, kAttributeFmtp, &payload_type_str, error)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003309 return false;
3310 }
3311
Donald Curtis0e07f922015-05-15 09:21:23 -07003312 int payload_type = 0;
Donald Curtis144d0182015-05-15 13:14:24 -07003313 if (!GetPayloadTypeFromString(line_payload, payload_type_str, &payload_type,
3314 error)) {
Donald Curtis0e07f922015-05-15 09:21:23 -07003315 return false;
3316 }
3317
3318 // Parse out format specific parameters.
3319 std::vector<std::string> fields;
3320 rtc::split(line_params, kSdpDelimiterSemicolon, &fields);
3321
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003322 cricket::CodecParameterMap codec_params;
Donald Curtis144d0182015-05-15 13:14:24 -07003323 for (auto& iter : fields) {
Donald Curtis0e07f922015-05-15 09:21:23 -07003324 if (iter.find(kSdpDelimiterEqual) == std::string::npos) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003325 // Only fmtps with equals are currently supported. Other fmtp types
3326 // should be ignored. Unknown fmtps do not constitute an error.
3327 continue;
3328 }
Donald Curtis0e07f922015-05-15 09:21:23 -07003329
3330 std::string name;
3331 std::string value;
3332 if (!ParseFmtpParam(rtc::string_trim(iter), &name, &value, error)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003333 return false;
3334 }
3335 codec_params[name] = value;
3336 }
3337
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003338 if (media_type == cricket::MEDIA_TYPE_AUDIO) {
3339 UpdateCodec<AudioContentDescription, cricket::AudioCodec>(
pkasting@chromium.orgd3245462015-02-23 21:28:22 +00003340 media_desc, payload_type, codec_params);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003341 } else if (media_type == cricket::MEDIA_TYPE_VIDEO) {
3342 UpdateCodec<VideoContentDescription, cricket::VideoCodec>(
pkasting@chromium.orgd3245462015-02-23 21:28:22 +00003343 media_desc, payload_type, codec_params);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003344 }
3345 return true;
3346}
3347
Yves Gerey665174f2018-06-19 15:03:05 +02003348bool ParseRtcpFbAttribute(const std::string& line,
Patrik Höglundb6b29e02018-06-21 16:58:01 +02003349 const cricket::MediaType media_type,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003350 MediaContentDescription* media_desc,
3351 SdpParseError* error) {
3352 if (media_type != cricket::MEDIA_TYPE_AUDIO &&
3353 media_type != cricket::MEDIA_TYPE_VIDEO) {
3354 return true;
3355 }
3356 std::vector<std::string> rtcp_fb_fields;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00003357 rtc::split(line.c_str(), kSdpDelimiterSpace, &rtcp_fb_fields);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003358 if (rtcp_fb_fields.size() < 2) {
3359 return ParseFailedGetValue(line, kAttributeRtcpFb, error);
3360 }
3361 std::string payload_type_string;
3362 if (!GetValue(rtcp_fb_fields[0], kAttributeRtcpFb, &payload_type_string,
3363 error)) {
3364 return false;
3365 }
wu@webrtc.org5e760e72014-04-02 23:19:09 +00003366 int payload_type = kWildcardPayloadType;
3367 if (payload_type_string != "*") {
pkasting@chromium.orge9facf82015-02-17 20:36:28 +00003368 if (!GetPayloadTypeFromString(line, payload_type_string, &payload_type,
3369 error)) {
wu@webrtc.org5e760e72014-04-02 23:19:09 +00003370 return false;
3371 }
3372 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003373 std::string id = rtcp_fb_fields[1];
3374 std::string param = "";
3375 for (std::vector<std::string>::iterator iter = rtcp_fb_fields.begin() + 2;
3376 iter != rtcp_fb_fields.end(); ++iter) {
3377 param.append(*iter);
3378 }
3379 const cricket::FeedbackParam feedback_param(id, param);
3380
3381 if (media_type == cricket::MEDIA_TYPE_AUDIO) {
pkasting@chromium.orgd3245462015-02-23 21:28:22 +00003382 UpdateCodec<AudioContentDescription, cricket::AudioCodec>(
3383 media_desc, payload_type, feedback_param);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003384 } else if (media_type == cricket::MEDIA_TYPE_VIDEO) {
pkasting@chromium.orgd3245462015-02-23 21:28:22 +00003385 UpdateCodec<VideoContentDescription, cricket::VideoCodec>(
3386 media_desc, payload_type, feedback_param);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003387 }
3388 return true;
3389}
3390
3391} // namespace webrtc