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
|
|
|
*/
|
|
|
|
#include "calls/calls_call.h"
|
|
|
|
|
2019-07-24 11:45:24 +00:00
|
|
|
#include "main/main_session.h"
|
2020-07-09 17:38:26 +00:00
|
|
|
#include "main/main_account.h"
|
|
|
|
#include "main/main_app_config.h"
|
2018-11-26 11:55:02 +00:00
|
|
|
#include "apiwrap.h"
|
2017-04-13 08:27:10 +00:00
|
|
|
#include "lang/lang_keys.h"
|
2017-04-29 16:25:33 +00:00
|
|
|
#include "boxes/confirm_box.h"
|
2017-05-02 11:56:39 +00:00
|
|
|
#include "boxes/rate_call_box.h"
|
2017-04-19 18:06:01 +00:00
|
|
|
#include "calls/calls_instance.h"
|
2017-04-19 20:25:48 +00:00
|
|
|
#include "base/openssl_help.h"
|
2019-11-13 14:12:04 +00:00
|
|
|
#include "mtproto/mtproto_dh_utils.h"
|
2020-06-17 09:36:25 +00:00
|
|
|
#include "mtproto/mtproto_config.h"
|
2020-06-18 18:04:16 +00:00
|
|
|
#include "core/application.h"
|
|
|
|
#include "core/core_settings.h"
|
2019-02-13 12:36:59 +00:00
|
|
|
#include "media/audio/media_audio_track.h"
|
2019-09-17 16:13:12 +00:00
|
|
|
#include "base/platform/base_platform_info.h"
|
2017-05-07 19:09:20 +00:00
|
|
|
#include "calls/calls_panel.h"
|
2020-07-17 18:59:38 +00:00
|
|
|
#include "webrtc/webrtc_video_track.h"
|
2020-08-18 07:23:45 +00:00
|
|
|
#include "webrtc/webrtc_media_devices.h"
|
2019-01-04 11:09:48 +00:00
|
|
|
#include "data/data_user.h"
|
2019-01-18 12:27:37 +00:00
|
|
|
#include "data/data_session.h"
|
2019-09-13 06:06:02 +00:00
|
|
|
#include "facades.h"
|
2017-04-19 18:06:01 +00:00
|
|
|
|
2020-07-10 13:55:46 +00:00
|
|
|
#include <tgcalls/Instance.h>
|
2020-07-16 16:23:55 +00:00
|
|
|
#include <tgcalls/VideoCaptureInterface.h>
|
2020-07-09 17:38:26 +00:00
|
|
|
|
|
|
|
namespace tgcalls {
|
|
|
|
class InstanceImpl;
|
|
|
|
class InstanceImplLegacy;
|
2020-07-30 10:32:37 +00:00
|
|
|
class InstanceImplReference;
|
2020-07-09 17:38:26 +00:00
|
|
|
void SetLegacyGlobalServerConfig(const std::string &serverConfig);
|
|
|
|
} // namespace tgcalls
|
|
|
|
|
2017-04-19 18:06:01 +00:00
|
|
|
namespace Calls {
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
constexpr auto kMinLayer = 65;
|
2017-04-28 17:16:14 +00:00
|
|
|
constexpr auto kHangupTimeoutMs = 5000;
|
2018-06-06 10:28:43 +00:00
|
|
|
constexpr auto kSha256Size = 32;
|
2020-05-19 06:33:57 +00:00
|
|
|
const auto kDefaultVersion = "2.4.4"_q;
|
2017-04-19 18:06:01 +00:00
|
|
|
|
2020-08-11 13:31:27 +00:00
|
|
|
#ifndef DESKTOP_APP_DISABLE_WEBRTC_INTEGRATION
|
2020-07-09 17:38:26 +00:00
|
|
|
const auto RegisterTag = tgcalls::Register<tgcalls::InstanceImpl>();
|
2020-07-31 14:36:35 +00:00
|
|
|
//const auto RegisterTagReference = tgcalls::Register<tgcalls::InstanceImplReference>();
|
2020-08-11 13:31:27 +00:00
|
|
|
#endif // DESKTOP_APP_DISABLE_WEBRTC_INTEGRATION
|
|
|
|
const auto RegisterTagLegacy = tgcalls::Register<tgcalls::InstanceImplLegacy>();
|
2020-07-09 17:38:26 +00:00
|
|
|
|
2019-03-12 11:16:58 +00:00
|
|
|
void AppendEndpoint(
|
2020-07-09 17:38:26 +00:00
|
|
|
std::vector<tgcalls::Endpoint> &list,
|
2019-03-12 11:16:58 +00:00
|
|
|
const MTPPhoneConnection &connection) {
|
|
|
|
connection.match([&](const MTPDphoneConnection &data) {
|
2019-07-05 13:38:38 +00:00
|
|
|
if (data.vpeer_tag().v.length() != 16) {
|
2019-03-12 11:16:58 +00:00
|
|
|
return;
|
|
|
|
}
|
2020-07-30 10:32:37 +00:00
|
|
|
tgcalls::Endpoint endpoint = {
|
2020-05-14 14:12:07 +00:00
|
|
|
.endpointId = (int64_t)data.vid().v,
|
2020-07-09 17:38:26 +00:00
|
|
|
.host = tgcalls::EndpointHost{
|
2020-05-14 14:12:07 +00:00
|
|
|
.ipv4 = data.vip().v.toStdString(),
|
|
|
|
.ipv6 = data.vipv6().v.toStdString() },
|
|
|
|
.port = (uint16_t)data.vport().v,
|
2020-08-04 18:31:30 +00:00
|
|
|
.type = tgcalls::EndpointType::UdpRelay,
|
2020-05-14 14:12:07 +00:00
|
|
|
};
|
|
|
|
const auto tag = data.vpeer_tag().v;
|
|
|
|
if (tag.size() >= 16) {
|
|
|
|
memcpy(endpoint.peerTag, tag.data(), 16);
|
|
|
|
}
|
|
|
|
list.push_back(std::move(endpoint));
|
2020-08-04 18:31:30 +00:00
|
|
|
}, [&](const MTPDphoneConnectionWebrtc &data) {
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
void AppendServer(
|
|
|
|
std::vector<tgcalls::RtcServer> &list,
|
|
|
|
const MTPPhoneConnection &connection) {
|
|
|
|
connection.match([&](const MTPDphoneConnection &data) {
|
|
|
|
}, [&](const MTPDphoneConnectionWebrtc &data) {
|
|
|
|
const auto host = qs(data.vip());
|
|
|
|
const auto hostv6 = qs(data.vipv6());
|
|
|
|
const auto port = uint16_t(data.vport().v);
|
|
|
|
if (data.is_stun()) {
|
|
|
|
const auto pushStun = [&](const QString &host) {
|
|
|
|
if (host.isEmpty()) {
|
|
|
|
return;
|
|
|
|
}
|
2020-08-11 09:59:48 +00:00
|
|
|
list.push_back(tgcalls::RtcServer{
|
2020-08-04 18:31:30 +00:00
|
|
|
.host = host.toStdString(),
|
|
|
|
.port = port,
|
|
|
|
.isTurn = false
|
|
|
|
});
|
|
|
|
};
|
|
|
|
pushStun(host);
|
|
|
|
pushStun(hostv6);
|
|
|
|
}
|
|
|
|
const auto username = qs(data.vusername());
|
|
|
|
const auto password = qs(data.vpassword());
|
|
|
|
if (data.is_turn() && !username.isEmpty() && !password.isEmpty()) {
|
|
|
|
const auto pushTurn = [&](const QString &host) {
|
2020-08-11 09:59:48 +00:00
|
|
|
list.push_back(tgcalls::RtcServer{
|
2020-08-04 18:31:30 +00:00
|
|
|
.host = host.toStdString(),
|
|
|
|
.port = port,
|
|
|
|
.login = username.toStdString(),
|
|
|
|
.password = password.toStdString(),
|
|
|
|
.isTurn = true,
|
|
|
|
});
|
|
|
|
};
|
|
|
|
pushTurn(host);
|
|
|
|
pushTurn(hostv6);
|
|
|
|
}
|
2019-03-12 11:16:58 +00:00
|
|
|
});
|
2017-04-19 18:06:01 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 20:25:48 +00:00
|
|
|
constexpr auto kFingerprintDataSize = 256;
|
2018-03-27 12:16:00 +00:00
|
|
|
uint64 ComputeFingerprint(bytes::const_span authKey) {
|
|
|
|
Expects(authKey.size() == kFingerprintDataSize);
|
|
|
|
|
2017-04-19 20:25:48 +00:00
|
|
|
auto hash = openssl::Sha1(authKey);
|
|
|
|
return (gsl::to_integer<uint64>(hash[19]) << 56)
|
|
|
|
| (gsl::to_integer<uint64>(hash[18]) << 48)
|
|
|
|
| (gsl::to_integer<uint64>(hash[17]) << 40)
|
|
|
|
| (gsl::to_integer<uint64>(hash[16]) << 32)
|
|
|
|
| (gsl::to_integer<uint64>(hash[15]) << 24)
|
|
|
|
| (gsl::to_integer<uint64>(hash[14]) << 16)
|
|
|
|
| (gsl::to_integer<uint64>(hash[13]) << 8)
|
|
|
|
| (gsl::to_integer<uint64>(hash[12]));
|
|
|
|
}
|
|
|
|
|
2020-05-14 14:12:07 +00:00
|
|
|
[[nodiscard]] QVector<MTPstring> WrapVersions(
|
|
|
|
const std::vector<std::string> &data) {
|
|
|
|
auto result = QVector<MTPstring>();
|
|
|
|
result.reserve(data.size());
|
|
|
|
for (const auto &version : data) {
|
|
|
|
result.push_back(MTP_string(version));
|
2018-05-24 16:42:57 +00:00
|
|
|
}
|
2020-05-14 14:12:07 +00:00
|
|
|
return result;
|
2018-05-24 16:42:57 +00:00
|
|
|
}
|
|
|
|
|
2020-05-14 14:12:07 +00:00
|
|
|
[[nodiscard]] QVector<MTPstring> CollectVersionsForApi() {
|
2020-07-09 17:38:26 +00:00
|
|
|
return WrapVersions(tgcalls::Meta::Versions() | ranges::action::reverse);
|
|
|
|
}
|
|
|
|
|
2020-08-11 09:59:48 +00:00
|
|
|
[[nodiscard]] Webrtc::VideoState StartVideoState(bool enabled) {
|
|
|
|
using State = Webrtc::VideoState;
|
2020-08-05 12:11:18 +00:00
|
|
|
return enabled ? State::Active : State::Inactive;
|
|
|
|
}
|
|
|
|
|
2020-05-14 14:12:07 +00:00
|
|
|
} // namespace
|
2018-05-24 16:42:57 +00:00
|
|
|
|
2018-05-06 21:29:53 +00:00
|
|
|
Call::Call(
|
|
|
|
not_null<Delegate*> delegate,
|
|
|
|
not_null<UserData*> user,
|
2020-07-31 17:36:20 +00:00
|
|
|
Type type,
|
|
|
|
bool video)
|
2017-04-19 18:06:01 +00:00
|
|
|
: _delegate(delegate)
|
2017-04-19 20:25:48 +00:00
|
|
|
, _user(user)
|
2020-06-17 09:36:25 +00:00
|
|
|
, _api(&_user->session().mtp())
|
2020-07-31 14:36:35 +00:00
|
|
|
, _type(type)
|
2020-08-11 09:59:48 +00:00
|
|
|
, _videoIncoming(std::make_unique<Webrtc::VideoTrack>(StartVideoState(video)))
|
|
|
|
, _videoOutgoing(std::make_unique<Webrtc::VideoTrack>(StartVideoState(video))) {
|
2020-07-31 14:36:35 +00:00
|
|
|
_discardByTimeoutTimer.setCallback([=] { hangup(); });
|
2017-04-29 18:00:27 +00:00
|
|
|
|
2017-04-19 20:25:48 +00:00
|
|
|
if (_type == Type::Outgoing) {
|
|
|
|
setState(State::Requesting);
|
2017-05-12 15:53:59 +00:00
|
|
|
} else {
|
|
|
|
startWaitingTrack();
|
2017-04-19 20:25:48 +00:00
|
|
|
}
|
2020-07-31 17:36:20 +00:00
|
|
|
setupOutgoingVideo();
|
2017-04-19 20:25:48 +00:00
|
|
|
}
|
|
|
|
|
2018-03-27 12:16:00 +00:00
|
|
|
void Call::generateModExpFirst(bytes::const_span randomSeed) {
|
2017-04-24 12:16:38 +00:00
|
|
|
auto first = MTP::CreateModExp(_dhConfig.g, _dhConfig.p, randomSeed);
|
|
|
|
if (first.modexp.empty()) {
|
|
|
|
LOG(("Call Error: Could not compute mod-exp first."));
|
2017-05-09 12:06:21 +00:00
|
|
|
finish(FinishType::Failed);
|
2017-04-24 12:16:38 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-03-27 12:16:00 +00:00
|
|
|
_randomPower = std::move(first.randomPower);
|
2017-04-24 12:16:38 +00:00
|
|
|
if (_type == Type::Incoming) {
|
|
|
|
_gb = std::move(first.modexp);
|
|
|
|
} else {
|
|
|
|
_ga = std::move(first.modexp);
|
|
|
|
_gaHash = openssl::Sha256(_ga);
|
2017-04-19 20:25:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-08 09:26:43 +00:00
|
|
|
bool Call::isIncomingWaiting() const {
|
|
|
|
if (type() != Call::Type::Incoming) {
|
|
|
|
return false;
|
|
|
|
}
|
2020-06-25 17:57:36 +00:00
|
|
|
return (state() == State::Starting)
|
|
|
|
|| (state() == State::WaitingIncoming);
|
2017-05-08 09:26:43 +00:00
|
|
|
}
|
|
|
|
|
2018-03-27 12:16:00 +00:00
|
|
|
void Call::start(bytes::const_span random) {
|
2017-04-19 18:06:01 +00:00
|
|
|
// Save config here, because it is possible that it changes between
|
|
|
|
// different usages inside the same call.
|
|
|
|
_dhConfig = _delegate->getDhConfig();
|
2017-08-17 09:06:26 +00:00
|
|
|
Assert(_dhConfig.g != 0);
|
|
|
|
Assert(!_dhConfig.p.empty());
|
2017-04-19 18:06:01 +00:00
|
|
|
|
2017-04-24 12:16:38 +00:00
|
|
|
generateModExpFirst(random);
|
2020-06-25 17:57:36 +00:00
|
|
|
const auto state = _state.current();
|
|
|
|
if (state == State::Starting || state == State::Requesting) {
|
2017-04-24 12:16:38 +00:00
|
|
|
if (_type == Type::Outgoing) {
|
|
|
|
startOutgoing();
|
|
|
|
} else {
|
|
|
|
startIncoming();
|
|
|
|
}
|
2020-06-25 17:57:36 +00:00
|
|
|
} else if (state == State::ExchangingKeys
|
2018-12-25 08:21:00 +00:00
|
|
|
&& _answerAfterDhConfigReceived) {
|
2017-05-10 07:58:02 +00:00
|
|
|
answer();
|
2017-04-19 18:06:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-19 20:25:48 +00:00
|
|
|
void Call::startOutgoing() {
|
2017-04-24 12:16:38 +00:00
|
|
|
Expects(_type == Type::Outgoing);
|
2020-06-25 17:57:36 +00:00
|
|
|
Expects(_state.current() == State::Requesting);
|
2018-06-06 10:28:43 +00:00
|
|
|
Expects(_gaHash.size() == kSha256Size);
|
2017-04-19 18:06:01 +00:00
|
|
|
|
2020-07-31 17:36:20 +00:00
|
|
|
const auto flags = _videoCapture
|
|
|
|
? MTPphone_RequestCall::Flag::f_video
|
|
|
|
: MTPphone_RequestCall::Flag(0);
|
2019-11-27 08:02:56 +00:00
|
|
|
_api.request(MTPphone_RequestCall(
|
2020-07-31 17:36:20 +00:00
|
|
|
MTP_flags(flags),
|
2018-04-07 06:28:01 +00:00
|
|
|
_user->inputUser,
|
|
|
|
MTP_int(rand_value<int32>()),
|
|
|
|
MTP_bytes(_gaHash),
|
|
|
|
MTP_phoneCallProtocol(
|
|
|
|
MTP_flags(MTPDphoneCallProtocol::Flag::f_udp_p2p
|
|
|
|
| MTPDphoneCallProtocol::Flag::f_udp_reflector),
|
|
|
|
MTP_int(kMinLayer),
|
2020-07-09 17:38:26 +00:00
|
|
|
MTP_int(tgcalls::Meta::MaxLayer()),
|
2020-05-14 14:12:07 +00:00
|
|
|
MTP_vector(CollectVersionsForApi()))
|
2019-03-22 13:42:03 +00:00
|
|
|
)).done([=](const MTPphone_PhoneCall &result) {
|
2017-04-19 18:06:01 +00:00
|
|
|
Expects(result.type() == mtpc_phone_phoneCall);
|
2017-05-09 12:06:21 +00:00
|
|
|
|
|
|
|
setState(State::Waiting);
|
|
|
|
|
2017-04-19 18:06:01 +00:00
|
|
|
auto &call = result.c_phone_phoneCall();
|
2019-08-06 16:40:08 +00:00
|
|
|
_user->session().data().processUsers(call.vusers());
|
2019-07-05 13:38:38 +00:00
|
|
|
if (call.vphone_call().type() != mtpc_phoneCallWaiting) {
|
2017-04-19 20:25:48 +00:00
|
|
|
LOG(("Call Error: Expected phoneCallWaiting in response to phone.requestCall()"));
|
2017-05-09 12:06:21 +00:00
|
|
|
finish(FinishType::Failed);
|
2017-04-19 20:25:48 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-07-05 13:38:38 +00:00
|
|
|
auto &phoneCall = call.vphone_call();
|
2017-05-05 16:27:05 +00:00
|
|
|
auto &waitingCall = phoneCall.c_phoneCallWaiting();
|
2019-07-05 13:38:38 +00:00
|
|
|
_id = waitingCall.vid().v;
|
|
|
|
_accessHash = waitingCall.vaccess_hash().v;
|
2017-05-09 12:06:21 +00:00
|
|
|
if (_finishAfterRequestingCall != FinishType::None) {
|
|
|
|
if (_finishAfterRequestingCall == FinishType::Failed) {
|
|
|
|
finish(_finishAfterRequestingCall);
|
|
|
|
} else {
|
|
|
|
hangup();
|
|
|
|
}
|
2017-04-19 18:06:01 +00:00
|
|
|
return;
|
|
|
|
}
|
2017-04-19 20:25:48 +00:00
|
|
|
|
2020-06-17 09:36:25 +00:00
|
|
|
const auto &config = _user->session().serverConfig();
|
|
|
|
_discardByTimeoutTimer.callOnce(config.callReceiveTimeoutMs);
|
2017-05-05 16:27:05 +00:00
|
|
|
handleUpdate(phoneCall);
|
2017-04-19 18:06:01 +00:00
|
|
|
}).fail([this](const RPCError &error) {
|
2017-04-29 16:25:33 +00:00
|
|
|
handleRequestError(error);
|
2017-04-19 18:06:01 +00:00
|
|
|
}).send();
|
|
|
|
}
|
|
|
|
|
2017-04-19 20:25:48 +00:00
|
|
|
void Call::startIncoming() {
|
2017-04-24 12:16:38 +00:00
|
|
|
Expects(_type == Type::Incoming);
|
2020-06-25 17:57:36 +00:00
|
|
|
Expects(_state.current() == State::Starting);
|
2017-04-24 12:16:38 +00:00
|
|
|
|
2019-11-27 08:02:56 +00:00
|
|
|
_api.request(MTPphone_ReceivedCall(
|
|
|
|
MTP_inputPhoneCall(MTP_long(_id), MTP_long(_accessHash))
|
|
|
|
)).done([=](const MTPBool &result) {
|
2020-06-25 17:57:36 +00:00
|
|
|
if (_state.current() == State::Starting) {
|
2017-04-25 20:36:04 +00:00
|
|
|
setState(State::WaitingIncoming);
|
|
|
|
}
|
2019-11-27 08:02:56 +00:00
|
|
|
}).fail([=](const RPCError &error) {
|
2017-04-29 16:25:33 +00:00
|
|
|
handleRequestError(error);
|
2017-04-24 12:16:38 +00:00
|
|
|
}).send();
|
2017-04-19 20:25:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Call::answer() {
|
2020-05-17 09:12:27 +00:00
|
|
|
_delegate->requestPermissionsOrFail(crl::guard(this, [=] {
|
2018-10-17 06:09:59 +00:00
|
|
|
actuallyAnswer();
|
|
|
|
}));
|
2018-09-30 15:42:50 +00:00
|
|
|
}
|
2018-10-17 06:09:59 +00:00
|
|
|
|
2018-09-30 15:42:50 +00:00
|
|
|
void Call::actuallyAnswer() {
|
2017-04-19 20:25:48 +00:00
|
|
|
Expects(_type == Type::Incoming);
|
|
|
|
|
2020-06-25 17:57:36 +00:00
|
|
|
const auto state = _state.current();
|
|
|
|
if (state != State::Starting && state != State::WaitingIncoming) {
|
|
|
|
if (state != State::ExchangingKeys
|
2019-03-12 11:16:58 +00:00
|
|
|
|| !_answerAfterDhConfigReceived) {
|
2017-05-10 07:58:02 +00:00
|
|
|
return;
|
|
|
|
}
|
2017-04-25 20:36:04 +00:00
|
|
|
}
|
2017-04-19 20:25:48 +00:00
|
|
|
setState(State::ExchangingKeys);
|
2017-05-10 07:58:02 +00:00
|
|
|
if (_gb.empty()) {
|
|
|
|
_answerAfterDhConfigReceived = true;
|
|
|
|
return;
|
|
|
|
} else {
|
|
|
|
_answerAfterDhConfigReceived = false;
|
|
|
|
}
|
2019-11-27 08:02:56 +00:00
|
|
|
_api.request(MTPphone_AcceptCall(
|
2018-04-07 06:28:01 +00:00
|
|
|
MTP_inputPhoneCall(MTP_long(_id), MTP_long(_accessHash)),
|
|
|
|
MTP_bytes(_gb),
|
|
|
|
MTP_phoneCallProtocol(
|
|
|
|
MTP_flags(MTPDphoneCallProtocol::Flag::f_udp_p2p
|
|
|
|
| MTPDphoneCallProtocol::Flag::f_udp_reflector),
|
|
|
|
MTP_int(kMinLayer),
|
2020-07-09 17:38:26 +00:00
|
|
|
MTP_int(tgcalls::Meta::MaxLayer()),
|
2020-05-14 14:12:07 +00:00
|
|
|
MTP_vector(CollectVersionsForApi()))
|
2019-03-12 11:16:58 +00:00
|
|
|
)).done([=](const MTPphone_PhoneCall &result) {
|
2017-04-19 20:25:48 +00:00
|
|
|
Expects(result.type() == mtpc_phone_phoneCall);
|
2020-07-31 14:36:35 +00:00
|
|
|
|
2017-04-19 20:25:48 +00:00
|
|
|
auto &call = result.c_phone_phoneCall();
|
2019-08-06 16:40:08 +00:00
|
|
|
_user->session().data().processUsers(call.vusers());
|
2019-07-05 13:38:38 +00:00
|
|
|
if (call.vphone_call().type() != mtpc_phoneCallWaiting) {
|
2019-03-12 11:16:58 +00:00
|
|
|
LOG(("Call Error: "
|
|
|
|
"Not phoneCallWaiting in response to phone.acceptCall."));
|
2017-05-09 12:06:21 +00:00
|
|
|
finish(FinishType::Failed);
|
2017-04-19 20:25:48 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-07-05 13:38:38 +00:00
|
|
|
handleUpdate(call.vphone_call());
|
2019-03-12 11:16:58 +00:00
|
|
|
}).fail([=](const RPCError &error) {
|
2017-04-29 16:25:33 +00:00
|
|
|
handleRequestError(error);
|
2017-04-19 20:25:48 +00:00
|
|
|
}).send();
|
|
|
|
}
|
|
|
|
|
2020-07-16 16:23:55 +00:00
|
|
|
void Call::setMuted(bool mute) {
|
|
|
|
_muted = mute;
|
2020-07-09 17:38:26 +00:00
|
|
|
if (_instance) {
|
2020-07-16 16:23:55 +00:00
|
|
|
_instance->setMuteMicrophone(mute);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-31 14:36:35 +00:00
|
|
|
void Call::setupOutgoingVideo() {
|
2020-08-18 14:00:33 +00:00
|
|
|
static const auto hasDevices = [] {
|
|
|
|
return !Webrtc::GetVideoInputList().empty();
|
|
|
|
};
|
2020-08-05 12:11:18 +00:00
|
|
|
const auto started = _videoOutgoing->state();
|
2020-08-18 14:00:33 +00:00
|
|
|
if (!hasDevices()) {
|
|
|
|
_videoOutgoing->setState(Webrtc::VideoState::Inactive);
|
|
|
|
}
|
2020-08-05 12:11:18 +00:00
|
|
|
_videoOutgoing->stateValue(
|
2020-08-11 09:59:48 +00:00
|
|
|
) | rpl::start_with_next([=](Webrtc::VideoState state) {
|
2020-08-18 14:00:33 +00:00
|
|
|
if (state != Webrtc::VideoState::Inactive && !hasDevices()) {
|
|
|
|
_errors.fire({ ErrorType::NoCamera });
|
2020-08-17 09:55:09 +00:00
|
|
|
_videoOutgoing->setState(Webrtc::VideoState::Inactive);
|
|
|
|
} else if (_state.current() != State::Established
|
2020-08-05 12:11:18 +00:00
|
|
|
&& state != started
|
|
|
|
&& !_videoCapture) {
|
2020-08-18 14:00:33 +00:00
|
|
|
_errors.fire({ ErrorType::NotStartedCall });
|
2020-08-05 12:11:18 +00:00
|
|
|
_videoOutgoing->setState(started);
|
2020-08-18 14:00:33 +00:00
|
|
|
} else if (state != Webrtc::VideoState::Inactive
|
|
|
|
&& _instance
|
|
|
|
&& !_instance->supportsVideo()) {
|
|
|
|
_errors.fire({ ErrorType::NotVideoCall });
|
|
|
|
_videoOutgoing->setState(Webrtc::VideoState::Inactive);
|
2020-08-11 09:59:48 +00:00
|
|
|
} else if (state != Webrtc::VideoState::Inactive) {
|
2020-08-05 12:11:18 +00:00
|
|
|
// Paused not supported right now.
|
2020-08-11 13:31:27 +00:00
|
|
|
#ifndef DESKTOP_APP_DISABLE_WEBRTC_INTEGRATION
|
2020-08-11 09:59:48 +00:00
|
|
|
Assert(state == Webrtc::VideoState::Active);
|
2020-07-31 14:36:35 +00:00
|
|
|
if (!_videoCapture) {
|
2020-08-21 11:49:51 +00:00
|
|
|
_videoCapture = _delegate->getVideoCapture();
|
2020-08-05 12:11:18 +00:00
|
|
|
_videoCapture->setOutput(_videoOutgoing->sink());
|
2020-07-31 14:36:35 +00:00
|
|
|
}
|
|
|
|
if (_instance) {
|
2020-08-05 12:11:18 +00:00
|
|
|
_instance->setVideoCapture(_videoCapture);
|
2020-07-31 14:36:35 +00:00
|
|
|
}
|
2020-08-05 12:11:18 +00:00
|
|
|
_videoCapture->setState(tgcalls::VideoState::Active);
|
2020-08-11 13:31:27 +00:00
|
|
|
#endif // DESKTOP_APP_DISABLE_WEBRTC_INTEGRATION
|
2020-07-31 14:36:35 +00:00
|
|
|
} else if (_videoCapture) {
|
2020-08-05 12:11:18 +00:00
|
|
|
_videoCapture->setState(tgcalls::VideoState::Inactive);
|
2020-07-16 16:23:55 +00:00
|
|
|
}
|
2020-07-31 14:36:35 +00:00
|
|
|
}, _lifetime);
|
|
|
|
}
|
|
|
|
|
2020-08-11 09:59:48 +00:00
|
|
|
not_null<Webrtc::VideoTrack*> Call::videoIncoming() const {
|
2020-07-31 14:36:35 +00:00
|
|
|
return _videoIncoming.get();
|
|
|
|
}
|
|
|
|
|
2020-08-11 09:59:48 +00:00
|
|
|
not_null<Webrtc::VideoTrack*> Call::videoOutgoing() const {
|
2020-07-31 14:36:35 +00:00
|
|
|
return _videoOutgoing.get();
|
2017-04-25 16:45:41 +00:00
|
|
|
}
|
|
|
|
|
2019-02-19 06:57:53 +00:00
|
|
|
crl::time Call::getDurationMs() const {
|
|
|
|
return _startTime ? (crl::now() - _startTime) : 0;
|
2017-04-19 20:25:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Call::hangup() {
|
2020-06-25 17:57:36 +00:00
|
|
|
const auto state = _state.current();
|
|
|
|
if (state == State::Busy) {
|
2017-05-04 13:32:56 +00:00
|
|
|
_delegate->callFinished(this);
|
2017-04-29 18:00:27 +00:00
|
|
|
} else {
|
2020-06-25 17:57:36 +00:00
|
|
|
auto missed = (state == State::Ringing || (state == State::Waiting && _type == Type::Outgoing));
|
2017-06-11 20:26:13 +00:00
|
|
|
auto declined = isIncomingWaiting();
|
2017-04-29 18:00:27 +00:00
|
|
|
auto reason = missed ? MTP_phoneCallDiscardReasonMissed() :
|
|
|
|
declined ? MTP_phoneCallDiscardReasonBusy() : MTP_phoneCallDiscardReasonHangup();
|
2017-05-09 12:06:21 +00:00
|
|
|
finish(FinishType::Ended, reason);
|
2017-04-29 18:00:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Call::redial() {
|
2020-06-25 17:57:36 +00:00
|
|
|
if (_state.current() != State::Busy) {
|
2017-04-29 18:00:27 +00:00
|
|
|
return;
|
|
|
|
}
|
2020-07-09 17:38:26 +00:00
|
|
|
Assert(_instance == nullptr);
|
2017-04-29 18:00:27 +00:00
|
|
|
_type = Type::Outgoing;
|
|
|
|
setState(State::Requesting);
|
2017-05-10 07:58:02 +00:00
|
|
|
_answerAfterDhConfigReceived = false;
|
2017-05-03 13:43:01 +00:00
|
|
|
startWaitingTrack();
|
2017-04-29 18:00:27 +00:00
|
|
|
_delegate->callRedial(this);
|
2017-04-19 20:25:48 +00:00
|
|
|
}
|
|
|
|
|
2017-05-04 12:28:37 +00:00
|
|
|
QString Call::getDebugLog() const {
|
2020-08-17 06:43:51 +00:00
|
|
|
return _instance
|
|
|
|
? QString::fromStdString(_instance->getDebugInfo())
|
|
|
|
: QString();
|
2017-05-04 12:28:37 +00:00
|
|
|
}
|
|
|
|
|
2017-05-03 13:43:01 +00:00
|
|
|
void Call::startWaitingTrack() {
|
|
|
|
_waitingTrack = Media::Audio::Current().createTrack();
|
2020-06-18 18:04:16 +00:00
|
|
|
auto trackFileName = Core::App().settings().getSoundPath(
|
2018-01-04 10:22:53 +00:00
|
|
|
(_type == Type::Outgoing)
|
|
|
|
? qsl("call_outgoing")
|
|
|
|
: qsl("call_incoming"));
|
2017-05-07 19:09:20 +00:00
|
|
|
_waitingTrack->samplePeakEach(kSoundSampleMs);
|
2017-05-03 13:43:01 +00:00
|
|
|
_waitingTrack->fillFromFile(trackFileName);
|
|
|
|
_waitingTrack->playInLoop();
|
|
|
|
}
|
|
|
|
|
2020-05-19 06:33:57 +00:00
|
|
|
void Call::sendSignalingData(const QByteArray &data) {
|
|
|
|
_api.request(MTPphone_SendSignalingData(
|
|
|
|
MTP_inputPhoneCall(
|
|
|
|
MTP_long(_id),
|
|
|
|
MTP_long(_accessHash)),
|
|
|
|
MTP_bytes(data)
|
|
|
|
)).done([=](const MTPBool &result) {
|
|
|
|
if (!mtpIsTrue(result)) {
|
|
|
|
finish(FinishType::Failed);
|
|
|
|
}
|
|
|
|
}).fail([=](const RPCError &error) {
|
|
|
|
handleRequestError(error);
|
|
|
|
}).send();
|
|
|
|
}
|
|
|
|
|
2017-05-07 19:09:20 +00:00
|
|
|
float64 Call::getWaitingSoundPeakValue() const {
|
|
|
|
if (_waitingTrack) {
|
2019-02-19 06:57:53 +00:00
|
|
|
auto when = crl::now() + kSoundSampleMs / 4;
|
2017-05-07 19:09:20 +00:00
|
|
|
return _waitingTrack->getPeakValue(when);
|
|
|
|
}
|
|
|
|
return 0.;
|
|
|
|
}
|
|
|
|
|
2017-04-25 16:45:41 +00:00
|
|
|
bool Call::isKeyShaForFingerprintReady() const {
|
|
|
|
return (_keyFingerprint != 0);
|
|
|
|
}
|
|
|
|
|
2018-03-27 12:16:00 +00:00
|
|
|
bytes::vector Call::getKeyShaForFingerprint() const {
|
2017-04-25 16:45:41 +00:00
|
|
|
Expects(isKeyShaForFingerprintReady());
|
|
|
|
Expects(!_ga.empty());
|
2018-03-27 12:16:00 +00:00
|
|
|
|
|
|
|
auto encryptedChatAuthKey = bytes::vector(_authKey.size() + _ga.size(), gsl::byte {});
|
|
|
|
bytes::copy(gsl::make_span(encryptedChatAuthKey).subspan(0, _authKey.size()), _authKey);
|
|
|
|
bytes::copy(gsl::make_span(encryptedChatAuthKey).subspan(_authKey.size(), _ga.size()), _ga);
|
2017-04-25 16:45:41 +00:00
|
|
|
return openssl::Sha256(encryptedChatAuthKey);
|
2017-04-19 20:25:48 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 18:06:01 +00:00
|
|
|
bool Call::handleUpdate(const MTPPhoneCall &call) {
|
|
|
|
switch (call.type()) {
|
2017-04-19 20:25:48 +00:00
|
|
|
case mtpc_phoneCallRequested: {
|
|
|
|
auto &data = call.c_phoneCallRequested();
|
|
|
|
if (_type != Type::Incoming
|
|
|
|
|| _id != 0
|
2019-07-05 13:38:38 +00:00
|
|
|
|| peerToUser(_user->id) != data.vadmin_id().v) {
|
2017-04-19 20:25:48 +00:00
|
|
|
Unexpected("phoneCallRequested call inside an existing call handleUpdate()");
|
|
|
|
}
|
2019-08-06 16:40:08 +00:00
|
|
|
if (_user->session().userId() != data.vparticipant_id().v) {
|
2019-03-12 11:16:58 +00:00
|
|
|
LOG(("Call Error: Wrong call participant_id %1, expected %2."
|
2019-07-05 13:38:38 +00:00
|
|
|
).arg(data.vparticipant_id().v
|
2019-08-06 16:40:08 +00:00
|
|
|
).arg(_user->session().userId()));
|
2017-05-09 12:06:21 +00:00
|
|
|
finish(FinishType::Failed);
|
2017-04-19 20:25:48 +00:00
|
|
|
return true;
|
|
|
|
}
|
2020-07-31 14:36:35 +00:00
|
|
|
|
2019-07-05 13:38:38 +00:00
|
|
|
_id = data.vid().v;
|
|
|
|
_accessHash = data.vaccess_hash().v;
|
|
|
|
auto gaHashBytes = bytes::make_span(data.vg_a_hash().v);
|
2018-06-06 10:28:43 +00:00
|
|
|
if (gaHashBytes.size() != kSha256Size) {
|
2019-03-12 11:16:58 +00:00
|
|
|
LOG(("Call Error: Wrong g_a_hash size %1, expected %2."
|
|
|
|
).arg(gaHashBytes.size()
|
|
|
|
).arg(kSha256Size));
|
2017-05-09 12:06:21 +00:00
|
|
|
finish(FinishType::Failed);
|
2017-04-19 20:25:48 +00:00
|
|
|
return true;
|
|
|
|
}
|
2018-06-06 10:28:43 +00:00
|
|
|
_gaHash = bytes::make_vector(gaHashBytes);
|
2017-04-19 20:25:48 +00:00
|
|
|
} return true;
|
2017-04-19 18:06:01 +00:00
|
|
|
|
|
|
|
case mtpc_phoneCallEmpty: {
|
|
|
|
auto &data = call.c_phoneCallEmpty();
|
2019-07-05 13:38:38 +00:00
|
|
|
if (data.vid().v != _id) {
|
2017-04-19 18:06:01 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
LOG(("Call Error: phoneCallEmpty received."));
|
2017-05-09 12:06:21 +00:00
|
|
|
finish(FinishType::Failed);
|
2017-04-19 18:06:01 +00:00
|
|
|
} return true;
|
|
|
|
|
|
|
|
case mtpc_phoneCallWaiting: {
|
|
|
|
auto &data = call.c_phoneCallWaiting();
|
2019-07-05 13:38:38 +00:00
|
|
|
if (data.vid().v != _id) {
|
2017-04-19 18:06:01 +00:00
|
|
|
return false;
|
|
|
|
}
|
2019-03-12 11:16:58 +00:00
|
|
|
if (_type == Type::Outgoing
|
2020-06-25 17:57:36 +00:00
|
|
|
&& _state.current() == State::Waiting
|
2019-07-05 13:38:38 +00:00
|
|
|
&& data.vreceive_date().value_or_empty() != 0) {
|
2020-06-17 09:36:25 +00:00
|
|
|
const auto &config = _user->session().serverConfig();
|
|
|
|
_discardByTimeoutTimer.callOnce(config.callRingTimeoutMs);
|
2017-04-25 16:45:41 +00:00
|
|
|
setState(State::Ringing);
|
2017-05-12 15:53:59 +00:00
|
|
|
startWaitingTrack();
|
2017-04-25 16:45:41 +00:00
|
|
|
}
|
2017-04-19 18:06:01 +00:00
|
|
|
} return true;
|
|
|
|
|
|
|
|
case mtpc_phoneCall: {
|
|
|
|
auto &data = call.c_phoneCall();
|
2019-07-05 13:38:38 +00:00
|
|
|
if (data.vid().v != _id) {
|
2017-04-19 18:06:01 +00:00
|
|
|
return false;
|
|
|
|
}
|
2018-12-25 08:21:00 +00:00
|
|
|
if (_type == Type::Incoming
|
2020-06-25 17:57:36 +00:00
|
|
|
&& _state.current() == State::ExchangingKeys
|
2020-07-09 17:38:26 +00:00
|
|
|
&& !_instance) {
|
2017-04-19 20:25:48 +00:00
|
|
|
startConfirmedCall(data);
|
|
|
|
}
|
2017-04-19 18:06:01 +00:00
|
|
|
} return true;
|
|
|
|
|
|
|
|
case mtpc_phoneCallDiscarded: {
|
|
|
|
auto &data = call.c_phoneCallDiscarded();
|
2019-07-05 13:38:38 +00:00
|
|
|
if (data.vid().v != _id) {
|
2017-04-19 18:06:01 +00:00
|
|
|
return false;
|
|
|
|
}
|
2017-04-19 20:25:48 +00:00
|
|
|
if (data.is_need_debug()) {
|
2020-07-09 17:38:26 +00:00
|
|
|
auto debugLog = _instance
|
|
|
|
? _instance->getDebugInfo()
|
2020-05-14 14:12:07 +00:00
|
|
|
: std::string();
|
2017-04-19 20:25:48 +00:00
|
|
|
if (!debugLog.empty()) {
|
2020-06-11 13:07:14 +00:00
|
|
|
user()->session().api().request(MTPphone_SaveCallDebug(
|
|
|
|
MTP_inputPhoneCall(
|
|
|
|
MTP_long(_id),
|
|
|
|
MTP_long(_accessHash)),
|
|
|
|
MTP_dataJSON(MTP_string(debugLog))
|
|
|
|
)).send();
|
2017-04-19 20:25:48 +00:00
|
|
|
}
|
|
|
|
}
|
2017-05-02 11:56:39 +00:00
|
|
|
if (data.is_need_rating() && _id && _accessHash) {
|
2019-08-06 16:40:08 +00:00
|
|
|
Ui::show(Box<RateCallBox>(&_user->session(), _id, _accessHash));
|
2017-05-02 11:56:39 +00:00
|
|
|
}
|
2019-07-05 13:38:38 +00:00
|
|
|
const auto reason = data.vreason();
|
|
|
|
if (reason && reason->type() == mtpc_phoneCallDiscardReasonDisconnect) {
|
2017-04-29 16:25:33 +00:00
|
|
|
LOG(("Call Info: Discarded with DISCONNECT reason."));
|
|
|
|
}
|
2019-07-05 13:38:38 +00:00
|
|
|
if (reason && reason->type() == mtpc_phoneCallDiscardReasonBusy) {
|
2017-04-19 20:25:48 +00:00
|
|
|
setState(State::Busy);
|
2020-06-25 17:57:36 +00:00
|
|
|
} else if (_type == Type::Outgoing
|
|
|
|
|| _state.current() == State::HangingUp) {
|
2017-04-19 20:25:48 +00:00
|
|
|
setState(State::Ended);
|
2017-05-12 16:09:34 +00:00
|
|
|
} else {
|
|
|
|
setState(State::EndedByOtherDevice);
|
2017-04-19 20:25:48 +00:00
|
|
|
}
|
2017-04-19 18:06:01 +00:00
|
|
|
} return true;
|
|
|
|
|
|
|
|
case mtpc_phoneCallAccepted: {
|
|
|
|
auto &data = call.c_phoneCallAccepted();
|
2019-07-05 13:38:38 +00:00
|
|
|
if (data.vid().v != _id) {
|
2017-04-19 18:06:01 +00:00
|
|
|
return false;
|
|
|
|
}
|
2017-04-19 20:25:48 +00:00
|
|
|
if (_type != Type::Outgoing) {
|
2019-03-12 11:16:58 +00:00
|
|
|
LOG(("Call Error: "
|
|
|
|
"Unexpected phoneCallAccepted for an incoming call."));
|
2017-05-09 12:06:21 +00:00
|
|
|
finish(FinishType::Failed);
|
2017-04-19 20:25:48 +00:00
|
|
|
} else if (checkCallFields(data)) {
|
2017-04-19 18:06:01 +00:00
|
|
|
confirmAcceptedCall(data);
|
|
|
|
}
|
|
|
|
} return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
Unexpected("phoneCall type inside an existing call handleUpdate()");
|
|
|
|
}
|
|
|
|
|
2020-08-05 12:11:18 +00:00
|
|
|
void Call::updateRemoteMediaState(
|
|
|
|
tgcalls::AudioState audio,
|
|
|
|
tgcalls::VideoState video) {
|
|
|
|
_remoteAudioState = [&] {
|
|
|
|
using From = tgcalls::AudioState;
|
|
|
|
using To = RemoteAudioState;
|
|
|
|
switch (audio) {
|
|
|
|
case From::Active: return To::Active;
|
|
|
|
case From::Muted: return To::Muted;
|
|
|
|
}
|
|
|
|
Unexpected("Audio state in remoteMediaStateUpdated.");
|
|
|
|
}();
|
|
|
|
_videoIncoming->setState([&] {
|
|
|
|
using From = tgcalls::VideoState;
|
2020-08-11 09:59:48 +00:00
|
|
|
using To = Webrtc::VideoState;
|
2020-08-05 12:11:18 +00:00
|
|
|
switch (video) {
|
|
|
|
case From::Inactive: return To::Inactive;
|
|
|
|
case From::Paused: return To::Paused;
|
|
|
|
case From::Active: return To::Active;
|
|
|
|
}
|
|
|
|
Unexpected("Video state in remoteMediaStateUpdated.");
|
|
|
|
}());
|
|
|
|
}
|
|
|
|
|
2020-05-19 06:33:57 +00:00
|
|
|
bool Call::handleSignalingData(
|
|
|
|
const MTPDupdatePhoneCallSignalingData &data) {
|
2020-07-09 17:38:26 +00:00
|
|
|
if (data.vphone_call_id().v != _id || !_instance) {
|
2020-05-19 06:33:57 +00:00
|
|
|
return false;
|
|
|
|
}
|
2020-07-09 17:38:26 +00:00
|
|
|
auto prepared = ranges::view::all(
|
|
|
|
data.vdata().v
|
|
|
|
) | ranges::view::transform([](char byte) {
|
|
|
|
return static_cast<uint8_t>(byte);
|
|
|
|
}) | ranges::to_vector;
|
|
|
|
_instance->receiveSignalingData(std::move(prepared));
|
|
|
|
return true;
|
2020-05-19 06:33:57 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 18:06:01 +00:00
|
|
|
void Call::confirmAcceptedCall(const MTPDphoneCallAccepted &call) {
|
2017-04-19 20:25:48 +00:00
|
|
|
Expects(_type == Type::Outgoing);
|
|
|
|
|
2020-06-25 17:57:36 +00:00
|
|
|
if (_state.current() == State::ExchangingKeys
|
2020-07-09 17:38:26 +00:00
|
|
|
|| _instance) {
|
2019-03-12 11:16:58 +00:00
|
|
|
LOG(("Call Warning: Unexpected confirmAcceptedCall."));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-07-05 13:38:38 +00:00
|
|
|
const auto firstBytes = bytes::make_span(call.vg_b().v);
|
2019-03-12 11:16:58 +00:00
|
|
|
const auto computedAuthKey = MTP::CreateAuthKey(
|
|
|
|
firstBytes,
|
|
|
|
_randomPower,
|
|
|
|
_dhConfig.p);
|
2017-04-19 20:25:48 +00:00
|
|
|
if (computedAuthKey.empty()) {
|
|
|
|
LOG(("Call Error: Could not compute mod-exp final."));
|
2017-05-09 12:06:21 +00:00
|
|
|
finish(FinishType::Failed);
|
2017-04-19 20:25:48 +00:00
|
|
|
return;
|
|
|
|
}
|
2017-04-19 18:06:01 +00:00
|
|
|
|
2017-04-24 12:16:38 +00:00
|
|
|
MTP::AuthKey::FillData(_authKey, computedAuthKey);
|
2017-04-19 20:25:48 +00:00
|
|
|
_keyFingerprint = ComputeFingerprint(_authKey);
|
2017-04-19 18:06:01 +00:00
|
|
|
|
2017-04-19 20:25:48 +00:00
|
|
|
setState(State::ExchangingKeys);
|
2019-11-27 08:02:56 +00:00
|
|
|
_api.request(MTPphone_ConfirmCall(
|
2018-04-07 06:28:01 +00:00
|
|
|
MTP_inputPhoneCall(MTP_long(_id), MTP_long(_accessHash)),
|
|
|
|
MTP_bytes(_ga),
|
|
|
|
MTP_long(_keyFingerprint),
|
|
|
|
MTP_phoneCallProtocol(
|
|
|
|
MTP_flags(MTPDphoneCallProtocol::Flag::f_udp_p2p
|
|
|
|
| MTPDphoneCallProtocol::Flag::f_udp_reflector),
|
|
|
|
MTP_int(kMinLayer),
|
2020-07-09 17:38:26 +00:00
|
|
|
MTP_int(tgcalls::Meta::MaxLayer()),
|
2020-05-14 14:12:07 +00:00
|
|
|
MTP_vector(CollectVersionsForApi()))
|
2020-05-19 06:33:57 +00:00
|
|
|
)).done([=](const MTPphone_PhoneCall &result) {
|
2017-04-19 18:06:01 +00:00
|
|
|
Expects(result.type() == mtpc_phone_phoneCall);
|
2019-01-18 12:27:37 +00:00
|
|
|
|
2017-04-19 18:06:01 +00:00
|
|
|
auto &call = result.c_phone_phoneCall();
|
2019-08-06 16:40:08 +00:00
|
|
|
_user->session().data().processUsers(call.vusers());
|
2019-07-05 13:38:38 +00:00
|
|
|
if (call.vphone_call().type() != mtpc_phoneCall) {
|
2017-04-19 20:25:48 +00:00
|
|
|
LOG(("Call Error: Expected phoneCall in response to phone.confirmCall()"));
|
2017-05-09 12:06:21 +00:00
|
|
|
finish(FinishType::Failed);
|
2017-04-19 18:06:01 +00:00
|
|
|
return;
|
|
|
|
}
|
2017-04-19 20:25:48 +00:00
|
|
|
|
2019-07-05 13:38:38 +00:00
|
|
|
createAndStartController(call.vphone_call().c_phoneCall());
|
2020-05-19 06:33:57 +00:00
|
|
|
}).fail([=](const RPCError &error) {
|
2017-04-29 16:25:33 +00:00
|
|
|
handleRequestError(error);
|
2017-04-19 18:06:01 +00:00
|
|
|
}).send();
|
|
|
|
}
|
|
|
|
|
2017-04-19 20:25:48 +00:00
|
|
|
void Call::startConfirmedCall(const MTPDphoneCall &call) {
|
|
|
|
Expects(_type == Type::Incoming);
|
|
|
|
|
2019-07-05 13:38:38 +00:00
|
|
|
auto firstBytes = bytes::make_span(call.vg_a_or_b().v);
|
2017-04-19 20:25:48 +00:00
|
|
|
if (_gaHash != openssl::Sha256(firstBytes)) {
|
|
|
|
LOG(("Call Error: Wrong g_a hash received."));
|
2017-05-09 12:06:21 +00:00
|
|
|
finish(FinishType::Failed);
|
2017-04-19 20:25:48 +00:00
|
|
|
return;
|
|
|
|
}
|
2018-03-27 12:16:00 +00:00
|
|
|
_ga = bytes::vector(firstBytes.begin(), firstBytes.end());
|
2017-04-19 20:25:48 +00:00
|
|
|
|
2017-04-24 12:16:38 +00:00
|
|
|
auto computedAuthKey = MTP::CreateAuthKey(firstBytes, _randomPower, _dhConfig.p);
|
2017-04-19 20:25:48 +00:00
|
|
|
if (computedAuthKey.empty()) {
|
|
|
|
LOG(("Call Error: Could not compute mod-exp final."));
|
2017-05-09 12:06:21 +00:00
|
|
|
finish(FinishType::Failed);
|
2017-04-19 20:25:48 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-04-24 12:16:38 +00:00
|
|
|
MTP::AuthKey::FillData(_authKey, computedAuthKey);
|
2017-04-19 20:25:48 +00:00
|
|
|
_keyFingerprint = ComputeFingerprint(_authKey);
|
|
|
|
|
|
|
|
createAndStartController(call);
|
|
|
|
}
|
|
|
|
|
2017-04-19 18:06:01 +00:00
|
|
|
void Call::createAndStartController(const MTPDphoneCall &call) {
|
2017-04-28 17:16:14 +00:00
|
|
|
_discardByTimeoutTimer.cancel();
|
2020-07-21 17:20:36 +00:00
|
|
|
if (!checkCallFields(call) || _authKey.size() != 256) {
|
2017-04-19 18:06:01 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-05-14 14:12:07 +00:00
|
|
|
const auto &protocol = call.vprotocol().c_phoneCallProtocol();
|
2020-06-17 09:36:25 +00:00
|
|
|
const auto &serverConfig = _user->session().serverConfig();
|
2020-05-14 14:12:07 +00:00
|
|
|
|
2020-07-21 17:20:36 +00:00
|
|
|
auto encryptionKeyValue = std::make_shared<std::array<uint8_t, 256>>();
|
|
|
|
memcpy(encryptionKeyValue->data(), _authKey.data(), 256);
|
2020-07-14 17:45:34 +00:00
|
|
|
|
2020-08-18 07:23:45 +00:00
|
|
|
const auto &settings = Core::App().settings();
|
|
|
|
|
2020-07-09 17:38:26 +00:00
|
|
|
const auto weak = base::make_weak(this);
|
2020-07-30 10:32:37 +00:00
|
|
|
tgcalls::Descriptor descriptor = {
|
2020-07-09 17:38:26 +00:00
|
|
|
.config = tgcalls::Config{
|
|
|
|
.initializationTimeout = serverConfig.callConnectTimeoutMs / 1000.,
|
|
|
|
.receiveTimeout = serverConfig.callPacketTimeoutMs / 1000.,
|
|
|
|
.dataSaving = tgcalls::DataSaving::Never,
|
|
|
|
.enableP2P = call.is_p2p_allowed(),
|
|
|
|
.enableAEC = !Platform::IsMac10_7OrGreater(),
|
|
|
|
.enableNS = true,
|
|
|
|
.enableAGC = true,
|
|
|
|
.enableVolumeControl = true,
|
|
|
|
.maxApiLayer = protocol.vmax_layer().v,
|
|
|
|
},
|
2020-07-14 17:45:34 +00:00
|
|
|
.encryptionKey = tgcalls::EncryptionKey(
|
|
|
|
std::move(encryptionKeyValue),
|
|
|
|
(_type == Type::Outgoing)),
|
2020-08-18 07:23:45 +00:00
|
|
|
.mediaDevicesConfig = tgcalls::MediaDevicesConfig{
|
2020-08-18 14:00:33 +00:00
|
|
|
.audioInputId = settings.callInputDeviceId().toStdString(),
|
|
|
|
.audioOutputId = settings.callOutputDeviceId().toStdString(),
|
2020-08-21 10:50:13 +00:00
|
|
|
.inputVolume = 1.f,//settings.callInputVolume() / 100.f,
|
|
|
|
.outputVolume = 1.f,//settings.callOutputVolume() / 100.f,
|
2020-08-18 07:23:45 +00:00
|
|
|
},
|
2020-08-05 12:11:18 +00:00
|
|
|
.videoCapture = _videoCapture,
|
|
|
|
.stateUpdated = [=](tgcalls::State state) {
|
2020-07-09 17:38:26 +00:00
|
|
|
crl::on_main(weak, [=] {
|
2020-08-05 12:11:18 +00:00
|
|
|
handleControllerStateChange(state);
|
2020-07-09 17:38:26 +00:00
|
|
|
});
|
|
|
|
},
|
|
|
|
.signalBarsUpdated = [=](int count) {
|
|
|
|
crl::on_main(weak, [=] {
|
|
|
|
handleControllerBarCountChange(count);
|
|
|
|
});
|
|
|
|
},
|
2020-08-05 12:11:18 +00:00
|
|
|
.remoteMediaStateUpdated = [=](tgcalls::AudioState audio, tgcalls::VideoState video) {
|
2020-07-16 16:23:55 +00:00
|
|
|
crl::on_main(weak, [=] {
|
2020-08-05 12:11:18 +00:00
|
|
|
updateRemoteMediaState(audio, video);
|
2020-07-16 16:23:55 +00:00
|
|
|
});
|
2020-07-09 17:38:26 +00:00
|
|
|
},
|
|
|
|
.signalingDataEmitted = [=](const std::vector<uint8_t> &data) {
|
|
|
|
const auto bytes = QByteArray(
|
|
|
|
reinterpret_cast<const char*>(data.data()),
|
|
|
|
data.size());
|
|
|
|
crl::on_main(weak, [=] {
|
|
|
|
sendSignalingData(bytes);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
};
|
2018-06-05 13:32:26 +00:00
|
|
|
if (Logs::DebugEnabled()) {
|
2017-05-07 15:40:37 +00:00
|
|
|
auto callLogFolder = cWorkingDir() + qsl("DebugLogs");
|
|
|
|
auto callLogPath = callLogFolder + qsl("/last_call_log.txt");
|
2018-08-30 11:56:35 +00:00
|
|
|
auto callLogNative = QDir::toNativeSeparators(callLogPath);
|
|
|
|
#ifdef Q_OS_WIN
|
2020-07-09 17:38:26 +00:00
|
|
|
descriptor.config.logPath = callLogNative.toStdWString();
|
2018-08-30 11:56:35 +00:00
|
|
|
#else // Q_OS_WIN
|
|
|
|
const auto callLogUtf = QFile::encodeName(callLogNative);
|
2020-07-09 17:38:26 +00:00
|
|
|
descriptor.config.logPath.resize(callLogUtf.size());
|
2020-07-13 14:04:02 +00:00
|
|
|
ranges::copy(callLogUtf, descriptor.config.logPath.begin());
|
2018-08-30 11:56:35 +00:00
|
|
|
#endif // Q_OS_WIN
|
|
|
|
QFile(callLogPath).remove();
|
|
|
|
QDir().mkpath(callLogFolder);
|
2017-05-07 15:40:37 +00:00
|
|
|
}
|
2017-04-19 18:06:01 +00:00
|
|
|
|
2019-07-05 13:38:38 +00:00
|
|
|
for (const auto &connection : call.vconnections().v) {
|
2020-07-09 17:38:26 +00:00
|
|
|
AppendEndpoint(descriptor.endpoints, connection);
|
2017-04-19 18:06:01 +00:00
|
|
|
}
|
2020-08-04 18:31:30 +00:00
|
|
|
for (const auto &connection : call.vconnections().v) {
|
|
|
|
AppendServer(descriptor.rtcServers, connection);
|
|
|
|
}
|
2020-07-09 17:38:26 +00:00
|
|
|
|
2018-11-05 13:58:24 +00:00
|
|
|
if (Global::UseProxyForCalls()
|
2019-11-13 14:12:04 +00:00
|
|
|
&& (Global::ProxySettings() == MTP::ProxyData::Settings::Enabled)) {
|
2020-05-14 14:12:07 +00:00
|
|
|
const auto &selected = Global::SelectedProxy();
|
2020-07-09 17:38:26 +00:00
|
|
|
if (selected.supportsCalls() && !selected.host.isEmpty()) {
|
2020-05-14 14:12:07 +00:00
|
|
|
Assert(selected.type == MTP::ProxyData::Type::Socks5);
|
2020-07-09 17:38:26 +00:00
|
|
|
descriptor.proxy = std::make_unique<tgcalls::Proxy>();
|
|
|
|
descriptor.proxy->host = selected.host.toStdString();
|
|
|
|
descriptor.proxy->port = selected.port;
|
|
|
|
descriptor.proxy->login = selected.user.toStdString();
|
|
|
|
descriptor.proxy->password = selected.password.toStdString();
|
2018-05-06 21:29:53 +00:00
|
|
|
}
|
|
|
|
}
|
2020-05-14 14:12:07 +00:00
|
|
|
|
2020-05-19 06:33:57 +00:00
|
|
|
const auto version = call.vprotocol().match([&](
|
|
|
|
const MTPDphoneCallProtocol &data) {
|
|
|
|
return data.vlibrary_versions().v;
|
|
|
|
}).value(0, MTP_bytes(kDefaultVersion)).v;
|
2020-07-09 17:38:26 +00:00
|
|
|
|
|
|
|
LOG(("Call Info: Creating instance with version '%1', allowP2P: %2"
|
|
|
|
).arg(QString::fromUtf8(version)
|
|
|
|
).arg(Logs::b(descriptor.config.enableP2P)));
|
|
|
|
_instance = tgcalls::Meta::Create(
|
2020-05-19 06:33:57 +00:00
|
|
|
version.toStdString(),
|
2020-07-09 17:38:26 +00:00
|
|
|
std::move(descriptor));
|
|
|
|
if (!_instance) {
|
|
|
|
LOG(("Call Error: Wrong library version: %1."
|
|
|
|
).arg(QString::fromUtf8(version)));
|
|
|
|
finish(FinishType::Failed);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const auto raw = _instance.get();
|
2020-07-16 16:23:55 +00:00
|
|
|
if (_muted.current()) {
|
|
|
|
raw->setMuteMicrophone(_muted.current());
|
2020-05-14 14:12:07 +00:00
|
|
|
}
|
2020-07-17 18:59:38 +00:00
|
|
|
|
2020-07-31 14:36:35 +00:00
|
|
|
raw->setIncomingVideoOutput(_videoIncoming->sink());
|
2020-06-18 18:04:16 +00:00
|
|
|
raw->setAudioOutputDuckingEnabled(settings.callAudioDuckingEnabled());
|
2017-04-19 18:06:01 +00:00
|
|
|
}
|
|
|
|
|
2020-08-05 12:11:18 +00:00
|
|
|
void Call::handleControllerStateChange(tgcalls::State state) {
|
2017-04-19 18:06:01 +00:00
|
|
|
switch (state) {
|
2020-07-09 17:38:26 +00:00
|
|
|
case tgcalls::State::WaitInit: {
|
2017-04-19 20:25:48 +00:00
|
|
|
DEBUG_LOG(("Call Info: State changed to WaitingInit."));
|
2020-07-09 17:38:26 +00:00
|
|
|
setState(State::WaitingInit);
|
2017-04-19 18:06:01 +00:00
|
|
|
} break;
|
|
|
|
|
2020-07-09 17:38:26 +00:00
|
|
|
case tgcalls::State::WaitInitAck: {
|
2017-04-19 20:25:48 +00:00
|
|
|
DEBUG_LOG(("Call Info: State changed to WaitingInitAck."));
|
2020-07-09 17:38:26 +00:00
|
|
|
setState(State::WaitingInitAck);
|
2017-04-19 18:06:01 +00:00
|
|
|
} break;
|
|
|
|
|
2020-07-09 17:38:26 +00:00
|
|
|
case tgcalls::State::Established: {
|
2017-04-19 18:06:01 +00:00
|
|
|
DEBUG_LOG(("Call Info: State changed to Established."));
|
2020-07-09 17:38:26 +00:00
|
|
|
setState(State::Established);
|
2017-04-19 18:06:01 +00:00
|
|
|
} break;
|
|
|
|
|
2020-07-09 17:38:26 +00:00
|
|
|
case tgcalls::State::Failed: {
|
2020-08-17 06:43:51 +00:00
|
|
|
auto error = _instance
|
|
|
|
? QString::fromStdString(_instance->getLastError())
|
|
|
|
: QString();
|
2017-04-29 16:25:33 +00:00
|
|
|
LOG(("Call Info: State changed to Failed, error: %1.").arg(error));
|
2020-07-09 17:38:26 +00:00
|
|
|
handleControllerError(error);
|
2017-04-19 18:06:01 +00:00
|
|
|
} break;
|
|
|
|
|
2020-05-14 14:12:07 +00:00
|
|
|
default: LOG(("Call Error: Unexpected state in handleStateChange: %1"
|
|
|
|
).arg(int(state)));
|
2017-04-19 18:06:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-14 14:12:07 +00:00
|
|
|
void Call::handleControllerBarCountChange(int count) {
|
2020-07-09 17:38:26 +00:00
|
|
|
setSignalBarCount(count);
|
2018-05-27 08:24:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Call::setSignalBarCount(int count) {
|
2020-07-16 16:23:55 +00:00
|
|
|
_signalBarCount = count;
|
2018-05-27 08:24:47 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 20:25:48 +00:00
|
|
|
template <typename T>
|
|
|
|
bool Call::checkCallCommonFields(const T &call) {
|
2017-04-19 18:06:01 +00:00
|
|
|
auto checkFailed = [this] {
|
2017-05-09 12:06:21 +00:00
|
|
|
finish(FinishType::Failed);
|
2017-04-19 18:06:01 +00:00
|
|
|
return false;
|
|
|
|
};
|
2019-07-05 13:38:38 +00:00
|
|
|
if (call.vaccess_hash().v != _accessHash) {
|
2017-04-19 20:25:48 +00:00
|
|
|
LOG(("Call Error: Wrong call access_hash."));
|
2017-04-19 18:06:01 +00:00
|
|
|
return checkFailed();
|
|
|
|
}
|
2019-08-06 16:40:08 +00:00
|
|
|
auto adminId = (_type == Type::Outgoing) ? _user->session().userId() : peerToUser(_user->id);
|
|
|
|
auto participantId = (_type == Type::Outgoing) ? peerToUser(_user->id) : _user->session().userId();
|
2019-07-05 13:38:38 +00:00
|
|
|
if (call.vadmin_id().v != adminId) {
|
|
|
|
LOG(("Call Error: Wrong call admin_id %1, expected %2.").arg(call.vadmin_id().v).arg(adminId));
|
2017-04-19 18:06:01 +00:00
|
|
|
return checkFailed();
|
|
|
|
}
|
2019-07-05 13:38:38 +00:00
|
|
|
if (call.vparticipant_id().v != participantId) {
|
|
|
|
LOG(("Call Error: Wrong call participant_id %1, expected %2.").arg(call.vparticipant_id().v).arg(participantId));
|
2017-04-19 18:06:01 +00:00
|
|
|
return checkFailed();
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Call::checkCallFields(const MTPDphoneCall &call) {
|
|
|
|
if (!checkCallCommonFields(call)) {
|
|
|
|
return false;
|
|
|
|
}
|
2019-07-05 13:38:38 +00:00
|
|
|
if (call.vkey_fingerprint().v != _keyFingerprint) {
|
2017-04-19 20:25:48 +00:00
|
|
|
LOG(("Call Error: Wrong call fingerprint."));
|
2017-05-09 12:06:21 +00:00
|
|
|
finish(FinishType::Failed);
|
2017-04-19 18:06:01 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Call::checkCallFields(const MTPDphoneCallAccepted &call) {
|
|
|
|
return checkCallCommonFields(call);
|
|
|
|
}
|
|
|
|
|
2017-04-19 20:25:48 +00:00
|
|
|
void Call::setState(State state) {
|
2020-06-25 17:57:36 +00:00
|
|
|
if (_state.current() == State::Failed) {
|
2017-05-09 12:06:21 +00:00
|
|
|
return;
|
|
|
|
}
|
2020-06-25 17:57:36 +00:00
|
|
|
if (_state.current() == State::FailedHangingUp && state != State::Failed) {
|
2017-05-09 12:06:21 +00:00
|
|
|
return;
|
|
|
|
}
|
2020-06-25 17:57:36 +00:00
|
|
|
if (_state.current() != state) {
|
2017-04-19 20:25:48 +00:00
|
|
|
_state = state;
|
|
|
|
|
2017-05-03 13:43:01 +00:00
|
|
|
if (true
|
2020-06-25 17:57:36 +00:00
|
|
|
&& state != State::Starting
|
|
|
|
&& state != State::Requesting
|
|
|
|
&& state != State::Waiting
|
|
|
|
&& state != State::WaitingIncoming
|
|
|
|
&& state != State::Ringing) {
|
2017-05-03 13:43:01 +00:00
|
|
|
_waitingTrack.reset();
|
|
|
|
}
|
2017-05-05 11:49:23 +00:00
|
|
|
if (false
|
2020-06-25 17:57:36 +00:00
|
|
|
|| state == State::Ended
|
|
|
|
|| state == State::EndedByOtherDevice
|
|
|
|
|| state == State::Failed
|
|
|
|
|| state == State::Busy) {
|
2017-05-05 11:49:23 +00:00
|
|
|
// Destroy controller before destroying Call Panel,
|
|
|
|
// so that the panel hide animation is smooth.
|
|
|
|
destroyController();
|
|
|
|
}
|
2020-06-25 17:57:36 +00:00
|
|
|
switch (state) {
|
2017-04-19 20:25:48 +00:00
|
|
|
case State::Established:
|
2019-02-19 06:57:53 +00:00
|
|
|
_startTime = crl::now();
|
2017-04-19 20:25:48 +00:00
|
|
|
break;
|
2017-05-03 13:43:01 +00:00
|
|
|
case State::ExchangingKeys:
|
|
|
|
_delegate->playSound(Delegate::Sound::Connecting);
|
|
|
|
break;
|
2017-04-19 20:25:48 +00:00
|
|
|
case State::Ended:
|
2017-05-04 13:32:56 +00:00
|
|
|
_delegate->playSound(Delegate::Sound::Ended);
|
2017-09-28 09:40:26 +00:00
|
|
|
[[fallthrough]];
|
2017-05-12 16:09:34 +00:00
|
|
|
case State::EndedByOtherDevice:
|
2017-04-19 20:25:48 +00:00
|
|
|
_delegate->callFinished(this);
|
|
|
|
break;
|
|
|
|
case State::Failed:
|
2017-05-04 13:32:56 +00:00
|
|
|
_delegate->playSound(Delegate::Sound::Ended);
|
2017-04-19 20:25:48 +00:00
|
|
|
_delegate->callFailed(this);
|
|
|
|
break;
|
|
|
|
case State::Busy:
|
2017-05-03 13:43:01 +00:00
|
|
|
_delegate->playSound(Delegate::Sound::Busy);
|
2017-04-19 20:25:48 +00:00
|
|
|
break;
|
|
|
|
}
|
2017-04-19 18:06:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-21 10:50:13 +00:00
|
|
|
void Call::setCurrentAudioDevice(bool input, const QString &deviceId) {
|
2020-07-09 17:38:26 +00:00
|
|
|
if (_instance) {
|
2020-08-21 10:50:13 +00:00
|
|
|
const auto id = deviceId.toStdString();
|
2019-01-05 11:08:02 +00:00
|
|
|
if (input) {
|
2020-08-21 10:50:13 +00:00
|
|
|
_instance->setAudioInputDevice(id);
|
2019-01-05 11:08:02 +00:00
|
|
|
} else {
|
2020-08-21 10:50:13 +00:00
|
|
|
_instance->setAudioOutputDevice(id);
|
2019-01-05 11:08:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-21 10:50:13 +00:00
|
|
|
void Call::setCurrentVideoDevice(const QString &deviceId) {
|
2020-08-18 14:00:33 +00:00
|
|
|
if (_videoCapture) {
|
2020-08-21 10:50:13 +00:00
|
|
|
_videoCapture->switchToDevice(deviceId.toStdString());
|
2020-08-18 14:00:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-14 14:12:07 +00:00
|
|
|
void Call::setAudioVolume(bool input, float level) {
|
2020-07-09 17:38:26 +00:00
|
|
|
if (_instance) {
|
2020-05-14 14:12:07 +00:00
|
|
|
if (input) {
|
2020-07-09 17:38:26 +00:00
|
|
|
_instance->setInputVolume(level);
|
2019-01-05 11:08:02 +00:00
|
|
|
} else {
|
2020-07-09 17:38:26 +00:00
|
|
|
_instance->setOutputVolume(level);
|
2019-01-05 11:08:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-14 14:12:07 +00:00
|
|
|
void Call::setAudioDuckingEnabled(bool enabled) {
|
2020-07-09 17:38:26 +00:00
|
|
|
if (_instance) {
|
|
|
|
_instance->setAudioOutputDuckingEnabled(enabled);
|
2019-01-05 11:08:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-09 12:06:21 +00:00
|
|
|
void Call::finish(FinishType type, const MTPPhoneCallDiscardReason &reason) {
|
|
|
|
Expects(type != FinishType::None);
|
2018-05-27 08:24:47 +00:00
|
|
|
|
|
|
|
setSignalBarCount(kSignalBarFinished);
|
|
|
|
|
2017-05-09 12:06:21 +00:00
|
|
|
auto finalState = (type == FinishType::Ended) ? State::Ended : State::Failed;
|
|
|
|
auto hangupState = (type == FinishType::Ended) ? State::HangingUp : State::FailedHangingUp;
|
2020-06-25 17:57:36 +00:00
|
|
|
const auto state = _state.current();
|
|
|
|
if (state == State::Requesting) {
|
2017-05-09 12:06:21 +00:00
|
|
|
_finishByTimeoutTimer.call(kHangupTimeoutMs, [this, finalState] { setState(finalState); });
|
|
|
|
_finishAfterRequestingCall = type;
|
2017-04-19 20:25:48 +00:00
|
|
|
return;
|
|
|
|
}
|
2020-06-25 17:57:36 +00:00
|
|
|
if (state == State::HangingUp
|
|
|
|
|| state == State::FailedHangingUp
|
|
|
|
|| state == State::EndedByOtherDevice
|
|
|
|
|| state == State::Ended
|
|
|
|
|| state == State::Failed) {
|
2017-04-19 20:25:48 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!_id) {
|
2017-05-09 12:06:21 +00:00
|
|
|
setState(finalState);
|
2017-04-19 20:25:48 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-05-09 12:06:21 +00:00
|
|
|
setState(hangupState);
|
2017-04-25 16:45:41 +00:00
|
|
|
auto duration = getDurationMs() / 1000;
|
2020-07-09 17:38:26 +00:00
|
|
|
auto connectionId = _instance ? _instance->getPreferredRelayId() : 0;
|
2017-05-09 12:06:21 +00:00
|
|
|
_finishByTimeoutTimer.call(kHangupTimeoutMs, [this, finalState] { setState(finalState); });
|
2020-08-11 09:59:48 +00:00
|
|
|
const auto flags = ((_videoIncoming->state() != Webrtc::VideoState::Inactive)
|
|
|
|
|| (_videoOutgoing->state() != Webrtc::VideoState::Inactive))
|
2020-07-31 16:56:18 +00:00
|
|
|
? MTPphone_DiscardCall::Flag::f_video
|
|
|
|
: MTPphone_DiscardCall::Flag(0);
|
2019-11-27 08:02:56 +00:00
|
|
|
_api.request(MTPphone_DiscardCall(
|
2020-07-31 16:56:18 +00:00
|
|
|
MTP_flags(flags),
|
2018-06-07 18:04:51 +00:00
|
|
|
MTP_inputPhoneCall(
|
|
|
|
MTP_long(_id),
|
|
|
|
MTP_long(_accessHash)),
|
|
|
|
MTP_int(duration),
|
|
|
|
reason,
|
|
|
|
MTP_long(connectionId)
|
|
|
|
)).done([=](const MTPUpdates &result) {
|
2019-08-06 16:40:08 +00:00
|
|
|
// Here 'this' could be destroyed by updates, so we set Ended after
|
2017-04-19 20:25:48 +00:00
|
|
|
// updates being handled, but in a guarded way.
|
2018-06-07 18:04:51 +00:00
|
|
|
crl::on_main(this, [=] { setState(finalState); });
|
2019-08-06 16:40:08 +00:00
|
|
|
_user->session().api().applyUpdates(result);
|
2017-05-09 12:06:21 +00:00
|
|
|
}).fail([this, finalState](const RPCError &error) {
|
|
|
|
setState(finalState);
|
2017-04-19 20:25:48 +00:00
|
|
|
}).send();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Call::setStateQueued(State state) {
|
2018-06-07 18:04:51 +00:00
|
|
|
crl::on_main(this, [=] {
|
2018-05-27 08:24:47 +00:00
|
|
|
setState(state);
|
|
|
|
});
|
2017-04-19 18:06:01 +00:00
|
|
|
}
|
|
|
|
|
2020-05-14 14:12:07 +00:00
|
|
|
void Call::setFailedQueued(const QString &error) {
|
2018-06-07 18:04:51 +00:00
|
|
|
crl::on_main(this, [=] {
|
2018-05-27 08:24:47 +00:00
|
|
|
handleControllerError(error);
|
|
|
|
});
|
2017-04-29 16:25:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Call::handleRequestError(const RPCError &error) {
|
|
|
|
if (error.type() == qstr("USER_PRIVACY_RESTRICTED")) {
|
2019-09-13 06:06:02 +00:00
|
|
|
Ui::show(Box<InformBox>(tr::lng_call_error_not_available(tr::now, lt_user, _user->name)));
|
2017-04-29 16:25:33 +00:00
|
|
|
} else if (error.type() == qstr("PARTICIPANT_VERSION_OUTDATED")) {
|
2019-09-13 06:06:02 +00:00
|
|
|
Ui::show(Box<InformBox>(tr::lng_call_error_outdated(tr::now, lt_user, _user->name)));
|
2017-04-29 16:25:33 +00:00
|
|
|
} else if (error.type() == qstr("CALL_PROTOCOL_LAYER_INVALID")) {
|
2019-09-13 06:06:02 +00:00
|
|
|
Ui::show(Box<InformBox>(Lang::Hard::CallErrorIncompatible().replace("{user}", _user->name)));
|
2017-04-29 16:25:33 +00:00
|
|
|
}
|
2017-05-09 12:06:21 +00:00
|
|
|
finish(FinishType::Failed);
|
2017-04-29 16:25:33 +00:00
|
|
|
}
|
|
|
|
|
2020-05-14 14:12:07 +00:00
|
|
|
void Call::handleControllerError(const QString &error) {
|
|
|
|
if (error == u"ERROR_INCOMPATIBLE"_q) {
|
2018-05-24 16:42:57 +00:00
|
|
|
Ui::show(Box<InformBox>(
|
|
|
|
Lang::Hard::CallErrorIncompatible().replace(
|
|
|
|
"{user}",
|
2019-09-13 06:06:02 +00:00
|
|
|
_user->name)));
|
2020-05-14 14:12:07 +00:00
|
|
|
} else if (error == u"ERROR_AUDIO_IO"_q) {
|
2019-06-19 15:09:03 +00:00
|
|
|
Ui::show(Box<InformBox>(tr::lng_call_error_audio_io(tr::now)));
|
2017-04-29 16:25:33 +00:00
|
|
|
}
|
2017-05-09 12:06:21 +00:00
|
|
|
finish(FinishType::Failed);
|
2017-04-29 16:25:33 +00:00
|
|
|
}
|
|
|
|
|
2017-04-29 18:00:27 +00:00
|
|
|
void Call::destroyController() {
|
2020-07-09 17:38:26 +00:00
|
|
|
if (_instance) {
|
2020-08-25 18:03:41 +00:00
|
|
|
_instance->stop([](tgcalls::FinalState) {
|
|
|
|
});
|
2020-07-09 17:38:26 +00:00
|
|
|
|
2017-04-19 20:25:48 +00:00
|
|
|
DEBUG_LOG(("Call Info: Destroying call controller.."));
|
2020-07-09 17:38:26 +00:00
|
|
|
_instance.reset();
|
2017-04-19 20:25:48 +00:00
|
|
|
DEBUG_LOG(("Call Info: Call controller destroyed."));
|
|
|
|
}
|
2018-05-27 08:24:47 +00:00
|
|
|
setSignalBarCount(kSignalBarFinished);
|
2017-04-19 18:06:01 +00:00
|
|
|
}
|
|
|
|
|
2017-04-29 18:00:27 +00:00
|
|
|
Call::~Call() {
|
|
|
|
destroyController();
|
|
|
|
}
|
|
|
|
|
2020-05-14 14:12:07 +00:00
|
|
|
void UpdateConfig(const std::string &data) {
|
2020-07-09 17:38:26 +00:00
|
|
|
tgcalls::SetLegacyGlobalServerConfig(data);
|
2017-04-28 15:00:16 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 18:06:01 +00:00
|
|
|
} // namespace Calls
|