blob: b749d6ff324887c8bce74c933c941b61b3fc1b28 [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
207static const int kDefaultVideoClockrate = 90000;
208
wu@webrtc.org78187522013-10-07 23:32:02 +0000209static const char kDefaultSctpmapProtocol[] = "webrtc-datachannel";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000210
pkasting@chromium.orgd3245462015-02-23 21:28:22 +0000211// RTP payload type is in the 0-127 range. Use -1 to indicate "all" payload
212// types.
213const int kWildcardPayloadType = -1;
214
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000215struct SsrcInfo {
Peter Boström0c4e06b2015-10-07 12:23:21 +0200216 uint32_t ssrc_id;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000217 std::string cname;
deadbeef9d3584c2016-02-16 17:54:10 -0800218 std::string stream_id;
219 std::string track_id;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000220
221 // For backward compatibility.
222 // TODO(ronghuawu): Remove below 2 fields once all the clients support msid.
223 std::string label;
224 std::string mslabel;
225};
226typedef std::vector<SsrcInfo> SsrcInfoVec;
227typedef std::vector<SsrcGroup> SsrcGroupVec;
228
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000229template <class T>
230static void AddFmtpLine(const T& codec, std::string* message);
231static void BuildMediaDescription(const ContentInfo* content_info,
232 const TransportInfo* transport_info,
233 const MediaType media_type,
wu@webrtc.org4c3e9912014-07-16 21:03:13 +0000234 const std::vector<Candidate>& candidates,
deadbeef9d3584c2016-02-16 17:54:10 -0800235 bool unified_plan_sdp,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000236 std::string* message);
zstein4b2e0822017-02-17 19:48:38 -0800237static void BuildSctpContentAttributes(std::string* message,
238 int sctp_port,
239 bool use_sctpmap);
deadbeef9d3584c2016-02-16 17:54:10 -0800240static void BuildRtpContentAttributes(const MediaContentDescription* media_desc,
241 const MediaType media_type,
242 bool unified_plan_sdp,
243 std::string* message);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000244static void BuildRtpMap(const MediaContentDescription* media_desc,
245 const MediaType media_type,
246 std::string* message);
247static void BuildCandidate(const std::vector<Candidate>& candidates,
honghaiza54a0802015-12-16 18:37:23 -0800248 bool include_ufrag,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000249 std::string* message);
250static void BuildIceOptions(const std::vector<std::string>& transport_options,
251 std::string* message);
pthatcher@webrtc.org3341b402015-02-13 21:14:22 +0000252static bool IsRtp(const std::string& protocol);
253static bool IsDtlsSctp(const std::string& protocol);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000254static bool ParseSessionDescription(const std::string& message, size_t* pos,
255 std::string* session_id,
256 std::string* session_version,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000257 TransportDescription* session_td,
258 RtpHeaderExtensions* session_extmaps,
259 cricket::SessionDescription* desc,
260 SdpParseError* error);
261static bool ParseGroupAttribute(const std::string& line,
262 cricket::SessionDescription* desc,
263 SdpParseError* error);
264static bool ParseMediaDescription(
265 const std::string& message,
266 const TransportDescription& session_td,
267 const RtpHeaderExtensions& session_extmaps,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000268 size_t* pos, cricket::SessionDescription* desc,
269 std::vector<JsepIceCandidate*>* candidates,
270 SdpParseError* error);
271static bool ParseContent(const std::string& message,
272 const MediaType media_type,
273 int mline_index,
274 const std::string& protocol,
deadbeef67cf2c12016-04-13 10:07:16 -0700275 const std::vector<int>& payload_types,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000276 size_t* pos,
277 std::string* content_name,
deadbeef25ed4352016-12-12 18:37:36 -0800278 bool* bundle_only,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000279 MediaContentDescription* media_desc,
280 TransportDescription* transport,
281 std::vector<JsepIceCandidate*>* candidates,
282 SdpParseError* error);
283static bool ParseSsrcAttribute(const std::string& line,
284 SsrcInfoVec* ssrc_infos,
285 SdpParseError* error);
286static bool ParseSsrcGroupAttribute(const std::string& line,
287 SsrcGroupVec* ssrc_groups,
288 SdpParseError* error);
289static bool ParseCryptoAttribute(const std::string& line,
290 MediaContentDescription* media_desc,
291 SdpParseError* error);
292static bool ParseRtpmapAttribute(const std::string& line,
293 const MediaType media_type,
deadbeef67cf2c12016-04-13 10:07:16 -0700294 const std::vector<int>& payload_types,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000295 MediaContentDescription* media_desc,
296 SdpParseError* error);
297static bool ParseFmtpAttributes(const std::string& line,
298 const MediaType media_type,
299 MediaContentDescription* media_desc,
300 SdpParseError* error);
301static bool ParseFmtpParam(const std::string& line, std::string* parameter,
302 std::string* value, SdpParseError* error);
303static bool ParseCandidate(const std::string& message, Candidate* candidate,
304 SdpParseError* error, bool is_raw);
305static bool ParseRtcpFbAttribute(const std::string& line,
306 const MediaType media_type,
307 MediaContentDescription* media_desc,
308 SdpParseError* error);
309static bool ParseIceOptions(const std::string& line,
310 std::vector<std::string>* transport_options,
311 SdpParseError* error);
312static bool ParseExtmap(const std::string& line,
isheriff6f8d6862016-05-26 11:24:55 -0700313 RtpExtension* extmap,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000314 SdpParseError* error);
315static bool ParseFingerprintAttribute(const std::string& line,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000316 rtc::SSLFingerprint** fingerprint,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000317 SdpParseError* error);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +0000318static bool ParseDtlsSetup(const std::string& line,
319 cricket::ConnectionRole* role,
320 SdpParseError* error);
deadbeef9d3584c2016-02-16 17:54:10 -0800321static bool ParseMsidAttribute(const std::string& line,
322 std::string* stream_id,
323 std::string* track_id,
324 SdpParseError* error);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000325
326// Helper functions
327
328// Below ParseFailed*** functions output the line that caused the parsing
329// failure and the detailed reason (|description|) of the failure to |error|.
330// The functions always return false so that they can be used directly in the
331// following way when error happens:
332// "return ParseFailed***(...);"
333
334// The line starting at |line_start| of |message| is the failing line.
335// The reason for the failure should be provided in the |description|.
336// An example of a description could be "unknown character".
337static bool ParseFailed(const std::string& message,
338 size_t line_start,
339 const std::string& description,
340 SdpParseError* error) {
341 // Get the first line of |message| from |line_start|.
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +0000342 std::string first_line;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000343 size_t line_end = message.find(kNewLine, line_start);
344 if (line_end != std::string::npos) {
345 if (line_end > 0 && (message.at(line_end - 1) == kReturn)) {
346 --line_end;
347 }
348 first_line = message.substr(line_start, (line_end - line_start));
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +0000349 } else {
350 first_line = message.substr(line_start);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000351 }
352
353 if (error) {
354 error->line = first_line;
355 error->description = description;
356 }
357 LOG(LS_ERROR) << "Failed to parse: \"" << first_line
358 << "\". Reason: " << description;
359 return false;
360}
361
362// |line| is the failing line. The reason for the failure should be
363// provided in the |description|.
364static bool ParseFailed(const std::string& line,
365 const std::string& description,
366 SdpParseError* error) {
367 return ParseFailed(line, 0, description, error);
368}
369
370// Parses failure where the failing SDP line isn't know or there are multiple
371// failing lines.
372static bool ParseFailed(const std::string& description,
373 SdpParseError* error) {
374 return ParseFailed("", description, error);
375}
376
377// |line| is the failing line. The failure is due to the fact that |line|
378// doesn't have |expected_fields| fields.
379static bool ParseFailedExpectFieldNum(const std::string& line,
380 int expected_fields,
381 SdpParseError* error) {
382 std::ostringstream description;
383 description << "Expects " << expected_fields << " fields.";
384 return ParseFailed(line, description.str(), error);
385}
386
387// |line| is the failing line. The failure is due to the fact that |line| has
388// less than |expected_min_fields| fields.
389static bool ParseFailedExpectMinFieldNum(const std::string& line,
390 int expected_min_fields,
391 SdpParseError* error) {
392 std::ostringstream description;
393 description << "Expects at least " << expected_min_fields << " fields.";
394 return ParseFailed(line, description.str(), error);
395}
396
397// |line| is the failing line. The failure is due to the fact that it failed to
398// get the value of |attribute|.
399static bool ParseFailedGetValue(const std::string& line,
400 const std::string& attribute,
401 SdpParseError* error) {
402 std::ostringstream description;
403 description << "Failed to get the value of attribute: " << attribute;
404 return ParseFailed(line, description.str(), error);
405}
406
407// The line starting at |line_start| of |message| is the failing line. The
408// failure is due to the line type (e.g. the "m" part of the "m-line")
409// not matching what is expected. The expected line type should be
410// provided as |line_type|.
411static bool ParseFailedExpectLine(const std::string& message,
412 size_t line_start,
413 const char line_type,
414 const std::string& line_value,
415 SdpParseError* error) {
416 std::ostringstream description;
417 description << "Expect line: " << line_type << "=" << line_value;
418 return ParseFailed(message, line_start, description.str(), error);
419}
420
421static bool AddLine(const std::string& line, std::string* message) {
422 if (!message)
423 return false;
424
425 message->append(line);
426 message->append(kLineBreak);
427 return true;
428}
429
430static bool GetLine(const std::string& message,
431 size_t* pos,
432 std::string* line) {
433 size_t line_begin = *pos;
434 size_t line_end = message.find(kNewLine, line_begin);
435 if (line_end == std::string::npos) {
436 return false;
437 }
438 // Update the new start position
439 *pos = line_end + 1;
440 if (line_end > 0 && (message.at(line_end - 1) == kReturn)) {
441 --line_end;
442 }
443 *line = message.substr(line_begin, (line_end - line_begin));
444 const char* cline = line->c_str();
445 // RFC 4566
446 // An SDP session description consists of a number of lines of text of
447 // the form:
448 // <type>=<value>
449 // where <type> MUST be exactly one case-significant character and
450 // <value> is structured text whose format depends on <type>.
451 // Whitespace MUST NOT be used on either side of the "=" sign.
decurtis@webrtc.org8af11042015-01-07 19:15:51 +0000452 if (line->length() < 3 ||
453 !islower(cline[0]) ||
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000454 cline[1] != kSdpDelimiterEqual ||
455 cline[2] == kSdpDelimiterSpace) {
456 *pos = line_begin;
457 return false;
458 }
459 return true;
460}
461
462// Init |os| to "|type|=|value|".
463static void InitLine(const char type,
464 const std::string& value,
465 std::ostringstream* os) {
466 os->str("");
467 *os << type << kSdpDelimiterEqual << value;
468}
469
470// Init |os| to "a=|attribute|".
471static void InitAttrLine(const std::string& attribute, std::ostringstream* os) {
472 InitLine(kLineTypeAttributes, attribute, os);
473}
474
475// Writes a SDP attribute line based on |attribute| and |value| to |message|.
476static void AddAttributeLine(const std::string& attribute, int value,
477 std::string* message) {
478 std::ostringstream os;
479 InitAttrLine(attribute, &os);
480 os << kSdpDelimiterColon << value;
481 AddLine(os.str(), message);
482}
483
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000484static bool IsLineType(const std::string& message,
485 const char type,
486 size_t line_start) {
487 if (message.size() < line_start + kLinePrefixLength) {
488 return false;
489 }
490 const char* cmessage = message.c_str();
491 return (cmessage[line_start] == type &&
492 cmessage[line_start + 1] == kSdpDelimiterEqual);
493}
494
495static bool IsLineType(const std::string& line,
496 const char type) {
497 return IsLineType(line, type, 0);
498}
499
500static bool GetLineWithType(const std::string& message, size_t* pos,
501 std::string* line, const char type) {
502 if (!IsLineType(message, type, *pos)) {
503 return false;
504 }
505
506 if (!GetLine(message, pos, line))
507 return false;
508
509 return true;
510}
511
512static bool HasAttribute(const std::string& line,
513 const std::string& attribute) {
514 return (line.compare(kLinePrefixLength, attribute.size(), attribute) == 0);
515}
516
Peter Boström0c4e06b2015-10-07 12:23:21 +0200517static bool AddSsrcLine(uint32_t ssrc_id,
518 const std::string& attribute,
519 const std::string& value,
520 std::string* message) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000521 // RFC 5576
522 // a=ssrc:<ssrc-id> <attribute>:<value>
523 std::ostringstream os;
524 InitAttrLine(kAttributeSsrc, &os);
525 os << kSdpDelimiterColon << ssrc_id << kSdpDelimiterSpace
526 << attribute << kSdpDelimiterColon << value;
527 return AddLine(os.str(), message);
528}
529
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000530// Get value only from <attribute>:<value>.
531static bool GetValue(const std::string& message, const std::string& attribute,
532 std::string* value, SdpParseError* error) {
533 std::string leftpart;
Donald Curtis0e07f922015-05-15 09:21:23 -0700534 if (!rtc::tokenize_first(message, kSdpDelimiterColon, &leftpart, value)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000535 return ParseFailedGetValue(message, attribute, error);
536 }
537 // The left part should end with the expected attribute.
538 if (leftpart.length() < attribute.length() ||
539 leftpart.compare(leftpart.length() - attribute.length(),
540 attribute.length(), attribute) != 0) {
541 return ParseFailedGetValue(message, attribute, error);
542 }
543 return true;
544}
545
546static bool CaseInsensitiveFind(std::string str1, std::string str2) {
547 std::transform(str1.begin(), str1.end(), str1.begin(),
548 ::tolower);
549 std::transform(str2.begin(), str2.end(), str2.begin(),
550 ::tolower);
551 return str1.find(str2) != std::string::npos;
552}
553
wu@webrtc.org5e760e72014-04-02 23:19:09 +0000554template <class T>
555static bool GetValueFromString(const std::string& line,
556 const std::string& s,
557 T* t,
558 SdpParseError* error) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000559 if (!rtc::FromString(s, t)) {
wu@webrtc.org5e760e72014-04-02 23:19:09 +0000560 std::ostringstream description;
561 description << "Invalid value: " << s << ".";
562 return ParseFailed(line, description.str(), error);
563 }
564 return true;
565}
566
pkasting@chromium.orge9facf82015-02-17 20:36:28 +0000567static bool GetPayloadTypeFromString(const std::string& line,
568 const std::string& s,
569 int* payload_type,
570 SdpParseError* error) {
571 return GetValueFromString(line, s, payload_type, error) &&
572 cricket::IsValidRtpPayloadType(*payload_type);
573}
574
Taylor Brandstetter5de6b752016-03-09 17:02:30 -0800575// |msid_stream_id| and |msid_track_id| represent the stream/track ID from the
576// "a=msid" attribute, if it exists. They are empty if the attribute does not
577// exist.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000578void CreateTracksFromSsrcInfos(const SsrcInfoVec& ssrc_infos,
Taylor Brandstetter5de6b752016-03-09 17:02:30 -0800579 const std::string& msid_stream_id,
580 const std::string& msid_track_id,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000581 StreamParamsVec* tracks) {
nisseede5da42017-01-12 05:15:36 -0800582 RTC_DCHECK(tracks != NULL);
583 RTC_DCHECK(msid_stream_id.empty() == msid_track_id.empty());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000584 for (SsrcInfoVec::const_iterator ssrc_info = ssrc_infos.begin();
585 ssrc_info != ssrc_infos.end(); ++ssrc_info) {
586 if (ssrc_info->cname.empty()) {
587 continue;
588 }
589
Taylor Brandstetter5de6b752016-03-09 17:02:30 -0800590 std::string stream_id;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000591 std::string track_id;
Taylor Brandstetter5de6b752016-03-09 17:02:30 -0800592 if (ssrc_info->stream_id.empty() && !ssrc_info->mslabel.empty()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000593 // If there's no msid and there's mslabel, we consider this is a sdp from
594 // a older version of client that doesn't support msid.
595 // In that case, we use the mslabel and label to construct the track.
Taylor Brandstetter5de6b752016-03-09 17:02:30 -0800596 stream_id = ssrc_info->mslabel;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000597 track_id = ssrc_info->label;
Taylor Brandstetter5de6b752016-03-09 17:02:30 -0800598 } else if (ssrc_info->stream_id.empty() && !msid_stream_id.empty()) {
599 // If there's no msid in the SSRC attributes, but there's a global one
600 // (from a=msid), use that. This is the case with unified plan SDP.
601 stream_id = msid_stream_id;
602 track_id = msid_track_id;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000603 } else {
Taylor Brandstetter5de6b752016-03-09 17:02:30 -0800604 stream_id = ssrc_info->stream_id;
deadbeef9d3584c2016-02-16 17:54:10 -0800605 track_id = ssrc_info->track_id;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000606 }
Taylor Brandstetter5de6b752016-03-09 17:02:30 -0800607 // If a stream/track ID wasn't populated from the SSRC attributes OR the
608 // msid attribute, use default/random values.
609 if (stream_id.empty()) {
610 stream_id = kDefaultMsid;
611 }
612 if (track_id.empty()) {
613 // TODO(ronghuawu): What should we do if the track id doesn't appear?
614 // Create random string (which will be used as track label later)?
615 track_id = rtc::CreateRandomString(8);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000616 }
617
618 StreamParamsVec::iterator track = tracks->begin();
619 for (; track != tracks->end(); ++track) {
620 if (track->id == track_id) {
621 break;
622 }
623 }
624 if (track == tracks->end()) {
625 // If we don't find an existing track, create a new one.
626 tracks->push_back(StreamParams());
627 track = tracks->end() - 1;
628 }
629 track->add_ssrc(ssrc_info->ssrc_id);
630 track->cname = ssrc_info->cname;
Taylor Brandstetter5de6b752016-03-09 17:02:30 -0800631 track->sync_label = stream_id;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000632 track->id = track_id;
633 }
634}
635
636void GetMediaStreamLabels(const ContentInfo* content,
637 std::set<std::string>* labels) {
638 const MediaContentDescription* media_desc =
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000639 static_cast<const MediaContentDescription*>(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000640 content->description);
641 const cricket::StreamParamsVec& streams = media_desc->streams();
642 for (cricket::StreamParamsVec::const_iterator it = streams.begin();
643 it != streams.end(); ++it) {
644 labels->insert(it->sync_label);
645 }
646}
647
648// RFC 5245
649// It is RECOMMENDED that default candidates be chosen based on the
650// likelihood of those candidates to work with the peer that is being
651// contacted. It is RECOMMENDED that relayed > reflexive > host.
652static const int kPreferenceUnknown = 0;
653static const int kPreferenceHost = 1;
654static const int kPreferenceReflexive = 2;
655static const int kPreferenceRelayed = 3;
656
657static int GetCandidatePreferenceFromType(const std::string& type) {
658 int preference = kPreferenceUnknown;
659 if (type == cricket::LOCAL_PORT_TYPE) {
660 preference = kPreferenceHost;
661 } else if (type == cricket::STUN_PORT_TYPE) {
662 preference = kPreferenceReflexive;
663 } else if (type == cricket::RELAY_PORT_TYPE) {
664 preference = kPreferenceRelayed;
665 } else {
nissec80e7412017-01-11 05:56:46 -0800666 RTC_NOTREACHED();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000667 }
668 return preference;
669}
670
guoweis@webrtc.org57ac2c82015-02-06 00:45:13 +0000671// Get ip and port of the default destination from the |candidates| with the
672// given value of |component_id|. The default candidate should be the one most
673// likely to work, typically IPv4 relay.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000674// RFC 5245
675// The value of |component_id| currently supported are 1 (RTP) and 2 (RTCP).
676// TODO: Decide the default destination in webrtcsession and
677// pass it down via SessionDescription.
pthatcher@webrtc.orgc9d6d142014-10-23 23:37:22 +0000678static void GetDefaultDestination(
679 const std::vector<Candidate>& candidates,
680 int component_id, std::string* port,
681 std::string* ip, std::string* addr_type) {
perkj@webrtc.orgd105cc82014-11-07 11:22:06 +0000682 *addr_type = kConnectionIpv4Addrtype;
pthatcher@webrtc.orgc9d6d142014-10-23 23:37:22 +0000683 *port = kDummyPort;
684 *ip = kDummyAddress;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000685 int current_preference = kPreferenceUnknown;
guoweis@webrtc.org57ac2c82015-02-06 00:45:13 +0000686 int current_family = AF_UNSPEC;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000687 for (std::vector<Candidate>::const_iterator it = candidates.begin();
688 it != candidates.end(); ++it) {
689 if (it->component() != component_id) {
690 continue;
691 }
guoweis@webrtc.org57ac2c82015-02-06 00:45:13 +0000692 // Default destination should be UDP only.
693 if (it->protocol() != cricket::UDP_PROTOCOL_NAME) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000694 continue;
695 }
guoweis@webrtc.org57ac2c82015-02-06 00:45:13 +0000696 const int preference = GetCandidatePreferenceFromType(it->type());
697 const int family = it->address().ipaddr().family();
698 // See if this candidate is more preferable then the current one if it's the
699 // same family. Or if the current family is IPv4 already so we could safely
700 // ignore all IPv6 ones. WebRTC bug 4269.
701 // http://code.google.com/p/webrtc/issues/detail?id=4269
702 if ((preference <= current_preference && current_family == family) ||
703 (current_family == AF_INET && family == AF_INET6)) {
704 continue;
705 }
pthatcher@webrtc.orgc9d6d142014-10-23 23:37:22 +0000706 if (family == AF_INET) {
707 addr_type->assign(kConnectionIpv4Addrtype);
708 } else if (family == AF_INET6) {
709 addr_type->assign(kConnectionIpv6Addrtype);
710 }
guoweis@webrtc.org57ac2c82015-02-06 00:45:13 +0000711 current_preference = preference;
712 current_family = family;
713 *port = it->address().PortAsString();
714 *ip = it->address().ipaddr().ToString();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000715 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000716}
717
wu@webrtc.org4c3e9912014-07-16 21:03:13 +0000718// Update |mline|'s default destination and append a c line after it.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000719static void UpdateMediaDefaultDestination(
wu@webrtc.org4c3e9912014-07-16 21:03:13 +0000720 const std::vector<Candidate>& candidates,
jbauch083b73f2015-07-16 02:46:32 -0700721 const std::string& mline,
wu@webrtc.org4c3e9912014-07-16 21:03:13 +0000722 std::string* message) {
723 std::string new_lines;
724 AddLine(mline, &new_lines);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000725 // RFC 4566
726 // m=<media> <port> <proto> <fmt> ...
727 std::vector<std::string> fields;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000728 rtc::split(mline, kSdpDelimiterSpace, &fields);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000729 if (fields.size() < 3) {
730 return;
731 }
732
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000733 std::ostringstream os;
pthatcher@webrtc.orgc9d6d142014-10-23 23:37:22 +0000734 std::string rtp_port, rtp_ip, addr_type;
735 GetDefaultDestination(candidates, ICE_CANDIDATE_COMPONENT_RTP,
736 &rtp_port, &rtp_ip, &addr_type);
737 // Found default RTP candidate.
738 // RFC 5245
739 // The default candidates are added to the SDP as the default
740 // destination for media. For streams based on RTP, this is done by
741 // placing the IP address and port of the RTP candidate into the c and m
742 // lines, respectively.
743 // Update the port in the m line.
744 // If this is a m-line with port equal to 0, we don't change it.
745 if (fields[1] != kMediaPortRejected) {
746 new_lines.replace(fields[0].size() + 1,
747 fields[1].size(),
748 rtp_port);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000749 }
pthatcher@webrtc.orgc9d6d142014-10-23 23:37:22 +0000750 // Add the c line.
751 // RFC 4566
752 // c=<nettype> <addrtype> <connection-address>
753 InitLine(kLineTypeConnection, kConnectionNettype, &os);
754 os << " " << addr_type << " " << rtp_ip;
755 AddLine(os.str(), &new_lines);
wu@webrtc.org4c3e9912014-07-16 21:03:13 +0000756 message->append(new_lines);
757}
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000758
wu@webrtc.org4c3e9912014-07-16 21:03:13 +0000759// Gets "a=rtcp" line if found default RTCP candidate from |candidates|.
760static std::string GetRtcpLine(const std::vector<Candidate>& candidates) {
pthatcher@webrtc.orgc9d6d142014-10-23 23:37:22 +0000761 std::string rtcp_line, rtcp_port, rtcp_ip, addr_type;
762 GetDefaultDestination(candidates, ICE_CANDIDATE_COMPONENT_RTCP,
763 &rtcp_port, &rtcp_ip, &addr_type);
764 // Found default RTCP candidate.
765 // RFC 5245
766 // If the agent is utilizing RTCP, it MUST encode the RTCP candidate
767 // using the a=rtcp attribute as defined in RFC 3605.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000768
pthatcher@webrtc.orgc9d6d142014-10-23 23:37:22 +0000769 // RFC 3605
770 // rtcp-attribute = "a=rtcp:" port [nettype space addrtype space
771 // connection-address] CRLF
772 std::ostringstream os;
773 InitAttrLine(kAttributeRtcp, &os);
774 os << kSdpDelimiterColon
775 << rtcp_port << " "
776 << kConnectionNettype << " "
777 << addr_type << " "
778 << rtcp_ip;
779 rtcp_line = os.str();
wu@webrtc.org4c3e9912014-07-16 21:03:13 +0000780 return rtcp_line;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000781}
782
783// Get candidates according to the mline index from SessionDescriptionInterface.
784static void GetCandidatesByMindex(const SessionDescriptionInterface& desci,
785 int mline_index,
786 std::vector<Candidate>* candidates) {
787 if (!candidates) {
788 return;
789 }
790 const IceCandidateCollection* cc = desci.candidates(mline_index);
791 for (size_t i = 0; i < cc->count(); ++i) {
792 const IceCandidateInterface* candidate = cc->at(i);
793 candidates->push_back(candidate->candidate());
794 }
795}
796
deadbeef90f1e1e2017-02-10 12:35:05 -0800797static bool IsValidPort(int port) {
798 return port >= 0 && port <= 65535;
799}
800
deadbeef9d3584c2016-02-16 17:54:10 -0800801std::string SdpSerialize(const JsepSessionDescription& jdesc,
802 bool unified_plan_sdp) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000803 const cricket::SessionDescription* desc = jdesc.description();
804 if (!desc) {
805 return "";
806 }
807
808 std::string message;
809
810 // Session Description.
811 AddLine(kSessionVersion, &message);
812 // Session Origin
813 // RFC 4566
814 // o=<username> <sess-id> <sess-version> <nettype> <addrtype>
815 // <unicast-address>
816 std::ostringstream os;
817 InitLine(kLineTypeOrigin, kSessionOriginUsername, &os);
jbauch083b73f2015-07-16 02:46:32 -0700818 const std::string& session_id = jdesc.session_id().empty() ?
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000819 kSessionOriginSessionId : jdesc.session_id();
jbauch083b73f2015-07-16 02:46:32 -0700820 const std::string& session_version = jdesc.session_version().empty() ?
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000821 kSessionOriginSessionVersion : jdesc.session_version();
822 os << " " << session_id << " " << session_version << " "
823 << kSessionOriginNettype << " " << kSessionOriginAddrtype << " "
824 << kSessionOriginAddress;
825 AddLine(os.str(), &message);
826 AddLine(kSessionName, &message);
827
828 // Time Description.
829 AddLine(kTimeDescription, &message);
830
831 // Group
832 if (desc->HasGroup(cricket::GROUP_TYPE_BUNDLE)) {
833 std::string group_line = kAttrGroup;
834 const cricket::ContentGroup* group =
835 desc->GetGroupByName(cricket::GROUP_TYPE_BUNDLE);
nisseede5da42017-01-12 05:15:36 -0800836 RTC_DCHECK(group != NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000837 const cricket::ContentNames& content_names = group->content_names();
838 for (cricket::ContentNames::const_iterator it = content_names.begin();
839 it != content_names.end(); ++it) {
840 group_line.append(" ");
841 group_line.append(*it);
842 }
843 AddLine(group_line, &message);
844 }
845
846 // MediaStream semantics
847 InitAttrLine(kAttributeMsidSemantics, &os);
848 os << kSdpDelimiterColon << " " << kMediaStreamSemantic;
wu@webrtc.org4c3e9912014-07-16 21:03:13 +0000849
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000850 std::set<std::string> media_stream_labels;
851 const ContentInfo* audio_content = GetFirstAudioContent(desc);
852 if (audio_content)
853 GetMediaStreamLabels(audio_content, &media_stream_labels);
wu@webrtc.org4c3e9912014-07-16 21:03:13 +0000854
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000855 const ContentInfo* video_content = GetFirstVideoContent(desc);
856 if (video_content)
857 GetMediaStreamLabels(video_content, &media_stream_labels);
wu@webrtc.org4c3e9912014-07-16 21:03:13 +0000858
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000859 for (std::set<std::string>::const_iterator it =
860 media_stream_labels.begin(); it != media_stream_labels.end(); ++it) {
861 os << " " << *it;
862 }
863 AddLine(os.str(), &message);
864
wu@webrtc.org4c3e9912014-07-16 21:03:13 +0000865 // Preserve the order of the media contents.
866 int mline_index = -1;
867 for (cricket::ContentInfos::const_iterator it = desc->contents().begin();
868 it != desc->contents().end(); ++it) {
869 const MediaContentDescription* mdesc =
870 static_cast<const MediaContentDescription*>(it->description);
871 std::vector<Candidate> candidates;
872 GetCandidatesByMindex(jdesc, ++mline_index, &candidates);
deadbeef9d3584c2016-02-16 17:54:10 -0800873 BuildMediaDescription(&*it, desc->GetTransportInfoByName(it->name),
874 mdesc->type(), candidates, unified_plan_sdp,
wu@webrtc.org4c3e9912014-07-16 21:03:13 +0000875 &message);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000876 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000877 return message;
878}
879
880// Serializes the passed in IceCandidateInterface to a SDP string.
881// candidate - The candidate to be serialized.
Honghai Zhang7fb69db2016-03-14 11:59:18 -0700882std::string SdpSerializeCandidate(const IceCandidateInterface& candidate) {
883 return SdpSerializeCandidate(candidate.candidate());
884}
885
886// Serializes a cricket Candidate.
887std::string SdpSerializeCandidate(const cricket::Candidate& candidate) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000888 std::string message;
Honghai Zhang7fb69db2016-03-14 11:59:18 -0700889 std::vector<cricket::Candidate> candidates(1, candidate);
honghaiza54a0802015-12-16 18:37:23 -0800890 BuildCandidate(candidates, true, &message);
wu@webrtc.orgec9f5fb2014-06-24 17:05:10 +0000891 // From WebRTC draft section 4.8.1.1 candidate-attribute will be
892 // just candidate:<candidate> not a=candidate:<blah>CRLF
nisseede5da42017-01-12 05:15:36 -0800893 RTC_DCHECK(message.find("a=") == 0);
wu@webrtc.orgec9f5fb2014-06-24 17:05:10 +0000894 message.erase(0, 2);
nisseede5da42017-01-12 05:15:36 -0800895 RTC_DCHECK(message.find(kLineBreak) == message.size() - 2);
wu@webrtc.orgec9f5fb2014-06-24 17:05:10 +0000896 message.resize(message.size() - 2);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000897 return message;
898}
899
900bool SdpDeserialize(const std::string& message,
901 JsepSessionDescription* jdesc,
902 SdpParseError* error) {
903 std::string session_id;
904 std::string session_version;
Peter Thatcher7cbd1882015-09-17 18:54:52 -0700905 TransportDescription session_td("", "");
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000906 RtpHeaderExtensions session_extmaps;
907 cricket::SessionDescription* desc = new cricket::SessionDescription();
908 std::vector<JsepIceCandidate*> candidates;
909 size_t current_pos = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000910
911 // Session Description
912 if (!ParseSessionDescription(message, &current_pos, &session_id,
deadbeefc80741f2015-10-22 13:14:45 -0700913 &session_version, &session_td, &session_extmaps,
914 desc, error)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000915 delete desc;
916 return false;
917 }
918
919 // Media Description
deadbeefc80741f2015-10-22 13:14:45 -0700920 if (!ParseMediaDescription(message, session_td, session_extmaps, &current_pos,
921 desc, &candidates, error)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000922 delete desc;
923 for (std::vector<JsepIceCandidate*>::const_iterator
924 it = candidates.begin(); it != candidates.end(); ++it) {
925 delete *it;
926 }
927 return false;
928 }
929
930 jdesc->Initialize(desc, session_id, session_version);
931
932 for (std::vector<JsepIceCandidate*>::const_iterator
933 it = candidates.begin(); it != candidates.end(); ++it) {
934 jdesc->AddCandidate(*it);
935 delete *it;
936 }
937 return true;
938}
939
940bool SdpDeserializeCandidate(const std::string& message,
941 JsepIceCandidate* jcandidate,
942 SdpParseError* error) {
nisseede5da42017-01-12 05:15:36 -0800943 RTC_DCHECK(jcandidate != NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000944 Candidate candidate;
945 if (!ParseCandidate(message, &candidate, error, true)) {
946 return false;
947 }
948 jcandidate->SetCandidate(candidate);
949 return true;
950}
951
Honghai Zhang7fb69db2016-03-14 11:59:18 -0700952bool SdpDeserializeCandidate(const std::string& transport_name,
953 const std::string& message,
954 cricket::Candidate* candidate,
955 SdpParseError* error) {
nisseede5da42017-01-12 05:15:36 -0800956 RTC_DCHECK(candidate != nullptr);
Honghai Zhang7fb69db2016-03-14 11:59:18 -0700957 if (!ParseCandidate(message, candidate, error, true)) {
958 return false;
959 }
960 candidate->set_transport_name(transport_name);
961 return true;
962}
963
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000964bool ParseCandidate(const std::string& message, Candidate* candidate,
965 SdpParseError* error, bool is_raw) {
nisseede5da42017-01-12 05:15:36 -0800966 RTC_DCHECK(candidate != NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000967
968 // Get the first line from |message|.
jiayl@webrtc.org7ec3f9f2014-08-08 23:09:15 +0000969 std::string first_line = message;
970 size_t pos = 0;
971 GetLine(message, &pos, &first_line);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000972
jiayl@webrtc.org7ec3f9f2014-08-08 23:09:15 +0000973 // Makes sure |message| contains only one line.
974 if (message.size() > first_line.size()) {
975 std::string left, right;
Donald Curtis0e07f922015-05-15 09:21:23 -0700976 if (rtc::tokenize_first(message, kNewLine, &left, &right) &&
977 !right.empty()) {
jiayl@webrtc.org7ec3f9f2014-08-08 23:09:15 +0000978 return ParseFailed(message, 0, "Expect one line only", error);
979 }
980 }
981
982 // From WebRTC draft section 4.8.1.1 candidate-attribute should be
983 // candidate:<candidate> when trickled, but we still support
984 // a=candidate:<blah>CRLF for backward compatibility and for parsing a line
985 // from the SDP.
986 if (IsLineType(first_line, kLineTypeAttributes)) {
987 first_line = first_line.substr(kLinePrefixLength);
988 }
989
990 std::string attribute_candidate;
991 std::string candidate_value;
992
993 // |first_line| must be in the form of "candidate:<value>".
Donald Curtis144d0182015-05-15 13:14:24 -0700994 if (!rtc::tokenize_first(first_line, kSdpDelimiterColon, &attribute_candidate,
995 &candidate_value) ||
jiayl@webrtc.org7ec3f9f2014-08-08 23:09:15 +0000996 attribute_candidate != kAttributeCandidate) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000997 if (is_raw) {
998 std::ostringstream description;
jiayl@webrtc.org7ec3f9f2014-08-08 23:09:15 +0000999 description << "Expect line: " << kAttributeCandidate
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001000 << ":" << "<candidate-str>";
1001 return ParseFailed(first_line, 0, description.str(), error);
1002 } else {
1003 return ParseFailedExpectLine(first_line, 0, kLineTypeAttributes,
1004 kAttributeCandidate, error);
1005 }
1006 }
1007
1008 std::vector<std::string> fields;
jiayl@webrtc.org7ec3f9f2014-08-08 23:09:15 +00001009 rtc::split(candidate_value, kSdpDelimiterSpace, &fields);
1010
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001011 // RFC 5245
1012 // a=candidate:<foundation> <component-id> <transport> <priority>
1013 // <connection-address> <port> typ <candidate-types>
1014 // [raddr <connection-address>] [rport <port>]
1015 // *(SP extension-att-name SP extension-att-value)
1016 const size_t expected_min_fields = 8;
1017 if (fields.size() < expected_min_fields ||
1018 (fields[6] != kAttributeCandidateTyp)) {
1019 return ParseFailedExpectMinFieldNum(first_line, expected_min_fields, error);
1020 }
jbauch083b73f2015-07-16 02:46:32 -07001021 const std::string& foundation = fields[0];
jiayl@webrtc.org7ec3f9f2014-08-08 23:09:15 +00001022
wu@webrtc.org5e760e72014-04-02 23:19:09 +00001023 int component_id = 0;
1024 if (!GetValueFromString(first_line, fields[1], &component_id, error)) {
1025 return false;
1026 }
jbauch083b73f2015-07-16 02:46:32 -07001027 const std::string& transport = fields[2];
Peter Boström0c4e06b2015-10-07 12:23:21 +02001028 uint32_t priority = 0;
wu@webrtc.org5e760e72014-04-02 23:19:09 +00001029 if (!GetValueFromString(first_line, fields[3], &priority, error)) {
1030 return false;
1031 }
jbauch083b73f2015-07-16 02:46:32 -07001032 const std::string& connection_address = fields[4];
wu@webrtc.org5e760e72014-04-02 23:19:09 +00001033 int port = 0;
1034 if (!GetValueFromString(first_line, fields[5], &port, error)) {
1035 return false;
1036 }
deadbeef90f1e1e2017-02-10 12:35:05 -08001037 if (!IsValidPort(port)) {
1038 return ParseFailed(first_line, "Invalid port number.", error);
1039 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001040 SocketAddress address(connection_address, port);
1041
1042 cricket::ProtocolType protocol;
hnslb68cc752016-12-13 10:33:41 -08001043 if (!StringToProto(transport.c_str(), &protocol)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001044 return ParseFailed(first_line, "Unsupported transport type.", error);
1045 }
hnslb68cc752016-12-13 10:33:41 -08001046 switch (protocol) {
1047 case cricket::PROTO_UDP:
1048 case cricket::PROTO_TCP:
1049 case cricket::PROTO_SSLTCP:
1050 // Supported protocol.
1051 break;
1052 default:
1053 return ParseFailed(first_line, "Unsupported transport type.", error);
1054 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001055
1056 std::string candidate_type;
jbauch083b73f2015-07-16 02:46:32 -07001057 const std::string& type = fields[7];
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001058 if (type == kCandidateHost) {
1059 candidate_type = cricket::LOCAL_PORT_TYPE;
1060 } else if (type == kCandidateSrflx) {
1061 candidate_type = cricket::STUN_PORT_TYPE;
1062 } else if (type == kCandidateRelay) {
1063 candidate_type = cricket::RELAY_PORT_TYPE;
Honghai Zhang7fb69db2016-03-14 11:59:18 -07001064 } else if (type == kCandidatePrflx) {
1065 candidate_type = cricket::PRFLX_PORT_TYPE;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001066 } else {
1067 return ParseFailed(first_line, "Unsupported candidate type.", error);
1068 }
1069
1070 size_t current_position = expected_min_fields;
1071 SocketAddress related_address;
1072 // The 2 optional fields for related address
1073 // [raddr <connection-address>] [rport <port>]
1074 if (fields.size() >= (current_position + 2) &&
1075 fields[current_position] == kAttributeCandidateRaddr) {
1076 related_address.SetIP(fields[++current_position]);
1077 ++current_position;
1078 }
1079 if (fields.size() >= (current_position + 2) &&
1080 fields[current_position] == kAttributeCandidateRport) {
wu@webrtc.org5e760e72014-04-02 23:19:09 +00001081 int port = 0;
1082 if (!GetValueFromString(
1083 first_line, fields[++current_position], &port, error)) {
1084 return false;
1085 }
deadbeef90f1e1e2017-02-10 12:35:05 -08001086 if (!IsValidPort(port)) {
1087 return ParseFailed(first_line, "Invalid port number.", error);
1088 }
wu@webrtc.org5e760e72014-04-02 23:19:09 +00001089 related_address.SetPort(port);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001090 ++current_position;
1091 }
1092
mallinath@webrtc.org2d60c5e2014-08-08 22:29:20 +00001093 // If this is a TCP candidate, it has additional extension as defined in
1094 // RFC 6544.
1095 std::string tcptype;
1096 if (fields.size() >= (current_position + 2) &&
1097 fields[current_position] == kTcpCandidateType) {
1098 tcptype = fields[++current_position];
1099 ++current_position;
1100
1101 if (tcptype != cricket::TCPTYPE_ACTIVE_STR &&
1102 tcptype != cricket::TCPTYPE_PASSIVE_STR &&
1103 tcptype != cricket::TCPTYPE_SIMOPEN_STR) {
1104 return ParseFailed(first_line, "Invalid TCP candidate type.", error);
1105 }
1106
1107 if (protocol != cricket::PROTO_TCP) {
1108 return ParseFailed(first_line, "Invalid non-TCP candidate", error);
1109 }
1110 }
1111
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001112 // Extension
honghaiza54a0802015-12-16 18:37:23 -08001113 // Though non-standard, we support the ICE ufrag and pwd being signaled on
1114 // the candidate to avoid issues with confusing which generation a candidate
1115 // belongs to when trickling multiple generations at the same time.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001116 std::string username;
1117 std::string password;
Peter Boström0c4e06b2015-10-07 12:23:21 +02001118 uint32_t generation = 0;
honghaiza0c44ea2016-03-23 16:07:48 -07001119 uint16_t network_id = 0;
1120 uint16_t network_cost = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001121 for (size_t i = current_position; i + 1 < fields.size(); ++i) {
1122 // RFC 5245
1123 // *(SP extension-att-name SP extension-att-value)
1124 if (fields[i] == kAttributeCandidateGeneration) {
wu@webrtc.org5e760e72014-04-02 23:19:09 +00001125 if (!GetValueFromString(first_line, fields[++i], &generation, error)) {
1126 return false;
1127 }
honghaiza54a0802015-12-16 18:37:23 -08001128 } else if (fields[i] == kAttributeCandidateUfrag) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001129 username = fields[++i];
honghaiza54a0802015-12-16 18:37:23 -08001130 } else if (fields[i] == kAttributeCandidatePwd) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001131 password = fields[++i];
honghaiza0c44ea2016-03-23 16:07:48 -07001132 } else if (fields[i] == kAttributeCandidateNetworkId) {
1133 if (!GetValueFromString(first_line, fields[++i], &network_id, error)) {
1134 return false;
1135 }
honghaize1a0c942016-02-16 14:54:56 -08001136 } else if (fields[i] == kAttributeCandidateNetworkCost) {
1137 if (!GetValueFromString(first_line, fields[++i], &network_cost, error)) {
1138 return false;
1139 }
Honghai Zhang351d77b2016-05-20 15:08:29 -07001140 network_cost = std::min(network_cost, rtc::kNetworkCostMax);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001141 } else {
1142 // Skip the unknown extension.
1143 ++i;
1144 }
1145 }
1146
guoweis@webrtc.org61c12472015-01-15 06:53:07 +00001147 *candidate = Candidate(component_id, cricket::ProtoToString(protocol),
guoweis@webrtc.org950c5182014-12-16 23:01:31 +00001148 address, priority, username, password, candidate_type,
honghaiza0c44ea2016-03-23 16:07:48 -07001149 generation, foundation, network_id, network_cost);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001150 candidate->set_related_address(related_address);
mallinath@webrtc.org2d60c5e2014-08-08 22:29:20 +00001151 candidate->set_tcptype(tcptype);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001152 return true;
1153}
1154
1155bool ParseIceOptions(const std::string& line,
1156 std::vector<std::string>* transport_options,
1157 SdpParseError* error) {
1158 std::string ice_options;
1159 if (!GetValue(line, kAttributeIceOption, &ice_options, error)) {
1160 return false;
1161 }
1162 std::vector<std::string> fields;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001163 rtc::split(ice_options, kSdpDelimiterSpace, &fields);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001164 for (size_t i = 0; i < fields.size(); ++i) {
1165 transport_options->push_back(fields[i]);
1166 }
1167 return true;
1168}
1169
jiayl@webrtc.orgddb85ab2014-09-05 16:31:56 +00001170bool ParseSctpPort(const std::string& line,
1171 int* sctp_port,
1172 SdpParseError* error) {
1173 // draft-ietf-mmusic-sctp-sdp-07
1174 // a=sctp-port
1175 std::vector<std::string> fields;
jiayl@webrtc.orgddb85ab2014-09-05 16:31:56 +00001176 const size_t expected_min_fields = 2;
lally69f57602015-10-08 10:15:04 -07001177 rtc::split(line.substr(kLinePrefixLength), kSdpDelimiterColon, &fields);
1178 if (fields.size() < expected_min_fields) {
1179 fields.resize(0);
1180 rtc::split(line.substr(kLinePrefixLength), kSdpDelimiterSpace, &fields);
1181 }
jiayl@webrtc.orgddb85ab2014-09-05 16:31:56 +00001182 if (fields.size() < expected_min_fields) {
1183 return ParseFailedExpectMinFieldNum(line, expected_min_fields, error);
1184 }
1185 if (!rtc::FromString(fields[1], sctp_port)) {
lally69f57602015-10-08 10:15:04 -07001186 return ParseFailed(line, "Invalid sctp port value.", error);
jiayl@webrtc.orgddb85ab2014-09-05 16:31:56 +00001187 }
1188 return true;
1189}
1190
isheriff6f8d6862016-05-26 11:24:55 -07001191bool ParseExtmap(const std::string& line,
1192 RtpExtension* extmap,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001193 SdpParseError* error) {
1194 // RFC 5285
1195 // a=extmap:<value>["/"<direction>] <URI> <extensionattributes>
1196 std::vector<std::string> fields;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001197 rtc::split(line.substr(kLinePrefixLength),
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001198 kSdpDelimiterSpace, &fields);
1199 const size_t expected_min_fields = 2;
1200 if (fields.size() < expected_min_fields) {
1201 return ParseFailedExpectMinFieldNum(line, expected_min_fields, error);
1202 }
1203 std::string uri = fields[1];
1204
1205 std::string value_direction;
1206 if (!GetValue(fields[0], kAttributeExtmap, &value_direction, error)) {
1207 return false;
1208 }
1209 std::vector<std::string> sub_fields;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001210 rtc::split(value_direction, kSdpDelimiterSlash, &sub_fields);
wu@webrtc.org5e760e72014-04-02 23:19:09 +00001211 int value = 0;
1212 if (!GetValueFromString(line, sub_fields[0], &value, error)) {
1213 return false;
1214 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001215
isheriff6f8d6862016-05-26 11:24:55 -07001216 *extmap = RtpExtension(uri, value);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001217 return true;
1218}
1219
1220void BuildMediaDescription(const ContentInfo* content_info,
1221 const TransportInfo* transport_info,
1222 const MediaType media_type,
wu@webrtc.org4c3e9912014-07-16 21:03:13 +00001223 const std::vector<Candidate>& candidates,
deadbeef9d3584c2016-02-16 17:54:10 -08001224 bool unified_plan_sdp,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001225 std::string* message) {
nisseede5da42017-01-12 05:15:36 -08001226 RTC_DCHECK(message != NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001227 if (content_info == NULL || message == NULL) {
1228 return;
1229 }
1230 // TODO: Rethink if we should use sprintfn instead of stringstream.
1231 // According to the style guide, streams should only be used for logging.
1232 // http://google-styleguide.googlecode.com/svn/
1233 // trunk/cppguide.xml?showone=Streams#Streams
1234 std::ostringstream os;
1235 const MediaContentDescription* media_desc =
henrike@webrtc.org28654cb2013-07-22 21:07:49 +00001236 static_cast<const MediaContentDescription*>(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001237 content_info->description);
nisseede5da42017-01-12 05:15:36 -08001238 RTC_DCHECK(media_desc != NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001239
jiayl@webrtc.org9c16c392014-05-01 18:30:30 +00001240 int sctp_port = cricket::kSctpDefaultPort;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001241
1242 // RFC 4566
1243 // m=<media> <port> <proto> <fmt>
1244 // fmt is a list of payload type numbers that MAY be used in the session.
1245 const char* type = NULL;
1246 if (media_type == cricket::MEDIA_TYPE_AUDIO)
1247 type = kMediaTypeAudio;
1248 else if (media_type == cricket::MEDIA_TYPE_VIDEO)
1249 type = kMediaTypeVideo;
1250 else if (media_type == cricket::MEDIA_TYPE_DATA)
1251 type = kMediaTypeData;
1252 else
nissec80e7412017-01-11 05:56:46 -08001253 RTC_NOTREACHED();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001254
1255 std::string fmt;
1256 if (media_type == cricket::MEDIA_TYPE_VIDEO) {
1257 const VideoContentDescription* video_desc =
1258 static_cast<const VideoContentDescription*>(media_desc);
1259 for (std::vector<cricket::VideoCodec>::const_iterator it =
1260 video_desc->codecs().begin();
1261 it != video_desc->codecs().end(); ++it) {
1262 fmt.append(" ");
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001263 fmt.append(rtc::ToString<int>(it->id));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001264 }
1265 } else if (media_type == cricket::MEDIA_TYPE_AUDIO) {
1266 const AudioContentDescription* audio_desc =
1267 static_cast<const AudioContentDescription*>(media_desc);
1268 for (std::vector<cricket::AudioCodec>::const_iterator it =
1269 audio_desc->codecs().begin();
1270 it != audio_desc->codecs().end(); ++it) {
1271 fmt.append(" ");
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001272 fmt.append(rtc::ToString<int>(it->id));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001273 }
1274 } else if (media_type == cricket::MEDIA_TYPE_DATA) {
jiayl@webrtc.org9c16c392014-05-01 18:30:30 +00001275 const DataContentDescription* data_desc =
1276 static_cast<const DataContentDescription*>(media_desc);
pthatcher@webrtc.org3341b402015-02-13 21:14:22 +00001277 if (IsDtlsSctp(media_desc->protocol())) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001278 fmt.append(" ");
jiayl@webrtc.org9c16c392014-05-01 18:30:30 +00001279
zstein4b2e0822017-02-17 19:48:38 -08001280 if (data_desc->use_sctpmap()) {
1281 for (std::vector<cricket::DataCodec>::const_iterator it =
1282 data_desc->codecs().begin();
1283 it != data_desc->codecs().end(); ++it) {
1284 if (cricket::CodecNamesEq(it->name,
1285 cricket::kGoogleSctpDataCodecName) &&
1286 it->GetParam(cricket::kCodecParamPort, &sctp_port)) {
1287 break;
1288 }
jiayl@webrtc.org9c16c392014-05-01 18:30:30 +00001289 }
jiayl@webrtc.org9c16c392014-05-01 18:30:30 +00001290
zstein4b2e0822017-02-17 19:48:38 -08001291 fmt.append(rtc::ToString<int>(sctp_port));
1292 } else {
1293 fmt.append(kDefaultSctpmapProtocol);
1294 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001295 } else {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001296 for (std::vector<cricket::DataCodec>::const_iterator it =
1297 data_desc->codecs().begin();
1298 it != data_desc->codecs().end(); ++it) {
1299 fmt.append(" ");
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001300 fmt.append(rtc::ToString<int>(it->id));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001301 }
1302 }
1303 }
1304 // The fmt must never be empty. If no codecs are found, set the fmt attribute
1305 // to 0.
1306 if (fmt.empty()) {
1307 fmt = " 0";
1308 }
1309
deadbeef25ed4352016-12-12 18:37:36 -08001310 // The port number in the m line will be updated later when associated with
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001311 // the candidates.
deadbeef25ed4352016-12-12 18:37:36 -08001312 //
1313 // A port value of 0 indicates that the m= section is rejected.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001314 // RFC 3264
1315 // To reject an offered stream, the port number in the corresponding stream in
1316 // the answer MUST be set to zero.
deadbeef25ed4352016-12-12 18:37:36 -08001317 //
1318 // However, the BUNDLE draft adds a new meaning to port zero, when used along
1319 // with a=bundle-only.
1320 const std::string& port =
1321 (content_info->rejected || content_info->bundle_only) ? kMediaPortRejected
1322 : kDummyPort;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001323
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001324 rtc::SSLFingerprint* fp = (transport_info) ?
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001325 transport_info->description.identity_fingerprint.get() : NULL;
1326
wu@webrtc.org4c3e9912014-07-16 21:03:13 +00001327 // Add the m and c lines.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001328 InitLine(kLineTypeMedia, type, &os);
1329 os << " " << port << " " << media_desc->protocol() << fmt;
wu@webrtc.org4c3e9912014-07-16 21:03:13 +00001330 std::string mline = os.str();
1331 UpdateMediaDefaultDestination(candidates, mline, message);
1332
1333 // RFC 4566
1334 // b=AS:<bandwidth>
Peter Thatcherc0c3a862015-06-24 15:31:25 -07001335 if (media_desc->bandwidth() >= 1000) {
wu@webrtc.org4c3e9912014-07-16 21:03:13 +00001336 InitLine(kLineTypeSessionBandwidth, kApplicationSpecificMaximum, &os);
1337 os << kSdpDelimiterColon << (media_desc->bandwidth() / 1000);
1338 AddLine(os.str(), message);
1339 }
1340
deadbeef25ed4352016-12-12 18:37:36 -08001341 // Add the a=bundle-only line.
1342 if (content_info->bundle_only) {
1343 InitAttrLine(kAttributeBundleOnly, &os);
1344 AddLine(os.str(), message);
1345 }
1346
wu@webrtc.org4c3e9912014-07-16 21:03:13 +00001347 // Add the a=rtcp line.
pthatcher@webrtc.org3341b402015-02-13 21:14:22 +00001348 if (IsRtp(media_desc->protocol())) {
wu@webrtc.org4c3e9912014-07-16 21:03:13 +00001349 std::string rtcp_line = GetRtcpLine(candidates);
1350 if (!rtcp_line.empty()) {
1351 AddLine(rtcp_line, message);
1352 }
1353 }
1354
honghaiza54a0802015-12-16 18:37:23 -08001355 // Build the a=candidate lines. We don't include ufrag and pwd in the
1356 // candidates in the SDP to avoid redundancy.
1357 BuildCandidate(candidates, false, message);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001358
1359 // Use the transport_info to build the media level ice-ufrag and ice-pwd.
1360 if (transport_info) {
1361 // RFC 5245
1362 // ice-pwd-att = "ice-pwd" ":" password
1363 // ice-ufrag-att = "ice-ufrag" ":" ufrag
1364 // ice-ufrag
deadbeef3f7219b2015-12-28 15:17:14 -08001365 if (!transport_info->description.ice_ufrag.empty()) {
1366 InitAttrLine(kAttributeIceUfrag, &os);
1367 os << kSdpDelimiterColon << transport_info->description.ice_ufrag;
1368 AddLine(os.str(), message);
1369 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001370 // ice-pwd
deadbeef3f7219b2015-12-28 15:17:14 -08001371 if (!transport_info->description.ice_pwd.empty()) {
1372 InitAttrLine(kAttributeIcePwd, &os);
1373 os << kSdpDelimiterColon << transport_info->description.ice_pwd;
1374 AddLine(os.str(), message);
1375 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001376
1377 // draft-petithuguenin-mmusic-ice-attributes-level-03
1378 BuildIceOptions(transport_info->description.transport_options, message);
1379
1380 // RFC 4572
1381 // fingerprint-attribute =
1382 // "fingerprint" ":" hash-func SP fingerprint
1383 if (fp) {
1384 // Insert the fingerprint attribute.
1385 InitAttrLine(kAttributeFingerprint, &os);
1386 os << kSdpDelimiterColon
1387 << fp->algorithm << kSdpDelimiterSpace
1388 << fp->GetRfc4572Fingerprint();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001389 AddLine(os.str(), message);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001390
1391 // Inserting setup attribute.
1392 if (transport_info->description.connection_role !=
1393 cricket::CONNECTIONROLE_NONE) {
1394 // Making sure we are not using "passive" mode.
1395 cricket::ConnectionRole role =
1396 transport_info->description.connection_role;
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00001397 std::string dtls_role_str;
nissec16fa5e2017-02-07 07:18:43 -08001398 const bool success =
1399 cricket::ConnectionRoleToString(role, &dtls_role_str);
1400 RTC_DCHECK(success);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001401 InitAttrLine(kAttributeSetup, &os);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001402 os << kSdpDelimiterColon << dtls_role_str;
1403 AddLine(os.str(), message);
1404 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001405 }
1406 }
1407
1408 // RFC 3388
1409 // mid-attribute = "a=mid:" identification-tag
1410 // identification-tag = token
1411 // Use the content name as the mid identification-tag.
1412 InitAttrLine(kAttributeMid, &os);
1413 os << kSdpDelimiterColon << content_info->name;
1414 AddLine(os.str(), message);
1415
pthatcher@webrtc.org3341b402015-02-13 21:14:22 +00001416 if (IsDtlsSctp(media_desc->protocol())) {
zstein4b2e0822017-02-17 19:48:38 -08001417 const DataContentDescription* data_desc =
1418 static_cast<const DataContentDescription*>(media_desc);
1419 bool use_sctpmap = data_desc->use_sctpmap();
1420 BuildSctpContentAttributes(message, sctp_port, use_sctpmap);
pthatcher@webrtc.org3341b402015-02-13 21:14:22 +00001421 } else if (IsRtp(media_desc->protocol())) {
deadbeef9d3584c2016-02-16 17:54:10 -08001422 BuildRtpContentAttributes(media_desc, media_type, unified_plan_sdp,
1423 message);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001424 }
1425}
1426
zstein4b2e0822017-02-17 19:48:38 -08001427void BuildSctpContentAttributes(std::string* message,
1428 int sctp_port,
1429 bool use_sctpmap) {
wu@webrtc.org78187522013-10-07 23:32:02 +00001430 std::ostringstream os;
zstein4b2e0822017-02-17 19:48:38 -08001431 if (use_sctpmap) {
1432 // draft-ietf-mmusic-sctp-sdp-04
1433 // a=sctpmap:sctpmap-number protocol [streams]
1434 InitAttrLine(kAttributeSctpmap, &os);
1435 os << kSdpDelimiterColon << sctp_port << kSdpDelimiterSpace
1436 << kDefaultSctpmapProtocol << kSdpDelimiterSpace
1437 << cricket::kMaxSctpStreams;
1438 } else {
1439 // draft-ietf-mmusic-sctp-sdp-23
1440 // a=sctp-port:<port>
1441 InitAttrLine(kAttributeSctpPort, &os);
1442 os << kSdpDelimiterColon << sctp_port;
1443 // TODO(zstein): emit max-message-size here
1444 }
wu@webrtc.org78187522013-10-07 23:32:02 +00001445 AddLine(os.str(), message);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001446}
1447
deadbeef9d3584c2016-02-16 17:54:10 -08001448// If unified_plan_sdp is true, will use "a=msid".
1449void BuildRtpContentAttributes(const MediaContentDescription* media_desc,
1450 const MediaType media_type,
1451 bool unified_plan_sdp,
1452 std::string* message) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001453 std::ostringstream os;
1454 // RFC 5285
1455 // a=extmap:<value>["/"<direction>] <URI> <extensionattributes>
1456 // The definitions MUST be either all session level or all media level. This
1457 // implementation uses all media level.
1458 for (size_t i = 0; i < media_desc->rtp_header_extensions().size(); ++i) {
1459 InitAttrLine(kAttributeExtmap, &os);
1460 os << kSdpDelimiterColon << media_desc->rtp_header_extensions()[i].id
1461 << kSdpDelimiterSpace << media_desc->rtp_header_extensions()[i].uri;
1462 AddLine(os.str(), message);
1463 }
1464
1465 // RFC 3264
1466 // a=sendrecv || a=sendonly || a=sendrecv || a=inactive
deadbeefc80741f2015-10-22 13:14:45 -07001467 switch (media_desc->direction()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001468 case cricket::MD_INACTIVE:
1469 InitAttrLine(kAttributeInactive, &os);
1470 break;
1471 case cricket::MD_SENDONLY:
1472 InitAttrLine(kAttributeSendOnly, &os);
1473 break;
1474 case cricket::MD_RECVONLY:
1475 InitAttrLine(kAttributeRecvOnly, &os);
1476 break;
1477 case cricket::MD_SENDRECV:
1478 default:
1479 InitAttrLine(kAttributeSendRecv, &os);
1480 break;
1481 }
1482 AddLine(os.str(), message);
1483
deadbeef9d3584c2016-02-16 17:54:10 -08001484 // draft-ietf-mmusic-msid-11
1485 // a=msid:<stream id> <track id>
1486 if (unified_plan_sdp && !media_desc->streams().empty()) {
1487 if (media_desc->streams().size() > 1u) {
1488 LOG(LS_WARNING) << "Trying to serialize unified plan SDP with more than "
1489 << "one track in a media section. Omitting 'a=msid'.";
1490 } else {
1491 auto track = media_desc->streams().begin();
1492 const std::string& stream_id = track->sync_label;
deadbeef9d3584c2016-02-16 17:54:10 -08001493 InitAttrLine(kAttributeMsid, &os);
1494 os << kSdpDelimiterColon << stream_id << kSdpDelimiterSpace << track->id;
1495 AddLine(os.str(), message);
1496 }
1497 }
1498
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001499 // RFC 5761
1500 // a=rtcp-mux
1501 if (media_desc->rtcp_mux()) {
1502 InitAttrLine(kAttributeRtcpMux, &os);
1503 AddLine(os.str(), message);
1504 }
1505
deadbeef13871492015-12-09 12:37:51 -08001506 // RFC 5506
1507 // a=rtcp-rsize
1508 if (media_desc->rtcp_reduced_size()) {
1509 InitAttrLine(kAttributeRtcpReducedSize, &os);
1510 AddLine(os.str(), message);
1511 }
1512
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001513 // RFC 4568
1514 // a=crypto:<tag> <crypto-suite> <key-params> [<session-params>]
1515 for (std::vector<CryptoParams>::const_iterator it =
1516 media_desc->cryptos().begin();
1517 it != media_desc->cryptos().end(); ++it) {
1518 InitAttrLine(kAttributeCrypto, &os);
1519 os << kSdpDelimiterColon << it->tag << " " << it->cipher_suite << " "
1520 << it->key_params;
1521 if (!it->session_params.empty()) {
1522 os << " " << it->session_params;
1523 }
1524 AddLine(os.str(), message);
1525 }
1526
1527 // RFC 4566
1528 // a=rtpmap:<payload type> <encoding name>/<clock rate>
1529 // [/<encodingparameters>]
1530 BuildRtpMap(media_desc, media_type, message);
1531
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001532 for (StreamParamsVec::const_iterator track = media_desc->streams().begin();
1533 track != media_desc->streams().end(); ++track) {
1534 // Require that the track belongs to a media stream,
1535 // ie the sync_label is set. This extra check is necessary since the
1536 // MediaContentDescription always contains a streamparam with an ssrc even
1537 // if no track or media stream have been created.
1538 if (track->sync_label.empty()) continue;
1539
1540 // Build the ssrc-group lines.
1541 for (size_t i = 0; i < track->ssrc_groups.size(); ++i) {
1542 // RFC 5576
1543 // a=ssrc-group:<semantics> <ssrc-id> ...
1544 if (track->ssrc_groups[i].ssrcs.empty()) {
1545 continue;
1546 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001547 InitAttrLine(kAttributeSsrcGroup, &os);
1548 os << kSdpDelimiterColon << track->ssrc_groups[i].semantics;
Peter Boström0c4e06b2015-10-07 12:23:21 +02001549 std::vector<uint32_t>::const_iterator ssrc =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001550 track->ssrc_groups[i].ssrcs.begin();
1551 for (; ssrc != track->ssrc_groups[i].ssrcs.end(); ++ssrc) {
Peter Boström0c4e06b2015-10-07 12:23:21 +02001552 os << kSdpDelimiterSpace << rtc::ToString<uint32_t>(*ssrc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001553 }
1554 AddLine(os.str(), message);
1555 }
1556 // Build the ssrc lines for each ssrc.
1557 for (size_t i = 0; i < track->ssrcs.size(); ++i) {
Peter Boström0c4e06b2015-10-07 12:23:21 +02001558 uint32_t ssrc = track->ssrcs[i];
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001559 // RFC 5576
1560 // a=ssrc:<ssrc-id> cname:<value>
1561 AddSsrcLine(ssrc, kSsrcAttributeCname,
1562 track->cname, message);
1563
1564 // draft-alvestrand-mmusic-msid-00
1565 // a=ssrc:<ssrc-id> msid:identifier [appdata]
deadbeef9d3584c2016-02-16 17:54:10 -08001566 // The appdata consists of the "id" attribute of a MediaStreamTrack,
1567 // which corresponds to the "id" attribute of StreamParams.
1568 const std::string& stream_id = track->sync_label;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001569 InitAttrLine(kAttributeSsrc, &os);
1570 os << kSdpDelimiterColon << ssrc << kSdpDelimiterSpace
deadbeef9d3584c2016-02-16 17:54:10 -08001571 << kSsrcAttributeMsid << kSdpDelimiterColon << stream_id
1572 << kSdpDelimiterSpace << track->id;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001573 AddLine(os.str(), message);
1574
deadbeef9d3584c2016-02-16 17:54:10 -08001575 // TODO(ronghuawu): Remove below code which is for backward
1576 // compatibility.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001577 // draft-alvestrand-rtcweb-mid-01
1578 // a=ssrc:<ssrc-id> mslabel:<value>
1579 // The label isn't yet defined.
1580 // a=ssrc:<ssrc-id> label:<value>
1581 AddSsrcLine(ssrc, kSsrcAttributeMslabel, track->sync_label, message);
1582 AddSsrcLine(ssrc, kSSrcAttributeLabel, track->id, message);
1583 }
1584 }
1585}
1586
1587void WriteFmtpHeader(int payload_type, std::ostringstream* os) {
1588 // fmtp header: a=fmtp:|payload_type| <parameters>
1589 // Add a=fmtp
1590 InitAttrLine(kAttributeFmtp, os);
1591 // Add :|payload_type|
1592 *os << kSdpDelimiterColon << payload_type;
1593}
1594
1595void WriteRtcpFbHeader(int payload_type, std::ostringstream* os) {
1596 // rtcp-fb header: a=rtcp-fb:|payload_type|
1597 // <parameters>/<ccm <ccm_parameters>>
1598 // Add a=rtcp-fb
1599 InitAttrLine(kAttributeRtcpFb, os);
1600 // Add :
1601 *os << kSdpDelimiterColon;
1602 if (payload_type == kWildcardPayloadType) {
1603 *os << "*";
1604 } else {
1605 *os << payload_type;
1606 }
1607}
1608
1609void WriteFmtpParameter(const std::string& parameter_name,
1610 const std::string& parameter_value,
1611 std::ostringstream* os) {
1612 // fmtp parameters: |parameter_name|=|parameter_value|
1613 *os << parameter_name << kSdpDelimiterEqual << parameter_value;
1614}
1615
1616void WriteFmtpParameters(const cricket::CodecParameterMap& parameters,
1617 std::ostringstream* os) {
1618 for (cricket::CodecParameterMap::const_iterator fmtp = parameters.begin();
1619 fmtp != parameters.end(); ++fmtp) {
hta62a216e2016-04-15 11:02:14 -07001620 // Parameters are a semicolon-separated list, no spaces.
1621 // The list is separated from the header by a space.
1622 if (fmtp == parameters.begin()) {
1623 *os << kSdpDelimiterSpace;
1624 } else {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001625 *os << kSdpDelimiterSemicolon;
1626 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001627 WriteFmtpParameter(fmtp->first, fmtp->second, os);
1628 }
1629}
1630
1631bool IsFmtpParam(const std::string& name) {
ossuaa4b0772017-01-30 07:41:18 -08001632 // RFC 4855, section 3 specifies the mapping of media format parameters to SDP
1633 // parameters. Only ptime, maxptime, channels and rate are placed outside of
1634 // the fmtp line. In WebRTC, channels and rate are already handled separately
1635 // and thus not included in the CodecParameterMap.
1636 return name != kCodecParamPTime && name != kCodecParamMaxPTime;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001637}
1638
1639// Retreives fmtp parameters from |params|, which may contain other parameters
1640// as well, and puts them in |fmtp_parameters|.
1641void GetFmtpParams(const cricket::CodecParameterMap& params,
1642 cricket::CodecParameterMap* fmtp_parameters) {
1643 for (cricket::CodecParameterMap::const_iterator iter = params.begin();
1644 iter != params.end(); ++iter) {
1645 if (IsFmtpParam(iter->first)) {
1646 (*fmtp_parameters)[iter->first] = iter->second;
1647 }
1648 }
1649}
1650
1651template <class T>
1652void AddFmtpLine(const T& codec, std::string* message) {
1653 cricket::CodecParameterMap fmtp_parameters;
1654 GetFmtpParams(codec.params, &fmtp_parameters);
1655 if (fmtp_parameters.empty()) {
1656 // No need to add an fmtp if it will have no (optional) parameters.
1657 return;
1658 }
1659 std::ostringstream os;
1660 WriteFmtpHeader(codec.id, &os);
1661 WriteFmtpParameters(fmtp_parameters, &os);
1662 AddLine(os.str(), message);
1663 return;
1664}
1665
1666template <class T>
1667void AddRtcpFbLines(const T& codec, std::string* message) {
1668 for (std::vector<cricket::FeedbackParam>::const_iterator iter =
1669 codec.feedback_params.params().begin();
1670 iter != codec.feedback_params.params().end(); ++iter) {
1671 std::ostringstream os;
1672 WriteRtcpFbHeader(codec.id, &os);
1673 os << " " << iter->id();
1674 if (!iter->param().empty()) {
1675 os << " " << iter->param();
1676 }
1677 AddLine(os.str(), message);
1678 }
1679}
1680
jiayl@webrtc.orgddb85ab2014-09-05 16:31:56 +00001681bool AddSctpDataCodec(DataContentDescription* media_desc,
1682 int sctp_port) {
solenberg9fa49752016-10-08 13:02:44 -07001683 for (const auto& codec : media_desc->codecs()) {
1684 if (cricket::CodecNamesEq(codec.name, cricket::kGoogleSctpDataCodecName)) {
1685 return ParseFailed("",
1686 "Can't have multiple sctp port attributes.",
1687 NULL);
1688 }
jiayl@webrtc.orgddb85ab2014-09-05 16:31:56 +00001689 }
1690 // Add the SCTP Port number as a pseudo-codec "port" parameter
solenberg9fa49752016-10-08 13:02:44 -07001691 cricket::DataCodec codec_port(cricket::kGoogleSctpDataCodecPlType,
deadbeef67cf2c12016-04-13 10:07:16 -07001692 cricket::kGoogleSctpDataCodecName);
jiayl@webrtc.orgddb85ab2014-09-05 16:31:56 +00001693 codec_port.SetParam(cricket::kCodecParamPort, sctp_port);
1694 LOG(INFO) << "AddSctpDataCodec: Got SCTP Port Number "
1695 << sctp_port;
1696 media_desc->AddCodec(codec_port);
1697 return true;
1698}
1699
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001700bool GetMinValue(const std::vector<int>& values, int* value) {
1701 if (values.empty()) {
1702 return false;
1703 }
1704 std::vector<int>::const_iterator found =
1705 std::min_element(values.begin(), values.end());
1706 *value = *found;
1707 return true;
1708}
1709
1710bool GetParameter(const std::string& name,
1711 const cricket::CodecParameterMap& params, int* value) {
1712 std::map<std::string, std::string>::const_iterator found =
1713 params.find(name);
1714 if (found == params.end()) {
1715 return false;
1716 }
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001717 if (!rtc::FromString(found->second, value)) {
wu@webrtc.org5e760e72014-04-02 23:19:09 +00001718 return false;
1719 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001720 return true;
1721}
1722
1723void BuildRtpMap(const MediaContentDescription* media_desc,
1724 const MediaType media_type,
1725 std::string* message) {
nisseede5da42017-01-12 05:15:36 -08001726 RTC_DCHECK(message != NULL);
1727 RTC_DCHECK(media_desc != NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001728 std::ostringstream os;
1729 if (media_type == cricket::MEDIA_TYPE_VIDEO) {
1730 const VideoContentDescription* video_desc =
1731 static_cast<const VideoContentDescription*>(media_desc);
1732 for (std::vector<cricket::VideoCodec>::const_iterator it =
1733 video_desc->codecs().begin();
1734 it != video_desc->codecs().end(); ++it) {
1735 // RFC 4566
1736 // a=rtpmap:<payload type> <encoding name>/<clock rate>
1737 // [/<encodingparameters>]
1738 if (it->id != kWildcardPayloadType) {
1739 InitAttrLine(kAttributeRtpmap, &os);
1740 os << kSdpDelimiterColon << it->id << " " << it->name
1741 << "/" << kDefaultVideoClockrate;
1742 AddLine(os.str(), message);
1743 }
1744 AddRtcpFbLines(*it, message);
1745 AddFmtpLine(*it, message);
1746 }
1747 } else if (media_type == cricket::MEDIA_TYPE_AUDIO) {
1748 const AudioContentDescription* audio_desc =
1749 static_cast<const AudioContentDescription*>(media_desc);
1750 std::vector<int> ptimes;
1751 std::vector<int> maxptimes;
1752 int max_minptime = 0;
1753 for (std::vector<cricket::AudioCodec>::const_iterator it =
1754 audio_desc->codecs().begin();
1755 it != audio_desc->codecs().end(); ++it) {
nisseede5da42017-01-12 05:15:36 -08001756 RTC_DCHECK(!it->name.empty());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001757 // RFC 4566
1758 // a=rtpmap:<payload type> <encoding name>/<clock rate>
1759 // [/<encodingparameters>]
1760 InitAttrLine(kAttributeRtpmap, &os);
1761 os << kSdpDelimiterColon << it->id << " ";
1762 os << it->name << "/" << it->clockrate;
1763 if (it->channels != 1) {
1764 os << "/" << it->channels;
1765 }
1766 AddLine(os.str(), message);
1767 AddRtcpFbLines(*it, message);
1768 AddFmtpLine(*it, message);
1769 int minptime = 0;
1770 if (GetParameter(kCodecParamMinPTime, it->params, &minptime)) {
1771 max_minptime = std::max(minptime, max_minptime);
1772 }
1773 int ptime;
1774 if (GetParameter(kCodecParamPTime, it->params, &ptime)) {
1775 ptimes.push_back(ptime);
1776 }
1777 int maxptime;
1778 if (GetParameter(kCodecParamMaxPTime, it->params, &maxptime)) {
1779 maxptimes.push_back(maxptime);
1780 }
1781 }
1782 // Populate the maxptime attribute with the smallest maxptime of all codecs
1783 // under the same m-line.
1784 int min_maxptime = INT_MAX;
1785 if (GetMinValue(maxptimes, &min_maxptime)) {
1786 AddAttributeLine(kCodecParamMaxPTime, min_maxptime, message);
1787 }
nisseede5da42017-01-12 05:15:36 -08001788 RTC_DCHECK(min_maxptime > max_minptime);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001789 // Populate the ptime attribute with the smallest ptime or the largest
1790 // minptime, whichever is the largest, for all codecs under the same m-line.
1791 int ptime = INT_MAX;
1792 if (GetMinValue(ptimes, &ptime)) {
1793 ptime = std::min(ptime, min_maxptime);
1794 ptime = std::max(ptime, max_minptime);
1795 AddAttributeLine(kCodecParamPTime, ptime, message);
1796 }
1797 } else if (media_type == cricket::MEDIA_TYPE_DATA) {
1798 const DataContentDescription* data_desc =
1799 static_cast<const DataContentDescription*>(media_desc);
1800 for (std::vector<cricket::DataCodec>::const_iterator it =
1801 data_desc->codecs().begin();
1802 it != data_desc->codecs().end(); ++it) {
1803 // RFC 4566
1804 // a=rtpmap:<payload type> <encoding name>/<clock rate>
1805 // [/<encodingparameters>]
1806 InitAttrLine(kAttributeRtpmap, &os);
1807 os << kSdpDelimiterColon << it->id << " "
1808 << it->name << "/" << it->clockrate;
1809 AddLine(os.str(), message);
1810 }
1811 }
1812}
1813
1814void BuildCandidate(const std::vector<Candidate>& candidates,
honghaiza54a0802015-12-16 18:37:23 -08001815 bool include_ufrag,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001816 std::string* message) {
1817 std::ostringstream os;
1818
1819 for (std::vector<Candidate>::const_iterator it = candidates.begin();
1820 it != candidates.end(); ++it) {
1821 // RFC 5245
1822 // a=candidate:<foundation> <component-id> <transport> <priority>
1823 // <connection-address> <port> typ <candidate-types>
1824 // [raddr <connection-address>] [rport <port>]
1825 // *(SP extension-att-name SP extension-att-value)
1826 std::string type;
1827 // Map the cricket candidate type to "host" / "srflx" / "prflx" / "relay"
1828 if (it->type() == cricket::LOCAL_PORT_TYPE) {
1829 type = kCandidateHost;
1830 } else if (it->type() == cricket::STUN_PORT_TYPE) {
1831 type = kCandidateSrflx;
1832 } else if (it->type() == cricket::RELAY_PORT_TYPE) {
1833 type = kCandidateRelay;
Honghai Zhang7fb69db2016-03-14 11:59:18 -07001834 } else if (it->type() == cricket::PRFLX_PORT_TYPE) {
1835 type = kCandidatePrflx;
1836 // Peer reflexive candidate may be signaled for being removed.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001837 } else {
nissec80e7412017-01-11 05:56:46 -08001838 RTC_NOTREACHED();
Peter Thatcher019087f2015-04-28 09:06:26 -07001839 // Never write out candidates if we don't know the type.
1840 continue;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001841 }
1842
1843 InitAttrLine(kAttributeCandidate, &os);
1844 os << kSdpDelimiterColon
mallinath@webrtc.org2d60c5e2014-08-08 22:29:20 +00001845 << it->foundation() << " "
1846 << it->component() << " "
1847 << it->protocol() << " "
1848 << it->priority() << " "
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001849 << it->address().ipaddr().ToString() << " "
1850 << it->address().PortAsString() << " "
mallinath@webrtc.org2d60c5e2014-08-08 22:29:20 +00001851 << kAttributeCandidateTyp << " "
1852 << type << " ";
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001853
1854 // Related address
1855 if (!it->related_address().IsNil()) {
1856 os << kAttributeCandidateRaddr << " "
1857 << it->related_address().ipaddr().ToString() << " "
1858 << kAttributeCandidateRport << " "
1859 << it->related_address().PortAsString() << " ";
1860 }
1861
mallinath@webrtc.org2d60c5e2014-08-08 22:29:20 +00001862 if (it->protocol() == cricket::TCP_PROTOCOL_NAME) {
mallinath@webrtc.orge999bd02014-08-13 06:05:55 +00001863 os << kTcpCandidateType << " " << it->tcptype() << " ";
mallinath@webrtc.org2d60c5e2014-08-08 22:29:20 +00001864 }
1865
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001866 // Extensions
1867 os << kAttributeCandidateGeneration << " " << it->generation();
honghaiza54a0802015-12-16 18:37:23 -08001868 if (include_ufrag && !it->username().empty()) {
1869 os << " " << kAttributeCandidateUfrag << " " << it->username();
1870 }
honghaiza0c44ea2016-03-23 16:07:48 -07001871 if (it->network_id() > 0) {
1872 os << " " << kAttributeCandidateNetworkId << " " << it->network_id();
1873 }
honghaize1a0c942016-02-16 14:54:56 -08001874 if (it->network_cost() > 0) {
1875 os << " " << kAttributeCandidateNetworkCost << " " << it->network_cost();
1876 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001877
1878 AddLine(os.str(), message);
1879 }
1880}
1881
1882void BuildIceOptions(const std::vector<std::string>& transport_options,
1883 std::string* message) {
1884 if (!transport_options.empty()) {
1885 std::ostringstream os;
1886 InitAttrLine(kAttributeIceOption, &os);
1887 os << kSdpDelimiterColon << transport_options[0];
1888 for (size_t i = 1; i < transport_options.size(); ++i) {
1889 os << kSdpDelimiterSpace << transport_options[i];
1890 }
1891 AddLine(os.str(), message);
1892 }
1893}
1894
pthatcher@webrtc.org3341b402015-02-13 21:14:22 +00001895bool IsRtp(const std::string& protocol) {
1896 return protocol.empty() ||
1897 (protocol.find(cricket::kMediaProtocolRtpPrefix) != std::string::npos);
1898}
1899
1900bool IsDtlsSctp(const std::string& protocol) {
1901 // This intentionally excludes "SCTP" and "SCTP/DTLS".
lally@webrtc.orga7470932015-02-24 20:19:21 +00001902 return protocol.find(cricket::kMediaProtocolDtlsSctp) != std::string::npos;
pthatcher@webrtc.org3341b402015-02-13 21:14:22 +00001903}
1904
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001905bool ParseSessionDescription(const std::string& message, size_t* pos,
1906 std::string* session_id,
1907 std::string* session_version,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001908 TransportDescription* session_td,
1909 RtpHeaderExtensions* session_extmaps,
1910 cricket::SessionDescription* desc,
1911 SdpParseError* error) {
1912 std::string line;
1913
deadbeefc80741f2015-10-22 13:14:45 -07001914 desc->set_msid_supported(false);
1915
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001916 // RFC 4566
1917 // v= (protocol version)
1918 if (!GetLineWithType(message, pos, &line, kLineTypeVersion)) {
1919 return ParseFailedExpectLine(message, *pos, kLineTypeVersion,
1920 std::string(), error);
1921 }
1922 // RFC 4566
1923 // o=<username> <sess-id> <sess-version> <nettype> <addrtype>
1924 // <unicast-address>
1925 if (!GetLineWithType(message, pos, &line, kLineTypeOrigin)) {
1926 return ParseFailedExpectLine(message, *pos, kLineTypeOrigin,
1927 std::string(), error);
1928 }
1929 std::vector<std::string> fields;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001930 rtc::split(line.substr(kLinePrefixLength),
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001931 kSdpDelimiterSpace, &fields);
1932 const size_t expected_fields = 6;
1933 if (fields.size() != expected_fields) {
1934 return ParseFailedExpectFieldNum(line, expected_fields, error);
1935 }
1936 *session_id = fields[1];
1937 *session_version = fields[2];
1938
1939 // RFC 4566
1940 // s= (session name)
1941 if (!GetLineWithType(message, pos, &line, kLineTypeSessionName)) {
1942 return ParseFailedExpectLine(message, *pos, kLineTypeSessionName,
1943 std::string(), error);
1944 }
1945
1946 // Optional lines
1947 // Those are the optional lines, so shouldn't return false if not present.
1948 // RFC 4566
1949 // i=* (session information)
1950 GetLineWithType(message, pos, &line, kLineTypeSessionInfo);
1951
1952 // RFC 4566
1953 // u=* (URI of description)
1954 GetLineWithType(message, pos, &line, kLineTypeSessionUri);
1955
1956 // RFC 4566
1957 // e=* (email address)
1958 GetLineWithType(message, pos, &line, kLineTypeSessionEmail);
1959
1960 // RFC 4566
1961 // p=* (phone number)
1962 GetLineWithType(message, pos, &line, kLineTypeSessionPhone);
1963
1964 // RFC 4566
1965 // c=* (connection information -- not required if included in
1966 // all media)
1967 GetLineWithType(message, pos, &line, kLineTypeConnection);
1968
1969 // RFC 4566
1970 // b=* (zero or more bandwidth information lines)
1971 while (GetLineWithType(message, pos, &line, kLineTypeSessionBandwidth)) {
1972 // By pass zero or more b lines.
1973 }
1974
1975 // RFC 4566
1976 // One or more time descriptions ("t=" and "r=" lines; see below)
1977 // t= (time the session is active)
1978 // r=* (zero or more repeat times)
1979 // Ensure there's at least one time description
1980 if (!GetLineWithType(message, pos, &line, kLineTypeTiming)) {
1981 return ParseFailedExpectLine(message, *pos, kLineTypeTiming, std::string(),
1982 error);
1983 }
1984
1985 while (GetLineWithType(message, pos, &line, kLineTypeRepeatTimes)) {
1986 // By pass zero or more r lines.
1987 }
1988
1989 // Go through the rest of the time descriptions
1990 while (GetLineWithType(message, pos, &line, kLineTypeTiming)) {
1991 while (GetLineWithType(message, pos, &line, kLineTypeRepeatTimes)) {
1992 // By pass zero or more r lines.
1993 }
1994 }
1995
1996 // RFC 4566
1997 // z=* (time zone adjustments)
1998 GetLineWithType(message, pos, &line, kLineTypeTimeZone);
1999
2000 // RFC 4566
2001 // k=* (encryption key)
2002 GetLineWithType(message, pos, &line, kLineTypeEncryptionKey);
2003
2004 // RFC 4566
2005 // a=* (zero or more session attribute lines)
2006 while (GetLineWithType(message, pos, &line, kLineTypeAttributes)) {
2007 if (HasAttribute(line, kAttributeGroup)) {
2008 if (!ParseGroupAttribute(line, desc, error)) {
2009 return false;
2010 }
2011 } else if (HasAttribute(line, kAttributeIceUfrag)) {
2012 if (!GetValue(line, kAttributeIceUfrag,
2013 &(session_td->ice_ufrag), error)) {
2014 return false;
2015 }
2016 } else if (HasAttribute(line, kAttributeIcePwd)) {
2017 if (!GetValue(line, kAttributeIcePwd, &(session_td->ice_pwd), error)) {
2018 return false;
2019 }
2020 } else if (HasAttribute(line, kAttributeIceLite)) {
2021 session_td->ice_mode = cricket::ICEMODE_LITE;
2022 } else if (HasAttribute(line, kAttributeIceOption)) {
2023 if (!ParseIceOptions(line, &(session_td->transport_options), error)) {
2024 return false;
2025 }
2026 } else if (HasAttribute(line, kAttributeFingerprint)) {
2027 if (session_td->identity_fingerprint.get()) {
2028 return ParseFailed(
2029 line,
2030 "Can't have multiple fingerprint attributes at the same level.",
2031 error);
2032 }
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002033 rtc::SSLFingerprint* fingerprint = NULL;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002034 if (!ParseFingerprintAttribute(line, &fingerprint, error)) {
2035 return false;
2036 }
2037 session_td->identity_fingerprint.reset(fingerprint);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00002038 } else if (HasAttribute(line, kAttributeSetup)) {
2039 if (!ParseDtlsSetup(line, &(session_td->connection_role), error)) {
2040 return false;
2041 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002042 } else if (HasAttribute(line, kAttributeMsidSemantics)) {
2043 std::string semantics;
2044 if (!GetValue(line, kAttributeMsidSemantics, &semantics, error)) {
2045 return false;
2046 }
deadbeefc80741f2015-10-22 13:14:45 -07002047 desc->set_msid_supported(
2048 CaseInsensitiveFind(semantics, kMediaStreamSemantic));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002049 } else if (HasAttribute(line, kAttributeExtmap)) {
isheriff6f8d6862016-05-26 11:24:55 -07002050 RtpExtension extmap;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002051 if (!ParseExtmap(line, &extmap, error)) {
2052 return false;
2053 }
2054 session_extmaps->push_back(extmap);
2055 }
2056 }
2057
2058 return true;
2059}
2060
2061bool ParseGroupAttribute(const std::string& line,
2062 cricket::SessionDescription* desc,
2063 SdpParseError* error) {
nisseede5da42017-01-12 05:15:36 -08002064 RTC_DCHECK(desc != NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002065
2066 // RFC 5888 and draft-holmberg-mmusic-sdp-bundle-negotiation-00
2067 // a=group:BUNDLE video voice
2068 std::vector<std::string> fields;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002069 rtc::split(line.substr(kLinePrefixLength),
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002070 kSdpDelimiterSpace, &fields);
2071 std::string semantics;
2072 if (!GetValue(fields[0], kAttributeGroup, &semantics, error)) {
2073 return false;
2074 }
2075 cricket::ContentGroup group(semantics);
2076 for (size_t i = 1; i < fields.size(); ++i) {
2077 group.AddContentName(fields[i]);
2078 }
2079 desc->AddGroup(group);
2080 return true;
2081}
2082
2083static bool ParseFingerprintAttribute(const std::string& line,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002084 rtc::SSLFingerprint** fingerprint,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002085 SdpParseError* error) {
2086 if (!IsLineType(line, kLineTypeAttributes) ||
2087 !HasAttribute(line, kAttributeFingerprint)) {
2088 return ParseFailedExpectLine(line, 0, kLineTypeAttributes,
2089 kAttributeFingerprint, error);
2090 }
2091
2092 std::vector<std::string> fields;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002093 rtc::split(line.substr(kLinePrefixLength),
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002094 kSdpDelimiterSpace, &fields);
2095 const size_t expected_fields = 2;
2096 if (fields.size() != expected_fields) {
2097 return ParseFailedExpectFieldNum(line, expected_fields, error);
2098 }
2099
2100 // The first field here is "fingerprint:<hash>.
2101 std::string algorithm;
2102 if (!GetValue(fields[0], kAttributeFingerprint, &algorithm, error)) {
2103 return false;
2104 }
2105
2106 // Downcase the algorithm. Note that we don't need to downcase the
2107 // fingerprint because hex_decode can handle upper-case.
2108 std::transform(algorithm.begin(), algorithm.end(), algorithm.begin(),
2109 ::tolower);
2110
2111 // The second field is the digest value. De-hexify it.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002112 *fingerprint = rtc::SSLFingerprint::CreateFromRfc4572(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002113 algorithm, fields[1]);
2114 if (!*fingerprint) {
2115 return ParseFailed(line,
2116 "Failed to create fingerprint from the digest.",
2117 error);
2118 }
2119
2120 return true;
2121}
2122
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00002123static bool ParseDtlsSetup(const std::string& line,
2124 cricket::ConnectionRole* role,
2125 SdpParseError* error) {
2126 // setup-attr = "a=setup:" role
2127 // role = "active" / "passive" / "actpass" / "holdconn"
2128 std::vector<std::string> fields;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002129 rtc::split(line.substr(kLinePrefixLength), kSdpDelimiterColon, &fields);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00002130 const size_t expected_fields = 2;
2131 if (fields.size() != expected_fields) {
2132 return ParseFailedExpectFieldNum(line, expected_fields, error);
2133 }
2134 std::string role_str = fields[1];
2135 if (!cricket::StringToConnectionRole(role_str, role)) {
2136 return ParseFailed(line, "Invalid attribute value.", error);
2137 }
2138 return true;
2139}
2140
deadbeef9d3584c2016-02-16 17:54:10 -08002141static bool ParseMsidAttribute(const std::string& line,
2142 std::string* stream_id,
2143 std::string* track_id,
2144 SdpParseError* error) {
2145 // draft-ietf-mmusic-msid-11
2146 // a=msid:<stream id> <track id>
2147 // msid-value = msid-id [ SP msid-appdata ]
2148 // msid-id = 1*64token-char ; see RFC 4566
2149 // msid-appdata = 1*64token-char ; see RFC 4566
2150 std::string field1;
2151 if (!rtc::tokenize_first(line.substr(kLinePrefixLength), kSdpDelimiterSpace,
2152 &field1, track_id)) {
2153 const size_t expected_fields = 2;
2154 return ParseFailedExpectFieldNum(line, expected_fields, error);
2155 }
2156
deadbeefa4549d62017-02-10 17:26:22 -08002157 if (track_id->empty()) {
2158 return ParseFailed(line, "Missing track ID in msid attribute.", error);
2159 }
2160
deadbeef9d3584c2016-02-16 17:54:10 -08002161 // msid:<msid-id>
2162 if (!GetValue(field1, kAttributeMsid, stream_id, error)) {
2163 return false;
2164 }
deadbeefa4549d62017-02-10 17:26:22 -08002165 if (stream_id->empty()) {
2166 return ParseFailed(line, "Missing stream ID in msid attribute.", error);
2167 }
deadbeef9d3584c2016-02-16 17:54:10 -08002168 return true;
2169}
2170
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002171// RFC 3551
2172// PT encoding media type clock rate channels
2173// name (Hz)
2174// 0 PCMU A 8,000 1
2175// 1 reserved A
2176// 2 reserved A
2177// 3 GSM A 8,000 1
2178// 4 G723 A 8,000 1
2179// 5 DVI4 A 8,000 1
2180// 6 DVI4 A 16,000 1
2181// 7 LPC A 8,000 1
2182// 8 PCMA A 8,000 1
2183// 9 G722 A 8,000 1
2184// 10 L16 A 44,100 2
2185// 11 L16 A 44,100 1
2186// 12 QCELP A 8,000 1
2187// 13 CN A 8,000 1
2188// 14 MPA A 90,000 (see text)
2189// 15 G728 A 8,000 1
2190// 16 DVI4 A 11,025 1
2191// 17 DVI4 A 22,050 1
2192// 18 G729 A 8,000 1
2193struct StaticPayloadAudioCodec {
2194 const char* name;
2195 int clockrate;
Peter Kasting69558702016-01-12 16:26:35 -08002196 size_t channels;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002197};
2198static const StaticPayloadAudioCodec kStaticPayloadAudioCodecs[] = {
2199 { "PCMU", 8000, 1 },
2200 { "reserved", 0, 0 },
2201 { "reserved", 0, 0 },
2202 { "GSM", 8000, 1 },
2203 { "G723", 8000, 1 },
2204 { "DVI4", 8000, 1 },
2205 { "DVI4", 16000, 1 },
2206 { "LPC", 8000, 1 },
2207 { "PCMA", 8000, 1 },
2208 { "G722", 8000, 1 },
2209 { "L16", 44100, 2 },
2210 { "L16", 44100, 1 },
2211 { "QCELP", 8000, 1 },
2212 { "CN", 8000, 1 },
2213 { "MPA", 90000, 1 },
2214 { "G728", 8000, 1 },
2215 { "DVI4", 11025, 1 },
2216 { "DVI4", 22050, 1 },
2217 { "G729", 8000, 1 },
2218};
2219
2220void MaybeCreateStaticPayloadAudioCodecs(
2221 const std::vector<int>& fmts, AudioContentDescription* media_desc) {
2222 if (!media_desc) {
2223 return;
2224 }
deadbeef67cf2c12016-04-13 10:07:16 -07002225 RTC_DCHECK(media_desc->codecs().empty());
solenberg9fa49752016-10-08 13:02:44 -07002226 for (int payload_type : fmts) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002227 if (!media_desc->HasCodec(payload_type) &&
2228 payload_type >= 0 &&
kjellander3e33bfe2016-06-20 07:04:09 -07002229 static_cast<uint32_t>(payload_type) <
2230 arraysize(kStaticPayloadAudioCodecs)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002231 std::string encoding_name = kStaticPayloadAudioCodecs[payload_type].name;
2232 int clock_rate = kStaticPayloadAudioCodecs[payload_type].clockrate;
Peter Kasting69558702016-01-12 16:26:35 -08002233 size_t channels = kStaticPayloadAudioCodecs[payload_type].channels;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002234 media_desc->AddCodec(cricket::AudioCodec(payload_type, encoding_name,
deadbeef67cf2c12016-04-13 10:07:16 -07002235 clock_rate, 0, channels));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002236 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002237 }
2238}
2239
2240template <class C>
2241static C* ParseContentDescription(const std::string& message,
2242 const MediaType media_type,
2243 int mline_index,
2244 const std::string& protocol,
deadbeef67cf2c12016-04-13 10:07:16 -07002245 const std::vector<int>& payload_types,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002246 size_t* pos,
2247 std::string* content_name,
deadbeef25ed4352016-12-12 18:37:36 -08002248 bool* bundle_only,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002249 TransportDescription* transport,
2250 std::vector<JsepIceCandidate*>* candidates,
2251 webrtc::SdpParseError* error) {
2252 C* media_desc = new C();
2253 switch (media_type) {
2254 case cricket::MEDIA_TYPE_AUDIO:
2255 *content_name = cricket::CN_AUDIO;
2256 break;
2257 case cricket::MEDIA_TYPE_VIDEO:
2258 *content_name = cricket::CN_VIDEO;
2259 break;
2260 case cricket::MEDIA_TYPE_DATA:
2261 *content_name = cricket::CN_DATA;
2262 break;
2263 default:
nissec80e7412017-01-11 05:56:46 -08002264 RTC_NOTREACHED();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002265 break;
2266 }
deadbeef67cf2c12016-04-13 10:07:16 -07002267 if (!ParseContent(message, media_type, mline_index, protocol, payload_types,
deadbeef25ed4352016-12-12 18:37:36 -08002268 pos, content_name, bundle_only, media_desc, transport,
2269 candidates, error)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002270 delete media_desc;
2271 return NULL;
2272 }
2273 // Sort the codecs according to the m-line fmt list.
deadbeef67cf2c12016-04-13 10:07:16 -07002274 std::unordered_map<int, int> payload_type_preferences;
2275 // "size + 1" so that the lowest preference payload type has a preference of
2276 // 1, which is greater than the default (0) for payload types not in the fmt
2277 // list.
2278 int preference = static_cast<int>(payload_types.size() + 1);
2279 for (int pt : payload_types) {
2280 payload_type_preferences[pt] = preference--;
2281 }
2282 std::vector<typename C::CodecType> codecs = media_desc->codecs();
2283 std::sort(codecs.begin(), codecs.end(), [&payload_type_preferences](
2284 const typename C::CodecType& a,
2285 const typename C::CodecType& b) {
2286 return payload_type_preferences[a.id] > payload_type_preferences[b.id];
2287 });
2288 media_desc->set_codecs(codecs);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002289 return media_desc;
2290}
2291
2292bool ParseMediaDescription(const std::string& message,
2293 const TransportDescription& session_td,
2294 const RtpHeaderExtensions& session_extmaps,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002295 size_t* pos,
2296 cricket::SessionDescription* desc,
2297 std::vector<JsepIceCandidate*>* candidates,
2298 SdpParseError* error) {
nisseede5da42017-01-12 05:15:36 -08002299 RTC_DCHECK(desc != NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002300 std::string line;
2301 int mline_index = -1;
2302
2303 // Zero or more media descriptions
2304 // RFC 4566
2305 // m=<media> <port> <proto> <fmt>
2306 while (GetLineWithType(message, pos, &line, kLineTypeMedia)) {
2307 ++mline_index;
2308
2309 std::vector<std::string> fields;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002310 rtc::split(line.substr(kLinePrefixLength),
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002311 kSdpDelimiterSpace, &fields);
zstein4b2e0822017-02-17 19:48:38 -08002312
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002313 const size_t expected_min_fields = 4;
2314 if (fields.size() < expected_min_fields) {
2315 return ParseFailedExpectMinFieldNum(line, expected_min_fields, error);
2316 }
deadbeef25ed4352016-12-12 18:37:36 -08002317 bool port_rejected = false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002318 // RFC 3264
2319 // To reject an offered stream, the port number in the corresponding stream
2320 // in the answer MUST be set to zero.
2321 if (fields[1] == kMediaPortRejected) {
deadbeef25ed4352016-12-12 18:37:36 -08002322 port_rejected = true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002323 }
2324
2325 std::string protocol = fields[2];
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002326
2327 // <fmt>
deadbeef67cf2c12016-04-13 10:07:16 -07002328 std::vector<int> payload_types;
pthatcher@webrtc.org3341b402015-02-13 21:14:22 +00002329 if (IsRtp(protocol)) {
jiayl@webrtc.orgddb85ab2014-09-05 16:31:56 +00002330 for (size_t j = 3 ; j < fields.size(); ++j) {
2331 // TODO(wu): Remove when below bug is fixed.
2332 // https://bugzilla.mozilla.org/show_bug.cgi?id=996329
pbosbb36fdf2015-07-09 07:48:14 -07002333 if (fields[j].empty() && j == fields.size() - 1) {
jiayl@webrtc.orgddb85ab2014-09-05 16:31:56 +00002334 continue;
2335 }
wu@webrtc.org36eda7c2014-04-15 20:37:30 +00002336
jiayl@webrtc.orgddb85ab2014-09-05 16:31:56 +00002337 int pl = 0;
pkasting@chromium.orge9facf82015-02-17 20:36:28 +00002338 if (!GetPayloadTypeFromString(line, fields[j], &pl, error)) {
jiayl@webrtc.orgddb85ab2014-09-05 16:31:56 +00002339 return false;
2340 }
deadbeef67cf2c12016-04-13 10:07:16 -07002341 payload_types.push_back(pl);
wu@webrtc.org5e760e72014-04-02 23:19:09 +00002342 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002343 }
2344
2345 // Make a temporary TransportDescription based on |session_td|.
2346 // Some of this gets overwritten by ParseContent.
deadbeef46eed762016-01-28 13:24:37 -08002347 TransportDescription transport(
2348 session_td.transport_options, session_td.ice_ufrag, session_td.ice_pwd,
2349 session_td.ice_mode, session_td.connection_role,
2350 session_td.identity_fingerprint.get());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002351
kwibergd1fe2812016-04-27 06:47:29 -07002352 std::unique_ptr<MediaContentDescription> content;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002353 std::string content_name;
deadbeef25ed4352016-12-12 18:37:36 -08002354 bool bundle_only = false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002355 if (HasAttribute(line, kMediaTypeVideo)) {
2356 content.reset(ParseContentDescription<VideoContentDescription>(
deadbeef67cf2c12016-04-13 10:07:16 -07002357 message, cricket::MEDIA_TYPE_VIDEO, mline_index, protocol,
deadbeef25ed4352016-12-12 18:37:36 -08002358 payload_types, pos, &content_name, &bundle_only, &transport,
2359 candidates, error));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002360 } else if (HasAttribute(line, kMediaTypeAudio)) {
2361 content.reset(ParseContentDescription<AudioContentDescription>(
deadbeef67cf2c12016-04-13 10:07:16 -07002362 message, cricket::MEDIA_TYPE_AUDIO, mline_index, protocol,
deadbeef25ed4352016-12-12 18:37:36 -08002363 payload_types, pos, &content_name, &bundle_only, &transport,
2364 candidates, error));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002365 } else if (HasAttribute(line, kMediaTypeData)) {
jiayl@webrtc.org0e527722014-09-08 21:43:43 +00002366 DataContentDescription* data_desc =
wu@webrtc.org78187522013-10-07 23:32:02 +00002367 ParseContentDescription<DataContentDescription>(
deadbeef67cf2c12016-04-13 10:07:16 -07002368 message, cricket::MEDIA_TYPE_DATA, mline_index, protocol,
deadbeef25ed4352016-12-12 18:37:36 -08002369 payload_types, pos, &content_name, &bundle_only, &transport,
2370 candidates, error);
jiayl@webrtc.org0e527722014-09-08 21:43:43 +00002371 content.reset(data_desc);
wu@webrtc.org78187522013-10-07 23:32:02 +00002372
zstein4b2e0822017-02-17 19:48:38 -08002373 if (data_desc && IsDtlsSctp(protocol)) {
2374 int p;
2375 if (rtc::FromString(fields[3], &p)) {
2376 if (!AddSctpDataCodec(data_desc, p)) {
2377 return false;
2378 }
2379 } else if (fields[3] == kDefaultSctpmapProtocol) {
2380 data_desc->set_use_sctpmap(false);
2381 }
wu@webrtc.org78187522013-10-07 23:32:02 +00002382 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002383 } else {
2384 LOG(LS_WARNING) << "Unsupported media type: " << line;
2385 continue;
2386 }
2387 if (!content.get()) {
2388 // ParseContentDescription returns NULL if failed.
2389 return false;
2390 }
2391
deadbeef25ed4352016-12-12 18:37:36 -08002392 bool content_rejected = false;
deadbeef12771a12017-01-03 13:53:47 -08002393 // A port of 0 is not interpreted as a rejected m= section when it's
2394 // used along with a=bundle-only.
deadbeef25ed4352016-12-12 18:37:36 -08002395 if (bundle_only) {
deadbeef25ed4352016-12-12 18:37:36 -08002396 if (!port_rejected) {
deadbeef12771a12017-01-03 13:53:47 -08002397 // Usage of bundle-only with a nonzero port is unspecified. So just
2398 // ignore bundle-only if we see this.
2399 bundle_only = false;
2400 LOG(LS_WARNING)
2401 << "a=bundle-only attribute observed with a nonzero "
2402 << "port; this usage is unspecified so the attribute is being "
2403 << "ignored.";
deadbeef25ed4352016-12-12 18:37:36 -08002404 }
2405 } else {
2406 // If not using bundle-only, interpret port 0 in the normal way; the m=
2407 // section is being rejected.
2408 content_rejected = port_rejected;
2409 }
2410
pthatcher@webrtc.org3341b402015-02-13 21:14:22 +00002411 if (IsRtp(protocol)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002412 // Set the extmap.
2413 if (!session_extmaps.empty() &&
2414 !content->rtp_header_extensions().empty()) {
2415 return ParseFailed("",
2416 "The a=extmap MUST be either all session level or "
2417 "all media level.",
2418 error);
2419 }
2420 for (size_t i = 0; i < session_extmaps.size(); ++i) {
2421 content->AddRtpHeaderExtension(session_extmaps[i]);
2422 }
2423 }
2424 content->set_protocol(protocol);
2425 desc->AddContent(content_name,
deadbeef25ed4352016-12-12 18:37:36 -08002426 IsDtlsSctp(protocol) ? cricket::NS_JINGLE_DRAFT_SCTP
2427 : cricket::NS_JINGLE_RTP,
2428 content_rejected, bundle_only, content.release());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002429 // Create TransportInfo with the media level "ice-pwd" and "ice-ufrag".
2430 TransportInfo transport_info(content_name, transport);
2431
2432 if (!desc->AddTransportInfo(transport_info)) {
2433 std::ostringstream description;
2434 description << "Failed to AddTransportInfo with content name: "
2435 << content_name;
2436 return ParseFailed("", description.str(), error);
2437 }
2438 }
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00002439
2440 size_t end_of_message = message.size();
2441 if (mline_index == -1 && *pos != end_of_message) {
2442 ParseFailed(message, *pos, "Expects m line.", error);
2443 return false;
2444 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002445 return true;
2446}
2447
2448bool VerifyCodec(const cricket::Codec& codec) {
2449 // Codec has not been populated correctly unless the name has been set. This
2450 // can happen if an SDP has an fmtp or rtcp-fb with a payload type but doesn't
2451 // have a corresponding "rtpmap" line.
htab39db842016-12-08 01:50:48 -08002452 return !codec.name.empty();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002453}
2454
2455bool VerifyAudioCodecs(const AudioContentDescription* audio_desc) {
2456 const std::vector<cricket::AudioCodec>& codecs = audio_desc->codecs();
2457 for (std::vector<cricket::AudioCodec>::const_iterator iter = codecs.begin();
2458 iter != codecs.end(); ++iter) {
2459 if (!VerifyCodec(*iter)) {
2460 return false;
2461 }
2462 }
2463 return true;
2464}
2465
2466bool VerifyVideoCodecs(const VideoContentDescription* video_desc) {
2467 const std::vector<cricket::VideoCodec>& codecs = video_desc->codecs();
2468 for (std::vector<cricket::VideoCodec>::const_iterator iter = codecs.begin();
2469 iter != codecs.end(); ++iter) {
2470 if (!VerifyCodec(*iter)) {
2471 return false;
2472 }
2473 }
2474 return true;
2475}
2476
2477void AddParameters(const cricket::CodecParameterMap& parameters,
2478 cricket::Codec* codec) {
2479 for (cricket::CodecParameterMap::const_iterator iter =
2480 parameters.begin(); iter != parameters.end(); ++iter) {
2481 codec->SetParam(iter->first, iter->second);
2482 }
2483}
2484
2485void AddFeedbackParameter(const cricket::FeedbackParam& feedback_param,
2486 cricket::Codec* codec) {
2487 codec->AddFeedbackParam(feedback_param);
2488}
2489
2490void AddFeedbackParameters(const cricket::FeedbackParams& feedback_params,
2491 cricket::Codec* codec) {
2492 for (std::vector<cricket::FeedbackParam>::const_iterator iter =
2493 feedback_params.params().begin();
2494 iter != feedback_params.params().end(); ++iter) {
2495 codec->AddFeedbackParam(*iter);
2496 }
2497}
2498
2499// Gets the current codec setting associated with |payload_type|. If there
changbin.shao@webrtc.org2d25b442015-03-16 04:14:34 +00002500// is no Codec associated with that payload type it returns an empty codec
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002501// with that payload type.
2502template <class T>
changbin.shao@webrtc.org2d25b442015-03-16 04:14:34 +00002503T GetCodecWithPayloadType(const std::vector<T>& codecs, int payload_type) {
magjedb05fa242016-11-11 04:00:16 -08002504 const T* codec = FindCodecById(codecs, payload_type);
2505 if (codec)
2506 return *codec;
2507 // Return empty codec with |payload_type|.
changbin.shao@webrtc.org2d25b442015-03-16 04:14:34 +00002508 T ret_val;
magjedb05fa242016-11-11 04:00:16 -08002509 ret_val.id = payload_type;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002510 return ret_val;
2511}
2512
2513// Updates or creates a new codec entry in the audio description.
2514template <class T, class U>
2515void AddOrReplaceCodec(MediaContentDescription* content_desc, const U& codec) {
2516 T* desc = static_cast<T*>(content_desc);
2517 std::vector<U> codecs = desc->codecs();
2518 bool found = false;
2519
2520 typename std::vector<U>::iterator iter;
2521 for (iter = codecs.begin(); iter != codecs.end(); ++iter) {
2522 if (iter->id == codec.id) {
2523 *iter = codec;
2524 found = true;
2525 break;
2526 }
2527 }
2528 if (!found) {
2529 desc->AddCodec(codec);
2530 return;
2531 }
2532 desc->set_codecs(codecs);
2533}
2534
2535// Adds or updates existing codec corresponding to |payload_type| according
2536// to |parameters|.
2537template <class T, class U>
2538void UpdateCodec(MediaContentDescription* content_desc, int payload_type,
2539 const cricket::CodecParameterMap& parameters) {
2540 // Codec might already have been populated (from rtpmap).
changbin.shao@webrtc.org2d25b442015-03-16 04:14:34 +00002541 U new_codec = GetCodecWithPayloadType(static_cast<T*>(content_desc)->codecs(),
2542 payload_type);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002543 AddParameters(parameters, &new_codec);
2544 AddOrReplaceCodec<T, U>(content_desc, new_codec);
2545}
2546
2547// Adds or updates existing codec corresponding to |payload_type| according
2548// to |feedback_param|.
2549template <class T, class U>
2550void UpdateCodec(MediaContentDescription* content_desc, int payload_type,
2551 const cricket::FeedbackParam& feedback_param) {
2552 // Codec might already have been populated (from rtpmap).
changbin.shao@webrtc.org2d25b442015-03-16 04:14:34 +00002553 U new_codec = GetCodecWithPayloadType(static_cast<T*>(content_desc)->codecs(),
2554 payload_type);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002555 AddFeedbackParameter(feedback_param, &new_codec);
2556 AddOrReplaceCodec<T, U>(content_desc, new_codec);
2557}
2558
jlmiller@webrtc.orga744a282015-02-18 21:37:46 +00002559template <class T>
2560bool PopWildcardCodec(std::vector<T>* codecs, T* wildcard_codec) {
2561 for (auto iter = codecs->begin(); iter != codecs->end(); ++iter) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002562 if (iter->id == kWildcardPayloadType) {
2563 *wildcard_codec = *iter;
2564 codecs->erase(iter);
2565 return true;
2566 }
2567 }
2568 return false;
2569}
2570
jlmiller@webrtc.orga744a282015-02-18 21:37:46 +00002571template<class T>
2572void UpdateFromWildcardCodecs(cricket::MediaContentDescriptionImpl<T>* desc) {
2573 auto codecs = desc->codecs();
2574 T wildcard_codec;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002575 if (!PopWildcardCodec(&codecs, &wildcard_codec)) {
2576 return;
2577 }
jlmiller@webrtc.orga744a282015-02-18 21:37:46 +00002578 for (auto& codec : codecs) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002579 AddFeedbackParameters(wildcard_codec.feedback_params, &codec);
2580 }
jlmiller@webrtc.orga744a282015-02-18 21:37:46 +00002581 desc->set_codecs(codecs);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002582}
2583
2584void AddAudioAttribute(const std::string& name, const std::string& value,
2585 AudioContentDescription* audio_desc) {
2586 if (value.empty()) {
2587 return;
2588 }
2589 std::vector<cricket::AudioCodec> codecs = audio_desc->codecs();
2590 for (std::vector<cricket::AudioCodec>::iterator iter = codecs.begin();
2591 iter != codecs.end(); ++iter) {
2592 iter->params[name] = value;
2593 }
2594 audio_desc->set_codecs(codecs);
2595}
2596
2597bool ParseContent(const std::string& message,
2598 const MediaType media_type,
2599 int mline_index,
2600 const std::string& protocol,
deadbeef67cf2c12016-04-13 10:07:16 -07002601 const std::vector<int>& payload_types,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002602 size_t* pos,
2603 std::string* content_name,
deadbeef25ed4352016-12-12 18:37:36 -08002604 bool* bundle_only,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002605 MediaContentDescription* media_desc,
2606 TransportDescription* transport,
2607 std::vector<JsepIceCandidate*>* candidates,
2608 SdpParseError* error) {
nisseede5da42017-01-12 05:15:36 -08002609 RTC_DCHECK(media_desc != NULL);
2610 RTC_DCHECK(content_name != NULL);
2611 RTC_DCHECK(transport != NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002612
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +00002613 if (media_type == cricket::MEDIA_TYPE_AUDIO) {
2614 MaybeCreateStaticPayloadAudioCodecs(
deadbeef67cf2c12016-04-13 10:07:16 -07002615 payload_types, static_cast<AudioContentDescription*>(media_desc));
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +00002616 }
2617
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002618 // The media level "ice-ufrag" and "ice-pwd".
2619 // The candidates before update the media level "ice-pwd" and "ice-ufrag".
2620 Candidates candidates_orig;
2621 std::string line;
2622 std::string mline_id;
2623 // Tracks created out of the ssrc attributes.
2624 StreamParamsVec tracks;
2625 SsrcInfoVec ssrc_infos;
2626 SsrcGroupVec ssrc_groups;
2627 std::string maxptime_as_string;
2628 std::string ptime_as_string;
deadbeef9d3584c2016-02-16 17:54:10 -08002629 std::string stream_id;
2630 std::string track_id;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002631
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002632 // Loop until the next m line
2633 while (!IsLineType(message, kLineTypeMedia, *pos)) {
2634 if (!GetLine(message, pos, &line)) {
2635 if (*pos >= message.size()) {
2636 break; // Done parsing
2637 } else {
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +00002638 return ParseFailed(message, *pos, "Invalid SDP line.", error);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002639 }
2640 }
2641
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002642 // RFC 4566
2643 // b=* (zero or more bandwidth information lines)
2644 if (IsLineType(line, kLineTypeSessionBandwidth)) {
2645 std::string bandwidth;
2646 if (HasAttribute(line, kApplicationSpecificMaximum)) {
2647 if (!GetValue(line, kApplicationSpecificMaximum, &bandwidth, error)) {
2648 return false;
2649 } else {
wu@webrtc.org5e760e72014-04-02 23:19:09 +00002650 int b = 0;
2651 if (!GetValueFromString(line, bandwidth, &b, error)) {
2652 return false;
2653 }
Peter Thatcherc0c3a862015-06-24 15:31:25 -07002654 // We should never use more than the default bandwidth for RTP-based
2655 // data channels. Don't allow SDP to set the bandwidth, because
2656 // that would give JS the opportunity to "break the Internet".
2657 // See: https://code.google.com/p/chromium/issues/detail?id=280726
2658 if (media_type == cricket::MEDIA_TYPE_DATA && IsRtp(protocol) &&
2659 b > cricket::kDataMaxBandwidth / 1000) {
2660 std::ostringstream description;
2661 description << "RTP-based data channels may not send more than "
2662 << cricket::kDataMaxBandwidth / 1000 << "kbps.";
2663 return ParseFailed(line, description.str(), error);
2664 }
deadbeefb2362572016-12-13 16:37:06 -08002665 // Prevent integer overflow.
2666 b = std::min(b, INT_MAX / 1000);
wu@webrtc.org5e760e72014-04-02 23:19:09 +00002667 media_desc->set_bandwidth(b * 1000);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002668 }
2669 }
2670 continue;
2671 }
2672
2673 if (!IsLineType(line, kLineTypeAttributes)) {
2674 // TODO: Handle other lines if needed.
2675 LOG(LS_INFO) << "Ignored line: " << line;
2676 continue;
2677 }
2678
2679 // Handle attributes common to SCTP and RTP.
2680 if (HasAttribute(line, kAttributeMid)) {
2681 // RFC 3388
2682 // mid-attribute = "a=mid:" identification-tag
2683 // identification-tag = token
2684 // Use the mid identification-tag as the content name.
2685 if (!GetValue(line, kAttributeMid, &mline_id, error)) {
2686 return false;
2687 }
2688 *content_name = mline_id;
deadbeef25ed4352016-12-12 18:37:36 -08002689 } else if (HasAttribute(line, kAttributeBundleOnly)) {
2690 *bundle_only = true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002691 } else if (HasAttribute(line, kAttributeCandidate)) {
2692 Candidate candidate;
2693 if (!ParseCandidate(line, &candidate, error, false)) {
2694 return false;
2695 }
deadbeef7bcdb692017-01-20 12:43:58 -08002696 // ParseCandidate will parse non-standard ufrag and password attributes,
2697 // since it's used for candidate trickling, but we only want to process
2698 // the "a=ice-ufrag"/"a=ice-pwd" values in a session description, so
2699 // strip them off at this point.
2700 candidate.set_username(std::string());
2701 candidate.set_password(std::string());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002702 candidates_orig.push_back(candidate);
2703 } else if (HasAttribute(line, kAttributeIceUfrag)) {
2704 if (!GetValue(line, kAttributeIceUfrag, &transport->ice_ufrag, error)) {
2705 return false;
2706 }
2707 } else if (HasAttribute(line, kAttributeIcePwd)) {
2708 if (!GetValue(line, kAttributeIcePwd, &transport->ice_pwd, error)) {
2709 return false;
2710 }
2711 } else if (HasAttribute(line, kAttributeIceOption)) {
2712 if (!ParseIceOptions(line, &transport->transport_options, error)) {
2713 return false;
2714 }
2715 } else if (HasAttribute(line, kAttributeFmtp)) {
2716 if (!ParseFmtpAttributes(line, media_type, media_desc, error)) {
2717 return false;
2718 }
2719 } else if (HasAttribute(line, kAttributeFingerprint)) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002720 rtc::SSLFingerprint* fingerprint = NULL;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002721
2722 if (!ParseFingerprintAttribute(line, &fingerprint, error)) {
2723 return false;
2724 }
2725 transport->identity_fingerprint.reset(fingerprint);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00002726 } else if (HasAttribute(line, kAttributeSetup)) {
2727 if (!ParseDtlsSetup(line, &(transport->connection_role), error)) {
2728 return false;
2729 }
pthatcher@webrtc.org3341b402015-02-13 21:14:22 +00002730 } else if (IsDtlsSctp(protocol) && HasAttribute(line, kAttributeSctpPort)) {
deadbeef7e146cb2016-09-28 10:04:34 -07002731 if (media_type != cricket::MEDIA_TYPE_DATA) {
2732 return ParseFailed(
2733 line, "sctp-port attribute found in non-data media description.",
2734 error);
2735 }
jiayl@webrtc.orgddb85ab2014-09-05 16:31:56 +00002736 int sctp_port;
2737 if (!ParseSctpPort(line, &sctp_port, error)) {
2738 return false;
2739 }
2740 if (!AddSctpDataCodec(static_cast<DataContentDescription*>(media_desc),
2741 sctp_port)) {
2742 return false;
2743 }
pthatcher@webrtc.org3341b402015-02-13 21:14:22 +00002744 } else if (IsRtp(protocol)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002745 //
2746 // RTP specific attrubtes
2747 //
2748 if (HasAttribute(line, kAttributeRtcpMux)) {
2749 media_desc->set_rtcp_mux(true);
deadbeef13871492015-12-09 12:37:51 -08002750 } else if (HasAttribute(line, kAttributeRtcpReducedSize)) {
2751 media_desc->set_rtcp_reduced_size(true);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002752 } else if (HasAttribute(line, kAttributeSsrcGroup)) {
2753 if (!ParseSsrcGroupAttribute(line, &ssrc_groups, error)) {
2754 return false;
2755 }
2756 } else if (HasAttribute(line, kAttributeSsrc)) {
2757 if (!ParseSsrcAttribute(line, &ssrc_infos, error)) {
2758 return false;
2759 }
2760 } else if (HasAttribute(line, kAttributeCrypto)) {
2761 if (!ParseCryptoAttribute(line, media_desc, error)) {
2762 return false;
2763 }
2764 } else if (HasAttribute(line, kAttributeRtpmap)) {
deadbeef67cf2c12016-04-13 10:07:16 -07002765 if (!ParseRtpmapAttribute(line, media_type, payload_types, media_desc,
2766 error)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002767 return false;
2768 }
2769 } else if (HasAttribute(line, kCodecParamMaxPTime)) {
2770 if (!GetValue(line, kCodecParamMaxPTime, &maxptime_as_string, error)) {
2771 return false;
2772 }
2773 } else if (HasAttribute(line, kAttributeRtcpFb)) {
2774 if (!ParseRtcpFbAttribute(line, media_type, media_desc, error)) {
2775 return false;
2776 }
2777 } else if (HasAttribute(line, kCodecParamPTime)) {
2778 if (!GetValue(line, kCodecParamPTime, &ptime_as_string, error)) {
2779 return false;
2780 }
2781 } else if (HasAttribute(line, kAttributeSendOnly)) {
2782 media_desc->set_direction(cricket::MD_SENDONLY);
2783 } else if (HasAttribute(line, kAttributeRecvOnly)) {
2784 media_desc->set_direction(cricket::MD_RECVONLY);
2785 } else if (HasAttribute(line, kAttributeInactive)) {
2786 media_desc->set_direction(cricket::MD_INACTIVE);
2787 } else if (HasAttribute(line, kAttributeSendRecv)) {
2788 media_desc->set_direction(cricket::MD_SENDRECV);
2789 } else if (HasAttribute(line, kAttributeExtmap)) {
isheriff6f8d6862016-05-26 11:24:55 -07002790 RtpExtension extmap;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002791 if (!ParseExtmap(line, &extmap, error)) {
2792 return false;
2793 }
2794 media_desc->AddRtpHeaderExtension(extmap);
2795 } else if (HasAttribute(line, kAttributeXGoogleFlag)) {
2796 // Experimental attribute. Conference mode activates more aggressive
2797 // AEC and NS settings.
2798 // TODO: expose API to set these directly.
2799 std::string flag_value;
2800 if (!GetValue(line, kAttributeXGoogleFlag, &flag_value, error)) {
2801 return false;
2802 }
2803 if (flag_value.compare(kValueConference) == 0)
2804 media_desc->set_conference_mode(true);
deadbeef9d3584c2016-02-16 17:54:10 -08002805 } else if (HasAttribute(line, kAttributeMsid)) {
2806 if (!ParseMsidAttribute(line, &stream_id, &track_id, error)) {
2807 return false;
2808 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002809 }
2810 } else {
2811 // Only parse lines that we are interested of.
2812 LOG(LS_INFO) << "Ignored line: " << line;
2813 continue;
2814 }
2815 }
2816
2817 // Create tracks from the |ssrc_infos|.
Taylor Brandstetter5de6b752016-03-09 17:02:30 -08002818 // If the stream_id/track_id for all SSRCS are identical, one StreamParams
2819 // will be created in CreateTracksFromSsrcInfos, containing all the SSRCs from
2820 // the m= section.
2821 CreateTracksFromSsrcInfos(ssrc_infos, stream_id, track_id, &tracks);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002822
2823 // Add the ssrc group to the track.
2824 for (SsrcGroupVec::iterator ssrc_group = ssrc_groups.begin();
2825 ssrc_group != ssrc_groups.end(); ++ssrc_group) {
2826 if (ssrc_group->ssrcs.empty()) {
2827 continue;
2828 }
Peter Boström0c4e06b2015-10-07 12:23:21 +02002829 uint32_t ssrc = ssrc_group->ssrcs.front();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002830 for (StreamParamsVec::iterator track = tracks.begin();
2831 track != tracks.end(); ++track) {
2832 if (track->has_ssrc(ssrc)) {
2833 track->ssrc_groups.push_back(*ssrc_group);
2834 }
2835 }
2836 }
2837
2838 // Add the new tracks to the |media_desc|.
deadbeef9d3584c2016-02-16 17:54:10 -08002839 for (StreamParams& track : tracks) {
2840 media_desc->AddStream(track);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002841 }
2842
2843 if (media_type == cricket::MEDIA_TYPE_AUDIO) {
2844 AudioContentDescription* audio_desc =
2845 static_cast<AudioContentDescription*>(media_desc);
jlmiller@webrtc.orga744a282015-02-18 21:37:46 +00002846 UpdateFromWildcardCodecs(audio_desc);
2847
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002848 // Verify audio codec ensures that no audio codec has been populated with
2849 // only fmtp.
2850 if (!VerifyAudioCodecs(audio_desc)) {
2851 return ParseFailed("Failed to parse audio codecs correctly.", error);
2852 }
2853 AddAudioAttribute(kCodecParamMaxPTime, maxptime_as_string, audio_desc);
2854 AddAudioAttribute(kCodecParamPTime, ptime_as_string, audio_desc);
2855 }
2856
2857 if (media_type == cricket::MEDIA_TYPE_VIDEO) {
jlmiller@webrtc.orga744a282015-02-18 21:37:46 +00002858 VideoContentDescription* video_desc =
2859 static_cast<VideoContentDescription*>(media_desc);
2860 UpdateFromWildcardCodecs(video_desc);
2861 // Verify video codec ensures that no video codec has been populated with
2862 // only rtcp-fb.
2863 if (!VerifyVideoCodecs(video_desc)) {
2864 return ParseFailed("Failed to parse video codecs correctly.", error);
2865 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002866 }
2867
2868 // RFC 5245
2869 // Update the candidates with the media level "ice-pwd" and "ice-ufrag".
2870 for (Candidates::iterator it = candidates_orig.begin();
2871 it != candidates_orig.end(); ++it) {
nisseede5da42017-01-12 05:15:36 -08002872 RTC_DCHECK((*it).username().empty() ||
2873 (*it).username() == transport->ice_ufrag);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002874 (*it).set_username(transport->ice_ufrag);
nisseede5da42017-01-12 05:15:36 -08002875 RTC_DCHECK((*it).password().empty());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002876 (*it).set_password(transport->ice_pwd);
2877 candidates->push_back(
2878 new JsepIceCandidate(mline_id, mline_index, *it));
2879 }
2880 return true;
2881}
2882
2883bool ParseSsrcAttribute(const std::string& line, SsrcInfoVec* ssrc_infos,
2884 SdpParseError* error) {
nisseede5da42017-01-12 05:15:36 -08002885 RTC_DCHECK(ssrc_infos != NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002886 // RFC 5576
2887 // a=ssrc:<ssrc-id> <attribute>
2888 // a=ssrc:<ssrc-id> <attribute>:<value>
2889 std::string field1, field2;
Donald Curtis144d0182015-05-15 13:14:24 -07002890 if (!rtc::tokenize_first(line.substr(kLinePrefixLength), kSdpDelimiterSpace,
2891 &field1, &field2)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002892 const size_t expected_fields = 2;
2893 return ParseFailedExpectFieldNum(line, expected_fields, error);
2894 }
2895
2896 // ssrc:<ssrc-id>
2897 std::string ssrc_id_s;
2898 if (!GetValue(field1, kAttributeSsrc, &ssrc_id_s, error)) {
2899 return false;
2900 }
Peter Boström0c4e06b2015-10-07 12:23:21 +02002901 uint32_t ssrc_id = 0;
wu@webrtc.org5e760e72014-04-02 23:19:09 +00002902 if (!GetValueFromString(line, ssrc_id_s, &ssrc_id, error)) {
2903 return false;
2904 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002905
2906 std::string attribute;
2907 std::string value;
Donald Curtis144d0182015-05-15 13:14:24 -07002908 if (!rtc::tokenize_first(field2, kSdpDelimiterColon, &attribute, &value)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002909 std::ostringstream description;
2910 description << "Failed to get the ssrc attribute value from " << field2
2911 << ". Expected format <attribute>:<value>.";
2912 return ParseFailed(line, description.str(), error);
2913 }
2914
2915 // Check if there's already an item for this |ssrc_id|. Create a new one if
2916 // there isn't.
2917 SsrcInfoVec::iterator ssrc_info = ssrc_infos->begin();
2918 for (; ssrc_info != ssrc_infos->end(); ++ssrc_info) {
2919 if (ssrc_info->ssrc_id == ssrc_id) {
2920 break;
2921 }
2922 }
2923 if (ssrc_info == ssrc_infos->end()) {
2924 SsrcInfo info;
2925 info.ssrc_id = ssrc_id;
2926 ssrc_infos->push_back(info);
2927 ssrc_info = ssrc_infos->end() - 1;
2928 }
2929
2930 // Store the info to the |ssrc_info|.
2931 if (attribute == kSsrcAttributeCname) {
2932 // RFC 5576
2933 // cname:<value>
2934 ssrc_info->cname = value;
2935 } else if (attribute == kSsrcAttributeMsid) {
2936 // draft-alvestrand-mmusic-msid-00
2937 // "msid:" identifier [ " " appdata ]
2938 std::vector<std::string> fields;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002939 rtc::split(value, kSdpDelimiterSpace, &fields);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002940 if (fields.size() < 1 || fields.size() > 2) {
2941 return ParseFailed(line,
2942 "Expected format \"msid:<identifier>[ <appdata>]\".",
2943 error);
2944 }
deadbeef9d3584c2016-02-16 17:54:10 -08002945 ssrc_info->stream_id = fields[0];
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002946 if (fields.size() == 2) {
deadbeef9d3584c2016-02-16 17:54:10 -08002947 ssrc_info->track_id = fields[1];
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002948 }
2949 } else if (attribute == kSsrcAttributeMslabel) {
2950 // draft-alvestrand-rtcweb-mid-01
2951 // mslabel:<value>
2952 ssrc_info->mslabel = value;
2953 } else if (attribute == kSSrcAttributeLabel) {
2954 // The label isn't defined.
2955 // label:<value>
2956 ssrc_info->label = value;
2957 }
2958 return true;
2959}
2960
2961bool ParseSsrcGroupAttribute(const std::string& line,
2962 SsrcGroupVec* ssrc_groups,
2963 SdpParseError* error) {
nisseede5da42017-01-12 05:15:36 -08002964 RTC_DCHECK(ssrc_groups != NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002965 // RFC 5576
2966 // a=ssrc-group:<semantics> <ssrc-id> ...
2967 std::vector<std::string> fields;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002968 rtc::split(line.substr(kLinePrefixLength),
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002969 kSdpDelimiterSpace, &fields);
2970 const size_t expected_min_fields = 2;
2971 if (fields.size() < expected_min_fields) {
2972 return ParseFailedExpectMinFieldNum(line, expected_min_fields, error);
2973 }
2974 std::string semantics;
2975 if (!GetValue(fields[0], kAttributeSsrcGroup, &semantics, error)) {
2976 return false;
2977 }
Peter Boström0c4e06b2015-10-07 12:23:21 +02002978 std::vector<uint32_t> ssrcs;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002979 for (size_t i = 1; i < fields.size(); ++i) {
Peter Boström0c4e06b2015-10-07 12:23:21 +02002980 uint32_t ssrc = 0;
wu@webrtc.org5e760e72014-04-02 23:19:09 +00002981 if (!GetValueFromString(line, fields[i], &ssrc, error)) {
2982 return false;
2983 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002984 ssrcs.push_back(ssrc);
2985 }
2986 ssrc_groups->push_back(SsrcGroup(semantics, ssrcs));
2987 return true;
2988}
2989
2990bool ParseCryptoAttribute(const std::string& line,
2991 MediaContentDescription* media_desc,
2992 SdpParseError* error) {
2993 std::vector<std::string> fields;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002994 rtc::split(line.substr(kLinePrefixLength),
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002995 kSdpDelimiterSpace, &fields);
2996 // RFC 4568
2997 // a=crypto:<tag> <crypto-suite> <key-params> [<session-params>]
2998 const size_t expected_min_fields = 3;
2999 if (fields.size() < expected_min_fields) {
3000 return ParseFailedExpectMinFieldNum(line, expected_min_fields, error);
3001 }
3002 std::string tag_value;
3003 if (!GetValue(fields[0], kAttributeCrypto, &tag_value, error)) {
3004 return false;
3005 }
wu@webrtc.org5e760e72014-04-02 23:19:09 +00003006 int tag = 0;
3007 if (!GetValueFromString(line, tag_value, &tag, error)) {
3008 return false;
3009 }
jbauch083b73f2015-07-16 02:46:32 -07003010 const std::string& crypto_suite = fields[1];
3011 const std::string& key_params = fields[2];
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003012 std::string session_params;
3013 if (fields.size() > 3) {
3014 session_params = fields[3];
3015 }
3016 media_desc->AddCrypto(CryptoParams(tag, crypto_suite, key_params,
3017 session_params));
3018 return true;
3019}
3020
3021// Updates or creates a new codec entry in the audio description with according
deadbeef67cf2c12016-04-13 10:07:16 -07003022// to |name|, |clockrate|, |bitrate|, and |channels|.
3023void UpdateCodec(int payload_type,
3024 const std::string& name,
3025 int clockrate,
3026 int bitrate,
3027 size_t channels,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003028 AudioContentDescription* audio_desc) {
3029 // Codec may already be populated with (only) optional parameters
3030 // (from an fmtp).
changbin.shao@webrtc.org2d25b442015-03-16 04:14:34 +00003031 cricket::AudioCodec codec =
3032 GetCodecWithPayloadType(audio_desc->codecs(), payload_type);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003033 codec.name = name;
3034 codec.clockrate = clockrate;
3035 codec.bitrate = bitrate;
3036 codec.channels = channels;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003037 AddOrReplaceCodec<AudioContentDescription, cricket::AudioCodec>(audio_desc,
3038 codec);
3039}
3040
3041// Updates or creates a new codec entry in the video description according to
deadbeef67cf2c12016-04-13 10:07:16 -07003042// |name|, |width|, |height|, and |framerate|.
3043void UpdateCodec(int payload_type,
3044 const std::string& name,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003045 VideoContentDescription* video_desc) {
3046 // Codec may already be populated with (only) optional parameters
3047 // (from an fmtp).
changbin.shao@webrtc.org2d25b442015-03-16 04:14:34 +00003048 cricket::VideoCodec codec =
3049 GetCodecWithPayloadType(video_desc->codecs(), payload_type);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003050 codec.name = name;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003051 AddOrReplaceCodec<VideoContentDescription, cricket::VideoCodec>(video_desc,
3052 codec);
3053}
3054
3055bool ParseRtpmapAttribute(const std::string& line,
3056 const MediaType media_type,
deadbeef67cf2c12016-04-13 10:07:16 -07003057 const std::vector<int>& payload_types,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003058 MediaContentDescription* media_desc,
3059 SdpParseError* error) {
3060 std::vector<std::string> fields;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00003061 rtc::split(line.substr(kLinePrefixLength),
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003062 kSdpDelimiterSpace, &fields);
3063 // RFC 4566
3064 // a=rtpmap:<payload type> <encoding name>/<clock rate>[/<encodingparameters>]
3065 const size_t expected_min_fields = 2;
3066 if (fields.size() < expected_min_fields) {
3067 return ParseFailedExpectMinFieldNum(line, expected_min_fields, error);
3068 }
3069 std::string payload_type_value;
3070 if (!GetValue(fields[0], kAttributeRtpmap, &payload_type_value, error)) {
3071 return false;
3072 }
wu@webrtc.org5e760e72014-04-02 23:19:09 +00003073 int payload_type = 0;
pkasting@chromium.orge9facf82015-02-17 20:36:28 +00003074 if (!GetPayloadTypeFromString(line, payload_type_value, &payload_type,
3075 error)) {
wu@webrtc.org5e760e72014-04-02 23:19:09 +00003076 return false;
3077 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003078
deadbeef67cf2c12016-04-13 10:07:16 -07003079 if (std::find(payload_types.begin(), payload_types.end(), payload_type) ==
3080 payload_types.end()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003081 LOG(LS_WARNING) << "Ignore rtpmap line that did not appear in the "
3082 << "<fmt> of the m-line: " << line;
3083 return true;
3084 }
jbauch083b73f2015-07-16 02:46:32 -07003085 const std::string& encoder = fields[1];
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003086 std::vector<std::string> codec_params;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00003087 rtc::split(encoder, '/', &codec_params);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003088 // <encoding name>/<clock rate>[/<encodingparameters>]
3089 // 2 mandatory fields
3090 if (codec_params.size() < 2 || codec_params.size() > 3) {
3091 return ParseFailed(line,
3092 "Expected format \"<encoding name>/<clock rate>"
3093 "[/<encodingparameters>]\".",
3094 error);
3095 }
jbauch083b73f2015-07-16 02:46:32 -07003096 const std::string& encoding_name = codec_params[0];
wu@webrtc.org5e760e72014-04-02 23:19:09 +00003097 int clock_rate = 0;
3098 if (!GetValueFromString(line, codec_params[1], &clock_rate, error)) {
3099 return false;
3100 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003101 if (media_type == cricket::MEDIA_TYPE_VIDEO) {
3102 VideoContentDescription* video_desc =
3103 static_cast<VideoContentDescription*>(media_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003104 UpdateCodec(payload_type, encoding_name,
deadbeef67cf2c12016-04-13 10:07:16 -07003105 video_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003106 } else if (media_type == cricket::MEDIA_TYPE_AUDIO) {
3107 // RFC 4566
3108 // For audio streams, <encoding parameters> indicates the number
3109 // of audio channels. This parameter is OPTIONAL and may be
3110 // omitted if the number of channels is one, provided that no
3111 // additional parameters are needed.
Peter Kasting69558702016-01-12 16:26:35 -08003112 size_t channels = 1;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003113 if (codec_params.size() == 3) {
wu@webrtc.org5e760e72014-04-02 23:19:09 +00003114 if (!GetValueFromString(line, codec_params[2], &channels, error)) {
3115 return false;
3116 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003117 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003118 AudioContentDescription* audio_desc =
3119 static_cast<AudioContentDescription*>(media_desc);
ossue1405ad2017-01-23 08:55:48 -08003120 UpdateCodec(payload_type, encoding_name, clock_rate, 0, channels,
deadbeef67cf2c12016-04-13 10:07:16 -07003121 audio_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003122 } else if (media_type == cricket::MEDIA_TYPE_DATA) {
3123 DataContentDescription* data_desc =
3124 static_cast<DataContentDescription*>(media_desc);
deadbeef67cf2c12016-04-13 10:07:16 -07003125 data_desc->AddCodec(cricket::DataCodec(payload_type, encoding_name));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003126 }
3127 return true;
3128}
3129
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003130bool ParseFmtpParam(const std::string& line, std::string* parameter,
3131 std::string* value, SdpParseError* error) {
Donald Curtis0e07f922015-05-15 09:21:23 -07003132 if (!rtc::tokenize_first(line, kSdpDelimiterEqual, parameter, value)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003133 ParseFailed(line, "Unable to parse fmtp parameter. \'=\' missing.", error);
3134 return false;
3135 }
3136 // a=fmtp:<payload_type> <param1>=<value1>; <param2>=<value2>; ...
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003137 return true;
3138}
3139
3140bool ParseFmtpAttributes(const std::string& line, const MediaType media_type,
3141 MediaContentDescription* media_desc,
3142 SdpParseError* error) {
3143 if (media_type != cricket::MEDIA_TYPE_AUDIO &&
3144 media_type != cricket::MEDIA_TYPE_VIDEO) {
3145 return true;
3146 }
Donald Curtis0e07f922015-05-15 09:21:23 -07003147
3148 std::string line_payload;
3149 std::string line_params;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003150
3151 // RFC 5576
3152 // a=fmtp:<format> <format specific parameters>
3153 // At least two fields, whereas the second one is any of the optional
3154 // parameters.
Donald Curtis144d0182015-05-15 13:14:24 -07003155 if (!rtc::tokenize_first(line.substr(kLinePrefixLength), kSdpDelimiterSpace,
3156 &line_payload, &line_params)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003157 ParseFailedExpectMinFieldNum(line, 2, error);
3158 return false;
3159 }
3160
Donald Curtis0e07f922015-05-15 09:21:23 -07003161 // Parse out the payload information.
pkasting@chromium.orgd3245462015-02-23 21:28:22 +00003162 std::string payload_type_str;
Donald Curtis0e07f922015-05-15 09:21:23 -07003163 if (!GetValue(line_payload, kAttributeFmtp, &payload_type_str, error)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003164 return false;
3165 }
3166
Donald Curtis0e07f922015-05-15 09:21:23 -07003167 int payload_type = 0;
Donald Curtis144d0182015-05-15 13:14:24 -07003168 if (!GetPayloadTypeFromString(line_payload, payload_type_str, &payload_type,
3169 error)) {
Donald Curtis0e07f922015-05-15 09:21:23 -07003170 return false;
3171 }
3172
3173 // Parse out format specific parameters.
3174 std::vector<std::string> fields;
3175 rtc::split(line_params, kSdpDelimiterSemicolon, &fields);
3176
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003177 cricket::CodecParameterMap codec_params;
Donald Curtis144d0182015-05-15 13:14:24 -07003178 for (auto& iter : fields) {
Donald Curtis0e07f922015-05-15 09:21:23 -07003179 if (iter.find(kSdpDelimiterEqual) == std::string::npos) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003180 // Only fmtps with equals are currently supported. Other fmtp types
3181 // should be ignored. Unknown fmtps do not constitute an error.
3182 continue;
3183 }
Donald Curtis0e07f922015-05-15 09:21:23 -07003184
3185 std::string name;
3186 std::string value;
3187 if (!ParseFmtpParam(rtc::string_trim(iter), &name, &value, error)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003188 return false;
3189 }
3190 codec_params[name] = value;
3191 }
3192
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003193 if (media_type == cricket::MEDIA_TYPE_AUDIO) {
3194 UpdateCodec<AudioContentDescription, cricket::AudioCodec>(
pkasting@chromium.orgd3245462015-02-23 21:28:22 +00003195 media_desc, payload_type, codec_params);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003196 } else if (media_type == cricket::MEDIA_TYPE_VIDEO) {
3197 UpdateCodec<VideoContentDescription, cricket::VideoCodec>(
pkasting@chromium.orgd3245462015-02-23 21:28:22 +00003198 media_desc, payload_type, codec_params);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003199 }
3200 return true;
3201}
3202
3203bool ParseRtcpFbAttribute(const std::string& line, const MediaType media_type,
3204 MediaContentDescription* media_desc,
3205 SdpParseError* error) {
3206 if (media_type != cricket::MEDIA_TYPE_AUDIO &&
3207 media_type != cricket::MEDIA_TYPE_VIDEO) {
3208 return true;
3209 }
3210 std::vector<std::string> rtcp_fb_fields;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00003211 rtc::split(line.c_str(), kSdpDelimiterSpace, &rtcp_fb_fields);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003212 if (rtcp_fb_fields.size() < 2) {
3213 return ParseFailedGetValue(line, kAttributeRtcpFb, error);
3214 }
3215 std::string payload_type_string;
3216 if (!GetValue(rtcp_fb_fields[0], kAttributeRtcpFb, &payload_type_string,
3217 error)) {
3218 return false;
3219 }
wu@webrtc.org5e760e72014-04-02 23:19:09 +00003220 int payload_type = kWildcardPayloadType;
3221 if (payload_type_string != "*") {
pkasting@chromium.orge9facf82015-02-17 20:36:28 +00003222 if (!GetPayloadTypeFromString(line, payload_type_string, &payload_type,
3223 error)) {
wu@webrtc.org5e760e72014-04-02 23:19:09 +00003224 return false;
3225 }
3226 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003227 std::string id = rtcp_fb_fields[1];
3228 std::string param = "";
3229 for (std::vector<std::string>::iterator iter = rtcp_fb_fields.begin() + 2;
3230 iter != rtcp_fb_fields.end(); ++iter) {
3231 param.append(*iter);
3232 }
3233 const cricket::FeedbackParam feedback_param(id, param);
3234
3235 if (media_type == cricket::MEDIA_TYPE_AUDIO) {
pkasting@chromium.orgd3245462015-02-23 21:28:22 +00003236 UpdateCodec<AudioContentDescription, cricket::AudioCodec>(
3237 media_desc, payload_type, feedback_param);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003238 } else if (media_type == cricket::MEDIA_TYPE_VIDEO) {
pkasting@chromium.orgd3245462015-02-23 21:28:22 +00003239 UpdateCodec<VideoContentDescription, cricket::VideoCodec>(
3240 media_desc, payload_type, feedback_param);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003241 }
3242 return true;
3243}
3244
3245} // namespace webrtc