2018-12-31 20:04:05 +01:00
|
|
|
//
|
2022-01-01 01:35:39 +01:00
|
|
|
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2022
|
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/AudiosManager.h"
|
|
|
|
|
2021-05-27 20:15:30 +02:00
|
|
|
#include "td/telegram/AuthManager.h"
|
2022-08-14 14:04:08 +02:00
|
|
|
#include "td/telegram/DialogId.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
#include "td/telegram/files/FileManager.h"
|
2022-06-02 15:19:16 +02:00
|
|
|
#include "td/telegram/files/FileType.h"
|
2022-08-14 14:04:08 +02:00
|
|
|
#include "td/telegram/Global.h"
|
2022-04-09 22:21:07 +02:00
|
|
|
#include "td/telegram/PhotoFormat.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
#include "td/telegram/secret_api.h"
|
2021-05-27 20:15:30 +02:00
|
|
|
#include "td/telegram/Td.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2022-08-14 14:04:08 +02:00
|
|
|
#include "td/actor/actor.h"
|
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
#include "td/utils/logging.h"
|
|
|
|
#include "td/utils/misc.h"
|
2022-04-17 18:01:01 +02:00
|
|
|
#include "td/utils/PathView.h"
|
2021-05-17 14:21:11 +02:00
|
|
|
#include "td/utils/SliceBuilder.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
#include "td/utils/Status.h"
|
|
|
|
|
|
|
|
namespace td {
|
|
|
|
|
|
|
|
AudiosManager::AudiosManager(Td *td) : td_(td) {
|
|
|
|
}
|
|
|
|
|
2022-07-20 12:40:14 +02:00
|
|
|
AudiosManager::~AudiosManager() {
|
|
|
|
Scheduler::instance()->destroy_on_scheduler(G()->get_gc_scheduler_id(), audios_);
|
|
|
|
}
|
|
|
|
|
2018-09-27 20:14:32 +02:00
|
|
|
int32 AudiosManager::get_audio_duration(FileId file_id) const {
|
2022-08-03 22:23:32 +02:00
|
|
|
const auto *audio = get_audio(file_id);
|
|
|
|
if (audio == nullptr) {
|
2022-04-18 18:08:32 +02:00
|
|
|
return 0;
|
|
|
|
}
|
2022-08-03 22:23:32 +02:00
|
|
|
return audio->duration;
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
2021-08-28 13:16:29 +02:00
|
|
|
tl_object_ptr<td_api::audio> AudiosManager::get_audio_object(FileId file_id) const {
|
2018-12-31 20:04:05 +01:00
|
|
|
if (!file_id.is_valid()) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2022-08-03 22:23:32 +02:00
|
|
|
auto audio = get_audio(file_id);
|
2018-12-31 20:04:05 +01:00
|
|
|
CHECK(audio != nullptr);
|
2022-07-28 16:11:51 +02:00
|
|
|
|
2022-08-05 21:43:53 +02:00
|
|
|
vector<td_api::object_ptr<td_api::thumbnail>> album_covers;
|
2022-07-28 16:11:51 +02:00
|
|
|
if (!td_->auth_manager_->is_bot()) {
|
2022-08-05 21:43:53 +02:00
|
|
|
auto add_album_cover = [&](bool is_small, int32 width, int32 height) {
|
|
|
|
auto r_file_id =
|
|
|
|
td_->file_manager_->get_audio_thumbnail_file_id(audio->title, audio->performer, is_small, DialogId());
|
|
|
|
if (r_file_id.is_ok()) {
|
|
|
|
album_covers.emplace_back(
|
|
|
|
td_api::make_object<td_api::thumbnail>(td_api::make_object<td_api::thumbnailFormatJpeg>(), width, height,
|
|
|
|
td_->file_manager_->get_file_object(r_file_id.move_as_ok())));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
add_album_cover(true, 100, 100);
|
|
|
|
add_album_cover(false, 600, 600);
|
2022-07-28 16:11:51 +02:00
|
|
|
}
|
2020-05-31 21:22:15 +02:00
|
|
|
return make_tl_object<td_api::audio>(
|
|
|
|
audio->duration, audio->title, audio->performer, audio->file_name, audio->mime_type,
|
|
|
|
get_minithumbnail_object(audio->minithumbnail),
|
2022-08-05 21:43:53 +02:00
|
|
|
get_thumbnail_object(td_->file_manager_.get(), audio->thumbnail, PhotoFormat::Jpeg), std::move(album_covers),
|
2020-05-31 21:22:15 +02:00
|
|
|
td_->file_manager_->get_file_object(file_id));
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
2022-04-13 21:14:40 +02:00
|
|
|
td_api::object_ptr<td_api::notificationSound> AudiosManager::get_notification_sound_object(FileId file_id) const {
|
2022-04-14 12:23:43 +02:00
|
|
|
if (!file_id.is_valid()) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
2022-04-13 21:14:40 +02:00
|
|
|
|
2022-08-03 22:23:32 +02:00
|
|
|
auto audio = get_audio(file_id);
|
2022-04-13 21:14:40 +02:00
|
|
|
CHECK(audio != nullptr);
|
|
|
|
auto file_view = td_->file_manager_->get_file_view(file_id);
|
|
|
|
CHECK(!file_view.empty());
|
|
|
|
CHECK(file_view.get_type() == FileType::Ringtone);
|
|
|
|
CHECK(file_view.has_remote_location());
|
|
|
|
auto document_id = file_view.remote_location().get_id();
|
2022-04-17 18:01:01 +02:00
|
|
|
auto title = audio->title;
|
|
|
|
if (title.empty() && !audio->file_name.empty()) {
|
|
|
|
title = PathView(audio->file_name).file_name_without_extension().str();
|
|
|
|
}
|
|
|
|
return td_api::make_object<td_api::notificationSound>(document_id, audio->duration, audio->date, title,
|
2022-04-15 14:30:51 +02:00
|
|
|
audio->performer, td_->file_manager_->get_file_object(file_id));
|
2022-04-13 21:14:40 +02:00
|
|
|
}
|
|
|
|
|
2018-09-27 03:19:03 +02:00
|
|
|
FileId AudiosManager::on_get_audio(unique_ptr<Audio> new_audio, bool replace) {
|
2018-12-31 20:04:05 +01:00
|
|
|
auto file_id = new_audio->file_id;
|
2019-08-01 02:58:49 +02:00
|
|
|
CHECK(file_id.is_valid());
|
2018-01-05 16:50:35 +01:00
|
|
|
LOG(INFO) << "Receive audio " << file_id;
|
2018-12-31 20:04:05 +01:00
|
|
|
auto &a = audios_[file_id];
|
|
|
|
if (a == nullptr) {
|
|
|
|
a = std::move(new_audio);
|
|
|
|
} else if (replace) {
|
|
|
|
CHECK(a->file_id == new_audio->file_id);
|
|
|
|
if (a->mime_type != new_audio->mime_type) {
|
|
|
|
LOG(DEBUG) << "Audio " << file_id << " info has changed";
|
2022-10-20 00:16:24 +02:00
|
|
|
a->mime_type = std::move(new_audio->mime_type);
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
if (a->duration != new_audio->duration || a->title != new_audio->title || a->performer != new_audio->performer) {
|
|
|
|
LOG(DEBUG) << "Audio " << file_id << " info has changed";
|
|
|
|
a->duration = new_audio->duration;
|
2022-10-20 00:16:24 +02:00
|
|
|
a->title = std::move(new_audio->title);
|
|
|
|
a->performer = std::move(new_audio->performer);
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
if (a->file_name != new_audio->file_name) {
|
|
|
|
LOG(DEBUG) << "Audio " << file_id << " file name has changed";
|
|
|
|
a->file_name = std::move(new_audio->file_name);
|
|
|
|
}
|
2022-04-15 14:30:51 +02:00
|
|
|
if (a->date != new_audio->date) {
|
|
|
|
a->date = new_audio->date;
|
|
|
|
}
|
2019-03-01 20:51:33 +01:00
|
|
|
if (a->minithumbnail != new_audio->minithumbnail) {
|
|
|
|
a->minithumbnail = std::move(new_audio->minithumbnail);
|
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
if (a->thumbnail != new_audio->thumbnail) {
|
|
|
|
if (!a->thumbnail.file_id.is_valid()) {
|
|
|
|
LOG(DEBUG) << "Audio " << file_id << " thumbnail has changed";
|
|
|
|
} else {
|
|
|
|
LOG(INFO) << "Audio " << file_id << " thumbnail has changed from " << a->thumbnail << " to "
|
|
|
|
<< new_audio->thumbnail;
|
|
|
|
}
|
2022-10-20 00:16:24 +02:00
|
|
|
a->thumbnail = std::move(new_audio->thumbnail);
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return file_id;
|
|
|
|
}
|
|
|
|
|
|
|
|
const AudiosManager::Audio *AudiosManager::get_audio(FileId file_id) const {
|
2022-08-04 09:50:34 +02:00
|
|
|
return audios_.get_pointer(file_id);
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
FileId AudiosManager::dup_audio(FileId new_id, FileId old_id) {
|
|
|
|
const Audio *old_audio = get_audio(old_id);
|
|
|
|
CHECK(old_audio != nullptr);
|
|
|
|
auto &new_audio = audios_[new_id];
|
2022-02-09 22:59:52 +01:00
|
|
|
CHECK(new_audio == nullptr);
|
2018-09-27 03:19:03 +02:00
|
|
|
new_audio = make_unique<Audio>(*old_audio);
|
2018-12-31 20:04:05 +01:00
|
|
|
new_audio->file_id = new_id;
|
2022-11-21 09:40:43 +01:00
|
|
|
new_audio->thumbnail.file_id = td_->file_manager_->dup_file_id(new_audio->thumbnail.file_id, "dup_audio");
|
2018-12-31 20:04:05 +01:00
|
|
|
return new_id;
|
|
|
|
}
|
|
|
|
|
2022-08-03 20:38:03 +02:00
|
|
|
void AudiosManager::merge_audios(FileId new_id, FileId old_id) {
|
2021-08-26 17:50:28 +02:00
|
|
|
CHECK(old_id.is_valid() && new_id.is_valid());
|
|
|
|
CHECK(new_id != old_id);
|
2018-01-05 16:50:35 +01:00
|
|
|
|
|
|
|
LOG(INFO) << "Merge audios " << new_id << " and " << old_id;
|
2018-12-31 20:04:05 +01:00
|
|
|
const Audio *old_ = get_audio(old_id);
|
|
|
|
CHECK(old_ != nullptr);
|
|
|
|
|
2022-08-03 20:58:07 +02:00
|
|
|
const auto *new_ = get_audio(new_id);
|
|
|
|
if (new_ == nullptr) {
|
2022-08-03 20:38:03 +02:00
|
|
|
dup_audio(new_id, old_id);
|
2018-12-31 20:04:05 +01:00
|
|
|
} else {
|
|
|
|
if (!old_->mime_type.empty() && old_->mime_type != new_->mime_type) {
|
|
|
|
LOG(INFO) << "Audio has changed: mime_type = (" << old_->mime_type << ", " << new_->mime_type << ")";
|
|
|
|
}
|
|
|
|
|
|
|
|
if (old_->thumbnail != new_->thumbnail) {
|
|
|
|
// LOG_STATUS(td_->file_manager_->merge(new_->thumbnail.file_id, old_->thumbnail.file_id));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
LOG_STATUS(td_->file_manager_->merge(new_id, old_id));
|
|
|
|
}
|
|
|
|
|
|
|
|
string AudiosManager::get_audio_search_text(FileId file_id) const {
|
|
|
|
auto audio = get_audio(file_id);
|
|
|
|
CHECK(audio != nullptr);
|
|
|
|
return PSTRING() << audio->file_name << " " << audio->title << " " << audio->performer;
|
|
|
|
}
|
|
|
|
|
|
|
|
FileId AudiosManager::get_audio_thumbnail_file_id(FileId file_id) const {
|
|
|
|
auto audio = get_audio(file_id);
|
|
|
|
CHECK(audio != nullptr);
|
|
|
|
return audio->thumbnail.file_id;
|
|
|
|
}
|
|
|
|
|
2022-08-06 10:03:48 +02:00
|
|
|
void AudiosManager::append_audio_album_cover_file_ids(FileId file_id, vector<FileId> &file_ids) const {
|
|
|
|
if (td_->auth_manager_->is_bot()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
auto audio = get_audio(file_id);
|
|
|
|
CHECK(audio != nullptr);
|
|
|
|
|
|
|
|
auto append_album_cover = [&](bool is_small) {
|
|
|
|
auto r_file_id =
|
|
|
|
td_->file_manager_->get_audio_thumbnail_file_id(audio->title, audio->performer, is_small, DialogId());
|
|
|
|
if (r_file_id.is_ok()) {
|
|
|
|
file_ids.push_back(r_file_id.ok());
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
append_album_cover(true);
|
|
|
|
append_album_cover(false);
|
|
|
|
}
|
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
void AudiosManager::delete_audio_thumbnail(FileId file_id) {
|
|
|
|
auto &audio = audios_[file_id];
|
|
|
|
CHECK(audio != nullptr);
|
|
|
|
audio->thumbnail = PhotoSize();
|
|
|
|
}
|
|
|
|
|
2019-03-01 20:51:33 +01:00
|
|
|
void AudiosManager::create_audio(FileId file_id, string minithumbnail, PhotoSize thumbnail, string file_name,
|
2022-04-15 14:30:51 +02:00
|
|
|
string mime_type, int32 duration, string title, string performer, int32 date,
|
|
|
|
bool replace) {
|
2018-09-27 03:19:03 +02:00
|
|
|
auto a = make_unique<Audio>();
|
2018-12-31 20:04:05 +01:00
|
|
|
a->file_id = file_id;
|
|
|
|
a->file_name = std::move(file_name);
|
|
|
|
a->mime_type = std::move(mime_type);
|
2018-02-12 11:37:54 +01:00
|
|
|
a->duration = max(duration, 0);
|
2018-12-31 20:04:05 +01:00
|
|
|
a->title = std::move(title);
|
|
|
|
a->performer = std::move(performer);
|
2022-04-15 14:30:51 +02:00
|
|
|
a->date = date;
|
2021-05-27 20:15:30 +02:00
|
|
|
if (!td_->auth_manager_->is_bot()) {
|
|
|
|
a->minithumbnail = std::move(minithumbnail);
|
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
a->thumbnail = std::move(thumbnail);
|
|
|
|
on_get_audio(std::move(a), replace);
|
|
|
|
}
|
|
|
|
|
|
|
|
SecretInputMedia AudiosManager::get_secret_input_media(FileId audio_file_id,
|
|
|
|
tl_object_ptr<telegram_api::InputEncryptedFile> input_file,
|
2022-05-11 06:46:06 +02:00
|
|
|
const string &caption, BufferSlice thumbnail,
|
|
|
|
int32 layer) const {
|
2018-12-31 20:04:05 +01:00
|
|
|
auto *audio = get_audio(audio_file_id);
|
|
|
|
CHECK(audio != nullptr);
|
|
|
|
auto file_view = td_->file_manager_->get_file_view(audio_file_id);
|
2022-05-11 00:53:18 +02:00
|
|
|
if (!file_view.is_encrypted_secret() || file_view.encryption_key().empty()) {
|
2018-12-31 20:04:05 +01:00
|
|
|
return SecretInputMedia{};
|
|
|
|
}
|
|
|
|
if (file_view.has_remote_location()) {
|
2019-11-17 20:41:28 +01:00
|
|
|
input_file = file_view.main_remote_location().as_input_encrypted_file();
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
if (!input_file) {
|
|
|
|
return SecretInputMedia{};
|
|
|
|
}
|
|
|
|
if (audio->thumbnail.file_id.is_valid() && thumbnail.empty()) {
|
|
|
|
return SecretInputMedia{};
|
|
|
|
}
|
|
|
|
vector<tl_object_ptr<secret_api::DocumentAttribute>> attributes;
|
|
|
|
if (!audio->file_name.empty()) {
|
|
|
|
attributes.push_back(make_tl_object<secret_api::documentAttributeFilename>(audio->file_name));
|
|
|
|
}
|
|
|
|
attributes.push_back(make_tl_object<secret_api::documentAttributeAudio>(
|
2018-12-04 19:18:07 +01:00
|
|
|
secret_api::documentAttributeAudio::TITLE_MASK | secret_api::documentAttributeAudio::PERFORMER_MASK,
|
2018-12-31 20:04:05 +01:00
|
|
|
false /*ignored*/, audio->duration, audio->title, audio->performer, BufferSlice()));
|
|
|
|
|
2022-05-11 00:53:18 +02:00
|
|
|
return {std::move(input_file),
|
|
|
|
std::move(thumbnail),
|
|
|
|
audio->thumbnail.dimensions,
|
|
|
|
audio->mime_type,
|
|
|
|
file_view,
|
|
|
|
std::move(attributes),
|
2022-05-11 06:46:06 +02:00
|
|
|
caption,
|
|
|
|
layer};
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
tl_object_ptr<telegram_api::InputMedia> AudiosManager::get_input_media(
|
|
|
|
FileId file_id, tl_object_ptr<telegram_api::InputFile> input_file,
|
2018-01-30 18:06:54 +01:00
|
|
|
tl_object_ptr<telegram_api::InputFile> input_thumbnail) const {
|
2018-12-31 20:04:05 +01:00
|
|
|
auto file_view = td_->file_manager_->get_file_view(file_id);
|
|
|
|
if (file_view.is_encrypted()) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
2019-11-17 20:41:28 +01:00
|
|
|
if (file_view.has_remote_location() && !file_view.main_remote_location().is_web() && input_file == nullptr) {
|
2022-12-08 18:58:37 +01:00
|
|
|
return make_tl_object<telegram_api::inputMediaDocument>(
|
|
|
|
0, false /*ignored*/, file_view.main_remote_location().as_input_document(), 0, string());
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
if (file_view.has_url()) {
|
2022-12-08 18:58:37 +01:00
|
|
|
return make_tl_object<telegram_api::inputMediaDocumentExternal>(0, false /*ignored*/, file_view.url(), 0);
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (input_file != nullptr) {
|
|
|
|
const Audio *audio = get_audio(file_id);
|
|
|
|
CHECK(audio != nullptr);
|
|
|
|
|
|
|
|
vector<tl_object_ptr<telegram_api::DocumentAttribute>> attributes;
|
|
|
|
attributes.push_back(make_tl_object<telegram_api::documentAttributeAudio>(
|
|
|
|
telegram_api::documentAttributeAudio::TITLE_MASK | telegram_api::documentAttributeAudio::PERFORMER_MASK,
|
|
|
|
false /*ignored*/, audio->duration, audio->title, audio->performer, BufferSlice()));
|
|
|
|
if (!audio->file_name.empty()) {
|
|
|
|
attributes.push_back(make_tl_object<telegram_api::documentAttributeFilename>(audio->file_name));
|
|
|
|
}
|
|
|
|
string mime_type = audio->mime_type;
|
|
|
|
if (!begins_with(mime_type, "audio/")) {
|
|
|
|
mime_type = "audio/mpeg";
|
|
|
|
}
|
|
|
|
int32 flags = 0;
|
|
|
|
if (input_thumbnail != nullptr) {
|
|
|
|
flags |= telegram_api::inputMediaUploadedDocument::THUMB_MASK;
|
|
|
|
}
|
|
|
|
return make_tl_object<telegram_api::inputMediaUploadedDocument>(
|
2022-12-08 18:58:37 +01:00
|
|
|
flags, false /*ignored*/, false /*ignored*/, false /*ignored*/, std::move(input_file),
|
|
|
|
std::move(input_thumbnail), mime_type, std::move(attributes),
|
|
|
|
vector<tl_object_ptr<telegram_api::InputDocument>>(), 0);
|
2019-02-12 02:50:30 +01:00
|
|
|
} else {
|
|
|
|
CHECK(!file_view.has_remote_location());
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace td
|