2018-12-31 20:04:05 +01:00
|
|
|
//
|
2020-01-01 02:23:48 +01:00
|
|
|
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2020
|
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/VideosManager.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/Td.h"
|
|
|
|
|
|
|
|
#include "td/utils/logging.h"
|
|
|
|
#include "td/utils/misc.h"
|
|
|
|
#include "td/utils/Status.h"
|
|
|
|
|
|
|
|
namespace td {
|
|
|
|
|
|
|
|
VideosManager::VideosManager(Td *td) : td_(td) {
|
|
|
|
}
|
|
|
|
|
2018-09-27 20:14:32 +02:00
|
|
|
int32 VideosManager::get_video_duration(FileId file_id) const {
|
|
|
|
auto it = videos_.find(file_id);
|
2020-05-23 21:27:24 +02:00
|
|
|
if (it == videos_.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::video> VideosManager::get_video_object(FileId file_id) {
|
|
|
|
if (!file_id.is_valid()) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto &video = videos_[file_id];
|
2020-05-23 21:27:24 +02:00
|
|
|
if (video == nullptr) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
video->is_changed = false;
|
|
|
|
|
2020-05-31 21:22:15 +02:00
|
|
|
auto thumbnail = video->animated_thumbnail.file_id.is_valid()
|
|
|
|
? get_thumbnail_object(td_->file_manager_.get(), video->animated_thumbnail, PhotoFormat::Mpeg4)
|
|
|
|
: get_thumbnail_object(td_->file_manager_.get(), video->thumbnail, PhotoFormat::Jpeg);
|
|
|
|
return make_tl_object<td_api::video>(video->duration, video->dimensions.width, video->dimensions.height,
|
|
|
|
video->file_name, video->mime_type, video->has_stickers,
|
|
|
|
video->supports_streaming, get_minithumbnail_object(video->minithumbnail),
|
|
|
|
std::move(thumbnail), 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 VideosManager::on_get_video(unique_ptr<Video> new_video, bool replace) {
|
2018-12-31 20:04:05 +01:00
|
|
|
auto file_id = new_video->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 " << file_id;
|
2018-12-31 20:04:05 +01:00
|
|
|
auto &v = videos_[file_id];
|
|
|
|
if (v == nullptr) {
|
|
|
|
v = std::move(new_video);
|
|
|
|
} else if (replace) {
|
|
|
|
CHECK(v->file_id == new_video->file_id);
|
|
|
|
if (v->mime_type != new_video->mime_type) {
|
|
|
|
LOG(DEBUG) << "Video " << file_id << " MIME type has changed";
|
|
|
|
v->mime_type = new_video->mime_type;
|
|
|
|
v->is_changed = true;
|
|
|
|
}
|
2018-02-07 00:31:38 +01:00
|
|
|
if (v->duration != new_video->duration || v->dimensions != new_video->dimensions ||
|
|
|
|
v->supports_streaming != new_video->supports_streaming) {
|
2018-12-31 20:04:05 +01:00
|
|
|
LOG(DEBUG) << "Video " << file_id << " info has changed";
|
|
|
|
v->duration = new_video->duration;
|
|
|
|
v->dimensions = new_video->dimensions;
|
2018-02-07 00:31:38 +01:00
|
|
|
v->supports_streaming = new_video->supports_streaming;
|
2018-12-31 20:04:05 +01:00
|
|
|
v->is_changed = true;
|
|
|
|
}
|
|
|
|
if (v->file_name != new_video->file_name) {
|
|
|
|
LOG(DEBUG) << "Video " << file_id << " file name has changed";
|
|
|
|
v->file_name = std::move(new_video->file_name);
|
|
|
|
v->is_changed = true;
|
|
|
|
}
|
2019-03-01 20:51:33 +01:00
|
|
|
if (v->minithumbnail != new_video->minithumbnail) {
|
|
|
|
v->minithumbnail = std::move(new_video->minithumbnail);
|
|
|
|
v->is_changed = true;
|
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
if (v->thumbnail != new_video->thumbnail) {
|
|
|
|
if (!v->thumbnail.file_id.is_valid()) {
|
|
|
|
LOG(DEBUG) << "Video " << file_id << " thumbnail has changed";
|
|
|
|
} else {
|
|
|
|
LOG(INFO) << "Video " << file_id << " thumbnail has changed from " << v->thumbnail << " to "
|
|
|
|
<< new_video->thumbnail;
|
|
|
|
}
|
|
|
|
v->thumbnail = new_video->thumbnail;
|
|
|
|
v->is_changed = true;
|
|
|
|
}
|
2020-05-30 00:48:56 +02:00
|
|
|
if (v->animated_thumbnail != new_video->animated_thumbnail) {
|
|
|
|
if (!v->animated_thumbnail.file_id.is_valid()) {
|
|
|
|
LOG(DEBUG) << "Video " << file_id << " animated thumbnail has changed";
|
|
|
|
} else {
|
|
|
|
LOG(INFO) << "Video " << file_id << " animated thumbnail has changed from " << v->animated_thumbnail << " to "
|
|
|
|
<< new_video->animated_thumbnail;
|
|
|
|
}
|
|
|
|
v->animated_thumbnail = new_video->animated_thumbnail;
|
|
|
|
v->is_changed = true;
|
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
if (v->has_stickers != new_video->has_stickers && new_video->has_stickers) {
|
|
|
|
v->has_stickers = new_video->has_stickers;
|
|
|
|
v->is_changed = true;
|
|
|
|
}
|
|
|
|
if (v->sticker_file_ids != new_video->sticker_file_ids && !new_video->sticker_file_ids.empty()) {
|
2020-05-29 10:51:51 +02:00
|
|
|
v->sticker_file_ids = std::move(new_video->sticker_file_ids);
|
2018-12-31 20:04:05 +01:00
|
|
|
v->is_changed = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return file_id;
|
|
|
|
}
|
|
|
|
|
|
|
|
const VideosManager::Video *VideosManager::get_video(FileId file_id) const {
|
|
|
|
auto video = videos_.find(file_id);
|
2020-05-23 21:27:24 +02:00
|
|
|
|
|
|
|
if (video == videos_.end() ||
|
|
|
|
video->second == nullptr ||
|
|
|
|
video->second->file_id != file_id) {
|
|
|
|
return make_unique<Video>().get();
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return video->second.get();
|
|
|
|
}
|
|
|
|
|
|
|
|
FileId VideosManager::get_video_thumbnail_file_id(FileId file_id) const {
|
|
|
|
auto video = get_video(file_id);
|
2020-05-23 21:27:24 +02:00
|
|
|
if (video == nullptr) {
|
|
|
|
return FileId();
|
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
return video->thumbnail.file_id;
|
|
|
|
}
|
|
|
|
|
2020-05-30 00:48:56 +02:00
|
|
|
FileId VideosManager::get_video_animated_thumbnail_file_id(FileId file_id) const {
|
|
|
|
auto video = get_video(file_id);
|
2020-06-11 17:44:52 +02:00
|
|
|
if (video == nullptr) {
|
|
|
|
return FileId();
|
|
|
|
}
|
2020-05-30 00:48:56 +02:00
|
|
|
return video->animated_thumbnail.file_id;
|
|
|
|
}
|
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
void VideosManager::delete_video_thumbnail(FileId file_id) {
|
|
|
|
auto &video = videos_[file_id];
|
2020-05-23 21:27:24 +02:00
|
|
|
if (video == nullptr) {
|
|
|
|
return;
|
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
video->thumbnail = PhotoSize();
|
2020-07-07 10:41:01 +02:00
|
|
|
video->animated_thumbnail = AnimationSize();
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
FileId VideosManager::dup_video(FileId new_id, FileId old_id) {
|
|
|
|
const Video *old_video = get_video(old_id);
|
|
|
|
CHECK(old_video != nullptr);
|
|
|
|
auto &new_video = videos_[new_id];
|
|
|
|
CHECK(!new_video);
|
2018-09-27 03:19:03 +02:00
|
|
|
new_video = make_unique<Video>(*old_video);
|
2018-12-31 20:04:05 +01:00
|
|
|
new_video->file_id = new_id;
|
|
|
|
new_video->thumbnail.file_id = td_->file_manager_->dup_file_id(new_video->thumbnail.file_id);
|
2020-05-30 00:48:56 +02:00
|
|
|
new_video->animated_thumbnail.file_id = td_->file_manager_->dup_file_id(new_video->animated_thumbnail.file_id);
|
2018-12-31 20:04:05 +01:00
|
|
|
return new_id;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool VideosManager::merge_videos(FileId new_id, FileId old_id, bool can_delete_old) {
|
|
|
|
if (!old_id.is_valid()) {
|
|
|
|
LOG(ERROR) << "Old file id is invalid";
|
|
|
|
return true;
|
|
|
|
}
|
2018-01-05 16:50:35 +01:00
|
|
|
|
|
|
|
LOG(INFO) << "Merge videos " << new_id << " and " << old_id;
|
2018-12-31 20:04:05 +01:00
|
|
|
const Video *old_ = get_video(old_id);
|
|
|
|
CHECK(old_ != nullptr);
|
|
|
|
if (old_id == new_id) {
|
|
|
|
return old_->is_changed;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto new_it = videos_.find(new_id);
|
2020-05-23 21:27:24 +02:00
|
|
|
if (new_it == videos_.end() || new_it->second == nullptr) {
|
2018-12-31 20:04:05 +01:00
|
|
|
auto &old = videos_[old_id];
|
|
|
|
old->is_changed = true;
|
|
|
|
if (!can_delete_old) {
|
|
|
|
dup_video(new_id, old_id);
|
|
|
|
} else {
|
|
|
|
old->file_id = new_id;
|
|
|
|
videos_.emplace(new_id, std::move(old));
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
Video *new_ = new_it->second.get();
|
|
|
|
CHECK(new_ != nullptr);
|
|
|
|
|
|
|
|
if (!old_->mime_type.empty() && old_->mime_type != new_->mime_type) {
|
|
|
|
LOG(INFO) << "Video has changed: mime_type = (" << old_->mime_type << ", " << new_->mime_type << ")";
|
|
|
|
}
|
|
|
|
|
|
|
|
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) {
|
|
|
|
videos_.erase(old_id);
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-05-30 00:48:56 +02:00
|
|
|
void VideosManager::create_video(FileId file_id, string minithumbnail, PhotoSize thumbnail,
|
2020-07-07 10:41:01 +02:00
|
|
|
AnimationSize animated_thumbnail, bool has_stickers, vector<FileId> &&sticker_file_ids,
|
2020-05-30 00:48:56 +02:00
|
|
|
string file_name, string mime_type, int32 duration, Dimensions dimensions,
|
|
|
|
bool supports_streaming, bool replace) {
|
2018-12-31 20:04:05 +01:00
|
|
|
auto v = make_unique<Video>();
|
|
|
|
v->file_id = file_id;
|
|
|
|
v->file_name = std::move(file_name);
|
|
|
|
v->mime_type = std::move(mime_type);
|
2018-02-12 11:37:54 +01:00
|
|
|
v->duration = max(duration, 0);
|
2018-12-31 20:04:05 +01:00
|
|
|
v->dimensions = dimensions;
|
2019-03-01 20:51:33 +01:00
|
|
|
v->minithumbnail = std::move(minithumbnail);
|
2018-12-31 20:04:05 +01:00
|
|
|
v->thumbnail = std::move(thumbnail);
|
2020-05-30 00:48:56 +02:00
|
|
|
v->animated_thumbnail = std::move(animated_thumbnail);
|
2018-02-07 00:31:38 +01:00
|
|
|
v->supports_streaming = supports_streaming;
|
2018-12-31 20:04:05 +01:00
|
|
|
v->has_stickers = has_stickers;
|
|
|
|
v->sticker_file_ids = std::move(sticker_file_ids);
|
|
|
|
on_get_video(std::move(v), replace);
|
|
|
|
}
|
|
|
|
|
|
|
|
SecretInputMedia VideosManager::get_secret_input_media(FileId video_file_id,
|
|
|
|
tl_object_ptr<telegram_api::InputEncryptedFile> input_file,
|
|
|
|
const string &caption, BufferSlice thumbnail) const {
|
|
|
|
const Video *video = get_video(video_file_id);
|
|
|
|
CHECK(video != nullptr);
|
|
|
|
auto file_view = td_->file_manager_->get_file_view(video_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->thumbnail.file_id.is_valid() && thumbnail.empty()) {
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
return SecretInputMedia{
|
|
|
|
std::move(input_file),
|
|
|
|
make_tl_object<secret_api::decryptedMessageMediaVideo>(
|
|
|
|
std::move(thumbnail), video->thumbnail.dimensions.width, video->thumbnail.dimensions.height, video->duration,
|
|
|
|
video->mime_type, video->dimensions.width, video->dimensions.height, narrow_cast<int32>(file_view.size()),
|
|
|
|
BufferSlice(encryption_key.key_slice()), BufferSlice(encryption_key.iv_slice()), caption)};
|
|
|
|
}
|
|
|
|
|
|
|
|
tl_object_ptr<telegram_api::InputMedia> VideosManager::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, int32 ttl) const {
|
2018-12-31 20:04:05 +01:00
|
|
|
if (!file_id.is_valid()) {
|
|
|
|
LOG_IF(ERROR, ttl == 0) << "Video has invalid file_id";
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
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) {
|
2018-12-31 20:04:05 +01:00
|
|
|
int32 flags = 0;
|
|
|
|
if (ttl != 0) {
|
|
|
|
flags |= telegram_api::inputMediaDocument::TTL_SECONDS_MASK;
|
|
|
|
}
|
2019-11-17 20:41:28 +01:00
|
|
|
return make_tl_object<telegram_api::inputMediaDocument>(flags, file_view.main_remote_location().as_input_document(),
|
2020-12-18 15:43:23 +01:00
|
|
|
ttl, string());
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
if (file_view.has_url()) {
|
|
|
|
int32 flags = 0;
|
|
|
|
if (ttl != 0) {
|
|
|
|
flags |= telegram_api::inputMediaDocumentExternal::TTL_SECONDS_MASK;
|
|
|
|
}
|
2018-01-30 18:06:54 +01:00
|
|
|
return make_tl_object<telegram_api::inputMediaDocumentExternal>(flags, file_view.url(), ttl);
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (input_file != nullptr) {
|
|
|
|
const Video *video = get_video(file_id);
|
|
|
|
CHECK(video != nullptr);
|
|
|
|
|
2018-02-07 00:31:38 +01:00
|
|
|
int32 attribute_flags = 0;
|
|
|
|
if (video->supports_streaming) {
|
|
|
|
attribute_flags |= telegram_api::documentAttributeVideo::SUPPORTS_STREAMING_MASK;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
attribute_flags, false /*ignored*/, false /*ignored*/, video->duration, video->dimensions.width,
|
|
|
|
video->dimensions.height));
|
2018-12-31 20:04:05 +01:00
|
|
|
if (!video->file_name.empty()) {
|
|
|
|
attributes.push_back(make_tl_object<telegram_api::documentAttributeFilename>(video->file_name));
|
|
|
|
}
|
2020-01-22 22:21:23 +01:00
|
|
|
int32 flags = telegram_api::inputMediaUploadedDocument::NOSOUND_VIDEO_MASK;
|
2018-12-31 20:04:05 +01:00
|
|
|
vector<tl_object_ptr<telegram_api::InputDocument>> added_stickers;
|
|
|
|
if (video->has_stickers) {
|
|
|
|
flags |= telegram_api::inputMediaUploadedDocument::STICKERS_MASK;
|
|
|
|
added_stickers = td_->file_manager_->get_input_documents(video->sticker_file_ids);
|
|
|
|
}
|
|
|
|
string mime_type = video->mime_type;
|
|
|
|
if (!begins_with(mime_type, "video/") || ttl > 0) {
|
|
|
|
mime_type = "video/mp4";
|
|
|
|
}
|
|
|
|
if (ttl != 0) {
|
|
|
|
flags |= telegram_api::inputMediaUploadedDocument::TTL_SECONDS_MASK;
|
|
|
|
}
|
|
|
|
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), mime_type,
|
|
|
|
std::move(attributes), std::move(added_stickers), ttl);
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
string VideosManager::get_video_search_text(FileId file_id) const {
|
|
|
|
auto *video = get_video(file_id);
|
|
|
|
CHECK(video != nullptr);
|
|
|
|
return video->file_name;
|
|
|
|
}
|
2020-05-23 21:27:24 +02:00
|
|
|
void VideosManager::memory_cleanup() {
|
|
|
|
videos_.clear();
|
|
|
|
videos_.rehash(0);
|
|
|
|
}
|
2020-10-25 02:20:24 +01:00
|
|
|
void VideosManager::memory_stats(vector<string> &output) {
|
2020-10-25 10:08:13 +01:00
|
|
|
output.push_back("\"videos_\":"); output.push_back(std::to_string(videos_.size()));
|
2020-10-25 02:20:24 +01:00
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
} // namespace td
|