xref: /aosp_15_r20/external/webrtc/media/base/media_engine.h (revision d9f758449e529ab9291ac668be2861e7a55c2422)
1*d9f75844SAndroid Build Coastguard Worker /*
2*d9f75844SAndroid Build Coastguard Worker  *  Copyright (c) 2004 The WebRTC project authors. All Rights Reserved.
3*d9f75844SAndroid Build Coastguard Worker  *
4*d9f75844SAndroid Build Coastguard Worker  *  Use of this source code is governed by a BSD-style license
5*d9f75844SAndroid Build Coastguard Worker  *  that can be found in the LICENSE file in the root of the source
6*d9f75844SAndroid Build Coastguard Worker  *  tree. An additional intellectual property rights grant can be found
7*d9f75844SAndroid Build Coastguard Worker  *  in the file PATENTS.  All contributing project authors may
8*d9f75844SAndroid Build Coastguard Worker  *  be found in the AUTHORS file in the root of the source tree.
9*d9f75844SAndroid Build Coastguard Worker  */
10*d9f75844SAndroid Build Coastguard Worker 
11*d9f75844SAndroid Build Coastguard Worker #ifndef MEDIA_BASE_MEDIA_ENGINE_H_
12*d9f75844SAndroid Build Coastguard Worker #define MEDIA_BASE_MEDIA_ENGINE_H_
13*d9f75844SAndroid Build Coastguard Worker 
14*d9f75844SAndroid Build Coastguard Worker #include <memory>
15*d9f75844SAndroid Build Coastguard Worker #include <string>
16*d9f75844SAndroid Build Coastguard Worker #include <vector>
17*d9f75844SAndroid Build Coastguard Worker 
18*d9f75844SAndroid Build Coastguard Worker #include "api/audio_codecs/audio_decoder_factory.h"
19*d9f75844SAndroid Build Coastguard Worker #include "api/audio_codecs/audio_encoder_factory.h"
20*d9f75844SAndroid Build Coastguard Worker #include "api/crypto/crypto_options.h"
21*d9f75844SAndroid Build Coastguard Worker #include "api/field_trials_view.h"
22*d9f75844SAndroid Build Coastguard Worker #include "api/rtp_parameters.h"
23*d9f75844SAndroid Build Coastguard Worker #include "api/video/video_bitrate_allocator_factory.h"
24*d9f75844SAndroid Build Coastguard Worker #include "call/audio_state.h"
25*d9f75844SAndroid Build Coastguard Worker #include "media/base/codec.h"
26*d9f75844SAndroid Build Coastguard Worker #include "media/base/media_channel.h"
27*d9f75844SAndroid Build Coastguard Worker #include "media/base/media_config.h"
28*d9f75844SAndroid Build Coastguard Worker #include "media/base/video_common.h"
29*d9f75844SAndroid Build Coastguard Worker #include "rtc_base/system/file_wrapper.h"
30*d9f75844SAndroid Build Coastguard Worker 
31*d9f75844SAndroid Build Coastguard Worker namespace webrtc {
32*d9f75844SAndroid Build Coastguard Worker class AudioDeviceModule;
33*d9f75844SAndroid Build Coastguard Worker class AudioMixer;
34*d9f75844SAndroid Build Coastguard Worker class AudioProcessing;
35*d9f75844SAndroid Build Coastguard Worker class Call;
36*d9f75844SAndroid Build Coastguard Worker }  // namespace webrtc
37*d9f75844SAndroid Build Coastguard Worker 
38*d9f75844SAndroid Build Coastguard Worker namespace cricket {
39*d9f75844SAndroid Build Coastguard Worker 
40*d9f75844SAndroid Build Coastguard Worker // Checks that the scalability_mode value of each encoding is supported by at
41*d9f75844SAndroid Build Coastguard Worker // least one video codec of the list. If the list is empty, no check is done.
42*d9f75844SAndroid Build Coastguard Worker webrtc::RTCError CheckScalabilityModeValues(
43*d9f75844SAndroid Build Coastguard Worker     const webrtc::RtpParameters& new_parameters,
44*d9f75844SAndroid Build Coastguard Worker     rtc::ArrayView<cricket::VideoCodec> codecs);
45*d9f75844SAndroid Build Coastguard Worker 
46*d9f75844SAndroid Build Coastguard Worker // Checks the parameters have valid and supported values, and checks parameters
47*d9f75844SAndroid Build Coastguard Worker // with CheckScalabilityModeValues().
48*d9f75844SAndroid Build Coastguard Worker webrtc::RTCError CheckRtpParametersValues(
49*d9f75844SAndroid Build Coastguard Worker     const webrtc::RtpParameters& new_parameters,
50*d9f75844SAndroid Build Coastguard Worker     rtc::ArrayView<cricket::VideoCodec> codecs);
51*d9f75844SAndroid Build Coastguard Worker 
52*d9f75844SAndroid Build Coastguard Worker // Checks that the immutable values have not changed in new_parameters and
53*d9f75844SAndroid Build Coastguard Worker // checks all parameters with CheckRtpParametersValues().
54*d9f75844SAndroid Build Coastguard Worker webrtc::RTCError CheckRtpParametersInvalidModificationAndValues(
55*d9f75844SAndroid Build Coastguard Worker     const webrtc::RtpParameters& old_parameters,
56*d9f75844SAndroid Build Coastguard Worker     const webrtc::RtpParameters& new_parameters,
57*d9f75844SAndroid Build Coastguard Worker     rtc::ArrayView<cricket::VideoCodec> codecs);
58*d9f75844SAndroid Build Coastguard Worker 
59*d9f75844SAndroid Build Coastguard Worker // Checks that the immutable values have not changed in new_parameters and
60*d9f75844SAndroid Build Coastguard Worker // checks parameters (except SVC) with CheckRtpParametersValues(). It should
61*d9f75844SAndroid Build Coastguard Worker // usually be paired with a call to CheckScalabilityModeValues().
62*d9f75844SAndroid Build Coastguard Worker webrtc::RTCError CheckRtpParametersInvalidModificationAndValues(
63*d9f75844SAndroid Build Coastguard Worker     const webrtc::RtpParameters& old_parameters,
64*d9f75844SAndroid Build Coastguard Worker     const webrtc::RtpParameters& new_parameters);
65*d9f75844SAndroid Build Coastguard Worker 
66*d9f75844SAndroid Build Coastguard Worker struct RtpCapabilities {
67*d9f75844SAndroid Build Coastguard Worker   RtpCapabilities();
68*d9f75844SAndroid Build Coastguard Worker   ~RtpCapabilities();
69*d9f75844SAndroid Build Coastguard Worker   std::vector<webrtc::RtpExtension> header_extensions;
70*d9f75844SAndroid Build Coastguard Worker };
71*d9f75844SAndroid Build Coastguard Worker 
72*d9f75844SAndroid Build Coastguard Worker class RtpHeaderExtensionQueryInterface {
73*d9f75844SAndroid Build Coastguard Worker  public:
74*d9f75844SAndroid Build Coastguard Worker   virtual ~RtpHeaderExtensionQueryInterface() = default;
75*d9f75844SAndroid Build Coastguard Worker 
76*d9f75844SAndroid Build Coastguard Worker   // Returns a vector of RtpHeaderExtensionCapability, whose direction is
77*d9f75844SAndroid Build Coastguard Worker   // kStopped if the extension is stopped (not used) by default.
78*d9f75844SAndroid Build Coastguard Worker   virtual std::vector<webrtc::RtpHeaderExtensionCapability>
79*d9f75844SAndroid Build Coastguard Worker   GetRtpHeaderExtensions() const = 0;
80*d9f75844SAndroid Build Coastguard Worker };
81*d9f75844SAndroid Build Coastguard Worker 
82*d9f75844SAndroid Build Coastguard Worker class VoiceEngineInterface : public RtpHeaderExtensionQueryInterface {
83*d9f75844SAndroid Build Coastguard Worker  public:
84*d9f75844SAndroid Build Coastguard Worker   VoiceEngineInterface() = default;
85*d9f75844SAndroid Build Coastguard Worker   virtual ~VoiceEngineInterface() = default;
86*d9f75844SAndroid Build Coastguard Worker 
87*d9f75844SAndroid Build Coastguard Worker   VoiceEngineInterface(const VoiceEngineInterface&) = delete;
88*d9f75844SAndroid Build Coastguard Worker   VoiceEngineInterface& operator=(const VoiceEngineInterface&) = delete;
89*d9f75844SAndroid Build Coastguard Worker 
90*d9f75844SAndroid Build Coastguard Worker   // Initialization
91*d9f75844SAndroid Build Coastguard Worker   // Starts the engine.
92*d9f75844SAndroid Build Coastguard Worker   virtual void Init() = 0;
93*d9f75844SAndroid Build Coastguard Worker 
94*d9f75844SAndroid Build Coastguard Worker   // TODO(solenberg): Remove once VoE API refactoring is done.
95*d9f75844SAndroid Build Coastguard Worker   virtual rtc::scoped_refptr<webrtc::AudioState> GetAudioState() const = 0;
96*d9f75844SAndroid Build Coastguard Worker 
97*d9f75844SAndroid Build Coastguard Worker   // MediaChannel creation
98*d9f75844SAndroid Build Coastguard Worker   // Creates a voice media channel. Returns NULL on failure.
99*d9f75844SAndroid Build Coastguard Worker   virtual VoiceMediaChannel* CreateMediaChannel(
100*d9f75844SAndroid Build Coastguard Worker       webrtc::Call* call,
101*d9f75844SAndroid Build Coastguard Worker       const MediaConfig& config,
102*d9f75844SAndroid Build Coastguard Worker       const AudioOptions& options,
103*d9f75844SAndroid Build Coastguard Worker       const webrtc::CryptoOptions& crypto_options) = 0;
104*d9f75844SAndroid Build Coastguard Worker 
105*d9f75844SAndroid Build Coastguard Worker   virtual const std::vector<AudioCodec>& send_codecs() const = 0;
106*d9f75844SAndroid Build Coastguard Worker   virtual const std::vector<AudioCodec>& recv_codecs() const = 0;
107*d9f75844SAndroid Build Coastguard Worker 
108*d9f75844SAndroid Build Coastguard Worker   // Starts AEC dump using existing file, a maximum file size in bytes can be
109*d9f75844SAndroid Build Coastguard Worker   // specified. Logging is stopped just before the size limit is exceeded.
110*d9f75844SAndroid Build Coastguard Worker   // If max_size_bytes is set to a value <= 0, no limit will be used.
111*d9f75844SAndroid Build Coastguard Worker   virtual bool StartAecDump(webrtc::FileWrapper file,
112*d9f75844SAndroid Build Coastguard Worker                             int64_t max_size_bytes) = 0;
113*d9f75844SAndroid Build Coastguard Worker 
114*d9f75844SAndroid Build Coastguard Worker   // Stops recording AEC dump.
115*d9f75844SAndroid Build Coastguard Worker   virtual void StopAecDump() = 0;
116*d9f75844SAndroid Build Coastguard Worker };
117*d9f75844SAndroid Build Coastguard Worker 
118*d9f75844SAndroid Build Coastguard Worker class VideoEngineInterface : public RtpHeaderExtensionQueryInterface {
119*d9f75844SAndroid Build Coastguard Worker  public:
120*d9f75844SAndroid Build Coastguard Worker   VideoEngineInterface() = default;
121*d9f75844SAndroid Build Coastguard Worker   virtual ~VideoEngineInterface() = default;
122*d9f75844SAndroid Build Coastguard Worker 
123*d9f75844SAndroid Build Coastguard Worker   VideoEngineInterface(const VideoEngineInterface&) = delete;
124*d9f75844SAndroid Build Coastguard Worker   VideoEngineInterface& operator=(const VideoEngineInterface&) = delete;
125*d9f75844SAndroid Build Coastguard Worker 
126*d9f75844SAndroid Build Coastguard Worker   // Creates a video media channel, paired with the specified voice channel.
127*d9f75844SAndroid Build Coastguard Worker   // Returns NULL on failure.
128*d9f75844SAndroid Build Coastguard Worker   virtual VideoMediaChannel* CreateMediaChannel(
129*d9f75844SAndroid Build Coastguard Worker       webrtc::Call* call,
130*d9f75844SAndroid Build Coastguard Worker       const MediaConfig& config,
131*d9f75844SAndroid Build Coastguard Worker       const VideoOptions& options,
132*d9f75844SAndroid Build Coastguard Worker       const webrtc::CryptoOptions& crypto_options,
133*d9f75844SAndroid Build Coastguard Worker       webrtc::VideoBitrateAllocatorFactory*
134*d9f75844SAndroid Build Coastguard Worker           video_bitrate_allocator_factory) = 0;
135*d9f75844SAndroid Build Coastguard Worker 
136*d9f75844SAndroid Build Coastguard Worker   // Retrieve list of supported codecs.
137*d9f75844SAndroid Build Coastguard Worker   virtual std::vector<VideoCodec> send_codecs() const = 0;
138*d9f75844SAndroid Build Coastguard Worker   virtual std::vector<VideoCodec> recv_codecs() const = 0;
139*d9f75844SAndroid Build Coastguard Worker   // As above, but if include_rtx is false, don't include RTX codecs.
140*d9f75844SAndroid Build Coastguard Worker   // TODO(bugs.webrtc.org/13931): Remove default implementation once
141*d9f75844SAndroid Build Coastguard Worker   // upstream subclasses have converted.
send_codecs(bool include_rtx)142*d9f75844SAndroid Build Coastguard Worker   virtual std::vector<VideoCodec> send_codecs(bool include_rtx) const {
143*d9f75844SAndroid Build Coastguard Worker     RTC_DCHECK(include_rtx);
144*d9f75844SAndroid Build Coastguard Worker     return send_codecs();
145*d9f75844SAndroid Build Coastguard Worker   }
recv_codecs(bool include_rtx)146*d9f75844SAndroid Build Coastguard Worker   virtual std::vector<VideoCodec> recv_codecs(bool include_rtx) const {
147*d9f75844SAndroid Build Coastguard Worker     RTC_DCHECK(include_rtx);
148*d9f75844SAndroid Build Coastguard Worker     return recv_codecs();
149*d9f75844SAndroid Build Coastguard Worker   }
150*d9f75844SAndroid Build Coastguard Worker };
151*d9f75844SAndroid Build Coastguard Worker 
152*d9f75844SAndroid Build Coastguard Worker // MediaEngineInterface is an abstraction of a media engine which can be
153*d9f75844SAndroid Build Coastguard Worker // subclassed to support different media componentry backends.
154*d9f75844SAndroid Build Coastguard Worker // It supports voice and video operations in the same class to facilitate
155*d9f75844SAndroid Build Coastguard Worker // proper synchronization between both media types.
156*d9f75844SAndroid Build Coastguard Worker class MediaEngineInterface {
157*d9f75844SAndroid Build Coastguard Worker  public:
~MediaEngineInterface()158*d9f75844SAndroid Build Coastguard Worker   virtual ~MediaEngineInterface() {}
159*d9f75844SAndroid Build Coastguard Worker 
160*d9f75844SAndroid Build Coastguard Worker   // Initialization. Needs to be called on the worker thread.
161*d9f75844SAndroid Build Coastguard Worker   virtual bool Init() = 0;
162*d9f75844SAndroid Build Coastguard Worker 
163*d9f75844SAndroid Build Coastguard Worker   virtual VoiceEngineInterface& voice() = 0;
164*d9f75844SAndroid Build Coastguard Worker   virtual VideoEngineInterface& video() = 0;
165*d9f75844SAndroid Build Coastguard Worker   virtual const VoiceEngineInterface& voice() const = 0;
166*d9f75844SAndroid Build Coastguard Worker   virtual const VideoEngineInterface& video() const = 0;
167*d9f75844SAndroid Build Coastguard Worker };
168*d9f75844SAndroid Build Coastguard Worker 
169*d9f75844SAndroid Build Coastguard Worker // CompositeMediaEngine constructs a MediaEngine from separate
170*d9f75844SAndroid Build Coastguard Worker // voice and video engine classes.
171*d9f75844SAndroid Build Coastguard Worker // Optionally owns a FieldTrialsView trials map.
172*d9f75844SAndroid Build Coastguard Worker class CompositeMediaEngine : public MediaEngineInterface {
173*d9f75844SAndroid Build Coastguard Worker  public:
174*d9f75844SAndroid Build Coastguard Worker   CompositeMediaEngine(std::unique_ptr<webrtc::FieldTrialsView> trials,
175*d9f75844SAndroid Build Coastguard Worker                        std::unique_ptr<VoiceEngineInterface> audio_engine,
176*d9f75844SAndroid Build Coastguard Worker                        std::unique_ptr<VideoEngineInterface> video_engine);
177*d9f75844SAndroid Build Coastguard Worker   CompositeMediaEngine(std::unique_ptr<VoiceEngineInterface> audio_engine,
178*d9f75844SAndroid Build Coastguard Worker                        std::unique_ptr<VideoEngineInterface> video_engine);
179*d9f75844SAndroid Build Coastguard Worker   ~CompositeMediaEngine() override;
180*d9f75844SAndroid Build Coastguard Worker 
181*d9f75844SAndroid Build Coastguard Worker   // Always succeeds.
182*d9f75844SAndroid Build Coastguard Worker   bool Init() override;
183*d9f75844SAndroid Build Coastguard Worker 
184*d9f75844SAndroid Build Coastguard Worker   VoiceEngineInterface& voice() override;
185*d9f75844SAndroid Build Coastguard Worker   VideoEngineInterface& video() override;
186*d9f75844SAndroid Build Coastguard Worker   const VoiceEngineInterface& voice() const override;
187*d9f75844SAndroid Build Coastguard Worker   const VideoEngineInterface& video() const override;
188*d9f75844SAndroid Build Coastguard Worker 
189*d9f75844SAndroid Build Coastguard Worker  private:
190*d9f75844SAndroid Build Coastguard Worker   const std::unique_ptr<webrtc::FieldTrialsView> trials_;
191*d9f75844SAndroid Build Coastguard Worker   const std::unique_ptr<VoiceEngineInterface> voice_engine_;
192*d9f75844SAndroid Build Coastguard Worker   const std::unique_ptr<VideoEngineInterface> video_engine_;
193*d9f75844SAndroid Build Coastguard Worker };
194*d9f75844SAndroid Build Coastguard Worker 
195*d9f75844SAndroid Build Coastguard Worker webrtc::RtpParameters CreateRtpParametersWithOneEncoding();
196*d9f75844SAndroid Build Coastguard Worker webrtc::RtpParameters CreateRtpParametersWithEncodings(StreamParams sp);
197*d9f75844SAndroid Build Coastguard Worker 
198*d9f75844SAndroid Build Coastguard Worker // Returns a vector of RTP extensions as visible from RtpSender/Receiver
199*d9f75844SAndroid Build Coastguard Worker // GetCapabilities(). The returned vector only shows what will definitely be
200*d9f75844SAndroid Build Coastguard Worker // offered by default, i.e. the list of extensions returned from
201*d9f75844SAndroid Build Coastguard Worker // GetRtpHeaderExtensions() that are not kStopped.
202*d9f75844SAndroid Build Coastguard Worker std::vector<webrtc::RtpExtension> GetDefaultEnabledRtpHeaderExtensions(
203*d9f75844SAndroid Build Coastguard Worker     const RtpHeaderExtensionQueryInterface& query_interface);
204*d9f75844SAndroid Build Coastguard Worker 
205*d9f75844SAndroid Build Coastguard Worker }  // namespace cricket
206*d9f75844SAndroid Build Coastguard Worker 
207*d9f75844SAndroid Build Coastguard Worker #endif  // MEDIA_BASE_MEDIA_ENGINE_H_
208