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