2023-11-21 13:14:26 +01:00
|
|
|
//
|
|
|
|
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2023
|
|
|
|
//
|
|
|
|
// 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
|
|
|
|
|
2023-11-23 11:25:56 +01:00
|
|
|
#include "td/telegram/files/FileId.h"
|
|
|
|
#include "td/telegram/MessageContentType.h"
|
|
|
|
#include "td/telegram/MessageFullId.h"
|
2023-11-21 17:27:25 +01:00
|
|
|
#include "td/telegram/td_api.h"
|
2023-11-21 18:32:23 +01:00
|
|
|
#include "td/telegram/telegram_api.h"
|
2023-11-23 11:25:56 +01:00
|
|
|
#include "td/telegram/TranscriptionInfo.h"
|
2023-11-21 17:27:25 +01:00
|
|
|
|
2023-11-21 13:14:26 +01:00
|
|
|
#include "td/actor/actor.h"
|
2023-11-21 18:32:23 +01:00
|
|
|
#include "td/actor/MultiTimeout.h"
|
2023-11-23 14:16:39 +01:00
|
|
|
#include "td/actor/Timeout.h"
|
2023-11-21 13:14:26 +01:00
|
|
|
|
|
|
|
#include "td/utils/common.h"
|
2023-11-21 18:32:23 +01:00
|
|
|
#include "td/utils/FlatHashMap.h"
|
2023-11-23 11:25:56 +01:00
|
|
|
#include "td/utils/FlatHashSet.h"
|
|
|
|
#include "td/utils/Promise.h"
|
2023-11-21 18:32:23 +01:00
|
|
|
#include "td/utils/Status.h"
|
|
|
|
|
|
|
|
#include <functional>
|
2023-11-23 11:25:56 +01:00
|
|
|
#include <utility>
|
2023-11-21 13:14:26 +01:00
|
|
|
|
|
|
|
namespace td {
|
|
|
|
|
|
|
|
class Td;
|
|
|
|
|
|
|
|
class TranscriptionManager final : public Actor {
|
|
|
|
public:
|
|
|
|
TranscriptionManager(Td *td, ActorShared<> parent);
|
2023-11-23 11:25:56 +01:00
|
|
|
TranscriptionManager(const TranscriptionManager &) = delete;
|
|
|
|
TranscriptionManager &operator=(const TranscriptionManager &) = delete;
|
|
|
|
TranscriptionManager(TranscriptionManager &&) = delete;
|
|
|
|
TranscriptionManager &operator=(TranscriptionManager &&) = delete;
|
|
|
|
~TranscriptionManager() final;
|
2023-11-21 13:14:26 +01:00
|
|
|
|
2023-11-21 17:27:25 +01:00
|
|
|
void on_update_trial_parameters(int32 weekly_number, int32 duration_max, int32 cooldown_until);
|
|
|
|
|
2023-11-23 11:25:56 +01:00
|
|
|
void register_voice(FileId file_id, MessageContentType content_type, MessageFullId message_full_id,
|
|
|
|
const char *source);
|
|
|
|
|
|
|
|
void unregister_voice(FileId file_id, MessageContentType content_type, MessageFullId message_full_id,
|
|
|
|
const char *source);
|
|
|
|
|
|
|
|
void recognize_speech(MessageFullId message_full_id, Promise<Unit> &&promise);
|
|
|
|
|
|
|
|
void on_transcription_completed(FileId file_id);
|
|
|
|
|
|
|
|
void rate_speech_recognition(MessageFullId message_full_id, bool is_good, Promise<Unit> &&promise);
|
2023-11-21 18:32:23 +01:00
|
|
|
|
|
|
|
void on_update_transcribed_audio(telegram_api::object_ptr<telegram_api::updateTranscribedAudio> &&update);
|
|
|
|
|
2023-11-21 17:31:27 +01:00
|
|
|
void get_current_state(vector<td_api::object_ptr<td_api::Update>> &updates) const;
|
|
|
|
|
2023-11-21 13:14:26 +01:00
|
|
|
private:
|
2023-11-21 18:32:23 +01:00
|
|
|
static constexpr int32 AUDIO_TRANSCRIPTION_TIMEOUT = 60;
|
|
|
|
|
2023-11-23 12:49:05 +01:00
|
|
|
struct TrialParameters {
|
|
|
|
int32 weekly_number_ = 0;
|
|
|
|
int32 duration_max_ = 0;
|
|
|
|
int32 left_tries_ = 0;
|
2023-11-23 13:35:54 +01:00
|
|
|
int32 next_reset_date_ = 0;
|
2023-11-23 12:49:05 +01:00
|
|
|
|
|
|
|
void update_left_tries();
|
|
|
|
|
|
|
|
td_api::object_ptr<td_api::updateSpeechRecognitionTrial> get_update_speech_recognition_trial_object() const;
|
|
|
|
|
|
|
|
template <class StorerT>
|
|
|
|
void store(StorerT &storer) const;
|
|
|
|
|
|
|
|
template <class ParserT>
|
|
|
|
void parse(ParserT &parser);
|
|
|
|
};
|
|
|
|
|
|
|
|
friend bool operator==(const TrialParameters &lhs, const TrialParameters &rhs);
|
|
|
|
|
2023-11-21 13:14:26 +01:00
|
|
|
void tear_down() final;
|
|
|
|
|
2023-11-21 18:32:23 +01:00
|
|
|
static void on_pending_audio_transcription_timeout_callback(void *td, int64 transcription_id);
|
|
|
|
|
2023-11-21 18:08:05 +01:00
|
|
|
static string get_trial_parameters_database_key();
|
|
|
|
|
|
|
|
void load_trial_parameters();
|
|
|
|
|
2023-11-23 12:49:05 +01:00
|
|
|
void set_trial_parameters(TrialParameters new_trial_parameters);
|
|
|
|
|
2023-11-23 14:16:39 +01:00
|
|
|
void set_speech_recognition_trial_timeout();
|
|
|
|
|
|
|
|
static void trial_parameters_timeout_static(void *td);
|
|
|
|
|
|
|
|
void on_trial_parameters_timeout();
|
|
|
|
|
2023-11-21 18:08:05 +01:00
|
|
|
void save_trial_parameters();
|
|
|
|
|
2023-11-21 17:27:25 +01:00
|
|
|
void send_update_speech_recognition_trial() const;
|
|
|
|
|
|
|
|
td_api::object_ptr<td_api::updateSpeechRecognitionTrial> get_update_speech_recognition_trial_object() const;
|
|
|
|
|
2023-11-23 11:25:56 +01:00
|
|
|
using FileInfo = std::pair<MessageContentType, FileId>;
|
|
|
|
|
|
|
|
TranscriptionInfo *get_transcription_info(const FileInfo &file_info, bool allow_creation);
|
|
|
|
|
2023-11-23 12:12:27 +01:00
|
|
|
void on_transcribed_audio(FileInfo file_info,
|
|
|
|
Result<telegram_api::object_ptr<telegram_api::messages_transcribedAudio>> r_audio);
|
|
|
|
|
2023-11-23 11:25:56 +01:00
|
|
|
using TranscribedAudioHandler =
|
|
|
|
std::function<void(Result<telegram_api::object_ptr<telegram_api::updateTranscribedAudio>>)>;
|
|
|
|
void subscribe_to_transcribed_audio_updates(int64 transcription_id, TranscribedAudioHandler on_update);
|
|
|
|
|
|
|
|
void on_transcribed_audio_update(FileInfo file_info, bool is_initial,
|
|
|
|
Result<telegram_api::object_ptr<telegram_api::updateTranscribedAudio>> r_update);
|
|
|
|
|
|
|
|
void on_transcription_updated(FileId file_id);
|
|
|
|
|
2023-11-21 18:32:23 +01:00
|
|
|
void on_pending_audio_transcription_failed(int64 transcription_id, Status &&error);
|
|
|
|
|
2023-11-21 13:14:26 +01:00
|
|
|
Td *td_;
|
|
|
|
ActorShared<> parent_;
|
2023-11-21 17:27:25 +01:00
|
|
|
|
2023-11-21 17:51:07 +01:00
|
|
|
TrialParameters trial_parameters_;
|
2023-11-23 14:16:39 +01:00
|
|
|
Timeout trial_parameters_timeout_;
|
2023-11-21 18:32:23 +01:00
|
|
|
|
|
|
|
FlatHashMap<int64, TranscribedAudioHandler> pending_audio_transcriptions_;
|
|
|
|
MultiTimeout pending_audio_transcription_timeout_{"PendingAudioTranscriptionTimeout"};
|
2023-11-23 11:25:56 +01:00
|
|
|
|
|
|
|
FlatHashMap<FileId, FlatHashSet<MessageFullId, MessageFullIdHash>, FileIdHash> voice_messages_;
|
|
|
|
FlatHashMap<MessageFullId, FileInfo, MessageFullIdHash> message_file_ids_;
|
2023-11-21 13:14:26 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace td
|