blob: e796f4c87c69829f832ddf9eb3712fc8bf4da1e7 [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,
328 std::string* stream_id,
329 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
Taylor Brandstetter5de6b752016-03-09 17:02:30 -0800581// |msid_stream_id| and |msid_track_id| represent the stream/track ID from the
582// "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,
Taylor Brandstetter5de6b752016-03-09 17:02:30 -0800585 const std::string& msid_stream_id,
586 const std::string& msid_track_id,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000587 StreamParamsVec* tracks) {
nisseede5da42017-01-12 05:15:36 -0800588 RTC_DCHECK(tracks != NULL);
589 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
Taylor Brandstetter5de6b752016-03-09 17:02:30 -0800596 std::string stream_id;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000597 std::string track_id;
Taylor Brandstetter5de6b752016-03-09 17:02:30 -0800598 if (ssrc_info->stream_id.empty() && !ssrc_info->mslabel.empty()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000599 // 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.
601 // In that case, we use the mslabel and label to construct the track.
Taylor Brandstetter5de6b752016-03-09 17:02:30 -0800602 stream_id = ssrc_info->mslabel;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000603 track_id = ssrc_info->label;
Taylor Brandstetter5de6b752016-03-09 17:02:30 -0800604 } 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
Steve Antone831b8c2018-02-01 12:22:16 -0800606 // (from a=msid), use that. This is the case with Unified Plan SDP.
Taylor Brandstetter5de6b752016-03-09 17:02:30 -0800607 stream_id = msid_stream_id;
608 track_id = msid_track_id;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000609 } else {
Taylor Brandstetter5de6b752016-03-09 17:02:30 -0800610 stream_id = ssrc_info->stream_id;
deadbeef9d3584c2016-02-16 17:54:10 -0800611 track_id = ssrc_info->track_id;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000612 }
Taylor Brandstetter5de6b752016-03-09 17:02:30 -0800613 // If a stream/track ID wasn't populated from the SSRC attributes OR the
614 // msid attribute, use default/random values.
615 if (stream_id.empty()) {
616 stream_id = kDefaultMsid;
617 }
618 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;
Taylor Brandstetter5de6b752016-03-09 17:02:30 -0800637 track->sync_label = stream_id;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000638 track->id = track_id;
639 }
640}
641
642void GetMediaStreamLabels(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()) {
646 labels->insert(stream_params.sync_label);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000647 }
648}
649
650// RFC 5245
651// It is RECOMMENDED that default candidates be chosen based on the
652// likelihood of those candidates to work with the peer that is being
653// contacted. It is RECOMMENDED that relayed > reflexive > host.
654static const int kPreferenceUnknown = 0;
655static const int kPreferenceHost = 1;
656static const int kPreferenceReflexive = 2;
657static const int kPreferenceRelayed = 3;
658
659static int GetCandidatePreferenceFromType(const std::string& type) {
660 int preference = kPreferenceUnknown;
661 if (type == cricket::LOCAL_PORT_TYPE) {
662 preference = kPreferenceHost;
663 } else if (type == cricket::STUN_PORT_TYPE) {
664 preference = kPreferenceReflexive;
665 } else if (type == cricket::RELAY_PORT_TYPE) {
666 preference = kPreferenceRelayed;
667 } else {
nissec80e7412017-01-11 05:56:46 -0800668 RTC_NOTREACHED();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000669 }
670 return preference;
671}
672
guoweis@webrtc.org57ac2c82015-02-06 00:45:13 +0000673// Get ip and port of the default destination from the |candidates| with the
674// given value of |component_id|. The default candidate should be the one most
675// likely to work, typically IPv4 relay.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000676// RFC 5245
677// The value of |component_id| currently supported are 1 (RTP) and 2 (RTCP).
Steve Anton36b29d12017-10-30 09:57:42 -0700678// TODO(deadbeef): Decide the default destination in webrtcsession and
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000679// pass it down via SessionDescription.
pthatcher@webrtc.orgc9d6d142014-10-23 23:37:22 +0000680static void GetDefaultDestination(
681 const std::vector<Candidate>& candidates,
682 int component_id, std::string* port,
683 std::string* ip, std::string* addr_type) {
perkj@webrtc.orgd105cc82014-11-07 11:22:06 +0000684 *addr_type = kConnectionIpv4Addrtype;
pthatcher@webrtc.orgc9d6d142014-10-23 23:37:22 +0000685 *port = kDummyPort;
686 *ip = kDummyAddress;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000687 int current_preference = kPreferenceUnknown;
guoweis@webrtc.org57ac2c82015-02-06 00:45:13 +0000688 int current_family = AF_UNSPEC;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000689 for (std::vector<Candidate>::const_iterator it = candidates.begin();
690 it != candidates.end(); ++it) {
691 if (it->component() != component_id) {
692 continue;
693 }
guoweis@webrtc.org57ac2c82015-02-06 00:45:13 +0000694 // Default destination should be UDP only.
695 if (it->protocol() != cricket::UDP_PROTOCOL_NAME) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000696 continue;
697 }
guoweis@webrtc.org57ac2c82015-02-06 00:45:13 +0000698 const int preference = GetCandidatePreferenceFromType(it->type());
699 const int family = it->address().ipaddr().family();
700 // See if this candidate is more preferable then the current one if it's the
701 // same family. Or if the current family is IPv4 already so we could safely
702 // ignore all IPv6 ones. WebRTC bug 4269.
703 // http://code.google.com/p/webrtc/issues/detail?id=4269
704 if ((preference <= current_preference && current_family == family) ||
705 (current_family == AF_INET && family == AF_INET6)) {
706 continue;
707 }
pthatcher@webrtc.orgc9d6d142014-10-23 23:37:22 +0000708 if (family == AF_INET) {
709 addr_type->assign(kConnectionIpv4Addrtype);
710 } else if (family == AF_INET6) {
711 addr_type->assign(kConnectionIpv6Addrtype);
712 }
guoweis@webrtc.org57ac2c82015-02-06 00:45:13 +0000713 current_preference = preference;
714 current_family = family;
715 *port = it->address().PortAsString();
716 *ip = it->address().ipaddr().ToString();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000717 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000718}
719
wu@webrtc.org4c3e9912014-07-16 21:03:13 +0000720// Gets "a=rtcp" line if found default RTCP candidate from |candidates|.
721static std::string GetRtcpLine(const std::vector<Candidate>& candidates) {
pthatcher@webrtc.orgc9d6d142014-10-23 23:37:22 +0000722 std::string rtcp_line, rtcp_port, rtcp_ip, addr_type;
723 GetDefaultDestination(candidates, ICE_CANDIDATE_COMPONENT_RTCP,
724 &rtcp_port, &rtcp_ip, &addr_type);
725 // Found default RTCP candidate.
726 // RFC 5245
727 // If the agent is utilizing RTCP, it MUST encode the RTCP candidate
728 // using the a=rtcp attribute as defined in RFC 3605.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000729
pthatcher@webrtc.orgc9d6d142014-10-23 23:37:22 +0000730 // RFC 3605
731 // rtcp-attribute = "a=rtcp:" port [nettype space addrtype space
732 // connection-address] CRLF
733 std::ostringstream os;
734 InitAttrLine(kAttributeRtcp, &os);
735 os << kSdpDelimiterColon
736 << rtcp_port << " "
737 << kConnectionNettype << " "
738 << addr_type << " "
739 << rtcp_ip;
740 rtcp_line = os.str();
wu@webrtc.org4c3e9912014-07-16 21:03:13 +0000741 return rtcp_line;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000742}
743
744// Get candidates according to the mline index from SessionDescriptionInterface.
745static void GetCandidatesByMindex(const SessionDescriptionInterface& desci,
746 int mline_index,
747 std::vector<Candidate>* candidates) {
748 if (!candidates) {
749 return;
750 }
751 const IceCandidateCollection* cc = desci.candidates(mline_index);
752 for (size_t i = 0; i < cc->count(); ++i) {
753 const IceCandidateInterface* candidate = cc->at(i);
754 candidates->push_back(candidate->candidate());
755 }
756}
757
deadbeef90f1e1e2017-02-10 12:35:05 -0800758static bool IsValidPort(int port) {
759 return port >= 0 && port <= 65535;
760}
761
Steve Antone831b8c2018-02-01 12:22:16 -0800762std::string SdpSerialize(const JsepSessionDescription& jdesc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000763 const cricket::SessionDescription* desc = jdesc.description();
764 if (!desc) {
765 return "";
766 }
767
768 std::string message;
769
770 // Session Description.
771 AddLine(kSessionVersion, &message);
772 // Session Origin
773 // RFC 4566
774 // o=<username> <sess-id> <sess-version> <nettype> <addrtype>
775 // <unicast-address>
776 std::ostringstream os;
777 InitLine(kLineTypeOrigin, kSessionOriginUsername, &os);
jbauch083b73f2015-07-16 02:46:32 -0700778 const std::string& session_id = jdesc.session_id().empty() ?
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000779 kSessionOriginSessionId : jdesc.session_id();
jbauch083b73f2015-07-16 02:46:32 -0700780 const std::string& session_version = jdesc.session_version().empty() ?
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000781 kSessionOriginSessionVersion : jdesc.session_version();
782 os << " " << session_id << " " << session_version << " "
783 << kSessionOriginNettype << " " << kSessionOriginAddrtype << " "
784 << kSessionOriginAddress;
785 AddLine(os.str(), &message);
786 AddLine(kSessionName, &message);
787
788 // Time Description.
789 AddLine(kTimeDescription, &message);
790
791 // Group
792 if (desc->HasGroup(cricket::GROUP_TYPE_BUNDLE)) {
793 std::string group_line = kAttrGroup;
794 const cricket::ContentGroup* group =
795 desc->GetGroupByName(cricket::GROUP_TYPE_BUNDLE);
nisseede5da42017-01-12 05:15:36 -0800796 RTC_DCHECK(group != NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000797 const cricket::ContentNames& content_names = group->content_names();
798 for (cricket::ContentNames::const_iterator it = content_names.begin();
799 it != content_names.end(); ++it) {
800 group_line.append(" ");
801 group_line.append(*it);
802 }
803 AddLine(group_line, &message);
804 }
805
806 // MediaStream semantics
807 InitAttrLine(kAttributeMsidSemantics, &os);
808 os << kSdpDelimiterColon << " " << kMediaStreamSemantic;
wu@webrtc.org4c3e9912014-07-16 21:03:13 +0000809
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000810 std::set<std::string> media_stream_labels;
811 const ContentInfo* audio_content = GetFirstAudioContent(desc);
812 if (audio_content)
813 GetMediaStreamLabels(audio_content, &media_stream_labels);
wu@webrtc.org4c3e9912014-07-16 21:03:13 +0000814
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000815 const ContentInfo* video_content = GetFirstVideoContent(desc);
816 if (video_content)
817 GetMediaStreamLabels(video_content, &media_stream_labels);
wu@webrtc.org4c3e9912014-07-16 21:03:13 +0000818
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000819 for (std::set<std::string>::const_iterator it =
820 media_stream_labels.begin(); it != media_stream_labels.end(); ++it) {
821 os << " " << *it;
822 }
823 AddLine(os.str(), &message);
824
wu@webrtc.org4c3e9912014-07-16 21:03:13 +0000825 // Preserve the order of the media contents.
826 int mline_index = -1;
827 for (cricket::ContentInfos::const_iterator it = desc->contents().begin();
828 it != desc->contents().end(); ++it) {
Steve Antonb1c1de12017-12-21 15:14:30 -0800829 const MediaContentDescription* mdesc = it->media_description();
wu@webrtc.org4c3e9912014-07-16 21:03:13 +0000830 std::vector<Candidate> candidates;
831 GetCandidatesByMindex(jdesc, ++mline_index, &candidates);
deadbeef9d3584c2016-02-16 17:54:10 -0800832 BuildMediaDescription(&*it, desc->GetTransportInfoByName(it->name),
Steve Antone831b8c2018-02-01 12:22:16 -0800833 mdesc->type(), candidates, desc->msid_signaling(),
wu@webrtc.org4c3e9912014-07-16 21:03:13 +0000834 &message);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000835 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000836 return message;
837}
838
839// Serializes the passed in IceCandidateInterface to a SDP string.
840// candidate - The candidate to be serialized.
Honghai Zhang7fb69db2016-03-14 11:59:18 -0700841std::string SdpSerializeCandidate(const IceCandidateInterface& candidate) {
842 return SdpSerializeCandidate(candidate.candidate());
843}
844
845// Serializes a cricket Candidate.
846std::string SdpSerializeCandidate(const cricket::Candidate& candidate) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000847 std::string message;
Honghai Zhang7fb69db2016-03-14 11:59:18 -0700848 std::vector<cricket::Candidate> candidates(1, candidate);
honghaiza54a0802015-12-16 18:37:23 -0800849 BuildCandidate(candidates, true, &message);
wu@webrtc.orgec9f5fb2014-06-24 17:05:10 +0000850 // From WebRTC draft section 4.8.1.1 candidate-attribute will be
851 // just candidate:<candidate> not a=candidate:<blah>CRLF
nisseede5da42017-01-12 05:15:36 -0800852 RTC_DCHECK(message.find("a=") == 0);
wu@webrtc.orgec9f5fb2014-06-24 17:05:10 +0000853 message.erase(0, 2);
nisseede5da42017-01-12 05:15:36 -0800854 RTC_DCHECK(message.find(kLineBreak) == message.size() - 2);
wu@webrtc.orgec9f5fb2014-06-24 17:05:10 +0000855 message.resize(message.size() - 2);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000856 return message;
857}
858
859bool SdpDeserialize(const std::string& message,
860 JsepSessionDescription* jdesc,
861 SdpParseError* error) {
862 std::string session_id;
863 std::string session_version;
Peter Thatcher7cbd1882015-09-17 18:54:52 -0700864 TransportDescription session_td("", "");
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000865 RtpHeaderExtensions session_extmaps;
zhihuang38989e52017-03-21 11:04:53 -0700866 rtc::SocketAddress session_connection_addr;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000867 cricket::SessionDescription* desc = new cricket::SessionDescription();
868 std::vector<JsepIceCandidate*> candidates;
869 size_t current_pos = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000870
871 // Session Description
872 if (!ParseSessionDescription(message, &current_pos, &session_id,
deadbeefc80741f2015-10-22 13:14:45 -0700873 &session_version, &session_td, &session_extmaps,
zhihuang38989e52017-03-21 11:04:53 -0700874 &session_connection_addr, desc, error)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000875 delete desc;
876 return false;
877 }
878
879 // Media Description
deadbeefc80741f2015-10-22 13:14:45 -0700880 if (!ParseMediaDescription(message, session_td, session_extmaps, &current_pos,
zhihuang38989e52017-03-21 11:04:53 -0700881 session_connection_addr, desc, &candidates,
882 error)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000883 delete desc;
884 for (std::vector<JsepIceCandidate*>::const_iterator
885 it = candidates.begin(); it != candidates.end(); ++it) {
886 delete *it;
887 }
888 return false;
889 }
890
891 jdesc->Initialize(desc, session_id, session_version);
892
893 for (std::vector<JsepIceCandidate*>::const_iterator
894 it = candidates.begin(); it != candidates.end(); ++it) {
895 jdesc->AddCandidate(*it);
896 delete *it;
897 }
898 return true;
899}
900
901bool SdpDeserializeCandidate(const std::string& message,
902 JsepIceCandidate* jcandidate,
903 SdpParseError* error) {
nisseede5da42017-01-12 05:15:36 -0800904 RTC_DCHECK(jcandidate != NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000905 Candidate candidate;
906 if (!ParseCandidate(message, &candidate, error, true)) {
907 return false;
908 }
909 jcandidate->SetCandidate(candidate);
910 return true;
911}
912
Honghai Zhang7fb69db2016-03-14 11:59:18 -0700913bool SdpDeserializeCandidate(const std::string& transport_name,
914 const std::string& message,
915 cricket::Candidate* candidate,
916 SdpParseError* error) {
nisseede5da42017-01-12 05:15:36 -0800917 RTC_DCHECK(candidate != nullptr);
Honghai Zhang7fb69db2016-03-14 11:59:18 -0700918 if (!ParseCandidate(message, candidate, error, true)) {
919 return false;
920 }
921 candidate->set_transport_name(transport_name);
922 return true;
923}
924
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000925bool ParseCandidate(const std::string& message, Candidate* candidate,
926 SdpParseError* error, bool is_raw) {
nisseede5da42017-01-12 05:15:36 -0800927 RTC_DCHECK(candidate != NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000928
929 // Get the first line from |message|.
jiayl@webrtc.org7ec3f9f2014-08-08 23:09:15 +0000930 std::string first_line = message;
931 size_t pos = 0;
932 GetLine(message, &pos, &first_line);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000933
jiayl@webrtc.org7ec3f9f2014-08-08 23:09:15 +0000934 // Makes sure |message| contains only one line.
935 if (message.size() > first_line.size()) {
936 std::string left, right;
Donald Curtis0e07f922015-05-15 09:21:23 -0700937 if (rtc::tokenize_first(message, kNewLine, &left, &right) &&
938 !right.empty()) {
jiayl@webrtc.org7ec3f9f2014-08-08 23:09:15 +0000939 return ParseFailed(message, 0, "Expect one line only", error);
940 }
941 }
942
943 // From WebRTC draft section 4.8.1.1 candidate-attribute should be
944 // candidate:<candidate> when trickled, but we still support
945 // a=candidate:<blah>CRLF for backward compatibility and for parsing a line
946 // from the SDP.
947 if (IsLineType(first_line, kLineTypeAttributes)) {
948 first_line = first_line.substr(kLinePrefixLength);
949 }
950
951 std::string attribute_candidate;
952 std::string candidate_value;
953
954 // |first_line| must be in the form of "candidate:<value>".
Donald Curtis144d0182015-05-15 13:14:24 -0700955 if (!rtc::tokenize_first(first_line, kSdpDelimiterColon, &attribute_candidate,
956 &candidate_value) ||
jiayl@webrtc.org7ec3f9f2014-08-08 23:09:15 +0000957 attribute_candidate != kAttributeCandidate) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000958 if (is_raw) {
959 std::ostringstream description;
jiayl@webrtc.org7ec3f9f2014-08-08 23:09:15 +0000960 description << "Expect line: " << kAttributeCandidate
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000961 << ":" << "<candidate-str>";
962 return ParseFailed(first_line, 0, description.str(), error);
963 } else {
964 return ParseFailedExpectLine(first_line, 0, kLineTypeAttributes,
965 kAttributeCandidate, error);
966 }
967 }
968
969 std::vector<std::string> fields;
jiayl@webrtc.org7ec3f9f2014-08-08 23:09:15 +0000970 rtc::split(candidate_value, kSdpDelimiterSpace, &fields);
971
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000972 // RFC 5245
973 // a=candidate:<foundation> <component-id> <transport> <priority>
974 // <connection-address> <port> typ <candidate-types>
975 // [raddr <connection-address>] [rport <port>]
976 // *(SP extension-att-name SP extension-att-value)
977 const size_t expected_min_fields = 8;
978 if (fields.size() < expected_min_fields ||
979 (fields[6] != kAttributeCandidateTyp)) {
980 return ParseFailedExpectMinFieldNum(first_line, expected_min_fields, error);
981 }
jbauch083b73f2015-07-16 02:46:32 -0700982 const std::string& foundation = fields[0];
jiayl@webrtc.org7ec3f9f2014-08-08 23:09:15 +0000983
wu@webrtc.org5e760e72014-04-02 23:19:09 +0000984 int component_id = 0;
985 if (!GetValueFromString(first_line, fields[1], &component_id, error)) {
986 return false;
987 }
jbauch083b73f2015-07-16 02:46:32 -0700988 const std::string& transport = fields[2];
Peter Boström0c4e06b2015-10-07 12:23:21 +0200989 uint32_t priority = 0;
wu@webrtc.org5e760e72014-04-02 23:19:09 +0000990 if (!GetValueFromString(first_line, fields[3], &priority, error)) {
991 return false;
992 }
jbauch083b73f2015-07-16 02:46:32 -0700993 const std::string& connection_address = fields[4];
wu@webrtc.org5e760e72014-04-02 23:19:09 +0000994 int port = 0;
995 if (!GetValueFromString(first_line, fields[5], &port, error)) {
996 return false;
997 }
deadbeef90f1e1e2017-02-10 12:35:05 -0800998 if (!IsValidPort(port)) {
999 return ParseFailed(first_line, "Invalid port number.", error);
1000 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001001 SocketAddress address(connection_address, port);
1002
1003 cricket::ProtocolType protocol;
hnslb68cc752016-12-13 10:33:41 -08001004 if (!StringToProto(transport.c_str(), &protocol)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001005 return ParseFailed(first_line, "Unsupported transport type.", error);
1006 }
hnslb68cc752016-12-13 10:33:41 -08001007 switch (protocol) {
1008 case cricket::PROTO_UDP:
1009 case cricket::PROTO_TCP:
1010 case cricket::PROTO_SSLTCP:
1011 // Supported protocol.
1012 break;
1013 default:
1014 return ParseFailed(first_line, "Unsupported transport type.", error);
1015 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001016
1017 std::string candidate_type;
jbauch083b73f2015-07-16 02:46:32 -07001018 const std::string& type = fields[7];
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001019 if (type == kCandidateHost) {
1020 candidate_type = cricket::LOCAL_PORT_TYPE;
1021 } else if (type == kCandidateSrflx) {
1022 candidate_type = cricket::STUN_PORT_TYPE;
1023 } else if (type == kCandidateRelay) {
1024 candidate_type = cricket::RELAY_PORT_TYPE;
Honghai Zhang7fb69db2016-03-14 11:59:18 -07001025 } else if (type == kCandidatePrflx) {
1026 candidate_type = cricket::PRFLX_PORT_TYPE;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001027 } else {
1028 return ParseFailed(first_line, "Unsupported candidate type.", error);
1029 }
1030
1031 size_t current_position = expected_min_fields;
1032 SocketAddress related_address;
1033 // The 2 optional fields for related address
1034 // [raddr <connection-address>] [rport <port>]
1035 if (fields.size() >= (current_position + 2) &&
1036 fields[current_position] == kAttributeCandidateRaddr) {
1037 related_address.SetIP(fields[++current_position]);
1038 ++current_position;
1039 }
1040 if (fields.size() >= (current_position + 2) &&
1041 fields[current_position] == kAttributeCandidateRport) {
wu@webrtc.org5e760e72014-04-02 23:19:09 +00001042 int port = 0;
1043 if (!GetValueFromString(
1044 first_line, fields[++current_position], &port, error)) {
1045 return false;
1046 }
deadbeef90f1e1e2017-02-10 12:35:05 -08001047 if (!IsValidPort(port)) {
1048 return ParseFailed(first_line, "Invalid port number.", error);
1049 }
wu@webrtc.org5e760e72014-04-02 23:19:09 +00001050 related_address.SetPort(port);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001051 ++current_position;
1052 }
1053
mallinath@webrtc.org2d60c5e2014-08-08 22:29:20 +00001054 // If this is a TCP candidate, it has additional extension as defined in
1055 // RFC 6544.
1056 std::string tcptype;
1057 if (fields.size() >= (current_position + 2) &&
1058 fields[current_position] == kTcpCandidateType) {
1059 tcptype = fields[++current_position];
1060 ++current_position;
1061
1062 if (tcptype != cricket::TCPTYPE_ACTIVE_STR &&
1063 tcptype != cricket::TCPTYPE_PASSIVE_STR &&
1064 tcptype != cricket::TCPTYPE_SIMOPEN_STR) {
1065 return ParseFailed(first_line, "Invalid TCP candidate type.", error);
1066 }
1067
1068 if (protocol != cricket::PROTO_TCP) {
1069 return ParseFailed(first_line, "Invalid non-TCP candidate", error);
1070 }
1071 }
1072
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001073 // Extension
honghaiza54a0802015-12-16 18:37:23 -08001074 // Though non-standard, we support the ICE ufrag and pwd being signaled on
1075 // the candidate to avoid issues with confusing which generation a candidate
1076 // belongs to when trickling multiple generations at the same time.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001077 std::string username;
1078 std::string password;
Peter Boström0c4e06b2015-10-07 12:23:21 +02001079 uint32_t generation = 0;
honghaiza0c44ea2016-03-23 16:07:48 -07001080 uint16_t network_id = 0;
1081 uint16_t network_cost = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001082 for (size_t i = current_position; i + 1 < fields.size(); ++i) {
1083 // RFC 5245
1084 // *(SP extension-att-name SP extension-att-value)
1085 if (fields[i] == kAttributeCandidateGeneration) {
wu@webrtc.org5e760e72014-04-02 23:19:09 +00001086 if (!GetValueFromString(first_line, fields[++i], &generation, error)) {
1087 return false;
1088 }
honghaiza54a0802015-12-16 18:37:23 -08001089 } else if (fields[i] == kAttributeCandidateUfrag) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001090 username = fields[++i];
honghaiza54a0802015-12-16 18:37:23 -08001091 } else if (fields[i] == kAttributeCandidatePwd) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001092 password = fields[++i];
honghaiza0c44ea2016-03-23 16:07:48 -07001093 } else if (fields[i] == kAttributeCandidateNetworkId) {
1094 if (!GetValueFromString(first_line, fields[++i], &network_id, error)) {
1095 return false;
1096 }
honghaize1a0c942016-02-16 14:54:56 -08001097 } else if (fields[i] == kAttributeCandidateNetworkCost) {
1098 if (!GetValueFromString(first_line, fields[++i], &network_cost, error)) {
1099 return false;
1100 }
Honghai Zhang351d77b2016-05-20 15:08:29 -07001101 network_cost = std::min(network_cost, rtc::kNetworkCostMax);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001102 } else {
1103 // Skip the unknown extension.
1104 ++i;
1105 }
1106 }
1107
guoweis@webrtc.org61c12472015-01-15 06:53:07 +00001108 *candidate = Candidate(component_id, cricket::ProtoToString(protocol),
guoweis@webrtc.org950c5182014-12-16 23:01:31 +00001109 address, priority, username, password, candidate_type,
honghaiza0c44ea2016-03-23 16:07:48 -07001110 generation, foundation, network_id, network_cost);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001111 candidate->set_related_address(related_address);
mallinath@webrtc.org2d60c5e2014-08-08 22:29:20 +00001112 candidate->set_tcptype(tcptype);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001113 return true;
1114}
1115
1116bool ParseIceOptions(const std::string& line,
1117 std::vector<std::string>* transport_options,
1118 SdpParseError* error) {
1119 std::string ice_options;
1120 if (!GetValue(line, kAttributeIceOption, &ice_options, error)) {
1121 return false;
1122 }
1123 std::vector<std::string> fields;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001124 rtc::split(ice_options, kSdpDelimiterSpace, &fields);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001125 for (size_t i = 0; i < fields.size(); ++i) {
1126 transport_options->push_back(fields[i]);
1127 }
1128 return true;
1129}
1130
jiayl@webrtc.orgddb85ab2014-09-05 16:31:56 +00001131bool ParseSctpPort(const std::string& line,
1132 int* sctp_port,
1133 SdpParseError* error) {
1134 // draft-ietf-mmusic-sctp-sdp-07
1135 // a=sctp-port
1136 std::vector<std::string> fields;
jiayl@webrtc.orgddb85ab2014-09-05 16:31:56 +00001137 const size_t expected_min_fields = 2;
lally69f57602015-10-08 10:15:04 -07001138 rtc::split(line.substr(kLinePrefixLength), kSdpDelimiterColon, &fields);
1139 if (fields.size() < expected_min_fields) {
1140 fields.resize(0);
1141 rtc::split(line.substr(kLinePrefixLength), kSdpDelimiterSpace, &fields);
1142 }
jiayl@webrtc.orgddb85ab2014-09-05 16:31:56 +00001143 if (fields.size() < expected_min_fields) {
1144 return ParseFailedExpectMinFieldNum(line, expected_min_fields, error);
1145 }
1146 if (!rtc::FromString(fields[1], sctp_port)) {
lally69f57602015-10-08 10:15:04 -07001147 return ParseFailed(line, "Invalid sctp port value.", error);
jiayl@webrtc.orgddb85ab2014-09-05 16:31:56 +00001148 }
1149 return true;
1150}
1151
isheriff6f8d6862016-05-26 11:24:55 -07001152bool ParseExtmap(const std::string& line,
1153 RtpExtension* extmap,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001154 SdpParseError* error) {
1155 // RFC 5285
1156 // a=extmap:<value>["/"<direction>] <URI> <extensionattributes>
1157 std::vector<std::string> fields;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001158 rtc::split(line.substr(kLinePrefixLength),
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001159 kSdpDelimiterSpace, &fields);
1160 const size_t expected_min_fields = 2;
1161 if (fields.size() < expected_min_fields) {
1162 return ParseFailedExpectMinFieldNum(line, expected_min_fields, error);
1163 }
1164 std::string uri = fields[1];
1165
1166 std::string value_direction;
1167 if (!GetValue(fields[0], kAttributeExtmap, &value_direction, error)) {
1168 return false;
1169 }
1170 std::vector<std::string> sub_fields;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001171 rtc::split(value_direction, kSdpDelimiterSlash, &sub_fields);
wu@webrtc.org5e760e72014-04-02 23:19:09 +00001172 int value = 0;
1173 if (!GetValueFromString(line, sub_fields[0], &value, error)) {
1174 return false;
1175 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001176
jbauch5869f502017-06-29 12:31:36 -07001177 bool encrypted = false;
1178 if (uri == RtpExtension::kEncryptHeaderExtensionsUri) {
1179 // RFC 6904
Steve Anton36b29d12017-10-30 09:57:42 -07001180 // a=extmap:<value["/"<direction>] urn:ietf:params:rtp-hdrext:encrypt <URI>
1181 // <extensionattributes>
jbauch5869f502017-06-29 12:31:36 -07001182 const size_t expected_min_fields_encrypted = expected_min_fields + 1;
1183 if (fields.size() < expected_min_fields_encrypted) {
1184 return ParseFailedExpectMinFieldNum(line, expected_min_fields_encrypted,
1185 error);
1186 }
1187
1188 encrypted = true;
1189 uri = fields[2];
1190 if (uri == RtpExtension::kEncryptHeaderExtensionsUri) {
1191 return ParseFailed(line, "Recursive encrypted header.", error);
1192 }
1193 }
1194
1195 *extmap = RtpExtension(uri, value, encrypted);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001196 return true;
1197}
1198
1199void BuildMediaDescription(const ContentInfo* content_info,
1200 const TransportInfo* transport_info,
1201 const MediaType media_type,
wu@webrtc.org4c3e9912014-07-16 21:03:13 +00001202 const std::vector<Candidate>& candidates,
Steve Antone831b8c2018-02-01 12:22:16 -08001203 int msid_signaling,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001204 std::string* message) {
nisseede5da42017-01-12 05:15:36 -08001205 RTC_DCHECK(message != NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001206 if (content_info == NULL || message == NULL) {
1207 return;
1208 }
Steve Anton36b29d12017-10-30 09:57:42 -07001209 // TODO(deadbeef): Rethink if we should use sprintfn instead of stringstream.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001210 // According to the style guide, streams should only be used for logging.
1211 // http://google-styleguide.googlecode.com/svn/
1212 // trunk/cppguide.xml?showone=Streams#Streams
1213 std::ostringstream os;
Steve Antonb1c1de12017-12-21 15:14:30 -08001214 const MediaContentDescription* media_desc = content_info->media_description();
1215 RTC_DCHECK(media_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001216
jiayl@webrtc.org9c16c392014-05-01 18:30:30 +00001217 int sctp_port = cricket::kSctpDefaultPort;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001218
1219 // RFC 4566
1220 // m=<media> <port> <proto> <fmt>
1221 // fmt is a list of payload type numbers that MAY be used in the session.
1222 const char* type = NULL;
1223 if (media_type == cricket::MEDIA_TYPE_AUDIO)
1224 type = kMediaTypeAudio;
1225 else if (media_type == cricket::MEDIA_TYPE_VIDEO)
1226 type = kMediaTypeVideo;
1227 else if (media_type == cricket::MEDIA_TYPE_DATA)
1228 type = kMediaTypeData;
1229 else
nissec80e7412017-01-11 05:56:46 -08001230 RTC_NOTREACHED();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001231
1232 std::string fmt;
1233 if (media_type == cricket::MEDIA_TYPE_VIDEO) {
Steve Antonb1c1de12017-12-21 15:14:30 -08001234 const VideoContentDescription* video_desc = media_desc->as_video();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001235 for (std::vector<cricket::VideoCodec>::const_iterator it =
1236 video_desc->codecs().begin();
1237 it != video_desc->codecs().end(); ++it) {
1238 fmt.append(" ");
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001239 fmt.append(rtc::ToString<int>(it->id));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001240 }
1241 } else if (media_type == cricket::MEDIA_TYPE_AUDIO) {
Steve Antonb1c1de12017-12-21 15:14:30 -08001242 const AudioContentDescription* audio_desc = media_desc->as_audio();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001243 for (std::vector<cricket::AudioCodec>::const_iterator it =
1244 audio_desc->codecs().begin();
1245 it != audio_desc->codecs().end(); ++it) {
1246 fmt.append(" ");
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001247 fmt.append(rtc::ToString<int>(it->id));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001248 }
1249 } else if (media_type == cricket::MEDIA_TYPE_DATA) {
Steve Antonb1c1de12017-12-21 15:14:30 -08001250 const DataContentDescription* data_desc = media_desc->as_data();
pthatcher@webrtc.org3341b402015-02-13 21:14:22 +00001251 if (IsDtlsSctp(media_desc->protocol())) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001252 fmt.append(" ");
jiayl@webrtc.org9c16c392014-05-01 18:30:30 +00001253
zstein4b2e0822017-02-17 19:48:38 -08001254 if (data_desc->use_sctpmap()) {
1255 for (std::vector<cricket::DataCodec>::const_iterator it =
1256 data_desc->codecs().begin();
1257 it != data_desc->codecs().end(); ++it) {
1258 if (cricket::CodecNamesEq(it->name,
1259 cricket::kGoogleSctpDataCodecName) &&
1260 it->GetParam(cricket::kCodecParamPort, &sctp_port)) {
1261 break;
1262 }
jiayl@webrtc.org9c16c392014-05-01 18:30:30 +00001263 }
jiayl@webrtc.org9c16c392014-05-01 18:30:30 +00001264
zstein4b2e0822017-02-17 19:48:38 -08001265 fmt.append(rtc::ToString<int>(sctp_port));
1266 } else {
1267 fmt.append(kDefaultSctpmapProtocol);
1268 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001269 } else {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001270 for (std::vector<cricket::DataCodec>::const_iterator it =
1271 data_desc->codecs().begin();
1272 it != data_desc->codecs().end(); ++it) {
1273 fmt.append(" ");
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001274 fmt.append(rtc::ToString<int>(it->id));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001275 }
1276 }
1277 }
1278 // The fmt must never be empty. If no codecs are found, set the fmt attribute
1279 // to 0.
1280 if (fmt.empty()) {
1281 fmt = " 0";
1282 }
1283
deadbeef25ed4352016-12-12 18:37:36 -08001284 // The port number in the m line will be updated later when associated with
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001285 // the candidates.
deadbeef25ed4352016-12-12 18:37:36 -08001286 //
1287 // A port value of 0 indicates that the m= section is rejected.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001288 // RFC 3264
1289 // To reject an offered stream, the port number in the corresponding stream in
1290 // the answer MUST be set to zero.
deadbeef25ed4352016-12-12 18:37:36 -08001291 //
1292 // However, the BUNDLE draft adds a new meaning to port zero, when used along
1293 // with a=bundle-only.
zhihuang38989e52017-03-21 11:04:53 -07001294 std::string port = kDummyPort;
1295 if (content_info->rejected || content_info->bundle_only) {
1296 port = kMediaPortRejected;
1297 } else if (!media_desc->connection_address().IsNil()) {
1298 port = rtc::ToString(media_desc->connection_address().port());
1299 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001300
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001301 rtc::SSLFingerprint* fp = (transport_info) ?
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001302 transport_info->description.identity_fingerprint.get() : NULL;
1303
wu@webrtc.org4c3e9912014-07-16 21:03:13 +00001304 // Add the m and c lines.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001305 InitLine(kLineTypeMedia, type, &os);
1306 os << " " << port << " " << media_desc->protocol() << fmt;
zhihuang38989e52017-03-21 11:04:53 -07001307 AddLine(os.str(), message);
1308
1309 InitLine(kLineTypeConnection, kConnectionNettype, &os);
1310 if (media_desc->connection_address().IsNil()) {
1311 os << " " << kConnectionIpv4Addrtype << " " << kDummyAddress;
1312 } else if (media_desc->connection_address().family() == AF_INET) {
1313 os << " " << kConnectionIpv4Addrtype << " "
1314 << media_desc->connection_address().ipaddr().ToString();
1315 } else {
1316 os << " " << kConnectionIpv6Addrtype << " "
1317 << media_desc->connection_address().ipaddr().ToString();
1318 }
1319 AddLine(os.str(), message);
wu@webrtc.org4c3e9912014-07-16 21:03:13 +00001320
1321 // RFC 4566
1322 // b=AS:<bandwidth>
Peter Thatcherc0c3a862015-06-24 15:31:25 -07001323 if (media_desc->bandwidth() >= 1000) {
wu@webrtc.org4c3e9912014-07-16 21:03:13 +00001324 InitLine(kLineTypeSessionBandwidth, kApplicationSpecificMaximum, &os);
1325 os << kSdpDelimiterColon << (media_desc->bandwidth() / 1000);
1326 AddLine(os.str(), message);
1327 }
1328
deadbeef25ed4352016-12-12 18:37:36 -08001329 // Add the a=bundle-only line.
1330 if (content_info->bundle_only) {
1331 InitAttrLine(kAttributeBundleOnly, &os);
1332 AddLine(os.str(), message);
1333 }
1334
wu@webrtc.org4c3e9912014-07-16 21:03:13 +00001335 // Add the a=rtcp line.
pthatcher@webrtc.org3341b402015-02-13 21:14:22 +00001336 if (IsRtp(media_desc->protocol())) {
wu@webrtc.org4c3e9912014-07-16 21:03:13 +00001337 std::string rtcp_line = GetRtcpLine(candidates);
1338 if (!rtcp_line.empty()) {
1339 AddLine(rtcp_line, message);
1340 }
1341 }
1342
honghaiza54a0802015-12-16 18:37:23 -08001343 // Build the a=candidate lines. We don't include ufrag and pwd in the
1344 // candidates in the SDP to avoid redundancy.
1345 BuildCandidate(candidates, false, message);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001346
1347 // Use the transport_info to build the media level ice-ufrag and ice-pwd.
1348 if (transport_info) {
1349 // RFC 5245
1350 // ice-pwd-att = "ice-pwd" ":" password
1351 // ice-ufrag-att = "ice-ufrag" ":" ufrag
1352 // ice-ufrag
deadbeef3f7219b2015-12-28 15:17:14 -08001353 if (!transport_info->description.ice_ufrag.empty()) {
1354 InitAttrLine(kAttributeIceUfrag, &os);
1355 os << kSdpDelimiterColon << transport_info->description.ice_ufrag;
1356 AddLine(os.str(), message);
1357 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001358 // ice-pwd
deadbeef3f7219b2015-12-28 15:17:14 -08001359 if (!transport_info->description.ice_pwd.empty()) {
1360 InitAttrLine(kAttributeIcePwd, &os);
1361 os << kSdpDelimiterColon << transport_info->description.ice_pwd;
1362 AddLine(os.str(), message);
1363 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001364
1365 // draft-petithuguenin-mmusic-ice-attributes-level-03
1366 BuildIceOptions(transport_info->description.transport_options, message);
1367
1368 // RFC 4572
1369 // fingerprint-attribute =
1370 // "fingerprint" ":" hash-func SP fingerprint
1371 if (fp) {
1372 // Insert the fingerprint attribute.
1373 InitAttrLine(kAttributeFingerprint, &os);
1374 os << kSdpDelimiterColon
1375 << fp->algorithm << kSdpDelimiterSpace
1376 << fp->GetRfc4572Fingerprint();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001377 AddLine(os.str(), message);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001378
1379 // Inserting setup attribute.
1380 if (transport_info->description.connection_role !=
1381 cricket::CONNECTIONROLE_NONE) {
1382 // Making sure we are not using "passive" mode.
1383 cricket::ConnectionRole role =
1384 transport_info->description.connection_role;
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00001385 std::string dtls_role_str;
nissec16fa5e2017-02-07 07:18:43 -08001386 const bool success =
1387 cricket::ConnectionRoleToString(role, &dtls_role_str);
1388 RTC_DCHECK(success);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001389 InitAttrLine(kAttributeSetup, &os);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001390 os << kSdpDelimiterColon << dtls_role_str;
1391 AddLine(os.str(), message);
1392 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001393 }
1394 }
1395
1396 // RFC 3388
1397 // mid-attribute = "a=mid:" identification-tag
1398 // identification-tag = token
1399 // Use the content name as the mid identification-tag.
1400 InitAttrLine(kAttributeMid, &os);
1401 os << kSdpDelimiterColon << content_info->name;
1402 AddLine(os.str(), message);
1403
pthatcher@webrtc.org3341b402015-02-13 21:14:22 +00001404 if (IsDtlsSctp(media_desc->protocol())) {
Steve Antonb1c1de12017-12-21 15:14:30 -08001405 const DataContentDescription* data_desc = media_desc->as_data();
zstein4b2e0822017-02-17 19:48:38 -08001406 bool use_sctpmap = data_desc->use_sctpmap();
1407 BuildSctpContentAttributes(message, sctp_port, use_sctpmap);
pthatcher@webrtc.org3341b402015-02-13 21:14:22 +00001408 } else if (IsRtp(media_desc->protocol())) {
Steve Antone831b8c2018-02-01 12:22:16 -08001409 BuildRtpContentAttributes(media_desc, media_type, msid_signaling, message);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001410 }
1411}
1412
zstein4b2e0822017-02-17 19:48:38 -08001413void BuildSctpContentAttributes(std::string* message,
1414 int sctp_port,
1415 bool use_sctpmap) {
wu@webrtc.org78187522013-10-07 23:32:02 +00001416 std::ostringstream os;
zstein4b2e0822017-02-17 19:48:38 -08001417 if (use_sctpmap) {
1418 // draft-ietf-mmusic-sctp-sdp-04
1419 // a=sctpmap:sctpmap-number protocol [streams]
1420 InitAttrLine(kAttributeSctpmap, &os);
1421 os << kSdpDelimiterColon << sctp_port << kSdpDelimiterSpace
1422 << kDefaultSctpmapProtocol << kSdpDelimiterSpace
1423 << cricket::kMaxSctpStreams;
1424 } else {
1425 // draft-ietf-mmusic-sctp-sdp-23
1426 // a=sctp-port:<port>
1427 InitAttrLine(kAttributeSctpPort, &os);
1428 os << kSdpDelimiterColon << sctp_port;
1429 // TODO(zstein): emit max-message-size here
1430 }
wu@webrtc.org78187522013-10-07 23:32:02 +00001431 AddLine(os.str(), message);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001432}
1433
deadbeef9d3584c2016-02-16 17:54:10 -08001434void BuildRtpContentAttributes(const MediaContentDescription* media_desc,
1435 const MediaType media_type,
Steve Antone831b8c2018-02-01 12:22:16 -08001436 int msid_signaling,
deadbeef9d3584c2016-02-16 17:54:10 -08001437 std::string* message) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001438 std::ostringstream os;
1439 // RFC 5285
1440 // a=extmap:<value>["/"<direction>] <URI> <extensionattributes>
1441 // The definitions MUST be either all session level or all media level. This
1442 // implementation uses all media level.
1443 for (size_t i = 0; i < media_desc->rtp_header_extensions().size(); ++i) {
jbauch5869f502017-06-29 12:31:36 -07001444 const RtpExtension& extension = media_desc->rtp_header_extensions()[i];
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001445 InitAttrLine(kAttributeExtmap, &os);
jbauch5869f502017-06-29 12:31:36 -07001446 os << kSdpDelimiterColon << extension.id;
1447 if (extension.encrypt) {
1448 os << kSdpDelimiterSpace << RtpExtension::kEncryptHeaderExtensionsUri;
1449 }
1450 os << kSdpDelimiterSpace << extension.uri;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001451 AddLine(os.str(), message);
1452 }
1453
1454 // RFC 3264
1455 // a=sendrecv || a=sendonly || a=sendrecv || a=inactive
deadbeefc80741f2015-10-22 13:14:45 -07001456 switch (media_desc->direction()) {
Steve Anton4e70a722017-11-28 14:57:10 -08001457 case RtpTransceiverDirection::kInactive:
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001458 InitAttrLine(kAttributeInactive, &os);
1459 break;
Steve Anton4e70a722017-11-28 14:57:10 -08001460 case RtpTransceiverDirection::kSendOnly:
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001461 InitAttrLine(kAttributeSendOnly, &os);
1462 break;
Steve Anton4e70a722017-11-28 14:57:10 -08001463 case RtpTransceiverDirection::kRecvOnly:
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001464 InitAttrLine(kAttributeRecvOnly, &os);
1465 break;
Steve Anton4e70a722017-11-28 14:57:10 -08001466 case RtpTransceiverDirection::kSendRecv:
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001467 default:
1468 InitAttrLine(kAttributeSendRecv, &os);
1469 break;
1470 }
1471 AddLine(os.str(), message);
1472
Steve Antone831b8c2018-02-01 12:22:16 -08001473 // Specified in https://tools.ietf.org/html/draft-ietf-mmusic-msid-16
deadbeef9d3584c2016-02-16 17:54:10 -08001474 // a=msid:<stream id> <track id>
Steve Antone831b8c2018-02-01 12:22:16 -08001475 if (msid_signaling & cricket::kMsidSignalingMediaSection) {
1476 const StreamParamsVec& streams = media_desc->streams();
1477 if (streams.size() == 1u) {
1478 const StreamParams& track = streams[0];
1479 const std::string& stream_id = track.sync_label;
deadbeef9d3584c2016-02-16 17:54:10 -08001480 InitAttrLine(kAttributeMsid, &os);
Steve Antone831b8c2018-02-01 12:22:16 -08001481 os << kSdpDelimiterColon << stream_id << kSdpDelimiterSpace << track.id;
deadbeef9d3584c2016-02-16 17:54:10 -08001482 AddLine(os.str(), message);
Steve Antone831b8c2018-02-01 12:22:16 -08001483 } else if (streams.size() > 1u) {
1484 RTC_LOG(LS_WARNING)
1485 << "Trying to serialize Unified Plan SDP with more than "
Jonas Olsson45cc8902018-02-13 10:37:07 +01001486 "one track in a media section. Omitting 'a=msid'.";
deadbeef9d3584c2016-02-16 17:54:10 -08001487 }
1488 }
1489
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001490 // RFC 5761
1491 // a=rtcp-mux
1492 if (media_desc->rtcp_mux()) {
1493 InitAttrLine(kAttributeRtcpMux, &os);
1494 AddLine(os.str(), message);
1495 }
1496
deadbeef13871492015-12-09 12:37:51 -08001497 // RFC 5506
1498 // a=rtcp-rsize
1499 if (media_desc->rtcp_reduced_size()) {
1500 InitAttrLine(kAttributeRtcpReducedSize, &os);
1501 AddLine(os.str(), message);
1502 }
1503
deadbeefd45aea82017-09-16 01:24:29 -07001504 if (media_desc->conference_mode()) {
1505 InitAttrLine(kAttributeXGoogleFlag, &os);
1506 os << kSdpDelimiterColon << kValueConference;
1507 AddLine(os.str(), message);
1508 }
1509
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001510 // RFC 4568
1511 // a=crypto:<tag> <crypto-suite> <key-params> [<session-params>]
1512 for (std::vector<CryptoParams>::const_iterator it =
1513 media_desc->cryptos().begin();
1514 it != media_desc->cryptos().end(); ++it) {
1515 InitAttrLine(kAttributeCrypto, &os);
1516 os << kSdpDelimiterColon << it->tag << " " << it->cipher_suite << " "
1517 << it->key_params;
1518 if (!it->session_params.empty()) {
1519 os << " " << it->session_params;
1520 }
1521 AddLine(os.str(), message);
1522 }
1523
1524 // RFC 4566
1525 // a=rtpmap:<payload type> <encoding name>/<clock rate>
1526 // [/<encodingparameters>]
1527 BuildRtpMap(media_desc, media_type, message);
1528
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001529 for (StreamParamsVec::const_iterator track = media_desc->streams().begin();
1530 track != media_desc->streams().end(); ++track) {
1531 // Require that the track belongs to a media stream,
1532 // ie the sync_label is set. This extra check is necessary since the
1533 // MediaContentDescription always contains a streamparam with an ssrc even
1534 // if no track or media stream have been created.
1535 if (track->sync_label.empty()) continue;
1536
1537 // Build the ssrc-group lines.
1538 for (size_t i = 0; i < track->ssrc_groups.size(); ++i) {
1539 // RFC 5576
1540 // a=ssrc-group:<semantics> <ssrc-id> ...
1541 if (track->ssrc_groups[i].ssrcs.empty()) {
1542 continue;
1543 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001544 InitAttrLine(kAttributeSsrcGroup, &os);
1545 os << kSdpDelimiterColon << track->ssrc_groups[i].semantics;
Peter Boström0c4e06b2015-10-07 12:23:21 +02001546 std::vector<uint32_t>::const_iterator ssrc =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001547 track->ssrc_groups[i].ssrcs.begin();
1548 for (; ssrc != track->ssrc_groups[i].ssrcs.end(); ++ssrc) {
Peter Boström0c4e06b2015-10-07 12:23:21 +02001549 os << kSdpDelimiterSpace << rtc::ToString<uint32_t>(*ssrc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001550 }
1551 AddLine(os.str(), message);
1552 }
1553 // Build the ssrc lines for each ssrc.
1554 for (size_t i = 0; i < track->ssrcs.size(); ++i) {
Peter Boström0c4e06b2015-10-07 12:23:21 +02001555 uint32_t ssrc = track->ssrcs[i];
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001556 // RFC 5576
1557 // a=ssrc:<ssrc-id> cname:<value>
1558 AddSsrcLine(ssrc, kSsrcAttributeCname,
1559 track->cname, message);
1560
Steve Antone831b8c2018-02-01 12:22:16 -08001561 if (msid_signaling & cricket::kMsidSignalingSsrcAttribute) {
1562 // draft-alvestrand-mmusic-msid-00
1563 // a=ssrc:<ssrc-id> msid:identifier [appdata]
1564 // The appdata consists of the "id" attribute of a MediaStreamTrack,
1565 // which corresponds to the "id" attribute of StreamParams.
1566 const std::string& stream_id = track->sync_label;
1567 InitAttrLine(kAttributeSsrc, &os);
1568 os << kSdpDelimiterColon << ssrc << kSdpDelimiterSpace
1569 << kSsrcAttributeMsid << kSdpDelimiterColon << stream_id
1570 << kSdpDelimiterSpace << track->id;
1571 AddLine(os.str(), message);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001572
Steve Antone831b8c2018-02-01 12:22:16 -08001573 // TODO(ronghuawu): Remove below code which is for backward
1574 // compatibility.
1575 // draft-alvestrand-rtcweb-mid-01
1576 // a=ssrc:<ssrc-id> mslabel:<value>
1577 // The label isn't yet defined.
1578 // a=ssrc:<ssrc-id> label:<value>
1579 AddSsrcLine(ssrc, kSsrcAttributeMslabel, track->sync_label, message);
1580 AddSsrcLine(ssrc, kSSrcAttributeLabel, track->id, message);
1581 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001582 }
1583 }
1584}
1585
1586void WriteFmtpHeader(int payload_type, std::ostringstream* os) {
1587 // fmtp header: a=fmtp:|payload_type| <parameters>
1588 // Add a=fmtp
1589 InitAttrLine(kAttributeFmtp, os);
1590 // Add :|payload_type|
1591 *os << kSdpDelimiterColon << payload_type;
1592}
1593
1594void WriteRtcpFbHeader(int payload_type, std::ostringstream* os) {
1595 // rtcp-fb header: a=rtcp-fb:|payload_type|
1596 // <parameters>/<ccm <ccm_parameters>>
1597 // Add a=rtcp-fb
1598 InitAttrLine(kAttributeRtcpFb, os);
1599 // Add :
1600 *os << kSdpDelimiterColon;
1601 if (payload_type == kWildcardPayloadType) {
1602 *os << "*";
1603 } else {
1604 *os << payload_type;
1605 }
1606}
1607
1608void WriteFmtpParameter(const std::string& parameter_name,
1609 const std::string& parameter_value,
1610 std::ostringstream* os) {
1611 // fmtp parameters: |parameter_name|=|parameter_value|
1612 *os << parameter_name << kSdpDelimiterEqual << parameter_value;
1613}
1614
1615void WriteFmtpParameters(const cricket::CodecParameterMap& parameters,
1616 std::ostringstream* os) {
1617 for (cricket::CodecParameterMap::const_iterator fmtp = parameters.begin();
1618 fmtp != parameters.end(); ++fmtp) {
hta62a216e2016-04-15 11:02:14 -07001619 // Parameters are a semicolon-separated list, no spaces.
1620 // The list is separated from the header by a space.
1621 if (fmtp == parameters.begin()) {
1622 *os << kSdpDelimiterSpace;
1623 } else {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001624 *os << kSdpDelimiterSemicolon;
1625 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001626 WriteFmtpParameter(fmtp->first, fmtp->second, os);
1627 }
1628}
1629
1630bool IsFmtpParam(const std::string& name) {
ossuaa4b0772017-01-30 07:41:18 -08001631 // RFC 4855, section 3 specifies the mapping of media format parameters to SDP
1632 // parameters. Only ptime, maxptime, channels and rate are placed outside of
1633 // the fmtp line. In WebRTC, channels and rate are already handled separately
1634 // and thus not included in the CodecParameterMap.
1635 return name != kCodecParamPTime && name != kCodecParamMaxPTime;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001636}
1637
1638// Retreives fmtp parameters from |params|, which may contain other parameters
1639// as well, and puts them in |fmtp_parameters|.
1640void GetFmtpParams(const cricket::CodecParameterMap& params,
1641 cricket::CodecParameterMap* fmtp_parameters) {
1642 for (cricket::CodecParameterMap::const_iterator iter = params.begin();
1643 iter != params.end(); ++iter) {
1644 if (IsFmtpParam(iter->first)) {
1645 (*fmtp_parameters)[iter->first] = iter->second;
1646 }
1647 }
1648}
1649
1650template <class T>
1651void AddFmtpLine(const T& codec, std::string* message) {
1652 cricket::CodecParameterMap fmtp_parameters;
1653 GetFmtpParams(codec.params, &fmtp_parameters);
1654 if (fmtp_parameters.empty()) {
1655 // No need to add an fmtp if it will have no (optional) parameters.
1656 return;
1657 }
1658 std::ostringstream os;
1659 WriteFmtpHeader(codec.id, &os);
1660 WriteFmtpParameters(fmtp_parameters, &os);
1661 AddLine(os.str(), message);
1662 return;
1663}
1664
1665template <class T>
1666void AddRtcpFbLines(const T& codec, std::string* message) {
1667 for (std::vector<cricket::FeedbackParam>::const_iterator iter =
1668 codec.feedback_params.params().begin();
1669 iter != codec.feedback_params.params().end(); ++iter) {
1670 std::ostringstream os;
1671 WriteRtcpFbHeader(codec.id, &os);
1672 os << " " << iter->id();
1673 if (!iter->param().empty()) {
1674 os << " " << iter->param();
1675 }
1676 AddLine(os.str(), message);
1677 }
1678}
1679
jiayl@webrtc.orgddb85ab2014-09-05 16:31:56 +00001680bool AddSctpDataCodec(DataContentDescription* media_desc,
1681 int sctp_port) {
solenberg9fa49752016-10-08 13:02:44 -07001682 for (const auto& codec : media_desc->codecs()) {
1683 if (cricket::CodecNamesEq(codec.name, cricket::kGoogleSctpDataCodecName)) {
1684 return ParseFailed("",
1685 "Can't have multiple sctp port attributes.",
1686 NULL);
1687 }
jiayl@webrtc.orgddb85ab2014-09-05 16:31:56 +00001688 }
1689 // Add the SCTP Port number as a pseudo-codec "port" parameter
solenberg9fa49752016-10-08 13:02:44 -07001690 cricket::DataCodec codec_port(cricket::kGoogleSctpDataCodecPlType,
deadbeef67cf2c12016-04-13 10:07:16 -07001691 cricket::kGoogleSctpDataCodecName);
jiayl@webrtc.orgddb85ab2014-09-05 16:31:56 +00001692 codec_port.SetParam(cricket::kCodecParamPort, sctp_port);
Mirko Bonadei675513b2017-11-09 11:09:25 +01001693 RTC_LOG(INFO) << "AddSctpDataCodec: Got SCTP Port Number " << sctp_port;
jiayl@webrtc.orgddb85ab2014-09-05 16:31:56 +00001694 media_desc->AddCodec(codec_port);
1695 return true;
1696}
1697
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001698bool GetMinValue(const std::vector<int>& values, int* value) {
1699 if (values.empty()) {
1700 return false;
1701 }
1702 std::vector<int>::const_iterator found =
1703 std::min_element(values.begin(), values.end());
1704 *value = *found;
1705 return true;
1706}
1707
1708bool GetParameter(const std::string& name,
1709 const cricket::CodecParameterMap& params, int* value) {
1710 std::map<std::string, std::string>::const_iterator found =
1711 params.find(name);
1712 if (found == params.end()) {
1713 return false;
1714 }
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001715 if (!rtc::FromString(found->second, value)) {
wu@webrtc.org5e760e72014-04-02 23:19:09 +00001716 return false;
1717 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001718 return true;
1719}
1720
1721void BuildRtpMap(const MediaContentDescription* media_desc,
1722 const MediaType media_type,
1723 std::string* message) {
nisseede5da42017-01-12 05:15:36 -08001724 RTC_DCHECK(message != NULL);
1725 RTC_DCHECK(media_desc != NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001726 std::ostringstream os;
1727 if (media_type == cricket::MEDIA_TYPE_VIDEO) {
Steve Antonb1c1de12017-12-21 15:14:30 -08001728 const VideoContentDescription* video_desc = media_desc->as_video();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001729 for (std::vector<cricket::VideoCodec>::const_iterator it =
1730 video_desc->codecs().begin();
1731 it != video_desc->codecs().end(); ++it) {
1732 // RFC 4566
1733 // a=rtpmap:<payload type> <encoding name>/<clock rate>
1734 // [/<encodingparameters>]
1735 if (it->id != kWildcardPayloadType) {
1736 InitAttrLine(kAttributeRtpmap, &os);
deadbeefe814a0d2017-02-25 18:15:09 -08001737 os << kSdpDelimiterColon << it->id << " " << it->name << "/"
1738 << cricket::kVideoCodecClockrate;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001739 AddLine(os.str(), message);
1740 }
1741 AddRtcpFbLines(*it, message);
1742 AddFmtpLine(*it, message);
1743 }
1744 } else if (media_type == cricket::MEDIA_TYPE_AUDIO) {
Steve Antonb1c1de12017-12-21 15:14:30 -08001745 const AudioContentDescription* audio_desc = media_desc->as_audio();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001746 std::vector<int> ptimes;
1747 std::vector<int> maxptimes;
1748 int max_minptime = 0;
1749 for (std::vector<cricket::AudioCodec>::const_iterator it =
1750 audio_desc->codecs().begin();
1751 it != audio_desc->codecs().end(); ++it) {
nisseede5da42017-01-12 05:15:36 -08001752 RTC_DCHECK(!it->name.empty());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001753 // RFC 4566
1754 // a=rtpmap:<payload type> <encoding name>/<clock rate>
1755 // [/<encodingparameters>]
1756 InitAttrLine(kAttributeRtpmap, &os);
1757 os << kSdpDelimiterColon << it->id << " ";
1758 os << it->name << "/" << it->clockrate;
1759 if (it->channels != 1) {
1760 os << "/" << it->channels;
1761 }
1762 AddLine(os.str(), message);
1763 AddRtcpFbLines(*it, message);
1764 AddFmtpLine(*it, message);
1765 int minptime = 0;
1766 if (GetParameter(kCodecParamMinPTime, it->params, &minptime)) {
1767 max_minptime = std::max(minptime, max_minptime);
1768 }
1769 int ptime;
1770 if (GetParameter(kCodecParamPTime, it->params, &ptime)) {
1771 ptimes.push_back(ptime);
1772 }
1773 int maxptime;
1774 if (GetParameter(kCodecParamMaxPTime, it->params, &maxptime)) {
1775 maxptimes.push_back(maxptime);
1776 }
1777 }
1778 // Populate the maxptime attribute with the smallest maxptime of all codecs
1779 // under the same m-line.
1780 int min_maxptime = INT_MAX;
1781 if (GetMinValue(maxptimes, &min_maxptime)) {
1782 AddAttributeLine(kCodecParamMaxPTime, min_maxptime, message);
1783 }
nisseede5da42017-01-12 05:15:36 -08001784 RTC_DCHECK(min_maxptime > max_minptime);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001785 // Populate the ptime attribute with the smallest ptime or the largest
1786 // minptime, whichever is the largest, for all codecs under the same m-line.
1787 int ptime = INT_MAX;
1788 if (GetMinValue(ptimes, &ptime)) {
1789 ptime = std::min(ptime, min_maxptime);
1790 ptime = std::max(ptime, max_minptime);
1791 AddAttributeLine(kCodecParamPTime, ptime, message);
1792 }
1793 } else if (media_type == cricket::MEDIA_TYPE_DATA) {
Steve Antonb1c1de12017-12-21 15:14:30 -08001794 const DataContentDescription* data_desc = media_desc->as_data();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001795 for (std::vector<cricket::DataCodec>::const_iterator it =
1796 data_desc->codecs().begin();
1797 it != data_desc->codecs().end(); ++it) {
1798 // RFC 4566
1799 // a=rtpmap:<payload type> <encoding name>/<clock rate>
1800 // [/<encodingparameters>]
1801 InitAttrLine(kAttributeRtpmap, &os);
1802 os << kSdpDelimiterColon << it->id << " "
1803 << it->name << "/" << it->clockrate;
1804 AddLine(os.str(), message);
1805 }
1806 }
1807}
1808
1809void BuildCandidate(const std::vector<Candidate>& candidates,
honghaiza54a0802015-12-16 18:37:23 -08001810 bool include_ufrag,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001811 std::string* message) {
1812 std::ostringstream os;
1813
1814 for (std::vector<Candidate>::const_iterator it = candidates.begin();
1815 it != candidates.end(); ++it) {
1816 // RFC 5245
1817 // a=candidate:<foundation> <component-id> <transport> <priority>
1818 // <connection-address> <port> typ <candidate-types>
1819 // [raddr <connection-address>] [rport <port>]
1820 // *(SP extension-att-name SP extension-att-value)
1821 std::string type;
1822 // Map the cricket candidate type to "host" / "srflx" / "prflx" / "relay"
1823 if (it->type() == cricket::LOCAL_PORT_TYPE) {
1824 type = kCandidateHost;
1825 } else if (it->type() == cricket::STUN_PORT_TYPE) {
1826 type = kCandidateSrflx;
1827 } else if (it->type() == cricket::RELAY_PORT_TYPE) {
1828 type = kCandidateRelay;
Honghai Zhang7fb69db2016-03-14 11:59:18 -07001829 } else if (it->type() == cricket::PRFLX_PORT_TYPE) {
1830 type = kCandidatePrflx;
1831 // Peer reflexive candidate may be signaled for being removed.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001832 } else {
nissec80e7412017-01-11 05:56:46 -08001833 RTC_NOTREACHED();
Peter Thatcher019087f2015-04-28 09:06:26 -07001834 // Never write out candidates if we don't know the type.
1835 continue;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001836 }
1837
1838 InitAttrLine(kAttributeCandidate, &os);
1839 os << kSdpDelimiterColon
mallinath@webrtc.org2d60c5e2014-08-08 22:29:20 +00001840 << it->foundation() << " "
1841 << it->component() << " "
1842 << it->protocol() << " "
1843 << it->priority() << " "
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001844 << it->address().ipaddr().ToString() << " "
1845 << it->address().PortAsString() << " "
mallinath@webrtc.org2d60c5e2014-08-08 22:29:20 +00001846 << kAttributeCandidateTyp << " "
1847 << type << " ";
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001848
1849 // Related address
1850 if (!it->related_address().IsNil()) {
1851 os << kAttributeCandidateRaddr << " "
1852 << it->related_address().ipaddr().ToString() << " "
1853 << kAttributeCandidateRport << " "
1854 << it->related_address().PortAsString() << " ";
1855 }
1856
mallinath@webrtc.org2d60c5e2014-08-08 22:29:20 +00001857 if (it->protocol() == cricket::TCP_PROTOCOL_NAME) {
mallinath@webrtc.orge999bd02014-08-13 06:05:55 +00001858 os << kTcpCandidateType << " " << it->tcptype() << " ";
mallinath@webrtc.org2d60c5e2014-08-08 22:29:20 +00001859 }
1860
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001861 // Extensions
1862 os << kAttributeCandidateGeneration << " " << it->generation();
honghaiza54a0802015-12-16 18:37:23 -08001863 if (include_ufrag && !it->username().empty()) {
1864 os << " " << kAttributeCandidateUfrag << " " << it->username();
1865 }
honghaiza0c44ea2016-03-23 16:07:48 -07001866 if (it->network_id() > 0) {
1867 os << " " << kAttributeCandidateNetworkId << " " << it->network_id();
1868 }
honghaize1a0c942016-02-16 14:54:56 -08001869 if (it->network_cost() > 0) {
1870 os << " " << kAttributeCandidateNetworkCost << " " << it->network_cost();
1871 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001872
1873 AddLine(os.str(), message);
1874 }
1875}
1876
1877void BuildIceOptions(const std::vector<std::string>& transport_options,
1878 std::string* message) {
1879 if (!transport_options.empty()) {
1880 std::ostringstream os;
1881 InitAttrLine(kAttributeIceOption, &os);
1882 os << kSdpDelimiterColon << transport_options[0];
1883 for (size_t i = 1; i < transport_options.size(); ++i) {
1884 os << kSdpDelimiterSpace << transport_options[i];
1885 }
1886 AddLine(os.str(), message);
1887 }
1888}
1889
pthatcher@webrtc.org3341b402015-02-13 21:14:22 +00001890bool IsRtp(const std::string& protocol) {
1891 return protocol.empty() ||
1892 (protocol.find(cricket::kMediaProtocolRtpPrefix) != std::string::npos);
1893}
1894
1895bool IsDtlsSctp(const std::string& protocol) {
1896 // This intentionally excludes "SCTP" and "SCTP/DTLS".
lally@webrtc.orga7470932015-02-24 20:19:21 +00001897 return protocol.find(cricket::kMediaProtocolDtlsSctp) != std::string::npos;
pthatcher@webrtc.org3341b402015-02-13 21:14:22 +00001898}
1899
zhihuang38989e52017-03-21 11:04:53 -07001900bool ParseConnectionData(const std::string& line,
1901 rtc::SocketAddress* addr,
1902 SdpParseError* error) {
1903 // Parse the line from left to right.
1904 std::string token;
1905 std::string rightpart;
1906 // RFC 4566
1907 // c=<nettype> <addrtype> <connection-address>
1908 // Skip the "c="
1909 if (!rtc::tokenize_first(line, kSdpDelimiterEqual, &token, &rightpart)) {
1910 return ParseFailed(line, "Failed to parse the network type.", error);
1911 }
1912
1913 // Extract and verify the <nettype>
1914 if (!rtc::tokenize_first(rightpart, kSdpDelimiterSpace, &token, &rightpart) ||
1915 token != kConnectionNettype) {
1916 return ParseFailed(line,
1917 "Failed to parse the connection data. The network type "
1918 "is not currently supported.",
1919 error);
1920 }
1921
1922 // Extract the "<addrtype>" and "<connection-address>".
1923 if (!rtc::tokenize_first(rightpart, kSdpDelimiterSpace, &token, &rightpart)) {
1924 return ParseFailed(line, "Failed to parse the address type.", error);
1925 }
1926
1927 // The rightpart part should be the IP address without the slash which is used
1928 // for multicast.
1929 if (rightpart.find('/') != std::string::npos) {
1930 return ParseFailed(line,
1931 "Failed to parse the connection data. Multicast is not "
1932 "currently supported.",
1933 error);
1934 }
1935 addr->SetIP(rightpart);
1936
1937 // Verify that the addrtype matches the type of the parsed address.
1938 if ((addr->family() == AF_INET && token != "IP4") ||
1939 (addr->family() == AF_INET6 && token != "IP6")) {
1940 addr->Clear();
1941 return ParseFailed(
1942 line,
1943 "Failed to parse the connection data. The address type is mismatching.",
1944 error);
1945 }
1946 return true;
1947}
1948
1949bool ParseSessionDescription(const std::string& message,
1950 size_t* pos,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001951 std::string* session_id,
1952 std::string* session_version,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001953 TransportDescription* session_td,
1954 RtpHeaderExtensions* session_extmaps,
zhihuang38989e52017-03-21 11:04:53 -07001955 rtc::SocketAddress* connection_addr,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001956 cricket::SessionDescription* desc,
1957 SdpParseError* error) {
1958 std::string line;
1959
deadbeefc80741f2015-10-22 13:14:45 -07001960 desc->set_msid_supported(false);
1961
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001962 // RFC 4566
1963 // v= (protocol version)
1964 if (!GetLineWithType(message, pos, &line, kLineTypeVersion)) {
1965 return ParseFailedExpectLine(message, *pos, kLineTypeVersion,
1966 std::string(), error);
1967 }
1968 // RFC 4566
1969 // o=<username> <sess-id> <sess-version> <nettype> <addrtype>
1970 // <unicast-address>
1971 if (!GetLineWithType(message, pos, &line, kLineTypeOrigin)) {
1972 return ParseFailedExpectLine(message, *pos, kLineTypeOrigin,
1973 std::string(), error);
1974 }
1975 std::vector<std::string> fields;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001976 rtc::split(line.substr(kLinePrefixLength),
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001977 kSdpDelimiterSpace, &fields);
1978 const size_t expected_fields = 6;
1979 if (fields.size() != expected_fields) {
1980 return ParseFailedExpectFieldNum(line, expected_fields, error);
1981 }
1982 *session_id = fields[1];
1983 *session_version = fields[2];
1984
1985 // RFC 4566
1986 // s= (session name)
1987 if (!GetLineWithType(message, pos, &line, kLineTypeSessionName)) {
1988 return ParseFailedExpectLine(message, *pos, kLineTypeSessionName,
1989 std::string(), error);
1990 }
1991
1992 // Optional lines
1993 // Those are the optional lines, so shouldn't return false if not present.
1994 // RFC 4566
1995 // i=* (session information)
1996 GetLineWithType(message, pos, &line, kLineTypeSessionInfo);
1997
1998 // RFC 4566
1999 // u=* (URI of description)
2000 GetLineWithType(message, pos, &line, kLineTypeSessionUri);
2001
2002 // RFC 4566
2003 // e=* (email address)
2004 GetLineWithType(message, pos, &line, kLineTypeSessionEmail);
2005
2006 // RFC 4566
2007 // p=* (phone number)
2008 GetLineWithType(message, pos, &line, kLineTypeSessionPhone);
2009
2010 // RFC 4566
2011 // c=* (connection information -- not required if included in
2012 // all media)
zhihuang38989e52017-03-21 11:04:53 -07002013 if (GetLineWithType(message, pos, &line, kLineTypeConnection)) {
2014 if (!ParseConnectionData(line, connection_addr, error)) {
2015 return false;
2016 }
2017 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002018
2019 // RFC 4566
2020 // b=* (zero or more bandwidth information lines)
2021 while (GetLineWithType(message, pos, &line, kLineTypeSessionBandwidth)) {
2022 // By pass zero or more b lines.
2023 }
2024
2025 // RFC 4566
2026 // One or more time descriptions ("t=" and "r=" lines; see below)
2027 // t= (time the session is active)
2028 // r=* (zero or more repeat times)
2029 // Ensure there's at least one time description
2030 if (!GetLineWithType(message, pos, &line, kLineTypeTiming)) {
2031 return ParseFailedExpectLine(message, *pos, kLineTypeTiming, std::string(),
2032 error);
2033 }
2034
2035 while (GetLineWithType(message, pos, &line, kLineTypeRepeatTimes)) {
2036 // By pass zero or more r lines.
2037 }
2038
2039 // Go through the rest of the time descriptions
2040 while (GetLineWithType(message, pos, &line, kLineTypeTiming)) {
2041 while (GetLineWithType(message, pos, &line, kLineTypeRepeatTimes)) {
2042 // By pass zero or more r lines.
2043 }
2044 }
2045
2046 // RFC 4566
2047 // z=* (time zone adjustments)
2048 GetLineWithType(message, pos, &line, kLineTypeTimeZone);
2049
2050 // RFC 4566
2051 // k=* (encryption key)
2052 GetLineWithType(message, pos, &line, kLineTypeEncryptionKey);
2053
2054 // RFC 4566
2055 // a=* (zero or more session attribute lines)
2056 while (GetLineWithType(message, pos, &line, kLineTypeAttributes)) {
2057 if (HasAttribute(line, kAttributeGroup)) {
2058 if (!ParseGroupAttribute(line, desc, error)) {
2059 return false;
2060 }
2061 } else if (HasAttribute(line, kAttributeIceUfrag)) {
2062 if (!GetValue(line, kAttributeIceUfrag,
2063 &(session_td->ice_ufrag), error)) {
2064 return false;
2065 }
2066 } else if (HasAttribute(line, kAttributeIcePwd)) {
2067 if (!GetValue(line, kAttributeIcePwd, &(session_td->ice_pwd), error)) {
2068 return false;
2069 }
2070 } else if (HasAttribute(line, kAttributeIceLite)) {
2071 session_td->ice_mode = cricket::ICEMODE_LITE;
2072 } else if (HasAttribute(line, kAttributeIceOption)) {
2073 if (!ParseIceOptions(line, &(session_td->transport_options), error)) {
2074 return false;
2075 }
2076 } else if (HasAttribute(line, kAttributeFingerprint)) {
2077 if (session_td->identity_fingerprint.get()) {
2078 return ParseFailed(
2079 line,
2080 "Can't have multiple fingerprint attributes at the same level.",
2081 error);
2082 }
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002083 rtc::SSLFingerprint* fingerprint = NULL;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002084 if (!ParseFingerprintAttribute(line, &fingerprint, error)) {
2085 return false;
2086 }
2087 session_td->identity_fingerprint.reset(fingerprint);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00002088 } else if (HasAttribute(line, kAttributeSetup)) {
2089 if (!ParseDtlsSetup(line, &(session_td->connection_role), error)) {
2090 return false;
2091 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002092 } else if (HasAttribute(line, kAttributeMsidSemantics)) {
2093 std::string semantics;
2094 if (!GetValue(line, kAttributeMsidSemantics, &semantics, error)) {
2095 return false;
2096 }
deadbeefc80741f2015-10-22 13:14:45 -07002097 desc->set_msid_supported(
2098 CaseInsensitiveFind(semantics, kMediaStreamSemantic));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002099 } else if (HasAttribute(line, kAttributeExtmap)) {
isheriff6f8d6862016-05-26 11:24:55 -07002100 RtpExtension extmap;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002101 if (!ParseExtmap(line, &extmap, error)) {
2102 return false;
2103 }
2104 session_extmaps->push_back(extmap);
2105 }
2106 }
2107
2108 return true;
2109}
2110
2111bool ParseGroupAttribute(const std::string& line,
2112 cricket::SessionDescription* desc,
2113 SdpParseError* error) {
nisseede5da42017-01-12 05:15:36 -08002114 RTC_DCHECK(desc != NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002115
2116 // RFC 5888 and draft-holmberg-mmusic-sdp-bundle-negotiation-00
2117 // a=group:BUNDLE video voice
2118 std::vector<std::string> fields;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002119 rtc::split(line.substr(kLinePrefixLength),
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002120 kSdpDelimiterSpace, &fields);
2121 std::string semantics;
2122 if (!GetValue(fields[0], kAttributeGroup, &semantics, error)) {
2123 return false;
2124 }
2125 cricket::ContentGroup group(semantics);
2126 for (size_t i = 1; i < fields.size(); ++i) {
2127 group.AddContentName(fields[i]);
2128 }
2129 desc->AddGroup(group);
2130 return true;
2131}
2132
2133static bool ParseFingerprintAttribute(const std::string& line,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002134 rtc::SSLFingerprint** fingerprint,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002135 SdpParseError* error) {
2136 if (!IsLineType(line, kLineTypeAttributes) ||
2137 !HasAttribute(line, kAttributeFingerprint)) {
2138 return ParseFailedExpectLine(line, 0, kLineTypeAttributes,
2139 kAttributeFingerprint, error);
2140 }
2141
2142 std::vector<std::string> fields;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002143 rtc::split(line.substr(kLinePrefixLength),
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002144 kSdpDelimiterSpace, &fields);
2145 const size_t expected_fields = 2;
2146 if (fields.size() != expected_fields) {
2147 return ParseFailedExpectFieldNum(line, expected_fields, error);
2148 }
2149
2150 // The first field here is "fingerprint:<hash>.
2151 std::string algorithm;
2152 if (!GetValue(fields[0], kAttributeFingerprint, &algorithm, error)) {
2153 return false;
2154 }
2155
2156 // Downcase the algorithm. Note that we don't need to downcase the
2157 // fingerprint because hex_decode can handle upper-case.
2158 std::transform(algorithm.begin(), algorithm.end(), algorithm.begin(),
2159 ::tolower);
2160
2161 // The second field is the digest value. De-hexify it.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002162 *fingerprint = rtc::SSLFingerprint::CreateFromRfc4572(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002163 algorithm, fields[1]);
2164 if (!*fingerprint) {
2165 return ParseFailed(line,
2166 "Failed to create fingerprint from the digest.",
2167 error);
2168 }
2169
2170 return true;
2171}
2172
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00002173static bool ParseDtlsSetup(const std::string& line,
2174 cricket::ConnectionRole* role,
2175 SdpParseError* error) {
2176 // setup-attr = "a=setup:" role
2177 // role = "active" / "passive" / "actpass" / "holdconn"
2178 std::vector<std::string> fields;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002179 rtc::split(line.substr(kLinePrefixLength), kSdpDelimiterColon, &fields);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00002180 const size_t expected_fields = 2;
2181 if (fields.size() != expected_fields) {
2182 return ParseFailedExpectFieldNum(line, expected_fields, error);
2183 }
2184 std::string role_str = fields[1];
2185 if (!cricket::StringToConnectionRole(role_str, role)) {
2186 return ParseFailed(line, "Invalid attribute value.", error);
2187 }
2188 return true;
2189}
2190
deadbeef9d3584c2016-02-16 17:54:10 -08002191static bool ParseMsidAttribute(const std::string& line,
2192 std::string* stream_id,
2193 std::string* track_id,
2194 SdpParseError* error) {
2195 // draft-ietf-mmusic-msid-11
2196 // a=msid:<stream id> <track id>
2197 // msid-value = msid-id [ SP msid-appdata ]
2198 // msid-id = 1*64token-char ; see RFC 4566
2199 // msid-appdata = 1*64token-char ; see RFC 4566
2200 std::string field1;
2201 if (!rtc::tokenize_first(line.substr(kLinePrefixLength), kSdpDelimiterSpace,
2202 &field1, track_id)) {
2203 const size_t expected_fields = 2;
2204 return ParseFailedExpectFieldNum(line, expected_fields, error);
2205 }
2206
deadbeefa4549d62017-02-10 17:26:22 -08002207 if (track_id->empty()) {
2208 return ParseFailed(line, "Missing track ID in msid attribute.", error);
2209 }
2210
deadbeef9d3584c2016-02-16 17:54:10 -08002211 // msid:<msid-id>
2212 if (!GetValue(field1, kAttributeMsid, stream_id, error)) {
2213 return false;
2214 }
deadbeefa4549d62017-02-10 17:26:22 -08002215 if (stream_id->empty()) {
2216 return ParseFailed(line, "Missing stream ID in msid attribute.", error);
2217 }
deadbeef9d3584c2016-02-16 17:54:10 -08002218 return true;
2219}
2220
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002221// RFC 3551
2222// PT encoding media type clock rate channels
2223// name (Hz)
2224// 0 PCMU A 8,000 1
2225// 1 reserved A
2226// 2 reserved A
2227// 3 GSM A 8,000 1
2228// 4 G723 A 8,000 1
2229// 5 DVI4 A 8,000 1
2230// 6 DVI4 A 16,000 1
2231// 7 LPC A 8,000 1
2232// 8 PCMA A 8,000 1
2233// 9 G722 A 8,000 1
2234// 10 L16 A 44,100 2
2235// 11 L16 A 44,100 1
2236// 12 QCELP A 8,000 1
2237// 13 CN A 8,000 1
2238// 14 MPA A 90,000 (see text)
2239// 15 G728 A 8,000 1
2240// 16 DVI4 A 11,025 1
2241// 17 DVI4 A 22,050 1
2242// 18 G729 A 8,000 1
2243struct StaticPayloadAudioCodec {
2244 const char* name;
2245 int clockrate;
Peter Kasting69558702016-01-12 16:26:35 -08002246 size_t channels;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002247};
2248static const StaticPayloadAudioCodec kStaticPayloadAudioCodecs[] = {
2249 { "PCMU", 8000, 1 },
2250 { "reserved", 0, 0 },
2251 { "reserved", 0, 0 },
2252 { "GSM", 8000, 1 },
2253 { "G723", 8000, 1 },
2254 { "DVI4", 8000, 1 },
2255 { "DVI4", 16000, 1 },
2256 { "LPC", 8000, 1 },
2257 { "PCMA", 8000, 1 },
2258 { "G722", 8000, 1 },
2259 { "L16", 44100, 2 },
2260 { "L16", 44100, 1 },
2261 { "QCELP", 8000, 1 },
2262 { "CN", 8000, 1 },
2263 { "MPA", 90000, 1 },
2264 { "G728", 8000, 1 },
2265 { "DVI4", 11025, 1 },
2266 { "DVI4", 22050, 1 },
2267 { "G729", 8000, 1 },
2268};
2269
2270void MaybeCreateStaticPayloadAudioCodecs(
2271 const std::vector<int>& fmts, AudioContentDescription* media_desc) {
2272 if (!media_desc) {
2273 return;
2274 }
deadbeef67cf2c12016-04-13 10:07:16 -07002275 RTC_DCHECK(media_desc->codecs().empty());
solenberg9fa49752016-10-08 13:02:44 -07002276 for (int payload_type : fmts) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002277 if (!media_desc->HasCodec(payload_type) &&
2278 payload_type >= 0 &&
kjellander3e33bfe2016-06-20 07:04:09 -07002279 static_cast<uint32_t>(payload_type) <
2280 arraysize(kStaticPayloadAudioCodecs)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002281 std::string encoding_name = kStaticPayloadAudioCodecs[payload_type].name;
2282 int clock_rate = kStaticPayloadAudioCodecs[payload_type].clockrate;
Peter Kasting69558702016-01-12 16:26:35 -08002283 size_t channels = kStaticPayloadAudioCodecs[payload_type].channels;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002284 media_desc->AddCodec(cricket::AudioCodec(payload_type, encoding_name,
deadbeef67cf2c12016-04-13 10:07:16 -07002285 clock_rate, 0, channels));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002286 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002287 }
2288}
2289
2290template <class C>
2291static C* ParseContentDescription(const std::string& message,
2292 const MediaType media_type,
2293 int mline_index,
2294 const std::string& protocol,
deadbeef67cf2c12016-04-13 10:07:16 -07002295 const std::vector<int>& payload_types,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002296 size_t* pos,
2297 std::string* content_name,
deadbeef25ed4352016-12-12 18:37:36 -08002298 bool* bundle_only,
Steve Antone831b8c2018-02-01 12:22:16 -08002299 int* msid_signaling,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002300 TransportDescription* transport,
2301 std::vector<JsepIceCandidate*>* candidates,
2302 webrtc::SdpParseError* error) {
2303 C* media_desc = new C();
2304 switch (media_type) {
2305 case cricket::MEDIA_TYPE_AUDIO:
2306 *content_name = cricket::CN_AUDIO;
2307 break;
2308 case cricket::MEDIA_TYPE_VIDEO:
2309 *content_name = cricket::CN_VIDEO;
2310 break;
2311 case cricket::MEDIA_TYPE_DATA:
2312 *content_name = cricket::CN_DATA;
2313 break;
2314 default:
nissec80e7412017-01-11 05:56:46 -08002315 RTC_NOTREACHED();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002316 break;
2317 }
deadbeef67cf2c12016-04-13 10:07:16 -07002318 if (!ParseContent(message, media_type, mline_index, protocol, payload_types,
Steve Antone831b8c2018-02-01 12:22:16 -08002319 pos, content_name, bundle_only, msid_signaling, media_desc,
2320 transport, candidates, error)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002321 delete media_desc;
zhihuang38989e52017-03-21 11:04:53 -07002322 return nullptr;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002323 }
2324 // Sort the codecs according to the m-line fmt list.
deadbeef67cf2c12016-04-13 10:07:16 -07002325 std::unordered_map<int, int> payload_type_preferences;
2326 // "size + 1" so that the lowest preference payload type has a preference of
2327 // 1, which is greater than the default (0) for payload types not in the fmt
2328 // list.
2329 int preference = static_cast<int>(payload_types.size() + 1);
2330 for (int pt : payload_types) {
2331 payload_type_preferences[pt] = preference--;
2332 }
2333 std::vector<typename C::CodecType> codecs = media_desc->codecs();
2334 std::sort(codecs.begin(), codecs.end(), [&payload_type_preferences](
2335 const typename C::CodecType& a,
2336 const typename C::CodecType& b) {
2337 return payload_type_preferences[a.id] > payload_type_preferences[b.id];
2338 });
2339 media_desc->set_codecs(codecs);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002340 return media_desc;
2341}
2342
2343bool ParseMediaDescription(const std::string& message,
2344 const TransportDescription& session_td,
2345 const RtpHeaderExtensions& session_extmaps,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002346 size_t* pos,
zhihuang38989e52017-03-21 11:04:53 -07002347 const rtc::SocketAddress& session_connection_addr,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002348 cricket::SessionDescription* desc,
2349 std::vector<JsepIceCandidate*>* candidates,
2350 SdpParseError* error) {
nisseede5da42017-01-12 05:15:36 -08002351 RTC_DCHECK(desc != NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002352 std::string line;
2353 int mline_index = -1;
Steve Antone831b8c2018-02-01 12:22:16 -08002354 int msid_signaling = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002355
2356 // Zero or more media descriptions
2357 // RFC 4566
2358 // m=<media> <port> <proto> <fmt>
2359 while (GetLineWithType(message, pos, &line, kLineTypeMedia)) {
2360 ++mline_index;
2361
2362 std::vector<std::string> fields;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002363 rtc::split(line.substr(kLinePrefixLength),
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002364 kSdpDelimiterSpace, &fields);
zstein4b2e0822017-02-17 19:48:38 -08002365
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002366 const size_t expected_min_fields = 4;
2367 if (fields.size() < expected_min_fields) {
2368 return ParseFailedExpectMinFieldNum(line, expected_min_fields, error);
2369 }
deadbeef25ed4352016-12-12 18:37:36 -08002370 bool port_rejected = false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002371 // RFC 3264
2372 // To reject an offered stream, the port number in the corresponding stream
2373 // in the answer MUST be set to zero.
2374 if (fields[1] == kMediaPortRejected) {
deadbeef25ed4352016-12-12 18:37:36 -08002375 port_rejected = true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002376 }
2377
zhihuang38989e52017-03-21 11:04:53 -07002378 int port = 0;
2379 if (!rtc::FromString<int>(fields[1], &port) || !IsValidPort(port)) {
2380 return ParseFailed(line, "The port number is invalid", error);
2381 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002382 std::string protocol = fields[2];
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002383
2384 // <fmt>
deadbeef67cf2c12016-04-13 10:07:16 -07002385 std::vector<int> payload_types;
pthatcher@webrtc.org3341b402015-02-13 21:14:22 +00002386 if (IsRtp(protocol)) {
jiayl@webrtc.orgddb85ab2014-09-05 16:31:56 +00002387 for (size_t j = 3 ; j < fields.size(); ++j) {
2388 // TODO(wu): Remove when below bug is fixed.
2389 // https://bugzilla.mozilla.org/show_bug.cgi?id=996329
pbosbb36fdf2015-07-09 07:48:14 -07002390 if (fields[j].empty() && j == fields.size() - 1) {
jiayl@webrtc.orgddb85ab2014-09-05 16:31:56 +00002391 continue;
2392 }
wu@webrtc.org36eda7c2014-04-15 20:37:30 +00002393
jiayl@webrtc.orgddb85ab2014-09-05 16:31:56 +00002394 int pl = 0;
pkasting@chromium.orge9facf82015-02-17 20:36:28 +00002395 if (!GetPayloadTypeFromString(line, fields[j], &pl, error)) {
jiayl@webrtc.orgddb85ab2014-09-05 16:31:56 +00002396 return false;
2397 }
deadbeef67cf2c12016-04-13 10:07:16 -07002398 payload_types.push_back(pl);
wu@webrtc.org5e760e72014-04-02 23:19:09 +00002399 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002400 }
2401
2402 // Make a temporary TransportDescription based on |session_td|.
2403 // Some of this gets overwritten by ParseContent.
deadbeef46eed762016-01-28 13:24:37 -08002404 TransportDescription transport(
2405 session_td.transport_options, session_td.ice_ufrag, session_td.ice_pwd,
2406 session_td.ice_mode, session_td.connection_role,
2407 session_td.identity_fingerprint.get());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002408
kwibergd1fe2812016-04-27 06:47:29 -07002409 std::unique_ptr<MediaContentDescription> content;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002410 std::string content_name;
deadbeef25ed4352016-12-12 18:37:36 -08002411 bool bundle_only = false;
Steve Antone831b8c2018-02-01 12:22:16 -08002412 int section_msid_signaling = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002413 if (HasAttribute(line, kMediaTypeVideo)) {
2414 content.reset(ParseContentDescription<VideoContentDescription>(
deadbeef67cf2c12016-04-13 10:07:16 -07002415 message, cricket::MEDIA_TYPE_VIDEO, mline_index, protocol,
Steve Antone831b8c2018-02-01 12:22:16 -08002416 payload_types, pos, &content_name, &bundle_only,
2417 &section_msid_signaling, &transport, candidates, error));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002418 } else if (HasAttribute(line, kMediaTypeAudio)) {
2419 content.reset(ParseContentDescription<AudioContentDescription>(
deadbeef67cf2c12016-04-13 10:07:16 -07002420 message, cricket::MEDIA_TYPE_AUDIO, mline_index, protocol,
Steve Antone831b8c2018-02-01 12:22:16 -08002421 payload_types, pos, &content_name, &bundle_only,
2422 &section_msid_signaling, &transport, candidates, error));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002423 } else if (HasAttribute(line, kMediaTypeData)) {
jiayl@webrtc.org0e527722014-09-08 21:43:43 +00002424 DataContentDescription* data_desc =
wu@webrtc.org78187522013-10-07 23:32:02 +00002425 ParseContentDescription<DataContentDescription>(
deadbeef67cf2c12016-04-13 10:07:16 -07002426 message, cricket::MEDIA_TYPE_DATA, mline_index, protocol,
Steve Antone831b8c2018-02-01 12:22:16 -08002427 payload_types, pos, &content_name, &bundle_only,
2428 &section_msid_signaling, &transport, candidates, error);
jiayl@webrtc.org0e527722014-09-08 21:43:43 +00002429 content.reset(data_desc);
wu@webrtc.org78187522013-10-07 23:32:02 +00002430
zstein4b2e0822017-02-17 19:48:38 -08002431 if (data_desc && IsDtlsSctp(protocol)) {
2432 int p;
2433 if (rtc::FromString(fields[3], &p)) {
2434 if (!AddSctpDataCodec(data_desc, p)) {
2435 return false;
2436 }
2437 } else if (fields[3] == kDefaultSctpmapProtocol) {
2438 data_desc->set_use_sctpmap(false);
2439 }
wu@webrtc.org78187522013-10-07 23:32:02 +00002440 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002441 } else {
Mirko Bonadei675513b2017-11-09 11:09:25 +01002442 RTC_LOG(LS_WARNING) << "Unsupported media type: " << line;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002443 continue;
2444 }
2445 if (!content.get()) {
2446 // ParseContentDescription returns NULL if failed.
2447 return false;
2448 }
2449
Steve Antone831b8c2018-02-01 12:22:16 -08002450 msid_signaling |= section_msid_signaling;
2451
deadbeef25ed4352016-12-12 18:37:36 -08002452 bool content_rejected = false;
deadbeef12771a12017-01-03 13:53:47 -08002453 // A port of 0 is not interpreted as a rejected m= section when it's
2454 // used along with a=bundle-only.
deadbeef25ed4352016-12-12 18:37:36 -08002455 if (bundle_only) {
deadbeef25ed4352016-12-12 18:37:36 -08002456 if (!port_rejected) {
deadbeef12771a12017-01-03 13:53:47 -08002457 // Usage of bundle-only with a nonzero port is unspecified. So just
2458 // ignore bundle-only if we see this.
2459 bundle_only = false;
Mirko Bonadei675513b2017-11-09 11:09:25 +01002460 RTC_LOG(LS_WARNING)
deadbeef12771a12017-01-03 13:53:47 -08002461 << "a=bundle-only attribute observed with a nonzero "
Jonas Olsson45cc8902018-02-13 10:37:07 +01002462 "port; this usage is unspecified so the attribute is being "
2463 "ignored.";
deadbeef25ed4352016-12-12 18:37:36 -08002464 }
2465 } else {
2466 // If not using bundle-only, interpret port 0 in the normal way; the m=
2467 // section is being rejected.
2468 content_rejected = port_rejected;
2469 }
2470
pthatcher@webrtc.org3341b402015-02-13 21:14:22 +00002471 if (IsRtp(protocol)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002472 // Set the extmap.
2473 if (!session_extmaps.empty() &&
2474 !content->rtp_header_extensions().empty()) {
2475 return ParseFailed("",
2476 "The a=extmap MUST be either all session level or "
2477 "all media level.",
2478 error);
2479 }
2480 for (size_t i = 0; i < session_extmaps.size(); ++i) {
2481 content->AddRtpHeaderExtension(session_extmaps[i]);
2482 }
2483 }
2484 content->set_protocol(protocol);
zhihuang38989e52017-03-21 11:04:53 -07002485
2486 // Use the session level connection address if the media level addresses are
2487 // not specified.
2488 rtc::SocketAddress address;
2489 address = content->connection_address().IsNil()
2490 ? session_connection_addr
2491 : content->connection_address();
2492 address.SetPort(port);
2493 content->set_connection_address(address);
2494
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002495 desc->AddContent(content_name,
Steve Anton5adfafd2017-12-20 16:34:00 -08002496 IsDtlsSctp(protocol) ? MediaProtocolType::kSctp
2497 : MediaProtocolType::kRtp,
deadbeef25ed4352016-12-12 18:37:36 -08002498 content_rejected, bundle_only, content.release());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002499 // Create TransportInfo with the media level "ice-pwd" and "ice-ufrag".
2500 TransportInfo transport_info(content_name, transport);
2501
2502 if (!desc->AddTransportInfo(transport_info)) {
2503 std::ostringstream description;
2504 description << "Failed to AddTransportInfo with content name: "
2505 << content_name;
2506 return ParseFailed("", description.str(), error);
2507 }
2508 }
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00002509
Steve Antone831b8c2018-02-01 12:22:16 -08002510 desc->set_msid_signaling(msid_signaling);
2511
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00002512 size_t end_of_message = message.size();
2513 if (mline_index == -1 && *pos != end_of_message) {
2514 ParseFailed(message, *pos, "Expects m line.", error);
2515 return false;
2516 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002517 return true;
2518}
2519
2520bool VerifyCodec(const cricket::Codec& codec) {
2521 // Codec has not been populated correctly unless the name has been set. This
2522 // can happen if an SDP has an fmtp or rtcp-fb with a payload type but doesn't
2523 // have a corresponding "rtpmap" line.
htab39db842016-12-08 01:50:48 -08002524 return !codec.name.empty();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002525}
2526
2527bool VerifyAudioCodecs(const AudioContentDescription* audio_desc) {
2528 const std::vector<cricket::AudioCodec>& codecs = audio_desc->codecs();
2529 for (std::vector<cricket::AudioCodec>::const_iterator iter = codecs.begin();
2530 iter != codecs.end(); ++iter) {
2531 if (!VerifyCodec(*iter)) {
2532 return false;
2533 }
2534 }
2535 return true;
2536}
2537
2538bool VerifyVideoCodecs(const VideoContentDescription* video_desc) {
2539 const std::vector<cricket::VideoCodec>& codecs = video_desc->codecs();
2540 for (std::vector<cricket::VideoCodec>::const_iterator iter = codecs.begin();
2541 iter != codecs.end(); ++iter) {
2542 if (!VerifyCodec(*iter)) {
2543 return false;
2544 }
2545 }
2546 return true;
2547}
2548
2549void AddParameters(const cricket::CodecParameterMap& parameters,
2550 cricket::Codec* codec) {
2551 for (cricket::CodecParameterMap::const_iterator iter =
2552 parameters.begin(); iter != parameters.end(); ++iter) {
2553 codec->SetParam(iter->first, iter->second);
2554 }
2555}
2556
2557void AddFeedbackParameter(const cricket::FeedbackParam& feedback_param,
2558 cricket::Codec* codec) {
2559 codec->AddFeedbackParam(feedback_param);
2560}
2561
2562void AddFeedbackParameters(const cricket::FeedbackParams& feedback_params,
2563 cricket::Codec* codec) {
2564 for (std::vector<cricket::FeedbackParam>::const_iterator iter =
2565 feedback_params.params().begin();
2566 iter != feedback_params.params().end(); ++iter) {
2567 codec->AddFeedbackParam(*iter);
2568 }
2569}
2570
2571// Gets the current codec setting associated with |payload_type|. If there
changbin.shao@webrtc.org2d25b442015-03-16 04:14:34 +00002572// is no Codec associated with that payload type it returns an empty codec
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002573// with that payload type.
2574template <class T>
changbin.shao@webrtc.org2d25b442015-03-16 04:14:34 +00002575T GetCodecWithPayloadType(const std::vector<T>& codecs, int payload_type) {
magjedb05fa242016-11-11 04:00:16 -08002576 const T* codec = FindCodecById(codecs, payload_type);
2577 if (codec)
2578 return *codec;
2579 // Return empty codec with |payload_type|.
changbin.shao@webrtc.org2d25b442015-03-16 04:14:34 +00002580 T ret_val;
magjedb05fa242016-11-11 04:00:16 -08002581 ret_val.id = payload_type;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002582 return ret_val;
2583}
2584
2585// Updates or creates a new codec entry in the audio description.
2586template <class T, class U>
2587void AddOrReplaceCodec(MediaContentDescription* content_desc, const U& codec) {
2588 T* desc = static_cast<T*>(content_desc);
2589 std::vector<U> codecs = desc->codecs();
2590 bool found = false;
2591
2592 typename std::vector<U>::iterator iter;
2593 for (iter = codecs.begin(); iter != codecs.end(); ++iter) {
2594 if (iter->id == codec.id) {
2595 *iter = codec;
2596 found = true;
2597 break;
2598 }
2599 }
2600 if (!found) {
2601 desc->AddCodec(codec);
2602 return;
2603 }
2604 desc->set_codecs(codecs);
2605}
2606
2607// Adds or updates existing codec corresponding to |payload_type| according
2608// to |parameters|.
2609template <class T, class U>
2610void UpdateCodec(MediaContentDescription* content_desc, int payload_type,
2611 const cricket::CodecParameterMap& parameters) {
2612 // Codec might already have been populated (from rtpmap).
changbin.shao@webrtc.org2d25b442015-03-16 04:14:34 +00002613 U new_codec = GetCodecWithPayloadType(static_cast<T*>(content_desc)->codecs(),
2614 payload_type);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002615 AddParameters(parameters, &new_codec);
2616 AddOrReplaceCodec<T, U>(content_desc, new_codec);
2617}
2618
2619// Adds or updates existing codec corresponding to |payload_type| according
2620// to |feedback_param|.
2621template <class T, class U>
2622void UpdateCodec(MediaContentDescription* content_desc, int payload_type,
2623 const cricket::FeedbackParam& feedback_param) {
2624 // Codec might already have been populated (from rtpmap).
changbin.shao@webrtc.org2d25b442015-03-16 04:14:34 +00002625 U new_codec = GetCodecWithPayloadType(static_cast<T*>(content_desc)->codecs(),
2626 payload_type);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002627 AddFeedbackParameter(feedback_param, &new_codec);
2628 AddOrReplaceCodec<T, U>(content_desc, new_codec);
2629}
2630
jlmiller@webrtc.orga744a282015-02-18 21:37:46 +00002631template <class T>
2632bool PopWildcardCodec(std::vector<T>* codecs, T* wildcard_codec) {
2633 for (auto iter = codecs->begin(); iter != codecs->end(); ++iter) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002634 if (iter->id == kWildcardPayloadType) {
2635 *wildcard_codec = *iter;
2636 codecs->erase(iter);
2637 return true;
2638 }
2639 }
2640 return false;
2641}
2642
jlmiller@webrtc.orga744a282015-02-18 21:37:46 +00002643template<class T>
2644void UpdateFromWildcardCodecs(cricket::MediaContentDescriptionImpl<T>* desc) {
2645 auto codecs = desc->codecs();
2646 T wildcard_codec;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002647 if (!PopWildcardCodec(&codecs, &wildcard_codec)) {
2648 return;
2649 }
jlmiller@webrtc.orga744a282015-02-18 21:37:46 +00002650 for (auto& codec : codecs) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002651 AddFeedbackParameters(wildcard_codec.feedback_params, &codec);
2652 }
jlmiller@webrtc.orga744a282015-02-18 21:37:46 +00002653 desc->set_codecs(codecs);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002654}
2655
2656void AddAudioAttribute(const std::string& name, const std::string& value,
2657 AudioContentDescription* audio_desc) {
2658 if (value.empty()) {
2659 return;
2660 }
2661 std::vector<cricket::AudioCodec> codecs = audio_desc->codecs();
2662 for (std::vector<cricket::AudioCodec>::iterator iter = codecs.begin();
2663 iter != codecs.end(); ++iter) {
2664 iter->params[name] = value;
2665 }
2666 audio_desc->set_codecs(codecs);
2667}
2668
2669bool ParseContent(const std::string& message,
2670 const MediaType media_type,
2671 int mline_index,
2672 const std::string& protocol,
deadbeef67cf2c12016-04-13 10:07:16 -07002673 const std::vector<int>& payload_types,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002674 size_t* pos,
2675 std::string* content_name,
deadbeef25ed4352016-12-12 18:37:36 -08002676 bool* bundle_only,
Steve Antone831b8c2018-02-01 12:22:16 -08002677 int* msid_signaling,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002678 MediaContentDescription* media_desc,
2679 TransportDescription* transport,
2680 std::vector<JsepIceCandidate*>* candidates,
2681 SdpParseError* error) {
nisseede5da42017-01-12 05:15:36 -08002682 RTC_DCHECK(media_desc != NULL);
2683 RTC_DCHECK(content_name != NULL);
2684 RTC_DCHECK(transport != NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002685
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +00002686 if (media_type == cricket::MEDIA_TYPE_AUDIO) {
Steve Antonb1c1de12017-12-21 15:14:30 -08002687 MaybeCreateStaticPayloadAudioCodecs(payload_types, media_desc->as_audio());
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +00002688 }
2689
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002690 // The media level "ice-ufrag" and "ice-pwd".
2691 // The candidates before update the media level "ice-pwd" and "ice-ufrag".
2692 Candidates candidates_orig;
2693 std::string line;
2694 std::string mline_id;
2695 // Tracks created out of the ssrc attributes.
2696 StreamParamsVec tracks;
2697 SsrcInfoVec ssrc_infos;
2698 SsrcGroupVec ssrc_groups;
2699 std::string maxptime_as_string;
2700 std::string ptime_as_string;
deadbeef9d3584c2016-02-16 17:54:10 -08002701 std::string stream_id;
2702 std::string track_id;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002703
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002704 // Loop until the next m line
2705 while (!IsLineType(message, kLineTypeMedia, *pos)) {
2706 if (!GetLine(message, pos, &line)) {
2707 if (*pos >= message.size()) {
2708 break; // Done parsing
2709 } else {
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +00002710 return ParseFailed(message, *pos, "Invalid SDP line.", error);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002711 }
2712 }
2713
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002714 // RFC 4566
2715 // b=* (zero or more bandwidth information lines)
2716 if (IsLineType(line, kLineTypeSessionBandwidth)) {
2717 std::string bandwidth;
2718 if (HasAttribute(line, kApplicationSpecificMaximum)) {
2719 if (!GetValue(line, kApplicationSpecificMaximum, &bandwidth, error)) {
2720 return false;
2721 } else {
wu@webrtc.org5e760e72014-04-02 23:19:09 +00002722 int b = 0;
2723 if (!GetValueFromString(line, bandwidth, &b, error)) {
2724 return false;
2725 }
deadbeef3e8016e2017-08-03 17:49:30 -07002726 // TODO(deadbeef): Historically, applications may be setting a value
2727 // of -1 to mean "unset any previously set bandwidth limit", even
2728 // though ommitting the "b=AS" entirely will do just that. Once we've
2729 // transitioned applications to doing the right thing, it would be
2730 // better to treat this as a hard error instead of just ignoring it.
2731 if (b == -1) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01002732 RTC_LOG(LS_WARNING)
2733 << "Ignoring \"b=AS:-1\"; will be treated as \"no "
2734 "bandwidth limit\".";
deadbeef3e8016e2017-08-03 17:49:30 -07002735 continue;
2736 }
deadbeefbc88c6b2017-08-02 11:26:34 -07002737 if (b < 0) {
2738 return ParseFailed(line, "b=AS value can't be negative.", error);
2739 }
Peter Thatcherc0c3a862015-06-24 15:31:25 -07002740 // We should never use more than the default bandwidth for RTP-based
2741 // data channels. Don't allow SDP to set the bandwidth, because
2742 // that would give JS the opportunity to "break the Internet".
2743 // See: https://code.google.com/p/chromium/issues/detail?id=280726
2744 if (media_type == cricket::MEDIA_TYPE_DATA && IsRtp(protocol) &&
2745 b > cricket::kDataMaxBandwidth / 1000) {
2746 std::ostringstream description;
2747 description << "RTP-based data channels may not send more than "
2748 << cricket::kDataMaxBandwidth / 1000 << "kbps.";
2749 return ParseFailed(line, description.str(), error);
2750 }
deadbeefb2362572016-12-13 16:37:06 -08002751 // Prevent integer overflow.
2752 b = std::min(b, INT_MAX / 1000);
wu@webrtc.org5e760e72014-04-02 23:19:09 +00002753 media_desc->set_bandwidth(b * 1000);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002754 }
2755 }
2756 continue;
2757 }
2758
zhihuang38989e52017-03-21 11:04:53 -07002759 // Parse the media level connection data.
2760 if (IsLineType(line, kLineTypeConnection)) {
2761 rtc::SocketAddress addr;
2762 if (!ParseConnectionData(line, &addr, error)) {
2763 return false;
2764 }
2765 media_desc->set_connection_address(addr);
2766 continue;
2767 }
2768
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002769 if (!IsLineType(line, kLineTypeAttributes)) {
Steve Anton36b29d12017-10-30 09:57:42 -07002770 // TODO(deadbeef): Handle other lines if needed.
Mirko Bonadei675513b2017-11-09 11:09:25 +01002771 RTC_LOG(LS_INFO) << "Ignored line: " << line;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002772 continue;
2773 }
2774
2775 // Handle attributes common to SCTP and RTP.
2776 if (HasAttribute(line, kAttributeMid)) {
2777 // RFC 3388
2778 // mid-attribute = "a=mid:" identification-tag
2779 // identification-tag = token
2780 // Use the mid identification-tag as the content name.
2781 if (!GetValue(line, kAttributeMid, &mline_id, error)) {
2782 return false;
2783 }
2784 *content_name = mline_id;
deadbeef25ed4352016-12-12 18:37:36 -08002785 } else if (HasAttribute(line, kAttributeBundleOnly)) {
2786 *bundle_only = true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002787 } else if (HasAttribute(line, kAttributeCandidate)) {
2788 Candidate candidate;
2789 if (!ParseCandidate(line, &candidate, error, false)) {
2790 return false;
2791 }
deadbeef7bcdb692017-01-20 12:43:58 -08002792 // ParseCandidate will parse non-standard ufrag and password attributes,
2793 // since it's used for candidate trickling, but we only want to process
2794 // the "a=ice-ufrag"/"a=ice-pwd" values in a session description, so
2795 // strip them off at this point.
2796 candidate.set_username(std::string());
2797 candidate.set_password(std::string());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002798 candidates_orig.push_back(candidate);
2799 } else if (HasAttribute(line, kAttributeIceUfrag)) {
2800 if (!GetValue(line, kAttributeIceUfrag, &transport->ice_ufrag, error)) {
2801 return false;
2802 }
2803 } else if (HasAttribute(line, kAttributeIcePwd)) {
2804 if (!GetValue(line, kAttributeIcePwd, &transport->ice_pwd, error)) {
2805 return false;
2806 }
2807 } else if (HasAttribute(line, kAttributeIceOption)) {
2808 if (!ParseIceOptions(line, &transport->transport_options, error)) {
2809 return false;
2810 }
2811 } else if (HasAttribute(line, kAttributeFmtp)) {
2812 if (!ParseFmtpAttributes(line, media_type, media_desc, error)) {
2813 return false;
2814 }
2815 } else if (HasAttribute(line, kAttributeFingerprint)) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002816 rtc::SSLFingerprint* fingerprint = NULL;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002817
2818 if (!ParseFingerprintAttribute(line, &fingerprint, error)) {
2819 return false;
2820 }
2821 transport->identity_fingerprint.reset(fingerprint);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00002822 } else if (HasAttribute(line, kAttributeSetup)) {
2823 if (!ParseDtlsSetup(line, &(transport->connection_role), error)) {
2824 return false;
2825 }
pthatcher@webrtc.org3341b402015-02-13 21:14:22 +00002826 } else if (IsDtlsSctp(protocol) && HasAttribute(line, kAttributeSctpPort)) {
deadbeef7e146cb2016-09-28 10:04:34 -07002827 if (media_type != cricket::MEDIA_TYPE_DATA) {
2828 return ParseFailed(
2829 line, "sctp-port attribute found in non-data media description.",
2830 error);
2831 }
jiayl@webrtc.orgddb85ab2014-09-05 16:31:56 +00002832 int sctp_port;
2833 if (!ParseSctpPort(line, &sctp_port, error)) {
2834 return false;
2835 }
Steve Antonb1c1de12017-12-21 15:14:30 -08002836 if (!AddSctpDataCodec(media_desc->as_data(), sctp_port)) {
jiayl@webrtc.orgddb85ab2014-09-05 16:31:56 +00002837 return false;
2838 }
pthatcher@webrtc.org3341b402015-02-13 21:14:22 +00002839 } else if (IsRtp(protocol)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002840 //
2841 // RTP specific attrubtes
2842 //
2843 if (HasAttribute(line, kAttributeRtcpMux)) {
2844 media_desc->set_rtcp_mux(true);
deadbeef13871492015-12-09 12:37:51 -08002845 } else if (HasAttribute(line, kAttributeRtcpReducedSize)) {
2846 media_desc->set_rtcp_reduced_size(true);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002847 } else if (HasAttribute(line, kAttributeSsrcGroup)) {
2848 if (!ParseSsrcGroupAttribute(line, &ssrc_groups, error)) {
2849 return false;
2850 }
2851 } else if (HasAttribute(line, kAttributeSsrc)) {
Steve Antone831b8c2018-02-01 12:22:16 -08002852 if (!ParseSsrcAttribute(line, &ssrc_infos, msid_signaling, error)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002853 return false;
2854 }
2855 } else if (HasAttribute(line, kAttributeCrypto)) {
2856 if (!ParseCryptoAttribute(line, media_desc, error)) {
2857 return false;
2858 }
2859 } else if (HasAttribute(line, kAttributeRtpmap)) {
deadbeef67cf2c12016-04-13 10:07:16 -07002860 if (!ParseRtpmapAttribute(line, media_type, payload_types, media_desc,
2861 error)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002862 return false;
2863 }
2864 } else if (HasAttribute(line, kCodecParamMaxPTime)) {
2865 if (!GetValue(line, kCodecParamMaxPTime, &maxptime_as_string, error)) {
2866 return false;
2867 }
2868 } else if (HasAttribute(line, kAttributeRtcpFb)) {
2869 if (!ParseRtcpFbAttribute(line, media_type, media_desc, error)) {
2870 return false;
2871 }
2872 } else if (HasAttribute(line, kCodecParamPTime)) {
2873 if (!GetValue(line, kCodecParamPTime, &ptime_as_string, error)) {
2874 return false;
2875 }
2876 } else if (HasAttribute(line, kAttributeSendOnly)) {
Steve Anton4e70a722017-11-28 14:57:10 -08002877 media_desc->set_direction(RtpTransceiverDirection::kSendOnly);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002878 } else if (HasAttribute(line, kAttributeRecvOnly)) {
Steve Anton4e70a722017-11-28 14:57:10 -08002879 media_desc->set_direction(RtpTransceiverDirection::kRecvOnly);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002880 } else if (HasAttribute(line, kAttributeInactive)) {
Steve Anton4e70a722017-11-28 14:57:10 -08002881 media_desc->set_direction(RtpTransceiverDirection::kInactive);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002882 } else if (HasAttribute(line, kAttributeSendRecv)) {
Steve Anton4e70a722017-11-28 14:57:10 -08002883 media_desc->set_direction(RtpTransceiverDirection::kSendRecv);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002884 } else if (HasAttribute(line, kAttributeExtmap)) {
isheriff6f8d6862016-05-26 11:24:55 -07002885 RtpExtension extmap;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002886 if (!ParseExtmap(line, &extmap, error)) {
2887 return false;
2888 }
2889 media_desc->AddRtpHeaderExtension(extmap);
2890 } else if (HasAttribute(line, kAttributeXGoogleFlag)) {
2891 // Experimental attribute. Conference mode activates more aggressive
2892 // AEC and NS settings.
Steve Anton36b29d12017-10-30 09:57:42 -07002893 // TODO(deadbeef): expose API to set these directly.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002894 std::string flag_value;
2895 if (!GetValue(line, kAttributeXGoogleFlag, &flag_value, error)) {
2896 return false;
2897 }
2898 if (flag_value.compare(kValueConference) == 0)
2899 media_desc->set_conference_mode(true);
deadbeef9d3584c2016-02-16 17:54:10 -08002900 } else if (HasAttribute(line, kAttributeMsid)) {
2901 if (!ParseMsidAttribute(line, &stream_id, &track_id, error)) {
2902 return false;
2903 }
Steve Antone831b8c2018-02-01 12:22:16 -08002904 *msid_signaling |= cricket::kMsidSignalingMediaSection;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002905 }
2906 } else {
2907 // Only parse lines that we are interested of.
Mirko Bonadei675513b2017-11-09 11:09:25 +01002908 RTC_LOG(LS_INFO) << "Ignored line: " << line;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002909 continue;
2910 }
2911 }
2912
2913 // Create tracks from the |ssrc_infos|.
Taylor Brandstetter5de6b752016-03-09 17:02:30 -08002914 // If the stream_id/track_id for all SSRCS are identical, one StreamParams
2915 // will be created in CreateTracksFromSsrcInfos, containing all the SSRCs from
2916 // the m= section.
2917 CreateTracksFromSsrcInfos(ssrc_infos, stream_id, track_id, &tracks);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002918
2919 // Add the ssrc group to the track.
2920 for (SsrcGroupVec::iterator ssrc_group = ssrc_groups.begin();
2921 ssrc_group != ssrc_groups.end(); ++ssrc_group) {
2922 if (ssrc_group->ssrcs.empty()) {
2923 continue;
2924 }
Peter Boström0c4e06b2015-10-07 12:23:21 +02002925 uint32_t ssrc = ssrc_group->ssrcs.front();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002926 for (StreamParamsVec::iterator track = tracks.begin();
2927 track != tracks.end(); ++track) {
2928 if (track->has_ssrc(ssrc)) {
2929 track->ssrc_groups.push_back(*ssrc_group);
2930 }
2931 }
2932 }
2933
2934 // Add the new tracks to the |media_desc|.
deadbeef9d3584c2016-02-16 17:54:10 -08002935 for (StreamParams& track : tracks) {
2936 media_desc->AddStream(track);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002937 }
2938
2939 if (media_type == cricket::MEDIA_TYPE_AUDIO) {
Steve Antonb1c1de12017-12-21 15:14:30 -08002940 AudioContentDescription* audio_desc = media_desc->as_audio();
jlmiller@webrtc.orga744a282015-02-18 21:37:46 +00002941 UpdateFromWildcardCodecs(audio_desc);
2942
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002943 // Verify audio codec ensures that no audio codec has been populated with
2944 // only fmtp.
2945 if (!VerifyAudioCodecs(audio_desc)) {
2946 return ParseFailed("Failed to parse audio codecs correctly.", error);
2947 }
2948 AddAudioAttribute(kCodecParamMaxPTime, maxptime_as_string, audio_desc);
2949 AddAudioAttribute(kCodecParamPTime, ptime_as_string, audio_desc);
2950 }
2951
2952 if (media_type == cricket::MEDIA_TYPE_VIDEO) {
Steve Antonb1c1de12017-12-21 15:14:30 -08002953 VideoContentDescription* video_desc = media_desc->as_video();
jlmiller@webrtc.orga744a282015-02-18 21:37:46 +00002954 UpdateFromWildcardCodecs(video_desc);
2955 // Verify video codec ensures that no video codec has been populated with
2956 // only rtcp-fb.
2957 if (!VerifyVideoCodecs(video_desc)) {
2958 return ParseFailed("Failed to parse video codecs correctly.", error);
2959 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002960 }
2961
2962 // RFC 5245
2963 // Update the candidates with the media level "ice-pwd" and "ice-ufrag".
2964 for (Candidates::iterator it = candidates_orig.begin();
2965 it != candidates_orig.end(); ++it) {
nisseede5da42017-01-12 05:15:36 -08002966 RTC_DCHECK((*it).username().empty() ||
2967 (*it).username() == transport->ice_ufrag);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002968 (*it).set_username(transport->ice_ufrag);
nisseede5da42017-01-12 05:15:36 -08002969 RTC_DCHECK((*it).password().empty());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002970 (*it).set_password(transport->ice_pwd);
2971 candidates->push_back(
2972 new JsepIceCandidate(mline_id, mline_index, *it));
2973 }
2974 return true;
2975}
2976
Steve Antone831b8c2018-02-01 12:22:16 -08002977bool ParseSsrcAttribute(const std::string& line,
2978 SsrcInfoVec* ssrc_infos,
2979 int* msid_signaling,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002980 SdpParseError* error) {
nisseede5da42017-01-12 05:15:36 -08002981 RTC_DCHECK(ssrc_infos != NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002982 // RFC 5576
2983 // a=ssrc:<ssrc-id> <attribute>
2984 // a=ssrc:<ssrc-id> <attribute>:<value>
2985 std::string field1, field2;
Donald Curtis144d0182015-05-15 13:14:24 -07002986 if (!rtc::tokenize_first(line.substr(kLinePrefixLength), kSdpDelimiterSpace,
2987 &field1, &field2)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002988 const size_t expected_fields = 2;
2989 return ParseFailedExpectFieldNum(line, expected_fields, error);
2990 }
2991
2992 // ssrc:<ssrc-id>
2993 std::string ssrc_id_s;
2994 if (!GetValue(field1, kAttributeSsrc, &ssrc_id_s, error)) {
2995 return false;
2996 }
Peter Boström0c4e06b2015-10-07 12:23:21 +02002997 uint32_t ssrc_id = 0;
wu@webrtc.org5e760e72014-04-02 23:19:09 +00002998 if (!GetValueFromString(line, ssrc_id_s, &ssrc_id, error)) {
2999 return false;
3000 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003001
3002 std::string attribute;
3003 std::string value;
Donald Curtis144d0182015-05-15 13:14:24 -07003004 if (!rtc::tokenize_first(field2, kSdpDelimiterColon, &attribute, &value)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003005 std::ostringstream description;
3006 description << "Failed to get the ssrc attribute value from " << field2
3007 << ". Expected format <attribute>:<value>.";
3008 return ParseFailed(line, description.str(), error);
3009 }
3010
3011 // Check if there's already an item for this |ssrc_id|. Create a new one if
3012 // there isn't.
3013 SsrcInfoVec::iterator ssrc_info = ssrc_infos->begin();
3014 for (; ssrc_info != ssrc_infos->end(); ++ssrc_info) {
3015 if (ssrc_info->ssrc_id == ssrc_id) {
3016 break;
3017 }
3018 }
3019 if (ssrc_info == ssrc_infos->end()) {
3020 SsrcInfo info;
3021 info.ssrc_id = ssrc_id;
3022 ssrc_infos->push_back(info);
3023 ssrc_info = ssrc_infos->end() - 1;
3024 }
3025
3026 // Store the info to the |ssrc_info|.
3027 if (attribute == kSsrcAttributeCname) {
3028 // RFC 5576
3029 // cname:<value>
3030 ssrc_info->cname = value;
3031 } else if (attribute == kSsrcAttributeMsid) {
3032 // draft-alvestrand-mmusic-msid-00
3033 // "msid:" identifier [ " " appdata ]
3034 std::vector<std::string> fields;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00003035 rtc::split(value, kSdpDelimiterSpace, &fields);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003036 if (fields.size() < 1 || fields.size() > 2) {
3037 return ParseFailed(line,
3038 "Expected format \"msid:<identifier>[ <appdata>]\".",
3039 error);
3040 }
deadbeef9d3584c2016-02-16 17:54:10 -08003041 ssrc_info->stream_id = fields[0];
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003042 if (fields.size() == 2) {
deadbeef9d3584c2016-02-16 17:54:10 -08003043 ssrc_info->track_id = fields[1];
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003044 }
Steve Antone831b8c2018-02-01 12:22:16 -08003045 *msid_signaling |= cricket::kMsidSignalingSsrcAttribute;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003046 } else if (attribute == kSsrcAttributeMslabel) {
3047 // draft-alvestrand-rtcweb-mid-01
3048 // mslabel:<value>
3049 ssrc_info->mslabel = value;
3050 } else if (attribute == kSSrcAttributeLabel) {
3051 // The label isn't defined.
3052 // label:<value>
3053 ssrc_info->label = value;
3054 }
3055 return true;
3056}
3057
3058bool ParseSsrcGroupAttribute(const std::string& line,
3059 SsrcGroupVec* ssrc_groups,
3060 SdpParseError* error) {
nisseede5da42017-01-12 05:15:36 -08003061 RTC_DCHECK(ssrc_groups != NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003062 // RFC 5576
3063 // a=ssrc-group:<semantics> <ssrc-id> ...
3064 std::vector<std::string> fields;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00003065 rtc::split(line.substr(kLinePrefixLength),
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003066 kSdpDelimiterSpace, &fields);
3067 const size_t expected_min_fields = 2;
3068 if (fields.size() < expected_min_fields) {
3069 return ParseFailedExpectMinFieldNum(line, expected_min_fields, error);
3070 }
3071 std::string semantics;
3072 if (!GetValue(fields[0], kAttributeSsrcGroup, &semantics, error)) {
3073 return false;
3074 }
Peter Boström0c4e06b2015-10-07 12:23:21 +02003075 std::vector<uint32_t> ssrcs;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003076 for (size_t i = 1; i < fields.size(); ++i) {
Peter Boström0c4e06b2015-10-07 12:23:21 +02003077 uint32_t ssrc = 0;
wu@webrtc.org5e760e72014-04-02 23:19:09 +00003078 if (!GetValueFromString(line, fields[i], &ssrc, error)) {
3079 return false;
3080 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003081 ssrcs.push_back(ssrc);
3082 }
3083 ssrc_groups->push_back(SsrcGroup(semantics, ssrcs));
3084 return true;
3085}
3086
3087bool ParseCryptoAttribute(const std::string& line,
3088 MediaContentDescription* media_desc,
3089 SdpParseError* error) {
3090 std::vector<std::string> fields;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00003091 rtc::split(line.substr(kLinePrefixLength),
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003092 kSdpDelimiterSpace, &fields);
3093 // RFC 4568
3094 // a=crypto:<tag> <crypto-suite> <key-params> [<session-params>]
3095 const size_t expected_min_fields = 3;
3096 if (fields.size() < expected_min_fields) {
3097 return ParseFailedExpectMinFieldNum(line, expected_min_fields, error);
3098 }
3099 std::string tag_value;
3100 if (!GetValue(fields[0], kAttributeCrypto, &tag_value, error)) {
3101 return false;
3102 }
wu@webrtc.org5e760e72014-04-02 23:19:09 +00003103 int tag = 0;
3104 if (!GetValueFromString(line, tag_value, &tag, error)) {
3105 return false;
3106 }
jbauch083b73f2015-07-16 02:46:32 -07003107 const std::string& crypto_suite = fields[1];
3108 const std::string& key_params = fields[2];
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003109 std::string session_params;
3110 if (fields.size() > 3) {
3111 session_params = fields[3];
3112 }
3113 media_desc->AddCrypto(CryptoParams(tag, crypto_suite, key_params,
3114 session_params));
3115 return true;
3116}
3117
3118// Updates or creates a new codec entry in the audio description with according
deadbeef67cf2c12016-04-13 10:07:16 -07003119// to |name|, |clockrate|, |bitrate|, and |channels|.
3120void UpdateCodec(int payload_type,
3121 const std::string& name,
3122 int clockrate,
3123 int bitrate,
3124 size_t channels,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003125 AudioContentDescription* audio_desc) {
3126 // Codec may already be populated with (only) optional parameters
3127 // (from an fmtp).
changbin.shao@webrtc.org2d25b442015-03-16 04:14:34 +00003128 cricket::AudioCodec codec =
3129 GetCodecWithPayloadType(audio_desc->codecs(), payload_type);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003130 codec.name = name;
3131 codec.clockrate = clockrate;
3132 codec.bitrate = bitrate;
3133 codec.channels = channels;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003134 AddOrReplaceCodec<AudioContentDescription, cricket::AudioCodec>(audio_desc,
3135 codec);
3136}
3137
3138// Updates or creates a new codec entry in the video description according to
deadbeef67cf2c12016-04-13 10:07:16 -07003139// |name|, |width|, |height|, and |framerate|.
3140void UpdateCodec(int payload_type,
3141 const std::string& name,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003142 VideoContentDescription* video_desc) {
3143 // Codec may already be populated with (only) optional parameters
3144 // (from an fmtp).
changbin.shao@webrtc.org2d25b442015-03-16 04:14:34 +00003145 cricket::VideoCodec codec =
3146 GetCodecWithPayloadType(video_desc->codecs(), payload_type);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003147 codec.name = name;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003148 AddOrReplaceCodec<VideoContentDescription, cricket::VideoCodec>(video_desc,
3149 codec);
3150}
3151
3152bool ParseRtpmapAttribute(const std::string& line,
3153 const MediaType media_type,
deadbeef67cf2c12016-04-13 10:07:16 -07003154 const std::vector<int>& payload_types,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003155 MediaContentDescription* media_desc,
3156 SdpParseError* error) {
3157 std::vector<std::string> fields;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00003158 rtc::split(line.substr(kLinePrefixLength),
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003159 kSdpDelimiterSpace, &fields);
3160 // RFC 4566
3161 // a=rtpmap:<payload type> <encoding name>/<clock rate>[/<encodingparameters>]
3162 const size_t expected_min_fields = 2;
3163 if (fields.size() < expected_min_fields) {
3164 return ParseFailedExpectMinFieldNum(line, expected_min_fields, error);
3165 }
3166 std::string payload_type_value;
3167 if (!GetValue(fields[0], kAttributeRtpmap, &payload_type_value, error)) {
3168 return false;
3169 }
wu@webrtc.org5e760e72014-04-02 23:19:09 +00003170 int payload_type = 0;
pkasting@chromium.orge9facf82015-02-17 20:36:28 +00003171 if (!GetPayloadTypeFromString(line, payload_type_value, &payload_type,
3172 error)) {
wu@webrtc.org5e760e72014-04-02 23:19:09 +00003173 return false;
3174 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003175
deadbeef67cf2c12016-04-13 10:07:16 -07003176 if (std::find(payload_types.begin(), payload_types.end(), payload_type) ==
3177 payload_types.end()) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01003178 RTC_LOG(LS_WARNING) << "Ignore rtpmap line that did not appear in the "
Jonas Olsson45cc8902018-02-13 10:37:07 +01003179 "<fmt> of the m-line: "
3180 << line;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003181 return true;
3182 }
jbauch083b73f2015-07-16 02:46:32 -07003183 const std::string& encoder = fields[1];
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003184 std::vector<std::string> codec_params;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00003185 rtc::split(encoder, '/', &codec_params);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003186 // <encoding name>/<clock rate>[/<encodingparameters>]
3187 // 2 mandatory fields
3188 if (codec_params.size() < 2 || codec_params.size() > 3) {
3189 return ParseFailed(line,
3190 "Expected format \"<encoding name>/<clock rate>"
3191 "[/<encodingparameters>]\".",
3192 error);
3193 }
jbauch083b73f2015-07-16 02:46:32 -07003194 const std::string& encoding_name = codec_params[0];
wu@webrtc.org5e760e72014-04-02 23:19:09 +00003195 int clock_rate = 0;
3196 if (!GetValueFromString(line, codec_params[1], &clock_rate, error)) {
3197 return false;
3198 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003199 if (media_type == cricket::MEDIA_TYPE_VIDEO) {
Steve Antonb1c1de12017-12-21 15:14:30 -08003200 VideoContentDescription* video_desc = media_desc->as_video();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003201 UpdateCodec(payload_type, encoding_name,
deadbeef67cf2c12016-04-13 10:07:16 -07003202 video_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003203 } else if (media_type == cricket::MEDIA_TYPE_AUDIO) {
3204 // RFC 4566
3205 // For audio streams, <encoding parameters> indicates the number
3206 // of audio channels. This parameter is OPTIONAL and may be
3207 // omitted if the number of channels is one, provided that no
3208 // additional parameters are needed.
Peter Kasting69558702016-01-12 16:26:35 -08003209 size_t channels = 1;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003210 if (codec_params.size() == 3) {
wu@webrtc.org5e760e72014-04-02 23:19:09 +00003211 if (!GetValueFromString(line, codec_params[2], &channels, error)) {
3212 return false;
3213 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003214 }
Steve Antonb1c1de12017-12-21 15:14:30 -08003215 AudioContentDescription* audio_desc = media_desc->as_audio();
ossue1405ad2017-01-23 08:55:48 -08003216 UpdateCodec(payload_type, encoding_name, clock_rate, 0, channels,
deadbeef67cf2c12016-04-13 10:07:16 -07003217 audio_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003218 } else if (media_type == cricket::MEDIA_TYPE_DATA) {
Steve Antonb1c1de12017-12-21 15:14:30 -08003219 DataContentDescription* data_desc = media_desc->as_data();
deadbeef67cf2c12016-04-13 10:07:16 -07003220 data_desc->AddCodec(cricket::DataCodec(payload_type, encoding_name));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003221 }
3222 return true;
3223}
3224
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003225bool ParseFmtpParam(const std::string& line, std::string* parameter,
3226 std::string* value, SdpParseError* error) {
Donald Curtis0e07f922015-05-15 09:21:23 -07003227 if (!rtc::tokenize_first(line, kSdpDelimiterEqual, parameter, value)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003228 ParseFailed(line, "Unable to parse fmtp parameter. \'=\' missing.", error);
3229 return false;
3230 }
3231 // a=fmtp:<payload_type> <param1>=<value1>; <param2>=<value2>; ...
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003232 return true;
3233}
3234
3235bool ParseFmtpAttributes(const std::string& line, const MediaType media_type,
3236 MediaContentDescription* media_desc,
3237 SdpParseError* error) {
3238 if (media_type != cricket::MEDIA_TYPE_AUDIO &&
3239 media_type != cricket::MEDIA_TYPE_VIDEO) {
3240 return true;
3241 }
Donald Curtis0e07f922015-05-15 09:21:23 -07003242
3243 std::string line_payload;
3244 std::string line_params;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003245
3246 // RFC 5576
3247 // a=fmtp:<format> <format specific parameters>
3248 // At least two fields, whereas the second one is any of the optional
3249 // parameters.
Donald Curtis144d0182015-05-15 13:14:24 -07003250 if (!rtc::tokenize_first(line.substr(kLinePrefixLength), kSdpDelimiterSpace,
3251 &line_payload, &line_params)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003252 ParseFailedExpectMinFieldNum(line, 2, error);
3253 return false;
3254 }
3255
Donald Curtis0e07f922015-05-15 09:21:23 -07003256 // Parse out the payload information.
pkasting@chromium.orgd3245462015-02-23 21:28:22 +00003257 std::string payload_type_str;
Donald Curtis0e07f922015-05-15 09:21:23 -07003258 if (!GetValue(line_payload, kAttributeFmtp, &payload_type_str, error)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003259 return false;
3260 }
3261
Donald Curtis0e07f922015-05-15 09:21:23 -07003262 int payload_type = 0;
Donald Curtis144d0182015-05-15 13:14:24 -07003263 if (!GetPayloadTypeFromString(line_payload, payload_type_str, &payload_type,
3264 error)) {
Donald Curtis0e07f922015-05-15 09:21:23 -07003265 return false;
3266 }
3267
3268 // Parse out format specific parameters.
3269 std::vector<std::string> fields;
3270 rtc::split(line_params, kSdpDelimiterSemicolon, &fields);
3271
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003272 cricket::CodecParameterMap codec_params;
Donald Curtis144d0182015-05-15 13:14:24 -07003273 for (auto& iter : fields) {
Donald Curtis0e07f922015-05-15 09:21:23 -07003274 if (iter.find(kSdpDelimiterEqual) == std::string::npos) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003275 // Only fmtps with equals are currently supported. Other fmtp types
3276 // should be ignored. Unknown fmtps do not constitute an error.
3277 continue;
3278 }
Donald Curtis0e07f922015-05-15 09:21:23 -07003279
3280 std::string name;
3281 std::string value;
3282 if (!ParseFmtpParam(rtc::string_trim(iter), &name, &value, error)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003283 return false;
3284 }
3285 codec_params[name] = value;
3286 }
3287
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003288 if (media_type == cricket::MEDIA_TYPE_AUDIO) {
3289 UpdateCodec<AudioContentDescription, cricket::AudioCodec>(
pkasting@chromium.orgd3245462015-02-23 21:28:22 +00003290 media_desc, payload_type, codec_params);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003291 } else if (media_type == cricket::MEDIA_TYPE_VIDEO) {
3292 UpdateCodec<VideoContentDescription, cricket::VideoCodec>(
pkasting@chromium.orgd3245462015-02-23 21:28:22 +00003293 media_desc, payload_type, codec_params);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003294 }
3295 return true;
3296}
3297
3298bool ParseRtcpFbAttribute(const std::string& line, const MediaType media_type,
3299 MediaContentDescription* media_desc,
3300 SdpParseError* error) {
3301 if (media_type != cricket::MEDIA_TYPE_AUDIO &&
3302 media_type != cricket::MEDIA_TYPE_VIDEO) {
3303 return true;
3304 }
3305 std::vector<std::string> rtcp_fb_fields;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00003306 rtc::split(line.c_str(), kSdpDelimiterSpace, &rtcp_fb_fields);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003307 if (rtcp_fb_fields.size() < 2) {
3308 return ParseFailedGetValue(line, kAttributeRtcpFb, error);
3309 }
3310 std::string payload_type_string;
3311 if (!GetValue(rtcp_fb_fields[0], kAttributeRtcpFb, &payload_type_string,
3312 error)) {
3313 return false;
3314 }
wu@webrtc.org5e760e72014-04-02 23:19:09 +00003315 int payload_type = kWildcardPayloadType;
3316 if (payload_type_string != "*") {
pkasting@chromium.orge9facf82015-02-17 20:36:28 +00003317 if (!GetPayloadTypeFromString(line, payload_type_string, &payload_type,
3318 error)) {
wu@webrtc.org5e760e72014-04-02 23:19:09 +00003319 return false;
3320 }
3321 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003322 std::string id = rtcp_fb_fields[1];
3323 std::string param = "";
3324 for (std::vector<std::string>::iterator iter = rtcp_fb_fields.begin() + 2;
3325 iter != rtcp_fb_fields.end(); ++iter) {
3326 param.append(*iter);
3327 }
3328 const cricket::FeedbackParam feedback_param(id, param);
3329
3330 if (media_type == cricket::MEDIA_TYPE_AUDIO) {
pkasting@chromium.orgd3245462015-02-23 21:28:22 +00003331 UpdateCodec<AudioContentDescription, cricket::AudioCodec>(
3332 media_desc, payload_type, feedback_param);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003333 } else if (media_type == cricket::MEDIA_TYPE_VIDEO) {
pkasting@chromium.orgd3245462015-02-23 21:28:22 +00003334 UpdateCodec<VideoContentDescription, cricket::VideoCodec>(
3335 media_desc, payload_type, feedback_param);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003336 }
3337 return true;
3338}
3339
3340} // namespace webrtc