calls_call.h 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. /*
  2. This file is part of Telegram Desktop,
  3. the official desktop application for the Telegram messaging service.
  4. For license and copyright information please follow this link:
  5. https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
  6. */
  7. #pragma once
  8. #include "base/weak_ptr.h"
  9. #include "base/timer.h"
  10. #include "base/bytes.h"
  11. #include "mtproto/sender.h"
  12. #include "mtproto/mtproto_auth_key.h"
  13. #include "webrtc/webrtc_device_resolver.h"
  14. namespace Media {
  15. namespace Audio {
  16. class Track;
  17. } // namespace Audio
  18. } // namespace Media
  19. namespace tgcalls {
  20. class Instance;
  21. class VideoCaptureInterface;
  22. enum class State;
  23. enum class VideoState;
  24. enum class AudioState;
  25. } // namespace tgcalls
  26. namespace Webrtc {
  27. enum class VideoState;
  28. class VideoTrack;
  29. struct DeviceResolvedId;
  30. } // namespace Webrtc
  31. namespace Calls {
  32. struct DhConfig {
  33. int32 version = 0;
  34. int32 g = 0;
  35. bytes::vector p;
  36. };
  37. enum class ErrorType {
  38. NoCamera,
  39. NoMicrophone,
  40. NotStartedCall,
  41. NotVideoCall,
  42. Unknown,
  43. };
  44. struct Error {
  45. ErrorType type = ErrorType::Unknown;
  46. QString details;
  47. };
  48. enum class CallType {
  49. Incoming,
  50. Outgoing,
  51. };
  52. class Call final
  53. : public base::has_weak_ptr
  54. , private Webrtc::CaptureMuteTracker {
  55. public:
  56. class Delegate {
  57. public:
  58. virtual DhConfig getDhConfig() const = 0;
  59. virtual void callFinished(not_null<Call*> call) = 0;
  60. virtual void callFailed(not_null<Call*> call) = 0;
  61. virtual void callRedial(not_null<Call*> call) = 0;
  62. enum class CallSound {
  63. Connecting,
  64. Busy,
  65. Ended,
  66. };
  67. virtual void callPlaySound(CallSound sound) = 0;
  68. virtual void callRequestPermissionsOrFail(
  69. Fn<void()> onSuccess,
  70. bool video) = 0;
  71. virtual auto callGetVideoCapture(
  72. const QString &deviceId,
  73. bool isScreenCapture)
  74. -> std::shared_ptr<tgcalls::VideoCaptureInterface> = 0;
  75. virtual ~Delegate() = default;
  76. };
  77. static constexpr auto kSoundSampleMs = 100;
  78. using Type = CallType;
  79. Call(
  80. not_null<Delegate*> delegate,
  81. not_null<UserData*> user,
  82. Type type,
  83. bool video);
  84. [[nodiscard]] Type type() const {
  85. return _type;
  86. }
  87. [[nodiscard]] not_null<UserData*> user() const {
  88. return _user;
  89. }
  90. [[nodiscard]] CallId id() const {
  91. return _id;
  92. }
  93. [[nodiscard]] bool isIncomingWaiting() const;
  94. void start(bytes::const_span random);
  95. bool handleUpdate(const MTPPhoneCall &call);
  96. bool handleSignalingData(const MTPDupdatePhoneCallSignalingData &data);
  97. enum State {
  98. Starting,
  99. WaitingInit,
  100. WaitingInitAck,
  101. Established,
  102. FailedHangingUp,
  103. Failed,
  104. HangingUp,
  105. Ended,
  106. EndedByOtherDevice,
  107. ExchangingKeys,
  108. Waiting,
  109. Requesting,
  110. WaitingIncoming,
  111. Ringing,
  112. Busy,
  113. WaitingUserConfirmation,
  114. };
  115. [[nodiscard]] State state() const {
  116. return _state.current();
  117. }
  118. [[nodiscard]] rpl::producer<State> stateValue() const {
  119. return _state.value();
  120. }
  121. [[nodiscard]] rpl::producer<Error> errors() const {
  122. return _errors.events();
  123. }
  124. enum class RemoteAudioState {
  125. Muted,
  126. Active,
  127. };
  128. [[nodiscard]] RemoteAudioState remoteAudioState() const {
  129. return _remoteAudioState.current();
  130. }
  131. [[nodiscard]] auto remoteAudioStateValue() const
  132. -> rpl::producer<RemoteAudioState> {
  133. return _remoteAudioState.value();
  134. }
  135. [[nodiscard]] Webrtc::VideoState remoteVideoState() const {
  136. return _remoteVideoState.current();
  137. }
  138. [[nodiscard]] auto remoteVideoStateValue() const
  139. -> rpl::producer<Webrtc::VideoState> {
  140. return _remoteVideoState.value();
  141. }
  142. enum class RemoteBatteryState {
  143. Low,
  144. Normal,
  145. };
  146. [[nodiscard]] RemoteBatteryState remoteBatteryState() const {
  147. return _remoteBatteryState.current();
  148. }
  149. [[nodiscard]] auto remoteBatteryStateValue() const
  150. -> rpl::producer<RemoteBatteryState> {
  151. return _remoteBatteryState.value();
  152. }
  153. static constexpr auto kSignalBarStarting = -1;
  154. static constexpr auto kSignalBarFinished = -2;
  155. static constexpr auto kSignalBarCount = 4;
  156. [[nodiscard]] rpl::producer<int> signalBarCountValue() const {
  157. return _signalBarCount.value();
  158. }
  159. void setMuted(bool mute);
  160. [[nodiscard]] bool muted() const {
  161. return _muted.current();
  162. }
  163. [[nodiscard]] rpl::producer<bool> mutedValue() const {
  164. return _muted.value();
  165. }
  166. [[nodiscard]] not_null<Webrtc::VideoTrack*> videoIncoming() const;
  167. [[nodiscard]] not_null<Webrtc::VideoTrack*> videoOutgoing() const;
  168. crl::time getDurationMs() const;
  169. float64 getWaitingSoundPeakValue() const;
  170. void applyUserConfirmation();
  171. void answer();
  172. void hangup();
  173. void redial();
  174. bool isKeyShaForFingerprintReady() const;
  175. bytes::vector getKeyShaForFingerprint() const;
  176. QString getDebugLog() const;
  177. //void setAudioVolume(bool input, float level);
  178. void setAudioDuckingEnabled(bool enabled);
  179. [[nodiscard]] QString videoDeviceId() const {
  180. return _videoCaptureDeviceId;
  181. }
  182. [[nodiscard]] bool isSharingVideo() const;
  183. [[nodiscard]] bool isSharingCamera() const;
  184. [[nodiscard]] bool isSharingScreen() const;
  185. [[nodiscard]] QString cameraSharingDeviceId() const;
  186. [[nodiscard]] QString screenSharingDeviceId() const;
  187. void toggleCameraSharing(bool enabled);
  188. void toggleScreenSharing(std::optional<QString> uniqueId);
  189. [[nodiscard]] auto playbackDeviceIdValue() const
  190. -> rpl::producer<Webrtc::DeviceResolvedId>;
  191. [[nodiscard]] auto captureDeviceIdValue() const
  192. -> rpl::producer<Webrtc::DeviceResolvedId>;
  193. [[nodiscard]] auto cameraDeviceIdValue() const
  194. -> rpl::producer<Webrtc::DeviceResolvedId>;
  195. [[nodiscard]] rpl::lifetime &lifetime() {
  196. return _lifetime;
  197. }
  198. ~Call();
  199. private:
  200. enum class FinishType {
  201. None,
  202. Ended,
  203. Failed,
  204. };
  205. void handleRequestError(const QString &error);
  206. void handleControllerError(const QString &error);
  207. void finish(
  208. FinishType type,
  209. const MTPPhoneCallDiscardReason &reason
  210. = MTP_phoneCallDiscardReasonDisconnect());
  211. void startOutgoing();
  212. void startIncoming();
  213. void startWaitingTrack();
  214. void sendSignalingData(const QByteArray &data);
  215. void generateModExpFirst(bytes::const_span randomSeed);
  216. void handleControllerStateChange(tgcalls::State state);
  217. void handleControllerBarCountChange(int count);
  218. void createAndStartController(const MTPDphoneCall &call);
  219. template <typename T>
  220. bool checkCallCommonFields(const T &call);
  221. bool checkCallFields(const MTPDphoneCall &call);
  222. bool checkCallFields(const MTPDphoneCallAccepted &call);
  223. void actuallyAnswer();
  224. void confirmAcceptedCall(const MTPDphoneCallAccepted &call);
  225. void startConfirmedCall(const MTPDphoneCall &call);
  226. void setState(State state);
  227. void setStateQueued(State state);
  228. void setFailedQueued(const QString &error);
  229. void setSignalBarCount(int count);
  230. void destroyController();
  231. void captureMuteChanged(bool mute) override;
  232. rpl::producer<Webrtc::DeviceResolvedId> captureMuteDeviceId() override;
  233. void setupMediaDevices();
  234. void setupOutgoingVideo();
  235. void updateRemoteMediaState(
  236. tgcalls::AudioState audio,
  237. tgcalls::VideoState video);
  238. const not_null<Delegate*> _delegate;
  239. const not_null<UserData*> _user;
  240. MTP::Sender _api;
  241. Type _type = Type::Outgoing;
  242. rpl::variable<State> _state = State::Starting;
  243. rpl::variable<RemoteAudioState> _remoteAudioState
  244. = RemoteAudioState::Active;
  245. rpl::variable<Webrtc::VideoState> _remoteVideoState;
  246. rpl::variable<RemoteBatteryState> _remoteBatteryState
  247. = RemoteBatteryState::Normal;
  248. rpl::event_stream<Error> _errors;
  249. FinishType _finishAfterRequestingCall = FinishType::None;
  250. bool _answerAfterDhConfigReceived = false;
  251. rpl::variable<int> _signalBarCount = kSignalBarStarting;
  252. crl::time _startTime = 0;
  253. base::DelayedCallTimer _finishByTimeoutTimer;
  254. base::Timer _discardByTimeoutTimer;
  255. Fn<void(Webrtc::DeviceResolvedId)> _setDeviceIdCallback;
  256. Webrtc::DeviceResolver _playbackDeviceId;
  257. Webrtc::DeviceResolver _captureDeviceId;
  258. Webrtc::DeviceResolver _cameraDeviceId;
  259. rpl::variable<bool> _muted = false;
  260. DhConfig _dhConfig;
  261. bytes::vector _ga;
  262. bytes::vector _gb;
  263. bytes::vector _gaHash;
  264. bytes::vector _randomPower;
  265. MTP::AuthKey::Data _authKey;
  266. CallId _id = 0;
  267. uint64 _accessHash = 0;
  268. uint64 _keyFingerprint = 0;
  269. std::unique_ptr<tgcalls::Instance> _instance;
  270. std::shared_ptr<tgcalls::VideoCaptureInterface> _videoCapture;
  271. QString _videoCaptureDeviceId;
  272. bool _videoCaptureIsScreencast = false;
  273. const std::unique_ptr<Webrtc::VideoTrack> _videoIncoming;
  274. const std::unique_ptr<Webrtc::VideoTrack> _videoOutgoing;
  275. std::unique_ptr<Media::Audio::Track> _waitingTrack;
  276. rpl::lifetime _instanceLifetime;
  277. rpl::lifetime _lifetime;
  278. };
  279. void UpdateConfig(const std::string &data);
  280. } // namespace Calls