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