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