blob: 49cd0444bf10db984f597340b706118e64319360 [file] [log] [blame]
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001/*
kjellanderb24317b2016-02-10 07:54:43 -08002 * Copyright 2011 The WebRTC project authors. All Rights Reserved.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003 *
kjellanderb24317b2016-02-10 07:54:43 -08004 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00009 */
10
ossu7bb87ee2017-01-23 04:56:25 -080011#include "webrtc/pc/webrtcsdp.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000012
kjellandera96e2d72016-02-04 23:52:28 -080013#include <ctype.h>
henrike@webrtc.org28e20752013-07-10 00:45:36 +000014#include <limits.h>
15#include <stdio.h>
kwibergd1fe2812016-04-27 06:47:29 -070016
henrike@webrtc.org28e20752013-07-10 00:45:36 +000017#include <algorithm>
kwibergd1fe2812016-04-27 06:47:29 -070018#include <memory>
henrike@webrtc.org28e20752013-07-10 00:45:36 +000019#include <string>
deadbeef67cf2c12016-04-13 10:07:16 -070020#include <unordered_map>
henrike@webrtc.org28e20752013-07-10 00:45:36 +000021#include <vector>
22
Henrik Kjellander15583c12016-02-10 10:53:12 +010023#include "webrtc/api/jsepicecandidate.h"
24#include "webrtc/api/jsepsessiondescription.h"
tfarina5237aaf2015-11-10 23:44:30 -080025#include "webrtc/base/arraysize.h"
nissec80e7412017-01-11 05:56:46 -080026#include "webrtc/base/checks.h"
buildbot@webrtc.orga09a9992014-08-13 17:26:08 +000027#include "webrtc/base/logging.h"
28#include "webrtc/base/messagedigest.h"
29#include "webrtc/base/stringutils.h"
isheriff6f8d6862016-05-26 11:24:55 -070030// for RtpExtension
31#include "webrtc/config.h"
kjellandera96e2d72016-02-04 23:52:28 -080032#include "webrtc/media/base/codec.h"
kjellandera96e2d72016-02-04 23:52:28 -080033#include "webrtc/media/base/cryptoparams.h"
kjellanderf4752772016-03-02 05:42:30 -080034#include "webrtc/media/base/mediaconstants.h"
kjellandera96e2d72016-02-04 23:52:28 -080035#include "webrtc/media/base/rtputils.h"
deadbeef953c2ce2017-01-09 14:53:41 -080036#include "webrtc/media/sctp/sctptransportinternal.h"
kjellandera96e2d72016-02-04 23:52:28 -080037#include "webrtc/p2p/base/candidate.h"
kjellanderf4752772016-03-02 05:42:30 -080038#include "webrtc/p2p/base/p2pconstants.h"
kjellandera96e2d72016-02-04 23:52:28 -080039#include "webrtc/p2p/base/port.h"
kjellander@webrtc.org9b8df252016-02-12 06:47:59 +010040#include "webrtc/pc/mediasession.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000041
42using cricket::AudioContentDescription;
43using cricket::Candidate;
44using cricket::Candidates;
45using cricket::ContentDescription;
46using cricket::ContentInfo;
47using cricket::CryptoParams;
48using cricket::DataContentDescription;
49using cricket::ICE_CANDIDATE_COMPONENT_RTP;
50using cricket::ICE_CANDIDATE_COMPONENT_RTCP;
51using cricket::kCodecParamMaxBitrate;
52using cricket::kCodecParamMaxPTime;
53using cricket::kCodecParamMaxQuantization;
54using cricket::kCodecParamMinBitrate;
55using cricket::kCodecParamMinPTime;
56using cricket::kCodecParamPTime;
57using cricket::kCodecParamSPropStereo;
buildbot@webrtc.orged97bb02014-05-07 11:15:20 +000058using cricket::kCodecParamStartBitrate;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000059using cricket::kCodecParamStereo;
60using cricket::kCodecParamUseInbandFec;
Minyue Li7100dcd2015-03-27 05:05:59 +010061using cricket::kCodecParamUseDtx;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000062using cricket::kCodecParamSctpProtocol;
63using cricket::kCodecParamSctpStreams;
henrike@webrtc.org1e09a712013-07-26 19:17:59 +000064using cricket::kCodecParamMaxAverageBitrate;
buildbot@webrtc.org5d639b32014-09-10 07:57:12 +000065using cricket::kCodecParamMaxPlaybackRate;
stefan@webrtc.org85d27942014-06-09 12:51:39 +000066using cricket::kCodecParamAssociatedPayloadType;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000067using cricket::MediaContentDescription;
68using cricket::MediaType;
isheriff6f8d6862016-05-26 11:24:55 -070069using cricket::RtpHeaderExtensions;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000070using cricket::SsrcGroup;
71using cricket::StreamParams;
72using cricket::StreamParamsVec;
73using cricket::TransportDescription;
74using cricket::TransportInfo;
75using cricket::VideoContentDescription;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000076using rtc::SocketAddress;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000077
henrike@webrtc.org28e20752013-07-10 00:45:36 +000078namespace cricket {
79class SessionDescription;
80}
81
deadbeef90f1e1e2017-02-10 12:35:05 -080082// TODO(deadbeef): Switch to using anonymous namespace rather than declaring
83// everything "static".
henrike@webrtc.org28e20752013-07-10 00:45:36 +000084namespace webrtc {
85
86// Line type
87// RFC 4566
88// An SDP session description consists of a number of lines of text of
89// the form:
90// <type>=<value>
91// where <type> MUST be exactly one case-significant character.
deadbeef9d3584c2016-02-16 17:54:10 -080092static const int kLinePrefixLength = 2; // Length of <type>=
henrike@webrtc.org28e20752013-07-10 00:45:36 +000093static const char kLineTypeVersion = 'v';
94static const char kLineTypeOrigin = 'o';
95static const char kLineTypeSessionName = 's';
96static const char kLineTypeSessionInfo = 'i';
97static const char kLineTypeSessionUri = 'u';
98static const char kLineTypeSessionEmail = 'e';
99static const char kLineTypeSessionPhone = 'p';
100static const char kLineTypeSessionBandwidth = 'b';
101static const char kLineTypeTiming = 't';
102static const char kLineTypeRepeatTimes = 'r';
103static const char kLineTypeTimeZone = 'z';
104static const char kLineTypeEncryptionKey = 'k';
105static const char kLineTypeMedia = 'm';
106static const char kLineTypeConnection = 'c';
107static const char kLineTypeAttributes = 'a';
108
109// Attributes
110static const char kAttributeGroup[] = "group";
111static const char kAttributeMid[] = "mid";
deadbeef9d3584c2016-02-16 17:54:10 -0800112static const char kAttributeMsid[] = "msid";
deadbeef25ed4352016-12-12 18:37:36 -0800113static const char kAttributeBundleOnly[] = "bundle-only";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000114static const char kAttributeRtcpMux[] = "rtcp-mux";
deadbeef13871492015-12-09 12:37:51 -0800115static const char kAttributeRtcpReducedSize[] = "rtcp-rsize";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000116static const char kAttributeSsrc[] = "ssrc";
117static const char kSsrcAttributeCname[] = "cname";
118static const char kAttributeExtmap[] = "extmap";
119// draft-alvestrand-mmusic-msid-01
120// a=msid-semantic: WMS
121static const char kAttributeMsidSemantics[] = "msid-semantic";
122static const char kMediaStreamSemantic[] = "WMS";
123static const char kSsrcAttributeMsid[] = "msid";
124static const char kDefaultMsid[] = "default";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000125static const char kSsrcAttributeMslabel[] = "mslabel";
126static const char kSSrcAttributeLabel[] = "label";
127static const char kAttributeSsrcGroup[] = "ssrc-group";
128static const char kAttributeCrypto[] = "crypto";
129static const char kAttributeCandidate[] = "candidate";
130static const char kAttributeCandidateTyp[] = "typ";
131static const char kAttributeCandidateRaddr[] = "raddr";
132static const char kAttributeCandidateRport[] = "rport";
honghaiza54a0802015-12-16 18:37:23 -0800133static const char kAttributeCandidateUfrag[] = "ufrag";
134static const char kAttributeCandidatePwd[] = "pwd";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000135static const char kAttributeCandidateGeneration[] = "generation";
honghaiza0c44ea2016-03-23 16:07:48 -0700136static const char kAttributeCandidateNetworkId[] = "network-id";
honghaize1a0c942016-02-16 14:54:56 -0800137static const char kAttributeCandidateNetworkCost[] = "network-cost";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000138static const char kAttributeFingerprint[] = "fingerprint";
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +0000139static const char kAttributeSetup[] = "setup";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000140static const char kAttributeFmtp[] = "fmtp";
141static const char kAttributeRtpmap[] = "rtpmap";
wu@webrtc.org78187522013-10-07 23:32:02 +0000142static const char kAttributeSctpmap[] = "sctpmap";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000143static const char kAttributeRtcp[] = "rtcp";
144static const char kAttributeIceUfrag[] = "ice-ufrag";
145static const char kAttributeIcePwd[] = "ice-pwd";
146static const char kAttributeIceLite[] = "ice-lite";
147static const char kAttributeIceOption[] = "ice-options";
148static const char kAttributeSendOnly[] = "sendonly";
149static const char kAttributeRecvOnly[] = "recvonly";
150static const char kAttributeRtcpFb[] = "rtcp-fb";
151static const char kAttributeSendRecv[] = "sendrecv";
152static const char kAttributeInactive[] = "inactive";
jiayl@webrtc.orgddb85ab2014-09-05 16:31:56 +0000153// draft-ietf-mmusic-sctp-sdp-07
154// a=sctp-port
155static const char kAttributeSctpPort[] = "sctp-port";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000156
157// Experimental flags
158static const char kAttributeXGoogleFlag[] = "x-google-flag";
159static const char kValueConference[] = "conference";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000160
161// Candidate
162static const char kCandidateHost[] = "host";
163static const char kCandidateSrflx[] = "srflx";
Honghai Zhang7fb69db2016-03-14 11:59:18 -0700164static const char kCandidatePrflx[] = "prflx";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000165static const char kCandidateRelay[] = "relay";
mallinath@webrtc.org2d60c5e2014-08-08 22:29:20 +0000166static const char kTcpCandidateType[] = "tcptype";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000167
168static const char kSdpDelimiterEqual = '=';
169static const char kSdpDelimiterSpace = ' ';
170static const char kSdpDelimiterColon = ':';
171static const char kSdpDelimiterSemicolon = ';';
172static const char kSdpDelimiterSlash = '/';
173static const char kNewLine = '\n';
174static const char kReturn = '\r';
175static const char kLineBreak[] = "\r\n";
176
177// TODO: Generate the Session and Time description
178// instead of hardcoding.
179static const char kSessionVersion[] = "v=0";
180// RFC 4566
181static const char kSessionOriginUsername[] = "-";
182static const char kSessionOriginSessionId[] = "0";
183static const char kSessionOriginSessionVersion[] = "0";
184static const char kSessionOriginNettype[] = "IN";
185static const char kSessionOriginAddrtype[] = "IP4";
186static const char kSessionOriginAddress[] = "127.0.0.1";
187static const char kSessionName[] = "s=-";
188static const char kTimeDescription[] = "t=0 0";
189static const char kAttrGroup[] = "a=group:BUNDLE";
190static const char kConnectionNettype[] = "IN";
pthatcher@webrtc.orgc9d6d142014-10-23 23:37:22 +0000191static const char kConnectionIpv4Addrtype[] = "IP4";
192static const char kConnectionIpv6Addrtype[] = "IP6";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000193static const char kMediaTypeVideo[] = "video";
194static const char kMediaTypeAudio[] = "audio";
195static const char kMediaTypeData[] = "application";
196static const char kMediaPortRejected[] = "0";
pthatcher@webrtc.orgc9d6d142014-10-23 23:37:22 +0000197// draft-ietf-mmusic-trickle-ice-01
198// When no candidates have been gathered, set the connection
199// address to IP6 ::.
perkj@webrtc.orgd105cc82014-11-07 11:22:06 +0000200// TODO(perkj): FF can not parse IP6 ::. See http://crbug/430333
201// Use IPV4 per default.
202static const char kDummyAddress[] = "0.0.0.0";
pthatcher@webrtc.orgc9d6d142014-10-23 23:37:22 +0000203static const char kDummyPort[] = "9";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000204// RFC 3556
205static const char kApplicationSpecificMaximum[] = "AS";
206
wu@webrtc.org78187522013-10-07 23:32:02 +0000207static const char kDefaultSctpmapProtocol[] = "webrtc-datachannel";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000208
pkasting@chromium.orgd3245462015-02-23 21:28:22 +0000209// RTP payload type is in the 0-127 range. Use -1 to indicate "all" payload
210// types.
211const int kWildcardPayloadType = -1;
212
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000213struct SsrcInfo {
Peter Boström0c4e06b2015-10-07 12:23:21 +0200214 uint32_t ssrc_id;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000215 std::string cname;
deadbeef9d3584c2016-02-16 17:54:10 -0800216 std::string stream_id;
217 std::string track_id;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000218
219 // For backward compatibility.
220 // TODO(ronghuawu): Remove below 2 fields once all the clients support msid.
221 std::string label;
222 std::string mslabel;
223};
224typedef std::vector<SsrcInfo> SsrcInfoVec;
225typedef std::vector<SsrcGroup> SsrcGroupVec;
226
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000227template <class T>
228static void AddFmtpLine(const T& codec, std::string* message);
229static void BuildMediaDescription(const ContentInfo* content_info,
230 const TransportInfo* transport_info,
231 const MediaType media_type,
wu@webrtc.org4c3e9912014-07-16 21:03:13 +0000232 const std::vector<Candidate>& candidates,
deadbeef9d3584c2016-02-16 17:54:10 -0800233 bool unified_plan_sdp,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000234 std::string* message);
zstein4b2e0822017-02-17 19:48:38 -0800235static void BuildSctpContentAttributes(std::string* message,
236 int sctp_port,
237 bool use_sctpmap);
deadbeef9d3584c2016-02-16 17:54:10 -0800238static void BuildRtpContentAttributes(const MediaContentDescription* media_desc,
239 const MediaType media_type,
240 bool unified_plan_sdp,
241 std::string* message);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000242static void BuildRtpMap(const MediaContentDescription* media_desc,
243 const MediaType media_type,
244 std::string* message);
245static void BuildCandidate(const std::vector<Candidate>& candidates,
honghaiza54a0802015-12-16 18:37:23 -0800246 bool include_ufrag,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000247 std::string* message);
248static void BuildIceOptions(const std::vector<std::string>& transport_options,
249 std::string* message);
pthatcher@webrtc.org3341b402015-02-13 21:14:22 +0000250static bool IsRtp(const std::string& protocol);
251static bool IsDtlsSctp(const std::string& protocol);
zhihuang38989e52017-03-21 11:04:53 -0700252static bool ParseSessionDescription(const std::string& message,
253 size_t* pos,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000254 std::string* session_id,
255 std::string* session_version,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000256 TransportDescription* session_td,
257 RtpHeaderExtensions* session_extmaps,
zhihuang38989e52017-03-21 11:04:53 -0700258 rtc::SocketAddress* connection_addr,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000259 cricket::SessionDescription* desc,
260 SdpParseError* error);
261static bool ParseGroupAttribute(const std::string& line,
262 cricket::SessionDescription* desc,
263 SdpParseError* error);
264static bool ParseMediaDescription(
265 const std::string& message,
266 const TransportDescription& session_td,
267 const RtpHeaderExtensions& session_extmaps,
zhihuang38989e52017-03-21 11:04:53 -0700268 size_t* pos,
269 const rtc::SocketAddress& session_connection_addr,
270 cricket::SessionDescription* desc,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000271 std::vector<JsepIceCandidate*>* candidates,
272 SdpParseError* error);
273static bool ParseContent(const std::string& message,
274 const MediaType media_type,
275 int mline_index,
276 const std::string& protocol,
deadbeef67cf2c12016-04-13 10:07:16 -0700277 const std::vector<int>& payload_types,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000278 size_t* pos,
279 std::string* content_name,
deadbeef25ed4352016-12-12 18:37:36 -0800280 bool* bundle_only,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000281 MediaContentDescription* media_desc,
282 TransportDescription* transport,
283 std::vector<JsepIceCandidate*>* candidates,
284 SdpParseError* error);
285static bool ParseSsrcAttribute(const std::string& line,
286 SsrcInfoVec* ssrc_infos,
287 SdpParseError* error);
288static bool ParseSsrcGroupAttribute(const std::string& line,
289 SsrcGroupVec* ssrc_groups,
290 SdpParseError* error);
291static bool ParseCryptoAttribute(const std::string& line,
292 MediaContentDescription* media_desc,
293 SdpParseError* error);
294static bool ParseRtpmapAttribute(const std::string& line,
295 const MediaType media_type,
deadbeef67cf2c12016-04-13 10:07:16 -0700296 const std::vector<int>& payload_types,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000297 MediaContentDescription* media_desc,
298 SdpParseError* error);
299static bool ParseFmtpAttributes(const std::string& line,
300 const MediaType media_type,
301 MediaContentDescription* media_desc,
302 SdpParseError* error);
303static bool ParseFmtpParam(const std::string& line, std::string* parameter,
304 std::string* value, SdpParseError* error);
305static bool ParseCandidate(const std::string& message, Candidate* candidate,
306 SdpParseError* error, bool is_raw);
307static bool ParseRtcpFbAttribute(const std::string& line,
308 const MediaType media_type,
309 MediaContentDescription* media_desc,
310 SdpParseError* error);
311static bool ParseIceOptions(const std::string& line,
312 std::vector<std::string>* transport_options,
313 SdpParseError* error);
314static bool ParseExtmap(const std::string& line,
isheriff6f8d6862016-05-26 11:24:55 -0700315 RtpExtension* extmap,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000316 SdpParseError* error);
317static bool ParseFingerprintAttribute(const std::string& line,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000318 rtc::SSLFingerprint** fingerprint,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000319 SdpParseError* error);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +0000320static bool ParseDtlsSetup(const std::string& line,
321 cricket::ConnectionRole* role,
322 SdpParseError* error);
deadbeef9d3584c2016-02-16 17:54:10 -0800323static bool ParseMsidAttribute(const std::string& line,
324 std::string* stream_id,
325 std::string* track_id,
326 SdpParseError* error);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000327
328// Helper functions
329
330// Below ParseFailed*** functions output the line that caused the parsing
331// failure and the detailed reason (|description|) of the failure to |error|.
332// The functions always return false so that they can be used directly in the
333// following way when error happens:
334// "return ParseFailed***(...);"
335
336// The line starting at |line_start| of |message| is the failing line.
337// The reason for the failure should be provided in the |description|.
338// An example of a description could be "unknown character".
339static bool ParseFailed(const std::string& message,
340 size_t line_start,
341 const std::string& description,
342 SdpParseError* error) {
343 // Get the first line of |message| from |line_start|.
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +0000344 std::string first_line;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000345 size_t line_end = message.find(kNewLine, line_start);
346 if (line_end != std::string::npos) {
347 if (line_end > 0 && (message.at(line_end - 1) == kReturn)) {
348 --line_end;
349 }
350 first_line = message.substr(line_start, (line_end - line_start));
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +0000351 } else {
352 first_line = message.substr(line_start);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000353 }
354
355 if (error) {
356 error->line = first_line;
357 error->description = description;
358 }
359 LOG(LS_ERROR) << "Failed to parse: \"" << first_line
360 << "\". Reason: " << description;
361 return false;
362}
363
364// |line| is the failing line. The reason for the failure should be
365// provided in the |description|.
366static bool ParseFailed(const std::string& line,
367 const std::string& description,
368 SdpParseError* error) {
369 return ParseFailed(line, 0, description, error);
370}
371
372// Parses failure where the failing SDP line isn't know or there are multiple
373// failing lines.
374static bool ParseFailed(const std::string& description,
375 SdpParseError* error) {
376 return ParseFailed("", description, error);
377}
378
379// |line| is the failing line. The failure is due to the fact that |line|
380// doesn't have |expected_fields| fields.
381static bool ParseFailedExpectFieldNum(const std::string& line,
382 int expected_fields,
383 SdpParseError* error) {
384 std::ostringstream description;
385 description << "Expects " << expected_fields << " fields.";
386 return ParseFailed(line, description.str(), error);
387}
388
389// |line| is the failing line. The failure is due to the fact that |line| has
390// less than |expected_min_fields| fields.
391static bool ParseFailedExpectMinFieldNum(const std::string& line,
392 int expected_min_fields,
393 SdpParseError* error) {
394 std::ostringstream description;
395 description << "Expects at least " << expected_min_fields << " fields.";
396 return ParseFailed(line, description.str(), error);
397}
398
399// |line| is the failing line. The failure is due to the fact that it failed to
400// get the value of |attribute|.
401static bool ParseFailedGetValue(const std::string& line,
402 const std::string& attribute,
403 SdpParseError* error) {
404 std::ostringstream description;
405 description << "Failed to get the value of attribute: " << attribute;
406 return ParseFailed(line, description.str(), error);
407}
408
409// The line starting at |line_start| of |message| is the failing line. The
410// failure is due to the line type (e.g. the "m" part of the "m-line")
411// not matching what is expected. The expected line type should be
412// provided as |line_type|.
413static bool ParseFailedExpectLine(const std::string& message,
414 size_t line_start,
415 const char line_type,
416 const std::string& line_value,
417 SdpParseError* error) {
418 std::ostringstream description;
419 description << "Expect line: " << line_type << "=" << line_value;
420 return ParseFailed(message, line_start, description.str(), error);
421}
422
423static bool AddLine(const std::string& line, std::string* message) {
424 if (!message)
425 return false;
426
427 message->append(line);
428 message->append(kLineBreak);
429 return true;
430}
431
432static bool GetLine(const std::string& message,
433 size_t* pos,
434 std::string* line) {
435 size_t line_begin = *pos;
436 size_t line_end = message.find(kNewLine, line_begin);
437 if (line_end == std::string::npos) {
438 return false;
439 }
440 // Update the new start position
441 *pos = line_end + 1;
442 if (line_end > 0 && (message.at(line_end - 1) == kReturn)) {
443 --line_end;
444 }
445 *line = message.substr(line_begin, (line_end - line_begin));
446 const char* cline = line->c_str();
447 // RFC 4566
448 // An SDP session description consists of a number of lines of text of
449 // the form:
450 // <type>=<value>
451 // where <type> MUST be exactly one case-significant character and
452 // <value> is structured text whose format depends on <type>.
453 // Whitespace MUST NOT be used on either side of the "=" sign.
decurtis@webrtc.org8af11042015-01-07 19:15:51 +0000454 if (line->length() < 3 ||
455 !islower(cline[0]) ||
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000456 cline[1] != kSdpDelimiterEqual ||
457 cline[2] == kSdpDelimiterSpace) {
458 *pos = line_begin;
459 return false;
460 }
461 return true;
462}
463
464// Init |os| to "|type|=|value|".
465static void InitLine(const char type,
466 const std::string& value,
467 std::ostringstream* os) {
468 os->str("");
469 *os << type << kSdpDelimiterEqual << value;
470}
471
472// Init |os| to "a=|attribute|".
473static void InitAttrLine(const std::string& attribute, std::ostringstream* os) {
474 InitLine(kLineTypeAttributes, attribute, os);
475}
476
477// Writes a SDP attribute line based on |attribute| and |value| to |message|.
478static void AddAttributeLine(const std::string& attribute, int value,
479 std::string* message) {
480 std::ostringstream os;
481 InitAttrLine(attribute, &os);
482 os << kSdpDelimiterColon << value;
483 AddLine(os.str(), message);
484}
485
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000486static bool IsLineType(const std::string& message,
487 const char type,
488 size_t line_start) {
489 if (message.size() < line_start + kLinePrefixLength) {
490 return false;
491 }
492 const char* cmessage = message.c_str();
493 return (cmessage[line_start] == type &&
494 cmessage[line_start + 1] == kSdpDelimiterEqual);
495}
496
497static bool IsLineType(const std::string& line,
498 const char type) {
499 return IsLineType(line, type, 0);
500}
501
502static bool GetLineWithType(const std::string& message, size_t* pos,
503 std::string* line, const char type) {
504 if (!IsLineType(message, type, *pos)) {
505 return false;
506 }
507
508 if (!GetLine(message, pos, line))
509 return false;
510
511 return true;
512}
513
514static bool HasAttribute(const std::string& line,
515 const std::string& attribute) {
516 return (line.compare(kLinePrefixLength, attribute.size(), attribute) == 0);
517}
518
Peter Boström0c4e06b2015-10-07 12:23:21 +0200519static bool AddSsrcLine(uint32_t ssrc_id,
520 const std::string& attribute,
521 const std::string& value,
522 std::string* message) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000523 // RFC 5576
524 // a=ssrc:<ssrc-id> <attribute>:<value>
525 std::ostringstream os;
526 InitAttrLine(kAttributeSsrc, &os);
527 os << kSdpDelimiterColon << ssrc_id << kSdpDelimiterSpace
528 << attribute << kSdpDelimiterColon << value;
529 return AddLine(os.str(), message);
530}
531
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000532// Get value only from <attribute>:<value>.
533static bool GetValue(const std::string& message, const std::string& attribute,
534 std::string* value, SdpParseError* error) {
535 std::string leftpart;
Donald Curtis0e07f922015-05-15 09:21:23 -0700536 if (!rtc::tokenize_first(message, kSdpDelimiterColon, &leftpart, value)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000537 return ParseFailedGetValue(message, attribute, error);
538 }
539 // The left part should end with the expected attribute.
540 if (leftpart.length() < attribute.length() ||
541 leftpart.compare(leftpart.length() - attribute.length(),
542 attribute.length(), attribute) != 0) {
543 return ParseFailedGetValue(message, attribute, error);
544 }
545 return true;
546}
547
548static bool CaseInsensitiveFind(std::string str1, std::string str2) {
549 std::transform(str1.begin(), str1.end(), str1.begin(),
550 ::tolower);
551 std::transform(str2.begin(), str2.end(), str2.begin(),
552 ::tolower);
553 return str1.find(str2) != std::string::npos;
554}
555
wu@webrtc.org5e760e72014-04-02 23:19:09 +0000556template <class T>
557static bool GetValueFromString(const std::string& line,
558 const std::string& s,
559 T* t,
560 SdpParseError* error) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000561 if (!rtc::FromString(s, t)) {
wu@webrtc.org5e760e72014-04-02 23:19:09 +0000562 std::ostringstream description;
563 description << "Invalid value: " << s << ".";
564 return ParseFailed(line, description.str(), error);
565 }
566 return true;
567}
568
pkasting@chromium.orge9facf82015-02-17 20:36:28 +0000569static bool GetPayloadTypeFromString(const std::string& line,
570 const std::string& s,
571 int* payload_type,
572 SdpParseError* error) {
573 return GetValueFromString(line, s, payload_type, error) &&
574 cricket::IsValidRtpPayloadType(*payload_type);
575}
576
Taylor Brandstetter5de6b752016-03-09 17:02:30 -0800577// |msid_stream_id| and |msid_track_id| represent the stream/track ID from the
578// "a=msid" attribute, if it exists. They are empty if the attribute does not
579// exist.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000580void CreateTracksFromSsrcInfos(const SsrcInfoVec& ssrc_infos,
Taylor Brandstetter5de6b752016-03-09 17:02:30 -0800581 const std::string& msid_stream_id,
582 const std::string& msid_track_id,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000583 StreamParamsVec* tracks) {
nisseede5da42017-01-12 05:15:36 -0800584 RTC_DCHECK(tracks != NULL);
585 RTC_DCHECK(msid_stream_id.empty() == msid_track_id.empty());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000586 for (SsrcInfoVec::const_iterator ssrc_info = ssrc_infos.begin();
587 ssrc_info != ssrc_infos.end(); ++ssrc_info) {
588 if (ssrc_info->cname.empty()) {
589 continue;
590 }
591
Taylor Brandstetter5de6b752016-03-09 17:02:30 -0800592 std::string stream_id;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000593 std::string track_id;
Taylor Brandstetter5de6b752016-03-09 17:02:30 -0800594 if (ssrc_info->stream_id.empty() && !ssrc_info->mslabel.empty()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000595 // If there's no msid and there's mslabel, we consider this is a sdp from
596 // a older version of client that doesn't support msid.
597 // In that case, we use the mslabel and label to construct the track.
Taylor Brandstetter5de6b752016-03-09 17:02:30 -0800598 stream_id = ssrc_info->mslabel;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000599 track_id = ssrc_info->label;
Taylor Brandstetter5de6b752016-03-09 17:02:30 -0800600 } else if (ssrc_info->stream_id.empty() && !msid_stream_id.empty()) {
601 // If there's no msid in the SSRC attributes, but there's a global one
602 // (from a=msid), use that. This is the case with unified plan SDP.
603 stream_id = msid_stream_id;
604 track_id = msid_track_id;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000605 } else {
Taylor Brandstetter5de6b752016-03-09 17:02:30 -0800606 stream_id = ssrc_info->stream_id;
deadbeef9d3584c2016-02-16 17:54:10 -0800607 track_id = ssrc_info->track_id;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000608 }
Taylor Brandstetter5de6b752016-03-09 17:02:30 -0800609 // If a stream/track ID wasn't populated from the SSRC attributes OR the
610 // msid attribute, use default/random values.
611 if (stream_id.empty()) {
612 stream_id = kDefaultMsid;
613 }
614 if (track_id.empty()) {
615 // TODO(ronghuawu): What should we do if the track id doesn't appear?
616 // Create random string (which will be used as track label later)?
617 track_id = rtc::CreateRandomString(8);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000618 }
619
620 StreamParamsVec::iterator track = tracks->begin();
621 for (; track != tracks->end(); ++track) {
622 if (track->id == track_id) {
623 break;
624 }
625 }
626 if (track == tracks->end()) {
627 // If we don't find an existing track, create a new one.
628 tracks->push_back(StreamParams());
629 track = tracks->end() - 1;
630 }
631 track->add_ssrc(ssrc_info->ssrc_id);
632 track->cname = ssrc_info->cname;
Taylor Brandstetter5de6b752016-03-09 17:02:30 -0800633 track->sync_label = stream_id;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000634 track->id = track_id;
635 }
636}
637
638void GetMediaStreamLabels(const ContentInfo* content,
639 std::set<std::string>* labels) {
640 const MediaContentDescription* media_desc =
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000641 static_cast<const MediaContentDescription*>(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000642 content->description);
643 const cricket::StreamParamsVec& streams = media_desc->streams();
644 for (cricket::StreamParamsVec::const_iterator it = streams.begin();
645 it != streams.end(); ++it) {
646 labels->insert(it->sync_label);
647 }
648}
649
650// RFC 5245
651// It is RECOMMENDED that default candidates be chosen based on the
652// likelihood of those candidates to work with the peer that is being
653// contacted. It is RECOMMENDED that relayed > reflexive > host.
654static const int kPreferenceUnknown = 0;
655static const int kPreferenceHost = 1;
656static const int kPreferenceReflexive = 2;
657static const int kPreferenceRelayed = 3;
658
659static int GetCandidatePreferenceFromType(const std::string& type) {
660 int preference = kPreferenceUnknown;
661 if (type == cricket::LOCAL_PORT_TYPE) {
662 preference = kPreferenceHost;
663 } else if (type == cricket::STUN_PORT_TYPE) {
664 preference = kPreferenceReflexive;
665 } else if (type == cricket::RELAY_PORT_TYPE) {
666 preference = kPreferenceRelayed;
667 } else {
nissec80e7412017-01-11 05:56:46 -0800668 RTC_NOTREACHED();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000669 }
670 return preference;
671}
672
guoweis@webrtc.org57ac2c82015-02-06 00:45:13 +0000673// Get ip and port of the default destination from the |candidates| with the
674// given value of |component_id|. The default candidate should be the one most
675// likely to work, typically IPv4 relay.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000676// RFC 5245
677// The value of |component_id| currently supported are 1 (RTP) and 2 (RTCP).
678// TODO: Decide the default destination in webrtcsession and
679// pass it down via SessionDescription.
pthatcher@webrtc.orgc9d6d142014-10-23 23:37:22 +0000680static void GetDefaultDestination(
681 const std::vector<Candidate>& candidates,
682 int component_id, std::string* port,
683 std::string* ip, std::string* addr_type) {
perkj@webrtc.orgd105cc82014-11-07 11:22:06 +0000684 *addr_type = kConnectionIpv4Addrtype;
pthatcher@webrtc.orgc9d6d142014-10-23 23:37:22 +0000685 *port = kDummyPort;
686 *ip = kDummyAddress;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000687 int current_preference = kPreferenceUnknown;
guoweis@webrtc.org57ac2c82015-02-06 00:45:13 +0000688 int current_family = AF_UNSPEC;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000689 for (std::vector<Candidate>::const_iterator it = candidates.begin();
690 it != candidates.end(); ++it) {
691 if (it->component() != component_id) {
692 continue;
693 }
guoweis@webrtc.org57ac2c82015-02-06 00:45:13 +0000694 // Default destination should be UDP only.
695 if (it->protocol() != cricket::UDP_PROTOCOL_NAME) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000696 continue;
697 }
guoweis@webrtc.org57ac2c82015-02-06 00:45:13 +0000698 const int preference = GetCandidatePreferenceFromType(it->type());
699 const int family = it->address().ipaddr().family();
700 // See if this candidate is more preferable then the current one if it's the
701 // same family. Or if the current family is IPv4 already so we could safely
702 // ignore all IPv6 ones. WebRTC bug 4269.
703 // http://code.google.com/p/webrtc/issues/detail?id=4269
704 if ((preference <= current_preference && current_family == family) ||
705 (current_family == AF_INET && family == AF_INET6)) {
706 continue;
707 }
pthatcher@webrtc.orgc9d6d142014-10-23 23:37:22 +0000708 if (family == AF_INET) {
709 addr_type->assign(kConnectionIpv4Addrtype);
710 } else if (family == AF_INET6) {
711 addr_type->assign(kConnectionIpv6Addrtype);
712 }
guoweis@webrtc.org57ac2c82015-02-06 00:45:13 +0000713 current_preference = preference;
714 current_family = family;
715 *port = it->address().PortAsString();
716 *ip = it->address().ipaddr().ToString();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000717 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000718}
719
wu@webrtc.org4c3e9912014-07-16 21:03:13 +0000720// Gets "a=rtcp" line if found default RTCP candidate from |candidates|.
721static std::string GetRtcpLine(const std::vector<Candidate>& candidates) {
pthatcher@webrtc.orgc9d6d142014-10-23 23:37:22 +0000722 std::string rtcp_line, rtcp_port, rtcp_ip, addr_type;
723 GetDefaultDestination(candidates, ICE_CANDIDATE_COMPONENT_RTCP,
724 &rtcp_port, &rtcp_ip, &addr_type);
725 // Found default RTCP candidate.
726 // RFC 5245
727 // If the agent is utilizing RTCP, it MUST encode the RTCP candidate
728 // using the a=rtcp attribute as defined in RFC 3605.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000729
pthatcher@webrtc.orgc9d6d142014-10-23 23:37:22 +0000730 // RFC 3605
731 // rtcp-attribute = "a=rtcp:" port [nettype space addrtype space
732 // connection-address] CRLF
733 std::ostringstream os;
734 InitAttrLine(kAttributeRtcp, &os);
735 os << kSdpDelimiterColon
736 << rtcp_port << " "
737 << kConnectionNettype << " "
738 << addr_type << " "
739 << rtcp_ip;
740 rtcp_line = os.str();
wu@webrtc.org4c3e9912014-07-16 21:03:13 +0000741 return rtcp_line;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000742}
743
744// Get candidates according to the mline index from SessionDescriptionInterface.
745static void GetCandidatesByMindex(const SessionDescriptionInterface& desci,
746 int mline_index,
747 std::vector<Candidate>* candidates) {
748 if (!candidates) {
749 return;
750 }
751 const IceCandidateCollection* cc = desci.candidates(mline_index);
752 for (size_t i = 0; i < cc->count(); ++i) {
753 const IceCandidateInterface* candidate = cc->at(i);
754 candidates->push_back(candidate->candidate());
755 }
756}
757
deadbeef90f1e1e2017-02-10 12:35:05 -0800758static bool IsValidPort(int port) {
759 return port >= 0 && port <= 65535;
760}
761
deadbeef9d3584c2016-02-16 17:54:10 -0800762std::string SdpSerialize(const JsepSessionDescription& jdesc,
763 bool unified_plan_sdp) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000764 const cricket::SessionDescription* desc = jdesc.description();
765 if (!desc) {
766 return "";
767 }
768
769 std::string message;
770
771 // Session Description.
772 AddLine(kSessionVersion, &message);
773 // Session Origin
774 // RFC 4566
775 // o=<username> <sess-id> <sess-version> <nettype> <addrtype>
776 // <unicast-address>
777 std::ostringstream os;
778 InitLine(kLineTypeOrigin, kSessionOriginUsername, &os);
jbauch083b73f2015-07-16 02:46:32 -0700779 const std::string& session_id = jdesc.session_id().empty() ?
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000780 kSessionOriginSessionId : jdesc.session_id();
jbauch083b73f2015-07-16 02:46:32 -0700781 const std::string& session_version = jdesc.session_version().empty() ?
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000782 kSessionOriginSessionVersion : jdesc.session_version();
783 os << " " << session_id << " " << session_version << " "
784 << kSessionOriginNettype << " " << kSessionOriginAddrtype << " "
785 << kSessionOriginAddress;
786 AddLine(os.str(), &message);
787 AddLine(kSessionName, &message);
788
789 // Time Description.
790 AddLine(kTimeDescription, &message);
791
792 // Group
793 if (desc->HasGroup(cricket::GROUP_TYPE_BUNDLE)) {
794 std::string group_line = kAttrGroup;
795 const cricket::ContentGroup* group =
796 desc->GetGroupByName(cricket::GROUP_TYPE_BUNDLE);
nisseede5da42017-01-12 05:15:36 -0800797 RTC_DCHECK(group != NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000798 const cricket::ContentNames& content_names = group->content_names();
799 for (cricket::ContentNames::const_iterator it = content_names.begin();
800 it != content_names.end(); ++it) {
801 group_line.append(" ");
802 group_line.append(*it);
803 }
804 AddLine(group_line, &message);
805 }
806
807 // MediaStream semantics
808 InitAttrLine(kAttributeMsidSemantics, &os);
809 os << kSdpDelimiterColon << " " << kMediaStreamSemantic;
wu@webrtc.org4c3e9912014-07-16 21:03:13 +0000810
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000811 std::set<std::string> media_stream_labels;
812 const ContentInfo* audio_content = GetFirstAudioContent(desc);
813 if (audio_content)
814 GetMediaStreamLabels(audio_content, &media_stream_labels);
wu@webrtc.org4c3e9912014-07-16 21:03:13 +0000815
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000816 const ContentInfo* video_content = GetFirstVideoContent(desc);
817 if (video_content)
818 GetMediaStreamLabels(video_content, &media_stream_labels);
wu@webrtc.org4c3e9912014-07-16 21:03:13 +0000819
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000820 for (std::set<std::string>::const_iterator it =
821 media_stream_labels.begin(); it != media_stream_labels.end(); ++it) {
822 os << " " << *it;
823 }
824 AddLine(os.str(), &message);
825
wu@webrtc.org4c3e9912014-07-16 21:03:13 +0000826 // Preserve the order of the media contents.
827 int mline_index = -1;
828 for (cricket::ContentInfos::const_iterator it = desc->contents().begin();
829 it != desc->contents().end(); ++it) {
830 const MediaContentDescription* mdesc =
831 static_cast<const MediaContentDescription*>(it->description);
832 std::vector<Candidate> candidates;
833 GetCandidatesByMindex(jdesc, ++mline_index, &candidates);
deadbeef9d3584c2016-02-16 17:54:10 -0800834 BuildMediaDescription(&*it, desc->GetTransportInfoByName(it->name),
835 mdesc->type(), candidates, unified_plan_sdp,
wu@webrtc.org4c3e9912014-07-16 21:03:13 +0000836 &message);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000837 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000838 return message;
839}
840
841// Serializes the passed in IceCandidateInterface to a SDP string.
842// candidate - The candidate to be serialized.
Honghai Zhang7fb69db2016-03-14 11:59:18 -0700843std::string SdpSerializeCandidate(const IceCandidateInterface& candidate) {
844 return SdpSerializeCandidate(candidate.candidate());
845}
846
847// Serializes a cricket Candidate.
848std::string SdpSerializeCandidate(const cricket::Candidate& candidate) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000849 std::string message;
Honghai Zhang7fb69db2016-03-14 11:59:18 -0700850 std::vector<cricket::Candidate> candidates(1, candidate);
honghaiza54a0802015-12-16 18:37:23 -0800851 BuildCandidate(candidates, true, &message);
wu@webrtc.orgec9f5fb2014-06-24 17:05:10 +0000852 // From WebRTC draft section 4.8.1.1 candidate-attribute will be
853 // just candidate:<candidate> not a=candidate:<blah>CRLF
nisseede5da42017-01-12 05:15:36 -0800854 RTC_DCHECK(message.find("a=") == 0);
wu@webrtc.orgec9f5fb2014-06-24 17:05:10 +0000855 message.erase(0, 2);
nisseede5da42017-01-12 05:15:36 -0800856 RTC_DCHECK(message.find(kLineBreak) == message.size() - 2);
wu@webrtc.orgec9f5fb2014-06-24 17:05:10 +0000857 message.resize(message.size() - 2);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000858 return message;
859}
860
861bool SdpDeserialize(const std::string& message,
862 JsepSessionDescription* jdesc,
863 SdpParseError* error) {
864 std::string session_id;
865 std::string session_version;
Peter Thatcher7cbd1882015-09-17 18:54:52 -0700866 TransportDescription session_td("", "");
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000867 RtpHeaderExtensions session_extmaps;
zhihuang38989e52017-03-21 11:04:53 -0700868 rtc::SocketAddress session_connection_addr;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000869 cricket::SessionDescription* desc = new cricket::SessionDescription();
870 std::vector<JsepIceCandidate*> candidates;
871 size_t current_pos = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000872
873 // Session Description
874 if (!ParseSessionDescription(message, &current_pos, &session_id,
deadbeefc80741f2015-10-22 13:14:45 -0700875 &session_version, &session_td, &session_extmaps,
zhihuang38989e52017-03-21 11:04:53 -0700876 &session_connection_addr, desc, error)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000877 delete desc;
878 return false;
879 }
880
881 // Media Description
deadbeefc80741f2015-10-22 13:14:45 -0700882 if (!ParseMediaDescription(message, session_td, session_extmaps, &current_pos,
zhihuang38989e52017-03-21 11:04:53 -0700883 session_connection_addr, desc, &candidates,
884 error)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000885 delete desc;
886 for (std::vector<JsepIceCandidate*>::const_iterator
887 it = candidates.begin(); it != candidates.end(); ++it) {
888 delete *it;
889 }
890 return false;
891 }
892
893 jdesc->Initialize(desc, session_id, session_version);
894
895 for (std::vector<JsepIceCandidate*>::const_iterator
896 it = candidates.begin(); it != candidates.end(); ++it) {
897 jdesc->AddCandidate(*it);
898 delete *it;
899 }
900 return true;
901}
902
903bool SdpDeserializeCandidate(const std::string& message,
904 JsepIceCandidate* jcandidate,
905 SdpParseError* error) {
nisseede5da42017-01-12 05:15:36 -0800906 RTC_DCHECK(jcandidate != NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000907 Candidate candidate;
908 if (!ParseCandidate(message, &candidate, error, true)) {
909 return false;
910 }
911 jcandidate->SetCandidate(candidate);
912 return true;
913}
914
Honghai Zhang7fb69db2016-03-14 11:59:18 -0700915bool SdpDeserializeCandidate(const std::string& transport_name,
916 const std::string& message,
917 cricket::Candidate* candidate,
918 SdpParseError* error) {
nisseede5da42017-01-12 05:15:36 -0800919 RTC_DCHECK(candidate != nullptr);
Honghai Zhang7fb69db2016-03-14 11:59:18 -0700920 if (!ParseCandidate(message, candidate, error, true)) {
921 return false;
922 }
923 candidate->set_transport_name(transport_name);
924 return true;
925}
926
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000927bool ParseCandidate(const std::string& message, Candidate* candidate,
928 SdpParseError* error, bool is_raw) {
nisseede5da42017-01-12 05:15:36 -0800929 RTC_DCHECK(candidate != NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000930
931 // Get the first line from |message|.
jiayl@webrtc.org7ec3f9f2014-08-08 23:09:15 +0000932 std::string first_line = message;
933 size_t pos = 0;
934 GetLine(message, &pos, &first_line);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000935
jiayl@webrtc.org7ec3f9f2014-08-08 23:09:15 +0000936 // Makes sure |message| contains only one line.
937 if (message.size() > first_line.size()) {
938 std::string left, right;
Donald Curtis0e07f922015-05-15 09:21:23 -0700939 if (rtc::tokenize_first(message, kNewLine, &left, &right) &&
940 !right.empty()) {
jiayl@webrtc.org7ec3f9f2014-08-08 23:09:15 +0000941 return ParseFailed(message, 0, "Expect one line only", error);
942 }
943 }
944
945 // From WebRTC draft section 4.8.1.1 candidate-attribute should be
946 // candidate:<candidate> when trickled, but we still support
947 // a=candidate:<blah>CRLF for backward compatibility and for parsing a line
948 // from the SDP.
949 if (IsLineType(first_line, kLineTypeAttributes)) {
950 first_line = first_line.substr(kLinePrefixLength);
951 }
952
953 std::string attribute_candidate;
954 std::string candidate_value;
955
956 // |first_line| must be in the form of "candidate:<value>".
Donald Curtis144d0182015-05-15 13:14:24 -0700957 if (!rtc::tokenize_first(first_line, kSdpDelimiterColon, &attribute_candidate,
958 &candidate_value) ||
jiayl@webrtc.org7ec3f9f2014-08-08 23:09:15 +0000959 attribute_candidate != kAttributeCandidate) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000960 if (is_raw) {
961 std::ostringstream description;
jiayl@webrtc.org7ec3f9f2014-08-08 23:09:15 +0000962 description << "Expect line: " << kAttributeCandidate
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000963 << ":" << "<candidate-str>";
964 return ParseFailed(first_line, 0, description.str(), error);
965 } else {
966 return ParseFailedExpectLine(first_line, 0, kLineTypeAttributes,
967 kAttributeCandidate, error);
968 }
969 }
970
971 std::vector<std::string> fields;
jiayl@webrtc.org7ec3f9f2014-08-08 23:09:15 +0000972 rtc::split(candidate_value, kSdpDelimiterSpace, &fields);
973
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000974 // RFC 5245
975 // a=candidate:<foundation> <component-id> <transport> <priority>
976 // <connection-address> <port> typ <candidate-types>
977 // [raddr <connection-address>] [rport <port>]
978 // *(SP extension-att-name SP extension-att-value)
979 const size_t expected_min_fields = 8;
980 if (fields.size() < expected_min_fields ||
981 (fields[6] != kAttributeCandidateTyp)) {
982 return ParseFailedExpectMinFieldNum(first_line, expected_min_fields, error);
983 }
jbauch083b73f2015-07-16 02:46:32 -0700984 const std::string& foundation = fields[0];
jiayl@webrtc.org7ec3f9f2014-08-08 23:09:15 +0000985
wu@webrtc.org5e760e72014-04-02 23:19:09 +0000986 int component_id = 0;
987 if (!GetValueFromString(first_line, fields[1], &component_id, error)) {
988 return false;
989 }
jbauch083b73f2015-07-16 02:46:32 -0700990 const std::string& transport = fields[2];
Peter Boström0c4e06b2015-10-07 12:23:21 +0200991 uint32_t priority = 0;
wu@webrtc.org5e760e72014-04-02 23:19:09 +0000992 if (!GetValueFromString(first_line, fields[3], &priority, error)) {
993 return false;
994 }
jbauch083b73f2015-07-16 02:46:32 -0700995 const std::string& connection_address = fields[4];
wu@webrtc.org5e760e72014-04-02 23:19:09 +0000996 int port = 0;
997 if (!GetValueFromString(first_line, fields[5], &port, error)) {
998 return false;
999 }
deadbeef90f1e1e2017-02-10 12:35:05 -08001000 if (!IsValidPort(port)) {
1001 return ParseFailed(first_line, "Invalid port number.", error);
1002 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001003 SocketAddress address(connection_address, port);
1004
1005 cricket::ProtocolType protocol;
hnslb68cc752016-12-13 10:33:41 -08001006 if (!StringToProto(transport.c_str(), &protocol)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001007 return ParseFailed(first_line, "Unsupported transport type.", error);
1008 }
hnslb68cc752016-12-13 10:33:41 -08001009 switch (protocol) {
1010 case cricket::PROTO_UDP:
1011 case cricket::PROTO_TCP:
1012 case cricket::PROTO_SSLTCP:
1013 // Supported protocol.
1014 break;
1015 default:
1016 return ParseFailed(first_line, "Unsupported transport type.", error);
1017 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001018
1019 std::string candidate_type;
jbauch083b73f2015-07-16 02:46:32 -07001020 const std::string& type = fields[7];
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001021 if (type == kCandidateHost) {
1022 candidate_type = cricket::LOCAL_PORT_TYPE;
1023 } else if (type == kCandidateSrflx) {
1024 candidate_type = cricket::STUN_PORT_TYPE;
1025 } else if (type == kCandidateRelay) {
1026 candidate_type = cricket::RELAY_PORT_TYPE;
Honghai Zhang7fb69db2016-03-14 11:59:18 -07001027 } else if (type == kCandidatePrflx) {
1028 candidate_type = cricket::PRFLX_PORT_TYPE;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001029 } else {
1030 return ParseFailed(first_line, "Unsupported candidate type.", error);
1031 }
1032
1033 size_t current_position = expected_min_fields;
1034 SocketAddress related_address;
1035 // The 2 optional fields for related address
1036 // [raddr <connection-address>] [rport <port>]
1037 if (fields.size() >= (current_position + 2) &&
1038 fields[current_position] == kAttributeCandidateRaddr) {
1039 related_address.SetIP(fields[++current_position]);
1040 ++current_position;
1041 }
1042 if (fields.size() >= (current_position + 2) &&
1043 fields[current_position] == kAttributeCandidateRport) {
wu@webrtc.org5e760e72014-04-02 23:19:09 +00001044 int port = 0;
1045 if (!GetValueFromString(
1046 first_line, fields[++current_position], &port, error)) {
1047 return false;
1048 }
deadbeef90f1e1e2017-02-10 12:35:05 -08001049 if (!IsValidPort(port)) {
1050 return ParseFailed(first_line, "Invalid port number.", error);
1051 }
wu@webrtc.org5e760e72014-04-02 23:19:09 +00001052 related_address.SetPort(port);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001053 ++current_position;
1054 }
1055
mallinath@webrtc.org2d60c5e2014-08-08 22:29:20 +00001056 // If this is a TCP candidate, it has additional extension as defined in
1057 // RFC 6544.
1058 std::string tcptype;
1059 if (fields.size() >= (current_position + 2) &&
1060 fields[current_position] == kTcpCandidateType) {
1061 tcptype = fields[++current_position];
1062 ++current_position;
1063
1064 if (tcptype != cricket::TCPTYPE_ACTIVE_STR &&
1065 tcptype != cricket::TCPTYPE_PASSIVE_STR &&
1066 tcptype != cricket::TCPTYPE_SIMOPEN_STR) {
1067 return ParseFailed(first_line, "Invalid TCP candidate type.", error);
1068 }
1069
1070 if (protocol != cricket::PROTO_TCP) {
1071 return ParseFailed(first_line, "Invalid non-TCP candidate", error);
1072 }
1073 }
1074
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001075 // Extension
honghaiza54a0802015-12-16 18:37:23 -08001076 // Though non-standard, we support the ICE ufrag and pwd being signaled on
1077 // the candidate to avoid issues with confusing which generation a candidate
1078 // belongs to when trickling multiple generations at the same time.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001079 std::string username;
1080 std::string password;
Peter Boström0c4e06b2015-10-07 12:23:21 +02001081 uint32_t generation = 0;
honghaiza0c44ea2016-03-23 16:07:48 -07001082 uint16_t network_id = 0;
1083 uint16_t network_cost = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001084 for (size_t i = current_position; i + 1 < fields.size(); ++i) {
1085 // RFC 5245
1086 // *(SP extension-att-name SP extension-att-value)
1087 if (fields[i] == kAttributeCandidateGeneration) {
wu@webrtc.org5e760e72014-04-02 23:19:09 +00001088 if (!GetValueFromString(first_line, fields[++i], &generation, error)) {
1089 return false;
1090 }
honghaiza54a0802015-12-16 18:37:23 -08001091 } else if (fields[i] == kAttributeCandidateUfrag) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001092 username = fields[++i];
honghaiza54a0802015-12-16 18:37:23 -08001093 } else if (fields[i] == kAttributeCandidatePwd) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001094 password = fields[++i];
honghaiza0c44ea2016-03-23 16:07:48 -07001095 } else if (fields[i] == kAttributeCandidateNetworkId) {
1096 if (!GetValueFromString(first_line, fields[++i], &network_id, error)) {
1097 return false;
1098 }
honghaize1a0c942016-02-16 14:54:56 -08001099 } else if (fields[i] == kAttributeCandidateNetworkCost) {
1100 if (!GetValueFromString(first_line, fields[++i], &network_cost, error)) {
1101 return false;
1102 }
Honghai Zhang351d77b2016-05-20 15:08:29 -07001103 network_cost = std::min(network_cost, rtc::kNetworkCostMax);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001104 } else {
1105 // Skip the unknown extension.
1106 ++i;
1107 }
1108 }
1109
guoweis@webrtc.org61c12472015-01-15 06:53:07 +00001110 *candidate = Candidate(component_id, cricket::ProtoToString(protocol),
guoweis@webrtc.org950c5182014-12-16 23:01:31 +00001111 address, priority, username, password, candidate_type,
honghaiza0c44ea2016-03-23 16:07:48 -07001112 generation, foundation, network_id, network_cost);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001113 candidate->set_related_address(related_address);
mallinath@webrtc.org2d60c5e2014-08-08 22:29:20 +00001114 candidate->set_tcptype(tcptype);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001115 return true;
1116}
1117
1118bool ParseIceOptions(const std::string& line,
1119 std::vector<std::string>* transport_options,
1120 SdpParseError* error) {
1121 std::string ice_options;
1122 if (!GetValue(line, kAttributeIceOption, &ice_options, error)) {
1123 return false;
1124 }
1125 std::vector<std::string> fields;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001126 rtc::split(ice_options, kSdpDelimiterSpace, &fields);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001127 for (size_t i = 0; i < fields.size(); ++i) {
1128 transport_options->push_back(fields[i]);
1129 }
1130 return true;
1131}
1132
jiayl@webrtc.orgddb85ab2014-09-05 16:31:56 +00001133bool ParseSctpPort(const std::string& line,
1134 int* sctp_port,
1135 SdpParseError* error) {
1136 // draft-ietf-mmusic-sctp-sdp-07
1137 // a=sctp-port
1138 std::vector<std::string> fields;
jiayl@webrtc.orgddb85ab2014-09-05 16:31:56 +00001139 const size_t expected_min_fields = 2;
lally69f57602015-10-08 10:15:04 -07001140 rtc::split(line.substr(kLinePrefixLength), kSdpDelimiterColon, &fields);
1141 if (fields.size() < expected_min_fields) {
1142 fields.resize(0);
1143 rtc::split(line.substr(kLinePrefixLength), kSdpDelimiterSpace, &fields);
1144 }
jiayl@webrtc.orgddb85ab2014-09-05 16:31:56 +00001145 if (fields.size() < expected_min_fields) {
1146 return ParseFailedExpectMinFieldNum(line, expected_min_fields, error);
1147 }
1148 if (!rtc::FromString(fields[1], sctp_port)) {
lally69f57602015-10-08 10:15:04 -07001149 return ParseFailed(line, "Invalid sctp port value.", error);
jiayl@webrtc.orgddb85ab2014-09-05 16:31:56 +00001150 }
1151 return true;
1152}
1153
isheriff6f8d6862016-05-26 11:24:55 -07001154bool ParseExtmap(const std::string& line,
1155 RtpExtension* extmap,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001156 SdpParseError* error) {
1157 // RFC 5285
1158 // a=extmap:<value>["/"<direction>] <URI> <extensionattributes>
1159 std::vector<std::string> fields;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001160 rtc::split(line.substr(kLinePrefixLength),
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001161 kSdpDelimiterSpace, &fields);
1162 const size_t expected_min_fields = 2;
1163 if (fields.size() < expected_min_fields) {
1164 return ParseFailedExpectMinFieldNum(line, expected_min_fields, error);
1165 }
1166 std::string uri = fields[1];
1167
1168 std::string value_direction;
1169 if (!GetValue(fields[0], kAttributeExtmap, &value_direction, error)) {
1170 return false;
1171 }
1172 std::vector<std::string> sub_fields;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001173 rtc::split(value_direction, kSdpDelimiterSlash, &sub_fields);
wu@webrtc.org5e760e72014-04-02 23:19:09 +00001174 int value = 0;
1175 if (!GetValueFromString(line, sub_fields[0], &value, error)) {
1176 return false;
1177 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001178
jbauch5869f502017-06-29 12:31:36 -07001179 bool encrypted = false;
1180 if (uri == RtpExtension::kEncryptHeaderExtensionsUri) {
1181 // RFC 6904
1182 // a=extmap:<value["/"<direction>] urn:ietf:params:rtp-hdrext:encrypt \
1183 // <URI> <extensionattributes>
1184 const size_t expected_min_fields_encrypted = expected_min_fields + 1;
1185 if (fields.size() < expected_min_fields_encrypted) {
1186 return ParseFailedExpectMinFieldNum(line, expected_min_fields_encrypted,
1187 error);
1188 }
1189
1190 encrypted = true;
1191 uri = fields[2];
1192 if (uri == RtpExtension::kEncryptHeaderExtensionsUri) {
1193 return ParseFailed(line, "Recursive encrypted header.", error);
1194 }
1195 }
1196
1197 *extmap = RtpExtension(uri, value, encrypted);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001198 return true;
1199}
1200
1201void BuildMediaDescription(const ContentInfo* content_info,
1202 const TransportInfo* transport_info,
1203 const MediaType media_type,
wu@webrtc.org4c3e9912014-07-16 21:03:13 +00001204 const std::vector<Candidate>& candidates,
deadbeef9d3584c2016-02-16 17:54:10 -08001205 bool unified_plan_sdp,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001206 std::string* message) {
nisseede5da42017-01-12 05:15:36 -08001207 RTC_DCHECK(message != NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001208 if (content_info == NULL || message == NULL) {
1209 return;
1210 }
1211 // TODO: Rethink if we should use sprintfn instead of stringstream.
1212 // According to the style guide, streams should only be used for logging.
1213 // http://google-styleguide.googlecode.com/svn/
1214 // trunk/cppguide.xml?showone=Streams#Streams
1215 std::ostringstream os;
1216 const MediaContentDescription* media_desc =
henrike@webrtc.org28654cb2013-07-22 21:07:49 +00001217 static_cast<const MediaContentDescription*>(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001218 content_info->description);
nisseede5da42017-01-12 05:15:36 -08001219 RTC_DCHECK(media_desc != NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001220
jiayl@webrtc.org9c16c392014-05-01 18:30:30 +00001221 int sctp_port = cricket::kSctpDefaultPort;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001222
1223 // RFC 4566
1224 // m=<media> <port> <proto> <fmt>
1225 // fmt is a list of payload type numbers that MAY be used in the session.
1226 const char* type = NULL;
1227 if (media_type == cricket::MEDIA_TYPE_AUDIO)
1228 type = kMediaTypeAudio;
1229 else if (media_type == cricket::MEDIA_TYPE_VIDEO)
1230 type = kMediaTypeVideo;
1231 else if (media_type == cricket::MEDIA_TYPE_DATA)
1232 type = kMediaTypeData;
1233 else
nissec80e7412017-01-11 05:56:46 -08001234 RTC_NOTREACHED();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001235
1236 std::string fmt;
1237 if (media_type == cricket::MEDIA_TYPE_VIDEO) {
1238 const VideoContentDescription* video_desc =
1239 static_cast<const VideoContentDescription*>(media_desc);
1240 for (std::vector<cricket::VideoCodec>::const_iterator it =
1241 video_desc->codecs().begin();
1242 it != video_desc->codecs().end(); ++it) {
1243 fmt.append(" ");
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001244 fmt.append(rtc::ToString<int>(it->id));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001245 }
1246 } else if (media_type == cricket::MEDIA_TYPE_AUDIO) {
1247 const AudioContentDescription* audio_desc =
1248 static_cast<const AudioContentDescription*>(media_desc);
1249 for (std::vector<cricket::AudioCodec>::const_iterator it =
1250 audio_desc->codecs().begin();
1251 it != audio_desc->codecs().end(); ++it) {
1252 fmt.append(" ");
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001253 fmt.append(rtc::ToString<int>(it->id));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001254 }
1255 } else if (media_type == cricket::MEDIA_TYPE_DATA) {
jiayl@webrtc.org9c16c392014-05-01 18:30:30 +00001256 const DataContentDescription* data_desc =
1257 static_cast<const DataContentDescription*>(media_desc);
pthatcher@webrtc.org3341b402015-02-13 21:14:22 +00001258 if (IsDtlsSctp(media_desc->protocol())) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001259 fmt.append(" ");
jiayl@webrtc.org9c16c392014-05-01 18:30:30 +00001260
zstein4b2e0822017-02-17 19:48:38 -08001261 if (data_desc->use_sctpmap()) {
1262 for (std::vector<cricket::DataCodec>::const_iterator it =
1263 data_desc->codecs().begin();
1264 it != data_desc->codecs().end(); ++it) {
1265 if (cricket::CodecNamesEq(it->name,
1266 cricket::kGoogleSctpDataCodecName) &&
1267 it->GetParam(cricket::kCodecParamPort, &sctp_port)) {
1268 break;
1269 }
jiayl@webrtc.org9c16c392014-05-01 18:30:30 +00001270 }
jiayl@webrtc.org9c16c392014-05-01 18:30:30 +00001271
zstein4b2e0822017-02-17 19:48:38 -08001272 fmt.append(rtc::ToString<int>(sctp_port));
1273 } else {
1274 fmt.append(kDefaultSctpmapProtocol);
1275 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001276 } else {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001277 for (std::vector<cricket::DataCodec>::const_iterator it =
1278 data_desc->codecs().begin();
1279 it != data_desc->codecs().end(); ++it) {
1280 fmt.append(" ");
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001281 fmt.append(rtc::ToString<int>(it->id));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001282 }
1283 }
1284 }
1285 // The fmt must never be empty. If no codecs are found, set the fmt attribute
1286 // to 0.
1287 if (fmt.empty()) {
1288 fmt = " 0";
1289 }
1290
deadbeef25ed4352016-12-12 18:37:36 -08001291 // The port number in the m line will be updated later when associated with
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001292 // the candidates.
deadbeef25ed4352016-12-12 18:37:36 -08001293 //
1294 // A port value of 0 indicates that the m= section is rejected.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001295 // RFC 3264
1296 // To reject an offered stream, the port number in the corresponding stream in
1297 // the answer MUST be set to zero.
deadbeef25ed4352016-12-12 18:37:36 -08001298 //
1299 // However, the BUNDLE draft adds a new meaning to port zero, when used along
1300 // with a=bundle-only.
zhihuang38989e52017-03-21 11:04:53 -07001301 std::string port = kDummyPort;
1302 if (content_info->rejected || content_info->bundle_only) {
1303 port = kMediaPortRejected;
1304 } else if (!media_desc->connection_address().IsNil()) {
1305 port = rtc::ToString(media_desc->connection_address().port());
1306 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001307
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001308 rtc::SSLFingerprint* fp = (transport_info) ?
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001309 transport_info->description.identity_fingerprint.get() : NULL;
1310
wu@webrtc.org4c3e9912014-07-16 21:03:13 +00001311 // Add the m and c lines.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001312 InitLine(kLineTypeMedia, type, &os);
1313 os << " " << port << " " << media_desc->protocol() << fmt;
zhihuang38989e52017-03-21 11:04:53 -07001314 AddLine(os.str(), message);
1315
1316 InitLine(kLineTypeConnection, kConnectionNettype, &os);
1317 if (media_desc->connection_address().IsNil()) {
1318 os << " " << kConnectionIpv4Addrtype << " " << kDummyAddress;
1319 } else if (media_desc->connection_address().family() == AF_INET) {
1320 os << " " << kConnectionIpv4Addrtype << " "
1321 << media_desc->connection_address().ipaddr().ToString();
1322 } else {
1323 os << " " << kConnectionIpv6Addrtype << " "
1324 << media_desc->connection_address().ipaddr().ToString();
1325 }
1326 AddLine(os.str(), message);
wu@webrtc.org4c3e9912014-07-16 21:03:13 +00001327
1328 // RFC 4566
1329 // b=AS:<bandwidth>
Peter Thatcherc0c3a862015-06-24 15:31:25 -07001330 if (media_desc->bandwidth() >= 1000) {
wu@webrtc.org4c3e9912014-07-16 21:03:13 +00001331 InitLine(kLineTypeSessionBandwidth, kApplicationSpecificMaximum, &os);
1332 os << kSdpDelimiterColon << (media_desc->bandwidth() / 1000);
1333 AddLine(os.str(), message);
1334 }
1335
deadbeef25ed4352016-12-12 18:37:36 -08001336 // Add the a=bundle-only line.
1337 if (content_info->bundle_only) {
1338 InitAttrLine(kAttributeBundleOnly, &os);
1339 AddLine(os.str(), message);
1340 }
1341
wu@webrtc.org4c3e9912014-07-16 21:03:13 +00001342 // Add the a=rtcp line.
pthatcher@webrtc.org3341b402015-02-13 21:14:22 +00001343 if (IsRtp(media_desc->protocol())) {
wu@webrtc.org4c3e9912014-07-16 21:03:13 +00001344 std::string rtcp_line = GetRtcpLine(candidates);
1345 if (!rtcp_line.empty()) {
1346 AddLine(rtcp_line, message);
1347 }
1348 }
1349
honghaiza54a0802015-12-16 18:37:23 -08001350 // Build the a=candidate lines. We don't include ufrag and pwd in the
1351 // candidates in the SDP to avoid redundancy.
1352 BuildCandidate(candidates, false, message);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001353
1354 // Use the transport_info to build the media level ice-ufrag and ice-pwd.
1355 if (transport_info) {
1356 // RFC 5245
1357 // ice-pwd-att = "ice-pwd" ":" password
1358 // ice-ufrag-att = "ice-ufrag" ":" ufrag
1359 // ice-ufrag
deadbeef3f7219b2015-12-28 15:17:14 -08001360 if (!transport_info->description.ice_ufrag.empty()) {
1361 InitAttrLine(kAttributeIceUfrag, &os);
1362 os << kSdpDelimiterColon << transport_info->description.ice_ufrag;
1363 AddLine(os.str(), message);
1364 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001365 // ice-pwd
deadbeef3f7219b2015-12-28 15:17:14 -08001366 if (!transport_info->description.ice_pwd.empty()) {
1367 InitAttrLine(kAttributeIcePwd, &os);
1368 os << kSdpDelimiterColon << transport_info->description.ice_pwd;
1369 AddLine(os.str(), message);
1370 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001371
1372 // draft-petithuguenin-mmusic-ice-attributes-level-03
1373 BuildIceOptions(transport_info->description.transport_options, message);
1374
1375 // RFC 4572
1376 // fingerprint-attribute =
1377 // "fingerprint" ":" hash-func SP fingerprint
1378 if (fp) {
1379 // Insert the fingerprint attribute.
1380 InitAttrLine(kAttributeFingerprint, &os);
1381 os << kSdpDelimiterColon
1382 << fp->algorithm << kSdpDelimiterSpace
1383 << fp->GetRfc4572Fingerprint();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001384 AddLine(os.str(), message);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001385
1386 // Inserting setup attribute.
1387 if (transport_info->description.connection_role !=
1388 cricket::CONNECTIONROLE_NONE) {
1389 // Making sure we are not using "passive" mode.
1390 cricket::ConnectionRole role =
1391 transport_info->description.connection_role;
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00001392 std::string dtls_role_str;
nissec16fa5e2017-02-07 07:18:43 -08001393 const bool success =
1394 cricket::ConnectionRoleToString(role, &dtls_role_str);
1395 RTC_DCHECK(success);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001396 InitAttrLine(kAttributeSetup, &os);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001397 os << kSdpDelimiterColon << dtls_role_str;
1398 AddLine(os.str(), message);
1399 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001400 }
1401 }
1402
1403 // RFC 3388
1404 // mid-attribute = "a=mid:" identification-tag
1405 // identification-tag = token
1406 // Use the content name as the mid identification-tag.
1407 InitAttrLine(kAttributeMid, &os);
1408 os << kSdpDelimiterColon << content_info->name;
1409 AddLine(os.str(), message);
1410
pthatcher@webrtc.org3341b402015-02-13 21:14:22 +00001411 if (IsDtlsSctp(media_desc->protocol())) {
zstein4b2e0822017-02-17 19:48:38 -08001412 const DataContentDescription* data_desc =
1413 static_cast<const DataContentDescription*>(media_desc);
1414 bool use_sctpmap = data_desc->use_sctpmap();
1415 BuildSctpContentAttributes(message, sctp_port, use_sctpmap);
pthatcher@webrtc.org3341b402015-02-13 21:14:22 +00001416 } else if (IsRtp(media_desc->protocol())) {
deadbeef9d3584c2016-02-16 17:54:10 -08001417 BuildRtpContentAttributes(media_desc, media_type, unified_plan_sdp,
1418 message);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001419 }
1420}
1421
zstein4b2e0822017-02-17 19:48:38 -08001422void BuildSctpContentAttributes(std::string* message,
1423 int sctp_port,
1424 bool use_sctpmap) {
wu@webrtc.org78187522013-10-07 23:32:02 +00001425 std::ostringstream os;
zstein4b2e0822017-02-17 19:48:38 -08001426 if (use_sctpmap) {
1427 // draft-ietf-mmusic-sctp-sdp-04
1428 // a=sctpmap:sctpmap-number protocol [streams]
1429 InitAttrLine(kAttributeSctpmap, &os);
1430 os << kSdpDelimiterColon << sctp_port << kSdpDelimiterSpace
1431 << kDefaultSctpmapProtocol << kSdpDelimiterSpace
1432 << cricket::kMaxSctpStreams;
1433 } else {
1434 // draft-ietf-mmusic-sctp-sdp-23
1435 // a=sctp-port:<port>
1436 InitAttrLine(kAttributeSctpPort, &os);
1437 os << kSdpDelimiterColon << sctp_port;
1438 // TODO(zstein): emit max-message-size here
1439 }
wu@webrtc.org78187522013-10-07 23:32:02 +00001440 AddLine(os.str(), message);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001441}
1442
deadbeef9d3584c2016-02-16 17:54:10 -08001443// If unified_plan_sdp is true, will use "a=msid".
1444void BuildRtpContentAttributes(const MediaContentDescription* media_desc,
1445 const MediaType media_type,
1446 bool unified_plan_sdp,
1447 std::string* message) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001448 std::ostringstream os;
1449 // RFC 5285
1450 // a=extmap:<value>["/"<direction>] <URI> <extensionattributes>
1451 // The definitions MUST be either all session level or all media level. This
1452 // implementation uses all media level.
1453 for (size_t i = 0; i < media_desc->rtp_header_extensions().size(); ++i) {
jbauch5869f502017-06-29 12:31:36 -07001454 const RtpExtension& extension = media_desc->rtp_header_extensions()[i];
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001455 InitAttrLine(kAttributeExtmap, &os);
jbauch5869f502017-06-29 12:31:36 -07001456 os << kSdpDelimiterColon << extension.id;
1457 if (extension.encrypt) {
1458 os << kSdpDelimiterSpace << RtpExtension::kEncryptHeaderExtensionsUri;
1459 }
1460 os << kSdpDelimiterSpace << extension.uri;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001461 AddLine(os.str(), message);
1462 }
1463
1464 // RFC 3264
1465 // a=sendrecv || a=sendonly || a=sendrecv || a=inactive
deadbeefc80741f2015-10-22 13:14:45 -07001466 switch (media_desc->direction()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001467 case cricket::MD_INACTIVE:
1468 InitAttrLine(kAttributeInactive, &os);
1469 break;
1470 case cricket::MD_SENDONLY:
1471 InitAttrLine(kAttributeSendOnly, &os);
1472 break;
1473 case cricket::MD_RECVONLY:
1474 InitAttrLine(kAttributeRecvOnly, &os);
1475 break;
1476 case cricket::MD_SENDRECV:
1477 default:
1478 InitAttrLine(kAttributeSendRecv, &os);
1479 break;
1480 }
1481 AddLine(os.str(), message);
1482
deadbeef9d3584c2016-02-16 17:54:10 -08001483 // draft-ietf-mmusic-msid-11
1484 // a=msid:<stream id> <track id>
1485 if (unified_plan_sdp && !media_desc->streams().empty()) {
1486 if (media_desc->streams().size() > 1u) {
1487 LOG(LS_WARNING) << "Trying to serialize unified plan SDP with more than "
1488 << "one track in a media section. Omitting 'a=msid'.";
1489 } else {
1490 auto track = media_desc->streams().begin();
1491 const std::string& stream_id = track->sync_label;
deadbeef9d3584c2016-02-16 17:54:10 -08001492 InitAttrLine(kAttributeMsid, &os);
1493 os << kSdpDelimiterColon << stream_id << kSdpDelimiterSpace << track->id;
1494 AddLine(os.str(), message);
1495 }
1496 }
1497
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001498 // RFC 5761
1499 // a=rtcp-mux
1500 if (media_desc->rtcp_mux()) {
1501 InitAttrLine(kAttributeRtcpMux, &os);
1502 AddLine(os.str(), message);
1503 }
1504
deadbeef13871492015-12-09 12:37:51 -08001505 // RFC 5506
1506 // a=rtcp-rsize
1507 if (media_desc->rtcp_reduced_size()) {
1508 InitAttrLine(kAttributeRtcpReducedSize, &os);
1509 AddLine(os.str(), message);
1510 }
1511
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001512 // RFC 4568
1513 // a=crypto:<tag> <crypto-suite> <key-params> [<session-params>]
1514 for (std::vector<CryptoParams>::const_iterator it =
1515 media_desc->cryptos().begin();
1516 it != media_desc->cryptos().end(); ++it) {
1517 InitAttrLine(kAttributeCrypto, &os);
1518 os << kSdpDelimiterColon << it->tag << " " << it->cipher_suite << " "
1519 << it->key_params;
1520 if (!it->session_params.empty()) {
1521 os << " " << it->session_params;
1522 }
1523 AddLine(os.str(), message);
1524 }
1525
1526 // RFC 4566
1527 // a=rtpmap:<payload type> <encoding name>/<clock rate>
1528 // [/<encodingparameters>]
1529 BuildRtpMap(media_desc, media_type, message);
1530
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001531 for (StreamParamsVec::const_iterator track = media_desc->streams().begin();
1532 track != media_desc->streams().end(); ++track) {
1533 // Require that the track belongs to a media stream,
1534 // ie the sync_label is set. This extra check is necessary since the
1535 // MediaContentDescription always contains a streamparam with an ssrc even
1536 // if no track or media stream have been created.
1537 if (track->sync_label.empty()) continue;
1538
1539 // Build the ssrc-group lines.
1540 for (size_t i = 0; i < track->ssrc_groups.size(); ++i) {
1541 // RFC 5576
1542 // a=ssrc-group:<semantics> <ssrc-id> ...
1543 if (track->ssrc_groups[i].ssrcs.empty()) {
1544 continue;
1545 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001546 InitAttrLine(kAttributeSsrcGroup, &os);
1547 os << kSdpDelimiterColon << track->ssrc_groups[i].semantics;
Peter Boström0c4e06b2015-10-07 12:23:21 +02001548 std::vector<uint32_t>::const_iterator ssrc =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001549 track->ssrc_groups[i].ssrcs.begin();
1550 for (; ssrc != track->ssrc_groups[i].ssrcs.end(); ++ssrc) {
Peter Boström0c4e06b2015-10-07 12:23:21 +02001551 os << kSdpDelimiterSpace << rtc::ToString<uint32_t>(*ssrc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001552 }
1553 AddLine(os.str(), message);
1554 }
1555 // Build the ssrc lines for each ssrc.
1556 for (size_t i = 0; i < track->ssrcs.size(); ++i) {
Peter Boström0c4e06b2015-10-07 12:23:21 +02001557 uint32_t ssrc = track->ssrcs[i];
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001558 // RFC 5576
1559 // a=ssrc:<ssrc-id> cname:<value>
1560 AddSsrcLine(ssrc, kSsrcAttributeCname,
1561 track->cname, message);
1562
1563 // draft-alvestrand-mmusic-msid-00
1564 // a=ssrc:<ssrc-id> msid:identifier [appdata]
deadbeef9d3584c2016-02-16 17:54:10 -08001565 // The appdata consists of the "id" attribute of a MediaStreamTrack,
1566 // which corresponds to the "id" attribute of StreamParams.
1567 const std::string& stream_id = track->sync_label;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001568 InitAttrLine(kAttributeSsrc, &os);
1569 os << kSdpDelimiterColon << ssrc << kSdpDelimiterSpace
deadbeef9d3584c2016-02-16 17:54:10 -08001570 << kSsrcAttributeMsid << kSdpDelimiterColon << stream_id
1571 << kSdpDelimiterSpace << track->id;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001572 AddLine(os.str(), message);
1573
deadbeef9d3584c2016-02-16 17:54:10 -08001574 // TODO(ronghuawu): Remove below code which is for backward
1575 // compatibility.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001576 // draft-alvestrand-rtcweb-mid-01
1577 // a=ssrc:<ssrc-id> mslabel:<value>
1578 // The label isn't yet defined.
1579 // a=ssrc:<ssrc-id> label:<value>
1580 AddSsrcLine(ssrc, kSsrcAttributeMslabel, track->sync_label, message);
1581 AddSsrcLine(ssrc, kSSrcAttributeLabel, track->id, message);
1582 }
1583 }
1584}
1585
1586void WriteFmtpHeader(int payload_type, std::ostringstream* os) {
1587 // fmtp header: a=fmtp:|payload_type| <parameters>
1588 // Add a=fmtp
1589 InitAttrLine(kAttributeFmtp, os);
1590 // Add :|payload_type|
1591 *os << kSdpDelimiterColon << payload_type;
1592}
1593
1594void WriteRtcpFbHeader(int payload_type, std::ostringstream* os) {
1595 // rtcp-fb header: a=rtcp-fb:|payload_type|
1596 // <parameters>/<ccm <ccm_parameters>>
1597 // Add a=rtcp-fb
1598 InitAttrLine(kAttributeRtcpFb, os);
1599 // Add :
1600 *os << kSdpDelimiterColon;
1601 if (payload_type == kWildcardPayloadType) {
1602 *os << "*";
1603 } else {
1604 *os << payload_type;
1605 }
1606}
1607
1608void WriteFmtpParameter(const std::string& parameter_name,
1609 const std::string& parameter_value,
1610 std::ostringstream* os) {
1611 // fmtp parameters: |parameter_name|=|parameter_value|
1612 *os << parameter_name << kSdpDelimiterEqual << parameter_value;
1613}
1614
1615void WriteFmtpParameters(const cricket::CodecParameterMap& parameters,
1616 std::ostringstream* os) {
1617 for (cricket::CodecParameterMap::const_iterator fmtp = parameters.begin();
1618 fmtp != parameters.end(); ++fmtp) {
hta62a216e2016-04-15 11:02:14 -07001619 // Parameters are a semicolon-separated list, no spaces.
1620 // The list is separated from the header by a space.
1621 if (fmtp == parameters.begin()) {
1622 *os << kSdpDelimiterSpace;
1623 } else {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001624 *os << kSdpDelimiterSemicolon;
1625 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001626 WriteFmtpParameter(fmtp->first, fmtp->second, os);
1627 }
1628}
1629
1630bool IsFmtpParam(const std::string& name) {
ossuaa4b0772017-01-30 07:41:18 -08001631 // RFC 4855, section 3 specifies the mapping of media format parameters to SDP
1632 // parameters. Only ptime, maxptime, channels and rate are placed outside of
1633 // the fmtp line. In WebRTC, channels and rate are already handled separately
1634 // and thus not included in the CodecParameterMap.
1635 return name != kCodecParamPTime && name != kCodecParamMaxPTime;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001636}
1637
1638// Retreives fmtp parameters from |params|, which may contain other parameters
1639// as well, and puts them in |fmtp_parameters|.
1640void GetFmtpParams(const cricket::CodecParameterMap& params,
1641 cricket::CodecParameterMap* fmtp_parameters) {
1642 for (cricket::CodecParameterMap::const_iterator iter = params.begin();
1643 iter != params.end(); ++iter) {
1644 if (IsFmtpParam(iter->first)) {
1645 (*fmtp_parameters)[iter->first] = iter->second;
1646 }
1647 }
1648}
1649
1650template <class T>
1651void AddFmtpLine(const T& codec, std::string* message) {
1652 cricket::CodecParameterMap fmtp_parameters;
1653 GetFmtpParams(codec.params, &fmtp_parameters);
1654 if (fmtp_parameters.empty()) {
1655 // No need to add an fmtp if it will have no (optional) parameters.
1656 return;
1657 }
1658 std::ostringstream os;
1659 WriteFmtpHeader(codec.id, &os);
1660 WriteFmtpParameters(fmtp_parameters, &os);
1661 AddLine(os.str(), message);
1662 return;
1663}
1664
1665template <class T>
1666void AddRtcpFbLines(const T& codec, std::string* message) {
1667 for (std::vector<cricket::FeedbackParam>::const_iterator iter =
1668 codec.feedback_params.params().begin();
1669 iter != codec.feedback_params.params().end(); ++iter) {
1670 std::ostringstream os;
1671 WriteRtcpFbHeader(codec.id, &os);
1672 os << " " << iter->id();
1673 if (!iter->param().empty()) {
1674 os << " " << iter->param();
1675 }
1676 AddLine(os.str(), message);
1677 }
1678}
1679
jiayl@webrtc.orgddb85ab2014-09-05 16:31:56 +00001680bool AddSctpDataCodec(DataContentDescription* media_desc,
1681 int sctp_port) {
solenberg9fa49752016-10-08 13:02:44 -07001682 for (const auto& codec : media_desc->codecs()) {
1683 if (cricket::CodecNamesEq(codec.name, cricket::kGoogleSctpDataCodecName)) {
1684 return ParseFailed("",
1685 "Can't have multiple sctp port attributes.",
1686 NULL);
1687 }
jiayl@webrtc.orgddb85ab2014-09-05 16:31:56 +00001688 }
1689 // Add the SCTP Port number as a pseudo-codec "port" parameter
solenberg9fa49752016-10-08 13:02:44 -07001690 cricket::DataCodec codec_port(cricket::kGoogleSctpDataCodecPlType,
deadbeef67cf2c12016-04-13 10:07:16 -07001691 cricket::kGoogleSctpDataCodecName);
jiayl@webrtc.orgddb85ab2014-09-05 16:31:56 +00001692 codec_port.SetParam(cricket::kCodecParamPort, sctp_port);
1693 LOG(INFO) << "AddSctpDataCodec: Got SCTP Port Number "
1694 << sctp_port;
1695 media_desc->AddCodec(codec_port);
1696 return true;
1697}
1698
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001699bool GetMinValue(const std::vector<int>& values, int* value) {
1700 if (values.empty()) {
1701 return false;
1702 }
1703 std::vector<int>::const_iterator found =
1704 std::min_element(values.begin(), values.end());
1705 *value = *found;
1706 return true;
1707}
1708
1709bool GetParameter(const std::string& name,
1710 const cricket::CodecParameterMap& params, int* value) {
1711 std::map<std::string, std::string>::const_iterator found =
1712 params.find(name);
1713 if (found == params.end()) {
1714 return false;
1715 }
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001716 if (!rtc::FromString(found->second, value)) {
wu@webrtc.org5e760e72014-04-02 23:19:09 +00001717 return false;
1718 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001719 return true;
1720}
1721
1722void BuildRtpMap(const MediaContentDescription* media_desc,
1723 const MediaType media_type,
1724 std::string* message) {
nisseede5da42017-01-12 05:15:36 -08001725 RTC_DCHECK(message != NULL);
1726 RTC_DCHECK(media_desc != NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001727 std::ostringstream os;
1728 if (media_type == cricket::MEDIA_TYPE_VIDEO) {
1729 const VideoContentDescription* video_desc =
1730 static_cast<const VideoContentDescription*>(media_desc);
1731 for (std::vector<cricket::VideoCodec>::const_iterator it =
1732 video_desc->codecs().begin();
1733 it != video_desc->codecs().end(); ++it) {
1734 // RFC 4566
1735 // a=rtpmap:<payload type> <encoding name>/<clock rate>
1736 // [/<encodingparameters>]
1737 if (it->id != kWildcardPayloadType) {
1738 InitAttrLine(kAttributeRtpmap, &os);
deadbeefe814a0d2017-02-25 18:15:09 -08001739 os << kSdpDelimiterColon << it->id << " " << it->name << "/"
1740 << cricket::kVideoCodecClockrate;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001741 AddLine(os.str(), message);
1742 }
1743 AddRtcpFbLines(*it, message);
1744 AddFmtpLine(*it, message);
1745 }
1746 } else if (media_type == cricket::MEDIA_TYPE_AUDIO) {
1747 const AudioContentDescription* audio_desc =
1748 static_cast<const AudioContentDescription*>(media_desc);
1749 std::vector<int> ptimes;
1750 std::vector<int> maxptimes;
1751 int max_minptime = 0;
1752 for (std::vector<cricket::AudioCodec>::const_iterator it =
1753 audio_desc->codecs().begin();
1754 it != audio_desc->codecs().end(); ++it) {
nisseede5da42017-01-12 05:15:36 -08001755 RTC_DCHECK(!it->name.empty());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001756 // RFC 4566
1757 // a=rtpmap:<payload type> <encoding name>/<clock rate>
1758 // [/<encodingparameters>]
1759 InitAttrLine(kAttributeRtpmap, &os);
1760 os << kSdpDelimiterColon << it->id << " ";
1761 os << it->name << "/" << it->clockrate;
1762 if (it->channels != 1) {
1763 os << "/" << it->channels;
1764 }
1765 AddLine(os.str(), message);
1766 AddRtcpFbLines(*it, message);
1767 AddFmtpLine(*it, message);
1768 int minptime = 0;
1769 if (GetParameter(kCodecParamMinPTime, it->params, &minptime)) {
1770 max_minptime = std::max(minptime, max_minptime);
1771 }
1772 int ptime;
1773 if (GetParameter(kCodecParamPTime, it->params, &ptime)) {
1774 ptimes.push_back(ptime);
1775 }
1776 int maxptime;
1777 if (GetParameter(kCodecParamMaxPTime, it->params, &maxptime)) {
1778 maxptimes.push_back(maxptime);
1779 }
1780 }
1781 // Populate the maxptime attribute with the smallest maxptime of all codecs
1782 // under the same m-line.
1783 int min_maxptime = INT_MAX;
1784 if (GetMinValue(maxptimes, &min_maxptime)) {
1785 AddAttributeLine(kCodecParamMaxPTime, min_maxptime, message);
1786 }
nisseede5da42017-01-12 05:15:36 -08001787 RTC_DCHECK(min_maxptime > max_minptime);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001788 // Populate the ptime attribute with the smallest ptime or the largest
1789 // minptime, whichever is the largest, for all codecs under the same m-line.
1790 int ptime = INT_MAX;
1791 if (GetMinValue(ptimes, &ptime)) {
1792 ptime = std::min(ptime, min_maxptime);
1793 ptime = std::max(ptime, max_minptime);
1794 AddAttributeLine(kCodecParamPTime, ptime, message);
1795 }
1796 } else if (media_type == cricket::MEDIA_TYPE_DATA) {
1797 const DataContentDescription* data_desc =
1798 static_cast<const DataContentDescription*>(media_desc);
1799 for (std::vector<cricket::DataCodec>::const_iterator it =
1800 data_desc->codecs().begin();
1801 it != data_desc->codecs().end(); ++it) {
1802 // RFC 4566
1803 // a=rtpmap:<payload type> <encoding name>/<clock rate>
1804 // [/<encodingparameters>]
1805 InitAttrLine(kAttributeRtpmap, &os);
1806 os << kSdpDelimiterColon << it->id << " "
1807 << it->name << "/" << it->clockrate;
1808 AddLine(os.str(), message);
1809 }
1810 }
1811}
1812
1813void BuildCandidate(const std::vector<Candidate>& candidates,
honghaiza54a0802015-12-16 18:37:23 -08001814 bool include_ufrag,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001815 std::string* message) {
1816 std::ostringstream os;
1817
1818 for (std::vector<Candidate>::const_iterator it = candidates.begin();
1819 it != candidates.end(); ++it) {
1820 // RFC 5245
1821 // a=candidate:<foundation> <component-id> <transport> <priority>
1822 // <connection-address> <port> typ <candidate-types>
1823 // [raddr <connection-address>] [rport <port>]
1824 // *(SP extension-att-name SP extension-att-value)
1825 std::string type;
1826 // Map the cricket candidate type to "host" / "srflx" / "prflx" / "relay"
1827 if (it->type() == cricket::LOCAL_PORT_TYPE) {
1828 type = kCandidateHost;
1829 } else if (it->type() == cricket::STUN_PORT_TYPE) {
1830 type = kCandidateSrflx;
1831 } else if (it->type() == cricket::RELAY_PORT_TYPE) {
1832 type = kCandidateRelay;
Honghai Zhang7fb69db2016-03-14 11:59:18 -07001833 } else if (it->type() == cricket::PRFLX_PORT_TYPE) {
1834 type = kCandidatePrflx;
1835 // Peer reflexive candidate may be signaled for being removed.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001836 } else {
nissec80e7412017-01-11 05:56:46 -08001837 RTC_NOTREACHED();
Peter Thatcher019087f2015-04-28 09:06:26 -07001838 // Never write out candidates if we don't know the type.
1839 continue;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001840 }
1841
1842 InitAttrLine(kAttributeCandidate, &os);
1843 os << kSdpDelimiterColon
mallinath@webrtc.org2d60c5e2014-08-08 22:29:20 +00001844 << it->foundation() << " "
1845 << it->component() << " "
1846 << it->protocol() << " "
1847 << it->priority() << " "
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001848 << it->address().ipaddr().ToString() << " "
1849 << it->address().PortAsString() << " "
mallinath@webrtc.org2d60c5e2014-08-08 22:29:20 +00001850 << kAttributeCandidateTyp << " "
1851 << type << " ";
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001852
1853 // Related address
1854 if (!it->related_address().IsNil()) {
1855 os << kAttributeCandidateRaddr << " "
1856 << it->related_address().ipaddr().ToString() << " "
1857 << kAttributeCandidateRport << " "
1858 << it->related_address().PortAsString() << " ";
1859 }
1860
mallinath@webrtc.org2d60c5e2014-08-08 22:29:20 +00001861 if (it->protocol() == cricket::TCP_PROTOCOL_NAME) {
mallinath@webrtc.orge999bd02014-08-13 06:05:55 +00001862 os << kTcpCandidateType << " " << it->tcptype() << " ";
mallinath@webrtc.org2d60c5e2014-08-08 22:29:20 +00001863 }
1864
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001865 // Extensions
1866 os << kAttributeCandidateGeneration << " " << it->generation();
honghaiza54a0802015-12-16 18:37:23 -08001867 if (include_ufrag && !it->username().empty()) {
1868 os << " " << kAttributeCandidateUfrag << " " << it->username();
1869 }
honghaiza0c44ea2016-03-23 16:07:48 -07001870 if (it->network_id() > 0) {
1871 os << " " << kAttributeCandidateNetworkId << " " << it->network_id();
1872 }
honghaize1a0c942016-02-16 14:54:56 -08001873 if (it->network_cost() > 0) {
1874 os << " " << kAttributeCandidateNetworkCost << " " << it->network_cost();
1875 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001876
1877 AddLine(os.str(), message);
1878 }
1879}
1880
1881void BuildIceOptions(const std::vector<std::string>& transport_options,
1882 std::string* message) {
1883 if (!transport_options.empty()) {
1884 std::ostringstream os;
1885 InitAttrLine(kAttributeIceOption, &os);
1886 os << kSdpDelimiterColon << transport_options[0];
1887 for (size_t i = 1; i < transport_options.size(); ++i) {
1888 os << kSdpDelimiterSpace << transport_options[i];
1889 }
1890 AddLine(os.str(), message);
1891 }
1892}
1893
pthatcher@webrtc.org3341b402015-02-13 21:14:22 +00001894bool IsRtp(const std::string& protocol) {
1895 return protocol.empty() ||
1896 (protocol.find(cricket::kMediaProtocolRtpPrefix) != std::string::npos);
1897}
1898
1899bool IsDtlsSctp(const std::string& protocol) {
1900 // This intentionally excludes "SCTP" and "SCTP/DTLS".
lally@webrtc.orga7470932015-02-24 20:19:21 +00001901 return protocol.find(cricket::kMediaProtocolDtlsSctp) != std::string::npos;
pthatcher@webrtc.org3341b402015-02-13 21:14:22 +00001902}
1903
zhihuang38989e52017-03-21 11:04:53 -07001904bool ParseConnectionData(const std::string& line,
1905 rtc::SocketAddress* addr,
1906 SdpParseError* error) {
1907 // Parse the line from left to right.
1908 std::string token;
1909 std::string rightpart;
1910 // RFC 4566
1911 // c=<nettype> <addrtype> <connection-address>
1912 // Skip the "c="
1913 if (!rtc::tokenize_first(line, kSdpDelimiterEqual, &token, &rightpart)) {
1914 return ParseFailed(line, "Failed to parse the network type.", error);
1915 }
1916
1917 // Extract and verify the <nettype>
1918 if (!rtc::tokenize_first(rightpart, kSdpDelimiterSpace, &token, &rightpart) ||
1919 token != kConnectionNettype) {
1920 return ParseFailed(line,
1921 "Failed to parse the connection data. The network type "
1922 "is not currently supported.",
1923 error);
1924 }
1925
1926 // Extract the "<addrtype>" and "<connection-address>".
1927 if (!rtc::tokenize_first(rightpart, kSdpDelimiterSpace, &token, &rightpart)) {
1928 return ParseFailed(line, "Failed to parse the address type.", error);
1929 }
1930
1931 // The rightpart part should be the IP address without the slash which is used
1932 // for multicast.
1933 if (rightpart.find('/') != std::string::npos) {
1934 return ParseFailed(line,
1935 "Failed to parse the connection data. Multicast is not "
1936 "currently supported.",
1937 error);
1938 }
1939 addr->SetIP(rightpart);
1940
1941 // Verify that the addrtype matches the type of the parsed address.
1942 if ((addr->family() == AF_INET && token != "IP4") ||
1943 (addr->family() == AF_INET6 && token != "IP6")) {
1944 addr->Clear();
1945 return ParseFailed(
1946 line,
1947 "Failed to parse the connection data. The address type is mismatching.",
1948 error);
1949 }
1950 return true;
1951}
1952
1953bool ParseSessionDescription(const std::string& message,
1954 size_t* pos,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001955 std::string* session_id,
1956 std::string* session_version,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001957 TransportDescription* session_td,
1958 RtpHeaderExtensions* session_extmaps,
zhihuang38989e52017-03-21 11:04:53 -07001959 rtc::SocketAddress* connection_addr,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001960 cricket::SessionDescription* desc,
1961 SdpParseError* error) {
1962 std::string line;
1963
deadbeefc80741f2015-10-22 13:14:45 -07001964 desc->set_msid_supported(false);
1965
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001966 // RFC 4566
1967 // v= (protocol version)
1968 if (!GetLineWithType(message, pos, &line, kLineTypeVersion)) {
1969 return ParseFailedExpectLine(message, *pos, kLineTypeVersion,
1970 std::string(), error);
1971 }
1972 // RFC 4566
1973 // o=<username> <sess-id> <sess-version> <nettype> <addrtype>
1974 // <unicast-address>
1975 if (!GetLineWithType(message, pos, &line, kLineTypeOrigin)) {
1976 return ParseFailedExpectLine(message, *pos, kLineTypeOrigin,
1977 std::string(), error);
1978 }
1979 std::vector<std::string> fields;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001980 rtc::split(line.substr(kLinePrefixLength),
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001981 kSdpDelimiterSpace, &fields);
1982 const size_t expected_fields = 6;
1983 if (fields.size() != expected_fields) {
1984 return ParseFailedExpectFieldNum(line, expected_fields, error);
1985 }
1986 *session_id = fields[1];
1987 *session_version = fields[2];
1988
1989 // RFC 4566
1990 // s= (session name)
1991 if (!GetLineWithType(message, pos, &line, kLineTypeSessionName)) {
1992 return ParseFailedExpectLine(message, *pos, kLineTypeSessionName,
1993 std::string(), error);
1994 }
1995
1996 // Optional lines
1997 // Those are the optional lines, so shouldn't return false if not present.
1998 // RFC 4566
1999 // i=* (session information)
2000 GetLineWithType(message, pos, &line, kLineTypeSessionInfo);
2001
2002 // RFC 4566
2003 // u=* (URI of description)
2004 GetLineWithType(message, pos, &line, kLineTypeSessionUri);
2005
2006 // RFC 4566
2007 // e=* (email address)
2008 GetLineWithType(message, pos, &line, kLineTypeSessionEmail);
2009
2010 // RFC 4566
2011 // p=* (phone number)
2012 GetLineWithType(message, pos, &line, kLineTypeSessionPhone);
2013
2014 // RFC 4566
2015 // c=* (connection information -- not required if included in
2016 // all media)
zhihuang38989e52017-03-21 11:04:53 -07002017 if (GetLineWithType(message, pos, &line, kLineTypeConnection)) {
2018 if (!ParseConnectionData(line, connection_addr, error)) {
2019 return false;
2020 }
2021 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002022
2023 // RFC 4566
2024 // b=* (zero or more bandwidth information lines)
2025 while (GetLineWithType(message, pos, &line, kLineTypeSessionBandwidth)) {
2026 // By pass zero or more b lines.
2027 }
2028
2029 // RFC 4566
2030 // One or more time descriptions ("t=" and "r=" lines; see below)
2031 // t= (time the session is active)
2032 // r=* (zero or more repeat times)
2033 // Ensure there's at least one time description
2034 if (!GetLineWithType(message, pos, &line, kLineTypeTiming)) {
2035 return ParseFailedExpectLine(message, *pos, kLineTypeTiming, std::string(),
2036 error);
2037 }
2038
2039 while (GetLineWithType(message, pos, &line, kLineTypeRepeatTimes)) {
2040 // By pass zero or more r lines.
2041 }
2042
2043 // Go through the rest of the time descriptions
2044 while (GetLineWithType(message, pos, &line, kLineTypeTiming)) {
2045 while (GetLineWithType(message, pos, &line, kLineTypeRepeatTimes)) {
2046 // By pass zero or more r lines.
2047 }
2048 }
2049
2050 // RFC 4566
2051 // z=* (time zone adjustments)
2052 GetLineWithType(message, pos, &line, kLineTypeTimeZone);
2053
2054 // RFC 4566
2055 // k=* (encryption key)
2056 GetLineWithType(message, pos, &line, kLineTypeEncryptionKey);
2057
2058 // RFC 4566
2059 // a=* (zero or more session attribute lines)
2060 while (GetLineWithType(message, pos, &line, kLineTypeAttributes)) {
2061 if (HasAttribute(line, kAttributeGroup)) {
2062 if (!ParseGroupAttribute(line, desc, error)) {
2063 return false;
2064 }
2065 } else if (HasAttribute(line, kAttributeIceUfrag)) {
2066 if (!GetValue(line, kAttributeIceUfrag,
2067 &(session_td->ice_ufrag), error)) {
2068 return false;
2069 }
2070 } else if (HasAttribute(line, kAttributeIcePwd)) {
2071 if (!GetValue(line, kAttributeIcePwd, &(session_td->ice_pwd), error)) {
2072 return false;
2073 }
2074 } else if (HasAttribute(line, kAttributeIceLite)) {
2075 session_td->ice_mode = cricket::ICEMODE_LITE;
2076 } else if (HasAttribute(line, kAttributeIceOption)) {
2077 if (!ParseIceOptions(line, &(session_td->transport_options), error)) {
2078 return false;
2079 }
2080 } else if (HasAttribute(line, kAttributeFingerprint)) {
2081 if (session_td->identity_fingerprint.get()) {
2082 return ParseFailed(
2083 line,
2084 "Can't have multiple fingerprint attributes at the same level.",
2085 error);
2086 }
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002087 rtc::SSLFingerprint* fingerprint = NULL;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002088 if (!ParseFingerprintAttribute(line, &fingerprint, error)) {
2089 return false;
2090 }
2091 session_td->identity_fingerprint.reset(fingerprint);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00002092 } else if (HasAttribute(line, kAttributeSetup)) {
2093 if (!ParseDtlsSetup(line, &(session_td->connection_role), error)) {
2094 return false;
2095 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002096 } else if (HasAttribute(line, kAttributeMsidSemantics)) {
2097 std::string semantics;
2098 if (!GetValue(line, kAttributeMsidSemantics, &semantics, error)) {
2099 return false;
2100 }
deadbeefc80741f2015-10-22 13:14:45 -07002101 desc->set_msid_supported(
2102 CaseInsensitiveFind(semantics, kMediaStreamSemantic));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002103 } else if (HasAttribute(line, kAttributeExtmap)) {
isheriff6f8d6862016-05-26 11:24:55 -07002104 RtpExtension extmap;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002105 if (!ParseExtmap(line, &extmap, error)) {
2106 return false;
2107 }
2108 session_extmaps->push_back(extmap);
2109 }
2110 }
2111
2112 return true;
2113}
2114
2115bool ParseGroupAttribute(const std::string& line,
2116 cricket::SessionDescription* desc,
2117 SdpParseError* error) {
nisseede5da42017-01-12 05:15:36 -08002118 RTC_DCHECK(desc != NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002119
2120 // RFC 5888 and draft-holmberg-mmusic-sdp-bundle-negotiation-00
2121 // a=group:BUNDLE video voice
2122 std::vector<std::string> fields;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002123 rtc::split(line.substr(kLinePrefixLength),
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002124 kSdpDelimiterSpace, &fields);
2125 std::string semantics;
2126 if (!GetValue(fields[0], kAttributeGroup, &semantics, error)) {
2127 return false;
2128 }
2129 cricket::ContentGroup group(semantics);
2130 for (size_t i = 1; i < fields.size(); ++i) {
2131 group.AddContentName(fields[i]);
2132 }
2133 desc->AddGroup(group);
2134 return true;
2135}
2136
2137static bool ParseFingerprintAttribute(const std::string& line,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002138 rtc::SSLFingerprint** fingerprint,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002139 SdpParseError* error) {
2140 if (!IsLineType(line, kLineTypeAttributes) ||
2141 !HasAttribute(line, kAttributeFingerprint)) {
2142 return ParseFailedExpectLine(line, 0, kLineTypeAttributes,
2143 kAttributeFingerprint, error);
2144 }
2145
2146 std::vector<std::string> fields;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002147 rtc::split(line.substr(kLinePrefixLength),
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002148 kSdpDelimiterSpace, &fields);
2149 const size_t expected_fields = 2;
2150 if (fields.size() != expected_fields) {
2151 return ParseFailedExpectFieldNum(line, expected_fields, error);
2152 }
2153
2154 // The first field here is "fingerprint:<hash>.
2155 std::string algorithm;
2156 if (!GetValue(fields[0], kAttributeFingerprint, &algorithm, error)) {
2157 return false;
2158 }
2159
2160 // Downcase the algorithm. Note that we don't need to downcase the
2161 // fingerprint because hex_decode can handle upper-case.
2162 std::transform(algorithm.begin(), algorithm.end(), algorithm.begin(),
2163 ::tolower);
2164
2165 // The second field is the digest value. De-hexify it.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002166 *fingerprint = rtc::SSLFingerprint::CreateFromRfc4572(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002167 algorithm, fields[1]);
2168 if (!*fingerprint) {
2169 return ParseFailed(line,
2170 "Failed to create fingerprint from the digest.",
2171 error);
2172 }
2173
2174 return true;
2175}
2176
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00002177static bool ParseDtlsSetup(const std::string& line,
2178 cricket::ConnectionRole* role,
2179 SdpParseError* error) {
2180 // setup-attr = "a=setup:" role
2181 // role = "active" / "passive" / "actpass" / "holdconn"
2182 std::vector<std::string> fields;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002183 rtc::split(line.substr(kLinePrefixLength), kSdpDelimiterColon, &fields);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00002184 const size_t expected_fields = 2;
2185 if (fields.size() != expected_fields) {
2186 return ParseFailedExpectFieldNum(line, expected_fields, error);
2187 }
2188 std::string role_str = fields[1];
2189 if (!cricket::StringToConnectionRole(role_str, role)) {
2190 return ParseFailed(line, "Invalid attribute value.", error);
2191 }
2192 return true;
2193}
2194
deadbeef9d3584c2016-02-16 17:54:10 -08002195static bool ParseMsidAttribute(const std::string& line,
2196 std::string* stream_id,
2197 std::string* track_id,
2198 SdpParseError* error) {
2199 // draft-ietf-mmusic-msid-11
2200 // a=msid:<stream id> <track id>
2201 // msid-value = msid-id [ SP msid-appdata ]
2202 // msid-id = 1*64token-char ; see RFC 4566
2203 // msid-appdata = 1*64token-char ; see RFC 4566
2204 std::string field1;
2205 if (!rtc::tokenize_first(line.substr(kLinePrefixLength), kSdpDelimiterSpace,
2206 &field1, track_id)) {
2207 const size_t expected_fields = 2;
2208 return ParseFailedExpectFieldNum(line, expected_fields, error);
2209 }
2210
deadbeefa4549d62017-02-10 17:26:22 -08002211 if (track_id->empty()) {
2212 return ParseFailed(line, "Missing track ID in msid attribute.", error);
2213 }
2214
deadbeef9d3584c2016-02-16 17:54:10 -08002215 // msid:<msid-id>
2216 if (!GetValue(field1, kAttributeMsid, stream_id, error)) {
2217 return false;
2218 }
deadbeefa4549d62017-02-10 17:26:22 -08002219 if (stream_id->empty()) {
2220 return ParseFailed(line, "Missing stream ID in msid attribute.", error);
2221 }
deadbeef9d3584c2016-02-16 17:54:10 -08002222 return true;
2223}
2224
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002225// RFC 3551
2226// PT encoding media type clock rate channels
2227// name (Hz)
2228// 0 PCMU A 8,000 1
2229// 1 reserved A
2230// 2 reserved A
2231// 3 GSM A 8,000 1
2232// 4 G723 A 8,000 1
2233// 5 DVI4 A 8,000 1
2234// 6 DVI4 A 16,000 1
2235// 7 LPC A 8,000 1
2236// 8 PCMA A 8,000 1
2237// 9 G722 A 8,000 1
2238// 10 L16 A 44,100 2
2239// 11 L16 A 44,100 1
2240// 12 QCELP A 8,000 1
2241// 13 CN A 8,000 1
2242// 14 MPA A 90,000 (see text)
2243// 15 G728 A 8,000 1
2244// 16 DVI4 A 11,025 1
2245// 17 DVI4 A 22,050 1
2246// 18 G729 A 8,000 1
2247struct StaticPayloadAudioCodec {
2248 const char* name;
2249 int clockrate;
Peter Kasting69558702016-01-12 16:26:35 -08002250 size_t channels;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002251};
2252static const StaticPayloadAudioCodec kStaticPayloadAudioCodecs[] = {
2253 { "PCMU", 8000, 1 },
2254 { "reserved", 0, 0 },
2255 { "reserved", 0, 0 },
2256 { "GSM", 8000, 1 },
2257 { "G723", 8000, 1 },
2258 { "DVI4", 8000, 1 },
2259 { "DVI4", 16000, 1 },
2260 { "LPC", 8000, 1 },
2261 { "PCMA", 8000, 1 },
2262 { "G722", 8000, 1 },
2263 { "L16", 44100, 2 },
2264 { "L16", 44100, 1 },
2265 { "QCELP", 8000, 1 },
2266 { "CN", 8000, 1 },
2267 { "MPA", 90000, 1 },
2268 { "G728", 8000, 1 },
2269 { "DVI4", 11025, 1 },
2270 { "DVI4", 22050, 1 },
2271 { "G729", 8000, 1 },
2272};
2273
2274void MaybeCreateStaticPayloadAudioCodecs(
2275 const std::vector<int>& fmts, AudioContentDescription* media_desc) {
2276 if (!media_desc) {
2277 return;
2278 }
deadbeef67cf2c12016-04-13 10:07:16 -07002279 RTC_DCHECK(media_desc->codecs().empty());
solenberg9fa49752016-10-08 13:02:44 -07002280 for (int payload_type : fmts) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002281 if (!media_desc->HasCodec(payload_type) &&
2282 payload_type >= 0 &&
kjellander3e33bfe2016-06-20 07:04:09 -07002283 static_cast<uint32_t>(payload_type) <
2284 arraysize(kStaticPayloadAudioCodecs)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002285 std::string encoding_name = kStaticPayloadAudioCodecs[payload_type].name;
2286 int clock_rate = kStaticPayloadAudioCodecs[payload_type].clockrate;
Peter Kasting69558702016-01-12 16:26:35 -08002287 size_t channels = kStaticPayloadAudioCodecs[payload_type].channels;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002288 media_desc->AddCodec(cricket::AudioCodec(payload_type, encoding_name,
deadbeef67cf2c12016-04-13 10:07:16 -07002289 clock_rate, 0, channels));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002290 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002291 }
2292}
2293
2294template <class C>
2295static C* ParseContentDescription(const std::string& message,
2296 const MediaType media_type,
2297 int mline_index,
2298 const std::string& protocol,
deadbeef67cf2c12016-04-13 10:07:16 -07002299 const std::vector<int>& payload_types,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002300 size_t* pos,
2301 std::string* content_name,
deadbeef25ed4352016-12-12 18:37:36 -08002302 bool* bundle_only,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002303 TransportDescription* transport,
2304 std::vector<JsepIceCandidate*>* candidates,
2305 webrtc::SdpParseError* error) {
2306 C* media_desc = new C();
2307 switch (media_type) {
2308 case cricket::MEDIA_TYPE_AUDIO:
2309 *content_name = cricket::CN_AUDIO;
2310 break;
2311 case cricket::MEDIA_TYPE_VIDEO:
2312 *content_name = cricket::CN_VIDEO;
2313 break;
2314 case cricket::MEDIA_TYPE_DATA:
2315 *content_name = cricket::CN_DATA;
2316 break;
2317 default:
nissec80e7412017-01-11 05:56:46 -08002318 RTC_NOTREACHED();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002319 break;
2320 }
deadbeef67cf2c12016-04-13 10:07:16 -07002321 if (!ParseContent(message, media_type, mline_index, protocol, payload_types,
deadbeef25ed4352016-12-12 18:37:36 -08002322 pos, content_name, bundle_only, media_desc, transport,
2323 candidates, error)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002324 delete media_desc;
zhihuang38989e52017-03-21 11:04:53 -07002325 return nullptr;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002326 }
2327 // Sort the codecs according to the m-line fmt list.
deadbeef67cf2c12016-04-13 10:07:16 -07002328 std::unordered_map<int, int> payload_type_preferences;
2329 // "size + 1" so that the lowest preference payload type has a preference of
2330 // 1, which is greater than the default (0) for payload types not in the fmt
2331 // list.
2332 int preference = static_cast<int>(payload_types.size() + 1);
2333 for (int pt : payload_types) {
2334 payload_type_preferences[pt] = preference--;
2335 }
2336 std::vector<typename C::CodecType> codecs = media_desc->codecs();
2337 std::sort(codecs.begin(), codecs.end(), [&payload_type_preferences](
2338 const typename C::CodecType& a,
2339 const typename C::CodecType& b) {
2340 return payload_type_preferences[a.id] > payload_type_preferences[b.id];
2341 });
2342 media_desc->set_codecs(codecs);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002343 return media_desc;
2344}
2345
2346bool ParseMediaDescription(const std::string& message,
2347 const TransportDescription& session_td,
2348 const RtpHeaderExtensions& session_extmaps,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002349 size_t* pos,
zhihuang38989e52017-03-21 11:04:53 -07002350 const rtc::SocketAddress& session_connection_addr,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002351 cricket::SessionDescription* desc,
2352 std::vector<JsepIceCandidate*>* candidates,
2353 SdpParseError* error) {
nisseede5da42017-01-12 05:15:36 -08002354 RTC_DCHECK(desc != NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002355 std::string line;
2356 int mline_index = -1;
2357
2358 // Zero or more media descriptions
2359 // RFC 4566
2360 // m=<media> <port> <proto> <fmt>
2361 while (GetLineWithType(message, pos, &line, kLineTypeMedia)) {
2362 ++mline_index;
2363
2364 std::vector<std::string> fields;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002365 rtc::split(line.substr(kLinePrefixLength),
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002366 kSdpDelimiterSpace, &fields);
zstein4b2e0822017-02-17 19:48:38 -08002367
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002368 const size_t expected_min_fields = 4;
2369 if (fields.size() < expected_min_fields) {
2370 return ParseFailedExpectMinFieldNum(line, expected_min_fields, error);
2371 }
deadbeef25ed4352016-12-12 18:37:36 -08002372 bool port_rejected = false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002373 // RFC 3264
2374 // To reject an offered stream, the port number in the corresponding stream
2375 // in the answer MUST be set to zero.
2376 if (fields[1] == kMediaPortRejected) {
deadbeef25ed4352016-12-12 18:37:36 -08002377 port_rejected = true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002378 }
2379
zhihuang38989e52017-03-21 11:04:53 -07002380 int port = 0;
2381 if (!rtc::FromString<int>(fields[1], &port) || !IsValidPort(port)) {
2382 return ParseFailed(line, "The port number is invalid", error);
2383 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002384 std::string protocol = fields[2];
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002385
2386 // <fmt>
deadbeef67cf2c12016-04-13 10:07:16 -07002387 std::vector<int> payload_types;
pthatcher@webrtc.org3341b402015-02-13 21:14:22 +00002388 if (IsRtp(protocol)) {
jiayl@webrtc.orgddb85ab2014-09-05 16:31:56 +00002389 for (size_t j = 3 ; j < fields.size(); ++j) {
2390 // TODO(wu): Remove when below bug is fixed.
2391 // https://bugzilla.mozilla.org/show_bug.cgi?id=996329
pbosbb36fdf2015-07-09 07:48:14 -07002392 if (fields[j].empty() && j == fields.size() - 1) {
jiayl@webrtc.orgddb85ab2014-09-05 16:31:56 +00002393 continue;
2394 }
wu@webrtc.org36eda7c2014-04-15 20:37:30 +00002395
jiayl@webrtc.orgddb85ab2014-09-05 16:31:56 +00002396 int pl = 0;
pkasting@chromium.orge9facf82015-02-17 20:36:28 +00002397 if (!GetPayloadTypeFromString(line, fields[j], &pl, error)) {
jiayl@webrtc.orgddb85ab2014-09-05 16:31:56 +00002398 return false;
2399 }
deadbeef67cf2c12016-04-13 10:07:16 -07002400 payload_types.push_back(pl);
wu@webrtc.org5e760e72014-04-02 23:19:09 +00002401 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002402 }
2403
2404 // Make a temporary TransportDescription based on |session_td|.
2405 // Some of this gets overwritten by ParseContent.
deadbeef46eed762016-01-28 13:24:37 -08002406 TransportDescription transport(
2407 session_td.transport_options, session_td.ice_ufrag, session_td.ice_pwd,
2408 session_td.ice_mode, session_td.connection_role,
2409 session_td.identity_fingerprint.get());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002410
kwibergd1fe2812016-04-27 06:47:29 -07002411 std::unique_ptr<MediaContentDescription> content;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002412 std::string content_name;
deadbeef25ed4352016-12-12 18:37:36 -08002413 bool bundle_only = false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002414 if (HasAttribute(line, kMediaTypeVideo)) {
2415 content.reset(ParseContentDescription<VideoContentDescription>(
deadbeef67cf2c12016-04-13 10:07:16 -07002416 message, cricket::MEDIA_TYPE_VIDEO, mline_index, protocol,
deadbeef25ed4352016-12-12 18:37:36 -08002417 payload_types, pos, &content_name, &bundle_only, &transport,
2418 candidates, error));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002419 } else if (HasAttribute(line, kMediaTypeAudio)) {
2420 content.reset(ParseContentDescription<AudioContentDescription>(
deadbeef67cf2c12016-04-13 10:07:16 -07002421 message, cricket::MEDIA_TYPE_AUDIO, mline_index, protocol,
deadbeef25ed4352016-12-12 18:37:36 -08002422 payload_types, pos, &content_name, &bundle_only, &transport,
2423 candidates, error));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002424 } else if (HasAttribute(line, kMediaTypeData)) {
jiayl@webrtc.org0e527722014-09-08 21:43:43 +00002425 DataContentDescription* data_desc =
wu@webrtc.org78187522013-10-07 23:32:02 +00002426 ParseContentDescription<DataContentDescription>(
deadbeef67cf2c12016-04-13 10:07:16 -07002427 message, cricket::MEDIA_TYPE_DATA, mline_index, protocol,
deadbeef25ed4352016-12-12 18:37:36 -08002428 payload_types, pos, &content_name, &bundle_only, &transport,
2429 candidates, error);
jiayl@webrtc.org0e527722014-09-08 21:43:43 +00002430 content.reset(data_desc);
wu@webrtc.org78187522013-10-07 23:32:02 +00002431
zstein4b2e0822017-02-17 19:48:38 -08002432 if (data_desc && IsDtlsSctp(protocol)) {
2433 int p;
2434 if (rtc::FromString(fields[3], &p)) {
2435 if (!AddSctpDataCodec(data_desc, p)) {
2436 return false;
2437 }
2438 } else if (fields[3] == kDefaultSctpmapProtocol) {
2439 data_desc->set_use_sctpmap(false);
2440 }
wu@webrtc.org78187522013-10-07 23:32:02 +00002441 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002442 } else {
2443 LOG(LS_WARNING) << "Unsupported media type: " << line;
2444 continue;
2445 }
2446 if (!content.get()) {
2447 // ParseContentDescription returns NULL if failed.
2448 return false;
2449 }
2450
deadbeef25ed4352016-12-12 18:37:36 -08002451 bool content_rejected = false;
deadbeef12771a12017-01-03 13:53:47 -08002452 // A port of 0 is not interpreted as a rejected m= section when it's
2453 // used along with a=bundle-only.
deadbeef25ed4352016-12-12 18:37:36 -08002454 if (bundle_only) {
deadbeef25ed4352016-12-12 18:37:36 -08002455 if (!port_rejected) {
deadbeef12771a12017-01-03 13:53:47 -08002456 // Usage of bundle-only with a nonzero port is unspecified. So just
2457 // ignore bundle-only if we see this.
2458 bundle_only = false;
2459 LOG(LS_WARNING)
2460 << "a=bundle-only attribute observed with a nonzero "
2461 << "port; this usage is unspecified so the attribute is being "
2462 << "ignored.";
deadbeef25ed4352016-12-12 18:37:36 -08002463 }
2464 } else {
2465 // If not using bundle-only, interpret port 0 in the normal way; the m=
2466 // section is being rejected.
2467 content_rejected = port_rejected;
2468 }
2469
pthatcher@webrtc.org3341b402015-02-13 21:14:22 +00002470 if (IsRtp(protocol)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002471 // Set the extmap.
2472 if (!session_extmaps.empty() &&
2473 !content->rtp_header_extensions().empty()) {
2474 return ParseFailed("",
2475 "The a=extmap MUST be either all session level or "
2476 "all media level.",
2477 error);
2478 }
2479 for (size_t i = 0; i < session_extmaps.size(); ++i) {
2480 content->AddRtpHeaderExtension(session_extmaps[i]);
2481 }
2482 }
2483 content->set_protocol(protocol);
zhihuang38989e52017-03-21 11:04:53 -07002484
2485 // Use the session level connection address if the media level addresses are
2486 // not specified.
2487 rtc::SocketAddress address;
2488 address = content->connection_address().IsNil()
2489 ? session_connection_addr
2490 : content->connection_address();
2491 address.SetPort(port);
2492 content->set_connection_address(address);
2493
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002494 desc->AddContent(content_name,
deadbeef25ed4352016-12-12 18:37:36 -08002495 IsDtlsSctp(protocol) ? cricket::NS_JINGLE_DRAFT_SCTP
2496 : cricket::NS_JINGLE_RTP,
2497 content_rejected, bundle_only, content.release());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002498 // Create TransportInfo with the media level "ice-pwd" and "ice-ufrag".
2499 TransportInfo transport_info(content_name, transport);
2500
2501 if (!desc->AddTransportInfo(transport_info)) {
2502 std::ostringstream description;
2503 description << "Failed to AddTransportInfo with content name: "
2504 << content_name;
2505 return ParseFailed("", description.str(), error);
2506 }
2507 }
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00002508
2509 size_t end_of_message = message.size();
2510 if (mline_index == -1 && *pos != end_of_message) {
2511 ParseFailed(message, *pos, "Expects m line.", error);
2512 return false;
2513 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002514 return true;
2515}
2516
2517bool VerifyCodec(const cricket::Codec& codec) {
2518 // Codec has not been populated correctly unless the name has been set. This
2519 // can happen if an SDP has an fmtp or rtcp-fb with a payload type but doesn't
2520 // have a corresponding "rtpmap" line.
htab39db842016-12-08 01:50:48 -08002521 return !codec.name.empty();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002522}
2523
2524bool VerifyAudioCodecs(const AudioContentDescription* audio_desc) {
2525 const std::vector<cricket::AudioCodec>& codecs = audio_desc->codecs();
2526 for (std::vector<cricket::AudioCodec>::const_iterator iter = codecs.begin();
2527 iter != codecs.end(); ++iter) {
2528 if (!VerifyCodec(*iter)) {
2529 return false;
2530 }
2531 }
2532 return true;
2533}
2534
2535bool VerifyVideoCodecs(const VideoContentDescription* video_desc) {
2536 const std::vector<cricket::VideoCodec>& codecs = video_desc->codecs();
2537 for (std::vector<cricket::VideoCodec>::const_iterator iter = codecs.begin();
2538 iter != codecs.end(); ++iter) {
2539 if (!VerifyCodec(*iter)) {
2540 return false;
2541 }
2542 }
2543 return true;
2544}
2545
2546void AddParameters(const cricket::CodecParameterMap& parameters,
2547 cricket::Codec* codec) {
2548 for (cricket::CodecParameterMap::const_iterator iter =
2549 parameters.begin(); iter != parameters.end(); ++iter) {
2550 codec->SetParam(iter->first, iter->second);
2551 }
2552}
2553
2554void AddFeedbackParameter(const cricket::FeedbackParam& feedback_param,
2555 cricket::Codec* codec) {
2556 codec->AddFeedbackParam(feedback_param);
2557}
2558
2559void AddFeedbackParameters(const cricket::FeedbackParams& feedback_params,
2560 cricket::Codec* codec) {
2561 for (std::vector<cricket::FeedbackParam>::const_iterator iter =
2562 feedback_params.params().begin();
2563 iter != feedback_params.params().end(); ++iter) {
2564 codec->AddFeedbackParam(*iter);
2565 }
2566}
2567
2568// Gets the current codec setting associated with |payload_type|. If there
changbin.shao@webrtc.org2d25b442015-03-16 04:14:34 +00002569// is no Codec associated with that payload type it returns an empty codec
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002570// with that payload type.
2571template <class T>
changbin.shao@webrtc.org2d25b442015-03-16 04:14:34 +00002572T GetCodecWithPayloadType(const std::vector<T>& codecs, int payload_type) {
magjedb05fa242016-11-11 04:00:16 -08002573 const T* codec = FindCodecById(codecs, payload_type);
2574 if (codec)
2575 return *codec;
2576 // Return empty codec with |payload_type|.
changbin.shao@webrtc.org2d25b442015-03-16 04:14:34 +00002577 T ret_val;
magjedb05fa242016-11-11 04:00:16 -08002578 ret_val.id = payload_type;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002579 return ret_val;
2580}
2581
2582// Updates or creates a new codec entry in the audio description.
2583template <class T, class U>
2584void AddOrReplaceCodec(MediaContentDescription* content_desc, const U& codec) {
2585 T* desc = static_cast<T*>(content_desc);
2586 std::vector<U> codecs = desc->codecs();
2587 bool found = false;
2588
2589 typename std::vector<U>::iterator iter;
2590 for (iter = codecs.begin(); iter != codecs.end(); ++iter) {
2591 if (iter->id == codec.id) {
2592 *iter = codec;
2593 found = true;
2594 break;
2595 }
2596 }
2597 if (!found) {
2598 desc->AddCodec(codec);
2599 return;
2600 }
2601 desc->set_codecs(codecs);
2602}
2603
2604// Adds or updates existing codec corresponding to |payload_type| according
2605// to |parameters|.
2606template <class T, class U>
2607void UpdateCodec(MediaContentDescription* content_desc, int payload_type,
2608 const cricket::CodecParameterMap& parameters) {
2609 // Codec might already have been populated (from rtpmap).
changbin.shao@webrtc.org2d25b442015-03-16 04:14:34 +00002610 U new_codec = GetCodecWithPayloadType(static_cast<T*>(content_desc)->codecs(),
2611 payload_type);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002612 AddParameters(parameters, &new_codec);
2613 AddOrReplaceCodec<T, U>(content_desc, new_codec);
2614}
2615
2616// Adds or updates existing codec corresponding to |payload_type| according
2617// to |feedback_param|.
2618template <class T, class U>
2619void UpdateCodec(MediaContentDescription* content_desc, int payload_type,
2620 const cricket::FeedbackParam& feedback_param) {
2621 // Codec might already have been populated (from rtpmap).
changbin.shao@webrtc.org2d25b442015-03-16 04:14:34 +00002622 U new_codec = GetCodecWithPayloadType(static_cast<T*>(content_desc)->codecs(),
2623 payload_type);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002624 AddFeedbackParameter(feedback_param, &new_codec);
2625 AddOrReplaceCodec<T, U>(content_desc, new_codec);
2626}
2627
jlmiller@webrtc.orga744a282015-02-18 21:37:46 +00002628template <class T>
2629bool PopWildcardCodec(std::vector<T>* codecs, T* wildcard_codec) {
2630 for (auto iter = codecs->begin(); iter != codecs->end(); ++iter) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002631 if (iter->id == kWildcardPayloadType) {
2632 *wildcard_codec = *iter;
2633 codecs->erase(iter);
2634 return true;
2635 }
2636 }
2637 return false;
2638}
2639
jlmiller@webrtc.orga744a282015-02-18 21:37:46 +00002640template<class T>
2641void UpdateFromWildcardCodecs(cricket::MediaContentDescriptionImpl<T>* desc) {
2642 auto codecs = desc->codecs();
2643 T wildcard_codec;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002644 if (!PopWildcardCodec(&codecs, &wildcard_codec)) {
2645 return;
2646 }
jlmiller@webrtc.orga744a282015-02-18 21:37:46 +00002647 for (auto& codec : codecs) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002648 AddFeedbackParameters(wildcard_codec.feedback_params, &codec);
2649 }
jlmiller@webrtc.orga744a282015-02-18 21:37:46 +00002650 desc->set_codecs(codecs);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002651}
2652
2653void AddAudioAttribute(const std::string& name, const std::string& value,
2654 AudioContentDescription* audio_desc) {
2655 if (value.empty()) {
2656 return;
2657 }
2658 std::vector<cricket::AudioCodec> codecs = audio_desc->codecs();
2659 for (std::vector<cricket::AudioCodec>::iterator iter = codecs.begin();
2660 iter != codecs.end(); ++iter) {
2661 iter->params[name] = value;
2662 }
2663 audio_desc->set_codecs(codecs);
2664}
2665
2666bool ParseContent(const std::string& message,
2667 const MediaType media_type,
2668 int mline_index,
2669 const std::string& protocol,
deadbeef67cf2c12016-04-13 10:07:16 -07002670 const std::vector<int>& payload_types,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002671 size_t* pos,
2672 std::string* content_name,
deadbeef25ed4352016-12-12 18:37:36 -08002673 bool* bundle_only,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002674 MediaContentDescription* media_desc,
2675 TransportDescription* transport,
2676 std::vector<JsepIceCandidate*>* candidates,
2677 SdpParseError* error) {
nisseede5da42017-01-12 05:15:36 -08002678 RTC_DCHECK(media_desc != NULL);
2679 RTC_DCHECK(content_name != NULL);
2680 RTC_DCHECK(transport != NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002681
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +00002682 if (media_type == cricket::MEDIA_TYPE_AUDIO) {
2683 MaybeCreateStaticPayloadAudioCodecs(
deadbeef67cf2c12016-04-13 10:07:16 -07002684 payload_types, static_cast<AudioContentDescription*>(media_desc));
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +00002685 }
2686
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002687 // The media level "ice-ufrag" and "ice-pwd".
2688 // The candidates before update the media level "ice-pwd" and "ice-ufrag".
2689 Candidates candidates_orig;
2690 std::string line;
2691 std::string mline_id;
2692 // Tracks created out of the ssrc attributes.
2693 StreamParamsVec tracks;
2694 SsrcInfoVec ssrc_infos;
2695 SsrcGroupVec ssrc_groups;
2696 std::string maxptime_as_string;
2697 std::string ptime_as_string;
deadbeef9d3584c2016-02-16 17:54:10 -08002698 std::string stream_id;
2699 std::string track_id;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002700
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002701 // Loop until the next m line
2702 while (!IsLineType(message, kLineTypeMedia, *pos)) {
2703 if (!GetLine(message, pos, &line)) {
2704 if (*pos >= message.size()) {
2705 break; // Done parsing
2706 } else {
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +00002707 return ParseFailed(message, *pos, "Invalid SDP line.", error);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002708 }
2709 }
2710
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002711 // RFC 4566
2712 // b=* (zero or more bandwidth information lines)
2713 if (IsLineType(line, kLineTypeSessionBandwidth)) {
2714 std::string bandwidth;
2715 if (HasAttribute(line, kApplicationSpecificMaximum)) {
2716 if (!GetValue(line, kApplicationSpecificMaximum, &bandwidth, error)) {
2717 return false;
2718 } else {
wu@webrtc.org5e760e72014-04-02 23:19:09 +00002719 int b = 0;
2720 if (!GetValueFromString(line, bandwidth, &b, error)) {
2721 return false;
2722 }
Peter Thatcherc0c3a862015-06-24 15:31:25 -07002723 // We should never use more than the default bandwidth for RTP-based
2724 // data channels. Don't allow SDP to set the bandwidth, because
2725 // that would give JS the opportunity to "break the Internet".
2726 // See: https://code.google.com/p/chromium/issues/detail?id=280726
2727 if (media_type == cricket::MEDIA_TYPE_DATA && IsRtp(protocol) &&
2728 b > cricket::kDataMaxBandwidth / 1000) {
2729 std::ostringstream description;
2730 description << "RTP-based data channels may not send more than "
2731 << cricket::kDataMaxBandwidth / 1000 << "kbps.";
2732 return ParseFailed(line, description.str(), error);
2733 }
deadbeefb2362572016-12-13 16:37:06 -08002734 // Prevent integer overflow.
2735 b = std::min(b, INT_MAX / 1000);
wu@webrtc.org5e760e72014-04-02 23:19:09 +00002736 media_desc->set_bandwidth(b * 1000);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002737 }
2738 }
2739 continue;
2740 }
2741
zhihuang38989e52017-03-21 11:04:53 -07002742 // Parse the media level connection data.
2743 if (IsLineType(line, kLineTypeConnection)) {
2744 rtc::SocketAddress addr;
2745 if (!ParseConnectionData(line, &addr, error)) {
2746 return false;
2747 }
2748 media_desc->set_connection_address(addr);
2749 continue;
2750 }
2751
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002752 if (!IsLineType(line, kLineTypeAttributes)) {
2753 // TODO: Handle other lines if needed.
2754 LOG(LS_INFO) << "Ignored line: " << line;
2755 continue;
2756 }
2757
2758 // Handle attributes common to SCTP and RTP.
2759 if (HasAttribute(line, kAttributeMid)) {
2760 // RFC 3388
2761 // mid-attribute = "a=mid:" identification-tag
2762 // identification-tag = token
2763 // Use the mid identification-tag as the content name.
2764 if (!GetValue(line, kAttributeMid, &mline_id, error)) {
2765 return false;
2766 }
2767 *content_name = mline_id;
deadbeef25ed4352016-12-12 18:37:36 -08002768 } else if (HasAttribute(line, kAttributeBundleOnly)) {
2769 *bundle_only = true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002770 } else if (HasAttribute(line, kAttributeCandidate)) {
2771 Candidate candidate;
2772 if (!ParseCandidate(line, &candidate, error, false)) {
2773 return false;
2774 }
deadbeef7bcdb692017-01-20 12:43:58 -08002775 // ParseCandidate will parse non-standard ufrag and password attributes,
2776 // since it's used for candidate trickling, but we only want to process
2777 // the "a=ice-ufrag"/"a=ice-pwd" values in a session description, so
2778 // strip them off at this point.
2779 candidate.set_username(std::string());
2780 candidate.set_password(std::string());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002781 candidates_orig.push_back(candidate);
2782 } else if (HasAttribute(line, kAttributeIceUfrag)) {
2783 if (!GetValue(line, kAttributeIceUfrag, &transport->ice_ufrag, error)) {
2784 return false;
2785 }
2786 } else if (HasAttribute(line, kAttributeIcePwd)) {
2787 if (!GetValue(line, kAttributeIcePwd, &transport->ice_pwd, error)) {
2788 return false;
2789 }
2790 } else if (HasAttribute(line, kAttributeIceOption)) {
2791 if (!ParseIceOptions(line, &transport->transport_options, error)) {
2792 return false;
2793 }
2794 } else if (HasAttribute(line, kAttributeFmtp)) {
2795 if (!ParseFmtpAttributes(line, media_type, media_desc, error)) {
2796 return false;
2797 }
2798 } else if (HasAttribute(line, kAttributeFingerprint)) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002799 rtc::SSLFingerprint* fingerprint = NULL;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002800
2801 if (!ParseFingerprintAttribute(line, &fingerprint, error)) {
2802 return false;
2803 }
2804 transport->identity_fingerprint.reset(fingerprint);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00002805 } else if (HasAttribute(line, kAttributeSetup)) {
2806 if (!ParseDtlsSetup(line, &(transport->connection_role), error)) {
2807 return false;
2808 }
pthatcher@webrtc.org3341b402015-02-13 21:14:22 +00002809 } else if (IsDtlsSctp(protocol) && HasAttribute(line, kAttributeSctpPort)) {
deadbeef7e146cb2016-09-28 10:04:34 -07002810 if (media_type != cricket::MEDIA_TYPE_DATA) {
2811 return ParseFailed(
2812 line, "sctp-port attribute found in non-data media description.",
2813 error);
2814 }
jiayl@webrtc.orgddb85ab2014-09-05 16:31:56 +00002815 int sctp_port;
2816 if (!ParseSctpPort(line, &sctp_port, error)) {
2817 return false;
2818 }
2819 if (!AddSctpDataCodec(static_cast<DataContentDescription*>(media_desc),
2820 sctp_port)) {
2821 return false;
2822 }
pthatcher@webrtc.org3341b402015-02-13 21:14:22 +00002823 } else if (IsRtp(protocol)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002824 //
2825 // RTP specific attrubtes
2826 //
2827 if (HasAttribute(line, kAttributeRtcpMux)) {
2828 media_desc->set_rtcp_mux(true);
deadbeef13871492015-12-09 12:37:51 -08002829 } else if (HasAttribute(line, kAttributeRtcpReducedSize)) {
2830 media_desc->set_rtcp_reduced_size(true);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002831 } else if (HasAttribute(line, kAttributeSsrcGroup)) {
2832 if (!ParseSsrcGroupAttribute(line, &ssrc_groups, error)) {
2833 return false;
2834 }
2835 } else if (HasAttribute(line, kAttributeSsrc)) {
2836 if (!ParseSsrcAttribute(line, &ssrc_infos, error)) {
2837 return false;
2838 }
2839 } else if (HasAttribute(line, kAttributeCrypto)) {
2840 if (!ParseCryptoAttribute(line, media_desc, error)) {
2841 return false;
2842 }
2843 } else if (HasAttribute(line, kAttributeRtpmap)) {
deadbeef67cf2c12016-04-13 10:07:16 -07002844 if (!ParseRtpmapAttribute(line, media_type, payload_types, media_desc,
2845 error)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002846 return false;
2847 }
2848 } else if (HasAttribute(line, kCodecParamMaxPTime)) {
2849 if (!GetValue(line, kCodecParamMaxPTime, &maxptime_as_string, error)) {
2850 return false;
2851 }
2852 } else if (HasAttribute(line, kAttributeRtcpFb)) {
2853 if (!ParseRtcpFbAttribute(line, media_type, media_desc, error)) {
2854 return false;
2855 }
2856 } else if (HasAttribute(line, kCodecParamPTime)) {
2857 if (!GetValue(line, kCodecParamPTime, &ptime_as_string, error)) {
2858 return false;
2859 }
2860 } else if (HasAttribute(line, kAttributeSendOnly)) {
2861 media_desc->set_direction(cricket::MD_SENDONLY);
2862 } else if (HasAttribute(line, kAttributeRecvOnly)) {
2863 media_desc->set_direction(cricket::MD_RECVONLY);
2864 } else if (HasAttribute(line, kAttributeInactive)) {
2865 media_desc->set_direction(cricket::MD_INACTIVE);
2866 } else if (HasAttribute(line, kAttributeSendRecv)) {
2867 media_desc->set_direction(cricket::MD_SENDRECV);
2868 } else if (HasAttribute(line, kAttributeExtmap)) {
isheriff6f8d6862016-05-26 11:24:55 -07002869 RtpExtension extmap;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002870 if (!ParseExtmap(line, &extmap, error)) {
2871 return false;
2872 }
2873 media_desc->AddRtpHeaderExtension(extmap);
2874 } else if (HasAttribute(line, kAttributeXGoogleFlag)) {
2875 // Experimental attribute. Conference mode activates more aggressive
2876 // AEC and NS settings.
2877 // TODO: expose API to set these directly.
2878 std::string flag_value;
2879 if (!GetValue(line, kAttributeXGoogleFlag, &flag_value, error)) {
2880 return false;
2881 }
2882 if (flag_value.compare(kValueConference) == 0)
2883 media_desc->set_conference_mode(true);
deadbeef9d3584c2016-02-16 17:54:10 -08002884 } else if (HasAttribute(line, kAttributeMsid)) {
2885 if (!ParseMsidAttribute(line, &stream_id, &track_id, error)) {
2886 return false;
2887 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002888 }
2889 } else {
2890 // Only parse lines that we are interested of.
2891 LOG(LS_INFO) << "Ignored line: " << line;
2892 continue;
2893 }
2894 }
2895
2896 // Create tracks from the |ssrc_infos|.
Taylor Brandstetter5de6b752016-03-09 17:02:30 -08002897 // If the stream_id/track_id for all SSRCS are identical, one StreamParams
2898 // will be created in CreateTracksFromSsrcInfos, containing all the SSRCs from
2899 // the m= section.
2900 CreateTracksFromSsrcInfos(ssrc_infos, stream_id, track_id, &tracks);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002901
2902 // Add the ssrc group to the track.
2903 for (SsrcGroupVec::iterator ssrc_group = ssrc_groups.begin();
2904 ssrc_group != ssrc_groups.end(); ++ssrc_group) {
2905 if (ssrc_group->ssrcs.empty()) {
2906 continue;
2907 }
Peter Boström0c4e06b2015-10-07 12:23:21 +02002908 uint32_t ssrc = ssrc_group->ssrcs.front();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002909 for (StreamParamsVec::iterator track = tracks.begin();
2910 track != tracks.end(); ++track) {
2911 if (track->has_ssrc(ssrc)) {
2912 track->ssrc_groups.push_back(*ssrc_group);
2913 }
2914 }
2915 }
2916
2917 // Add the new tracks to the |media_desc|.
deadbeef9d3584c2016-02-16 17:54:10 -08002918 for (StreamParams& track : tracks) {
2919 media_desc->AddStream(track);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002920 }
2921
2922 if (media_type == cricket::MEDIA_TYPE_AUDIO) {
2923 AudioContentDescription* audio_desc =
2924 static_cast<AudioContentDescription*>(media_desc);
jlmiller@webrtc.orga744a282015-02-18 21:37:46 +00002925 UpdateFromWildcardCodecs(audio_desc);
2926
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002927 // Verify audio codec ensures that no audio codec has been populated with
2928 // only fmtp.
2929 if (!VerifyAudioCodecs(audio_desc)) {
2930 return ParseFailed("Failed to parse audio codecs correctly.", error);
2931 }
2932 AddAudioAttribute(kCodecParamMaxPTime, maxptime_as_string, audio_desc);
2933 AddAudioAttribute(kCodecParamPTime, ptime_as_string, audio_desc);
2934 }
2935
2936 if (media_type == cricket::MEDIA_TYPE_VIDEO) {
jlmiller@webrtc.orga744a282015-02-18 21:37:46 +00002937 VideoContentDescription* video_desc =
2938 static_cast<VideoContentDescription*>(media_desc);
2939 UpdateFromWildcardCodecs(video_desc);
2940 // Verify video codec ensures that no video codec has been populated with
2941 // only rtcp-fb.
2942 if (!VerifyVideoCodecs(video_desc)) {
2943 return ParseFailed("Failed to parse video codecs correctly.", error);
2944 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002945 }
2946
2947 // RFC 5245
2948 // Update the candidates with the media level "ice-pwd" and "ice-ufrag".
2949 for (Candidates::iterator it = candidates_orig.begin();
2950 it != candidates_orig.end(); ++it) {
nisseede5da42017-01-12 05:15:36 -08002951 RTC_DCHECK((*it).username().empty() ||
2952 (*it).username() == transport->ice_ufrag);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002953 (*it).set_username(transport->ice_ufrag);
nisseede5da42017-01-12 05:15:36 -08002954 RTC_DCHECK((*it).password().empty());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002955 (*it).set_password(transport->ice_pwd);
2956 candidates->push_back(
2957 new JsepIceCandidate(mline_id, mline_index, *it));
2958 }
2959 return true;
2960}
2961
2962bool ParseSsrcAttribute(const std::string& line, SsrcInfoVec* ssrc_infos,
2963 SdpParseError* error) {
nisseede5da42017-01-12 05:15:36 -08002964 RTC_DCHECK(ssrc_infos != NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002965 // RFC 5576
2966 // a=ssrc:<ssrc-id> <attribute>
2967 // a=ssrc:<ssrc-id> <attribute>:<value>
2968 std::string field1, field2;
Donald Curtis144d0182015-05-15 13:14:24 -07002969 if (!rtc::tokenize_first(line.substr(kLinePrefixLength), kSdpDelimiterSpace,
2970 &field1, &field2)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002971 const size_t expected_fields = 2;
2972 return ParseFailedExpectFieldNum(line, expected_fields, error);
2973 }
2974
2975 // ssrc:<ssrc-id>
2976 std::string ssrc_id_s;
2977 if (!GetValue(field1, kAttributeSsrc, &ssrc_id_s, error)) {
2978 return false;
2979 }
Peter Boström0c4e06b2015-10-07 12:23:21 +02002980 uint32_t ssrc_id = 0;
wu@webrtc.org5e760e72014-04-02 23:19:09 +00002981 if (!GetValueFromString(line, ssrc_id_s, &ssrc_id, error)) {
2982 return false;
2983 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002984
2985 std::string attribute;
2986 std::string value;
Donald Curtis144d0182015-05-15 13:14:24 -07002987 if (!rtc::tokenize_first(field2, kSdpDelimiterColon, &attribute, &value)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002988 std::ostringstream description;
2989 description << "Failed to get the ssrc attribute value from " << field2
2990 << ". Expected format <attribute>:<value>.";
2991 return ParseFailed(line, description.str(), error);
2992 }
2993
2994 // Check if there's already an item for this |ssrc_id|. Create a new one if
2995 // there isn't.
2996 SsrcInfoVec::iterator ssrc_info = ssrc_infos->begin();
2997 for (; ssrc_info != ssrc_infos->end(); ++ssrc_info) {
2998 if (ssrc_info->ssrc_id == ssrc_id) {
2999 break;
3000 }
3001 }
3002 if (ssrc_info == ssrc_infos->end()) {
3003 SsrcInfo info;
3004 info.ssrc_id = ssrc_id;
3005 ssrc_infos->push_back(info);
3006 ssrc_info = ssrc_infos->end() - 1;
3007 }
3008
3009 // Store the info to the |ssrc_info|.
3010 if (attribute == kSsrcAttributeCname) {
3011 // RFC 5576
3012 // cname:<value>
3013 ssrc_info->cname = value;
3014 } else if (attribute == kSsrcAttributeMsid) {
3015 // draft-alvestrand-mmusic-msid-00
3016 // "msid:" identifier [ " " appdata ]
3017 std::vector<std::string> fields;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00003018 rtc::split(value, kSdpDelimiterSpace, &fields);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003019 if (fields.size() < 1 || fields.size() > 2) {
3020 return ParseFailed(line,
3021 "Expected format \"msid:<identifier>[ <appdata>]\".",
3022 error);
3023 }
deadbeef9d3584c2016-02-16 17:54:10 -08003024 ssrc_info->stream_id = fields[0];
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003025 if (fields.size() == 2) {
deadbeef9d3584c2016-02-16 17:54:10 -08003026 ssrc_info->track_id = fields[1];
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003027 }
3028 } else if (attribute == kSsrcAttributeMslabel) {
3029 // draft-alvestrand-rtcweb-mid-01
3030 // mslabel:<value>
3031 ssrc_info->mslabel = value;
3032 } else if (attribute == kSSrcAttributeLabel) {
3033 // The label isn't defined.
3034 // label:<value>
3035 ssrc_info->label = value;
3036 }
3037 return true;
3038}
3039
3040bool ParseSsrcGroupAttribute(const std::string& line,
3041 SsrcGroupVec* ssrc_groups,
3042 SdpParseError* error) {
nisseede5da42017-01-12 05:15:36 -08003043 RTC_DCHECK(ssrc_groups != NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003044 // RFC 5576
3045 // a=ssrc-group:<semantics> <ssrc-id> ...
3046 std::vector<std::string> fields;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00003047 rtc::split(line.substr(kLinePrefixLength),
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003048 kSdpDelimiterSpace, &fields);
3049 const size_t expected_min_fields = 2;
3050 if (fields.size() < expected_min_fields) {
3051 return ParseFailedExpectMinFieldNum(line, expected_min_fields, error);
3052 }
3053 std::string semantics;
3054 if (!GetValue(fields[0], kAttributeSsrcGroup, &semantics, error)) {
3055 return false;
3056 }
Peter Boström0c4e06b2015-10-07 12:23:21 +02003057 std::vector<uint32_t> ssrcs;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003058 for (size_t i = 1; i < fields.size(); ++i) {
Peter Boström0c4e06b2015-10-07 12:23:21 +02003059 uint32_t ssrc = 0;
wu@webrtc.org5e760e72014-04-02 23:19:09 +00003060 if (!GetValueFromString(line, fields[i], &ssrc, error)) {
3061 return false;
3062 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003063 ssrcs.push_back(ssrc);
3064 }
3065 ssrc_groups->push_back(SsrcGroup(semantics, ssrcs));
3066 return true;
3067}
3068
3069bool ParseCryptoAttribute(const std::string& line,
3070 MediaContentDescription* media_desc,
3071 SdpParseError* error) {
3072 std::vector<std::string> fields;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00003073 rtc::split(line.substr(kLinePrefixLength),
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003074 kSdpDelimiterSpace, &fields);
3075 // RFC 4568
3076 // a=crypto:<tag> <crypto-suite> <key-params> [<session-params>]
3077 const size_t expected_min_fields = 3;
3078 if (fields.size() < expected_min_fields) {
3079 return ParseFailedExpectMinFieldNum(line, expected_min_fields, error);
3080 }
3081 std::string tag_value;
3082 if (!GetValue(fields[0], kAttributeCrypto, &tag_value, error)) {
3083 return false;
3084 }
wu@webrtc.org5e760e72014-04-02 23:19:09 +00003085 int tag = 0;
3086 if (!GetValueFromString(line, tag_value, &tag, error)) {
3087 return false;
3088 }
jbauch083b73f2015-07-16 02:46:32 -07003089 const std::string& crypto_suite = fields[1];
3090 const std::string& key_params = fields[2];
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003091 std::string session_params;
3092 if (fields.size() > 3) {
3093 session_params = fields[3];
3094 }
3095 media_desc->AddCrypto(CryptoParams(tag, crypto_suite, key_params,
3096 session_params));
3097 return true;
3098}
3099
3100// Updates or creates a new codec entry in the audio description with according
deadbeef67cf2c12016-04-13 10:07:16 -07003101// to |name|, |clockrate|, |bitrate|, and |channels|.
3102void UpdateCodec(int payload_type,
3103 const std::string& name,
3104 int clockrate,
3105 int bitrate,
3106 size_t channels,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003107 AudioContentDescription* audio_desc) {
3108 // Codec may already be populated with (only) optional parameters
3109 // (from an fmtp).
changbin.shao@webrtc.org2d25b442015-03-16 04:14:34 +00003110 cricket::AudioCodec codec =
3111 GetCodecWithPayloadType(audio_desc->codecs(), payload_type);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003112 codec.name = name;
3113 codec.clockrate = clockrate;
3114 codec.bitrate = bitrate;
3115 codec.channels = channels;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003116 AddOrReplaceCodec<AudioContentDescription, cricket::AudioCodec>(audio_desc,
3117 codec);
3118}
3119
3120// Updates or creates a new codec entry in the video description according to
deadbeef67cf2c12016-04-13 10:07:16 -07003121// |name|, |width|, |height|, and |framerate|.
3122void UpdateCodec(int payload_type,
3123 const std::string& name,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003124 VideoContentDescription* video_desc) {
3125 // Codec may already be populated with (only) optional parameters
3126 // (from an fmtp).
changbin.shao@webrtc.org2d25b442015-03-16 04:14:34 +00003127 cricket::VideoCodec codec =
3128 GetCodecWithPayloadType(video_desc->codecs(), payload_type);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003129 codec.name = name;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003130 AddOrReplaceCodec<VideoContentDescription, cricket::VideoCodec>(video_desc,
3131 codec);
3132}
3133
3134bool ParseRtpmapAttribute(const std::string& line,
3135 const MediaType media_type,
deadbeef67cf2c12016-04-13 10:07:16 -07003136 const std::vector<int>& payload_types,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003137 MediaContentDescription* media_desc,
3138 SdpParseError* error) {
3139 std::vector<std::string> fields;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00003140 rtc::split(line.substr(kLinePrefixLength),
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003141 kSdpDelimiterSpace, &fields);
3142 // RFC 4566
3143 // a=rtpmap:<payload type> <encoding name>/<clock rate>[/<encodingparameters>]
3144 const size_t expected_min_fields = 2;
3145 if (fields.size() < expected_min_fields) {
3146 return ParseFailedExpectMinFieldNum(line, expected_min_fields, error);
3147 }
3148 std::string payload_type_value;
3149 if (!GetValue(fields[0], kAttributeRtpmap, &payload_type_value, error)) {
3150 return false;
3151 }
wu@webrtc.org5e760e72014-04-02 23:19:09 +00003152 int payload_type = 0;
pkasting@chromium.orge9facf82015-02-17 20:36:28 +00003153 if (!GetPayloadTypeFromString(line, payload_type_value, &payload_type,
3154 error)) {
wu@webrtc.org5e760e72014-04-02 23:19:09 +00003155 return false;
3156 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003157
deadbeef67cf2c12016-04-13 10:07:16 -07003158 if (std::find(payload_types.begin(), payload_types.end(), payload_type) ==
3159 payload_types.end()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003160 LOG(LS_WARNING) << "Ignore rtpmap line that did not appear in the "
3161 << "<fmt> of the m-line: " << line;
3162 return true;
3163 }
jbauch083b73f2015-07-16 02:46:32 -07003164 const std::string& encoder = fields[1];
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003165 std::vector<std::string> codec_params;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00003166 rtc::split(encoder, '/', &codec_params);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003167 // <encoding name>/<clock rate>[/<encodingparameters>]
3168 // 2 mandatory fields
3169 if (codec_params.size() < 2 || codec_params.size() > 3) {
3170 return ParseFailed(line,
3171 "Expected format \"<encoding name>/<clock rate>"
3172 "[/<encodingparameters>]\".",
3173 error);
3174 }
jbauch083b73f2015-07-16 02:46:32 -07003175 const std::string& encoding_name = codec_params[0];
wu@webrtc.org5e760e72014-04-02 23:19:09 +00003176 int clock_rate = 0;
3177 if (!GetValueFromString(line, codec_params[1], &clock_rate, error)) {
3178 return false;
3179 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003180 if (media_type == cricket::MEDIA_TYPE_VIDEO) {
3181 VideoContentDescription* video_desc =
3182 static_cast<VideoContentDescription*>(media_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003183 UpdateCodec(payload_type, encoding_name,
deadbeef67cf2c12016-04-13 10:07:16 -07003184 video_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003185 } else if (media_type == cricket::MEDIA_TYPE_AUDIO) {
3186 // RFC 4566
3187 // For audio streams, <encoding parameters> indicates the number
3188 // of audio channels. This parameter is OPTIONAL and may be
3189 // omitted if the number of channels is one, provided that no
3190 // additional parameters are needed.
Peter Kasting69558702016-01-12 16:26:35 -08003191 size_t channels = 1;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003192 if (codec_params.size() == 3) {
wu@webrtc.org5e760e72014-04-02 23:19:09 +00003193 if (!GetValueFromString(line, codec_params[2], &channels, error)) {
3194 return false;
3195 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003196 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003197 AudioContentDescription* audio_desc =
3198 static_cast<AudioContentDescription*>(media_desc);
ossue1405ad2017-01-23 08:55:48 -08003199 UpdateCodec(payload_type, encoding_name, clock_rate, 0, channels,
deadbeef67cf2c12016-04-13 10:07:16 -07003200 audio_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003201 } else if (media_type == cricket::MEDIA_TYPE_DATA) {
3202 DataContentDescription* data_desc =
3203 static_cast<DataContentDescription*>(media_desc);
deadbeef67cf2c12016-04-13 10:07:16 -07003204 data_desc->AddCodec(cricket::DataCodec(payload_type, encoding_name));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003205 }
3206 return true;
3207}
3208
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003209bool ParseFmtpParam(const std::string& line, std::string* parameter,
3210 std::string* value, SdpParseError* error) {
Donald Curtis0e07f922015-05-15 09:21:23 -07003211 if (!rtc::tokenize_first(line, kSdpDelimiterEqual, parameter, value)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003212 ParseFailed(line, "Unable to parse fmtp parameter. \'=\' missing.", error);
3213 return false;
3214 }
3215 // a=fmtp:<payload_type> <param1>=<value1>; <param2>=<value2>; ...
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003216 return true;
3217}
3218
3219bool ParseFmtpAttributes(const std::string& line, const MediaType media_type,
3220 MediaContentDescription* media_desc,
3221 SdpParseError* error) {
3222 if (media_type != cricket::MEDIA_TYPE_AUDIO &&
3223 media_type != cricket::MEDIA_TYPE_VIDEO) {
3224 return true;
3225 }
Donald Curtis0e07f922015-05-15 09:21:23 -07003226
3227 std::string line_payload;
3228 std::string line_params;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003229
3230 // RFC 5576
3231 // a=fmtp:<format> <format specific parameters>
3232 // At least two fields, whereas the second one is any of the optional
3233 // parameters.
Donald Curtis144d0182015-05-15 13:14:24 -07003234 if (!rtc::tokenize_first(line.substr(kLinePrefixLength), kSdpDelimiterSpace,
3235 &line_payload, &line_params)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003236 ParseFailedExpectMinFieldNum(line, 2, error);
3237 return false;
3238 }
3239
Donald Curtis0e07f922015-05-15 09:21:23 -07003240 // Parse out the payload information.
pkasting@chromium.orgd3245462015-02-23 21:28:22 +00003241 std::string payload_type_str;
Donald Curtis0e07f922015-05-15 09:21:23 -07003242 if (!GetValue(line_payload, kAttributeFmtp, &payload_type_str, error)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003243 return false;
3244 }
3245
Donald Curtis0e07f922015-05-15 09:21:23 -07003246 int payload_type = 0;
Donald Curtis144d0182015-05-15 13:14:24 -07003247 if (!GetPayloadTypeFromString(line_payload, payload_type_str, &payload_type,
3248 error)) {
Donald Curtis0e07f922015-05-15 09:21:23 -07003249 return false;
3250 }
3251
3252 // Parse out format specific parameters.
3253 std::vector<std::string> fields;
3254 rtc::split(line_params, kSdpDelimiterSemicolon, &fields);
3255
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003256 cricket::CodecParameterMap codec_params;
Donald Curtis144d0182015-05-15 13:14:24 -07003257 for (auto& iter : fields) {
Donald Curtis0e07f922015-05-15 09:21:23 -07003258 if (iter.find(kSdpDelimiterEqual) == std::string::npos) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003259 // Only fmtps with equals are currently supported. Other fmtp types
3260 // should be ignored. Unknown fmtps do not constitute an error.
3261 continue;
3262 }
Donald Curtis0e07f922015-05-15 09:21:23 -07003263
3264 std::string name;
3265 std::string value;
3266 if (!ParseFmtpParam(rtc::string_trim(iter), &name, &value, error)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003267 return false;
3268 }
3269 codec_params[name] = value;
3270 }
3271
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003272 if (media_type == cricket::MEDIA_TYPE_AUDIO) {
3273 UpdateCodec<AudioContentDescription, cricket::AudioCodec>(
pkasting@chromium.orgd3245462015-02-23 21:28:22 +00003274 media_desc, payload_type, codec_params);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003275 } else if (media_type == cricket::MEDIA_TYPE_VIDEO) {
3276 UpdateCodec<VideoContentDescription, cricket::VideoCodec>(
pkasting@chromium.orgd3245462015-02-23 21:28:22 +00003277 media_desc, payload_type, codec_params);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003278 }
3279 return true;
3280}
3281
3282bool ParseRtcpFbAttribute(const std::string& line, const MediaType media_type,
3283 MediaContentDescription* media_desc,
3284 SdpParseError* error) {
3285 if (media_type != cricket::MEDIA_TYPE_AUDIO &&
3286 media_type != cricket::MEDIA_TYPE_VIDEO) {
3287 return true;
3288 }
3289 std::vector<std::string> rtcp_fb_fields;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00003290 rtc::split(line.c_str(), kSdpDelimiterSpace, &rtcp_fb_fields);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003291 if (rtcp_fb_fields.size() < 2) {
3292 return ParseFailedGetValue(line, kAttributeRtcpFb, error);
3293 }
3294 std::string payload_type_string;
3295 if (!GetValue(rtcp_fb_fields[0], kAttributeRtcpFb, &payload_type_string,
3296 error)) {
3297 return false;
3298 }
wu@webrtc.org5e760e72014-04-02 23:19:09 +00003299 int payload_type = kWildcardPayloadType;
3300 if (payload_type_string != "*") {
pkasting@chromium.orge9facf82015-02-17 20:36:28 +00003301 if (!GetPayloadTypeFromString(line, payload_type_string, &payload_type,
3302 error)) {
wu@webrtc.org5e760e72014-04-02 23:19:09 +00003303 return false;
3304 }
3305 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003306 std::string id = rtcp_fb_fields[1];
3307 std::string param = "";
3308 for (std::vector<std::string>::iterator iter = rtcp_fb_fields.begin() + 2;
3309 iter != rtcp_fb_fields.end(); ++iter) {
3310 param.append(*iter);
3311 }
3312 const cricket::FeedbackParam feedback_param(id, param);
3313
3314 if (media_type == cricket::MEDIA_TYPE_AUDIO) {
pkasting@chromium.orgd3245462015-02-23 21:28:22 +00003315 UpdateCodec<AudioContentDescription, cricket::AudioCodec>(
3316 media_desc, payload_type, feedback_param);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003317 } else if (media_type == cricket::MEDIA_TYPE_VIDEO) {
pkasting@chromium.orgd3245462015-02-23 21:28:22 +00003318 UpdateCodec<VideoContentDescription, cricket::VideoCodec>(
3319 media_desc, payload_type, feedback_param);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003320 }
3321 return true;
3322}
3323
3324} // namespace webrtc