blob: 60c3d6b92cba2a53222f6ab657af19f775193c48 [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>
16#include <iosfwd>
Harald Alvestrand4d7160e2019-04-12 07:01:29 +020017#include <memory>
Steve Anton4ab68ee2017-12-19 14:26:11 -080018#include <string>
19#include <vector>
20
Harald Alvestrand14b27582019-05-04 11:37:04 +020021#include "absl/memory/memory.h"
Steve Anton10542f22019-01-11 09:11:00 -080022#include "api/crypto_params.h"
23#include "api/media_types.h"
24#include "api/rtp_parameters.h"
25#include "api/rtp_transceiver_interface.h"
26#include "media/base/media_channel.h"
27#include "media/base/stream_params.h"
28#include "p2p/base/transport_description.h"
29#include "p2p/base/transport_info.h"
Harald Alvestrand14b27582019-05-04 11:37:04 +020030#include "pc/media_protocol_names.h"
Steve Anton10542f22019-01-11 09:11:00 -080031#include "pc/simulcast_description.h"
32#include "rtc_base/socket_address.h"
Steve Anton4ab68ee2017-12-19 14:26:11 -080033
34namespace cricket {
35
Steve Antonafd8e8c2017-12-19 16:35:35 -080036typedef std::vector<AudioCodec> AudioCodecs;
37typedef std::vector<VideoCodec> VideoCodecs;
Harald Alvestrand14b27582019-05-04 11:37:04 +020038typedef std::vector<RtpDataCodec> RtpDataCodecs;
Steve Antonafd8e8c2017-12-19 16:35:35 -080039typedef std::vector<CryptoParams> CryptoParamsVec;
40typedef std::vector<webrtc::RtpExtension> RtpHeaderExtensions;
41
42// RTC4585 RTP/AVPF
43extern const char kMediaProtocolAvpf[];
44// RFC5124 RTP/SAVPF
45extern const char kMediaProtocolSavpf[];
46
47extern const char kMediaProtocolDtlsSavpf[];
48
Steve Antonafd8e8c2017-12-19 16:35:35 -080049
50// Options to control how session descriptions are generated.
51const int kAutoBandwidth = -1;
52
Steve Anton5adfafd2017-12-20 16:34:00 -080053class AudioContentDescription;
Yves Gerey3e707812018-11-28 16:47:49 +010054class VideoContentDescription;
Harald Alvestrand14b27582019-05-04 11:37:04 +020055class DataContentDescription;
56class RtpDataContentDescription;
57class SctpDataContentDescription;
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 Alvestrand14b27582019-05-04 11:37:04 +020078 // Backwards compatible shim: Return a shim object that allows
79 // callers to ignore the distinction between RtpDataContentDescription
80 // and SctpDataContentDescription objects.
Steve Anton5adfafd2017-12-20 16:34:00 -080081 virtual DataContentDescription* as_data() { return nullptr; }
82 virtual const DataContentDescription* as_data() const { return nullptr; }
83
Harald Alvestrand14b27582019-05-04 11:37:04 +020084 virtual RtpDataContentDescription* as_rtp_data() { return nullptr; }
85 virtual const RtpDataContentDescription* as_rtp_data() const {
86 return nullptr;
87 }
88
89 virtual SctpDataContentDescription* as_sctp() { return nullptr; }
90 virtual const SctpDataContentDescription* as_sctp() const { return nullptr; }
91
Steve Antonafd8e8c2017-12-19 16:35:35 -080092 virtual bool has_codecs() const = 0;
93
Steve Anton5adfafd2017-12-20 16:34:00 -080094 virtual MediaContentDescription* Copy() const = 0;
95
Steve Antonafd8e8c2017-12-19 16:35:35 -080096 // |protocol| is the expected media transport protocol, such as RTP/AVPF,
97 // RTP/SAVPF or SCTP/DTLS.
Harald Alvestrand14b27582019-05-04 11:37:04 +020098 virtual std::string protocol() const { return protocol_; }
99 virtual void set_protocol(const std::string& protocol) {
100 protocol_ = protocol;
101 }
Steve Antonafd8e8c2017-12-19 16:35:35 -0800102
Harald Alvestrand14b27582019-05-04 11:37:04 +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 Alvestrand14b27582019-05-04 11:37:04 +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 Alvestrand14b27582019-05-04 11:37:04 +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
Harald Alvestrand14b27582019-05-04 11:37:04 +0200118 virtual int bandwidth() const { return bandwidth_; }
119 virtual void set_bandwidth(int bandwidth) { bandwidth_ = bandwidth; }
Steve Antonafd8e8c2017-12-19 16:35:35 -0800120
Harald Alvestrand14b27582019-05-04 11:37:04 +0200121 virtual const std::vector<CryptoParams>& cryptos() const { return cryptos_; }
122 virtual void AddCrypto(const CryptoParams& params) {
123 cryptos_.push_back(params);
124 }
125 virtual void set_cryptos(const std::vector<CryptoParams>& cryptos) {
Steve Antonafd8e8c2017-12-19 16:35:35 -0800126 cryptos_ = cryptos;
127 }
128
Harald Alvestrand14b27582019-05-04 11:37:04 +0200129 virtual const RtpHeaderExtensions& rtp_header_extensions() const {
Steve Antonafd8e8c2017-12-19 16:35:35 -0800130 return rtp_header_extensions_;
131 }
Harald Alvestrand14b27582019-05-04 11:37:04 +0200132 virtual void set_rtp_header_extensions(
133 const RtpHeaderExtensions& extensions) {
Steve Antonafd8e8c2017-12-19 16:35:35 -0800134 rtp_header_extensions_ = extensions;
135 rtp_header_extensions_set_ = true;
136 }
Harald Alvestrand14b27582019-05-04 11:37:04 +0200137 virtual void AddRtpHeaderExtension(const webrtc::RtpExtension& ext) {
Steve Antonafd8e8c2017-12-19 16:35:35 -0800138 rtp_header_extensions_.push_back(ext);
139 rtp_header_extensions_set_ = true;
140 }
Harald Alvestrand14b27582019-05-04 11:37:04 +0200141 virtual void AddRtpHeaderExtension(const cricket::RtpHeaderExtension& ext) {
Steve Antonafd8e8c2017-12-19 16:35:35 -0800142 webrtc::RtpExtension webrtc_extension;
143 webrtc_extension.uri = ext.uri;
144 webrtc_extension.id = ext.id;
145 rtp_header_extensions_.push_back(webrtc_extension);
146 rtp_header_extensions_set_ = true;
147 }
Harald Alvestrand14b27582019-05-04 11:37:04 +0200148 virtual void ClearRtpHeaderExtensions() {
Steve Antonafd8e8c2017-12-19 16:35:35 -0800149 rtp_header_extensions_.clear();
150 rtp_header_extensions_set_ = true;
151 }
152 // We can't always tell if an empty list of header extensions is
153 // because the other side doesn't support them, or just isn't hooked up to
154 // signal them. For now we assume an empty list means no signaling, but
155 // provide the ClearRtpHeaderExtensions method to allow "no support" to be
156 // clearly indicated (i.e. when derived from other information).
Harald Alvestrand14b27582019-05-04 11:37:04 +0200157 virtual bool rtp_header_extensions_set() const {
158 return rtp_header_extensions_set_;
159 }
160 virtual const StreamParamsVec& streams() const { return send_streams_; }
Steve Antonafd8e8c2017-12-19 16:35:35 -0800161 // TODO(pthatcher): Remove this by giving mediamessage.cc access
162 // to MediaContentDescription
Harald Alvestrand14b27582019-05-04 11:37:04 +0200163 virtual StreamParamsVec& mutable_streams() { return send_streams_; }
164 virtual void AddStream(const StreamParams& stream) {
Amit Hilbuchc57d5732018-12-11 15:30:11 -0800165 send_streams_.push_back(stream);
166 }
Steve Antonafd8e8c2017-12-19 16:35:35 -0800167 // Legacy streams have an ssrc, but nothing else.
168 void AddLegacyStream(uint32_t ssrc) {
Harald Alvestrand14b27582019-05-04 11:37:04 +0200169 AddStream(StreamParams::CreateLegacy(ssrc));
Steve Antonafd8e8c2017-12-19 16:35:35 -0800170 }
171 void AddLegacyStream(uint32_t ssrc, uint32_t fid_ssrc) {
172 StreamParams sp = StreamParams::CreateLegacy(ssrc);
173 sp.AddFidSsrc(ssrc, fid_ssrc);
Harald Alvestrand14b27582019-05-04 11:37:04 +0200174 AddStream(sp);
Steve Antonafd8e8c2017-12-19 16:35:35 -0800175 }
Amit Hilbuchc57d5732018-12-11 15:30:11 -0800176
Steve Antonafd8e8c2017-12-19 16:35:35 -0800177 // Sets the CNAME of all StreamParams if it have not been set.
Harald Alvestrand14b27582019-05-04 11:37:04 +0200178 virtual void SetCnameIfEmpty(const std::string& cname) {
Amit Hilbuchc57d5732018-12-11 15:30:11 -0800179 for (cricket::StreamParamsVec::iterator it = send_streams_.begin();
180 it != send_streams_.end(); ++it) {
Steve Antonafd8e8c2017-12-19 16:35:35 -0800181 if (it->cname.empty())
182 it->cname = cname;
183 }
184 }
Harald Alvestrand14b27582019-05-04 11:37:04 +0200185 virtual uint32_t first_ssrc() const {
Amit Hilbuchc57d5732018-12-11 15:30:11 -0800186 if (send_streams_.empty()) {
Steve Antonafd8e8c2017-12-19 16:35:35 -0800187 return 0;
188 }
Amit Hilbuchc57d5732018-12-11 15:30:11 -0800189 return send_streams_[0].first_ssrc();
Steve Antonafd8e8c2017-12-19 16:35:35 -0800190 }
Harald Alvestrand14b27582019-05-04 11:37:04 +0200191 virtual bool has_ssrcs() const {
Amit Hilbuchc57d5732018-12-11 15:30:11 -0800192 if (send_streams_.empty()) {
Steve Antonafd8e8c2017-12-19 16:35:35 -0800193 return false;
194 }
Amit Hilbuchc57d5732018-12-11 15:30:11 -0800195 return send_streams_[0].has_ssrcs();
Steve Antonafd8e8c2017-12-19 16:35:35 -0800196 }
197
Harald Alvestrand14b27582019-05-04 11:37:04 +0200198 virtual void set_conference_mode(bool enable) { conference_mode_ = enable; }
199 virtual bool conference_mode() const { return conference_mode_; }
Steve Antonafd8e8c2017-12-19 16:35:35 -0800200
201 // https://tools.ietf.org/html/rfc4566#section-5.7
202 // May be present at the media or session level of SDP. If present at both
203 // levels, the media-level attribute overwrites the session-level one.
Harald Alvestrand14b27582019-05-04 11:37:04 +0200204 virtual void set_connection_address(const rtc::SocketAddress& address) {
Steve Antonafd8e8c2017-12-19 16:35:35 -0800205 connection_address_ = address;
206 }
Harald Alvestrand14b27582019-05-04 11:37:04 +0200207 virtual const rtc::SocketAddress& connection_address() const {
Steve Antonafd8e8c2017-12-19 16:35:35 -0800208 return connection_address_;
209 }
210
Johannes Kron0854eb62018-10-10 22:33:20 +0200211 // Determines if it's allowed to mix one- and two-byte rtp header extensions
212 // within the same rtp stream.
Johannes Kron9581bc42018-10-23 10:17:39 +0200213 enum ExtmapAllowMixed { kNo, kSession, kMedia };
Harald Alvestrand14b27582019-05-04 11:37:04 +0200214 virtual void set_extmap_allow_mixed_enum(
215 ExtmapAllowMixed new_extmap_allow_mixed) {
Johannes Kron9ac3c912018-10-12 10:54:26 +0200216 if (new_extmap_allow_mixed == kMedia &&
Johannes Kron9581bc42018-10-23 10:17:39 +0200217 extmap_allow_mixed_enum_ == kSession) {
Johannes Kron0854eb62018-10-10 22:33:20 +0200218 // Do not downgrade from session level to media level.
219 return;
220 }
Johannes Kron9581bc42018-10-23 10:17:39 +0200221 extmap_allow_mixed_enum_ = new_extmap_allow_mixed;
Johannes Kron0854eb62018-10-10 22:33:20 +0200222 }
Harald Alvestrand14b27582019-05-04 11:37:04 +0200223 virtual ExtmapAllowMixed extmap_allow_mixed_enum() const {
Johannes Kron9581bc42018-10-23 10:17:39 +0200224 return extmap_allow_mixed_enum_;
Johannes Kron9ac3c912018-10-12 10:54:26 +0200225 }
Harald Alvestrand14b27582019-05-04 11:37:04 +0200226 virtual bool extmap_allow_mixed() const {
227 return extmap_allow_mixed_enum_ != kNo;
228 }
Johannes Kron0854eb62018-10-10 22:33:20 +0200229
Amit Hilbucha2012042018-12-03 11:35:05 -0800230 // Simulcast functionality.
231 virtual bool HasSimulcast() const { return !simulcast_.empty(); }
232 virtual SimulcastDescription& simulcast_description() { return simulcast_; }
233 virtual const SimulcastDescription& simulcast_description() const {
234 return simulcast_;
235 }
236 virtual void set_simulcast_description(
237 const SimulcastDescription& simulcast) {
238 simulcast_ = simulcast;
239 }
240
Steve Antonafd8e8c2017-12-19 16:35:35 -0800241 protected:
242 bool rtcp_mux_ = false;
243 bool rtcp_reduced_size_ = false;
244 int bandwidth_ = kAutoBandwidth;
245 std::string protocol_;
246 std::vector<CryptoParams> cryptos_;
247 std::vector<webrtc::RtpExtension> rtp_header_extensions_;
248 bool rtp_header_extensions_set_ = false;
Amit Hilbuchc57d5732018-12-11 15:30:11 -0800249 StreamParamsVec send_streams_;
Steve Antonafd8e8c2017-12-19 16:35:35 -0800250 bool conference_mode_ = false;
251 webrtc::RtpTransceiverDirection direction_ =
252 webrtc::RtpTransceiverDirection::kSendRecv;
253 rtc::SocketAddress connection_address_;
Johannes Kron0854eb62018-10-10 22:33:20 +0200254 // Mixed one- and two-byte header not included in offer on media level or
255 // session level, but we will respond that we support it. The plan is to add
256 // it to our offer on session level. See todo in SessionDescription.
Johannes Kron9581bc42018-10-23 10:17:39 +0200257 ExtmapAllowMixed extmap_allow_mixed_enum_ = kNo;
Amit Hilbucha2012042018-12-03 11:35:05 -0800258
259 SimulcastDescription simulcast_;
Steve Antonafd8e8c2017-12-19 16:35:35 -0800260};
261
Steve Anton5adfafd2017-12-20 16:34:00 -0800262// TODO(bugs.webrtc.org/8620): Remove this alias once downstream projects have
263// updated.
264using ContentDescription = MediaContentDescription;
265
Steve Antonafd8e8c2017-12-19 16:35:35 -0800266template <class C>
267class MediaContentDescriptionImpl : public MediaContentDescription {
268 public:
Harald Alvestrand14b27582019-05-04 11:37:04 +0200269 void set_protocol(const std::string& protocol) override {
270 RTC_DCHECK(IsRtpProtocol(protocol));
271 protocol_ = protocol;
272 }
273
Steve Antonafd8e8c2017-12-19 16:35:35 -0800274 typedef C CodecType;
275
276 // Codecs should be in preference order (most preferred codec first).
Harald Alvestrand14b27582019-05-04 11:37:04 +0200277 virtual const std::vector<C>& codecs() const { return codecs_; }
278 virtual void set_codecs(const std::vector<C>& codecs) { codecs_ = codecs; }
279 bool has_codecs() const override { return !codecs_.empty(); }
280 virtual bool HasCodec(int id) {
Steve Antonafd8e8c2017-12-19 16:35:35 -0800281 bool found = false;
282 for (typename std::vector<C>::iterator iter = codecs_.begin();
283 iter != codecs_.end(); ++iter) {
284 if (iter->id == id) {
285 found = true;
286 break;
287 }
288 }
289 return found;
290 }
Harald Alvestrand14b27582019-05-04 11:37:04 +0200291 virtual void AddCodec(const C& codec) { codecs_.push_back(codec); }
292 virtual void AddOrReplaceCodec(const C& codec) {
Steve Antonafd8e8c2017-12-19 16:35:35 -0800293 for (typename std::vector<C>::iterator iter = codecs_.begin();
294 iter != codecs_.end(); ++iter) {
295 if (iter->id == codec.id) {
296 *iter = codec;
297 return;
298 }
299 }
300 AddCodec(codec);
301 }
Harald Alvestrand14b27582019-05-04 11:37:04 +0200302 virtual void AddCodecs(const std::vector<C>& codecs) {
Steve Antonafd8e8c2017-12-19 16:35:35 -0800303 typename std::vector<C>::const_iterator codec;
304 for (codec = codecs.begin(); codec != codecs.end(); ++codec) {
305 AddCodec(*codec);
306 }
307 }
308
309 private:
310 std::vector<C> codecs_;
311};
312
313class AudioContentDescription : public MediaContentDescriptionImpl<AudioCodec> {
314 public:
315 AudioContentDescription() {}
316
Steve Antonb1c1de12017-12-21 15:14:30 -0800317 virtual AudioContentDescription* Copy() const {
Steve Antonafd8e8c2017-12-19 16:35:35 -0800318 return new AudioContentDescription(*this);
319 }
320 virtual MediaType type() const { return MEDIA_TYPE_AUDIO; }
Steve Anton5adfafd2017-12-20 16:34:00 -0800321 virtual AudioContentDescription* as_audio() { return this; }
322 virtual const AudioContentDescription* as_audio() const { return this; }
Steve Antonafd8e8c2017-12-19 16:35:35 -0800323};
324
325class VideoContentDescription : public MediaContentDescriptionImpl<VideoCodec> {
326 public:
Steve Antonb1c1de12017-12-21 15:14:30 -0800327 virtual VideoContentDescription* Copy() const {
Steve Antonafd8e8c2017-12-19 16:35:35 -0800328 return new VideoContentDescription(*this);
329 }
330 virtual MediaType type() const { return MEDIA_TYPE_VIDEO; }
Steve Anton5adfafd2017-12-20 16:34:00 -0800331 virtual VideoContentDescription* as_video() { return this; }
332 virtual const VideoContentDescription* as_video() const { return this; }
Steve Antonafd8e8c2017-12-19 16:35:35 -0800333};
334
Harald Alvestrand14b27582019-05-04 11:37:04 +0200335// The DataContentDescription is a shim over the RtpDataContentDescription
336// and SctpDataContentDescription classes that is used for external callers
337// into this internal API.
338// It is a templated derivation of MediaContentDescriptionImpl because
339// that's what the external caller expects it to be.
340// TODO(bugs.webrtc.org/10597): Declare this class obsolete and remove it
341// once external callers have been updated.
Steve Antonafd8e8c2017-12-19 16:35:35 -0800342class DataContentDescription : public MediaContentDescriptionImpl<DataCodec> {
343 public:
Harald Alvestrand14b27582019-05-04 11:37:04 +0200344 DataContentDescription();
345 MediaType type() const override { return MEDIA_TYPE_DATA; }
346 DataContentDescription* as_data() override { return this; }
347 const DataContentDescription* as_data() const override { return this; }
Steve Antonafd8e8c2017-12-19 16:35:35 -0800348
Harald Alvestrand14b27582019-05-04 11:37:04 +0200349 // Override all methods defined in MediaContentDescription.
350 bool has_codecs() const override;
351 DataContentDescription* Copy() const override {
352 return new DataContentDescription(this);
Steve Antonafd8e8c2017-12-19 16:35:35 -0800353 }
Harald Alvestrand14b27582019-05-04 11:37:04 +0200354 std::string protocol() const override;
355 void set_protocol(const std::string& protocol) override;
356 webrtc::RtpTransceiverDirection direction() const override;
357 void set_direction(webrtc::RtpTransceiverDirection direction) override;
358 bool rtcp_mux() const override;
359 void set_rtcp_mux(bool mux) override;
360 bool rtcp_reduced_size() const override;
361 void set_rtcp_reduced_size(bool) override;
362 int bandwidth() const override;
363 void set_bandwidth(int bandwidth) override;
364 const std::vector<CryptoParams>& cryptos() const override;
365 void AddCrypto(const CryptoParams& params) override;
366 void set_cryptos(const std::vector<CryptoParams>& cryptos) override;
367 const RtpHeaderExtensions& rtp_header_extensions() const override;
368 void set_rtp_header_extensions(
369 const RtpHeaderExtensions& extensions) override;
370 void AddRtpHeaderExtension(const webrtc::RtpExtension& ext) override;
371 void AddRtpHeaderExtension(const cricket::RtpHeaderExtension& ext) override;
372 void ClearRtpHeaderExtensions() override;
373 bool rtp_header_extensions_set() const override;
374 const StreamParamsVec& streams() const override;
375 StreamParamsVec& mutable_streams() override;
376 void AddStream(const StreamParams& stream) override;
377 void SetCnameIfEmpty(const std::string& cname) override;
378 uint32_t first_ssrc() const override;
379 bool has_ssrcs() const override;
380 void set_conference_mode(bool enable) override;
381 bool conference_mode() const override;
382 void set_connection_address(const rtc::SocketAddress& address) override;
383 const rtc::SocketAddress& connection_address() const override;
384 void set_extmap_allow_mixed_enum(ExtmapAllowMixed) override;
385 ExtmapAllowMixed extmap_allow_mixed_enum() const override;
386 bool HasSimulcast() const override;
387 SimulcastDescription& simulcast_description() override;
388 const SimulcastDescription& simulcast_description() const override;
389 void set_simulcast_description(
390 const SimulcastDescription& simulcast) override;
391
392 // Override all methods defined in MediaContentDescriptionImpl.
393 const std::vector<CodecType>& codecs() const override;
394 void set_codecs(const std::vector<CodecType>& codecs) override;
395 bool HasCodec(int id) override;
396 void AddCodec(const CodecType& codec) override;
397 void AddOrReplaceCodec(const CodecType& codec) override;
398 void AddCodecs(const std::vector<CodecType>& codec) override;
399
400 private:
401 typedef MediaContentDescriptionImpl<DataCodec> Super;
402 // Friend classes are allowed to create proxies for themselves.
403 friend class RtpDataContentDescription; // for constructors
404 friend class SctpDataContentDescription;
405 friend class SessionDescription; // for Unshim()
406 // Copy constructor. A copy results in an object that owns its
407 // real description, which is a copy of the original description
408 // (whether that was owned or not).
409 explicit DataContentDescription(const DataContentDescription* o);
410
411 explicit DataContentDescription(RtpDataContentDescription*);
412 explicit DataContentDescription(SctpDataContentDescription*);
413
414 // Exposed for internal use - new clients should not use this class.
415 RtpDataContentDescription* as_rtp_data() override;
416 SctpDataContentDescription* as_sctp() override;
417
418 // Create a shimmed object, owned by the shim.
419 void CreateShimTarget(bool is_sctp);
420
421 // Return the shimmed object, passing ownership if owned, and set
422 // |should_delete| to true if it was the owner. If |should_delete|
423 // is true on return, the caller should immediately delete the
424 // DataContentDescription object.
425 MediaContentDescription* Unshim(bool* should_delete);
426
427 // Returns whether SCTP is in use. False when it's not decided.
428 bool IsSctp() const;
429 // Check function for use when caller obviously assumes RTP.
430 void EnsureIsRtp();
431
432 MediaContentDescription* real_description_ = nullptr;
433 std::unique_ptr<MediaContentDescription> owned_description_;
434};
435
436class RtpDataContentDescription
437 : public MediaContentDescriptionImpl<RtpDataCodec> {
438 public:
439 RtpDataContentDescription() {}
440 RtpDataContentDescription(const RtpDataContentDescription& o)
441 : MediaContentDescriptionImpl<RtpDataCodec>(o), shim_(nullptr) {}
442 RtpDataContentDescription& operator=(const RtpDataContentDescription& o) {
443 this->MediaContentDescriptionImpl<RtpDataCodec>::operator=(o);
444 // Do not copy the shim.
445 return *this;
446 }
447
448 RtpDataContentDescription* Copy() const override {
449 return new RtpDataContentDescription(*this);
450 }
451 MediaType type() const override { return MEDIA_TYPE_DATA; }
452 RtpDataContentDescription* as_rtp_data() override { return this; }
453 const RtpDataContentDescription* as_rtp_data() const override { return this; }
454 // Shim support
455 DataContentDescription* as_data() override;
456 const DataContentDescription* as_data() const override;
457
458 private:
459 std::unique_ptr<DataContentDescription> shim_;
460};
461
462class SctpDataContentDescription : public MediaContentDescription {
463 public:
464 SctpDataContentDescription() {}
465 SctpDataContentDescription(const SctpDataContentDescription& o)
466 : MediaContentDescription(o),
467 use_sctpmap_(o.use_sctpmap_),
468 port_(o.port_),
469 max_message_size_(o.max_message_size_),
470 shim_(nullptr) {}
471 SctpDataContentDescription* Copy() const override {
472 return new SctpDataContentDescription(*this);
473 }
474 MediaType type() const override { return MEDIA_TYPE_DATA; }
475 SctpDataContentDescription* as_sctp() override { return this; }
476 const SctpDataContentDescription* as_sctp() const override { return this; }
477 // Shim support
478 DataContentDescription* as_data() override;
479 const DataContentDescription* as_data() const override;
480
481 bool has_codecs() const override { return false; }
482 void set_protocol(const std::string& protocol) override {
483 RTC_DCHECK(IsSctpProtocol(protocol));
484 protocol_ = protocol;
485 }
Steve Antonafd8e8c2017-12-19 16:35:35 -0800486
487 bool use_sctpmap() const { return use_sctpmap_; }
488 void set_use_sctpmap(bool enable) { use_sctpmap_ = enable; }
Harald Alvestrand14b27582019-05-04 11:37:04 +0200489 int port() const { return port_; }
490 void set_port(int port) { port_ = port; }
491 int max_message_size() const { return max_message_size_; }
492 void set_max_message_size(int max_message_size) {
493 max_message_size_ = max_message_size;
494 }
Steve Antonafd8e8c2017-12-19 16:35:35 -0800495
496 private:
Harald Alvestrand14b27582019-05-04 11:37:04 +0200497 bool use_sctpmap_ = true; // Note: "true" is no longer conformant.
498 // Defaults should be constants imported from SCTP. Quick hack.
499 int port_ = 5000;
500 int max_message_size_ = 256 * 1024;
501 std::unique_ptr<DataContentDescription> shim_;
Steve Antonafd8e8c2017-12-19 16:35:35 -0800502};
503
Steve Anton5adfafd2017-12-20 16:34:00 -0800504// Protocol used for encoding media. This is the "top level" protocol that may
505// be wrapped by zero or many transport protocols (UDP, ICE, etc.).
506enum class MediaProtocolType {
507 kRtp, // Section will use the RTP protocol (e.g., for audio or video).
508 // https://tools.ietf.org/html/rfc3550
509 kSctp // Section will use the SCTP protocol (e.g., for a data channel).
510 // https://tools.ietf.org/html/rfc4960
511};
512
513// TODO(bugs.webrtc.org/8620): Remove once downstream projects have updated.
514constexpr MediaProtocolType NS_JINGLE_RTP = MediaProtocolType::kRtp;
515constexpr MediaProtocolType NS_JINGLE_DRAFT_SCTP = MediaProtocolType::kSctp;
516
517// Represents a session description section. Most information about the section
518// is stored in the description, which is a subclass of MediaContentDescription.
Steve Anton4ab68ee2017-12-19 14:26:11 -0800519struct ContentInfo {
Steve Antonb1c1de12017-12-21 15:14:30 -0800520 friend class SessionDescription;
521
Steve Anton5adfafd2017-12-20 16:34:00 -0800522 explicit ContentInfo(MediaProtocolType type) : type(type) {}
523
524 // Alias for |name|.
525 std::string mid() const { return name; }
526 void set_mid(const std::string& mid) { this->name = mid; }
527
528 // Alias for |description|.
529 MediaContentDescription* media_description() { return description; }
530 const MediaContentDescription* media_description() const {
531 return description;
532 }
Steve Anton81712112018-01-05 11:27:54 -0800533 void set_media_description(MediaContentDescription* desc) {
534 description = desc;
Steve Anton5adfafd2017-12-20 16:34:00 -0800535 }
536
Steve Anton81712112018-01-05 11:27:54 -0800537 // TODO(bugs.webrtc.org/8620): Rename this to mid.
Steve Anton4ab68ee2017-12-19 14:26:11 -0800538 std::string name;
Steve Anton5adfafd2017-12-20 16:34:00 -0800539 MediaProtocolType type;
Steve Anton4ab68ee2017-12-19 14:26:11 -0800540 bool rejected = false;
541 bool bundle_only = false;
Steve Anton81712112018-01-05 11:27:54 -0800542 // TODO(bugs.webrtc.org/8620): Switch to the getter and setter, and make this
543 // private.
Steve Antonb1c1de12017-12-21 15:14:30 -0800544 MediaContentDescription* description = nullptr;
Steve Anton4ab68ee2017-12-19 14:26:11 -0800545};
546
547typedef std::vector<std::string> ContentNames;
548
549// This class provides a mechanism to aggregate different media contents into a
550// group. This group can also be shared with the peers in a pre-defined format.
551// GroupInfo should be populated only with the |content_name| of the
552// MediaDescription.
553class ContentGroup {
554 public:
555 explicit ContentGroup(const std::string& semantics);
556 ContentGroup(const ContentGroup&);
557 ContentGroup(ContentGroup&&);
558 ContentGroup& operator=(const ContentGroup&);
559 ContentGroup& operator=(ContentGroup&&);
560 ~ContentGroup();
561
562 const std::string& semantics() const { return semantics_; }
563 const ContentNames& content_names() const { return content_names_; }
564
565 const std::string* FirstContentName() const;
566 bool HasContentName(const std::string& content_name) const;
567 void AddContentName(const std::string& content_name);
568 bool RemoveContentName(const std::string& content_name);
569
570 private:
571 std::string semantics_;
572 ContentNames content_names_;
573};
574
575typedef std::vector<ContentInfo> ContentInfos;
576typedef std::vector<ContentGroup> ContentGroups;
577
578const ContentInfo* FindContentInfoByName(const ContentInfos& contents,
579 const std::string& name);
580const ContentInfo* FindContentInfoByType(const ContentInfos& contents,
581 const std::string& type);
582
Steve Antone831b8c2018-02-01 12:22:16 -0800583// Determines how the MSID will be signaled in the SDP. These can be used as
584// flags to indicate both or none.
585enum MsidSignaling {
586 // Signal MSID with one a=msid line in the media section.
587 kMsidSignalingMediaSection = 0x1,
588 // Signal MSID with a=ssrc: msid lines in the media section.
589 kMsidSignalingSsrcAttribute = 0x2
590};
591
Steve Anton4ab68ee2017-12-19 14:26:11 -0800592// Describes a collection of contents, each with its own name and
593// type. Analogous to a <jingle> or <session> stanza. Assumes that
594// contents are unique be name, but doesn't enforce that.
595class SessionDescription {
596 public:
597 SessionDescription();
Steve Anton4ab68ee2017-12-19 14:26:11 -0800598 ~SessionDescription();
599
Harald Alvestrand4d7160e2019-04-12 07:01:29 +0200600 std::unique_ptr<SessionDescription> Clone() const;
601 // Older API - to be deprecated. Still expects caller to take ownership.
Steve Anton4ab68ee2017-12-19 14:26:11 -0800602 SessionDescription* Copy() const;
603
Piotr (Peter) Slatala13e570f2019-02-27 11:34:26 -0800604 struct MediaTransportSetting;
605
Steve Anton4ab68ee2017-12-19 14:26:11 -0800606 // Content accessors.
607 const ContentInfos& contents() const { return contents_; }
608 ContentInfos& contents() { return contents_; }
609 const ContentInfo* GetContentByName(const std::string& name) const;
610 ContentInfo* GetContentByName(const std::string& name);
Steve Antonb1c1de12017-12-21 15:14:30 -0800611 const MediaContentDescription* GetContentDescriptionByName(
Steve Anton4ab68ee2017-12-19 14:26:11 -0800612 const std::string& name) const;
Steve Antonb1c1de12017-12-21 15:14:30 -0800613 MediaContentDescription* GetContentDescriptionByName(const std::string& name);
Steve Anton5adfafd2017-12-20 16:34:00 -0800614 const ContentInfo* FirstContentByType(MediaProtocolType type) const;
Steve Anton4ab68ee2017-12-19 14:26:11 -0800615 const ContentInfo* FirstContent() const;
616
617 // Content mutators.
618 // Adds a content to this description. Takes ownership of ContentDescription*.
619 void AddContent(const std::string& name,
Steve Anton5adfafd2017-12-20 16:34:00 -0800620 MediaProtocolType type,
Steve Antonb1c1de12017-12-21 15:14:30 -0800621 MediaContentDescription* description);
Steve Anton4ab68ee2017-12-19 14:26:11 -0800622 void AddContent(const std::string& name,
Steve Anton5adfafd2017-12-20 16:34:00 -0800623 MediaProtocolType type,
Steve Anton4ab68ee2017-12-19 14:26:11 -0800624 bool rejected,
Steve Antonb1c1de12017-12-21 15:14:30 -0800625 MediaContentDescription* description);
Steve Anton4ab68ee2017-12-19 14:26:11 -0800626 void AddContent(const std::string& name,
Steve Anton5adfafd2017-12-20 16:34:00 -0800627 MediaProtocolType type,
Steve Anton4ab68ee2017-12-19 14:26:11 -0800628 bool rejected,
629 bool bundle_only,
Steve Antonb1c1de12017-12-21 15:14:30 -0800630 MediaContentDescription* description);
Johannes Kron9ac3c912018-10-12 10:54:26 +0200631 void AddContent(ContentInfo* content);
632
Steve Anton4ab68ee2017-12-19 14:26:11 -0800633 bool RemoveContentByName(const std::string& name);
634
635 // Transport accessors.
636 const TransportInfos& transport_infos() const { return transport_infos_; }
637 TransportInfos& transport_infos() { return transport_infos_; }
638 const TransportInfo* GetTransportInfoByName(const std::string& name) const;
639 TransportInfo* GetTransportInfoByName(const std::string& name);
640 const TransportDescription* GetTransportDescriptionByName(
641 const std::string& name) const {
642 const TransportInfo* tinfo = GetTransportInfoByName(name);
643 return tinfo ? &tinfo->description : NULL;
644 }
645
646 // Transport mutators.
647 void set_transport_infos(const TransportInfos& transport_infos) {
648 transport_infos_ = transport_infos;
649 }
650 // Adds a TransportInfo to this description.
Steve Anton06817cd2018-12-18 15:55:30 -0800651 void AddTransportInfo(const TransportInfo& transport_info);
Steve Anton4ab68ee2017-12-19 14:26:11 -0800652 bool RemoveTransportInfoByName(const std::string& name);
653
654 // Group accessors.
655 const ContentGroups& groups() const { return content_groups_; }
656 const ContentGroup* GetGroupByName(const std::string& name) const;
657 bool HasGroup(const std::string& name) const;
658
659 // Group mutators.
660 void AddGroup(const ContentGroup& group) { content_groups_.push_back(group); }
661 // Remove the first group with the same semantics specified by |name|.
662 void RemoveGroupByName(const std::string& name);
663
664 // Global attributes.
665 void set_msid_supported(bool supported) { msid_supported_ = supported; }
666 bool msid_supported() const { return msid_supported_; }
667
Steve Antone831b8c2018-02-01 12:22:16 -0800668 // Determines how the MSIDs were/will be signaled. Flag value composed of
669 // MsidSignaling bits (see enum above).
670 void set_msid_signaling(int msid_signaling) {
671 msid_signaling_ = msid_signaling;
672 }
673 int msid_signaling() const { return msid_signaling_; }
674
Johannes Kron0854eb62018-10-10 22:33:20 +0200675 // Determines if it's allowed to mix one- and two-byte rtp header extensions
676 // within the same rtp stream.
Johannes Kron9581bc42018-10-23 10:17:39 +0200677 void set_extmap_allow_mixed(bool supported) {
678 extmap_allow_mixed_ = supported;
679 MediaContentDescription::ExtmapAllowMixed media_level_setting =
Johannes Kron0854eb62018-10-10 22:33:20 +0200680 supported ? MediaContentDescription::kSession
681 : MediaContentDescription::kNo;
682 for (auto& content : contents_) {
Johannes Kron9ac3c912018-10-12 10:54:26 +0200683 // Do not set to kNo if the current setting is kMedia.
Johannes Kron9581bc42018-10-23 10:17:39 +0200684 if (supported || content.media_description()->extmap_allow_mixed_enum() !=
685 MediaContentDescription::kMedia) {
686 content.media_description()->set_extmap_allow_mixed_enum(
Johannes Kron9ac3c912018-10-12 10:54:26 +0200687 media_level_setting);
688 }
Johannes Kron0854eb62018-10-10 22:33:20 +0200689 }
690 }
Johannes Kron9581bc42018-10-23 10:17:39 +0200691 bool extmap_allow_mixed() const { return extmap_allow_mixed_; }
Johannes Kron0854eb62018-10-10 22:33:20 +0200692
Piotr (Peter) Slatala13e570f2019-02-27 11:34:26 -0800693 // Adds the media transport setting.
694 // Media transport name uniquely identifies the type of media transport.
695 // The name cannot be empty, or repeated in the previously added transport
696 // settings.
697 void AddMediaTransportSetting(const std::string& media_transport_name,
698 const std::string& media_transport_setting) {
699 RTC_DCHECK(!media_transport_name.empty());
700 for (const auto& setting : media_transport_settings_) {
701 RTC_DCHECK(media_transport_name != setting.transport_name)
702 << "MediaTransportSetting was already registered, transport_name="
703 << setting.transport_name;
704 }
705 media_transport_settings_.push_back(
706 {media_transport_name, media_transport_setting});
707 }
708
709 // Gets the media transport settings, in order of preference.
710 const std::vector<MediaTransportSetting>& MediaTransportSettings() const {
711 return media_transport_settings_;
712 }
713
714 struct MediaTransportSetting {
715 std::string transport_name;
716 std::string transport_setting;
717 };
718
Steve Anton4ab68ee2017-12-19 14:26:11 -0800719 private:
720 SessionDescription(const SessionDescription&);
721
722 ContentInfos contents_;
723 TransportInfos transport_infos_;
724 ContentGroups content_groups_;
725 bool msid_supported_ = true;
Steve Antone831b8c2018-02-01 12:22:16 -0800726 // Default to what Plan B would do.
727 // TODO(bugs.webrtc.org/8530): Change default to kMsidSignalingMediaSection.
728 int msid_signaling_ = kMsidSignalingSsrcAttribute;
Johannes Kron89f874e2018-11-12 10:25:48 +0100729 // TODO(webrtc:9985): Activate mixed one- and two-byte header extension in
730 // offer at session level. It's currently not included in offer by default
731 // because clients prior to https://bugs.webrtc.org/9712 cannot parse this
732 // correctly. If it's included in offer to us we will respond that we support
733 // it.
Johannes Kron9581bc42018-10-23 10:17:39 +0200734 bool extmap_allow_mixed_ = false;
Piotr (Peter) Slatala13e570f2019-02-27 11:34:26 -0800735
736 std::vector<MediaTransportSetting> media_transport_settings_;
Steve Anton4ab68ee2017-12-19 14:26:11 -0800737};
738
Steve Antonb1c1de12017-12-21 15:14:30 -0800739// Indicates whether a session description was sent by the local client or
740// received from the remote client.
Steve Anton4ab68ee2017-12-19 14:26:11 -0800741enum ContentSource { CS_LOCAL, CS_REMOTE };
742
743} // namespace cricket
744
Steve Anton10542f22019-01-11 09:11:00 -0800745#endif // PC_SESSION_DESCRIPTION_H_