blob: 633e228983a2626d35cba61d740563382ae28002 [file] [log] [blame]
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001/*
kjellanderb24317b2016-02-10 07:54:43 -08002 * Copyright 2011 The WebRTC project authors. All Rights Reserved.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003 *
kjellanderb24317b2016-02-10 07:54:43 -08004 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00009 */
10
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#include "pc/webrtcsdp.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000012
kjellandera96e2d72016-02-04 23:52:28 -080013#include <ctype.h>
henrike@webrtc.org28e20752013-07-10 00:45:36 +000014#include <limits.h>
15#include <stdio.h>
kwibergd1fe2812016-04-27 06:47:29 -070016
henrike@webrtc.org28e20752013-07-10 00:45:36 +000017#include <algorithm>
Steve Anton36b29d12017-10-30 09:57:42 -070018#include <map>
kwibergd1fe2812016-04-27 06:47:29 -070019#include <memory>
Steve Anton36b29d12017-10-30 09:57:42 -070020#include <set>
henrike@webrtc.org28e20752013-07-10 00:45:36 +000021#include <string>
deadbeef67cf2c12016-04-13 10:07:16 -070022#include <unordered_map>
henrike@webrtc.org28e20752013-07-10 00:45:36 +000023#include <vector>
24
Patrik Höglunde2d6a062017-10-05 14:53:33 +020025#include "api/candidate.h"
Patrik Höglund7aee3d52017-11-15 13:15:17 +010026#include "api/cryptoparams.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020027#include "api/jsepicecandidate.h"
28#include "api/jsepsessiondescription.h"
isheriff6f8d6862016-05-26 11:24:55 -070029// for RtpExtension
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020030#include "api/rtpparameters.h"
31#include "media/base/codec.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020032#include "media/base/mediaconstants.h"
33#include "media/base/rtputils.h"
34#include "media/sctp/sctptransportinternal.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020035#include "p2p/base/p2pconstants.h"
36#include "p2p/base/port.h"
37#include "pc/mediasession.h"
38#include "rtc_base/arraysize.h"
39#include "rtc_base/checks.h"
40#include "rtc_base/logging.h"
41#include "rtc_base/messagedigest.h"
42#include "rtc_base/stringutils.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000043
44using cricket::AudioContentDescription;
45using cricket::Candidate;
46using cricket::Candidates;
47using cricket::ContentDescription;
48using cricket::ContentInfo;
49using cricket::CryptoParams;
50using cricket::DataContentDescription;
51using cricket::ICE_CANDIDATE_COMPONENT_RTP;
52using cricket::ICE_CANDIDATE_COMPONENT_RTCP;
53using cricket::kCodecParamMaxBitrate;
54using cricket::kCodecParamMaxPTime;
55using cricket::kCodecParamMaxQuantization;
56using cricket::kCodecParamMinBitrate;
57using cricket::kCodecParamMinPTime;
58using cricket::kCodecParamPTime;
59using cricket::kCodecParamSPropStereo;
buildbot@webrtc.orged97bb02014-05-07 11:15:20 +000060using cricket::kCodecParamStartBitrate;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000061using cricket::kCodecParamStereo;
62using cricket::kCodecParamUseInbandFec;
Minyue Li7100dcd2015-03-27 05:05:59 +010063using cricket::kCodecParamUseDtx;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000064using cricket::kCodecParamSctpProtocol;
65using cricket::kCodecParamSctpStreams;
henrike@webrtc.org1e09a712013-07-26 19:17:59 +000066using cricket::kCodecParamMaxAverageBitrate;
buildbot@webrtc.org5d639b32014-09-10 07:57:12 +000067using cricket::kCodecParamMaxPlaybackRate;
stefan@webrtc.org85d27942014-06-09 12:51:39 +000068using cricket::kCodecParamAssociatedPayloadType;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000069using cricket::MediaContentDescription;
70using cricket::MediaType;
isheriff6f8d6862016-05-26 11:24:55 -070071using cricket::RtpHeaderExtensions;
Steve Anton5adfafd2017-12-20 16:34:00 -080072using cricket::MediaProtocolType;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000073using cricket::SsrcGroup;
74using cricket::StreamParams;
75using cricket::StreamParamsVec;
76using cricket::TransportDescription;
77using cricket::TransportInfo;
78using cricket::VideoContentDescription;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000079using rtc::SocketAddress;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000080
henrike@webrtc.org28e20752013-07-10 00:45:36 +000081namespace cricket {
82class SessionDescription;
83}
84
deadbeef90f1e1e2017-02-10 12:35:05 -080085// TODO(deadbeef): Switch to using anonymous namespace rather than declaring
86// everything "static".
henrike@webrtc.org28e20752013-07-10 00:45:36 +000087namespace webrtc {
88
89// Line type
90// RFC 4566
91// An SDP session description consists of a number of lines of text of
92// the form:
93// <type>=<value>
94// where <type> MUST be exactly one case-significant character.
deadbeef9d3584c2016-02-16 17:54:10 -080095static const int kLinePrefixLength = 2; // Length of <type>=
henrike@webrtc.org28e20752013-07-10 00:45:36 +000096static const char kLineTypeVersion = 'v';
97static const char kLineTypeOrigin = 'o';
98static const char kLineTypeSessionName = 's';
99static const char kLineTypeSessionInfo = 'i';
100static const char kLineTypeSessionUri = 'u';
101static const char kLineTypeSessionEmail = 'e';
102static const char kLineTypeSessionPhone = 'p';
103static const char kLineTypeSessionBandwidth = 'b';
104static const char kLineTypeTiming = 't';
105static const char kLineTypeRepeatTimes = 'r';
106static const char kLineTypeTimeZone = 'z';
107static const char kLineTypeEncryptionKey = 'k';
108static const char kLineTypeMedia = 'm';
109static const char kLineTypeConnection = 'c';
110static const char kLineTypeAttributes = 'a';
111
112// Attributes
113static const char kAttributeGroup[] = "group";
114static const char kAttributeMid[] = "mid";
deadbeef9d3584c2016-02-16 17:54:10 -0800115static const char kAttributeMsid[] = "msid";
deadbeef25ed4352016-12-12 18:37:36 -0800116static const char kAttributeBundleOnly[] = "bundle-only";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000117static const char kAttributeRtcpMux[] = "rtcp-mux";
deadbeef13871492015-12-09 12:37:51 -0800118static const char kAttributeRtcpReducedSize[] = "rtcp-rsize";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000119static const char kAttributeSsrc[] = "ssrc";
120static const char kSsrcAttributeCname[] = "cname";
121static const char kAttributeExtmap[] = "extmap";
122// draft-alvestrand-mmusic-msid-01
123// a=msid-semantic: WMS
124static const char kAttributeMsidSemantics[] = "msid-semantic";
125static const char kMediaStreamSemantic[] = "WMS";
126static const char kSsrcAttributeMsid[] = "msid";
127static const char kDefaultMsid[] = "default";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000128static const char kSsrcAttributeMslabel[] = "mslabel";
129static const char kSSrcAttributeLabel[] = "label";
130static const char kAttributeSsrcGroup[] = "ssrc-group";
131static const char kAttributeCrypto[] = "crypto";
132static const char kAttributeCandidate[] = "candidate";
133static const char kAttributeCandidateTyp[] = "typ";
134static const char kAttributeCandidateRaddr[] = "raddr";
135static const char kAttributeCandidateRport[] = "rport";
honghaiza54a0802015-12-16 18:37:23 -0800136static const char kAttributeCandidateUfrag[] = "ufrag";
137static const char kAttributeCandidatePwd[] = "pwd";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000138static const char kAttributeCandidateGeneration[] = "generation";
honghaiza0c44ea2016-03-23 16:07:48 -0700139static const char kAttributeCandidateNetworkId[] = "network-id";
honghaize1a0c942016-02-16 14:54:56 -0800140static const char kAttributeCandidateNetworkCost[] = "network-cost";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000141static const char kAttributeFingerprint[] = "fingerprint";
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +0000142static const char kAttributeSetup[] = "setup";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000143static const char kAttributeFmtp[] = "fmtp";
144static const char kAttributeRtpmap[] = "rtpmap";
wu@webrtc.org78187522013-10-07 23:32:02 +0000145static const char kAttributeSctpmap[] = "sctpmap";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000146static const char kAttributeRtcp[] = "rtcp";
147static const char kAttributeIceUfrag[] = "ice-ufrag";
148static const char kAttributeIcePwd[] = "ice-pwd";
149static const char kAttributeIceLite[] = "ice-lite";
150static const char kAttributeIceOption[] = "ice-options";
151static const char kAttributeSendOnly[] = "sendonly";
152static const char kAttributeRecvOnly[] = "recvonly";
153static const char kAttributeRtcpFb[] = "rtcp-fb";
154static const char kAttributeSendRecv[] = "sendrecv";
155static const char kAttributeInactive[] = "inactive";
jiayl@webrtc.orgddb85ab2014-09-05 16:31:56 +0000156// draft-ietf-mmusic-sctp-sdp-07
157// a=sctp-port
158static const char kAttributeSctpPort[] = "sctp-port";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000159
160// Experimental flags
161static const char kAttributeXGoogleFlag[] = "x-google-flag";
162static const char kValueConference[] = "conference";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000163
164// Candidate
165static const char kCandidateHost[] = "host";
166static const char kCandidateSrflx[] = "srflx";
Honghai Zhang7fb69db2016-03-14 11:59:18 -0700167static const char kCandidatePrflx[] = "prflx";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000168static const char kCandidateRelay[] = "relay";
mallinath@webrtc.org2d60c5e2014-08-08 22:29:20 +0000169static const char kTcpCandidateType[] = "tcptype";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000170
171static const char kSdpDelimiterEqual = '=';
172static const char kSdpDelimiterSpace = ' ';
173static const char kSdpDelimiterColon = ':';
174static const char kSdpDelimiterSemicolon = ';';
175static const char kSdpDelimiterSlash = '/';
176static const char kNewLine = '\n';
177static const char kReturn = '\r';
178static const char kLineBreak[] = "\r\n";
179
Steve Anton36b29d12017-10-30 09:57:42 -0700180// TODO(deadbeef): Generate the Session and Time description
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000181// instead of hardcoding.
182static const char kSessionVersion[] = "v=0";
183// RFC 4566
184static const char kSessionOriginUsername[] = "-";
185static const char kSessionOriginSessionId[] = "0";
186static const char kSessionOriginSessionVersion[] = "0";
187static const char kSessionOriginNettype[] = "IN";
188static const char kSessionOriginAddrtype[] = "IP4";
189static const char kSessionOriginAddress[] = "127.0.0.1";
190static const char kSessionName[] = "s=-";
191static const char kTimeDescription[] = "t=0 0";
192static const char kAttrGroup[] = "a=group:BUNDLE";
193static const char kConnectionNettype[] = "IN";
pthatcher@webrtc.orgc9d6d142014-10-23 23:37:22 +0000194static const char kConnectionIpv4Addrtype[] = "IP4";
195static const char kConnectionIpv6Addrtype[] = "IP6";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000196static const char kMediaTypeVideo[] = "video";
197static const char kMediaTypeAudio[] = "audio";
198static const char kMediaTypeData[] = "application";
199static const char kMediaPortRejected[] = "0";
pthatcher@webrtc.orgc9d6d142014-10-23 23:37:22 +0000200// draft-ietf-mmusic-trickle-ice-01
201// When no candidates have been gathered, set the connection
202// address to IP6 ::.
perkj@webrtc.orgd105cc82014-11-07 11:22:06 +0000203// TODO(perkj): FF can not parse IP6 ::. See http://crbug/430333
204// Use IPV4 per default.
205static const char kDummyAddress[] = "0.0.0.0";
pthatcher@webrtc.orgc9d6d142014-10-23 23:37:22 +0000206static const char kDummyPort[] = "9";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000207// RFC 3556
208static const char kApplicationSpecificMaximum[] = "AS";
209
wu@webrtc.org78187522013-10-07 23:32:02 +0000210static const char kDefaultSctpmapProtocol[] = "webrtc-datachannel";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000211
pkasting@chromium.orgd3245462015-02-23 21:28:22 +0000212// RTP payload type is in the 0-127 range. Use -1 to indicate "all" payload
213// types.
214const int kWildcardPayloadType = -1;
215
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000216struct SsrcInfo {
Peter Boström0c4e06b2015-10-07 12:23:21 +0200217 uint32_t ssrc_id;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000218 std::string cname;
deadbeef9d3584c2016-02-16 17:54:10 -0800219 std::string stream_id;
220 std::string track_id;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000221
222 // For backward compatibility.
223 // TODO(ronghuawu): Remove below 2 fields once all the clients support msid.
224 std::string label;
225 std::string mslabel;
226};
227typedef std::vector<SsrcInfo> SsrcInfoVec;
228typedef std::vector<SsrcGroup> SsrcGroupVec;
229
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000230template <class T>
231static void AddFmtpLine(const T& codec, std::string* message);
232static void BuildMediaDescription(const ContentInfo* content_info,
233 const TransportInfo* transport_info,
234 const MediaType media_type,
wu@webrtc.org4c3e9912014-07-16 21:03:13 +0000235 const std::vector<Candidate>& candidates,
deadbeef9d3584c2016-02-16 17:54:10 -0800236 bool unified_plan_sdp,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000237 std::string* message);
zstein4b2e0822017-02-17 19:48:38 -0800238static void BuildSctpContentAttributes(std::string* message,
239 int sctp_port,
240 bool use_sctpmap);
deadbeef9d3584c2016-02-16 17:54:10 -0800241static void BuildRtpContentAttributes(const MediaContentDescription* media_desc,
242 const MediaType media_type,
243 bool unified_plan_sdp,
244 std::string* message);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000245static void BuildRtpMap(const MediaContentDescription* media_desc,
246 const MediaType media_type,
247 std::string* message);
248static void BuildCandidate(const std::vector<Candidate>& candidates,
honghaiza54a0802015-12-16 18:37:23 -0800249 bool include_ufrag,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000250 std::string* message);
251static void BuildIceOptions(const std::vector<std::string>& transport_options,
252 std::string* message);
pthatcher@webrtc.org3341b402015-02-13 21:14:22 +0000253static bool IsRtp(const std::string& protocol);
254static bool IsDtlsSctp(const std::string& protocol);
zhihuang38989e52017-03-21 11:04:53 -0700255static bool ParseSessionDescription(const std::string& message,
256 size_t* pos,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000257 std::string* session_id,
258 std::string* session_version,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000259 TransportDescription* session_td,
260 RtpHeaderExtensions* session_extmaps,
zhihuang38989e52017-03-21 11:04:53 -0700261 rtc::SocketAddress* connection_addr,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000262 cricket::SessionDescription* desc,
263 SdpParseError* error);
264static bool ParseGroupAttribute(const std::string& line,
265 cricket::SessionDescription* desc,
266 SdpParseError* error);
267static bool ParseMediaDescription(
268 const std::string& message,
269 const TransportDescription& session_td,
270 const RtpHeaderExtensions& session_extmaps,
zhihuang38989e52017-03-21 11:04:53 -0700271 size_t* pos,
272 const rtc::SocketAddress& session_connection_addr,
273 cricket::SessionDescription* desc,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000274 std::vector<JsepIceCandidate*>* candidates,
275 SdpParseError* error);
276static bool ParseContent(const std::string& message,
277 const MediaType media_type,
278 int mline_index,
279 const std::string& protocol,
deadbeef67cf2c12016-04-13 10:07:16 -0700280 const std::vector<int>& payload_types,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000281 size_t* pos,
282 std::string* content_name,
deadbeef25ed4352016-12-12 18:37:36 -0800283 bool* bundle_only,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000284 MediaContentDescription* media_desc,
285 TransportDescription* transport,
286 std::vector<JsepIceCandidate*>* candidates,
287 SdpParseError* error);
288static bool ParseSsrcAttribute(const std::string& line,
289 SsrcInfoVec* ssrc_infos,
290 SdpParseError* error);
291static bool ParseSsrcGroupAttribute(const std::string& line,
292 SsrcGroupVec* ssrc_groups,
293 SdpParseError* error);
294static bool ParseCryptoAttribute(const std::string& line,
295 MediaContentDescription* media_desc,
296 SdpParseError* error);
297static bool ParseRtpmapAttribute(const std::string& line,
298 const MediaType media_type,
deadbeef67cf2c12016-04-13 10:07:16 -0700299 const std::vector<int>& payload_types,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000300 MediaContentDescription* media_desc,
301 SdpParseError* error);
302static bool ParseFmtpAttributes(const std::string& line,
303 const MediaType media_type,
304 MediaContentDescription* media_desc,
305 SdpParseError* error);
306static bool ParseFmtpParam(const std::string& line, std::string* parameter,
307 std::string* value, SdpParseError* error);
308static bool ParseCandidate(const std::string& message, Candidate* candidate,
309 SdpParseError* error, bool is_raw);
310static bool ParseRtcpFbAttribute(const std::string& line,
311 const MediaType media_type,
312 MediaContentDescription* media_desc,
313 SdpParseError* error);
314static bool ParseIceOptions(const std::string& line,
315 std::vector<std::string>* transport_options,
316 SdpParseError* error);
317static bool ParseExtmap(const std::string& line,
isheriff6f8d6862016-05-26 11:24:55 -0700318 RtpExtension* extmap,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000319 SdpParseError* error);
320static bool ParseFingerprintAttribute(const std::string& line,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000321 rtc::SSLFingerprint** fingerprint,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000322 SdpParseError* error);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +0000323static bool ParseDtlsSetup(const std::string& line,
324 cricket::ConnectionRole* role,
325 SdpParseError* error);
deadbeef9d3584c2016-02-16 17:54:10 -0800326static bool ParseMsidAttribute(const std::string& line,
327 std::string* stream_id,
328 std::string* track_id,
329 SdpParseError* error);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000330
331// Helper functions
332
333// Below ParseFailed*** functions output the line that caused the parsing
334// failure and the detailed reason (|description|) of the failure to |error|.
335// The functions always return false so that they can be used directly in the
336// following way when error happens:
337// "return ParseFailed***(...);"
338
339// The line starting at |line_start| of |message| is the failing line.
340// The reason for the failure should be provided in the |description|.
341// An example of a description could be "unknown character".
342static bool ParseFailed(const std::string& message,
343 size_t line_start,
344 const std::string& description,
345 SdpParseError* error) {
346 // Get the first line of |message| from |line_start|.
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +0000347 std::string first_line;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000348 size_t line_end = message.find(kNewLine, line_start);
349 if (line_end != std::string::npos) {
350 if (line_end > 0 && (message.at(line_end - 1) == kReturn)) {
351 --line_end;
352 }
353 first_line = message.substr(line_start, (line_end - line_start));
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +0000354 } else {
355 first_line = message.substr(line_start);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000356 }
357
358 if (error) {
359 error->line = first_line;
360 error->description = description;
361 }
Mirko Bonadei675513b2017-11-09 11:09:25 +0100362 RTC_LOG(LS_ERROR) << "Failed to parse: \"" << first_line
363 << "\". Reason: " << description;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000364 return false;
365}
366
367// |line| is the failing line. The reason for the failure should be
368// provided in the |description|.
369static bool ParseFailed(const std::string& line,
370 const std::string& description,
371 SdpParseError* error) {
372 return ParseFailed(line, 0, description, error);
373}
374
375// Parses failure where the failing SDP line isn't know or there are multiple
376// failing lines.
377static bool ParseFailed(const std::string& description,
378 SdpParseError* error) {
379 return ParseFailed("", description, error);
380}
381
382// |line| is the failing line. The failure is due to the fact that |line|
383// doesn't have |expected_fields| fields.
384static bool ParseFailedExpectFieldNum(const std::string& line,
385 int expected_fields,
386 SdpParseError* error) {
387 std::ostringstream description;
388 description << "Expects " << expected_fields << " fields.";
389 return ParseFailed(line, description.str(), error);
390}
391
392// |line| is the failing line. The failure is due to the fact that |line| has
393// less than |expected_min_fields| fields.
394static bool ParseFailedExpectMinFieldNum(const std::string& line,
395 int expected_min_fields,
396 SdpParseError* error) {
397 std::ostringstream description;
398 description << "Expects at least " << expected_min_fields << " fields.";
399 return ParseFailed(line, description.str(), error);
400}
401
402// |line| is the failing line. The failure is due to the fact that it failed to
403// get the value of |attribute|.
404static bool ParseFailedGetValue(const std::string& line,
405 const std::string& attribute,
406 SdpParseError* error) {
407 std::ostringstream description;
408 description << "Failed to get the value of attribute: " << attribute;
409 return ParseFailed(line, description.str(), error);
410}
411
412// The line starting at |line_start| of |message| is the failing line. The
413// failure is due to the line type (e.g. the "m" part of the "m-line")
414// not matching what is expected. The expected line type should be
415// provided as |line_type|.
416static bool ParseFailedExpectLine(const std::string& message,
417 size_t line_start,
418 const char line_type,
419 const std::string& line_value,
420 SdpParseError* error) {
421 std::ostringstream description;
422 description << "Expect line: " << line_type << "=" << line_value;
423 return ParseFailed(message, line_start, description.str(), error);
424}
425
426static bool AddLine(const std::string& line, std::string* message) {
427 if (!message)
428 return false;
429
430 message->append(line);
431 message->append(kLineBreak);
432 return true;
433}
434
435static bool GetLine(const std::string& message,
436 size_t* pos,
437 std::string* line) {
438 size_t line_begin = *pos;
439 size_t line_end = message.find(kNewLine, line_begin);
440 if (line_end == std::string::npos) {
441 return false;
442 }
443 // Update the new start position
444 *pos = line_end + 1;
445 if (line_end > 0 && (message.at(line_end - 1) == kReturn)) {
446 --line_end;
447 }
448 *line = message.substr(line_begin, (line_end - line_begin));
449 const char* cline = line->c_str();
450 // RFC 4566
451 // An SDP session description consists of a number of lines of text of
452 // the form:
453 // <type>=<value>
454 // where <type> MUST be exactly one case-significant character and
455 // <value> is structured text whose format depends on <type>.
456 // Whitespace MUST NOT be used on either side of the "=" sign.
decurtis@webrtc.org8af11042015-01-07 19:15:51 +0000457 if (line->length() < 3 ||
458 !islower(cline[0]) ||
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000459 cline[1] != kSdpDelimiterEqual ||
460 cline[2] == kSdpDelimiterSpace) {
461 *pos = line_begin;
462 return false;
463 }
464 return true;
465}
466
467// Init |os| to "|type|=|value|".
468static void InitLine(const char type,
469 const std::string& value,
470 std::ostringstream* os) {
471 os->str("");
472 *os << type << kSdpDelimiterEqual << value;
473}
474
475// Init |os| to "a=|attribute|".
476static void InitAttrLine(const std::string& attribute, std::ostringstream* os) {
477 InitLine(kLineTypeAttributes, attribute, os);
478}
479
480// Writes a SDP attribute line based on |attribute| and |value| to |message|.
481static void AddAttributeLine(const std::string& attribute, int value,
482 std::string* message) {
483 std::ostringstream os;
484 InitAttrLine(attribute, &os);
485 os << kSdpDelimiterColon << value;
486 AddLine(os.str(), message);
487}
488
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000489static bool IsLineType(const std::string& message,
490 const char type,
491 size_t line_start) {
492 if (message.size() < line_start + kLinePrefixLength) {
493 return false;
494 }
495 const char* cmessage = message.c_str();
496 return (cmessage[line_start] == type &&
497 cmessage[line_start + 1] == kSdpDelimiterEqual);
498}
499
500static bool IsLineType(const std::string& line,
501 const char type) {
502 return IsLineType(line, type, 0);
503}
504
505static bool GetLineWithType(const std::string& message, size_t* pos,
506 std::string* line, const char type) {
507 if (!IsLineType(message, type, *pos)) {
508 return false;
509 }
510
511 if (!GetLine(message, pos, line))
512 return false;
513
514 return true;
515}
516
517static bool HasAttribute(const std::string& line,
518 const std::string& attribute) {
519 return (line.compare(kLinePrefixLength, attribute.size(), attribute) == 0);
520}
521
Peter Boström0c4e06b2015-10-07 12:23:21 +0200522static bool AddSsrcLine(uint32_t ssrc_id,
523 const std::string& attribute,
524 const std::string& value,
525 std::string* message) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000526 // RFC 5576
527 // a=ssrc:<ssrc-id> <attribute>:<value>
528 std::ostringstream os;
529 InitAttrLine(kAttributeSsrc, &os);
530 os << kSdpDelimiterColon << ssrc_id << kSdpDelimiterSpace
531 << attribute << kSdpDelimiterColon << value;
532 return AddLine(os.str(), message);
533}
534
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000535// Get value only from <attribute>:<value>.
536static bool GetValue(const std::string& message, const std::string& attribute,
537 std::string* value, SdpParseError* error) {
538 std::string leftpart;
Donald Curtis0e07f922015-05-15 09:21:23 -0700539 if (!rtc::tokenize_first(message, kSdpDelimiterColon, &leftpart, value)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000540 return ParseFailedGetValue(message, attribute, error);
541 }
542 // The left part should end with the expected attribute.
543 if (leftpart.length() < attribute.length() ||
544 leftpart.compare(leftpart.length() - attribute.length(),
545 attribute.length(), attribute) != 0) {
546 return ParseFailedGetValue(message, attribute, error);
547 }
548 return true;
549}
550
551static bool CaseInsensitiveFind(std::string str1, std::string str2) {
552 std::transform(str1.begin(), str1.end(), str1.begin(),
553 ::tolower);
554 std::transform(str2.begin(), str2.end(), str2.begin(),
555 ::tolower);
556 return str1.find(str2) != std::string::npos;
557}
558
wu@webrtc.org5e760e72014-04-02 23:19:09 +0000559template <class T>
560static bool GetValueFromString(const std::string& line,
561 const std::string& s,
562 T* t,
563 SdpParseError* error) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000564 if (!rtc::FromString(s, t)) {
wu@webrtc.org5e760e72014-04-02 23:19:09 +0000565 std::ostringstream description;
566 description << "Invalid value: " << s << ".";
567 return ParseFailed(line, description.str(), error);
568 }
569 return true;
570}
571
pkasting@chromium.orge9facf82015-02-17 20:36:28 +0000572static bool GetPayloadTypeFromString(const std::string& line,
573 const std::string& s,
574 int* payload_type,
575 SdpParseError* error) {
576 return GetValueFromString(line, s, payload_type, error) &&
577 cricket::IsValidRtpPayloadType(*payload_type);
578}
579
Taylor Brandstetter5de6b752016-03-09 17:02:30 -0800580// |msid_stream_id| and |msid_track_id| represent the stream/track ID from the
581// "a=msid" attribute, if it exists. They are empty if the attribute does not
582// exist.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000583void CreateTracksFromSsrcInfos(const SsrcInfoVec& ssrc_infos,
Taylor Brandstetter5de6b752016-03-09 17:02:30 -0800584 const std::string& msid_stream_id,
585 const std::string& msid_track_id,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000586 StreamParamsVec* tracks) {
nisseede5da42017-01-12 05:15:36 -0800587 RTC_DCHECK(tracks != NULL);
588 RTC_DCHECK(msid_stream_id.empty() == msid_track_id.empty());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000589 for (SsrcInfoVec::const_iterator ssrc_info = ssrc_infos.begin();
590 ssrc_info != ssrc_infos.end(); ++ssrc_info) {
591 if (ssrc_info->cname.empty()) {
592 continue;
593 }
594
Taylor Brandstetter5de6b752016-03-09 17:02:30 -0800595 std::string stream_id;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000596 std::string track_id;
Taylor Brandstetter5de6b752016-03-09 17:02:30 -0800597 if (ssrc_info->stream_id.empty() && !ssrc_info->mslabel.empty()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000598 // If there's no msid and there's mslabel, we consider this is a sdp from
599 // a older version of client that doesn't support msid.
600 // In that case, we use the mslabel and label to construct the track.
Taylor Brandstetter5de6b752016-03-09 17:02:30 -0800601 stream_id = ssrc_info->mslabel;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000602 track_id = ssrc_info->label;
Taylor Brandstetter5de6b752016-03-09 17:02:30 -0800603 } else if (ssrc_info->stream_id.empty() && !msid_stream_id.empty()) {
604 // If there's no msid in the SSRC attributes, but there's a global one
605 // (from a=msid), use that. This is the case with unified plan SDP.
606 stream_id = msid_stream_id;
607 track_id = msid_track_id;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000608 } else {
Taylor Brandstetter5de6b752016-03-09 17:02:30 -0800609 stream_id = ssrc_info->stream_id;
deadbeef9d3584c2016-02-16 17:54:10 -0800610 track_id = ssrc_info->track_id;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000611 }
Taylor Brandstetter5de6b752016-03-09 17:02:30 -0800612 // If a stream/track ID wasn't populated from the SSRC attributes OR the
613 // msid attribute, use default/random values.
614 if (stream_id.empty()) {
615 stream_id = kDefaultMsid;
616 }
617 if (track_id.empty()) {
618 // TODO(ronghuawu): What should we do if the track id doesn't appear?
619 // Create random string (which will be used as track label later)?
620 track_id = rtc::CreateRandomString(8);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000621 }
622
623 StreamParamsVec::iterator track = tracks->begin();
624 for (; track != tracks->end(); ++track) {
625 if (track->id == track_id) {
626 break;
627 }
628 }
629 if (track == tracks->end()) {
630 // If we don't find an existing track, create a new one.
631 tracks->push_back(StreamParams());
632 track = tracks->end() - 1;
633 }
634 track->add_ssrc(ssrc_info->ssrc_id);
635 track->cname = ssrc_info->cname;
Taylor Brandstetter5de6b752016-03-09 17:02:30 -0800636 track->sync_label = stream_id;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000637 track->id = track_id;
638 }
639}
640
641void GetMediaStreamLabels(const ContentInfo* content,
642 std::set<std::string>* labels) {
643 const MediaContentDescription* media_desc =
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000644 static_cast<const MediaContentDescription*>(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000645 content->description);
646 const cricket::StreamParamsVec& streams = media_desc->streams();
647 for (cricket::StreamParamsVec::const_iterator it = streams.begin();
648 it != streams.end(); ++it) {
649 labels->insert(it->sync_label);
650 }
651}
652
653// RFC 5245
654// It is RECOMMENDED that default candidates be chosen based on the
655// likelihood of those candidates to work with the peer that is being
656// contacted. It is RECOMMENDED that relayed > reflexive > host.
657static const int kPreferenceUnknown = 0;
658static const int kPreferenceHost = 1;
659static const int kPreferenceReflexive = 2;
660static const int kPreferenceRelayed = 3;
661
662static int GetCandidatePreferenceFromType(const std::string& type) {
663 int preference = kPreferenceUnknown;
664 if (type == cricket::LOCAL_PORT_TYPE) {
665 preference = kPreferenceHost;
666 } else if (type == cricket::STUN_PORT_TYPE) {
667 preference = kPreferenceReflexive;
668 } else if (type == cricket::RELAY_PORT_TYPE) {
669 preference = kPreferenceRelayed;
670 } else {
nissec80e7412017-01-11 05:56:46 -0800671 RTC_NOTREACHED();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000672 }
673 return preference;
674}
675
guoweis@webrtc.org57ac2c82015-02-06 00:45:13 +0000676// Get ip and port of the default destination from the |candidates| with the
677// given value of |component_id|. The default candidate should be the one most
678// likely to work, typically IPv4 relay.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000679// RFC 5245
680// The value of |component_id| currently supported are 1 (RTP) and 2 (RTCP).
Steve Anton36b29d12017-10-30 09:57:42 -0700681// TODO(deadbeef): Decide the default destination in webrtcsession and
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000682// pass it down via SessionDescription.
pthatcher@webrtc.orgc9d6d142014-10-23 23:37:22 +0000683static void GetDefaultDestination(
684 const std::vector<Candidate>& candidates,
685 int component_id, std::string* port,
686 std::string* ip, std::string* addr_type) {
perkj@webrtc.orgd105cc82014-11-07 11:22:06 +0000687 *addr_type = kConnectionIpv4Addrtype;
pthatcher@webrtc.orgc9d6d142014-10-23 23:37:22 +0000688 *port = kDummyPort;
689 *ip = kDummyAddress;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000690 int current_preference = kPreferenceUnknown;
guoweis@webrtc.org57ac2c82015-02-06 00:45:13 +0000691 int current_family = AF_UNSPEC;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000692 for (std::vector<Candidate>::const_iterator it = candidates.begin();
693 it != candidates.end(); ++it) {
694 if (it->component() != component_id) {
695 continue;
696 }
guoweis@webrtc.org57ac2c82015-02-06 00:45:13 +0000697 // Default destination should be UDP only.
698 if (it->protocol() != cricket::UDP_PROTOCOL_NAME) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000699 continue;
700 }
guoweis@webrtc.org57ac2c82015-02-06 00:45:13 +0000701 const int preference = GetCandidatePreferenceFromType(it->type());
702 const int family = it->address().ipaddr().family();
703 // See if this candidate is more preferable then the current one if it's the
704 // same family. Or if the current family is IPv4 already so we could safely
705 // ignore all IPv6 ones. WebRTC bug 4269.
706 // http://code.google.com/p/webrtc/issues/detail?id=4269
707 if ((preference <= current_preference && current_family == family) ||
708 (current_family == AF_INET && family == AF_INET6)) {
709 continue;
710 }
pthatcher@webrtc.orgc9d6d142014-10-23 23:37:22 +0000711 if (family == AF_INET) {
712 addr_type->assign(kConnectionIpv4Addrtype);
713 } else if (family == AF_INET6) {
714 addr_type->assign(kConnectionIpv6Addrtype);
715 }
guoweis@webrtc.org57ac2c82015-02-06 00:45:13 +0000716 current_preference = preference;
717 current_family = family;
718 *port = it->address().PortAsString();
719 *ip = it->address().ipaddr().ToString();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000720 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000721}
722
wu@webrtc.org4c3e9912014-07-16 21:03:13 +0000723// Gets "a=rtcp" line if found default RTCP candidate from |candidates|.
724static std::string GetRtcpLine(const std::vector<Candidate>& candidates) {
pthatcher@webrtc.orgc9d6d142014-10-23 23:37:22 +0000725 std::string rtcp_line, rtcp_port, rtcp_ip, addr_type;
726 GetDefaultDestination(candidates, ICE_CANDIDATE_COMPONENT_RTCP,
727 &rtcp_port, &rtcp_ip, &addr_type);
728 // Found default RTCP candidate.
729 // RFC 5245
730 // If the agent is utilizing RTCP, it MUST encode the RTCP candidate
731 // using the a=rtcp attribute as defined in RFC 3605.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000732
pthatcher@webrtc.orgc9d6d142014-10-23 23:37:22 +0000733 // RFC 3605
734 // rtcp-attribute = "a=rtcp:" port [nettype space addrtype space
735 // connection-address] CRLF
736 std::ostringstream os;
737 InitAttrLine(kAttributeRtcp, &os);
738 os << kSdpDelimiterColon
739 << rtcp_port << " "
740 << kConnectionNettype << " "
741 << addr_type << " "
742 << rtcp_ip;
743 rtcp_line = os.str();
wu@webrtc.org4c3e9912014-07-16 21:03:13 +0000744 return rtcp_line;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000745}
746
747// Get candidates according to the mline index from SessionDescriptionInterface.
748static void GetCandidatesByMindex(const SessionDescriptionInterface& desci,
749 int mline_index,
750 std::vector<Candidate>* candidates) {
751 if (!candidates) {
752 return;
753 }
754 const IceCandidateCollection* cc = desci.candidates(mline_index);
755 for (size_t i = 0; i < cc->count(); ++i) {
756 const IceCandidateInterface* candidate = cc->at(i);
757 candidates->push_back(candidate->candidate());
758 }
759}
760
deadbeef90f1e1e2017-02-10 12:35:05 -0800761static bool IsValidPort(int port) {
762 return port >= 0 && port <= 65535;
763}
764
deadbeef9d3584c2016-02-16 17:54:10 -0800765std::string SdpSerialize(const JsepSessionDescription& jdesc,
766 bool unified_plan_sdp) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000767 const cricket::SessionDescription* desc = jdesc.description();
768 if (!desc) {
769 return "";
770 }
771
772 std::string message;
773
774 // Session Description.
775 AddLine(kSessionVersion, &message);
776 // Session Origin
777 // RFC 4566
778 // o=<username> <sess-id> <sess-version> <nettype> <addrtype>
779 // <unicast-address>
780 std::ostringstream os;
781 InitLine(kLineTypeOrigin, kSessionOriginUsername, &os);
jbauch083b73f2015-07-16 02:46:32 -0700782 const std::string& session_id = jdesc.session_id().empty() ?
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000783 kSessionOriginSessionId : jdesc.session_id();
jbauch083b73f2015-07-16 02:46:32 -0700784 const std::string& session_version = jdesc.session_version().empty() ?
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000785 kSessionOriginSessionVersion : jdesc.session_version();
786 os << " " << session_id << " " << session_version << " "
787 << kSessionOriginNettype << " " << kSessionOriginAddrtype << " "
788 << kSessionOriginAddress;
789 AddLine(os.str(), &message);
790 AddLine(kSessionName, &message);
791
792 // Time Description.
793 AddLine(kTimeDescription, &message);
794
795 // Group
796 if (desc->HasGroup(cricket::GROUP_TYPE_BUNDLE)) {
797 std::string group_line = kAttrGroup;
798 const cricket::ContentGroup* group =
799 desc->GetGroupByName(cricket::GROUP_TYPE_BUNDLE);
nisseede5da42017-01-12 05:15:36 -0800800 RTC_DCHECK(group != NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000801 const cricket::ContentNames& content_names = group->content_names();
802 for (cricket::ContentNames::const_iterator it = content_names.begin();
803 it != content_names.end(); ++it) {
804 group_line.append(" ");
805 group_line.append(*it);
806 }
807 AddLine(group_line, &message);
808 }
809
810 // MediaStream semantics
811 InitAttrLine(kAttributeMsidSemantics, &os);
812 os << kSdpDelimiterColon << " " << kMediaStreamSemantic;
wu@webrtc.org4c3e9912014-07-16 21:03:13 +0000813
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000814 std::set<std::string> media_stream_labels;
815 const ContentInfo* audio_content = GetFirstAudioContent(desc);
816 if (audio_content)
817 GetMediaStreamLabels(audio_content, &media_stream_labels);
wu@webrtc.org4c3e9912014-07-16 21:03:13 +0000818
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000819 const ContentInfo* video_content = GetFirstVideoContent(desc);
820 if (video_content)
821 GetMediaStreamLabels(video_content, &media_stream_labels);
wu@webrtc.org4c3e9912014-07-16 21:03:13 +0000822
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000823 for (std::set<std::string>::const_iterator it =
824 media_stream_labels.begin(); it != media_stream_labels.end(); ++it) {
825 os << " " << *it;
826 }
827 AddLine(os.str(), &message);
828
wu@webrtc.org4c3e9912014-07-16 21:03:13 +0000829 // Preserve the order of the media contents.
830 int mline_index = -1;
831 for (cricket::ContentInfos::const_iterator it = desc->contents().begin();
832 it != desc->contents().end(); ++it) {
833 const MediaContentDescription* mdesc =
834 static_cast<const MediaContentDescription*>(it->description);
835 std::vector<Candidate> candidates;
836 GetCandidatesByMindex(jdesc, ++mline_index, &candidates);
deadbeef9d3584c2016-02-16 17:54:10 -0800837 BuildMediaDescription(&*it, desc->GetTransportInfoByName(it->name),
838 mdesc->type(), candidates, unified_plan_sdp,
wu@webrtc.org4c3e9912014-07-16 21:03:13 +0000839 &message);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000840 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000841 return message;
842}
843
844// Serializes the passed in IceCandidateInterface to a SDP string.
845// candidate - The candidate to be serialized.
Honghai Zhang7fb69db2016-03-14 11:59:18 -0700846std::string SdpSerializeCandidate(const IceCandidateInterface& candidate) {
847 return SdpSerializeCandidate(candidate.candidate());
848}
849
850// Serializes a cricket Candidate.
851std::string SdpSerializeCandidate(const cricket::Candidate& candidate) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000852 std::string message;
Honghai Zhang7fb69db2016-03-14 11:59:18 -0700853 std::vector<cricket::Candidate> candidates(1, candidate);
honghaiza54a0802015-12-16 18:37:23 -0800854 BuildCandidate(candidates, true, &message);
wu@webrtc.orgec9f5fb2014-06-24 17:05:10 +0000855 // From WebRTC draft section 4.8.1.1 candidate-attribute will be
856 // just candidate:<candidate> not a=candidate:<blah>CRLF
nisseede5da42017-01-12 05:15:36 -0800857 RTC_DCHECK(message.find("a=") == 0);
wu@webrtc.orgec9f5fb2014-06-24 17:05:10 +0000858 message.erase(0, 2);
nisseede5da42017-01-12 05:15:36 -0800859 RTC_DCHECK(message.find(kLineBreak) == message.size() - 2);
wu@webrtc.orgec9f5fb2014-06-24 17:05:10 +0000860 message.resize(message.size() - 2);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000861 return message;
862}
863
864bool SdpDeserialize(const std::string& message,
865 JsepSessionDescription* jdesc,
866 SdpParseError* error) {
867 std::string session_id;
868 std::string session_version;
Peter Thatcher7cbd1882015-09-17 18:54:52 -0700869 TransportDescription session_td("", "");
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000870 RtpHeaderExtensions session_extmaps;
zhihuang38989e52017-03-21 11:04:53 -0700871 rtc::SocketAddress session_connection_addr;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000872 cricket::SessionDescription* desc = new cricket::SessionDescription();
873 std::vector<JsepIceCandidate*> candidates;
874 size_t current_pos = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000875
876 // Session Description
877 if (!ParseSessionDescription(message, &current_pos, &session_id,
deadbeefc80741f2015-10-22 13:14:45 -0700878 &session_version, &session_td, &session_extmaps,
zhihuang38989e52017-03-21 11:04:53 -0700879 &session_connection_addr, desc, error)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000880 delete desc;
881 return false;
882 }
883
884 // Media Description
deadbeefc80741f2015-10-22 13:14:45 -0700885 if (!ParseMediaDescription(message, session_td, session_extmaps, &current_pos,
zhihuang38989e52017-03-21 11:04:53 -0700886 session_connection_addr, desc, &candidates,
887 error)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000888 delete desc;
889 for (std::vector<JsepIceCandidate*>::const_iterator
890 it = candidates.begin(); it != candidates.end(); ++it) {
891 delete *it;
892 }
893 return false;
894 }
895
896 jdesc->Initialize(desc, session_id, session_version);
897
898 for (std::vector<JsepIceCandidate*>::const_iterator
899 it = candidates.begin(); it != candidates.end(); ++it) {
900 jdesc->AddCandidate(*it);
901 delete *it;
902 }
903 return true;
904}
905
906bool SdpDeserializeCandidate(const std::string& message,
907 JsepIceCandidate* jcandidate,
908 SdpParseError* error) {
nisseede5da42017-01-12 05:15:36 -0800909 RTC_DCHECK(jcandidate != NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000910 Candidate candidate;
911 if (!ParseCandidate(message, &candidate, error, true)) {
912 return false;
913 }
914 jcandidate->SetCandidate(candidate);
915 return true;
916}
917
Honghai Zhang7fb69db2016-03-14 11:59:18 -0700918bool SdpDeserializeCandidate(const std::string& transport_name,
919 const std::string& message,
920 cricket::Candidate* candidate,
921 SdpParseError* error) {
nisseede5da42017-01-12 05:15:36 -0800922 RTC_DCHECK(candidate != nullptr);
Honghai Zhang7fb69db2016-03-14 11:59:18 -0700923 if (!ParseCandidate(message, candidate, error, true)) {
924 return false;
925 }
926 candidate->set_transport_name(transport_name);
927 return true;
928}
929
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000930bool ParseCandidate(const std::string& message, Candidate* candidate,
931 SdpParseError* error, bool is_raw) {
nisseede5da42017-01-12 05:15:36 -0800932 RTC_DCHECK(candidate != NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000933
934 // Get the first line from |message|.
jiayl@webrtc.org7ec3f9f2014-08-08 23:09:15 +0000935 std::string first_line = message;
936 size_t pos = 0;
937 GetLine(message, &pos, &first_line);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000938
jiayl@webrtc.org7ec3f9f2014-08-08 23:09:15 +0000939 // Makes sure |message| contains only one line.
940 if (message.size() > first_line.size()) {
941 std::string left, right;
Donald Curtis0e07f922015-05-15 09:21:23 -0700942 if (rtc::tokenize_first(message, kNewLine, &left, &right) &&
943 !right.empty()) {
jiayl@webrtc.org7ec3f9f2014-08-08 23:09:15 +0000944 return ParseFailed(message, 0, "Expect one line only", error);
945 }
946 }
947
948 // From WebRTC draft section 4.8.1.1 candidate-attribute should be
949 // candidate:<candidate> when trickled, but we still support
950 // a=candidate:<blah>CRLF for backward compatibility and for parsing a line
951 // from the SDP.
952 if (IsLineType(first_line, kLineTypeAttributes)) {
953 first_line = first_line.substr(kLinePrefixLength);
954 }
955
956 std::string attribute_candidate;
957 std::string candidate_value;
958
959 // |first_line| must be in the form of "candidate:<value>".
Donald Curtis144d0182015-05-15 13:14:24 -0700960 if (!rtc::tokenize_first(first_line, kSdpDelimiterColon, &attribute_candidate,
961 &candidate_value) ||
jiayl@webrtc.org7ec3f9f2014-08-08 23:09:15 +0000962 attribute_candidate != kAttributeCandidate) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000963 if (is_raw) {
964 std::ostringstream description;
jiayl@webrtc.org7ec3f9f2014-08-08 23:09:15 +0000965 description << "Expect line: " << kAttributeCandidate
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000966 << ":" << "<candidate-str>";
967 return ParseFailed(first_line, 0, description.str(), error);
968 } else {
969 return ParseFailedExpectLine(first_line, 0, kLineTypeAttributes,
970 kAttributeCandidate, error);
971 }
972 }
973
974 std::vector<std::string> fields;
jiayl@webrtc.org7ec3f9f2014-08-08 23:09:15 +0000975 rtc::split(candidate_value, kSdpDelimiterSpace, &fields);
976
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000977 // RFC 5245
978 // a=candidate:<foundation> <component-id> <transport> <priority>
979 // <connection-address> <port> typ <candidate-types>
980 // [raddr <connection-address>] [rport <port>]
981 // *(SP extension-att-name SP extension-att-value)
982 const size_t expected_min_fields = 8;
983 if (fields.size() < expected_min_fields ||
984 (fields[6] != kAttributeCandidateTyp)) {
985 return ParseFailedExpectMinFieldNum(first_line, expected_min_fields, error);
986 }
jbauch083b73f2015-07-16 02:46:32 -0700987 const std::string& foundation = fields[0];
jiayl@webrtc.org7ec3f9f2014-08-08 23:09:15 +0000988
wu@webrtc.org5e760e72014-04-02 23:19:09 +0000989 int component_id = 0;
990 if (!GetValueFromString(first_line, fields[1], &component_id, error)) {
991 return false;
992 }
jbauch083b73f2015-07-16 02:46:32 -0700993 const std::string& transport = fields[2];
Peter Boström0c4e06b2015-10-07 12:23:21 +0200994 uint32_t priority = 0;
wu@webrtc.org5e760e72014-04-02 23:19:09 +0000995 if (!GetValueFromString(first_line, fields[3], &priority, error)) {
996 return false;
997 }
jbauch083b73f2015-07-16 02:46:32 -0700998 const std::string& connection_address = fields[4];
wu@webrtc.org5e760e72014-04-02 23:19:09 +0000999 int port = 0;
1000 if (!GetValueFromString(first_line, fields[5], &port, error)) {
1001 return false;
1002 }
deadbeef90f1e1e2017-02-10 12:35:05 -08001003 if (!IsValidPort(port)) {
1004 return ParseFailed(first_line, "Invalid port number.", error);
1005 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001006 SocketAddress address(connection_address, port);
1007
1008 cricket::ProtocolType protocol;
hnslb68cc752016-12-13 10:33:41 -08001009 if (!StringToProto(transport.c_str(), &protocol)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001010 return ParseFailed(first_line, "Unsupported transport type.", error);
1011 }
hnslb68cc752016-12-13 10:33:41 -08001012 switch (protocol) {
1013 case cricket::PROTO_UDP:
1014 case cricket::PROTO_TCP:
1015 case cricket::PROTO_SSLTCP:
1016 // Supported protocol.
1017 break;
1018 default:
1019 return ParseFailed(first_line, "Unsupported transport type.", error);
1020 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001021
1022 std::string candidate_type;
jbauch083b73f2015-07-16 02:46:32 -07001023 const std::string& type = fields[7];
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001024 if (type == kCandidateHost) {
1025 candidate_type = cricket::LOCAL_PORT_TYPE;
1026 } else if (type == kCandidateSrflx) {
1027 candidate_type = cricket::STUN_PORT_TYPE;
1028 } else if (type == kCandidateRelay) {
1029 candidate_type = cricket::RELAY_PORT_TYPE;
Honghai Zhang7fb69db2016-03-14 11:59:18 -07001030 } else if (type == kCandidatePrflx) {
1031 candidate_type = cricket::PRFLX_PORT_TYPE;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001032 } else {
1033 return ParseFailed(first_line, "Unsupported candidate type.", error);
1034 }
1035
1036 size_t current_position = expected_min_fields;
1037 SocketAddress related_address;
1038 // The 2 optional fields for related address
1039 // [raddr <connection-address>] [rport <port>]
1040 if (fields.size() >= (current_position + 2) &&
1041 fields[current_position] == kAttributeCandidateRaddr) {
1042 related_address.SetIP(fields[++current_position]);
1043 ++current_position;
1044 }
1045 if (fields.size() >= (current_position + 2) &&
1046 fields[current_position] == kAttributeCandidateRport) {
wu@webrtc.org5e760e72014-04-02 23:19:09 +00001047 int port = 0;
1048 if (!GetValueFromString(
1049 first_line, fields[++current_position], &port, error)) {
1050 return false;
1051 }
deadbeef90f1e1e2017-02-10 12:35:05 -08001052 if (!IsValidPort(port)) {
1053 return ParseFailed(first_line, "Invalid port number.", error);
1054 }
wu@webrtc.org5e760e72014-04-02 23:19:09 +00001055 related_address.SetPort(port);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001056 ++current_position;
1057 }
1058
mallinath@webrtc.org2d60c5e2014-08-08 22:29:20 +00001059 // If this is a TCP candidate, it has additional extension as defined in
1060 // RFC 6544.
1061 std::string tcptype;
1062 if (fields.size() >= (current_position + 2) &&
1063 fields[current_position] == kTcpCandidateType) {
1064 tcptype = fields[++current_position];
1065 ++current_position;
1066
1067 if (tcptype != cricket::TCPTYPE_ACTIVE_STR &&
1068 tcptype != cricket::TCPTYPE_PASSIVE_STR &&
1069 tcptype != cricket::TCPTYPE_SIMOPEN_STR) {
1070 return ParseFailed(first_line, "Invalid TCP candidate type.", error);
1071 }
1072
1073 if (protocol != cricket::PROTO_TCP) {
1074 return ParseFailed(first_line, "Invalid non-TCP candidate", error);
1075 }
1076 }
1077
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001078 // Extension
honghaiza54a0802015-12-16 18:37:23 -08001079 // Though non-standard, we support the ICE ufrag and pwd being signaled on
1080 // the candidate to avoid issues with confusing which generation a candidate
1081 // belongs to when trickling multiple generations at the same time.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001082 std::string username;
1083 std::string password;
Peter Boström0c4e06b2015-10-07 12:23:21 +02001084 uint32_t generation = 0;
honghaiza0c44ea2016-03-23 16:07:48 -07001085 uint16_t network_id = 0;
1086 uint16_t network_cost = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001087 for (size_t i = current_position; i + 1 < fields.size(); ++i) {
1088 // RFC 5245
1089 // *(SP extension-att-name SP extension-att-value)
1090 if (fields[i] == kAttributeCandidateGeneration) {
wu@webrtc.org5e760e72014-04-02 23:19:09 +00001091 if (!GetValueFromString(first_line, fields[++i], &generation, error)) {
1092 return false;
1093 }
honghaiza54a0802015-12-16 18:37:23 -08001094 } else if (fields[i] == kAttributeCandidateUfrag) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001095 username = fields[++i];
honghaiza54a0802015-12-16 18:37:23 -08001096 } else if (fields[i] == kAttributeCandidatePwd) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001097 password = fields[++i];
honghaiza0c44ea2016-03-23 16:07:48 -07001098 } else if (fields[i] == kAttributeCandidateNetworkId) {
1099 if (!GetValueFromString(first_line, fields[++i], &network_id, error)) {
1100 return false;
1101 }
honghaize1a0c942016-02-16 14:54:56 -08001102 } else if (fields[i] == kAttributeCandidateNetworkCost) {
1103 if (!GetValueFromString(first_line, fields[++i], &network_cost, error)) {
1104 return false;
1105 }
Honghai Zhang351d77b2016-05-20 15:08:29 -07001106 network_cost = std::min(network_cost, rtc::kNetworkCostMax);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001107 } else {
1108 // Skip the unknown extension.
1109 ++i;
1110 }
1111 }
1112
guoweis@webrtc.org61c12472015-01-15 06:53:07 +00001113 *candidate = Candidate(component_id, cricket::ProtoToString(protocol),
guoweis@webrtc.org950c5182014-12-16 23:01:31 +00001114 address, priority, username, password, candidate_type,
honghaiza0c44ea2016-03-23 16:07:48 -07001115 generation, foundation, network_id, network_cost);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001116 candidate->set_related_address(related_address);
mallinath@webrtc.org2d60c5e2014-08-08 22:29:20 +00001117 candidate->set_tcptype(tcptype);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001118 return true;
1119}
1120
1121bool ParseIceOptions(const std::string& line,
1122 std::vector<std::string>* transport_options,
1123 SdpParseError* error) {
1124 std::string ice_options;
1125 if (!GetValue(line, kAttributeIceOption, &ice_options, error)) {
1126 return false;
1127 }
1128 std::vector<std::string> fields;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001129 rtc::split(ice_options, kSdpDelimiterSpace, &fields);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001130 for (size_t i = 0; i < fields.size(); ++i) {
1131 transport_options->push_back(fields[i]);
1132 }
1133 return true;
1134}
1135
jiayl@webrtc.orgddb85ab2014-09-05 16:31:56 +00001136bool ParseSctpPort(const std::string& line,
1137 int* sctp_port,
1138 SdpParseError* error) {
1139 // draft-ietf-mmusic-sctp-sdp-07
1140 // a=sctp-port
1141 std::vector<std::string> fields;
jiayl@webrtc.orgddb85ab2014-09-05 16:31:56 +00001142 const size_t expected_min_fields = 2;
lally69f57602015-10-08 10:15:04 -07001143 rtc::split(line.substr(kLinePrefixLength), kSdpDelimiterColon, &fields);
1144 if (fields.size() < expected_min_fields) {
1145 fields.resize(0);
1146 rtc::split(line.substr(kLinePrefixLength), kSdpDelimiterSpace, &fields);
1147 }
jiayl@webrtc.orgddb85ab2014-09-05 16:31:56 +00001148 if (fields.size() < expected_min_fields) {
1149 return ParseFailedExpectMinFieldNum(line, expected_min_fields, error);
1150 }
1151 if (!rtc::FromString(fields[1], sctp_port)) {
lally69f57602015-10-08 10:15:04 -07001152 return ParseFailed(line, "Invalid sctp port value.", error);
jiayl@webrtc.orgddb85ab2014-09-05 16:31:56 +00001153 }
1154 return true;
1155}
1156
isheriff6f8d6862016-05-26 11:24:55 -07001157bool ParseExtmap(const std::string& line,
1158 RtpExtension* extmap,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001159 SdpParseError* error) {
1160 // RFC 5285
1161 // a=extmap:<value>["/"<direction>] <URI> <extensionattributes>
1162 std::vector<std::string> fields;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001163 rtc::split(line.substr(kLinePrefixLength),
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001164 kSdpDelimiterSpace, &fields);
1165 const size_t expected_min_fields = 2;
1166 if (fields.size() < expected_min_fields) {
1167 return ParseFailedExpectMinFieldNum(line, expected_min_fields, error);
1168 }
1169 std::string uri = fields[1];
1170
1171 std::string value_direction;
1172 if (!GetValue(fields[0], kAttributeExtmap, &value_direction, error)) {
1173 return false;
1174 }
1175 std::vector<std::string> sub_fields;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001176 rtc::split(value_direction, kSdpDelimiterSlash, &sub_fields);
wu@webrtc.org5e760e72014-04-02 23:19:09 +00001177 int value = 0;
1178 if (!GetValueFromString(line, sub_fields[0], &value, error)) {
1179 return false;
1180 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001181
jbauch5869f502017-06-29 12:31:36 -07001182 bool encrypted = false;
1183 if (uri == RtpExtension::kEncryptHeaderExtensionsUri) {
1184 // RFC 6904
Steve Anton36b29d12017-10-30 09:57:42 -07001185 // a=extmap:<value["/"<direction>] urn:ietf:params:rtp-hdrext:encrypt <URI>
1186 // <extensionattributes>
jbauch5869f502017-06-29 12:31:36 -07001187 const size_t expected_min_fields_encrypted = expected_min_fields + 1;
1188 if (fields.size() < expected_min_fields_encrypted) {
1189 return ParseFailedExpectMinFieldNum(line, expected_min_fields_encrypted,
1190 error);
1191 }
1192
1193 encrypted = true;
1194 uri = fields[2];
1195 if (uri == RtpExtension::kEncryptHeaderExtensionsUri) {
1196 return ParseFailed(line, "Recursive encrypted header.", error);
1197 }
1198 }
1199
1200 *extmap = RtpExtension(uri, value, encrypted);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001201 return true;
1202}
1203
1204void BuildMediaDescription(const ContentInfo* content_info,
1205 const TransportInfo* transport_info,
1206 const MediaType media_type,
wu@webrtc.org4c3e9912014-07-16 21:03:13 +00001207 const std::vector<Candidate>& candidates,
deadbeef9d3584c2016-02-16 17:54:10 -08001208 bool unified_plan_sdp,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001209 std::string* message) {
nisseede5da42017-01-12 05:15:36 -08001210 RTC_DCHECK(message != NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001211 if (content_info == NULL || message == NULL) {
1212 return;
1213 }
Steve Anton36b29d12017-10-30 09:57:42 -07001214 // TODO(deadbeef): Rethink if we should use sprintfn instead of stringstream.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001215 // According to the style guide, streams should only be used for logging.
1216 // http://google-styleguide.googlecode.com/svn/
1217 // trunk/cppguide.xml?showone=Streams#Streams
1218 std::ostringstream os;
1219 const MediaContentDescription* media_desc =
henrike@webrtc.org28654cb2013-07-22 21:07:49 +00001220 static_cast<const MediaContentDescription*>(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001221 content_info->description);
nisseede5da42017-01-12 05:15:36 -08001222 RTC_DCHECK(media_desc != NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001223
jiayl@webrtc.org9c16c392014-05-01 18:30:30 +00001224 int sctp_port = cricket::kSctpDefaultPort;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001225
1226 // RFC 4566
1227 // m=<media> <port> <proto> <fmt>
1228 // fmt is a list of payload type numbers that MAY be used in the session.
1229 const char* type = NULL;
1230 if (media_type == cricket::MEDIA_TYPE_AUDIO)
1231 type = kMediaTypeAudio;
1232 else if (media_type == cricket::MEDIA_TYPE_VIDEO)
1233 type = kMediaTypeVideo;
1234 else if (media_type == cricket::MEDIA_TYPE_DATA)
1235 type = kMediaTypeData;
1236 else
nissec80e7412017-01-11 05:56:46 -08001237 RTC_NOTREACHED();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001238
1239 std::string fmt;
1240 if (media_type == cricket::MEDIA_TYPE_VIDEO) {
1241 const VideoContentDescription* video_desc =
1242 static_cast<const VideoContentDescription*>(media_desc);
1243 for (std::vector<cricket::VideoCodec>::const_iterator it =
1244 video_desc->codecs().begin();
1245 it != video_desc->codecs().end(); ++it) {
1246 fmt.append(" ");
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001247 fmt.append(rtc::ToString<int>(it->id));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001248 }
1249 } else if (media_type == cricket::MEDIA_TYPE_AUDIO) {
1250 const AudioContentDescription* audio_desc =
1251 static_cast<const AudioContentDescription*>(media_desc);
1252 for (std::vector<cricket::AudioCodec>::const_iterator it =
1253 audio_desc->codecs().begin();
1254 it != audio_desc->codecs().end(); ++it) {
1255 fmt.append(" ");
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001256 fmt.append(rtc::ToString<int>(it->id));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001257 }
1258 } else if (media_type == cricket::MEDIA_TYPE_DATA) {
jiayl@webrtc.org9c16c392014-05-01 18:30:30 +00001259 const DataContentDescription* data_desc =
1260 static_cast<const DataContentDescription*>(media_desc);
pthatcher@webrtc.org3341b402015-02-13 21:14:22 +00001261 if (IsDtlsSctp(media_desc->protocol())) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001262 fmt.append(" ");
jiayl@webrtc.org9c16c392014-05-01 18:30:30 +00001263
zstein4b2e0822017-02-17 19:48:38 -08001264 if (data_desc->use_sctpmap()) {
1265 for (std::vector<cricket::DataCodec>::const_iterator it =
1266 data_desc->codecs().begin();
1267 it != data_desc->codecs().end(); ++it) {
1268 if (cricket::CodecNamesEq(it->name,
1269 cricket::kGoogleSctpDataCodecName) &&
1270 it->GetParam(cricket::kCodecParamPort, &sctp_port)) {
1271 break;
1272 }
jiayl@webrtc.org9c16c392014-05-01 18:30:30 +00001273 }
jiayl@webrtc.org9c16c392014-05-01 18:30:30 +00001274
zstein4b2e0822017-02-17 19:48:38 -08001275 fmt.append(rtc::ToString<int>(sctp_port));
1276 } else {
1277 fmt.append(kDefaultSctpmapProtocol);
1278 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001279 } else {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001280 for (std::vector<cricket::DataCodec>::const_iterator it =
1281 data_desc->codecs().begin();
1282 it != data_desc->codecs().end(); ++it) {
1283 fmt.append(" ");
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001284 fmt.append(rtc::ToString<int>(it->id));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001285 }
1286 }
1287 }
1288 // The fmt must never be empty. If no codecs are found, set the fmt attribute
1289 // to 0.
1290 if (fmt.empty()) {
1291 fmt = " 0";
1292 }
1293
deadbeef25ed4352016-12-12 18:37:36 -08001294 // The port number in the m line will be updated later when associated with
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001295 // the candidates.
deadbeef25ed4352016-12-12 18:37:36 -08001296 //
1297 // A port value of 0 indicates that the m= section is rejected.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001298 // RFC 3264
1299 // To reject an offered stream, the port number in the corresponding stream in
1300 // the answer MUST be set to zero.
deadbeef25ed4352016-12-12 18:37:36 -08001301 //
1302 // However, the BUNDLE draft adds a new meaning to port zero, when used along
1303 // with a=bundle-only.
zhihuang38989e52017-03-21 11:04:53 -07001304 std::string port = kDummyPort;
1305 if (content_info->rejected || content_info->bundle_only) {
1306 port = kMediaPortRejected;
1307 } else if (!media_desc->connection_address().IsNil()) {
1308 port = rtc::ToString(media_desc->connection_address().port());
1309 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001310
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001311 rtc::SSLFingerprint* fp = (transport_info) ?
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001312 transport_info->description.identity_fingerprint.get() : NULL;
1313
wu@webrtc.org4c3e9912014-07-16 21:03:13 +00001314 // Add the m and c lines.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001315 InitLine(kLineTypeMedia, type, &os);
1316 os << " " << port << " " << media_desc->protocol() << fmt;
zhihuang38989e52017-03-21 11:04:53 -07001317 AddLine(os.str(), message);
1318
1319 InitLine(kLineTypeConnection, kConnectionNettype, &os);
1320 if (media_desc->connection_address().IsNil()) {
1321 os << " " << kConnectionIpv4Addrtype << " " << kDummyAddress;
1322 } else if (media_desc->connection_address().family() == AF_INET) {
1323 os << " " << kConnectionIpv4Addrtype << " "
1324 << media_desc->connection_address().ipaddr().ToString();
1325 } else {
1326 os << " " << kConnectionIpv6Addrtype << " "
1327 << media_desc->connection_address().ipaddr().ToString();
1328 }
1329 AddLine(os.str(), message);
wu@webrtc.org4c3e9912014-07-16 21:03:13 +00001330
1331 // RFC 4566
1332 // b=AS:<bandwidth>
Peter Thatcherc0c3a862015-06-24 15:31:25 -07001333 if (media_desc->bandwidth() >= 1000) {
wu@webrtc.org4c3e9912014-07-16 21:03:13 +00001334 InitLine(kLineTypeSessionBandwidth, kApplicationSpecificMaximum, &os);
1335 os << kSdpDelimiterColon << (media_desc->bandwidth() / 1000);
1336 AddLine(os.str(), message);
1337 }
1338
deadbeef25ed4352016-12-12 18:37:36 -08001339 // Add the a=bundle-only line.
1340 if (content_info->bundle_only) {
1341 InitAttrLine(kAttributeBundleOnly, &os);
1342 AddLine(os.str(), message);
1343 }
1344
wu@webrtc.org4c3e9912014-07-16 21:03:13 +00001345 // Add the a=rtcp line.
pthatcher@webrtc.org3341b402015-02-13 21:14:22 +00001346 if (IsRtp(media_desc->protocol())) {
wu@webrtc.org4c3e9912014-07-16 21:03:13 +00001347 std::string rtcp_line = GetRtcpLine(candidates);
1348 if (!rtcp_line.empty()) {
1349 AddLine(rtcp_line, message);
1350 }
1351 }
1352
honghaiza54a0802015-12-16 18:37:23 -08001353 // Build the a=candidate lines. We don't include ufrag and pwd in the
1354 // candidates in the SDP to avoid redundancy.
1355 BuildCandidate(candidates, false, message);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001356
1357 // Use the transport_info to build the media level ice-ufrag and ice-pwd.
1358 if (transport_info) {
1359 // RFC 5245
1360 // ice-pwd-att = "ice-pwd" ":" password
1361 // ice-ufrag-att = "ice-ufrag" ":" ufrag
1362 // ice-ufrag
deadbeef3f7219b2015-12-28 15:17:14 -08001363 if (!transport_info->description.ice_ufrag.empty()) {
1364 InitAttrLine(kAttributeIceUfrag, &os);
1365 os << kSdpDelimiterColon << transport_info->description.ice_ufrag;
1366 AddLine(os.str(), message);
1367 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001368 // ice-pwd
deadbeef3f7219b2015-12-28 15:17:14 -08001369 if (!transport_info->description.ice_pwd.empty()) {
1370 InitAttrLine(kAttributeIcePwd, &os);
1371 os << kSdpDelimiterColon << transport_info->description.ice_pwd;
1372 AddLine(os.str(), message);
1373 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001374
1375 // draft-petithuguenin-mmusic-ice-attributes-level-03
1376 BuildIceOptions(transport_info->description.transport_options, message);
1377
1378 // RFC 4572
1379 // fingerprint-attribute =
1380 // "fingerprint" ":" hash-func SP fingerprint
1381 if (fp) {
1382 // Insert the fingerprint attribute.
1383 InitAttrLine(kAttributeFingerprint, &os);
1384 os << kSdpDelimiterColon
1385 << fp->algorithm << kSdpDelimiterSpace
1386 << fp->GetRfc4572Fingerprint();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001387 AddLine(os.str(), message);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001388
1389 // Inserting setup attribute.
1390 if (transport_info->description.connection_role !=
1391 cricket::CONNECTIONROLE_NONE) {
1392 // Making sure we are not using "passive" mode.
1393 cricket::ConnectionRole role =
1394 transport_info->description.connection_role;
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00001395 std::string dtls_role_str;
nissec16fa5e2017-02-07 07:18:43 -08001396 const bool success =
1397 cricket::ConnectionRoleToString(role, &dtls_role_str);
1398 RTC_DCHECK(success);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001399 InitAttrLine(kAttributeSetup, &os);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001400 os << kSdpDelimiterColon << dtls_role_str;
1401 AddLine(os.str(), message);
1402 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001403 }
1404 }
1405
1406 // RFC 3388
1407 // mid-attribute = "a=mid:" identification-tag
1408 // identification-tag = token
1409 // Use the content name as the mid identification-tag.
1410 InitAttrLine(kAttributeMid, &os);
1411 os << kSdpDelimiterColon << content_info->name;
1412 AddLine(os.str(), message);
1413
pthatcher@webrtc.org3341b402015-02-13 21:14:22 +00001414 if (IsDtlsSctp(media_desc->protocol())) {
zstein4b2e0822017-02-17 19:48:38 -08001415 const DataContentDescription* data_desc =
1416 static_cast<const DataContentDescription*>(media_desc);
1417 bool use_sctpmap = data_desc->use_sctpmap();
1418 BuildSctpContentAttributes(message, sctp_port, use_sctpmap);
pthatcher@webrtc.org3341b402015-02-13 21:14:22 +00001419 } else if (IsRtp(media_desc->protocol())) {
deadbeef9d3584c2016-02-16 17:54:10 -08001420 BuildRtpContentAttributes(media_desc, media_type, unified_plan_sdp,
1421 message);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001422 }
1423}
1424
zstein4b2e0822017-02-17 19:48:38 -08001425void BuildSctpContentAttributes(std::string* message,
1426 int sctp_port,
1427 bool use_sctpmap) {
wu@webrtc.org78187522013-10-07 23:32:02 +00001428 std::ostringstream os;
zstein4b2e0822017-02-17 19:48:38 -08001429 if (use_sctpmap) {
1430 // draft-ietf-mmusic-sctp-sdp-04
1431 // a=sctpmap:sctpmap-number protocol [streams]
1432 InitAttrLine(kAttributeSctpmap, &os);
1433 os << kSdpDelimiterColon << sctp_port << kSdpDelimiterSpace
1434 << kDefaultSctpmapProtocol << kSdpDelimiterSpace
1435 << cricket::kMaxSctpStreams;
1436 } else {
1437 // draft-ietf-mmusic-sctp-sdp-23
1438 // a=sctp-port:<port>
1439 InitAttrLine(kAttributeSctpPort, &os);
1440 os << kSdpDelimiterColon << sctp_port;
1441 // TODO(zstein): emit max-message-size here
1442 }
wu@webrtc.org78187522013-10-07 23:32:02 +00001443 AddLine(os.str(), message);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001444}
1445
deadbeef9d3584c2016-02-16 17:54:10 -08001446// If unified_plan_sdp is true, will use "a=msid".
1447void BuildRtpContentAttributes(const MediaContentDescription* media_desc,
1448 const MediaType media_type,
1449 bool unified_plan_sdp,
1450 std::string* message) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001451 std::ostringstream os;
1452 // RFC 5285
1453 // a=extmap:<value>["/"<direction>] <URI> <extensionattributes>
1454 // The definitions MUST be either all session level or all media level. This
1455 // implementation uses all media level.
1456 for (size_t i = 0; i < media_desc->rtp_header_extensions().size(); ++i) {
jbauch5869f502017-06-29 12:31:36 -07001457 const RtpExtension& extension = media_desc->rtp_header_extensions()[i];
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001458 InitAttrLine(kAttributeExtmap, &os);
jbauch5869f502017-06-29 12:31:36 -07001459 os << kSdpDelimiterColon << extension.id;
1460 if (extension.encrypt) {
1461 os << kSdpDelimiterSpace << RtpExtension::kEncryptHeaderExtensionsUri;
1462 }
1463 os << kSdpDelimiterSpace << extension.uri;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001464 AddLine(os.str(), message);
1465 }
1466
1467 // RFC 3264
1468 // a=sendrecv || a=sendonly || a=sendrecv || a=inactive
deadbeefc80741f2015-10-22 13:14:45 -07001469 switch (media_desc->direction()) {
Steve Anton4e70a722017-11-28 14:57:10 -08001470 case RtpTransceiverDirection::kInactive:
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001471 InitAttrLine(kAttributeInactive, &os);
1472 break;
Steve Anton4e70a722017-11-28 14:57:10 -08001473 case RtpTransceiverDirection::kSendOnly:
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001474 InitAttrLine(kAttributeSendOnly, &os);
1475 break;
Steve Anton4e70a722017-11-28 14:57:10 -08001476 case RtpTransceiverDirection::kRecvOnly:
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001477 InitAttrLine(kAttributeRecvOnly, &os);
1478 break;
Steve Anton4e70a722017-11-28 14:57:10 -08001479 case RtpTransceiverDirection::kSendRecv:
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001480 default:
1481 InitAttrLine(kAttributeSendRecv, &os);
1482 break;
1483 }
1484 AddLine(os.str(), message);
1485
deadbeef9d3584c2016-02-16 17:54:10 -08001486 // draft-ietf-mmusic-msid-11
1487 // a=msid:<stream id> <track id>
1488 if (unified_plan_sdp && !media_desc->streams().empty()) {
1489 if (media_desc->streams().size() > 1u) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01001490 RTC_LOG(LS_WARNING)
1491 << "Trying to serialize unified plan SDP with more than "
1492 << "one track in a media section. Omitting 'a=msid'.";
deadbeef9d3584c2016-02-16 17:54:10 -08001493 } else {
1494 auto track = media_desc->streams().begin();
1495 const std::string& stream_id = track->sync_label;
deadbeef9d3584c2016-02-16 17:54:10 -08001496 InitAttrLine(kAttributeMsid, &os);
1497 os << kSdpDelimiterColon << stream_id << kSdpDelimiterSpace << track->id;
1498 AddLine(os.str(), message);
1499 }
1500 }
1501
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001502 // RFC 5761
1503 // a=rtcp-mux
1504 if (media_desc->rtcp_mux()) {
1505 InitAttrLine(kAttributeRtcpMux, &os);
1506 AddLine(os.str(), message);
1507 }
1508
deadbeef13871492015-12-09 12:37:51 -08001509 // RFC 5506
1510 // a=rtcp-rsize
1511 if (media_desc->rtcp_reduced_size()) {
1512 InitAttrLine(kAttributeRtcpReducedSize, &os);
1513 AddLine(os.str(), message);
1514 }
1515
deadbeefd45aea82017-09-16 01:24:29 -07001516 if (media_desc->conference_mode()) {
1517 InitAttrLine(kAttributeXGoogleFlag, &os);
1518 os << kSdpDelimiterColon << kValueConference;
1519 AddLine(os.str(), message);
1520 }
1521
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001522 // RFC 4568
1523 // a=crypto:<tag> <crypto-suite> <key-params> [<session-params>]
1524 for (std::vector<CryptoParams>::const_iterator it =
1525 media_desc->cryptos().begin();
1526 it != media_desc->cryptos().end(); ++it) {
1527 InitAttrLine(kAttributeCrypto, &os);
1528 os << kSdpDelimiterColon << it->tag << " " << it->cipher_suite << " "
1529 << it->key_params;
1530 if (!it->session_params.empty()) {
1531 os << " " << it->session_params;
1532 }
1533 AddLine(os.str(), message);
1534 }
1535
1536 // RFC 4566
1537 // a=rtpmap:<payload type> <encoding name>/<clock rate>
1538 // [/<encodingparameters>]
1539 BuildRtpMap(media_desc, media_type, message);
1540
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001541 for (StreamParamsVec::const_iterator track = media_desc->streams().begin();
1542 track != media_desc->streams().end(); ++track) {
1543 // Require that the track belongs to a media stream,
1544 // ie the sync_label is set. This extra check is necessary since the
1545 // MediaContentDescription always contains a streamparam with an ssrc even
1546 // if no track or media stream have been created.
1547 if (track->sync_label.empty()) continue;
1548
1549 // Build the ssrc-group lines.
1550 for (size_t i = 0; i < track->ssrc_groups.size(); ++i) {
1551 // RFC 5576
1552 // a=ssrc-group:<semantics> <ssrc-id> ...
1553 if (track->ssrc_groups[i].ssrcs.empty()) {
1554 continue;
1555 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001556 InitAttrLine(kAttributeSsrcGroup, &os);
1557 os << kSdpDelimiterColon << track->ssrc_groups[i].semantics;
Peter Boström0c4e06b2015-10-07 12:23:21 +02001558 std::vector<uint32_t>::const_iterator ssrc =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001559 track->ssrc_groups[i].ssrcs.begin();
1560 for (; ssrc != track->ssrc_groups[i].ssrcs.end(); ++ssrc) {
Peter Boström0c4e06b2015-10-07 12:23:21 +02001561 os << kSdpDelimiterSpace << rtc::ToString<uint32_t>(*ssrc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001562 }
1563 AddLine(os.str(), message);
1564 }
1565 // Build the ssrc lines for each ssrc.
1566 for (size_t i = 0; i < track->ssrcs.size(); ++i) {
Peter Boström0c4e06b2015-10-07 12:23:21 +02001567 uint32_t ssrc = track->ssrcs[i];
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001568 // RFC 5576
1569 // a=ssrc:<ssrc-id> cname:<value>
1570 AddSsrcLine(ssrc, kSsrcAttributeCname,
1571 track->cname, message);
1572
1573 // draft-alvestrand-mmusic-msid-00
1574 // a=ssrc:<ssrc-id> msid:identifier [appdata]
deadbeef9d3584c2016-02-16 17:54:10 -08001575 // The appdata consists of the "id" attribute of a MediaStreamTrack,
1576 // which corresponds to the "id" attribute of StreamParams.
1577 const std::string& stream_id = track->sync_label;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001578 InitAttrLine(kAttributeSsrc, &os);
1579 os << kSdpDelimiterColon << ssrc << kSdpDelimiterSpace
deadbeef9d3584c2016-02-16 17:54:10 -08001580 << kSsrcAttributeMsid << kSdpDelimiterColon << stream_id
1581 << kSdpDelimiterSpace << track->id;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001582 AddLine(os.str(), message);
1583
deadbeef9d3584c2016-02-16 17:54:10 -08001584 // TODO(ronghuawu): Remove below code which is for backward
1585 // compatibility.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001586 // draft-alvestrand-rtcweb-mid-01
1587 // a=ssrc:<ssrc-id> mslabel:<value>
1588 // The label isn't yet defined.
1589 // a=ssrc:<ssrc-id> label:<value>
1590 AddSsrcLine(ssrc, kSsrcAttributeMslabel, track->sync_label, message);
1591 AddSsrcLine(ssrc, kSSrcAttributeLabel, track->id, message);
1592 }
1593 }
1594}
1595
1596void WriteFmtpHeader(int payload_type, std::ostringstream* os) {
1597 // fmtp header: a=fmtp:|payload_type| <parameters>
1598 // Add a=fmtp
1599 InitAttrLine(kAttributeFmtp, os);
1600 // Add :|payload_type|
1601 *os << kSdpDelimiterColon << payload_type;
1602}
1603
1604void WriteRtcpFbHeader(int payload_type, std::ostringstream* os) {
1605 // rtcp-fb header: a=rtcp-fb:|payload_type|
1606 // <parameters>/<ccm <ccm_parameters>>
1607 // Add a=rtcp-fb
1608 InitAttrLine(kAttributeRtcpFb, os);
1609 // Add :
1610 *os << kSdpDelimiterColon;
1611 if (payload_type == kWildcardPayloadType) {
1612 *os << "*";
1613 } else {
1614 *os << payload_type;
1615 }
1616}
1617
1618void WriteFmtpParameter(const std::string& parameter_name,
1619 const std::string& parameter_value,
1620 std::ostringstream* os) {
1621 // fmtp parameters: |parameter_name|=|parameter_value|
1622 *os << parameter_name << kSdpDelimiterEqual << parameter_value;
1623}
1624
1625void WriteFmtpParameters(const cricket::CodecParameterMap& parameters,
1626 std::ostringstream* os) {
1627 for (cricket::CodecParameterMap::const_iterator fmtp = parameters.begin();
1628 fmtp != parameters.end(); ++fmtp) {
hta62a216e2016-04-15 11:02:14 -07001629 // Parameters are a semicolon-separated list, no spaces.
1630 // The list is separated from the header by a space.
1631 if (fmtp == parameters.begin()) {
1632 *os << kSdpDelimiterSpace;
1633 } else {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001634 *os << kSdpDelimiterSemicolon;
1635 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001636 WriteFmtpParameter(fmtp->first, fmtp->second, os);
1637 }
1638}
1639
1640bool IsFmtpParam(const std::string& name) {
ossuaa4b0772017-01-30 07:41:18 -08001641 // RFC 4855, section 3 specifies the mapping of media format parameters to SDP
1642 // parameters. Only ptime, maxptime, channels and rate are placed outside of
1643 // the fmtp line. In WebRTC, channels and rate are already handled separately
1644 // and thus not included in the CodecParameterMap.
1645 return name != kCodecParamPTime && name != kCodecParamMaxPTime;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001646}
1647
1648// Retreives fmtp parameters from |params|, which may contain other parameters
1649// as well, and puts them in |fmtp_parameters|.
1650void GetFmtpParams(const cricket::CodecParameterMap& params,
1651 cricket::CodecParameterMap* fmtp_parameters) {
1652 for (cricket::CodecParameterMap::const_iterator iter = params.begin();
1653 iter != params.end(); ++iter) {
1654 if (IsFmtpParam(iter->first)) {
1655 (*fmtp_parameters)[iter->first] = iter->second;
1656 }
1657 }
1658}
1659
1660template <class T>
1661void AddFmtpLine(const T& codec, std::string* message) {
1662 cricket::CodecParameterMap fmtp_parameters;
1663 GetFmtpParams(codec.params, &fmtp_parameters);
1664 if (fmtp_parameters.empty()) {
1665 // No need to add an fmtp if it will have no (optional) parameters.
1666 return;
1667 }
1668 std::ostringstream os;
1669 WriteFmtpHeader(codec.id, &os);
1670 WriteFmtpParameters(fmtp_parameters, &os);
1671 AddLine(os.str(), message);
1672 return;
1673}
1674
1675template <class T>
1676void AddRtcpFbLines(const T& codec, std::string* message) {
1677 for (std::vector<cricket::FeedbackParam>::const_iterator iter =
1678 codec.feedback_params.params().begin();
1679 iter != codec.feedback_params.params().end(); ++iter) {
1680 std::ostringstream os;
1681 WriteRtcpFbHeader(codec.id, &os);
1682 os << " " << iter->id();
1683 if (!iter->param().empty()) {
1684 os << " " << iter->param();
1685 }
1686 AddLine(os.str(), message);
1687 }
1688}
1689
jiayl@webrtc.orgddb85ab2014-09-05 16:31:56 +00001690bool AddSctpDataCodec(DataContentDescription* media_desc,
1691 int sctp_port) {
solenberg9fa49752016-10-08 13:02:44 -07001692 for (const auto& codec : media_desc->codecs()) {
1693 if (cricket::CodecNamesEq(codec.name, cricket::kGoogleSctpDataCodecName)) {
1694 return ParseFailed("",
1695 "Can't have multiple sctp port attributes.",
1696 NULL);
1697 }
jiayl@webrtc.orgddb85ab2014-09-05 16:31:56 +00001698 }
1699 // Add the SCTP Port number as a pseudo-codec "port" parameter
solenberg9fa49752016-10-08 13:02:44 -07001700 cricket::DataCodec codec_port(cricket::kGoogleSctpDataCodecPlType,
deadbeef67cf2c12016-04-13 10:07:16 -07001701 cricket::kGoogleSctpDataCodecName);
jiayl@webrtc.orgddb85ab2014-09-05 16:31:56 +00001702 codec_port.SetParam(cricket::kCodecParamPort, sctp_port);
Mirko Bonadei675513b2017-11-09 11:09:25 +01001703 RTC_LOG(INFO) << "AddSctpDataCodec: Got SCTP Port Number " << sctp_port;
jiayl@webrtc.orgddb85ab2014-09-05 16:31:56 +00001704 media_desc->AddCodec(codec_port);
1705 return true;
1706}
1707
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001708bool GetMinValue(const std::vector<int>& values, int* value) {
1709 if (values.empty()) {
1710 return false;
1711 }
1712 std::vector<int>::const_iterator found =
1713 std::min_element(values.begin(), values.end());
1714 *value = *found;
1715 return true;
1716}
1717
1718bool GetParameter(const std::string& name,
1719 const cricket::CodecParameterMap& params, int* value) {
1720 std::map<std::string, std::string>::const_iterator found =
1721 params.find(name);
1722 if (found == params.end()) {
1723 return false;
1724 }
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001725 if (!rtc::FromString(found->second, value)) {
wu@webrtc.org5e760e72014-04-02 23:19:09 +00001726 return false;
1727 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001728 return true;
1729}
1730
1731void BuildRtpMap(const MediaContentDescription* media_desc,
1732 const MediaType media_type,
1733 std::string* message) {
nisseede5da42017-01-12 05:15:36 -08001734 RTC_DCHECK(message != NULL);
1735 RTC_DCHECK(media_desc != NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001736 std::ostringstream os;
1737 if (media_type == cricket::MEDIA_TYPE_VIDEO) {
1738 const VideoContentDescription* video_desc =
1739 static_cast<const VideoContentDescription*>(media_desc);
1740 for (std::vector<cricket::VideoCodec>::const_iterator it =
1741 video_desc->codecs().begin();
1742 it != video_desc->codecs().end(); ++it) {
1743 // RFC 4566
1744 // a=rtpmap:<payload type> <encoding name>/<clock rate>
1745 // [/<encodingparameters>]
1746 if (it->id != kWildcardPayloadType) {
1747 InitAttrLine(kAttributeRtpmap, &os);
deadbeefe814a0d2017-02-25 18:15:09 -08001748 os << kSdpDelimiterColon << it->id << " " << it->name << "/"
1749 << cricket::kVideoCodecClockrate;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001750 AddLine(os.str(), message);
1751 }
1752 AddRtcpFbLines(*it, message);
1753 AddFmtpLine(*it, message);
1754 }
1755 } else if (media_type == cricket::MEDIA_TYPE_AUDIO) {
1756 const AudioContentDescription* audio_desc =
1757 static_cast<const AudioContentDescription*>(media_desc);
1758 std::vector<int> ptimes;
1759 std::vector<int> maxptimes;
1760 int max_minptime = 0;
1761 for (std::vector<cricket::AudioCodec>::const_iterator it =
1762 audio_desc->codecs().begin();
1763 it != audio_desc->codecs().end(); ++it) {
nisseede5da42017-01-12 05:15:36 -08001764 RTC_DCHECK(!it->name.empty());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001765 // RFC 4566
1766 // a=rtpmap:<payload type> <encoding name>/<clock rate>
1767 // [/<encodingparameters>]
1768 InitAttrLine(kAttributeRtpmap, &os);
1769 os << kSdpDelimiterColon << it->id << " ";
1770 os << it->name << "/" << it->clockrate;
1771 if (it->channels != 1) {
1772 os << "/" << it->channels;
1773 }
1774 AddLine(os.str(), message);
1775 AddRtcpFbLines(*it, message);
1776 AddFmtpLine(*it, message);
1777 int minptime = 0;
1778 if (GetParameter(kCodecParamMinPTime, it->params, &minptime)) {
1779 max_minptime = std::max(minptime, max_minptime);
1780 }
1781 int ptime;
1782 if (GetParameter(kCodecParamPTime, it->params, &ptime)) {
1783 ptimes.push_back(ptime);
1784 }
1785 int maxptime;
1786 if (GetParameter(kCodecParamMaxPTime, it->params, &maxptime)) {
1787 maxptimes.push_back(maxptime);
1788 }
1789 }
1790 // Populate the maxptime attribute with the smallest maxptime of all codecs
1791 // under the same m-line.
1792 int min_maxptime = INT_MAX;
1793 if (GetMinValue(maxptimes, &min_maxptime)) {
1794 AddAttributeLine(kCodecParamMaxPTime, min_maxptime, message);
1795 }
nisseede5da42017-01-12 05:15:36 -08001796 RTC_DCHECK(min_maxptime > max_minptime);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001797 // Populate the ptime attribute with the smallest ptime or the largest
1798 // minptime, whichever is the largest, for all codecs under the same m-line.
1799 int ptime = INT_MAX;
1800 if (GetMinValue(ptimes, &ptime)) {
1801 ptime = std::min(ptime, min_maxptime);
1802 ptime = std::max(ptime, max_minptime);
1803 AddAttributeLine(kCodecParamPTime, ptime, message);
1804 }
1805 } else if (media_type == cricket::MEDIA_TYPE_DATA) {
1806 const DataContentDescription* data_desc =
1807 static_cast<const DataContentDescription*>(media_desc);
1808 for (std::vector<cricket::DataCodec>::const_iterator it =
1809 data_desc->codecs().begin();
1810 it != data_desc->codecs().end(); ++it) {
1811 // RFC 4566
1812 // a=rtpmap:<payload type> <encoding name>/<clock rate>
1813 // [/<encodingparameters>]
1814 InitAttrLine(kAttributeRtpmap, &os);
1815 os << kSdpDelimiterColon << it->id << " "
1816 << it->name << "/" << it->clockrate;
1817 AddLine(os.str(), message);
1818 }
1819 }
1820}
1821
1822void BuildCandidate(const std::vector<Candidate>& candidates,
honghaiza54a0802015-12-16 18:37:23 -08001823 bool include_ufrag,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001824 std::string* message) {
1825 std::ostringstream os;
1826
1827 for (std::vector<Candidate>::const_iterator it = candidates.begin();
1828 it != candidates.end(); ++it) {
1829 // RFC 5245
1830 // a=candidate:<foundation> <component-id> <transport> <priority>
1831 // <connection-address> <port> typ <candidate-types>
1832 // [raddr <connection-address>] [rport <port>]
1833 // *(SP extension-att-name SP extension-att-value)
1834 std::string type;
1835 // Map the cricket candidate type to "host" / "srflx" / "prflx" / "relay"
1836 if (it->type() == cricket::LOCAL_PORT_TYPE) {
1837 type = kCandidateHost;
1838 } else if (it->type() == cricket::STUN_PORT_TYPE) {
1839 type = kCandidateSrflx;
1840 } else if (it->type() == cricket::RELAY_PORT_TYPE) {
1841 type = kCandidateRelay;
Honghai Zhang7fb69db2016-03-14 11:59:18 -07001842 } else if (it->type() == cricket::PRFLX_PORT_TYPE) {
1843 type = kCandidatePrflx;
1844 // Peer reflexive candidate may be signaled for being removed.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001845 } else {
nissec80e7412017-01-11 05:56:46 -08001846 RTC_NOTREACHED();
Peter Thatcher019087f2015-04-28 09:06:26 -07001847 // Never write out candidates if we don't know the type.
1848 continue;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001849 }
1850
1851 InitAttrLine(kAttributeCandidate, &os);
1852 os << kSdpDelimiterColon
mallinath@webrtc.org2d60c5e2014-08-08 22:29:20 +00001853 << it->foundation() << " "
1854 << it->component() << " "
1855 << it->protocol() << " "
1856 << it->priority() << " "
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001857 << it->address().ipaddr().ToString() << " "
1858 << it->address().PortAsString() << " "
mallinath@webrtc.org2d60c5e2014-08-08 22:29:20 +00001859 << kAttributeCandidateTyp << " "
1860 << type << " ";
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001861
1862 // Related address
1863 if (!it->related_address().IsNil()) {
1864 os << kAttributeCandidateRaddr << " "
1865 << it->related_address().ipaddr().ToString() << " "
1866 << kAttributeCandidateRport << " "
1867 << it->related_address().PortAsString() << " ";
1868 }
1869
mallinath@webrtc.org2d60c5e2014-08-08 22:29:20 +00001870 if (it->protocol() == cricket::TCP_PROTOCOL_NAME) {
mallinath@webrtc.orge999bd02014-08-13 06:05:55 +00001871 os << kTcpCandidateType << " " << it->tcptype() << " ";
mallinath@webrtc.org2d60c5e2014-08-08 22:29:20 +00001872 }
1873
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001874 // Extensions
1875 os << kAttributeCandidateGeneration << " " << it->generation();
honghaiza54a0802015-12-16 18:37:23 -08001876 if (include_ufrag && !it->username().empty()) {
1877 os << " " << kAttributeCandidateUfrag << " " << it->username();
1878 }
honghaiza0c44ea2016-03-23 16:07:48 -07001879 if (it->network_id() > 0) {
1880 os << " " << kAttributeCandidateNetworkId << " " << it->network_id();
1881 }
honghaize1a0c942016-02-16 14:54:56 -08001882 if (it->network_cost() > 0) {
1883 os << " " << kAttributeCandidateNetworkCost << " " << it->network_cost();
1884 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001885
1886 AddLine(os.str(), message);
1887 }
1888}
1889
1890void BuildIceOptions(const std::vector<std::string>& transport_options,
1891 std::string* message) {
1892 if (!transport_options.empty()) {
1893 std::ostringstream os;
1894 InitAttrLine(kAttributeIceOption, &os);
1895 os << kSdpDelimiterColon << transport_options[0];
1896 for (size_t i = 1; i < transport_options.size(); ++i) {
1897 os << kSdpDelimiterSpace << transport_options[i];
1898 }
1899 AddLine(os.str(), message);
1900 }
1901}
1902
pthatcher@webrtc.org3341b402015-02-13 21:14:22 +00001903bool IsRtp(const std::string& protocol) {
1904 return protocol.empty() ||
1905 (protocol.find(cricket::kMediaProtocolRtpPrefix) != std::string::npos);
1906}
1907
1908bool IsDtlsSctp(const std::string& protocol) {
1909 // This intentionally excludes "SCTP" and "SCTP/DTLS".
lally@webrtc.orga7470932015-02-24 20:19:21 +00001910 return protocol.find(cricket::kMediaProtocolDtlsSctp) != std::string::npos;
pthatcher@webrtc.org3341b402015-02-13 21:14:22 +00001911}
1912
zhihuang38989e52017-03-21 11:04:53 -07001913bool ParseConnectionData(const std::string& line,
1914 rtc::SocketAddress* addr,
1915 SdpParseError* error) {
1916 // Parse the line from left to right.
1917 std::string token;
1918 std::string rightpart;
1919 // RFC 4566
1920 // c=<nettype> <addrtype> <connection-address>
1921 // Skip the "c="
1922 if (!rtc::tokenize_first(line, kSdpDelimiterEqual, &token, &rightpart)) {
1923 return ParseFailed(line, "Failed to parse the network type.", error);
1924 }
1925
1926 // Extract and verify the <nettype>
1927 if (!rtc::tokenize_first(rightpart, kSdpDelimiterSpace, &token, &rightpart) ||
1928 token != kConnectionNettype) {
1929 return ParseFailed(line,
1930 "Failed to parse the connection data. The network type "
1931 "is not currently supported.",
1932 error);
1933 }
1934
1935 // Extract the "<addrtype>" and "<connection-address>".
1936 if (!rtc::tokenize_first(rightpart, kSdpDelimiterSpace, &token, &rightpart)) {
1937 return ParseFailed(line, "Failed to parse the address type.", error);
1938 }
1939
1940 // The rightpart part should be the IP address without the slash which is used
1941 // for multicast.
1942 if (rightpart.find('/') != std::string::npos) {
1943 return ParseFailed(line,
1944 "Failed to parse the connection data. Multicast is not "
1945 "currently supported.",
1946 error);
1947 }
1948 addr->SetIP(rightpart);
1949
1950 // Verify that the addrtype matches the type of the parsed address.
1951 if ((addr->family() == AF_INET && token != "IP4") ||
1952 (addr->family() == AF_INET6 && token != "IP6")) {
1953 addr->Clear();
1954 return ParseFailed(
1955 line,
1956 "Failed to parse the connection data. The address type is mismatching.",
1957 error);
1958 }
1959 return true;
1960}
1961
1962bool ParseSessionDescription(const std::string& message,
1963 size_t* pos,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001964 std::string* session_id,
1965 std::string* session_version,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001966 TransportDescription* session_td,
1967 RtpHeaderExtensions* session_extmaps,
zhihuang38989e52017-03-21 11:04:53 -07001968 rtc::SocketAddress* connection_addr,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001969 cricket::SessionDescription* desc,
1970 SdpParseError* error) {
1971 std::string line;
1972
deadbeefc80741f2015-10-22 13:14:45 -07001973 desc->set_msid_supported(false);
1974
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001975 // RFC 4566
1976 // v= (protocol version)
1977 if (!GetLineWithType(message, pos, &line, kLineTypeVersion)) {
1978 return ParseFailedExpectLine(message, *pos, kLineTypeVersion,
1979 std::string(), error);
1980 }
1981 // RFC 4566
1982 // o=<username> <sess-id> <sess-version> <nettype> <addrtype>
1983 // <unicast-address>
1984 if (!GetLineWithType(message, pos, &line, kLineTypeOrigin)) {
1985 return ParseFailedExpectLine(message, *pos, kLineTypeOrigin,
1986 std::string(), error);
1987 }
1988 std::vector<std::string> fields;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001989 rtc::split(line.substr(kLinePrefixLength),
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001990 kSdpDelimiterSpace, &fields);
1991 const size_t expected_fields = 6;
1992 if (fields.size() != expected_fields) {
1993 return ParseFailedExpectFieldNum(line, expected_fields, error);
1994 }
1995 *session_id = fields[1];
1996 *session_version = fields[2];
1997
1998 // RFC 4566
1999 // s= (session name)
2000 if (!GetLineWithType(message, pos, &line, kLineTypeSessionName)) {
2001 return ParseFailedExpectLine(message, *pos, kLineTypeSessionName,
2002 std::string(), error);
2003 }
2004
2005 // Optional lines
2006 // Those are the optional lines, so shouldn't return false if not present.
2007 // RFC 4566
2008 // i=* (session information)
2009 GetLineWithType(message, pos, &line, kLineTypeSessionInfo);
2010
2011 // RFC 4566
2012 // u=* (URI of description)
2013 GetLineWithType(message, pos, &line, kLineTypeSessionUri);
2014
2015 // RFC 4566
2016 // e=* (email address)
2017 GetLineWithType(message, pos, &line, kLineTypeSessionEmail);
2018
2019 // RFC 4566
2020 // p=* (phone number)
2021 GetLineWithType(message, pos, &line, kLineTypeSessionPhone);
2022
2023 // RFC 4566
2024 // c=* (connection information -- not required if included in
2025 // all media)
zhihuang38989e52017-03-21 11:04:53 -07002026 if (GetLineWithType(message, pos, &line, kLineTypeConnection)) {
2027 if (!ParseConnectionData(line, connection_addr, error)) {
2028 return false;
2029 }
2030 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002031
2032 // RFC 4566
2033 // b=* (zero or more bandwidth information lines)
2034 while (GetLineWithType(message, pos, &line, kLineTypeSessionBandwidth)) {
2035 // By pass zero or more b lines.
2036 }
2037
2038 // RFC 4566
2039 // One or more time descriptions ("t=" and "r=" lines; see below)
2040 // t= (time the session is active)
2041 // r=* (zero or more repeat times)
2042 // Ensure there's at least one time description
2043 if (!GetLineWithType(message, pos, &line, kLineTypeTiming)) {
2044 return ParseFailedExpectLine(message, *pos, kLineTypeTiming, std::string(),
2045 error);
2046 }
2047
2048 while (GetLineWithType(message, pos, &line, kLineTypeRepeatTimes)) {
2049 // By pass zero or more r lines.
2050 }
2051
2052 // Go through the rest of the time descriptions
2053 while (GetLineWithType(message, pos, &line, kLineTypeTiming)) {
2054 while (GetLineWithType(message, pos, &line, kLineTypeRepeatTimes)) {
2055 // By pass zero or more r lines.
2056 }
2057 }
2058
2059 // RFC 4566
2060 // z=* (time zone adjustments)
2061 GetLineWithType(message, pos, &line, kLineTypeTimeZone);
2062
2063 // RFC 4566
2064 // k=* (encryption key)
2065 GetLineWithType(message, pos, &line, kLineTypeEncryptionKey);
2066
2067 // RFC 4566
2068 // a=* (zero or more session attribute lines)
2069 while (GetLineWithType(message, pos, &line, kLineTypeAttributes)) {
2070 if (HasAttribute(line, kAttributeGroup)) {
2071 if (!ParseGroupAttribute(line, desc, error)) {
2072 return false;
2073 }
2074 } else if (HasAttribute(line, kAttributeIceUfrag)) {
2075 if (!GetValue(line, kAttributeIceUfrag,
2076 &(session_td->ice_ufrag), error)) {
2077 return false;
2078 }
2079 } else if (HasAttribute(line, kAttributeIcePwd)) {
2080 if (!GetValue(line, kAttributeIcePwd, &(session_td->ice_pwd), error)) {
2081 return false;
2082 }
2083 } else if (HasAttribute(line, kAttributeIceLite)) {
2084 session_td->ice_mode = cricket::ICEMODE_LITE;
2085 } else if (HasAttribute(line, kAttributeIceOption)) {
2086 if (!ParseIceOptions(line, &(session_td->transport_options), error)) {
2087 return false;
2088 }
2089 } else if (HasAttribute(line, kAttributeFingerprint)) {
2090 if (session_td->identity_fingerprint.get()) {
2091 return ParseFailed(
2092 line,
2093 "Can't have multiple fingerprint attributes at the same level.",
2094 error);
2095 }
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002096 rtc::SSLFingerprint* fingerprint = NULL;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002097 if (!ParseFingerprintAttribute(line, &fingerprint, error)) {
2098 return false;
2099 }
2100 session_td->identity_fingerprint.reset(fingerprint);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00002101 } else if (HasAttribute(line, kAttributeSetup)) {
2102 if (!ParseDtlsSetup(line, &(session_td->connection_role), error)) {
2103 return false;
2104 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002105 } else if (HasAttribute(line, kAttributeMsidSemantics)) {
2106 std::string semantics;
2107 if (!GetValue(line, kAttributeMsidSemantics, &semantics, error)) {
2108 return false;
2109 }
deadbeefc80741f2015-10-22 13:14:45 -07002110 desc->set_msid_supported(
2111 CaseInsensitiveFind(semantics, kMediaStreamSemantic));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002112 } else if (HasAttribute(line, kAttributeExtmap)) {
isheriff6f8d6862016-05-26 11:24:55 -07002113 RtpExtension extmap;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002114 if (!ParseExtmap(line, &extmap, error)) {
2115 return false;
2116 }
2117 session_extmaps->push_back(extmap);
2118 }
2119 }
2120
2121 return true;
2122}
2123
2124bool ParseGroupAttribute(const std::string& line,
2125 cricket::SessionDescription* desc,
2126 SdpParseError* error) {
nisseede5da42017-01-12 05:15:36 -08002127 RTC_DCHECK(desc != NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002128
2129 // RFC 5888 and draft-holmberg-mmusic-sdp-bundle-negotiation-00
2130 // a=group:BUNDLE video voice
2131 std::vector<std::string> fields;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002132 rtc::split(line.substr(kLinePrefixLength),
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002133 kSdpDelimiterSpace, &fields);
2134 std::string semantics;
2135 if (!GetValue(fields[0], kAttributeGroup, &semantics, error)) {
2136 return false;
2137 }
2138 cricket::ContentGroup group(semantics);
2139 for (size_t i = 1; i < fields.size(); ++i) {
2140 group.AddContentName(fields[i]);
2141 }
2142 desc->AddGroup(group);
2143 return true;
2144}
2145
2146static bool ParseFingerprintAttribute(const std::string& line,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002147 rtc::SSLFingerprint** fingerprint,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002148 SdpParseError* error) {
2149 if (!IsLineType(line, kLineTypeAttributes) ||
2150 !HasAttribute(line, kAttributeFingerprint)) {
2151 return ParseFailedExpectLine(line, 0, kLineTypeAttributes,
2152 kAttributeFingerprint, error);
2153 }
2154
2155 std::vector<std::string> fields;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002156 rtc::split(line.substr(kLinePrefixLength),
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002157 kSdpDelimiterSpace, &fields);
2158 const size_t expected_fields = 2;
2159 if (fields.size() != expected_fields) {
2160 return ParseFailedExpectFieldNum(line, expected_fields, error);
2161 }
2162
2163 // The first field here is "fingerprint:<hash>.
2164 std::string algorithm;
2165 if (!GetValue(fields[0], kAttributeFingerprint, &algorithm, error)) {
2166 return false;
2167 }
2168
2169 // Downcase the algorithm. Note that we don't need to downcase the
2170 // fingerprint because hex_decode can handle upper-case.
2171 std::transform(algorithm.begin(), algorithm.end(), algorithm.begin(),
2172 ::tolower);
2173
2174 // The second field is the digest value. De-hexify it.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002175 *fingerprint = rtc::SSLFingerprint::CreateFromRfc4572(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002176 algorithm, fields[1]);
2177 if (!*fingerprint) {
2178 return ParseFailed(line,
2179 "Failed to create fingerprint from the digest.",
2180 error);
2181 }
2182
2183 return true;
2184}
2185
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00002186static bool ParseDtlsSetup(const std::string& line,
2187 cricket::ConnectionRole* role,
2188 SdpParseError* error) {
2189 // setup-attr = "a=setup:" role
2190 // role = "active" / "passive" / "actpass" / "holdconn"
2191 std::vector<std::string> fields;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002192 rtc::split(line.substr(kLinePrefixLength), kSdpDelimiterColon, &fields);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00002193 const size_t expected_fields = 2;
2194 if (fields.size() != expected_fields) {
2195 return ParseFailedExpectFieldNum(line, expected_fields, error);
2196 }
2197 std::string role_str = fields[1];
2198 if (!cricket::StringToConnectionRole(role_str, role)) {
2199 return ParseFailed(line, "Invalid attribute value.", error);
2200 }
2201 return true;
2202}
2203
deadbeef9d3584c2016-02-16 17:54:10 -08002204static bool ParseMsidAttribute(const std::string& line,
2205 std::string* stream_id,
2206 std::string* track_id,
2207 SdpParseError* error) {
2208 // draft-ietf-mmusic-msid-11
2209 // a=msid:<stream id> <track id>
2210 // msid-value = msid-id [ SP msid-appdata ]
2211 // msid-id = 1*64token-char ; see RFC 4566
2212 // msid-appdata = 1*64token-char ; see RFC 4566
2213 std::string field1;
2214 if (!rtc::tokenize_first(line.substr(kLinePrefixLength), kSdpDelimiterSpace,
2215 &field1, track_id)) {
2216 const size_t expected_fields = 2;
2217 return ParseFailedExpectFieldNum(line, expected_fields, error);
2218 }
2219
deadbeefa4549d62017-02-10 17:26:22 -08002220 if (track_id->empty()) {
2221 return ParseFailed(line, "Missing track ID in msid attribute.", error);
2222 }
2223
deadbeef9d3584c2016-02-16 17:54:10 -08002224 // msid:<msid-id>
2225 if (!GetValue(field1, kAttributeMsid, stream_id, error)) {
2226 return false;
2227 }
deadbeefa4549d62017-02-10 17:26:22 -08002228 if (stream_id->empty()) {
2229 return ParseFailed(line, "Missing stream ID in msid attribute.", error);
2230 }
deadbeef9d3584c2016-02-16 17:54:10 -08002231 return true;
2232}
2233
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002234// RFC 3551
2235// PT encoding media type clock rate channels
2236// name (Hz)
2237// 0 PCMU A 8,000 1
2238// 1 reserved A
2239// 2 reserved A
2240// 3 GSM A 8,000 1
2241// 4 G723 A 8,000 1
2242// 5 DVI4 A 8,000 1
2243// 6 DVI4 A 16,000 1
2244// 7 LPC A 8,000 1
2245// 8 PCMA A 8,000 1
2246// 9 G722 A 8,000 1
2247// 10 L16 A 44,100 2
2248// 11 L16 A 44,100 1
2249// 12 QCELP A 8,000 1
2250// 13 CN A 8,000 1
2251// 14 MPA A 90,000 (see text)
2252// 15 G728 A 8,000 1
2253// 16 DVI4 A 11,025 1
2254// 17 DVI4 A 22,050 1
2255// 18 G729 A 8,000 1
2256struct StaticPayloadAudioCodec {
2257 const char* name;
2258 int clockrate;
Peter Kasting69558702016-01-12 16:26:35 -08002259 size_t channels;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002260};
2261static const StaticPayloadAudioCodec kStaticPayloadAudioCodecs[] = {
2262 { "PCMU", 8000, 1 },
2263 { "reserved", 0, 0 },
2264 { "reserved", 0, 0 },
2265 { "GSM", 8000, 1 },
2266 { "G723", 8000, 1 },
2267 { "DVI4", 8000, 1 },
2268 { "DVI4", 16000, 1 },
2269 { "LPC", 8000, 1 },
2270 { "PCMA", 8000, 1 },
2271 { "G722", 8000, 1 },
2272 { "L16", 44100, 2 },
2273 { "L16", 44100, 1 },
2274 { "QCELP", 8000, 1 },
2275 { "CN", 8000, 1 },
2276 { "MPA", 90000, 1 },
2277 { "G728", 8000, 1 },
2278 { "DVI4", 11025, 1 },
2279 { "DVI4", 22050, 1 },
2280 { "G729", 8000, 1 },
2281};
2282
2283void MaybeCreateStaticPayloadAudioCodecs(
2284 const std::vector<int>& fmts, AudioContentDescription* media_desc) {
2285 if (!media_desc) {
2286 return;
2287 }
deadbeef67cf2c12016-04-13 10:07:16 -07002288 RTC_DCHECK(media_desc->codecs().empty());
solenberg9fa49752016-10-08 13:02:44 -07002289 for (int payload_type : fmts) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002290 if (!media_desc->HasCodec(payload_type) &&
2291 payload_type >= 0 &&
kjellander3e33bfe2016-06-20 07:04:09 -07002292 static_cast<uint32_t>(payload_type) <
2293 arraysize(kStaticPayloadAudioCodecs)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002294 std::string encoding_name = kStaticPayloadAudioCodecs[payload_type].name;
2295 int clock_rate = kStaticPayloadAudioCodecs[payload_type].clockrate;
Peter Kasting69558702016-01-12 16:26:35 -08002296 size_t channels = kStaticPayloadAudioCodecs[payload_type].channels;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002297 media_desc->AddCodec(cricket::AudioCodec(payload_type, encoding_name,
deadbeef67cf2c12016-04-13 10:07:16 -07002298 clock_rate, 0, channels));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002299 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002300 }
2301}
2302
2303template <class C>
2304static C* ParseContentDescription(const std::string& message,
2305 const MediaType media_type,
2306 int mline_index,
2307 const std::string& protocol,
deadbeef67cf2c12016-04-13 10:07:16 -07002308 const std::vector<int>& payload_types,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002309 size_t* pos,
2310 std::string* content_name,
deadbeef25ed4352016-12-12 18:37:36 -08002311 bool* bundle_only,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002312 TransportDescription* transport,
2313 std::vector<JsepIceCandidate*>* candidates,
2314 webrtc::SdpParseError* error) {
2315 C* media_desc = new C();
2316 switch (media_type) {
2317 case cricket::MEDIA_TYPE_AUDIO:
2318 *content_name = cricket::CN_AUDIO;
2319 break;
2320 case cricket::MEDIA_TYPE_VIDEO:
2321 *content_name = cricket::CN_VIDEO;
2322 break;
2323 case cricket::MEDIA_TYPE_DATA:
2324 *content_name = cricket::CN_DATA;
2325 break;
2326 default:
nissec80e7412017-01-11 05:56:46 -08002327 RTC_NOTREACHED();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002328 break;
2329 }
deadbeef67cf2c12016-04-13 10:07:16 -07002330 if (!ParseContent(message, media_type, mline_index, protocol, payload_types,
deadbeef25ed4352016-12-12 18:37:36 -08002331 pos, content_name, bundle_only, media_desc, transport,
2332 candidates, error)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002333 delete media_desc;
zhihuang38989e52017-03-21 11:04:53 -07002334 return nullptr;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002335 }
2336 // Sort the codecs according to the m-line fmt list.
deadbeef67cf2c12016-04-13 10:07:16 -07002337 std::unordered_map<int, int> payload_type_preferences;
2338 // "size + 1" so that the lowest preference payload type has a preference of
2339 // 1, which is greater than the default (0) for payload types not in the fmt
2340 // list.
2341 int preference = static_cast<int>(payload_types.size() + 1);
2342 for (int pt : payload_types) {
2343 payload_type_preferences[pt] = preference--;
2344 }
2345 std::vector<typename C::CodecType> codecs = media_desc->codecs();
2346 std::sort(codecs.begin(), codecs.end(), [&payload_type_preferences](
2347 const typename C::CodecType& a,
2348 const typename C::CodecType& b) {
2349 return payload_type_preferences[a.id] > payload_type_preferences[b.id];
2350 });
2351 media_desc->set_codecs(codecs);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002352 return media_desc;
2353}
2354
2355bool ParseMediaDescription(const std::string& message,
2356 const TransportDescription& session_td,
2357 const RtpHeaderExtensions& session_extmaps,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002358 size_t* pos,
zhihuang38989e52017-03-21 11:04:53 -07002359 const rtc::SocketAddress& session_connection_addr,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002360 cricket::SessionDescription* desc,
2361 std::vector<JsepIceCandidate*>* candidates,
2362 SdpParseError* error) {
nisseede5da42017-01-12 05:15:36 -08002363 RTC_DCHECK(desc != NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002364 std::string line;
2365 int mline_index = -1;
2366
2367 // Zero or more media descriptions
2368 // RFC 4566
2369 // m=<media> <port> <proto> <fmt>
2370 while (GetLineWithType(message, pos, &line, kLineTypeMedia)) {
2371 ++mline_index;
2372
2373 std::vector<std::string> fields;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002374 rtc::split(line.substr(kLinePrefixLength),
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002375 kSdpDelimiterSpace, &fields);
zstein4b2e0822017-02-17 19:48:38 -08002376
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002377 const size_t expected_min_fields = 4;
2378 if (fields.size() < expected_min_fields) {
2379 return ParseFailedExpectMinFieldNum(line, expected_min_fields, error);
2380 }
deadbeef25ed4352016-12-12 18:37:36 -08002381 bool port_rejected = false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002382 // RFC 3264
2383 // To reject an offered stream, the port number in the corresponding stream
2384 // in the answer MUST be set to zero.
2385 if (fields[1] == kMediaPortRejected) {
deadbeef25ed4352016-12-12 18:37:36 -08002386 port_rejected = true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002387 }
2388
zhihuang38989e52017-03-21 11:04:53 -07002389 int port = 0;
2390 if (!rtc::FromString<int>(fields[1], &port) || !IsValidPort(port)) {
2391 return ParseFailed(line, "The port number is invalid", error);
2392 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002393 std::string protocol = fields[2];
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002394
2395 // <fmt>
deadbeef67cf2c12016-04-13 10:07:16 -07002396 std::vector<int> payload_types;
pthatcher@webrtc.org3341b402015-02-13 21:14:22 +00002397 if (IsRtp(protocol)) {
jiayl@webrtc.orgddb85ab2014-09-05 16:31:56 +00002398 for (size_t j = 3 ; j < fields.size(); ++j) {
2399 // TODO(wu): Remove when below bug is fixed.
2400 // https://bugzilla.mozilla.org/show_bug.cgi?id=996329
pbosbb36fdf2015-07-09 07:48:14 -07002401 if (fields[j].empty() && j == fields.size() - 1) {
jiayl@webrtc.orgddb85ab2014-09-05 16:31:56 +00002402 continue;
2403 }
wu@webrtc.org36eda7c2014-04-15 20:37:30 +00002404
jiayl@webrtc.orgddb85ab2014-09-05 16:31:56 +00002405 int pl = 0;
pkasting@chromium.orge9facf82015-02-17 20:36:28 +00002406 if (!GetPayloadTypeFromString(line, fields[j], &pl, error)) {
jiayl@webrtc.orgddb85ab2014-09-05 16:31:56 +00002407 return false;
2408 }
deadbeef67cf2c12016-04-13 10:07:16 -07002409 payload_types.push_back(pl);
wu@webrtc.org5e760e72014-04-02 23:19:09 +00002410 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002411 }
2412
2413 // Make a temporary TransportDescription based on |session_td|.
2414 // Some of this gets overwritten by ParseContent.
deadbeef46eed762016-01-28 13:24:37 -08002415 TransportDescription transport(
2416 session_td.transport_options, session_td.ice_ufrag, session_td.ice_pwd,
2417 session_td.ice_mode, session_td.connection_role,
2418 session_td.identity_fingerprint.get());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002419
kwibergd1fe2812016-04-27 06:47:29 -07002420 std::unique_ptr<MediaContentDescription> content;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002421 std::string content_name;
deadbeef25ed4352016-12-12 18:37:36 -08002422 bool bundle_only = false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002423 if (HasAttribute(line, kMediaTypeVideo)) {
2424 content.reset(ParseContentDescription<VideoContentDescription>(
deadbeef67cf2c12016-04-13 10:07:16 -07002425 message, cricket::MEDIA_TYPE_VIDEO, mline_index, protocol,
deadbeef25ed4352016-12-12 18:37:36 -08002426 payload_types, pos, &content_name, &bundle_only, &transport,
2427 candidates, error));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002428 } else if (HasAttribute(line, kMediaTypeAudio)) {
2429 content.reset(ParseContentDescription<AudioContentDescription>(
deadbeef67cf2c12016-04-13 10:07:16 -07002430 message, cricket::MEDIA_TYPE_AUDIO, mline_index, protocol,
deadbeef25ed4352016-12-12 18:37:36 -08002431 payload_types, pos, &content_name, &bundle_only, &transport,
2432 candidates, error));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002433 } else if (HasAttribute(line, kMediaTypeData)) {
jiayl@webrtc.org0e527722014-09-08 21:43:43 +00002434 DataContentDescription* data_desc =
wu@webrtc.org78187522013-10-07 23:32:02 +00002435 ParseContentDescription<DataContentDescription>(
deadbeef67cf2c12016-04-13 10:07:16 -07002436 message, cricket::MEDIA_TYPE_DATA, mline_index, protocol,
deadbeef25ed4352016-12-12 18:37:36 -08002437 payload_types, pos, &content_name, &bundle_only, &transport,
2438 candidates, error);
jiayl@webrtc.org0e527722014-09-08 21:43:43 +00002439 content.reset(data_desc);
wu@webrtc.org78187522013-10-07 23:32:02 +00002440
zstein4b2e0822017-02-17 19:48:38 -08002441 if (data_desc && IsDtlsSctp(protocol)) {
2442 int p;
2443 if (rtc::FromString(fields[3], &p)) {
2444 if (!AddSctpDataCodec(data_desc, p)) {
2445 return false;
2446 }
2447 } else if (fields[3] == kDefaultSctpmapProtocol) {
2448 data_desc->set_use_sctpmap(false);
2449 }
wu@webrtc.org78187522013-10-07 23:32:02 +00002450 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002451 } else {
Mirko Bonadei675513b2017-11-09 11:09:25 +01002452 RTC_LOG(LS_WARNING) << "Unsupported media type: " << line;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002453 continue;
2454 }
2455 if (!content.get()) {
2456 // ParseContentDescription returns NULL if failed.
2457 return false;
2458 }
2459
deadbeef25ed4352016-12-12 18:37:36 -08002460 bool content_rejected = false;
deadbeef12771a12017-01-03 13:53:47 -08002461 // A port of 0 is not interpreted as a rejected m= section when it's
2462 // used along with a=bundle-only.
deadbeef25ed4352016-12-12 18:37:36 -08002463 if (bundle_only) {
deadbeef25ed4352016-12-12 18:37:36 -08002464 if (!port_rejected) {
deadbeef12771a12017-01-03 13:53:47 -08002465 // Usage of bundle-only with a nonzero port is unspecified. So just
2466 // ignore bundle-only if we see this.
2467 bundle_only = false;
Mirko Bonadei675513b2017-11-09 11:09:25 +01002468 RTC_LOG(LS_WARNING)
deadbeef12771a12017-01-03 13:53:47 -08002469 << "a=bundle-only attribute observed with a nonzero "
2470 << "port; this usage is unspecified so the attribute is being "
2471 << "ignored.";
deadbeef25ed4352016-12-12 18:37:36 -08002472 }
2473 } else {
2474 // If not using bundle-only, interpret port 0 in the normal way; the m=
2475 // section is being rejected.
2476 content_rejected = port_rejected;
2477 }
2478
pthatcher@webrtc.org3341b402015-02-13 21:14:22 +00002479 if (IsRtp(protocol)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002480 // Set the extmap.
2481 if (!session_extmaps.empty() &&
2482 !content->rtp_header_extensions().empty()) {
2483 return ParseFailed("",
2484 "The a=extmap MUST be either all session level or "
2485 "all media level.",
2486 error);
2487 }
2488 for (size_t i = 0; i < session_extmaps.size(); ++i) {
2489 content->AddRtpHeaderExtension(session_extmaps[i]);
2490 }
2491 }
2492 content->set_protocol(protocol);
zhihuang38989e52017-03-21 11:04:53 -07002493
2494 // Use the session level connection address if the media level addresses are
2495 // not specified.
2496 rtc::SocketAddress address;
2497 address = content->connection_address().IsNil()
2498 ? session_connection_addr
2499 : content->connection_address();
2500 address.SetPort(port);
2501 content->set_connection_address(address);
2502
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002503 desc->AddContent(content_name,
Steve Anton5adfafd2017-12-20 16:34:00 -08002504 IsDtlsSctp(protocol) ? MediaProtocolType::kSctp
2505 : MediaProtocolType::kRtp,
deadbeef25ed4352016-12-12 18:37:36 -08002506 content_rejected, bundle_only, content.release());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002507 // Create TransportInfo with the media level "ice-pwd" and "ice-ufrag".
2508 TransportInfo transport_info(content_name, transport);
2509
2510 if (!desc->AddTransportInfo(transport_info)) {
2511 std::ostringstream description;
2512 description << "Failed to AddTransportInfo with content name: "
2513 << content_name;
2514 return ParseFailed("", description.str(), error);
2515 }
2516 }
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00002517
2518 size_t end_of_message = message.size();
2519 if (mline_index == -1 && *pos != end_of_message) {
2520 ParseFailed(message, *pos, "Expects m line.", error);
2521 return false;
2522 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002523 return true;
2524}
2525
2526bool VerifyCodec(const cricket::Codec& codec) {
2527 // Codec has not been populated correctly unless the name has been set. This
2528 // can happen if an SDP has an fmtp or rtcp-fb with a payload type but doesn't
2529 // have a corresponding "rtpmap" line.
htab39db842016-12-08 01:50:48 -08002530 return !codec.name.empty();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002531}
2532
2533bool VerifyAudioCodecs(const AudioContentDescription* audio_desc) {
2534 const std::vector<cricket::AudioCodec>& codecs = audio_desc->codecs();
2535 for (std::vector<cricket::AudioCodec>::const_iterator iter = codecs.begin();
2536 iter != codecs.end(); ++iter) {
2537 if (!VerifyCodec(*iter)) {
2538 return false;
2539 }
2540 }
2541 return true;
2542}
2543
2544bool VerifyVideoCodecs(const VideoContentDescription* video_desc) {
2545 const std::vector<cricket::VideoCodec>& codecs = video_desc->codecs();
2546 for (std::vector<cricket::VideoCodec>::const_iterator iter = codecs.begin();
2547 iter != codecs.end(); ++iter) {
2548 if (!VerifyCodec(*iter)) {
2549 return false;
2550 }
2551 }
2552 return true;
2553}
2554
2555void AddParameters(const cricket::CodecParameterMap& parameters,
2556 cricket::Codec* codec) {
2557 for (cricket::CodecParameterMap::const_iterator iter =
2558 parameters.begin(); iter != parameters.end(); ++iter) {
2559 codec->SetParam(iter->first, iter->second);
2560 }
2561}
2562
2563void AddFeedbackParameter(const cricket::FeedbackParam& feedback_param,
2564 cricket::Codec* codec) {
2565 codec->AddFeedbackParam(feedback_param);
2566}
2567
2568void AddFeedbackParameters(const cricket::FeedbackParams& feedback_params,
2569 cricket::Codec* codec) {
2570 for (std::vector<cricket::FeedbackParam>::const_iterator iter =
2571 feedback_params.params().begin();
2572 iter != feedback_params.params().end(); ++iter) {
2573 codec->AddFeedbackParam(*iter);
2574 }
2575}
2576
2577// Gets the current codec setting associated with |payload_type|. If there
changbin.shao@webrtc.org2d25b442015-03-16 04:14:34 +00002578// is no Codec associated with that payload type it returns an empty codec
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002579// with that payload type.
2580template <class T>
changbin.shao@webrtc.org2d25b442015-03-16 04:14:34 +00002581T GetCodecWithPayloadType(const std::vector<T>& codecs, int payload_type) {
magjedb05fa242016-11-11 04:00:16 -08002582 const T* codec = FindCodecById(codecs, payload_type);
2583 if (codec)
2584 return *codec;
2585 // Return empty codec with |payload_type|.
changbin.shao@webrtc.org2d25b442015-03-16 04:14:34 +00002586 T ret_val;
magjedb05fa242016-11-11 04:00:16 -08002587 ret_val.id = payload_type;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002588 return ret_val;
2589}
2590
2591// Updates or creates a new codec entry in the audio description.
2592template <class T, class U>
2593void AddOrReplaceCodec(MediaContentDescription* content_desc, const U& codec) {
2594 T* desc = static_cast<T*>(content_desc);
2595 std::vector<U> codecs = desc->codecs();
2596 bool found = false;
2597
2598 typename std::vector<U>::iterator iter;
2599 for (iter = codecs.begin(); iter != codecs.end(); ++iter) {
2600 if (iter->id == codec.id) {
2601 *iter = codec;
2602 found = true;
2603 break;
2604 }
2605 }
2606 if (!found) {
2607 desc->AddCodec(codec);
2608 return;
2609 }
2610 desc->set_codecs(codecs);
2611}
2612
2613// Adds or updates existing codec corresponding to |payload_type| according
2614// to |parameters|.
2615template <class T, class U>
2616void UpdateCodec(MediaContentDescription* content_desc, int payload_type,
2617 const cricket::CodecParameterMap& parameters) {
2618 // Codec might already have been populated (from rtpmap).
changbin.shao@webrtc.org2d25b442015-03-16 04:14:34 +00002619 U new_codec = GetCodecWithPayloadType(static_cast<T*>(content_desc)->codecs(),
2620 payload_type);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002621 AddParameters(parameters, &new_codec);
2622 AddOrReplaceCodec<T, U>(content_desc, new_codec);
2623}
2624
2625// Adds or updates existing codec corresponding to |payload_type| according
2626// to |feedback_param|.
2627template <class T, class U>
2628void UpdateCodec(MediaContentDescription* content_desc, int payload_type,
2629 const cricket::FeedbackParam& feedback_param) {
2630 // Codec might already have been populated (from rtpmap).
changbin.shao@webrtc.org2d25b442015-03-16 04:14:34 +00002631 U new_codec = GetCodecWithPayloadType(static_cast<T*>(content_desc)->codecs(),
2632 payload_type);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002633 AddFeedbackParameter(feedback_param, &new_codec);
2634 AddOrReplaceCodec<T, U>(content_desc, new_codec);
2635}
2636
jlmiller@webrtc.orga744a282015-02-18 21:37:46 +00002637template <class T>
2638bool PopWildcardCodec(std::vector<T>* codecs, T* wildcard_codec) {
2639 for (auto iter = codecs->begin(); iter != codecs->end(); ++iter) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002640 if (iter->id == kWildcardPayloadType) {
2641 *wildcard_codec = *iter;
2642 codecs->erase(iter);
2643 return true;
2644 }
2645 }
2646 return false;
2647}
2648
jlmiller@webrtc.orga744a282015-02-18 21:37:46 +00002649template<class T>
2650void UpdateFromWildcardCodecs(cricket::MediaContentDescriptionImpl<T>* desc) {
2651 auto codecs = desc->codecs();
2652 T wildcard_codec;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002653 if (!PopWildcardCodec(&codecs, &wildcard_codec)) {
2654 return;
2655 }
jlmiller@webrtc.orga744a282015-02-18 21:37:46 +00002656 for (auto& codec : codecs) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002657 AddFeedbackParameters(wildcard_codec.feedback_params, &codec);
2658 }
jlmiller@webrtc.orga744a282015-02-18 21:37:46 +00002659 desc->set_codecs(codecs);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002660}
2661
2662void AddAudioAttribute(const std::string& name, const std::string& value,
2663 AudioContentDescription* audio_desc) {
2664 if (value.empty()) {
2665 return;
2666 }
2667 std::vector<cricket::AudioCodec> codecs = audio_desc->codecs();
2668 for (std::vector<cricket::AudioCodec>::iterator iter = codecs.begin();
2669 iter != codecs.end(); ++iter) {
2670 iter->params[name] = value;
2671 }
2672 audio_desc->set_codecs(codecs);
2673}
2674
2675bool ParseContent(const std::string& message,
2676 const MediaType media_type,
2677 int mline_index,
2678 const std::string& protocol,
deadbeef67cf2c12016-04-13 10:07:16 -07002679 const std::vector<int>& payload_types,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002680 size_t* pos,
2681 std::string* content_name,
deadbeef25ed4352016-12-12 18:37:36 -08002682 bool* bundle_only,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002683 MediaContentDescription* media_desc,
2684 TransportDescription* transport,
2685 std::vector<JsepIceCandidate*>* candidates,
2686 SdpParseError* error) {
nisseede5da42017-01-12 05:15:36 -08002687 RTC_DCHECK(media_desc != NULL);
2688 RTC_DCHECK(content_name != NULL);
2689 RTC_DCHECK(transport != NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002690
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +00002691 if (media_type == cricket::MEDIA_TYPE_AUDIO) {
2692 MaybeCreateStaticPayloadAudioCodecs(
deadbeef67cf2c12016-04-13 10:07:16 -07002693 payload_types, static_cast<AudioContentDescription*>(media_desc));
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +00002694 }
2695
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002696 // The media level "ice-ufrag" and "ice-pwd".
2697 // The candidates before update the media level "ice-pwd" and "ice-ufrag".
2698 Candidates candidates_orig;
2699 std::string line;
2700 std::string mline_id;
2701 // Tracks created out of the ssrc attributes.
2702 StreamParamsVec tracks;
2703 SsrcInfoVec ssrc_infos;
2704 SsrcGroupVec ssrc_groups;
2705 std::string maxptime_as_string;
2706 std::string ptime_as_string;
deadbeef9d3584c2016-02-16 17:54:10 -08002707 std::string stream_id;
2708 std::string track_id;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002709
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002710 // Loop until the next m line
2711 while (!IsLineType(message, kLineTypeMedia, *pos)) {
2712 if (!GetLine(message, pos, &line)) {
2713 if (*pos >= message.size()) {
2714 break; // Done parsing
2715 } else {
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +00002716 return ParseFailed(message, *pos, "Invalid SDP line.", error);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002717 }
2718 }
2719
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002720 // RFC 4566
2721 // b=* (zero or more bandwidth information lines)
2722 if (IsLineType(line, kLineTypeSessionBandwidth)) {
2723 std::string bandwidth;
2724 if (HasAttribute(line, kApplicationSpecificMaximum)) {
2725 if (!GetValue(line, kApplicationSpecificMaximum, &bandwidth, error)) {
2726 return false;
2727 } else {
wu@webrtc.org5e760e72014-04-02 23:19:09 +00002728 int b = 0;
2729 if (!GetValueFromString(line, bandwidth, &b, error)) {
2730 return false;
2731 }
deadbeef3e8016e2017-08-03 17:49:30 -07002732 // TODO(deadbeef): Historically, applications may be setting a value
2733 // of -1 to mean "unset any previously set bandwidth limit", even
2734 // though ommitting the "b=AS" entirely will do just that. Once we've
2735 // transitioned applications to doing the right thing, it would be
2736 // better to treat this as a hard error instead of just ignoring it.
2737 if (b == -1) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01002738 RTC_LOG(LS_WARNING)
2739 << "Ignoring \"b=AS:-1\"; will be treated as \"no "
2740 "bandwidth limit\".";
deadbeef3e8016e2017-08-03 17:49:30 -07002741 continue;
2742 }
deadbeefbc88c6b2017-08-02 11:26:34 -07002743 if (b < 0) {
2744 return ParseFailed(line, "b=AS value can't be negative.", error);
2745 }
Peter Thatcherc0c3a862015-06-24 15:31:25 -07002746 // We should never use more than the default bandwidth for RTP-based
2747 // data channels. Don't allow SDP to set the bandwidth, because
2748 // that would give JS the opportunity to "break the Internet".
2749 // See: https://code.google.com/p/chromium/issues/detail?id=280726
2750 if (media_type == cricket::MEDIA_TYPE_DATA && IsRtp(protocol) &&
2751 b > cricket::kDataMaxBandwidth / 1000) {
2752 std::ostringstream description;
2753 description << "RTP-based data channels may not send more than "
2754 << cricket::kDataMaxBandwidth / 1000 << "kbps.";
2755 return ParseFailed(line, description.str(), error);
2756 }
deadbeefb2362572016-12-13 16:37:06 -08002757 // Prevent integer overflow.
2758 b = std::min(b, INT_MAX / 1000);
wu@webrtc.org5e760e72014-04-02 23:19:09 +00002759 media_desc->set_bandwidth(b * 1000);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002760 }
2761 }
2762 continue;
2763 }
2764
zhihuang38989e52017-03-21 11:04:53 -07002765 // Parse the media level connection data.
2766 if (IsLineType(line, kLineTypeConnection)) {
2767 rtc::SocketAddress addr;
2768 if (!ParseConnectionData(line, &addr, error)) {
2769 return false;
2770 }
2771 media_desc->set_connection_address(addr);
2772 continue;
2773 }
2774
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002775 if (!IsLineType(line, kLineTypeAttributes)) {
Steve Anton36b29d12017-10-30 09:57:42 -07002776 // TODO(deadbeef): Handle other lines if needed.
Mirko Bonadei675513b2017-11-09 11:09:25 +01002777 RTC_LOG(LS_INFO) << "Ignored line: " << line;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002778 continue;
2779 }
2780
2781 // Handle attributes common to SCTP and RTP.
2782 if (HasAttribute(line, kAttributeMid)) {
2783 // RFC 3388
2784 // mid-attribute = "a=mid:" identification-tag
2785 // identification-tag = token
2786 // Use the mid identification-tag as the content name.
2787 if (!GetValue(line, kAttributeMid, &mline_id, error)) {
2788 return false;
2789 }
2790 *content_name = mline_id;
deadbeef25ed4352016-12-12 18:37:36 -08002791 } else if (HasAttribute(line, kAttributeBundleOnly)) {
2792 *bundle_only = true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002793 } else if (HasAttribute(line, kAttributeCandidate)) {
2794 Candidate candidate;
2795 if (!ParseCandidate(line, &candidate, error, false)) {
2796 return false;
2797 }
deadbeef7bcdb692017-01-20 12:43:58 -08002798 // ParseCandidate will parse non-standard ufrag and password attributes,
2799 // since it's used for candidate trickling, but we only want to process
2800 // the "a=ice-ufrag"/"a=ice-pwd" values in a session description, so
2801 // strip them off at this point.
2802 candidate.set_username(std::string());
2803 candidate.set_password(std::string());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002804 candidates_orig.push_back(candidate);
2805 } else if (HasAttribute(line, kAttributeIceUfrag)) {
2806 if (!GetValue(line, kAttributeIceUfrag, &transport->ice_ufrag, error)) {
2807 return false;
2808 }
2809 } else if (HasAttribute(line, kAttributeIcePwd)) {
2810 if (!GetValue(line, kAttributeIcePwd, &transport->ice_pwd, error)) {
2811 return false;
2812 }
2813 } else if (HasAttribute(line, kAttributeIceOption)) {
2814 if (!ParseIceOptions(line, &transport->transport_options, error)) {
2815 return false;
2816 }
2817 } else if (HasAttribute(line, kAttributeFmtp)) {
2818 if (!ParseFmtpAttributes(line, media_type, media_desc, error)) {
2819 return false;
2820 }
2821 } else if (HasAttribute(line, kAttributeFingerprint)) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002822 rtc::SSLFingerprint* fingerprint = NULL;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002823
2824 if (!ParseFingerprintAttribute(line, &fingerprint, error)) {
2825 return false;
2826 }
2827 transport->identity_fingerprint.reset(fingerprint);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00002828 } else if (HasAttribute(line, kAttributeSetup)) {
2829 if (!ParseDtlsSetup(line, &(transport->connection_role), error)) {
2830 return false;
2831 }
pthatcher@webrtc.org3341b402015-02-13 21:14:22 +00002832 } else if (IsDtlsSctp(protocol) && HasAttribute(line, kAttributeSctpPort)) {
deadbeef7e146cb2016-09-28 10:04:34 -07002833 if (media_type != cricket::MEDIA_TYPE_DATA) {
2834 return ParseFailed(
2835 line, "sctp-port attribute found in non-data media description.",
2836 error);
2837 }
jiayl@webrtc.orgddb85ab2014-09-05 16:31:56 +00002838 int sctp_port;
2839 if (!ParseSctpPort(line, &sctp_port, error)) {
2840 return false;
2841 }
2842 if (!AddSctpDataCodec(static_cast<DataContentDescription*>(media_desc),
2843 sctp_port)) {
2844 return false;
2845 }
pthatcher@webrtc.org3341b402015-02-13 21:14:22 +00002846 } else if (IsRtp(protocol)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002847 //
2848 // RTP specific attrubtes
2849 //
2850 if (HasAttribute(line, kAttributeRtcpMux)) {
2851 media_desc->set_rtcp_mux(true);
deadbeef13871492015-12-09 12:37:51 -08002852 } else if (HasAttribute(line, kAttributeRtcpReducedSize)) {
2853 media_desc->set_rtcp_reduced_size(true);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002854 } else if (HasAttribute(line, kAttributeSsrcGroup)) {
2855 if (!ParseSsrcGroupAttribute(line, &ssrc_groups, error)) {
2856 return false;
2857 }
2858 } else if (HasAttribute(line, kAttributeSsrc)) {
2859 if (!ParseSsrcAttribute(line, &ssrc_infos, error)) {
2860 return false;
2861 }
2862 } else if (HasAttribute(line, kAttributeCrypto)) {
2863 if (!ParseCryptoAttribute(line, media_desc, error)) {
2864 return false;
2865 }
2866 } else if (HasAttribute(line, kAttributeRtpmap)) {
deadbeef67cf2c12016-04-13 10:07:16 -07002867 if (!ParseRtpmapAttribute(line, media_type, payload_types, media_desc,
2868 error)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002869 return false;
2870 }
2871 } else if (HasAttribute(line, kCodecParamMaxPTime)) {
2872 if (!GetValue(line, kCodecParamMaxPTime, &maxptime_as_string, error)) {
2873 return false;
2874 }
2875 } else if (HasAttribute(line, kAttributeRtcpFb)) {
2876 if (!ParseRtcpFbAttribute(line, media_type, media_desc, error)) {
2877 return false;
2878 }
2879 } else if (HasAttribute(line, kCodecParamPTime)) {
2880 if (!GetValue(line, kCodecParamPTime, &ptime_as_string, error)) {
2881 return false;
2882 }
2883 } else if (HasAttribute(line, kAttributeSendOnly)) {
Steve Anton4e70a722017-11-28 14:57:10 -08002884 media_desc->set_direction(RtpTransceiverDirection::kSendOnly);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002885 } else if (HasAttribute(line, kAttributeRecvOnly)) {
Steve Anton4e70a722017-11-28 14:57:10 -08002886 media_desc->set_direction(RtpTransceiverDirection::kRecvOnly);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002887 } else if (HasAttribute(line, kAttributeInactive)) {
Steve Anton4e70a722017-11-28 14:57:10 -08002888 media_desc->set_direction(RtpTransceiverDirection::kInactive);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002889 } else if (HasAttribute(line, kAttributeSendRecv)) {
Steve Anton4e70a722017-11-28 14:57:10 -08002890 media_desc->set_direction(RtpTransceiverDirection::kSendRecv);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002891 } else if (HasAttribute(line, kAttributeExtmap)) {
isheriff6f8d6862016-05-26 11:24:55 -07002892 RtpExtension extmap;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002893 if (!ParseExtmap(line, &extmap, error)) {
2894 return false;
2895 }
2896 media_desc->AddRtpHeaderExtension(extmap);
2897 } else if (HasAttribute(line, kAttributeXGoogleFlag)) {
2898 // Experimental attribute. Conference mode activates more aggressive
2899 // AEC and NS settings.
Steve Anton36b29d12017-10-30 09:57:42 -07002900 // TODO(deadbeef): expose API to set these directly.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002901 std::string flag_value;
2902 if (!GetValue(line, kAttributeXGoogleFlag, &flag_value, error)) {
2903 return false;
2904 }
2905 if (flag_value.compare(kValueConference) == 0)
2906 media_desc->set_conference_mode(true);
deadbeef9d3584c2016-02-16 17:54:10 -08002907 } else if (HasAttribute(line, kAttributeMsid)) {
2908 if (!ParseMsidAttribute(line, &stream_id, &track_id, error)) {
2909 return false;
2910 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002911 }
2912 } else {
2913 // Only parse lines that we are interested of.
Mirko Bonadei675513b2017-11-09 11:09:25 +01002914 RTC_LOG(LS_INFO) << "Ignored line: " << line;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002915 continue;
2916 }
2917 }
2918
2919 // Create tracks from the |ssrc_infos|.
Taylor Brandstetter5de6b752016-03-09 17:02:30 -08002920 // If the stream_id/track_id for all SSRCS are identical, one StreamParams
2921 // will be created in CreateTracksFromSsrcInfos, containing all the SSRCs from
2922 // the m= section.
2923 CreateTracksFromSsrcInfos(ssrc_infos, stream_id, track_id, &tracks);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002924
2925 // Add the ssrc group to the track.
2926 for (SsrcGroupVec::iterator ssrc_group = ssrc_groups.begin();
2927 ssrc_group != ssrc_groups.end(); ++ssrc_group) {
2928 if (ssrc_group->ssrcs.empty()) {
2929 continue;
2930 }
Peter Boström0c4e06b2015-10-07 12:23:21 +02002931 uint32_t ssrc = ssrc_group->ssrcs.front();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002932 for (StreamParamsVec::iterator track = tracks.begin();
2933 track != tracks.end(); ++track) {
2934 if (track->has_ssrc(ssrc)) {
2935 track->ssrc_groups.push_back(*ssrc_group);
2936 }
2937 }
2938 }
2939
2940 // Add the new tracks to the |media_desc|.
deadbeef9d3584c2016-02-16 17:54:10 -08002941 for (StreamParams& track : tracks) {
2942 media_desc->AddStream(track);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002943 }
2944
2945 if (media_type == cricket::MEDIA_TYPE_AUDIO) {
2946 AudioContentDescription* audio_desc =
2947 static_cast<AudioContentDescription*>(media_desc);
jlmiller@webrtc.orga744a282015-02-18 21:37:46 +00002948 UpdateFromWildcardCodecs(audio_desc);
2949
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002950 // Verify audio codec ensures that no audio codec has been populated with
2951 // only fmtp.
2952 if (!VerifyAudioCodecs(audio_desc)) {
2953 return ParseFailed("Failed to parse audio codecs correctly.", error);
2954 }
2955 AddAudioAttribute(kCodecParamMaxPTime, maxptime_as_string, audio_desc);
2956 AddAudioAttribute(kCodecParamPTime, ptime_as_string, audio_desc);
2957 }
2958
2959 if (media_type == cricket::MEDIA_TYPE_VIDEO) {
jlmiller@webrtc.orga744a282015-02-18 21:37:46 +00002960 VideoContentDescription* video_desc =
2961 static_cast<VideoContentDescription*>(media_desc);
2962 UpdateFromWildcardCodecs(video_desc);
2963 // Verify video codec ensures that no video codec has been populated with
2964 // only rtcp-fb.
2965 if (!VerifyVideoCodecs(video_desc)) {
2966 return ParseFailed("Failed to parse video codecs correctly.", error);
2967 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002968 }
2969
2970 // RFC 5245
2971 // Update the candidates with the media level "ice-pwd" and "ice-ufrag".
2972 for (Candidates::iterator it = candidates_orig.begin();
2973 it != candidates_orig.end(); ++it) {
nisseede5da42017-01-12 05:15:36 -08002974 RTC_DCHECK((*it).username().empty() ||
2975 (*it).username() == transport->ice_ufrag);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002976 (*it).set_username(transport->ice_ufrag);
nisseede5da42017-01-12 05:15:36 -08002977 RTC_DCHECK((*it).password().empty());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002978 (*it).set_password(transport->ice_pwd);
2979 candidates->push_back(
2980 new JsepIceCandidate(mline_id, mline_index, *it));
2981 }
2982 return true;
2983}
2984
2985bool ParseSsrcAttribute(const std::string& line, SsrcInfoVec* ssrc_infos,
2986 SdpParseError* error) {
nisseede5da42017-01-12 05:15:36 -08002987 RTC_DCHECK(ssrc_infos != NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002988 // RFC 5576
2989 // a=ssrc:<ssrc-id> <attribute>
2990 // a=ssrc:<ssrc-id> <attribute>:<value>
2991 std::string field1, field2;
Donald Curtis144d0182015-05-15 13:14:24 -07002992 if (!rtc::tokenize_first(line.substr(kLinePrefixLength), kSdpDelimiterSpace,
2993 &field1, &field2)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002994 const size_t expected_fields = 2;
2995 return ParseFailedExpectFieldNum(line, expected_fields, error);
2996 }
2997
2998 // ssrc:<ssrc-id>
2999 std::string ssrc_id_s;
3000 if (!GetValue(field1, kAttributeSsrc, &ssrc_id_s, error)) {
3001 return false;
3002 }
Peter Boström0c4e06b2015-10-07 12:23:21 +02003003 uint32_t ssrc_id = 0;
wu@webrtc.org5e760e72014-04-02 23:19:09 +00003004 if (!GetValueFromString(line, ssrc_id_s, &ssrc_id, error)) {
3005 return false;
3006 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003007
3008 std::string attribute;
3009 std::string value;
Donald Curtis144d0182015-05-15 13:14:24 -07003010 if (!rtc::tokenize_first(field2, kSdpDelimiterColon, &attribute, &value)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003011 std::ostringstream description;
3012 description << "Failed to get the ssrc attribute value from " << field2
3013 << ". Expected format <attribute>:<value>.";
3014 return ParseFailed(line, description.str(), error);
3015 }
3016
3017 // Check if there's already an item for this |ssrc_id|. Create a new one if
3018 // there isn't.
3019 SsrcInfoVec::iterator ssrc_info = ssrc_infos->begin();
3020 for (; ssrc_info != ssrc_infos->end(); ++ssrc_info) {
3021 if (ssrc_info->ssrc_id == ssrc_id) {
3022 break;
3023 }
3024 }
3025 if (ssrc_info == ssrc_infos->end()) {
3026 SsrcInfo info;
3027 info.ssrc_id = ssrc_id;
3028 ssrc_infos->push_back(info);
3029 ssrc_info = ssrc_infos->end() - 1;
3030 }
3031
3032 // Store the info to the |ssrc_info|.
3033 if (attribute == kSsrcAttributeCname) {
3034 // RFC 5576
3035 // cname:<value>
3036 ssrc_info->cname = value;
3037 } else if (attribute == kSsrcAttributeMsid) {
3038 // draft-alvestrand-mmusic-msid-00
3039 // "msid:" identifier [ " " appdata ]
3040 std::vector<std::string> fields;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00003041 rtc::split(value, kSdpDelimiterSpace, &fields);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003042 if (fields.size() < 1 || fields.size() > 2) {
3043 return ParseFailed(line,
3044 "Expected format \"msid:<identifier>[ <appdata>]\".",
3045 error);
3046 }
deadbeef9d3584c2016-02-16 17:54:10 -08003047 ssrc_info->stream_id = fields[0];
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003048 if (fields.size() == 2) {
deadbeef9d3584c2016-02-16 17:54:10 -08003049 ssrc_info->track_id = fields[1];
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003050 }
3051 } else if (attribute == kSsrcAttributeMslabel) {
3052 // draft-alvestrand-rtcweb-mid-01
3053 // mslabel:<value>
3054 ssrc_info->mslabel = value;
3055 } else if (attribute == kSSrcAttributeLabel) {
3056 // The label isn't defined.
3057 // label:<value>
3058 ssrc_info->label = value;
3059 }
3060 return true;
3061}
3062
3063bool ParseSsrcGroupAttribute(const std::string& line,
3064 SsrcGroupVec* ssrc_groups,
3065 SdpParseError* error) {
nisseede5da42017-01-12 05:15:36 -08003066 RTC_DCHECK(ssrc_groups != NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003067 // RFC 5576
3068 // a=ssrc-group:<semantics> <ssrc-id> ...
3069 std::vector<std::string> fields;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00003070 rtc::split(line.substr(kLinePrefixLength),
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003071 kSdpDelimiterSpace, &fields);
3072 const size_t expected_min_fields = 2;
3073 if (fields.size() < expected_min_fields) {
3074 return ParseFailedExpectMinFieldNum(line, expected_min_fields, error);
3075 }
3076 std::string semantics;
3077 if (!GetValue(fields[0], kAttributeSsrcGroup, &semantics, error)) {
3078 return false;
3079 }
Peter Boström0c4e06b2015-10-07 12:23:21 +02003080 std::vector<uint32_t> ssrcs;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003081 for (size_t i = 1; i < fields.size(); ++i) {
Peter Boström0c4e06b2015-10-07 12:23:21 +02003082 uint32_t ssrc = 0;
wu@webrtc.org5e760e72014-04-02 23:19:09 +00003083 if (!GetValueFromString(line, fields[i], &ssrc, error)) {
3084 return false;
3085 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003086 ssrcs.push_back(ssrc);
3087 }
3088 ssrc_groups->push_back(SsrcGroup(semantics, ssrcs));
3089 return true;
3090}
3091
3092bool ParseCryptoAttribute(const std::string& line,
3093 MediaContentDescription* media_desc,
3094 SdpParseError* error) {
3095 std::vector<std::string> fields;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00003096 rtc::split(line.substr(kLinePrefixLength),
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003097 kSdpDelimiterSpace, &fields);
3098 // RFC 4568
3099 // a=crypto:<tag> <crypto-suite> <key-params> [<session-params>]
3100 const size_t expected_min_fields = 3;
3101 if (fields.size() < expected_min_fields) {
3102 return ParseFailedExpectMinFieldNum(line, expected_min_fields, error);
3103 }
3104 std::string tag_value;
3105 if (!GetValue(fields[0], kAttributeCrypto, &tag_value, error)) {
3106 return false;
3107 }
wu@webrtc.org5e760e72014-04-02 23:19:09 +00003108 int tag = 0;
3109 if (!GetValueFromString(line, tag_value, &tag, error)) {
3110 return false;
3111 }
jbauch083b73f2015-07-16 02:46:32 -07003112 const std::string& crypto_suite = fields[1];
3113 const std::string& key_params = fields[2];
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003114 std::string session_params;
3115 if (fields.size() > 3) {
3116 session_params = fields[3];
3117 }
3118 media_desc->AddCrypto(CryptoParams(tag, crypto_suite, key_params,
3119 session_params));
3120 return true;
3121}
3122
3123// Updates or creates a new codec entry in the audio description with according
deadbeef67cf2c12016-04-13 10:07:16 -07003124// to |name|, |clockrate|, |bitrate|, and |channels|.
3125void UpdateCodec(int payload_type,
3126 const std::string& name,
3127 int clockrate,
3128 int bitrate,
3129 size_t channels,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003130 AudioContentDescription* audio_desc) {
3131 // Codec may already be populated with (only) optional parameters
3132 // (from an fmtp).
changbin.shao@webrtc.org2d25b442015-03-16 04:14:34 +00003133 cricket::AudioCodec codec =
3134 GetCodecWithPayloadType(audio_desc->codecs(), payload_type);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003135 codec.name = name;
3136 codec.clockrate = clockrate;
3137 codec.bitrate = bitrate;
3138 codec.channels = channels;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003139 AddOrReplaceCodec<AudioContentDescription, cricket::AudioCodec>(audio_desc,
3140 codec);
3141}
3142
3143// Updates or creates a new codec entry in the video description according to
deadbeef67cf2c12016-04-13 10:07:16 -07003144// |name|, |width|, |height|, and |framerate|.
3145void UpdateCodec(int payload_type,
3146 const std::string& name,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003147 VideoContentDescription* video_desc) {
3148 // Codec may already be populated with (only) optional parameters
3149 // (from an fmtp).
changbin.shao@webrtc.org2d25b442015-03-16 04:14:34 +00003150 cricket::VideoCodec codec =
3151 GetCodecWithPayloadType(video_desc->codecs(), payload_type);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003152 codec.name = name;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003153 AddOrReplaceCodec<VideoContentDescription, cricket::VideoCodec>(video_desc,
3154 codec);
3155}
3156
3157bool ParseRtpmapAttribute(const std::string& line,
3158 const MediaType media_type,
deadbeef67cf2c12016-04-13 10:07:16 -07003159 const std::vector<int>& payload_types,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003160 MediaContentDescription* media_desc,
3161 SdpParseError* error) {
3162 std::vector<std::string> fields;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00003163 rtc::split(line.substr(kLinePrefixLength),
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003164 kSdpDelimiterSpace, &fields);
3165 // RFC 4566
3166 // a=rtpmap:<payload type> <encoding name>/<clock rate>[/<encodingparameters>]
3167 const size_t expected_min_fields = 2;
3168 if (fields.size() < expected_min_fields) {
3169 return ParseFailedExpectMinFieldNum(line, expected_min_fields, error);
3170 }
3171 std::string payload_type_value;
3172 if (!GetValue(fields[0], kAttributeRtpmap, &payload_type_value, error)) {
3173 return false;
3174 }
wu@webrtc.org5e760e72014-04-02 23:19:09 +00003175 int payload_type = 0;
pkasting@chromium.orge9facf82015-02-17 20:36:28 +00003176 if (!GetPayloadTypeFromString(line, payload_type_value, &payload_type,
3177 error)) {
wu@webrtc.org5e760e72014-04-02 23:19:09 +00003178 return false;
3179 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003180
deadbeef67cf2c12016-04-13 10:07:16 -07003181 if (std::find(payload_types.begin(), payload_types.end(), payload_type) ==
3182 payload_types.end()) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01003183 RTC_LOG(LS_WARNING) << "Ignore rtpmap line that did not appear in the "
3184 << "<fmt> of the m-line: " << line;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003185 return true;
3186 }
jbauch083b73f2015-07-16 02:46:32 -07003187 const std::string& encoder = fields[1];
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003188 std::vector<std::string> codec_params;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00003189 rtc::split(encoder, '/', &codec_params);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003190 // <encoding name>/<clock rate>[/<encodingparameters>]
3191 // 2 mandatory fields
3192 if (codec_params.size() < 2 || codec_params.size() > 3) {
3193 return ParseFailed(line,
3194 "Expected format \"<encoding name>/<clock rate>"
3195 "[/<encodingparameters>]\".",
3196 error);
3197 }
jbauch083b73f2015-07-16 02:46:32 -07003198 const std::string& encoding_name = codec_params[0];
wu@webrtc.org5e760e72014-04-02 23:19:09 +00003199 int clock_rate = 0;
3200 if (!GetValueFromString(line, codec_params[1], &clock_rate, error)) {
3201 return false;
3202 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003203 if (media_type == cricket::MEDIA_TYPE_VIDEO) {
3204 VideoContentDescription* video_desc =
3205 static_cast<VideoContentDescription*>(media_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003206 UpdateCodec(payload_type, encoding_name,
deadbeef67cf2c12016-04-13 10:07:16 -07003207 video_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003208 } else if (media_type == cricket::MEDIA_TYPE_AUDIO) {
3209 // RFC 4566
3210 // For audio streams, <encoding parameters> indicates the number
3211 // of audio channels. This parameter is OPTIONAL and may be
3212 // omitted if the number of channels is one, provided that no
3213 // additional parameters are needed.
Peter Kasting69558702016-01-12 16:26:35 -08003214 size_t channels = 1;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003215 if (codec_params.size() == 3) {
wu@webrtc.org5e760e72014-04-02 23:19:09 +00003216 if (!GetValueFromString(line, codec_params[2], &channels, error)) {
3217 return false;
3218 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003219 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003220 AudioContentDescription* audio_desc =
3221 static_cast<AudioContentDescription*>(media_desc);
ossue1405ad2017-01-23 08:55:48 -08003222 UpdateCodec(payload_type, encoding_name, clock_rate, 0, channels,
deadbeef67cf2c12016-04-13 10:07:16 -07003223 audio_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003224 } else if (media_type == cricket::MEDIA_TYPE_DATA) {
3225 DataContentDescription* data_desc =
3226 static_cast<DataContentDescription*>(media_desc);
deadbeef67cf2c12016-04-13 10:07:16 -07003227 data_desc->AddCodec(cricket::DataCodec(payload_type, encoding_name));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003228 }
3229 return true;
3230}
3231
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003232bool ParseFmtpParam(const std::string& line, std::string* parameter,
3233 std::string* value, SdpParseError* error) {
Donald Curtis0e07f922015-05-15 09:21:23 -07003234 if (!rtc::tokenize_first(line, kSdpDelimiterEqual, parameter, value)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003235 ParseFailed(line, "Unable to parse fmtp parameter. \'=\' missing.", error);
3236 return false;
3237 }
3238 // a=fmtp:<payload_type> <param1>=<value1>; <param2>=<value2>; ...
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003239 return true;
3240}
3241
3242bool ParseFmtpAttributes(const std::string& line, const MediaType media_type,
3243 MediaContentDescription* media_desc,
3244 SdpParseError* error) {
3245 if (media_type != cricket::MEDIA_TYPE_AUDIO &&
3246 media_type != cricket::MEDIA_TYPE_VIDEO) {
3247 return true;
3248 }
Donald Curtis0e07f922015-05-15 09:21:23 -07003249
3250 std::string line_payload;
3251 std::string line_params;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003252
3253 // RFC 5576
3254 // a=fmtp:<format> <format specific parameters>
3255 // At least two fields, whereas the second one is any of the optional
3256 // parameters.
Donald Curtis144d0182015-05-15 13:14:24 -07003257 if (!rtc::tokenize_first(line.substr(kLinePrefixLength), kSdpDelimiterSpace,
3258 &line_payload, &line_params)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003259 ParseFailedExpectMinFieldNum(line, 2, error);
3260 return false;
3261 }
3262
Donald Curtis0e07f922015-05-15 09:21:23 -07003263 // Parse out the payload information.
pkasting@chromium.orgd3245462015-02-23 21:28:22 +00003264 std::string payload_type_str;
Donald Curtis0e07f922015-05-15 09:21:23 -07003265 if (!GetValue(line_payload, kAttributeFmtp, &payload_type_str, error)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003266 return false;
3267 }
3268
Donald Curtis0e07f922015-05-15 09:21:23 -07003269 int payload_type = 0;
Donald Curtis144d0182015-05-15 13:14:24 -07003270 if (!GetPayloadTypeFromString(line_payload, payload_type_str, &payload_type,
3271 error)) {
Donald Curtis0e07f922015-05-15 09:21:23 -07003272 return false;
3273 }
3274
3275 // Parse out format specific parameters.
3276 std::vector<std::string> fields;
3277 rtc::split(line_params, kSdpDelimiterSemicolon, &fields);
3278
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003279 cricket::CodecParameterMap codec_params;
Donald Curtis144d0182015-05-15 13:14:24 -07003280 for (auto& iter : fields) {
Donald Curtis0e07f922015-05-15 09:21:23 -07003281 if (iter.find(kSdpDelimiterEqual) == std::string::npos) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003282 // Only fmtps with equals are currently supported. Other fmtp types
3283 // should be ignored. Unknown fmtps do not constitute an error.
3284 continue;
3285 }
Donald Curtis0e07f922015-05-15 09:21:23 -07003286
3287 std::string name;
3288 std::string value;
3289 if (!ParseFmtpParam(rtc::string_trim(iter), &name, &value, error)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003290 return false;
3291 }
3292 codec_params[name] = value;
3293 }
3294
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003295 if (media_type == cricket::MEDIA_TYPE_AUDIO) {
3296 UpdateCodec<AudioContentDescription, cricket::AudioCodec>(
pkasting@chromium.orgd3245462015-02-23 21:28:22 +00003297 media_desc, payload_type, codec_params);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003298 } else if (media_type == cricket::MEDIA_TYPE_VIDEO) {
3299 UpdateCodec<VideoContentDescription, cricket::VideoCodec>(
pkasting@chromium.orgd3245462015-02-23 21:28:22 +00003300 media_desc, payload_type, codec_params);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003301 }
3302 return true;
3303}
3304
3305bool ParseRtcpFbAttribute(const std::string& line, const MediaType media_type,
3306 MediaContentDescription* media_desc,
3307 SdpParseError* error) {
3308 if (media_type != cricket::MEDIA_TYPE_AUDIO &&
3309 media_type != cricket::MEDIA_TYPE_VIDEO) {
3310 return true;
3311 }
3312 std::vector<std::string> rtcp_fb_fields;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00003313 rtc::split(line.c_str(), kSdpDelimiterSpace, &rtcp_fb_fields);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003314 if (rtcp_fb_fields.size() < 2) {
3315 return ParseFailedGetValue(line, kAttributeRtcpFb, error);
3316 }
3317 std::string payload_type_string;
3318 if (!GetValue(rtcp_fb_fields[0], kAttributeRtcpFb, &payload_type_string,
3319 error)) {
3320 return false;
3321 }
wu@webrtc.org5e760e72014-04-02 23:19:09 +00003322 int payload_type = kWildcardPayloadType;
3323 if (payload_type_string != "*") {
pkasting@chromium.orge9facf82015-02-17 20:36:28 +00003324 if (!GetPayloadTypeFromString(line, payload_type_string, &payload_type,
3325 error)) {
wu@webrtc.org5e760e72014-04-02 23:19:09 +00003326 return false;
3327 }
3328 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003329 std::string id = rtcp_fb_fields[1];
3330 std::string param = "";
3331 for (std::vector<std::string>::iterator iter = rtcp_fb_fields.begin() + 2;
3332 iter != rtcp_fb_fields.end(); ++iter) {
3333 param.append(*iter);
3334 }
3335 const cricket::FeedbackParam feedback_param(id, param);
3336
3337 if (media_type == cricket::MEDIA_TYPE_AUDIO) {
pkasting@chromium.orgd3245462015-02-23 21:28:22 +00003338 UpdateCodec<AudioContentDescription, cricket::AudioCodec>(
3339 media_desc, payload_type, feedback_param);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003340 } else if (media_type == cricket::MEDIA_TYPE_VIDEO) {
pkasting@chromium.orgd3245462015-02-23 21:28:22 +00003341 UpdateCodec<VideoContentDescription, cricket::VideoCodec>(
3342 media_desc, payload_type, feedback_param);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003343 }
3344 return true;
3345}
3346
3347} // namespace webrtc