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