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