blob: 13d09a6ee96efd33219e6e7df89ba5983cb49d7e [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);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000252static bool ParseSessionDescription(const std::string& message, size_t* pos,
253 std::string* session_id,
254 std::string* session_version,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000255 TransportDescription* session_td,
256 RtpHeaderExtensions* session_extmaps,
257 cricket::SessionDescription* desc,
258 SdpParseError* error);
259static bool ParseGroupAttribute(const std::string& line,
260 cricket::SessionDescription* desc,
261 SdpParseError* error);
262static bool ParseMediaDescription(
263 const std::string& message,
264 const TransportDescription& session_td,
265 const RtpHeaderExtensions& session_extmaps,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000266 size_t* pos, cricket::SessionDescription* desc,
267 std::vector<JsepIceCandidate*>* candidates,
268 SdpParseError* error);
269static bool ParseContent(const std::string& message,
270 const MediaType media_type,
271 int mline_index,
272 const std::string& protocol,
deadbeef67cf2c12016-04-13 10:07:16 -0700273 const std::vector<int>& payload_types,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000274 size_t* pos,
275 std::string* content_name,
deadbeef25ed4352016-12-12 18:37:36 -0800276 bool* bundle_only,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000277 MediaContentDescription* media_desc,
278 TransportDescription* transport,
279 std::vector<JsepIceCandidate*>* candidates,
280 SdpParseError* error);
281static bool ParseSsrcAttribute(const std::string& line,
282 SsrcInfoVec* ssrc_infos,
283 SdpParseError* error);
284static bool ParseSsrcGroupAttribute(const std::string& line,
285 SsrcGroupVec* ssrc_groups,
286 SdpParseError* error);
287static bool ParseCryptoAttribute(const std::string& line,
288 MediaContentDescription* media_desc,
289 SdpParseError* error);
290static bool ParseRtpmapAttribute(const std::string& line,
291 const MediaType media_type,
deadbeef67cf2c12016-04-13 10:07:16 -0700292 const std::vector<int>& payload_types,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000293 MediaContentDescription* media_desc,
294 SdpParseError* error);
295static bool ParseFmtpAttributes(const std::string& line,
296 const MediaType media_type,
297 MediaContentDescription* media_desc,
298 SdpParseError* error);
299static bool ParseFmtpParam(const std::string& line, std::string* parameter,
300 std::string* value, SdpParseError* error);
301static bool ParseCandidate(const std::string& message, Candidate* candidate,
302 SdpParseError* error, bool is_raw);
303static bool ParseRtcpFbAttribute(const std::string& line,
304 const MediaType media_type,
305 MediaContentDescription* media_desc,
306 SdpParseError* error);
307static bool ParseIceOptions(const std::string& line,
308 std::vector<std::string>* transport_options,
309 SdpParseError* error);
310static bool ParseExtmap(const std::string& line,
isheriff6f8d6862016-05-26 11:24:55 -0700311 RtpExtension* extmap,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000312 SdpParseError* error);
313static bool ParseFingerprintAttribute(const std::string& line,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000314 rtc::SSLFingerprint** fingerprint,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000315 SdpParseError* error);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +0000316static bool ParseDtlsSetup(const std::string& line,
317 cricket::ConnectionRole* role,
318 SdpParseError* error);
deadbeef9d3584c2016-02-16 17:54:10 -0800319static bool ParseMsidAttribute(const std::string& line,
320 std::string* stream_id,
321 std::string* track_id,
322 SdpParseError* error);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000323
324// Helper functions
325
326// Below ParseFailed*** functions output the line that caused the parsing
327// failure and the detailed reason (|description|) of the failure to |error|.
328// The functions always return false so that they can be used directly in the
329// following way when error happens:
330// "return ParseFailed***(...);"
331
332// The line starting at |line_start| of |message| is the failing line.
333// The reason for the failure should be provided in the |description|.
334// An example of a description could be "unknown character".
335static bool ParseFailed(const std::string& message,
336 size_t line_start,
337 const std::string& description,
338 SdpParseError* error) {
339 // Get the first line of |message| from |line_start|.
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +0000340 std::string first_line;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000341 size_t line_end = message.find(kNewLine, line_start);
342 if (line_end != std::string::npos) {
343 if (line_end > 0 && (message.at(line_end - 1) == kReturn)) {
344 --line_end;
345 }
346 first_line = message.substr(line_start, (line_end - line_start));
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +0000347 } else {
348 first_line = message.substr(line_start);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000349 }
350
351 if (error) {
352 error->line = first_line;
353 error->description = description;
354 }
355 LOG(LS_ERROR) << "Failed to parse: \"" << first_line
356 << "\". Reason: " << description;
357 return false;
358}
359
360// |line| is the failing line. The reason for the failure should be
361// provided in the |description|.
362static bool ParseFailed(const std::string& line,
363 const std::string& description,
364 SdpParseError* error) {
365 return ParseFailed(line, 0, description, error);
366}
367
368// Parses failure where the failing SDP line isn't know or there are multiple
369// failing lines.
370static bool ParseFailed(const std::string& description,
371 SdpParseError* error) {
372 return ParseFailed("", description, error);
373}
374
375// |line| is the failing line. The failure is due to the fact that |line|
376// doesn't have |expected_fields| fields.
377static bool ParseFailedExpectFieldNum(const std::string& line,
378 int expected_fields,
379 SdpParseError* error) {
380 std::ostringstream description;
381 description << "Expects " << expected_fields << " fields.";
382 return ParseFailed(line, description.str(), error);
383}
384
385// |line| is the failing line. The failure is due to the fact that |line| has
386// less than |expected_min_fields| fields.
387static bool ParseFailedExpectMinFieldNum(const std::string& line,
388 int expected_min_fields,
389 SdpParseError* error) {
390 std::ostringstream description;
391 description << "Expects at least " << expected_min_fields << " fields.";
392 return ParseFailed(line, description.str(), error);
393}
394
395// |line| is the failing line. The failure is due to the fact that it failed to
396// get the value of |attribute|.
397static bool ParseFailedGetValue(const std::string& line,
398 const std::string& attribute,
399 SdpParseError* error) {
400 std::ostringstream description;
401 description << "Failed to get the value of attribute: " << attribute;
402 return ParseFailed(line, description.str(), error);
403}
404
405// The line starting at |line_start| of |message| is the failing line. The
406// failure is due to the line type (e.g. the "m" part of the "m-line")
407// not matching what is expected. The expected line type should be
408// provided as |line_type|.
409static bool ParseFailedExpectLine(const std::string& message,
410 size_t line_start,
411 const char line_type,
412 const std::string& line_value,
413 SdpParseError* error) {
414 std::ostringstream description;
415 description << "Expect line: " << line_type << "=" << line_value;
416 return ParseFailed(message, line_start, description.str(), error);
417}
418
419static bool AddLine(const std::string& line, std::string* message) {
420 if (!message)
421 return false;
422
423 message->append(line);
424 message->append(kLineBreak);
425 return true;
426}
427
428static bool GetLine(const std::string& message,
429 size_t* pos,
430 std::string* line) {
431 size_t line_begin = *pos;
432 size_t line_end = message.find(kNewLine, line_begin);
433 if (line_end == std::string::npos) {
434 return false;
435 }
436 // Update the new start position
437 *pos = line_end + 1;
438 if (line_end > 0 && (message.at(line_end - 1) == kReturn)) {
439 --line_end;
440 }
441 *line = message.substr(line_begin, (line_end - line_begin));
442 const char* cline = line->c_str();
443 // RFC 4566
444 // An SDP session description consists of a number of lines of text of
445 // the form:
446 // <type>=<value>
447 // where <type> MUST be exactly one case-significant character and
448 // <value> is structured text whose format depends on <type>.
449 // Whitespace MUST NOT be used on either side of the "=" sign.
decurtis@webrtc.org8af11042015-01-07 19:15:51 +0000450 if (line->length() < 3 ||
451 !islower(cline[0]) ||
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000452 cline[1] != kSdpDelimiterEqual ||
453 cline[2] == kSdpDelimiterSpace) {
454 *pos = line_begin;
455 return false;
456 }
457 return true;
458}
459
460// Init |os| to "|type|=|value|".
461static void InitLine(const char type,
462 const std::string& value,
463 std::ostringstream* os) {
464 os->str("");
465 *os << type << kSdpDelimiterEqual << value;
466}
467
468// Init |os| to "a=|attribute|".
469static void InitAttrLine(const std::string& attribute, std::ostringstream* os) {
470 InitLine(kLineTypeAttributes, attribute, os);
471}
472
473// Writes a SDP attribute line based on |attribute| and |value| to |message|.
474static void AddAttributeLine(const std::string& attribute, int value,
475 std::string* message) {
476 std::ostringstream os;
477 InitAttrLine(attribute, &os);
478 os << kSdpDelimiterColon << value;
479 AddLine(os.str(), message);
480}
481
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000482static bool IsLineType(const std::string& message,
483 const char type,
484 size_t line_start) {
485 if (message.size() < line_start + kLinePrefixLength) {
486 return false;
487 }
488 const char* cmessage = message.c_str();
489 return (cmessage[line_start] == type &&
490 cmessage[line_start + 1] == kSdpDelimiterEqual);
491}
492
493static bool IsLineType(const std::string& line,
494 const char type) {
495 return IsLineType(line, type, 0);
496}
497
498static bool GetLineWithType(const std::string& message, size_t* pos,
499 std::string* line, const char type) {
500 if (!IsLineType(message, type, *pos)) {
501 return false;
502 }
503
504 if (!GetLine(message, pos, line))
505 return false;
506
507 return true;
508}
509
510static bool HasAttribute(const std::string& line,
511 const std::string& attribute) {
512 return (line.compare(kLinePrefixLength, attribute.size(), attribute) == 0);
513}
514
Peter Boström0c4e06b2015-10-07 12:23:21 +0200515static bool AddSsrcLine(uint32_t ssrc_id,
516 const std::string& attribute,
517 const std::string& value,
518 std::string* message) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000519 // RFC 5576
520 // a=ssrc:<ssrc-id> <attribute>:<value>
521 std::ostringstream os;
522 InitAttrLine(kAttributeSsrc, &os);
523 os << kSdpDelimiterColon << ssrc_id << kSdpDelimiterSpace
524 << attribute << kSdpDelimiterColon << value;
525 return AddLine(os.str(), message);
526}
527
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000528// Get value only from <attribute>:<value>.
529static bool GetValue(const std::string& message, const std::string& attribute,
530 std::string* value, SdpParseError* error) {
531 std::string leftpart;
Donald Curtis0e07f922015-05-15 09:21:23 -0700532 if (!rtc::tokenize_first(message, kSdpDelimiterColon, &leftpart, value)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000533 return ParseFailedGetValue(message, attribute, error);
534 }
535 // The left part should end with the expected attribute.
536 if (leftpart.length() < attribute.length() ||
537 leftpart.compare(leftpart.length() - attribute.length(),
538 attribute.length(), attribute) != 0) {
539 return ParseFailedGetValue(message, attribute, error);
540 }
541 return true;
542}
543
544static bool CaseInsensitiveFind(std::string str1, std::string str2) {
545 std::transform(str1.begin(), str1.end(), str1.begin(),
546 ::tolower);
547 std::transform(str2.begin(), str2.end(), str2.begin(),
548 ::tolower);
549 return str1.find(str2) != std::string::npos;
550}
551
wu@webrtc.org5e760e72014-04-02 23:19:09 +0000552template <class T>
553static bool GetValueFromString(const std::string& line,
554 const std::string& s,
555 T* t,
556 SdpParseError* error) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000557 if (!rtc::FromString(s, t)) {
wu@webrtc.org5e760e72014-04-02 23:19:09 +0000558 std::ostringstream description;
559 description << "Invalid value: " << s << ".";
560 return ParseFailed(line, description.str(), error);
561 }
562 return true;
563}
564
pkasting@chromium.orge9facf82015-02-17 20:36:28 +0000565static bool GetPayloadTypeFromString(const std::string& line,
566 const std::string& s,
567 int* payload_type,
568 SdpParseError* error) {
569 return GetValueFromString(line, s, payload_type, error) &&
570 cricket::IsValidRtpPayloadType(*payload_type);
571}
572
Taylor Brandstetter5de6b752016-03-09 17:02:30 -0800573// |msid_stream_id| and |msid_track_id| represent the stream/track ID from the
574// "a=msid" attribute, if it exists. They are empty if the attribute does not
575// exist.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000576void CreateTracksFromSsrcInfos(const SsrcInfoVec& ssrc_infos,
Taylor Brandstetter5de6b752016-03-09 17:02:30 -0800577 const std::string& msid_stream_id,
578 const std::string& msid_track_id,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000579 StreamParamsVec* tracks) {
nisseede5da42017-01-12 05:15:36 -0800580 RTC_DCHECK(tracks != NULL);
581 RTC_DCHECK(msid_stream_id.empty() == msid_track_id.empty());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000582 for (SsrcInfoVec::const_iterator ssrc_info = ssrc_infos.begin();
583 ssrc_info != ssrc_infos.end(); ++ssrc_info) {
584 if (ssrc_info->cname.empty()) {
585 continue;
586 }
587
Taylor Brandstetter5de6b752016-03-09 17:02:30 -0800588 std::string stream_id;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000589 std::string track_id;
Taylor Brandstetter5de6b752016-03-09 17:02:30 -0800590 if (ssrc_info->stream_id.empty() && !ssrc_info->mslabel.empty()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000591 // If there's no msid and there's mslabel, we consider this is a sdp from
592 // a older version of client that doesn't support msid.
593 // In that case, we use the mslabel and label to construct the track.
Taylor Brandstetter5de6b752016-03-09 17:02:30 -0800594 stream_id = ssrc_info->mslabel;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000595 track_id = ssrc_info->label;
Taylor Brandstetter5de6b752016-03-09 17:02:30 -0800596 } else if (ssrc_info->stream_id.empty() && !msid_stream_id.empty()) {
597 // If there's no msid in the SSRC attributes, but there's a global one
598 // (from a=msid), use that. This is the case with unified plan SDP.
599 stream_id = msid_stream_id;
600 track_id = msid_track_id;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000601 } else {
Taylor Brandstetter5de6b752016-03-09 17:02:30 -0800602 stream_id = ssrc_info->stream_id;
deadbeef9d3584c2016-02-16 17:54:10 -0800603 track_id = ssrc_info->track_id;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000604 }
Taylor Brandstetter5de6b752016-03-09 17:02:30 -0800605 // If a stream/track ID wasn't populated from the SSRC attributes OR the
606 // msid attribute, use default/random values.
607 if (stream_id.empty()) {
608 stream_id = kDefaultMsid;
609 }
610 if (track_id.empty()) {
611 // TODO(ronghuawu): What should we do if the track id doesn't appear?
612 // Create random string (which will be used as track label later)?
613 track_id = rtc::CreateRandomString(8);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000614 }
615
616 StreamParamsVec::iterator track = tracks->begin();
617 for (; track != tracks->end(); ++track) {
618 if (track->id == track_id) {
619 break;
620 }
621 }
622 if (track == tracks->end()) {
623 // If we don't find an existing track, create a new one.
624 tracks->push_back(StreamParams());
625 track = tracks->end() - 1;
626 }
627 track->add_ssrc(ssrc_info->ssrc_id);
628 track->cname = ssrc_info->cname;
Taylor Brandstetter5de6b752016-03-09 17:02:30 -0800629 track->sync_label = stream_id;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000630 track->id = track_id;
631 }
632}
633
634void GetMediaStreamLabels(const ContentInfo* content,
635 std::set<std::string>* labels) {
636 const MediaContentDescription* media_desc =
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000637 static_cast<const MediaContentDescription*>(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000638 content->description);
639 const cricket::StreamParamsVec& streams = media_desc->streams();
640 for (cricket::StreamParamsVec::const_iterator it = streams.begin();
641 it != streams.end(); ++it) {
642 labels->insert(it->sync_label);
643 }
644}
645
646// RFC 5245
647// It is RECOMMENDED that default candidates be chosen based on the
648// likelihood of those candidates to work with the peer that is being
649// contacted. It is RECOMMENDED that relayed > reflexive > host.
650static const int kPreferenceUnknown = 0;
651static const int kPreferenceHost = 1;
652static const int kPreferenceReflexive = 2;
653static const int kPreferenceRelayed = 3;
654
655static int GetCandidatePreferenceFromType(const std::string& type) {
656 int preference = kPreferenceUnknown;
657 if (type == cricket::LOCAL_PORT_TYPE) {
658 preference = kPreferenceHost;
659 } else if (type == cricket::STUN_PORT_TYPE) {
660 preference = kPreferenceReflexive;
661 } else if (type == cricket::RELAY_PORT_TYPE) {
662 preference = kPreferenceRelayed;
663 } else {
nissec80e7412017-01-11 05:56:46 -0800664 RTC_NOTREACHED();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000665 }
666 return preference;
667}
668
guoweis@webrtc.org57ac2c82015-02-06 00:45:13 +0000669// Get ip and port of the default destination from the |candidates| with the
670// given value of |component_id|. The default candidate should be the one most
671// likely to work, typically IPv4 relay.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000672// RFC 5245
673// The value of |component_id| currently supported are 1 (RTP) and 2 (RTCP).
674// TODO: Decide the default destination in webrtcsession and
675// pass it down via SessionDescription.
pthatcher@webrtc.orgc9d6d142014-10-23 23:37:22 +0000676static void GetDefaultDestination(
677 const std::vector<Candidate>& candidates,
678 int component_id, std::string* port,
679 std::string* ip, std::string* addr_type) {
perkj@webrtc.orgd105cc82014-11-07 11:22:06 +0000680 *addr_type = kConnectionIpv4Addrtype;
pthatcher@webrtc.orgc9d6d142014-10-23 23:37:22 +0000681 *port = kDummyPort;
682 *ip = kDummyAddress;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000683 int current_preference = kPreferenceUnknown;
guoweis@webrtc.org57ac2c82015-02-06 00:45:13 +0000684 int current_family = AF_UNSPEC;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000685 for (std::vector<Candidate>::const_iterator it = candidates.begin();
686 it != candidates.end(); ++it) {
687 if (it->component() != component_id) {
688 continue;
689 }
guoweis@webrtc.org57ac2c82015-02-06 00:45:13 +0000690 // Default destination should be UDP only.
691 if (it->protocol() != cricket::UDP_PROTOCOL_NAME) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000692 continue;
693 }
guoweis@webrtc.org57ac2c82015-02-06 00:45:13 +0000694 const int preference = GetCandidatePreferenceFromType(it->type());
695 const int family = it->address().ipaddr().family();
696 // See if this candidate is more preferable then the current one if it's the
697 // same family. Or if the current family is IPv4 already so we could safely
698 // ignore all IPv6 ones. WebRTC bug 4269.
699 // http://code.google.com/p/webrtc/issues/detail?id=4269
700 if ((preference <= current_preference && current_family == family) ||
701 (current_family == AF_INET && family == AF_INET6)) {
702 continue;
703 }
pthatcher@webrtc.orgc9d6d142014-10-23 23:37:22 +0000704 if (family == AF_INET) {
705 addr_type->assign(kConnectionIpv4Addrtype);
706 } else if (family == AF_INET6) {
707 addr_type->assign(kConnectionIpv6Addrtype);
708 }
guoweis@webrtc.org57ac2c82015-02-06 00:45:13 +0000709 current_preference = preference;
710 current_family = family;
711 *port = it->address().PortAsString();
712 *ip = it->address().ipaddr().ToString();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000713 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000714}
715
wu@webrtc.org4c3e9912014-07-16 21:03:13 +0000716// Update |mline|'s default destination and append a c line after it.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000717static void UpdateMediaDefaultDestination(
wu@webrtc.org4c3e9912014-07-16 21:03:13 +0000718 const std::vector<Candidate>& candidates,
jbauch083b73f2015-07-16 02:46:32 -0700719 const std::string& mline,
wu@webrtc.org4c3e9912014-07-16 21:03:13 +0000720 std::string* message) {
721 std::string new_lines;
722 AddLine(mline, &new_lines);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000723 // RFC 4566
724 // m=<media> <port> <proto> <fmt> ...
725 std::vector<std::string> fields;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000726 rtc::split(mline, kSdpDelimiterSpace, &fields);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000727 if (fields.size() < 3) {
728 return;
729 }
730
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000731 std::ostringstream os;
pthatcher@webrtc.orgc9d6d142014-10-23 23:37:22 +0000732 std::string rtp_port, rtp_ip, addr_type;
733 GetDefaultDestination(candidates, ICE_CANDIDATE_COMPONENT_RTP,
734 &rtp_port, &rtp_ip, &addr_type);
735 // Found default RTP candidate.
736 // RFC 5245
737 // The default candidates are added to the SDP as the default
738 // destination for media. For streams based on RTP, this is done by
739 // placing the IP address and port of the RTP candidate into the c and m
740 // lines, respectively.
741 // Update the port in the m line.
742 // If this is a m-line with port equal to 0, we don't change it.
743 if (fields[1] != kMediaPortRejected) {
744 new_lines.replace(fields[0].size() + 1,
745 fields[1].size(),
746 rtp_port);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000747 }
pthatcher@webrtc.orgc9d6d142014-10-23 23:37:22 +0000748 // Add the c line.
749 // RFC 4566
750 // c=<nettype> <addrtype> <connection-address>
751 InitLine(kLineTypeConnection, kConnectionNettype, &os);
752 os << " " << addr_type << " " << rtp_ip;
753 AddLine(os.str(), &new_lines);
wu@webrtc.org4c3e9912014-07-16 21:03:13 +0000754 message->append(new_lines);
755}
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000756
wu@webrtc.org4c3e9912014-07-16 21:03:13 +0000757// Gets "a=rtcp" line if found default RTCP candidate from |candidates|.
758static std::string GetRtcpLine(const std::vector<Candidate>& candidates) {
pthatcher@webrtc.orgc9d6d142014-10-23 23:37:22 +0000759 std::string rtcp_line, rtcp_port, rtcp_ip, addr_type;
760 GetDefaultDestination(candidates, ICE_CANDIDATE_COMPONENT_RTCP,
761 &rtcp_port, &rtcp_ip, &addr_type);
762 // Found default RTCP candidate.
763 // RFC 5245
764 // If the agent is utilizing RTCP, it MUST encode the RTCP candidate
765 // using the a=rtcp attribute as defined in RFC 3605.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000766
pthatcher@webrtc.orgc9d6d142014-10-23 23:37:22 +0000767 // RFC 3605
768 // rtcp-attribute = "a=rtcp:" port [nettype space addrtype space
769 // connection-address] CRLF
770 std::ostringstream os;
771 InitAttrLine(kAttributeRtcp, &os);
772 os << kSdpDelimiterColon
773 << rtcp_port << " "
774 << kConnectionNettype << " "
775 << addr_type << " "
776 << rtcp_ip;
777 rtcp_line = os.str();
wu@webrtc.org4c3e9912014-07-16 21:03:13 +0000778 return rtcp_line;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000779}
780
781// Get candidates according to the mline index from SessionDescriptionInterface.
782static void GetCandidatesByMindex(const SessionDescriptionInterface& desci,
783 int mline_index,
784 std::vector<Candidate>* candidates) {
785 if (!candidates) {
786 return;
787 }
788 const IceCandidateCollection* cc = desci.candidates(mline_index);
789 for (size_t i = 0; i < cc->count(); ++i) {
790 const IceCandidateInterface* candidate = cc->at(i);
791 candidates->push_back(candidate->candidate());
792 }
793}
794
deadbeef90f1e1e2017-02-10 12:35:05 -0800795static bool IsValidPort(int port) {
796 return port >= 0 && port <= 65535;
797}
798
deadbeef9d3584c2016-02-16 17:54:10 -0800799std::string SdpSerialize(const JsepSessionDescription& jdesc,
800 bool unified_plan_sdp) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000801 const cricket::SessionDescription* desc = jdesc.description();
802 if (!desc) {
803 return "";
804 }
805
806 std::string message;
807
808 // Session Description.
809 AddLine(kSessionVersion, &message);
810 // Session Origin
811 // RFC 4566
812 // o=<username> <sess-id> <sess-version> <nettype> <addrtype>
813 // <unicast-address>
814 std::ostringstream os;
815 InitLine(kLineTypeOrigin, kSessionOriginUsername, &os);
jbauch083b73f2015-07-16 02:46:32 -0700816 const std::string& session_id = jdesc.session_id().empty() ?
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000817 kSessionOriginSessionId : jdesc.session_id();
jbauch083b73f2015-07-16 02:46:32 -0700818 const std::string& session_version = jdesc.session_version().empty() ?
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000819 kSessionOriginSessionVersion : jdesc.session_version();
820 os << " " << session_id << " " << session_version << " "
821 << kSessionOriginNettype << " " << kSessionOriginAddrtype << " "
822 << kSessionOriginAddress;
823 AddLine(os.str(), &message);
824 AddLine(kSessionName, &message);
825
826 // Time Description.
827 AddLine(kTimeDescription, &message);
828
829 // Group
830 if (desc->HasGroup(cricket::GROUP_TYPE_BUNDLE)) {
831 std::string group_line = kAttrGroup;
832 const cricket::ContentGroup* group =
833 desc->GetGroupByName(cricket::GROUP_TYPE_BUNDLE);
nisseede5da42017-01-12 05:15:36 -0800834 RTC_DCHECK(group != NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000835 const cricket::ContentNames& content_names = group->content_names();
836 for (cricket::ContentNames::const_iterator it = content_names.begin();
837 it != content_names.end(); ++it) {
838 group_line.append(" ");
839 group_line.append(*it);
840 }
841 AddLine(group_line, &message);
842 }
843
844 // MediaStream semantics
845 InitAttrLine(kAttributeMsidSemantics, &os);
846 os << kSdpDelimiterColon << " " << kMediaStreamSemantic;
wu@webrtc.org4c3e9912014-07-16 21:03:13 +0000847
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000848 std::set<std::string> media_stream_labels;
849 const ContentInfo* audio_content = GetFirstAudioContent(desc);
850 if (audio_content)
851 GetMediaStreamLabels(audio_content, &media_stream_labels);
wu@webrtc.org4c3e9912014-07-16 21:03:13 +0000852
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000853 const ContentInfo* video_content = GetFirstVideoContent(desc);
854 if (video_content)
855 GetMediaStreamLabels(video_content, &media_stream_labels);
wu@webrtc.org4c3e9912014-07-16 21:03:13 +0000856
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000857 for (std::set<std::string>::const_iterator it =
858 media_stream_labels.begin(); it != media_stream_labels.end(); ++it) {
859 os << " " << *it;
860 }
861 AddLine(os.str(), &message);
862
wu@webrtc.org4c3e9912014-07-16 21:03:13 +0000863 // Preserve the order of the media contents.
864 int mline_index = -1;
865 for (cricket::ContentInfos::const_iterator it = desc->contents().begin();
866 it != desc->contents().end(); ++it) {
867 const MediaContentDescription* mdesc =
868 static_cast<const MediaContentDescription*>(it->description);
869 std::vector<Candidate> candidates;
870 GetCandidatesByMindex(jdesc, ++mline_index, &candidates);
deadbeef9d3584c2016-02-16 17:54:10 -0800871 BuildMediaDescription(&*it, desc->GetTransportInfoByName(it->name),
872 mdesc->type(), candidates, unified_plan_sdp,
wu@webrtc.org4c3e9912014-07-16 21:03:13 +0000873 &message);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000874 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000875 return message;
876}
877
878// Serializes the passed in IceCandidateInterface to a SDP string.
879// candidate - The candidate to be serialized.
Honghai Zhang7fb69db2016-03-14 11:59:18 -0700880std::string SdpSerializeCandidate(const IceCandidateInterface& candidate) {
881 return SdpSerializeCandidate(candidate.candidate());
882}
883
884// Serializes a cricket Candidate.
885std::string SdpSerializeCandidate(const cricket::Candidate& candidate) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000886 std::string message;
Honghai Zhang7fb69db2016-03-14 11:59:18 -0700887 std::vector<cricket::Candidate> candidates(1, candidate);
honghaiza54a0802015-12-16 18:37:23 -0800888 BuildCandidate(candidates, true, &message);
wu@webrtc.orgec9f5fb2014-06-24 17:05:10 +0000889 // From WebRTC draft section 4.8.1.1 candidate-attribute will be
890 // just candidate:<candidate> not a=candidate:<blah>CRLF
nisseede5da42017-01-12 05:15:36 -0800891 RTC_DCHECK(message.find("a=") == 0);
wu@webrtc.orgec9f5fb2014-06-24 17:05:10 +0000892 message.erase(0, 2);
nisseede5da42017-01-12 05:15:36 -0800893 RTC_DCHECK(message.find(kLineBreak) == message.size() - 2);
wu@webrtc.orgec9f5fb2014-06-24 17:05:10 +0000894 message.resize(message.size() - 2);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000895 return message;
896}
897
898bool SdpDeserialize(const std::string& message,
899 JsepSessionDescription* jdesc,
900 SdpParseError* error) {
901 std::string session_id;
902 std::string session_version;
Peter Thatcher7cbd1882015-09-17 18:54:52 -0700903 TransportDescription session_td("", "");
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000904 RtpHeaderExtensions session_extmaps;
905 cricket::SessionDescription* desc = new cricket::SessionDescription();
906 std::vector<JsepIceCandidate*> candidates;
907 size_t current_pos = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000908
909 // Session Description
910 if (!ParseSessionDescription(message, &current_pos, &session_id,
deadbeefc80741f2015-10-22 13:14:45 -0700911 &session_version, &session_td, &session_extmaps,
912 desc, error)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000913 delete desc;
914 return false;
915 }
916
917 // Media Description
deadbeefc80741f2015-10-22 13:14:45 -0700918 if (!ParseMediaDescription(message, session_td, session_extmaps, &current_pos,
919 desc, &candidates, error)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000920 delete desc;
921 for (std::vector<JsepIceCandidate*>::const_iterator
922 it = candidates.begin(); it != candidates.end(); ++it) {
923 delete *it;
924 }
925 return false;
926 }
927
928 jdesc->Initialize(desc, session_id, session_version);
929
930 for (std::vector<JsepIceCandidate*>::const_iterator
931 it = candidates.begin(); it != candidates.end(); ++it) {
932 jdesc->AddCandidate(*it);
933 delete *it;
934 }
935 return true;
936}
937
938bool SdpDeserializeCandidate(const std::string& message,
939 JsepIceCandidate* jcandidate,
940 SdpParseError* error) {
nisseede5da42017-01-12 05:15:36 -0800941 RTC_DCHECK(jcandidate != NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000942 Candidate candidate;
943 if (!ParseCandidate(message, &candidate, error, true)) {
944 return false;
945 }
946 jcandidate->SetCandidate(candidate);
947 return true;
948}
949
Honghai Zhang7fb69db2016-03-14 11:59:18 -0700950bool SdpDeserializeCandidate(const std::string& transport_name,
951 const std::string& message,
952 cricket::Candidate* candidate,
953 SdpParseError* error) {
nisseede5da42017-01-12 05:15:36 -0800954 RTC_DCHECK(candidate != nullptr);
Honghai Zhang7fb69db2016-03-14 11:59:18 -0700955 if (!ParseCandidate(message, candidate, error, true)) {
956 return false;
957 }
958 candidate->set_transport_name(transport_name);
959 return true;
960}
961
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000962bool ParseCandidate(const std::string& message, Candidate* candidate,
963 SdpParseError* error, bool is_raw) {
nisseede5da42017-01-12 05:15:36 -0800964 RTC_DCHECK(candidate != NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000965
966 // Get the first line from |message|.
jiayl@webrtc.org7ec3f9f2014-08-08 23:09:15 +0000967 std::string first_line = message;
968 size_t pos = 0;
969 GetLine(message, &pos, &first_line);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000970
jiayl@webrtc.org7ec3f9f2014-08-08 23:09:15 +0000971 // Makes sure |message| contains only one line.
972 if (message.size() > first_line.size()) {
973 std::string left, right;
Donald Curtis0e07f922015-05-15 09:21:23 -0700974 if (rtc::tokenize_first(message, kNewLine, &left, &right) &&
975 !right.empty()) {
jiayl@webrtc.org7ec3f9f2014-08-08 23:09:15 +0000976 return ParseFailed(message, 0, "Expect one line only", error);
977 }
978 }
979
980 // From WebRTC draft section 4.8.1.1 candidate-attribute should be
981 // candidate:<candidate> when trickled, but we still support
982 // a=candidate:<blah>CRLF for backward compatibility and for parsing a line
983 // from the SDP.
984 if (IsLineType(first_line, kLineTypeAttributes)) {
985 first_line = first_line.substr(kLinePrefixLength);
986 }
987
988 std::string attribute_candidate;
989 std::string candidate_value;
990
991 // |first_line| must be in the form of "candidate:<value>".
Donald Curtis144d0182015-05-15 13:14:24 -0700992 if (!rtc::tokenize_first(first_line, kSdpDelimiterColon, &attribute_candidate,
993 &candidate_value) ||
jiayl@webrtc.org7ec3f9f2014-08-08 23:09:15 +0000994 attribute_candidate != kAttributeCandidate) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000995 if (is_raw) {
996 std::ostringstream description;
jiayl@webrtc.org7ec3f9f2014-08-08 23:09:15 +0000997 description << "Expect line: " << kAttributeCandidate
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000998 << ":" << "<candidate-str>";
999 return ParseFailed(first_line, 0, description.str(), error);
1000 } else {
1001 return ParseFailedExpectLine(first_line, 0, kLineTypeAttributes,
1002 kAttributeCandidate, error);
1003 }
1004 }
1005
1006 std::vector<std::string> fields;
jiayl@webrtc.org7ec3f9f2014-08-08 23:09:15 +00001007 rtc::split(candidate_value, kSdpDelimiterSpace, &fields);
1008
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001009 // RFC 5245
1010 // a=candidate:<foundation> <component-id> <transport> <priority>
1011 // <connection-address> <port> typ <candidate-types>
1012 // [raddr <connection-address>] [rport <port>]
1013 // *(SP extension-att-name SP extension-att-value)
1014 const size_t expected_min_fields = 8;
1015 if (fields.size() < expected_min_fields ||
1016 (fields[6] != kAttributeCandidateTyp)) {
1017 return ParseFailedExpectMinFieldNum(first_line, expected_min_fields, error);
1018 }
jbauch083b73f2015-07-16 02:46:32 -07001019 const std::string& foundation = fields[0];
jiayl@webrtc.org7ec3f9f2014-08-08 23:09:15 +00001020
wu@webrtc.org5e760e72014-04-02 23:19:09 +00001021 int component_id = 0;
1022 if (!GetValueFromString(first_line, fields[1], &component_id, error)) {
1023 return false;
1024 }
jbauch083b73f2015-07-16 02:46:32 -07001025 const std::string& transport = fields[2];
Peter Boström0c4e06b2015-10-07 12:23:21 +02001026 uint32_t priority = 0;
wu@webrtc.org5e760e72014-04-02 23:19:09 +00001027 if (!GetValueFromString(first_line, fields[3], &priority, error)) {
1028 return false;
1029 }
jbauch083b73f2015-07-16 02:46:32 -07001030 const std::string& connection_address = fields[4];
wu@webrtc.org5e760e72014-04-02 23:19:09 +00001031 int port = 0;
1032 if (!GetValueFromString(first_line, fields[5], &port, error)) {
1033 return false;
1034 }
deadbeef90f1e1e2017-02-10 12:35:05 -08001035 if (!IsValidPort(port)) {
1036 return ParseFailed(first_line, "Invalid port number.", error);
1037 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001038 SocketAddress address(connection_address, port);
1039
1040 cricket::ProtocolType protocol;
hnslb68cc752016-12-13 10:33:41 -08001041 if (!StringToProto(transport.c_str(), &protocol)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001042 return ParseFailed(first_line, "Unsupported transport type.", error);
1043 }
hnslb68cc752016-12-13 10:33:41 -08001044 switch (protocol) {
1045 case cricket::PROTO_UDP:
1046 case cricket::PROTO_TCP:
1047 case cricket::PROTO_SSLTCP:
1048 // Supported protocol.
1049 break;
1050 default:
1051 return ParseFailed(first_line, "Unsupported transport type.", error);
1052 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001053
1054 std::string candidate_type;
jbauch083b73f2015-07-16 02:46:32 -07001055 const std::string& type = fields[7];
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001056 if (type == kCandidateHost) {
1057 candidate_type = cricket::LOCAL_PORT_TYPE;
1058 } else if (type == kCandidateSrflx) {
1059 candidate_type = cricket::STUN_PORT_TYPE;
1060 } else if (type == kCandidateRelay) {
1061 candidate_type = cricket::RELAY_PORT_TYPE;
Honghai Zhang7fb69db2016-03-14 11:59:18 -07001062 } else if (type == kCandidatePrflx) {
1063 candidate_type = cricket::PRFLX_PORT_TYPE;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001064 } else {
1065 return ParseFailed(first_line, "Unsupported candidate type.", error);
1066 }
1067
1068 size_t current_position = expected_min_fields;
1069 SocketAddress related_address;
1070 // The 2 optional fields for related address
1071 // [raddr <connection-address>] [rport <port>]
1072 if (fields.size() >= (current_position + 2) &&
1073 fields[current_position] == kAttributeCandidateRaddr) {
1074 related_address.SetIP(fields[++current_position]);
1075 ++current_position;
1076 }
1077 if (fields.size() >= (current_position + 2) &&
1078 fields[current_position] == kAttributeCandidateRport) {
wu@webrtc.org5e760e72014-04-02 23:19:09 +00001079 int port = 0;
1080 if (!GetValueFromString(
1081 first_line, fields[++current_position], &port, error)) {
1082 return false;
1083 }
deadbeef90f1e1e2017-02-10 12:35:05 -08001084 if (!IsValidPort(port)) {
1085 return ParseFailed(first_line, "Invalid port number.", error);
1086 }
wu@webrtc.org5e760e72014-04-02 23:19:09 +00001087 related_address.SetPort(port);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001088 ++current_position;
1089 }
1090
mallinath@webrtc.org2d60c5e2014-08-08 22:29:20 +00001091 // If this is a TCP candidate, it has additional extension as defined in
1092 // RFC 6544.
1093 std::string tcptype;
1094 if (fields.size() >= (current_position + 2) &&
1095 fields[current_position] == kTcpCandidateType) {
1096 tcptype = fields[++current_position];
1097 ++current_position;
1098
1099 if (tcptype != cricket::TCPTYPE_ACTIVE_STR &&
1100 tcptype != cricket::TCPTYPE_PASSIVE_STR &&
1101 tcptype != cricket::TCPTYPE_SIMOPEN_STR) {
1102 return ParseFailed(first_line, "Invalid TCP candidate type.", error);
1103 }
1104
1105 if (protocol != cricket::PROTO_TCP) {
1106 return ParseFailed(first_line, "Invalid non-TCP candidate", error);
1107 }
1108 }
1109
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001110 // Extension
honghaiza54a0802015-12-16 18:37:23 -08001111 // Though non-standard, we support the ICE ufrag and pwd being signaled on
1112 // the candidate to avoid issues with confusing which generation a candidate
1113 // belongs to when trickling multiple generations at the same time.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001114 std::string username;
1115 std::string password;
Peter Boström0c4e06b2015-10-07 12:23:21 +02001116 uint32_t generation = 0;
honghaiza0c44ea2016-03-23 16:07:48 -07001117 uint16_t network_id = 0;
1118 uint16_t network_cost = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001119 for (size_t i = current_position; i + 1 < fields.size(); ++i) {
1120 // RFC 5245
1121 // *(SP extension-att-name SP extension-att-value)
1122 if (fields[i] == kAttributeCandidateGeneration) {
wu@webrtc.org5e760e72014-04-02 23:19:09 +00001123 if (!GetValueFromString(first_line, fields[++i], &generation, error)) {
1124 return false;
1125 }
honghaiza54a0802015-12-16 18:37:23 -08001126 } else if (fields[i] == kAttributeCandidateUfrag) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001127 username = fields[++i];
honghaiza54a0802015-12-16 18:37:23 -08001128 } else if (fields[i] == kAttributeCandidatePwd) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001129 password = fields[++i];
honghaiza0c44ea2016-03-23 16:07:48 -07001130 } else if (fields[i] == kAttributeCandidateNetworkId) {
1131 if (!GetValueFromString(first_line, fields[++i], &network_id, error)) {
1132 return false;
1133 }
honghaize1a0c942016-02-16 14:54:56 -08001134 } else if (fields[i] == kAttributeCandidateNetworkCost) {
1135 if (!GetValueFromString(first_line, fields[++i], &network_cost, error)) {
1136 return false;
1137 }
Honghai Zhang351d77b2016-05-20 15:08:29 -07001138 network_cost = std::min(network_cost, rtc::kNetworkCostMax);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001139 } else {
1140 // Skip the unknown extension.
1141 ++i;
1142 }
1143 }
1144
guoweis@webrtc.org61c12472015-01-15 06:53:07 +00001145 *candidate = Candidate(component_id, cricket::ProtoToString(protocol),
guoweis@webrtc.org950c5182014-12-16 23:01:31 +00001146 address, priority, username, password, candidate_type,
honghaiza0c44ea2016-03-23 16:07:48 -07001147 generation, foundation, network_id, network_cost);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001148 candidate->set_related_address(related_address);
mallinath@webrtc.org2d60c5e2014-08-08 22:29:20 +00001149 candidate->set_tcptype(tcptype);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001150 return true;
1151}
1152
1153bool ParseIceOptions(const std::string& line,
1154 std::vector<std::string>* transport_options,
1155 SdpParseError* error) {
1156 std::string ice_options;
1157 if (!GetValue(line, kAttributeIceOption, &ice_options, error)) {
1158 return false;
1159 }
1160 std::vector<std::string> fields;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001161 rtc::split(ice_options, kSdpDelimiterSpace, &fields);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001162 for (size_t i = 0; i < fields.size(); ++i) {
1163 transport_options->push_back(fields[i]);
1164 }
1165 return true;
1166}
1167
jiayl@webrtc.orgddb85ab2014-09-05 16:31:56 +00001168bool ParseSctpPort(const std::string& line,
1169 int* sctp_port,
1170 SdpParseError* error) {
1171 // draft-ietf-mmusic-sctp-sdp-07
1172 // a=sctp-port
1173 std::vector<std::string> fields;
jiayl@webrtc.orgddb85ab2014-09-05 16:31:56 +00001174 const size_t expected_min_fields = 2;
lally69f57602015-10-08 10:15:04 -07001175 rtc::split(line.substr(kLinePrefixLength), kSdpDelimiterColon, &fields);
1176 if (fields.size() < expected_min_fields) {
1177 fields.resize(0);
1178 rtc::split(line.substr(kLinePrefixLength), kSdpDelimiterSpace, &fields);
1179 }
jiayl@webrtc.orgddb85ab2014-09-05 16:31:56 +00001180 if (fields.size() < expected_min_fields) {
1181 return ParseFailedExpectMinFieldNum(line, expected_min_fields, error);
1182 }
1183 if (!rtc::FromString(fields[1], sctp_port)) {
lally69f57602015-10-08 10:15:04 -07001184 return ParseFailed(line, "Invalid sctp port value.", error);
jiayl@webrtc.orgddb85ab2014-09-05 16:31:56 +00001185 }
1186 return true;
1187}
1188
isheriff6f8d6862016-05-26 11:24:55 -07001189bool ParseExtmap(const std::string& line,
1190 RtpExtension* extmap,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001191 SdpParseError* error) {
1192 // RFC 5285
1193 // a=extmap:<value>["/"<direction>] <URI> <extensionattributes>
1194 std::vector<std::string> fields;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001195 rtc::split(line.substr(kLinePrefixLength),
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001196 kSdpDelimiterSpace, &fields);
1197 const size_t expected_min_fields = 2;
1198 if (fields.size() < expected_min_fields) {
1199 return ParseFailedExpectMinFieldNum(line, expected_min_fields, error);
1200 }
1201 std::string uri = fields[1];
1202
1203 std::string value_direction;
1204 if (!GetValue(fields[0], kAttributeExtmap, &value_direction, error)) {
1205 return false;
1206 }
1207 std::vector<std::string> sub_fields;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001208 rtc::split(value_direction, kSdpDelimiterSlash, &sub_fields);
wu@webrtc.org5e760e72014-04-02 23:19:09 +00001209 int value = 0;
1210 if (!GetValueFromString(line, sub_fields[0], &value, error)) {
1211 return false;
1212 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001213
isheriff6f8d6862016-05-26 11:24:55 -07001214 *extmap = RtpExtension(uri, value);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001215 return true;
1216}
1217
1218void BuildMediaDescription(const ContentInfo* content_info,
1219 const TransportInfo* transport_info,
1220 const MediaType media_type,
wu@webrtc.org4c3e9912014-07-16 21:03:13 +00001221 const std::vector<Candidate>& candidates,
deadbeef9d3584c2016-02-16 17:54:10 -08001222 bool unified_plan_sdp,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001223 std::string* message) {
nisseede5da42017-01-12 05:15:36 -08001224 RTC_DCHECK(message != NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001225 if (content_info == NULL || message == NULL) {
1226 return;
1227 }
1228 // TODO: Rethink if we should use sprintfn instead of stringstream.
1229 // According to the style guide, streams should only be used for logging.
1230 // http://google-styleguide.googlecode.com/svn/
1231 // trunk/cppguide.xml?showone=Streams#Streams
1232 std::ostringstream os;
1233 const MediaContentDescription* media_desc =
henrike@webrtc.org28654cb2013-07-22 21:07:49 +00001234 static_cast<const MediaContentDescription*>(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001235 content_info->description);
nisseede5da42017-01-12 05:15:36 -08001236 RTC_DCHECK(media_desc != NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001237
jiayl@webrtc.org9c16c392014-05-01 18:30:30 +00001238 int sctp_port = cricket::kSctpDefaultPort;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001239
1240 // RFC 4566
1241 // m=<media> <port> <proto> <fmt>
1242 // fmt is a list of payload type numbers that MAY be used in the session.
1243 const char* type = NULL;
1244 if (media_type == cricket::MEDIA_TYPE_AUDIO)
1245 type = kMediaTypeAudio;
1246 else if (media_type == cricket::MEDIA_TYPE_VIDEO)
1247 type = kMediaTypeVideo;
1248 else if (media_type == cricket::MEDIA_TYPE_DATA)
1249 type = kMediaTypeData;
1250 else
nissec80e7412017-01-11 05:56:46 -08001251 RTC_NOTREACHED();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001252
1253 std::string fmt;
1254 if (media_type == cricket::MEDIA_TYPE_VIDEO) {
1255 const VideoContentDescription* video_desc =
1256 static_cast<const VideoContentDescription*>(media_desc);
1257 for (std::vector<cricket::VideoCodec>::const_iterator it =
1258 video_desc->codecs().begin();
1259 it != video_desc->codecs().end(); ++it) {
1260 fmt.append(" ");
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001261 fmt.append(rtc::ToString<int>(it->id));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001262 }
1263 } else if (media_type == cricket::MEDIA_TYPE_AUDIO) {
1264 const AudioContentDescription* audio_desc =
1265 static_cast<const AudioContentDescription*>(media_desc);
1266 for (std::vector<cricket::AudioCodec>::const_iterator it =
1267 audio_desc->codecs().begin();
1268 it != audio_desc->codecs().end(); ++it) {
1269 fmt.append(" ");
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001270 fmt.append(rtc::ToString<int>(it->id));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001271 }
1272 } else if (media_type == cricket::MEDIA_TYPE_DATA) {
jiayl@webrtc.org9c16c392014-05-01 18:30:30 +00001273 const DataContentDescription* data_desc =
1274 static_cast<const DataContentDescription*>(media_desc);
pthatcher@webrtc.org3341b402015-02-13 21:14:22 +00001275 if (IsDtlsSctp(media_desc->protocol())) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001276 fmt.append(" ");
jiayl@webrtc.org9c16c392014-05-01 18:30:30 +00001277
zstein4b2e0822017-02-17 19:48:38 -08001278 if (data_desc->use_sctpmap()) {
1279 for (std::vector<cricket::DataCodec>::const_iterator it =
1280 data_desc->codecs().begin();
1281 it != data_desc->codecs().end(); ++it) {
1282 if (cricket::CodecNamesEq(it->name,
1283 cricket::kGoogleSctpDataCodecName) &&
1284 it->GetParam(cricket::kCodecParamPort, &sctp_port)) {
1285 break;
1286 }
jiayl@webrtc.org9c16c392014-05-01 18:30:30 +00001287 }
jiayl@webrtc.org9c16c392014-05-01 18:30:30 +00001288
zstein4b2e0822017-02-17 19:48:38 -08001289 fmt.append(rtc::ToString<int>(sctp_port));
1290 } else {
1291 fmt.append(kDefaultSctpmapProtocol);
1292 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001293 } else {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001294 for (std::vector<cricket::DataCodec>::const_iterator it =
1295 data_desc->codecs().begin();
1296 it != data_desc->codecs().end(); ++it) {
1297 fmt.append(" ");
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001298 fmt.append(rtc::ToString<int>(it->id));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001299 }
1300 }
1301 }
1302 // The fmt must never be empty. If no codecs are found, set the fmt attribute
1303 // to 0.
1304 if (fmt.empty()) {
1305 fmt = " 0";
1306 }
1307
deadbeef25ed4352016-12-12 18:37:36 -08001308 // The port number in the m line will be updated later when associated with
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001309 // the candidates.
deadbeef25ed4352016-12-12 18:37:36 -08001310 //
1311 // A port value of 0 indicates that the m= section is rejected.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001312 // RFC 3264
1313 // To reject an offered stream, the port number in the corresponding stream in
1314 // the answer MUST be set to zero.
deadbeef25ed4352016-12-12 18:37:36 -08001315 //
1316 // However, the BUNDLE draft adds a new meaning to port zero, when used along
1317 // with a=bundle-only.
1318 const std::string& port =
1319 (content_info->rejected || content_info->bundle_only) ? kMediaPortRejected
1320 : kDummyPort;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001321
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001322 rtc::SSLFingerprint* fp = (transport_info) ?
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001323 transport_info->description.identity_fingerprint.get() : NULL;
1324
wu@webrtc.org4c3e9912014-07-16 21:03:13 +00001325 // Add the m and c lines.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001326 InitLine(kLineTypeMedia, type, &os);
1327 os << " " << port << " " << media_desc->protocol() << fmt;
wu@webrtc.org4c3e9912014-07-16 21:03:13 +00001328 std::string mline = os.str();
1329 UpdateMediaDefaultDestination(candidates, mline, message);
1330
1331 // RFC 4566
1332 // b=AS:<bandwidth>
Peter Thatcherc0c3a862015-06-24 15:31:25 -07001333 if (media_desc->bandwidth() >= 1000) {
wu@webrtc.org4c3e9912014-07-16 21:03:13 +00001334 InitLine(kLineTypeSessionBandwidth, kApplicationSpecificMaximum, &os);
1335 os << kSdpDelimiterColon << (media_desc->bandwidth() / 1000);
1336 AddLine(os.str(), message);
1337 }
1338
deadbeef25ed4352016-12-12 18:37:36 -08001339 // Add the a=bundle-only line.
1340 if (content_info->bundle_only) {
1341 InitAttrLine(kAttributeBundleOnly, &os);
1342 AddLine(os.str(), message);
1343 }
1344
wu@webrtc.org4c3e9912014-07-16 21:03:13 +00001345 // Add the a=rtcp line.
pthatcher@webrtc.org3341b402015-02-13 21:14:22 +00001346 if (IsRtp(media_desc->protocol())) {
wu@webrtc.org4c3e9912014-07-16 21:03:13 +00001347 std::string rtcp_line = GetRtcpLine(candidates);
1348 if (!rtcp_line.empty()) {
1349 AddLine(rtcp_line, message);
1350 }
1351 }
1352
honghaiza54a0802015-12-16 18:37:23 -08001353 // Build the a=candidate lines. We don't include ufrag and pwd in the
1354 // candidates in the SDP to avoid redundancy.
1355 BuildCandidate(candidates, false, message);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001356
1357 // Use the transport_info to build the media level ice-ufrag and ice-pwd.
1358 if (transport_info) {
1359 // RFC 5245
1360 // ice-pwd-att = "ice-pwd" ":" password
1361 // ice-ufrag-att = "ice-ufrag" ":" ufrag
1362 // ice-ufrag
deadbeef3f7219b2015-12-28 15:17:14 -08001363 if (!transport_info->description.ice_ufrag.empty()) {
1364 InitAttrLine(kAttributeIceUfrag, &os);
1365 os << kSdpDelimiterColon << transport_info->description.ice_ufrag;
1366 AddLine(os.str(), message);
1367 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001368 // ice-pwd
deadbeef3f7219b2015-12-28 15:17:14 -08001369 if (!transport_info->description.ice_pwd.empty()) {
1370 InitAttrLine(kAttributeIcePwd, &os);
1371 os << kSdpDelimiterColon << transport_info->description.ice_pwd;
1372 AddLine(os.str(), message);
1373 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001374
1375 // draft-petithuguenin-mmusic-ice-attributes-level-03
1376 BuildIceOptions(transport_info->description.transport_options, message);
1377
1378 // RFC 4572
1379 // fingerprint-attribute =
1380 // "fingerprint" ":" hash-func SP fingerprint
1381 if (fp) {
1382 // Insert the fingerprint attribute.
1383 InitAttrLine(kAttributeFingerprint, &os);
1384 os << kSdpDelimiterColon
1385 << fp->algorithm << kSdpDelimiterSpace
1386 << fp->GetRfc4572Fingerprint();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001387 AddLine(os.str(), message);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001388
1389 // Inserting setup attribute.
1390 if (transport_info->description.connection_role !=
1391 cricket::CONNECTIONROLE_NONE) {
1392 // Making sure we are not using "passive" mode.
1393 cricket::ConnectionRole role =
1394 transport_info->description.connection_role;
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00001395 std::string dtls_role_str;
nissec16fa5e2017-02-07 07:18:43 -08001396 const bool success =
1397 cricket::ConnectionRoleToString(role, &dtls_role_str);
1398 RTC_DCHECK(success);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001399 InitAttrLine(kAttributeSetup, &os);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001400 os << kSdpDelimiterColon << dtls_role_str;
1401 AddLine(os.str(), message);
1402 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001403 }
1404 }
1405
1406 // RFC 3388
1407 // mid-attribute = "a=mid:" identification-tag
1408 // identification-tag = token
1409 // Use the content name as the mid identification-tag.
1410 InitAttrLine(kAttributeMid, &os);
1411 os << kSdpDelimiterColon << content_info->name;
1412 AddLine(os.str(), message);
1413
pthatcher@webrtc.org3341b402015-02-13 21:14:22 +00001414 if (IsDtlsSctp(media_desc->protocol())) {
zstein4b2e0822017-02-17 19:48:38 -08001415 const DataContentDescription* data_desc =
1416 static_cast<const DataContentDescription*>(media_desc);
1417 bool use_sctpmap = data_desc->use_sctpmap();
1418 BuildSctpContentAttributes(message, sctp_port, use_sctpmap);
pthatcher@webrtc.org3341b402015-02-13 21:14:22 +00001419 } else if (IsRtp(media_desc->protocol())) {
deadbeef9d3584c2016-02-16 17:54:10 -08001420 BuildRtpContentAttributes(media_desc, media_type, unified_plan_sdp,
1421 message);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001422 }
1423}
1424
zstein4b2e0822017-02-17 19:48:38 -08001425void BuildSctpContentAttributes(std::string* message,
1426 int sctp_port,
1427 bool use_sctpmap) {
wu@webrtc.org78187522013-10-07 23:32:02 +00001428 std::ostringstream os;
zstein4b2e0822017-02-17 19:48:38 -08001429 if (use_sctpmap) {
1430 // draft-ietf-mmusic-sctp-sdp-04
1431 // a=sctpmap:sctpmap-number protocol [streams]
1432 InitAttrLine(kAttributeSctpmap, &os);
1433 os << kSdpDelimiterColon << sctp_port << kSdpDelimiterSpace
1434 << kDefaultSctpmapProtocol << kSdpDelimiterSpace
1435 << cricket::kMaxSctpStreams;
1436 } else {
1437 // draft-ietf-mmusic-sctp-sdp-23
1438 // a=sctp-port:<port>
1439 InitAttrLine(kAttributeSctpPort, &os);
1440 os << kSdpDelimiterColon << sctp_port;
1441 // TODO(zstein): emit max-message-size here
1442 }
wu@webrtc.org78187522013-10-07 23:32:02 +00001443 AddLine(os.str(), message);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001444}
1445
deadbeef9d3584c2016-02-16 17:54:10 -08001446// If unified_plan_sdp is true, will use "a=msid".
1447void BuildRtpContentAttributes(const MediaContentDescription* media_desc,
1448 const MediaType media_type,
1449 bool unified_plan_sdp,
1450 std::string* message) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001451 std::ostringstream os;
1452 // RFC 5285
1453 // a=extmap:<value>["/"<direction>] <URI> <extensionattributes>
1454 // The definitions MUST be either all session level or all media level. This
1455 // implementation uses all media level.
1456 for (size_t i = 0; i < media_desc->rtp_header_extensions().size(); ++i) {
1457 InitAttrLine(kAttributeExtmap, &os);
1458 os << kSdpDelimiterColon << media_desc->rtp_header_extensions()[i].id
1459 << kSdpDelimiterSpace << media_desc->rtp_header_extensions()[i].uri;
1460 AddLine(os.str(), message);
1461 }
1462
1463 // RFC 3264
1464 // a=sendrecv || a=sendonly || a=sendrecv || a=inactive
deadbeefc80741f2015-10-22 13:14:45 -07001465 switch (media_desc->direction()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001466 case cricket::MD_INACTIVE:
1467 InitAttrLine(kAttributeInactive, &os);
1468 break;
1469 case cricket::MD_SENDONLY:
1470 InitAttrLine(kAttributeSendOnly, &os);
1471 break;
1472 case cricket::MD_RECVONLY:
1473 InitAttrLine(kAttributeRecvOnly, &os);
1474 break;
1475 case cricket::MD_SENDRECV:
1476 default:
1477 InitAttrLine(kAttributeSendRecv, &os);
1478 break;
1479 }
1480 AddLine(os.str(), message);
1481
deadbeef9d3584c2016-02-16 17:54:10 -08001482 // draft-ietf-mmusic-msid-11
1483 // a=msid:<stream id> <track id>
1484 if (unified_plan_sdp && !media_desc->streams().empty()) {
1485 if (media_desc->streams().size() > 1u) {
1486 LOG(LS_WARNING) << "Trying to serialize unified plan SDP with more than "
1487 << "one track in a media section. Omitting 'a=msid'.";
1488 } else {
1489 auto track = media_desc->streams().begin();
1490 const std::string& stream_id = track->sync_label;
deadbeef9d3584c2016-02-16 17:54:10 -08001491 InitAttrLine(kAttributeMsid, &os);
1492 os << kSdpDelimiterColon << stream_id << kSdpDelimiterSpace << track->id;
1493 AddLine(os.str(), message);
1494 }
1495 }
1496
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001497 // RFC 5761
1498 // a=rtcp-mux
1499 if (media_desc->rtcp_mux()) {
1500 InitAttrLine(kAttributeRtcpMux, &os);
1501 AddLine(os.str(), message);
1502 }
1503
deadbeef13871492015-12-09 12:37:51 -08001504 // RFC 5506
1505 // a=rtcp-rsize
1506 if (media_desc->rtcp_reduced_size()) {
1507 InitAttrLine(kAttributeRtcpReducedSize, &os);
1508 AddLine(os.str(), message);
1509 }
1510
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001511 // RFC 4568
1512 // a=crypto:<tag> <crypto-suite> <key-params> [<session-params>]
1513 for (std::vector<CryptoParams>::const_iterator it =
1514 media_desc->cryptos().begin();
1515 it != media_desc->cryptos().end(); ++it) {
1516 InitAttrLine(kAttributeCrypto, &os);
1517 os << kSdpDelimiterColon << it->tag << " " << it->cipher_suite << " "
1518 << it->key_params;
1519 if (!it->session_params.empty()) {
1520 os << " " << it->session_params;
1521 }
1522 AddLine(os.str(), message);
1523 }
1524
1525 // RFC 4566
1526 // a=rtpmap:<payload type> <encoding name>/<clock rate>
1527 // [/<encodingparameters>]
1528 BuildRtpMap(media_desc, media_type, message);
1529
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001530 for (StreamParamsVec::const_iterator track = media_desc->streams().begin();
1531 track != media_desc->streams().end(); ++track) {
1532 // Require that the track belongs to a media stream,
1533 // ie the sync_label is set. This extra check is necessary since the
1534 // MediaContentDescription always contains a streamparam with an ssrc even
1535 // if no track or media stream have been created.
1536 if (track->sync_label.empty()) continue;
1537
1538 // Build the ssrc-group lines.
1539 for (size_t i = 0; i < track->ssrc_groups.size(); ++i) {
1540 // RFC 5576
1541 // a=ssrc-group:<semantics> <ssrc-id> ...
1542 if (track->ssrc_groups[i].ssrcs.empty()) {
1543 continue;
1544 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001545 InitAttrLine(kAttributeSsrcGroup, &os);
1546 os << kSdpDelimiterColon << track->ssrc_groups[i].semantics;
Peter Boström0c4e06b2015-10-07 12:23:21 +02001547 std::vector<uint32_t>::const_iterator ssrc =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001548 track->ssrc_groups[i].ssrcs.begin();
1549 for (; ssrc != track->ssrc_groups[i].ssrcs.end(); ++ssrc) {
Peter Boström0c4e06b2015-10-07 12:23:21 +02001550 os << kSdpDelimiterSpace << rtc::ToString<uint32_t>(*ssrc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001551 }
1552 AddLine(os.str(), message);
1553 }
1554 // Build the ssrc lines for each ssrc.
1555 for (size_t i = 0; i < track->ssrcs.size(); ++i) {
Peter Boström0c4e06b2015-10-07 12:23:21 +02001556 uint32_t ssrc = track->ssrcs[i];
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001557 // RFC 5576
1558 // a=ssrc:<ssrc-id> cname:<value>
1559 AddSsrcLine(ssrc, kSsrcAttributeCname,
1560 track->cname, message);
1561
1562 // draft-alvestrand-mmusic-msid-00
1563 // a=ssrc:<ssrc-id> msid:identifier [appdata]
deadbeef9d3584c2016-02-16 17:54:10 -08001564 // The appdata consists of the "id" attribute of a MediaStreamTrack,
1565 // which corresponds to the "id" attribute of StreamParams.
1566 const std::string& stream_id = track->sync_label;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001567 InitAttrLine(kAttributeSsrc, &os);
1568 os << kSdpDelimiterColon << ssrc << kSdpDelimiterSpace
deadbeef9d3584c2016-02-16 17:54:10 -08001569 << kSsrcAttributeMsid << kSdpDelimiterColon << stream_id
1570 << kSdpDelimiterSpace << track->id;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001571 AddLine(os.str(), message);
1572
deadbeef9d3584c2016-02-16 17:54:10 -08001573 // TODO(ronghuawu): Remove below code which is for backward
1574 // compatibility.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001575 // draft-alvestrand-rtcweb-mid-01
1576 // a=ssrc:<ssrc-id> mslabel:<value>
1577 // The label isn't yet defined.
1578 // a=ssrc:<ssrc-id> label:<value>
1579 AddSsrcLine(ssrc, kSsrcAttributeMslabel, track->sync_label, message);
1580 AddSsrcLine(ssrc, kSSrcAttributeLabel, track->id, message);
1581 }
1582 }
1583}
1584
1585void WriteFmtpHeader(int payload_type, std::ostringstream* os) {
1586 // fmtp header: a=fmtp:|payload_type| <parameters>
1587 // Add a=fmtp
1588 InitAttrLine(kAttributeFmtp, os);
1589 // Add :|payload_type|
1590 *os << kSdpDelimiterColon << payload_type;
1591}
1592
1593void WriteRtcpFbHeader(int payload_type, std::ostringstream* os) {
1594 // rtcp-fb header: a=rtcp-fb:|payload_type|
1595 // <parameters>/<ccm <ccm_parameters>>
1596 // Add a=rtcp-fb
1597 InitAttrLine(kAttributeRtcpFb, os);
1598 // Add :
1599 *os << kSdpDelimiterColon;
1600 if (payload_type == kWildcardPayloadType) {
1601 *os << "*";
1602 } else {
1603 *os << payload_type;
1604 }
1605}
1606
1607void WriteFmtpParameter(const std::string& parameter_name,
1608 const std::string& parameter_value,
1609 std::ostringstream* os) {
1610 // fmtp parameters: |parameter_name|=|parameter_value|
1611 *os << parameter_name << kSdpDelimiterEqual << parameter_value;
1612}
1613
1614void WriteFmtpParameters(const cricket::CodecParameterMap& parameters,
1615 std::ostringstream* os) {
1616 for (cricket::CodecParameterMap::const_iterator fmtp = parameters.begin();
1617 fmtp != parameters.end(); ++fmtp) {
hta62a216e2016-04-15 11:02:14 -07001618 // Parameters are a semicolon-separated list, no spaces.
1619 // The list is separated from the header by a space.
1620 if (fmtp == parameters.begin()) {
1621 *os << kSdpDelimiterSpace;
1622 } else {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001623 *os << kSdpDelimiterSemicolon;
1624 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001625 WriteFmtpParameter(fmtp->first, fmtp->second, os);
1626 }
1627}
1628
1629bool IsFmtpParam(const std::string& name) {
ossuaa4b0772017-01-30 07:41:18 -08001630 // RFC 4855, section 3 specifies the mapping of media format parameters to SDP
1631 // parameters. Only ptime, maxptime, channels and rate are placed outside of
1632 // the fmtp line. In WebRTC, channels and rate are already handled separately
1633 // and thus not included in the CodecParameterMap.
1634 return name != kCodecParamPTime && name != kCodecParamMaxPTime;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001635}
1636
1637// Retreives fmtp parameters from |params|, which may contain other parameters
1638// as well, and puts them in |fmtp_parameters|.
1639void GetFmtpParams(const cricket::CodecParameterMap& params,
1640 cricket::CodecParameterMap* fmtp_parameters) {
1641 for (cricket::CodecParameterMap::const_iterator iter = params.begin();
1642 iter != params.end(); ++iter) {
1643 if (IsFmtpParam(iter->first)) {
1644 (*fmtp_parameters)[iter->first] = iter->second;
1645 }
1646 }
1647}
1648
1649template <class T>
1650void AddFmtpLine(const T& codec, std::string* message) {
1651 cricket::CodecParameterMap fmtp_parameters;
1652 GetFmtpParams(codec.params, &fmtp_parameters);
1653 if (fmtp_parameters.empty()) {
1654 // No need to add an fmtp if it will have no (optional) parameters.
1655 return;
1656 }
1657 std::ostringstream os;
1658 WriteFmtpHeader(codec.id, &os);
1659 WriteFmtpParameters(fmtp_parameters, &os);
1660 AddLine(os.str(), message);
1661 return;
1662}
1663
1664template <class T>
1665void AddRtcpFbLines(const T& codec, std::string* message) {
1666 for (std::vector<cricket::FeedbackParam>::const_iterator iter =
1667 codec.feedback_params.params().begin();
1668 iter != codec.feedback_params.params().end(); ++iter) {
1669 std::ostringstream os;
1670 WriteRtcpFbHeader(codec.id, &os);
1671 os << " " << iter->id();
1672 if (!iter->param().empty()) {
1673 os << " " << iter->param();
1674 }
1675 AddLine(os.str(), message);
1676 }
1677}
1678
jiayl@webrtc.orgddb85ab2014-09-05 16:31:56 +00001679bool AddSctpDataCodec(DataContentDescription* media_desc,
1680 int sctp_port) {
solenberg9fa49752016-10-08 13:02:44 -07001681 for (const auto& codec : media_desc->codecs()) {
1682 if (cricket::CodecNamesEq(codec.name, cricket::kGoogleSctpDataCodecName)) {
1683 return ParseFailed("",
1684 "Can't have multiple sctp port attributes.",
1685 NULL);
1686 }
jiayl@webrtc.orgddb85ab2014-09-05 16:31:56 +00001687 }
1688 // Add the SCTP Port number as a pseudo-codec "port" parameter
solenberg9fa49752016-10-08 13:02:44 -07001689 cricket::DataCodec codec_port(cricket::kGoogleSctpDataCodecPlType,
deadbeef67cf2c12016-04-13 10:07:16 -07001690 cricket::kGoogleSctpDataCodecName);
jiayl@webrtc.orgddb85ab2014-09-05 16:31:56 +00001691 codec_port.SetParam(cricket::kCodecParamPort, sctp_port);
1692 LOG(INFO) << "AddSctpDataCodec: Got SCTP Port Number "
1693 << sctp_port;
1694 media_desc->AddCodec(codec_port);
1695 return true;
1696}
1697
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001698bool GetMinValue(const std::vector<int>& values, int* value) {
1699 if (values.empty()) {
1700 return false;
1701 }
1702 std::vector<int>::const_iterator found =
1703 std::min_element(values.begin(), values.end());
1704 *value = *found;
1705 return true;
1706}
1707
1708bool GetParameter(const std::string& name,
1709 const cricket::CodecParameterMap& params, int* value) {
1710 std::map<std::string, std::string>::const_iterator found =
1711 params.find(name);
1712 if (found == params.end()) {
1713 return false;
1714 }
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001715 if (!rtc::FromString(found->second, value)) {
wu@webrtc.org5e760e72014-04-02 23:19:09 +00001716 return false;
1717 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001718 return true;
1719}
1720
1721void BuildRtpMap(const MediaContentDescription* media_desc,
1722 const MediaType media_type,
1723 std::string* message) {
nisseede5da42017-01-12 05:15:36 -08001724 RTC_DCHECK(message != NULL);
1725 RTC_DCHECK(media_desc != NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001726 std::ostringstream os;
1727 if (media_type == cricket::MEDIA_TYPE_VIDEO) {
1728 const VideoContentDescription* video_desc =
1729 static_cast<const VideoContentDescription*>(media_desc);
1730 for (std::vector<cricket::VideoCodec>::const_iterator it =
1731 video_desc->codecs().begin();
1732 it != video_desc->codecs().end(); ++it) {
1733 // RFC 4566
1734 // a=rtpmap:<payload type> <encoding name>/<clock rate>
1735 // [/<encodingparameters>]
1736 if (it->id != kWildcardPayloadType) {
1737 InitAttrLine(kAttributeRtpmap, &os);
deadbeefe814a0d2017-02-25 18:15:09 -08001738 os << kSdpDelimiterColon << it->id << " " << it->name << "/"
1739 << cricket::kVideoCodecClockrate;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001740 AddLine(os.str(), message);
1741 }
1742 AddRtcpFbLines(*it, message);
1743 AddFmtpLine(*it, message);
1744 }
1745 } else if (media_type == cricket::MEDIA_TYPE_AUDIO) {
1746 const AudioContentDescription* audio_desc =
1747 static_cast<const AudioContentDescription*>(media_desc);
1748 std::vector<int> ptimes;
1749 std::vector<int> maxptimes;
1750 int max_minptime = 0;
1751 for (std::vector<cricket::AudioCodec>::const_iterator it =
1752 audio_desc->codecs().begin();
1753 it != audio_desc->codecs().end(); ++it) {
nisseede5da42017-01-12 05:15:36 -08001754 RTC_DCHECK(!it->name.empty());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001755 // RFC 4566
1756 // a=rtpmap:<payload type> <encoding name>/<clock rate>
1757 // [/<encodingparameters>]
1758 InitAttrLine(kAttributeRtpmap, &os);
1759 os << kSdpDelimiterColon << it->id << " ";
1760 os << it->name << "/" << it->clockrate;
1761 if (it->channels != 1) {
1762 os << "/" << it->channels;
1763 }
1764 AddLine(os.str(), message);
1765 AddRtcpFbLines(*it, message);
1766 AddFmtpLine(*it, message);
1767 int minptime = 0;
1768 if (GetParameter(kCodecParamMinPTime, it->params, &minptime)) {
1769 max_minptime = std::max(minptime, max_minptime);
1770 }
1771 int ptime;
1772 if (GetParameter(kCodecParamPTime, it->params, &ptime)) {
1773 ptimes.push_back(ptime);
1774 }
1775 int maxptime;
1776 if (GetParameter(kCodecParamMaxPTime, it->params, &maxptime)) {
1777 maxptimes.push_back(maxptime);
1778 }
1779 }
1780 // Populate the maxptime attribute with the smallest maxptime of all codecs
1781 // under the same m-line.
1782 int min_maxptime = INT_MAX;
1783 if (GetMinValue(maxptimes, &min_maxptime)) {
1784 AddAttributeLine(kCodecParamMaxPTime, min_maxptime, message);
1785 }
nisseede5da42017-01-12 05:15:36 -08001786 RTC_DCHECK(min_maxptime > max_minptime);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001787 // Populate the ptime attribute with the smallest ptime or the largest
1788 // minptime, whichever is the largest, for all codecs under the same m-line.
1789 int ptime = INT_MAX;
1790 if (GetMinValue(ptimes, &ptime)) {
1791 ptime = std::min(ptime, min_maxptime);
1792 ptime = std::max(ptime, max_minptime);
1793 AddAttributeLine(kCodecParamPTime, ptime, message);
1794 }
1795 } else if (media_type == cricket::MEDIA_TYPE_DATA) {
1796 const DataContentDescription* data_desc =
1797 static_cast<const DataContentDescription*>(media_desc);
1798 for (std::vector<cricket::DataCodec>::const_iterator it =
1799 data_desc->codecs().begin();
1800 it != data_desc->codecs().end(); ++it) {
1801 // RFC 4566
1802 // a=rtpmap:<payload type> <encoding name>/<clock rate>
1803 // [/<encodingparameters>]
1804 InitAttrLine(kAttributeRtpmap, &os);
1805 os << kSdpDelimiterColon << it->id << " "
1806 << it->name << "/" << it->clockrate;
1807 AddLine(os.str(), message);
1808 }
1809 }
1810}
1811
1812void BuildCandidate(const std::vector<Candidate>& candidates,
honghaiza54a0802015-12-16 18:37:23 -08001813 bool include_ufrag,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001814 std::string* message) {
1815 std::ostringstream os;
1816
1817 for (std::vector<Candidate>::const_iterator it = candidates.begin();
1818 it != candidates.end(); ++it) {
1819 // RFC 5245
1820 // a=candidate:<foundation> <component-id> <transport> <priority>
1821 // <connection-address> <port> typ <candidate-types>
1822 // [raddr <connection-address>] [rport <port>]
1823 // *(SP extension-att-name SP extension-att-value)
1824 std::string type;
1825 // Map the cricket candidate type to "host" / "srflx" / "prflx" / "relay"
1826 if (it->type() == cricket::LOCAL_PORT_TYPE) {
1827 type = kCandidateHost;
1828 } else if (it->type() == cricket::STUN_PORT_TYPE) {
1829 type = kCandidateSrflx;
1830 } else if (it->type() == cricket::RELAY_PORT_TYPE) {
1831 type = kCandidateRelay;
Honghai Zhang7fb69db2016-03-14 11:59:18 -07001832 } else if (it->type() == cricket::PRFLX_PORT_TYPE) {
1833 type = kCandidatePrflx;
1834 // Peer reflexive candidate may be signaled for being removed.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001835 } else {
nissec80e7412017-01-11 05:56:46 -08001836 RTC_NOTREACHED();
Peter Thatcher019087f2015-04-28 09:06:26 -07001837 // Never write out candidates if we don't know the type.
1838 continue;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001839 }
1840
1841 InitAttrLine(kAttributeCandidate, &os);
1842 os << kSdpDelimiterColon
mallinath@webrtc.org2d60c5e2014-08-08 22:29:20 +00001843 << it->foundation() << " "
1844 << it->component() << " "
1845 << it->protocol() << " "
1846 << it->priority() << " "
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001847 << it->address().ipaddr().ToString() << " "
1848 << it->address().PortAsString() << " "
mallinath@webrtc.org2d60c5e2014-08-08 22:29:20 +00001849 << kAttributeCandidateTyp << " "
1850 << type << " ";
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001851
1852 // Related address
1853 if (!it->related_address().IsNil()) {
1854 os << kAttributeCandidateRaddr << " "
1855 << it->related_address().ipaddr().ToString() << " "
1856 << kAttributeCandidateRport << " "
1857 << it->related_address().PortAsString() << " ";
1858 }
1859
mallinath@webrtc.org2d60c5e2014-08-08 22:29:20 +00001860 if (it->protocol() == cricket::TCP_PROTOCOL_NAME) {
mallinath@webrtc.orge999bd02014-08-13 06:05:55 +00001861 os << kTcpCandidateType << " " << it->tcptype() << " ";
mallinath@webrtc.org2d60c5e2014-08-08 22:29:20 +00001862 }
1863
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001864 // Extensions
1865 os << kAttributeCandidateGeneration << " " << it->generation();
honghaiza54a0802015-12-16 18:37:23 -08001866 if (include_ufrag && !it->username().empty()) {
1867 os << " " << kAttributeCandidateUfrag << " " << it->username();
1868 }
honghaiza0c44ea2016-03-23 16:07:48 -07001869 if (it->network_id() > 0) {
1870 os << " " << kAttributeCandidateNetworkId << " " << it->network_id();
1871 }
honghaize1a0c942016-02-16 14:54:56 -08001872 if (it->network_cost() > 0) {
1873 os << " " << kAttributeCandidateNetworkCost << " " << it->network_cost();
1874 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001875
1876 AddLine(os.str(), message);
1877 }
1878}
1879
1880void BuildIceOptions(const std::vector<std::string>& transport_options,
1881 std::string* message) {
1882 if (!transport_options.empty()) {
1883 std::ostringstream os;
1884 InitAttrLine(kAttributeIceOption, &os);
1885 os << kSdpDelimiterColon << transport_options[0];
1886 for (size_t i = 1; i < transport_options.size(); ++i) {
1887 os << kSdpDelimiterSpace << transport_options[i];
1888 }
1889 AddLine(os.str(), message);
1890 }
1891}
1892
pthatcher@webrtc.org3341b402015-02-13 21:14:22 +00001893bool IsRtp(const std::string& protocol) {
1894 return protocol.empty() ||
1895 (protocol.find(cricket::kMediaProtocolRtpPrefix) != std::string::npos);
1896}
1897
1898bool IsDtlsSctp(const std::string& protocol) {
1899 // This intentionally excludes "SCTP" and "SCTP/DTLS".
lally@webrtc.orga7470932015-02-24 20:19:21 +00001900 return protocol.find(cricket::kMediaProtocolDtlsSctp) != std::string::npos;
pthatcher@webrtc.org3341b402015-02-13 21:14:22 +00001901}
1902
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001903bool ParseSessionDescription(const std::string& message, size_t* pos,
1904 std::string* session_id,
1905 std::string* session_version,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001906 TransportDescription* session_td,
1907 RtpHeaderExtensions* session_extmaps,
1908 cricket::SessionDescription* desc,
1909 SdpParseError* error) {
1910 std::string line;
1911
deadbeefc80741f2015-10-22 13:14:45 -07001912 desc->set_msid_supported(false);
1913
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001914 // RFC 4566
1915 // v= (protocol version)
1916 if (!GetLineWithType(message, pos, &line, kLineTypeVersion)) {
1917 return ParseFailedExpectLine(message, *pos, kLineTypeVersion,
1918 std::string(), error);
1919 }
1920 // RFC 4566
1921 // o=<username> <sess-id> <sess-version> <nettype> <addrtype>
1922 // <unicast-address>
1923 if (!GetLineWithType(message, pos, &line, kLineTypeOrigin)) {
1924 return ParseFailedExpectLine(message, *pos, kLineTypeOrigin,
1925 std::string(), error);
1926 }
1927 std::vector<std::string> fields;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001928 rtc::split(line.substr(kLinePrefixLength),
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001929 kSdpDelimiterSpace, &fields);
1930 const size_t expected_fields = 6;
1931 if (fields.size() != expected_fields) {
1932 return ParseFailedExpectFieldNum(line, expected_fields, error);
1933 }
1934 *session_id = fields[1];
1935 *session_version = fields[2];
1936
1937 // RFC 4566
1938 // s= (session name)
1939 if (!GetLineWithType(message, pos, &line, kLineTypeSessionName)) {
1940 return ParseFailedExpectLine(message, *pos, kLineTypeSessionName,
1941 std::string(), error);
1942 }
1943
1944 // Optional lines
1945 // Those are the optional lines, so shouldn't return false if not present.
1946 // RFC 4566
1947 // i=* (session information)
1948 GetLineWithType(message, pos, &line, kLineTypeSessionInfo);
1949
1950 // RFC 4566
1951 // u=* (URI of description)
1952 GetLineWithType(message, pos, &line, kLineTypeSessionUri);
1953
1954 // RFC 4566
1955 // e=* (email address)
1956 GetLineWithType(message, pos, &line, kLineTypeSessionEmail);
1957
1958 // RFC 4566
1959 // p=* (phone number)
1960 GetLineWithType(message, pos, &line, kLineTypeSessionPhone);
1961
1962 // RFC 4566
1963 // c=* (connection information -- not required if included in
1964 // all media)
1965 GetLineWithType(message, pos, &line, kLineTypeConnection);
1966
1967 // RFC 4566
1968 // b=* (zero or more bandwidth information lines)
1969 while (GetLineWithType(message, pos, &line, kLineTypeSessionBandwidth)) {
1970 // By pass zero or more b lines.
1971 }
1972
1973 // RFC 4566
1974 // One or more time descriptions ("t=" and "r=" lines; see below)
1975 // t= (time the session is active)
1976 // r=* (zero or more repeat times)
1977 // Ensure there's at least one time description
1978 if (!GetLineWithType(message, pos, &line, kLineTypeTiming)) {
1979 return ParseFailedExpectLine(message, *pos, kLineTypeTiming, std::string(),
1980 error);
1981 }
1982
1983 while (GetLineWithType(message, pos, &line, kLineTypeRepeatTimes)) {
1984 // By pass zero or more r lines.
1985 }
1986
1987 // Go through the rest of the time descriptions
1988 while (GetLineWithType(message, pos, &line, kLineTypeTiming)) {
1989 while (GetLineWithType(message, pos, &line, kLineTypeRepeatTimes)) {
1990 // By pass zero or more r lines.
1991 }
1992 }
1993
1994 // RFC 4566
1995 // z=* (time zone adjustments)
1996 GetLineWithType(message, pos, &line, kLineTypeTimeZone);
1997
1998 // RFC 4566
1999 // k=* (encryption key)
2000 GetLineWithType(message, pos, &line, kLineTypeEncryptionKey);
2001
2002 // RFC 4566
2003 // a=* (zero or more session attribute lines)
2004 while (GetLineWithType(message, pos, &line, kLineTypeAttributes)) {
2005 if (HasAttribute(line, kAttributeGroup)) {
2006 if (!ParseGroupAttribute(line, desc, error)) {
2007 return false;
2008 }
2009 } else if (HasAttribute(line, kAttributeIceUfrag)) {
2010 if (!GetValue(line, kAttributeIceUfrag,
2011 &(session_td->ice_ufrag), error)) {
2012 return false;
2013 }
2014 } else if (HasAttribute(line, kAttributeIcePwd)) {
2015 if (!GetValue(line, kAttributeIcePwd, &(session_td->ice_pwd), error)) {
2016 return false;
2017 }
2018 } else if (HasAttribute(line, kAttributeIceLite)) {
2019 session_td->ice_mode = cricket::ICEMODE_LITE;
2020 } else if (HasAttribute(line, kAttributeIceOption)) {
2021 if (!ParseIceOptions(line, &(session_td->transport_options), error)) {
2022 return false;
2023 }
2024 } else if (HasAttribute(line, kAttributeFingerprint)) {
2025 if (session_td->identity_fingerprint.get()) {
2026 return ParseFailed(
2027 line,
2028 "Can't have multiple fingerprint attributes at the same level.",
2029 error);
2030 }
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002031 rtc::SSLFingerprint* fingerprint = NULL;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002032 if (!ParseFingerprintAttribute(line, &fingerprint, error)) {
2033 return false;
2034 }
2035 session_td->identity_fingerprint.reset(fingerprint);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00002036 } else if (HasAttribute(line, kAttributeSetup)) {
2037 if (!ParseDtlsSetup(line, &(session_td->connection_role), error)) {
2038 return false;
2039 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002040 } else if (HasAttribute(line, kAttributeMsidSemantics)) {
2041 std::string semantics;
2042 if (!GetValue(line, kAttributeMsidSemantics, &semantics, error)) {
2043 return false;
2044 }
deadbeefc80741f2015-10-22 13:14:45 -07002045 desc->set_msid_supported(
2046 CaseInsensitiveFind(semantics, kMediaStreamSemantic));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002047 } else if (HasAttribute(line, kAttributeExtmap)) {
isheriff6f8d6862016-05-26 11:24:55 -07002048 RtpExtension extmap;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002049 if (!ParseExtmap(line, &extmap, error)) {
2050 return false;
2051 }
2052 session_extmaps->push_back(extmap);
2053 }
2054 }
2055
2056 return true;
2057}
2058
2059bool ParseGroupAttribute(const std::string& line,
2060 cricket::SessionDescription* desc,
2061 SdpParseError* error) {
nisseede5da42017-01-12 05:15:36 -08002062 RTC_DCHECK(desc != NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002063
2064 // RFC 5888 and draft-holmberg-mmusic-sdp-bundle-negotiation-00
2065 // a=group:BUNDLE video voice
2066 std::vector<std::string> fields;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002067 rtc::split(line.substr(kLinePrefixLength),
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002068 kSdpDelimiterSpace, &fields);
2069 std::string semantics;
2070 if (!GetValue(fields[0], kAttributeGroup, &semantics, error)) {
2071 return false;
2072 }
2073 cricket::ContentGroup group(semantics);
2074 for (size_t i = 1; i < fields.size(); ++i) {
2075 group.AddContentName(fields[i]);
2076 }
2077 desc->AddGroup(group);
2078 return true;
2079}
2080
2081static bool ParseFingerprintAttribute(const std::string& line,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002082 rtc::SSLFingerprint** fingerprint,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002083 SdpParseError* error) {
2084 if (!IsLineType(line, kLineTypeAttributes) ||
2085 !HasAttribute(line, kAttributeFingerprint)) {
2086 return ParseFailedExpectLine(line, 0, kLineTypeAttributes,
2087 kAttributeFingerprint, error);
2088 }
2089
2090 std::vector<std::string> fields;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002091 rtc::split(line.substr(kLinePrefixLength),
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002092 kSdpDelimiterSpace, &fields);
2093 const size_t expected_fields = 2;
2094 if (fields.size() != expected_fields) {
2095 return ParseFailedExpectFieldNum(line, expected_fields, error);
2096 }
2097
2098 // The first field here is "fingerprint:<hash>.
2099 std::string algorithm;
2100 if (!GetValue(fields[0], kAttributeFingerprint, &algorithm, error)) {
2101 return false;
2102 }
2103
2104 // Downcase the algorithm. Note that we don't need to downcase the
2105 // fingerprint because hex_decode can handle upper-case.
2106 std::transform(algorithm.begin(), algorithm.end(), algorithm.begin(),
2107 ::tolower);
2108
2109 // The second field is the digest value. De-hexify it.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002110 *fingerprint = rtc::SSLFingerprint::CreateFromRfc4572(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002111 algorithm, fields[1]);
2112 if (!*fingerprint) {
2113 return ParseFailed(line,
2114 "Failed to create fingerprint from the digest.",
2115 error);
2116 }
2117
2118 return true;
2119}
2120
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00002121static bool ParseDtlsSetup(const std::string& line,
2122 cricket::ConnectionRole* role,
2123 SdpParseError* error) {
2124 // setup-attr = "a=setup:" role
2125 // role = "active" / "passive" / "actpass" / "holdconn"
2126 std::vector<std::string> fields;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002127 rtc::split(line.substr(kLinePrefixLength), kSdpDelimiterColon, &fields);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00002128 const size_t expected_fields = 2;
2129 if (fields.size() != expected_fields) {
2130 return ParseFailedExpectFieldNum(line, expected_fields, error);
2131 }
2132 std::string role_str = fields[1];
2133 if (!cricket::StringToConnectionRole(role_str, role)) {
2134 return ParseFailed(line, "Invalid attribute value.", error);
2135 }
2136 return true;
2137}
2138
deadbeef9d3584c2016-02-16 17:54:10 -08002139static bool ParseMsidAttribute(const std::string& line,
2140 std::string* stream_id,
2141 std::string* track_id,
2142 SdpParseError* error) {
2143 // draft-ietf-mmusic-msid-11
2144 // a=msid:<stream id> <track id>
2145 // msid-value = msid-id [ SP msid-appdata ]
2146 // msid-id = 1*64token-char ; see RFC 4566
2147 // msid-appdata = 1*64token-char ; see RFC 4566
2148 std::string field1;
2149 if (!rtc::tokenize_first(line.substr(kLinePrefixLength), kSdpDelimiterSpace,
2150 &field1, track_id)) {
2151 const size_t expected_fields = 2;
2152 return ParseFailedExpectFieldNum(line, expected_fields, error);
2153 }
2154
deadbeefa4549d62017-02-10 17:26:22 -08002155 if (track_id->empty()) {
2156 return ParseFailed(line, "Missing track ID in msid attribute.", error);
2157 }
2158
deadbeef9d3584c2016-02-16 17:54:10 -08002159 // msid:<msid-id>
2160 if (!GetValue(field1, kAttributeMsid, stream_id, error)) {
2161 return false;
2162 }
deadbeefa4549d62017-02-10 17:26:22 -08002163 if (stream_id->empty()) {
2164 return ParseFailed(line, "Missing stream ID in msid attribute.", error);
2165 }
deadbeef9d3584c2016-02-16 17:54:10 -08002166 return true;
2167}
2168
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002169// RFC 3551
2170// PT encoding media type clock rate channels
2171// name (Hz)
2172// 0 PCMU A 8,000 1
2173// 1 reserved A
2174// 2 reserved A
2175// 3 GSM A 8,000 1
2176// 4 G723 A 8,000 1
2177// 5 DVI4 A 8,000 1
2178// 6 DVI4 A 16,000 1
2179// 7 LPC A 8,000 1
2180// 8 PCMA A 8,000 1
2181// 9 G722 A 8,000 1
2182// 10 L16 A 44,100 2
2183// 11 L16 A 44,100 1
2184// 12 QCELP A 8,000 1
2185// 13 CN A 8,000 1
2186// 14 MPA A 90,000 (see text)
2187// 15 G728 A 8,000 1
2188// 16 DVI4 A 11,025 1
2189// 17 DVI4 A 22,050 1
2190// 18 G729 A 8,000 1
2191struct StaticPayloadAudioCodec {
2192 const char* name;
2193 int clockrate;
Peter Kasting69558702016-01-12 16:26:35 -08002194 size_t channels;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002195};
2196static const StaticPayloadAudioCodec kStaticPayloadAudioCodecs[] = {
2197 { "PCMU", 8000, 1 },
2198 { "reserved", 0, 0 },
2199 { "reserved", 0, 0 },
2200 { "GSM", 8000, 1 },
2201 { "G723", 8000, 1 },
2202 { "DVI4", 8000, 1 },
2203 { "DVI4", 16000, 1 },
2204 { "LPC", 8000, 1 },
2205 { "PCMA", 8000, 1 },
2206 { "G722", 8000, 1 },
2207 { "L16", 44100, 2 },
2208 { "L16", 44100, 1 },
2209 { "QCELP", 8000, 1 },
2210 { "CN", 8000, 1 },
2211 { "MPA", 90000, 1 },
2212 { "G728", 8000, 1 },
2213 { "DVI4", 11025, 1 },
2214 { "DVI4", 22050, 1 },
2215 { "G729", 8000, 1 },
2216};
2217
2218void MaybeCreateStaticPayloadAudioCodecs(
2219 const std::vector<int>& fmts, AudioContentDescription* media_desc) {
2220 if (!media_desc) {
2221 return;
2222 }
deadbeef67cf2c12016-04-13 10:07:16 -07002223 RTC_DCHECK(media_desc->codecs().empty());
solenberg9fa49752016-10-08 13:02:44 -07002224 for (int payload_type : fmts) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002225 if (!media_desc->HasCodec(payload_type) &&
2226 payload_type >= 0 &&
kjellander3e33bfe2016-06-20 07:04:09 -07002227 static_cast<uint32_t>(payload_type) <
2228 arraysize(kStaticPayloadAudioCodecs)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002229 std::string encoding_name = kStaticPayloadAudioCodecs[payload_type].name;
2230 int clock_rate = kStaticPayloadAudioCodecs[payload_type].clockrate;
Peter Kasting69558702016-01-12 16:26:35 -08002231 size_t channels = kStaticPayloadAudioCodecs[payload_type].channels;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002232 media_desc->AddCodec(cricket::AudioCodec(payload_type, encoding_name,
deadbeef67cf2c12016-04-13 10:07:16 -07002233 clock_rate, 0, channels));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002234 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002235 }
2236}
2237
2238template <class C>
2239static C* ParseContentDescription(const std::string& message,
2240 const MediaType media_type,
2241 int mline_index,
2242 const std::string& protocol,
deadbeef67cf2c12016-04-13 10:07:16 -07002243 const std::vector<int>& payload_types,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002244 size_t* pos,
2245 std::string* content_name,
deadbeef25ed4352016-12-12 18:37:36 -08002246 bool* bundle_only,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002247 TransportDescription* transport,
2248 std::vector<JsepIceCandidate*>* candidates,
2249 webrtc::SdpParseError* error) {
2250 C* media_desc = new C();
2251 switch (media_type) {
2252 case cricket::MEDIA_TYPE_AUDIO:
2253 *content_name = cricket::CN_AUDIO;
2254 break;
2255 case cricket::MEDIA_TYPE_VIDEO:
2256 *content_name = cricket::CN_VIDEO;
2257 break;
2258 case cricket::MEDIA_TYPE_DATA:
2259 *content_name = cricket::CN_DATA;
2260 break;
2261 default:
nissec80e7412017-01-11 05:56:46 -08002262 RTC_NOTREACHED();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002263 break;
2264 }
deadbeef67cf2c12016-04-13 10:07:16 -07002265 if (!ParseContent(message, media_type, mline_index, protocol, payload_types,
deadbeef25ed4352016-12-12 18:37:36 -08002266 pos, content_name, bundle_only, media_desc, transport,
2267 candidates, error)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002268 delete media_desc;
2269 return NULL;
2270 }
2271 // Sort the codecs according to the m-line fmt list.
deadbeef67cf2c12016-04-13 10:07:16 -07002272 std::unordered_map<int, int> payload_type_preferences;
2273 // "size + 1" so that the lowest preference payload type has a preference of
2274 // 1, which is greater than the default (0) for payload types not in the fmt
2275 // list.
2276 int preference = static_cast<int>(payload_types.size() + 1);
2277 for (int pt : payload_types) {
2278 payload_type_preferences[pt] = preference--;
2279 }
2280 std::vector<typename C::CodecType> codecs = media_desc->codecs();
2281 std::sort(codecs.begin(), codecs.end(), [&payload_type_preferences](
2282 const typename C::CodecType& a,
2283 const typename C::CodecType& b) {
2284 return payload_type_preferences[a.id] > payload_type_preferences[b.id];
2285 });
2286 media_desc->set_codecs(codecs);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002287 return media_desc;
2288}
2289
2290bool ParseMediaDescription(const std::string& message,
2291 const TransportDescription& session_td,
2292 const RtpHeaderExtensions& session_extmaps,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002293 size_t* pos,
2294 cricket::SessionDescription* desc,
2295 std::vector<JsepIceCandidate*>* candidates,
2296 SdpParseError* error) {
nisseede5da42017-01-12 05:15:36 -08002297 RTC_DCHECK(desc != NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002298 std::string line;
2299 int mline_index = -1;
2300
2301 // Zero or more media descriptions
2302 // RFC 4566
2303 // m=<media> <port> <proto> <fmt>
2304 while (GetLineWithType(message, pos, &line, kLineTypeMedia)) {
2305 ++mline_index;
2306
2307 std::vector<std::string> fields;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002308 rtc::split(line.substr(kLinePrefixLength),
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002309 kSdpDelimiterSpace, &fields);
zstein4b2e0822017-02-17 19:48:38 -08002310
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002311 const size_t expected_min_fields = 4;
2312 if (fields.size() < expected_min_fields) {
2313 return ParseFailedExpectMinFieldNum(line, expected_min_fields, error);
2314 }
deadbeef25ed4352016-12-12 18:37:36 -08002315 bool port_rejected = false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002316 // RFC 3264
2317 // To reject an offered stream, the port number in the corresponding stream
2318 // in the answer MUST be set to zero.
2319 if (fields[1] == kMediaPortRejected) {
deadbeef25ed4352016-12-12 18:37:36 -08002320 port_rejected = true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002321 }
2322
2323 std::string protocol = fields[2];
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002324
2325 // <fmt>
deadbeef67cf2c12016-04-13 10:07:16 -07002326 std::vector<int> payload_types;
pthatcher@webrtc.org3341b402015-02-13 21:14:22 +00002327 if (IsRtp(protocol)) {
jiayl@webrtc.orgddb85ab2014-09-05 16:31:56 +00002328 for (size_t j = 3 ; j < fields.size(); ++j) {
2329 // TODO(wu): Remove when below bug is fixed.
2330 // https://bugzilla.mozilla.org/show_bug.cgi?id=996329
pbosbb36fdf2015-07-09 07:48:14 -07002331 if (fields[j].empty() && j == fields.size() - 1) {
jiayl@webrtc.orgddb85ab2014-09-05 16:31:56 +00002332 continue;
2333 }
wu@webrtc.org36eda7c2014-04-15 20:37:30 +00002334
jiayl@webrtc.orgddb85ab2014-09-05 16:31:56 +00002335 int pl = 0;
pkasting@chromium.orge9facf82015-02-17 20:36:28 +00002336 if (!GetPayloadTypeFromString(line, fields[j], &pl, error)) {
jiayl@webrtc.orgddb85ab2014-09-05 16:31:56 +00002337 return false;
2338 }
deadbeef67cf2c12016-04-13 10:07:16 -07002339 payload_types.push_back(pl);
wu@webrtc.org5e760e72014-04-02 23:19:09 +00002340 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002341 }
2342
2343 // Make a temporary TransportDescription based on |session_td|.
2344 // Some of this gets overwritten by ParseContent.
deadbeef46eed762016-01-28 13:24:37 -08002345 TransportDescription transport(
2346 session_td.transport_options, session_td.ice_ufrag, session_td.ice_pwd,
2347 session_td.ice_mode, session_td.connection_role,
2348 session_td.identity_fingerprint.get());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002349
kwibergd1fe2812016-04-27 06:47:29 -07002350 std::unique_ptr<MediaContentDescription> content;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002351 std::string content_name;
deadbeef25ed4352016-12-12 18:37:36 -08002352 bool bundle_only = false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002353 if (HasAttribute(line, kMediaTypeVideo)) {
2354 content.reset(ParseContentDescription<VideoContentDescription>(
deadbeef67cf2c12016-04-13 10:07:16 -07002355 message, cricket::MEDIA_TYPE_VIDEO, mline_index, protocol,
deadbeef25ed4352016-12-12 18:37:36 -08002356 payload_types, pos, &content_name, &bundle_only, &transport,
2357 candidates, error));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002358 } else if (HasAttribute(line, kMediaTypeAudio)) {
2359 content.reset(ParseContentDescription<AudioContentDescription>(
deadbeef67cf2c12016-04-13 10:07:16 -07002360 message, cricket::MEDIA_TYPE_AUDIO, mline_index, protocol,
deadbeef25ed4352016-12-12 18:37:36 -08002361 payload_types, pos, &content_name, &bundle_only, &transport,
2362 candidates, error));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002363 } else if (HasAttribute(line, kMediaTypeData)) {
jiayl@webrtc.org0e527722014-09-08 21:43:43 +00002364 DataContentDescription* data_desc =
wu@webrtc.org78187522013-10-07 23:32:02 +00002365 ParseContentDescription<DataContentDescription>(
deadbeef67cf2c12016-04-13 10:07:16 -07002366 message, cricket::MEDIA_TYPE_DATA, mline_index, protocol,
deadbeef25ed4352016-12-12 18:37:36 -08002367 payload_types, pos, &content_name, &bundle_only, &transport,
2368 candidates, error);
jiayl@webrtc.org0e527722014-09-08 21:43:43 +00002369 content.reset(data_desc);
wu@webrtc.org78187522013-10-07 23:32:02 +00002370
zstein4b2e0822017-02-17 19:48:38 -08002371 if (data_desc && IsDtlsSctp(protocol)) {
2372 int p;
2373 if (rtc::FromString(fields[3], &p)) {
2374 if (!AddSctpDataCodec(data_desc, p)) {
2375 return false;
2376 }
2377 } else if (fields[3] == kDefaultSctpmapProtocol) {
2378 data_desc->set_use_sctpmap(false);
2379 }
wu@webrtc.org78187522013-10-07 23:32:02 +00002380 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002381 } else {
2382 LOG(LS_WARNING) << "Unsupported media type: " << line;
2383 continue;
2384 }
2385 if (!content.get()) {
2386 // ParseContentDescription returns NULL if failed.
2387 return false;
2388 }
2389
deadbeef25ed4352016-12-12 18:37:36 -08002390 bool content_rejected = false;
deadbeef12771a12017-01-03 13:53:47 -08002391 // A port of 0 is not interpreted as a rejected m= section when it's
2392 // used along with a=bundle-only.
deadbeef25ed4352016-12-12 18:37:36 -08002393 if (bundle_only) {
deadbeef25ed4352016-12-12 18:37:36 -08002394 if (!port_rejected) {
deadbeef12771a12017-01-03 13:53:47 -08002395 // Usage of bundle-only with a nonzero port is unspecified. So just
2396 // ignore bundle-only if we see this.
2397 bundle_only = false;
2398 LOG(LS_WARNING)
2399 << "a=bundle-only attribute observed with a nonzero "
2400 << "port; this usage is unspecified so the attribute is being "
2401 << "ignored.";
deadbeef25ed4352016-12-12 18:37:36 -08002402 }
2403 } else {
2404 // If not using bundle-only, interpret port 0 in the normal way; the m=
2405 // section is being rejected.
2406 content_rejected = port_rejected;
2407 }
2408
pthatcher@webrtc.org3341b402015-02-13 21:14:22 +00002409 if (IsRtp(protocol)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002410 // Set the extmap.
2411 if (!session_extmaps.empty() &&
2412 !content->rtp_header_extensions().empty()) {
2413 return ParseFailed("",
2414 "The a=extmap MUST be either all session level or "
2415 "all media level.",
2416 error);
2417 }
2418 for (size_t i = 0; i < session_extmaps.size(); ++i) {
2419 content->AddRtpHeaderExtension(session_extmaps[i]);
2420 }
2421 }
2422 content->set_protocol(protocol);
2423 desc->AddContent(content_name,
deadbeef25ed4352016-12-12 18:37:36 -08002424 IsDtlsSctp(protocol) ? cricket::NS_JINGLE_DRAFT_SCTP
2425 : cricket::NS_JINGLE_RTP,
2426 content_rejected, bundle_only, content.release());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002427 // Create TransportInfo with the media level "ice-pwd" and "ice-ufrag".
2428 TransportInfo transport_info(content_name, transport);
2429
2430 if (!desc->AddTransportInfo(transport_info)) {
2431 std::ostringstream description;
2432 description << "Failed to AddTransportInfo with content name: "
2433 << content_name;
2434 return ParseFailed("", description.str(), error);
2435 }
2436 }
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00002437
2438 size_t end_of_message = message.size();
2439 if (mline_index == -1 && *pos != end_of_message) {
2440 ParseFailed(message, *pos, "Expects m line.", error);
2441 return false;
2442 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002443 return true;
2444}
2445
2446bool VerifyCodec(const cricket::Codec& codec) {
2447 // Codec has not been populated correctly unless the name has been set. This
2448 // can happen if an SDP has an fmtp or rtcp-fb with a payload type but doesn't
2449 // have a corresponding "rtpmap" line.
htab39db842016-12-08 01:50:48 -08002450 return !codec.name.empty();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002451}
2452
2453bool VerifyAudioCodecs(const AudioContentDescription* audio_desc) {
2454 const std::vector<cricket::AudioCodec>& codecs = audio_desc->codecs();
2455 for (std::vector<cricket::AudioCodec>::const_iterator iter = codecs.begin();
2456 iter != codecs.end(); ++iter) {
2457 if (!VerifyCodec(*iter)) {
2458 return false;
2459 }
2460 }
2461 return true;
2462}
2463
2464bool VerifyVideoCodecs(const VideoContentDescription* video_desc) {
2465 const std::vector<cricket::VideoCodec>& codecs = video_desc->codecs();
2466 for (std::vector<cricket::VideoCodec>::const_iterator iter = codecs.begin();
2467 iter != codecs.end(); ++iter) {
2468 if (!VerifyCodec(*iter)) {
2469 return false;
2470 }
2471 }
2472 return true;
2473}
2474
2475void AddParameters(const cricket::CodecParameterMap& parameters,
2476 cricket::Codec* codec) {
2477 for (cricket::CodecParameterMap::const_iterator iter =
2478 parameters.begin(); iter != parameters.end(); ++iter) {
2479 codec->SetParam(iter->first, iter->second);
2480 }
2481}
2482
2483void AddFeedbackParameter(const cricket::FeedbackParam& feedback_param,
2484 cricket::Codec* codec) {
2485 codec->AddFeedbackParam(feedback_param);
2486}
2487
2488void AddFeedbackParameters(const cricket::FeedbackParams& feedback_params,
2489 cricket::Codec* codec) {
2490 for (std::vector<cricket::FeedbackParam>::const_iterator iter =
2491 feedback_params.params().begin();
2492 iter != feedback_params.params().end(); ++iter) {
2493 codec->AddFeedbackParam(*iter);
2494 }
2495}
2496
2497// Gets the current codec setting associated with |payload_type|. If there
changbin.shao@webrtc.org2d25b442015-03-16 04:14:34 +00002498// is no Codec associated with that payload type it returns an empty codec
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002499// with that payload type.
2500template <class T>
changbin.shao@webrtc.org2d25b442015-03-16 04:14:34 +00002501T GetCodecWithPayloadType(const std::vector<T>& codecs, int payload_type) {
magjedb05fa242016-11-11 04:00:16 -08002502 const T* codec = FindCodecById(codecs, payload_type);
2503 if (codec)
2504 return *codec;
2505 // Return empty codec with |payload_type|.
changbin.shao@webrtc.org2d25b442015-03-16 04:14:34 +00002506 T ret_val;
magjedb05fa242016-11-11 04:00:16 -08002507 ret_val.id = payload_type;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002508 return ret_val;
2509}
2510
2511// Updates or creates a new codec entry in the audio description.
2512template <class T, class U>
2513void AddOrReplaceCodec(MediaContentDescription* content_desc, const U& codec) {
2514 T* desc = static_cast<T*>(content_desc);
2515 std::vector<U> codecs = desc->codecs();
2516 bool found = false;
2517
2518 typename std::vector<U>::iterator iter;
2519 for (iter = codecs.begin(); iter != codecs.end(); ++iter) {
2520 if (iter->id == codec.id) {
2521 *iter = codec;
2522 found = true;
2523 break;
2524 }
2525 }
2526 if (!found) {
2527 desc->AddCodec(codec);
2528 return;
2529 }
2530 desc->set_codecs(codecs);
2531}
2532
2533// Adds or updates existing codec corresponding to |payload_type| according
2534// to |parameters|.
2535template <class T, class U>
2536void UpdateCodec(MediaContentDescription* content_desc, int payload_type,
2537 const cricket::CodecParameterMap& parameters) {
2538 // Codec might already have been populated (from rtpmap).
changbin.shao@webrtc.org2d25b442015-03-16 04:14:34 +00002539 U new_codec = GetCodecWithPayloadType(static_cast<T*>(content_desc)->codecs(),
2540 payload_type);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002541 AddParameters(parameters, &new_codec);
2542 AddOrReplaceCodec<T, U>(content_desc, new_codec);
2543}
2544
2545// Adds or updates existing codec corresponding to |payload_type| according
2546// to |feedback_param|.
2547template <class T, class U>
2548void UpdateCodec(MediaContentDescription* content_desc, int payload_type,
2549 const cricket::FeedbackParam& feedback_param) {
2550 // Codec might already have been populated (from rtpmap).
changbin.shao@webrtc.org2d25b442015-03-16 04:14:34 +00002551 U new_codec = GetCodecWithPayloadType(static_cast<T*>(content_desc)->codecs(),
2552 payload_type);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002553 AddFeedbackParameter(feedback_param, &new_codec);
2554 AddOrReplaceCodec<T, U>(content_desc, new_codec);
2555}
2556
jlmiller@webrtc.orga744a282015-02-18 21:37:46 +00002557template <class T>
2558bool PopWildcardCodec(std::vector<T>* codecs, T* wildcard_codec) {
2559 for (auto iter = codecs->begin(); iter != codecs->end(); ++iter) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002560 if (iter->id == kWildcardPayloadType) {
2561 *wildcard_codec = *iter;
2562 codecs->erase(iter);
2563 return true;
2564 }
2565 }
2566 return false;
2567}
2568
jlmiller@webrtc.orga744a282015-02-18 21:37:46 +00002569template<class T>
2570void UpdateFromWildcardCodecs(cricket::MediaContentDescriptionImpl<T>* desc) {
2571 auto codecs = desc->codecs();
2572 T wildcard_codec;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002573 if (!PopWildcardCodec(&codecs, &wildcard_codec)) {
2574 return;
2575 }
jlmiller@webrtc.orga744a282015-02-18 21:37:46 +00002576 for (auto& codec : codecs) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002577 AddFeedbackParameters(wildcard_codec.feedback_params, &codec);
2578 }
jlmiller@webrtc.orga744a282015-02-18 21:37:46 +00002579 desc->set_codecs(codecs);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002580}
2581
2582void AddAudioAttribute(const std::string& name, const std::string& value,
2583 AudioContentDescription* audio_desc) {
2584 if (value.empty()) {
2585 return;
2586 }
2587 std::vector<cricket::AudioCodec> codecs = audio_desc->codecs();
2588 for (std::vector<cricket::AudioCodec>::iterator iter = codecs.begin();
2589 iter != codecs.end(); ++iter) {
2590 iter->params[name] = value;
2591 }
2592 audio_desc->set_codecs(codecs);
2593}
2594
2595bool ParseContent(const std::string& message,
2596 const MediaType media_type,
2597 int mline_index,
2598 const std::string& protocol,
deadbeef67cf2c12016-04-13 10:07:16 -07002599 const std::vector<int>& payload_types,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002600 size_t* pos,
2601 std::string* content_name,
deadbeef25ed4352016-12-12 18:37:36 -08002602 bool* bundle_only,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002603 MediaContentDescription* media_desc,
2604 TransportDescription* transport,
2605 std::vector<JsepIceCandidate*>* candidates,
2606 SdpParseError* error) {
nisseede5da42017-01-12 05:15:36 -08002607 RTC_DCHECK(media_desc != NULL);
2608 RTC_DCHECK(content_name != NULL);
2609 RTC_DCHECK(transport != NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002610
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +00002611 if (media_type == cricket::MEDIA_TYPE_AUDIO) {
2612 MaybeCreateStaticPayloadAudioCodecs(
deadbeef67cf2c12016-04-13 10:07:16 -07002613 payload_types, static_cast<AudioContentDescription*>(media_desc));
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +00002614 }
2615
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002616 // The media level "ice-ufrag" and "ice-pwd".
2617 // The candidates before update the media level "ice-pwd" and "ice-ufrag".
2618 Candidates candidates_orig;
2619 std::string line;
2620 std::string mline_id;
2621 // Tracks created out of the ssrc attributes.
2622 StreamParamsVec tracks;
2623 SsrcInfoVec ssrc_infos;
2624 SsrcGroupVec ssrc_groups;
2625 std::string maxptime_as_string;
2626 std::string ptime_as_string;
deadbeef9d3584c2016-02-16 17:54:10 -08002627 std::string stream_id;
2628 std::string track_id;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002629
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002630 // Loop until the next m line
2631 while (!IsLineType(message, kLineTypeMedia, *pos)) {
2632 if (!GetLine(message, pos, &line)) {
2633 if (*pos >= message.size()) {
2634 break; // Done parsing
2635 } else {
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +00002636 return ParseFailed(message, *pos, "Invalid SDP line.", error);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002637 }
2638 }
2639
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002640 // RFC 4566
2641 // b=* (zero or more bandwidth information lines)
2642 if (IsLineType(line, kLineTypeSessionBandwidth)) {
2643 std::string bandwidth;
2644 if (HasAttribute(line, kApplicationSpecificMaximum)) {
2645 if (!GetValue(line, kApplicationSpecificMaximum, &bandwidth, error)) {
2646 return false;
2647 } else {
wu@webrtc.org5e760e72014-04-02 23:19:09 +00002648 int b = 0;
2649 if (!GetValueFromString(line, bandwidth, &b, error)) {
2650 return false;
2651 }
Peter Thatcherc0c3a862015-06-24 15:31:25 -07002652 // We should never use more than the default bandwidth for RTP-based
2653 // data channels. Don't allow SDP to set the bandwidth, because
2654 // that would give JS the opportunity to "break the Internet".
2655 // See: https://code.google.com/p/chromium/issues/detail?id=280726
2656 if (media_type == cricket::MEDIA_TYPE_DATA && IsRtp(protocol) &&
2657 b > cricket::kDataMaxBandwidth / 1000) {
2658 std::ostringstream description;
2659 description << "RTP-based data channels may not send more than "
2660 << cricket::kDataMaxBandwidth / 1000 << "kbps.";
2661 return ParseFailed(line, description.str(), error);
2662 }
deadbeefb2362572016-12-13 16:37:06 -08002663 // Prevent integer overflow.
2664 b = std::min(b, INT_MAX / 1000);
wu@webrtc.org5e760e72014-04-02 23:19:09 +00002665 media_desc->set_bandwidth(b * 1000);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002666 }
2667 }
2668 continue;
2669 }
2670
2671 if (!IsLineType(line, kLineTypeAttributes)) {
2672 // TODO: Handle other lines if needed.
2673 LOG(LS_INFO) << "Ignored line: " << line;
2674 continue;
2675 }
2676
2677 // Handle attributes common to SCTP and RTP.
2678 if (HasAttribute(line, kAttributeMid)) {
2679 // RFC 3388
2680 // mid-attribute = "a=mid:" identification-tag
2681 // identification-tag = token
2682 // Use the mid identification-tag as the content name.
2683 if (!GetValue(line, kAttributeMid, &mline_id, error)) {
2684 return false;
2685 }
2686 *content_name = mline_id;
deadbeef25ed4352016-12-12 18:37:36 -08002687 } else if (HasAttribute(line, kAttributeBundleOnly)) {
2688 *bundle_only = true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002689 } else if (HasAttribute(line, kAttributeCandidate)) {
2690 Candidate candidate;
2691 if (!ParseCandidate(line, &candidate, error, false)) {
2692 return false;
2693 }
deadbeef7bcdb692017-01-20 12:43:58 -08002694 // ParseCandidate will parse non-standard ufrag and password attributes,
2695 // since it's used for candidate trickling, but we only want to process
2696 // the "a=ice-ufrag"/"a=ice-pwd" values in a session description, so
2697 // strip them off at this point.
2698 candidate.set_username(std::string());
2699 candidate.set_password(std::string());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002700 candidates_orig.push_back(candidate);
2701 } else if (HasAttribute(line, kAttributeIceUfrag)) {
2702 if (!GetValue(line, kAttributeIceUfrag, &transport->ice_ufrag, error)) {
2703 return false;
2704 }
2705 } else if (HasAttribute(line, kAttributeIcePwd)) {
2706 if (!GetValue(line, kAttributeIcePwd, &transport->ice_pwd, error)) {
2707 return false;
2708 }
2709 } else if (HasAttribute(line, kAttributeIceOption)) {
2710 if (!ParseIceOptions(line, &transport->transport_options, error)) {
2711 return false;
2712 }
2713 } else if (HasAttribute(line, kAttributeFmtp)) {
2714 if (!ParseFmtpAttributes(line, media_type, media_desc, error)) {
2715 return false;
2716 }
2717 } else if (HasAttribute(line, kAttributeFingerprint)) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002718 rtc::SSLFingerprint* fingerprint = NULL;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002719
2720 if (!ParseFingerprintAttribute(line, &fingerprint, error)) {
2721 return false;
2722 }
2723 transport->identity_fingerprint.reset(fingerprint);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00002724 } else if (HasAttribute(line, kAttributeSetup)) {
2725 if (!ParseDtlsSetup(line, &(transport->connection_role), error)) {
2726 return false;
2727 }
pthatcher@webrtc.org3341b402015-02-13 21:14:22 +00002728 } else if (IsDtlsSctp(protocol) && HasAttribute(line, kAttributeSctpPort)) {
deadbeef7e146cb2016-09-28 10:04:34 -07002729 if (media_type != cricket::MEDIA_TYPE_DATA) {
2730 return ParseFailed(
2731 line, "sctp-port attribute found in non-data media description.",
2732 error);
2733 }
jiayl@webrtc.orgddb85ab2014-09-05 16:31:56 +00002734 int sctp_port;
2735 if (!ParseSctpPort(line, &sctp_port, error)) {
2736 return false;
2737 }
2738 if (!AddSctpDataCodec(static_cast<DataContentDescription*>(media_desc),
2739 sctp_port)) {
2740 return false;
2741 }
pthatcher@webrtc.org3341b402015-02-13 21:14:22 +00002742 } else if (IsRtp(protocol)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002743 //
2744 // RTP specific attrubtes
2745 //
2746 if (HasAttribute(line, kAttributeRtcpMux)) {
2747 media_desc->set_rtcp_mux(true);
deadbeef13871492015-12-09 12:37:51 -08002748 } else if (HasAttribute(line, kAttributeRtcpReducedSize)) {
2749 media_desc->set_rtcp_reduced_size(true);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002750 } else if (HasAttribute(line, kAttributeSsrcGroup)) {
2751 if (!ParseSsrcGroupAttribute(line, &ssrc_groups, error)) {
2752 return false;
2753 }
2754 } else if (HasAttribute(line, kAttributeSsrc)) {
2755 if (!ParseSsrcAttribute(line, &ssrc_infos, error)) {
2756 return false;
2757 }
2758 } else if (HasAttribute(line, kAttributeCrypto)) {
2759 if (!ParseCryptoAttribute(line, media_desc, error)) {
2760 return false;
2761 }
2762 } else if (HasAttribute(line, kAttributeRtpmap)) {
deadbeef67cf2c12016-04-13 10:07:16 -07002763 if (!ParseRtpmapAttribute(line, media_type, payload_types, media_desc,
2764 error)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002765 return false;
2766 }
2767 } else if (HasAttribute(line, kCodecParamMaxPTime)) {
2768 if (!GetValue(line, kCodecParamMaxPTime, &maxptime_as_string, error)) {
2769 return false;
2770 }
2771 } else if (HasAttribute(line, kAttributeRtcpFb)) {
2772 if (!ParseRtcpFbAttribute(line, media_type, media_desc, error)) {
2773 return false;
2774 }
2775 } else if (HasAttribute(line, kCodecParamPTime)) {
2776 if (!GetValue(line, kCodecParamPTime, &ptime_as_string, error)) {
2777 return false;
2778 }
2779 } else if (HasAttribute(line, kAttributeSendOnly)) {
2780 media_desc->set_direction(cricket::MD_SENDONLY);
2781 } else if (HasAttribute(line, kAttributeRecvOnly)) {
2782 media_desc->set_direction(cricket::MD_RECVONLY);
2783 } else if (HasAttribute(line, kAttributeInactive)) {
2784 media_desc->set_direction(cricket::MD_INACTIVE);
2785 } else if (HasAttribute(line, kAttributeSendRecv)) {
2786 media_desc->set_direction(cricket::MD_SENDRECV);
2787 } else if (HasAttribute(line, kAttributeExtmap)) {
isheriff6f8d6862016-05-26 11:24:55 -07002788 RtpExtension extmap;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002789 if (!ParseExtmap(line, &extmap, error)) {
2790 return false;
2791 }
2792 media_desc->AddRtpHeaderExtension(extmap);
2793 } else if (HasAttribute(line, kAttributeXGoogleFlag)) {
2794 // Experimental attribute. Conference mode activates more aggressive
2795 // AEC and NS settings.
2796 // TODO: expose API to set these directly.
2797 std::string flag_value;
2798 if (!GetValue(line, kAttributeXGoogleFlag, &flag_value, error)) {
2799 return false;
2800 }
2801 if (flag_value.compare(kValueConference) == 0)
2802 media_desc->set_conference_mode(true);
deadbeef9d3584c2016-02-16 17:54:10 -08002803 } else if (HasAttribute(line, kAttributeMsid)) {
2804 if (!ParseMsidAttribute(line, &stream_id, &track_id, error)) {
2805 return false;
2806 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002807 }
2808 } else {
2809 // Only parse lines that we are interested of.
2810 LOG(LS_INFO) << "Ignored line: " << line;
2811 continue;
2812 }
2813 }
2814
2815 // Create tracks from the |ssrc_infos|.
Taylor Brandstetter5de6b752016-03-09 17:02:30 -08002816 // If the stream_id/track_id for all SSRCS are identical, one StreamParams
2817 // will be created in CreateTracksFromSsrcInfos, containing all the SSRCs from
2818 // the m= section.
2819 CreateTracksFromSsrcInfos(ssrc_infos, stream_id, track_id, &tracks);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002820
2821 // Add the ssrc group to the track.
2822 for (SsrcGroupVec::iterator ssrc_group = ssrc_groups.begin();
2823 ssrc_group != ssrc_groups.end(); ++ssrc_group) {
2824 if (ssrc_group->ssrcs.empty()) {
2825 continue;
2826 }
Peter Boström0c4e06b2015-10-07 12:23:21 +02002827 uint32_t ssrc = ssrc_group->ssrcs.front();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002828 for (StreamParamsVec::iterator track = tracks.begin();
2829 track != tracks.end(); ++track) {
2830 if (track->has_ssrc(ssrc)) {
2831 track->ssrc_groups.push_back(*ssrc_group);
2832 }
2833 }
2834 }
2835
2836 // Add the new tracks to the |media_desc|.
deadbeef9d3584c2016-02-16 17:54:10 -08002837 for (StreamParams& track : tracks) {
2838 media_desc->AddStream(track);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002839 }
2840
2841 if (media_type == cricket::MEDIA_TYPE_AUDIO) {
2842 AudioContentDescription* audio_desc =
2843 static_cast<AudioContentDescription*>(media_desc);
jlmiller@webrtc.orga744a282015-02-18 21:37:46 +00002844 UpdateFromWildcardCodecs(audio_desc);
2845
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002846 // Verify audio codec ensures that no audio codec has been populated with
2847 // only fmtp.
2848 if (!VerifyAudioCodecs(audio_desc)) {
2849 return ParseFailed("Failed to parse audio codecs correctly.", error);
2850 }
2851 AddAudioAttribute(kCodecParamMaxPTime, maxptime_as_string, audio_desc);
2852 AddAudioAttribute(kCodecParamPTime, ptime_as_string, audio_desc);
2853 }
2854
2855 if (media_type == cricket::MEDIA_TYPE_VIDEO) {
jlmiller@webrtc.orga744a282015-02-18 21:37:46 +00002856 VideoContentDescription* video_desc =
2857 static_cast<VideoContentDescription*>(media_desc);
2858 UpdateFromWildcardCodecs(video_desc);
2859 // Verify video codec ensures that no video codec has been populated with
2860 // only rtcp-fb.
2861 if (!VerifyVideoCodecs(video_desc)) {
2862 return ParseFailed("Failed to parse video codecs correctly.", error);
2863 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002864 }
2865
2866 // RFC 5245
2867 // Update the candidates with the media level "ice-pwd" and "ice-ufrag".
2868 for (Candidates::iterator it = candidates_orig.begin();
2869 it != candidates_orig.end(); ++it) {
nisseede5da42017-01-12 05:15:36 -08002870 RTC_DCHECK((*it).username().empty() ||
2871 (*it).username() == transport->ice_ufrag);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002872 (*it).set_username(transport->ice_ufrag);
nisseede5da42017-01-12 05:15:36 -08002873 RTC_DCHECK((*it).password().empty());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002874 (*it).set_password(transport->ice_pwd);
2875 candidates->push_back(
2876 new JsepIceCandidate(mline_id, mline_index, *it));
2877 }
2878 return true;
2879}
2880
2881bool ParseSsrcAttribute(const std::string& line, SsrcInfoVec* ssrc_infos,
2882 SdpParseError* error) {
nisseede5da42017-01-12 05:15:36 -08002883 RTC_DCHECK(ssrc_infos != NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002884 // RFC 5576
2885 // a=ssrc:<ssrc-id> <attribute>
2886 // a=ssrc:<ssrc-id> <attribute>:<value>
2887 std::string field1, field2;
Donald Curtis144d0182015-05-15 13:14:24 -07002888 if (!rtc::tokenize_first(line.substr(kLinePrefixLength), kSdpDelimiterSpace,
2889 &field1, &field2)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002890 const size_t expected_fields = 2;
2891 return ParseFailedExpectFieldNum(line, expected_fields, error);
2892 }
2893
2894 // ssrc:<ssrc-id>
2895 std::string ssrc_id_s;
2896 if (!GetValue(field1, kAttributeSsrc, &ssrc_id_s, error)) {
2897 return false;
2898 }
Peter Boström0c4e06b2015-10-07 12:23:21 +02002899 uint32_t ssrc_id = 0;
wu@webrtc.org5e760e72014-04-02 23:19:09 +00002900 if (!GetValueFromString(line, ssrc_id_s, &ssrc_id, error)) {
2901 return false;
2902 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002903
2904 std::string attribute;
2905 std::string value;
Donald Curtis144d0182015-05-15 13:14:24 -07002906 if (!rtc::tokenize_first(field2, kSdpDelimiterColon, &attribute, &value)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002907 std::ostringstream description;
2908 description << "Failed to get the ssrc attribute value from " << field2
2909 << ". Expected format <attribute>:<value>.";
2910 return ParseFailed(line, description.str(), error);
2911 }
2912
2913 // Check if there's already an item for this |ssrc_id|. Create a new one if
2914 // there isn't.
2915 SsrcInfoVec::iterator ssrc_info = ssrc_infos->begin();
2916 for (; ssrc_info != ssrc_infos->end(); ++ssrc_info) {
2917 if (ssrc_info->ssrc_id == ssrc_id) {
2918 break;
2919 }
2920 }
2921 if (ssrc_info == ssrc_infos->end()) {
2922 SsrcInfo info;
2923 info.ssrc_id = ssrc_id;
2924 ssrc_infos->push_back(info);
2925 ssrc_info = ssrc_infos->end() - 1;
2926 }
2927
2928 // Store the info to the |ssrc_info|.
2929 if (attribute == kSsrcAttributeCname) {
2930 // RFC 5576
2931 // cname:<value>
2932 ssrc_info->cname = value;
2933 } else if (attribute == kSsrcAttributeMsid) {
2934 // draft-alvestrand-mmusic-msid-00
2935 // "msid:" identifier [ " " appdata ]
2936 std::vector<std::string> fields;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002937 rtc::split(value, kSdpDelimiterSpace, &fields);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002938 if (fields.size() < 1 || fields.size() > 2) {
2939 return ParseFailed(line,
2940 "Expected format \"msid:<identifier>[ <appdata>]\".",
2941 error);
2942 }
deadbeef9d3584c2016-02-16 17:54:10 -08002943 ssrc_info->stream_id = fields[0];
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002944 if (fields.size() == 2) {
deadbeef9d3584c2016-02-16 17:54:10 -08002945 ssrc_info->track_id = fields[1];
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002946 }
2947 } else if (attribute == kSsrcAttributeMslabel) {
2948 // draft-alvestrand-rtcweb-mid-01
2949 // mslabel:<value>
2950 ssrc_info->mslabel = value;
2951 } else if (attribute == kSSrcAttributeLabel) {
2952 // The label isn't defined.
2953 // label:<value>
2954 ssrc_info->label = value;
2955 }
2956 return true;
2957}
2958
2959bool ParseSsrcGroupAttribute(const std::string& line,
2960 SsrcGroupVec* ssrc_groups,
2961 SdpParseError* error) {
nisseede5da42017-01-12 05:15:36 -08002962 RTC_DCHECK(ssrc_groups != NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002963 // RFC 5576
2964 // a=ssrc-group:<semantics> <ssrc-id> ...
2965 std::vector<std::string> fields;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002966 rtc::split(line.substr(kLinePrefixLength),
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002967 kSdpDelimiterSpace, &fields);
2968 const size_t expected_min_fields = 2;
2969 if (fields.size() < expected_min_fields) {
2970 return ParseFailedExpectMinFieldNum(line, expected_min_fields, error);
2971 }
2972 std::string semantics;
2973 if (!GetValue(fields[0], kAttributeSsrcGroup, &semantics, error)) {
2974 return false;
2975 }
Peter Boström0c4e06b2015-10-07 12:23:21 +02002976 std::vector<uint32_t> ssrcs;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002977 for (size_t i = 1; i < fields.size(); ++i) {
Peter Boström0c4e06b2015-10-07 12:23:21 +02002978 uint32_t ssrc = 0;
wu@webrtc.org5e760e72014-04-02 23:19:09 +00002979 if (!GetValueFromString(line, fields[i], &ssrc, error)) {
2980 return false;
2981 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002982 ssrcs.push_back(ssrc);
2983 }
2984 ssrc_groups->push_back(SsrcGroup(semantics, ssrcs));
2985 return true;
2986}
2987
2988bool ParseCryptoAttribute(const std::string& line,
2989 MediaContentDescription* media_desc,
2990 SdpParseError* error) {
2991 std::vector<std::string> fields;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002992 rtc::split(line.substr(kLinePrefixLength),
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002993 kSdpDelimiterSpace, &fields);
2994 // RFC 4568
2995 // a=crypto:<tag> <crypto-suite> <key-params> [<session-params>]
2996 const size_t expected_min_fields = 3;
2997 if (fields.size() < expected_min_fields) {
2998 return ParseFailedExpectMinFieldNum(line, expected_min_fields, error);
2999 }
3000 std::string tag_value;
3001 if (!GetValue(fields[0], kAttributeCrypto, &tag_value, error)) {
3002 return false;
3003 }
wu@webrtc.org5e760e72014-04-02 23:19:09 +00003004 int tag = 0;
3005 if (!GetValueFromString(line, tag_value, &tag, error)) {
3006 return false;
3007 }
jbauch083b73f2015-07-16 02:46:32 -07003008 const std::string& crypto_suite = fields[1];
3009 const std::string& key_params = fields[2];
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003010 std::string session_params;
3011 if (fields.size() > 3) {
3012 session_params = fields[3];
3013 }
3014 media_desc->AddCrypto(CryptoParams(tag, crypto_suite, key_params,
3015 session_params));
3016 return true;
3017}
3018
3019// Updates or creates a new codec entry in the audio description with according
deadbeef67cf2c12016-04-13 10:07:16 -07003020// to |name|, |clockrate|, |bitrate|, and |channels|.
3021void UpdateCodec(int payload_type,
3022 const std::string& name,
3023 int clockrate,
3024 int bitrate,
3025 size_t channels,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003026 AudioContentDescription* audio_desc) {
3027 // Codec may already be populated with (only) optional parameters
3028 // (from an fmtp).
changbin.shao@webrtc.org2d25b442015-03-16 04:14:34 +00003029 cricket::AudioCodec codec =
3030 GetCodecWithPayloadType(audio_desc->codecs(), payload_type);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003031 codec.name = name;
3032 codec.clockrate = clockrate;
3033 codec.bitrate = bitrate;
3034 codec.channels = channels;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003035 AddOrReplaceCodec<AudioContentDescription, cricket::AudioCodec>(audio_desc,
3036 codec);
3037}
3038
3039// Updates or creates a new codec entry in the video description according to
deadbeef67cf2c12016-04-13 10:07:16 -07003040// |name|, |width|, |height|, and |framerate|.
3041void UpdateCodec(int payload_type,
3042 const std::string& name,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003043 VideoContentDescription* video_desc) {
3044 // Codec may already be populated with (only) optional parameters
3045 // (from an fmtp).
changbin.shao@webrtc.org2d25b442015-03-16 04:14:34 +00003046 cricket::VideoCodec codec =
3047 GetCodecWithPayloadType(video_desc->codecs(), payload_type);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003048 codec.name = name;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003049 AddOrReplaceCodec<VideoContentDescription, cricket::VideoCodec>(video_desc,
3050 codec);
3051}
3052
3053bool ParseRtpmapAttribute(const std::string& line,
3054 const MediaType media_type,
deadbeef67cf2c12016-04-13 10:07:16 -07003055 const std::vector<int>& payload_types,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003056 MediaContentDescription* media_desc,
3057 SdpParseError* error) {
3058 std::vector<std::string> fields;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00003059 rtc::split(line.substr(kLinePrefixLength),
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003060 kSdpDelimiterSpace, &fields);
3061 // RFC 4566
3062 // a=rtpmap:<payload type> <encoding name>/<clock rate>[/<encodingparameters>]
3063 const size_t expected_min_fields = 2;
3064 if (fields.size() < expected_min_fields) {
3065 return ParseFailedExpectMinFieldNum(line, expected_min_fields, error);
3066 }
3067 std::string payload_type_value;
3068 if (!GetValue(fields[0], kAttributeRtpmap, &payload_type_value, error)) {
3069 return false;
3070 }
wu@webrtc.org5e760e72014-04-02 23:19:09 +00003071 int payload_type = 0;
pkasting@chromium.orge9facf82015-02-17 20:36:28 +00003072 if (!GetPayloadTypeFromString(line, payload_type_value, &payload_type,
3073 error)) {
wu@webrtc.org5e760e72014-04-02 23:19:09 +00003074 return false;
3075 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003076
deadbeef67cf2c12016-04-13 10:07:16 -07003077 if (std::find(payload_types.begin(), payload_types.end(), payload_type) ==
3078 payload_types.end()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003079 LOG(LS_WARNING) << "Ignore rtpmap line that did not appear in the "
3080 << "<fmt> of the m-line: " << line;
3081 return true;
3082 }
jbauch083b73f2015-07-16 02:46:32 -07003083 const std::string& encoder = fields[1];
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003084 std::vector<std::string> codec_params;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00003085 rtc::split(encoder, '/', &codec_params);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003086 // <encoding name>/<clock rate>[/<encodingparameters>]
3087 // 2 mandatory fields
3088 if (codec_params.size() < 2 || codec_params.size() > 3) {
3089 return ParseFailed(line,
3090 "Expected format \"<encoding name>/<clock rate>"
3091 "[/<encodingparameters>]\".",
3092 error);
3093 }
jbauch083b73f2015-07-16 02:46:32 -07003094 const std::string& encoding_name = codec_params[0];
wu@webrtc.org5e760e72014-04-02 23:19:09 +00003095 int clock_rate = 0;
3096 if (!GetValueFromString(line, codec_params[1], &clock_rate, error)) {
3097 return false;
3098 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003099 if (media_type == cricket::MEDIA_TYPE_VIDEO) {
3100 VideoContentDescription* video_desc =
3101 static_cast<VideoContentDescription*>(media_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003102 UpdateCodec(payload_type, encoding_name,
deadbeef67cf2c12016-04-13 10:07:16 -07003103 video_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003104 } else if (media_type == cricket::MEDIA_TYPE_AUDIO) {
3105 // RFC 4566
3106 // For audio streams, <encoding parameters> indicates the number
3107 // of audio channels. This parameter is OPTIONAL and may be
3108 // omitted if the number of channels is one, provided that no
3109 // additional parameters are needed.
Peter Kasting69558702016-01-12 16:26:35 -08003110 size_t channels = 1;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003111 if (codec_params.size() == 3) {
wu@webrtc.org5e760e72014-04-02 23:19:09 +00003112 if (!GetValueFromString(line, codec_params[2], &channels, error)) {
3113 return false;
3114 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003115 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003116 AudioContentDescription* audio_desc =
3117 static_cast<AudioContentDescription*>(media_desc);
ossue1405ad2017-01-23 08:55:48 -08003118 UpdateCodec(payload_type, encoding_name, clock_rate, 0, channels,
deadbeef67cf2c12016-04-13 10:07:16 -07003119 audio_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003120 } else if (media_type == cricket::MEDIA_TYPE_DATA) {
3121 DataContentDescription* data_desc =
3122 static_cast<DataContentDescription*>(media_desc);
deadbeef67cf2c12016-04-13 10:07:16 -07003123 data_desc->AddCodec(cricket::DataCodec(payload_type, encoding_name));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003124 }
3125 return true;
3126}
3127
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003128bool ParseFmtpParam(const std::string& line, std::string* parameter,
3129 std::string* value, SdpParseError* error) {
Donald Curtis0e07f922015-05-15 09:21:23 -07003130 if (!rtc::tokenize_first(line, kSdpDelimiterEqual, parameter, value)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003131 ParseFailed(line, "Unable to parse fmtp parameter. \'=\' missing.", error);
3132 return false;
3133 }
3134 // a=fmtp:<payload_type> <param1>=<value1>; <param2>=<value2>; ...
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003135 return true;
3136}
3137
3138bool ParseFmtpAttributes(const std::string& line, const MediaType media_type,
3139 MediaContentDescription* media_desc,
3140 SdpParseError* error) {
3141 if (media_type != cricket::MEDIA_TYPE_AUDIO &&
3142 media_type != cricket::MEDIA_TYPE_VIDEO) {
3143 return true;
3144 }
Donald Curtis0e07f922015-05-15 09:21:23 -07003145
3146 std::string line_payload;
3147 std::string line_params;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003148
3149 // RFC 5576
3150 // a=fmtp:<format> <format specific parameters>
3151 // At least two fields, whereas the second one is any of the optional
3152 // parameters.
Donald Curtis144d0182015-05-15 13:14:24 -07003153 if (!rtc::tokenize_first(line.substr(kLinePrefixLength), kSdpDelimiterSpace,
3154 &line_payload, &line_params)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003155 ParseFailedExpectMinFieldNum(line, 2, error);
3156 return false;
3157 }
3158
Donald Curtis0e07f922015-05-15 09:21:23 -07003159 // Parse out the payload information.
pkasting@chromium.orgd3245462015-02-23 21:28:22 +00003160 std::string payload_type_str;
Donald Curtis0e07f922015-05-15 09:21:23 -07003161 if (!GetValue(line_payload, kAttributeFmtp, &payload_type_str, error)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003162 return false;
3163 }
3164
Donald Curtis0e07f922015-05-15 09:21:23 -07003165 int payload_type = 0;
Donald Curtis144d0182015-05-15 13:14:24 -07003166 if (!GetPayloadTypeFromString(line_payload, payload_type_str, &payload_type,
3167 error)) {
Donald Curtis0e07f922015-05-15 09:21:23 -07003168 return false;
3169 }
3170
3171 // Parse out format specific parameters.
3172 std::vector<std::string> fields;
3173 rtc::split(line_params, kSdpDelimiterSemicolon, &fields);
3174
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003175 cricket::CodecParameterMap codec_params;
Donald Curtis144d0182015-05-15 13:14:24 -07003176 for (auto& iter : fields) {
Donald Curtis0e07f922015-05-15 09:21:23 -07003177 if (iter.find(kSdpDelimiterEqual) == std::string::npos) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003178 // Only fmtps with equals are currently supported. Other fmtp types
3179 // should be ignored. Unknown fmtps do not constitute an error.
3180 continue;
3181 }
Donald Curtis0e07f922015-05-15 09:21:23 -07003182
3183 std::string name;
3184 std::string value;
3185 if (!ParseFmtpParam(rtc::string_trim(iter), &name, &value, error)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003186 return false;
3187 }
3188 codec_params[name] = value;
3189 }
3190
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003191 if (media_type == cricket::MEDIA_TYPE_AUDIO) {
3192 UpdateCodec<AudioContentDescription, cricket::AudioCodec>(
pkasting@chromium.orgd3245462015-02-23 21:28:22 +00003193 media_desc, payload_type, codec_params);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003194 } else if (media_type == cricket::MEDIA_TYPE_VIDEO) {
3195 UpdateCodec<VideoContentDescription, cricket::VideoCodec>(
pkasting@chromium.orgd3245462015-02-23 21:28:22 +00003196 media_desc, payload_type, codec_params);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003197 }
3198 return true;
3199}
3200
3201bool ParseRtcpFbAttribute(const std::string& line, const MediaType media_type,
3202 MediaContentDescription* media_desc,
3203 SdpParseError* error) {
3204 if (media_type != cricket::MEDIA_TYPE_AUDIO &&
3205 media_type != cricket::MEDIA_TYPE_VIDEO) {
3206 return true;
3207 }
3208 std::vector<std::string> rtcp_fb_fields;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00003209 rtc::split(line.c_str(), kSdpDelimiterSpace, &rtcp_fb_fields);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003210 if (rtcp_fb_fields.size() < 2) {
3211 return ParseFailedGetValue(line, kAttributeRtcpFb, error);
3212 }
3213 std::string payload_type_string;
3214 if (!GetValue(rtcp_fb_fields[0], kAttributeRtcpFb, &payload_type_string,
3215 error)) {
3216 return false;
3217 }
wu@webrtc.org5e760e72014-04-02 23:19:09 +00003218 int payload_type = kWildcardPayloadType;
3219 if (payload_type_string != "*") {
pkasting@chromium.orge9facf82015-02-17 20:36:28 +00003220 if (!GetPayloadTypeFromString(line, payload_type_string, &payload_type,
3221 error)) {
wu@webrtc.org5e760e72014-04-02 23:19:09 +00003222 return false;
3223 }
3224 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003225 std::string id = rtcp_fb_fields[1];
3226 std::string param = "";
3227 for (std::vector<std::string>::iterator iter = rtcp_fb_fields.begin() + 2;
3228 iter != rtcp_fb_fields.end(); ++iter) {
3229 param.append(*iter);
3230 }
3231 const cricket::FeedbackParam feedback_param(id, param);
3232
3233 if (media_type == cricket::MEDIA_TYPE_AUDIO) {
pkasting@chromium.orgd3245462015-02-23 21:28:22 +00003234 UpdateCodec<AudioContentDescription, cricket::AudioCodec>(
3235 media_desc, payload_type, feedback_param);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003236 } else if (media_type == cricket::MEDIA_TYPE_VIDEO) {
pkasting@chromium.orgd3245462015-02-23 21:28:22 +00003237 UpdateCodec<VideoContentDescription, cricket::VideoCodec>(
3238 media_desc, payload_type, feedback_param);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003239 }
3240 return true;
3241}
3242
3243} // namespace webrtc