| #include "rtc_base/copy_on_write_buffer.h" |
| 6 | #include "rtc_base/third_party/sigslot/sigslot.h" |
| 7 | #include "api/transport/field_trial_based_config.h" |
| 8 | #include "pc/rtp_sender.h" |
| 9 | #include "media/base/media_channel.h" |
| 10 | #include "pc/media_factory.h" |
| 11 | #include "api/environment/environment.h" |
| 12 |
| 13 | #include "Instance.h" |
| 14 | #include "Message.h" |
| 15 | #include "VideoCaptureInterface.h" |
| 16 | #include "Stats.h" |
| 17 |
| 18 | #include <functional> |
| 19 | #include <memory> |
| 20 |
| 21 | namespace webrtc { |
| 22 | class Call; |
| 23 | class RtcEventLogNull; |
| 24 | class TaskQueueFactory; |
| 25 | class VideoBitrateAllocatorFactory; |
| 26 | class VideoTrackSourceInterface; |
| 27 | class AudioDeviceModule; |
| 28 | } // namespace webrtc |
| 29 |
| 30 | namespace cricket { |
| 31 | class MediaEngineInterface; |
| 32 | class VoiceMediaChannel; |
| 33 | class VideoMediaChannel; |
| 34 | } // namespace cricket |
| 35 |
| 36 | namespace tgcalls { |
| 37 |
| 38 | class VideoSinkInterfaceProxyImpl; |
| 39 |
| 40 | class MediaManager : public sigslot::has_slots<>, public std::enable_shared_from_this<MediaManager> { |
| 41 | public: |
| 42 | static rtc::Thread *getWorkerThread(); |
| 43 |
| 44 | MediaManager( |
| 45 | rtc::Thread *thread, |
| 46 | bool isOutgoing, |
| 47 | ProtocolVersion protocolVersion, |
| 48 | const MediaDevicesConfig &devicesConfig, |
| 49 | std::shared_ptr<VideoCaptureInterface> videoCapture, |
| 50 | std::function<void(Message &&)> sendSignalingMessage, |
| 51 | std::function<void(Message &&)> sendTransportMessage, |
| 52 | std::function<void(int)> signalBarsUpdated, |
| 53 | std::function<void(float, float)> audioLevelUpdated, |
| 54 | std::function<webrtc::scoped_refptr<webrtc::AudioDeviceModule>(webrtc::TaskQueueFactory*)> createAudioDeviceModule, |
| 55 | bool enableHighBitrateVideo, |
| 56 | std::vector<std::string> preferredCodecs, |
| 57 | std::shared_ptr<PlatformContext> platformContext); |
| 58 | ~MediaManager(); |
| 59 |
| 60 | void start(); |
| 61 | void setIsConnected(bool isConnected); |
| 62 | void notifyPacketSent(const rtc::SentPacket &sentPacket); |
| 63 | void setSendVideo(std::shared_ptr<VideoCaptureInterface> videoCapture); |
| 64 | void sendVideoDeviceUpdated(); |
| 65 | void setRequestedVideoAspect(float aspect); |
| 66 | void setMuteOutgoingAudio(bool mute); |
| 67 | void setIncomingVideoOutput(std::weak_ptr<rtc::VideoSinkInterface<webrtc::VideoFrame>> sink); |
| 68 | void receiveMessage(DecryptedMessage &&message); |
| 69 | void remoteVideoStateUpdated(VideoState videoState); |
| 70 | void setNetworkParameters(bool isLowCost, bool isDataSavingActive); |
| 71 | void fillCallStats(CallStats &callStats); |
| 72 |
| 73 | void setAudioInputDevice(std::string id); |
| 74 | void setAudioOutputDevice(std::string id); |
| 75 | void setInputVolume(float level); |
| 76 | void setOutputVolume(float level); |
| 77 |
| 78 | void addExternalAudioSamples(std::vector<uint8_t> &&samples); |
| 79 |
| 80 | private: |
| 81 | struct SSRC { |
| 82 | uint32_t incoming = 0; |
| 83 | uint32_t outgoing = 0; |
| 84 | uint32_t fecIncoming = 0; |
| 85 | uint32_t fecOutgoing = 0; |
| 86 | }; |
| 87 |
| 88 | class NetworkInterfaceImpl : public cricket::MediaChannelNetworkInterface { |
| 89 | public: |
| 90 | NetworkInterfaceImpl(MediaManager *mediaManager, bool isVideo); |
| 91 |
| 92 | bool SendPacket(rtc::CopyOnWriteBuffer *packet, const rtc::PacketOptions& options) override; |
| 93 | bool SendRtcp(rtc::CopyOnWriteBuffer *packet, const rtc::PacketOptions& options) override; |
| 94 | int SetOption(SocketType type, rtc::Socket::Option opt, int option) override; |
| 95 |
| 96 | private: |
| 97 | bool sendTransportMessage(rtc::CopyOnWriteBuffer *packet, const rtc::PacketOptions& options); |
| 98 |
| 99 | MediaManager *_mediaManager = nullptr; |
| 100 | bool _isVideo = false; |
| 101 |
| 102 | }; |
| 103 |
| 104 | friend class MediaManager::NetworkInterfaceImpl; |
| 105 |
| 106 | void setPeerVideoFormats(VideoFormatsMessage &&peerFormats); |
| 107 |
| 108 | bool computeIsSendingVideo() const; |
| 109 | void configureSendingVideoIfNeeded(); |
| 110 | void checkIsSendingVideoChanged(bool wasSending); |
| 111 | bool videoCodecsNegotiated() const; |
| 112 |
| 113 | int getMaxVideoBitrate() const; |
| 114 | int getMaxAudioBitrate() const; |
| 115 | void adjustBitratePreferences(bool resetStartBitrate); |
| 116 | bool computeIsReceivingVideo() const; |
| 117 | void checkIsReceivingVideoChanged(bool wasReceiving); |
| 118 |
| 119 | void setOutgoingVideoState(VideoState state); |
| 120 | void setOutgoingAudioState(AudioState state); |
| 121 | void sendVideoParametersMessage(); |
| 122 | void sendOutgoingMediaStateMessage(); |
| 123 |
| 124 | webrtc::scoped_refptr<webrtc::AudioDeviceModule> createAudioDeviceModule(); |
| 125 |
| 126 | void beginStatsTimer(int timeoutMs); |
| 127 | void beginLevelsTimer(int timeoutMs); |
| 128 | void collectStats(); |
| 129 |
| 130 | rtc::Thread *_thread = nullptr; |
| 131 | std::unique_ptr<webrtc::RtcEventLogNull> _eventLog; |
| 132 |
| 133 | std::function<void(Message &&)> _sendSignalingMessage; |
| 134 | std::function<void(Message &&)> _sendTransportMessage; |
| 135 | std::function<void(int)> _signalBarsUpdated; |
| 136 | std::function<void(float, float)> _audioLevelsUpdated; |
| 137 | std::function<webrtc::scoped_refptr<webrtc::AudioDeviceModule>(webrtc::TaskQueueFactory*)> _createAudioDeviceModule; |
| 138 |
| 139 | SSRC _ssrcAudio; |
| 140 | SSRC _ssrcVideo; |
| 141 | bool _enableFlexfec = true; |
| 142 |
| 143 | ProtocolVersion _protocolVersion; |
| 144 |
| 145 | bool _isConnected = false; |
| 146 | bool _didConnectOnce = false; |
| 147 | bool _readyToReceiveVideo = false; |
| 148 | bool _didConfigureVideo = false; |
| 149 | AudioState _outgoingAudioState = AudioState::Active; |
| 150 | VideoState _outgoingVideoState = VideoState::Inactive; |
| 151 |
| 152 | VideoFormatsMessage _myVideoFormats; |
| 153 | std::vector<cricket::VideoCodec> _videoCodecs; |
| 154 | absl::optional<cricket::VideoCodec> _videoCodecOut; |
| 155 |
| 156 | webrtc::Environment _webrtcEnvironment; |
| 157 | std::unique_ptr<webrtc::MediaFactory> _mediaFactory; |
| 158 | std::unique_ptr<cricket::MediaEngineInterface> _mediaEngine; |
| 159 | std::unique_ptr<webrtc::Call> _call; |
| 160 | webrtc::LocalAudioSinkAdapter _audioSource; |
| 161 | webrtc::scoped_refptr<webrtc::AudioDeviceModule> _audioDeviceModule; |
| 162 | std::unique_ptr<cricket::VoiceMediaSendChannelInterface> _audioSendChannel; |
| 163 | std::unique_ptr<cricket::VoiceMediaReceiveChannelInterface> _audioReceiveChannel; |
| 164 | std::unique_ptr<cricket::VideoMediaSendChannelInterface> _videoSendChannel; |
| 165 | bool _haveVideoSendChannel = false; |
| 166 | std::unique_ptr<cricket::VideoMediaReceiveChannelInterface> _videoReceiveChannel; |
| 167 | std::unique_ptr<webrtc::VideoBitrateAllocatorFactory> _videoBitrateAllocatorFactory; |
| 168 | std::shared_ptr<VideoCaptureInterface> _videoCapture; |
| 169 | std::shared_ptr<bool> _videoCaptureGuard; |
| 170 | bool _isScreenCapture = false; |
| 171 | std::shared_ptr<VideoSinkInterfaceProxyImpl> _incomingVideoSinkProxy; |
| 172 | webrtc::RtpHeaderExtensionMap _audioRtpHeaderExtensionMap; |
| 173 | webrtc::RtpHeaderExtensionMap _videoRtpHeaderExtensionMap; |
| 174 |
| 175 | float _localPreferredVideoAspectRatio = 0.0f; |
| 176 | float _preferredAspectRatio = 0.0f; |
| 177 | bool _enableHighBitrateVideo = false; |
| 178 | bool _isLowCostNetwork = false; |
| 179 | bool _isDataSavingActive = false; |
| 180 |
| 181 | float _currentAudioLevel = 0.0f; |
| 182 | float _currentMyAudioLevel = 0.0f; |
| 183 |
| 184 | std::unique_ptr<MediaManager::NetworkInterfaceImpl> _audioNetworkInterface; |
| 185 | std::unique_ptr<MediaManager::NetworkInterfaceImpl> _videoNetworkInterface; |
| 186 |
| 187 | std::vector<CallStatsBitrateRecord> _bitrateRecords; |
| 188 |
| 189 | std::vector<float> _externalAudioSamples; |
| 190 | webrtc::Mutex _externalAudioSamplesMutex; |
| 191 |
| 192 | std::shared_ptr<PlatformContext> _platformContext; |
| 193 | }; |
| 194 |
| 195 | } // namespace tgcalls |
| 196 |
| 197 | #endif |
| 198 |