blob: 47e429863e1e0e1bea08ece8b20a83a3b190b497 [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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#include "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
Patrik Höglund5117b042017-10-02 09:55:40 +020023#include "api/candidate.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020024#include "api/jsepicecandidate.h"
25#include "api/jsepsessiondescription.h"
isheriff6f8d6862016-05-26 11:24:55 -070026// for RtpExtension
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020027#include "api/rtpparameters.h"
28#include "media/base/codec.h"
29#include "media/base/cryptoparams.h"
30#include "media/base/mediaconstants.h"
31#include "media/base/rtputils.h"
32#include "media/sctp/sctptransportinternal.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020033#include "p2p/base/p2pconstants.h"
34#include "p2p/base/port.h"
35#include "pc/mediasession.h"
36#include "rtc_base/arraysize.h"
37#include "rtc_base/checks.h"
38#include "rtc_base/logging.h"
39#include "rtc_base/messagedigest.h"
40#include "rtc_base/stringutils.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000041
42using cricket::AudioContentDescription;
43using cricket::Candidate;
44using cricket::Candidates;
45using cricket::ContentDescription;
46using cricket::ContentInfo;
47using cricket::CryptoParams;
48using cricket::DataContentDescription;
49using cricket::ICE_CANDIDATE_COMPONENT_RTP;
50using cricket::ICE_CANDIDATE_COMPONENT_RTCP;
51using cricket::kCodecParamMaxBitrate;
52using cricket::kCodecParamMaxPTime;
53using cricket::kCodecParamMaxQuantization;
54using cricket::kCodecParamMinBitrate;
55using cricket::kCodecParamMinPTime;
56using cricket::kCodecParamPTime;
57using cricket::kCodecParamSPropStereo;
buildbot@webrtc.orged97bb02014-05-07 11:15:20 +000058using cricket::kCodecParamStartBitrate;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000059using cricket::kCodecParamStereo;
60using cricket::kCodecParamUseInbandFec;
Minyue Li7100dcd2015-03-27 05:05:59 +010061using cricket::kCodecParamUseDtx;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000062using cricket::kCodecParamSctpProtocol;
63using cricket::kCodecParamSctpStreams;
henrike@webrtc.org1e09a712013-07-26 19:17:59 +000064using cricket::kCodecParamMaxAverageBitrate;
buildbot@webrtc.org5d639b32014-09-10 07:57:12 +000065using cricket::kCodecParamMaxPlaybackRate;
stefan@webrtc.org85d27942014-06-09 12:51:39 +000066using cricket::kCodecParamAssociatedPayloadType;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000067using cricket::MediaContentDescription;
68using cricket::MediaType;
isheriff6f8d6862016-05-26 11:24:55 -070069using cricket::RtpHeaderExtensions;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000070using cricket::SsrcGroup;
71using cricket::StreamParams;
72using cricket::StreamParamsVec;
73using cricket::TransportDescription;
74using cricket::TransportInfo;
75using cricket::VideoContentDescription;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000076using rtc::SocketAddress;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000077
henrike@webrtc.org28e20752013-07-10 00:45:36 +000078namespace cricket {
79class SessionDescription;
80}
81
deadbeef90f1e1e2017-02-10 12:35:05 -080082// TODO(deadbeef): Switch to using anonymous namespace rather than declaring
83// everything "static".
henrike@webrtc.org28e20752013-07-10 00:45:36 +000084namespace webrtc {
85
86// Line type
87// RFC 4566
88// An SDP session description consists of a number of lines of text of
89// the form:
90// <type>=<value>
91// where <type> MUST be exactly one case-significant character.
deadbeef9d3584c2016-02-16 17:54:10 -080092static const int kLinePrefixLength = 2; // Length of <type>=
henrike@webrtc.org28e20752013-07-10 00:45:36 +000093static const char kLineTypeVersion = 'v';
94static const char kLineTypeOrigin = 'o';
95static const char kLineTypeSessionName = 's';
96static const char kLineTypeSessionInfo = 'i';
97static const char kLineTypeSessionUri = 'u';
98static const char kLineTypeSessionEmail = 'e';
99static const char kLineTypeSessionPhone = 'p';
100static const char kLineTypeSessionBandwidth = 'b';
101static const char kLineTypeTiming = 't';
102static const char kLineTypeRepeatTimes = 'r';
103static const char kLineTypeTimeZone = 'z';
104static const char kLineTypeEncryptionKey = 'k';
105static const char kLineTypeMedia = 'm';
106static const char kLineTypeConnection = 'c';
107static const char kLineTypeAttributes = 'a';
108
109// Attributes
110static const char kAttributeGroup[] = "group";
111static const char kAttributeMid[] = "mid";
deadbeef9d3584c2016-02-16 17:54:10 -0800112static const char kAttributeMsid[] = "msid";
deadbeef25ed4352016-12-12 18:37:36 -0800113static const char kAttributeBundleOnly[] = "bundle-only";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000114static const char kAttributeRtcpMux[] = "rtcp-mux";
deadbeef13871492015-12-09 12:37:51 -0800115static const char kAttributeRtcpReducedSize[] = "rtcp-rsize";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000116static const char kAttributeSsrc[] = "ssrc";
117static const char kSsrcAttributeCname[] = "cname";
118static const char kAttributeExtmap[] = "extmap";
119// draft-alvestrand-mmusic-msid-01
120// a=msid-semantic: WMS
121static const char kAttributeMsidSemantics[] = "msid-semantic";
122static const char kMediaStreamSemantic[] = "WMS";
123static const char kSsrcAttributeMsid[] = "msid";
124static const char kDefaultMsid[] = "default";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000125static const char kSsrcAttributeMslabel[] = "mslabel";
126static const char kSSrcAttributeLabel[] = "label";
127static const char kAttributeSsrcGroup[] = "ssrc-group";
128static const char kAttributeCrypto[] = "crypto";
129static const char kAttributeCandidate[] = "candidate";
130static const char kAttributeCandidateTyp[] = "typ";
131static const char kAttributeCandidateRaddr[] = "raddr";
132static const char kAttributeCandidateRport[] = "rport";
honghaiza54a0802015-12-16 18:37:23 -0800133static const char kAttributeCandidateUfrag[] = "ufrag";
134static const char kAttributeCandidatePwd[] = "pwd";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000135static const char kAttributeCandidateGeneration[] = "generation";
honghaiza0c44ea2016-03-23 16:07:48 -0700136static const char kAttributeCandidateNetworkId[] = "network-id";
honghaize1a0c942016-02-16 14:54:56 -0800137static const char kAttributeCandidateNetworkCost[] = "network-cost";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000138static const char kAttributeFingerprint[] = "fingerprint";
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +0000139static const char kAttributeSetup[] = "setup";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000140static const char kAttributeFmtp[] = "fmtp";
141static const char kAttributeRtpmap[] = "rtpmap";
wu@webrtc.org78187522013-10-07 23:32:02 +0000142static const char kAttributeSctpmap[] = "sctpmap";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000143static const char kAttributeRtcp[] = "rtcp";
144static const char kAttributeIceUfrag[] = "ice-ufrag";
145static const char kAttributeIcePwd[] = "ice-pwd";
146static const char kAttributeIceLite[] = "ice-lite";
147static const char kAttributeIceOption[] = "ice-options";
148static const char kAttributeSendOnly[] = "sendonly";
149static const char kAttributeRecvOnly[] = "recvonly";
150static const char kAttributeRtcpFb[] = "rtcp-fb";
151static const char kAttributeSendRecv[] = "sendrecv";
152static const char kAttributeInactive[] = "inactive";
jiayl@webrtc.orgddb85ab2014-09-05 16:31:56 +0000153// draft-ietf-mmusic-sctp-sdp-07
154// a=sctp-port
155static const char kAttributeSctpPort[] = "sctp-port";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000156
157// Experimental flags
158static const char kAttributeXGoogleFlag[] = "x-google-flag";
159static const char kValueConference[] = "conference";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000160
161// Candidate
162static const char kCandidateHost[] = "host";
163static const char kCandidateSrflx[] = "srflx";
Honghai Zhang7fb69db2016-03-14 11:59:18 -0700164static const char kCandidatePrflx[] = "prflx";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000165static const char kCandidateRelay[] = "relay";
mallinath@webrtc.org2d60c5e2014-08-08 22:29:20 +0000166static const char kTcpCandidateType[] = "tcptype";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000167
168static const char kSdpDelimiterEqual = '=';
169static const char kSdpDelimiterSpace = ' ';
170static const char kSdpDelimiterColon = ':';
171static const char kSdpDelimiterSemicolon = ';';
172static const char kSdpDelimiterSlash = '/';
173static const char kNewLine = '\n';
174static const char kReturn = '\r';
175static const char kLineBreak[] = "\r\n";
176
177// TODO: Generate the Session and Time description
178// instead of hardcoding.
179static const char kSessionVersion[] = "v=0";
180// RFC 4566
181static const char kSessionOriginUsername[] = "-";
182static const char kSessionOriginSessionId[] = "0";
183static const char kSessionOriginSessionVersion[] = "0";
184static const char kSessionOriginNettype[] = "IN";
185static const char kSessionOriginAddrtype[] = "IP4";
186static const char kSessionOriginAddress[] = "127.0.0.1";
187static const char kSessionName[] = "s=-";
188static const char kTimeDescription[] = "t=0 0";
189static const char kAttrGroup[] = "a=group:BUNDLE";
190static const char kConnectionNettype[] = "IN";
pthatcher@webrtc.orgc9d6d142014-10-23 23:37:22 +0000191static const char kConnectionIpv4Addrtype[] = "IP4";
192static const char kConnectionIpv6Addrtype[] = "IP6";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000193static const char kMediaTypeVideo[] = "video";
194static const char kMediaTypeAudio[] = "audio";
195static const char kMediaTypeData[] = "application";
196static const char kMediaPortRejected[] = "0";
pthatcher@webrtc.orgc9d6d142014-10-23 23:37:22 +0000197// draft-ietf-mmusic-trickle-ice-01
198// When no candidates have been gathered, set the connection
199// address to IP6 ::.
perkj@webrtc.orgd105cc82014-11-07 11:22:06 +0000200// TODO(perkj): FF can not parse IP6 ::. See http://crbug/430333
201// Use IPV4 per default.
202static const char kDummyAddress[] = "0.0.0.0";
pthatcher@webrtc.orgc9d6d142014-10-23 23:37:22 +0000203static const char kDummyPort[] = "9";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000204// RFC 3556
205static const char kApplicationSpecificMaximum[] = "AS";
206
wu@webrtc.org78187522013-10-07 23:32:02 +0000207static const char kDefaultSctpmapProtocol[] = "webrtc-datachannel";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000208
pkasting@chromium.orgd3245462015-02-23 21:28:22 +0000209// RTP payload type is in the 0-127 range. Use -1 to indicate "all" payload
210// types.
211const int kWildcardPayloadType = -1;
212
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000213struct SsrcInfo {
Peter Boström0c4e06b2015-10-07 12:23:21 +0200214 uint32_t ssrc_id;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000215 std::string cname;
deadbeef9d3584c2016-02-16 17:54:10 -0800216 std::string stream_id;
217 std::string track_id;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000218
219 // For backward compatibility.
220 // TODO(ronghuawu): Remove below 2 fields once all the clients support msid.
221 std::string label;
222 std::string mslabel;
223};
224typedef std::vector<SsrcInfo> SsrcInfoVec;
225typedef std::vector<SsrcGroup> SsrcGroupVec;
226
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000227template <class T>
228static void AddFmtpLine(const T& codec, std::string* message);
229static void BuildMediaDescription(const ContentInfo* content_info,
230 const TransportInfo* transport_info,
231 const MediaType media_type,
wu@webrtc.org4c3e9912014-07-16 21:03:13 +0000232 const std::vector<Candidate>& candidates,
deadbeef9d3584c2016-02-16 17:54:10 -0800233 bool unified_plan_sdp,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000234 std::string* message);
zstein4b2e0822017-02-17 19:48:38 -0800235static void BuildSctpContentAttributes(std::string* message,
236 int sctp_port,
237 bool use_sctpmap);
deadbeef9d3584c2016-02-16 17:54:10 -0800238static void BuildRtpContentAttributes(const MediaContentDescription* media_desc,
239 const MediaType media_type,
240 bool unified_plan_sdp,
241 std::string* message);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000242static void BuildRtpMap(const MediaContentDescription* media_desc,
243 const MediaType media_type,
244 std::string* message);
245static void BuildCandidate(const std::vector<Candidate>& candidates,
honghaiza54a0802015-12-16 18:37:23 -0800246 bool include_ufrag,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000247 std::string* message);
248static void BuildIceOptions(const std::vector<std::string>& transport_options,
249 std::string* message);
pthatcher@webrtc.org3341b402015-02-13 21:14:22 +0000250static bool IsRtp(const std::string& protocol);
251static bool IsDtlsSctp(const std::string& protocol);
zhihuang38989e52017-03-21 11:04:53 -0700252static bool ParseSessionDescription(const std::string& message,
253 size_t* pos,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000254 std::string* session_id,
255 std::string* session_version,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000256 TransportDescription* session_td,
257 RtpHeaderExtensions* session_extmaps,
zhihuang38989e52017-03-21 11:04:53 -0700258 rtc::SocketAddress* connection_addr,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000259 cricket::SessionDescription* desc,
260 SdpParseError* error);
261static bool ParseGroupAttribute(const std::string& line,
262 cricket::SessionDescription* desc,
263 SdpParseError* error);
264static bool ParseMediaDescription(
265 const std::string& message,
266 const TransportDescription& session_td,
267 const RtpHeaderExtensions& session_extmaps,
zhihuang38989e52017-03-21 11:04:53 -0700268 size_t* pos,
269 const rtc::SocketAddress& session_connection_addr,
270 cricket::SessionDescription* desc,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000271 std::vector<JsepIceCandidate*>* candidates,
272 SdpParseError* error);
273static bool ParseContent(const std::string& message,
274 const MediaType media_type,
275 int mline_index,
276 const std::string& protocol,
deadbeef67cf2c12016-04-13 10:07:16 -0700277 const std::vector<int>& payload_types,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000278 size_t* pos,
279 std::string* content_name,
deadbeef25ed4352016-12-12 18:37:36 -0800280 bool* bundle_only,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000281 MediaContentDescription* media_desc,
282 TransportDescription* transport,
283 std::vector<JsepIceCandidate*>* candidates,
284 SdpParseError* error);
285static bool ParseSsrcAttribute(const std::string& line,
286 SsrcInfoVec* ssrc_infos,
287 SdpParseError* error);
288static bool ParseSsrcGroupAttribute(const std::string& line,
289 SsrcGroupVec* ssrc_groups,
290 SdpParseError* error);
291static bool ParseCryptoAttribute(const std::string& line,
292 MediaContentDescription* media_desc,
293 SdpParseError* error);
294static bool ParseRtpmapAttribute(const std::string& line,
295 const MediaType media_type,
deadbeef67cf2c12016-04-13 10:07:16 -0700296 const std::vector<int>& payload_types,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000297 MediaContentDescription* media_desc,
298 SdpParseError* error);
299static bool ParseFmtpAttributes(const std::string& line,
300 const MediaType media_type,
301 MediaContentDescription* media_desc,
302 SdpParseError* error);
303static bool ParseFmtpParam(const std::string& line, std::string* parameter,
304 std::string* value, SdpParseError* error);
305static bool ParseCandidate(const std::string& message, Candidate* candidate,
306 SdpParseError* error, bool is_raw);
307static bool ParseRtcpFbAttribute(const std::string& line,
308 const MediaType media_type,
309 MediaContentDescription* media_desc,
310 SdpParseError* error);
311static bool ParseIceOptions(const std::string& line,
312 std::vector<std::string>* transport_options,
313 SdpParseError* error);
314static bool ParseExtmap(const std::string& line,
isheriff6f8d6862016-05-26 11:24:55 -0700315 RtpExtension* extmap,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000316 SdpParseError* error);
317static bool ParseFingerprintAttribute(const std::string& line,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000318 rtc::SSLFingerprint** fingerprint,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000319 SdpParseError* error);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +0000320static bool ParseDtlsSetup(const std::string& line,
321 cricket::ConnectionRole* role,
322 SdpParseError* error);
deadbeef9d3584c2016-02-16 17:54:10 -0800323static bool ParseMsidAttribute(const std::string& line,
324 std::string* stream_id,
325 std::string* track_id,
326 SdpParseError* error);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000327
328// Helper functions
329
330// Below ParseFailed*** functions output the line that caused the parsing
331// failure and the detailed reason (|description|) of the failure to |error|.
332// The functions always return false so that they can be used directly in the
333// following way when error happens:
334// "return ParseFailed***(...);"
335
336// The line starting at |line_start| of |message| is the failing line.
337// The reason for the failure should be provided in the |description|.
338// An example of a description could be "unknown character".
339static bool ParseFailed(const std::string& message,
340 size_t line_start,
341 const std::string& description,
342 SdpParseError* error) {
343 // Get the first line of |message| from |line_start|.
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +0000344 std::string first_line;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000345 size_t line_end = message.find(kNewLine, line_start);
346 if (line_end != std::string::npos) {
347 if (line_end > 0 && (message.at(line_end - 1) == kReturn)) {
348 --line_end;
349 }
350 first_line = message.substr(line_start, (line_end - line_start));
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +0000351 } else {
352 first_line = message.substr(line_start);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000353 }
354
355 if (error) {
356 error->line = first_line;
357 error->description = description;
358 }
359 LOG(LS_ERROR) << "Failed to parse: \"" << first_line
360 << "\". Reason: " << description;
361 return false;
362}
363
364// |line| is the failing line. The reason for the failure should be
365// provided in the |description|.
366static bool ParseFailed(const std::string& line,
367 const std::string& description,
368 SdpParseError* error) {
369 return ParseFailed(line, 0, description, error);
370}
371
372// Parses failure where the failing SDP line isn't know or there are multiple
373// failing lines.
374static bool ParseFailed(const std::string& description,
375 SdpParseError* error) {
376 return ParseFailed("", description, error);
377}
378
379// |line| is the failing line. The failure is due to the fact that |line|
380// doesn't have |expected_fields| fields.
381static bool ParseFailedExpectFieldNum(const std::string& line,
382 int expected_fields,
383 SdpParseError* error) {
384 std::ostringstream description;
385 description << "Expects " << expected_fields << " fields.";
386 return ParseFailed(line, description.str(), error);
387}
388
389// |line| is the failing line. The failure is due to the fact that |line| has
390// less than |expected_min_fields| fields.
391static bool ParseFailedExpectMinFieldNum(const std::string& line,
392 int expected_min_fields,
393 SdpParseError* error) {
394 std::ostringstream description;
395 description << "Expects at least " << expected_min_fields << " fields.";
396 return ParseFailed(line, description.str(), error);
397}
398
399// |line| is the failing line. The failure is due to the fact that it failed to
400// get the value of |attribute|.
401static bool ParseFailedGetValue(const std::string& line,
402 const std::string& attribute,
403 SdpParseError* error) {
404 std::ostringstream description;
405 description << "Failed to get the value of attribute: " << attribute;
406 return ParseFailed(line, description.str(), error);
407}
408
409// The line starting at |line_start| of |message| is the failing line. The
410// failure is due to the line type (e.g. the "m" part of the "m-line")
411// not matching what is expected. The expected line type should be
412// provided as |line_type|.
413static bool ParseFailedExpectLine(const std::string& message,
414 size_t line_start,
415 const char line_type,
416 const std::string& line_value,
417 SdpParseError* error) {
418 std::ostringstream description;
419 description << "Expect line: " << line_type << "=" << line_value;
420 return ParseFailed(message, line_start, description.str(), error);
421}
422
423static bool AddLine(const std::string& line, std::string* message) {
424 if (!message)
425 return false;
426
427 message->append(line);
428 message->append(kLineBreak);
429 return true;
430}
431
432static bool GetLine(const std::string& message,
433 size_t* pos,
434 std::string* line) {
435 size_t line_begin = *pos;
436 size_t line_end = message.find(kNewLine, line_begin);
437 if (line_end == std::string::npos) {
438 return false;
439 }
440 // Update the new start position
441 *pos = line_end + 1;
442 if (line_end > 0 && (message.at(line_end - 1) == kReturn)) {
443 --line_end;
444 }
445 *line = message.substr(line_begin, (line_end - line_begin));
446 const char* cline = line->c_str();
447 // RFC 4566
448 // An SDP session description consists of a number of lines of text of
449 // the form:
450 // <type>=<value>
451 // where <type> MUST be exactly one case-significant character and
452 // <value> is structured text whose format depends on <type>.
453 // Whitespace MUST NOT be used on either side of the "=" sign.
decurtis@webrtc.org8af11042015-01-07 19:15:51 +0000454 if (line->length() < 3 ||
455 !islower(cline[0]) ||
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000456 cline[1] != kSdpDelimiterEqual ||
457 cline[2] == kSdpDelimiterSpace) {
458 *pos = line_begin;
459 return false;
460 }
461 return true;
462}
463
464// Init |os| to "|type|=|value|".
465static void InitLine(const char type,
466 const std::string& value,
467 std::ostringstream* os) {
468 os->str("");
469 *os << type << kSdpDelimiterEqual << value;
470}
471
472// Init |os| to "a=|attribute|".
473static void InitAttrLine(const std::string& attribute, std::ostringstream* os) {
474 InitLine(kLineTypeAttributes, attribute, os);
475}
476
477// Writes a SDP attribute line based on |attribute| and |value| to |message|.
478static void AddAttributeLine(const std::string& attribute, int value,
479 std::string* message) {
480 std::ostringstream os;
481 InitAttrLine(attribute, &os);
482 os << kSdpDelimiterColon << value;
483 AddLine(os.str(), message);
484}
485
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000486static bool IsLineType(const std::string& message,
487 const char type,
488 size_t line_start) {
489 if (message.size() < line_start + kLinePrefixLength) {
490 return false;
491 }
492 const char* cmessage = message.c_str();
493 return (cmessage[line_start] == type &&
494 cmessage[line_start + 1] == kSdpDelimiterEqual);
495}
496
497static bool IsLineType(const std::string& line,
498 const char type) {
499 return IsLineType(line, type, 0);
500}
501
502static bool GetLineWithType(const std::string& message, size_t* pos,
503 std::string* line, const char type) {
504 if (!IsLineType(message, type, *pos)) {
505 return false;
506 }
507
508 if (!GetLine(message, pos, line))
509 return false;
510
511 return true;
512}
513
514static bool HasAttribute(const std::string& line,
515 const std::string& attribute) {
516 return (line.compare(kLinePrefixLength, attribute.size(), attribute) == 0);
517}
518
Peter Boström0c4e06b2015-10-07 12:23:21 +0200519static bool AddSsrcLine(uint32_t ssrc_id,
520 const std::string& attribute,
521 const std::string& value,
522 std::string* message) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000523 // RFC 5576
524 // a=ssrc:<ssrc-id> <attribute>:<value>
525 std::ostringstream os;
526 InitAttrLine(kAttributeSsrc, &os);
527 os << kSdpDelimiterColon << ssrc_id << kSdpDelimiterSpace
528 << attribute << kSdpDelimiterColon << value;
529 return AddLine(os.str(), message);
530}
531
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000532// Get value only from <attribute>:<value>.
533static bool GetValue(const std::string& message, const std::string& attribute,
534 std::string* value, SdpParseError* error) {
535 std::string leftpart;
Donald Curtis0e07f922015-05-15 09:21:23 -0700536 if (!rtc::tokenize_first(message, kSdpDelimiterColon, &leftpart, value)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000537 return ParseFailedGetValue(message, attribute, error);
538 }
539 // The left part should end with the expected attribute.
540 if (leftpart.length() < attribute.length() ||
541 leftpart.compare(leftpart.length() - attribute.length(),
542 attribute.length(), attribute) != 0) {
543 return ParseFailedGetValue(message, attribute, error);
544 }
545 return true;
546}
547
548static bool CaseInsensitiveFind(std::string str1, std::string str2) {
549 std::transform(str1.begin(), str1.end(), str1.begin(),
550 ::tolower);
551 std::transform(str2.begin(), str2.end(), str2.begin(),
552 ::tolower);
553 return str1.find(str2) != std::string::npos;
554}
555
wu@webrtc.org5e760e72014-04-02 23:19:09 +0000556template <class T>
557static bool GetValueFromString(const std::string& line,
558 const std::string& s,
559 T* t,
560 SdpParseError* error) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000561 if (!rtc::FromString(s, t)) {
wu@webrtc.org5e760e72014-04-02 23:19:09 +0000562 std::ostringstream description;
563 description << "Invalid value: " << s << ".";
564 return ParseFailed(line, description.str(), error);
565 }
566 return true;
567}
568
pkasting@chromium.orge9facf82015-02-17 20:36:28 +0000569static bool GetPayloadTypeFromString(const std::string& line,
570 const std::string& s,
571 int* payload_type,
572 SdpParseError* error) {
573 return GetValueFromString(line, s, payload_type, error) &&
574 cricket::IsValidRtpPayloadType(*payload_type);
575}
576
Taylor Brandstetter5de6b752016-03-09 17:02:30 -0800577// |msid_stream_id| and |msid_track_id| represent the stream/track ID from the
578// "a=msid" attribute, if it exists. They are empty if the attribute does not
579// exist.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000580void CreateTracksFromSsrcInfos(const SsrcInfoVec& ssrc_infos,
Taylor Brandstetter5de6b752016-03-09 17:02:30 -0800581 const std::string& msid_stream_id,
582 const std::string& msid_track_id,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000583 StreamParamsVec* tracks) {
nisseede5da42017-01-12 05:15:36 -0800584 RTC_DCHECK(tracks != NULL);
585 RTC_DCHECK(msid_stream_id.empty() == msid_track_id.empty());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000586 for (SsrcInfoVec::const_iterator ssrc_info = ssrc_infos.begin();
587 ssrc_info != ssrc_infos.end(); ++ssrc_info) {
588 if (ssrc_info->cname.empty()) {
589 continue;
590 }
591
Taylor Brandstetter5de6b752016-03-09 17:02:30 -0800592 std::string stream_id;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000593 std::string track_id;
Taylor Brandstetter5de6b752016-03-09 17:02:30 -0800594 if (ssrc_info->stream_id.empty() && !ssrc_info->mslabel.empty()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000595 // If there's no msid and there's mslabel, we consider this is a sdp from
596 // a older version of client that doesn't support msid.
597 // In that case, we use the mslabel and label to construct the track.
Taylor Brandstetter5de6b752016-03-09 17:02:30 -0800598 stream_id = ssrc_info->mslabel;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000599 track_id = ssrc_info->label;
Taylor Brandstetter5de6b752016-03-09 17:02:30 -0800600 } else if (ssrc_info->stream_id.empty() && !msid_stream_id.empty()) {
601 // If there's no msid in the SSRC attributes, but there's a global one
602 // (from a=msid), use that. This is the case with unified plan SDP.
603 stream_id = msid_stream_id;
604 track_id = msid_track_id;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000605 } else {
Taylor Brandstetter5de6b752016-03-09 17:02:30 -0800606 stream_id = ssrc_info->stream_id;
deadbeef9d3584c2016-02-16 17:54:10 -0800607 track_id = ssrc_info->track_id;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000608 }
Taylor Brandstetter5de6b752016-03-09 17:02:30 -0800609 // If a stream/track ID wasn't populated from the SSRC attributes OR the
610 // msid attribute, use default/random values.
611 if (stream_id.empty()) {
612 stream_id = kDefaultMsid;
613 }
614 if (track_id.empty()) {
615 // TODO(ronghuawu): What should we do if the track id doesn't appear?
616 // Create random string (which will be used as track label later)?
617 track_id = rtc::CreateRandomString(8);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000618 }
619
620 StreamParamsVec::iterator track = tracks->begin();
621 for (; track != tracks->end(); ++track) {
622 if (track->id == track_id) {
623 break;
624 }
625 }
626 if (track == tracks->end()) {
627 // If we don't find an existing track, create a new one.
628 tracks->push_back(StreamParams());
629 track = tracks->end() - 1;
630 }
631 track->add_ssrc(ssrc_info->ssrc_id);
632 track->cname = ssrc_info->cname;
Taylor Brandstetter5de6b752016-03-09 17:02:30 -0800633 track->sync_label = stream_id;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000634 track->id = track_id;
635 }
636}
637
638void GetMediaStreamLabels(const ContentInfo* content,
639 std::set<std::string>* labels) {
640 const MediaContentDescription* media_desc =
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000641 static_cast<const MediaContentDescription*>(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000642 content->description);
643 const cricket::StreamParamsVec& streams = media_desc->streams();
644 for (cricket::StreamParamsVec::const_iterator it = streams.begin();
645 it != streams.end(); ++it) {
646 labels->insert(it->sync_label);
647 }
648}
649
650// RFC 5245
651// It is RECOMMENDED that default candidates be chosen based on the
652// likelihood of those candidates to work with the peer that is being
653// contacted. It is RECOMMENDED that relayed > reflexive > host.
654static const int kPreferenceUnknown = 0;
655static const int kPreferenceHost = 1;
656static const int kPreferenceReflexive = 2;
657static const int kPreferenceRelayed = 3;
658
659static int GetCandidatePreferenceFromType(const std::string& type) {
660 int preference = kPreferenceUnknown;
661 if (type == cricket::LOCAL_PORT_TYPE) {
662 preference = kPreferenceHost;
663 } else if (type == cricket::STUN_PORT_TYPE) {
664 preference = kPreferenceReflexive;
665 } else if (type == cricket::RELAY_PORT_TYPE) {
666 preference = kPreferenceRelayed;
667 } else {
nissec80e7412017-01-11 05:56:46 -0800668 RTC_NOTREACHED();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000669 }
670 return preference;
671}
672
guoweis@webrtc.org57ac2c82015-02-06 00:45:13 +0000673// Get ip and port of the default destination from the |candidates| with the
674// given value of |component_id|. The default candidate should be the one most
675// likely to work, typically IPv4 relay.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000676// RFC 5245
677// The value of |component_id| currently supported are 1 (RTP) and 2 (RTCP).
678// TODO: Decide the default destination in webrtcsession and
679// pass it down via SessionDescription.
pthatcher@webrtc.orgc9d6d142014-10-23 23:37:22 +0000680static void GetDefaultDestination(
681 const std::vector<Candidate>& candidates,
682 int component_id, std::string* port,
683 std::string* ip, std::string* addr_type) {
perkj@webrtc.orgd105cc82014-11-07 11:22:06 +0000684 *addr_type = kConnectionIpv4Addrtype;
pthatcher@webrtc.orgc9d6d142014-10-23 23:37:22 +0000685 *port = kDummyPort;
686 *ip = kDummyAddress;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000687 int current_preference = kPreferenceUnknown;
guoweis@webrtc.org57ac2c82015-02-06 00:45:13 +0000688 int current_family = AF_UNSPEC;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000689 for (std::vector<Candidate>::const_iterator it = candidates.begin();
690 it != candidates.end(); ++it) {
691 if (it->component() != component_id) {
692 continue;
693 }
guoweis@webrtc.org57ac2c82015-02-06 00:45:13 +0000694 // Default destination should be UDP only.
695 if (it->protocol() != cricket::UDP_PROTOCOL_NAME) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000696 continue;
697 }
guoweis@webrtc.org57ac2c82015-02-06 00:45:13 +0000698 const int preference = GetCandidatePreferenceFromType(it->type());
699 const int family = it->address().ipaddr().family();
700 // See if this candidate is more preferable then the current one if it's the
701 // same family. Or if the current family is IPv4 already so we could safely
702 // ignore all IPv6 ones. WebRTC bug 4269.
703 // http://code.google.com/p/webrtc/issues/detail?id=4269
704 if ((preference <= current_preference && current_family == family) ||
705 (current_family == AF_INET && family == AF_INET6)) {
706 continue;
707 }
pthatcher@webrtc.orgc9d6d142014-10-23 23:37:22 +0000708 if (family == AF_INET) {
709 addr_type->assign(kConnectionIpv4Addrtype);
710 } else if (family == AF_INET6) {
711 addr_type->assign(kConnectionIpv6Addrtype);
712 }
guoweis@webrtc.org57ac2c82015-02-06 00:45:13 +0000713 current_preference = preference;
714 current_family = family;
715 *port = it->address().PortAsString();
716 *ip = it->address().ipaddr().ToString();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000717 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000718}
719
wu@webrtc.org4c3e9912014-07-16 21:03:13 +0000720// Gets "a=rtcp" line if found default RTCP candidate from |candidates|.
721static std::string GetRtcpLine(const std::vector<Candidate>& candidates) {
pthatcher@webrtc.orgc9d6d142014-10-23 23:37:22 +0000722 std::string rtcp_line, rtcp_port, rtcp_ip, addr_type;
723 GetDefaultDestination(candidates, ICE_CANDIDATE_COMPONENT_RTCP,
724 &rtcp_port, &rtcp_ip, &addr_type);
725 // Found default RTCP candidate.
726 // RFC 5245
727 // If the agent is utilizing RTCP, it MUST encode the RTCP candidate
728 // using the a=rtcp attribute as defined in RFC 3605.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000729
pthatcher@webrtc.orgc9d6d142014-10-23 23:37:22 +0000730 // RFC 3605
731 // rtcp-attribute = "a=rtcp:" port [nettype space addrtype space
732 // connection-address] CRLF
733 std::ostringstream os;
734 InitAttrLine(kAttributeRtcp, &os);
735 os << kSdpDelimiterColon
736 << rtcp_port << " "
737 << kConnectionNettype << " "
738 << addr_type << " "
739 << rtcp_ip;
740 rtcp_line = os.str();
wu@webrtc.org4c3e9912014-07-16 21:03:13 +0000741 return rtcp_line;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000742}
743
744// Get candidates according to the mline index from SessionDescriptionInterface.
745static void GetCandidatesByMindex(const SessionDescriptionInterface& desci,
746 int mline_index,
747 std::vector<Candidate>* candidates) {
748 if (!candidates) {
749 return;
750 }
751 const IceCandidateCollection* cc = desci.candidates(mline_index);
752 for (size_t i = 0; i < cc->count(); ++i) {
753 const IceCandidateInterface* candidate = cc->at(i);
754 candidates->push_back(candidate->candidate());
755 }
756}
757
deadbeef90f1e1e2017-02-10 12:35:05 -0800758static bool IsValidPort(int port) {
759 return port >= 0 && port <= 65535;
760}
761
deadbeef9d3584c2016-02-16 17:54:10 -0800762std::string SdpSerialize(const JsepSessionDescription& jdesc,
763 bool unified_plan_sdp) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000764 const cricket::SessionDescription* desc = jdesc.description();
765 if (!desc) {
766 return "";
767 }
768
769 std::string message;
770
771 // Session Description.
772 AddLine(kSessionVersion, &message);
773 // Session Origin
774 // RFC 4566
775 // o=<username> <sess-id> <sess-version> <nettype> <addrtype>
776 // <unicast-address>
777 std::ostringstream os;
778 InitLine(kLineTypeOrigin, kSessionOriginUsername, &os);
jbauch083b73f2015-07-16 02:46:32 -0700779 const std::string& session_id = jdesc.session_id().empty() ?
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000780 kSessionOriginSessionId : jdesc.session_id();
jbauch083b73f2015-07-16 02:46:32 -0700781 const std::string& session_version = jdesc.session_version().empty() ?
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000782 kSessionOriginSessionVersion : jdesc.session_version();
783 os << " " << session_id << " " << session_version << " "
784 << kSessionOriginNettype << " " << kSessionOriginAddrtype << " "
785 << kSessionOriginAddress;
786 AddLine(os.str(), &message);
787 AddLine(kSessionName, &message);
788
789 // Time Description.
790 AddLine(kTimeDescription, &message);
791
792 // Group
793 if (desc->HasGroup(cricket::GROUP_TYPE_BUNDLE)) {
794 std::string group_line = kAttrGroup;
795 const cricket::ContentGroup* group =
796 desc->GetGroupByName(cricket::GROUP_TYPE_BUNDLE);
nisseede5da42017-01-12 05:15:36 -0800797 RTC_DCHECK(group != NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000798 const cricket::ContentNames& content_names = group->content_names();
799 for (cricket::ContentNames::const_iterator it = content_names.begin();
800 it != content_names.end(); ++it) {
801 group_line.append(" ");
802 group_line.append(*it);
803 }
804 AddLine(group_line, &message);
805 }
806
807 // MediaStream semantics
808 InitAttrLine(kAttributeMsidSemantics, &os);
809 os << kSdpDelimiterColon << " " << kMediaStreamSemantic;
wu@webrtc.org4c3e9912014-07-16 21:03:13 +0000810
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000811 std::set<std::string> media_stream_labels;
812 const ContentInfo* audio_content = GetFirstAudioContent(desc);
813 if (audio_content)
814 GetMediaStreamLabels(audio_content, &media_stream_labels);
wu@webrtc.org4c3e9912014-07-16 21:03:13 +0000815
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000816 const ContentInfo* video_content = GetFirstVideoContent(desc);
817 if (video_content)
818 GetMediaStreamLabels(video_content, &media_stream_labels);
wu@webrtc.org4c3e9912014-07-16 21:03:13 +0000819
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000820 for (std::set<std::string>::const_iterator it =
821 media_stream_labels.begin(); it != media_stream_labels.end(); ++it) {
822 os << " " << *it;
823 }
824 AddLine(os.str(), &message);
825
wu@webrtc.org4c3e9912014-07-16 21:03:13 +0000826 // Preserve the order of the media contents.
827 int mline_index = -1;
828 for (cricket::ContentInfos::const_iterator it = desc->contents().begin();
829 it != desc->contents().end(); ++it) {
830 const MediaContentDescription* mdesc =
831 static_cast<const MediaContentDescription*>(it->description);
832 std::vector<Candidate> candidates;
833 GetCandidatesByMindex(jdesc, ++mline_index, &candidates);
deadbeef9d3584c2016-02-16 17:54:10 -0800834 BuildMediaDescription(&*it, desc->GetTransportInfoByName(it->name),
835 mdesc->type(), candidates, unified_plan_sdp,
wu@webrtc.org4c3e9912014-07-16 21:03:13 +0000836 &message);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000837 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000838 return message;
839}
840
841// Serializes the passed in IceCandidateInterface to a SDP string.
842// candidate - The candidate to be serialized.
Honghai Zhang7fb69db2016-03-14 11:59:18 -0700843std::string SdpSerializeCandidate(const IceCandidateInterface& candidate) {
844 return SdpSerializeCandidate(candidate.candidate());
845}
846
847// Serializes a cricket Candidate.
848std::string SdpSerializeCandidate(const cricket::Candidate& candidate) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000849 std::string message;
Honghai Zhang7fb69db2016-03-14 11:59:18 -0700850 std::vector<cricket::Candidate> candidates(1, candidate);
honghaiza54a0802015-12-16 18:37:23 -0800851 BuildCandidate(candidates, true, &message);
wu@webrtc.orgec9f5fb2014-06-24 17:05:10 +0000852 // From WebRTC draft section 4.8.1.1 candidate-attribute will be
853 // just candidate:<candidate> not a=candidate:<blah>CRLF
nisseede5da42017-01-12 05:15:36 -0800854 RTC_DCHECK(message.find("a=") == 0);
wu@webrtc.orgec9f5fb2014-06-24 17:05:10 +0000855 message.erase(0, 2);
nisseede5da42017-01-12 05:15:36 -0800856 RTC_DCHECK(message.find(kLineBreak) == message.size() - 2);
wu@webrtc.orgec9f5fb2014-06-24 17:05:10 +0000857 message.resize(message.size() - 2);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000858 return message;
859}
860
861bool SdpDeserialize(const std::string& message,
862 JsepSessionDescription* jdesc,
863 SdpParseError* error) {
864 std::string session_id;
865 std::string session_version;
Peter Thatcher7cbd1882015-09-17 18:54:52 -0700866 TransportDescription session_td("", "");
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000867 RtpHeaderExtensions session_extmaps;
zhihuang38989e52017-03-21 11:04:53 -0700868 rtc::SocketAddress session_connection_addr;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000869 cricket::SessionDescription* desc = new cricket::SessionDescription();
870 std::vector<JsepIceCandidate*> candidates;
871 size_t current_pos = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000872
873 // Session Description
874 if (!ParseSessionDescription(message, &current_pos, &session_id,
deadbeefc80741f2015-10-22 13:14:45 -0700875 &session_version, &session_td, &session_extmaps,
zhihuang38989e52017-03-21 11:04:53 -0700876 &session_connection_addr, desc, error)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000877 delete desc;
878 return false;
879 }
880
881 // Media Description
deadbeefc80741f2015-10-22 13:14:45 -0700882 if (!ParseMediaDescription(message, session_td, session_extmaps, &current_pos,
zhihuang38989e52017-03-21 11:04:53 -0700883 session_connection_addr, desc, &candidates,
884 error)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000885 delete desc;
886 for (std::vector<JsepIceCandidate*>::const_iterator
887 it = candidates.begin(); it != candidates.end(); ++it) {
888 delete *it;
889 }
890 return false;
891 }
892
893 jdesc->Initialize(desc, session_id, session_version);
894
895 for (std::vector<JsepIceCandidate*>::const_iterator
896 it = candidates.begin(); it != candidates.end(); ++it) {
897 jdesc->AddCandidate(*it);
898 delete *it;
899 }
900 return true;
901}
902
903bool SdpDeserializeCandidate(const std::string& message,
904 JsepIceCandidate* jcandidate,
905 SdpParseError* error) {
nisseede5da42017-01-12 05:15:36 -0800906 RTC_DCHECK(jcandidate != NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000907 Candidate candidate;
908 if (!ParseCandidate(message, &candidate, error, true)) {
909 return false;
910 }
911 jcandidate->SetCandidate(candidate);
912 return true;
913}
914
Honghai Zhang7fb69db2016-03-14 11:59:18 -0700915bool SdpDeserializeCandidate(const std::string& transport_name,
916 const std::string& message,
917 cricket::Candidate* candidate,
918 SdpParseError* error) {
nisseede5da42017-01-12 05:15:36 -0800919 RTC_DCHECK(candidate != nullptr);
Honghai Zhang7fb69db2016-03-14 11:59:18 -0700920 if (!ParseCandidate(message, candidate, error, true)) {
921 return false;
922 }
923 candidate->set_transport_name(transport_name);
924 return true;
925}
926
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000927bool ParseCandidate(const std::string& message, Candidate* candidate,
928 SdpParseError* error, bool is_raw) {
nisseede5da42017-01-12 05:15:36 -0800929 RTC_DCHECK(candidate != NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000930
931 // Get the first line from |message|.
jiayl@webrtc.org7ec3f9f2014-08-08 23:09:15 +0000932 std::string first_line = message;
933 size_t pos = 0;
934 GetLine(message, &pos, &first_line);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000935
jiayl@webrtc.org7ec3f9f2014-08-08 23:09:15 +0000936 // Makes sure |message| contains only one line.
937 if (message.size() > first_line.size()) {
938 std::string left, right;
Donald Curtis0e07f922015-05-15 09:21:23 -0700939 if (rtc::tokenize_first(message, kNewLine, &left, &right) &&
940 !right.empty()) {
jiayl@webrtc.org7ec3f9f2014-08-08 23:09:15 +0000941 return ParseFailed(message, 0, "Expect one line only", error);
942 }
943 }
944
945 // From WebRTC draft section 4.8.1.1 candidate-attribute should be
946 // candidate:<candidate> when trickled, but we still support
947 // a=candidate:<blah>CRLF for backward compatibility and for parsing a line
948 // from the SDP.
949 if (IsLineType(first_line, kLineTypeAttributes)) {
950 first_line = first_line.substr(kLinePrefixLength);
951 }
952
953 std::string attribute_candidate;
954 std::string candidate_value;
955
956 // |first_line| must be in the form of "candidate:<value>".
Donald Curtis144d0182015-05-15 13:14:24 -0700957 if (!rtc::tokenize_first(first_line, kSdpDelimiterColon, &attribute_candidate,
958 &candidate_value) ||
jiayl@webrtc.org7ec3f9f2014-08-08 23:09:15 +0000959 attribute_candidate != kAttributeCandidate) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000960 if (is_raw) {
961 std::ostringstream description;
jiayl@webrtc.org7ec3f9f2014-08-08 23:09:15 +0000962 description << "Expect line: " << kAttributeCandidate
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000963 << ":" << "<candidate-str>";
964 return ParseFailed(first_line, 0, description.str(), error);
965 } else {
966 return ParseFailedExpectLine(first_line, 0, kLineTypeAttributes,
967 kAttributeCandidate, error);
968 }
969 }
970
971 std::vector<std::string> fields;
jiayl@webrtc.org7ec3f9f2014-08-08 23:09:15 +0000972 rtc::split(candidate_value, kSdpDelimiterSpace, &fields);
973
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000974 // RFC 5245
975 // a=candidate:<foundation> <component-id> <transport> <priority>
976 // <connection-address> <port> typ <candidate-types>
977 // [raddr <connection-address>] [rport <port>]
978 // *(SP extension-att-name SP extension-att-value)
979 const size_t expected_min_fields = 8;
980 if (fields.size() < expected_min_fields ||
981 (fields[6] != kAttributeCandidateTyp)) {
982 return ParseFailedExpectMinFieldNum(first_line, expected_min_fields, error);
983 }
jbauch083b73f2015-07-16 02:46:32 -0700984 const std::string& foundation = fields[0];
jiayl@webrtc.org7ec3f9f2014-08-08 23:09:15 +0000985
wu@webrtc.org5e760e72014-04-02 23:19:09 +0000986 int component_id = 0;
987 if (!GetValueFromString(first_line, fields[1], &component_id, error)) {
988 return false;
989 }
jbauch083b73f2015-07-16 02:46:32 -0700990 const std::string& transport = fields[2];
Peter Boström0c4e06b2015-10-07 12:23:21 +0200991 uint32_t priority = 0;
wu@webrtc.org5e760e72014-04-02 23:19:09 +0000992 if (!GetValueFromString(first_line, fields[3], &priority, error)) {
993 return false;
994 }
jbauch083b73f2015-07-16 02:46:32 -0700995 const std::string& connection_address = fields[4];
wu@webrtc.org5e760e72014-04-02 23:19:09 +0000996 int port = 0;
997 if (!GetValueFromString(first_line, fields[5], &port, error)) {
998 return false;
999 }
deadbeef90f1e1e2017-02-10 12:35:05 -08001000 if (!IsValidPort(port)) {
1001 return ParseFailed(first_line, "Invalid port number.", error);
1002 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001003 SocketAddress address(connection_address, port);
1004
1005 cricket::ProtocolType protocol;
hnslb68cc752016-12-13 10:33:41 -08001006 if (!StringToProto(transport.c_str(), &protocol)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001007 return ParseFailed(first_line, "Unsupported transport type.", error);
1008 }
hnslb68cc752016-12-13 10:33:41 -08001009 switch (protocol) {
1010 case cricket::PROTO_UDP:
1011 case cricket::PROTO_TCP:
1012 case cricket::PROTO_SSLTCP:
1013 // Supported protocol.
1014 break;
1015 default:
1016 return ParseFailed(first_line, "Unsupported transport type.", error);
1017 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001018
1019 std::string candidate_type;
jbauch083b73f2015-07-16 02:46:32 -07001020 const std::string& type = fields[7];
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001021 if (type == kCandidateHost) {
1022 candidate_type = cricket::LOCAL_PORT_TYPE;
1023 } else if (type == kCandidateSrflx) {
1024 candidate_type = cricket::STUN_PORT_TYPE;
1025 } else if (type == kCandidateRelay) {
1026 candidate_type = cricket::RELAY_PORT_TYPE;
Honghai Zhang7fb69db2016-03-14 11:59:18 -07001027 } else if (type == kCandidatePrflx) {
1028 candidate_type = cricket::PRFLX_PORT_TYPE;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001029 } else {
1030 return ParseFailed(first_line, "Unsupported candidate type.", error);
1031 }
1032
1033 size_t current_position = expected_min_fields;
1034 SocketAddress related_address;
1035 // The 2 optional fields for related address
1036 // [raddr <connection-address>] [rport <port>]
1037 if (fields.size() >= (current_position + 2) &&
1038 fields[current_position] == kAttributeCandidateRaddr) {
1039 related_address.SetIP(fields[++current_position]);
1040 ++current_position;
1041 }
1042 if (fields.size() >= (current_position + 2) &&
1043 fields[current_position] == kAttributeCandidateRport) {
wu@webrtc.org5e760e72014-04-02 23:19:09 +00001044 int port = 0;
1045 if (!GetValueFromString(
1046 first_line, fields[++current_position], &port, error)) {
1047 return false;
1048 }
deadbeef90f1e1e2017-02-10 12:35:05 -08001049 if (!IsValidPort(port)) {
1050 return ParseFailed(first_line, "Invalid port number.", error);
1051 }
wu@webrtc.org5e760e72014-04-02 23:19:09 +00001052 related_address.SetPort(port);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001053 ++current_position;
1054 }
1055
mallinath@webrtc.org2d60c5e2014-08-08 22:29:20 +00001056 // If this is a TCP candidate, it has additional extension as defined in
1057 // RFC 6544.
1058 std::string tcptype;
1059 if (fields.size() >= (current_position + 2) &&
1060 fields[current_position] == kTcpCandidateType) {
1061 tcptype = fields[++current_position];
1062 ++current_position;
1063
1064 if (tcptype != cricket::TCPTYPE_ACTIVE_STR &&
1065 tcptype != cricket::TCPTYPE_PASSIVE_STR &&
1066 tcptype != cricket::TCPTYPE_SIMOPEN_STR) {
1067 return ParseFailed(first_line, "Invalid TCP candidate type.", error);
1068 }
1069
1070 if (protocol != cricket::PROTO_TCP) {
1071 return ParseFailed(first_line, "Invalid non-TCP candidate", error);
1072 }
1073 }
1074
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001075 // Extension
honghaiza54a0802015-12-16 18:37:23 -08001076 // Though non-standard, we support the ICE ufrag and pwd being signaled on
1077 // the candidate to avoid issues with confusing which generation a candidate
1078 // belongs to when trickling multiple generations at the same time.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001079 std::string username;
1080 std::string password;
Peter Boström0c4e06b2015-10-07 12:23:21 +02001081 uint32_t generation = 0;
honghaiza0c44ea2016-03-23 16:07:48 -07001082 uint16_t network_id = 0;
1083 uint16_t network_cost = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001084 for (size_t i = current_position; i + 1 < fields.size(); ++i) {
1085 // RFC 5245
1086 // *(SP extension-att-name SP extension-att-value)
1087 if (fields[i] == kAttributeCandidateGeneration) {
wu@webrtc.org5e760e72014-04-02 23:19:09 +00001088 if (!GetValueFromString(first_line, fields[++i], &generation, error)) {
1089 return false;
1090 }
honghaiza54a0802015-12-16 18:37:23 -08001091 } else if (fields[i] == kAttributeCandidateUfrag) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001092 username = fields[++i];
honghaiza54a0802015-12-16 18:37:23 -08001093 } else if (fields[i] == kAttributeCandidatePwd) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001094 password = fields[++i];
honghaiza0c44ea2016-03-23 16:07:48 -07001095 } else if (fields[i] == kAttributeCandidateNetworkId) {
1096 if (!GetValueFromString(first_line, fields[++i], &network_id, error)) {
1097 return false;
1098 }
honghaize1a0c942016-02-16 14:54:56 -08001099 } else if (fields[i] == kAttributeCandidateNetworkCost) {
1100 if (!GetValueFromString(first_line, fields[++i], &network_cost, error)) {
1101 return false;
1102 }
Honghai Zhang351d77b2016-05-20 15:08:29 -07001103 network_cost = std::min(network_cost, rtc::kNetworkCostMax);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001104 } else {
1105 // Skip the unknown extension.
1106 ++i;
1107 }
1108 }
1109
guoweis@webrtc.org61c12472015-01-15 06:53:07 +00001110 *candidate = Candidate(component_id, cricket::ProtoToString(protocol),
guoweis@webrtc.org950c5182014-12-16 23:01:31 +00001111 address, priority, username, password, candidate_type,
honghaiza0c44ea2016-03-23 16:07:48 -07001112 generation, foundation, network_id, network_cost);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001113 candidate->set_related_address(related_address);
mallinath@webrtc.org2d60c5e2014-08-08 22:29:20 +00001114 candidate->set_tcptype(tcptype);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001115 return true;
1116}
1117
1118bool ParseIceOptions(const std::string& line,
1119 std::vector<std::string>* transport_options,
1120 SdpParseError* error) {
1121 std::string ice_options;
1122 if (!GetValue(line, kAttributeIceOption, &ice_options, error)) {
1123 return false;
1124 }
1125 std::vector<std::string> fields;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001126 rtc::split(ice_options, kSdpDelimiterSpace, &fields);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001127 for (size_t i = 0; i < fields.size(); ++i) {
1128 transport_options->push_back(fields[i]);
1129 }
1130 return true;
1131}
1132
jiayl@webrtc.orgddb85ab2014-09-05 16:31:56 +00001133bool ParseSctpPort(const std::string& line,
1134 int* sctp_port,
1135 SdpParseError* error) {
1136 // draft-ietf-mmusic-sctp-sdp-07
1137 // a=sctp-port
1138 std::vector<std::string> fields;
jiayl@webrtc.orgddb85ab2014-09-05 16:31:56 +00001139 const size_t expected_min_fields = 2;
lally69f57602015-10-08 10:15:04 -07001140 rtc::split(line.substr(kLinePrefixLength), kSdpDelimiterColon, &fields);
1141 if (fields.size() < expected_min_fields) {
1142 fields.resize(0);
1143 rtc::split(line.substr(kLinePrefixLength), kSdpDelimiterSpace, &fields);
1144 }
jiayl@webrtc.orgddb85ab2014-09-05 16:31:56 +00001145 if (fields.size() < expected_min_fields) {
1146 return ParseFailedExpectMinFieldNum(line, expected_min_fields, error);
1147 }
1148 if (!rtc::FromString(fields[1], sctp_port)) {
lally69f57602015-10-08 10:15:04 -07001149 return ParseFailed(line, "Invalid sctp port value.", error);
jiayl@webrtc.orgddb85ab2014-09-05 16:31:56 +00001150 }
1151 return true;
1152}
1153
isheriff6f8d6862016-05-26 11:24:55 -07001154bool ParseExtmap(const std::string& line,
1155 RtpExtension* extmap,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001156 SdpParseError* error) {
1157 // RFC 5285
1158 // a=extmap:<value>["/"<direction>] <URI> <extensionattributes>
1159 std::vector<std::string> fields;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001160 rtc::split(line.substr(kLinePrefixLength),
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001161 kSdpDelimiterSpace, &fields);
1162 const size_t expected_min_fields = 2;
1163 if (fields.size() < expected_min_fields) {
1164 return ParseFailedExpectMinFieldNum(line, expected_min_fields, error);
1165 }
1166 std::string uri = fields[1];
1167
1168 std::string value_direction;
1169 if (!GetValue(fields[0], kAttributeExtmap, &value_direction, error)) {
1170 return false;
1171 }
1172 std::vector<std::string> sub_fields;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001173 rtc::split(value_direction, kSdpDelimiterSlash, &sub_fields);
wu@webrtc.org5e760e72014-04-02 23:19:09 +00001174 int value = 0;
1175 if (!GetValueFromString(line, sub_fields[0], &value, error)) {
1176 return false;
1177 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001178
jbauch5869f502017-06-29 12:31:36 -07001179 bool encrypted = false;
1180 if (uri == RtpExtension::kEncryptHeaderExtensionsUri) {
1181 // RFC 6904
kjellander88af8b42017-06-29 23:19:31 -07001182 // a=extmap:<value["/"<direction>] urn:ietf:params:rtp-hdrext:encrypt <URI> <extensionattributes>
jbauch5869f502017-06-29 12:31:36 -07001183 const size_t expected_min_fields_encrypted = expected_min_fields + 1;
1184 if (fields.size() < expected_min_fields_encrypted) {
1185 return ParseFailedExpectMinFieldNum(line, expected_min_fields_encrypted,
1186 error);
1187 }
1188
1189 encrypted = true;
1190 uri = fields[2];
1191 if (uri == RtpExtension::kEncryptHeaderExtensionsUri) {
1192 return ParseFailed(line, "Recursive encrypted header.", error);
1193 }
1194 }
1195
1196 *extmap = RtpExtension(uri, value, encrypted);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001197 return true;
1198}
1199
1200void BuildMediaDescription(const ContentInfo* content_info,
1201 const TransportInfo* transport_info,
1202 const MediaType media_type,
wu@webrtc.org4c3e9912014-07-16 21:03:13 +00001203 const std::vector<Candidate>& candidates,
deadbeef9d3584c2016-02-16 17:54:10 -08001204 bool unified_plan_sdp,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001205 std::string* message) {
nisseede5da42017-01-12 05:15:36 -08001206 RTC_DCHECK(message != NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001207 if (content_info == NULL || message == NULL) {
1208 return;
1209 }
1210 // TODO: Rethink if we should use sprintfn instead of stringstream.
1211 // According to the style guide, streams should only be used for logging.
1212 // http://google-styleguide.googlecode.com/svn/
1213 // trunk/cppguide.xml?showone=Streams#Streams
1214 std::ostringstream os;
1215 const MediaContentDescription* media_desc =
henrike@webrtc.org28654cb2013-07-22 21:07:49 +00001216 static_cast<const MediaContentDescription*>(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001217 content_info->description);
nisseede5da42017-01-12 05:15:36 -08001218 RTC_DCHECK(media_desc != NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001219
jiayl@webrtc.org9c16c392014-05-01 18:30:30 +00001220 int sctp_port = cricket::kSctpDefaultPort;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001221
1222 // RFC 4566
1223 // m=<media> <port> <proto> <fmt>
1224 // fmt is a list of payload type numbers that MAY be used in the session.
1225 const char* type = NULL;
1226 if (media_type == cricket::MEDIA_TYPE_AUDIO)
1227 type = kMediaTypeAudio;
1228 else if (media_type == cricket::MEDIA_TYPE_VIDEO)
1229 type = kMediaTypeVideo;
1230 else if (media_type == cricket::MEDIA_TYPE_DATA)
1231 type = kMediaTypeData;
1232 else
nissec80e7412017-01-11 05:56:46 -08001233 RTC_NOTREACHED();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001234
1235 std::string fmt;
1236 if (media_type == cricket::MEDIA_TYPE_VIDEO) {
1237 const VideoContentDescription* video_desc =
1238 static_cast<const VideoContentDescription*>(media_desc);
1239 for (std::vector<cricket::VideoCodec>::const_iterator it =
1240 video_desc->codecs().begin();
1241 it != video_desc->codecs().end(); ++it) {
1242 fmt.append(" ");
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001243 fmt.append(rtc::ToString<int>(it->id));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001244 }
1245 } else if (media_type == cricket::MEDIA_TYPE_AUDIO) {
1246 const AudioContentDescription* audio_desc =
1247 static_cast<const AudioContentDescription*>(media_desc);
1248 for (std::vector<cricket::AudioCodec>::const_iterator it =
1249 audio_desc->codecs().begin();
1250 it != audio_desc->codecs().end(); ++it) {
1251 fmt.append(" ");
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001252 fmt.append(rtc::ToString<int>(it->id));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001253 }
1254 } else if (media_type == cricket::MEDIA_TYPE_DATA) {
jiayl@webrtc.org9c16c392014-05-01 18:30:30 +00001255 const DataContentDescription* data_desc =
1256 static_cast<const DataContentDescription*>(media_desc);
pthatcher@webrtc.org3341b402015-02-13 21:14:22 +00001257 if (IsDtlsSctp(media_desc->protocol())) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001258 fmt.append(" ");
jiayl@webrtc.org9c16c392014-05-01 18:30:30 +00001259
zstein4b2e0822017-02-17 19:48:38 -08001260 if (data_desc->use_sctpmap()) {
1261 for (std::vector<cricket::DataCodec>::const_iterator it =
1262 data_desc->codecs().begin();
1263 it != data_desc->codecs().end(); ++it) {
1264 if (cricket::CodecNamesEq(it->name,
1265 cricket::kGoogleSctpDataCodecName) &&
1266 it->GetParam(cricket::kCodecParamPort, &sctp_port)) {
1267 break;
1268 }
jiayl@webrtc.org9c16c392014-05-01 18:30:30 +00001269 }
jiayl@webrtc.org9c16c392014-05-01 18:30:30 +00001270
zstein4b2e0822017-02-17 19:48:38 -08001271 fmt.append(rtc::ToString<int>(sctp_port));
1272 } else {
1273 fmt.append(kDefaultSctpmapProtocol);
1274 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001275 } else {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001276 for (std::vector<cricket::DataCodec>::const_iterator it =
1277 data_desc->codecs().begin();
1278 it != data_desc->codecs().end(); ++it) {
1279 fmt.append(" ");
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001280 fmt.append(rtc::ToString<int>(it->id));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001281 }
1282 }
1283 }
1284 // The fmt must never be empty. If no codecs are found, set the fmt attribute
1285 // to 0.
1286 if (fmt.empty()) {
1287 fmt = " 0";
1288 }
1289
deadbeef25ed4352016-12-12 18:37:36 -08001290 // The port number in the m line will be updated later when associated with
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001291 // the candidates.
deadbeef25ed4352016-12-12 18:37:36 -08001292 //
1293 // A port value of 0 indicates that the m= section is rejected.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001294 // RFC 3264
1295 // To reject an offered stream, the port number in the corresponding stream in
1296 // the answer MUST be set to zero.
deadbeef25ed4352016-12-12 18:37:36 -08001297 //
1298 // However, the BUNDLE draft adds a new meaning to port zero, when used along
1299 // with a=bundle-only.
zhihuang38989e52017-03-21 11:04:53 -07001300 std::string port = kDummyPort;
1301 if (content_info->rejected || content_info->bundle_only) {
1302 port = kMediaPortRejected;
1303 } else if (!media_desc->connection_address().IsNil()) {
1304 port = rtc::ToString(media_desc->connection_address().port());
1305 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001306
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001307 rtc::SSLFingerprint* fp = (transport_info) ?
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001308 transport_info->description.identity_fingerprint.get() : NULL;
1309
wu@webrtc.org4c3e9912014-07-16 21:03:13 +00001310 // Add the m and c lines.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001311 InitLine(kLineTypeMedia, type, &os);
1312 os << " " << port << " " << media_desc->protocol() << fmt;
zhihuang38989e52017-03-21 11:04:53 -07001313 AddLine(os.str(), message);
1314
1315 InitLine(kLineTypeConnection, kConnectionNettype, &os);
1316 if (media_desc->connection_address().IsNil()) {
1317 os << " " << kConnectionIpv4Addrtype << " " << kDummyAddress;
1318 } else if (media_desc->connection_address().family() == AF_INET) {
1319 os << " " << kConnectionIpv4Addrtype << " "
1320 << media_desc->connection_address().ipaddr().ToString();
1321 } else {
1322 os << " " << kConnectionIpv6Addrtype << " "
1323 << media_desc->connection_address().ipaddr().ToString();
1324 }
1325 AddLine(os.str(), message);
wu@webrtc.org4c3e9912014-07-16 21:03:13 +00001326
1327 // RFC 4566
1328 // b=AS:<bandwidth>
Peter Thatcherc0c3a862015-06-24 15:31:25 -07001329 if (media_desc->bandwidth() >= 1000) {
wu@webrtc.org4c3e9912014-07-16 21:03:13 +00001330 InitLine(kLineTypeSessionBandwidth, kApplicationSpecificMaximum, &os);
1331 os << kSdpDelimiterColon << (media_desc->bandwidth() / 1000);
1332 AddLine(os.str(), message);
1333 }
1334
deadbeef25ed4352016-12-12 18:37:36 -08001335 // Add the a=bundle-only line.
1336 if (content_info->bundle_only) {
1337 InitAttrLine(kAttributeBundleOnly, &os);
1338 AddLine(os.str(), message);
1339 }
1340
wu@webrtc.org4c3e9912014-07-16 21:03:13 +00001341 // Add the a=rtcp line.
pthatcher@webrtc.org3341b402015-02-13 21:14:22 +00001342 if (IsRtp(media_desc->protocol())) {
wu@webrtc.org4c3e9912014-07-16 21:03:13 +00001343 std::string rtcp_line = GetRtcpLine(candidates);
1344 if (!rtcp_line.empty()) {
1345 AddLine(rtcp_line, message);
1346 }
1347 }
1348
honghaiza54a0802015-12-16 18:37:23 -08001349 // Build the a=candidate lines. We don't include ufrag and pwd in the
1350 // candidates in the SDP to avoid redundancy.
1351 BuildCandidate(candidates, false, message);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001352
1353 // Use the transport_info to build the media level ice-ufrag and ice-pwd.
1354 if (transport_info) {
1355 // RFC 5245
1356 // ice-pwd-att = "ice-pwd" ":" password
1357 // ice-ufrag-att = "ice-ufrag" ":" ufrag
1358 // ice-ufrag
deadbeef3f7219b2015-12-28 15:17:14 -08001359 if (!transport_info->description.ice_ufrag.empty()) {
1360 InitAttrLine(kAttributeIceUfrag, &os);
1361 os << kSdpDelimiterColon << transport_info->description.ice_ufrag;
1362 AddLine(os.str(), message);
1363 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001364 // ice-pwd
deadbeef3f7219b2015-12-28 15:17:14 -08001365 if (!transport_info->description.ice_pwd.empty()) {
1366 InitAttrLine(kAttributeIcePwd, &os);
1367 os << kSdpDelimiterColon << transport_info->description.ice_pwd;
1368 AddLine(os.str(), message);
1369 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001370
1371 // draft-petithuguenin-mmusic-ice-attributes-level-03
1372 BuildIceOptions(transport_info->description.transport_options, message);
1373
1374 // RFC 4572
1375 // fingerprint-attribute =
1376 // "fingerprint" ":" hash-func SP fingerprint
1377 if (fp) {
1378 // Insert the fingerprint attribute.
1379 InitAttrLine(kAttributeFingerprint, &os);
1380 os << kSdpDelimiterColon
1381 << fp->algorithm << kSdpDelimiterSpace
1382 << fp->GetRfc4572Fingerprint();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001383 AddLine(os.str(), message);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001384
1385 // Inserting setup attribute.
1386 if (transport_info->description.connection_role !=
1387 cricket::CONNECTIONROLE_NONE) {
1388 // Making sure we are not using "passive" mode.
1389 cricket::ConnectionRole role =
1390 transport_info->description.connection_role;
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00001391 std::string dtls_role_str;
nissec16fa5e2017-02-07 07:18:43 -08001392 const bool success =
1393 cricket::ConnectionRoleToString(role, &dtls_role_str);
1394 RTC_DCHECK(success);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001395 InitAttrLine(kAttributeSetup, &os);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001396 os << kSdpDelimiterColon << dtls_role_str;
1397 AddLine(os.str(), message);
1398 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001399 }
1400 }
1401
1402 // RFC 3388
1403 // mid-attribute = "a=mid:" identification-tag
1404 // identification-tag = token
1405 // Use the content name as the mid identification-tag.
1406 InitAttrLine(kAttributeMid, &os);
1407 os << kSdpDelimiterColon << content_info->name;
1408 AddLine(os.str(), message);
1409
pthatcher@webrtc.org3341b402015-02-13 21:14:22 +00001410 if (IsDtlsSctp(media_desc->protocol())) {
zstein4b2e0822017-02-17 19:48:38 -08001411 const DataContentDescription* data_desc =
1412 static_cast<const DataContentDescription*>(media_desc);
1413 bool use_sctpmap = data_desc->use_sctpmap();
1414 BuildSctpContentAttributes(message, sctp_port, use_sctpmap);
pthatcher@webrtc.org3341b402015-02-13 21:14:22 +00001415 } else if (IsRtp(media_desc->protocol())) {
deadbeef9d3584c2016-02-16 17:54:10 -08001416 BuildRtpContentAttributes(media_desc, media_type, unified_plan_sdp,
1417 message);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001418 }
1419}
1420
zstein4b2e0822017-02-17 19:48:38 -08001421void BuildSctpContentAttributes(std::string* message,
1422 int sctp_port,
1423 bool use_sctpmap) {
wu@webrtc.org78187522013-10-07 23:32:02 +00001424 std::ostringstream os;
zstein4b2e0822017-02-17 19:48:38 -08001425 if (use_sctpmap) {
1426 // draft-ietf-mmusic-sctp-sdp-04
1427 // a=sctpmap:sctpmap-number protocol [streams]
1428 InitAttrLine(kAttributeSctpmap, &os);
1429 os << kSdpDelimiterColon << sctp_port << kSdpDelimiterSpace
1430 << kDefaultSctpmapProtocol << kSdpDelimiterSpace
1431 << cricket::kMaxSctpStreams;
1432 } else {
1433 // draft-ietf-mmusic-sctp-sdp-23
1434 // a=sctp-port:<port>
1435 InitAttrLine(kAttributeSctpPort, &os);
1436 os << kSdpDelimiterColon << sctp_port;
1437 // TODO(zstein): emit max-message-size here
1438 }
wu@webrtc.org78187522013-10-07 23:32:02 +00001439 AddLine(os.str(), message);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001440}
1441
deadbeef9d3584c2016-02-16 17:54:10 -08001442// If unified_plan_sdp is true, will use "a=msid".
1443void BuildRtpContentAttributes(const MediaContentDescription* media_desc,
1444 const MediaType media_type,
1445 bool unified_plan_sdp,
1446 std::string* message) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001447 std::ostringstream os;
1448 // RFC 5285
1449 // a=extmap:<value>["/"<direction>] <URI> <extensionattributes>
1450 // The definitions MUST be either all session level or all media level. This
1451 // implementation uses all media level.
1452 for (size_t i = 0; i < media_desc->rtp_header_extensions().size(); ++i) {
jbauch5869f502017-06-29 12:31:36 -07001453 const RtpExtension& extension = media_desc->rtp_header_extensions()[i];
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001454 InitAttrLine(kAttributeExtmap, &os);
jbauch5869f502017-06-29 12:31:36 -07001455 os << kSdpDelimiterColon << extension.id;
1456 if (extension.encrypt) {
1457 os << kSdpDelimiterSpace << RtpExtension::kEncryptHeaderExtensionsUri;
1458 }
1459 os << kSdpDelimiterSpace << extension.uri;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001460 AddLine(os.str(), message);
1461 }
1462
1463 // RFC 3264
1464 // a=sendrecv || a=sendonly || a=sendrecv || a=inactive
deadbeefc80741f2015-10-22 13:14:45 -07001465 switch (media_desc->direction()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001466 case cricket::MD_INACTIVE:
1467 InitAttrLine(kAttributeInactive, &os);
1468 break;
1469 case cricket::MD_SENDONLY:
1470 InitAttrLine(kAttributeSendOnly, &os);
1471 break;
1472 case cricket::MD_RECVONLY:
1473 InitAttrLine(kAttributeRecvOnly, &os);
1474 break;
1475 case cricket::MD_SENDRECV:
1476 default:
1477 InitAttrLine(kAttributeSendRecv, &os);
1478 break;
1479 }
1480 AddLine(os.str(), message);
1481
deadbeef9d3584c2016-02-16 17:54:10 -08001482 // draft-ietf-mmusic-msid-11
1483 // a=msid:<stream id> <track id>
1484 if (unified_plan_sdp && !media_desc->streams().empty()) {
1485 if (media_desc->streams().size() > 1u) {
1486 LOG(LS_WARNING) << "Trying to serialize unified plan SDP with more than "
1487 << "one track in a media section. Omitting 'a=msid'.";
1488 } else {
1489 auto track = media_desc->streams().begin();
1490 const std::string& stream_id = track->sync_label;
deadbeef9d3584c2016-02-16 17:54:10 -08001491 InitAttrLine(kAttributeMsid, &os);
1492 os << kSdpDelimiterColon << stream_id << kSdpDelimiterSpace << track->id;
1493 AddLine(os.str(), message);
1494 }
1495 }
1496
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001497 // RFC 5761
1498 // a=rtcp-mux
1499 if (media_desc->rtcp_mux()) {
1500 InitAttrLine(kAttributeRtcpMux, &os);
1501 AddLine(os.str(), message);
1502 }
1503
deadbeef13871492015-12-09 12:37:51 -08001504 // RFC 5506
1505 // a=rtcp-rsize
1506 if (media_desc->rtcp_reduced_size()) {
1507 InitAttrLine(kAttributeRtcpReducedSize, &os);
1508 AddLine(os.str(), message);
1509 }
1510
deadbeefd45aea82017-09-16 01:24:29 -07001511 if (media_desc->conference_mode()) {
1512 InitAttrLine(kAttributeXGoogleFlag, &os);
1513 os << kSdpDelimiterColon << kValueConference;
1514 AddLine(os.str(), message);
1515 }
1516
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001517 // RFC 4568
1518 // a=crypto:<tag> <crypto-suite> <key-params> [<session-params>]
1519 for (std::vector<CryptoParams>::const_iterator it =
1520 media_desc->cryptos().begin();
1521 it != media_desc->cryptos().end(); ++it) {
1522 InitAttrLine(kAttributeCrypto, &os);
1523 os << kSdpDelimiterColon << it->tag << " " << it->cipher_suite << " "
1524 << it->key_params;
1525 if (!it->session_params.empty()) {
1526 os << " " << it->session_params;
1527 }
1528 AddLine(os.str(), message);
1529 }
1530
1531 // RFC 4566
1532 // a=rtpmap:<payload type> <encoding name>/<clock rate>
1533 // [/<encodingparameters>]
1534 BuildRtpMap(media_desc, media_type, message);
1535
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001536 for (StreamParamsVec::const_iterator track = media_desc->streams().begin();
1537 track != media_desc->streams().end(); ++track) {
1538 // Require that the track belongs to a media stream,
1539 // ie the sync_label is set. This extra check is necessary since the
1540 // MediaContentDescription always contains a streamparam with an ssrc even
1541 // if no track or media stream have been created.
1542 if (track->sync_label.empty()) continue;
1543
1544 // Build the ssrc-group lines.
1545 for (size_t i = 0; i < track->ssrc_groups.size(); ++i) {
1546 // RFC 5576
1547 // a=ssrc-group:<semantics> <ssrc-id> ...
1548 if (track->ssrc_groups[i].ssrcs.empty()) {
1549 continue;
1550 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001551 InitAttrLine(kAttributeSsrcGroup, &os);
1552 os << kSdpDelimiterColon << track->ssrc_groups[i].semantics;
Peter Boström0c4e06b2015-10-07 12:23:21 +02001553 std::vector<uint32_t>::const_iterator ssrc =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001554 track->ssrc_groups[i].ssrcs.begin();
1555 for (; ssrc != track->ssrc_groups[i].ssrcs.end(); ++ssrc) {
Peter Boström0c4e06b2015-10-07 12:23:21 +02001556 os << kSdpDelimiterSpace << rtc::ToString<uint32_t>(*ssrc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001557 }
1558 AddLine(os.str(), message);
1559 }
1560 // Build the ssrc lines for each ssrc.
1561 for (size_t i = 0; i < track->ssrcs.size(); ++i) {
Peter Boström0c4e06b2015-10-07 12:23:21 +02001562 uint32_t ssrc = track->ssrcs[i];
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001563 // RFC 5576
1564 // a=ssrc:<ssrc-id> cname:<value>
1565 AddSsrcLine(ssrc, kSsrcAttributeCname,
1566 track->cname, message);
1567
1568 // draft-alvestrand-mmusic-msid-00
1569 // a=ssrc:<ssrc-id> msid:identifier [appdata]
deadbeef9d3584c2016-02-16 17:54:10 -08001570 // The appdata consists of the "id" attribute of a MediaStreamTrack,
1571 // which corresponds to the "id" attribute of StreamParams.
1572 const std::string& stream_id = track->sync_label;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001573 InitAttrLine(kAttributeSsrc, &os);
1574 os << kSdpDelimiterColon << ssrc << kSdpDelimiterSpace
deadbeef9d3584c2016-02-16 17:54:10 -08001575 << kSsrcAttributeMsid << kSdpDelimiterColon << stream_id
1576 << kSdpDelimiterSpace << track->id;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001577 AddLine(os.str(), message);
1578
deadbeef9d3584c2016-02-16 17:54:10 -08001579 // TODO(ronghuawu): Remove below code which is for backward
1580 // compatibility.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001581 // draft-alvestrand-rtcweb-mid-01
1582 // a=ssrc:<ssrc-id> mslabel:<value>
1583 // The label isn't yet defined.
1584 // a=ssrc:<ssrc-id> label:<value>
1585 AddSsrcLine(ssrc, kSsrcAttributeMslabel, track->sync_label, message);
1586 AddSsrcLine(ssrc, kSSrcAttributeLabel, track->id, message);
1587 }
1588 }
1589}
1590
1591void WriteFmtpHeader(int payload_type, std::ostringstream* os) {
1592 // fmtp header: a=fmtp:|payload_type| <parameters>
1593 // Add a=fmtp
1594 InitAttrLine(kAttributeFmtp, os);
1595 // Add :|payload_type|
1596 *os << kSdpDelimiterColon << payload_type;
1597}
1598
1599void WriteRtcpFbHeader(int payload_type, std::ostringstream* os) {
1600 // rtcp-fb header: a=rtcp-fb:|payload_type|
1601 // <parameters>/<ccm <ccm_parameters>>
1602 // Add a=rtcp-fb
1603 InitAttrLine(kAttributeRtcpFb, os);
1604 // Add :
1605 *os << kSdpDelimiterColon;
1606 if (payload_type == kWildcardPayloadType) {
1607 *os << "*";
1608 } else {
1609 *os << payload_type;
1610 }
1611}
1612
1613void WriteFmtpParameter(const std::string& parameter_name,
1614 const std::string& parameter_value,
1615 std::ostringstream* os) {
1616 // fmtp parameters: |parameter_name|=|parameter_value|
1617 *os << parameter_name << kSdpDelimiterEqual << parameter_value;
1618}
1619
1620void WriteFmtpParameters(const cricket::CodecParameterMap& parameters,
1621 std::ostringstream* os) {
1622 for (cricket::CodecParameterMap::const_iterator fmtp = parameters.begin();
1623 fmtp != parameters.end(); ++fmtp) {
hta62a216e2016-04-15 11:02:14 -07001624 // Parameters are a semicolon-separated list, no spaces.
1625 // The list is separated from the header by a space.
1626 if (fmtp == parameters.begin()) {
1627 *os << kSdpDelimiterSpace;
1628 } else {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001629 *os << kSdpDelimiterSemicolon;
1630 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001631 WriteFmtpParameter(fmtp->first, fmtp->second, os);
1632 }
1633}
1634
1635bool IsFmtpParam(const std::string& name) {
ossuaa4b0772017-01-30 07:41:18 -08001636 // RFC 4855, section 3 specifies the mapping of media format parameters to SDP
1637 // parameters. Only ptime, maxptime, channels and rate are placed outside of
1638 // the fmtp line. In WebRTC, channels and rate are already handled separately
1639 // and thus not included in the CodecParameterMap.
1640 return name != kCodecParamPTime && name != kCodecParamMaxPTime;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001641}
1642
1643// Retreives fmtp parameters from |params|, which may contain other parameters
1644// as well, and puts them in |fmtp_parameters|.
1645void GetFmtpParams(const cricket::CodecParameterMap& params,
1646 cricket::CodecParameterMap* fmtp_parameters) {
1647 for (cricket::CodecParameterMap::const_iterator iter = params.begin();
1648 iter != params.end(); ++iter) {
1649 if (IsFmtpParam(iter->first)) {
1650 (*fmtp_parameters)[iter->first] = iter->second;
1651 }
1652 }
1653}
1654
1655template <class T>
1656void AddFmtpLine(const T& codec, std::string* message) {
1657 cricket::CodecParameterMap fmtp_parameters;
1658 GetFmtpParams(codec.params, &fmtp_parameters);
1659 if (fmtp_parameters.empty()) {
1660 // No need to add an fmtp if it will have no (optional) parameters.
1661 return;
1662 }
1663 std::ostringstream os;
1664 WriteFmtpHeader(codec.id, &os);
1665 WriteFmtpParameters(fmtp_parameters, &os);
1666 AddLine(os.str(), message);
1667 return;
1668}
1669
1670template <class T>
1671void AddRtcpFbLines(const T& codec, std::string* message) {
1672 for (std::vector<cricket::FeedbackParam>::const_iterator iter =
1673 codec.feedback_params.params().begin();
1674 iter != codec.feedback_params.params().end(); ++iter) {
1675 std::ostringstream os;
1676 WriteRtcpFbHeader(codec.id, &os);
1677 os << " " << iter->id();
1678 if (!iter->param().empty()) {
1679 os << " " << iter->param();
1680 }
1681 AddLine(os.str(), message);
1682 }
1683}
1684
jiayl@webrtc.orgddb85ab2014-09-05 16:31:56 +00001685bool AddSctpDataCodec(DataContentDescription* media_desc,
1686 int sctp_port) {
solenberg9fa49752016-10-08 13:02:44 -07001687 for (const auto& codec : media_desc->codecs()) {
1688 if (cricket::CodecNamesEq(codec.name, cricket::kGoogleSctpDataCodecName)) {
1689 return ParseFailed("",
1690 "Can't have multiple sctp port attributes.",
1691 NULL);
1692 }
jiayl@webrtc.orgddb85ab2014-09-05 16:31:56 +00001693 }
1694 // Add the SCTP Port number as a pseudo-codec "port" parameter
solenberg9fa49752016-10-08 13:02:44 -07001695 cricket::DataCodec codec_port(cricket::kGoogleSctpDataCodecPlType,
deadbeef67cf2c12016-04-13 10:07:16 -07001696 cricket::kGoogleSctpDataCodecName);
jiayl@webrtc.orgddb85ab2014-09-05 16:31:56 +00001697 codec_port.SetParam(cricket::kCodecParamPort, sctp_port);
1698 LOG(INFO) << "AddSctpDataCodec: Got SCTP Port Number "
1699 << sctp_port;
1700 media_desc->AddCodec(codec_port);
1701 return true;
1702}
1703
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001704bool GetMinValue(const std::vector<int>& values, int* value) {
1705 if (values.empty()) {
1706 return false;
1707 }
1708 std::vector<int>::const_iterator found =
1709 std::min_element(values.begin(), values.end());
1710 *value = *found;
1711 return true;
1712}
1713
1714bool GetParameter(const std::string& name,
1715 const cricket::CodecParameterMap& params, int* value) {
1716 std::map<std::string, std::string>::const_iterator found =
1717 params.find(name);
1718 if (found == params.end()) {
1719 return false;
1720 }
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001721 if (!rtc::FromString(found->second, value)) {
wu@webrtc.org5e760e72014-04-02 23:19:09 +00001722 return false;
1723 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001724 return true;
1725}
1726
1727void BuildRtpMap(const MediaContentDescription* media_desc,
1728 const MediaType media_type,
1729 std::string* message) {
nisseede5da42017-01-12 05:15:36 -08001730 RTC_DCHECK(message != NULL);
1731 RTC_DCHECK(media_desc != NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001732 std::ostringstream os;
1733 if (media_type == cricket::MEDIA_TYPE_VIDEO) {
1734 const VideoContentDescription* video_desc =
1735 static_cast<const VideoContentDescription*>(media_desc);
1736 for (std::vector<cricket::VideoCodec>::const_iterator it =
1737 video_desc->codecs().begin();
1738 it != video_desc->codecs().end(); ++it) {
1739 // RFC 4566
1740 // a=rtpmap:<payload type> <encoding name>/<clock rate>
1741 // [/<encodingparameters>]
1742 if (it->id != kWildcardPayloadType) {
1743 InitAttrLine(kAttributeRtpmap, &os);
deadbeefe814a0d2017-02-25 18:15:09 -08001744 os << kSdpDelimiterColon << it->id << " " << it->name << "/"
1745 << cricket::kVideoCodecClockrate;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001746 AddLine(os.str(), message);
1747 }
1748 AddRtcpFbLines(*it, message);
1749 AddFmtpLine(*it, message);
1750 }
1751 } else if (media_type == cricket::MEDIA_TYPE_AUDIO) {
1752 const AudioContentDescription* audio_desc =
1753 static_cast<const AudioContentDescription*>(media_desc);
1754 std::vector<int> ptimes;
1755 std::vector<int> maxptimes;
1756 int max_minptime = 0;
1757 for (std::vector<cricket::AudioCodec>::const_iterator it =
1758 audio_desc->codecs().begin();
1759 it != audio_desc->codecs().end(); ++it) {
nisseede5da42017-01-12 05:15:36 -08001760 RTC_DCHECK(!it->name.empty());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001761 // RFC 4566
1762 // a=rtpmap:<payload type> <encoding name>/<clock rate>
1763 // [/<encodingparameters>]
1764 InitAttrLine(kAttributeRtpmap, &os);
1765 os << kSdpDelimiterColon << it->id << " ";
1766 os << it->name << "/" << it->clockrate;
1767 if (it->channels != 1) {
1768 os << "/" << it->channels;
1769 }
1770 AddLine(os.str(), message);
1771 AddRtcpFbLines(*it, message);
1772 AddFmtpLine(*it, message);
1773 int minptime = 0;
1774 if (GetParameter(kCodecParamMinPTime, it->params, &minptime)) {
1775 max_minptime = std::max(minptime, max_minptime);
1776 }
1777 int ptime;
1778 if (GetParameter(kCodecParamPTime, it->params, &ptime)) {
1779 ptimes.push_back(ptime);
1780 }
1781 int maxptime;
1782 if (GetParameter(kCodecParamMaxPTime, it->params, &maxptime)) {
1783 maxptimes.push_back(maxptime);
1784 }
1785 }
1786 // Populate the maxptime attribute with the smallest maxptime of all codecs
1787 // under the same m-line.
1788 int min_maxptime = INT_MAX;
1789 if (GetMinValue(maxptimes, &min_maxptime)) {
1790 AddAttributeLine(kCodecParamMaxPTime, min_maxptime, message);
1791 }
nisseede5da42017-01-12 05:15:36 -08001792 RTC_DCHECK(min_maxptime > max_minptime);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001793 // Populate the ptime attribute with the smallest ptime or the largest
1794 // minptime, whichever is the largest, for all codecs under the same m-line.
1795 int ptime = INT_MAX;
1796 if (GetMinValue(ptimes, &ptime)) {
1797 ptime = std::min(ptime, min_maxptime);
1798 ptime = std::max(ptime, max_minptime);
1799 AddAttributeLine(kCodecParamPTime, ptime, message);
1800 }
1801 } else if (media_type == cricket::MEDIA_TYPE_DATA) {
1802 const DataContentDescription* data_desc =
1803 static_cast<const DataContentDescription*>(media_desc);
1804 for (std::vector<cricket::DataCodec>::const_iterator it =
1805 data_desc->codecs().begin();
1806 it != data_desc->codecs().end(); ++it) {
1807 // RFC 4566
1808 // a=rtpmap:<payload type> <encoding name>/<clock rate>
1809 // [/<encodingparameters>]
1810 InitAttrLine(kAttributeRtpmap, &os);
1811 os << kSdpDelimiterColon << it->id << " "
1812 << it->name << "/" << it->clockrate;
1813 AddLine(os.str(), message);
1814 }
1815 }
1816}
1817
1818void BuildCandidate(const std::vector<Candidate>& candidates,
honghaiza54a0802015-12-16 18:37:23 -08001819 bool include_ufrag,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001820 std::string* message) {
1821 std::ostringstream os;
1822
1823 for (std::vector<Candidate>::const_iterator it = candidates.begin();
1824 it != candidates.end(); ++it) {
1825 // RFC 5245
1826 // a=candidate:<foundation> <component-id> <transport> <priority>
1827 // <connection-address> <port> typ <candidate-types>
1828 // [raddr <connection-address>] [rport <port>]
1829 // *(SP extension-att-name SP extension-att-value)
1830 std::string type;
1831 // Map the cricket candidate type to "host" / "srflx" / "prflx" / "relay"
1832 if (it->type() == cricket::LOCAL_PORT_TYPE) {
1833 type = kCandidateHost;
1834 } else if (it->type() == cricket::STUN_PORT_TYPE) {
1835 type = kCandidateSrflx;
1836 } else if (it->type() == cricket::RELAY_PORT_TYPE) {
1837 type = kCandidateRelay;
Honghai Zhang7fb69db2016-03-14 11:59:18 -07001838 } else if (it->type() == cricket::PRFLX_PORT_TYPE) {
1839 type = kCandidatePrflx;
1840 // Peer reflexive candidate may be signaled for being removed.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001841 } else {
nissec80e7412017-01-11 05:56:46 -08001842 RTC_NOTREACHED();
Peter Thatcher019087f2015-04-28 09:06:26 -07001843 // Never write out candidates if we don't know the type.
1844 continue;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001845 }
1846
1847 InitAttrLine(kAttributeCandidate, &os);
1848 os << kSdpDelimiterColon
mallinath@webrtc.org2d60c5e2014-08-08 22:29:20 +00001849 << it->foundation() << " "
1850 << it->component() << " "
1851 << it->protocol() << " "
1852 << it->priority() << " "
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001853 << it->address().ipaddr().ToString() << " "
1854 << it->address().PortAsString() << " "
mallinath@webrtc.org2d60c5e2014-08-08 22:29:20 +00001855 << kAttributeCandidateTyp << " "
1856 << type << " ";
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001857
1858 // Related address
1859 if (!it->related_address().IsNil()) {
1860 os << kAttributeCandidateRaddr << " "
1861 << it->related_address().ipaddr().ToString() << " "
1862 << kAttributeCandidateRport << " "
1863 << it->related_address().PortAsString() << " ";
1864 }
1865
mallinath@webrtc.org2d60c5e2014-08-08 22:29:20 +00001866 if (it->protocol() == cricket::TCP_PROTOCOL_NAME) {
mallinath@webrtc.orge999bd02014-08-13 06:05:55 +00001867 os << kTcpCandidateType << " " << it->tcptype() << " ";
mallinath@webrtc.org2d60c5e2014-08-08 22:29:20 +00001868 }
1869
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001870 // Extensions
1871 os << kAttributeCandidateGeneration << " " << it->generation();
honghaiza54a0802015-12-16 18:37:23 -08001872 if (include_ufrag && !it->username().empty()) {
1873 os << " " << kAttributeCandidateUfrag << " " << it->username();
1874 }
honghaiza0c44ea2016-03-23 16:07:48 -07001875 if (it->network_id() > 0) {
1876 os << " " << kAttributeCandidateNetworkId << " " << it->network_id();
1877 }
honghaize1a0c942016-02-16 14:54:56 -08001878 if (it->network_cost() > 0) {
1879 os << " " << kAttributeCandidateNetworkCost << " " << it->network_cost();
1880 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001881
1882 AddLine(os.str(), message);
1883 }
1884}
1885
1886void BuildIceOptions(const std::vector<std::string>& transport_options,
1887 std::string* message) {
1888 if (!transport_options.empty()) {
1889 std::ostringstream os;
1890 InitAttrLine(kAttributeIceOption, &os);
1891 os << kSdpDelimiterColon << transport_options[0];
1892 for (size_t i = 1; i < transport_options.size(); ++i) {
1893 os << kSdpDelimiterSpace << transport_options[i];
1894 }
1895 AddLine(os.str(), message);
1896 }
1897}
1898
pthatcher@webrtc.org3341b402015-02-13 21:14:22 +00001899bool IsRtp(const std::string& protocol) {
1900 return protocol.empty() ||
1901 (protocol.find(cricket::kMediaProtocolRtpPrefix) != std::string::npos);
1902}
1903
1904bool IsDtlsSctp(const std::string& protocol) {
1905 // This intentionally excludes "SCTP" and "SCTP/DTLS".
lally@webrtc.orga7470932015-02-24 20:19:21 +00001906 return protocol.find(cricket::kMediaProtocolDtlsSctp) != std::string::npos;
pthatcher@webrtc.org3341b402015-02-13 21:14:22 +00001907}
1908
zhihuang38989e52017-03-21 11:04:53 -07001909bool ParseConnectionData(const std::string& line,
1910 rtc::SocketAddress* addr,
1911 SdpParseError* error) {
1912 // Parse the line from left to right.
1913 std::string token;
1914 std::string rightpart;
1915 // RFC 4566
1916 // c=<nettype> <addrtype> <connection-address>
1917 // Skip the "c="
1918 if (!rtc::tokenize_first(line, kSdpDelimiterEqual, &token, &rightpart)) {
1919 return ParseFailed(line, "Failed to parse the network type.", error);
1920 }
1921
1922 // Extract and verify the <nettype>
1923 if (!rtc::tokenize_first(rightpart, kSdpDelimiterSpace, &token, &rightpart) ||
1924 token != kConnectionNettype) {
1925 return ParseFailed(line,
1926 "Failed to parse the connection data. The network type "
1927 "is not currently supported.",
1928 error);
1929 }
1930
1931 // Extract the "<addrtype>" and "<connection-address>".
1932 if (!rtc::tokenize_first(rightpart, kSdpDelimiterSpace, &token, &rightpart)) {
1933 return ParseFailed(line, "Failed to parse the address type.", error);
1934 }
1935
1936 // The rightpart part should be the IP address without the slash which is used
1937 // for multicast.
1938 if (rightpart.find('/') != std::string::npos) {
1939 return ParseFailed(line,
1940 "Failed to parse the connection data. Multicast is not "
1941 "currently supported.",
1942 error);
1943 }
1944 addr->SetIP(rightpart);
1945
1946 // Verify that the addrtype matches the type of the parsed address.
1947 if ((addr->family() == AF_INET && token != "IP4") ||
1948 (addr->family() == AF_INET6 && token != "IP6")) {
1949 addr->Clear();
1950 return ParseFailed(
1951 line,
1952 "Failed to parse the connection data. The address type is mismatching.",
1953 error);
1954 }
1955 return true;
1956}
1957
1958bool ParseSessionDescription(const std::string& message,
1959 size_t* pos,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001960 std::string* session_id,
1961 std::string* session_version,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001962 TransportDescription* session_td,
1963 RtpHeaderExtensions* session_extmaps,
zhihuang38989e52017-03-21 11:04:53 -07001964 rtc::SocketAddress* connection_addr,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001965 cricket::SessionDescription* desc,
1966 SdpParseError* error) {
1967 std::string line;
1968
deadbeefc80741f2015-10-22 13:14:45 -07001969 desc->set_msid_supported(false);
1970
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001971 // RFC 4566
1972 // v= (protocol version)
1973 if (!GetLineWithType(message, pos, &line, kLineTypeVersion)) {
1974 return ParseFailedExpectLine(message, *pos, kLineTypeVersion,
1975 std::string(), error);
1976 }
1977 // RFC 4566
1978 // o=<username> <sess-id> <sess-version> <nettype> <addrtype>
1979 // <unicast-address>
1980 if (!GetLineWithType(message, pos, &line, kLineTypeOrigin)) {
1981 return ParseFailedExpectLine(message, *pos, kLineTypeOrigin,
1982 std::string(), error);
1983 }
1984 std::vector<std::string> fields;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001985 rtc::split(line.substr(kLinePrefixLength),
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001986 kSdpDelimiterSpace, &fields);
1987 const size_t expected_fields = 6;
1988 if (fields.size() != expected_fields) {
1989 return ParseFailedExpectFieldNum(line, expected_fields, error);
1990 }
1991 *session_id = fields[1];
1992 *session_version = fields[2];
1993
1994 // RFC 4566
1995 // s= (session name)
1996 if (!GetLineWithType(message, pos, &line, kLineTypeSessionName)) {
1997 return ParseFailedExpectLine(message, *pos, kLineTypeSessionName,
1998 std::string(), error);
1999 }
2000
2001 // Optional lines
2002 // Those are the optional lines, so shouldn't return false if not present.
2003 // RFC 4566
2004 // i=* (session information)
2005 GetLineWithType(message, pos, &line, kLineTypeSessionInfo);
2006
2007 // RFC 4566
2008 // u=* (URI of description)
2009 GetLineWithType(message, pos, &line, kLineTypeSessionUri);
2010
2011 // RFC 4566
2012 // e=* (email address)
2013 GetLineWithType(message, pos, &line, kLineTypeSessionEmail);
2014
2015 // RFC 4566
2016 // p=* (phone number)
2017 GetLineWithType(message, pos, &line, kLineTypeSessionPhone);
2018
2019 // RFC 4566
2020 // c=* (connection information -- not required if included in
2021 // all media)
zhihuang38989e52017-03-21 11:04:53 -07002022 if (GetLineWithType(message, pos, &line, kLineTypeConnection)) {
2023 if (!ParseConnectionData(line, connection_addr, error)) {
2024 return false;
2025 }
2026 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002027
2028 // RFC 4566
2029 // b=* (zero or more bandwidth information lines)
2030 while (GetLineWithType(message, pos, &line, kLineTypeSessionBandwidth)) {
2031 // By pass zero or more b lines.
2032 }
2033
2034 // RFC 4566
2035 // One or more time descriptions ("t=" and "r=" lines; see below)
2036 // t= (time the session is active)
2037 // r=* (zero or more repeat times)
2038 // Ensure there's at least one time description
2039 if (!GetLineWithType(message, pos, &line, kLineTypeTiming)) {
2040 return ParseFailedExpectLine(message, *pos, kLineTypeTiming, std::string(),
2041 error);
2042 }
2043
2044 while (GetLineWithType(message, pos, &line, kLineTypeRepeatTimes)) {
2045 // By pass zero or more r lines.
2046 }
2047
2048 // Go through the rest of the time descriptions
2049 while (GetLineWithType(message, pos, &line, kLineTypeTiming)) {
2050 while (GetLineWithType(message, pos, &line, kLineTypeRepeatTimes)) {
2051 // By pass zero or more r lines.
2052 }
2053 }
2054
2055 // RFC 4566
2056 // z=* (time zone adjustments)
2057 GetLineWithType(message, pos, &line, kLineTypeTimeZone);
2058
2059 // RFC 4566
2060 // k=* (encryption key)
2061 GetLineWithType(message, pos, &line, kLineTypeEncryptionKey);
2062
2063 // RFC 4566
2064 // a=* (zero or more session attribute lines)
2065 while (GetLineWithType(message, pos, &line, kLineTypeAttributes)) {
2066 if (HasAttribute(line, kAttributeGroup)) {
2067 if (!ParseGroupAttribute(line, desc, error)) {
2068 return false;
2069 }
2070 } else if (HasAttribute(line, kAttributeIceUfrag)) {
2071 if (!GetValue(line, kAttributeIceUfrag,
2072 &(session_td->ice_ufrag), error)) {
2073 return false;
2074 }
2075 } else if (HasAttribute(line, kAttributeIcePwd)) {
2076 if (!GetValue(line, kAttributeIcePwd, &(session_td->ice_pwd), error)) {
2077 return false;
2078 }
2079 } else if (HasAttribute(line, kAttributeIceLite)) {
2080 session_td->ice_mode = cricket::ICEMODE_LITE;
2081 } else if (HasAttribute(line, kAttributeIceOption)) {
2082 if (!ParseIceOptions(line, &(session_td->transport_options), error)) {
2083 return false;
2084 }
2085 } else if (HasAttribute(line, kAttributeFingerprint)) {
2086 if (session_td->identity_fingerprint.get()) {
2087 return ParseFailed(
2088 line,
2089 "Can't have multiple fingerprint attributes at the same level.",
2090 error);
2091 }
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002092 rtc::SSLFingerprint* fingerprint = NULL;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002093 if (!ParseFingerprintAttribute(line, &fingerprint, error)) {
2094 return false;
2095 }
2096 session_td->identity_fingerprint.reset(fingerprint);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00002097 } else if (HasAttribute(line, kAttributeSetup)) {
2098 if (!ParseDtlsSetup(line, &(session_td->connection_role), error)) {
2099 return false;
2100 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002101 } else if (HasAttribute(line, kAttributeMsidSemantics)) {
2102 std::string semantics;
2103 if (!GetValue(line, kAttributeMsidSemantics, &semantics, error)) {
2104 return false;
2105 }
deadbeefc80741f2015-10-22 13:14:45 -07002106 desc->set_msid_supported(
2107 CaseInsensitiveFind(semantics, kMediaStreamSemantic));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002108 } else if (HasAttribute(line, kAttributeExtmap)) {
isheriff6f8d6862016-05-26 11:24:55 -07002109 RtpExtension extmap;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002110 if (!ParseExtmap(line, &extmap, error)) {
2111 return false;
2112 }
2113 session_extmaps->push_back(extmap);
2114 }
2115 }
2116
2117 return true;
2118}
2119
2120bool ParseGroupAttribute(const std::string& line,
2121 cricket::SessionDescription* desc,
2122 SdpParseError* error) {
nisseede5da42017-01-12 05:15:36 -08002123 RTC_DCHECK(desc != NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002124
2125 // RFC 5888 and draft-holmberg-mmusic-sdp-bundle-negotiation-00
2126 // a=group:BUNDLE video voice
2127 std::vector<std::string> fields;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002128 rtc::split(line.substr(kLinePrefixLength),
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002129 kSdpDelimiterSpace, &fields);
2130 std::string semantics;
2131 if (!GetValue(fields[0], kAttributeGroup, &semantics, error)) {
2132 return false;
2133 }
2134 cricket::ContentGroup group(semantics);
2135 for (size_t i = 1; i < fields.size(); ++i) {
2136 group.AddContentName(fields[i]);
2137 }
2138 desc->AddGroup(group);
2139 return true;
2140}
2141
2142static bool ParseFingerprintAttribute(const std::string& line,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002143 rtc::SSLFingerprint** fingerprint,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002144 SdpParseError* error) {
2145 if (!IsLineType(line, kLineTypeAttributes) ||
2146 !HasAttribute(line, kAttributeFingerprint)) {
2147 return ParseFailedExpectLine(line, 0, kLineTypeAttributes,
2148 kAttributeFingerprint, error);
2149 }
2150
2151 std::vector<std::string> fields;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002152 rtc::split(line.substr(kLinePrefixLength),
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002153 kSdpDelimiterSpace, &fields);
2154 const size_t expected_fields = 2;
2155 if (fields.size() != expected_fields) {
2156 return ParseFailedExpectFieldNum(line, expected_fields, error);
2157 }
2158
2159 // The first field here is "fingerprint:<hash>.
2160 std::string algorithm;
2161 if (!GetValue(fields[0], kAttributeFingerprint, &algorithm, error)) {
2162 return false;
2163 }
2164
2165 // Downcase the algorithm. Note that we don't need to downcase the
2166 // fingerprint because hex_decode can handle upper-case.
2167 std::transform(algorithm.begin(), algorithm.end(), algorithm.begin(),
2168 ::tolower);
2169
2170 // The second field is the digest value. De-hexify it.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002171 *fingerprint = rtc::SSLFingerprint::CreateFromRfc4572(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002172 algorithm, fields[1]);
2173 if (!*fingerprint) {
2174 return ParseFailed(line,
2175 "Failed to create fingerprint from the digest.",
2176 error);
2177 }
2178
2179 return true;
2180}
2181
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00002182static bool ParseDtlsSetup(const std::string& line,
2183 cricket::ConnectionRole* role,
2184 SdpParseError* error) {
2185 // setup-attr = "a=setup:" role
2186 // role = "active" / "passive" / "actpass" / "holdconn"
2187 std::vector<std::string> fields;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002188 rtc::split(line.substr(kLinePrefixLength), kSdpDelimiterColon, &fields);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00002189 const size_t expected_fields = 2;
2190 if (fields.size() != expected_fields) {
2191 return ParseFailedExpectFieldNum(line, expected_fields, error);
2192 }
2193 std::string role_str = fields[1];
2194 if (!cricket::StringToConnectionRole(role_str, role)) {
2195 return ParseFailed(line, "Invalid attribute value.", error);
2196 }
2197 return true;
2198}
2199
deadbeef9d3584c2016-02-16 17:54:10 -08002200static bool ParseMsidAttribute(const std::string& line,
2201 std::string* stream_id,
2202 std::string* track_id,
2203 SdpParseError* error) {
2204 // draft-ietf-mmusic-msid-11
2205 // a=msid:<stream id> <track id>
2206 // msid-value = msid-id [ SP msid-appdata ]
2207 // msid-id = 1*64token-char ; see RFC 4566
2208 // msid-appdata = 1*64token-char ; see RFC 4566
2209 std::string field1;
2210 if (!rtc::tokenize_first(line.substr(kLinePrefixLength), kSdpDelimiterSpace,
2211 &field1, track_id)) {
2212 const size_t expected_fields = 2;
2213 return ParseFailedExpectFieldNum(line, expected_fields, error);
2214 }
2215
deadbeefa4549d62017-02-10 17:26:22 -08002216 if (track_id->empty()) {
2217 return ParseFailed(line, "Missing track ID in msid attribute.", error);
2218 }
2219
deadbeef9d3584c2016-02-16 17:54:10 -08002220 // msid:<msid-id>
2221 if (!GetValue(field1, kAttributeMsid, stream_id, error)) {
2222 return false;
2223 }
deadbeefa4549d62017-02-10 17:26:22 -08002224 if (stream_id->empty()) {
2225 return ParseFailed(line, "Missing stream ID in msid attribute.", error);
2226 }
deadbeef9d3584c2016-02-16 17:54:10 -08002227 return true;
2228}
2229
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002230// RFC 3551
2231// PT encoding media type clock rate channels
2232// name (Hz)
2233// 0 PCMU A 8,000 1
2234// 1 reserved A
2235// 2 reserved A
2236// 3 GSM A 8,000 1
2237// 4 G723 A 8,000 1
2238// 5 DVI4 A 8,000 1
2239// 6 DVI4 A 16,000 1
2240// 7 LPC A 8,000 1
2241// 8 PCMA A 8,000 1
2242// 9 G722 A 8,000 1
2243// 10 L16 A 44,100 2
2244// 11 L16 A 44,100 1
2245// 12 QCELP A 8,000 1
2246// 13 CN A 8,000 1
2247// 14 MPA A 90,000 (see text)
2248// 15 G728 A 8,000 1
2249// 16 DVI4 A 11,025 1
2250// 17 DVI4 A 22,050 1
2251// 18 G729 A 8,000 1
2252struct StaticPayloadAudioCodec {
2253 const char* name;
2254 int clockrate;
Peter Kasting69558702016-01-12 16:26:35 -08002255 size_t channels;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002256};
2257static const StaticPayloadAudioCodec kStaticPayloadAudioCodecs[] = {
2258 { "PCMU", 8000, 1 },
2259 { "reserved", 0, 0 },
2260 { "reserved", 0, 0 },
2261 { "GSM", 8000, 1 },
2262 { "G723", 8000, 1 },
2263 { "DVI4", 8000, 1 },
2264 { "DVI4", 16000, 1 },
2265 { "LPC", 8000, 1 },
2266 { "PCMA", 8000, 1 },
2267 { "G722", 8000, 1 },
2268 { "L16", 44100, 2 },
2269 { "L16", 44100, 1 },
2270 { "QCELP", 8000, 1 },
2271 { "CN", 8000, 1 },
2272 { "MPA", 90000, 1 },
2273 { "G728", 8000, 1 },
2274 { "DVI4", 11025, 1 },
2275 { "DVI4", 22050, 1 },
2276 { "G729", 8000, 1 },
2277};
2278
2279void MaybeCreateStaticPayloadAudioCodecs(
2280 const std::vector<int>& fmts, AudioContentDescription* media_desc) {
2281 if (!media_desc) {
2282 return;
2283 }
deadbeef67cf2c12016-04-13 10:07:16 -07002284 RTC_DCHECK(media_desc->codecs().empty());
solenberg9fa49752016-10-08 13:02:44 -07002285 for (int payload_type : fmts) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002286 if (!media_desc->HasCodec(payload_type) &&
2287 payload_type >= 0 &&
kjellander3e33bfe2016-06-20 07:04:09 -07002288 static_cast<uint32_t>(payload_type) <
2289 arraysize(kStaticPayloadAudioCodecs)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002290 std::string encoding_name = kStaticPayloadAudioCodecs[payload_type].name;
2291 int clock_rate = kStaticPayloadAudioCodecs[payload_type].clockrate;
Peter Kasting69558702016-01-12 16:26:35 -08002292 size_t channels = kStaticPayloadAudioCodecs[payload_type].channels;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002293 media_desc->AddCodec(cricket::AudioCodec(payload_type, encoding_name,
deadbeef67cf2c12016-04-13 10:07:16 -07002294 clock_rate, 0, channels));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002295 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002296 }
2297}
2298
2299template <class C>
2300static C* ParseContentDescription(const std::string& message,
2301 const MediaType media_type,
2302 int mline_index,
2303 const std::string& protocol,
deadbeef67cf2c12016-04-13 10:07:16 -07002304 const std::vector<int>& payload_types,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002305 size_t* pos,
2306 std::string* content_name,
deadbeef25ed4352016-12-12 18:37:36 -08002307 bool* bundle_only,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002308 TransportDescription* transport,
2309 std::vector<JsepIceCandidate*>* candidates,
2310 webrtc::SdpParseError* error) {
2311 C* media_desc = new C();
2312 switch (media_type) {
2313 case cricket::MEDIA_TYPE_AUDIO:
2314 *content_name = cricket::CN_AUDIO;
2315 break;
2316 case cricket::MEDIA_TYPE_VIDEO:
2317 *content_name = cricket::CN_VIDEO;
2318 break;
2319 case cricket::MEDIA_TYPE_DATA:
2320 *content_name = cricket::CN_DATA;
2321 break;
2322 default:
nissec80e7412017-01-11 05:56:46 -08002323 RTC_NOTREACHED();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002324 break;
2325 }
deadbeef67cf2c12016-04-13 10:07:16 -07002326 if (!ParseContent(message, media_type, mline_index, protocol, payload_types,
deadbeef25ed4352016-12-12 18:37:36 -08002327 pos, content_name, bundle_only, media_desc, transport,
2328 candidates, error)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002329 delete media_desc;
zhihuang38989e52017-03-21 11:04:53 -07002330 return nullptr;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002331 }
2332 // Sort the codecs according to the m-line fmt list.
deadbeef67cf2c12016-04-13 10:07:16 -07002333 std::unordered_map<int, int> payload_type_preferences;
2334 // "size + 1" so that the lowest preference payload type has a preference of
2335 // 1, which is greater than the default (0) for payload types not in the fmt
2336 // list.
2337 int preference = static_cast<int>(payload_types.size() + 1);
2338 for (int pt : payload_types) {
2339 payload_type_preferences[pt] = preference--;
2340 }
2341 std::vector<typename C::CodecType> codecs = media_desc->codecs();
2342 std::sort(codecs.begin(), codecs.end(), [&payload_type_preferences](
2343 const typename C::CodecType& a,
2344 const typename C::CodecType& b) {
2345 return payload_type_preferences[a.id] > payload_type_preferences[b.id];
2346 });
2347 media_desc->set_codecs(codecs);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002348 return media_desc;
2349}
2350
2351bool ParseMediaDescription(const std::string& message,
2352 const TransportDescription& session_td,
2353 const RtpHeaderExtensions& session_extmaps,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002354 size_t* pos,
zhihuang38989e52017-03-21 11:04:53 -07002355 const rtc::SocketAddress& session_connection_addr,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002356 cricket::SessionDescription* desc,
2357 std::vector<JsepIceCandidate*>* candidates,
2358 SdpParseError* error) {
nisseede5da42017-01-12 05:15:36 -08002359 RTC_DCHECK(desc != NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002360 std::string line;
2361 int mline_index = -1;
2362
2363 // Zero or more media descriptions
2364 // RFC 4566
2365 // m=<media> <port> <proto> <fmt>
2366 while (GetLineWithType(message, pos, &line, kLineTypeMedia)) {
2367 ++mline_index;
2368
2369 std::vector<std::string> fields;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002370 rtc::split(line.substr(kLinePrefixLength),
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002371 kSdpDelimiterSpace, &fields);
zstein4b2e0822017-02-17 19:48:38 -08002372
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002373 const size_t expected_min_fields = 4;
2374 if (fields.size() < expected_min_fields) {
2375 return ParseFailedExpectMinFieldNum(line, expected_min_fields, error);
2376 }
deadbeef25ed4352016-12-12 18:37:36 -08002377 bool port_rejected = false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002378 // RFC 3264
2379 // To reject an offered stream, the port number in the corresponding stream
2380 // in the answer MUST be set to zero.
2381 if (fields[1] == kMediaPortRejected) {
deadbeef25ed4352016-12-12 18:37:36 -08002382 port_rejected = true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002383 }
2384
zhihuang38989e52017-03-21 11:04:53 -07002385 int port = 0;
2386 if (!rtc::FromString<int>(fields[1], &port) || !IsValidPort(port)) {
2387 return ParseFailed(line, "The port number is invalid", error);
2388 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002389 std::string protocol = fields[2];
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002390
2391 // <fmt>
deadbeef67cf2c12016-04-13 10:07:16 -07002392 std::vector<int> payload_types;
pthatcher@webrtc.org3341b402015-02-13 21:14:22 +00002393 if (IsRtp(protocol)) {
jiayl@webrtc.orgddb85ab2014-09-05 16:31:56 +00002394 for (size_t j = 3 ; j < fields.size(); ++j) {
2395 // TODO(wu): Remove when below bug is fixed.
2396 // https://bugzilla.mozilla.org/show_bug.cgi?id=996329
pbosbb36fdf2015-07-09 07:48:14 -07002397 if (fields[j].empty() && j == fields.size() - 1) {
jiayl@webrtc.orgddb85ab2014-09-05 16:31:56 +00002398 continue;
2399 }
wu@webrtc.org36eda7c2014-04-15 20:37:30 +00002400
jiayl@webrtc.orgddb85ab2014-09-05 16:31:56 +00002401 int pl = 0;
pkasting@chromium.orge9facf82015-02-17 20:36:28 +00002402 if (!GetPayloadTypeFromString(line, fields[j], &pl, error)) {
jiayl@webrtc.orgddb85ab2014-09-05 16:31:56 +00002403 return false;
2404 }
deadbeef67cf2c12016-04-13 10:07:16 -07002405 payload_types.push_back(pl);
wu@webrtc.org5e760e72014-04-02 23:19:09 +00002406 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002407 }
2408
2409 // Make a temporary TransportDescription based on |session_td|.
2410 // Some of this gets overwritten by ParseContent.
deadbeef46eed762016-01-28 13:24:37 -08002411 TransportDescription transport(
2412 session_td.transport_options, session_td.ice_ufrag, session_td.ice_pwd,
2413 session_td.ice_mode, session_td.connection_role,
2414 session_td.identity_fingerprint.get());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002415
kwibergd1fe2812016-04-27 06:47:29 -07002416 std::unique_ptr<MediaContentDescription> content;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002417 std::string content_name;
deadbeef25ed4352016-12-12 18:37:36 -08002418 bool bundle_only = false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002419 if (HasAttribute(line, kMediaTypeVideo)) {
2420 content.reset(ParseContentDescription<VideoContentDescription>(
deadbeef67cf2c12016-04-13 10:07:16 -07002421 message, cricket::MEDIA_TYPE_VIDEO, mline_index, protocol,
deadbeef25ed4352016-12-12 18:37:36 -08002422 payload_types, pos, &content_name, &bundle_only, &transport,
2423 candidates, error));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002424 } else if (HasAttribute(line, kMediaTypeAudio)) {
2425 content.reset(ParseContentDescription<AudioContentDescription>(
deadbeef67cf2c12016-04-13 10:07:16 -07002426 message, cricket::MEDIA_TYPE_AUDIO, mline_index, protocol,
deadbeef25ed4352016-12-12 18:37:36 -08002427 payload_types, pos, &content_name, &bundle_only, &transport,
2428 candidates, error));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002429 } else if (HasAttribute(line, kMediaTypeData)) {
jiayl@webrtc.org0e527722014-09-08 21:43:43 +00002430 DataContentDescription* data_desc =
wu@webrtc.org78187522013-10-07 23:32:02 +00002431 ParseContentDescription<DataContentDescription>(
deadbeef67cf2c12016-04-13 10:07:16 -07002432 message, cricket::MEDIA_TYPE_DATA, mline_index, protocol,
deadbeef25ed4352016-12-12 18:37:36 -08002433 payload_types, pos, &content_name, &bundle_only, &transport,
2434 candidates, error);
jiayl@webrtc.org0e527722014-09-08 21:43:43 +00002435 content.reset(data_desc);
wu@webrtc.org78187522013-10-07 23:32:02 +00002436
zstein4b2e0822017-02-17 19:48:38 -08002437 if (data_desc && IsDtlsSctp(protocol)) {
2438 int p;
2439 if (rtc::FromString(fields[3], &p)) {
2440 if (!AddSctpDataCodec(data_desc, p)) {
2441 return false;
2442 }
2443 } else if (fields[3] == kDefaultSctpmapProtocol) {
2444 data_desc->set_use_sctpmap(false);
2445 }
wu@webrtc.org78187522013-10-07 23:32:02 +00002446 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002447 } else {
2448 LOG(LS_WARNING) << "Unsupported media type: " << line;
2449 continue;
2450 }
2451 if (!content.get()) {
2452 // ParseContentDescription returns NULL if failed.
2453 return false;
2454 }
2455
deadbeef25ed4352016-12-12 18:37:36 -08002456 bool content_rejected = false;
deadbeef12771a12017-01-03 13:53:47 -08002457 // A port of 0 is not interpreted as a rejected m= section when it's
2458 // used along with a=bundle-only.
deadbeef25ed4352016-12-12 18:37:36 -08002459 if (bundle_only) {
deadbeef25ed4352016-12-12 18:37:36 -08002460 if (!port_rejected) {
deadbeef12771a12017-01-03 13:53:47 -08002461 // Usage of bundle-only with a nonzero port is unspecified. So just
2462 // ignore bundle-only if we see this.
2463 bundle_only = false;
2464 LOG(LS_WARNING)
2465 << "a=bundle-only attribute observed with a nonzero "
2466 << "port; this usage is unspecified so the attribute is being "
2467 << "ignored.";
deadbeef25ed4352016-12-12 18:37:36 -08002468 }
2469 } else {
2470 // If not using bundle-only, interpret port 0 in the normal way; the m=
2471 // section is being rejected.
2472 content_rejected = port_rejected;
2473 }
2474
pthatcher@webrtc.org3341b402015-02-13 21:14:22 +00002475 if (IsRtp(protocol)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002476 // Set the extmap.
2477 if (!session_extmaps.empty() &&
2478 !content->rtp_header_extensions().empty()) {
2479 return ParseFailed("",
2480 "The a=extmap MUST be either all session level or "
2481 "all media level.",
2482 error);
2483 }
2484 for (size_t i = 0; i < session_extmaps.size(); ++i) {
2485 content->AddRtpHeaderExtension(session_extmaps[i]);
2486 }
2487 }
2488 content->set_protocol(protocol);
zhihuang38989e52017-03-21 11:04:53 -07002489
2490 // Use the session level connection address if the media level addresses are
2491 // not specified.
2492 rtc::SocketAddress address;
2493 address = content->connection_address().IsNil()
2494 ? session_connection_addr
2495 : content->connection_address();
2496 address.SetPort(port);
2497 content->set_connection_address(address);
2498
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002499 desc->AddContent(content_name,
deadbeef25ed4352016-12-12 18:37:36 -08002500 IsDtlsSctp(protocol) ? cricket::NS_JINGLE_DRAFT_SCTP
2501 : cricket::NS_JINGLE_RTP,
2502 content_rejected, bundle_only, content.release());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002503 // Create TransportInfo with the media level "ice-pwd" and "ice-ufrag".
2504 TransportInfo transport_info(content_name, transport);
2505
2506 if (!desc->AddTransportInfo(transport_info)) {
2507 std::ostringstream description;
2508 description << "Failed to AddTransportInfo with content name: "
2509 << content_name;
2510 return ParseFailed("", description.str(), error);
2511 }
2512 }
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00002513
2514 size_t end_of_message = message.size();
2515 if (mline_index == -1 && *pos != end_of_message) {
2516 ParseFailed(message, *pos, "Expects m line.", error);
2517 return false;
2518 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002519 return true;
2520}
2521
2522bool VerifyCodec(const cricket::Codec& codec) {
2523 // Codec has not been populated correctly unless the name has been set. This
2524 // can happen if an SDP has an fmtp or rtcp-fb with a payload type but doesn't
2525 // have a corresponding "rtpmap" line.
htab39db842016-12-08 01:50:48 -08002526 return !codec.name.empty();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002527}
2528
2529bool VerifyAudioCodecs(const AudioContentDescription* audio_desc) {
2530 const std::vector<cricket::AudioCodec>& codecs = audio_desc->codecs();
2531 for (std::vector<cricket::AudioCodec>::const_iterator iter = codecs.begin();
2532 iter != codecs.end(); ++iter) {
2533 if (!VerifyCodec(*iter)) {
2534 return false;
2535 }
2536 }
2537 return true;
2538}
2539
2540bool VerifyVideoCodecs(const VideoContentDescription* video_desc) {
2541 const std::vector<cricket::VideoCodec>& codecs = video_desc->codecs();
2542 for (std::vector<cricket::VideoCodec>::const_iterator iter = codecs.begin();
2543 iter != codecs.end(); ++iter) {
2544 if (!VerifyCodec(*iter)) {
2545 return false;
2546 }
2547 }
2548 return true;
2549}
2550
2551void AddParameters(const cricket::CodecParameterMap& parameters,
2552 cricket::Codec* codec) {
2553 for (cricket::CodecParameterMap::const_iterator iter =
2554 parameters.begin(); iter != parameters.end(); ++iter) {
2555 codec->SetParam(iter->first, iter->second);
2556 }
2557}
2558
2559void AddFeedbackParameter(const cricket::FeedbackParam& feedback_param,
2560 cricket::Codec* codec) {
2561 codec->AddFeedbackParam(feedback_param);
2562}
2563
2564void AddFeedbackParameters(const cricket::FeedbackParams& feedback_params,
2565 cricket::Codec* codec) {
2566 for (std::vector<cricket::FeedbackParam>::const_iterator iter =
2567 feedback_params.params().begin();
2568 iter != feedback_params.params().end(); ++iter) {
2569 codec->AddFeedbackParam(*iter);
2570 }
2571}
2572
2573// Gets the current codec setting associated with |payload_type|. If there
changbin.shao@webrtc.org2d25b442015-03-16 04:14:34 +00002574// is no Codec associated with that payload type it returns an empty codec
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002575// with that payload type.
2576template <class T>
changbin.shao@webrtc.org2d25b442015-03-16 04:14:34 +00002577T GetCodecWithPayloadType(const std::vector<T>& codecs, int payload_type) {
magjedb05fa242016-11-11 04:00:16 -08002578 const T* codec = FindCodecById(codecs, payload_type);
2579 if (codec)
2580 return *codec;
2581 // Return empty codec with |payload_type|.
changbin.shao@webrtc.org2d25b442015-03-16 04:14:34 +00002582 T ret_val;
magjedb05fa242016-11-11 04:00:16 -08002583 ret_val.id = payload_type;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002584 return ret_val;
2585}
2586
2587// Updates or creates a new codec entry in the audio description.
2588template <class T, class U>
2589void AddOrReplaceCodec(MediaContentDescription* content_desc, const U& codec) {
2590 T* desc = static_cast<T*>(content_desc);
2591 std::vector<U> codecs = desc->codecs();
2592 bool found = false;
2593
2594 typename std::vector<U>::iterator iter;
2595 for (iter = codecs.begin(); iter != codecs.end(); ++iter) {
2596 if (iter->id == codec.id) {
2597 *iter = codec;
2598 found = true;
2599 break;
2600 }
2601 }
2602 if (!found) {
2603 desc->AddCodec(codec);
2604 return;
2605 }
2606 desc->set_codecs(codecs);
2607}
2608
2609// Adds or updates existing codec corresponding to |payload_type| according
2610// to |parameters|.
2611template <class T, class U>
2612void UpdateCodec(MediaContentDescription* content_desc, int payload_type,
2613 const cricket::CodecParameterMap& parameters) {
2614 // Codec might already have been populated (from rtpmap).
changbin.shao@webrtc.org2d25b442015-03-16 04:14:34 +00002615 U new_codec = GetCodecWithPayloadType(static_cast<T*>(content_desc)->codecs(),
2616 payload_type);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002617 AddParameters(parameters, &new_codec);
2618 AddOrReplaceCodec<T, U>(content_desc, new_codec);
2619}
2620
2621// Adds or updates existing codec corresponding to |payload_type| according
2622// to |feedback_param|.
2623template <class T, class U>
2624void UpdateCodec(MediaContentDescription* content_desc, int payload_type,
2625 const cricket::FeedbackParam& feedback_param) {
2626 // Codec might already have been populated (from rtpmap).
changbin.shao@webrtc.org2d25b442015-03-16 04:14:34 +00002627 U new_codec = GetCodecWithPayloadType(static_cast<T*>(content_desc)->codecs(),
2628 payload_type);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002629 AddFeedbackParameter(feedback_param, &new_codec);
2630 AddOrReplaceCodec<T, U>(content_desc, new_codec);
2631}
2632
jlmiller@webrtc.orga744a282015-02-18 21:37:46 +00002633template <class T>
2634bool PopWildcardCodec(std::vector<T>* codecs, T* wildcard_codec) {
2635 for (auto iter = codecs->begin(); iter != codecs->end(); ++iter) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002636 if (iter->id == kWildcardPayloadType) {
2637 *wildcard_codec = *iter;
2638 codecs->erase(iter);
2639 return true;
2640 }
2641 }
2642 return false;
2643}
2644
jlmiller@webrtc.orga744a282015-02-18 21:37:46 +00002645template<class T>
2646void UpdateFromWildcardCodecs(cricket::MediaContentDescriptionImpl<T>* desc) {
2647 auto codecs = desc->codecs();
2648 T wildcard_codec;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002649 if (!PopWildcardCodec(&codecs, &wildcard_codec)) {
2650 return;
2651 }
jlmiller@webrtc.orga744a282015-02-18 21:37:46 +00002652 for (auto& codec : codecs) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002653 AddFeedbackParameters(wildcard_codec.feedback_params, &codec);
2654 }
jlmiller@webrtc.orga744a282015-02-18 21:37:46 +00002655 desc->set_codecs(codecs);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002656}
2657
2658void AddAudioAttribute(const std::string& name, const std::string& value,
2659 AudioContentDescription* audio_desc) {
2660 if (value.empty()) {
2661 return;
2662 }
2663 std::vector<cricket::AudioCodec> codecs = audio_desc->codecs();
2664 for (std::vector<cricket::AudioCodec>::iterator iter = codecs.begin();
2665 iter != codecs.end(); ++iter) {
2666 iter->params[name] = value;
2667 }
2668 audio_desc->set_codecs(codecs);
2669}
2670
2671bool ParseContent(const std::string& message,
2672 const MediaType media_type,
2673 int mline_index,
2674 const std::string& protocol,
deadbeef67cf2c12016-04-13 10:07:16 -07002675 const std::vector<int>& payload_types,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002676 size_t* pos,
2677 std::string* content_name,
deadbeef25ed4352016-12-12 18:37:36 -08002678 bool* bundle_only,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002679 MediaContentDescription* media_desc,
2680 TransportDescription* transport,
2681 std::vector<JsepIceCandidate*>* candidates,
2682 SdpParseError* error) {
nisseede5da42017-01-12 05:15:36 -08002683 RTC_DCHECK(media_desc != NULL);
2684 RTC_DCHECK(content_name != NULL);
2685 RTC_DCHECK(transport != NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002686
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +00002687 if (media_type == cricket::MEDIA_TYPE_AUDIO) {
2688 MaybeCreateStaticPayloadAudioCodecs(
deadbeef67cf2c12016-04-13 10:07:16 -07002689 payload_types, static_cast<AudioContentDescription*>(media_desc));
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +00002690 }
2691
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002692 // The media level "ice-ufrag" and "ice-pwd".
2693 // The candidates before update the media level "ice-pwd" and "ice-ufrag".
2694 Candidates candidates_orig;
2695 std::string line;
2696 std::string mline_id;
2697 // Tracks created out of the ssrc attributes.
2698 StreamParamsVec tracks;
2699 SsrcInfoVec ssrc_infos;
2700 SsrcGroupVec ssrc_groups;
2701 std::string maxptime_as_string;
2702 std::string ptime_as_string;
deadbeef9d3584c2016-02-16 17:54:10 -08002703 std::string stream_id;
2704 std::string track_id;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002705
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002706 // Loop until the next m line
2707 while (!IsLineType(message, kLineTypeMedia, *pos)) {
2708 if (!GetLine(message, pos, &line)) {
2709 if (*pos >= message.size()) {
2710 break; // Done parsing
2711 } else {
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +00002712 return ParseFailed(message, *pos, "Invalid SDP line.", error);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002713 }
2714 }
2715
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002716 // RFC 4566
2717 // b=* (zero or more bandwidth information lines)
2718 if (IsLineType(line, kLineTypeSessionBandwidth)) {
2719 std::string bandwidth;
2720 if (HasAttribute(line, kApplicationSpecificMaximum)) {
2721 if (!GetValue(line, kApplicationSpecificMaximum, &bandwidth, error)) {
2722 return false;
2723 } else {
wu@webrtc.org5e760e72014-04-02 23:19:09 +00002724 int b = 0;
2725 if (!GetValueFromString(line, bandwidth, &b, error)) {
2726 return false;
2727 }
deadbeef3e8016e2017-08-03 17:49:30 -07002728 // TODO(deadbeef): Historically, applications may be setting a value
2729 // of -1 to mean "unset any previously set bandwidth limit", even
2730 // though ommitting the "b=AS" entirely will do just that. Once we've
2731 // transitioned applications to doing the right thing, it would be
2732 // better to treat this as a hard error instead of just ignoring it.
2733 if (b == -1) {
2734 LOG(LS_WARNING) << "Ignoring \"b=AS:-1\"; will be treated as \"no "
2735 "bandwidth limit\".";
2736 continue;
2737 }
deadbeefbc88c6b2017-08-02 11:26:34 -07002738 if (b < 0) {
2739 return ParseFailed(line, "b=AS value can't be negative.", error);
2740 }
Peter Thatcherc0c3a862015-06-24 15:31:25 -07002741 // We should never use more than the default bandwidth for RTP-based
2742 // data channels. Don't allow SDP to set the bandwidth, because
2743 // that would give JS the opportunity to "break the Internet".
2744 // See: https://code.google.com/p/chromium/issues/detail?id=280726
2745 if (media_type == cricket::MEDIA_TYPE_DATA && IsRtp(protocol) &&
2746 b > cricket::kDataMaxBandwidth / 1000) {
2747 std::ostringstream description;
2748 description << "RTP-based data channels may not send more than "
2749 << cricket::kDataMaxBandwidth / 1000 << "kbps.";
2750 return ParseFailed(line, description.str(), error);
2751 }
deadbeefb2362572016-12-13 16:37:06 -08002752 // Prevent integer overflow.
2753 b = std::min(b, INT_MAX / 1000);
wu@webrtc.org5e760e72014-04-02 23:19:09 +00002754 media_desc->set_bandwidth(b * 1000);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002755 }
2756 }
2757 continue;
2758 }
2759
zhihuang38989e52017-03-21 11:04:53 -07002760 // Parse the media level connection data.
2761 if (IsLineType(line, kLineTypeConnection)) {
2762 rtc::SocketAddress addr;
2763 if (!ParseConnectionData(line, &addr, error)) {
2764 return false;
2765 }
2766 media_desc->set_connection_address(addr);
2767 continue;
2768 }
2769
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002770 if (!IsLineType(line, kLineTypeAttributes)) {
2771 // TODO: Handle other lines if needed.
2772 LOG(LS_INFO) << "Ignored line: " << line;
2773 continue;
2774 }
2775
2776 // Handle attributes common to SCTP and RTP.
2777 if (HasAttribute(line, kAttributeMid)) {
2778 // RFC 3388
2779 // mid-attribute = "a=mid:" identification-tag
2780 // identification-tag = token
2781 // Use the mid identification-tag as the content name.
2782 if (!GetValue(line, kAttributeMid, &mline_id, error)) {
2783 return false;
2784 }
2785 *content_name = mline_id;
deadbeef25ed4352016-12-12 18:37:36 -08002786 } else if (HasAttribute(line, kAttributeBundleOnly)) {
2787 *bundle_only = true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002788 } else if (HasAttribute(line, kAttributeCandidate)) {
2789 Candidate candidate;
2790 if (!ParseCandidate(line, &candidate, error, false)) {
2791 return false;
2792 }
deadbeef7bcdb692017-01-20 12:43:58 -08002793 // ParseCandidate will parse non-standard ufrag and password attributes,
2794 // since it's used for candidate trickling, but we only want to process
2795 // the "a=ice-ufrag"/"a=ice-pwd" values in a session description, so
2796 // strip them off at this point.
2797 candidate.set_username(std::string());
2798 candidate.set_password(std::string());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002799 candidates_orig.push_back(candidate);
2800 } else if (HasAttribute(line, kAttributeIceUfrag)) {
2801 if (!GetValue(line, kAttributeIceUfrag, &transport->ice_ufrag, error)) {
2802 return false;
2803 }
2804 } else if (HasAttribute(line, kAttributeIcePwd)) {
2805 if (!GetValue(line, kAttributeIcePwd, &transport->ice_pwd, error)) {
2806 return false;
2807 }
2808 } else if (HasAttribute(line, kAttributeIceOption)) {
2809 if (!ParseIceOptions(line, &transport->transport_options, error)) {
2810 return false;
2811 }
2812 } else if (HasAttribute(line, kAttributeFmtp)) {
2813 if (!ParseFmtpAttributes(line, media_type, media_desc, error)) {
2814 return false;
2815 }
2816 } else if (HasAttribute(line, kAttributeFingerprint)) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002817 rtc::SSLFingerprint* fingerprint = NULL;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002818
2819 if (!ParseFingerprintAttribute(line, &fingerprint, error)) {
2820 return false;
2821 }
2822 transport->identity_fingerprint.reset(fingerprint);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00002823 } else if (HasAttribute(line, kAttributeSetup)) {
2824 if (!ParseDtlsSetup(line, &(transport->connection_role), error)) {
2825 return false;
2826 }
pthatcher@webrtc.org3341b402015-02-13 21:14:22 +00002827 } else if (IsDtlsSctp(protocol) && HasAttribute(line, kAttributeSctpPort)) {
deadbeef7e146cb2016-09-28 10:04:34 -07002828 if (media_type != cricket::MEDIA_TYPE_DATA) {
2829 return ParseFailed(
2830 line, "sctp-port attribute found in non-data media description.",
2831 error);
2832 }
jiayl@webrtc.orgddb85ab2014-09-05 16:31:56 +00002833 int sctp_port;
2834 if (!ParseSctpPort(line, &sctp_port, error)) {
2835 return false;
2836 }
2837 if (!AddSctpDataCodec(static_cast<DataContentDescription*>(media_desc),
2838 sctp_port)) {
2839 return false;
2840 }
pthatcher@webrtc.org3341b402015-02-13 21:14:22 +00002841 } else if (IsRtp(protocol)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002842 //
2843 // RTP specific attrubtes
2844 //
2845 if (HasAttribute(line, kAttributeRtcpMux)) {
2846 media_desc->set_rtcp_mux(true);
deadbeef13871492015-12-09 12:37:51 -08002847 } else if (HasAttribute(line, kAttributeRtcpReducedSize)) {
2848 media_desc->set_rtcp_reduced_size(true);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002849 } else if (HasAttribute(line, kAttributeSsrcGroup)) {
2850 if (!ParseSsrcGroupAttribute(line, &ssrc_groups, error)) {
2851 return false;
2852 }
2853 } else if (HasAttribute(line, kAttributeSsrc)) {
2854 if (!ParseSsrcAttribute(line, &ssrc_infos, error)) {
2855 return false;
2856 }
2857 } else if (HasAttribute(line, kAttributeCrypto)) {
2858 if (!ParseCryptoAttribute(line, media_desc, error)) {
2859 return false;
2860 }
2861 } else if (HasAttribute(line, kAttributeRtpmap)) {
deadbeef67cf2c12016-04-13 10:07:16 -07002862 if (!ParseRtpmapAttribute(line, media_type, payload_types, media_desc,
2863 error)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002864 return false;
2865 }
2866 } else if (HasAttribute(line, kCodecParamMaxPTime)) {
2867 if (!GetValue(line, kCodecParamMaxPTime, &maxptime_as_string, error)) {
2868 return false;
2869 }
2870 } else if (HasAttribute(line, kAttributeRtcpFb)) {
2871 if (!ParseRtcpFbAttribute(line, media_type, media_desc, error)) {
2872 return false;
2873 }
2874 } else if (HasAttribute(line, kCodecParamPTime)) {
2875 if (!GetValue(line, kCodecParamPTime, &ptime_as_string, error)) {
2876 return false;
2877 }
2878 } else if (HasAttribute(line, kAttributeSendOnly)) {
2879 media_desc->set_direction(cricket::MD_SENDONLY);
2880 } else if (HasAttribute(line, kAttributeRecvOnly)) {
2881 media_desc->set_direction(cricket::MD_RECVONLY);
2882 } else if (HasAttribute(line, kAttributeInactive)) {
2883 media_desc->set_direction(cricket::MD_INACTIVE);
2884 } else if (HasAttribute(line, kAttributeSendRecv)) {
2885 media_desc->set_direction(cricket::MD_SENDRECV);
2886 } else if (HasAttribute(line, kAttributeExtmap)) {
isheriff6f8d6862016-05-26 11:24:55 -07002887 RtpExtension extmap;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002888 if (!ParseExtmap(line, &extmap, error)) {
2889 return false;
2890 }
2891 media_desc->AddRtpHeaderExtension(extmap);
2892 } else if (HasAttribute(line, kAttributeXGoogleFlag)) {
2893 // Experimental attribute. Conference mode activates more aggressive
2894 // AEC and NS settings.
2895 // TODO: expose API to set these directly.
2896 std::string flag_value;
2897 if (!GetValue(line, kAttributeXGoogleFlag, &flag_value, error)) {
2898 return false;
2899 }
2900 if (flag_value.compare(kValueConference) == 0)
2901 media_desc->set_conference_mode(true);
deadbeef9d3584c2016-02-16 17:54:10 -08002902 } else if (HasAttribute(line, kAttributeMsid)) {
2903 if (!ParseMsidAttribute(line, &stream_id, &track_id, error)) {
2904 return false;
2905 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002906 }
2907 } else {
2908 // Only parse lines that we are interested of.
2909 LOG(LS_INFO) << "Ignored line: " << line;
2910 continue;
2911 }
2912 }
2913
2914 // Create tracks from the |ssrc_infos|.
Taylor Brandstetter5de6b752016-03-09 17:02:30 -08002915 // If the stream_id/track_id for all SSRCS are identical, one StreamParams
2916 // will be created in CreateTracksFromSsrcInfos, containing all the SSRCs from
2917 // the m= section.
2918 CreateTracksFromSsrcInfos(ssrc_infos, stream_id, track_id, &tracks);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002919
2920 // Add the ssrc group to the track.
2921 for (SsrcGroupVec::iterator ssrc_group = ssrc_groups.begin();
2922 ssrc_group != ssrc_groups.end(); ++ssrc_group) {
2923 if (ssrc_group->ssrcs.empty()) {
2924 continue;
2925 }
Peter Boström0c4e06b2015-10-07 12:23:21 +02002926 uint32_t ssrc = ssrc_group->ssrcs.front();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002927 for (StreamParamsVec::iterator track = tracks.begin();
2928 track != tracks.end(); ++track) {
2929 if (track->has_ssrc(ssrc)) {
2930 track->ssrc_groups.push_back(*ssrc_group);
2931 }
2932 }
2933 }
2934
2935 // Add the new tracks to the |media_desc|.
deadbeef9d3584c2016-02-16 17:54:10 -08002936 for (StreamParams& track : tracks) {
2937 media_desc->AddStream(track);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002938 }
2939
2940 if (media_type == cricket::MEDIA_TYPE_AUDIO) {
2941 AudioContentDescription* audio_desc =
2942 static_cast<AudioContentDescription*>(media_desc);
jlmiller@webrtc.orga744a282015-02-18 21:37:46 +00002943 UpdateFromWildcardCodecs(audio_desc);
2944
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002945 // Verify audio codec ensures that no audio codec has been populated with
2946 // only fmtp.
2947 if (!VerifyAudioCodecs(audio_desc)) {
2948 return ParseFailed("Failed to parse audio codecs correctly.", error);
2949 }
2950 AddAudioAttribute(kCodecParamMaxPTime, maxptime_as_string, audio_desc);
2951 AddAudioAttribute(kCodecParamPTime, ptime_as_string, audio_desc);
2952 }
2953
2954 if (media_type == cricket::MEDIA_TYPE_VIDEO) {
jlmiller@webrtc.orga744a282015-02-18 21:37:46 +00002955 VideoContentDescription* video_desc =
2956 static_cast<VideoContentDescription*>(media_desc);
2957 UpdateFromWildcardCodecs(video_desc);
2958 // Verify video codec ensures that no video codec has been populated with
2959 // only rtcp-fb.
2960 if (!VerifyVideoCodecs(video_desc)) {
2961 return ParseFailed("Failed to parse video codecs correctly.", error);
2962 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002963 }
2964
2965 // RFC 5245
2966 // Update the candidates with the media level "ice-pwd" and "ice-ufrag".
2967 for (Candidates::iterator it = candidates_orig.begin();
2968 it != candidates_orig.end(); ++it) {
nisseede5da42017-01-12 05:15:36 -08002969 RTC_DCHECK((*it).username().empty() ||
2970 (*it).username() == transport->ice_ufrag);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002971 (*it).set_username(transport->ice_ufrag);
nisseede5da42017-01-12 05:15:36 -08002972 RTC_DCHECK((*it).password().empty());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002973 (*it).set_password(transport->ice_pwd);
2974 candidates->push_back(
2975 new JsepIceCandidate(mline_id, mline_index, *it));
2976 }
2977 return true;
2978}
2979
2980bool ParseSsrcAttribute(const std::string& line, SsrcInfoVec* ssrc_infos,
2981 SdpParseError* error) {
nisseede5da42017-01-12 05:15:36 -08002982 RTC_DCHECK(ssrc_infos != NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002983 // RFC 5576
2984 // a=ssrc:<ssrc-id> <attribute>
2985 // a=ssrc:<ssrc-id> <attribute>:<value>
2986 std::string field1, field2;
Donald Curtis144d0182015-05-15 13:14:24 -07002987 if (!rtc::tokenize_first(line.substr(kLinePrefixLength), kSdpDelimiterSpace,
2988 &field1, &field2)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002989 const size_t expected_fields = 2;
2990 return ParseFailedExpectFieldNum(line, expected_fields, error);
2991 }
2992
2993 // ssrc:<ssrc-id>
2994 std::string ssrc_id_s;
2995 if (!GetValue(field1, kAttributeSsrc, &ssrc_id_s, error)) {
2996 return false;
2997 }
Peter Boström0c4e06b2015-10-07 12:23:21 +02002998 uint32_t ssrc_id = 0;
wu@webrtc.org5e760e72014-04-02 23:19:09 +00002999 if (!GetValueFromString(line, ssrc_id_s, &ssrc_id, error)) {
3000 return false;
3001 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003002
3003 std::string attribute;
3004 std::string value;
Donald Curtis144d0182015-05-15 13:14:24 -07003005 if (!rtc::tokenize_first(field2, kSdpDelimiterColon, &attribute, &value)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003006 std::ostringstream description;
3007 description << "Failed to get the ssrc attribute value from " << field2
3008 << ". Expected format <attribute>:<value>.";
3009 return ParseFailed(line, description.str(), error);
3010 }
3011
3012 // Check if there's already an item for this |ssrc_id|. Create a new one if
3013 // there isn't.
3014 SsrcInfoVec::iterator ssrc_info = ssrc_infos->begin();
3015 for (; ssrc_info != ssrc_infos->end(); ++ssrc_info) {
3016 if (ssrc_info->ssrc_id == ssrc_id) {
3017 break;
3018 }
3019 }
3020 if (ssrc_info == ssrc_infos->end()) {
3021 SsrcInfo info;
3022 info.ssrc_id = ssrc_id;
3023 ssrc_infos->push_back(info);
3024 ssrc_info = ssrc_infos->end() - 1;
3025 }
3026
3027 // Store the info to the |ssrc_info|.
3028 if (attribute == kSsrcAttributeCname) {
3029 // RFC 5576
3030 // cname:<value>
3031 ssrc_info->cname = value;
3032 } else if (attribute == kSsrcAttributeMsid) {
3033 // draft-alvestrand-mmusic-msid-00
3034 // "msid:" identifier [ " " appdata ]
3035 std::vector<std::string> fields;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00003036 rtc::split(value, kSdpDelimiterSpace, &fields);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003037 if (fields.size() < 1 || fields.size() > 2) {
3038 return ParseFailed(line,
3039 "Expected format \"msid:<identifier>[ <appdata>]\".",
3040 error);
3041 }
deadbeef9d3584c2016-02-16 17:54:10 -08003042 ssrc_info->stream_id = fields[0];
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003043 if (fields.size() == 2) {
deadbeef9d3584c2016-02-16 17:54:10 -08003044 ssrc_info->track_id = fields[1];
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003045 }
3046 } else if (attribute == kSsrcAttributeMslabel) {
3047 // draft-alvestrand-rtcweb-mid-01
3048 // mslabel:<value>
3049 ssrc_info->mslabel = value;
3050 } else if (attribute == kSSrcAttributeLabel) {
3051 // The label isn't defined.
3052 // label:<value>
3053 ssrc_info->label = value;
3054 }
3055 return true;
3056}
3057
3058bool ParseSsrcGroupAttribute(const std::string& line,
3059 SsrcGroupVec* ssrc_groups,
3060 SdpParseError* error) {
nisseede5da42017-01-12 05:15:36 -08003061 RTC_DCHECK(ssrc_groups != NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003062 // RFC 5576
3063 // a=ssrc-group:<semantics> <ssrc-id> ...
3064 std::vector<std::string> fields;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00003065 rtc::split(line.substr(kLinePrefixLength),
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003066 kSdpDelimiterSpace, &fields);
3067 const size_t expected_min_fields = 2;
3068 if (fields.size() < expected_min_fields) {
3069 return ParseFailedExpectMinFieldNum(line, expected_min_fields, error);
3070 }
3071 std::string semantics;
3072 if (!GetValue(fields[0], kAttributeSsrcGroup, &semantics, error)) {
3073 return false;
3074 }
Peter Boström0c4e06b2015-10-07 12:23:21 +02003075 std::vector<uint32_t> ssrcs;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003076 for (size_t i = 1; i < fields.size(); ++i) {
Peter Boström0c4e06b2015-10-07 12:23:21 +02003077 uint32_t ssrc = 0;
wu@webrtc.org5e760e72014-04-02 23:19:09 +00003078 if (!GetValueFromString(line, fields[i], &ssrc, error)) {
3079 return false;
3080 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003081 ssrcs.push_back(ssrc);
3082 }
3083 ssrc_groups->push_back(SsrcGroup(semantics, ssrcs));
3084 return true;
3085}
3086
3087bool ParseCryptoAttribute(const std::string& line,
3088 MediaContentDescription* media_desc,
3089 SdpParseError* error) {
3090 std::vector<std::string> fields;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00003091 rtc::split(line.substr(kLinePrefixLength),
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003092 kSdpDelimiterSpace, &fields);
3093 // RFC 4568
3094 // a=crypto:<tag> <crypto-suite> <key-params> [<session-params>]
3095 const size_t expected_min_fields = 3;
3096 if (fields.size() < expected_min_fields) {
3097 return ParseFailedExpectMinFieldNum(line, expected_min_fields, error);
3098 }
3099 std::string tag_value;
3100 if (!GetValue(fields[0], kAttributeCrypto, &tag_value, error)) {
3101 return false;
3102 }
wu@webrtc.org5e760e72014-04-02 23:19:09 +00003103 int tag = 0;
3104 if (!GetValueFromString(line, tag_value, &tag, error)) {
3105 return false;
3106 }
jbauch083b73f2015-07-16 02:46:32 -07003107 const std::string& crypto_suite = fields[1];
3108 const std::string& key_params = fields[2];
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003109 std::string session_params;
3110 if (fields.size() > 3) {
3111 session_params = fields[3];
3112 }
3113 media_desc->AddCrypto(CryptoParams(tag, crypto_suite, key_params,
3114 session_params));
3115 return true;
3116}
3117
3118// Updates or creates a new codec entry in the audio description with according
deadbeef67cf2c12016-04-13 10:07:16 -07003119// to |name|, |clockrate|, |bitrate|, and |channels|.
3120void UpdateCodec(int payload_type,
3121 const std::string& name,
3122 int clockrate,
3123 int bitrate,
3124 size_t channels,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003125 AudioContentDescription* audio_desc) {
3126 // Codec may already be populated with (only) optional parameters
3127 // (from an fmtp).
changbin.shao@webrtc.org2d25b442015-03-16 04:14:34 +00003128 cricket::AudioCodec codec =
3129 GetCodecWithPayloadType(audio_desc->codecs(), payload_type);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003130 codec.name = name;
3131 codec.clockrate = clockrate;
3132 codec.bitrate = bitrate;
3133 codec.channels = channels;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003134 AddOrReplaceCodec<AudioContentDescription, cricket::AudioCodec>(audio_desc,
3135 codec);
3136}
3137
3138// Updates or creates a new codec entry in the video description according to
deadbeef67cf2c12016-04-13 10:07:16 -07003139// |name|, |width|, |height|, and |framerate|.
3140void UpdateCodec(int payload_type,
3141 const std::string& name,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003142 VideoContentDescription* video_desc) {
3143 // Codec may already be populated with (only) optional parameters
3144 // (from an fmtp).
changbin.shao@webrtc.org2d25b442015-03-16 04:14:34 +00003145 cricket::VideoCodec codec =
3146 GetCodecWithPayloadType(video_desc->codecs(), payload_type);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003147 codec.name = name;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003148 AddOrReplaceCodec<VideoContentDescription, cricket::VideoCodec>(video_desc,
3149 codec);
3150}
3151
3152bool ParseRtpmapAttribute(const std::string& line,
3153 const MediaType media_type,
deadbeef67cf2c12016-04-13 10:07:16 -07003154 const std::vector<int>& payload_types,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003155 MediaContentDescription* media_desc,
3156 SdpParseError* error) {
3157 std::vector<std::string> fields;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00003158 rtc::split(line.substr(kLinePrefixLength),
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003159 kSdpDelimiterSpace, &fields);
3160 // RFC 4566
3161 // a=rtpmap:<payload type> <encoding name>/<clock rate>[/<encodingparameters>]
3162 const size_t expected_min_fields = 2;
3163 if (fields.size() < expected_min_fields) {
3164 return ParseFailedExpectMinFieldNum(line, expected_min_fields, error);
3165 }
3166 std::string payload_type_value;
3167 if (!GetValue(fields[0], kAttributeRtpmap, &payload_type_value, error)) {
3168 return false;
3169 }
wu@webrtc.org5e760e72014-04-02 23:19:09 +00003170 int payload_type = 0;
pkasting@chromium.orge9facf82015-02-17 20:36:28 +00003171 if (!GetPayloadTypeFromString(line, payload_type_value, &payload_type,
3172 error)) {
wu@webrtc.org5e760e72014-04-02 23:19:09 +00003173 return false;
3174 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003175
deadbeef67cf2c12016-04-13 10:07:16 -07003176 if (std::find(payload_types.begin(), payload_types.end(), payload_type) ==
3177 payload_types.end()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003178 LOG(LS_WARNING) << "Ignore rtpmap line that did not appear in the "
3179 << "<fmt> of the m-line: " << line;
3180 return true;
3181 }
jbauch083b73f2015-07-16 02:46:32 -07003182 const std::string& encoder = fields[1];
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003183 std::vector<std::string> codec_params;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00003184 rtc::split(encoder, '/', &codec_params);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003185 // <encoding name>/<clock rate>[/<encodingparameters>]
3186 // 2 mandatory fields
3187 if (codec_params.size() < 2 || codec_params.size() > 3) {
3188 return ParseFailed(line,
3189 "Expected format \"<encoding name>/<clock rate>"
3190 "[/<encodingparameters>]\".",
3191 error);
3192 }
jbauch083b73f2015-07-16 02:46:32 -07003193 const std::string& encoding_name = codec_params[0];
wu@webrtc.org5e760e72014-04-02 23:19:09 +00003194 int clock_rate = 0;
3195 if (!GetValueFromString(line, codec_params[1], &clock_rate, error)) {
3196 return false;
3197 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003198 if (media_type == cricket::MEDIA_TYPE_VIDEO) {
3199 VideoContentDescription* video_desc =
3200 static_cast<VideoContentDescription*>(media_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003201 UpdateCodec(payload_type, encoding_name,
deadbeef67cf2c12016-04-13 10:07:16 -07003202 video_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003203 } else if (media_type == cricket::MEDIA_TYPE_AUDIO) {
3204 // RFC 4566
3205 // For audio streams, <encoding parameters> indicates the number
3206 // of audio channels. This parameter is OPTIONAL and may be
3207 // omitted if the number of channels is one, provided that no
3208 // additional parameters are needed.
Peter Kasting69558702016-01-12 16:26:35 -08003209 size_t channels = 1;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003210 if (codec_params.size() == 3) {
wu@webrtc.org5e760e72014-04-02 23:19:09 +00003211 if (!GetValueFromString(line, codec_params[2], &channels, error)) {
3212 return false;
3213 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003214 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003215 AudioContentDescription* audio_desc =
3216 static_cast<AudioContentDescription*>(media_desc);
ossue1405ad2017-01-23 08:55:48 -08003217 UpdateCodec(payload_type, encoding_name, clock_rate, 0, channels,
deadbeef67cf2c12016-04-13 10:07:16 -07003218 audio_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003219 } else if (media_type == cricket::MEDIA_TYPE_DATA) {
3220 DataContentDescription* data_desc =
3221 static_cast<DataContentDescription*>(media_desc);
deadbeef67cf2c12016-04-13 10:07:16 -07003222 data_desc->AddCodec(cricket::DataCodec(payload_type, encoding_name));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003223 }
3224 return true;
3225}
3226
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003227bool ParseFmtpParam(const std::string& line, std::string* parameter,
3228 std::string* value, SdpParseError* error) {
Donald Curtis0e07f922015-05-15 09:21:23 -07003229 if (!rtc::tokenize_first(line, kSdpDelimiterEqual, parameter, value)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003230 ParseFailed(line, "Unable to parse fmtp parameter. \'=\' missing.", error);
3231 return false;
3232 }
3233 // a=fmtp:<payload_type> <param1>=<value1>; <param2>=<value2>; ...
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003234 return true;
3235}
3236
3237bool ParseFmtpAttributes(const std::string& line, const MediaType media_type,
3238 MediaContentDescription* media_desc,
3239 SdpParseError* error) {
3240 if (media_type != cricket::MEDIA_TYPE_AUDIO &&
3241 media_type != cricket::MEDIA_TYPE_VIDEO) {
3242 return true;
3243 }
Donald Curtis0e07f922015-05-15 09:21:23 -07003244
3245 std::string line_payload;
3246 std::string line_params;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003247
3248 // RFC 5576
3249 // a=fmtp:<format> <format specific parameters>
3250 // At least two fields, whereas the second one is any of the optional
3251 // parameters.
Donald Curtis144d0182015-05-15 13:14:24 -07003252 if (!rtc::tokenize_first(line.substr(kLinePrefixLength), kSdpDelimiterSpace,
3253 &line_payload, &line_params)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003254 ParseFailedExpectMinFieldNum(line, 2, error);
3255 return false;
3256 }
3257
Donald Curtis0e07f922015-05-15 09:21:23 -07003258 // Parse out the payload information.
pkasting@chromium.orgd3245462015-02-23 21:28:22 +00003259 std::string payload_type_str;
Donald Curtis0e07f922015-05-15 09:21:23 -07003260 if (!GetValue(line_payload, kAttributeFmtp, &payload_type_str, error)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003261 return false;
3262 }
3263
Donald Curtis0e07f922015-05-15 09:21:23 -07003264 int payload_type = 0;
Donald Curtis144d0182015-05-15 13:14:24 -07003265 if (!GetPayloadTypeFromString(line_payload, payload_type_str, &payload_type,
3266 error)) {
Donald Curtis0e07f922015-05-15 09:21:23 -07003267 return false;
3268 }
3269
3270 // Parse out format specific parameters.
3271 std::vector<std::string> fields;
3272 rtc::split(line_params, kSdpDelimiterSemicolon, &fields);
3273
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003274 cricket::CodecParameterMap codec_params;
Donald Curtis144d0182015-05-15 13:14:24 -07003275 for (auto& iter : fields) {
Donald Curtis0e07f922015-05-15 09:21:23 -07003276 if (iter.find(kSdpDelimiterEqual) == std::string::npos) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003277 // Only fmtps with equals are currently supported. Other fmtp types
3278 // should be ignored. Unknown fmtps do not constitute an error.
3279 continue;
3280 }
Donald Curtis0e07f922015-05-15 09:21:23 -07003281
3282 std::string name;
3283 std::string value;
3284 if (!ParseFmtpParam(rtc::string_trim(iter), &name, &value, error)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003285 return false;
3286 }
3287 codec_params[name] = value;
3288 }
3289
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003290 if (media_type == cricket::MEDIA_TYPE_AUDIO) {
3291 UpdateCodec<AudioContentDescription, cricket::AudioCodec>(
pkasting@chromium.orgd3245462015-02-23 21:28:22 +00003292 media_desc, payload_type, codec_params);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003293 } else if (media_type == cricket::MEDIA_TYPE_VIDEO) {
3294 UpdateCodec<VideoContentDescription, cricket::VideoCodec>(
pkasting@chromium.orgd3245462015-02-23 21:28:22 +00003295 media_desc, payload_type, codec_params);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003296 }
3297 return true;
3298}
3299
3300bool ParseRtcpFbAttribute(const std::string& line, const MediaType media_type,
3301 MediaContentDescription* media_desc,
3302 SdpParseError* error) {
3303 if (media_type != cricket::MEDIA_TYPE_AUDIO &&
3304 media_type != cricket::MEDIA_TYPE_VIDEO) {
3305 return true;
3306 }
3307 std::vector<std::string> rtcp_fb_fields;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00003308 rtc::split(line.c_str(), kSdpDelimiterSpace, &rtcp_fb_fields);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003309 if (rtcp_fb_fields.size() < 2) {
3310 return ParseFailedGetValue(line, kAttributeRtcpFb, error);
3311 }
3312 std::string payload_type_string;
3313 if (!GetValue(rtcp_fb_fields[0], kAttributeRtcpFb, &payload_type_string,
3314 error)) {
3315 return false;
3316 }
wu@webrtc.org5e760e72014-04-02 23:19:09 +00003317 int payload_type = kWildcardPayloadType;
3318 if (payload_type_string != "*") {
pkasting@chromium.orge9facf82015-02-17 20:36:28 +00003319 if (!GetPayloadTypeFromString(line, payload_type_string, &payload_type,
3320 error)) {
wu@webrtc.org5e760e72014-04-02 23:19:09 +00003321 return false;
3322 }
3323 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003324 std::string id = rtcp_fb_fields[1];
3325 std::string param = "";
3326 for (std::vector<std::string>::iterator iter = rtcp_fb_fields.begin() + 2;
3327 iter != rtcp_fb_fields.end(); ++iter) {
3328 param.append(*iter);
3329 }
3330 const cricket::FeedbackParam feedback_param(id, param);
3331
3332 if (media_type == cricket::MEDIA_TYPE_AUDIO) {
pkasting@chromium.orgd3245462015-02-23 21:28:22 +00003333 UpdateCodec<AudioContentDescription, cricket::AudioCodec>(
3334 media_desc, payload_type, feedback_param);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003335 } else if (media_type == cricket::MEDIA_TYPE_VIDEO) {
pkasting@chromium.orgd3245462015-02-23 21:28:22 +00003336 UpdateCodec<VideoContentDescription, cricket::VideoCodec>(
3337 media_desc, payload_type, feedback_param);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003338 }
3339 return true;
3340}
3341
3342} // namespace webrtc