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