blob: f92baa18732947faac6a350be1a5947697eb7be9 [file] [log] [blame]
henrike@webrtc.org28e20752013-07-10 00:45:361/*
kjellanderb24317b2016-02-10 15:54:432 * Copyright 2011 The WebRTC project authors. All Rights Reserved.
henrike@webrtc.org28e20752013-07-10 00:45:363 *
kjellanderb24317b2016-02-10 15:54:434 * 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:369 */
10
Mirko Bonadei92ea95e2017-09-15 04:47:3111#include "pc/webrtcsdp.h"
henrike@webrtc.org28e20752013-07-10 00:45:3612
kjellandera96e2d72016-02-05 07:52:2813#include <ctype.h>
henrike@webrtc.org28e20752013-07-10 00:45:3614#include <limits.h>
15#include <stdio.h>
kwibergd1fe2812016-04-27 13:47:2916
henrike@webrtc.org28e20752013-07-10 00:45:3617#include <algorithm>
Steve Anton36b29d12017-10-30 16:57:4218#include <map>
kwibergd1fe2812016-04-27 13:47:2919#include <memory>
Steve Anton36b29d12017-10-30 16:57:4220#include <set>
henrike@webrtc.org28e20752013-07-10 00:45:3621#include <string>
deadbeef67cf2c12016-04-13 17:07:1622#include <unordered_map>
henrike@webrtc.org28e20752013-07-10 00:45:3623#include <vector>
24
Patrik Höglunde2d6a062017-10-05 12:53:3325#include "api/candidate.h"
Patrik Höglund7aee3d52017-11-15 12:15:1726#include "api/cryptoparams.h"
Mirko Bonadei92ea95e2017-09-15 04:47:3127#include "api/jsepicecandidate.h"
28#include "api/jsepsessiondescription.h"
isheriff6f8d6862016-05-26 18:24:5529// for RtpExtension
Mirko Bonadei92ea95e2017-09-15 04:47:3130#include "api/rtpparameters.h"
31#include "media/base/codec.h"
Mirko Bonadei92ea95e2017-09-15 04:47:3132#include "media/base/mediaconstants.h"
33#include "media/base/rtputils.h"
34#include "media/sctp/sctptransportinternal.h"
Mirko Bonadei92ea95e2017-09-15 04:47:3135#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:3643
44using cricket::AudioContentDescription;
45using cricket::Candidate;
46using cricket::Candidates;
henrike@webrtc.org28e20752013-07-10 00:45:3647using 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:2059using cricket::kCodecParamStartBitrate;
henrike@webrtc.org28e20752013-07-10 00:45:3660using cricket::kCodecParamStereo;
61using cricket::kCodecParamUseInbandFec;
Minyue Li7100dcd2015-03-27 04:05:5962using cricket::kCodecParamUseDtx;
henrike@webrtc.org28e20752013-07-10 00:45:3663using cricket::kCodecParamSctpProtocol;
64using cricket::kCodecParamSctpStreams;
henrike@webrtc.org1e09a712013-07-26 19:17:5965using cricket::kCodecParamMaxAverageBitrate;
buildbot@webrtc.org5d639b32014-09-10 07:57:1266using cricket::kCodecParamMaxPlaybackRate;
stefan@webrtc.org85d27942014-06-09 12:51:3967using cricket::kCodecParamAssociatedPayloadType;
henrike@webrtc.org28e20752013-07-10 00:45:3668using cricket::MediaContentDescription;
69using cricket::MediaType;
isheriff6f8d6862016-05-26 18:24:5570using cricket::RtpHeaderExtensions;
Steve Anton5adfafd2017-12-21 00:34:0071using cricket::MediaProtocolType;
henrike@webrtc.org28e20752013-07-10 00:45:3672using cricket::SsrcGroup;
73using cricket::StreamParams;
74using cricket::StreamParamsVec;
75using cricket::TransportDescription;
76using cricket::TransportInfo;
77using cricket::VideoContentDescription;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:5278using rtc::SocketAddress;
henrike@webrtc.org28e20752013-07-10 00:45:3679
henrike@webrtc.org28e20752013-07-10 00:45:3680namespace cricket {
81class SessionDescription;
82}
83
deadbeef90f1e1e2017-02-10 20:35:0584// TODO(deadbeef): Switch to using anonymous namespace rather than declaring
85// everything "static".
henrike@webrtc.org28e20752013-07-10 00:45:3686namespace 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-17 01:54:1094static const int kLinePrefixLength = 2; // Length of <type>=
henrike@webrtc.org28e20752013-07-10 00:45:3695static 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-17 01:54:10114static const char kAttributeMsid[] = "msid";
deadbeef25ed4352016-12-13 02:37:36115static const char kAttributeBundleOnly[] = "bundle-only";
henrike@webrtc.org28e20752013-07-10 00:45:36116static const char kAttributeRtcpMux[] = "rtcp-mux";
deadbeef13871492015-12-09 20:37:51117static const char kAttributeRtcpReducedSize[] = "rtcp-rsize";
henrike@webrtc.org28e20752013-07-10 00:45:36118static 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
Seth Hampson5b4f0752018-04-02 23:31:36123// This is a legacy field supported only for Plan B semantics.
henrike@webrtc.org28e20752013-07-10 00:45:36124static const char kAttributeMsidSemantics[] = "msid-semantic";
125static const char kMediaStreamSemantic[] = "WMS";
126static const char kSsrcAttributeMsid[] = "msid";
127static const char kDefaultMsid[] = "default";
Seth Hampson5b4f0752018-04-02 23:31:36128static const char kNoStreamMsid[] = "-";
henrike@webrtc.org28e20752013-07-10 00:45:36129static const char kSsrcAttributeMslabel[] = "mslabel";
130static const char kSSrcAttributeLabel[] = "label";
131static const char kAttributeSsrcGroup[] = "ssrc-group";
132static const char kAttributeCrypto[] = "crypto";
133static const char kAttributeCandidate[] = "candidate";
134static const char kAttributeCandidateTyp[] = "typ";
135static const char kAttributeCandidateRaddr[] = "raddr";
136static const char kAttributeCandidateRport[] = "rport";
honghaiza54a0802015-12-17 02:37:23137static const char kAttributeCandidateUfrag[] = "ufrag";
138static const char kAttributeCandidatePwd[] = "pwd";
henrike@webrtc.org28e20752013-07-10 00:45:36139static const char kAttributeCandidateGeneration[] = "generation";
honghaiza0c44ea2016-03-23 23:07:48140static const char kAttributeCandidateNetworkId[] = "network-id";
honghaize1a0c942016-02-16 22:54:56141static const char kAttributeCandidateNetworkCost[] = "network-cost";
henrike@webrtc.org28e20752013-07-10 00:45:36142static const char kAttributeFingerprint[] = "fingerprint";
sergeyu@chromium.org0be6aa02013-08-23 23:21:25143static const char kAttributeSetup[] = "setup";
henrike@webrtc.org28e20752013-07-10 00:45:36144static const char kAttributeFmtp[] = "fmtp";
145static const char kAttributeRtpmap[] = "rtpmap";
wu@webrtc.org78187522013-10-07 23:32:02146static const char kAttributeSctpmap[] = "sctpmap";
henrike@webrtc.org28e20752013-07-10 00:45:36147static const char kAttributeRtcp[] = "rtcp";
148static const char kAttributeIceUfrag[] = "ice-ufrag";
149static const char kAttributeIcePwd[] = "ice-pwd";
150static const char kAttributeIceLite[] = "ice-lite";
151static const char kAttributeIceOption[] = "ice-options";
152static const char kAttributeSendOnly[] = "sendonly";
153static const char kAttributeRecvOnly[] = "recvonly";
154static const char kAttributeRtcpFb[] = "rtcp-fb";
155static const char kAttributeSendRecv[] = "sendrecv";
156static const char kAttributeInactive[] = "inactive";
jiayl@webrtc.orgddb85ab2014-09-05 16:31:56157// draft-ietf-mmusic-sctp-sdp-07
158// a=sctp-port
159static const char kAttributeSctpPort[] = "sctp-port";
henrike@webrtc.org28e20752013-07-10 00:45:36160
161// Experimental flags
162static const char kAttributeXGoogleFlag[] = "x-google-flag";
163static const char kValueConference[] = "conference";
henrike@webrtc.org28e20752013-07-10 00:45:36164
165// Candidate
166static const char kCandidateHost[] = "host";
167static const char kCandidateSrflx[] = "srflx";
Honghai Zhang7fb69db2016-03-14 18:59:18168static const char kCandidatePrflx[] = "prflx";
henrike@webrtc.org28e20752013-07-10 00:45:36169static const char kCandidateRelay[] = "relay";
mallinath@webrtc.org2d60c5e2014-08-08 22:29:20170static const char kTcpCandidateType[] = "tcptype";
henrike@webrtc.org28e20752013-07-10 00:45:36171
172static const char kSdpDelimiterEqual = '=';
173static const char kSdpDelimiterSpace = ' ';
174static const char kSdpDelimiterColon = ':';
175static const char kSdpDelimiterSemicolon = ';';
176static const char kSdpDelimiterSlash = '/';
177static const char kNewLine = '\n';
178static const char kReturn = '\r';
179static const char kLineBreak[] = "\r\n";
180
Steve Anton36b29d12017-10-30 16:57:42181// TODO(deadbeef): Generate the Session and Time description
henrike@webrtc.org28e20752013-07-10 00:45:36182// instead of hardcoding.
183static const char kSessionVersion[] = "v=0";
184// RFC 4566
185static const char kSessionOriginUsername[] = "-";
186static const char kSessionOriginSessionId[] = "0";
187static const char kSessionOriginSessionVersion[] = "0";
188static const char kSessionOriginNettype[] = "IN";
189static const char kSessionOriginAddrtype[] = "IP4";
190static const char kSessionOriginAddress[] = "127.0.0.1";
191static const char kSessionName[] = "s=-";
192static const char kTimeDescription[] = "t=0 0";
193static const char kAttrGroup[] = "a=group:BUNDLE";
194static const char kConnectionNettype[] = "IN";
pthatcher@webrtc.orgc9d6d142014-10-23 23:37:22195static const char kConnectionIpv4Addrtype[] = "IP4";
196static const char kConnectionIpv6Addrtype[] = "IP6";
henrike@webrtc.org28e20752013-07-10 00:45:36197static const char kMediaTypeVideo[] = "video";
198static const char kMediaTypeAudio[] = "audio";
199static const char kMediaTypeData[] = "application";
200static const char kMediaPortRejected[] = "0";
pthatcher@webrtc.orgc9d6d142014-10-23 23:37:22201// draft-ietf-mmusic-trickle-ice-01
202// When no candidates have been gathered, set the connection
203// address to IP6 ::.
perkj@webrtc.orgd105cc82014-11-07 11:22:06204// TODO(perkj): FF can not parse IP6 ::. See http://crbug/430333
205// Use IPV4 per default.
206static const char kDummyAddress[] = "0.0.0.0";
pthatcher@webrtc.orgc9d6d142014-10-23 23:37:22207static const char kDummyPort[] = "9";
henrike@webrtc.org28e20752013-07-10 00:45:36208// RFC 3556
209static const char kApplicationSpecificMaximum[] = "AS";
210
wu@webrtc.org78187522013-10-07 23:32:02211static const char kDefaultSctpmapProtocol[] = "webrtc-datachannel";
henrike@webrtc.org28e20752013-07-10 00:45:36212
pkasting@chromium.orgd3245462015-02-23 21:28:22213// RTP payload type is in the 0-127 range. Use -1 to indicate "all" payload
214// types.
215const int kWildcardPayloadType = -1;
216
henrike@webrtc.org28e20752013-07-10 00:45:36217struct SsrcInfo {
Peter Boström0c4e06b2015-10-07 10:23:21218 uint32_t ssrc_id;
henrike@webrtc.org28e20752013-07-10 00:45:36219 std::string cname;
deadbeef9d3584c2016-02-17 01:54:10220 std::string stream_id;
221 std::string track_id;
henrike@webrtc.org28e20752013-07-10 00:45:36222
223 // For backward compatibility.
224 // TODO(ronghuawu): Remove below 2 fields once all the clients support msid.
225 std::string label;
226 std::string mslabel;
227};
228typedef std::vector<SsrcInfo> SsrcInfoVec;
229typedef std::vector<SsrcGroup> SsrcGroupVec;
230
henrike@webrtc.org28e20752013-07-10 00:45:36231template <class T>
232static void AddFmtpLine(const T& codec, std::string* message);
233static void BuildMediaDescription(const ContentInfo* content_info,
234 const TransportInfo* transport_info,
235 const MediaType media_type,
wu@webrtc.org4c3e9912014-07-16 21:03:13236 const std::vector<Candidate>& candidates,
Steve Antone831b8c2018-02-01 20:22:16237 int msid_signaling,
henrike@webrtc.org28e20752013-07-10 00:45:36238 std::string* message);
zstein4b2e0822017-02-18 03:48:38239static void BuildSctpContentAttributes(std::string* message,
240 int sctp_port,
241 bool use_sctpmap);
deadbeef9d3584c2016-02-17 01:54:10242static void BuildRtpContentAttributes(const MediaContentDescription* media_desc,
243 const MediaType media_type,
Steve Antone831b8c2018-02-01 20:22:16244 int msid_signaling,
deadbeef9d3584c2016-02-17 01:54:10245 std::string* message);
henrike@webrtc.org28e20752013-07-10 00:45:36246static void BuildRtpMap(const MediaContentDescription* media_desc,
247 const MediaType media_type,
248 std::string* message);
249static void BuildCandidate(const std::vector<Candidate>& candidates,
honghaiza54a0802015-12-17 02:37:23250 bool include_ufrag,
henrike@webrtc.org28e20752013-07-10 00:45:36251 std::string* message);
252static void BuildIceOptions(const std::vector<std::string>& transport_options,
253 std::string* message);
pthatcher@webrtc.org3341b402015-02-13 21:14:22254static bool IsRtp(const std::string& protocol);
255static bool IsDtlsSctp(const std::string& protocol);
zhihuang38989e52017-03-21 18:04:53256static bool ParseSessionDescription(const std::string& message,
257 size_t* pos,
henrike@webrtc.org28e20752013-07-10 00:45:36258 std::string* session_id,
259 std::string* session_version,
henrike@webrtc.org28e20752013-07-10 00:45:36260 TransportDescription* session_td,
261 RtpHeaderExtensions* session_extmaps,
zhihuang38989e52017-03-21 18:04:53262 rtc::SocketAddress* connection_addr,
henrike@webrtc.org28e20752013-07-10 00:45:36263 cricket::SessionDescription* desc,
264 SdpParseError* error);
265static bool ParseGroupAttribute(const std::string& line,
266 cricket::SessionDescription* desc,
267 SdpParseError* error);
268static bool ParseMediaDescription(
269 const std::string& message,
270 const TransportDescription& session_td,
271 const RtpHeaderExtensions& session_extmaps,
zhihuang38989e52017-03-21 18:04:53272 size_t* pos,
273 const rtc::SocketAddress& session_connection_addr,
274 cricket::SessionDescription* desc,
henrike@webrtc.org28e20752013-07-10 00:45:36275 std::vector<JsepIceCandidate*>* candidates,
276 SdpParseError* error);
277static bool ParseContent(const std::string& message,
278 const MediaType media_type,
279 int mline_index,
280 const std::string& protocol,
deadbeef67cf2c12016-04-13 17:07:16281 const std::vector<int>& payload_types,
henrike@webrtc.org28e20752013-07-10 00:45:36282 size_t* pos,
283 std::string* content_name,
deadbeef25ed4352016-12-13 02:37:36284 bool* bundle_only,
Steve Antone831b8c2018-02-01 20:22:16285 int* msid_signaling,
henrike@webrtc.org28e20752013-07-10 00:45:36286 MediaContentDescription* media_desc,
287 TransportDescription* transport,
288 std::vector<JsepIceCandidate*>* candidates,
289 SdpParseError* error);
290static bool ParseSsrcAttribute(const std::string& line,
291 SsrcInfoVec* ssrc_infos,
Steve Antone831b8c2018-02-01 20:22:16292 int* msid_signaling,
henrike@webrtc.org28e20752013-07-10 00:45:36293 SdpParseError* error);
294static bool ParseSsrcGroupAttribute(const std::string& line,
295 SsrcGroupVec* ssrc_groups,
296 SdpParseError* error);
297static bool ParseCryptoAttribute(const std::string& line,
298 MediaContentDescription* media_desc,
299 SdpParseError* error);
300static bool ParseRtpmapAttribute(const std::string& line,
301 const MediaType media_type,
deadbeef67cf2c12016-04-13 17:07:16302 const std::vector<int>& payload_types,
henrike@webrtc.org28e20752013-07-10 00:45:36303 MediaContentDescription* media_desc,
304 SdpParseError* error);
305static bool ParseFmtpAttributes(const std::string& line,
306 const MediaType media_type,
307 MediaContentDescription* media_desc,
308 SdpParseError* error);
309static bool ParseFmtpParam(const std::string& line, std::string* parameter,
310 std::string* value, SdpParseError* error);
311static bool ParseCandidate(const std::string& message, Candidate* candidate,
312 SdpParseError* error, bool is_raw);
313static bool ParseRtcpFbAttribute(const std::string& line,
314 const MediaType media_type,
315 MediaContentDescription* media_desc,
316 SdpParseError* error);
317static bool ParseIceOptions(const std::string& line,
318 std::vector<std::string>* transport_options,
319 SdpParseError* error);
320static bool ParseExtmap(const std::string& line,
isheriff6f8d6862016-05-26 18:24:55321 RtpExtension* extmap,
henrike@webrtc.org28e20752013-07-10 00:45:36322 SdpParseError* error);
323static bool ParseFingerprintAttribute(const std::string& line,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52324 rtc::SSLFingerprint** fingerprint,
henrike@webrtc.org28e20752013-07-10 00:45:36325 SdpParseError* error);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25326static bool ParseDtlsSetup(const std::string& line,
327 cricket::ConnectionRole* role,
328 SdpParseError* error);
deadbeef9d3584c2016-02-17 01:54:10329static bool ParseMsidAttribute(const std::string& line,
Seth Hampson5b4f0752018-04-02 23:31:36330 std::vector<std::string>* stream_ids,
deadbeef9d3584c2016-02-17 01:54:10331 std::string* track_id,
332 SdpParseError* error);
henrike@webrtc.org28e20752013-07-10 00:45:36333
334// Helper functions
335
336// Below ParseFailed*** functions output the line that caused the parsing
337// failure and the detailed reason (|description|) of the failure to |error|.
338// The functions always return false so that they can be used directly in the
339// following way when error happens:
340// "return ParseFailed***(...);"
341
342// The line starting at |line_start| of |message| is the failing line.
343// The reason for the failure should be provided in the |description|.
344// An example of a description could be "unknown character".
345static bool ParseFailed(const std::string& message,
346 size_t line_start,
347 const std::string& description,
348 SdpParseError* error) {
349 // Get the first line of |message| from |line_start|.
sergeyu@chromium.org5bc25c42013-12-05 00:24:06350 std::string first_line;
henrike@webrtc.org28e20752013-07-10 00:45:36351 size_t line_end = message.find(kNewLine, line_start);
352 if (line_end != std::string::npos) {
353 if (line_end > 0 && (message.at(line_end - 1) == kReturn)) {
354 --line_end;
355 }
356 first_line = message.substr(line_start, (line_end - line_start));
sergeyu@chromium.org5bc25c42013-12-05 00:24:06357 } else {
358 first_line = message.substr(line_start);
henrike@webrtc.org28e20752013-07-10 00:45:36359 }
360
361 if (error) {
362 error->line = first_line;
363 error->description = description;
364 }
Mirko Bonadei675513b2017-11-09 10:09:25365 RTC_LOG(LS_ERROR) << "Failed to parse: \"" << first_line
366 << "\". Reason: " << description;
henrike@webrtc.org28e20752013-07-10 00:45:36367 return false;
368}
369
370// |line| is the failing line. The reason for the failure should be
371// provided in the |description|.
372static bool ParseFailed(const std::string& line,
373 const std::string& description,
374 SdpParseError* error) {
375 return ParseFailed(line, 0, description, error);
376}
377
378// Parses failure where the failing SDP line isn't know or there are multiple
379// failing lines.
380static bool ParseFailed(const std::string& description,
381 SdpParseError* error) {
382 return ParseFailed("", description, error);
383}
384
385// |line| is the failing line. The failure is due to the fact that |line|
386// doesn't have |expected_fields| fields.
387static bool ParseFailedExpectFieldNum(const std::string& line,
388 int expected_fields,
389 SdpParseError* error) {
390 std::ostringstream description;
391 description << "Expects " << expected_fields << " fields.";
392 return ParseFailed(line, description.str(), error);
393}
394
395// |line| is the failing line. The failure is due to the fact that |line| has
396// less than |expected_min_fields| fields.
397static bool ParseFailedExpectMinFieldNum(const std::string& line,
398 int expected_min_fields,
399 SdpParseError* error) {
400 std::ostringstream description;
401 description << "Expects at least " << expected_min_fields << " fields.";
402 return ParseFailed(line, description.str(), error);
403}
404
405// |line| is the failing line. The failure is due to the fact that it failed to
406// get the value of |attribute|.
407static bool ParseFailedGetValue(const std::string& line,
408 const std::string& attribute,
409 SdpParseError* error) {
410 std::ostringstream description;
411 description << "Failed to get the value of attribute: " << attribute;
412 return ParseFailed(line, description.str(), error);
413}
414
415// The line starting at |line_start| of |message| is the failing line. The
416// failure is due to the line type (e.g. the "m" part of the "m-line")
417// not matching what is expected. The expected line type should be
418// provided as |line_type|.
419static bool ParseFailedExpectLine(const std::string& message,
420 size_t line_start,
421 const char line_type,
422 const std::string& line_value,
423 SdpParseError* error) {
424 std::ostringstream description;
425 description << "Expect line: " << line_type << "=" << line_value;
426 return ParseFailed(message, line_start, description.str(), error);
427}
428
429static bool AddLine(const std::string& line, std::string* message) {
430 if (!message)
431 return false;
432
433 message->append(line);
434 message->append(kLineBreak);
435 return true;
436}
437
438static bool GetLine(const std::string& message,
439 size_t* pos,
440 std::string* line) {
441 size_t line_begin = *pos;
442 size_t line_end = message.find(kNewLine, line_begin);
443 if (line_end == std::string::npos) {
444 return false;
445 }
446 // Update the new start position
447 *pos = line_end + 1;
448 if (line_end > 0 && (message.at(line_end - 1) == kReturn)) {
449 --line_end;
450 }
451 *line = message.substr(line_begin, (line_end - line_begin));
452 const char* cline = line->c_str();
453 // RFC 4566
454 // An SDP session description consists of a number of lines of text of
455 // the form:
456 // <type>=<value>
457 // where <type> MUST be exactly one case-significant character and
458 // <value> is structured text whose format depends on <type>.
459 // Whitespace MUST NOT be used on either side of the "=" sign.
Taylor Brandstetter93a7b242018-04-16 17:45:24460 //
461 // However, an exception to the whitespace rule is made for "s=", since
462 // RFC4566 also says:
463 //
464 // If a session has no meaningful name, the value "s= " SHOULD be used
465 // (i.e., a single space as the session name).
466 if (line->length() < 3 || !islower(cline[0]) ||
henrike@webrtc.org28e20752013-07-10 00:45:36467 cline[1] != kSdpDelimiterEqual ||
Taylor Brandstetter93a7b242018-04-16 17:45:24468 (cline[0] != kLineTypeSessionName && cline[2] == kSdpDelimiterSpace)) {
henrike@webrtc.org28e20752013-07-10 00:45:36469 *pos = line_begin;
470 return false;
471 }
472 return true;
473}
474
475// Init |os| to "|type|=|value|".
476static void InitLine(const char type,
477 const std::string& value,
478 std::ostringstream* os) {
479 os->str("");
480 *os << type << kSdpDelimiterEqual << value;
481}
482
483// Init |os| to "a=|attribute|".
484static void InitAttrLine(const std::string& attribute, std::ostringstream* os) {
485 InitLine(kLineTypeAttributes, attribute, os);
486}
487
488// Writes a SDP attribute line based on |attribute| and |value| to |message|.
489static void AddAttributeLine(const std::string& attribute, int value,
490 std::string* message) {
491 std::ostringstream os;
492 InitAttrLine(attribute, &os);
493 os << kSdpDelimiterColon << value;
494 AddLine(os.str(), message);
495}
496
henrike@webrtc.org28e20752013-07-10 00:45:36497static bool IsLineType(const std::string& message,
498 const char type,
499 size_t line_start) {
500 if (message.size() < line_start + kLinePrefixLength) {
501 return false;
502 }
503 const char* cmessage = message.c_str();
504 return (cmessage[line_start] == type &&
505 cmessage[line_start + 1] == kSdpDelimiterEqual);
506}
507
508static bool IsLineType(const std::string& line,
509 const char type) {
510 return IsLineType(line, type, 0);
511}
512
513static bool GetLineWithType(const std::string& message, size_t* pos,
514 std::string* line, const char type) {
515 if (!IsLineType(message, type, *pos)) {
516 return false;
517 }
518
519 if (!GetLine(message, pos, line))
520 return false;
521
522 return true;
523}
524
525static bool HasAttribute(const std::string& line,
526 const std::string& attribute) {
527 return (line.compare(kLinePrefixLength, attribute.size(), attribute) == 0);
528}
529
Peter Boström0c4e06b2015-10-07 10:23:21530static bool AddSsrcLine(uint32_t ssrc_id,
531 const std::string& attribute,
532 const std::string& value,
533 std::string* message) {
henrike@webrtc.org28e20752013-07-10 00:45:36534 // RFC 5576
535 // a=ssrc:<ssrc-id> <attribute>:<value>
536 std::ostringstream os;
537 InitAttrLine(kAttributeSsrc, &os);
538 os << kSdpDelimiterColon << ssrc_id << kSdpDelimiterSpace
539 << attribute << kSdpDelimiterColon << value;
540 return AddLine(os.str(), message);
541}
542
henrike@webrtc.org28e20752013-07-10 00:45:36543// Get value only from <attribute>:<value>.
544static bool GetValue(const std::string& message, const std::string& attribute,
545 std::string* value, SdpParseError* error) {
546 std::string leftpart;
Donald Curtis0e07f922015-05-15 16:21:23547 if (!rtc::tokenize_first(message, kSdpDelimiterColon, &leftpart, value)) {
henrike@webrtc.org28e20752013-07-10 00:45:36548 return ParseFailedGetValue(message, attribute, error);
549 }
550 // The left part should end with the expected attribute.
551 if (leftpart.length() < attribute.length() ||
552 leftpart.compare(leftpart.length() - attribute.length(),
553 attribute.length(), attribute) != 0) {
554 return ParseFailedGetValue(message, attribute, error);
555 }
556 return true;
557}
558
559static bool CaseInsensitiveFind(std::string str1, std::string str2) {
560 std::transform(str1.begin(), str1.end(), str1.begin(),
561 ::tolower);
562 std::transform(str2.begin(), str2.end(), str2.begin(),
563 ::tolower);
564 return str1.find(str2) != std::string::npos;
565}
566
wu@webrtc.org5e760e72014-04-02 23:19:09567template <class T>
568static bool GetValueFromString(const std::string& line,
569 const std::string& s,
570 T* t,
571 SdpParseError* error) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52572 if (!rtc::FromString(s, t)) {
wu@webrtc.org5e760e72014-04-02 23:19:09573 std::ostringstream description;
574 description << "Invalid value: " << s << ".";
575 return ParseFailed(line, description.str(), error);
576 }
577 return true;
578}
579
pkasting@chromium.orge9facf82015-02-17 20:36:28580static bool GetPayloadTypeFromString(const std::string& line,
581 const std::string& s,
582 int* payload_type,
583 SdpParseError* error) {
584 return GetValueFromString(line, s, payload_type, error) &&
585 cricket::IsValidRtpPayloadType(*payload_type);
586}
587
Seth Hampson5897a6e2018-04-03 18:16:33588// Creates a StreamParams track in the case when no SSRC lines are signaled.
589// This is a track that does not contain SSRCs and only contains
590// stream_ids/track_id if it's signaled with a=msid lines.
591void CreateTrackWithNoSsrcs(const std::vector<std::string>& msid_stream_ids,
592 const std::string& msid_track_id,
593 StreamParamsVec* tracks) {
594 StreamParams track;
595 if (msid_track_id.empty() || msid_stream_ids.empty()) {
596 // We only create an unsignaled track if a=msid lines were signaled.
597 return;
598 }
599 track.set_stream_ids(msid_stream_ids);
600 track.id = msid_track_id;
601 tracks->push_back(track);
602}
603
604// Creates the StreamParams tracks, for the case when SSRC lines are signaled.
605// |msid_stream_ids| and |msid_track_id| represent the stream/track ID from the
Taylor Brandstetter5de6b752016-03-10 01:02:30606// "a=msid" attribute, if it exists. They are empty if the attribute does not
Seth Hampson5897a6e2018-04-03 18:16:33607// exist. We prioritize getting stream_ids/track_ids signaled in a=msid lines.
henrike@webrtc.org28e20752013-07-10 00:45:36608void CreateTracksFromSsrcInfos(const SsrcInfoVec& ssrc_infos,
Seth Hampson5b4f0752018-04-02 23:31:36609 const std::vector<std::string>& msid_stream_ids,
Taylor Brandstetter5de6b752016-03-10 01:02:30610 const std::string& msid_track_id,
Seth Hampson5b4f0752018-04-02 23:31:36611 StreamParamsVec* tracks,
612 int msid_signaling) {
nisseede5da42017-01-12 13:15:36613 RTC_DCHECK(tracks != NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36614 for (SsrcInfoVec::const_iterator ssrc_info = ssrc_infos.begin();
615 ssrc_info != ssrc_infos.end(); ++ssrc_info) {
616 if (ssrc_info->cname.empty()) {
617 continue;
618 }
619
Seth Hampson5b4f0752018-04-02 23:31:36620 std::vector<std::string> stream_ids;
henrike@webrtc.org28e20752013-07-10 00:45:36621 std::string track_id;
Seth Hampson5b4f0752018-04-02 23:31:36622 if (msid_signaling & cricket::kMsidSignalingMediaSection) {
623 // This is the case with Unified Plan SDP msid signaling.
624 stream_ids = msid_stream_ids;
Tomas Gunnarsson191bf5c2018-03-30 10:44:43625 track_id = msid_track_id;
Seth Hampson5b4f0752018-04-02 23:31:36626 } else if (msid_signaling & cricket::kMsidSignalingSsrcAttribute) {
627 // This is the case with Plan B SDP msid signaling.
628 stream_ids.push_back(ssrc_info->stream_id);
Tomas Gunnarsson191bf5c2018-03-30 10:44:43629 track_id = ssrc_info->track_id;
Seth Hampson5b4f0752018-04-02 23:31:36630 } else if (!ssrc_info->mslabel.empty()) {
631 // Since there's no a=msid or a=ssrc msid signaling, this is a sdp from
632 // an older version of client that doesn't support msid.
633 // In that case, we use the mslabel and label to construct the track.
634 stream_ids.push_back(ssrc_info->mslabel);
635 track_id = ssrc_info->label;
636 } else {
637 // Since no media streams isn't supported with older SDP signaling, we
638 // use a default a stream id.
639 stream_ids.push_back(kDefaultMsid);
henrike@webrtc.org28e20752013-07-10 00:45:36640 }
Seth Hampson5b4f0752018-04-02 23:31:36641 // If a track ID wasn't populated from the SSRC attributes OR the
Taylor Brandstetter5de6b752016-03-10 01:02:30642 // msid attribute, use default/random values.
Taylor Brandstetter5de6b752016-03-10 01:02:30643 if (track_id.empty()) {
644 // TODO(ronghuawu): What should we do if the track id doesn't appear?
645 // Create random string (which will be used as track label later)?
646 track_id = rtc::CreateRandomString(8);
henrike@webrtc.org28e20752013-07-10 00:45:36647 }
648
649 StreamParamsVec::iterator track = tracks->begin();
650 for (; track != tracks->end(); ++track) {
651 if (track->id == track_id) {
652 break;
653 }
654 }
655 if (track == tracks->end()) {
656 // If we don't find an existing track, create a new one.
657 tracks->push_back(StreamParams());
658 track = tracks->end() - 1;
659 }
660 track->add_ssrc(ssrc_info->ssrc_id);
661 track->cname = ssrc_info->cname;
Seth Hampson5b4f0752018-04-02 23:31:36662 track->set_stream_ids(stream_ids);
henrike@webrtc.org28e20752013-07-10 00:45:36663 track->id = track_id;
664 }
665}
666
Seth Hampson845e8782018-03-02 19:34:10667void GetMediaStreamIds(const ContentInfo* content,
668 std::set<std::string>* labels) {
Steve Antonb1c1de12017-12-21 23:14:30669 for (const StreamParams& stream_params :
670 content->media_description()->streams()) {
Seth Hampson845e8782018-03-02 19:34:10671 for (const std::string& stream_id : stream_params.stream_ids()) {
672 labels->insert(stream_id);
Steve Anton5a26a3a2018-02-28 19:38:47673 }
henrike@webrtc.org28e20752013-07-10 00:45:36674 }
675}
676
677// RFC 5245
678// It is RECOMMENDED that default candidates be chosen based on the
679// likelihood of those candidates to work with the peer that is being
680// contacted. It is RECOMMENDED that relayed > reflexive > host.
681static const int kPreferenceUnknown = 0;
682static const int kPreferenceHost = 1;
683static const int kPreferenceReflexive = 2;
684static const int kPreferenceRelayed = 3;
685
686static int GetCandidatePreferenceFromType(const std::string& type) {
687 int preference = kPreferenceUnknown;
688 if (type == cricket::LOCAL_PORT_TYPE) {
689 preference = kPreferenceHost;
690 } else if (type == cricket::STUN_PORT_TYPE) {
691 preference = kPreferenceReflexive;
692 } else if (type == cricket::RELAY_PORT_TYPE) {
693 preference = kPreferenceRelayed;
694 } else {
nissec80e7412017-01-11 13:56:46695 RTC_NOTREACHED();
henrike@webrtc.org28e20752013-07-10 00:45:36696 }
697 return preference;
698}
699
guoweis@webrtc.org57ac2c82015-02-06 00:45:13700// Get ip and port of the default destination from the |candidates| with the
701// given value of |component_id|. The default candidate should be the one most
702// likely to work, typically IPv4 relay.
henrike@webrtc.org28e20752013-07-10 00:45:36703// RFC 5245
704// The value of |component_id| currently supported are 1 (RTP) and 2 (RTCP).
Steve Anton36b29d12017-10-30 16:57:42705// TODO(deadbeef): Decide the default destination in webrtcsession and
henrike@webrtc.org28e20752013-07-10 00:45:36706// pass it down via SessionDescription.
pthatcher@webrtc.orgc9d6d142014-10-23 23:37:22707static void GetDefaultDestination(
708 const std::vector<Candidate>& candidates,
709 int component_id, std::string* port,
710 std::string* ip, std::string* addr_type) {
perkj@webrtc.orgd105cc82014-11-07 11:22:06711 *addr_type = kConnectionIpv4Addrtype;
pthatcher@webrtc.orgc9d6d142014-10-23 23:37:22712 *port = kDummyPort;
713 *ip = kDummyAddress;
henrike@webrtc.org28e20752013-07-10 00:45:36714 int current_preference = kPreferenceUnknown;
guoweis@webrtc.org57ac2c82015-02-06 00:45:13715 int current_family = AF_UNSPEC;
henrike@webrtc.org28e20752013-07-10 00:45:36716 for (std::vector<Candidate>::const_iterator it = candidates.begin();
717 it != candidates.end(); ++it) {
718 if (it->component() != component_id) {
719 continue;
720 }
guoweis@webrtc.org57ac2c82015-02-06 00:45:13721 // Default destination should be UDP only.
722 if (it->protocol() != cricket::UDP_PROTOCOL_NAME) {
henrike@webrtc.org28e20752013-07-10 00:45:36723 continue;
724 }
guoweis@webrtc.org57ac2c82015-02-06 00:45:13725 const int preference = GetCandidatePreferenceFromType(it->type());
726 const int family = it->address().ipaddr().family();
727 // See if this candidate is more preferable then the current one if it's the
728 // same family. Or if the current family is IPv4 already so we could safely
729 // ignore all IPv6 ones. WebRTC bug 4269.
730 // http://code.google.com/p/webrtc/issues/detail?id=4269
731 if ((preference <= current_preference && current_family == family) ||
732 (current_family == AF_INET && family == AF_INET6)) {
733 continue;
734 }
pthatcher@webrtc.orgc9d6d142014-10-23 23:37:22735 if (family == AF_INET) {
736 addr_type->assign(kConnectionIpv4Addrtype);
737 } else if (family == AF_INET6) {
738 addr_type->assign(kConnectionIpv6Addrtype);
739 }
guoweis@webrtc.org57ac2c82015-02-06 00:45:13740 current_preference = preference;
741 current_family = family;
742 *port = it->address().PortAsString();
743 *ip = it->address().ipaddr().ToString();
henrike@webrtc.org28e20752013-07-10 00:45:36744 }
henrike@webrtc.org28e20752013-07-10 00:45:36745}
746
wu@webrtc.org4c3e9912014-07-16 21:03:13747// Gets "a=rtcp" line if found default RTCP candidate from |candidates|.
748static std::string GetRtcpLine(const std::vector<Candidate>& candidates) {
pthatcher@webrtc.orgc9d6d142014-10-23 23:37:22749 std::string rtcp_line, rtcp_port, rtcp_ip, addr_type;
750 GetDefaultDestination(candidates, ICE_CANDIDATE_COMPONENT_RTCP,
751 &rtcp_port, &rtcp_ip, &addr_type);
752 // Found default RTCP candidate.
753 // RFC 5245
754 // If the agent is utilizing RTCP, it MUST encode the RTCP candidate
755 // using the a=rtcp attribute as defined in RFC 3605.
henrike@webrtc.org28e20752013-07-10 00:45:36756
pthatcher@webrtc.orgc9d6d142014-10-23 23:37:22757 // RFC 3605
758 // rtcp-attribute = "a=rtcp:" port [nettype space addrtype space
759 // connection-address] CRLF
760 std::ostringstream os;
761 InitAttrLine(kAttributeRtcp, &os);
762 os << kSdpDelimiterColon
763 << rtcp_port << " "
764 << kConnectionNettype << " "
765 << addr_type << " "
766 << rtcp_ip;
767 rtcp_line = os.str();
wu@webrtc.org4c3e9912014-07-16 21:03:13768 return rtcp_line;
henrike@webrtc.org28e20752013-07-10 00:45:36769}
770
771// Get candidates according to the mline index from SessionDescriptionInterface.
772static void GetCandidatesByMindex(const SessionDescriptionInterface& desci,
773 int mline_index,
774 std::vector<Candidate>* candidates) {
775 if (!candidates) {
776 return;
777 }
778 const IceCandidateCollection* cc = desci.candidates(mline_index);
779 for (size_t i = 0; i < cc->count(); ++i) {
780 const IceCandidateInterface* candidate = cc->at(i);
781 candidates->push_back(candidate->candidate());
782 }
783}
784
deadbeef90f1e1e2017-02-10 20:35:05785static bool IsValidPort(int port) {
786 return port >= 0 && port <= 65535;
787}
788
Steve Antone831b8c2018-02-01 20:22:16789std::string SdpSerialize(const JsepSessionDescription& jdesc) {
henrike@webrtc.org28e20752013-07-10 00:45:36790 const cricket::SessionDescription* desc = jdesc.description();
791 if (!desc) {
792 return "";
793 }
794
795 std::string message;
796
797 // Session Description.
798 AddLine(kSessionVersion, &message);
799 // Session Origin
800 // RFC 4566
801 // o=<username> <sess-id> <sess-version> <nettype> <addrtype>
802 // <unicast-address>
803 std::ostringstream os;
804 InitLine(kLineTypeOrigin, kSessionOriginUsername, &os);
jbauch083b73f2015-07-16 09:46:32805 const std::string& session_id = jdesc.session_id().empty() ?
henrike@webrtc.org28e20752013-07-10 00:45:36806 kSessionOriginSessionId : jdesc.session_id();
jbauch083b73f2015-07-16 09:46:32807 const std::string& session_version = jdesc.session_version().empty() ?
henrike@webrtc.org28e20752013-07-10 00:45:36808 kSessionOriginSessionVersion : jdesc.session_version();
809 os << " " << session_id << " " << session_version << " "
810 << kSessionOriginNettype << " " << kSessionOriginAddrtype << " "
811 << kSessionOriginAddress;
812 AddLine(os.str(), &message);
813 AddLine(kSessionName, &message);
814
815 // Time Description.
816 AddLine(kTimeDescription, &message);
817
818 // Group
819 if (desc->HasGroup(cricket::GROUP_TYPE_BUNDLE)) {
820 std::string group_line = kAttrGroup;
821 const cricket::ContentGroup* group =
822 desc->GetGroupByName(cricket::GROUP_TYPE_BUNDLE);
nisseede5da42017-01-12 13:15:36823 RTC_DCHECK(group != NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36824 const cricket::ContentNames& content_names = group->content_names();
825 for (cricket::ContentNames::const_iterator it = content_names.begin();
826 it != content_names.end(); ++it) {
827 group_line.append(" ");
828 group_line.append(*it);
829 }
830 AddLine(group_line, &message);
831 }
832
833 // MediaStream semantics
834 InitAttrLine(kAttributeMsidSemantics, &os);
835 os << kSdpDelimiterColon << " " << kMediaStreamSemantic;
wu@webrtc.org4c3e9912014-07-16 21:03:13836
Seth Hampson845e8782018-03-02 19:34:10837 std::set<std::string> media_stream_ids;
henrike@webrtc.org28e20752013-07-10 00:45:36838 const ContentInfo* audio_content = GetFirstAudioContent(desc);
839 if (audio_content)
Seth Hampson845e8782018-03-02 19:34:10840 GetMediaStreamIds(audio_content, &media_stream_ids);
wu@webrtc.org4c3e9912014-07-16 21:03:13841
henrike@webrtc.org28e20752013-07-10 00:45:36842 const ContentInfo* video_content = GetFirstVideoContent(desc);
843 if (video_content)
Seth Hampson845e8782018-03-02 19:34:10844 GetMediaStreamIds(video_content, &media_stream_ids);
wu@webrtc.org4c3e9912014-07-16 21:03:13845
Seth Hampson845e8782018-03-02 19:34:10846 for (std::set<std::string>::const_iterator it = media_stream_ids.begin();
847 it != media_stream_ids.end(); ++it) {
henrike@webrtc.org28e20752013-07-10 00:45:36848 os << " " << *it;
849 }
850 AddLine(os.str(), &message);
851
wu@webrtc.org4c3e9912014-07-16 21:03:13852 // Preserve the order of the media contents.
853 int mline_index = -1;
854 for (cricket::ContentInfos::const_iterator it = desc->contents().begin();
855 it != desc->contents().end(); ++it) {
Steve Antonb1c1de12017-12-21 23:14:30856 const MediaContentDescription* mdesc = it->media_description();
wu@webrtc.org4c3e9912014-07-16 21:03:13857 std::vector<Candidate> candidates;
858 GetCandidatesByMindex(jdesc, ++mline_index, &candidates);
deadbeef9d3584c2016-02-17 01:54:10859 BuildMediaDescription(&*it, desc->GetTransportInfoByName(it->name),
Steve Antone831b8c2018-02-01 20:22:16860 mdesc->type(), candidates, desc->msid_signaling(),
wu@webrtc.org4c3e9912014-07-16 21:03:13861 &message);
henrike@webrtc.org28e20752013-07-10 00:45:36862 }
henrike@webrtc.org28e20752013-07-10 00:45:36863 return message;
864}
865
866// Serializes the passed in IceCandidateInterface to a SDP string.
867// candidate - The candidate to be serialized.
Honghai Zhang7fb69db2016-03-14 18:59:18868std::string SdpSerializeCandidate(const IceCandidateInterface& candidate) {
869 return SdpSerializeCandidate(candidate.candidate());
870}
871
872// Serializes a cricket Candidate.
873std::string SdpSerializeCandidate(const cricket::Candidate& candidate) {
henrike@webrtc.org28e20752013-07-10 00:45:36874 std::string message;
Honghai Zhang7fb69db2016-03-14 18:59:18875 std::vector<cricket::Candidate> candidates(1, candidate);
honghaiza54a0802015-12-17 02:37:23876 BuildCandidate(candidates, true, &message);
wu@webrtc.orgec9f5fb2014-06-24 17:05:10877 // From WebRTC draft section 4.8.1.1 candidate-attribute will be
878 // just candidate:<candidate> not a=candidate:<blah>CRLF
nisseede5da42017-01-12 13:15:36879 RTC_DCHECK(message.find("a=") == 0);
wu@webrtc.orgec9f5fb2014-06-24 17:05:10880 message.erase(0, 2);
nisseede5da42017-01-12 13:15:36881 RTC_DCHECK(message.find(kLineBreak) == message.size() - 2);
wu@webrtc.orgec9f5fb2014-06-24 17:05:10882 message.resize(message.size() - 2);
henrike@webrtc.org28e20752013-07-10 00:45:36883 return message;
884}
885
886bool SdpDeserialize(const std::string& message,
887 JsepSessionDescription* jdesc,
888 SdpParseError* error) {
889 std::string session_id;
890 std::string session_version;
Peter Thatcher7cbd1882015-09-18 01:54:52891 TransportDescription session_td("", "");
henrike@webrtc.org28e20752013-07-10 00:45:36892 RtpHeaderExtensions session_extmaps;
zhihuang38989e52017-03-21 18:04:53893 rtc::SocketAddress session_connection_addr;
henrike@webrtc.org28e20752013-07-10 00:45:36894 cricket::SessionDescription* desc = new cricket::SessionDescription();
895 std::vector<JsepIceCandidate*> candidates;
896 size_t current_pos = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36897
898 // Session Description
899 if (!ParseSessionDescription(message, &current_pos, &session_id,
deadbeefc80741f2015-10-22 20:14:45900 &session_version, &session_td, &session_extmaps,
zhihuang38989e52017-03-21 18:04:53901 &session_connection_addr, desc, error)) {
henrike@webrtc.org28e20752013-07-10 00:45:36902 delete desc;
903 return false;
904 }
905
906 // Media Description
deadbeefc80741f2015-10-22 20:14:45907 if (!ParseMediaDescription(message, session_td, session_extmaps, &current_pos,
zhihuang38989e52017-03-21 18:04:53908 session_connection_addr, desc, &candidates,
909 error)) {
henrike@webrtc.org28e20752013-07-10 00:45:36910 delete desc;
911 for (std::vector<JsepIceCandidate*>::const_iterator
912 it = candidates.begin(); it != candidates.end(); ++it) {
913 delete *it;
914 }
915 return false;
916 }
917
918 jdesc->Initialize(desc, session_id, session_version);
919
920 for (std::vector<JsepIceCandidate*>::const_iterator
921 it = candidates.begin(); it != candidates.end(); ++it) {
922 jdesc->AddCandidate(*it);
923 delete *it;
924 }
925 return true;
926}
927
928bool SdpDeserializeCandidate(const std::string& message,
929 JsepIceCandidate* jcandidate,
930 SdpParseError* error) {
nisseede5da42017-01-12 13:15:36931 RTC_DCHECK(jcandidate != NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36932 Candidate candidate;
933 if (!ParseCandidate(message, &candidate, error, true)) {
934 return false;
935 }
936 jcandidate->SetCandidate(candidate);
937 return true;
938}
939
Honghai Zhang7fb69db2016-03-14 18:59:18940bool SdpDeserializeCandidate(const std::string& transport_name,
941 const std::string& message,
942 cricket::Candidate* candidate,
943 SdpParseError* error) {
nisseede5da42017-01-12 13:15:36944 RTC_DCHECK(candidate != nullptr);
Honghai Zhang7fb69db2016-03-14 18:59:18945 if (!ParseCandidate(message, candidate, error, true)) {
946 return false;
947 }
948 candidate->set_transport_name(transport_name);
949 return true;
950}
951
henrike@webrtc.org28e20752013-07-10 00:45:36952bool ParseCandidate(const std::string& message, Candidate* candidate,
953 SdpParseError* error, bool is_raw) {
nisseede5da42017-01-12 13:15:36954 RTC_DCHECK(candidate != NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36955
956 // Get the first line from |message|.
jiayl@webrtc.org7ec3f9f2014-08-08 23:09:15957 std::string first_line = message;
958 size_t pos = 0;
959 GetLine(message, &pos, &first_line);
henrike@webrtc.org28e20752013-07-10 00:45:36960
jiayl@webrtc.org7ec3f9f2014-08-08 23:09:15961 // Makes sure |message| contains only one line.
962 if (message.size() > first_line.size()) {
963 std::string left, right;
Donald Curtis0e07f922015-05-15 16:21:23964 if (rtc::tokenize_first(message, kNewLine, &left, &right) &&
965 !right.empty()) {
jiayl@webrtc.org7ec3f9f2014-08-08 23:09:15966 return ParseFailed(message, 0, "Expect one line only", error);
967 }
968 }
969
970 // From WebRTC draft section 4.8.1.1 candidate-attribute should be
971 // candidate:<candidate> when trickled, but we still support
972 // a=candidate:<blah>CRLF for backward compatibility and for parsing a line
973 // from the SDP.
974 if (IsLineType(first_line, kLineTypeAttributes)) {
975 first_line = first_line.substr(kLinePrefixLength);
976 }
977
978 std::string attribute_candidate;
979 std::string candidate_value;
980
981 // |first_line| must be in the form of "candidate:<value>".
Donald Curtis144d0182015-05-15 20:14:24982 if (!rtc::tokenize_first(first_line, kSdpDelimiterColon, &attribute_candidate,
983 &candidate_value) ||
jiayl@webrtc.org7ec3f9f2014-08-08 23:09:15984 attribute_candidate != kAttributeCandidate) {
henrike@webrtc.org28e20752013-07-10 00:45:36985 if (is_raw) {
986 std::ostringstream description;
jiayl@webrtc.org7ec3f9f2014-08-08 23:09:15987 description << "Expect line: " << kAttributeCandidate
henrike@webrtc.org28e20752013-07-10 00:45:36988 << ":" << "<candidate-str>";
989 return ParseFailed(first_line, 0, description.str(), error);
990 } else {
991 return ParseFailedExpectLine(first_line, 0, kLineTypeAttributes,
992 kAttributeCandidate, error);
993 }
994 }
995
996 std::vector<std::string> fields;
jiayl@webrtc.org7ec3f9f2014-08-08 23:09:15997 rtc::split(candidate_value, kSdpDelimiterSpace, &fields);
998
henrike@webrtc.org28e20752013-07-10 00:45:36999 // RFC 5245
1000 // a=candidate:<foundation> <component-id> <transport> <priority>
1001 // <connection-address> <port> typ <candidate-types>
1002 // [raddr <connection-address>] [rport <port>]
1003 // *(SP extension-att-name SP extension-att-value)
1004 const size_t expected_min_fields = 8;
1005 if (fields.size() < expected_min_fields ||
1006 (fields[6] != kAttributeCandidateTyp)) {
1007 return ParseFailedExpectMinFieldNum(first_line, expected_min_fields, error);
1008 }
jbauch083b73f2015-07-16 09:46:321009 const std::string& foundation = fields[0];
jiayl@webrtc.org7ec3f9f2014-08-08 23:09:151010
wu@webrtc.org5e760e72014-04-02 23:19:091011 int component_id = 0;
1012 if (!GetValueFromString(first_line, fields[1], &component_id, error)) {
1013 return false;
1014 }
jbauch083b73f2015-07-16 09:46:321015 const std::string& transport = fields[2];
Peter Boström0c4e06b2015-10-07 10:23:211016 uint32_t priority = 0;
wu@webrtc.org5e760e72014-04-02 23:19:091017 if (!GetValueFromString(first_line, fields[3], &priority, error)) {
1018 return false;
1019 }
jbauch083b73f2015-07-16 09:46:321020 const std::string& connection_address = fields[4];
wu@webrtc.org5e760e72014-04-02 23:19:091021 int port = 0;
1022 if (!GetValueFromString(first_line, fields[5], &port, error)) {
1023 return false;
1024 }
deadbeef90f1e1e2017-02-10 20:35:051025 if (!IsValidPort(port)) {
1026 return ParseFailed(first_line, "Invalid port number.", error);
1027 }
henrike@webrtc.org28e20752013-07-10 00:45:361028 SocketAddress address(connection_address, port);
1029
1030 cricket::ProtocolType protocol;
hnslb68cc752016-12-13 18:33:411031 if (!StringToProto(transport.c_str(), &protocol)) {
henrike@webrtc.org28e20752013-07-10 00:45:361032 return ParseFailed(first_line, "Unsupported transport type.", error);
1033 }
hnslb68cc752016-12-13 18:33:411034 switch (protocol) {
1035 case cricket::PROTO_UDP:
1036 case cricket::PROTO_TCP:
1037 case cricket::PROTO_SSLTCP:
1038 // Supported protocol.
1039 break;
1040 default:
1041 return ParseFailed(first_line, "Unsupported transport type.", error);
1042 }
henrike@webrtc.org28e20752013-07-10 00:45:361043
1044 std::string candidate_type;
jbauch083b73f2015-07-16 09:46:321045 const std::string& type = fields[7];
henrike@webrtc.org28e20752013-07-10 00:45:361046 if (type == kCandidateHost) {
1047 candidate_type = cricket::LOCAL_PORT_TYPE;
1048 } else if (type == kCandidateSrflx) {
1049 candidate_type = cricket::STUN_PORT_TYPE;
1050 } else if (type == kCandidateRelay) {
1051 candidate_type = cricket::RELAY_PORT_TYPE;
Honghai Zhang7fb69db2016-03-14 18:59:181052 } else if (type == kCandidatePrflx) {
1053 candidate_type = cricket::PRFLX_PORT_TYPE;
henrike@webrtc.org28e20752013-07-10 00:45:361054 } else {
1055 return ParseFailed(first_line, "Unsupported candidate type.", error);
1056 }
1057
1058 size_t current_position = expected_min_fields;
1059 SocketAddress related_address;
1060 // The 2 optional fields for related address
1061 // [raddr <connection-address>] [rport <port>]
1062 if (fields.size() >= (current_position + 2) &&
1063 fields[current_position] == kAttributeCandidateRaddr) {
1064 related_address.SetIP(fields[++current_position]);
1065 ++current_position;
1066 }
1067 if (fields.size() >= (current_position + 2) &&
1068 fields[current_position] == kAttributeCandidateRport) {
wu@webrtc.org5e760e72014-04-02 23:19:091069 int port = 0;
1070 if (!GetValueFromString(
1071 first_line, fields[++current_position], &port, error)) {
1072 return false;
1073 }
deadbeef90f1e1e2017-02-10 20:35:051074 if (!IsValidPort(port)) {
1075 return ParseFailed(first_line, "Invalid port number.", error);
1076 }
wu@webrtc.org5e760e72014-04-02 23:19:091077 related_address.SetPort(port);
henrike@webrtc.org28e20752013-07-10 00:45:361078 ++current_position;
1079 }
1080
mallinath@webrtc.org2d60c5e2014-08-08 22:29:201081 // If this is a TCP candidate, it has additional extension as defined in
1082 // RFC 6544.
1083 std::string tcptype;
1084 if (fields.size() >= (current_position + 2) &&
1085 fields[current_position] == kTcpCandidateType) {
1086 tcptype = fields[++current_position];
1087 ++current_position;
1088
1089 if (tcptype != cricket::TCPTYPE_ACTIVE_STR &&
1090 tcptype != cricket::TCPTYPE_PASSIVE_STR &&
1091 tcptype != cricket::TCPTYPE_SIMOPEN_STR) {
1092 return ParseFailed(first_line, "Invalid TCP candidate type.", error);
1093 }
1094
1095 if (protocol != cricket::PROTO_TCP) {
1096 return ParseFailed(first_line, "Invalid non-TCP candidate", error);
1097 }
1098 }
1099
henrike@webrtc.org28e20752013-07-10 00:45:361100 // Extension
honghaiza54a0802015-12-17 02:37:231101 // Though non-standard, we support the ICE ufrag and pwd being signaled on
1102 // the candidate to avoid issues with confusing which generation a candidate
1103 // belongs to when trickling multiple generations at the same time.
henrike@webrtc.org28e20752013-07-10 00:45:361104 std::string username;
1105 std::string password;
Peter Boström0c4e06b2015-10-07 10:23:211106 uint32_t generation = 0;
honghaiza0c44ea2016-03-23 23:07:481107 uint16_t network_id = 0;
1108 uint16_t network_cost = 0;
henrike@webrtc.org28e20752013-07-10 00:45:361109 for (size_t i = current_position; i + 1 < fields.size(); ++i) {
1110 // RFC 5245
1111 // *(SP extension-att-name SP extension-att-value)
1112 if (fields[i] == kAttributeCandidateGeneration) {
wu@webrtc.org5e760e72014-04-02 23:19:091113 if (!GetValueFromString(first_line, fields[++i], &generation, error)) {
1114 return false;
1115 }
honghaiza54a0802015-12-17 02:37:231116 } else if (fields[i] == kAttributeCandidateUfrag) {
henrike@webrtc.org28e20752013-07-10 00:45:361117 username = fields[++i];
honghaiza54a0802015-12-17 02:37:231118 } else if (fields[i] == kAttributeCandidatePwd) {
henrike@webrtc.org28e20752013-07-10 00:45:361119 password = fields[++i];
honghaiza0c44ea2016-03-23 23:07:481120 } else if (fields[i] == kAttributeCandidateNetworkId) {
1121 if (!GetValueFromString(first_line, fields[++i], &network_id, error)) {
1122 return false;
1123 }
honghaize1a0c942016-02-16 22:54:561124 } else if (fields[i] == kAttributeCandidateNetworkCost) {
1125 if (!GetValueFromString(first_line, fields[++i], &network_cost, error)) {
1126 return false;
1127 }
Honghai Zhang351d77b2016-05-20 22:08:291128 network_cost = std::min(network_cost, rtc::kNetworkCostMax);
henrike@webrtc.org28e20752013-07-10 00:45:361129 } else {
1130 // Skip the unknown extension.
1131 ++i;
1132 }
1133 }
1134
guoweis@webrtc.org61c12472015-01-15 06:53:071135 *candidate = Candidate(component_id, cricket::ProtoToString(protocol),
guoweis@webrtc.org950c5182014-12-16 23:01:311136 address, priority, username, password, candidate_type,
honghaiza0c44ea2016-03-23 23:07:481137 generation, foundation, network_id, network_cost);
henrike@webrtc.org28e20752013-07-10 00:45:361138 candidate->set_related_address(related_address);
mallinath@webrtc.org2d60c5e2014-08-08 22:29:201139 candidate->set_tcptype(tcptype);
henrike@webrtc.org28e20752013-07-10 00:45:361140 return true;
1141}
1142
1143bool ParseIceOptions(const std::string& line,
1144 std::vector<std::string>* transport_options,
1145 SdpParseError* error) {
1146 std::string ice_options;
1147 if (!GetValue(line, kAttributeIceOption, &ice_options, error)) {
1148 return false;
1149 }
1150 std::vector<std::string> fields;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:521151 rtc::split(ice_options, kSdpDelimiterSpace, &fields);
henrike@webrtc.org28e20752013-07-10 00:45:361152 for (size_t i = 0; i < fields.size(); ++i) {
1153 transport_options->push_back(fields[i]);
1154 }
1155 return true;
1156}
1157
jiayl@webrtc.orgddb85ab2014-09-05 16:31:561158bool ParseSctpPort(const std::string& line,
1159 int* sctp_port,
1160 SdpParseError* error) {
1161 // draft-ietf-mmusic-sctp-sdp-07
1162 // a=sctp-port
1163 std::vector<std::string> fields;
jiayl@webrtc.orgddb85ab2014-09-05 16:31:561164 const size_t expected_min_fields = 2;
lally69f57602015-10-08 17:15:041165 rtc::split(line.substr(kLinePrefixLength), kSdpDelimiterColon, &fields);
1166 if (fields.size() < expected_min_fields) {
1167 fields.resize(0);
1168 rtc::split(line.substr(kLinePrefixLength), kSdpDelimiterSpace, &fields);
1169 }
jiayl@webrtc.orgddb85ab2014-09-05 16:31:561170 if (fields.size() < expected_min_fields) {
1171 return ParseFailedExpectMinFieldNum(line, expected_min_fields, error);
1172 }
1173 if (!rtc::FromString(fields[1], sctp_port)) {
lally69f57602015-10-08 17:15:041174 return ParseFailed(line, "Invalid sctp port value.", error);
jiayl@webrtc.orgddb85ab2014-09-05 16:31:561175 }
1176 return true;
1177}
1178
isheriff6f8d6862016-05-26 18:24:551179bool ParseExtmap(const std::string& line,
1180 RtpExtension* extmap,
henrike@webrtc.org28e20752013-07-10 00:45:361181 SdpParseError* error) {
1182 // RFC 5285
1183 // a=extmap:<value>["/"<direction>] <URI> <extensionattributes>
1184 std::vector<std::string> fields;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:521185 rtc::split(line.substr(kLinePrefixLength),
henrike@webrtc.org28e20752013-07-10 00:45:361186 kSdpDelimiterSpace, &fields);
1187 const size_t expected_min_fields = 2;
1188 if (fields.size() < expected_min_fields) {
1189 return ParseFailedExpectMinFieldNum(line, expected_min_fields, error);
1190 }
1191 std::string uri = fields[1];
1192
1193 std::string value_direction;
1194 if (!GetValue(fields[0], kAttributeExtmap, &value_direction, error)) {
1195 return false;
1196 }
1197 std::vector<std::string> sub_fields;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:521198 rtc::split(value_direction, kSdpDelimiterSlash, &sub_fields);
wu@webrtc.org5e760e72014-04-02 23:19:091199 int value = 0;
1200 if (!GetValueFromString(line, sub_fields[0], &value, error)) {
1201 return false;
1202 }
henrike@webrtc.org28e20752013-07-10 00:45:361203
jbauch5869f502017-06-29 19:31:361204 bool encrypted = false;
1205 if (uri == RtpExtension::kEncryptHeaderExtensionsUri) {
1206 // RFC 6904
Steve Anton36b29d12017-10-30 16:57:421207 // a=extmap:<value["/"<direction>] urn:ietf:params:rtp-hdrext:encrypt <URI>
1208 // <extensionattributes>
jbauch5869f502017-06-29 19:31:361209 const size_t expected_min_fields_encrypted = expected_min_fields + 1;
1210 if (fields.size() < expected_min_fields_encrypted) {
1211 return ParseFailedExpectMinFieldNum(line, expected_min_fields_encrypted,
1212 error);
1213 }
1214
1215 encrypted = true;
1216 uri = fields[2];
1217 if (uri == RtpExtension::kEncryptHeaderExtensionsUri) {
1218 return ParseFailed(line, "Recursive encrypted header.", error);
1219 }
1220 }
1221
1222 *extmap = RtpExtension(uri, value, encrypted);
henrike@webrtc.org28e20752013-07-10 00:45:361223 return true;
1224}
1225
1226void BuildMediaDescription(const ContentInfo* content_info,
1227 const TransportInfo* transport_info,
1228 const MediaType media_type,
wu@webrtc.org4c3e9912014-07-16 21:03:131229 const std::vector<Candidate>& candidates,
Steve Antone831b8c2018-02-01 20:22:161230 int msid_signaling,
henrike@webrtc.org28e20752013-07-10 00:45:361231 std::string* message) {
nisseede5da42017-01-12 13:15:361232 RTC_DCHECK(message != NULL);
henrike@webrtc.org28e20752013-07-10 00:45:361233 if (content_info == NULL || message == NULL) {
1234 return;
1235 }
Steve Anton36b29d12017-10-30 16:57:421236 // TODO(deadbeef): Rethink if we should use sprintfn instead of stringstream.
henrike@webrtc.org28e20752013-07-10 00:45:361237 // According to the style guide, streams should only be used for logging.
1238 // http://google-styleguide.googlecode.com/svn/
1239 // trunk/cppguide.xml?showone=Streams#Streams
1240 std::ostringstream os;
Steve Antonb1c1de12017-12-21 23:14:301241 const MediaContentDescription* media_desc = content_info->media_description();
1242 RTC_DCHECK(media_desc);
henrike@webrtc.org28e20752013-07-10 00:45:361243
jiayl@webrtc.org9c16c392014-05-01 18:30:301244 int sctp_port = cricket::kSctpDefaultPort;
henrike@webrtc.org28e20752013-07-10 00:45:361245
1246 // RFC 4566
1247 // m=<media> <port> <proto> <fmt>
1248 // fmt is a list of payload type numbers that MAY be used in the session.
1249 const char* type = NULL;
1250 if (media_type == cricket::MEDIA_TYPE_AUDIO)
1251 type = kMediaTypeAudio;
1252 else if (media_type == cricket::MEDIA_TYPE_VIDEO)
1253 type = kMediaTypeVideo;
1254 else if (media_type == cricket::MEDIA_TYPE_DATA)
1255 type = kMediaTypeData;
1256 else
nissec80e7412017-01-11 13:56:461257 RTC_NOTREACHED();
henrike@webrtc.org28e20752013-07-10 00:45:361258
1259 std::string fmt;
1260 if (media_type == cricket::MEDIA_TYPE_VIDEO) {
Steve Antonb1c1de12017-12-21 23:14:301261 const VideoContentDescription* video_desc = media_desc->as_video();
henrike@webrtc.org28e20752013-07-10 00:45:361262 for (std::vector<cricket::VideoCodec>::const_iterator it =
1263 video_desc->codecs().begin();
1264 it != video_desc->codecs().end(); ++it) {
1265 fmt.append(" ");
buildbot@webrtc.orgd4e598d2014-07-29 17:36:521266 fmt.append(rtc::ToString<int>(it->id));
henrike@webrtc.org28e20752013-07-10 00:45:361267 }
1268 } else if (media_type == cricket::MEDIA_TYPE_AUDIO) {
Steve Antonb1c1de12017-12-21 23:14:301269 const AudioContentDescription* audio_desc = media_desc->as_audio();
henrike@webrtc.org28e20752013-07-10 00:45:361270 for (std::vector<cricket::AudioCodec>::const_iterator it =
1271 audio_desc->codecs().begin();
1272 it != audio_desc->codecs().end(); ++it) {
1273 fmt.append(" ");
buildbot@webrtc.orgd4e598d2014-07-29 17:36:521274 fmt.append(rtc::ToString<int>(it->id));
henrike@webrtc.org28e20752013-07-10 00:45:361275 }
1276 } else if (media_type == cricket::MEDIA_TYPE_DATA) {
Steve Antonb1c1de12017-12-21 23:14:301277 const DataContentDescription* data_desc = media_desc->as_data();
pthatcher@webrtc.org3341b402015-02-13 21:14:221278 if (IsDtlsSctp(media_desc->protocol())) {
henrike@webrtc.org28e20752013-07-10 00:45:361279 fmt.append(" ");
jiayl@webrtc.org9c16c392014-05-01 18:30:301280
zstein4b2e0822017-02-18 03:48:381281 if (data_desc->use_sctpmap()) {
1282 for (std::vector<cricket::DataCodec>::const_iterator it =
1283 data_desc->codecs().begin();
1284 it != data_desc->codecs().end(); ++it) {
1285 if (cricket::CodecNamesEq(it->name,
1286 cricket::kGoogleSctpDataCodecName) &&
1287 it->GetParam(cricket::kCodecParamPort, &sctp_port)) {
1288 break;
1289 }
jiayl@webrtc.org9c16c392014-05-01 18:30:301290 }
jiayl@webrtc.org9c16c392014-05-01 18:30:301291
zstein4b2e0822017-02-18 03:48:381292 fmt.append(rtc::ToString<int>(sctp_port));
1293 } else {
1294 fmt.append(kDefaultSctpmapProtocol);
1295 }
henrike@webrtc.org28e20752013-07-10 00:45:361296 } else {
henrike@webrtc.org28e20752013-07-10 00:45:361297 for (std::vector<cricket::DataCodec>::const_iterator it =
1298 data_desc->codecs().begin();
1299 it != data_desc->codecs().end(); ++it) {
1300 fmt.append(" ");
buildbot@webrtc.orgd4e598d2014-07-29 17:36:521301 fmt.append(rtc::ToString<int>(it->id));
henrike@webrtc.org28e20752013-07-10 00:45:361302 }
1303 }
1304 }
1305 // The fmt must never be empty. If no codecs are found, set the fmt attribute
1306 // to 0.
1307 if (fmt.empty()) {
1308 fmt = " 0";
1309 }
1310
deadbeef25ed4352016-12-13 02:37:361311 // The port number in the m line will be updated later when associated with
henrike@webrtc.org28e20752013-07-10 00:45:361312 // the candidates.
deadbeef25ed4352016-12-13 02:37:361313 //
1314 // A port value of 0 indicates that the m= section is rejected.
henrike@webrtc.org28e20752013-07-10 00:45:361315 // RFC 3264
1316 // To reject an offered stream, the port number in the corresponding stream in
1317 // the answer MUST be set to zero.
deadbeef25ed4352016-12-13 02:37:361318 //
1319 // However, the BUNDLE draft adds a new meaning to port zero, when used along
1320 // with a=bundle-only.
zhihuang38989e52017-03-21 18:04:531321 std::string port = kDummyPort;
1322 if (content_info->rejected || content_info->bundle_only) {
1323 port = kMediaPortRejected;
1324 } else if (!media_desc->connection_address().IsNil()) {
1325 port = rtc::ToString(media_desc->connection_address().port());
1326 }
henrike@webrtc.org28e20752013-07-10 00:45:361327
buildbot@webrtc.orgd4e598d2014-07-29 17:36:521328 rtc::SSLFingerprint* fp = (transport_info) ?
henrike@webrtc.org28e20752013-07-10 00:45:361329 transport_info->description.identity_fingerprint.get() : NULL;
1330
wu@webrtc.org4c3e9912014-07-16 21:03:131331 // Add the m and c lines.
henrike@webrtc.org28e20752013-07-10 00:45:361332 InitLine(kLineTypeMedia, type, &os);
1333 os << " " << port << " " << media_desc->protocol() << fmt;
zhihuang38989e52017-03-21 18:04:531334 AddLine(os.str(), message);
1335
1336 InitLine(kLineTypeConnection, kConnectionNettype, &os);
1337 if (media_desc->connection_address().IsNil()) {
1338 os << " " << kConnectionIpv4Addrtype << " " << kDummyAddress;
1339 } else if (media_desc->connection_address().family() == AF_INET) {
1340 os << " " << kConnectionIpv4Addrtype << " "
1341 << media_desc->connection_address().ipaddr().ToString();
1342 } else {
1343 os << " " << kConnectionIpv6Addrtype << " "
1344 << media_desc->connection_address().ipaddr().ToString();
1345 }
1346 AddLine(os.str(), message);
wu@webrtc.org4c3e9912014-07-16 21:03:131347
1348 // RFC 4566
1349 // b=AS:<bandwidth>
Peter Thatcherc0c3a862015-06-24 22:31:251350 if (media_desc->bandwidth() >= 1000) {
wu@webrtc.org4c3e9912014-07-16 21:03:131351 InitLine(kLineTypeSessionBandwidth, kApplicationSpecificMaximum, &os);
1352 os << kSdpDelimiterColon << (media_desc->bandwidth() / 1000);
1353 AddLine(os.str(), message);
1354 }
1355
deadbeef25ed4352016-12-13 02:37:361356 // Add the a=bundle-only line.
1357 if (content_info->bundle_only) {
1358 InitAttrLine(kAttributeBundleOnly, &os);
1359 AddLine(os.str(), message);
1360 }
1361
wu@webrtc.org4c3e9912014-07-16 21:03:131362 // Add the a=rtcp line.
pthatcher@webrtc.org3341b402015-02-13 21:14:221363 if (IsRtp(media_desc->protocol())) {
wu@webrtc.org4c3e9912014-07-16 21:03:131364 std::string rtcp_line = GetRtcpLine(candidates);
1365 if (!rtcp_line.empty()) {
1366 AddLine(rtcp_line, message);
1367 }
1368 }
1369
honghaiza54a0802015-12-17 02:37:231370 // Build the a=candidate lines. We don't include ufrag and pwd in the
1371 // candidates in the SDP to avoid redundancy.
1372 BuildCandidate(candidates, false, message);
henrike@webrtc.org28e20752013-07-10 00:45:361373
1374 // Use the transport_info to build the media level ice-ufrag and ice-pwd.
1375 if (transport_info) {
1376 // RFC 5245
1377 // ice-pwd-att = "ice-pwd" ":" password
1378 // ice-ufrag-att = "ice-ufrag" ":" ufrag
1379 // ice-ufrag
deadbeef3f7219b2015-12-28 23:17:141380 if (!transport_info->description.ice_ufrag.empty()) {
1381 InitAttrLine(kAttributeIceUfrag, &os);
1382 os << kSdpDelimiterColon << transport_info->description.ice_ufrag;
1383 AddLine(os.str(), message);
1384 }
henrike@webrtc.org28e20752013-07-10 00:45:361385 // ice-pwd
deadbeef3f7219b2015-12-28 23:17:141386 if (!transport_info->description.ice_pwd.empty()) {
1387 InitAttrLine(kAttributeIcePwd, &os);
1388 os << kSdpDelimiterColon << transport_info->description.ice_pwd;
1389 AddLine(os.str(), message);
1390 }
henrike@webrtc.org28e20752013-07-10 00:45:361391
1392 // draft-petithuguenin-mmusic-ice-attributes-level-03
1393 BuildIceOptions(transport_info->description.transport_options, message);
1394
1395 // RFC 4572
1396 // fingerprint-attribute =
1397 // "fingerprint" ":" hash-func SP fingerprint
1398 if (fp) {
1399 // Insert the fingerprint attribute.
1400 InitAttrLine(kAttributeFingerprint, &os);
1401 os << kSdpDelimiterColon
1402 << fp->algorithm << kSdpDelimiterSpace
1403 << fp->GetRfc4572Fingerprint();
henrike@webrtc.org28e20752013-07-10 00:45:361404 AddLine(os.str(), message);
sergeyu@chromium.org0be6aa02013-08-23 23:21:251405
1406 // Inserting setup attribute.
1407 if (transport_info->description.connection_role !=
1408 cricket::CONNECTIONROLE_NONE) {
1409 // Making sure we are not using "passive" mode.
1410 cricket::ConnectionRole role =
1411 transport_info->description.connection_role;
mallinath@webrtc.org19f27e62013-10-13 17:18:271412 std::string dtls_role_str;
nissec16fa5e2017-02-07 15:18:431413 const bool success =
1414 cricket::ConnectionRoleToString(role, &dtls_role_str);
1415 RTC_DCHECK(success);
sergeyu@chromium.org0be6aa02013-08-23 23:21:251416 InitAttrLine(kAttributeSetup, &os);
sergeyu@chromium.org0be6aa02013-08-23 23:21:251417 os << kSdpDelimiterColon << dtls_role_str;
1418 AddLine(os.str(), message);
1419 }
henrike@webrtc.org28e20752013-07-10 00:45:361420 }
1421 }
1422
1423 // RFC 3388
1424 // mid-attribute = "a=mid:" identification-tag
1425 // identification-tag = token
1426 // Use the content name as the mid identification-tag.
1427 InitAttrLine(kAttributeMid, &os);
1428 os << kSdpDelimiterColon << content_info->name;
1429 AddLine(os.str(), message);
1430
pthatcher@webrtc.org3341b402015-02-13 21:14:221431 if (IsDtlsSctp(media_desc->protocol())) {
Steve Antonb1c1de12017-12-21 23:14:301432 const DataContentDescription* data_desc = media_desc->as_data();
zstein4b2e0822017-02-18 03:48:381433 bool use_sctpmap = data_desc->use_sctpmap();
1434 BuildSctpContentAttributes(message, sctp_port, use_sctpmap);
pthatcher@webrtc.org3341b402015-02-13 21:14:221435 } else if (IsRtp(media_desc->protocol())) {
Steve Antone831b8c2018-02-01 20:22:161436 BuildRtpContentAttributes(media_desc, media_type, msid_signaling, message);
henrike@webrtc.org28e20752013-07-10 00:45:361437 }
1438}
1439
zstein4b2e0822017-02-18 03:48:381440void BuildSctpContentAttributes(std::string* message,
1441 int sctp_port,
1442 bool use_sctpmap) {
wu@webrtc.org78187522013-10-07 23:32:021443 std::ostringstream os;
zstein4b2e0822017-02-18 03:48:381444 if (use_sctpmap) {
1445 // draft-ietf-mmusic-sctp-sdp-04
1446 // a=sctpmap:sctpmap-number protocol [streams]
1447 InitAttrLine(kAttributeSctpmap, &os);
1448 os << kSdpDelimiterColon << sctp_port << kSdpDelimiterSpace
1449 << kDefaultSctpmapProtocol << kSdpDelimiterSpace
1450 << cricket::kMaxSctpStreams;
1451 } else {
1452 // draft-ietf-mmusic-sctp-sdp-23
1453 // a=sctp-port:<port>
1454 InitAttrLine(kAttributeSctpPort, &os);
1455 os << kSdpDelimiterColon << sctp_port;
1456 // TODO(zstein): emit max-message-size here
1457 }
wu@webrtc.org78187522013-10-07 23:32:021458 AddLine(os.str(), message);
henrike@webrtc.org28e20752013-07-10 00:45:361459}
1460
deadbeef9d3584c2016-02-17 01:54:101461void BuildRtpContentAttributes(const MediaContentDescription* media_desc,
1462 const MediaType media_type,
Steve Antone831b8c2018-02-01 20:22:161463 int msid_signaling,
deadbeef9d3584c2016-02-17 01:54:101464 std::string* message) {
henrike@webrtc.org28e20752013-07-10 00:45:361465 std::ostringstream os;
1466 // RFC 5285
1467 // a=extmap:<value>["/"<direction>] <URI> <extensionattributes>
1468 // The definitions MUST be either all session level or all media level. This
1469 // implementation uses all media level.
1470 for (size_t i = 0; i < media_desc->rtp_header_extensions().size(); ++i) {
jbauch5869f502017-06-29 19:31:361471 const RtpExtension& extension = media_desc->rtp_header_extensions()[i];
henrike@webrtc.org28e20752013-07-10 00:45:361472 InitAttrLine(kAttributeExtmap, &os);
jbauch5869f502017-06-29 19:31:361473 os << kSdpDelimiterColon << extension.id;
1474 if (extension.encrypt) {
1475 os << kSdpDelimiterSpace << RtpExtension::kEncryptHeaderExtensionsUri;
1476 }
1477 os << kSdpDelimiterSpace << extension.uri;
henrike@webrtc.org28e20752013-07-10 00:45:361478 AddLine(os.str(), message);
1479 }
1480
1481 // RFC 3264
1482 // a=sendrecv || a=sendonly || a=sendrecv || a=inactive
deadbeefc80741f2015-10-22 20:14:451483 switch (media_desc->direction()) {
Steve Anton4e70a722017-11-28 22:57:101484 case RtpTransceiverDirection::kInactive:
henrike@webrtc.org28e20752013-07-10 00:45:361485 InitAttrLine(kAttributeInactive, &os);
1486 break;
Steve Anton4e70a722017-11-28 22:57:101487 case RtpTransceiverDirection::kSendOnly:
henrike@webrtc.org28e20752013-07-10 00:45:361488 InitAttrLine(kAttributeSendOnly, &os);
1489 break;
Steve Anton4e70a722017-11-28 22:57:101490 case RtpTransceiverDirection::kRecvOnly:
henrike@webrtc.org28e20752013-07-10 00:45:361491 InitAttrLine(kAttributeRecvOnly, &os);
1492 break;
Steve Anton4e70a722017-11-28 22:57:101493 case RtpTransceiverDirection::kSendRecv:
henrike@webrtc.org28e20752013-07-10 00:45:361494 default:
1495 InitAttrLine(kAttributeSendRecv, &os);
1496 break;
1497 }
1498 AddLine(os.str(), message);
1499
Seth Hampson5b4f0752018-04-02 23:31:361500 // Specified in https://datatracker.ietf.org/doc/draft-ietf-mmusic-msid/16/
1501 // a=msid:<msid-id> <msid-appdata>
1502 // The msid-id is a 1*64 token char representing the media stream id, and the
1503 // msid-appdata is a 1*64 token char representing the track id. There is a
1504 // line for every media stream, with a special msid-id value of "-"
1505 // representing no streams. The value of "msid-appdata" MUST be identical for
1506 // all lines.
Steve Antone831b8c2018-02-01 20:22:161507 if (msid_signaling & cricket::kMsidSignalingMediaSection) {
1508 const StreamParamsVec& streams = media_desc->streams();
1509 if (streams.size() == 1u) {
1510 const StreamParams& track = streams[0];
Seth Hampson5b4f0752018-04-02 23:31:361511 std::vector<std::string> stream_ids = track.stream_ids();
1512 if (stream_ids.empty()) {
1513 stream_ids.push_back(kNoStreamMsid);
1514 }
1515 for (const std::string& stream_id : stream_ids) {
1516 InitAttrLine(kAttributeMsid, &os);
1517 os << kSdpDelimiterColon << stream_id << kSdpDelimiterSpace << track.id;
1518 AddLine(os.str(), message);
1519 }
Steve Antone831b8c2018-02-01 20:22:161520 } else if (streams.size() > 1u) {
1521 RTC_LOG(LS_WARNING)
1522 << "Trying to serialize Unified Plan SDP with more than "
Jonas Olsson45cc8902018-02-13 09:37:071523 "one track in a media section. Omitting 'a=msid'.";
deadbeef9d3584c2016-02-17 01:54:101524 }
1525 }
1526
henrike@webrtc.org28e20752013-07-10 00:45:361527 // RFC 5761
1528 // a=rtcp-mux
1529 if (media_desc->rtcp_mux()) {
1530 InitAttrLine(kAttributeRtcpMux, &os);
1531 AddLine(os.str(), message);
1532 }
1533
deadbeef13871492015-12-09 20:37:511534 // RFC 5506
1535 // a=rtcp-rsize
1536 if (media_desc->rtcp_reduced_size()) {
1537 InitAttrLine(kAttributeRtcpReducedSize, &os);
1538 AddLine(os.str(), message);
1539 }
1540
deadbeefd45aea82017-09-16 08:24:291541 if (media_desc->conference_mode()) {
1542 InitAttrLine(kAttributeXGoogleFlag, &os);
1543 os << kSdpDelimiterColon << kValueConference;
1544 AddLine(os.str(), message);
1545 }
1546
henrike@webrtc.org28e20752013-07-10 00:45:361547 // RFC 4568
1548 // a=crypto:<tag> <crypto-suite> <key-params> [<session-params>]
1549 for (std::vector<CryptoParams>::const_iterator it =
1550 media_desc->cryptos().begin();
1551 it != media_desc->cryptos().end(); ++it) {
1552 InitAttrLine(kAttributeCrypto, &os);
1553 os << kSdpDelimiterColon << it->tag << " " << it->cipher_suite << " "
1554 << it->key_params;
1555 if (!it->session_params.empty()) {
1556 os << " " << it->session_params;
1557 }
1558 AddLine(os.str(), message);
1559 }
1560
1561 // RFC 4566
1562 // a=rtpmap:<payload type> <encoding name>/<clock rate>
1563 // [/<encodingparameters>]
1564 BuildRtpMap(media_desc, media_type, message);
1565
henrike@webrtc.org28e20752013-07-10 00:45:361566 for (StreamParamsVec::const_iterator track = media_desc->streams().begin();
1567 track != media_desc->streams().end(); ++track) {
henrike@webrtc.org28e20752013-07-10 00:45:361568 // Build the ssrc-group lines.
1569 for (size_t i = 0; i < track->ssrc_groups.size(); ++i) {
1570 // RFC 5576
1571 // a=ssrc-group:<semantics> <ssrc-id> ...
1572 if (track->ssrc_groups[i].ssrcs.empty()) {
1573 continue;
1574 }
henrike@webrtc.org28e20752013-07-10 00:45:361575 InitAttrLine(kAttributeSsrcGroup, &os);
1576 os << kSdpDelimiterColon << track->ssrc_groups[i].semantics;
Peter Boström0c4e06b2015-10-07 10:23:211577 std::vector<uint32_t>::const_iterator ssrc =
henrike@webrtc.org28e20752013-07-10 00:45:361578 track->ssrc_groups[i].ssrcs.begin();
1579 for (; ssrc != track->ssrc_groups[i].ssrcs.end(); ++ssrc) {
Peter Boström0c4e06b2015-10-07 10:23:211580 os << kSdpDelimiterSpace << rtc::ToString<uint32_t>(*ssrc);
henrike@webrtc.org28e20752013-07-10 00:45:361581 }
1582 AddLine(os.str(), message);
1583 }
1584 // Build the ssrc lines for each ssrc.
1585 for (size_t i = 0; i < track->ssrcs.size(); ++i) {
Peter Boström0c4e06b2015-10-07 10:23:211586 uint32_t ssrc = track->ssrcs[i];
henrike@webrtc.org28e20752013-07-10 00:45:361587 // RFC 5576
1588 // a=ssrc:<ssrc-id> cname:<value>
1589 AddSsrcLine(ssrc, kSsrcAttributeCname,
1590 track->cname, message);
1591
Steve Antone831b8c2018-02-01 20:22:161592 if (msid_signaling & cricket::kMsidSignalingSsrcAttribute) {
1593 // draft-alvestrand-mmusic-msid-00
1594 // a=ssrc:<ssrc-id> msid:identifier [appdata]
1595 // The appdata consists of the "id" attribute of a MediaStreamTrack,
1596 // which corresponds to the "id" attribute of StreamParams.
Seth Hampson5b4f0752018-04-02 23:31:361597 // Since a=ssrc msid signaling is used in Plan B SDP semantics, and
1598 // multiple stream ids are not supported for Plan B, we are only adding
1599 // a line for the first media stream id here.
Seth Hampson845e8782018-03-02 19:34:101600 const std::string& stream_id = track->first_stream_id();
Steve Antone831b8c2018-02-01 20:22:161601 InitAttrLine(kAttributeSsrc, &os);
1602 os << kSdpDelimiterColon << ssrc << kSdpDelimiterSpace
1603 << kSsrcAttributeMsid << kSdpDelimiterColon << stream_id
1604 << kSdpDelimiterSpace << track->id;
1605 AddLine(os.str(), message);
henrike@webrtc.org28e20752013-07-10 00:45:361606
Steve Antone831b8c2018-02-01 20:22:161607 // TODO(ronghuawu): Remove below code which is for backward
1608 // compatibility.
1609 // draft-alvestrand-rtcweb-mid-01
1610 // a=ssrc:<ssrc-id> mslabel:<value>
1611 // The label isn't yet defined.
1612 // a=ssrc:<ssrc-id> label:<value>
Seth Hampson5b4f0752018-04-02 23:31:361613 AddSsrcLine(ssrc, kSsrcAttributeMslabel, stream_id, message);
Steve Antone831b8c2018-02-01 20:22:161614 AddSsrcLine(ssrc, kSSrcAttributeLabel, track->id, message);
1615 }
henrike@webrtc.org28e20752013-07-10 00:45:361616 }
1617 }
1618}
1619
1620void WriteFmtpHeader(int payload_type, std::ostringstream* os) {
1621 // fmtp header: a=fmtp:|payload_type| <parameters>
1622 // Add a=fmtp
1623 InitAttrLine(kAttributeFmtp, os);
1624 // Add :|payload_type|
1625 *os << kSdpDelimiterColon << payload_type;
1626}
1627
1628void WriteRtcpFbHeader(int payload_type, std::ostringstream* os) {
1629 // rtcp-fb header: a=rtcp-fb:|payload_type|
1630 // <parameters>/<ccm <ccm_parameters>>
1631 // Add a=rtcp-fb
1632 InitAttrLine(kAttributeRtcpFb, os);
1633 // Add :
1634 *os << kSdpDelimiterColon;
1635 if (payload_type == kWildcardPayloadType) {
1636 *os << "*";
1637 } else {
1638 *os << payload_type;
1639 }
1640}
1641
1642void WriteFmtpParameter(const std::string& parameter_name,
1643 const std::string& parameter_value,
1644 std::ostringstream* os) {
1645 // fmtp parameters: |parameter_name|=|parameter_value|
1646 *os << parameter_name << kSdpDelimiterEqual << parameter_value;
1647}
1648
1649void WriteFmtpParameters(const cricket::CodecParameterMap& parameters,
1650 std::ostringstream* os) {
1651 for (cricket::CodecParameterMap::const_iterator fmtp = parameters.begin();
1652 fmtp != parameters.end(); ++fmtp) {
hta62a216e2016-04-15 18:02:141653 // Parameters are a semicolon-separated list, no spaces.
1654 // The list is separated from the header by a space.
1655 if (fmtp == parameters.begin()) {
1656 *os << kSdpDelimiterSpace;
1657 } else {
henrike@webrtc.org28e20752013-07-10 00:45:361658 *os << kSdpDelimiterSemicolon;
1659 }
henrike@webrtc.org28e20752013-07-10 00:45:361660 WriteFmtpParameter(fmtp->first, fmtp->second, os);
1661 }
1662}
1663
1664bool IsFmtpParam(const std::string& name) {
ossuaa4b0772017-01-30 15:41:181665 // RFC 4855, section 3 specifies the mapping of media format parameters to SDP
1666 // parameters. Only ptime, maxptime, channels and rate are placed outside of
1667 // the fmtp line. In WebRTC, channels and rate are already handled separately
1668 // and thus not included in the CodecParameterMap.
1669 return name != kCodecParamPTime && name != kCodecParamMaxPTime;
henrike@webrtc.org28e20752013-07-10 00:45:361670}
1671
1672// Retreives fmtp parameters from |params|, which may contain other parameters
1673// as well, and puts them in |fmtp_parameters|.
1674void GetFmtpParams(const cricket::CodecParameterMap& params,
1675 cricket::CodecParameterMap* fmtp_parameters) {
1676 for (cricket::CodecParameterMap::const_iterator iter = params.begin();
1677 iter != params.end(); ++iter) {
1678 if (IsFmtpParam(iter->first)) {
1679 (*fmtp_parameters)[iter->first] = iter->second;
1680 }
1681 }
1682}
1683
1684template <class T>
1685void AddFmtpLine(const T& codec, std::string* message) {
1686 cricket::CodecParameterMap fmtp_parameters;
1687 GetFmtpParams(codec.params, &fmtp_parameters);
1688 if (fmtp_parameters.empty()) {
1689 // No need to add an fmtp if it will have no (optional) parameters.
1690 return;
1691 }
1692 std::ostringstream os;
1693 WriteFmtpHeader(codec.id, &os);
1694 WriteFmtpParameters(fmtp_parameters, &os);
1695 AddLine(os.str(), message);
1696 return;
1697}
1698
1699template <class T>
1700void AddRtcpFbLines(const T& codec, std::string* message) {
1701 for (std::vector<cricket::FeedbackParam>::const_iterator iter =
1702 codec.feedback_params.params().begin();
1703 iter != codec.feedback_params.params().end(); ++iter) {
1704 std::ostringstream os;
1705 WriteRtcpFbHeader(codec.id, &os);
1706 os << " " << iter->id();
1707 if (!iter->param().empty()) {
1708 os << " " << iter->param();
1709 }
1710 AddLine(os.str(), message);
1711 }
1712}
1713
jiayl@webrtc.orgddb85ab2014-09-05 16:31:561714bool AddSctpDataCodec(DataContentDescription* media_desc,
1715 int sctp_port) {
solenberg9fa49752016-10-08 20:02:441716 for (const auto& codec : media_desc->codecs()) {
1717 if (cricket::CodecNamesEq(codec.name, cricket::kGoogleSctpDataCodecName)) {
1718 return ParseFailed("",
1719 "Can't have multiple sctp port attributes.",
1720 NULL);
1721 }
jiayl@webrtc.orgddb85ab2014-09-05 16:31:561722 }
1723 // Add the SCTP Port number as a pseudo-codec "port" parameter
solenberg9fa49752016-10-08 20:02:441724 cricket::DataCodec codec_port(cricket::kGoogleSctpDataCodecPlType,
deadbeef67cf2c12016-04-13 17:07:161725 cricket::kGoogleSctpDataCodecName);
jiayl@webrtc.orgddb85ab2014-09-05 16:31:561726 codec_port.SetParam(cricket::kCodecParamPort, sctp_port);
Mirko Bonadei675513b2017-11-09 10:09:251727 RTC_LOG(INFO) << "AddSctpDataCodec: Got SCTP Port Number " << sctp_port;
jiayl@webrtc.orgddb85ab2014-09-05 16:31:561728 media_desc->AddCodec(codec_port);
1729 return true;
1730}
1731
henrike@webrtc.org28e20752013-07-10 00:45:361732bool GetMinValue(const std::vector<int>& values, int* value) {
1733 if (values.empty()) {
1734 return false;
1735 }
1736 std::vector<int>::const_iterator found =
1737 std::min_element(values.begin(), values.end());
1738 *value = *found;
1739 return true;
1740}
1741
1742bool GetParameter(const std::string& name,
1743 const cricket::CodecParameterMap& params, int* value) {
1744 std::map<std::string, std::string>::const_iterator found =
1745 params.find(name);
1746 if (found == params.end()) {
1747 return false;
1748 }
buildbot@webrtc.orgd4e598d2014-07-29 17:36:521749 if (!rtc::FromString(found->second, value)) {
wu@webrtc.org5e760e72014-04-02 23:19:091750 return false;
1751 }
henrike@webrtc.org28e20752013-07-10 00:45:361752 return true;
1753}
1754
1755void BuildRtpMap(const MediaContentDescription* media_desc,
1756 const MediaType media_type,
1757 std::string* message) {
nisseede5da42017-01-12 13:15:361758 RTC_DCHECK(message != NULL);
1759 RTC_DCHECK(media_desc != NULL);
henrike@webrtc.org28e20752013-07-10 00:45:361760 std::ostringstream os;
1761 if (media_type == cricket::MEDIA_TYPE_VIDEO) {
Steve Antonb1c1de12017-12-21 23:14:301762 const VideoContentDescription* video_desc = media_desc->as_video();
henrike@webrtc.org28e20752013-07-10 00:45:361763 for (std::vector<cricket::VideoCodec>::const_iterator it =
1764 video_desc->codecs().begin();
1765 it != video_desc->codecs().end(); ++it) {
1766 // RFC 4566
1767 // a=rtpmap:<payload type> <encoding name>/<clock rate>
1768 // [/<encodingparameters>]
1769 if (it->id != kWildcardPayloadType) {
1770 InitAttrLine(kAttributeRtpmap, &os);
deadbeefe814a0d2017-02-26 02:15:091771 os << kSdpDelimiterColon << it->id << " " << it->name << "/"
1772 << cricket::kVideoCodecClockrate;
henrike@webrtc.org28e20752013-07-10 00:45:361773 AddLine(os.str(), message);
1774 }
1775 AddRtcpFbLines(*it, message);
1776 AddFmtpLine(*it, message);
1777 }
1778 } else if (media_type == cricket::MEDIA_TYPE_AUDIO) {
Steve Antonb1c1de12017-12-21 23:14:301779 const AudioContentDescription* audio_desc = media_desc->as_audio();
henrike@webrtc.org28e20752013-07-10 00:45:361780 std::vector<int> ptimes;
1781 std::vector<int> maxptimes;
1782 int max_minptime = 0;
1783 for (std::vector<cricket::AudioCodec>::const_iterator it =
1784 audio_desc->codecs().begin();
1785 it != audio_desc->codecs().end(); ++it) {
nisseede5da42017-01-12 13:15:361786 RTC_DCHECK(!it->name.empty());
henrike@webrtc.org28e20752013-07-10 00:45:361787 // RFC 4566
1788 // a=rtpmap:<payload type> <encoding name>/<clock rate>
1789 // [/<encodingparameters>]
1790 InitAttrLine(kAttributeRtpmap, &os);
1791 os << kSdpDelimiterColon << it->id << " ";
1792 os << it->name << "/" << it->clockrate;
1793 if (it->channels != 1) {
1794 os << "/" << it->channels;
1795 }
1796 AddLine(os.str(), message);
1797 AddRtcpFbLines(*it, message);
1798 AddFmtpLine(*it, message);
1799 int minptime = 0;
1800 if (GetParameter(kCodecParamMinPTime, it->params, &minptime)) {
1801 max_minptime = std::max(minptime, max_minptime);
1802 }
1803 int ptime;
1804 if (GetParameter(kCodecParamPTime, it->params, &ptime)) {
1805 ptimes.push_back(ptime);
1806 }
1807 int maxptime;
1808 if (GetParameter(kCodecParamMaxPTime, it->params, &maxptime)) {
1809 maxptimes.push_back(maxptime);
1810 }
1811 }
1812 // Populate the maxptime attribute with the smallest maxptime of all codecs
1813 // under the same m-line.
1814 int min_maxptime = INT_MAX;
1815 if (GetMinValue(maxptimes, &min_maxptime)) {
1816 AddAttributeLine(kCodecParamMaxPTime, min_maxptime, message);
1817 }
nisseede5da42017-01-12 13:15:361818 RTC_DCHECK(min_maxptime > max_minptime);
henrike@webrtc.org28e20752013-07-10 00:45:361819 // Populate the ptime attribute with the smallest ptime or the largest
1820 // minptime, whichever is the largest, for all codecs under the same m-line.
1821 int ptime = INT_MAX;
1822 if (GetMinValue(ptimes, &ptime)) {
1823 ptime = std::min(ptime, min_maxptime);
1824 ptime = std::max(ptime, max_minptime);
1825 AddAttributeLine(kCodecParamPTime, ptime, message);
1826 }
1827 } else if (media_type == cricket::MEDIA_TYPE_DATA) {
Steve Antonb1c1de12017-12-21 23:14:301828 const DataContentDescription* data_desc = media_desc->as_data();
henrike@webrtc.org28e20752013-07-10 00:45:361829 for (std::vector<cricket::DataCodec>::const_iterator it =
1830 data_desc->codecs().begin();
1831 it != data_desc->codecs().end(); ++it) {
1832 // RFC 4566
1833 // a=rtpmap:<payload type> <encoding name>/<clock rate>
1834 // [/<encodingparameters>]
1835 InitAttrLine(kAttributeRtpmap, &os);
1836 os << kSdpDelimiterColon << it->id << " "
1837 << it->name << "/" << it->clockrate;
1838 AddLine(os.str(), message);
1839 }
1840 }
1841}
1842
1843void BuildCandidate(const std::vector<Candidate>& candidates,
honghaiza54a0802015-12-17 02:37:231844 bool include_ufrag,
henrike@webrtc.org28e20752013-07-10 00:45:361845 std::string* message) {
1846 std::ostringstream os;
1847
1848 for (std::vector<Candidate>::const_iterator it = candidates.begin();
1849 it != candidates.end(); ++it) {
1850 // RFC 5245
1851 // a=candidate:<foundation> <component-id> <transport> <priority>
1852 // <connection-address> <port> typ <candidate-types>
1853 // [raddr <connection-address>] [rport <port>]
1854 // *(SP extension-att-name SP extension-att-value)
1855 std::string type;
1856 // Map the cricket candidate type to "host" / "srflx" / "prflx" / "relay"
1857 if (it->type() == cricket::LOCAL_PORT_TYPE) {
1858 type = kCandidateHost;
1859 } else if (it->type() == cricket::STUN_PORT_TYPE) {
1860 type = kCandidateSrflx;
1861 } else if (it->type() == cricket::RELAY_PORT_TYPE) {
1862 type = kCandidateRelay;
Honghai Zhang7fb69db2016-03-14 18:59:181863 } else if (it->type() == cricket::PRFLX_PORT_TYPE) {
1864 type = kCandidatePrflx;
1865 // Peer reflexive candidate may be signaled for being removed.
henrike@webrtc.org28e20752013-07-10 00:45:361866 } else {
nissec80e7412017-01-11 13:56:461867 RTC_NOTREACHED();
Peter Thatcher019087f2015-04-28 16:06:261868 // Never write out candidates if we don't know the type.
1869 continue;
henrike@webrtc.org28e20752013-07-10 00:45:361870 }
1871
1872 InitAttrLine(kAttributeCandidate, &os);
1873 os << kSdpDelimiterColon
mallinath@webrtc.org2d60c5e2014-08-08 22:29:201874 << it->foundation() << " "
1875 << it->component() << " "
1876 << it->protocol() << " "
1877 << it->priority() << " "
henrike@webrtc.org28e20752013-07-10 00:45:361878 << it->address().ipaddr().ToString() << " "
1879 << it->address().PortAsString() << " "
mallinath@webrtc.org2d60c5e2014-08-08 22:29:201880 << kAttributeCandidateTyp << " "
1881 << type << " ";
henrike@webrtc.org28e20752013-07-10 00:45:361882
1883 // Related address
1884 if (!it->related_address().IsNil()) {
1885 os << kAttributeCandidateRaddr << " "
1886 << it->related_address().ipaddr().ToString() << " "
1887 << kAttributeCandidateRport << " "
1888 << it->related_address().PortAsString() << " ";
1889 }
1890
mallinath@webrtc.org2d60c5e2014-08-08 22:29:201891 if (it->protocol() == cricket::TCP_PROTOCOL_NAME) {
mallinath@webrtc.orge999bd02014-08-13 06:05:551892 os << kTcpCandidateType << " " << it->tcptype() << " ";
mallinath@webrtc.org2d60c5e2014-08-08 22:29:201893 }
1894
henrike@webrtc.org28e20752013-07-10 00:45:361895 // Extensions
1896 os << kAttributeCandidateGeneration << " " << it->generation();
honghaiza54a0802015-12-17 02:37:231897 if (include_ufrag && !it->username().empty()) {
1898 os << " " << kAttributeCandidateUfrag << " " << it->username();
1899 }
honghaiza0c44ea2016-03-23 23:07:481900 if (it->network_id() > 0) {
1901 os << " " << kAttributeCandidateNetworkId << " " << it->network_id();
1902 }
honghaize1a0c942016-02-16 22:54:561903 if (it->network_cost() > 0) {
1904 os << " " << kAttributeCandidateNetworkCost << " " << it->network_cost();
1905 }
henrike@webrtc.org28e20752013-07-10 00:45:361906
1907 AddLine(os.str(), message);
1908 }
1909}
1910
1911void BuildIceOptions(const std::vector<std::string>& transport_options,
1912 std::string* message) {
1913 if (!transport_options.empty()) {
1914 std::ostringstream os;
1915 InitAttrLine(kAttributeIceOption, &os);
1916 os << kSdpDelimiterColon << transport_options[0];
1917 for (size_t i = 1; i < transport_options.size(); ++i) {
1918 os << kSdpDelimiterSpace << transport_options[i];
1919 }
1920 AddLine(os.str(), message);
1921 }
1922}
1923
pthatcher@webrtc.org3341b402015-02-13 21:14:221924bool IsRtp(const std::string& protocol) {
1925 return protocol.empty() ||
1926 (protocol.find(cricket::kMediaProtocolRtpPrefix) != std::string::npos);
1927}
1928
1929bool IsDtlsSctp(const std::string& protocol) {
1930 // This intentionally excludes "SCTP" and "SCTP/DTLS".
lally@webrtc.orga7470932015-02-24 20:19:211931 return protocol.find(cricket::kMediaProtocolDtlsSctp) != std::string::npos;
pthatcher@webrtc.org3341b402015-02-13 21:14:221932}
1933
zhihuang38989e52017-03-21 18:04:531934bool ParseConnectionData(const std::string& line,
1935 rtc::SocketAddress* addr,
1936 SdpParseError* error) {
1937 // Parse the line from left to right.
1938 std::string token;
1939 std::string rightpart;
1940 // RFC 4566
1941 // c=<nettype> <addrtype> <connection-address>
1942 // Skip the "c="
1943 if (!rtc::tokenize_first(line, kSdpDelimiterEqual, &token, &rightpart)) {
1944 return ParseFailed(line, "Failed to parse the network type.", error);
1945 }
1946
1947 // Extract and verify the <nettype>
1948 if (!rtc::tokenize_first(rightpart, kSdpDelimiterSpace, &token, &rightpart) ||
1949 token != kConnectionNettype) {
1950 return ParseFailed(line,
1951 "Failed to parse the connection data. The network type "
1952 "is not currently supported.",
1953 error);
1954 }
1955
1956 // Extract the "<addrtype>" and "<connection-address>".
1957 if (!rtc::tokenize_first(rightpart, kSdpDelimiterSpace, &token, &rightpart)) {
1958 return ParseFailed(line, "Failed to parse the address type.", error);
1959 }
1960
1961 // The rightpart part should be the IP address without the slash which is used
1962 // for multicast.
1963 if (rightpart.find('/') != std::string::npos) {
1964 return ParseFailed(line,
1965 "Failed to parse the connection data. Multicast is not "
1966 "currently supported.",
1967 error);
1968 }
1969 addr->SetIP(rightpart);
1970
1971 // Verify that the addrtype matches the type of the parsed address.
1972 if ((addr->family() == AF_INET && token != "IP4") ||
1973 (addr->family() == AF_INET6 && token != "IP6")) {
1974 addr->Clear();
1975 return ParseFailed(
1976 line,
1977 "Failed to parse the connection data. The address type is mismatching.",
1978 error);
1979 }
1980 return true;
1981}
1982
1983bool ParseSessionDescription(const std::string& message,
1984 size_t* pos,
henrike@webrtc.org28e20752013-07-10 00:45:361985 std::string* session_id,
1986 std::string* session_version,
henrike@webrtc.org28e20752013-07-10 00:45:361987 TransportDescription* session_td,
1988 RtpHeaderExtensions* session_extmaps,
zhihuang38989e52017-03-21 18:04:531989 rtc::SocketAddress* connection_addr,
henrike@webrtc.org28e20752013-07-10 00:45:361990 cricket::SessionDescription* desc,
1991 SdpParseError* error) {
1992 std::string line;
1993
deadbeefc80741f2015-10-22 20:14:451994 desc->set_msid_supported(false);
1995
henrike@webrtc.org28e20752013-07-10 00:45:361996 // RFC 4566
1997 // v= (protocol version)
1998 if (!GetLineWithType(message, pos, &line, kLineTypeVersion)) {
1999 return ParseFailedExpectLine(message, *pos, kLineTypeVersion,
2000 std::string(), error);
2001 }
2002 // RFC 4566
2003 // o=<username> <sess-id> <sess-version> <nettype> <addrtype>
2004 // <unicast-address>
2005 if (!GetLineWithType(message, pos, &line, kLineTypeOrigin)) {
2006 return ParseFailedExpectLine(message, *pos, kLineTypeOrigin,
2007 std::string(), error);
2008 }
2009 std::vector<std::string> fields;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:522010 rtc::split(line.substr(kLinePrefixLength),
henrike@webrtc.org28e20752013-07-10 00:45:362011 kSdpDelimiterSpace, &fields);
2012 const size_t expected_fields = 6;
2013 if (fields.size() != expected_fields) {
2014 return ParseFailedExpectFieldNum(line, expected_fields, error);
2015 }
2016 *session_id = fields[1];
2017 *session_version = fields[2];
2018
2019 // RFC 4566
2020 // s= (session name)
2021 if (!GetLineWithType(message, pos, &line, kLineTypeSessionName)) {
2022 return ParseFailedExpectLine(message, *pos, kLineTypeSessionName,
2023 std::string(), error);
2024 }
2025
2026 // Optional lines
2027 // Those are the optional lines, so shouldn't return false if not present.
2028 // RFC 4566
2029 // i=* (session information)
2030 GetLineWithType(message, pos, &line, kLineTypeSessionInfo);
2031
2032 // RFC 4566
2033 // u=* (URI of description)
2034 GetLineWithType(message, pos, &line, kLineTypeSessionUri);
2035
2036 // RFC 4566
2037 // e=* (email address)
2038 GetLineWithType(message, pos, &line, kLineTypeSessionEmail);
2039
2040 // RFC 4566
2041 // p=* (phone number)
2042 GetLineWithType(message, pos, &line, kLineTypeSessionPhone);
2043
2044 // RFC 4566
2045 // c=* (connection information -- not required if included in
2046 // all media)
zhihuang38989e52017-03-21 18:04:532047 if (GetLineWithType(message, pos, &line, kLineTypeConnection)) {
2048 if (!ParseConnectionData(line, connection_addr, error)) {
2049 return false;
2050 }
2051 }
henrike@webrtc.org28e20752013-07-10 00:45:362052
2053 // RFC 4566
2054 // b=* (zero or more bandwidth information lines)
2055 while (GetLineWithType(message, pos, &line, kLineTypeSessionBandwidth)) {
2056 // By pass zero or more b lines.
2057 }
2058
2059 // RFC 4566
2060 // One or more time descriptions ("t=" and "r=" lines; see below)
2061 // t= (time the session is active)
2062 // r=* (zero or more repeat times)
2063 // Ensure there's at least one time description
2064 if (!GetLineWithType(message, pos, &line, kLineTypeTiming)) {
2065 return ParseFailedExpectLine(message, *pos, kLineTypeTiming, std::string(),
2066 error);
2067 }
2068
2069 while (GetLineWithType(message, pos, &line, kLineTypeRepeatTimes)) {
2070 // By pass zero or more r lines.
2071 }
2072
2073 // Go through the rest of the time descriptions
2074 while (GetLineWithType(message, pos, &line, kLineTypeTiming)) {
2075 while (GetLineWithType(message, pos, &line, kLineTypeRepeatTimes)) {
2076 // By pass zero or more r lines.
2077 }
2078 }
2079
2080 // RFC 4566
2081 // z=* (time zone adjustments)
2082 GetLineWithType(message, pos, &line, kLineTypeTimeZone);
2083
2084 // RFC 4566
2085 // k=* (encryption key)
2086 GetLineWithType(message, pos, &line, kLineTypeEncryptionKey);
2087
2088 // RFC 4566
2089 // a=* (zero or more session attribute lines)
2090 while (GetLineWithType(message, pos, &line, kLineTypeAttributes)) {
2091 if (HasAttribute(line, kAttributeGroup)) {
2092 if (!ParseGroupAttribute(line, desc, error)) {
2093 return false;
2094 }
2095 } else if (HasAttribute(line, kAttributeIceUfrag)) {
2096 if (!GetValue(line, kAttributeIceUfrag,
2097 &(session_td->ice_ufrag), error)) {
2098 return false;
2099 }
2100 } else if (HasAttribute(line, kAttributeIcePwd)) {
2101 if (!GetValue(line, kAttributeIcePwd, &(session_td->ice_pwd), error)) {
2102 return false;
2103 }
2104 } else if (HasAttribute(line, kAttributeIceLite)) {
2105 session_td->ice_mode = cricket::ICEMODE_LITE;
2106 } else if (HasAttribute(line, kAttributeIceOption)) {
2107 if (!ParseIceOptions(line, &(session_td->transport_options), error)) {
2108 return false;
2109 }
2110 } else if (HasAttribute(line, kAttributeFingerprint)) {
2111 if (session_td->identity_fingerprint.get()) {
2112 return ParseFailed(
2113 line,
2114 "Can't have multiple fingerprint attributes at the same level.",
2115 error);
2116 }
buildbot@webrtc.orgd4e598d2014-07-29 17:36:522117 rtc::SSLFingerprint* fingerprint = NULL;
henrike@webrtc.org28e20752013-07-10 00:45:362118 if (!ParseFingerprintAttribute(line, &fingerprint, error)) {
2119 return false;
2120 }
2121 session_td->identity_fingerprint.reset(fingerprint);
sergeyu@chromium.org0be6aa02013-08-23 23:21:252122 } else if (HasAttribute(line, kAttributeSetup)) {
2123 if (!ParseDtlsSetup(line, &(session_td->connection_role), error)) {
2124 return false;
2125 }
henrike@webrtc.org28e20752013-07-10 00:45:362126 } else if (HasAttribute(line, kAttributeMsidSemantics)) {
2127 std::string semantics;
2128 if (!GetValue(line, kAttributeMsidSemantics, &semantics, error)) {
2129 return false;
2130 }
deadbeefc80741f2015-10-22 20:14:452131 desc->set_msid_supported(
2132 CaseInsensitiveFind(semantics, kMediaStreamSemantic));
henrike@webrtc.org28e20752013-07-10 00:45:362133 } else if (HasAttribute(line, kAttributeExtmap)) {
isheriff6f8d6862016-05-26 18:24:552134 RtpExtension extmap;
henrike@webrtc.org28e20752013-07-10 00:45:362135 if (!ParseExtmap(line, &extmap, error)) {
2136 return false;
2137 }
2138 session_extmaps->push_back(extmap);
2139 }
2140 }
2141
2142 return true;
2143}
2144
2145bool ParseGroupAttribute(const std::string& line,
2146 cricket::SessionDescription* desc,
2147 SdpParseError* error) {
nisseede5da42017-01-12 13:15:362148 RTC_DCHECK(desc != NULL);
henrike@webrtc.org28e20752013-07-10 00:45:362149
2150 // RFC 5888 and draft-holmberg-mmusic-sdp-bundle-negotiation-00
2151 // a=group:BUNDLE video voice
2152 std::vector<std::string> fields;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:522153 rtc::split(line.substr(kLinePrefixLength),
henrike@webrtc.org28e20752013-07-10 00:45:362154 kSdpDelimiterSpace, &fields);
2155 std::string semantics;
2156 if (!GetValue(fields[0], kAttributeGroup, &semantics, error)) {
2157 return false;
2158 }
2159 cricket::ContentGroup group(semantics);
2160 for (size_t i = 1; i < fields.size(); ++i) {
2161 group.AddContentName(fields[i]);
2162 }
2163 desc->AddGroup(group);
2164 return true;
2165}
2166
2167static bool ParseFingerprintAttribute(const std::string& line,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:522168 rtc::SSLFingerprint** fingerprint,
henrike@webrtc.org28e20752013-07-10 00:45:362169 SdpParseError* error) {
2170 if (!IsLineType(line, kLineTypeAttributes) ||
2171 !HasAttribute(line, kAttributeFingerprint)) {
2172 return ParseFailedExpectLine(line, 0, kLineTypeAttributes,
2173 kAttributeFingerprint, error);
2174 }
2175
2176 std::vector<std::string> fields;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:522177 rtc::split(line.substr(kLinePrefixLength),
henrike@webrtc.org28e20752013-07-10 00:45:362178 kSdpDelimiterSpace, &fields);
2179 const size_t expected_fields = 2;
2180 if (fields.size() != expected_fields) {
2181 return ParseFailedExpectFieldNum(line, expected_fields, error);
2182 }
2183
2184 // The first field here is "fingerprint:<hash>.
2185 std::string algorithm;
2186 if (!GetValue(fields[0], kAttributeFingerprint, &algorithm, error)) {
2187 return false;
2188 }
2189
2190 // Downcase the algorithm. Note that we don't need to downcase the
2191 // fingerprint because hex_decode can handle upper-case.
2192 std::transform(algorithm.begin(), algorithm.end(), algorithm.begin(),
2193 ::tolower);
2194
2195 // The second field is the digest value. De-hexify it.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:522196 *fingerprint = rtc::SSLFingerprint::CreateFromRfc4572(
henrike@webrtc.org28e20752013-07-10 00:45:362197 algorithm, fields[1]);
2198 if (!*fingerprint) {
2199 return ParseFailed(line,
2200 "Failed to create fingerprint from the digest.",
2201 error);
2202 }
2203
2204 return true;
2205}
2206
sergeyu@chromium.org0be6aa02013-08-23 23:21:252207static bool ParseDtlsSetup(const std::string& line,
2208 cricket::ConnectionRole* role,
2209 SdpParseError* error) {
2210 // setup-attr = "a=setup:" role
2211 // role = "active" / "passive" / "actpass" / "holdconn"
2212 std::vector<std::string> fields;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:522213 rtc::split(line.substr(kLinePrefixLength), kSdpDelimiterColon, &fields);
sergeyu@chromium.org0be6aa02013-08-23 23:21:252214 const size_t expected_fields = 2;
2215 if (fields.size() != expected_fields) {
2216 return ParseFailedExpectFieldNum(line, expected_fields, error);
2217 }
2218 std::string role_str = fields[1];
2219 if (!cricket::StringToConnectionRole(role_str, role)) {
2220 return ParseFailed(line, "Invalid attribute value.", error);
2221 }
2222 return true;
2223}
2224
deadbeef9d3584c2016-02-17 01:54:102225static bool ParseMsidAttribute(const std::string& line,
Seth Hampson5b4f0752018-04-02 23:31:362226 std::vector<std::string>* stream_ids,
deadbeef9d3584c2016-02-17 01:54:102227 std::string* track_id,
2228 SdpParseError* error) {
Seth Hampson5b4f0752018-04-02 23:31:362229 // https://datatracker.ietf.org/doc/draft-ietf-mmusic-msid/16/
deadbeef9d3584c2016-02-17 01:54:102230 // a=msid:<stream id> <track id>
2231 // msid-value = msid-id [ SP msid-appdata ]
2232 // msid-id = 1*64token-char ; see RFC 4566
2233 // msid-appdata = 1*64token-char ; see RFC 4566
2234 std::string field1;
Seth Hampson5b4f0752018-04-02 23:31:362235 std::string new_stream_id;
2236 std::string new_track_id;
deadbeef9d3584c2016-02-17 01:54:102237 if (!rtc::tokenize_first(line.substr(kLinePrefixLength), kSdpDelimiterSpace,
Seth Hampson5b4f0752018-04-02 23:31:362238 &field1, &new_track_id)) {
deadbeef9d3584c2016-02-17 01:54:102239 const size_t expected_fields = 2;
2240 return ParseFailedExpectFieldNum(line, expected_fields, error);
2241 }
2242
Seth Hampson5b4f0752018-04-02 23:31:362243 if (new_track_id.empty()) {
deadbeefa4549d62017-02-11 01:26:222244 return ParseFailed(line, "Missing track ID in msid attribute.", error);
2245 }
Seth Hampson5b4f0752018-04-02 23:31:362246 // All track ids should be the same within an m section in a Unified Plan SDP.
2247 if (!track_id->empty() && new_track_id.compare(*track_id) != 0) {
2248 return ParseFailed(
2249 line, "Two different track IDs in msid attribute in one m= section",
2250 error);
2251 }
2252 *track_id = new_track_id;
deadbeefa4549d62017-02-11 01:26:222253
deadbeef9d3584c2016-02-17 01:54:102254 // msid:<msid-id>
Seth Hampson5b4f0752018-04-02 23:31:362255 if (!GetValue(field1, kAttributeMsid, &new_stream_id, error)) {
deadbeef9d3584c2016-02-17 01:54:102256 return false;
2257 }
Seth Hampson5b4f0752018-04-02 23:31:362258 if (new_stream_id.empty()) {
deadbeefa4549d62017-02-11 01:26:222259 return ParseFailed(line, "Missing stream ID in msid attribute.", error);
2260 }
Seth Hampson5b4f0752018-04-02 23:31:362261 // The special value "-" indicates "no MediaStream".
2262 if (new_stream_id.compare(kNoStreamMsid) != 0) {
2263 stream_ids->push_back(new_stream_id);
2264 }
deadbeef9d3584c2016-02-17 01:54:102265 return true;
2266}
2267
henrike@webrtc.org28e20752013-07-10 00:45:362268// RFC 3551
2269// PT encoding media type clock rate channels
2270// name (Hz)
2271// 0 PCMU A 8,000 1
2272// 1 reserved A
2273// 2 reserved A
2274// 3 GSM A 8,000 1
2275// 4 G723 A 8,000 1
2276// 5 DVI4 A 8,000 1
2277// 6 DVI4 A 16,000 1
2278// 7 LPC A 8,000 1
2279// 8 PCMA A 8,000 1
2280// 9 G722 A 8,000 1
2281// 10 L16 A 44,100 2
2282// 11 L16 A 44,100 1
2283// 12 QCELP A 8,000 1
2284// 13 CN A 8,000 1
2285// 14 MPA A 90,000 (see text)
2286// 15 G728 A 8,000 1
2287// 16 DVI4 A 11,025 1
2288// 17 DVI4 A 22,050 1
2289// 18 G729 A 8,000 1
2290struct StaticPayloadAudioCodec {
2291 const char* name;
2292 int clockrate;
Peter Kasting69558702016-01-13 00:26:352293 size_t channels;
henrike@webrtc.org28e20752013-07-10 00:45:362294};
2295static const StaticPayloadAudioCodec kStaticPayloadAudioCodecs[] = {
2296 { "PCMU", 8000, 1 },
2297 { "reserved", 0, 0 },
2298 { "reserved", 0, 0 },
2299 { "GSM", 8000, 1 },
2300 { "G723", 8000, 1 },
2301 { "DVI4", 8000, 1 },
2302 { "DVI4", 16000, 1 },
2303 { "LPC", 8000, 1 },
2304 { "PCMA", 8000, 1 },
2305 { "G722", 8000, 1 },
2306 { "L16", 44100, 2 },
2307 { "L16", 44100, 1 },
2308 { "QCELP", 8000, 1 },
2309 { "CN", 8000, 1 },
2310 { "MPA", 90000, 1 },
2311 { "G728", 8000, 1 },
2312 { "DVI4", 11025, 1 },
2313 { "DVI4", 22050, 1 },
2314 { "G729", 8000, 1 },
2315};
2316
2317void MaybeCreateStaticPayloadAudioCodecs(
2318 const std::vector<int>& fmts, AudioContentDescription* media_desc) {
2319 if (!media_desc) {
2320 return;
2321 }
deadbeef67cf2c12016-04-13 17:07:162322 RTC_DCHECK(media_desc->codecs().empty());
solenberg9fa49752016-10-08 20:02:442323 for (int payload_type : fmts) {
henrike@webrtc.org28e20752013-07-10 00:45:362324 if (!media_desc->HasCodec(payload_type) &&
2325 payload_type >= 0 &&
kjellander3e33bfe2016-06-20 14:04:092326 static_cast<uint32_t>(payload_type) <
2327 arraysize(kStaticPayloadAudioCodecs)) {
henrike@webrtc.org28e20752013-07-10 00:45:362328 std::string encoding_name = kStaticPayloadAudioCodecs[payload_type].name;
2329 int clock_rate = kStaticPayloadAudioCodecs[payload_type].clockrate;
Peter Kasting69558702016-01-13 00:26:352330 size_t channels = kStaticPayloadAudioCodecs[payload_type].channels;
henrike@webrtc.org28e20752013-07-10 00:45:362331 media_desc->AddCodec(cricket::AudioCodec(payload_type, encoding_name,
deadbeef67cf2c12016-04-13 17:07:162332 clock_rate, 0, channels));
henrike@webrtc.org28e20752013-07-10 00:45:362333 }
henrike@webrtc.org28e20752013-07-10 00:45:362334 }
2335}
2336
2337template <class C>
2338static C* ParseContentDescription(const std::string& message,
2339 const MediaType media_type,
2340 int mline_index,
2341 const std::string& protocol,
deadbeef67cf2c12016-04-13 17:07:162342 const std::vector<int>& payload_types,
henrike@webrtc.org28e20752013-07-10 00:45:362343 size_t* pos,
2344 std::string* content_name,
deadbeef25ed4352016-12-13 02:37:362345 bool* bundle_only,
Steve Antone831b8c2018-02-01 20:22:162346 int* msid_signaling,
henrike@webrtc.org28e20752013-07-10 00:45:362347 TransportDescription* transport,
2348 std::vector<JsepIceCandidate*>* candidates,
2349 webrtc::SdpParseError* error) {
2350 C* media_desc = new C();
2351 switch (media_type) {
2352 case cricket::MEDIA_TYPE_AUDIO:
2353 *content_name = cricket::CN_AUDIO;
2354 break;
2355 case cricket::MEDIA_TYPE_VIDEO:
2356 *content_name = cricket::CN_VIDEO;
2357 break;
2358 case cricket::MEDIA_TYPE_DATA:
2359 *content_name = cricket::CN_DATA;
2360 break;
2361 default:
nissec80e7412017-01-11 13:56:462362 RTC_NOTREACHED();
henrike@webrtc.org28e20752013-07-10 00:45:362363 break;
2364 }
deadbeef67cf2c12016-04-13 17:07:162365 if (!ParseContent(message, media_type, mline_index, protocol, payload_types,
Steve Antone831b8c2018-02-01 20:22:162366 pos, content_name, bundle_only, msid_signaling, media_desc,
2367 transport, candidates, error)) {
henrike@webrtc.org28e20752013-07-10 00:45:362368 delete media_desc;
zhihuang38989e52017-03-21 18:04:532369 return nullptr;
henrike@webrtc.org28e20752013-07-10 00:45:362370 }
2371 // Sort the codecs according to the m-line fmt list.
deadbeef67cf2c12016-04-13 17:07:162372 std::unordered_map<int, int> payload_type_preferences;
2373 // "size + 1" so that the lowest preference payload type has a preference of
2374 // 1, which is greater than the default (0) for payload types not in the fmt
2375 // list.
2376 int preference = static_cast<int>(payload_types.size() + 1);
2377 for (int pt : payload_types) {
2378 payload_type_preferences[pt] = preference--;
2379 }
2380 std::vector<typename C::CodecType> codecs = media_desc->codecs();
2381 std::sort(codecs.begin(), codecs.end(), [&payload_type_preferences](
2382 const typename C::CodecType& a,
2383 const typename C::CodecType& b) {
2384 return payload_type_preferences[a.id] > payload_type_preferences[b.id];
2385 });
2386 media_desc->set_codecs(codecs);
henrike@webrtc.org28e20752013-07-10 00:45:362387 return media_desc;
2388}
2389
2390bool ParseMediaDescription(const std::string& message,
2391 const TransportDescription& session_td,
2392 const RtpHeaderExtensions& session_extmaps,
henrike@webrtc.org28e20752013-07-10 00:45:362393 size_t* pos,
zhihuang38989e52017-03-21 18:04:532394 const rtc::SocketAddress& session_connection_addr,
henrike@webrtc.org28e20752013-07-10 00:45:362395 cricket::SessionDescription* desc,
2396 std::vector<JsepIceCandidate*>* candidates,
2397 SdpParseError* error) {
nisseede5da42017-01-12 13:15:362398 RTC_DCHECK(desc != NULL);
henrike@webrtc.org28e20752013-07-10 00:45:362399 std::string line;
2400 int mline_index = -1;
Steve Antone831b8c2018-02-01 20:22:162401 int msid_signaling = 0;
henrike@webrtc.org28e20752013-07-10 00:45:362402
2403 // Zero or more media descriptions
2404 // RFC 4566
2405 // m=<media> <port> <proto> <fmt>
2406 while (GetLineWithType(message, pos, &line, kLineTypeMedia)) {
2407 ++mline_index;
2408
2409 std::vector<std::string> fields;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:522410 rtc::split(line.substr(kLinePrefixLength),
henrike@webrtc.org28e20752013-07-10 00:45:362411 kSdpDelimiterSpace, &fields);
zstein4b2e0822017-02-18 03:48:382412
henrike@webrtc.org28e20752013-07-10 00:45:362413 const size_t expected_min_fields = 4;
2414 if (fields.size() < expected_min_fields) {
2415 return ParseFailedExpectMinFieldNum(line, expected_min_fields, error);
2416 }
deadbeef25ed4352016-12-13 02:37:362417 bool port_rejected = false;
henrike@webrtc.org28e20752013-07-10 00:45:362418 // RFC 3264
2419 // To reject an offered stream, the port number in the corresponding stream
2420 // in the answer MUST be set to zero.
2421 if (fields[1] == kMediaPortRejected) {
deadbeef25ed4352016-12-13 02:37:362422 port_rejected = true;
henrike@webrtc.org28e20752013-07-10 00:45:362423 }
2424
zhihuang38989e52017-03-21 18:04:532425 int port = 0;
2426 if (!rtc::FromString<int>(fields[1], &port) || !IsValidPort(port)) {
2427 return ParseFailed(line, "The port number is invalid", error);
2428 }
henrike@webrtc.org28e20752013-07-10 00:45:362429 std::string protocol = fields[2];
henrike@webrtc.org28e20752013-07-10 00:45:362430
2431 // <fmt>
deadbeef67cf2c12016-04-13 17:07:162432 std::vector<int> payload_types;
pthatcher@webrtc.org3341b402015-02-13 21:14:222433 if (IsRtp(protocol)) {
jiayl@webrtc.orgddb85ab2014-09-05 16:31:562434 for (size_t j = 3 ; j < fields.size(); ++j) {
2435 // TODO(wu): Remove when below bug is fixed.
2436 // https://bugzilla.mozilla.org/show_bug.cgi?id=996329
pbosbb36fdf2015-07-09 14:48:142437 if (fields[j].empty() && j == fields.size() - 1) {
jiayl@webrtc.orgddb85ab2014-09-05 16:31:562438 continue;
2439 }
wu@webrtc.org36eda7c2014-04-15 20:37:302440
jiayl@webrtc.orgddb85ab2014-09-05 16:31:562441 int pl = 0;
pkasting@chromium.orge9facf82015-02-17 20:36:282442 if (!GetPayloadTypeFromString(line, fields[j], &pl, error)) {
jiayl@webrtc.orgddb85ab2014-09-05 16:31:562443 return false;
2444 }
deadbeef67cf2c12016-04-13 17:07:162445 payload_types.push_back(pl);
wu@webrtc.org5e760e72014-04-02 23:19:092446 }
henrike@webrtc.org28e20752013-07-10 00:45:362447 }
2448
2449 // Make a temporary TransportDescription based on |session_td|.
2450 // Some of this gets overwritten by ParseContent.
deadbeef46eed762016-01-28 21:24:372451 TransportDescription transport(
2452 session_td.transport_options, session_td.ice_ufrag, session_td.ice_pwd,
2453 session_td.ice_mode, session_td.connection_role,
2454 session_td.identity_fingerprint.get());
henrike@webrtc.org28e20752013-07-10 00:45:362455
kwibergd1fe2812016-04-27 13:47:292456 std::unique_ptr<MediaContentDescription> content;
henrike@webrtc.org28e20752013-07-10 00:45:362457 std::string content_name;
deadbeef25ed4352016-12-13 02:37:362458 bool bundle_only = false;
Steve Antone831b8c2018-02-01 20:22:162459 int section_msid_signaling = 0;
henrike@webrtc.org28e20752013-07-10 00:45:362460 if (HasAttribute(line, kMediaTypeVideo)) {
2461 content.reset(ParseContentDescription<VideoContentDescription>(
deadbeef67cf2c12016-04-13 17:07:162462 message, cricket::MEDIA_TYPE_VIDEO, mline_index, protocol,
Steve Antone831b8c2018-02-01 20:22:162463 payload_types, pos, &content_name, &bundle_only,
2464 &section_msid_signaling, &transport, candidates, error));
henrike@webrtc.org28e20752013-07-10 00:45:362465 } else if (HasAttribute(line, kMediaTypeAudio)) {
2466 content.reset(ParseContentDescription<AudioContentDescription>(
deadbeef67cf2c12016-04-13 17:07:162467 message, cricket::MEDIA_TYPE_AUDIO, mline_index, protocol,
Steve Antone831b8c2018-02-01 20:22:162468 payload_types, pos, &content_name, &bundle_only,
2469 &section_msid_signaling, &transport, candidates, error));
henrike@webrtc.org28e20752013-07-10 00:45:362470 } else if (HasAttribute(line, kMediaTypeData)) {
jiayl@webrtc.org0e527722014-09-08 21:43:432471 DataContentDescription* data_desc =
wu@webrtc.org78187522013-10-07 23:32:022472 ParseContentDescription<DataContentDescription>(
deadbeef67cf2c12016-04-13 17:07:162473 message, cricket::MEDIA_TYPE_DATA, mline_index, protocol,
Steve Antone831b8c2018-02-01 20:22:162474 payload_types, pos, &content_name, &bundle_only,
2475 &section_msid_signaling, &transport, candidates, error);
jiayl@webrtc.org0e527722014-09-08 21:43:432476 content.reset(data_desc);
wu@webrtc.org78187522013-10-07 23:32:022477
zstein4b2e0822017-02-18 03:48:382478 if (data_desc && IsDtlsSctp(protocol)) {
2479 int p;
2480 if (rtc::FromString(fields[3], &p)) {
2481 if (!AddSctpDataCodec(data_desc, p)) {
2482 return false;
2483 }
2484 } else if (fields[3] == kDefaultSctpmapProtocol) {
2485 data_desc->set_use_sctpmap(false);
2486 }
wu@webrtc.org78187522013-10-07 23:32:022487 }
henrike@webrtc.org28e20752013-07-10 00:45:362488 } else {
Mirko Bonadei675513b2017-11-09 10:09:252489 RTC_LOG(LS_WARNING) << "Unsupported media type: " << line;
henrike@webrtc.org28e20752013-07-10 00:45:362490 continue;
2491 }
2492 if (!content.get()) {
2493 // ParseContentDescription returns NULL if failed.
2494 return false;
2495 }
2496
Steve Antone831b8c2018-02-01 20:22:162497 msid_signaling |= section_msid_signaling;
2498
deadbeef25ed4352016-12-13 02:37:362499 bool content_rejected = false;
deadbeef12771a12017-01-03 21:53:472500 // A port of 0 is not interpreted as a rejected m= section when it's
2501 // used along with a=bundle-only.
deadbeef25ed4352016-12-13 02:37:362502 if (bundle_only) {
deadbeef25ed4352016-12-13 02:37:362503 if (!port_rejected) {
deadbeef12771a12017-01-03 21:53:472504 // Usage of bundle-only with a nonzero port is unspecified. So just
2505 // ignore bundle-only if we see this.
2506 bundle_only = false;
Mirko Bonadei675513b2017-11-09 10:09:252507 RTC_LOG(LS_WARNING)
deadbeef12771a12017-01-03 21:53:472508 << "a=bundle-only attribute observed with a nonzero "
Jonas Olsson45cc8902018-02-13 09:37:072509 "port; this usage is unspecified so the attribute is being "
2510 "ignored.";
deadbeef25ed4352016-12-13 02:37:362511 }
2512 } else {
2513 // If not using bundle-only, interpret port 0 in the normal way; the m=
2514 // section is being rejected.
2515 content_rejected = port_rejected;
2516 }
2517
pthatcher@webrtc.org3341b402015-02-13 21:14:222518 if (IsRtp(protocol)) {
henrike@webrtc.org28e20752013-07-10 00:45:362519 // Set the extmap.
2520 if (!session_extmaps.empty() &&
2521 !content->rtp_header_extensions().empty()) {
2522 return ParseFailed("",
2523 "The a=extmap MUST be either all session level or "
2524 "all media level.",
2525 error);
2526 }
2527 for (size_t i = 0; i < session_extmaps.size(); ++i) {
2528 content->AddRtpHeaderExtension(session_extmaps[i]);
2529 }
2530 }
2531 content->set_protocol(protocol);
zhihuang38989e52017-03-21 18:04:532532
2533 // Use the session level connection address if the media level addresses are
2534 // not specified.
2535 rtc::SocketAddress address;
2536 address = content->connection_address().IsNil()
2537 ? session_connection_addr
2538 : content->connection_address();
2539 address.SetPort(port);
2540 content->set_connection_address(address);
2541
henrike@webrtc.org28e20752013-07-10 00:45:362542 desc->AddContent(content_name,
Steve Anton5adfafd2017-12-21 00:34:002543 IsDtlsSctp(protocol) ? MediaProtocolType::kSctp
2544 : MediaProtocolType::kRtp,
deadbeef25ed4352016-12-13 02:37:362545 content_rejected, bundle_only, content.release());
henrike@webrtc.org28e20752013-07-10 00:45:362546 // Create TransportInfo with the media level "ice-pwd" and "ice-ufrag".
2547 TransportInfo transport_info(content_name, transport);
2548
2549 if (!desc->AddTransportInfo(transport_info)) {
2550 std::ostringstream description;
2551 description << "Failed to AddTransportInfo with content name: "
2552 << content_name;
2553 return ParseFailed("", description.str(), error);
2554 }
2555 }
wu@webrtc.orgcecfd182013-10-30 05:18:122556
Steve Antone831b8c2018-02-01 20:22:162557 desc->set_msid_signaling(msid_signaling);
2558
wu@webrtc.orgcecfd182013-10-30 05:18:122559 size_t end_of_message = message.size();
2560 if (mline_index == -1 && *pos != end_of_message) {
2561 ParseFailed(message, *pos, "Expects m line.", error);
2562 return false;
2563 }
henrike@webrtc.org28e20752013-07-10 00:45:362564 return true;
2565}
2566
2567bool VerifyCodec(const cricket::Codec& codec) {
2568 // Codec has not been populated correctly unless the name has been set. This
2569 // can happen if an SDP has an fmtp or rtcp-fb with a payload type but doesn't
2570 // have a corresponding "rtpmap" line.
htab39db842016-12-08 09:50:482571 return !codec.name.empty();
henrike@webrtc.org28e20752013-07-10 00:45:362572}
2573
2574bool VerifyAudioCodecs(const AudioContentDescription* audio_desc) {
2575 const std::vector<cricket::AudioCodec>& codecs = audio_desc->codecs();
2576 for (std::vector<cricket::AudioCodec>::const_iterator iter = codecs.begin();
2577 iter != codecs.end(); ++iter) {
2578 if (!VerifyCodec(*iter)) {
2579 return false;
2580 }
2581 }
2582 return true;
2583}
2584
2585bool VerifyVideoCodecs(const VideoContentDescription* video_desc) {
2586 const std::vector<cricket::VideoCodec>& codecs = video_desc->codecs();
2587 for (std::vector<cricket::VideoCodec>::const_iterator iter = codecs.begin();
2588 iter != codecs.end(); ++iter) {
2589 if (!VerifyCodec(*iter)) {
2590 return false;
2591 }
2592 }
2593 return true;
2594}
2595
2596void AddParameters(const cricket::CodecParameterMap& parameters,
2597 cricket::Codec* codec) {
2598 for (cricket::CodecParameterMap::const_iterator iter =
2599 parameters.begin(); iter != parameters.end(); ++iter) {
2600 codec->SetParam(iter->first, iter->second);
2601 }
2602}
2603
2604void AddFeedbackParameter(const cricket::FeedbackParam& feedback_param,
2605 cricket::Codec* codec) {
2606 codec->AddFeedbackParam(feedback_param);
2607}
2608
2609void AddFeedbackParameters(const cricket::FeedbackParams& feedback_params,
2610 cricket::Codec* codec) {
2611 for (std::vector<cricket::FeedbackParam>::const_iterator iter =
2612 feedback_params.params().begin();
2613 iter != feedback_params.params().end(); ++iter) {
2614 codec->AddFeedbackParam(*iter);
2615 }
2616}
2617
2618// Gets the current codec setting associated with |payload_type|. If there
changbin.shao@webrtc.org2d25b442015-03-16 04:14:342619// is no Codec associated with that payload type it returns an empty codec
henrike@webrtc.org28e20752013-07-10 00:45:362620// with that payload type.
2621template <class T>
changbin.shao@webrtc.org2d25b442015-03-16 04:14:342622T GetCodecWithPayloadType(const std::vector<T>& codecs, int payload_type) {
magjedb05fa242016-11-11 12:00:162623 const T* codec = FindCodecById(codecs, payload_type);
2624 if (codec)
2625 return *codec;
2626 // Return empty codec with |payload_type|.
changbin.shao@webrtc.org2d25b442015-03-16 04:14:342627 T ret_val;
magjedb05fa242016-11-11 12:00:162628 ret_val.id = payload_type;
henrike@webrtc.org28e20752013-07-10 00:45:362629 return ret_val;
2630}
2631
2632// Updates or creates a new codec entry in the audio description.
2633template <class T, class U>
2634void AddOrReplaceCodec(MediaContentDescription* content_desc, const U& codec) {
2635 T* desc = static_cast<T*>(content_desc);
2636 std::vector<U> codecs = desc->codecs();
2637 bool found = false;
2638
2639 typename std::vector<U>::iterator iter;
2640 for (iter = codecs.begin(); iter != codecs.end(); ++iter) {
2641 if (iter->id == codec.id) {
2642 *iter = codec;
2643 found = true;
2644 break;
2645 }
2646 }
2647 if (!found) {
2648 desc->AddCodec(codec);
2649 return;
2650 }
2651 desc->set_codecs(codecs);
2652}
2653
2654// Adds or updates existing codec corresponding to |payload_type| according
2655// to |parameters|.
2656template <class T, class U>
2657void UpdateCodec(MediaContentDescription* content_desc, int payload_type,
2658 const cricket::CodecParameterMap& parameters) {
2659 // Codec might already have been populated (from rtpmap).
changbin.shao@webrtc.org2d25b442015-03-16 04:14:342660 U new_codec = GetCodecWithPayloadType(static_cast<T*>(content_desc)->codecs(),
2661 payload_type);
henrike@webrtc.org28e20752013-07-10 00:45:362662 AddParameters(parameters, &new_codec);
2663 AddOrReplaceCodec<T, U>(content_desc, new_codec);
2664}
2665
2666// Adds or updates existing codec corresponding to |payload_type| according
2667// to |feedback_param|.
2668template <class T, class U>
2669void UpdateCodec(MediaContentDescription* content_desc, int payload_type,
2670 const cricket::FeedbackParam& feedback_param) {
2671 // Codec might already have been populated (from rtpmap).
changbin.shao@webrtc.org2d25b442015-03-16 04:14:342672 U new_codec = GetCodecWithPayloadType(static_cast<T*>(content_desc)->codecs(),
2673 payload_type);
henrike@webrtc.org28e20752013-07-10 00:45:362674 AddFeedbackParameter(feedback_param, &new_codec);
2675 AddOrReplaceCodec<T, U>(content_desc, new_codec);
2676}
2677
jlmiller@webrtc.orga744a282015-02-18 21:37:462678template <class T>
2679bool PopWildcardCodec(std::vector<T>* codecs, T* wildcard_codec) {
2680 for (auto iter = codecs->begin(); iter != codecs->end(); ++iter) {
henrike@webrtc.org28e20752013-07-10 00:45:362681 if (iter->id == kWildcardPayloadType) {
2682 *wildcard_codec = *iter;
2683 codecs->erase(iter);
2684 return true;
2685 }
2686 }
2687 return false;
2688}
2689
jlmiller@webrtc.orga744a282015-02-18 21:37:462690template<class T>
2691void UpdateFromWildcardCodecs(cricket::MediaContentDescriptionImpl<T>* desc) {
2692 auto codecs = desc->codecs();
2693 T wildcard_codec;
henrike@webrtc.org28e20752013-07-10 00:45:362694 if (!PopWildcardCodec(&codecs, &wildcard_codec)) {
2695 return;
2696 }
jlmiller@webrtc.orga744a282015-02-18 21:37:462697 for (auto& codec : codecs) {
henrike@webrtc.org28e20752013-07-10 00:45:362698 AddFeedbackParameters(wildcard_codec.feedback_params, &codec);
2699 }
jlmiller@webrtc.orga744a282015-02-18 21:37:462700 desc->set_codecs(codecs);
henrike@webrtc.org28e20752013-07-10 00:45:362701}
2702
2703void AddAudioAttribute(const std::string& name, const std::string& value,
2704 AudioContentDescription* audio_desc) {
2705 if (value.empty()) {
2706 return;
2707 }
2708 std::vector<cricket::AudioCodec> codecs = audio_desc->codecs();
2709 for (std::vector<cricket::AudioCodec>::iterator iter = codecs.begin();
2710 iter != codecs.end(); ++iter) {
2711 iter->params[name] = value;
2712 }
2713 audio_desc->set_codecs(codecs);
2714}
2715
2716bool ParseContent(const std::string& message,
2717 const MediaType media_type,
2718 int mline_index,
2719 const std::string& protocol,
deadbeef67cf2c12016-04-13 17:07:162720 const std::vector<int>& payload_types,
henrike@webrtc.org28e20752013-07-10 00:45:362721 size_t* pos,
2722 std::string* content_name,
deadbeef25ed4352016-12-13 02:37:362723 bool* bundle_only,
Steve Antone831b8c2018-02-01 20:22:162724 int* msid_signaling,
henrike@webrtc.org28e20752013-07-10 00:45:362725 MediaContentDescription* media_desc,
2726 TransportDescription* transport,
2727 std::vector<JsepIceCandidate*>* candidates,
2728 SdpParseError* error) {
nisseede5da42017-01-12 13:15:362729 RTC_DCHECK(media_desc != NULL);
2730 RTC_DCHECK(content_name != NULL);
2731 RTC_DCHECK(transport != NULL);
henrike@webrtc.org28e20752013-07-10 00:45:362732
henrike@webrtc.org704bf9e2014-02-27 17:52:042733 if (media_type == cricket::MEDIA_TYPE_AUDIO) {
Steve Antonb1c1de12017-12-21 23:14:302734 MaybeCreateStaticPayloadAudioCodecs(payload_types, media_desc->as_audio());
henrike@webrtc.org704bf9e2014-02-27 17:52:042735 }
2736
henrike@webrtc.org28e20752013-07-10 00:45:362737 // The media level "ice-ufrag" and "ice-pwd".
2738 // The candidates before update the media level "ice-pwd" and "ice-ufrag".
2739 Candidates candidates_orig;
2740 std::string line;
2741 std::string mline_id;
2742 // Tracks created out of the ssrc attributes.
2743 StreamParamsVec tracks;
2744 SsrcInfoVec ssrc_infos;
2745 SsrcGroupVec ssrc_groups;
2746 std::string maxptime_as_string;
2747 std::string ptime_as_string;
Seth Hampson5b4f0752018-04-02 23:31:362748 std::vector<std::string> stream_ids;
deadbeef9d3584c2016-02-17 01:54:102749 std::string track_id;
henrike@webrtc.org28e20752013-07-10 00:45:362750
henrike@webrtc.org28e20752013-07-10 00:45:362751 // Loop until the next m line
2752 while (!IsLineType(message, kLineTypeMedia, *pos)) {
2753 if (!GetLine(message, pos, &line)) {
2754 if (*pos >= message.size()) {
2755 break; // Done parsing
2756 } else {
sergeyu@chromium.org5bc25c42013-12-05 00:24:062757 return ParseFailed(message, *pos, "Invalid SDP line.", error);
henrike@webrtc.org28e20752013-07-10 00:45:362758 }
2759 }
2760
henrike@webrtc.org28e20752013-07-10 00:45:362761 // RFC 4566
2762 // b=* (zero or more bandwidth information lines)
2763 if (IsLineType(line, kLineTypeSessionBandwidth)) {
2764 std::string bandwidth;
2765 if (HasAttribute(line, kApplicationSpecificMaximum)) {
2766 if (!GetValue(line, kApplicationSpecificMaximum, &bandwidth, error)) {
2767 return false;
2768 } else {
wu@webrtc.org5e760e72014-04-02 23:19:092769 int b = 0;
2770 if (!GetValueFromString(line, bandwidth, &b, error)) {
2771 return false;
2772 }
deadbeef3e8016e2017-08-04 00:49:302773 // TODO(deadbeef): Historically, applications may be setting a value
2774 // of -1 to mean "unset any previously set bandwidth limit", even
2775 // though ommitting the "b=AS" entirely will do just that. Once we've
2776 // transitioned applications to doing the right thing, it would be
2777 // better to treat this as a hard error instead of just ignoring it.
2778 if (b == -1) {
Mirko Bonadei675513b2017-11-09 10:09:252779 RTC_LOG(LS_WARNING)
2780 << "Ignoring \"b=AS:-1\"; will be treated as \"no "
2781 "bandwidth limit\".";
deadbeef3e8016e2017-08-04 00:49:302782 continue;
2783 }
deadbeefbc88c6b2017-08-02 18:26:342784 if (b < 0) {
2785 return ParseFailed(line, "b=AS value can't be negative.", error);
2786 }
Peter Thatcherc0c3a862015-06-24 22:31:252787 // We should never use more than the default bandwidth for RTP-based
2788 // data channels. Don't allow SDP to set the bandwidth, because
2789 // that would give JS the opportunity to "break the Internet".
2790 // See: https://code.google.com/p/chromium/issues/detail?id=280726
2791 if (media_type == cricket::MEDIA_TYPE_DATA && IsRtp(protocol) &&
2792 b > cricket::kDataMaxBandwidth / 1000) {
2793 std::ostringstream description;
2794 description << "RTP-based data channels may not send more than "
2795 << cricket::kDataMaxBandwidth / 1000 << "kbps.";
2796 return ParseFailed(line, description.str(), error);
2797 }
deadbeefb2362572016-12-14 00:37:062798 // Prevent integer overflow.
2799 b = std::min(b, INT_MAX / 1000);
wu@webrtc.org5e760e72014-04-02 23:19:092800 media_desc->set_bandwidth(b * 1000);
henrike@webrtc.org28e20752013-07-10 00:45:362801 }
2802 }
2803 continue;
2804 }
2805
zhihuang38989e52017-03-21 18:04:532806 // Parse the media level connection data.
2807 if (IsLineType(line, kLineTypeConnection)) {
2808 rtc::SocketAddress addr;
2809 if (!ParseConnectionData(line, &addr, error)) {
2810 return false;
2811 }
2812 media_desc->set_connection_address(addr);
2813 continue;
2814 }
2815
henrike@webrtc.org28e20752013-07-10 00:45:362816 if (!IsLineType(line, kLineTypeAttributes)) {
Steve Anton36b29d12017-10-30 16:57:422817 // TODO(deadbeef): Handle other lines if needed.
Mirko Bonadei675513b2017-11-09 10:09:252818 RTC_LOG(LS_INFO) << "Ignored line: " << line;
henrike@webrtc.org28e20752013-07-10 00:45:362819 continue;
2820 }
2821
2822 // Handle attributes common to SCTP and RTP.
2823 if (HasAttribute(line, kAttributeMid)) {
2824 // RFC 3388
2825 // mid-attribute = "a=mid:" identification-tag
2826 // identification-tag = token
2827 // Use the mid identification-tag as the content name.
2828 if (!GetValue(line, kAttributeMid, &mline_id, error)) {
2829 return false;
2830 }
2831 *content_name = mline_id;
deadbeef25ed4352016-12-13 02:37:362832 } else if (HasAttribute(line, kAttributeBundleOnly)) {
2833 *bundle_only = true;
henrike@webrtc.org28e20752013-07-10 00:45:362834 } else if (HasAttribute(line, kAttributeCandidate)) {
2835 Candidate candidate;
2836 if (!ParseCandidate(line, &candidate, error, false)) {
2837 return false;
2838 }
deadbeef7bcdb692017-01-20 20:43:582839 // ParseCandidate will parse non-standard ufrag and password attributes,
2840 // since it's used for candidate trickling, but we only want to process
2841 // the "a=ice-ufrag"/"a=ice-pwd" values in a session description, so
2842 // strip them off at this point.
2843 candidate.set_username(std::string());
2844 candidate.set_password(std::string());
henrike@webrtc.org28e20752013-07-10 00:45:362845 candidates_orig.push_back(candidate);
2846 } else if (HasAttribute(line, kAttributeIceUfrag)) {
2847 if (!GetValue(line, kAttributeIceUfrag, &transport->ice_ufrag, error)) {
2848 return false;
2849 }
2850 } else if (HasAttribute(line, kAttributeIcePwd)) {
2851 if (!GetValue(line, kAttributeIcePwd, &transport->ice_pwd, error)) {
2852 return false;
2853 }
2854 } else if (HasAttribute(line, kAttributeIceOption)) {
2855 if (!ParseIceOptions(line, &transport->transport_options, error)) {
2856 return false;
2857 }
2858 } else if (HasAttribute(line, kAttributeFmtp)) {
2859 if (!ParseFmtpAttributes(line, media_type, media_desc, error)) {
2860 return false;
2861 }
2862 } else if (HasAttribute(line, kAttributeFingerprint)) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:522863 rtc::SSLFingerprint* fingerprint = NULL;
henrike@webrtc.org28e20752013-07-10 00:45:362864
2865 if (!ParseFingerprintAttribute(line, &fingerprint, error)) {
2866 return false;
2867 }
2868 transport->identity_fingerprint.reset(fingerprint);
sergeyu@chromium.org0be6aa02013-08-23 23:21:252869 } else if (HasAttribute(line, kAttributeSetup)) {
2870 if (!ParseDtlsSetup(line, &(transport->connection_role), error)) {
2871 return false;
2872 }
pthatcher@webrtc.org3341b402015-02-13 21:14:222873 } else if (IsDtlsSctp(protocol) && HasAttribute(line, kAttributeSctpPort)) {
deadbeef7e146cb2016-09-28 17:04:342874 if (media_type != cricket::MEDIA_TYPE_DATA) {
2875 return ParseFailed(
2876 line, "sctp-port attribute found in non-data media description.",
2877 error);
2878 }
jiayl@webrtc.orgddb85ab2014-09-05 16:31:562879 int sctp_port;
2880 if (!ParseSctpPort(line, &sctp_port, error)) {
2881 return false;
2882 }
Steve Antonb1c1de12017-12-21 23:14:302883 if (!AddSctpDataCodec(media_desc->as_data(), sctp_port)) {
jiayl@webrtc.orgddb85ab2014-09-05 16:31:562884 return false;
2885 }
pthatcher@webrtc.org3341b402015-02-13 21:14:222886 } else if (IsRtp(protocol)) {
henrike@webrtc.org28e20752013-07-10 00:45:362887 //
2888 // RTP specific attrubtes
2889 //
2890 if (HasAttribute(line, kAttributeRtcpMux)) {
2891 media_desc->set_rtcp_mux(true);
deadbeef13871492015-12-09 20:37:512892 } else if (HasAttribute(line, kAttributeRtcpReducedSize)) {
2893 media_desc->set_rtcp_reduced_size(true);
henrike@webrtc.org28e20752013-07-10 00:45:362894 } else if (HasAttribute(line, kAttributeSsrcGroup)) {
2895 if (!ParseSsrcGroupAttribute(line, &ssrc_groups, error)) {
2896 return false;
2897 }
2898 } else if (HasAttribute(line, kAttributeSsrc)) {
Steve Antone831b8c2018-02-01 20:22:162899 if (!ParseSsrcAttribute(line, &ssrc_infos, msid_signaling, error)) {
henrike@webrtc.org28e20752013-07-10 00:45:362900 return false;
2901 }
2902 } else if (HasAttribute(line, kAttributeCrypto)) {
2903 if (!ParseCryptoAttribute(line, media_desc, error)) {
2904 return false;
2905 }
2906 } else if (HasAttribute(line, kAttributeRtpmap)) {
deadbeef67cf2c12016-04-13 17:07:162907 if (!ParseRtpmapAttribute(line, media_type, payload_types, media_desc,
2908 error)) {
henrike@webrtc.org28e20752013-07-10 00:45:362909 return false;
2910 }
2911 } else if (HasAttribute(line, kCodecParamMaxPTime)) {
2912 if (!GetValue(line, kCodecParamMaxPTime, &maxptime_as_string, error)) {
2913 return false;
2914 }
2915 } else if (HasAttribute(line, kAttributeRtcpFb)) {
2916 if (!ParseRtcpFbAttribute(line, media_type, media_desc, error)) {
2917 return false;
2918 }
2919 } else if (HasAttribute(line, kCodecParamPTime)) {
2920 if (!GetValue(line, kCodecParamPTime, &ptime_as_string, error)) {
2921 return false;
2922 }
2923 } else if (HasAttribute(line, kAttributeSendOnly)) {
Steve Anton4e70a722017-11-28 22:57:102924 media_desc->set_direction(RtpTransceiverDirection::kSendOnly);
henrike@webrtc.org28e20752013-07-10 00:45:362925 } else if (HasAttribute(line, kAttributeRecvOnly)) {
Steve Anton4e70a722017-11-28 22:57:102926 media_desc->set_direction(RtpTransceiverDirection::kRecvOnly);
henrike@webrtc.org28e20752013-07-10 00:45:362927 } else if (HasAttribute(line, kAttributeInactive)) {
Steve Anton4e70a722017-11-28 22:57:102928 media_desc->set_direction(RtpTransceiverDirection::kInactive);
henrike@webrtc.org28e20752013-07-10 00:45:362929 } else if (HasAttribute(line, kAttributeSendRecv)) {
Steve Anton4e70a722017-11-28 22:57:102930 media_desc->set_direction(RtpTransceiverDirection::kSendRecv);
henrike@webrtc.org28e20752013-07-10 00:45:362931 } else if (HasAttribute(line, kAttributeExtmap)) {
isheriff6f8d6862016-05-26 18:24:552932 RtpExtension extmap;
henrike@webrtc.org28e20752013-07-10 00:45:362933 if (!ParseExtmap(line, &extmap, error)) {
2934 return false;
2935 }
2936 media_desc->AddRtpHeaderExtension(extmap);
2937 } else if (HasAttribute(line, kAttributeXGoogleFlag)) {
2938 // Experimental attribute. Conference mode activates more aggressive
2939 // AEC and NS settings.
Steve Anton36b29d12017-10-30 16:57:422940 // TODO(deadbeef): expose API to set these directly.
henrike@webrtc.org28e20752013-07-10 00:45:362941 std::string flag_value;
2942 if (!GetValue(line, kAttributeXGoogleFlag, &flag_value, error)) {
2943 return false;
2944 }
2945 if (flag_value.compare(kValueConference) == 0)
2946 media_desc->set_conference_mode(true);
deadbeef9d3584c2016-02-17 01:54:102947 } else if (HasAttribute(line, kAttributeMsid)) {
Seth Hampson5b4f0752018-04-02 23:31:362948 if (!ParseMsidAttribute(line, &stream_ids, &track_id, error)) {
deadbeef9d3584c2016-02-17 01:54:102949 return false;
2950 }
Steve Antone831b8c2018-02-01 20:22:162951 *msid_signaling |= cricket::kMsidSignalingMediaSection;
henrike@webrtc.org28e20752013-07-10 00:45:362952 }
2953 } else {
2954 // Only parse lines that we are interested of.
Mirko Bonadei675513b2017-11-09 10:09:252955 RTC_LOG(LS_INFO) << "Ignored line: " << line;
henrike@webrtc.org28e20752013-07-10 00:45:362956 continue;
2957 }
2958 }
2959
2960 // Create tracks from the |ssrc_infos|.
Taylor Brandstetter5de6b752016-03-10 01:02:302961 // If the stream_id/track_id for all SSRCS are identical, one StreamParams
2962 // will be created in CreateTracksFromSsrcInfos, containing all the SSRCs from
2963 // the m= section.
Seth Hampson5897a6e2018-04-03 18:16:332964 if (!ssrc_infos.empty()) {
2965 CreateTracksFromSsrcInfos(ssrc_infos, stream_ids, track_id, &tracks,
2966 *msid_signaling);
2967 } else if (media_type != cricket::MEDIA_TYPE_DATA &&
2968 (*msid_signaling & cricket::kMsidSignalingMediaSection)) {
2969 // If the stream_ids/track_id was signaled but SSRCs were unsignaled we
2970 // still create a track. This isn't done for data media types because
2971 // StreamParams aren't used for SCTP streams, and RTP data channels don't
2972 // support unsignaled SSRCs.
2973 CreateTrackWithNoSsrcs(stream_ids, track_id, &tracks);
2974 }
henrike@webrtc.org28e20752013-07-10 00:45:362975
2976 // Add the ssrc group to the track.
2977 for (SsrcGroupVec::iterator ssrc_group = ssrc_groups.begin();
2978 ssrc_group != ssrc_groups.end(); ++ssrc_group) {
2979 if (ssrc_group->ssrcs.empty()) {
2980 continue;
2981 }
Peter Boström0c4e06b2015-10-07 10:23:212982 uint32_t ssrc = ssrc_group->ssrcs.front();
henrike@webrtc.org28e20752013-07-10 00:45:362983 for (StreamParamsVec::iterator track = tracks.begin();
2984 track != tracks.end(); ++track) {
2985 if (track->has_ssrc(ssrc)) {
2986 track->ssrc_groups.push_back(*ssrc_group);
2987 }
2988 }
2989 }
2990
2991 // Add the new tracks to the |media_desc|.
deadbeef9d3584c2016-02-17 01:54:102992 for (StreamParams& track : tracks) {
2993 media_desc->AddStream(track);
henrike@webrtc.org28e20752013-07-10 00:45:362994 }
2995
2996 if (media_type == cricket::MEDIA_TYPE_AUDIO) {
Steve Antonb1c1de12017-12-21 23:14:302997 AudioContentDescription* audio_desc = media_desc->as_audio();
jlmiller@webrtc.orga744a282015-02-18 21:37:462998 UpdateFromWildcardCodecs(audio_desc);
2999
henrike@webrtc.org28e20752013-07-10 00:45:363000 // Verify audio codec ensures that no audio codec has been populated with
3001 // only fmtp.
3002 if (!VerifyAudioCodecs(audio_desc)) {
3003 return ParseFailed("Failed to parse audio codecs correctly.", error);
3004 }
3005 AddAudioAttribute(kCodecParamMaxPTime, maxptime_as_string, audio_desc);
3006 AddAudioAttribute(kCodecParamPTime, ptime_as_string, audio_desc);
3007 }
3008
3009 if (media_type == cricket::MEDIA_TYPE_VIDEO) {
Steve Antonb1c1de12017-12-21 23:14:303010 VideoContentDescription* video_desc = media_desc->as_video();
jlmiller@webrtc.orga744a282015-02-18 21:37:463011 UpdateFromWildcardCodecs(video_desc);
3012 // Verify video codec ensures that no video codec has been populated with
3013 // only rtcp-fb.
3014 if (!VerifyVideoCodecs(video_desc)) {
3015 return ParseFailed("Failed to parse video codecs correctly.", error);
3016 }
henrike@webrtc.org28e20752013-07-10 00:45:363017 }
3018
3019 // RFC 5245
3020 // Update the candidates with the media level "ice-pwd" and "ice-ufrag".
3021 for (Candidates::iterator it = candidates_orig.begin();
3022 it != candidates_orig.end(); ++it) {
nisseede5da42017-01-12 13:15:363023 RTC_DCHECK((*it).username().empty() ||
3024 (*it).username() == transport->ice_ufrag);
henrike@webrtc.org28e20752013-07-10 00:45:363025 (*it).set_username(transport->ice_ufrag);
nisseede5da42017-01-12 13:15:363026 RTC_DCHECK((*it).password().empty());
henrike@webrtc.org28e20752013-07-10 00:45:363027 (*it).set_password(transport->ice_pwd);
3028 candidates->push_back(
3029 new JsepIceCandidate(mline_id, mline_index, *it));
3030 }
3031 return true;
3032}
3033
Steve Antone831b8c2018-02-01 20:22:163034bool ParseSsrcAttribute(const std::string& line,
3035 SsrcInfoVec* ssrc_infos,
3036 int* msid_signaling,
henrike@webrtc.org28e20752013-07-10 00:45:363037 SdpParseError* error) {
nisseede5da42017-01-12 13:15:363038 RTC_DCHECK(ssrc_infos != NULL);
henrike@webrtc.org28e20752013-07-10 00:45:363039 // RFC 5576
3040 // a=ssrc:<ssrc-id> <attribute>
3041 // a=ssrc:<ssrc-id> <attribute>:<value>
3042 std::string field1, field2;
Donald Curtis144d0182015-05-15 20:14:243043 if (!rtc::tokenize_first(line.substr(kLinePrefixLength), kSdpDelimiterSpace,
3044 &field1, &field2)) {
henrike@webrtc.org28e20752013-07-10 00:45:363045 const size_t expected_fields = 2;
3046 return ParseFailedExpectFieldNum(line, expected_fields, error);
3047 }
3048
3049 // ssrc:<ssrc-id>
3050 std::string ssrc_id_s;
3051 if (!GetValue(field1, kAttributeSsrc, &ssrc_id_s, error)) {
3052 return false;
3053 }
Peter Boström0c4e06b2015-10-07 10:23:213054 uint32_t ssrc_id = 0;
wu@webrtc.org5e760e72014-04-02 23:19:093055 if (!GetValueFromString(line, ssrc_id_s, &ssrc_id, error)) {
3056 return false;
3057 }
henrike@webrtc.org28e20752013-07-10 00:45:363058
3059 std::string attribute;
3060 std::string value;
Donald Curtis144d0182015-05-15 20:14:243061 if (!rtc::tokenize_first(field2, kSdpDelimiterColon, &attribute, &value)) {
henrike@webrtc.org28e20752013-07-10 00:45:363062 std::ostringstream description;
3063 description << "Failed to get the ssrc attribute value from " << field2
3064 << ". Expected format <attribute>:<value>.";
3065 return ParseFailed(line, description.str(), error);
3066 }
3067
3068 // Check if there's already an item for this |ssrc_id|. Create a new one if
3069 // there isn't.
3070 SsrcInfoVec::iterator ssrc_info = ssrc_infos->begin();
3071 for (; ssrc_info != ssrc_infos->end(); ++ssrc_info) {
3072 if (ssrc_info->ssrc_id == ssrc_id) {
3073 break;
3074 }
3075 }
3076 if (ssrc_info == ssrc_infos->end()) {
3077 SsrcInfo info;
3078 info.ssrc_id = ssrc_id;
3079 ssrc_infos->push_back(info);
3080 ssrc_info = ssrc_infos->end() - 1;
3081 }
3082
3083 // Store the info to the |ssrc_info|.
3084 if (attribute == kSsrcAttributeCname) {
3085 // RFC 5576
3086 // cname:<value>
3087 ssrc_info->cname = value;
3088 } else if (attribute == kSsrcAttributeMsid) {
3089 // draft-alvestrand-mmusic-msid-00
Seth Hampson5b4f0752018-04-02 23:31:363090 // msid:identifier [appdata]
henrike@webrtc.org28e20752013-07-10 00:45:363091 std::vector<std::string> fields;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:523092 rtc::split(value, kSdpDelimiterSpace, &fields);
henrike@webrtc.org28e20752013-07-10 00:45:363093 if (fields.size() < 1 || fields.size() > 2) {
3094 return ParseFailed(line,
3095 "Expected format \"msid:<identifier>[ <appdata>]\".",
3096 error);
3097 }
deadbeef9d3584c2016-02-17 01:54:103098 ssrc_info->stream_id = fields[0];
henrike@webrtc.org28e20752013-07-10 00:45:363099 if (fields.size() == 2) {
deadbeef9d3584c2016-02-17 01:54:103100 ssrc_info->track_id = fields[1];
henrike@webrtc.org28e20752013-07-10 00:45:363101 }
Steve Antone831b8c2018-02-01 20:22:163102 *msid_signaling |= cricket::kMsidSignalingSsrcAttribute;
henrike@webrtc.org28e20752013-07-10 00:45:363103 } else if (attribute == kSsrcAttributeMslabel) {
3104 // draft-alvestrand-rtcweb-mid-01
3105 // mslabel:<value>
3106 ssrc_info->mslabel = value;
3107 } else if (attribute == kSSrcAttributeLabel) {
3108 // The label isn't defined.
3109 // label:<value>
3110 ssrc_info->label = value;
3111 }
3112 return true;
3113}
3114
3115bool ParseSsrcGroupAttribute(const std::string& line,
3116 SsrcGroupVec* ssrc_groups,
3117 SdpParseError* error) {
nisseede5da42017-01-12 13:15:363118 RTC_DCHECK(ssrc_groups != NULL);
henrike@webrtc.org28e20752013-07-10 00:45:363119 // RFC 5576
3120 // a=ssrc-group:<semantics> <ssrc-id> ...
3121 std::vector<std::string> fields;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:523122 rtc::split(line.substr(kLinePrefixLength),
henrike@webrtc.org28e20752013-07-10 00:45:363123 kSdpDelimiterSpace, &fields);
3124 const size_t expected_min_fields = 2;
3125 if (fields.size() < expected_min_fields) {
3126 return ParseFailedExpectMinFieldNum(line, expected_min_fields, error);
3127 }
3128 std::string semantics;
3129 if (!GetValue(fields[0], kAttributeSsrcGroup, &semantics, error)) {
3130 return false;
3131 }
Peter Boström0c4e06b2015-10-07 10:23:213132 std::vector<uint32_t> ssrcs;
henrike@webrtc.org28e20752013-07-10 00:45:363133 for (size_t i = 1; i < fields.size(); ++i) {
Peter Boström0c4e06b2015-10-07 10:23:213134 uint32_t ssrc = 0;
wu@webrtc.org5e760e72014-04-02 23:19:093135 if (!GetValueFromString(line, fields[i], &ssrc, error)) {
3136 return false;
3137 }
henrike@webrtc.org28e20752013-07-10 00:45:363138 ssrcs.push_back(ssrc);
3139 }
3140 ssrc_groups->push_back(SsrcGroup(semantics, ssrcs));
3141 return true;
3142}
3143
3144bool ParseCryptoAttribute(const std::string& line,
3145 MediaContentDescription* media_desc,
3146 SdpParseError* error) {
3147 std::vector<std::string> fields;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:523148 rtc::split(line.substr(kLinePrefixLength),
henrike@webrtc.org28e20752013-07-10 00:45:363149 kSdpDelimiterSpace, &fields);
3150 // RFC 4568
3151 // a=crypto:<tag> <crypto-suite> <key-params> [<session-params>]
3152 const size_t expected_min_fields = 3;
3153 if (fields.size() < expected_min_fields) {
3154 return ParseFailedExpectMinFieldNum(line, expected_min_fields, error);
3155 }
3156 std::string tag_value;
3157 if (!GetValue(fields[0], kAttributeCrypto, &tag_value, error)) {
3158 return false;
3159 }
wu@webrtc.org5e760e72014-04-02 23:19:093160 int tag = 0;
3161 if (!GetValueFromString(line, tag_value, &tag, error)) {
3162 return false;
3163 }
jbauch083b73f2015-07-16 09:46:323164 const std::string& crypto_suite = fields[1];
3165 const std::string& key_params = fields[2];
henrike@webrtc.org28e20752013-07-10 00:45:363166 std::string session_params;
3167 if (fields.size() > 3) {
3168 session_params = fields[3];
3169 }
3170 media_desc->AddCrypto(CryptoParams(tag, crypto_suite, key_params,
3171 session_params));
3172 return true;
3173}
3174
3175// Updates or creates a new codec entry in the audio description with according
deadbeef67cf2c12016-04-13 17:07:163176// to |name|, |clockrate|, |bitrate|, and |channels|.
3177void UpdateCodec(int payload_type,
3178 const std::string& name,
3179 int clockrate,
3180 int bitrate,
3181 size_t channels,
henrike@webrtc.org28e20752013-07-10 00:45:363182 AudioContentDescription* audio_desc) {
3183 // Codec may already be populated with (only) optional parameters
3184 // (from an fmtp).
changbin.shao@webrtc.org2d25b442015-03-16 04:14:343185 cricket::AudioCodec codec =
3186 GetCodecWithPayloadType(audio_desc->codecs(), payload_type);
henrike@webrtc.org28e20752013-07-10 00:45:363187 codec.name = name;
3188 codec.clockrate = clockrate;
3189 codec.bitrate = bitrate;
3190 codec.channels = channels;
henrike@webrtc.org28e20752013-07-10 00:45:363191 AddOrReplaceCodec<AudioContentDescription, cricket::AudioCodec>(audio_desc,
3192 codec);
3193}
3194
3195// Updates or creates a new codec entry in the video description according to
deadbeef67cf2c12016-04-13 17:07:163196// |name|, |width|, |height|, and |framerate|.
3197void UpdateCodec(int payload_type,
3198 const std::string& name,
henrike@webrtc.org28e20752013-07-10 00:45:363199 VideoContentDescription* video_desc) {
3200 // Codec may already be populated with (only) optional parameters
3201 // (from an fmtp).
changbin.shao@webrtc.org2d25b442015-03-16 04:14:343202 cricket::VideoCodec codec =
3203 GetCodecWithPayloadType(video_desc->codecs(), payload_type);
henrike@webrtc.org28e20752013-07-10 00:45:363204 codec.name = name;
henrike@webrtc.org28e20752013-07-10 00:45:363205 AddOrReplaceCodec<VideoContentDescription, cricket::VideoCodec>(video_desc,
3206 codec);
3207}
3208
3209bool ParseRtpmapAttribute(const std::string& line,
3210 const MediaType media_type,
deadbeef67cf2c12016-04-13 17:07:163211 const std::vector<int>& payload_types,
henrike@webrtc.org28e20752013-07-10 00:45:363212 MediaContentDescription* media_desc,
3213 SdpParseError* error) {
3214 std::vector<std::string> fields;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:523215 rtc::split(line.substr(kLinePrefixLength),
henrike@webrtc.org28e20752013-07-10 00:45:363216 kSdpDelimiterSpace, &fields);
3217 // RFC 4566
3218 // a=rtpmap:<payload type> <encoding name>/<clock rate>[/<encodingparameters>]
3219 const size_t expected_min_fields = 2;
3220 if (fields.size() < expected_min_fields) {
3221 return ParseFailedExpectMinFieldNum(line, expected_min_fields, error);
3222 }
3223 std::string payload_type_value;
3224 if (!GetValue(fields[0], kAttributeRtpmap, &payload_type_value, error)) {
3225 return false;
3226 }
wu@webrtc.org5e760e72014-04-02 23:19:093227 int payload_type = 0;
pkasting@chromium.orge9facf82015-02-17 20:36:283228 if (!GetPayloadTypeFromString(line, payload_type_value, &payload_type,
3229 error)) {
wu@webrtc.org5e760e72014-04-02 23:19:093230 return false;
3231 }
henrike@webrtc.org28e20752013-07-10 00:45:363232
deadbeef67cf2c12016-04-13 17:07:163233 if (std::find(payload_types.begin(), payload_types.end(), payload_type) ==
3234 payload_types.end()) {
Mirko Bonadei675513b2017-11-09 10:09:253235 RTC_LOG(LS_WARNING) << "Ignore rtpmap line that did not appear in the "
Jonas Olsson45cc8902018-02-13 09:37:073236 "<fmt> of the m-line: "
3237 << line;
henrike@webrtc.org28e20752013-07-10 00:45:363238 return true;
3239 }
jbauch083b73f2015-07-16 09:46:323240 const std::string& encoder = fields[1];
henrike@webrtc.org28e20752013-07-10 00:45:363241 std::vector<std::string> codec_params;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:523242 rtc::split(encoder, '/', &codec_params);
henrike@webrtc.org28e20752013-07-10 00:45:363243 // <encoding name>/<clock rate>[/<encodingparameters>]
3244 // 2 mandatory fields
3245 if (codec_params.size() < 2 || codec_params.size() > 3) {
3246 return ParseFailed(line,
3247 "Expected format \"<encoding name>/<clock rate>"
3248 "[/<encodingparameters>]\".",
3249 error);
3250 }
jbauch083b73f2015-07-16 09:46:323251 const std::string& encoding_name = codec_params[0];
wu@webrtc.org5e760e72014-04-02 23:19:093252 int clock_rate = 0;
3253 if (!GetValueFromString(line, codec_params[1], &clock_rate, error)) {
3254 return false;
3255 }
henrike@webrtc.org28e20752013-07-10 00:45:363256 if (media_type == cricket::MEDIA_TYPE_VIDEO) {
Steve Antonb1c1de12017-12-21 23:14:303257 VideoContentDescription* video_desc = media_desc->as_video();
henrike@webrtc.org28e20752013-07-10 00:45:363258 UpdateCodec(payload_type, encoding_name,
deadbeef67cf2c12016-04-13 17:07:163259 video_desc);
henrike@webrtc.org28e20752013-07-10 00:45:363260 } else if (media_type == cricket::MEDIA_TYPE_AUDIO) {
3261 // RFC 4566
3262 // For audio streams, <encoding parameters> indicates the number
3263 // of audio channels. This parameter is OPTIONAL and may be
3264 // omitted if the number of channels is one, provided that no
3265 // additional parameters are needed.
Peter Kasting69558702016-01-13 00:26:353266 size_t channels = 1;
henrike@webrtc.org28e20752013-07-10 00:45:363267 if (codec_params.size() == 3) {
wu@webrtc.org5e760e72014-04-02 23:19:093268 if (!GetValueFromString(line, codec_params[2], &channels, error)) {
3269 return false;
3270 }
henrike@webrtc.org28e20752013-07-10 00:45:363271 }
Steve Antonb1c1de12017-12-21 23:14:303272 AudioContentDescription* audio_desc = media_desc->as_audio();
ossue1405ad2017-01-23 16:55:483273 UpdateCodec(payload_type, encoding_name, clock_rate, 0, channels,
deadbeef67cf2c12016-04-13 17:07:163274 audio_desc);
henrike@webrtc.org28e20752013-07-10 00:45:363275 } else if (media_type == cricket::MEDIA_TYPE_DATA) {
Steve Antonb1c1de12017-12-21 23:14:303276 DataContentDescription* data_desc = media_desc->as_data();
deadbeef67cf2c12016-04-13 17:07:163277 data_desc->AddCodec(cricket::DataCodec(payload_type, encoding_name));
henrike@webrtc.org28e20752013-07-10 00:45:363278 }
3279 return true;
3280}
3281
henrike@webrtc.org28e20752013-07-10 00:45:363282bool ParseFmtpParam(const std::string& line, std::string* parameter,
3283 std::string* value, SdpParseError* error) {
Donald Curtis0e07f922015-05-15 16:21:233284 if (!rtc::tokenize_first(line, kSdpDelimiterEqual, parameter, value)) {
henrike@webrtc.org28e20752013-07-10 00:45:363285 ParseFailed(line, "Unable to parse fmtp parameter. \'=\' missing.", error);
3286 return false;
3287 }
3288 // a=fmtp:<payload_type> <param1>=<value1>; <param2>=<value2>; ...
henrike@webrtc.org28e20752013-07-10 00:45:363289 return true;
3290}
3291
3292bool ParseFmtpAttributes(const std::string& line, const MediaType media_type,
3293 MediaContentDescription* media_desc,
3294 SdpParseError* error) {
3295 if (media_type != cricket::MEDIA_TYPE_AUDIO &&
3296 media_type != cricket::MEDIA_TYPE_VIDEO) {
3297 return true;
3298 }
Donald Curtis0e07f922015-05-15 16:21:233299
3300 std::string line_payload;
3301 std::string line_params;
henrike@webrtc.org28e20752013-07-10 00:45:363302
3303 // RFC 5576
3304 // a=fmtp:<format> <format specific parameters>
3305 // At least two fields, whereas the second one is any of the optional
3306 // parameters.
Donald Curtis144d0182015-05-15 20:14:243307 if (!rtc::tokenize_first(line.substr(kLinePrefixLength), kSdpDelimiterSpace,
3308 &line_payload, &line_params)) {
henrike@webrtc.org28e20752013-07-10 00:45:363309 ParseFailedExpectMinFieldNum(line, 2, error);
3310 return false;
3311 }
3312
Donald Curtis0e07f922015-05-15 16:21:233313 // Parse out the payload information.
pkasting@chromium.orgd3245462015-02-23 21:28:223314 std::string payload_type_str;
Donald Curtis0e07f922015-05-15 16:21:233315 if (!GetValue(line_payload, kAttributeFmtp, &payload_type_str, error)) {
henrike@webrtc.org28e20752013-07-10 00:45:363316 return false;
3317 }
3318
Donald Curtis0e07f922015-05-15 16:21:233319 int payload_type = 0;
Donald Curtis144d0182015-05-15 20:14:243320 if (!GetPayloadTypeFromString(line_payload, payload_type_str, &payload_type,
3321 error)) {
Donald Curtis0e07f922015-05-15 16:21:233322 return false;
3323 }
3324
3325 // Parse out format specific parameters.
3326 std::vector<std::string> fields;
3327 rtc::split(line_params, kSdpDelimiterSemicolon, &fields);
3328
henrike@webrtc.org28e20752013-07-10 00:45:363329 cricket::CodecParameterMap codec_params;
Donald Curtis144d0182015-05-15 20:14:243330 for (auto& iter : fields) {
Donald Curtis0e07f922015-05-15 16:21:233331 if (iter.find(kSdpDelimiterEqual) == std::string::npos) {
henrike@webrtc.org28e20752013-07-10 00:45:363332 // Only fmtps with equals are currently supported. Other fmtp types
3333 // should be ignored. Unknown fmtps do not constitute an error.
3334 continue;
3335 }
Donald Curtis0e07f922015-05-15 16:21:233336
3337 std::string name;
3338 std::string value;
3339 if (!ParseFmtpParam(rtc::string_trim(iter), &name, &value, error)) {
henrike@webrtc.org28e20752013-07-10 00:45:363340 return false;
3341 }
3342 codec_params[name] = value;
3343 }
3344
henrike@webrtc.org28e20752013-07-10 00:45:363345 if (media_type == cricket::MEDIA_TYPE_AUDIO) {
3346 UpdateCodec<AudioContentDescription, cricket::AudioCodec>(
pkasting@chromium.orgd3245462015-02-23 21:28:223347 media_desc, payload_type, codec_params);
henrike@webrtc.org28e20752013-07-10 00:45:363348 } else if (media_type == cricket::MEDIA_TYPE_VIDEO) {
3349 UpdateCodec<VideoContentDescription, cricket::VideoCodec>(
pkasting@chromium.orgd3245462015-02-23 21:28:223350 media_desc, payload_type, codec_params);
henrike@webrtc.org28e20752013-07-10 00:45:363351 }
3352 return true;
3353}
3354
3355bool ParseRtcpFbAttribute(const std::string& line, const MediaType media_type,
3356 MediaContentDescription* media_desc,
3357 SdpParseError* error) {
3358 if (media_type != cricket::MEDIA_TYPE_AUDIO &&
3359 media_type != cricket::MEDIA_TYPE_VIDEO) {
3360 return true;
3361 }
3362 std::vector<std::string> rtcp_fb_fields;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:523363 rtc::split(line.c_str(), kSdpDelimiterSpace, &rtcp_fb_fields);
henrike@webrtc.org28e20752013-07-10 00:45:363364 if (rtcp_fb_fields.size() < 2) {
3365 return ParseFailedGetValue(line, kAttributeRtcpFb, error);
3366 }
3367 std::string payload_type_string;
3368 if (!GetValue(rtcp_fb_fields[0], kAttributeRtcpFb, &payload_type_string,
3369 error)) {
3370 return false;
3371 }
wu@webrtc.org5e760e72014-04-02 23:19:093372 int payload_type = kWildcardPayloadType;
3373 if (payload_type_string != "*") {
pkasting@chromium.orge9facf82015-02-17 20:36:283374 if (!GetPayloadTypeFromString(line, payload_type_string, &payload_type,
3375 error)) {
wu@webrtc.org5e760e72014-04-02 23:19:093376 return false;
3377 }
3378 }
henrike@webrtc.org28e20752013-07-10 00:45:363379 std::string id = rtcp_fb_fields[1];
3380 std::string param = "";
3381 for (std::vector<std::string>::iterator iter = rtcp_fb_fields.begin() + 2;
3382 iter != rtcp_fb_fields.end(); ++iter) {
3383 param.append(*iter);
3384 }
3385 const cricket::FeedbackParam feedback_param(id, param);
3386
3387 if (media_type == cricket::MEDIA_TYPE_AUDIO) {
pkasting@chromium.orgd3245462015-02-23 21:28:223388 UpdateCodec<AudioContentDescription, cricket::AudioCodec>(
3389 media_desc, payload_type, feedback_param);
henrike@webrtc.org28e20752013-07-10 00:45:363390 } else if (media_type == cricket::MEDIA_TYPE_VIDEO) {
pkasting@chromium.orgd3245462015-02-23 21:28:223391 UpdateCodec<VideoContentDescription, cricket::VideoCodec>(
3392 media_desc, payload_type, feedback_param);
henrike@webrtc.org28e20752013-07-10 00:45:363393 }
3394 return true;
3395}
3396
3397} // namespace webrtc