2017-04-19 09:44:07 +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 09:44:07 +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 09:44:07 +00:00
|
|
|
*/
|
|
|
|
#include "calls/calls_instance.h"
|
|
|
|
|
|
|
|
#include "mtproto/connection.h"
|
2019-01-21 13:42:21 +00:00
|
|
|
#include "core/application.h"
|
2017-04-19 09:44:07 +00:00
|
|
|
#include "auth_session.h"
|
2017-04-29 16:25:33 +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-04-19 18:06:01 +00:00
|
|
|
#include "calls/calls_call.h"
|
2017-04-19 20:25:48 +00:00
|
|
|
#include "calls/calls_panel.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-02-13 12:36:59 +00:00
|
|
|
#include "media/audio/media_audio_track.h"
|
2018-09-30 15:42:50 +00:00
|
|
|
#include "platform/platform_specific.h"
|
|
|
|
#include "mainwidget.h"
|
2017-05-02 11:56:39 +00:00
|
|
|
#include "boxes/rate_call_box.h"
|
2019-01-04 11:09:48 +00:00
|
|
|
|
2017-04-19 09:44:07 +00:00
|
|
|
namespace Calls {
|
2017-04-28 15:00:16 +00:00
|
|
|
namespace {
|
|
|
|
|
2019-02-19 06:57:53 +00:00
|
|
|
constexpr auto kServerConfigUpdateTimeoutMs = 24 * 3600 * crl::time(1000);
|
2017-04-28 15:00:16 +00:00
|
|
|
|
|
|
|
} // namespace
|
2017-04-19 09:44:07 +00:00
|
|
|
|
|
|
|
Instance::Instance() = default;
|
|
|
|
|
2017-08-17 08:31:24 +00:00
|
|
|
void Instance::startOutgoingCall(not_null<UserData*> user) {
|
2017-05-04 12:29:32 +00:00
|
|
|
if (alreadyInCall()) { // Already in a call.
|
2017-04-27 21:17:00 +00:00
|
|
|
_currentCallPanel->showAndActivate();
|
2017-05-03 13:43:01 +00:00
|
|
|
return;
|
2017-04-19 09:44:07 +00:00
|
|
|
}
|
2017-04-29 16:25:33 +00:00
|
|
|
if (user->callsStatus() == UserData::CallsStatus::Private) {
|
|
|
|
// Request full user once more to refresh the setting in case it was changed.
|
2017-08-04 14:54:32 +00:00
|
|
|
Auth().api().requestFullPeer(user);
|
2017-04-29 16:25:33 +00:00
|
|
|
Ui::show(Box<InformBox>(lng_call_error_not_available(lt_user, App::peerName(user))));
|
|
|
|
return;
|
|
|
|
}
|
2018-10-17 06:09:59 +00:00
|
|
|
requestMicrophonePermissionOrFail(crl::guard(this, [=] {
|
2018-09-30 15:42:50 +00:00
|
|
|
createCall(user, Call::Type::Outgoing);
|
2018-10-17 06:09:59 +00:00
|
|
|
}));
|
2017-04-19 20:25:48 +00:00
|
|
|
}
|
2017-04-19 09:44:07 +00:00
|
|
|
|
2017-08-17 08:31:24 +00:00
|
|
|
void Instance::callFinished(not_null<Call*> call) {
|
2017-04-25 20:36:04 +00:00
|
|
|
destroyCall(call);
|
2017-04-19 20:25:48 +00:00
|
|
|
}
|
|
|
|
|
2017-08-17 08:31:24 +00:00
|
|
|
void Instance::callFailed(not_null<Call*> call) {
|
2017-04-25 20:36:04 +00:00
|
|
|
destroyCall(call);
|
|
|
|
}
|
|
|
|
|
2017-08-17 08:31:24 +00:00
|
|
|
void Instance::callRedial(not_null<Call*> call) {
|
2017-04-29 18:00:27 +00:00
|
|
|
if (_currentCall.get() == call) {
|
|
|
|
refreshDhConfig();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-03 13:43:01 +00:00
|
|
|
void Instance::playSound(Sound sound) {
|
|
|
|
switch (sound) {
|
|
|
|
case Sound::Busy: {
|
|
|
|
if (!_callBusyTrack) {
|
|
|
|
_callBusyTrack = Media::Audio::Current().createTrack();
|
2018-01-04 10:22:53 +00:00
|
|
|
_callBusyTrack->fillFromFile(
|
|
|
|
Auth().settings().getSoundPath(qsl("call_busy")));
|
2017-05-03 13:43:01 +00:00
|
|
|
}
|
|
|
|
_callBusyTrack->playOnce();
|
|
|
|
} break;
|
|
|
|
|
|
|
|
case Sound::Ended: {
|
|
|
|
if (!_callEndedTrack) {
|
|
|
|
_callEndedTrack = Media::Audio::Current().createTrack();
|
2018-01-04 10:22:53 +00:00
|
|
|
_callEndedTrack->fillFromFile(
|
|
|
|
Auth().settings().getSoundPath(qsl("call_end")));
|
2017-05-03 13:43:01 +00:00
|
|
|
}
|
|
|
|
_callEndedTrack->playOnce();
|
|
|
|
} break;
|
|
|
|
|
|
|
|
case Sound::Connecting: {
|
|
|
|
if (!_callConnectingTrack) {
|
|
|
|
_callConnectingTrack = Media::Audio::Current().createTrack();
|
2018-01-04 10:22:53 +00:00
|
|
|
_callConnectingTrack->fillFromFile(
|
|
|
|
Auth().settings().getSoundPath(qsl("call_connect")));
|
2017-05-03 13:43:01 +00:00
|
|
|
}
|
|
|
|
_callConnectingTrack->playOnce();
|
|
|
|
} break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-17 08:31:24 +00:00
|
|
|
void Instance::destroyCall(not_null<Call*> call) {
|
2017-04-19 20:25:48 +00:00
|
|
|
if (_currentCall.get() == call) {
|
2017-05-04 13:32:56 +00:00
|
|
|
destroyCurrentPanel();
|
2017-04-19 20:25:48 +00:00
|
|
|
_currentCall.reset();
|
2017-04-25 20:36:04 +00:00
|
|
|
_currentCallChanged.notify(nullptr, true);
|
2017-04-29 20:06:32 +00:00
|
|
|
|
|
|
|
if (App::quitting()) {
|
|
|
|
LOG(("Calls::Instance doesn't prevent quit any more."));
|
|
|
|
}
|
2019-01-21 13:42:21 +00:00
|
|
|
Core::App().quitPreventFinished();
|
2017-04-19 20:25:48 +00:00
|
|
|
}
|
|
|
|
}
|
2017-04-19 18:06:01 +00:00
|
|
|
|
2017-05-04 13:32:56 +00:00
|
|
|
void Instance::destroyCurrentPanel() {
|
2019-03-12 11:16:58 +00:00
|
|
|
_pendingPanels.erase(
|
|
|
|
std::remove_if(
|
|
|
|
_pendingPanels.begin(),
|
|
|
|
_pendingPanels.end(),
|
|
|
|
[](auto &&panel) { return !panel; }),
|
|
|
|
_pendingPanels.end());
|
|
|
|
_pendingPanels.emplace_back(_currentCallPanel.release());
|
2017-05-04 13:32:56 +00:00
|
|
|
_pendingPanels.back()->hideAndDestroy(); // Always queues the destruction.
|
|
|
|
}
|
|
|
|
|
2017-08-17 08:31:24 +00:00
|
|
|
void Instance::createCall(not_null<UserData*> user, Call::Type type) {
|
2017-05-04 12:29:32 +00:00
|
|
|
auto call = std::make_unique<Call>(getCallDelegate(), user, type);;
|
|
|
|
if (_currentCall) {
|
|
|
|
_currentCallPanel->replaceCall(call.get());
|
|
|
|
std::swap(_currentCall, call);
|
|
|
|
call->hangup();
|
|
|
|
} else {
|
|
|
|
_currentCallPanel = std::make_unique<Panel>(call.get());
|
|
|
|
_currentCall = std::move(call);
|
|
|
|
}
|
2017-04-25 20:36:04 +00:00
|
|
|
_currentCallChanged.notify(_currentCall.get(), true);
|
2017-04-28 15:00:16 +00:00
|
|
|
refreshServerConfig();
|
2017-04-19 20:25:48 +00:00
|
|
|
refreshDhConfig();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Instance::refreshDhConfig() {
|
|
|
|
Expects(_currentCall != nullptr);
|
2019-03-12 11:16:58 +00:00
|
|
|
|
|
|
|
const auto weak = base::make_weak(_currentCall);
|
2017-11-30 17:33:27 +00:00
|
|
|
request(MTPmessages_GetDhConfig(
|
|
|
|
MTP_int(_dhConfig.version),
|
2018-03-27 12:16:00 +00:00
|
|
|
MTP_int(MTP::ModExpFirst::kRandomPowerSize)
|
2019-03-12 11:16:58 +00:00
|
|
|
)).done([=](const MTPmessages_DhConfig &result) {
|
|
|
|
const auto call = weak.get();
|
|
|
|
const auto random = updateDhConfig(result);
|
|
|
|
if (!call) {
|
2017-04-19 09:44:07 +00:00
|
|
|
return;
|
|
|
|
}
|
2019-03-12 11:16:58 +00:00
|
|
|
if (!random.empty()) {
|
|
|
|
Assert(random.size() == MTP::ModExpFirst::kRandomPowerSize);
|
2017-04-19 20:25:48 +00:00
|
|
|
call->start(random);
|
2019-03-12 11:16:58 +00:00
|
|
|
} else {
|
|
|
|
callFailed(call);
|
2017-04-19 20:25:48 +00:00
|
|
|
}
|
2019-03-12 11:16:58 +00:00
|
|
|
}).fail([=](const RPCError &error) {
|
|
|
|
const auto call = weak.get();
|
2017-04-19 18:06:01 +00:00
|
|
|
if (!call) {
|
|
|
|
return;
|
2017-04-19 09:44:07 +00:00
|
|
|
}
|
2019-03-12 11:16:58 +00:00
|
|
|
callFailed(call);
|
2017-04-19 09:44:07 +00:00
|
|
|
}).send();
|
2017-04-28 15:00:16 +00:00
|
|
|
}
|
2017-04-19 09:44:07 +00:00
|
|
|
|
2019-03-12 11:16:58 +00:00
|
|
|
bytes::const_span Instance::updateDhConfig(
|
|
|
|
const MTPmessages_DhConfig &data) {
|
|
|
|
const auto validRandom = [](const QByteArray & random) {
|
|
|
|
if (random.size() != MTP::ModExpFirst::kRandomPowerSize) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
};
|
|
|
|
return data.match([&](const MTPDmessages_dhConfig &data)
|
|
|
|
-> bytes::const_span {
|
|
|
|
auto primeBytes = bytes::make_vector(data.vp.v);
|
|
|
|
if (!MTP::IsPrimeAndGood(primeBytes, data.vg.v)) {
|
|
|
|
LOG(("API Error: bad p/g received in dhConfig."));
|
|
|
|
return {};
|
|
|
|
} else if (!validRandom(data.vrandom.v)) {
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
_dhConfig.g = data.vg.v;
|
|
|
|
_dhConfig.p = std::move(primeBytes);
|
|
|
|
_dhConfig.version = data.vversion.v;
|
|
|
|
return bytes::make_span(data.vrandom.v);
|
|
|
|
}, [&](const MTPDmessages_dhConfigNotModified &data)
|
|
|
|
-> bytes::const_span {
|
|
|
|
if (!_dhConfig.g || _dhConfig.p.empty()) {
|
|
|
|
LOG(("API Error: dhConfigNotModified on zero version."));
|
|
|
|
return {};
|
|
|
|
} else if (!validRandom(data.vrandom.v)) {
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
return bytes::make_span(data.vrandom.v);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-04-28 15:00:16 +00:00
|
|
|
void Instance::refreshServerConfig() {
|
|
|
|
if (_serverConfigRequestId) {
|
|
|
|
return;
|
|
|
|
}
|
2019-02-19 06:57:53 +00:00
|
|
|
if (_lastServerConfigUpdateTime && (crl::now() - _lastServerConfigUpdateTime) < kServerConfigUpdateTimeoutMs) {
|
2017-04-28 15:00:16 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
_serverConfigRequestId = request(MTPphone_GetCallConfig()).done([this](const MTPDataJSON &result) {
|
|
|
|
_serverConfigRequestId = 0;
|
2019-02-19 06:57:53 +00:00
|
|
|
_lastServerConfigUpdateTime = crl::now();
|
2017-04-28 15:00:16 +00:00
|
|
|
|
2019-01-05 11:08:02 +00:00
|
|
|
const auto &json = result.c_dataJSON().vdata.v;
|
|
|
|
UpdateConfig(std::string(json.data(), json.size()));
|
2017-04-28 15:00:16 +00:00
|
|
|
}).fail([this](const RPCError &error) {
|
|
|
|
_serverConfigRequestId = 0;
|
|
|
|
}).send();
|
2017-04-19 09:44:07 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 18:06:01 +00:00
|
|
|
void Instance::handleUpdate(const MTPDupdatePhoneCall& update) {
|
|
|
|
handleCallUpdate(update.vphone_call);
|
2017-04-19 09:44:07 +00:00
|
|
|
}
|
|
|
|
|
2017-08-17 08:31:24 +00:00
|
|
|
void Instance::showInfoPanel(not_null<Call*> call) {
|
2017-04-25 20:36:04 +00:00
|
|
|
if (_currentCall.get() == call) {
|
|
|
|
_currentCallPanel->showAndActivate();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-29 20:06:32 +00:00
|
|
|
bool Instance::isQuitPrevent() {
|
2017-05-08 09:26:43 +00:00
|
|
|
if (!_currentCall || _currentCall->isIncomingWaiting()) {
|
2017-04-29 20:06:32 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
_currentCall->hangup();
|
|
|
|
if (!_currentCall) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
LOG(("Calls::Instance prevents quit, saving drafts..."));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-04-19 18:06:01 +00:00
|
|
|
void Instance::handleCallUpdate(const MTPPhoneCall &call) {
|
|
|
|
if (call.type() == mtpc_phoneCallRequested) {
|
2017-04-19 20:25:48 +00:00
|
|
|
auto &phoneCall = call.c_phoneCallRequested();
|
2019-01-18 12:27:37 +00:00
|
|
|
auto user = Auth().data().userLoaded(phoneCall.vadmin_id.v);
|
2017-04-19 20:25:48 +00:00
|
|
|
if (!user) {
|
|
|
|
LOG(("API Error: User not loaded for phoneCallRequested."));
|
|
|
|
} else if (user->isSelf()) {
|
|
|
|
LOG(("API Error: Self found in phoneCallRequested."));
|
|
|
|
}
|
2017-05-04 12:29:32 +00:00
|
|
|
if (alreadyInCall() || !user || user->isSelf()) {
|
2019-03-22 13:42:03 +00:00
|
|
|
request(MTPphone_DiscardCall(
|
|
|
|
MTP_flags(0),
|
|
|
|
MTP_inputPhoneCall(phoneCall.vid, phoneCall.vaccess_hash),
|
|
|
|
MTP_int(0),
|
|
|
|
MTP_phoneCallDiscardReasonBusy(),
|
|
|
|
MTP_long(0)
|
|
|
|
)).send();
|
2017-05-04 12:29:32 +00:00
|
|
|
} else if (phoneCall.vdate.v + (Global::CallRingTimeoutMs() / 1000) < unixtime()) {
|
2017-04-28 17:16:14 +00:00
|
|
|
LOG(("Ignoring too old call."));
|
2017-04-19 18:06:01 +00:00
|
|
|
} else {
|
2017-04-19 20:25:48 +00:00
|
|
|
createCall(user, Call::Type::Incoming);
|
|
|
|
_currentCall->handleUpdate(call);
|
2017-04-19 18:06:01 +00:00
|
|
|
}
|
|
|
|
} else if (!_currentCall || !_currentCall->handleUpdate(call)) {
|
|
|
|
DEBUG_LOG(("API Warning: unexpected phone call update %1").arg(call.type()));
|
|
|
|
}
|
2017-04-19 09:44:07 +00:00
|
|
|
}
|
|
|
|
|
2017-05-04 12:29:32 +00:00
|
|
|
bool Instance::alreadyInCall() {
|
|
|
|
return (_currentCall && _currentCall->state() != Call::State::Busy);
|
2017-05-03 13:43:01 +00:00
|
|
|
}
|
2018-10-17 06:09:59 +00:00
|
|
|
|
2019-01-05 11:08:02 +00:00
|
|
|
Call *Instance::currentCall() {
|
|
|
|
return _currentCall.get();
|
|
|
|
}
|
|
|
|
|
2018-09-30 15:42:50 +00:00
|
|
|
void Instance::requestMicrophonePermissionOrFail(Fn<void()> onSuccess) {
|
|
|
|
Platform::PermissionStatus status=Platform::GetPermissionStatus(Platform::PermissionType::Microphone);
|
|
|
|
if (status==Platform::PermissionStatus::Granted) {
|
|
|
|
onSuccess();
|
|
|
|
} else if(status==Platform::PermissionStatus::CanRequest) {
|
2018-10-17 06:09:59 +00:00
|
|
|
Platform::RequestPermission(Platform::PermissionType::Microphone, crl::guard(this, [=](Platform::PermissionStatus status) {
|
2018-09-30 15:42:50 +00:00
|
|
|
if (status==Platform::PermissionStatus::Granted) {
|
|
|
|
crl::on_main(onSuccess);
|
|
|
|
} else {
|
|
|
|
if (_currentCall) {
|
|
|
|
_currentCall->hangup();
|
|
|
|
}
|
|
|
|
}
|
2018-10-17 06:09:59 +00:00
|
|
|
}));
|
2018-09-30 15:42:50 +00:00
|
|
|
} else {
|
|
|
|
if (alreadyInCall()) {
|
|
|
|
_currentCall->hangup();
|
|
|
|
}
|
2018-10-17 06:09:59 +00:00
|
|
|
Ui::show(Box<ConfirmBox>(lang(lng_no_mic_permission), lang(lng_menu_settings), crl::guard(this, [] {
|
2018-09-30 15:42:50 +00:00
|
|
|
Platform::OpenSystemSettingsForPermission(Platform::PermissionType::Microphone);
|
|
|
|
Ui::hideLayer();
|
2018-10-17 06:09:59 +00:00
|
|
|
})));
|
2018-09-30 15:42:50 +00:00
|
|
|
}
|
|
|
|
}
|
2017-05-03 13:43:01 +00:00
|
|
|
|
2017-05-04 13:32:56 +00:00
|
|
|
Instance::~Instance() {
|
|
|
|
for (auto panel : _pendingPanels) {
|
|
|
|
if (panel) {
|
|
|
|
delete panel;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-04-19 09:44:07 +00:00
|
|
|
|
|
|
|
Instance &Current() {
|
2017-08-04 14:54:32 +00:00
|
|
|
return Auth().calls();
|
2017-04-19 09:44:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace Calls
|