1 #ifndef TGCALLS_INSTANCE_H
2 #define TGCALLS_INSTANCE_H
3 
4 #include <functional>
5 #include <vector>
6 #include <string>
7 #include <memory>
8 #include <map>
9 
10 #include "Stats.h"
11 
12 namespace rtc {
13 template <typename VideoFrameT>
14 class VideoSinkInterface;
15 template <class T>
16 class scoped_refptr;
17 } // namespace rtc
18 
19 namespace webrtc {
20 class VideoFrame;
21 class AudioDeviceModule;
22 class TaskQueueFactory;
23 } // namespace webrtc
24 
25 namespace tgcalls {
26 
27 class VideoCaptureInterface;
28 
29 struct FilePath {
30 #ifndef _WIN32
31 	std::string data;
32 #else
33 	std::wstring data;
34 #endif
35 };
36 
37 struct Proxy {
38 	std::string host;
39 	uint16_t port = 0;
40 	std::string login;
41 	std::string password;
42 };
43 
44 struct RtcServer {
45 	std::string host;
46 	uint16_t port = 0;
47 	std::string login;
48 	std::string password;
49 	bool isTurn = false;
50 };
51 
52 enum class EndpointType {
53 	Inet,
54 	Lan,
55 	UdpRelay,
56 	TcpRelay
57 };
58 
59 struct EndpointHost {
60 	std::string ipv4;
61 	std::string ipv6;
62 };
63 
64 struct Endpoint {
65 	int64_t endpointId = 0;
66 	EndpointHost host;
67 	uint16_t port = 0;
68 	EndpointType type = EndpointType{};
69 	unsigned char peerTag[16] = { 0 };
70 };
71 
72 enum class ProtocolVersion {
73     V0,
74     V1 // Low-cost network negotiation
75 };
76 
77 enum class NetworkType {
78 	Unknown,
79 	Gprs,
80 	Edge,
81 	ThirdGeneration,
82 	Hspa,
83 	Lte,
84 	WiFi,
85 	Ethernet,
86 	OtherHighSpeed,
87 	OtherLowSpeed,
88 	OtherMobile,
89 	Dialup
90 };
91 
92 enum class DataSaving {
93 	Never,
94 	Mobile,
95 	Always
96 };
97 
98 struct PersistentState {
99 	std::vector<uint8_t> value;
100 };
101 
102 struct Config {
103 	double initializationTimeout = 0.;
104 	double receiveTimeout = 0.;
105 	DataSaving dataSaving = DataSaving::Never;
106 	bool enableP2P = false;
107     bool allowTCP = false;
108     bool enableStunMarking = false;
109 	bool enableAEC = false;
110 	bool enableNS = false;
111 	bool enableAGC = false;
112 	bool enableCallUpgrade = false;
113 	bool enableVolumeControl = false;
114 	FilePath logPath;
115     FilePath statsLogPath;
116 	int maxApiLayer = 0;
117     bool enableHighBitrateVideo = false;
118     std::vector<std::string> preferredVideoCodecs;
119     ProtocolVersion protocolVersion = ProtocolVersion::V0;
120 };
121 
122 struct EncryptionKey {
123 	static constexpr int kSize = 256;
124 
125 	std::shared_ptr<const std::array<uint8_t, kSize>> value;
126 	bool isOutgoing = false;
127 
EncryptionKeyEncryptionKey128     EncryptionKey(
129 		std::shared_ptr<std::array<uint8_t, kSize>> value,
130 		bool isOutgoing)
131 	: value(std::move(value)), isOutgoing(isOutgoing) {
132     }
133 };
134 
135 enum class State {
136 	WaitInit,
137 	WaitInitAck,
138 	Established,
139 	Failed,
140 	Reconnecting
141 };
142 
143 // Defined in VideoCaptureInterface.h
144 enum class VideoState;
145 
146 enum class AudioState {
147 	Muted,
148 	Active,
149 };
150 
151 struct TrafficStats {
152 	uint64_t bytesSentWifi = 0;
153 	uint64_t bytesReceivedWifi = 0;
154 	uint64_t bytesSentMobile = 0;
155 	uint64_t bytesReceivedMobile = 0;
156 };
157 
158 struct FinalState {
159 	PersistentState persistentState;
160 	std::string debugLog;
161 	TrafficStats trafficStats;
162     CallStats callStats;
163 	bool isRatingSuggested = false;
164 };
165 
166 struct MediaDevicesConfig {
167 	std::string audioInputId;
168 	std::string audioOutputId;
169 	float inputVolume = 1.f;
170 	float outputVolume = 1.f;
171 };
172 
173 class Instance {
174 protected:
175 	Instance() = default;
176 
177 public:
178 	virtual ~Instance() = default;
179 
180 	virtual void setNetworkType(NetworkType networkType) = 0;
181 	virtual void setMuteMicrophone(bool muteMicrophone) = 0;
182 	virtual void setAudioOutputGainControlEnabled(bool enabled) = 0;
183 	virtual void setEchoCancellationStrength(int strength) = 0;
184 
185 	virtual bool supportsVideo() = 0;
186 	virtual void setIncomingVideoOutput(std::shared_ptr<rtc::VideoSinkInterface<webrtc::VideoFrame>> sink) = 0;
187 
188 	virtual void setAudioInputDevice(std::string id) = 0;
189 	virtual void setAudioOutputDevice(std::string id) = 0;
190 	virtual void setInputVolume(float level) = 0;
191 	virtual void setOutputVolume(float level) = 0;
192 	virtual void setAudioOutputDuckingEnabled(bool enabled) = 0;
addExternalAudioSamples(std::vector<uint8_t> && samples)193     virtual void addExternalAudioSamples(std::vector<uint8_t> &&samples) {
194     }
195 
196     virtual void setIsLowBatteryLevel(bool isLowBatteryLevel) = 0;
197 
198 	virtual std::string getLastError() = 0;
199 	virtual std::string getDebugInfo() = 0;
200 	virtual int64_t getPreferredRelayId() = 0;
201 	virtual TrafficStats getTrafficStats() = 0;
202 	virtual PersistentState getPersistentState() = 0;
203 
204 	virtual void receiveSignalingData(const std::vector<uint8_t> &data) = 0;
205 	virtual void setVideoCapture(std::shared_ptr<VideoCaptureInterface> videoCapture) = 0;
206 	virtual void sendVideoDeviceUpdated() = 0;
207     virtual void setRequestedVideoAspect(float aspect) = 0;
208 
209 	virtual void stop(std::function<void(FinalState)> completion) = 0;
210 
211 };
212 
213 template <typename Implementation>
214 bool Register();
215 
216 struct Descriptor {
217 	Config config;
218 	PersistentState persistentState;
219 	std::vector<Endpoint> endpoints;
220 	std::unique_ptr<Proxy> proxy;
221 	std::vector<RtcServer> rtcServers;
222 	NetworkType initialNetworkType = NetworkType();
223 	EncryptionKey encryptionKey;
224 	MediaDevicesConfig mediaDevicesConfig;
225 	std::shared_ptr<VideoCaptureInterface> videoCapture;
226 	std::function<void(State)> stateUpdated;
227 	std::function<void(int)> signalBarsUpdated;
228     std::function<void(float)> audioLevelUpdated;
229     std::function<void(bool)> remoteBatteryLevelIsLowUpdated;
230 	std::function<void(AudioState, VideoState)> remoteMediaStateUpdated;
231     std::function<void(float)> remotePrefferedAspectRatioUpdated;
232 	std::function<void(const std::vector<uint8_t> &)> signalingDataEmitted;
233 	std::function<rtc::scoped_refptr<webrtc::AudioDeviceModule>(webrtc::TaskQueueFactory*)> createAudioDeviceModule;
234 };
235 
236 class Meta {
237 public:
238 	virtual ~Meta() = default;
239 
240 	virtual std::unique_ptr<Instance> construct(Descriptor &&descriptor) = 0;
241 	virtual int connectionMaxLayer() = 0;
242 	virtual std::vector<std::string> versions() = 0;
243 
244 	static std::unique_ptr<Instance> Create(
245 		const std::string &version,
246 		Descriptor &&descriptor);
247 	static std::vector<std::string> Versions();
248 	static int MaxLayer();
249 
250 private:
251 	template <typename Implementation>
252 	friend bool Register();
253 
254 	template <typename Implementation>
255 	static bool RegisterOne();
256 	static void RegisterOne(std::shared_ptr<Meta> meta);
257 
258 };
259 
260 template <typename Implementation>
RegisterOne()261 bool Meta::RegisterOne() {
262 	class MetaImpl final : public Meta {
263 	public:
264 		int connectionMaxLayer() override {
265 			return Implementation::GetConnectionMaxLayer();
266 		}
267 		std::vector<std::string> versions() override {
268 			return Implementation::GetVersions();
269 		}
270 		std::unique_ptr<Instance> construct(Descriptor &&descriptor) override {
271 			return std::make_unique<Implementation>(std::move(descriptor));
272 		}
273 	};
274 	RegisterOne(std::make_shared<MetaImpl>());
275 	return true;
276 }
277 
278 void SetLoggingFunction(std::function<void(std::string const &)> loggingFunction);
279 
280 } // namespace tgcalls
281 
282 #endif
283