blob: 60c427d16f17a6ad4eb500305b84f2a467f34b9c [file] [log] [blame]
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001/*
2 * libjingle
3 * Copyright 2011, Google Inc.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright notice,
9 * this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright notice,
11 * this list of conditions and the following disclaimer in the documentation
12 * and/or other materials provided with the distribution.
13 * 3. The name of the author may not be used to endorse or promote products
14 * derived from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
19 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28#include "talk/app/webrtc/webrtcsdp.h"
29
30#include <limits.h>
31#include <stdio.h>
32#include <algorithm>
33#include <string>
34#include <vector>
35
36#include "talk/app/webrtc/jsepicecandidate.h"
37#include "talk/app/webrtc/jsepsessiondescription.h"
38#include "talk/base/common.h"
39#include "talk/base/logging.h"
40#include "talk/base/messagedigest.h"
41#include "talk/base/stringutils.h"
42#include "talk/media/base/codec.h"
43#include "talk/media/base/constants.h"
44#include "talk/media/base/cryptoparams.h"
mallinath@webrtc.org1112c302013-09-23 20:34:45 +000045#include "talk/media/sctp/sctpdataengine.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000046#include "talk/p2p/base/candidate.h"
47#include "talk/p2p/base/constants.h"
48#include "talk/p2p/base/port.h"
49#include "talk/session/media/mediasession.h"
50#include "talk/session/media/mediasessionclient.h"
51
52using cricket::AudioContentDescription;
53using cricket::Candidate;
54using cricket::Candidates;
55using cricket::ContentDescription;
56using cricket::ContentInfo;
57using cricket::CryptoParams;
58using cricket::DataContentDescription;
59using cricket::ICE_CANDIDATE_COMPONENT_RTP;
60using cricket::ICE_CANDIDATE_COMPONENT_RTCP;
61using cricket::kCodecParamMaxBitrate;
62using cricket::kCodecParamMaxPTime;
63using cricket::kCodecParamMaxQuantization;
64using cricket::kCodecParamMinBitrate;
65using cricket::kCodecParamMinPTime;
66using cricket::kCodecParamPTime;
67using cricket::kCodecParamSPropStereo;
68using cricket::kCodecParamStereo;
69using cricket::kCodecParamUseInbandFec;
70using cricket::kCodecParamSctpProtocol;
71using cricket::kCodecParamSctpStreams;
henrike@webrtc.org1e09a712013-07-26 19:17:59 +000072using cricket::kCodecParamMaxAverageBitrate;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000073using cricket::kWildcardPayloadType;
74using cricket::MediaContentDescription;
75using cricket::MediaType;
76using cricket::NS_JINGLE_ICE_UDP;
77using cricket::RtpHeaderExtension;
78using cricket::SsrcGroup;
79using cricket::StreamParams;
80using cricket::StreamParamsVec;
81using cricket::TransportDescription;
82using cricket::TransportInfo;
83using cricket::VideoContentDescription;
84using talk_base::SocketAddress;
85
86typedef std::vector<RtpHeaderExtension> RtpHeaderExtensions;
87
88namespace cricket {
89class SessionDescription;
90}
91
92namespace webrtc {
93
94// Line type
95// RFC 4566
96// An SDP session description consists of a number of lines of text of
97// the form:
98// <type>=<value>
99// where <type> MUST be exactly one case-significant character.
100static const int kLinePrefixLength = 2; // Lenght of <type>=
101static const char kLineTypeVersion = 'v';
102static const char kLineTypeOrigin = 'o';
103static const char kLineTypeSessionName = 's';
104static const char kLineTypeSessionInfo = 'i';
105static const char kLineTypeSessionUri = 'u';
106static const char kLineTypeSessionEmail = 'e';
107static const char kLineTypeSessionPhone = 'p';
108static const char kLineTypeSessionBandwidth = 'b';
109static const char kLineTypeTiming = 't';
110static const char kLineTypeRepeatTimes = 'r';
111static const char kLineTypeTimeZone = 'z';
112static const char kLineTypeEncryptionKey = 'k';
113static const char kLineTypeMedia = 'm';
114static const char kLineTypeConnection = 'c';
115static const char kLineTypeAttributes = 'a';
116
117// Attributes
118static const char kAttributeGroup[] = "group";
119static const char kAttributeMid[] = "mid";
120static const char kAttributeRtcpMux[] = "rtcp-mux";
121static const char kAttributeSsrc[] = "ssrc";
122static const char kSsrcAttributeCname[] = "cname";
123static const char kAttributeExtmap[] = "extmap";
124// draft-alvestrand-mmusic-msid-01
125// a=msid-semantic: WMS
126static const char kAttributeMsidSemantics[] = "msid-semantic";
127static const char kMediaStreamSemantic[] = "WMS";
128static const char kSsrcAttributeMsid[] = "msid";
129static const char kDefaultMsid[] = "default";
130static const char kMsidAppdataAudio[] = "a";
131static const char kMsidAppdataVideo[] = "v";
132static const char kMsidAppdataData[] = "d";
133static const char kSsrcAttributeMslabel[] = "mslabel";
134static const char kSSrcAttributeLabel[] = "label";
135static const char kAttributeSsrcGroup[] = "ssrc-group";
136static const char kAttributeCrypto[] = "crypto";
137static const char kAttributeCandidate[] = "candidate";
138static const char kAttributeCandidateTyp[] = "typ";
139static const char kAttributeCandidateRaddr[] = "raddr";
140static const char kAttributeCandidateRport[] = "rport";
141static const char kAttributeCandidateUsername[] = "username";
142static const char kAttributeCandidatePassword[] = "password";
143static const char kAttributeCandidateGeneration[] = "generation";
144static const char kAttributeFingerprint[] = "fingerprint";
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +0000145static const char kAttributeSetup[] = "setup";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000146static const char kAttributeFmtp[] = "fmtp";
147static const char kAttributeRtpmap[] = "rtpmap";
148static const char kAttributeRtcp[] = "rtcp";
149static const char kAttributeIceUfrag[] = "ice-ufrag";
150static const char kAttributeIcePwd[] = "ice-pwd";
151static const char kAttributeIceLite[] = "ice-lite";
152static const char kAttributeIceOption[] = "ice-options";
153static const char kAttributeSendOnly[] = "sendonly";
154static const char kAttributeRecvOnly[] = "recvonly";
155static const char kAttributeRtcpFb[] = "rtcp-fb";
156static const char kAttributeSendRecv[] = "sendrecv";
157static const char kAttributeInactive[] = "inactive";
158
159// Experimental flags
160static const char kAttributeXGoogleFlag[] = "x-google-flag";
161static const char kValueConference[] = "conference";
162static const char kAttributeXGoogleBufferLatency[] =
163 "x-google-buffer-latency";
164
165// Candidate
166static const char kCandidateHost[] = "host";
167static const char kCandidateSrflx[] = "srflx";
168// TODO: How to map the prflx with circket candidate type
169// static const char kCandidatePrflx[] = "prflx";
170static const char kCandidateRelay[] = "relay";
171
172static const char kSdpDelimiterEqual = '=';
173static const char kSdpDelimiterSpace = ' ';
174static const char kSdpDelimiterColon = ':';
175static const char kSdpDelimiterSemicolon = ';';
176static const char kSdpDelimiterSlash = '/';
177static const char kNewLine = '\n';
178static const char kReturn = '\r';
179static const char kLineBreak[] = "\r\n";
180
181// TODO: Generate the Session and Time description
182// instead of hardcoding.
183static const char kSessionVersion[] = "v=0";
184// RFC 4566
185static const char kSessionOriginUsername[] = "-";
186static const char kSessionOriginSessionId[] = "0";
187static const char kSessionOriginSessionVersion[] = "0";
188static const char kSessionOriginNettype[] = "IN";
189static const char kSessionOriginAddrtype[] = "IP4";
190static const char kSessionOriginAddress[] = "127.0.0.1";
191static const char kSessionName[] = "s=-";
192static const char kTimeDescription[] = "t=0 0";
193static const char kAttrGroup[] = "a=group:BUNDLE";
194static const char kConnectionNettype[] = "IN";
195static const char kConnectionAddrtype[] = "IP4";
196static const char kMediaTypeVideo[] = "video";
197static const char kMediaTypeAudio[] = "audio";
198static const char kMediaTypeData[] = "application";
199static const char kMediaPortRejected[] = "0";
200static const char kDefaultAddress[] = "0.0.0.0";
201static const char kDefaultPort[] = "1";
202// RFC 3556
203static const char kApplicationSpecificMaximum[] = "AS";
204
205static const int kDefaultVideoClockrate = 90000;
206
207// ISAC special-case.
208static const char kIsacCodecName[] = "ISAC"; // From webrtcvoiceengine.cc
209static const int kIsacWbDefaultRate = 32000; // From acm_common_defs.h
210static const int kIsacSwbDefaultRate = 56000; // From acm_common_defs.h
211
212static const int kDefaultSctpFmt = 5000;
213static const char kDefaultSctpFmtProtocol[] = "webrtc-datachannel";
214
215struct SsrcInfo {
216 SsrcInfo()
217 : msid_identifier(kDefaultMsid),
218 // TODO(ronghuawu): What should we do if the appdata doesn't appear?
219 // Create random string (which will be used as track label later)?
220 msid_appdata(talk_base::CreateRandomString(8)) {
221 }
222 uint32 ssrc_id;
223 std::string cname;
224 std::string msid_identifier;
225 std::string msid_appdata;
226
227 // For backward compatibility.
228 // TODO(ronghuawu): Remove below 2 fields once all the clients support msid.
229 std::string label;
230 std::string mslabel;
231};
232typedef std::vector<SsrcInfo> SsrcInfoVec;
233typedef std::vector<SsrcGroup> SsrcGroupVec;
234
235// Serializes the passed in SessionDescription to a SDP string.
236// desc - The SessionDescription object to be serialized.
237static std::string SdpSerializeSessionDescription(
238 const JsepSessionDescription& jdesc);
239template <class T>
240static void AddFmtpLine(const T& codec, std::string* message);
241static void BuildMediaDescription(const ContentInfo* content_info,
242 const TransportInfo* transport_info,
243 const MediaType media_type,
244 std::string* message);
245static void BuildSctpContentAttributes(std::string* message);
246static void BuildRtpContentAttributes(
247 const MediaContentDescription* media_desc,
248 const MediaType media_type,
249 std::string* message);
250static void BuildRtpMap(const MediaContentDescription* media_desc,
251 const MediaType media_type,
252 std::string* message);
253static void BuildCandidate(const std::vector<Candidate>& candidates,
254 std::string* message);
255static void BuildIceOptions(const std::vector<std::string>& transport_options,
256 std::string* message);
257
258static bool ParseSessionDescription(const std::string& message, size_t* pos,
259 std::string* session_id,
260 std::string* session_version,
261 bool* supports_msid,
262 TransportDescription* session_td,
263 RtpHeaderExtensions* session_extmaps,
264 cricket::SessionDescription* desc,
265 SdpParseError* error);
266static bool ParseGroupAttribute(const std::string& line,
267 cricket::SessionDescription* desc,
268 SdpParseError* error);
269static bool ParseMediaDescription(
270 const std::string& message,
271 const TransportDescription& session_td,
272 const RtpHeaderExtensions& session_extmaps,
273 bool supports_msid,
274 size_t* pos, cricket::SessionDescription* desc,
275 std::vector<JsepIceCandidate*>* candidates,
276 SdpParseError* error);
277static bool ParseContent(const std::string& message,
278 const MediaType media_type,
279 int mline_index,
280 const std::string& protocol,
281 const std::vector<int>& codec_preference,
282 size_t* pos,
283 std::string* content_name,
284 MediaContentDescription* media_desc,
285 TransportDescription* transport,
286 std::vector<JsepIceCandidate*>* candidates,
287 SdpParseError* error);
288static bool ParseSsrcAttribute(const std::string& line,
289 SsrcInfoVec* ssrc_infos,
290 SdpParseError* error);
291static bool ParseSsrcGroupAttribute(const std::string& line,
292 SsrcGroupVec* ssrc_groups,
293 SdpParseError* error);
294static bool ParseCryptoAttribute(const std::string& line,
295 MediaContentDescription* media_desc,
296 SdpParseError* error);
297static bool ParseRtpmapAttribute(const std::string& line,
298 const MediaType media_type,
299 const std::vector<int>& codec_preference,
300 MediaContentDescription* media_desc,
301 SdpParseError* error);
302static bool ParseFmtpAttributes(const std::string& line,
303 const MediaType media_type,
304 MediaContentDescription* media_desc,
305 SdpParseError* error);
306static bool ParseFmtpParam(const std::string& line, std::string* parameter,
307 std::string* value, SdpParseError* error);
308static bool ParseCandidate(const std::string& message, Candidate* candidate,
309 SdpParseError* error, bool is_raw);
310static bool ParseRtcpFbAttribute(const std::string& line,
311 const MediaType media_type,
312 MediaContentDescription* media_desc,
313 SdpParseError* error);
314static bool ParseIceOptions(const std::string& line,
315 std::vector<std::string>* transport_options,
316 SdpParseError* error);
317static bool ParseExtmap(const std::string& line,
318 RtpHeaderExtension* extmap,
319 SdpParseError* error);
320static bool ParseFingerprintAttribute(const std::string& line,
321 talk_base::SSLFingerprint** fingerprint,
322 SdpParseError* error);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +0000323static bool ParseDtlsSetup(const std::string& line,
324 cricket::ConnectionRole* role,
325 SdpParseError* error);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000326
327// Helper functions
328
329// Below ParseFailed*** functions output the line that caused the parsing
330// failure and the detailed reason (|description|) of the failure to |error|.
331// The functions always return false so that they can be used directly in the
332// following way when error happens:
333// "return ParseFailed***(...);"
334
335// The line starting at |line_start| of |message| is the failing line.
336// The reason for the failure should be provided in the |description|.
337// An example of a description could be "unknown character".
338static bool ParseFailed(const std::string& message,
339 size_t line_start,
340 const std::string& description,
341 SdpParseError* error) {
342 // Get the first line of |message| from |line_start|.
343 std::string first_line = message;
344 size_t line_end = message.find(kNewLine, line_start);
345 if (line_end != std::string::npos) {
346 if (line_end > 0 && (message.at(line_end - 1) == kReturn)) {
347 --line_end;
348 }
349 first_line = message.substr(line_start, (line_end - line_start));
350 }
351
352 if (error) {
353 error->line = first_line;
354 error->description = description;
355 }
356 LOG(LS_ERROR) << "Failed to parse: \"" << first_line
357 << "\". Reason: " << description;
358 return false;
359}
360
361// |line| is the failing line. The reason for the failure should be
362// provided in the |description|.
363static bool ParseFailed(const std::string& line,
364 const std::string& description,
365 SdpParseError* error) {
366 return ParseFailed(line, 0, description, error);
367}
368
369// Parses failure where the failing SDP line isn't know or there are multiple
370// failing lines.
371static bool ParseFailed(const std::string& description,
372 SdpParseError* error) {
373 return ParseFailed("", description, error);
374}
375
376// |line| is the failing line. The failure is due to the fact that |line|
377// doesn't have |expected_fields| fields.
378static bool ParseFailedExpectFieldNum(const std::string& line,
379 int expected_fields,
380 SdpParseError* error) {
381 std::ostringstream description;
382 description << "Expects " << expected_fields << " fields.";
383 return ParseFailed(line, description.str(), error);
384}
385
386// |line| is the failing line. The failure is due to the fact that |line| has
387// less than |expected_min_fields| fields.
388static bool ParseFailedExpectMinFieldNum(const std::string& line,
389 int expected_min_fields,
390 SdpParseError* error) {
391 std::ostringstream description;
392 description << "Expects at least " << expected_min_fields << " fields.";
393 return ParseFailed(line, description.str(), error);
394}
395
396// |line| is the failing line. The failure is due to the fact that it failed to
397// get the value of |attribute|.
398static bool ParseFailedGetValue(const std::string& line,
399 const std::string& attribute,
400 SdpParseError* error) {
401 std::ostringstream description;
402 description << "Failed to get the value of attribute: " << attribute;
403 return ParseFailed(line, description.str(), error);
404}
405
406// The line starting at |line_start| of |message| is the failing line. The
407// failure is due to the line type (e.g. the "m" part of the "m-line")
408// not matching what is expected. The expected line type should be
409// provided as |line_type|.
410static bool ParseFailedExpectLine(const std::string& message,
411 size_t line_start,
412 const char line_type,
413 const std::string& line_value,
414 SdpParseError* error) {
415 std::ostringstream description;
416 description << "Expect line: " << line_type << "=" << line_value;
417 return ParseFailed(message, line_start, description.str(), error);
418}
419
420static bool AddLine(const std::string& line, std::string* message) {
421 if (!message)
422 return false;
423
424 message->append(line);
425 message->append(kLineBreak);
426 return true;
427}
428
429static bool GetLine(const std::string& message,
430 size_t* pos,
431 std::string* line) {
432 size_t line_begin = *pos;
433 size_t line_end = message.find(kNewLine, line_begin);
434 if (line_end == std::string::npos) {
435 return false;
436 }
437 // Update the new start position
438 *pos = line_end + 1;
439 if (line_end > 0 && (message.at(line_end - 1) == kReturn)) {
440 --line_end;
441 }
442 *line = message.substr(line_begin, (line_end - line_begin));
443 const char* cline = line->c_str();
444 // RFC 4566
445 // An SDP session description consists of a number of lines of text of
446 // the form:
447 // <type>=<value>
448 // where <type> MUST be exactly one case-significant character and
449 // <value> is structured text whose format depends on <type>.
450 // Whitespace MUST NOT be used on either side of the "=" sign.
451 if (cline[0] == kSdpDelimiterSpace ||
452 cline[1] != kSdpDelimiterEqual ||
453 cline[2] == kSdpDelimiterSpace) {
454 *pos = line_begin;
455 return false;
456 }
457 return true;
458}
459
460// Init |os| to "|type|=|value|".
461static void InitLine(const char type,
462 const std::string& value,
463 std::ostringstream* os) {
464 os->str("");
465 *os << type << kSdpDelimiterEqual << value;
466}
467
468// Init |os| to "a=|attribute|".
469static void InitAttrLine(const std::string& attribute, std::ostringstream* os) {
470 InitLine(kLineTypeAttributes, attribute, os);
471}
472
473// Writes a SDP attribute line based on |attribute| and |value| to |message|.
474static void AddAttributeLine(const std::string& attribute, int value,
475 std::string* message) {
476 std::ostringstream os;
477 InitAttrLine(attribute, &os);
478 os << kSdpDelimiterColon << value;
479 AddLine(os.str(), message);
480}
481
482// Returns the first line of the message without the line breaker.
483static bool GetFirstLine(const std::string& message, std::string* line) {
484 size_t pos = 0;
485 if (!GetLine(message, &pos, line)) {
486 // If GetLine failed, just return the full |message|.
487 *line = message;
488 }
489 return true;
490}
491
492static bool IsLineType(const std::string& message,
493 const char type,
494 size_t line_start) {
495 if (message.size() < line_start + kLinePrefixLength) {
496 return false;
497 }
498 const char* cmessage = message.c_str();
499 return (cmessage[line_start] == type &&
500 cmessage[line_start + 1] == kSdpDelimiterEqual);
501}
502
503static bool IsLineType(const std::string& line,
504 const char type) {
505 return IsLineType(line, type, 0);
506}
507
508static bool GetLineWithType(const std::string& message, size_t* pos,
509 std::string* line, const char type) {
510 if (!IsLineType(message, type, *pos)) {
511 return false;
512 }
513
514 if (!GetLine(message, pos, line))
515 return false;
516
517 return true;
518}
519
520static bool HasAttribute(const std::string& line,
521 const std::string& attribute) {
522 return (line.compare(kLinePrefixLength, attribute.size(), attribute) == 0);
523}
524
525// Verifies the candiate to be of the format candidate:<blah>
526static bool IsRawCandidate(const std::string& line) {
527 // Checking candiadte-attribute is starting with "candidate" str.
528 if (line.compare(0, strlen(kAttributeCandidate), kAttributeCandidate) != 0) {
529 return false;
530 }
531 const size_t first_candidate = line.find(kSdpDelimiterColon);
532 if (first_candidate == std::string::npos)
533 return false;
534 // In this format we only expecting one candiate. If any additional
535 // candidates present, whole string will be discared.
536 const size_t any_other = line.find(kSdpDelimiterColon, first_candidate + 1);
537 return (any_other == std::string::npos);
538}
539
540static bool AddSsrcLine(uint32 ssrc_id, const std::string& attribute,
541 const std::string& value, std::string* message) {
542 // RFC 5576
543 // a=ssrc:<ssrc-id> <attribute>:<value>
544 std::ostringstream os;
545 InitAttrLine(kAttributeSsrc, &os);
546 os << kSdpDelimiterColon << ssrc_id << kSdpDelimiterSpace
547 << attribute << kSdpDelimiterColon << value;
548 return AddLine(os.str(), message);
549}
550
551// Split the message into two parts by the first delimiter.
552static bool SplitByDelimiter(const std::string& message,
553 const char delimiter,
554 std::string* field1,
555 std::string* field2) {
556 // Find the first delimiter
557 size_t pos = message.find(delimiter);
558 if (pos == std::string::npos) {
559 return false;
560 }
561 *field1 = message.substr(0, pos);
562 // The rest is the value.
563 *field2 = message.substr(pos + 1);
564 return true;
565}
566
567// Get value only from <attribute>:<value>.
568static bool GetValue(const std::string& message, const std::string& attribute,
569 std::string* value, SdpParseError* error) {
570 std::string leftpart;
571 if (!SplitByDelimiter(message, kSdpDelimiterColon, &leftpart, value)) {
572 return ParseFailedGetValue(message, attribute, error);
573 }
574 // The left part should end with the expected attribute.
575 if (leftpart.length() < attribute.length() ||
576 leftpart.compare(leftpart.length() - attribute.length(),
577 attribute.length(), attribute) != 0) {
578 return ParseFailedGetValue(message, attribute, error);
579 }
580 return true;
581}
582
583static bool CaseInsensitiveFind(std::string str1, std::string str2) {
584 std::transform(str1.begin(), str1.end(), str1.begin(),
585 ::tolower);
586 std::transform(str2.begin(), str2.end(), str2.begin(),
587 ::tolower);
588 return str1.find(str2) != std::string::npos;
589}
590
591void CreateTracksFromSsrcInfos(const SsrcInfoVec& ssrc_infos,
592 StreamParamsVec* tracks) {
593 ASSERT(tracks != NULL);
594 for (SsrcInfoVec::const_iterator ssrc_info = ssrc_infos.begin();
595 ssrc_info != ssrc_infos.end(); ++ssrc_info) {
596 if (ssrc_info->cname.empty()) {
597 continue;
598 }
599
600 std::string sync_label;
601 std::string track_id;
602 if (ssrc_info->msid_identifier == kDefaultMsid &&
603 !ssrc_info->mslabel.empty()) {
604 // If there's no msid and there's mslabel, we consider this is a sdp from
605 // a older version of client that doesn't support msid.
606 // In that case, we use the mslabel and label to construct the track.
607 sync_label = ssrc_info->mslabel;
608 track_id = ssrc_info->label;
609 } else {
610 sync_label = ssrc_info->msid_identifier;
611 // The appdata consists of the "id" attribute of a MediaStreamTrack, which
612 // is corresponding to the "id" attribute of StreamParams.
613 track_id = ssrc_info->msid_appdata;
614 }
615 if (sync_label.empty() || track_id.empty()) {
616 ASSERT(false);
617 continue;
618 }
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;
633 track->sync_label = sync_label;
634 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 {
668 ASSERT(false);
669 }
670 return preference;
671}
672
673// Get ip and port of the default destination from the |candidates| with
674// the given value of |component_id|.
675// RFC 5245
676// The value of |component_id| currently supported are 1 (RTP) and 2 (RTCP).
677// TODO: Decide the default destination in webrtcsession and
678// pass it down via SessionDescription.
679static bool GetDefaultDestination(const std::vector<Candidate>& candidates,
680 int component_id, std::string* port, std::string* ip) {
681 *port = kDefaultPort;
682 *ip = kDefaultAddress;
683 int current_preference = kPreferenceUnknown;
684 for (std::vector<Candidate>::const_iterator it = candidates.begin();
685 it != candidates.end(); ++it) {
686 if (it->component() != component_id) {
687 continue;
688 }
689 const int preference = GetCandidatePreferenceFromType(it->type());
690 // See if this candidate is more preferable then the current one.
691 if (preference <= current_preference) {
692 continue;
693 }
694 current_preference = preference;
695 *port = it->address().PortAsString();
696 *ip = it->address().ipaddr().ToString();
697 }
698 return true;
699}
700
701// Update the media default destination.
702static void UpdateMediaDefaultDestination(
703 const std::vector<Candidate>& candidates, std::string* mline) {
704 // RFC 4566
705 // m=<media> <port> <proto> <fmt> ...
706 std::vector<std::string> fields;
707 talk_base::split(*mline, kSdpDelimiterSpace, &fields);
708 if (fields.size() < 3) {
709 return;
710 }
711
712 bool is_rtp =
713 fields[2].empty() ||
714 talk_base::starts_with(fields[2].data(),
715 cricket::kMediaProtocolRtpPrefix);
716
717 std::ostringstream os;
718 std::string rtp_port, rtp_ip;
719 if (GetDefaultDestination(candidates, ICE_CANDIDATE_COMPONENT_RTP,
720 &rtp_port, &rtp_ip)) {
721 // Found default RTP candidate.
722 // RFC 5245
723 // The default candidates are added to the SDP as the default
724 // destination for media. For streams based on RTP, this is done by
725 // placing the IP address and port of the RTP candidate into the c and m
726 // lines, respectively.
727
728 // Update the port in the m line.
729 // If this is a m-line with port equal to 0, we don't change it.
730 if (fields[1] != kMediaPortRejected) {
731 mline->replace(fields[0].size() + 1,
732 fields[1].size(),
733 rtp_port);
734 }
735 // Add the c line.
736 // RFC 4566
737 // c=<nettype> <addrtype> <connection-address>
738 InitLine(kLineTypeConnection, kConnectionNettype, &os);
739 os << " " << kConnectionAddrtype << " " << rtp_ip;
740 AddLine(os.str(), mline);
741 }
742
743 if (is_rtp) {
744 std::string rtcp_port, rtcp_ip;
745 if (GetDefaultDestination(candidates, ICE_CANDIDATE_COMPONENT_RTCP,
746 &rtcp_port, &rtcp_ip)) {
747 // Found default RTCP candidate.
748 // RFC 5245
749 // If the agent is utilizing RTCP, it MUST encode the RTCP candidate
750 // using the a=rtcp attribute as defined in RFC 3605.
751
752 // RFC 3605
753 // rtcp-attribute = "a=rtcp:" port [nettype space addrtype space
754 // connection-address] CRLF
755 InitAttrLine(kAttributeRtcp, &os);
756 os << kSdpDelimiterColon
757 << rtcp_port << " "
758 << kConnectionNettype << " "
759 << kConnectionAddrtype << " "
760 << rtcp_ip;
761 AddLine(os.str(), mline);
762 }
763 }
764}
765
766// Get candidates according to the mline index from SessionDescriptionInterface.
767static void GetCandidatesByMindex(const SessionDescriptionInterface& desci,
768 int mline_index,
769 std::vector<Candidate>* candidates) {
770 if (!candidates) {
771 return;
772 }
773 const IceCandidateCollection* cc = desci.candidates(mline_index);
774 for (size_t i = 0; i < cc->count(); ++i) {
775 const IceCandidateInterface* candidate = cc->at(i);
776 candidates->push_back(candidate->candidate());
777 }
778}
779
780std::string SdpSerialize(const JsepSessionDescription& jdesc) {
781 std::string sdp = SdpSerializeSessionDescription(jdesc);
782
783 std::string sdp_with_candidates;
784 size_t pos = 0;
785 std::string line;
786 int mline_index = -1;
787 while (GetLine(sdp, &pos, &line)) {
788 if (IsLineType(line, kLineTypeMedia)) {
789 ++mline_index;
790 std::vector<Candidate> candidates;
791 GetCandidatesByMindex(jdesc, mline_index, &candidates);
792 // Media line may append other lines inside the
793 // UpdateMediaDefaultDestination call, so add the kLineBreak here first.
794 line.append(kLineBreak);
795 UpdateMediaDefaultDestination(candidates, &line);
796 sdp_with_candidates.append(line);
797 // Build the a=candidate lines.
798 BuildCandidate(candidates, &sdp_with_candidates);
799 } else {
800 // Copy old line to new sdp without change.
801 AddLine(line, &sdp_with_candidates);
802 }
803 }
804 sdp = sdp_with_candidates;
805
806 return sdp;
807}
808
809std::string SdpSerializeSessionDescription(
810 const JsepSessionDescription& jdesc) {
811 const cricket::SessionDescription* desc = jdesc.description();
812 if (!desc) {
813 return "";
814 }
815
816 std::string message;
817
818 // Session Description.
819 AddLine(kSessionVersion, &message);
820 // Session Origin
821 // RFC 4566
822 // o=<username> <sess-id> <sess-version> <nettype> <addrtype>
823 // <unicast-address>
824 std::ostringstream os;
825 InitLine(kLineTypeOrigin, kSessionOriginUsername, &os);
826 const std::string session_id = jdesc.session_id().empty() ?
827 kSessionOriginSessionId : jdesc.session_id();
828 const std::string session_version = jdesc.session_version().empty() ?
829 kSessionOriginSessionVersion : jdesc.session_version();
830 os << " " << session_id << " " << session_version << " "
831 << kSessionOriginNettype << " " << kSessionOriginAddrtype << " "
832 << kSessionOriginAddress;
833 AddLine(os.str(), &message);
834 AddLine(kSessionName, &message);
835
836 // Time Description.
837 AddLine(kTimeDescription, &message);
838
839 // Group
840 if (desc->HasGroup(cricket::GROUP_TYPE_BUNDLE)) {
841 std::string group_line = kAttrGroup;
842 const cricket::ContentGroup* group =
843 desc->GetGroupByName(cricket::GROUP_TYPE_BUNDLE);
844 ASSERT(group != NULL);
845 const cricket::ContentNames& content_names = group->content_names();
846 for (cricket::ContentNames::const_iterator it = content_names.begin();
847 it != content_names.end(); ++it) {
848 group_line.append(" ");
849 group_line.append(*it);
850 }
851 AddLine(group_line, &message);
852 }
853
854 // MediaStream semantics
855 InitAttrLine(kAttributeMsidSemantics, &os);
856 os << kSdpDelimiterColon << " " << kMediaStreamSemantic;
857 std::set<std::string> media_stream_labels;
858 const ContentInfo* audio_content = GetFirstAudioContent(desc);
859 if (audio_content)
860 GetMediaStreamLabels(audio_content, &media_stream_labels);
861 const ContentInfo* video_content = GetFirstVideoContent(desc);
862 if (video_content)
863 GetMediaStreamLabels(video_content, &media_stream_labels);
864 for (std::set<std::string>::const_iterator it =
865 media_stream_labels.begin(); it != media_stream_labels.end(); ++it) {
866 os << " " << *it;
867 }
868 AddLine(os.str(), &message);
869
870 if (audio_content) {
871 BuildMediaDescription(audio_content,
872 desc->GetTransportInfoByName(audio_content->name),
873 cricket::MEDIA_TYPE_AUDIO, &message);
874 }
875
876
877 if (video_content) {
878 BuildMediaDescription(video_content,
879 desc->GetTransportInfoByName(video_content->name),
880 cricket::MEDIA_TYPE_VIDEO, &message);
881 }
882
883 const ContentInfo* data_content = GetFirstDataContent(desc);
884 if (data_content) {
885 BuildMediaDescription(data_content,
886 desc->GetTransportInfoByName(data_content->name),
887 cricket::MEDIA_TYPE_DATA, &message);
888 }
889
890
891 return message;
892}
893
894// Serializes the passed in IceCandidateInterface to a SDP string.
895// candidate - The candidate to be serialized.
896std::string SdpSerializeCandidate(
897 const IceCandidateInterface& candidate) {
898 std::string message;
899 std::vector<cricket::Candidate> candidates;
900 candidates.push_back(candidate.candidate());
901 BuildCandidate(candidates, &message);
902 return message;
903}
904
905bool SdpDeserialize(const std::string& message,
906 JsepSessionDescription* jdesc,
907 SdpParseError* error) {
908 std::string session_id;
909 std::string session_version;
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +0000910 TransportDescription session_td(NS_JINGLE_ICE_UDP,
911 std::string(), std::string());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000912 RtpHeaderExtensions session_extmaps;
913 cricket::SessionDescription* desc = new cricket::SessionDescription();
914 std::vector<JsepIceCandidate*> candidates;
915 size_t current_pos = 0;
916 bool supports_msid = false;
917
918 // Session Description
919 if (!ParseSessionDescription(message, &current_pos, &session_id,
920 &session_version, &supports_msid, &session_td,
921 &session_extmaps, desc, error)) {
922 delete desc;
923 return false;
924 }
925
926 // Media Description
927 if (!ParseMediaDescription(message, session_td, session_extmaps,
928 supports_msid, &current_pos, desc, &candidates,
929 error)) {
930 delete desc;
931 for (std::vector<JsepIceCandidate*>::const_iterator
932 it = candidates.begin(); it != candidates.end(); ++it) {
933 delete *it;
934 }
935 return false;
936 }
937
938 jdesc->Initialize(desc, session_id, session_version);
939
940 for (std::vector<JsepIceCandidate*>::const_iterator
941 it = candidates.begin(); it != candidates.end(); ++it) {
942 jdesc->AddCandidate(*it);
943 delete *it;
944 }
945 return true;
946}
947
948bool SdpDeserializeCandidate(const std::string& message,
949 JsepIceCandidate* jcandidate,
950 SdpParseError* error) {
951 ASSERT(jcandidate != NULL);
952 Candidate candidate;
953 if (!ParseCandidate(message, &candidate, error, true)) {
954 return false;
955 }
956 jcandidate->SetCandidate(candidate);
957 return true;
958}
959
960bool ParseCandidate(const std::string& message, Candidate* candidate,
961 SdpParseError* error, bool is_raw) {
962 ASSERT(candidate != NULL);
963
964 // Get the first line from |message|.
965 std::string first_line;
966 GetFirstLine(message, &first_line);
967
968 size_t start_pos = kLinePrefixLength; // Starting position to parse.
969 if (IsRawCandidate(first_line)) {
970 // From WebRTC draft section 4.8.1.1 candidate-attribute will be
971 // just candidate:<candidate> not a=candidate:<blah>CRLF
972 start_pos = 0;
973 } else if (!IsLineType(first_line, kLineTypeAttributes) ||
974 !HasAttribute(first_line, kAttributeCandidate)) {
975 // Must start with a=candidate line.
976 // Expecting to be of the format a=candidate:<blah>CRLF.
977 if (is_raw) {
978 std::ostringstream description;
979 description << "Expect line: "
980 << kAttributeCandidate
981 << ":" << "<candidate-str>";
982 return ParseFailed(first_line, 0, description.str(), error);
983 } else {
984 return ParseFailedExpectLine(first_line, 0, kLineTypeAttributes,
985 kAttributeCandidate, error);
986 }
987 }
988
989 std::vector<std::string> fields;
990 talk_base::split(first_line.substr(start_pos),
991 kSdpDelimiterSpace, &fields);
992 // RFC 5245
993 // a=candidate:<foundation> <component-id> <transport> <priority>
994 // <connection-address> <port> typ <candidate-types>
995 // [raddr <connection-address>] [rport <port>]
996 // *(SP extension-att-name SP extension-att-value)
997 const size_t expected_min_fields = 8;
998 if (fields.size() < expected_min_fields ||
999 (fields[6] != kAttributeCandidateTyp)) {
1000 return ParseFailedExpectMinFieldNum(first_line, expected_min_fields, error);
1001 }
1002 std::string foundation;
1003 if (!GetValue(fields[0], kAttributeCandidate, &foundation, error)) {
1004 return false;
1005 }
1006 const int component_id = talk_base::FromString<int>(fields[1]);
1007 const std::string transport = fields[2];
1008 const uint32 priority = talk_base::FromString<uint32>(fields[3]);
1009 const std::string connection_address = fields[4];
1010 const int port = talk_base::FromString<int>(fields[5]);
1011 SocketAddress address(connection_address, port);
1012
1013 cricket::ProtocolType protocol;
1014 if (!StringToProto(transport.c_str(), &protocol)) {
1015 return ParseFailed(first_line, "Unsupported transport type.", error);
1016 }
1017
1018 std::string candidate_type;
1019 const std::string type = fields[7];
1020 if (type == kCandidateHost) {
1021 candidate_type = cricket::LOCAL_PORT_TYPE;
1022 } else if (type == kCandidateSrflx) {
1023 candidate_type = cricket::STUN_PORT_TYPE;
1024 } else if (type == kCandidateRelay) {
1025 candidate_type = cricket::RELAY_PORT_TYPE;
1026 } else {
1027 return ParseFailed(first_line, "Unsupported candidate type.", error);
1028 }
1029
1030 size_t current_position = expected_min_fields;
1031 SocketAddress related_address;
1032 // The 2 optional fields for related address
1033 // [raddr <connection-address>] [rport <port>]
1034 if (fields.size() >= (current_position + 2) &&
1035 fields[current_position] == kAttributeCandidateRaddr) {
1036 related_address.SetIP(fields[++current_position]);
1037 ++current_position;
1038 }
1039 if (fields.size() >= (current_position + 2) &&
1040 fields[current_position] == kAttributeCandidateRport) {
1041 related_address.SetPort(
1042 talk_base::FromString<int>(fields[++current_position]));
1043 ++current_position;
1044 }
1045
1046 // Extension
1047 // Empty string as the candidate username and password.
1048 // Will be updated later with the ice-ufrag and ice-pwd.
1049 // TODO: Remove the username/password extension, which is currently
1050 // kept for backwards compatibility.
1051 std::string username;
1052 std::string password;
1053 uint32 generation = 0;
1054 for (size_t i = current_position; i + 1 < fields.size(); ++i) {
1055 // RFC 5245
1056 // *(SP extension-att-name SP extension-att-value)
1057 if (fields[i] == kAttributeCandidateGeneration) {
1058 generation = talk_base::FromString<uint32>(fields[++i]);
1059 } else if (fields[i] == kAttributeCandidateUsername) {
1060 username = fields[++i];
1061 } else if (fields[i] == kAttributeCandidatePassword) {
1062 password = fields[++i];
1063 } else {
1064 // Skip the unknown extension.
1065 ++i;
1066 }
1067 }
1068
1069 // Empty string as the candidate id and network name.
1070 const std::string id;
1071 const std::string network_name;
1072 *candidate = Candidate(id, component_id, cricket::ProtoToString(protocol),
1073 address, priority, username, password, candidate_type, network_name,
1074 generation, foundation);
1075 candidate->set_related_address(related_address);
1076 return true;
1077}
1078
1079bool ParseIceOptions(const std::string& line,
1080 std::vector<std::string>* transport_options,
1081 SdpParseError* error) {
1082 std::string ice_options;
1083 if (!GetValue(line, kAttributeIceOption, &ice_options, error)) {
1084 return false;
1085 }
1086 std::vector<std::string> fields;
1087 talk_base::split(ice_options, kSdpDelimiterSpace, &fields);
1088 for (size_t i = 0; i < fields.size(); ++i) {
1089 transport_options->push_back(fields[i]);
1090 }
1091 return true;
1092}
1093
1094bool ParseExtmap(const std::string& line, RtpHeaderExtension* extmap,
1095 SdpParseError* error) {
1096 // RFC 5285
1097 // a=extmap:<value>["/"<direction>] <URI> <extensionattributes>
1098 std::vector<std::string> fields;
1099 talk_base::split(line.substr(kLinePrefixLength),
1100 kSdpDelimiterSpace, &fields);
1101 const size_t expected_min_fields = 2;
1102 if (fields.size() < expected_min_fields) {
1103 return ParseFailedExpectMinFieldNum(line, expected_min_fields, error);
1104 }
1105 std::string uri = fields[1];
1106
1107 std::string value_direction;
1108 if (!GetValue(fields[0], kAttributeExtmap, &value_direction, error)) {
1109 return false;
1110 }
1111 std::vector<std::string> sub_fields;
1112 talk_base::split(value_direction, kSdpDelimiterSlash, &sub_fields);
1113 int value = talk_base::FromString<int>(sub_fields[0]);
1114
1115 *extmap = RtpHeaderExtension(uri, value);
1116 return true;
1117}
1118
1119void BuildMediaDescription(const ContentInfo* content_info,
1120 const TransportInfo* transport_info,
1121 const MediaType media_type,
1122 std::string* message) {
1123 ASSERT(message != NULL);
1124 if (content_info == NULL || message == NULL) {
1125 return;
1126 }
1127 // TODO: Rethink if we should use sprintfn instead of stringstream.
1128 // According to the style guide, streams should only be used for logging.
1129 // http://google-styleguide.googlecode.com/svn/
1130 // trunk/cppguide.xml?showone=Streams#Streams
1131 std::ostringstream os;
1132 const MediaContentDescription* media_desc =
henrike@webrtc.org28654cb2013-07-22 21:07:49 +00001133 static_cast<const MediaContentDescription*>(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001134 content_info->description);
1135 ASSERT(media_desc != NULL);
1136
1137 bool is_sctp = (media_desc->protocol() == cricket::kMediaProtocolDtlsSctp);
1138
1139 // RFC 4566
1140 // m=<media> <port> <proto> <fmt>
1141 // fmt is a list of payload type numbers that MAY be used in the session.
1142 const char* type = NULL;
1143 if (media_type == cricket::MEDIA_TYPE_AUDIO)
1144 type = kMediaTypeAudio;
1145 else if (media_type == cricket::MEDIA_TYPE_VIDEO)
1146 type = kMediaTypeVideo;
1147 else if (media_type == cricket::MEDIA_TYPE_DATA)
1148 type = kMediaTypeData;
1149 else
1150 ASSERT(false);
1151
1152 std::string fmt;
1153 if (media_type == cricket::MEDIA_TYPE_VIDEO) {
1154 const VideoContentDescription* video_desc =
1155 static_cast<const VideoContentDescription*>(media_desc);
1156 for (std::vector<cricket::VideoCodec>::const_iterator it =
1157 video_desc->codecs().begin();
1158 it != video_desc->codecs().end(); ++it) {
1159 fmt.append(" ");
1160 fmt.append(talk_base::ToString<int>(it->id));
1161 }
1162 } else if (media_type == cricket::MEDIA_TYPE_AUDIO) {
1163 const AudioContentDescription* audio_desc =
1164 static_cast<const AudioContentDescription*>(media_desc);
1165 for (std::vector<cricket::AudioCodec>::const_iterator it =
1166 audio_desc->codecs().begin();
1167 it != audio_desc->codecs().end(); ++it) {
1168 fmt.append(" ");
1169 fmt.append(talk_base::ToString<int>(it->id));
1170 }
1171 } else if (media_type == cricket::MEDIA_TYPE_DATA) {
1172 if (is_sctp) {
1173 fmt.append(" ");
1174 // TODO(jiayl): Replace the hard-coded string with the fmt read out of the
1175 // ContentDescription.
1176 fmt.append(talk_base::ToString<int>(kDefaultSctpFmt));
1177 } else {
1178 const DataContentDescription* data_desc =
1179 static_cast<const DataContentDescription*>(media_desc);
1180 for (std::vector<cricket::DataCodec>::const_iterator it =
1181 data_desc->codecs().begin();
1182 it != data_desc->codecs().end(); ++it) {
1183 fmt.append(" ");
1184 fmt.append(talk_base::ToString<int>(it->id));
1185 }
1186 }
1187 }
1188 // The fmt must never be empty. If no codecs are found, set the fmt attribute
1189 // to 0.
1190 if (fmt.empty()) {
1191 fmt = " 0";
1192 }
1193
1194 // The port number in the m line will be updated later when associate with
1195 // the candidates.
1196 // RFC 3264
1197 // To reject an offered stream, the port number in the corresponding stream in
1198 // the answer MUST be set to zero.
1199 const std::string port = content_info->rejected ?
1200 kMediaPortRejected : kDefaultPort;
1201
1202 talk_base::SSLFingerprint* fp = (transport_info) ?
1203 transport_info->description.identity_fingerprint.get() : NULL;
1204
1205 InitLine(kLineTypeMedia, type, &os);
1206 os << " " << port << " " << media_desc->protocol() << fmt;
1207 AddLine(os.str(), message);
1208
1209 // Use the transport_info to build the media level ice-ufrag and ice-pwd.
1210 if (transport_info) {
1211 // RFC 5245
1212 // ice-pwd-att = "ice-pwd" ":" password
1213 // ice-ufrag-att = "ice-ufrag" ":" ufrag
1214 // ice-ufrag
1215 InitAttrLine(kAttributeIceUfrag, &os);
1216 os << kSdpDelimiterColon << transport_info->description.ice_ufrag;
1217 AddLine(os.str(), message);
1218 // ice-pwd
1219 InitAttrLine(kAttributeIcePwd, &os);
1220 os << kSdpDelimiterColon << transport_info->description.ice_pwd;
1221 AddLine(os.str(), message);
1222
1223 // draft-petithuguenin-mmusic-ice-attributes-level-03
1224 BuildIceOptions(transport_info->description.transport_options, message);
1225
1226 // RFC 4572
1227 // fingerprint-attribute =
1228 // "fingerprint" ":" hash-func SP fingerprint
1229 if (fp) {
1230 // Insert the fingerprint attribute.
1231 InitAttrLine(kAttributeFingerprint, &os);
1232 os << kSdpDelimiterColon
1233 << fp->algorithm << kSdpDelimiterSpace
1234 << fp->GetRfc4572Fingerprint();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001235 AddLine(os.str(), message);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001236
1237 // Inserting setup attribute.
1238 if (transport_info->description.connection_role !=
1239 cricket::CONNECTIONROLE_NONE) {
1240 // Making sure we are not using "passive" mode.
1241 cricket::ConnectionRole role =
1242 transport_info->description.connection_role;
1243 ASSERT(role == cricket::CONNECTIONROLE_ACTIVE ||
1244 role == cricket::CONNECTIONROLE_ACTPASS);
1245 InitAttrLine(kAttributeSetup, &os);
1246 std::string dtls_role_str = role == cricket::CONNECTIONROLE_ACTPASS ?
1247 cricket::CONNECTIONROLE_ACTPASS_STR :
1248 cricket::CONNECTIONROLE_ACTIVE_STR;
1249 os << kSdpDelimiterColon << dtls_role_str;
1250 AddLine(os.str(), message);
1251 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001252 }
1253 }
1254
1255 // RFC 3388
1256 // mid-attribute = "a=mid:" identification-tag
1257 // identification-tag = token
1258 // Use the content name as the mid identification-tag.
1259 InitAttrLine(kAttributeMid, &os);
1260 os << kSdpDelimiterColon << content_info->name;
1261 AddLine(os.str(), message);
1262
1263 if (is_sctp) {
1264 BuildSctpContentAttributes(message);
1265 } else {
1266 BuildRtpContentAttributes(media_desc, media_type, message);
1267 }
1268}
1269
1270void BuildSctpContentAttributes(std::string* message) {
1271 cricket::DataCodec sctp_codec(kDefaultSctpFmt, kDefaultSctpFmtProtocol, 0);
1272 sctp_codec.SetParam(kCodecParamSctpProtocol, kDefaultSctpFmtProtocol);
1273 sctp_codec.SetParam(kCodecParamSctpStreams, cricket::kMaxSctpSid + 1);
1274 AddFmtpLine(sctp_codec, message);
1275}
1276
1277void BuildRtpContentAttributes(
1278 const MediaContentDescription* media_desc,
1279 const MediaType media_type,
1280 std::string* message) {
1281 std::ostringstream os;
1282 // RFC 5285
1283 // a=extmap:<value>["/"<direction>] <URI> <extensionattributes>
1284 // The definitions MUST be either all session level or all media level. This
1285 // implementation uses all media level.
1286 for (size_t i = 0; i < media_desc->rtp_header_extensions().size(); ++i) {
1287 InitAttrLine(kAttributeExtmap, &os);
1288 os << kSdpDelimiterColon << media_desc->rtp_header_extensions()[i].id
1289 << kSdpDelimiterSpace << media_desc->rtp_header_extensions()[i].uri;
1290 AddLine(os.str(), message);
1291 }
1292
1293 // RFC 3264
1294 // a=sendrecv || a=sendonly || a=sendrecv || a=inactive
1295
1296 cricket::MediaContentDirection direction = media_desc->direction();
1297 if (media_desc->streams().empty() && direction == cricket::MD_SENDRECV) {
1298 direction = cricket::MD_RECVONLY;
1299 }
1300
1301 switch (direction) {
1302 case cricket::MD_INACTIVE:
1303 InitAttrLine(kAttributeInactive, &os);
1304 break;
1305 case cricket::MD_SENDONLY:
1306 InitAttrLine(kAttributeSendOnly, &os);
1307 break;
1308 case cricket::MD_RECVONLY:
1309 InitAttrLine(kAttributeRecvOnly, &os);
1310 break;
1311 case cricket::MD_SENDRECV:
1312 default:
1313 InitAttrLine(kAttributeSendRecv, &os);
1314 break;
1315 }
1316 AddLine(os.str(), message);
1317
1318 // RFC 4566
1319 // b=AS:<bandwidth>
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001320 // We should always use the default bandwidth for RTP-based data
1321 // channels. Don't allow SDP to set the bandwidth, because that
1322 // would give JS the opportunity to "break the Internet".
sergeyu@chromium.orga59696b2013-09-13 23:48:58 +00001323 // TODO(pthatcher): But we need to temporarily allow the SDP to control
1324 // this for backwards-compatibility. Once we don't need that any
1325 // more, remove this.
1326 bool support_dc_sdp_bandwidth_temporarily = true;
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001327 if (media_desc->bandwidth() >= 1000 &&
sergeyu@chromium.orga59696b2013-09-13 23:48:58 +00001328 (media_type != cricket::MEDIA_TYPE_DATA ||
1329 support_dc_sdp_bandwidth_temporarily)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001330 InitLine(kLineTypeSessionBandwidth, kApplicationSpecificMaximum, &os);
1331 os << kSdpDelimiterColon << (media_desc->bandwidth() / 1000);
1332 AddLine(os.str(), message);
1333 }
1334
1335 // RFC 5761
1336 // a=rtcp-mux
1337 if (media_desc->rtcp_mux()) {
1338 InitAttrLine(kAttributeRtcpMux, &os);
1339 AddLine(os.str(), message);
1340 }
1341
1342 // RFC 4568
1343 // a=crypto:<tag> <crypto-suite> <key-params> [<session-params>]
1344 for (std::vector<CryptoParams>::const_iterator it =
1345 media_desc->cryptos().begin();
1346 it != media_desc->cryptos().end(); ++it) {
1347 InitAttrLine(kAttributeCrypto, &os);
1348 os << kSdpDelimiterColon << it->tag << " " << it->cipher_suite << " "
1349 << it->key_params;
1350 if (!it->session_params.empty()) {
1351 os << " " << it->session_params;
1352 }
1353 AddLine(os.str(), message);
1354 }
1355
1356 // RFC 4566
1357 // a=rtpmap:<payload type> <encoding name>/<clock rate>
1358 // [/<encodingparameters>]
1359 BuildRtpMap(media_desc, media_type, message);
1360
1361 // Specify latency for buffered mode.
1362 // a=x-google-buffer-latency:<value>
1363 if (media_desc->buffered_mode_latency() != cricket::kBufferedModeDisabled) {
1364 std::ostringstream os;
1365 InitAttrLine(kAttributeXGoogleBufferLatency, &os);
1366 os << kSdpDelimiterColon << media_desc->buffered_mode_latency();
1367 AddLine(os.str(), message);
1368 }
1369
1370 for (StreamParamsVec::const_iterator track = media_desc->streams().begin();
1371 track != media_desc->streams().end(); ++track) {
1372 // Require that the track belongs to a media stream,
1373 // ie the sync_label is set. This extra check is necessary since the
1374 // MediaContentDescription always contains a streamparam with an ssrc even
1375 // if no track or media stream have been created.
1376 if (track->sync_label.empty()) continue;
1377
1378 // Build the ssrc-group lines.
1379 for (size_t i = 0; i < track->ssrc_groups.size(); ++i) {
1380 // RFC 5576
1381 // a=ssrc-group:<semantics> <ssrc-id> ...
1382 if (track->ssrc_groups[i].ssrcs.empty()) {
1383 continue;
1384 }
1385 std::ostringstream os;
1386 InitAttrLine(kAttributeSsrcGroup, &os);
1387 os << kSdpDelimiterColon << track->ssrc_groups[i].semantics;
1388 std::vector<uint32>::const_iterator ssrc =
1389 track->ssrc_groups[i].ssrcs.begin();
1390 for (; ssrc != track->ssrc_groups[i].ssrcs.end(); ++ssrc) {
1391 os << kSdpDelimiterSpace << talk_base::ToString<uint32>(*ssrc);
1392 }
1393 AddLine(os.str(), message);
1394 }
1395 // Build the ssrc lines for each ssrc.
1396 for (size_t i = 0; i < track->ssrcs.size(); ++i) {
1397 uint32 ssrc = track->ssrcs[i];
1398 // RFC 5576
1399 // a=ssrc:<ssrc-id> cname:<value>
1400 AddSsrcLine(ssrc, kSsrcAttributeCname,
1401 track->cname, message);
1402
1403 // draft-alvestrand-mmusic-msid-00
1404 // a=ssrc:<ssrc-id> msid:identifier [appdata]
1405 // The appdata consists of the "id" attribute of a MediaStreamTrack, which
1406 // is corresponding to the "name" attribute of StreamParams.
1407 std::string appdata = track->id;
1408 std::ostringstream os;
1409 InitAttrLine(kAttributeSsrc, &os);
1410 os << kSdpDelimiterColon << ssrc << kSdpDelimiterSpace
1411 << kSsrcAttributeMsid << kSdpDelimiterColon << track->sync_label
1412 << kSdpDelimiterSpace << appdata;
1413 AddLine(os.str(), message);
1414
1415 // TODO(ronghuawu): Remove below code which is for backward compatibility.
1416 // draft-alvestrand-rtcweb-mid-01
1417 // a=ssrc:<ssrc-id> mslabel:<value>
1418 // The label isn't yet defined.
1419 // a=ssrc:<ssrc-id> label:<value>
1420 AddSsrcLine(ssrc, kSsrcAttributeMslabel, track->sync_label, message);
1421 AddSsrcLine(ssrc, kSSrcAttributeLabel, track->id, message);
1422 }
1423 }
1424}
1425
1426void WriteFmtpHeader(int payload_type, std::ostringstream* os) {
1427 // fmtp header: a=fmtp:|payload_type| <parameters>
1428 // Add a=fmtp
1429 InitAttrLine(kAttributeFmtp, os);
1430 // Add :|payload_type|
1431 *os << kSdpDelimiterColon << payload_type;
1432}
1433
1434void WriteRtcpFbHeader(int payload_type, std::ostringstream* os) {
1435 // rtcp-fb header: a=rtcp-fb:|payload_type|
1436 // <parameters>/<ccm <ccm_parameters>>
1437 // Add a=rtcp-fb
1438 InitAttrLine(kAttributeRtcpFb, os);
1439 // Add :
1440 *os << kSdpDelimiterColon;
1441 if (payload_type == kWildcardPayloadType) {
1442 *os << "*";
1443 } else {
1444 *os << payload_type;
1445 }
1446}
1447
1448void WriteFmtpParameter(const std::string& parameter_name,
1449 const std::string& parameter_value,
1450 std::ostringstream* os) {
1451 // fmtp parameters: |parameter_name|=|parameter_value|
1452 *os << parameter_name << kSdpDelimiterEqual << parameter_value;
1453}
1454
1455void WriteFmtpParameters(const cricket::CodecParameterMap& parameters,
1456 std::ostringstream* os) {
1457 for (cricket::CodecParameterMap::const_iterator fmtp = parameters.begin();
1458 fmtp != parameters.end(); ++fmtp) {
1459 // Each new parameter, except the first one starts with ";" and " ".
1460 if (fmtp != parameters.begin()) {
1461 *os << kSdpDelimiterSemicolon;
1462 }
1463 *os << kSdpDelimiterSpace;
1464 WriteFmtpParameter(fmtp->first, fmtp->second, os);
1465 }
1466}
1467
1468bool IsFmtpParam(const std::string& name) {
1469 const char* kFmtpParams[] = {
1470 kCodecParamMinPTime, kCodecParamSPropStereo,
1471 kCodecParamStereo, kCodecParamUseInbandFec,
1472 kCodecParamMaxBitrate, kCodecParamMinBitrate, kCodecParamMaxQuantization,
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001473 kCodecParamSctpProtocol, kCodecParamSctpStreams,
1474 kCodecParamMaxAverageBitrate
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001475 };
1476 for (size_t i = 0; i < ARRAY_SIZE(kFmtpParams); ++i) {
1477 if (_stricmp(name.c_str(), kFmtpParams[i]) == 0) {
1478 return true;
1479 }
1480 }
1481 return false;
1482}
1483
1484// Retreives fmtp parameters from |params|, which may contain other parameters
1485// as well, and puts them in |fmtp_parameters|.
1486void GetFmtpParams(const cricket::CodecParameterMap& params,
1487 cricket::CodecParameterMap* fmtp_parameters) {
1488 for (cricket::CodecParameterMap::const_iterator iter = params.begin();
1489 iter != params.end(); ++iter) {
1490 if (IsFmtpParam(iter->first)) {
1491 (*fmtp_parameters)[iter->first] = iter->second;
1492 }
1493 }
1494}
1495
1496template <class T>
1497void AddFmtpLine(const T& codec, std::string* message) {
1498 cricket::CodecParameterMap fmtp_parameters;
1499 GetFmtpParams(codec.params, &fmtp_parameters);
1500 if (fmtp_parameters.empty()) {
1501 // No need to add an fmtp if it will have no (optional) parameters.
1502 return;
1503 }
1504 std::ostringstream os;
1505 WriteFmtpHeader(codec.id, &os);
1506 WriteFmtpParameters(fmtp_parameters, &os);
1507 AddLine(os.str(), message);
1508 return;
1509}
1510
1511template <class T>
1512void AddRtcpFbLines(const T& codec, std::string* message) {
1513 for (std::vector<cricket::FeedbackParam>::const_iterator iter =
1514 codec.feedback_params.params().begin();
1515 iter != codec.feedback_params.params().end(); ++iter) {
1516 std::ostringstream os;
1517 WriteRtcpFbHeader(codec.id, &os);
1518 os << " " << iter->id();
1519 if (!iter->param().empty()) {
1520 os << " " << iter->param();
1521 }
1522 AddLine(os.str(), message);
1523 }
1524}
1525
1526bool GetMinValue(const std::vector<int>& values, int* value) {
1527 if (values.empty()) {
1528 return false;
1529 }
1530 std::vector<int>::const_iterator found =
1531 std::min_element(values.begin(), values.end());
1532 *value = *found;
1533 return true;
1534}
1535
1536bool GetParameter(const std::string& name,
1537 const cricket::CodecParameterMap& params, int* value) {
1538 std::map<std::string, std::string>::const_iterator found =
1539 params.find(name);
1540 if (found == params.end()) {
1541 return false;
1542 }
1543 *value = talk_base::FromString<int>(found->second);
1544 return true;
1545}
1546
1547void BuildRtpMap(const MediaContentDescription* media_desc,
1548 const MediaType media_type,
1549 std::string* message) {
1550 ASSERT(message != NULL);
1551 ASSERT(media_desc != NULL);
1552 std::ostringstream os;
1553 if (media_type == cricket::MEDIA_TYPE_VIDEO) {
1554 const VideoContentDescription* video_desc =
1555 static_cast<const VideoContentDescription*>(media_desc);
1556 for (std::vector<cricket::VideoCodec>::const_iterator it =
1557 video_desc->codecs().begin();
1558 it != video_desc->codecs().end(); ++it) {
1559 // RFC 4566
1560 // a=rtpmap:<payload type> <encoding name>/<clock rate>
1561 // [/<encodingparameters>]
1562 if (it->id != kWildcardPayloadType) {
1563 InitAttrLine(kAttributeRtpmap, &os);
1564 os << kSdpDelimiterColon << it->id << " " << it->name
1565 << "/" << kDefaultVideoClockrate;
1566 AddLine(os.str(), message);
1567 }
1568 AddRtcpFbLines(*it, message);
1569 AddFmtpLine(*it, message);
1570 }
1571 } else if (media_type == cricket::MEDIA_TYPE_AUDIO) {
1572 const AudioContentDescription* audio_desc =
1573 static_cast<const AudioContentDescription*>(media_desc);
1574 std::vector<int> ptimes;
1575 std::vector<int> maxptimes;
1576 int max_minptime = 0;
1577 for (std::vector<cricket::AudioCodec>::const_iterator it =
1578 audio_desc->codecs().begin();
1579 it != audio_desc->codecs().end(); ++it) {
1580 ASSERT(!it->name.empty());
1581 // RFC 4566
1582 // a=rtpmap:<payload type> <encoding name>/<clock rate>
1583 // [/<encodingparameters>]
1584 InitAttrLine(kAttributeRtpmap, &os);
1585 os << kSdpDelimiterColon << it->id << " ";
1586 os << it->name << "/" << it->clockrate;
1587 if (it->channels != 1) {
1588 os << "/" << it->channels;
1589 }
1590 AddLine(os.str(), message);
1591 AddRtcpFbLines(*it, message);
1592 AddFmtpLine(*it, message);
1593 int minptime = 0;
1594 if (GetParameter(kCodecParamMinPTime, it->params, &minptime)) {
1595 max_minptime = std::max(minptime, max_minptime);
1596 }
1597 int ptime;
1598 if (GetParameter(kCodecParamPTime, it->params, &ptime)) {
1599 ptimes.push_back(ptime);
1600 }
1601 int maxptime;
1602 if (GetParameter(kCodecParamMaxPTime, it->params, &maxptime)) {
1603 maxptimes.push_back(maxptime);
1604 }
1605 }
1606 // Populate the maxptime attribute with the smallest maxptime of all codecs
1607 // under the same m-line.
1608 int min_maxptime = INT_MAX;
1609 if (GetMinValue(maxptimes, &min_maxptime)) {
1610 AddAttributeLine(kCodecParamMaxPTime, min_maxptime, message);
1611 }
1612 ASSERT(min_maxptime > max_minptime);
1613 // Populate the ptime attribute with the smallest ptime or the largest
1614 // minptime, whichever is the largest, for all codecs under the same m-line.
1615 int ptime = INT_MAX;
1616 if (GetMinValue(ptimes, &ptime)) {
1617 ptime = std::min(ptime, min_maxptime);
1618 ptime = std::max(ptime, max_minptime);
1619 AddAttributeLine(kCodecParamPTime, ptime, message);
1620 }
1621 } else if (media_type == cricket::MEDIA_TYPE_DATA) {
1622 const DataContentDescription* data_desc =
1623 static_cast<const DataContentDescription*>(media_desc);
1624 for (std::vector<cricket::DataCodec>::const_iterator it =
1625 data_desc->codecs().begin();
1626 it != data_desc->codecs().end(); ++it) {
1627 // RFC 4566
1628 // a=rtpmap:<payload type> <encoding name>/<clock rate>
1629 // [/<encodingparameters>]
1630 InitAttrLine(kAttributeRtpmap, &os);
1631 os << kSdpDelimiterColon << it->id << " "
1632 << it->name << "/" << it->clockrate;
1633 AddLine(os.str(), message);
1634 }
1635 }
1636}
1637
1638void BuildCandidate(const std::vector<Candidate>& candidates,
1639 std::string* message) {
1640 std::ostringstream os;
1641
1642 for (std::vector<Candidate>::const_iterator it = candidates.begin();
1643 it != candidates.end(); ++it) {
1644 // RFC 5245
1645 // a=candidate:<foundation> <component-id> <transport> <priority>
1646 // <connection-address> <port> typ <candidate-types>
1647 // [raddr <connection-address>] [rport <port>]
1648 // *(SP extension-att-name SP extension-att-value)
1649 std::string type;
1650 // Map the cricket candidate type to "host" / "srflx" / "prflx" / "relay"
1651 if (it->type() == cricket::LOCAL_PORT_TYPE) {
1652 type = kCandidateHost;
1653 } else if (it->type() == cricket::STUN_PORT_TYPE) {
1654 type = kCandidateSrflx;
1655 } else if (it->type() == cricket::RELAY_PORT_TYPE) {
1656 type = kCandidateRelay;
1657 } else {
1658 ASSERT(false);
1659 }
1660
1661 InitAttrLine(kAttributeCandidate, &os);
1662 os << kSdpDelimiterColon
1663 << it->foundation() << " " << it->component() << " "
1664 << it->protocol() << " " << it->priority() << " "
1665 << it->address().ipaddr().ToString() << " "
1666 << it->address().PortAsString() << " "
1667 << kAttributeCandidateTyp << " " << type << " ";
1668
1669 // Related address
1670 if (!it->related_address().IsNil()) {
1671 os << kAttributeCandidateRaddr << " "
1672 << it->related_address().ipaddr().ToString() << " "
1673 << kAttributeCandidateRport << " "
1674 << it->related_address().PortAsString() << " ";
1675 }
1676
1677 // Extensions
1678 os << kAttributeCandidateGeneration << " " << it->generation();
1679
1680 AddLine(os.str(), message);
1681 }
1682}
1683
1684void BuildIceOptions(const std::vector<std::string>& transport_options,
1685 std::string* message) {
1686 if (!transport_options.empty()) {
1687 std::ostringstream os;
1688 InitAttrLine(kAttributeIceOption, &os);
1689 os << kSdpDelimiterColon << transport_options[0];
1690 for (size_t i = 1; i < transport_options.size(); ++i) {
1691 os << kSdpDelimiterSpace << transport_options[i];
1692 }
1693 AddLine(os.str(), message);
1694 }
1695}
1696
1697bool ParseSessionDescription(const std::string& message, size_t* pos,
1698 std::string* session_id,
1699 std::string* session_version,
1700 bool* supports_msid,
1701 TransportDescription* session_td,
1702 RtpHeaderExtensions* session_extmaps,
1703 cricket::SessionDescription* desc,
1704 SdpParseError* error) {
1705 std::string line;
1706
1707 // RFC 4566
1708 // v= (protocol version)
1709 if (!GetLineWithType(message, pos, &line, kLineTypeVersion)) {
1710 return ParseFailedExpectLine(message, *pos, kLineTypeVersion,
1711 std::string(), error);
1712 }
1713 // RFC 4566
1714 // o=<username> <sess-id> <sess-version> <nettype> <addrtype>
1715 // <unicast-address>
1716 if (!GetLineWithType(message, pos, &line, kLineTypeOrigin)) {
1717 return ParseFailedExpectLine(message, *pos, kLineTypeOrigin,
1718 std::string(), error);
1719 }
1720 std::vector<std::string> fields;
1721 talk_base::split(line.substr(kLinePrefixLength),
1722 kSdpDelimiterSpace, &fields);
1723 const size_t expected_fields = 6;
1724 if (fields.size() != expected_fields) {
1725 return ParseFailedExpectFieldNum(line, expected_fields, error);
1726 }
1727 *session_id = fields[1];
1728 *session_version = fields[2];
1729
1730 // RFC 4566
1731 // s= (session name)
1732 if (!GetLineWithType(message, pos, &line, kLineTypeSessionName)) {
1733 return ParseFailedExpectLine(message, *pos, kLineTypeSessionName,
1734 std::string(), error);
1735 }
1736
1737 // Optional lines
1738 // Those are the optional lines, so shouldn't return false if not present.
1739 // RFC 4566
1740 // i=* (session information)
1741 GetLineWithType(message, pos, &line, kLineTypeSessionInfo);
1742
1743 // RFC 4566
1744 // u=* (URI of description)
1745 GetLineWithType(message, pos, &line, kLineTypeSessionUri);
1746
1747 // RFC 4566
1748 // e=* (email address)
1749 GetLineWithType(message, pos, &line, kLineTypeSessionEmail);
1750
1751 // RFC 4566
1752 // p=* (phone number)
1753 GetLineWithType(message, pos, &line, kLineTypeSessionPhone);
1754
1755 // RFC 4566
1756 // c=* (connection information -- not required if included in
1757 // all media)
1758 GetLineWithType(message, pos, &line, kLineTypeConnection);
1759
1760 // RFC 4566
1761 // b=* (zero or more bandwidth information lines)
1762 while (GetLineWithType(message, pos, &line, kLineTypeSessionBandwidth)) {
1763 // By pass zero or more b lines.
1764 }
1765
1766 // RFC 4566
1767 // One or more time descriptions ("t=" and "r=" lines; see below)
1768 // t= (time the session is active)
1769 // r=* (zero or more repeat times)
1770 // Ensure there's at least one time description
1771 if (!GetLineWithType(message, pos, &line, kLineTypeTiming)) {
1772 return ParseFailedExpectLine(message, *pos, kLineTypeTiming, std::string(),
1773 error);
1774 }
1775
1776 while (GetLineWithType(message, pos, &line, kLineTypeRepeatTimes)) {
1777 // By pass zero or more r lines.
1778 }
1779
1780 // Go through the rest of the time descriptions
1781 while (GetLineWithType(message, pos, &line, kLineTypeTiming)) {
1782 while (GetLineWithType(message, pos, &line, kLineTypeRepeatTimes)) {
1783 // By pass zero or more r lines.
1784 }
1785 }
1786
1787 // RFC 4566
1788 // z=* (time zone adjustments)
1789 GetLineWithType(message, pos, &line, kLineTypeTimeZone);
1790
1791 // RFC 4566
1792 // k=* (encryption key)
1793 GetLineWithType(message, pos, &line, kLineTypeEncryptionKey);
1794
1795 // RFC 4566
1796 // a=* (zero or more session attribute lines)
1797 while (GetLineWithType(message, pos, &line, kLineTypeAttributes)) {
1798 if (HasAttribute(line, kAttributeGroup)) {
1799 if (!ParseGroupAttribute(line, desc, error)) {
1800 return false;
1801 }
1802 } else if (HasAttribute(line, kAttributeIceUfrag)) {
1803 if (!GetValue(line, kAttributeIceUfrag,
1804 &(session_td->ice_ufrag), error)) {
1805 return false;
1806 }
1807 } else if (HasAttribute(line, kAttributeIcePwd)) {
1808 if (!GetValue(line, kAttributeIcePwd, &(session_td->ice_pwd), error)) {
1809 return false;
1810 }
1811 } else if (HasAttribute(line, kAttributeIceLite)) {
1812 session_td->ice_mode = cricket::ICEMODE_LITE;
1813 } else if (HasAttribute(line, kAttributeIceOption)) {
1814 if (!ParseIceOptions(line, &(session_td->transport_options), error)) {
1815 return false;
1816 }
1817 } else if (HasAttribute(line, kAttributeFingerprint)) {
1818 if (session_td->identity_fingerprint.get()) {
1819 return ParseFailed(
1820 line,
1821 "Can't have multiple fingerprint attributes at the same level.",
1822 error);
1823 }
1824 talk_base::SSLFingerprint* fingerprint = NULL;
1825 if (!ParseFingerprintAttribute(line, &fingerprint, error)) {
1826 return false;
1827 }
1828 session_td->identity_fingerprint.reset(fingerprint);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001829 } else if (HasAttribute(line, kAttributeSetup)) {
1830 if (!ParseDtlsSetup(line, &(session_td->connection_role), error)) {
1831 return false;
1832 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001833 } else if (HasAttribute(line, kAttributeMsidSemantics)) {
1834 std::string semantics;
1835 if (!GetValue(line, kAttributeMsidSemantics, &semantics, error)) {
1836 return false;
1837 }
1838 *supports_msid = CaseInsensitiveFind(semantics, kMediaStreamSemantic);
1839 } else if (HasAttribute(line, kAttributeExtmap)) {
1840 RtpHeaderExtension extmap;
1841 if (!ParseExtmap(line, &extmap, error)) {
1842 return false;
1843 }
1844 session_extmaps->push_back(extmap);
1845 }
1846 }
1847
1848 return true;
1849}
1850
1851bool ParseGroupAttribute(const std::string& line,
1852 cricket::SessionDescription* desc,
1853 SdpParseError* error) {
1854 ASSERT(desc != NULL);
1855
1856 // RFC 5888 and draft-holmberg-mmusic-sdp-bundle-negotiation-00
1857 // a=group:BUNDLE video voice
1858 std::vector<std::string> fields;
1859 talk_base::split(line.substr(kLinePrefixLength),
1860 kSdpDelimiterSpace, &fields);
1861 std::string semantics;
1862 if (!GetValue(fields[0], kAttributeGroup, &semantics, error)) {
1863 return false;
1864 }
1865 cricket::ContentGroup group(semantics);
1866 for (size_t i = 1; i < fields.size(); ++i) {
1867 group.AddContentName(fields[i]);
1868 }
1869 desc->AddGroup(group);
1870 return true;
1871}
1872
1873static bool ParseFingerprintAttribute(const std::string& line,
1874 talk_base::SSLFingerprint** fingerprint,
1875 SdpParseError* error) {
1876 if (!IsLineType(line, kLineTypeAttributes) ||
1877 !HasAttribute(line, kAttributeFingerprint)) {
1878 return ParseFailedExpectLine(line, 0, kLineTypeAttributes,
1879 kAttributeFingerprint, error);
1880 }
1881
1882 std::vector<std::string> fields;
1883 talk_base::split(line.substr(kLinePrefixLength),
1884 kSdpDelimiterSpace, &fields);
1885 const size_t expected_fields = 2;
1886 if (fields.size() != expected_fields) {
1887 return ParseFailedExpectFieldNum(line, expected_fields, error);
1888 }
1889
1890 // The first field here is "fingerprint:<hash>.
1891 std::string algorithm;
1892 if (!GetValue(fields[0], kAttributeFingerprint, &algorithm, error)) {
1893 return false;
1894 }
1895
1896 // Downcase the algorithm. Note that we don't need to downcase the
1897 // fingerprint because hex_decode can handle upper-case.
1898 std::transform(algorithm.begin(), algorithm.end(), algorithm.begin(),
1899 ::tolower);
1900
1901 // The second field is the digest value. De-hexify it.
1902 *fingerprint = talk_base::SSLFingerprint::CreateFromRfc4572(
1903 algorithm, fields[1]);
1904 if (!*fingerprint) {
1905 return ParseFailed(line,
1906 "Failed to create fingerprint from the digest.",
1907 error);
1908 }
1909
1910 return true;
1911}
1912
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001913static bool ParseDtlsSetup(const std::string& line,
1914 cricket::ConnectionRole* role,
1915 SdpParseError* error) {
1916 // setup-attr = "a=setup:" role
1917 // role = "active" / "passive" / "actpass" / "holdconn"
1918 std::vector<std::string> fields;
1919 talk_base::split(line.substr(kLinePrefixLength), kSdpDelimiterColon, &fields);
1920 const size_t expected_fields = 2;
1921 if (fields.size() != expected_fields) {
1922 return ParseFailedExpectFieldNum(line, expected_fields, error);
1923 }
1924 std::string role_str = fields[1];
1925 if (!cricket::StringToConnectionRole(role_str, role)) {
1926 return ParseFailed(line, "Invalid attribute value.", error);
1927 }
1928 return true;
1929}
1930
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001931// RFC 3551
1932// PT encoding media type clock rate channels
1933// name (Hz)
1934// 0 PCMU A 8,000 1
1935// 1 reserved A
1936// 2 reserved A
1937// 3 GSM A 8,000 1
1938// 4 G723 A 8,000 1
1939// 5 DVI4 A 8,000 1
1940// 6 DVI4 A 16,000 1
1941// 7 LPC A 8,000 1
1942// 8 PCMA A 8,000 1
1943// 9 G722 A 8,000 1
1944// 10 L16 A 44,100 2
1945// 11 L16 A 44,100 1
1946// 12 QCELP A 8,000 1
1947// 13 CN A 8,000 1
1948// 14 MPA A 90,000 (see text)
1949// 15 G728 A 8,000 1
1950// 16 DVI4 A 11,025 1
1951// 17 DVI4 A 22,050 1
1952// 18 G729 A 8,000 1
1953struct StaticPayloadAudioCodec {
1954 const char* name;
1955 int clockrate;
1956 int channels;
1957};
1958static const StaticPayloadAudioCodec kStaticPayloadAudioCodecs[] = {
1959 { "PCMU", 8000, 1 },
1960 { "reserved", 0, 0 },
1961 { "reserved", 0, 0 },
1962 { "GSM", 8000, 1 },
1963 { "G723", 8000, 1 },
1964 { "DVI4", 8000, 1 },
1965 { "DVI4", 16000, 1 },
1966 { "LPC", 8000, 1 },
1967 { "PCMA", 8000, 1 },
1968 { "G722", 8000, 1 },
1969 { "L16", 44100, 2 },
1970 { "L16", 44100, 1 },
1971 { "QCELP", 8000, 1 },
1972 { "CN", 8000, 1 },
1973 { "MPA", 90000, 1 },
1974 { "G728", 8000, 1 },
1975 { "DVI4", 11025, 1 },
1976 { "DVI4", 22050, 1 },
1977 { "G729", 8000, 1 },
1978};
1979
1980void MaybeCreateStaticPayloadAudioCodecs(
1981 const std::vector<int>& fmts, AudioContentDescription* media_desc) {
1982 if (!media_desc) {
1983 return;
1984 }
henrike@webrtc.org28654cb2013-07-22 21:07:49 +00001985 int preference = static_cast<int>(fmts.size());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001986 std::vector<int>::const_iterator it = fmts.begin();
1987 bool add_new_codec = false;
1988 for (; it != fmts.end(); ++it) {
1989 int payload_type = *it;
1990 if (!media_desc->HasCodec(payload_type) &&
1991 payload_type >= 0 &&
1992 payload_type < ARRAY_SIZE(kStaticPayloadAudioCodecs)) {
1993 std::string encoding_name = kStaticPayloadAudioCodecs[payload_type].name;
1994 int clock_rate = kStaticPayloadAudioCodecs[payload_type].clockrate;
1995 int channels = kStaticPayloadAudioCodecs[payload_type].channels;
1996 media_desc->AddCodec(cricket::AudioCodec(payload_type, encoding_name,
1997 clock_rate, 0, channels,
1998 preference));
1999 add_new_codec = true;
2000 }
2001 --preference;
2002 }
2003 if (add_new_codec) {
2004 media_desc->SortCodecs();
2005 }
2006}
2007
2008template <class C>
2009static C* ParseContentDescription(const std::string& message,
2010 const MediaType media_type,
2011 int mline_index,
2012 const std::string& protocol,
2013 const std::vector<int>& codec_preference,
2014 size_t* pos,
2015 std::string* content_name,
2016 TransportDescription* transport,
2017 std::vector<JsepIceCandidate*>* candidates,
2018 webrtc::SdpParseError* error) {
2019 C* media_desc = new C();
2020 switch (media_type) {
2021 case cricket::MEDIA_TYPE_AUDIO:
2022 *content_name = cricket::CN_AUDIO;
2023 break;
2024 case cricket::MEDIA_TYPE_VIDEO:
2025 *content_name = cricket::CN_VIDEO;
2026 break;
2027 case cricket::MEDIA_TYPE_DATA:
2028 *content_name = cricket::CN_DATA;
2029 break;
2030 default:
2031 ASSERT(false);
2032 break;
2033 }
2034 if (!ParseContent(message, media_type, mline_index, protocol,
2035 codec_preference, pos, content_name,
2036 media_desc, transport, candidates, error)) {
2037 delete media_desc;
2038 return NULL;
2039 }
2040 // Sort the codecs according to the m-line fmt list.
2041 media_desc->SortCodecs();
2042 return media_desc;
2043}
2044
2045bool ParseMediaDescription(const std::string& message,
2046 const TransportDescription& session_td,
2047 const RtpHeaderExtensions& session_extmaps,
2048 bool supports_msid,
2049 size_t* pos,
2050 cricket::SessionDescription* desc,
2051 std::vector<JsepIceCandidate*>* candidates,
2052 SdpParseError* error) {
2053 ASSERT(desc != NULL);
2054 std::string line;
2055 int mline_index = -1;
2056
2057 // Zero or more media descriptions
2058 // RFC 4566
2059 // m=<media> <port> <proto> <fmt>
2060 while (GetLineWithType(message, pos, &line, kLineTypeMedia)) {
2061 ++mline_index;
2062
2063 std::vector<std::string> fields;
2064 talk_base::split(line.substr(kLinePrefixLength),
2065 kSdpDelimiterSpace, &fields);
2066 const size_t expected_min_fields = 4;
2067 if (fields.size() < expected_min_fields) {
2068 return ParseFailedExpectMinFieldNum(line, expected_min_fields, error);
2069 }
2070 bool rejected = false;
2071 // RFC 3264
2072 // To reject an offered stream, the port number in the corresponding stream
2073 // in the answer MUST be set to zero.
2074 if (fields[1] == kMediaPortRejected) {
2075 rejected = true;
2076 }
2077
2078 std::string protocol = fields[2];
2079 bool is_sctp = (protocol == cricket::kMediaProtocolDtlsSctp);
2080
2081 // <fmt>
2082 std::vector<int> codec_preference;
2083 for (size_t j = 3 ; j < fields.size(); ++j) {
2084 codec_preference.push_back(talk_base::FromString<int>(fields[j]));
2085 }
2086
2087 // Make a temporary TransportDescription based on |session_td|.
2088 // Some of this gets overwritten by ParseContent.
2089 TransportDescription transport(NS_JINGLE_ICE_UDP,
2090 session_td.transport_options,
2091 session_td.ice_ufrag,
2092 session_td.ice_pwd,
2093 session_td.ice_mode,
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00002094 session_td.connection_role,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002095 session_td.identity_fingerprint.get(),
2096 Candidates());
2097
2098 talk_base::scoped_ptr<MediaContentDescription> content;
2099 std::string content_name;
2100 if (HasAttribute(line, kMediaTypeVideo)) {
2101 content.reset(ParseContentDescription<VideoContentDescription>(
2102 message, cricket::MEDIA_TYPE_VIDEO, mline_index, protocol,
2103 codec_preference, pos, &content_name,
2104 &transport, candidates, error));
2105 } else if (HasAttribute(line, kMediaTypeAudio)) {
2106 content.reset(ParseContentDescription<AudioContentDescription>(
2107 message, cricket::MEDIA_TYPE_AUDIO, mline_index, protocol,
2108 codec_preference, pos, &content_name,
2109 &transport, candidates, error));
2110 MaybeCreateStaticPayloadAudioCodecs(
2111 codec_preference,
2112 static_cast<AudioContentDescription*>(content.get()));
2113 } else if (HasAttribute(line, kMediaTypeData)) {
2114 content.reset(ParseContentDescription<DataContentDescription>(
2115 message, cricket::MEDIA_TYPE_DATA, mline_index, protocol,
2116 codec_preference, pos, &content_name,
2117 &transport, candidates, error));
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00002118 // We should always use the default bandwidth for RTP-based data
2119 // channels. Don't allow SDP to set the bandwidth, because that
2120 // would give JS the opportunity to "break the Internet".
sergeyu@chromium.orga59696b2013-09-13 23:48:58 +00002121 // TODO(pthatcher): But we need to temporarily allow the SDP to control
2122 // this for backwards-compatibility. Once we don't need that any
2123 // more, remove this.
2124 bool support_dc_sdp_bandwidth_temporarily = true;
wu@webrtc.org967bfff2013-09-19 05:49:50 +00002125 if (content.get() && !support_dc_sdp_bandwidth_temporarily) {
sergeyu@chromium.orga59696b2013-09-13 23:48:58 +00002126 content->set_bandwidth(cricket::kAutoBandwidth);
2127 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002128 } else {
2129 LOG(LS_WARNING) << "Unsupported media type: " << line;
2130 continue;
2131 }
2132 if (!content.get()) {
2133 // ParseContentDescription returns NULL if failed.
2134 return false;
2135 }
2136
2137 if (!is_sctp) {
2138 // Make sure to set the media direction correctly. If the direction is not
2139 // MD_RECVONLY or Inactive and no streams are parsed,
2140 // a default MediaStream will be created to prepare for receiving media.
2141 if (supports_msid && content->streams().empty() &&
2142 content->direction() == cricket::MD_SENDRECV) {
2143 content->set_direction(cricket::MD_RECVONLY);
2144 }
2145
2146 // Set the extmap.
2147 if (!session_extmaps.empty() &&
2148 !content->rtp_header_extensions().empty()) {
2149 return ParseFailed("",
2150 "The a=extmap MUST be either all session level or "
2151 "all media level.",
2152 error);
2153 }
2154 for (size_t i = 0; i < session_extmaps.size(); ++i) {
2155 content->AddRtpHeaderExtension(session_extmaps[i]);
2156 }
2157 }
2158 content->set_protocol(protocol);
2159 desc->AddContent(content_name,
2160 is_sctp ? cricket::NS_JINGLE_DRAFT_SCTP :
2161 cricket::NS_JINGLE_RTP,
2162 rejected,
2163 content.release());
2164 // Create TransportInfo with the media level "ice-pwd" and "ice-ufrag".
2165 TransportInfo transport_info(content_name, transport);
2166
2167 if (!desc->AddTransportInfo(transport_info)) {
2168 std::ostringstream description;
2169 description << "Failed to AddTransportInfo with content name: "
2170 << content_name;
2171 return ParseFailed("", description.str(), error);
2172 }
2173 }
2174 return true;
2175}
2176
2177bool VerifyCodec(const cricket::Codec& codec) {
2178 // Codec has not been populated correctly unless the name has been set. This
2179 // can happen if an SDP has an fmtp or rtcp-fb with a payload type but doesn't
2180 // have a corresponding "rtpmap" line.
2181 cricket::Codec default_codec;
2182 return default_codec.name != codec.name;
2183}
2184
2185bool VerifyAudioCodecs(const AudioContentDescription* audio_desc) {
2186 const std::vector<cricket::AudioCodec>& codecs = audio_desc->codecs();
2187 for (std::vector<cricket::AudioCodec>::const_iterator iter = codecs.begin();
2188 iter != codecs.end(); ++iter) {
2189 if (!VerifyCodec(*iter)) {
2190 return false;
2191 }
2192 }
2193 return true;
2194}
2195
2196bool VerifyVideoCodecs(const VideoContentDescription* video_desc) {
2197 const std::vector<cricket::VideoCodec>& codecs = video_desc->codecs();
2198 for (std::vector<cricket::VideoCodec>::const_iterator iter = codecs.begin();
2199 iter != codecs.end(); ++iter) {
2200 if (!VerifyCodec(*iter)) {
2201 return false;
2202 }
2203 }
2204 return true;
2205}
2206
2207void AddParameters(const cricket::CodecParameterMap& parameters,
2208 cricket::Codec* codec) {
2209 for (cricket::CodecParameterMap::const_iterator iter =
2210 parameters.begin(); iter != parameters.end(); ++iter) {
2211 codec->SetParam(iter->first, iter->second);
2212 }
2213}
2214
2215void AddFeedbackParameter(const cricket::FeedbackParam& feedback_param,
2216 cricket::Codec* codec) {
2217 codec->AddFeedbackParam(feedback_param);
2218}
2219
2220void AddFeedbackParameters(const cricket::FeedbackParams& feedback_params,
2221 cricket::Codec* codec) {
2222 for (std::vector<cricket::FeedbackParam>::const_iterator iter =
2223 feedback_params.params().begin();
2224 iter != feedback_params.params().end(); ++iter) {
2225 codec->AddFeedbackParam(*iter);
2226 }
2227}
2228
2229// Gets the current codec setting associated with |payload_type|. If there
2230// is no AudioCodec associated with that payload type it returns an empty codec
2231// with that payload type.
2232template <class T>
2233T GetCodec(const std::vector<T>& codecs, int payload_type) {
2234 for (typename std::vector<T>::const_iterator codec = codecs.begin();
2235 codec != codecs.end(); ++codec) {
2236 if (codec->id == payload_type) {
2237 return *codec;
2238 }
2239 }
2240 T ret_val = T();
2241 ret_val.id = payload_type;
2242 return ret_val;
2243}
2244
2245// Updates or creates a new codec entry in the audio description.
2246template <class T, class U>
2247void AddOrReplaceCodec(MediaContentDescription* content_desc, const U& codec) {
2248 T* desc = static_cast<T*>(content_desc);
2249 std::vector<U> codecs = desc->codecs();
2250 bool found = false;
2251
2252 typename std::vector<U>::iterator iter;
2253 for (iter = codecs.begin(); iter != codecs.end(); ++iter) {
2254 if (iter->id == codec.id) {
2255 *iter = codec;
2256 found = true;
2257 break;
2258 }
2259 }
2260 if (!found) {
2261 desc->AddCodec(codec);
2262 return;
2263 }
2264 desc->set_codecs(codecs);
2265}
2266
2267// Adds or updates existing codec corresponding to |payload_type| according
2268// to |parameters|.
2269template <class T, class U>
2270void UpdateCodec(MediaContentDescription* content_desc, int payload_type,
2271 const cricket::CodecParameterMap& parameters) {
2272 // Codec might already have been populated (from rtpmap).
2273 U new_codec = GetCodec(static_cast<T*>(content_desc)->codecs(), payload_type);
2274 AddParameters(parameters, &new_codec);
2275 AddOrReplaceCodec<T, U>(content_desc, new_codec);
2276}
2277
2278// Adds or updates existing codec corresponding to |payload_type| according
2279// to |feedback_param|.
2280template <class T, class U>
2281void UpdateCodec(MediaContentDescription* content_desc, int payload_type,
2282 const cricket::FeedbackParam& feedback_param) {
2283 // Codec might already have been populated (from rtpmap).
2284 U new_codec = GetCodec(static_cast<T*>(content_desc)->codecs(), payload_type);
2285 AddFeedbackParameter(feedback_param, &new_codec);
2286 AddOrReplaceCodec<T, U>(content_desc, new_codec);
2287}
2288
2289bool PopWildcardCodec(std::vector<cricket::VideoCodec>* codecs,
2290 cricket::VideoCodec* wildcard_codec) {
2291 for (std::vector<cricket::VideoCodec>::iterator iter = codecs->begin();
2292 iter != codecs->end(); ++iter) {
2293 if (iter->id == kWildcardPayloadType) {
2294 *wildcard_codec = *iter;
2295 codecs->erase(iter);
2296 return true;
2297 }
2298 }
2299 return false;
2300}
2301
2302void UpdateFromWildcardVideoCodecs(VideoContentDescription* video_desc) {
2303 std::vector<cricket::VideoCodec> codecs = video_desc->codecs();
2304 cricket::VideoCodec wildcard_codec;
2305 if (!PopWildcardCodec(&codecs, &wildcard_codec)) {
2306 return;
2307 }
2308 for (std::vector<cricket::VideoCodec>::iterator iter = codecs.begin();
2309 iter != codecs.end(); ++iter) {
2310 cricket::VideoCodec& codec = *iter;
2311 AddFeedbackParameters(wildcard_codec.feedback_params, &codec);
2312 }
2313 video_desc->set_codecs(codecs);
2314}
2315
2316void AddAudioAttribute(const std::string& name, const std::string& value,
2317 AudioContentDescription* audio_desc) {
2318 if (value.empty()) {
2319 return;
2320 }
2321 std::vector<cricket::AudioCodec> codecs = audio_desc->codecs();
2322 for (std::vector<cricket::AudioCodec>::iterator iter = codecs.begin();
2323 iter != codecs.end(); ++iter) {
2324 iter->params[name] = value;
2325 }
2326 audio_desc->set_codecs(codecs);
2327}
2328
2329bool ParseContent(const std::string& message,
2330 const MediaType media_type,
2331 int mline_index,
2332 const std::string& protocol,
2333 const std::vector<int>& codec_preference,
2334 size_t* pos,
2335 std::string* content_name,
2336 MediaContentDescription* media_desc,
2337 TransportDescription* transport,
2338 std::vector<JsepIceCandidate*>* candidates,
2339 SdpParseError* error) {
2340 ASSERT(media_desc != NULL);
2341 ASSERT(content_name != NULL);
2342 ASSERT(transport != NULL);
2343
2344 // The media level "ice-ufrag" and "ice-pwd".
2345 // The candidates before update the media level "ice-pwd" and "ice-ufrag".
2346 Candidates candidates_orig;
2347 std::string line;
2348 std::string mline_id;
2349 // Tracks created out of the ssrc attributes.
2350 StreamParamsVec tracks;
2351 SsrcInfoVec ssrc_infos;
2352 SsrcGroupVec ssrc_groups;
2353 std::string maxptime_as_string;
2354 std::string ptime_as_string;
2355
2356 bool is_rtp =
2357 protocol.empty() ||
2358 talk_base::starts_with(protocol.data(),
2359 cricket::kMediaProtocolRtpPrefix);
2360
2361 // Loop until the next m line
2362 while (!IsLineType(message, kLineTypeMedia, *pos)) {
2363 if (!GetLine(message, pos, &line)) {
2364 if (*pos >= message.size()) {
2365 break; // Done parsing
2366 } else {
2367 return ParseFailed(message, *pos, "Can't find valid SDP line.", error);
2368 }
2369 }
2370
2371 if (IsLineType(line, kLineTypeSessionBandwidth)) {
2372 std::string bandwidth;
2373 if (HasAttribute(line, kApplicationSpecificMaximum)) {
2374 if (!GetValue(line, kApplicationSpecificMaximum, &bandwidth, error)) {
2375 return false;
2376 } else {
2377 media_desc->set_bandwidth(
2378 talk_base::FromString<int>(bandwidth) * 1000);
2379 }
2380 }
2381 continue;
2382 }
2383
2384 // RFC 4566
2385 // b=* (zero or more bandwidth information lines)
2386 if (IsLineType(line, kLineTypeSessionBandwidth)) {
2387 std::string bandwidth;
2388 if (HasAttribute(line, kApplicationSpecificMaximum)) {
2389 if (!GetValue(line, kApplicationSpecificMaximum, &bandwidth, error)) {
2390 return false;
2391 } else {
2392 media_desc->set_bandwidth(
2393 talk_base::FromString<int>(bandwidth) * 1000);
2394 }
2395 }
2396 continue;
2397 }
2398
2399 if (!IsLineType(line, kLineTypeAttributes)) {
2400 // TODO: Handle other lines if needed.
2401 LOG(LS_INFO) << "Ignored line: " << line;
2402 continue;
2403 }
2404
2405 // Handle attributes common to SCTP and RTP.
2406 if (HasAttribute(line, kAttributeMid)) {
2407 // RFC 3388
2408 // mid-attribute = "a=mid:" identification-tag
2409 // identification-tag = token
2410 // Use the mid identification-tag as the content name.
2411 if (!GetValue(line, kAttributeMid, &mline_id, error)) {
2412 return false;
2413 }
2414 *content_name = mline_id;
2415 } else if (HasAttribute(line, kAttributeCandidate)) {
2416 Candidate candidate;
2417 if (!ParseCandidate(line, &candidate, error, false)) {
2418 return false;
2419 }
2420 candidates_orig.push_back(candidate);
2421 } else if (HasAttribute(line, kAttributeIceUfrag)) {
2422 if (!GetValue(line, kAttributeIceUfrag, &transport->ice_ufrag, error)) {
2423 return false;
2424 }
2425 } else if (HasAttribute(line, kAttributeIcePwd)) {
2426 if (!GetValue(line, kAttributeIcePwd, &transport->ice_pwd, error)) {
2427 return false;
2428 }
2429 } else if (HasAttribute(line, kAttributeIceOption)) {
2430 if (!ParseIceOptions(line, &transport->transport_options, error)) {
2431 return false;
2432 }
2433 } else if (HasAttribute(line, kAttributeFmtp)) {
2434 if (!ParseFmtpAttributes(line, media_type, media_desc, error)) {
2435 return false;
2436 }
2437 } else if (HasAttribute(line, kAttributeFingerprint)) {
2438 talk_base::SSLFingerprint* fingerprint = NULL;
2439
2440 if (!ParseFingerprintAttribute(line, &fingerprint, error)) {
2441 return false;
2442 }
2443 transport->identity_fingerprint.reset(fingerprint);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00002444 } else if (HasAttribute(line, kAttributeSetup)) {
2445 if (!ParseDtlsSetup(line, &(transport->connection_role), error)) {
2446 return false;
2447 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002448 } else if (is_rtp) {
2449 //
2450 // RTP specific attrubtes
2451 //
2452 if (HasAttribute(line, kAttributeRtcpMux)) {
2453 media_desc->set_rtcp_mux(true);
2454 } else if (HasAttribute(line, kAttributeSsrcGroup)) {
2455 if (!ParseSsrcGroupAttribute(line, &ssrc_groups, error)) {
2456 return false;
2457 }
2458 } else if (HasAttribute(line, kAttributeSsrc)) {
2459 if (!ParseSsrcAttribute(line, &ssrc_infos, error)) {
2460 return false;
2461 }
2462 } else if (HasAttribute(line, kAttributeCrypto)) {
2463 if (!ParseCryptoAttribute(line, media_desc, error)) {
2464 return false;
2465 }
2466 } else if (HasAttribute(line, kAttributeRtpmap)) {
2467 if (!ParseRtpmapAttribute(line, media_type, codec_preference,
2468 media_desc, error)) {
2469 return false;
2470 }
2471 } else if (HasAttribute(line, kCodecParamMaxPTime)) {
2472 if (!GetValue(line, kCodecParamMaxPTime, &maxptime_as_string, error)) {
2473 return false;
2474 }
2475 } else if (HasAttribute(line, kAttributeRtcpFb)) {
2476 if (!ParseRtcpFbAttribute(line, media_type, media_desc, error)) {
2477 return false;
2478 }
2479 } else if (HasAttribute(line, kCodecParamPTime)) {
2480 if (!GetValue(line, kCodecParamPTime, &ptime_as_string, error)) {
2481 return false;
2482 }
2483 } else if (HasAttribute(line, kAttributeSendOnly)) {
2484 media_desc->set_direction(cricket::MD_SENDONLY);
2485 } else if (HasAttribute(line, kAttributeRecvOnly)) {
2486 media_desc->set_direction(cricket::MD_RECVONLY);
2487 } else if (HasAttribute(line, kAttributeInactive)) {
2488 media_desc->set_direction(cricket::MD_INACTIVE);
2489 } else if (HasAttribute(line, kAttributeSendRecv)) {
2490 media_desc->set_direction(cricket::MD_SENDRECV);
2491 } else if (HasAttribute(line, kAttributeExtmap)) {
2492 RtpHeaderExtension extmap;
2493 if (!ParseExtmap(line, &extmap, error)) {
2494 return false;
2495 }
2496 media_desc->AddRtpHeaderExtension(extmap);
2497 } else if (HasAttribute(line, kAttributeXGoogleFlag)) {
2498 // Experimental attribute. Conference mode activates more aggressive
2499 // AEC and NS settings.
2500 // TODO: expose API to set these directly.
2501 std::string flag_value;
2502 if (!GetValue(line, kAttributeXGoogleFlag, &flag_value, error)) {
2503 return false;
2504 }
2505 if (flag_value.compare(kValueConference) == 0)
2506 media_desc->set_conference_mode(true);
2507 } else if (HasAttribute(line, kAttributeXGoogleBufferLatency)) {
2508 // Experimental attribute.
2509 // TODO: expose API to set this directly.
2510 std::string flag_value;
2511 if (!GetValue(line, kAttributeXGoogleBufferLatency, &flag_value,
2512 error)) {
2513 return false;
2514 }
2515 int buffer_latency = 0;
2516 if (!talk_base::FromString(flag_value, &buffer_latency) ||
2517 buffer_latency < 0) {
2518 return ParseFailed(message, "Invalid buffer latency.", error);
2519 }
2520 media_desc->set_buffered_mode_latency(buffer_latency);
2521 }
2522 } else {
2523 // Only parse lines that we are interested of.
2524 LOG(LS_INFO) << "Ignored line: " << line;
2525 continue;
2526 }
2527 }
2528
2529 // Create tracks from the |ssrc_infos|.
2530 CreateTracksFromSsrcInfos(ssrc_infos, &tracks);
2531
2532 // Add the ssrc group to the track.
2533 for (SsrcGroupVec::iterator ssrc_group = ssrc_groups.begin();
2534 ssrc_group != ssrc_groups.end(); ++ssrc_group) {
2535 if (ssrc_group->ssrcs.empty()) {
2536 continue;
2537 }
2538 uint32 ssrc = ssrc_group->ssrcs.front();
2539 for (StreamParamsVec::iterator track = tracks.begin();
2540 track != tracks.end(); ++track) {
2541 if (track->has_ssrc(ssrc)) {
2542 track->ssrc_groups.push_back(*ssrc_group);
2543 }
2544 }
2545 }
2546
2547 // Add the new tracks to the |media_desc|.
2548 for (StreamParamsVec::iterator track = tracks.begin();
2549 track != tracks.end(); ++track) {
2550 media_desc->AddStream(*track);
2551 }
2552
2553 if (media_type == cricket::MEDIA_TYPE_AUDIO) {
2554 AudioContentDescription* audio_desc =
2555 static_cast<AudioContentDescription*>(media_desc);
2556 // Verify audio codec ensures that no audio codec has been populated with
2557 // only fmtp.
2558 if (!VerifyAudioCodecs(audio_desc)) {
2559 return ParseFailed("Failed to parse audio codecs correctly.", error);
2560 }
2561 AddAudioAttribute(kCodecParamMaxPTime, maxptime_as_string, audio_desc);
2562 AddAudioAttribute(kCodecParamPTime, ptime_as_string, audio_desc);
2563 }
2564
2565 if (media_type == cricket::MEDIA_TYPE_VIDEO) {
2566 VideoContentDescription* video_desc =
2567 static_cast<VideoContentDescription*>(media_desc);
2568 UpdateFromWildcardVideoCodecs(video_desc);
2569 // Verify video codec ensures that no video codec has been populated with
2570 // only rtcp-fb.
2571 if (!VerifyVideoCodecs(video_desc)) {
2572 return ParseFailed("Failed to parse video codecs correctly.", error);
2573 }
2574 }
2575
2576 // RFC 5245
2577 // Update the candidates with the media level "ice-pwd" and "ice-ufrag".
2578 for (Candidates::iterator it = candidates_orig.begin();
2579 it != candidates_orig.end(); ++it) {
2580 ASSERT((*it).username().empty());
2581 (*it).set_username(transport->ice_ufrag);
2582 ASSERT((*it).password().empty());
2583 (*it).set_password(transport->ice_pwd);
2584 candidates->push_back(
2585 new JsepIceCandidate(mline_id, mline_index, *it));
2586 }
2587 return true;
2588}
2589
2590bool ParseSsrcAttribute(const std::string& line, SsrcInfoVec* ssrc_infos,
2591 SdpParseError* error) {
2592 ASSERT(ssrc_infos != NULL);
2593 // RFC 5576
2594 // a=ssrc:<ssrc-id> <attribute>
2595 // a=ssrc:<ssrc-id> <attribute>:<value>
2596 std::string field1, field2;
2597 if (!SplitByDelimiter(line.substr(kLinePrefixLength),
2598 kSdpDelimiterSpace,
2599 &field1,
2600 &field2)) {
2601 const size_t expected_fields = 2;
2602 return ParseFailedExpectFieldNum(line, expected_fields, error);
2603 }
2604
2605 // ssrc:<ssrc-id>
2606 std::string ssrc_id_s;
2607 if (!GetValue(field1, kAttributeSsrc, &ssrc_id_s, error)) {
2608 return false;
2609 }
2610 uint32 ssrc_id = talk_base::FromString<uint32>(ssrc_id_s);
2611
2612 std::string attribute;
2613 std::string value;
2614 if (!SplitByDelimiter(field2, kSdpDelimiterColon,
2615 &attribute, &value)) {
2616 std::ostringstream description;
2617 description << "Failed to get the ssrc attribute value from " << field2
2618 << ". Expected format <attribute>:<value>.";
2619 return ParseFailed(line, description.str(), error);
2620 }
2621
2622 // Check if there's already an item for this |ssrc_id|. Create a new one if
2623 // there isn't.
2624 SsrcInfoVec::iterator ssrc_info = ssrc_infos->begin();
2625 for (; ssrc_info != ssrc_infos->end(); ++ssrc_info) {
2626 if (ssrc_info->ssrc_id == ssrc_id) {
2627 break;
2628 }
2629 }
2630 if (ssrc_info == ssrc_infos->end()) {
2631 SsrcInfo info;
2632 info.ssrc_id = ssrc_id;
2633 ssrc_infos->push_back(info);
2634 ssrc_info = ssrc_infos->end() - 1;
2635 }
2636
2637 // Store the info to the |ssrc_info|.
2638 if (attribute == kSsrcAttributeCname) {
2639 // RFC 5576
2640 // cname:<value>
2641 ssrc_info->cname = value;
2642 } else if (attribute == kSsrcAttributeMsid) {
2643 // draft-alvestrand-mmusic-msid-00
2644 // "msid:" identifier [ " " appdata ]
2645 std::vector<std::string> fields;
2646 talk_base::split(value, kSdpDelimiterSpace, &fields);
2647 if (fields.size() < 1 || fields.size() > 2) {
2648 return ParseFailed(line,
2649 "Expected format \"msid:<identifier>[ <appdata>]\".",
2650 error);
2651 }
2652 ssrc_info->msid_identifier = fields[0];
2653 if (fields.size() == 2) {
2654 ssrc_info->msid_appdata = fields[1];
2655 }
2656 } else if (attribute == kSsrcAttributeMslabel) {
2657 // draft-alvestrand-rtcweb-mid-01
2658 // mslabel:<value>
2659 ssrc_info->mslabel = value;
2660 } else if (attribute == kSSrcAttributeLabel) {
2661 // The label isn't defined.
2662 // label:<value>
2663 ssrc_info->label = value;
2664 }
2665 return true;
2666}
2667
2668bool ParseSsrcGroupAttribute(const std::string& line,
2669 SsrcGroupVec* ssrc_groups,
2670 SdpParseError* error) {
2671 ASSERT(ssrc_groups != NULL);
2672 // RFC 5576
2673 // a=ssrc-group:<semantics> <ssrc-id> ...
2674 std::vector<std::string> fields;
2675 talk_base::split(line.substr(kLinePrefixLength),
2676 kSdpDelimiterSpace, &fields);
2677 const size_t expected_min_fields = 2;
2678 if (fields.size() < expected_min_fields) {
2679 return ParseFailedExpectMinFieldNum(line, expected_min_fields, error);
2680 }
2681 std::string semantics;
2682 if (!GetValue(fields[0], kAttributeSsrcGroup, &semantics, error)) {
2683 return false;
2684 }
2685 std::vector<uint32> ssrcs;
2686 for (size_t i = 1; i < fields.size(); ++i) {
2687 uint32 ssrc = talk_base::FromString<uint32>(fields[i]);
2688 ssrcs.push_back(ssrc);
2689 }
2690 ssrc_groups->push_back(SsrcGroup(semantics, ssrcs));
2691 return true;
2692}
2693
2694bool ParseCryptoAttribute(const std::string& line,
2695 MediaContentDescription* media_desc,
2696 SdpParseError* error) {
2697 std::vector<std::string> fields;
2698 talk_base::split(line.substr(kLinePrefixLength),
2699 kSdpDelimiterSpace, &fields);
2700 // RFC 4568
2701 // a=crypto:<tag> <crypto-suite> <key-params> [<session-params>]
2702 const size_t expected_min_fields = 3;
2703 if (fields.size() < expected_min_fields) {
2704 return ParseFailedExpectMinFieldNum(line, expected_min_fields, error);
2705 }
2706 std::string tag_value;
2707 if (!GetValue(fields[0], kAttributeCrypto, &tag_value, error)) {
2708 return false;
2709 }
2710 int tag = talk_base::FromString<int>(tag_value);
2711 const std::string crypto_suite = fields[1];
2712 const std::string key_params = fields[2];
2713 std::string session_params;
2714 if (fields.size() > 3) {
2715 session_params = fields[3];
2716 }
2717 media_desc->AddCrypto(CryptoParams(tag, crypto_suite, key_params,
2718 session_params));
2719 return true;
2720}
2721
2722// Updates or creates a new codec entry in the audio description with according
2723// to |name|, |clockrate|, |bitrate|, |channels| and |preference|.
2724void UpdateCodec(int payload_type, const std::string& name, int clockrate,
2725 int bitrate, int channels, int preference,
2726 AudioContentDescription* audio_desc) {
2727 // Codec may already be populated with (only) optional parameters
2728 // (from an fmtp).
2729 cricket::AudioCodec codec = GetCodec(audio_desc->codecs(), payload_type);
2730 codec.name = name;
2731 codec.clockrate = clockrate;
2732 codec.bitrate = bitrate;
2733 codec.channels = channels;
2734 codec.preference = preference;
2735 AddOrReplaceCodec<AudioContentDescription, cricket::AudioCodec>(audio_desc,
2736 codec);
2737}
2738
2739// Updates or creates a new codec entry in the video description according to
2740// |name|, |width|, |height|, |framerate| and |preference|.
2741void UpdateCodec(int payload_type, const std::string& name, int width,
2742 int height, int framerate, int preference,
2743 VideoContentDescription* video_desc) {
2744 // Codec may already be populated with (only) optional parameters
2745 // (from an fmtp).
2746 cricket::VideoCodec codec = GetCodec(video_desc->codecs(), payload_type);
2747 codec.name = name;
2748 codec.width = width;
2749 codec.height = height;
2750 codec.framerate = framerate;
2751 codec.preference = preference;
2752 AddOrReplaceCodec<VideoContentDescription, cricket::VideoCodec>(video_desc,
2753 codec);
2754}
2755
2756bool ParseRtpmapAttribute(const std::string& line,
2757 const MediaType media_type,
2758 const std::vector<int>& codec_preference,
2759 MediaContentDescription* media_desc,
2760 SdpParseError* error) {
2761 std::vector<std::string> fields;
2762 talk_base::split(line.substr(kLinePrefixLength),
2763 kSdpDelimiterSpace, &fields);
2764 // RFC 4566
2765 // a=rtpmap:<payload type> <encoding name>/<clock rate>[/<encodingparameters>]
2766 const size_t expected_min_fields = 2;
2767 if (fields.size() < expected_min_fields) {
2768 return ParseFailedExpectMinFieldNum(line, expected_min_fields, error);
2769 }
2770 std::string payload_type_value;
2771 if (!GetValue(fields[0], kAttributeRtpmap, &payload_type_value, error)) {
2772 return false;
2773 }
2774 const int payload_type = talk_base::FromString<int>(payload_type_value);
2775
2776 // Set the preference order depending on the order of the pl type in the
2777 // <fmt> of the m-line.
2778 const int preference = codec_preference.end() -
2779 std::find(codec_preference.begin(), codec_preference.end(),
2780 payload_type);
2781 if (preference == 0) {
2782 LOG(LS_WARNING) << "Ignore rtpmap line that did not appear in the "
2783 << "<fmt> of the m-line: " << line;
2784 return true;
2785 }
2786 const std::string encoder = fields[1];
2787 std::vector<std::string> codec_params;
2788 talk_base::split(encoder, '/', &codec_params);
2789 // <encoding name>/<clock rate>[/<encodingparameters>]
2790 // 2 mandatory fields
2791 if (codec_params.size() < 2 || codec_params.size() > 3) {
2792 return ParseFailed(line,
2793 "Expected format \"<encoding name>/<clock rate>"
2794 "[/<encodingparameters>]\".",
2795 error);
2796 }
2797 const std::string encoding_name = codec_params[0];
2798 const int clock_rate = talk_base::FromString<int>(codec_params[1]);
2799 if (media_type == cricket::MEDIA_TYPE_VIDEO) {
2800 VideoContentDescription* video_desc =
2801 static_cast<VideoContentDescription*>(media_desc);
2802 // TODO: We will send resolution in SDP. For now use
2803 // JsepSessionDescription::kMaxVideoCodecWidth and kMaxVideoCodecHeight.
2804 UpdateCodec(payload_type, encoding_name,
2805 JsepSessionDescription::kMaxVideoCodecWidth,
2806 JsepSessionDescription::kMaxVideoCodecHeight,
2807 JsepSessionDescription::kDefaultVideoCodecFramerate,
2808 preference, video_desc);
2809 } else if (media_type == cricket::MEDIA_TYPE_AUDIO) {
2810 // RFC 4566
2811 // For audio streams, <encoding parameters> indicates the number
2812 // of audio channels. This parameter is OPTIONAL and may be
2813 // omitted if the number of channels is one, provided that no
2814 // additional parameters are needed.
2815 int channels = 1;
2816 if (codec_params.size() == 3) {
2817 channels = talk_base::FromString<int>(codec_params[2]);
2818 }
2819 int bitrate = 0;
2820 // The default behavior for ISAC (bitrate == 0) in webrtcvoiceengine.cc
2821 // (specifically FindWebRtcCodec) is bandwidth-adaptive variable bitrate.
2822 // The bandwidth adaptation doesn't always work well, so this code
2823 // sets a fixed target bitrate instead.
2824 if (_stricmp(encoding_name.c_str(), kIsacCodecName) == 0) {
2825 if (clock_rate <= 16000) {
2826 bitrate = kIsacWbDefaultRate;
2827 } else {
2828 bitrate = kIsacSwbDefaultRate;
2829 }
2830 }
2831 AudioContentDescription* audio_desc =
2832 static_cast<AudioContentDescription*>(media_desc);
2833 UpdateCodec(payload_type, encoding_name, clock_rate, bitrate, channels,
2834 preference, audio_desc);
2835 } else if (media_type == cricket::MEDIA_TYPE_DATA) {
2836 DataContentDescription* data_desc =
2837 static_cast<DataContentDescription*>(media_desc);
2838 data_desc->AddCodec(cricket::DataCodec(payload_type, encoding_name,
2839 preference));
2840 }
2841 return true;
2842}
2843
2844void PruneRight(const char delimiter, std::string* message) {
2845 size_t trailing = message->find(delimiter);
2846 if (trailing != std::string::npos) {
2847 *message = message->substr(0, trailing);
2848 }
2849}
2850
2851bool ParseFmtpParam(const std::string& line, std::string* parameter,
2852 std::string* value, SdpParseError* error) {
2853 if (!SplitByDelimiter(line, kSdpDelimiterEqual, parameter, value)) {
2854 ParseFailed(line, "Unable to parse fmtp parameter. \'=\' missing.", error);
2855 return false;
2856 }
2857 // a=fmtp:<payload_type> <param1>=<value1>; <param2>=<value2>; ...
2858 // When parsing the values the trailing ";" gets picked up. Remove them.
2859 PruneRight(kSdpDelimiterSemicolon, value);
2860 return true;
2861}
2862
2863bool ParseFmtpAttributes(const std::string& line, const MediaType media_type,
2864 MediaContentDescription* media_desc,
2865 SdpParseError* error) {
2866 if (media_type != cricket::MEDIA_TYPE_AUDIO &&
2867 media_type != cricket::MEDIA_TYPE_VIDEO) {
2868 return true;
2869 }
2870 std::vector<std::string> fields;
2871 talk_base::split(line.substr(kLinePrefixLength),
2872 kSdpDelimiterSpace, &fields);
2873
2874 // RFC 5576
2875 // a=fmtp:<format> <format specific parameters>
2876 // At least two fields, whereas the second one is any of the optional
2877 // parameters.
2878 if (fields.size() < 2) {
2879 ParseFailedExpectMinFieldNum(line, 2, error);
2880 return false;
2881 }
2882
2883 std::string payload_type;
2884 if (!GetValue(fields[0], kAttributeFmtp, &payload_type, error)) {
2885 return false;
2886 }
2887
2888 cricket::CodecParameterMap codec_params;
2889 for (std::vector<std::string>::const_iterator iter = fields.begin() + 1;
2890 iter != fields.end(); ++iter) {
2891 std::string name;
2892 std::string value;
2893 if (iter->find(kSdpDelimiterEqual) == std::string::npos) {
2894 // Only fmtps with equals are currently supported. Other fmtp types
2895 // should be ignored. Unknown fmtps do not constitute an error.
2896 continue;
2897 }
2898 if (!ParseFmtpParam(*iter, &name, &value, error)) {
2899 return false;
2900 }
2901 codec_params[name] = value;
2902 }
2903
2904 int int_payload_type = talk_base::FromString<int>(payload_type);
2905 if (media_type == cricket::MEDIA_TYPE_AUDIO) {
2906 UpdateCodec<AudioContentDescription, cricket::AudioCodec>(
2907 media_desc, int_payload_type, codec_params);
2908 } else if (media_type == cricket::MEDIA_TYPE_VIDEO) {
2909 UpdateCodec<VideoContentDescription, cricket::VideoCodec>(
2910 media_desc, int_payload_type, codec_params);
2911 }
2912 return true;
2913}
2914
2915bool ParseRtcpFbAttribute(const std::string& line, const MediaType media_type,
2916 MediaContentDescription* media_desc,
2917 SdpParseError* error) {
2918 if (media_type != cricket::MEDIA_TYPE_AUDIO &&
2919 media_type != cricket::MEDIA_TYPE_VIDEO) {
2920 return true;
2921 }
2922 std::vector<std::string> rtcp_fb_fields;
2923 talk_base::split(line.c_str(), kSdpDelimiterSpace, &rtcp_fb_fields);
2924 if (rtcp_fb_fields.size() < 2) {
2925 return ParseFailedGetValue(line, kAttributeRtcpFb, error);
2926 }
2927 std::string payload_type_string;
2928 if (!GetValue(rtcp_fb_fields[0], kAttributeRtcpFb, &payload_type_string,
2929 error)) {
2930 return false;
2931 }
2932 int payload_type = (payload_type_string == "*") ?
2933 kWildcardPayloadType : talk_base::FromString<int>(payload_type_string);
2934 std::string id = rtcp_fb_fields[1];
2935 std::string param = "";
2936 for (std::vector<std::string>::iterator iter = rtcp_fb_fields.begin() + 2;
2937 iter != rtcp_fb_fields.end(); ++iter) {
2938 param.append(*iter);
2939 }
2940 const cricket::FeedbackParam feedback_param(id, param);
2941
2942 if (media_type == cricket::MEDIA_TYPE_AUDIO) {
2943 UpdateCodec<AudioContentDescription, cricket::AudioCodec>(media_desc,
2944 payload_type,
2945 feedback_param);
2946 } else if (media_type == cricket::MEDIA_TYPE_VIDEO) {
2947 UpdateCodec<VideoContentDescription, cricket::VideoCodec>(media_desc,
2948 payload_type,
2949 feedback_param);
2950 }
2951 return true;
2952}
2953
2954} // namespace webrtc