2018-12-31 20:04:05 +01:00
|
|
|
//
|
2021-01-01 13:57:46 +01:00
|
|
|
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2021
|
2018-12-31 20:04:05 +01:00
|
|
|
//
|
|
|
|
// Distributed under the Boost Software License, Version 1.0. (See accompanying
|
|
|
|
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
|
|
|
//
|
|
|
|
#include "td/telegram/CallActor.h"
|
|
|
|
|
|
|
|
#include "td/telegram/td_api.h"
|
|
|
|
#include "td/telegram/telegram_api.h"
|
|
|
|
#include "td/telegram/telegram_api.hpp"
|
|
|
|
|
|
|
|
#include "td/telegram/ConfigShared.h"
|
|
|
|
#include "td/telegram/ContactsManager.h"
|
|
|
|
#include "td/telegram/DhCache.h"
|
2018-12-19 21:35:13 +01:00
|
|
|
#include "td/telegram/DialogId.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
#include "td/telegram/Global.h"
|
|
|
|
#include "td/telegram/misc.h"
|
|
|
|
#include "td/telegram/net/NetQueryCreator.h"
|
|
|
|
#include "td/telegram/net/NetQueryDispatcher.h"
|
2018-12-18 21:59:35 +01:00
|
|
|
#include "td/telegram/NotificationManager.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
#include "td/telegram/Td.h"
|
|
|
|
#include "td/telegram/UpdatesManager.h"
|
|
|
|
|
2021-01-01 13:59:53 +01:00
|
|
|
#include "td/utils/algorithm.h"
|
2020-02-25 12:47:31 +01:00
|
|
|
#include "td/utils/as.h"
|
|
|
|
#include "td/utils/bits.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
#include "td/utils/buffer.h"
|
2019-02-12 22:26:36 +01:00
|
|
|
#include "td/utils/common.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
#include "td/utils/crypto.h"
|
|
|
|
#include "td/utils/logging.h"
|
|
|
|
#include "td/utils/Random.h"
|
2021-05-17 14:21:11 +02:00
|
|
|
#include "td/utils/SliceBuilder.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
#include <tuple>
|
2019-08-06 19:07:23 +02:00
|
|
|
#include <unordered_set>
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
namespace td {
|
2019-08-06 19:07:23 +02:00
|
|
|
|
2020-06-22 03:28:03 +02:00
|
|
|
CallProtocol::CallProtocol(const telegram_api::phoneCallProtocol &protocol)
|
|
|
|
: udp_p2p(protocol.udp_p2p_)
|
|
|
|
, udp_reflector(protocol.udp_reflector_)
|
|
|
|
, min_layer(protocol.min_layer_)
|
|
|
|
, max_layer(protocol.max_layer_)
|
|
|
|
, library_versions(protocol.library_versions_) {
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
2020-06-22 03:28:03 +02:00
|
|
|
tl_object_ptr<telegram_api::phoneCallProtocol> CallProtocol::get_input_phone_call_protocol() const {
|
2018-12-31 20:04:05 +01:00
|
|
|
int32 flags = 0;
|
|
|
|
if (udp_p2p) {
|
2018-12-04 19:18:07 +01:00
|
|
|
flags |= telegram_api::phoneCallProtocol::UDP_P2P_MASK;
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
if (udp_reflector) {
|
2018-12-04 19:18:07 +01:00
|
|
|
flags |= telegram_api::phoneCallProtocol::UDP_REFLECTOR_MASK;
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
2020-03-13 23:56:47 +01:00
|
|
|
return make_tl_object<telegram_api::phoneCallProtocol>(flags, udp_p2p, udp_reflector, min_layer, max_layer,
|
|
|
|
vector<string>(library_versions));
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
2020-06-22 03:28:03 +02:00
|
|
|
CallProtocol::CallProtocol(const td_api::callProtocol &protocol)
|
|
|
|
: udp_p2p(protocol.udp_p2p_)
|
|
|
|
, udp_reflector(protocol.udp_reflector_)
|
|
|
|
, min_layer(protocol.min_layer_)
|
|
|
|
, max_layer(protocol.max_layer_)
|
|
|
|
, library_versions(protocol.library_versions_) {
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
2020-03-13 23:56:47 +01:00
|
|
|
|
2020-08-10 17:37:03 +02:00
|
|
|
CallConnection::CallConnection(const telegram_api::PhoneConnection &connection) {
|
|
|
|
switch (connection.get_id()) {
|
|
|
|
case telegram_api::phoneConnection::ID: {
|
|
|
|
auto &conn = static_cast<const telegram_api::phoneConnection &>(connection);
|
|
|
|
type = Type::Telegram;
|
|
|
|
id = conn.id_;
|
|
|
|
ip = conn.ip_;
|
|
|
|
ipv6 = conn.ipv6_;
|
|
|
|
port = conn.port_;
|
|
|
|
peer_tag = conn.peer_tag_.as_slice().str();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case telegram_api::phoneConnectionWebrtc::ID: {
|
|
|
|
auto &conn = static_cast<const telegram_api::phoneConnectionWebrtc &>(connection);
|
|
|
|
type = Type::Webrtc;
|
|
|
|
id = conn.id_;
|
|
|
|
ip = conn.ip_;
|
|
|
|
ipv6 = conn.ipv6_;
|
|
|
|
port = conn.port_;
|
|
|
|
username = conn.username_;
|
|
|
|
password = conn.password_;
|
|
|
|
supports_turn = conn.turn_;
|
|
|
|
supports_stun = conn.stun_;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
UNREACHABLE();
|
|
|
|
}
|
2020-06-22 03:28:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
tl_object_ptr<td_api::callProtocol> CallProtocol::get_call_protocol_object() const {
|
2020-03-13 23:56:47 +01:00
|
|
|
return make_tl_object<td_api::callProtocol>(udp_p2p, udp_reflector, min_layer, max_layer,
|
|
|
|
vector<string>(library_versions));
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
2020-08-10 17:37:03 +02:00
|
|
|
tl_object_ptr<td_api::callServer> CallConnection::get_call_server_object() const {
|
|
|
|
auto server_type = [&]() -> tl_object_ptr<td_api::CallServerType> {
|
|
|
|
switch (type) {
|
|
|
|
case Type::Telegram:
|
|
|
|
return make_tl_object<td_api::callServerTypeTelegramReflector>(peer_tag);
|
|
|
|
case Type::Webrtc:
|
|
|
|
return make_tl_object<td_api::callServerTypeWebrtc>(username, password, supports_turn, supports_stun);
|
|
|
|
default:
|
|
|
|
UNREACHABLE();
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
}();
|
|
|
|
return make_tl_object<td_api::callServer>(id, ip, ipv6, port, std::move(server_type));
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
2020-06-22 03:28:03 +02:00
|
|
|
tl_object_ptr<td_api::CallState> CallState::get_call_state_object() const {
|
2018-12-31 20:04:05 +01:00
|
|
|
switch (type) {
|
|
|
|
case Type::Pending:
|
|
|
|
return make_tl_object<td_api::callStatePending>(is_created, is_received);
|
|
|
|
case Type::ExchangingKey:
|
|
|
|
return make_tl_object<td_api::callStateExchangingKeys>();
|
|
|
|
case Type::Ready: {
|
2020-08-10 17:37:03 +02:00
|
|
|
auto call_connections = transform(connections, [](auto &c) { return c.get_call_server_object(); });
|
2020-06-22 03:28:03 +02:00
|
|
|
return make_tl_object<td_api::callStateReady>(protocol.get_call_protocol_object(), std::move(call_connections),
|
|
|
|
config, key, vector<string>(emojis_fingerprint), allow_p2p);
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
case Type::HangingUp:
|
|
|
|
return make_tl_object<td_api::callStateHangingUp>();
|
|
|
|
case Type::Discarded:
|
|
|
|
return make_tl_object<td_api::callStateDiscarded>(get_call_discard_reason_object(discard_reason), need_rating,
|
|
|
|
need_debug_information);
|
|
|
|
case Type::Error:
|
|
|
|
CHECK(error.is_error());
|
|
|
|
return make_tl_object<td_api::callStateError>(make_tl_object<td_api::error>(error.code(), error.message().str()));
|
|
|
|
case Type::Empty:
|
|
|
|
default:
|
|
|
|
UNREACHABLE();
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
CallActor::CallActor(CallId call_id, ActorShared<> parent, Promise<int64> promise)
|
|
|
|
: parent_(std::move(parent)), call_id_promise_(std::move(promise)), local_call_id_(call_id) {
|
|
|
|
}
|
|
|
|
|
|
|
|
void CallActor::create_call(UserId user_id, tl_object_ptr<telegram_api::InputUser> &&input_user,
|
2019-06-08 11:26:35 +02:00
|
|
|
CallProtocol &&protocol, bool is_video, Promise<CallId> &&promise) {
|
2018-12-31 20:04:05 +01:00
|
|
|
CHECK(state_ == State::Empty);
|
|
|
|
state_ = State::SendRequestQuery;
|
|
|
|
is_outgoing_ = true;
|
2019-06-08 11:26:35 +02:00
|
|
|
is_video_ = is_video;
|
2018-12-31 20:04:05 +01:00
|
|
|
user_id_ = user_id;
|
|
|
|
input_user_ = std::move(input_user);
|
|
|
|
call_state_.protocol = std::move(protocol);
|
|
|
|
call_state_.type = CallState::Type::Pending;
|
|
|
|
call_state_.is_received = false;
|
|
|
|
call_state_need_flush_ = true;
|
|
|
|
loop();
|
|
|
|
promise.set_value(CallId(local_call_id_));
|
|
|
|
}
|
|
|
|
|
2020-08-10 10:34:28 +02:00
|
|
|
void CallActor::accept_call(CallProtocol &&protocol, Promise<> promise) {
|
|
|
|
if (state_ != State::SendAcceptQuery) {
|
|
|
|
return promise.set_error(Status::Error(400, "Unexpected acceptCall"));
|
|
|
|
}
|
|
|
|
is_accepted_ = true;
|
|
|
|
call_state_.protocol = std::move(protocol);
|
|
|
|
promise.set_value(Unit());
|
|
|
|
loop();
|
|
|
|
}
|
|
|
|
|
2020-05-29 10:26:08 +02:00
|
|
|
void CallActor::update_call_signaling_data(string data) {
|
2020-08-10 09:38:24 +02:00
|
|
|
if (call_state_.type != CallState::Type::Ready) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto update = td_api::make_object<td_api::updateNewCallSignalingData>();
|
|
|
|
update->call_id_ = local_call_id_.get();
|
|
|
|
update->data_ = std::move(data);
|
|
|
|
send_closure(G()->td(), &Td::send_update, std::move(update));
|
2020-05-29 10:26:08 +02:00
|
|
|
}
|
|
|
|
|
2020-08-10 10:34:28 +02:00
|
|
|
void CallActor::send_call_signaling_data(string &&data, Promise<> promise) {
|
|
|
|
if (call_state_.type != CallState::Type::Ready) {
|
|
|
|
return promise.set_error(Status::Error(400, "Call is not active"));
|
|
|
|
}
|
|
|
|
|
|
|
|
auto query = G()->net_query_creator().create(
|
|
|
|
telegram_api::phone_sendSignalingData(get_input_phone_call("send_call_signaling_data"), BufferSlice(data)));
|
|
|
|
send_with_promise(std::move(query),
|
2021-03-05 00:47:27 +01:00
|
|
|
PromiseCreator::lambda([promise = std::move(promise)](Result<NetQueryPtr> r_net_query) mutable {
|
|
|
|
auto res = fetch_result<telegram_api::phone_sendSignalingData>(std::move(r_net_query));
|
2020-08-10 10:34:28 +02:00
|
|
|
if (res.is_error()) {
|
|
|
|
promise.set_error(res.move_as_error());
|
|
|
|
} else {
|
|
|
|
promise.set_value(Unit());
|
|
|
|
}
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
2019-06-08 11:26:35 +02:00
|
|
|
void CallActor::discard_call(bool is_disconnected, int32 duration, bool is_video, int64 connection_id,
|
|
|
|
Promise<> promise) {
|
2018-12-31 20:04:05 +01:00
|
|
|
promise.set_value(Unit());
|
|
|
|
if (state_ == State::Discarded || state_ == State::WaitDiscardResult || state_ == State::SendDiscardQuery) {
|
|
|
|
return;
|
|
|
|
}
|
2019-06-08 11:26:35 +02:00
|
|
|
is_video_ |= is_video;
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2018-07-13 01:43:57 +02:00
|
|
|
if (state_ == State::WaitRequestResult && !request_query_ref_.empty()) {
|
|
|
|
LOG(INFO) << "Cancel request call query";
|
|
|
|
cancel_query(request_query_ref_);
|
|
|
|
}
|
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
switch (call_state_.type) {
|
|
|
|
case CallState::Type::Empty:
|
|
|
|
case CallState::Type::Pending:
|
|
|
|
if (is_outgoing_) {
|
|
|
|
call_state_.discard_reason = CallDiscardReason::Missed;
|
|
|
|
} else {
|
|
|
|
call_state_.discard_reason = CallDiscardReason::Declined;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case CallState::Type::ExchangingKey:
|
|
|
|
call_state_.discard_reason = is_disconnected ? CallDiscardReason::Disconnected : CallDiscardReason::HungUp;
|
|
|
|
break;
|
|
|
|
case CallState::Type::Ready:
|
|
|
|
call_state_.discard_reason = is_disconnected ? CallDiscardReason::Disconnected : CallDiscardReason::HungUp;
|
|
|
|
duration_ = duration;
|
|
|
|
connection_id_ = connection_id;
|
|
|
|
break;
|
|
|
|
case CallState::Type::HangingUp:
|
|
|
|
case CallState::Type::Discarded:
|
|
|
|
case CallState::Type::Error:
|
|
|
|
default:
|
|
|
|
UNREACHABLE();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
call_state_.type = CallState::Type::HangingUp;
|
|
|
|
call_state_need_flush_ = true;
|
|
|
|
|
|
|
|
state_ = State::SendDiscardQuery;
|
|
|
|
loop();
|
|
|
|
}
|
|
|
|
|
2019-08-06 19:07:23 +02:00
|
|
|
void CallActor::rate_call(int32 rating, string comment, vector<td_api::object_ptr<td_api::CallProblem>> &&problems,
|
|
|
|
Promise<> promise) {
|
2018-12-31 20:04:05 +01:00
|
|
|
if (!call_state_.need_rating) {
|
|
|
|
return promise.set_error(Status::Error(400, "Unexpected sendCallRating"));
|
|
|
|
}
|
|
|
|
promise.set_value(Unit());
|
2019-08-06 19:07:23 +02:00
|
|
|
|
|
|
|
if (rating == 5) {
|
|
|
|
comment.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
std::unordered_set<string> tags;
|
|
|
|
for (auto &problem : problems) {
|
|
|
|
if (problem == nullptr) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
const char *tag = [problem_id = problem->get_id()] {
|
|
|
|
switch (problem_id) {
|
|
|
|
case td_api::callProblemEcho::ID:
|
|
|
|
return "echo";
|
2019-08-18 14:24:51 +02:00
|
|
|
case td_api::callProblemNoise::ID:
|
2019-08-06 19:07:23 +02:00
|
|
|
return "noise";
|
|
|
|
case td_api::callProblemInterruptions::ID:
|
|
|
|
return "interruptions";
|
|
|
|
case td_api::callProblemDistortedSpeech::ID:
|
|
|
|
return "distorted_speech";
|
|
|
|
case td_api::callProblemSilentLocal::ID:
|
|
|
|
return "silent_local";
|
|
|
|
case td_api::callProblemSilentRemote::ID:
|
|
|
|
return "silent_remote";
|
|
|
|
case td_api::callProblemDropped::ID:
|
|
|
|
return "dropped";
|
2020-08-21 19:53:39 +02:00
|
|
|
case td_api::callProblemDistortedVideo::ID:
|
|
|
|
return "distorted_video";
|
|
|
|
case td_api::callProblemPixelatedVideo::ID:
|
|
|
|
return "pixelated_video";
|
2019-08-06 19:07:23 +02:00
|
|
|
default:
|
|
|
|
UNREACHABLE();
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
}();
|
|
|
|
if (tags.insert(tag).second) {
|
|
|
|
if (!comment.empty()) {
|
|
|
|
comment += ' ';
|
|
|
|
}
|
|
|
|
comment += '#';
|
|
|
|
comment += tag;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-12 05:00:24 +02:00
|
|
|
auto tl_query = telegram_api::phone_setCallRating(0, false /*ignored*/, get_input_phone_call("rate_call"), rating,
|
|
|
|
std::move(comment));
|
2020-03-15 22:17:11 +01:00
|
|
|
auto query = G()->net_query_creator().create(tl_query);
|
2021-03-05 00:47:27 +01:00
|
|
|
send_with_promise(std::move(query),
|
|
|
|
PromiseCreator::lambda([actor_id = actor_id(this)](Result<NetQueryPtr> r_net_query) {
|
|
|
|
send_closure(actor_id, &CallActor::on_set_rating_query_result, std::move(r_net_query));
|
2018-12-31 20:04:05 +01:00
|
|
|
}));
|
|
|
|
loop();
|
|
|
|
}
|
|
|
|
|
2021-03-05 00:47:27 +01:00
|
|
|
void CallActor::on_set_rating_query_result(Result<NetQueryPtr> r_net_query) {
|
|
|
|
auto res = fetch_result<telegram_api::phone_setCallRating>(std::move(r_net_query));
|
2018-12-31 20:04:05 +01:00
|
|
|
if (res.is_error()) {
|
|
|
|
return on_error(res.move_as_error());
|
|
|
|
}
|
|
|
|
call_state_.need_rating = false;
|
2020-12-21 20:06:52 +01:00
|
|
|
send_closure(G()->updates_manager(), &UpdatesManager::on_get_updates, res.move_as_ok(), Promise<Unit>());
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void CallActor::send_call_debug_information(string data, Promise<> promise) {
|
|
|
|
if (!call_state_.need_debug_information) {
|
|
|
|
return promise.set_error(Status::Error(400, "Unexpected sendCallDebugInformation"));
|
|
|
|
}
|
|
|
|
promise.set_value(Unit());
|
2018-10-24 20:37:12 +02:00
|
|
|
auto tl_query = telegram_api::phone_saveCallDebug(get_input_phone_call("send_call_debug_information"),
|
2018-12-31 20:04:05 +01:00
|
|
|
make_tl_object<telegram_api::dataJSON>(std::move(data)));
|
2020-03-15 22:17:11 +01:00
|
|
|
auto query = G()->net_query_creator().create(tl_query);
|
2021-03-05 00:47:27 +01:00
|
|
|
send_with_promise(std::move(query),
|
|
|
|
PromiseCreator::lambda([actor_id = actor_id(this)](Result<NetQueryPtr> r_net_query) {
|
|
|
|
send_closure(actor_id, &CallActor::on_set_debug_query_result, std::move(r_net_query));
|
2018-12-31 20:04:05 +01:00
|
|
|
}));
|
|
|
|
loop();
|
|
|
|
}
|
|
|
|
|
2021-03-05 00:47:27 +01:00
|
|
|
void CallActor::on_set_debug_query_result(Result<NetQueryPtr> r_net_query) {
|
|
|
|
auto res = fetch_result<telegram_api::phone_saveCallDebug>(std::move(r_net_query));
|
2018-12-31 20:04:05 +01:00
|
|
|
if (res.is_error()) {
|
|
|
|
return on_error(res.move_as_error());
|
|
|
|
}
|
|
|
|
call_state_.need_debug_information = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Requests
|
|
|
|
void CallActor::update_call(tl_object_ptr<telegram_api::PhoneCall> call) {
|
2018-07-11 22:37:40 +02:00
|
|
|
LOG(INFO) << "Receive " << to_string(call);
|
2018-12-31 20:04:05 +01:00
|
|
|
Status status;
|
|
|
|
downcast_call(*call, [&](auto &call) { status = this->do_update_call(call); });
|
|
|
|
if (status.is_error()) {
|
2018-07-11 22:37:40 +02:00
|
|
|
LOG(INFO) << "Receive error " << status << ", while handling update " << to_string(call);
|
2018-12-31 20:04:05 +01:00
|
|
|
on_error(std::move(status));
|
|
|
|
}
|
|
|
|
loop();
|
|
|
|
}
|
|
|
|
|
|
|
|
void CallActor::update_call_inner(tl_object_ptr<telegram_api::phone_phoneCall> call) {
|
2018-07-11 22:37:40 +02:00
|
|
|
LOG(INFO) << "Update call with " << to_string(call);
|
2019-01-12 04:48:39 +01:00
|
|
|
send_closure(G()->contacts_manager(), &ContactsManager::on_get_users, std::move(call->users_), "UpdatePhoneCall");
|
2018-12-31 20:04:05 +01:00
|
|
|
update_call(std::move(call->phone_call_));
|
|
|
|
}
|
|
|
|
|
|
|
|
Status CallActor::do_update_call(telegram_api::phoneCallEmpty &call) {
|
|
|
|
return Status::Error(400, "Call is finished");
|
|
|
|
}
|
|
|
|
|
|
|
|
Status CallActor::do_update_call(telegram_api::phoneCallWaiting &call) {
|
|
|
|
if (state_ != State::WaitRequestResult && state_ != State::WaitAcceptResult) {
|
|
|
|
return Status::Error(500, PSLICE() << "Drop unexpected " << to_string(call));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (state_ == State::WaitAcceptResult) {
|
2018-07-12 16:44:22 +02:00
|
|
|
LOG(DEBUG) << "Do update call to Waiting";
|
2018-07-13 04:06:04 +02:00
|
|
|
on_begin_exchanging_key();
|
2018-12-31 20:04:05 +01:00
|
|
|
} else {
|
2018-07-12 16:44:22 +02:00
|
|
|
LOG(DEBUG) << "Do update call to Waiting";
|
2018-12-31 20:04:05 +01:00
|
|
|
if ((call.flags_ & telegram_api::phoneCallWaiting::RECEIVE_DATE_MASK) != 0) {
|
|
|
|
call_state_.is_received = true;
|
|
|
|
call_state_need_flush_ = true;
|
2020-09-24 23:46:16 +02:00
|
|
|
int64 call_ring_timeout_ms = G()->shared_config().get_option_integer("call_ring_timeout_ms", 90000);
|
2020-09-30 21:21:32 +02:00
|
|
|
set_timeout_in(static_cast<double>(call_ring_timeout_ms) * 0.001);
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
call_id_ = call.id_;
|
|
|
|
call_access_hash_ = call.access_hash_;
|
2018-10-24 20:38:48 +02:00
|
|
|
is_call_id_inited_ = true;
|
2019-06-08 11:26:35 +02:00
|
|
|
is_video_ |= (call.flags_ & telegram_api::phoneCallWaiting::VIDEO_MASK) != 0;
|
2021-07-26 19:32:51 +02:00
|
|
|
call_admin_user_id_ = UserId(call.admin_id_);
|
|
|
|
// call_participant_user_id_ = UserId(call.participant_id_);
|
2018-12-31 20:04:05 +01:00
|
|
|
if (call_id_promise_) {
|
|
|
|
call_id_promise_.set_value(std::move(call.id_));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!call_state_.is_created) {
|
|
|
|
call_state_.is_created = true;
|
|
|
|
call_state_need_flush_ = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return Status::OK();
|
|
|
|
}
|
|
|
|
|
|
|
|
Status CallActor::do_update_call(telegram_api::phoneCallRequested &call) {
|
|
|
|
if (state_ != State::Empty) {
|
|
|
|
return Status::Error(500, PSLICE() << "Drop unexpected " << to_string(call));
|
|
|
|
}
|
2018-07-12 16:44:22 +02:00
|
|
|
LOG(DEBUG) << "Do update call to Requested";
|
2018-12-31 20:04:05 +01:00
|
|
|
call_id_ = call.id_;
|
|
|
|
call_access_hash_ = call.access_hash_;
|
2018-10-24 20:33:06 +02:00
|
|
|
is_call_id_inited_ = true;
|
2019-06-08 11:26:35 +02:00
|
|
|
is_video_ |= (call.flags_ & telegram_api::phoneCallRequested::VIDEO_MASK) != 0;
|
2021-07-26 19:32:51 +02:00
|
|
|
call_admin_user_id_ = UserId(call.admin_id_);
|
|
|
|
// call_participant_user_id_ = UserId(call.participant_id_);
|
2018-12-31 20:04:05 +01:00
|
|
|
if (call_id_promise_) {
|
|
|
|
call_id_promise_.set_value(std::move(call.id_));
|
|
|
|
}
|
|
|
|
|
|
|
|
dh_handshake_.set_g_a_hash(call.g_a_hash_.as_slice());
|
|
|
|
state_ = State::SendAcceptQuery;
|
|
|
|
|
|
|
|
call_state_.type = CallState::Type::Pending;
|
|
|
|
call_state_.is_created = true;
|
|
|
|
call_state_.is_received = true;
|
|
|
|
call_state_need_flush_ = true;
|
|
|
|
|
|
|
|
send_received_query();
|
|
|
|
return Status::OK();
|
|
|
|
}
|
|
|
|
|
2018-10-24 20:37:12 +02:00
|
|
|
tl_object_ptr<telegram_api::inputPhoneCall> CallActor::get_input_phone_call(const char *source) {
|
2019-02-12 17:17:20 +01:00
|
|
|
LOG_CHECK(is_call_id_inited_) << source;
|
2018-12-31 20:04:05 +01:00
|
|
|
return make_tl_object<telegram_api::inputPhoneCall>(call_id_, call_access_hash_);
|
|
|
|
}
|
|
|
|
|
|
|
|
Status CallActor::do_update_call(telegram_api::phoneCallAccepted &call) {
|
|
|
|
if (state_ != State::WaitRequestResult) {
|
|
|
|
return Status::Error(500, PSLICE() << "Drop unexpected " << to_string(call));
|
|
|
|
}
|
|
|
|
|
2018-07-12 16:44:22 +02:00
|
|
|
LOG(DEBUG) << "Do update call to Accepted";
|
2018-10-24 20:44:14 +02:00
|
|
|
if (!is_call_id_inited_) {
|
|
|
|
call_id_ = call.id_;
|
|
|
|
call_access_hash_ = call.access_hash_;
|
|
|
|
is_call_id_inited_ = true;
|
2021-07-26 19:32:51 +02:00
|
|
|
call_admin_user_id_ = UserId(call.admin_id_);
|
|
|
|
// call_participant_user_id_ = UserId(call.participant_id_);
|
2018-10-24 20:44:14 +02:00
|
|
|
if (call_id_promise_) {
|
|
|
|
call_id_promise_.set_value(std::move(call.id_));
|
|
|
|
}
|
|
|
|
}
|
2019-06-08 11:26:35 +02:00
|
|
|
is_video_ |= (call.flags_ & telegram_api::phoneCallAccepted::VIDEO_MASK) != 0;
|
2018-12-31 20:04:05 +01:00
|
|
|
dh_handshake_.set_g_a(call.g_b_.as_slice());
|
2018-08-09 22:41:01 +02:00
|
|
|
TRY_STATUS(dh_handshake_.run_checks(true, DhCache::instance()));
|
2018-12-31 20:04:05 +01:00
|
|
|
std::tie(call_state_.key_fingerprint, call_state_.key) = dh_handshake_.gen_key();
|
|
|
|
state_ = State::SendConfirmQuery;
|
2018-07-13 04:06:04 +02:00
|
|
|
on_begin_exchanging_key();
|
|
|
|
return Status::OK();
|
|
|
|
}
|
|
|
|
|
|
|
|
void CallActor::on_begin_exchanging_key() {
|
2018-12-31 20:04:05 +01:00
|
|
|
call_state_.type = CallState::Type::ExchangingKey;
|
|
|
|
call_state_need_flush_ = true;
|
2020-09-24 23:46:16 +02:00
|
|
|
int64 call_receive_timeout_ms = G()->shared_config().get_option_integer("call_receive_timeout_ms", 20000);
|
2021-10-20 00:27:02 +02:00
|
|
|
auto timeout = static_cast<double>(call_receive_timeout_ms) * 0.001;
|
2018-07-13 04:06:04 +02:00
|
|
|
LOG(INFO) << "Set call timeout to " << timeout;
|
|
|
|
set_timeout_in(timeout);
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Status CallActor::do_update_call(telegram_api::phoneCall &call) {
|
|
|
|
if (state_ != State::WaitAcceptResult && state_ != State::WaitConfirmResult) {
|
|
|
|
return Status::Error(500, PSLICE() << "Drop unexpected " << to_string(call));
|
|
|
|
}
|
2018-07-13 04:06:04 +02:00
|
|
|
cancel_timeout();
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2020-08-05 20:34:04 +02:00
|
|
|
is_video_ |= (call.flags_ & telegram_api::phoneCall::VIDEO_MASK) != 0;
|
|
|
|
|
2018-07-12 16:44:22 +02:00
|
|
|
LOG(DEBUG) << "Do update call to Ready from state " << static_cast<int32>(state_);
|
2018-12-31 20:04:05 +01:00
|
|
|
if (state_ == State::WaitAcceptResult) {
|
|
|
|
dh_handshake_.set_g_a(call.g_a_or_b_.as_slice());
|
2018-08-09 22:41:01 +02:00
|
|
|
TRY_STATUS(dh_handshake_.run_checks(true, DhCache::instance()));
|
2018-12-31 20:04:05 +01:00
|
|
|
std::tie(call_state_.key_fingerprint, call_state_.key) = dh_handshake_.gen_key();
|
|
|
|
}
|
|
|
|
if (call_state_.key_fingerprint != call.key_fingerprint_) {
|
|
|
|
return Status::Error(400, "Key fingerprints mismatch");
|
|
|
|
}
|
|
|
|
|
|
|
|
call_state_.emojis_fingerprint =
|
|
|
|
get_emojis_fingerprint(call_state_.key, is_outgoing_ ? dh_handshake_.get_g_b() : dh_handshake_.get_g_a());
|
|
|
|
|
2019-06-08 11:26:35 +02:00
|
|
|
for (auto &connection : call.connections_) {
|
2021-10-19 17:11:16 +02:00
|
|
|
call_state_.connections.emplace_back(*connection);
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
2020-06-22 03:28:03 +02:00
|
|
|
call_state_.protocol = CallProtocol(*call.protocol_);
|
2019-02-04 23:08:15 +01:00
|
|
|
call_state_.allow_p2p = (call.flags_ & telegram_api::phoneCall::P2P_ALLOWED_MASK) != 0;
|
2018-12-31 20:04:05 +01:00
|
|
|
call_state_.type = CallState::Type::Ready;
|
|
|
|
call_state_need_flush_ = true;
|
|
|
|
|
|
|
|
return Status::OK();
|
|
|
|
}
|
|
|
|
|
|
|
|
Status CallActor::do_update_call(telegram_api::phoneCallDiscarded &call) {
|
2018-07-12 16:44:22 +02:00
|
|
|
LOG(DEBUG) << "Do update call to Discarded";
|
2019-06-08 11:26:35 +02:00
|
|
|
on_call_discarded(get_call_discard_reason(call.reason_), call.need_rating_, call.need_debug_, call.video_);
|
2018-07-13 01:43:57 +02:00
|
|
|
return Status::OK();
|
|
|
|
}
|
|
|
|
|
2019-06-08 11:26:35 +02:00
|
|
|
void CallActor::on_call_discarded(CallDiscardReason reason, bool need_rating, bool need_debug, bool is_video) {
|
2018-12-31 20:04:05 +01:00
|
|
|
state_ = State::Discarded;
|
2019-06-08 11:26:35 +02:00
|
|
|
is_video_ |= is_video;
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
if (call_state_.discard_reason == CallDiscardReason::Empty || reason != CallDiscardReason::Empty) {
|
|
|
|
call_state_.discard_reason = reason;
|
|
|
|
}
|
|
|
|
if (call_state_.type != CallState::Type::Error) {
|
2018-07-13 01:43:57 +02:00
|
|
|
call_state_.need_rating = need_rating;
|
|
|
|
call_state_.need_debug_information = need_debug;
|
2018-12-31 20:04:05 +01:00
|
|
|
call_state_.type = CallState::Type::Discarded;
|
|
|
|
call_state_need_flush_ = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CallActor::load_dh_config() {
|
|
|
|
if (dh_config_ready_) {
|
2018-07-11 22:37:40 +02:00
|
|
|
LOG(DEBUG) << "Dh config is ready";
|
2018-12-31 20:04:05 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (!dh_config_query_sent_) {
|
|
|
|
dh_config_query_sent_ = true;
|
|
|
|
do_load_dh_config(PromiseCreator::lambda([actor_id = actor_id(this)](Result<std::shared_ptr<DhConfig>> dh_config) {
|
|
|
|
send_closure(actor_id, &CallActor::on_dh_config, std::move(dh_config), false);
|
|
|
|
}));
|
|
|
|
}
|
2018-07-11 22:37:40 +02:00
|
|
|
LOG(INFO) << "Dh config is not loaded";
|
2018-12-31 20:04:05 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CallActor::on_error(Status status) {
|
|
|
|
CHECK(status.is_error());
|
2018-07-13 01:43:57 +02:00
|
|
|
LOG(INFO) << "Receive error " << status;
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2018-07-13 01:43:57 +02:00
|
|
|
if (state_ == State::WaitRequestResult && !request_query_ref_.empty()) {
|
|
|
|
LOG(INFO) << "Cancel request call query";
|
|
|
|
cancel_query(request_query_ref_);
|
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
if (state_ == State::WaitDiscardResult || state_ == State::Discarded) {
|
|
|
|
state_ = State::Discarded;
|
|
|
|
} else {
|
|
|
|
state_ = State::SendDiscardQuery;
|
|
|
|
call_state_.discard_reason =
|
|
|
|
call_state_.type == CallState::Type::Pending ? CallDiscardReason::Missed : CallDiscardReason::Disconnected;
|
|
|
|
}
|
|
|
|
|
|
|
|
call_state_.type = CallState::Type::Error;
|
|
|
|
call_state_.error = std::move(status);
|
|
|
|
call_state_need_flush_ = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CallActor::on_dh_config(Result<std::shared_ptr<DhConfig>> r_dh_config, bool dummy) {
|
|
|
|
if (r_dh_config.is_error()) {
|
|
|
|
return on_error(r_dh_config.move_as_error());
|
|
|
|
}
|
2018-08-09 22:41:01 +02:00
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
dh_config_ = r_dh_config.move_as_ok();
|
2021-07-05 20:04:23 +02:00
|
|
|
auto check_result = mtproto::DhHandshake::check_config(dh_config_->g, dh_config_->prime, DhCache::instance());
|
2018-08-09 22:41:01 +02:00
|
|
|
if (check_result.is_error()) {
|
|
|
|
return on_error(std::move(check_result));
|
|
|
|
}
|
|
|
|
|
|
|
|
dh_config_ready_ = true;
|
2018-12-31 20:04:05 +01:00
|
|
|
yield();
|
|
|
|
}
|
|
|
|
|
|
|
|
void CallActor::do_load_dh_config(Promise<std::shared_ptr<DhConfig>> promise) {
|
|
|
|
//TODO: move to external actor
|
|
|
|
auto dh_config = G()->get_dh_config();
|
|
|
|
int32 version = 0;
|
|
|
|
if (dh_config) {
|
|
|
|
version = dh_config->version;
|
|
|
|
}
|
|
|
|
int random_length = 0;
|
|
|
|
telegram_api::messages_getDhConfig tl_query(version, random_length);
|
2020-03-15 22:17:11 +01:00
|
|
|
auto query = G()->net_query_creator().create(tl_query);
|
2018-12-31 20:04:05 +01:00
|
|
|
send_with_promise(std::move(query),
|
|
|
|
PromiseCreator::lambda([actor_id = actor_id(this), old_dh_config = std::move(dh_config),
|
|
|
|
promise = std::move(promise)](Result<NetQueryPtr> result_query) mutable {
|
|
|
|
promise.set_result([&]() -> Result<std::shared_ptr<DhConfig>> {
|
|
|
|
TRY_RESULT(query, std::move(result_query));
|
|
|
|
TRY_RESULT(new_dh_config, fetch_result<telegram_api::messages_getDhConfig>(std::move(query)));
|
|
|
|
if (new_dh_config->get_id() == telegram_api::messages_dhConfig::ID) {
|
|
|
|
auto dh = move_tl_object_as<telegram_api::messages_dhConfig>(new_dh_config);
|
|
|
|
auto dh_config = std::make_shared<DhConfig>();
|
|
|
|
dh_config->version = dh->version_;
|
|
|
|
dh_config->prime = dh->p_.as_slice().str();
|
|
|
|
dh_config->g = dh->g_;
|
2018-08-07 21:43:29 +02:00
|
|
|
Random::add_seed(dh->random_.as_slice());
|
2018-12-31 20:04:05 +01:00
|
|
|
G()->set_dh_config(dh_config);
|
|
|
|
return std::move(dh_config);
|
2018-08-07 21:43:29 +02:00
|
|
|
} else if (new_dh_config->get_id() == telegram_api::messages_dhConfigNotModified::ID) {
|
|
|
|
auto dh = move_tl_object_as<telegram_api::messages_dhConfigNotModified>(new_dh_config);
|
|
|
|
Random::add_seed(dh->random_.as_slice());
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
if (old_dh_config) {
|
|
|
|
return std::move(old_dh_config);
|
|
|
|
}
|
|
|
|
return Status::Error(500, "Can't load DhConfig");
|
|
|
|
}());
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
|
|
|
void CallActor::send_received_query() {
|
2018-10-24 20:37:12 +02:00
|
|
|
auto tl_query = telegram_api::phone_receivedCall(get_input_phone_call("send_received_query"));
|
2020-03-15 22:17:11 +01:00
|
|
|
auto query = G()->net_query_creator().create(tl_query);
|
2021-03-05 00:47:27 +01:00
|
|
|
send_with_promise(std::move(query),
|
|
|
|
PromiseCreator::lambda([actor_id = actor_id(this)](Result<NetQueryPtr> r_net_query) {
|
|
|
|
send_closure(actor_id, &CallActor::on_received_query_result, std::move(r_net_query));
|
2018-12-31 20:04:05 +01:00
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
2021-03-05 00:47:27 +01:00
|
|
|
void CallActor::on_received_query_result(Result<NetQueryPtr> r_net_query) {
|
|
|
|
auto res = fetch_result<telegram_api::phone_receivedCall>(std::move(r_net_query));
|
2018-12-31 20:04:05 +01:00
|
|
|
if (res.is_error()) {
|
|
|
|
return on_error(res.move_as_error());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CallActor::try_send_request_query() {
|
2018-10-07 22:40:58 +02:00
|
|
|
LOG(INFO) << "Trying to send request query";
|
2018-12-31 20:04:05 +01:00
|
|
|
if (!load_dh_config()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
dh_handshake_.set_config(dh_config_->g, dh_config_->prime);
|
|
|
|
CHECK(input_user_ != nullptr);
|
2019-06-08 11:26:35 +02:00
|
|
|
int32 flags = 0;
|
|
|
|
if (is_video_) {
|
|
|
|
flags |= telegram_api::phone_requestCall::VIDEO_MASK;
|
|
|
|
}
|
|
|
|
auto tl_query = telegram_api::phone_requestCall(flags, false /*ignored*/, std::move(input_user_),
|
|
|
|
Random::secure_int32(), BufferSlice(dh_handshake_.get_g_b_hash()),
|
2020-06-22 03:28:03 +02:00
|
|
|
call_state_.protocol.get_input_phone_call_protocol());
|
2020-03-15 22:17:11 +01:00
|
|
|
auto query = G()->net_query_creator().create(tl_query);
|
2018-12-31 20:04:05 +01:00
|
|
|
state_ = State::WaitRequestResult;
|
2020-09-24 23:46:16 +02:00
|
|
|
int64 call_receive_timeout_ms = G()->shared_config().get_option_integer("call_receive_timeout_ms", 20000);
|
2021-10-20 00:27:02 +02:00
|
|
|
auto timeout = static_cast<double>(call_receive_timeout_ms) * 0.001;
|
2018-07-13 01:43:57 +02:00
|
|
|
LOG(INFO) << "Set call timeout to " << timeout;
|
|
|
|
set_timeout_in(timeout);
|
2020-06-10 01:44:54 +02:00
|
|
|
query->total_timeout_limit_ = max(timeout, 10.0);
|
2018-07-13 01:43:57 +02:00
|
|
|
request_query_ref_ = query.get_weak();
|
2021-03-05 00:47:27 +01:00
|
|
|
send_with_promise(std::move(query),
|
|
|
|
PromiseCreator::lambda([actor_id = actor_id(this)](Result<NetQueryPtr> r_net_query) {
|
|
|
|
send_closure(actor_id, &CallActor::on_request_query_result, std::move(r_net_query));
|
2018-12-31 20:04:05 +01:00
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
2021-03-05 00:47:27 +01:00
|
|
|
void CallActor::on_request_query_result(Result<NetQueryPtr> r_net_query) {
|
|
|
|
auto res = fetch_result<telegram_api::phone_requestCall>(std::move(r_net_query));
|
2018-12-31 20:04:05 +01:00
|
|
|
if (res.is_error()) {
|
|
|
|
return on_error(res.move_as_error());
|
|
|
|
}
|
|
|
|
update_call_inner(res.move_as_ok());
|
|
|
|
}
|
|
|
|
|
|
|
|
//phone.acceptCall#3bd2b4a0 peer:InputPhoneCall g_b:bytes protocol:PhoneCallProtocol = phone.PhoneCall;
|
|
|
|
void CallActor::try_send_accept_query() {
|
2018-10-07 22:40:58 +02:00
|
|
|
LOG(INFO) << "Trying to send accept query";
|
2018-12-31 20:04:05 +01:00
|
|
|
if (!load_dh_config()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!is_accepted_) {
|
2018-07-11 22:37:40 +02:00
|
|
|
LOG(DEBUG) << "Call is not accepted";
|
2018-12-31 20:04:05 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
dh_handshake_.set_config(dh_config_->g, dh_config_->prime);
|
2020-06-22 03:28:03 +02:00
|
|
|
auto tl_query = telegram_api::phone_acceptCall(get_input_phone_call("try_send_accept_query"),
|
|
|
|
BufferSlice(dh_handshake_.get_g_b()),
|
|
|
|
call_state_.protocol.get_input_phone_call_protocol());
|
2020-03-15 22:17:11 +01:00
|
|
|
auto query = G()->net_query_creator().create(tl_query);
|
2018-12-31 20:04:05 +01:00
|
|
|
state_ = State::WaitAcceptResult;
|
2021-03-05 00:47:27 +01:00
|
|
|
send_with_promise(std::move(query),
|
|
|
|
PromiseCreator::lambda([actor_id = actor_id(this)](Result<NetQueryPtr> r_net_query) {
|
|
|
|
send_closure(actor_id, &CallActor::on_accept_query_result, std::move(r_net_query));
|
2018-12-31 20:04:05 +01:00
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
2021-03-05 00:47:27 +01:00
|
|
|
void CallActor::on_accept_query_result(Result<NetQueryPtr> r_net_query) {
|
|
|
|
auto res = fetch_result<telegram_api::phone_acceptCall>(std::move(r_net_query));
|
2018-12-31 20:04:05 +01:00
|
|
|
if (res.is_error()) {
|
|
|
|
return on_error(res.move_as_error());
|
|
|
|
}
|
|
|
|
update_call_inner(res.move_as_ok());
|
|
|
|
}
|
|
|
|
|
|
|
|
//phone.confirmCall#2efe1722 peer:InputPhoneCall g_a:bytes key_fingerprint:long protocol:PhoneCallProtocol = phone.PhoneCall;
|
|
|
|
void CallActor::try_send_confirm_query() {
|
2018-10-07 22:40:58 +02:00
|
|
|
LOG(INFO) << "Trying to send confirm query";
|
2018-12-31 20:04:05 +01:00
|
|
|
if (!load_dh_config()) {
|
|
|
|
return;
|
|
|
|
}
|
2018-10-24 20:37:12 +02:00
|
|
|
auto tl_query = telegram_api::phone_confirmCall(get_input_phone_call("try_send_confirm_query"),
|
|
|
|
BufferSlice(dh_handshake_.get_g_b()), call_state_.key_fingerprint,
|
2020-06-22 03:28:03 +02:00
|
|
|
call_state_.protocol.get_input_phone_call_protocol());
|
2020-03-15 22:17:11 +01:00
|
|
|
auto query = G()->net_query_creator().create(tl_query);
|
2018-12-31 20:04:05 +01:00
|
|
|
state_ = State::WaitConfirmResult;
|
2021-03-05 00:47:27 +01:00
|
|
|
send_with_promise(std::move(query),
|
|
|
|
PromiseCreator::lambda([actor_id = actor_id(this)](Result<NetQueryPtr> r_net_query) {
|
|
|
|
send_closure(actor_id, &CallActor::on_confirm_query_result, std::move(r_net_query));
|
2018-12-31 20:04:05 +01:00
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
2021-03-05 00:47:27 +01:00
|
|
|
void CallActor::on_confirm_query_result(Result<NetQueryPtr> r_net_query) {
|
|
|
|
auto res = fetch_result<telegram_api::phone_confirmCall>(std::move(r_net_query));
|
2018-12-31 20:04:05 +01:00
|
|
|
if (res.is_error()) {
|
|
|
|
return on_error(res.move_as_error());
|
|
|
|
}
|
|
|
|
update_call_inner(res.move_as_ok());
|
|
|
|
}
|
|
|
|
|
|
|
|
void CallActor::try_send_discard_query() {
|
|
|
|
if (call_id_ == 0) {
|
2018-07-13 01:43:57 +02:00
|
|
|
LOG(INFO) << "Failed to send discard query, because call_id_ is unknown";
|
2019-06-08 11:26:35 +02:00
|
|
|
on_call_discarded(CallDiscardReason::Missed, false, false, is_video_);
|
2018-12-31 20:04:05 +01:00
|
|
|
yield();
|
|
|
|
return;
|
|
|
|
}
|
2018-07-13 01:43:57 +02:00
|
|
|
LOG(INFO) << "Trying to send discard query";
|
2019-06-08 11:26:35 +02:00
|
|
|
int32 flags = 0;
|
|
|
|
if (is_video_) {
|
|
|
|
flags |= telegram_api::phone_discardCall::VIDEO_MASK;
|
|
|
|
}
|
|
|
|
auto tl_query = telegram_api::phone_discardCall(
|
|
|
|
flags, false /*ignored*/, get_input_phone_call("try_send_discard_query"), duration_,
|
|
|
|
get_input_phone_call_discard_reason(call_state_.discard_reason), connection_id_);
|
2020-03-15 22:17:11 +01:00
|
|
|
auto query = G()->net_query_creator().create(tl_query);
|
2018-12-31 20:04:05 +01:00
|
|
|
state_ = State::WaitDiscardResult;
|
2021-03-05 00:47:27 +01:00
|
|
|
send_with_promise(std::move(query),
|
|
|
|
PromiseCreator::lambda([actor_id = actor_id(this)](Result<NetQueryPtr> r_net_query) {
|
|
|
|
send_closure(actor_id, &CallActor::on_discard_query_result, std::move(r_net_query));
|
2018-12-31 20:04:05 +01:00
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
2021-03-05 00:47:27 +01:00
|
|
|
void CallActor::on_discard_query_result(Result<NetQueryPtr> r_net_query) {
|
|
|
|
auto res = fetch_result<telegram_api::phone_discardCall>(std::move(r_net_query));
|
2018-12-31 20:04:05 +01:00
|
|
|
if (res.is_error()) {
|
|
|
|
return on_error(res.move_as_error());
|
|
|
|
}
|
2020-12-21 20:06:52 +01:00
|
|
|
send_closure(G()->updates_manager(), &UpdatesManager::on_get_updates, res.move_as_ok(), Promise<Unit>());
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void CallActor::flush_call_state() {
|
|
|
|
if (call_state_need_flush_) {
|
2018-12-18 21:59:35 +01:00
|
|
|
if (!is_outgoing_) {
|
|
|
|
if (call_state_.type == CallState::Type::Pending) {
|
|
|
|
if (!has_notification_) {
|
|
|
|
has_notification_ = true;
|
|
|
|
send_closure(G()->notification_manager(), &NotificationManager::add_call_notification,
|
2021-07-26 19:32:51 +02:00
|
|
|
DialogId(call_admin_user_id_), local_call_id_);
|
2018-12-18 21:59:35 +01:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (has_notification_) {
|
|
|
|
has_notification_ = false;
|
|
|
|
send_closure(G()->notification_manager(), &NotificationManager::remove_call_notification,
|
2021-07-26 19:32:51 +02:00
|
|
|
DialogId(call_admin_user_id_), local_call_id_);
|
2018-12-18 21:59:35 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
if (call_state_.type == CallState::Type::Ready && !call_state_has_config_) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
call_state_need_flush_ = false;
|
2018-01-31 14:39:11 +01:00
|
|
|
|
2018-12-04 13:50:57 +01:00
|
|
|
// TODO can't call const function
|
2018-02-03 00:05:11 +01:00
|
|
|
// send_closure(G()->contacts_manager(), &ContactsManager::get_user_id_object, user_id_, "flush_call_state");
|
2018-12-31 20:04:05 +01:00
|
|
|
send_closure(G()->td(), &Td::send_update,
|
2021-07-26 19:31:14 +02:00
|
|
|
make_tl_object<td_api::updateCall>(make_tl_object<td_api::call>(
|
2021-07-26 19:32:51 +02:00
|
|
|
local_call_id_.get(), is_outgoing_ ? user_id_.get() : call_admin_user_id_.get(), is_outgoing_,
|
2021-07-26 19:31:14 +02:00
|
|
|
is_video_, call_state_.get_call_state_object())));
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CallActor::start_up() {
|
|
|
|
auto tl_query = telegram_api::phone_getCallConfig();
|
2020-03-15 22:17:11 +01:00
|
|
|
auto query = G()->net_query_creator().create(tl_query);
|
2021-03-05 00:47:27 +01:00
|
|
|
send_with_promise(std::move(query),
|
|
|
|
PromiseCreator::lambda([actor_id = actor_id(this)](Result<NetQueryPtr> r_net_query) {
|
|
|
|
send_closure(actor_id, &CallActor::on_get_call_config_result, std::move(r_net_query));
|
2018-12-31 20:04:05 +01:00
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
2021-03-05 00:47:27 +01:00
|
|
|
void CallActor::on_get_call_config_result(Result<NetQueryPtr> r_net_query) {
|
|
|
|
auto res = fetch_result<telegram_api::phone_getCallConfig>(std::move(r_net_query));
|
2018-12-31 20:04:05 +01:00
|
|
|
if (res.is_error()) {
|
|
|
|
return on_error(res.move_as_error());
|
|
|
|
}
|
|
|
|
call_state_.config = res.ok()->data_;
|
|
|
|
call_state_has_config_ = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CallActor::loop() {
|
2020-05-29 10:26:08 +02:00
|
|
|
LOG(DEBUG) << "Enter loop for " << call_id_ << " in state " << static_cast<int32>(state_) << '/'
|
2018-07-13 01:43:57 +02:00
|
|
|
<< static_cast<int32>(call_state_.type);
|
2018-12-31 20:04:05 +01:00
|
|
|
flush_call_state();
|
|
|
|
switch (state_) {
|
|
|
|
case State::SendRequestQuery:
|
|
|
|
try_send_request_query();
|
|
|
|
break;
|
|
|
|
case State::SendAcceptQuery:
|
|
|
|
try_send_accept_query();
|
|
|
|
break;
|
|
|
|
case State::SendConfirmQuery:
|
|
|
|
try_send_confirm_query();
|
|
|
|
break;
|
|
|
|
case State::SendDiscardQuery:
|
|
|
|
try_send_discard_query();
|
|
|
|
break;
|
|
|
|
case State::Discarded: {
|
|
|
|
if (call_state_.type == CallState::Type::Discarded &&
|
|
|
|
(call_state_.need_rating || call_state_.need_debug_information)) {
|
|
|
|
break;
|
|
|
|
}
|
2020-05-29 10:26:08 +02:00
|
|
|
LOG(INFO) << "Close " << local_call_id_;
|
2018-12-31 20:04:05 +01:00
|
|
|
stop();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CallActor::timeout_expired() {
|
|
|
|
on_error(Status::Error(4005000, "Call timeout expired"));
|
|
|
|
yield();
|
|
|
|
}
|
|
|
|
|
|
|
|
void CallActor::on_result(NetQueryPtr query) {
|
|
|
|
auto token = get_link_token();
|
|
|
|
container_.extract(token).set_value(std::move(query));
|
|
|
|
yield(); // Call loop AFTER all events from the promise are executed
|
|
|
|
}
|
|
|
|
|
|
|
|
void CallActor::send_with_promise(NetQueryPtr query, Promise<NetQueryPtr> promise) {
|
|
|
|
auto id = container_.create(std::move(promise));
|
|
|
|
G()->net_query_dispatcher().dispatch_with_callback(std::move(query), actor_shared(this, id));
|
|
|
|
}
|
|
|
|
|
2018-04-19 17:39:30 +02:00
|
|
|
void CallActor::hangup() {
|
|
|
|
container_.for_each(
|
2021-10-07 15:36:21 +02:00
|
|
|
[](auto id, Promise<NetQueryPtr> &promise) { promise.set_error(Global::request_aborted_error()); });
|
2018-04-19 17:39:30 +02:00
|
|
|
stop();
|
|
|
|
}
|
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
vector<string> CallActor::get_emojis_fingerprint(const string &key, const string &g_a) {
|
|
|
|
string str = key + g_a;
|
|
|
|
unsigned char sha256_buf[32];
|
|
|
|
sha256(str, {sha256_buf, 32});
|
|
|
|
|
|
|
|
vector<string> result;
|
|
|
|
result.reserve(4);
|
|
|
|
for (int i = 0; i < 4; i++) {
|
2020-06-17 03:16:59 +02:00
|
|
|
uint64 num = big_endian_to_host64(as<uint64>(sha256_buf + 8 * i));
|
2018-12-31 20:04:05 +01:00
|
|
|
result.push_back(get_emoji_fingerprint(num));
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace td
|