blob: 93b77274bfc79a3ec550cf400d19d244c31fab45 [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);
jiayl@webrtc.org9c16c392014-05-01 18:30:30 +0000237static void BuildSctpContentAttributes(std::string* message, int sctp_port);
deadbeef9d3584c2016-02-16 17:54:10 -0800238static void BuildRtpContentAttributes(const MediaContentDescription* media_desc,
239 const MediaType media_type,
240 bool unified_plan_sdp,
241 std::string* message);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000242static void BuildRtpMap(const MediaContentDescription* media_desc,
243 const MediaType media_type,
244 std::string* message);
245static void BuildCandidate(const std::vector<Candidate>& candidates,
honghaiza54a0802015-12-16 18:37:23 -0800246 bool include_ufrag,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000247 std::string* message);
248static void BuildIceOptions(const std::vector<std::string>& transport_options,
249 std::string* message);
pthatcher@webrtc.org3341b402015-02-13 21:14:22 +0000250static bool IsRtp(const std::string& protocol);
251static bool IsDtlsSctp(const std::string& protocol);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000252static bool ParseSessionDescription(const std::string& message, size_t* pos,
253 std::string* session_id,
254 std::string* session_version,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000255 TransportDescription* session_td,
256 RtpHeaderExtensions* session_extmaps,
257 cricket::SessionDescription* desc,
258 SdpParseError* error);
259static bool ParseGroupAttribute(const std::string& line,
260 cricket::SessionDescription* desc,
261 SdpParseError* error);
262static bool ParseMediaDescription(
263 const std::string& message,
264 const TransportDescription& session_td,
265 const RtpHeaderExtensions& session_extmaps,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000266 size_t* pos, cricket::SessionDescription* desc,
267 std::vector<JsepIceCandidate*>* candidates,
268 SdpParseError* error);
269static bool ParseContent(const std::string& message,
270 const MediaType media_type,
271 int mline_index,
272 const std::string& protocol,
deadbeef67cf2c12016-04-13 10:07:16 -0700273 const std::vector<int>& payload_types,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000274 size_t* pos,
275 std::string* content_name,
deadbeef25ed4352016-12-12 18:37:36 -0800276 bool* bundle_only,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000277 MediaContentDescription* media_desc,
278 TransportDescription* transport,
279 std::vector<JsepIceCandidate*>* candidates,
280 SdpParseError* error);
281static bool ParseSsrcAttribute(const std::string& line,
282 SsrcInfoVec* ssrc_infos,
283 SdpParseError* error);
284static bool ParseSsrcGroupAttribute(const std::string& line,
285 SsrcGroupVec* ssrc_groups,
286 SdpParseError* error);
287static bool ParseCryptoAttribute(const std::string& line,
288 MediaContentDescription* media_desc,
289 SdpParseError* error);
290static bool ParseRtpmapAttribute(const std::string& line,
291 const MediaType media_type,
deadbeef67cf2c12016-04-13 10:07:16 -0700292 const std::vector<int>& payload_types,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000293 MediaContentDescription* media_desc,
294 SdpParseError* error);
295static bool ParseFmtpAttributes(const std::string& line,
296 const MediaType media_type,
297 MediaContentDescription* media_desc,
298 SdpParseError* error);
299static bool ParseFmtpParam(const std::string& line, std::string* parameter,
300 std::string* value, SdpParseError* error);
301static bool ParseCandidate(const std::string& message, Candidate* candidate,
302 SdpParseError* error, bool is_raw);
303static bool ParseRtcpFbAttribute(const std::string& line,
304 const MediaType media_type,
305 MediaContentDescription* media_desc,
306 SdpParseError* error);
307static bool ParseIceOptions(const std::string& line,
308 std::vector<std::string>* transport_options,
309 SdpParseError* error);
310static bool ParseExtmap(const std::string& line,
isheriff6f8d6862016-05-26 11:24:55 -0700311 RtpExtension* extmap,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000312 SdpParseError* error);
313static bool ParseFingerprintAttribute(const std::string& line,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000314 rtc::SSLFingerprint** fingerprint,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000315 SdpParseError* error);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +0000316static bool ParseDtlsSetup(const std::string& line,
317 cricket::ConnectionRole* role,
318 SdpParseError* error);
deadbeef9d3584c2016-02-16 17:54:10 -0800319static bool ParseMsidAttribute(const std::string& line,
320 std::string* stream_id,
321 std::string* track_id,
322 SdpParseError* error);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000323
324// Helper functions
325
326// Below ParseFailed*** functions output the line that caused the parsing
327// failure and the detailed reason (|description|) of the failure to |error|.
328// The functions always return false so that they can be used directly in the
329// following way when error happens:
330// "return ParseFailed***(...);"
331
332// The line starting at |line_start| of |message| is the failing line.
333// The reason for the failure should be provided in the |description|.
334// An example of a description could be "unknown character".
335static bool ParseFailed(const std::string& message,
336 size_t line_start,
337 const std::string& description,
338 SdpParseError* error) {
339 // Get the first line of |message| from |line_start|.
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +0000340 std::string first_line;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000341 size_t line_end = message.find(kNewLine, line_start);
342 if (line_end != std::string::npos) {
343 if (line_end > 0 && (message.at(line_end - 1) == kReturn)) {
344 --line_end;
345 }
346 first_line = message.substr(line_start, (line_end - line_start));
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +0000347 } else {
348 first_line = message.substr(line_start);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000349 }
350
351 if (error) {
352 error->line = first_line;
353 error->description = description;
354 }
355 LOG(LS_ERROR) << "Failed to parse: \"" << first_line
356 << "\". Reason: " << description;
357 return false;
358}
359
360// |line| is the failing line. The reason for the failure should be
361// provided in the |description|.
362static bool ParseFailed(const std::string& line,
363 const std::string& description,
364 SdpParseError* error) {
365 return ParseFailed(line, 0, description, error);
366}
367
368// Parses failure where the failing SDP line isn't know or there are multiple
369// failing lines.
370static bool ParseFailed(const std::string& description,
371 SdpParseError* error) {
372 return ParseFailed("", description, error);
373}
374
375// |line| is the failing line. The failure is due to the fact that |line|
376// doesn't have |expected_fields| fields.
377static bool ParseFailedExpectFieldNum(const std::string& line,
378 int expected_fields,
379 SdpParseError* error) {
380 std::ostringstream description;
381 description << "Expects " << expected_fields << " fields.";
382 return ParseFailed(line, description.str(), error);
383}
384
385// |line| is the failing line. The failure is due to the fact that |line| has
386// less than |expected_min_fields| fields.
387static bool ParseFailedExpectMinFieldNum(const std::string& line,
388 int expected_min_fields,
389 SdpParseError* error) {
390 std::ostringstream description;
391 description << "Expects at least " << expected_min_fields << " fields.";
392 return ParseFailed(line, description.str(), error);
393}
394
395// |line| is the failing line. The failure is due to the fact that it failed to
396// get the value of |attribute|.
397static bool ParseFailedGetValue(const std::string& line,
398 const std::string& attribute,
399 SdpParseError* error) {
400 std::ostringstream description;
401 description << "Failed to get the value of attribute: " << attribute;
402 return ParseFailed(line, description.str(), error);
403}
404
405// The line starting at |line_start| of |message| is the failing line. The
406// failure is due to the line type (e.g. the "m" part of the "m-line")
407// not matching what is expected. The expected line type should be
408// provided as |line_type|.
409static bool ParseFailedExpectLine(const std::string& message,
410 size_t line_start,
411 const char line_type,
412 const std::string& line_value,
413 SdpParseError* error) {
414 std::ostringstream description;
415 description << "Expect line: " << line_type << "=" << line_value;
416 return ParseFailed(message, line_start, description.str(), error);
417}
418
419static bool AddLine(const std::string& line, std::string* message) {
420 if (!message)
421 return false;
422
423 message->append(line);
424 message->append(kLineBreak);
425 return true;
426}
427
428static bool GetLine(const std::string& message,
429 size_t* pos,
430 std::string* line) {
431 size_t line_begin = *pos;
432 size_t line_end = message.find(kNewLine, line_begin);
433 if (line_end == std::string::npos) {
434 return false;
435 }
436 // Update the new start position
437 *pos = line_end + 1;
438 if (line_end > 0 && (message.at(line_end - 1) == kReturn)) {
439 --line_end;
440 }
441 *line = message.substr(line_begin, (line_end - line_begin));
442 const char* cline = line->c_str();
443 // RFC 4566
444 // An SDP session description consists of a number of lines of text of
445 // the form:
446 // <type>=<value>
447 // where <type> MUST be exactly one case-significant character and
448 // <value> is structured text whose format depends on <type>.
449 // Whitespace MUST NOT be used on either side of the "=" sign.
decurtis@webrtc.org8af11042015-01-07 19:15:51 +0000450 if (line->length() < 3 ||
451 !islower(cline[0]) ||
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000452 cline[1] != kSdpDelimiterEqual ||
453 cline[2] == kSdpDelimiterSpace) {
454 *pos = line_begin;
455 return false;
456 }
457 return true;
458}
459
460// Init |os| to "|type|=|value|".
461static void InitLine(const char type,
462 const std::string& value,
463 std::ostringstream* os) {
464 os->str("");
465 *os << type << kSdpDelimiterEqual << value;
466}
467
468// Init |os| to "a=|attribute|".
469static void InitAttrLine(const std::string& attribute, std::ostringstream* os) {
470 InitLine(kLineTypeAttributes, attribute, os);
471}
472
473// Writes a SDP attribute line based on |attribute| and |value| to |message|.
474static void AddAttributeLine(const std::string& attribute, int value,
475 std::string* message) {
476 std::ostringstream os;
477 InitAttrLine(attribute, &os);
478 os << kSdpDelimiterColon << value;
479 AddLine(os.str(), message);
480}
481
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000482static bool IsLineType(const std::string& message,
483 const char type,
484 size_t line_start) {
485 if (message.size() < line_start + kLinePrefixLength) {
486 return false;
487 }
488 const char* cmessage = message.c_str();
489 return (cmessage[line_start] == type &&
490 cmessage[line_start + 1] == kSdpDelimiterEqual);
491}
492
493static bool IsLineType(const std::string& line,
494 const char type) {
495 return IsLineType(line, type, 0);
496}
497
498static bool GetLineWithType(const std::string& message, size_t* pos,
499 std::string* line, const char type) {
500 if (!IsLineType(message, type, *pos)) {
501 return false;
502 }
503
504 if (!GetLine(message, pos, line))
505 return false;
506
507 return true;
508}
509
510static bool HasAttribute(const std::string& line,
511 const std::string& attribute) {
512 return (line.compare(kLinePrefixLength, attribute.size(), attribute) == 0);
513}
514
Peter Boström0c4e06b2015-10-07 12:23:21 +0200515static bool AddSsrcLine(uint32_t ssrc_id,
516 const std::string& attribute,
517 const std::string& value,
518 std::string* message) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000519 // RFC 5576
520 // a=ssrc:<ssrc-id> <attribute>:<value>
521 std::ostringstream os;
522 InitAttrLine(kAttributeSsrc, &os);
523 os << kSdpDelimiterColon << ssrc_id << kSdpDelimiterSpace
524 << attribute << kSdpDelimiterColon << value;
525 return AddLine(os.str(), message);
526}
527
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000528// Get value only from <attribute>:<value>.
529static bool GetValue(const std::string& message, const std::string& attribute,
530 std::string* value, SdpParseError* error) {
531 std::string leftpart;
Donald Curtis0e07f922015-05-15 09:21:23 -0700532 if (!rtc::tokenize_first(message, kSdpDelimiterColon, &leftpart, value)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000533 return ParseFailedGetValue(message, attribute, error);
534 }
535 // The left part should end with the expected attribute.
536 if (leftpart.length() < attribute.length() ||
537 leftpart.compare(leftpart.length() - attribute.length(),
538 attribute.length(), attribute) != 0) {
539 return ParseFailedGetValue(message, attribute, error);
540 }
541 return true;
542}
543
544static bool CaseInsensitiveFind(std::string str1, std::string str2) {
545 std::transform(str1.begin(), str1.end(), str1.begin(),
546 ::tolower);
547 std::transform(str2.begin(), str2.end(), str2.begin(),
548 ::tolower);
549 return str1.find(str2) != std::string::npos;
550}
551
wu@webrtc.org5e760e72014-04-02 23:19:09 +0000552template <class T>
553static bool GetValueFromString(const std::string& line,
554 const std::string& s,
555 T* t,
556 SdpParseError* error) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000557 if (!rtc::FromString(s, t)) {
wu@webrtc.org5e760e72014-04-02 23:19:09 +0000558 std::ostringstream description;
559 description << "Invalid value: " << s << ".";
560 return ParseFailed(line, description.str(), error);
561 }
562 return true;
563}
564
pkasting@chromium.orge9facf82015-02-17 20:36:28 +0000565static bool GetPayloadTypeFromString(const std::string& line,
566 const std::string& s,
567 int* payload_type,
568 SdpParseError* error) {
569 return GetValueFromString(line, s, payload_type, error) &&
570 cricket::IsValidRtpPayloadType(*payload_type);
571}
572
Taylor Brandstetter5de6b752016-03-09 17:02:30 -0800573// |msid_stream_id| and |msid_track_id| represent the stream/track ID from the
574// "a=msid" attribute, if it exists. They are empty if the attribute does not
575// exist.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000576void CreateTracksFromSsrcInfos(const SsrcInfoVec& ssrc_infos,
Taylor Brandstetter5de6b752016-03-09 17:02:30 -0800577 const std::string& msid_stream_id,
578 const std::string& msid_track_id,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000579 StreamParamsVec* tracks) {
nisseede5da42017-01-12 05:15:36 -0800580 RTC_DCHECK(tracks != NULL);
581 RTC_DCHECK(msid_stream_id.empty() == msid_track_id.empty());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000582 for (SsrcInfoVec::const_iterator ssrc_info = ssrc_infos.begin();
583 ssrc_info != ssrc_infos.end(); ++ssrc_info) {
584 if (ssrc_info->cname.empty()) {
585 continue;
586 }
587
Taylor Brandstetter5de6b752016-03-09 17:02:30 -0800588 std::string stream_id;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000589 std::string track_id;
Taylor Brandstetter5de6b752016-03-09 17:02:30 -0800590 if (ssrc_info->stream_id.empty() && !ssrc_info->mslabel.empty()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000591 // If there's no msid and there's mslabel, we consider this is a sdp from
592 // a older version of client that doesn't support msid.
593 // In that case, we use the mslabel and label to construct the track.
Taylor Brandstetter5de6b752016-03-09 17:02:30 -0800594 stream_id = ssrc_info->mslabel;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000595 track_id = ssrc_info->label;
Taylor Brandstetter5de6b752016-03-09 17:02:30 -0800596 } else if (ssrc_info->stream_id.empty() && !msid_stream_id.empty()) {
597 // If there's no msid in the SSRC attributes, but there's a global one
598 // (from a=msid), use that. This is the case with unified plan SDP.
599 stream_id = msid_stream_id;
600 track_id = msid_track_id;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000601 } else {
Taylor Brandstetter5de6b752016-03-09 17:02:30 -0800602 stream_id = ssrc_info->stream_id;
deadbeef9d3584c2016-02-16 17:54:10 -0800603 track_id = ssrc_info->track_id;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000604 }
Taylor Brandstetter5de6b752016-03-09 17:02:30 -0800605 // If a stream/track ID wasn't populated from the SSRC attributes OR the
606 // msid attribute, use default/random values.
607 if (stream_id.empty()) {
608 stream_id = kDefaultMsid;
609 }
610 if (track_id.empty()) {
611 // TODO(ronghuawu): What should we do if the track id doesn't appear?
612 // Create random string (which will be used as track label later)?
613 track_id = rtc::CreateRandomString(8);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000614 }
615
616 StreamParamsVec::iterator track = tracks->begin();
617 for (; track != tracks->end(); ++track) {
618 if (track->id == track_id) {
619 break;
620 }
621 }
622 if (track == tracks->end()) {
623 // If we don't find an existing track, create a new one.
624 tracks->push_back(StreamParams());
625 track = tracks->end() - 1;
626 }
627 track->add_ssrc(ssrc_info->ssrc_id);
628 track->cname = ssrc_info->cname;
Taylor Brandstetter5de6b752016-03-09 17:02:30 -0800629 track->sync_label = stream_id;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000630 track->id = track_id;
631 }
632}
633
634void GetMediaStreamLabels(const ContentInfo* content,
635 std::set<std::string>* labels) {
636 const MediaContentDescription* media_desc =
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000637 static_cast<const MediaContentDescription*>(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000638 content->description);
639 const cricket::StreamParamsVec& streams = media_desc->streams();
640 for (cricket::StreamParamsVec::const_iterator it = streams.begin();
641 it != streams.end(); ++it) {
642 labels->insert(it->sync_label);
643 }
644}
645
646// RFC 5245
647// It is RECOMMENDED that default candidates be chosen based on the
648// likelihood of those candidates to work with the peer that is being
649// contacted. It is RECOMMENDED that relayed > reflexive > host.
650static const int kPreferenceUnknown = 0;
651static const int kPreferenceHost = 1;
652static const int kPreferenceReflexive = 2;
653static const int kPreferenceRelayed = 3;
654
655static int GetCandidatePreferenceFromType(const std::string& type) {
656 int preference = kPreferenceUnknown;
657 if (type == cricket::LOCAL_PORT_TYPE) {
658 preference = kPreferenceHost;
659 } else if (type == cricket::STUN_PORT_TYPE) {
660 preference = kPreferenceReflexive;
661 } else if (type == cricket::RELAY_PORT_TYPE) {
662 preference = kPreferenceRelayed;
663 } else {
nissec80e7412017-01-11 05:56:46 -0800664 RTC_NOTREACHED();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000665 }
666 return preference;
667}
668
guoweis@webrtc.org57ac2c82015-02-06 00:45:13 +0000669// Get ip and port of the default destination from the |candidates| with the
670// given value of |component_id|. The default candidate should be the one most
671// likely to work, typically IPv4 relay.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000672// RFC 5245
673// The value of |component_id| currently supported are 1 (RTP) and 2 (RTCP).
674// TODO: Decide the default destination in webrtcsession and
675// pass it down via SessionDescription.
pthatcher@webrtc.orgc9d6d142014-10-23 23:37:22 +0000676static void GetDefaultDestination(
677 const std::vector<Candidate>& candidates,
678 int component_id, std::string* port,
679 std::string* ip, std::string* addr_type) {
perkj@webrtc.orgd105cc82014-11-07 11:22:06 +0000680 *addr_type = kConnectionIpv4Addrtype;
pthatcher@webrtc.orgc9d6d142014-10-23 23:37:22 +0000681 *port = kDummyPort;
682 *ip = kDummyAddress;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000683 int current_preference = kPreferenceUnknown;
guoweis@webrtc.org57ac2c82015-02-06 00:45:13 +0000684 int current_family = AF_UNSPEC;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000685 for (std::vector<Candidate>::const_iterator it = candidates.begin();
686 it != candidates.end(); ++it) {
687 if (it->component() != component_id) {
688 continue;
689 }
guoweis@webrtc.org57ac2c82015-02-06 00:45:13 +0000690 // Default destination should be UDP only.
691 if (it->protocol() != cricket::UDP_PROTOCOL_NAME) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000692 continue;
693 }
guoweis@webrtc.org57ac2c82015-02-06 00:45:13 +0000694 const int preference = GetCandidatePreferenceFromType(it->type());
695 const int family = it->address().ipaddr().family();
696 // See if this candidate is more preferable then the current one if it's the
697 // same family. Or if the current family is IPv4 already so we could safely
698 // ignore all IPv6 ones. WebRTC bug 4269.
699 // http://code.google.com/p/webrtc/issues/detail?id=4269
700 if ((preference <= current_preference && current_family == family) ||
701 (current_family == AF_INET && family == AF_INET6)) {
702 continue;
703 }
pthatcher@webrtc.orgc9d6d142014-10-23 23:37:22 +0000704 if (family == AF_INET) {
705 addr_type->assign(kConnectionIpv4Addrtype);
706 } else if (family == AF_INET6) {
707 addr_type->assign(kConnectionIpv6Addrtype);
708 }
guoweis@webrtc.org57ac2c82015-02-06 00:45:13 +0000709 current_preference = preference;
710 current_family = family;
711 *port = it->address().PortAsString();
712 *ip = it->address().ipaddr().ToString();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000713 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000714}
715
wu@webrtc.org4c3e9912014-07-16 21:03:13 +0000716// Update |mline|'s default destination and append a c line after it.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000717static void UpdateMediaDefaultDestination(
wu@webrtc.org4c3e9912014-07-16 21:03:13 +0000718 const std::vector<Candidate>& candidates,
jbauch083b73f2015-07-16 02:46:32 -0700719 const std::string& mline,
wu@webrtc.org4c3e9912014-07-16 21:03:13 +0000720 std::string* message) {
721 std::string new_lines;
722 AddLine(mline, &new_lines);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000723 // RFC 4566
724 // m=<media> <port> <proto> <fmt> ...
725 std::vector<std::string> fields;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000726 rtc::split(mline, kSdpDelimiterSpace, &fields);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000727 if (fields.size() < 3) {
728 return;
729 }
730
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000731 std::ostringstream os;
pthatcher@webrtc.orgc9d6d142014-10-23 23:37:22 +0000732 std::string rtp_port, rtp_ip, addr_type;
733 GetDefaultDestination(candidates, ICE_CANDIDATE_COMPONENT_RTP,
734 &rtp_port, &rtp_ip, &addr_type);
735 // Found default RTP candidate.
736 // RFC 5245
737 // The default candidates are added to the SDP as the default
738 // destination for media. For streams based on RTP, this is done by
739 // placing the IP address and port of the RTP candidate into the c and m
740 // lines, respectively.
741 // Update the port in the m line.
742 // If this is a m-line with port equal to 0, we don't change it.
743 if (fields[1] != kMediaPortRejected) {
744 new_lines.replace(fields[0].size() + 1,
745 fields[1].size(),
746 rtp_port);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000747 }
pthatcher@webrtc.orgc9d6d142014-10-23 23:37:22 +0000748 // Add the c line.
749 // RFC 4566
750 // c=<nettype> <addrtype> <connection-address>
751 InitLine(kLineTypeConnection, kConnectionNettype, &os);
752 os << " " << addr_type << " " << rtp_ip;
753 AddLine(os.str(), &new_lines);
wu@webrtc.org4c3e9912014-07-16 21:03:13 +0000754 message->append(new_lines);
755}
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000756
wu@webrtc.org4c3e9912014-07-16 21:03:13 +0000757// Gets "a=rtcp" line if found default RTCP candidate from |candidates|.
758static std::string GetRtcpLine(const std::vector<Candidate>& candidates) {
pthatcher@webrtc.orgc9d6d142014-10-23 23:37:22 +0000759 std::string rtcp_line, rtcp_port, rtcp_ip, addr_type;
760 GetDefaultDestination(candidates, ICE_CANDIDATE_COMPONENT_RTCP,
761 &rtcp_port, &rtcp_ip, &addr_type);
762 // Found default RTCP candidate.
763 // RFC 5245
764 // If the agent is utilizing RTCP, it MUST encode the RTCP candidate
765 // using the a=rtcp attribute as defined in RFC 3605.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000766
pthatcher@webrtc.orgc9d6d142014-10-23 23:37:22 +0000767 // RFC 3605
768 // rtcp-attribute = "a=rtcp:" port [nettype space addrtype space
769 // connection-address] CRLF
770 std::ostringstream os;
771 InitAttrLine(kAttributeRtcp, &os);
772 os << kSdpDelimiterColon
773 << rtcp_port << " "
774 << kConnectionNettype << " "
775 << addr_type << " "
776 << rtcp_ip;
777 rtcp_line = os.str();
wu@webrtc.org4c3e9912014-07-16 21:03:13 +0000778 return rtcp_line;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000779}
780
781// Get candidates according to the mline index from SessionDescriptionInterface.
782static void GetCandidatesByMindex(const SessionDescriptionInterface& desci,
783 int mline_index,
784 std::vector<Candidate>* candidates) {
785 if (!candidates) {
786 return;
787 }
788 const IceCandidateCollection* cc = desci.candidates(mline_index);
789 for (size_t i = 0; i < cc->count(); ++i) {
790 const IceCandidateInterface* candidate = cc->at(i);
791 candidates->push_back(candidate->candidate());
792 }
793}
794
deadbeef90f1e1e2017-02-10 12:35:05 -0800795static bool IsValidPort(int port) {
796 return port >= 0 && port <= 65535;
797}
798
deadbeef9d3584c2016-02-16 17:54:10 -0800799std::string SdpSerialize(const JsepSessionDescription& jdesc,
800 bool unified_plan_sdp) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000801 const cricket::SessionDescription* desc = jdesc.description();
802 if (!desc) {
803 return "";
804 }
805
806 std::string message;
807
808 // Session Description.
809 AddLine(kSessionVersion, &message);
810 // Session Origin
811 // RFC 4566
812 // o=<username> <sess-id> <sess-version> <nettype> <addrtype>
813 // <unicast-address>
814 std::ostringstream os;
815 InitLine(kLineTypeOrigin, kSessionOriginUsername, &os);
jbauch083b73f2015-07-16 02:46:32 -0700816 const std::string& session_id = jdesc.session_id().empty() ?
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000817 kSessionOriginSessionId : jdesc.session_id();
jbauch083b73f2015-07-16 02:46:32 -0700818 const std::string& session_version = jdesc.session_version().empty() ?
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000819 kSessionOriginSessionVersion : jdesc.session_version();
820 os << " " << session_id << " " << session_version << " "
821 << kSessionOriginNettype << " " << kSessionOriginAddrtype << " "
822 << kSessionOriginAddress;
823 AddLine(os.str(), &message);
824 AddLine(kSessionName, &message);
825
826 // Time Description.
827 AddLine(kTimeDescription, &message);
828
829 // Group
830 if (desc->HasGroup(cricket::GROUP_TYPE_BUNDLE)) {
831 std::string group_line = kAttrGroup;
832 const cricket::ContentGroup* group =
833 desc->GetGroupByName(cricket::GROUP_TYPE_BUNDLE);
nisseede5da42017-01-12 05:15:36 -0800834 RTC_DCHECK(group != NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000835 const cricket::ContentNames& content_names = group->content_names();
836 for (cricket::ContentNames::const_iterator it = content_names.begin();
837 it != content_names.end(); ++it) {
838 group_line.append(" ");
839 group_line.append(*it);
840 }
841 AddLine(group_line, &message);
842 }
843
844 // MediaStream semantics
845 InitAttrLine(kAttributeMsidSemantics, &os);
846 os << kSdpDelimiterColon << " " << kMediaStreamSemantic;
wu@webrtc.org4c3e9912014-07-16 21:03:13 +0000847
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000848 std::set<std::string> media_stream_labels;
849 const ContentInfo* audio_content = GetFirstAudioContent(desc);
850 if (audio_content)
851 GetMediaStreamLabels(audio_content, &media_stream_labels);
wu@webrtc.org4c3e9912014-07-16 21:03:13 +0000852
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000853 const ContentInfo* video_content = GetFirstVideoContent(desc);
854 if (video_content)
855 GetMediaStreamLabels(video_content, &media_stream_labels);
wu@webrtc.org4c3e9912014-07-16 21:03:13 +0000856
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000857 for (std::set<std::string>::const_iterator it =
858 media_stream_labels.begin(); it != media_stream_labels.end(); ++it) {
859 os << " " << *it;
860 }
861 AddLine(os.str(), &message);
862
wu@webrtc.org4c3e9912014-07-16 21:03:13 +0000863 // Preserve the order of the media contents.
864 int mline_index = -1;
865 for (cricket::ContentInfos::const_iterator it = desc->contents().begin();
866 it != desc->contents().end(); ++it) {
867 const MediaContentDescription* mdesc =
868 static_cast<const MediaContentDescription*>(it->description);
869 std::vector<Candidate> candidates;
870 GetCandidatesByMindex(jdesc, ++mline_index, &candidates);
deadbeef9d3584c2016-02-16 17:54:10 -0800871 BuildMediaDescription(&*it, desc->GetTransportInfoByName(it->name),
872 mdesc->type(), candidates, unified_plan_sdp,
wu@webrtc.org4c3e9912014-07-16 21:03:13 +0000873 &message);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000874 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000875 return message;
876}
877
878// Serializes the passed in IceCandidateInterface to a SDP string.
879// candidate - The candidate to be serialized.
Honghai Zhang7fb69db2016-03-14 11:59:18 -0700880std::string SdpSerializeCandidate(const IceCandidateInterface& candidate) {
881 return SdpSerializeCandidate(candidate.candidate());
882}
883
884// Serializes a cricket Candidate.
885std::string SdpSerializeCandidate(const cricket::Candidate& candidate) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000886 std::string message;
Honghai Zhang7fb69db2016-03-14 11:59:18 -0700887 std::vector<cricket::Candidate> candidates(1, candidate);
honghaiza54a0802015-12-16 18:37:23 -0800888 BuildCandidate(candidates, true, &message);
wu@webrtc.orgec9f5fb2014-06-24 17:05:10 +0000889 // From WebRTC draft section 4.8.1.1 candidate-attribute will be
890 // just candidate:<candidate> not a=candidate:<blah>CRLF
nisseede5da42017-01-12 05:15:36 -0800891 RTC_DCHECK(message.find("a=") == 0);
wu@webrtc.orgec9f5fb2014-06-24 17:05:10 +0000892 message.erase(0, 2);
nisseede5da42017-01-12 05:15:36 -0800893 RTC_DCHECK(message.find(kLineBreak) == message.size() - 2);
wu@webrtc.orgec9f5fb2014-06-24 17:05:10 +0000894 message.resize(message.size() - 2);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000895 return message;
896}
897
898bool SdpDeserialize(const std::string& message,
899 JsepSessionDescription* jdesc,
900 SdpParseError* error) {
901 std::string session_id;
902 std::string session_version;
Peter Thatcher7cbd1882015-09-17 18:54:52 -0700903 TransportDescription session_td("", "");
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000904 RtpHeaderExtensions session_extmaps;
905 cricket::SessionDescription* desc = new cricket::SessionDescription();
906 std::vector<JsepIceCandidate*> candidates;
907 size_t current_pos = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000908
909 // Session Description
910 if (!ParseSessionDescription(message, &current_pos, &session_id,
deadbeefc80741f2015-10-22 13:14:45 -0700911 &session_version, &session_td, &session_extmaps,
912 desc, error)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000913 delete desc;
914 return false;
915 }
916
917 // Media Description
deadbeefc80741f2015-10-22 13:14:45 -0700918 if (!ParseMediaDescription(message, session_td, session_extmaps, &current_pos,
919 desc, &candidates, error)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000920 delete desc;
921 for (std::vector<JsepIceCandidate*>::const_iterator
922 it = candidates.begin(); it != candidates.end(); ++it) {
923 delete *it;
924 }
925 return false;
926 }
927
928 jdesc->Initialize(desc, session_id, session_version);
929
930 for (std::vector<JsepIceCandidate*>::const_iterator
931 it = candidates.begin(); it != candidates.end(); ++it) {
932 jdesc->AddCandidate(*it);
933 delete *it;
934 }
935 return true;
936}
937
938bool SdpDeserializeCandidate(const std::string& message,
939 JsepIceCandidate* jcandidate,
940 SdpParseError* error) {
nisseede5da42017-01-12 05:15:36 -0800941 RTC_DCHECK(jcandidate != NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000942 Candidate candidate;
943 if (!ParseCandidate(message, &candidate, error, true)) {
944 return false;
945 }
946 jcandidate->SetCandidate(candidate);
947 return true;
948}
949
Honghai Zhang7fb69db2016-03-14 11:59:18 -0700950bool SdpDeserializeCandidate(const std::string& transport_name,
951 const std::string& message,
952 cricket::Candidate* candidate,
953 SdpParseError* error) {
nisseede5da42017-01-12 05:15:36 -0800954 RTC_DCHECK(candidate != nullptr);
Honghai Zhang7fb69db2016-03-14 11:59:18 -0700955 if (!ParseCandidate(message, candidate, error, true)) {
956 return false;
957 }
958 candidate->set_transport_name(transport_name);
959 return true;
960}
961
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000962bool ParseCandidate(const std::string& message, Candidate* candidate,
963 SdpParseError* error, bool is_raw) {
nisseede5da42017-01-12 05:15:36 -0800964 RTC_DCHECK(candidate != NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000965
966 // Get the first line from |message|.
jiayl@webrtc.org7ec3f9f2014-08-08 23:09:15 +0000967 std::string first_line = message;
968 size_t pos = 0;
969 GetLine(message, &pos, &first_line);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000970
jiayl@webrtc.org7ec3f9f2014-08-08 23:09:15 +0000971 // Makes sure |message| contains only one line.
972 if (message.size() > first_line.size()) {
973 std::string left, right;
Donald Curtis0e07f922015-05-15 09:21:23 -0700974 if (rtc::tokenize_first(message, kNewLine, &left, &right) &&
975 !right.empty()) {
jiayl@webrtc.org7ec3f9f2014-08-08 23:09:15 +0000976 return ParseFailed(message, 0, "Expect one line only", error);
977 }
978 }
979
980 // From WebRTC draft section 4.8.1.1 candidate-attribute should be
981 // candidate:<candidate> when trickled, but we still support
982 // a=candidate:<blah>CRLF for backward compatibility and for parsing a line
983 // from the SDP.
984 if (IsLineType(first_line, kLineTypeAttributes)) {
985 first_line = first_line.substr(kLinePrefixLength);
986 }
987
988 std::string attribute_candidate;
989 std::string candidate_value;
990
991 // |first_line| must be in the form of "candidate:<value>".
Donald Curtis144d0182015-05-15 13:14:24 -0700992 if (!rtc::tokenize_first(first_line, kSdpDelimiterColon, &attribute_candidate,
993 &candidate_value) ||
jiayl@webrtc.org7ec3f9f2014-08-08 23:09:15 +0000994 attribute_candidate != kAttributeCandidate) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000995 if (is_raw) {
996 std::ostringstream description;
jiayl@webrtc.org7ec3f9f2014-08-08 23:09:15 +0000997 description << "Expect line: " << kAttributeCandidate
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000998 << ":" << "<candidate-str>";
999 return ParseFailed(first_line, 0, description.str(), error);
1000 } else {
1001 return ParseFailedExpectLine(first_line, 0, kLineTypeAttributes,
1002 kAttributeCandidate, error);
1003 }
1004 }
1005
1006 std::vector<std::string> fields;
jiayl@webrtc.org7ec3f9f2014-08-08 23:09:15 +00001007 rtc::split(candidate_value, kSdpDelimiterSpace, &fields);
1008
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001009 // RFC 5245
1010 // a=candidate:<foundation> <component-id> <transport> <priority>
1011 // <connection-address> <port> typ <candidate-types>
1012 // [raddr <connection-address>] [rport <port>]
1013 // *(SP extension-att-name SP extension-att-value)
1014 const size_t expected_min_fields = 8;
1015 if (fields.size() < expected_min_fields ||
1016 (fields[6] != kAttributeCandidateTyp)) {
1017 return ParseFailedExpectMinFieldNum(first_line, expected_min_fields, error);
1018 }
jbauch083b73f2015-07-16 02:46:32 -07001019 const std::string& foundation = fields[0];
jiayl@webrtc.org7ec3f9f2014-08-08 23:09:15 +00001020
wu@webrtc.org5e760e72014-04-02 23:19:09 +00001021 int component_id = 0;
1022 if (!GetValueFromString(first_line, fields[1], &component_id, error)) {
1023 return false;
1024 }
jbauch083b73f2015-07-16 02:46:32 -07001025 const std::string& transport = fields[2];
Peter Boström0c4e06b2015-10-07 12:23:21 +02001026 uint32_t priority = 0;
wu@webrtc.org5e760e72014-04-02 23:19:09 +00001027 if (!GetValueFromString(first_line, fields[3], &priority, error)) {
1028 return false;
1029 }
jbauch083b73f2015-07-16 02:46:32 -07001030 const std::string& connection_address = fields[4];
wu@webrtc.org5e760e72014-04-02 23:19:09 +00001031 int port = 0;
1032 if (!GetValueFromString(first_line, fields[5], &port, error)) {
1033 return false;
1034 }
deadbeef90f1e1e2017-02-10 12:35:05 -08001035 if (!IsValidPort(port)) {
1036 return ParseFailed(first_line, "Invalid port number.", error);
1037 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001038 SocketAddress address(connection_address, port);
1039
1040 cricket::ProtocolType protocol;
hnslb68cc752016-12-13 10:33:41 -08001041 if (!StringToProto(transport.c_str(), &protocol)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001042 return ParseFailed(first_line, "Unsupported transport type.", error);
1043 }
hnslb68cc752016-12-13 10:33:41 -08001044 switch (protocol) {
1045 case cricket::PROTO_UDP:
1046 case cricket::PROTO_TCP:
1047 case cricket::PROTO_SSLTCP:
1048 // Supported protocol.
1049 break;
1050 default:
1051 return ParseFailed(first_line, "Unsupported transport type.", error);
1052 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001053
1054 std::string candidate_type;
jbauch083b73f2015-07-16 02:46:32 -07001055 const std::string& type = fields[7];
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001056 if (type == kCandidateHost) {
1057 candidate_type = cricket::LOCAL_PORT_TYPE;
1058 } else if (type == kCandidateSrflx) {
1059 candidate_type = cricket::STUN_PORT_TYPE;
1060 } else if (type == kCandidateRelay) {
1061 candidate_type = cricket::RELAY_PORT_TYPE;
Honghai Zhang7fb69db2016-03-14 11:59:18 -07001062 } else if (type == kCandidatePrflx) {
1063 candidate_type = cricket::PRFLX_PORT_TYPE;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001064 } else {
1065 return ParseFailed(first_line, "Unsupported candidate type.", error);
1066 }
1067
1068 size_t current_position = expected_min_fields;
1069 SocketAddress related_address;
1070 // The 2 optional fields for related address
1071 // [raddr <connection-address>] [rport <port>]
1072 if (fields.size() >= (current_position + 2) &&
1073 fields[current_position] == kAttributeCandidateRaddr) {
1074 related_address.SetIP(fields[++current_position]);
1075 ++current_position;
1076 }
1077 if (fields.size() >= (current_position + 2) &&
1078 fields[current_position] == kAttributeCandidateRport) {
wu@webrtc.org5e760e72014-04-02 23:19:09 +00001079 int port = 0;
1080 if (!GetValueFromString(
1081 first_line, fields[++current_position], &port, error)) {
1082 return false;
1083 }
deadbeef90f1e1e2017-02-10 12:35:05 -08001084 if (!IsValidPort(port)) {
1085 return ParseFailed(first_line, "Invalid port number.", error);
1086 }
wu@webrtc.org5e760e72014-04-02 23:19:09 +00001087 related_address.SetPort(port);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001088 ++current_position;
1089 }
1090
mallinath@webrtc.org2d60c5e2014-08-08 22:29:20 +00001091 // If this is a TCP candidate, it has additional extension as defined in
1092 // RFC 6544.
1093 std::string tcptype;
1094 if (fields.size() >= (current_position + 2) &&
1095 fields[current_position] == kTcpCandidateType) {
1096 tcptype = fields[++current_position];
1097 ++current_position;
1098
1099 if (tcptype != cricket::TCPTYPE_ACTIVE_STR &&
1100 tcptype != cricket::TCPTYPE_PASSIVE_STR &&
1101 tcptype != cricket::TCPTYPE_SIMOPEN_STR) {
1102 return ParseFailed(first_line, "Invalid TCP candidate type.", error);
1103 }
1104
1105 if (protocol != cricket::PROTO_TCP) {
1106 return ParseFailed(first_line, "Invalid non-TCP candidate", error);
1107 }
1108 }
1109
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001110 // Extension
honghaiza54a0802015-12-16 18:37:23 -08001111 // Though non-standard, we support the ICE ufrag and pwd being signaled on
1112 // the candidate to avoid issues with confusing which generation a candidate
1113 // belongs to when trickling multiple generations at the same time.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001114 std::string username;
1115 std::string password;
Peter Boström0c4e06b2015-10-07 12:23:21 +02001116 uint32_t generation = 0;
honghaiza0c44ea2016-03-23 16:07:48 -07001117 uint16_t network_id = 0;
1118 uint16_t network_cost = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001119 for (size_t i = current_position; i + 1 < fields.size(); ++i) {
1120 // RFC 5245
1121 // *(SP extension-att-name SP extension-att-value)
1122 if (fields[i] == kAttributeCandidateGeneration) {
wu@webrtc.org5e760e72014-04-02 23:19:09 +00001123 if (!GetValueFromString(first_line, fields[++i], &generation, error)) {
1124 return false;
1125 }
honghaiza54a0802015-12-16 18:37:23 -08001126 } else if (fields[i] == kAttributeCandidateUfrag) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001127 username = fields[++i];
honghaiza54a0802015-12-16 18:37:23 -08001128 } else if (fields[i] == kAttributeCandidatePwd) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001129 password = fields[++i];
honghaiza0c44ea2016-03-23 16:07:48 -07001130 } else if (fields[i] == kAttributeCandidateNetworkId) {
1131 if (!GetValueFromString(first_line, fields[++i], &network_id, error)) {
1132 return false;
1133 }
honghaize1a0c942016-02-16 14:54:56 -08001134 } else if (fields[i] == kAttributeCandidateNetworkCost) {
1135 if (!GetValueFromString(first_line, fields[++i], &network_cost, error)) {
1136 return false;
1137 }
Honghai Zhang351d77b2016-05-20 15:08:29 -07001138 network_cost = std::min(network_cost, rtc::kNetworkCostMax);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001139 } else {
1140 // Skip the unknown extension.
1141 ++i;
1142 }
1143 }
1144
guoweis@webrtc.org61c12472015-01-15 06:53:07 +00001145 *candidate = Candidate(component_id, cricket::ProtoToString(protocol),
guoweis@webrtc.org950c5182014-12-16 23:01:31 +00001146 address, priority, username, password, candidate_type,
honghaiza0c44ea2016-03-23 16:07:48 -07001147 generation, foundation, network_id, network_cost);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001148 candidate->set_related_address(related_address);
mallinath@webrtc.org2d60c5e2014-08-08 22:29:20 +00001149 candidate->set_tcptype(tcptype);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001150 return true;
1151}
1152
1153bool ParseIceOptions(const std::string& line,
1154 std::vector<std::string>* transport_options,
1155 SdpParseError* error) {
1156 std::string ice_options;
1157 if (!GetValue(line, kAttributeIceOption, &ice_options, error)) {
1158 return false;
1159 }
1160 std::vector<std::string> fields;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001161 rtc::split(ice_options, kSdpDelimiterSpace, &fields);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001162 for (size_t i = 0; i < fields.size(); ++i) {
1163 transport_options->push_back(fields[i]);
1164 }
1165 return true;
1166}
1167
jiayl@webrtc.orgddb85ab2014-09-05 16:31:56 +00001168bool ParseSctpPort(const std::string& line,
1169 int* sctp_port,
1170 SdpParseError* error) {
1171 // draft-ietf-mmusic-sctp-sdp-07
1172 // a=sctp-port
1173 std::vector<std::string> fields;
jiayl@webrtc.orgddb85ab2014-09-05 16:31:56 +00001174 const size_t expected_min_fields = 2;
lally69f57602015-10-08 10:15:04 -07001175 rtc::split(line.substr(kLinePrefixLength), kSdpDelimiterColon, &fields);
1176 if (fields.size() < expected_min_fields) {
1177 fields.resize(0);
1178 rtc::split(line.substr(kLinePrefixLength), kSdpDelimiterSpace, &fields);
1179 }
jiayl@webrtc.orgddb85ab2014-09-05 16:31:56 +00001180 if (fields.size() < expected_min_fields) {
1181 return ParseFailedExpectMinFieldNum(line, expected_min_fields, error);
1182 }
1183 if (!rtc::FromString(fields[1], sctp_port)) {
lally69f57602015-10-08 10:15:04 -07001184 return ParseFailed(line, "Invalid sctp port value.", error);
jiayl@webrtc.orgddb85ab2014-09-05 16:31:56 +00001185 }
1186 return true;
1187}
1188
isheriff6f8d6862016-05-26 11:24:55 -07001189bool ParseExtmap(const std::string& line,
1190 RtpExtension* extmap,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001191 SdpParseError* error) {
1192 // RFC 5285
1193 // a=extmap:<value>["/"<direction>] <URI> <extensionattributes>
1194 std::vector<std::string> fields;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001195 rtc::split(line.substr(kLinePrefixLength),
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001196 kSdpDelimiterSpace, &fields);
1197 const size_t expected_min_fields = 2;
1198 if (fields.size() < expected_min_fields) {
1199 return ParseFailedExpectMinFieldNum(line, expected_min_fields, error);
1200 }
1201 std::string uri = fields[1];
1202
1203 std::string value_direction;
1204 if (!GetValue(fields[0], kAttributeExtmap, &value_direction, error)) {
1205 return false;
1206 }
1207 std::vector<std::string> sub_fields;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001208 rtc::split(value_direction, kSdpDelimiterSlash, &sub_fields);
wu@webrtc.org5e760e72014-04-02 23:19:09 +00001209 int value = 0;
1210 if (!GetValueFromString(line, sub_fields[0], &value, error)) {
1211 return false;
1212 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001213
isheriff6f8d6862016-05-26 11:24:55 -07001214 *extmap = RtpExtension(uri, value);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001215 return true;
1216}
1217
1218void BuildMediaDescription(const ContentInfo* content_info,
1219 const TransportInfo* transport_info,
1220 const MediaType media_type,
wu@webrtc.org4c3e9912014-07-16 21:03:13 +00001221 const std::vector<Candidate>& candidates,
deadbeef9d3584c2016-02-16 17:54:10 -08001222 bool unified_plan_sdp,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001223 std::string* message) {
nisseede5da42017-01-12 05:15:36 -08001224 RTC_DCHECK(message != NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001225 if (content_info == NULL || message == NULL) {
1226 return;
1227 }
1228 // TODO: Rethink if we should use sprintfn instead of stringstream.
1229 // According to the style guide, streams should only be used for logging.
1230 // http://google-styleguide.googlecode.com/svn/
1231 // trunk/cppguide.xml?showone=Streams#Streams
1232 std::ostringstream os;
1233 const MediaContentDescription* media_desc =
henrike@webrtc.org28654cb2013-07-22 21:07:49 +00001234 static_cast<const MediaContentDescription*>(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001235 content_info->description);
nisseede5da42017-01-12 05:15:36 -08001236 RTC_DCHECK(media_desc != NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001237
jiayl@webrtc.org9c16c392014-05-01 18:30:30 +00001238 int sctp_port = cricket::kSctpDefaultPort;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001239
1240 // RFC 4566
1241 // m=<media> <port> <proto> <fmt>
1242 // fmt is a list of payload type numbers that MAY be used in the session.
1243 const char* type = NULL;
1244 if (media_type == cricket::MEDIA_TYPE_AUDIO)
1245 type = kMediaTypeAudio;
1246 else if (media_type == cricket::MEDIA_TYPE_VIDEO)
1247 type = kMediaTypeVideo;
1248 else if (media_type == cricket::MEDIA_TYPE_DATA)
1249 type = kMediaTypeData;
1250 else
nissec80e7412017-01-11 05:56:46 -08001251 RTC_NOTREACHED();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001252
1253 std::string fmt;
1254 if (media_type == cricket::MEDIA_TYPE_VIDEO) {
1255 const VideoContentDescription* video_desc =
1256 static_cast<const VideoContentDescription*>(media_desc);
1257 for (std::vector<cricket::VideoCodec>::const_iterator it =
1258 video_desc->codecs().begin();
1259 it != video_desc->codecs().end(); ++it) {
1260 fmt.append(" ");
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001261 fmt.append(rtc::ToString<int>(it->id));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001262 }
1263 } else if (media_type == cricket::MEDIA_TYPE_AUDIO) {
1264 const AudioContentDescription* audio_desc =
1265 static_cast<const AudioContentDescription*>(media_desc);
1266 for (std::vector<cricket::AudioCodec>::const_iterator it =
1267 audio_desc->codecs().begin();
1268 it != audio_desc->codecs().end(); ++it) {
1269 fmt.append(" ");
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001270 fmt.append(rtc::ToString<int>(it->id));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001271 }
1272 } else if (media_type == cricket::MEDIA_TYPE_DATA) {
jiayl@webrtc.org9c16c392014-05-01 18:30:30 +00001273 const DataContentDescription* data_desc =
1274 static_cast<const DataContentDescription*>(media_desc);
pthatcher@webrtc.org3341b402015-02-13 21:14:22 +00001275 if (IsDtlsSctp(media_desc->protocol())) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001276 fmt.append(" ");
jiayl@webrtc.org9c16c392014-05-01 18:30:30 +00001277
1278 for (std::vector<cricket::DataCodec>::const_iterator it =
1279 data_desc->codecs().begin();
1280 it != data_desc->codecs().end(); ++it) {
solenberg9fa49752016-10-08 13:02:44 -07001281 if (cricket::CodecNamesEq(it->name, cricket::kGoogleSctpDataCodecName)
1282 && it->GetParam(cricket::kCodecParamPort, &sctp_port)) {
jiayl@webrtc.org9c16c392014-05-01 18:30:30 +00001283 break;
1284 }
1285 }
1286
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001287 fmt.append(rtc::ToString<int>(sctp_port));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001288 } else {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001289 for (std::vector<cricket::DataCodec>::const_iterator it =
1290 data_desc->codecs().begin();
1291 it != data_desc->codecs().end(); ++it) {
1292 fmt.append(" ");
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001293 fmt.append(rtc::ToString<int>(it->id));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001294 }
1295 }
1296 }
1297 // The fmt must never be empty. If no codecs are found, set the fmt attribute
1298 // to 0.
1299 if (fmt.empty()) {
1300 fmt = " 0";
1301 }
1302
deadbeef25ed4352016-12-12 18:37:36 -08001303 // The port number in the m line will be updated later when associated with
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001304 // the candidates.
deadbeef25ed4352016-12-12 18:37:36 -08001305 //
1306 // A port value of 0 indicates that the m= section is rejected.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001307 // RFC 3264
1308 // To reject an offered stream, the port number in the corresponding stream in
1309 // the answer MUST be set to zero.
deadbeef25ed4352016-12-12 18:37:36 -08001310 //
1311 // However, the BUNDLE draft adds a new meaning to port zero, when used along
1312 // with a=bundle-only.
1313 const std::string& port =
1314 (content_info->rejected || content_info->bundle_only) ? kMediaPortRejected
1315 : kDummyPort;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001316
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001317 rtc::SSLFingerprint* fp = (transport_info) ?
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001318 transport_info->description.identity_fingerprint.get() : NULL;
1319
wu@webrtc.org4c3e9912014-07-16 21:03:13 +00001320 // Add the m and c lines.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001321 InitLine(kLineTypeMedia, type, &os);
1322 os << " " << port << " " << media_desc->protocol() << fmt;
wu@webrtc.org4c3e9912014-07-16 21:03:13 +00001323 std::string mline = os.str();
1324 UpdateMediaDefaultDestination(candidates, mline, message);
1325
1326 // RFC 4566
1327 // b=AS:<bandwidth>
Peter Thatcherc0c3a862015-06-24 15:31:25 -07001328 if (media_desc->bandwidth() >= 1000) {
wu@webrtc.org4c3e9912014-07-16 21:03:13 +00001329 InitLine(kLineTypeSessionBandwidth, kApplicationSpecificMaximum, &os);
1330 os << kSdpDelimiterColon << (media_desc->bandwidth() / 1000);
1331 AddLine(os.str(), message);
1332 }
1333
deadbeef25ed4352016-12-12 18:37:36 -08001334 // Add the a=bundle-only line.
1335 if (content_info->bundle_only) {
1336 InitAttrLine(kAttributeBundleOnly, &os);
1337 AddLine(os.str(), message);
1338 }
1339
wu@webrtc.org4c3e9912014-07-16 21:03:13 +00001340 // Add the a=rtcp line.
pthatcher@webrtc.org3341b402015-02-13 21:14:22 +00001341 if (IsRtp(media_desc->protocol())) {
wu@webrtc.org4c3e9912014-07-16 21:03:13 +00001342 std::string rtcp_line = GetRtcpLine(candidates);
1343 if (!rtcp_line.empty()) {
1344 AddLine(rtcp_line, message);
1345 }
1346 }
1347
honghaiza54a0802015-12-16 18:37:23 -08001348 // Build the a=candidate lines. We don't include ufrag and pwd in the
1349 // candidates in the SDP to avoid redundancy.
1350 BuildCandidate(candidates, false, message);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001351
1352 // Use the transport_info to build the media level ice-ufrag and ice-pwd.
1353 if (transport_info) {
1354 // RFC 5245
1355 // ice-pwd-att = "ice-pwd" ":" password
1356 // ice-ufrag-att = "ice-ufrag" ":" ufrag
1357 // ice-ufrag
deadbeef3f7219b2015-12-28 15:17:14 -08001358 if (!transport_info->description.ice_ufrag.empty()) {
1359 InitAttrLine(kAttributeIceUfrag, &os);
1360 os << kSdpDelimiterColon << transport_info->description.ice_ufrag;
1361 AddLine(os.str(), message);
1362 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001363 // ice-pwd
deadbeef3f7219b2015-12-28 15:17:14 -08001364 if (!transport_info->description.ice_pwd.empty()) {
1365 InitAttrLine(kAttributeIcePwd, &os);
1366 os << kSdpDelimiterColon << transport_info->description.ice_pwd;
1367 AddLine(os.str(), message);
1368 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001369
1370 // draft-petithuguenin-mmusic-ice-attributes-level-03
1371 BuildIceOptions(transport_info->description.transport_options, message);
1372
1373 // RFC 4572
1374 // fingerprint-attribute =
1375 // "fingerprint" ":" hash-func SP fingerprint
1376 if (fp) {
1377 // Insert the fingerprint attribute.
1378 InitAttrLine(kAttributeFingerprint, &os);
1379 os << kSdpDelimiterColon
1380 << fp->algorithm << kSdpDelimiterSpace
1381 << fp->GetRfc4572Fingerprint();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001382 AddLine(os.str(), message);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001383
1384 // Inserting setup attribute.
1385 if (transport_info->description.connection_role !=
1386 cricket::CONNECTIONROLE_NONE) {
1387 // Making sure we are not using "passive" mode.
1388 cricket::ConnectionRole role =
1389 transport_info->description.connection_role;
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00001390 std::string dtls_role_str;
nissec16fa5e2017-02-07 07:18:43 -08001391 const bool success =
1392 cricket::ConnectionRoleToString(role, &dtls_role_str);
1393 RTC_DCHECK(success);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001394 InitAttrLine(kAttributeSetup, &os);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001395 os << kSdpDelimiterColon << dtls_role_str;
1396 AddLine(os.str(), message);
1397 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001398 }
1399 }
1400
1401 // RFC 3388
1402 // mid-attribute = "a=mid:" identification-tag
1403 // identification-tag = token
1404 // Use the content name as the mid identification-tag.
1405 InitAttrLine(kAttributeMid, &os);
1406 os << kSdpDelimiterColon << content_info->name;
1407 AddLine(os.str(), message);
1408
pthatcher@webrtc.org3341b402015-02-13 21:14:22 +00001409 if (IsDtlsSctp(media_desc->protocol())) {
jiayl@webrtc.org9c16c392014-05-01 18:30:30 +00001410 BuildSctpContentAttributes(message, sctp_port);
pthatcher@webrtc.org3341b402015-02-13 21:14:22 +00001411 } else if (IsRtp(media_desc->protocol())) {
deadbeef9d3584c2016-02-16 17:54:10 -08001412 BuildRtpContentAttributes(media_desc, media_type, unified_plan_sdp,
1413 message);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001414 }
1415}
1416
jiayl@webrtc.org9c16c392014-05-01 18:30:30 +00001417void BuildSctpContentAttributes(std::string* message, int sctp_port) {
wu@webrtc.org78187522013-10-07 23:32:02 +00001418 // draft-ietf-mmusic-sctp-sdp-04
1419 // a=sctpmap:sctpmap-number protocol [streams]
lally69f57602015-10-08 10:15:04 -07001420 // TODO(lally): switch this over to mmusic-sctp-sdp-12 (or later), with
1421 // 'a=sctp-port:'
wu@webrtc.org78187522013-10-07 23:32:02 +00001422 std::ostringstream os;
1423 InitAttrLine(kAttributeSctpmap, &os);
jiayl@webrtc.org9c16c392014-05-01 18:30:30 +00001424 os << kSdpDelimiterColon << sctp_port << kSdpDelimiterSpace
wu@webrtc.org78187522013-10-07 23:32:02 +00001425 << kDefaultSctpmapProtocol << kSdpDelimiterSpace
Taylor Brandstetter1d7a6372016-08-24 13:15:27 -07001426 << cricket::kMaxSctpStreams;
wu@webrtc.org78187522013-10-07 23:32:02 +00001427 AddLine(os.str(), message);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001428}
1429
deadbeef9d3584c2016-02-16 17:54:10 -08001430// If unified_plan_sdp is true, will use "a=msid".
1431void BuildRtpContentAttributes(const MediaContentDescription* media_desc,
1432 const MediaType media_type,
1433 bool unified_plan_sdp,
1434 std::string* message) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001435 std::ostringstream os;
1436 // RFC 5285
1437 // a=extmap:<value>["/"<direction>] <URI> <extensionattributes>
1438 // The definitions MUST be either all session level or all media level. This
1439 // implementation uses all media level.
1440 for (size_t i = 0; i < media_desc->rtp_header_extensions().size(); ++i) {
1441 InitAttrLine(kAttributeExtmap, &os);
1442 os << kSdpDelimiterColon << media_desc->rtp_header_extensions()[i].id
1443 << kSdpDelimiterSpace << media_desc->rtp_header_extensions()[i].uri;
1444 AddLine(os.str(), message);
1445 }
1446
1447 // RFC 3264
1448 // a=sendrecv || a=sendonly || a=sendrecv || a=inactive
deadbeefc80741f2015-10-22 13:14:45 -07001449 switch (media_desc->direction()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001450 case cricket::MD_INACTIVE:
1451 InitAttrLine(kAttributeInactive, &os);
1452 break;
1453 case cricket::MD_SENDONLY:
1454 InitAttrLine(kAttributeSendOnly, &os);
1455 break;
1456 case cricket::MD_RECVONLY:
1457 InitAttrLine(kAttributeRecvOnly, &os);
1458 break;
1459 case cricket::MD_SENDRECV:
1460 default:
1461 InitAttrLine(kAttributeSendRecv, &os);
1462 break;
1463 }
1464 AddLine(os.str(), message);
1465
deadbeef9d3584c2016-02-16 17:54:10 -08001466 // draft-ietf-mmusic-msid-11
1467 // a=msid:<stream id> <track id>
1468 if (unified_plan_sdp && !media_desc->streams().empty()) {
1469 if (media_desc->streams().size() > 1u) {
1470 LOG(LS_WARNING) << "Trying to serialize unified plan SDP with more than "
1471 << "one track in a media section. Omitting 'a=msid'.";
1472 } else {
1473 auto track = media_desc->streams().begin();
1474 const std::string& stream_id = track->sync_label;
deadbeef9d3584c2016-02-16 17:54:10 -08001475 InitAttrLine(kAttributeMsid, &os);
1476 os << kSdpDelimiterColon << stream_id << kSdpDelimiterSpace << track->id;
1477 AddLine(os.str(), message);
1478 }
1479 }
1480
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001481 // RFC 5761
1482 // a=rtcp-mux
1483 if (media_desc->rtcp_mux()) {
1484 InitAttrLine(kAttributeRtcpMux, &os);
1485 AddLine(os.str(), message);
1486 }
1487
deadbeef13871492015-12-09 12:37:51 -08001488 // RFC 5506
1489 // a=rtcp-rsize
1490 if (media_desc->rtcp_reduced_size()) {
1491 InitAttrLine(kAttributeRtcpReducedSize, &os);
1492 AddLine(os.str(), message);
1493 }
1494
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001495 // RFC 4568
1496 // a=crypto:<tag> <crypto-suite> <key-params> [<session-params>]
1497 for (std::vector<CryptoParams>::const_iterator it =
1498 media_desc->cryptos().begin();
1499 it != media_desc->cryptos().end(); ++it) {
1500 InitAttrLine(kAttributeCrypto, &os);
1501 os << kSdpDelimiterColon << it->tag << " " << it->cipher_suite << " "
1502 << it->key_params;
1503 if (!it->session_params.empty()) {
1504 os << " " << it->session_params;
1505 }
1506 AddLine(os.str(), message);
1507 }
1508
1509 // RFC 4566
1510 // a=rtpmap:<payload type> <encoding name>/<clock rate>
1511 // [/<encodingparameters>]
1512 BuildRtpMap(media_desc, media_type, message);
1513
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001514 for (StreamParamsVec::const_iterator track = media_desc->streams().begin();
1515 track != media_desc->streams().end(); ++track) {
1516 // Require that the track belongs to a media stream,
1517 // ie the sync_label is set. This extra check is necessary since the
1518 // MediaContentDescription always contains a streamparam with an ssrc even
1519 // if no track or media stream have been created.
1520 if (track->sync_label.empty()) continue;
1521
1522 // Build the ssrc-group lines.
1523 for (size_t i = 0; i < track->ssrc_groups.size(); ++i) {
1524 // RFC 5576
1525 // a=ssrc-group:<semantics> <ssrc-id> ...
1526 if (track->ssrc_groups[i].ssrcs.empty()) {
1527 continue;
1528 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001529 InitAttrLine(kAttributeSsrcGroup, &os);
1530 os << kSdpDelimiterColon << track->ssrc_groups[i].semantics;
Peter Boström0c4e06b2015-10-07 12:23:21 +02001531 std::vector<uint32_t>::const_iterator ssrc =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001532 track->ssrc_groups[i].ssrcs.begin();
1533 for (; ssrc != track->ssrc_groups[i].ssrcs.end(); ++ssrc) {
Peter Boström0c4e06b2015-10-07 12:23:21 +02001534 os << kSdpDelimiterSpace << rtc::ToString<uint32_t>(*ssrc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001535 }
1536 AddLine(os.str(), message);
1537 }
1538 // Build the ssrc lines for each ssrc.
1539 for (size_t i = 0; i < track->ssrcs.size(); ++i) {
Peter Boström0c4e06b2015-10-07 12:23:21 +02001540 uint32_t ssrc = track->ssrcs[i];
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001541 // RFC 5576
1542 // a=ssrc:<ssrc-id> cname:<value>
1543 AddSsrcLine(ssrc, kSsrcAttributeCname,
1544 track->cname, message);
1545
1546 // draft-alvestrand-mmusic-msid-00
1547 // a=ssrc:<ssrc-id> msid:identifier [appdata]
deadbeef9d3584c2016-02-16 17:54:10 -08001548 // The appdata consists of the "id" attribute of a MediaStreamTrack,
1549 // which corresponds to the "id" attribute of StreamParams.
1550 const std::string& stream_id = track->sync_label;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001551 InitAttrLine(kAttributeSsrc, &os);
1552 os << kSdpDelimiterColon << ssrc << kSdpDelimiterSpace
deadbeef9d3584c2016-02-16 17:54:10 -08001553 << kSsrcAttributeMsid << kSdpDelimiterColon << stream_id
1554 << kSdpDelimiterSpace << track->id;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001555 AddLine(os.str(), message);
1556
deadbeef9d3584c2016-02-16 17:54:10 -08001557 // TODO(ronghuawu): Remove below code which is for backward
1558 // compatibility.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001559 // draft-alvestrand-rtcweb-mid-01
1560 // a=ssrc:<ssrc-id> mslabel:<value>
1561 // The label isn't yet defined.
1562 // a=ssrc:<ssrc-id> label:<value>
1563 AddSsrcLine(ssrc, kSsrcAttributeMslabel, track->sync_label, message);
1564 AddSsrcLine(ssrc, kSSrcAttributeLabel, track->id, message);
1565 }
1566 }
1567}
1568
1569void WriteFmtpHeader(int payload_type, std::ostringstream* os) {
1570 // fmtp header: a=fmtp:|payload_type| <parameters>
1571 // Add a=fmtp
1572 InitAttrLine(kAttributeFmtp, os);
1573 // Add :|payload_type|
1574 *os << kSdpDelimiterColon << payload_type;
1575}
1576
1577void WriteRtcpFbHeader(int payload_type, std::ostringstream* os) {
1578 // rtcp-fb header: a=rtcp-fb:|payload_type|
1579 // <parameters>/<ccm <ccm_parameters>>
1580 // Add a=rtcp-fb
1581 InitAttrLine(kAttributeRtcpFb, os);
1582 // Add :
1583 *os << kSdpDelimiterColon;
1584 if (payload_type == kWildcardPayloadType) {
1585 *os << "*";
1586 } else {
1587 *os << payload_type;
1588 }
1589}
1590
1591void WriteFmtpParameter(const std::string& parameter_name,
1592 const std::string& parameter_value,
1593 std::ostringstream* os) {
1594 // fmtp parameters: |parameter_name|=|parameter_value|
1595 *os << parameter_name << kSdpDelimiterEqual << parameter_value;
1596}
1597
1598void WriteFmtpParameters(const cricket::CodecParameterMap& parameters,
1599 std::ostringstream* os) {
1600 for (cricket::CodecParameterMap::const_iterator fmtp = parameters.begin();
1601 fmtp != parameters.end(); ++fmtp) {
hta62a216e2016-04-15 11:02:14 -07001602 // Parameters are a semicolon-separated list, no spaces.
1603 // The list is separated from the header by a space.
1604 if (fmtp == parameters.begin()) {
1605 *os << kSdpDelimiterSpace;
1606 } else {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001607 *os << kSdpDelimiterSemicolon;
1608 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001609 WriteFmtpParameter(fmtp->first, fmtp->second, os);
1610 }
1611}
1612
1613bool IsFmtpParam(const std::string& name) {
ossuaa4b0772017-01-30 07:41:18 -08001614 // RFC 4855, section 3 specifies the mapping of media format parameters to SDP
1615 // parameters. Only ptime, maxptime, channels and rate are placed outside of
1616 // the fmtp line. In WebRTC, channels and rate are already handled separately
1617 // and thus not included in the CodecParameterMap.
1618 return name != kCodecParamPTime && name != kCodecParamMaxPTime;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001619}
1620
1621// Retreives fmtp parameters from |params|, which may contain other parameters
1622// as well, and puts them in |fmtp_parameters|.
1623void GetFmtpParams(const cricket::CodecParameterMap& params,
1624 cricket::CodecParameterMap* fmtp_parameters) {
1625 for (cricket::CodecParameterMap::const_iterator iter = params.begin();
1626 iter != params.end(); ++iter) {
1627 if (IsFmtpParam(iter->first)) {
1628 (*fmtp_parameters)[iter->first] = iter->second;
1629 }
1630 }
1631}
1632
1633template <class T>
1634void AddFmtpLine(const T& codec, std::string* message) {
1635 cricket::CodecParameterMap fmtp_parameters;
1636 GetFmtpParams(codec.params, &fmtp_parameters);
1637 if (fmtp_parameters.empty()) {
1638 // No need to add an fmtp if it will have no (optional) parameters.
1639 return;
1640 }
1641 std::ostringstream os;
1642 WriteFmtpHeader(codec.id, &os);
1643 WriteFmtpParameters(fmtp_parameters, &os);
1644 AddLine(os.str(), message);
1645 return;
1646}
1647
1648template <class T>
1649void AddRtcpFbLines(const T& codec, std::string* message) {
1650 for (std::vector<cricket::FeedbackParam>::const_iterator iter =
1651 codec.feedback_params.params().begin();
1652 iter != codec.feedback_params.params().end(); ++iter) {
1653 std::ostringstream os;
1654 WriteRtcpFbHeader(codec.id, &os);
1655 os << " " << iter->id();
1656 if (!iter->param().empty()) {
1657 os << " " << iter->param();
1658 }
1659 AddLine(os.str(), message);
1660 }
1661}
1662
jiayl@webrtc.orgddb85ab2014-09-05 16:31:56 +00001663bool AddSctpDataCodec(DataContentDescription* media_desc,
1664 int sctp_port) {
solenberg9fa49752016-10-08 13:02:44 -07001665 for (const auto& codec : media_desc->codecs()) {
1666 if (cricket::CodecNamesEq(codec.name, cricket::kGoogleSctpDataCodecName)) {
1667 return ParseFailed("",
1668 "Can't have multiple sctp port attributes.",
1669 NULL);
1670 }
jiayl@webrtc.orgddb85ab2014-09-05 16:31:56 +00001671 }
1672 // Add the SCTP Port number as a pseudo-codec "port" parameter
solenberg9fa49752016-10-08 13:02:44 -07001673 cricket::DataCodec codec_port(cricket::kGoogleSctpDataCodecPlType,
deadbeef67cf2c12016-04-13 10:07:16 -07001674 cricket::kGoogleSctpDataCodecName);
jiayl@webrtc.orgddb85ab2014-09-05 16:31:56 +00001675 codec_port.SetParam(cricket::kCodecParamPort, sctp_port);
1676 LOG(INFO) << "AddSctpDataCodec: Got SCTP Port Number "
1677 << sctp_port;
1678 media_desc->AddCodec(codec_port);
1679 return true;
1680}
1681
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001682bool GetMinValue(const std::vector<int>& values, int* value) {
1683 if (values.empty()) {
1684 return false;
1685 }
1686 std::vector<int>::const_iterator found =
1687 std::min_element(values.begin(), values.end());
1688 *value = *found;
1689 return true;
1690}
1691
1692bool GetParameter(const std::string& name,
1693 const cricket::CodecParameterMap& params, int* value) {
1694 std::map<std::string, std::string>::const_iterator found =
1695 params.find(name);
1696 if (found == params.end()) {
1697 return false;
1698 }
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001699 if (!rtc::FromString(found->second, value)) {
wu@webrtc.org5e760e72014-04-02 23:19:09 +00001700 return false;
1701 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001702 return true;
1703}
1704
1705void BuildRtpMap(const MediaContentDescription* media_desc,
1706 const MediaType media_type,
1707 std::string* message) {
nisseede5da42017-01-12 05:15:36 -08001708 RTC_DCHECK(message != NULL);
1709 RTC_DCHECK(media_desc != NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001710 std::ostringstream os;
1711 if (media_type == cricket::MEDIA_TYPE_VIDEO) {
1712 const VideoContentDescription* video_desc =
1713 static_cast<const VideoContentDescription*>(media_desc);
1714 for (std::vector<cricket::VideoCodec>::const_iterator it =
1715 video_desc->codecs().begin();
1716 it != video_desc->codecs().end(); ++it) {
1717 // RFC 4566
1718 // a=rtpmap:<payload type> <encoding name>/<clock rate>
1719 // [/<encodingparameters>]
1720 if (it->id != kWildcardPayloadType) {
1721 InitAttrLine(kAttributeRtpmap, &os);
1722 os << kSdpDelimiterColon << it->id << " " << it->name
1723 << "/" << kDefaultVideoClockrate;
1724 AddLine(os.str(), message);
1725 }
1726 AddRtcpFbLines(*it, message);
1727 AddFmtpLine(*it, message);
1728 }
1729 } else if (media_type == cricket::MEDIA_TYPE_AUDIO) {
1730 const AudioContentDescription* audio_desc =
1731 static_cast<const AudioContentDescription*>(media_desc);
1732 std::vector<int> ptimes;
1733 std::vector<int> maxptimes;
1734 int max_minptime = 0;
1735 for (std::vector<cricket::AudioCodec>::const_iterator it =
1736 audio_desc->codecs().begin();
1737 it != audio_desc->codecs().end(); ++it) {
nisseede5da42017-01-12 05:15:36 -08001738 RTC_DCHECK(!it->name.empty());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001739 // RFC 4566
1740 // a=rtpmap:<payload type> <encoding name>/<clock rate>
1741 // [/<encodingparameters>]
1742 InitAttrLine(kAttributeRtpmap, &os);
1743 os << kSdpDelimiterColon << it->id << " ";
1744 os << it->name << "/" << it->clockrate;
1745 if (it->channels != 1) {
1746 os << "/" << it->channels;
1747 }
1748 AddLine(os.str(), message);
1749 AddRtcpFbLines(*it, message);
1750 AddFmtpLine(*it, message);
1751 int minptime = 0;
1752 if (GetParameter(kCodecParamMinPTime, it->params, &minptime)) {
1753 max_minptime = std::max(minptime, max_minptime);
1754 }
1755 int ptime;
1756 if (GetParameter(kCodecParamPTime, it->params, &ptime)) {
1757 ptimes.push_back(ptime);
1758 }
1759 int maxptime;
1760 if (GetParameter(kCodecParamMaxPTime, it->params, &maxptime)) {
1761 maxptimes.push_back(maxptime);
1762 }
1763 }
1764 // Populate the maxptime attribute with the smallest maxptime of all codecs
1765 // under the same m-line.
1766 int min_maxptime = INT_MAX;
1767 if (GetMinValue(maxptimes, &min_maxptime)) {
1768 AddAttributeLine(kCodecParamMaxPTime, min_maxptime, message);
1769 }
nisseede5da42017-01-12 05:15:36 -08001770 RTC_DCHECK(min_maxptime > max_minptime);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001771 // Populate the ptime attribute with the smallest ptime or the largest
1772 // minptime, whichever is the largest, for all codecs under the same m-line.
1773 int ptime = INT_MAX;
1774 if (GetMinValue(ptimes, &ptime)) {
1775 ptime = std::min(ptime, min_maxptime);
1776 ptime = std::max(ptime, max_minptime);
1777 AddAttributeLine(kCodecParamPTime, ptime, message);
1778 }
1779 } else if (media_type == cricket::MEDIA_TYPE_DATA) {
1780 const DataContentDescription* data_desc =
1781 static_cast<const DataContentDescription*>(media_desc);
1782 for (std::vector<cricket::DataCodec>::const_iterator it =
1783 data_desc->codecs().begin();
1784 it != data_desc->codecs().end(); ++it) {
1785 // RFC 4566
1786 // a=rtpmap:<payload type> <encoding name>/<clock rate>
1787 // [/<encodingparameters>]
1788 InitAttrLine(kAttributeRtpmap, &os);
1789 os << kSdpDelimiterColon << it->id << " "
1790 << it->name << "/" << it->clockrate;
1791 AddLine(os.str(), message);
1792 }
1793 }
1794}
1795
1796void BuildCandidate(const std::vector<Candidate>& candidates,
honghaiza54a0802015-12-16 18:37:23 -08001797 bool include_ufrag,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001798 std::string* message) {
1799 std::ostringstream os;
1800
1801 for (std::vector<Candidate>::const_iterator it = candidates.begin();
1802 it != candidates.end(); ++it) {
1803 // RFC 5245
1804 // a=candidate:<foundation> <component-id> <transport> <priority>
1805 // <connection-address> <port> typ <candidate-types>
1806 // [raddr <connection-address>] [rport <port>]
1807 // *(SP extension-att-name SP extension-att-value)
1808 std::string type;
1809 // Map the cricket candidate type to "host" / "srflx" / "prflx" / "relay"
1810 if (it->type() == cricket::LOCAL_PORT_TYPE) {
1811 type = kCandidateHost;
1812 } else if (it->type() == cricket::STUN_PORT_TYPE) {
1813 type = kCandidateSrflx;
1814 } else if (it->type() == cricket::RELAY_PORT_TYPE) {
1815 type = kCandidateRelay;
Honghai Zhang7fb69db2016-03-14 11:59:18 -07001816 } else if (it->type() == cricket::PRFLX_PORT_TYPE) {
1817 type = kCandidatePrflx;
1818 // Peer reflexive candidate may be signaled for being removed.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001819 } else {
nissec80e7412017-01-11 05:56:46 -08001820 RTC_NOTREACHED();
Peter Thatcher019087f2015-04-28 09:06:26 -07001821 // Never write out candidates if we don't know the type.
1822 continue;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001823 }
1824
1825 InitAttrLine(kAttributeCandidate, &os);
1826 os << kSdpDelimiterColon
mallinath@webrtc.org2d60c5e2014-08-08 22:29:20 +00001827 << it->foundation() << " "
1828 << it->component() << " "
1829 << it->protocol() << " "
1830 << it->priority() << " "
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001831 << it->address().ipaddr().ToString() << " "
1832 << it->address().PortAsString() << " "
mallinath@webrtc.org2d60c5e2014-08-08 22:29:20 +00001833 << kAttributeCandidateTyp << " "
1834 << type << " ";
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001835
1836 // Related address
1837 if (!it->related_address().IsNil()) {
1838 os << kAttributeCandidateRaddr << " "
1839 << it->related_address().ipaddr().ToString() << " "
1840 << kAttributeCandidateRport << " "
1841 << it->related_address().PortAsString() << " ";
1842 }
1843
mallinath@webrtc.org2d60c5e2014-08-08 22:29:20 +00001844 if (it->protocol() == cricket::TCP_PROTOCOL_NAME) {
mallinath@webrtc.orge999bd02014-08-13 06:05:55 +00001845 os << kTcpCandidateType << " " << it->tcptype() << " ";
mallinath@webrtc.org2d60c5e2014-08-08 22:29:20 +00001846 }
1847
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001848 // Extensions
1849 os << kAttributeCandidateGeneration << " " << it->generation();
honghaiza54a0802015-12-16 18:37:23 -08001850 if (include_ufrag && !it->username().empty()) {
1851 os << " " << kAttributeCandidateUfrag << " " << it->username();
1852 }
honghaiza0c44ea2016-03-23 16:07:48 -07001853 if (it->network_id() > 0) {
1854 os << " " << kAttributeCandidateNetworkId << " " << it->network_id();
1855 }
honghaize1a0c942016-02-16 14:54:56 -08001856 if (it->network_cost() > 0) {
1857 os << " " << kAttributeCandidateNetworkCost << " " << it->network_cost();
1858 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001859
1860 AddLine(os.str(), message);
1861 }
1862}
1863
1864void BuildIceOptions(const std::vector<std::string>& transport_options,
1865 std::string* message) {
1866 if (!transport_options.empty()) {
1867 std::ostringstream os;
1868 InitAttrLine(kAttributeIceOption, &os);
1869 os << kSdpDelimiterColon << transport_options[0];
1870 for (size_t i = 1; i < transport_options.size(); ++i) {
1871 os << kSdpDelimiterSpace << transport_options[i];
1872 }
1873 AddLine(os.str(), message);
1874 }
1875}
1876
pthatcher@webrtc.org3341b402015-02-13 21:14:22 +00001877bool IsRtp(const std::string& protocol) {
1878 return protocol.empty() ||
1879 (protocol.find(cricket::kMediaProtocolRtpPrefix) != std::string::npos);
1880}
1881
1882bool IsDtlsSctp(const std::string& protocol) {
1883 // This intentionally excludes "SCTP" and "SCTP/DTLS".
lally@webrtc.orga7470932015-02-24 20:19:21 +00001884 return protocol.find(cricket::kMediaProtocolDtlsSctp) != std::string::npos;
pthatcher@webrtc.org3341b402015-02-13 21:14:22 +00001885}
1886
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001887bool ParseSessionDescription(const std::string& message, size_t* pos,
1888 std::string* session_id,
1889 std::string* session_version,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001890 TransportDescription* session_td,
1891 RtpHeaderExtensions* session_extmaps,
1892 cricket::SessionDescription* desc,
1893 SdpParseError* error) {
1894 std::string line;
1895
deadbeefc80741f2015-10-22 13:14:45 -07001896 desc->set_msid_supported(false);
1897
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001898 // RFC 4566
1899 // v= (protocol version)
1900 if (!GetLineWithType(message, pos, &line, kLineTypeVersion)) {
1901 return ParseFailedExpectLine(message, *pos, kLineTypeVersion,
1902 std::string(), error);
1903 }
1904 // RFC 4566
1905 // o=<username> <sess-id> <sess-version> <nettype> <addrtype>
1906 // <unicast-address>
1907 if (!GetLineWithType(message, pos, &line, kLineTypeOrigin)) {
1908 return ParseFailedExpectLine(message, *pos, kLineTypeOrigin,
1909 std::string(), error);
1910 }
1911 std::vector<std::string> fields;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001912 rtc::split(line.substr(kLinePrefixLength),
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001913 kSdpDelimiterSpace, &fields);
1914 const size_t expected_fields = 6;
1915 if (fields.size() != expected_fields) {
1916 return ParseFailedExpectFieldNum(line, expected_fields, error);
1917 }
1918 *session_id = fields[1];
1919 *session_version = fields[2];
1920
1921 // RFC 4566
1922 // s= (session name)
1923 if (!GetLineWithType(message, pos, &line, kLineTypeSessionName)) {
1924 return ParseFailedExpectLine(message, *pos, kLineTypeSessionName,
1925 std::string(), error);
1926 }
1927
1928 // Optional lines
1929 // Those are the optional lines, so shouldn't return false if not present.
1930 // RFC 4566
1931 // i=* (session information)
1932 GetLineWithType(message, pos, &line, kLineTypeSessionInfo);
1933
1934 // RFC 4566
1935 // u=* (URI of description)
1936 GetLineWithType(message, pos, &line, kLineTypeSessionUri);
1937
1938 // RFC 4566
1939 // e=* (email address)
1940 GetLineWithType(message, pos, &line, kLineTypeSessionEmail);
1941
1942 // RFC 4566
1943 // p=* (phone number)
1944 GetLineWithType(message, pos, &line, kLineTypeSessionPhone);
1945
1946 // RFC 4566
1947 // c=* (connection information -- not required if included in
1948 // all media)
1949 GetLineWithType(message, pos, &line, kLineTypeConnection);
1950
1951 // RFC 4566
1952 // b=* (zero or more bandwidth information lines)
1953 while (GetLineWithType(message, pos, &line, kLineTypeSessionBandwidth)) {
1954 // By pass zero or more b lines.
1955 }
1956
1957 // RFC 4566
1958 // One or more time descriptions ("t=" and "r=" lines; see below)
1959 // t= (time the session is active)
1960 // r=* (zero or more repeat times)
1961 // Ensure there's at least one time description
1962 if (!GetLineWithType(message, pos, &line, kLineTypeTiming)) {
1963 return ParseFailedExpectLine(message, *pos, kLineTypeTiming, std::string(),
1964 error);
1965 }
1966
1967 while (GetLineWithType(message, pos, &line, kLineTypeRepeatTimes)) {
1968 // By pass zero or more r lines.
1969 }
1970
1971 // Go through the rest of the time descriptions
1972 while (GetLineWithType(message, pos, &line, kLineTypeTiming)) {
1973 while (GetLineWithType(message, pos, &line, kLineTypeRepeatTimes)) {
1974 // By pass zero or more r lines.
1975 }
1976 }
1977
1978 // RFC 4566
1979 // z=* (time zone adjustments)
1980 GetLineWithType(message, pos, &line, kLineTypeTimeZone);
1981
1982 // RFC 4566
1983 // k=* (encryption key)
1984 GetLineWithType(message, pos, &line, kLineTypeEncryptionKey);
1985
1986 // RFC 4566
1987 // a=* (zero or more session attribute lines)
1988 while (GetLineWithType(message, pos, &line, kLineTypeAttributes)) {
1989 if (HasAttribute(line, kAttributeGroup)) {
1990 if (!ParseGroupAttribute(line, desc, error)) {
1991 return false;
1992 }
1993 } else if (HasAttribute(line, kAttributeIceUfrag)) {
1994 if (!GetValue(line, kAttributeIceUfrag,
1995 &(session_td->ice_ufrag), error)) {
1996 return false;
1997 }
1998 } else if (HasAttribute(line, kAttributeIcePwd)) {
1999 if (!GetValue(line, kAttributeIcePwd, &(session_td->ice_pwd), error)) {
2000 return false;
2001 }
2002 } else if (HasAttribute(line, kAttributeIceLite)) {
2003 session_td->ice_mode = cricket::ICEMODE_LITE;
2004 } else if (HasAttribute(line, kAttributeIceOption)) {
2005 if (!ParseIceOptions(line, &(session_td->transport_options), error)) {
2006 return false;
2007 }
2008 } else if (HasAttribute(line, kAttributeFingerprint)) {
2009 if (session_td->identity_fingerprint.get()) {
2010 return ParseFailed(
2011 line,
2012 "Can't have multiple fingerprint attributes at the same level.",
2013 error);
2014 }
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002015 rtc::SSLFingerprint* fingerprint = NULL;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002016 if (!ParseFingerprintAttribute(line, &fingerprint, error)) {
2017 return false;
2018 }
2019 session_td->identity_fingerprint.reset(fingerprint);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00002020 } else if (HasAttribute(line, kAttributeSetup)) {
2021 if (!ParseDtlsSetup(line, &(session_td->connection_role), error)) {
2022 return false;
2023 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002024 } else if (HasAttribute(line, kAttributeMsidSemantics)) {
2025 std::string semantics;
2026 if (!GetValue(line, kAttributeMsidSemantics, &semantics, error)) {
2027 return false;
2028 }
deadbeefc80741f2015-10-22 13:14:45 -07002029 desc->set_msid_supported(
2030 CaseInsensitiveFind(semantics, kMediaStreamSemantic));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002031 } else if (HasAttribute(line, kAttributeExtmap)) {
isheriff6f8d6862016-05-26 11:24:55 -07002032 RtpExtension extmap;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002033 if (!ParseExtmap(line, &extmap, error)) {
2034 return false;
2035 }
2036 session_extmaps->push_back(extmap);
2037 }
2038 }
2039
2040 return true;
2041}
2042
2043bool ParseGroupAttribute(const std::string& line,
2044 cricket::SessionDescription* desc,
2045 SdpParseError* error) {
nisseede5da42017-01-12 05:15:36 -08002046 RTC_DCHECK(desc != NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002047
2048 // RFC 5888 and draft-holmberg-mmusic-sdp-bundle-negotiation-00
2049 // a=group:BUNDLE video voice
2050 std::vector<std::string> fields;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002051 rtc::split(line.substr(kLinePrefixLength),
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002052 kSdpDelimiterSpace, &fields);
2053 std::string semantics;
2054 if (!GetValue(fields[0], kAttributeGroup, &semantics, error)) {
2055 return false;
2056 }
2057 cricket::ContentGroup group(semantics);
2058 for (size_t i = 1; i < fields.size(); ++i) {
2059 group.AddContentName(fields[i]);
2060 }
2061 desc->AddGroup(group);
2062 return true;
2063}
2064
2065static bool ParseFingerprintAttribute(const std::string& line,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002066 rtc::SSLFingerprint** fingerprint,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002067 SdpParseError* error) {
2068 if (!IsLineType(line, kLineTypeAttributes) ||
2069 !HasAttribute(line, kAttributeFingerprint)) {
2070 return ParseFailedExpectLine(line, 0, kLineTypeAttributes,
2071 kAttributeFingerprint, error);
2072 }
2073
2074 std::vector<std::string> fields;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002075 rtc::split(line.substr(kLinePrefixLength),
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002076 kSdpDelimiterSpace, &fields);
2077 const size_t expected_fields = 2;
2078 if (fields.size() != expected_fields) {
2079 return ParseFailedExpectFieldNum(line, expected_fields, error);
2080 }
2081
2082 // The first field here is "fingerprint:<hash>.
2083 std::string algorithm;
2084 if (!GetValue(fields[0], kAttributeFingerprint, &algorithm, error)) {
2085 return false;
2086 }
2087
2088 // Downcase the algorithm. Note that we don't need to downcase the
2089 // fingerprint because hex_decode can handle upper-case.
2090 std::transform(algorithm.begin(), algorithm.end(), algorithm.begin(),
2091 ::tolower);
2092
2093 // The second field is the digest value. De-hexify it.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002094 *fingerprint = rtc::SSLFingerprint::CreateFromRfc4572(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002095 algorithm, fields[1]);
2096 if (!*fingerprint) {
2097 return ParseFailed(line,
2098 "Failed to create fingerprint from the digest.",
2099 error);
2100 }
2101
2102 return true;
2103}
2104
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00002105static bool ParseDtlsSetup(const std::string& line,
2106 cricket::ConnectionRole* role,
2107 SdpParseError* error) {
2108 // setup-attr = "a=setup:" role
2109 // role = "active" / "passive" / "actpass" / "holdconn"
2110 std::vector<std::string> fields;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002111 rtc::split(line.substr(kLinePrefixLength), kSdpDelimiterColon, &fields);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00002112 const size_t expected_fields = 2;
2113 if (fields.size() != expected_fields) {
2114 return ParseFailedExpectFieldNum(line, expected_fields, error);
2115 }
2116 std::string role_str = fields[1];
2117 if (!cricket::StringToConnectionRole(role_str, role)) {
2118 return ParseFailed(line, "Invalid attribute value.", error);
2119 }
2120 return true;
2121}
2122
deadbeef9d3584c2016-02-16 17:54:10 -08002123static bool ParseMsidAttribute(const std::string& line,
2124 std::string* stream_id,
2125 std::string* track_id,
2126 SdpParseError* error) {
2127 // draft-ietf-mmusic-msid-11
2128 // a=msid:<stream id> <track id>
2129 // msid-value = msid-id [ SP msid-appdata ]
2130 // msid-id = 1*64token-char ; see RFC 4566
2131 // msid-appdata = 1*64token-char ; see RFC 4566
2132 std::string field1;
2133 if (!rtc::tokenize_first(line.substr(kLinePrefixLength), kSdpDelimiterSpace,
2134 &field1, track_id)) {
2135 const size_t expected_fields = 2;
2136 return ParseFailedExpectFieldNum(line, expected_fields, error);
2137 }
2138
2139 // msid:<msid-id>
2140 if (!GetValue(field1, kAttributeMsid, stream_id, error)) {
2141 return false;
2142 }
2143 return true;
2144}
2145
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002146// RFC 3551
2147// PT encoding media type clock rate channels
2148// name (Hz)
2149// 0 PCMU A 8,000 1
2150// 1 reserved A
2151// 2 reserved A
2152// 3 GSM A 8,000 1
2153// 4 G723 A 8,000 1
2154// 5 DVI4 A 8,000 1
2155// 6 DVI4 A 16,000 1
2156// 7 LPC A 8,000 1
2157// 8 PCMA A 8,000 1
2158// 9 G722 A 8,000 1
2159// 10 L16 A 44,100 2
2160// 11 L16 A 44,100 1
2161// 12 QCELP A 8,000 1
2162// 13 CN A 8,000 1
2163// 14 MPA A 90,000 (see text)
2164// 15 G728 A 8,000 1
2165// 16 DVI4 A 11,025 1
2166// 17 DVI4 A 22,050 1
2167// 18 G729 A 8,000 1
2168struct StaticPayloadAudioCodec {
2169 const char* name;
2170 int clockrate;
Peter Kasting69558702016-01-12 16:26:35 -08002171 size_t channels;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002172};
2173static const StaticPayloadAudioCodec kStaticPayloadAudioCodecs[] = {
2174 { "PCMU", 8000, 1 },
2175 { "reserved", 0, 0 },
2176 { "reserved", 0, 0 },
2177 { "GSM", 8000, 1 },
2178 { "G723", 8000, 1 },
2179 { "DVI4", 8000, 1 },
2180 { "DVI4", 16000, 1 },
2181 { "LPC", 8000, 1 },
2182 { "PCMA", 8000, 1 },
2183 { "G722", 8000, 1 },
2184 { "L16", 44100, 2 },
2185 { "L16", 44100, 1 },
2186 { "QCELP", 8000, 1 },
2187 { "CN", 8000, 1 },
2188 { "MPA", 90000, 1 },
2189 { "G728", 8000, 1 },
2190 { "DVI4", 11025, 1 },
2191 { "DVI4", 22050, 1 },
2192 { "G729", 8000, 1 },
2193};
2194
2195void MaybeCreateStaticPayloadAudioCodecs(
2196 const std::vector<int>& fmts, AudioContentDescription* media_desc) {
2197 if (!media_desc) {
2198 return;
2199 }
deadbeef67cf2c12016-04-13 10:07:16 -07002200 RTC_DCHECK(media_desc->codecs().empty());
solenberg9fa49752016-10-08 13:02:44 -07002201 for (int payload_type : fmts) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002202 if (!media_desc->HasCodec(payload_type) &&
2203 payload_type >= 0 &&
kjellander3e33bfe2016-06-20 07:04:09 -07002204 static_cast<uint32_t>(payload_type) <
2205 arraysize(kStaticPayloadAudioCodecs)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002206 std::string encoding_name = kStaticPayloadAudioCodecs[payload_type].name;
2207 int clock_rate = kStaticPayloadAudioCodecs[payload_type].clockrate;
Peter Kasting69558702016-01-12 16:26:35 -08002208 size_t channels = kStaticPayloadAudioCodecs[payload_type].channels;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002209 media_desc->AddCodec(cricket::AudioCodec(payload_type, encoding_name,
deadbeef67cf2c12016-04-13 10:07:16 -07002210 clock_rate, 0, channels));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002211 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002212 }
2213}
2214
2215template <class C>
2216static C* ParseContentDescription(const std::string& message,
2217 const MediaType media_type,
2218 int mline_index,
2219 const std::string& protocol,
deadbeef67cf2c12016-04-13 10:07:16 -07002220 const std::vector<int>& payload_types,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002221 size_t* pos,
2222 std::string* content_name,
deadbeef25ed4352016-12-12 18:37:36 -08002223 bool* bundle_only,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002224 TransportDescription* transport,
2225 std::vector<JsepIceCandidate*>* candidates,
2226 webrtc::SdpParseError* error) {
2227 C* media_desc = new C();
2228 switch (media_type) {
2229 case cricket::MEDIA_TYPE_AUDIO:
2230 *content_name = cricket::CN_AUDIO;
2231 break;
2232 case cricket::MEDIA_TYPE_VIDEO:
2233 *content_name = cricket::CN_VIDEO;
2234 break;
2235 case cricket::MEDIA_TYPE_DATA:
2236 *content_name = cricket::CN_DATA;
2237 break;
2238 default:
nissec80e7412017-01-11 05:56:46 -08002239 RTC_NOTREACHED();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002240 break;
2241 }
deadbeef67cf2c12016-04-13 10:07:16 -07002242 if (!ParseContent(message, media_type, mline_index, protocol, payload_types,
deadbeef25ed4352016-12-12 18:37:36 -08002243 pos, content_name, bundle_only, media_desc, transport,
2244 candidates, error)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002245 delete media_desc;
2246 return NULL;
2247 }
2248 // Sort the codecs according to the m-line fmt list.
deadbeef67cf2c12016-04-13 10:07:16 -07002249 std::unordered_map<int, int> payload_type_preferences;
2250 // "size + 1" so that the lowest preference payload type has a preference of
2251 // 1, which is greater than the default (0) for payload types not in the fmt
2252 // list.
2253 int preference = static_cast<int>(payload_types.size() + 1);
2254 for (int pt : payload_types) {
2255 payload_type_preferences[pt] = preference--;
2256 }
2257 std::vector<typename C::CodecType> codecs = media_desc->codecs();
2258 std::sort(codecs.begin(), codecs.end(), [&payload_type_preferences](
2259 const typename C::CodecType& a,
2260 const typename C::CodecType& b) {
2261 return payload_type_preferences[a.id] > payload_type_preferences[b.id];
2262 });
2263 media_desc->set_codecs(codecs);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002264 return media_desc;
2265}
2266
2267bool ParseMediaDescription(const std::string& message,
2268 const TransportDescription& session_td,
2269 const RtpHeaderExtensions& session_extmaps,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002270 size_t* pos,
2271 cricket::SessionDescription* desc,
2272 std::vector<JsepIceCandidate*>* candidates,
2273 SdpParseError* error) {
nisseede5da42017-01-12 05:15:36 -08002274 RTC_DCHECK(desc != NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002275 std::string line;
2276 int mline_index = -1;
2277
2278 // Zero or more media descriptions
2279 // RFC 4566
2280 // m=<media> <port> <proto> <fmt>
2281 while (GetLineWithType(message, pos, &line, kLineTypeMedia)) {
2282 ++mline_index;
2283
2284 std::vector<std::string> fields;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002285 rtc::split(line.substr(kLinePrefixLength),
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002286 kSdpDelimiterSpace, &fields);
2287 const size_t expected_min_fields = 4;
2288 if (fields.size() < expected_min_fields) {
2289 return ParseFailedExpectMinFieldNum(line, expected_min_fields, error);
2290 }
deadbeef25ed4352016-12-12 18:37:36 -08002291 bool port_rejected = false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002292 // RFC 3264
2293 // To reject an offered stream, the port number in the corresponding stream
2294 // in the answer MUST be set to zero.
2295 if (fields[1] == kMediaPortRejected) {
deadbeef25ed4352016-12-12 18:37:36 -08002296 port_rejected = true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002297 }
2298
2299 std::string protocol = fields[2];
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002300
2301 // <fmt>
deadbeef67cf2c12016-04-13 10:07:16 -07002302 std::vector<int> payload_types;
pthatcher@webrtc.org3341b402015-02-13 21:14:22 +00002303 if (IsRtp(protocol)) {
jiayl@webrtc.orgddb85ab2014-09-05 16:31:56 +00002304 for (size_t j = 3 ; j < fields.size(); ++j) {
2305 // TODO(wu): Remove when below bug is fixed.
2306 // https://bugzilla.mozilla.org/show_bug.cgi?id=996329
pbosbb36fdf2015-07-09 07:48:14 -07002307 if (fields[j].empty() && j == fields.size() - 1) {
jiayl@webrtc.orgddb85ab2014-09-05 16:31:56 +00002308 continue;
2309 }
wu@webrtc.org36eda7c2014-04-15 20:37:30 +00002310
jiayl@webrtc.orgddb85ab2014-09-05 16:31:56 +00002311 int pl = 0;
pkasting@chromium.orge9facf82015-02-17 20:36:28 +00002312 if (!GetPayloadTypeFromString(line, fields[j], &pl, error)) {
jiayl@webrtc.orgddb85ab2014-09-05 16:31:56 +00002313 return false;
2314 }
deadbeef67cf2c12016-04-13 10:07:16 -07002315 payload_types.push_back(pl);
wu@webrtc.org5e760e72014-04-02 23:19:09 +00002316 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002317 }
2318
2319 // Make a temporary TransportDescription based on |session_td|.
2320 // Some of this gets overwritten by ParseContent.
deadbeef46eed762016-01-28 13:24:37 -08002321 TransportDescription transport(
2322 session_td.transport_options, session_td.ice_ufrag, session_td.ice_pwd,
2323 session_td.ice_mode, session_td.connection_role,
2324 session_td.identity_fingerprint.get());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002325
kwibergd1fe2812016-04-27 06:47:29 -07002326 std::unique_ptr<MediaContentDescription> content;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002327 std::string content_name;
deadbeef25ed4352016-12-12 18:37:36 -08002328 bool bundle_only = false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002329 if (HasAttribute(line, kMediaTypeVideo)) {
2330 content.reset(ParseContentDescription<VideoContentDescription>(
deadbeef67cf2c12016-04-13 10:07:16 -07002331 message, cricket::MEDIA_TYPE_VIDEO, mline_index, protocol,
deadbeef25ed4352016-12-12 18:37:36 -08002332 payload_types, pos, &content_name, &bundle_only, &transport,
2333 candidates, error));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002334 } else if (HasAttribute(line, kMediaTypeAudio)) {
2335 content.reset(ParseContentDescription<AudioContentDescription>(
deadbeef67cf2c12016-04-13 10:07:16 -07002336 message, cricket::MEDIA_TYPE_AUDIO, mline_index, protocol,
deadbeef25ed4352016-12-12 18:37:36 -08002337 payload_types, pos, &content_name, &bundle_only, &transport,
2338 candidates, error));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002339 } else if (HasAttribute(line, kMediaTypeData)) {
jiayl@webrtc.org0e527722014-09-08 21:43:43 +00002340 DataContentDescription* data_desc =
wu@webrtc.org78187522013-10-07 23:32:02 +00002341 ParseContentDescription<DataContentDescription>(
deadbeef67cf2c12016-04-13 10:07:16 -07002342 message, cricket::MEDIA_TYPE_DATA, mline_index, protocol,
deadbeef25ed4352016-12-12 18:37:36 -08002343 payload_types, pos, &content_name, &bundle_only, &transport,
2344 candidates, error);
jiayl@webrtc.org0e527722014-09-08 21:43:43 +00002345 content.reset(data_desc);
wu@webrtc.org78187522013-10-07 23:32:02 +00002346
jiayl@webrtc.orgddb85ab2014-09-05 16:31:56 +00002347 int p;
pthatcher@webrtc.org3341b402015-02-13 21:14:22 +00002348 if (data_desc && IsDtlsSctp(protocol) && rtc::FromString(fields[3], &p)) {
jiayl@webrtc.org0e527722014-09-08 21:43:43 +00002349 if (!AddSctpDataCodec(data_desc, p))
jiayl@webrtc.orgddb85ab2014-09-05 16:31:56 +00002350 return false;
wu@webrtc.org78187522013-10-07 23:32:02 +00002351 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002352 } else {
2353 LOG(LS_WARNING) << "Unsupported media type: " << line;
2354 continue;
2355 }
2356 if (!content.get()) {
2357 // ParseContentDescription returns NULL if failed.
2358 return false;
2359 }
2360
deadbeef25ed4352016-12-12 18:37:36 -08002361 bool content_rejected = false;
deadbeef12771a12017-01-03 13:53:47 -08002362 // A port of 0 is not interpreted as a rejected m= section when it's
2363 // used along with a=bundle-only.
deadbeef25ed4352016-12-12 18:37:36 -08002364 if (bundle_only) {
deadbeef25ed4352016-12-12 18:37:36 -08002365 if (!port_rejected) {
deadbeef12771a12017-01-03 13:53:47 -08002366 // Usage of bundle-only with a nonzero port is unspecified. So just
2367 // ignore bundle-only if we see this.
2368 bundle_only = false;
2369 LOG(LS_WARNING)
2370 << "a=bundle-only attribute observed with a nonzero "
2371 << "port; this usage is unspecified so the attribute is being "
2372 << "ignored.";
deadbeef25ed4352016-12-12 18:37:36 -08002373 }
2374 } else {
2375 // If not using bundle-only, interpret port 0 in the normal way; the m=
2376 // section is being rejected.
2377 content_rejected = port_rejected;
2378 }
2379
pthatcher@webrtc.org3341b402015-02-13 21:14:22 +00002380 if (IsRtp(protocol)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002381 // Set the extmap.
2382 if (!session_extmaps.empty() &&
2383 !content->rtp_header_extensions().empty()) {
2384 return ParseFailed("",
2385 "The a=extmap MUST be either all session level or "
2386 "all media level.",
2387 error);
2388 }
2389 for (size_t i = 0; i < session_extmaps.size(); ++i) {
2390 content->AddRtpHeaderExtension(session_extmaps[i]);
2391 }
2392 }
2393 content->set_protocol(protocol);
2394 desc->AddContent(content_name,
deadbeef25ed4352016-12-12 18:37:36 -08002395 IsDtlsSctp(protocol) ? cricket::NS_JINGLE_DRAFT_SCTP
2396 : cricket::NS_JINGLE_RTP,
2397 content_rejected, bundle_only, content.release());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002398 // Create TransportInfo with the media level "ice-pwd" and "ice-ufrag".
2399 TransportInfo transport_info(content_name, transport);
2400
2401 if (!desc->AddTransportInfo(transport_info)) {
2402 std::ostringstream description;
2403 description << "Failed to AddTransportInfo with content name: "
2404 << content_name;
2405 return ParseFailed("", description.str(), error);
2406 }
2407 }
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00002408
2409 size_t end_of_message = message.size();
2410 if (mline_index == -1 && *pos != end_of_message) {
2411 ParseFailed(message, *pos, "Expects m line.", error);
2412 return false;
2413 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002414 return true;
2415}
2416
2417bool VerifyCodec(const cricket::Codec& codec) {
2418 // Codec has not been populated correctly unless the name has been set. This
2419 // can happen if an SDP has an fmtp or rtcp-fb with a payload type but doesn't
2420 // have a corresponding "rtpmap" line.
htab39db842016-12-08 01:50:48 -08002421 return !codec.name.empty();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002422}
2423
2424bool VerifyAudioCodecs(const AudioContentDescription* audio_desc) {
2425 const std::vector<cricket::AudioCodec>& codecs = audio_desc->codecs();
2426 for (std::vector<cricket::AudioCodec>::const_iterator iter = codecs.begin();
2427 iter != codecs.end(); ++iter) {
2428 if (!VerifyCodec(*iter)) {
2429 return false;
2430 }
2431 }
2432 return true;
2433}
2434
2435bool VerifyVideoCodecs(const VideoContentDescription* video_desc) {
2436 const std::vector<cricket::VideoCodec>& codecs = video_desc->codecs();
2437 for (std::vector<cricket::VideoCodec>::const_iterator iter = codecs.begin();
2438 iter != codecs.end(); ++iter) {
2439 if (!VerifyCodec(*iter)) {
2440 return false;
2441 }
2442 }
2443 return true;
2444}
2445
2446void AddParameters(const cricket::CodecParameterMap& parameters,
2447 cricket::Codec* codec) {
2448 for (cricket::CodecParameterMap::const_iterator iter =
2449 parameters.begin(); iter != parameters.end(); ++iter) {
2450 codec->SetParam(iter->first, iter->second);
2451 }
2452}
2453
2454void AddFeedbackParameter(const cricket::FeedbackParam& feedback_param,
2455 cricket::Codec* codec) {
2456 codec->AddFeedbackParam(feedback_param);
2457}
2458
2459void AddFeedbackParameters(const cricket::FeedbackParams& feedback_params,
2460 cricket::Codec* codec) {
2461 for (std::vector<cricket::FeedbackParam>::const_iterator iter =
2462 feedback_params.params().begin();
2463 iter != feedback_params.params().end(); ++iter) {
2464 codec->AddFeedbackParam(*iter);
2465 }
2466}
2467
2468// Gets the current codec setting associated with |payload_type|. If there
changbin.shao@webrtc.org2d25b442015-03-16 04:14:34 +00002469// is no Codec associated with that payload type it returns an empty codec
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002470// with that payload type.
2471template <class T>
changbin.shao@webrtc.org2d25b442015-03-16 04:14:34 +00002472T GetCodecWithPayloadType(const std::vector<T>& codecs, int payload_type) {
magjedb05fa242016-11-11 04:00:16 -08002473 const T* codec = FindCodecById(codecs, payload_type);
2474 if (codec)
2475 return *codec;
2476 // Return empty codec with |payload_type|.
changbin.shao@webrtc.org2d25b442015-03-16 04:14:34 +00002477 T ret_val;
magjedb05fa242016-11-11 04:00:16 -08002478 ret_val.id = payload_type;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002479 return ret_val;
2480}
2481
2482// Updates or creates a new codec entry in the audio description.
2483template <class T, class U>
2484void AddOrReplaceCodec(MediaContentDescription* content_desc, const U& codec) {
2485 T* desc = static_cast<T*>(content_desc);
2486 std::vector<U> codecs = desc->codecs();
2487 bool found = false;
2488
2489 typename std::vector<U>::iterator iter;
2490 for (iter = codecs.begin(); iter != codecs.end(); ++iter) {
2491 if (iter->id == codec.id) {
2492 *iter = codec;
2493 found = true;
2494 break;
2495 }
2496 }
2497 if (!found) {
2498 desc->AddCodec(codec);
2499 return;
2500 }
2501 desc->set_codecs(codecs);
2502}
2503
2504// Adds or updates existing codec corresponding to |payload_type| according
2505// to |parameters|.
2506template <class T, class U>
2507void UpdateCodec(MediaContentDescription* content_desc, int payload_type,
2508 const cricket::CodecParameterMap& parameters) {
2509 // Codec might already have been populated (from rtpmap).
changbin.shao@webrtc.org2d25b442015-03-16 04:14:34 +00002510 U new_codec = GetCodecWithPayloadType(static_cast<T*>(content_desc)->codecs(),
2511 payload_type);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002512 AddParameters(parameters, &new_codec);
2513 AddOrReplaceCodec<T, U>(content_desc, new_codec);
2514}
2515
2516// Adds or updates existing codec corresponding to |payload_type| according
2517// to |feedback_param|.
2518template <class T, class U>
2519void UpdateCodec(MediaContentDescription* content_desc, int payload_type,
2520 const cricket::FeedbackParam& feedback_param) {
2521 // Codec might already have been populated (from rtpmap).
changbin.shao@webrtc.org2d25b442015-03-16 04:14:34 +00002522 U new_codec = GetCodecWithPayloadType(static_cast<T*>(content_desc)->codecs(),
2523 payload_type);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002524 AddFeedbackParameter(feedback_param, &new_codec);
2525 AddOrReplaceCodec<T, U>(content_desc, new_codec);
2526}
2527
jlmiller@webrtc.orga744a282015-02-18 21:37:46 +00002528template <class T>
2529bool PopWildcardCodec(std::vector<T>* codecs, T* wildcard_codec) {
2530 for (auto iter = codecs->begin(); iter != codecs->end(); ++iter) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002531 if (iter->id == kWildcardPayloadType) {
2532 *wildcard_codec = *iter;
2533 codecs->erase(iter);
2534 return true;
2535 }
2536 }
2537 return false;
2538}
2539
jlmiller@webrtc.orga744a282015-02-18 21:37:46 +00002540template<class T>
2541void UpdateFromWildcardCodecs(cricket::MediaContentDescriptionImpl<T>* desc) {
2542 auto codecs = desc->codecs();
2543 T wildcard_codec;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002544 if (!PopWildcardCodec(&codecs, &wildcard_codec)) {
2545 return;
2546 }
jlmiller@webrtc.orga744a282015-02-18 21:37:46 +00002547 for (auto& codec : codecs) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002548 AddFeedbackParameters(wildcard_codec.feedback_params, &codec);
2549 }
jlmiller@webrtc.orga744a282015-02-18 21:37:46 +00002550 desc->set_codecs(codecs);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002551}
2552
2553void AddAudioAttribute(const std::string& name, const std::string& value,
2554 AudioContentDescription* audio_desc) {
2555 if (value.empty()) {
2556 return;
2557 }
2558 std::vector<cricket::AudioCodec> codecs = audio_desc->codecs();
2559 for (std::vector<cricket::AudioCodec>::iterator iter = codecs.begin();
2560 iter != codecs.end(); ++iter) {
2561 iter->params[name] = value;
2562 }
2563 audio_desc->set_codecs(codecs);
2564}
2565
2566bool ParseContent(const std::string& message,
2567 const MediaType media_type,
2568 int mline_index,
2569 const std::string& protocol,
deadbeef67cf2c12016-04-13 10:07:16 -07002570 const std::vector<int>& payload_types,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002571 size_t* pos,
2572 std::string* content_name,
deadbeef25ed4352016-12-12 18:37:36 -08002573 bool* bundle_only,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002574 MediaContentDescription* media_desc,
2575 TransportDescription* transport,
2576 std::vector<JsepIceCandidate*>* candidates,
2577 SdpParseError* error) {
nisseede5da42017-01-12 05:15:36 -08002578 RTC_DCHECK(media_desc != NULL);
2579 RTC_DCHECK(content_name != NULL);
2580 RTC_DCHECK(transport != NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002581
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +00002582 if (media_type == cricket::MEDIA_TYPE_AUDIO) {
2583 MaybeCreateStaticPayloadAudioCodecs(
deadbeef67cf2c12016-04-13 10:07:16 -07002584 payload_types, static_cast<AudioContentDescription*>(media_desc));
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +00002585 }
2586
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002587 // The media level "ice-ufrag" and "ice-pwd".
2588 // The candidates before update the media level "ice-pwd" and "ice-ufrag".
2589 Candidates candidates_orig;
2590 std::string line;
2591 std::string mline_id;
2592 // Tracks created out of the ssrc attributes.
2593 StreamParamsVec tracks;
2594 SsrcInfoVec ssrc_infos;
2595 SsrcGroupVec ssrc_groups;
2596 std::string maxptime_as_string;
2597 std::string ptime_as_string;
deadbeef9d3584c2016-02-16 17:54:10 -08002598 std::string stream_id;
2599 std::string track_id;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002600
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002601 // Loop until the next m line
2602 while (!IsLineType(message, kLineTypeMedia, *pos)) {
2603 if (!GetLine(message, pos, &line)) {
2604 if (*pos >= message.size()) {
2605 break; // Done parsing
2606 } else {
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +00002607 return ParseFailed(message, *pos, "Invalid SDP line.", error);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002608 }
2609 }
2610
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002611 // RFC 4566
2612 // b=* (zero or more bandwidth information lines)
2613 if (IsLineType(line, kLineTypeSessionBandwidth)) {
2614 std::string bandwidth;
2615 if (HasAttribute(line, kApplicationSpecificMaximum)) {
2616 if (!GetValue(line, kApplicationSpecificMaximum, &bandwidth, error)) {
2617 return false;
2618 } else {
wu@webrtc.org5e760e72014-04-02 23:19:09 +00002619 int b = 0;
2620 if (!GetValueFromString(line, bandwidth, &b, error)) {
2621 return false;
2622 }
Peter Thatcherc0c3a862015-06-24 15:31:25 -07002623 // We should never use more than the default bandwidth for RTP-based
2624 // data channels. Don't allow SDP to set the bandwidth, because
2625 // that would give JS the opportunity to "break the Internet".
2626 // See: https://code.google.com/p/chromium/issues/detail?id=280726
2627 if (media_type == cricket::MEDIA_TYPE_DATA && IsRtp(protocol) &&
2628 b > cricket::kDataMaxBandwidth / 1000) {
2629 std::ostringstream description;
2630 description << "RTP-based data channels may not send more than "
2631 << cricket::kDataMaxBandwidth / 1000 << "kbps.";
2632 return ParseFailed(line, description.str(), error);
2633 }
deadbeefb2362572016-12-13 16:37:06 -08002634 // Prevent integer overflow.
2635 b = std::min(b, INT_MAX / 1000);
wu@webrtc.org5e760e72014-04-02 23:19:09 +00002636 media_desc->set_bandwidth(b * 1000);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002637 }
2638 }
2639 continue;
2640 }
2641
2642 if (!IsLineType(line, kLineTypeAttributes)) {
2643 // TODO: Handle other lines if needed.
2644 LOG(LS_INFO) << "Ignored line: " << line;
2645 continue;
2646 }
2647
2648 // Handle attributes common to SCTP and RTP.
2649 if (HasAttribute(line, kAttributeMid)) {
2650 // RFC 3388
2651 // mid-attribute = "a=mid:" identification-tag
2652 // identification-tag = token
2653 // Use the mid identification-tag as the content name.
2654 if (!GetValue(line, kAttributeMid, &mline_id, error)) {
2655 return false;
2656 }
2657 *content_name = mline_id;
deadbeef25ed4352016-12-12 18:37:36 -08002658 } else if (HasAttribute(line, kAttributeBundleOnly)) {
2659 *bundle_only = true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002660 } else if (HasAttribute(line, kAttributeCandidate)) {
2661 Candidate candidate;
2662 if (!ParseCandidate(line, &candidate, error, false)) {
2663 return false;
2664 }
deadbeef7bcdb692017-01-20 12:43:58 -08002665 // ParseCandidate will parse non-standard ufrag and password attributes,
2666 // since it's used for candidate trickling, but we only want to process
2667 // the "a=ice-ufrag"/"a=ice-pwd" values in a session description, so
2668 // strip them off at this point.
2669 candidate.set_username(std::string());
2670 candidate.set_password(std::string());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002671 candidates_orig.push_back(candidate);
2672 } else if (HasAttribute(line, kAttributeIceUfrag)) {
2673 if (!GetValue(line, kAttributeIceUfrag, &transport->ice_ufrag, error)) {
2674 return false;
2675 }
2676 } else if (HasAttribute(line, kAttributeIcePwd)) {
2677 if (!GetValue(line, kAttributeIcePwd, &transport->ice_pwd, error)) {
2678 return false;
2679 }
2680 } else if (HasAttribute(line, kAttributeIceOption)) {
2681 if (!ParseIceOptions(line, &transport->transport_options, error)) {
2682 return false;
2683 }
2684 } else if (HasAttribute(line, kAttributeFmtp)) {
2685 if (!ParseFmtpAttributes(line, media_type, media_desc, error)) {
2686 return false;
2687 }
2688 } else if (HasAttribute(line, kAttributeFingerprint)) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002689 rtc::SSLFingerprint* fingerprint = NULL;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002690
2691 if (!ParseFingerprintAttribute(line, &fingerprint, error)) {
2692 return false;
2693 }
2694 transport->identity_fingerprint.reset(fingerprint);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00002695 } else if (HasAttribute(line, kAttributeSetup)) {
2696 if (!ParseDtlsSetup(line, &(transport->connection_role), error)) {
2697 return false;
2698 }
pthatcher@webrtc.org3341b402015-02-13 21:14:22 +00002699 } else if (IsDtlsSctp(protocol) && HasAttribute(line, kAttributeSctpPort)) {
deadbeef7e146cb2016-09-28 10:04:34 -07002700 if (media_type != cricket::MEDIA_TYPE_DATA) {
2701 return ParseFailed(
2702 line, "sctp-port attribute found in non-data media description.",
2703 error);
2704 }
jiayl@webrtc.orgddb85ab2014-09-05 16:31:56 +00002705 int sctp_port;
2706 if (!ParseSctpPort(line, &sctp_port, error)) {
2707 return false;
2708 }
2709 if (!AddSctpDataCodec(static_cast<DataContentDescription*>(media_desc),
2710 sctp_port)) {
2711 return false;
2712 }
pthatcher@webrtc.org3341b402015-02-13 21:14:22 +00002713 } else if (IsRtp(protocol)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002714 //
2715 // RTP specific attrubtes
2716 //
2717 if (HasAttribute(line, kAttributeRtcpMux)) {
2718 media_desc->set_rtcp_mux(true);
deadbeef13871492015-12-09 12:37:51 -08002719 } else if (HasAttribute(line, kAttributeRtcpReducedSize)) {
2720 media_desc->set_rtcp_reduced_size(true);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002721 } else if (HasAttribute(line, kAttributeSsrcGroup)) {
2722 if (!ParseSsrcGroupAttribute(line, &ssrc_groups, error)) {
2723 return false;
2724 }
2725 } else if (HasAttribute(line, kAttributeSsrc)) {
2726 if (!ParseSsrcAttribute(line, &ssrc_infos, error)) {
2727 return false;
2728 }
2729 } else if (HasAttribute(line, kAttributeCrypto)) {
2730 if (!ParseCryptoAttribute(line, media_desc, error)) {
2731 return false;
2732 }
2733 } else if (HasAttribute(line, kAttributeRtpmap)) {
deadbeef67cf2c12016-04-13 10:07:16 -07002734 if (!ParseRtpmapAttribute(line, media_type, payload_types, media_desc,
2735 error)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002736 return false;
2737 }
2738 } else if (HasAttribute(line, kCodecParamMaxPTime)) {
2739 if (!GetValue(line, kCodecParamMaxPTime, &maxptime_as_string, error)) {
2740 return false;
2741 }
2742 } else if (HasAttribute(line, kAttributeRtcpFb)) {
2743 if (!ParseRtcpFbAttribute(line, media_type, media_desc, error)) {
2744 return false;
2745 }
2746 } else if (HasAttribute(line, kCodecParamPTime)) {
2747 if (!GetValue(line, kCodecParamPTime, &ptime_as_string, error)) {
2748 return false;
2749 }
2750 } else if (HasAttribute(line, kAttributeSendOnly)) {
2751 media_desc->set_direction(cricket::MD_SENDONLY);
2752 } else if (HasAttribute(line, kAttributeRecvOnly)) {
2753 media_desc->set_direction(cricket::MD_RECVONLY);
2754 } else if (HasAttribute(line, kAttributeInactive)) {
2755 media_desc->set_direction(cricket::MD_INACTIVE);
2756 } else if (HasAttribute(line, kAttributeSendRecv)) {
2757 media_desc->set_direction(cricket::MD_SENDRECV);
2758 } else if (HasAttribute(line, kAttributeExtmap)) {
isheriff6f8d6862016-05-26 11:24:55 -07002759 RtpExtension extmap;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002760 if (!ParseExtmap(line, &extmap, error)) {
2761 return false;
2762 }
2763 media_desc->AddRtpHeaderExtension(extmap);
2764 } else if (HasAttribute(line, kAttributeXGoogleFlag)) {
2765 // Experimental attribute. Conference mode activates more aggressive
2766 // AEC and NS settings.
2767 // TODO: expose API to set these directly.
2768 std::string flag_value;
2769 if (!GetValue(line, kAttributeXGoogleFlag, &flag_value, error)) {
2770 return false;
2771 }
2772 if (flag_value.compare(kValueConference) == 0)
2773 media_desc->set_conference_mode(true);
deadbeef9d3584c2016-02-16 17:54:10 -08002774 } else if (HasAttribute(line, kAttributeMsid)) {
2775 if (!ParseMsidAttribute(line, &stream_id, &track_id, error)) {
2776 return false;
2777 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002778 }
2779 } else {
2780 // Only parse lines that we are interested of.
2781 LOG(LS_INFO) << "Ignored line: " << line;
2782 continue;
2783 }
2784 }
2785
2786 // Create tracks from the |ssrc_infos|.
Taylor Brandstetter5de6b752016-03-09 17:02:30 -08002787 // If the stream_id/track_id for all SSRCS are identical, one StreamParams
2788 // will be created in CreateTracksFromSsrcInfos, containing all the SSRCs from
2789 // the m= section.
2790 CreateTracksFromSsrcInfos(ssrc_infos, stream_id, track_id, &tracks);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002791
2792 // Add the ssrc group to the track.
2793 for (SsrcGroupVec::iterator ssrc_group = ssrc_groups.begin();
2794 ssrc_group != ssrc_groups.end(); ++ssrc_group) {
2795 if (ssrc_group->ssrcs.empty()) {
2796 continue;
2797 }
Peter Boström0c4e06b2015-10-07 12:23:21 +02002798 uint32_t ssrc = ssrc_group->ssrcs.front();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002799 for (StreamParamsVec::iterator track = tracks.begin();
2800 track != tracks.end(); ++track) {
2801 if (track->has_ssrc(ssrc)) {
2802 track->ssrc_groups.push_back(*ssrc_group);
2803 }
2804 }
2805 }
2806
2807 // Add the new tracks to the |media_desc|.
deadbeef9d3584c2016-02-16 17:54:10 -08002808 for (StreamParams& track : tracks) {
2809 media_desc->AddStream(track);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002810 }
2811
2812 if (media_type == cricket::MEDIA_TYPE_AUDIO) {
2813 AudioContentDescription* audio_desc =
2814 static_cast<AudioContentDescription*>(media_desc);
jlmiller@webrtc.orga744a282015-02-18 21:37:46 +00002815 UpdateFromWildcardCodecs(audio_desc);
2816
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002817 // Verify audio codec ensures that no audio codec has been populated with
2818 // only fmtp.
2819 if (!VerifyAudioCodecs(audio_desc)) {
2820 return ParseFailed("Failed to parse audio codecs correctly.", error);
2821 }
2822 AddAudioAttribute(kCodecParamMaxPTime, maxptime_as_string, audio_desc);
2823 AddAudioAttribute(kCodecParamPTime, ptime_as_string, audio_desc);
2824 }
2825
2826 if (media_type == cricket::MEDIA_TYPE_VIDEO) {
jlmiller@webrtc.orga744a282015-02-18 21:37:46 +00002827 VideoContentDescription* video_desc =
2828 static_cast<VideoContentDescription*>(media_desc);
2829 UpdateFromWildcardCodecs(video_desc);
2830 // Verify video codec ensures that no video codec has been populated with
2831 // only rtcp-fb.
2832 if (!VerifyVideoCodecs(video_desc)) {
2833 return ParseFailed("Failed to parse video codecs correctly.", error);
2834 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002835 }
2836
2837 // RFC 5245
2838 // Update the candidates with the media level "ice-pwd" and "ice-ufrag".
2839 for (Candidates::iterator it = candidates_orig.begin();
2840 it != candidates_orig.end(); ++it) {
nisseede5da42017-01-12 05:15:36 -08002841 RTC_DCHECK((*it).username().empty() ||
2842 (*it).username() == transport->ice_ufrag);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002843 (*it).set_username(transport->ice_ufrag);
nisseede5da42017-01-12 05:15:36 -08002844 RTC_DCHECK((*it).password().empty());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002845 (*it).set_password(transport->ice_pwd);
2846 candidates->push_back(
2847 new JsepIceCandidate(mline_id, mline_index, *it));
2848 }
2849 return true;
2850}
2851
2852bool ParseSsrcAttribute(const std::string& line, SsrcInfoVec* ssrc_infos,
2853 SdpParseError* error) {
nisseede5da42017-01-12 05:15:36 -08002854 RTC_DCHECK(ssrc_infos != NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002855 // RFC 5576
2856 // a=ssrc:<ssrc-id> <attribute>
2857 // a=ssrc:<ssrc-id> <attribute>:<value>
2858 std::string field1, field2;
Donald Curtis144d0182015-05-15 13:14:24 -07002859 if (!rtc::tokenize_first(line.substr(kLinePrefixLength), kSdpDelimiterSpace,
2860 &field1, &field2)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002861 const size_t expected_fields = 2;
2862 return ParseFailedExpectFieldNum(line, expected_fields, error);
2863 }
2864
2865 // ssrc:<ssrc-id>
2866 std::string ssrc_id_s;
2867 if (!GetValue(field1, kAttributeSsrc, &ssrc_id_s, error)) {
2868 return false;
2869 }
Peter Boström0c4e06b2015-10-07 12:23:21 +02002870 uint32_t ssrc_id = 0;
wu@webrtc.org5e760e72014-04-02 23:19:09 +00002871 if (!GetValueFromString(line, ssrc_id_s, &ssrc_id, error)) {
2872 return false;
2873 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002874
2875 std::string attribute;
2876 std::string value;
Donald Curtis144d0182015-05-15 13:14:24 -07002877 if (!rtc::tokenize_first(field2, kSdpDelimiterColon, &attribute, &value)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002878 std::ostringstream description;
2879 description << "Failed to get the ssrc attribute value from " << field2
2880 << ". Expected format <attribute>:<value>.";
2881 return ParseFailed(line, description.str(), error);
2882 }
2883
2884 // Check if there's already an item for this |ssrc_id|. Create a new one if
2885 // there isn't.
2886 SsrcInfoVec::iterator ssrc_info = ssrc_infos->begin();
2887 for (; ssrc_info != ssrc_infos->end(); ++ssrc_info) {
2888 if (ssrc_info->ssrc_id == ssrc_id) {
2889 break;
2890 }
2891 }
2892 if (ssrc_info == ssrc_infos->end()) {
2893 SsrcInfo info;
2894 info.ssrc_id = ssrc_id;
2895 ssrc_infos->push_back(info);
2896 ssrc_info = ssrc_infos->end() - 1;
2897 }
2898
2899 // Store the info to the |ssrc_info|.
2900 if (attribute == kSsrcAttributeCname) {
2901 // RFC 5576
2902 // cname:<value>
2903 ssrc_info->cname = value;
2904 } else if (attribute == kSsrcAttributeMsid) {
2905 // draft-alvestrand-mmusic-msid-00
2906 // "msid:" identifier [ " " appdata ]
2907 std::vector<std::string> fields;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002908 rtc::split(value, kSdpDelimiterSpace, &fields);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002909 if (fields.size() < 1 || fields.size() > 2) {
2910 return ParseFailed(line,
2911 "Expected format \"msid:<identifier>[ <appdata>]\".",
2912 error);
2913 }
deadbeef9d3584c2016-02-16 17:54:10 -08002914 ssrc_info->stream_id = fields[0];
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002915 if (fields.size() == 2) {
deadbeef9d3584c2016-02-16 17:54:10 -08002916 ssrc_info->track_id = fields[1];
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002917 }
2918 } else if (attribute == kSsrcAttributeMslabel) {
2919 // draft-alvestrand-rtcweb-mid-01
2920 // mslabel:<value>
2921 ssrc_info->mslabel = value;
2922 } else if (attribute == kSSrcAttributeLabel) {
2923 // The label isn't defined.
2924 // label:<value>
2925 ssrc_info->label = value;
2926 }
2927 return true;
2928}
2929
2930bool ParseSsrcGroupAttribute(const std::string& line,
2931 SsrcGroupVec* ssrc_groups,
2932 SdpParseError* error) {
nisseede5da42017-01-12 05:15:36 -08002933 RTC_DCHECK(ssrc_groups != NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002934 // RFC 5576
2935 // a=ssrc-group:<semantics> <ssrc-id> ...
2936 std::vector<std::string> fields;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002937 rtc::split(line.substr(kLinePrefixLength),
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002938 kSdpDelimiterSpace, &fields);
2939 const size_t expected_min_fields = 2;
2940 if (fields.size() < expected_min_fields) {
2941 return ParseFailedExpectMinFieldNum(line, expected_min_fields, error);
2942 }
2943 std::string semantics;
2944 if (!GetValue(fields[0], kAttributeSsrcGroup, &semantics, error)) {
2945 return false;
2946 }
Peter Boström0c4e06b2015-10-07 12:23:21 +02002947 std::vector<uint32_t> ssrcs;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002948 for (size_t i = 1; i < fields.size(); ++i) {
Peter Boström0c4e06b2015-10-07 12:23:21 +02002949 uint32_t ssrc = 0;
wu@webrtc.org5e760e72014-04-02 23:19:09 +00002950 if (!GetValueFromString(line, fields[i], &ssrc, error)) {
2951 return false;
2952 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002953 ssrcs.push_back(ssrc);
2954 }
2955 ssrc_groups->push_back(SsrcGroup(semantics, ssrcs));
2956 return true;
2957}
2958
2959bool ParseCryptoAttribute(const std::string& line,
2960 MediaContentDescription* media_desc,
2961 SdpParseError* error) {
2962 std::vector<std::string> fields;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002963 rtc::split(line.substr(kLinePrefixLength),
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002964 kSdpDelimiterSpace, &fields);
2965 // RFC 4568
2966 // a=crypto:<tag> <crypto-suite> <key-params> [<session-params>]
2967 const size_t expected_min_fields = 3;
2968 if (fields.size() < expected_min_fields) {
2969 return ParseFailedExpectMinFieldNum(line, expected_min_fields, error);
2970 }
2971 std::string tag_value;
2972 if (!GetValue(fields[0], kAttributeCrypto, &tag_value, error)) {
2973 return false;
2974 }
wu@webrtc.org5e760e72014-04-02 23:19:09 +00002975 int tag = 0;
2976 if (!GetValueFromString(line, tag_value, &tag, error)) {
2977 return false;
2978 }
jbauch083b73f2015-07-16 02:46:32 -07002979 const std::string& crypto_suite = fields[1];
2980 const std::string& key_params = fields[2];
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002981 std::string session_params;
2982 if (fields.size() > 3) {
2983 session_params = fields[3];
2984 }
2985 media_desc->AddCrypto(CryptoParams(tag, crypto_suite, key_params,
2986 session_params));
2987 return true;
2988}
2989
2990// Updates or creates a new codec entry in the audio description with according
deadbeef67cf2c12016-04-13 10:07:16 -07002991// to |name|, |clockrate|, |bitrate|, and |channels|.
2992void UpdateCodec(int payload_type,
2993 const std::string& name,
2994 int clockrate,
2995 int bitrate,
2996 size_t channels,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002997 AudioContentDescription* audio_desc) {
2998 // Codec may already be populated with (only) optional parameters
2999 // (from an fmtp).
changbin.shao@webrtc.org2d25b442015-03-16 04:14:34 +00003000 cricket::AudioCodec codec =
3001 GetCodecWithPayloadType(audio_desc->codecs(), payload_type);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003002 codec.name = name;
3003 codec.clockrate = clockrate;
3004 codec.bitrate = bitrate;
3005 codec.channels = channels;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003006 AddOrReplaceCodec<AudioContentDescription, cricket::AudioCodec>(audio_desc,
3007 codec);
3008}
3009
3010// Updates or creates a new codec entry in the video description according to
deadbeef67cf2c12016-04-13 10:07:16 -07003011// |name|, |width|, |height|, and |framerate|.
3012void UpdateCodec(int payload_type,
3013 const std::string& name,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003014 VideoContentDescription* video_desc) {
3015 // Codec may already be populated with (only) optional parameters
3016 // (from an fmtp).
changbin.shao@webrtc.org2d25b442015-03-16 04:14:34 +00003017 cricket::VideoCodec codec =
3018 GetCodecWithPayloadType(video_desc->codecs(), payload_type);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003019 codec.name = name;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003020 AddOrReplaceCodec<VideoContentDescription, cricket::VideoCodec>(video_desc,
3021 codec);
3022}
3023
3024bool ParseRtpmapAttribute(const std::string& line,
3025 const MediaType media_type,
deadbeef67cf2c12016-04-13 10:07:16 -07003026 const std::vector<int>& payload_types,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003027 MediaContentDescription* media_desc,
3028 SdpParseError* error) {
3029 std::vector<std::string> fields;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00003030 rtc::split(line.substr(kLinePrefixLength),
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003031 kSdpDelimiterSpace, &fields);
3032 // RFC 4566
3033 // a=rtpmap:<payload type> <encoding name>/<clock rate>[/<encodingparameters>]
3034 const size_t expected_min_fields = 2;
3035 if (fields.size() < expected_min_fields) {
3036 return ParseFailedExpectMinFieldNum(line, expected_min_fields, error);
3037 }
3038 std::string payload_type_value;
3039 if (!GetValue(fields[0], kAttributeRtpmap, &payload_type_value, error)) {
3040 return false;
3041 }
wu@webrtc.org5e760e72014-04-02 23:19:09 +00003042 int payload_type = 0;
pkasting@chromium.orge9facf82015-02-17 20:36:28 +00003043 if (!GetPayloadTypeFromString(line, payload_type_value, &payload_type,
3044 error)) {
wu@webrtc.org5e760e72014-04-02 23:19:09 +00003045 return false;
3046 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003047
deadbeef67cf2c12016-04-13 10:07:16 -07003048 if (std::find(payload_types.begin(), payload_types.end(), payload_type) ==
3049 payload_types.end()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003050 LOG(LS_WARNING) << "Ignore rtpmap line that did not appear in the "
3051 << "<fmt> of the m-line: " << line;
3052 return true;
3053 }
jbauch083b73f2015-07-16 02:46:32 -07003054 const std::string& encoder = fields[1];
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003055 std::vector<std::string> codec_params;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00003056 rtc::split(encoder, '/', &codec_params);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003057 // <encoding name>/<clock rate>[/<encodingparameters>]
3058 // 2 mandatory fields
3059 if (codec_params.size() < 2 || codec_params.size() > 3) {
3060 return ParseFailed(line,
3061 "Expected format \"<encoding name>/<clock rate>"
3062 "[/<encodingparameters>]\".",
3063 error);
3064 }
jbauch083b73f2015-07-16 02:46:32 -07003065 const std::string& encoding_name = codec_params[0];
wu@webrtc.org5e760e72014-04-02 23:19:09 +00003066 int clock_rate = 0;
3067 if (!GetValueFromString(line, codec_params[1], &clock_rate, error)) {
3068 return false;
3069 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003070 if (media_type == cricket::MEDIA_TYPE_VIDEO) {
3071 VideoContentDescription* video_desc =
3072 static_cast<VideoContentDescription*>(media_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003073 UpdateCodec(payload_type, encoding_name,
deadbeef67cf2c12016-04-13 10:07:16 -07003074 video_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003075 } else if (media_type == cricket::MEDIA_TYPE_AUDIO) {
3076 // RFC 4566
3077 // For audio streams, <encoding parameters> indicates the number
3078 // of audio channels. This parameter is OPTIONAL and may be
3079 // omitted if the number of channels is one, provided that no
3080 // additional parameters are needed.
Peter Kasting69558702016-01-12 16:26:35 -08003081 size_t channels = 1;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003082 if (codec_params.size() == 3) {
wu@webrtc.org5e760e72014-04-02 23:19:09 +00003083 if (!GetValueFromString(line, codec_params[2], &channels, error)) {
3084 return false;
3085 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003086 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003087 AudioContentDescription* audio_desc =
3088 static_cast<AudioContentDescription*>(media_desc);
ossue1405ad2017-01-23 08:55:48 -08003089 UpdateCodec(payload_type, encoding_name, clock_rate, 0, channels,
deadbeef67cf2c12016-04-13 10:07:16 -07003090 audio_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003091 } else if (media_type == cricket::MEDIA_TYPE_DATA) {
3092 DataContentDescription* data_desc =
3093 static_cast<DataContentDescription*>(media_desc);
deadbeef67cf2c12016-04-13 10:07:16 -07003094 data_desc->AddCodec(cricket::DataCodec(payload_type, encoding_name));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003095 }
3096 return true;
3097}
3098
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003099bool ParseFmtpParam(const std::string& line, std::string* parameter,
3100 std::string* value, SdpParseError* error) {
Donald Curtis0e07f922015-05-15 09:21:23 -07003101 if (!rtc::tokenize_first(line, kSdpDelimiterEqual, parameter, value)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003102 ParseFailed(line, "Unable to parse fmtp parameter. \'=\' missing.", error);
3103 return false;
3104 }
3105 // a=fmtp:<payload_type> <param1>=<value1>; <param2>=<value2>; ...
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003106 return true;
3107}
3108
3109bool ParseFmtpAttributes(const std::string& line, const MediaType media_type,
3110 MediaContentDescription* media_desc,
3111 SdpParseError* error) {
3112 if (media_type != cricket::MEDIA_TYPE_AUDIO &&
3113 media_type != cricket::MEDIA_TYPE_VIDEO) {
3114 return true;
3115 }
Donald Curtis0e07f922015-05-15 09:21:23 -07003116
3117 std::string line_payload;
3118 std::string line_params;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003119
3120 // RFC 5576
3121 // a=fmtp:<format> <format specific parameters>
3122 // At least two fields, whereas the second one is any of the optional
3123 // parameters.
Donald Curtis144d0182015-05-15 13:14:24 -07003124 if (!rtc::tokenize_first(line.substr(kLinePrefixLength), kSdpDelimiterSpace,
3125 &line_payload, &line_params)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003126 ParseFailedExpectMinFieldNum(line, 2, error);
3127 return false;
3128 }
3129
Donald Curtis0e07f922015-05-15 09:21:23 -07003130 // Parse out the payload information.
pkasting@chromium.orgd3245462015-02-23 21:28:22 +00003131 std::string payload_type_str;
Donald Curtis0e07f922015-05-15 09:21:23 -07003132 if (!GetValue(line_payload, kAttributeFmtp, &payload_type_str, error)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003133 return false;
3134 }
3135
Donald Curtis0e07f922015-05-15 09:21:23 -07003136 int payload_type = 0;
Donald Curtis144d0182015-05-15 13:14:24 -07003137 if (!GetPayloadTypeFromString(line_payload, payload_type_str, &payload_type,
3138 error)) {
Donald Curtis0e07f922015-05-15 09:21:23 -07003139 return false;
3140 }
3141
3142 // Parse out format specific parameters.
3143 std::vector<std::string> fields;
3144 rtc::split(line_params, kSdpDelimiterSemicolon, &fields);
3145
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003146 cricket::CodecParameterMap codec_params;
Donald Curtis144d0182015-05-15 13:14:24 -07003147 for (auto& iter : fields) {
Donald Curtis0e07f922015-05-15 09:21:23 -07003148 if (iter.find(kSdpDelimiterEqual) == std::string::npos) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003149 // Only fmtps with equals are currently supported. Other fmtp types
3150 // should be ignored. Unknown fmtps do not constitute an error.
3151 continue;
3152 }
Donald Curtis0e07f922015-05-15 09:21:23 -07003153
3154 std::string name;
3155 std::string value;
3156 if (!ParseFmtpParam(rtc::string_trim(iter), &name, &value, error)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003157 return false;
3158 }
3159 codec_params[name] = value;
3160 }
3161
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003162 if (media_type == cricket::MEDIA_TYPE_AUDIO) {
3163 UpdateCodec<AudioContentDescription, cricket::AudioCodec>(
pkasting@chromium.orgd3245462015-02-23 21:28:22 +00003164 media_desc, payload_type, codec_params);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003165 } else if (media_type == cricket::MEDIA_TYPE_VIDEO) {
3166 UpdateCodec<VideoContentDescription, cricket::VideoCodec>(
pkasting@chromium.orgd3245462015-02-23 21:28:22 +00003167 media_desc, payload_type, codec_params);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003168 }
3169 return true;
3170}
3171
3172bool ParseRtcpFbAttribute(const std::string& line, const MediaType media_type,
3173 MediaContentDescription* media_desc,
3174 SdpParseError* error) {
3175 if (media_type != cricket::MEDIA_TYPE_AUDIO &&
3176 media_type != cricket::MEDIA_TYPE_VIDEO) {
3177 return true;
3178 }
3179 std::vector<std::string> rtcp_fb_fields;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00003180 rtc::split(line.c_str(), kSdpDelimiterSpace, &rtcp_fb_fields);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003181 if (rtcp_fb_fields.size() < 2) {
3182 return ParseFailedGetValue(line, kAttributeRtcpFb, error);
3183 }
3184 std::string payload_type_string;
3185 if (!GetValue(rtcp_fb_fields[0], kAttributeRtcpFb, &payload_type_string,
3186 error)) {
3187 return false;
3188 }
wu@webrtc.org5e760e72014-04-02 23:19:09 +00003189 int payload_type = kWildcardPayloadType;
3190 if (payload_type_string != "*") {
pkasting@chromium.orge9facf82015-02-17 20:36:28 +00003191 if (!GetPayloadTypeFromString(line, payload_type_string, &payload_type,
3192 error)) {
wu@webrtc.org5e760e72014-04-02 23:19:09 +00003193 return false;
3194 }
3195 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003196 std::string id = rtcp_fb_fields[1];
3197 std::string param = "";
3198 for (std::vector<std::string>::iterator iter = rtcp_fb_fields.begin() + 2;
3199 iter != rtcp_fb_fields.end(); ++iter) {
3200 param.append(*iter);
3201 }
3202 const cricket::FeedbackParam feedback_param(id, param);
3203
3204 if (media_type == cricket::MEDIA_TYPE_AUDIO) {
pkasting@chromium.orgd3245462015-02-23 21:28:22 +00003205 UpdateCodec<AudioContentDescription, cricket::AudioCodec>(
3206 media_desc, payload_type, feedback_param);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003207 } else if (media_type == cricket::MEDIA_TYPE_VIDEO) {
pkasting@chromium.orgd3245462015-02-23 21:28:22 +00003208 UpdateCodec<VideoContentDescription, cricket::VideoCodec>(
3209 media_desc, payload_type, feedback_param);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003210 }
3211 return true;
3212}
3213
3214} // namespace webrtc