2017-04-19 18:06:01 +00:00
|
|
|
/*
|
|
|
|
This file is part of Telegram Desktop,
|
|
|
|
the official desktop version of Telegram messaging app, see https://telegram.org
|
|
|
|
|
|
|
|
Telegram Desktop is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
It is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
In addition, as a special exception, the copyright holders give permission
|
|
|
|
to link the code of portions of this program with the OpenSSL library.
|
|
|
|
|
|
|
|
Full license: https://github.com/telegramdesktop/tdesktop/blob/master/LICENSE
|
|
|
|
Copyright (c) 2014-2017 John Preston, https://desktop.telegram.org
|
|
|
|
*/
|
|
|
|
#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"
|
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;
|
|
|
|
std::vector<gsl::byte> p;
|
|
|
|
};
|
|
|
|
|
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-04-19 20:25:48 +00:00
|
|
|
static constexpr auto kRandomPowerSize = 256;
|
2017-04-25 16:45:41 +00:00
|
|
|
static constexpr auto kSha256Size = 32;
|
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
|
|
|
|
2017-04-19 20:25:48 +00:00
|
|
|
void start(base::const_byte_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
|
|
|
|
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;
|
|
|
|
std::array<gsl::byte, kSha256Size> 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:
|
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
|
|
|
|
2017-04-24 12:16:38 +00:00
|
|
|
void generateModExpFirst(base::const_byte_span randomSeed);
|
2017-04-19 18:06:01 +00:00
|
|
|
void handleControllerStateChange(tgvoip::VoIPController *controller, int state);
|
|
|
|
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);
|
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;
|
|
|
|
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;
|
|
|
|
std::vector<gsl::byte> _ga;
|
|
|
|
std::vector<gsl::byte> _gb;
|
|
|
|
std::array<gsl::byte, kSha256Size> _gaHash;
|
|
|
|
std::array<gsl::byte, kRandomPowerSize> _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;
|
|
|
|
|
|
|
|
std::unique_ptr<tgvoip::VoIPController> _controller;
|
|
|
|
|
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
|