blob: 80ac8d58d64cbd19e53c4f9fcc5b60a0a3e78179 [file] [log] [blame]
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001/*
kjellanderb24317b2016-02-10 07:54:43 -08002 * Copyright 2011 The WebRTC project authors. All Rights Reserved.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003 *
kjellanderb24317b2016-02-10 07:54:43 -08004 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00009 */
10
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#include "pc/webrtcsdp.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000012
kjellandera96e2d72016-02-04 23:52:28 -080013#include <ctype.h>
henrike@webrtc.org28e20752013-07-10 00:45:36 +000014#include <limits.h>
15#include <stdio.h>
kwibergd1fe2812016-04-27 06:47:29 -070016
henrike@webrtc.org28e20752013-07-10 00:45:36 +000017#include <algorithm>
Steve Anton36b29d12017-10-30 09:57:42 -070018#include <map>
kwibergd1fe2812016-04-27 06:47:29 -070019#include <memory>
Steve Anton36b29d12017-10-30 09:57:42 -070020#include <set>
henrike@webrtc.org28e20752013-07-10 00:45:36 +000021#include <string>
deadbeef67cf2c12016-04-13 10:07:16 -070022#include <unordered_map>
henrike@webrtc.org28e20752013-07-10 00:45:36 +000023#include <vector>
24
Patrik Höglunde2d6a062017-10-05 14:53:33 +020025#include "api/candidate.h"
Patrik Höglund7aee3d52017-11-15 13:15:17 +010026#include "api/cryptoparams.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020027#include "api/jsepicecandidate.h"
28#include "api/jsepsessiondescription.h"
isheriff6f8d6862016-05-26 11:24:55 -070029// for RtpExtension
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020030#include "api/rtpparameters.h"
31#include "media/base/codec.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020032#include "media/base/mediaconstants.h"
33#include "media/base/rtputils.h"
34#include "media/sctp/sctptransportinternal.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020035#include "p2p/base/p2pconstants.h"
36#include "p2p/base/port.h"
37#include "pc/mediasession.h"
38#include "rtc_base/arraysize.h"
39#include "rtc_base/checks.h"
40#include "rtc_base/logging.h"
41#include "rtc_base/messagedigest.h"
42#include "rtc_base/stringutils.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000043
44using cricket::AudioContentDescription;
45using cricket::Candidate;
46using cricket::Candidates;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000047using cricket::ContentInfo;
48using cricket::CryptoParams;
49using cricket::DataContentDescription;
50using cricket::ICE_CANDIDATE_COMPONENT_RTP;
51using cricket::ICE_CANDIDATE_COMPONENT_RTCP;
52using cricket::kCodecParamMaxBitrate;
53using cricket::kCodecParamMaxPTime;
54using cricket::kCodecParamMaxQuantization;
55using cricket::kCodecParamMinBitrate;
56using cricket::kCodecParamMinPTime;
57using cricket::kCodecParamPTime;
58using cricket::kCodecParamSPropStereo;
buildbot@webrtc.orged97bb02014-05-07 11:15:20 +000059using cricket::kCodecParamStartBitrate;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000060using cricket::kCodecParamStereo;
61using cricket::kCodecParamUseInbandFec;
Minyue Li7100dcd2015-03-27 05:05:59 +010062using cricket::kCodecParamUseDtx;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000063using cricket::kCodecParamSctpProtocol;
64using cricket::kCodecParamSctpStreams;
henrike@webrtc.org1e09a712013-07-26 19:17:59 +000065using cricket::kCodecParamMaxAverageBitrate;
buildbot@webrtc.org5d639b32014-09-10 07:57:12 +000066using cricket::kCodecParamMaxPlaybackRate;
stefan@webrtc.org85d27942014-06-09 12:51:39 +000067using cricket::kCodecParamAssociatedPayloadType;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000068using cricket::MediaContentDescription;
69using cricket::MediaType;
isheriff6f8d6862016-05-26 11:24:55 -070070using cricket::RtpHeaderExtensions;
Steve Anton5adfafd2017-12-20 16:34:00 -080071using cricket::MediaProtocolType;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000072using cricket::SsrcGroup;
73using cricket::StreamParams;
74using cricket::StreamParamsVec;
75using cricket::TransportDescription;
76using cricket::TransportInfo;
77using cricket::VideoContentDescription;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000078using rtc::SocketAddress;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000079
henrike@webrtc.org28e20752013-07-10 00:45:36 +000080namespace cricket {
81class SessionDescription;
82}
83
deadbeef90f1e1e2017-02-10 12:35:05 -080084// TODO(deadbeef): Switch to using anonymous namespace rather than declaring
85// everything "static".
henrike@webrtc.org28e20752013-07-10 00:45:36 +000086namespace webrtc {
87
88// Line type
89// RFC 4566
90// An SDP session description consists of a number of lines of text of
91// the form:
92// <type>=<value>
93// where <type> MUST be exactly one case-significant character.
deadbeef9d3584c2016-02-16 17:54:10 -080094static const int kLinePrefixLength = 2; // Length of <type>=
henrike@webrtc.org28e20752013-07-10 00:45:36 +000095static const char kLineTypeVersion = 'v';
96static const char kLineTypeOrigin = 'o';
97static const char kLineTypeSessionName = 's';
98static const char kLineTypeSessionInfo = 'i';
99static const char kLineTypeSessionUri = 'u';
100static const char kLineTypeSessionEmail = 'e';
101static const char kLineTypeSessionPhone = 'p';
102static const char kLineTypeSessionBandwidth = 'b';
103static const char kLineTypeTiming = 't';
104static const char kLineTypeRepeatTimes = 'r';
105static const char kLineTypeTimeZone = 'z';
106static const char kLineTypeEncryptionKey = 'k';
107static const char kLineTypeMedia = 'm';
108static const char kLineTypeConnection = 'c';
109static const char kLineTypeAttributes = 'a';
110
111// Attributes
112static const char kAttributeGroup[] = "group";
113static const char kAttributeMid[] = "mid";
deadbeef9d3584c2016-02-16 17:54:10 -0800114static const char kAttributeMsid[] = "msid";
deadbeef25ed4352016-12-12 18:37:36 -0800115static const char kAttributeBundleOnly[] = "bundle-only";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000116static const char kAttributeRtcpMux[] = "rtcp-mux";
deadbeef13871492015-12-09 12:37:51 -0800117static const char kAttributeRtcpReducedSize[] = "rtcp-rsize";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000118static const char kAttributeSsrc[] = "ssrc";
119static const char kSsrcAttributeCname[] = "cname";
120static const char kAttributeExtmap[] = "extmap";
121// draft-alvestrand-mmusic-msid-01
122// a=msid-semantic: WMS
123static const char kAttributeMsidSemantics[] = "msid-semantic";
124static const char kMediaStreamSemantic[] = "WMS";
125static const char kSsrcAttributeMsid[] = "msid";
126static const char kDefaultMsid[] = "default";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000127static const char kSsrcAttributeMslabel[] = "mslabel";
128static const char kSSrcAttributeLabel[] = "label";
129static const char kAttributeSsrcGroup[] = "ssrc-group";
130static const char kAttributeCrypto[] = "crypto";
131static const char kAttributeCandidate[] = "candidate";
132static const char kAttributeCandidateTyp[] = "typ";
133static const char kAttributeCandidateRaddr[] = "raddr";
134static const char kAttributeCandidateRport[] = "rport";
honghaiza54a0802015-12-16 18:37:23 -0800135static const char kAttributeCandidateUfrag[] = "ufrag";
136static const char kAttributeCandidatePwd[] = "pwd";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000137static const char kAttributeCandidateGeneration[] = "generation";
honghaiza0c44ea2016-03-23 16:07:48 -0700138static const char kAttributeCandidateNetworkId[] = "network-id";
honghaize1a0c942016-02-16 14:54:56 -0800139static const char kAttributeCandidateNetworkCost[] = "network-cost";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000140static const char kAttributeFingerprint[] = "fingerprint";
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +0000141static const char kAttributeSetup[] = "setup";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000142static const char kAttributeFmtp[] = "fmtp";
143static const char kAttributeRtpmap[] = "rtpmap";
wu@webrtc.org78187522013-10-07 23:32:02 +0000144static const char kAttributeSctpmap[] = "sctpmap";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000145static const char kAttributeRtcp[] = "rtcp";
146static const char kAttributeIceUfrag[] = "ice-ufrag";
147static const char kAttributeIcePwd[] = "ice-pwd";
148static const char kAttributeIceLite[] = "ice-lite";
149static const char kAttributeIceOption[] = "ice-options";
150static const char kAttributeSendOnly[] = "sendonly";
151static const char kAttributeRecvOnly[] = "recvonly";
152static const char kAttributeRtcpFb[] = "rtcp-fb";
153static const char kAttributeSendRecv[] = "sendrecv";
154static const char kAttributeInactive[] = "inactive";
jiayl@webrtc.orgddb85ab2014-09-05 16:31:56 +0000155// draft-ietf-mmusic-sctp-sdp-07
156// a=sctp-port
157static const char kAttributeSctpPort[] = "sctp-port";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000158
159// Experimental flags
160static const char kAttributeXGoogleFlag[] = "x-google-flag";
161static const char kValueConference[] = "conference";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000162
163// Candidate
164static const char kCandidateHost[] = "host";
165static const char kCandidateSrflx[] = "srflx";
Honghai Zhang7fb69db2016-03-14 11:59:18 -0700166static const char kCandidatePrflx[] = "prflx";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000167static const char kCandidateRelay[] = "relay";
mallinath@webrtc.org2d60c5e2014-08-08 22:29:20 +0000168static const char kTcpCandidateType[] = "tcptype";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000169
170static const char kSdpDelimiterEqual = '=';
171static const char kSdpDelimiterSpace = ' ';
172static const char kSdpDelimiterColon = ':';
173static const char kSdpDelimiterSemicolon = ';';
174static const char kSdpDelimiterSlash = '/';
175static const char kNewLine = '\n';
176static const char kReturn = '\r';
177static const char kLineBreak[] = "\r\n";
178
Steve Anton36b29d12017-10-30 09:57:42 -0700179// TODO(deadbeef): Generate the Session and Time description
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000180// instead of hardcoding.
181static const char kSessionVersion[] = "v=0";
182// RFC 4566
183static const char kSessionOriginUsername[] = "-";
184static const char kSessionOriginSessionId[] = "0";
185static const char kSessionOriginSessionVersion[] = "0";
186static const char kSessionOriginNettype[] = "IN";
187static const char kSessionOriginAddrtype[] = "IP4";
188static const char kSessionOriginAddress[] = "127.0.0.1";
189static const char kSessionName[] = "s=-";
190static const char kTimeDescription[] = "t=0 0";
191static const char kAttrGroup[] = "a=group:BUNDLE";
192static const char kConnectionNettype[] = "IN";
pthatcher@webrtc.orgc9d6d142014-10-23 23:37:22 +0000193static const char kConnectionIpv4Addrtype[] = "IP4";
194static const char kConnectionIpv6Addrtype[] = "IP6";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000195static const char kMediaTypeVideo[] = "video";
196static const char kMediaTypeAudio[] = "audio";
197static const char kMediaTypeData[] = "application";
198static const char kMediaPortRejected[] = "0";
pthatcher@webrtc.orgc9d6d142014-10-23 23:37:22 +0000199// draft-ietf-mmusic-trickle-ice-01
200// When no candidates have been gathered, set the connection
201// address to IP6 ::.
perkj@webrtc.orgd105cc82014-11-07 11:22:06 +0000202// TODO(perkj): FF can not parse IP6 ::. See http://crbug/430333
203// Use IPV4 per default.
204static const char kDummyAddress[] = "0.0.0.0";
pthatcher@webrtc.orgc9d6d142014-10-23 23:37:22 +0000205static const char kDummyPort[] = "9";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000206// RFC 3556
207static const char kApplicationSpecificMaximum[] = "AS";
208
wu@webrtc.org78187522013-10-07 23:32:02 +0000209static const char kDefaultSctpmapProtocol[] = "webrtc-datachannel";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000210
pkasting@chromium.orgd3245462015-02-23 21:28:22 +0000211// RTP payload type is in the 0-127 range. Use -1 to indicate "all" payload
212// types.
213const int kWildcardPayloadType = -1;
214
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000215struct SsrcInfo {
Peter Boström0c4e06b2015-10-07 12:23:21 +0200216 uint32_t ssrc_id;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000217 std::string cname;
deadbeef9d3584c2016-02-16 17:54:10 -0800218 std::string stream_id;
219 std::string track_id;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000220
221 // For backward compatibility.
222 // TODO(ronghuawu): Remove below 2 fields once all the clients support msid.
223 std::string label;
224 std::string mslabel;
225};
226typedef std::vector<SsrcInfo> SsrcInfoVec;
227typedef std::vector<SsrcGroup> SsrcGroupVec;
228
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000229template <class T>
230static void AddFmtpLine(const T& codec, std::string* message);
231static void BuildMediaDescription(const ContentInfo* content_info,
232 const TransportInfo* transport_info,
233 const MediaType media_type,
wu@webrtc.org4c3e9912014-07-16 21:03:13 +0000234 const std::vector<Candidate>& candidates,
Steve Antone831b8c2018-02-01 12:22:16 -0800235 int msid_signaling,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000236 std::string* message);
zstein4b2e0822017-02-17 19:48:38 -0800237static void BuildSctpContentAttributes(std::string* message,
238 int sctp_port,
239 bool use_sctpmap);
deadbeef9d3584c2016-02-16 17:54:10 -0800240static void BuildRtpContentAttributes(const MediaContentDescription* media_desc,
241 const MediaType media_type,
Steve Antone831b8c2018-02-01 12:22:16 -0800242 int msid_signaling,
deadbeef9d3584c2016-02-16 17:54:10 -0800243 std::string* message);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000244static void BuildRtpMap(const MediaContentDescription* media_desc,
245 const MediaType media_type,
246 std::string* message);
247static void BuildCandidate(const std::vector<Candidate>& candidates,
honghaiza54a0802015-12-16 18:37:23 -0800248 bool include_ufrag,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000249 std::string* message);
250static void BuildIceOptions(const std::vector<std::string>& transport_options,
251 std::string* message);
pthatcher@webrtc.org3341b402015-02-13 21:14:22 +0000252static bool IsRtp(const std::string& protocol);
253static bool IsDtlsSctp(const std::string& protocol);
zhihuang38989e52017-03-21 11:04:53 -0700254static bool ParseSessionDescription(const std::string& message,
255 size_t* pos,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000256 std::string* session_id,
257 std::string* session_version,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000258 TransportDescription* session_td,
259 RtpHeaderExtensions* session_extmaps,
zhihuang38989e52017-03-21 11:04:53 -0700260 rtc::SocketAddress* connection_addr,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000261 cricket::SessionDescription* desc,
262 SdpParseError* error);
263static bool ParseGroupAttribute(const std::string& line,
264 cricket::SessionDescription* desc,
265 SdpParseError* error);
266static bool ParseMediaDescription(
267 const std::string& message,
268 const TransportDescription& session_td,
269 const RtpHeaderExtensions& session_extmaps,
zhihuang38989e52017-03-21 11:04:53 -0700270 size_t* pos,
271 const rtc::SocketAddress& session_connection_addr,
272 cricket::SessionDescription* desc,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000273 std::vector<JsepIceCandidate*>* candidates,
274 SdpParseError* error);
275static bool ParseContent(const std::string& message,
276 const MediaType media_type,
277 int mline_index,
278 const std::string& protocol,
deadbeef67cf2c12016-04-13 10:07:16 -0700279 const std::vector<int>& payload_types,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000280 size_t* pos,
281 std::string* content_name,
deadbeef25ed4352016-12-12 18:37:36 -0800282 bool* bundle_only,
Steve Antone831b8c2018-02-01 12:22:16 -0800283 int* msid_signaling,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000284 MediaContentDescription* media_desc,
285 TransportDescription* transport,
286 std::vector<JsepIceCandidate*>* candidates,
287 SdpParseError* error);
288static bool ParseSsrcAttribute(const std::string& line,
289 SsrcInfoVec* ssrc_infos,
Steve Antone831b8c2018-02-01 12:22:16 -0800290 int* msid_signaling,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000291 SdpParseError* error);
292static bool ParseSsrcGroupAttribute(const std::string& line,
293 SsrcGroupVec* ssrc_groups,
294 SdpParseError* error);
295static bool ParseCryptoAttribute(const std::string& line,
296 MediaContentDescription* media_desc,
297 SdpParseError* error);
298static bool ParseRtpmapAttribute(const std::string& line,
299 const MediaType media_type,
deadbeef67cf2c12016-04-13 10:07:16 -0700300 const std::vector<int>& payload_types,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000301 MediaContentDescription* media_desc,
302 SdpParseError* error);
303static bool ParseFmtpAttributes(const std::string& line,
304 const MediaType media_type,
305 MediaContentDescription* media_desc,
306 SdpParseError* error);
307static bool ParseFmtpParam(const std::string& line, std::string* parameter,
308 std::string* value, SdpParseError* error);
309static bool ParseCandidate(const std::string& message, Candidate* candidate,
310 SdpParseError* error, bool is_raw);
311static bool ParseRtcpFbAttribute(const std::string& line,
312 const MediaType media_type,
313 MediaContentDescription* media_desc,
314 SdpParseError* error);
315static bool ParseIceOptions(const std::string& line,
316 std::vector<std::string>* transport_options,
317 SdpParseError* error);
318static bool ParseExtmap(const std::string& line,
isheriff6f8d6862016-05-26 11:24:55 -0700319 RtpExtension* extmap,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000320 SdpParseError* error);
321static bool ParseFingerprintAttribute(const std::string& line,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000322 rtc::SSLFingerprint** fingerprint,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000323 SdpParseError* error);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +0000324static bool ParseDtlsSetup(const std::string& line,
325 cricket::ConnectionRole* role,
326 SdpParseError* error);
deadbeef9d3584c2016-02-16 17:54:10 -0800327static bool ParseMsidAttribute(const std::string& line,
Tomas Gunnarsson191bf5c2018-03-30 10:44:43 +0000328 std::string* stream_id,
deadbeef9d3584c2016-02-16 17:54:10 -0800329 std::string* track_id,
330 SdpParseError* error);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000331
332// Helper functions
333
334// Below ParseFailed*** functions output the line that caused the parsing
335// failure and the detailed reason (|description|) of the failure to |error|.
336// The functions always return false so that they can be used directly in the
337// following way when error happens:
338// "return ParseFailed***(...);"
339
340// The line starting at |line_start| of |message| is the failing line.
341// The reason for the failure should be provided in the |description|.
342// An example of a description could be "unknown character".
343static bool ParseFailed(const std::string& message,
344 size_t line_start,
345 const std::string& description,
346 SdpParseError* error) {
347 // Get the first line of |message| from |line_start|.
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +0000348 std::string first_line;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000349 size_t line_end = message.find(kNewLine, line_start);
350 if (line_end != std::string::npos) {
351 if (line_end > 0 && (message.at(line_end - 1) == kReturn)) {
352 --line_end;
353 }
354 first_line = message.substr(line_start, (line_end - line_start));
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +0000355 } else {
356 first_line = message.substr(line_start);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000357 }
358
359 if (error) {
360 error->line = first_line;
361 error->description = description;
362 }
Mirko Bonadei675513b2017-11-09 11:09:25 +0100363 RTC_LOG(LS_ERROR) << "Failed to parse: \"" << first_line
364 << "\". Reason: " << description;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000365 return false;
366}
367
368// |line| is the failing line. The reason for the failure should be
369// provided in the |description|.
370static bool ParseFailed(const std::string& line,
371 const std::string& description,
372 SdpParseError* error) {
373 return ParseFailed(line, 0, description, error);
374}
375
376// Parses failure where the failing SDP line isn't know or there are multiple
377// failing lines.
378static bool ParseFailed(const std::string& description,
379 SdpParseError* error) {
380 return ParseFailed("", description, error);
381}
382
383// |line| is the failing line. The failure is due to the fact that |line|
384// doesn't have |expected_fields| fields.
385static bool ParseFailedExpectFieldNum(const std::string& line,
386 int expected_fields,
387 SdpParseError* error) {
388 std::ostringstream description;
389 description << "Expects " << expected_fields << " fields.";
390 return ParseFailed(line, description.str(), error);
391}
392
393// |line| is the failing line. The failure is due to the fact that |line| has
394// less than |expected_min_fields| fields.
395static bool ParseFailedExpectMinFieldNum(const std::string& line,
396 int expected_min_fields,
397 SdpParseError* error) {
398 std::ostringstream description;
399 description << "Expects at least " << expected_min_fields << " fields.";
400 return ParseFailed(line, description.str(), error);
401}
402
403// |line| is the failing line. The failure is due to the fact that it failed to
404// get the value of |attribute|.
405static bool ParseFailedGetValue(const std::string& line,
406 const std::string& attribute,
407 SdpParseError* error) {
408 std::ostringstream description;
409 description << "Failed to get the value of attribute: " << attribute;
410 return ParseFailed(line, description.str(), error);
411}
412
413// The line starting at |line_start| of |message| is the failing line. The
414// failure is due to the line type (e.g. the "m" part of the "m-line")
415// not matching what is expected. The expected line type should be
416// provided as |line_type|.
417static bool ParseFailedExpectLine(const std::string& message,
418 size_t line_start,
419 const char line_type,
420 const std::string& line_value,
421 SdpParseError* error) {
422 std::ostringstream description;
423 description << "Expect line: " << line_type << "=" << line_value;
424 return ParseFailed(message, line_start, description.str(), error);
425}
426
427static bool AddLine(const std::string& line, std::string* message) {
428 if (!message)
429 return false;
430
431 message->append(line);
432 message->append(kLineBreak);
433 return true;
434}
435
436static bool GetLine(const std::string& message,
437 size_t* pos,
438 std::string* line) {
439 size_t line_begin = *pos;
440 size_t line_end = message.find(kNewLine, line_begin);
441 if (line_end == std::string::npos) {
442 return false;
443 }
444 // Update the new start position
445 *pos = line_end + 1;
446 if (line_end > 0 && (message.at(line_end - 1) == kReturn)) {
447 --line_end;
448 }
449 *line = message.substr(line_begin, (line_end - line_begin));
450 const char* cline = line->c_str();
451 // RFC 4566
452 // An SDP session description consists of a number of lines of text of
453 // the form:
454 // <type>=<value>
455 // where <type> MUST be exactly one case-significant character and
456 // <value> is structured text whose format depends on <type>.
457 // Whitespace MUST NOT be used on either side of the "=" sign.
decurtis@webrtc.org8af11042015-01-07 19:15:51 +0000458 if (line->length() < 3 ||
459 !islower(cline[0]) ||
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000460 cline[1] != kSdpDelimiterEqual ||
461 cline[2] == kSdpDelimiterSpace) {
462 *pos = line_begin;
463 return false;
464 }
465 return true;
466}
467
468// Init |os| to "|type|=|value|".
469static void InitLine(const char type,
470 const std::string& value,
471 std::ostringstream* os) {
472 os->str("");
473 *os << type << kSdpDelimiterEqual << value;
474}
475
476// Init |os| to "a=|attribute|".
477static void InitAttrLine(const std::string& attribute, std::ostringstream* os) {
478 InitLine(kLineTypeAttributes, attribute, os);
479}
480
481// Writes a SDP attribute line based on |attribute| and |value| to |message|.
482static void AddAttributeLine(const std::string& attribute, int value,
483 std::string* message) {
484 std::ostringstream os;
485 InitAttrLine(attribute, &os);
486 os << kSdpDelimiterColon << value;
487 AddLine(os.str(), message);
488}
489
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000490static bool IsLineType(const std::string& message,
491 const char type,
492 size_t line_start) {
493 if (message.size() < line_start + kLinePrefixLength) {
494 return false;
495 }
496 const char* cmessage = message.c_str();
497 return (cmessage[line_start] == type &&
498 cmessage[line_start + 1] == kSdpDelimiterEqual);
499}
500
501static bool IsLineType(const std::string& line,
502 const char type) {
503 return IsLineType(line, type, 0);
504}
505
506static bool GetLineWithType(const std::string& message, size_t* pos,
507 std::string* line, const char type) {
508 if (!IsLineType(message, type, *pos)) {
509 return false;
510 }
511
512 if (!GetLine(message, pos, line))
513 return false;
514
515 return true;
516}
517
518static bool HasAttribute(const std::string& line,
519 const std::string& attribute) {
520 return (line.compare(kLinePrefixLength, attribute.size(), attribute) == 0);
521}
522
Peter Boström0c4e06b2015-10-07 12:23:21 +0200523static bool AddSsrcLine(uint32_t ssrc_id,
524 const std::string& attribute,
525 const std::string& value,
526 std::string* message) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000527 // RFC 5576
528 // a=ssrc:<ssrc-id> <attribute>:<value>
529 std::ostringstream os;
530 InitAttrLine(kAttributeSsrc, &os);
531 os << kSdpDelimiterColon << ssrc_id << kSdpDelimiterSpace
532 << attribute << kSdpDelimiterColon << value;
533 return AddLine(os.str(), message);
534}
535
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000536// Get value only from <attribute>:<value>.
537static bool GetValue(const std::string& message, const std::string& attribute,
538 std::string* value, SdpParseError* error) {
539 std::string leftpart;
Donald Curtis0e07f922015-05-15 09:21:23 -0700540 if (!rtc::tokenize_first(message, kSdpDelimiterColon, &leftpart, value)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000541 return ParseFailedGetValue(message, attribute, error);
542 }
543 // The left part should end with the expected attribute.
544 if (leftpart.length() < attribute.length() ||
545 leftpart.compare(leftpart.length() - attribute.length(),
546 attribute.length(), attribute) != 0) {
547 return ParseFailedGetValue(message, attribute, error);
548 }
549 return true;
550}
551
552static bool CaseInsensitiveFind(std::string str1, std::string str2) {
553 std::transform(str1.begin(), str1.end(), str1.begin(),
554 ::tolower);
555 std::transform(str2.begin(), str2.end(), str2.begin(),
556 ::tolower);
557 return str1.find(str2) != std::string::npos;
558}
559
wu@webrtc.org5e760e72014-04-02 23:19:09 +0000560template <class T>
561static bool GetValueFromString(const std::string& line,
562 const std::string& s,
563 T* t,
564 SdpParseError* error) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000565 if (!rtc::FromString(s, t)) {
wu@webrtc.org5e760e72014-04-02 23:19:09 +0000566 std::ostringstream description;
567 description << "Invalid value: " << s << ".";
568 return ParseFailed(line, description.str(), error);
569 }
570 return true;
571}
572
pkasting@chromium.orge9facf82015-02-17 20:36:28 +0000573static bool GetPayloadTypeFromString(const std::string& line,
574 const std::string& s,
575 int* payload_type,
576 SdpParseError* error) {
577 return GetValueFromString(line, s, payload_type, error) &&
578 cricket::IsValidRtpPayloadType(*payload_type);
579}
580
Tomas Gunnarsson191bf5c2018-03-30 10:44:43 +0000581// |msid_stream_id| and |msid_track_id| represent the stream/track ID from the
Taylor Brandstetter5de6b752016-03-09 17:02:30 -0800582// "a=msid" attribute, if it exists. They are empty if the attribute does not
583// exist.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000584void CreateTracksFromSsrcInfos(const SsrcInfoVec& ssrc_infos,
Tomas Gunnarsson191bf5c2018-03-30 10:44:43 +0000585 const std::string& msid_stream_id,
Taylor Brandstetter5de6b752016-03-09 17:02:30 -0800586 const std::string& msid_track_id,
Tomas Gunnarsson191bf5c2018-03-30 10:44:43 +0000587 StreamParamsVec* tracks) {
nisseede5da42017-01-12 05:15:36 -0800588 RTC_DCHECK(tracks != NULL);
Tomas Gunnarsson191bf5c2018-03-30 10:44:43 +0000589 RTC_DCHECK(msid_stream_id.empty() == msid_track_id.empty());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000590 for (SsrcInfoVec::const_iterator ssrc_info = ssrc_infos.begin();
591 ssrc_info != ssrc_infos.end(); ++ssrc_info) {
592 if (ssrc_info->cname.empty()) {
593 continue;
594 }
595
Tomas Gunnarsson191bf5c2018-03-30 10:44:43 +0000596 std::string stream_id;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000597 std::string track_id;
Tomas Gunnarsson191bf5c2018-03-30 10:44:43 +0000598 if (ssrc_info->stream_id.empty() && !ssrc_info->mslabel.empty()) {
599 // If there's no msid and there's mslabel, we consider this is a sdp from
600 // a older version of client that doesn't support msid.
Seth Hampsonf351c342018-03-29 15:45:58 -0700601 // In that case, we use the mslabel and label to construct the track.
Tomas Gunnarsson191bf5c2018-03-30 10:44:43 +0000602 stream_id = ssrc_info->mslabel;
Seth Hampsonf351c342018-03-29 15:45:58 -0700603 track_id = ssrc_info->label;
Tomas Gunnarsson191bf5c2018-03-30 10:44:43 +0000604 } else if (ssrc_info->stream_id.empty() && !msid_stream_id.empty()) {
605 // If there's no msid in the SSRC attributes, but there's a global one
606 // (from a=msid), use that. This is the case with Unified Plan SDP.
607 stream_id = msid_stream_id;
608 track_id = msid_track_id;
Seth Hampsonf351c342018-03-29 15:45:58 -0700609 } else {
Tomas Gunnarsson191bf5c2018-03-30 10:44:43 +0000610 stream_id = ssrc_info->stream_id;
611 track_id = ssrc_info->track_id;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000612 }
Tomas Gunnarsson191bf5c2018-03-30 10:44:43 +0000613 // If a stream/track ID wasn't populated from the SSRC attributes OR the
Taylor Brandstetter5de6b752016-03-09 17:02:30 -0800614 // msid attribute, use default/random values.
Tomas Gunnarsson191bf5c2018-03-30 10:44:43 +0000615 if (stream_id.empty()) {
616 stream_id = kDefaultMsid;
617 }
Taylor Brandstetter5de6b752016-03-09 17:02:30 -0800618 if (track_id.empty()) {
619 // TODO(ronghuawu): What should we do if the track id doesn't appear?
620 // Create random string (which will be used as track label later)?
621 track_id = rtc::CreateRandomString(8);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000622 }
623
624 StreamParamsVec::iterator track = tracks->begin();
625 for (; track != tracks->end(); ++track) {
626 if (track->id == track_id) {
627 break;
628 }
629 }
630 if (track == tracks->end()) {
631 // If we don't find an existing track, create a new one.
632 tracks->push_back(StreamParams());
633 track = tracks->end() - 1;
634 }
635 track->add_ssrc(ssrc_info->ssrc_id);
636 track->cname = ssrc_info->cname;
Tomas Gunnarsson191bf5c2018-03-30 10:44:43 +0000637 track->set_stream_ids({stream_id});
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000638 track->id = track_id;
639 }
640}
641
Seth Hampson845e8782018-03-02 11:34:10 -0800642void GetMediaStreamIds(const ContentInfo* content,
643 std::set<std::string>* labels) {
Steve Antonb1c1de12017-12-21 15:14:30 -0800644 for (const StreamParams& stream_params :
645 content->media_description()->streams()) {
Seth Hampson845e8782018-03-02 11:34:10 -0800646 for (const std::string& stream_id : stream_params.stream_ids()) {
647 labels->insert(stream_id);
Steve Anton5a26a3a2018-02-28 11:38:47 -0800648 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000649 }
650}
651
652// RFC 5245
653// It is RECOMMENDED that default candidates be chosen based on the
654// likelihood of those candidates to work with the peer that is being
655// contacted. It is RECOMMENDED that relayed > reflexive > host.
656static const int kPreferenceUnknown = 0;
657static const int kPreferenceHost = 1;
658static const int kPreferenceReflexive = 2;
659static const int kPreferenceRelayed = 3;
660
661static int GetCandidatePreferenceFromType(const std::string& type) {
662 int preference = kPreferenceUnknown;
663 if (type == cricket::LOCAL_PORT_TYPE) {
664 preference = kPreferenceHost;
665 } else if (type == cricket::STUN_PORT_TYPE) {
666 preference = kPreferenceReflexive;
667 } else if (type == cricket::RELAY_PORT_TYPE) {
668 preference = kPreferenceRelayed;
669 } else {
nissec80e7412017-01-11 05:56:46 -0800670 RTC_NOTREACHED();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000671 }
672 return preference;
673}
674
guoweis@webrtc.org57ac2c82015-02-06 00:45:13 +0000675// Get ip and port of the default destination from the |candidates| with the
676// given value of |component_id|. The default candidate should be the one most
677// likely to work, typically IPv4 relay.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000678// RFC 5245
679// The value of |component_id| currently supported are 1 (RTP) and 2 (RTCP).
Steve Anton36b29d12017-10-30 09:57:42 -0700680// TODO(deadbeef): Decide the default destination in webrtcsession and
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000681// pass it down via SessionDescription.
pthatcher@webrtc.orgc9d6d142014-10-23 23:37:22 +0000682static void GetDefaultDestination(
683 const std::vector<Candidate>& candidates,
684 int component_id, std::string* port,
685 std::string* ip, std::string* addr_type) {
perkj@webrtc.orgd105cc82014-11-07 11:22:06 +0000686 *addr_type = kConnectionIpv4Addrtype;
pthatcher@webrtc.orgc9d6d142014-10-23 23:37:22 +0000687 *port = kDummyPort;
688 *ip = kDummyAddress;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000689 int current_preference = kPreferenceUnknown;
guoweis@webrtc.org57ac2c82015-02-06 00:45:13 +0000690 int current_family = AF_UNSPEC;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000691 for (std::vector<Candidate>::const_iterator it = candidates.begin();
692 it != candidates.end(); ++it) {
693 if (it->component() != component_id) {
694 continue;
695 }
guoweis@webrtc.org57ac2c82015-02-06 00:45:13 +0000696 // Default destination should be UDP only.
697 if (it->protocol() != cricket::UDP_PROTOCOL_NAME) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000698 continue;
699 }
guoweis@webrtc.org57ac2c82015-02-06 00:45:13 +0000700 const int preference = GetCandidatePreferenceFromType(it->type());
701 const int family = it->address().ipaddr().family();
702 // See if this candidate is more preferable then the current one if it's the
703 // same family. Or if the current family is IPv4 already so we could safely
704 // ignore all IPv6 ones. WebRTC bug 4269.
705 // http://code.google.com/p/webrtc/issues/detail?id=4269
706 if ((preference <= current_preference && current_family == family) ||
707 (current_family == AF_INET && family == AF_INET6)) {
708 continue;
709 }
pthatcher@webrtc.orgc9d6d142014-10-23 23:37:22 +0000710 if (family == AF_INET) {
711 addr_type->assign(kConnectionIpv4Addrtype);
712 } else if (family == AF_INET6) {
713 addr_type->assign(kConnectionIpv6Addrtype);
714 }
guoweis@webrtc.org57ac2c82015-02-06 00:45:13 +0000715 current_preference = preference;
716 current_family = family;
717 *port = it->address().PortAsString();
718 *ip = it->address().ipaddr().ToString();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000719 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000720}
721
wu@webrtc.org4c3e9912014-07-16 21:03:13 +0000722// Gets "a=rtcp" line if found default RTCP candidate from |candidates|.
723static std::string GetRtcpLine(const std::vector<Candidate>& candidates) {
pthatcher@webrtc.orgc9d6d142014-10-23 23:37:22 +0000724 std::string rtcp_line, rtcp_port, rtcp_ip, addr_type;
725 GetDefaultDestination(candidates, ICE_CANDIDATE_COMPONENT_RTCP,
726 &rtcp_port, &rtcp_ip, &addr_type);
727 // Found default RTCP candidate.
728 // RFC 5245
729 // If the agent is utilizing RTCP, it MUST encode the RTCP candidate
730 // using the a=rtcp attribute as defined in RFC 3605.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000731
pthatcher@webrtc.orgc9d6d142014-10-23 23:37:22 +0000732 // RFC 3605
733 // rtcp-attribute = "a=rtcp:" port [nettype space addrtype space
734 // connection-address] CRLF
735 std::ostringstream os;
736 InitAttrLine(kAttributeRtcp, &os);
737 os << kSdpDelimiterColon
738 << rtcp_port << " "
739 << kConnectionNettype << " "
740 << addr_type << " "
741 << rtcp_ip;
742 rtcp_line = os.str();
wu@webrtc.org4c3e9912014-07-16 21:03:13 +0000743 return rtcp_line;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000744}
745
746// Get candidates according to the mline index from SessionDescriptionInterface.
747static void GetCandidatesByMindex(const SessionDescriptionInterface& desci,
748 int mline_index,
749 std::vector<Candidate>* candidates) {
750 if (!candidates) {
751 return;
752 }
753 const IceCandidateCollection* cc = desci.candidates(mline_index);
754 for (size_t i = 0; i < cc->count(); ++i) {
755 const IceCandidateInterface* candidate = cc->at(i);
756 candidates->push_back(candidate->candidate());
757 }
758}
759
deadbeef90f1e1e2017-02-10 12:35:05 -0800760static bool IsValidPort(int port) {
761 return port >= 0 && port <= 65535;
762}
763
Steve Antone831b8c2018-02-01 12:22:16 -0800764std::string SdpSerialize(const JsepSessionDescription& jdesc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000765 const cricket::SessionDescription* desc = jdesc.description();
766 if (!desc) {
767 return "";
768 }
769
770 std::string message;
771
772 // Session Description.
773 AddLine(kSessionVersion, &message);
774 // Session Origin
775 // RFC 4566
776 // o=<username> <sess-id> <sess-version> <nettype> <addrtype>
777 // <unicast-address>
778 std::ostringstream os;
779 InitLine(kLineTypeOrigin, kSessionOriginUsername, &os);
jbauch083b73f2015-07-16 02:46:32 -0700780 const std::string& session_id = jdesc.session_id().empty() ?
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000781 kSessionOriginSessionId : jdesc.session_id();
jbauch083b73f2015-07-16 02:46:32 -0700782 const std::string& session_version = jdesc.session_version().empty() ?
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000783 kSessionOriginSessionVersion : jdesc.session_version();
784 os << " " << session_id << " " << session_version << " "
785 << kSessionOriginNettype << " " << kSessionOriginAddrtype << " "
786 << kSessionOriginAddress;
787 AddLine(os.str(), &message);
788 AddLine(kSessionName, &message);
789
790 // Time Description.
791 AddLine(kTimeDescription, &message);
792
793 // Group
794 if (desc->HasGroup(cricket::GROUP_TYPE_BUNDLE)) {
795 std::string group_line = kAttrGroup;
796 const cricket::ContentGroup* group =
797 desc->GetGroupByName(cricket::GROUP_TYPE_BUNDLE);
nisseede5da42017-01-12 05:15:36 -0800798 RTC_DCHECK(group != NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000799 const cricket::ContentNames& content_names = group->content_names();
800 for (cricket::ContentNames::const_iterator it = content_names.begin();
801 it != content_names.end(); ++it) {
802 group_line.append(" ");
803 group_line.append(*it);
804 }
805 AddLine(group_line, &message);
806 }
807
808 // MediaStream semantics
809 InitAttrLine(kAttributeMsidSemantics, &os);
810 os << kSdpDelimiterColon << " " << kMediaStreamSemantic;
wu@webrtc.org4c3e9912014-07-16 21:03:13 +0000811
Seth Hampson845e8782018-03-02 11:34:10 -0800812 std::set<std::string> media_stream_ids;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000813 const ContentInfo* audio_content = GetFirstAudioContent(desc);
814 if (audio_content)
Seth Hampson845e8782018-03-02 11:34:10 -0800815 GetMediaStreamIds(audio_content, &media_stream_ids);
wu@webrtc.org4c3e9912014-07-16 21:03:13 +0000816
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000817 const ContentInfo* video_content = GetFirstVideoContent(desc);
818 if (video_content)
Seth Hampson845e8782018-03-02 11:34:10 -0800819 GetMediaStreamIds(video_content, &media_stream_ids);
wu@webrtc.org4c3e9912014-07-16 21:03:13 +0000820
Seth Hampson845e8782018-03-02 11:34:10 -0800821 for (std::set<std::string>::const_iterator it = media_stream_ids.begin();
822 it != media_stream_ids.end(); ++it) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000823 os << " " << *it;
824 }
825 AddLine(os.str(), &message);
826
wu@webrtc.org4c3e9912014-07-16 21:03:13 +0000827 // Preserve the order of the media contents.
828 int mline_index = -1;
829 for (cricket::ContentInfos::const_iterator it = desc->contents().begin();
830 it != desc->contents().end(); ++it) {
Steve Antonb1c1de12017-12-21 15:14:30 -0800831 const MediaContentDescription* mdesc = it->media_description();
wu@webrtc.org4c3e9912014-07-16 21:03:13 +0000832 std::vector<Candidate> candidates;
833 GetCandidatesByMindex(jdesc, ++mline_index, &candidates);
deadbeef9d3584c2016-02-16 17:54:10 -0800834 BuildMediaDescription(&*it, desc->GetTransportInfoByName(it->name),
Steve Antone831b8c2018-02-01 12:22:16 -0800835 mdesc->type(), candidates, desc->msid_signaling(),
wu@webrtc.org4c3e9912014-07-16 21:03:13 +0000836 &message);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000837 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000838 return message;
839}
840
841// Serializes the passed in IceCandidateInterface to a SDP string.
842// candidate - The candidate to be serialized.
Honghai Zhang7fb69db2016-03-14 11:59:18 -0700843std::string SdpSerializeCandidate(const IceCandidateInterface& candidate) {
844 return SdpSerializeCandidate(candidate.candidate());
845}
846
847// Serializes a cricket Candidate.
848std::string SdpSerializeCandidate(const cricket::Candidate& candidate) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000849 std::string message;
Honghai Zhang7fb69db2016-03-14 11:59:18 -0700850 std::vector<cricket::Candidate> candidates(1, candidate);
honghaiza54a0802015-12-16 18:37:23 -0800851 BuildCandidate(candidates, true, &message);
wu@webrtc.orgec9f5fb2014-06-24 17:05:10 +0000852 // From WebRTC draft section 4.8.1.1 candidate-attribute will be
853 // just candidate:<candidate> not a=candidate:<blah>CRLF
nisseede5da42017-01-12 05:15:36 -0800854 RTC_DCHECK(message.find("a=") == 0);
wu@webrtc.orgec9f5fb2014-06-24 17:05:10 +0000855 message.erase(0, 2);
nisseede5da42017-01-12 05:15:36 -0800856 RTC_DCHECK(message.find(kLineBreak) == message.size() - 2);
wu@webrtc.orgec9f5fb2014-06-24 17:05:10 +0000857 message.resize(message.size() - 2);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000858 return message;
859}
860
861bool SdpDeserialize(const std::string& message,
862 JsepSessionDescription* jdesc,
863 SdpParseError* error) {
864 std::string session_id;
865 std::string session_version;
Peter Thatcher7cbd1882015-09-17 18:54:52 -0700866 TransportDescription session_td("", "");
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000867 RtpHeaderExtensions session_extmaps;
zhihuang38989e52017-03-21 11:04:53 -0700868 rtc::SocketAddress session_connection_addr;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000869 cricket::SessionDescription* desc = new cricket::SessionDescription();
870 std::vector<JsepIceCandidate*> candidates;
871 size_t current_pos = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000872
873 // Session Description
874 if (!ParseSessionDescription(message, &current_pos, &session_id,
deadbeefc80741f2015-10-22 13:14:45 -0700875 &session_version, &session_td, &session_extmaps,
zhihuang38989e52017-03-21 11:04:53 -0700876 &session_connection_addr, desc, error)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000877 delete desc;
878 return false;
879 }
880
881 // Media Description
deadbeefc80741f2015-10-22 13:14:45 -0700882 if (!ParseMediaDescription(message, session_td, session_extmaps, &current_pos,
zhihuang38989e52017-03-21 11:04:53 -0700883 session_connection_addr, desc, &candidates,
884 error)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000885 delete desc;
886 for (std::vector<JsepIceCandidate*>::const_iterator
887 it = candidates.begin(); it != candidates.end(); ++it) {
888 delete *it;
889 }
890 return false;
891 }
892
893 jdesc->Initialize(desc, session_id, session_version);
894
895 for (std::vector<JsepIceCandidate*>::const_iterator
896 it = candidates.begin(); it != candidates.end(); ++it) {
897 jdesc->AddCandidate(*it);
898 delete *it;
899 }
900 return true;
901}
902
903bool SdpDeserializeCandidate(const std::string& message,
904 JsepIceCandidate* jcandidate,
905 SdpParseError* error) {
nisseede5da42017-01-12 05:15:36 -0800906 RTC_DCHECK(jcandidate != NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000907 Candidate candidate;
908 if (!ParseCandidate(message, &candidate, error, true)) {
909 return false;
910 }
911 jcandidate->SetCandidate(candidate);
912 return true;
913}
914
Honghai Zhang7fb69db2016-03-14 11:59:18 -0700915bool SdpDeserializeCandidate(const std::string& transport_name,
916 const std::string& message,
917 cricket::Candidate* candidate,
918 SdpParseError* error) {
nisseede5da42017-01-12 05:15:36 -0800919 RTC_DCHECK(candidate != nullptr);
Honghai Zhang7fb69db2016-03-14 11:59:18 -0700920 if (!ParseCandidate(message, candidate, error, true)) {
921 return false;
922 }
923 candidate->set_transport_name(transport_name);
924 return true;
925}
926
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000927bool ParseCandidate(const std::string& message, Candidate* candidate,
928 SdpParseError* error, bool is_raw) {
nisseede5da42017-01-12 05:15:36 -0800929 RTC_DCHECK(candidate != NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000930
931 // Get the first line from |message|.
jiayl@webrtc.org7ec3f9f2014-08-08 23:09:15 +0000932 std::string first_line = message;
933 size_t pos = 0;
934 GetLine(message, &pos, &first_line);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000935
jiayl@webrtc.org7ec3f9f2014-08-08 23:09:15 +0000936 // Makes sure |message| contains only one line.
937 if (message.size() > first_line.size()) {
938 std::string left, right;
Donald Curtis0e07f922015-05-15 09:21:23 -0700939 if (rtc::tokenize_first(message, kNewLine, &left, &right) &&
940 !right.empty()) {
jiayl@webrtc.org7ec3f9f2014-08-08 23:09:15 +0000941 return ParseFailed(message, 0, "Expect one line only", error);
942 }
943 }
944
945 // From WebRTC draft section 4.8.1.1 candidate-attribute should be
946 // candidate:<candidate> when trickled, but we still support
947 // a=candidate:<blah>CRLF for backward compatibility and for parsing a line
948 // from the SDP.
949 if (IsLineType(first_line, kLineTypeAttributes)) {
950 first_line = first_line.substr(kLinePrefixLength);
951 }
952
953 std::string attribute_candidate;
954 std::string candidate_value;
955
956 // |first_line| must be in the form of "candidate:<value>".
Donald Curtis144d0182015-05-15 13:14:24 -0700957 if (!rtc::tokenize_first(first_line, kSdpDelimiterColon, &attribute_candidate,
958 &candidate_value) ||
jiayl@webrtc.org7ec3f9f2014-08-08 23:09:15 +0000959 attribute_candidate != kAttributeCandidate) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000960 if (is_raw) {
961 std::ostringstream description;
jiayl@webrtc.org7ec3f9f2014-08-08 23:09:15 +0000962 description << "Expect line: " << kAttributeCandidate
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000963 << ":" << "<candidate-str>";
964 return ParseFailed(first_line, 0, description.str(), error);
965 } else {
966 return ParseFailedExpectLine(first_line, 0, kLineTypeAttributes,
967 kAttributeCandidate, error);
968 }
969 }
970
971 std::vector<std::string> fields;
jiayl@webrtc.org7ec3f9f2014-08-08 23:09:15 +0000972 rtc::split(candidate_value, kSdpDelimiterSpace, &fields);
973
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000974 // RFC 5245
975 // a=candidate:<foundation> <component-id> <transport> <priority>
976 // <connection-address> <port> typ <candidate-types>
977 // [raddr <connection-address>] [rport <port>]
978 // *(SP extension-att-name SP extension-att-value)
979 const size_t expected_min_fields = 8;
980 if (fields.size() < expected_min_fields ||
981 (fields[6] != kAttributeCandidateTyp)) {
982 return ParseFailedExpectMinFieldNum(first_line, expected_min_fields, error);
983 }
jbauch083b73f2015-07-16 02:46:32 -0700984 const std::string& foundation = fields[0];
jiayl@webrtc.org7ec3f9f2014-08-08 23:09:15 +0000985
wu@webrtc.org5e760e72014-04-02 23:19:09 +0000986 int component_id = 0;
987 if (!GetValueFromString(first_line, fields[1], &component_id, error)) {
988 return false;
989 }
jbauch083b73f2015-07-16 02:46:32 -0700990 const std::string& transport = fields[2];
Peter Boström0c4e06b2015-10-07 12:23:21 +0200991 uint32_t priority = 0;
wu@webrtc.org5e760e72014-04-02 23:19:09 +0000992 if (!GetValueFromString(first_line, fields[3], &priority, error)) {
993 return false;
994 }
jbauch083b73f2015-07-16 02:46:32 -0700995 const std::string& connection_address = fields[4];
wu@webrtc.org5e760e72014-04-02 23:19:09 +0000996 int port = 0;
997 if (!GetValueFromString(first_line, fields[5], &port, error)) {
998 return false;
999 }
deadbeef90f1e1e2017-02-10 12:35:05 -08001000 if (!IsValidPort(port)) {
1001 return ParseFailed(first_line, "Invalid port number.", error);
1002 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001003 SocketAddress address(connection_address, port);
1004
1005 cricket::ProtocolType protocol;
hnslb68cc752016-12-13 10:33:41 -08001006 if (!StringToProto(transport.c_str(), &protocol)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001007 return ParseFailed(first_line, "Unsupported transport type.", error);
1008 }
hnslb68cc752016-12-13 10:33:41 -08001009 switch (protocol) {
1010 case cricket::PROTO_UDP:
1011 case cricket::PROTO_TCP:
1012 case cricket::PROTO_SSLTCP:
1013 // Supported protocol.
1014 break;
1015 default:
1016 return ParseFailed(first_line, "Unsupported transport type.", error);
1017 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001018
1019 std::string candidate_type;
jbauch083b73f2015-07-16 02:46:32 -07001020 const std::string& type = fields[7];
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001021 if (type == kCandidateHost) {
1022 candidate_type = cricket::LOCAL_PORT_TYPE;
1023 } else if (type == kCandidateSrflx) {
1024 candidate_type = cricket::STUN_PORT_TYPE;
1025 } else if (type == kCandidateRelay) {
1026 candidate_type = cricket::RELAY_PORT_TYPE;
Honghai Zhang7fb69db2016-03-14 11:59:18 -07001027 } else if (type == kCandidatePrflx) {
1028 candidate_type = cricket::PRFLX_PORT_TYPE;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001029 } else {
1030 return ParseFailed(first_line, "Unsupported candidate type.", error);
1031 }
1032
1033 size_t current_position = expected_min_fields;
1034 SocketAddress related_address;
1035 // The 2 optional fields for related address
1036 // [raddr <connection-address>] [rport <port>]
1037 if (fields.size() >= (current_position + 2) &&
1038 fields[current_position] == kAttributeCandidateRaddr) {
1039 related_address.SetIP(fields[++current_position]);
1040 ++current_position;
1041 }
1042 if (fields.size() >= (current_position + 2) &&
1043 fields[current_position] == kAttributeCandidateRport) {
wu@webrtc.org5e760e72014-04-02 23:19:09 +00001044 int port = 0;
1045 if (!GetValueFromString(
1046 first_line, fields[++current_position], &port, error)) {
1047 return false;
1048 }
deadbeef90f1e1e2017-02-10 12:35:05 -08001049 if (!IsValidPort(port)) {
1050 return ParseFailed(first_line, "Invalid port number.", error);
1051 }
wu@webrtc.org5e760e72014-04-02 23:19:09 +00001052 related_address.SetPort(port);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001053 ++current_position;
1054 }
1055
mallinath@webrtc.org2d60c5e2014-08-08 22:29:20 +00001056 // If this is a TCP candidate, it has additional extension as defined in
1057 // RFC 6544.
1058 std::string tcptype;
1059 if (fields.size() >= (current_position + 2) &&
1060 fields[current_position] == kTcpCandidateType) {
1061 tcptype = fields[++current_position];
1062 ++current_position;
1063
1064 if (tcptype != cricket::TCPTYPE_ACTIVE_STR &&
1065 tcptype != cricket::TCPTYPE_PASSIVE_STR &&
1066 tcptype != cricket::TCPTYPE_SIMOPEN_STR) {
1067 return ParseFailed(first_line, "Invalid TCP candidate type.", error);
1068 }
1069
1070 if (protocol != cricket::PROTO_TCP) {
1071 return ParseFailed(first_line, "Invalid non-TCP candidate", error);
1072 }
1073 }
1074
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001075 // Extension
honghaiza54a0802015-12-16 18:37:23 -08001076 // Though non-standard, we support the ICE ufrag and pwd being signaled on
1077 // the candidate to avoid issues with confusing which generation a candidate
1078 // belongs to when trickling multiple generations at the same time.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001079 std::string username;
1080 std::string password;
Peter Boström0c4e06b2015-10-07 12:23:21 +02001081 uint32_t generation = 0;
honghaiza0c44ea2016-03-23 16:07:48 -07001082 uint16_t network_id = 0;
1083 uint16_t network_cost = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001084 for (size_t i = current_position; i + 1 < fields.size(); ++i) {
1085 // RFC 5245
1086 // *(SP extension-att-name SP extension-att-value)
1087 if (fields[i] == kAttributeCandidateGeneration) {
wu@webrtc.org5e760e72014-04-02 23:19:09 +00001088 if (!GetValueFromString(first_line, fields[++i], &generation, error)) {
1089 return false;
1090 }
honghaiza54a0802015-12-16 18:37:23 -08001091 } else if (fields[i] == kAttributeCandidateUfrag) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001092 username = fields[++i];
honghaiza54a0802015-12-16 18:37:23 -08001093 } else if (fields[i] == kAttributeCandidatePwd) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001094 password = fields[++i];
honghaiza0c44ea2016-03-23 16:07:48 -07001095 } else if (fields[i] == kAttributeCandidateNetworkId) {
1096 if (!GetValueFromString(first_line, fields[++i], &network_id, error)) {
1097 return false;
1098 }
honghaize1a0c942016-02-16 14:54:56 -08001099 } else if (fields[i] == kAttributeCandidateNetworkCost) {
1100 if (!GetValueFromString(first_line, fields[++i], &network_cost, error)) {
1101 return false;
1102 }
Honghai Zhang351d77b2016-05-20 15:08:29 -07001103 network_cost = std::min(network_cost, rtc::kNetworkCostMax);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001104 } else {
1105 // Skip the unknown extension.
1106 ++i;
1107 }
1108 }
1109
guoweis@webrtc.org61c12472015-01-15 06:53:07 +00001110 *candidate = Candidate(component_id, cricket::ProtoToString(protocol),
guoweis@webrtc.org950c5182014-12-16 23:01:31 +00001111 address, priority, username, password, candidate_type,
honghaiza0c44ea2016-03-23 16:07:48 -07001112 generation, foundation, network_id, network_cost);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001113 candidate->set_related_address(related_address);
mallinath@webrtc.org2d60c5e2014-08-08 22:29:20 +00001114 candidate->set_tcptype(tcptype);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001115 return true;
1116}
1117
1118bool ParseIceOptions(const std::string& line,
1119 std::vector<std::string>* transport_options,
1120 SdpParseError* error) {
1121 std::string ice_options;
1122 if (!GetValue(line, kAttributeIceOption, &ice_options, error)) {
1123 return false;
1124 }
1125 std::vector<std::string> fields;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001126 rtc::split(ice_options, kSdpDelimiterSpace, &fields);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001127 for (size_t i = 0; i < fields.size(); ++i) {
1128 transport_options->push_back(fields[i]);
1129 }
1130 return true;
1131}
1132
jiayl@webrtc.orgddb85ab2014-09-05 16:31:56 +00001133bool ParseSctpPort(const std::string& line,
1134 int* sctp_port,
1135 SdpParseError* error) {
1136 // draft-ietf-mmusic-sctp-sdp-07
1137 // a=sctp-port
1138 std::vector<std::string> fields;
jiayl@webrtc.orgddb85ab2014-09-05 16:31:56 +00001139 const size_t expected_min_fields = 2;
lally69f57602015-10-08 10:15:04 -07001140 rtc::split(line.substr(kLinePrefixLength), kSdpDelimiterColon, &fields);
1141 if (fields.size() < expected_min_fields) {
1142 fields.resize(0);
1143 rtc::split(line.substr(kLinePrefixLength), kSdpDelimiterSpace, &fields);
1144 }
jiayl@webrtc.orgddb85ab2014-09-05 16:31:56 +00001145 if (fields.size() < expected_min_fields) {
1146 return ParseFailedExpectMinFieldNum(line, expected_min_fields, error);
1147 }
1148 if (!rtc::FromString(fields[1], sctp_port)) {
lally69f57602015-10-08 10:15:04 -07001149 return ParseFailed(line, "Invalid sctp port value.", error);
jiayl@webrtc.orgddb85ab2014-09-05 16:31:56 +00001150 }
1151 return true;
1152}
1153
isheriff6f8d6862016-05-26 11:24:55 -07001154bool ParseExtmap(const std::string& line,
1155 RtpExtension* extmap,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001156 SdpParseError* error) {
1157 // RFC 5285
1158 // a=extmap:<value>["/"<direction>] <URI> <extensionattributes>
1159 std::vector<std::string> fields;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001160 rtc::split(line.substr(kLinePrefixLength),
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001161 kSdpDelimiterSpace, &fields);
1162 const size_t expected_min_fields = 2;
1163 if (fields.size() < expected_min_fields) {
1164 return ParseFailedExpectMinFieldNum(line, expected_min_fields, error);
1165 }
1166 std::string uri = fields[1];
1167
1168 std::string value_direction;
1169 if (!GetValue(fields[0], kAttributeExtmap, &value_direction, error)) {
1170 return false;
1171 }
1172 std::vector<std::string> sub_fields;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001173 rtc::split(value_direction, kSdpDelimiterSlash, &sub_fields);
wu@webrtc.org5e760e72014-04-02 23:19:09 +00001174 int value = 0;
1175 if (!GetValueFromString(line, sub_fields[0], &value, error)) {
1176 return false;
1177 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001178
jbauch5869f502017-06-29 12:31:36 -07001179 bool encrypted = false;
1180 if (uri == RtpExtension::kEncryptHeaderExtensionsUri) {
1181 // RFC 6904
Steve Anton36b29d12017-10-30 09:57:42 -07001182 // a=extmap:<value["/"<direction>] urn:ietf:params:rtp-hdrext:encrypt <URI>
1183 // <extensionattributes>
jbauch5869f502017-06-29 12:31:36 -07001184 const size_t expected_min_fields_encrypted = expected_min_fields + 1;
1185 if (fields.size() < expected_min_fields_encrypted) {
1186 return ParseFailedExpectMinFieldNum(line, expected_min_fields_encrypted,
1187 error);
1188 }
1189
1190 encrypted = true;
1191 uri = fields[2];
1192 if (uri == RtpExtension::kEncryptHeaderExtensionsUri) {
1193 return ParseFailed(line, "Recursive encrypted header.", error);
1194 }
1195 }
1196
1197 *extmap = RtpExtension(uri, value, encrypted);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001198 return true;
1199}
1200
1201void BuildMediaDescription(const ContentInfo* content_info,
1202 const TransportInfo* transport_info,
1203 const MediaType media_type,
wu@webrtc.org4c3e9912014-07-16 21:03:13 +00001204 const std::vector<Candidate>& candidates,
Steve Antone831b8c2018-02-01 12:22:16 -08001205 int msid_signaling,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001206 std::string* message) {
nisseede5da42017-01-12 05:15:36 -08001207 RTC_DCHECK(message != NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001208 if (content_info == NULL || message == NULL) {
1209 return;
1210 }
Steve Anton36b29d12017-10-30 09:57:42 -07001211 // TODO(deadbeef): Rethink if we should use sprintfn instead of stringstream.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001212 // According to the style guide, streams should only be used for logging.
1213 // http://google-styleguide.googlecode.com/svn/
1214 // trunk/cppguide.xml?showone=Streams#Streams
1215 std::ostringstream os;
Steve Antonb1c1de12017-12-21 15:14:30 -08001216 const MediaContentDescription* media_desc = content_info->media_description();
1217 RTC_DCHECK(media_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001218
jiayl@webrtc.org9c16c392014-05-01 18:30:30 +00001219 int sctp_port = cricket::kSctpDefaultPort;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001220
1221 // RFC 4566
1222 // m=<media> <port> <proto> <fmt>
1223 // fmt is a list of payload type numbers that MAY be used in the session.
1224 const char* type = NULL;
1225 if (media_type == cricket::MEDIA_TYPE_AUDIO)
1226 type = kMediaTypeAudio;
1227 else if (media_type == cricket::MEDIA_TYPE_VIDEO)
1228 type = kMediaTypeVideo;
1229 else if (media_type == cricket::MEDIA_TYPE_DATA)
1230 type = kMediaTypeData;
1231 else
nissec80e7412017-01-11 05:56:46 -08001232 RTC_NOTREACHED();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001233
1234 std::string fmt;
1235 if (media_type == cricket::MEDIA_TYPE_VIDEO) {
Steve Antonb1c1de12017-12-21 15:14:30 -08001236 const VideoContentDescription* video_desc = media_desc->as_video();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001237 for (std::vector<cricket::VideoCodec>::const_iterator it =
1238 video_desc->codecs().begin();
1239 it != video_desc->codecs().end(); ++it) {
1240 fmt.append(" ");
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001241 fmt.append(rtc::ToString<int>(it->id));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001242 }
1243 } else if (media_type == cricket::MEDIA_TYPE_AUDIO) {
Steve Antonb1c1de12017-12-21 15:14:30 -08001244 const AudioContentDescription* audio_desc = media_desc->as_audio();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001245 for (std::vector<cricket::AudioCodec>::const_iterator it =
1246 audio_desc->codecs().begin();
1247 it != audio_desc->codecs().end(); ++it) {
1248 fmt.append(" ");
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001249 fmt.append(rtc::ToString<int>(it->id));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001250 }
1251 } else if (media_type == cricket::MEDIA_TYPE_DATA) {
Steve Antonb1c1de12017-12-21 15:14:30 -08001252 const DataContentDescription* data_desc = media_desc->as_data();
pthatcher@webrtc.org3341b402015-02-13 21:14:22 +00001253 if (IsDtlsSctp(media_desc->protocol())) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001254 fmt.append(" ");
jiayl@webrtc.org9c16c392014-05-01 18:30:30 +00001255
zstein4b2e0822017-02-17 19:48:38 -08001256 if (data_desc->use_sctpmap()) {
1257 for (std::vector<cricket::DataCodec>::const_iterator it =
1258 data_desc->codecs().begin();
1259 it != data_desc->codecs().end(); ++it) {
1260 if (cricket::CodecNamesEq(it->name,
1261 cricket::kGoogleSctpDataCodecName) &&
1262 it->GetParam(cricket::kCodecParamPort, &sctp_port)) {
1263 break;
1264 }
jiayl@webrtc.org9c16c392014-05-01 18:30:30 +00001265 }
jiayl@webrtc.org9c16c392014-05-01 18:30:30 +00001266
zstein4b2e0822017-02-17 19:48:38 -08001267 fmt.append(rtc::ToString<int>(sctp_port));
1268 } else {
1269 fmt.append(kDefaultSctpmapProtocol);
1270 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001271 } else {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001272 for (std::vector<cricket::DataCodec>::const_iterator it =
1273 data_desc->codecs().begin();
1274 it != data_desc->codecs().end(); ++it) {
1275 fmt.append(" ");
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001276 fmt.append(rtc::ToString<int>(it->id));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001277 }
1278 }
1279 }
1280 // The fmt must never be empty. If no codecs are found, set the fmt attribute
1281 // to 0.
1282 if (fmt.empty()) {
1283 fmt = " 0";
1284 }
1285
deadbeef25ed4352016-12-12 18:37:36 -08001286 // The port number in the m line will be updated later when associated with
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001287 // the candidates.
deadbeef25ed4352016-12-12 18:37:36 -08001288 //
1289 // A port value of 0 indicates that the m= section is rejected.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001290 // RFC 3264
1291 // To reject an offered stream, the port number in the corresponding stream in
1292 // the answer MUST be set to zero.
deadbeef25ed4352016-12-12 18:37:36 -08001293 //
1294 // However, the BUNDLE draft adds a new meaning to port zero, when used along
1295 // with a=bundle-only.
zhihuang38989e52017-03-21 11:04:53 -07001296 std::string port = kDummyPort;
1297 if (content_info->rejected || content_info->bundle_only) {
1298 port = kMediaPortRejected;
1299 } else if (!media_desc->connection_address().IsNil()) {
1300 port = rtc::ToString(media_desc->connection_address().port());
1301 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001302
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001303 rtc::SSLFingerprint* fp = (transport_info) ?
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001304 transport_info->description.identity_fingerprint.get() : NULL;
1305
wu@webrtc.org4c3e9912014-07-16 21:03:13 +00001306 // Add the m and c lines.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001307 InitLine(kLineTypeMedia, type, &os);
1308 os << " " << port << " " << media_desc->protocol() << fmt;
zhihuang38989e52017-03-21 11:04:53 -07001309 AddLine(os.str(), message);
1310
1311 InitLine(kLineTypeConnection, kConnectionNettype, &os);
1312 if (media_desc->connection_address().IsNil()) {
1313 os << " " << kConnectionIpv4Addrtype << " " << kDummyAddress;
1314 } else if (media_desc->connection_address().family() == AF_INET) {
1315 os << " " << kConnectionIpv4Addrtype << " "
1316 << media_desc->connection_address().ipaddr().ToString();
1317 } else {
1318 os << " " << kConnectionIpv6Addrtype << " "
1319 << media_desc->connection_address().ipaddr().ToString();
1320 }
1321 AddLine(os.str(), message);
wu@webrtc.org4c3e9912014-07-16 21:03:13 +00001322
1323 // RFC 4566
1324 // b=AS:<bandwidth>
Peter Thatcherc0c3a862015-06-24 15:31:25 -07001325 if (media_desc->bandwidth() >= 1000) {
wu@webrtc.org4c3e9912014-07-16 21:03:13 +00001326 InitLine(kLineTypeSessionBandwidth, kApplicationSpecificMaximum, &os);
1327 os << kSdpDelimiterColon << (media_desc->bandwidth() / 1000);
1328 AddLine(os.str(), message);
1329 }
1330
deadbeef25ed4352016-12-12 18:37:36 -08001331 // Add the a=bundle-only line.
1332 if (content_info->bundle_only) {
1333 InitAttrLine(kAttributeBundleOnly, &os);
1334 AddLine(os.str(), message);
1335 }
1336
wu@webrtc.org4c3e9912014-07-16 21:03:13 +00001337 // Add the a=rtcp line.
pthatcher@webrtc.org3341b402015-02-13 21:14:22 +00001338 if (IsRtp(media_desc->protocol())) {
wu@webrtc.org4c3e9912014-07-16 21:03:13 +00001339 std::string rtcp_line = GetRtcpLine(candidates);
1340 if (!rtcp_line.empty()) {
1341 AddLine(rtcp_line, message);
1342 }
1343 }
1344
honghaiza54a0802015-12-16 18:37:23 -08001345 // Build the a=candidate lines. We don't include ufrag and pwd in the
1346 // candidates in the SDP to avoid redundancy.
1347 BuildCandidate(candidates, false, message);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001348
1349 // Use the transport_info to build the media level ice-ufrag and ice-pwd.
1350 if (transport_info) {
1351 // RFC 5245
1352 // ice-pwd-att = "ice-pwd" ":" password
1353 // ice-ufrag-att = "ice-ufrag" ":" ufrag
1354 // ice-ufrag
deadbeef3f7219b2015-12-28 15:17:14 -08001355 if (!transport_info->description.ice_ufrag.empty()) {
1356 InitAttrLine(kAttributeIceUfrag, &os);
1357 os << kSdpDelimiterColon << transport_info->description.ice_ufrag;
1358 AddLine(os.str(), message);
1359 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001360 // ice-pwd
deadbeef3f7219b2015-12-28 15:17:14 -08001361 if (!transport_info->description.ice_pwd.empty()) {
1362 InitAttrLine(kAttributeIcePwd, &os);
1363 os << kSdpDelimiterColon << transport_info->description.ice_pwd;
1364 AddLine(os.str(), message);
1365 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001366
1367 // draft-petithuguenin-mmusic-ice-attributes-level-03
1368 BuildIceOptions(transport_info->description.transport_options, message);
1369
1370 // RFC 4572
1371 // fingerprint-attribute =
1372 // "fingerprint" ":" hash-func SP fingerprint
1373 if (fp) {
1374 // Insert the fingerprint attribute.
1375 InitAttrLine(kAttributeFingerprint, &os);
1376 os << kSdpDelimiterColon
1377 << fp->algorithm << kSdpDelimiterSpace
1378 << fp->GetRfc4572Fingerprint();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001379 AddLine(os.str(), message);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001380
1381 // Inserting setup attribute.
1382 if (transport_info->description.connection_role !=
1383 cricket::CONNECTIONROLE_NONE) {
1384 // Making sure we are not using "passive" mode.
1385 cricket::ConnectionRole role =
1386 transport_info->description.connection_role;
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00001387 std::string dtls_role_str;
nissec16fa5e2017-02-07 07:18:43 -08001388 const bool success =
1389 cricket::ConnectionRoleToString(role, &dtls_role_str);
1390 RTC_DCHECK(success);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001391 InitAttrLine(kAttributeSetup, &os);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001392 os << kSdpDelimiterColon << dtls_role_str;
1393 AddLine(os.str(), message);
1394 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001395 }
1396 }
1397
1398 // RFC 3388
1399 // mid-attribute = "a=mid:" identification-tag
1400 // identification-tag = token
1401 // Use the content name as the mid identification-tag.
1402 InitAttrLine(kAttributeMid, &os);
1403 os << kSdpDelimiterColon << content_info->name;
1404 AddLine(os.str(), message);
1405
pthatcher@webrtc.org3341b402015-02-13 21:14:22 +00001406 if (IsDtlsSctp(media_desc->protocol())) {
Steve Antonb1c1de12017-12-21 15:14:30 -08001407 const DataContentDescription* data_desc = media_desc->as_data();
zstein4b2e0822017-02-17 19:48:38 -08001408 bool use_sctpmap = data_desc->use_sctpmap();
1409 BuildSctpContentAttributes(message, sctp_port, use_sctpmap);
pthatcher@webrtc.org3341b402015-02-13 21:14:22 +00001410 } else if (IsRtp(media_desc->protocol())) {
Steve Antone831b8c2018-02-01 12:22:16 -08001411 BuildRtpContentAttributes(media_desc, media_type, msid_signaling, message);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001412 }
1413}
1414
zstein4b2e0822017-02-17 19:48:38 -08001415void BuildSctpContentAttributes(std::string* message,
1416 int sctp_port,
1417 bool use_sctpmap) {
wu@webrtc.org78187522013-10-07 23:32:02 +00001418 std::ostringstream os;
zstein4b2e0822017-02-17 19:48:38 -08001419 if (use_sctpmap) {
1420 // draft-ietf-mmusic-sctp-sdp-04
1421 // a=sctpmap:sctpmap-number protocol [streams]
1422 InitAttrLine(kAttributeSctpmap, &os);
1423 os << kSdpDelimiterColon << sctp_port << kSdpDelimiterSpace
1424 << kDefaultSctpmapProtocol << kSdpDelimiterSpace
1425 << cricket::kMaxSctpStreams;
1426 } else {
1427 // draft-ietf-mmusic-sctp-sdp-23
1428 // a=sctp-port:<port>
1429 InitAttrLine(kAttributeSctpPort, &os);
1430 os << kSdpDelimiterColon << sctp_port;
1431 // TODO(zstein): emit max-message-size here
1432 }
wu@webrtc.org78187522013-10-07 23:32:02 +00001433 AddLine(os.str(), message);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001434}
1435
deadbeef9d3584c2016-02-16 17:54:10 -08001436void BuildRtpContentAttributes(const MediaContentDescription* media_desc,
1437 const MediaType media_type,
Steve Antone831b8c2018-02-01 12:22:16 -08001438 int msid_signaling,
deadbeef9d3584c2016-02-16 17:54:10 -08001439 std::string* message) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001440 std::ostringstream os;
1441 // RFC 5285
1442 // a=extmap:<value>["/"<direction>] <URI> <extensionattributes>
1443 // The definitions MUST be either all session level or all media level. This
1444 // implementation uses all media level.
1445 for (size_t i = 0; i < media_desc->rtp_header_extensions().size(); ++i) {
jbauch5869f502017-06-29 12:31:36 -07001446 const RtpExtension& extension = media_desc->rtp_header_extensions()[i];
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001447 InitAttrLine(kAttributeExtmap, &os);
jbauch5869f502017-06-29 12:31:36 -07001448 os << kSdpDelimiterColon << extension.id;
1449 if (extension.encrypt) {
1450 os << kSdpDelimiterSpace << RtpExtension::kEncryptHeaderExtensionsUri;
1451 }
1452 os << kSdpDelimiterSpace << extension.uri;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001453 AddLine(os.str(), message);
1454 }
1455
1456 // RFC 3264
1457 // a=sendrecv || a=sendonly || a=sendrecv || a=inactive
deadbeefc80741f2015-10-22 13:14:45 -07001458 switch (media_desc->direction()) {
Steve Anton4e70a722017-11-28 14:57:10 -08001459 case RtpTransceiverDirection::kInactive:
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001460 InitAttrLine(kAttributeInactive, &os);
1461 break;
Steve Anton4e70a722017-11-28 14:57:10 -08001462 case RtpTransceiverDirection::kSendOnly:
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001463 InitAttrLine(kAttributeSendOnly, &os);
1464 break;
Steve Anton4e70a722017-11-28 14:57:10 -08001465 case RtpTransceiverDirection::kRecvOnly:
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001466 InitAttrLine(kAttributeRecvOnly, &os);
1467 break;
Steve Anton4e70a722017-11-28 14:57:10 -08001468 case RtpTransceiverDirection::kSendRecv:
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001469 default:
1470 InitAttrLine(kAttributeSendRecv, &os);
1471 break;
1472 }
1473 AddLine(os.str(), message);
1474
Tomas Gunnarsson191bf5c2018-03-30 10:44:43 +00001475 // Specified in https://tools.ietf.org/html/draft-ietf-mmusic-msid-16
1476 // a=msid:<stream id> <track id>
Steve Antone831b8c2018-02-01 12:22:16 -08001477 if (msid_signaling & cricket::kMsidSignalingMediaSection) {
1478 const StreamParamsVec& streams = media_desc->streams();
1479 if (streams.size() == 1u) {
1480 const StreamParams& track = streams[0];
Tomas Gunnarsson191bf5c2018-03-30 10:44:43 +00001481 // TODO(bugs.webrtc.org/7932): Support serializing more than one stream
1482 // label.
1483 const std::string& stream_id = track.first_stream_id();
1484 InitAttrLine(kAttributeMsid, &os);
1485 os << kSdpDelimiterColon << stream_id << kSdpDelimiterSpace << track.id;
1486 AddLine(os.str(), message);
Steve Antone831b8c2018-02-01 12:22:16 -08001487 } else if (streams.size() > 1u) {
1488 RTC_LOG(LS_WARNING)
1489 << "Trying to serialize Unified Plan SDP with more than "
Jonas Olsson45cc8902018-02-13 10:37:07 +01001490 "one track in a media section. Omitting 'a=msid'.";
deadbeef9d3584c2016-02-16 17:54:10 -08001491 }
1492 }
1493
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001494 // RFC 5761
1495 // a=rtcp-mux
1496 if (media_desc->rtcp_mux()) {
1497 InitAttrLine(kAttributeRtcpMux, &os);
1498 AddLine(os.str(), message);
1499 }
1500
deadbeef13871492015-12-09 12:37:51 -08001501 // RFC 5506
1502 // a=rtcp-rsize
1503 if (media_desc->rtcp_reduced_size()) {
1504 InitAttrLine(kAttributeRtcpReducedSize, &os);
1505 AddLine(os.str(), message);
1506 }
1507
deadbeefd45aea82017-09-16 01:24:29 -07001508 if (media_desc->conference_mode()) {
1509 InitAttrLine(kAttributeXGoogleFlag, &os);
1510 os << kSdpDelimiterColon << kValueConference;
1511 AddLine(os.str(), message);
1512 }
1513
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001514 // RFC 4568
1515 // a=crypto:<tag> <crypto-suite> <key-params> [<session-params>]
1516 for (std::vector<CryptoParams>::const_iterator it =
1517 media_desc->cryptos().begin();
1518 it != media_desc->cryptos().end(); ++it) {
1519 InitAttrLine(kAttributeCrypto, &os);
1520 os << kSdpDelimiterColon << it->tag << " " << it->cipher_suite << " "
1521 << it->key_params;
1522 if (!it->session_params.empty()) {
1523 os << " " << it->session_params;
1524 }
1525 AddLine(os.str(), message);
1526 }
1527
1528 // RFC 4566
1529 // a=rtpmap:<payload type> <encoding name>/<clock rate>
1530 // [/<encodingparameters>]
1531 BuildRtpMap(media_desc, media_type, message);
1532
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001533 for (StreamParamsVec::const_iterator track = media_desc->streams().begin();
1534 track != media_desc->streams().end(); ++track) {
Tomas Gunnarsson191bf5c2018-03-30 10:44:43 +00001535 // Require that the track belongs to a media stream. This extra check is
1536 // necessary since the MediaContentDescription always contains a
1537 // StreamParams with an ssrc even if no track or media stream have been
1538 // created.
1539 if (track->stream_ids().empty())
1540 continue;
1541
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001542 // Build the ssrc-group lines.
1543 for (size_t i = 0; i < track->ssrc_groups.size(); ++i) {
1544 // RFC 5576
1545 // a=ssrc-group:<semantics> <ssrc-id> ...
1546 if (track->ssrc_groups[i].ssrcs.empty()) {
1547 continue;
1548 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001549 InitAttrLine(kAttributeSsrcGroup, &os);
1550 os << kSdpDelimiterColon << track->ssrc_groups[i].semantics;
Peter Boström0c4e06b2015-10-07 12:23:21 +02001551 std::vector<uint32_t>::const_iterator ssrc =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001552 track->ssrc_groups[i].ssrcs.begin();
1553 for (; ssrc != track->ssrc_groups[i].ssrcs.end(); ++ssrc) {
Peter Boström0c4e06b2015-10-07 12:23:21 +02001554 os << kSdpDelimiterSpace << rtc::ToString<uint32_t>(*ssrc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001555 }
1556 AddLine(os.str(), message);
1557 }
1558 // Build the ssrc lines for each ssrc.
1559 for (size_t i = 0; i < track->ssrcs.size(); ++i) {
Peter Boström0c4e06b2015-10-07 12:23:21 +02001560 uint32_t ssrc = track->ssrcs[i];
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001561 // RFC 5576
1562 // a=ssrc:<ssrc-id> cname:<value>
1563 AddSsrcLine(ssrc, kSsrcAttributeCname,
1564 track->cname, message);
1565
Steve Antone831b8c2018-02-01 12:22:16 -08001566 if (msid_signaling & cricket::kMsidSignalingSsrcAttribute) {
1567 // draft-alvestrand-mmusic-msid-00
1568 // a=ssrc:<ssrc-id> msid:identifier [appdata]
1569 // The appdata consists of the "id" attribute of a MediaStreamTrack,
1570 // which corresponds to the "id" attribute of StreamParams.
Tomas Gunnarsson191bf5c2018-03-30 10:44:43 +00001571 // TODO(bugs.webrtc.org/7932): Support serializing more than one stream
1572 // label.
Seth Hampson845e8782018-03-02 11:34:10 -08001573 const std::string& stream_id = track->first_stream_id();
Steve Antone831b8c2018-02-01 12:22:16 -08001574 InitAttrLine(kAttributeSsrc, &os);
1575 os << kSdpDelimiterColon << ssrc << kSdpDelimiterSpace
1576 << kSsrcAttributeMsid << kSdpDelimiterColon << stream_id
1577 << kSdpDelimiterSpace << track->id;
1578 AddLine(os.str(), message);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001579
Steve Antone831b8c2018-02-01 12:22:16 -08001580 // TODO(ronghuawu): Remove below code which is for backward
1581 // compatibility.
1582 // draft-alvestrand-rtcweb-mid-01
1583 // a=ssrc:<ssrc-id> mslabel:<value>
1584 // The label isn't yet defined.
1585 // a=ssrc:<ssrc-id> label:<value>
Tomas Gunnarsson191bf5c2018-03-30 10:44:43 +00001586 AddSsrcLine(ssrc, kSsrcAttributeMslabel, track->first_stream_id(),
1587 message);
Steve Antone831b8c2018-02-01 12:22:16 -08001588 AddSsrcLine(ssrc, kSSrcAttributeLabel, track->id, message);
1589 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001590 }
1591 }
1592}
1593
1594void WriteFmtpHeader(int payload_type, std::ostringstream* os) {
1595 // fmtp header: a=fmtp:|payload_type| <parameters>
1596 // Add a=fmtp
1597 InitAttrLine(kAttributeFmtp, os);
1598 // Add :|payload_type|
1599 *os << kSdpDelimiterColon << payload_type;
1600}
1601
1602void WriteRtcpFbHeader(int payload_type, std::ostringstream* os) {
1603 // rtcp-fb header: a=rtcp-fb:|payload_type|
1604 // <parameters>/<ccm <ccm_parameters>>
1605 // Add a=rtcp-fb
1606 InitAttrLine(kAttributeRtcpFb, os);
1607 // Add :
1608 *os << kSdpDelimiterColon;
1609 if (payload_type == kWildcardPayloadType) {
1610 *os << "*";
1611 } else {
1612 *os << payload_type;
1613 }
1614}
1615
1616void WriteFmtpParameter(const std::string& parameter_name,
1617 const std::string& parameter_value,
1618 std::ostringstream* os) {
1619 // fmtp parameters: |parameter_name|=|parameter_value|
1620 *os << parameter_name << kSdpDelimiterEqual << parameter_value;
1621}
1622
1623void WriteFmtpParameters(const cricket::CodecParameterMap& parameters,
1624 std::ostringstream* os) {
1625 for (cricket::CodecParameterMap::const_iterator fmtp = parameters.begin();
1626 fmtp != parameters.end(); ++fmtp) {
hta62a216e2016-04-15 11:02:14 -07001627 // Parameters are a semicolon-separated list, no spaces.
1628 // The list is separated from the header by a space.
1629 if (fmtp == parameters.begin()) {
1630 *os << kSdpDelimiterSpace;
1631 } else {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001632 *os << kSdpDelimiterSemicolon;
1633 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001634 WriteFmtpParameter(fmtp->first, fmtp->second, os);
1635 }
1636}
1637
1638bool IsFmtpParam(const std::string& name) {
ossuaa4b0772017-01-30 07:41:18 -08001639 // RFC 4855, section 3 specifies the mapping of media format parameters to SDP
1640 // parameters. Only ptime, maxptime, channels and rate are placed outside of
1641 // the fmtp line. In WebRTC, channels and rate are already handled separately
1642 // and thus not included in the CodecParameterMap.
1643 return name != kCodecParamPTime && name != kCodecParamMaxPTime;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001644}
1645
1646// Retreives fmtp parameters from |params|, which may contain other parameters
1647// as well, and puts them in |fmtp_parameters|.
1648void GetFmtpParams(const cricket::CodecParameterMap& params,
1649 cricket::CodecParameterMap* fmtp_parameters) {
1650 for (cricket::CodecParameterMap::const_iterator iter = params.begin();
1651 iter != params.end(); ++iter) {
1652 if (IsFmtpParam(iter->first)) {
1653 (*fmtp_parameters)[iter->first] = iter->second;
1654 }
1655 }
1656}
1657
1658template <class T>
1659void AddFmtpLine(const T& codec, std::string* message) {
1660 cricket::CodecParameterMap fmtp_parameters;
1661 GetFmtpParams(codec.params, &fmtp_parameters);
1662 if (fmtp_parameters.empty()) {
1663 // No need to add an fmtp if it will have no (optional) parameters.
1664 return;
1665 }
1666 std::ostringstream os;
1667 WriteFmtpHeader(codec.id, &os);
1668 WriteFmtpParameters(fmtp_parameters, &os);
1669 AddLine(os.str(), message);
1670 return;
1671}
1672
1673template <class T>
1674void AddRtcpFbLines(const T& codec, std::string* message) {
1675 for (std::vector<cricket::FeedbackParam>::const_iterator iter =
1676 codec.feedback_params.params().begin();
1677 iter != codec.feedback_params.params().end(); ++iter) {
1678 std::ostringstream os;
1679 WriteRtcpFbHeader(codec.id, &os);
1680 os << " " << iter->id();
1681 if (!iter->param().empty()) {
1682 os << " " << iter->param();
1683 }
1684 AddLine(os.str(), message);
1685 }
1686}
1687
jiayl@webrtc.orgddb85ab2014-09-05 16:31:56 +00001688bool AddSctpDataCodec(DataContentDescription* media_desc,
1689 int sctp_port) {
solenberg9fa49752016-10-08 13:02:44 -07001690 for (const auto& codec : media_desc->codecs()) {
1691 if (cricket::CodecNamesEq(codec.name, cricket::kGoogleSctpDataCodecName)) {
1692 return ParseFailed("",
1693 "Can't have multiple sctp port attributes.",
1694 NULL);
1695 }
jiayl@webrtc.orgddb85ab2014-09-05 16:31:56 +00001696 }
1697 // Add the SCTP Port number as a pseudo-codec "port" parameter
solenberg9fa49752016-10-08 13:02:44 -07001698 cricket::DataCodec codec_port(cricket::kGoogleSctpDataCodecPlType,
deadbeef67cf2c12016-04-13 10:07:16 -07001699 cricket::kGoogleSctpDataCodecName);
jiayl@webrtc.orgddb85ab2014-09-05 16:31:56 +00001700 codec_port.SetParam(cricket::kCodecParamPort, sctp_port);
Mirko Bonadei675513b2017-11-09 11:09:25 +01001701 RTC_LOG(INFO) << "AddSctpDataCodec: Got SCTP Port Number " << sctp_port;
jiayl@webrtc.orgddb85ab2014-09-05 16:31:56 +00001702 media_desc->AddCodec(codec_port);
1703 return true;
1704}
1705
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001706bool GetMinValue(const std::vector<int>& values, int* value) {
1707 if (values.empty()) {
1708 return false;
1709 }
1710 std::vector<int>::const_iterator found =
1711 std::min_element(values.begin(), values.end());
1712 *value = *found;
1713 return true;
1714}
1715
1716bool GetParameter(const std::string& name,
1717 const cricket::CodecParameterMap& params, int* value) {
1718 std::map<std::string, std::string>::const_iterator found =
1719 params.find(name);
1720 if (found == params.end()) {
1721 return false;
1722 }
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001723 if (!rtc::FromString(found->second, value)) {
wu@webrtc.org5e760e72014-04-02 23:19:09 +00001724 return false;
1725 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001726 return true;
1727}
1728
1729void BuildRtpMap(const MediaContentDescription* media_desc,
1730 const MediaType media_type,
1731 std::string* message) {
nisseede5da42017-01-12 05:15:36 -08001732 RTC_DCHECK(message != NULL);
1733 RTC_DCHECK(media_desc != NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001734 std::ostringstream os;
1735 if (media_type == cricket::MEDIA_TYPE_VIDEO) {
Steve Antonb1c1de12017-12-21 15:14:30 -08001736 const VideoContentDescription* video_desc = media_desc->as_video();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001737 for (std::vector<cricket::VideoCodec>::const_iterator it =
1738 video_desc->codecs().begin();
1739 it != video_desc->codecs().end(); ++it) {
1740 // RFC 4566
1741 // a=rtpmap:<payload type> <encoding name>/<clock rate>
1742 // [/<encodingparameters>]
1743 if (it->id != kWildcardPayloadType) {
1744 InitAttrLine(kAttributeRtpmap, &os);
deadbeefe814a0d2017-02-25 18:15:09 -08001745 os << kSdpDelimiterColon << it->id << " " << it->name << "/"
1746 << cricket::kVideoCodecClockrate;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001747 AddLine(os.str(), message);
1748 }
1749 AddRtcpFbLines(*it, message);
1750 AddFmtpLine(*it, message);
1751 }
1752 } else if (media_type == cricket::MEDIA_TYPE_AUDIO) {
Steve Antonb1c1de12017-12-21 15:14:30 -08001753 const AudioContentDescription* audio_desc = media_desc->as_audio();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001754 std::vector<int> ptimes;
1755 std::vector<int> maxptimes;
1756 int max_minptime = 0;
1757 for (std::vector<cricket::AudioCodec>::const_iterator it =
1758 audio_desc->codecs().begin();
1759 it != audio_desc->codecs().end(); ++it) {
nisseede5da42017-01-12 05:15:36 -08001760 RTC_DCHECK(!it->name.empty());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001761 // RFC 4566
1762 // a=rtpmap:<payload type> <encoding name>/<clock rate>
1763 // [/<encodingparameters>]
1764 InitAttrLine(kAttributeRtpmap, &os);
1765 os << kSdpDelimiterColon << it->id << " ";
1766 os << it->name << "/" << it->clockrate;
1767 if (it->channels != 1) {
1768 os << "/" << it->channels;
1769 }
1770 AddLine(os.str(), message);
1771 AddRtcpFbLines(*it, message);
1772 AddFmtpLine(*it, message);
1773 int minptime = 0;
1774 if (GetParameter(kCodecParamMinPTime, it->params, &minptime)) {
1775 max_minptime = std::max(minptime, max_minptime);
1776 }
1777 int ptime;
1778 if (GetParameter(kCodecParamPTime, it->params, &ptime)) {
1779 ptimes.push_back(ptime);
1780 }
1781 int maxptime;
1782 if (GetParameter(kCodecParamMaxPTime, it->params, &maxptime)) {
1783 maxptimes.push_back(maxptime);
1784 }
1785 }
1786 // Populate the maxptime attribute with the smallest maxptime of all codecs
1787 // under the same m-line.
1788 int min_maxptime = INT_MAX;
1789 if (GetMinValue(maxptimes, &min_maxptime)) {
1790 AddAttributeLine(kCodecParamMaxPTime, min_maxptime, message);
1791 }
nisseede5da42017-01-12 05:15:36 -08001792 RTC_DCHECK(min_maxptime > max_minptime);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001793 // Populate the ptime attribute with the smallest ptime or the largest
1794 // minptime, whichever is the largest, for all codecs under the same m-line.
1795 int ptime = INT_MAX;
1796 if (GetMinValue(ptimes, &ptime)) {
1797 ptime = std::min(ptime, min_maxptime);
1798 ptime = std::max(ptime, max_minptime);
1799 AddAttributeLine(kCodecParamPTime, ptime, message);
1800 }
1801 } else if (media_type == cricket::MEDIA_TYPE_DATA) {
Steve Antonb1c1de12017-12-21 15:14:30 -08001802 const DataContentDescription* data_desc = media_desc->as_data();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001803 for (std::vector<cricket::DataCodec>::const_iterator it =
1804 data_desc->codecs().begin();
1805 it != data_desc->codecs().end(); ++it) {
1806 // RFC 4566
1807 // a=rtpmap:<payload type> <encoding name>/<clock rate>
1808 // [/<encodingparameters>]
1809 InitAttrLine(kAttributeRtpmap, &os);
1810 os << kSdpDelimiterColon << it->id << " "
1811 << it->name << "/" << it->clockrate;
1812 AddLine(os.str(), message);
1813 }
1814 }
1815}
1816
1817void BuildCandidate(const std::vector<Candidate>& candidates,
honghaiza54a0802015-12-16 18:37:23 -08001818 bool include_ufrag,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001819 std::string* message) {
1820 std::ostringstream os;
1821
1822 for (std::vector<Candidate>::const_iterator it = candidates.begin();
1823 it != candidates.end(); ++it) {
1824 // RFC 5245
1825 // a=candidate:<foundation> <component-id> <transport> <priority>
1826 // <connection-address> <port> typ <candidate-types>
1827 // [raddr <connection-address>] [rport <port>]
1828 // *(SP extension-att-name SP extension-att-value)
1829 std::string type;
1830 // Map the cricket candidate type to "host" / "srflx" / "prflx" / "relay"
1831 if (it->type() == cricket::LOCAL_PORT_TYPE) {
1832 type = kCandidateHost;
1833 } else if (it->type() == cricket::STUN_PORT_TYPE) {
1834 type = kCandidateSrflx;
1835 } else if (it->type() == cricket::RELAY_PORT_TYPE) {
1836 type = kCandidateRelay;
Honghai Zhang7fb69db2016-03-14 11:59:18 -07001837 } else if (it->type() == cricket::PRFLX_PORT_TYPE) {
1838 type = kCandidatePrflx;
1839 // Peer reflexive candidate may be signaled for being removed.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001840 } else {
nissec80e7412017-01-11 05:56:46 -08001841 RTC_NOTREACHED();
Peter Thatcher019087f2015-04-28 09:06:26 -07001842 // Never write out candidates if we don't know the type.
1843 continue;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001844 }
1845
1846 InitAttrLine(kAttributeCandidate, &os);
1847 os << kSdpDelimiterColon
mallinath@webrtc.org2d60c5e2014-08-08 22:29:20 +00001848 << it->foundation() << " "
1849 << it->component() << " "
1850 << it->protocol() << " "
1851 << it->priority() << " "
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001852 << it->address().ipaddr().ToString() << " "
1853 << it->address().PortAsString() << " "
mallinath@webrtc.org2d60c5e2014-08-08 22:29:20 +00001854 << kAttributeCandidateTyp << " "
1855 << type << " ";
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001856
1857 // Related address
1858 if (!it->related_address().IsNil()) {
1859 os << kAttributeCandidateRaddr << " "
1860 << it->related_address().ipaddr().ToString() << " "
1861 << kAttributeCandidateRport << " "
1862 << it->related_address().PortAsString() << " ";
1863 }
1864
mallinath@webrtc.org2d60c5e2014-08-08 22:29:20 +00001865 if (it->protocol() == cricket::TCP_PROTOCOL_NAME) {
mallinath@webrtc.orge999bd02014-08-13 06:05:55 +00001866 os << kTcpCandidateType << " " << it->tcptype() << " ";
mallinath@webrtc.org2d60c5e2014-08-08 22:29:20 +00001867 }
1868
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001869 // Extensions
1870 os << kAttributeCandidateGeneration << " " << it->generation();
honghaiza54a0802015-12-16 18:37:23 -08001871 if (include_ufrag && !it->username().empty()) {
1872 os << " " << kAttributeCandidateUfrag << " " << it->username();
1873 }
honghaiza0c44ea2016-03-23 16:07:48 -07001874 if (it->network_id() > 0) {
1875 os << " " << kAttributeCandidateNetworkId << " " << it->network_id();
1876 }
honghaize1a0c942016-02-16 14:54:56 -08001877 if (it->network_cost() > 0) {
1878 os << " " << kAttributeCandidateNetworkCost << " " << it->network_cost();
1879 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001880
1881 AddLine(os.str(), message);
1882 }
1883}
1884
1885void BuildIceOptions(const std::vector<std::string>& transport_options,
1886 std::string* message) {
1887 if (!transport_options.empty()) {
1888 std::ostringstream os;
1889 InitAttrLine(kAttributeIceOption, &os);
1890 os << kSdpDelimiterColon << transport_options[0];
1891 for (size_t i = 1; i < transport_options.size(); ++i) {
1892 os << kSdpDelimiterSpace << transport_options[i];
1893 }
1894 AddLine(os.str(), message);
1895 }
1896}
1897
pthatcher@webrtc.org3341b402015-02-13 21:14:22 +00001898bool IsRtp(const std::string& protocol) {
1899 return protocol.empty() ||
1900 (protocol.find(cricket::kMediaProtocolRtpPrefix) != std::string::npos);
1901}
1902
1903bool IsDtlsSctp(const std::string& protocol) {
1904 // This intentionally excludes "SCTP" and "SCTP/DTLS".
lally@webrtc.orga7470932015-02-24 20:19:21 +00001905 return protocol.find(cricket::kMediaProtocolDtlsSctp) != std::string::npos;
pthatcher@webrtc.org3341b402015-02-13 21:14:22 +00001906}
1907
zhihuang38989e52017-03-21 11:04:53 -07001908bool ParseConnectionData(const std::string& line,
1909 rtc::SocketAddress* addr,
1910 SdpParseError* error) {
1911 // Parse the line from left to right.
1912 std::string token;
1913 std::string rightpart;
1914 // RFC 4566
1915 // c=<nettype> <addrtype> <connection-address>
1916 // Skip the "c="
1917 if (!rtc::tokenize_first(line, kSdpDelimiterEqual, &token, &rightpart)) {
1918 return ParseFailed(line, "Failed to parse the network type.", error);
1919 }
1920
1921 // Extract and verify the <nettype>
1922 if (!rtc::tokenize_first(rightpart, kSdpDelimiterSpace, &token, &rightpart) ||
1923 token != kConnectionNettype) {
1924 return ParseFailed(line,
1925 "Failed to parse the connection data. The network type "
1926 "is not currently supported.",
1927 error);
1928 }
1929
1930 // Extract the "<addrtype>" and "<connection-address>".
1931 if (!rtc::tokenize_first(rightpart, kSdpDelimiterSpace, &token, &rightpart)) {
1932 return ParseFailed(line, "Failed to parse the address type.", error);
1933 }
1934
1935 // The rightpart part should be the IP address without the slash which is used
1936 // for multicast.
1937 if (rightpart.find('/') != std::string::npos) {
1938 return ParseFailed(line,
1939 "Failed to parse the connection data. Multicast is not "
1940 "currently supported.",
1941 error);
1942 }
1943 addr->SetIP(rightpart);
1944
1945 // Verify that the addrtype matches the type of the parsed address.
1946 if ((addr->family() == AF_INET && token != "IP4") ||
1947 (addr->family() == AF_INET6 && token != "IP6")) {
1948 addr->Clear();
1949 return ParseFailed(
1950 line,
1951 "Failed to parse the connection data. The address type is mismatching.",
1952 error);
1953 }
1954 return true;
1955}
1956
1957bool ParseSessionDescription(const std::string& message,
1958 size_t* pos,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001959 std::string* session_id,
1960 std::string* session_version,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001961 TransportDescription* session_td,
1962 RtpHeaderExtensions* session_extmaps,
zhihuang38989e52017-03-21 11:04:53 -07001963 rtc::SocketAddress* connection_addr,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001964 cricket::SessionDescription* desc,
1965 SdpParseError* error) {
1966 std::string line;
1967
deadbeefc80741f2015-10-22 13:14:45 -07001968 desc->set_msid_supported(false);
1969
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001970 // RFC 4566
1971 // v= (protocol version)
1972 if (!GetLineWithType(message, pos, &line, kLineTypeVersion)) {
1973 return ParseFailedExpectLine(message, *pos, kLineTypeVersion,
1974 std::string(), error);
1975 }
1976 // RFC 4566
1977 // o=<username> <sess-id> <sess-version> <nettype> <addrtype>
1978 // <unicast-address>
1979 if (!GetLineWithType(message, pos, &line, kLineTypeOrigin)) {
1980 return ParseFailedExpectLine(message, *pos, kLineTypeOrigin,
1981 std::string(), error);
1982 }
1983 std::vector<std::string> fields;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001984 rtc::split(line.substr(kLinePrefixLength),
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001985 kSdpDelimiterSpace, &fields);
1986 const size_t expected_fields = 6;
1987 if (fields.size() != expected_fields) {
1988 return ParseFailedExpectFieldNum(line, expected_fields, error);
1989 }
1990 *session_id = fields[1];
1991 *session_version = fields[2];
1992
1993 // RFC 4566
1994 // s= (session name)
1995 if (!GetLineWithType(message, pos, &line, kLineTypeSessionName)) {
1996 return ParseFailedExpectLine(message, *pos, kLineTypeSessionName,
1997 std::string(), error);
1998 }
1999
2000 // Optional lines
2001 // Those are the optional lines, so shouldn't return false if not present.
2002 // RFC 4566
2003 // i=* (session information)
2004 GetLineWithType(message, pos, &line, kLineTypeSessionInfo);
2005
2006 // RFC 4566
2007 // u=* (URI of description)
2008 GetLineWithType(message, pos, &line, kLineTypeSessionUri);
2009
2010 // RFC 4566
2011 // e=* (email address)
2012 GetLineWithType(message, pos, &line, kLineTypeSessionEmail);
2013
2014 // RFC 4566
2015 // p=* (phone number)
2016 GetLineWithType(message, pos, &line, kLineTypeSessionPhone);
2017
2018 // RFC 4566
2019 // c=* (connection information -- not required if included in
2020 // all media)
zhihuang38989e52017-03-21 11:04:53 -07002021 if (GetLineWithType(message, pos, &line, kLineTypeConnection)) {
2022 if (!ParseConnectionData(line, connection_addr, error)) {
2023 return false;
2024 }
2025 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002026
2027 // RFC 4566
2028 // b=* (zero or more bandwidth information lines)
2029 while (GetLineWithType(message, pos, &line, kLineTypeSessionBandwidth)) {
2030 // By pass zero or more b lines.
2031 }
2032
2033 // RFC 4566
2034 // One or more time descriptions ("t=" and "r=" lines; see below)
2035 // t= (time the session is active)
2036 // r=* (zero or more repeat times)
2037 // Ensure there's at least one time description
2038 if (!GetLineWithType(message, pos, &line, kLineTypeTiming)) {
2039 return ParseFailedExpectLine(message, *pos, kLineTypeTiming, std::string(),
2040 error);
2041 }
2042
2043 while (GetLineWithType(message, pos, &line, kLineTypeRepeatTimes)) {
2044 // By pass zero or more r lines.
2045 }
2046
2047 // Go through the rest of the time descriptions
2048 while (GetLineWithType(message, pos, &line, kLineTypeTiming)) {
2049 while (GetLineWithType(message, pos, &line, kLineTypeRepeatTimes)) {
2050 // By pass zero or more r lines.
2051 }
2052 }
2053
2054 // RFC 4566
2055 // z=* (time zone adjustments)
2056 GetLineWithType(message, pos, &line, kLineTypeTimeZone);
2057
2058 // RFC 4566
2059 // k=* (encryption key)
2060 GetLineWithType(message, pos, &line, kLineTypeEncryptionKey);
2061
2062 // RFC 4566
2063 // a=* (zero or more session attribute lines)
2064 while (GetLineWithType(message, pos, &line, kLineTypeAttributes)) {
2065 if (HasAttribute(line, kAttributeGroup)) {
2066 if (!ParseGroupAttribute(line, desc, error)) {
2067 return false;
2068 }
2069 } else if (HasAttribute(line, kAttributeIceUfrag)) {
2070 if (!GetValue(line, kAttributeIceUfrag,
2071 &(session_td->ice_ufrag), error)) {
2072 return false;
2073 }
2074 } else if (HasAttribute(line, kAttributeIcePwd)) {
2075 if (!GetValue(line, kAttributeIcePwd, &(session_td->ice_pwd), error)) {
2076 return false;
2077 }
2078 } else if (HasAttribute(line, kAttributeIceLite)) {
2079 session_td->ice_mode = cricket::ICEMODE_LITE;
2080 } else if (HasAttribute(line, kAttributeIceOption)) {
2081 if (!ParseIceOptions(line, &(session_td->transport_options), error)) {
2082 return false;
2083 }
2084 } else if (HasAttribute(line, kAttributeFingerprint)) {
2085 if (session_td->identity_fingerprint.get()) {
2086 return ParseFailed(
2087 line,
2088 "Can't have multiple fingerprint attributes at the same level.",
2089 error);
2090 }
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002091 rtc::SSLFingerprint* fingerprint = NULL;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002092 if (!ParseFingerprintAttribute(line, &fingerprint, error)) {
2093 return false;
2094 }
2095 session_td->identity_fingerprint.reset(fingerprint);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00002096 } else if (HasAttribute(line, kAttributeSetup)) {
2097 if (!ParseDtlsSetup(line, &(session_td->connection_role), error)) {
2098 return false;
2099 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002100 } else if (HasAttribute(line, kAttributeMsidSemantics)) {
2101 std::string semantics;
2102 if (!GetValue(line, kAttributeMsidSemantics, &semantics, error)) {
2103 return false;
2104 }
deadbeefc80741f2015-10-22 13:14:45 -07002105 desc->set_msid_supported(
2106 CaseInsensitiveFind(semantics, kMediaStreamSemantic));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002107 } else if (HasAttribute(line, kAttributeExtmap)) {
isheriff6f8d6862016-05-26 11:24:55 -07002108 RtpExtension extmap;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002109 if (!ParseExtmap(line, &extmap, error)) {
2110 return false;
2111 }
2112 session_extmaps->push_back(extmap);
2113 }
2114 }
2115
2116 return true;
2117}
2118
2119bool ParseGroupAttribute(const std::string& line,
2120 cricket::SessionDescription* desc,
2121 SdpParseError* error) {
nisseede5da42017-01-12 05:15:36 -08002122 RTC_DCHECK(desc != NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002123
2124 // RFC 5888 and draft-holmberg-mmusic-sdp-bundle-negotiation-00
2125 // a=group:BUNDLE video voice
2126 std::vector<std::string> fields;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002127 rtc::split(line.substr(kLinePrefixLength),
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002128 kSdpDelimiterSpace, &fields);
2129 std::string semantics;
2130 if (!GetValue(fields[0], kAttributeGroup, &semantics, error)) {
2131 return false;
2132 }
2133 cricket::ContentGroup group(semantics);
2134 for (size_t i = 1; i < fields.size(); ++i) {
2135 group.AddContentName(fields[i]);
2136 }
2137 desc->AddGroup(group);
2138 return true;
2139}
2140
2141static bool ParseFingerprintAttribute(const std::string& line,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002142 rtc::SSLFingerprint** fingerprint,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002143 SdpParseError* error) {
2144 if (!IsLineType(line, kLineTypeAttributes) ||
2145 !HasAttribute(line, kAttributeFingerprint)) {
2146 return ParseFailedExpectLine(line, 0, kLineTypeAttributes,
2147 kAttributeFingerprint, error);
2148 }
2149
2150 std::vector<std::string> fields;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002151 rtc::split(line.substr(kLinePrefixLength),
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002152 kSdpDelimiterSpace, &fields);
2153 const size_t expected_fields = 2;
2154 if (fields.size() != expected_fields) {
2155 return ParseFailedExpectFieldNum(line, expected_fields, error);
2156 }
2157
2158 // The first field here is "fingerprint:<hash>.
2159 std::string algorithm;
2160 if (!GetValue(fields[0], kAttributeFingerprint, &algorithm, error)) {
2161 return false;
2162 }
2163
2164 // Downcase the algorithm. Note that we don't need to downcase the
2165 // fingerprint because hex_decode can handle upper-case.
2166 std::transform(algorithm.begin(), algorithm.end(), algorithm.begin(),
2167 ::tolower);
2168
2169 // The second field is the digest value. De-hexify it.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002170 *fingerprint = rtc::SSLFingerprint::CreateFromRfc4572(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002171 algorithm, fields[1]);
2172 if (!*fingerprint) {
2173 return ParseFailed(line,
2174 "Failed to create fingerprint from the digest.",
2175 error);
2176 }
2177
2178 return true;
2179}
2180
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00002181static bool ParseDtlsSetup(const std::string& line,
2182 cricket::ConnectionRole* role,
2183 SdpParseError* error) {
2184 // setup-attr = "a=setup:" role
2185 // role = "active" / "passive" / "actpass" / "holdconn"
2186 std::vector<std::string> fields;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002187 rtc::split(line.substr(kLinePrefixLength), kSdpDelimiterColon, &fields);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00002188 const size_t expected_fields = 2;
2189 if (fields.size() != expected_fields) {
2190 return ParseFailedExpectFieldNum(line, expected_fields, error);
2191 }
2192 std::string role_str = fields[1];
2193 if (!cricket::StringToConnectionRole(role_str, role)) {
2194 return ParseFailed(line, "Invalid attribute value.", error);
2195 }
2196 return true;
2197}
2198
deadbeef9d3584c2016-02-16 17:54:10 -08002199static bool ParseMsidAttribute(const std::string& line,
Tomas Gunnarsson191bf5c2018-03-30 10:44:43 +00002200 std::string* stream_id,
deadbeef9d3584c2016-02-16 17:54:10 -08002201 std::string* track_id,
2202 SdpParseError* error) {
Tomas Gunnarsson191bf5c2018-03-30 10:44:43 +00002203 // draft-ietf-mmusic-msid-11
deadbeef9d3584c2016-02-16 17:54:10 -08002204 // a=msid:<stream id> <track id>
2205 // msid-value = msid-id [ SP msid-appdata ]
2206 // msid-id = 1*64token-char ; see RFC 4566
2207 // msid-appdata = 1*64token-char ; see RFC 4566
2208 std::string field1;
2209 if (!rtc::tokenize_first(line.substr(kLinePrefixLength), kSdpDelimiterSpace,
Tomas Gunnarsson191bf5c2018-03-30 10:44:43 +00002210 &field1, track_id)) {
deadbeef9d3584c2016-02-16 17:54:10 -08002211 const size_t expected_fields = 2;
2212 return ParseFailedExpectFieldNum(line, expected_fields, error);
2213 }
2214
Tomas Gunnarsson191bf5c2018-03-30 10:44:43 +00002215 if (track_id->empty()) {
deadbeefa4549d62017-02-10 17:26:22 -08002216 return ParseFailed(line, "Missing track ID in msid attribute.", error);
2217 }
2218
deadbeef9d3584c2016-02-16 17:54:10 -08002219 // msid:<msid-id>
Tomas Gunnarsson191bf5c2018-03-30 10:44:43 +00002220 if (!GetValue(field1, kAttributeMsid, stream_id, error)) {
deadbeef9d3584c2016-02-16 17:54:10 -08002221 return false;
2222 }
Tomas Gunnarsson191bf5c2018-03-30 10:44:43 +00002223 if (stream_id->empty()) {
deadbeefa4549d62017-02-10 17:26:22 -08002224 return ParseFailed(line, "Missing stream ID in msid attribute.", error);
2225 }
deadbeef9d3584c2016-02-16 17:54:10 -08002226 return true;
2227}
2228
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002229// RFC 3551
2230// PT encoding media type clock rate channels
2231// name (Hz)
2232// 0 PCMU A 8,000 1
2233// 1 reserved A
2234// 2 reserved A
2235// 3 GSM A 8,000 1
2236// 4 G723 A 8,000 1
2237// 5 DVI4 A 8,000 1
2238// 6 DVI4 A 16,000 1
2239// 7 LPC A 8,000 1
2240// 8 PCMA A 8,000 1
2241// 9 G722 A 8,000 1
2242// 10 L16 A 44,100 2
2243// 11 L16 A 44,100 1
2244// 12 QCELP A 8,000 1
2245// 13 CN A 8,000 1
2246// 14 MPA A 90,000 (see text)
2247// 15 G728 A 8,000 1
2248// 16 DVI4 A 11,025 1
2249// 17 DVI4 A 22,050 1
2250// 18 G729 A 8,000 1
2251struct StaticPayloadAudioCodec {
2252 const char* name;
2253 int clockrate;
Peter Kasting69558702016-01-12 16:26:35 -08002254 size_t channels;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002255};
2256static const StaticPayloadAudioCodec kStaticPayloadAudioCodecs[] = {
2257 { "PCMU", 8000, 1 },
2258 { "reserved", 0, 0 },
2259 { "reserved", 0, 0 },
2260 { "GSM", 8000, 1 },
2261 { "G723", 8000, 1 },
2262 { "DVI4", 8000, 1 },
2263 { "DVI4", 16000, 1 },
2264 { "LPC", 8000, 1 },
2265 { "PCMA", 8000, 1 },
2266 { "G722", 8000, 1 },
2267 { "L16", 44100, 2 },
2268 { "L16", 44100, 1 },
2269 { "QCELP", 8000, 1 },
2270 { "CN", 8000, 1 },
2271 { "MPA", 90000, 1 },
2272 { "G728", 8000, 1 },
2273 { "DVI4", 11025, 1 },
2274 { "DVI4", 22050, 1 },
2275 { "G729", 8000, 1 },
2276};
2277
2278void MaybeCreateStaticPayloadAudioCodecs(
2279 const std::vector<int>& fmts, AudioContentDescription* media_desc) {
2280 if (!media_desc) {
2281 return;
2282 }
deadbeef67cf2c12016-04-13 10:07:16 -07002283 RTC_DCHECK(media_desc->codecs().empty());
solenberg9fa49752016-10-08 13:02:44 -07002284 for (int payload_type : fmts) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002285 if (!media_desc->HasCodec(payload_type) &&
2286 payload_type >= 0 &&
kjellander3e33bfe2016-06-20 07:04:09 -07002287 static_cast<uint32_t>(payload_type) <
2288 arraysize(kStaticPayloadAudioCodecs)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002289 std::string encoding_name = kStaticPayloadAudioCodecs[payload_type].name;
2290 int clock_rate = kStaticPayloadAudioCodecs[payload_type].clockrate;
Peter Kasting69558702016-01-12 16:26:35 -08002291 size_t channels = kStaticPayloadAudioCodecs[payload_type].channels;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002292 media_desc->AddCodec(cricket::AudioCodec(payload_type, encoding_name,
deadbeef67cf2c12016-04-13 10:07:16 -07002293 clock_rate, 0, channels));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002294 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002295 }
2296}
2297
2298template <class C>
2299static C* ParseContentDescription(const std::string& message,
2300 const MediaType media_type,
2301 int mline_index,
2302 const std::string& protocol,
deadbeef67cf2c12016-04-13 10:07:16 -07002303 const std::vector<int>& payload_types,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002304 size_t* pos,
2305 std::string* content_name,
deadbeef25ed4352016-12-12 18:37:36 -08002306 bool* bundle_only,
Steve Antone831b8c2018-02-01 12:22:16 -08002307 int* msid_signaling,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002308 TransportDescription* transport,
2309 std::vector<JsepIceCandidate*>* candidates,
2310 webrtc::SdpParseError* error) {
2311 C* media_desc = new C();
2312 switch (media_type) {
2313 case cricket::MEDIA_TYPE_AUDIO:
2314 *content_name = cricket::CN_AUDIO;
2315 break;
2316 case cricket::MEDIA_TYPE_VIDEO:
2317 *content_name = cricket::CN_VIDEO;
2318 break;
2319 case cricket::MEDIA_TYPE_DATA:
2320 *content_name = cricket::CN_DATA;
2321 break;
2322 default:
nissec80e7412017-01-11 05:56:46 -08002323 RTC_NOTREACHED();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002324 break;
2325 }
deadbeef67cf2c12016-04-13 10:07:16 -07002326 if (!ParseContent(message, media_type, mline_index, protocol, payload_types,
Steve Antone831b8c2018-02-01 12:22:16 -08002327 pos, content_name, bundle_only, msid_signaling, media_desc,
2328 transport, candidates, error)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002329 delete media_desc;
zhihuang38989e52017-03-21 11:04:53 -07002330 return nullptr;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002331 }
2332 // Sort the codecs according to the m-line fmt list.
deadbeef67cf2c12016-04-13 10:07:16 -07002333 std::unordered_map<int, int> payload_type_preferences;
2334 // "size + 1" so that the lowest preference payload type has a preference of
2335 // 1, which is greater than the default (0) for payload types not in the fmt
2336 // list.
2337 int preference = static_cast<int>(payload_types.size() + 1);
2338 for (int pt : payload_types) {
2339 payload_type_preferences[pt] = preference--;
2340 }
2341 std::vector<typename C::CodecType> codecs = media_desc->codecs();
2342 std::sort(codecs.begin(), codecs.end(), [&payload_type_preferences](
2343 const typename C::CodecType& a,
2344 const typename C::CodecType& b) {
2345 return payload_type_preferences[a.id] > payload_type_preferences[b.id];
2346 });
2347 media_desc->set_codecs(codecs);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002348 return media_desc;
2349}
2350
2351bool ParseMediaDescription(const std::string& message,
2352 const TransportDescription& session_td,
2353 const RtpHeaderExtensions& session_extmaps,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002354 size_t* pos,
zhihuang38989e52017-03-21 11:04:53 -07002355 const rtc::SocketAddress& session_connection_addr,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002356 cricket::SessionDescription* desc,
2357 std::vector<JsepIceCandidate*>* candidates,
2358 SdpParseError* error) {
nisseede5da42017-01-12 05:15:36 -08002359 RTC_DCHECK(desc != NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002360 std::string line;
2361 int mline_index = -1;
Steve Antone831b8c2018-02-01 12:22:16 -08002362 int msid_signaling = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002363
2364 // Zero or more media descriptions
2365 // RFC 4566
2366 // m=<media> <port> <proto> <fmt>
2367 while (GetLineWithType(message, pos, &line, kLineTypeMedia)) {
2368 ++mline_index;
2369
2370 std::vector<std::string> fields;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002371 rtc::split(line.substr(kLinePrefixLength),
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002372 kSdpDelimiterSpace, &fields);
zstein4b2e0822017-02-17 19:48:38 -08002373
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002374 const size_t expected_min_fields = 4;
2375 if (fields.size() < expected_min_fields) {
2376 return ParseFailedExpectMinFieldNum(line, expected_min_fields, error);
2377 }
deadbeef25ed4352016-12-12 18:37:36 -08002378 bool port_rejected = false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002379 // RFC 3264
2380 // To reject an offered stream, the port number in the corresponding stream
2381 // in the answer MUST be set to zero.
2382 if (fields[1] == kMediaPortRejected) {
deadbeef25ed4352016-12-12 18:37:36 -08002383 port_rejected = true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002384 }
2385
zhihuang38989e52017-03-21 11:04:53 -07002386 int port = 0;
2387 if (!rtc::FromString<int>(fields[1], &port) || !IsValidPort(port)) {
2388 return ParseFailed(line, "The port number is invalid", error);
2389 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002390 std::string protocol = fields[2];
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002391
2392 // <fmt>
deadbeef67cf2c12016-04-13 10:07:16 -07002393 std::vector<int> payload_types;
pthatcher@webrtc.org3341b402015-02-13 21:14:22 +00002394 if (IsRtp(protocol)) {
jiayl@webrtc.orgddb85ab2014-09-05 16:31:56 +00002395 for (size_t j = 3 ; j < fields.size(); ++j) {
2396 // TODO(wu): Remove when below bug is fixed.
2397 // https://bugzilla.mozilla.org/show_bug.cgi?id=996329
pbosbb36fdf2015-07-09 07:48:14 -07002398 if (fields[j].empty() && j == fields.size() - 1) {
jiayl@webrtc.orgddb85ab2014-09-05 16:31:56 +00002399 continue;
2400 }
wu@webrtc.org36eda7c2014-04-15 20:37:30 +00002401
jiayl@webrtc.orgddb85ab2014-09-05 16:31:56 +00002402 int pl = 0;
pkasting@chromium.orge9facf82015-02-17 20:36:28 +00002403 if (!GetPayloadTypeFromString(line, fields[j], &pl, error)) {
jiayl@webrtc.orgddb85ab2014-09-05 16:31:56 +00002404 return false;
2405 }
deadbeef67cf2c12016-04-13 10:07:16 -07002406 payload_types.push_back(pl);
wu@webrtc.org5e760e72014-04-02 23:19:09 +00002407 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002408 }
2409
2410 // Make a temporary TransportDescription based on |session_td|.
2411 // Some of this gets overwritten by ParseContent.
deadbeef46eed762016-01-28 13:24:37 -08002412 TransportDescription transport(
2413 session_td.transport_options, session_td.ice_ufrag, session_td.ice_pwd,
2414 session_td.ice_mode, session_td.connection_role,
2415 session_td.identity_fingerprint.get());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002416
kwibergd1fe2812016-04-27 06:47:29 -07002417 std::unique_ptr<MediaContentDescription> content;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002418 std::string content_name;
deadbeef25ed4352016-12-12 18:37:36 -08002419 bool bundle_only = false;
Steve Antone831b8c2018-02-01 12:22:16 -08002420 int section_msid_signaling = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002421 if (HasAttribute(line, kMediaTypeVideo)) {
2422 content.reset(ParseContentDescription<VideoContentDescription>(
deadbeef67cf2c12016-04-13 10:07:16 -07002423 message, cricket::MEDIA_TYPE_VIDEO, mline_index, protocol,
Steve Antone831b8c2018-02-01 12:22:16 -08002424 payload_types, pos, &content_name, &bundle_only,
2425 &section_msid_signaling, &transport, candidates, error));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002426 } else if (HasAttribute(line, kMediaTypeAudio)) {
2427 content.reset(ParseContentDescription<AudioContentDescription>(
deadbeef67cf2c12016-04-13 10:07:16 -07002428 message, cricket::MEDIA_TYPE_AUDIO, mline_index, protocol,
Steve Antone831b8c2018-02-01 12:22:16 -08002429 payload_types, pos, &content_name, &bundle_only,
2430 &section_msid_signaling, &transport, candidates, error));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002431 } else if (HasAttribute(line, kMediaTypeData)) {
jiayl@webrtc.org0e527722014-09-08 21:43:43 +00002432 DataContentDescription* data_desc =
wu@webrtc.org78187522013-10-07 23:32:02 +00002433 ParseContentDescription<DataContentDescription>(
deadbeef67cf2c12016-04-13 10:07:16 -07002434 message, cricket::MEDIA_TYPE_DATA, mline_index, protocol,
Steve Antone831b8c2018-02-01 12:22:16 -08002435 payload_types, pos, &content_name, &bundle_only,
2436 &section_msid_signaling, &transport, candidates, error);
jiayl@webrtc.org0e527722014-09-08 21:43:43 +00002437 content.reset(data_desc);
wu@webrtc.org78187522013-10-07 23:32:02 +00002438
zstein4b2e0822017-02-17 19:48:38 -08002439 if (data_desc && IsDtlsSctp(protocol)) {
2440 int p;
2441 if (rtc::FromString(fields[3], &p)) {
2442 if (!AddSctpDataCodec(data_desc, p)) {
2443 return false;
2444 }
2445 } else if (fields[3] == kDefaultSctpmapProtocol) {
2446 data_desc->set_use_sctpmap(false);
2447 }
wu@webrtc.org78187522013-10-07 23:32:02 +00002448 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002449 } else {
Mirko Bonadei675513b2017-11-09 11:09:25 +01002450 RTC_LOG(LS_WARNING) << "Unsupported media type: " << line;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002451 continue;
2452 }
2453 if (!content.get()) {
2454 // ParseContentDescription returns NULL if failed.
2455 return false;
2456 }
2457
Steve Antone831b8c2018-02-01 12:22:16 -08002458 msid_signaling |= section_msid_signaling;
2459
deadbeef25ed4352016-12-12 18:37:36 -08002460 bool content_rejected = false;
deadbeef12771a12017-01-03 13:53:47 -08002461 // A port of 0 is not interpreted as a rejected m= section when it's
2462 // used along with a=bundle-only.
deadbeef25ed4352016-12-12 18:37:36 -08002463 if (bundle_only) {
deadbeef25ed4352016-12-12 18:37:36 -08002464 if (!port_rejected) {
deadbeef12771a12017-01-03 13:53:47 -08002465 // Usage of bundle-only with a nonzero port is unspecified. So just
2466 // ignore bundle-only if we see this.
2467 bundle_only = false;
Mirko Bonadei675513b2017-11-09 11:09:25 +01002468 RTC_LOG(LS_WARNING)
deadbeef12771a12017-01-03 13:53:47 -08002469 << "a=bundle-only attribute observed with a nonzero "
Jonas Olsson45cc8902018-02-13 10:37:07 +01002470 "port; this usage is unspecified so the attribute is being "
2471 "ignored.";
deadbeef25ed4352016-12-12 18:37:36 -08002472 }
2473 } else {
2474 // If not using bundle-only, interpret port 0 in the normal way; the m=
2475 // section is being rejected.
2476 content_rejected = port_rejected;
2477 }
2478
pthatcher@webrtc.org3341b402015-02-13 21:14:22 +00002479 if (IsRtp(protocol)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002480 // Set the extmap.
2481 if (!session_extmaps.empty() &&
2482 !content->rtp_header_extensions().empty()) {
2483 return ParseFailed("",
2484 "The a=extmap MUST be either all session level or "
2485 "all media level.",
2486 error);
2487 }
2488 for (size_t i = 0; i < session_extmaps.size(); ++i) {
2489 content->AddRtpHeaderExtension(session_extmaps[i]);
2490 }
2491 }
2492 content->set_protocol(protocol);
zhihuang38989e52017-03-21 11:04:53 -07002493
2494 // Use the session level connection address if the media level addresses are
2495 // not specified.
2496 rtc::SocketAddress address;
2497 address = content->connection_address().IsNil()
2498 ? session_connection_addr
2499 : content->connection_address();
2500 address.SetPort(port);
2501 content->set_connection_address(address);
2502
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002503 desc->AddContent(content_name,
Steve Anton5adfafd2017-12-20 16:34:00 -08002504 IsDtlsSctp(protocol) ? MediaProtocolType::kSctp
2505 : MediaProtocolType::kRtp,
deadbeef25ed4352016-12-12 18:37:36 -08002506 content_rejected, bundle_only, content.release());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002507 // Create TransportInfo with the media level "ice-pwd" and "ice-ufrag".
2508 TransportInfo transport_info(content_name, transport);
2509
2510 if (!desc->AddTransportInfo(transport_info)) {
2511 std::ostringstream description;
2512 description << "Failed to AddTransportInfo with content name: "
2513 << content_name;
2514 return ParseFailed("", description.str(), error);
2515 }
2516 }
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00002517
Steve Antone831b8c2018-02-01 12:22:16 -08002518 desc->set_msid_signaling(msid_signaling);
2519
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00002520 size_t end_of_message = message.size();
2521 if (mline_index == -1 && *pos != end_of_message) {
2522 ParseFailed(message, *pos, "Expects m line.", error);
2523 return false;
2524 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002525 return true;
2526}
2527
2528bool VerifyCodec(const cricket::Codec& codec) {
2529 // Codec has not been populated correctly unless the name has been set. This
2530 // can happen if an SDP has an fmtp or rtcp-fb with a payload type but doesn't
2531 // have a corresponding "rtpmap" line.
htab39db842016-12-08 01:50:48 -08002532 return !codec.name.empty();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002533}
2534
2535bool VerifyAudioCodecs(const AudioContentDescription* audio_desc) {
2536 const std::vector<cricket::AudioCodec>& codecs = audio_desc->codecs();
2537 for (std::vector<cricket::AudioCodec>::const_iterator iter = codecs.begin();
2538 iter != codecs.end(); ++iter) {
2539 if (!VerifyCodec(*iter)) {
2540 return false;
2541 }
2542 }
2543 return true;
2544}
2545
2546bool VerifyVideoCodecs(const VideoContentDescription* video_desc) {
2547 const std::vector<cricket::VideoCodec>& codecs = video_desc->codecs();
2548 for (std::vector<cricket::VideoCodec>::const_iterator iter = codecs.begin();
2549 iter != codecs.end(); ++iter) {
2550 if (!VerifyCodec(*iter)) {
2551 return false;
2552 }
2553 }
2554 return true;
2555}
2556
2557void AddParameters(const cricket::CodecParameterMap& parameters,
2558 cricket::Codec* codec) {
2559 for (cricket::CodecParameterMap::const_iterator iter =
2560 parameters.begin(); iter != parameters.end(); ++iter) {
2561 codec->SetParam(iter->first, iter->second);
2562 }
2563}
2564
2565void AddFeedbackParameter(const cricket::FeedbackParam& feedback_param,
2566 cricket::Codec* codec) {
2567 codec->AddFeedbackParam(feedback_param);
2568}
2569
2570void AddFeedbackParameters(const cricket::FeedbackParams& feedback_params,
2571 cricket::Codec* codec) {
2572 for (std::vector<cricket::FeedbackParam>::const_iterator iter =
2573 feedback_params.params().begin();
2574 iter != feedback_params.params().end(); ++iter) {
2575 codec->AddFeedbackParam(*iter);
2576 }
2577}
2578
2579// Gets the current codec setting associated with |payload_type|. If there
changbin.shao@webrtc.org2d25b442015-03-16 04:14:34 +00002580// is no Codec associated with that payload type it returns an empty codec
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002581// with that payload type.
2582template <class T>
changbin.shao@webrtc.org2d25b442015-03-16 04:14:34 +00002583T GetCodecWithPayloadType(const std::vector<T>& codecs, int payload_type) {
magjedb05fa242016-11-11 04:00:16 -08002584 const T* codec = FindCodecById(codecs, payload_type);
2585 if (codec)
2586 return *codec;
2587 // Return empty codec with |payload_type|.
changbin.shao@webrtc.org2d25b442015-03-16 04:14:34 +00002588 T ret_val;
magjedb05fa242016-11-11 04:00:16 -08002589 ret_val.id = payload_type;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002590 return ret_val;
2591}
2592
2593// Updates or creates a new codec entry in the audio description.
2594template <class T, class U>
2595void AddOrReplaceCodec(MediaContentDescription* content_desc, const U& codec) {
2596 T* desc = static_cast<T*>(content_desc);
2597 std::vector<U> codecs = desc->codecs();
2598 bool found = false;
2599
2600 typename std::vector<U>::iterator iter;
2601 for (iter = codecs.begin(); iter != codecs.end(); ++iter) {
2602 if (iter->id == codec.id) {
2603 *iter = codec;
2604 found = true;
2605 break;
2606 }
2607 }
2608 if (!found) {
2609 desc->AddCodec(codec);
2610 return;
2611 }
2612 desc->set_codecs(codecs);
2613}
2614
2615// Adds or updates existing codec corresponding to |payload_type| according
2616// to |parameters|.
2617template <class T, class U>
2618void UpdateCodec(MediaContentDescription* content_desc, int payload_type,
2619 const cricket::CodecParameterMap& parameters) {
2620 // Codec might already have been populated (from rtpmap).
changbin.shao@webrtc.org2d25b442015-03-16 04:14:34 +00002621 U new_codec = GetCodecWithPayloadType(static_cast<T*>(content_desc)->codecs(),
2622 payload_type);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002623 AddParameters(parameters, &new_codec);
2624 AddOrReplaceCodec<T, U>(content_desc, new_codec);
2625}
2626
2627// Adds or updates existing codec corresponding to |payload_type| according
2628// to |feedback_param|.
2629template <class T, class U>
2630void UpdateCodec(MediaContentDescription* content_desc, int payload_type,
2631 const cricket::FeedbackParam& feedback_param) {
2632 // Codec might already have been populated (from rtpmap).
changbin.shao@webrtc.org2d25b442015-03-16 04:14:34 +00002633 U new_codec = GetCodecWithPayloadType(static_cast<T*>(content_desc)->codecs(),
2634 payload_type);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002635 AddFeedbackParameter(feedback_param, &new_codec);
2636 AddOrReplaceCodec<T, U>(content_desc, new_codec);
2637}
2638
jlmiller@webrtc.orga744a282015-02-18 21:37:46 +00002639template <class T>
2640bool PopWildcardCodec(std::vector<T>* codecs, T* wildcard_codec) {
2641 for (auto iter = codecs->begin(); iter != codecs->end(); ++iter) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002642 if (iter->id == kWildcardPayloadType) {
2643 *wildcard_codec = *iter;
2644 codecs->erase(iter);
2645 return true;
2646 }
2647 }
2648 return false;
2649}
2650
jlmiller@webrtc.orga744a282015-02-18 21:37:46 +00002651template<class T>
2652void UpdateFromWildcardCodecs(cricket::MediaContentDescriptionImpl<T>* desc) {
2653 auto codecs = desc->codecs();
2654 T wildcard_codec;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002655 if (!PopWildcardCodec(&codecs, &wildcard_codec)) {
2656 return;
2657 }
jlmiller@webrtc.orga744a282015-02-18 21:37:46 +00002658 for (auto& codec : codecs) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002659 AddFeedbackParameters(wildcard_codec.feedback_params, &codec);
2660 }
jlmiller@webrtc.orga744a282015-02-18 21:37:46 +00002661 desc->set_codecs(codecs);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002662}
2663
2664void AddAudioAttribute(const std::string& name, const std::string& value,
2665 AudioContentDescription* audio_desc) {
2666 if (value.empty()) {
2667 return;
2668 }
2669 std::vector<cricket::AudioCodec> codecs = audio_desc->codecs();
2670 for (std::vector<cricket::AudioCodec>::iterator iter = codecs.begin();
2671 iter != codecs.end(); ++iter) {
2672 iter->params[name] = value;
2673 }
2674 audio_desc->set_codecs(codecs);
2675}
2676
2677bool ParseContent(const std::string& message,
2678 const MediaType media_type,
2679 int mline_index,
2680 const std::string& protocol,
deadbeef67cf2c12016-04-13 10:07:16 -07002681 const std::vector<int>& payload_types,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002682 size_t* pos,
2683 std::string* content_name,
deadbeef25ed4352016-12-12 18:37:36 -08002684 bool* bundle_only,
Steve Antone831b8c2018-02-01 12:22:16 -08002685 int* msid_signaling,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002686 MediaContentDescription* media_desc,
2687 TransportDescription* transport,
2688 std::vector<JsepIceCandidate*>* candidates,
2689 SdpParseError* error) {
nisseede5da42017-01-12 05:15:36 -08002690 RTC_DCHECK(media_desc != NULL);
2691 RTC_DCHECK(content_name != NULL);
2692 RTC_DCHECK(transport != NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002693
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +00002694 if (media_type == cricket::MEDIA_TYPE_AUDIO) {
Steve Antonb1c1de12017-12-21 15:14:30 -08002695 MaybeCreateStaticPayloadAudioCodecs(payload_types, media_desc->as_audio());
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +00002696 }
2697
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002698 // The media level "ice-ufrag" and "ice-pwd".
2699 // The candidates before update the media level "ice-pwd" and "ice-ufrag".
2700 Candidates candidates_orig;
2701 std::string line;
2702 std::string mline_id;
2703 // Tracks created out of the ssrc attributes.
2704 StreamParamsVec tracks;
2705 SsrcInfoVec ssrc_infos;
2706 SsrcGroupVec ssrc_groups;
2707 std::string maxptime_as_string;
2708 std::string ptime_as_string;
Tomas Gunnarsson191bf5c2018-03-30 10:44:43 +00002709 std::string stream_id;
deadbeef9d3584c2016-02-16 17:54:10 -08002710 std::string track_id;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002711
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002712 // Loop until the next m line
2713 while (!IsLineType(message, kLineTypeMedia, *pos)) {
2714 if (!GetLine(message, pos, &line)) {
2715 if (*pos >= message.size()) {
2716 break; // Done parsing
2717 } else {
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +00002718 return ParseFailed(message, *pos, "Invalid SDP line.", error);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002719 }
2720 }
2721
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002722 // RFC 4566
2723 // b=* (zero or more bandwidth information lines)
2724 if (IsLineType(line, kLineTypeSessionBandwidth)) {
2725 std::string bandwidth;
2726 if (HasAttribute(line, kApplicationSpecificMaximum)) {
2727 if (!GetValue(line, kApplicationSpecificMaximum, &bandwidth, error)) {
2728 return false;
2729 } else {
wu@webrtc.org5e760e72014-04-02 23:19:09 +00002730 int b = 0;
2731 if (!GetValueFromString(line, bandwidth, &b, error)) {
2732 return false;
2733 }
deadbeef3e8016e2017-08-03 17:49:30 -07002734 // TODO(deadbeef): Historically, applications may be setting a value
2735 // of -1 to mean "unset any previously set bandwidth limit", even
2736 // though ommitting the "b=AS" entirely will do just that. Once we've
2737 // transitioned applications to doing the right thing, it would be
2738 // better to treat this as a hard error instead of just ignoring it.
2739 if (b == -1) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01002740 RTC_LOG(LS_WARNING)
2741 << "Ignoring \"b=AS:-1\"; will be treated as \"no "
2742 "bandwidth limit\".";
deadbeef3e8016e2017-08-03 17:49:30 -07002743 continue;
2744 }
deadbeefbc88c6b2017-08-02 11:26:34 -07002745 if (b < 0) {
2746 return ParseFailed(line, "b=AS value can't be negative.", error);
2747 }
Peter Thatcherc0c3a862015-06-24 15:31:25 -07002748 // We should never use more than the default bandwidth for RTP-based
2749 // data channels. Don't allow SDP to set the bandwidth, because
2750 // that would give JS the opportunity to "break the Internet".
2751 // See: https://code.google.com/p/chromium/issues/detail?id=280726
2752 if (media_type == cricket::MEDIA_TYPE_DATA && IsRtp(protocol) &&
2753 b > cricket::kDataMaxBandwidth / 1000) {
2754 std::ostringstream description;
2755 description << "RTP-based data channels may not send more than "
2756 << cricket::kDataMaxBandwidth / 1000 << "kbps.";
2757 return ParseFailed(line, description.str(), error);
2758 }
deadbeefb2362572016-12-13 16:37:06 -08002759 // Prevent integer overflow.
2760 b = std::min(b, INT_MAX / 1000);
wu@webrtc.org5e760e72014-04-02 23:19:09 +00002761 media_desc->set_bandwidth(b * 1000);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002762 }
2763 }
2764 continue;
2765 }
2766
zhihuang38989e52017-03-21 11:04:53 -07002767 // Parse the media level connection data.
2768 if (IsLineType(line, kLineTypeConnection)) {
2769 rtc::SocketAddress addr;
2770 if (!ParseConnectionData(line, &addr, error)) {
2771 return false;
2772 }
2773 media_desc->set_connection_address(addr);
2774 continue;
2775 }
2776
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002777 if (!IsLineType(line, kLineTypeAttributes)) {
Steve Anton36b29d12017-10-30 09:57:42 -07002778 // TODO(deadbeef): Handle other lines if needed.
Mirko Bonadei675513b2017-11-09 11:09:25 +01002779 RTC_LOG(LS_INFO) << "Ignored line: " << line;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002780 continue;
2781 }
2782
2783 // Handle attributes common to SCTP and RTP.
2784 if (HasAttribute(line, kAttributeMid)) {
2785 // RFC 3388
2786 // mid-attribute = "a=mid:" identification-tag
2787 // identification-tag = token
2788 // Use the mid identification-tag as the content name.
2789 if (!GetValue(line, kAttributeMid, &mline_id, error)) {
2790 return false;
2791 }
2792 *content_name = mline_id;
deadbeef25ed4352016-12-12 18:37:36 -08002793 } else if (HasAttribute(line, kAttributeBundleOnly)) {
2794 *bundle_only = true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002795 } else if (HasAttribute(line, kAttributeCandidate)) {
2796 Candidate candidate;
2797 if (!ParseCandidate(line, &candidate, error, false)) {
2798 return false;
2799 }
deadbeef7bcdb692017-01-20 12:43:58 -08002800 // ParseCandidate will parse non-standard ufrag and password attributes,
2801 // since it's used for candidate trickling, but we only want to process
2802 // the "a=ice-ufrag"/"a=ice-pwd" values in a session description, so
2803 // strip them off at this point.
2804 candidate.set_username(std::string());
2805 candidate.set_password(std::string());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002806 candidates_orig.push_back(candidate);
2807 } else if (HasAttribute(line, kAttributeIceUfrag)) {
2808 if (!GetValue(line, kAttributeIceUfrag, &transport->ice_ufrag, error)) {
2809 return false;
2810 }
2811 } else if (HasAttribute(line, kAttributeIcePwd)) {
2812 if (!GetValue(line, kAttributeIcePwd, &transport->ice_pwd, error)) {
2813 return false;
2814 }
2815 } else if (HasAttribute(line, kAttributeIceOption)) {
2816 if (!ParseIceOptions(line, &transport->transport_options, error)) {
2817 return false;
2818 }
2819 } else if (HasAttribute(line, kAttributeFmtp)) {
2820 if (!ParseFmtpAttributes(line, media_type, media_desc, error)) {
2821 return false;
2822 }
2823 } else if (HasAttribute(line, kAttributeFingerprint)) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002824 rtc::SSLFingerprint* fingerprint = NULL;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002825
2826 if (!ParseFingerprintAttribute(line, &fingerprint, error)) {
2827 return false;
2828 }
2829 transport->identity_fingerprint.reset(fingerprint);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00002830 } else if (HasAttribute(line, kAttributeSetup)) {
2831 if (!ParseDtlsSetup(line, &(transport->connection_role), error)) {
2832 return false;
2833 }
pthatcher@webrtc.org3341b402015-02-13 21:14:22 +00002834 } else if (IsDtlsSctp(protocol) && HasAttribute(line, kAttributeSctpPort)) {
deadbeef7e146cb2016-09-28 10:04:34 -07002835 if (media_type != cricket::MEDIA_TYPE_DATA) {
2836 return ParseFailed(
2837 line, "sctp-port attribute found in non-data media description.",
2838 error);
2839 }
jiayl@webrtc.orgddb85ab2014-09-05 16:31:56 +00002840 int sctp_port;
2841 if (!ParseSctpPort(line, &sctp_port, error)) {
2842 return false;
2843 }
Steve Antonb1c1de12017-12-21 15:14:30 -08002844 if (!AddSctpDataCodec(media_desc->as_data(), sctp_port)) {
jiayl@webrtc.orgddb85ab2014-09-05 16:31:56 +00002845 return false;
2846 }
pthatcher@webrtc.org3341b402015-02-13 21:14:22 +00002847 } else if (IsRtp(protocol)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002848 //
2849 // RTP specific attrubtes
2850 //
2851 if (HasAttribute(line, kAttributeRtcpMux)) {
2852 media_desc->set_rtcp_mux(true);
deadbeef13871492015-12-09 12:37:51 -08002853 } else if (HasAttribute(line, kAttributeRtcpReducedSize)) {
2854 media_desc->set_rtcp_reduced_size(true);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002855 } else if (HasAttribute(line, kAttributeSsrcGroup)) {
2856 if (!ParseSsrcGroupAttribute(line, &ssrc_groups, error)) {
2857 return false;
2858 }
2859 } else if (HasAttribute(line, kAttributeSsrc)) {
Steve Antone831b8c2018-02-01 12:22:16 -08002860 if (!ParseSsrcAttribute(line, &ssrc_infos, msid_signaling, error)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002861 return false;
2862 }
2863 } else if (HasAttribute(line, kAttributeCrypto)) {
2864 if (!ParseCryptoAttribute(line, media_desc, error)) {
2865 return false;
2866 }
2867 } else if (HasAttribute(line, kAttributeRtpmap)) {
deadbeef67cf2c12016-04-13 10:07:16 -07002868 if (!ParseRtpmapAttribute(line, media_type, payload_types, media_desc,
2869 error)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002870 return false;
2871 }
2872 } else if (HasAttribute(line, kCodecParamMaxPTime)) {
2873 if (!GetValue(line, kCodecParamMaxPTime, &maxptime_as_string, error)) {
2874 return false;
2875 }
2876 } else if (HasAttribute(line, kAttributeRtcpFb)) {
2877 if (!ParseRtcpFbAttribute(line, media_type, media_desc, error)) {
2878 return false;
2879 }
2880 } else if (HasAttribute(line, kCodecParamPTime)) {
2881 if (!GetValue(line, kCodecParamPTime, &ptime_as_string, error)) {
2882 return false;
2883 }
2884 } else if (HasAttribute(line, kAttributeSendOnly)) {
Steve Anton4e70a722017-11-28 14:57:10 -08002885 media_desc->set_direction(RtpTransceiverDirection::kSendOnly);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002886 } else if (HasAttribute(line, kAttributeRecvOnly)) {
Steve Anton4e70a722017-11-28 14:57:10 -08002887 media_desc->set_direction(RtpTransceiverDirection::kRecvOnly);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002888 } else if (HasAttribute(line, kAttributeInactive)) {
Steve Anton4e70a722017-11-28 14:57:10 -08002889 media_desc->set_direction(RtpTransceiverDirection::kInactive);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002890 } else if (HasAttribute(line, kAttributeSendRecv)) {
Steve Anton4e70a722017-11-28 14:57:10 -08002891 media_desc->set_direction(RtpTransceiverDirection::kSendRecv);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002892 } else if (HasAttribute(line, kAttributeExtmap)) {
isheriff6f8d6862016-05-26 11:24:55 -07002893 RtpExtension extmap;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002894 if (!ParseExtmap(line, &extmap, error)) {
2895 return false;
2896 }
2897 media_desc->AddRtpHeaderExtension(extmap);
2898 } else if (HasAttribute(line, kAttributeXGoogleFlag)) {
2899 // Experimental attribute. Conference mode activates more aggressive
2900 // AEC and NS settings.
Steve Anton36b29d12017-10-30 09:57:42 -07002901 // TODO(deadbeef): expose API to set these directly.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002902 std::string flag_value;
2903 if (!GetValue(line, kAttributeXGoogleFlag, &flag_value, error)) {
2904 return false;
2905 }
2906 if (flag_value.compare(kValueConference) == 0)
2907 media_desc->set_conference_mode(true);
deadbeef9d3584c2016-02-16 17:54:10 -08002908 } else if (HasAttribute(line, kAttributeMsid)) {
Tomas Gunnarsson191bf5c2018-03-30 10:44:43 +00002909 if (!ParseMsidAttribute(line, &stream_id, &track_id, error)) {
deadbeef9d3584c2016-02-16 17:54:10 -08002910 return false;
2911 }
Steve Antone831b8c2018-02-01 12:22:16 -08002912 *msid_signaling |= cricket::kMsidSignalingMediaSection;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002913 }
2914 } else {
2915 // Only parse lines that we are interested of.
Mirko Bonadei675513b2017-11-09 11:09:25 +01002916 RTC_LOG(LS_INFO) << "Ignored line: " << line;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002917 continue;
2918 }
2919 }
2920
2921 // Create tracks from the |ssrc_infos|.
Taylor Brandstetter5de6b752016-03-09 17:02:30 -08002922 // If the stream_id/track_id for all SSRCS are identical, one StreamParams
2923 // will be created in CreateTracksFromSsrcInfos, containing all the SSRCs from
2924 // the m= section.
Tomas Gunnarsson191bf5c2018-03-30 10:44:43 +00002925 CreateTracksFromSsrcInfos(ssrc_infos, stream_id, track_id, &tracks);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002926
2927 // Add the ssrc group to the track.
2928 for (SsrcGroupVec::iterator ssrc_group = ssrc_groups.begin();
2929 ssrc_group != ssrc_groups.end(); ++ssrc_group) {
2930 if (ssrc_group->ssrcs.empty()) {
2931 continue;
2932 }
Peter Boström0c4e06b2015-10-07 12:23:21 +02002933 uint32_t ssrc = ssrc_group->ssrcs.front();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002934 for (StreamParamsVec::iterator track = tracks.begin();
2935 track != tracks.end(); ++track) {
2936 if (track->has_ssrc(ssrc)) {
2937 track->ssrc_groups.push_back(*ssrc_group);
2938 }
2939 }
2940 }
2941
2942 // Add the new tracks to the |media_desc|.
deadbeef9d3584c2016-02-16 17:54:10 -08002943 for (StreamParams& track : tracks) {
2944 media_desc->AddStream(track);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002945 }
2946
2947 if (media_type == cricket::MEDIA_TYPE_AUDIO) {
Steve Antonb1c1de12017-12-21 15:14:30 -08002948 AudioContentDescription* audio_desc = media_desc->as_audio();
jlmiller@webrtc.orga744a282015-02-18 21:37:46 +00002949 UpdateFromWildcardCodecs(audio_desc);
2950
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002951 // Verify audio codec ensures that no audio codec has been populated with
2952 // only fmtp.
2953 if (!VerifyAudioCodecs(audio_desc)) {
2954 return ParseFailed("Failed to parse audio codecs correctly.", error);
2955 }
2956 AddAudioAttribute(kCodecParamMaxPTime, maxptime_as_string, audio_desc);
2957 AddAudioAttribute(kCodecParamPTime, ptime_as_string, audio_desc);
2958 }
2959
2960 if (media_type == cricket::MEDIA_TYPE_VIDEO) {
Steve Antonb1c1de12017-12-21 15:14:30 -08002961 VideoContentDescription* video_desc = media_desc->as_video();
jlmiller@webrtc.orga744a282015-02-18 21:37:46 +00002962 UpdateFromWildcardCodecs(video_desc);
2963 // Verify video codec ensures that no video codec has been populated with
2964 // only rtcp-fb.
2965 if (!VerifyVideoCodecs(video_desc)) {
2966 return ParseFailed("Failed to parse video codecs correctly.", error);
2967 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002968 }
2969
2970 // RFC 5245
2971 // Update the candidates with the media level "ice-pwd" and "ice-ufrag".
2972 for (Candidates::iterator it = candidates_orig.begin();
2973 it != candidates_orig.end(); ++it) {
nisseede5da42017-01-12 05:15:36 -08002974 RTC_DCHECK((*it).username().empty() ||
2975 (*it).username() == transport->ice_ufrag);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002976 (*it).set_username(transport->ice_ufrag);
nisseede5da42017-01-12 05:15:36 -08002977 RTC_DCHECK((*it).password().empty());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002978 (*it).set_password(transport->ice_pwd);
2979 candidates->push_back(
2980 new JsepIceCandidate(mline_id, mline_index, *it));
2981 }
2982 return true;
2983}
2984
Steve Antone831b8c2018-02-01 12:22:16 -08002985bool ParseSsrcAttribute(const std::string& line,
2986 SsrcInfoVec* ssrc_infos,
2987 int* msid_signaling,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002988 SdpParseError* error) {
nisseede5da42017-01-12 05:15:36 -08002989 RTC_DCHECK(ssrc_infos != NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002990 // RFC 5576
2991 // a=ssrc:<ssrc-id> <attribute>
2992 // a=ssrc:<ssrc-id> <attribute>:<value>
2993 std::string field1, field2;
Donald Curtis144d0182015-05-15 13:14:24 -07002994 if (!rtc::tokenize_first(line.substr(kLinePrefixLength), kSdpDelimiterSpace,
2995 &field1, &field2)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002996 const size_t expected_fields = 2;
2997 return ParseFailedExpectFieldNum(line, expected_fields, error);
2998 }
2999
3000 // ssrc:<ssrc-id>
3001 std::string ssrc_id_s;
3002 if (!GetValue(field1, kAttributeSsrc, &ssrc_id_s, error)) {
3003 return false;
3004 }
Peter Boström0c4e06b2015-10-07 12:23:21 +02003005 uint32_t ssrc_id = 0;
wu@webrtc.org5e760e72014-04-02 23:19:09 +00003006 if (!GetValueFromString(line, ssrc_id_s, &ssrc_id, error)) {
3007 return false;
3008 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003009
3010 std::string attribute;
3011 std::string value;
Donald Curtis144d0182015-05-15 13:14:24 -07003012 if (!rtc::tokenize_first(field2, kSdpDelimiterColon, &attribute, &value)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003013 std::ostringstream description;
3014 description << "Failed to get the ssrc attribute value from " << field2
3015 << ". Expected format <attribute>:<value>.";
3016 return ParseFailed(line, description.str(), error);
3017 }
3018
3019 // Check if there's already an item for this |ssrc_id|. Create a new one if
3020 // there isn't.
3021 SsrcInfoVec::iterator ssrc_info = ssrc_infos->begin();
3022 for (; ssrc_info != ssrc_infos->end(); ++ssrc_info) {
3023 if (ssrc_info->ssrc_id == ssrc_id) {
3024 break;
3025 }
3026 }
3027 if (ssrc_info == ssrc_infos->end()) {
3028 SsrcInfo info;
3029 info.ssrc_id = ssrc_id;
3030 ssrc_infos->push_back(info);
3031 ssrc_info = ssrc_infos->end() - 1;
3032 }
3033
3034 // Store the info to the |ssrc_info|.
3035 if (attribute == kSsrcAttributeCname) {
3036 // RFC 5576
3037 // cname:<value>
3038 ssrc_info->cname = value;
3039 } else if (attribute == kSsrcAttributeMsid) {
3040 // draft-alvestrand-mmusic-msid-00
Tomas Gunnarsson191bf5c2018-03-30 10:44:43 +00003041 // "msid:" identifier [ " " appdata ]
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003042 std::vector<std::string> fields;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00003043 rtc::split(value, kSdpDelimiterSpace, &fields);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003044 if (fields.size() < 1 || fields.size() > 2) {
3045 return ParseFailed(line,
3046 "Expected format \"msid:<identifier>[ <appdata>]\".",
3047 error);
3048 }
deadbeef9d3584c2016-02-16 17:54:10 -08003049 ssrc_info->stream_id = fields[0];
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003050 if (fields.size() == 2) {
deadbeef9d3584c2016-02-16 17:54:10 -08003051 ssrc_info->track_id = fields[1];
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003052 }
Steve Antone831b8c2018-02-01 12:22:16 -08003053 *msid_signaling |= cricket::kMsidSignalingSsrcAttribute;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003054 } else if (attribute == kSsrcAttributeMslabel) {
3055 // draft-alvestrand-rtcweb-mid-01
3056 // mslabel:<value>
3057 ssrc_info->mslabel = value;
3058 } else if (attribute == kSSrcAttributeLabel) {
3059 // The label isn't defined.
3060 // label:<value>
3061 ssrc_info->label = value;
3062 }
3063 return true;
3064}
3065
3066bool ParseSsrcGroupAttribute(const std::string& line,
3067 SsrcGroupVec* ssrc_groups,
3068 SdpParseError* error) {
nisseede5da42017-01-12 05:15:36 -08003069 RTC_DCHECK(ssrc_groups != NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003070 // RFC 5576
3071 // a=ssrc-group:<semantics> <ssrc-id> ...
3072 std::vector<std::string> fields;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00003073 rtc::split(line.substr(kLinePrefixLength),
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003074 kSdpDelimiterSpace, &fields);
3075 const size_t expected_min_fields = 2;
3076 if (fields.size() < expected_min_fields) {
3077 return ParseFailedExpectMinFieldNum(line, expected_min_fields, error);
3078 }
3079 std::string semantics;
3080 if (!GetValue(fields[0], kAttributeSsrcGroup, &semantics, error)) {
3081 return false;
3082 }
Peter Boström0c4e06b2015-10-07 12:23:21 +02003083 std::vector<uint32_t> ssrcs;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003084 for (size_t i = 1; i < fields.size(); ++i) {
Peter Boström0c4e06b2015-10-07 12:23:21 +02003085 uint32_t ssrc = 0;
wu@webrtc.org5e760e72014-04-02 23:19:09 +00003086 if (!GetValueFromString(line, fields[i], &ssrc, error)) {
3087 return false;
3088 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003089 ssrcs.push_back(ssrc);
3090 }
3091 ssrc_groups->push_back(SsrcGroup(semantics, ssrcs));
3092 return true;
3093}
3094
3095bool ParseCryptoAttribute(const std::string& line,
3096 MediaContentDescription* media_desc,
3097 SdpParseError* error) {
3098 std::vector<std::string> fields;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00003099 rtc::split(line.substr(kLinePrefixLength),
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003100 kSdpDelimiterSpace, &fields);
3101 // RFC 4568
3102 // a=crypto:<tag> <crypto-suite> <key-params> [<session-params>]
3103 const size_t expected_min_fields = 3;
3104 if (fields.size() < expected_min_fields) {
3105 return ParseFailedExpectMinFieldNum(line, expected_min_fields, error);
3106 }
3107 std::string tag_value;
3108 if (!GetValue(fields[0], kAttributeCrypto, &tag_value, error)) {
3109 return false;
3110 }
wu@webrtc.org5e760e72014-04-02 23:19:09 +00003111 int tag = 0;
3112 if (!GetValueFromString(line, tag_value, &tag, error)) {
3113 return false;
3114 }
jbauch083b73f2015-07-16 02:46:32 -07003115 const std::string& crypto_suite = fields[1];
3116 const std::string& key_params = fields[2];
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003117 std::string session_params;
3118 if (fields.size() > 3) {
3119 session_params = fields[3];
3120 }
3121 media_desc->AddCrypto(CryptoParams(tag, crypto_suite, key_params,
3122 session_params));
3123 return true;
3124}
3125
3126// Updates or creates a new codec entry in the audio description with according
deadbeef67cf2c12016-04-13 10:07:16 -07003127// to |name|, |clockrate|, |bitrate|, and |channels|.
3128void UpdateCodec(int payload_type,
3129 const std::string& name,
3130 int clockrate,
3131 int bitrate,
3132 size_t channels,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003133 AudioContentDescription* audio_desc) {
3134 // Codec may already be populated with (only) optional parameters
3135 // (from an fmtp).
changbin.shao@webrtc.org2d25b442015-03-16 04:14:34 +00003136 cricket::AudioCodec codec =
3137 GetCodecWithPayloadType(audio_desc->codecs(), payload_type);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003138 codec.name = name;
3139 codec.clockrate = clockrate;
3140 codec.bitrate = bitrate;
3141 codec.channels = channels;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003142 AddOrReplaceCodec<AudioContentDescription, cricket::AudioCodec>(audio_desc,
3143 codec);
3144}
3145
3146// Updates or creates a new codec entry in the video description according to
deadbeef67cf2c12016-04-13 10:07:16 -07003147// |name|, |width|, |height|, and |framerate|.
3148void UpdateCodec(int payload_type,
3149 const std::string& name,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003150 VideoContentDescription* video_desc) {
3151 // Codec may already be populated with (only) optional parameters
3152 // (from an fmtp).
changbin.shao@webrtc.org2d25b442015-03-16 04:14:34 +00003153 cricket::VideoCodec codec =
3154 GetCodecWithPayloadType(video_desc->codecs(), payload_type);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003155 codec.name = name;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003156 AddOrReplaceCodec<VideoContentDescription, cricket::VideoCodec>(video_desc,
3157 codec);
3158}
3159
3160bool ParseRtpmapAttribute(const std::string& line,
3161 const MediaType media_type,
deadbeef67cf2c12016-04-13 10:07:16 -07003162 const std::vector<int>& payload_types,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003163 MediaContentDescription* media_desc,
3164 SdpParseError* error) {
3165 std::vector<std::string> fields;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00003166 rtc::split(line.substr(kLinePrefixLength),
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003167 kSdpDelimiterSpace, &fields);
3168 // RFC 4566
3169 // a=rtpmap:<payload type> <encoding name>/<clock rate>[/<encodingparameters>]
3170 const size_t expected_min_fields = 2;
3171 if (fields.size() < expected_min_fields) {
3172 return ParseFailedExpectMinFieldNum(line, expected_min_fields, error);
3173 }
3174 std::string payload_type_value;
3175 if (!GetValue(fields[0], kAttributeRtpmap, &payload_type_value, error)) {
3176 return false;
3177 }
wu@webrtc.org5e760e72014-04-02 23:19:09 +00003178 int payload_type = 0;
pkasting@chromium.orge9facf82015-02-17 20:36:28 +00003179 if (!GetPayloadTypeFromString(line, payload_type_value, &payload_type,
3180 error)) {
wu@webrtc.org5e760e72014-04-02 23:19:09 +00003181 return false;
3182 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003183
deadbeef67cf2c12016-04-13 10:07:16 -07003184 if (std::find(payload_types.begin(), payload_types.end(), payload_type) ==
3185 payload_types.end()) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01003186 RTC_LOG(LS_WARNING) << "Ignore rtpmap line that did not appear in the "
Jonas Olsson45cc8902018-02-13 10:37:07 +01003187 "<fmt> of the m-line: "
3188 << line;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003189 return true;
3190 }
jbauch083b73f2015-07-16 02:46:32 -07003191 const std::string& encoder = fields[1];
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003192 std::vector<std::string> codec_params;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00003193 rtc::split(encoder, '/', &codec_params);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003194 // <encoding name>/<clock rate>[/<encodingparameters>]
3195 // 2 mandatory fields
3196 if (codec_params.size() < 2 || codec_params.size() > 3) {
3197 return ParseFailed(line,
3198 "Expected format \"<encoding name>/<clock rate>"
3199 "[/<encodingparameters>]\".",
3200 error);
3201 }
jbauch083b73f2015-07-16 02:46:32 -07003202 const std::string& encoding_name = codec_params[0];
wu@webrtc.org5e760e72014-04-02 23:19:09 +00003203 int clock_rate = 0;
3204 if (!GetValueFromString(line, codec_params[1], &clock_rate, error)) {
3205 return false;
3206 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003207 if (media_type == cricket::MEDIA_TYPE_VIDEO) {
Steve Antonb1c1de12017-12-21 15:14:30 -08003208 VideoContentDescription* video_desc = media_desc->as_video();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003209 UpdateCodec(payload_type, encoding_name,
deadbeef67cf2c12016-04-13 10:07:16 -07003210 video_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003211 } else if (media_type == cricket::MEDIA_TYPE_AUDIO) {
3212 // RFC 4566
3213 // For audio streams, <encoding parameters> indicates the number
3214 // of audio channels. This parameter is OPTIONAL and may be
3215 // omitted if the number of channels is one, provided that no
3216 // additional parameters are needed.
Peter Kasting69558702016-01-12 16:26:35 -08003217 size_t channels = 1;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003218 if (codec_params.size() == 3) {
wu@webrtc.org5e760e72014-04-02 23:19:09 +00003219 if (!GetValueFromString(line, codec_params[2], &channels, error)) {
3220 return false;
3221 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003222 }
Steve Antonb1c1de12017-12-21 15:14:30 -08003223 AudioContentDescription* audio_desc = media_desc->as_audio();
ossue1405ad2017-01-23 08:55:48 -08003224 UpdateCodec(payload_type, encoding_name, clock_rate, 0, channels,
deadbeef67cf2c12016-04-13 10:07:16 -07003225 audio_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003226 } else if (media_type == cricket::MEDIA_TYPE_DATA) {
Steve Antonb1c1de12017-12-21 15:14:30 -08003227 DataContentDescription* data_desc = media_desc->as_data();
deadbeef67cf2c12016-04-13 10:07:16 -07003228 data_desc->AddCodec(cricket::DataCodec(payload_type, encoding_name));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003229 }
3230 return true;
3231}
3232
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003233bool ParseFmtpParam(const std::string& line, std::string* parameter,
3234 std::string* value, SdpParseError* error) {
Donald Curtis0e07f922015-05-15 09:21:23 -07003235 if (!rtc::tokenize_first(line, kSdpDelimiterEqual, parameter, value)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003236 ParseFailed(line, "Unable to parse fmtp parameter. \'=\' missing.", error);
3237 return false;
3238 }
3239 // a=fmtp:<payload_type> <param1>=<value1>; <param2>=<value2>; ...
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003240 return true;
3241}
3242
3243bool ParseFmtpAttributes(const std::string& line, const MediaType media_type,
3244 MediaContentDescription* media_desc,
3245 SdpParseError* error) {
3246 if (media_type != cricket::MEDIA_TYPE_AUDIO &&
3247 media_type != cricket::MEDIA_TYPE_VIDEO) {
3248 return true;
3249 }
Donald Curtis0e07f922015-05-15 09:21:23 -07003250
3251 std::string line_payload;
3252 std::string line_params;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003253
3254 // RFC 5576
3255 // a=fmtp:<format> <format specific parameters>
3256 // At least two fields, whereas the second one is any of the optional
3257 // parameters.
Donald Curtis144d0182015-05-15 13:14:24 -07003258 if (!rtc::tokenize_first(line.substr(kLinePrefixLength), kSdpDelimiterSpace,
3259 &line_payload, &line_params)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003260 ParseFailedExpectMinFieldNum(line, 2, error);
3261 return false;
3262 }
3263
Donald Curtis0e07f922015-05-15 09:21:23 -07003264 // Parse out the payload information.
pkasting@chromium.orgd3245462015-02-23 21:28:22 +00003265 std::string payload_type_str;
Donald Curtis0e07f922015-05-15 09:21:23 -07003266 if (!GetValue(line_payload, kAttributeFmtp, &payload_type_str, error)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003267 return false;
3268 }
3269
Donald Curtis0e07f922015-05-15 09:21:23 -07003270 int payload_type = 0;
Donald Curtis144d0182015-05-15 13:14:24 -07003271 if (!GetPayloadTypeFromString(line_payload, payload_type_str, &payload_type,
3272 error)) {
Donald Curtis0e07f922015-05-15 09:21:23 -07003273 return false;
3274 }
3275
3276 // Parse out format specific parameters.
3277 std::vector<std::string> fields;
3278 rtc::split(line_params, kSdpDelimiterSemicolon, &fields);
3279
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003280 cricket::CodecParameterMap codec_params;
Donald Curtis144d0182015-05-15 13:14:24 -07003281 for (auto& iter : fields) {
Donald Curtis0e07f922015-05-15 09:21:23 -07003282 if (iter.find(kSdpDelimiterEqual) == std::string::npos) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003283 // Only fmtps with equals are currently supported. Other fmtp types
3284 // should be ignored. Unknown fmtps do not constitute an error.
3285 continue;
3286 }
Donald Curtis0e07f922015-05-15 09:21:23 -07003287
3288 std::string name;
3289 std::string value;
3290 if (!ParseFmtpParam(rtc::string_trim(iter), &name, &value, error)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003291 return false;
3292 }
3293 codec_params[name] = value;
3294 }
3295
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003296 if (media_type == cricket::MEDIA_TYPE_AUDIO) {
3297 UpdateCodec<AudioContentDescription, cricket::AudioCodec>(
pkasting@chromium.orgd3245462015-02-23 21:28:22 +00003298 media_desc, payload_type, codec_params);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003299 } else if (media_type == cricket::MEDIA_TYPE_VIDEO) {
3300 UpdateCodec<VideoContentDescription, cricket::VideoCodec>(
pkasting@chromium.orgd3245462015-02-23 21:28:22 +00003301 media_desc, payload_type, codec_params);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003302 }
3303 return true;
3304}
3305
3306bool ParseRtcpFbAttribute(const std::string& line, const MediaType media_type,
3307 MediaContentDescription* media_desc,
3308 SdpParseError* error) {
3309 if (media_type != cricket::MEDIA_TYPE_AUDIO &&
3310 media_type != cricket::MEDIA_TYPE_VIDEO) {
3311 return true;
3312 }
3313 std::vector<std::string> rtcp_fb_fields;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00003314 rtc::split(line.c_str(), kSdpDelimiterSpace, &rtcp_fb_fields);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003315 if (rtcp_fb_fields.size() < 2) {
3316 return ParseFailedGetValue(line, kAttributeRtcpFb, error);
3317 }
3318 std::string payload_type_string;
3319 if (!GetValue(rtcp_fb_fields[0], kAttributeRtcpFb, &payload_type_string,
3320 error)) {
3321 return false;
3322 }
wu@webrtc.org5e760e72014-04-02 23:19:09 +00003323 int payload_type = kWildcardPayloadType;
3324 if (payload_type_string != "*") {
pkasting@chromium.orge9facf82015-02-17 20:36:28 +00003325 if (!GetPayloadTypeFromString(line, payload_type_string, &payload_type,
3326 error)) {
wu@webrtc.org5e760e72014-04-02 23:19:09 +00003327 return false;
3328 }
3329 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003330 std::string id = rtcp_fb_fields[1];
3331 std::string param = "";
3332 for (std::vector<std::string>::iterator iter = rtcp_fb_fields.begin() + 2;
3333 iter != rtcp_fb_fields.end(); ++iter) {
3334 param.append(*iter);
3335 }
3336 const cricket::FeedbackParam feedback_param(id, param);
3337
3338 if (media_type == cricket::MEDIA_TYPE_AUDIO) {
pkasting@chromium.orgd3245462015-02-23 21:28:22 +00003339 UpdateCodec<AudioContentDescription, cricket::AudioCodec>(
3340 media_desc, payload_type, feedback_param);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003341 } else if (media_type == cricket::MEDIA_TYPE_VIDEO) {
pkasting@chromium.orgd3245462015-02-23 21:28:22 +00003342 UpdateCodec<VideoContentDescription, cricket::VideoCodec>(
3343 media_desc, payload_type, feedback_param);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003344 }
3345 return true;
3346}
3347
3348} // namespace webrtc