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