2018-12-31 20:04:05 +01:00
|
|
|
//
|
2024-01-01 01:07:21 +01:00
|
|
|
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2024
|
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)
|
|
|
|
//
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include "td/telegram/CallDiscardReason.h"
|
|
|
|
#include "td/telegram/CallId.h"
|
|
|
|
#include "td/telegram/DhConfig.h"
|
2022-04-22 17:17:36 +02:00
|
|
|
#include "td/telegram/files/FileId.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
#include "td/telegram/net/NetQuery.h"
|
2021-09-18 23:47:05 +02:00
|
|
|
#include "td/telegram/td_api.h"
|
|
|
|
#include "td/telegram/telegram_api.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
#include "td/telegram/UserId.h"
|
|
|
|
|
2021-09-18 23:47:05 +02:00
|
|
|
#include "td/mtproto/DhHandshake.h"
|
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
#include "td/actor/actor.h"
|
|
|
|
|
2022-06-02 15:19:16 +02:00
|
|
|
#include "td/utils/common.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
#include "td/utils/Container.h"
|
2022-06-27 12:30:18 +02:00
|
|
|
#include "td/utils/Promise.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
#include "td/utils/Status.h"
|
|
|
|
|
|
|
|
#include <memory>
|
|
|
|
|
|
|
|
namespace td {
|
2018-04-19 17:39:30 +02:00
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
struct CallProtocol {
|
|
|
|
bool udp_p2p{true};
|
|
|
|
bool udp_reflector{true};
|
|
|
|
int32 min_layer{65};
|
|
|
|
int32 max_layer{65};
|
2020-03-13 23:56:47 +01:00
|
|
|
vector<string> library_versions;
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2020-06-22 03:28:03 +02:00
|
|
|
CallProtocol() = default;
|
|
|
|
|
|
|
|
explicit CallProtocol(const td_api::callProtocol &protocol);
|
|
|
|
|
|
|
|
explicit CallProtocol(const telegram_api::phoneCallProtocol &protocol);
|
|
|
|
|
|
|
|
tl_object_ptr<telegram_api::phoneCallProtocol> get_input_phone_call_protocol() const;
|
|
|
|
|
|
|
|
tl_object_ptr<td_api::callProtocol> get_call_protocol_object() const;
|
2018-12-31 20:04:05 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
struct CallConnection {
|
2020-08-10 17:37:03 +02:00
|
|
|
enum class Type : int32 { Telegram, Webrtc };
|
|
|
|
Type type;
|
2018-12-31 20:04:05 +01:00
|
|
|
int64 id;
|
|
|
|
string ip;
|
|
|
|
string ipv6;
|
|
|
|
int32 port;
|
2020-08-10 17:37:03 +02:00
|
|
|
|
|
|
|
// Telegram
|
2018-12-31 20:04:05 +01:00
|
|
|
string peer_tag;
|
2022-05-07 10:42:48 +02:00
|
|
|
bool is_tcp = false;
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2020-08-10 17:37:03 +02:00
|
|
|
// WebRTC
|
|
|
|
string username;
|
|
|
|
string password;
|
|
|
|
bool supports_turn = false;
|
|
|
|
bool supports_stun = false;
|
2020-06-22 03:28:03 +02:00
|
|
|
|
2020-08-10 17:37:03 +02:00
|
|
|
explicit CallConnection(const telegram_api::PhoneConnection &connection);
|
2020-06-22 03:28:03 +02:00
|
|
|
|
2020-08-10 17:37:03 +02:00
|
|
|
tl_object_ptr<td_api::callServer> get_call_server_object() const;
|
2018-12-31 20:04:05 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
struct CallState {
|
2018-04-19 15:08:30 +02:00
|
|
|
enum class Type : int32 { Empty, Pending, ExchangingKey, Ready, HangingUp, Discarded, Error } type{Type::Empty};
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
CallProtocol protocol;
|
2021-10-19 17:11:16 +02:00
|
|
|
vector<CallConnection> connections;
|
2018-12-31 20:04:05 +01:00
|
|
|
CallDiscardReason discard_reason{CallDiscardReason::Empty};
|
|
|
|
bool is_created{false};
|
|
|
|
bool is_received{false};
|
|
|
|
bool need_debug_information{false};
|
|
|
|
bool need_rating{false};
|
2022-04-22 14:16:09 +02:00
|
|
|
bool need_log{false};
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
int64 key_fingerprint{0};
|
|
|
|
string key;
|
|
|
|
string config;
|
|
|
|
vector<string> emojis_fingerprint;
|
2024-04-02 14:22:19 +02:00
|
|
|
string custom_parameters;
|
2019-02-04 23:08:15 +01:00
|
|
|
bool allow_p2p{false};
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
Status error;
|
|
|
|
|
2020-06-22 03:28:03 +02:00
|
|
|
tl_object_ptr<td_api::CallState> get_call_state_object() const;
|
2018-12-31 20:04:05 +01:00
|
|
|
};
|
|
|
|
|
2021-07-04 04:58:54 +02:00
|
|
|
class CallActor final : public NetQueryCallback {
|
2018-12-31 20:04:05 +01:00
|
|
|
public:
|
|
|
|
CallActor(CallId call_id, ActorShared<> parent, Promise<int64> promise);
|
|
|
|
|
|
|
|
void create_call(UserId user_id, tl_object_ptr<telegram_api::InputUser> &&input_user, CallProtocol &&protocol,
|
2019-06-08 11:26:35 +02:00
|
|
|
bool is_video, Promise<CallId> &&promise);
|
2022-04-22 14:16:09 +02:00
|
|
|
void accept_call(CallProtocol &&protocol, Promise<Unit> promise);
|
2020-05-29 10:26:08 +02:00
|
|
|
void update_call_signaling_data(string data);
|
2022-04-22 14:16:09 +02:00
|
|
|
void send_call_signaling_data(string &&data, Promise<Unit> promise);
|
|
|
|
void discard_call(bool is_disconnected, int32 duration, bool is_video, int64 connection_id, Promise<Unit> promise);
|
2019-08-06 19:07:23 +02:00
|
|
|
void rate_call(int32 rating, string comment, vector<td_api::object_ptr<td_api::CallProblem>> &&problems,
|
2022-04-22 14:16:09 +02:00
|
|
|
Promise<Unit> promise);
|
|
|
|
void send_call_debug_information(string data, Promise<Unit> promise);
|
|
|
|
void send_call_log(td_api::object_ptr<td_api::InputFile> log_file, Promise<Unit> promise);
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
void update_call(tl_object_ptr<telegram_api::PhoneCall> call);
|
|
|
|
|
|
|
|
private:
|
|
|
|
void update_call_inner(tl_object_ptr<telegram_api::phone_phoneCall> call);
|
|
|
|
ActorShared<> parent_;
|
|
|
|
Promise<int64> call_id_promise_;
|
|
|
|
|
2021-07-05 20:04:23 +02:00
|
|
|
mtproto::DhHandshake dh_handshake_;
|
2018-12-31 20:04:05 +01:00
|
|
|
std::shared_ptr<DhConfig> dh_config_;
|
|
|
|
bool dh_config_query_sent_{false};
|
|
|
|
bool dh_config_ready_{false};
|
|
|
|
|
|
|
|
int32 duration_{0};
|
|
|
|
int64 connection_id_{0};
|
|
|
|
|
2018-04-19 15:08:30 +02:00
|
|
|
enum class State : int32 {
|
2018-12-31 20:04:05 +01:00
|
|
|
Empty,
|
|
|
|
SendRequestQuery,
|
|
|
|
WaitRequestResult,
|
|
|
|
SendAcceptQuery,
|
|
|
|
WaitAcceptResult,
|
|
|
|
SendConfirmQuery,
|
|
|
|
WaitConfirmResult,
|
|
|
|
SendDiscardQuery,
|
|
|
|
WaitDiscardResult,
|
|
|
|
Discarded
|
|
|
|
} state_{State::Empty};
|
|
|
|
bool is_accepted_{false};
|
|
|
|
|
|
|
|
bool is_outgoing_{false};
|
2019-06-08 11:26:35 +02:00
|
|
|
bool is_video_{false};
|
2018-12-31 20:04:05 +01:00
|
|
|
UserId user_id_;
|
|
|
|
tl_object_ptr<telegram_api::InputUser> input_user_;
|
|
|
|
|
|
|
|
CallId local_call_id_;
|
|
|
|
int64 call_id_{0};
|
2018-10-24 20:33:06 +02:00
|
|
|
bool is_call_id_inited_{false};
|
2018-12-18 21:59:35 +01:00
|
|
|
bool has_notification_{false};
|
2018-12-31 20:04:05 +01:00
|
|
|
int64 call_access_hash_{0};
|
2021-07-26 19:32:51 +02:00
|
|
|
UserId call_admin_user_id_;
|
|
|
|
// UserId call_participant_user_id_;
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
CallState call_state_;
|
|
|
|
bool call_state_need_flush_{false};
|
|
|
|
bool call_state_has_config_{false};
|
|
|
|
|
2018-07-13 01:43:57 +02:00
|
|
|
NetQueryRef request_query_ref_;
|
|
|
|
|
2018-10-24 20:37:12 +02:00
|
|
|
tl_object_ptr<telegram_api::inputPhoneCall> get_input_phone_call(const char *source);
|
2018-12-31 20:04:05 +01:00
|
|
|
bool load_dh_config();
|
|
|
|
void on_dh_config(Result<std::shared_ptr<DhConfig>> r_dh_config, bool dummy);
|
|
|
|
void do_load_dh_config(Promise<std::shared_ptr<DhConfig>> promise);
|
|
|
|
|
2023-07-01 12:19:46 +02:00
|
|
|
Status do_update_call(const telegram_api::phoneCallEmpty &call);
|
|
|
|
Status do_update_call(const telegram_api::phoneCallWaiting &call);
|
|
|
|
Status do_update_call(const telegram_api::phoneCallRequested &call);
|
|
|
|
Status do_update_call(const telegram_api::phoneCallAccepted &call);
|
|
|
|
Status do_update_call(const telegram_api::phoneCall &call);
|
|
|
|
Status do_update_call(const telegram_api::phoneCallDiscarded &call);
|
|
|
|
|
|
|
|
void on_get_call_id();
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
void send_received_query();
|
2021-03-05 00:47:27 +01:00
|
|
|
void on_received_query_result(Result<NetQueryPtr> r_net_query);
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
void try_send_request_query();
|
2021-03-05 00:47:27 +01:00
|
|
|
void on_request_query_result(Result<NetQueryPtr> r_net_query);
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
void try_send_accept_query();
|
2021-03-05 00:47:27 +01:00
|
|
|
void on_accept_query_result(Result<NetQueryPtr> r_net_query);
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
void try_send_confirm_query();
|
2021-03-05 00:47:27 +01:00
|
|
|
void on_confirm_query_result(Result<NetQueryPtr> r_net_query);
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
void try_send_discard_query();
|
2021-03-05 00:47:27 +01:00
|
|
|
void on_discard_query_result(Result<NetQueryPtr> r_net_query);
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2018-07-13 04:06:04 +02:00
|
|
|
void on_begin_exchanging_key();
|
|
|
|
|
2019-06-08 11:26:35 +02:00
|
|
|
void on_call_discarded(CallDiscardReason reason, bool need_rating, bool need_debug, bool is_video);
|
2018-07-13 01:43:57 +02:00
|
|
|
|
2021-03-05 00:47:27 +01:00
|
|
|
void on_set_rating_query_result(Result<NetQueryPtr> r_net_query);
|
2022-04-22 17:17:36 +02:00
|
|
|
|
|
|
|
void on_save_debug_query_result(Result<NetQueryPtr> r_net_query);
|
|
|
|
|
|
|
|
void upload_log_file(FileId file_id, Promise<Unit> &&promise);
|
|
|
|
|
|
|
|
void on_upload_log_file(FileId file_id, Promise<Unit> &&promise, tl_object_ptr<telegram_api::InputFile> input_file);
|
|
|
|
|
|
|
|
void on_upload_log_file_error(FileId file_id, Promise<Unit> &&promise, Status status);
|
|
|
|
|
|
|
|
void do_upload_log_file(FileId file_id, tl_object_ptr<telegram_api::InputFile> &&input_file, Promise<Unit> &&promise);
|
|
|
|
|
|
|
|
void on_save_log_query_result(FileId file_id, Promise<Unit> promise, Result<NetQueryPtr> r_net_query);
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2021-03-05 00:47:27 +01:00
|
|
|
void on_get_call_config_result(Result<NetQueryPtr> r_net_query);
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
void flush_call_state();
|
|
|
|
|
|
|
|
static vector<string> get_emojis_fingerprint(const string &key, const string &g_a);
|
|
|
|
|
2021-07-03 22:51:36 +02:00
|
|
|
void start_up() final;
|
|
|
|
void loop() final;
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
Container<Promise<NetQueryPtr>> container_;
|
2021-07-03 22:51:36 +02:00
|
|
|
void on_result(NetQueryPtr query) final;
|
2018-12-31 20:04:05 +01:00
|
|
|
void send_with_promise(NetQueryPtr query, Promise<NetQueryPtr> promise);
|
|
|
|
|
2021-07-03 22:51:36 +02:00
|
|
|
void timeout_expired() final;
|
|
|
|
void hangup() final;
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
void on_error(Status status);
|
|
|
|
};
|
2018-04-19 17:39:30 +02:00
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
} // namespace td
|