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/VideoNotesManager.h"
|
|
|
|
|
|
|
|
#include "td/telegram/secret_api.h"
|
|
|
|
#include "td/telegram/td_api.h"
|
|
|
|
#include "td/telegram/telegram_api.h"
|
|
|
|
|
|
|
|
#include "td/telegram/files/FileManager.h"
|
|
|
|
#include "td/telegram/SecretChatActor.h"
|
2021-04-03 13:10:25 +02:00
|
|
|
#include "td/telegram/ConfigShared.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
#include "td/telegram/Td.h"
|
|
|
|
|
|
|
|
#include "td/utils/logging.h"
|
|
|
|
#include "td/utils/misc.h"
|
|
|
|
#include "td/utils/Status.h"
|
|
|
|
|
|
|
|
namespace td {
|
|
|
|
|
|
|
|
VideoNotesManager::VideoNotesManager(Td *td) : td_(td) {
|
|
|
|
}
|
|
|
|
|
2018-09-27 20:14:32 +02:00
|
|
|
int32 VideoNotesManager::get_video_note_duration(FileId file_id) const {
|
|
|
|
auto it = video_notes_.find(file_id);
|
2020-05-23 21:27:24 +02:00
|
|
|
if (it == video_notes_.end() || it->second == nullptr) {
|
|
|
|
return 0;
|
|
|
|
}
|
2018-09-27 20:14:32 +02:00
|
|
|
return it->second->duration;
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
tl_object_ptr<td_api::videoNote> VideoNotesManager::get_video_note_object(FileId file_id) {
|
|
|
|
if (!file_id.is_valid()) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto &video_note = video_notes_[file_id];
|
2020-05-23 21:27:24 +02:00
|
|
|
if (video_note == nullptr) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
video_note->is_changed = false;
|
|
|
|
|
2020-05-31 21:22:15 +02:00
|
|
|
return make_tl_object<td_api::videoNote>(
|
|
|
|
video_note->duration, video_note->dimensions.width, get_minithumbnail_object(video_note->minithumbnail),
|
|
|
|
get_thumbnail_object(td_->file_manager_.get(), video_note->thumbnail, PhotoFormat::Jpeg),
|
|
|
|
td_->file_manager_->get_file_object(file_id));
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
2018-09-27 03:19:03 +02:00
|
|
|
FileId VideoNotesManager::on_get_video_note(unique_ptr<VideoNote> new_video_note, bool replace) {
|
2018-12-31 20:04:05 +01:00
|
|
|
auto file_id = new_video_note->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 video note " << file_id;
|
2018-12-31 20:04:05 +01:00
|
|
|
auto &v = video_notes_[file_id];
|
|
|
|
if (v == nullptr) {
|
|
|
|
v = std::move(new_video_note);
|
|
|
|
} else if (replace) {
|
|
|
|
CHECK(v->file_id == new_video_note->file_id);
|
|
|
|
if (v->duration != new_video_note->duration || v->dimensions != new_video_note->dimensions) {
|
|
|
|
LOG(DEBUG) << "Video note " << file_id << " info has changed";
|
|
|
|
v->duration = new_video_note->duration;
|
|
|
|
v->dimensions = new_video_note->dimensions;
|
|
|
|
v->is_changed = true;
|
|
|
|
}
|
2019-03-01 20:51:33 +01:00
|
|
|
if (v->minithumbnail != new_video_note->minithumbnail) {
|
|
|
|
v->minithumbnail = std::move(new_video_note->minithumbnail);
|
|
|
|
v->is_changed = true;
|
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
if (v->thumbnail != new_video_note->thumbnail) {
|
|
|
|
if (!v->thumbnail.file_id.is_valid()) {
|
|
|
|
LOG(DEBUG) << "Video note " << file_id << " thumbnail has changed";
|
|
|
|
} else {
|
|
|
|
LOG(INFO) << "Video note " << file_id << " thumbnail has changed from " << v->thumbnail << " to "
|
|
|
|
<< new_video_note->thumbnail;
|
|
|
|
}
|
|
|
|
v->thumbnail = new_video_note->thumbnail;
|
|
|
|
v->is_changed = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return file_id;
|
|
|
|
}
|
|
|
|
|
|
|
|
const VideoNotesManager::VideoNote *VideoNotesManager::get_video_note(FileId file_id) const {
|
|
|
|
auto video_note = video_notes_.find(file_id);
|
2020-05-23 21:27:24 +02:00
|
|
|
|
|
|
|
if (video_note == video_notes_.end() ||
|
|
|
|
video_note->second == nullptr ||
|
|
|
|
video_note->second->file_id != file_id) {
|
|
|
|
return make_unique<VideoNote>().get();
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return video_note->second.get();
|
|
|
|
}
|
|
|
|
|
|
|
|
FileId VideoNotesManager::get_video_note_thumbnail_file_id(FileId file_id) const {
|
|
|
|
auto video_note = get_video_note(file_id);
|
2020-05-23 21:27:24 +02:00
|
|
|
if (video_note == nullptr) {
|
|
|
|
return FileId();
|
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
return video_note->thumbnail.file_id;
|
|
|
|
}
|
|
|
|
|
|
|
|
void VideoNotesManager::delete_video_note_thumbnail(FileId file_id) {
|
|
|
|
auto &video_note = video_notes_[file_id];
|
2020-05-23 21:27:24 +02:00
|
|
|
if (video_note == nullptr) {
|
|
|
|
return;
|
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
video_note->thumbnail = PhotoSize();
|
|
|
|
}
|
|
|
|
|
|
|
|
FileId VideoNotesManager::dup_video_note(FileId new_id, FileId old_id) {
|
|
|
|
const VideoNote *old_video_note = get_video_note(old_id);
|
2020-05-23 21:27:24 +02:00
|
|
|
if (old_video_note == nullptr) {
|
|
|
|
return FileId();
|
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
auto &new_video_note = video_notes_[new_id];
|
|
|
|
CHECK(!new_video_note);
|
2018-09-27 03:19:03 +02:00
|
|
|
new_video_note = make_unique<VideoNote>(*old_video_note);
|
2018-12-31 20:04:05 +01:00
|
|
|
new_video_note->file_id = new_id;
|
|
|
|
new_video_note->thumbnail.file_id = td_->file_manager_->dup_file_id(new_video_note->thumbnail.file_id);
|
|
|
|
return new_id;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool VideoNotesManager::merge_video_notes(FileId new_id, FileId old_id, bool can_delete_old) {
|
|
|
|
if (!old_id.is_valid()) {
|
2021-03-15 01:32:14 +01:00
|
|
|
LOG(ERROR) << "Old file identifier is invalid";
|
2018-12-31 20:04:05 +01:00
|
|
|
return true;
|
|
|
|
}
|
2018-01-05 16:50:35 +01:00
|
|
|
|
|
|
|
LOG(INFO) << "Merge video notes " << new_id << " and " << old_id;
|
2018-12-31 20:04:05 +01:00
|
|
|
const VideoNote *old_ = get_video_note(old_id);
|
|
|
|
CHECK(old_ != nullptr);
|
|
|
|
if (old_id == new_id) {
|
|
|
|
return old_->is_changed;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto new_it = video_notes_.find(new_id);
|
2020-05-23 21:27:24 +02:00
|
|
|
if (new_it == video_notes_.end() || new_it->second == nullptr) {
|
2018-12-31 20:04:05 +01:00
|
|
|
auto &old = video_notes_[old_id];
|
|
|
|
old->is_changed = true;
|
|
|
|
if (!can_delete_old) {
|
|
|
|
dup_video_note(new_id, old_id);
|
|
|
|
} else {
|
|
|
|
old->file_id = new_id;
|
|
|
|
video_notes_.emplace(new_id, std::move(old));
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
VideoNote *new_ = new_it->second.get();
|
|
|
|
CHECK(new_ != nullptr);
|
|
|
|
new_->is_changed = true;
|
|
|
|
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));
|
|
|
|
if (can_delete_old) {
|
|
|
|
video_notes_.erase(old_id);
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-03-01 20:51:33 +01:00
|
|
|
void VideoNotesManager::create_video_note(FileId file_id, string minithumbnail, PhotoSize thumbnail, int32 duration,
|
|
|
|
Dimensions dimensions, bool replace) {
|
2018-12-31 20:04:05 +01:00
|
|
|
auto v = make_unique<VideoNote>();
|
|
|
|
v->file_id = file_id;
|
2018-02-12 11:37:54 +01:00
|
|
|
v->duration = max(duration, 0);
|
2018-12-31 20:04:05 +01:00
|
|
|
if (dimensions.width == dimensions.height && dimensions.width <= 640) {
|
|
|
|
v->dimensions = dimensions;
|
|
|
|
} else {
|
|
|
|
LOG(INFO) << "Receive wrong video note dimensions " << dimensions;
|
|
|
|
}
|
2021-04-03 13:10:25 +02:00
|
|
|
if (G()->shared_config().get_option_boolean("disable_minithumbnails")) {
|
|
|
|
v->minithumbnail = "";
|
|
|
|
} else {
|
|
|
|
v->minithumbnail = std::move(minithumbnail);
|
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
v->thumbnail = std::move(thumbnail);
|
|
|
|
on_get_video_note(std::move(v), replace);
|
|
|
|
}
|
|
|
|
|
|
|
|
SecretInputMedia VideoNotesManager::get_secret_input_media(FileId video_note_file_id,
|
|
|
|
tl_object_ptr<telegram_api::InputEncryptedFile> input_file,
|
|
|
|
BufferSlice thumbnail, int32 layer) const {
|
|
|
|
const VideoNote *video_note = get_video_note(video_note_file_id);
|
|
|
|
CHECK(video_note != nullptr);
|
|
|
|
auto file_view = td_->file_manager_->get_file_view(video_note_file_id);
|
|
|
|
auto &encryption_key = file_view.encryption_key();
|
2018-06-08 21:23:49 +02:00
|
|
|
if (!file_view.is_encrypted_secret() || 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 (video_note->thumbnail.file_id.is_valid() && thumbnail.empty()) {
|
|
|
|
return SecretInputMedia{};
|
|
|
|
}
|
2018-12-04 19:18:07 +01:00
|
|
|
CHECK(layer >= SecretChatActor::VIDEO_NOTES_LAYER);
|
2018-12-31 20:04:05 +01:00
|
|
|
vector<tl_object_ptr<secret_api::DocumentAttribute>> attributes;
|
|
|
|
attributes.push_back(make_tl_object<secret_api::documentAttributeVideo66>(
|
2018-12-04 19:18:07 +01:00
|
|
|
secret_api::documentAttributeVideo66::ROUND_MESSAGE_MASK, true, video_note->duration,
|
2018-12-31 20:04:05 +01:00
|
|
|
video_note->dimensions.width, video_note->dimensions.height));
|
|
|
|
return SecretInputMedia{
|
|
|
|
std::move(input_file),
|
|
|
|
make_tl_object<secret_api::decryptedMessageMediaDocument>(
|
|
|
|
std::move(thumbnail), video_note->thumbnail.dimensions.width, video_note->thumbnail.dimensions.height,
|
|
|
|
"video/mp4", narrow_cast<int32>(file_view.size()), BufferSlice(encryption_key.key_slice()),
|
|
|
|
BufferSlice(encryption_key.iv_slice()), std::move(attributes), "")};
|
|
|
|
}
|
|
|
|
|
|
|
|
tl_object_ptr<telegram_api::InputMedia> VideoNotesManager::get_input_media(
|
|
|
|
FileId file_id, tl_object_ptr<telegram_api::InputFile> input_file,
|
|
|
|
tl_object_ptr<telegram_api::InputFile> input_thumbnail) const {
|
|
|
|
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) {
|
2020-12-18 15:43:23 +01:00
|
|
|
return make_tl_object<telegram_api::inputMediaDocument>(0, file_view.main_remote_location().as_input_document(), 0,
|
|
|
|
string());
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
if (file_view.has_url()) {
|
2018-01-30 18:06:54 +01:00
|
|
|
return make_tl_object<telegram_api::inputMediaDocumentExternal>(0, file_view.url(), 0);
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (input_file != nullptr) {
|
|
|
|
const VideoNote *video_note = get_video_note(file_id);
|
2020-05-23 21:27:24 +02:00
|
|
|
if (video_note == nullptr) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
vector<tl_object_ptr<telegram_api::DocumentAttribute>> attributes;
|
|
|
|
attributes.push_back(make_tl_object<telegram_api::documentAttributeVideo>(
|
2018-02-07 00:31:38 +01:00
|
|
|
telegram_api::documentAttributeVideo::ROUND_MESSAGE_MASK, false /*ignored*/, false /*ignored*/,
|
|
|
|
video_note->duration, video_note->dimensions.width ? video_note->dimensions.width : 240,
|
2018-12-31 20:04:05 +01:00
|
|
|
video_note->dimensions.height ? video_note->dimensions.height : 240));
|
|
|
|
int32 flags = 0;
|
|
|
|
if (input_thumbnail != nullptr) {
|
|
|
|
flags |= telegram_api::inputMediaUploadedDocument::THUMB_MASK;
|
|
|
|
}
|
|
|
|
return make_tl_object<telegram_api::inputMediaUploadedDocument>(
|
2020-06-22 01:02:21 +02:00
|
|
|
flags, false /*ignored*/, false /*ignored*/, std::move(input_file), std::move(input_thumbnail), "video/mp4",
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2020-05-23 21:27:24 +02:00
|
|
|
void VideoNotesManager::memory_cleanup() {
|
|
|
|
video_notes_.clear();
|
|
|
|
video_notes_.rehash(0);
|
|
|
|
}
|
2020-10-25 02:20:24 +01:00
|
|
|
void VideoNotesManager::memory_stats(vector<string> &output) {
|
2020-10-25 10:08:13 +01:00
|
|
|
output.push_back("\"video_notes_\":"); output.push_back(std::to_string(video_notes_.size()));
|
2020-10-25 02:20:24 +01:00
|
|
|
}
|
2020-05-23 21:27:24 +02:00
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
} // namespace td
|