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