blob: 3953773d83f361837e7239ef81550da44635d247 [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
ossu7bb87ee2017-01-23 04:56:25 -080011#include "webrtc/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>
kwibergd1fe2812016-04-27 06:47:29 -070018#include <memory>
henrike@webrtc.org28e20752013-07-10 00:45:36 +000019#include <string>
deadbeef67cf2c12016-04-13 10:07:16 -070020#include <unordered_map>
henrike@webrtc.org28e20752013-07-10 00:45:36 +000021#include <vector>
22
Henrik Kjellander15583c12016-02-10 10:53:12 +010023#include "webrtc/api/jsepicecandidate.h"
24#include "webrtc/api/jsepsessiondescription.h"
tfarina5237aaf2015-11-10 23:44:30 -080025#include "webrtc/base/arraysize.h"
nissec80e7412017-01-11 05:56:46 -080026#include "webrtc/base/checks.h"
buildbot@webrtc.orga09a9992014-08-13 17:26:08 +000027#include "webrtc/base/logging.h"
28#include "webrtc/base/messagedigest.h"
29#include "webrtc/base/stringutils.h"
isheriff6f8d6862016-05-26 11:24:55 -070030// for RtpExtension
31#include "webrtc/config.h"
kjellandera96e2d72016-02-04 23:52:28 -080032#include "webrtc/media/base/codec.h"
kjellandera96e2d72016-02-04 23:52:28 -080033#include "webrtc/media/base/cryptoparams.h"
kjellanderf4752772016-03-02 05:42:30 -080034#include "webrtc/media/base/mediaconstants.h"
kjellandera96e2d72016-02-04 23:52:28 -080035#include "webrtc/media/base/rtputils.h"
deadbeef953c2ce2017-01-09 14:53:41 -080036#include "webrtc/media/sctp/sctptransportinternal.h"
kjellandera96e2d72016-02-04 23:52:28 -080037#include "webrtc/p2p/base/candidate.h"
kjellanderf4752772016-03-02 05:42:30 -080038#include "webrtc/p2p/base/p2pconstants.h"
kjellandera96e2d72016-02-04 23:52:28 -080039#include "webrtc/p2p/base/port.h"
kjellander@webrtc.org9b8df252016-02-12 06:47:59 +010040#include "webrtc/pc/mediasession.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000041
42using cricket::AudioContentDescription;
43using cricket::Candidate;
44using cricket::Candidates;
45using cricket::ContentDescription;
46using cricket::ContentInfo;
47using cricket::CryptoParams;
48using cricket::DataContentDescription;
49using cricket::ICE_CANDIDATE_COMPONENT_RTP;
50using cricket::ICE_CANDIDATE_COMPONENT_RTCP;
51using cricket::kCodecParamMaxBitrate;
52using cricket::kCodecParamMaxPTime;
53using cricket::kCodecParamMaxQuantization;
54using cricket::kCodecParamMinBitrate;
55using cricket::kCodecParamMinPTime;
56using cricket::kCodecParamPTime;
57using cricket::kCodecParamSPropStereo;
buildbot@webrtc.orged97bb02014-05-07 11:15:20 +000058using cricket::kCodecParamStartBitrate;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000059using cricket::kCodecParamStereo;
60using cricket::kCodecParamUseInbandFec;
Minyue Li7100dcd2015-03-27 05:05:59 +010061using cricket::kCodecParamUseDtx;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000062using cricket::kCodecParamSctpProtocol;
63using cricket::kCodecParamSctpStreams;
henrike@webrtc.org1e09a712013-07-26 19:17:59 +000064using cricket::kCodecParamMaxAverageBitrate;
buildbot@webrtc.org5d639b32014-09-10 07:57:12 +000065using cricket::kCodecParamMaxPlaybackRate;
stefan@webrtc.org85d27942014-06-09 12:51:39 +000066using cricket::kCodecParamAssociatedPayloadType;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000067using cricket::MediaContentDescription;
68using cricket::MediaType;
isheriff6f8d6862016-05-26 11:24:55 -070069using cricket::RtpHeaderExtensions;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000070using cricket::SsrcGroup;
71using cricket::StreamParams;
72using cricket::StreamParamsVec;
73using cricket::TransportDescription;
74using cricket::TransportInfo;
75using cricket::VideoContentDescription;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000076using rtc::SocketAddress;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000077
henrike@webrtc.org28e20752013-07-10 00:45:36 +000078namespace cricket {
79class SessionDescription;
80}
81
deadbeef90f1e1e2017-02-10 12:35:05 -080082// TODO(deadbeef): Switch to using anonymous namespace rather than declaring
83// everything "static".
henrike@webrtc.org28e20752013-07-10 00:45:36 +000084namespace webrtc {
85
86// Line type
87// RFC 4566
88// An SDP session description consists of a number of lines of text of
89// the form:
90// <type>=<value>
91// where <type> MUST be exactly one case-significant character.
deadbeef9d3584c2016-02-16 17:54:10 -080092static const int kLinePrefixLength = 2; // Length of <type>=
henrike@webrtc.org28e20752013-07-10 00:45:36 +000093static const char kLineTypeVersion = 'v';
94static const char kLineTypeOrigin = 'o';
95static const char kLineTypeSessionName = 's';
96static const char kLineTypeSessionInfo = 'i';
97static const char kLineTypeSessionUri = 'u';
98static const char kLineTypeSessionEmail = 'e';
99static const char kLineTypeSessionPhone = 'p';
100static const char kLineTypeSessionBandwidth = 'b';
101static const char kLineTypeTiming = 't';
102static const char kLineTypeRepeatTimes = 'r';
103static const char kLineTypeTimeZone = 'z';
104static const char kLineTypeEncryptionKey = 'k';
105static const char kLineTypeMedia = 'm';
106static const char kLineTypeConnection = 'c';
107static const char kLineTypeAttributes = 'a';
108
109// Attributes
110static const char kAttributeGroup[] = "group";
111static const char kAttributeMid[] = "mid";
deadbeef9d3584c2016-02-16 17:54:10 -0800112static const char kAttributeMsid[] = "msid";
deadbeef25ed4352016-12-12 18:37:36 -0800113static const char kAttributeBundleOnly[] = "bundle-only";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000114static const char kAttributeRtcpMux[] = "rtcp-mux";
deadbeef13871492015-12-09 12:37:51 -0800115static const char kAttributeRtcpReducedSize[] = "rtcp-rsize";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000116static const char kAttributeSsrc[] = "ssrc";
117static const char kSsrcAttributeCname[] = "cname";
118static const char kAttributeExtmap[] = "extmap";
119// draft-alvestrand-mmusic-msid-01
120// a=msid-semantic: WMS
121static const char kAttributeMsidSemantics[] = "msid-semantic";
122static const char kMediaStreamSemantic[] = "WMS";
123static const char kSsrcAttributeMsid[] = "msid";
124static const char kDefaultMsid[] = "default";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000125static const char kSsrcAttributeMslabel[] = "mslabel";
126static const char kSSrcAttributeLabel[] = "label";
127static const char kAttributeSsrcGroup[] = "ssrc-group";
128static const char kAttributeCrypto[] = "crypto";
129static const char kAttributeCandidate[] = "candidate";
130static const char kAttributeCandidateTyp[] = "typ";
131static const char kAttributeCandidateRaddr[] = "raddr";
132static const char kAttributeCandidateRport[] = "rport";
honghaiza54a0802015-12-16 18:37:23 -0800133static const char kAttributeCandidateUfrag[] = "ufrag";
134static const char kAttributeCandidatePwd[] = "pwd";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000135static const char kAttributeCandidateGeneration[] = "generation";
honghaiza0c44ea2016-03-23 16:07:48 -0700136static const char kAttributeCandidateNetworkId[] = "network-id";
honghaize1a0c942016-02-16 14:54:56 -0800137static const char kAttributeCandidateNetworkCost[] = "network-cost";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000138static const char kAttributeFingerprint[] = "fingerprint";
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +0000139static const char kAttributeSetup[] = "setup";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000140static const char kAttributeFmtp[] = "fmtp";
141static const char kAttributeRtpmap[] = "rtpmap";
wu@webrtc.org78187522013-10-07 23:32:02 +0000142static const char kAttributeSctpmap[] = "sctpmap";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000143static const char kAttributeRtcp[] = "rtcp";
144static const char kAttributeIceUfrag[] = "ice-ufrag";
145static const char kAttributeIcePwd[] = "ice-pwd";
146static const char kAttributeIceLite[] = "ice-lite";
147static const char kAttributeIceOption[] = "ice-options";
148static const char kAttributeSendOnly[] = "sendonly";
149static const char kAttributeRecvOnly[] = "recvonly";
150static const char kAttributeRtcpFb[] = "rtcp-fb";
151static const char kAttributeSendRecv[] = "sendrecv";
152static const char kAttributeInactive[] = "inactive";
jiayl@webrtc.orgddb85ab2014-09-05 16:31:56 +0000153// draft-ietf-mmusic-sctp-sdp-07
154// a=sctp-port
155static const char kAttributeSctpPort[] = "sctp-port";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000156
157// Experimental flags
158static const char kAttributeXGoogleFlag[] = "x-google-flag";
159static const char kValueConference[] = "conference";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000160
161// Candidate
162static const char kCandidateHost[] = "host";
163static const char kCandidateSrflx[] = "srflx";
Honghai Zhang7fb69db2016-03-14 11:59:18 -0700164static const char kCandidatePrflx[] = "prflx";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000165static const char kCandidateRelay[] = "relay";
mallinath@webrtc.org2d60c5e2014-08-08 22:29:20 +0000166static const char kTcpCandidateType[] = "tcptype";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000167
168static const char kSdpDelimiterEqual = '=';
169static const char kSdpDelimiterSpace = ' ';
170static const char kSdpDelimiterColon = ':';
171static const char kSdpDelimiterSemicolon = ';';
172static const char kSdpDelimiterSlash = '/';
173static const char kNewLine = '\n';
174static const char kReturn = '\r';
175static const char kLineBreak[] = "\r\n";
176
177// TODO: Generate the Session and Time description
178// instead of hardcoding.
179static const char kSessionVersion[] = "v=0";
180// RFC 4566
181static const char kSessionOriginUsername[] = "-";
182static const char kSessionOriginSessionId[] = "0";
183static const char kSessionOriginSessionVersion[] = "0";
184static const char kSessionOriginNettype[] = "IN";
185static const char kSessionOriginAddrtype[] = "IP4";
186static const char kSessionOriginAddress[] = "127.0.0.1";
187static const char kSessionName[] = "s=-";
188static const char kTimeDescription[] = "t=0 0";
189static const char kAttrGroup[] = "a=group:BUNDLE";
190static const char kConnectionNettype[] = "IN";
pthatcher@webrtc.orgc9d6d142014-10-23 23:37:22 +0000191static const char kConnectionIpv4Addrtype[] = "IP4";
192static const char kConnectionIpv6Addrtype[] = "IP6";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000193static const char kMediaTypeVideo[] = "video";
194static const char kMediaTypeAudio[] = "audio";
195static const char kMediaTypeData[] = "application";
196static const char kMediaPortRejected[] = "0";
pthatcher@webrtc.orgc9d6d142014-10-23 23:37:22 +0000197// draft-ietf-mmusic-trickle-ice-01
198// When no candidates have been gathered, set the connection
199// address to IP6 ::.
perkj@webrtc.orgd105cc82014-11-07 11:22:06 +0000200// TODO(perkj): FF can not parse IP6 ::. See http://crbug/430333
201// Use IPV4 per default.
202static const char kDummyAddress[] = "0.0.0.0";
pthatcher@webrtc.orgc9d6d142014-10-23 23:37:22 +0000203static const char kDummyPort[] = "9";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000204// RFC 3556
205static const char kApplicationSpecificMaximum[] = "AS";
206
wu@webrtc.org78187522013-10-07 23:32:02 +0000207static const char kDefaultSctpmapProtocol[] = "webrtc-datachannel";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000208
pkasting@chromium.orgd3245462015-02-23 21:28:22 +0000209// RTP payload type is in the 0-127 range. Use -1 to indicate "all" payload
210// types.
211const int kWildcardPayloadType = -1;
212
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000213struct SsrcInfo {
Peter Boström0c4e06b2015-10-07 12:23:21 +0200214 uint32_t ssrc_id;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000215 std::string cname;
deadbeef9d3584c2016-02-16 17:54:10 -0800216 std::string stream_id;
217 std::string track_id;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000218
219 // For backward compatibility.
220 // TODO(ronghuawu): Remove below 2 fields once all the clients support msid.
221 std::string label;
222 std::string mslabel;
223};
224typedef std::vector<SsrcInfo> SsrcInfoVec;
225typedef std::vector<SsrcGroup> SsrcGroupVec;
226
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000227template <class T>
228static void AddFmtpLine(const T& codec, std::string* message);
229static void BuildMediaDescription(const ContentInfo* content_info,
230 const TransportInfo* transport_info,
231 const MediaType media_type,
wu@webrtc.org4c3e9912014-07-16 21:03:13 +0000232 const std::vector<Candidate>& candidates,
deadbeef9d3584c2016-02-16 17:54:10 -0800233 bool unified_plan_sdp,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000234 std::string* message);
zstein4b2e0822017-02-17 19:48:38 -0800235static void BuildSctpContentAttributes(std::string* message,
236 int sctp_port,
237 bool use_sctpmap);
deadbeef9d3584c2016-02-16 17:54:10 -0800238static void BuildRtpContentAttributes(const MediaContentDescription* media_desc,
239 const MediaType media_type,
240 bool unified_plan_sdp,
241 std::string* message);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000242static void BuildRtpMap(const MediaContentDescription* media_desc,
243 const MediaType media_type,
244 std::string* message);
245static void BuildCandidate(const std::vector<Candidate>& candidates,
honghaiza54a0802015-12-16 18:37:23 -0800246 bool include_ufrag,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000247 std::string* message);
248static void BuildIceOptions(const std::vector<std::string>& transport_options,
249 std::string* message);
pthatcher@webrtc.org3341b402015-02-13 21:14:22 +0000250static bool IsRtp(const std::string& protocol);
251static bool IsDtlsSctp(const std::string& protocol);
zhihuang38989e52017-03-21 11:04:53 -0700252static bool ParseSessionDescription(const std::string& message,
253 size_t* pos,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000254 std::string* session_id,
255 std::string* session_version,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000256 TransportDescription* session_td,
257 RtpHeaderExtensions* session_extmaps,
zhihuang38989e52017-03-21 11:04:53 -0700258 rtc::SocketAddress* connection_addr,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000259 cricket::SessionDescription* desc,
260 SdpParseError* error);
261static bool ParseGroupAttribute(const std::string& line,
262 cricket::SessionDescription* desc,
263 SdpParseError* error);
264static bool ParseMediaDescription(
265 const std::string& message,
266 const TransportDescription& session_td,
267 const RtpHeaderExtensions& session_extmaps,
zhihuang38989e52017-03-21 11:04:53 -0700268 size_t* pos,
269 const rtc::SocketAddress& session_connection_addr,
270 cricket::SessionDescription* desc,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000271 std::vector<JsepIceCandidate*>* candidates,
272 SdpParseError* error);
273static bool ParseContent(const std::string& message,
274 const MediaType media_type,
275 int mline_index,
276 const std::string& protocol,
deadbeef67cf2c12016-04-13 10:07:16 -0700277 const std::vector<int>& payload_types,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000278 size_t* pos,
279 std::string* content_name,
deadbeef25ed4352016-12-12 18:37:36 -0800280 bool* bundle_only,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000281 MediaContentDescription* media_desc,
282 TransportDescription* transport,
283 std::vector<JsepIceCandidate*>* candidates,
284 SdpParseError* error);
285static bool ParseSsrcAttribute(const std::string& line,
286 SsrcInfoVec* ssrc_infos,
287 SdpParseError* error);
288static bool ParseSsrcGroupAttribute(const std::string& line,
289 SsrcGroupVec* ssrc_groups,
290 SdpParseError* error);
291static bool ParseCryptoAttribute(const std::string& line,
292 MediaContentDescription* media_desc,
293 SdpParseError* error);
294static bool ParseRtpmapAttribute(const std::string& line,
295 const MediaType media_type,
deadbeef67cf2c12016-04-13 10:07:16 -0700296 const std::vector<int>& payload_types,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000297 MediaContentDescription* media_desc,
298 SdpParseError* error);
299static bool ParseFmtpAttributes(const std::string& line,
300 const MediaType media_type,
301 MediaContentDescription* media_desc,
302 SdpParseError* error);
303static bool ParseFmtpParam(const std::string& line, std::string* parameter,
304 std::string* value, SdpParseError* error);
305static bool ParseCandidate(const std::string& message, Candidate* candidate,
306 SdpParseError* error, bool is_raw);
307static bool ParseRtcpFbAttribute(const std::string& line,
308 const MediaType media_type,
309 MediaContentDescription* media_desc,
310 SdpParseError* error);
311static bool ParseIceOptions(const std::string& line,
312 std::vector<std::string>* transport_options,
313 SdpParseError* error);
314static bool ParseExtmap(const std::string& line,
isheriff6f8d6862016-05-26 11:24:55 -0700315 RtpExtension* extmap,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000316 SdpParseError* error);
317static bool ParseFingerprintAttribute(const std::string& line,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000318 rtc::SSLFingerprint** fingerprint,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000319 SdpParseError* error);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +0000320static bool ParseDtlsSetup(const std::string& line,
321 cricket::ConnectionRole* role,
322 SdpParseError* error);
deadbeef9d3584c2016-02-16 17:54:10 -0800323static bool ParseMsidAttribute(const std::string& line,
324 std::string* stream_id,
325 std::string* track_id,
326 SdpParseError* error);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000327
328// Helper functions
329
330// Below ParseFailed*** functions output the line that caused the parsing
331// failure and the detailed reason (|description|) of the failure to |error|.
332// The functions always return false so that they can be used directly in the
333// following way when error happens:
334// "return ParseFailed***(...);"
335
336// The line starting at |line_start| of |message| is the failing line.
337// The reason for the failure should be provided in the |description|.
338// An example of a description could be "unknown character".
339static bool ParseFailed(const std::string& message,
340 size_t line_start,
341 const std::string& description,
342 SdpParseError* error) {
343 // Get the first line of |message| from |line_start|.
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +0000344 std::string first_line;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000345 size_t line_end = message.find(kNewLine, line_start);
346 if (line_end != std::string::npos) {
347 if (line_end > 0 && (message.at(line_end - 1) == kReturn)) {
348 --line_end;
349 }
350 first_line = message.substr(line_start, (line_end - line_start));
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +0000351 } else {
352 first_line = message.substr(line_start);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000353 }
354
355 if (error) {
356 error->line = first_line;
357 error->description = description;
358 }
359 LOG(LS_ERROR) << "Failed to parse: \"" << first_line
360 << "\". Reason: " << description;
361 return false;
362}
363
364// |line| is the failing line. The reason for the failure should be
365// provided in the |description|.
366static bool ParseFailed(const std::string& line,
367 const std::string& description,
368 SdpParseError* error) {
369 return ParseFailed(line, 0, description, error);
370}
371
372// Parses failure where the failing SDP line isn't know or there are multiple
373// failing lines.
374static bool ParseFailed(const std::string& description,
375 SdpParseError* error) {
376 return ParseFailed("", description, error);
377}
378
379// |line| is the failing line. The failure is due to the fact that |line|
380// doesn't have |expected_fields| fields.
381static bool ParseFailedExpectFieldNum(const std::string& line,
382 int expected_fields,
383 SdpParseError* error) {
384 std::ostringstream description;
385 description << "Expects " << expected_fields << " fields.";
386 return ParseFailed(line, description.str(), error);
387}
388
389// |line| is the failing line. The failure is due to the fact that |line| has
390// less than |expected_min_fields| fields.
391static bool ParseFailedExpectMinFieldNum(const std::string& line,
392 int expected_min_fields,
393 SdpParseError* error) {
394 std::ostringstream description;
395 description << "Expects at least " << expected_min_fields << " fields.";
396 return ParseFailed(line, description.str(), error);
397}
398
399// |line| is the failing line. The failure is due to the fact that it failed to
400// get the value of |attribute|.
401static bool ParseFailedGetValue(const std::string& line,
402 const std::string& attribute,
403 SdpParseError* error) {
404 std::ostringstream description;
405 description << "Failed to get the value of attribute: " << attribute;
406 return ParseFailed(line, description.str(), error);
407}
408
409// The line starting at |line_start| of |message| is the failing line. The
410// failure is due to the line type (e.g. the "m" part of the "m-line")
411// not matching what is expected. The expected line type should be
412// provided as |line_type|.
413static bool ParseFailedExpectLine(const std::string& message,
414 size_t line_start,
415 const char line_type,
416 const std::string& line_value,
417 SdpParseError* error) {
418 std::ostringstream description;
419 description << "Expect line: " << line_type << "=" << line_value;
420 return ParseFailed(message, line_start, description.str(), error);
421}
422
423static bool AddLine(const std::string& line, std::string* message) {
424 if (!message)
425 return false;
426
427 message->append(line);
428 message->append(kLineBreak);
429 return true;
430}
431
432static bool GetLine(const std::string& message,
433 size_t* pos,
434 std::string* line) {
435 size_t line_begin = *pos;
436 size_t line_end = message.find(kNewLine, line_begin);
437 if (line_end == std::string::npos) {
438 return false;
439 }
440 // Update the new start position
441 *pos = line_end + 1;
442 if (line_end > 0 && (message.at(line_end - 1) == kReturn)) {
443 --line_end;
444 }
445 *line = message.substr(line_begin, (line_end - line_begin));
446 const char* cline = line->c_str();
447 // RFC 4566
448 // An SDP session description consists of a number of lines of text of
449 // the form:
450 // <type>=<value>
451 // where <type> MUST be exactly one case-significant character and
452 // <value> is structured text whose format depends on <type>.
453 // Whitespace MUST NOT be used on either side of the "=" sign.
decurtis@webrtc.org8af11042015-01-07 19:15:51 +0000454 if (line->length() < 3 ||
455 !islower(cline[0]) ||
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000456 cline[1] != kSdpDelimiterEqual ||
457 cline[2] == kSdpDelimiterSpace) {
458 *pos = line_begin;
459 return false;
460 }
461 return true;
462}
463
464// Init |os| to "|type|=|value|".
465static void InitLine(const char type,
466 const std::string& value,
467 std::ostringstream* os) {
468 os->str("");
469 *os << type << kSdpDelimiterEqual << value;
470}
471
472// Init |os| to "a=|attribute|".
473static void InitAttrLine(const std::string& attribute, std::ostringstream* os) {
474 InitLine(kLineTypeAttributes, attribute, os);
475}
476
477// Writes a SDP attribute line based on |attribute| and |value| to |message|.
478static void AddAttributeLine(const std::string& attribute, int value,
479 std::string* message) {
480 std::ostringstream os;
481 InitAttrLine(attribute, &os);
482 os << kSdpDelimiterColon << value;
483 AddLine(os.str(), message);
484}
485
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000486static bool IsLineType(const std::string& message,
487 const char type,
488 size_t line_start) {
489 if (message.size() < line_start + kLinePrefixLength) {
490 return false;
491 }
492 const char* cmessage = message.c_str();
493 return (cmessage[line_start] == type &&
494 cmessage[line_start + 1] == kSdpDelimiterEqual);
495}
496
497static bool IsLineType(const std::string& line,
498 const char type) {
499 return IsLineType(line, type, 0);
500}
501
502static bool GetLineWithType(const std::string& message, size_t* pos,
503 std::string* line, const char type) {
504 if (!IsLineType(message, type, *pos)) {
505 return false;
506 }
507
508 if (!GetLine(message, pos, line))
509 return false;
510
511 return true;
512}
513
514static bool HasAttribute(const std::string& line,
515 const std::string& attribute) {
516 return (line.compare(kLinePrefixLength, attribute.size(), attribute) == 0);
517}
518
Peter Boström0c4e06b2015-10-07 12:23:21 +0200519static bool AddSsrcLine(uint32_t ssrc_id,
520 const std::string& attribute,
521 const std::string& value,
522 std::string* message) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000523 // RFC 5576
524 // a=ssrc:<ssrc-id> <attribute>:<value>
525 std::ostringstream os;
526 InitAttrLine(kAttributeSsrc, &os);
527 os << kSdpDelimiterColon << ssrc_id << kSdpDelimiterSpace
528 << attribute << kSdpDelimiterColon << value;
529 return AddLine(os.str(), message);
530}
531
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000532// Get value only from <attribute>:<value>.
533static bool GetValue(const std::string& message, const std::string& attribute,
534 std::string* value, SdpParseError* error) {
535 std::string leftpart;
Donald Curtis0e07f922015-05-15 09:21:23 -0700536 if (!rtc::tokenize_first(message, kSdpDelimiterColon, &leftpart, value)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000537 return ParseFailedGetValue(message, attribute, error);
538 }
539 // The left part should end with the expected attribute.
540 if (leftpart.length() < attribute.length() ||
541 leftpart.compare(leftpart.length() - attribute.length(),
542 attribute.length(), attribute) != 0) {
543 return ParseFailedGetValue(message, attribute, error);
544 }
545 return true;
546}
547
548static bool CaseInsensitiveFind(std::string str1, std::string str2) {
549 std::transform(str1.begin(), str1.end(), str1.begin(),
550 ::tolower);
551 std::transform(str2.begin(), str2.end(), str2.begin(),
552 ::tolower);
553 return str1.find(str2) != std::string::npos;
554}
555
wu@webrtc.org5e760e72014-04-02 23:19:09 +0000556template <class T>
557static bool GetValueFromString(const std::string& line,
558 const std::string& s,
559 T* t,
560 SdpParseError* error) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000561 if (!rtc::FromString(s, t)) {
wu@webrtc.org5e760e72014-04-02 23:19:09 +0000562 std::ostringstream description;
563 description << "Invalid value: " << s << ".";
564 return ParseFailed(line, description.str(), error);
565 }
566 return true;
567}
568
pkasting@chromium.orge9facf82015-02-17 20:36:28 +0000569static bool GetPayloadTypeFromString(const std::string& line,
570 const std::string& s,
571 int* payload_type,
572 SdpParseError* error) {
573 return GetValueFromString(line, s, payload_type, error) &&
574 cricket::IsValidRtpPayloadType(*payload_type);
575}
576
Taylor Brandstetter5de6b752016-03-09 17:02:30 -0800577// |msid_stream_id| and |msid_track_id| represent the stream/track ID from the
578// "a=msid" attribute, if it exists. They are empty if the attribute does not
579// exist.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000580void CreateTracksFromSsrcInfos(const SsrcInfoVec& ssrc_infos,
Taylor Brandstetter5de6b752016-03-09 17:02:30 -0800581 const std::string& msid_stream_id,
582 const std::string& msid_track_id,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000583 StreamParamsVec* tracks) {
nisseede5da42017-01-12 05:15:36 -0800584 RTC_DCHECK(tracks != NULL);
585 RTC_DCHECK(msid_stream_id.empty() == msid_track_id.empty());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000586 for (SsrcInfoVec::const_iterator ssrc_info = ssrc_infos.begin();
587 ssrc_info != ssrc_infos.end(); ++ssrc_info) {
588 if (ssrc_info->cname.empty()) {
589 continue;
590 }
591
Taylor Brandstetter5de6b752016-03-09 17:02:30 -0800592 std::string stream_id;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000593 std::string track_id;
Taylor Brandstetter5de6b752016-03-09 17:02:30 -0800594 if (ssrc_info->stream_id.empty() && !ssrc_info->mslabel.empty()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000595 // If there's no msid and there's mslabel, we consider this is a sdp from
596 // a older version of client that doesn't support msid.
597 // In that case, we use the mslabel and label to construct the track.
Taylor Brandstetter5de6b752016-03-09 17:02:30 -0800598 stream_id = ssrc_info->mslabel;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000599 track_id = ssrc_info->label;
Taylor Brandstetter5de6b752016-03-09 17:02:30 -0800600 } else if (ssrc_info->stream_id.empty() && !msid_stream_id.empty()) {
601 // If there's no msid in the SSRC attributes, but there's a global one
602 // (from a=msid), use that. This is the case with unified plan SDP.
603 stream_id = msid_stream_id;
604 track_id = msid_track_id;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000605 } else {
Taylor Brandstetter5de6b752016-03-09 17:02:30 -0800606 stream_id = ssrc_info->stream_id;
deadbeef9d3584c2016-02-16 17:54:10 -0800607 track_id = ssrc_info->track_id;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000608 }
Taylor Brandstetter5de6b752016-03-09 17:02:30 -0800609 // If a stream/track ID wasn't populated from the SSRC attributes OR the
610 // msid attribute, use default/random values.
611 if (stream_id.empty()) {
612 stream_id = kDefaultMsid;
613 }
614 if (track_id.empty()) {
615 // TODO(ronghuawu): What should we do if the track id doesn't appear?
616 // Create random string (which will be used as track label later)?
617 track_id = rtc::CreateRandomString(8);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000618 }
619
620 StreamParamsVec::iterator track = tracks->begin();
621 for (; track != tracks->end(); ++track) {
622 if (track->id == track_id) {
623 break;
624 }
625 }
626 if (track == tracks->end()) {
627 // If we don't find an existing track, create a new one.
628 tracks->push_back(StreamParams());
629 track = tracks->end() - 1;
630 }
631 track->add_ssrc(ssrc_info->ssrc_id);
632 track->cname = ssrc_info->cname;
Taylor Brandstetter5de6b752016-03-09 17:02:30 -0800633 track->sync_label = stream_id;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000634 track->id = track_id;
635 }
636}
637
638void GetMediaStreamLabels(const ContentInfo* content,
639 std::set<std::string>* labels) {
640 const MediaContentDescription* media_desc =
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000641 static_cast<const MediaContentDescription*>(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000642 content->description);
643 const cricket::StreamParamsVec& streams = media_desc->streams();
644 for (cricket::StreamParamsVec::const_iterator it = streams.begin();
645 it != streams.end(); ++it) {
646 labels->insert(it->sync_label);
647 }
648}
649
650// RFC 5245
651// It is RECOMMENDED that default candidates be chosen based on the
652// likelihood of those candidates to work with the peer that is being
653// contacted. It is RECOMMENDED that relayed > reflexive > host.
654static const int kPreferenceUnknown = 0;
655static const int kPreferenceHost = 1;
656static const int kPreferenceReflexive = 2;
657static const int kPreferenceRelayed = 3;
658
659static int GetCandidatePreferenceFromType(const std::string& type) {
660 int preference = kPreferenceUnknown;
661 if (type == cricket::LOCAL_PORT_TYPE) {
662 preference = kPreferenceHost;
663 } else if (type == cricket::STUN_PORT_TYPE) {
664 preference = kPreferenceReflexive;
665 } else if (type == cricket::RELAY_PORT_TYPE) {
666 preference = kPreferenceRelayed;
667 } else {
nissec80e7412017-01-11 05:56:46 -0800668 RTC_NOTREACHED();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000669 }
670 return preference;
671}
672
guoweis@webrtc.org57ac2c82015-02-06 00:45:13 +0000673// Get ip and port of the default destination from the |candidates| with the
674// given value of |component_id|. The default candidate should be the one most
675// likely to work, typically IPv4 relay.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000676// RFC 5245
677// The value of |component_id| currently supported are 1 (RTP) and 2 (RTCP).
678// TODO: Decide the default destination in webrtcsession and
679// pass it down via SessionDescription.
pthatcher@webrtc.orgc9d6d142014-10-23 23:37:22 +0000680static void GetDefaultDestination(
681 const std::vector<Candidate>& candidates,
682 int component_id, std::string* port,
683 std::string* ip, std::string* addr_type) {
perkj@webrtc.orgd105cc82014-11-07 11:22:06 +0000684 *addr_type = kConnectionIpv4Addrtype;
pthatcher@webrtc.orgc9d6d142014-10-23 23:37:22 +0000685 *port = kDummyPort;
686 *ip = kDummyAddress;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000687 int current_preference = kPreferenceUnknown;
guoweis@webrtc.org57ac2c82015-02-06 00:45:13 +0000688 int current_family = AF_UNSPEC;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000689 for (std::vector<Candidate>::const_iterator it = candidates.begin();
690 it != candidates.end(); ++it) {
691 if (it->component() != component_id) {
692 continue;
693 }
guoweis@webrtc.org57ac2c82015-02-06 00:45:13 +0000694 // Default destination should be UDP only.
695 if (it->protocol() != cricket::UDP_PROTOCOL_NAME) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000696 continue;
697 }
guoweis@webrtc.org57ac2c82015-02-06 00:45:13 +0000698 const int preference = GetCandidatePreferenceFromType(it->type());
699 const int family = it->address().ipaddr().family();
700 // See if this candidate is more preferable then the current one if it's the
701 // same family. Or if the current family is IPv4 already so we could safely
702 // ignore all IPv6 ones. WebRTC bug 4269.
703 // http://code.google.com/p/webrtc/issues/detail?id=4269
704 if ((preference <= current_preference && current_family == family) ||
705 (current_family == AF_INET && family == AF_INET6)) {
706 continue;
707 }
pthatcher@webrtc.orgc9d6d142014-10-23 23:37:22 +0000708 if (family == AF_INET) {
709 addr_type->assign(kConnectionIpv4Addrtype);
710 } else if (family == AF_INET6) {
711 addr_type->assign(kConnectionIpv6Addrtype);
712 }
guoweis@webrtc.org57ac2c82015-02-06 00:45:13 +0000713 current_preference = preference;
714 current_family = family;
715 *port = it->address().PortAsString();
716 *ip = it->address().ipaddr().ToString();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000717 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000718}
719
wu@webrtc.org4c3e9912014-07-16 21:03:13 +0000720// Gets "a=rtcp" line if found default RTCP candidate from |candidates|.
721static std::string GetRtcpLine(const std::vector<Candidate>& candidates) {
pthatcher@webrtc.orgc9d6d142014-10-23 23:37:22 +0000722 std::string rtcp_line, rtcp_port, rtcp_ip, addr_type;
723 GetDefaultDestination(candidates, ICE_CANDIDATE_COMPONENT_RTCP,
724 &rtcp_port, &rtcp_ip, &addr_type);
725 // Found default RTCP candidate.
726 // RFC 5245
727 // If the agent is utilizing RTCP, it MUST encode the RTCP candidate
728 // using the a=rtcp attribute as defined in RFC 3605.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000729
pthatcher@webrtc.orgc9d6d142014-10-23 23:37:22 +0000730 // RFC 3605
731 // rtcp-attribute = "a=rtcp:" port [nettype space addrtype space
732 // connection-address] CRLF
733 std::ostringstream os;
734 InitAttrLine(kAttributeRtcp, &os);
735 os << kSdpDelimiterColon
736 << rtcp_port << " "
737 << kConnectionNettype << " "
738 << addr_type << " "
739 << rtcp_ip;
740 rtcp_line = os.str();
wu@webrtc.org4c3e9912014-07-16 21:03:13 +0000741 return rtcp_line;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000742}
743
744// Get candidates according to the mline index from SessionDescriptionInterface.
745static void GetCandidatesByMindex(const SessionDescriptionInterface& desci,
746 int mline_index,
747 std::vector<Candidate>* candidates) {
748 if (!candidates) {
749 return;
750 }
751 const IceCandidateCollection* cc = desci.candidates(mline_index);
752 for (size_t i = 0; i < cc->count(); ++i) {
753 const IceCandidateInterface* candidate = cc->at(i);
754 candidates->push_back(candidate->candidate());
755 }
756}
757
deadbeef90f1e1e2017-02-10 12:35:05 -0800758static bool IsValidPort(int port) {
759 return port >= 0 && port <= 65535;
760}
761
deadbeef9d3584c2016-02-16 17:54:10 -0800762std::string SdpSerialize(const JsepSessionDescription& jdesc,
763 bool unified_plan_sdp) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000764 const cricket::SessionDescription* desc = jdesc.description();
765 if (!desc) {
766 return "";
767 }
768
769 std::string message;
770
771 // Session Description.
772 AddLine(kSessionVersion, &message);
773 // Session Origin
774 // RFC 4566
775 // o=<username> <sess-id> <sess-version> <nettype> <addrtype>
776 // <unicast-address>
777 std::ostringstream os;
778 InitLine(kLineTypeOrigin, kSessionOriginUsername, &os);
jbauch083b73f2015-07-16 02:46:32 -0700779 const std::string& session_id = jdesc.session_id().empty() ?
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000780 kSessionOriginSessionId : jdesc.session_id();
jbauch083b73f2015-07-16 02:46:32 -0700781 const std::string& session_version = jdesc.session_version().empty() ?
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000782 kSessionOriginSessionVersion : jdesc.session_version();
783 os << " " << session_id << " " << session_version << " "
784 << kSessionOriginNettype << " " << kSessionOriginAddrtype << " "
785 << kSessionOriginAddress;
786 AddLine(os.str(), &message);
787 AddLine(kSessionName, &message);
788
789 // Time Description.
790 AddLine(kTimeDescription, &message);
791
792 // Group
793 if (desc->HasGroup(cricket::GROUP_TYPE_BUNDLE)) {
794 std::string group_line = kAttrGroup;
795 const cricket::ContentGroup* group =
796 desc->GetGroupByName(cricket::GROUP_TYPE_BUNDLE);
nisseede5da42017-01-12 05:15:36 -0800797 RTC_DCHECK(group != NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000798 const cricket::ContentNames& content_names = group->content_names();
799 for (cricket::ContentNames::const_iterator it = content_names.begin();
800 it != content_names.end(); ++it) {
801 group_line.append(" ");
802 group_line.append(*it);
803 }
804 AddLine(group_line, &message);
805 }
806
807 // MediaStream semantics
808 InitAttrLine(kAttributeMsidSemantics, &os);
809 os << kSdpDelimiterColon << " " << kMediaStreamSemantic;
wu@webrtc.org4c3e9912014-07-16 21:03:13 +0000810
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000811 std::set<std::string> media_stream_labels;
812 const ContentInfo* audio_content = GetFirstAudioContent(desc);
813 if (audio_content)
814 GetMediaStreamLabels(audio_content, &media_stream_labels);
wu@webrtc.org4c3e9912014-07-16 21:03:13 +0000815
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000816 const ContentInfo* video_content = GetFirstVideoContent(desc);
817 if (video_content)
818 GetMediaStreamLabels(video_content, &media_stream_labels);
wu@webrtc.org4c3e9912014-07-16 21:03:13 +0000819
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000820 for (std::set<std::string>::const_iterator it =
821 media_stream_labels.begin(); it != media_stream_labels.end(); ++it) {
822 os << " " << *it;
823 }
824 AddLine(os.str(), &message);
825
wu@webrtc.org4c3e9912014-07-16 21:03:13 +0000826 // Preserve the order of the media contents.
827 int mline_index = -1;
828 for (cricket::ContentInfos::const_iterator it = desc->contents().begin();
829 it != desc->contents().end(); ++it) {
830 const MediaContentDescription* mdesc =
831 static_cast<const MediaContentDescription*>(it->description);
832 std::vector<Candidate> candidates;
833 GetCandidatesByMindex(jdesc, ++mline_index, &candidates);
deadbeef9d3584c2016-02-16 17:54:10 -0800834 BuildMediaDescription(&*it, desc->GetTransportInfoByName(it->name),
835 mdesc->type(), candidates, unified_plan_sdp,
wu@webrtc.org4c3e9912014-07-16 21:03:13 +0000836 &message);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000837 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000838 return message;
839}
840
841// Serializes the passed in IceCandidateInterface to a SDP string.
842// candidate - The candidate to be serialized.
Honghai Zhang7fb69db2016-03-14 11:59:18 -0700843std::string SdpSerializeCandidate(const IceCandidateInterface& candidate) {
844 return SdpSerializeCandidate(candidate.candidate());
845}
846
847// Serializes a cricket Candidate.
848std::string SdpSerializeCandidate(const cricket::Candidate& candidate) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000849 std::string message;
Honghai Zhang7fb69db2016-03-14 11:59:18 -0700850 std::vector<cricket::Candidate> candidates(1, candidate);
honghaiza54a0802015-12-16 18:37:23 -0800851 BuildCandidate(candidates, true, &message);
wu@webrtc.orgec9f5fb2014-06-24 17:05:10 +0000852 // From WebRTC draft section 4.8.1.1 candidate-attribute will be
853 // just candidate:<candidate> not a=candidate:<blah>CRLF
nisseede5da42017-01-12 05:15:36 -0800854 RTC_DCHECK(message.find("a=") == 0);
wu@webrtc.orgec9f5fb2014-06-24 17:05:10 +0000855 message.erase(0, 2);
nisseede5da42017-01-12 05:15:36 -0800856 RTC_DCHECK(message.find(kLineBreak) == message.size() - 2);
wu@webrtc.orgec9f5fb2014-06-24 17:05:10 +0000857 message.resize(message.size() - 2);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000858 return message;
859}
860
861bool SdpDeserialize(const std::string& message,
862 JsepSessionDescription* jdesc,
863 SdpParseError* error) {
864 std::string session_id;
865 std::string session_version;
Peter Thatcher7cbd1882015-09-17 18:54:52 -0700866 TransportDescription session_td("", "");
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000867 RtpHeaderExtensions session_extmaps;
zhihuang38989e52017-03-21 11:04:53 -0700868 rtc::SocketAddress session_connection_addr;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000869 cricket::SessionDescription* desc = new cricket::SessionDescription();
870 std::vector<JsepIceCandidate*> candidates;
871 size_t current_pos = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000872
873 // Session Description
874 if (!ParseSessionDescription(message, &current_pos, &session_id,
deadbeefc80741f2015-10-22 13:14:45 -0700875 &session_version, &session_td, &session_extmaps,
zhihuang38989e52017-03-21 11:04:53 -0700876 &session_connection_addr, desc, error)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000877 delete desc;
878 return false;
879 }
880
881 // Media Description
deadbeefc80741f2015-10-22 13:14:45 -0700882 if (!ParseMediaDescription(message, session_td, session_extmaps, &current_pos,
zhihuang38989e52017-03-21 11:04:53 -0700883 session_connection_addr, desc, &candidates,
884 error)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000885 delete desc;
886 for (std::vector<JsepIceCandidate*>::const_iterator
887 it = candidates.begin(); it != candidates.end(); ++it) {
888 delete *it;
889 }
890 return false;
891 }
892
893 jdesc->Initialize(desc, session_id, session_version);
894
895 for (std::vector<JsepIceCandidate*>::const_iterator
896 it = candidates.begin(); it != candidates.end(); ++it) {
897 jdesc->AddCandidate(*it);
898 delete *it;
899 }
900 return true;
901}
902
903bool SdpDeserializeCandidate(const std::string& message,
904 JsepIceCandidate* jcandidate,
905 SdpParseError* error) {
nisseede5da42017-01-12 05:15:36 -0800906 RTC_DCHECK(jcandidate != NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000907 Candidate candidate;
908 if (!ParseCandidate(message, &candidate, error, true)) {
909 return false;
910 }
911 jcandidate->SetCandidate(candidate);
912 return true;
913}
914
Honghai Zhang7fb69db2016-03-14 11:59:18 -0700915bool SdpDeserializeCandidate(const std::string& transport_name,
916 const std::string& message,
917 cricket::Candidate* candidate,
918 SdpParseError* error) {
nisseede5da42017-01-12 05:15:36 -0800919 RTC_DCHECK(candidate != nullptr);
Honghai Zhang7fb69db2016-03-14 11:59:18 -0700920 if (!ParseCandidate(message, candidate, error, true)) {
921 return false;
922 }
923 candidate->set_transport_name(transport_name);
924 return true;
925}
926
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000927bool ParseCandidate(const std::string& message, Candidate* candidate,
928 SdpParseError* error, bool is_raw) {
nisseede5da42017-01-12 05:15:36 -0800929 RTC_DCHECK(candidate != NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000930
931 // Get the first line from |message|.
jiayl@webrtc.org7ec3f9f2014-08-08 23:09:15 +0000932 std::string first_line = message;
933 size_t pos = 0;
934 GetLine(message, &pos, &first_line);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000935
jiayl@webrtc.org7ec3f9f2014-08-08 23:09:15 +0000936 // Makes sure |message| contains only one line.
937 if (message.size() > first_line.size()) {
938 std::string left, right;
Donald Curtis0e07f922015-05-15 09:21:23 -0700939 if (rtc::tokenize_first(message, kNewLine, &left, &right) &&
940 !right.empty()) {
jiayl@webrtc.org7ec3f9f2014-08-08 23:09:15 +0000941 return ParseFailed(message, 0, "Expect one line only", error);
942 }
943 }
944
945 // From WebRTC draft section 4.8.1.1 candidate-attribute should be
946 // candidate:<candidate> when trickled, but we still support
947 // a=candidate:<blah>CRLF for backward compatibility and for parsing a line
948 // from the SDP.
949 if (IsLineType(first_line, kLineTypeAttributes)) {
950 first_line = first_line.substr(kLinePrefixLength);
951 }
952
953 std::string attribute_candidate;
954 std::string candidate_value;
955
956 // |first_line| must be in the form of "candidate:<value>".
Donald Curtis144d0182015-05-15 13:14:24 -0700957 if (!rtc::tokenize_first(first_line, kSdpDelimiterColon, &attribute_candidate,
958 &candidate_value) ||
jiayl@webrtc.org7ec3f9f2014-08-08 23:09:15 +0000959 attribute_candidate != kAttributeCandidate) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000960 if (is_raw) {
961 std::ostringstream description;
jiayl@webrtc.org7ec3f9f2014-08-08 23:09:15 +0000962 description << "Expect line: " << kAttributeCandidate
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000963 << ":" << "<candidate-str>";
964 return ParseFailed(first_line, 0, description.str(), error);
965 } else {
966 return ParseFailedExpectLine(first_line, 0, kLineTypeAttributes,
967 kAttributeCandidate, error);
968 }
969 }
970
971 std::vector<std::string> fields;
jiayl@webrtc.org7ec3f9f2014-08-08 23:09:15 +0000972 rtc::split(candidate_value, kSdpDelimiterSpace, &fields);
973
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000974 // RFC 5245
975 // a=candidate:<foundation> <component-id> <transport> <priority>
976 // <connection-address> <port> typ <candidate-types>
977 // [raddr <connection-address>] [rport <port>]
978 // *(SP extension-att-name SP extension-att-value)
979 const size_t expected_min_fields = 8;
980 if (fields.size() < expected_min_fields ||
981 (fields[6] != kAttributeCandidateTyp)) {
982 return ParseFailedExpectMinFieldNum(first_line, expected_min_fields, error);
983 }
jbauch083b73f2015-07-16 02:46:32 -0700984 const std::string& foundation = fields[0];
jiayl@webrtc.org7ec3f9f2014-08-08 23:09:15 +0000985
wu@webrtc.org5e760e72014-04-02 23:19:09 +0000986 int component_id = 0;
987 if (!GetValueFromString(first_line, fields[1], &component_id, error)) {
988 return false;
989 }
jbauch083b73f2015-07-16 02:46:32 -0700990 const std::string& transport = fields[2];
Peter Boström0c4e06b2015-10-07 12:23:21 +0200991 uint32_t priority = 0;
wu@webrtc.org5e760e72014-04-02 23:19:09 +0000992 if (!GetValueFromString(first_line, fields[3], &priority, error)) {
993 return false;
994 }
jbauch083b73f2015-07-16 02:46:32 -0700995 const std::string& connection_address = fields[4];
wu@webrtc.org5e760e72014-04-02 23:19:09 +0000996 int port = 0;
997 if (!GetValueFromString(first_line, fields[5], &port, error)) {
998 return false;
999 }
deadbeef90f1e1e2017-02-10 12:35:05 -08001000 if (!IsValidPort(port)) {
1001 return ParseFailed(first_line, "Invalid port number.", error);
1002 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001003 SocketAddress address(connection_address, port);
1004
1005 cricket::ProtocolType protocol;
hnslb68cc752016-12-13 10:33:41 -08001006 if (!StringToProto(transport.c_str(), &protocol)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001007 return ParseFailed(first_line, "Unsupported transport type.", error);
1008 }
hnslb68cc752016-12-13 10:33:41 -08001009 switch (protocol) {
1010 case cricket::PROTO_UDP:
1011 case cricket::PROTO_TCP:
1012 case cricket::PROTO_SSLTCP:
1013 // Supported protocol.
1014 break;
1015 default:
1016 return ParseFailed(first_line, "Unsupported transport type.", error);
1017 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001018
1019 std::string candidate_type;
jbauch083b73f2015-07-16 02:46:32 -07001020 const std::string& type = fields[7];
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001021 if (type == kCandidateHost) {
1022 candidate_type = cricket::LOCAL_PORT_TYPE;
1023 } else if (type == kCandidateSrflx) {
1024 candidate_type = cricket::STUN_PORT_TYPE;
1025 } else if (type == kCandidateRelay) {
1026 candidate_type = cricket::RELAY_PORT_TYPE;
Honghai Zhang7fb69db2016-03-14 11:59:18 -07001027 } else if (type == kCandidatePrflx) {
1028 candidate_type = cricket::PRFLX_PORT_TYPE;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001029 } else {
1030 return ParseFailed(first_line, "Unsupported candidate type.", error);
1031 }
1032
1033 size_t current_position = expected_min_fields;
1034 SocketAddress related_address;
1035 // The 2 optional fields for related address
1036 // [raddr <connection-address>] [rport <port>]
1037 if (fields.size() >= (current_position + 2) &&
1038 fields[current_position] == kAttributeCandidateRaddr) {
1039 related_address.SetIP(fields[++current_position]);
1040 ++current_position;
1041 }
1042 if (fields.size() >= (current_position + 2) &&
1043 fields[current_position] == kAttributeCandidateRport) {
wu@webrtc.org5e760e72014-04-02 23:19:09 +00001044 int port = 0;
1045 if (!GetValueFromString(
1046 first_line, fields[++current_position], &port, error)) {
1047 return false;
1048 }
deadbeef90f1e1e2017-02-10 12:35:05 -08001049 if (!IsValidPort(port)) {
1050 return ParseFailed(first_line, "Invalid port number.", error);
1051 }
wu@webrtc.org5e760e72014-04-02 23:19:09 +00001052 related_address.SetPort(port);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001053 ++current_position;
1054 }
1055
mallinath@webrtc.org2d60c5e2014-08-08 22:29:20 +00001056 // If this is a TCP candidate, it has additional extension as defined in
1057 // RFC 6544.
1058 std::string tcptype;
1059 if (fields.size() >= (current_position + 2) &&
1060 fields[current_position] == kTcpCandidateType) {
1061 tcptype = fields[++current_position];
1062 ++current_position;
1063
1064 if (tcptype != cricket::TCPTYPE_ACTIVE_STR &&
1065 tcptype != cricket::TCPTYPE_PASSIVE_STR &&
1066 tcptype != cricket::TCPTYPE_SIMOPEN_STR) {
1067 return ParseFailed(first_line, "Invalid TCP candidate type.", error);
1068 }
1069
1070 if (protocol != cricket::PROTO_TCP) {
1071 return ParseFailed(first_line, "Invalid non-TCP candidate", error);
1072 }
1073 }
1074
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001075 // Extension
honghaiza54a0802015-12-16 18:37:23 -08001076 // Though non-standard, we support the ICE ufrag and pwd being signaled on
1077 // the candidate to avoid issues with confusing which generation a candidate
1078 // belongs to when trickling multiple generations at the same time.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001079 std::string username;
1080 std::string password;
Peter Boström0c4e06b2015-10-07 12:23:21 +02001081 uint32_t generation = 0;
honghaiza0c44ea2016-03-23 16:07:48 -07001082 uint16_t network_id = 0;
1083 uint16_t network_cost = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001084 for (size_t i = current_position; i + 1 < fields.size(); ++i) {
1085 // RFC 5245
1086 // *(SP extension-att-name SP extension-att-value)
1087 if (fields[i] == kAttributeCandidateGeneration) {
wu@webrtc.org5e760e72014-04-02 23:19:09 +00001088 if (!GetValueFromString(first_line, fields[++i], &generation, error)) {
1089 return false;
1090 }
honghaiza54a0802015-12-16 18:37:23 -08001091 } else if (fields[i] == kAttributeCandidateUfrag) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001092 username = fields[++i];
honghaiza54a0802015-12-16 18:37:23 -08001093 } else if (fields[i] == kAttributeCandidatePwd) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001094 password = fields[++i];
honghaiza0c44ea2016-03-23 16:07:48 -07001095 } else if (fields[i] == kAttributeCandidateNetworkId) {
1096 if (!GetValueFromString(first_line, fields[++i], &network_id, error)) {
1097 return false;
1098 }
honghaize1a0c942016-02-16 14:54:56 -08001099 } else if (fields[i] == kAttributeCandidateNetworkCost) {
1100 if (!GetValueFromString(first_line, fields[++i], &network_cost, error)) {
1101 return false;
1102 }
Honghai Zhang351d77b2016-05-20 15:08:29 -07001103 network_cost = std::min(network_cost, rtc::kNetworkCostMax);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001104 } else {
1105 // Skip the unknown extension.
1106 ++i;
1107 }
1108 }
1109
guoweis@webrtc.org61c12472015-01-15 06:53:07 +00001110 *candidate = Candidate(component_id, cricket::ProtoToString(protocol),
guoweis@webrtc.org950c5182014-12-16 23:01:31 +00001111 address, priority, username, password, candidate_type,
honghaiza0c44ea2016-03-23 16:07:48 -07001112 generation, foundation, network_id, network_cost);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001113 candidate->set_related_address(related_address);
mallinath@webrtc.org2d60c5e2014-08-08 22:29:20 +00001114 candidate->set_tcptype(tcptype);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001115 return true;
1116}
1117
1118bool ParseIceOptions(const std::string& line,
1119 std::vector<std::string>* transport_options,
1120 SdpParseError* error) {
1121 std::string ice_options;
1122 if (!GetValue(line, kAttributeIceOption, &ice_options, error)) {
1123 return false;
1124 }
1125 std::vector<std::string> fields;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001126 rtc::split(ice_options, kSdpDelimiterSpace, &fields);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001127 for (size_t i = 0; i < fields.size(); ++i) {
1128 transport_options->push_back(fields[i]);
1129 }
1130 return true;
1131}
1132
jiayl@webrtc.orgddb85ab2014-09-05 16:31:56 +00001133bool ParseSctpPort(const std::string& line,
1134 int* sctp_port,
1135 SdpParseError* error) {
1136 // draft-ietf-mmusic-sctp-sdp-07
1137 // a=sctp-port
1138 std::vector<std::string> fields;
jiayl@webrtc.orgddb85ab2014-09-05 16:31:56 +00001139 const size_t expected_min_fields = 2;
lally69f57602015-10-08 10:15:04 -07001140 rtc::split(line.substr(kLinePrefixLength), kSdpDelimiterColon, &fields);
1141 if (fields.size() < expected_min_fields) {
1142 fields.resize(0);
1143 rtc::split(line.substr(kLinePrefixLength), kSdpDelimiterSpace, &fields);
1144 }
jiayl@webrtc.orgddb85ab2014-09-05 16:31:56 +00001145 if (fields.size() < expected_min_fields) {
1146 return ParseFailedExpectMinFieldNum(line, expected_min_fields, error);
1147 }
1148 if (!rtc::FromString(fields[1], sctp_port)) {
lally69f57602015-10-08 10:15:04 -07001149 return ParseFailed(line, "Invalid sctp port value.", error);
jiayl@webrtc.orgddb85ab2014-09-05 16:31:56 +00001150 }
1151 return true;
1152}
1153
isheriff6f8d6862016-05-26 11:24:55 -07001154bool ParseExtmap(const std::string& line,
1155 RtpExtension* extmap,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001156 SdpParseError* error) {
1157 // RFC 5285
1158 // a=extmap:<value>["/"<direction>] <URI> <extensionattributes>
1159 std::vector<std::string> fields;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001160 rtc::split(line.substr(kLinePrefixLength),
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001161 kSdpDelimiterSpace, &fields);
1162 const size_t expected_min_fields = 2;
1163 if (fields.size() < expected_min_fields) {
1164 return ParseFailedExpectMinFieldNum(line, expected_min_fields, error);
1165 }
1166 std::string uri = fields[1];
1167
1168 std::string value_direction;
1169 if (!GetValue(fields[0], kAttributeExtmap, &value_direction, error)) {
1170 return false;
1171 }
1172 std::vector<std::string> sub_fields;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001173 rtc::split(value_direction, kSdpDelimiterSlash, &sub_fields);
wu@webrtc.org5e760e72014-04-02 23:19:09 +00001174 int value = 0;
1175 if (!GetValueFromString(line, sub_fields[0], &value, error)) {
1176 return false;
1177 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001178
isheriff6f8d6862016-05-26 11:24:55 -07001179 *extmap = RtpExtension(uri, value);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001180 return true;
1181}
1182
1183void BuildMediaDescription(const ContentInfo* content_info,
1184 const TransportInfo* transport_info,
1185 const MediaType media_type,
wu@webrtc.org4c3e9912014-07-16 21:03:13 +00001186 const std::vector<Candidate>& candidates,
deadbeef9d3584c2016-02-16 17:54:10 -08001187 bool unified_plan_sdp,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001188 std::string* message) {
nisseede5da42017-01-12 05:15:36 -08001189 RTC_DCHECK(message != NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001190 if (content_info == NULL || message == NULL) {
1191 return;
1192 }
1193 // TODO: Rethink if we should use sprintfn instead of stringstream.
1194 // According to the style guide, streams should only be used for logging.
1195 // http://google-styleguide.googlecode.com/svn/
1196 // trunk/cppguide.xml?showone=Streams#Streams
1197 std::ostringstream os;
1198 const MediaContentDescription* media_desc =
henrike@webrtc.org28654cb2013-07-22 21:07:49 +00001199 static_cast<const MediaContentDescription*>(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001200 content_info->description);
nisseede5da42017-01-12 05:15:36 -08001201 RTC_DCHECK(media_desc != NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001202
jiayl@webrtc.org9c16c392014-05-01 18:30:30 +00001203 int sctp_port = cricket::kSctpDefaultPort;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001204
1205 // RFC 4566
1206 // m=<media> <port> <proto> <fmt>
1207 // fmt is a list of payload type numbers that MAY be used in the session.
1208 const char* type = NULL;
1209 if (media_type == cricket::MEDIA_TYPE_AUDIO)
1210 type = kMediaTypeAudio;
1211 else if (media_type == cricket::MEDIA_TYPE_VIDEO)
1212 type = kMediaTypeVideo;
1213 else if (media_type == cricket::MEDIA_TYPE_DATA)
1214 type = kMediaTypeData;
1215 else
nissec80e7412017-01-11 05:56:46 -08001216 RTC_NOTREACHED();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001217
1218 std::string fmt;
1219 if (media_type == cricket::MEDIA_TYPE_VIDEO) {
1220 const VideoContentDescription* video_desc =
1221 static_cast<const VideoContentDescription*>(media_desc);
1222 for (std::vector<cricket::VideoCodec>::const_iterator it =
1223 video_desc->codecs().begin();
1224 it != video_desc->codecs().end(); ++it) {
1225 fmt.append(" ");
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001226 fmt.append(rtc::ToString<int>(it->id));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001227 }
1228 } else if (media_type == cricket::MEDIA_TYPE_AUDIO) {
1229 const AudioContentDescription* audio_desc =
1230 static_cast<const AudioContentDescription*>(media_desc);
1231 for (std::vector<cricket::AudioCodec>::const_iterator it =
1232 audio_desc->codecs().begin();
1233 it != audio_desc->codecs().end(); ++it) {
1234 fmt.append(" ");
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001235 fmt.append(rtc::ToString<int>(it->id));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001236 }
1237 } else if (media_type == cricket::MEDIA_TYPE_DATA) {
jiayl@webrtc.org9c16c392014-05-01 18:30:30 +00001238 const DataContentDescription* data_desc =
1239 static_cast<const DataContentDescription*>(media_desc);
pthatcher@webrtc.org3341b402015-02-13 21:14:22 +00001240 if (IsDtlsSctp(media_desc->protocol())) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001241 fmt.append(" ");
jiayl@webrtc.org9c16c392014-05-01 18:30:30 +00001242
zstein4b2e0822017-02-17 19:48:38 -08001243 if (data_desc->use_sctpmap()) {
1244 for (std::vector<cricket::DataCodec>::const_iterator it =
1245 data_desc->codecs().begin();
1246 it != data_desc->codecs().end(); ++it) {
1247 if (cricket::CodecNamesEq(it->name,
1248 cricket::kGoogleSctpDataCodecName) &&
1249 it->GetParam(cricket::kCodecParamPort, &sctp_port)) {
1250 break;
1251 }
jiayl@webrtc.org9c16c392014-05-01 18:30:30 +00001252 }
jiayl@webrtc.org9c16c392014-05-01 18:30:30 +00001253
zstein4b2e0822017-02-17 19:48:38 -08001254 fmt.append(rtc::ToString<int>(sctp_port));
1255 } else {
1256 fmt.append(kDefaultSctpmapProtocol);
1257 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001258 } else {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001259 for (std::vector<cricket::DataCodec>::const_iterator it =
1260 data_desc->codecs().begin();
1261 it != data_desc->codecs().end(); ++it) {
1262 fmt.append(" ");
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001263 fmt.append(rtc::ToString<int>(it->id));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001264 }
1265 }
1266 }
1267 // The fmt must never be empty. If no codecs are found, set the fmt attribute
1268 // to 0.
1269 if (fmt.empty()) {
1270 fmt = " 0";
1271 }
1272
deadbeef25ed4352016-12-12 18:37:36 -08001273 // The port number in the m line will be updated later when associated with
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001274 // the candidates.
deadbeef25ed4352016-12-12 18:37:36 -08001275 //
1276 // A port value of 0 indicates that the m= section is rejected.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001277 // RFC 3264
1278 // To reject an offered stream, the port number in the corresponding stream in
1279 // the answer MUST be set to zero.
deadbeef25ed4352016-12-12 18:37:36 -08001280 //
1281 // However, the BUNDLE draft adds a new meaning to port zero, when used along
1282 // with a=bundle-only.
zhihuang38989e52017-03-21 11:04:53 -07001283 std::string port = kDummyPort;
1284 if (content_info->rejected || content_info->bundle_only) {
1285 port = kMediaPortRejected;
1286 } else if (!media_desc->connection_address().IsNil()) {
1287 port = rtc::ToString(media_desc->connection_address().port());
1288 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001289
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001290 rtc::SSLFingerprint* fp = (transport_info) ?
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001291 transport_info->description.identity_fingerprint.get() : NULL;
1292
wu@webrtc.org4c3e9912014-07-16 21:03:13 +00001293 // Add the m and c lines.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001294 InitLine(kLineTypeMedia, type, &os);
1295 os << " " << port << " " << media_desc->protocol() << fmt;
zhihuang38989e52017-03-21 11:04:53 -07001296 AddLine(os.str(), message);
1297
1298 InitLine(kLineTypeConnection, kConnectionNettype, &os);
1299 if (media_desc->connection_address().IsNil()) {
1300 os << " " << kConnectionIpv4Addrtype << " " << kDummyAddress;
1301 } else if (media_desc->connection_address().family() == AF_INET) {
1302 os << " " << kConnectionIpv4Addrtype << " "
1303 << media_desc->connection_address().ipaddr().ToString();
1304 } else {
1305 os << " " << kConnectionIpv6Addrtype << " "
1306 << media_desc->connection_address().ipaddr().ToString();
1307 }
1308 AddLine(os.str(), message);
wu@webrtc.org4c3e9912014-07-16 21:03:13 +00001309
1310 // RFC 4566
1311 // b=AS:<bandwidth>
Peter Thatcherc0c3a862015-06-24 15:31:25 -07001312 if (media_desc->bandwidth() >= 1000) {
wu@webrtc.org4c3e9912014-07-16 21:03:13 +00001313 InitLine(kLineTypeSessionBandwidth, kApplicationSpecificMaximum, &os);
1314 os << kSdpDelimiterColon << (media_desc->bandwidth() / 1000);
1315 AddLine(os.str(), message);
1316 }
1317
deadbeef25ed4352016-12-12 18:37:36 -08001318 // Add the a=bundle-only line.
1319 if (content_info->bundle_only) {
1320 InitAttrLine(kAttributeBundleOnly, &os);
1321 AddLine(os.str(), message);
1322 }
1323
wu@webrtc.org4c3e9912014-07-16 21:03:13 +00001324 // Add the a=rtcp line.
pthatcher@webrtc.org3341b402015-02-13 21:14:22 +00001325 if (IsRtp(media_desc->protocol())) {
wu@webrtc.org4c3e9912014-07-16 21:03:13 +00001326 std::string rtcp_line = GetRtcpLine(candidates);
1327 if (!rtcp_line.empty()) {
1328 AddLine(rtcp_line, message);
1329 }
1330 }
1331
honghaiza54a0802015-12-16 18:37:23 -08001332 // Build the a=candidate lines. We don't include ufrag and pwd in the
1333 // candidates in the SDP to avoid redundancy.
1334 BuildCandidate(candidates, false, message);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001335
1336 // Use the transport_info to build the media level ice-ufrag and ice-pwd.
1337 if (transport_info) {
1338 // RFC 5245
1339 // ice-pwd-att = "ice-pwd" ":" password
1340 // ice-ufrag-att = "ice-ufrag" ":" ufrag
1341 // ice-ufrag
deadbeef3f7219b2015-12-28 15:17:14 -08001342 if (!transport_info->description.ice_ufrag.empty()) {
1343 InitAttrLine(kAttributeIceUfrag, &os);
1344 os << kSdpDelimiterColon << transport_info->description.ice_ufrag;
1345 AddLine(os.str(), message);
1346 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001347 // ice-pwd
deadbeef3f7219b2015-12-28 15:17:14 -08001348 if (!transport_info->description.ice_pwd.empty()) {
1349 InitAttrLine(kAttributeIcePwd, &os);
1350 os << kSdpDelimiterColon << transport_info->description.ice_pwd;
1351 AddLine(os.str(), message);
1352 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001353
1354 // draft-petithuguenin-mmusic-ice-attributes-level-03
1355 BuildIceOptions(transport_info->description.transport_options, message);
1356
1357 // RFC 4572
1358 // fingerprint-attribute =
1359 // "fingerprint" ":" hash-func SP fingerprint
1360 if (fp) {
1361 // Insert the fingerprint attribute.
1362 InitAttrLine(kAttributeFingerprint, &os);
1363 os << kSdpDelimiterColon
1364 << fp->algorithm << kSdpDelimiterSpace
1365 << fp->GetRfc4572Fingerprint();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001366 AddLine(os.str(), message);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001367
1368 // Inserting setup attribute.
1369 if (transport_info->description.connection_role !=
1370 cricket::CONNECTIONROLE_NONE) {
1371 // Making sure we are not using "passive" mode.
1372 cricket::ConnectionRole role =
1373 transport_info->description.connection_role;
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00001374 std::string dtls_role_str;
nissec16fa5e2017-02-07 07:18:43 -08001375 const bool success =
1376 cricket::ConnectionRoleToString(role, &dtls_role_str);
1377 RTC_DCHECK(success);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001378 InitAttrLine(kAttributeSetup, &os);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001379 os << kSdpDelimiterColon << dtls_role_str;
1380 AddLine(os.str(), message);
1381 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001382 }
1383 }
1384
1385 // RFC 3388
1386 // mid-attribute = "a=mid:" identification-tag
1387 // identification-tag = token
1388 // Use the content name as the mid identification-tag.
1389 InitAttrLine(kAttributeMid, &os);
1390 os << kSdpDelimiterColon << content_info->name;
1391 AddLine(os.str(), message);
1392
pthatcher@webrtc.org3341b402015-02-13 21:14:22 +00001393 if (IsDtlsSctp(media_desc->protocol())) {
zstein4b2e0822017-02-17 19:48:38 -08001394 const DataContentDescription* data_desc =
1395 static_cast<const DataContentDescription*>(media_desc);
1396 bool use_sctpmap = data_desc->use_sctpmap();
1397 BuildSctpContentAttributes(message, sctp_port, use_sctpmap);
pthatcher@webrtc.org3341b402015-02-13 21:14:22 +00001398 } else if (IsRtp(media_desc->protocol())) {
deadbeef9d3584c2016-02-16 17:54:10 -08001399 BuildRtpContentAttributes(media_desc, media_type, unified_plan_sdp,
1400 message);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001401 }
1402}
1403
zstein4b2e0822017-02-17 19:48:38 -08001404void BuildSctpContentAttributes(std::string* message,
1405 int sctp_port,
1406 bool use_sctpmap) {
wu@webrtc.org78187522013-10-07 23:32:02 +00001407 std::ostringstream os;
zstein4b2e0822017-02-17 19:48:38 -08001408 if (use_sctpmap) {
1409 // draft-ietf-mmusic-sctp-sdp-04
1410 // a=sctpmap:sctpmap-number protocol [streams]
1411 InitAttrLine(kAttributeSctpmap, &os);
1412 os << kSdpDelimiterColon << sctp_port << kSdpDelimiterSpace
1413 << kDefaultSctpmapProtocol << kSdpDelimiterSpace
1414 << cricket::kMaxSctpStreams;
1415 } else {
1416 // draft-ietf-mmusic-sctp-sdp-23
1417 // a=sctp-port:<port>
1418 InitAttrLine(kAttributeSctpPort, &os);
1419 os << kSdpDelimiterColon << sctp_port;
1420 // TODO(zstein): emit max-message-size here
1421 }
wu@webrtc.org78187522013-10-07 23:32:02 +00001422 AddLine(os.str(), message);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001423}
1424
deadbeef9d3584c2016-02-16 17:54:10 -08001425// If unified_plan_sdp is true, will use "a=msid".
1426void BuildRtpContentAttributes(const MediaContentDescription* media_desc,
1427 const MediaType media_type,
1428 bool unified_plan_sdp,
1429 std::string* message) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001430 std::ostringstream os;
1431 // RFC 5285
1432 // a=extmap:<value>["/"<direction>] <URI> <extensionattributes>
1433 // The definitions MUST be either all session level or all media level. This
1434 // implementation uses all media level.
1435 for (size_t i = 0; i < media_desc->rtp_header_extensions().size(); ++i) {
1436 InitAttrLine(kAttributeExtmap, &os);
1437 os << kSdpDelimiterColon << media_desc->rtp_header_extensions()[i].id
1438 << kSdpDelimiterSpace << media_desc->rtp_header_extensions()[i].uri;
1439 AddLine(os.str(), message);
1440 }
1441
1442 // RFC 3264
1443 // a=sendrecv || a=sendonly || a=sendrecv || a=inactive
deadbeefc80741f2015-10-22 13:14:45 -07001444 switch (media_desc->direction()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001445 case cricket::MD_INACTIVE:
1446 InitAttrLine(kAttributeInactive, &os);
1447 break;
1448 case cricket::MD_SENDONLY:
1449 InitAttrLine(kAttributeSendOnly, &os);
1450 break;
1451 case cricket::MD_RECVONLY:
1452 InitAttrLine(kAttributeRecvOnly, &os);
1453 break;
1454 case cricket::MD_SENDRECV:
1455 default:
1456 InitAttrLine(kAttributeSendRecv, &os);
1457 break;
1458 }
1459 AddLine(os.str(), message);
1460
deadbeef9d3584c2016-02-16 17:54:10 -08001461 // draft-ietf-mmusic-msid-11
1462 // a=msid:<stream id> <track id>
1463 if (unified_plan_sdp && !media_desc->streams().empty()) {
1464 if (media_desc->streams().size() > 1u) {
1465 LOG(LS_WARNING) << "Trying to serialize unified plan SDP with more than "
1466 << "one track in a media section. Omitting 'a=msid'.";
1467 } else {
1468 auto track = media_desc->streams().begin();
1469 const std::string& stream_id = track->sync_label;
deadbeef9d3584c2016-02-16 17:54:10 -08001470 InitAttrLine(kAttributeMsid, &os);
1471 os << kSdpDelimiterColon << stream_id << kSdpDelimiterSpace << track->id;
1472 AddLine(os.str(), message);
1473 }
1474 }
1475
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001476 // RFC 5761
1477 // a=rtcp-mux
1478 if (media_desc->rtcp_mux()) {
1479 InitAttrLine(kAttributeRtcpMux, &os);
1480 AddLine(os.str(), message);
1481 }
1482
deadbeef13871492015-12-09 12:37:51 -08001483 // RFC 5506
1484 // a=rtcp-rsize
1485 if (media_desc->rtcp_reduced_size()) {
1486 InitAttrLine(kAttributeRtcpReducedSize, &os);
1487 AddLine(os.str(), message);
1488 }
1489
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001490 // RFC 4568
1491 // a=crypto:<tag> <crypto-suite> <key-params> [<session-params>]
1492 for (std::vector<CryptoParams>::const_iterator it =
1493 media_desc->cryptos().begin();
1494 it != media_desc->cryptos().end(); ++it) {
1495 InitAttrLine(kAttributeCrypto, &os);
1496 os << kSdpDelimiterColon << it->tag << " " << it->cipher_suite << " "
1497 << it->key_params;
1498 if (!it->session_params.empty()) {
1499 os << " " << it->session_params;
1500 }
1501 AddLine(os.str(), message);
1502 }
1503
1504 // RFC 4566
1505 // a=rtpmap:<payload type> <encoding name>/<clock rate>
1506 // [/<encodingparameters>]
1507 BuildRtpMap(media_desc, media_type, message);
1508
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001509 for (StreamParamsVec::const_iterator track = media_desc->streams().begin();
1510 track != media_desc->streams().end(); ++track) {
1511 // Require that the track belongs to a media stream,
1512 // ie the sync_label is set. This extra check is necessary since the
1513 // MediaContentDescription always contains a streamparam with an ssrc even
1514 // if no track or media stream have been created.
1515 if (track->sync_label.empty()) continue;
1516
1517 // Build the ssrc-group lines.
1518 for (size_t i = 0; i < track->ssrc_groups.size(); ++i) {
1519 // RFC 5576
1520 // a=ssrc-group:<semantics> <ssrc-id> ...
1521 if (track->ssrc_groups[i].ssrcs.empty()) {
1522 continue;
1523 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001524 InitAttrLine(kAttributeSsrcGroup, &os);
1525 os << kSdpDelimiterColon << track->ssrc_groups[i].semantics;
Peter Boström0c4e06b2015-10-07 12:23:21 +02001526 std::vector<uint32_t>::const_iterator ssrc =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001527 track->ssrc_groups[i].ssrcs.begin();
1528 for (; ssrc != track->ssrc_groups[i].ssrcs.end(); ++ssrc) {
Peter Boström0c4e06b2015-10-07 12:23:21 +02001529 os << kSdpDelimiterSpace << rtc::ToString<uint32_t>(*ssrc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001530 }
1531 AddLine(os.str(), message);
1532 }
1533 // Build the ssrc lines for each ssrc.
1534 for (size_t i = 0; i < track->ssrcs.size(); ++i) {
Peter Boström0c4e06b2015-10-07 12:23:21 +02001535 uint32_t ssrc = track->ssrcs[i];
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001536 // RFC 5576
1537 // a=ssrc:<ssrc-id> cname:<value>
1538 AddSsrcLine(ssrc, kSsrcAttributeCname,
1539 track->cname, message);
1540
1541 // draft-alvestrand-mmusic-msid-00
1542 // a=ssrc:<ssrc-id> msid:identifier [appdata]
deadbeef9d3584c2016-02-16 17:54:10 -08001543 // The appdata consists of the "id" attribute of a MediaStreamTrack,
1544 // which corresponds to the "id" attribute of StreamParams.
1545 const std::string& stream_id = track->sync_label;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001546 InitAttrLine(kAttributeSsrc, &os);
1547 os << kSdpDelimiterColon << ssrc << kSdpDelimiterSpace
deadbeef9d3584c2016-02-16 17:54:10 -08001548 << kSsrcAttributeMsid << kSdpDelimiterColon << stream_id
1549 << kSdpDelimiterSpace << track->id;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001550 AddLine(os.str(), message);
1551
deadbeef9d3584c2016-02-16 17:54:10 -08001552 // TODO(ronghuawu): Remove below code which is for backward
1553 // compatibility.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001554 // draft-alvestrand-rtcweb-mid-01
1555 // a=ssrc:<ssrc-id> mslabel:<value>
1556 // The label isn't yet defined.
1557 // a=ssrc:<ssrc-id> label:<value>
1558 AddSsrcLine(ssrc, kSsrcAttributeMslabel, track->sync_label, message);
1559 AddSsrcLine(ssrc, kSSrcAttributeLabel, track->id, message);
1560 }
1561 }
1562}
1563
1564void WriteFmtpHeader(int payload_type, std::ostringstream* os) {
1565 // fmtp header: a=fmtp:|payload_type| <parameters>
1566 // Add a=fmtp
1567 InitAttrLine(kAttributeFmtp, os);
1568 // Add :|payload_type|
1569 *os << kSdpDelimiterColon << payload_type;
1570}
1571
1572void WriteRtcpFbHeader(int payload_type, std::ostringstream* os) {
1573 // rtcp-fb header: a=rtcp-fb:|payload_type|
1574 // <parameters>/<ccm <ccm_parameters>>
1575 // Add a=rtcp-fb
1576 InitAttrLine(kAttributeRtcpFb, os);
1577 // Add :
1578 *os << kSdpDelimiterColon;
1579 if (payload_type == kWildcardPayloadType) {
1580 *os << "*";
1581 } else {
1582 *os << payload_type;
1583 }
1584}
1585
1586void WriteFmtpParameter(const std::string& parameter_name,
1587 const std::string& parameter_value,
1588 std::ostringstream* os) {
1589 // fmtp parameters: |parameter_name|=|parameter_value|
1590 *os << parameter_name << kSdpDelimiterEqual << parameter_value;
1591}
1592
1593void WriteFmtpParameters(const cricket::CodecParameterMap& parameters,
1594 std::ostringstream* os) {
1595 for (cricket::CodecParameterMap::const_iterator fmtp = parameters.begin();
1596 fmtp != parameters.end(); ++fmtp) {
hta62a216e2016-04-15 11:02:14 -07001597 // Parameters are a semicolon-separated list, no spaces.
1598 // The list is separated from the header by a space.
1599 if (fmtp == parameters.begin()) {
1600 *os << kSdpDelimiterSpace;
1601 } else {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001602 *os << kSdpDelimiterSemicolon;
1603 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001604 WriteFmtpParameter(fmtp->first, fmtp->second, os);
1605 }
1606}
1607
1608bool IsFmtpParam(const std::string& name) {
ossuaa4b0772017-01-30 07:41:18 -08001609 // RFC 4855, section 3 specifies the mapping of media format parameters to SDP
1610 // parameters. Only ptime, maxptime, channels and rate are placed outside of
1611 // the fmtp line. In WebRTC, channels and rate are already handled separately
1612 // and thus not included in the CodecParameterMap.
1613 return name != kCodecParamPTime && name != kCodecParamMaxPTime;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001614}
1615
1616// Retreives fmtp parameters from |params|, which may contain other parameters
1617// as well, and puts them in |fmtp_parameters|.
1618void GetFmtpParams(const cricket::CodecParameterMap& params,
1619 cricket::CodecParameterMap* fmtp_parameters) {
1620 for (cricket::CodecParameterMap::const_iterator iter = params.begin();
1621 iter != params.end(); ++iter) {
1622 if (IsFmtpParam(iter->first)) {
1623 (*fmtp_parameters)[iter->first] = iter->second;
1624 }
1625 }
1626}
1627
1628template <class T>
1629void AddFmtpLine(const T& codec, std::string* message) {
1630 cricket::CodecParameterMap fmtp_parameters;
1631 GetFmtpParams(codec.params, &fmtp_parameters);
1632 if (fmtp_parameters.empty()) {
1633 // No need to add an fmtp if it will have no (optional) parameters.
1634 return;
1635 }
1636 std::ostringstream os;
1637 WriteFmtpHeader(codec.id, &os);
1638 WriteFmtpParameters(fmtp_parameters, &os);
1639 AddLine(os.str(), message);
1640 return;
1641}
1642
1643template <class T>
1644void AddRtcpFbLines(const T& codec, std::string* message) {
1645 for (std::vector<cricket::FeedbackParam>::const_iterator iter =
1646 codec.feedback_params.params().begin();
1647 iter != codec.feedback_params.params().end(); ++iter) {
1648 std::ostringstream os;
1649 WriteRtcpFbHeader(codec.id, &os);
1650 os << " " << iter->id();
1651 if (!iter->param().empty()) {
1652 os << " " << iter->param();
1653 }
1654 AddLine(os.str(), message);
1655 }
1656}
1657
jiayl@webrtc.orgddb85ab2014-09-05 16:31:56 +00001658bool AddSctpDataCodec(DataContentDescription* media_desc,
1659 int sctp_port) {
solenberg9fa49752016-10-08 13:02:44 -07001660 for (const auto& codec : media_desc->codecs()) {
1661 if (cricket::CodecNamesEq(codec.name, cricket::kGoogleSctpDataCodecName)) {
1662 return ParseFailed("",
1663 "Can't have multiple sctp port attributes.",
1664 NULL);
1665 }
jiayl@webrtc.orgddb85ab2014-09-05 16:31:56 +00001666 }
1667 // Add the SCTP Port number as a pseudo-codec "port" parameter
solenberg9fa49752016-10-08 13:02:44 -07001668 cricket::DataCodec codec_port(cricket::kGoogleSctpDataCodecPlType,
deadbeef67cf2c12016-04-13 10:07:16 -07001669 cricket::kGoogleSctpDataCodecName);
jiayl@webrtc.orgddb85ab2014-09-05 16:31:56 +00001670 codec_port.SetParam(cricket::kCodecParamPort, sctp_port);
1671 LOG(INFO) << "AddSctpDataCodec: Got SCTP Port Number "
1672 << sctp_port;
1673 media_desc->AddCodec(codec_port);
1674 return true;
1675}
1676
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001677bool GetMinValue(const std::vector<int>& values, int* value) {
1678 if (values.empty()) {
1679 return false;
1680 }
1681 std::vector<int>::const_iterator found =
1682 std::min_element(values.begin(), values.end());
1683 *value = *found;
1684 return true;
1685}
1686
1687bool GetParameter(const std::string& name,
1688 const cricket::CodecParameterMap& params, int* value) {
1689 std::map<std::string, std::string>::const_iterator found =
1690 params.find(name);
1691 if (found == params.end()) {
1692 return false;
1693 }
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001694 if (!rtc::FromString(found->second, value)) {
wu@webrtc.org5e760e72014-04-02 23:19:09 +00001695 return false;
1696 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001697 return true;
1698}
1699
1700void BuildRtpMap(const MediaContentDescription* media_desc,
1701 const MediaType media_type,
1702 std::string* message) {
nisseede5da42017-01-12 05:15:36 -08001703 RTC_DCHECK(message != NULL);
1704 RTC_DCHECK(media_desc != NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001705 std::ostringstream os;
1706 if (media_type == cricket::MEDIA_TYPE_VIDEO) {
1707 const VideoContentDescription* video_desc =
1708 static_cast<const VideoContentDescription*>(media_desc);
1709 for (std::vector<cricket::VideoCodec>::const_iterator it =
1710 video_desc->codecs().begin();
1711 it != video_desc->codecs().end(); ++it) {
1712 // RFC 4566
1713 // a=rtpmap:<payload type> <encoding name>/<clock rate>
1714 // [/<encodingparameters>]
1715 if (it->id != kWildcardPayloadType) {
1716 InitAttrLine(kAttributeRtpmap, &os);
deadbeefe814a0d2017-02-25 18:15:09 -08001717 os << kSdpDelimiterColon << it->id << " " << it->name << "/"
1718 << cricket::kVideoCodecClockrate;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001719 AddLine(os.str(), message);
1720 }
1721 AddRtcpFbLines(*it, message);
1722 AddFmtpLine(*it, message);
1723 }
1724 } else if (media_type == cricket::MEDIA_TYPE_AUDIO) {
1725 const AudioContentDescription* audio_desc =
1726 static_cast<const AudioContentDescription*>(media_desc);
1727 std::vector<int> ptimes;
1728 std::vector<int> maxptimes;
1729 int max_minptime = 0;
1730 for (std::vector<cricket::AudioCodec>::const_iterator it =
1731 audio_desc->codecs().begin();
1732 it != audio_desc->codecs().end(); ++it) {
nisseede5da42017-01-12 05:15:36 -08001733 RTC_DCHECK(!it->name.empty());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001734 // RFC 4566
1735 // a=rtpmap:<payload type> <encoding name>/<clock rate>
1736 // [/<encodingparameters>]
1737 InitAttrLine(kAttributeRtpmap, &os);
1738 os << kSdpDelimiterColon << it->id << " ";
1739 os << it->name << "/" << it->clockrate;
1740 if (it->channels != 1) {
1741 os << "/" << it->channels;
1742 }
1743 AddLine(os.str(), message);
1744 AddRtcpFbLines(*it, message);
1745 AddFmtpLine(*it, message);
1746 int minptime = 0;
1747 if (GetParameter(kCodecParamMinPTime, it->params, &minptime)) {
1748 max_minptime = std::max(minptime, max_minptime);
1749 }
1750 int ptime;
1751 if (GetParameter(kCodecParamPTime, it->params, &ptime)) {
1752 ptimes.push_back(ptime);
1753 }
1754 int maxptime;
1755 if (GetParameter(kCodecParamMaxPTime, it->params, &maxptime)) {
1756 maxptimes.push_back(maxptime);
1757 }
1758 }
1759 // Populate the maxptime attribute with the smallest maxptime of all codecs
1760 // under the same m-line.
1761 int min_maxptime = INT_MAX;
1762 if (GetMinValue(maxptimes, &min_maxptime)) {
1763 AddAttributeLine(kCodecParamMaxPTime, min_maxptime, message);
1764 }
nisseede5da42017-01-12 05:15:36 -08001765 RTC_DCHECK(min_maxptime > max_minptime);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001766 // Populate the ptime attribute with the smallest ptime or the largest
1767 // minptime, whichever is the largest, for all codecs under the same m-line.
1768 int ptime = INT_MAX;
1769 if (GetMinValue(ptimes, &ptime)) {
1770 ptime = std::min(ptime, min_maxptime);
1771 ptime = std::max(ptime, max_minptime);
1772 AddAttributeLine(kCodecParamPTime, ptime, message);
1773 }
1774 } else if (media_type == cricket::MEDIA_TYPE_DATA) {
1775 const DataContentDescription* data_desc =
1776 static_cast<const DataContentDescription*>(media_desc);
1777 for (std::vector<cricket::DataCodec>::const_iterator it =
1778 data_desc->codecs().begin();
1779 it != data_desc->codecs().end(); ++it) {
1780 // RFC 4566
1781 // a=rtpmap:<payload type> <encoding name>/<clock rate>
1782 // [/<encodingparameters>]
1783 InitAttrLine(kAttributeRtpmap, &os);
1784 os << kSdpDelimiterColon << it->id << " "
1785 << it->name << "/" << it->clockrate;
1786 AddLine(os.str(), message);
1787 }
1788 }
1789}
1790
1791void BuildCandidate(const std::vector<Candidate>& candidates,
honghaiza54a0802015-12-16 18:37:23 -08001792 bool include_ufrag,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001793 std::string* message) {
1794 std::ostringstream os;
1795
1796 for (std::vector<Candidate>::const_iterator it = candidates.begin();
1797 it != candidates.end(); ++it) {
1798 // RFC 5245
1799 // a=candidate:<foundation> <component-id> <transport> <priority>
1800 // <connection-address> <port> typ <candidate-types>
1801 // [raddr <connection-address>] [rport <port>]
1802 // *(SP extension-att-name SP extension-att-value)
1803 std::string type;
1804 // Map the cricket candidate type to "host" / "srflx" / "prflx" / "relay"
1805 if (it->type() == cricket::LOCAL_PORT_TYPE) {
1806 type = kCandidateHost;
1807 } else if (it->type() == cricket::STUN_PORT_TYPE) {
1808 type = kCandidateSrflx;
1809 } else if (it->type() == cricket::RELAY_PORT_TYPE) {
1810 type = kCandidateRelay;
Honghai Zhang7fb69db2016-03-14 11:59:18 -07001811 } else if (it->type() == cricket::PRFLX_PORT_TYPE) {
1812 type = kCandidatePrflx;
1813 // Peer reflexive candidate may be signaled for being removed.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001814 } else {
nissec80e7412017-01-11 05:56:46 -08001815 RTC_NOTREACHED();
Peter Thatcher019087f2015-04-28 09:06:26 -07001816 // Never write out candidates if we don't know the type.
1817 continue;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001818 }
1819
1820 InitAttrLine(kAttributeCandidate, &os);
1821 os << kSdpDelimiterColon
mallinath@webrtc.org2d60c5e2014-08-08 22:29:20 +00001822 << it->foundation() << " "
1823 << it->component() << " "
1824 << it->protocol() << " "
1825 << it->priority() << " "
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001826 << it->address().ipaddr().ToString() << " "
1827 << it->address().PortAsString() << " "
mallinath@webrtc.org2d60c5e2014-08-08 22:29:20 +00001828 << kAttributeCandidateTyp << " "
1829 << type << " ";
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001830
1831 // Related address
1832 if (!it->related_address().IsNil()) {
1833 os << kAttributeCandidateRaddr << " "
1834 << it->related_address().ipaddr().ToString() << " "
1835 << kAttributeCandidateRport << " "
1836 << it->related_address().PortAsString() << " ";
1837 }
1838
mallinath@webrtc.org2d60c5e2014-08-08 22:29:20 +00001839 if (it->protocol() == cricket::TCP_PROTOCOL_NAME) {
mallinath@webrtc.orge999bd02014-08-13 06:05:55 +00001840 os << kTcpCandidateType << " " << it->tcptype() << " ";
mallinath@webrtc.org2d60c5e2014-08-08 22:29:20 +00001841 }
1842
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001843 // Extensions
1844 os << kAttributeCandidateGeneration << " " << it->generation();
honghaiza54a0802015-12-16 18:37:23 -08001845 if (include_ufrag && !it->username().empty()) {
1846 os << " " << kAttributeCandidateUfrag << " " << it->username();
1847 }
honghaiza0c44ea2016-03-23 16:07:48 -07001848 if (it->network_id() > 0) {
1849 os << " " << kAttributeCandidateNetworkId << " " << it->network_id();
1850 }
honghaize1a0c942016-02-16 14:54:56 -08001851 if (it->network_cost() > 0) {
1852 os << " " << kAttributeCandidateNetworkCost << " " << it->network_cost();
1853 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001854
1855 AddLine(os.str(), message);
1856 }
1857}
1858
1859void BuildIceOptions(const std::vector<std::string>& transport_options,
1860 std::string* message) {
1861 if (!transport_options.empty()) {
1862 std::ostringstream os;
1863 InitAttrLine(kAttributeIceOption, &os);
1864 os << kSdpDelimiterColon << transport_options[0];
1865 for (size_t i = 1; i < transport_options.size(); ++i) {
1866 os << kSdpDelimiterSpace << transport_options[i];
1867 }
1868 AddLine(os.str(), message);
1869 }
1870}
1871
pthatcher@webrtc.org3341b402015-02-13 21:14:22 +00001872bool IsRtp(const std::string& protocol) {
1873 return protocol.empty() ||
1874 (protocol.find(cricket::kMediaProtocolRtpPrefix) != std::string::npos);
1875}
1876
1877bool IsDtlsSctp(const std::string& protocol) {
1878 // This intentionally excludes "SCTP" and "SCTP/DTLS".
lally@webrtc.orga7470932015-02-24 20:19:21 +00001879 return protocol.find(cricket::kMediaProtocolDtlsSctp) != std::string::npos;
pthatcher@webrtc.org3341b402015-02-13 21:14:22 +00001880}
1881
zhihuang38989e52017-03-21 11:04:53 -07001882bool ParseConnectionData(const std::string& line,
1883 rtc::SocketAddress* addr,
1884 SdpParseError* error) {
1885 // Parse the line from left to right.
1886 std::string token;
1887 std::string rightpart;
1888 // RFC 4566
1889 // c=<nettype> <addrtype> <connection-address>
1890 // Skip the "c="
1891 if (!rtc::tokenize_first(line, kSdpDelimiterEqual, &token, &rightpart)) {
1892 return ParseFailed(line, "Failed to parse the network type.", error);
1893 }
1894
1895 // Extract and verify the <nettype>
1896 if (!rtc::tokenize_first(rightpart, kSdpDelimiterSpace, &token, &rightpart) ||
1897 token != kConnectionNettype) {
1898 return ParseFailed(line,
1899 "Failed to parse the connection data. The network type "
1900 "is not currently supported.",
1901 error);
1902 }
1903
1904 // Extract the "<addrtype>" and "<connection-address>".
1905 if (!rtc::tokenize_first(rightpart, kSdpDelimiterSpace, &token, &rightpart)) {
1906 return ParseFailed(line, "Failed to parse the address type.", error);
1907 }
1908
1909 // The rightpart part should be the IP address without the slash which is used
1910 // for multicast.
1911 if (rightpart.find('/') != std::string::npos) {
1912 return ParseFailed(line,
1913 "Failed to parse the connection data. Multicast is not "
1914 "currently supported.",
1915 error);
1916 }
1917 addr->SetIP(rightpart);
1918
1919 // Verify that the addrtype matches the type of the parsed address.
1920 if ((addr->family() == AF_INET && token != "IP4") ||
1921 (addr->family() == AF_INET6 && token != "IP6")) {
1922 addr->Clear();
1923 return ParseFailed(
1924 line,
1925 "Failed to parse the connection data. The address type is mismatching.",
1926 error);
1927 }
1928 return true;
1929}
1930
1931bool ParseSessionDescription(const std::string& message,
1932 size_t* pos,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001933 std::string* session_id,
1934 std::string* session_version,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001935 TransportDescription* session_td,
1936 RtpHeaderExtensions* session_extmaps,
zhihuang38989e52017-03-21 11:04:53 -07001937 rtc::SocketAddress* connection_addr,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001938 cricket::SessionDescription* desc,
1939 SdpParseError* error) {
1940 std::string line;
1941
deadbeefc80741f2015-10-22 13:14:45 -07001942 desc->set_msid_supported(false);
1943
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001944 // RFC 4566
1945 // v= (protocol version)
1946 if (!GetLineWithType(message, pos, &line, kLineTypeVersion)) {
1947 return ParseFailedExpectLine(message, *pos, kLineTypeVersion,
1948 std::string(), error);
1949 }
1950 // RFC 4566
1951 // o=<username> <sess-id> <sess-version> <nettype> <addrtype>
1952 // <unicast-address>
1953 if (!GetLineWithType(message, pos, &line, kLineTypeOrigin)) {
1954 return ParseFailedExpectLine(message, *pos, kLineTypeOrigin,
1955 std::string(), error);
1956 }
1957 std::vector<std::string> fields;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001958 rtc::split(line.substr(kLinePrefixLength),
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001959 kSdpDelimiterSpace, &fields);
1960 const size_t expected_fields = 6;
1961 if (fields.size() != expected_fields) {
1962 return ParseFailedExpectFieldNum(line, expected_fields, error);
1963 }
1964 *session_id = fields[1];
1965 *session_version = fields[2];
1966
1967 // RFC 4566
1968 // s= (session name)
1969 if (!GetLineWithType(message, pos, &line, kLineTypeSessionName)) {
1970 return ParseFailedExpectLine(message, *pos, kLineTypeSessionName,
1971 std::string(), error);
1972 }
1973
1974 // Optional lines
1975 // Those are the optional lines, so shouldn't return false if not present.
1976 // RFC 4566
1977 // i=* (session information)
1978 GetLineWithType(message, pos, &line, kLineTypeSessionInfo);
1979
1980 // RFC 4566
1981 // u=* (URI of description)
1982 GetLineWithType(message, pos, &line, kLineTypeSessionUri);
1983
1984 // RFC 4566
1985 // e=* (email address)
1986 GetLineWithType(message, pos, &line, kLineTypeSessionEmail);
1987
1988 // RFC 4566
1989 // p=* (phone number)
1990 GetLineWithType(message, pos, &line, kLineTypeSessionPhone);
1991
1992 // RFC 4566
1993 // c=* (connection information -- not required if included in
1994 // all media)
zhihuang38989e52017-03-21 11:04:53 -07001995 if (GetLineWithType(message, pos, &line, kLineTypeConnection)) {
1996 if (!ParseConnectionData(line, connection_addr, error)) {
1997 return false;
1998 }
1999 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002000
2001 // RFC 4566
2002 // b=* (zero or more bandwidth information lines)
2003 while (GetLineWithType(message, pos, &line, kLineTypeSessionBandwidth)) {
2004 // By pass zero or more b lines.
2005 }
2006
2007 // RFC 4566
2008 // One or more time descriptions ("t=" and "r=" lines; see below)
2009 // t= (time the session is active)
2010 // r=* (zero or more repeat times)
2011 // Ensure there's at least one time description
2012 if (!GetLineWithType(message, pos, &line, kLineTypeTiming)) {
2013 return ParseFailedExpectLine(message, *pos, kLineTypeTiming, std::string(),
2014 error);
2015 }
2016
2017 while (GetLineWithType(message, pos, &line, kLineTypeRepeatTimes)) {
2018 // By pass zero or more r lines.
2019 }
2020
2021 // Go through the rest of the time descriptions
2022 while (GetLineWithType(message, pos, &line, kLineTypeTiming)) {
2023 while (GetLineWithType(message, pos, &line, kLineTypeRepeatTimes)) {
2024 // By pass zero or more r lines.
2025 }
2026 }
2027
2028 // RFC 4566
2029 // z=* (time zone adjustments)
2030 GetLineWithType(message, pos, &line, kLineTypeTimeZone);
2031
2032 // RFC 4566
2033 // k=* (encryption key)
2034 GetLineWithType(message, pos, &line, kLineTypeEncryptionKey);
2035
2036 // RFC 4566
2037 // a=* (zero or more session attribute lines)
2038 while (GetLineWithType(message, pos, &line, kLineTypeAttributes)) {
2039 if (HasAttribute(line, kAttributeGroup)) {
2040 if (!ParseGroupAttribute(line, desc, error)) {
2041 return false;
2042 }
2043 } else if (HasAttribute(line, kAttributeIceUfrag)) {
2044 if (!GetValue(line, kAttributeIceUfrag,
2045 &(session_td->ice_ufrag), error)) {
2046 return false;
2047 }
2048 } else if (HasAttribute(line, kAttributeIcePwd)) {
2049 if (!GetValue(line, kAttributeIcePwd, &(session_td->ice_pwd), error)) {
2050 return false;
2051 }
2052 } else if (HasAttribute(line, kAttributeIceLite)) {
2053 session_td->ice_mode = cricket::ICEMODE_LITE;
2054 } else if (HasAttribute(line, kAttributeIceOption)) {
2055 if (!ParseIceOptions(line, &(session_td->transport_options), error)) {
2056 return false;
2057 }
2058 } else if (HasAttribute(line, kAttributeFingerprint)) {
2059 if (session_td->identity_fingerprint.get()) {
2060 return ParseFailed(
2061 line,
2062 "Can't have multiple fingerprint attributes at the same level.",
2063 error);
2064 }
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002065 rtc::SSLFingerprint* fingerprint = NULL;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002066 if (!ParseFingerprintAttribute(line, &fingerprint, error)) {
2067 return false;
2068 }
2069 session_td->identity_fingerprint.reset(fingerprint);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00002070 } else if (HasAttribute(line, kAttributeSetup)) {
2071 if (!ParseDtlsSetup(line, &(session_td->connection_role), error)) {
2072 return false;
2073 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002074 } else if (HasAttribute(line, kAttributeMsidSemantics)) {
2075 std::string semantics;
2076 if (!GetValue(line, kAttributeMsidSemantics, &semantics, error)) {
2077 return false;
2078 }
deadbeefc80741f2015-10-22 13:14:45 -07002079 desc->set_msid_supported(
2080 CaseInsensitiveFind(semantics, kMediaStreamSemantic));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002081 } else if (HasAttribute(line, kAttributeExtmap)) {
isheriff6f8d6862016-05-26 11:24:55 -07002082 RtpExtension extmap;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002083 if (!ParseExtmap(line, &extmap, error)) {
2084 return false;
2085 }
2086 session_extmaps->push_back(extmap);
2087 }
2088 }
2089
2090 return true;
2091}
2092
2093bool ParseGroupAttribute(const std::string& line,
2094 cricket::SessionDescription* desc,
2095 SdpParseError* error) {
nisseede5da42017-01-12 05:15:36 -08002096 RTC_DCHECK(desc != NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002097
2098 // RFC 5888 and draft-holmberg-mmusic-sdp-bundle-negotiation-00
2099 // a=group:BUNDLE video voice
2100 std::vector<std::string> fields;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002101 rtc::split(line.substr(kLinePrefixLength),
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002102 kSdpDelimiterSpace, &fields);
2103 std::string semantics;
2104 if (!GetValue(fields[0], kAttributeGroup, &semantics, error)) {
2105 return false;
2106 }
2107 cricket::ContentGroup group(semantics);
2108 for (size_t i = 1; i < fields.size(); ++i) {
2109 group.AddContentName(fields[i]);
2110 }
2111 desc->AddGroup(group);
2112 return true;
2113}
2114
2115static bool ParseFingerprintAttribute(const std::string& line,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002116 rtc::SSLFingerprint** fingerprint,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002117 SdpParseError* error) {
2118 if (!IsLineType(line, kLineTypeAttributes) ||
2119 !HasAttribute(line, kAttributeFingerprint)) {
2120 return ParseFailedExpectLine(line, 0, kLineTypeAttributes,
2121 kAttributeFingerprint, error);
2122 }
2123
2124 std::vector<std::string> fields;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002125 rtc::split(line.substr(kLinePrefixLength),
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002126 kSdpDelimiterSpace, &fields);
2127 const size_t expected_fields = 2;
2128 if (fields.size() != expected_fields) {
2129 return ParseFailedExpectFieldNum(line, expected_fields, error);
2130 }
2131
2132 // The first field here is "fingerprint:<hash>.
2133 std::string algorithm;
2134 if (!GetValue(fields[0], kAttributeFingerprint, &algorithm, error)) {
2135 return false;
2136 }
2137
2138 // Downcase the algorithm. Note that we don't need to downcase the
2139 // fingerprint because hex_decode can handle upper-case.
2140 std::transform(algorithm.begin(), algorithm.end(), algorithm.begin(),
2141 ::tolower);
2142
2143 // The second field is the digest value. De-hexify it.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002144 *fingerprint = rtc::SSLFingerprint::CreateFromRfc4572(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002145 algorithm, fields[1]);
2146 if (!*fingerprint) {
2147 return ParseFailed(line,
2148 "Failed to create fingerprint from the digest.",
2149 error);
2150 }
2151
2152 return true;
2153}
2154
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00002155static bool ParseDtlsSetup(const std::string& line,
2156 cricket::ConnectionRole* role,
2157 SdpParseError* error) {
2158 // setup-attr = "a=setup:" role
2159 // role = "active" / "passive" / "actpass" / "holdconn"
2160 std::vector<std::string> fields;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002161 rtc::split(line.substr(kLinePrefixLength), kSdpDelimiterColon, &fields);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00002162 const size_t expected_fields = 2;
2163 if (fields.size() != expected_fields) {
2164 return ParseFailedExpectFieldNum(line, expected_fields, error);
2165 }
2166 std::string role_str = fields[1];
2167 if (!cricket::StringToConnectionRole(role_str, role)) {
2168 return ParseFailed(line, "Invalid attribute value.", error);
2169 }
2170 return true;
2171}
2172
deadbeef9d3584c2016-02-16 17:54:10 -08002173static bool ParseMsidAttribute(const std::string& line,
2174 std::string* stream_id,
2175 std::string* track_id,
2176 SdpParseError* error) {
2177 // draft-ietf-mmusic-msid-11
2178 // a=msid:<stream id> <track id>
2179 // msid-value = msid-id [ SP msid-appdata ]
2180 // msid-id = 1*64token-char ; see RFC 4566
2181 // msid-appdata = 1*64token-char ; see RFC 4566
2182 std::string field1;
2183 if (!rtc::tokenize_first(line.substr(kLinePrefixLength), kSdpDelimiterSpace,
2184 &field1, track_id)) {
2185 const size_t expected_fields = 2;
2186 return ParseFailedExpectFieldNum(line, expected_fields, error);
2187 }
2188
deadbeefa4549d62017-02-10 17:26:22 -08002189 if (track_id->empty()) {
2190 return ParseFailed(line, "Missing track ID in msid attribute.", error);
2191 }
2192
deadbeef9d3584c2016-02-16 17:54:10 -08002193 // msid:<msid-id>
2194 if (!GetValue(field1, kAttributeMsid, stream_id, error)) {
2195 return false;
2196 }
deadbeefa4549d62017-02-10 17:26:22 -08002197 if (stream_id->empty()) {
2198 return ParseFailed(line, "Missing stream ID in msid attribute.", error);
2199 }
deadbeef9d3584c2016-02-16 17:54:10 -08002200 return true;
2201}
2202
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002203// RFC 3551
2204// PT encoding media type clock rate channels
2205// name (Hz)
2206// 0 PCMU A 8,000 1
2207// 1 reserved A
2208// 2 reserved A
2209// 3 GSM A 8,000 1
2210// 4 G723 A 8,000 1
2211// 5 DVI4 A 8,000 1
2212// 6 DVI4 A 16,000 1
2213// 7 LPC A 8,000 1
2214// 8 PCMA A 8,000 1
2215// 9 G722 A 8,000 1
2216// 10 L16 A 44,100 2
2217// 11 L16 A 44,100 1
2218// 12 QCELP A 8,000 1
2219// 13 CN A 8,000 1
2220// 14 MPA A 90,000 (see text)
2221// 15 G728 A 8,000 1
2222// 16 DVI4 A 11,025 1
2223// 17 DVI4 A 22,050 1
2224// 18 G729 A 8,000 1
2225struct StaticPayloadAudioCodec {
2226 const char* name;
2227 int clockrate;
Peter Kasting69558702016-01-12 16:26:35 -08002228 size_t channels;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002229};
2230static const StaticPayloadAudioCodec kStaticPayloadAudioCodecs[] = {
2231 { "PCMU", 8000, 1 },
2232 { "reserved", 0, 0 },
2233 { "reserved", 0, 0 },
2234 { "GSM", 8000, 1 },
2235 { "G723", 8000, 1 },
2236 { "DVI4", 8000, 1 },
2237 { "DVI4", 16000, 1 },
2238 { "LPC", 8000, 1 },
2239 { "PCMA", 8000, 1 },
2240 { "G722", 8000, 1 },
2241 { "L16", 44100, 2 },
2242 { "L16", 44100, 1 },
2243 { "QCELP", 8000, 1 },
2244 { "CN", 8000, 1 },
2245 { "MPA", 90000, 1 },
2246 { "G728", 8000, 1 },
2247 { "DVI4", 11025, 1 },
2248 { "DVI4", 22050, 1 },
2249 { "G729", 8000, 1 },
2250};
2251
2252void MaybeCreateStaticPayloadAudioCodecs(
2253 const std::vector<int>& fmts, AudioContentDescription* media_desc) {
2254 if (!media_desc) {
2255 return;
2256 }
deadbeef67cf2c12016-04-13 10:07:16 -07002257 RTC_DCHECK(media_desc->codecs().empty());
solenberg9fa49752016-10-08 13:02:44 -07002258 for (int payload_type : fmts) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002259 if (!media_desc->HasCodec(payload_type) &&
2260 payload_type >= 0 &&
kjellander3e33bfe2016-06-20 07:04:09 -07002261 static_cast<uint32_t>(payload_type) <
2262 arraysize(kStaticPayloadAudioCodecs)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002263 std::string encoding_name = kStaticPayloadAudioCodecs[payload_type].name;
2264 int clock_rate = kStaticPayloadAudioCodecs[payload_type].clockrate;
Peter Kasting69558702016-01-12 16:26:35 -08002265 size_t channels = kStaticPayloadAudioCodecs[payload_type].channels;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002266 media_desc->AddCodec(cricket::AudioCodec(payload_type, encoding_name,
deadbeef67cf2c12016-04-13 10:07:16 -07002267 clock_rate, 0, channels));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002268 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002269 }
2270}
2271
2272template <class C>
2273static C* ParseContentDescription(const std::string& message,
2274 const MediaType media_type,
2275 int mline_index,
2276 const std::string& protocol,
deadbeef67cf2c12016-04-13 10:07:16 -07002277 const std::vector<int>& payload_types,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002278 size_t* pos,
2279 std::string* content_name,
deadbeef25ed4352016-12-12 18:37:36 -08002280 bool* bundle_only,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002281 TransportDescription* transport,
2282 std::vector<JsepIceCandidate*>* candidates,
2283 webrtc::SdpParseError* error) {
2284 C* media_desc = new C();
2285 switch (media_type) {
2286 case cricket::MEDIA_TYPE_AUDIO:
2287 *content_name = cricket::CN_AUDIO;
2288 break;
2289 case cricket::MEDIA_TYPE_VIDEO:
2290 *content_name = cricket::CN_VIDEO;
2291 break;
2292 case cricket::MEDIA_TYPE_DATA:
2293 *content_name = cricket::CN_DATA;
2294 break;
2295 default:
nissec80e7412017-01-11 05:56:46 -08002296 RTC_NOTREACHED();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002297 break;
2298 }
deadbeef67cf2c12016-04-13 10:07:16 -07002299 if (!ParseContent(message, media_type, mline_index, protocol, payload_types,
deadbeef25ed4352016-12-12 18:37:36 -08002300 pos, content_name, bundle_only, media_desc, transport,
2301 candidates, error)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002302 delete media_desc;
zhihuang38989e52017-03-21 11:04:53 -07002303 return nullptr;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002304 }
2305 // Sort the codecs according to the m-line fmt list.
deadbeef67cf2c12016-04-13 10:07:16 -07002306 std::unordered_map<int, int> payload_type_preferences;
2307 // "size + 1" so that the lowest preference payload type has a preference of
2308 // 1, which is greater than the default (0) for payload types not in the fmt
2309 // list.
2310 int preference = static_cast<int>(payload_types.size() + 1);
2311 for (int pt : payload_types) {
2312 payload_type_preferences[pt] = preference--;
2313 }
2314 std::vector<typename C::CodecType> codecs = media_desc->codecs();
2315 std::sort(codecs.begin(), codecs.end(), [&payload_type_preferences](
2316 const typename C::CodecType& a,
2317 const typename C::CodecType& b) {
2318 return payload_type_preferences[a.id] > payload_type_preferences[b.id];
2319 });
2320 media_desc->set_codecs(codecs);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002321 return media_desc;
2322}
2323
2324bool ParseMediaDescription(const std::string& message,
2325 const TransportDescription& session_td,
2326 const RtpHeaderExtensions& session_extmaps,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002327 size_t* pos,
zhihuang38989e52017-03-21 11:04:53 -07002328 const rtc::SocketAddress& session_connection_addr,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002329 cricket::SessionDescription* desc,
2330 std::vector<JsepIceCandidate*>* candidates,
2331 SdpParseError* error) {
nisseede5da42017-01-12 05:15:36 -08002332 RTC_DCHECK(desc != NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002333 std::string line;
2334 int mline_index = -1;
2335
2336 // Zero or more media descriptions
2337 // RFC 4566
2338 // m=<media> <port> <proto> <fmt>
2339 while (GetLineWithType(message, pos, &line, kLineTypeMedia)) {
2340 ++mline_index;
2341
2342 std::vector<std::string> fields;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002343 rtc::split(line.substr(kLinePrefixLength),
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002344 kSdpDelimiterSpace, &fields);
zstein4b2e0822017-02-17 19:48:38 -08002345
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002346 const size_t expected_min_fields = 4;
2347 if (fields.size() < expected_min_fields) {
2348 return ParseFailedExpectMinFieldNum(line, expected_min_fields, error);
2349 }
deadbeef25ed4352016-12-12 18:37:36 -08002350 bool port_rejected = false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002351 // RFC 3264
2352 // To reject an offered stream, the port number in the corresponding stream
2353 // in the answer MUST be set to zero.
2354 if (fields[1] == kMediaPortRejected) {
deadbeef25ed4352016-12-12 18:37:36 -08002355 port_rejected = true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002356 }
2357
zhihuang38989e52017-03-21 11:04:53 -07002358 int port = 0;
2359 if (!rtc::FromString<int>(fields[1], &port) || !IsValidPort(port)) {
2360 return ParseFailed(line, "The port number is invalid", error);
2361 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002362 std::string protocol = fields[2];
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002363
2364 // <fmt>
deadbeef67cf2c12016-04-13 10:07:16 -07002365 std::vector<int> payload_types;
pthatcher@webrtc.org3341b402015-02-13 21:14:22 +00002366 if (IsRtp(protocol)) {
jiayl@webrtc.orgddb85ab2014-09-05 16:31:56 +00002367 for (size_t j = 3 ; j < fields.size(); ++j) {
2368 // TODO(wu): Remove when below bug is fixed.
2369 // https://bugzilla.mozilla.org/show_bug.cgi?id=996329
pbosbb36fdf2015-07-09 07:48:14 -07002370 if (fields[j].empty() && j == fields.size() - 1) {
jiayl@webrtc.orgddb85ab2014-09-05 16:31:56 +00002371 continue;
2372 }
wu@webrtc.org36eda7c2014-04-15 20:37:30 +00002373
jiayl@webrtc.orgddb85ab2014-09-05 16:31:56 +00002374 int pl = 0;
pkasting@chromium.orge9facf82015-02-17 20:36:28 +00002375 if (!GetPayloadTypeFromString(line, fields[j], &pl, error)) {
jiayl@webrtc.orgddb85ab2014-09-05 16:31:56 +00002376 return false;
2377 }
deadbeef67cf2c12016-04-13 10:07:16 -07002378 payload_types.push_back(pl);
wu@webrtc.org5e760e72014-04-02 23:19:09 +00002379 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002380 }
2381
2382 // Make a temporary TransportDescription based on |session_td|.
2383 // Some of this gets overwritten by ParseContent.
deadbeef46eed762016-01-28 13:24:37 -08002384 TransportDescription transport(
2385 session_td.transport_options, session_td.ice_ufrag, session_td.ice_pwd,
2386 session_td.ice_mode, session_td.connection_role,
2387 session_td.identity_fingerprint.get());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002388
kwibergd1fe2812016-04-27 06:47:29 -07002389 std::unique_ptr<MediaContentDescription> content;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002390 std::string content_name;
deadbeef25ed4352016-12-12 18:37:36 -08002391 bool bundle_only = false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002392 if (HasAttribute(line, kMediaTypeVideo)) {
2393 content.reset(ParseContentDescription<VideoContentDescription>(
deadbeef67cf2c12016-04-13 10:07:16 -07002394 message, cricket::MEDIA_TYPE_VIDEO, mline_index, protocol,
deadbeef25ed4352016-12-12 18:37:36 -08002395 payload_types, pos, &content_name, &bundle_only, &transport,
2396 candidates, error));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002397 } else if (HasAttribute(line, kMediaTypeAudio)) {
2398 content.reset(ParseContentDescription<AudioContentDescription>(
deadbeef67cf2c12016-04-13 10:07:16 -07002399 message, cricket::MEDIA_TYPE_AUDIO, mline_index, protocol,
deadbeef25ed4352016-12-12 18:37:36 -08002400 payload_types, pos, &content_name, &bundle_only, &transport,
2401 candidates, error));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002402 } else if (HasAttribute(line, kMediaTypeData)) {
jiayl@webrtc.org0e527722014-09-08 21:43:43 +00002403 DataContentDescription* data_desc =
wu@webrtc.org78187522013-10-07 23:32:02 +00002404 ParseContentDescription<DataContentDescription>(
deadbeef67cf2c12016-04-13 10:07:16 -07002405 message, cricket::MEDIA_TYPE_DATA, mline_index, protocol,
deadbeef25ed4352016-12-12 18:37:36 -08002406 payload_types, pos, &content_name, &bundle_only, &transport,
2407 candidates, error);
jiayl@webrtc.org0e527722014-09-08 21:43:43 +00002408 content.reset(data_desc);
wu@webrtc.org78187522013-10-07 23:32:02 +00002409
zstein4b2e0822017-02-17 19:48:38 -08002410 if (data_desc && IsDtlsSctp(protocol)) {
2411 int p;
2412 if (rtc::FromString(fields[3], &p)) {
2413 if (!AddSctpDataCodec(data_desc, p)) {
2414 return false;
2415 }
2416 } else if (fields[3] == kDefaultSctpmapProtocol) {
2417 data_desc->set_use_sctpmap(false);
2418 }
wu@webrtc.org78187522013-10-07 23:32:02 +00002419 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002420 } else {
2421 LOG(LS_WARNING) << "Unsupported media type: " << line;
2422 continue;
2423 }
2424 if (!content.get()) {
2425 // ParseContentDescription returns NULL if failed.
2426 return false;
2427 }
2428
deadbeef25ed4352016-12-12 18:37:36 -08002429 bool content_rejected = false;
deadbeef12771a12017-01-03 13:53:47 -08002430 // A port of 0 is not interpreted as a rejected m= section when it's
2431 // used along with a=bundle-only.
deadbeef25ed4352016-12-12 18:37:36 -08002432 if (bundle_only) {
deadbeef25ed4352016-12-12 18:37:36 -08002433 if (!port_rejected) {
deadbeef12771a12017-01-03 13:53:47 -08002434 // Usage of bundle-only with a nonzero port is unspecified. So just
2435 // ignore bundle-only if we see this.
2436 bundle_only = false;
2437 LOG(LS_WARNING)
2438 << "a=bundle-only attribute observed with a nonzero "
2439 << "port; this usage is unspecified so the attribute is being "
2440 << "ignored.";
deadbeef25ed4352016-12-12 18:37:36 -08002441 }
2442 } else {
2443 // If not using bundle-only, interpret port 0 in the normal way; the m=
2444 // section is being rejected.
2445 content_rejected = port_rejected;
2446 }
2447
pthatcher@webrtc.org3341b402015-02-13 21:14:22 +00002448 if (IsRtp(protocol)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002449 // Set the extmap.
2450 if (!session_extmaps.empty() &&
2451 !content->rtp_header_extensions().empty()) {
2452 return ParseFailed("",
2453 "The a=extmap MUST be either all session level or "
2454 "all media level.",
2455 error);
2456 }
2457 for (size_t i = 0; i < session_extmaps.size(); ++i) {
2458 content->AddRtpHeaderExtension(session_extmaps[i]);
2459 }
2460 }
2461 content->set_protocol(protocol);
zhihuang38989e52017-03-21 11:04:53 -07002462
2463 // Use the session level connection address if the media level addresses are
2464 // not specified.
2465 rtc::SocketAddress address;
2466 address = content->connection_address().IsNil()
2467 ? session_connection_addr
2468 : content->connection_address();
2469 address.SetPort(port);
2470 content->set_connection_address(address);
2471
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002472 desc->AddContent(content_name,
deadbeef25ed4352016-12-12 18:37:36 -08002473 IsDtlsSctp(protocol) ? cricket::NS_JINGLE_DRAFT_SCTP
2474 : cricket::NS_JINGLE_RTP,
2475 content_rejected, bundle_only, content.release());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002476 // Create TransportInfo with the media level "ice-pwd" and "ice-ufrag".
2477 TransportInfo transport_info(content_name, transport);
2478
2479 if (!desc->AddTransportInfo(transport_info)) {
2480 std::ostringstream description;
2481 description << "Failed to AddTransportInfo with content name: "
2482 << content_name;
2483 return ParseFailed("", description.str(), error);
2484 }
2485 }
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00002486
2487 size_t end_of_message = message.size();
2488 if (mline_index == -1 && *pos != end_of_message) {
2489 ParseFailed(message, *pos, "Expects m line.", error);
2490 return false;
2491 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002492 return true;
2493}
2494
2495bool VerifyCodec(const cricket::Codec& codec) {
2496 // Codec has not been populated correctly unless the name has been set. This
2497 // can happen if an SDP has an fmtp or rtcp-fb with a payload type but doesn't
2498 // have a corresponding "rtpmap" line.
htab39db842016-12-08 01:50:48 -08002499 return !codec.name.empty();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002500}
2501
2502bool VerifyAudioCodecs(const AudioContentDescription* audio_desc) {
2503 const std::vector<cricket::AudioCodec>& codecs = audio_desc->codecs();
2504 for (std::vector<cricket::AudioCodec>::const_iterator iter = codecs.begin();
2505 iter != codecs.end(); ++iter) {
2506 if (!VerifyCodec(*iter)) {
2507 return false;
2508 }
2509 }
2510 return true;
2511}
2512
2513bool VerifyVideoCodecs(const VideoContentDescription* video_desc) {
2514 const std::vector<cricket::VideoCodec>& codecs = video_desc->codecs();
2515 for (std::vector<cricket::VideoCodec>::const_iterator iter = codecs.begin();
2516 iter != codecs.end(); ++iter) {
2517 if (!VerifyCodec(*iter)) {
2518 return false;
2519 }
2520 }
2521 return true;
2522}
2523
2524void AddParameters(const cricket::CodecParameterMap& parameters,
2525 cricket::Codec* codec) {
2526 for (cricket::CodecParameterMap::const_iterator iter =
2527 parameters.begin(); iter != parameters.end(); ++iter) {
2528 codec->SetParam(iter->first, iter->second);
2529 }
2530}
2531
2532void AddFeedbackParameter(const cricket::FeedbackParam& feedback_param,
2533 cricket::Codec* codec) {
2534 codec->AddFeedbackParam(feedback_param);
2535}
2536
2537void AddFeedbackParameters(const cricket::FeedbackParams& feedback_params,
2538 cricket::Codec* codec) {
2539 for (std::vector<cricket::FeedbackParam>::const_iterator iter =
2540 feedback_params.params().begin();
2541 iter != feedback_params.params().end(); ++iter) {
2542 codec->AddFeedbackParam(*iter);
2543 }
2544}
2545
2546// Gets the current codec setting associated with |payload_type|. If there
changbin.shao@webrtc.org2d25b442015-03-16 04:14:34 +00002547// is no Codec associated with that payload type it returns an empty codec
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002548// with that payload type.
2549template <class T>
changbin.shao@webrtc.org2d25b442015-03-16 04:14:34 +00002550T GetCodecWithPayloadType(const std::vector<T>& codecs, int payload_type) {
magjedb05fa242016-11-11 04:00:16 -08002551 const T* codec = FindCodecById(codecs, payload_type);
2552 if (codec)
2553 return *codec;
2554 // Return empty codec with |payload_type|.
changbin.shao@webrtc.org2d25b442015-03-16 04:14:34 +00002555 T ret_val;
magjedb05fa242016-11-11 04:00:16 -08002556 ret_val.id = payload_type;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002557 return ret_val;
2558}
2559
2560// Updates or creates a new codec entry in the audio description.
2561template <class T, class U>
2562void AddOrReplaceCodec(MediaContentDescription* content_desc, const U& codec) {
2563 T* desc = static_cast<T*>(content_desc);
2564 std::vector<U> codecs = desc->codecs();
2565 bool found = false;
2566
2567 typename std::vector<U>::iterator iter;
2568 for (iter = codecs.begin(); iter != codecs.end(); ++iter) {
2569 if (iter->id == codec.id) {
2570 *iter = codec;
2571 found = true;
2572 break;
2573 }
2574 }
2575 if (!found) {
2576 desc->AddCodec(codec);
2577 return;
2578 }
2579 desc->set_codecs(codecs);
2580}
2581
2582// Adds or updates existing codec corresponding to |payload_type| according
2583// to |parameters|.
2584template <class T, class U>
2585void UpdateCodec(MediaContentDescription* content_desc, int payload_type,
2586 const cricket::CodecParameterMap& parameters) {
2587 // Codec might already have been populated (from rtpmap).
changbin.shao@webrtc.org2d25b442015-03-16 04:14:34 +00002588 U new_codec = GetCodecWithPayloadType(static_cast<T*>(content_desc)->codecs(),
2589 payload_type);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002590 AddParameters(parameters, &new_codec);
2591 AddOrReplaceCodec<T, U>(content_desc, new_codec);
2592}
2593
2594// Adds or updates existing codec corresponding to |payload_type| according
2595// to |feedback_param|.
2596template <class T, class U>
2597void UpdateCodec(MediaContentDescription* content_desc, int payload_type,
2598 const cricket::FeedbackParam& feedback_param) {
2599 // Codec might already have been populated (from rtpmap).
changbin.shao@webrtc.org2d25b442015-03-16 04:14:34 +00002600 U new_codec = GetCodecWithPayloadType(static_cast<T*>(content_desc)->codecs(),
2601 payload_type);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002602 AddFeedbackParameter(feedback_param, &new_codec);
2603 AddOrReplaceCodec<T, U>(content_desc, new_codec);
2604}
2605
jlmiller@webrtc.orga744a282015-02-18 21:37:46 +00002606template <class T>
2607bool PopWildcardCodec(std::vector<T>* codecs, T* wildcard_codec) {
2608 for (auto iter = codecs->begin(); iter != codecs->end(); ++iter) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002609 if (iter->id == kWildcardPayloadType) {
2610 *wildcard_codec = *iter;
2611 codecs->erase(iter);
2612 return true;
2613 }
2614 }
2615 return false;
2616}
2617
jlmiller@webrtc.orga744a282015-02-18 21:37:46 +00002618template<class T>
2619void UpdateFromWildcardCodecs(cricket::MediaContentDescriptionImpl<T>* desc) {
2620 auto codecs = desc->codecs();
2621 T wildcard_codec;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002622 if (!PopWildcardCodec(&codecs, &wildcard_codec)) {
2623 return;
2624 }
jlmiller@webrtc.orga744a282015-02-18 21:37:46 +00002625 for (auto& codec : codecs) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002626 AddFeedbackParameters(wildcard_codec.feedback_params, &codec);
2627 }
jlmiller@webrtc.orga744a282015-02-18 21:37:46 +00002628 desc->set_codecs(codecs);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002629}
2630
2631void AddAudioAttribute(const std::string& name, const std::string& value,
2632 AudioContentDescription* audio_desc) {
2633 if (value.empty()) {
2634 return;
2635 }
2636 std::vector<cricket::AudioCodec> codecs = audio_desc->codecs();
2637 for (std::vector<cricket::AudioCodec>::iterator iter = codecs.begin();
2638 iter != codecs.end(); ++iter) {
2639 iter->params[name] = value;
2640 }
2641 audio_desc->set_codecs(codecs);
2642}
2643
2644bool ParseContent(const std::string& message,
2645 const MediaType media_type,
2646 int mline_index,
2647 const std::string& protocol,
deadbeef67cf2c12016-04-13 10:07:16 -07002648 const std::vector<int>& payload_types,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002649 size_t* pos,
2650 std::string* content_name,
deadbeef25ed4352016-12-12 18:37:36 -08002651 bool* bundle_only,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002652 MediaContentDescription* media_desc,
2653 TransportDescription* transport,
2654 std::vector<JsepIceCandidate*>* candidates,
2655 SdpParseError* error) {
nisseede5da42017-01-12 05:15:36 -08002656 RTC_DCHECK(media_desc != NULL);
2657 RTC_DCHECK(content_name != NULL);
2658 RTC_DCHECK(transport != NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002659
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +00002660 if (media_type == cricket::MEDIA_TYPE_AUDIO) {
2661 MaybeCreateStaticPayloadAudioCodecs(
deadbeef67cf2c12016-04-13 10:07:16 -07002662 payload_types, static_cast<AudioContentDescription*>(media_desc));
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +00002663 }
2664
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002665 // The media level "ice-ufrag" and "ice-pwd".
2666 // The candidates before update the media level "ice-pwd" and "ice-ufrag".
2667 Candidates candidates_orig;
2668 std::string line;
2669 std::string mline_id;
2670 // Tracks created out of the ssrc attributes.
2671 StreamParamsVec tracks;
2672 SsrcInfoVec ssrc_infos;
2673 SsrcGroupVec ssrc_groups;
2674 std::string maxptime_as_string;
2675 std::string ptime_as_string;
deadbeef9d3584c2016-02-16 17:54:10 -08002676 std::string stream_id;
2677 std::string track_id;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002678
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002679 // Loop until the next m line
2680 while (!IsLineType(message, kLineTypeMedia, *pos)) {
2681 if (!GetLine(message, pos, &line)) {
2682 if (*pos >= message.size()) {
2683 break; // Done parsing
2684 } else {
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +00002685 return ParseFailed(message, *pos, "Invalid SDP line.", error);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002686 }
2687 }
2688
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002689 // RFC 4566
2690 // b=* (zero or more bandwidth information lines)
2691 if (IsLineType(line, kLineTypeSessionBandwidth)) {
2692 std::string bandwidth;
2693 if (HasAttribute(line, kApplicationSpecificMaximum)) {
2694 if (!GetValue(line, kApplicationSpecificMaximum, &bandwidth, error)) {
2695 return false;
2696 } else {
wu@webrtc.org5e760e72014-04-02 23:19:09 +00002697 int b = 0;
2698 if (!GetValueFromString(line, bandwidth, &b, error)) {
2699 return false;
2700 }
Peter Thatcherc0c3a862015-06-24 15:31:25 -07002701 // We should never use more than the default bandwidth for RTP-based
2702 // data channels. Don't allow SDP to set the bandwidth, because
2703 // that would give JS the opportunity to "break the Internet".
2704 // See: https://code.google.com/p/chromium/issues/detail?id=280726
2705 if (media_type == cricket::MEDIA_TYPE_DATA && IsRtp(protocol) &&
2706 b > cricket::kDataMaxBandwidth / 1000) {
2707 std::ostringstream description;
2708 description << "RTP-based data channels may not send more than "
2709 << cricket::kDataMaxBandwidth / 1000 << "kbps.";
2710 return ParseFailed(line, description.str(), error);
2711 }
deadbeefb2362572016-12-13 16:37:06 -08002712 // Prevent integer overflow.
2713 b = std::min(b, INT_MAX / 1000);
wu@webrtc.org5e760e72014-04-02 23:19:09 +00002714 media_desc->set_bandwidth(b * 1000);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002715 }
2716 }
2717 continue;
2718 }
2719
zhihuang38989e52017-03-21 11:04:53 -07002720 // Parse the media level connection data.
2721 if (IsLineType(line, kLineTypeConnection)) {
2722 rtc::SocketAddress addr;
2723 if (!ParseConnectionData(line, &addr, error)) {
2724 return false;
2725 }
2726 media_desc->set_connection_address(addr);
2727 continue;
2728 }
2729
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002730 if (!IsLineType(line, kLineTypeAttributes)) {
2731 // TODO: Handle other lines if needed.
2732 LOG(LS_INFO) << "Ignored line: " << line;
2733 continue;
2734 }
2735
2736 // Handle attributes common to SCTP and RTP.
2737 if (HasAttribute(line, kAttributeMid)) {
2738 // RFC 3388
2739 // mid-attribute = "a=mid:" identification-tag
2740 // identification-tag = token
2741 // Use the mid identification-tag as the content name.
2742 if (!GetValue(line, kAttributeMid, &mline_id, error)) {
2743 return false;
2744 }
2745 *content_name = mline_id;
deadbeef25ed4352016-12-12 18:37:36 -08002746 } else if (HasAttribute(line, kAttributeBundleOnly)) {
2747 *bundle_only = true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002748 } else if (HasAttribute(line, kAttributeCandidate)) {
2749 Candidate candidate;
2750 if (!ParseCandidate(line, &candidate, error, false)) {
2751 return false;
2752 }
deadbeef7bcdb692017-01-20 12:43:58 -08002753 // ParseCandidate will parse non-standard ufrag and password attributes,
2754 // since it's used for candidate trickling, but we only want to process
2755 // the "a=ice-ufrag"/"a=ice-pwd" values in a session description, so
2756 // strip them off at this point.
2757 candidate.set_username(std::string());
2758 candidate.set_password(std::string());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002759 candidates_orig.push_back(candidate);
2760 } else if (HasAttribute(line, kAttributeIceUfrag)) {
2761 if (!GetValue(line, kAttributeIceUfrag, &transport->ice_ufrag, error)) {
2762 return false;
2763 }
2764 } else if (HasAttribute(line, kAttributeIcePwd)) {
2765 if (!GetValue(line, kAttributeIcePwd, &transport->ice_pwd, error)) {
2766 return false;
2767 }
2768 } else if (HasAttribute(line, kAttributeIceOption)) {
2769 if (!ParseIceOptions(line, &transport->transport_options, error)) {
2770 return false;
2771 }
2772 } else if (HasAttribute(line, kAttributeFmtp)) {
2773 if (!ParseFmtpAttributes(line, media_type, media_desc, error)) {
2774 return false;
2775 }
2776 } else if (HasAttribute(line, kAttributeFingerprint)) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002777 rtc::SSLFingerprint* fingerprint = NULL;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002778
2779 if (!ParseFingerprintAttribute(line, &fingerprint, error)) {
2780 return false;
2781 }
2782 transport->identity_fingerprint.reset(fingerprint);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00002783 } else if (HasAttribute(line, kAttributeSetup)) {
2784 if (!ParseDtlsSetup(line, &(transport->connection_role), error)) {
2785 return false;
2786 }
pthatcher@webrtc.org3341b402015-02-13 21:14:22 +00002787 } else if (IsDtlsSctp(protocol) && HasAttribute(line, kAttributeSctpPort)) {
deadbeef7e146cb2016-09-28 10:04:34 -07002788 if (media_type != cricket::MEDIA_TYPE_DATA) {
2789 return ParseFailed(
2790 line, "sctp-port attribute found in non-data media description.",
2791 error);
2792 }
jiayl@webrtc.orgddb85ab2014-09-05 16:31:56 +00002793 int sctp_port;
2794 if (!ParseSctpPort(line, &sctp_port, error)) {
2795 return false;
2796 }
2797 if (!AddSctpDataCodec(static_cast<DataContentDescription*>(media_desc),
2798 sctp_port)) {
2799 return false;
2800 }
pthatcher@webrtc.org3341b402015-02-13 21:14:22 +00002801 } else if (IsRtp(protocol)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002802 //
2803 // RTP specific attrubtes
2804 //
2805 if (HasAttribute(line, kAttributeRtcpMux)) {
2806 media_desc->set_rtcp_mux(true);
deadbeef13871492015-12-09 12:37:51 -08002807 } else if (HasAttribute(line, kAttributeRtcpReducedSize)) {
2808 media_desc->set_rtcp_reduced_size(true);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002809 } else if (HasAttribute(line, kAttributeSsrcGroup)) {
2810 if (!ParseSsrcGroupAttribute(line, &ssrc_groups, error)) {
2811 return false;
2812 }
2813 } else if (HasAttribute(line, kAttributeSsrc)) {
2814 if (!ParseSsrcAttribute(line, &ssrc_infos, error)) {
2815 return false;
2816 }
2817 } else if (HasAttribute(line, kAttributeCrypto)) {
2818 if (!ParseCryptoAttribute(line, media_desc, error)) {
2819 return false;
2820 }
2821 } else if (HasAttribute(line, kAttributeRtpmap)) {
deadbeef67cf2c12016-04-13 10:07:16 -07002822 if (!ParseRtpmapAttribute(line, media_type, payload_types, media_desc,
2823 error)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002824 return false;
2825 }
2826 } else if (HasAttribute(line, kCodecParamMaxPTime)) {
2827 if (!GetValue(line, kCodecParamMaxPTime, &maxptime_as_string, error)) {
2828 return false;
2829 }
2830 } else if (HasAttribute(line, kAttributeRtcpFb)) {
2831 if (!ParseRtcpFbAttribute(line, media_type, media_desc, error)) {
2832 return false;
2833 }
2834 } else if (HasAttribute(line, kCodecParamPTime)) {
2835 if (!GetValue(line, kCodecParamPTime, &ptime_as_string, error)) {
2836 return false;
2837 }
2838 } else if (HasAttribute(line, kAttributeSendOnly)) {
2839 media_desc->set_direction(cricket::MD_SENDONLY);
2840 } else if (HasAttribute(line, kAttributeRecvOnly)) {
2841 media_desc->set_direction(cricket::MD_RECVONLY);
2842 } else if (HasAttribute(line, kAttributeInactive)) {
2843 media_desc->set_direction(cricket::MD_INACTIVE);
2844 } else if (HasAttribute(line, kAttributeSendRecv)) {
2845 media_desc->set_direction(cricket::MD_SENDRECV);
2846 } else if (HasAttribute(line, kAttributeExtmap)) {
isheriff6f8d6862016-05-26 11:24:55 -07002847 RtpExtension extmap;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002848 if (!ParseExtmap(line, &extmap, error)) {
2849 return false;
2850 }
2851 media_desc->AddRtpHeaderExtension(extmap);
2852 } else if (HasAttribute(line, kAttributeXGoogleFlag)) {
2853 // Experimental attribute. Conference mode activates more aggressive
2854 // AEC and NS settings.
2855 // TODO: expose API to set these directly.
2856 std::string flag_value;
2857 if (!GetValue(line, kAttributeXGoogleFlag, &flag_value, error)) {
2858 return false;
2859 }
2860 if (flag_value.compare(kValueConference) == 0)
2861 media_desc->set_conference_mode(true);
deadbeef9d3584c2016-02-16 17:54:10 -08002862 } else if (HasAttribute(line, kAttributeMsid)) {
2863 if (!ParseMsidAttribute(line, &stream_id, &track_id, error)) {
2864 return false;
2865 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002866 }
2867 } else {
2868 // Only parse lines that we are interested of.
2869 LOG(LS_INFO) << "Ignored line: " << line;
2870 continue;
2871 }
2872 }
2873
2874 // Create tracks from the |ssrc_infos|.
Taylor Brandstetter5de6b752016-03-09 17:02:30 -08002875 // If the stream_id/track_id for all SSRCS are identical, one StreamParams
2876 // will be created in CreateTracksFromSsrcInfos, containing all the SSRCs from
2877 // the m= section.
2878 CreateTracksFromSsrcInfos(ssrc_infos, stream_id, track_id, &tracks);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002879
2880 // Add the ssrc group to the track.
2881 for (SsrcGroupVec::iterator ssrc_group = ssrc_groups.begin();
2882 ssrc_group != ssrc_groups.end(); ++ssrc_group) {
2883 if (ssrc_group->ssrcs.empty()) {
2884 continue;
2885 }
Peter Boström0c4e06b2015-10-07 12:23:21 +02002886 uint32_t ssrc = ssrc_group->ssrcs.front();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002887 for (StreamParamsVec::iterator track = tracks.begin();
2888 track != tracks.end(); ++track) {
2889 if (track->has_ssrc(ssrc)) {
2890 track->ssrc_groups.push_back(*ssrc_group);
2891 }
2892 }
2893 }
2894
2895 // Add the new tracks to the |media_desc|.
deadbeef9d3584c2016-02-16 17:54:10 -08002896 for (StreamParams& track : tracks) {
2897 media_desc->AddStream(track);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002898 }
2899
2900 if (media_type == cricket::MEDIA_TYPE_AUDIO) {
2901 AudioContentDescription* audio_desc =
2902 static_cast<AudioContentDescription*>(media_desc);
jlmiller@webrtc.orga744a282015-02-18 21:37:46 +00002903 UpdateFromWildcardCodecs(audio_desc);
2904
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002905 // Verify audio codec ensures that no audio codec has been populated with
2906 // only fmtp.
2907 if (!VerifyAudioCodecs(audio_desc)) {
2908 return ParseFailed("Failed to parse audio codecs correctly.", error);
2909 }
2910 AddAudioAttribute(kCodecParamMaxPTime, maxptime_as_string, audio_desc);
2911 AddAudioAttribute(kCodecParamPTime, ptime_as_string, audio_desc);
2912 }
2913
2914 if (media_type == cricket::MEDIA_TYPE_VIDEO) {
jlmiller@webrtc.orga744a282015-02-18 21:37:46 +00002915 VideoContentDescription* video_desc =
2916 static_cast<VideoContentDescription*>(media_desc);
2917 UpdateFromWildcardCodecs(video_desc);
2918 // Verify video codec ensures that no video codec has been populated with
2919 // only rtcp-fb.
2920 if (!VerifyVideoCodecs(video_desc)) {
2921 return ParseFailed("Failed to parse video codecs correctly.", error);
2922 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002923 }
2924
2925 // RFC 5245
2926 // Update the candidates with the media level "ice-pwd" and "ice-ufrag".
2927 for (Candidates::iterator it = candidates_orig.begin();
2928 it != candidates_orig.end(); ++it) {
nisseede5da42017-01-12 05:15:36 -08002929 RTC_DCHECK((*it).username().empty() ||
2930 (*it).username() == transport->ice_ufrag);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002931 (*it).set_username(transport->ice_ufrag);
nisseede5da42017-01-12 05:15:36 -08002932 RTC_DCHECK((*it).password().empty());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002933 (*it).set_password(transport->ice_pwd);
2934 candidates->push_back(
2935 new JsepIceCandidate(mline_id, mline_index, *it));
2936 }
2937 return true;
2938}
2939
2940bool ParseSsrcAttribute(const std::string& line, SsrcInfoVec* ssrc_infos,
2941 SdpParseError* error) {
nisseede5da42017-01-12 05:15:36 -08002942 RTC_DCHECK(ssrc_infos != NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002943 // RFC 5576
2944 // a=ssrc:<ssrc-id> <attribute>
2945 // a=ssrc:<ssrc-id> <attribute>:<value>
2946 std::string field1, field2;
Donald Curtis144d0182015-05-15 13:14:24 -07002947 if (!rtc::tokenize_first(line.substr(kLinePrefixLength), kSdpDelimiterSpace,
2948 &field1, &field2)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002949 const size_t expected_fields = 2;
2950 return ParseFailedExpectFieldNum(line, expected_fields, error);
2951 }
2952
2953 // ssrc:<ssrc-id>
2954 std::string ssrc_id_s;
2955 if (!GetValue(field1, kAttributeSsrc, &ssrc_id_s, error)) {
2956 return false;
2957 }
Peter Boström0c4e06b2015-10-07 12:23:21 +02002958 uint32_t ssrc_id = 0;
wu@webrtc.org5e760e72014-04-02 23:19:09 +00002959 if (!GetValueFromString(line, ssrc_id_s, &ssrc_id, error)) {
2960 return false;
2961 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002962
2963 std::string attribute;
2964 std::string value;
Donald Curtis144d0182015-05-15 13:14:24 -07002965 if (!rtc::tokenize_first(field2, kSdpDelimiterColon, &attribute, &value)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002966 std::ostringstream description;
2967 description << "Failed to get the ssrc attribute value from " << field2
2968 << ". Expected format <attribute>:<value>.";
2969 return ParseFailed(line, description.str(), error);
2970 }
2971
2972 // Check if there's already an item for this |ssrc_id|. Create a new one if
2973 // there isn't.
2974 SsrcInfoVec::iterator ssrc_info = ssrc_infos->begin();
2975 for (; ssrc_info != ssrc_infos->end(); ++ssrc_info) {
2976 if (ssrc_info->ssrc_id == ssrc_id) {
2977 break;
2978 }
2979 }
2980 if (ssrc_info == ssrc_infos->end()) {
2981 SsrcInfo info;
2982 info.ssrc_id = ssrc_id;
2983 ssrc_infos->push_back(info);
2984 ssrc_info = ssrc_infos->end() - 1;
2985 }
2986
2987 // Store the info to the |ssrc_info|.
2988 if (attribute == kSsrcAttributeCname) {
2989 // RFC 5576
2990 // cname:<value>
2991 ssrc_info->cname = value;
2992 } else if (attribute == kSsrcAttributeMsid) {
2993 // draft-alvestrand-mmusic-msid-00
2994 // "msid:" identifier [ " " appdata ]
2995 std::vector<std::string> fields;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002996 rtc::split(value, kSdpDelimiterSpace, &fields);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002997 if (fields.size() < 1 || fields.size() > 2) {
2998 return ParseFailed(line,
2999 "Expected format \"msid:<identifier>[ <appdata>]\".",
3000 error);
3001 }
deadbeef9d3584c2016-02-16 17:54:10 -08003002 ssrc_info->stream_id = fields[0];
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003003 if (fields.size() == 2) {
deadbeef9d3584c2016-02-16 17:54:10 -08003004 ssrc_info->track_id = fields[1];
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003005 }
3006 } else if (attribute == kSsrcAttributeMslabel) {
3007 // draft-alvestrand-rtcweb-mid-01
3008 // mslabel:<value>
3009 ssrc_info->mslabel = value;
3010 } else if (attribute == kSSrcAttributeLabel) {
3011 // The label isn't defined.
3012 // label:<value>
3013 ssrc_info->label = value;
3014 }
3015 return true;
3016}
3017
3018bool ParseSsrcGroupAttribute(const std::string& line,
3019 SsrcGroupVec* ssrc_groups,
3020 SdpParseError* error) {
nisseede5da42017-01-12 05:15:36 -08003021 RTC_DCHECK(ssrc_groups != NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003022 // RFC 5576
3023 // a=ssrc-group:<semantics> <ssrc-id> ...
3024 std::vector<std::string> fields;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00003025 rtc::split(line.substr(kLinePrefixLength),
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003026 kSdpDelimiterSpace, &fields);
3027 const size_t expected_min_fields = 2;
3028 if (fields.size() < expected_min_fields) {
3029 return ParseFailedExpectMinFieldNum(line, expected_min_fields, error);
3030 }
3031 std::string semantics;
3032 if (!GetValue(fields[0], kAttributeSsrcGroup, &semantics, error)) {
3033 return false;
3034 }
Peter Boström0c4e06b2015-10-07 12:23:21 +02003035 std::vector<uint32_t> ssrcs;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003036 for (size_t i = 1; i < fields.size(); ++i) {
Peter Boström0c4e06b2015-10-07 12:23:21 +02003037 uint32_t ssrc = 0;
wu@webrtc.org5e760e72014-04-02 23:19:09 +00003038 if (!GetValueFromString(line, fields[i], &ssrc, error)) {
3039 return false;
3040 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003041 ssrcs.push_back(ssrc);
3042 }
3043 ssrc_groups->push_back(SsrcGroup(semantics, ssrcs));
3044 return true;
3045}
3046
3047bool ParseCryptoAttribute(const std::string& line,
3048 MediaContentDescription* media_desc,
3049 SdpParseError* error) {
3050 std::vector<std::string> fields;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00003051 rtc::split(line.substr(kLinePrefixLength),
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003052 kSdpDelimiterSpace, &fields);
3053 // RFC 4568
3054 // a=crypto:<tag> <crypto-suite> <key-params> [<session-params>]
3055 const size_t expected_min_fields = 3;
3056 if (fields.size() < expected_min_fields) {
3057 return ParseFailedExpectMinFieldNum(line, expected_min_fields, error);
3058 }
3059 std::string tag_value;
3060 if (!GetValue(fields[0], kAttributeCrypto, &tag_value, error)) {
3061 return false;
3062 }
wu@webrtc.org5e760e72014-04-02 23:19:09 +00003063 int tag = 0;
3064 if (!GetValueFromString(line, tag_value, &tag, error)) {
3065 return false;
3066 }
jbauch083b73f2015-07-16 02:46:32 -07003067 const std::string& crypto_suite = fields[1];
3068 const std::string& key_params = fields[2];
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003069 std::string session_params;
3070 if (fields.size() > 3) {
3071 session_params = fields[3];
3072 }
3073 media_desc->AddCrypto(CryptoParams(tag, crypto_suite, key_params,
3074 session_params));
3075 return true;
3076}
3077
3078// Updates or creates a new codec entry in the audio description with according
deadbeef67cf2c12016-04-13 10:07:16 -07003079// to |name|, |clockrate|, |bitrate|, and |channels|.
3080void UpdateCodec(int payload_type,
3081 const std::string& name,
3082 int clockrate,
3083 int bitrate,
3084 size_t channels,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003085 AudioContentDescription* audio_desc) {
3086 // Codec may already be populated with (only) optional parameters
3087 // (from an fmtp).
changbin.shao@webrtc.org2d25b442015-03-16 04:14:34 +00003088 cricket::AudioCodec codec =
3089 GetCodecWithPayloadType(audio_desc->codecs(), payload_type);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003090 codec.name = name;
3091 codec.clockrate = clockrate;
3092 codec.bitrate = bitrate;
3093 codec.channels = channels;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003094 AddOrReplaceCodec<AudioContentDescription, cricket::AudioCodec>(audio_desc,
3095 codec);
3096}
3097
3098// Updates or creates a new codec entry in the video description according to
deadbeef67cf2c12016-04-13 10:07:16 -07003099// |name|, |width|, |height|, and |framerate|.
3100void UpdateCodec(int payload_type,
3101 const std::string& name,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003102 VideoContentDescription* video_desc) {
3103 // Codec may already be populated with (only) optional parameters
3104 // (from an fmtp).
changbin.shao@webrtc.org2d25b442015-03-16 04:14:34 +00003105 cricket::VideoCodec codec =
3106 GetCodecWithPayloadType(video_desc->codecs(), payload_type);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003107 codec.name = name;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003108 AddOrReplaceCodec<VideoContentDescription, cricket::VideoCodec>(video_desc,
3109 codec);
3110}
3111
3112bool ParseRtpmapAttribute(const std::string& line,
3113 const MediaType media_type,
deadbeef67cf2c12016-04-13 10:07:16 -07003114 const std::vector<int>& payload_types,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003115 MediaContentDescription* media_desc,
3116 SdpParseError* error) {
3117 std::vector<std::string> fields;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00003118 rtc::split(line.substr(kLinePrefixLength),
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003119 kSdpDelimiterSpace, &fields);
3120 // RFC 4566
3121 // a=rtpmap:<payload type> <encoding name>/<clock rate>[/<encodingparameters>]
3122 const size_t expected_min_fields = 2;
3123 if (fields.size() < expected_min_fields) {
3124 return ParseFailedExpectMinFieldNum(line, expected_min_fields, error);
3125 }
3126 std::string payload_type_value;
3127 if (!GetValue(fields[0], kAttributeRtpmap, &payload_type_value, error)) {
3128 return false;
3129 }
wu@webrtc.org5e760e72014-04-02 23:19:09 +00003130 int payload_type = 0;
pkasting@chromium.orge9facf82015-02-17 20:36:28 +00003131 if (!GetPayloadTypeFromString(line, payload_type_value, &payload_type,
3132 error)) {
wu@webrtc.org5e760e72014-04-02 23:19:09 +00003133 return false;
3134 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003135
deadbeef67cf2c12016-04-13 10:07:16 -07003136 if (std::find(payload_types.begin(), payload_types.end(), payload_type) ==
3137 payload_types.end()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003138 LOG(LS_WARNING) << "Ignore rtpmap line that did not appear in the "
3139 << "<fmt> of the m-line: " << line;
3140 return true;
3141 }
jbauch083b73f2015-07-16 02:46:32 -07003142 const std::string& encoder = fields[1];
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003143 std::vector<std::string> codec_params;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00003144 rtc::split(encoder, '/', &codec_params);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003145 // <encoding name>/<clock rate>[/<encodingparameters>]
3146 // 2 mandatory fields
3147 if (codec_params.size() < 2 || codec_params.size() > 3) {
3148 return ParseFailed(line,
3149 "Expected format \"<encoding name>/<clock rate>"
3150 "[/<encodingparameters>]\".",
3151 error);
3152 }
jbauch083b73f2015-07-16 02:46:32 -07003153 const std::string& encoding_name = codec_params[0];
wu@webrtc.org5e760e72014-04-02 23:19:09 +00003154 int clock_rate = 0;
3155 if (!GetValueFromString(line, codec_params[1], &clock_rate, error)) {
3156 return false;
3157 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003158 if (media_type == cricket::MEDIA_TYPE_VIDEO) {
3159 VideoContentDescription* video_desc =
3160 static_cast<VideoContentDescription*>(media_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003161 UpdateCodec(payload_type, encoding_name,
deadbeef67cf2c12016-04-13 10:07:16 -07003162 video_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003163 } else if (media_type == cricket::MEDIA_TYPE_AUDIO) {
3164 // RFC 4566
3165 // For audio streams, <encoding parameters> indicates the number
3166 // of audio channels. This parameter is OPTIONAL and may be
3167 // omitted if the number of channels is one, provided that no
3168 // additional parameters are needed.
Peter Kasting69558702016-01-12 16:26:35 -08003169 size_t channels = 1;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003170 if (codec_params.size() == 3) {
wu@webrtc.org5e760e72014-04-02 23:19:09 +00003171 if (!GetValueFromString(line, codec_params[2], &channels, error)) {
3172 return false;
3173 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003174 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003175 AudioContentDescription* audio_desc =
3176 static_cast<AudioContentDescription*>(media_desc);
ossue1405ad2017-01-23 08:55:48 -08003177 UpdateCodec(payload_type, encoding_name, clock_rate, 0, channels,
deadbeef67cf2c12016-04-13 10:07:16 -07003178 audio_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003179 } else if (media_type == cricket::MEDIA_TYPE_DATA) {
3180 DataContentDescription* data_desc =
3181 static_cast<DataContentDescription*>(media_desc);
deadbeef67cf2c12016-04-13 10:07:16 -07003182 data_desc->AddCodec(cricket::DataCodec(payload_type, encoding_name));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003183 }
3184 return true;
3185}
3186
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003187bool ParseFmtpParam(const std::string& line, std::string* parameter,
3188 std::string* value, SdpParseError* error) {
Donald Curtis0e07f922015-05-15 09:21:23 -07003189 if (!rtc::tokenize_first(line, kSdpDelimiterEqual, parameter, value)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003190 ParseFailed(line, "Unable to parse fmtp parameter. \'=\' missing.", error);
3191 return false;
3192 }
3193 // a=fmtp:<payload_type> <param1>=<value1>; <param2>=<value2>; ...
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003194 return true;
3195}
3196
3197bool ParseFmtpAttributes(const std::string& line, const MediaType media_type,
3198 MediaContentDescription* media_desc,
3199 SdpParseError* error) {
3200 if (media_type != cricket::MEDIA_TYPE_AUDIO &&
3201 media_type != cricket::MEDIA_TYPE_VIDEO) {
3202 return true;
3203 }
Donald Curtis0e07f922015-05-15 09:21:23 -07003204
3205 std::string line_payload;
3206 std::string line_params;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003207
3208 // RFC 5576
3209 // a=fmtp:<format> <format specific parameters>
3210 // At least two fields, whereas the second one is any of the optional
3211 // parameters.
Donald Curtis144d0182015-05-15 13:14:24 -07003212 if (!rtc::tokenize_first(line.substr(kLinePrefixLength), kSdpDelimiterSpace,
3213 &line_payload, &line_params)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003214 ParseFailedExpectMinFieldNum(line, 2, error);
3215 return false;
3216 }
3217
Donald Curtis0e07f922015-05-15 09:21:23 -07003218 // Parse out the payload information.
pkasting@chromium.orgd3245462015-02-23 21:28:22 +00003219 std::string payload_type_str;
Donald Curtis0e07f922015-05-15 09:21:23 -07003220 if (!GetValue(line_payload, kAttributeFmtp, &payload_type_str, error)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003221 return false;
3222 }
3223
Donald Curtis0e07f922015-05-15 09:21:23 -07003224 int payload_type = 0;
Donald Curtis144d0182015-05-15 13:14:24 -07003225 if (!GetPayloadTypeFromString(line_payload, payload_type_str, &payload_type,
3226 error)) {
Donald Curtis0e07f922015-05-15 09:21:23 -07003227 return false;
3228 }
3229
3230 // Parse out format specific parameters.
3231 std::vector<std::string> fields;
3232 rtc::split(line_params, kSdpDelimiterSemicolon, &fields);
3233
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003234 cricket::CodecParameterMap codec_params;
Donald Curtis144d0182015-05-15 13:14:24 -07003235 for (auto& iter : fields) {
Donald Curtis0e07f922015-05-15 09:21:23 -07003236 if (iter.find(kSdpDelimiterEqual) == std::string::npos) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003237 // Only fmtps with equals are currently supported. Other fmtp types
3238 // should be ignored. Unknown fmtps do not constitute an error.
3239 continue;
3240 }
Donald Curtis0e07f922015-05-15 09:21:23 -07003241
3242 std::string name;
3243 std::string value;
3244 if (!ParseFmtpParam(rtc::string_trim(iter), &name, &value, error)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003245 return false;
3246 }
3247 codec_params[name] = value;
3248 }
3249
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003250 if (media_type == cricket::MEDIA_TYPE_AUDIO) {
3251 UpdateCodec<AudioContentDescription, cricket::AudioCodec>(
pkasting@chromium.orgd3245462015-02-23 21:28:22 +00003252 media_desc, payload_type, codec_params);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003253 } else if (media_type == cricket::MEDIA_TYPE_VIDEO) {
3254 UpdateCodec<VideoContentDescription, cricket::VideoCodec>(
pkasting@chromium.orgd3245462015-02-23 21:28:22 +00003255 media_desc, payload_type, codec_params);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003256 }
3257 return true;
3258}
3259
3260bool ParseRtcpFbAttribute(const std::string& line, const MediaType media_type,
3261 MediaContentDescription* media_desc,
3262 SdpParseError* error) {
3263 if (media_type != cricket::MEDIA_TYPE_AUDIO &&
3264 media_type != cricket::MEDIA_TYPE_VIDEO) {
3265 return true;
3266 }
3267 std::vector<std::string> rtcp_fb_fields;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00003268 rtc::split(line.c_str(), kSdpDelimiterSpace, &rtcp_fb_fields);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003269 if (rtcp_fb_fields.size() < 2) {
3270 return ParseFailedGetValue(line, kAttributeRtcpFb, error);
3271 }
3272 std::string payload_type_string;
3273 if (!GetValue(rtcp_fb_fields[0], kAttributeRtcpFb, &payload_type_string,
3274 error)) {
3275 return false;
3276 }
wu@webrtc.org5e760e72014-04-02 23:19:09 +00003277 int payload_type = kWildcardPayloadType;
3278 if (payload_type_string != "*") {
pkasting@chromium.orge9facf82015-02-17 20:36:28 +00003279 if (!GetPayloadTypeFromString(line, payload_type_string, &payload_type,
3280 error)) {
wu@webrtc.org5e760e72014-04-02 23:19:09 +00003281 return false;
3282 }
3283 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003284 std::string id = rtcp_fb_fields[1];
3285 std::string param = "";
3286 for (std::vector<std::string>::iterator iter = rtcp_fb_fields.begin() + 2;
3287 iter != rtcp_fb_fields.end(); ++iter) {
3288 param.append(*iter);
3289 }
3290 const cricket::FeedbackParam feedback_param(id, param);
3291
3292 if (media_type == cricket::MEDIA_TYPE_AUDIO) {
pkasting@chromium.orgd3245462015-02-23 21:28:22 +00003293 UpdateCodec<AudioContentDescription, cricket::AudioCodec>(
3294 media_desc, payload_type, feedback_param);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003295 } else if (media_type == cricket::MEDIA_TYPE_VIDEO) {
pkasting@chromium.orgd3245462015-02-23 21:28:22 +00003296 UpdateCodec<VideoContentDescription, cricket::VideoCodec>(
3297 media_desc, payload_type, feedback_param);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003298 }
3299 return true;
3300}
3301
3302} // namespace webrtc