| #include "api/video_codecs/video_encoder_factory.h" |
| 6 | #include "api/video_codecs/video_decoder_factory.h" |
| 7 | #include "api/media_stream_interface.h" |
| 8 | #include "rtc_base/network_monitor_factory.h" |
| 9 | #include "modules/audio_device/include/audio_device.h" |
| 10 | #include "rtc_base/ref_counted_object.h" |
| 11 | #include <string> |
| 12 | #include <map> |
| 13 |
| 14 | struct AVFrame; |
| 15 | struct AVCodecContext; |
| 16 |
| 17 | namespace tgcalls { |
| 18 |
| 19 | enum class VideoState; |
| 20 |
| 21 | class VideoCapturerInterface; |
| 22 | class PlatformContext; |
| 23 |
| 24 | struct PlatformCaptureInfo { |
| 25 | bool shouldBeAdaptedToReceiverAspectRate = false; |
| 26 | int rotation = 0; |
| 27 | }; |
| 28 |
| 29 | class WrappedAudioDeviceModule : public webrtc::AudioDeviceModule { |
| 30 | public: |
| 31 | virtual void Stop() = 0; |
| 32 | virtual void setIsActive(bool isActive) = 0; |
| 33 | }; |
| 34 |
| 35 | class DefaultWrappedAudioDeviceModule : public WrappedAudioDeviceModule { |
| 36 | public: |
| 37 | DefaultWrappedAudioDeviceModule(webrtc::scoped_refptr<webrtc::AudioDeviceModule> impl) : |
| 38 | _impl(impl) { |
| 39 | } |
| 40 |
| 41 | virtual ~DefaultWrappedAudioDeviceModule() { |
| 42 | } |
| 43 |
| 44 | virtual void Stop() override { |
| 45 | } |
| 46 |
| 47 | virtual void setIsActive(bool isActive) override { |
| 48 | } |
| 49 |
| 50 | virtual int32_t ActiveAudioLayer(AudioLayer *audioLayer) const override { |
| 51 | return _impl->ActiveAudioLayer(audioLayer); |
| 52 | } |
| 53 |
| 54 | virtual int32_t RegisterAudioCallback(webrtc::AudioTransport *audioCallback) override { |
| 55 | return _impl->RegisterAudioCallback(audioCallback); |
| 56 | } |
| 57 |
| 58 | virtual int32_t Init() override { |
| 59 | return _impl->Init(); |
| 60 | } |
| 61 |
| 62 | virtual int32_t Terminate() override { |
| 63 | return _impl->Terminate(); |
| 64 | } |
| 65 |
| 66 | virtual bool Initialized() const override { |
| 67 | return _impl->Initialized(); |
| 68 | } |
| 69 |
| 70 | virtual int16_t PlayoutDevices() override { |
| 71 | return _impl->PlayoutDevices(); |
| 72 | } |
| 73 |
| 74 | virtual int16_t RecordingDevices() override { |
| 75 | return _impl->RecordingDevices(); |
| 76 | } |
| 77 |
| 78 | virtual int32_t PlayoutDeviceName(uint16_t index, char name[webrtc::kAdmMaxDeviceNameSize], char guid[webrtc::kAdmMaxGuidSize]) override { |
| 79 | return _impl->PlayoutDeviceName(index, name, guid); |
| 80 | } |
| 81 |
| 82 | virtual int32_t RecordingDeviceName(uint16_t index, char name[webrtc::kAdmMaxDeviceNameSize], char guid[webrtc::kAdmMaxGuidSize]) override { |
| 83 | return _impl->RecordingDeviceName(index, name, guid); |
| 84 | } |
| 85 |
| 86 | virtual int32_t SetPlayoutDevice(uint16_t index) override { |
| 87 | return _impl->SetPlayoutDevice(index); |
| 88 | } |
| 89 |
| 90 | #ifdef TGCALLS_UWP_DESKTOP |
| 91 | virtual int32_t SetPlayoutDevice(std::string deviceId) override { |
| 92 | return _impl->SetPlayoutDevice(deviceId); |
| 93 | } |
| 94 | #endif |
| 95 |
| 96 | virtual int32_t SetPlayoutDevice(WindowsDeviceType device) override { |
| 97 | return _impl->SetPlayoutDevice(device); |
| 98 | } |
| 99 |
| 100 | virtual int32_t SetRecordingDevice(uint16_t index) override { |
| 101 | return _impl->SetRecordingDevice(index); |
| 102 | } |
| 103 |
| 104 | #ifdef TGCALLS_UWP_DESKTOP |
| 105 | virtual int32_t SetRecordingDevice(std::string deviceId) override { |
| 106 | return _impl->SetRecordingDevice(deviceId); |
| 107 | } |
| 108 | #endif |
| 109 |
| 110 | virtual int32_t SetRecordingDevice(WindowsDeviceType device) override { |
| 111 | return _impl->SetRecordingDevice(device); |
| 112 | } |
| 113 |
| 114 | virtual int32_t PlayoutIsAvailable(bool *available) override { |
| 115 | return _impl->PlayoutIsAvailable(available); |
| 116 | } |
| 117 |
| 118 | virtual int32_t InitPlayout() override { |
| 119 | return _impl->InitPlayout(); |
| 120 | } |
| 121 |
| 122 | virtual bool PlayoutIsInitialized() const override { |
| 123 | return _impl->PlayoutIsInitialized(); |
| 124 | } |
| 125 |
| 126 | virtual int32_t RecordingIsAvailable(bool *available) override { |
| 127 | return _impl->RecordingIsAvailable(available); |
| 128 | } |
| 129 |
| 130 | virtual int32_t InitRecording() override { |
| 131 | return _impl->InitRecording(); |
| 132 | } |
| 133 |
| 134 | virtual bool RecordingIsInitialized() const override { |
| 135 | return _impl->RecordingIsInitialized(); |
| 136 | } |
| 137 |
| 138 | virtual int32_t StartPlayout() override { |
| 139 | return _impl->StartPlayout(); |
| 140 | } |
| 141 |
| 142 | virtual int32_t StopPlayout() override { |
| 143 | return _impl->StopPlayout(); |
| 144 | } |
| 145 |
| 146 | virtual bool Playing() const override { |
| 147 | return _impl->Playing(); |
| 148 | } |
| 149 |
| 150 | virtual int32_t StartRecording() override { |
| 151 | return _impl->StartRecording(); |
| 152 | } |
| 153 |
| 154 | virtual int32_t StopRecording() override { |
| 155 | return _impl->StopRecording(); |
| 156 | } |
| 157 |
| 158 | virtual bool Recording() const override { |
| 159 | return _impl->Recording(); |
| 160 | } |
| 161 |
| 162 | virtual int32_t InitSpeaker() override { |
| 163 | return _impl->InitSpeaker(); |
| 164 | } |
| 165 |
| 166 | virtual bool SpeakerIsInitialized() const override { |
| 167 | return _impl->SpeakerIsInitialized(); |
| 168 | } |
| 169 |
| 170 | virtual int32_t InitMicrophone() override { |
| 171 | return _impl->InitMicrophone(); |
| 172 | } |
| 173 |
| 174 | virtual bool MicrophoneIsInitialized() const override { |
| 175 | return _impl->MicrophoneIsInitialized(); |
| 176 | } |
| 177 |
| 178 | virtual int32_t SpeakerVolumeIsAvailable(bool *available) override { |
| 179 | return _impl->SpeakerVolumeIsAvailable(available); |
| 180 | } |
| 181 |
| 182 | virtual int32_t SetSpeakerVolume(uint32_t volume) override { |
| 183 | return _impl->SetSpeakerVolume(volume); |
| 184 | } |
| 185 |
| 186 | virtual int32_t SpeakerVolume(uint32_t* volume) const override { |
| 187 | return _impl->SpeakerVolume(volume); |
| 188 | } |
| 189 |
| 190 | virtual int32_t MaxSpeakerVolume(uint32_t *maxVolume) const override { |
| 191 | return _impl->MaxSpeakerVolume(maxVolume); |
| 192 | } |
| 193 |
| 194 | virtual int32_t MinSpeakerVolume(uint32_t *minVolume) const override { |
| 195 | return _impl->MinSpeakerVolume(minVolume); |
| 196 | } |
| 197 |
| 198 | virtual int32_t MicrophoneVolumeIsAvailable(bool *available) override { |
| 199 | return _impl->MicrophoneVolumeIsAvailable(available); |
| 200 | } |
| 201 |
| 202 | virtual int32_t SetMicrophoneVolume(uint32_t volume) override { |
| 203 | return _impl->SetMicrophoneVolume(volume); |
| 204 | } |
| 205 |
| 206 | virtual int32_t MicrophoneVolume(uint32_t *volume) const override { |
| 207 | return _impl->MicrophoneVolume(volume); |
| 208 | } |
| 209 |
| 210 | virtual int32_t MaxMicrophoneVolume(uint32_t *maxVolume) const override { |
| 211 | return _impl->MaxMicrophoneVolume(maxVolume); |
| 212 | } |
| 213 |
| 214 | virtual int32_t MinMicrophoneVolume(uint32_t *minVolume) const override { |
| 215 | return _impl->MinMicrophoneVolume(minVolume); |
| 216 | } |
| 217 |
| 218 | virtual int32_t SpeakerMuteIsAvailable(bool *available) override { |
| 219 | return _impl->SpeakerMuteIsAvailable(available); |
| 220 | } |
| 221 |
| 222 | virtual int32_t SetSpeakerMute(bool enable) override { |
| 223 | return _impl->SetSpeakerMute(enable); |
| 224 | } |
| 225 |
| 226 | virtual int32_t SpeakerMute(bool *enabled) const override { |
| 227 | return _impl->SpeakerMute(enabled); |
| 228 | } |
| 229 |
| 230 | virtual int32_t MicrophoneMuteIsAvailable(bool *available) override { |
| 231 | return _impl->MicrophoneMuteIsAvailable(available); |
| 232 | } |
| 233 |
| 234 | virtual int32_t SetMicrophoneMute(bool enable) override { |
| 235 | return _impl->SetMicrophoneMute(enable); |
| 236 | } |
| 237 |
| 238 | virtual int32_t MicrophoneMute(bool *enabled) const override { |
| 239 | return _impl->MicrophoneMute(enabled); |
| 240 | } |
| 241 |
| 242 | virtual int32_t StereoPlayoutIsAvailable(bool *available) const override { |
| 243 | return _impl->StereoPlayoutIsAvailable(available); |
| 244 | } |
| 245 |
| 246 | virtual int32_t SetStereoPlayout(bool enable) override { |
| 247 | return _impl->SetStereoPlayout(enable); |
| 248 | } |
| 249 |
| 250 | virtual int32_t StereoPlayout(bool *enabled) const override { |
| 251 | return _impl->StereoPlayout(enabled); |
| 252 | } |
| 253 |
| 254 | virtual int32_t StereoRecordingIsAvailable(bool *available) const override { |
| 255 | return _impl->StereoRecordingIsAvailable(available); |
| 256 | } |
| 257 |
| 258 | virtual int32_t SetStereoRecording(bool enable) override { |
| 259 | return _impl->SetStereoRecording(enable); |
| 260 | } |
| 261 |
| 262 | virtual int32_t StereoRecording(bool *enabled) const override { |
| 263 | return _impl->StereoRecording(enabled); |
| 264 | } |
| 265 |
| 266 | virtual int32_t PlayoutDelay(uint16_t* delayMS) const override { |
| 267 | return _impl->PlayoutDelay(delayMS); |
| 268 | } |
| 269 |
| 270 | virtual bool BuiltInAECIsAvailable() const override { |
| 271 | return _impl->BuiltInAECIsAvailable(); |
| 272 | } |
| 273 |
| 274 | virtual bool BuiltInAGCIsAvailable() const override { |
| 275 | return _impl->BuiltInAGCIsAvailable(); |
| 276 | } |
| 277 |
| 278 | virtual bool BuiltInNSIsAvailable() const override { |
| 279 | return _impl->BuiltInNSIsAvailable(); |
| 280 | } |
| 281 |
| 282 | virtual int32_t EnableBuiltInAEC(bool enable) override { |
| 283 | return _impl->EnableBuiltInAEC(enable); |
| 284 | } |
| 285 |
| 286 | virtual int32_t EnableBuiltInAGC(bool enable) override { |
| 287 | return _impl->EnableBuiltInAGC(enable); |
| 288 | } |
| 289 |
| 290 | virtual int32_t EnableBuiltInNS(bool enable) override { |
| 291 | return _impl->EnableBuiltInNS(enable); |
| 292 | } |
| 293 |
| 294 | virtual int32_t GetPlayoutUnderrunCount() const override { |
| 295 | return _impl->GetPlayoutUnderrunCount(); |
| 296 | } |
| 297 |
| 298 | #if defined(WEBRTC_IOS) |
| 299 | virtual int GetPlayoutAudioParameters(webrtc::AudioParameters *params) const override { |
| 300 | return _impl->GetPlayoutAudioParameters(params); |
| 301 | } |
| 302 | virtual int GetRecordAudioParameters(webrtc::AudioParameters *params) const override { |
| 303 | return _impl->GetRecordAudioParameters(params); |
| 304 | } |
| 305 | #endif // WEBRTC_IOS |
| 306 |
| 307 | webrtc::scoped_refptr<webrtc::AudioDeviceModule> WrappedInstance() const { |
| 308 | return _impl; |
| 309 | } |
| 310 |
| 311 | private: |
| 312 | webrtc::scoped_refptr<webrtc::AudioDeviceModule> _impl; |
| 313 | }; |
| 314 |
| 315 | class PlatformVideoFrame { |
| 316 | public: |
| 317 | PlatformVideoFrame() { |
| 318 | } |
| 319 |
| 320 | virtual ~PlatformVideoFrame() = default; |
| 321 | }; |
| 322 |
| 323 | class PlatformInterface { |
| 324 | public: |
| 325 | static PlatformInterface *SharedInstance(); |
| 326 | virtual ~PlatformInterface() = default; |
| 327 |
| 328 | virtual void configurePlatformAudio(int numChannels = 1) { |
| 329 | } |
| 330 |
| 331 | virtual std::unique_ptr<rtc::NetworkMonitorFactory> createNetworkMonitorFactory() { |
| 332 | return nullptr; |
| 333 | } |
| 334 |
| 335 | virtual std::unique_ptr<webrtc::VideoEncoderFactory> makeVideoEncoderFactory(std::shared_ptr<PlatformContext> platformContext, bool preferHardwareEncoding = false, bool isScreencast = false) = 0; |
| 336 | virtual std::unique_ptr<webrtc::VideoDecoderFactory> makeVideoDecoderFactory(std::shared_ptr<PlatformContext> platformContext) = 0; |
| 337 | virtual bool supportsEncoding(const std::string &codecName, std::shared_ptr<PlatformContext> platformContext) = 0; |
| 338 | virtual webrtc::scoped_refptr<webrtc::VideoTrackSourceInterface> makeVideoSource(rtc::Thread *signalingThread, rtc::Thread *workerThread, bool screencapture) = 0; |
| 339 | virtual void adaptVideoSource(webrtc::scoped_refptr<webrtc::VideoTrackSourceInterface> videoSource, int width, int height, int fps) = 0; |
| 340 | virtual std::unique_ptr<VideoCapturerInterface> makeVideoCapturer(webrtc::scoped_refptr<webrtc::VideoTrackSourceInterface> source, std::string deviceId, std::function<void(VideoState)> stateUpdated, std::function<void(PlatformCaptureInfo)> captureInfoUpdated, std::shared_ptr<PlatformContext> platformContext, std::pair<int, int> &outResolution) = 0; |
| 341 | virtual webrtc::scoped_refptr<WrappedAudioDeviceModule> wrapAudioDeviceModule(webrtc::scoped_refptr<webrtc::AudioDeviceModule> module) { |
| 342 | return rtc::make_ref_counted<DefaultWrappedAudioDeviceModule>(module); |
| 343 | } |
| 344 | virtual void setupVideoDecoding(AVCodecContext *codecContext) { |
| 345 | } |
| 346 | virtual webrtc::scoped_refptr<webrtc::VideoFrameBuffer> createPlatformFrameFromData(AVFrame const *frame) { |
| 347 | return nullptr; |
| 348 | } |
| 349 |
| 350 | public: |
| 351 | bool preferX264 = false; |
| 352 | }; |
| 353 |
| 354 | std::unique_ptr<PlatformInterface> CreatePlatformInterface(); |
| 355 |
| 356 | inline PlatformInterface *PlatformInterface::SharedInstance() { |
| 357 | static const auto result = CreatePlatformInterface(); |
| 358 | return result.get(); |
| 359 | } |
| 360 |
| 361 | } // namespace tgcalls |
| 362 |
| 363 | #endif |
| 364 |