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"
|
2017-04-29 20:06:32 +00:00
|
|
|
#include "messenger.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"
|
2017-05-03 13:43:01 +00:00
|
|
|
#include "media/media_audio_track.h"
|
2017-05-02 11:56:39 +00:00
|
|
|
|
|
|
|
#include "boxes/rate_call_box.h"
|
2017-04-19 09:44:07 +00:00
|
|
|
namespace Calls {
|
2017-04-28 15:00:16 +00:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
constexpr auto kServerConfigUpdateTimeoutMs = 24 * 3600 * TimeMs(1000);
|
|
|
|
|
|
|
|
} // 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;
|
|
|
|
}
|
2017-04-19 20:25:48 +00:00
|
|
|
createCall(user, Call::Type::Outgoing);
|
|
|
|
}
|
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."));
|
|
|
|
}
|
|
|
|
Messenger::Instance().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() {
|
|
|
|
_pendingPanels.erase(std::remove_if(_pendingPanels.begin(), _pendingPanels.end(), [](auto &&panel) {
|
|
|
|
return !panel;
|
|
|
|
}), _pendingPanels.end());
|
|
|
|
_pendingPanels.push_back(_currentCallPanel.release());
|
|
|
|
_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);
|
2017-11-30 17:33:27 +00:00
|
|
|
request(MTPmessages_GetDhConfig(
|
|
|
|
MTP_int(_dhConfig.version),
|
|
|
|
MTP_int(Call::kRandomPowerSize)
|
|
|
|
)).done([this, call = base::make_weak(_currentCall)](
|
|
|
|
const MTPmessages_DhConfig &result) {
|
2017-04-19 18:06:01 +00:00
|
|
|
auto random = base::const_byte_span();
|
2017-04-19 09:44:07 +00:00
|
|
|
switch (result.type()) {
|
|
|
|
case mtpc_messages_dhConfig: {
|
|
|
|
auto &config = result.c_messages_dhConfig();
|
2017-04-24 12:16:38 +00:00
|
|
|
if (!MTP::IsPrimeAndGood(bytesFromMTP(config.vp), config.vg.v)) {
|
2017-04-19 09:44:07 +00:00
|
|
|
LOG(("API Error: bad p/g received in dhConfig."));
|
2017-04-19 18:06:01 +00:00
|
|
|
callFailed(call.get());
|
2017-04-19 09:44:07 +00:00
|
|
|
return;
|
|
|
|
}
|
2017-04-19 18:06:01 +00:00
|
|
|
_dhConfig.g = config.vg.v;
|
|
|
|
_dhConfig.p = byteVectorFromMTP(config.vp);
|
|
|
|
random = bytesFromMTP(config.vrandom);
|
2017-04-19 09:44:07 +00:00
|
|
|
} break;
|
|
|
|
|
|
|
|
case mtpc_messages_dhConfigNotModified: {
|
|
|
|
auto &config = result.c_messages_dhConfigNotModified();
|
2017-04-19 18:06:01 +00:00
|
|
|
random = bytesFromMTP(config.vrandom);
|
|
|
|
if (!_dhConfig.g || _dhConfig.p.empty()) {
|
2017-04-19 09:44:07 +00:00
|
|
|
LOG(("API Error: dhConfigNotModified on zero version."));
|
2017-04-19 18:06:01 +00:00
|
|
|
callFailed(call.get());
|
2017-04-19 09:44:07 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
} break;
|
|
|
|
|
|
|
|
default: Unexpected("Type in messages.getDhConfig");
|
|
|
|
}
|
2017-04-19 18:06:01 +00:00
|
|
|
|
2017-04-19 20:25:48 +00:00
|
|
|
if (random.size() != Call::kRandomPowerSize) {
|
2017-04-19 09:44:07 +00:00
|
|
|
LOG(("API Error: dhConfig random bytes wrong size: %1").arg(random.size()));
|
2017-04-19 18:06:01 +00:00
|
|
|
callFailed(call.get());
|
2017-04-19 09:44:07 +00:00
|
|
|
return;
|
|
|
|
}
|
2017-04-19 20:25:48 +00:00
|
|
|
if (call) {
|
|
|
|
call->start(random);
|
|
|
|
}
|
2017-11-30 17:33:27 +00:00
|
|
|
}).fail([this, call = base::make_weak(_currentCall)](
|
|
|
|
const RPCError &error) {
|
2017-04-19 18:06:01 +00:00
|
|
|
if (!call) {
|
|
|
|
DEBUG_LOG(("API Warning: call was destroyed before got dhConfig."));
|
|
|
|
return;
|
2017-04-19 09:44:07 +00:00
|
|
|
}
|
2017-04-19 18:06:01 +00:00
|
|
|
callFailed(call.get());
|
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
|
|
|
|
2017-04-28 15:00:16 +00:00
|
|
|
void Instance::refreshServerConfig() {
|
|
|
|
if (_serverConfigRequestId) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (_lastServerConfigUpdateTime && (getms(true) - _lastServerConfigUpdateTime) < kServerConfigUpdateTimeoutMs) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
_serverConfigRequestId = request(MTPphone_GetCallConfig()).done([this](const MTPDataJSON &result) {
|
|
|
|
_serverConfigRequestId = 0;
|
|
|
|
_lastServerConfigUpdateTime = getms(true);
|
|
|
|
|
|
|
|
auto configUpdate = std::map<std::string, std::string>();
|
|
|
|
auto bytes = bytesFromMTP(result.c_dataJSON().vdata);
|
|
|
|
auto error = QJsonParseError { 0, QJsonParseError::NoError };
|
|
|
|
auto document = QJsonDocument::fromJson(QByteArray::fromRawData(reinterpret_cast<const char*>(bytes.data()), bytes.size()), &error);
|
|
|
|
if (error.error != QJsonParseError::NoError) {
|
2017-07-06 21:44:37 +00:00
|
|
|
LOG(("API Error: Failed to parse call config JSON, error: %1").arg(error.errorString()));
|
2017-04-28 15:00:16 +00:00
|
|
|
return;
|
|
|
|
} else if (!document.isObject()) {
|
|
|
|
LOG(("API Error: Not an object received in call config JSON."));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto parseValue = [](QJsonValueRef data) -> std::string {
|
|
|
|
switch (data.type()) {
|
|
|
|
case QJsonValue::String: return data.toString().toStdString();
|
|
|
|
case QJsonValue::Double: return QString::number(data.toDouble(), 'f').toStdString();
|
|
|
|
case QJsonValue::Bool: return data.toBool() ? "true" : "false";
|
|
|
|
case QJsonValue::Null: {
|
|
|
|
LOG(("API Warning: null field in call config JSON."));
|
|
|
|
} return "null";
|
|
|
|
case QJsonValue::Undefined: {
|
|
|
|
LOG(("API Warning: undefined field in call config JSON."));
|
|
|
|
} return "undefined";
|
|
|
|
case QJsonValue::Object:
|
|
|
|
case QJsonValue::Array: {
|
|
|
|
LOG(("API Warning: complex field in call config JSON."));
|
|
|
|
QJsonDocument serializer;
|
|
|
|
if (data.isArray()) {
|
|
|
|
serializer.setArray(data.toArray());
|
|
|
|
} else {
|
|
|
|
serializer.setObject(data.toObject());
|
|
|
|
}
|
|
|
|
auto byteArray = serializer.toJson(QJsonDocument::Compact);
|
|
|
|
return std::string(byteArray.constData(), byteArray.size());
|
|
|
|
} break;
|
|
|
|
}
|
|
|
|
Unexpected("Type in Json parse.");
|
|
|
|
};
|
|
|
|
|
|
|
|
auto object = document.object();
|
|
|
|
for (auto i = object.begin(), e = object.end(); i != e; ++i) {
|
|
|
|
auto key = i.key().toStdString();
|
|
|
|
auto value = parseValue(i.value());
|
|
|
|
configUpdate[key] = value;
|
|
|
|
}
|
|
|
|
|
|
|
|
UpdateConfig(configUpdate);
|
|
|
|
}).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();
|
|
|
|
auto user = App::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."));
|
|
|
|
}
|
2017-05-04 12:29:32 +00:00
|
|
|
if (alreadyInCall() || !user || user->isSelf()) {
|
2017-04-19 20:25:48 +00:00
|
|
|
request(MTPphone_DiscardCall(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
|
|
|
}
|
|
|
|
|
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
|