| 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 | #ifndef MEDIA_BASE_MEDIA_ENGINE_H_ |
| 12 | #define MEDIA_BASE_MEDIA_ENGINE_H_ |
| 13 |
| 14 | #include <memory> |
| 15 | #include <string> |
| 16 | #include <vector> |
| 17 |
| 18 | #include "api/audio_codecs/audio_decoder_factory.h" |
| 19 | #include "api/audio_codecs/audio_encoder_factory.h" |
| 20 | #include "api/crypto/crypto_options.h" |
| 21 | #include "api/field_trials_view.h" |
| 22 | #include "api/rtp_parameters.h" |
| 23 | #include "api/video/video_bitrate_allocator_factory.h" |
| 24 | #include "call/audio_state.h" |
| 25 | #include "media/base/codec.h" |
| 26 | #include "media/base/media_channel.h" |
| 27 | #include "media/base/media_channel_impl.h" |
| 28 | #include "media/base/media_config.h" |
| 29 | #include "media/base/video_common.h" |
| 30 | #include "rtc_base/system/file_wrapper.h" |
| 31 |
| 32 | namespace webrtc { |
| 33 | class AudioDeviceModule; |
| 34 | class AudioMixer; |
| 35 | class AudioProcessing; |
| 36 | class Call; |
| 37 | } // namespace webrtc |
| 38 |
| 39 | namespace cricket { |
| 40 |
| 41 | // Checks that the scalability_mode value of each encoding is supported by at |
| 42 | // least one video codec of the list. If the list is empty, no check is done. |
| 43 | webrtc::RTCError CheckScalabilityModeValues( |
| 44 | const webrtc::RtpParameters& new_parameters, |
| 45 | rtc::ArrayView<cricket::Codec> codec_preferences, |
| 46 | absl::optional<cricket::Codec> send_codec); |
| 47 |
| 48 | // Checks the parameters have valid and supported values, and checks parameters |
| 49 | // with CheckScalabilityModeValues(). |
| 50 | webrtc::RTCError CheckRtpParametersValues( |
| 51 | const webrtc::RtpParameters& new_parameters, |
| 52 | rtc::ArrayView<cricket::Codec> codec_preferences, |
| 53 | absl::optional<cricket::Codec> send_codec); |
| 54 |
| 55 | // Checks that the immutable values have not changed in new_parameters and |
| 56 | // checks all parameters with CheckRtpParametersValues(). |
| 57 | webrtc::RTCError CheckRtpParametersInvalidModificationAndValues( |
| 58 | const webrtc::RtpParameters& old_parameters, |
| 59 | const webrtc::RtpParameters& new_parameters, |
| 60 | rtc::ArrayView<cricket::Codec> codec_preferences, |
| 61 | absl::optional<cricket::Codec> send_codec); |
| 62 |
| 63 | // Checks that the immutable values have not changed in new_parameters and |
| 64 | // checks parameters (except SVC) with CheckRtpParametersValues(). It should |
| 65 | // usually be paired with a call to CheckScalabilityModeValues(). |
| 66 | webrtc::RTCError CheckRtpParametersInvalidModificationAndValues( |
| 67 | const webrtc::RtpParameters& old_parameters, |
| 68 | const webrtc::RtpParameters& new_parameters); |
| 69 |
| 70 | struct RtpCapabilities { |
| 71 | RtpCapabilities(); |
| 72 | ~RtpCapabilities(); |
| 73 | std::vector<webrtc::RtpExtension> header_extensions; |
| 74 | }; |
| 75 |
| 76 | class RtpHeaderExtensionQueryInterface { |
| 77 | public: |
| 78 | virtual ~RtpHeaderExtensionQueryInterface() = default; |
| 79 |
| 80 | // Returns a vector of RtpHeaderExtensionCapability, whose direction is |
| 81 | // kStopped if the extension is stopped (not used) by default. |
| 82 | virtual std::vector<webrtc::RtpHeaderExtensionCapability> |
| 83 | GetRtpHeaderExtensions() const = 0; |
| 84 | }; |
| 85 |
| 86 | class VoiceEngineInterface : public RtpHeaderExtensionQueryInterface { |
| 87 | public: |
| 88 | VoiceEngineInterface() = default; |
| 89 | virtual ~VoiceEngineInterface() = default; |
| 90 |
| 91 | VoiceEngineInterface(const VoiceEngineInterface&) = delete; |
| 92 | VoiceEngineInterface& operator=(const VoiceEngineInterface&) = delete; |
| 93 |
| 94 | // Initialization |
| 95 | // Starts the engine. |
| 96 | virtual void Init() = 0; |
| 97 |
| 98 | // TODO(solenberg): Remove once VoE API refactoring is done. |
| 99 | virtual rtc::scoped_refptr<webrtc::AudioState> GetAudioState() const = 0; |
| 100 |
| 101 | virtual std::unique_ptr<VoiceMediaSendChannelInterface> CreateSendChannel( |
| 102 | webrtc::Call* call, |
| 103 | const MediaConfig& config, |
| 104 | const AudioOptions& options, |
| 105 | const webrtc::CryptoOptions& crypto_options, |
| 106 | webrtc::AudioCodecPairId codec_pair_id) { |
| 107 | // TODO(hta): Make pure virtual when all downstream has updated |
| 108 | RTC_CHECK_NOTREACHED(); |
| 109 | return nullptr; |
| 110 | } |
| 111 |
| 112 | virtual std::unique_ptr<VoiceMediaReceiveChannelInterface> |
| 113 | CreateReceiveChannel(webrtc::Call* call, |
| 114 | const MediaConfig& config, |
| 115 | const AudioOptions& options, |
| 116 | const webrtc::CryptoOptions& crypto_options, |
| 117 | webrtc::AudioCodecPairId codec_pair_id) { |
| 118 | // TODO(hta): Make pure virtual when all downstream has updated |
| 119 | RTC_CHECK_NOTREACHED(); |
| 120 | return nullptr; |
| 121 | } |
| 122 |
| 123 | virtual const std::vector<AudioCodec>& send_codecs() const = 0; |
| 124 | virtual const std::vector<AudioCodec>& recv_codecs() const = 0; |
| 125 |
| 126 | // Starts AEC dump using existing file, a maximum file size in bytes can be |
| 127 | // specified. Logging is stopped just before the size limit is exceeded. |
| 128 | // If max_size_bytes is set to a value <= 0, no limit will be used. |
| 129 | virtual bool StartAecDump(webrtc::FileWrapper file, |
| 130 | int64_t max_size_bytes) = 0; |
| 131 |
| 132 | // Stops recording AEC dump. |
| 133 | virtual void StopAecDump() = 0; |
| 134 |
| 135 | virtual absl::optional<webrtc::AudioDeviceModule::Stats> |
| 136 | GetAudioDeviceStats() = 0; |
| 137 | }; |
| 138 |
| 139 | class VideoEngineInterface : public RtpHeaderExtensionQueryInterface { |
| 140 | public: |
| 141 | VideoEngineInterface() = default; |
| 142 | virtual ~VideoEngineInterface() = default; |
| 143 |
| 144 | VideoEngineInterface(const VideoEngineInterface&) = delete; |
| 145 | VideoEngineInterface& operator=(const VideoEngineInterface&) = delete; |
| 146 |
| 147 | virtual std::unique_ptr<VideoMediaSendChannelInterface> CreateSendChannel( |
| 148 | webrtc::Call* call, |
| 149 | const MediaConfig& config, |
| 150 | const VideoOptions& options, |
| 151 | const webrtc::CryptoOptions& crypto_options, |
| 152 | webrtc::VideoBitrateAllocatorFactory* video_bitrate_allocator_factory) { |
| 153 | // Default implementation, delete when all is updated |
| 154 | RTC_CHECK_NOTREACHED(); |
| 155 | return nullptr; |
| 156 | } |
| 157 |
| 158 | virtual std::unique_ptr<VideoMediaReceiveChannelInterface> |
| 159 | CreateReceiveChannel(webrtc::Call* call, |
| 160 | const MediaConfig& config, |
| 161 | const VideoOptions& options, |
| 162 | const webrtc::CryptoOptions& crypto_options) { |
| 163 | // Default implementation, delete when all is updated |
| 164 | RTC_CHECK_NOTREACHED(); |
| 165 | return nullptr; |
| 166 | } |
| 167 |
| 168 | // Retrieve list of supported codecs. |
| 169 | virtual std::vector<VideoCodec> send_codecs() const = 0; |
| 170 | virtual std::vector<VideoCodec> recv_codecs() const = 0; |
| 171 | // As above, but if include_rtx is false, don't include RTX codecs. |
| 172 | // TODO(bugs.webrtc.org/13931): Remove default implementation once |
| 173 | // upstream subclasses have converted. |
| 174 | virtual std::vector<VideoCodec> send_codecs(bool include_rtx) const { |
| 175 | RTC_DCHECK(include_rtx); |
| 176 | return send_codecs(); |
| 177 | } |
| 178 | virtual std::vector<VideoCodec> recv_codecs(bool include_rtx) const { |
| 179 | RTC_DCHECK(include_rtx); |
| 180 | return recv_codecs(); |
| 181 | } |
| 182 | }; |
| 183 |
| 184 | // MediaEngineInterface is an abstraction of a media engine which can be |
| 185 | // subclassed to support different media componentry backends. |
| 186 | // It supports voice and video operations in the same class to facilitate |
| 187 | // proper synchronization between both media types. |
| 188 | class MediaEngineInterface { |
| 189 | public: |
| 190 | virtual ~MediaEngineInterface() {} |
| 191 |
| 192 | // Initialization. Needs to be called on the worker thread. |
| 193 | virtual bool Init() = 0; |
| 194 |
| 195 | virtual VoiceEngineInterface& voice() = 0; |
| 196 | virtual VideoEngineInterface& video() = 0; |
| 197 | virtual const VoiceEngineInterface& voice() const = 0; |
| 198 | virtual const VideoEngineInterface& video() const = 0; |
| 199 | }; |
| 200 |
| 201 | // CompositeMediaEngine constructs a MediaEngine from separate |
| 202 | // voice and video engine classes. |
| 203 | // Optionally owns a FieldTrialsView trials map. |
| 204 | class CompositeMediaEngine : public MediaEngineInterface { |
| 205 | public: |
| 206 | CompositeMediaEngine(std::unique_ptr<webrtc::FieldTrialsView> trials, |
| 207 | std::unique_ptr<VoiceEngineInterface> audio_engine, |
| 208 | std::unique_ptr<VideoEngineInterface> video_engine); |
| 209 | CompositeMediaEngine(std::unique_ptr<VoiceEngineInterface> audio_engine, |
| 210 | std::unique_ptr<VideoEngineInterface> video_engine); |
| 211 | ~CompositeMediaEngine() override; |
| 212 |
| 213 | // Always succeeds. |
| 214 | bool Init() override; |
| 215 |
| 216 | VoiceEngineInterface& voice() override; |
| 217 | VideoEngineInterface& video() override; |
| 218 | const VoiceEngineInterface& voice() const override; |
| 219 | const VideoEngineInterface& video() const override; |
| 220 |
| 221 | private: |
| 222 | const std::unique_ptr<webrtc::FieldTrialsView> trials_; |
| 223 | const std::unique_ptr<VoiceEngineInterface> voice_engine_; |
| 224 | const std::unique_ptr<VideoEngineInterface> video_engine_; |
| 225 | }; |
| 226 |
| 227 | webrtc::RtpParameters CreateRtpParametersWithOneEncoding(); |
| 228 | webrtc::RtpParameters CreateRtpParametersWithEncodings(StreamParams sp); |
| 229 |
| 230 | // Returns a vector of RTP extensions as visible from RtpSender/Receiver |
| 231 | // GetCapabilities(). The returned vector only shows what will definitely be |
| 232 | // offered by default, i.e. the list of extensions returned from |
| 233 | // GetRtpHeaderExtensions() that are not kStopped. |
| 234 | std::vector<webrtc::RtpExtension> GetDefaultEnabledRtpHeaderExtensions( |
| 235 | const RtpHeaderExtensionQueryInterface& query_interface); |
| 236 |
| 237 | } // namespace cricket |
| 238 |
| 239 | #endif // MEDIA_BASE_MEDIA_ENGINE_H_ |
| 240 |