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