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