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