1 #ifndef TGCALLS_MANAGER_H
2 #define TGCALLS_MANAGER_H
3 
4 #include "ThreadLocalObject.h"
5 #include "EncryptedConnection.h"
6 #include "NetworkManager.h"
7 #include "MediaManager.h"
8 #include "Instance.h"
9 #include "Stats.h"
10 
11 namespace tgcalls {
12 
13 class Manager final : public std::enable_shared_from_this<Manager> {
14 private:
15     struct ResolvedNetworkStatus {
16         bool isLowCost = false;
17         bool isLowDataRequested = false;
18 
19         bool operator==(const ResolvedNetworkStatus &rhs);
20         bool operator!=(const ResolvedNetworkStatus &rhs);
21     };
22 
23 public:
24 	static rtc::Thread *getMediaThread();
25 
26 	Manager(rtc::Thread *thread, Descriptor &&descriptor);
27 	~Manager();
28 
29 	void start();
30 	void receiveSignalingData(const std::vector<uint8_t> &data);
31 	void setVideoCapture(std::shared_ptr<VideoCaptureInterface> videoCapture);
32     void sendVideoDeviceUpdated();
33     void setRequestedVideoAspect(float aspect);
34     void setMuteOutgoingAudio(bool mute);
35 	void setIncomingVideoOutput(std::shared_ptr<rtc::VideoSinkInterface<webrtc::VideoFrame>> sink);
36     void setIsLowBatteryLevel(bool isLowBatteryLevel);
37     void setIsLocalNetworkLowCost(bool isLocalNetworkLowCost);
38     void getNetworkStats(std::function<void(TrafficStats, CallStats)> completion);
39 
40 
41 	void setAudioInputDevice(std::string id);
42 	void setAudioOutputDevice(std::string id);
43 	void setInputVolume(float level);
44 	void setOutputVolume(float level);
45 
46     void addExternalAudioSamples(std::vector<uint8_t> &&samples);
47 
48 private:
49 	void sendSignalingAsync(int delayMs, int cause);
50 	void receiveMessage(DecryptedMessage &&message);
51     void updateCurrentResolvedNetworkStatus();
52     void sendInitialSignalingMessages();
53 
54 	rtc::Thread *_thread;
55 	EncryptionKey _encryptionKey;
56 	EncryptedConnection _signaling;
57 	bool _enableP2P = false;
58     bool _enableTCP = false;
59     bool _enableStunMarking = false;
60     ProtocolVersion _protocolVersion = ProtocolVersion::V0;
61     FilePath _statsLogPath;
62 	std::vector<RtcServer> _rtcServers;
63     std::unique_ptr<Proxy> _proxy;
64 	MediaDevicesConfig _mediaDevicesConfig;
65 	std::shared_ptr<VideoCaptureInterface> _videoCapture;
66 	std::function<void(State)> _stateUpdated;
67 	std::function<void(AudioState, VideoState)> _remoteMediaStateUpdated;
68     std::function<void(bool)> _remoteBatteryLevelIsLowUpdated;
69     std::function<void(float)> _remotePrefferedAspectRatioUpdated;
70 	std::function<void(const std::vector<uint8_t> &)> _signalingDataEmitted;
71     std::function<void(int)> _signalBarsUpdated;
72     std::function<void(float)> _audioLevelUpdated;
73 	std::function<rtc::scoped_refptr<webrtc::AudioDeviceModule>(webrtc::TaskQueueFactory*)> _createAudioDeviceModule;
74 	std::function<uint32_t(const Message &)> _sendSignalingMessage;
75 	std::function<void(Message&&)> _sendTransportMessage;
76 	std::unique_ptr<ThreadLocalObject<NetworkManager>> _networkManager;
77 	std::unique_ptr<ThreadLocalObject<MediaManager>> _mediaManager;
78 	State _state = State::Reconnecting;
79     bool _didConnectOnce = false;
80     bool _enableHighBitrateVideo = false;
81     DataSaving _dataSaving = DataSaving::Never;
82     std::vector<std::string> _preferredCodecs;
83     bool _localNetworkIsLowCost = false;
84     bool _remoteNetworkIsLowCost = false;
85     bool _remoteIsLowDataRequested = false;
86     absl::optional<ResolvedNetworkStatus> _currentResolvedLocalNetworkStatus;
87     absl::optional<ResolvedNetworkStatus> _currentResolvedNetworkStatus;
88 
89 };
90 
91 } // namespace tgcalls
92 
93 #endif
94