blob: 094525a8b6ccfd5c2fd4c63c9f3fb8a9bc775b76 [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
Steve Anton10542f22019-01-11 09:11:00 -080011#include "pc/webrtc_sdp.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>
Steve Anton4905edb2018-10-15 19:27:44 -070023#include <utility>
henrike@webrtc.org28e20752013-07-10 00:45:36 +000024#include <vector>
25
Steve Anton5a1de872018-12-18 11:25:55 -080026#include "absl/memory/memory.h"
Niels Möller039743e2018-10-23 10:07:25 +020027#include "absl/strings/match.h"
Patrik Höglunde2d6a062017-10-05 14:53:33 +020028#include "api/candidate.h"
Steve Anton10542f22019-01-11 09:11:00 -080029#include "api/crypto_params.h"
30#include "api/jsep_ice_candidate.h"
31#include "api/jsep_session_description.h"
32#include "api/media_types.h"
isheriff6f8d6862016-05-26 11:24:55 -070033// for RtpExtension
Steve Anton10542f22019-01-11 09:11:00 -080034#include "api/rtp_parameters.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020035#include "media/base/codec.h"
Steve Anton10542f22019-01-11 09:11:00 -080036#include "media/base/media_constants.h"
37#include "media/base/rtp_utils.h"
38#include "media/sctp/sctp_transport_internal.h"
39#include "p2p/base/p2p_constants.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020040#include "p2p/base/port.h"
Steve Anton10542f22019-01-11 09:11:00 -080041#include "pc/media_session.h"
42#include "pc/sdp_serializer.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020043#include "rtc_base/arraysize.h"
44#include "rtc_base/checks.h"
45#include "rtc_base/logging.h"
Steve Anton10542f22019-01-11 09:11:00 -080046#include "rtc_base/message_digest.h"
Jonas Olssonec9e4922018-09-05 09:53:49 +020047#include "rtc_base/strings/string_builder.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000048
49using cricket::AudioContentDescription;
50using cricket::Candidate;
51using cricket::Candidates;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000052using cricket::ContentInfo;
53using cricket::CryptoParams;
54using cricket::DataContentDescription;
55using cricket::ICE_CANDIDATE_COMPONENT_RTP;
56using cricket::ICE_CANDIDATE_COMPONENT_RTCP;
57using cricket::kCodecParamMaxBitrate;
58using cricket::kCodecParamMaxPTime;
59using cricket::kCodecParamMaxQuantization;
60using cricket::kCodecParamMinBitrate;
61using cricket::kCodecParamMinPTime;
62using cricket::kCodecParamPTime;
63using cricket::kCodecParamSPropStereo;
buildbot@webrtc.orged97bb02014-05-07 11:15:20 +000064using cricket::kCodecParamStartBitrate;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000065using cricket::kCodecParamStereo;
66using cricket::kCodecParamUseInbandFec;
Minyue Li7100dcd2015-03-27 05:05:59 +010067using cricket::kCodecParamUseDtx;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000068using cricket::kCodecParamSctpProtocol;
69using cricket::kCodecParamSctpStreams;
henrike@webrtc.org1e09a712013-07-26 19:17:59 +000070using cricket::kCodecParamMaxAverageBitrate;
buildbot@webrtc.org5d639b32014-09-10 07:57:12 +000071using cricket::kCodecParamMaxPlaybackRate;
stefan@webrtc.org85d27942014-06-09 12:51:39 +000072using cricket::kCodecParamAssociatedPayloadType;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000073using cricket::MediaContentDescription;
74using cricket::MediaType;
isheriff6f8d6862016-05-26 11:24:55 -070075using cricket::RtpHeaderExtensions;
Steve Anton5adfafd2017-12-20 16:34:00 -080076using cricket::MediaProtocolType;
Amit Hilbuchc57d5732018-12-11 15:30:11 -080077using cricket::RidDescription;
Amit Hilbucha2012042018-12-03 11:35:05 -080078using cricket::SimulcastDescription;
Amit Hilbuchc57d5732018-12-11 15:30:11 -080079using cricket::SimulcastLayer;
80using cricket::SimulcastLayerList;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000081using cricket::SsrcGroup;
82using cricket::StreamParams;
83using cricket::StreamParamsVec;
84using cricket::TransportDescription;
85using cricket::TransportInfo;
86using cricket::VideoContentDescription;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000087using rtc::SocketAddress;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000088
henrike@webrtc.org28e20752013-07-10 00:45:36 +000089namespace cricket {
90class SessionDescription;
91}
92
deadbeef90f1e1e2017-02-10 12:35:05 -080093// TODO(deadbeef): Switch to using anonymous namespace rather than declaring
94// everything "static".
henrike@webrtc.org28e20752013-07-10 00:45:36 +000095namespace webrtc {
96
97// Line type
98// RFC 4566
99// An SDP session description consists of a number of lines of text of
100// the form:
101// <type>=<value>
102// where <type> MUST be exactly one case-significant character.
deadbeef9d3584c2016-02-16 17:54:10 -0800103static const int kLinePrefixLength = 2; // Length of <type>=
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000104static const char kLineTypeVersion = 'v';
105static const char kLineTypeOrigin = 'o';
106static const char kLineTypeSessionName = 's';
107static const char kLineTypeSessionInfo = 'i';
108static const char kLineTypeSessionUri = 'u';
109static const char kLineTypeSessionEmail = 'e';
110static const char kLineTypeSessionPhone = 'p';
111static const char kLineTypeSessionBandwidth = 'b';
112static const char kLineTypeTiming = 't';
113static const char kLineTypeRepeatTimes = 'r';
114static const char kLineTypeTimeZone = 'z';
115static const char kLineTypeEncryptionKey = 'k';
116static const char kLineTypeMedia = 'm';
117static const char kLineTypeConnection = 'c';
118static const char kLineTypeAttributes = 'a';
119
120// Attributes
121static const char kAttributeGroup[] = "group";
122static const char kAttributeMid[] = "mid";
deadbeef9d3584c2016-02-16 17:54:10 -0800123static const char kAttributeMsid[] = "msid";
deadbeef25ed4352016-12-12 18:37:36 -0800124static const char kAttributeBundleOnly[] = "bundle-only";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000125static const char kAttributeRtcpMux[] = "rtcp-mux";
deadbeef13871492015-12-09 12:37:51 -0800126static const char kAttributeRtcpReducedSize[] = "rtcp-rsize";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000127static const char kAttributeSsrc[] = "ssrc";
128static const char kSsrcAttributeCname[] = "cname";
Johannes Kron0854eb62018-10-10 22:33:20 +0200129static const char kAttributeExtmapAllowMixed[] = "extmap-allow-mixed";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000130static const char kAttributeExtmap[] = "extmap";
131// draft-alvestrand-mmusic-msid-01
132// a=msid-semantic: WMS
Seth Hampson5b4f0752018-04-02 16:31:36 -0700133// This is a legacy field supported only for Plan B semantics.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000134static const char kAttributeMsidSemantics[] = "msid-semantic";
135static const char kMediaStreamSemantic[] = "WMS";
136static const char kSsrcAttributeMsid[] = "msid";
137static const char kDefaultMsid[] = "default";
Seth Hampson5b4f0752018-04-02 16:31:36 -0700138static const char kNoStreamMsid[] = "-";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000139static const char kSsrcAttributeMslabel[] = "mslabel";
140static const char kSSrcAttributeLabel[] = "label";
141static const char kAttributeSsrcGroup[] = "ssrc-group";
142static const char kAttributeCrypto[] = "crypto";
143static const char kAttributeCandidate[] = "candidate";
144static const char kAttributeCandidateTyp[] = "typ";
145static const char kAttributeCandidateRaddr[] = "raddr";
146static const char kAttributeCandidateRport[] = "rport";
honghaiza54a0802015-12-16 18:37:23 -0800147static const char kAttributeCandidateUfrag[] = "ufrag";
148static const char kAttributeCandidatePwd[] = "pwd";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000149static const char kAttributeCandidateGeneration[] = "generation";
honghaiza0c44ea2016-03-23 16:07:48 -0700150static const char kAttributeCandidateNetworkId[] = "network-id";
honghaize1a0c942016-02-16 14:54:56 -0800151static const char kAttributeCandidateNetworkCost[] = "network-cost";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000152static const char kAttributeFingerprint[] = "fingerprint";
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +0000153static const char kAttributeSetup[] = "setup";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000154static const char kAttributeFmtp[] = "fmtp";
155static const char kAttributeRtpmap[] = "rtpmap";
wu@webrtc.org78187522013-10-07 23:32:02 +0000156static const char kAttributeSctpmap[] = "sctpmap";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000157static const char kAttributeRtcp[] = "rtcp";
158static const char kAttributeIceUfrag[] = "ice-ufrag";
159static const char kAttributeIcePwd[] = "ice-pwd";
160static const char kAttributeIceLite[] = "ice-lite";
161static const char kAttributeIceOption[] = "ice-options";
162static const char kAttributeSendOnly[] = "sendonly";
163static const char kAttributeRecvOnly[] = "recvonly";
164static const char kAttributeRtcpFb[] = "rtcp-fb";
165static const char kAttributeSendRecv[] = "sendrecv";
166static const char kAttributeInactive[] = "inactive";
jiayl@webrtc.orgddb85ab2014-09-05 16:31:56 +0000167// draft-ietf-mmusic-sctp-sdp-07
168// a=sctp-port
169static const char kAttributeSctpPort[] = "sctp-port";
Amit Hilbucha2012042018-12-03 11:35:05 -0800170// draft-ietf-mmusic-sdp-simulcast-13
171// a=simulcast
172static const char kAttributeSimulcast[] = "simulcast";
Amit Hilbuchc57d5732018-12-11 15:30:11 -0800173// draft-ietf-mmusic-rid-15
174// a=rid
175static const char kAttributeRid[] = "rid";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000176
177// Experimental flags
178static const char kAttributeXGoogleFlag[] = "x-google-flag";
179static const char kValueConference[] = "conference";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000180
181// Candidate
182static const char kCandidateHost[] = "host";
183static const char kCandidateSrflx[] = "srflx";
Honghai Zhang7fb69db2016-03-14 11:59:18 -0700184static const char kCandidatePrflx[] = "prflx";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000185static const char kCandidateRelay[] = "relay";
mallinath@webrtc.org2d60c5e2014-08-08 22:29:20 +0000186static const char kTcpCandidateType[] = "tcptype";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000187
Jonas Olssonec9e4922018-09-05 09:53:49 +0200188// rtc::StringBuilder doesn't have a << overload for chars, while rtc::split and
189// rtc::tokenize_first both take a char delimiter. To handle both cases these
190// constants come in pairs of a chars and length-one strings.
191static const char kSdpDelimiterEqual[] = "=";
192static const char kSdpDelimiterEqualChar = '=';
193static const char kSdpDelimiterSpace[] = " ";
194static const char kSdpDelimiterSpaceChar = ' ';
195static const char kSdpDelimiterColon[] = ":";
196static const char kSdpDelimiterColonChar = ':';
197static const char kSdpDelimiterSemicolon[] = ";";
198static const char kSdpDelimiterSemicolonChar = ';';
199static const char kSdpDelimiterSlashChar = '/';
200static const char kNewLine[] = "\n";
201static const char kNewLineChar = '\n';
202static const char kReturnChar = '\r';
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000203static const char kLineBreak[] = "\r\n";
204
Steve Anton36b29d12017-10-30 09:57:42 -0700205// TODO(deadbeef): Generate the Session and Time description
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000206// instead of hardcoding.
207static const char kSessionVersion[] = "v=0";
208// RFC 4566
209static const char kSessionOriginUsername[] = "-";
210static const char kSessionOriginSessionId[] = "0";
211static const char kSessionOriginSessionVersion[] = "0";
212static const char kSessionOriginNettype[] = "IN";
213static const char kSessionOriginAddrtype[] = "IP4";
214static const char kSessionOriginAddress[] = "127.0.0.1";
215static const char kSessionName[] = "s=-";
216static const char kTimeDescription[] = "t=0 0";
217static const char kAttrGroup[] = "a=group:BUNDLE";
218static const char kConnectionNettype[] = "IN";
pthatcher@webrtc.orgc9d6d142014-10-23 23:37:22 +0000219static const char kConnectionIpv4Addrtype[] = "IP4";
220static const char kConnectionIpv6Addrtype[] = "IP6";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000221static const char kMediaTypeVideo[] = "video";
222static const char kMediaTypeAudio[] = "audio";
223static const char kMediaTypeData[] = "application";
224static const char kMediaPortRejected[] = "0";
pthatcher@webrtc.orgc9d6d142014-10-23 23:37:22 +0000225// draft-ietf-mmusic-trickle-ice-01
226// When no candidates have been gathered, set the connection
227// address to IP6 ::.
perkj@webrtc.orgd105cc82014-11-07 11:22:06 +0000228// TODO(perkj): FF can not parse IP6 ::. See http://crbug/430333
229// Use IPV4 per default.
230static const char kDummyAddress[] = "0.0.0.0";
pthatcher@webrtc.orgc9d6d142014-10-23 23:37:22 +0000231static const char kDummyPort[] = "9";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000232// RFC 3556
233static const char kApplicationSpecificMaximum[] = "AS";
234
wu@webrtc.org78187522013-10-07 23:32:02 +0000235static const char kDefaultSctpmapProtocol[] = "webrtc-datachannel";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000236
pkasting@chromium.orgd3245462015-02-23 21:28:22 +0000237// RTP payload type is in the 0-127 range. Use -1 to indicate "all" payload
238// types.
239const int kWildcardPayloadType = -1;
240
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000241struct SsrcInfo {
Peter Boström0c4e06b2015-10-07 12:23:21 +0200242 uint32_t ssrc_id;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000243 std::string cname;
deadbeef9d3584c2016-02-16 17:54:10 -0800244 std::string stream_id;
245 std::string track_id;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000246
247 // For backward compatibility.
248 // TODO(ronghuawu): Remove below 2 fields once all the clients support msid.
249 std::string label;
250 std::string mslabel;
251};
252typedef std::vector<SsrcInfo> SsrcInfoVec;
253typedef std::vector<SsrcGroup> SsrcGroupVec;
254
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000255template <class T>
256static void AddFmtpLine(const T& codec, std::string* message);
257static void BuildMediaDescription(const ContentInfo* content_info,
258 const TransportInfo* transport_info,
Patrik Höglundb6b29e02018-06-21 16:58:01 +0200259 const cricket::MediaType media_type,
wu@webrtc.org4c3e9912014-07-16 21:03:13 +0000260 const std::vector<Candidate>& candidates,
Steve Antone831b8c2018-02-01 12:22:16 -0800261 int msid_signaling,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000262 std::string* message);
zstein4b2e0822017-02-17 19:48:38 -0800263static void BuildSctpContentAttributes(std::string* message,
264 int sctp_port,
265 bool use_sctpmap);
deadbeef9d3584c2016-02-16 17:54:10 -0800266static void BuildRtpContentAttributes(const MediaContentDescription* media_desc,
Patrik Höglundb6b29e02018-06-21 16:58:01 +0200267 const cricket::MediaType media_type,
Steve Antone831b8c2018-02-01 12:22:16 -0800268 int msid_signaling,
deadbeef9d3584c2016-02-16 17:54:10 -0800269 std::string* message);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000270static void BuildRtpMap(const MediaContentDescription* media_desc,
Patrik Höglundb6b29e02018-06-21 16:58:01 +0200271 const cricket::MediaType media_type,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000272 std::string* message);
273static void BuildCandidate(const std::vector<Candidate>& candidates,
honghaiza54a0802015-12-16 18:37:23 -0800274 bool include_ufrag,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000275 std::string* message);
276static void BuildIceOptions(const std::vector<std::string>& transport_options,
277 std::string* message);
pthatcher@webrtc.org3341b402015-02-13 21:14:22 +0000278static bool IsRtp(const std::string& protocol);
279static bool IsDtlsSctp(const std::string& protocol);
zhihuang38989e52017-03-21 11:04:53 -0700280static bool ParseSessionDescription(const std::string& message,
281 size_t* pos,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000282 std::string* session_id,
283 std::string* session_version,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000284 TransportDescription* session_td,
285 RtpHeaderExtensions* session_extmaps,
zhihuang38989e52017-03-21 11:04:53 -0700286 rtc::SocketAddress* connection_addr,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000287 cricket::SessionDescription* desc,
288 SdpParseError* error);
289static bool ParseGroupAttribute(const std::string& line,
290 cricket::SessionDescription* desc,
291 SdpParseError* error);
292static bool ParseMediaDescription(
293 const std::string& message,
294 const TransportDescription& session_td,
295 const RtpHeaderExtensions& session_extmaps,
zhihuang38989e52017-03-21 11:04:53 -0700296 size_t* pos,
297 const rtc::SocketAddress& session_connection_addr,
298 cricket::SessionDescription* desc,
Steve Anton5a1de872018-12-18 11:25:55 -0800299 std::vector<std::unique_ptr<JsepIceCandidate>>* candidates,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000300 SdpParseError* error);
Steve Anton5a1de872018-12-18 11:25:55 -0800301static bool ParseContent(
302 const std::string& message,
303 const cricket::MediaType media_type,
304 int mline_index,
305 const std::string& protocol,
306 const std::vector<int>& payload_types,
307 size_t* pos,
308 std::string* content_name,
309 bool* bundle_only,
310 int* msid_signaling,
311 MediaContentDescription* media_desc,
312 TransportDescription* transport,
313 std::vector<std::unique_ptr<JsepIceCandidate>>* candidates,
314 SdpParseError* error);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000315static bool ParseSsrcAttribute(const std::string& line,
316 SsrcInfoVec* ssrc_infos,
Steve Antone831b8c2018-02-01 12:22:16 -0800317 int* msid_signaling,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000318 SdpParseError* error);
319static bool ParseSsrcGroupAttribute(const std::string& line,
320 SsrcGroupVec* ssrc_groups,
321 SdpParseError* error);
322static bool ParseCryptoAttribute(const std::string& line,
323 MediaContentDescription* media_desc,
324 SdpParseError* error);
325static bool ParseRtpmapAttribute(const std::string& line,
Patrik Höglundb6b29e02018-06-21 16:58:01 +0200326 const cricket::MediaType media_type,
deadbeef67cf2c12016-04-13 10:07:16 -0700327 const std::vector<int>& payload_types,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000328 MediaContentDescription* media_desc,
329 SdpParseError* error);
330static bool ParseFmtpAttributes(const std::string& line,
Patrik Höglundb6b29e02018-06-21 16:58:01 +0200331 const cricket::MediaType media_type,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000332 MediaContentDescription* media_desc,
333 SdpParseError* error);
Yves Gerey665174f2018-06-19 15:03:05 +0200334static bool ParseFmtpParam(const std::string& line,
335 std::string* parameter,
336 std::string* value,
337 SdpParseError* error);
338static bool ParseCandidate(const std::string& message,
339 Candidate* candidate,
340 SdpParseError* error,
341 bool is_raw);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000342static bool ParseRtcpFbAttribute(const std::string& line,
Patrik Höglundb6b29e02018-06-21 16:58:01 +0200343 const cricket::MediaType media_type,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000344 MediaContentDescription* media_desc,
345 SdpParseError* error);
346static bool ParseIceOptions(const std::string& line,
347 std::vector<std::string>* transport_options,
348 SdpParseError* error);
349static bool ParseExtmap(const std::string& line,
isheriff6f8d6862016-05-26 11:24:55 -0700350 RtpExtension* extmap,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000351 SdpParseError* error);
Steve Anton4905edb2018-10-15 19:27:44 -0700352static bool ParseFingerprintAttribute(
353 const std::string& line,
354 std::unique_ptr<rtc::SSLFingerprint>* fingerprint,
355 SdpParseError* error);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +0000356static bool ParseDtlsSetup(const std::string& line,
357 cricket::ConnectionRole* role,
358 SdpParseError* error);
deadbeef9d3584c2016-02-16 17:54:10 -0800359static bool ParseMsidAttribute(const std::string& line,
Seth Hampson5b4f0752018-04-02 16:31:36 -0700360 std::vector<std::string>* stream_ids,
deadbeef9d3584c2016-02-16 17:54:10 -0800361 std::string* track_id,
362 SdpParseError* error);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000363
Amit Hilbuchc57d5732018-12-11 15:30:11 -0800364static void RemoveInvalidRidDescriptions(const std::vector<int>& payload_types,
365 std::vector<RidDescription>* rids);
366
367static SimulcastLayerList RemoveRidsFromSimulcastLayerList(
368 const std::set<std::string>& to_remove,
369 const SimulcastLayerList& layers);
370
371static void RemoveInvalidRidsFromSimulcast(
372 const std::vector<RidDescription>& rids,
373 SimulcastDescription* simulcast);
374
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000375// Helper functions
376
377// Below ParseFailed*** functions output the line that caused the parsing
378// failure and the detailed reason (|description|) of the failure to |error|.
379// The functions always return false so that they can be used directly in the
380// following way when error happens:
381// "return ParseFailed***(...);"
382
383// The line starting at |line_start| of |message| is the failing line.
384// The reason for the failure should be provided in the |description|.
385// An example of a description could be "unknown character".
386static bool ParseFailed(const std::string& message,
387 size_t line_start,
388 const std::string& description,
389 SdpParseError* error) {
390 // Get the first line of |message| from |line_start|.
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +0000391 std::string first_line;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000392 size_t line_end = message.find(kNewLine, line_start);
393 if (line_end != std::string::npos) {
Jonas Olssonec9e4922018-09-05 09:53:49 +0200394 if (line_end > 0 && (message.at(line_end - 1) == kReturnChar)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000395 --line_end;
396 }
397 first_line = message.substr(line_start, (line_end - line_start));
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +0000398 } else {
399 first_line = message.substr(line_start);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000400 }
401
402 if (error) {
403 error->line = first_line;
404 error->description = description;
405 }
Mirko Bonadei675513b2017-11-09 11:09:25 +0100406 RTC_LOG(LS_ERROR) << "Failed to parse: \"" << first_line
407 << "\". Reason: " << description;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000408 return false;
409}
410
411// |line| is the failing line. The reason for the failure should be
412// provided in the |description|.
413static bool ParseFailed(const std::string& line,
414 const std::string& description,
415 SdpParseError* error) {
416 return ParseFailed(line, 0, description, error);
417}
418
419// Parses failure where the failing SDP line isn't know or there are multiple
420// failing lines.
Yves Gerey665174f2018-06-19 15:03:05 +0200421static bool ParseFailed(const std::string& description, SdpParseError* error) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000422 return ParseFailed("", description, error);
423}
424
425// |line| is the failing line. The failure is due to the fact that |line|
426// doesn't have |expected_fields| fields.
427static bool ParseFailedExpectFieldNum(const std::string& line,
428 int expected_fields,
429 SdpParseError* error) {
Jonas Olssonec9e4922018-09-05 09:53:49 +0200430 rtc::StringBuilder description;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000431 description << "Expects " << expected_fields << " fields.";
432 return ParseFailed(line, description.str(), error);
433}
434
435// |line| is the failing line. The failure is due to the fact that |line| has
436// less than |expected_min_fields| fields.
437static bool ParseFailedExpectMinFieldNum(const std::string& line,
438 int expected_min_fields,
439 SdpParseError* error) {
Jonas Olssonec9e4922018-09-05 09:53:49 +0200440 rtc::StringBuilder description;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000441 description << "Expects at least " << expected_min_fields << " fields.";
442 return ParseFailed(line, description.str(), error);
443}
444
445// |line| is the failing line. The failure is due to the fact that it failed to
446// get the value of |attribute|.
447static bool ParseFailedGetValue(const std::string& line,
448 const std::string& attribute,
449 SdpParseError* error) {
Jonas Olssonec9e4922018-09-05 09:53:49 +0200450 rtc::StringBuilder description;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000451 description << "Failed to get the value of attribute: " << attribute;
452 return ParseFailed(line, description.str(), error);
453}
454
455// The line starting at |line_start| of |message| is the failing line. The
456// failure is due to the line type (e.g. the "m" part of the "m-line")
457// not matching what is expected. The expected line type should be
458// provided as |line_type|.
459static bool ParseFailedExpectLine(const std::string& message,
460 size_t line_start,
461 const char line_type,
462 const std::string& line_value,
463 SdpParseError* error) {
Jonas Olssonec9e4922018-09-05 09:53:49 +0200464 rtc::StringBuilder description;
465 description << "Expect line: " << std::string(1, line_type) << "="
466 << line_value;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000467 return ParseFailed(message, line_start, description.str(), error);
468}
469
470static bool AddLine(const std::string& line, std::string* message) {
471 if (!message)
472 return false;
473
474 message->append(line);
475 message->append(kLineBreak);
476 return true;
477}
478
479static bool GetLine(const std::string& message,
480 size_t* pos,
481 std::string* line) {
482 size_t line_begin = *pos;
483 size_t line_end = message.find(kNewLine, line_begin);
484 if (line_end == std::string::npos) {
485 return false;
486 }
487 // Update the new start position
488 *pos = line_end + 1;
Jonas Olssonec9e4922018-09-05 09:53:49 +0200489 if (line_end > 0 && (message.at(line_end - 1) == kReturnChar)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000490 --line_end;
491 }
492 *line = message.substr(line_begin, (line_end - line_begin));
493 const char* cline = line->c_str();
494 // RFC 4566
495 // An SDP session description consists of a number of lines of text of
496 // the form:
497 // <type>=<value>
498 // where <type> MUST be exactly one case-significant character and
499 // <value> is structured text whose format depends on <type>.
500 // Whitespace MUST NOT be used on either side of the "=" sign.
Taylor Brandstetter93a7b242018-04-16 10:45:24 -0700501 //
502 // However, an exception to the whitespace rule is made for "s=", since
503 // RFC4566 also says:
504 //
505 // If a session has no meaningful name, the value "s= " SHOULD be used
506 // (i.e., a single space as the session name).
507 if (line->length() < 3 || !islower(cline[0]) ||
Jonas Olssonec9e4922018-09-05 09:53:49 +0200508 cline[1] != kSdpDelimiterEqualChar ||
509 (cline[0] != kLineTypeSessionName &&
510 cline[2] == kSdpDelimiterSpaceChar)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000511 *pos = line_begin;
512 return false;
513 }
514 return true;
515}
516
517// Init |os| to "|type|=|value|".
518static void InitLine(const char type,
519 const std::string& value,
Jonas Olssonec9e4922018-09-05 09:53:49 +0200520 rtc::StringBuilder* os) {
521 os->Clear();
522 *os << std::string(1, type) << kSdpDelimiterEqual << value;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000523}
524
525// Init |os| to "a=|attribute|".
Jonas Olssonec9e4922018-09-05 09:53:49 +0200526static void InitAttrLine(const std::string& attribute, rtc::StringBuilder* os) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000527 InitLine(kLineTypeAttributes, attribute, os);
528}
529
530// Writes a SDP attribute line based on |attribute| and |value| to |message|.
Yves Gerey665174f2018-06-19 15:03:05 +0200531static void AddAttributeLine(const std::string& attribute,
532 int value,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000533 std::string* message) {
Jonas Olssonec9e4922018-09-05 09:53:49 +0200534 rtc::StringBuilder os;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000535 InitAttrLine(attribute, &os);
536 os << kSdpDelimiterColon << value;
537 AddLine(os.str(), message);
538}
539
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000540static bool IsLineType(const std::string& message,
541 const char type,
542 size_t line_start) {
543 if (message.size() < line_start + kLinePrefixLength) {
544 return false;
545 }
546 const char* cmessage = message.c_str();
547 return (cmessage[line_start] == type &&
Jonas Olssonec9e4922018-09-05 09:53:49 +0200548 cmessage[line_start + 1] == kSdpDelimiterEqualChar);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000549}
550
Yves Gerey665174f2018-06-19 15:03:05 +0200551static bool IsLineType(const std::string& line, const char type) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000552 return IsLineType(line, type, 0);
553}
554
Yves Gerey665174f2018-06-19 15:03:05 +0200555static bool GetLineWithType(const std::string& message,
556 size_t* pos,
557 std::string* line,
558 const char type) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000559 if (!IsLineType(message, type, *pos)) {
560 return false;
561 }
562
563 if (!GetLine(message, pos, line))
564 return false;
565
566 return true;
567}
568
569static bool HasAttribute(const std::string& line,
570 const std::string& attribute) {
Johannes Kron211856b2018-09-06 12:12:28 +0200571 if (line.compare(kLinePrefixLength, attribute.size(), attribute) == 0) {
572 // Make sure that the match is not only a partial match. If length of
573 // strings doesn't match, the next character of the line must be ':' or ' '.
574 // This function is also used for media descriptions (e.g., "m=audio 9..."),
575 // hence the need to also allow space in the end.
576 RTC_CHECK_LE(kLinePrefixLength + attribute.size(), line.size());
577 if ((kLinePrefixLength + attribute.size()) == line.size() ||
578 line[kLinePrefixLength + attribute.size()] == kSdpDelimiterColonChar ||
579 line[kLinePrefixLength + attribute.size()] == kSdpDelimiterSpaceChar) {
580 return true;
581 }
582 }
583 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000584}
585
Peter Boström0c4e06b2015-10-07 12:23:21 +0200586static bool AddSsrcLine(uint32_t ssrc_id,
587 const std::string& attribute,
588 const std::string& value,
589 std::string* message) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000590 // RFC 5576
591 // a=ssrc:<ssrc-id> <attribute>:<value>
Jonas Olssonec9e4922018-09-05 09:53:49 +0200592 rtc::StringBuilder os;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000593 InitAttrLine(kAttributeSsrc, &os);
Yves Gerey665174f2018-06-19 15:03:05 +0200594 os << kSdpDelimiterColon << ssrc_id << kSdpDelimiterSpace << attribute
595 << kSdpDelimiterColon << value;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000596 return AddLine(os.str(), message);
597}
598
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000599// Get value only from <attribute>:<value>.
Yves Gerey665174f2018-06-19 15:03:05 +0200600static bool GetValue(const std::string& message,
601 const std::string& attribute,
602 std::string* value,
603 SdpParseError* error) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000604 std::string leftpart;
Jonas Olssonec9e4922018-09-05 09:53:49 +0200605 if (!rtc::tokenize_first(message, kSdpDelimiterColonChar, &leftpart, value)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000606 return ParseFailedGetValue(message, attribute, error);
607 }
608 // The left part should end with the expected attribute.
609 if (leftpart.length() < attribute.length() ||
610 leftpart.compare(leftpart.length() - attribute.length(),
611 attribute.length(), attribute) != 0) {
612 return ParseFailedGetValue(message, attribute, error);
613 }
614 return true;
615}
616
617static bool CaseInsensitiveFind(std::string str1, std::string str2) {
Yves Gerey665174f2018-06-19 15:03:05 +0200618 std::transform(str1.begin(), str1.end(), str1.begin(), ::tolower);
619 std::transform(str2.begin(), str2.end(), str2.begin(), ::tolower);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000620 return str1.find(str2) != std::string::npos;
621}
622
wu@webrtc.org5e760e72014-04-02 23:19:09 +0000623template <class T>
624static bool GetValueFromString(const std::string& line,
625 const std::string& s,
626 T* t,
627 SdpParseError* error) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000628 if (!rtc::FromString(s, t)) {
Jonas Olssonec9e4922018-09-05 09:53:49 +0200629 rtc::StringBuilder description;
wu@webrtc.org5e760e72014-04-02 23:19:09 +0000630 description << "Invalid value: " << s << ".";
631 return ParseFailed(line, description.str(), error);
632 }
633 return true;
634}
635
pkasting@chromium.orge9facf82015-02-17 20:36:28 +0000636static bool GetPayloadTypeFromString(const std::string& line,
637 const std::string& s,
638 int* payload_type,
639 SdpParseError* error) {
640 return GetValueFromString(line, s, payload_type, error) &&
Yves Gerey665174f2018-06-19 15:03:05 +0200641 cricket::IsValidRtpPayloadType(*payload_type);
pkasting@chromium.orge9facf82015-02-17 20:36:28 +0000642}
643
Seth Hampson5897a6e2018-04-03 11:16:33 -0700644// Creates a StreamParams track in the case when no SSRC lines are signaled.
645// This is a track that does not contain SSRCs and only contains
646// stream_ids/track_id if it's signaled with a=msid lines.
647void CreateTrackWithNoSsrcs(const std::vector<std::string>& msid_stream_ids,
648 const std::string& msid_track_id,
Amit Hilbuchc57d5732018-12-11 15:30:11 -0800649 const std::vector<RidDescription>& rids,
Seth Hampson5897a6e2018-04-03 11:16:33 -0700650 StreamParamsVec* tracks) {
651 StreamParams track;
652 if (msid_track_id.empty() || msid_stream_ids.empty()) {
653 // We only create an unsignaled track if a=msid lines were signaled.
654 return;
655 }
656 track.set_stream_ids(msid_stream_ids);
657 track.id = msid_track_id;
Amit Hilbuchc57d5732018-12-11 15:30:11 -0800658 track.set_rids(rids);
Seth Hampson5897a6e2018-04-03 11:16:33 -0700659 tracks->push_back(track);
660}
661
662// Creates the StreamParams tracks, for the case when SSRC lines are signaled.
663// |msid_stream_ids| and |msid_track_id| represent the stream/track ID from the
Taylor Brandstetter5de6b752016-03-09 17:02:30 -0800664// "a=msid" attribute, if it exists. They are empty if the attribute does not
Seth Hampson5897a6e2018-04-03 11:16:33 -0700665// exist. We prioritize getting stream_ids/track_ids signaled in a=msid lines.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000666void CreateTracksFromSsrcInfos(const SsrcInfoVec& ssrc_infos,
Seth Hampson5b4f0752018-04-02 16:31:36 -0700667 const std::vector<std::string>& msid_stream_ids,
Taylor Brandstetter5de6b752016-03-09 17:02:30 -0800668 const std::string& msid_track_id,
Seth Hampson5b4f0752018-04-02 16:31:36 -0700669 StreamParamsVec* tracks,
670 int msid_signaling) {
nisseede5da42017-01-12 05:15:36 -0800671 RTC_DCHECK(tracks != NULL);
Steve Anton4daf66e2018-09-07 14:55:53 -0700672 for (const SsrcInfo& ssrc_info : ssrc_infos) {
673 if (ssrc_info.cname.empty()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000674 continue;
675 }
676
Seth Hampson5b4f0752018-04-02 16:31:36 -0700677 std::vector<std::string> stream_ids;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000678 std::string track_id;
Seth Hampson5b4f0752018-04-02 16:31:36 -0700679 if (msid_signaling & cricket::kMsidSignalingMediaSection) {
680 // This is the case with Unified Plan SDP msid signaling.
681 stream_ids = msid_stream_ids;
Tomas Gunnarsson191bf5c2018-03-30 10:44:43 +0000682 track_id = msid_track_id;
Seth Hampson5b4f0752018-04-02 16:31:36 -0700683 } else if (msid_signaling & cricket::kMsidSignalingSsrcAttribute) {
684 // This is the case with Plan B SDP msid signaling.
Steve Anton4daf66e2018-09-07 14:55:53 -0700685 stream_ids.push_back(ssrc_info.stream_id);
686 track_id = ssrc_info.track_id;
687 } else if (!ssrc_info.mslabel.empty()) {
Seth Hampson5b4f0752018-04-02 16:31:36 -0700688 // Since there's no a=msid or a=ssrc msid signaling, this is a sdp from
689 // an older version of client that doesn't support msid.
690 // In that case, we use the mslabel and label to construct the track.
Steve Anton4daf66e2018-09-07 14:55:53 -0700691 stream_ids.push_back(ssrc_info.mslabel);
692 track_id = ssrc_info.label;
Seth Hampson5b4f0752018-04-02 16:31:36 -0700693 } else {
694 // Since no media streams isn't supported with older SDP signaling, we
695 // use a default a stream id.
696 stream_ids.push_back(kDefaultMsid);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000697 }
Seth Hampson5b4f0752018-04-02 16:31:36 -0700698 // If a track ID wasn't populated from the SSRC attributes OR the
Taylor Brandstetter5de6b752016-03-09 17:02:30 -0800699 // msid attribute, use default/random values.
Taylor Brandstetter5de6b752016-03-09 17:02:30 -0800700 if (track_id.empty()) {
701 // TODO(ronghuawu): What should we do if the track id doesn't appear?
702 // Create random string (which will be used as track label later)?
703 track_id = rtc::CreateRandomString(8);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000704 }
705
Steve Anton4daf66e2018-09-07 14:55:53 -0700706 auto track_it = std::find_if(
707 tracks->begin(), tracks->end(),
708 [track_id](const StreamParams& track) { return track.id == track_id; });
709 if (track_it == tracks->end()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000710 // If we don't find an existing track, create a new one.
711 tracks->push_back(StreamParams());
Steve Anton4daf66e2018-09-07 14:55:53 -0700712 track_it = tracks->end() - 1;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000713 }
Steve Anton4daf66e2018-09-07 14:55:53 -0700714 StreamParams& track = *track_it;
715 track.add_ssrc(ssrc_info.ssrc_id);
716 track.cname = ssrc_info.cname;
717 track.set_stream_ids(stream_ids);
718 track.id = track_id;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000719 }
720}
721
Seth Hampson845e8782018-03-02 11:34:10 -0800722void GetMediaStreamIds(const ContentInfo* content,
723 std::set<std::string>* labels) {
Steve Antonb1c1de12017-12-21 15:14:30 -0800724 for (const StreamParams& stream_params :
725 content->media_description()->streams()) {
Seth Hampson845e8782018-03-02 11:34:10 -0800726 for (const std::string& stream_id : stream_params.stream_ids()) {
727 labels->insert(stream_id);
Steve Anton5a26a3a2018-02-28 11:38:47 -0800728 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000729 }
730}
731
732// RFC 5245
733// It is RECOMMENDED that default candidates be chosen based on the
734// likelihood of those candidates to work with the peer that is being
735// contacted. It is RECOMMENDED that relayed > reflexive > host.
736static const int kPreferenceUnknown = 0;
737static const int kPreferenceHost = 1;
738static const int kPreferenceReflexive = 2;
739static const int kPreferenceRelayed = 3;
740
741static int GetCandidatePreferenceFromType(const std::string& type) {
742 int preference = kPreferenceUnknown;
743 if (type == cricket::LOCAL_PORT_TYPE) {
744 preference = kPreferenceHost;
745 } else if (type == cricket::STUN_PORT_TYPE) {
746 preference = kPreferenceReflexive;
747 } else if (type == cricket::RELAY_PORT_TYPE) {
748 preference = kPreferenceRelayed;
749 } else {
nissec80e7412017-01-11 05:56:46 -0800750 RTC_NOTREACHED();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000751 }
752 return preference;
753}
754
guoweis@webrtc.org57ac2c82015-02-06 00:45:13 +0000755// Get ip and port of the default destination from the |candidates| with the
756// given value of |component_id|. The default candidate should be the one most
757// likely to work, typically IPv4 relay.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000758// RFC 5245
759// The value of |component_id| currently supported are 1 (RTP) and 2 (RTCP).
Steve Anton36b29d12017-10-30 09:57:42 -0700760// TODO(deadbeef): Decide the default destination in webrtcsession and
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000761// pass it down via SessionDescription.
Yves Gerey665174f2018-06-19 15:03:05 +0200762static void GetDefaultDestination(const std::vector<Candidate>& candidates,
763 int component_id,
764 std::string* port,
765 std::string* ip,
766 std::string* addr_type) {
perkj@webrtc.orgd105cc82014-11-07 11:22:06 +0000767 *addr_type = kConnectionIpv4Addrtype;
pthatcher@webrtc.orgc9d6d142014-10-23 23:37:22 +0000768 *port = kDummyPort;
769 *ip = kDummyAddress;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000770 int current_preference = kPreferenceUnknown;
guoweis@webrtc.org57ac2c82015-02-06 00:45:13 +0000771 int current_family = AF_UNSPEC;
Steve Anton4daf66e2018-09-07 14:55:53 -0700772 for (const Candidate& candidate : candidates) {
773 if (candidate.component() != component_id) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000774 continue;
775 }
guoweis@webrtc.org57ac2c82015-02-06 00:45:13 +0000776 // Default destination should be UDP only.
Steve Anton4daf66e2018-09-07 14:55:53 -0700777 if (candidate.protocol() != cricket::UDP_PROTOCOL_NAME) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000778 continue;
779 }
Steve Anton4daf66e2018-09-07 14:55:53 -0700780 const int preference = GetCandidatePreferenceFromType(candidate.type());
781 const int family = candidate.address().ipaddr().family();
guoweis@webrtc.org57ac2c82015-02-06 00:45:13 +0000782 // See if this candidate is more preferable then the current one if it's the
783 // same family. Or if the current family is IPv4 already so we could safely
784 // ignore all IPv6 ones. WebRTC bug 4269.
785 // http://code.google.com/p/webrtc/issues/detail?id=4269
786 if ((preference <= current_preference && current_family == family) ||
787 (current_family == AF_INET && family == AF_INET6)) {
788 continue;
789 }
pthatcher@webrtc.orgc9d6d142014-10-23 23:37:22 +0000790 if (family == AF_INET) {
791 addr_type->assign(kConnectionIpv4Addrtype);
792 } else if (family == AF_INET6) {
793 addr_type->assign(kConnectionIpv6Addrtype);
794 }
guoweis@webrtc.org57ac2c82015-02-06 00:45:13 +0000795 current_preference = preference;
796 current_family = family;
Steve Anton4daf66e2018-09-07 14:55:53 -0700797 *port = candidate.address().PortAsString();
798 *ip = candidate.address().ipaddr().ToString();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000799 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000800}
801
wu@webrtc.org4c3e9912014-07-16 21:03:13 +0000802// Gets "a=rtcp" line if found default RTCP candidate from |candidates|.
803static std::string GetRtcpLine(const std::vector<Candidate>& candidates) {
pthatcher@webrtc.orgc9d6d142014-10-23 23:37:22 +0000804 std::string rtcp_line, rtcp_port, rtcp_ip, addr_type;
Yves Gerey665174f2018-06-19 15:03:05 +0200805 GetDefaultDestination(candidates, ICE_CANDIDATE_COMPONENT_RTCP, &rtcp_port,
806 &rtcp_ip, &addr_type);
pthatcher@webrtc.orgc9d6d142014-10-23 23:37:22 +0000807 // Found default RTCP candidate.
808 // RFC 5245
809 // If the agent is utilizing RTCP, it MUST encode the RTCP candidate
810 // using the a=rtcp attribute as defined in RFC 3605.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000811
pthatcher@webrtc.orgc9d6d142014-10-23 23:37:22 +0000812 // RFC 3605
813 // rtcp-attribute = "a=rtcp:" port [nettype space addrtype space
814 // connection-address] CRLF
Jonas Olssonec9e4922018-09-05 09:53:49 +0200815 rtc::StringBuilder os;
pthatcher@webrtc.orgc9d6d142014-10-23 23:37:22 +0000816 InitAttrLine(kAttributeRtcp, &os);
Yves Gerey665174f2018-06-19 15:03:05 +0200817 os << kSdpDelimiterColon << rtcp_port << " " << kConnectionNettype << " "
818 << addr_type << " " << rtcp_ip;
pthatcher@webrtc.orgc9d6d142014-10-23 23:37:22 +0000819 rtcp_line = os.str();
wu@webrtc.org4c3e9912014-07-16 21:03:13 +0000820 return rtcp_line;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000821}
822
823// Get candidates according to the mline index from SessionDescriptionInterface.
824static void GetCandidatesByMindex(const SessionDescriptionInterface& desci,
825 int mline_index,
826 std::vector<Candidate>* candidates) {
827 if (!candidates) {
828 return;
829 }
830 const IceCandidateCollection* cc = desci.candidates(mline_index);
831 for (size_t i = 0; i < cc->count(); ++i) {
832 const IceCandidateInterface* candidate = cc->at(i);
833 candidates->push_back(candidate->candidate());
834 }
835}
836
deadbeef90f1e1e2017-02-10 12:35:05 -0800837static bool IsValidPort(int port) {
838 return port >= 0 && port <= 65535;
839}
840
Steve Antone831b8c2018-02-01 12:22:16 -0800841std::string SdpSerialize(const JsepSessionDescription& jdesc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000842 const cricket::SessionDescription* desc = jdesc.description();
843 if (!desc) {
844 return "";
845 }
846
847 std::string message;
848
849 // Session Description.
850 AddLine(kSessionVersion, &message);
851 // Session Origin
852 // RFC 4566
853 // o=<username> <sess-id> <sess-version> <nettype> <addrtype>
854 // <unicast-address>
Jonas Olssonec9e4922018-09-05 09:53:49 +0200855 rtc::StringBuilder os;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000856 InitLine(kLineTypeOrigin, kSessionOriginUsername, &os);
Yves Gerey665174f2018-06-19 15:03:05 +0200857 const std::string& session_id =
858 jdesc.session_id().empty() ? kSessionOriginSessionId : jdesc.session_id();
859 const std::string& session_version = jdesc.session_version().empty()
860 ? kSessionOriginSessionVersion
861 : jdesc.session_version();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000862 os << " " << session_id << " " << session_version << " "
863 << kSessionOriginNettype << " " << kSessionOriginAddrtype << " "
864 << kSessionOriginAddress;
865 AddLine(os.str(), &message);
866 AddLine(kSessionName, &message);
867
868 // Time Description.
869 AddLine(kTimeDescription, &message);
870
871 // Group
872 if (desc->HasGroup(cricket::GROUP_TYPE_BUNDLE)) {
873 std::string group_line = kAttrGroup;
874 const cricket::ContentGroup* group =
875 desc->GetGroupByName(cricket::GROUP_TYPE_BUNDLE);
nisseede5da42017-01-12 05:15:36 -0800876 RTC_DCHECK(group != NULL);
Steve Anton4daf66e2018-09-07 14:55:53 -0700877 for (const std::string& content_name : group->content_names()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000878 group_line.append(" ");
Steve Anton4daf66e2018-09-07 14:55:53 -0700879 group_line.append(content_name);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000880 }
881 AddLine(group_line, &message);
882 }
883
Johannes Kron0854eb62018-10-10 22:33:20 +0200884 // Mixed one- and two-byte header extension.
Johannes Kron9581bc42018-10-23 10:17:39 +0200885 if (desc->extmap_allow_mixed()) {
Johannes Kron0854eb62018-10-10 22:33:20 +0200886 InitAttrLine(kAttributeExtmapAllowMixed, &os);
887 AddLine(os.str(), &message);
888 }
889
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000890 // MediaStream semantics
891 InitAttrLine(kAttributeMsidSemantics, &os);
892 os << kSdpDelimiterColon << " " << kMediaStreamSemantic;
wu@webrtc.org4c3e9912014-07-16 21:03:13 +0000893
Seth Hampson845e8782018-03-02 11:34:10 -0800894 std::set<std::string> media_stream_ids;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000895 const ContentInfo* audio_content = GetFirstAudioContent(desc);
896 if (audio_content)
Seth Hampson845e8782018-03-02 11:34:10 -0800897 GetMediaStreamIds(audio_content, &media_stream_ids);
wu@webrtc.org4c3e9912014-07-16 21:03:13 +0000898
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000899 const ContentInfo* video_content = GetFirstVideoContent(desc);
900 if (video_content)
Seth Hampson845e8782018-03-02 11:34:10 -0800901 GetMediaStreamIds(video_content, &media_stream_ids);
wu@webrtc.org4c3e9912014-07-16 21:03:13 +0000902
Steve Anton4daf66e2018-09-07 14:55:53 -0700903 for (const std::string& id : media_stream_ids) {
904 os << " " << id;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000905 }
906 AddLine(os.str(), &message);
907
Taylor Brandstetter2f65ec52018-05-24 11:37:28 -0700908 // a=ice-lite
909 //
910 // TODO(deadbeef): It's weird that we need to iterate TransportInfos for
911 // this, when it's a session-level attribute. It really should be moved to a
912 // session-level structure like SessionDescription.
913 for (const cricket::TransportInfo& transport : desc->transport_infos()) {
914 if (transport.description.ice_mode == cricket::ICEMODE_LITE) {
915 InitAttrLine(kAttributeIceLite, &os);
916 AddLine(os.str(), &message);
917 break;
918 }
919 }
920
wu@webrtc.org4c3e9912014-07-16 21:03:13 +0000921 // Preserve the order of the media contents.
922 int mline_index = -1;
Steve Anton4daf66e2018-09-07 14:55:53 -0700923 for (const ContentInfo& content : desc->contents()) {
wu@webrtc.org4c3e9912014-07-16 21:03:13 +0000924 std::vector<Candidate> candidates;
925 GetCandidatesByMindex(jdesc, ++mline_index, &candidates);
Steve Anton4daf66e2018-09-07 14:55:53 -0700926 BuildMediaDescription(&content, desc->GetTransportInfoByName(content.name),
927 content.media_description()->type(), candidates,
928 desc->msid_signaling(), &message);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000929 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000930 return message;
931}
932
933// Serializes the passed in IceCandidateInterface to a SDP string.
934// candidate - The candidate to be serialized.
Honghai Zhang7fb69db2016-03-14 11:59:18 -0700935std::string SdpSerializeCandidate(const IceCandidateInterface& candidate) {
936 return SdpSerializeCandidate(candidate.candidate());
937}
938
939// Serializes a cricket Candidate.
940std::string SdpSerializeCandidate(const cricket::Candidate& candidate) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000941 std::string message;
Honghai Zhang7fb69db2016-03-14 11:59:18 -0700942 std::vector<cricket::Candidate> candidates(1, candidate);
honghaiza54a0802015-12-16 18:37:23 -0800943 BuildCandidate(candidates, true, &message);
wu@webrtc.orgec9f5fb2014-06-24 17:05:10 +0000944 // From WebRTC draft section 4.8.1.1 candidate-attribute will be
945 // just candidate:<candidate> not a=candidate:<blah>CRLF
nisseede5da42017-01-12 05:15:36 -0800946 RTC_DCHECK(message.find("a=") == 0);
wu@webrtc.orgec9f5fb2014-06-24 17:05:10 +0000947 message.erase(0, 2);
nisseede5da42017-01-12 05:15:36 -0800948 RTC_DCHECK(message.find(kLineBreak) == message.size() - 2);
wu@webrtc.orgec9f5fb2014-06-24 17:05:10 +0000949 message.resize(message.size() - 2);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000950 return message;
951}
952
953bool SdpDeserialize(const std::string& message,
954 JsepSessionDescription* jdesc,
955 SdpParseError* error) {
956 std::string session_id;
957 std::string session_version;
Peter Thatcher7cbd1882015-09-17 18:54:52 -0700958 TransportDescription session_td("", "");
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000959 RtpHeaderExtensions session_extmaps;
zhihuang38989e52017-03-21 11:04:53 -0700960 rtc::SocketAddress session_connection_addr;
Steve Anton5a1de872018-12-18 11:25:55 -0800961 auto desc = absl::make_unique<cricket::SessionDescription>();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000962 size_t current_pos = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000963
964 // Session Description
965 if (!ParseSessionDescription(message, &current_pos, &session_id,
deadbeefc80741f2015-10-22 13:14:45 -0700966 &session_version, &session_td, &session_extmaps,
Steve Anton5a1de872018-12-18 11:25:55 -0800967 &session_connection_addr, desc.get(), error)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000968 return false;
969 }
970
971 // Media Description
Steve Anton5a1de872018-12-18 11:25:55 -0800972 std::vector<std::unique_ptr<JsepIceCandidate>> candidates;
deadbeefc80741f2015-10-22 13:14:45 -0700973 if (!ParseMediaDescription(message, session_td, session_extmaps, &current_pos,
Steve Anton5a1de872018-12-18 11:25:55 -0800974 session_connection_addr, desc.get(), &candidates,
zhihuang38989e52017-03-21 11:04:53 -0700975 error)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000976 return false;
977 }
978
Steve Anton5a1de872018-12-18 11:25:55 -0800979 jdesc->Initialize(desc.release(), session_id, session_version);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000980
Steve Anton5a1de872018-12-18 11:25:55 -0800981 for (const auto& candidate : candidates) {
982 jdesc->AddCandidate(candidate.get());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000983 }
984 return true;
985}
986
987bool SdpDeserializeCandidate(const std::string& message,
988 JsepIceCandidate* jcandidate,
989 SdpParseError* error) {
nisseede5da42017-01-12 05:15:36 -0800990 RTC_DCHECK(jcandidate != NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000991 Candidate candidate;
992 if (!ParseCandidate(message, &candidate, error, true)) {
993 return false;
994 }
995 jcandidate->SetCandidate(candidate);
996 return true;
997}
998
Honghai Zhang7fb69db2016-03-14 11:59:18 -0700999bool SdpDeserializeCandidate(const std::string& transport_name,
1000 const std::string& message,
1001 cricket::Candidate* candidate,
1002 SdpParseError* error) {
nisseede5da42017-01-12 05:15:36 -08001003 RTC_DCHECK(candidate != nullptr);
Honghai Zhang7fb69db2016-03-14 11:59:18 -07001004 if (!ParseCandidate(message, candidate, error, true)) {
1005 return false;
1006 }
1007 candidate->set_transport_name(transport_name);
1008 return true;
1009}
1010
Yves Gerey665174f2018-06-19 15:03:05 +02001011bool ParseCandidate(const std::string& message,
1012 Candidate* candidate,
1013 SdpParseError* error,
1014 bool is_raw) {
nisseede5da42017-01-12 05:15:36 -08001015 RTC_DCHECK(candidate != NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001016
1017 // Get the first line from |message|.
jiayl@webrtc.org7ec3f9f2014-08-08 23:09:15 +00001018 std::string first_line = message;
1019 size_t pos = 0;
1020 GetLine(message, &pos, &first_line);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001021
jiayl@webrtc.org7ec3f9f2014-08-08 23:09:15 +00001022 // Makes sure |message| contains only one line.
1023 if (message.size() > first_line.size()) {
1024 std::string left, right;
Jonas Olssonec9e4922018-09-05 09:53:49 +02001025 if (rtc::tokenize_first(message, kNewLineChar, &left, &right) &&
Donald Curtis0e07f922015-05-15 09:21:23 -07001026 !right.empty()) {
jiayl@webrtc.org7ec3f9f2014-08-08 23:09:15 +00001027 return ParseFailed(message, 0, "Expect one line only", error);
1028 }
1029 }
1030
1031 // From WebRTC draft section 4.8.1.1 candidate-attribute should be
1032 // candidate:<candidate> when trickled, but we still support
1033 // a=candidate:<blah>CRLF for backward compatibility and for parsing a line
1034 // from the SDP.
1035 if (IsLineType(first_line, kLineTypeAttributes)) {
1036 first_line = first_line.substr(kLinePrefixLength);
1037 }
1038
1039 std::string attribute_candidate;
1040 std::string candidate_value;
1041
1042 // |first_line| must be in the form of "candidate:<value>".
Jonas Olssonec9e4922018-09-05 09:53:49 +02001043 if (!rtc::tokenize_first(first_line, kSdpDelimiterColonChar,
1044 &attribute_candidate, &candidate_value) ||
jiayl@webrtc.org7ec3f9f2014-08-08 23:09:15 +00001045 attribute_candidate != kAttributeCandidate) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001046 if (is_raw) {
Jonas Olssonec9e4922018-09-05 09:53:49 +02001047 rtc::StringBuilder description;
Yves Gerey665174f2018-06-19 15:03:05 +02001048 description << "Expect line: " << kAttributeCandidate << ":"
1049 << "<candidate-str>";
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001050 return ParseFailed(first_line, 0, description.str(), error);
1051 } else {
1052 return ParseFailedExpectLine(first_line, 0, kLineTypeAttributes,
1053 kAttributeCandidate, error);
1054 }
1055 }
1056
1057 std::vector<std::string> fields;
Jonas Olssonec9e4922018-09-05 09:53:49 +02001058 rtc::split(candidate_value, kSdpDelimiterSpaceChar, &fields);
jiayl@webrtc.org7ec3f9f2014-08-08 23:09:15 +00001059
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001060 // RFC 5245
1061 // a=candidate:<foundation> <component-id> <transport> <priority>
1062 // <connection-address> <port> typ <candidate-types>
1063 // [raddr <connection-address>] [rport <port>]
1064 // *(SP extension-att-name SP extension-att-value)
1065 const size_t expected_min_fields = 8;
1066 if (fields.size() < expected_min_fields ||
1067 (fields[6] != kAttributeCandidateTyp)) {
1068 return ParseFailedExpectMinFieldNum(first_line, expected_min_fields, error);
1069 }
jbauch083b73f2015-07-16 02:46:32 -07001070 const std::string& foundation = fields[0];
jiayl@webrtc.org7ec3f9f2014-08-08 23:09:15 +00001071
wu@webrtc.org5e760e72014-04-02 23:19:09 +00001072 int component_id = 0;
1073 if (!GetValueFromString(first_line, fields[1], &component_id, error)) {
1074 return false;
1075 }
jbauch083b73f2015-07-16 02:46:32 -07001076 const std::string& transport = fields[2];
Peter Boström0c4e06b2015-10-07 12:23:21 +02001077 uint32_t priority = 0;
wu@webrtc.org5e760e72014-04-02 23:19:09 +00001078 if (!GetValueFromString(first_line, fields[3], &priority, error)) {
1079 return false;
1080 }
jbauch083b73f2015-07-16 02:46:32 -07001081 const std::string& connection_address = fields[4];
wu@webrtc.org5e760e72014-04-02 23:19:09 +00001082 int port = 0;
1083 if (!GetValueFromString(first_line, fields[5], &port, error)) {
1084 return false;
1085 }
deadbeef90f1e1e2017-02-10 12:35:05 -08001086 if (!IsValidPort(port)) {
1087 return ParseFailed(first_line, "Invalid port number.", error);
1088 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001089 SocketAddress address(connection_address, port);
1090
1091 cricket::ProtocolType protocol;
hnslb68cc752016-12-13 10:33:41 -08001092 if (!StringToProto(transport.c_str(), &protocol)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001093 return ParseFailed(first_line, "Unsupported transport type.", error);
1094 }
hnslb68cc752016-12-13 10:33:41 -08001095 switch (protocol) {
1096 case cricket::PROTO_UDP:
1097 case cricket::PROTO_TCP:
1098 case cricket::PROTO_SSLTCP:
1099 // Supported protocol.
1100 break;
1101 default:
1102 return ParseFailed(first_line, "Unsupported transport type.", error);
1103 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001104
1105 std::string candidate_type;
jbauch083b73f2015-07-16 02:46:32 -07001106 const std::string& type = fields[7];
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001107 if (type == kCandidateHost) {
1108 candidate_type = cricket::LOCAL_PORT_TYPE;
1109 } else if (type == kCandidateSrflx) {
1110 candidate_type = cricket::STUN_PORT_TYPE;
1111 } else if (type == kCandidateRelay) {
1112 candidate_type = cricket::RELAY_PORT_TYPE;
Honghai Zhang7fb69db2016-03-14 11:59:18 -07001113 } else if (type == kCandidatePrflx) {
1114 candidate_type = cricket::PRFLX_PORT_TYPE;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001115 } else {
1116 return ParseFailed(first_line, "Unsupported candidate type.", error);
1117 }
1118
1119 size_t current_position = expected_min_fields;
1120 SocketAddress related_address;
1121 // The 2 optional fields for related address
1122 // [raddr <connection-address>] [rport <port>]
1123 if (fields.size() >= (current_position + 2) &&
1124 fields[current_position] == kAttributeCandidateRaddr) {
1125 related_address.SetIP(fields[++current_position]);
1126 ++current_position;
1127 }
1128 if (fields.size() >= (current_position + 2) &&
1129 fields[current_position] == kAttributeCandidateRport) {
wu@webrtc.org5e760e72014-04-02 23:19:09 +00001130 int port = 0;
Yves Gerey665174f2018-06-19 15:03:05 +02001131 if (!GetValueFromString(first_line, fields[++current_position], &port,
1132 error)) {
wu@webrtc.org5e760e72014-04-02 23:19:09 +00001133 return false;
1134 }
deadbeef90f1e1e2017-02-10 12:35:05 -08001135 if (!IsValidPort(port)) {
1136 return ParseFailed(first_line, "Invalid port number.", error);
1137 }
wu@webrtc.org5e760e72014-04-02 23:19:09 +00001138 related_address.SetPort(port);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001139 ++current_position;
1140 }
1141
mallinath@webrtc.org2d60c5e2014-08-08 22:29:20 +00001142 // If this is a TCP candidate, it has additional extension as defined in
1143 // RFC 6544.
1144 std::string tcptype;
1145 if (fields.size() >= (current_position + 2) &&
1146 fields[current_position] == kTcpCandidateType) {
1147 tcptype = fields[++current_position];
1148 ++current_position;
1149
1150 if (tcptype != cricket::TCPTYPE_ACTIVE_STR &&
1151 tcptype != cricket::TCPTYPE_PASSIVE_STR &&
1152 tcptype != cricket::TCPTYPE_SIMOPEN_STR) {
1153 return ParseFailed(first_line, "Invalid TCP candidate type.", error);
1154 }
1155
1156 if (protocol != cricket::PROTO_TCP) {
1157 return ParseFailed(first_line, "Invalid non-TCP candidate", error);
1158 }
1159 }
1160
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001161 // Extension
honghaiza54a0802015-12-16 18:37:23 -08001162 // Though non-standard, we support the ICE ufrag and pwd being signaled on
1163 // the candidate to avoid issues with confusing which generation a candidate
1164 // belongs to when trickling multiple generations at the same time.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001165 std::string username;
1166 std::string password;
Peter Boström0c4e06b2015-10-07 12:23:21 +02001167 uint32_t generation = 0;
honghaiza0c44ea2016-03-23 16:07:48 -07001168 uint16_t network_id = 0;
1169 uint16_t network_cost = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001170 for (size_t i = current_position; i + 1 < fields.size(); ++i) {
1171 // RFC 5245
1172 // *(SP extension-att-name SP extension-att-value)
1173 if (fields[i] == kAttributeCandidateGeneration) {
wu@webrtc.org5e760e72014-04-02 23:19:09 +00001174 if (!GetValueFromString(first_line, fields[++i], &generation, error)) {
1175 return false;
1176 }
honghaiza54a0802015-12-16 18:37:23 -08001177 } else if (fields[i] == kAttributeCandidateUfrag) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001178 username = fields[++i];
honghaiza54a0802015-12-16 18:37:23 -08001179 } else if (fields[i] == kAttributeCandidatePwd) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001180 password = fields[++i];
honghaiza0c44ea2016-03-23 16:07:48 -07001181 } else if (fields[i] == kAttributeCandidateNetworkId) {
1182 if (!GetValueFromString(first_line, fields[++i], &network_id, error)) {
1183 return false;
1184 }
honghaize1a0c942016-02-16 14:54:56 -08001185 } else if (fields[i] == kAttributeCandidateNetworkCost) {
1186 if (!GetValueFromString(first_line, fields[++i], &network_cost, error)) {
1187 return false;
1188 }
Honghai Zhang351d77b2016-05-20 15:08:29 -07001189 network_cost = std::min(network_cost, rtc::kNetworkCostMax);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001190 } else {
1191 // Skip the unknown extension.
1192 ++i;
1193 }
1194 }
1195
guoweis@webrtc.org61c12472015-01-15 06:53:07 +00001196 *candidate = Candidate(component_id, cricket::ProtoToString(protocol),
guoweis@webrtc.org950c5182014-12-16 23:01:31 +00001197 address, priority, username, password, candidate_type,
honghaiza0c44ea2016-03-23 16:07:48 -07001198 generation, foundation, network_id, network_cost);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001199 candidate->set_related_address(related_address);
mallinath@webrtc.org2d60c5e2014-08-08 22:29:20 +00001200 candidate->set_tcptype(tcptype);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001201 return true;
1202}
1203
1204bool ParseIceOptions(const std::string& line,
1205 std::vector<std::string>* transport_options,
1206 SdpParseError* error) {
1207 std::string ice_options;
1208 if (!GetValue(line, kAttributeIceOption, &ice_options, error)) {
1209 return false;
1210 }
1211 std::vector<std::string> fields;
Jonas Olssonec9e4922018-09-05 09:53:49 +02001212 rtc::split(ice_options, kSdpDelimiterSpaceChar, &fields);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001213 for (size_t i = 0; i < fields.size(); ++i) {
1214 transport_options->push_back(fields[i]);
1215 }
1216 return true;
1217}
1218
jiayl@webrtc.orgddb85ab2014-09-05 16:31:56 +00001219bool ParseSctpPort(const std::string& line,
1220 int* sctp_port,
1221 SdpParseError* error) {
1222 // draft-ietf-mmusic-sctp-sdp-07
1223 // a=sctp-port
1224 std::vector<std::string> fields;
jiayl@webrtc.orgddb85ab2014-09-05 16:31:56 +00001225 const size_t expected_min_fields = 2;
Jonas Olssonec9e4922018-09-05 09:53:49 +02001226 rtc::split(line.substr(kLinePrefixLength), kSdpDelimiterColonChar, &fields);
lally69f57602015-10-08 10:15:04 -07001227 if (fields.size() < expected_min_fields) {
1228 fields.resize(0);
Jonas Olssonec9e4922018-09-05 09:53:49 +02001229 rtc::split(line.substr(kLinePrefixLength), kSdpDelimiterSpaceChar, &fields);
lally69f57602015-10-08 10:15:04 -07001230 }
jiayl@webrtc.orgddb85ab2014-09-05 16:31:56 +00001231 if (fields.size() < expected_min_fields) {
1232 return ParseFailedExpectMinFieldNum(line, expected_min_fields, error);
1233 }
1234 if (!rtc::FromString(fields[1], sctp_port)) {
lally69f57602015-10-08 10:15:04 -07001235 return ParseFailed(line, "Invalid sctp port value.", error);
jiayl@webrtc.orgddb85ab2014-09-05 16:31:56 +00001236 }
1237 return true;
1238}
1239
isheriff6f8d6862016-05-26 11:24:55 -07001240bool ParseExtmap(const std::string& line,
1241 RtpExtension* extmap,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001242 SdpParseError* error) {
1243 // RFC 5285
1244 // a=extmap:<value>["/"<direction>] <URI> <extensionattributes>
1245 std::vector<std::string> fields;
Jonas Olssonec9e4922018-09-05 09:53:49 +02001246 rtc::split(line.substr(kLinePrefixLength), kSdpDelimiterSpaceChar, &fields);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001247 const size_t expected_min_fields = 2;
1248 if (fields.size() < expected_min_fields) {
1249 return ParseFailedExpectMinFieldNum(line, expected_min_fields, error);
1250 }
1251 std::string uri = fields[1];
1252
1253 std::string value_direction;
1254 if (!GetValue(fields[0], kAttributeExtmap, &value_direction, error)) {
1255 return false;
1256 }
1257 std::vector<std::string> sub_fields;
Jonas Olssonec9e4922018-09-05 09:53:49 +02001258 rtc::split(value_direction, kSdpDelimiterSlashChar, &sub_fields);
wu@webrtc.org5e760e72014-04-02 23:19:09 +00001259 int value = 0;
1260 if (!GetValueFromString(line, sub_fields[0], &value, error)) {
1261 return false;
1262 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001263
jbauch5869f502017-06-29 12:31:36 -07001264 bool encrypted = false;
1265 if (uri == RtpExtension::kEncryptHeaderExtensionsUri) {
1266 // RFC 6904
Steve Anton36b29d12017-10-30 09:57:42 -07001267 // a=extmap:<value["/"<direction>] urn:ietf:params:rtp-hdrext:encrypt <URI>
1268 // <extensionattributes>
jbauch5869f502017-06-29 12:31:36 -07001269 const size_t expected_min_fields_encrypted = expected_min_fields + 1;
1270 if (fields.size() < expected_min_fields_encrypted) {
1271 return ParseFailedExpectMinFieldNum(line, expected_min_fields_encrypted,
Yves Gerey665174f2018-06-19 15:03:05 +02001272 error);
jbauch5869f502017-06-29 12:31:36 -07001273 }
1274
1275 encrypted = true;
1276 uri = fields[2];
1277 if (uri == RtpExtension::kEncryptHeaderExtensionsUri) {
1278 return ParseFailed(line, "Recursive encrypted header.", error);
1279 }
1280 }
1281
1282 *extmap = RtpExtension(uri, value, encrypted);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001283 return true;
1284}
1285
1286void BuildMediaDescription(const ContentInfo* content_info,
1287 const TransportInfo* transport_info,
Patrik Höglundb6b29e02018-06-21 16:58:01 +02001288 const cricket::MediaType media_type,
wu@webrtc.org4c3e9912014-07-16 21:03:13 +00001289 const std::vector<Candidate>& candidates,
Steve Antone831b8c2018-02-01 12:22:16 -08001290 int msid_signaling,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001291 std::string* message) {
nisseede5da42017-01-12 05:15:36 -08001292 RTC_DCHECK(message != NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001293 if (content_info == NULL || message == NULL) {
1294 return;
1295 }
Jonas Olssonec9e4922018-09-05 09:53:49 +02001296 rtc::StringBuilder os;
Steve Antonb1c1de12017-12-21 15:14:30 -08001297 const MediaContentDescription* media_desc = content_info->media_description();
1298 RTC_DCHECK(media_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001299
jiayl@webrtc.org9c16c392014-05-01 18:30:30 +00001300 int sctp_port = cricket::kSctpDefaultPort;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001301
1302 // RFC 4566
1303 // m=<media> <port> <proto> <fmt>
1304 // fmt is a list of payload type numbers that MAY be used in the session.
1305 const char* type = NULL;
1306 if (media_type == cricket::MEDIA_TYPE_AUDIO)
1307 type = kMediaTypeAudio;
1308 else if (media_type == cricket::MEDIA_TYPE_VIDEO)
1309 type = kMediaTypeVideo;
1310 else if (media_type == cricket::MEDIA_TYPE_DATA)
1311 type = kMediaTypeData;
1312 else
nissec80e7412017-01-11 05:56:46 -08001313 RTC_NOTREACHED();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001314
1315 std::string fmt;
1316 if (media_type == cricket::MEDIA_TYPE_VIDEO) {
Steve Antonb1c1de12017-12-21 15:14:30 -08001317 const VideoContentDescription* video_desc = media_desc->as_video();
Steve Anton4daf66e2018-09-07 14:55:53 -07001318 for (const cricket::VideoCodec& codec : video_desc->codecs()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001319 fmt.append(" ");
Steve Anton4daf66e2018-09-07 14:55:53 -07001320 fmt.append(rtc::ToString(codec.id));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001321 }
1322 } else if (media_type == cricket::MEDIA_TYPE_AUDIO) {
Steve Antonb1c1de12017-12-21 15:14:30 -08001323 const AudioContentDescription* audio_desc = media_desc->as_audio();
Steve Anton4daf66e2018-09-07 14:55:53 -07001324 for (const cricket::AudioCodec& codec : audio_desc->codecs()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001325 fmt.append(" ");
Steve Anton4daf66e2018-09-07 14:55:53 -07001326 fmt.append(rtc::ToString(codec.id));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001327 }
1328 } else if (media_type == cricket::MEDIA_TYPE_DATA) {
Steve Antonb1c1de12017-12-21 15:14:30 -08001329 const DataContentDescription* data_desc = media_desc->as_data();
pthatcher@webrtc.org3341b402015-02-13 21:14:22 +00001330 if (IsDtlsSctp(media_desc->protocol())) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001331 fmt.append(" ");
jiayl@webrtc.org9c16c392014-05-01 18:30:30 +00001332
zstein4b2e0822017-02-17 19:48:38 -08001333 if (data_desc->use_sctpmap()) {
Steve Anton4daf66e2018-09-07 14:55:53 -07001334 for (const cricket::DataCodec& codec : data_desc->codecs()) {
Niels Möller039743e2018-10-23 10:07:25 +02001335 if (absl::EqualsIgnoreCase(codec.name,
1336 cricket::kGoogleSctpDataCodecName) &&
Steve Anton4daf66e2018-09-07 14:55:53 -07001337 codec.GetParam(cricket::kCodecParamPort, &sctp_port)) {
zstein4b2e0822017-02-17 19:48:38 -08001338 break;
1339 }
jiayl@webrtc.org9c16c392014-05-01 18:30:30 +00001340 }
jiayl@webrtc.org9c16c392014-05-01 18:30:30 +00001341
Jonas Olsson6b1985d2018-07-05 11:59:48 +02001342 fmt.append(rtc::ToString(sctp_port));
zstein4b2e0822017-02-17 19:48:38 -08001343 } else {
1344 fmt.append(kDefaultSctpmapProtocol);
1345 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001346 } else {
Steve Anton4daf66e2018-09-07 14:55:53 -07001347 for (const cricket::DataCodec& codec : data_desc->codecs()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001348 fmt.append(" ");
Steve Anton4daf66e2018-09-07 14:55:53 -07001349 fmt.append(rtc::ToString(codec.id));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001350 }
1351 }
1352 }
1353 // The fmt must never be empty. If no codecs are found, set the fmt attribute
1354 // to 0.
1355 if (fmt.empty()) {
1356 fmt = " 0";
1357 }
1358
deadbeef25ed4352016-12-12 18:37:36 -08001359 // The port number in the m line will be updated later when associated with
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001360 // the candidates.
deadbeef25ed4352016-12-12 18:37:36 -08001361 //
1362 // A port value of 0 indicates that the m= section is rejected.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001363 // RFC 3264
1364 // To reject an offered stream, the port number in the corresponding stream in
1365 // the answer MUST be set to zero.
deadbeef25ed4352016-12-12 18:37:36 -08001366 //
1367 // However, the BUNDLE draft adds a new meaning to port zero, when used along
1368 // with a=bundle-only.
zhihuang38989e52017-03-21 11:04:53 -07001369 std::string port = kDummyPort;
1370 if (content_info->rejected || content_info->bundle_only) {
1371 port = kMediaPortRejected;
1372 } else if (!media_desc->connection_address().IsNil()) {
1373 port = rtc::ToString(media_desc->connection_address().port());
1374 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001375
Yves Gerey665174f2018-06-19 15:03:05 +02001376 rtc::SSLFingerprint* fp =
1377 (transport_info) ? transport_info->description.identity_fingerprint.get()
1378 : NULL;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001379
wu@webrtc.org4c3e9912014-07-16 21:03:13 +00001380 // Add the m and c lines.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001381 InitLine(kLineTypeMedia, type, &os);
1382 os << " " << port << " " << media_desc->protocol() << fmt;
zhihuang38989e52017-03-21 11:04:53 -07001383 AddLine(os.str(), message);
1384
1385 InitLine(kLineTypeConnection, kConnectionNettype, &os);
1386 if (media_desc->connection_address().IsNil()) {
1387 os << " " << kConnectionIpv4Addrtype << " " << kDummyAddress;
1388 } else if (media_desc->connection_address().family() == AF_INET) {
1389 os << " " << kConnectionIpv4Addrtype << " "
1390 << media_desc->connection_address().ipaddr().ToString();
1391 } else {
1392 os << " " << kConnectionIpv6Addrtype << " "
1393 << media_desc->connection_address().ipaddr().ToString();
1394 }
1395 AddLine(os.str(), message);
wu@webrtc.org4c3e9912014-07-16 21:03:13 +00001396
1397 // RFC 4566
1398 // b=AS:<bandwidth>
Peter Thatcherc0c3a862015-06-24 15:31:25 -07001399 if (media_desc->bandwidth() >= 1000) {
wu@webrtc.org4c3e9912014-07-16 21:03:13 +00001400 InitLine(kLineTypeSessionBandwidth, kApplicationSpecificMaximum, &os);
1401 os << kSdpDelimiterColon << (media_desc->bandwidth() / 1000);
1402 AddLine(os.str(), message);
1403 }
1404
deadbeef25ed4352016-12-12 18:37:36 -08001405 // Add the a=bundle-only line.
1406 if (content_info->bundle_only) {
1407 InitAttrLine(kAttributeBundleOnly, &os);
1408 AddLine(os.str(), message);
1409 }
1410
wu@webrtc.org4c3e9912014-07-16 21:03:13 +00001411 // Add the a=rtcp line.
pthatcher@webrtc.org3341b402015-02-13 21:14:22 +00001412 if (IsRtp(media_desc->protocol())) {
wu@webrtc.org4c3e9912014-07-16 21:03:13 +00001413 std::string rtcp_line = GetRtcpLine(candidates);
1414 if (!rtcp_line.empty()) {
1415 AddLine(rtcp_line, message);
1416 }
1417 }
1418
honghaiza54a0802015-12-16 18:37:23 -08001419 // Build the a=candidate lines. We don't include ufrag and pwd in the
1420 // candidates in the SDP to avoid redundancy.
1421 BuildCandidate(candidates, false, message);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001422
1423 // Use the transport_info to build the media level ice-ufrag and ice-pwd.
1424 if (transport_info) {
1425 // RFC 5245
1426 // ice-pwd-att = "ice-pwd" ":" password
1427 // ice-ufrag-att = "ice-ufrag" ":" ufrag
1428 // ice-ufrag
deadbeef3f7219b2015-12-28 15:17:14 -08001429 if (!transport_info->description.ice_ufrag.empty()) {
1430 InitAttrLine(kAttributeIceUfrag, &os);
1431 os << kSdpDelimiterColon << transport_info->description.ice_ufrag;
1432 AddLine(os.str(), message);
1433 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001434 // ice-pwd
deadbeef3f7219b2015-12-28 15:17:14 -08001435 if (!transport_info->description.ice_pwd.empty()) {
1436 InitAttrLine(kAttributeIcePwd, &os);
1437 os << kSdpDelimiterColon << transport_info->description.ice_pwd;
1438 AddLine(os.str(), message);
1439 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001440
1441 // draft-petithuguenin-mmusic-ice-attributes-level-03
1442 BuildIceOptions(transport_info->description.transport_options, message);
1443
1444 // RFC 4572
1445 // fingerprint-attribute =
1446 // "fingerprint" ":" hash-func SP fingerprint
1447 if (fp) {
1448 // Insert the fingerprint attribute.
1449 InitAttrLine(kAttributeFingerprint, &os);
Yves Gerey665174f2018-06-19 15:03:05 +02001450 os << kSdpDelimiterColon << fp->algorithm << kSdpDelimiterSpace
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001451 << fp->GetRfc4572Fingerprint();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001452 AddLine(os.str(), message);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001453
1454 // Inserting setup attribute.
1455 if (transport_info->description.connection_role !=
Yves Gerey665174f2018-06-19 15:03:05 +02001456 cricket::CONNECTIONROLE_NONE) {
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001457 // Making sure we are not using "passive" mode.
1458 cricket::ConnectionRole role =
1459 transport_info->description.connection_role;
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00001460 std::string dtls_role_str;
nissec16fa5e2017-02-07 07:18:43 -08001461 const bool success =
1462 cricket::ConnectionRoleToString(role, &dtls_role_str);
1463 RTC_DCHECK(success);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001464 InitAttrLine(kAttributeSetup, &os);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001465 os << kSdpDelimiterColon << dtls_role_str;
1466 AddLine(os.str(), message);
1467 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001468 }
1469 }
1470
1471 // RFC 3388
1472 // mid-attribute = "a=mid:" identification-tag
1473 // identification-tag = token
1474 // Use the content name as the mid identification-tag.
1475 InitAttrLine(kAttributeMid, &os);
1476 os << kSdpDelimiterColon << content_info->name;
1477 AddLine(os.str(), message);
1478
pthatcher@webrtc.org3341b402015-02-13 21:14:22 +00001479 if (IsDtlsSctp(media_desc->protocol())) {
Steve Antonb1c1de12017-12-21 15:14:30 -08001480 const DataContentDescription* data_desc = media_desc->as_data();
zstein4b2e0822017-02-17 19:48:38 -08001481 bool use_sctpmap = data_desc->use_sctpmap();
1482 BuildSctpContentAttributes(message, sctp_port, use_sctpmap);
pthatcher@webrtc.org3341b402015-02-13 21:14:22 +00001483 } else if (IsRtp(media_desc->protocol())) {
Steve Antone831b8c2018-02-01 12:22:16 -08001484 BuildRtpContentAttributes(media_desc, media_type, msid_signaling, message);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001485 }
1486}
1487
zstein4b2e0822017-02-17 19:48:38 -08001488void BuildSctpContentAttributes(std::string* message,
1489 int sctp_port,
1490 bool use_sctpmap) {
Jonas Olssonec9e4922018-09-05 09:53:49 +02001491 rtc::StringBuilder os;
zstein4b2e0822017-02-17 19:48:38 -08001492 if (use_sctpmap) {
1493 // draft-ietf-mmusic-sctp-sdp-04
1494 // a=sctpmap:sctpmap-number protocol [streams]
1495 InitAttrLine(kAttributeSctpmap, &os);
1496 os << kSdpDelimiterColon << sctp_port << kSdpDelimiterSpace
1497 << kDefaultSctpmapProtocol << kSdpDelimiterSpace
1498 << cricket::kMaxSctpStreams;
1499 } else {
1500 // draft-ietf-mmusic-sctp-sdp-23
1501 // a=sctp-port:<port>
1502 InitAttrLine(kAttributeSctpPort, &os);
1503 os << kSdpDelimiterColon << sctp_port;
1504 // TODO(zstein): emit max-message-size here
1505 }
wu@webrtc.org78187522013-10-07 23:32:02 +00001506 AddLine(os.str(), message);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001507}
1508
deadbeef9d3584c2016-02-16 17:54:10 -08001509void BuildRtpContentAttributes(const MediaContentDescription* media_desc,
Patrik Höglundb6b29e02018-06-21 16:58:01 +02001510 const cricket::MediaType media_type,
Steve Antone831b8c2018-02-01 12:22:16 -08001511 int msid_signaling,
deadbeef9d3584c2016-02-16 17:54:10 -08001512 std::string* message) {
Amit Hilbuchc57d5732018-12-11 15:30:11 -08001513 SdpSerializer serializer;
Jonas Olssonec9e4922018-09-05 09:53:49 +02001514 rtc::StringBuilder os;
Johannes Kron0854eb62018-10-10 22:33:20 +02001515 // RFC 8285
1516 // a=extmap-allow-mixed
1517 // The attribute MUST be either on session level or media level. We support
1518 // responding on both levels, however, we don't respond on media level if it's
1519 // set on session level.
Johannes Kron9581bc42018-10-23 10:17:39 +02001520 if (media_desc->extmap_allow_mixed_enum() ==
Johannes Kron0854eb62018-10-10 22:33:20 +02001521 MediaContentDescription::kMedia) {
1522 InitAttrLine(kAttributeExtmapAllowMixed, &os);
1523 AddLine(os.str(), message);
1524 }
1525 // RFC 8285
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001526 // a=extmap:<value>["/"<direction>] <URI> <extensionattributes>
1527 // The definitions MUST be either all session level or all media level. This
1528 // implementation uses all media level.
1529 for (size_t i = 0; i < media_desc->rtp_header_extensions().size(); ++i) {
jbauch5869f502017-06-29 12:31:36 -07001530 const RtpExtension& extension = media_desc->rtp_header_extensions()[i];
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001531 InitAttrLine(kAttributeExtmap, &os);
jbauch5869f502017-06-29 12:31:36 -07001532 os << kSdpDelimiterColon << extension.id;
1533 if (extension.encrypt) {
1534 os << kSdpDelimiterSpace << RtpExtension::kEncryptHeaderExtensionsUri;
1535 }
1536 os << kSdpDelimiterSpace << extension.uri;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001537 AddLine(os.str(), message);
1538 }
1539
1540 // RFC 3264
1541 // a=sendrecv || a=sendonly || a=sendrecv || a=inactive
deadbeefc80741f2015-10-22 13:14:45 -07001542 switch (media_desc->direction()) {
Steve Anton4e70a722017-11-28 14:57:10 -08001543 case RtpTransceiverDirection::kInactive:
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001544 InitAttrLine(kAttributeInactive, &os);
1545 break;
Steve Anton4e70a722017-11-28 14:57:10 -08001546 case RtpTransceiverDirection::kSendOnly:
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001547 InitAttrLine(kAttributeSendOnly, &os);
1548 break;
Steve Anton4e70a722017-11-28 14:57:10 -08001549 case RtpTransceiverDirection::kRecvOnly:
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001550 InitAttrLine(kAttributeRecvOnly, &os);
1551 break;
Steve Anton4e70a722017-11-28 14:57:10 -08001552 case RtpTransceiverDirection::kSendRecv:
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001553 default:
1554 InitAttrLine(kAttributeSendRecv, &os);
1555 break;
1556 }
1557 AddLine(os.str(), message);
1558
Seth Hampson5b4f0752018-04-02 16:31:36 -07001559 // Specified in https://datatracker.ietf.org/doc/draft-ietf-mmusic-msid/16/
1560 // a=msid:<msid-id> <msid-appdata>
1561 // The msid-id is a 1*64 token char representing the media stream id, and the
1562 // msid-appdata is a 1*64 token char representing the track id. There is a
1563 // line for every media stream, with a special msid-id value of "-"
1564 // representing no streams. The value of "msid-appdata" MUST be identical for
1565 // all lines.
Steve Antone831b8c2018-02-01 12:22:16 -08001566 if (msid_signaling & cricket::kMsidSignalingMediaSection) {
1567 const StreamParamsVec& streams = media_desc->streams();
1568 if (streams.size() == 1u) {
1569 const StreamParams& track = streams[0];
Seth Hampson5b4f0752018-04-02 16:31:36 -07001570 std::vector<std::string> stream_ids = track.stream_ids();
1571 if (stream_ids.empty()) {
1572 stream_ids.push_back(kNoStreamMsid);
1573 }
1574 for (const std::string& stream_id : stream_ids) {
1575 InitAttrLine(kAttributeMsid, &os);
1576 os << kSdpDelimiterColon << stream_id << kSdpDelimiterSpace << track.id;
1577 AddLine(os.str(), message);
1578 }
Steve Antone831b8c2018-02-01 12:22:16 -08001579 } else if (streams.size() > 1u) {
1580 RTC_LOG(LS_WARNING)
1581 << "Trying to serialize Unified Plan SDP with more than "
Jonas Olsson45cc8902018-02-13 10:37:07 +01001582 "one track in a media section. Omitting 'a=msid'.";
deadbeef9d3584c2016-02-16 17:54:10 -08001583 }
1584 }
1585
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001586 // RFC 5761
1587 // a=rtcp-mux
1588 if (media_desc->rtcp_mux()) {
1589 InitAttrLine(kAttributeRtcpMux, &os);
1590 AddLine(os.str(), message);
1591 }
1592
deadbeef13871492015-12-09 12:37:51 -08001593 // RFC 5506
1594 // a=rtcp-rsize
1595 if (media_desc->rtcp_reduced_size()) {
1596 InitAttrLine(kAttributeRtcpReducedSize, &os);
1597 AddLine(os.str(), message);
1598 }
1599
deadbeefd45aea82017-09-16 01:24:29 -07001600 if (media_desc->conference_mode()) {
1601 InitAttrLine(kAttributeXGoogleFlag, &os);
1602 os << kSdpDelimiterColon << kValueConference;
1603 AddLine(os.str(), message);
1604 }
1605
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001606 // RFC 4568
1607 // a=crypto:<tag> <crypto-suite> <key-params> [<session-params>]
Steve Anton4daf66e2018-09-07 14:55:53 -07001608 for (const CryptoParams& crypto_params : media_desc->cryptos()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001609 InitAttrLine(kAttributeCrypto, &os);
Steve Anton4daf66e2018-09-07 14:55:53 -07001610 os << kSdpDelimiterColon << crypto_params.tag << " "
1611 << crypto_params.cipher_suite << " " << crypto_params.key_params;
1612 if (!crypto_params.session_params.empty()) {
1613 os << " " << crypto_params.session_params;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001614 }
1615 AddLine(os.str(), message);
1616 }
1617
1618 // RFC 4566
1619 // a=rtpmap:<payload type> <encoding name>/<clock rate>
1620 // [/<encodingparameters>]
1621 BuildRtpMap(media_desc, media_type, message);
1622
Steve Anton4daf66e2018-09-07 14:55:53 -07001623 for (const StreamParams& track : media_desc->streams()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001624 // Build the ssrc-group lines.
Steve Anton4daf66e2018-09-07 14:55:53 -07001625 for (const SsrcGroup& ssrc_group : track.ssrc_groups) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001626 // RFC 5576
1627 // a=ssrc-group:<semantics> <ssrc-id> ...
Steve Anton4daf66e2018-09-07 14:55:53 -07001628 if (ssrc_group.ssrcs.empty()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001629 continue;
1630 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001631 InitAttrLine(kAttributeSsrcGroup, &os);
Steve Anton4daf66e2018-09-07 14:55:53 -07001632 os << kSdpDelimiterColon << ssrc_group.semantics;
1633 for (uint32_t ssrc : ssrc_group.ssrcs) {
1634 os << kSdpDelimiterSpace << rtc::ToString(ssrc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001635 }
1636 AddLine(os.str(), message);
1637 }
1638 // Build the ssrc lines for each ssrc.
Steve Anton4daf66e2018-09-07 14:55:53 -07001639 for (uint32_t ssrc : track.ssrcs) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001640 // RFC 5576
1641 // a=ssrc:<ssrc-id> cname:<value>
Steve Anton4daf66e2018-09-07 14:55:53 -07001642 AddSsrcLine(ssrc, kSsrcAttributeCname, track.cname, message);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001643
Steve Antone831b8c2018-02-01 12:22:16 -08001644 if (msid_signaling & cricket::kMsidSignalingSsrcAttribute) {
1645 // draft-alvestrand-mmusic-msid-00
1646 // a=ssrc:<ssrc-id> msid:identifier [appdata]
1647 // The appdata consists of the "id" attribute of a MediaStreamTrack,
1648 // which corresponds to the "id" attribute of StreamParams.
Seth Hampson5b4f0752018-04-02 16:31:36 -07001649 // Since a=ssrc msid signaling is used in Plan B SDP semantics, and
1650 // multiple stream ids are not supported for Plan B, we are only adding
1651 // a line for the first media stream id here.
Seth Hampson7fa6ee62018-10-17 10:25:28 -07001652 const std::string& track_stream_id = track.first_stream_id();
1653 // We use a special msid-id value of "-" to represent no streams,
1654 // for Unified Plan compatibility. Plan B will always have a
1655 // track_stream_id.
1656 const std::string& stream_id =
1657 track_stream_id.empty() ? kNoStreamMsid : track_stream_id;
Steve Antone831b8c2018-02-01 12:22:16 -08001658 InitAttrLine(kAttributeSsrc, &os);
1659 os << kSdpDelimiterColon << ssrc << kSdpDelimiterSpace
1660 << kSsrcAttributeMsid << kSdpDelimiterColon << stream_id
Steve Anton4daf66e2018-09-07 14:55:53 -07001661 << kSdpDelimiterSpace << track.id;
Steve Antone831b8c2018-02-01 12:22:16 -08001662 AddLine(os.str(), message);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001663
Steve Antone831b8c2018-02-01 12:22:16 -08001664 // TODO(ronghuawu): Remove below code which is for backward
1665 // compatibility.
1666 // draft-alvestrand-rtcweb-mid-01
1667 // a=ssrc:<ssrc-id> mslabel:<value>
1668 // The label isn't yet defined.
1669 // a=ssrc:<ssrc-id> label:<value>
Seth Hampson5b4f0752018-04-02 16:31:36 -07001670 AddSsrcLine(ssrc, kSsrcAttributeMslabel, stream_id, message);
Steve Anton4daf66e2018-09-07 14:55:53 -07001671 AddSsrcLine(ssrc, kSSrcAttributeLabel, track.id, message);
Steve Antone831b8c2018-02-01 12:22:16 -08001672 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001673 }
Amit Hilbuchc57d5732018-12-11 15:30:11 -08001674
1675 // Build the rid lines for each layer of the track
1676 for (const RidDescription& rid_description : track.rids()) {
1677 InitAttrLine(kAttributeRid, &os);
1678 os << kSdpDelimiterColon
1679 << serializer.SerializeRidDescription(rid_description);
1680 AddLine(os.str(), message);
1681 }
1682 }
1683
1684 if (media_desc->has_receive_stream()) {
1685 const auto& rids = media_desc->receive_stream().rids();
1686 for (const RidDescription& rid_description : rids) {
1687 InitAttrLine(kAttributeRid, &os);
1688 os << kSdpDelimiterColon
1689 << serializer.SerializeRidDescription(rid_description);
1690 AddLine(os.str(), message);
1691 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001692 }
Amit Hilbucha2012042018-12-03 11:35:05 -08001693
1694 // Simulcast (a=simulcast)
1695 // https://tools.ietf.org/html/draft-ietf-mmusic-sdp-simulcast-13#section-5.1
Amit Hilbuchc57d5732018-12-11 15:30:11 -08001696 if (media_desc->HasSimulcast()) {
1697 const auto& simulcast = media_desc->simulcast_description();
Amit Hilbucha2012042018-12-03 11:35:05 -08001698 InitAttrLine(kAttributeSimulcast, &os);
Amit Hilbucha2012042018-12-03 11:35:05 -08001699 os << kSdpDelimiterColon
1700 << serializer.SerializeSimulcastDescription(simulcast);
1701 AddLine(os.str(), message);
1702 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001703}
1704
Jonas Olssonec9e4922018-09-05 09:53:49 +02001705void WriteFmtpHeader(int payload_type, rtc::StringBuilder* os) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001706 // fmtp header: a=fmtp:|payload_type| <parameters>
1707 // Add a=fmtp
1708 InitAttrLine(kAttributeFmtp, os);
1709 // Add :|payload_type|
1710 *os << kSdpDelimiterColon << payload_type;
1711}
1712
Jonas Olssonec9e4922018-09-05 09:53:49 +02001713void WriteRtcpFbHeader(int payload_type, rtc::StringBuilder* os) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001714 // rtcp-fb header: a=rtcp-fb:|payload_type|
1715 // <parameters>/<ccm <ccm_parameters>>
1716 // Add a=rtcp-fb
1717 InitAttrLine(kAttributeRtcpFb, os);
1718 // Add :
1719 *os << kSdpDelimiterColon;
1720 if (payload_type == kWildcardPayloadType) {
1721 *os << "*";
1722 } else {
1723 *os << payload_type;
1724 }
1725}
1726
1727void WriteFmtpParameter(const std::string& parameter_name,
1728 const std::string& parameter_value,
Jonas Olssonec9e4922018-09-05 09:53:49 +02001729 rtc::StringBuilder* os) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001730 // fmtp parameters: |parameter_name|=|parameter_value|
1731 *os << parameter_name << kSdpDelimiterEqual << parameter_value;
1732}
1733
1734void WriteFmtpParameters(const cricket::CodecParameterMap& parameters,
Jonas Olssonec9e4922018-09-05 09:53:49 +02001735 rtc::StringBuilder* os) {
Steve Anton4daf66e2018-09-07 14:55:53 -07001736 bool first = true;
1737 for (const auto& entry : parameters) {
1738 const std::string& key = entry.first;
1739 const std::string& value = entry.second;
hta62a216e2016-04-15 11:02:14 -07001740 // Parameters are a semicolon-separated list, no spaces.
1741 // The list is separated from the header by a space.
Steve Anton4daf66e2018-09-07 14:55:53 -07001742 if (first) {
hta62a216e2016-04-15 11:02:14 -07001743 *os << kSdpDelimiterSpace;
Steve Anton4daf66e2018-09-07 14:55:53 -07001744 first = false;
hta62a216e2016-04-15 11:02:14 -07001745 } else {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001746 *os << kSdpDelimiterSemicolon;
1747 }
Steve Anton4daf66e2018-09-07 14:55:53 -07001748 WriteFmtpParameter(key, value, os);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001749 }
1750}
1751
1752bool IsFmtpParam(const std::string& name) {
ossuaa4b0772017-01-30 07:41:18 -08001753 // RFC 4855, section 3 specifies the mapping of media format parameters to SDP
1754 // parameters. Only ptime, maxptime, channels and rate are placed outside of
1755 // the fmtp line. In WebRTC, channels and rate are already handled separately
1756 // and thus not included in the CodecParameterMap.
1757 return name != kCodecParamPTime && name != kCodecParamMaxPTime;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001758}
1759
1760// Retreives fmtp parameters from |params|, which may contain other parameters
1761// as well, and puts them in |fmtp_parameters|.
1762void GetFmtpParams(const cricket::CodecParameterMap& params,
1763 cricket::CodecParameterMap* fmtp_parameters) {
Steve Anton4daf66e2018-09-07 14:55:53 -07001764 for (const auto& entry : params) {
1765 const std::string& key = entry.first;
1766 const std::string& value = entry.second;
1767 if (IsFmtpParam(key)) {
1768 (*fmtp_parameters)[key] = value;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001769 }
1770 }
1771}
1772
1773template <class T>
1774void AddFmtpLine(const T& codec, std::string* message) {
1775 cricket::CodecParameterMap fmtp_parameters;
1776 GetFmtpParams(codec.params, &fmtp_parameters);
1777 if (fmtp_parameters.empty()) {
1778 // No need to add an fmtp if it will have no (optional) parameters.
1779 return;
1780 }
Jonas Olssonec9e4922018-09-05 09:53:49 +02001781 rtc::StringBuilder os;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001782 WriteFmtpHeader(codec.id, &os);
1783 WriteFmtpParameters(fmtp_parameters, &os);
1784 AddLine(os.str(), message);
1785 return;
1786}
1787
1788template <class T>
1789void AddRtcpFbLines(const T& codec, std::string* message) {
Steve Anton4daf66e2018-09-07 14:55:53 -07001790 for (const cricket::FeedbackParam& param : codec.feedback_params.params()) {
Jonas Olssonec9e4922018-09-05 09:53:49 +02001791 rtc::StringBuilder os;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001792 WriteRtcpFbHeader(codec.id, &os);
Steve Anton4daf66e2018-09-07 14:55:53 -07001793 os << " " << param.id();
1794 if (!param.param().empty()) {
1795 os << " " << param.param();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001796 }
1797 AddLine(os.str(), message);
1798 }
1799}
1800
Yves Gerey665174f2018-06-19 15:03:05 +02001801bool AddSctpDataCodec(DataContentDescription* media_desc, int sctp_port) {
solenberg9fa49752016-10-08 13:02:44 -07001802 for (const auto& codec : media_desc->codecs()) {
Niels Möller039743e2018-10-23 10:07:25 +02001803 if (absl::EqualsIgnoreCase(codec.name, cricket::kGoogleSctpDataCodecName)) {
Yves Gerey665174f2018-06-19 15:03:05 +02001804 return ParseFailed("", "Can't have multiple sctp port attributes.", NULL);
solenberg9fa49752016-10-08 13:02:44 -07001805 }
jiayl@webrtc.orgddb85ab2014-09-05 16:31:56 +00001806 }
1807 // Add the SCTP Port number as a pseudo-codec "port" parameter
solenberg9fa49752016-10-08 13:02:44 -07001808 cricket::DataCodec codec_port(cricket::kGoogleSctpDataCodecPlType,
deadbeef67cf2c12016-04-13 10:07:16 -07001809 cricket::kGoogleSctpDataCodecName);
jiayl@webrtc.orgddb85ab2014-09-05 16:31:56 +00001810 codec_port.SetParam(cricket::kCodecParamPort, sctp_port);
Mirko Bonadei675513b2017-11-09 11:09:25 +01001811 RTC_LOG(INFO) << "AddSctpDataCodec: Got SCTP Port Number " << sctp_port;
jiayl@webrtc.orgddb85ab2014-09-05 16:31:56 +00001812 media_desc->AddCodec(codec_port);
1813 return true;
1814}
1815
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001816bool GetMinValue(const std::vector<int>& values, int* value) {
1817 if (values.empty()) {
1818 return false;
1819 }
1820 std::vector<int>::const_iterator found =
1821 std::min_element(values.begin(), values.end());
1822 *value = *found;
1823 return true;
1824}
1825
1826bool GetParameter(const std::string& name,
Yves Gerey665174f2018-06-19 15:03:05 +02001827 const cricket::CodecParameterMap& params,
1828 int* value) {
1829 std::map<std::string, std::string>::const_iterator found = params.find(name);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001830 if (found == params.end()) {
1831 return false;
1832 }
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001833 if (!rtc::FromString(found->second, value)) {
wu@webrtc.org5e760e72014-04-02 23:19:09 +00001834 return false;
1835 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001836 return true;
1837}
1838
1839void BuildRtpMap(const MediaContentDescription* media_desc,
Patrik Höglundb6b29e02018-06-21 16:58:01 +02001840 const cricket::MediaType media_type,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001841 std::string* message) {
nisseede5da42017-01-12 05:15:36 -08001842 RTC_DCHECK(message != NULL);
1843 RTC_DCHECK(media_desc != NULL);
Jonas Olssonec9e4922018-09-05 09:53:49 +02001844 rtc::StringBuilder os;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001845 if (media_type == cricket::MEDIA_TYPE_VIDEO) {
Steve Anton4daf66e2018-09-07 14:55:53 -07001846 for (const cricket::VideoCodec& codec : media_desc->as_video()->codecs()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001847 // RFC 4566
1848 // a=rtpmap:<payload type> <encoding name>/<clock rate>
1849 // [/<encodingparameters>]
Steve Anton4daf66e2018-09-07 14:55:53 -07001850 if (codec.id != kWildcardPayloadType) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001851 InitAttrLine(kAttributeRtpmap, &os);
Steve Anton4daf66e2018-09-07 14:55:53 -07001852 os << kSdpDelimiterColon << codec.id << " " << codec.name << "/"
deadbeefe814a0d2017-02-25 18:15:09 -08001853 << cricket::kVideoCodecClockrate;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001854 AddLine(os.str(), message);
1855 }
Steve Anton4daf66e2018-09-07 14:55:53 -07001856 AddRtcpFbLines(codec, message);
1857 AddFmtpLine(codec, message);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001858 }
1859 } else if (media_type == cricket::MEDIA_TYPE_AUDIO) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001860 std::vector<int> ptimes;
1861 std::vector<int> maxptimes;
1862 int max_minptime = 0;
Steve Anton4daf66e2018-09-07 14:55:53 -07001863 for (const cricket::AudioCodec& codec : media_desc->as_audio()->codecs()) {
1864 RTC_DCHECK(!codec.name.empty());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001865 // RFC 4566
1866 // a=rtpmap:<payload type> <encoding name>/<clock rate>
1867 // [/<encodingparameters>]
1868 InitAttrLine(kAttributeRtpmap, &os);
Steve Anton4daf66e2018-09-07 14:55:53 -07001869 os << kSdpDelimiterColon << codec.id << " ";
1870 os << codec.name << "/" << codec.clockrate;
1871 if (codec.channels != 1) {
1872 os << "/" << codec.channels;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001873 }
1874 AddLine(os.str(), message);
Steve Anton4daf66e2018-09-07 14:55:53 -07001875 AddRtcpFbLines(codec, message);
1876 AddFmtpLine(codec, message);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001877 int minptime = 0;
Steve Anton4daf66e2018-09-07 14:55:53 -07001878 if (GetParameter(kCodecParamMinPTime, codec.params, &minptime)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001879 max_minptime = std::max(minptime, max_minptime);
1880 }
1881 int ptime;
Steve Anton4daf66e2018-09-07 14:55:53 -07001882 if (GetParameter(kCodecParamPTime, codec.params, &ptime)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001883 ptimes.push_back(ptime);
1884 }
1885 int maxptime;
Steve Anton4daf66e2018-09-07 14:55:53 -07001886 if (GetParameter(kCodecParamMaxPTime, codec.params, &maxptime)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001887 maxptimes.push_back(maxptime);
1888 }
1889 }
1890 // Populate the maxptime attribute with the smallest maxptime of all codecs
1891 // under the same m-line.
1892 int min_maxptime = INT_MAX;
1893 if (GetMinValue(maxptimes, &min_maxptime)) {
1894 AddAttributeLine(kCodecParamMaxPTime, min_maxptime, message);
1895 }
nisseede5da42017-01-12 05:15:36 -08001896 RTC_DCHECK(min_maxptime > max_minptime);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001897 // Populate the ptime attribute with the smallest ptime or the largest
1898 // minptime, whichever is the largest, for all codecs under the same m-line.
1899 int ptime = INT_MAX;
1900 if (GetMinValue(ptimes, &ptime)) {
1901 ptime = std::min(ptime, min_maxptime);
1902 ptime = std::max(ptime, max_minptime);
1903 AddAttributeLine(kCodecParamPTime, ptime, message);
1904 }
1905 } else if (media_type == cricket::MEDIA_TYPE_DATA) {
Steve Anton4daf66e2018-09-07 14:55:53 -07001906 for (const cricket::DataCodec& codec : media_desc->as_data()->codecs()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001907 // RFC 4566
1908 // a=rtpmap:<payload type> <encoding name>/<clock rate>
1909 // [/<encodingparameters>]
1910 InitAttrLine(kAttributeRtpmap, &os);
Steve Anton4daf66e2018-09-07 14:55:53 -07001911 os << kSdpDelimiterColon << codec.id << " " << codec.name << "/"
1912 << codec.clockrate;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001913 AddLine(os.str(), message);
1914 }
1915 }
1916}
1917
1918void BuildCandidate(const std::vector<Candidate>& candidates,
honghaiza54a0802015-12-16 18:37:23 -08001919 bool include_ufrag,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001920 std::string* message) {
Jonas Olssonec9e4922018-09-05 09:53:49 +02001921 rtc::StringBuilder os;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001922
Steve Anton4daf66e2018-09-07 14:55:53 -07001923 for (const Candidate& candidate : candidates) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001924 // RFC 5245
1925 // a=candidate:<foundation> <component-id> <transport> <priority>
1926 // <connection-address> <port> typ <candidate-types>
1927 // [raddr <connection-address>] [rport <port>]
1928 // *(SP extension-att-name SP extension-att-value)
1929 std::string type;
1930 // Map the cricket candidate type to "host" / "srflx" / "prflx" / "relay"
Steve Anton4daf66e2018-09-07 14:55:53 -07001931 if (candidate.type() == cricket::LOCAL_PORT_TYPE) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001932 type = kCandidateHost;
Steve Anton4daf66e2018-09-07 14:55:53 -07001933 } else if (candidate.type() == cricket::STUN_PORT_TYPE) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001934 type = kCandidateSrflx;
Steve Anton4daf66e2018-09-07 14:55:53 -07001935 } else if (candidate.type() == cricket::RELAY_PORT_TYPE) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001936 type = kCandidateRelay;
Steve Anton4daf66e2018-09-07 14:55:53 -07001937 } else if (candidate.type() == cricket::PRFLX_PORT_TYPE) {
Honghai Zhang7fb69db2016-03-14 11:59:18 -07001938 type = kCandidatePrflx;
1939 // Peer reflexive candidate may be signaled for being removed.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001940 } else {
nissec80e7412017-01-11 05:56:46 -08001941 RTC_NOTREACHED();
Peter Thatcher019087f2015-04-28 09:06:26 -07001942 // Never write out candidates if we don't know the type.
1943 continue;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001944 }
1945
1946 InitAttrLine(kAttributeCandidate, &os);
Steve Anton4daf66e2018-09-07 14:55:53 -07001947 os << kSdpDelimiterColon << candidate.foundation() << " "
1948 << candidate.component() << " " << candidate.protocol() << " "
1949 << candidate.priority() << " "
1950 << (candidate.address().ipaddr().IsNil()
1951 ? candidate.address().hostname()
1952 : candidate.address().ipaddr().ToString())
1953 << " " << candidate.address().PortAsString() << " "
1954 << kAttributeCandidateTyp << " " << type << " ";
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001955
1956 // Related address
Steve Anton4daf66e2018-09-07 14:55:53 -07001957 if (!candidate.related_address().IsNil()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001958 os << kAttributeCandidateRaddr << " "
Steve Anton4daf66e2018-09-07 14:55:53 -07001959 << candidate.related_address().ipaddr().ToString() << " "
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001960 << kAttributeCandidateRport << " "
Steve Anton4daf66e2018-09-07 14:55:53 -07001961 << candidate.related_address().PortAsString() << " ";
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001962 }
1963
Steve Anton4daf66e2018-09-07 14:55:53 -07001964 if (candidate.protocol() == cricket::TCP_PROTOCOL_NAME) {
1965 os << kTcpCandidateType << " " << candidate.tcptype() << " ";
mallinath@webrtc.org2d60c5e2014-08-08 22:29:20 +00001966 }
1967
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001968 // Extensions
Steve Anton4daf66e2018-09-07 14:55:53 -07001969 os << kAttributeCandidateGeneration << " " << candidate.generation();
1970 if (include_ufrag && !candidate.username().empty()) {
1971 os << " " << kAttributeCandidateUfrag << " " << candidate.username();
honghaiza54a0802015-12-16 18:37:23 -08001972 }
Steve Anton4daf66e2018-09-07 14:55:53 -07001973 if (candidate.network_id() > 0) {
1974 os << " " << kAttributeCandidateNetworkId << " "
1975 << candidate.network_id();
honghaiza0c44ea2016-03-23 16:07:48 -07001976 }
Steve Anton4daf66e2018-09-07 14:55:53 -07001977 if (candidate.network_cost() > 0) {
1978 os << " " << kAttributeCandidateNetworkCost << " "
1979 << candidate.network_cost();
honghaize1a0c942016-02-16 14:54:56 -08001980 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001981
1982 AddLine(os.str(), message);
1983 }
1984}
1985
1986void BuildIceOptions(const std::vector<std::string>& transport_options,
1987 std::string* message) {
1988 if (!transport_options.empty()) {
Jonas Olssonec9e4922018-09-05 09:53:49 +02001989 rtc::StringBuilder os;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001990 InitAttrLine(kAttributeIceOption, &os);
1991 os << kSdpDelimiterColon << transport_options[0];
1992 for (size_t i = 1; i < transport_options.size(); ++i) {
1993 os << kSdpDelimiterSpace << transport_options[i];
1994 }
1995 AddLine(os.str(), message);
1996 }
1997}
1998
pthatcher@webrtc.org3341b402015-02-13 21:14:22 +00001999bool IsRtp(const std::string& protocol) {
2000 return protocol.empty() ||
Yves Gerey665174f2018-06-19 15:03:05 +02002001 (protocol.find(cricket::kMediaProtocolRtpPrefix) != std::string::npos);
pthatcher@webrtc.org3341b402015-02-13 21:14:22 +00002002}
2003
2004bool IsDtlsSctp(const std::string& protocol) {
2005 // This intentionally excludes "SCTP" and "SCTP/DTLS".
lally@webrtc.orga7470932015-02-24 20:19:21 +00002006 return protocol.find(cricket::kMediaProtocolDtlsSctp) != std::string::npos;
pthatcher@webrtc.org3341b402015-02-13 21:14:22 +00002007}
2008
zhihuang38989e52017-03-21 11:04:53 -07002009bool ParseConnectionData(const std::string& line,
2010 rtc::SocketAddress* addr,
2011 SdpParseError* error) {
2012 // Parse the line from left to right.
2013 std::string token;
2014 std::string rightpart;
2015 // RFC 4566
2016 // c=<nettype> <addrtype> <connection-address>
2017 // Skip the "c="
Jonas Olssonec9e4922018-09-05 09:53:49 +02002018 if (!rtc::tokenize_first(line, kSdpDelimiterEqualChar, &token, &rightpart)) {
zhihuang38989e52017-03-21 11:04:53 -07002019 return ParseFailed(line, "Failed to parse the network type.", error);
2020 }
2021
2022 // Extract and verify the <nettype>
Jonas Olssonec9e4922018-09-05 09:53:49 +02002023 if (!rtc::tokenize_first(rightpart, kSdpDelimiterSpaceChar, &token,
2024 &rightpart) ||
zhihuang38989e52017-03-21 11:04:53 -07002025 token != kConnectionNettype) {
2026 return ParseFailed(line,
2027 "Failed to parse the connection data. The network type "
2028 "is not currently supported.",
2029 error);
2030 }
2031
2032 // Extract the "<addrtype>" and "<connection-address>".
Jonas Olssonec9e4922018-09-05 09:53:49 +02002033 if (!rtc::tokenize_first(rightpart, kSdpDelimiterSpaceChar, &token,
2034 &rightpart)) {
zhihuang38989e52017-03-21 11:04:53 -07002035 return ParseFailed(line, "Failed to parse the address type.", error);
2036 }
2037
2038 // The rightpart part should be the IP address without the slash which is used
2039 // for multicast.
2040 if (rightpart.find('/') != std::string::npos) {
2041 return ParseFailed(line,
2042 "Failed to parse the connection data. Multicast is not "
2043 "currently supported.",
2044 error);
2045 }
2046 addr->SetIP(rightpart);
2047
2048 // Verify that the addrtype matches the type of the parsed address.
2049 if ((addr->family() == AF_INET && token != "IP4") ||
2050 (addr->family() == AF_INET6 && token != "IP6")) {
2051 addr->Clear();
2052 return ParseFailed(
2053 line,
2054 "Failed to parse the connection data. The address type is mismatching.",
2055 error);
2056 }
2057 return true;
2058}
2059
2060bool ParseSessionDescription(const std::string& message,
2061 size_t* pos,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002062 std::string* session_id,
2063 std::string* session_version,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002064 TransportDescription* session_td,
2065 RtpHeaderExtensions* session_extmaps,
zhihuang38989e52017-03-21 11:04:53 -07002066 rtc::SocketAddress* connection_addr,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002067 cricket::SessionDescription* desc,
2068 SdpParseError* error) {
2069 std::string line;
2070
deadbeefc80741f2015-10-22 13:14:45 -07002071 desc->set_msid_supported(false);
Johannes Kron9581bc42018-10-23 10:17:39 +02002072 desc->set_extmap_allow_mixed(false);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002073 // RFC 4566
2074 // v= (protocol version)
2075 if (!GetLineWithType(message, pos, &line, kLineTypeVersion)) {
Yves Gerey665174f2018-06-19 15:03:05 +02002076 return ParseFailedExpectLine(message, *pos, kLineTypeVersion, std::string(),
2077 error);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002078 }
2079 // RFC 4566
2080 // o=<username> <sess-id> <sess-version> <nettype> <addrtype>
2081 // <unicast-address>
2082 if (!GetLineWithType(message, pos, &line, kLineTypeOrigin)) {
Yves Gerey665174f2018-06-19 15:03:05 +02002083 return ParseFailedExpectLine(message, *pos, kLineTypeOrigin, std::string(),
2084 error);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002085 }
2086 std::vector<std::string> fields;
Jonas Olssonec9e4922018-09-05 09:53:49 +02002087 rtc::split(line.substr(kLinePrefixLength), kSdpDelimiterSpaceChar, &fields);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002088 const size_t expected_fields = 6;
2089 if (fields.size() != expected_fields) {
2090 return ParseFailedExpectFieldNum(line, expected_fields, error);
2091 }
2092 *session_id = fields[1];
2093 *session_version = fields[2];
2094
2095 // RFC 4566
2096 // s= (session name)
2097 if (!GetLineWithType(message, pos, &line, kLineTypeSessionName)) {
2098 return ParseFailedExpectLine(message, *pos, kLineTypeSessionName,
2099 std::string(), error);
2100 }
2101
Danil Chapovalov66cadcc2018-06-19 16:47:43 +02002102 // absl::optional lines
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002103 // Those are the optional lines, so shouldn't return false if not present.
2104 // RFC 4566
2105 // i=* (session information)
2106 GetLineWithType(message, pos, &line, kLineTypeSessionInfo);
2107
2108 // RFC 4566
2109 // u=* (URI of description)
2110 GetLineWithType(message, pos, &line, kLineTypeSessionUri);
2111
2112 // RFC 4566
2113 // e=* (email address)
2114 GetLineWithType(message, pos, &line, kLineTypeSessionEmail);
2115
2116 // RFC 4566
2117 // p=* (phone number)
2118 GetLineWithType(message, pos, &line, kLineTypeSessionPhone);
2119
2120 // RFC 4566
2121 // c=* (connection information -- not required if included in
2122 // all media)
zhihuang38989e52017-03-21 11:04:53 -07002123 if (GetLineWithType(message, pos, &line, kLineTypeConnection)) {
2124 if (!ParseConnectionData(line, connection_addr, error)) {
2125 return false;
2126 }
2127 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002128
2129 // RFC 4566
2130 // b=* (zero or more bandwidth information lines)
2131 while (GetLineWithType(message, pos, &line, kLineTypeSessionBandwidth)) {
2132 // By pass zero or more b lines.
2133 }
2134
2135 // RFC 4566
2136 // One or more time descriptions ("t=" and "r=" lines; see below)
2137 // t= (time the session is active)
2138 // r=* (zero or more repeat times)
2139 // Ensure there's at least one time description
2140 if (!GetLineWithType(message, pos, &line, kLineTypeTiming)) {
2141 return ParseFailedExpectLine(message, *pos, kLineTypeTiming, std::string(),
2142 error);
2143 }
2144
2145 while (GetLineWithType(message, pos, &line, kLineTypeRepeatTimes)) {
2146 // By pass zero or more r lines.
2147 }
2148
2149 // Go through the rest of the time descriptions
2150 while (GetLineWithType(message, pos, &line, kLineTypeTiming)) {
2151 while (GetLineWithType(message, pos, &line, kLineTypeRepeatTimes)) {
2152 // By pass zero or more r lines.
2153 }
2154 }
2155
2156 // RFC 4566
2157 // z=* (time zone adjustments)
2158 GetLineWithType(message, pos, &line, kLineTypeTimeZone);
2159
2160 // RFC 4566
2161 // k=* (encryption key)
2162 GetLineWithType(message, pos, &line, kLineTypeEncryptionKey);
2163
2164 // RFC 4566
2165 // a=* (zero or more session attribute lines)
2166 while (GetLineWithType(message, pos, &line, kLineTypeAttributes)) {
2167 if (HasAttribute(line, kAttributeGroup)) {
2168 if (!ParseGroupAttribute(line, desc, error)) {
2169 return false;
2170 }
2171 } else if (HasAttribute(line, kAttributeIceUfrag)) {
Yves Gerey665174f2018-06-19 15:03:05 +02002172 if (!GetValue(line, kAttributeIceUfrag, &(session_td->ice_ufrag),
2173 error)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002174 return false;
2175 }
2176 } else if (HasAttribute(line, kAttributeIcePwd)) {
2177 if (!GetValue(line, kAttributeIcePwd, &(session_td->ice_pwd), error)) {
2178 return false;
2179 }
2180 } else if (HasAttribute(line, kAttributeIceLite)) {
2181 session_td->ice_mode = cricket::ICEMODE_LITE;
2182 } else if (HasAttribute(line, kAttributeIceOption)) {
2183 if (!ParseIceOptions(line, &(session_td->transport_options), error)) {
2184 return false;
2185 }
2186 } else if (HasAttribute(line, kAttributeFingerprint)) {
2187 if (session_td->identity_fingerprint.get()) {
2188 return ParseFailed(
2189 line,
2190 "Can't have multiple fingerprint attributes at the same level.",
2191 error);
2192 }
Steve Anton4905edb2018-10-15 19:27:44 -07002193 std::unique_ptr<rtc::SSLFingerprint> fingerprint;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002194 if (!ParseFingerprintAttribute(line, &fingerprint, error)) {
2195 return false;
2196 }
Steve Anton4905edb2018-10-15 19:27:44 -07002197 session_td->identity_fingerprint = std::move(fingerprint);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00002198 } else if (HasAttribute(line, kAttributeSetup)) {
2199 if (!ParseDtlsSetup(line, &(session_td->connection_role), error)) {
2200 return false;
2201 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002202 } else if (HasAttribute(line, kAttributeMsidSemantics)) {
2203 std::string semantics;
2204 if (!GetValue(line, kAttributeMsidSemantics, &semantics, error)) {
2205 return false;
2206 }
deadbeefc80741f2015-10-22 13:14:45 -07002207 desc->set_msid_supported(
2208 CaseInsensitiveFind(semantics, kMediaStreamSemantic));
Johannes Kron0854eb62018-10-10 22:33:20 +02002209 } else if (HasAttribute(line, kAttributeExtmapAllowMixed)) {
Johannes Kron9581bc42018-10-23 10:17:39 +02002210 desc->set_extmap_allow_mixed(true);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002211 } else if (HasAttribute(line, kAttributeExtmap)) {
isheriff6f8d6862016-05-26 11:24:55 -07002212 RtpExtension extmap;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002213 if (!ParseExtmap(line, &extmap, error)) {
2214 return false;
2215 }
2216 session_extmaps->push_back(extmap);
2217 }
2218 }
2219
2220 return true;
2221}
2222
2223bool ParseGroupAttribute(const std::string& line,
2224 cricket::SessionDescription* desc,
2225 SdpParseError* error) {
nisseede5da42017-01-12 05:15:36 -08002226 RTC_DCHECK(desc != NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002227
2228 // RFC 5888 and draft-holmberg-mmusic-sdp-bundle-negotiation-00
2229 // a=group:BUNDLE video voice
2230 std::vector<std::string> fields;
Jonas Olssonec9e4922018-09-05 09:53:49 +02002231 rtc::split(line.substr(kLinePrefixLength), kSdpDelimiterSpaceChar, &fields);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002232 std::string semantics;
2233 if (!GetValue(fields[0], kAttributeGroup, &semantics, error)) {
2234 return false;
2235 }
2236 cricket::ContentGroup group(semantics);
2237 for (size_t i = 1; i < fields.size(); ++i) {
2238 group.AddContentName(fields[i]);
2239 }
2240 desc->AddGroup(group);
2241 return true;
2242}
2243
Steve Anton4905edb2018-10-15 19:27:44 -07002244static bool ParseFingerprintAttribute(
2245 const std::string& line,
2246 std::unique_ptr<rtc::SSLFingerprint>* fingerprint,
2247 SdpParseError* error) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002248 if (!IsLineType(line, kLineTypeAttributes) ||
2249 !HasAttribute(line, kAttributeFingerprint)) {
2250 return ParseFailedExpectLine(line, 0, kLineTypeAttributes,
2251 kAttributeFingerprint, error);
2252 }
2253
2254 std::vector<std::string> fields;
Jonas Olssonec9e4922018-09-05 09:53:49 +02002255 rtc::split(line.substr(kLinePrefixLength), kSdpDelimiterSpaceChar, &fields);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002256 const size_t expected_fields = 2;
2257 if (fields.size() != expected_fields) {
2258 return ParseFailedExpectFieldNum(line, expected_fields, error);
2259 }
2260
2261 // The first field here is "fingerprint:<hash>.
2262 std::string algorithm;
2263 if (!GetValue(fields[0], kAttributeFingerprint, &algorithm, error)) {
2264 return false;
2265 }
2266
2267 // Downcase the algorithm. Note that we don't need to downcase the
2268 // fingerprint because hex_decode can handle upper-case.
2269 std::transform(algorithm.begin(), algorithm.end(), algorithm.begin(),
2270 ::tolower);
2271
2272 // The second field is the digest value. De-hexify it.
Steve Anton4905edb2018-10-15 19:27:44 -07002273 *fingerprint =
2274 rtc::SSLFingerprint::CreateUniqueFromRfc4572(algorithm, fields[1]);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002275 if (!*fingerprint) {
Yves Gerey665174f2018-06-19 15:03:05 +02002276 return ParseFailed(line, "Failed to create fingerprint from the digest.",
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002277 error);
2278 }
2279
2280 return true;
2281}
2282
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00002283static bool ParseDtlsSetup(const std::string& line,
2284 cricket::ConnectionRole* role,
2285 SdpParseError* error) {
2286 // setup-attr = "a=setup:" role
2287 // role = "active" / "passive" / "actpass" / "holdconn"
2288 std::vector<std::string> fields;
Jonas Olssonec9e4922018-09-05 09:53:49 +02002289 rtc::split(line.substr(kLinePrefixLength), kSdpDelimiterColonChar, &fields);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00002290 const size_t expected_fields = 2;
2291 if (fields.size() != expected_fields) {
2292 return ParseFailedExpectFieldNum(line, expected_fields, error);
2293 }
2294 std::string role_str = fields[1];
2295 if (!cricket::StringToConnectionRole(role_str, role)) {
2296 return ParseFailed(line, "Invalid attribute value.", error);
2297 }
2298 return true;
2299}
2300
deadbeef9d3584c2016-02-16 17:54:10 -08002301static bool ParseMsidAttribute(const std::string& line,
Seth Hampson5b4f0752018-04-02 16:31:36 -07002302 std::vector<std::string>* stream_ids,
deadbeef9d3584c2016-02-16 17:54:10 -08002303 std::string* track_id,
2304 SdpParseError* error) {
Seth Hampson5b4f0752018-04-02 16:31:36 -07002305 // https://datatracker.ietf.org/doc/draft-ietf-mmusic-msid/16/
deadbeef9d3584c2016-02-16 17:54:10 -08002306 // a=msid:<stream id> <track id>
2307 // msid-value = msid-id [ SP msid-appdata ]
2308 // msid-id = 1*64token-char ; see RFC 4566
2309 // msid-appdata = 1*64token-char ; see RFC 4566
2310 std::string field1;
Seth Hampson5b4f0752018-04-02 16:31:36 -07002311 std::string new_stream_id;
2312 std::string new_track_id;
Jonas Olssonec9e4922018-09-05 09:53:49 +02002313 if (!rtc::tokenize_first(line.substr(kLinePrefixLength),
2314 kSdpDelimiterSpaceChar, &field1, &new_track_id)) {
deadbeef9d3584c2016-02-16 17:54:10 -08002315 const size_t expected_fields = 2;
2316 return ParseFailedExpectFieldNum(line, expected_fields, error);
2317 }
2318
Seth Hampson5b4f0752018-04-02 16:31:36 -07002319 if (new_track_id.empty()) {
deadbeefa4549d62017-02-10 17:26:22 -08002320 return ParseFailed(line, "Missing track ID in msid attribute.", error);
2321 }
Seth Hampson5b4f0752018-04-02 16:31:36 -07002322 // All track ids should be the same within an m section in a Unified Plan SDP.
2323 if (!track_id->empty() && new_track_id.compare(*track_id) != 0) {
2324 return ParseFailed(
2325 line, "Two different track IDs in msid attribute in one m= section",
2326 error);
2327 }
2328 *track_id = new_track_id;
deadbeefa4549d62017-02-10 17:26:22 -08002329
deadbeef9d3584c2016-02-16 17:54:10 -08002330 // msid:<msid-id>
Seth Hampson5b4f0752018-04-02 16:31:36 -07002331 if (!GetValue(field1, kAttributeMsid, &new_stream_id, error)) {
deadbeef9d3584c2016-02-16 17:54:10 -08002332 return false;
2333 }
Seth Hampson5b4f0752018-04-02 16:31:36 -07002334 if (new_stream_id.empty()) {
deadbeefa4549d62017-02-10 17:26:22 -08002335 return ParseFailed(line, "Missing stream ID in msid attribute.", error);
2336 }
Seth Hampson5b4f0752018-04-02 16:31:36 -07002337 // The special value "-" indicates "no MediaStream".
2338 if (new_stream_id.compare(kNoStreamMsid) != 0) {
2339 stream_ids->push_back(new_stream_id);
2340 }
deadbeef9d3584c2016-02-16 17:54:10 -08002341 return true;
2342}
2343
Amit Hilbuchc57d5732018-12-11 15:30:11 -08002344static void RemoveInvalidRidDescriptions(const std::vector<int>& payload_types,
2345 std::vector<RidDescription>* rids) {
2346 RTC_DCHECK(rids);
2347 std::set<std::string> to_remove;
2348 std::set<std::string> unique_rids;
2349
2350 // Check the rids to see which ones should be removed.
2351 for (RidDescription& rid : *rids) {
2352 // In the case of a duplicate, the entire "a=rid" line, and all "a=rid"
2353 // lines with rid-ids that duplicate this line, are discarded and MUST NOT
2354 // be included in the SDP Answer.
2355 auto pair = unique_rids.insert(rid.rid);
2356 // Insert will "fail" if element already exists.
2357 if (!pair.second) {
2358 to_remove.insert(rid.rid);
2359 continue;
2360 }
2361
2362 // If the "a=rid" line contains a "pt=", the list of payload types
2363 // is verified against the list of valid payload types for the media
2364 // section (that is, those listed on the "m=" line). Any PT missing
2365 // from the "m=" line is discarded from the set of values in the
2366 // "pt=". If no values are left in the "pt=" parameter after this
2367 // processing, then the "a=rid" line is discarded.
2368 if (rid.payload_types.empty()) {
2369 // If formats were not specified, rid should not be removed.
2370 continue;
2371 }
2372
2373 // Note: Spec does not mention how to handle duplicate formats.
2374 // Media section does not handle duplicates either.
2375 std::set<int> removed_formats;
2376 for (int payload_type : rid.payload_types) {
2377 if (std::find(payload_types.begin(), payload_types.end(), payload_type) ==
2378 payload_types.end()) {
2379 removed_formats.insert(payload_type);
2380 }
2381 }
2382
2383 rid.payload_types.erase(
2384 std::remove_if(rid.payload_types.begin(), rid.payload_types.end(),
2385 [&removed_formats](int format) {
2386 return removed_formats.count(format) > 0;
2387 }),
2388 rid.payload_types.end());
2389
2390 // If all formats were removed then remove the rid alogether.
2391 if (rid.payload_types.empty()) {
2392 to_remove.insert(rid.rid);
2393 }
2394 }
2395
2396 // Remove every rid description that appears in the to_remove list.
2397 if (!to_remove.empty()) {
2398 rids->erase(std::remove_if(rids->begin(), rids->end(),
2399 [&to_remove](const RidDescription& rid) {
2400 return to_remove.count(rid.rid) > 0;
2401 }),
2402 rids->end());
2403 }
2404}
2405
2406// Create a new list (because SimulcastLayerList is immutable) without any
2407// layers that have a rid in the to_remove list.
2408// If a group of alternatives is empty after removing layers, the group should
2409// be removed altogether.
2410static SimulcastLayerList RemoveRidsFromSimulcastLayerList(
2411 const std::set<std::string>& to_remove,
2412 const SimulcastLayerList& layers) {
2413 SimulcastLayerList result;
2414 for (const std::vector<SimulcastLayer>& vector : layers) {
2415 std::vector<SimulcastLayer> new_layers;
2416 for (const SimulcastLayer& layer : vector) {
2417 if (to_remove.find(layer.rid) == to_remove.end()) {
2418 new_layers.push_back(layer);
2419 }
2420 }
2421 // If all layers were removed, do not add an entry.
2422 if (!new_layers.empty()) {
2423 result.AddLayerWithAlternatives(new_layers);
2424 }
2425 }
2426
2427 return result;
2428}
2429
2430// Will remove Simulcast Layers if:
2431// 1. They appear in both send and receive directions.
2432// 2. They do not appear in the list of |valid_rids|.
2433static void RemoveInvalidRidsFromSimulcast(
2434 const std::vector<RidDescription>& valid_rids,
2435 SimulcastDescription* simulcast) {
2436 RTC_DCHECK(simulcast);
2437 std::set<std::string> to_remove;
2438 std::vector<SimulcastLayer> all_send_layers =
2439 simulcast->send_layers().GetAllLayers();
2440 std::vector<SimulcastLayer> all_receive_layers =
2441 simulcast->receive_layers().GetAllLayers();
2442
2443 // If a rid appears in both send and receive directions, remove it from both.
2444 // This algorithm runs in O(n^2) time, but for small n (as is the case with
2445 // simulcast layers) it should still perform well.
2446 for (const SimulcastLayer& send_layer : all_send_layers) {
2447 if (std::find_if(all_receive_layers.begin(), all_receive_layers.end(),
2448 [&send_layer](const SimulcastLayer& layer) {
2449 return layer.rid == send_layer.rid;
2450 }) != all_receive_layers.end()) {
2451 to_remove.insert(send_layer.rid);
2452 }
2453 }
2454
2455 // Add any rid that is not in the valid list to the remove set.
2456 for (const SimulcastLayer& send_layer : all_send_layers) {
2457 if (std::find_if(valid_rids.begin(), valid_rids.end(),
2458 [&send_layer](const RidDescription& rid) {
2459 return send_layer.rid == rid.rid;
2460 }) == valid_rids.end()) {
2461 to_remove.insert(send_layer.rid);
2462 }
2463 }
2464
2465 // Add any rid that is not in the valid list to the remove set.
2466 for (const SimulcastLayer& receive_layer : all_receive_layers) {
2467 if (std::find_if(valid_rids.begin(), valid_rids.end(),
2468 [&receive_layer](const RidDescription& rid) {
2469 return receive_layer.rid == rid.rid;
2470 }) == valid_rids.end()) {
2471 to_remove.insert(receive_layer.rid);
2472 }
2473 }
2474
2475 simulcast->send_layers() =
2476 RemoveRidsFromSimulcastLayerList(to_remove, simulcast->send_layers());
2477 simulcast->receive_layers() =
2478 RemoveRidsFromSimulcastLayerList(to_remove, simulcast->receive_layers());
2479}
2480
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002481// RFC 3551
2482// PT encoding media type clock rate channels
2483// name (Hz)
2484// 0 PCMU A 8,000 1
2485// 1 reserved A
2486// 2 reserved A
2487// 3 GSM A 8,000 1
2488// 4 G723 A 8,000 1
2489// 5 DVI4 A 8,000 1
2490// 6 DVI4 A 16,000 1
2491// 7 LPC A 8,000 1
2492// 8 PCMA A 8,000 1
2493// 9 G722 A 8,000 1
2494// 10 L16 A 44,100 2
2495// 11 L16 A 44,100 1
2496// 12 QCELP A 8,000 1
2497// 13 CN A 8,000 1
2498// 14 MPA A 90,000 (see text)
2499// 15 G728 A 8,000 1
2500// 16 DVI4 A 11,025 1
2501// 17 DVI4 A 22,050 1
2502// 18 G729 A 8,000 1
2503struct StaticPayloadAudioCodec {
2504 const char* name;
2505 int clockrate;
Peter Kasting69558702016-01-12 16:26:35 -08002506 size_t channels;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002507};
2508static const StaticPayloadAudioCodec kStaticPayloadAudioCodecs[] = {
Yves Gerey665174f2018-06-19 15:03:05 +02002509 {"PCMU", 8000, 1}, {"reserved", 0, 0}, {"reserved", 0, 0},
2510 {"GSM", 8000, 1}, {"G723", 8000, 1}, {"DVI4", 8000, 1},
2511 {"DVI4", 16000, 1}, {"LPC", 8000, 1}, {"PCMA", 8000, 1},
2512 {"G722", 8000, 1}, {"L16", 44100, 2}, {"L16", 44100, 1},
2513 {"QCELP", 8000, 1}, {"CN", 8000, 1}, {"MPA", 90000, 1},
2514 {"G728", 8000, 1}, {"DVI4", 11025, 1}, {"DVI4", 22050, 1},
2515 {"G729", 8000, 1},
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002516};
2517
Yves Gerey665174f2018-06-19 15:03:05 +02002518void MaybeCreateStaticPayloadAudioCodecs(const std::vector<int>& fmts,
2519 AudioContentDescription* media_desc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002520 if (!media_desc) {
2521 return;
2522 }
deadbeef67cf2c12016-04-13 10:07:16 -07002523 RTC_DCHECK(media_desc->codecs().empty());
solenberg9fa49752016-10-08 13:02:44 -07002524 for (int payload_type : fmts) {
Yves Gerey665174f2018-06-19 15:03:05 +02002525 if (!media_desc->HasCodec(payload_type) && payload_type >= 0 &&
kjellander3e33bfe2016-06-20 07:04:09 -07002526 static_cast<uint32_t>(payload_type) <
2527 arraysize(kStaticPayloadAudioCodecs)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002528 std::string encoding_name = kStaticPayloadAudioCodecs[payload_type].name;
2529 int clock_rate = kStaticPayloadAudioCodecs[payload_type].clockrate;
Peter Kasting69558702016-01-12 16:26:35 -08002530 size_t channels = kStaticPayloadAudioCodecs[payload_type].channels;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002531 media_desc->AddCodec(cricket::AudioCodec(payload_type, encoding_name,
deadbeef67cf2c12016-04-13 10:07:16 -07002532 clock_rate, 0, channels));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002533 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002534 }
2535}
2536
2537template <class C>
Steve Anton5a1de872018-12-18 11:25:55 -08002538static std::unique_ptr<C> ParseContentDescription(
2539 const std::string& message,
2540 const cricket::MediaType media_type,
2541 int mline_index,
2542 const std::string& protocol,
2543 const std::vector<int>& payload_types,
2544 size_t* pos,
2545 std::string* content_name,
2546 bool* bundle_only,
2547 int* msid_signaling,
2548 TransportDescription* transport,
2549 std::vector<std::unique_ptr<JsepIceCandidate>>* candidates,
2550 webrtc::SdpParseError* error) {
2551 auto media_desc = absl::make_unique<C>();
deadbeef67cf2c12016-04-13 10:07:16 -07002552 if (!ParseContent(message, media_type, mline_index, protocol, payload_types,
Steve Anton5a1de872018-12-18 11:25:55 -08002553 pos, content_name, bundle_only, msid_signaling,
2554 media_desc.get(), transport, candidates, error)) {
zhihuang38989e52017-03-21 11:04:53 -07002555 return nullptr;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002556 }
2557 // Sort the codecs according to the m-line fmt list.
deadbeef67cf2c12016-04-13 10:07:16 -07002558 std::unordered_map<int, int> payload_type_preferences;
2559 // "size + 1" so that the lowest preference payload type has a preference of
2560 // 1, which is greater than the default (0) for payload types not in the fmt
2561 // list.
2562 int preference = static_cast<int>(payload_types.size() + 1);
2563 for (int pt : payload_types) {
2564 payload_type_preferences[pt] = preference--;
2565 }
2566 std::vector<typename C::CodecType> codecs = media_desc->codecs();
Yves Gerey665174f2018-06-19 15:03:05 +02002567 std::sort(codecs.begin(), codecs.end(),
2568 [&payload_type_preferences](const typename C::CodecType& a,
2569 const typename C::CodecType& b) {
2570 return payload_type_preferences[a.id] >
2571 payload_type_preferences[b.id];
2572 });
deadbeef67cf2c12016-04-13 10:07:16 -07002573 media_desc->set_codecs(codecs);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002574 return media_desc;
2575}
2576
Steve Anton5a1de872018-12-18 11:25:55 -08002577bool ParseMediaDescription(
2578 const std::string& message,
2579 const TransportDescription& session_td,
2580 const RtpHeaderExtensions& session_extmaps,
2581 size_t* pos,
2582 const rtc::SocketAddress& session_connection_addr,
2583 cricket::SessionDescription* desc,
2584 std::vector<std::unique_ptr<JsepIceCandidate>>* candidates,
2585 SdpParseError* error) {
nisseede5da42017-01-12 05:15:36 -08002586 RTC_DCHECK(desc != NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002587 std::string line;
2588 int mline_index = -1;
Steve Antone831b8c2018-02-01 12:22:16 -08002589 int msid_signaling = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002590
2591 // Zero or more media descriptions
2592 // RFC 4566
2593 // m=<media> <port> <proto> <fmt>
2594 while (GetLineWithType(message, pos, &line, kLineTypeMedia)) {
2595 ++mline_index;
2596
2597 std::vector<std::string> fields;
Jonas Olssonec9e4922018-09-05 09:53:49 +02002598 rtc::split(line.substr(kLinePrefixLength), kSdpDelimiterSpaceChar, &fields);
zstein4b2e0822017-02-17 19:48:38 -08002599
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002600 const size_t expected_min_fields = 4;
2601 if (fields.size() < expected_min_fields) {
2602 return ParseFailedExpectMinFieldNum(line, expected_min_fields, error);
2603 }
deadbeef25ed4352016-12-12 18:37:36 -08002604 bool port_rejected = false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002605 // RFC 3264
2606 // To reject an offered stream, the port number in the corresponding stream
2607 // in the answer MUST be set to zero.
2608 if (fields[1] == kMediaPortRejected) {
deadbeef25ed4352016-12-12 18:37:36 -08002609 port_rejected = true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002610 }
2611
zhihuang38989e52017-03-21 11:04:53 -07002612 int port = 0;
2613 if (!rtc::FromString<int>(fields[1], &port) || !IsValidPort(port)) {
2614 return ParseFailed(line, "The port number is invalid", error);
2615 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002616 std::string protocol = fields[2];
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002617
2618 // <fmt>
deadbeef67cf2c12016-04-13 10:07:16 -07002619 std::vector<int> payload_types;
pthatcher@webrtc.org3341b402015-02-13 21:14:22 +00002620 if (IsRtp(protocol)) {
Yves Gerey665174f2018-06-19 15:03:05 +02002621 for (size_t j = 3; j < fields.size(); ++j) {
jiayl@webrtc.orgddb85ab2014-09-05 16:31:56 +00002622 // TODO(wu): Remove when below bug is fixed.
2623 // https://bugzilla.mozilla.org/show_bug.cgi?id=996329
pbosbb36fdf2015-07-09 07:48:14 -07002624 if (fields[j].empty() && j == fields.size() - 1) {
jiayl@webrtc.orgddb85ab2014-09-05 16:31:56 +00002625 continue;
2626 }
wu@webrtc.org36eda7c2014-04-15 20:37:30 +00002627
jiayl@webrtc.orgddb85ab2014-09-05 16:31:56 +00002628 int pl = 0;
pkasting@chromium.orge9facf82015-02-17 20:36:28 +00002629 if (!GetPayloadTypeFromString(line, fields[j], &pl, error)) {
jiayl@webrtc.orgddb85ab2014-09-05 16:31:56 +00002630 return false;
2631 }
deadbeef67cf2c12016-04-13 10:07:16 -07002632 payload_types.push_back(pl);
wu@webrtc.org5e760e72014-04-02 23:19:09 +00002633 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002634 }
2635
2636 // Make a temporary TransportDescription based on |session_td|.
2637 // Some of this gets overwritten by ParseContent.
deadbeef46eed762016-01-28 13:24:37 -08002638 TransportDescription transport(
2639 session_td.transport_options, session_td.ice_ufrag, session_td.ice_pwd,
2640 session_td.ice_mode, session_td.connection_role,
2641 session_td.identity_fingerprint.get());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002642
kwibergd1fe2812016-04-27 06:47:29 -07002643 std::unique_ptr<MediaContentDescription> content;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002644 std::string content_name;
deadbeef25ed4352016-12-12 18:37:36 -08002645 bool bundle_only = false;
Steve Antone831b8c2018-02-01 12:22:16 -08002646 int section_msid_signaling = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002647 if (HasAttribute(line, kMediaTypeVideo)) {
Steve Anton5a1de872018-12-18 11:25:55 -08002648 content = ParseContentDescription<VideoContentDescription>(
deadbeef67cf2c12016-04-13 10:07:16 -07002649 message, cricket::MEDIA_TYPE_VIDEO, mline_index, protocol,
Steve Antone831b8c2018-02-01 12:22:16 -08002650 payload_types, pos, &content_name, &bundle_only,
Steve Anton5a1de872018-12-18 11:25:55 -08002651 &section_msid_signaling, &transport, candidates, error);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002652 } else if (HasAttribute(line, kMediaTypeAudio)) {
Steve Anton5a1de872018-12-18 11:25:55 -08002653 content = ParseContentDescription<AudioContentDescription>(
deadbeef67cf2c12016-04-13 10:07:16 -07002654 message, cricket::MEDIA_TYPE_AUDIO, mline_index, protocol,
Steve Antone831b8c2018-02-01 12:22:16 -08002655 payload_types, pos, &content_name, &bundle_only,
Steve Anton5a1de872018-12-18 11:25:55 -08002656 &section_msid_signaling, &transport, candidates, error);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002657 } else if (HasAttribute(line, kMediaTypeData)) {
Steve Anton5a1de872018-12-18 11:25:55 -08002658 std::unique_ptr<DataContentDescription> data_desc =
wu@webrtc.org78187522013-10-07 23:32:02 +00002659 ParseContentDescription<DataContentDescription>(
deadbeef67cf2c12016-04-13 10:07:16 -07002660 message, cricket::MEDIA_TYPE_DATA, mline_index, protocol,
Steve Antone831b8c2018-02-01 12:22:16 -08002661 payload_types, pos, &content_name, &bundle_only,
2662 &section_msid_signaling, &transport, candidates, error);
wu@webrtc.org78187522013-10-07 23:32:02 +00002663
zstein4b2e0822017-02-17 19:48:38 -08002664 if (data_desc && IsDtlsSctp(protocol)) {
2665 int p;
2666 if (rtc::FromString(fields[3], &p)) {
Steve Anton5a1de872018-12-18 11:25:55 -08002667 if (!AddSctpDataCodec(data_desc.get(), p)) {
zstein4b2e0822017-02-17 19:48:38 -08002668 return false;
2669 }
2670 } else if (fields[3] == kDefaultSctpmapProtocol) {
2671 data_desc->set_use_sctpmap(false);
2672 }
wu@webrtc.org78187522013-10-07 23:32:02 +00002673 }
Steve Anton5a1de872018-12-18 11:25:55 -08002674
2675 content = std::move(data_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002676 } else {
Mirko Bonadei675513b2017-11-09 11:09:25 +01002677 RTC_LOG(LS_WARNING) << "Unsupported media type: " << line;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002678 continue;
2679 }
2680 if (!content.get()) {
2681 // ParseContentDescription returns NULL if failed.
2682 return false;
2683 }
2684
Steve Antone831b8c2018-02-01 12:22:16 -08002685 msid_signaling |= section_msid_signaling;
2686
deadbeef25ed4352016-12-12 18:37:36 -08002687 bool content_rejected = false;
deadbeef12771a12017-01-03 13:53:47 -08002688 // A port of 0 is not interpreted as a rejected m= section when it's
2689 // used along with a=bundle-only.
deadbeef25ed4352016-12-12 18:37:36 -08002690 if (bundle_only) {
deadbeef25ed4352016-12-12 18:37:36 -08002691 if (!port_rejected) {
deadbeef12771a12017-01-03 13:53:47 -08002692 // Usage of bundle-only with a nonzero port is unspecified. So just
2693 // ignore bundle-only if we see this.
2694 bundle_only = false;
Mirko Bonadei675513b2017-11-09 11:09:25 +01002695 RTC_LOG(LS_WARNING)
deadbeef12771a12017-01-03 13:53:47 -08002696 << "a=bundle-only attribute observed with a nonzero "
Jonas Olsson45cc8902018-02-13 10:37:07 +01002697 "port; this usage is unspecified so the attribute is being "
2698 "ignored.";
deadbeef25ed4352016-12-12 18:37:36 -08002699 }
2700 } else {
2701 // If not using bundle-only, interpret port 0 in the normal way; the m=
2702 // section is being rejected.
2703 content_rejected = port_rejected;
2704 }
2705
pthatcher@webrtc.org3341b402015-02-13 21:14:22 +00002706 if (IsRtp(protocol)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002707 // Set the extmap.
2708 if (!session_extmaps.empty() &&
2709 !content->rtp_header_extensions().empty()) {
2710 return ParseFailed("",
2711 "The a=extmap MUST be either all session level or "
2712 "all media level.",
2713 error);
2714 }
2715 for (size_t i = 0; i < session_extmaps.size(); ++i) {
2716 content->AddRtpHeaderExtension(session_extmaps[i]);
2717 }
2718 }
2719 content->set_protocol(protocol);
zhihuang38989e52017-03-21 11:04:53 -07002720
2721 // Use the session level connection address if the media level addresses are
2722 // not specified.
2723 rtc::SocketAddress address;
2724 address = content->connection_address().IsNil()
2725 ? session_connection_addr
2726 : content->connection_address();
2727 address.SetPort(port);
2728 content->set_connection_address(address);
2729
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002730 desc->AddContent(content_name,
Steve Anton5adfafd2017-12-20 16:34:00 -08002731 IsDtlsSctp(protocol) ? MediaProtocolType::kSctp
2732 : MediaProtocolType::kRtp,
deadbeef25ed4352016-12-12 18:37:36 -08002733 content_rejected, bundle_only, content.release());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002734 // Create TransportInfo with the media level "ice-pwd" and "ice-ufrag".
Steve Anton06817cd2018-12-18 15:55:30 -08002735 desc->AddTransportInfo(TransportInfo(content_name, transport));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002736 }
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00002737
Steve Antone831b8c2018-02-01 12:22:16 -08002738 desc->set_msid_signaling(msid_signaling);
2739
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00002740 size_t end_of_message = message.size();
2741 if (mline_index == -1 && *pos != end_of_message) {
2742 ParseFailed(message, *pos, "Expects m line.", error);
2743 return false;
2744 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002745 return true;
2746}
2747
2748bool VerifyCodec(const cricket::Codec& codec) {
2749 // Codec has not been populated correctly unless the name has been set. This
2750 // can happen if an SDP has an fmtp or rtcp-fb with a payload type but doesn't
2751 // have a corresponding "rtpmap" line.
htab39db842016-12-08 01:50:48 -08002752 return !codec.name.empty();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002753}
2754
2755bool VerifyAudioCodecs(const AudioContentDescription* audio_desc) {
2756 const std::vector<cricket::AudioCodec>& codecs = audio_desc->codecs();
Steve Anton4daf66e2018-09-07 14:55:53 -07002757 return std::all_of(codecs.begin(), codecs.end(), &VerifyCodec);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002758}
2759
2760bool VerifyVideoCodecs(const VideoContentDescription* video_desc) {
2761 const std::vector<cricket::VideoCodec>& codecs = video_desc->codecs();
Steve Anton4daf66e2018-09-07 14:55:53 -07002762 return std::all_of(codecs.begin(), codecs.end(), &VerifyCodec);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002763}
2764
2765void AddParameters(const cricket::CodecParameterMap& parameters,
2766 cricket::Codec* codec) {
Steve Anton4daf66e2018-09-07 14:55:53 -07002767 for (const auto& entry : parameters) {
2768 const std::string& key = entry.first;
2769 const std::string& value = entry.second;
2770 codec->SetParam(key, value);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002771 }
2772}
2773
2774void AddFeedbackParameter(const cricket::FeedbackParam& feedback_param,
2775 cricket::Codec* codec) {
2776 codec->AddFeedbackParam(feedback_param);
2777}
2778
2779void AddFeedbackParameters(const cricket::FeedbackParams& feedback_params,
2780 cricket::Codec* codec) {
Steve Anton4daf66e2018-09-07 14:55:53 -07002781 for (const cricket::FeedbackParam& param : feedback_params.params()) {
2782 codec->AddFeedbackParam(param);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002783 }
2784}
2785
2786// Gets the current codec setting associated with |payload_type|. If there
changbin.shao@webrtc.org2d25b442015-03-16 04:14:34 +00002787// is no Codec associated with that payload type it returns an empty codec
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002788// with that payload type.
2789template <class T>
changbin.shao@webrtc.org2d25b442015-03-16 04:14:34 +00002790T GetCodecWithPayloadType(const std::vector<T>& codecs, int payload_type) {
magjedb05fa242016-11-11 04:00:16 -08002791 const T* codec = FindCodecById(codecs, payload_type);
2792 if (codec)
2793 return *codec;
2794 // Return empty codec with |payload_type|.
changbin.shao@webrtc.org2d25b442015-03-16 04:14:34 +00002795 T ret_val;
magjedb05fa242016-11-11 04:00:16 -08002796 ret_val.id = payload_type;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002797 return ret_val;
2798}
2799
2800// Updates or creates a new codec entry in the audio description.
2801template <class T, class U>
2802void AddOrReplaceCodec(MediaContentDescription* content_desc, const U& codec) {
2803 T* desc = static_cast<T*>(content_desc);
2804 std::vector<U> codecs = desc->codecs();
2805 bool found = false;
Steve Anton4daf66e2018-09-07 14:55:53 -07002806 for (U& existing_codec : codecs) {
2807 if (codec.id == existing_codec.id) {
2808 // Overwrite existing codec with the new codec.
2809 existing_codec = codec;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002810 found = true;
2811 break;
2812 }
2813 }
2814 if (!found) {
2815 desc->AddCodec(codec);
2816 return;
2817 }
2818 desc->set_codecs(codecs);
2819}
2820
2821// Adds or updates existing codec corresponding to |payload_type| according
2822// to |parameters|.
2823template <class T, class U>
Yves Gerey665174f2018-06-19 15:03:05 +02002824void UpdateCodec(MediaContentDescription* content_desc,
2825 int payload_type,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002826 const cricket::CodecParameterMap& parameters) {
2827 // Codec might already have been populated (from rtpmap).
changbin.shao@webrtc.org2d25b442015-03-16 04:14:34 +00002828 U new_codec = GetCodecWithPayloadType(static_cast<T*>(content_desc)->codecs(),
2829 payload_type);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002830 AddParameters(parameters, &new_codec);
2831 AddOrReplaceCodec<T, U>(content_desc, new_codec);
2832}
2833
2834// Adds or updates existing codec corresponding to |payload_type| according
2835// to |feedback_param|.
2836template <class T, class U>
Yves Gerey665174f2018-06-19 15:03:05 +02002837void UpdateCodec(MediaContentDescription* content_desc,
2838 int payload_type,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002839 const cricket::FeedbackParam& feedback_param) {
2840 // Codec might already have been populated (from rtpmap).
changbin.shao@webrtc.org2d25b442015-03-16 04:14:34 +00002841 U new_codec = GetCodecWithPayloadType(static_cast<T*>(content_desc)->codecs(),
2842 payload_type);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002843 AddFeedbackParameter(feedback_param, &new_codec);
2844 AddOrReplaceCodec<T, U>(content_desc, new_codec);
2845}
2846
jlmiller@webrtc.orga744a282015-02-18 21:37:46 +00002847template <class T>
2848bool PopWildcardCodec(std::vector<T>* codecs, T* wildcard_codec) {
2849 for (auto iter = codecs->begin(); iter != codecs->end(); ++iter) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002850 if (iter->id == kWildcardPayloadType) {
2851 *wildcard_codec = *iter;
2852 codecs->erase(iter);
2853 return true;
2854 }
2855 }
2856 return false;
2857}
2858
Yves Gerey665174f2018-06-19 15:03:05 +02002859template <class T>
jlmiller@webrtc.orga744a282015-02-18 21:37:46 +00002860void UpdateFromWildcardCodecs(cricket::MediaContentDescriptionImpl<T>* desc) {
2861 auto codecs = desc->codecs();
2862 T wildcard_codec;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002863 if (!PopWildcardCodec(&codecs, &wildcard_codec)) {
2864 return;
2865 }
jlmiller@webrtc.orga744a282015-02-18 21:37:46 +00002866 for (auto& codec : codecs) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002867 AddFeedbackParameters(wildcard_codec.feedback_params, &codec);
2868 }
jlmiller@webrtc.orga744a282015-02-18 21:37:46 +00002869 desc->set_codecs(codecs);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002870}
2871
Yves Gerey665174f2018-06-19 15:03:05 +02002872void AddAudioAttribute(const std::string& name,
2873 const std::string& value,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002874 AudioContentDescription* audio_desc) {
2875 if (value.empty()) {
2876 return;
2877 }
2878 std::vector<cricket::AudioCodec> codecs = audio_desc->codecs();
Steve Anton4daf66e2018-09-07 14:55:53 -07002879 for (cricket::AudioCodec& codec : codecs) {
2880 codec.params[name] = value;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002881 }
2882 audio_desc->set_codecs(codecs);
2883}
2884
2885bool ParseContent(const std::string& message,
Patrik Höglundb6b29e02018-06-21 16:58:01 +02002886 const cricket::MediaType media_type,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002887 int mline_index,
2888 const std::string& protocol,
deadbeef67cf2c12016-04-13 10:07:16 -07002889 const std::vector<int>& payload_types,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002890 size_t* pos,
2891 std::string* content_name,
deadbeef25ed4352016-12-12 18:37:36 -08002892 bool* bundle_only,
Steve Antone831b8c2018-02-01 12:22:16 -08002893 int* msid_signaling,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002894 MediaContentDescription* media_desc,
2895 TransportDescription* transport,
Steve Anton5a1de872018-12-18 11:25:55 -08002896 std::vector<std::unique_ptr<JsepIceCandidate>>* candidates,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002897 SdpParseError* error) {
nisseede5da42017-01-12 05:15:36 -08002898 RTC_DCHECK(media_desc != NULL);
2899 RTC_DCHECK(content_name != NULL);
2900 RTC_DCHECK(transport != NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002901
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +00002902 if (media_type == cricket::MEDIA_TYPE_AUDIO) {
Steve Antonb1c1de12017-12-21 15:14:30 -08002903 MaybeCreateStaticPayloadAudioCodecs(payload_types, media_desc->as_audio());
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +00002904 }
2905
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002906 // The media level "ice-ufrag" and "ice-pwd".
2907 // The candidates before update the media level "ice-pwd" and "ice-ufrag".
2908 Candidates candidates_orig;
2909 std::string line;
2910 std::string mline_id;
2911 // Tracks created out of the ssrc attributes.
2912 StreamParamsVec tracks;
2913 SsrcInfoVec ssrc_infos;
2914 SsrcGroupVec ssrc_groups;
2915 std::string maxptime_as_string;
2916 std::string ptime_as_string;
Seth Hampson5b4f0752018-04-02 16:31:36 -07002917 std::vector<std::string> stream_ids;
deadbeef9d3584c2016-02-16 17:54:10 -08002918 std::string track_id;
Amit Hilbuchc57d5732018-12-11 15:30:11 -08002919 SdpSerializer deserializer;
2920 std::vector<RidDescription> rids;
Amit Hilbucha2012042018-12-03 11:35:05 -08002921 SimulcastDescription simulcast;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002922
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002923 // Loop until the next m line
2924 while (!IsLineType(message, kLineTypeMedia, *pos)) {
2925 if (!GetLine(message, pos, &line)) {
2926 if (*pos >= message.size()) {
2927 break; // Done parsing
2928 } else {
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +00002929 return ParseFailed(message, *pos, "Invalid SDP line.", error);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002930 }
2931 }
2932
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002933 // RFC 4566
2934 // b=* (zero or more bandwidth information lines)
2935 if (IsLineType(line, kLineTypeSessionBandwidth)) {
2936 std::string bandwidth;
2937 if (HasAttribute(line, kApplicationSpecificMaximum)) {
2938 if (!GetValue(line, kApplicationSpecificMaximum, &bandwidth, error)) {
2939 return false;
2940 } else {
wu@webrtc.org5e760e72014-04-02 23:19:09 +00002941 int b = 0;
2942 if (!GetValueFromString(line, bandwidth, &b, error)) {
2943 return false;
2944 }
deadbeef3e8016e2017-08-03 17:49:30 -07002945 // TODO(deadbeef): Historically, applications may be setting a value
2946 // of -1 to mean "unset any previously set bandwidth limit", even
2947 // though ommitting the "b=AS" entirely will do just that. Once we've
2948 // transitioned applications to doing the right thing, it would be
2949 // better to treat this as a hard error instead of just ignoring it.
2950 if (b == -1) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01002951 RTC_LOG(LS_WARNING)
2952 << "Ignoring \"b=AS:-1\"; will be treated as \"no "
2953 "bandwidth limit\".";
deadbeef3e8016e2017-08-03 17:49:30 -07002954 continue;
2955 }
deadbeefbc88c6b2017-08-02 11:26:34 -07002956 if (b < 0) {
2957 return ParseFailed(line, "b=AS value can't be negative.", error);
2958 }
Peter Thatcherc0c3a862015-06-24 15:31:25 -07002959 // We should never use more than the default bandwidth for RTP-based
2960 // data channels. Don't allow SDP to set the bandwidth, because
2961 // that would give JS the opportunity to "break the Internet".
2962 // See: https://code.google.com/p/chromium/issues/detail?id=280726
2963 if (media_type == cricket::MEDIA_TYPE_DATA && IsRtp(protocol) &&
2964 b > cricket::kDataMaxBandwidth / 1000) {
Jonas Olssonec9e4922018-09-05 09:53:49 +02002965 rtc::StringBuilder description;
Peter Thatcherc0c3a862015-06-24 15:31:25 -07002966 description << "RTP-based data channels may not send more than "
2967 << cricket::kDataMaxBandwidth / 1000 << "kbps.";
2968 return ParseFailed(line, description.str(), error);
2969 }
deadbeefb2362572016-12-13 16:37:06 -08002970 // Prevent integer overflow.
2971 b = std::min(b, INT_MAX / 1000);
wu@webrtc.org5e760e72014-04-02 23:19:09 +00002972 media_desc->set_bandwidth(b * 1000);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002973 }
2974 }
2975 continue;
2976 }
2977
zhihuang38989e52017-03-21 11:04:53 -07002978 // Parse the media level connection data.
2979 if (IsLineType(line, kLineTypeConnection)) {
2980 rtc::SocketAddress addr;
2981 if (!ParseConnectionData(line, &addr, error)) {
2982 return false;
2983 }
2984 media_desc->set_connection_address(addr);
2985 continue;
2986 }
2987
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002988 if (!IsLineType(line, kLineTypeAttributes)) {
Steve Anton36b29d12017-10-30 09:57:42 -07002989 // TODO(deadbeef): Handle other lines if needed.
Mirko Bonadei675513b2017-11-09 11:09:25 +01002990 RTC_LOG(LS_INFO) << "Ignored line: " << line;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002991 continue;
2992 }
2993
2994 // Handle attributes common to SCTP and RTP.
2995 if (HasAttribute(line, kAttributeMid)) {
2996 // RFC 3388
2997 // mid-attribute = "a=mid:" identification-tag
2998 // identification-tag = token
2999 // Use the mid identification-tag as the content name.
3000 if (!GetValue(line, kAttributeMid, &mline_id, error)) {
3001 return false;
3002 }
3003 *content_name = mline_id;
deadbeef25ed4352016-12-12 18:37:36 -08003004 } else if (HasAttribute(line, kAttributeBundleOnly)) {
3005 *bundle_only = true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003006 } else if (HasAttribute(line, kAttributeCandidate)) {
3007 Candidate candidate;
3008 if (!ParseCandidate(line, &candidate, error, false)) {
3009 return false;
3010 }
deadbeef7bcdb692017-01-20 12:43:58 -08003011 // ParseCandidate will parse non-standard ufrag and password attributes,
3012 // since it's used for candidate trickling, but we only want to process
3013 // the "a=ice-ufrag"/"a=ice-pwd" values in a session description, so
3014 // strip them off at this point.
3015 candidate.set_username(std::string());
3016 candidate.set_password(std::string());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003017 candidates_orig.push_back(candidate);
3018 } else if (HasAttribute(line, kAttributeIceUfrag)) {
3019 if (!GetValue(line, kAttributeIceUfrag, &transport->ice_ufrag, error)) {
3020 return false;
3021 }
3022 } else if (HasAttribute(line, kAttributeIcePwd)) {
3023 if (!GetValue(line, kAttributeIcePwd, &transport->ice_pwd, error)) {
3024 return false;
3025 }
3026 } else if (HasAttribute(line, kAttributeIceOption)) {
3027 if (!ParseIceOptions(line, &transport->transport_options, error)) {
3028 return false;
3029 }
3030 } else if (HasAttribute(line, kAttributeFmtp)) {
3031 if (!ParseFmtpAttributes(line, media_type, media_desc, error)) {
3032 return false;
3033 }
3034 } else if (HasAttribute(line, kAttributeFingerprint)) {
Steve Anton4905edb2018-10-15 19:27:44 -07003035 std::unique_ptr<rtc::SSLFingerprint> fingerprint;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003036 if (!ParseFingerprintAttribute(line, &fingerprint, error)) {
3037 return false;
3038 }
Steve Anton4905edb2018-10-15 19:27:44 -07003039 transport->identity_fingerprint = std::move(fingerprint);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00003040 } else if (HasAttribute(line, kAttributeSetup)) {
3041 if (!ParseDtlsSetup(line, &(transport->connection_role), error)) {
3042 return false;
3043 }
pthatcher@webrtc.org3341b402015-02-13 21:14:22 +00003044 } else if (IsDtlsSctp(protocol) && HasAttribute(line, kAttributeSctpPort)) {
deadbeef7e146cb2016-09-28 10:04:34 -07003045 if (media_type != cricket::MEDIA_TYPE_DATA) {
3046 return ParseFailed(
3047 line, "sctp-port attribute found in non-data media description.",
3048 error);
3049 }
jiayl@webrtc.orgddb85ab2014-09-05 16:31:56 +00003050 int sctp_port;
3051 if (!ParseSctpPort(line, &sctp_port, error)) {
3052 return false;
3053 }
Steve Antonb1c1de12017-12-21 15:14:30 -08003054 if (!AddSctpDataCodec(media_desc->as_data(), sctp_port)) {
jiayl@webrtc.orgddb85ab2014-09-05 16:31:56 +00003055 return false;
3056 }
pthatcher@webrtc.org3341b402015-02-13 21:14:22 +00003057 } else if (IsRtp(protocol)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003058 //
3059 // RTP specific attrubtes
3060 //
3061 if (HasAttribute(line, kAttributeRtcpMux)) {
3062 media_desc->set_rtcp_mux(true);
deadbeef13871492015-12-09 12:37:51 -08003063 } else if (HasAttribute(line, kAttributeRtcpReducedSize)) {
3064 media_desc->set_rtcp_reduced_size(true);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003065 } else if (HasAttribute(line, kAttributeSsrcGroup)) {
3066 if (!ParseSsrcGroupAttribute(line, &ssrc_groups, error)) {
3067 return false;
3068 }
3069 } else if (HasAttribute(line, kAttributeSsrc)) {
Steve Antone831b8c2018-02-01 12:22:16 -08003070 if (!ParseSsrcAttribute(line, &ssrc_infos, msid_signaling, error)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003071 return false;
3072 }
3073 } else if (HasAttribute(line, kAttributeCrypto)) {
3074 if (!ParseCryptoAttribute(line, media_desc, error)) {
3075 return false;
3076 }
3077 } else if (HasAttribute(line, kAttributeRtpmap)) {
deadbeef67cf2c12016-04-13 10:07:16 -07003078 if (!ParseRtpmapAttribute(line, media_type, payload_types, media_desc,
3079 error)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003080 return false;
3081 }
3082 } else if (HasAttribute(line, kCodecParamMaxPTime)) {
3083 if (!GetValue(line, kCodecParamMaxPTime, &maxptime_as_string, error)) {
3084 return false;
3085 }
3086 } else if (HasAttribute(line, kAttributeRtcpFb)) {
3087 if (!ParseRtcpFbAttribute(line, media_type, media_desc, error)) {
3088 return false;
3089 }
3090 } else if (HasAttribute(line, kCodecParamPTime)) {
3091 if (!GetValue(line, kCodecParamPTime, &ptime_as_string, error)) {
3092 return false;
3093 }
3094 } else if (HasAttribute(line, kAttributeSendOnly)) {
Steve Anton4e70a722017-11-28 14:57:10 -08003095 media_desc->set_direction(RtpTransceiverDirection::kSendOnly);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003096 } else if (HasAttribute(line, kAttributeRecvOnly)) {
Steve Anton4e70a722017-11-28 14:57:10 -08003097 media_desc->set_direction(RtpTransceiverDirection::kRecvOnly);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003098 } else if (HasAttribute(line, kAttributeInactive)) {
Steve Anton4e70a722017-11-28 14:57:10 -08003099 media_desc->set_direction(RtpTransceiverDirection::kInactive);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003100 } else if (HasAttribute(line, kAttributeSendRecv)) {
Steve Anton4e70a722017-11-28 14:57:10 -08003101 media_desc->set_direction(RtpTransceiverDirection::kSendRecv);
Johannes Kron0854eb62018-10-10 22:33:20 +02003102 } else if (HasAttribute(line, kAttributeExtmapAllowMixed)) {
Johannes Kron9581bc42018-10-23 10:17:39 +02003103 media_desc->set_extmap_allow_mixed_enum(
Johannes Kron0854eb62018-10-10 22:33:20 +02003104 MediaContentDescription::kMedia);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003105 } else if (HasAttribute(line, kAttributeExtmap)) {
isheriff6f8d6862016-05-26 11:24:55 -07003106 RtpExtension extmap;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003107 if (!ParseExtmap(line, &extmap, error)) {
3108 return false;
3109 }
3110 media_desc->AddRtpHeaderExtension(extmap);
3111 } else if (HasAttribute(line, kAttributeXGoogleFlag)) {
3112 // Experimental attribute. Conference mode activates more aggressive
3113 // AEC and NS settings.
Steve Anton36b29d12017-10-30 09:57:42 -07003114 // TODO(deadbeef): expose API to set these directly.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003115 std::string flag_value;
3116 if (!GetValue(line, kAttributeXGoogleFlag, &flag_value, error)) {
3117 return false;
3118 }
3119 if (flag_value.compare(kValueConference) == 0)
3120 media_desc->set_conference_mode(true);
deadbeef9d3584c2016-02-16 17:54:10 -08003121 } else if (HasAttribute(line, kAttributeMsid)) {
Seth Hampson5b4f0752018-04-02 16:31:36 -07003122 if (!ParseMsidAttribute(line, &stream_ids, &track_id, error)) {
deadbeef9d3584c2016-02-16 17:54:10 -08003123 return false;
3124 }
Steve Antone831b8c2018-02-01 12:22:16 -08003125 *msid_signaling |= cricket::kMsidSignalingMediaSection;
Amit Hilbuchc57d5732018-12-11 15:30:11 -08003126 } else if (HasAttribute(line, kAttributeRid)) {
3127 const size_t kRidPrefixLength =
3128 kLinePrefixLength + arraysize(kAttributeRid);
3129 if (line.size() <= kRidPrefixLength) {
3130 RTC_LOG(LS_INFO) << "Ignoring empty RID attribute: " << line;
3131 continue;
3132 }
3133 RTCErrorOr<RidDescription> error_or_rid_description =
3134 deserializer.DeserializeRidDescription(
3135 line.substr(kRidPrefixLength));
3136
3137 // Malformed a=rid lines are discarded.
3138 if (!error_or_rid_description.ok()) {
3139 RTC_LOG(LS_INFO) << "Ignoring malformed RID line: '" << line
3140 << "'. Error: "
3141 << error_or_rid_description.error().message();
3142 continue;
3143 }
3144
3145 rids.push_back(error_or_rid_description.MoveValue());
Amit Hilbucha2012042018-12-03 11:35:05 -08003146 } else if (HasAttribute(line, kAttributeSimulcast)) {
3147 const size_t kSimulcastPrefixLength =
3148 kLinePrefixLength + arraysize(kAttributeSimulcast);
3149 if (line.size() <= kSimulcastPrefixLength) {
3150 return ParseFailed(line, "Simulcast attribute is empty.", error);
3151 }
3152
3153 if (!simulcast.empty()) {
3154 return ParseFailed(line, "Multiple Simulcast attributes specified.",
3155 error);
3156 }
3157
Amit Hilbucha2012042018-12-03 11:35:05 -08003158 RTCErrorOr<SimulcastDescription> error_or_simulcast =
3159 deserializer.DeserializeSimulcastDescription(
3160 line.substr(kSimulcastPrefixLength));
3161 if (!error_or_simulcast.ok()) {
3162 return ParseFailed(line,
3163 std::string("Malformed simulcast line: ") +
3164 error_or_simulcast.error().message(),
3165 error);
3166 }
3167
3168 simulcast = error_or_simulcast.value();
3169 } else {
3170 // Unrecognized attribute in RTP protocol.
3171 RTC_LOG(LS_INFO) << "Ignored line: " << line;
3172 continue;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003173 }
3174 } else {
3175 // Only parse lines that we are interested of.
Mirko Bonadei675513b2017-11-09 11:09:25 +01003176 RTC_LOG(LS_INFO) << "Ignored line: " << line;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003177 continue;
3178 }
3179 }
3180
Amit Hilbuchc57d5732018-12-11 15:30:11 -08003181 // Remove duplicate or inconsistent rids.
3182 RemoveInvalidRidDescriptions(payload_types, &rids);
3183
3184 // If simulcast is specifed, split the rids into send and receive.
3185 // Rids that do not appear in simulcast attribute will be removed.
3186 // If it is not specified, we assume that all rids are for send layers.
3187 std::vector<RidDescription> send_rids, receive_rids;
3188 if (!simulcast.empty()) {
3189 // Verify that the rids in simulcast match rids in sdp.
3190 RemoveInvalidRidsFromSimulcast(rids, &simulcast);
3191
3192 // Use simulcast description to figure out Send / Receive RIDs.
3193 std::map<std::string, RidDescription> rid_map;
3194 for (const RidDescription& rid : rids) {
3195 rid_map[rid.rid] = rid;
3196 }
3197
3198 for (const auto& layer : simulcast.send_layers().GetAllLayers()) {
3199 auto iter = rid_map.find(layer.rid);
3200 RTC_DCHECK(iter != rid_map.end());
3201 send_rids.push_back(iter->second);
3202 }
3203
3204 for (const auto& layer : simulcast.receive_layers().GetAllLayers()) {
3205 auto iter = rid_map.find(layer.rid);
3206 RTC_DCHECK(iter != rid_map.end());
3207 receive_rids.push_back(iter->second);
3208 }
3209
3210 media_desc->set_simulcast_description(simulcast);
3211 } else {
3212 send_rids = rids;
3213 }
3214
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003215 // Create tracks from the |ssrc_infos|.
Taylor Brandstetter5de6b752016-03-09 17:02:30 -08003216 // If the stream_id/track_id for all SSRCS are identical, one StreamParams
3217 // will be created in CreateTracksFromSsrcInfos, containing all the SSRCs from
3218 // the m= section.
Seth Hampson5897a6e2018-04-03 11:16:33 -07003219 if (!ssrc_infos.empty()) {
3220 CreateTracksFromSsrcInfos(ssrc_infos, stream_ids, track_id, &tracks,
3221 *msid_signaling);
3222 } else if (media_type != cricket::MEDIA_TYPE_DATA &&
3223 (*msid_signaling & cricket::kMsidSignalingMediaSection)) {
3224 // If the stream_ids/track_id was signaled but SSRCs were unsignaled we
3225 // still create a track. This isn't done for data media types because
3226 // StreamParams aren't used for SCTP streams, and RTP data channels don't
3227 // support unsignaled SSRCs.
Amit Hilbuchc57d5732018-12-11 15:30:11 -08003228 CreateTrackWithNoSsrcs(stream_ids, track_id, send_rids, &tracks);
3229 }
3230
3231 // Create receive track when we have incoming receive rids.
3232 if (!receive_rids.empty()) {
3233 StreamParams receive_track;
3234 receive_track.set_rids(receive_rids);
3235 media_desc->set_receive_stream(receive_track);
Seth Hampson5897a6e2018-04-03 11:16:33 -07003236 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003237
3238 // Add the ssrc group to the track.
Steve Anton4daf66e2018-09-07 14:55:53 -07003239 for (const SsrcGroup& ssrc_group : ssrc_groups) {
3240 if (ssrc_group.ssrcs.empty()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003241 continue;
3242 }
Steve Anton4daf66e2018-09-07 14:55:53 -07003243 uint32_t ssrc = ssrc_group.ssrcs.front();
3244 for (StreamParams& track : tracks) {
3245 if (track.has_ssrc(ssrc)) {
3246 track.ssrc_groups.push_back(ssrc_group);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003247 }
3248 }
3249 }
3250
3251 // Add the new tracks to the |media_desc|.
deadbeef9d3584c2016-02-16 17:54:10 -08003252 for (StreamParams& track : tracks) {
3253 media_desc->AddStream(track);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003254 }
3255
3256 if (media_type == cricket::MEDIA_TYPE_AUDIO) {
Steve Antonb1c1de12017-12-21 15:14:30 -08003257 AudioContentDescription* audio_desc = media_desc->as_audio();
jlmiller@webrtc.orga744a282015-02-18 21:37:46 +00003258 UpdateFromWildcardCodecs(audio_desc);
3259
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003260 // Verify audio codec ensures that no audio codec has been populated with
3261 // only fmtp.
3262 if (!VerifyAudioCodecs(audio_desc)) {
3263 return ParseFailed("Failed to parse audio codecs correctly.", error);
3264 }
3265 AddAudioAttribute(kCodecParamMaxPTime, maxptime_as_string, audio_desc);
3266 AddAudioAttribute(kCodecParamPTime, ptime_as_string, audio_desc);
3267 }
3268
3269 if (media_type == cricket::MEDIA_TYPE_VIDEO) {
Steve Antonb1c1de12017-12-21 15:14:30 -08003270 VideoContentDescription* video_desc = media_desc->as_video();
jlmiller@webrtc.orga744a282015-02-18 21:37:46 +00003271 UpdateFromWildcardCodecs(video_desc);
3272 // Verify video codec ensures that no video codec has been populated with
3273 // only rtcp-fb.
3274 if (!VerifyVideoCodecs(video_desc)) {
3275 return ParseFailed("Failed to parse video codecs correctly.", error);
3276 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003277 }
3278
3279 // RFC 5245
3280 // Update the candidates with the media level "ice-pwd" and "ice-ufrag".
Steve Anton4daf66e2018-09-07 14:55:53 -07003281 for (Candidate& candidate : candidates_orig) {
3282 RTC_DCHECK(candidate.username().empty() ||
3283 candidate.username() == transport->ice_ufrag);
3284 candidate.set_username(transport->ice_ufrag);
3285 RTC_DCHECK(candidate.password().empty());
3286 candidate.set_password(transport->ice_pwd);
3287 candidates->push_back(
Steve Anton5a1de872018-12-18 11:25:55 -08003288 absl::make_unique<JsepIceCandidate>(mline_id, mline_index, candidate));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003289 }
Amit Hilbucha2012042018-12-03 11:35:05 -08003290
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003291 return true;
3292}
3293
Steve Antone831b8c2018-02-01 12:22:16 -08003294bool ParseSsrcAttribute(const std::string& line,
3295 SsrcInfoVec* ssrc_infos,
3296 int* msid_signaling,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003297 SdpParseError* error) {
nisseede5da42017-01-12 05:15:36 -08003298 RTC_DCHECK(ssrc_infos != NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003299 // RFC 5576
3300 // a=ssrc:<ssrc-id> <attribute>
3301 // a=ssrc:<ssrc-id> <attribute>:<value>
3302 std::string field1, field2;
Jonas Olssonec9e4922018-09-05 09:53:49 +02003303 if (!rtc::tokenize_first(line.substr(kLinePrefixLength),
3304 kSdpDelimiterSpaceChar, &field1, &field2)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003305 const size_t expected_fields = 2;
3306 return ParseFailedExpectFieldNum(line, expected_fields, error);
3307 }
3308
3309 // ssrc:<ssrc-id>
3310 std::string ssrc_id_s;
3311 if (!GetValue(field1, kAttributeSsrc, &ssrc_id_s, error)) {
3312 return false;
3313 }
Peter Boström0c4e06b2015-10-07 12:23:21 +02003314 uint32_t ssrc_id = 0;
wu@webrtc.org5e760e72014-04-02 23:19:09 +00003315 if (!GetValueFromString(line, ssrc_id_s, &ssrc_id, error)) {
3316 return false;
3317 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003318
3319 std::string attribute;
3320 std::string value;
Jonas Olssonec9e4922018-09-05 09:53:49 +02003321 if (!rtc::tokenize_first(field2, kSdpDelimiterColonChar, &attribute,
3322 &value)) {
3323 rtc::StringBuilder description;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003324 description << "Failed to get the ssrc attribute value from " << field2
3325 << ". Expected format <attribute>:<value>.";
3326 return ParseFailed(line, description.str(), error);
3327 }
3328
3329 // Check if there's already an item for this |ssrc_id|. Create a new one if
3330 // there isn't.
Steve Anton4daf66e2018-09-07 14:55:53 -07003331 auto ssrc_info_it = std::find_if(ssrc_infos->begin(), ssrc_infos->end(),
3332 [ssrc_id](const SsrcInfo& ssrc_info) {
3333 return ssrc_info.ssrc_id == ssrc_id;
3334 });
3335 if (ssrc_info_it == ssrc_infos->end()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003336 SsrcInfo info;
3337 info.ssrc_id = ssrc_id;
3338 ssrc_infos->push_back(info);
Steve Anton4daf66e2018-09-07 14:55:53 -07003339 ssrc_info_it = ssrc_infos->end() - 1;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003340 }
Steve Anton4daf66e2018-09-07 14:55:53 -07003341 SsrcInfo& ssrc_info = *ssrc_info_it;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003342
3343 // Store the info to the |ssrc_info|.
3344 if (attribute == kSsrcAttributeCname) {
3345 // RFC 5576
3346 // cname:<value>
Steve Anton4daf66e2018-09-07 14:55:53 -07003347 ssrc_info.cname = value;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003348 } else if (attribute == kSsrcAttributeMsid) {
3349 // draft-alvestrand-mmusic-msid-00
Seth Hampson5b4f0752018-04-02 16:31:36 -07003350 // msid:identifier [appdata]
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003351 std::vector<std::string> fields;
Jonas Olssonec9e4922018-09-05 09:53:49 +02003352 rtc::split(value, kSdpDelimiterSpaceChar, &fields);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003353 if (fields.size() < 1 || fields.size() > 2) {
Yves Gerey665174f2018-06-19 15:03:05 +02003354 return ParseFailed(
3355 line, "Expected format \"msid:<identifier>[ <appdata>]\".", error);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003356 }
Steve Anton4daf66e2018-09-07 14:55:53 -07003357 ssrc_info.stream_id = fields[0];
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003358 if (fields.size() == 2) {
Steve Anton4daf66e2018-09-07 14:55:53 -07003359 ssrc_info.track_id = fields[1];
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003360 }
Steve Antone831b8c2018-02-01 12:22:16 -08003361 *msid_signaling |= cricket::kMsidSignalingSsrcAttribute;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003362 } else if (attribute == kSsrcAttributeMslabel) {
3363 // draft-alvestrand-rtcweb-mid-01
3364 // mslabel:<value>
Steve Anton4daf66e2018-09-07 14:55:53 -07003365 ssrc_info.mslabel = value;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003366 } else if (attribute == kSSrcAttributeLabel) {
3367 // The label isn't defined.
3368 // label:<value>
Steve Anton4daf66e2018-09-07 14:55:53 -07003369 ssrc_info.label = value;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003370 }
3371 return true;
3372}
3373
3374bool ParseSsrcGroupAttribute(const std::string& line,
3375 SsrcGroupVec* ssrc_groups,
3376 SdpParseError* error) {
nisseede5da42017-01-12 05:15:36 -08003377 RTC_DCHECK(ssrc_groups != NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003378 // RFC 5576
3379 // a=ssrc-group:<semantics> <ssrc-id> ...
3380 std::vector<std::string> fields;
Jonas Olssonec9e4922018-09-05 09:53:49 +02003381 rtc::split(line.substr(kLinePrefixLength), kSdpDelimiterSpaceChar, &fields);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003382 const size_t expected_min_fields = 2;
3383 if (fields.size() < expected_min_fields) {
3384 return ParseFailedExpectMinFieldNum(line, expected_min_fields, error);
3385 }
3386 std::string semantics;
3387 if (!GetValue(fields[0], kAttributeSsrcGroup, &semantics, error)) {
3388 return false;
3389 }
Peter Boström0c4e06b2015-10-07 12:23:21 +02003390 std::vector<uint32_t> ssrcs;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003391 for (size_t i = 1; i < fields.size(); ++i) {
Peter Boström0c4e06b2015-10-07 12:23:21 +02003392 uint32_t ssrc = 0;
wu@webrtc.org5e760e72014-04-02 23:19:09 +00003393 if (!GetValueFromString(line, fields[i], &ssrc, error)) {
3394 return false;
3395 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003396 ssrcs.push_back(ssrc);
3397 }
3398 ssrc_groups->push_back(SsrcGroup(semantics, ssrcs));
3399 return true;
3400}
3401
3402bool ParseCryptoAttribute(const std::string& line,
3403 MediaContentDescription* media_desc,
3404 SdpParseError* error) {
3405 std::vector<std::string> fields;
Jonas Olssonec9e4922018-09-05 09:53:49 +02003406 rtc::split(line.substr(kLinePrefixLength), kSdpDelimiterSpaceChar, &fields);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003407 // RFC 4568
3408 // a=crypto:<tag> <crypto-suite> <key-params> [<session-params>]
3409 const size_t expected_min_fields = 3;
3410 if (fields.size() < expected_min_fields) {
3411 return ParseFailedExpectMinFieldNum(line, expected_min_fields, error);
3412 }
3413 std::string tag_value;
3414 if (!GetValue(fields[0], kAttributeCrypto, &tag_value, error)) {
3415 return false;
3416 }
wu@webrtc.org5e760e72014-04-02 23:19:09 +00003417 int tag = 0;
3418 if (!GetValueFromString(line, tag_value, &tag, error)) {
3419 return false;
3420 }
jbauch083b73f2015-07-16 02:46:32 -07003421 const std::string& crypto_suite = fields[1];
3422 const std::string& key_params = fields[2];
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003423 std::string session_params;
3424 if (fields.size() > 3) {
3425 session_params = fields[3];
3426 }
Yves Gerey665174f2018-06-19 15:03:05 +02003427 media_desc->AddCrypto(
3428 CryptoParams(tag, crypto_suite, key_params, session_params));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003429 return true;
3430}
3431
3432// Updates or creates a new codec entry in the audio description with according
deadbeef67cf2c12016-04-13 10:07:16 -07003433// to |name|, |clockrate|, |bitrate|, and |channels|.
3434void UpdateCodec(int payload_type,
3435 const std::string& name,
3436 int clockrate,
3437 int bitrate,
3438 size_t channels,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003439 AudioContentDescription* audio_desc) {
3440 // Codec may already be populated with (only) optional parameters
3441 // (from an fmtp).
changbin.shao@webrtc.org2d25b442015-03-16 04:14:34 +00003442 cricket::AudioCodec codec =
3443 GetCodecWithPayloadType(audio_desc->codecs(), payload_type);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003444 codec.name = name;
3445 codec.clockrate = clockrate;
3446 codec.bitrate = bitrate;
3447 codec.channels = channels;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003448 AddOrReplaceCodec<AudioContentDescription, cricket::AudioCodec>(audio_desc,
3449 codec);
3450}
3451
3452// Updates or creates a new codec entry in the video description according to
deadbeef67cf2c12016-04-13 10:07:16 -07003453// |name|, |width|, |height|, and |framerate|.
3454void UpdateCodec(int payload_type,
3455 const std::string& name,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003456 VideoContentDescription* video_desc) {
3457 // Codec may already be populated with (only) optional parameters
3458 // (from an fmtp).
changbin.shao@webrtc.org2d25b442015-03-16 04:14:34 +00003459 cricket::VideoCodec codec =
3460 GetCodecWithPayloadType(video_desc->codecs(), payload_type);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003461 codec.name = name;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003462 AddOrReplaceCodec<VideoContentDescription, cricket::VideoCodec>(video_desc,
3463 codec);
3464}
3465
3466bool ParseRtpmapAttribute(const std::string& line,
Patrik Höglundb6b29e02018-06-21 16:58:01 +02003467 const cricket::MediaType media_type,
deadbeef67cf2c12016-04-13 10:07:16 -07003468 const std::vector<int>& payload_types,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003469 MediaContentDescription* media_desc,
3470 SdpParseError* error) {
3471 std::vector<std::string> fields;
Jonas Olssonec9e4922018-09-05 09:53:49 +02003472 rtc::split(line.substr(kLinePrefixLength), kSdpDelimiterSpaceChar, &fields);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003473 // RFC 4566
3474 // a=rtpmap:<payload type> <encoding name>/<clock rate>[/<encodingparameters>]
3475 const size_t expected_min_fields = 2;
3476 if (fields.size() < expected_min_fields) {
3477 return ParseFailedExpectMinFieldNum(line, expected_min_fields, error);
3478 }
3479 std::string payload_type_value;
3480 if (!GetValue(fields[0], kAttributeRtpmap, &payload_type_value, error)) {
3481 return false;
3482 }
wu@webrtc.org5e760e72014-04-02 23:19:09 +00003483 int payload_type = 0;
pkasting@chromium.orge9facf82015-02-17 20:36:28 +00003484 if (!GetPayloadTypeFromString(line, payload_type_value, &payload_type,
3485 error)) {
wu@webrtc.org5e760e72014-04-02 23:19:09 +00003486 return false;
3487 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003488
deadbeef67cf2c12016-04-13 10:07:16 -07003489 if (std::find(payload_types.begin(), payload_types.end(), payload_type) ==
3490 payload_types.end()) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01003491 RTC_LOG(LS_WARNING) << "Ignore rtpmap line that did not appear in the "
Jonas Olsson45cc8902018-02-13 10:37:07 +01003492 "<fmt> of the m-line: "
3493 << line;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003494 return true;
3495 }
jbauch083b73f2015-07-16 02:46:32 -07003496 const std::string& encoder = fields[1];
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003497 std::vector<std::string> codec_params;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00003498 rtc::split(encoder, '/', &codec_params);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003499 // <encoding name>/<clock rate>[/<encodingparameters>]
3500 // 2 mandatory fields
3501 if (codec_params.size() < 2 || codec_params.size() > 3) {
3502 return ParseFailed(line,
3503 "Expected format \"<encoding name>/<clock rate>"
3504 "[/<encodingparameters>]\".",
3505 error);
3506 }
jbauch083b73f2015-07-16 02:46:32 -07003507 const std::string& encoding_name = codec_params[0];
wu@webrtc.org5e760e72014-04-02 23:19:09 +00003508 int clock_rate = 0;
3509 if (!GetValueFromString(line, codec_params[1], &clock_rate, error)) {
3510 return false;
3511 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003512 if (media_type == cricket::MEDIA_TYPE_VIDEO) {
Steve Antonb1c1de12017-12-21 15:14:30 -08003513 VideoContentDescription* video_desc = media_desc->as_video();
Yves Gerey665174f2018-06-19 15:03:05 +02003514 UpdateCodec(payload_type, encoding_name, video_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003515 } else if (media_type == cricket::MEDIA_TYPE_AUDIO) {
3516 // RFC 4566
3517 // For audio streams, <encoding parameters> indicates the number
3518 // of audio channels. This parameter is OPTIONAL and may be
3519 // omitted if the number of channels is one, provided that no
3520 // additional parameters are needed.
Peter Kasting69558702016-01-12 16:26:35 -08003521 size_t channels = 1;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003522 if (codec_params.size() == 3) {
wu@webrtc.org5e760e72014-04-02 23:19:09 +00003523 if (!GetValueFromString(line, codec_params[2], &channels, error)) {
3524 return false;
3525 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003526 }
Steve Antonb1c1de12017-12-21 15:14:30 -08003527 AudioContentDescription* audio_desc = media_desc->as_audio();
ossue1405ad2017-01-23 08:55:48 -08003528 UpdateCodec(payload_type, encoding_name, clock_rate, 0, channels,
deadbeef67cf2c12016-04-13 10:07:16 -07003529 audio_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003530 } else if (media_type == cricket::MEDIA_TYPE_DATA) {
Steve Antonb1c1de12017-12-21 15:14:30 -08003531 DataContentDescription* data_desc = media_desc->as_data();
deadbeef67cf2c12016-04-13 10:07:16 -07003532 data_desc->AddCodec(cricket::DataCodec(payload_type, encoding_name));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003533 }
3534 return true;
3535}
3536
Yves Gerey665174f2018-06-19 15:03:05 +02003537bool ParseFmtpParam(const std::string& line,
3538 std::string* parameter,
3539 std::string* value,
3540 SdpParseError* error) {
Jonas Olssonec9e4922018-09-05 09:53:49 +02003541 if (!rtc::tokenize_first(line, kSdpDelimiterEqualChar, parameter, value)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003542 ParseFailed(line, "Unable to parse fmtp parameter. \'=\' missing.", error);
3543 return false;
3544 }
3545 // a=fmtp:<payload_type> <param1>=<value1>; <param2>=<value2>; ...
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003546 return true;
3547}
3548
Yves Gerey665174f2018-06-19 15:03:05 +02003549bool ParseFmtpAttributes(const std::string& line,
Patrik Höglundb6b29e02018-06-21 16:58:01 +02003550 const cricket::MediaType media_type,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003551 MediaContentDescription* media_desc,
3552 SdpParseError* error) {
3553 if (media_type != cricket::MEDIA_TYPE_AUDIO &&
3554 media_type != cricket::MEDIA_TYPE_VIDEO) {
3555 return true;
3556 }
Donald Curtis0e07f922015-05-15 09:21:23 -07003557
3558 std::string line_payload;
3559 std::string line_params;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003560
3561 // RFC 5576
3562 // a=fmtp:<format> <format specific parameters>
3563 // At least two fields, whereas the second one is any of the optional
3564 // parameters.
Jonas Olssonec9e4922018-09-05 09:53:49 +02003565 if (!rtc::tokenize_first(line.substr(kLinePrefixLength),
3566 kSdpDelimiterSpaceChar, &line_payload,
3567 &line_params)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003568 ParseFailedExpectMinFieldNum(line, 2, error);
3569 return false;
3570 }
3571
Donald Curtis0e07f922015-05-15 09:21:23 -07003572 // Parse out the payload information.
pkasting@chromium.orgd3245462015-02-23 21:28:22 +00003573 std::string payload_type_str;
Donald Curtis0e07f922015-05-15 09:21:23 -07003574 if (!GetValue(line_payload, kAttributeFmtp, &payload_type_str, error)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003575 return false;
3576 }
3577
Donald Curtis0e07f922015-05-15 09:21:23 -07003578 int payload_type = 0;
Donald Curtis144d0182015-05-15 13:14:24 -07003579 if (!GetPayloadTypeFromString(line_payload, payload_type_str, &payload_type,
3580 error)) {
Donald Curtis0e07f922015-05-15 09:21:23 -07003581 return false;
3582 }
3583
3584 // Parse out format specific parameters.
3585 std::vector<std::string> fields;
Jonas Olssonec9e4922018-09-05 09:53:49 +02003586 rtc::split(line_params, kSdpDelimiterSemicolonChar, &fields);
Donald Curtis0e07f922015-05-15 09:21:23 -07003587
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003588 cricket::CodecParameterMap codec_params;
Donald Curtis144d0182015-05-15 13:14:24 -07003589 for (auto& iter : fields) {
Donald Curtis0e07f922015-05-15 09:21:23 -07003590 if (iter.find(kSdpDelimiterEqual) == std::string::npos) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003591 // Only fmtps with equals are currently supported. Other fmtp types
3592 // should be ignored. Unknown fmtps do not constitute an error.
3593 continue;
3594 }
Donald Curtis0e07f922015-05-15 09:21:23 -07003595
3596 std::string name;
3597 std::string value;
3598 if (!ParseFmtpParam(rtc::string_trim(iter), &name, &value, error)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003599 return false;
3600 }
3601 codec_params[name] = value;
3602 }
3603
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003604 if (media_type == cricket::MEDIA_TYPE_AUDIO) {
3605 UpdateCodec<AudioContentDescription, cricket::AudioCodec>(
pkasting@chromium.orgd3245462015-02-23 21:28:22 +00003606 media_desc, payload_type, codec_params);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003607 } else if (media_type == cricket::MEDIA_TYPE_VIDEO) {
3608 UpdateCodec<VideoContentDescription, cricket::VideoCodec>(
pkasting@chromium.orgd3245462015-02-23 21:28:22 +00003609 media_desc, payload_type, codec_params);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003610 }
3611 return true;
3612}
3613
Yves Gerey665174f2018-06-19 15:03:05 +02003614bool ParseRtcpFbAttribute(const std::string& line,
Patrik Höglundb6b29e02018-06-21 16:58:01 +02003615 const cricket::MediaType media_type,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003616 MediaContentDescription* media_desc,
3617 SdpParseError* error) {
3618 if (media_type != cricket::MEDIA_TYPE_AUDIO &&
3619 media_type != cricket::MEDIA_TYPE_VIDEO) {
3620 return true;
3621 }
3622 std::vector<std::string> rtcp_fb_fields;
Jonas Olssonec9e4922018-09-05 09:53:49 +02003623 rtc::split(line.c_str(), kSdpDelimiterSpaceChar, &rtcp_fb_fields);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003624 if (rtcp_fb_fields.size() < 2) {
3625 return ParseFailedGetValue(line, kAttributeRtcpFb, error);
3626 }
3627 std::string payload_type_string;
3628 if (!GetValue(rtcp_fb_fields[0], kAttributeRtcpFb, &payload_type_string,
3629 error)) {
3630 return false;
3631 }
wu@webrtc.org5e760e72014-04-02 23:19:09 +00003632 int payload_type = kWildcardPayloadType;
3633 if (payload_type_string != "*") {
pkasting@chromium.orge9facf82015-02-17 20:36:28 +00003634 if (!GetPayloadTypeFromString(line, payload_type_string, &payload_type,
3635 error)) {
wu@webrtc.org5e760e72014-04-02 23:19:09 +00003636 return false;
3637 }
3638 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003639 std::string id = rtcp_fb_fields[1];
3640 std::string param = "";
3641 for (std::vector<std::string>::iterator iter = rtcp_fb_fields.begin() + 2;
3642 iter != rtcp_fb_fields.end(); ++iter) {
3643 param.append(*iter);
3644 }
3645 const cricket::FeedbackParam feedback_param(id, param);
3646
3647 if (media_type == cricket::MEDIA_TYPE_AUDIO) {
pkasting@chromium.orgd3245462015-02-23 21:28:22 +00003648 UpdateCodec<AudioContentDescription, cricket::AudioCodec>(
3649 media_desc, payload_type, feedback_param);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003650 } else if (media_type == cricket::MEDIA_TYPE_VIDEO) {
pkasting@chromium.orgd3245462015-02-23 21:28:22 +00003651 UpdateCodec<VideoContentDescription, cricket::VideoCodec>(
3652 media_desc, payload_type, feedback_param);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003653 }
3654 return true;
3655}
3656
3657} // namespace webrtc