blob: 63265cdf260b6f5d6571a938197208632cd7b994 [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;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000072using cricket::SsrcGroup;
73using cricket::StreamParams;
74using cricket::StreamParamsVec;
75using cricket::TransportDescription;
76using cricket::TransportInfo;
77using cricket::VideoContentDescription;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000078using rtc::SocketAddress;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000079
henrike@webrtc.org28e20752013-07-10 00:45:36 +000080namespace cricket {
81class SessionDescription;
82}
83
deadbeef90f1e1e2017-02-10 12:35:05 -080084// TODO(deadbeef): Switch to using anonymous namespace rather than declaring
85// everything "static".
henrike@webrtc.org28e20752013-07-10 00:45:36 +000086namespace webrtc {
87
88// Line type
89// RFC 4566
90// An SDP session description consists of a number of lines of text of
91// the form:
92// <type>=<value>
93// where <type> MUST be exactly one case-significant character.
deadbeef9d3584c2016-02-16 17:54:10 -080094static const int kLinePrefixLength = 2; // Length of <type>=
henrike@webrtc.org28e20752013-07-10 00:45:36 +000095static const char kLineTypeVersion = 'v';
96static const char kLineTypeOrigin = 'o';
97static const char kLineTypeSessionName = 's';
98static const char kLineTypeSessionInfo = 'i';
99static const char kLineTypeSessionUri = 'u';
100static const char kLineTypeSessionEmail = 'e';
101static const char kLineTypeSessionPhone = 'p';
102static const char kLineTypeSessionBandwidth = 'b';
103static const char kLineTypeTiming = 't';
104static const char kLineTypeRepeatTimes = 'r';
105static const char kLineTypeTimeZone = 'z';
106static const char kLineTypeEncryptionKey = 'k';
107static const char kLineTypeMedia = 'm';
108static const char kLineTypeConnection = 'c';
109static const char kLineTypeAttributes = 'a';
110
111// Attributes
112static const char kAttributeGroup[] = "group";
113static const char kAttributeMid[] = "mid";
deadbeef9d3584c2016-02-16 17:54:10 -0800114static const char kAttributeMsid[] = "msid";
deadbeef25ed4352016-12-12 18:37:36 -0800115static const char kAttributeBundleOnly[] = "bundle-only";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000116static const char kAttributeRtcpMux[] = "rtcp-mux";
deadbeef13871492015-12-09 12:37:51 -0800117static const char kAttributeRtcpReducedSize[] = "rtcp-rsize";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000118static const char kAttributeSsrc[] = "ssrc";
119static const char kSsrcAttributeCname[] = "cname";
120static const char kAttributeExtmap[] = "extmap";
121// draft-alvestrand-mmusic-msid-01
122// a=msid-semantic: WMS
123static const char kAttributeMsidSemantics[] = "msid-semantic";
124static const char kMediaStreamSemantic[] = "WMS";
125static const char kSsrcAttributeMsid[] = "msid";
126static const char kDefaultMsid[] = "default";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000127static const char kSsrcAttributeMslabel[] = "mslabel";
128static const char kSSrcAttributeLabel[] = "label";
129static const char kAttributeSsrcGroup[] = "ssrc-group";
130static const char kAttributeCrypto[] = "crypto";
131static const char kAttributeCandidate[] = "candidate";
132static const char kAttributeCandidateTyp[] = "typ";
133static const char kAttributeCandidateRaddr[] = "raddr";
134static const char kAttributeCandidateRport[] = "rport";
honghaiza54a0802015-12-16 18:37:23 -0800135static const char kAttributeCandidateUfrag[] = "ufrag";
136static const char kAttributeCandidatePwd[] = "pwd";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000137static const char kAttributeCandidateGeneration[] = "generation";
honghaiza0c44ea2016-03-23 16:07:48 -0700138static const char kAttributeCandidateNetworkId[] = "network-id";
honghaize1a0c942016-02-16 14:54:56 -0800139static const char kAttributeCandidateNetworkCost[] = "network-cost";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000140static const char kAttributeFingerprint[] = "fingerprint";
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +0000141static const char kAttributeSetup[] = "setup";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000142static const char kAttributeFmtp[] = "fmtp";
143static const char kAttributeRtpmap[] = "rtpmap";
wu@webrtc.org78187522013-10-07 23:32:02 +0000144static const char kAttributeSctpmap[] = "sctpmap";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000145static const char kAttributeRtcp[] = "rtcp";
146static const char kAttributeIceUfrag[] = "ice-ufrag";
147static const char kAttributeIcePwd[] = "ice-pwd";
148static const char kAttributeIceLite[] = "ice-lite";
149static const char kAttributeIceOption[] = "ice-options";
150static const char kAttributeSendOnly[] = "sendonly";
151static const char kAttributeRecvOnly[] = "recvonly";
152static const char kAttributeRtcpFb[] = "rtcp-fb";
153static const char kAttributeSendRecv[] = "sendrecv";
154static const char kAttributeInactive[] = "inactive";
jiayl@webrtc.orgddb85ab2014-09-05 16:31:56 +0000155// draft-ietf-mmusic-sctp-sdp-07
156// a=sctp-port
157static const char kAttributeSctpPort[] = "sctp-port";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000158
159// Experimental flags
160static const char kAttributeXGoogleFlag[] = "x-google-flag";
161static const char kValueConference[] = "conference";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000162
163// Candidate
164static const char kCandidateHost[] = "host";
165static const char kCandidateSrflx[] = "srflx";
Honghai Zhang7fb69db2016-03-14 11:59:18 -0700166static const char kCandidatePrflx[] = "prflx";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000167static const char kCandidateRelay[] = "relay";
mallinath@webrtc.org2d60c5e2014-08-08 22:29:20 +0000168static const char kTcpCandidateType[] = "tcptype";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000169
170static const char kSdpDelimiterEqual = '=';
171static const char kSdpDelimiterSpace = ' ';
172static const char kSdpDelimiterColon = ':';
173static const char kSdpDelimiterSemicolon = ';';
174static const char kSdpDelimiterSlash = '/';
175static const char kNewLine = '\n';
176static const char kReturn = '\r';
177static const char kLineBreak[] = "\r\n";
178
Steve Anton36b29d12017-10-30 09:57:42 -0700179// TODO(deadbeef): Generate the Session and Time description
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000180// instead of hardcoding.
181static const char kSessionVersion[] = "v=0";
182// RFC 4566
183static const char kSessionOriginUsername[] = "-";
184static const char kSessionOriginSessionId[] = "0";
185static const char kSessionOriginSessionVersion[] = "0";
186static const char kSessionOriginNettype[] = "IN";
187static const char kSessionOriginAddrtype[] = "IP4";
188static const char kSessionOriginAddress[] = "127.0.0.1";
189static const char kSessionName[] = "s=-";
190static const char kTimeDescription[] = "t=0 0";
191static const char kAttrGroup[] = "a=group:BUNDLE";
192static const char kConnectionNettype[] = "IN";
pthatcher@webrtc.orgc9d6d142014-10-23 23:37:22 +0000193static const char kConnectionIpv4Addrtype[] = "IP4";
194static const char kConnectionIpv6Addrtype[] = "IP6";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000195static const char kMediaTypeVideo[] = "video";
196static const char kMediaTypeAudio[] = "audio";
197static const char kMediaTypeData[] = "application";
198static const char kMediaPortRejected[] = "0";
pthatcher@webrtc.orgc9d6d142014-10-23 23:37:22 +0000199// draft-ietf-mmusic-trickle-ice-01
200// When no candidates have been gathered, set the connection
201// address to IP6 ::.
perkj@webrtc.orgd105cc82014-11-07 11:22:06 +0000202// TODO(perkj): FF can not parse IP6 ::. See http://crbug/430333
203// Use IPV4 per default.
204static const char kDummyAddress[] = "0.0.0.0";
pthatcher@webrtc.orgc9d6d142014-10-23 23:37:22 +0000205static const char kDummyPort[] = "9";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000206// RFC 3556
207static const char kApplicationSpecificMaximum[] = "AS";
208
wu@webrtc.org78187522013-10-07 23:32:02 +0000209static const char kDefaultSctpmapProtocol[] = "webrtc-datachannel";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000210
pkasting@chromium.orgd3245462015-02-23 21:28:22 +0000211// RTP payload type is in the 0-127 range. Use -1 to indicate "all" payload
212// types.
213const int kWildcardPayloadType = -1;
214
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000215struct SsrcInfo {
Peter Boström0c4e06b2015-10-07 12:23:21 +0200216 uint32_t ssrc_id;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000217 std::string cname;
deadbeef9d3584c2016-02-16 17:54:10 -0800218 std::string stream_id;
219 std::string track_id;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000220
221 // For backward compatibility.
222 // TODO(ronghuawu): Remove below 2 fields once all the clients support msid.
223 std::string label;
224 std::string mslabel;
225};
226typedef std::vector<SsrcInfo> SsrcInfoVec;
227typedef std::vector<SsrcGroup> SsrcGroupVec;
228
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000229template <class T>
230static void AddFmtpLine(const T& codec, std::string* message);
231static void BuildMediaDescription(const ContentInfo* content_info,
232 const TransportInfo* transport_info,
233 const MediaType media_type,
wu@webrtc.org4c3e9912014-07-16 21:03:13 +0000234 const std::vector<Candidate>& candidates,
deadbeef9d3584c2016-02-16 17:54:10 -0800235 bool unified_plan_sdp,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000236 std::string* message);
zstein4b2e0822017-02-17 19:48:38 -0800237static void BuildSctpContentAttributes(std::string* message,
238 int sctp_port,
239 bool use_sctpmap);
deadbeef9d3584c2016-02-16 17:54:10 -0800240static void BuildRtpContentAttributes(const MediaContentDescription* media_desc,
241 const MediaType media_type,
242 bool unified_plan_sdp,
243 std::string* message);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000244static void BuildRtpMap(const MediaContentDescription* media_desc,
245 const MediaType media_type,
246 std::string* message);
247static void BuildCandidate(const std::vector<Candidate>& candidates,
honghaiza54a0802015-12-16 18:37:23 -0800248 bool include_ufrag,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000249 std::string* message);
250static void BuildIceOptions(const std::vector<std::string>& transport_options,
251 std::string* message);
pthatcher@webrtc.org3341b402015-02-13 21:14:22 +0000252static bool IsRtp(const std::string& protocol);
253static bool IsDtlsSctp(const std::string& protocol);
zhihuang38989e52017-03-21 11:04:53 -0700254static bool ParseSessionDescription(const std::string& message,
255 size_t* pos,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000256 std::string* session_id,
257 std::string* session_version,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000258 TransportDescription* session_td,
259 RtpHeaderExtensions* session_extmaps,
zhihuang38989e52017-03-21 11:04:53 -0700260 rtc::SocketAddress* connection_addr,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000261 cricket::SessionDescription* desc,
262 SdpParseError* error);
263static bool ParseGroupAttribute(const std::string& line,
264 cricket::SessionDescription* desc,
265 SdpParseError* error);
266static bool ParseMediaDescription(
267 const std::string& message,
268 const TransportDescription& session_td,
269 const RtpHeaderExtensions& session_extmaps,
zhihuang38989e52017-03-21 11:04:53 -0700270 size_t* pos,
271 const rtc::SocketAddress& session_connection_addr,
272 cricket::SessionDescription* desc,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000273 std::vector<JsepIceCandidate*>* candidates,
274 SdpParseError* error);
275static bool ParseContent(const std::string& message,
276 const MediaType media_type,
277 int mline_index,
278 const std::string& protocol,
deadbeef67cf2c12016-04-13 10:07:16 -0700279 const std::vector<int>& payload_types,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000280 size_t* pos,
281 std::string* content_name,
deadbeef25ed4352016-12-12 18:37:36 -0800282 bool* bundle_only,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000283 MediaContentDescription* media_desc,
284 TransportDescription* transport,
285 std::vector<JsepIceCandidate*>* candidates,
286 SdpParseError* error);
287static bool ParseSsrcAttribute(const std::string& line,
288 SsrcInfoVec* ssrc_infos,
289 SdpParseError* error);
290static bool ParseSsrcGroupAttribute(const std::string& line,
291 SsrcGroupVec* ssrc_groups,
292 SdpParseError* error);
293static bool ParseCryptoAttribute(const std::string& line,
294 MediaContentDescription* media_desc,
295 SdpParseError* error);
296static bool ParseRtpmapAttribute(const std::string& line,
297 const MediaType media_type,
deadbeef67cf2c12016-04-13 10:07:16 -0700298 const std::vector<int>& payload_types,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000299 MediaContentDescription* media_desc,
300 SdpParseError* error);
301static bool ParseFmtpAttributes(const std::string& line,
302 const MediaType media_type,
303 MediaContentDescription* media_desc,
304 SdpParseError* error);
305static bool ParseFmtpParam(const std::string& line, std::string* parameter,
306 std::string* value, SdpParseError* error);
307static bool ParseCandidate(const std::string& message, Candidate* candidate,
308 SdpParseError* error, bool is_raw);
309static bool ParseRtcpFbAttribute(const std::string& line,
310 const MediaType media_type,
311 MediaContentDescription* media_desc,
312 SdpParseError* error);
313static bool ParseIceOptions(const std::string& line,
314 std::vector<std::string>* transport_options,
315 SdpParseError* error);
316static bool ParseExtmap(const std::string& line,
isheriff6f8d6862016-05-26 11:24:55 -0700317 RtpExtension* extmap,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000318 SdpParseError* error);
319static bool ParseFingerprintAttribute(const std::string& line,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000320 rtc::SSLFingerprint** fingerprint,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000321 SdpParseError* error);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +0000322static bool ParseDtlsSetup(const std::string& line,
323 cricket::ConnectionRole* role,
324 SdpParseError* error);
deadbeef9d3584c2016-02-16 17:54:10 -0800325static bool ParseMsidAttribute(const std::string& line,
326 std::string* stream_id,
327 std::string* track_id,
328 SdpParseError* error);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000329
330// Helper functions
331
332// Below ParseFailed*** functions output the line that caused the parsing
333// failure and the detailed reason (|description|) of the failure to |error|.
334// The functions always return false so that they can be used directly in the
335// following way when error happens:
336// "return ParseFailed***(...);"
337
338// The line starting at |line_start| of |message| is the failing line.
339// The reason for the failure should be provided in the |description|.
340// An example of a description could be "unknown character".
341static bool ParseFailed(const std::string& message,
342 size_t line_start,
343 const std::string& description,
344 SdpParseError* error) {
345 // Get the first line of |message| from |line_start|.
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +0000346 std::string first_line;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000347 size_t line_end = message.find(kNewLine, line_start);
348 if (line_end != std::string::npos) {
349 if (line_end > 0 && (message.at(line_end - 1) == kReturn)) {
350 --line_end;
351 }
352 first_line = message.substr(line_start, (line_end - line_start));
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +0000353 } else {
354 first_line = message.substr(line_start);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000355 }
356
357 if (error) {
358 error->line = first_line;
359 error->description = description;
360 }
Mirko Bonadei675513b2017-11-09 11:09:25 +0100361 RTC_LOG(LS_ERROR) << "Failed to parse: \"" << first_line
362 << "\". Reason: " << description;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000363 return false;
364}
365
366// |line| is the failing line. The reason for the failure should be
367// provided in the |description|.
368static bool ParseFailed(const std::string& line,
369 const std::string& description,
370 SdpParseError* error) {
371 return ParseFailed(line, 0, description, error);
372}
373
374// Parses failure where the failing SDP line isn't know or there are multiple
375// failing lines.
376static bool ParseFailed(const std::string& description,
377 SdpParseError* error) {
378 return ParseFailed("", description, error);
379}
380
381// |line| is the failing line. The failure is due to the fact that |line|
382// doesn't have |expected_fields| fields.
383static bool ParseFailedExpectFieldNum(const std::string& line,
384 int expected_fields,
385 SdpParseError* error) {
386 std::ostringstream description;
387 description << "Expects " << expected_fields << " fields.";
388 return ParseFailed(line, description.str(), error);
389}
390
391// |line| is the failing line. The failure is due to the fact that |line| has
392// less than |expected_min_fields| fields.
393static bool ParseFailedExpectMinFieldNum(const std::string& line,
394 int expected_min_fields,
395 SdpParseError* error) {
396 std::ostringstream description;
397 description << "Expects at least " << expected_min_fields << " fields.";
398 return ParseFailed(line, description.str(), error);
399}
400
401// |line| is the failing line. The failure is due to the fact that it failed to
402// get the value of |attribute|.
403static bool ParseFailedGetValue(const std::string& line,
404 const std::string& attribute,
405 SdpParseError* error) {
406 std::ostringstream description;
407 description << "Failed to get the value of attribute: " << attribute;
408 return ParseFailed(line, description.str(), error);
409}
410
411// The line starting at |line_start| of |message| is the failing line. The
412// failure is due to the line type (e.g. the "m" part of the "m-line")
413// not matching what is expected. The expected line type should be
414// provided as |line_type|.
415static bool ParseFailedExpectLine(const std::string& message,
416 size_t line_start,
417 const char line_type,
418 const std::string& line_value,
419 SdpParseError* error) {
420 std::ostringstream description;
421 description << "Expect line: " << line_type << "=" << line_value;
422 return ParseFailed(message, line_start, description.str(), error);
423}
424
425static bool AddLine(const std::string& line, std::string* message) {
426 if (!message)
427 return false;
428
429 message->append(line);
430 message->append(kLineBreak);
431 return true;
432}
433
434static bool GetLine(const std::string& message,
435 size_t* pos,
436 std::string* line) {
437 size_t line_begin = *pos;
438 size_t line_end = message.find(kNewLine, line_begin);
439 if (line_end == std::string::npos) {
440 return false;
441 }
442 // Update the new start position
443 *pos = line_end + 1;
444 if (line_end > 0 && (message.at(line_end - 1) == kReturn)) {
445 --line_end;
446 }
447 *line = message.substr(line_begin, (line_end - line_begin));
448 const char* cline = line->c_str();
449 // RFC 4566
450 // An SDP session description consists of a number of lines of text of
451 // the form:
452 // <type>=<value>
453 // where <type> MUST be exactly one case-significant character and
454 // <value> is structured text whose format depends on <type>.
455 // Whitespace MUST NOT be used on either side of the "=" sign.
decurtis@webrtc.org8af11042015-01-07 19:15:51 +0000456 if (line->length() < 3 ||
457 !islower(cline[0]) ||
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000458 cline[1] != kSdpDelimiterEqual ||
459 cline[2] == kSdpDelimiterSpace) {
460 *pos = line_begin;
461 return false;
462 }
463 return true;
464}
465
466// Init |os| to "|type|=|value|".
467static void InitLine(const char type,
468 const std::string& value,
469 std::ostringstream* os) {
470 os->str("");
471 *os << type << kSdpDelimiterEqual << value;
472}
473
474// Init |os| to "a=|attribute|".
475static void InitAttrLine(const std::string& attribute, std::ostringstream* os) {
476 InitLine(kLineTypeAttributes, attribute, os);
477}
478
479// Writes a SDP attribute line based on |attribute| and |value| to |message|.
480static void AddAttributeLine(const std::string& attribute, int value,
481 std::string* message) {
482 std::ostringstream os;
483 InitAttrLine(attribute, &os);
484 os << kSdpDelimiterColon << value;
485 AddLine(os.str(), message);
486}
487
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000488static bool IsLineType(const std::string& message,
489 const char type,
490 size_t line_start) {
491 if (message.size() < line_start + kLinePrefixLength) {
492 return false;
493 }
494 const char* cmessage = message.c_str();
495 return (cmessage[line_start] == type &&
496 cmessage[line_start + 1] == kSdpDelimiterEqual);
497}
498
499static bool IsLineType(const std::string& line,
500 const char type) {
501 return IsLineType(line, type, 0);
502}
503
504static bool GetLineWithType(const std::string& message, size_t* pos,
505 std::string* line, const char type) {
506 if (!IsLineType(message, type, *pos)) {
507 return false;
508 }
509
510 if (!GetLine(message, pos, line))
511 return false;
512
513 return true;
514}
515
516static bool HasAttribute(const std::string& line,
517 const std::string& attribute) {
518 return (line.compare(kLinePrefixLength, attribute.size(), attribute) == 0);
519}
520
Peter Boström0c4e06b2015-10-07 12:23:21 +0200521static bool AddSsrcLine(uint32_t ssrc_id,
522 const std::string& attribute,
523 const std::string& value,
524 std::string* message) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000525 // RFC 5576
526 // a=ssrc:<ssrc-id> <attribute>:<value>
527 std::ostringstream os;
528 InitAttrLine(kAttributeSsrc, &os);
529 os << kSdpDelimiterColon << ssrc_id << kSdpDelimiterSpace
530 << attribute << kSdpDelimiterColon << value;
531 return AddLine(os.str(), message);
532}
533
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000534// Get value only from <attribute>:<value>.
535static bool GetValue(const std::string& message, const std::string& attribute,
536 std::string* value, SdpParseError* error) {
537 std::string leftpart;
Donald Curtis0e07f922015-05-15 09:21:23 -0700538 if (!rtc::tokenize_first(message, kSdpDelimiterColon, &leftpart, value)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000539 return ParseFailedGetValue(message, attribute, error);
540 }
541 // The left part should end with the expected attribute.
542 if (leftpart.length() < attribute.length() ||
543 leftpart.compare(leftpart.length() - attribute.length(),
544 attribute.length(), attribute) != 0) {
545 return ParseFailedGetValue(message, attribute, error);
546 }
547 return true;
548}
549
550static bool CaseInsensitiveFind(std::string str1, std::string str2) {
551 std::transform(str1.begin(), str1.end(), str1.begin(),
552 ::tolower);
553 std::transform(str2.begin(), str2.end(), str2.begin(),
554 ::tolower);
555 return str1.find(str2) != std::string::npos;
556}
557
wu@webrtc.org5e760e72014-04-02 23:19:09 +0000558template <class T>
559static bool GetValueFromString(const std::string& line,
560 const std::string& s,
561 T* t,
562 SdpParseError* error) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000563 if (!rtc::FromString(s, t)) {
wu@webrtc.org5e760e72014-04-02 23:19:09 +0000564 std::ostringstream description;
565 description << "Invalid value: " << s << ".";
566 return ParseFailed(line, description.str(), error);
567 }
568 return true;
569}
570
pkasting@chromium.orge9facf82015-02-17 20:36:28 +0000571static bool GetPayloadTypeFromString(const std::string& line,
572 const std::string& s,
573 int* payload_type,
574 SdpParseError* error) {
575 return GetValueFromString(line, s, payload_type, error) &&
576 cricket::IsValidRtpPayloadType(*payload_type);
577}
578
Taylor Brandstetter5de6b752016-03-09 17:02:30 -0800579// |msid_stream_id| and |msid_track_id| represent the stream/track ID from the
580// "a=msid" attribute, if it exists. They are empty if the attribute does not
581// exist.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000582void CreateTracksFromSsrcInfos(const SsrcInfoVec& ssrc_infos,
Taylor Brandstetter5de6b752016-03-09 17:02:30 -0800583 const std::string& msid_stream_id,
584 const std::string& msid_track_id,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000585 StreamParamsVec* tracks) {
nisseede5da42017-01-12 05:15:36 -0800586 RTC_DCHECK(tracks != NULL);
587 RTC_DCHECK(msid_stream_id.empty() == msid_track_id.empty());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000588 for (SsrcInfoVec::const_iterator ssrc_info = ssrc_infos.begin();
589 ssrc_info != ssrc_infos.end(); ++ssrc_info) {
590 if (ssrc_info->cname.empty()) {
591 continue;
592 }
593
Taylor Brandstetter5de6b752016-03-09 17:02:30 -0800594 std::string stream_id;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000595 std::string track_id;
Taylor Brandstetter5de6b752016-03-09 17:02:30 -0800596 if (ssrc_info->stream_id.empty() && !ssrc_info->mslabel.empty()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000597 // If there's no msid and there's mslabel, we consider this is a sdp from
598 // a older version of client that doesn't support msid.
599 // In that case, we use the mslabel and label to construct the track.
Taylor Brandstetter5de6b752016-03-09 17:02:30 -0800600 stream_id = ssrc_info->mslabel;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000601 track_id = ssrc_info->label;
Taylor Brandstetter5de6b752016-03-09 17:02:30 -0800602 } else if (ssrc_info->stream_id.empty() && !msid_stream_id.empty()) {
603 // If there's no msid in the SSRC attributes, but there's a global one
604 // (from a=msid), use that. This is the case with unified plan SDP.
605 stream_id = msid_stream_id;
606 track_id = msid_track_id;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000607 } else {
Taylor Brandstetter5de6b752016-03-09 17:02:30 -0800608 stream_id = ssrc_info->stream_id;
deadbeef9d3584c2016-02-16 17:54:10 -0800609 track_id = ssrc_info->track_id;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000610 }
Taylor Brandstetter5de6b752016-03-09 17:02:30 -0800611 // If a stream/track ID wasn't populated from the SSRC attributes OR the
612 // msid attribute, use default/random values.
613 if (stream_id.empty()) {
614 stream_id = kDefaultMsid;
615 }
616 if (track_id.empty()) {
617 // TODO(ronghuawu): What should we do if the track id doesn't appear?
618 // Create random string (which will be used as track label later)?
619 track_id = rtc::CreateRandomString(8);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000620 }
621
622 StreamParamsVec::iterator track = tracks->begin();
623 for (; track != tracks->end(); ++track) {
624 if (track->id == track_id) {
625 break;
626 }
627 }
628 if (track == tracks->end()) {
629 // If we don't find an existing track, create a new one.
630 tracks->push_back(StreamParams());
631 track = tracks->end() - 1;
632 }
633 track->add_ssrc(ssrc_info->ssrc_id);
634 track->cname = ssrc_info->cname;
Taylor Brandstetter5de6b752016-03-09 17:02:30 -0800635 track->sync_label = stream_id;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000636 track->id = track_id;
637 }
638}
639
640void GetMediaStreamLabels(const ContentInfo* content,
641 std::set<std::string>* labels) {
642 const MediaContentDescription* media_desc =
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000643 static_cast<const MediaContentDescription*>(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000644 content->description);
645 const cricket::StreamParamsVec& streams = media_desc->streams();
646 for (cricket::StreamParamsVec::const_iterator it = streams.begin();
647 it != streams.end(); ++it) {
648 labels->insert(it->sync_label);
649 }
650}
651
652// RFC 5245
653// It is RECOMMENDED that default candidates be chosen based on the
654// likelihood of those candidates to work with the peer that is being
655// contacted. It is RECOMMENDED that relayed > reflexive > host.
656static const int kPreferenceUnknown = 0;
657static const int kPreferenceHost = 1;
658static const int kPreferenceReflexive = 2;
659static const int kPreferenceRelayed = 3;
660
661static int GetCandidatePreferenceFromType(const std::string& type) {
662 int preference = kPreferenceUnknown;
663 if (type == cricket::LOCAL_PORT_TYPE) {
664 preference = kPreferenceHost;
665 } else if (type == cricket::STUN_PORT_TYPE) {
666 preference = kPreferenceReflexive;
667 } else if (type == cricket::RELAY_PORT_TYPE) {
668 preference = kPreferenceRelayed;
669 } else {
nissec80e7412017-01-11 05:56:46 -0800670 RTC_NOTREACHED();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000671 }
672 return preference;
673}
674
guoweis@webrtc.org57ac2c82015-02-06 00:45:13 +0000675// Get ip and port of the default destination from the |candidates| with the
676// given value of |component_id|. The default candidate should be the one most
677// likely to work, typically IPv4 relay.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000678// RFC 5245
679// The value of |component_id| currently supported are 1 (RTP) and 2 (RTCP).
Steve Anton36b29d12017-10-30 09:57:42 -0700680// TODO(deadbeef): Decide the default destination in webrtcsession and
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000681// pass it down via SessionDescription.
pthatcher@webrtc.orgc9d6d142014-10-23 23:37:22 +0000682static void GetDefaultDestination(
683 const std::vector<Candidate>& candidates,
684 int component_id, std::string* port,
685 std::string* ip, std::string* addr_type) {
perkj@webrtc.orgd105cc82014-11-07 11:22:06 +0000686 *addr_type = kConnectionIpv4Addrtype;
pthatcher@webrtc.orgc9d6d142014-10-23 23:37:22 +0000687 *port = kDummyPort;
688 *ip = kDummyAddress;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000689 int current_preference = kPreferenceUnknown;
guoweis@webrtc.org57ac2c82015-02-06 00:45:13 +0000690 int current_family = AF_UNSPEC;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000691 for (std::vector<Candidate>::const_iterator it = candidates.begin();
692 it != candidates.end(); ++it) {
693 if (it->component() != component_id) {
694 continue;
695 }
guoweis@webrtc.org57ac2c82015-02-06 00:45:13 +0000696 // Default destination should be UDP only.
697 if (it->protocol() != cricket::UDP_PROTOCOL_NAME) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000698 continue;
699 }
guoweis@webrtc.org57ac2c82015-02-06 00:45:13 +0000700 const int preference = GetCandidatePreferenceFromType(it->type());
701 const int family = it->address().ipaddr().family();
702 // See if this candidate is more preferable then the current one if it's the
703 // same family. Or if the current family is IPv4 already so we could safely
704 // ignore all IPv6 ones. WebRTC bug 4269.
705 // http://code.google.com/p/webrtc/issues/detail?id=4269
706 if ((preference <= current_preference && current_family == family) ||
707 (current_family == AF_INET && family == AF_INET6)) {
708 continue;
709 }
pthatcher@webrtc.orgc9d6d142014-10-23 23:37:22 +0000710 if (family == AF_INET) {
711 addr_type->assign(kConnectionIpv4Addrtype);
712 } else if (family == AF_INET6) {
713 addr_type->assign(kConnectionIpv6Addrtype);
714 }
guoweis@webrtc.org57ac2c82015-02-06 00:45:13 +0000715 current_preference = preference;
716 current_family = family;
717 *port = it->address().PortAsString();
718 *ip = it->address().ipaddr().ToString();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000719 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000720}
721
wu@webrtc.org4c3e9912014-07-16 21:03:13 +0000722// Gets "a=rtcp" line if found default RTCP candidate from |candidates|.
723static std::string GetRtcpLine(const std::vector<Candidate>& candidates) {
pthatcher@webrtc.orgc9d6d142014-10-23 23:37:22 +0000724 std::string rtcp_line, rtcp_port, rtcp_ip, addr_type;
725 GetDefaultDestination(candidates, ICE_CANDIDATE_COMPONENT_RTCP,
726 &rtcp_port, &rtcp_ip, &addr_type);
727 // Found default RTCP candidate.
728 // RFC 5245
729 // If the agent is utilizing RTCP, it MUST encode the RTCP candidate
730 // using the a=rtcp attribute as defined in RFC 3605.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000731
pthatcher@webrtc.orgc9d6d142014-10-23 23:37:22 +0000732 // RFC 3605
733 // rtcp-attribute = "a=rtcp:" port [nettype space addrtype space
734 // connection-address] CRLF
735 std::ostringstream os;
736 InitAttrLine(kAttributeRtcp, &os);
737 os << kSdpDelimiterColon
738 << rtcp_port << " "
739 << kConnectionNettype << " "
740 << addr_type << " "
741 << rtcp_ip;
742 rtcp_line = os.str();
wu@webrtc.org4c3e9912014-07-16 21:03:13 +0000743 return rtcp_line;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000744}
745
746// Get candidates according to the mline index from SessionDescriptionInterface.
747static void GetCandidatesByMindex(const SessionDescriptionInterface& desci,
748 int mline_index,
749 std::vector<Candidate>* candidates) {
750 if (!candidates) {
751 return;
752 }
753 const IceCandidateCollection* cc = desci.candidates(mline_index);
754 for (size_t i = 0; i < cc->count(); ++i) {
755 const IceCandidateInterface* candidate = cc->at(i);
756 candidates->push_back(candidate->candidate());
757 }
758}
759
deadbeef90f1e1e2017-02-10 12:35:05 -0800760static bool IsValidPort(int port) {
761 return port >= 0 && port <= 65535;
762}
763
deadbeef9d3584c2016-02-16 17:54:10 -0800764std::string SdpSerialize(const JsepSessionDescription& jdesc,
765 bool unified_plan_sdp) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000766 const cricket::SessionDescription* desc = jdesc.description();
767 if (!desc) {
768 return "";
769 }
770
771 std::string message;
772
773 // Session Description.
774 AddLine(kSessionVersion, &message);
775 // Session Origin
776 // RFC 4566
777 // o=<username> <sess-id> <sess-version> <nettype> <addrtype>
778 // <unicast-address>
779 std::ostringstream os;
780 InitLine(kLineTypeOrigin, kSessionOriginUsername, &os);
jbauch083b73f2015-07-16 02:46:32 -0700781 const std::string& session_id = jdesc.session_id().empty() ?
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000782 kSessionOriginSessionId : jdesc.session_id();
jbauch083b73f2015-07-16 02:46:32 -0700783 const std::string& session_version = jdesc.session_version().empty() ?
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000784 kSessionOriginSessionVersion : jdesc.session_version();
785 os << " " << session_id << " " << session_version << " "
786 << kSessionOriginNettype << " " << kSessionOriginAddrtype << " "
787 << kSessionOriginAddress;
788 AddLine(os.str(), &message);
789 AddLine(kSessionName, &message);
790
791 // Time Description.
792 AddLine(kTimeDescription, &message);
793
794 // Group
795 if (desc->HasGroup(cricket::GROUP_TYPE_BUNDLE)) {
796 std::string group_line = kAttrGroup;
797 const cricket::ContentGroup* group =
798 desc->GetGroupByName(cricket::GROUP_TYPE_BUNDLE);
nisseede5da42017-01-12 05:15:36 -0800799 RTC_DCHECK(group != NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000800 const cricket::ContentNames& content_names = group->content_names();
801 for (cricket::ContentNames::const_iterator it = content_names.begin();
802 it != content_names.end(); ++it) {
803 group_line.append(" ");
804 group_line.append(*it);
805 }
806 AddLine(group_line, &message);
807 }
808
809 // MediaStream semantics
810 InitAttrLine(kAttributeMsidSemantics, &os);
811 os << kSdpDelimiterColon << " " << kMediaStreamSemantic;
wu@webrtc.org4c3e9912014-07-16 21:03:13 +0000812
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000813 std::set<std::string> media_stream_labels;
814 const ContentInfo* audio_content = GetFirstAudioContent(desc);
815 if (audio_content)
816 GetMediaStreamLabels(audio_content, &media_stream_labels);
wu@webrtc.org4c3e9912014-07-16 21:03:13 +0000817
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000818 const ContentInfo* video_content = GetFirstVideoContent(desc);
819 if (video_content)
820 GetMediaStreamLabels(video_content, &media_stream_labels);
wu@webrtc.org4c3e9912014-07-16 21:03:13 +0000821
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000822 for (std::set<std::string>::const_iterator it =
823 media_stream_labels.begin(); it != media_stream_labels.end(); ++it) {
824 os << " " << *it;
825 }
826 AddLine(os.str(), &message);
827
wu@webrtc.org4c3e9912014-07-16 21:03:13 +0000828 // Preserve the order of the media contents.
829 int mline_index = -1;
830 for (cricket::ContentInfos::const_iterator it = desc->contents().begin();
831 it != desc->contents().end(); ++it) {
832 const MediaContentDescription* mdesc =
833 static_cast<const MediaContentDescription*>(it->description);
834 std::vector<Candidate> candidates;
835 GetCandidatesByMindex(jdesc, ++mline_index, &candidates);
deadbeef9d3584c2016-02-16 17:54:10 -0800836 BuildMediaDescription(&*it, desc->GetTransportInfoByName(it->name),
837 mdesc->type(), candidates, unified_plan_sdp,
wu@webrtc.org4c3e9912014-07-16 21:03:13 +0000838 &message);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000839 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000840 return message;
841}
842
843// Serializes the passed in IceCandidateInterface to a SDP string.
844// candidate - The candidate to be serialized.
Honghai Zhang7fb69db2016-03-14 11:59:18 -0700845std::string SdpSerializeCandidate(const IceCandidateInterface& candidate) {
846 return SdpSerializeCandidate(candidate.candidate());
847}
848
849// Serializes a cricket Candidate.
850std::string SdpSerializeCandidate(const cricket::Candidate& candidate) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000851 std::string message;
Honghai Zhang7fb69db2016-03-14 11:59:18 -0700852 std::vector<cricket::Candidate> candidates(1, candidate);
honghaiza54a0802015-12-16 18:37:23 -0800853 BuildCandidate(candidates, true, &message);
wu@webrtc.orgec9f5fb2014-06-24 17:05:10 +0000854 // From WebRTC draft section 4.8.1.1 candidate-attribute will be
855 // just candidate:<candidate> not a=candidate:<blah>CRLF
nisseede5da42017-01-12 05:15:36 -0800856 RTC_DCHECK(message.find("a=") == 0);
wu@webrtc.orgec9f5fb2014-06-24 17:05:10 +0000857 message.erase(0, 2);
nisseede5da42017-01-12 05:15:36 -0800858 RTC_DCHECK(message.find(kLineBreak) == message.size() - 2);
wu@webrtc.orgec9f5fb2014-06-24 17:05:10 +0000859 message.resize(message.size() - 2);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000860 return message;
861}
862
863bool SdpDeserialize(const std::string& message,
864 JsepSessionDescription* jdesc,
865 SdpParseError* error) {
866 std::string session_id;
867 std::string session_version;
Peter Thatcher7cbd1882015-09-17 18:54:52 -0700868 TransportDescription session_td("", "");
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000869 RtpHeaderExtensions session_extmaps;
zhihuang38989e52017-03-21 11:04:53 -0700870 rtc::SocketAddress session_connection_addr;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000871 cricket::SessionDescription* desc = new cricket::SessionDescription();
872 std::vector<JsepIceCandidate*> candidates;
873 size_t current_pos = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000874
875 // Session Description
876 if (!ParseSessionDescription(message, &current_pos, &session_id,
deadbeefc80741f2015-10-22 13:14:45 -0700877 &session_version, &session_td, &session_extmaps,
zhihuang38989e52017-03-21 11:04:53 -0700878 &session_connection_addr, desc, error)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000879 delete desc;
880 return false;
881 }
882
883 // Media Description
deadbeefc80741f2015-10-22 13:14:45 -0700884 if (!ParseMediaDescription(message, session_td, session_extmaps, &current_pos,
zhihuang38989e52017-03-21 11:04:53 -0700885 session_connection_addr, desc, &candidates,
886 error)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000887 delete desc;
888 for (std::vector<JsepIceCandidate*>::const_iterator
889 it = candidates.begin(); it != candidates.end(); ++it) {
890 delete *it;
891 }
892 return false;
893 }
894
895 jdesc->Initialize(desc, session_id, session_version);
896
897 for (std::vector<JsepIceCandidate*>::const_iterator
898 it = candidates.begin(); it != candidates.end(); ++it) {
899 jdesc->AddCandidate(*it);
900 delete *it;
901 }
902 return true;
903}
904
905bool SdpDeserializeCandidate(const std::string& message,
906 JsepIceCandidate* jcandidate,
907 SdpParseError* error) {
nisseede5da42017-01-12 05:15:36 -0800908 RTC_DCHECK(jcandidate != NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000909 Candidate candidate;
910 if (!ParseCandidate(message, &candidate, error, true)) {
911 return false;
912 }
913 jcandidate->SetCandidate(candidate);
914 return true;
915}
916
Honghai Zhang7fb69db2016-03-14 11:59:18 -0700917bool SdpDeserializeCandidate(const std::string& transport_name,
918 const std::string& message,
919 cricket::Candidate* candidate,
920 SdpParseError* error) {
nisseede5da42017-01-12 05:15:36 -0800921 RTC_DCHECK(candidate != nullptr);
Honghai Zhang7fb69db2016-03-14 11:59:18 -0700922 if (!ParseCandidate(message, candidate, error, true)) {
923 return false;
924 }
925 candidate->set_transport_name(transport_name);
926 return true;
927}
928
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000929bool ParseCandidate(const std::string& message, Candidate* candidate,
930 SdpParseError* error, bool is_raw) {
nisseede5da42017-01-12 05:15:36 -0800931 RTC_DCHECK(candidate != NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000932
933 // Get the first line from |message|.
jiayl@webrtc.org7ec3f9f2014-08-08 23:09:15 +0000934 std::string first_line = message;
935 size_t pos = 0;
936 GetLine(message, &pos, &first_line);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000937
jiayl@webrtc.org7ec3f9f2014-08-08 23:09:15 +0000938 // Makes sure |message| contains only one line.
939 if (message.size() > first_line.size()) {
940 std::string left, right;
Donald Curtis0e07f922015-05-15 09:21:23 -0700941 if (rtc::tokenize_first(message, kNewLine, &left, &right) &&
942 !right.empty()) {
jiayl@webrtc.org7ec3f9f2014-08-08 23:09:15 +0000943 return ParseFailed(message, 0, "Expect one line only", error);
944 }
945 }
946
947 // From WebRTC draft section 4.8.1.1 candidate-attribute should be
948 // candidate:<candidate> when trickled, but we still support
949 // a=candidate:<blah>CRLF for backward compatibility and for parsing a line
950 // from the SDP.
951 if (IsLineType(first_line, kLineTypeAttributes)) {
952 first_line = first_line.substr(kLinePrefixLength);
953 }
954
955 std::string attribute_candidate;
956 std::string candidate_value;
957
958 // |first_line| must be in the form of "candidate:<value>".
Donald Curtis144d0182015-05-15 13:14:24 -0700959 if (!rtc::tokenize_first(first_line, kSdpDelimiterColon, &attribute_candidate,
960 &candidate_value) ||
jiayl@webrtc.org7ec3f9f2014-08-08 23:09:15 +0000961 attribute_candidate != kAttributeCandidate) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000962 if (is_raw) {
963 std::ostringstream description;
jiayl@webrtc.org7ec3f9f2014-08-08 23:09:15 +0000964 description << "Expect line: " << kAttributeCandidate
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000965 << ":" << "<candidate-str>";
966 return ParseFailed(first_line, 0, description.str(), error);
967 } else {
968 return ParseFailedExpectLine(first_line, 0, kLineTypeAttributes,
969 kAttributeCandidate, error);
970 }
971 }
972
973 std::vector<std::string> fields;
jiayl@webrtc.org7ec3f9f2014-08-08 23:09:15 +0000974 rtc::split(candidate_value, kSdpDelimiterSpace, &fields);
975
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000976 // RFC 5245
977 // a=candidate:<foundation> <component-id> <transport> <priority>
978 // <connection-address> <port> typ <candidate-types>
979 // [raddr <connection-address>] [rport <port>]
980 // *(SP extension-att-name SP extension-att-value)
981 const size_t expected_min_fields = 8;
982 if (fields.size() < expected_min_fields ||
983 (fields[6] != kAttributeCandidateTyp)) {
984 return ParseFailedExpectMinFieldNum(first_line, expected_min_fields, error);
985 }
jbauch083b73f2015-07-16 02:46:32 -0700986 const std::string& foundation = fields[0];
jiayl@webrtc.org7ec3f9f2014-08-08 23:09:15 +0000987
wu@webrtc.org5e760e72014-04-02 23:19:09 +0000988 int component_id = 0;
989 if (!GetValueFromString(first_line, fields[1], &component_id, error)) {
990 return false;
991 }
jbauch083b73f2015-07-16 02:46:32 -0700992 const std::string& transport = fields[2];
Peter Boström0c4e06b2015-10-07 12:23:21 +0200993 uint32_t priority = 0;
wu@webrtc.org5e760e72014-04-02 23:19:09 +0000994 if (!GetValueFromString(first_line, fields[3], &priority, error)) {
995 return false;
996 }
jbauch083b73f2015-07-16 02:46:32 -0700997 const std::string& connection_address = fields[4];
wu@webrtc.org5e760e72014-04-02 23:19:09 +0000998 int port = 0;
999 if (!GetValueFromString(first_line, fields[5], &port, error)) {
1000 return false;
1001 }
deadbeef90f1e1e2017-02-10 12:35:05 -08001002 if (!IsValidPort(port)) {
1003 return ParseFailed(first_line, "Invalid port number.", error);
1004 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001005 SocketAddress address(connection_address, port);
1006
1007 cricket::ProtocolType protocol;
hnslb68cc752016-12-13 10:33:41 -08001008 if (!StringToProto(transport.c_str(), &protocol)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001009 return ParseFailed(first_line, "Unsupported transport type.", error);
1010 }
hnslb68cc752016-12-13 10:33:41 -08001011 switch (protocol) {
1012 case cricket::PROTO_UDP:
1013 case cricket::PROTO_TCP:
1014 case cricket::PROTO_SSLTCP:
1015 // Supported protocol.
1016 break;
1017 default:
1018 return ParseFailed(first_line, "Unsupported transport type.", error);
1019 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001020
1021 std::string candidate_type;
jbauch083b73f2015-07-16 02:46:32 -07001022 const std::string& type = fields[7];
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001023 if (type == kCandidateHost) {
1024 candidate_type = cricket::LOCAL_PORT_TYPE;
1025 } else if (type == kCandidateSrflx) {
1026 candidate_type = cricket::STUN_PORT_TYPE;
1027 } else if (type == kCandidateRelay) {
1028 candidate_type = cricket::RELAY_PORT_TYPE;
Honghai Zhang7fb69db2016-03-14 11:59:18 -07001029 } else if (type == kCandidatePrflx) {
1030 candidate_type = cricket::PRFLX_PORT_TYPE;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001031 } else {
1032 return ParseFailed(first_line, "Unsupported candidate type.", error);
1033 }
1034
1035 size_t current_position = expected_min_fields;
1036 SocketAddress related_address;
1037 // The 2 optional fields for related address
1038 // [raddr <connection-address>] [rport <port>]
1039 if (fields.size() >= (current_position + 2) &&
1040 fields[current_position] == kAttributeCandidateRaddr) {
1041 related_address.SetIP(fields[++current_position]);
1042 ++current_position;
1043 }
1044 if (fields.size() >= (current_position + 2) &&
1045 fields[current_position] == kAttributeCandidateRport) {
wu@webrtc.org5e760e72014-04-02 23:19:09 +00001046 int port = 0;
1047 if (!GetValueFromString(
1048 first_line, fields[++current_position], &port, error)) {
1049 return false;
1050 }
deadbeef90f1e1e2017-02-10 12:35:05 -08001051 if (!IsValidPort(port)) {
1052 return ParseFailed(first_line, "Invalid port number.", error);
1053 }
wu@webrtc.org5e760e72014-04-02 23:19:09 +00001054 related_address.SetPort(port);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001055 ++current_position;
1056 }
1057
mallinath@webrtc.org2d60c5e2014-08-08 22:29:20 +00001058 // If this is a TCP candidate, it has additional extension as defined in
1059 // RFC 6544.
1060 std::string tcptype;
1061 if (fields.size() >= (current_position + 2) &&
1062 fields[current_position] == kTcpCandidateType) {
1063 tcptype = fields[++current_position];
1064 ++current_position;
1065
1066 if (tcptype != cricket::TCPTYPE_ACTIVE_STR &&
1067 tcptype != cricket::TCPTYPE_PASSIVE_STR &&
1068 tcptype != cricket::TCPTYPE_SIMOPEN_STR) {
1069 return ParseFailed(first_line, "Invalid TCP candidate type.", error);
1070 }
1071
1072 if (protocol != cricket::PROTO_TCP) {
1073 return ParseFailed(first_line, "Invalid non-TCP candidate", error);
1074 }
1075 }
1076
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001077 // Extension
honghaiza54a0802015-12-16 18:37:23 -08001078 // Though non-standard, we support the ICE ufrag and pwd being signaled on
1079 // the candidate to avoid issues with confusing which generation a candidate
1080 // belongs to when trickling multiple generations at the same time.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001081 std::string username;
1082 std::string password;
Peter Boström0c4e06b2015-10-07 12:23:21 +02001083 uint32_t generation = 0;
honghaiza0c44ea2016-03-23 16:07:48 -07001084 uint16_t network_id = 0;
1085 uint16_t network_cost = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001086 for (size_t i = current_position; i + 1 < fields.size(); ++i) {
1087 // RFC 5245
1088 // *(SP extension-att-name SP extension-att-value)
1089 if (fields[i] == kAttributeCandidateGeneration) {
wu@webrtc.org5e760e72014-04-02 23:19:09 +00001090 if (!GetValueFromString(first_line, fields[++i], &generation, error)) {
1091 return false;
1092 }
honghaiza54a0802015-12-16 18:37:23 -08001093 } else if (fields[i] == kAttributeCandidateUfrag) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001094 username = fields[++i];
honghaiza54a0802015-12-16 18:37:23 -08001095 } else if (fields[i] == kAttributeCandidatePwd) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001096 password = fields[++i];
honghaiza0c44ea2016-03-23 16:07:48 -07001097 } else if (fields[i] == kAttributeCandidateNetworkId) {
1098 if (!GetValueFromString(first_line, fields[++i], &network_id, error)) {
1099 return false;
1100 }
honghaize1a0c942016-02-16 14:54:56 -08001101 } else if (fields[i] == kAttributeCandidateNetworkCost) {
1102 if (!GetValueFromString(first_line, fields[++i], &network_cost, error)) {
1103 return false;
1104 }
Honghai Zhang351d77b2016-05-20 15:08:29 -07001105 network_cost = std::min(network_cost, rtc::kNetworkCostMax);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001106 } else {
1107 // Skip the unknown extension.
1108 ++i;
1109 }
1110 }
1111
guoweis@webrtc.org61c12472015-01-15 06:53:07 +00001112 *candidate = Candidate(component_id, cricket::ProtoToString(protocol),
guoweis@webrtc.org950c5182014-12-16 23:01:31 +00001113 address, priority, username, password, candidate_type,
honghaiza0c44ea2016-03-23 16:07:48 -07001114 generation, foundation, network_id, network_cost);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001115 candidate->set_related_address(related_address);
mallinath@webrtc.org2d60c5e2014-08-08 22:29:20 +00001116 candidate->set_tcptype(tcptype);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001117 return true;
1118}
1119
1120bool ParseIceOptions(const std::string& line,
1121 std::vector<std::string>* transport_options,
1122 SdpParseError* error) {
1123 std::string ice_options;
1124 if (!GetValue(line, kAttributeIceOption, &ice_options, error)) {
1125 return false;
1126 }
1127 std::vector<std::string> fields;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001128 rtc::split(ice_options, kSdpDelimiterSpace, &fields);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001129 for (size_t i = 0; i < fields.size(); ++i) {
1130 transport_options->push_back(fields[i]);
1131 }
1132 return true;
1133}
1134
jiayl@webrtc.orgddb85ab2014-09-05 16:31:56 +00001135bool ParseSctpPort(const std::string& line,
1136 int* sctp_port,
1137 SdpParseError* error) {
1138 // draft-ietf-mmusic-sctp-sdp-07
1139 // a=sctp-port
1140 std::vector<std::string> fields;
jiayl@webrtc.orgddb85ab2014-09-05 16:31:56 +00001141 const size_t expected_min_fields = 2;
lally69f57602015-10-08 10:15:04 -07001142 rtc::split(line.substr(kLinePrefixLength), kSdpDelimiterColon, &fields);
1143 if (fields.size() < expected_min_fields) {
1144 fields.resize(0);
1145 rtc::split(line.substr(kLinePrefixLength), kSdpDelimiterSpace, &fields);
1146 }
jiayl@webrtc.orgddb85ab2014-09-05 16:31:56 +00001147 if (fields.size() < expected_min_fields) {
1148 return ParseFailedExpectMinFieldNum(line, expected_min_fields, error);
1149 }
1150 if (!rtc::FromString(fields[1], sctp_port)) {
lally69f57602015-10-08 10:15:04 -07001151 return ParseFailed(line, "Invalid sctp port value.", error);
jiayl@webrtc.orgddb85ab2014-09-05 16:31:56 +00001152 }
1153 return true;
1154}
1155
isheriff6f8d6862016-05-26 11:24:55 -07001156bool ParseExtmap(const std::string& line,
1157 RtpExtension* extmap,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001158 SdpParseError* error) {
1159 // RFC 5285
1160 // a=extmap:<value>["/"<direction>] <URI> <extensionattributes>
1161 std::vector<std::string> fields;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001162 rtc::split(line.substr(kLinePrefixLength),
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001163 kSdpDelimiterSpace, &fields);
1164 const size_t expected_min_fields = 2;
1165 if (fields.size() < expected_min_fields) {
1166 return ParseFailedExpectMinFieldNum(line, expected_min_fields, error);
1167 }
1168 std::string uri = fields[1];
1169
1170 std::string value_direction;
1171 if (!GetValue(fields[0], kAttributeExtmap, &value_direction, error)) {
1172 return false;
1173 }
1174 std::vector<std::string> sub_fields;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001175 rtc::split(value_direction, kSdpDelimiterSlash, &sub_fields);
wu@webrtc.org5e760e72014-04-02 23:19:09 +00001176 int value = 0;
1177 if (!GetValueFromString(line, sub_fields[0], &value, error)) {
1178 return false;
1179 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001180
jbauch5869f502017-06-29 12:31:36 -07001181 bool encrypted = false;
1182 if (uri == RtpExtension::kEncryptHeaderExtensionsUri) {
1183 // RFC 6904
Steve Anton36b29d12017-10-30 09:57:42 -07001184 // a=extmap:<value["/"<direction>] urn:ietf:params:rtp-hdrext:encrypt <URI>
1185 // <extensionattributes>
jbauch5869f502017-06-29 12:31:36 -07001186 const size_t expected_min_fields_encrypted = expected_min_fields + 1;
1187 if (fields.size() < expected_min_fields_encrypted) {
1188 return ParseFailedExpectMinFieldNum(line, expected_min_fields_encrypted,
1189 error);
1190 }
1191
1192 encrypted = true;
1193 uri = fields[2];
1194 if (uri == RtpExtension::kEncryptHeaderExtensionsUri) {
1195 return ParseFailed(line, "Recursive encrypted header.", error);
1196 }
1197 }
1198
1199 *extmap = RtpExtension(uri, value, encrypted);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001200 return true;
1201}
1202
1203void BuildMediaDescription(const ContentInfo* content_info,
1204 const TransportInfo* transport_info,
1205 const MediaType media_type,
wu@webrtc.org4c3e9912014-07-16 21:03:13 +00001206 const std::vector<Candidate>& candidates,
deadbeef9d3584c2016-02-16 17:54:10 -08001207 bool unified_plan_sdp,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001208 std::string* message) {
nisseede5da42017-01-12 05:15:36 -08001209 RTC_DCHECK(message != NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001210 if (content_info == NULL || message == NULL) {
1211 return;
1212 }
Steve Anton36b29d12017-10-30 09:57:42 -07001213 // TODO(deadbeef): Rethink if we should use sprintfn instead of stringstream.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001214 // According to the style guide, streams should only be used for logging.
1215 // http://google-styleguide.googlecode.com/svn/
1216 // trunk/cppguide.xml?showone=Streams#Streams
1217 std::ostringstream os;
1218 const MediaContentDescription* media_desc =
henrike@webrtc.org28654cb2013-07-22 21:07:49 +00001219 static_cast<const MediaContentDescription*>(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001220 content_info->description);
nisseede5da42017-01-12 05:15:36 -08001221 RTC_DCHECK(media_desc != NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001222
jiayl@webrtc.org9c16c392014-05-01 18:30:30 +00001223 int sctp_port = cricket::kSctpDefaultPort;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001224
1225 // RFC 4566
1226 // m=<media> <port> <proto> <fmt>
1227 // fmt is a list of payload type numbers that MAY be used in the session.
1228 const char* type = NULL;
1229 if (media_type == cricket::MEDIA_TYPE_AUDIO)
1230 type = kMediaTypeAudio;
1231 else if (media_type == cricket::MEDIA_TYPE_VIDEO)
1232 type = kMediaTypeVideo;
1233 else if (media_type == cricket::MEDIA_TYPE_DATA)
1234 type = kMediaTypeData;
1235 else
nissec80e7412017-01-11 05:56:46 -08001236 RTC_NOTREACHED();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001237
1238 std::string fmt;
1239 if (media_type == cricket::MEDIA_TYPE_VIDEO) {
1240 const VideoContentDescription* video_desc =
1241 static_cast<const VideoContentDescription*>(media_desc);
1242 for (std::vector<cricket::VideoCodec>::const_iterator it =
1243 video_desc->codecs().begin();
1244 it != video_desc->codecs().end(); ++it) {
1245 fmt.append(" ");
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001246 fmt.append(rtc::ToString<int>(it->id));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001247 }
1248 } else if (media_type == cricket::MEDIA_TYPE_AUDIO) {
1249 const AudioContentDescription* audio_desc =
1250 static_cast<const AudioContentDescription*>(media_desc);
1251 for (std::vector<cricket::AudioCodec>::const_iterator it =
1252 audio_desc->codecs().begin();
1253 it != audio_desc->codecs().end(); ++it) {
1254 fmt.append(" ");
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001255 fmt.append(rtc::ToString<int>(it->id));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001256 }
1257 } else if (media_type == cricket::MEDIA_TYPE_DATA) {
jiayl@webrtc.org9c16c392014-05-01 18:30:30 +00001258 const DataContentDescription* data_desc =
1259 static_cast<const DataContentDescription*>(media_desc);
pthatcher@webrtc.org3341b402015-02-13 21:14:22 +00001260 if (IsDtlsSctp(media_desc->protocol())) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001261 fmt.append(" ");
jiayl@webrtc.org9c16c392014-05-01 18:30:30 +00001262
zstein4b2e0822017-02-17 19:48:38 -08001263 if (data_desc->use_sctpmap()) {
1264 for (std::vector<cricket::DataCodec>::const_iterator it =
1265 data_desc->codecs().begin();
1266 it != data_desc->codecs().end(); ++it) {
1267 if (cricket::CodecNamesEq(it->name,
1268 cricket::kGoogleSctpDataCodecName) &&
1269 it->GetParam(cricket::kCodecParamPort, &sctp_port)) {
1270 break;
1271 }
jiayl@webrtc.org9c16c392014-05-01 18:30:30 +00001272 }
jiayl@webrtc.org9c16c392014-05-01 18:30:30 +00001273
zstein4b2e0822017-02-17 19:48:38 -08001274 fmt.append(rtc::ToString<int>(sctp_port));
1275 } else {
1276 fmt.append(kDefaultSctpmapProtocol);
1277 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001278 } else {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001279 for (std::vector<cricket::DataCodec>::const_iterator it =
1280 data_desc->codecs().begin();
1281 it != data_desc->codecs().end(); ++it) {
1282 fmt.append(" ");
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001283 fmt.append(rtc::ToString<int>(it->id));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001284 }
1285 }
1286 }
1287 // The fmt must never be empty. If no codecs are found, set the fmt attribute
1288 // to 0.
1289 if (fmt.empty()) {
1290 fmt = " 0";
1291 }
1292
deadbeef25ed4352016-12-12 18:37:36 -08001293 // The port number in the m line will be updated later when associated with
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001294 // the candidates.
deadbeef25ed4352016-12-12 18:37:36 -08001295 //
1296 // A port value of 0 indicates that the m= section is rejected.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001297 // RFC 3264
1298 // To reject an offered stream, the port number in the corresponding stream in
1299 // the answer MUST be set to zero.
deadbeef25ed4352016-12-12 18:37:36 -08001300 //
1301 // However, the BUNDLE draft adds a new meaning to port zero, when used along
1302 // with a=bundle-only.
zhihuang38989e52017-03-21 11:04:53 -07001303 std::string port = kDummyPort;
1304 if (content_info->rejected || content_info->bundle_only) {
1305 port = kMediaPortRejected;
1306 } else if (!media_desc->connection_address().IsNil()) {
1307 port = rtc::ToString(media_desc->connection_address().port());
1308 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001309
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001310 rtc::SSLFingerprint* fp = (transport_info) ?
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001311 transport_info->description.identity_fingerprint.get() : NULL;
1312
wu@webrtc.org4c3e9912014-07-16 21:03:13 +00001313 // Add the m and c lines.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001314 InitLine(kLineTypeMedia, type, &os);
1315 os << " " << port << " " << media_desc->protocol() << fmt;
zhihuang38989e52017-03-21 11:04:53 -07001316 AddLine(os.str(), message);
1317
1318 InitLine(kLineTypeConnection, kConnectionNettype, &os);
1319 if (media_desc->connection_address().IsNil()) {
1320 os << " " << kConnectionIpv4Addrtype << " " << kDummyAddress;
1321 } else if (media_desc->connection_address().family() == AF_INET) {
1322 os << " " << kConnectionIpv4Addrtype << " "
1323 << media_desc->connection_address().ipaddr().ToString();
1324 } else {
1325 os << " " << kConnectionIpv6Addrtype << " "
1326 << media_desc->connection_address().ipaddr().ToString();
1327 }
1328 AddLine(os.str(), message);
wu@webrtc.org4c3e9912014-07-16 21:03:13 +00001329
1330 // RFC 4566
1331 // b=AS:<bandwidth>
Peter Thatcherc0c3a862015-06-24 15:31:25 -07001332 if (media_desc->bandwidth() >= 1000) {
wu@webrtc.org4c3e9912014-07-16 21:03:13 +00001333 InitLine(kLineTypeSessionBandwidth, kApplicationSpecificMaximum, &os);
1334 os << kSdpDelimiterColon << (media_desc->bandwidth() / 1000);
1335 AddLine(os.str(), message);
1336 }
1337
deadbeef25ed4352016-12-12 18:37:36 -08001338 // Add the a=bundle-only line.
1339 if (content_info->bundle_only) {
1340 InitAttrLine(kAttributeBundleOnly, &os);
1341 AddLine(os.str(), message);
1342 }
1343
wu@webrtc.org4c3e9912014-07-16 21:03:13 +00001344 // Add the a=rtcp line.
pthatcher@webrtc.org3341b402015-02-13 21:14:22 +00001345 if (IsRtp(media_desc->protocol())) {
wu@webrtc.org4c3e9912014-07-16 21:03:13 +00001346 std::string rtcp_line = GetRtcpLine(candidates);
1347 if (!rtcp_line.empty()) {
1348 AddLine(rtcp_line, message);
1349 }
1350 }
1351
honghaiza54a0802015-12-16 18:37:23 -08001352 // Build the a=candidate lines. We don't include ufrag and pwd in the
1353 // candidates in the SDP to avoid redundancy.
1354 BuildCandidate(candidates, false, message);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001355
1356 // Use the transport_info to build the media level ice-ufrag and ice-pwd.
1357 if (transport_info) {
1358 // RFC 5245
1359 // ice-pwd-att = "ice-pwd" ":" password
1360 // ice-ufrag-att = "ice-ufrag" ":" ufrag
1361 // ice-ufrag
deadbeef3f7219b2015-12-28 15:17:14 -08001362 if (!transport_info->description.ice_ufrag.empty()) {
1363 InitAttrLine(kAttributeIceUfrag, &os);
1364 os << kSdpDelimiterColon << transport_info->description.ice_ufrag;
1365 AddLine(os.str(), message);
1366 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001367 // ice-pwd
deadbeef3f7219b2015-12-28 15:17:14 -08001368 if (!transport_info->description.ice_pwd.empty()) {
1369 InitAttrLine(kAttributeIcePwd, &os);
1370 os << kSdpDelimiterColon << transport_info->description.ice_pwd;
1371 AddLine(os.str(), message);
1372 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001373
1374 // draft-petithuguenin-mmusic-ice-attributes-level-03
1375 BuildIceOptions(transport_info->description.transport_options, message);
1376
1377 // RFC 4572
1378 // fingerprint-attribute =
1379 // "fingerprint" ":" hash-func SP fingerprint
1380 if (fp) {
1381 // Insert the fingerprint attribute.
1382 InitAttrLine(kAttributeFingerprint, &os);
1383 os << kSdpDelimiterColon
1384 << fp->algorithm << kSdpDelimiterSpace
1385 << fp->GetRfc4572Fingerprint();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001386 AddLine(os.str(), message);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001387
1388 // Inserting setup attribute.
1389 if (transport_info->description.connection_role !=
1390 cricket::CONNECTIONROLE_NONE) {
1391 // Making sure we are not using "passive" mode.
1392 cricket::ConnectionRole role =
1393 transport_info->description.connection_role;
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00001394 std::string dtls_role_str;
nissec16fa5e2017-02-07 07:18:43 -08001395 const bool success =
1396 cricket::ConnectionRoleToString(role, &dtls_role_str);
1397 RTC_DCHECK(success);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001398 InitAttrLine(kAttributeSetup, &os);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001399 os << kSdpDelimiterColon << dtls_role_str;
1400 AddLine(os.str(), message);
1401 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001402 }
1403 }
1404
1405 // RFC 3388
1406 // mid-attribute = "a=mid:" identification-tag
1407 // identification-tag = token
1408 // Use the content name as the mid identification-tag.
1409 InitAttrLine(kAttributeMid, &os);
1410 os << kSdpDelimiterColon << content_info->name;
1411 AddLine(os.str(), message);
1412
pthatcher@webrtc.org3341b402015-02-13 21:14:22 +00001413 if (IsDtlsSctp(media_desc->protocol())) {
zstein4b2e0822017-02-17 19:48:38 -08001414 const DataContentDescription* data_desc =
1415 static_cast<const DataContentDescription*>(media_desc);
1416 bool use_sctpmap = data_desc->use_sctpmap();
1417 BuildSctpContentAttributes(message, sctp_port, use_sctpmap);
pthatcher@webrtc.org3341b402015-02-13 21:14:22 +00001418 } else if (IsRtp(media_desc->protocol())) {
deadbeef9d3584c2016-02-16 17:54:10 -08001419 BuildRtpContentAttributes(media_desc, media_type, unified_plan_sdp,
1420 message);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001421 }
1422}
1423
zstein4b2e0822017-02-17 19:48:38 -08001424void BuildSctpContentAttributes(std::string* message,
1425 int sctp_port,
1426 bool use_sctpmap) {
wu@webrtc.org78187522013-10-07 23:32:02 +00001427 std::ostringstream os;
zstein4b2e0822017-02-17 19:48:38 -08001428 if (use_sctpmap) {
1429 // draft-ietf-mmusic-sctp-sdp-04
1430 // a=sctpmap:sctpmap-number protocol [streams]
1431 InitAttrLine(kAttributeSctpmap, &os);
1432 os << kSdpDelimiterColon << sctp_port << kSdpDelimiterSpace
1433 << kDefaultSctpmapProtocol << kSdpDelimiterSpace
1434 << cricket::kMaxSctpStreams;
1435 } else {
1436 // draft-ietf-mmusic-sctp-sdp-23
1437 // a=sctp-port:<port>
1438 InitAttrLine(kAttributeSctpPort, &os);
1439 os << kSdpDelimiterColon << sctp_port;
1440 // TODO(zstein): emit max-message-size here
1441 }
wu@webrtc.org78187522013-10-07 23:32:02 +00001442 AddLine(os.str(), message);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001443}
1444
deadbeef9d3584c2016-02-16 17:54:10 -08001445// If unified_plan_sdp is true, will use "a=msid".
1446void BuildRtpContentAttributes(const MediaContentDescription* media_desc,
1447 const MediaType media_type,
1448 bool unified_plan_sdp,
1449 std::string* message) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001450 std::ostringstream os;
1451 // RFC 5285
1452 // a=extmap:<value>["/"<direction>] <URI> <extensionattributes>
1453 // The definitions MUST be either all session level or all media level. This
1454 // implementation uses all media level.
1455 for (size_t i = 0; i < media_desc->rtp_header_extensions().size(); ++i) {
jbauch5869f502017-06-29 12:31:36 -07001456 const RtpExtension& extension = media_desc->rtp_header_extensions()[i];
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001457 InitAttrLine(kAttributeExtmap, &os);
jbauch5869f502017-06-29 12:31:36 -07001458 os << kSdpDelimiterColon << extension.id;
1459 if (extension.encrypt) {
1460 os << kSdpDelimiterSpace << RtpExtension::kEncryptHeaderExtensionsUri;
1461 }
1462 os << kSdpDelimiterSpace << extension.uri;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001463 AddLine(os.str(), message);
1464 }
1465
1466 // RFC 3264
1467 // a=sendrecv || a=sendonly || a=sendrecv || a=inactive
deadbeefc80741f2015-10-22 13:14:45 -07001468 switch (media_desc->direction()) {
Steve Anton4e70a722017-11-28 14:57:10 -08001469 case RtpTransceiverDirection::kInactive:
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001470 InitAttrLine(kAttributeInactive, &os);
1471 break;
Steve Anton4e70a722017-11-28 14:57:10 -08001472 case RtpTransceiverDirection::kSendOnly:
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001473 InitAttrLine(kAttributeSendOnly, &os);
1474 break;
Steve Anton4e70a722017-11-28 14:57:10 -08001475 case RtpTransceiverDirection::kRecvOnly:
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001476 InitAttrLine(kAttributeRecvOnly, &os);
1477 break;
Steve Anton4e70a722017-11-28 14:57:10 -08001478 case RtpTransceiverDirection::kSendRecv:
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001479 default:
1480 InitAttrLine(kAttributeSendRecv, &os);
1481 break;
1482 }
1483 AddLine(os.str(), message);
1484
deadbeef9d3584c2016-02-16 17:54:10 -08001485 // draft-ietf-mmusic-msid-11
1486 // a=msid:<stream id> <track id>
1487 if (unified_plan_sdp && !media_desc->streams().empty()) {
1488 if (media_desc->streams().size() > 1u) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01001489 RTC_LOG(LS_WARNING)
1490 << "Trying to serialize unified plan SDP with more than "
1491 << "one track in a media section. Omitting 'a=msid'.";
deadbeef9d3584c2016-02-16 17:54:10 -08001492 } else {
1493 auto track = media_desc->streams().begin();
1494 const std::string& stream_id = track->sync_label;
deadbeef9d3584c2016-02-16 17:54:10 -08001495 InitAttrLine(kAttributeMsid, &os);
1496 os << kSdpDelimiterColon << stream_id << kSdpDelimiterSpace << track->id;
1497 AddLine(os.str(), message);
1498 }
1499 }
1500
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001501 // RFC 5761
1502 // a=rtcp-mux
1503 if (media_desc->rtcp_mux()) {
1504 InitAttrLine(kAttributeRtcpMux, &os);
1505 AddLine(os.str(), message);
1506 }
1507
deadbeef13871492015-12-09 12:37:51 -08001508 // RFC 5506
1509 // a=rtcp-rsize
1510 if (media_desc->rtcp_reduced_size()) {
1511 InitAttrLine(kAttributeRtcpReducedSize, &os);
1512 AddLine(os.str(), message);
1513 }
1514
deadbeefd45aea82017-09-16 01:24:29 -07001515 if (media_desc->conference_mode()) {
1516 InitAttrLine(kAttributeXGoogleFlag, &os);
1517 os << kSdpDelimiterColon << kValueConference;
1518 AddLine(os.str(), message);
1519 }
1520
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001521 // RFC 4568
1522 // a=crypto:<tag> <crypto-suite> <key-params> [<session-params>]
1523 for (std::vector<CryptoParams>::const_iterator it =
1524 media_desc->cryptos().begin();
1525 it != media_desc->cryptos().end(); ++it) {
1526 InitAttrLine(kAttributeCrypto, &os);
1527 os << kSdpDelimiterColon << it->tag << " " << it->cipher_suite << " "
1528 << it->key_params;
1529 if (!it->session_params.empty()) {
1530 os << " " << it->session_params;
1531 }
1532 AddLine(os.str(), message);
1533 }
1534
1535 // RFC 4566
1536 // a=rtpmap:<payload type> <encoding name>/<clock rate>
1537 // [/<encodingparameters>]
1538 BuildRtpMap(media_desc, media_type, message);
1539
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001540 for (StreamParamsVec::const_iterator track = media_desc->streams().begin();
1541 track != media_desc->streams().end(); ++track) {
1542 // Require that the track belongs to a media stream,
1543 // ie the sync_label is set. This extra check is necessary since the
1544 // MediaContentDescription always contains a streamparam with an ssrc even
1545 // if no track or media stream have been created.
1546 if (track->sync_label.empty()) continue;
1547
1548 // Build the ssrc-group lines.
1549 for (size_t i = 0; i < track->ssrc_groups.size(); ++i) {
1550 // RFC 5576
1551 // a=ssrc-group:<semantics> <ssrc-id> ...
1552 if (track->ssrc_groups[i].ssrcs.empty()) {
1553 continue;
1554 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001555 InitAttrLine(kAttributeSsrcGroup, &os);
1556 os << kSdpDelimiterColon << track->ssrc_groups[i].semantics;
Peter Boström0c4e06b2015-10-07 12:23:21 +02001557 std::vector<uint32_t>::const_iterator ssrc =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001558 track->ssrc_groups[i].ssrcs.begin();
1559 for (; ssrc != track->ssrc_groups[i].ssrcs.end(); ++ssrc) {
Peter Boström0c4e06b2015-10-07 12:23:21 +02001560 os << kSdpDelimiterSpace << rtc::ToString<uint32_t>(*ssrc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001561 }
1562 AddLine(os.str(), message);
1563 }
1564 // Build the ssrc lines for each ssrc.
1565 for (size_t i = 0; i < track->ssrcs.size(); ++i) {
Peter Boström0c4e06b2015-10-07 12:23:21 +02001566 uint32_t ssrc = track->ssrcs[i];
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001567 // RFC 5576
1568 // a=ssrc:<ssrc-id> cname:<value>
1569 AddSsrcLine(ssrc, kSsrcAttributeCname,
1570 track->cname, message);
1571
1572 // draft-alvestrand-mmusic-msid-00
1573 // a=ssrc:<ssrc-id> msid:identifier [appdata]
deadbeef9d3584c2016-02-16 17:54:10 -08001574 // The appdata consists of the "id" attribute of a MediaStreamTrack,
1575 // which corresponds to the "id" attribute of StreamParams.
1576 const std::string& stream_id = track->sync_label;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001577 InitAttrLine(kAttributeSsrc, &os);
1578 os << kSdpDelimiterColon << ssrc << kSdpDelimiterSpace
deadbeef9d3584c2016-02-16 17:54:10 -08001579 << kSsrcAttributeMsid << kSdpDelimiterColon << stream_id
1580 << kSdpDelimiterSpace << track->id;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001581 AddLine(os.str(), message);
1582
deadbeef9d3584c2016-02-16 17:54:10 -08001583 // TODO(ronghuawu): Remove below code which is for backward
1584 // compatibility.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001585 // draft-alvestrand-rtcweb-mid-01
1586 // a=ssrc:<ssrc-id> mslabel:<value>
1587 // The label isn't yet defined.
1588 // a=ssrc:<ssrc-id> label:<value>
1589 AddSsrcLine(ssrc, kSsrcAttributeMslabel, track->sync_label, message);
1590 AddSsrcLine(ssrc, kSSrcAttributeLabel, track->id, message);
1591 }
1592 }
1593}
1594
1595void WriteFmtpHeader(int payload_type, std::ostringstream* os) {
1596 // fmtp header: a=fmtp:|payload_type| <parameters>
1597 // Add a=fmtp
1598 InitAttrLine(kAttributeFmtp, os);
1599 // Add :|payload_type|
1600 *os << kSdpDelimiterColon << payload_type;
1601}
1602
1603void WriteRtcpFbHeader(int payload_type, std::ostringstream* os) {
1604 // rtcp-fb header: a=rtcp-fb:|payload_type|
1605 // <parameters>/<ccm <ccm_parameters>>
1606 // Add a=rtcp-fb
1607 InitAttrLine(kAttributeRtcpFb, os);
1608 // Add :
1609 *os << kSdpDelimiterColon;
1610 if (payload_type == kWildcardPayloadType) {
1611 *os << "*";
1612 } else {
1613 *os << payload_type;
1614 }
1615}
1616
1617void WriteFmtpParameter(const std::string& parameter_name,
1618 const std::string& parameter_value,
1619 std::ostringstream* os) {
1620 // fmtp parameters: |parameter_name|=|parameter_value|
1621 *os << parameter_name << kSdpDelimiterEqual << parameter_value;
1622}
1623
1624void WriteFmtpParameters(const cricket::CodecParameterMap& parameters,
1625 std::ostringstream* os) {
1626 for (cricket::CodecParameterMap::const_iterator fmtp = parameters.begin();
1627 fmtp != parameters.end(); ++fmtp) {
hta62a216e2016-04-15 11:02:14 -07001628 // Parameters are a semicolon-separated list, no spaces.
1629 // The list is separated from the header by a space.
1630 if (fmtp == parameters.begin()) {
1631 *os << kSdpDelimiterSpace;
1632 } else {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001633 *os << kSdpDelimiterSemicolon;
1634 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001635 WriteFmtpParameter(fmtp->first, fmtp->second, os);
1636 }
1637}
1638
1639bool IsFmtpParam(const std::string& name) {
ossuaa4b0772017-01-30 07:41:18 -08001640 // RFC 4855, section 3 specifies the mapping of media format parameters to SDP
1641 // parameters. Only ptime, maxptime, channels and rate are placed outside of
1642 // the fmtp line. In WebRTC, channels and rate are already handled separately
1643 // and thus not included in the CodecParameterMap.
1644 return name != kCodecParamPTime && name != kCodecParamMaxPTime;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001645}
1646
1647// Retreives fmtp parameters from |params|, which may contain other parameters
1648// as well, and puts them in |fmtp_parameters|.
1649void GetFmtpParams(const cricket::CodecParameterMap& params,
1650 cricket::CodecParameterMap* fmtp_parameters) {
1651 for (cricket::CodecParameterMap::const_iterator iter = params.begin();
1652 iter != params.end(); ++iter) {
1653 if (IsFmtpParam(iter->first)) {
1654 (*fmtp_parameters)[iter->first] = iter->second;
1655 }
1656 }
1657}
1658
1659template <class T>
1660void AddFmtpLine(const T& codec, std::string* message) {
1661 cricket::CodecParameterMap fmtp_parameters;
1662 GetFmtpParams(codec.params, &fmtp_parameters);
1663 if (fmtp_parameters.empty()) {
1664 // No need to add an fmtp if it will have no (optional) parameters.
1665 return;
1666 }
1667 std::ostringstream os;
1668 WriteFmtpHeader(codec.id, &os);
1669 WriteFmtpParameters(fmtp_parameters, &os);
1670 AddLine(os.str(), message);
1671 return;
1672}
1673
1674template <class T>
1675void AddRtcpFbLines(const T& codec, std::string* message) {
1676 for (std::vector<cricket::FeedbackParam>::const_iterator iter =
1677 codec.feedback_params.params().begin();
1678 iter != codec.feedback_params.params().end(); ++iter) {
1679 std::ostringstream os;
1680 WriteRtcpFbHeader(codec.id, &os);
1681 os << " " << iter->id();
1682 if (!iter->param().empty()) {
1683 os << " " << iter->param();
1684 }
1685 AddLine(os.str(), message);
1686 }
1687}
1688
jiayl@webrtc.orgddb85ab2014-09-05 16:31:56 +00001689bool AddSctpDataCodec(DataContentDescription* media_desc,
1690 int sctp_port) {
solenberg9fa49752016-10-08 13:02:44 -07001691 for (const auto& codec : media_desc->codecs()) {
1692 if (cricket::CodecNamesEq(codec.name, cricket::kGoogleSctpDataCodecName)) {
1693 return ParseFailed("",
1694 "Can't have multiple sctp port attributes.",
1695 NULL);
1696 }
jiayl@webrtc.orgddb85ab2014-09-05 16:31:56 +00001697 }
1698 // Add the SCTP Port number as a pseudo-codec "port" parameter
solenberg9fa49752016-10-08 13:02:44 -07001699 cricket::DataCodec codec_port(cricket::kGoogleSctpDataCodecPlType,
deadbeef67cf2c12016-04-13 10:07:16 -07001700 cricket::kGoogleSctpDataCodecName);
jiayl@webrtc.orgddb85ab2014-09-05 16:31:56 +00001701 codec_port.SetParam(cricket::kCodecParamPort, sctp_port);
Mirko Bonadei675513b2017-11-09 11:09:25 +01001702 RTC_LOG(INFO) << "AddSctpDataCodec: Got SCTP Port Number " << sctp_port;
jiayl@webrtc.orgddb85ab2014-09-05 16:31:56 +00001703 media_desc->AddCodec(codec_port);
1704 return true;
1705}
1706
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001707bool GetMinValue(const std::vector<int>& values, int* value) {
1708 if (values.empty()) {
1709 return false;
1710 }
1711 std::vector<int>::const_iterator found =
1712 std::min_element(values.begin(), values.end());
1713 *value = *found;
1714 return true;
1715}
1716
1717bool GetParameter(const std::string& name,
1718 const cricket::CodecParameterMap& params, int* value) {
1719 std::map<std::string, std::string>::const_iterator found =
1720 params.find(name);
1721 if (found == params.end()) {
1722 return false;
1723 }
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001724 if (!rtc::FromString(found->second, value)) {
wu@webrtc.org5e760e72014-04-02 23:19:09 +00001725 return false;
1726 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001727 return true;
1728}
1729
1730void BuildRtpMap(const MediaContentDescription* media_desc,
1731 const MediaType media_type,
1732 std::string* message) {
nisseede5da42017-01-12 05:15:36 -08001733 RTC_DCHECK(message != NULL);
1734 RTC_DCHECK(media_desc != NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001735 std::ostringstream os;
1736 if (media_type == cricket::MEDIA_TYPE_VIDEO) {
1737 const VideoContentDescription* video_desc =
1738 static_cast<const VideoContentDescription*>(media_desc);
1739 for (std::vector<cricket::VideoCodec>::const_iterator it =
1740 video_desc->codecs().begin();
1741 it != video_desc->codecs().end(); ++it) {
1742 // RFC 4566
1743 // a=rtpmap:<payload type> <encoding name>/<clock rate>
1744 // [/<encodingparameters>]
1745 if (it->id != kWildcardPayloadType) {
1746 InitAttrLine(kAttributeRtpmap, &os);
deadbeefe814a0d2017-02-25 18:15:09 -08001747 os << kSdpDelimiterColon << it->id << " " << it->name << "/"
1748 << cricket::kVideoCodecClockrate;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001749 AddLine(os.str(), message);
1750 }
1751 AddRtcpFbLines(*it, message);
1752 AddFmtpLine(*it, message);
1753 }
1754 } else if (media_type == cricket::MEDIA_TYPE_AUDIO) {
1755 const AudioContentDescription* audio_desc =
1756 static_cast<const AudioContentDescription*>(media_desc);
1757 std::vector<int> ptimes;
1758 std::vector<int> maxptimes;
1759 int max_minptime = 0;
1760 for (std::vector<cricket::AudioCodec>::const_iterator it =
1761 audio_desc->codecs().begin();
1762 it != audio_desc->codecs().end(); ++it) {
nisseede5da42017-01-12 05:15:36 -08001763 RTC_DCHECK(!it->name.empty());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001764 // RFC 4566
1765 // a=rtpmap:<payload type> <encoding name>/<clock rate>
1766 // [/<encodingparameters>]
1767 InitAttrLine(kAttributeRtpmap, &os);
1768 os << kSdpDelimiterColon << it->id << " ";
1769 os << it->name << "/" << it->clockrate;
1770 if (it->channels != 1) {
1771 os << "/" << it->channels;
1772 }
1773 AddLine(os.str(), message);
1774 AddRtcpFbLines(*it, message);
1775 AddFmtpLine(*it, message);
1776 int minptime = 0;
1777 if (GetParameter(kCodecParamMinPTime, it->params, &minptime)) {
1778 max_minptime = std::max(minptime, max_minptime);
1779 }
1780 int ptime;
1781 if (GetParameter(kCodecParamPTime, it->params, &ptime)) {
1782 ptimes.push_back(ptime);
1783 }
1784 int maxptime;
1785 if (GetParameter(kCodecParamMaxPTime, it->params, &maxptime)) {
1786 maxptimes.push_back(maxptime);
1787 }
1788 }
1789 // Populate the maxptime attribute with the smallest maxptime of all codecs
1790 // under the same m-line.
1791 int min_maxptime = INT_MAX;
1792 if (GetMinValue(maxptimes, &min_maxptime)) {
1793 AddAttributeLine(kCodecParamMaxPTime, min_maxptime, message);
1794 }
nisseede5da42017-01-12 05:15:36 -08001795 RTC_DCHECK(min_maxptime > max_minptime);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001796 // Populate the ptime attribute with the smallest ptime or the largest
1797 // minptime, whichever is the largest, for all codecs under the same m-line.
1798 int ptime = INT_MAX;
1799 if (GetMinValue(ptimes, &ptime)) {
1800 ptime = std::min(ptime, min_maxptime);
1801 ptime = std::max(ptime, max_minptime);
1802 AddAttributeLine(kCodecParamPTime, ptime, message);
1803 }
1804 } else if (media_type == cricket::MEDIA_TYPE_DATA) {
1805 const DataContentDescription* data_desc =
1806 static_cast<const DataContentDescription*>(media_desc);
1807 for (std::vector<cricket::DataCodec>::const_iterator it =
1808 data_desc->codecs().begin();
1809 it != data_desc->codecs().end(); ++it) {
1810 // RFC 4566
1811 // a=rtpmap:<payload type> <encoding name>/<clock rate>
1812 // [/<encodingparameters>]
1813 InitAttrLine(kAttributeRtpmap, &os);
1814 os << kSdpDelimiterColon << it->id << " "
1815 << it->name << "/" << it->clockrate;
1816 AddLine(os.str(), message);
1817 }
1818 }
1819}
1820
1821void BuildCandidate(const std::vector<Candidate>& candidates,
honghaiza54a0802015-12-16 18:37:23 -08001822 bool include_ufrag,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001823 std::string* message) {
1824 std::ostringstream os;
1825
1826 for (std::vector<Candidate>::const_iterator it = candidates.begin();
1827 it != candidates.end(); ++it) {
1828 // RFC 5245
1829 // a=candidate:<foundation> <component-id> <transport> <priority>
1830 // <connection-address> <port> typ <candidate-types>
1831 // [raddr <connection-address>] [rport <port>]
1832 // *(SP extension-att-name SP extension-att-value)
1833 std::string type;
1834 // Map the cricket candidate type to "host" / "srflx" / "prflx" / "relay"
1835 if (it->type() == cricket::LOCAL_PORT_TYPE) {
1836 type = kCandidateHost;
1837 } else if (it->type() == cricket::STUN_PORT_TYPE) {
1838 type = kCandidateSrflx;
1839 } else if (it->type() == cricket::RELAY_PORT_TYPE) {
1840 type = kCandidateRelay;
Honghai Zhang7fb69db2016-03-14 11:59:18 -07001841 } else if (it->type() == cricket::PRFLX_PORT_TYPE) {
1842 type = kCandidatePrflx;
1843 // Peer reflexive candidate may be signaled for being removed.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001844 } else {
nissec80e7412017-01-11 05:56:46 -08001845 RTC_NOTREACHED();
Peter Thatcher019087f2015-04-28 09:06:26 -07001846 // Never write out candidates if we don't know the type.
1847 continue;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001848 }
1849
1850 InitAttrLine(kAttributeCandidate, &os);
1851 os << kSdpDelimiterColon
mallinath@webrtc.org2d60c5e2014-08-08 22:29:20 +00001852 << it->foundation() << " "
1853 << it->component() << " "
1854 << it->protocol() << " "
1855 << it->priority() << " "
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001856 << it->address().ipaddr().ToString() << " "
1857 << it->address().PortAsString() << " "
mallinath@webrtc.org2d60c5e2014-08-08 22:29:20 +00001858 << kAttributeCandidateTyp << " "
1859 << type << " ";
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001860
1861 // Related address
1862 if (!it->related_address().IsNil()) {
1863 os << kAttributeCandidateRaddr << " "
1864 << it->related_address().ipaddr().ToString() << " "
1865 << kAttributeCandidateRport << " "
1866 << it->related_address().PortAsString() << " ";
1867 }
1868
mallinath@webrtc.org2d60c5e2014-08-08 22:29:20 +00001869 if (it->protocol() == cricket::TCP_PROTOCOL_NAME) {
mallinath@webrtc.orge999bd02014-08-13 06:05:55 +00001870 os << kTcpCandidateType << " " << it->tcptype() << " ";
mallinath@webrtc.org2d60c5e2014-08-08 22:29:20 +00001871 }
1872
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001873 // Extensions
1874 os << kAttributeCandidateGeneration << " " << it->generation();
honghaiza54a0802015-12-16 18:37:23 -08001875 if (include_ufrag && !it->username().empty()) {
1876 os << " " << kAttributeCandidateUfrag << " " << it->username();
1877 }
honghaiza0c44ea2016-03-23 16:07:48 -07001878 if (it->network_id() > 0) {
1879 os << " " << kAttributeCandidateNetworkId << " " << it->network_id();
1880 }
honghaize1a0c942016-02-16 14:54:56 -08001881 if (it->network_cost() > 0) {
1882 os << " " << kAttributeCandidateNetworkCost << " " << it->network_cost();
1883 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001884
1885 AddLine(os.str(), message);
1886 }
1887}
1888
1889void BuildIceOptions(const std::vector<std::string>& transport_options,
1890 std::string* message) {
1891 if (!transport_options.empty()) {
1892 std::ostringstream os;
1893 InitAttrLine(kAttributeIceOption, &os);
1894 os << kSdpDelimiterColon << transport_options[0];
1895 for (size_t i = 1; i < transport_options.size(); ++i) {
1896 os << kSdpDelimiterSpace << transport_options[i];
1897 }
1898 AddLine(os.str(), message);
1899 }
1900}
1901
pthatcher@webrtc.org3341b402015-02-13 21:14:22 +00001902bool IsRtp(const std::string& protocol) {
1903 return protocol.empty() ||
1904 (protocol.find(cricket::kMediaProtocolRtpPrefix) != std::string::npos);
1905}
1906
1907bool IsDtlsSctp(const std::string& protocol) {
1908 // This intentionally excludes "SCTP" and "SCTP/DTLS".
lally@webrtc.orga7470932015-02-24 20:19:21 +00001909 return protocol.find(cricket::kMediaProtocolDtlsSctp) != std::string::npos;
pthatcher@webrtc.org3341b402015-02-13 21:14:22 +00001910}
1911
zhihuang38989e52017-03-21 11:04:53 -07001912bool ParseConnectionData(const std::string& line,
1913 rtc::SocketAddress* addr,
1914 SdpParseError* error) {
1915 // Parse the line from left to right.
1916 std::string token;
1917 std::string rightpart;
1918 // RFC 4566
1919 // c=<nettype> <addrtype> <connection-address>
1920 // Skip the "c="
1921 if (!rtc::tokenize_first(line, kSdpDelimiterEqual, &token, &rightpart)) {
1922 return ParseFailed(line, "Failed to parse the network type.", error);
1923 }
1924
1925 // Extract and verify the <nettype>
1926 if (!rtc::tokenize_first(rightpart, kSdpDelimiterSpace, &token, &rightpart) ||
1927 token != kConnectionNettype) {
1928 return ParseFailed(line,
1929 "Failed to parse the connection data. The network type "
1930 "is not currently supported.",
1931 error);
1932 }
1933
1934 // Extract the "<addrtype>" and "<connection-address>".
1935 if (!rtc::tokenize_first(rightpart, kSdpDelimiterSpace, &token, &rightpart)) {
1936 return ParseFailed(line, "Failed to parse the address type.", error);
1937 }
1938
1939 // The rightpart part should be the IP address without the slash which is used
1940 // for multicast.
1941 if (rightpart.find('/') != std::string::npos) {
1942 return ParseFailed(line,
1943 "Failed to parse the connection data. Multicast is not "
1944 "currently supported.",
1945 error);
1946 }
1947 addr->SetIP(rightpart);
1948
1949 // Verify that the addrtype matches the type of the parsed address.
1950 if ((addr->family() == AF_INET && token != "IP4") ||
1951 (addr->family() == AF_INET6 && token != "IP6")) {
1952 addr->Clear();
1953 return ParseFailed(
1954 line,
1955 "Failed to parse the connection data. The address type is mismatching.",
1956 error);
1957 }
1958 return true;
1959}
1960
1961bool ParseSessionDescription(const std::string& message,
1962 size_t* pos,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001963 std::string* session_id,
1964 std::string* session_version,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001965 TransportDescription* session_td,
1966 RtpHeaderExtensions* session_extmaps,
zhihuang38989e52017-03-21 11:04:53 -07001967 rtc::SocketAddress* connection_addr,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001968 cricket::SessionDescription* desc,
1969 SdpParseError* error) {
1970 std::string line;
1971
deadbeefc80741f2015-10-22 13:14:45 -07001972 desc->set_msid_supported(false);
1973
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001974 // RFC 4566
1975 // v= (protocol version)
1976 if (!GetLineWithType(message, pos, &line, kLineTypeVersion)) {
1977 return ParseFailedExpectLine(message, *pos, kLineTypeVersion,
1978 std::string(), error);
1979 }
1980 // RFC 4566
1981 // o=<username> <sess-id> <sess-version> <nettype> <addrtype>
1982 // <unicast-address>
1983 if (!GetLineWithType(message, pos, &line, kLineTypeOrigin)) {
1984 return ParseFailedExpectLine(message, *pos, kLineTypeOrigin,
1985 std::string(), error);
1986 }
1987 std::vector<std::string> fields;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001988 rtc::split(line.substr(kLinePrefixLength),
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001989 kSdpDelimiterSpace, &fields);
1990 const size_t expected_fields = 6;
1991 if (fields.size() != expected_fields) {
1992 return ParseFailedExpectFieldNum(line, expected_fields, error);
1993 }
1994 *session_id = fields[1];
1995 *session_version = fields[2];
1996
1997 // RFC 4566
1998 // s= (session name)
1999 if (!GetLineWithType(message, pos, &line, kLineTypeSessionName)) {
2000 return ParseFailedExpectLine(message, *pos, kLineTypeSessionName,
2001 std::string(), error);
2002 }
2003
2004 // Optional lines
2005 // Those are the optional lines, so shouldn't return false if not present.
2006 // RFC 4566
2007 // i=* (session information)
2008 GetLineWithType(message, pos, &line, kLineTypeSessionInfo);
2009
2010 // RFC 4566
2011 // u=* (URI of description)
2012 GetLineWithType(message, pos, &line, kLineTypeSessionUri);
2013
2014 // RFC 4566
2015 // e=* (email address)
2016 GetLineWithType(message, pos, &line, kLineTypeSessionEmail);
2017
2018 // RFC 4566
2019 // p=* (phone number)
2020 GetLineWithType(message, pos, &line, kLineTypeSessionPhone);
2021
2022 // RFC 4566
2023 // c=* (connection information -- not required if included in
2024 // all media)
zhihuang38989e52017-03-21 11:04:53 -07002025 if (GetLineWithType(message, pos, &line, kLineTypeConnection)) {
2026 if (!ParseConnectionData(line, connection_addr, error)) {
2027 return false;
2028 }
2029 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002030
2031 // RFC 4566
2032 // b=* (zero or more bandwidth information lines)
2033 while (GetLineWithType(message, pos, &line, kLineTypeSessionBandwidth)) {
2034 // By pass zero or more b lines.
2035 }
2036
2037 // RFC 4566
2038 // One or more time descriptions ("t=" and "r=" lines; see below)
2039 // t= (time the session is active)
2040 // r=* (zero or more repeat times)
2041 // Ensure there's at least one time description
2042 if (!GetLineWithType(message, pos, &line, kLineTypeTiming)) {
2043 return ParseFailedExpectLine(message, *pos, kLineTypeTiming, std::string(),
2044 error);
2045 }
2046
2047 while (GetLineWithType(message, pos, &line, kLineTypeRepeatTimes)) {
2048 // By pass zero or more r lines.
2049 }
2050
2051 // Go through the rest of the time descriptions
2052 while (GetLineWithType(message, pos, &line, kLineTypeTiming)) {
2053 while (GetLineWithType(message, pos, &line, kLineTypeRepeatTimes)) {
2054 // By pass zero or more r lines.
2055 }
2056 }
2057
2058 // RFC 4566
2059 // z=* (time zone adjustments)
2060 GetLineWithType(message, pos, &line, kLineTypeTimeZone);
2061
2062 // RFC 4566
2063 // k=* (encryption key)
2064 GetLineWithType(message, pos, &line, kLineTypeEncryptionKey);
2065
2066 // RFC 4566
2067 // a=* (zero or more session attribute lines)
2068 while (GetLineWithType(message, pos, &line, kLineTypeAttributes)) {
2069 if (HasAttribute(line, kAttributeGroup)) {
2070 if (!ParseGroupAttribute(line, desc, error)) {
2071 return false;
2072 }
2073 } else if (HasAttribute(line, kAttributeIceUfrag)) {
2074 if (!GetValue(line, kAttributeIceUfrag,
2075 &(session_td->ice_ufrag), error)) {
2076 return false;
2077 }
2078 } else if (HasAttribute(line, kAttributeIcePwd)) {
2079 if (!GetValue(line, kAttributeIcePwd, &(session_td->ice_pwd), error)) {
2080 return false;
2081 }
2082 } else if (HasAttribute(line, kAttributeIceLite)) {
2083 session_td->ice_mode = cricket::ICEMODE_LITE;
2084 } else if (HasAttribute(line, kAttributeIceOption)) {
2085 if (!ParseIceOptions(line, &(session_td->transport_options), error)) {
2086 return false;
2087 }
2088 } else if (HasAttribute(line, kAttributeFingerprint)) {
2089 if (session_td->identity_fingerprint.get()) {
2090 return ParseFailed(
2091 line,
2092 "Can't have multiple fingerprint attributes at the same level.",
2093 error);
2094 }
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002095 rtc::SSLFingerprint* fingerprint = NULL;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002096 if (!ParseFingerprintAttribute(line, &fingerprint, error)) {
2097 return false;
2098 }
2099 session_td->identity_fingerprint.reset(fingerprint);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00002100 } else if (HasAttribute(line, kAttributeSetup)) {
2101 if (!ParseDtlsSetup(line, &(session_td->connection_role), error)) {
2102 return false;
2103 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002104 } else if (HasAttribute(line, kAttributeMsidSemantics)) {
2105 std::string semantics;
2106 if (!GetValue(line, kAttributeMsidSemantics, &semantics, error)) {
2107 return false;
2108 }
deadbeefc80741f2015-10-22 13:14:45 -07002109 desc->set_msid_supported(
2110 CaseInsensitiveFind(semantics, kMediaStreamSemantic));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002111 } else if (HasAttribute(line, kAttributeExtmap)) {
isheriff6f8d6862016-05-26 11:24:55 -07002112 RtpExtension extmap;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002113 if (!ParseExtmap(line, &extmap, error)) {
2114 return false;
2115 }
2116 session_extmaps->push_back(extmap);
2117 }
2118 }
2119
2120 return true;
2121}
2122
2123bool ParseGroupAttribute(const std::string& line,
2124 cricket::SessionDescription* desc,
2125 SdpParseError* error) {
nisseede5da42017-01-12 05:15:36 -08002126 RTC_DCHECK(desc != NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002127
2128 // RFC 5888 and draft-holmberg-mmusic-sdp-bundle-negotiation-00
2129 // a=group:BUNDLE video voice
2130 std::vector<std::string> fields;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002131 rtc::split(line.substr(kLinePrefixLength),
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002132 kSdpDelimiterSpace, &fields);
2133 std::string semantics;
2134 if (!GetValue(fields[0], kAttributeGroup, &semantics, error)) {
2135 return false;
2136 }
2137 cricket::ContentGroup group(semantics);
2138 for (size_t i = 1; i < fields.size(); ++i) {
2139 group.AddContentName(fields[i]);
2140 }
2141 desc->AddGroup(group);
2142 return true;
2143}
2144
2145static bool ParseFingerprintAttribute(const std::string& line,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002146 rtc::SSLFingerprint** fingerprint,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002147 SdpParseError* error) {
2148 if (!IsLineType(line, kLineTypeAttributes) ||
2149 !HasAttribute(line, kAttributeFingerprint)) {
2150 return ParseFailedExpectLine(line, 0, kLineTypeAttributes,
2151 kAttributeFingerprint, error);
2152 }
2153
2154 std::vector<std::string> fields;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002155 rtc::split(line.substr(kLinePrefixLength),
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002156 kSdpDelimiterSpace, &fields);
2157 const size_t expected_fields = 2;
2158 if (fields.size() != expected_fields) {
2159 return ParseFailedExpectFieldNum(line, expected_fields, error);
2160 }
2161
2162 // The first field here is "fingerprint:<hash>.
2163 std::string algorithm;
2164 if (!GetValue(fields[0], kAttributeFingerprint, &algorithm, error)) {
2165 return false;
2166 }
2167
2168 // Downcase the algorithm. Note that we don't need to downcase the
2169 // fingerprint because hex_decode can handle upper-case.
2170 std::transform(algorithm.begin(), algorithm.end(), algorithm.begin(),
2171 ::tolower);
2172
2173 // The second field is the digest value. De-hexify it.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002174 *fingerprint = rtc::SSLFingerprint::CreateFromRfc4572(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002175 algorithm, fields[1]);
2176 if (!*fingerprint) {
2177 return ParseFailed(line,
2178 "Failed to create fingerprint from the digest.",
2179 error);
2180 }
2181
2182 return true;
2183}
2184
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00002185static bool ParseDtlsSetup(const std::string& line,
2186 cricket::ConnectionRole* role,
2187 SdpParseError* error) {
2188 // setup-attr = "a=setup:" role
2189 // role = "active" / "passive" / "actpass" / "holdconn"
2190 std::vector<std::string> fields;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002191 rtc::split(line.substr(kLinePrefixLength), kSdpDelimiterColon, &fields);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00002192 const size_t expected_fields = 2;
2193 if (fields.size() != expected_fields) {
2194 return ParseFailedExpectFieldNum(line, expected_fields, error);
2195 }
2196 std::string role_str = fields[1];
2197 if (!cricket::StringToConnectionRole(role_str, role)) {
2198 return ParseFailed(line, "Invalid attribute value.", error);
2199 }
2200 return true;
2201}
2202
deadbeef9d3584c2016-02-16 17:54:10 -08002203static bool ParseMsidAttribute(const std::string& line,
2204 std::string* stream_id,
2205 std::string* track_id,
2206 SdpParseError* error) {
2207 // draft-ietf-mmusic-msid-11
2208 // a=msid:<stream id> <track id>
2209 // msid-value = msid-id [ SP msid-appdata ]
2210 // msid-id = 1*64token-char ; see RFC 4566
2211 // msid-appdata = 1*64token-char ; see RFC 4566
2212 std::string field1;
2213 if (!rtc::tokenize_first(line.substr(kLinePrefixLength), kSdpDelimiterSpace,
2214 &field1, track_id)) {
2215 const size_t expected_fields = 2;
2216 return ParseFailedExpectFieldNum(line, expected_fields, error);
2217 }
2218
deadbeefa4549d62017-02-10 17:26:22 -08002219 if (track_id->empty()) {
2220 return ParseFailed(line, "Missing track ID in msid attribute.", error);
2221 }
2222
deadbeef9d3584c2016-02-16 17:54:10 -08002223 // msid:<msid-id>
2224 if (!GetValue(field1, kAttributeMsid, stream_id, error)) {
2225 return false;
2226 }
deadbeefa4549d62017-02-10 17:26:22 -08002227 if (stream_id->empty()) {
2228 return ParseFailed(line, "Missing stream ID in msid attribute.", error);
2229 }
deadbeef9d3584c2016-02-16 17:54:10 -08002230 return true;
2231}
2232
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002233// RFC 3551
2234// PT encoding media type clock rate channels
2235// name (Hz)
2236// 0 PCMU A 8,000 1
2237// 1 reserved A
2238// 2 reserved A
2239// 3 GSM A 8,000 1
2240// 4 G723 A 8,000 1
2241// 5 DVI4 A 8,000 1
2242// 6 DVI4 A 16,000 1
2243// 7 LPC A 8,000 1
2244// 8 PCMA A 8,000 1
2245// 9 G722 A 8,000 1
2246// 10 L16 A 44,100 2
2247// 11 L16 A 44,100 1
2248// 12 QCELP A 8,000 1
2249// 13 CN A 8,000 1
2250// 14 MPA A 90,000 (see text)
2251// 15 G728 A 8,000 1
2252// 16 DVI4 A 11,025 1
2253// 17 DVI4 A 22,050 1
2254// 18 G729 A 8,000 1
2255struct StaticPayloadAudioCodec {
2256 const char* name;
2257 int clockrate;
Peter Kasting69558702016-01-12 16:26:35 -08002258 size_t channels;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002259};
2260static const StaticPayloadAudioCodec kStaticPayloadAudioCodecs[] = {
2261 { "PCMU", 8000, 1 },
2262 { "reserved", 0, 0 },
2263 { "reserved", 0, 0 },
2264 { "GSM", 8000, 1 },
2265 { "G723", 8000, 1 },
2266 { "DVI4", 8000, 1 },
2267 { "DVI4", 16000, 1 },
2268 { "LPC", 8000, 1 },
2269 { "PCMA", 8000, 1 },
2270 { "G722", 8000, 1 },
2271 { "L16", 44100, 2 },
2272 { "L16", 44100, 1 },
2273 { "QCELP", 8000, 1 },
2274 { "CN", 8000, 1 },
2275 { "MPA", 90000, 1 },
2276 { "G728", 8000, 1 },
2277 { "DVI4", 11025, 1 },
2278 { "DVI4", 22050, 1 },
2279 { "G729", 8000, 1 },
2280};
2281
2282void MaybeCreateStaticPayloadAudioCodecs(
2283 const std::vector<int>& fmts, AudioContentDescription* media_desc) {
2284 if (!media_desc) {
2285 return;
2286 }
deadbeef67cf2c12016-04-13 10:07:16 -07002287 RTC_DCHECK(media_desc->codecs().empty());
solenberg9fa49752016-10-08 13:02:44 -07002288 for (int payload_type : fmts) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002289 if (!media_desc->HasCodec(payload_type) &&
2290 payload_type >= 0 &&
kjellander3e33bfe2016-06-20 07:04:09 -07002291 static_cast<uint32_t>(payload_type) <
2292 arraysize(kStaticPayloadAudioCodecs)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002293 std::string encoding_name = kStaticPayloadAudioCodecs[payload_type].name;
2294 int clock_rate = kStaticPayloadAudioCodecs[payload_type].clockrate;
Peter Kasting69558702016-01-12 16:26:35 -08002295 size_t channels = kStaticPayloadAudioCodecs[payload_type].channels;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002296 media_desc->AddCodec(cricket::AudioCodec(payload_type, encoding_name,
deadbeef67cf2c12016-04-13 10:07:16 -07002297 clock_rate, 0, channels));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002298 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002299 }
2300}
2301
2302template <class C>
2303static C* ParseContentDescription(const std::string& message,
2304 const MediaType media_type,
2305 int mline_index,
2306 const std::string& protocol,
deadbeef67cf2c12016-04-13 10:07:16 -07002307 const std::vector<int>& payload_types,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002308 size_t* pos,
2309 std::string* content_name,
deadbeef25ed4352016-12-12 18:37:36 -08002310 bool* bundle_only,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002311 TransportDescription* transport,
2312 std::vector<JsepIceCandidate*>* candidates,
2313 webrtc::SdpParseError* error) {
2314 C* media_desc = new C();
2315 switch (media_type) {
2316 case cricket::MEDIA_TYPE_AUDIO:
2317 *content_name = cricket::CN_AUDIO;
2318 break;
2319 case cricket::MEDIA_TYPE_VIDEO:
2320 *content_name = cricket::CN_VIDEO;
2321 break;
2322 case cricket::MEDIA_TYPE_DATA:
2323 *content_name = cricket::CN_DATA;
2324 break;
2325 default:
nissec80e7412017-01-11 05:56:46 -08002326 RTC_NOTREACHED();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002327 break;
2328 }
deadbeef67cf2c12016-04-13 10:07:16 -07002329 if (!ParseContent(message, media_type, mline_index, protocol, payload_types,
deadbeef25ed4352016-12-12 18:37:36 -08002330 pos, content_name, bundle_only, media_desc, transport,
2331 candidates, error)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002332 delete media_desc;
zhihuang38989e52017-03-21 11:04:53 -07002333 return nullptr;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002334 }
2335 // Sort the codecs according to the m-line fmt list.
deadbeef67cf2c12016-04-13 10:07:16 -07002336 std::unordered_map<int, int> payload_type_preferences;
2337 // "size + 1" so that the lowest preference payload type has a preference of
2338 // 1, which is greater than the default (0) for payload types not in the fmt
2339 // list.
2340 int preference = static_cast<int>(payload_types.size() + 1);
2341 for (int pt : payload_types) {
2342 payload_type_preferences[pt] = preference--;
2343 }
2344 std::vector<typename C::CodecType> codecs = media_desc->codecs();
2345 std::sort(codecs.begin(), codecs.end(), [&payload_type_preferences](
2346 const typename C::CodecType& a,
2347 const typename C::CodecType& b) {
2348 return payload_type_preferences[a.id] > payload_type_preferences[b.id];
2349 });
2350 media_desc->set_codecs(codecs);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002351 return media_desc;
2352}
2353
2354bool ParseMediaDescription(const std::string& message,
2355 const TransportDescription& session_td,
2356 const RtpHeaderExtensions& session_extmaps,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002357 size_t* pos,
zhihuang38989e52017-03-21 11:04:53 -07002358 const rtc::SocketAddress& session_connection_addr,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002359 cricket::SessionDescription* desc,
2360 std::vector<JsepIceCandidate*>* candidates,
2361 SdpParseError* error) {
nisseede5da42017-01-12 05:15:36 -08002362 RTC_DCHECK(desc != NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002363 std::string line;
2364 int mline_index = -1;
2365
2366 // Zero or more media descriptions
2367 // RFC 4566
2368 // m=<media> <port> <proto> <fmt>
2369 while (GetLineWithType(message, pos, &line, kLineTypeMedia)) {
2370 ++mline_index;
2371
2372 std::vector<std::string> fields;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002373 rtc::split(line.substr(kLinePrefixLength),
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002374 kSdpDelimiterSpace, &fields);
zstein4b2e0822017-02-17 19:48:38 -08002375
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002376 const size_t expected_min_fields = 4;
2377 if (fields.size() < expected_min_fields) {
2378 return ParseFailedExpectMinFieldNum(line, expected_min_fields, error);
2379 }
deadbeef25ed4352016-12-12 18:37:36 -08002380 bool port_rejected = false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002381 // RFC 3264
2382 // To reject an offered stream, the port number in the corresponding stream
2383 // in the answer MUST be set to zero.
2384 if (fields[1] == kMediaPortRejected) {
deadbeef25ed4352016-12-12 18:37:36 -08002385 port_rejected = true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002386 }
2387
zhihuang38989e52017-03-21 11:04:53 -07002388 int port = 0;
2389 if (!rtc::FromString<int>(fields[1], &port) || !IsValidPort(port)) {
2390 return ParseFailed(line, "The port number is invalid", error);
2391 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002392 std::string protocol = fields[2];
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002393
2394 // <fmt>
deadbeef67cf2c12016-04-13 10:07:16 -07002395 std::vector<int> payload_types;
pthatcher@webrtc.org3341b402015-02-13 21:14:22 +00002396 if (IsRtp(protocol)) {
jiayl@webrtc.orgddb85ab2014-09-05 16:31:56 +00002397 for (size_t j = 3 ; j < fields.size(); ++j) {
2398 // TODO(wu): Remove when below bug is fixed.
2399 // https://bugzilla.mozilla.org/show_bug.cgi?id=996329
pbosbb36fdf2015-07-09 07:48:14 -07002400 if (fields[j].empty() && j == fields.size() - 1) {
jiayl@webrtc.orgddb85ab2014-09-05 16:31:56 +00002401 continue;
2402 }
wu@webrtc.org36eda7c2014-04-15 20:37:30 +00002403
jiayl@webrtc.orgddb85ab2014-09-05 16:31:56 +00002404 int pl = 0;
pkasting@chromium.orge9facf82015-02-17 20:36:28 +00002405 if (!GetPayloadTypeFromString(line, fields[j], &pl, error)) {
jiayl@webrtc.orgddb85ab2014-09-05 16:31:56 +00002406 return false;
2407 }
deadbeef67cf2c12016-04-13 10:07:16 -07002408 payload_types.push_back(pl);
wu@webrtc.org5e760e72014-04-02 23:19:09 +00002409 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002410 }
2411
2412 // Make a temporary TransportDescription based on |session_td|.
2413 // Some of this gets overwritten by ParseContent.
deadbeef46eed762016-01-28 13:24:37 -08002414 TransportDescription transport(
2415 session_td.transport_options, session_td.ice_ufrag, session_td.ice_pwd,
2416 session_td.ice_mode, session_td.connection_role,
2417 session_td.identity_fingerprint.get());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002418
kwibergd1fe2812016-04-27 06:47:29 -07002419 std::unique_ptr<MediaContentDescription> content;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002420 std::string content_name;
deadbeef25ed4352016-12-12 18:37:36 -08002421 bool bundle_only = false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002422 if (HasAttribute(line, kMediaTypeVideo)) {
2423 content.reset(ParseContentDescription<VideoContentDescription>(
deadbeef67cf2c12016-04-13 10:07:16 -07002424 message, cricket::MEDIA_TYPE_VIDEO, mline_index, protocol,
deadbeef25ed4352016-12-12 18:37:36 -08002425 payload_types, pos, &content_name, &bundle_only, &transport,
2426 candidates, error));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002427 } else if (HasAttribute(line, kMediaTypeAudio)) {
2428 content.reset(ParseContentDescription<AudioContentDescription>(
deadbeef67cf2c12016-04-13 10:07:16 -07002429 message, cricket::MEDIA_TYPE_AUDIO, mline_index, protocol,
deadbeef25ed4352016-12-12 18:37:36 -08002430 payload_types, pos, &content_name, &bundle_only, &transport,
2431 candidates, error));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002432 } else if (HasAttribute(line, kMediaTypeData)) {
jiayl@webrtc.org0e527722014-09-08 21:43:43 +00002433 DataContentDescription* data_desc =
wu@webrtc.org78187522013-10-07 23:32:02 +00002434 ParseContentDescription<DataContentDescription>(
deadbeef67cf2c12016-04-13 10:07:16 -07002435 message, cricket::MEDIA_TYPE_DATA, mline_index, protocol,
deadbeef25ed4352016-12-12 18:37:36 -08002436 payload_types, pos, &content_name, &bundle_only, &transport,
2437 candidates, error);
jiayl@webrtc.org0e527722014-09-08 21:43:43 +00002438 content.reset(data_desc);
wu@webrtc.org78187522013-10-07 23:32:02 +00002439
zstein4b2e0822017-02-17 19:48:38 -08002440 if (data_desc && IsDtlsSctp(protocol)) {
2441 int p;
2442 if (rtc::FromString(fields[3], &p)) {
2443 if (!AddSctpDataCodec(data_desc, p)) {
2444 return false;
2445 }
2446 } else if (fields[3] == kDefaultSctpmapProtocol) {
2447 data_desc->set_use_sctpmap(false);
2448 }
wu@webrtc.org78187522013-10-07 23:32:02 +00002449 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002450 } else {
Mirko Bonadei675513b2017-11-09 11:09:25 +01002451 RTC_LOG(LS_WARNING) << "Unsupported media type: " << line;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002452 continue;
2453 }
2454 if (!content.get()) {
2455 // ParseContentDescription returns NULL if failed.
2456 return false;
2457 }
2458
deadbeef25ed4352016-12-12 18:37:36 -08002459 bool content_rejected = false;
deadbeef12771a12017-01-03 13:53:47 -08002460 // A port of 0 is not interpreted as a rejected m= section when it's
2461 // used along with a=bundle-only.
deadbeef25ed4352016-12-12 18:37:36 -08002462 if (bundle_only) {
deadbeef25ed4352016-12-12 18:37:36 -08002463 if (!port_rejected) {
deadbeef12771a12017-01-03 13:53:47 -08002464 // Usage of bundle-only with a nonzero port is unspecified. So just
2465 // ignore bundle-only if we see this.
2466 bundle_only = false;
Mirko Bonadei675513b2017-11-09 11:09:25 +01002467 RTC_LOG(LS_WARNING)
deadbeef12771a12017-01-03 13:53:47 -08002468 << "a=bundle-only attribute observed with a nonzero "
2469 << "port; this usage is unspecified so the attribute is being "
2470 << "ignored.";
deadbeef25ed4352016-12-12 18:37:36 -08002471 }
2472 } else {
2473 // If not using bundle-only, interpret port 0 in the normal way; the m=
2474 // section is being rejected.
2475 content_rejected = port_rejected;
2476 }
2477
pthatcher@webrtc.org3341b402015-02-13 21:14:22 +00002478 if (IsRtp(protocol)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002479 // Set the extmap.
2480 if (!session_extmaps.empty() &&
2481 !content->rtp_header_extensions().empty()) {
2482 return ParseFailed("",
2483 "The a=extmap MUST be either all session level or "
2484 "all media level.",
2485 error);
2486 }
2487 for (size_t i = 0; i < session_extmaps.size(); ++i) {
2488 content->AddRtpHeaderExtension(session_extmaps[i]);
2489 }
2490 }
2491 content->set_protocol(protocol);
zhihuang38989e52017-03-21 11:04:53 -07002492
2493 // Use the session level connection address if the media level addresses are
2494 // not specified.
2495 rtc::SocketAddress address;
2496 address = content->connection_address().IsNil()
2497 ? session_connection_addr
2498 : content->connection_address();
2499 address.SetPort(port);
2500 content->set_connection_address(address);
2501
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002502 desc->AddContent(content_name,
deadbeef25ed4352016-12-12 18:37:36 -08002503 IsDtlsSctp(protocol) ? cricket::NS_JINGLE_DRAFT_SCTP
2504 : cricket::NS_JINGLE_RTP,
2505 content_rejected, bundle_only, content.release());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002506 // Create TransportInfo with the media level "ice-pwd" and "ice-ufrag".
2507 TransportInfo transport_info(content_name, transport);
2508
2509 if (!desc->AddTransportInfo(transport_info)) {
2510 std::ostringstream description;
2511 description << "Failed to AddTransportInfo with content name: "
2512 << content_name;
2513 return ParseFailed("", description.str(), error);
2514 }
2515 }
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00002516
2517 size_t end_of_message = message.size();
2518 if (mline_index == -1 && *pos != end_of_message) {
2519 ParseFailed(message, *pos, "Expects m line.", error);
2520 return false;
2521 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002522 return true;
2523}
2524
2525bool VerifyCodec(const cricket::Codec& codec) {
2526 // Codec has not been populated correctly unless the name has been set. This
2527 // can happen if an SDP has an fmtp or rtcp-fb with a payload type but doesn't
2528 // have a corresponding "rtpmap" line.
htab39db842016-12-08 01:50:48 -08002529 return !codec.name.empty();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002530}
2531
2532bool VerifyAudioCodecs(const AudioContentDescription* audio_desc) {
2533 const std::vector<cricket::AudioCodec>& codecs = audio_desc->codecs();
2534 for (std::vector<cricket::AudioCodec>::const_iterator iter = codecs.begin();
2535 iter != codecs.end(); ++iter) {
2536 if (!VerifyCodec(*iter)) {
2537 return false;
2538 }
2539 }
2540 return true;
2541}
2542
2543bool VerifyVideoCodecs(const VideoContentDescription* video_desc) {
2544 const std::vector<cricket::VideoCodec>& codecs = video_desc->codecs();
2545 for (std::vector<cricket::VideoCodec>::const_iterator iter = codecs.begin();
2546 iter != codecs.end(); ++iter) {
2547 if (!VerifyCodec(*iter)) {
2548 return false;
2549 }
2550 }
2551 return true;
2552}
2553
2554void AddParameters(const cricket::CodecParameterMap& parameters,
2555 cricket::Codec* codec) {
2556 for (cricket::CodecParameterMap::const_iterator iter =
2557 parameters.begin(); iter != parameters.end(); ++iter) {
2558 codec->SetParam(iter->first, iter->second);
2559 }
2560}
2561
2562void AddFeedbackParameter(const cricket::FeedbackParam& feedback_param,
2563 cricket::Codec* codec) {
2564 codec->AddFeedbackParam(feedback_param);
2565}
2566
2567void AddFeedbackParameters(const cricket::FeedbackParams& feedback_params,
2568 cricket::Codec* codec) {
2569 for (std::vector<cricket::FeedbackParam>::const_iterator iter =
2570 feedback_params.params().begin();
2571 iter != feedback_params.params().end(); ++iter) {
2572 codec->AddFeedbackParam(*iter);
2573 }
2574}
2575
2576// Gets the current codec setting associated with |payload_type|. If there
changbin.shao@webrtc.org2d25b442015-03-16 04:14:34 +00002577// is no Codec associated with that payload type it returns an empty codec
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002578// with that payload type.
2579template <class T>
changbin.shao@webrtc.org2d25b442015-03-16 04:14:34 +00002580T GetCodecWithPayloadType(const std::vector<T>& codecs, int payload_type) {
magjedb05fa242016-11-11 04:00:16 -08002581 const T* codec = FindCodecById(codecs, payload_type);
2582 if (codec)
2583 return *codec;
2584 // Return empty codec with |payload_type|.
changbin.shao@webrtc.org2d25b442015-03-16 04:14:34 +00002585 T ret_val;
magjedb05fa242016-11-11 04:00:16 -08002586 ret_val.id = payload_type;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002587 return ret_val;
2588}
2589
2590// Updates or creates a new codec entry in the audio description.
2591template <class T, class U>
2592void AddOrReplaceCodec(MediaContentDescription* content_desc, const U& codec) {
2593 T* desc = static_cast<T*>(content_desc);
2594 std::vector<U> codecs = desc->codecs();
2595 bool found = false;
2596
2597 typename std::vector<U>::iterator iter;
2598 for (iter = codecs.begin(); iter != codecs.end(); ++iter) {
2599 if (iter->id == codec.id) {
2600 *iter = codec;
2601 found = true;
2602 break;
2603 }
2604 }
2605 if (!found) {
2606 desc->AddCodec(codec);
2607 return;
2608 }
2609 desc->set_codecs(codecs);
2610}
2611
2612// Adds or updates existing codec corresponding to |payload_type| according
2613// to |parameters|.
2614template <class T, class U>
2615void UpdateCodec(MediaContentDescription* content_desc, int payload_type,
2616 const cricket::CodecParameterMap& parameters) {
2617 // Codec might already have been populated (from rtpmap).
changbin.shao@webrtc.org2d25b442015-03-16 04:14:34 +00002618 U new_codec = GetCodecWithPayloadType(static_cast<T*>(content_desc)->codecs(),
2619 payload_type);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002620 AddParameters(parameters, &new_codec);
2621 AddOrReplaceCodec<T, U>(content_desc, new_codec);
2622}
2623
2624// Adds or updates existing codec corresponding to |payload_type| according
2625// to |feedback_param|.
2626template <class T, class U>
2627void UpdateCodec(MediaContentDescription* content_desc, int payload_type,
2628 const cricket::FeedbackParam& feedback_param) {
2629 // Codec might already have been populated (from rtpmap).
changbin.shao@webrtc.org2d25b442015-03-16 04:14:34 +00002630 U new_codec = GetCodecWithPayloadType(static_cast<T*>(content_desc)->codecs(),
2631 payload_type);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002632 AddFeedbackParameter(feedback_param, &new_codec);
2633 AddOrReplaceCodec<T, U>(content_desc, new_codec);
2634}
2635
jlmiller@webrtc.orga744a282015-02-18 21:37:46 +00002636template <class T>
2637bool PopWildcardCodec(std::vector<T>* codecs, T* wildcard_codec) {
2638 for (auto iter = codecs->begin(); iter != codecs->end(); ++iter) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002639 if (iter->id == kWildcardPayloadType) {
2640 *wildcard_codec = *iter;
2641 codecs->erase(iter);
2642 return true;
2643 }
2644 }
2645 return false;
2646}
2647
jlmiller@webrtc.orga744a282015-02-18 21:37:46 +00002648template<class T>
2649void UpdateFromWildcardCodecs(cricket::MediaContentDescriptionImpl<T>* desc) {
2650 auto codecs = desc->codecs();
2651 T wildcard_codec;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002652 if (!PopWildcardCodec(&codecs, &wildcard_codec)) {
2653 return;
2654 }
jlmiller@webrtc.orga744a282015-02-18 21:37:46 +00002655 for (auto& codec : codecs) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002656 AddFeedbackParameters(wildcard_codec.feedback_params, &codec);
2657 }
jlmiller@webrtc.orga744a282015-02-18 21:37:46 +00002658 desc->set_codecs(codecs);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002659}
2660
2661void AddAudioAttribute(const std::string& name, const std::string& value,
2662 AudioContentDescription* audio_desc) {
2663 if (value.empty()) {
2664 return;
2665 }
2666 std::vector<cricket::AudioCodec> codecs = audio_desc->codecs();
2667 for (std::vector<cricket::AudioCodec>::iterator iter = codecs.begin();
2668 iter != codecs.end(); ++iter) {
2669 iter->params[name] = value;
2670 }
2671 audio_desc->set_codecs(codecs);
2672}
2673
2674bool ParseContent(const std::string& message,
2675 const MediaType media_type,
2676 int mline_index,
2677 const std::string& protocol,
deadbeef67cf2c12016-04-13 10:07:16 -07002678 const std::vector<int>& payload_types,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002679 size_t* pos,
2680 std::string* content_name,
deadbeef25ed4352016-12-12 18:37:36 -08002681 bool* bundle_only,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002682 MediaContentDescription* media_desc,
2683 TransportDescription* transport,
2684 std::vector<JsepIceCandidate*>* candidates,
2685 SdpParseError* error) {
nisseede5da42017-01-12 05:15:36 -08002686 RTC_DCHECK(media_desc != NULL);
2687 RTC_DCHECK(content_name != NULL);
2688 RTC_DCHECK(transport != NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002689
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +00002690 if (media_type == cricket::MEDIA_TYPE_AUDIO) {
2691 MaybeCreateStaticPayloadAudioCodecs(
deadbeef67cf2c12016-04-13 10:07:16 -07002692 payload_types, static_cast<AudioContentDescription*>(media_desc));
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +00002693 }
2694
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002695 // The media level "ice-ufrag" and "ice-pwd".
2696 // The candidates before update the media level "ice-pwd" and "ice-ufrag".
2697 Candidates candidates_orig;
2698 std::string line;
2699 std::string mline_id;
2700 // Tracks created out of the ssrc attributes.
2701 StreamParamsVec tracks;
2702 SsrcInfoVec ssrc_infos;
2703 SsrcGroupVec ssrc_groups;
2704 std::string maxptime_as_string;
2705 std::string ptime_as_string;
deadbeef9d3584c2016-02-16 17:54:10 -08002706 std::string stream_id;
2707 std::string track_id;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002708
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002709 // Loop until the next m line
2710 while (!IsLineType(message, kLineTypeMedia, *pos)) {
2711 if (!GetLine(message, pos, &line)) {
2712 if (*pos >= message.size()) {
2713 break; // Done parsing
2714 } else {
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +00002715 return ParseFailed(message, *pos, "Invalid SDP line.", error);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002716 }
2717 }
2718
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002719 // RFC 4566
2720 // b=* (zero or more bandwidth information lines)
2721 if (IsLineType(line, kLineTypeSessionBandwidth)) {
2722 std::string bandwidth;
2723 if (HasAttribute(line, kApplicationSpecificMaximum)) {
2724 if (!GetValue(line, kApplicationSpecificMaximum, &bandwidth, error)) {
2725 return false;
2726 } else {
wu@webrtc.org5e760e72014-04-02 23:19:09 +00002727 int b = 0;
2728 if (!GetValueFromString(line, bandwidth, &b, error)) {
2729 return false;
2730 }
deadbeef3e8016e2017-08-03 17:49:30 -07002731 // TODO(deadbeef): Historically, applications may be setting a value
2732 // of -1 to mean "unset any previously set bandwidth limit", even
2733 // though ommitting the "b=AS" entirely will do just that. Once we've
2734 // transitioned applications to doing the right thing, it would be
2735 // better to treat this as a hard error instead of just ignoring it.
2736 if (b == -1) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01002737 RTC_LOG(LS_WARNING)
2738 << "Ignoring \"b=AS:-1\"; will be treated as \"no "
2739 "bandwidth limit\".";
deadbeef3e8016e2017-08-03 17:49:30 -07002740 continue;
2741 }
deadbeefbc88c6b2017-08-02 11:26:34 -07002742 if (b < 0) {
2743 return ParseFailed(line, "b=AS value can't be negative.", error);
2744 }
Peter Thatcherc0c3a862015-06-24 15:31:25 -07002745 // We should never use more than the default bandwidth for RTP-based
2746 // data channels. Don't allow SDP to set the bandwidth, because
2747 // that would give JS the opportunity to "break the Internet".
2748 // See: https://code.google.com/p/chromium/issues/detail?id=280726
2749 if (media_type == cricket::MEDIA_TYPE_DATA && IsRtp(protocol) &&
2750 b > cricket::kDataMaxBandwidth / 1000) {
2751 std::ostringstream description;
2752 description << "RTP-based data channels may not send more than "
2753 << cricket::kDataMaxBandwidth / 1000 << "kbps.";
2754 return ParseFailed(line, description.str(), error);
2755 }
deadbeefb2362572016-12-13 16:37:06 -08002756 // Prevent integer overflow.
2757 b = std::min(b, INT_MAX / 1000);
wu@webrtc.org5e760e72014-04-02 23:19:09 +00002758 media_desc->set_bandwidth(b * 1000);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002759 }
2760 }
2761 continue;
2762 }
2763
zhihuang38989e52017-03-21 11:04:53 -07002764 // Parse the media level connection data.
2765 if (IsLineType(line, kLineTypeConnection)) {
2766 rtc::SocketAddress addr;
2767 if (!ParseConnectionData(line, &addr, error)) {
2768 return false;
2769 }
2770 media_desc->set_connection_address(addr);
2771 continue;
2772 }
2773
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002774 if (!IsLineType(line, kLineTypeAttributes)) {
Steve Anton36b29d12017-10-30 09:57:42 -07002775 // TODO(deadbeef): Handle other lines if needed.
Mirko Bonadei675513b2017-11-09 11:09:25 +01002776 RTC_LOG(LS_INFO) << "Ignored line: " << line;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002777 continue;
2778 }
2779
2780 // Handle attributes common to SCTP and RTP.
2781 if (HasAttribute(line, kAttributeMid)) {
2782 // RFC 3388
2783 // mid-attribute = "a=mid:" identification-tag
2784 // identification-tag = token
2785 // Use the mid identification-tag as the content name.
2786 if (!GetValue(line, kAttributeMid, &mline_id, error)) {
2787 return false;
2788 }
2789 *content_name = mline_id;
deadbeef25ed4352016-12-12 18:37:36 -08002790 } else if (HasAttribute(line, kAttributeBundleOnly)) {
2791 *bundle_only = true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002792 } else if (HasAttribute(line, kAttributeCandidate)) {
2793 Candidate candidate;
2794 if (!ParseCandidate(line, &candidate, error, false)) {
2795 return false;
2796 }
deadbeef7bcdb692017-01-20 12:43:58 -08002797 // ParseCandidate will parse non-standard ufrag and password attributes,
2798 // since it's used for candidate trickling, but we only want to process
2799 // the "a=ice-ufrag"/"a=ice-pwd" values in a session description, so
2800 // strip them off at this point.
2801 candidate.set_username(std::string());
2802 candidate.set_password(std::string());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002803 candidates_orig.push_back(candidate);
2804 } else if (HasAttribute(line, kAttributeIceUfrag)) {
2805 if (!GetValue(line, kAttributeIceUfrag, &transport->ice_ufrag, error)) {
2806 return false;
2807 }
2808 } else if (HasAttribute(line, kAttributeIcePwd)) {
2809 if (!GetValue(line, kAttributeIcePwd, &transport->ice_pwd, error)) {
2810 return false;
2811 }
2812 } else if (HasAttribute(line, kAttributeIceOption)) {
2813 if (!ParseIceOptions(line, &transport->transport_options, error)) {
2814 return false;
2815 }
2816 } else if (HasAttribute(line, kAttributeFmtp)) {
2817 if (!ParseFmtpAttributes(line, media_type, media_desc, error)) {
2818 return false;
2819 }
2820 } else if (HasAttribute(line, kAttributeFingerprint)) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002821 rtc::SSLFingerprint* fingerprint = NULL;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002822
2823 if (!ParseFingerprintAttribute(line, &fingerprint, error)) {
2824 return false;
2825 }
2826 transport->identity_fingerprint.reset(fingerprint);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00002827 } else if (HasAttribute(line, kAttributeSetup)) {
2828 if (!ParseDtlsSetup(line, &(transport->connection_role), error)) {
2829 return false;
2830 }
pthatcher@webrtc.org3341b402015-02-13 21:14:22 +00002831 } else if (IsDtlsSctp(protocol) && HasAttribute(line, kAttributeSctpPort)) {
deadbeef7e146cb2016-09-28 10:04:34 -07002832 if (media_type != cricket::MEDIA_TYPE_DATA) {
2833 return ParseFailed(
2834 line, "sctp-port attribute found in non-data media description.",
2835 error);
2836 }
jiayl@webrtc.orgddb85ab2014-09-05 16:31:56 +00002837 int sctp_port;
2838 if (!ParseSctpPort(line, &sctp_port, error)) {
2839 return false;
2840 }
2841 if (!AddSctpDataCodec(static_cast<DataContentDescription*>(media_desc),
2842 sctp_port)) {
2843 return false;
2844 }
pthatcher@webrtc.org3341b402015-02-13 21:14:22 +00002845 } else if (IsRtp(protocol)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002846 //
2847 // RTP specific attrubtes
2848 //
2849 if (HasAttribute(line, kAttributeRtcpMux)) {
2850 media_desc->set_rtcp_mux(true);
deadbeef13871492015-12-09 12:37:51 -08002851 } else if (HasAttribute(line, kAttributeRtcpReducedSize)) {
2852 media_desc->set_rtcp_reduced_size(true);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002853 } else if (HasAttribute(line, kAttributeSsrcGroup)) {
2854 if (!ParseSsrcGroupAttribute(line, &ssrc_groups, error)) {
2855 return false;
2856 }
2857 } else if (HasAttribute(line, kAttributeSsrc)) {
2858 if (!ParseSsrcAttribute(line, &ssrc_infos, error)) {
2859 return false;
2860 }
2861 } else if (HasAttribute(line, kAttributeCrypto)) {
2862 if (!ParseCryptoAttribute(line, media_desc, error)) {
2863 return false;
2864 }
2865 } else if (HasAttribute(line, kAttributeRtpmap)) {
deadbeef67cf2c12016-04-13 10:07:16 -07002866 if (!ParseRtpmapAttribute(line, media_type, payload_types, media_desc,
2867 error)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002868 return false;
2869 }
2870 } else if (HasAttribute(line, kCodecParamMaxPTime)) {
2871 if (!GetValue(line, kCodecParamMaxPTime, &maxptime_as_string, error)) {
2872 return false;
2873 }
2874 } else if (HasAttribute(line, kAttributeRtcpFb)) {
2875 if (!ParseRtcpFbAttribute(line, media_type, media_desc, error)) {
2876 return false;
2877 }
2878 } else if (HasAttribute(line, kCodecParamPTime)) {
2879 if (!GetValue(line, kCodecParamPTime, &ptime_as_string, error)) {
2880 return false;
2881 }
2882 } else if (HasAttribute(line, kAttributeSendOnly)) {
Steve Anton4e70a722017-11-28 14:57:10 -08002883 media_desc->set_direction(RtpTransceiverDirection::kSendOnly);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002884 } else if (HasAttribute(line, kAttributeRecvOnly)) {
Steve Anton4e70a722017-11-28 14:57:10 -08002885 media_desc->set_direction(RtpTransceiverDirection::kRecvOnly);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002886 } else if (HasAttribute(line, kAttributeInactive)) {
Steve Anton4e70a722017-11-28 14:57:10 -08002887 media_desc->set_direction(RtpTransceiverDirection::kInactive);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002888 } else if (HasAttribute(line, kAttributeSendRecv)) {
Steve Anton4e70a722017-11-28 14:57:10 -08002889 media_desc->set_direction(RtpTransceiverDirection::kSendRecv);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002890 } else if (HasAttribute(line, kAttributeExtmap)) {
isheriff6f8d6862016-05-26 11:24:55 -07002891 RtpExtension extmap;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002892 if (!ParseExtmap(line, &extmap, error)) {
2893 return false;
2894 }
2895 media_desc->AddRtpHeaderExtension(extmap);
2896 } else if (HasAttribute(line, kAttributeXGoogleFlag)) {
2897 // Experimental attribute. Conference mode activates more aggressive
2898 // AEC and NS settings.
Steve Anton36b29d12017-10-30 09:57:42 -07002899 // TODO(deadbeef): expose API to set these directly.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002900 std::string flag_value;
2901 if (!GetValue(line, kAttributeXGoogleFlag, &flag_value, error)) {
2902 return false;
2903 }
2904 if (flag_value.compare(kValueConference) == 0)
2905 media_desc->set_conference_mode(true);
deadbeef9d3584c2016-02-16 17:54:10 -08002906 } else if (HasAttribute(line, kAttributeMsid)) {
2907 if (!ParseMsidAttribute(line, &stream_id, &track_id, error)) {
2908 return false;
2909 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002910 }
2911 } else {
2912 // Only parse lines that we are interested of.
Mirko Bonadei675513b2017-11-09 11:09:25 +01002913 RTC_LOG(LS_INFO) << "Ignored line: " << line;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002914 continue;
2915 }
2916 }
2917
2918 // Create tracks from the |ssrc_infos|.
Taylor Brandstetter5de6b752016-03-09 17:02:30 -08002919 // If the stream_id/track_id for all SSRCS are identical, one StreamParams
2920 // will be created in CreateTracksFromSsrcInfos, containing all the SSRCs from
2921 // the m= section.
2922 CreateTracksFromSsrcInfos(ssrc_infos, stream_id, track_id, &tracks);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002923
2924 // Add the ssrc group to the track.
2925 for (SsrcGroupVec::iterator ssrc_group = ssrc_groups.begin();
2926 ssrc_group != ssrc_groups.end(); ++ssrc_group) {
2927 if (ssrc_group->ssrcs.empty()) {
2928 continue;
2929 }
Peter Boström0c4e06b2015-10-07 12:23:21 +02002930 uint32_t ssrc = ssrc_group->ssrcs.front();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002931 for (StreamParamsVec::iterator track = tracks.begin();
2932 track != tracks.end(); ++track) {
2933 if (track->has_ssrc(ssrc)) {
2934 track->ssrc_groups.push_back(*ssrc_group);
2935 }
2936 }
2937 }
2938
2939 // Add the new tracks to the |media_desc|.
deadbeef9d3584c2016-02-16 17:54:10 -08002940 for (StreamParams& track : tracks) {
2941 media_desc->AddStream(track);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002942 }
2943
2944 if (media_type == cricket::MEDIA_TYPE_AUDIO) {
2945 AudioContentDescription* audio_desc =
2946 static_cast<AudioContentDescription*>(media_desc);
jlmiller@webrtc.orga744a282015-02-18 21:37:46 +00002947 UpdateFromWildcardCodecs(audio_desc);
2948
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002949 // Verify audio codec ensures that no audio codec has been populated with
2950 // only fmtp.
2951 if (!VerifyAudioCodecs(audio_desc)) {
2952 return ParseFailed("Failed to parse audio codecs correctly.", error);
2953 }
2954 AddAudioAttribute(kCodecParamMaxPTime, maxptime_as_string, audio_desc);
2955 AddAudioAttribute(kCodecParamPTime, ptime_as_string, audio_desc);
2956 }
2957
2958 if (media_type == cricket::MEDIA_TYPE_VIDEO) {
jlmiller@webrtc.orga744a282015-02-18 21:37:46 +00002959 VideoContentDescription* video_desc =
2960 static_cast<VideoContentDescription*>(media_desc);
2961 UpdateFromWildcardCodecs(video_desc);
2962 // Verify video codec ensures that no video codec has been populated with
2963 // only rtcp-fb.
2964 if (!VerifyVideoCodecs(video_desc)) {
2965 return ParseFailed("Failed to parse video codecs correctly.", error);
2966 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002967 }
2968
2969 // RFC 5245
2970 // Update the candidates with the media level "ice-pwd" and "ice-ufrag".
2971 for (Candidates::iterator it = candidates_orig.begin();
2972 it != candidates_orig.end(); ++it) {
nisseede5da42017-01-12 05:15:36 -08002973 RTC_DCHECK((*it).username().empty() ||
2974 (*it).username() == transport->ice_ufrag);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002975 (*it).set_username(transport->ice_ufrag);
nisseede5da42017-01-12 05:15:36 -08002976 RTC_DCHECK((*it).password().empty());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002977 (*it).set_password(transport->ice_pwd);
2978 candidates->push_back(
2979 new JsepIceCandidate(mline_id, mline_index, *it));
2980 }
2981 return true;
2982}
2983
2984bool ParseSsrcAttribute(const std::string& line, SsrcInfoVec* ssrc_infos,
2985 SdpParseError* error) {
nisseede5da42017-01-12 05:15:36 -08002986 RTC_DCHECK(ssrc_infos != NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002987 // RFC 5576
2988 // a=ssrc:<ssrc-id> <attribute>
2989 // a=ssrc:<ssrc-id> <attribute>:<value>
2990 std::string field1, field2;
Donald Curtis144d0182015-05-15 13:14:24 -07002991 if (!rtc::tokenize_first(line.substr(kLinePrefixLength), kSdpDelimiterSpace,
2992 &field1, &field2)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002993 const size_t expected_fields = 2;
2994 return ParseFailedExpectFieldNum(line, expected_fields, error);
2995 }
2996
2997 // ssrc:<ssrc-id>
2998 std::string ssrc_id_s;
2999 if (!GetValue(field1, kAttributeSsrc, &ssrc_id_s, error)) {
3000 return false;
3001 }
Peter Boström0c4e06b2015-10-07 12:23:21 +02003002 uint32_t ssrc_id = 0;
wu@webrtc.org5e760e72014-04-02 23:19:09 +00003003 if (!GetValueFromString(line, ssrc_id_s, &ssrc_id, error)) {
3004 return false;
3005 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003006
3007 std::string attribute;
3008 std::string value;
Donald Curtis144d0182015-05-15 13:14:24 -07003009 if (!rtc::tokenize_first(field2, kSdpDelimiterColon, &attribute, &value)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003010 std::ostringstream description;
3011 description << "Failed to get the ssrc attribute value from " << field2
3012 << ". Expected format <attribute>:<value>.";
3013 return ParseFailed(line, description.str(), error);
3014 }
3015
3016 // Check if there's already an item for this |ssrc_id|. Create a new one if
3017 // there isn't.
3018 SsrcInfoVec::iterator ssrc_info = ssrc_infos->begin();
3019 for (; ssrc_info != ssrc_infos->end(); ++ssrc_info) {
3020 if (ssrc_info->ssrc_id == ssrc_id) {
3021 break;
3022 }
3023 }
3024 if (ssrc_info == ssrc_infos->end()) {
3025 SsrcInfo info;
3026 info.ssrc_id = ssrc_id;
3027 ssrc_infos->push_back(info);
3028 ssrc_info = ssrc_infos->end() - 1;
3029 }
3030
3031 // Store the info to the |ssrc_info|.
3032 if (attribute == kSsrcAttributeCname) {
3033 // RFC 5576
3034 // cname:<value>
3035 ssrc_info->cname = value;
3036 } else if (attribute == kSsrcAttributeMsid) {
3037 // draft-alvestrand-mmusic-msid-00
3038 // "msid:" identifier [ " " appdata ]
3039 std::vector<std::string> fields;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00003040 rtc::split(value, kSdpDelimiterSpace, &fields);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003041 if (fields.size() < 1 || fields.size() > 2) {
3042 return ParseFailed(line,
3043 "Expected format \"msid:<identifier>[ <appdata>]\".",
3044 error);
3045 }
deadbeef9d3584c2016-02-16 17:54:10 -08003046 ssrc_info->stream_id = fields[0];
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003047 if (fields.size() == 2) {
deadbeef9d3584c2016-02-16 17:54:10 -08003048 ssrc_info->track_id = fields[1];
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003049 }
3050 } else if (attribute == kSsrcAttributeMslabel) {
3051 // draft-alvestrand-rtcweb-mid-01
3052 // mslabel:<value>
3053 ssrc_info->mslabel = value;
3054 } else if (attribute == kSSrcAttributeLabel) {
3055 // The label isn't defined.
3056 // label:<value>
3057 ssrc_info->label = value;
3058 }
3059 return true;
3060}
3061
3062bool ParseSsrcGroupAttribute(const std::string& line,
3063 SsrcGroupVec* ssrc_groups,
3064 SdpParseError* error) {
nisseede5da42017-01-12 05:15:36 -08003065 RTC_DCHECK(ssrc_groups != NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003066 // RFC 5576
3067 // a=ssrc-group:<semantics> <ssrc-id> ...
3068 std::vector<std::string> fields;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00003069 rtc::split(line.substr(kLinePrefixLength),
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003070 kSdpDelimiterSpace, &fields);
3071 const size_t expected_min_fields = 2;
3072 if (fields.size() < expected_min_fields) {
3073 return ParseFailedExpectMinFieldNum(line, expected_min_fields, error);
3074 }
3075 std::string semantics;
3076 if (!GetValue(fields[0], kAttributeSsrcGroup, &semantics, error)) {
3077 return false;
3078 }
Peter Boström0c4e06b2015-10-07 12:23:21 +02003079 std::vector<uint32_t> ssrcs;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003080 for (size_t i = 1; i < fields.size(); ++i) {
Peter Boström0c4e06b2015-10-07 12:23:21 +02003081 uint32_t ssrc = 0;
wu@webrtc.org5e760e72014-04-02 23:19:09 +00003082 if (!GetValueFromString(line, fields[i], &ssrc, error)) {
3083 return false;
3084 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003085 ssrcs.push_back(ssrc);
3086 }
3087 ssrc_groups->push_back(SsrcGroup(semantics, ssrcs));
3088 return true;
3089}
3090
3091bool ParseCryptoAttribute(const std::string& line,
3092 MediaContentDescription* media_desc,
3093 SdpParseError* error) {
3094 std::vector<std::string> fields;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00003095 rtc::split(line.substr(kLinePrefixLength),
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003096 kSdpDelimiterSpace, &fields);
3097 // RFC 4568
3098 // a=crypto:<tag> <crypto-suite> <key-params> [<session-params>]
3099 const size_t expected_min_fields = 3;
3100 if (fields.size() < expected_min_fields) {
3101 return ParseFailedExpectMinFieldNum(line, expected_min_fields, error);
3102 }
3103 std::string tag_value;
3104 if (!GetValue(fields[0], kAttributeCrypto, &tag_value, error)) {
3105 return false;
3106 }
wu@webrtc.org5e760e72014-04-02 23:19:09 +00003107 int tag = 0;
3108 if (!GetValueFromString(line, tag_value, &tag, error)) {
3109 return false;
3110 }
jbauch083b73f2015-07-16 02:46:32 -07003111 const std::string& crypto_suite = fields[1];
3112 const std::string& key_params = fields[2];
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003113 std::string session_params;
3114 if (fields.size() > 3) {
3115 session_params = fields[3];
3116 }
3117 media_desc->AddCrypto(CryptoParams(tag, crypto_suite, key_params,
3118 session_params));
3119 return true;
3120}
3121
3122// Updates or creates a new codec entry in the audio description with according
deadbeef67cf2c12016-04-13 10:07:16 -07003123// to |name|, |clockrate|, |bitrate|, and |channels|.
3124void UpdateCodec(int payload_type,
3125 const std::string& name,
3126 int clockrate,
3127 int bitrate,
3128 size_t channels,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003129 AudioContentDescription* audio_desc) {
3130 // Codec may already be populated with (only) optional parameters
3131 // (from an fmtp).
changbin.shao@webrtc.org2d25b442015-03-16 04:14:34 +00003132 cricket::AudioCodec codec =
3133 GetCodecWithPayloadType(audio_desc->codecs(), payload_type);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003134 codec.name = name;
3135 codec.clockrate = clockrate;
3136 codec.bitrate = bitrate;
3137 codec.channels = channels;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003138 AddOrReplaceCodec<AudioContentDescription, cricket::AudioCodec>(audio_desc,
3139 codec);
3140}
3141
3142// Updates or creates a new codec entry in the video description according to
deadbeef67cf2c12016-04-13 10:07:16 -07003143// |name|, |width|, |height|, and |framerate|.
3144void UpdateCodec(int payload_type,
3145 const std::string& name,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003146 VideoContentDescription* video_desc) {
3147 // Codec may already be populated with (only) optional parameters
3148 // (from an fmtp).
changbin.shao@webrtc.org2d25b442015-03-16 04:14:34 +00003149 cricket::VideoCodec codec =
3150 GetCodecWithPayloadType(video_desc->codecs(), payload_type);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003151 codec.name = name;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003152 AddOrReplaceCodec<VideoContentDescription, cricket::VideoCodec>(video_desc,
3153 codec);
3154}
3155
3156bool ParseRtpmapAttribute(const std::string& line,
3157 const MediaType media_type,
deadbeef67cf2c12016-04-13 10:07:16 -07003158 const std::vector<int>& payload_types,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003159 MediaContentDescription* media_desc,
3160 SdpParseError* error) {
3161 std::vector<std::string> fields;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00003162 rtc::split(line.substr(kLinePrefixLength),
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003163 kSdpDelimiterSpace, &fields);
3164 // RFC 4566
3165 // a=rtpmap:<payload type> <encoding name>/<clock rate>[/<encodingparameters>]
3166 const size_t expected_min_fields = 2;
3167 if (fields.size() < expected_min_fields) {
3168 return ParseFailedExpectMinFieldNum(line, expected_min_fields, error);
3169 }
3170 std::string payload_type_value;
3171 if (!GetValue(fields[0], kAttributeRtpmap, &payload_type_value, error)) {
3172 return false;
3173 }
wu@webrtc.org5e760e72014-04-02 23:19:09 +00003174 int payload_type = 0;
pkasting@chromium.orge9facf82015-02-17 20:36:28 +00003175 if (!GetPayloadTypeFromString(line, payload_type_value, &payload_type,
3176 error)) {
wu@webrtc.org5e760e72014-04-02 23:19:09 +00003177 return false;
3178 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003179
deadbeef67cf2c12016-04-13 10:07:16 -07003180 if (std::find(payload_types.begin(), payload_types.end(), payload_type) ==
3181 payload_types.end()) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01003182 RTC_LOG(LS_WARNING) << "Ignore rtpmap line that did not appear in the "
3183 << "<fmt> of the m-line: " << line;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003184 return true;
3185 }
jbauch083b73f2015-07-16 02:46:32 -07003186 const std::string& encoder = fields[1];
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003187 std::vector<std::string> codec_params;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00003188 rtc::split(encoder, '/', &codec_params);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003189 // <encoding name>/<clock rate>[/<encodingparameters>]
3190 // 2 mandatory fields
3191 if (codec_params.size() < 2 || codec_params.size() > 3) {
3192 return ParseFailed(line,
3193 "Expected format \"<encoding name>/<clock rate>"
3194 "[/<encodingparameters>]\".",
3195 error);
3196 }
jbauch083b73f2015-07-16 02:46:32 -07003197 const std::string& encoding_name = codec_params[0];
wu@webrtc.org5e760e72014-04-02 23:19:09 +00003198 int clock_rate = 0;
3199 if (!GetValueFromString(line, codec_params[1], &clock_rate, error)) {
3200 return false;
3201 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003202 if (media_type == cricket::MEDIA_TYPE_VIDEO) {
3203 VideoContentDescription* video_desc =
3204 static_cast<VideoContentDescription*>(media_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003205 UpdateCodec(payload_type, encoding_name,
deadbeef67cf2c12016-04-13 10:07:16 -07003206 video_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003207 } else if (media_type == cricket::MEDIA_TYPE_AUDIO) {
3208 // RFC 4566
3209 // For audio streams, <encoding parameters> indicates the number
3210 // of audio channels. This parameter is OPTIONAL and may be
3211 // omitted if the number of channels is one, provided that no
3212 // additional parameters are needed.
Peter Kasting69558702016-01-12 16:26:35 -08003213 size_t channels = 1;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003214 if (codec_params.size() == 3) {
wu@webrtc.org5e760e72014-04-02 23:19:09 +00003215 if (!GetValueFromString(line, codec_params[2], &channels, error)) {
3216 return false;
3217 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003218 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003219 AudioContentDescription* audio_desc =
3220 static_cast<AudioContentDescription*>(media_desc);
ossue1405ad2017-01-23 08:55:48 -08003221 UpdateCodec(payload_type, encoding_name, clock_rate, 0, channels,
deadbeef67cf2c12016-04-13 10:07:16 -07003222 audio_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003223 } else if (media_type == cricket::MEDIA_TYPE_DATA) {
3224 DataContentDescription* data_desc =
3225 static_cast<DataContentDescription*>(media_desc);
deadbeef67cf2c12016-04-13 10:07:16 -07003226 data_desc->AddCodec(cricket::DataCodec(payload_type, encoding_name));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003227 }
3228 return true;
3229}
3230
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003231bool ParseFmtpParam(const std::string& line, std::string* parameter,
3232 std::string* value, SdpParseError* error) {
Donald Curtis0e07f922015-05-15 09:21:23 -07003233 if (!rtc::tokenize_first(line, kSdpDelimiterEqual, parameter, value)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003234 ParseFailed(line, "Unable to parse fmtp parameter. \'=\' missing.", error);
3235 return false;
3236 }
3237 // a=fmtp:<payload_type> <param1>=<value1>; <param2>=<value2>; ...
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003238 return true;
3239}
3240
3241bool ParseFmtpAttributes(const std::string& line, const MediaType media_type,
3242 MediaContentDescription* media_desc,
3243 SdpParseError* error) {
3244 if (media_type != cricket::MEDIA_TYPE_AUDIO &&
3245 media_type != cricket::MEDIA_TYPE_VIDEO) {
3246 return true;
3247 }
Donald Curtis0e07f922015-05-15 09:21:23 -07003248
3249 std::string line_payload;
3250 std::string line_params;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003251
3252 // RFC 5576
3253 // a=fmtp:<format> <format specific parameters>
3254 // At least two fields, whereas the second one is any of the optional
3255 // parameters.
Donald Curtis144d0182015-05-15 13:14:24 -07003256 if (!rtc::tokenize_first(line.substr(kLinePrefixLength), kSdpDelimiterSpace,
3257 &line_payload, &line_params)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003258 ParseFailedExpectMinFieldNum(line, 2, error);
3259 return false;
3260 }
3261
Donald Curtis0e07f922015-05-15 09:21:23 -07003262 // Parse out the payload information.
pkasting@chromium.orgd3245462015-02-23 21:28:22 +00003263 std::string payload_type_str;
Donald Curtis0e07f922015-05-15 09:21:23 -07003264 if (!GetValue(line_payload, kAttributeFmtp, &payload_type_str, error)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003265 return false;
3266 }
3267
Donald Curtis0e07f922015-05-15 09:21:23 -07003268 int payload_type = 0;
Donald Curtis144d0182015-05-15 13:14:24 -07003269 if (!GetPayloadTypeFromString(line_payload, payload_type_str, &payload_type,
3270 error)) {
Donald Curtis0e07f922015-05-15 09:21:23 -07003271 return false;
3272 }
3273
3274 // Parse out format specific parameters.
3275 std::vector<std::string> fields;
3276 rtc::split(line_params, kSdpDelimiterSemicolon, &fields);
3277
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003278 cricket::CodecParameterMap codec_params;
Donald Curtis144d0182015-05-15 13:14:24 -07003279 for (auto& iter : fields) {
Donald Curtis0e07f922015-05-15 09:21:23 -07003280 if (iter.find(kSdpDelimiterEqual) == std::string::npos) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003281 // Only fmtps with equals are currently supported. Other fmtp types
3282 // should be ignored. Unknown fmtps do not constitute an error.
3283 continue;
3284 }
Donald Curtis0e07f922015-05-15 09:21:23 -07003285
3286 std::string name;
3287 std::string value;
3288 if (!ParseFmtpParam(rtc::string_trim(iter), &name, &value, error)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003289 return false;
3290 }
3291 codec_params[name] = value;
3292 }
3293
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003294 if (media_type == cricket::MEDIA_TYPE_AUDIO) {
3295 UpdateCodec<AudioContentDescription, cricket::AudioCodec>(
pkasting@chromium.orgd3245462015-02-23 21:28:22 +00003296 media_desc, payload_type, codec_params);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003297 } else if (media_type == cricket::MEDIA_TYPE_VIDEO) {
3298 UpdateCodec<VideoContentDescription, cricket::VideoCodec>(
pkasting@chromium.orgd3245462015-02-23 21:28:22 +00003299 media_desc, payload_type, codec_params);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003300 }
3301 return true;
3302}
3303
3304bool ParseRtcpFbAttribute(const std::string& line, const MediaType media_type,
3305 MediaContentDescription* media_desc,
3306 SdpParseError* error) {
3307 if (media_type != cricket::MEDIA_TYPE_AUDIO &&
3308 media_type != cricket::MEDIA_TYPE_VIDEO) {
3309 return true;
3310 }
3311 std::vector<std::string> rtcp_fb_fields;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00003312 rtc::split(line.c_str(), kSdpDelimiterSpace, &rtcp_fb_fields);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003313 if (rtcp_fb_fields.size() < 2) {
3314 return ParseFailedGetValue(line, kAttributeRtcpFb, error);
3315 }
3316 std::string payload_type_string;
3317 if (!GetValue(rtcp_fb_fields[0], kAttributeRtcpFb, &payload_type_string,
3318 error)) {
3319 return false;
3320 }
wu@webrtc.org5e760e72014-04-02 23:19:09 +00003321 int payload_type = kWildcardPayloadType;
3322 if (payload_type_string != "*") {
pkasting@chromium.orge9facf82015-02-17 20:36:28 +00003323 if (!GetPayloadTypeFromString(line, payload_type_string, &payload_type,
3324 error)) {
wu@webrtc.org5e760e72014-04-02 23:19:09 +00003325 return false;
3326 }
3327 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003328 std::string id = rtcp_fb_fields[1];
3329 std::string param = "";
3330 for (std::vector<std::string>::iterator iter = rtcp_fb_fields.begin() + 2;
3331 iter != rtcp_fb_fields.end(); ++iter) {
3332 param.append(*iter);
3333 }
3334 const cricket::FeedbackParam feedback_param(id, param);
3335
3336 if (media_type == cricket::MEDIA_TYPE_AUDIO) {
pkasting@chromium.orgd3245462015-02-23 21:28:22 +00003337 UpdateCodec<AudioContentDescription, cricket::AudioCodec>(
3338 media_desc, payload_type, feedback_param);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003339 } else if (media_type == cricket::MEDIA_TYPE_VIDEO) {
pkasting@chromium.orgd3245462015-02-23 21:28:22 +00003340 UpdateCodec<VideoContentDescription, cricket::VideoCodec>(
3341 media_desc, payload_type, feedback_param);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003342 }
3343 return true;
3344}
3345
3346} // namespace webrtc