tdesktop/Telegram/SourceFiles/calls/calls_instance.cpp

595 lines
16 KiB
C++
Raw Normal View History

/*
This file is part of Telegram Desktop,
the official desktop application for the Telegram messaging service.
For license and copyright information please follow this link:
https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
*/
#include "calls/calls_instance.h"
2019-11-13 14:12:04 +00:00
#include "mtproto/mtproto_dh_utils.h"
#include "core/application.h"
2019-07-24 11:45:24 +00:00
#include "main/main_session.h"
2020-06-25 17:57:36 +00:00
#include "main/main_account.h"
#include "apiwrap.h"
2017-04-13 08:27:10 +00:00
#include "lang/lang_keys.h"
#include "boxes/confirm_box.h"
#include "calls/calls_call.h"
2020-11-24 12:54:20 +00:00
#include "calls/calls_group_call.h"
#include "calls/calls_panel.h"
2020-11-24 12:54:20 +00:00
#include "calls/calls_group_panel.h"
#include "data/data_user.h"
2020-11-24 11:56:46 +00:00
#include "data/data_group_call.h"
2020-11-20 19:25:35 +00:00
#include "data/data_channel.h"
#include "data/data_chat.h"
2019-01-18 12:27:37 +00:00
#include "data/data_session.h"
#include "media/audio/media_audio_track.h"
#include "platform/platform_specific.h"
#include "base/unixtime.h"
#include "mainwidget.h"
#include "mtproto/mtproto_config.h"
#include "boxes/rate_call_box.h"
#include "tgcalls/VideoCaptureInterface.h"
#include "app.h"
namespace Calls {
namespace {
constexpr auto kServerConfigUpdateTimeoutMs = 24 * 3600 * crl::time(1000);
} // namespace
2020-06-25 17:57:36 +00:00
Instance::Instance() = default;
2020-08-12 13:35:31 +00:00
Instance::~Instance() = default;
void Instance::startOutgoingCall(not_null<UserData*> user, bool video) {
2020-11-20 19:25:35 +00:00
if (activateCurrentCall()) {
2017-05-03 13:43:01 +00:00
return;
}
if (user->callsStatus() == UserData::CallsStatus::Private) {
// Request full user once more to refresh the setting in case it was changed.
2020-06-25 17:57:36 +00:00
user->session().api().requestFullPeer(user);
Ui::show(Box<InformBox>(
tr::lng_call_error_not_available(tr::now, lt_user, user->name)));
return;
}
2020-05-17 09:12:27 +00:00
requestPermissionsOrFail(crl::guard(this, [=] {
createCall(user, Call::Type::Outgoing, video);
}), video);
}
void Instance::startOrJoinGroupCall(not_null<PeerData*> peer) {
const auto context = peer->groupCall()
? ChooseJoinAsProcess::Context::Join
: ChooseJoinAsProcess::Context::Create;
_chooseJoinAs.start(peer, context, [=](
not_null<PeerData*> peer,
not_null<PeerData*> joinAs) {
startOrJoinGroupCall(peer, joinAs);
});
}
void Instance::startOrJoinGroupCall(
not_null<PeerData*> peer,
not_null<PeerData*> joinAs) {
2020-12-08 15:09:13 +00:00
destroyCurrentCall();
2020-12-09 13:11:42 +00:00
const auto call = peer->groupCall();
2020-12-09 13:11:42 +00:00
createGroupCall(
peer,
call ? call->input() : MTP_inputGroupCall(MTPlong(), MTPlong()),
joinAs);
2020-11-20 19:25:35 +00:00
}
void Instance::callFinished(not_null<Call*> call) {
2020-08-14 15:59:24 +00:00
crl::on_main(call, [=] {
destroyCall(call);
});
}
void Instance::callFailed(not_null<Call*> call) {
2020-08-14 15:59:24 +00:00
crl::on_main(call, [=] {
destroyCall(call);
});
}
void Instance::callRedial(not_null<Call*> call) {
if (_currentCall.get() == call) {
refreshDhConfig();
}
}
2020-11-24 11:56:46 +00:00
void Instance::groupCallFinished(not_null<GroupCall*> call) {
crl::on_main(call, [=] {
destroyGroupCall(call);
});
}
void Instance::groupCallFailed(not_null<GroupCall*> call) {
crl::on_main(call, [=] {
destroyGroupCall(call);
});
}
not_null<Media::Audio::Track*> Instance::ensureSoundLoaded(
const QString &key) {
const auto i = _tracks.find(key);
if (i != end(_tracks)) {
return i->second.get();
}
const auto result = _tracks.emplace(
key,
Media::Audio::Current().createTrack()).first->second.get();
result->fillFromFile(Core::App().settings().getSoundPath(key));
return result;
}
void Instance::playSoundOnce(const QString &key) {
ensureSoundLoaded(key)->playOnce();
}
void Instance::callPlaySound(CallSound sound) {
playSoundOnce([&] {
switch (sound) {
case CallSound::Busy: return "call_busy";
case CallSound::Ended: return "call_end";
case CallSound::Connecting: return "call_connect";
2017-05-03 13:43:01 +00:00
}
Unexpected("CallSound in Instance::callPlaySound.");
return "";
}());
}
void Instance::groupCallPlaySound(GroupCallSound sound) {
playSoundOnce([&] {
switch (sound) {
case GroupCallSound::Started: return "group_call_start";
case GroupCallSound::Ended: return "group_call_end";
case GroupCallSound::Connecting: return "group_call_connect";
2017-05-03 13:43:01 +00:00
}
Unexpected("GroupCallSound in Instance::groupCallPlaySound.");
return "";
}());
2017-05-03 13:43:01 +00:00
}
void Instance::destroyCall(not_null<Call*> call) {
if (_currentCall.get() == call) {
2020-08-14 15:59:24 +00:00
_currentCallPanel->closeBeforeDestroy();
_currentCallPanel = nullptr;
2020-06-25 17:57:36 +00:00
auto taken = base::take(_currentCall);
_currentCallChanges.fire(nullptr);
taken.reset();
if (App::quitting()) {
LOG(("Calls::Instance doesn't prevent quit any more."));
}
Core::App().quitPreventFinished();
}
}
void Instance::createCall(not_null<UserData*> user, Call::Type type, bool video) {
auto call = std::make_unique<Call>(getCallDelegate(), user, type, video);
2020-06-25 17:57:36 +00:00
const auto raw = call.get();
user->session().account().sessionChanges(
) | rpl::start_with_next([=] {
destroyCall(raw);
}, raw->lifetime());
if (_currentCall) {
2020-06-25 17:57:36 +00:00
_currentCallPanel->replaceCall(raw);
std::swap(_currentCall, call);
call->hangup();
} else {
2020-06-25 17:57:36 +00:00
_currentCallPanel = std::make_unique<Panel>(raw);
_currentCall = std::move(call);
}
2020-06-25 17:57:36 +00:00
_currentCallChanges.fire_copy(raw);
refreshServerConfig(&user->session());
refreshDhConfig();
}
2020-11-20 19:25:35 +00:00
void Instance::destroyGroupCall(not_null<GroupCall*> call) {
if (_currentGroupCall.get() == call) {
2020-11-24 12:54:20 +00:00
_currentGroupCallPanel->closeBeforeDestroy();
_currentGroupCallPanel = nullptr;
2020-11-20 19:25:35 +00:00
auto taken = base::take(_currentGroupCall);
_currentGroupCallChanges.fire(nullptr);
taken.reset();
if (App::quitting()) {
LOG(("Calls::Instance doesn't prevent quit any more."));
}
Core::App().quitPreventFinished();
}
}
void Instance::createGroupCall(
not_null<PeerData*> peer,
const MTPInputGroupCall &inputCall,
not_null<PeerData*> joinAs) {
2020-12-09 13:11:42 +00:00
destroyCurrentCall();
2020-11-20 19:25:35 +00:00
auto call = std::make_unique<GroupCall>(
getGroupCallDelegate(),
peer,
inputCall,
joinAs);
2020-11-20 19:25:35 +00:00
const auto raw = call.get();
peer->session().account().sessionChanges(
2020-11-20 19:25:35 +00:00
) | rpl::start_with_next([=] {
destroyGroupCall(raw);
}, raw->lifetime());
2020-11-24 12:54:20 +00:00
_currentGroupCallPanel = std::make_unique<GroupPanel>(raw);
2020-11-20 19:25:35 +00:00
_currentGroupCall = std::move(call);
_currentGroupCallChanges.fire_copy(raw);
}
void Instance::refreshDhConfig() {
Expects(_currentCall != nullptr);
const auto weak = base::make_weak(_currentCall);
2020-06-25 17:57:36 +00:00
_currentCall->user()->session().api().request(MTPmessages_GetDhConfig(
MTP_int(_dhConfig.version),
MTP_int(MTP::ModExpFirst::kRandomPowerSize)
)).done([=](const MTPmessages_DhConfig &result) {
const auto call = weak.get();
const auto random = updateDhConfig(result);
if (!call) {
return;
}
if (!random.empty()) {
Assert(random.size() == MTP::ModExpFirst::kRandomPowerSize);
call->start(random);
} else {
callFailed(call);
}
}).fail([=](const RPCError &error) {
const auto call = weak.get();
if (!call) {
return;
}
callFailed(call);
}).send();
}
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 {
2019-07-05 13:38:38 +00:00
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 {};
2019-07-05 13:38:38 +00:00
} else if (!validRandom(data.vrandom().v)) {
return {};
}
2019-07-05 13:38:38 +00:00
_dhConfig.g = data.vg().v;
_dhConfig.p = std::move(primeBytes);
2019-07-05 13:38:38 +00:00
_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 {};
2019-07-05 13:38:38 +00:00
} else if (!validRandom(data.vrandom().v)) {
return {};
}
2019-07-05 13:38:38 +00:00
return bytes::make_span(data.vrandom().v);
});
}
2020-06-25 17:57:36 +00:00
void Instance::refreshServerConfig(not_null<Main::Session*> session) {
if (_serverConfigRequestSession) {
return;
}
2020-06-25 17:57:36 +00:00
if (_lastServerConfigUpdateTime
&& ((crl::now() - _lastServerConfigUpdateTime)
< kServerConfigUpdateTimeoutMs)) {
return;
}
2020-06-25 17:57:36 +00:00
_serverConfigRequestSession = session;
session->api().request(MTPphone_GetCallConfig(
2019-11-27 08:02:56 +00:00
)).done([=](const MTPDataJSON &result) {
2020-06-25 17:57:36 +00:00
_serverConfigRequestSession = nullptr;
_lastServerConfigUpdateTime = crl::now();
2019-07-05 13:38:38 +00:00
const auto &json = result.c_dataJSON().vdata().v;
2019-01-05 11:08:02 +00:00
UpdateConfig(std::string(json.data(), json.size()));
2019-11-27 08:02:56 +00:00
}).fail([=](const RPCError &error) {
2020-06-25 17:57:36 +00:00
_serverConfigRequestSession = nullptr;
}).send();
}
2020-06-25 17:57:36 +00:00
void Instance::handleUpdate(
not_null<Main::Session*> session,
const MTPUpdate &update) {
update.match([&](const MTPDupdatePhoneCall &data) {
handleCallUpdate(session, data.vphone_call());
}, [&](const MTPDupdatePhoneCallSignalingData &data) {
handleSignalingData(session, data);
2020-11-20 19:25:35 +00:00
}, [&](const MTPDupdateGroupCall &data) {
handleGroupCallUpdate(session, data.vcall());
}, [&](const MTPDupdateGroupCallParticipants &data) {
handleGroupCallUpdate(session, data);
}, [](const auto &) {
Unexpected("Update type in Calls::Instance::handleUpdate.");
});
}
void Instance::showInfoPanel(not_null<Call*> call) {
if (_currentCall.get() == call) {
_currentCallPanel->showAndActivate();
}
}
2020-11-24 12:54:20 +00:00
void Instance::showInfoPanel(not_null<GroupCall*> call) {
if (_currentGroupCall.get() == call) {
_currentGroupCallPanel->showAndActivate();
}
}
2020-11-28 16:18:51 +00:00
void Instance::setCurrentAudioDevice(bool input, const QString &deviceId) {
if (input) {
Core::App().settings().setCallInputDeviceId(deviceId);
} else {
Core::App().settings().setCallOutputDeviceId(deviceId);
}
Core::App().saveSettingsDelayed();
if (const auto call = currentCall()) {
call->setCurrentAudioDevice(input, deviceId);
} else if (const auto group = currentGroupCall()) {
group->setCurrentAudioDevice(input, deviceId);
}
}
bool Instance::isQuitPrevent() {
if (!_currentCall || _currentCall->isIncomingWaiting()) {
return false;
}
_currentCall->hangup();
if (!_currentCall) {
return false;
}
LOG(("Calls::Instance prevents quit, hanging up a call..."));
return true;
}
2020-06-25 17:57:36 +00:00
void Instance::handleCallUpdate(
not_null<Main::Session*> session,
const MTPPhoneCall &call) {
if (call.type() == mtpc_phoneCallRequested) {
auto &phoneCall = call.c_phoneCallRequested();
2020-06-25 17:57:36 +00:00
auto user = session->data().userLoaded(phoneCall.vadmin_id().v);
if (!user) {
LOG(("API Error: User not loaded for phoneCallRequested."));
} else if (user->isSelf()) {
LOG(("API Error: Self found in phoneCallRequested."));
}
2020-06-25 17:57:36 +00:00
const auto &config = session->serverConfig();
2020-11-20 19:25:35 +00:00
if (inCall() || inGroupCall() || !user || user->isSelf()) {
const auto flags = phoneCall.is_video()
? MTPphone_DiscardCall::Flag::f_video
: MTPphone_DiscardCall::Flag(0);
2020-06-25 17:57:36 +00:00
session->api().request(MTPphone_DiscardCall(
MTP_flags(flags),
2019-07-05 13:38:38 +00:00
MTP_inputPhoneCall(phoneCall.vid(), phoneCall.vaccess_hash()),
2019-03-22 13:42:03 +00:00
MTP_int(0),
MTP_phoneCallDiscardReasonBusy(),
MTP_long(0)
)).send();
} else if (phoneCall.vdate().v + (config.callRingTimeoutMs / 1000)
< base::unixtime::now()) {
LOG(("Ignoring too old call."));
} else if (Core::App().settings().disableCalls()) {
LOG(("Ignoring call because of 'accept calls' settings."));
} else {
createCall(user, Call::Type::Incoming, phoneCall.is_video());
_currentCall->handleUpdate(call);
}
} else if (!_currentCall
|| (&_currentCall->user()->session() != session)
|| !_currentCall->handleUpdate(call)) {
DEBUG_LOG(("API Warning: unexpected phone call update %1").arg(call.type()));
}
}
2020-11-20 19:25:35 +00:00
void Instance::handleGroupCallUpdate(
not_null<Main::Session*> session,
const MTPGroupCall &call) {
2020-11-24 11:56:46 +00:00
const auto callId = call.match([](const auto &data) {
return data.vid().v;
});
if (const auto existing = session->data().groupCall(callId)) {
existing->applyUpdate(call);
}
if (_currentGroupCall
&& (&_currentGroupCall->peer()->session() == session)) {
2020-11-20 19:25:35 +00:00
_currentGroupCall->handleUpdate(call);
}
}
void Instance::handleGroupCallUpdate(
not_null<Main::Session*> session,
const MTPDupdateGroupCallParticipants &update) {
2020-11-24 11:56:46 +00:00
const auto callId = update.vcall().match([](const auto &data) {
return data.vid().v;
});
if (const auto existing = session->data().groupCall(callId)) {
existing->applyUpdate(update);
}
if (_currentGroupCall
&& (&_currentGroupCall->peer()->session() == session)
&& (_currentGroupCall->id() == callId)) {
_currentGroupCall->handleUpdate(update);
}
2020-11-20 19:25:35 +00:00
}
void Instance::handleSignalingData(
not_null<Main::Session*> session,
const MTPDupdatePhoneCallSignalingData &data) {
if (!_currentCall
|| (&_currentCall->user()->session() != session)
|| !_currentCall->handleSignalingData(data)) {
DEBUG_LOG(("API Warning: unexpected call signaling data %1"
).arg(data.vphone_call_id().v));
}
}
2020-11-20 19:25:35 +00:00
bool Instance::inCall() const {
2020-12-08 15:09:13 +00:00
if (!_currentCall) {
return false;
}
const auto state = _currentCall->state();
return (state != Call::State::Busy);
2017-05-03 13:43:01 +00:00
}
2018-10-17 06:09:59 +00:00
2020-11-20 19:25:35 +00:00
bool Instance::inGroupCall() const {
if (!_currentGroupCall) {
return false;
}
const auto state = _currentGroupCall->state();
return (state != GroupCall::State::HangingUp)
&& (state != GroupCall::State::Ended)
&& (state != GroupCall::State::FailedHangingUp)
&& (state != GroupCall::State::Failed);
2020-11-20 19:25:35 +00:00
}
2020-12-08 15:09:13 +00:00
void Instance::destroyCurrentCall() {
if (const auto current = currentCall()) {
current->hangup();
if (const auto still = currentCall()) {
destroyCall(still);
}
}
if (const auto current = currentGroupCall()) {
current->hangup();
if (const auto still = currentGroupCall()) {
destroyGroupCall(still);
}
}
}
bool Instance::hasActivePanel(not_null<Main::Session*> session) const {
if (inCall()) {
return (&_currentCall->user()->session() == session)
&& _currentCallPanel->isActive();
} else if (inGroupCall()) {
return (&_currentGroupCall->peer()->session() == session)
&& _currentGroupCallPanel->isActive();
}
return false;
}
2020-11-20 19:25:35 +00:00
bool Instance::activateCurrentCall() {
if (inCall()) {
_currentCallPanel->showAndActivate();
return true;
} else if (inGroupCall()) {
2020-11-24 12:54:20 +00:00
_currentGroupCallPanel->showAndActivate();
2020-11-20 19:25:35 +00:00
return true;
}
return false;
}
bool Instance::minimizeCurrentActiveCall() {
if (inCall() && _currentCallPanel->isActive()) {
_currentCallPanel->minimize();
return true;
} else if (inGroupCall() && _currentGroupCallPanel->isActive()) {
_currentGroupCallPanel->minimize();
return true;
}
return false;
}
bool Instance::closeCurrentActiveCall() {
if (inGroupCall() && _currentGroupCallPanel->isActive()) {
_currentGroupCallPanel->close();
return true;
}
return false;
}
2020-06-25 17:57:36 +00:00
Call *Instance::currentCall() const {
2019-01-05 11:08:02 +00:00
return _currentCall.get();
}
2020-06-25 17:57:36 +00:00
rpl::producer<Call*> Instance::currentCallValue() const {
return _currentCallChanges.events_starting_with(currentCall());
}
2020-11-24 12:54:20 +00:00
GroupCall *Instance::currentGroupCall() const {
return _currentGroupCall.get();
}
rpl::producer<GroupCall*> Instance::currentGroupCallValue() const {
return _currentGroupCallChanges.events_starting_with(currentGroupCall());
}
void Instance::requestPermissionsOrFail(Fn<void()> onSuccess, bool video) {
2020-05-17 09:12:27 +00:00
using Type = Platform::PermissionType;
requestPermissionOrFail(Type::Microphone, [=] {
auto callback = [=] { crl::on_main(onSuccess); };
if (video) {
requestPermissionOrFail(Type::Camera, std::move(callback));
} else {
callback();
}
2020-05-17 09:12:27 +00:00
});
}
void Instance::requestPermissionOrFail(Platform::PermissionType type, Fn<void()> onSuccess) {
using Status = Platform::PermissionStatus;
const auto status = Platform::GetPermissionStatus(type);
if (status == Status::Granted) {
onSuccess();
2020-05-17 09:12:27 +00:00
} else if (status == Status::CanRequest) {
Platform::RequestPermission(type, crl::guard(this, [=](Status status) {
if (status == Status::Granted) {
crl::on_main(onSuccess);
} else {
if (_currentCall) {
_currentCall->hangup();
}
}
2018-10-17 06:09:59 +00:00
}));
} else {
2020-11-20 19:25:35 +00:00
if (inCall()) {
_currentCall->hangup();
}
2020-11-20 19:25:35 +00:00
if (inGroupCall()) {
2020-11-24 11:56:46 +00:00
_currentGroupCall->hangup();
2020-11-20 19:25:35 +00:00
}
2020-05-17 09:12:27 +00:00
Ui::show(Box<ConfirmBox>(tr::lng_no_mic_permission(tr::now), tr::lng_menu_settings(tr::now), crl::guard(this, [=] {
Platform::OpenSystemSettingsForPermission(type);
Ui::hideLayer();
2018-10-17 06:09:59 +00:00
})));
}
}
2017-05-03 13:43:01 +00:00
std::shared_ptr<tgcalls::VideoCaptureInterface> Instance::getVideoCapture() {
if (auto result = _videoCapture.lock()) {
return result;
}
auto result = std::shared_ptr<tgcalls::VideoCaptureInterface>(
tgcalls::VideoCaptureInterface::Create(
Core::App().settings().callVideoInputDeviceId().toStdString()));
_videoCapture = result;
return result;
}
} // namespace Calls