xref: /aosp_15_r20/external/webrtc/pc/media_session.h (revision d9f758449e529ab9291ac668be2861e7a55c2422)
1 /*
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 
11 // Types and classes used in media session descriptions.
12 
13 #ifndef PC_MEDIA_SESSION_H_
14 #define PC_MEDIA_SESSION_H_
15 
16 #include <map>
17 #include <memory>
18 #include <string>
19 #include <vector>
20 
21 #include "api/crypto/crypto_options.h"
22 #include "api/field_trials_view.h"
23 #include "api/media_types.h"
24 #include "api/rtp_parameters.h"
25 #include "api/rtp_transceiver_direction.h"
26 #include "media/base/media_constants.h"
27 #include "media/base/rid_description.h"
28 #include "media/base/stream_params.h"
29 #include "p2p/base/ice_credentials_iterator.h"
30 #include "p2p/base/transport_description.h"
31 #include "p2p/base/transport_description_factory.h"
32 #include "p2p/base/transport_info.h"
33 #include "pc/jsep_transport.h"
34 #include "pc/media_protocol_names.h"
35 #include "pc/session_description.h"
36 #include "pc/simulcast_description.h"
37 #include "rtc_base/memory/always_valid_pointer.h"
38 #include "rtc_base/unique_id_generator.h"
39 
40 namespace webrtc {
41 
42 // Forward declaration due to circular dependecy.
43 class ConnectionContext;
44 
45 }  // namespace webrtc
46 
47 namespace cricket {
48 
49 class MediaEngineInterface;
50 
51 // Default RTCP CNAME for unit tests.
52 const char kDefaultRtcpCname[] = "DefaultRtcpCname";
53 
54 // Options for an RtpSender contained with an media description/"m=" section.
55 // Note: Spec-compliant Simulcast and legacy simulcast are mutually exclusive.
56 struct SenderOptions {
57   std::string track_id;
58   std::vector<std::string> stream_ids;
59   // Use RIDs and Simulcast Layers to indicate spec-compliant Simulcast.
60   std::vector<RidDescription> rids;
61   SimulcastLayerList simulcast_layers;
62   // Use `num_sim_layers` to indicate legacy simulcast.
63   int num_sim_layers;
64 };
65 
66 // Options for an individual media description/"m=" section.
67 struct MediaDescriptionOptions {
MediaDescriptionOptionsMediaDescriptionOptions68   MediaDescriptionOptions(MediaType type,
69                           const std::string& mid,
70                           webrtc::RtpTransceiverDirection direction,
71                           bool stopped)
72       : type(type), mid(mid), direction(direction), stopped(stopped) {}
73 
74   // TODO(deadbeef): When we don't support Plan B, there will only be one
75   // sender per media description and this can be simplified.
76   void AddAudioSender(const std::string& track_id,
77                       const std::vector<std::string>& stream_ids);
78   void AddVideoSender(const std::string& track_id,
79                       const std::vector<std::string>& stream_ids,
80                       const std::vector<RidDescription>& rids,
81                       const SimulcastLayerList& simulcast_layers,
82                       int num_sim_layers);
83 
84   MediaType type;
85   std::string mid;
86   webrtc::RtpTransceiverDirection direction;
87   bool stopped;
88   TransportOptions transport_options;
89   // Note: There's no equivalent "RtpReceiverOptions" because only send
90   // stream information goes in the local descriptions.
91   std::vector<SenderOptions> sender_options;
92   std::vector<webrtc::RtpCodecCapability> codec_preferences;
93   std::vector<webrtc::RtpHeaderExtensionCapability> header_extensions;
94 
95  private:
96   // Doesn't DCHECK on `type`.
97   void AddSenderInternal(const std::string& track_id,
98                          const std::vector<std::string>& stream_ids,
99                          const std::vector<RidDescription>& rids,
100                          const SimulcastLayerList& simulcast_layers,
101                          int num_sim_layers);
102 };
103 
104 // Provides a mechanism for describing how m= sections should be generated.
105 // The m= section with index X will use media_description_options[X]. There
106 // must be an option for each existing section if creating an answer, or a
107 // subsequent offer.
108 struct MediaSessionOptions {
MediaSessionOptionsMediaSessionOptions109   MediaSessionOptions() {}
110 
has_audioMediaSessionOptions111   bool has_audio() const { return HasMediaDescription(MEDIA_TYPE_AUDIO); }
has_videoMediaSessionOptions112   bool has_video() const { return HasMediaDescription(MEDIA_TYPE_VIDEO); }
has_dataMediaSessionOptions113   bool has_data() const { return HasMediaDescription(MEDIA_TYPE_DATA); }
114 
115   bool HasMediaDescription(MediaType type) const;
116 
117   bool vad_enabled = true;  // When disabled, removes all CN codecs from SDP.
118   bool rtcp_mux_enabled = true;
119   bool bundle_enabled = false;
120   bool offer_extmap_allow_mixed = false;
121   bool raw_packetization_for_video = false;
122   std::string rtcp_cname = kDefaultRtcpCname;
123   webrtc::CryptoOptions crypto_options;
124   // List of media description options in the same order that the media
125   // descriptions will be generated.
126   std::vector<MediaDescriptionOptions> media_description_options;
127   std::vector<IceParameters> pooled_ice_credentials;
128 
129   // Use the draft-ietf-mmusic-sctp-sdp-03 obsolete syntax for SCTP
130   // datachannels.
131   // Default is true for backwards compatibility with clients that use
132   // this internal interface.
133   bool use_obsolete_sctp_sdp = true;
134 };
135 
136 // Creates media session descriptions according to the supplied codecs and
137 // other fields, as well as the supplied per-call options.
138 // When creating answers, performs the appropriate negotiation
139 // of the various fields to determine the proper result.
140 class MediaSessionDescriptionFactory {
141  public:
142   // Simple constructor that does not set any configuration for the factory.
143   // When using this constructor, the methods below can be used to set the
144   // configuration.
145   // The TransportDescriptionFactory and the UniqueRandomIdGenerator are not
146   // owned by MediaSessionDescriptionFactory, so they must be kept alive by the
147   // user of this class.
148   MediaSessionDescriptionFactory(const TransportDescriptionFactory* factory,
149                                  rtc::UniqueRandomIdGenerator* ssrc_generator);
150   // This helper automatically sets up the factory to get its configuration
151   // from the specified MediaEngine
152   MediaSessionDescriptionFactory(cricket::MediaEngineInterface* media_engine,
153                                  bool rtx_enabled,
154                                  rtc::UniqueRandomIdGenerator* ssrc_generator,
155                                  const TransportDescriptionFactory* factory);
156 
157   const AudioCodecs& audio_sendrecv_codecs() const;
158   const AudioCodecs& audio_send_codecs() const;
159   const AudioCodecs& audio_recv_codecs() const;
160   void set_audio_codecs(const AudioCodecs& send_codecs,
161                         const AudioCodecs& recv_codecs);
162   const VideoCodecs& video_sendrecv_codecs() const;
163   const VideoCodecs& video_send_codecs() const;
164   const VideoCodecs& video_recv_codecs() const;
165   void set_video_codecs(const VideoCodecs& send_codecs,
166                         const VideoCodecs& recv_codecs);
167   RtpHeaderExtensions filtered_rtp_header_extensions(
168       RtpHeaderExtensions extensions) const;
secure()169   SecurePolicy secure() const { return secure_; }
set_secure(SecurePolicy s)170   void set_secure(SecurePolicy s) { secure_ = s; }
171 
set_enable_encrypted_rtp_header_extensions(bool enable)172   void set_enable_encrypted_rtp_header_extensions(bool enable) {
173     enable_encrypted_rtp_header_extensions_ = enable;
174   }
175 
set_is_unified_plan(bool is_unified_plan)176   void set_is_unified_plan(bool is_unified_plan) {
177     is_unified_plan_ = is_unified_plan;
178   }
179 
180   std::unique_ptr<SessionDescription> CreateOffer(
181       const MediaSessionOptions& options,
182       const SessionDescription* current_description) const;
183   std::unique_ptr<SessionDescription> CreateAnswer(
184       const SessionDescription* offer,
185       const MediaSessionOptions& options,
186       const SessionDescription* current_description) const;
187 
188  private:
189   struct AudioVideoRtpHeaderExtensions {
190     RtpHeaderExtensions audio;
191     RtpHeaderExtensions video;
192   };
193 
194   const AudioCodecs& GetAudioCodecsForOffer(
195       const webrtc::RtpTransceiverDirection& direction) const;
196   const AudioCodecs& GetAudioCodecsForAnswer(
197       const webrtc::RtpTransceiverDirection& offer,
198       const webrtc::RtpTransceiverDirection& answer) const;
199   const VideoCodecs& GetVideoCodecsForOffer(
200       const webrtc::RtpTransceiverDirection& direction) const;
201   const VideoCodecs& GetVideoCodecsForAnswer(
202       const webrtc::RtpTransceiverDirection& offer,
203       const webrtc::RtpTransceiverDirection& answer) const;
204   void GetCodecsForOffer(
205       const std::vector<const ContentInfo*>& current_active_contents,
206       AudioCodecs* audio_codecs,
207       VideoCodecs* video_codecs) const;
208   void GetCodecsForAnswer(
209       const std::vector<const ContentInfo*>& current_active_contents,
210       const SessionDescription& remote_offer,
211       AudioCodecs* audio_codecs,
212       VideoCodecs* video_codecs) const;
213   AudioVideoRtpHeaderExtensions GetOfferedRtpHeaderExtensionsWithIds(
214       const std::vector<const ContentInfo*>& current_active_contents,
215       bool extmap_allow_mixed,
216       const std::vector<MediaDescriptionOptions>& media_description_options)
217       const;
218   bool AddTransportOffer(const std::string& content_name,
219                          const TransportOptions& transport_options,
220                          const SessionDescription* current_desc,
221                          SessionDescription* offer,
222                          IceCredentialsIterator* ice_credentials) const;
223 
224   std::unique_ptr<TransportDescription> CreateTransportAnswer(
225       const std::string& content_name,
226       const SessionDescription* offer_desc,
227       const TransportOptions& transport_options,
228       const SessionDescription* current_desc,
229       bool require_transport_attributes,
230       IceCredentialsIterator* ice_credentials) const;
231 
232   bool AddTransportAnswer(const std::string& content_name,
233                           const TransportDescription& transport_desc,
234                           SessionDescription* answer_desc) const;
235 
236   // Helpers for adding media contents to the SessionDescription. Returns true
237   // it succeeds or the media content is not needed, or false if there is any
238   // error.
239 
240   bool AddAudioContentForOffer(
241       const MediaDescriptionOptions& media_description_options,
242       const MediaSessionOptions& session_options,
243       const ContentInfo* current_content,
244       const SessionDescription* current_description,
245       const RtpHeaderExtensions& audio_rtp_extensions,
246       const AudioCodecs& audio_codecs,
247       StreamParamsVec* current_streams,
248       SessionDescription* desc,
249       IceCredentialsIterator* ice_credentials) const;
250 
251   bool AddVideoContentForOffer(
252       const MediaDescriptionOptions& media_description_options,
253       const MediaSessionOptions& session_options,
254       const ContentInfo* current_content,
255       const SessionDescription* current_description,
256       const RtpHeaderExtensions& video_rtp_extensions,
257       const VideoCodecs& video_codecs,
258       StreamParamsVec* current_streams,
259       SessionDescription* desc,
260       IceCredentialsIterator* ice_credentials) const;
261 
262   bool AddDataContentForOffer(
263       const MediaDescriptionOptions& media_description_options,
264       const MediaSessionOptions& session_options,
265       const ContentInfo* current_content,
266       const SessionDescription* current_description,
267       StreamParamsVec* current_streams,
268       SessionDescription* desc,
269       IceCredentialsIterator* ice_credentials) const;
270 
271   bool AddUnsupportedContentForOffer(
272       const MediaDescriptionOptions& media_description_options,
273       const MediaSessionOptions& session_options,
274       const ContentInfo* current_content,
275       const SessionDescription* current_description,
276       SessionDescription* desc,
277       IceCredentialsIterator* ice_credentials) const;
278 
279   bool AddAudioContentForAnswer(
280       const MediaDescriptionOptions& media_description_options,
281       const MediaSessionOptions& session_options,
282       const ContentInfo* offer_content,
283       const SessionDescription* offer_description,
284       const ContentInfo* current_content,
285       const SessionDescription* current_description,
286       const TransportInfo* bundle_transport,
287       const AudioCodecs& audio_codecs,
288       const RtpHeaderExtensions& default_audio_rtp_header_extensions,
289       StreamParamsVec* current_streams,
290       SessionDescription* answer,
291       IceCredentialsIterator* ice_credentials) const;
292 
293   bool AddVideoContentForAnswer(
294       const MediaDescriptionOptions& media_description_options,
295       const MediaSessionOptions& session_options,
296       const ContentInfo* offer_content,
297       const SessionDescription* offer_description,
298       const ContentInfo* current_content,
299       const SessionDescription* current_description,
300       const TransportInfo* bundle_transport,
301       const VideoCodecs& video_codecs,
302       const RtpHeaderExtensions& default_video_rtp_header_extensions,
303       StreamParamsVec* current_streams,
304       SessionDescription* answer,
305       IceCredentialsIterator* ice_credentials) const;
306 
307   bool AddDataContentForAnswer(
308       const MediaDescriptionOptions& media_description_options,
309       const MediaSessionOptions& session_options,
310       const ContentInfo* offer_content,
311       const SessionDescription* offer_description,
312       const ContentInfo* current_content,
313       const SessionDescription* current_description,
314       const TransportInfo* bundle_transport,
315       StreamParamsVec* current_streams,
316       SessionDescription* answer,
317       IceCredentialsIterator* ice_credentials) const;
318 
319   bool AddUnsupportedContentForAnswer(
320       const MediaDescriptionOptions& media_description_options,
321       const MediaSessionOptions& session_options,
322       const ContentInfo* offer_content,
323       const SessionDescription* offer_description,
324       const ContentInfo* current_content,
325       const SessionDescription* current_description,
326       const TransportInfo* bundle_transport,
327       SessionDescription* answer,
328       IceCredentialsIterator* ice_credentials) const;
329 
330   void ComputeAudioCodecsIntersectionAndUnion();
331 
332   void ComputeVideoCodecsIntersectionAndUnion();
333 
ssrc_generator()334   rtc::UniqueRandomIdGenerator* ssrc_generator() const {
335     return ssrc_generator_.get();
336   }
337 
338   bool is_unified_plan_ = false;
339   AudioCodecs audio_send_codecs_;
340   AudioCodecs audio_recv_codecs_;
341   // Intersection of send and recv.
342   AudioCodecs audio_sendrecv_codecs_;
343   // Union of send and recv.
344   AudioCodecs all_audio_codecs_;
345   VideoCodecs video_send_codecs_;
346   VideoCodecs video_recv_codecs_;
347   // Intersection of send and recv.
348   VideoCodecs video_sendrecv_codecs_;
349   // Union of send and recv.
350   VideoCodecs all_video_codecs_;
351   // This object may or may not be owned by this class.
352   webrtc::AlwaysValidPointer<rtc::UniqueRandomIdGenerator> const
353       ssrc_generator_;
354   bool enable_encrypted_rtp_header_extensions_ = false;
355   // TODO(zhihuang): Rename secure_ to sdec_policy_; rename the related getter
356   // and setter.
357   SecurePolicy secure_ = SEC_DISABLED;
358   const TransportDescriptionFactory* transport_desc_factory_;
359 };
360 
361 // Convenience functions.
362 bool IsMediaContent(const ContentInfo* content);
363 bool IsAudioContent(const ContentInfo* content);
364 bool IsVideoContent(const ContentInfo* content);
365 bool IsDataContent(const ContentInfo* content);
366 bool IsUnsupportedContent(const ContentInfo* content);
367 const ContentInfo* GetFirstMediaContent(const ContentInfos& contents,
368                                         MediaType media_type);
369 const ContentInfo* GetFirstAudioContent(const ContentInfos& contents);
370 const ContentInfo* GetFirstVideoContent(const ContentInfos& contents);
371 const ContentInfo* GetFirstDataContent(const ContentInfos& contents);
372 const ContentInfo* GetFirstMediaContent(const SessionDescription* sdesc,
373                                         MediaType media_type);
374 const ContentInfo* GetFirstAudioContent(const SessionDescription* sdesc);
375 const ContentInfo* GetFirstVideoContent(const SessionDescription* sdesc);
376 const ContentInfo* GetFirstDataContent(const SessionDescription* sdesc);
377 const AudioContentDescription* GetFirstAudioContentDescription(
378     const SessionDescription* sdesc);
379 const VideoContentDescription* GetFirstVideoContentDescription(
380     const SessionDescription* sdesc);
381 const SctpDataContentDescription* GetFirstSctpDataContentDescription(
382     const SessionDescription* sdesc);
383 // Non-const versions of the above functions.
384 // Useful when modifying an existing description.
385 ContentInfo* GetFirstMediaContent(ContentInfos* contents, MediaType media_type);
386 ContentInfo* GetFirstAudioContent(ContentInfos* contents);
387 ContentInfo* GetFirstVideoContent(ContentInfos* contents);
388 ContentInfo* GetFirstDataContent(ContentInfos* contents);
389 ContentInfo* GetFirstMediaContent(SessionDescription* sdesc,
390                                   MediaType media_type);
391 ContentInfo* GetFirstAudioContent(SessionDescription* sdesc);
392 ContentInfo* GetFirstVideoContent(SessionDescription* sdesc);
393 ContentInfo* GetFirstDataContent(SessionDescription* sdesc);
394 AudioContentDescription* GetFirstAudioContentDescription(
395     SessionDescription* sdesc);
396 VideoContentDescription* GetFirstVideoContentDescription(
397     SessionDescription* sdesc);
398 SctpDataContentDescription* GetFirstSctpDataContentDescription(
399     SessionDescription* sdesc);
400 
401 // Helper functions to return crypto suites used for SDES.
402 void GetSupportedAudioSdesCryptoSuites(
403     const webrtc::CryptoOptions& crypto_options,
404     std::vector<int>* crypto_suites);
405 void GetSupportedVideoSdesCryptoSuites(
406     const webrtc::CryptoOptions& crypto_options,
407     std::vector<int>* crypto_suites);
408 void GetSupportedDataSdesCryptoSuites(
409     const webrtc::CryptoOptions& crypto_options,
410     std::vector<int>* crypto_suites);
411 void GetSupportedAudioSdesCryptoSuiteNames(
412     const webrtc::CryptoOptions& crypto_options,
413     std::vector<std::string>* crypto_suite_names);
414 void GetSupportedVideoSdesCryptoSuiteNames(
415     const webrtc::CryptoOptions& crypto_options,
416     std::vector<std::string>* crypto_suite_names);
417 void GetSupportedDataSdesCryptoSuiteNames(
418     const webrtc::CryptoOptions& crypto_options,
419     std::vector<std::string>* crypto_suite_names);
420 
421 }  // namespace cricket
422 
423 #endif  // PC_MEDIA_SESSION_H_
424