blob: 02205c68af84b4c6020bb3bef3db480a6a9f0680 [file] [log] [blame]
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001/*
kjellander65c7f672016-02-12 00:05:01 -08002 * Copyright 2004 The WebRTC project authors. All Rights Reserved.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003 *
kjellander65c7f672016-02-12 00:05:01 -08004 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00009 */
10
11// Types and classes used in media session descriptions.
12
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020013#ifndef PC_MEDIASESSION_H_
14#define PC_MEDIASESSION_H_
henrike@webrtc.org28e20752013-07-10 00:45:36 +000015
buildbot@webrtc.orga09a9992014-08-13 17:26:08 +000016#include <algorithm>
deadbeef0ed85b22016-02-23 17:24:52 -080017#include <map>
henrike@webrtc.org28e20752013-07-10 00:45:36 +000018#include <string>
19#include <vector>
henrike@webrtc.org28e20752013-07-10 00:45:36 +000020
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020021#include "api/mediatypes.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020022#include "media/base/mediaconstants.h"
23#include "media/base/mediaengine.h" // For DataChannelType
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020024#include "p2p/base/transportdescriptionfactory.h"
Taylor Brandstetter6e2e7ce2017-12-19 10:26:23 -080025#include "pc/jseptransport.h"
Steve Anton4ab68ee2017-12-19 14:26:11 -080026#include "pc/sessiondescription.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000027
28namespace cricket {
29
30class ChannelManager;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000031
zhihuang8f65cdf2016-05-06 18:40:30 -070032// Default RTCP CNAME for unit tests.
33const char kDefaultRtcpCname[] = "DefaultRtcpCname";
34
zhihuang1c378ed2017-08-17 14:10:50 -070035// Options for an RtpSender contained with an media description/"m=" section.
36struct SenderOptions {
37 std::string track_id;
Steve Anton8ffb9c32017-08-31 15:45:38 -070038 // TODO(steveanton): As part of work towards Unified Plan, this has been
39 // changed to be a vector. But for now this can only have exactly one.
40 std::vector<std::string> stream_ids;
zhihuang1c378ed2017-08-17 14:10:50 -070041 int num_sim_layers;
42};
jiayl@webrtc.org742922b2014-10-07 21:32:43 +000043
zhihuang1c378ed2017-08-17 14:10:50 -070044// Options for an individual media description/"m=" section.
45struct MediaDescriptionOptions {
46 MediaDescriptionOptions(MediaType type,
47 const std::string& mid,
Steve Anton1d03a752017-11-27 14:30:09 -080048 webrtc::RtpTransceiverDirection direction,
zhihuang1c378ed2017-08-17 14:10:50 -070049 bool stopped)
50 : type(type), mid(mid), direction(direction), stopped(stopped) {}
zhihuanga77e6bb2017-08-14 18:17:48 -070051
zhihuang1c378ed2017-08-17 14:10:50 -070052 // TODO(deadbeef): When we don't support Plan B, there will only be one
53 // sender per media description and this can be simplified.
54 void AddAudioSender(const std::string& track_id,
Steve Anton8ffb9c32017-08-31 15:45:38 -070055 const std::vector<std::string>& stream_ids);
zhihuang1c378ed2017-08-17 14:10:50 -070056 void AddVideoSender(const std::string& track_id,
Steve Anton8ffb9c32017-08-31 15:45:38 -070057 const std::vector<std::string>& stream_ids,
olka3c747662017-08-17 06:50:32 -070058 int num_sim_layers);
zhihuanga77e6bb2017-08-14 18:17:48 -070059
zhihuang1c378ed2017-08-17 14:10:50 -070060 // Internally just uses sender_options.
61 void AddRtpDataChannel(const std::string& track_id,
62 const std::string& stream_id);
olka3c747662017-08-17 06:50:32 -070063
zhihuang1c378ed2017-08-17 14:10:50 -070064 MediaType type;
65 std::string mid;
Steve Anton1d03a752017-11-27 14:30:09 -080066 webrtc::RtpTransceiverDirection direction;
zhihuang1c378ed2017-08-17 14:10:50 -070067 bool stopped;
68 TransportOptions transport_options;
69 // Note: There's no equivalent "RtpReceiverOptions" because only send
70 // stream information goes in the local descriptions.
71 std::vector<SenderOptions> sender_options;
72
73 private:
74 // Doesn't DCHECK on |type|.
75 void AddSenderInternal(const std::string& track_id,
Steve Anton8ffb9c32017-08-31 15:45:38 -070076 const std::vector<std::string>& stream_ids,
olka3c747662017-08-17 06:50:32 -070077 int num_sim_layers);
zhihuang1c378ed2017-08-17 14:10:50 -070078};
olka3c747662017-08-17 06:50:32 -070079
zhihuang1c378ed2017-08-17 14:10:50 -070080// Provides a mechanism for describing how m= sections should be generated.
81// The m= section with index X will use media_description_options[X]. There
82// must be an option for each existing section if creating an answer, or a
83// subsequent offer.
84struct MediaSessionOptions {
85 MediaSessionOptions() {}
olka3c747662017-08-17 06:50:32 -070086
zhihuang1c378ed2017-08-17 14:10:50 -070087 bool has_audio() const { return HasMediaDescription(MEDIA_TYPE_AUDIO); }
88 bool has_video() const { return HasMediaDescription(MEDIA_TYPE_VIDEO); }
89 bool has_data() const { return HasMediaDescription(MEDIA_TYPE_DATA); }
90
91 bool HasMediaDescription(MediaType type) const;
92
93 DataChannelType data_channel_type = DCT_NONE;
zhihuang1c378ed2017-08-17 14:10:50 -070094 bool vad_enabled = true; // When disabled, removes all CN codecs from SDP.
95 bool rtcp_mux_enabled = true;
96 bool bundle_enabled = false;
97 std::string rtcp_cname = kDefaultRtcpCname;
jbauchcb560652016-08-04 05:20:32 -070098 rtc::CryptoOptions crypto_options;
zhihuang1c378ed2017-08-17 14:10:50 -070099 // List of media description options in the same order that the media
100 // descriptions will be generated.
101 std::vector<MediaDescriptionOptions> media_description_options;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000102};
103
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000104// Creates media session descriptions according to the supplied codecs and
105// other fields, as well as the supplied per-call options.
106// When creating answers, performs the appropriate negotiation
107// of the various fields to determine the proper result.
108class MediaSessionDescriptionFactory {
109 public:
110 // Default ctor; use methods below to set configuration.
111 // The TransportDescriptionFactory is not owned by MediaSessionDescFactory,
112 // so it must be kept alive by the user of this class.
113 explicit MediaSessionDescriptionFactory(
114 const TransportDescriptionFactory* factory);
115 // This helper automatically sets up the factory to get its configuration
116 // from the specified ChannelManager.
117 MediaSessionDescriptionFactory(ChannelManager* cmanager,
118 const TransportDescriptionFactory* factory);
119
ossudedfd282016-06-14 07:12:39 -0700120 const AudioCodecs& audio_sendrecv_codecs() const;
ossu075af922016-06-14 03:29:38 -0700121 const AudioCodecs& audio_send_codecs() const;
122 const AudioCodecs& audio_recv_codecs() const;
123 void set_audio_codecs(const AudioCodecs& send_codecs,
124 const AudioCodecs& recv_codecs);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000125 void set_audio_rtp_header_extensions(const RtpHeaderExtensions& extensions) {
126 audio_rtp_extensions_ = extensions;
127 }
128 const RtpHeaderExtensions& audio_rtp_header_extensions() const {
129 return audio_rtp_extensions_;
130 }
131 const VideoCodecs& video_codecs() const { return video_codecs_; }
132 void set_video_codecs(const VideoCodecs& codecs) { video_codecs_ = codecs; }
133 void set_video_rtp_header_extensions(const RtpHeaderExtensions& extensions) {
134 video_rtp_extensions_ = extensions;
135 }
136 const RtpHeaderExtensions& video_rtp_header_extensions() const {
137 return video_rtp_extensions_;
138 }
139 const DataCodecs& data_codecs() const { return data_codecs_; }
140 void set_data_codecs(const DataCodecs& codecs) { data_codecs_ = codecs; }
141 SecurePolicy secure() const { return secure_; }
142 void set_secure(SecurePolicy s) { secure_ = s; }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000143
jbauch5869f502017-06-29 12:31:36 -0700144 void set_enable_encrypted_rtp_header_extensions(bool enable) {
145 enable_encrypted_rtp_header_extensions_ = enable;
146 }
147
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000148 SessionDescription* CreateOffer(
149 const MediaSessionOptions& options,
150 const SessionDescription* current_description) const;
151 SessionDescription* CreateAnswer(
zstein4b2e0822017-02-17 19:48:38 -0800152 const SessionDescription* offer,
153 const MediaSessionOptions& options,
154 const SessionDescription* current_description) const;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000155
156 private:
ossu075af922016-06-14 03:29:38 -0700157 const AudioCodecs& GetAudioCodecsForOffer(
Steve Anton1d03a752017-11-27 14:30:09 -0800158 const webrtc::RtpTransceiverDirection& direction) const;
ossu075af922016-06-14 03:29:38 -0700159 const AudioCodecs& GetAudioCodecsForAnswer(
Steve Anton1d03a752017-11-27 14:30:09 -0800160 const webrtc::RtpTransceiverDirection& offer,
161 const webrtc::RtpTransceiverDirection& answer) const;
zhihuang1c378ed2017-08-17 14:10:50 -0700162 void GetCodecsForOffer(const SessionDescription* current_description,
163 AudioCodecs* audio_codecs,
164 VideoCodecs* video_codecs,
165 DataCodecs* data_codecs) const;
166 void GetCodecsForAnswer(const SessionDescription* current_description,
167 const SessionDescription* remote_offer,
168 AudioCodecs* audio_codecs,
169 VideoCodecs* video_codecs,
170 DataCodecs* data_codecs) const;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000171 void GetRtpHdrExtsToOffer(const SessionDescription* current_description,
172 RtpHeaderExtensions* audio_extensions,
173 RtpHeaderExtensions* video_extensions) const;
174 bool AddTransportOffer(
175 const std::string& content_name,
176 const TransportOptions& transport_options,
177 const SessionDescription* current_desc,
178 SessionDescription* offer) const;
179
180 TransportDescription* CreateTransportAnswer(
181 const std::string& content_name,
182 const SessionDescription* offer_desc,
183 const TransportOptions& transport_options,
deadbeefb7892532017-02-22 19:35:18 -0800184 const SessionDescription* current_desc,
185 bool require_transport_attributes) const;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000186
187 bool AddTransportAnswer(
188 const std::string& content_name,
189 const TransportDescription& transport_desc,
190 SessionDescription* answer_desc) const;
191
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +0000192 // Helpers for adding media contents to the SessionDescription. Returns true
193 // it succeeds or the media content is not needed, or false if there is any
194 // error.
195
196 bool AddAudioContentForOffer(
zhihuang1c378ed2017-08-17 14:10:50 -0700197 const MediaDescriptionOptions& media_description_options,
198 const MediaSessionOptions& session_options,
199 const ContentInfo* current_content,
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +0000200 const SessionDescription* current_description,
201 const RtpHeaderExtensions& audio_rtp_extensions,
202 const AudioCodecs& audio_codecs,
203 StreamParamsVec* current_streams,
204 SessionDescription* desc) const;
205
206 bool AddVideoContentForOffer(
zhihuang1c378ed2017-08-17 14:10:50 -0700207 const MediaDescriptionOptions& media_description_options,
208 const MediaSessionOptions& session_options,
209 const ContentInfo* current_content,
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +0000210 const SessionDescription* current_description,
211 const RtpHeaderExtensions& video_rtp_extensions,
212 const VideoCodecs& video_codecs,
213 StreamParamsVec* current_streams,
214 SessionDescription* desc) const;
215
216 bool AddDataContentForOffer(
zhihuang1c378ed2017-08-17 14:10:50 -0700217 const MediaDescriptionOptions& media_description_options,
218 const MediaSessionOptions& session_options,
219 const ContentInfo* current_content,
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +0000220 const SessionDescription* current_description,
zhihuang1c378ed2017-08-17 14:10:50 -0700221 const DataCodecs& data_codecs,
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +0000222 StreamParamsVec* current_streams,
223 SessionDescription* desc) const;
224
zhihuang1c378ed2017-08-17 14:10:50 -0700225 bool AddAudioContentForAnswer(
226 const MediaDescriptionOptions& media_description_options,
227 const MediaSessionOptions& session_options,
228 const ContentInfo* offer_content,
229 const SessionDescription* offer_description,
230 const ContentInfo* current_content,
231 const SessionDescription* current_description,
232 const TransportInfo* bundle_transport,
233 const AudioCodecs& audio_codecs,
234 StreamParamsVec* current_streams,
235 SessionDescription* answer) const;
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +0000236
zhihuang1c378ed2017-08-17 14:10:50 -0700237 bool AddVideoContentForAnswer(
238 const MediaDescriptionOptions& media_description_options,
239 const MediaSessionOptions& session_options,
240 const ContentInfo* offer_content,
241 const SessionDescription* offer_description,
242 const ContentInfo* current_content,
243 const SessionDescription* current_description,
244 const TransportInfo* bundle_transport,
245 const VideoCodecs& video_codecs,
246 StreamParamsVec* current_streams,
247 SessionDescription* answer) const;
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +0000248
zhihuang1c378ed2017-08-17 14:10:50 -0700249 bool AddDataContentForAnswer(
250 const MediaDescriptionOptions& media_description_options,
251 const MediaSessionOptions& session_options,
252 const ContentInfo* offer_content,
253 const SessionDescription* offer_description,
254 const ContentInfo* current_content,
255 const SessionDescription* current_description,
256 const TransportInfo* bundle_transport,
257 const DataCodecs& data_codecs,
258 StreamParamsVec* current_streams,
259 SessionDescription* answer) const;
260
261 void ComputeAudioCodecsIntersectionAndUnion();
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +0000262
ossu075af922016-06-14 03:29:38 -0700263 AudioCodecs audio_send_codecs_;
264 AudioCodecs audio_recv_codecs_;
zhihuang1c378ed2017-08-17 14:10:50 -0700265 // Intersection of send and recv.
ossu075af922016-06-14 03:29:38 -0700266 AudioCodecs audio_sendrecv_codecs_;
zhihuang1c378ed2017-08-17 14:10:50 -0700267 // Union of send and recv.
268 AudioCodecs all_audio_codecs_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000269 RtpHeaderExtensions audio_rtp_extensions_;
270 VideoCodecs video_codecs_;
271 RtpHeaderExtensions video_rtp_extensions_;
272 DataCodecs data_codecs_;
jbauch5869f502017-06-29 12:31:36 -0700273 bool enable_encrypted_rtp_header_extensions_ = false;
zhihuang1c378ed2017-08-17 14:10:50 -0700274 // TODO(zhihuang): Rename secure_ to sdec_policy_; rename the related getter
275 // and setter.
276 SecurePolicy secure_ = SEC_DISABLED;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000277 const TransportDescriptionFactory* transport_desc_factory_;
278};
279
280// Convenience functions.
281bool IsMediaContent(const ContentInfo* content);
282bool IsAudioContent(const ContentInfo* content);
283bool IsVideoContent(const ContentInfo* content);
284bool IsDataContent(const ContentInfo* content);
deadbeef0ed85b22016-02-23 17:24:52 -0800285const ContentInfo* GetFirstMediaContent(const ContentInfos& contents,
286 MediaType media_type);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000287const ContentInfo* GetFirstAudioContent(const ContentInfos& contents);
288const ContentInfo* GetFirstVideoContent(const ContentInfos& contents);
289const ContentInfo* GetFirstDataContent(const ContentInfos& contents);
Steve Antonad7bffc2018-01-22 10:21:56 -0800290const ContentInfo* GetFirstMediaContent(const SessionDescription* sdesc,
291 MediaType media_type);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000292const ContentInfo* GetFirstAudioContent(const SessionDescription* sdesc);
293const ContentInfo* GetFirstVideoContent(const SessionDescription* sdesc);
294const ContentInfo* GetFirstDataContent(const SessionDescription* sdesc);
295const AudioContentDescription* GetFirstAudioContentDescription(
296 const SessionDescription* sdesc);
297const VideoContentDescription* GetFirstVideoContentDescription(
298 const SessionDescription* sdesc);
299const DataContentDescription* GetFirstDataContentDescription(
300 const SessionDescription* sdesc);
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -0700301// Non-const versions of the above functions.
302// Useful when modifying an existing description.
Steve Anton36b29d12017-10-30 09:57:42 -0700303ContentInfo* GetFirstMediaContent(ContentInfos* contents, MediaType media_type);
304ContentInfo* GetFirstAudioContent(ContentInfos* contents);
305ContentInfo* GetFirstVideoContent(ContentInfos* contents);
306ContentInfo* GetFirstDataContent(ContentInfos* contents);
Steve Antonad7bffc2018-01-22 10:21:56 -0800307ContentInfo* GetFirstMediaContent(SessionDescription* sdesc,
308 MediaType media_type);
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -0700309ContentInfo* GetFirstAudioContent(SessionDescription* sdesc);
310ContentInfo* GetFirstVideoContent(SessionDescription* sdesc);
311ContentInfo* GetFirstDataContent(SessionDescription* sdesc);
312AudioContentDescription* GetFirstAudioContentDescription(
313 SessionDescription* sdesc);
314VideoContentDescription* GetFirstVideoContentDescription(
315 SessionDescription* sdesc);
316DataContentDescription* GetFirstDataContentDescription(
317 SessionDescription* sdesc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000318
deadbeef7914b8c2017-04-21 03:23:33 -0700319// Helper functions to return crypto suites used for SDES.
320void GetSupportedAudioSdesCryptoSuites(const rtc::CryptoOptions& crypto_options,
321 std::vector<int>* crypto_suites);
322void GetSupportedVideoSdesCryptoSuites(const rtc::CryptoOptions& crypto_options,
323 std::vector<int>* crypto_suites);
324void GetSupportedDataSdesCryptoSuites(const rtc::CryptoOptions& crypto_options,
325 std::vector<int>* crypto_suites);
326void GetSupportedAudioSdesCryptoSuiteNames(
327 const rtc::CryptoOptions& crypto_options,
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -0800328 std::vector<std::string>* crypto_suite_names);
deadbeef7914b8c2017-04-21 03:23:33 -0700329void GetSupportedVideoSdesCryptoSuiteNames(
330 const rtc::CryptoOptions& crypto_options,
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -0800331 std::vector<std::string>* crypto_suite_names);
deadbeef7914b8c2017-04-21 03:23:33 -0700332void GetSupportedDataSdesCryptoSuiteNames(
333 const rtc::CryptoOptions& crypto_options,
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -0800334 std::vector<std::string>* crypto_suite_names);
335
Steve Antonfa2260d2017-12-28 16:38:23 -0800336// Returns true if the given media section protocol indicates use of RTP.
337bool IsRtpProtocol(const std::string& protocol);
338
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000339} // namespace cricket
340
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200341#endif // PC_MEDIASESSION_H_