2017-04-19 18:06:01 +00:00
|
|
|
/*
|
|
|
|
This file is part of Telegram Desktop,
|
2018-01-03 10:23:14 +00:00
|
|
|
the official desktop application for the Telegram messaging service.
|
2017-04-19 18:06:01 +00:00
|
|
|
|
2018-01-03 10:23:14 +00:00
|
|
|
For license and copyright information please follow this link:
|
|
|
|
https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
|
2017-04-19 18:06:01 +00:00
|
|
|
*/
|
|
|
|
#pragma once
|
|
|
|
|
2017-11-30 17:33:27 +00:00
|
|
|
#include "base/weak_ptr.h"
|
2017-04-19 20:25:48 +00:00
|
|
|
#include "base/timer.h"
|
2018-03-27 12:16:00 +00:00
|
|
|
#include "base/bytes.h"
|
2017-04-24 12:16:38 +00:00
|
|
|
#include "mtproto/sender.h"
|
|
|
|
#include "mtproto/auth_key.h"
|
2017-04-19 18:06:01 +00:00
|
|
|
|
2017-05-03 13:43:01 +00:00
|
|
|
namespace Media {
|
|
|
|
namespace Audio {
|
|
|
|
class Track;
|
|
|
|
} // namespace Audio
|
|
|
|
} // namespace Media
|
|
|
|
|
2017-04-19 18:06:01 +00:00
|
|
|
namespace tgvoip {
|
|
|
|
class VoIPController;
|
|
|
|
} // namespace tgvoip
|
|
|
|
|
|
|
|
namespace Calls {
|
|
|
|
|
|
|
|
struct DhConfig {
|
|
|
|
int32 version = 0;
|
|
|
|
int32 g = 0;
|
2018-03-27 12:16:00 +00:00
|
|
|
bytes::vector p;
|
2017-04-19 18:06:01 +00:00
|
|
|
};
|
|
|
|
|
2017-11-30 17:33:27 +00:00
|
|
|
class Call : public base::has_weak_ptr, private MTP::Sender {
|
2017-04-19 18:06:01 +00:00
|
|
|
public:
|
|
|
|
class Delegate {
|
|
|
|
public:
|
|
|
|
virtual DhConfig getDhConfig() const = 0;
|
2017-08-17 08:31:24 +00:00
|
|
|
virtual void callFinished(not_null<Call*> call) = 0;
|
|
|
|
virtual void callFailed(not_null<Call*> call) = 0;
|
|
|
|
virtual void callRedial(not_null<Call*> call) = 0;
|
2017-04-19 18:06:01 +00:00
|
|
|
|
2017-05-03 13:43:01 +00:00
|
|
|
enum class Sound {
|
|
|
|
Connecting,
|
|
|
|
Busy,
|
|
|
|
Ended,
|
|
|
|
};
|
|
|
|
virtual void playSound(Sound sound) = 0;
|
|
|
|
|
2017-04-19 18:06:01 +00:00
|
|
|
};
|
|
|
|
|
2017-05-07 19:09:20 +00:00
|
|
|
static constexpr auto kSoundSampleMs = 100;
|
2017-04-19 18:06:01 +00:00
|
|
|
|
2017-04-19 20:25:48 +00:00
|
|
|
enum class Type {
|
|
|
|
Incoming,
|
|
|
|
Outgoing,
|
|
|
|
};
|
2017-08-17 08:31:24 +00:00
|
|
|
Call(not_null<Delegate*> delegate, not_null<UserData*> user, Type type);
|
2017-04-19 20:25:48 +00:00
|
|
|
|
|
|
|
Type type() const {
|
|
|
|
return _type;
|
|
|
|
}
|
2017-08-17 08:31:24 +00:00
|
|
|
not_null<UserData*> user() const {
|
2017-04-19 20:25:48 +00:00
|
|
|
return _user;
|
|
|
|
}
|
2017-05-08 09:26:43 +00:00
|
|
|
bool isIncomingWaiting() const;
|
2017-04-19 18:06:01 +00:00
|
|
|
|
2018-03-27 12:16:00 +00:00
|
|
|
void start(bytes::const_span random);
|
2017-04-19 18:06:01 +00:00
|
|
|
bool handleUpdate(const MTPPhoneCall &call);
|
|
|
|
|
2017-04-19 20:25:48 +00:00
|
|
|
enum State {
|
2017-04-25 20:36:04 +00:00
|
|
|
Starting,
|
2017-04-19 20:25:48 +00:00
|
|
|
WaitingInit,
|
|
|
|
WaitingInitAck,
|
|
|
|
Established,
|
2017-05-09 12:06:21 +00:00
|
|
|
FailedHangingUp,
|
2017-04-19 20:25:48 +00:00
|
|
|
Failed,
|
|
|
|
HangingUp,
|
|
|
|
Ended,
|
2017-05-12 16:09:34 +00:00
|
|
|
EndedByOtherDevice,
|
2017-04-19 20:25:48 +00:00
|
|
|
ExchangingKeys,
|
|
|
|
Waiting,
|
|
|
|
Requesting,
|
|
|
|
WaitingIncoming,
|
|
|
|
Ringing,
|
|
|
|
Busy,
|
|
|
|
};
|
|
|
|
State state() const {
|
|
|
|
return _state;
|
|
|
|
}
|
|
|
|
base::Observable<State> &stateChanged() {
|
|
|
|
return _stateChanged;
|
|
|
|
}
|
2017-04-25 16:45:41 +00:00
|
|
|
|
2018-05-27 08:24:47 +00:00
|
|
|
static constexpr auto kSignalBarStarting = -1;
|
|
|
|
static constexpr auto kSignalBarFinished = -2;
|
|
|
|
static constexpr auto kSignalBarCount = 4;
|
|
|
|
base::Observable<int> &signalBarCountChanged() {
|
|
|
|
return _signalBarCountChanged;
|
|
|
|
}
|
|
|
|
|
2017-04-19 20:25:48 +00:00
|
|
|
void setMute(bool mute);
|
2017-04-25 16:45:41 +00:00
|
|
|
bool isMute() const {
|
|
|
|
return _mute;
|
|
|
|
}
|
|
|
|
base::Observable<bool> &muteChanged() {
|
|
|
|
return _muteChanged;
|
|
|
|
}
|
|
|
|
|
|
|
|
TimeMs getDurationMs() const;
|
2017-05-07 19:09:20 +00:00
|
|
|
float64 getWaitingSoundPeakValue() const;
|
2017-04-19 20:25:48 +00:00
|
|
|
|
|
|
|
void answer();
|
|
|
|
void hangup();
|
2017-04-29 18:00:27 +00:00
|
|
|
void redial();
|
2017-04-25 16:45:41 +00:00
|
|
|
|
|
|
|
bool isKeyShaForFingerprintReady() const;
|
2018-03-27 12:16:00 +00:00
|
|
|
bytes::vector getKeyShaForFingerprint() const;
|
2017-04-19 20:25:48 +00:00
|
|
|
|
2017-05-04 12:28:37 +00:00
|
|
|
QString getDebugLog() const;
|
|
|
|
|
2017-04-19 18:06:01 +00:00
|
|
|
~Call();
|
|
|
|
|
|
|
|
private:
|
2018-05-24 16:42:57 +00:00
|
|
|
class ControllerPointer {
|
|
|
|
public:
|
|
|
|
void create();
|
|
|
|
void reset();
|
|
|
|
bool empty() const;
|
|
|
|
|
|
|
|
bool operator==(std::nullptr_t) const;
|
|
|
|
explicit operator bool() const;
|
|
|
|
tgvoip::VoIPController *operator->() const;
|
|
|
|
tgvoip::VoIPController &operator*() const;
|
|
|
|
|
|
|
|
~ControllerPointer();
|
|
|
|
|
|
|
|
private:
|
|
|
|
std::unique_ptr<tgvoip::VoIPController> _data;
|
|
|
|
|
|
|
|
};
|
2017-05-09 12:06:21 +00:00
|
|
|
enum class FinishType {
|
|
|
|
None,
|
|
|
|
Ended,
|
|
|
|
Failed,
|
|
|
|
};
|
2017-04-29 16:25:33 +00:00
|
|
|
void handleRequestError(const RPCError &error);
|
|
|
|
void handleControllerError(int error);
|
2017-05-09 12:06:21 +00:00
|
|
|
void finish(FinishType type, const MTPPhoneCallDiscardReason &reason = MTP_phoneCallDiscardReasonDisconnect());
|
2017-04-19 20:25:48 +00:00
|
|
|
void startOutgoing();
|
|
|
|
void startIncoming();
|
2017-05-03 13:43:01 +00:00
|
|
|
void startWaitingTrack();
|
2017-04-19 18:06:01 +00:00
|
|
|
|
2018-03-27 12:16:00 +00:00
|
|
|
void generateModExpFirst(bytes::const_span randomSeed);
|
2018-05-27 08:24:47 +00:00
|
|
|
void handleControllerStateChange(
|
|
|
|
tgvoip::VoIPController *controller,
|
|
|
|
int state);
|
|
|
|
void handleControllerBarCountChange(
|
|
|
|
tgvoip::VoIPController *controller,
|
|
|
|
int count);
|
2017-04-19 18:06:01 +00:00
|
|
|
void createAndStartController(const MTPDphoneCall &call);
|
|
|
|
|
2017-04-19 20:25:48 +00:00
|
|
|
template <typename T>
|
|
|
|
bool checkCallCommonFields(const T &call);
|
2017-04-19 18:06:01 +00:00
|
|
|
bool checkCallFields(const MTPDphoneCall &call);
|
|
|
|
bool checkCallFields(const MTPDphoneCallAccepted &call);
|
|
|
|
|
|
|
|
void confirmAcceptedCall(const MTPDphoneCallAccepted &call);
|
2017-04-19 20:25:48 +00:00
|
|
|
void startConfirmedCall(const MTPDphoneCall &call);
|
|
|
|
void setState(State state);
|
|
|
|
void setStateQueued(State state);
|
2017-04-29 16:25:33 +00:00
|
|
|
void setFailedQueued(int error);
|
2018-05-27 08:24:47 +00:00
|
|
|
void setSignalBarCount(int count);
|
2017-04-29 18:00:27 +00:00
|
|
|
void destroyController();
|
2017-04-19 18:06:01 +00:00
|
|
|
|
2017-08-17 08:31:24 +00:00
|
|
|
not_null<Delegate*> _delegate;
|
|
|
|
not_null<UserData*> _user;
|
2017-04-19 20:25:48 +00:00
|
|
|
Type _type = Type::Outgoing;
|
2017-04-25 20:36:04 +00:00
|
|
|
State _state = State::Starting;
|
2017-05-09 12:06:21 +00:00
|
|
|
FinishType _finishAfterRequestingCall = FinishType::None;
|
2017-05-10 07:58:02 +00:00
|
|
|
bool _answerAfterDhConfigReceived = false;
|
2017-04-19 20:25:48 +00:00
|
|
|
base::Observable<State> _stateChanged;
|
2018-05-27 08:24:47 +00:00
|
|
|
int _signalBarCount = kSignalBarStarting;
|
|
|
|
base::Observable<int> _signalBarCountChanged;
|
2017-04-19 20:25:48 +00:00
|
|
|
TimeMs _startTime = 0;
|
2017-04-28 17:16:14 +00:00
|
|
|
base::DelayedCallTimer _finishByTimeoutTimer;
|
|
|
|
base::Timer _discardByTimeoutTimer;
|
|
|
|
|
2017-04-19 20:25:48 +00:00
|
|
|
bool _mute = false;
|
2017-04-25 16:45:41 +00:00
|
|
|
base::Observable<bool> _muteChanged;
|
2017-04-19 20:25:48 +00:00
|
|
|
|
|
|
|
DhConfig _dhConfig;
|
2018-03-27 12:16:00 +00:00
|
|
|
bytes::vector _ga;
|
|
|
|
bytes::vector _gb;
|
|
|
|
bytes::vector _gaHash;
|
|
|
|
bytes::vector _randomPower;
|
2017-04-24 12:16:38 +00:00
|
|
|
MTP::AuthKey::Data _authKey;
|
2017-04-19 20:25:48 +00:00
|
|
|
MTPPhoneCallProtocol _protocol;
|
|
|
|
|
2017-04-19 18:06:01 +00:00
|
|
|
uint64 _id = 0;
|
|
|
|
uint64 _accessHash = 0;
|
|
|
|
uint64 _keyFingerprint = 0;
|
|
|
|
|
2018-05-24 16:42:57 +00:00
|
|
|
ControllerPointer _controller;
|
2017-04-19 18:06:01 +00:00
|
|
|
|
2017-05-03 13:43:01 +00:00
|
|
|
std::unique_ptr<Media::Audio::Track> _waitingTrack;
|
|
|
|
|
2017-04-19 18:06:01 +00:00
|
|
|
};
|
|
|
|
|
2017-04-28 15:00:16 +00:00
|
|
|
void UpdateConfig(const std::map<std::string, std::string> &data);
|
|
|
|
|
2017-04-19 18:06:01 +00:00
|
|
|
} // namespace Calls
|