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