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