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