blob: 9aa8e3556aa4f0f073153d3f2bf1cd03139fb4a2 [file] [log] [blame]
Steve Anton4ab68ee2017-12-19 14:26:11 -08001/*
2 * Copyright 2004 The WebRTC Project Authors. All rights reserved.
3 *
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10
Steve Anton10542f22019-01-11 09:11:00 -080011#ifndef PC_SESSION_DESCRIPTION_H_
12#define PC_SESSION_DESCRIPTION_H_
Steve Anton4ab68ee2017-12-19 14:26:11 -080013
Yves Gerey3e707812018-11-28 16:47:49 +010014#include <stddef.h>
15#include <stdint.h>
Jonas Olssona4d87372019-07-05 19:08:33 +020016
Harald Alvestrand5761e7b2021-01-29 14:45:08 +000017#include <algorithm>
Harald Alvestrand4d7160e2019-04-12 07:01:29 +020018#include <memory>
Steve Anton4ab68ee2017-12-19 14:26:11 -080019#include <string>
Harald Alvestrandc24a2182022-02-23 13:44:59 +000020#include <type_traits>
Harald Alvestrand1716d392019-06-03 20:35:45 +020021#include <utility>
Steve Anton4ab68ee2017-12-19 14:26:11 -080022#include <vector>
23
Harald Alvestrand5fc28b12019-05-13 13:36:16 +020024#include "absl/memory/memory.h"
Niels Möllerf1d822b2022-06-07 13:58:27 +020025#include "absl/strings/string_view.h"
Harald Alvestrand0d018412021-11-04 13:52:31 +000026#include "api/crypto_params.h"
Steve Anton10542f22019-01-11 09:11:00 -080027#include "api/media_types.h"
28#include "api/rtp_parameters.h"
Harald Alvestrand5761e7b2021-01-29 14:45:08 +000029#include "api/rtp_transceiver_direction.h"
Steve Anton10542f22019-01-11 09:11:00 -080030#include "api/rtp_transceiver_interface.h"
Harald Alvestrand5761e7b2021-01-29 14:45:08 +000031#include "media/base/codec.h"
Steve Anton10542f22019-01-11 09:11:00 -080032#include "media/base/media_channel.h"
Taylor Brandstetteree8c2462020-07-27 15:52:02 -070033#include "media/base/media_constants.h"
Harald Alvestrand5761e7b2021-01-29 14:45:08 +000034#include "media/base/rid_description.h"
Steve Anton10542f22019-01-11 09:11:00 -080035#include "media/base/stream_params.h"
36#include "p2p/base/transport_description.h"
37#include "p2p/base/transport_info.h"
Harald Alvestrand5fc28b12019-05-13 13:36:16 +020038#include "pc/media_protocol_names.h"
Steve Anton10542f22019-01-11 09:11:00 -080039#include "pc/simulcast_description.h"
Harald Alvestrand5761e7b2021-01-29 14:45:08 +000040#include "rtc_base/checks.h"
Steve Anton10542f22019-01-11 09:11:00 -080041#include "rtc_base/socket_address.h"
Mirko Bonadei35214fc2019-09-23 14:54:28 +020042#include "rtc_base/system/rtc_export.h"
Steve Anton4ab68ee2017-12-19 14:26:11 -080043
44namespace cricket {
45
Steve Antonafd8e8c2017-12-19 16:35:35 -080046typedef std::vector<AudioCodec> AudioCodecs;
47typedef std::vector<VideoCodec> VideoCodecs;
Harald Alvestrand0d018412021-11-04 13:52:31 +000048typedef std::vector<CryptoParams> CryptoParamsVec;
Steve Antonafd8e8c2017-12-19 16:35:35 -080049typedef std::vector<webrtc::RtpExtension> RtpHeaderExtensions;
50
Steve Antonafd8e8c2017-12-19 16:35:35 -080051// Options to control how session descriptions are generated.
52const int kAutoBandwidth = -1;
53
Steve Anton5adfafd2017-12-20 16:34:00 -080054class AudioContentDescription;
Steve Anton46afbf92019-05-10 11:15:18 -070055class VideoContentDescription;
Harald Alvestrand5fc28b12019-05-13 13:36:16 +020056class SctpDataContentDescription;
Philipp Hancke4e8c1152020-10-13 12:43:15 +020057class UnsupportedContentDescription;
Steve Anton4ab68ee2017-12-19 14:26:11 -080058
Steve Anton5adfafd2017-12-20 16:34:00 -080059// Describes a session description media section. There are subclasses for each
60// media type (audio, video, data) that will have additional information.
61class MediaContentDescription {
Steve Antonafd8e8c2017-12-19 16:35:35 -080062 public:
Steve Anton5adfafd2017-12-20 16:34:00 -080063 MediaContentDescription() = default;
64 virtual ~MediaContentDescription() = default;
Steve Antonafd8e8c2017-12-19 16:35:35 -080065
66 virtual MediaType type() const = 0;
Steve Anton5adfafd2017-12-20 16:34:00 -080067
68 // Try to cast this media description to an AudioContentDescription. Returns
69 // nullptr if the cast fails.
70 virtual AudioContentDescription* as_audio() { return nullptr; }
71 virtual const AudioContentDescription* as_audio() const { return nullptr; }
72
73 // Try to cast this media description to a VideoContentDescription. Returns
74 // nullptr if the cast fails.
75 virtual VideoContentDescription* as_video() { return nullptr; }
76 virtual const VideoContentDescription* as_video() const { return nullptr; }
77
Harald Alvestrand5fc28b12019-05-13 13:36:16 +020078 virtual SctpDataContentDescription* as_sctp() { return nullptr; }
79 virtual const SctpDataContentDescription* as_sctp() const { return nullptr; }
80
Philipp Hancke4e8c1152020-10-13 12:43:15 +020081 virtual UnsupportedContentDescription* as_unsupported() { return nullptr; }
82 virtual const UnsupportedContentDescription* as_unsupported() const {
83 return nullptr;
84 }
85
Steve Antonafd8e8c2017-12-19 16:35:35 -080086 virtual bool has_codecs() const = 0;
87
Harald Alvestrand0fb07f82020-02-27 20:21:37 +010088 // Copy operator that returns an unique_ptr.
89 // Not a virtual function.
90 // If a type-specific variant of Clone() is desired, override it, or
91 // simply use std::make_unique<typename>(*this) instead of Clone().
92 std::unique_ptr<MediaContentDescription> Clone() const {
93 return absl::WrapUnique(CloneInternal());
Harald Alvestrand1716d392019-06-03 20:35:45 +020094 }
Steve Anton5adfafd2017-12-20 16:34:00 -080095
Artem Titov880fa812021-07-30 22:30:23 +020096 // `protocol` is the expected media transport protocol, such as RTP/AVPF,
Steve Antonafd8e8c2017-12-19 16:35:35 -080097 // RTP/SAVPF or SCTP/DTLS.
Harald Alvestrand5fc28b12019-05-13 13:36:16 +020098 virtual std::string protocol() const { return protocol_; }
Niels Möllerf1d822b2022-06-07 13:58:27 +020099 virtual void set_protocol(absl::string_view protocol) {
100 protocol_ = std::string(protocol);
Harald Alvestrand5fc28b12019-05-13 13:36:16 +0200101 }
Steve Antonafd8e8c2017-12-19 16:35:35 -0800102
Harald Alvestrand5fc28b12019-05-13 13:36:16 +0200103 virtual webrtc::RtpTransceiverDirection direction() const {
104 return direction_;
105 }
106 virtual void set_direction(webrtc::RtpTransceiverDirection direction) {
Steve Antonafd8e8c2017-12-19 16:35:35 -0800107 direction_ = direction;
108 }
109
Harald Alvestrand5fc28b12019-05-13 13:36:16 +0200110 virtual bool rtcp_mux() const { return rtcp_mux_; }
111 virtual void set_rtcp_mux(bool mux) { rtcp_mux_ = mux; }
Steve Antonafd8e8c2017-12-19 16:35:35 -0800112
Harald Alvestrand5fc28b12019-05-13 13:36:16 +0200113 virtual bool rtcp_reduced_size() const { return rtcp_reduced_size_; }
114 virtual void set_rtcp_reduced_size(bool reduced_size) {
Steve Antonafd8e8c2017-12-19 16:35:35 -0800115 rtcp_reduced_size_ = reduced_size;
116 }
117
Sebastian Jansson97321b62019-07-24 14:01:18 +0200118 // Indicates support for the remote network estimate packet type. This
119 // functionality is experimental and subject to change without notice.
Sebastian Janssone1795f42019-07-24 11:38:03 +0200120 virtual bool remote_estimate() const { return remote_estimate_; }
121 virtual void set_remote_estimate(bool remote_estimate) {
122 remote_estimate_ = remote_estimate;
123 }
124
Harald Alvestrand5fc28b12019-05-13 13:36:16 +0200125 virtual int bandwidth() const { return bandwidth_; }
126 virtual void set_bandwidth(int bandwidth) { bandwidth_ = bandwidth; }
Taylor Brandstetteree8c2462020-07-27 15:52:02 -0700127 virtual std::string bandwidth_type() const { return bandwidth_type_; }
128 virtual void set_bandwidth_type(std::string bandwidth_type) {
129 bandwidth_type_ = bandwidth_type;
130 }
Steve Antonafd8e8c2017-12-19 16:35:35 -0800131
Harald Alvestrand0d018412021-11-04 13:52:31 +0000132 virtual const std::vector<CryptoParams>& cryptos() const { return cryptos_; }
133 virtual void AddCrypto(const CryptoParams& params) {
134 cryptos_.push_back(params);
135 }
136 virtual void set_cryptos(const std::vector<CryptoParams>& cryptos) {
137 cryptos_ = cryptos;
138 }
139
Lennart Grahl0d0ed762021-05-17 16:06:37 +0200140 // List of RTP header extensions. URIs are **NOT** guaranteed to be unique
141 // as they can appear twice when both encrypted and non-encrypted extensions
142 // are present.
143 // Use RtpExtension::FindHeaderExtensionByUri for finding and
144 // RtpExtension::DeduplicateHeaderExtensions for filtering.
Harald Alvestrand5fc28b12019-05-13 13:36:16 +0200145 virtual const RtpHeaderExtensions& rtp_header_extensions() const {
Steve Antonafd8e8c2017-12-19 16:35:35 -0800146 return rtp_header_extensions_;
147 }
Harald Alvestrand5fc28b12019-05-13 13:36:16 +0200148 virtual void set_rtp_header_extensions(
149 const RtpHeaderExtensions& extensions) {
Steve Antonafd8e8c2017-12-19 16:35:35 -0800150 rtp_header_extensions_ = extensions;
151 rtp_header_extensions_set_ = true;
152 }
Harald Alvestrand5fc28b12019-05-13 13:36:16 +0200153 virtual void AddRtpHeaderExtension(const webrtc::RtpExtension& ext) {
Steve Antonafd8e8c2017-12-19 16:35:35 -0800154 rtp_header_extensions_.push_back(ext);
155 rtp_header_extensions_set_ = true;
156 }
Harald Alvestrand5fc28b12019-05-13 13:36:16 +0200157 virtual void ClearRtpHeaderExtensions() {
Steve Antonafd8e8c2017-12-19 16:35:35 -0800158 rtp_header_extensions_.clear();
159 rtp_header_extensions_set_ = true;
160 }
161 // We can't always tell if an empty list of header extensions is
162 // because the other side doesn't support them, or just isn't hooked up to
163 // signal them. For now we assume an empty list means no signaling, but
164 // provide the ClearRtpHeaderExtensions method to allow "no support" to be
165 // clearly indicated (i.e. when derived from other information).
Harald Alvestrand5fc28b12019-05-13 13:36:16 +0200166 virtual bool rtp_header_extensions_set() const {
167 return rtp_header_extensions_set_;
168 }
169 virtual const StreamParamsVec& streams() const { return send_streams_; }
Steve Antonafd8e8c2017-12-19 16:35:35 -0800170 // TODO(pthatcher): Remove this by giving mediamessage.cc access
171 // to MediaContentDescription
Harald Alvestrand5fc28b12019-05-13 13:36:16 +0200172 virtual StreamParamsVec& mutable_streams() { return send_streams_; }
173 virtual void AddStream(const StreamParams& stream) {
Amit Hilbuchc57d5732018-12-11 15:30:11 -0800174 send_streams_.push_back(stream);
175 }
Steve Antonafd8e8c2017-12-19 16:35:35 -0800176 // Legacy streams have an ssrc, but nothing else.
177 void AddLegacyStream(uint32_t ssrc) {
Harald Alvestrand5fc28b12019-05-13 13:36:16 +0200178 AddStream(StreamParams::CreateLegacy(ssrc));
Steve Antonafd8e8c2017-12-19 16:35:35 -0800179 }
180 void AddLegacyStream(uint32_t ssrc, uint32_t fid_ssrc) {
181 StreamParams sp = StreamParams::CreateLegacy(ssrc);
182 sp.AddFidSsrc(ssrc, fid_ssrc);
Harald Alvestrand5fc28b12019-05-13 13:36:16 +0200183 AddStream(sp);
Steve Antonafd8e8c2017-12-19 16:35:35 -0800184 }
Amit Hilbuchc57d5732018-12-11 15:30:11 -0800185
Steve Antonafd8e8c2017-12-19 16:35:35 -0800186 // Sets the CNAME of all StreamParams if it have not been set.
Harald Alvestrand5fc28b12019-05-13 13:36:16 +0200187 virtual void SetCnameIfEmpty(const std::string& cname) {
Amit Hilbuchc57d5732018-12-11 15:30:11 -0800188 for (cricket::StreamParamsVec::iterator it = send_streams_.begin();
189 it != send_streams_.end(); ++it) {
Steve Antonafd8e8c2017-12-19 16:35:35 -0800190 if (it->cname.empty())
191 it->cname = cname;
192 }
193 }
Harald Alvestrand5fc28b12019-05-13 13:36:16 +0200194 virtual uint32_t first_ssrc() const {
Amit Hilbuchc57d5732018-12-11 15:30:11 -0800195 if (send_streams_.empty()) {
Steve Antonafd8e8c2017-12-19 16:35:35 -0800196 return 0;
197 }
Amit Hilbuchc57d5732018-12-11 15:30:11 -0800198 return send_streams_[0].first_ssrc();
Steve Antonafd8e8c2017-12-19 16:35:35 -0800199 }
Harald Alvestrand5fc28b12019-05-13 13:36:16 +0200200 virtual bool has_ssrcs() const {
Amit Hilbuchc57d5732018-12-11 15:30:11 -0800201 if (send_streams_.empty()) {
Steve Antonafd8e8c2017-12-19 16:35:35 -0800202 return false;
203 }
Amit Hilbuchc57d5732018-12-11 15:30:11 -0800204 return send_streams_[0].has_ssrcs();
Steve Antonafd8e8c2017-12-19 16:35:35 -0800205 }
206
Harald Alvestrand5fc28b12019-05-13 13:36:16 +0200207 virtual void set_conference_mode(bool enable) { conference_mode_ = enable; }
208 virtual bool conference_mode() const { return conference_mode_; }
Steve Antonafd8e8c2017-12-19 16:35:35 -0800209
210 // https://tools.ietf.org/html/rfc4566#section-5.7
211 // May be present at the media or session level of SDP. If present at both
212 // levels, the media-level attribute overwrites the session-level one.
Harald Alvestrand5fc28b12019-05-13 13:36:16 +0200213 virtual void set_connection_address(const rtc::SocketAddress& address) {
Steve Antonafd8e8c2017-12-19 16:35:35 -0800214 connection_address_ = address;
215 }
Harald Alvestrand5fc28b12019-05-13 13:36:16 +0200216 virtual const rtc::SocketAddress& connection_address() const {
Steve Antonafd8e8c2017-12-19 16:35:35 -0800217 return connection_address_;
218 }
219
Johannes Kron0854eb62018-10-10 22:33:20 +0200220 // Determines if it's allowed to mix one- and two-byte rtp header extensions
221 // within the same rtp stream.
Johannes Kron9581bc42018-10-23 10:17:39 +0200222 enum ExtmapAllowMixed { kNo, kSession, kMedia };
Harald Alvestrand5fc28b12019-05-13 13:36:16 +0200223 virtual void set_extmap_allow_mixed_enum(
224 ExtmapAllowMixed new_extmap_allow_mixed) {
Johannes Kron9ac3c912018-10-12 10:54:26 +0200225 if (new_extmap_allow_mixed == kMedia &&
Johannes Kron9581bc42018-10-23 10:17:39 +0200226 extmap_allow_mixed_enum_ == kSession) {
Johannes Kron0854eb62018-10-10 22:33:20 +0200227 // Do not downgrade from session level to media level.
228 return;
229 }
Johannes Kron9581bc42018-10-23 10:17:39 +0200230 extmap_allow_mixed_enum_ = new_extmap_allow_mixed;
Johannes Kron0854eb62018-10-10 22:33:20 +0200231 }
Harald Alvestrand5fc28b12019-05-13 13:36:16 +0200232 virtual ExtmapAllowMixed extmap_allow_mixed_enum() const {
Johannes Kron9581bc42018-10-23 10:17:39 +0200233 return extmap_allow_mixed_enum_;
Johannes Kron9ac3c912018-10-12 10:54:26 +0200234 }
Harald Alvestrand5fc28b12019-05-13 13:36:16 +0200235 virtual bool extmap_allow_mixed() const {
236 return extmap_allow_mixed_enum_ != kNo;
237 }
Johannes Kron0854eb62018-10-10 22:33:20 +0200238
Amit Hilbucha2012042018-12-03 11:35:05 -0800239 // Simulcast functionality.
240 virtual bool HasSimulcast() const { return !simulcast_.empty(); }
241 virtual SimulcastDescription& simulcast_description() { return simulcast_; }
242 virtual const SimulcastDescription& simulcast_description() const {
243 return simulcast_;
244 }
245 virtual void set_simulcast_description(
246 const SimulcastDescription& simulcast) {
247 simulcast_ = simulcast;
248 }
Florent Castellib60141b2019-07-03 12:47:54 +0200249 virtual const std::vector<RidDescription>& receive_rids() const {
250 return receive_rids_;
251 }
252 virtual void set_receive_rids(const std::vector<RidDescription>& rids) {
253 receive_rids_ = rids;
254 }
Amit Hilbucha2012042018-12-03 11:35:05 -0800255
Steve Antonafd8e8c2017-12-19 16:35:35 -0800256 protected:
257 bool rtcp_mux_ = false;
258 bool rtcp_reduced_size_ = false;
Sebastian Janssone1795f42019-07-24 11:38:03 +0200259 bool remote_estimate_ = false;
Steve Antonafd8e8c2017-12-19 16:35:35 -0800260 int bandwidth_ = kAutoBandwidth;
Taylor Brandstetteree8c2462020-07-27 15:52:02 -0700261 std::string bandwidth_type_ = kApplicationSpecificBandwidth;
Steve Antonafd8e8c2017-12-19 16:35:35 -0800262 std::string protocol_;
Harald Alvestrand0d018412021-11-04 13:52:31 +0000263 std::vector<CryptoParams> cryptos_;
Steve Antonafd8e8c2017-12-19 16:35:35 -0800264 std::vector<webrtc::RtpExtension> rtp_header_extensions_;
265 bool rtp_header_extensions_set_ = false;
Amit Hilbuchc57d5732018-12-11 15:30:11 -0800266 StreamParamsVec send_streams_;
Steve Antonafd8e8c2017-12-19 16:35:35 -0800267 bool conference_mode_ = false;
268 webrtc::RtpTransceiverDirection direction_ =
269 webrtc::RtpTransceiverDirection::kSendRecv;
270 rtc::SocketAddress connection_address_;
Emil Lundmark801c9992021-01-19 13:06:32 +0100271 ExtmapAllowMixed extmap_allow_mixed_enum_ = kMedia;
Amit Hilbucha2012042018-12-03 11:35:05 -0800272
273 SimulcastDescription simulcast_;
Florent Castellib60141b2019-07-03 12:47:54 +0200274 std::vector<RidDescription> receive_rids_;
Bjorn A Mellem8e1343a2019-09-30 15:12:47 -0700275
Harald Alvestrand0fb07f82020-02-27 20:21:37 +0100276 private:
277 // Copy function that returns a raw pointer. Caller will assert ownership.
278 // Should only be called by the Clone() function. Must be implemented
279 // by each final subclass.
280 virtual MediaContentDescription* CloneInternal() const = 0;
Steve Antonafd8e8c2017-12-19 16:35:35 -0800281};
282
283template <class C>
284class MediaContentDescriptionImpl : public MediaContentDescription {
285 public:
Niels Möllerf1d822b2022-06-07 13:58:27 +0200286 void set_protocol(absl::string_view protocol) override {
Harald Alvestrand5fc28b12019-05-13 13:36:16 +0200287 RTC_DCHECK(IsRtpProtocol(protocol));
Niels Möllerf1d822b2022-06-07 13:58:27 +0200288 protocol_ = std::string(protocol);
Harald Alvestrand5fc28b12019-05-13 13:36:16 +0200289 }
290
Steve Antonafd8e8c2017-12-19 16:35:35 -0800291 typedef C CodecType;
292
293 // Codecs should be in preference order (most preferred codec first).
Harald Alvestrand5fc28b12019-05-13 13:36:16 +0200294 virtual const std::vector<C>& codecs() const { return codecs_; }
295 virtual void set_codecs(const std::vector<C>& codecs) { codecs_ = codecs; }
296 bool has_codecs() const override { return !codecs_.empty(); }
297 virtual bool HasCodec(int id) {
Steve Antonafd8e8c2017-12-19 16:35:35 -0800298 bool found = false;
299 for (typename std::vector<C>::iterator iter = codecs_.begin();
300 iter != codecs_.end(); ++iter) {
301 if (iter->id == id) {
302 found = true;
303 break;
304 }
305 }
306 return found;
307 }
Harald Alvestrand5fc28b12019-05-13 13:36:16 +0200308 virtual void AddCodec(const C& codec) { codecs_.push_back(codec); }
309 virtual void AddOrReplaceCodec(const C& codec) {
Steve Antonafd8e8c2017-12-19 16:35:35 -0800310 for (typename std::vector<C>::iterator iter = codecs_.begin();
311 iter != codecs_.end(); ++iter) {
312 if (iter->id == codec.id) {
313 *iter = codec;
314 return;
315 }
316 }
317 AddCodec(codec);
318 }
Harald Alvestrand5fc28b12019-05-13 13:36:16 +0200319 virtual void AddCodecs(const std::vector<C>& codecs) {
Steve Antonafd8e8c2017-12-19 16:35:35 -0800320 typename std::vector<C>::const_iterator codec;
321 for (codec = codecs.begin(); codec != codecs.end(); ++codec) {
322 AddCodec(*codec);
323 }
324 }
325
326 private:
327 std::vector<C> codecs_;
328};
329
330class AudioContentDescription : public MediaContentDescriptionImpl<AudioCodec> {
331 public:
332 AudioContentDescription() {}
333
Steve Antonafd8e8c2017-12-19 16:35:35 -0800334 virtual MediaType type() const { return MEDIA_TYPE_AUDIO; }
Steve Anton5adfafd2017-12-20 16:34:00 -0800335 virtual AudioContentDescription* as_audio() { return this; }
336 virtual const AudioContentDescription* as_audio() const { return this; }
Harald Alvestrand0fb07f82020-02-27 20:21:37 +0100337
338 private:
339 virtual AudioContentDescription* CloneInternal() const {
340 return new AudioContentDescription(*this);
341 }
Steve Antonafd8e8c2017-12-19 16:35:35 -0800342};
343
344class VideoContentDescription : public MediaContentDescriptionImpl<VideoCodec> {
345 public:
Steve Antonafd8e8c2017-12-19 16:35:35 -0800346 virtual MediaType type() const { return MEDIA_TYPE_VIDEO; }
Steve Anton5adfafd2017-12-20 16:34:00 -0800347 virtual VideoContentDescription* as_video() { return this; }
348 virtual const VideoContentDescription* as_video() const { return this; }
Harald Alvestrand0fb07f82020-02-27 20:21:37 +0100349
350 private:
351 virtual VideoContentDescription* CloneInternal() const {
352 return new VideoContentDescription(*this);
353 }
Steve Antonafd8e8c2017-12-19 16:35:35 -0800354};
355
Harald Alvestrand5fc28b12019-05-13 13:36:16 +0200356class SctpDataContentDescription : public MediaContentDescription {
357 public:
358 SctpDataContentDescription() {}
359 SctpDataContentDescription(const SctpDataContentDescription& o)
360 : MediaContentDescription(o),
361 use_sctpmap_(o.use_sctpmap_),
362 port_(o.port_),
Harald Alvestrandc5effc22019-06-11 11:46:59 +0200363 max_message_size_(o.max_message_size_) {}
Harald Alvestrand5fc28b12019-05-13 13:36:16 +0200364 MediaType type() const override { return MEDIA_TYPE_DATA; }
365 SctpDataContentDescription* as_sctp() override { return this; }
366 const SctpDataContentDescription* as_sctp() const override { return this; }
Harald Alvestrand5fc28b12019-05-13 13:36:16 +0200367
368 bool has_codecs() const override { return false; }
Niels Möllerf1d822b2022-06-07 13:58:27 +0200369 void set_protocol(absl::string_view protocol) override {
Harald Alvestrand5fc28b12019-05-13 13:36:16 +0200370 RTC_DCHECK(IsSctpProtocol(protocol));
Niels Möllerf1d822b2022-06-07 13:58:27 +0200371 protocol_ = std::string(protocol);
Harald Alvestrand5fc28b12019-05-13 13:36:16 +0200372 }
Steve Antonafd8e8c2017-12-19 16:35:35 -0800373
374 bool use_sctpmap() const { return use_sctpmap_; }
375 void set_use_sctpmap(bool enable) { use_sctpmap_ = enable; }
Harald Alvestrand5fc28b12019-05-13 13:36:16 +0200376 int port() const { return port_; }
377 void set_port(int port) { port_ = port; }
378 int max_message_size() const { return max_message_size_; }
379 void set_max_message_size(int max_message_size) {
380 max_message_size_ = max_message_size;
381 }
Steve Antonafd8e8c2017-12-19 16:35:35 -0800382
383 private:
Harald Alvestrand0fb07f82020-02-27 20:21:37 +0100384 SctpDataContentDescription* CloneInternal() const override {
385 return new SctpDataContentDescription(*this);
386 }
Harald Alvestrand5fc28b12019-05-13 13:36:16 +0200387 bool use_sctpmap_ = true; // Note: "true" is no longer conformant.
388 // Defaults should be constants imported from SCTP. Quick hack.
389 int port_ = 5000;
Harald Alvestrandfbb45bd2019-05-15 08:07:47 +0200390 // draft-ietf-mmusic-sdp-sctp-23: Max message size default is 64K
391 int max_message_size_ = 64 * 1024;
Steve Antonafd8e8c2017-12-19 16:35:35 -0800392};
393
Philipp Hancke4e8c1152020-10-13 12:43:15 +0200394class UnsupportedContentDescription : public MediaContentDescription {
395 public:
Niels Möllerf1d822b2022-06-07 13:58:27 +0200396 explicit UnsupportedContentDescription(absl::string_view media_type)
Philipp Hancke4e8c1152020-10-13 12:43:15 +0200397 : media_type_(media_type) {}
398 MediaType type() const override { return MEDIA_TYPE_UNSUPPORTED; }
399
400 UnsupportedContentDescription* as_unsupported() override { return this; }
401 const UnsupportedContentDescription* as_unsupported() const override {
402 return this;
403 }
404
405 bool has_codecs() const override { return false; }
406 const std::string& media_type() const { return media_type_; }
407
408 private:
409 UnsupportedContentDescription* CloneInternal() const override {
410 return new UnsupportedContentDescription(*this);
411 }
412
413 std::string media_type_;
414};
415
Steve Anton5adfafd2017-12-20 16:34:00 -0800416// Protocol used for encoding media. This is the "top level" protocol that may
417// be wrapped by zero or many transport protocols (UDP, ICE, etc.).
418enum class MediaProtocolType {
Philipp Hancke4e8c1152020-10-13 12:43:15 +0200419 kRtp, // Section will use the RTP protocol (e.g., for audio or video).
420 // https://tools.ietf.org/html/rfc3550
421 kSctp, // Section will use the SCTP protocol (e.g., for a data channel).
422 // https://tools.ietf.org/html/rfc4960
423 kOther // Section will use another top protocol which is not
424 // explicitly supported.
Steve Anton5adfafd2017-12-20 16:34:00 -0800425};
426
Steve Anton5adfafd2017-12-20 16:34:00 -0800427// Represents a session description section. Most information about the section
428// is stored in the description, which is a subclass of MediaContentDescription.
Harald Alvestrand1716d392019-06-03 20:35:45 +0200429// Owns the description.
Mirko Bonadei35214fc2019-09-23 14:54:28 +0200430class RTC_EXPORT ContentInfo {
Harald Alvestrand1716d392019-06-03 20:35:45 +0200431 public:
Steve Anton5adfafd2017-12-20 16:34:00 -0800432 explicit ContentInfo(MediaProtocolType type) : type(type) {}
Harald Alvestrand1716d392019-06-03 20:35:45 +0200433 ~ContentInfo();
434 // Copy
435 ContentInfo(const ContentInfo& o);
436 ContentInfo& operator=(const ContentInfo& o);
437 ContentInfo(ContentInfo&& o) = default;
438 ContentInfo& operator=(ContentInfo&& o) = default;
Steve Anton5adfafd2017-12-20 16:34:00 -0800439
Artem Titov880fa812021-07-30 22:30:23 +0200440 // Alias for `name`.
Steve Anton5adfafd2017-12-20 16:34:00 -0800441 std::string mid() const { return name; }
442 void set_mid(const std::string& mid) { this->name = mid; }
443
Artem Titov880fa812021-07-30 22:30:23 +0200444 // Alias for `description`.
Harald Alvestrand1716d392019-06-03 20:35:45 +0200445 MediaContentDescription* media_description();
446 const MediaContentDescription* media_description() const;
447
448 void set_media_description(std::unique_ptr<MediaContentDescription> desc) {
449 description_ = std::move(desc);
Steve Anton5adfafd2017-12-20 16:34:00 -0800450 }
451
Steve Anton81712112018-01-05 11:27:54 -0800452 // TODO(bugs.webrtc.org/8620): Rename this to mid.
Steve Anton4ab68ee2017-12-19 14:26:11 -0800453 std::string name;
Steve Anton5adfafd2017-12-20 16:34:00 -0800454 MediaProtocolType type;
Steve Anton4ab68ee2017-12-19 14:26:11 -0800455 bool rejected = false;
456 bool bundle_only = false;
Harald Alvestrand1716d392019-06-03 20:35:45 +0200457
458 private:
459 friend class SessionDescription;
460 std::unique_ptr<MediaContentDescription> description_;
Steve Anton4ab68ee2017-12-19 14:26:11 -0800461};
462
463typedef std::vector<std::string> ContentNames;
464
465// This class provides a mechanism to aggregate different media contents into a
466// group. This group can also be shared with the peers in a pre-defined format.
Artem Titov880fa812021-07-30 22:30:23 +0200467// GroupInfo should be populated only with the `content_name` of the
Steve Anton4ab68ee2017-12-19 14:26:11 -0800468// MediaDescription.
469class ContentGroup {
470 public:
471 explicit ContentGroup(const std::string& semantics);
472 ContentGroup(const ContentGroup&);
473 ContentGroup(ContentGroup&&);
474 ContentGroup& operator=(const ContentGroup&);
475 ContentGroup& operator=(ContentGroup&&);
476 ~ContentGroup();
477
478 const std::string& semantics() const { return semantics_; }
479 const ContentNames& content_names() const { return content_names_; }
480
481 const std::string* FirstContentName() const;
Niels Möllerf1d822b2022-06-07 13:58:27 +0200482 bool HasContentName(absl::string_view content_name) const;
483 void AddContentName(absl::string_view content_name);
484 bool RemoveContentName(absl::string_view content_name);
Harald Alvestrand7a2db8a2021-06-14 15:41:30 +0000485 // for debugging
486 std::string ToString() const;
Steve Anton4ab68ee2017-12-19 14:26:11 -0800487
488 private:
489 std::string semantics_;
490 ContentNames content_names_;
491};
492
493typedef std::vector<ContentInfo> ContentInfos;
494typedef std::vector<ContentGroup> ContentGroups;
495
496const ContentInfo* FindContentInfoByName(const ContentInfos& contents,
497 const std::string& name);
498const ContentInfo* FindContentInfoByType(const ContentInfos& contents,
499 const std::string& type);
500
Steve Antone831b8c2018-02-01 12:22:16 -0800501// Determines how the MSID will be signaled in the SDP. These can be used as
502// flags to indicate both or none.
503enum MsidSignaling {
504 // Signal MSID with one a=msid line in the media section.
505 kMsidSignalingMediaSection = 0x1,
506 // Signal MSID with a=ssrc: msid lines in the media section.
507 kMsidSignalingSsrcAttribute = 0x2
508};
509
Steve Anton4ab68ee2017-12-19 14:26:11 -0800510// Describes a collection of contents, each with its own name and
511// type. Analogous to a <jingle> or <session> stanza. Assumes that
512// contents are unique be name, but doesn't enforce that.
513class SessionDescription {
514 public:
515 SessionDescription();
Steve Anton4ab68ee2017-12-19 14:26:11 -0800516 ~SessionDescription();
517
Harald Alvestrand4d7160e2019-04-12 07:01:29 +0200518 std::unique_ptr<SessionDescription> Clone() const;
Steve Anton4ab68ee2017-12-19 14:26:11 -0800519
520 // Content accessors.
521 const ContentInfos& contents() const { return contents_; }
522 ContentInfos& contents() { return contents_; }
523 const ContentInfo* GetContentByName(const std::string& name) const;
524 ContentInfo* GetContentByName(const std::string& name);
Steve Antonb1c1de12017-12-21 15:14:30 -0800525 const MediaContentDescription* GetContentDescriptionByName(
Steve Anton4ab68ee2017-12-19 14:26:11 -0800526 const std::string& name) const;
Steve Antonb1c1de12017-12-21 15:14:30 -0800527 MediaContentDescription* GetContentDescriptionByName(const std::string& name);
Steve Anton5adfafd2017-12-20 16:34:00 -0800528 const ContentInfo* FirstContentByType(MediaProtocolType type) const;
Steve Anton4ab68ee2017-12-19 14:26:11 -0800529 const ContentInfo* FirstContent() const;
530
531 // Content mutators.
532 // Adds a content to this description. Takes ownership of ContentDescription*.
533 void AddContent(const std::string& name,
Steve Anton5adfafd2017-12-20 16:34:00 -0800534 MediaProtocolType type,
Harald Alvestrand1716d392019-06-03 20:35:45 +0200535 std::unique_ptr<MediaContentDescription> description);
Steve Anton4ab68ee2017-12-19 14:26:11 -0800536 void AddContent(const std::string& name,
Steve Anton5adfafd2017-12-20 16:34:00 -0800537 MediaProtocolType type,
Steve Anton4ab68ee2017-12-19 14:26:11 -0800538 bool rejected,
Harald Alvestrand1716d392019-06-03 20:35:45 +0200539 std::unique_ptr<MediaContentDescription> description);
Steve Anton4ab68ee2017-12-19 14:26:11 -0800540 void AddContent(const std::string& name,
Steve Anton5adfafd2017-12-20 16:34:00 -0800541 MediaProtocolType type,
Steve Anton4ab68ee2017-12-19 14:26:11 -0800542 bool rejected,
543 bool bundle_only,
Harald Alvestrand1716d392019-06-03 20:35:45 +0200544 std::unique_ptr<MediaContentDescription> description);
545 void AddContent(ContentInfo&& content);
Johannes Kron9ac3c912018-10-12 10:54:26 +0200546
Steve Anton4ab68ee2017-12-19 14:26:11 -0800547 bool RemoveContentByName(const std::string& name);
548
549 // Transport accessors.
550 const TransportInfos& transport_infos() const { return transport_infos_; }
551 TransportInfos& transport_infos() { return transport_infos_; }
552 const TransportInfo* GetTransportInfoByName(const std::string& name) const;
553 TransportInfo* GetTransportInfoByName(const std::string& name);
554 const TransportDescription* GetTransportDescriptionByName(
555 const std::string& name) const {
556 const TransportInfo* tinfo = GetTransportInfoByName(name);
557 return tinfo ? &tinfo->description : NULL;
558 }
559
560 // Transport mutators.
561 void set_transport_infos(const TransportInfos& transport_infos) {
562 transport_infos_ = transport_infos;
563 }
564 // Adds a TransportInfo to this description.
Steve Anton06817cd2018-12-18 15:55:30 -0800565 void AddTransportInfo(const TransportInfo& transport_info);
Steve Anton4ab68ee2017-12-19 14:26:11 -0800566 bool RemoveTransportInfoByName(const std::string& name);
567
568 // Group accessors.
569 const ContentGroups& groups() const { return content_groups_; }
570 const ContentGroup* GetGroupByName(const std::string& name) const;
Henrik Boströmf8187e02021-04-26 21:04:26 +0200571 std::vector<const ContentGroup*> GetGroupsByName(
572 const std::string& name) const;
Steve Anton4ab68ee2017-12-19 14:26:11 -0800573 bool HasGroup(const std::string& name) const;
574
575 // Group mutators.
576 void AddGroup(const ContentGroup& group) { content_groups_.push_back(group); }
Artem Titov880fa812021-07-30 22:30:23 +0200577 // Remove the first group with the same semantics specified by `name`.
Steve Anton4ab68ee2017-12-19 14:26:11 -0800578 void RemoveGroupByName(const std::string& name);
579
580 // Global attributes.
581 void set_msid_supported(bool supported) { msid_supported_ = supported; }
582 bool msid_supported() const { return msid_supported_; }
583
Steve Antone831b8c2018-02-01 12:22:16 -0800584 // Determines how the MSIDs were/will be signaled. Flag value composed of
585 // MsidSignaling bits (see enum above).
586 void set_msid_signaling(int msid_signaling) {
587 msid_signaling_ = msid_signaling;
588 }
589 int msid_signaling() const { return msid_signaling_; }
590
Johannes Kron0854eb62018-10-10 22:33:20 +0200591 // Determines if it's allowed to mix one- and two-byte rtp header extensions
592 // within the same rtp stream.
Johannes Kron9581bc42018-10-23 10:17:39 +0200593 void set_extmap_allow_mixed(bool supported) {
594 extmap_allow_mixed_ = supported;
595 MediaContentDescription::ExtmapAllowMixed media_level_setting =
Johannes Kron0854eb62018-10-10 22:33:20 +0200596 supported ? MediaContentDescription::kSession
597 : MediaContentDescription::kNo;
598 for (auto& content : contents_) {
Johannes Kron9ac3c912018-10-12 10:54:26 +0200599 // Do not set to kNo if the current setting is kMedia.
Johannes Kron9581bc42018-10-23 10:17:39 +0200600 if (supported || content.media_description()->extmap_allow_mixed_enum() !=
601 MediaContentDescription::kMedia) {
602 content.media_description()->set_extmap_allow_mixed_enum(
Johannes Kron9ac3c912018-10-12 10:54:26 +0200603 media_level_setting);
604 }
Johannes Kron0854eb62018-10-10 22:33:20 +0200605 }
606 }
Johannes Kron9581bc42018-10-23 10:17:39 +0200607 bool extmap_allow_mixed() const { return extmap_allow_mixed_; }
Johannes Kron0854eb62018-10-10 22:33:20 +0200608
Steve Anton4ab68ee2017-12-19 14:26:11 -0800609 private:
610 SessionDescription(const SessionDescription&);
611
612 ContentInfos contents_;
613 TransportInfos transport_infos_;
614 ContentGroups content_groups_;
615 bool msid_supported_ = true;
Steve Antone831b8c2018-02-01 12:22:16 -0800616 // Default to what Plan B would do.
617 // TODO(bugs.webrtc.org/8530): Change default to kMsidSignalingMediaSection.
618 int msid_signaling_ = kMsidSignalingSsrcAttribute;
Emil Lundmark801c9992021-01-19 13:06:32 +0100619 bool extmap_allow_mixed_ = true;
Steve Anton4ab68ee2017-12-19 14:26:11 -0800620};
621
Steve Antonb1c1de12017-12-21 15:14:30 -0800622// Indicates whether a session description was sent by the local client or
623// received from the remote client.
Steve Anton4ab68ee2017-12-19 14:26:11 -0800624enum ContentSource { CS_LOCAL, CS_REMOTE };
625
626} // namespace cricket
627
Steve Anton10542f22019-01-11 09:11:00 -0800628#endif // PC_SESSION_DESCRIPTION_H_