Nagram/TMessagesProj/jni/voip/tgcalls/MediaManager.h

188 lines
6.2 KiB
C
Raw Normal View History

2020-08-14 16:58:22 +00:00
#ifndef TGCALLS_MEDIA_MANAGER_H
#define TGCALLS_MEDIA_MANAGER_H
#include "rtc_base/thread.h"
#include "rtc_base/copy_on_write_buffer.h"
#include "rtc_base/third_party/sigslot/sigslot.h"
#include "api/transport/field_trial_based_config.h"
#include "pc/rtp_sender.h"
#include "Instance.h"
#include "Message.h"
#include "VideoCaptureInterface.h"
2020-10-01 01:59:32 +00:00
#include "Stats.h"
2020-08-14 16:58:22 +00:00
#include <functional>
#include <memory>
namespace webrtc {
class Call;
class RtcEventLogNull;
class TaskQueueFactory;
class VideoBitrateAllocatorFactory;
class VideoTrackSourceInterface;
2020-10-01 01:59:32 +00:00
class AudioDeviceModule;
2020-08-14 16:58:22 +00:00
} // namespace webrtc
namespace cricket {
class MediaEngineInterface;
class VoiceMediaChannel;
class VideoMediaChannel;
} // namespace cricket
namespace tgcalls {
2020-10-01 01:59:32 +00:00
class VideoSinkInterfaceProxyImpl;
2020-08-14 16:58:22 +00:00
class MediaManager : public sigslot::has_slots<>, public std::enable_shared_from_this<MediaManager> {
public:
static rtc::Thread *getWorkerThread();
MediaManager(
rtc::Thread *thread,
bool isOutgoing,
2020-10-01 01:59:32 +00:00
ProtocolVersion protocolVersion,
const MediaDevicesConfig &devicesConfig,
2020-08-14 16:58:22 +00:00
std::shared_ptr<VideoCaptureInterface> videoCapture,
std::function<void(Message &&)> sendSignalingMessage,
std::function<void(Message &&)> sendTransportMessage,
std::function<void(int)> signalBarsUpdated,
2023-10-28 20:52:06 +00:00
std::function<void(float, float)> audioLevelUpdated,
2021-03-19 10:25:58 +00:00
std::function<rtc::scoped_refptr<webrtc::AudioDeviceModule>(webrtc::TaskQueueFactory*)> createAudioDeviceModule,
2020-08-15 21:06:36 +00:00
bool enableHighBitrateVideo,
2020-08-21 23:59:49 +00:00
std::vector<std::string> preferredCodecs,
std::shared_ptr<PlatformContext> platformContext);
2020-08-14 16:58:22 +00:00
~MediaManager();
void start();
void setIsConnected(bool isConnected);
void notifyPacketSent(const rtc::SentPacket &sentPacket);
void setSendVideo(std::shared_ptr<VideoCaptureInterface> videoCapture);
2021-08-31 19:06:39 +00:00
void sendVideoDeviceUpdated();
2020-10-01 01:59:32 +00:00
void setRequestedVideoAspect(float aspect);
2020-08-14 16:58:22 +00:00
void setMuteOutgoingAudio(bool mute);
2022-03-11 16:49:54 +00:00
void setIncomingVideoOutput(std::weak_ptr<rtc::VideoSinkInterface<webrtc::VideoFrame>> sink);
2020-08-14 16:58:22 +00:00
void receiveMessage(DecryptedMessage &&message);
void remoteVideoStateUpdated(VideoState videoState);
2020-10-01 01:59:32 +00:00
void setNetworkParameters(bool isLowCost, bool isDataSavingActive);
void fillCallStats(CallStats &callStats);
void setAudioInputDevice(std::string id);
void setAudioOutputDevice(std::string id);
void setInputVolume(float level);
void setOutputVolume(float level);
2020-08-14 16:58:22 +00:00
2021-07-30 14:49:55 +00:00
void addExternalAudioSamples(std::vector<uint8_t> &&samples);
2020-08-14 16:58:22 +00:00
private:
struct SSRC {
uint32_t incoming = 0;
uint32_t outgoing = 0;
uint32_t fecIncoming = 0;
uint32_t fecOutgoing = 0;
};
class NetworkInterfaceImpl : public cricket::MediaChannel::NetworkInterface {
public:
NetworkInterfaceImpl(MediaManager *mediaManager, bool isVideo);
bool SendPacket(rtc::CopyOnWriteBuffer *packet, const rtc::PacketOptions& options) override;
bool SendRtcp(rtc::CopyOnWriteBuffer *packet, const rtc::PacketOptions& options) override;
int SetOption(SocketType type, rtc::Socket::Option opt, int option) override;
private:
bool sendTransportMessage(rtc::CopyOnWriteBuffer *packet, const rtc::PacketOptions& options);
MediaManager *_mediaManager = nullptr;
bool _isVideo = false;
};
friend class MediaManager::NetworkInterfaceImpl;
void setPeerVideoFormats(VideoFormatsMessage &&peerFormats);
bool computeIsSendingVideo() const;
void configureSendingVideoIfNeeded();
void checkIsSendingVideoChanged(bool wasSending);
bool videoCodecsNegotiated() const;
2021-03-19 10:25:58 +00:00
2020-08-15 21:06:36 +00:00
int getMaxVideoBitrate() const;
2020-10-01 01:59:32 +00:00
int getMaxAudioBitrate() const;
2020-08-15 21:06:36 +00:00
void adjustBitratePreferences(bool resetStartBitrate);
2020-08-14 16:58:22 +00:00
bool computeIsReceivingVideo() const;
void checkIsReceivingVideoChanged(bool wasReceiving);
void setOutgoingVideoState(VideoState state);
void setOutgoingAudioState(AudioState state);
void sendVideoParametersMessage();
void sendOutgoingMediaStateMessage();
2020-10-01 01:59:32 +00:00
rtc::scoped_refptr<webrtc::AudioDeviceModule> createAudioDeviceModule();
2020-08-14 16:58:22 +00:00
void beginStatsTimer(int timeoutMs);
2020-12-23 07:48:30 +00:00
void beginLevelsTimer(int timeoutMs);
2020-08-14 16:58:22 +00:00
void collectStats();
rtc::Thread *_thread = nullptr;
std::unique_ptr<webrtc::RtcEventLogNull> _eventLog;
std::unique_ptr<webrtc::TaskQueueFactory> _taskQueueFactory;
std::function<void(Message &&)> _sendSignalingMessage;
std::function<void(Message &&)> _sendTransportMessage;
std::function<void(int)> _signalBarsUpdated;
2023-10-28 20:52:06 +00:00
std::function<void(float, float)> _audioLevelUpdated;
2021-03-19 10:25:58 +00:00
std::function<rtc::scoped_refptr<webrtc::AudioDeviceModule>(webrtc::TaskQueueFactory*)> _createAudioDeviceModule;
2020-08-14 16:58:22 +00:00
SSRC _ssrcAudio;
SSRC _ssrcVideo;
bool _enableFlexfec = true;
2021-03-19 10:25:58 +00:00
2020-10-01 01:59:32 +00:00
ProtocolVersion _protocolVersion;
2020-08-14 16:58:22 +00:00
bool _isConnected = false;
2020-10-01 01:59:32 +00:00
bool _didConnectOnce = false;
2020-08-14 16:58:22 +00:00
bool _readyToReceiveVideo = false;
bool _didConfigureVideo = false;
AudioState _outgoingAudioState = AudioState::Active;
VideoState _outgoingVideoState = VideoState::Inactive;
VideoFormatsMessage _myVideoFormats;
std::vector<cricket::VideoCodec> _videoCodecs;
absl::optional<cricket::VideoCodec> _videoCodecOut;
std::unique_ptr<cricket::MediaEngineInterface> _mediaEngine;
std::unique_ptr<webrtc::Call> _call;
webrtc::LocalAudioSinkAdapter _audioSource;
2020-10-01 01:59:32 +00:00
rtc::scoped_refptr<webrtc::AudioDeviceModule> _audioDeviceModule;
2020-08-14 16:58:22 +00:00
std::unique_ptr<cricket::VoiceMediaChannel> _audioChannel;
std::unique_ptr<cricket::VideoMediaChannel> _videoChannel;
std::unique_ptr<webrtc::VideoBitrateAllocatorFactory> _videoBitrateAllocatorFactory;
std::shared_ptr<VideoCaptureInterface> _videoCapture;
2023-02-18 21:24:25 +00:00
std::shared_ptr<bool> _videoCaptureGuard;
2021-07-30 14:49:55 +00:00
bool _isScreenCapture = false;
2020-10-01 01:59:32 +00:00
std::shared_ptr<VideoSinkInterfaceProxyImpl> _incomingVideoSinkProxy;
2020-08-14 16:58:22 +00:00
float _localPreferredVideoAspectRatio = 0.0f;
float _preferredAspectRatio = 0.0f;
bool _enableHighBitrateVideo = false;
2020-08-15 21:06:36 +00:00
bool _isLowCostNetwork = false;
2020-10-01 01:59:32 +00:00
bool _isDataSavingActive = false;
2021-03-19 10:25:58 +00:00
2020-12-23 07:48:30 +00:00
float _currentAudioLevel = 0.0f;
float _currentMyAudioLevel = 0.0f;
2020-08-14 16:58:22 +00:00
std::unique_ptr<MediaManager::NetworkInterfaceImpl> _audioNetworkInterface;
std::unique_ptr<MediaManager::NetworkInterfaceImpl> _videoNetworkInterface;
2021-03-19 10:25:58 +00:00
2020-10-01 01:59:32 +00:00
std::vector<CallStatsBitrateRecord> _bitrateRecords;
2020-08-21 23:59:49 +00:00
2021-07-30 14:49:55 +00:00
std::vector<float> _externalAudioSamples;
webrtc::Mutex _externalAudioSamplesMutex;
2020-08-21 23:59:49 +00:00
std::shared_ptr<PlatformContext> _platformContext;
2020-08-14 16:58:22 +00:00
};
} // namespace tgcalls
#endif