blob: bc2ec89645d37e1667444d38b29c837be0d8fd3a [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"
Edward Lemurc20978e2017-07-06 19:44:34 +020025#include "webrtc/rtc_base/arraysize.h"
26#include "webrtc/rtc_base/checks.h"
27#include "webrtc/rtc_base/logging.h"
28#include "webrtc/rtc_base/messagedigest.h"
29#include "webrtc/rtc_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
jbauch5869f502017-06-29 12:31:36 -07001179 bool encrypted = false;
1180 if (uri == RtpExtension::kEncryptHeaderExtensionsUri) {
1181 // RFC 6904
kjellander88af8b42017-06-29 23:19:31 -07001182 // a=extmap:<value["/"<direction>] urn:ietf:params:rtp-hdrext:encrypt <URI> <extensionattributes>
jbauch5869f502017-06-29 12:31:36 -07001183 const size_t expected_min_fields_encrypted = expected_min_fields + 1;
1184 if (fields.size() < expected_min_fields_encrypted) {
1185 return ParseFailedExpectMinFieldNum(line, expected_min_fields_encrypted,
1186 error);
1187 }
1188
1189 encrypted = true;
1190 uri = fields[2];
1191 if (uri == RtpExtension::kEncryptHeaderExtensionsUri) {
1192 return ParseFailed(line, "Recursive encrypted header.", error);
1193 }
1194 }
1195
1196 *extmap = RtpExtension(uri, value, encrypted);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001197 return true;
1198}
1199
1200void BuildMediaDescription(const ContentInfo* content_info,
1201 const TransportInfo* transport_info,
1202 const MediaType media_type,
wu@webrtc.org4c3e9912014-07-16 21:03:13 +00001203 const std::vector<Candidate>& candidates,
deadbeef9d3584c2016-02-16 17:54:10 -08001204 bool unified_plan_sdp,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001205 std::string* message) {
nisseede5da42017-01-12 05:15:36 -08001206 RTC_DCHECK(message != NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001207 if (content_info == NULL || message == NULL) {
1208 return;
1209 }
1210 // TODO: Rethink if we should use sprintfn instead of stringstream.
1211 // According to the style guide, streams should only be used for logging.
1212 // http://google-styleguide.googlecode.com/svn/
1213 // trunk/cppguide.xml?showone=Streams#Streams
1214 std::ostringstream os;
1215 const MediaContentDescription* media_desc =
henrike@webrtc.org28654cb2013-07-22 21:07:49 +00001216 static_cast<const MediaContentDescription*>(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001217 content_info->description);
nisseede5da42017-01-12 05:15:36 -08001218 RTC_DCHECK(media_desc != NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001219
jiayl@webrtc.org9c16c392014-05-01 18:30:30 +00001220 int sctp_port = cricket::kSctpDefaultPort;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001221
1222 // RFC 4566
1223 // m=<media> <port> <proto> <fmt>
1224 // fmt is a list of payload type numbers that MAY be used in the session.
1225 const char* type = NULL;
1226 if (media_type == cricket::MEDIA_TYPE_AUDIO)
1227 type = kMediaTypeAudio;
1228 else if (media_type == cricket::MEDIA_TYPE_VIDEO)
1229 type = kMediaTypeVideo;
1230 else if (media_type == cricket::MEDIA_TYPE_DATA)
1231 type = kMediaTypeData;
1232 else
nissec80e7412017-01-11 05:56:46 -08001233 RTC_NOTREACHED();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001234
1235 std::string fmt;
1236 if (media_type == cricket::MEDIA_TYPE_VIDEO) {
1237 const VideoContentDescription* video_desc =
1238 static_cast<const VideoContentDescription*>(media_desc);
1239 for (std::vector<cricket::VideoCodec>::const_iterator it =
1240 video_desc->codecs().begin();
1241 it != video_desc->codecs().end(); ++it) {
1242 fmt.append(" ");
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001243 fmt.append(rtc::ToString<int>(it->id));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001244 }
1245 } else if (media_type == cricket::MEDIA_TYPE_AUDIO) {
1246 const AudioContentDescription* audio_desc =
1247 static_cast<const AudioContentDescription*>(media_desc);
1248 for (std::vector<cricket::AudioCodec>::const_iterator it =
1249 audio_desc->codecs().begin();
1250 it != audio_desc->codecs().end(); ++it) {
1251 fmt.append(" ");
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001252 fmt.append(rtc::ToString<int>(it->id));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001253 }
1254 } else if (media_type == cricket::MEDIA_TYPE_DATA) {
jiayl@webrtc.org9c16c392014-05-01 18:30:30 +00001255 const DataContentDescription* data_desc =
1256 static_cast<const DataContentDescription*>(media_desc);
pthatcher@webrtc.org3341b402015-02-13 21:14:22 +00001257 if (IsDtlsSctp(media_desc->protocol())) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001258 fmt.append(" ");
jiayl@webrtc.org9c16c392014-05-01 18:30:30 +00001259
zstein4b2e0822017-02-17 19:48:38 -08001260 if (data_desc->use_sctpmap()) {
1261 for (std::vector<cricket::DataCodec>::const_iterator it =
1262 data_desc->codecs().begin();
1263 it != data_desc->codecs().end(); ++it) {
1264 if (cricket::CodecNamesEq(it->name,
1265 cricket::kGoogleSctpDataCodecName) &&
1266 it->GetParam(cricket::kCodecParamPort, &sctp_port)) {
1267 break;
1268 }
jiayl@webrtc.org9c16c392014-05-01 18:30:30 +00001269 }
jiayl@webrtc.org9c16c392014-05-01 18:30:30 +00001270
zstein4b2e0822017-02-17 19:48:38 -08001271 fmt.append(rtc::ToString<int>(sctp_port));
1272 } else {
1273 fmt.append(kDefaultSctpmapProtocol);
1274 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001275 } else {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001276 for (std::vector<cricket::DataCodec>::const_iterator it =
1277 data_desc->codecs().begin();
1278 it != data_desc->codecs().end(); ++it) {
1279 fmt.append(" ");
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001280 fmt.append(rtc::ToString<int>(it->id));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001281 }
1282 }
1283 }
1284 // The fmt must never be empty. If no codecs are found, set the fmt attribute
1285 // to 0.
1286 if (fmt.empty()) {
1287 fmt = " 0";
1288 }
1289
deadbeef25ed4352016-12-12 18:37:36 -08001290 // The port number in the m line will be updated later when associated with
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001291 // the candidates.
deadbeef25ed4352016-12-12 18:37:36 -08001292 //
1293 // A port value of 0 indicates that the m= section is rejected.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001294 // RFC 3264
1295 // To reject an offered stream, the port number in the corresponding stream in
1296 // the answer MUST be set to zero.
deadbeef25ed4352016-12-12 18:37:36 -08001297 //
1298 // However, the BUNDLE draft adds a new meaning to port zero, when used along
1299 // with a=bundle-only.
zhihuang38989e52017-03-21 11:04:53 -07001300 std::string port = kDummyPort;
1301 if (content_info->rejected || content_info->bundle_only) {
1302 port = kMediaPortRejected;
1303 } else if (!media_desc->connection_address().IsNil()) {
1304 port = rtc::ToString(media_desc->connection_address().port());
1305 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001306
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001307 rtc::SSLFingerprint* fp = (transport_info) ?
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001308 transport_info->description.identity_fingerprint.get() : NULL;
1309
wu@webrtc.org4c3e9912014-07-16 21:03:13 +00001310 // Add the m and c lines.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001311 InitLine(kLineTypeMedia, type, &os);
1312 os << " " << port << " " << media_desc->protocol() << fmt;
zhihuang38989e52017-03-21 11:04:53 -07001313 AddLine(os.str(), message);
1314
1315 InitLine(kLineTypeConnection, kConnectionNettype, &os);
1316 if (media_desc->connection_address().IsNil()) {
1317 os << " " << kConnectionIpv4Addrtype << " " << kDummyAddress;
1318 } else if (media_desc->connection_address().family() == AF_INET) {
1319 os << " " << kConnectionIpv4Addrtype << " "
1320 << media_desc->connection_address().ipaddr().ToString();
1321 } else {
1322 os << " " << kConnectionIpv6Addrtype << " "
1323 << media_desc->connection_address().ipaddr().ToString();
1324 }
1325 AddLine(os.str(), message);
wu@webrtc.org4c3e9912014-07-16 21:03:13 +00001326
1327 // RFC 4566
1328 // b=AS:<bandwidth>
Peter Thatcherc0c3a862015-06-24 15:31:25 -07001329 if (media_desc->bandwidth() >= 1000) {
wu@webrtc.org4c3e9912014-07-16 21:03:13 +00001330 InitLine(kLineTypeSessionBandwidth, kApplicationSpecificMaximum, &os);
1331 os << kSdpDelimiterColon << (media_desc->bandwidth() / 1000);
1332 AddLine(os.str(), message);
1333 }
1334
deadbeef25ed4352016-12-12 18:37:36 -08001335 // Add the a=bundle-only line.
1336 if (content_info->bundle_only) {
1337 InitAttrLine(kAttributeBundleOnly, &os);
1338 AddLine(os.str(), message);
1339 }
1340
wu@webrtc.org4c3e9912014-07-16 21:03:13 +00001341 // Add the a=rtcp line.
pthatcher@webrtc.org3341b402015-02-13 21:14:22 +00001342 if (IsRtp(media_desc->protocol())) {
wu@webrtc.org4c3e9912014-07-16 21:03:13 +00001343 std::string rtcp_line = GetRtcpLine(candidates);
1344 if (!rtcp_line.empty()) {
1345 AddLine(rtcp_line, message);
1346 }
1347 }
1348
honghaiza54a0802015-12-16 18:37:23 -08001349 // Build the a=candidate lines. We don't include ufrag and pwd in the
1350 // candidates in the SDP to avoid redundancy.
1351 BuildCandidate(candidates, false, message);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001352
1353 // Use the transport_info to build the media level ice-ufrag and ice-pwd.
1354 if (transport_info) {
1355 // RFC 5245
1356 // ice-pwd-att = "ice-pwd" ":" password
1357 // ice-ufrag-att = "ice-ufrag" ":" ufrag
1358 // ice-ufrag
deadbeef3f7219b2015-12-28 15:17:14 -08001359 if (!transport_info->description.ice_ufrag.empty()) {
1360 InitAttrLine(kAttributeIceUfrag, &os);
1361 os << kSdpDelimiterColon << transport_info->description.ice_ufrag;
1362 AddLine(os.str(), message);
1363 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001364 // ice-pwd
deadbeef3f7219b2015-12-28 15:17:14 -08001365 if (!transport_info->description.ice_pwd.empty()) {
1366 InitAttrLine(kAttributeIcePwd, &os);
1367 os << kSdpDelimiterColon << transport_info->description.ice_pwd;
1368 AddLine(os.str(), message);
1369 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001370
1371 // draft-petithuguenin-mmusic-ice-attributes-level-03
1372 BuildIceOptions(transport_info->description.transport_options, message);
1373
1374 // RFC 4572
1375 // fingerprint-attribute =
1376 // "fingerprint" ":" hash-func SP fingerprint
1377 if (fp) {
1378 // Insert the fingerprint attribute.
1379 InitAttrLine(kAttributeFingerprint, &os);
1380 os << kSdpDelimiterColon
1381 << fp->algorithm << kSdpDelimiterSpace
1382 << fp->GetRfc4572Fingerprint();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001383 AddLine(os.str(), message);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001384
1385 // Inserting setup attribute.
1386 if (transport_info->description.connection_role !=
1387 cricket::CONNECTIONROLE_NONE) {
1388 // Making sure we are not using "passive" mode.
1389 cricket::ConnectionRole role =
1390 transport_info->description.connection_role;
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00001391 std::string dtls_role_str;
nissec16fa5e2017-02-07 07:18:43 -08001392 const bool success =
1393 cricket::ConnectionRoleToString(role, &dtls_role_str);
1394 RTC_DCHECK(success);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001395 InitAttrLine(kAttributeSetup, &os);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001396 os << kSdpDelimiterColon << dtls_role_str;
1397 AddLine(os.str(), message);
1398 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001399 }
1400 }
1401
1402 // RFC 3388
1403 // mid-attribute = "a=mid:" identification-tag
1404 // identification-tag = token
1405 // Use the content name as the mid identification-tag.
1406 InitAttrLine(kAttributeMid, &os);
1407 os << kSdpDelimiterColon << content_info->name;
1408 AddLine(os.str(), message);
1409
pthatcher@webrtc.org3341b402015-02-13 21:14:22 +00001410 if (IsDtlsSctp(media_desc->protocol())) {
zstein4b2e0822017-02-17 19:48:38 -08001411 const DataContentDescription* data_desc =
1412 static_cast<const DataContentDescription*>(media_desc);
1413 bool use_sctpmap = data_desc->use_sctpmap();
1414 BuildSctpContentAttributes(message, sctp_port, use_sctpmap);
pthatcher@webrtc.org3341b402015-02-13 21:14:22 +00001415 } else if (IsRtp(media_desc->protocol())) {
deadbeef9d3584c2016-02-16 17:54:10 -08001416 BuildRtpContentAttributes(media_desc, media_type, unified_plan_sdp,
1417 message);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001418 }
1419}
1420
zstein4b2e0822017-02-17 19:48:38 -08001421void BuildSctpContentAttributes(std::string* message,
1422 int sctp_port,
1423 bool use_sctpmap) {
wu@webrtc.org78187522013-10-07 23:32:02 +00001424 std::ostringstream os;
zstein4b2e0822017-02-17 19:48:38 -08001425 if (use_sctpmap) {
1426 // draft-ietf-mmusic-sctp-sdp-04
1427 // a=sctpmap:sctpmap-number protocol [streams]
1428 InitAttrLine(kAttributeSctpmap, &os);
1429 os << kSdpDelimiterColon << sctp_port << kSdpDelimiterSpace
1430 << kDefaultSctpmapProtocol << kSdpDelimiterSpace
1431 << cricket::kMaxSctpStreams;
1432 } else {
1433 // draft-ietf-mmusic-sctp-sdp-23
1434 // a=sctp-port:<port>
1435 InitAttrLine(kAttributeSctpPort, &os);
1436 os << kSdpDelimiterColon << sctp_port;
1437 // TODO(zstein): emit max-message-size here
1438 }
wu@webrtc.org78187522013-10-07 23:32:02 +00001439 AddLine(os.str(), message);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001440}
1441
deadbeef9d3584c2016-02-16 17:54:10 -08001442// If unified_plan_sdp is true, will use "a=msid".
1443void BuildRtpContentAttributes(const MediaContentDescription* media_desc,
1444 const MediaType media_type,
1445 bool unified_plan_sdp,
1446 std::string* message) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001447 std::ostringstream os;
1448 // RFC 5285
1449 // a=extmap:<value>["/"<direction>] <URI> <extensionattributes>
1450 // The definitions MUST be either all session level or all media level. This
1451 // implementation uses all media level.
1452 for (size_t i = 0; i < media_desc->rtp_header_extensions().size(); ++i) {
jbauch5869f502017-06-29 12:31:36 -07001453 const RtpExtension& extension = media_desc->rtp_header_extensions()[i];
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001454 InitAttrLine(kAttributeExtmap, &os);
jbauch5869f502017-06-29 12:31:36 -07001455 os << kSdpDelimiterColon << extension.id;
1456 if (extension.encrypt) {
1457 os << kSdpDelimiterSpace << RtpExtension::kEncryptHeaderExtensionsUri;
1458 }
1459 os << kSdpDelimiterSpace << extension.uri;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001460 AddLine(os.str(), message);
1461 }
1462
1463 // RFC 3264
1464 // a=sendrecv || a=sendonly || a=sendrecv || a=inactive
deadbeefc80741f2015-10-22 13:14:45 -07001465 switch (media_desc->direction()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001466 case cricket::MD_INACTIVE:
1467 InitAttrLine(kAttributeInactive, &os);
1468 break;
1469 case cricket::MD_SENDONLY:
1470 InitAttrLine(kAttributeSendOnly, &os);
1471 break;
1472 case cricket::MD_RECVONLY:
1473 InitAttrLine(kAttributeRecvOnly, &os);
1474 break;
1475 case cricket::MD_SENDRECV:
1476 default:
1477 InitAttrLine(kAttributeSendRecv, &os);
1478 break;
1479 }
1480 AddLine(os.str(), message);
1481
deadbeef9d3584c2016-02-16 17:54:10 -08001482 // draft-ietf-mmusic-msid-11
1483 // a=msid:<stream id> <track id>
1484 if (unified_plan_sdp && !media_desc->streams().empty()) {
1485 if (media_desc->streams().size() > 1u) {
1486 LOG(LS_WARNING) << "Trying to serialize unified plan SDP with more than "
1487 << "one track in a media section. Omitting 'a=msid'.";
1488 } else {
1489 auto track = media_desc->streams().begin();
1490 const std::string& stream_id = track->sync_label;
deadbeef9d3584c2016-02-16 17:54:10 -08001491 InitAttrLine(kAttributeMsid, &os);
1492 os << kSdpDelimiterColon << stream_id << kSdpDelimiterSpace << track->id;
1493 AddLine(os.str(), message);
1494 }
1495 }
1496
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001497 // RFC 5761
1498 // a=rtcp-mux
1499 if (media_desc->rtcp_mux()) {
1500 InitAttrLine(kAttributeRtcpMux, &os);
1501 AddLine(os.str(), message);
1502 }
1503
deadbeef13871492015-12-09 12:37:51 -08001504 // RFC 5506
1505 // a=rtcp-rsize
1506 if (media_desc->rtcp_reduced_size()) {
1507 InitAttrLine(kAttributeRtcpReducedSize, &os);
1508 AddLine(os.str(), message);
1509 }
1510
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001511 // RFC 4568
1512 // a=crypto:<tag> <crypto-suite> <key-params> [<session-params>]
1513 for (std::vector<CryptoParams>::const_iterator it =
1514 media_desc->cryptos().begin();
1515 it != media_desc->cryptos().end(); ++it) {
1516 InitAttrLine(kAttributeCrypto, &os);
1517 os << kSdpDelimiterColon << it->tag << " " << it->cipher_suite << " "
1518 << it->key_params;
1519 if (!it->session_params.empty()) {
1520 os << " " << it->session_params;
1521 }
1522 AddLine(os.str(), message);
1523 }
1524
1525 // RFC 4566
1526 // a=rtpmap:<payload type> <encoding name>/<clock rate>
1527 // [/<encodingparameters>]
1528 BuildRtpMap(media_desc, media_type, message);
1529
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001530 for (StreamParamsVec::const_iterator track = media_desc->streams().begin();
1531 track != media_desc->streams().end(); ++track) {
1532 // Require that the track belongs to a media stream,
1533 // ie the sync_label is set. This extra check is necessary since the
1534 // MediaContentDescription always contains a streamparam with an ssrc even
1535 // if no track or media stream have been created.
1536 if (track->sync_label.empty()) continue;
1537
1538 // Build the ssrc-group lines.
1539 for (size_t i = 0; i < track->ssrc_groups.size(); ++i) {
1540 // RFC 5576
1541 // a=ssrc-group:<semantics> <ssrc-id> ...
1542 if (track->ssrc_groups[i].ssrcs.empty()) {
1543 continue;
1544 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001545 InitAttrLine(kAttributeSsrcGroup, &os);
1546 os << kSdpDelimiterColon << track->ssrc_groups[i].semantics;
Peter Boström0c4e06b2015-10-07 12:23:21 +02001547 std::vector<uint32_t>::const_iterator ssrc =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001548 track->ssrc_groups[i].ssrcs.begin();
1549 for (; ssrc != track->ssrc_groups[i].ssrcs.end(); ++ssrc) {
Peter Boström0c4e06b2015-10-07 12:23:21 +02001550 os << kSdpDelimiterSpace << rtc::ToString<uint32_t>(*ssrc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001551 }
1552 AddLine(os.str(), message);
1553 }
1554 // Build the ssrc lines for each ssrc.
1555 for (size_t i = 0; i < track->ssrcs.size(); ++i) {
Peter Boström0c4e06b2015-10-07 12:23:21 +02001556 uint32_t ssrc = track->ssrcs[i];
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001557 // RFC 5576
1558 // a=ssrc:<ssrc-id> cname:<value>
1559 AddSsrcLine(ssrc, kSsrcAttributeCname,
1560 track->cname, message);
1561
1562 // draft-alvestrand-mmusic-msid-00
1563 // a=ssrc:<ssrc-id> msid:identifier [appdata]
deadbeef9d3584c2016-02-16 17:54:10 -08001564 // The appdata consists of the "id" attribute of a MediaStreamTrack,
1565 // which corresponds to the "id" attribute of StreamParams.
1566 const std::string& stream_id = track->sync_label;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001567 InitAttrLine(kAttributeSsrc, &os);
1568 os << kSdpDelimiterColon << ssrc << kSdpDelimiterSpace
deadbeef9d3584c2016-02-16 17:54:10 -08001569 << kSsrcAttributeMsid << kSdpDelimiterColon << stream_id
1570 << kSdpDelimiterSpace << track->id;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001571 AddLine(os.str(), message);
1572
deadbeef9d3584c2016-02-16 17:54:10 -08001573 // TODO(ronghuawu): Remove below code which is for backward
1574 // compatibility.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001575 // draft-alvestrand-rtcweb-mid-01
1576 // a=ssrc:<ssrc-id> mslabel:<value>
1577 // The label isn't yet defined.
1578 // a=ssrc:<ssrc-id> label:<value>
1579 AddSsrcLine(ssrc, kSsrcAttributeMslabel, track->sync_label, message);
1580 AddSsrcLine(ssrc, kSSrcAttributeLabel, track->id, message);
1581 }
1582 }
1583}
1584
1585void WriteFmtpHeader(int payload_type, std::ostringstream* os) {
1586 // fmtp header: a=fmtp:|payload_type| <parameters>
1587 // Add a=fmtp
1588 InitAttrLine(kAttributeFmtp, os);
1589 // Add :|payload_type|
1590 *os << kSdpDelimiterColon << payload_type;
1591}
1592
1593void WriteRtcpFbHeader(int payload_type, std::ostringstream* os) {
1594 // rtcp-fb header: a=rtcp-fb:|payload_type|
1595 // <parameters>/<ccm <ccm_parameters>>
1596 // Add a=rtcp-fb
1597 InitAttrLine(kAttributeRtcpFb, os);
1598 // Add :
1599 *os << kSdpDelimiterColon;
1600 if (payload_type == kWildcardPayloadType) {
1601 *os << "*";
1602 } else {
1603 *os << payload_type;
1604 }
1605}
1606
1607void WriteFmtpParameter(const std::string& parameter_name,
1608 const std::string& parameter_value,
1609 std::ostringstream* os) {
1610 // fmtp parameters: |parameter_name|=|parameter_value|
1611 *os << parameter_name << kSdpDelimiterEqual << parameter_value;
1612}
1613
1614void WriteFmtpParameters(const cricket::CodecParameterMap& parameters,
1615 std::ostringstream* os) {
1616 for (cricket::CodecParameterMap::const_iterator fmtp = parameters.begin();
1617 fmtp != parameters.end(); ++fmtp) {
hta62a216e2016-04-15 11:02:14 -07001618 // Parameters are a semicolon-separated list, no spaces.
1619 // The list is separated from the header by a space.
1620 if (fmtp == parameters.begin()) {
1621 *os << kSdpDelimiterSpace;
1622 } else {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001623 *os << kSdpDelimiterSemicolon;
1624 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001625 WriteFmtpParameter(fmtp->first, fmtp->second, os);
1626 }
1627}
1628
1629bool IsFmtpParam(const std::string& name) {
ossuaa4b0772017-01-30 07:41:18 -08001630 // RFC 4855, section 3 specifies the mapping of media format parameters to SDP
1631 // parameters. Only ptime, maxptime, channels and rate are placed outside of
1632 // the fmtp line. In WebRTC, channels and rate are already handled separately
1633 // and thus not included in the CodecParameterMap.
1634 return name != kCodecParamPTime && name != kCodecParamMaxPTime;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001635}
1636
1637// Retreives fmtp parameters from |params|, which may contain other parameters
1638// as well, and puts them in |fmtp_parameters|.
1639void GetFmtpParams(const cricket::CodecParameterMap& params,
1640 cricket::CodecParameterMap* fmtp_parameters) {
1641 for (cricket::CodecParameterMap::const_iterator iter = params.begin();
1642 iter != params.end(); ++iter) {
1643 if (IsFmtpParam(iter->first)) {
1644 (*fmtp_parameters)[iter->first] = iter->second;
1645 }
1646 }
1647}
1648
1649template <class T>
1650void AddFmtpLine(const T& codec, std::string* message) {
1651 cricket::CodecParameterMap fmtp_parameters;
1652 GetFmtpParams(codec.params, &fmtp_parameters);
1653 if (fmtp_parameters.empty()) {
1654 // No need to add an fmtp if it will have no (optional) parameters.
1655 return;
1656 }
1657 std::ostringstream os;
1658 WriteFmtpHeader(codec.id, &os);
1659 WriteFmtpParameters(fmtp_parameters, &os);
1660 AddLine(os.str(), message);
1661 return;
1662}
1663
1664template <class T>
1665void AddRtcpFbLines(const T& codec, std::string* message) {
1666 for (std::vector<cricket::FeedbackParam>::const_iterator iter =
1667 codec.feedback_params.params().begin();
1668 iter != codec.feedback_params.params().end(); ++iter) {
1669 std::ostringstream os;
1670 WriteRtcpFbHeader(codec.id, &os);
1671 os << " " << iter->id();
1672 if (!iter->param().empty()) {
1673 os << " " << iter->param();
1674 }
1675 AddLine(os.str(), message);
1676 }
1677}
1678
jiayl@webrtc.orgddb85ab2014-09-05 16:31:56 +00001679bool AddSctpDataCodec(DataContentDescription* media_desc,
1680 int sctp_port) {
solenberg9fa49752016-10-08 13:02:44 -07001681 for (const auto& codec : media_desc->codecs()) {
1682 if (cricket::CodecNamesEq(codec.name, cricket::kGoogleSctpDataCodecName)) {
1683 return ParseFailed("",
1684 "Can't have multiple sctp port attributes.",
1685 NULL);
1686 }
jiayl@webrtc.orgddb85ab2014-09-05 16:31:56 +00001687 }
1688 // Add the SCTP Port number as a pseudo-codec "port" parameter
solenberg9fa49752016-10-08 13:02:44 -07001689 cricket::DataCodec codec_port(cricket::kGoogleSctpDataCodecPlType,
deadbeef67cf2c12016-04-13 10:07:16 -07001690 cricket::kGoogleSctpDataCodecName);
jiayl@webrtc.orgddb85ab2014-09-05 16:31:56 +00001691 codec_port.SetParam(cricket::kCodecParamPort, sctp_port);
1692 LOG(INFO) << "AddSctpDataCodec: Got SCTP Port Number "
1693 << sctp_port;
1694 media_desc->AddCodec(codec_port);
1695 return true;
1696}
1697
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001698bool GetMinValue(const std::vector<int>& values, int* value) {
1699 if (values.empty()) {
1700 return false;
1701 }
1702 std::vector<int>::const_iterator found =
1703 std::min_element(values.begin(), values.end());
1704 *value = *found;
1705 return true;
1706}
1707
1708bool GetParameter(const std::string& name,
1709 const cricket::CodecParameterMap& params, int* value) {
1710 std::map<std::string, std::string>::const_iterator found =
1711 params.find(name);
1712 if (found == params.end()) {
1713 return false;
1714 }
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001715 if (!rtc::FromString(found->second, value)) {
wu@webrtc.org5e760e72014-04-02 23:19:09 +00001716 return false;
1717 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001718 return true;
1719}
1720
1721void BuildRtpMap(const MediaContentDescription* media_desc,
1722 const MediaType media_type,
1723 std::string* message) {
nisseede5da42017-01-12 05:15:36 -08001724 RTC_DCHECK(message != NULL);
1725 RTC_DCHECK(media_desc != NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001726 std::ostringstream os;
1727 if (media_type == cricket::MEDIA_TYPE_VIDEO) {
1728 const VideoContentDescription* video_desc =
1729 static_cast<const VideoContentDescription*>(media_desc);
1730 for (std::vector<cricket::VideoCodec>::const_iterator it =
1731 video_desc->codecs().begin();
1732 it != video_desc->codecs().end(); ++it) {
1733 // RFC 4566
1734 // a=rtpmap:<payload type> <encoding name>/<clock rate>
1735 // [/<encodingparameters>]
1736 if (it->id != kWildcardPayloadType) {
1737 InitAttrLine(kAttributeRtpmap, &os);
deadbeefe814a0d2017-02-25 18:15:09 -08001738 os << kSdpDelimiterColon << it->id << " " << it->name << "/"
1739 << cricket::kVideoCodecClockrate;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001740 AddLine(os.str(), message);
1741 }
1742 AddRtcpFbLines(*it, message);
1743 AddFmtpLine(*it, message);
1744 }
1745 } else if (media_type == cricket::MEDIA_TYPE_AUDIO) {
1746 const AudioContentDescription* audio_desc =
1747 static_cast<const AudioContentDescription*>(media_desc);
1748 std::vector<int> ptimes;
1749 std::vector<int> maxptimes;
1750 int max_minptime = 0;
1751 for (std::vector<cricket::AudioCodec>::const_iterator it =
1752 audio_desc->codecs().begin();
1753 it != audio_desc->codecs().end(); ++it) {
nisseede5da42017-01-12 05:15:36 -08001754 RTC_DCHECK(!it->name.empty());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001755 // RFC 4566
1756 // a=rtpmap:<payload type> <encoding name>/<clock rate>
1757 // [/<encodingparameters>]
1758 InitAttrLine(kAttributeRtpmap, &os);
1759 os << kSdpDelimiterColon << it->id << " ";
1760 os << it->name << "/" << it->clockrate;
1761 if (it->channels != 1) {
1762 os << "/" << it->channels;
1763 }
1764 AddLine(os.str(), message);
1765 AddRtcpFbLines(*it, message);
1766 AddFmtpLine(*it, message);
1767 int minptime = 0;
1768 if (GetParameter(kCodecParamMinPTime, it->params, &minptime)) {
1769 max_minptime = std::max(minptime, max_minptime);
1770 }
1771 int ptime;
1772 if (GetParameter(kCodecParamPTime, it->params, &ptime)) {
1773 ptimes.push_back(ptime);
1774 }
1775 int maxptime;
1776 if (GetParameter(kCodecParamMaxPTime, it->params, &maxptime)) {
1777 maxptimes.push_back(maxptime);
1778 }
1779 }
1780 // Populate the maxptime attribute with the smallest maxptime of all codecs
1781 // under the same m-line.
1782 int min_maxptime = INT_MAX;
1783 if (GetMinValue(maxptimes, &min_maxptime)) {
1784 AddAttributeLine(kCodecParamMaxPTime, min_maxptime, message);
1785 }
nisseede5da42017-01-12 05:15:36 -08001786 RTC_DCHECK(min_maxptime > max_minptime);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001787 // Populate the ptime attribute with the smallest ptime or the largest
1788 // minptime, whichever is the largest, for all codecs under the same m-line.
1789 int ptime = INT_MAX;
1790 if (GetMinValue(ptimes, &ptime)) {
1791 ptime = std::min(ptime, min_maxptime);
1792 ptime = std::max(ptime, max_minptime);
1793 AddAttributeLine(kCodecParamPTime, ptime, message);
1794 }
1795 } else if (media_type == cricket::MEDIA_TYPE_DATA) {
1796 const DataContentDescription* data_desc =
1797 static_cast<const DataContentDescription*>(media_desc);
1798 for (std::vector<cricket::DataCodec>::const_iterator it =
1799 data_desc->codecs().begin();
1800 it != data_desc->codecs().end(); ++it) {
1801 // RFC 4566
1802 // a=rtpmap:<payload type> <encoding name>/<clock rate>
1803 // [/<encodingparameters>]
1804 InitAttrLine(kAttributeRtpmap, &os);
1805 os << kSdpDelimiterColon << it->id << " "
1806 << it->name << "/" << it->clockrate;
1807 AddLine(os.str(), message);
1808 }
1809 }
1810}
1811
1812void BuildCandidate(const std::vector<Candidate>& candidates,
honghaiza54a0802015-12-16 18:37:23 -08001813 bool include_ufrag,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001814 std::string* message) {
1815 std::ostringstream os;
1816
1817 for (std::vector<Candidate>::const_iterator it = candidates.begin();
1818 it != candidates.end(); ++it) {
1819 // RFC 5245
1820 // a=candidate:<foundation> <component-id> <transport> <priority>
1821 // <connection-address> <port> typ <candidate-types>
1822 // [raddr <connection-address>] [rport <port>]
1823 // *(SP extension-att-name SP extension-att-value)
1824 std::string type;
1825 // Map the cricket candidate type to "host" / "srflx" / "prflx" / "relay"
1826 if (it->type() == cricket::LOCAL_PORT_TYPE) {
1827 type = kCandidateHost;
1828 } else if (it->type() == cricket::STUN_PORT_TYPE) {
1829 type = kCandidateSrflx;
1830 } else if (it->type() == cricket::RELAY_PORT_TYPE) {
1831 type = kCandidateRelay;
Honghai Zhang7fb69db2016-03-14 11:59:18 -07001832 } else if (it->type() == cricket::PRFLX_PORT_TYPE) {
1833 type = kCandidatePrflx;
1834 // Peer reflexive candidate may be signaled for being removed.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001835 } else {
nissec80e7412017-01-11 05:56:46 -08001836 RTC_NOTREACHED();
Peter Thatcher019087f2015-04-28 09:06:26 -07001837 // Never write out candidates if we don't know the type.
1838 continue;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001839 }
1840
1841 InitAttrLine(kAttributeCandidate, &os);
1842 os << kSdpDelimiterColon
mallinath@webrtc.org2d60c5e2014-08-08 22:29:20 +00001843 << it->foundation() << " "
1844 << it->component() << " "
1845 << it->protocol() << " "
1846 << it->priority() << " "
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001847 << it->address().ipaddr().ToString() << " "
1848 << it->address().PortAsString() << " "
mallinath@webrtc.org2d60c5e2014-08-08 22:29:20 +00001849 << kAttributeCandidateTyp << " "
1850 << type << " ";
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001851
1852 // Related address
1853 if (!it->related_address().IsNil()) {
1854 os << kAttributeCandidateRaddr << " "
1855 << it->related_address().ipaddr().ToString() << " "
1856 << kAttributeCandidateRport << " "
1857 << it->related_address().PortAsString() << " ";
1858 }
1859
mallinath@webrtc.org2d60c5e2014-08-08 22:29:20 +00001860 if (it->protocol() == cricket::TCP_PROTOCOL_NAME) {
mallinath@webrtc.orge999bd02014-08-13 06:05:55 +00001861 os << kTcpCandidateType << " " << it->tcptype() << " ";
mallinath@webrtc.org2d60c5e2014-08-08 22:29:20 +00001862 }
1863
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001864 // Extensions
1865 os << kAttributeCandidateGeneration << " " << it->generation();
honghaiza54a0802015-12-16 18:37:23 -08001866 if (include_ufrag && !it->username().empty()) {
1867 os << " " << kAttributeCandidateUfrag << " " << it->username();
1868 }
honghaiza0c44ea2016-03-23 16:07:48 -07001869 if (it->network_id() > 0) {
1870 os << " " << kAttributeCandidateNetworkId << " " << it->network_id();
1871 }
honghaize1a0c942016-02-16 14:54:56 -08001872 if (it->network_cost() > 0) {
1873 os << " " << kAttributeCandidateNetworkCost << " " << it->network_cost();
1874 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001875
1876 AddLine(os.str(), message);
1877 }
1878}
1879
1880void BuildIceOptions(const std::vector<std::string>& transport_options,
1881 std::string* message) {
1882 if (!transport_options.empty()) {
1883 std::ostringstream os;
1884 InitAttrLine(kAttributeIceOption, &os);
1885 os << kSdpDelimiterColon << transport_options[0];
1886 for (size_t i = 1; i < transport_options.size(); ++i) {
1887 os << kSdpDelimiterSpace << transport_options[i];
1888 }
1889 AddLine(os.str(), message);
1890 }
1891}
1892
pthatcher@webrtc.org3341b402015-02-13 21:14:22 +00001893bool IsRtp(const std::string& protocol) {
1894 return protocol.empty() ||
1895 (protocol.find(cricket::kMediaProtocolRtpPrefix) != std::string::npos);
1896}
1897
1898bool IsDtlsSctp(const std::string& protocol) {
1899 // This intentionally excludes "SCTP" and "SCTP/DTLS".
lally@webrtc.orga7470932015-02-24 20:19:21 +00001900 return protocol.find(cricket::kMediaProtocolDtlsSctp) != std::string::npos;
pthatcher@webrtc.org3341b402015-02-13 21:14:22 +00001901}
1902
zhihuang38989e52017-03-21 11:04:53 -07001903bool ParseConnectionData(const std::string& line,
1904 rtc::SocketAddress* addr,
1905 SdpParseError* error) {
1906 // Parse the line from left to right.
1907 std::string token;
1908 std::string rightpart;
1909 // RFC 4566
1910 // c=<nettype> <addrtype> <connection-address>
1911 // Skip the "c="
1912 if (!rtc::tokenize_first(line, kSdpDelimiterEqual, &token, &rightpart)) {
1913 return ParseFailed(line, "Failed to parse the network type.", error);
1914 }
1915
1916 // Extract and verify the <nettype>
1917 if (!rtc::tokenize_first(rightpart, kSdpDelimiterSpace, &token, &rightpart) ||
1918 token != kConnectionNettype) {
1919 return ParseFailed(line,
1920 "Failed to parse the connection data. The network type "
1921 "is not currently supported.",
1922 error);
1923 }
1924
1925 // Extract the "<addrtype>" and "<connection-address>".
1926 if (!rtc::tokenize_first(rightpart, kSdpDelimiterSpace, &token, &rightpart)) {
1927 return ParseFailed(line, "Failed to parse the address type.", error);
1928 }
1929
1930 // The rightpart part should be the IP address without the slash which is used
1931 // for multicast.
1932 if (rightpart.find('/') != std::string::npos) {
1933 return ParseFailed(line,
1934 "Failed to parse the connection data. Multicast is not "
1935 "currently supported.",
1936 error);
1937 }
1938 addr->SetIP(rightpart);
1939
1940 // Verify that the addrtype matches the type of the parsed address.
1941 if ((addr->family() == AF_INET && token != "IP4") ||
1942 (addr->family() == AF_INET6 && token != "IP6")) {
1943 addr->Clear();
1944 return ParseFailed(
1945 line,
1946 "Failed to parse the connection data. The address type is mismatching.",
1947 error);
1948 }
1949 return true;
1950}
1951
1952bool ParseSessionDescription(const std::string& message,
1953 size_t* pos,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001954 std::string* session_id,
1955 std::string* session_version,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001956 TransportDescription* session_td,
1957 RtpHeaderExtensions* session_extmaps,
zhihuang38989e52017-03-21 11:04:53 -07001958 rtc::SocketAddress* connection_addr,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001959 cricket::SessionDescription* desc,
1960 SdpParseError* error) {
1961 std::string line;
1962
deadbeefc80741f2015-10-22 13:14:45 -07001963 desc->set_msid_supported(false);
1964
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001965 // RFC 4566
1966 // v= (protocol version)
1967 if (!GetLineWithType(message, pos, &line, kLineTypeVersion)) {
1968 return ParseFailedExpectLine(message, *pos, kLineTypeVersion,
1969 std::string(), error);
1970 }
1971 // RFC 4566
1972 // o=<username> <sess-id> <sess-version> <nettype> <addrtype>
1973 // <unicast-address>
1974 if (!GetLineWithType(message, pos, &line, kLineTypeOrigin)) {
1975 return ParseFailedExpectLine(message, *pos, kLineTypeOrigin,
1976 std::string(), error);
1977 }
1978 std::vector<std::string> fields;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001979 rtc::split(line.substr(kLinePrefixLength),
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001980 kSdpDelimiterSpace, &fields);
1981 const size_t expected_fields = 6;
1982 if (fields.size() != expected_fields) {
1983 return ParseFailedExpectFieldNum(line, expected_fields, error);
1984 }
1985 *session_id = fields[1];
1986 *session_version = fields[2];
1987
1988 // RFC 4566
1989 // s= (session name)
1990 if (!GetLineWithType(message, pos, &line, kLineTypeSessionName)) {
1991 return ParseFailedExpectLine(message, *pos, kLineTypeSessionName,
1992 std::string(), error);
1993 }
1994
1995 // Optional lines
1996 // Those are the optional lines, so shouldn't return false if not present.
1997 // RFC 4566
1998 // i=* (session information)
1999 GetLineWithType(message, pos, &line, kLineTypeSessionInfo);
2000
2001 // RFC 4566
2002 // u=* (URI of description)
2003 GetLineWithType(message, pos, &line, kLineTypeSessionUri);
2004
2005 // RFC 4566
2006 // e=* (email address)
2007 GetLineWithType(message, pos, &line, kLineTypeSessionEmail);
2008
2009 // RFC 4566
2010 // p=* (phone number)
2011 GetLineWithType(message, pos, &line, kLineTypeSessionPhone);
2012
2013 // RFC 4566
2014 // c=* (connection information -- not required if included in
2015 // all media)
zhihuang38989e52017-03-21 11:04:53 -07002016 if (GetLineWithType(message, pos, &line, kLineTypeConnection)) {
2017 if (!ParseConnectionData(line, connection_addr, error)) {
2018 return false;
2019 }
2020 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002021
2022 // RFC 4566
2023 // b=* (zero or more bandwidth information lines)
2024 while (GetLineWithType(message, pos, &line, kLineTypeSessionBandwidth)) {
2025 // By pass zero or more b lines.
2026 }
2027
2028 // RFC 4566
2029 // One or more time descriptions ("t=" and "r=" lines; see below)
2030 // t= (time the session is active)
2031 // r=* (zero or more repeat times)
2032 // Ensure there's at least one time description
2033 if (!GetLineWithType(message, pos, &line, kLineTypeTiming)) {
2034 return ParseFailedExpectLine(message, *pos, kLineTypeTiming, std::string(),
2035 error);
2036 }
2037
2038 while (GetLineWithType(message, pos, &line, kLineTypeRepeatTimes)) {
2039 // By pass zero or more r lines.
2040 }
2041
2042 // Go through the rest of the time descriptions
2043 while (GetLineWithType(message, pos, &line, kLineTypeTiming)) {
2044 while (GetLineWithType(message, pos, &line, kLineTypeRepeatTimes)) {
2045 // By pass zero or more r lines.
2046 }
2047 }
2048
2049 // RFC 4566
2050 // z=* (time zone adjustments)
2051 GetLineWithType(message, pos, &line, kLineTypeTimeZone);
2052
2053 // RFC 4566
2054 // k=* (encryption key)
2055 GetLineWithType(message, pos, &line, kLineTypeEncryptionKey);
2056
2057 // RFC 4566
2058 // a=* (zero or more session attribute lines)
2059 while (GetLineWithType(message, pos, &line, kLineTypeAttributes)) {
2060 if (HasAttribute(line, kAttributeGroup)) {
2061 if (!ParseGroupAttribute(line, desc, error)) {
2062 return false;
2063 }
2064 } else if (HasAttribute(line, kAttributeIceUfrag)) {
2065 if (!GetValue(line, kAttributeIceUfrag,
2066 &(session_td->ice_ufrag), error)) {
2067 return false;
2068 }
2069 } else if (HasAttribute(line, kAttributeIcePwd)) {
2070 if (!GetValue(line, kAttributeIcePwd, &(session_td->ice_pwd), error)) {
2071 return false;
2072 }
2073 } else if (HasAttribute(line, kAttributeIceLite)) {
2074 session_td->ice_mode = cricket::ICEMODE_LITE;
2075 } else if (HasAttribute(line, kAttributeIceOption)) {
2076 if (!ParseIceOptions(line, &(session_td->transport_options), error)) {
2077 return false;
2078 }
2079 } else if (HasAttribute(line, kAttributeFingerprint)) {
2080 if (session_td->identity_fingerprint.get()) {
2081 return ParseFailed(
2082 line,
2083 "Can't have multiple fingerprint attributes at the same level.",
2084 error);
2085 }
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002086 rtc::SSLFingerprint* fingerprint = NULL;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002087 if (!ParseFingerprintAttribute(line, &fingerprint, error)) {
2088 return false;
2089 }
2090 session_td->identity_fingerprint.reset(fingerprint);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00002091 } else if (HasAttribute(line, kAttributeSetup)) {
2092 if (!ParseDtlsSetup(line, &(session_td->connection_role), error)) {
2093 return false;
2094 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002095 } else if (HasAttribute(line, kAttributeMsidSemantics)) {
2096 std::string semantics;
2097 if (!GetValue(line, kAttributeMsidSemantics, &semantics, error)) {
2098 return false;
2099 }
deadbeefc80741f2015-10-22 13:14:45 -07002100 desc->set_msid_supported(
2101 CaseInsensitiveFind(semantics, kMediaStreamSemantic));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002102 } else if (HasAttribute(line, kAttributeExtmap)) {
isheriff6f8d6862016-05-26 11:24:55 -07002103 RtpExtension extmap;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002104 if (!ParseExtmap(line, &extmap, error)) {
2105 return false;
2106 }
2107 session_extmaps->push_back(extmap);
2108 }
2109 }
2110
2111 return true;
2112}
2113
2114bool ParseGroupAttribute(const std::string& line,
2115 cricket::SessionDescription* desc,
2116 SdpParseError* error) {
nisseede5da42017-01-12 05:15:36 -08002117 RTC_DCHECK(desc != NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002118
2119 // RFC 5888 and draft-holmberg-mmusic-sdp-bundle-negotiation-00
2120 // a=group:BUNDLE video voice
2121 std::vector<std::string> fields;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002122 rtc::split(line.substr(kLinePrefixLength),
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002123 kSdpDelimiterSpace, &fields);
2124 std::string semantics;
2125 if (!GetValue(fields[0], kAttributeGroup, &semantics, error)) {
2126 return false;
2127 }
2128 cricket::ContentGroup group(semantics);
2129 for (size_t i = 1; i < fields.size(); ++i) {
2130 group.AddContentName(fields[i]);
2131 }
2132 desc->AddGroup(group);
2133 return true;
2134}
2135
2136static bool ParseFingerprintAttribute(const std::string& line,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002137 rtc::SSLFingerprint** fingerprint,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002138 SdpParseError* error) {
2139 if (!IsLineType(line, kLineTypeAttributes) ||
2140 !HasAttribute(line, kAttributeFingerprint)) {
2141 return ParseFailedExpectLine(line, 0, kLineTypeAttributes,
2142 kAttributeFingerprint, error);
2143 }
2144
2145 std::vector<std::string> fields;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002146 rtc::split(line.substr(kLinePrefixLength),
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002147 kSdpDelimiterSpace, &fields);
2148 const size_t expected_fields = 2;
2149 if (fields.size() != expected_fields) {
2150 return ParseFailedExpectFieldNum(line, expected_fields, error);
2151 }
2152
2153 // The first field here is "fingerprint:<hash>.
2154 std::string algorithm;
2155 if (!GetValue(fields[0], kAttributeFingerprint, &algorithm, error)) {
2156 return false;
2157 }
2158
2159 // Downcase the algorithm. Note that we don't need to downcase the
2160 // fingerprint because hex_decode can handle upper-case.
2161 std::transform(algorithm.begin(), algorithm.end(), algorithm.begin(),
2162 ::tolower);
2163
2164 // The second field is the digest value. De-hexify it.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002165 *fingerprint = rtc::SSLFingerprint::CreateFromRfc4572(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002166 algorithm, fields[1]);
2167 if (!*fingerprint) {
2168 return ParseFailed(line,
2169 "Failed to create fingerprint from the digest.",
2170 error);
2171 }
2172
2173 return true;
2174}
2175
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00002176static bool ParseDtlsSetup(const std::string& line,
2177 cricket::ConnectionRole* role,
2178 SdpParseError* error) {
2179 // setup-attr = "a=setup:" role
2180 // role = "active" / "passive" / "actpass" / "holdconn"
2181 std::vector<std::string> fields;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002182 rtc::split(line.substr(kLinePrefixLength), kSdpDelimiterColon, &fields);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00002183 const size_t expected_fields = 2;
2184 if (fields.size() != expected_fields) {
2185 return ParseFailedExpectFieldNum(line, expected_fields, error);
2186 }
2187 std::string role_str = fields[1];
2188 if (!cricket::StringToConnectionRole(role_str, role)) {
2189 return ParseFailed(line, "Invalid attribute value.", error);
2190 }
2191 return true;
2192}
2193
deadbeef9d3584c2016-02-16 17:54:10 -08002194static bool ParseMsidAttribute(const std::string& line,
2195 std::string* stream_id,
2196 std::string* track_id,
2197 SdpParseError* error) {
2198 // draft-ietf-mmusic-msid-11
2199 // a=msid:<stream id> <track id>
2200 // msid-value = msid-id [ SP msid-appdata ]
2201 // msid-id = 1*64token-char ; see RFC 4566
2202 // msid-appdata = 1*64token-char ; see RFC 4566
2203 std::string field1;
2204 if (!rtc::tokenize_first(line.substr(kLinePrefixLength), kSdpDelimiterSpace,
2205 &field1, track_id)) {
2206 const size_t expected_fields = 2;
2207 return ParseFailedExpectFieldNum(line, expected_fields, error);
2208 }
2209
deadbeefa4549d62017-02-10 17:26:22 -08002210 if (track_id->empty()) {
2211 return ParseFailed(line, "Missing track ID in msid attribute.", error);
2212 }
2213
deadbeef9d3584c2016-02-16 17:54:10 -08002214 // msid:<msid-id>
2215 if (!GetValue(field1, kAttributeMsid, stream_id, error)) {
2216 return false;
2217 }
deadbeefa4549d62017-02-10 17:26:22 -08002218 if (stream_id->empty()) {
2219 return ParseFailed(line, "Missing stream ID in msid attribute.", error);
2220 }
deadbeef9d3584c2016-02-16 17:54:10 -08002221 return true;
2222}
2223
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002224// RFC 3551
2225// PT encoding media type clock rate channels
2226// name (Hz)
2227// 0 PCMU A 8,000 1
2228// 1 reserved A
2229// 2 reserved A
2230// 3 GSM A 8,000 1
2231// 4 G723 A 8,000 1
2232// 5 DVI4 A 8,000 1
2233// 6 DVI4 A 16,000 1
2234// 7 LPC A 8,000 1
2235// 8 PCMA A 8,000 1
2236// 9 G722 A 8,000 1
2237// 10 L16 A 44,100 2
2238// 11 L16 A 44,100 1
2239// 12 QCELP A 8,000 1
2240// 13 CN A 8,000 1
2241// 14 MPA A 90,000 (see text)
2242// 15 G728 A 8,000 1
2243// 16 DVI4 A 11,025 1
2244// 17 DVI4 A 22,050 1
2245// 18 G729 A 8,000 1
2246struct StaticPayloadAudioCodec {
2247 const char* name;
2248 int clockrate;
Peter Kasting69558702016-01-12 16:26:35 -08002249 size_t channels;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002250};
2251static const StaticPayloadAudioCodec kStaticPayloadAudioCodecs[] = {
2252 { "PCMU", 8000, 1 },
2253 { "reserved", 0, 0 },
2254 { "reserved", 0, 0 },
2255 { "GSM", 8000, 1 },
2256 { "G723", 8000, 1 },
2257 { "DVI4", 8000, 1 },
2258 { "DVI4", 16000, 1 },
2259 { "LPC", 8000, 1 },
2260 { "PCMA", 8000, 1 },
2261 { "G722", 8000, 1 },
2262 { "L16", 44100, 2 },
2263 { "L16", 44100, 1 },
2264 { "QCELP", 8000, 1 },
2265 { "CN", 8000, 1 },
2266 { "MPA", 90000, 1 },
2267 { "G728", 8000, 1 },
2268 { "DVI4", 11025, 1 },
2269 { "DVI4", 22050, 1 },
2270 { "G729", 8000, 1 },
2271};
2272
2273void MaybeCreateStaticPayloadAudioCodecs(
2274 const std::vector<int>& fmts, AudioContentDescription* media_desc) {
2275 if (!media_desc) {
2276 return;
2277 }
deadbeef67cf2c12016-04-13 10:07:16 -07002278 RTC_DCHECK(media_desc->codecs().empty());
solenberg9fa49752016-10-08 13:02:44 -07002279 for (int payload_type : fmts) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002280 if (!media_desc->HasCodec(payload_type) &&
2281 payload_type >= 0 &&
kjellander3e33bfe2016-06-20 07:04:09 -07002282 static_cast<uint32_t>(payload_type) <
2283 arraysize(kStaticPayloadAudioCodecs)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002284 std::string encoding_name = kStaticPayloadAudioCodecs[payload_type].name;
2285 int clock_rate = kStaticPayloadAudioCodecs[payload_type].clockrate;
Peter Kasting69558702016-01-12 16:26:35 -08002286 size_t channels = kStaticPayloadAudioCodecs[payload_type].channels;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002287 media_desc->AddCodec(cricket::AudioCodec(payload_type, encoding_name,
deadbeef67cf2c12016-04-13 10:07:16 -07002288 clock_rate, 0, channels));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002289 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002290 }
2291}
2292
2293template <class C>
2294static C* ParseContentDescription(const std::string& message,
2295 const MediaType media_type,
2296 int mline_index,
2297 const std::string& protocol,
deadbeef67cf2c12016-04-13 10:07:16 -07002298 const std::vector<int>& payload_types,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002299 size_t* pos,
2300 std::string* content_name,
deadbeef25ed4352016-12-12 18:37:36 -08002301 bool* bundle_only,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002302 TransportDescription* transport,
2303 std::vector<JsepIceCandidate*>* candidates,
2304 webrtc::SdpParseError* error) {
2305 C* media_desc = new C();
2306 switch (media_type) {
2307 case cricket::MEDIA_TYPE_AUDIO:
2308 *content_name = cricket::CN_AUDIO;
2309 break;
2310 case cricket::MEDIA_TYPE_VIDEO:
2311 *content_name = cricket::CN_VIDEO;
2312 break;
2313 case cricket::MEDIA_TYPE_DATA:
2314 *content_name = cricket::CN_DATA;
2315 break;
2316 default:
nissec80e7412017-01-11 05:56:46 -08002317 RTC_NOTREACHED();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002318 break;
2319 }
deadbeef67cf2c12016-04-13 10:07:16 -07002320 if (!ParseContent(message, media_type, mline_index, protocol, payload_types,
deadbeef25ed4352016-12-12 18:37:36 -08002321 pos, content_name, bundle_only, media_desc, transport,
2322 candidates, error)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002323 delete media_desc;
zhihuang38989e52017-03-21 11:04:53 -07002324 return nullptr;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002325 }
2326 // Sort the codecs according to the m-line fmt list.
deadbeef67cf2c12016-04-13 10:07:16 -07002327 std::unordered_map<int, int> payload_type_preferences;
2328 // "size + 1" so that the lowest preference payload type has a preference of
2329 // 1, which is greater than the default (0) for payload types not in the fmt
2330 // list.
2331 int preference = static_cast<int>(payload_types.size() + 1);
2332 for (int pt : payload_types) {
2333 payload_type_preferences[pt] = preference--;
2334 }
2335 std::vector<typename C::CodecType> codecs = media_desc->codecs();
2336 std::sort(codecs.begin(), codecs.end(), [&payload_type_preferences](
2337 const typename C::CodecType& a,
2338 const typename C::CodecType& b) {
2339 return payload_type_preferences[a.id] > payload_type_preferences[b.id];
2340 });
2341 media_desc->set_codecs(codecs);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002342 return media_desc;
2343}
2344
2345bool ParseMediaDescription(const std::string& message,
2346 const TransportDescription& session_td,
2347 const RtpHeaderExtensions& session_extmaps,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002348 size_t* pos,
zhihuang38989e52017-03-21 11:04:53 -07002349 const rtc::SocketAddress& session_connection_addr,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002350 cricket::SessionDescription* desc,
2351 std::vector<JsepIceCandidate*>* candidates,
2352 SdpParseError* error) {
nisseede5da42017-01-12 05:15:36 -08002353 RTC_DCHECK(desc != NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002354 std::string line;
2355 int mline_index = -1;
2356
2357 // Zero or more media descriptions
2358 // RFC 4566
2359 // m=<media> <port> <proto> <fmt>
2360 while (GetLineWithType(message, pos, &line, kLineTypeMedia)) {
2361 ++mline_index;
2362
2363 std::vector<std::string> fields;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002364 rtc::split(line.substr(kLinePrefixLength),
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002365 kSdpDelimiterSpace, &fields);
zstein4b2e0822017-02-17 19:48:38 -08002366
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002367 const size_t expected_min_fields = 4;
2368 if (fields.size() < expected_min_fields) {
2369 return ParseFailedExpectMinFieldNum(line, expected_min_fields, error);
2370 }
deadbeef25ed4352016-12-12 18:37:36 -08002371 bool port_rejected = false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002372 // RFC 3264
2373 // To reject an offered stream, the port number in the corresponding stream
2374 // in the answer MUST be set to zero.
2375 if (fields[1] == kMediaPortRejected) {
deadbeef25ed4352016-12-12 18:37:36 -08002376 port_rejected = true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002377 }
2378
zhihuang38989e52017-03-21 11:04:53 -07002379 int port = 0;
2380 if (!rtc::FromString<int>(fields[1], &port) || !IsValidPort(port)) {
2381 return ParseFailed(line, "The port number is invalid", error);
2382 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002383 std::string protocol = fields[2];
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002384
2385 // <fmt>
deadbeef67cf2c12016-04-13 10:07:16 -07002386 std::vector<int> payload_types;
pthatcher@webrtc.org3341b402015-02-13 21:14:22 +00002387 if (IsRtp(protocol)) {
jiayl@webrtc.orgddb85ab2014-09-05 16:31:56 +00002388 for (size_t j = 3 ; j < fields.size(); ++j) {
2389 // TODO(wu): Remove when below bug is fixed.
2390 // https://bugzilla.mozilla.org/show_bug.cgi?id=996329
pbosbb36fdf2015-07-09 07:48:14 -07002391 if (fields[j].empty() && j == fields.size() - 1) {
jiayl@webrtc.orgddb85ab2014-09-05 16:31:56 +00002392 continue;
2393 }
wu@webrtc.org36eda7c2014-04-15 20:37:30 +00002394
jiayl@webrtc.orgddb85ab2014-09-05 16:31:56 +00002395 int pl = 0;
pkasting@chromium.orge9facf82015-02-17 20:36:28 +00002396 if (!GetPayloadTypeFromString(line, fields[j], &pl, error)) {
jiayl@webrtc.orgddb85ab2014-09-05 16:31:56 +00002397 return false;
2398 }
deadbeef67cf2c12016-04-13 10:07:16 -07002399 payload_types.push_back(pl);
wu@webrtc.org5e760e72014-04-02 23:19:09 +00002400 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002401 }
2402
2403 // Make a temporary TransportDescription based on |session_td|.
2404 // Some of this gets overwritten by ParseContent.
deadbeef46eed762016-01-28 13:24:37 -08002405 TransportDescription transport(
2406 session_td.transport_options, session_td.ice_ufrag, session_td.ice_pwd,
2407 session_td.ice_mode, session_td.connection_role,
2408 session_td.identity_fingerprint.get());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002409
kwibergd1fe2812016-04-27 06:47:29 -07002410 std::unique_ptr<MediaContentDescription> content;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002411 std::string content_name;
deadbeef25ed4352016-12-12 18:37:36 -08002412 bool bundle_only = false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002413 if (HasAttribute(line, kMediaTypeVideo)) {
2414 content.reset(ParseContentDescription<VideoContentDescription>(
deadbeef67cf2c12016-04-13 10:07:16 -07002415 message, cricket::MEDIA_TYPE_VIDEO, mline_index, protocol,
deadbeef25ed4352016-12-12 18:37:36 -08002416 payload_types, pos, &content_name, &bundle_only, &transport,
2417 candidates, error));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002418 } else if (HasAttribute(line, kMediaTypeAudio)) {
2419 content.reset(ParseContentDescription<AudioContentDescription>(
deadbeef67cf2c12016-04-13 10:07:16 -07002420 message, cricket::MEDIA_TYPE_AUDIO, mline_index, protocol,
deadbeef25ed4352016-12-12 18:37:36 -08002421 payload_types, pos, &content_name, &bundle_only, &transport,
2422 candidates, error));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002423 } else if (HasAttribute(line, kMediaTypeData)) {
jiayl@webrtc.org0e527722014-09-08 21:43:43 +00002424 DataContentDescription* data_desc =
wu@webrtc.org78187522013-10-07 23:32:02 +00002425 ParseContentDescription<DataContentDescription>(
deadbeef67cf2c12016-04-13 10:07:16 -07002426 message, cricket::MEDIA_TYPE_DATA, mline_index, protocol,
deadbeef25ed4352016-12-12 18:37:36 -08002427 payload_types, pos, &content_name, &bundle_only, &transport,
2428 candidates, error);
jiayl@webrtc.org0e527722014-09-08 21:43:43 +00002429 content.reset(data_desc);
wu@webrtc.org78187522013-10-07 23:32:02 +00002430
zstein4b2e0822017-02-17 19:48:38 -08002431 if (data_desc && IsDtlsSctp(protocol)) {
2432 int p;
2433 if (rtc::FromString(fields[3], &p)) {
2434 if (!AddSctpDataCodec(data_desc, p)) {
2435 return false;
2436 }
2437 } else if (fields[3] == kDefaultSctpmapProtocol) {
2438 data_desc->set_use_sctpmap(false);
2439 }
wu@webrtc.org78187522013-10-07 23:32:02 +00002440 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002441 } else {
2442 LOG(LS_WARNING) << "Unsupported media type: " << line;
2443 continue;
2444 }
2445 if (!content.get()) {
2446 // ParseContentDescription returns NULL if failed.
2447 return false;
2448 }
2449
deadbeef25ed4352016-12-12 18:37:36 -08002450 bool content_rejected = false;
deadbeef12771a12017-01-03 13:53:47 -08002451 // A port of 0 is not interpreted as a rejected m= section when it's
2452 // used along with a=bundle-only.
deadbeef25ed4352016-12-12 18:37:36 -08002453 if (bundle_only) {
deadbeef25ed4352016-12-12 18:37:36 -08002454 if (!port_rejected) {
deadbeef12771a12017-01-03 13:53:47 -08002455 // Usage of bundle-only with a nonzero port is unspecified. So just
2456 // ignore bundle-only if we see this.
2457 bundle_only = false;
2458 LOG(LS_WARNING)
2459 << "a=bundle-only attribute observed with a nonzero "
2460 << "port; this usage is unspecified so the attribute is being "
2461 << "ignored.";
deadbeef25ed4352016-12-12 18:37:36 -08002462 }
2463 } else {
2464 // If not using bundle-only, interpret port 0 in the normal way; the m=
2465 // section is being rejected.
2466 content_rejected = port_rejected;
2467 }
2468
pthatcher@webrtc.org3341b402015-02-13 21:14:22 +00002469 if (IsRtp(protocol)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002470 // Set the extmap.
2471 if (!session_extmaps.empty() &&
2472 !content->rtp_header_extensions().empty()) {
2473 return ParseFailed("",
2474 "The a=extmap MUST be either all session level or "
2475 "all media level.",
2476 error);
2477 }
2478 for (size_t i = 0; i < session_extmaps.size(); ++i) {
2479 content->AddRtpHeaderExtension(session_extmaps[i]);
2480 }
2481 }
2482 content->set_protocol(protocol);
zhihuang38989e52017-03-21 11:04:53 -07002483
2484 // Use the session level connection address if the media level addresses are
2485 // not specified.
2486 rtc::SocketAddress address;
2487 address = content->connection_address().IsNil()
2488 ? session_connection_addr
2489 : content->connection_address();
2490 address.SetPort(port);
2491 content->set_connection_address(address);
2492
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002493 desc->AddContent(content_name,
deadbeef25ed4352016-12-12 18:37:36 -08002494 IsDtlsSctp(protocol) ? cricket::NS_JINGLE_DRAFT_SCTP
2495 : cricket::NS_JINGLE_RTP,
2496 content_rejected, bundle_only, content.release());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002497 // Create TransportInfo with the media level "ice-pwd" and "ice-ufrag".
2498 TransportInfo transport_info(content_name, transport);
2499
2500 if (!desc->AddTransportInfo(transport_info)) {
2501 std::ostringstream description;
2502 description << "Failed to AddTransportInfo with content name: "
2503 << content_name;
2504 return ParseFailed("", description.str(), error);
2505 }
2506 }
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00002507
2508 size_t end_of_message = message.size();
2509 if (mline_index == -1 && *pos != end_of_message) {
2510 ParseFailed(message, *pos, "Expects m line.", error);
2511 return false;
2512 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002513 return true;
2514}
2515
2516bool VerifyCodec(const cricket::Codec& codec) {
2517 // Codec has not been populated correctly unless the name has been set. This
2518 // can happen if an SDP has an fmtp or rtcp-fb with a payload type but doesn't
2519 // have a corresponding "rtpmap" line.
htab39db842016-12-08 01:50:48 -08002520 return !codec.name.empty();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002521}
2522
2523bool VerifyAudioCodecs(const AudioContentDescription* audio_desc) {
2524 const std::vector<cricket::AudioCodec>& codecs = audio_desc->codecs();
2525 for (std::vector<cricket::AudioCodec>::const_iterator iter = codecs.begin();
2526 iter != codecs.end(); ++iter) {
2527 if (!VerifyCodec(*iter)) {
2528 return false;
2529 }
2530 }
2531 return true;
2532}
2533
2534bool VerifyVideoCodecs(const VideoContentDescription* video_desc) {
2535 const std::vector<cricket::VideoCodec>& codecs = video_desc->codecs();
2536 for (std::vector<cricket::VideoCodec>::const_iterator iter = codecs.begin();
2537 iter != codecs.end(); ++iter) {
2538 if (!VerifyCodec(*iter)) {
2539 return false;
2540 }
2541 }
2542 return true;
2543}
2544
2545void AddParameters(const cricket::CodecParameterMap& parameters,
2546 cricket::Codec* codec) {
2547 for (cricket::CodecParameterMap::const_iterator iter =
2548 parameters.begin(); iter != parameters.end(); ++iter) {
2549 codec->SetParam(iter->first, iter->second);
2550 }
2551}
2552
2553void AddFeedbackParameter(const cricket::FeedbackParam& feedback_param,
2554 cricket::Codec* codec) {
2555 codec->AddFeedbackParam(feedback_param);
2556}
2557
2558void AddFeedbackParameters(const cricket::FeedbackParams& feedback_params,
2559 cricket::Codec* codec) {
2560 for (std::vector<cricket::FeedbackParam>::const_iterator iter =
2561 feedback_params.params().begin();
2562 iter != feedback_params.params().end(); ++iter) {
2563 codec->AddFeedbackParam(*iter);
2564 }
2565}
2566
2567// Gets the current codec setting associated with |payload_type|. If there
changbin.shao@webrtc.org2d25b442015-03-16 04:14:34 +00002568// is no Codec associated with that payload type it returns an empty codec
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002569// with that payload type.
2570template <class T>
changbin.shao@webrtc.org2d25b442015-03-16 04:14:34 +00002571T GetCodecWithPayloadType(const std::vector<T>& codecs, int payload_type) {
magjedb05fa242016-11-11 04:00:16 -08002572 const T* codec = FindCodecById(codecs, payload_type);
2573 if (codec)
2574 return *codec;
2575 // Return empty codec with |payload_type|.
changbin.shao@webrtc.org2d25b442015-03-16 04:14:34 +00002576 T ret_val;
magjedb05fa242016-11-11 04:00:16 -08002577 ret_val.id = payload_type;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002578 return ret_val;
2579}
2580
2581// Updates or creates a new codec entry in the audio description.
2582template <class T, class U>
2583void AddOrReplaceCodec(MediaContentDescription* content_desc, const U& codec) {
2584 T* desc = static_cast<T*>(content_desc);
2585 std::vector<U> codecs = desc->codecs();
2586 bool found = false;
2587
2588 typename std::vector<U>::iterator iter;
2589 for (iter = codecs.begin(); iter != codecs.end(); ++iter) {
2590 if (iter->id == codec.id) {
2591 *iter = codec;
2592 found = true;
2593 break;
2594 }
2595 }
2596 if (!found) {
2597 desc->AddCodec(codec);
2598 return;
2599 }
2600 desc->set_codecs(codecs);
2601}
2602
2603// Adds or updates existing codec corresponding to |payload_type| according
2604// to |parameters|.
2605template <class T, class U>
2606void UpdateCodec(MediaContentDescription* content_desc, int payload_type,
2607 const cricket::CodecParameterMap& parameters) {
2608 // Codec might already have been populated (from rtpmap).
changbin.shao@webrtc.org2d25b442015-03-16 04:14:34 +00002609 U new_codec = GetCodecWithPayloadType(static_cast<T*>(content_desc)->codecs(),
2610 payload_type);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002611 AddParameters(parameters, &new_codec);
2612 AddOrReplaceCodec<T, U>(content_desc, new_codec);
2613}
2614
2615// Adds or updates existing codec corresponding to |payload_type| according
2616// to |feedback_param|.
2617template <class T, class U>
2618void UpdateCodec(MediaContentDescription* content_desc, int payload_type,
2619 const cricket::FeedbackParam& feedback_param) {
2620 // Codec might already have been populated (from rtpmap).
changbin.shao@webrtc.org2d25b442015-03-16 04:14:34 +00002621 U new_codec = GetCodecWithPayloadType(static_cast<T*>(content_desc)->codecs(),
2622 payload_type);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002623 AddFeedbackParameter(feedback_param, &new_codec);
2624 AddOrReplaceCodec<T, U>(content_desc, new_codec);
2625}
2626
jlmiller@webrtc.orga744a282015-02-18 21:37:46 +00002627template <class T>
2628bool PopWildcardCodec(std::vector<T>* codecs, T* wildcard_codec) {
2629 for (auto iter = codecs->begin(); iter != codecs->end(); ++iter) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002630 if (iter->id == kWildcardPayloadType) {
2631 *wildcard_codec = *iter;
2632 codecs->erase(iter);
2633 return true;
2634 }
2635 }
2636 return false;
2637}
2638
jlmiller@webrtc.orga744a282015-02-18 21:37:46 +00002639template<class T>
2640void UpdateFromWildcardCodecs(cricket::MediaContentDescriptionImpl<T>* desc) {
2641 auto codecs = desc->codecs();
2642 T wildcard_codec;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002643 if (!PopWildcardCodec(&codecs, &wildcard_codec)) {
2644 return;
2645 }
jlmiller@webrtc.orga744a282015-02-18 21:37:46 +00002646 for (auto& codec : codecs) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002647 AddFeedbackParameters(wildcard_codec.feedback_params, &codec);
2648 }
jlmiller@webrtc.orga744a282015-02-18 21:37:46 +00002649 desc->set_codecs(codecs);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002650}
2651
2652void AddAudioAttribute(const std::string& name, const std::string& value,
2653 AudioContentDescription* audio_desc) {
2654 if (value.empty()) {
2655 return;
2656 }
2657 std::vector<cricket::AudioCodec> codecs = audio_desc->codecs();
2658 for (std::vector<cricket::AudioCodec>::iterator iter = codecs.begin();
2659 iter != codecs.end(); ++iter) {
2660 iter->params[name] = value;
2661 }
2662 audio_desc->set_codecs(codecs);
2663}
2664
2665bool ParseContent(const std::string& message,
2666 const MediaType media_type,
2667 int mline_index,
2668 const std::string& protocol,
deadbeef67cf2c12016-04-13 10:07:16 -07002669 const std::vector<int>& payload_types,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002670 size_t* pos,
2671 std::string* content_name,
deadbeef25ed4352016-12-12 18:37:36 -08002672 bool* bundle_only,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002673 MediaContentDescription* media_desc,
2674 TransportDescription* transport,
2675 std::vector<JsepIceCandidate*>* candidates,
2676 SdpParseError* error) {
nisseede5da42017-01-12 05:15:36 -08002677 RTC_DCHECK(media_desc != NULL);
2678 RTC_DCHECK(content_name != NULL);
2679 RTC_DCHECK(transport != NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002680
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +00002681 if (media_type == cricket::MEDIA_TYPE_AUDIO) {
2682 MaybeCreateStaticPayloadAudioCodecs(
deadbeef67cf2c12016-04-13 10:07:16 -07002683 payload_types, static_cast<AudioContentDescription*>(media_desc));
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +00002684 }
2685
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002686 // The media level "ice-ufrag" and "ice-pwd".
2687 // The candidates before update the media level "ice-pwd" and "ice-ufrag".
2688 Candidates candidates_orig;
2689 std::string line;
2690 std::string mline_id;
2691 // Tracks created out of the ssrc attributes.
2692 StreamParamsVec tracks;
2693 SsrcInfoVec ssrc_infos;
2694 SsrcGroupVec ssrc_groups;
2695 std::string maxptime_as_string;
2696 std::string ptime_as_string;
deadbeef9d3584c2016-02-16 17:54:10 -08002697 std::string stream_id;
2698 std::string track_id;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002699
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002700 // Loop until the next m line
2701 while (!IsLineType(message, kLineTypeMedia, *pos)) {
2702 if (!GetLine(message, pos, &line)) {
2703 if (*pos >= message.size()) {
2704 break; // Done parsing
2705 } else {
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +00002706 return ParseFailed(message, *pos, "Invalid SDP line.", error);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002707 }
2708 }
2709
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002710 // RFC 4566
2711 // b=* (zero or more bandwidth information lines)
2712 if (IsLineType(line, kLineTypeSessionBandwidth)) {
2713 std::string bandwidth;
2714 if (HasAttribute(line, kApplicationSpecificMaximum)) {
2715 if (!GetValue(line, kApplicationSpecificMaximum, &bandwidth, error)) {
2716 return false;
2717 } else {
wu@webrtc.org5e760e72014-04-02 23:19:09 +00002718 int b = 0;
2719 if (!GetValueFromString(line, bandwidth, &b, error)) {
2720 return false;
2721 }
deadbeefbc88c6b2017-08-02 11:26:34 -07002722 if (b < 0) {
2723 return ParseFailed(line, "b=AS value can't be negative.", error);
2724 }
Peter Thatcherc0c3a862015-06-24 15:31:25 -07002725 // We should never use more than the default bandwidth for RTP-based
2726 // data channels. Don't allow SDP to set the bandwidth, because
2727 // that would give JS the opportunity to "break the Internet".
2728 // See: https://code.google.com/p/chromium/issues/detail?id=280726
2729 if (media_type == cricket::MEDIA_TYPE_DATA && IsRtp(protocol) &&
2730 b > cricket::kDataMaxBandwidth / 1000) {
2731 std::ostringstream description;
2732 description << "RTP-based data channels may not send more than "
2733 << cricket::kDataMaxBandwidth / 1000 << "kbps.";
2734 return ParseFailed(line, description.str(), error);
2735 }
deadbeefb2362572016-12-13 16:37:06 -08002736 // Prevent integer overflow.
2737 b = std::min(b, INT_MAX / 1000);
wu@webrtc.org5e760e72014-04-02 23:19:09 +00002738 media_desc->set_bandwidth(b * 1000);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002739 }
2740 }
2741 continue;
2742 }
2743
zhihuang38989e52017-03-21 11:04:53 -07002744 // Parse the media level connection data.
2745 if (IsLineType(line, kLineTypeConnection)) {
2746 rtc::SocketAddress addr;
2747 if (!ParseConnectionData(line, &addr, error)) {
2748 return false;
2749 }
2750 media_desc->set_connection_address(addr);
2751 continue;
2752 }
2753
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002754 if (!IsLineType(line, kLineTypeAttributes)) {
2755 // TODO: Handle other lines if needed.
2756 LOG(LS_INFO) << "Ignored line: " << line;
2757 continue;
2758 }
2759
2760 // Handle attributes common to SCTP and RTP.
2761 if (HasAttribute(line, kAttributeMid)) {
2762 // RFC 3388
2763 // mid-attribute = "a=mid:" identification-tag
2764 // identification-tag = token
2765 // Use the mid identification-tag as the content name.
2766 if (!GetValue(line, kAttributeMid, &mline_id, error)) {
2767 return false;
2768 }
2769 *content_name = mline_id;
deadbeef25ed4352016-12-12 18:37:36 -08002770 } else if (HasAttribute(line, kAttributeBundleOnly)) {
2771 *bundle_only = true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002772 } else if (HasAttribute(line, kAttributeCandidate)) {
2773 Candidate candidate;
2774 if (!ParseCandidate(line, &candidate, error, false)) {
2775 return false;
2776 }
deadbeef7bcdb692017-01-20 12:43:58 -08002777 // ParseCandidate will parse non-standard ufrag and password attributes,
2778 // since it's used for candidate trickling, but we only want to process
2779 // the "a=ice-ufrag"/"a=ice-pwd" values in a session description, so
2780 // strip them off at this point.
2781 candidate.set_username(std::string());
2782 candidate.set_password(std::string());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002783 candidates_orig.push_back(candidate);
2784 } else if (HasAttribute(line, kAttributeIceUfrag)) {
2785 if (!GetValue(line, kAttributeIceUfrag, &transport->ice_ufrag, error)) {
2786 return false;
2787 }
2788 } else if (HasAttribute(line, kAttributeIcePwd)) {
2789 if (!GetValue(line, kAttributeIcePwd, &transport->ice_pwd, error)) {
2790 return false;
2791 }
2792 } else if (HasAttribute(line, kAttributeIceOption)) {
2793 if (!ParseIceOptions(line, &transport->transport_options, error)) {
2794 return false;
2795 }
2796 } else if (HasAttribute(line, kAttributeFmtp)) {
2797 if (!ParseFmtpAttributes(line, media_type, media_desc, error)) {
2798 return false;
2799 }
2800 } else if (HasAttribute(line, kAttributeFingerprint)) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002801 rtc::SSLFingerprint* fingerprint = NULL;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002802
2803 if (!ParseFingerprintAttribute(line, &fingerprint, error)) {
2804 return false;
2805 }
2806 transport->identity_fingerprint.reset(fingerprint);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00002807 } else if (HasAttribute(line, kAttributeSetup)) {
2808 if (!ParseDtlsSetup(line, &(transport->connection_role), error)) {
2809 return false;
2810 }
pthatcher@webrtc.org3341b402015-02-13 21:14:22 +00002811 } else if (IsDtlsSctp(protocol) && HasAttribute(line, kAttributeSctpPort)) {
deadbeef7e146cb2016-09-28 10:04:34 -07002812 if (media_type != cricket::MEDIA_TYPE_DATA) {
2813 return ParseFailed(
2814 line, "sctp-port attribute found in non-data media description.",
2815 error);
2816 }
jiayl@webrtc.orgddb85ab2014-09-05 16:31:56 +00002817 int sctp_port;
2818 if (!ParseSctpPort(line, &sctp_port, error)) {
2819 return false;
2820 }
2821 if (!AddSctpDataCodec(static_cast<DataContentDescription*>(media_desc),
2822 sctp_port)) {
2823 return false;
2824 }
pthatcher@webrtc.org3341b402015-02-13 21:14:22 +00002825 } else if (IsRtp(protocol)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002826 //
2827 // RTP specific attrubtes
2828 //
2829 if (HasAttribute(line, kAttributeRtcpMux)) {
2830 media_desc->set_rtcp_mux(true);
deadbeef13871492015-12-09 12:37:51 -08002831 } else if (HasAttribute(line, kAttributeRtcpReducedSize)) {
2832 media_desc->set_rtcp_reduced_size(true);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002833 } else if (HasAttribute(line, kAttributeSsrcGroup)) {
2834 if (!ParseSsrcGroupAttribute(line, &ssrc_groups, error)) {
2835 return false;
2836 }
2837 } else if (HasAttribute(line, kAttributeSsrc)) {
2838 if (!ParseSsrcAttribute(line, &ssrc_infos, error)) {
2839 return false;
2840 }
2841 } else if (HasAttribute(line, kAttributeCrypto)) {
2842 if (!ParseCryptoAttribute(line, media_desc, error)) {
2843 return false;
2844 }
2845 } else if (HasAttribute(line, kAttributeRtpmap)) {
deadbeef67cf2c12016-04-13 10:07:16 -07002846 if (!ParseRtpmapAttribute(line, media_type, payload_types, media_desc,
2847 error)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002848 return false;
2849 }
2850 } else if (HasAttribute(line, kCodecParamMaxPTime)) {
2851 if (!GetValue(line, kCodecParamMaxPTime, &maxptime_as_string, error)) {
2852 return false;
2853 }
2854 } else if (HasAttribute(line, kAttributeRtcpFb)) {
2855 if (!ParseRtcpFbAttribute(line, media_type, media_desc, error)) {
2856 return false;
2857 }
2858 } else if (HasAttribute(line, kCodecParamPTime)) {
2859 if (!GetValue(line, kCodecParamPTime, &ptime_as_string, error)) {
2860 return false;
2861 }
2862 } else if (HasAttribute(line, kAttributeSendOnly)) {
2863 media_desc->set_direction(cricket::MD_SENDONLY);
2864 } else if (HasAttribute(line, kAttributeRecvOnly)) {
2865 media_desc->set_direction(cricket::MD_RECVONLY);
2866 } else if (HasAttribute(line, kAttributeInactive)) {
2867 media_desc->set_direction(cricket::MD_INACTIVE);
2868 } else if (HasAttribute(line, kAttributeSendRecv)) {
2869 media_desc->set_direction(cricket::MD_SENDRECV);
2870 } else if (HasAttribute(line, kAttributeExtmap)) {
isheriff6f8d6862016-05-26 11:24:55 -07002871 RtpExtension extmap;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002872 if (!ParseExtmap(line, &extmap, error)) {
2873 return false;
2874 }
2875 media_desc->AddRtpHeaderExtension(extmap);
2876 } else if (HasAttribute(line, kAttributeXGoogleFlag)) {
2877 // Experimental attribute. Conference mode activates more aggressive
2878 // AEC and NS settings.
2879 // TODO: expose API to set these directly.
2880 std::string flag_value;
2881 if (!GetValue(line, kAttributeXGoogleFlag, &flag_value, error)) {
2882 return false;
2883 }
2884 if (flag_value.compare(kValueConference) == 0)
2885 media_desc->set_conference_mode(true);
deadbeef9d3584c2016-02-16 17:54:10 -08002886 } else if (HasAttribute(line, kAttributeMsid)) {
2887 if (!ParseMsidAttribute(line, &stream_id, &track_id, error)) {
2888 return false;
2889 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002890 }
2891 } else {
2892 // Only parse lines that we are interested of.
2893 LOG(LS_INFO) << "Ignored line: " << line;
2894 continue;
2895 }
2896 }
2897
2898 // Create tracks from the |ssrc_infos|.
Taylor Brandstetter5de6b752016-03-09 17:02:30 -08002899 // If the stream_id/track_id for all SSRCS are identical, one StreamParams
2900 // will be created in CreateTracksFromSsrcInfos, containing all the SSRCs from
2901 // the m= section.
2902 CreateTracksFromSsrcInfos(ssrc_infos, stream_id, track_id, &tracks);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002903
2904 // Add the ssrc group to the track.
2905 for (SsrcGroupVec::iterator ssrc_group = ssrc_groups.begin();
2906 ssrc_group != ssrc_groups.end(); ++ssrc_group) {
2907 if (ssrc_group->ssrcs.empty()) {
2908 continue;
2909 }
Peter Boström0c4e06b2015-10-07 12:23:21 +02002910 uint32_t ssrc = ssrc_group->ssrcs.front();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002911 for (StreamParamsVec::iterator track = tracks.begin();
2912 track != tracks.end(); ++track) {
2913 if (track->has_ssrc(ssrc)) {
2914 track->ssrc_groups.push_back(*ssrc_group);
2915 }
2916 }
2917 }
2918
2919 // Add the new tracks to the |media_desc|.
deadbeef9d3584c2016-02-16 17:54:10 -08002920 for (StreamParams& track : tracks) {
2921 media_desc->AddStream(track);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002922 }
2923
2924 if (media_type == cricket::MEDIA_TYPE_AUDIO) {
2925 AudioContentDescription* audio_desc =
2926 static_cast<AudioContentDescription*>(media_desc);
jlmiller@webrtc.orga744a282015-02-18 21:37:46 +00002927 UpdateFromWildcardCodecs(audio_desc);
2928
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002929 // Verify audio codec ensures that no audio codec has been populated with
2930 // only fmtp.
2931 if (!VerifyAudioCodecs(audio_desc)) {
2932 return ParseFailed("Failed to parse audio codecs correctly.", error);
2933 }
2934 AddAudioAttribute(kCodecParamMaxPTime, maxptime_as_string, audio_desc);
2935 AddAudioAttribute(kCodecParamPTime, ptime_as_string, audio_desc);
2936 }
2937
2938 if (media_type == cricket::MEDIA_TYPE_VIDEO) {
jlmiller@webrtc.orga744a282015-02-18 21:37:46 +00002939 VideoContentDescription* video_desc =
2940 static_cast<VideoContentDescription*>(media_desc);
2941 UpdateFromWildcardCodecs(video_desc);
2942 // Verify video codec ensures that no video codec has been populated with
2943 // only rtcp-fb.
2944 if (!VerifyVideoCodecs(video_desc)) {
2945 return ParseFailed("Failed to parse video codecs correctly.", error);
2946 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002947 }
2948
2949 // RFC 5245
2950 // Update the candidates with the media level "ice-pwd" and "ice-ufrag".
2951 for (Candidates::iterator it = candidates_orig.begin();
2952 it != candidates_orig.end(); ++it) {
nisseede5da42017-01-12 05:15:36 -08002953 RTC_DCHECK((*it).username().empty() ||
2954 (*it).username() == transport->ice_ufrag);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002955 (*it).set_username(transport->ice_ufrag);
nisseede5da42017-01-12 05:15:36 -08002956 RTC_DCHECK((*it).password().empty());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002957 (*it).set_password(transport->ice_pwd);
2958 candidates->push_back(
2959 new JsepIceCandidate(mline_id, mline_index, *it));
2960 }
2961 return true;
2962}
2963
2964bool ParseSsrcAttribute(const std::string& line, SsrcInfoVec* ssrc_infos,
2965 SdpParseError* error) {
nisseede5da42017-01-12 05:15:36 -08002966 RTC_DCHECK(ssrc_infos != NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002967 // RFC 5576
2968 // a=ssrc:<ssrc-id> <attribute>
2969 // a=ssrc:<ssrc-id> <attribute>:<value>
2970 std::string field1, field2;
Donald Curtis144d0182015-05-15 13:14:24 -07002971 if (!rtc::tokenize_first(line.substr(kLinePrefixLength), kSdpDelimiterSpace,
2972 &field1, &field2)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002973 const size_t expected_fields = 2;
2974 return ParseFailedExpectFieldNum(line, expected_fields, error);
2975 }
2976
2977 // ssrc:<ssrc-id>
2978 std::string ssrc_id_s;
2979 if (!GetValue(field1, kAttributeSsrc, &ssrc_id_s, error)) {
2980 return false;
2981 }
Peter Boström0c4e06b2015-10-07 12:23:21 +02002982 uint32_t ssrc_id = 0;
wu@webrtc.org5e760e72014-04-02 23:19:09 +00002983 if (!GetValueFromString(line, ssrc_id_s, &ssrc_id, error)) {
2984 return false;
2985 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002986
2987 std::string attribute;
2988 std::string value;
Donald Curtis144d0182015-05-15 13:14:24 -07002989 if (!rtc::tokenize_first(field2, kSdpDelimiterColon, &attribute, &value)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002990 std::ostringstream description;
2991 description << "Failed to get the ssrc attribute value from " << field2
2992 << ". Expected format <attribute>:<value>.";
2993 return ParseFailed(line, description.str(), error);
2994 }
2995
2996 // Check if there's already an item for this |ssrc_id|. Create a new one if
2997 // there isn't.
2998 SsrcInfoVec::iterator ssrc_info = ssrc_infos->begin();
2999 for (; ssrc_info != ssrc_infos->end(); ++ssrc_info) {
3000 if (ssrc_info->ssrc_id == ssrc_id) {
3001 break;
3002 }
3003 }
3004 if (ssrc_info == ssrc_infos->end()) {
3005 SsrcInfo info;
3006 info.ssrc_id = ssrc_id;
3007 ssrc_infos->push_back(info);
3008 ssrc_info = ssrc_infos->end() - 1;
3009 }
3010
3011 // Store the info to the |ssrc_info|.
3012 if (attribute == kSsrcAttributeCname) {
3013 // RFC 5576
3014 // cname:<value>
3015 ssrc_info->cname = value;
3016 } else if (attribute == kSsrcAttributeMsid) {
3017 // draft-alvestrand-mmusic-msid-00
3018 // "msid:" identifier [ " " appdata ]
3019 std::vector<std::string> fields;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00003020 rtc::split(value, kSdpDelimiterSpace, &fields);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003021 if (fields.size() < 1 || fields.size() > 2) {
3022 return ParseFailed(line,
3023 "Expected format \"msid:<identifier>[ <appdata>]\".",
3024 error);
3025 }
deadbeef9d3584c2016-02-16 17:54:10 -08003026 ssrc_info->stream_id = fields[0];
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003027 if (fields.size() == 2) {
deadbeef9d3584c2016-02-16 17:54:10 -08003028 ssrc_info->track_id = fields[1];
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003029 }
3030 } else if (attribute == kSsrcAttributeMslabel) {
3031 // draft-alvestrand-rtcweb-mid-01
3032 // mslabel:<value>
3033 ssrc_info->mslabel = value;
3034 } else if (attribute == kSSrcAttributeLabel) {
3035 // The label isn't defined.
3036 // label:<value>
3037 ssrc_info->label = value;
3038 }
3039 return true;
3040}
3041
3042bool ParseSsrcGroupAttribute(const std::string& line,
3043 SsrcGroupVec* ssrc_groups,
3044 SdpParseError* error) {
nisseede5da42017-01-12 05:15:36 -08003045 RTC_DCHECK(ssrc_groups != NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003046 // RFC 5576
3047 // a=ssrc-group:<semantics> <ssrc-id> ...
3048 std::vector<std::string> fields;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00003049 rtc::split(line.substr(kLinePrefixLength),
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003050 kSdpDelimiterSpace, &fields);
3051 const size_t expected_min_fields = 2;
3052 if (fields.size() < expected_min_fields) {
3053 return ParseFailedExpectMinFieldNum(line, expected_min_fields, error);
3054 }
3055 std::string semantics;
3056 if (!GetValue(fields[0], kAttributeSsrcGroup, &semantics, error)) {
3057 return false;
3058 }
Peter Boström0c4e06b2015-10-07 12:23:21 +02003059 std::vector<uint32_t> ssrcs;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003060 for (size_t i = 1; i < fields.size(); ++i) {
Peter Boström0c4e06b2015-10-07 12:23:21 +02003061 uint32_t ssrc = 0;
wu@webrtc.org5e760e72014-04-02 23:19:09 +00003062 if (!GetValueFromString(line, fields[i], &ssrc, error)) {
3063 return false;
3064 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003065 ssrcs.push_back(ssrc);
3066 }
3067 ssrc_groups->push_back(SsrcGroup(semantics, ssrcs));
3068 return true;
3069}
3070
3071bool ParseCryptoAttribute(const std::string& line,
3072 MediaContentDescription* media_desc,
3073 SdpParseError* error) {
3074 std::vector<std::string> fields;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00003075 rtc::split(line.substr(kLinePrefixLength),
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003076 kSdpDelimiterSpace, &fields);
3077 // RFC 4568
3078 // a=crypto:<tag> <crypto-suite> <key-params> [<session-params>]
3079 const size_t expected_min_fields = 3;
3080 if (fields.size() < expected_min_fields) {
3081 return ParseFailedExpectMinFieldNum(line, expected_min_fields, error);
3082 }
3083 std::string tag_value;
3084 if (!GetValue(fields[0], kAttributeCrypto, &tag_value, error)) {
3085 return false;
3086 }
wu@webrtc.org5e760e72014-04-02 23:19:09 +00003087 int tag = 0;
3088 if (!GetValueFromString(line, tag_value, &tag, error)) {
3089 return false;
3090 }
jbauch083b73f2015-07-16 02:46:32 -07003091 const std::string& crypto_suite = fields[1];
3092 const std::string& key_params = fields[2];
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003093 std::string session_params;
3094 if (fields.size() > 3) {
3095 session_params = fields[3];
3096 }
3097 media_desc->AddCrypto(CryptoParams(tag, crypto_suite, key_params,
3098 session_params));
3099 return true;
3100}
3101
3102// Updates or creates a new codec entry in the audio description with according
deadbeef67cf2c12016-04-13 10:07:16 -07003103// to |name|, |clockrate|, |bitrate|, and |channels|.
3104void UpdateCodec(int payload_type,
3105 const std::string& name,
3106 int clockrate,
3107 int bitrate,
3108 size_t channels,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003109 AudioContentDescription* audio_desc) {
3110 // Codec may already be populated with (only) optional parameters
3111 // (from an fmtp).
changbin.shao@webrtc.org2d25b442015-03-16 04:14:34 +00003112 cricket::AudioCodec codec =
3113 GetCodecWithPayloadType(audio_desc->codecs(), payload_type);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003114 codec.name = name;
3115 codec.clockrate = clockrate;
3116 codec.bitrate = bitrate;
3117 codec.channels = channels;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003118 AddOrReplaceCodec<AudioContentDescription, cricket::AudioCodec>(audio_desc,
3119 codec);
3120}
3121
3122// Updates or creates a new codec entry in the video description according to
deadbeef67cf2c12016-04-13 10:07:16 -07003123// |name|, |width|, |height|, and |framerate|.
3124void UpdateCodec(int payload_type,
3125 const std::string& name,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003126 VideoContentDescription* video_desc) {
3127 // Codec may already be populated with (only) optional parameters
3128 // (from an fmtp).
changbin.shao@webrtc.org2d25b442015-03-16 04:14:34 +00003129 cricket::VideoCodec codec =
3130 GetCodecWithPayloadType(video_desc->codecs(), payload_type);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003131 codec.name = name;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003132 AddOrReplaceCodec<VideoContentDescription, cricket::VideoCodec>(video_desc,
3133 codec);
3134}
3135
3136bool ParseRtpmapAttribute(const std::string& line,
3137 const MediaType media_type,
deadbeef67cf2c12016-04-13 10:07:16 -07003138 const std::vector<int>& payload_types,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003139 MediaContentDescription* media_desc,
3140 SdpParseError* error) {
3141 std::vector<std::string> fields;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00003142 rtc::split(line.substr(kLinePrefixLength),
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003143 kSdpDelimiterSpace, &fields);
3144 // RFC 4566
3145 // a=rtpmap:<payload type> <encoding name>/<clock rate>[/<encodingparameters>]
3146 const size_t expected_min_fields = 2;
3147 if (fields.size() < expected_min_fields) {
3148 return ParseFailedExpectMinFieldNum(line, expected_min_fields, error);
3149 }
3150 std::string payload_type_value;
3151 if (!GetValue(fields[0], kAttributeRtpmap, &payload_type_value, error)) {
3152 return false;
3153 }
wu@webrtc.org5e760e72014-04-02 23:19:09 +00003154 int payload_type = 0;
pkasting@chromium.orge9facf82015-02-17 20:36:28 +00003155 if (!GetPayloadTypeFromString(line, payload_type_value, &payload_type,
3156 error)) {
wu@webrtc.org5e760e72014-04-02 23:19:09 +00003157 return false;
3158 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003159
deadbeef67cf2c12016-04-13 10:07:16 -07003160 if (std::find(payload_types.begin(), payload_types.end(), payload_type) ==
3161 payload_types.end()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003162 LOG(LS_WARNING) << "Ignore rtpmap line that did not appear in the "
3163 << "<fmt> of the m-line: " << line;
3164 return true;
3165 }
jbauch083b73f2015-07-16 02:46:32 -07003166 const std::string& encoder = fields[1];
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003167 std::vector<std::string> codec_params;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00003168 rtc::split(encoder, '/', &codec_params);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003169 // <encoding name>/<clock rate>[/<encodingparameters>]
3170 // 2 mandatory fields
3171 if (codec_params.size() < 2 || codec_params.size() > 3) {
3172 return ParseFailed(line,
3173 "Expected format \"<encoding name>/<clock rate>"
3174 "[/<encodingparameters>]\".",
3175 error);
3176 }
jbauch083b73f2015-07-16 02:46:32 -07003177 const std::string& encoding_name = codec_params[0];
wu@webrtc.org5e760e72014-04-02 23:19:09 +00003178 int clock_rate = 0;
3179 if (!GetValueFromString(line, codec_params[1], &clock_rate, error)) {
3180 return false;
3181 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003182 if (media_type == cricket::MEDIA_TYPE_VIDEO) {
3183 VideoContentDescription* video_desc =
3184 static_cast<VideoContentDescription*>(media_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003185 UpdateCodec(payload_type, encoding_name,
deadbeef67cf2c12016-04-13 10:07:16 -07003186 video_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003187 } else if (media_type == cricket::MEDIA_TYPE_AUDIO) {
3188 // RFC 4566
3189 // For audio streams, <encoding parameters> indicates the number
3190 // of audio channels. This parameter is OPTIONAL and may be
3191 // omitted if the number of channels is one, provided that no
3192 // additional parameters are needed.
Peter Kasting69558702016-01-12 16:26:35 -08003193 size_t channels = 1;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003194 if (codec_params.size() == 3) {
wu@webrtc.org5e760e72014-04-02 23:19:09 +00003195 if (!GetValueFromString(line, codec_params[2], &channels, error)) {
3196 return false;
3197 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003198 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003199 AudioContentDescription* audio_desc =
3200 static_cast<AudioContentDescription*>(media_desc);
ossue1405ad2017-01-23 08:55:48 -08003201 UpdateCodec(payload_type, encoding_name, clock_rate, 0, channels,
deadbeef67cf2c12016-04-13 10:07:16 -07003202 audio_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003203 } else if (media_type == cricket::MEDIA_TYPE_DATA) {
3204 DataContentDescription* data_desc =
3205 static_cast<DataContentDescription*>(media_desc);
deadbeef67cf2c12016-04-13 10:07:16 -07003206 data_desc->AddCodec(cricket::DataCodec(payload_type, encoding_name));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003207 }
3208 return true;
3209}
3210
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003211bool ParseFmtpParam(const std::string& line, std::string* parameter,
3212 std::string* value, SdpParseError* error) {
Donald Curtis0e07f922015-05-15 09:21:23 -07003213 if (!rtc::tokenize_first(line, kSdpDelimiterEqual, parameter, value)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003214 ParseFailed(line, "Unable to parse fmtp parameter. \'=\' missing.", error);
3215 return false;
3216 }
3217 // a=fmtp:<payload_type> <param1>=<value1>; <param2>=<value2>; ...
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003218 return true;
3219}
3220
3221bool ParseFmtpAttributes(const std::string& line, const MediaType media_type,
3222 MediaContentDescription* media_desc,
3223 SdpParseError* error) {
3224 if (media_type != cricket::MEDIA_TYPE_AUDIO &&
3225 media_type != cricket::MEDIA_TYPE_VIDEO) {
3226 return true;
3227 }
Donald Curtis0e07f922015-05-15 09:21:23 -07003228
3229 std::string line_payload;
3230 std::string line_params;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003231
3232 // RFC 5576
3233 // a=fmtp:<format> <format specific parameters>
3234 // At least two fields, whereas the second one is any of the optional
3235 // parameters.
Donald Curtis144d0182015-05-15 13:14:24 -07003236 if (!rtc::tokenize_first(line.substr(kLinePrefixLength), kSdpDelimiterSpace,
3237 &line_payload, &line_params)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003238 ParseFailedExpectMinFieldNum(line, 2, error);
3239 return false;
3240 }
3241
Donald Curtis0e07f922015-05-15 09:21:23 -07003242 // Parse out the payload information.
pkasting@chromium.orgd3245462015-02-23 21:28:22 +00003243 std::string payload_type_str;
Donald Curtis0e07f922015-05-15 09:21:23 -07003244 if (!GetValue(line_payload, kAttributeFmtp, &payload_type_str, error)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003245 return false;
3246 }
3247
Donald Curtis0e07f922015-05-15 09:21:23 -07003248 int payload_type = 0;
Donald Curtis144d0182015-05-15 13:14:24 -07003249 if (!GetPayloadTypeFromString(line_payload, payload_type_str, &payload_type,
3250 error)) {
Donald Curtis0e07f922015-05-15 09:21:23 -07003251 return false;
3252 }
3253
3254 // Parse out format specific parameters.
3255 std::vector<std::string> fields;
3256 rtc::split(line_params, kSdpDelimiterSemicolon, &fields);
3257
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003258 cricket::CodecParameterMap codec_params;
Donald Curtis144d0182015-05-15 13:14:24 -07003259 for (auto& iter : fields) {
Donald Curtis0e07f922015-05-15 09:21:23 -07003260 if (iter.find(kSdpDelimiterEqual) == std::string::npos) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003261 // Only fmtps with equals are currently supported. Other fmtp types
3262 // should be ignored. Unknown fmtps do not constitute an error.
3263 continue;
3264 }
Donald Curtis0e07f922015-05-15 09:21:23 -07003265
3266 std::string name;
3267 std::string value;
3268 if (!ParseFmtpParam(rtc::string_trim(iter), &name, &value, error)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003269 return false;
3270 }
3271 codec_params[name] = value;
3272 }
3273
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003274 if (media_type == cricket::MEDIA_TYPE_AUDIO) {
3275 UpdateCodec<AudioContentDescription, cricket::AudioCodec>(
pkasting@chromium.orgd3245462015-02-23 21:28:22 +00003276 media_desc, payload_type, codec_params);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003277 } else if (media_type == cricket::MEDIA_TYPE_VIDEO) {
3278 UpdateCodec<VideoContentDescription, cricket::VideoCodec>(
pkasting@chromium.orgd3245462015-02-23 21:28:22 +00003279 media_desc, payload_type, codec_params);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003280 }
3281 return true;
3282}
3283
3284bool ParseRtcpFbAttribute(const std::string& line, const MediaType media_type,
3285 MediaContentDescription* media_desc,
3286 SdpParseError* error) {
3287 if (media_type != cricket::MEDIA_TYPE_AUDIO &&
3288 media_type != cricket::MEDIA_TYPE_VIDEO) {
3289 return true;
3290 }
3291 std::vector<std::string> rtcp_fb_fields;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00003292 rtc::split(line.c_str(), kSdpDelimiterSpace, &rtcp_fb_fields);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003293 if (rtcp_fb_fields.size() < 2) {
3294 return ParseFailedGetValue(line, kAttributeRtcpFb, error);
3295 }
3296 std::string payload_type_string;
3297 if (!GetValue(rtcp_fb_fields[0], kAttributeRtcpFb, &payload_type_string,
3298 error)) {
3299 return false;
3300 }
wu@webrtc.org5e760e72014-04-02 23:19:09 +00003301 int payload_type = kWildcardPayloadType;
3302 if (payload_type_string != "*") {
pkasting@chromium.orge9facf82015-02-17 20:36:28 +00003303 if (!GetPayloadTypeFromString(line, payload_type_string, &payload_type,
3304 error)) {
wu@webrtc.org5e760e72014-04-02 23:19:09 +00003305 return false;
3306 }
3307 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003308 std::string id = rtcp_fb_fields[1];
3309 std::string param = "";
3310 for (std::vector<std::string>::iterator iter = rtcp_fb_fields.begin() + 2;
3311 iter != rtcp_fb_fields.end(); ++iter) {
3312 param.append(*iter);
3313 }
3314 const cricket::FeedbackParam feedback_param(id, param);
3315
3316 if (media_type == cricket::MEDIA_TYPE_AUDIO) {
pkasting@chromium.orgd3245462015-02-23 21:28:22 +00003317 UpdateCodec<AudioContentDescription, cricket::AudioCodec>(
3318 media_desc, payload_type, feedback_param);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003319 } else if (media_type == cricket::MEDIA_TYPE_VIDEO) {
pkasting@chromium.orgd3245462015-02-23 21:28:22 +00003320 UpdateCodec<VideoContentDescription, cricket::VideoCodec>(
3321 media_desc, payload_type, feedback_param);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003322 }
3323 return true;
3324}
3325
3326} // namespace webrtc