blob: 97f2455d7abf56b8b53a9e3d3c28a615e748cd2b [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);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000338static bool ParseRtcpFbAttribute(const std::string& line,
Patrik Höglundb6b29e02018-06-21 16:58:01 +0200339 const cricket::MediaType media_type,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000340 MediaContentDescription* media_desc,
341 SdpParseError* error);
342static bool ParseIceOptions(const std::string& line,
343 std::vector<std::string>* transport_options,
344 SdpParseError* error);
345static bool ParseExtmap(const std::string& line,
isheriff6f8d6862016-05-26 11:24:55 -0700346 RtpExtension* extmap,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000347 SdpParseError* error);
Steve Anton4905edb2018-10-15 19:27:44 -0700348static bool ParseFingerprintAttribute(
349 const std::string& line,
350 std::unique_ptr<rtc::SSLFingerprint>* fingerprint,
351 SdpParseError* error);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +0000352static bool ParseDtlsSetup(const std::string& line,
353 cricket::ConnectionRole* role,
354 SdpParseError* error);
deadbeef9d3584c2016-02-16 17:54:10 -0800355static bool ParseMsidAttribute(const std::string& line,
Seth Hampson5b4f0752018-04-02 16:31:36 -0700356 std::vector<std::string>* stream_ids,
deadbeef9d3584c2016-02-16 17:54:10 -0800357 std::string* track_id,
358 SdpParseError* error);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000359
Amit Hilbuchc57d5732018-12-11 15:30:11 -0800360static void RemoveInvalidRidDescriptions(const std::vector<int>& payload_types,
361 std::vector<RidDescription>* rids);
362
363static SimulcastLayerList RemoveRidsFromSimulcastLayerList(
364 const std::set<std::string>& to_remove,
365 const SimulcastLayerList& layers);
366
367static void RemoveInvalidRidsFromSimulcast(
368 const std::vector<RidDescription>& rids,
369 SimulcastDescription* simulcast);
370
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000371// Helper functions
372
373// Below ParseFailed*** functions output the line that caused the parsing
374// failure and the detailed reason (|description|) of the failure to |error|.
375// The functions always return false so that they can be used directly in the
376// following way when error happens:
377// "return ParseFailed***(...);"
378
379// The line starting at |line_start| of |message| is the failing line.
380// The reason for the failure should be provided in the |description|.
381// An example of a description could be "unknown character".
382static bool ParseFailed(const std::string& message,
383 size_t line_start,
384 const std::string& description,
385 SdpParseError* error) {
386 // Get the first line of |message| from |line_start|.
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +0000387 std::string first_line;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000388 size_t line_end = message.find(kNewLine, line_start);
389 if (line_end != std::string::npos) {
Jonas Olssonec9e4922018-09-05 09:53:49 +0200390 if (line_end > 0 && (message.at(line_end - 1) == kReturnChar)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000391 --line_end;
392 }
393 first_line = message.substr(line_start, (line_end - line_start));
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +0000394 } else {
395 first_line = message.substr(line_start);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000396 }
397
398 if (error) {
399 error->line = first_line;
400 error->description = description;
401 }
Mirko Bonadei675513b2017-11-09 11:09:25 +0100402 RTC_LOG(LS_ERROR) << "Failed to parse: \"" << first_line
403 << "\". Reason: " << description;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000404 return false;
405}
406
407// |line| is the failing line. The reason for the failure should be
408// provided in the |description|.
409static bool ParseFailed(const std::string& line,
410 const std::string& description,
411 SdpParseError* error) {
412 return ParseFailed(line, 0, description, error);
413}
414
415// Parses failure where the failing SDP line isn't know or there are multiple
416// failing lines.
Yves Gerey665174f2018-06-19 15:03:05 +0200417static bool ParseFailed(const std::string& description, SdpParseError* error) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000418 return ParseFailed("", description, error);
419}
420
421// |line| is the failing line. The failure is due to the fact that |line|
422// doesn't have |expected_fields| fields.
423static bool ParseFailedExpectFieldNum(const std::string& line,
424 int expected_fields,
425 SdpParseError* error) {
Jonas Olssonec9e4922018-09-05 09:53:49 +0200426 rtc::StringBuilder description;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000427 description << "Expects " << expected_fields << " fields.";
428 return ParseFailed(line, description.str(), error);
429}
430
431// |line| is the failing line. The failure is due to the fact that |line| has
432// less than |expected_min_fields| fields.
433static bool ParseFailedExpectMinFieldNum(const std::string& line,
434 int expected_min_fields,
435 SdpParseError* error) {
Jonas Olssonec9e4922018-09-05 09:53:49 +0200436 rtc::StringBuilder description;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000437 description << "Expects at least " << expected_min_fields << " fields.";
438 return ParseFailed(line, description.str(), error);
439}
440
441// |line| is the failing line. The failure is due to the fact that it failed to
442// get the value of |attribute|.
443static bool ParseFailedGetValue(const std::string& line,
444 const std::string& attribute,
445 SdpParseError* error) {
Jonas Olssonec9e4922018-09-05 09:53:49 +0200446 rtc::StringBuilder description;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000447 description << "Failed to get the value of attribute: " << attribute;
448 return ParseFailed(line, description.str(), error);
449}
450
451// The line starting at |line_start| of |message| is the failing line. The
452// failure is due to the line type (e.g. the "m" part of the "m-line")
453// not matching what is expected. The expected line type should be
454// provided as |line_type|.
455static bool ParseFailedExpectLine(const std::string& message,
456 size_t line_start,
457 const char line_type,
458 const std::string& line_value,
459 SdpParseError* error) {
Jonas Olssonec9e4922018-09-05 09:53:49 +0200460 rtc::StringBuilder description;
461 description << "Expect line: " << std::string(1, line_type) << "="
462 << line_value;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000463 return ParseFailed(message, line_start, description.str(), error);
464}
465
466static bool AddLine(const std::string& line, std::string* message) {
467 if (!message)
468 return false;
469
470 message->append(line);
471 message->append(kLineBreak);
472 return true;
473}
474
475static bool GetLine(const std::string& message,
476 size_t* pos,
477 std::string* line) {
478 size_t line_begin = *pos;
479 size_t line_end = message.find(kNewLine, line_begin);
480 if (line_end == std::string::npos) {
481 return false;
482 }
483 // Update the new start position
484 *pos = line_end + 1;
Jonas Olssonec9e4922018-09-05 09:53:49 +0200485 if (line_end > 0 && (message.at(line_end - 1) == kReturnChar)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000486 --line_end;
487 }
488 *line = message.substr(line_begin, (line_end - line_begin));
489 const char* cline = line->c_str();
490 // RFC 4566
491 // An SDP session description consists of a number of lines of text of
492 // the form:
493 // <type>=<value>
494 // where <type> MUST be exactly one case-significant character and
495 // <value> is structured text whose format depends on <type>.
496 // Whitespace MUST NOT be used on either side of the "=" sign.
Taylor Brandstetter93a7b242018-04-16 10:45:24 -0700497 //
498 // However, an exception to the whitespace rule is made for "s=", since
499 // RFC4566 also says:
500 //
501 // If a session has no meaningful name, the value "s= " SHOULD be used
502 // (i.e., a single space as the session name).
503 if (line->length() < 3 || !islower(cline[0]) ||
Jonas Olssonec9e4922018-09-05 09:53:49 +0200504 cline[1] != kSdpDelimiterEqualChar ||
505 (cline[0] != kLineTypeSessionName &&
506 cline[2] == kSdpDelimiterSpaceChar)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000507 *pos = line_begin;
508 return false;
509 }
510 return true;
511}
512
513// Init |os| to "|type|=|value|".
514static void InitLine(const char type,
515 const std::string& value,
Jonas Olssonec9e4922018-09-05 09:53:49 +0200516 rtc::StringBuilder* os) {
517 os->Clear();
518 *os << std::string(1, type) << kSdpDelimiterEqual << value;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000519}
520
521// Init |os| to "a=|attribute|".
Jonas Olssonec9e4922018-09-05 09:53:49 +0200522static void InitAttrLine(const std::string& attribute, rtc::StringBuilder* os) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000523 InitLine(kLineTypeAttributes, attribute, os);
524}
525
526// Writes a SDP attribute line based on |attribute| and |value| to |message|.
Yves Gerey665174f2018-06-19 15:03:05 +0200527static void AddAttributeLine(const std::string& attribute,
528 int value,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000529 std::string* message) {
Jonas Olssonec9e4922018-09-05 09:53:49 +0200530 rtc::StringBuilder os;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000531 InitAttrLine(attribute, &os);
532 os << kSdpDelimiterColon << value;
533 AddLine(os.str(), message);
534}
535
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000536static bool IsLineType(const std::string& message,
537 const char type,
538 size_t line_start) {
539 if (message.size() < line_start + kLinePrefixLength) {
540 return false;
541 }
542 const char* cmessage = message.c_str();
543 return (cmessage[line_start] == type &&
Jonas Olssonec9e4922018-09-05 09:53:49 +0200544 cmessage[line_start + 1] == kSdpDelimiterEqualChar);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000545}
546
Yves Gerey665174f2018-06-19 15:03:05 +0200547static bool IsLineType(const std::string& line, const char type) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000548 return IsLineType(line, type, 0);
549}
550
Yves Gerey665174f2018-06-19 15:03:05 +0200551static bool GetLineWithType(const std::string& message,
552 size_t* pos,
553 std::string* line,
554 const char type) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000555 if (!IsLineType(message, type, *pos)) {
556 return false;
557 }
558
559 if (!GetLine(message, pos, line))
560 return false;
561
562 return true;
563}
564
565static bool HasAttribute(const std::string& line,
566 const std::string& attribute) {
Johannes Kron211856b2018-09-06 12:12:28 +0200567 if (line.compare(kLinePrefixLength, attribute.size(), attribute) == 0) {
568 // Make sure that the match is not only a partial match. If length of
569 // strings doesn't match, the next character of the line must be ':' or ' '.
570 // This function is also used for media descriptions (e.g., "m=audio 9..."),
571 // hence the need to also allow space in the end.
572 RTC_CHECK_LE(kLinePrefixLength + attribute.size(), line.size());
573 if ((kLinePrefixLength + attribute.size()) == line.size() ||
574 line[kLinePrefixLength + attribute.size()] == kSdpDelimiterColonChar ||
575 line[kLinePrefixLength + attribute.size()] == kSdpDelimiterSpaceChar) {
576 return true;
577 }
578 }
579 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000580}
581
Peter Boström0c4e06b2015-10-07 12:23:21 +0200582static bool AddSsrcLine(uint32_t ssrc_id,
583 const std::string& attribute,
584 const std::string& value,
585 std::string* message) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000586 // RFC 5576
587 // a=ssrc:<ssrc-id> <attribute>:<value>
Jonas Olssonec9e4922018-09-05 09:53:49 +0200588 rtc::StringBuilder os;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000589 InitAttrLine(kAttributeSsrc, &os);
Yves Gerey665174f2018-06-19 15:03:05 +0200590 os << kSdpDelimiterColon << ssrc_id << kSdpDelimiterSpace << attribute
591 << kSdpDelimiterColon << value;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000592 return AddLine(os.str(), message);
593}
594
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000595// Get value only from <attribute>:<value>.
Yves Gerey665174f2018-06-19 15:03:05 +0200596static bool GetValue(const std::string& message,
597 const std::string& attribute,
598 std::string* value,
599 SdpParseError* error) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000600 std::string leftpart;
Jonas Olssonec9e4922018-09-05 09:53:49 +0200601 if (!rtc::tokenize_first(message, kSdpDelimiterColonChar, &leftpart, value)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000602 return ParseFailedGetValue(message, attribute, error);
603 }
604 // The left part should end with the expected attribute.
605 if (leftpart.length() < attribute.length() ||
606 leftpart.compare(leftpart.length() - attribute.length(),
607 attribute.length(), attribute) != 0) {
608 return ParseFailedGetValue(message, attribute, error);
609 }
610 return true;
611}
612
613static bool CaseInsensitiveFind(std::string str1, std::string str2) {
Yves Gerey665174f2018-06-19 15:03:05 +0200614 std::transform(str1.begin(), str1.end(), str1.begin(), ::tolower);
615 std::transform(str2.begin(), str2.end(), str2.begin(), ::tolower);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000616 return str1.find(str2) != std::string::npos;
617}
618
wu@webrtc.org5e760e72014-04-02 23:19:09 +0000619template <class T>
620static bool GetValueFromString(const std::string& line,
621 const std::string& s,
622 T* t,
623 SdpParseError* error) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000624 if (!rtc::FromString(s, t)) {
Jonas Olssonec9e4922018-09-05 09:53:49 +0200625 rtc::StringBuilder description;
wu@webrtc.org5e760e72014-04-02 23:19:09 +0000626 description << "Invalid value: " << s << ".";
627 return ParseFailed(line, description.str(), error);
628 }
629 return true;
630}
631
pkasting@chromium.orge9facf82015-02-17 20:36:28 +0000632static bool GetPayloadTypeFromString(const std::string& line,
633 const std::string& s,
634 int* payload_type,
635 SdpParseError* error) {
636 return GetValueFromString(line, s, payload_type, error) &&
Yves Gerey665174f2018-06-19 15:03:05 +0200637 cricket::IsValidRtpPayloadType(*payload_type);
pkasting@chromium.orge9facf82015-02-17 20:36:28 +0000638}
639
Seth Hampson5897a6e2018-04-03 11:16:33 -0700640// Creates a StreamParams track in the case when no SSRC lines are signaled.
641// This is a track that does not contain SSRCs and only contains
642// stream_ids/track_id if it's signaled with a=msid lines.
643void CreateTrackWithNoSsrcs(const std::vector<std::string>& msid_stream_ids,
644 const std::string& msid_track_id,
Amit Hilbuchc57d5732018-12-11 15:30:11 -0800645 const std::vector<RidDescription>& rids,
Seth Hampson5897a6e2018-04-03 11:16:33 -0700646 StreamParamsVec* tracks) {
647 StreamParams track;
648 if (msid_track_id.empty() || msid_stream_ids.empty()) {
649 // We only create an unsignaled track if a=msid lines were signaled.
650 return;
651 }
652 track.set_stream_ids(msid_stream_ids);
653 track.id = msid_track_id;
Amit Hilbuchc57d5732018-12-11 15:30:11 -0800654 track.set_rids(rids);
Seth Hampson5897a6e2018-04-03 11:16:33 -0700655 tracks->push_back(track);
656}
657
658// Creates the StreamParams tracks, for the case when SSRC lines are signaled.
659// |msid_stream_ids| and |msid_track_id| represent the stream/track ID from the
Taylor Brandstetter5de6b752016-03-09 17:02:30 -0800660// "a=msid" attribute, if it exists. They are empty if the attribute does not
Seth Hampson5897a6e2018-04-03 11:16:33 -0700661// exist. We prioritize getting stream_ids/track_ids signaled in a=msid lines.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000662void CreateTracksFromSsrcInfos(const SsrcInfoVec& ssrc_infos,
Seth Hampson5b4f0752018-04-02 16:31:36 -0700663 const std::vector<std::string>& msid_stream_ids,
Taylor Brandstetter5de6b752016-03-09 17:02:30 -0800664 const std::string& msid_track_id,
Seth Hampson5b4f0752018-04-02 16:31:36 -0700665 StreamParamsVec* tracks,
666 int msid_signaling) {
nisseede5da42017-01-12 05:15:36 -0800667 RTC_DCHECK(tracks != NULL);
Steve Anton4daf66e2018-09-07 14:55:53 -0700668 for (const SsrcInfo& ssrc_info : ssrc_infos) {
669 if (ssrc_info.cname.empty()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000670 continue;
671 }
672
Seth Hampson5b4f0752018-04-02 16:31:36 -0700673 std::vector<std::string> stream_ids;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000674 std::string track_id;
Seth Hampson5b4f0752018-04-02 16:31:36 -0700675 if (msid_signaling & cricket::kMsidSignalingMediaSection) {
676 // This is the case with Unified Plan SDP msid signaling.
677 stream_ids = msid_stream_ids;
Tomas Gunnarsson191bf5c2018-03-30 10:44:43 +0000678 track_id = msid_track_id;
Seth Hampson5b4f0752018-04-02 16:31:36 -0700679 } else if (msid_signaling & cricket::kMsidSignalingSsrcAttribute) {
680 // This is the case with Plan B SDP msid signaling.
Steve Anton4daf66e2018-09-07 14:55:53 -0700681 stream_ids.push_back(ssrc_info.stream_id);
682 track_id = ssrc_info.track_id;
683 } else if (!ssrc_info.mslabel.empty()) {
Seth Hampson5b4f0752018-04-02 16:31:36 -0700684 // Since there's no a=msid or a=ssrc msid signaling, this is a sdp from
685 // an older version of client that doesn't support msid.
686 // In that case, we use the mslabel and label to construct the track.
Steve Anton4daf66e2018-09-07 14:55:53 -0700687 stream_ids.push_back(ssrc_info.mslabel);
688 track_id = ssrc_info.label;
Seth Hampson5b4f0752018-04-02 16:31:36 -0700689 } else {
690 // Since no media streams isn't supported with older SDP signaling, we
691 // use a default a stream id.
692 stream_ids.push_back(kDefaultMsid);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000693 }
Seth Hampson5b4f0752018-04-02 16:31:36 -0700694 // If a track ID wasn't populated from the SSRC attributes OR the
Taylor Brandstetter5de6b752016-03-09 17:02:30 -0800695 // msid attribute, use default/random values.
Taylor Brandstetter5de6b752016-03-09 17:02:30 -0800696 if (track_id.empty()) {
697 // TODO(ronghuawu): What should we do if the track id doesn't appear?
698 // Create random string (which will be used as track label later)?
699 track_id = rtc::CreateRandomString(8);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000700 }
701
Steve Anton4daf66e2018-09-07 14:55:53 -0700702 auto track_it = std::find_if(
703 tracks->begin(), tracks->end(),
704 [track_id](const StreamParams& track) { return track.id == track_id; });
705 if (track_it == tracks->end()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000706 // If we don't find an existing track, create a new one.
707 tracks->push_back(StreamParams());
Steve Anton4daf66e2018-09-07 14:55:53 -0700708 track_it = tracks->end() - 1;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000709 }
Steve Anton4daf66e2018-09-07 14:55:53 -0700710 StreamParams& track = *track_it;
711 track.add_ssrc(ssrc_info.ssrc_id);
712 track.cname = ssrc_info.cname;
713 track.set_stream_ids(stream_ids);
714 track.id = track_id;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000715 }
716}
717
Seth Hampson845e8782018-03-02 11:34:10 -0800718void GetMediaStreamIds(const ContentInfo* content,
719 std::set<std::string>* labels) {
Steve Antonb1c1de12017-12-21 15:14:30 -0800720 for (const StreamParams& stream_params :
721 content->media_description()->streams()) {
Seth Hampson845e8782018-03-02 11:34:10 -0800722 for (const std::string& stream_id : stream_params.stream_ids()) {
723 labels->insert(stream_id);
Steve Anton5a26a3a2018-02-28 11:38:47 -0800724 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000725 }
726}
727
728// RFC 5245
729// It is RECOMMENDED that default candidates be chosen based on the
730// likelihood of those candidates to work with the peer that is being
731// contacted. It is RECOMMENDED that relayed > reflexive > host.
732static const int kPreferenceUnknown = 0;
733static const int kPreferenceHost = 1;
734static const int kPreferenceReflexive = 2;
735static const int kPreferenceRelayed = 3;
736
737static int GetCandidatePreferenceFromType(const std::string& type) {
738 int preference = kPreferenceUnknown;
739 if (type == cricket::LOCAL_PORT_TYPE) {
740 preference = kPreferenceHost;
741 } else if (type == cricket::STUN_PORT_TYPE) {
742 preference = kPreferenceReflexive;
743 } else if (type == cricket::RELAY_PORT_TYPE) {
744 preference = kPreferenceRelayed;
745 } else {
nissec80e7412017-01-11 05:56:46 -0800746 RTC_NOTREACHED();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000747 }
748 return preference;
749}
750
guoweis@webrtc.org57ac2c82015-02-06 00:45:13 +0000751// Get ip and port of the default destination from the |candidates| with the
752// given value of |component_id|. The default candidate should be the one most
753// likely to work, typically IPv4 relay.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000754// RFC 5245
755// The value of |component_id| currently supported are 1 (RTP) and 2 (RTCP).
Steve Anton36b29d12017-10-30 09:57:42 -0700756// TODO(deadbeef): Decide the default destination in webrtcsession and
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000757// pass it down via SessionDescription.
Yves Gerey665174f2018-06-19 15:03:05 +0200758static void GetDefaultDestination(const std::vector<Candidate>& candidates,
759 int component_id,
760 std::string* port,
761 std::string* ip,
762 std::string* addr_type) {
perkj@webrtc.orgd105cc82014-11-07 11:22:06 +0000763 *addr_type = kConnectionIpv4Addrtype;
pthatcher@webrtc.orgc9d6d142014-10-23 23:37:22 +0000764 *port = kDummyPort;
765 *ip = kDummyAddress;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000766 int current_preference = kPreferenceUnknown;
guoweis@webrtc.org57ac2c82015-02-06 00:45:13 +0000767 int current_family = AF_UNSPEC;
Steve Anton4daf66e2018-09-07 14:55:53 -0700768 for (const Candidate& candidate : candidates) {
769 if (candidate.component() != component_id) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000770 continue;
771 }
guoweis@webrtc.org57ac2c82015-02-06 00:45:13 +0000772 // Default destination should be UDP only.
Steve Anton4daf66e2018-09-07 14:55:53 -0700773 if (candidate.protocol() != cricket::UDP_PROTOCOL_NAME) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000774 continue;
775 }
Steve Anton4daf66e2018-09-07 14:55:53 -0700776 const int preference = GetCandidatePreferenceFromType(candidate.type());
777 const int family = candidate.address().ipaddr().family();
guoweis@webrtc.org57ac2c82015-02-06 00:45:13 +0000778 // See if this candidate is more preferable then the current one if it's the
779 // same family. Or if the current family is IPv4 already so we could safely
780 // ignore all IPv6 ones. WebRTC bug 4269.
781 // http://code.google.com/p/webrtc/issues/detail?id=4269
782 if ((preference <= current_preference && current_family == family) ||
783 (current_family == AF_INET && family == AF_INET6)) {
784 continue;
785 }
pthatcher@webrtc.orgc9d6d142014-10-23 23:37:22 +0000786 if (family == AF_INET) {
787 addr_type->assign(kConnectionIpv4Addrtype);
788 } else if (family == AF_INET6) {
789 addr_type->assign(kConnectionIpv6Addrtype);
790 }
guoweis@webrtc.org57ac2c82015-02-06 00:45:13 +0000791 current_preference = preference;
792 current_family = family;
Steve Anton4daf66e2018-09-07 14:55:53 -0700793 *port = candidate.address().PortAsString();
794 *ip = candidate.address().ipaddr().ToString();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000795 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000796}
797
wu@webrtc.org4c3e9912014-07-16 21:03:13 +0000798// Gets "a=rtcp" line if found default RTCP candidate from |candidates|.
799static std::string GetRtcpLine(const std::vector<Candidate>& candidates) {
pthatcher@webrtc.orgc9d6d142014-10-23 23:37:22 +0000800 std::string rtcp_line, rtcp_port, rtcp_ip, addr_type;
Yves Gerey665174f2018-06-19 15:03:05 +0200801 GetDefaultDestination(candidates, ICE_CANDIDATE_COMPONENT_RTCP, &rtcp_port,
802 &rtcp_ip, &addr_type);
pthatcher@webrtc.orgc9d6d142014-10-23 23:37:22 +0000803 // Found default RTCP candidate.
804 // RFC 5245
805 // If the agent is utilizing RTCP, it MUST encode the RTCP candidate
806 // using the a=rtcp attribute as defined in RFC 3605.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000807
pthatcher@webrtc.orgc9d6d142014-10-23 23:37:22 +0000808 // RFC 3605
809 // rtcp-attribute = "a=rtcp:" port [nettype space addrtype space
810 // connection-address] CRLF
Jonas Olssonec9e4922018-09-05 09:53:49 +0200811 rtc::StringBuilder os;
pthatcher@webrtc.orgc9d6d142014-10-23 23:37:22 +0000812 InitAttrLine(kAttributeRtcp, &os);
Yves Gerey665174f2018-06-19 15:03:05 +0200813 os << kSdpDelimiterColon << rtcp_port << " " << kConnectionNettype << " "
814 << addr_type << " " << rtcp_ip;
pthatcher@webrtc.orgc9d6d142014-10-23 23:37:22 +0000815 rtcp_line = os.str();
wu@webrtc.org4c3e9912014-07-16 21:03:13 +0000816 return rtcp_line;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000817}
818
819// Get candidates according to the mline index from SessionDescriptionInterface.
820static void GetCandidatesByMindex(const SessionDescriptionInterface& desci,
821 int mline_index,
822 std::vector<Candidate>* candidates) {
823 if (!candidates) {
824 return;
825 }
826 const IceCandidateCollection* cc = desci.candidates(mline_index);
827 for (size_t i = 0; i < cc->count(); ++i) {
828 const IceCandidateInterface* candidate = cc->at(i);
829 candidates->push_back(candidate->candidate());
830 }
831}
832
deadbeef90f1e1e2017-02-10 12:35:05 -0800833static bool IsValidPort(int port) {
834 return port >= 0 && port <= 65535;
835}
836
Steve Antone831b8c2018-02-01 12:22:16 -0800837std::string SdpSerialize(const JsepSessionDescription& jdesc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000838 const cricket::SessionDescription* desc = jdesc.description();
839 if (!desc) {
840 return "";
841 }
842
843 std::string message;
844
845 // Session Description.
846 AddLine(kSessionVersion, &message);
847 // Session Origin
848 // RFC 4566
849 // o=<username> <sess-id> <sess-version> <nettype> <addrtype>
850 // <unicast-address>
Jonas Olssonec9e4922018-09-05 09:53:49 +0200851 rtc::StringBuilder os;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000852 InitLine(kLineTypeOrigin, kSessionOriginUsername, &os);
Yves Gerey665174f2018-06-19 15:03:05 +0200853 const std::string& session_id =
854 jdesc.session_id().empty() ? kSessionOriginSessionId : jdesc.session_id();
855 const std::string& session_version = jdesc.session_version().empty()
856 ? kSessionOriginSessionVersion
857 : jdesc.session_version();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000858 os << " " << session_id << " " << session_version << " "
859 << kSessionOriginNettype << " " << kSessionOriginAddrtype << " "
860 << kSessionOriginAddress;
861 AddLine(os.str(), &message);
862 AddLine(kSessionName, &message);
863
864 // Time Description.
865 AddLine(kTimeDescription, &message);
866
867 // Group
868 if (desc->HasGroup(cricket::GROUP_TYPE_BUNDLE)) {
869 std::string group_line = kAttrGroup;
870 const cricket::ContentGroup* group =
871 desc->GetGroupByName(cricket::GROUP_TYPE_BUNDLE);
nisseede5da42017-01-12 05:15:36 -0800872 RTC_DCHECK(group != NULL);
Steve Anton4daf66e2018-09-07 14:55:53 -0700873 for (const std::string& content_name : group->content_names()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000874 group_line.append(" ");
Steve Anton4daf66e2018-09-07 14:55:53 -0700875 group_line.append(content_name);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000876 }
877 AddLine(group_line, &message);
878 }
879
Johannes Kron0854eb62018-10-10 22:33:20 +0200880 // Mixed one- and two-byte header extension.
Johannes Kron9581bc42018-10-23 10:17:39 +0200881 if (desc->extmap_allow_mixed()) {
Johannes Kron0854eb62018-10-10 22:33:20 +0200882 InitAttrLine(kAttributeExtmapAllowMixed, &os);
883 AddLine(os.str(), &message);
884 }
885
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000886 // MediaStream semantics
887 InitAttrLine(kAttributeMsidSemantics, &os);
888 os << kSdpDelimiterColon << " " << kMediaStreamSemantic;
wu@webrtc.org4c3e9912014-07-16 21:03:13 +0000889
Seth Hampson845e8782018-03-02 11:34:10 -0800890 std::set<std::string> media_stream_ids;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000891 const ContentInfo* audio_content = GetFirstAudioContent(desc);
892 if (audio_content)
Seth Hampson845e8782018-03-02 11:34:10 -0800893 GetMediaStreamIds(audio_content, &media_stream_ids);
wu@webrtc.org4c3e9912014-07-16 21:03:13 +0000894
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000895 const ContentInfo* video_content = GetFirstVideoContent(desc);
896 if (video_content)
Seth Hampson845e8782018-03-02 11:34:10 -0800897 GetMediaStreamIds(video_content, &media_stream_ids);
wu@webrtc.org4c3e9912014-07-16 21:03:13 +0000898
Steve Anton4daf66e2018-09-07 14:55:53 -0700899 for (const std::string& id : media_stream_ids) {
900 os << " " << id;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000901 }
902 AddLine(os.str(), &message);
903
Taylor Brandstetter2f65ec52018-05-24 11:37:28 -0700904 // a=ice-lite
905 //
906 // TODO(deadbeef): It's weird that we need to iterate TransportInfos for
907 // this, when it's a session-level attribute. It really should be moved to a
908 // session-level structure like SessionDescription.
909 for (const cricket::TransportInfo& transport : desc->transport_infos()) {
910 if (transport.description.ice_mode == cricket::ICEMODE_LITE) {
911 InitAttrLine(kAttributeIceLite, &os);
912 AddLine(os.str(), &message);
913 break;
914 }
915 }
916
wu@webrtc.org4c3e9912014-07-16 21:03:13 +0000917 // Preserve the order of the media contents.
918 int mline_index = -1;
Steve Anton4daf66e2018-09-07 14:55:53 -0700919 for (const ContentInfo& content : desc->contents()) {
wu@webrtc.org4c3e9912014-07-16 21:03:13 +0000920 std::vector<Candidate> candidates;
921 GetCandidatesByMindex(jdesc, ++mline_index, &candidates);
Steve Anton4daf66e2018-09-07 14:55:53 -0700922 BuildMediaDescription(&content, desc->GetTransportInfoByName(content.name),
923 content.media_description()->type(), candidates,
924 desc->msid_signaling(), &message);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000925 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000926 return message;
927}
928
929// Serializes the passed in IceCandidateInterface to a SDP string.
930// candidate - The candidate to be serialized.
Honghai Zhang7fb69db2016-03-14 11:59:18 -0700931std::string SdpSerializeCandidate(const IceCandidateInterface& candidate) {
932 return SdpSerializeCandidate(candidate.candidate());
933}
934
935// Serializes a cricket Candidate.
936std::string SdpSerializeCandidate(const cricket::Candidate& candidate) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000937 std::string message;
Honghai Zhang7fb69db2016-03-14 11:59:18 -0700938 std::vector<cricket::Candidate> candidates(1, candidate);
honghaiza54a0802015-12-16 18:37:23 -0800939 BuildCandidate(candidates, true, &message);
wu@webrtc.orgec9f5fb2014-06-24 17:05:10 +0000940 // From WebRTC draft section 4.8.1.1 candidate-attribute will be
941 // just candidate:<candidate> not a=candidate:<blah>CRLF
nisseede5da42017-01-12 05:15:36 -0800942 RTC_DCHECK(message.find("a=") == 0);
wu@webrtc.orgec9f5fb2014-06-24 17:05:10 +0000943 message.erase(0, 2);
nisseede5da42017-01-12 05:15:36 -0800944 RTC_DCHECK(message.find(kLineBreak) == message.size() - 2);
wu@webrtc.orgec9f5fb2014-06-24 17:05:10 +0000945 message.resize(message.size() - 2);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000946 return message;
947}
948
949bool SdpDeserialize(const std::string& message,
950 JsepSessionDescription* jdesc,
951 SdpParseError* error) {
952 std::string session_id;
953 std::string session_version;
Peter Thatcher7cbd1882015-09-17 18:54:52 -0700954 TransportDescription session_td("", "");
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000955 RtpHeaderExtensions session_extmaps;
zhihuang38989e52017-03-21 11:04:53 -0700956 rtc::SocketAddress session_connection_addr;
Steve Anton5a1de872018-12-18 11:25:55 -0800957 auto desc = absl::make_unique<cricket::SessionDescription>();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000958 size_t current_pos = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000959
960 // Session Description
961 if (!ParseSessionDescription(message, &current_pos, &session_id,
deadbeefc80741f2015-10-22 13:14:45 -0700962 &session_version, &session_td, &session_extmaps,
Steve Anton5a1de872018-12-18 11:25:55 -0800963 &session_connection_addr, desc.get(), error)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000964 return false;
965 }
966
967 // Media Description
Steve Anton5a1de872018-12-18 11:25:55 -0800968 std::vector<std::unique_ptr<JsepIceCandidate>> candidates;
deadbeefc80741f2015-10-22 13:14:45 -0700969 if (!ParseMediaDescription(message, session_td, session_extmaps, &current_pos,
Steve Anton5a1de872018-12-18 11:25:55 -0800970 session_connection_addr, desc.get(), &candidates,
zhihuang38989e52017-03-21 11:04:53 -0700971 error)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000972 return false;
973 }
974
Steve Anton5a1de872018-12-18 11:25:55 -0800975 jdesc->Initialize(desc.release(), session_id, session_version);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000976
Steve Anton5a1de872018-12-18 11:25:55 -0800977 for (const auto& candidate : candidates) {
978 jdesc->AddCandidate(candidate.get());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000979 }
980 return true;
981}
982
983bool SdpDeserializeCandidate(const std::string& message,
984 JsepIceCandidate* jcandidate,
985 SdpParseError* error) {
nisseede5da42017-01-12 05:15:36 -0800986 RTC_DCHECK(jcandidate != NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000987 Candidate candidate;
988 if (!ParseCandidate(message, &candidate, error, true)) {
989 return false;
990 }
991 jcandidate->SetCandidate(candidate);
992 return true;
993}
994
Honghai Zhang7fb69db2016-03-14 11:59:18 -0700995bool SdpDeserializeCandidate(const std::string& transport_name,
996 const std::string& message,
997 cricket::Candidate* candidate,
998 SdpParseError* error) {
nisseede5da42017-01-12 05:15:36 -0800999 RTC_DCHECK(candidate != nullptr);
Honghai Zhang7fb69db2016-03-14 11:59:18 -07001000 if (!ParseCandidate(message, candidate, error, true)) {
1001 return false;
1002 }
1003 candidate->set_transport_name(transport_name);
1004 return true;
1005}
1006
Yves Gerey665174f2018-06-19 15:03:05 +02001007bool ParseCandidate(const std::string& message,
1008 Candidate* candidate,
1009 SdpParseError* error,
1010 bool is_raw) {
nisseede5da42017-01-12 05:15:36 -08001011 RTC_DCHECK(candidate != NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001012
1013 // Get the first line from |message|.
jiayl@webrtc.org7ec3f9f2014-08-08 23:09:15 +00001014 std::string first_line = message;
1015 size_t pos = 0;
1016 GetLine(message, &pos, &first_line);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001017
jiayl@webrtc.org7ec3f9f2014-08-08 23:09:15 +00001018 // Makes sure |message| contains only one line.
1019 if (message.size() > first_line.size()) {
1020 std::string left, right;
Jonas Olssonec9e4922018-09-05 09:53:49 +02001021 if (rtc::tokenize_first(message, kNewLineChar, &left, &right) &&
Donald Curtis0e07f922015-05-15 09:21:23 -07001022 !right.empty()) {
jiayl@webrtc.org7ec3f9f2014-08-08 23:09:15 +00001023 return ParseFailed(message, 0, "Expect one line only", error);
1024 }
1025 }
1026
1027 // From WebRTC draft section 4.8.1.1 candidate-attribute should be
1028 // candidate:<candidate> when trickled, but we still support
1029 // a=candidate:<blah>CRLF for backward compatibility and for parsing a line
1030 // from the SDP.
1031 if (IsLineType(first_line, kLineTypeAttributes)) {
1032 first_line = first_line.substr(kLinePrefixLength);
1033 }
1034
1035 std::string attribute_candidate;
1036 std::string candidate_value;
1037
1038 // |first_line| must be in the form of "candidate:<value>".
Jonas Olssonec9e4922018-09-05 09:53:49 +02001039 if (!rtc::tokenize_first(first_line, kSdpDelimiterColonChar,
1040 &attribute_candidate, &candidate_value) ||
jiayl@webrtc.org7ec3f9f2014-08-08 23:09:15 +00001041 attribute_candidate != kAttributeCandidate) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001042 if (is_raw) {
Jonas Olssonec9e4922018-09-05 09:53:49 +02001043 rtc::StringBuilder description;
Yves Gerey665174f2018-06-19 15:03:05 +02001044 description << "Expect line: " << kAttributeCandidate << ":"
1045 << "<candidate-str>";
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001046 return ParseFailed(first_line, 0, description.str(), error);
1047 } else {
1048 return ParseFailedExpectLine(first_line, 0, kLineTypeAttributes,
1049 kAttributeCandidate, error);
1050 }
1051 }
1052
1053 std::vector<std::string> fields;
Jonas Olssonec9e4922018-09-05 09:53:49 +02001054 rtc::split(candidate_value, kSdpDelimiterSpaceChar, &fields);
jiayl@webrtc.org7ec3f9f2014-08-08 23:09:15 +00001055
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001056 // RFC 5245
1057 // a=candidate:<foundation> <component-id> <transport> <priority>
1058 // <connection-address> <port> typ <candidate-types>
1059 // [raddr <connection-address>] [rport <port>]
1060 // *(SP extension-att-name SP extension-att-value)
1061 const size_t expected_min_fields = 8;
1062 if (fields.size() < expected_min_fields ||
1063 (fields[6] != kAttributeCandidateTyp)) {
1064 return ParseFailedExpectMinFieldNum(first_line, expected_min_fields, error);
1065 }
jbauch083b73f2015-07-16 02:46:32 -07001066 const std::string& foundation = fields[0];
jiayl@webrtc.org7ec3f9f2014-08-08 23:09:15 +00001067
wu@webrtc.org5e760e72014-04-02 23:19:09 +00001068 int component_id = 0;
1069 if (!GetValueFromString(first_line, fields[1], &component_id, error)) {
1070 return false;
1071 }
jbauch083b73f2015-07-16 02:46:32 -07001072 const std::string& transport = fields[2];
Peter Boström0c4e06b2015-10-07 12:23:21 +02001073 uint32_t priority = 0;
wu@webrtc.org5e760e72014-04-02 23:19:09 +00001074 if (!GetValueFromString(first_line, fields[3], &priority, error)) {
1075 return false;
1076 }
jbauch083b73f2015-07-16 02:46:32 -07001077 const std::string& connection_address = fields[4];
wu@webrtc.org5e760e72014-04-02 23:19:09 +00001078 int port = 0;
1079 if (!GetValueFromString(first_line, fields[5], &port, error)) {
1080 return false;
1081 }
deadbeef90f1e1e2017-02-10 12:35:05 -08001082 if (!IsValidPort(port)) {
1083 return ParseFailed(first_line, "Invalid port number.", error);
1084 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001085 SocketAddress address(connection_address, port);
1086
1087 cricket::ProtocolType protocol;
hnslb68cc752016-12-13 10:33:41 -08001088 if (!StringToProto(transport.c_str(), &protocol)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001089 return ParseFailed(first_line, "Unsupported transport type.", error);
1090 }
hnslb68cc752016-12-13 10:33:41 -08001091 switch (protocol) {
1092 case cricket::PROTO_UDP:
1093 case cricket::PROTO_TCP:
1094 case cricket::PROTO_SSLTCP:
1095 // Supported protocol.
1096 break;
1097 default:
1098 return ParseFailed(first_line, "Unsupported transport type.", error);
1099 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001100
1101 std::string candidate_type;
jbauch083b73f2015-07-16 02:46:32 -07001102 const std::string& type = fields[7];
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001103 if (type == kCandidateHost) {
1104 candidate_type = cricket::LOCAL_PORT_TYPE;
1105 } else if (type == kCandidateSrflx) {
1106 candidate_type = cricket::STUN_PORT_TYPE;
1107 } else if (type == kCandidateRelay) {
1108 candidate_type = cricket::RELAY_PORT_TYPE;
Honghai Zhang7fb69db2016-03-14 11:59:18 -07001109 } else if (type == kCandidatePrflx) {
1110 candidate_type = cricket::PRFLX_PORT_TYPE;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001111 } else {
1112 return ParseFailed(first_line, "Unsupported candidate type.", error);
1113 }
1114
1115 size_t current_position = expected_min_fields;
1116 SocketAddress related_address;
1117 // The 2 optional fields for related address
1118 // [raddr <connection-address>] [rport <port>]
1119 if (fields.size() >= (current_position + 2) &&
1120 fields[current_position] == kAttributeCandidateRaddr) {
1121 related_address.SetIP(fields[++current_position]);
1122 ++current_position;
1123 }
1124 if (fields.size() >= (current_position + 2) &&
1125 fields[current_position] == kAttributeCandidateRport) {
wu@webrtc.org5e760e72014-04-02 23:19:09 +00001126 int port = 0;
Yves Gerey665174f2018-06-19 15:03:05 +02001127 if (!GetValueFromString(first_line, fields[++current_position], &port,
1128 error)) {
wu@webrtc.org5e760e72014-04-02 23:19:09 +00001129 return false;
1130 }
deadbeef90f1e1e2017-02-10 12:35:05 -08001131 if (!IsValidPort(port)) {
1132 return ParseFailed(first_line, "Invalid port number.", error);
1133 }
wu@webrtc.org5e760e72014-04-02 23:19:09 +00001134 related_address.SetPort(port);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001135 ++current_position;
1136 }
1137
mallinath@webrtc.org2d60c5e2014-08-08 22:29:20 +00001138 // If this is a TCP candidate, it has additional extension as defined in
1139 // RFC 6544.
1140 std::string tcptype;
1141 if (fields.size() >= (current_position + 2) &&
1142 fields[current_position] == kTcpCandidateType) {
1143 tcptype = fields[++current_position];
1144 ++current_position;
1145
1146 if (tcptype != cricket::TCPTYPE_ACTIVE_STR &&
1147 tcptype != cricket::TCPTYPE_PASSIVE_STR &&
1148 tcptype != cricket::TCPTYPE_SIMOPEN_STR) {
1149 return ParseFailed(first_line, "Invalid TCP candidate type.", error);
1150 }
1151
1152 if (protocol != cricket::PROTO_TCP) {
1153 return ParseFailed(first_line, "Invalid non-TCP candidate", error);
1154 }
1155 }
1156
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001157 // Extension
honghaiza54a0802015-12-16 18:37:23 -08001158 // Though non-standard, we support the ICE ufrag and pwd being signaled on
1159 // the candidate to avoid issues with confusing which generation a candidate
1160 // belongs to when trickling multiple generations at the same time.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001161 std::string username;
1162 std::string password;
Peter Boström0c4e06b2015-10-07 12:23:21 +02001163 uint32_t generation = 0;
honghaiza0c44ea2016-03-23 16:07:48 -07001164 uint16_t network_id = 0;
1165 uint16_t network_cost = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001166 for (size_t i = current_position; i + 1 < fields.size(); ++i) {
1167 // RFC 5245
1168 // *(SP extension-att-name SP extension-att-value)
1169 if (fields[i] == kAttributeCandidateGeneration) {
wu@webrtc.org5e760e72014-04-02 23:19:09 +00001170 if (!GetValueFromString(first_line, fields[++i], &generation, error)) {
1171 return false;
1172 }
honghaiza54a0802015-12-16 18:37:23 -08001173 } else if (fields[i] == kAttributeCandidateUfrag) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001174 username = fields[++i];
honghaiza54a0802015-12-16 18:37:23 -08001175 } else if (fields[i] == kAttributeCandidatePwd) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001176 password = fields[++i];
honghaiza0c44ea2016-03-23 16:07:48 -07001177 } else if (fields[i] == kAttributeCandidateNetworkId) {
1178 if (!GetValueFromString(first_line, fields[++i], &network_id, error)) {
1179 return false;
1180 }
honghaize1a0c942016-02-16 14:54:56 -08001181 } else if (fields[i] == kAttributeCandidateNetworkCost) {
1182 if (!GetValueFromString(first_line, fields[++i], &network_cost, error)) {
1183 return false;
1184 }
Honghai Zhang351d77b2016-05-20 15:08:29 -07001185 network_cost = std::min(network_cost, rtc::kNetworkCostMax);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001186 } else {
1187 // Skip the unknown extension.
1188 ++i;
1189 }
1190 }
1191
guoweis@webrtc.org61c12472015-01-15 06:53:07 +00001192 *candidate = Candidate(component_id, cricket::ProtoToString(protocol),
guoweis@webrtc.org950c5182014-12-16 23:01:31 +00001193 address, priority, username, password, candidate_type,
honghaiza0c44ea2016-03-23 16:07:48 -07001194 generation, foundation, network_id, network_cost);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001195 candidate->set_related_address(related_address);
mallinath@webrtc.org2d60c5e2014-08-08 22:29:20 +00001196 candidate->set_tcptype(tcptype);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001197 return true;
1198}
1199
1200bool ParseIceOptions(const std::string& line,
1201 std::vector<std::string>* transport_options,
1202 SdpParseError* error) {
1203 std::string ice_options;
1204 if (!GetValue(line, kAttributeIceOption, &ice_options, error)) {
1205 return false;
1206 }
1207 std::vector<std::string> fields;
Jonas Olssonec9e4922018-09-05 09:53:49 +02001208 rtc::split(ice_options, kSdpDelimiterSpaceChar, &fields);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001209 for (size_t i = 0; i < fields.size(); ++i) {
1210 transport_options->push_back(fields[i]);
1211 }
1212 return true;
1213}
1214
jiayl@webrtc.orgddb85ab2014-09-05 16:31:56 +00001215bool ParseSctpPort(const std::string& line,
1216 int* sctp_port,
1217 SdpParseError* error) {
1218 // draft-ietf-mmusic-sctp-sdp-07
1219 // a=sctp-port
1220 std::vector<std::string> fields;
jiayl@webrtc.orgddb85ab2014-09-05 16:31:56 +00001221 const size_t expected_min_fields = 2;
Jonas Olssonec9e4922018-09-05 09:53:49 +02001222 rtc::split(line.substr(kLinePrefixLength), kSdpDelimiterColonChar, &fields);
lally69f57602015-10-08 10:15:04 -07001223 if (fields.size() < expected_min_fields) {
1224 fields.resize(0);
Jonas Olssonec9e4922018-09-05 09:53:49 +02001225 rtc::split(line.substr(kLinePrefixLength), kSdpDelimiterSpaceChar, &fields);
lally69f57602015-10-08 10:15:04 -07001226 }
jiayl@webrtc.orgddb85ab2014-09-05 16:31:56 +00001227 if (fields.size() < expected_min_fields) {
1228 return ParseFailedExpectMinFieldNum(line, expected_min_fields, error);
1229 }
1230 if (!rtc::FromString(fields[1], sctp_port)) {
lally69f57602015-10-08 10:15:04 -07001231 return ParseFailed(line, "Invalid sctp port value.", error);
jiayl@webrtc.orgddb85ab2014-09-05 16:31:56 +00001232 }
1233 return true;
1234}
1235
isheriff6f8d6862016-05-26 11:24:55 -07001236bool ParseExtmap(const std::string& line,
1237 RtpExtension* extmap,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001238 SdpParseError* error) {
1239 // RFC 5285
1240 // a=extmap:<value>["/"<direction>] <URI> <extensionattributes>
1241 std::vector<std::string> fields;
Jonas Olssonec9e4922018-09-05 09:53:49 +02001242 rtc::split(line.substr(kLinePrefixLength), kSdpDelimiterSpaceChar, &fields);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001243 const size_t expected_min_fields = 2;
1244 if (fields.size() < expected_min_fields) {
1245 return ParseFailedExpectMinFieldNum(line, expected_min_fields, error);
1246 }
1247 std::string uri = fields[1];
1248
1249 std::string value_direction;
1250 if (!GetValue(fields[0], kAttributeExtmap, &value_direction, error)) {
1251 return false;
1252 }
1253 std::vector<std::string> sub_fields;
Jonas Olssonec9e4922018-09-05 09:53:49 +02001254 rtc::split(value_direction, kSdpDelimiterSlashChar, &sub_fields);
wu@webrtc.org5e760e72014-04-02 23:19:09 +00001255 int value = 0;
1256 if (!GetValueFromString(line, sub_fields[0], &value, error)) {
1257 return false;
1258 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001259
jbauch5869f502017-06-29 12:31:36 -07001260 bool encrypted = false;
1261 if (uri == RtpExtension::kEncryptHeaderExtensionsUri) {
1262 // RFC 6904
Steve Anton36b29d12017-10-30 09:57:42 -07001263 // a=extmap:<value["/"<direction>] urn:ietf:params:rtp-hdrext:encrypt <URI>
1264 // <extensionattributes>
jbauch5869f502017-06-29 12:31:36 -07001265 const size_t expected_min_fields_encrypted = expected_min_fields + 1;
1266 if (fields.size() < expected_min_fields_encrypted) {
1267 return ParseFailedExpectMinFieldNum(line, expected_min_fields_encrypted,
Yves Gerey665174f2018-06-19 15:03:05 +02001268 error);
jbauch5869f502017-06-29 12:31:36 -07001269 }
1270
1271 encrypted = true;
1272 uri = fields[2];
1273 if (uri == RtpExtension::kEncryptHeaderExtensionsUri) {
1274 return ParseFailed(line, "Recursive encrypted header.", error);
1275 }
1276 }
1277
1278 *extmap = RtpExtension(uri, value, encrypted);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001279 return true;
1280}
1281
1282void BuildMediaDescription(const ContentInfo* content_info,
1283 const TransportInfo* transport_info,
Patrik Höglundb6b29e02018-06-21 16:58:01 +02001284 const cricket::MediaType media_type,
wu@webrtc.org4c3e9912014-07-16 21:03:13 +00001285 const std::vector<Candidate>& candidates,
Steve Antone831b8c2018-02-01 12:22:16 -08001286 int msid_signaling,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001287 std::string* message) {
nisseede5da42017-01-12 05:15:36 -08001288 RTC_DCHECK(message != NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001289 if (content_info == NULL || message == NULL) {
1290 return;
1291 }
Jonas Olssonec9e4922018-09-05 09:53:49 +02001292 rtc::StringBuilder os;
Steve Antonb1c1de12017-12-21 15:14:30 -08001293 const MediaContentDescription* media_desc = content_info->media_description();
1294 RTC_DCHECK(media_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001295
jiayl@webrtc.org9c16c392014-05-01 18:30:30 +00001296 int sctp_port = cricket::kSctpDefaultPort;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001297
1298 // RFC 4566
1299 // m=<media> <port> <proto> <fmt>
1300 // fmt is a list of payload type numbers that MAY be used in the session.
1301 const char* type = NULL;
1302 if (media_type == cricket::MEDIA_TYPE_AUDIO)
1303 type = kMediaTypeAudio;
1304 else if (media_type == cricket::MEDIA_TYPE_VIDEO)
1305 type = kMediaTypeVideo;
1306 else if (media_type == cricket::MEDIA_TYPE_DATA)
1307 type = kMediaTypeData;
1308 else
nissec80e7412017-01-11 05:56:46 -08001309 RTC_NOTREACHED();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001310
1311 std::string fmt;
1312 if (media_type == cricket::MEDIA_TYPE_VIDEO) {
Steve Antonb1c1de12017-12-21 15:14:30 -08001313 const VideoContentDescription* video_desc = media_desc->as_video();
Steve Anton4daf66e2018-09-07 14:55:53 -07001314 for (const cricket::VideoCodec& codec : video_desc->codecs()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001315 fmt.append(" ");
Steve Anton4daf66e2018-09-07 14:55:53 -07001316 fmt.append(rtc::ToString(codec.id));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001317 }
1318 } else if (media_type == cricket::MEDIA_TYPE_AUDIO) {
Steve Antonb1c1de12017-12-21 15:14:30 -08001319 const AudioContentDescription* audio_desc = media_desc->as_audio();
Steve Anton4daf66e2018-09-07 14:55:53 -07001320 for (const cricket::AudioCodec& codec : audio_desc->codecs()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001321 fmt.append(" ");
Steve Anton4daf66e2018-09-07 14:55:53 -07001322 fmt.append(rtc::ToString(codec.id));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001323 }
1324 } else if (media_type == cricket::MEDIA_TYPE_DATA) {
Steve Antonb1c1de12017-12-21 15:14:30 -08001325 const DataContentDescription* data_desc = media_desc->as_data();
pthatcher@webrtc.org3341b402015-02-13 21:14:22 +00001326 if (IsDtlsSctp(media_desc->protocol())) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001327 fmt.append(" ");
jiayl@webrtc.org9c16c392014-05-01 18:30:30 +00001328
zstein4b2e0822017-02-17 19:48:38 -08001329 if (data_desc->use_sctpmap()) {
Steve Anton4daf66e2018-09-07 14:55:53 -07001330 for (const cricket::DataCodec& codec : data_desc->codecs()) {
Niels Möller039743e2018-10-23 10:07:25 +02001331 if (absl::EqualsIgnoreCase(codec.name,
1332 cricket::kGoogleSctpDataCodecName) &&
Steve Anton4daf66e2018-09-07 14:55:53 -07001333 codec.GetParam(cricket::kCodecParamPort, &sctp_port)) {
zstein4b2e0822017-02-17 19:48:38 -08001334 break;
1335 }
jiayl@webrtc.org9c16c392014-05-01 18:30:30 +00001336 }
jiayl@webrtc.org9c16c392014-05-01 18:30:30 +00001337
Jonas Olsson6b1985d2018-07-05 11:59:48 +02001338 fmt.append(rtc::ToString(sctp_port));
zstein4b2e0822017-02-17 19:48:38 -08001339 } else {
1340 fmt.append(kDefaultSctpmapProtocol);
1341 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001342 } else {
Steve Anton4daf66e2018-09-07 14:55:53 -07001343 for (const cricket::DataCodec& codec : data_desc->codecs()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001344 fmt.append(" ");
Steve Anton4daf66e2018-09-07 14:55:53 -07001345 fmt.append(rtc::ToString(codec.id));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001346 }
1347 }
1348 }
1349 // The fmt must never be empty. If no codecs are found, set the fmt attribute
1350 // to 0.
1351 if (fmt.empty()) {
1352 fmt = " 0";
1353 }
1354
deadbeef25ed4352016-12-12 18:37:36 -08001355 // The port number in the m line will be updated later when associated with
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001356 // the candidates.
deadbeef25ed4352016-12-12 18:37:36 -08001357 //
1358 // A port value of 0 indicates that the m= section is rejected.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001359 // RFC 3264
1360 // To reject an offered stream, the port number in the corresponding stream in
1361 // the answer MUST be set to zero.
deadbeef25ed4352016-12-12 18:37:36 -08001362 //
1363 // However, the BUNDLE draft adds a new meaning to port zero, when used along
1364 // with a=bundle-only.
zhihuang38989e52017-03-21 11:04:53 -07001365 std::string port = kDummyPort;
1366 if (content_info->rejected || content_info->bundle_only) {
1367 port = kMediaPortRejected;
1368 } else if (!media_desc->connection_address().IsNil()) {
1369 port = rtc::ToString(media_desc->connection_address().port());
1370 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001371
Yves Gerey665174f2018-06-19 15:03:05 +02001372 rtc::SSLFingerprint* fp =
1373 (transport_info) ? transport_info->description.identity_fingerprint.get()
1374 : NULL;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001375
wu@webrtc.org4c3e9912014-07-16 21:03:13 +00001376 // Add the m and c lines.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001377 InitLine(kLineTypeMedia, type, &os);
1378 os << " " << port << " " << media_desc->protocol() << fmt;
zhihuang38989e52017-03-21 11:04:53 -07001379 AddLine(os.str(), message);
1380
1381 InitLine(kLineTypeConnection, kConnectionNettype, &os);
1382 if (media_desc->connection_address().IsNil()) {
1383 os << " " << kConnectionIpv4Addrtype << " " << kDummyAddress;
1384 } else if (media_desc->connection_address().family() == AF_INET) {
1385 os << " " << kConnectionIpv4Addrtype << " "
1386 << media_desc->connection_address().ipaddr().ToString();
1387 } else {
1388 os << " " << kConnectionIpv6Addrtype << " "
1389 << media_desc->connection_address().ipaddr().ToString();
1390 }
1391 AddLine(os.str(), message);
wu@webrtc.org4c3e9912014-07-16 21:03:13 +00001392
1393 // RFC 4566
1394 // b=AS:<bandwidth>
Peter Thatcherc0c3a862015-06-24 15:31:25 -07001395 if (media_desc->bandwidth() >= 1000) {
wu@webrtc.org4c3e9912014-07-16 21:03:13 +00001396 InitLine(kLineTypeSessionBandwidth, kApplicationSpecificMaximum, &os);
1397 os << kSdpDelimiterColon << (media_desc->bandwidth() / 1000);
1398 AddLine(os.str(), message);
1399 }
1400
deadbeef25ed4352016-12-12 18:37:36 -08001401 // Add the a=bundle-only line.
1402 if (content_info->bundle_only) {
1403 InitAttrLine(kAttributeBundleOnly, &os);
1404 AddLine(os.str(), message);
1405 }
1406
wu@webrtc.org4c3e9912014-07-16 21:03:13 +00001407 // Add the a=rtcp line.
pthatcher@webrtc.org3341b402015-02-13 21:14:22 +00001408 if (IsRtp(media_desc->protocol())) {
wu@webrtc.org4c3e9912014-07-16 21:03:13 +00001409 std::string rtcp_line = GetRtcpLine(candidates);
1410 if (!rtcp_line.empty()) {
1411 AddLine(rtcp_line, message);
1412 }
1413 }
1414
honghaiza54a0802015-12-16 18:37:23 -08001415 // Build the a=candidate lines. We don't include ufrag and pwd in the
1416 // candidates in the SDP to avoid redundancy.
1417 BuildCandidate(candidates, false, message);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001418
1419 // Use the transport_info to build the media level ice-ufrag and ice-pwd.
1420 if (transport_info) {
1421 // RFC 5245
1422 // ice-pwd-att = "ice-pwd" ":" password
1423 // ice-ufrag-att = "ice-ufrag" ":" ufrag
1424 // ice-ufrag
deadbeef3f7219b2015-12-28 15:17:14 -08001425 if (!transport_info->description.ice_ufrag.empty()) {
1426 InitAttrLine(kAttributeIceUfrag, &os);
1427 os << kSdpDelimiterColon << transport_info->description.ice_ufrag;
1428 AddLine(os.str(), message);
1429 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001430 // ice-pwd
deadbeef3f7219b2015-12-28 15:17:14 -08001431 if (!transport_info->description.ice_pwd.empty()) {
1432 InitAttrLine(kAttributeIcePwd, &os);
1433 os << kSdpDelimiterColon << transport_info->description.ice_pwd;
1434 AddLine(os.str(), message);
1435 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001436
1437 // draft-petithuguenin-mmusic-ice-attributes-level-03
1438 BuildIceOptions(transport_info->description.transport_options, message);
1439
1440 // RFC 4572
1441 // fingerprint-attribute =
1442 // "fingerprint" ":" hash-func SP fingerprint
1443 if (fp) {
1444 // Insert the fingerprint attribute.
1445 InitAttrLine(kAttributeFingerprint, &os);
Yves Gerey665174f2018-06-19 15:03:05 +02001446 os << kSdpDelimiterColon << fp->algorithm << kSdpDelimiterSpace
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001447 << fp->GetRfc4572Fingerprint();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001448 AddLine(os.str(), message);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001449
1450 // Inserting setup attribute.
1451 if (transport_info->description.connection_role !=
Yves Gerey665174f2018-06-19 15:03:05 +02001452 cricket::CONNECTIONROLE_NONE) {
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001453 // Making sure we are not using "passive" mode.
1454 cricket::ConnectionRole role =
1455 transport_info->description.connection_role;
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00001456 std::string dtls_role_str;
nissec16fa5e2017-02-07 07:18:43 -08001457 const bool success =
1458 cricket::ConnectionRoleToString(role, &dtls_role_str);
1459 RTC_DCHECK(success);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001460 InitAttrLine(kAttributeSetup, &os);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001461 os << kSdpDelimiterColon << dtls_role_str;
1462 AddLine(os.str(), message);
1463 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001464 }
1465 }
1466
1467 // RFC 3388
1468 // mid-attribute = "a=mid:" identification-tag
1469 // identification-tag = token
1470 // Use the content name as the mid identification-tag.
1471 InitAttrLine(kAttributeMid, &os);
1472 os << kSdpDelimiterColon << content_info->name;
1473 AddLine(os.str(), message);
1474
pthatcher@webrtc.org3341b402015-02-13 21:14:22 +00001475 if (IsDtlsSctp(media_desc->protocol())) {
Steve Antonb1c1de12017-12-21 15:14:30 -08001476 const DataContentDescription* data_desc = media_desc->as_data();
zstein4b2e0822017-02-17 19:48:38 -08001477 bool use_sctpmap = data_desc->use_sctpmap();
1478 BuildSctpContentAttributes(message, sctp_port, use_sctpmap);
pthatcher@webrtc.org3341b402015-02-13 21:14:22 +00001479 } else if (IsRtp(media_desc->protocol())) {
Steve Antone831b8c2018-02-01 12:22:16 -08001480 BuildRtpContentAttributes(media_desc, media_type, msid_signaling, message);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001481 }
1482}
1483
zstein4b2e0822017-02-17 19:48:38 -08001484void BuildSctpContentAttributes(std::string* message,
1485 int sctp_port,
1486 bool use_sctpmap) {
Jonas Olssonec9e4922018-09-05 09:53:49 +02001487 rtc::StringBuilder os;
zstein4b2e0822017-02-17 19:48:38 -08001488 if (use_sctpmap) {
1489 // draft-ietf-mmusic-sctp-sdp-04
1490 // a=sctpmap:sctpmap-number protocol [streams]
1491 InitAttrLine(kAttributeSctpmap, &os);
1492 os << kSdpDelimiterColon << sctp_port << kSdpDelimiterSpace
1493 << kDefaultSctpmapProtocol << kSdpDelimiterSpace
1494 << cricket::kMaxSctpStreams;
1495 } else {
1496 // draft-ietf-mmusic-sctp-sdp-23
1497 // a=sctp-port:<port>
1498 InitAttrLine(kAttributeSctpPort, &os);
1499 os << kSdpDelimiterColon << sctp_port;
1500 // TODO(zstein): emit max-message-size here
1501 }
wu@webrtc.org78187522013-10-07 23:32:02 +00001502 AddLine(os.str(), message);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001503}
1504
deadbeef9d3584c2016-02-16 17:54:10 -08001505void BuildRtpContentAttributes(const MediaContentDescription* media_desc,
Patrik Höglundb6b29e02018-06-21 16:58:01 +02001506 const cricket::MediaType media_type,
Steve Antone831b8c2018-02-01 12:22:16 -08001507 int msid_signaling,
deadbeef9d3584c2016-02-16 17:54:10 -08001508 std::string* message) {
Amit Hilbuchc57d5732018-12-11 15:30:11 -08001509 SdpSerializer serializer;
Jonas Olssonec9e4922018-09-05 09:53:49 +02001510 rtc::StringBuilder os;
Johannes Kron0854eb62018-10-10 22:33:20 +02001511 // RFC 8285
1512 // a=extmap-allow-mixed
1513 // The attribute MUST be either on session level or media level. We support
1514 // responding on both levels, however, we don't respond on media level if it's
1515 // set on session level.
Johannes Kron9581bc42018-10-23 10:17:39 +02001516 if (media_desc->extmap_allow_mixed_enum() ==
Johannes Kron0854eb62018-10-10 22:33:20 +02001517 MediaContentDescription::kMedia) {
1518 InitAttrLine(kAttributeExtmapAllowMixed, &os);
1519 AddLine(os.str(), message);
1520 }
1521 // RFC 8285
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001522 // a=extmap:<value>["/"<direction>] <URI> <extensionattributes>
1523 // The definitions MUST be either all session level or all media level. This
1524 // implementation uses all media level.
1525 for (size_t i = 0; i < media_desc->rtp_header_extensions().size(); ++i) {
jbauch5869f502017-06-29 12:31:36 -07001526 const RtpExtension& extension = media_desc->rtp_header_extensions()[i];
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001527 InitAttrLine(kAttributeExtmap, &os);
jbauch5869f502017-06-29 12:31:36 -07001528 os << kSdpDelimiterColon << extension.id;
1529 if (extension.encrypt) {
1530 os << kSdpDelimiterSpace << RtpExtension::kEncryptHeaderExtensionsUri;
1531 }
1532 os << kSdpDelimiterSpace << extension.uri;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001533 AddLine(os.str(), message);
1534 }
1535
1536 // RFC 3264
1537 // a=sendrecv || a=sendonly || a=sendrecv || a=inactive
deadbeefc80741f2015-10-22 13:14:45 -07001538 switch (media_desc->direction()) {
Steve Anton4e70a722017-11-28 14:57:10 -08001539 case RtpTransceiverDirection::kInactive:
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001540 InitAttrLine(kAttributeInactive, &os);
1541 break;
Steve Anton4e70a722017-11-28 14:57:10 -08001542 case RtpTransceiverDirection::kSendOnly:
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001543 InitAttrLine(kAttributeSendOnly, &os);
1544 break;
Steve Anton4e70a722017-11-28 14:57:10 -08001545 case RtpTransceiverDirection::kRecvOnly:
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001546 InitAttrLine(kAttributeRecvOnly, &os);
1547 break;
Steve Anton4e70a722017-11-28 14:57:10 -08001548 case RtpTransceiverDirection::kSendRecv:
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001549 default:
1550 InitAttrLine(kAttributeSendRecv, &os);
1551 break;
1552 }
1553 AddLine(os.str(), message);
1554
Seth Hampson5b4f0752018-04-02 16:31:36 -07001555 // Specified in https://datatracker.ietf.org/doc/draft-ietf-mmusic-msid/16/
1556 // a=msid:<msid-id> <msid-appdata>
1557 // The msid-id is a 1*64 token char representing the media stream id, and the
1558 // msid-appdata is a 1*64 token char representing the track id. There is a
1559 // line for every media stream, with a special msid-id value of "-"
1560 // representing no streams. The value of "msid-appdata" MUST be identical for
1561 // all lines.
Steve Antone831b8c2018-02-01 12:22:16 -08001562 if (msid_signaling & cricket::kMsidSignalingMediaSection) {
1563 const StreamParamsVec& streams = media_desc->streams();
1564 if (streams.size() == 1u) {
1565 const StreamParams& track = streams[0];
Seth Hampson5b4f0752018-04-02 16:31:36 -07001566 std::vector<std::string> stream_ids = track.stream_ids();
1567 if (stream_ids.empty()) {
1568 stream_ids.push_back(kNoStreamMsid);
1569 }
1570 for (const std::string& stream_id : stream_ids) {
1571 InitAttrLine(kAttributeMsid, &os);
1572 os << kSdpDelimiterColon << stream_id << kSdpDelimiterSpace << track.id;
1573 AddLine(os.str(), message);
1574 }
Steve Antone831b8c2018-02-01 12:22:16 -08001575 } else if (streams.size() > 1u) {
1576 RTC_LOG(LS_WARNING)
1577 << "Trying to serialize Unified Plan SDP with more than "
Jonas Olsson45cc8902018-02-13 10:37:07 +01001578 "one track in a media section. Omitting 'a=msid'.";
deadbeef9d3584c2016-02-16 17:54:10 -08001579 }
1580 }
1581
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001582 // RFC 5761
1583 // a=rtcp-mux
1584 if (media_desc->rtcp_mux()) {
1585 InitAttrLine(kAttributeRtcpMux, &os);
1586 AddLine(os.str(), message);
1587 }
1588
deadbeef13871492015-12-09 12:37:51 -08001589 // RFC 5506
1590 // a=rtcp-rsize
1591 if (media_desc->rtcp_reduced_size()) {
1592 InitAttrLine(kAttributeRtcpReducedSize, &os);
1593 AddLine(os.str(), message);
1594 }
1595
deadbeefd45aea82017-09-16 01:24:29 -07001596 if (media_desc->conference_mode()) {
1597 InitAttrLine(kAttributeXGoogleFlag, &os);
1598 os << kSdpDelimiterColon << kValueConference;
1599 AddLine(os.str(), message);
1600 }
1601
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001602 // RFC 4568
1603 // a=crypto:<tag> <crypto-suite> <key-params> [<session-params>]
Steve Anton4daf66e2018-09-07 14:55:53 -07001604 for (const CryptoParams& crypto_params : media_desc->cryptos()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001605 InitAttrLine(kAttributeCrypto, &os);
Steve Anton4daf66e2018-09-07 14:55:53 -07001606 os << kSdpDelimiterColon << crypto_params.tag << " "
1607 << crypto_params.cipher_suite << " " << crypto_params.key_params;
1608 if (!crypto_params.session_params.empty()) {
1609 os << " " << crypto_params.session_params;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001610 }
1611 AddLine(os.str(), message);
1612 }
1613
1614 // RFC 4566
1615 // a=rtpmap:<payload type> <encoding name>/<clock rate>
1616 // [/<encodingparameters>]
1617 BuildRtpMap(media_desc, media_type, message);
1618
Steve Anton4daf66e2018-09-07 14:55:53 -07001619 for (const StreamParams& track : media_desc->streams()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001620 // Build the ssrc-group lines.
Steve Anton4daf66e2018-09-07 14:55:53 -07001621 for (const SsrcGroup& ssrc_group : track.ssrc_groups) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001622 // RFC 5576
1623 // a=ssrc-group:<semantics> <ssrc-id> ...
Steve Anton4daf66e2018-09-07 14:55:53 -07001624 if (ssrc_group.ssrcs.empty()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001625 continue;
1626 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001627 InitAttrLine(kAttributeSsrcGroup, &os);
Steve Anton4daf66e2018-09-07 14:55:53 -07001628 os << kSdpDelimiterColon << ssrc_group.semantics;
1629 for (uint32_t ssrc : ssrc_group.ssrcs) {
1630 os << kSdpDelimiterSpace << rtc::ToString(ssrc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001631 }
1632 AddLine(os.str(), message);
1633 }
1634 // Build the ssrc lines for each ssrc.
Steve Anton4daf66e2018-09-07 14:55:53 -07001635 for (uint32_t ssrc : track.ssrcs) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001636 // RFC 5576
1637 // a=ssrc:<ssrc-id> cname:<value>
Steve Anton4daf66e2018-09-07 14:55:53 -07001638 AddSsrcLine(ssrc, kSsrcAttributeCname, track.cname, message);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001639
Steve Antone831b8c2018-02-01 12:22:16 -08001640 if (msid_signaling & cricket::kMsidSignalingSsrcAttribute) {
1641 // draft-alvestrand-mmusic-msid-00
1642 // a=ssrc:<ssrc-id> msid:identifier [appdata]
1643 // The appdata consists of the "id" attribute of a MediaStreamTrack,
1644 // which corresponds to the "id" attribute of StreamParams.
Seth Hampson5b4f0752018-04-02 16:31:36 -07001645 // Since a=ssrc msid signaling is used in Plan B SDP semantics, and
1646 // multiple stream ids are not supported for Plan B, we are only adding
1647 // a line for the first media stream id here.
Seth Hampson7fa6ee62018-10-17 10:25:28 -07001648 const std::string& track_stream_id = track.first_stream_id();
1649 // We use a special msid-id value of "-" to represent no streams,
1650 // for Unified Plan compatibility. Plan B will always have a
1651 // track_stream_id.
1652 const std::string& stream_id =
1653 track_stream_id.empty() ? kNoStreamMsid : track_stream_id;
Steve Antone831b8c2018-02-01 12:22:16 -08001654 InitAttrLine(kAttributeSsrc, &os);
1655 os << kSdpDelimiterColon << ssrc << kSdpDelimiterSpace
1656 << kSsrcAttributeMsid << kSdpDelimiterColon << stream_id
Steve Anton4daf66e2018-09-07 14:55:53 -07001657 << kSdpDelimiterSpace << track.id;
Steve Antone831b8c2018-02-01 12:22:16 -08001658 AddLine(os.str(), message);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001659
Steve Antone831b8c2018-02-01 12:22:16 -08001660 // TODO(ronghuawu): Remove below code which is for backward
1661 // compatibility.
1662 // draft-alvestrand-rtcweb-mid-01
1663 // a=ssrc:<ssrc-id> mslabel:<value>
1664 // The label isn't yet defined.
1665 // a=ssrc:<ssrc-id> label:<value>
Seth Hampson5b4f0752018-04-02 16:31:36 -07001666 AddSsrcLine(ssrc, kSsrcAttributeMslabel, stream_id, message);
Steve Anton4daf66e2018-09-07 14:55:53 -07001667 AddSsrcLine(ssrc, kSSrcAttributeLabel, track.id, message);
Steve Antone831b8c2018-02-01 12:22:16 -08001668 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001669 }
Amit Hilbuchc57d5732018-12-11 15:30:11 -08001670
1671 // Build the rid lines for each layer of the track
1672 for (const RidDescription& rid_description : track.rids()) {
1673 InitAttrLine(kAttributeRid, &os);
1674 os << kSdpDelimiterColon
1675 << serializer.SerializeRidDescription(rid_description);
1676 AddLine(os.str(), message);
1677 }
1678 }
1679
Amit Hilbucha2012042018-12-03 11:35:05 -08001680 // Simulcast (a=simulcast)
1681 // https://tools.ietf.org/html/draft-ietf-mmusic-sdp-simulcast-13#section-5.1
Amit Hilbuchc57d5732018-12-11 15:30:11 -08001682 if (media_desc->HasSimulcast()) {
1683 const auto& simulcast = media_desc->simulcast_description();
Amit Hilbucha2012042018-12-03 11:35:05 -08001684 InitAttrLine(kAttributeSimulcast, &os);
Amit Hilbucha2012042018-12-03 11:35:05 -08001685 os << kSdpDelimiterColon
1686 << serializer.SerializeSimulcastDescription(simulcast);
1687 AddLine(os.str(), message);
1688 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001689}
1690
Jonas Olssonec9e4922018-09-05 09:53:49 +02001691void WriteFmtpHeader(int payload_type, rtc::StringBuilder* os) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001692 // fmtp header: a=fmtp:|payload_type| <parameters>
1693 // Add a=fmtp
1694 InitAttrLine(kAttributeFmtp, os);
1695 // Add :|payload_type|
1696 *os << kSdpDelimiterColon << payload_type;
1697}
1698
Jonas Olssonec9e4922018-09-05 09:53:49 +02001699void WriteRtcpFbHeader(int payload_type, rtc::StringBuilder* os) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001700 // rtcp-fb header: a=rtcp-fb:|payload_type|
1701 // <parameters>/<ccm <ccm_parameters>>
1702 // Add a=rtcp-fb
1703 InitAttrLine(kAttributeRtcpFb, os);
1704 // Add :
1705 *os << kSdpDelimiterColon;
1706 if (payload_type == kWildcardPayloadType) {
1707 *os << "*";
1708 } else {
1709 *os << payload_type;
1710 }
1711}
1712
1713void WriteFmtpParameter(const std::string& parameter_name,
1714 const std::string& parameter_value,
Jonas Olssonec9e4922018-09-05 09:53:49 +02001715 rtc::StringBuilder* os) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001716 // fmtp parameters: |parameter_name|=|parameter_value|
1717 *os << parameter_name << kSdpDelimiterEqual << parameter_value;
1718}
1719
1720void WriteFmtpParameters(const cricket::CodecParameterMap& parameters,
Jonas Olssonec9e4922018-09-05 09:53:49 +02001721 rtc::StringBuilder* os) {
Steve Anton4daf66e2018-09-07 14:55:53 -07001722 bool first = true;
1723 for (const auto& entry : parameters) {
1724 const std::string& key = entry.first;
1725 const std::string& value = entry.second;
hta62a216e2016-04-15 11:02:14 -07001726 // Parameters are a semicolon-separated list, no spaces.
1727 // The list is separated from the header by a space.
Steve Anton4daf66e2018-09-07 14:55:53 -07001728 if (first) {
hta62a216e2016-04-15 11:02:14 -07001729 *os << kSdpDelimiterSpace;
Steve Anton4daf66e2018-09-07 14:55:53 -07001730 first = false;
hta62a216e2016-04-15 11:02:14 -07001731 } else {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001732 *os << kSdpDelimiterSemicolon;
1733 }
Steve Anton4daf66e2018-09-07 14:55:53 -07001734 WriteFmtpParameter(key, value, os);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001735 }
1736}
1737
1738bool IsFmtpParam(const std::string& name) {
ossuaa4b0772017-01-30 07:41:18 -08001739 // RFC 4855, section 3 specifies the mapping of media format parameters to SDP
1740 // parameters. Only ptime, maxptime, channels and rate are placed outside of
1741 // the fmtp line. In WebRTC, channels and rate are already handled separately
1742 // and thus not included in the CodecParameterMap.
1743 return name != kCodecParamPTime && name != kCodecParamMaxPTime;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001744}
1745
1746// Retreives fmtp parameters from |params|, which may contain other parameters
1747// as well, and puts them in |fmtp_parameters|.
1748void GetFmtpParams(const cricket::CodecParameterMap& params,
1749 cricket::CodecParameterMap* fmtp_parameters) {
Steve Anton4daf66e2018-09-07 14:55:53 -07001750 for (const auto& entry : params) {
1751 const std::string& key = entry.first;
1752 const std::string& value = entry.second;
1753 if (IsFmtpParam(key)) {
1754 (*fmtp_parameters)[key] = value;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001755 }
1756 }
1757}
1758
1759template <class T>
1760void AddFmtpLine(const T& codec, std::string* message) {
1761 cricket::CodecParameterMap fmtp_parameters;
1762 GetFmtpParams(codec.params, &fmtp_parameters);
1763 if (fmtp_parameters.empty()) {
1764 // No need to add an fmtp if it will have no (optional) parameters.
1765 return;
1766 }
Jonas Olssonec9e4922018-09-05 09:53:49 +02001767 rtc::StringBuilder os;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001768 WriteFmtpHeader(codec.id, &os);
1769 WriteFmtpParameters(fmtp_parameters, &os);
1770 AddLine(os.str(), message);
1771 return;
1772}
1773
1774template <class T>
1775void AddRtcpFbLines(const T& codec, std::string* message) {
Steve Anton4daf66e2018-09-07 14:55:53 -07001776 for (const cricket::FeedbackParam& param : codec.feedback_params.params()) {
Jonas Olssonec9e4922018-09-05 09:53:49 +02001777 rtc::StringBuilder os;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001778 WriteRtcpFbHeader(codec.id, &os);
Steve Anton4daf66e2018-09-07 14:55:53 -07001779 os << " " << param.id();
1780 if (!param.param().empty()) {
1781 os << " " << param.param();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001782 }
1783 AddLine(os.str(), message);
1784 }
1785}
1786
Yves Gerey665174f2018-06-19 15:03:05 +02001787bool AddSctpDataCodec(DataContentDescription* media_desc, int sctp_port) {
solenberg9fa49752016-10-08 13:02:44 -07001788 for (const auto& codec : media_desc->codecs()) {
Niels Möller039743e2018-10-23 10:07:25 +02001789 if (absl::EqualsIgnoreCase(codec.name, cricket::kGoogleSctpDataCodecName)) {
Yves Gerey665174f2018-06-19 15:03:05 +02001790 return ParseFailed("", "Can't have multiple sctp port attributes.", NULL);
solenberg9fa49752016-10-08 13:02:44 -07001791 }
jiayl@webrtc.orgddb85ab2014-09-05 16:31:56 +00001792 }
1793 // Add the SCTP Port number as a pseudo-codec "port" parameter
solenberg9fa49752016-10-08 13:02:44 -07001794 cricket::DataCodec codec_port(cricket::kGoogleSctpDataCodecPlType,
deadbeef67cf2c12016-04-13 10:07:16 -07001795 cricket::kGoogleSctpDataCodecName);
jiayl@webrtc.orgddb85ab2014-09-05 16:31:56 +00001796 codec_port.SetParam(cricket::kCodecParamPort, sctp_port);
Mirko Bonadei675513b2017-11-09 11:09:25 +01001797 RTC_LOG(INFO) << "AddSctpDataCodec: Got SCTP Port Number " << sctp_port;
jiayl@webrtc.orgddb85ab2014-09-05 16:31:56 +00001798 media_desc->AddCodec(codec_port);
1799 return true;
1800}
1801
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001802bool GetMinValue(const std::vector<int>& values, int* value) {
1803 if (values.empty()) {
1804 return false;
1805 }
1806 std::vector<int>::const_iterator found =
1807 std::min_element(values.begin(), values.end());
1808 *value = *found;
1809 return true;
1810}
1811
1812bool GetParameter(const std::string& name,
Yves Gerey665174f2018-06-19 15:03:05 +02001813 const cricket::CodecParameterMap& params,
1814 int* value) {
1815 std::map<std::string, std::string>::const_iterator found = params.find(name);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001816 if (found == params.end()) {
1817 return false;
1818 }
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001819 if (!rtc::FromString(found->second, value)) {
wu@webrtc.org5e760e72014-04-02 23:19:09 +00001820 return false;
1821 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001822 return true;
1823}
1824
1825void BuildRtpMap(const MediaContentDescription* media_desc,
Patrik Höglundb6b29e02018-06-21 16:58:01 +02001826 const cricket::MediaType media_type,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001827 std::string* message) {
nisseede5da42017-01-12 05:15:36 -08001828 RTC_DCHECK(message != NULL);
1829 RTC_DCHECK(media_desc != NULL);
Jonas Olssonec9e4922018-09-05 09:53:49 +02001830 rtc::StringBuilder os;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001831 if (media_type == cricket::MEDIA_TYPE_VIDEO) {
Steve Anton4daf66e2018-09-07 14:55:53 -07001832 for (const cricket::VideoCodec& codec : media_desc->as_video()->codecs()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001833 // RFC 4566
1834 // a=rtpmap:<payload type> <encoding name>/<clock rate>
1835 // [/<encodingparameters>]
Steve Anton4daf66e2018-09-07 14:55:53 -07001836 if (codec.id != kWildcardPayloadType) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001837 InitAttrLine(kAttributeRtpmap, &os);
Steve Anton4daf66e2018-09-07 14:55:53 -07001838 os << kSdpDelimiterColon << codec.id << " " << codec.name << "/"
deadbeefe814a0d2017-02-25 18:15:09 -08001839 << cricket::kVideoCodecClockrate;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001840 AddLine(os.str(), message);
1841 }
Steve Anton4daf66e2018-09-07 14:55:53 -07001842 AddRtcpFbLines(codec, message);
1843 AddFmtpLine(codec, message);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001844 }
1845 } else if (media_type == cricket::MEDIA_TYPE_AUDIO) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001846 std::vector<int> ptimes;
1847 std::vector<int> maxptimes;
1848 int max_minptime = 0;
Steve Anton4daf66e2018-09-07 14:55:53 -07001849 for (const cricket::AudioCodec& codec : media_desc->as_audio()->codecs()) {
1850 RTC_DCHECK(!codec.name.empty());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001851 // RFC 4566
1852 // a=rtpmap:<payload type> <encoding name>/<clock rate>
1853 // [/<encodingparameters>]
1854 InitAttrLine(kAttributeRtpmap, &os);
Steve Anton4daf66e2018-09-07 14:55:53 -07001855 os << kSdpDelimiterColon << codec.id << " ";
1856 os << codec.name << "/" << codec.clockrate;
1857 if (codec.channels != 1) {
1858 os << "/" << codec.channels;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001859 }
1860 AddLine(os.str(), message);
Steve Anton4daf66e2018-09-07 14:55:53 -07001861 AddRtcpFbLines(codec, message);
1862 AddFmtpLine(codec, message);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001863 int minptime = 0;
Steve Anton4daf66e2018-09-07 14:55:53 -07001864 if (GetParameter(kCodecParamMinPTime, codec.params, &minptime)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001865 max_minptime = std::max(minptime, max_minptime);
1866 }
1867 int ptime;
Steve Anton4daf66e2018-09-07 14:55:53 -07001868 if (GetParameter(kCodecParamPTime, codec.params, &ptime)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001869 ptimes.push_back(ptime);
1870 }
1871 int maxptime;
Steve Anton4daf66e2018-09-07 14:55:53 -07001872 if (GetParameter(kCodecParamMaxPTime, codec.params, &maxptime)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001873 maxptimes.push_back(maxptime);
1874 }
1875 }
1876 // Populate the maxptime attribute with the smallest maxptime of all codecs
1877 // under the same m-line.
1878 int min_maxptime = INT_MAX;
1879 if (GetMinValue(maxptimes, &min_maxptime)) {
1880 AddAttributeLine(kCodecParamMaxPTime, min_maxptime, message);
1881 }
nisseede5da42017-01-12 05:15:36 -08001882 RTC_DCHECK(min_maxptime > max_minptime);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001883 // Populate the ptime attribute with the smallest ptime or the largest
1884 // minptime, whichever is the largest, for all codecs under the same m-line.
1885 int ptime = INT_MAX;
1886 if (GetMinValue(ptimes, &ptime)) {
1887 ptime = std::min(ptime, min_maxptime);
1888 ptime = std::max(ptime, max_minptime);
1889 AddAttributeLine(kCodecParamPTime, ptime, message);
1890 }
1891 } else if (media_type == cricket::MEDIA_TYPE_DATA) {
Steve Anton4daf66e2018-09-07 14:55:53 -07001892 for (const cricket::DataCodec& codec : media_desc->as_data()->codecs()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001893 // RFC 4566
1894 // a=rtpmap:<payload type> <encoding name>/<clock rate>
1895 // [/<encodingparameters>]
1896 InitAttrLine(kAttributeRtpmap, &os);
Steve Anton4daf66e2018-09-07 14:55:53 -07001897 os << kSdpDelimiterColon << codec.id << " " << codec.name << "/"
1898 << codec.clockrate;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001899 AddLine(os.str(), message);
1900 }
1901 }
1902}
1903
1904void BuildCandidate(const std::vector<Candidate>& candidates,
honghaiza54a0802015-12-16 18:37:23 -08001905 bool include_ufrag,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001906 std::string* message) {
Jonas Olssonec9e4922018-09-05 09:53:49 +02001907 rtc::StringBuilder os;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001908
Steve Anton4daf66e2018-09-07 14:55:53 -07001909 for (const Candidate& candidate : candidates) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001910 // RFC 5245
1911 // a=candidate:<foundation> <component-id> <transport> <priority>
1912 // <connection-address> <port> typ <candidate-types>
1913 // [raddr <connection-address>] [rport <port>]
1914 // *(SP extension-att-name SP extension-att-value)
1915 std::string type;
1916 // Map the cricket candidate type to "host" / "srflx" / "prflx" / "relay"
Steve Anton4daf66e2018-09-07 14:55:53 -07001917 if (candidate.type() == cricket::LOCAL_PORT_TYPE) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001918 type = kCandidateHost;
Steve Anton4daf66e2018-09-07 14:55:53 -07001919 } else if (candidate.type() == cricket::STUN_PORT_TYPE) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001920 type = kCandidateSrflx;
Steve Anton4daf66e2018-09-07 14:55:53 -07001921 } else if (candidate.type() == cricket::RELAY_PORT_TYPE) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001922 type = kCandidateRelay;
Steve Anton4daf66e2018-09-07 14:55:53 -07001923 } else if (candidate.type() == cricket::PRFLX_PORT_TYPE) {
Honghai Zhang7fb69db2016-03-14 11:59:18 -07001924 type = kCandidatePrflx;
1925 // Peer reflexive candidate may be signaled for being removed.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001926 } else {
nissec80e7412017-01-11 05:56:46 -08001927 RTC_NOTREACHED();
Peter Thatcher019087f2015-04-28 09:06:26 -07001928 // Never write out candidates if we don't know the type.
1929 continue;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001930 }
1931
1932 InitAttrLine(kAttributeCandidate, &os);
Steve Anton4daf66e2018-09-07 14:55:53 -07001933 os << kSdpDelimiterColon << candidate.foundation() << " "
1934 << candidate.component() << " " << candidate.protocol() << " "
1935 << candidate.priority() << " "
1936 << (candidate.address().ipaddr().IsNil()
1937 ? candidate.address().hostname()
1938 : candidate.address().ipaddr().ToString())
1939 << " " << candidate.address().PortAsString() << " "
1940 << kAttributeCandidateTyp << " " << type << " ";
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001941
1942 // Related address
Steve Anton4daf66e2018-09-07 14:55:53 -07001943 if (!candidate.related_address().IsNil()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001944 os << kAttributeCandidateRaddr << " "
Steve Anton4daf66e2018-09-07 14:55:53 -07001945 << candidate.related_address().ipaddr().ToString() << " "
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001946 << kAttributeCandidateRport << " "
Steve Anton4daf66e2018-09-07 14:55:53 -07001947 << candidate.related_address().PortAsString() << " ";
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001948 }
1949
Steve Anton4daf66e2018-09-07 14:55:53 -07001950 if (candidate.protocol() == cricket::TCP_PROTOCOL_NAME) {
1951 os << kTcpCandidateType << " " << candidate.tcptype() << " ";
mallinath@webrtc.org2d60c5e2014-08-08 22:29:20 +00001952 }
1953
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001954 // Extensions
Steve Anton4daf66e2018-09-07 14:55:53 -07001955 os << kAttributeCandidateGeneration << " " << candidate.generation();
1956 if (include_ufrag && !candidate.username().empty()) {
1957 os << " " << kAttributeCandidateUfrag << " " << candidate.username();
honghaiza54a0802015-12-16 18:37:23 -08001958 }
Steve Anton4daf66e2018-09-07 14:55:53 -07001959 if (candidate.network_id() > 0) {
1960 os << " " << kAttributeCandidateNetworkId << " "
1961 << candidate.network_id();
honghaiza0c44ea2016-03-23 16:07:48 -07001962 }
Steve Anton4daf66e2018-09-07 14:55:53 -07001963 if (candidate.network_cost() > 0) {
1964 os << " " << kAttributeCandidateNetworkCost << " "
1965 << candidate.network_cost();
honghaize1a0c942016-02-16 14:54:56 -08001966 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001967
1968 AddLine(os.str(), message);
1969 }
1970}
1971
1972void BuildIceOptions(const std::vector<std::string>& transport_options,
1973 std::string* message) {
1974 if (!transport_options.empty()) {
Jonas Olssonec9e4922018-09-05 09:53:49 +02001975 rtc::StringBuilder os;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001976 InitAttrLine(kAttributeIceOption, &os);
1977 os << kSdpDelimiterColon << transport_options[0];
1978 for (size_t i = 1; i < transport_options.size(); ++i) {
1979 os << kSdpDelimiterSpace << transport_options[i];
1980 }
1981 AddLine(os.str(), message);
1982 }
1983}
1984
pthatcher@webrtc.org3341b402015-02-13 21:14:22 +00001985bool IsRtp(const std::string& protocol) {
1986 return protocol.empty() ||
Yves Gerey665174f2018-06-19 15:03:05 +02001987 (protocol.find(cricket::kMediaProtocolRtpPrefix) != std::string::npos);
pthatcher@webrtc.org3341b402015-02-13 21:14:22 +00001988}
1989
1990bool IsDtlsSctp(const std::string& protocol) {
1991 // This intentionally excludes "SCTP" and "SCTP/DTLS".
lally@webrtc.orga7470932015-02-24 20:19:21 +00001992 return protocol.find(cricket::kMediaProtocolDtlsSctp) != std::string::npos;
pthatcher@webrtc.org3341b402015-02-13 21:14:22 +00001993}
1994
zhihuang38989e52017-03-21 11:04:53 -07001995bool ParseConnectionData(const std::string& line,
1996 rtc::SocketAddress* addr,
1997 SdpParseError* error) {
1998 // Parse the line from left to right.
1999 std::string token;
2000 std::string rightpart;
2001 // RFC 4566
2002 // c=<nettype> <addrtype> <connection-address>
2003 // Skip the "c="
Jonas Olssonec9e4922018-09-05 09:53:49 +02002004 if (!rtc::tokenize_first(line, kSdpDelimiterEqualChar, &token, &rightpart)) {
zhihuang38989e52017-03-21 11:04:53 -07002005 return ParseFailed(line, "Failed to parse the network type.", error);
2006 }
2007
2008 // Extract and verify the <nettype>
Jonas Olssonec9e4922018-09-05 09:53:49 +02002009 if (!rtc::tokenize_first(rightpart, kSdpDelimiterSpaceChar, &token,
2010 &rightpart) ||
zhihuang38989e52017-03-21 11:04:53 -07002011 token != kConnectionNettype) {
2012 return ParseFailed(line,
2013 "Failed to parse the connection data. The network type "
2014 "is not currently supported.",
2015 error);
2016 }
2017
2018 // Extract the "<addrtype>" and "<connection-address>".
Jonas Olssonec9e4922018-09-05 09:53:49 +02002019 if (!rtc::tokenize_first(rightpart, kSdpDelimiterSpaceChar, &token,
2020 &rightpart)) {
zhihuang38989e52017-03-21 11:04:53 -07002021 return ParseFailed(line, "Failed to parse the address type.", error);
2022 }
2023
2024 // The rightpart part should be the IP address without the slash which is used
2025 // for multicast.
2026 if (rightpart.find('/') != std::string::npos) {
2027 return ParseFailed(line,
2028 "Failed to parse the connection data. Multicast is not "
2029 "currently supported.",
2030 error);
2031 }
2032 addr->SetIP(rightpart);
2033
2034 // Verify that the addrtype matches the type of the parsed address.
2035 if ((addr->family() == AF_INET && token != "IP4") ||
2036 (addr->family() == AF_INET6 && token != "IP6")) {
2037 addr->Clear();
2038 return ParseFailed(
2039 line,
2040 "Failed to parse the connection data. The address type is mismatching.",
2041 error);
2042 }
2043 return true;
2044}
2045
2046bool ParseSessionDescription(const std::string& message,
2047 size_t* pos,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002048 std::string* session_id,
2049 std::string* session_version,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002050 TransportDescription* session_td,
2051 RtpHeaderExtensions* session_extmaps,
zhihuang38989e52017-03-21 11:04:53 -07002052 rtc::SocketAddress* connection_addr,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002053 cricket::SessionDescription* desc,
2054 SdpParseError* error) {
2055 std::string line;
2056
deadbeefc80741f2015-10-22 13:14:45 -07002057 desc->set_msid_supported(false);
Johannes Kron9581bc42018-10-23 10:17:39 +02002058 desc->set_extmap_allow_mixed(false);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002059 // RFC 4566
2060 // v= (protocol version)
2061 if (!GetLineWithType(message, pos, &line, kLineTypeVersion)) {
Yves Gerey665174f2018-06-19 15:03:05 +02002062 return ParseFailedExpectLine(message, *pos, kLineTypeVersion, std::string(),
2063 error);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002064 }
2065 // RFC 4566
2066 // o=<username> <sess-id> <sess-version> <nettype> <addrtype>
2067 // <unicast-address>
2068 if (!GetLineWithType(message, pos, &line, kLineTypeOrigin)) {
Yves Gerey665174f2018-06-19 15:03:05 +02002069 return ParseFailedExpectLine(message, *pos, kLineTypeOrigin, std::string(),
2070 error);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002071 }
2072 std::vector<std::string> fields;
Jonas Olssonec9e4922018-09-05 09:53:49 +02002073 rtc::split(line.substr(kLinePrefixLength), kSdpDelimiterSpaceChar, &fields);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002074 const size_t expected_fields = 6;
2075 if (fields.size() != expected_fields) {
2076 return ParseFailedExpectFieldNum(line, expected_fields, error);
2077 }
2078 *session_id = fields[1];
2079 *session_version = fields[2];
2080
2081 // RFC 4566
2082 // s= (session name)
2083 if (!GetLineWithType(message, pos, &line, kLineTypeSessionName)) {
2084 return ParseFailedExpectLine(message, *pos, kLineTypeSessionName,
2085 std::string(), error);
2086 }
2087
Danil Chapovalov66cadcc2018-06-19 16:47:43 +02002088 // absl::optional lines
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002089 // Those are the optional lines, so shouldn't return false if not present.
2090 // RFC 4566
2091 // i=* (session information)
2092 GetLineWithType(message, pos, &line, kLineTypeSessionInfo);
2093
2094 // RFC 4566
2095 // u=* (URI of description)
2096 GetLineWithType(message, pos, &line, kLineTypeSessionUri);
2097
2098 // RFC 4566
2099 // e=* (email address)
2100 GetLineWithType(message, pos, &line, kLineTypeSessionEmail);
2101
2102 // RFC 4566
2103 // p=* (phone number)
2104 GetLineWithType(message, pos, &line, kLineTypeSessionPhone);
2105
2106 // RFC 4566
2107 // c=* (connection information -- not required if included in
2108 // all media)
zhihuang38989e52017-03-21 11:04:53 -07002109 if (GetLineWithType(message, pos, &line, kLineTypeConnection)) {
2110 if (!ParseConnectionData(line, connection_addr, error)) {
2111 return false;
2112 }
2113 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002114
2115 // RFC 4566
2116 // b=* (zero or more bandwidth information lines)
2117 while (GetLineWithType(message, pos, &line, kLineTypeSessionBandwidth)) {
2118 // By pass zero or more b lines.
2119 }
2120
2121 // RFC 4566
2122 // One or more time descriptions ("t=" and "r=" lines; see below)
2123 // t= (time the session is active)
2124 // r=* (zero or more repeat times)
2125 // Ensure there's at least one time description
2126 if (!GetLineWithType(message, pos, &line, kLineTypeTiming)) {
2127 return ParseFailedExpectLine(message, *pos, kLineTypeTiming, std::string(),
2128 error);
2129 }
2130
2131 while (GetLineWithType(message, pos, &line, kLineTypeRepeatTimes)) {
2132 // By pass zero or more r lines.
2133 }
2134
2135 // Go through the rest of the time descriptions
2136 while (GetLineWithType(message, pos, &line, kLineTypeTiming)) {
2137 while (GetLineWithType(message, pos, &line, kLineTypeRepeatTimes)) {
2138 // By pass zero or more r lines.
2139 }
2140 }
2141
2142 // RFC 4566
2143 // z=* (time zone adjustments)
2144 GetLineWithType(message, pos, &line, kLineTypeTimeZone);
2145
2146 // RFC 4566
2147 // k=* (encryption key)
2148 GetLineWithType(message, pos, &line, kLineTypeEncryptionKey);
2149
2150 // RFC 4566
2151 // a=* (zero or more session attribute lines)
2152 while (GetLineWithType(message, pos, &line, kLineTypeAttributes)) {
2153 if (HasAttribute(line, kAttributeGroup)) {
2154 if (!ParseGroupAttribute(line, desc, error)) {
2155 return false;
2156 }
2157 } else if (HasAttribute(line, kAttributeIceUfrag)) {
Yves Gerey665174f2018-06-19 15:03:05 +02002158 if (!GetValue(line, kAttributeIceUfrag, &(session_td->ice_ufrag),
2159 error)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002160 return false;
2161 }
2162 } else if (HasAttribute(line, kAttributeIcePwd)) {
2163 if (!GetValue(line, kAttributeIcePwd, &(session_td->ice_pwd), error)) {
2164 return false;
2165 }
2166 } else if (HasAttribute(line, kAttributeIceLite)) {
2167 session_td->ice_mode = cricket::ICEMODE_LITE;
2168 } else if (HasAttribute(line, kAttributeIceOption)) {
2169 if (!ParseIceOptions(line, &(session_td->transport_options), error)) {
2170 return false;
2171 }
2172 } else if (HasAttribute(line, kAttributeFingerprint)) {
2173 if (session_td->identity_fingerprint.get()) {
2174 return ParseFailed(
2175 line,
2176 "Can't have multiple fingerprint attributes at the same level.",
2177 error);
2178 }
Steve Anton4905edb2018-10-15 19:27:44 -07002179 std::unique_ptr<rtc::SSLFingerprint> fingerprint;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002180 if (!ParseFingerprintAttribute(line, &fingerprint, error)) {
2181 return false;
2182 }
Steve Anton4905edb2018-10-15 19:27:44 -07002183 session_td->identity_fingerprint = std::move(fingerprint);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00002184 } else if (HasAttribute(line, kAttributeSetup)) {
2185 if (!ParseDtlsSetup(line, &(session_td->connection_role), error)) {
2186 return false;
2187 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002188 } else if (HasAttribute(line, kAttributeMsidSemantics)) {
2189 std::string semantics;
2190 if (!GetValue(line, kAttributeMsidSemantics, &semantics, error)) {
2191 return false;
2192 }
deadbeefc80741f2015-10-22 13:14:45 -07002193 desc->set_msid_supported(
2194 CaseInsensitiveFind(semantics, kMediaStreamSemantic));
Johannes Kron0854eb62018-10-10 22:33:20 +02002195 } else if (HasAttribute(line, kAttributeExtmapAllowMixed)) {
Johannes Kron9581bc42018-10-23 10:17:39 +02002196 desc->set_extmap_allow_mixed(true);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002197 } else if (HasAttribute(line, kAttributeExtmap)) {
isheriff6f8d6862016-05-26 11:24:55 -07002198 RtpExtension extmap;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002199 if (!ParseExtmap(line, &extmap, error)) {
2200 return false;
2201 }
2202 session_extmaps->push_back(extmap);
2203 }
2204 }
2205
2206 return true;
2207}
2208
2209bool ParseGroupAttribute(const std::string& line,
2210 cricket::SessionDescription* desc,
2211 SdpParseError* error) {
nisseede5da42017-01-12 05:15:36 -08002212 RTC_DCHECK(desc != NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002213
2214 // RFC 5888 and draft-holmberg-mmusic-sdp-bundle-negotiation-00
2215 // a=group:BUNDLE video voice
2216 std::vector<std::string> fields;
Jonas Olssonec9e4922018-09-05 09:53:49 +02002217 rtc::split(line.substr(kLinePrefixLength), kSdpDelimiterSpaceChar, &fields);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002218 std::string semantics;
2219 if (!GetValue(fields[0], kAttributeGroup, &semantics, error)) {
2220 return false;
2221 }
2222 cricket::ContentGroup group(semantics);
2223 for (size_t i = 1; i < fields.size(); ++i) {
2224 group.AddContentName(fields[i]);
2225 }
2226 desc->AddGroup(group);
2227 return true;
2228}
2229
Steve Anton4905edb2018-10-15 19:27:44 -07002230static bool ParseFingerprintAttribute(
2231 const std::string& line,
2232 std::unique_ptr<rtc::SSLFingerprint>* fingerprint,
2233 SdpParseError* error) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002234 if (!IsLineType(line, kLineTypeAttributes) ||
2235 !HasAttribute(line, kAttributeFingerprint)) {
2236 return ParseFailedExpectLine(line, 0, kLineTypeAttributes,
2237 kAttributeFingerprint, error);
2238 }
2239
2240 std::vector<std::string> fields;
Jonas Olssonec9e4922018-09-05 09:53:49 +02002241 rtc::split(line.substr(kLinePrefixLength), kSdpDelimiterSpaceChar, &fields);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002242 const size_t expected_fields = 2;
2243 if (fields.size() != expected_fields) {
2244 return ParseFailedExpectFieldNum(line, expected_fields, error);
2245 }
2246
2247 // The first field here is "fingerprint:<hash>.
2248 std::string algorithm;
2249 if (!GetValue(fields[0], kAttributeFingerprint, &algorithm, error)) {
2250 return false;
2251 }
2252
2253 // Downcase the algorithm. Note that we don't need to downcase the
2254 // fingerprint because hex_decode can handle upper-case.
2255 std::transform(algorithm.begin(), algorithm.end(), algorithm.begin(),
2256 ::tolower);
2257
2258 // The second field is the digest value. De-hexify it.
Steve Anton4905edb2018-10-15 19:27:44 -07002259 *fingerprint =
2260 rtc::SSLFingerprint::CreateUniqueFromRfc4572(algorithm, fields[1]);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002261 if (!*fingerprint) {
Yves Gerey665174f2018-06-19 15:03:05 +02002262 return ParseFailed(line, "Failed to create fingerprint from the digest.",
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002263 error);
2264 }
2265
2266 return true;
2267}
2268
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00002269static bool ParseDtlsSetup(const std::string& line,
2270 cricket::ConnectionRole* role,
2271 SdpParseError* error) {
2272 // setup-attr = "a=setup:" role
2273 // role = "active" / "passive" / "actpass" / "holdconn"
2274 std::vector<std::string> fields;
Jonas Olssonec9e4922018-09-05 09:53:49 +02002275 rtc::split(line.substr(kLinePrefixLength), kSdpDelimiterColonChar, &fields);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00002276 const size_t expected_fields = 2;
2277 if (fields.size() != expected_fields) {
2278 return ParseFailedExpectFieldNum(line, expected_fields, error);
2279 }
2280 std::string role_str = fields[1];
2281 if (!cricket::StringToConnectionRole(role_str, role)) {
2282 return ParseFailed(line, "Invalid attribute value.", error);
2283 }
2284 return true;
2285}
2286
deadbeef9d3584c2016-02-16 17:54:10 -08002287static bool ParseMsidAttribute(const std::string& line,
Seth Hampson5b4f0752018-04-02 16:31:36 -07002288 std::vector<std::string>* stream_ids,
deadbeef9d3584c2016-02-16 17:54:10 -08002289 std::string* track_id,
2290 SdpParseError* error) {
Seth Hampson5b4f0752018-04-02 16:31:36 -07002291 // https://datatracker.ietf.org/doc/draft-ietf-mmusic-msid/16/
deadbeef9d3584c2016-02-16 17:54:10 -08002292 // a=msid:<stream id> <track id>
2293 // msid-value = msid-id [ SP msid-appdata ]
2294 // msid-id = 1*64token-char ; see RFC 4566
2295 // msid-appdata = 1*64token-char ; see RFC 4566
2296 std::string field1;
Seth Hampson5b4f0752018-04-02 16:31:36 -07002297 std::string new_stream_id;
2298 std::string new_track_id;
Jonas Olssonec9e4922018-09-05 09:53:49 +02002299 if (!rtc::tokenize_first(line.substr(kLinePrefixLength),
2300 kSdpDelimiterSpaceChar, &field1, &new_track_id)) {
deadbeef9d3584c2016-02-16 17:54:10 -08002301 const size_t expected_fields = 2;
2302 return ParseFailedExpectFieldNum(line, expected_fields, error);
2303 }
2304
Seth Hampson5b4f0752018-04-02 16:31:36 -07002305 if (new_track_id.empty()) {
deadbeefa4549d62017-02-10 17:26:22 -08002306 return ParseFailed(line, "Missing track ID in msid attribute.", error);
2307 }
Seth Hampson5b4f0752018-04-02 16:31:36 -07002308 // All track ids should be the same within an m section in a Unified Plan SDP.
2309 if (!track_id->empty() && new_track_id.compare(*track_id) != 0) {
2310 return ParseFailed(
2311 line, "Two different track IDs in msid attribute in one m= section",
2312 error);
2313 }
2314 *track_id = new_track_id;
deadbeefa4549d62017-02-10 17:26:22 -08002315
deadbeef9d3584c2016-02-16 17:54:10 -08002316 // msid:<msid-id>
Seth Hampson5b4f0752018-04-02 16:31:36 -07002317 if (!GetValue(field1, kAttributeMsid, &new_stream_id, error)) {
deadbeef9d3584c2016-02-16 17:54:10 -08002318 return false;
2319 }
Seth Hampson5b4f0752018-04-02 16:31:36 -07002320 if (new_stream_id.empty()) {
deadbeefa4549d62017-02-10 17:26:22 -08002321 return ParseFailed(line, "Missing stream ID in msid attribute.", error);
2322 }
Seth Hampson5b4f0752018-04-02 16:31:36 -07002323 // The special value "-" indicates "no MediaStream".
2324 if (new_stream_id.compare(kNoStreamMsid) != 0) {
2325 stream_ids->push_back(new_stream_id);
2326 }
deadbeef9d3584c2016-02-16 17:54:10 -08002327 return true;
2328}
2329
Amit Hilbuchc57d5732018-12-11 15:30:11 -08002330static void RemoveInvalidRidDescriptions(const std::vector<int>& payload_types,
2331 std::vector<RidDescription>* rids) {
2332 RTC_DCHECK(rids);
2333 std::set<std::string> to_remove;
2334 std::set<std::string> unique_rids;
2335
2336 // Check the rids to see which ones should be removed.
2337 for (RidDescription& rid : *rids) {
2338 // In the case of a duplicate, the entire "a=rid" line, and all "a=rid"
2339 // lines with rid-ids that duplicate this line, are discarded and MUST NOT
2340 // be included in the SDP Answer.
2341 auto pair = unique_rids.insert(rid.rid);
2342 // Insert will "fail" if element already exists.
2343 if (!pair.second) {
2344 to_remove.insert(rid.rid);
2345 continue;
2346 }
2347
2348 // If the "a=rid" line contains a "pt=", the list of payload types
2349 // is verified against the list of valid payload types for the media
2350 // section (that is, those listed on the "m=" line). Any PT missing
2351 // from the "m=" line is discarded from the set of values in the
2352 // "pt=". If no values are left in the "pt=" parameter after this
2353 // processing, then the "a=rid" line is discarded.
2354 if (rid.payload_types.empty()) {
2355 // If formats were not specified, rid should not be removed.
2356 continue;
2357 }
2358
2359 // Note: Spec does not mention how to handle duplicate formats.
2360 // Media section does not handle duplicates either.
2361 std::set<int> removed_formats;
2362 for (int payload_type : rid.payload_types) {
2363 if (std::find(payload_types.begin(), payload_types.end(), payload_type) ==
2364 payload_types.end()) {
2365 removed_formats.insert(payload_type);
2366 }
2367 }
2368
2369 rid.payload_types.erase(
2370 std::remove_if(rid.payload_types.begin(), rid.payload_types.end(),
2371 [&removed_formats](int format) {
2372 return removed_formats.count(format) > 0;
2373 }),
2374 rid.payload_types.end());
2375
2376 // If all formats were removed then remove the rid alogether.
2377 if (rid.payload_types.empty()) {
2378 to_remove.insert(rid.rid);
2379 }
2380 }
2381
2382 // Remove every rid description that appears in the to_remove list.
2383 if (!to_remove.empty()) {
2384 rids->erase(std::remove_if(rids->begin(), rids->end(),
2385 [&to_remove](const RidDescription& rid) {
2386 return to_remove.count(rid.rid) > 0;
2387 }),
2388 rids->end());
2389 }
2390}
2391
2392// Create a new list (because SimulcastLayerList is immutable) without any
2393// layers that have a rid in the to_remove list.
2394// If a group of alternatives is empty after removing layers, the group should
2395// be removed altogether.
2396static SimulcastLayerList RemoveRidsFromSimulcastLayerList(
2397 const std::set<std::string>& to_remove,
2398 const SimulcastLayerList& layers) {
2399 SimulcastLayerList result;
2400 for (const std::vector<SimulcastLayer>& vector : layers) {
2401 std::vector<SimulcastLayer> new_layers;
2402 for (const SimulcastLayer& layer : vector) {
2403 if (to_remove.find(layer.rid) == to_remove.end()) {
2404 new_layers.push_back(layer);
2405 }
2406 }
2407 // If all layers were removed, do not add an entry.
2408 if (!new_layers.empty()) {
2409 result.AddLayerWithAlternatives(new_layers);
2410 }
2411 }
2412
2413 return result;
2414}
2415
2416// Will remove Simulcast Layers if:
2417// 1. They appear in both send and receive directions.
2418// 2. They do not appear in the list of |valid_rids|.
2419static void RemoveInvalidRidsFromSimulcast(
2420 const std::vector<RidDescription>& valid_rids,
2421 SimulcastDescription* simulcast) {
2422 RTC_DCHECK(simulcast);
2423 std::set<std::string> to_remove;
2424 std::vector<SimulcastLayer> all_send_layers =
2425 simulcast->send_layers().GetAllLayers();
2426 std::vector<SimulcastLayer> all_receive_layers =
2427 simulcast->receive_layers().GetAllLayers();
2428
2429 // If a rid appears in both send and receive directions, remove it from both.
2430 // This algorithm runs in O(n^2) time, but for small n (as is the case with
2431 // simulcast layers) it should still perform well.
2432 for (const SimulcastLayer& send_layer : all_send_layers) {
2433 if (std::find_if(all_receive_layers.begin(), all_receive_layers.end(),
2434 [&send_layer](const SimulcastLayer& layer) {
2435 return layer.rid == send_layer.rid;
2436 }) != all_receive_layers.end()) {
2437 to_remove.insert(send_layer.rid);
2438 }
2439 }
2440
2441 // Add any rid that is not in the valid list to the remove set.
2442 for (const SimulcastLayer& send_layer : all_send_layers) {
2443 if (std::find_if(valid_rids.begin(), valid_rids.end(),
2444 [&send_layer](const RidDescription& rid) {
2445 return send_layer.rid == rid.rid;
2446 }) == valid_rids.end()) {
2447 to_remove.insert(send_layer.rid);
2448 }
2449 }
2450
2451 // Add any rid that is not in the valid list to the remove set.
2452 for (const SimulcastLayer& receive_layer : all_receive_layers) {
2453 if (std::find_if(valid_rids.begin(), valid_rids.end(),
2454 [&receive_layer](const RidDescription& rid) {
2455 return receive_layer.rid == rid.rid;
2456 }) == valid_rids.end()) {
2457 to_remove.insert(receive_layer.rid);
2458 }
2459 }
2460
2461 simulcast->send_layers() =
2462 RemoveRidsFromSimulcastLayerList(to_remove, simulcast->send_layers());
2463 simulcast->receive_layers() =
2464 RemoveRidsFromSimulcastLayerList(to_remove, simulcast->receive_layers());
2465}
2466
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002467// RFC 3551
2468// PT encoding media type clock rate channels
2469// name (Hz)
2470// 0 PCMU A 8,000 1
2471// 1 reserved A
2472// 2 reserved A
2473// 3 GSM A 8,000 1
2474// 4 G723 A 8,000 1
2475// 5 DVI4 A 8,000 1
2476// 6 DVI4 A 16,000 1
2477// 7 LPC A 8,000 1
2478// 8 PCMA A 8,000 1
2479// 9 G722 A 8,000 1
2480// 10 L16 A 44,100 2
2481// 11 L16 A 44,100 1
2482// 12 QCELP A 8,000 1
2483// 13 CN A 8,000 1
2484// 14 MPA A 90,000 (see text)
2485// 15 G728 A 8,000 1
2486// 16 DVI4 A 11,025 1
2487// 17 DVI4 A 22,050 1
2488// 18 G729 A 8,000 1
2489struct StaticPayloadAudioCodec {
2490 const char* name;
2491 int clockrate;
Peter Kasting69558702016-01-12 16:26:35 -08002492 size_t channels;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002493};
2494static const StaticPayloadAudioCodec kStaticPayloadAudioCodecs[] = {
Yves Gerey665174f2018-06-19 15:03:05 +02002495 {"PCMU", 8000, 1}, {"reserved", 0, 0}, {"reserved", 0, 0},
2496 {"GSM", 8000, 1}, {"G723", 8000, 1}, {"DVI4", 8000, 1},
2497 {"DVI4", 16000, 1}, {"LPC", 8000, 1}, {"PCMA", 8000, 1},
2498 {"G722", 8000, 1}, {"L16", 44100, 2}, {"L16", 44100, 1},
2499 {"QCELP", 8000, 1}, {"CN", 8000, 1}, {"MPA", 90000, 1},
2500 {"G728", 8000, 1}, {"DVI4", 11025, 1}, {"DVI4", 22050, 1},
2501 {"G729", 8000, 1},
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002502};
2503
Yves Gerey665174f2018-06-19 15:03:05 +02002504void MaybeCreateStaticPayloadAudioCodecs(const std::vector<int>& fmts,
2505 AudioContentDescription* media_desc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002506 if (!media_desc) {
2507 return;
2508 }
deadbeef67cf2c12016-04-13 10:07:16 -07002509 RTC_DCHECK(media_desc->codecs().empty());
solenberg9fa49752016-10-08 13:02:44 -07002510 for (int payload_type : fmts) {
Yves Gerey665174f2018-06-19 15:03:05 +02002511 if (!media_desc->HasCodec(payload_type) && payload_type >= 0 &&
kjellander3e33bfe2016-06-20 07:04:09 -07002512 static_cast<uint32_t>(payload_type) <
2513 arraysize(kStaticPayloadAudioCodecs)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002514 std::string encoding_name = kStaticPayloadAudioCodecs[payload_type].name;
2515 int clock_rate = kStaticPayloadAudioCodecs[payload_type].clockrate;
Peter Kasting69558702016-01-12 16:26:35 -08002516 size_t channels = kStaticPayloadAudioCodecs[payload_type].channels;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002517 media_desc->AddCodec(cricket::AudioCodec(payload_type, encoding_name,
deadbeef67cf2c12016-04-13 10:07:16 -07002518 clock_rate, 0, channels));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002519 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002520 }
2521}
2522
2523template <class C>
Steve Anton5a1de872018-12-18 11:25:55 -08002524static std::unique_ptr<C> ParseContentDescription(
2525 const std::string& message,
2526 const cricket::MediaType media_type,
2527 int mline_index,
2528 const std::string& protocol,
2529 const std::vector<int>& payload_types,
2530 size_t* pos,
2531 std::string* content_name,
2532 bool* bundle_only,
2533 int* msid_signaling,
2534 TransportDescription* transport,
2535 std::vector<std::unique_ptr<JsepIceCandidate>>* candidates,
2536 webrtc::SdpParseError* error) {
2537 auto media_desc = absl::make_unique<C>();
deadbeef67cf2c12016-04-13 10:07:16 -07002538 if (!ParseContent(message, media_type, mline_index, protocol, payload_types,
Steve Anton5a1de872018-12-18 11:25:55 -08002539 pos, content_name, bundle_only, msid_signaling,
2540 media_desc.get(), transport, candidates, error)) {
zhihuang38989e52017-03-21 11:04:53 -07002541 return nullptr;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002542 }
2543 // Sort the codecs according to the m-line fmt list.
deadbeef67cf2c12016-04-13 10:07:16 -07002544 std::unordered_map<int, int> payload_type_preferences;
2545 // "size + 1" so that the lowest preference payload type has a preference of
2546 // 1, which is greater than the default (0) for payload types not in the fmt
2547 // list.
2548 int preference = static_cast<int>(payload_types.size() + 1);
2549 for (int pt : payload_types) {
2550 payload_type_preferences[pt] = preference--;
2551 }
2552 std::vector<typename C::CodecType> codecs = media_desc->codecs();
Yves Gerey665174f2018-06-19 15:03:05 +02002553 std::sort(codecs.begin(), codecs.end(),
2554 [&payload_type_preferences](const typename C::CodecType& a,
2555 const typename C::CodecType& b) {
2556 return payload_type_preferences[a.id] >
2557 payload_type_preferences[b.id];
2558 });
deadbeef67cf2c12016-04-13 10:07:16 -07002559 media_desc->set_codecs(codecs);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002560 return media_desc;
2561}
2562
Steve Anton5a1de872018-12-18 11:25:55 -08002563bool ParseMediaDescription(
2564 const std::string& message,
2565 const TransportDescription& session_td,
2566 const RtpHeaderExtensions& session_extmaps,
2567 size_t* pos,
2568 const rtc::SocketAddress& session_connection_addr,
2569 cricket::SessionDescription* desc,
2570 std::vector<std::unique_ptr<JsepIceCandidate>>* candidates,
2571 SdpParseError* error) {
nisseede5da42017-01-12 05:15:36 -08002572 RTC_DCHECK(desc != NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002573 std::string line;
2574 int mline_index = -1;
Steve Antone831b8c2018-02-01 12:22:16 -08002575 int msid_signaling = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002576
2577 // Zero or more media descriptions
2578 // RFC 4566
2579 // m=<media> <port> <proto> <fmt>
2580 while (GetLineWithType(message, pos, &line, kLineTypeMedia)) {
2581 ++mline_index;
2582
2583 std::vector<std::string> fields;
Jonas Olssonec9e4922018-09-05 09:53:49 +02002584 rtc::split(line.substr(kLinePrefixLength), kSdpDelimiterSpaceChar, &fields);
zstein4b2e0822017-02-17 19:48:38 -08002585
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002586 const size_t expected_min_fields = 4;
2587 if (fields.size() < expected_min_fields) {
2588 return ParseFailedExpectMinFieldNum(line, expected_min_fields, error);
2589 }
deadbeef25ed4352016-12-12 18:37:36 -08002590 bool port_rejected = false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002591 // RFC 3264
2592 // To reject an offered stream, the port number in the corresponding stream
2593 // in the answer MUST be set to zero.
2594 if (fields[1] == kMediaPortRejected) {
deadbeef25ed4352016-12-12 18:37:36 -08002595 port_rejected = true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002596 }
2597
zhihuang38989e52017-03-21 11:04:53 -07002598 int port = 0;
2599 if (!rtc::FromString<int>(fields[1], &port) || !IsValidPort(port)) {
2600 return ParseFailed(line, "The port number is invalid", error);
2601 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002602 std::string protocol = fields[2];
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002603
2604 // <fmt>
deadbeef67cf2c12016-04-13 10:07:16 -07002605 std::vector<int> payload_types;
pthatcher@webrtc.org3341b402015-02-13 21:14:22 +00002606 if (IsRtp(protocol)) {
Yves Gerey665174f2018-06-19 15:03:05 +02002607 for (size_t j = 3; j < fields.size(); ++j) {
jiayl@webrtc.orgddb85ab2014-09-05 16:31:56 +00002608 // TODO(wu): Remove when below bug is fixed.
2609 // https://bugzilla.mozilla.org/show_bug.cgi?id=996329
pbosbb36fdf2015-07-09 07:48:14 -07002610 if (fields[j].empty() && j == fields.size() - 1) {
jiayl@webrtc.orgddb85ab2014-09-05 16:31:56 +00002611 continue;
2612 }
wu@webrtc.org36eda7c2014-04-15 20:37:30 +00002613
jiayl@webrtc.orgddb85ab2014-09-05 16:31:56 +00002614 int pl = 0;
pkasting@chromium.orge9facf82015-02-17 20:36:28 +00002615 if (!GetPayloadTypeFromString(line, fields[j], &pl, error)) {
jiayl@webrtc.orgddb85ab2014-09-05 16:31:56 +00002616 return false;
2617 }
deadbeef67cf2c12016-04-13 10:07:16 -07002618 payload_types.push_back(pl);
wu@webrtc.org5e760e72014-04-02 23:19:09 +00002619 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002620 }
2621
2622 // Make a temporary TransportDescription based on |session_td|.
2623 // Some of this gets overwritten by ParseContent.
deadbeef46eed762016-01-28 13:24:37 -08002624 TransportDescription transport(
2625 session_td.transport_options, session_td.ice_ufrag, session_td.ice_pwd,
2626 session_td.ice_mode, session_td.connection_role,
2627 session_td.identity_fingerprint.get());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002628
kwibergd1fe2812016-04-27 06:47:29 -07002629 std::unique_ptr<MediaContentDescription> content;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002630 std::string content_name;
deadbeef25ed4352016-12-12 18:37:36 -08002631 bool bundle_only = false;
Steve Antone831b8c2018-02-01 12:22:16 -08002632 int section_msid_signaling = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002633 if (HasAttribute(line, kMediaTypeVideo)) {
Steve Anton5a1de872018-12-18 11:25:55 -08002634 content = ParseContentDescription<VideoContentDescription>(
deadbeef67cf2c12016-04-13 10:07:16 -07002635 message, cricket::MEDIA_TYPE_VIDEO, mline_index, protocol,
Steve Antone831b8c2018-02-01 12:22:16 -08002636 payload_types, pos, &content_name, &bundle_only,
Steve Anton5a1de872018-12-18 11:25:55 -08002637 &section_msid_signaling, &transport, candidates, error);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002638 } else if (HasAttribute(line, kMediaTypeAudio)) {
Steve Anton5a1de872018-12-18 11:25:55 -08002639 content = ParseContentDescription<AudioContentDescription>(
deadbeef67cf2c12016-04-13 10:07:16 -07002640 message, cricket::MEDIA_TYPE_AUDIO, mline_index, protocol,
Steve Antone831b8c2018-02-01 12:22:16 -08002641 payload_types, pos, &content_name, &bundle_only,
Steve Anton5a1de872018-12-18 11:25:55 -08002642 &section_msid_signaling, &transport, candidates, error);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002643 } else if (HasAttribute(line, kMediaTypeData)) {
Steve Anton5a1de872018-12-18 11:25:55 -08002644 std::unique_ptr<DataContentDescription> data_desc =
wu@webrtc.org78187522013-10-07 23:32:02 +00002645 ParseContentDescription<DataContentDescription>(
deadbeef67cf2c12016-04-13 10:07:16 -07002646 message, cricket::MEDIA_TYPE_DATA, mline_index, protocol,
Steve Antone831b8c2018-02-01 12:22:16 -08002647 payload_types, pos, &content_name, &bundle_only,
2648 &section_msid_signaling, &transport, candidates, error);
wu@webrtc.org78187522013-10-07 23:32:02 +00002649
zstein4b2e0822017-02-17 19:48:38 -08002650 if (data_desc && IsDtlsSctp(protocol)) {
2651 int p;
2652 if (rtc::FromString(fields[3], &p)) {
Steve Anton5a1de872018-12-18 11:25:55 -08002653 if (!AddSctpDataCodec(data_desc.get(), p)) {
zstein4b2e0822017-02-17 19:48:38 -08002654 return false;
2655 }
2656 } else if (fields[3] == kDefaultSctpmapProtocol) {
2657 data_desc->set_use_sctpmap(false);
2658 }
wu@webrtc.org78187522013-10-07 23:32:02 +00002659 }
Steve Anton5a1de872018-12-18 11:25:55 -08002660
2661 content = std::move(data_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002662 } else {
Mirko Bonadei675513b2017-11-09 11:09:25 +01002663 RTC_LOG(LS_WARNING) << "Unsupported media type: " << line;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002664 continue;
2665 }
2666 if (!content.get()) {
2667 // ParseContentDescription returns NULL if failed.
2668 return false;
2669 }
2670
Steve Antone831b8c2018-02-01 12:22:16 -08002671 msid_signaling |= section_msid_signaling;
2672
deadbeef25ed4352016-12-12 18:37:36 -08002673 bool content_rejected = false;
deadbeef12771a12017-01-03 13:53:47 -08002674 // A port of 0 is not interpreted as a rejected m= section when it's
2675 // used along with a=bundle-only.
deadbeef25ed4352016-12-12 18:37:36 -08002676 if (bundle_only) {
deadbeef25ed4352016-12-12 18:37:36 -08002677 if (!port_rejected) {
deadbeef12771a12017-01-03 13:53:47 -08002678 // Usage of bundle-only with a nonzero port is unspecified. So just
2679 // ignore bundle-only if we see this.
2680 bundle_only = false;
Mirko Bonadei675513b2017-11-09 11:09:25 +01002681 RTC_LOG(LS_WARNING)
deadbeef12771a12017-01-03 13:53:47 -08002682 << "a=bundle-only attribute observed with a nonzero "
Jonas Olsson45cc8902018-02-13 10:37:07 +01002683 "port; this usage is unspecified so the attribute is being "
2684 "ignored.";
deadbeef25ed4352016-12-12 18:37:36 -08002685 }
2686 } else {
2687 // If not using bundle-only, interpret port 0 in the normal way; the m=
2688 // section is being rejected.
2689 content_rejected = port_rejected;
2690 }
2691
pthatcher@webrtc.org3341b402015-02-13 21:14:22 +00002692 if (IsRtp(protocol)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002693 // Set the extmap.
2694 if (!session_extmaps.empty() &&
2695 !content->rtp_header_extensions().empty()) {
2696 return ParseFailed("",
2697 "The a=extmap MUST be either all session level or "
2698 "all media level.",
2699 error);
2700 }
2701 for (size_t i = 0; i < session_extmaps.size(); ++i) {
2702 content->AddRtpHeaderExtension(session_extmaps[i]);
2703 }
2704 }
2705 content->set_protocol(protocol);
zhihuang38989e52017-03-21 11:04:53 -07002706
2707 // Use the session level connection address if the media level addresses are
2708 // not specified.
2709 rtc::SocketAddress address;
2710 address = content->connection_address().IsNil()
2711 ? session_connection_addr
2712 : content->connection_address();
2713 address.SetPort(port);
2714 content->set_connection_address(address);
2715
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002716 desc->AddContent(content_name,
Steve Anton5adfafd2017-12-20 16:34:00 -08002717 IsDtlsSctp(protocol) ? MediaProtocolType::kSctp
2718 : MediaProtocolType::kRtp,
deadbeef25ed4352016-12-12 18:37:36 -08002719 content_rejected, bundle_only, content.release());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002720 // Create TransportInfo with the media level "ice-pwd" and "ice-ufrag".
Steve Anton06817cd2018-12-18 15:55:30 -08002721 desc->AddTransportInfo(TransportInfo(content_name, transport));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002722 }
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00002723
Steve Antone831b8c2018-02-01 12:22:16 -08002724 desc->set_msid_signaling(msid_signaling);
2725
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00002726 size_t end_of_message = message.size();
2727 if (mline_index == -1 && *pos != end_of_message) {
2728 ParseFailed(message, *pos, "Expects m line.", error);
2729 return false;
2730 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002731 return true;
2732}
2733
2734bool VerifyCodec(const cricket::Codec& codec) {
2735 // Codec has not been populated correctly unless the name has been set. This
2736 // can happen if an SDP has an fmtp or rtcp-fb with a payload type but doesn't
2737 // have a corresponding "rtpmap" line.
htab39db842016-12-08 01:50:48 -08002738 return !codec.name.empty();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002739}
2740
2741bool VerifyAudioCodecs(const AudioContentDescription* audio_desc) {
2742 const std::vector<cricket::AudioCodec>& codecs = audio_desc->codecs();
Steve Anton4daf66e2018-09-07 14:55:53 -07002743 return std::all_of(codecs.begin(), codecs.end(), &VerifyCodec);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002744}
2745
2746bool VerifyVideoCodecs(const VideoContentDescription* video_desc) {
2747 const std::vector<cricket::VideoCodec>& codecs = video_desc->codecs();
Steve Anton4daf66e2018-09-07 14:55:53 -07002748 return std::all_of(codecs.begin(), codecs.end(), &VerifyCodec);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002749}
2750
2751void AddParameters(const cricket::CodecParameterMap& parameters,
2752 cricket::Codec* codec) {
Steve Anton4daf66e2018-09-07 14:55:53 -07002753 for (const auto& entry : parameters) {
2754 const std::string& key = entry.first;
2755 const std::string& value = entry.second;
2756 codec->SetParam(key, value);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002757 }
2758}
2759
2760void AddFeedbackParameter(const cricket::FeedbackParam& feedback_param,
2761 cricket::Codec* codec) {
2762 codec->AddFeedbackParam(feedback_param);
2763}
2764
2765void AddFeedbackParameters(const cricket::FeedbackParams& feedback_params,
2766 cricket::Codec* codec) {
Steve Anton4daf66e2018-09-07 14:55:53 -07002767 for (const cricket::FeedbackParam& param : feedback_params.params()) {
2768 codec->AddFeedbackParam(param);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002769 }
2770}
2771
2772// Gets the current codec setting associated with |payload_type|. If there
changbin.shao@webrtc.org2d25b442015-03-16 04:14:34 +00002773// is no Codec associated with that payload type it returns an empty codec
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002774// with that payload type.
2775template <class T>
changbin.shao@webrtc.org2d25b442015-03-16 04:14:34 +00002776T GetCodecWithPayloadType(const std::vector<T>& codecs, int payload_type) {
magjedb05fa242016-11-11 04:00:16 -08002777 const T* codec = FindCodecById(codecs, payload_type);
2778 if (codec)
2779 return *codec;
2780 // Return empty codec with |payload_type|.
changbin.shao@webrtc.org2d25b442015-03-16 04:14:34 +00002781 T ret_val;
magjedb05fa242016-11-11 04:00:16 -08002782 ret_val.id = payload_type;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002783 return ret_val;
2784}
2785
2786// Updates or creates a new codec entry in the audio description.
2787template <class T, class U>
2788void AddOrReplaceCodec(MediaContentDescription* content_desc, const U& codec) {
2789 T* desc = static_cast<T*>(content_desc);
2790 std::vector<U> codecs = desc->codecs();
2791 bool found = false;
Steve Anton4daf66e2018-09-07 14:55:53 -07002792 for (U& existing_codec : codecs) {
2793 if (codec.id == existing_codec.id) {
2794 // Overwrite existing codec with the new codec.
2795 existing_codec = codec;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002796 found = true;
2797 break;
2798 }
2799 }
2800 if (!found) {
2801 desc->AddCodec(codec);
2802 return;
2803 }
2804 desc->set_codecs(codecs);
2805}
2806
2807// Adds or updates existing codec corresponding to |payload_type| according
2808// to |parameters|.
2809template <class T, class U>
Yves Gerey665174f2018-06-19 15:03:05 +02002810void UpdateCodec(MediaContentDescription* content_desc,
2811 int payload_type,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002812 const cricket::CodecParameterMap& parameters) {
2813 // Codec might already have been populated (from rtpmap).
changbin.shao@webrtc.org2d25b442015-03-16 04:14:34 +00002814 U new_codec = GetCodecWithPayloadType(static_cast<T*>(content_desc)->codecs(),
2815 payload_type);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002816 AddParameters(parameters, &new_codec);
2817 AddOrReplaceCodec<T, U>(content_desc, new_codec);
2818}
2819
2820// Adds or updates existing codec corresponding to |payload_type| according
2821// to |feedback_param|.
2822template <class T, class U>
Yves Gerey665174f2018-06-19 15:03:05 +02002823void UpdateCodec(MediaContentDescription* content_desc,
2824 int payload_type,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002825 const cricket::FeedbackParam& feedback_param) {
2826 // Codec might already have been populated (from rtpmap).
changbin.shao@webrtc.org2d25b442015-03-16 04:14:34 +00002827 U new_codec = GetCodecWithPayloadType(static_cast<T*>(content_desc)->codecs(),
2828 payload_type);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002829 AddFeedbackParameter(feedback_param, &new_codec);
2830 AddOrReplaceCodec<T, U>(content_desc, new_codec);
2831}
2832
jlmiller@webrtc.orga744a282015-02-18 21:37:46 +00002833template <class T>
2834bool PopWildcardCodec(std::vector<T>* codecs, T* wildcard_codec) {
2835 for (auto iter = codecs->begin(); iter != codecs->end(); ++iter) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002836 if (iter->id == kWildcardPayloadType) {
2837 *wildcard_codec = *iter;
2838 codecs->erase(iter);
2839 return true;
2840 }
2841 }
2842 return false;
2843}
2844
Yves Gerey665174f2018-06-19 15:03:05 +02002845template <class T>
jlmiller@webrtc.orga744a282015-02-18 21:37:46 +00002846void UpdateFromWildcardCodecs(cricket::MediaContentDescriptionImpl<T>* desc) {
2847 auto codecs = desc->codecs();
2848 T wildcard_codec;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002849 if (!PopWildcardCodec(&codecs, &wildcard_codec)) {
2850 return;
2851 }
jlmiller@webrtc.orga744a282015-02-18 21:37:46 +00002852 for (auto& codec : codecs) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002853 AddFeedbackParameters(wildcard_codec.feedback_params, &codec);
2854 }
jlmiller@webrtc.orga744a282015-02-18 21:37:46 +00002855 desc->set_codecs(codecs);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002856}
2857
Yves Gerey665174f2018-06-19 15:03:05 +02002858void AddAudioAttribute(const std::string& name,
2859 const std::string& value,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002860 AudioContentDescription* audio_desc) {
2861 if (value.empty()) {
2862 return;
2863 }
2864 std::vector<cricket::AudioCodec> codecs = audio_desc->codecs();
Steve Anton4daf66e2018-09-07 14:55:53 -07002865 for (cricket::AudioCodec& codec : codecs) {
2866 codec.params[name] = value;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002867 }
2868 audio_desc->set_codecs(codecs);
2869}
2870
2871bool ParseContent(const std::string& message,
Patrik Höglundb6b29e02018-06-21 16:58:01 +02002872 const cricket::MediaType media_type,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002873 int mline_index,
2874 const std::string& protocol,
deadbeef67cf2c12016-04-13 10:07:16 -07002875 const std::vector<int>& payload_types,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002876 size_t* pos,
2877 std::string* content_name,
deadbeef25ed4352016-12-12 18:37:36 -08002878 bool* bundle_only,
Steve Antone831b8c2018-02-01 12:22:16 -08002879 int* msid_signaling,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002880 MediaContentDescription* media_desc,
2881 TransportDescription* transport,
Steve Anton5a1de872018-12-18 11:25:55 -08002882 std::vector<std::unique_ptr<JsepIceCandidate>>* candidates,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002883 SdpParseError* error) {
nisseede5da42017-01-12 05:15:36 -08002884 RTC_DCHECK(media_desc != NULL);
2885 RTC_DCHECK(content_name != NULL);
2886 RTC_DCHECK(transport != NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002887
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +00002888 if (media_type == cricket::MEDIA_TYPE_AUDIO) {
Steve Antonb1c1de12017-12-21 15:14:30 -08002889 MaybeCreateStaticPayloadAudioCodecs(payload_types, media_desc->as_audio());
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +00002890 }
2891
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002892 // The media level "ice-ufrag" and "ice-pwd".
2893 // The candidates before update the media level "ice-pwd" and "ice-ufrag".
2894 Candidates candidates_orig;
2895 std::string line;
2896 std::string mline_id;
2897 // Tracks created out of the ssrc attributes.
2898 StreamParamsVec tracks;
2899 SsrcInfoVec ssrc_infos;
2900 SsrcGroupVec ssrc_groups;
2901 std::string maxptime_as_string;
2902 std::string ptime_as_string;
Seth Hampson5b4f0752018-04-02 16:31:36 -07002903 std::vector<std::string> stream_ids;
deadbeef9d3584c2016-02-16 17:54:10 -08002904 std::string track_id;
Amit Hilbuchc57d5732018-12-11 15:30:11 -08002905 SdpSerializer deserializer;
2906 std::vector<RidDescription> rids;
Amit Hilbucha2012042018-12-03 11:35:05 -08002907 SimulcastDescription simulcast;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002908
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002909 // Loop until the next m line
2910 while (!IsLineType(message, kLineTypeMedia, *pos)) {
2911 if (!GetLine(message, pos, &line)) {
2912 if (*pos >= message.size()) {
2913 break; // Done parsing
2914 } else {
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +00002915 return ParseFailed(message, *pos, "Invalid SDP line.", error);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002916 }
2917 }
2918
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002919 // RFC 4566
2920 // b=* (zero or more bandwidth information lines)
2921 if (IsLineType(line, kLineTypeSessionBandwidth)) {
2922 std::string bandwidth;
2923 if (HasAttribute(line, kApplicationSpecificMaximum)) {
2924 if (!GetValue(line, kApplicationSpecificMaximum, &bandwidth, error)) {
2925 return false;
2926 } else {
wu@webrtc.org5e760e72014-04-02 23:19:09 +00002927 int b = 0;
2928 if (!GetValueFromString(line, bandwidth, &b, error)) {
2929 return false;
2930 }
deadbeef3e8016e2017-08-03 17:49:30 -07002931 // TODO(deadbeef): Historically, applications may be setting a value
2932 // of -1 to mean "unset any previously set bandwidth limit", even
2933 // though ommitting the "b=AS" entirely will do just that. Once we've
2934 // transitioned applications to doing the right thing, it would be
2935 // better to treat this as a hard error instead of just ignoring it.
2936 if (b == -1) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01002937 RTC_LOG(LS_WARNING)
2938 << "Ignoring \"b=AS:-1\"; will be treated as \"no "
2939 "bandwidth limit\".";
deadbeef3e8016e2017-08-03 17:49:30 -07002940 continue;
2941 }
deadbeefbc88c6b2017-08-02 11:26:34 -07002942 if (b < 0) {
2943 return ParseFailed(line, "b=AS value can't be negative.", error);
2944 }
Peter Thatcherc0c3a862015-06-24 15:31:25 -07002945 // We should never use more than the default bandwidth for RTP-based
2946 // data channels. Don't allow SDP to set the bandwidth, because
2947 // that would give JS the opportunity to "break the Internet".
2948 // See: https://code.google.com/p/chromium/issues/detail?id=280726
2949 if (media_type == cricket::MEDIA_TYPE_DATA && IsRtp(protocol) &&
2950 b > cricket::kDataMaxBandwidth / 1000) {
Jonas Olssonec9e4922018-09-05 09:53:49 +02002951 rtc::StringBuilder description;
Peter Thatcherc0c3a862015-06-24 15:31:25 -07002952 description << "RTP-based data channels may not send more than "
2953 << cricket::kDataMaxBandwidth / 1000 << "kbps.";
2954 return ParseFailed(line, description.str(), error);
2955 }
deadbeefb2362572016-12-13 16:37:06 -08002956 // Prevent integer overflow.
2957 b = std::min(b, INT_MAX / 1000);
wu@webrtc.org5e760e72014-04-02 23:19:09 +00002958 media_desc->set_bandwidth(b * 1000);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002959 }
2960 }
2961 continue;
2962 }
2963
zhihuang38989e52017-03-21 11:04:53 -07002964 // Parse the media level connection data.
2965 if (IsLineType(line, kLineTypeConnection)) {
2966 rtc::SocketAddress addr;
2967 if (!ParseConnectionData(line, &addr, error)) {
2968 return false;
2969 }
2970 media_desc->set_connection_address(addr);
2971 continue;
2972 }
2973
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002974 if (!IsLineType(line, kLineTypeAttributes)) {
Steve Anton36b29d12017-10-30 09:57:42 -07002975 // TODO(deadbeef): Handle other lines if needed.
Mirko Bonadei675513b2017-11-09 11:09:25 +01002976 RTC_LOG(LS_INFO) << "Ignored line: " << line;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002977 continue;
2978 }
2979
2980 // Handle attributes common to SCTP and RTP.
2981 if (HasAttribute(line, kAttributeMid)) {
2982 // RFC 3388
2983 // mid-attribute = "a=mid:" identification-tag
2984 // identification-tag = token
2985 // Use the mid identification-tag as the content name.
2986 if (!GetValue(line, kAttributeMid, &mline_id, error)) {
2987 return false;
2988 }
2989 *content_name = mline_id;
deadbeef25ed4352016-12-12 18:37:36 -08002990 } else if (HasAttribute(line, kAttributeBundleOnly)) {
2991 *bundle_only = true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002992 } else if (HasAttribute(line, kAttributeCandidate)) {
2993 Candidate candidate;
2994 if (!ParseCandidate(line, &candidate, error, false)) {
2995 return false;
2996 }
deadbeef7bcdb692017-01-20 12:43:58 -08002997 // ParseCandidate will parse non-standard ufrag and password attributes,
2998 // since it's used for candidate trickling, but we only want to process
2999 // the "a=ice-ufrag"/"a=ice-pwd" values in a session description, so
3000 // strip them off at this point.
3001 candidate.set_username(std::string());
3002 candidate.set_password(std::string());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003003 candidates_orig.push_back(candidate);
3004 } else if (HasAttribute(line, kAttributeIceUfrag)) {
3005 if (!GetValue(line, kAttributeIceUfrag, &transport->ice_ufrag, error)) {
3006 return false;
3007 }
3008 } else if (HasAttribute(line, kAttributeIcePwd)) {
3009 if (!GetValue(line, kAttributeIcePwd, &transport->ice_pwd, error)) {
3010 return false;
3011 }
3012 } else if (HasAttribute(line, kAttributeIceOption)) {
3013 if (!ParseIceOptions(line, &transport->transport_options, error)) {
3014 return false;
3015 }
3016 } else if (HasAttribute(line, kAttributeFmtp)) {
3017 if (!ParseFmtpAttributes(line, media_type, media_desc, error)) {
3018 return false;
3019 }
3020 } else if (HasAttribute(line, kAttributeFingerprint)) {
Steve Anton4905edb2018-10-15 19:27:44 -07003021 std::unique_ptr<rtc::SSLFingerprint> fingerprint;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003022 if (!ParseFingerprintAttribute(line, &fingerprint, error)) {
3023 return false;
3024 }
Steve Anton4905edb2018-10-15 19:27:44 -07003025 transport->identity_fingerprint = std::move(fingerprint);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00003026 } else if (HasAttribute(line, kAttributeSetup)) {
3027 if (!ParseDtlsSetup(line, &(transport->connection_role), error)) {
3028 return false;
3029 }
pthatcher@webrtc.org3341b402015-02-13 21:14:22 +00003030 } else if (IsDtlsSctp(protocol) && HasAttribute(line, kAttributeSctpPort)) {
deadbeef7e146cb2016-09-28 10:04:34 -07003031 if (media_type != cricket::MEDIA_TYPE_DATA) {
3032 return ParseFailed(
3033 line, "sctp-port attribute found in non-data media description.",
3034 error);
3035 }
jiayl@webrtc.orgddb85ab2014-09-05 16:31:56 +00003036 int sctp_port;
3037 if (!ParseSctpPort(line, &sctp_port, error)) {
3038 return false;
3039 }
Steve Antonb1c1de12017-12-21 15:14:30 -08003040 if (!AddSctpDataCodec(media_desc->as_data(), sctp_port)) {
jiayl@webrtc.orgddb85ab2014-09-05 16:31:56 +00003041 return false;
3042 }
pthatcher@webrtc.org3341b402015-02-13 21:14:22 +00003043 } else if (IsRtp(protocol)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003044 //
3045 // RTP specific attrubtes
3046 //
3047 if (HasAttribute(line, kAttributeRtcpMux)) {
3048 media_desc->set_rtcp_mux(true);
deadbeef13871492015-12-09 12:37:51 -08003049 } else if (HasAttribute(line, kAttributeRtcpReducedSize)) {
3050 media_desc->set_rtcp_reduced_size(true);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003051 } else if (HasAttribute(line, kAttributeSsrcGroup)) {
3052 if (!ParseSsrcGroupAttribute(line, &ssrc_groups, error)) {
3053 return false;
3054 }
3055 } else if (HasAttribute(line, kAttributeSsrc)) {
Steve Antone831b8c2018-02-01 12:22:16 -08003056 if (!ParseSsrcAttribute(line, &ssrc_infos, msid_signaling, error)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003057 return false;
3058 }
3059 } else if (HasAttribute(line, kAttributeCrypto)) {
3060 if (!ParseCryptoAttribute(line, media_desc, error)) {
3061 return false;
3062 }
3063 } else if (HasAttribute(line, kAttributeRtpmap)) {
deadbeef67cf2c12016-04-13 10:07:16 -07003064 if (!ParseRtpmapAttribute(line, media_type, payload_types, media_desc,
3065 error)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003066 return false;
3067 }
3068 } else if (HasAttribute(line, kCodecParamMaxPTime)) {
3069 if (!GetValue(line, kCodecParamMaxPTime, &maxptime_as_string, error)) {
3070 return false;
3071 }
3072 } else if (HasAttribute(line, kAttributeRtcpFb)) {
3073 if (!ParseRtcpFbAttribute(line, media_type, media_desc, error)) {
3074 return false;
3075 }
3076 } else if (HasAttribute(line, kCodecParamPTime)) {
3077 if (!GetValue(line, kCodecParamPTime, &ptime_as_string, error)) {
3078 return false;
3079 }
3080 } else if (HasAttribute(line, kAttributeSendOnly)) {
Steve Anton4e70a722017-11-28 14:57:10 -08003081 media_desc->set_direction(RtpTransceiverDirection::kSendOnly);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003082 } else if (HasAttribute(line, kAttributeRecvOnly)) {
Steve Anton4e70a722017-11-28 14:57:10 -08003083 media_desc->set_direction(RtpTransceiverDirection::kRecvOnly);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003084 } else if (HasAttribute(line, kAttributeInactive)) {
Steve Anton4e70a722017-11-28 14:57:10 -08003085 media_desc->set_direction(RtpTransceiverDirection::kInactive);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003086 } else if (HasAttribute(line, kAttributeSendRecv)) {
Steve Anton4e70a722017-11-28 14:57:10 -08003087 media_desc->set_direction(RtpTransceiverDirection::kSendRecv);
Johannes Kron0854eb62018-10-10 22:33:20 +02003088 } else if (HasAttribute(line, kAttributeExtmapAllowMixed)) {
Johannes Kron9581bc42018-10-23 10:17:39 +02003089 media_desc->set_extmap_allow_mixed_enum(
Johannes Kron0854eb62018-10-10 22:33:20 +02003090 MediaContentDescription::kMedia);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003091 } else if (HasAttribute(line, kAttributeExtmap)) {
isheriff6f8d6862016-05-26 11:24:55 -07003092 RtpExtension extmap;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003093 if (!ParseExtmap(line, &extmap, error)) {
3094 return false;
3095 }
3096 media_desc->AddRtpHeaderExtension(extmap);
3097 } else if (HasAttribute(line, kAttributeXGoogleFlag)) {
3098 // Experimental attribute. Conference mode activates more aggressive
3099 // AEC and NS settings.
Steve Anton36b29d12017-10-30 09:57:42 -07003100 // TODO(deadbeef): expose API to set these directly.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003101 std::string flag_value;
3102 if (!GetValue(line, kAttributeXGoogleFlag, &flag_value, error)) {
3103 return false;
3104 }
3105 if (flag_value.compare(kValueConference) == 0)
3106 media_desc->set_conference_mode(true);
deadbeef9d3584c2016-02-16 17:54:10 -08003107 } else if (HasAttribute(line, kAttributeMsid)) {
Seth Hampson5b4f0752018-04-02 16:31:36 -07003108 if (!ParseMsidAttribute(line, &stream_ids, &track_id, error)) {
deadbeef9d3584c2016-02-16 17:54:10 -08003109 return false;
3110 }
Steve Antone831b8c2018-02-01 12:22:16 -08003111 *msid_signaling |= cricket::kMsidSignalingMediaSection;
Amit Hilbuchc57d5732018-12-11 15:30:11 -08003112 } else if (HasAttribute(line, kAttributeRid)) {
3113 const size_t kRidPrefixLength =
3114 kLinePrefixLength + arraysize(kAttributeRid);
3115 if (line.size() <= kRidPrefixLength) {
3116 RTC_LOG(LS_INFO) << "Ignoring empty RID attribute: " << line;
3117 continue;
3118 }
3119 RTCErrorOr<RidDescription> error_or_rid_description =
3120 deserializer.DeserializeRidDescription(
3121 line.substr(kRidPrefixLength));
3122
3123 // Malformed a=rid lines are discarded.
3124 if (!error_or_rid_description.ok()) {
3125 RTC_LOG(LS_INFO) << "Ignoring malformed RID line: '" << line
3126 << "'. Error: "
3127 << error_or_rid_description.error().message();
3128 continue;
3129 }
3130
3131 rids.push_back(error_or_rid_description.MoveValue());
Amit Hilbucha2012042018-12-03 11:35:05 -08003132 } else if (HasAttribute(line, kAttributeSimulcast)) {
3133 const size_t kSimulcastPrefixLength =
3134 kLinePrefixLength + arraysize(kAttributeSimulcast);
3135 if (line.size() <= kSimulcastPrefixLength) {
3136 return ParseFailed(line, "Simulcast attribute is empty.", error);
3137 }
3138
3139 if (!simulcast.empty()) {
3140 return ParseFailed(line, "Multiple Simulcast attributes specified.",
3141 error);
3142 }
3143
Amit Hilbucha2012042018-12-03 11:35:05 -08003144 RTCErrorOr<SimulcastDescription> error_or_simulcast =
3145 deserializer.DeserializeSimulcastDescription(
3146 line.substr(kSimulcastPrefixLength));
3147 if (!error_or_simulcast.ok()) {
3148 return ParseFailed(line,
3149 std::string("Malformed simulcast line: ") +
3150 error_or_simulcast.error().message(),
3151 error);
3152 }
3153
3154 simulcast = error_or_simulcast.value();
3155 } else {
3156 // Unrecognized attribute in RTP protocol.
3157 RTC_LOG(LS_INFO) << "Ignored line: " << line;
3158 continue;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003159 }
3160 } else {
3161 // Only parse lines that we are interested of.
Mirko Bonadei675513b2017-11-09 11:09:25 +01003162 RTC_LOG(LS_INFO) << "Ignored line: " << line;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003163 continue;
3164 }
3165 }
3166
Amit Hilbuchc57d5732018-12-11 15:30:11 -08003167 // Remove duplicate or inconsistent rids.
3168 RemoveInvalidRidDescriptions(payload_types, &rids);
3169
3170 // If simulcast is specifed, split the rids into send and receive.
3171 // Rids that do not appear in simulcast attribute will be removed.
3172 // If it is not specified, we assume that all rids are for send layers.
Amit Hilbuchb7446ed2019-01-28 12:25:25 -08003173 std::vector<RidDescription> send_rids;
Amit Hilbuchc57d5732018-12-11 15:30:11 -08003174 if (!simulcast.empty()) {
3175 // Verify that the rids in simulcast match rids in sdp.
3176 RemoveInvalidRidsFromSimulcast(rids, &simulcast);
3177
3178 // Use simulcast description to figure out Send / Receive RIDs.
3179 std::map<std::string, RidDescription> rid_map;
3180 for (const RidDescription& rid : rids) {
3181 rid_map[rid.rid] = rid;
3182 }
3183
3184 for (const auto& layer : simulcast.send_layers().GetAllLayers()) {
3185 auto iter = rid_map.find(layer.rid);
3186 RTC_DCHECK(iter != rid_map.end());
3187 send_rids.push_back(iter->second);
3188 }
3189
Amit Hilbuchc57d5732018-12-11 15:30:11 -08003190 media_desc->set_simulcast_description(simulcast);
3191 } else {
3192 send_rids = rids;
3193 }
3194
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003195 // Create tracks from the |ssrc_infos|.
Taylor Brandstetter5de6b752016-03-09 17:02:30 -08003196 // If the stream_id/track_id for all SSRCS are identical, one StreamParams
3197 // will be created in CreateTracksFromSsrcInfos, containing all the SSRCs from
3198 // the m= section.
Seth Hampson5897a6e2018-04-03 11:16:33 -07003199 if (!ssrc_infos.empty()) {
3200 CreateTracksFromSsrcInfos(ssrc_infos, stream_ids, track_id, &tracks,
3201 *msid_signaling);
3202 } else if (media_type != cricket::MEDIA_TYPE_DATA &&
3203 (*msid_signaling & cricket::kMsidSignalingMediaSection)) {
3204 // If the stream_ids/track_id was signaled but SSRCs were unsignaled we
3205 // still create a track. This isn't done for data media types because
3206 // StreamParams aren't used for SCTP streams, and RTP data channels don't
3207 // support unsignaled SSRCs.
Amit Hilbuchc57d5732018-12-11 15:30:11 -08003208 CreateTrackWithNoSsrcs(stream_ids, track_id, send_rids, &tracks);
3209 }
3210
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003211 // Add the ssrc group to the track.
Steve Anton4daf66e2018-09-07 14:55:53 -07003212 for (const SsrcGroup& ssrc_group : ssrc_groups) {
3213 if (ssrc_group.ssrcs.empty()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003214 continue;
3215 }
Steve Anton4daf66e2018-09-07 14:55:53 -07003216 uint32_t ssrc = ssrc_group.ssrcs.front();
3217 for (StreamParams& track : tracks) {
3218 if (track.has_ssrc(ssrc)) {
3219 track.ssrc_groups.push_back(ssrc_group);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003220 }
3221 }
3222 }
3223
3224 // Add the new tracks to the |media_desc|.
deadbeef9d3584c2016-02-16 17:54:10 -08003225 for (StreamParams& track : tracks) {
3226 media_desc->AddStream(track);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003227 }
3228
3229 if (media_type == cricket::MEDIA_TYPE_AUDIO) {
Steve Antonb1c1de12017-12-21 15:14:30 -08003230 AudioContentDescription* audio_desc = media_desc->as_audio();
jlmiller@webrtc.orga744a282015-02-18 21:37:46 +00003231 UpdateFromWildcardCodecs(audio_desc);
3232
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003233 // Verify audio codec ensures that no audio codec has been populated with
3234 // only fmtp.
3235 if (!VerifyAudioCodecs(audio_desc)) {
3236 return ParseFailed("Failed to parse audio codecs correctly.", error);
3237 }
3238 AddAudioAttribute(kCodecParamMaxPTime, maxptime_as_string, audio_desc);
3239 AddAudioAttribute(kCodecParamPTime, ptime_as_string, audio_desc);
3240 }
3241
3242 if (media_type == cricket::MEDIA_TYPE_VIDEO) {
Steve Antonb1c1de12017-12-21 15:14:30 -08003243 VideoContentDescription* video_desc = media_desc->as_video();
jlmiller@webrtc.orga744a282015-02-18 21:37:46 +00003244 UpdateFromWildcardCodecs(video_desc);
3245 // Verify video codec ensures that no video codec has been populated with
3246 // only rtcp-fb.
3247 if (!VerifyVideoCodecs(video_desc)) {
3248 return ParseFailed("Failed to parse video codecs correctly.", error);
3249 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003250 }
3251
3252 // RFC 5245
3253 // Update the candidates with the media level "ice-pwd" and "ice-ufrag".
Steve Anton4daf66e2018-09-07 14:55:53 -07003254 for (Candidate& candidate : candidates_orig) {
3255 RTC_DCHECK(candidate.username().empty() ||
3256 candidate.username() == transport->ice_ufrag);
3257 candidate.set_username(transport->ice_ufrag);
3258 RTC_DCHECK(candidate.password().empty());
3259 candidate.set_password(transport->ice_pwd);
3260 candidates->push_back(
Steve Anton5a1de872018-12-18 11:25:55 -08003261 absl::make_unique<JsepIceCandidate>(mline_id, mline_index, candidate));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003262 }
Amit Hilbucha2012042018-12-03 11:35:05 -08003263
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003264 return true;
3265}
3266
Steve Antone831b8c2018-02-01 12:22:16 -08003267bool ParseSsrcAttribute(const std::string& line,
3268 SsrcInfoVec* ssrc_infos,
3269 int* msid_signaling,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003270 SdpParseError* error) {
nisseede5da42017-01-12 05:15:36 -08003271 RTC_DCHECK(ssrc_infos != NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003272 // RFC 5576
3273 // a=ssrc:<ssrc-id> <attribute>
3274 // a=ssrc:<ssrc-id> <attribute>:<value>
3275 std::string field1, field2;
Jonas Olssonec9e4922018-09-05 09:53:49 +02003276 if (!rtc::tokenize_first(line.substr(kLinePrefixLength),
3277 kSdpDelimiterSpaceChar, &field1, &field2)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003278 const size_t expected_fields = 2;
3279 return ParseFailedExpectFieldNum(line, expected_fields, error);
3280 }
3281
3282 // ssrc:<ssrc-id>
3283 std::string ssrc_id_s;
3284 if (!GetValue(field1, kAttributeSsrc, &ssrc_id_s, error)) {
3285 return false;
3286 }
Peter Boström0c4e06b2015-10-07 12:23:21 +02003287 uint32_t ssrc_id = 0;
wu@webrtc.org5e760e72014-04-02 23:19:09 +00003288 if (!GetValueFromString(line, ssrc_id_s, &ssrc_id, error)) {
3289 return false;
3290 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003291
3292 std::string attribute;
3293 std::string value;
Jonas Olssonec9e4922018-09-05 09:53:49 +02003294 if (!rtc::tokenize_first(field2, kSdpDelimiterColonChar, &attribute,
3295 &value)) {
3296 rtc::StringBuilder description;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003297 description << "Failed to get the ssrc attribute value from " << field2
3298 << ". Expected format <attribute>:<value>.";
3299 return ParseFailed(line, description.str(), error);
3300 }
3301
3302 // Check if there's already an item for this |ssrc_id|. Create a new one if
3303 // there isn't.
Steve Anton4daf66e2018-09-07 14:55:53 -07003304 auto ssrc_info_it = std::find_if(ssrc_infos->begin(), ssrc_infos->end(),
3305 [ssrc_id](const SsrcInfo& ssrc_info) {
3306 return ssrc_info.ssrc_id == ssrc_id;
3307 });
3308 if (ssrc_info_it == ssrc_infos->end()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003309 SsrcInfo info;
3310 info.ssrc_id = ssrc_id;
3311 ssrc_infos->push_back(info);
Steve Anton4daf66e2018-09-07 14:55:53 -07003312 ssrc_info_it = ssrc_infos->end() - 1;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003313 }
Steve Anton4daf66e2018-09-07 14:55:53 -07003314 SsrcInfo& ssrc_info = *ssrc_info_it;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003315
3316 // Store the info to the |ssrc_info|.
3317 if (attribute == kSsrcAttributeCname) {
3318 // RFC 5576
3319 // cname:<value>
Steve Anton4daf66e2018-09-07 14:55:53 -07003320 ssrc_info.cname = value;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003321 } else if (attribute == kSsrcAttributeMsid) {
3322 // draft-alvestrand-mmusic-msid-00
Seth Hampson5b4f0752018-04-02 16:31:36 -07003323 // msid:identifier [appdata]
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003324 std::vector<std::string> fields;
Jonas Olssonec9e4922018-09-05 09:53:49 +02003325 rtc::split(value, kSdpDelimiterSpaceChar, &fields);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003326 if (fields.size() < 1 || fields.size() > 2) {
Yves Gerey665174f2018-06-19 15:03:05 +02003327 return ParseFailed(
3328 line, "Expected format \"msid:<identifier>[ <appdata>]\".", error);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003329 }
Steve Anton4daf66e2018-09-07 14:55:53 -07003330 ssrc_info.stream_id = fields[0];
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003331 if (fields.size() == 2) {
Steve Anton4daf66e2018-09-07 14:55:53 -07003332 ssrc_info.track_id = fields[1];
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003333 }
Steve Antone831b8c2018-02-01 12:22:16 -08003334 *msid_signaling |= cricket::kMsidSignalingSsrcAttribute;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003335 } else if (attribute == kSsrcAttributeMslabel) {
3336 // draft-alvestrand-rtcweb-mid-01
3337 // mslabel:<value>
Steve Anton4daf66e2018-09-07 14:55:53 -07003338 ssrc_info.mslabel = value;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003339 } else if (attribute == kSSrcAttributeLabel) {
3340 // The label isn't defined.
3341 // label:<value>
Steve Anton4daf66e2018-09-07 14:55:53 -07003342 ssrc_info.label = value;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003343 }
3344 return true;
3345}
3346
3347bool ParseSsrcGroupAttribute(const std::string& line,
3348 SsrcGroupVec* ssrc_groups,
3349 SdpParseError* error) {
nisseede5da42017-01-12 05:15:36 -08003350 RTC_DCHECK(ssrc_groups != NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003351 // RFC 5576
3352 // a=ssrc-group:<semantics> <ssrc-id> ...
3353 std::vector<std::string> fields;
Jonas Olssonec9e4922018-09-05 09:53:49 +02003354 rtc::split(line.substr(kLinePrefixLength), kSdpDelimiterSpaceChar, &fields);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003355 const size_t expected_min_fields = 2;
3356 if (fields.size() < expected_min_fields) {
3357 return ParseFailedExpectMinFieldNum(line, expected_min_fields, error);
3358 }
3359 std::string semantics;
3360 if (!GetValue(fields[0], kAttributeSsrcGroup, &semantics, error)) {
3361 return false;
3362 }
Peter Boström0c4e06b2015-10-07 12:23:21 +02003363 std::vector<uint32_t> ssrcs;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003364 for (size_t i = 1; i < fields.size(); ++i) {
Peter Boström0c4e06b2015-10-07 12:23:21 +02003365 uint32_t ssrc = 0;
wu@webrtc.org5e760e72014-04-02 23:19:09 +00003366 if (!GetValueFromString(line, fields[i], &ssrc, error)) {
3367 return false;
3368 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003369 ssrcs.push_back(ssrc);
3370 }
3371 ssrc_groups->push_back(SsrcGroup(semantics, ssrcs));
3372 return true;
3373}
3374
3375bool ParseCryptoAttribute(const std::string& line,
3376 MediaContentDescription* media_desc,
3377 SdpParseError* error) {
3378 std::vector<std::string> fields;
Jonas Olssonec9e4922018-09-05 09:53:49 +02003379 rtc::split(line.substr(kLinePrefixLength), kSdpDelimiterSpaceChar, &fields);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003380 // RFC 4568
3381 // a=crypto:<tag> <crypto-suite> <key-params> [<session-params>]
3382 const size_t expected_min_fields = 3;
3383 if (fields.size() < expected_min_fields) {
3384 return ParseFailedExpectMinFieldNum(line, expected_min_fields, error);
3385 }
3386 std::string tag_value;
3387 if (!GetValue(fields[0], kAttributeCrypto, &tag_value, error)) {
3388 return false;
3389 }
wu@webrtc.org5e760e72014-04-02 23:19:09 +00003390 int tag = 0;
3391 if (!GetValueFromString(line, tag_value, &tag, error)) {
3392 return false;
3393 }
jbauch083b73f2015-07-16 02:46:32 -07003394 const std::string& crypto_suite = fields[1];
3395 const std::string& key_params = fields[2];
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003396 std::string session_params;
3397 if (fields.size() > 3) {
3398 session_params = fields[3];
3399 }
Yves Gerey665174f2018-06-19 15:03:05 +02003400 media_desc->AddCrypto(
3401 CryptoParams(tag, crypto_suite, key_params, session_params));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003402 return true;
3403}
3404
3405// Updates or creates a new codec entry in the audio description with according
deadbeef67cf2c12016-04-13 10:07:16 -07003406// to |name|, |clockrate|, |bitrate|, and |channels|.
3407void UpdateCodec(int payload_type,
3408 const std::string& name,
3409 int clockrate,
3410 int bitrate,
3411 size_t channels,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003412 AudioContentDescription* audio_desc) {
3413 // Codec may already be populated with (only) optional parameters
3414 // (from an fmtp).
changbin.shao@webrtc.org2d25b442015-03-16 04:14:34 +00003415 cricket::AudioCodec codec =
3416 GetCodecWithPayloadType(audio_desc->codecs(), payload_type);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003417 codec.name = name;
3418 codec.clockrate = clockrate;
3419 codec.bitrate = bitrate;
3420 codec.channels = channels;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003421 AddOrReplaceCodec<AudioContentDescription, cricket::AudioCodec>(audio_desc,
3422 codec);
3423}
3424
3425// Updates or creates a new codec entry in the video description according to
deadbeef67cf2c12016-04-13 10:07:16 -07003426// |name|, |width|, |height|, and |framerate|.
3427void UpdateCodec(int payload_type,
3428 const std::string& name,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003429 VideoContentDescription* video_desc) {
3430 // Codec may already be populated with (only) optional parameters
3431 // (from an fmtp).
changbin.shao@webrtc.org2d25b442015-03-16 04:14:34 +00003432 cricket::VideoCodec codec =
3433 GetCodecWithPayloadType(video_desc->codecs(), payload_type);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003434 codec.name = name;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003435 AddOrReplaceCodec<VideoContentDescription, cricket::VideoCodec>(video_desc,
3436 codec);
3437}
3438
3439bool ParseRtpmapAttribute(const std::string& line,
Patrik Höglundb6b29e02018-06-21 16:58:01 +02003440 const cricket::MediaType media_type,
deadbeef67cf2c12016-04-13 10:07:16 -07003441 const std::vector<int>& payload_types,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003442 MediaContentDescription* media_desc,
3443 SdpParseError* error) {
3444 std::vector<std::string> fields;
Jonas Olssonec9e4922018-09-05 09:53:49 +02003445 rtc::split(line.substr(kLinePrefixLength), kSdpDelimiterSpaceChar, &fields);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003446 // RFC 4566
3447 // a=rtpmap:<payload type> <encoding name>/<clock rate>[/<encodingparameters>]
3448 const size_t expected_min_fields = 2;
3449 if (fields.size() < expected_min_fields) {
3450 return ParseFailedExpectMinFieldNum(line, expected_min_fields, error);
3451 }
3452 std::string payload_type_value;
3453 if (!GetValue(fields[0], kAttributeRtpmap, &payload_type_value, error)) {
3454 return false;
3455 }
wu@webrtc.org5e760e72014-04-02 23:19:09 +00003456 int payload_type = 0;
pkasting@chromium.orge9facf82015-02-17 20:36:28 +00003457 if (!GetPayloadTypeFromString(line, payload_type_value, &payload_type,
3458 error)) {
wu@webrtc.org5e760e72014-04-02 23:19:09 +00003459 return false;
3460 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003461
deadbeef67cf2c12016-04-13 10:07:16 -07003462 if (std::find(payload_types.begin(), payload_types.end(), payload_type) ==
3463 payload_types.end()) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01003464 RTC_LOG(LS_WARNING) << "Ignore rtpmap line that did not appear in the "
Jonas Olsson45cc8902018-02-13 10:37:07 +01003465 "<fmt> of the m-line: "
3466 << line;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003467 return true;
3468 }
jbauch083b73f2015-07-16 02:46:32 -07003469 const std::string& encoder = fields[1];
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003470 std::vector<std::string> codec_params;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00003471 rtc::split(encoder, '/', &codec_params);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003472 // <encoding name>/<clock rate>[/<encodingparameters>]
3473 // 2 mandatory fields
3474 if (codec_params.size() < 2 || codec_params.size() > 3) {
3475 return ParseFailed(line,
3476 "Expected format \"<encoding name>/<clock rate>"
3477 "[/<encodingparameters>]\".",
3478 error);
3479 }
jbauch083b73f2015-07-16 02:46:32 -07003480 const std::string& encoding_name = codec_params[0];
wu@webrtc.org5e760e72014-04-02 23:19:09 +00003481 int clock_rate = 0;
3482 if (!GetValueFromString(line, codec_params[1], &clock_rate, error)) {
3483 return false;
3484 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003485 if (media_type == cricket::MEDIA_TYPE_VIDEO) {
Steve Antonb1c1de12017-12-21 15:14:30 -08003486 VideoContentDescription* video_desc = media_desc->as_video();
Yves Gerey665174f2018-06-19 15:03:05 +02003487 UpdateCodec(payload_type, encoding_name, video_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003488 } else if (media_type == cricket::MEDIA_TYPE_AUDIO) {
3489 // RFC 4566
3490 // For audio streams, <encoding parameters> indicates the number
3491 // of audio channels. This parameter is OPTIONAL and may be
3492 // omitted if the number of channels is one, provided that no
3493 // additional parameters are needed.
Peter Kasting69558702016-01-12 16:26:35 -08003494 size_t channels = 1;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003495 if (codec_params.size() == 3) {
wu@webrtc.org5e760e72014-04-02 23:19:09 +00003496 if (!GetValueFromString(line, codec_params[2], &channels, error)) {
3497 return false;
3498 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003499 }
Steve Antonb1c1de12017-12-21 15:14:30 -08003500 AudioContentDescription* audio_desc = media_desc->as_audio();
ossue1405ad2017-01-23 08:55:48 -08003501 UpdateCodec(payload_type, encoding_name, clock_rate, 0, channels,
deadbeef67cf2c12016-04-13 10:07:16 -07003502 audio_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003503 } else if (media_type == cricket::MEDIA_TYPE_DATA) {
Steve Antonb1c1de12017-12-21 15:14:30 -08003504 DataContentDescription* data_desc = media_desc->as_data();
deadbeef67cf2c12016-04-13 10:07:16 -07003505 data_desc->AddCodec(cricket::DataCodec(payload_type, encoding_name));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003506 }
3507 return true;
3508}
3509
Yves Gerey665174f2018-06-19 15:03:05 +02003510bool ParseFmtpParam(const std::string& line,
3511 std::string* parameter,
3512 std::string* value,
3513 SdpParseError* error) {
Jonas Olssonec9e4922018-09-05 09:53:49 +02003514 if (!rtc::tokenize_first(line, kSdpDelimiterEqualChar, parameter, value)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003515 ParseFailed(line, "Unable to parse fmtp parameter. \'=\' missing.", error);
3516 return false;
3517 }
3518 // a=fmtp:<payload_type> <param1>=<value1>; <param2>=<value2>; ...
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003519 return true;
3520}
3521
Yves Gerey665174f2018-06-19 15:03:05 +02003522bool ParseFmtpAttributes(const std::string& line,
Patrik Höglundb6b29e02018-06-21 16:58:01 +02003523 const cricket::MediaType media_type,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003524 MediaContentDescription* media_desc,
3525 SdpParseError* error) {
3526 if (media_type != cricket::MEDIA_TYPE_AUDIO &&
3527 media_type != cricket::MEDIA_TYPE_VIDEO) {
3528 return true;
3529 }
Donald Curtis0e07f922015-05-15 09:21:23 -07003530
3531 std::string line_payload;
3532 std::string line_params;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003533
3534 // RFC 5576
3535 // a=fmtp:<format> <format specific parameters>
3536 // At least two fields, whereas the second one is any of the optional
3537 // parameters.
Jonas Olssonec9e4922018-09-05 09:53:49 +02003538 if (!rtc::tokenize_first(line.substr(kLinePrefixLength),
3539 kSdpDelimiterSpaceChar, &line_payload,
3540 &line_params)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003541 ParseFailedExpectMinFieldNum(line, 2, error);
3542 return false;
3543 }
3544
Donald Curtis0e07f922015-05-15 09:21:23 -07003545 // Parse out the payload information.
pkasting@chromium.orgd3245462015-02-23 21:28:22 +00003546 std::string payload_type_str;
Donald Curtis0e07f922015-05-15 09:21:23 -07003547 if (!GetValue(line_payload, kAttributeFmtp, &payload_type_str, error)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003548 return false;
3549 }
3550
Donald Curtis0e07f922015-05-15 09:21:23 -07003551 int payload_type = 0;
Donald Curtis144d0182015-05-15 13:14:24 -07003552 if (!GetPayloadTypeFromString(line_payload, payload_type_str, &payload_type,
3553 error)) {
Donald Curtis0e07f922015-05-15 09:21:23 -07003554 return false;
3555 }
3556
3557 // Parse out format specific parameters.
3558 std::vector<std::string> fields;
Jonas Olssonec9e4922018-09-05 09:53:49 +02003559 rtc::split(line_params, kSdpDelimiterSemicolonChar, &fields);
Donald Curtis0e07f922015-05-15 09:21:23 -07003560
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003561 cricket::CodecParameterMap codec_params;
Donald Curtis144d0182015-05-15 13:14:24 -07003562 for (auto& iter : fields) {
Donald Curtis0e07f922015-05-15 09:21:23 -07003563 if (iter.find(kSdpDelimiterEqual) == std::string::npos) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003564 // Only fmtps with equals are currently supported. Other fmtp types
3565 // should be ignored. Unknown fmtps do not constitute an error.
3566 continue;
3567 }
Donald Curtis0e07f922015-05-15 09:21:23 -07003568
3569 std::string name;
3570 std::string value;
3571 if (!ParseFmtpParam(rtc::string_trim(iter), &name, &value, error)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003572 return false;
3573 }
3574 codec_params[name] = value;
3575 }
3576
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003577 if (media_type == cricket::MEDIA_TYPE_AUDIO) {
3578 UpdateCodec<AudioContentDescription, cricket::AudioCodec>(
pkasting@chromium.orgd3245462015-02-23 21:28:22 +00003579 media_desc, payload_type, codec_params);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003580 } else if (media_type == cricket::MEDIA_TYPE_VIDEO) {
3581 UpdateCodec<VideoContentDescription, cricket::VideoCodec>(
pkasting@chromium.orgd3245462015-02-23 21:28:22 +00003582 media_desc, payload_type, codec_params);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003583 }
3584 return true;
3585}
3586
Yves Gerey665174f2018-06-19 15:03:05 +02003587bool ParseRtcpFbAttribute(const std::string& line,
Patrik Höglundb6b29e02018-06-21 16:58:01 +02003588 const cricket::MediaType media_type,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003589 MediaContentDescription* media_desc,
3590 SdpParseError* error) {
3591 if (media_type != cricket::MEDIA_TYPE_AUDIO &&
3592 media_type != cricket::MEDIA_TYPE_VIDEO) {
3593 return true;
3594 }
3595 std::vector<std::string> rtcp_fb_fields;
Jonas Olssonec9e4922018-09-05 09:53:49 +02003596 rtc::split(line.c_str(), kSdpDelimiterSpaceChar, &rtcp_fb_fields);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003597 if (rtcp_fb_fields.size() < 2) {
3598 return ParseFailedGetValue(line, kAttributeRtcpFb, error);
3599 }
3600 std::string payload_type_string;
3601 if (!GetValue(rtcp_fb_fields[0], kAttributeRtcpFb, &payload_type_string,
3602 error)) {
3603 return false;
3604 }
wu@webrtc.org5e760e72014-04-02 23:19:09 +00003605 int payload_type = kWildcardPayloadType;
3606 if (payload_type_string != "*") {
pkasting@chromium.orge9facf82015-02-17 20:36:28 +00003607 if (!GetPayloadTypeFromString(line, payload_type_string, &payload_type,
3608 error)) {
wu@webrtc.org5e760e72014-04-02 23:19:09 +00003609 return false;
3610 }
3611 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003612 std::string id = rtcp_fb_fields[1];
3613 std::string param = "";
3614 for (std::vector<std::string>::iterator iter = rtcp_fb_fields.begin() + 2;
3615 iter != rtcp_fb_fields.end(); ++iter) {
3616 param.append(*iter);
3617 }
3618 const cricket::FeedbackParam feedback_param(id, param);
3619
3620 if (media_type == cricket::MEDIA_TYPE_AUDIO) {
pkasting@chromium.orgd3245462015-02-23 21:28:22 +00003621 UpdateCodec<AudioContentDescription, cricket::AudioCodec>(
3622 media_desc, payload_type, feedback_param);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003623 } else if (media_type == cricket::MEDIA_TYPE_VIDEO) {
pkasting@chromium.orgd3245462015-02-23 21:28:22 +00003624 UpdateCodec<VideoContentDescription, cricket::VideoCodec>(
3625 media_desc, payload_type, feedback_param);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003626 }
3627 return true;
3628}
3629
3630} // namespace webrtc