2019-07-30 14:57:39 +00:00
|
|
|
/*
|
|
|
|
This file is part of Telegram Desktop,
|
|
|
|
the official desktop application for the Telegram messaging service.
|
|
|
|
|
|
|
|
For license and copyright information please follow this link:
|
|
|
|
https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
|
|
|
|
*/
|
|
|
|
#include "media/streaming/media_streaming_document.h"
|
|
|
|
|
2019-12-11 12:09:21 +00:00
|
|
|
#include "media/streaming/media_streaming_instance.h"
|
2020-05-20 11:04:56 +00:00
|
|
|
#include "media/streaming/media_streaming_loader.h"
|
|
|
|
#include "media/streaming/media_streaming_reader.h"
|
2019-07-30 14:57:39 +00:00
|
|
|
#include "data/data_session.h"
|
|
|
|
#include "data/data_document.h"
|
2020-07-03 10:48:55 +00:00
|
|
|
#include "data/data_photo.h"
|
2020-03-27 11:40:50 +00:00
|
|
|
#include "data/data_document_media.h"
|
2019-07-30 14:57:39 +00:00
|
|
|
#include "data/data_file_origin.h"
|
2020-03-27 11:40:50 +00:00
|
|
|
#include "main/main_session.h"
|
2019-07-30 14:57:39 +00:00
|
|
|
#include "storage/file_download.h" // Storage::kMaxFileInMemory.
|
|
|
|
#include "styles/style_widgets.h"
|
|
|
|
|
|
|
|
#include <QtCore/QBuffer>
|
|
|
|
|
|
|
|
namespace Media {
|
|
|
|
namespace Streaming {
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
constexpr auto kWaitingFastDuration = crl::time(200);
|
|
|
|
constexpr auto kWaitingShowDuration = crl::time(500);
|
|
|
|
constexpr auto kWaitingShowDelay = crl::time(500);
|
2022-01-21 14:30:12 +00:00
|
|
|
constexpr auto kGoodThumbQuality = 87;
|
2019-07-30 14:57:39 +00:00
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
Document::Document(
|
|
|
|
not_null<DocumentData*> document,
|
2019-12-11 10:15:48 +00:00
|
|
|
std::shared_ptr<Reader> reader)
|
2020-07-03 10:48:55 +00:00
|
|
|
: Document(std::move(reader), document, nullptr) {
|
2020-05-20 11:04:56 +00:00
|
|
|
_player.fullInCache(
|
|
|
|
) | rpl::start_with_next([=](bool fullInCache) {
|
|
|
|
_document->setLoadedInMediaCache(fullInCache);
|
|
|
|
}, _player.lifetime());
|
|
|
|
}
|
|
|
|
|
2020-07-03 10:48:55 +00:00
|
|
|
Document::Document(
|
|
|
|
not_null<PhotoData*> photo,
|
|
|
|
std::shared_ptr<Reader> reader)
|
|
|
|
: Document(std::move(reader), nullptr, photo) {
|
|
|
|
}
|
|
|
|
|
2020-05-20 11:04:56 +00:00
|
|
|
Document::Document(std::unique_ptr<Loader> loader)
|
2020-07-03 10:48:55 +00:00
|
|
|
: Document(std::make_shared<Reader>(std::move(loader)), nullptr, nullptr) {
|
2020-05-20 11:04:56 +00:00
|
|
|
}
|
|
|
|
|
2020-07-03 10:48:55 +00:00
|
|
|
Document::Document(
|
|
|
|
std::shared_ptr<Reader> reader,
|
|
|
|
DocumentData *document,
|
|
|
|
PhotoData *photo)
|
2020-05-20 11:04:56 +00:00
|
|
|
: _document(document)
|
2020-07-03 10:48:55 +00:00
|
|
|
, _photo(photo)
|
2020-05-20 11:04:56 +00:00
|
|
|
, _player(std::move(reader))
|
2019-07-30 14:57:39 +00:00
|
|
|
, _radial(
|
2020-05-20 11:04:56 +00:00
|
|
|
[=] { waitingCallback(); },
|
|
|
|
st::defaultInfiniteRadialAnimation) {
|
2019-07-30 14:57:39 +00:00
|
|
|
_player.updates(
|
|
|
|
) | rpl::start_with_next_error([=](Update &&update) {
|
|
|
|
handleUpdate(std::move(update));
|
|
|
|
}, [=](Streaming::Error &&error) {
|
|
|
|
handleError(std::move(error));
|
2019-12-10 14:06:22 +00:00
|
|
|
}, _player.lifetime());
|
2019-07-30 14:57:39 +00:00
|
|
|
}
|
|
|
|
|
2019-12-11 12:09:21 +00:00
|
|
|
Player &Document::player() {
|
|
|
|
return _player;
|
|
|
|
}
|
|
|
|
|
2019-07-30 14:57:39 +00:00
|
|
|
const Player &Document::player() const {
|
|
|
|
return _player;
|
|
|
|
}
|
|
|
|
|
|
|
|
const Information &Document::info() const {
|
|
|
|
return _info;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Document::play(const PlaybackOptions &options) {
|
|
|
|
_player.play(options);
|
|
|
|
_info.audio.state.position
|
|
|
|
= _info.video.state.position
|
|
|
|
= options.position;
|
|
|
|
waitingChange(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Document::saveFrameToCover() {
|
|
|
|
_info.video.cover = _player.ready()
|
2021-06-04 09:50:41 +00:00
|
|
|
? _player.currentFrameImage()
|
2019-07-30 14:57:39 +00:00
|
|
|
: _info.video.cover;
|
|
|
|
}
|
|
|
|
|
2019-12-11 12:09:21 +00:00
|
|
|
void Document::registerInstance(not_null<Instance*> instance) {
|
|
|
|
_instances.emplace(instance);
|
2019-12-11 10:15:48 +00:00
|
|
|
}
|
|
|
|
|
2019-12-11 12:09:21 +00:00
|
|
|
void Document::unregisterInstance(not_null<Instance*> instance) {
|
|
|
|
_instances.remove(instance);
|
2019-12-12 06:56:08 +00:00
|
|
|
_player.unregisterInstance(instance);
|
2019-12-23 10:27:20 +00:00
|
|
|
refreshPlayerPriority();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Document::refreshPlayerPriority() {
|
|
|
|
if (_instances.empty()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const auto max = ranges::max_element(
|
|
|
|
_instances,
|
|
|
|
ranges::less(),
|
|
|
|
&Instance::priority);
|
|
|
|
_player.setLoaderPriority((*max)->priority());
|
2019-07-30 14:57:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool Document::waitingShown() const {
|
|
|
|
if (!_fading.animating() && !_waiting) {
|
|
|
|
_radial.stop(anim::type::instant);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return _radial.animating();
|
|
|
|
}
|
|
|
|
|
|
|
|
float64 Document::waitingOpacity() const {
|
|
|
|
return _fading.value(_waiting ? 1. : 0.);
|
|
|
|
}
|
|
|
|
|
|
|
|
Ui::RadialState Document::waitingState() const {
|
|
|
|
return _radial.computeState();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Document::handleUpdate(Update &&update) {
|
2020-08-31 08:14:53 +00:00
|
|
|
v::match(update.data, [&](Information &update) {
|
2019-07-30 14:57:39 +00:00
|
|
|
ready(std::move(update));
|
|
|
|
}, [&](const PreloadedVideo &update) {
|
|
|
|
_info.video.state.receivedTill = update.till;
|
|
|
|
}, [&](const UpdateVideo &update) {
|
|
|
|
_info.video.state.position = update.position;
|
|
|
|
}, [&](const PreloadedAudio &update) {
|
|
|
|
_info.audio.state.receivedTill = update.till;
|
|
|
|
}, [&](const UpdateAudio &update) {
|
|
|
|
_info.audio.state.position = update.position;
|
|
|
|
}, [&](const WaitingForData &update) {
|
|
|
|
waitingChange(update.waiting);
|
|
|
|
}, [&](MutedByOther) {
|
|
|
|
}, [&](Finished) {
|
|
|
|
const auto finishTrack = [](TrackState &state) {
|
|
|
|
state.position = state.receivedTill = state.duration;
|
|
|
|
};
|
|
|
|
finishTrack(_info.audio.state);
|
|
|
|
finishTrack(_info.video.state);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
void Document::handleError(Error &&error) {
|
2020-05-20 11:04:56 +00:00
|
|
|
if (_document) {
|
|
|
|
if (error == Error::NotStreamable) {
|
|
|
|
_document->setNotSupportsStreaming();
|
|
|
|
} else if (error == Error::OpenFailed) {
|
|
|
|
_document->setInappPlaybackFailed();
|
|
|
|
}
|
2020-07-03 10:48:55 +00:00
|
|
|
} else if (_photo) {
|
|
|
|
if (error == Error::NotStreamable || error == Error::OpenFailed) {
|
|
|
|
_photo->setVideoPlaybackFailed();
|
|
|
|
}
|
2019-07-30 14:57:39 +00:00
|
|
|
}
|
|
|
|
waitingChange(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Document::ready(Information &&info) {
|
|
|
|
_info = std::move(info);
|
|
|
|
validateGoodThumbnail();
|
|
|
|
waitingChange(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Document::waitingChange(bool waiting) {
|
|
|
|
if (_waiting == waiting) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
_waiting = waiting;
|
|
|
|
const auto fade = [=](crl::time duration) {
|
|
|
|
if (!_radial.animating()) {
|
|
|
|
_radial.start(
|
|
|
|
st::defaultInfiniteRadialAnimation.sineDuration);
|
|
|
|
}
|
|
|
|
_fading.start(
|
|
|
|
[=] { waitingCallback(); },
|
|
|
|
_waiting ? 0. : 1.,
|
|
|
|
_waiting ? 1. : 0.,
|
|
|
|
duration);
|
|
|
|
};
|
|
|
|
if (waiting) {
|
|
|
|
if (_radial.animating()) {
|
|
|
|
_timer.cancel();
|
|
|
|
fade(kWaitingFastDuration);
|
|
|
|
} else {
|
|
|
|
_timer.callOnce(kWaitingShowDelay);
|
|
|
|
_timer.setCallback([=] {
|
|
|
|
fade(kWaitingShowDuration);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
_timer.cancel();
|
|
|
|
if (_radial.animating()) {
|
|
|
|
fade(kWaitingFastDuration);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Document::validateGoodThumbnail() {
|
2020-05-20 11:04:56 +00:00
|
|
|
if (_info.video.cover.isNull()
|
|
|
|
|| !_document
|
|
|
|
|| _document->goodThumbnailChecked()) {
|
2019-07-30 14:57:39 +00:00
|
|
|
return;
|
|
|
|
}
|
2022-01-21 14:30:12 +00:00
|
|
|
const auto sticker = (_document->sticker() != nullptr);
|
2020-03-27 11:40:50 +00:00
|
|
|
const auto document = _document;
|
|
|
|
const auto information = _info.video;
|
|
|
|
const auto key = document->goodThumbnailCacheKey();
|
|
|
|
const auto guard = base::make_weak(&document->session());
|
|
|
|
document->owner().cache().get(key, [=](QByteArray value) {
|
|
|
|
if (!value.isEmpty()) {
|
|
|
|
return;
|
2019-07-30 14:57:39 +00:00
|
|
|
}
|
2020-03-27 11:40:50 +00:00
|
|
|
const auto image = [&] {
|
|
|
|
auto result = information.cover;
|
|
|
|
if (information.rotation != 0) {
|
|
|
|
auto transform = QTransform();
|
|
|
|
transform.rotate(information.rotation);
|
|
|
|
result = result.transformed(transform);
|
|
|
|
}
|
|
|
|
if (result.size() != information.size) {
|
|
|
|
result = result.scaled(
|
|
|
|
information.size,
|
|
|
|
Qt::IgnoreAspectRatio,
|
|
|
|
Qt::SmoothTransformation);
|
|
|
|
}
|
2022-01-21 14:57:06 +00:00
|
|
|
if (!sticker && information.alpha) {
|
|
|
|
result = Images::Opaque(std::move(result));
|
|
|
|
}
|
2020-03-27 11:40:50 +00:00
|
|
|
return result;
|
|
|
|
}();
|
|
|
|
auto bytes = QByteArray();
|
|
|
|
{
|
|
|
|
auto buffer = QBuffer(&bytes);
|
2022-01-21 14:30:12 +00:00
|
|
|
image.save(&buffer, sticker ? "WEBP" : "JPG", kGoodThumbQuality);
|
2019-07-30 14:57:39 +00:00
|
|
|
}
|
2020-03-27 11:40:50 +00:00
|
|
|
const auto length = bytes.size();
|
|
|
|
if (!length || length > Storage::kMaxFileInMemory) {
|
|
|
|
LOG(("App Error: Bad thumbnail data for saving to cache."));
|
|
|
|
bytes = "(failed)";
|
|
|
|
}
|
|
|
|
crl::on_main(guard, [=] {
|
|
|
|
if (const auto active = document->activeMediaView()) {
|
|
|
|
active->setGoodThumbnail(image);
|
|
|
|
}
|
|
|
|
document->owner().cache().putIfEmpty(
|
|
|
|
document->goodThumbnailCacheKey(),
|
|
|
|
Storage::Cache::Database::TaggedValue(
|
|
|
|
base::duplicate(bytes),
|
|
|
|
Data::kImageCacheTag));
|
|
|
|
});
|
|
|
|
});
|
2019-07-30 14:57:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Document::waitingCallback() {
|
|
|
|
for (const auto &instance : _instances) {
|
|
|
|
instance->callWaitingCallback();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace Streaming
|
|
|
|
} // namespace Media
|