2019-04-10 11:26:15 +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 "storage/streamed_file_downloader.h"
|
|
|
|
|
|
|
|
#include "media/streaming/media_streaming_loader.h"
|
|
|
|
#include "media/streaming/media_streaming_reader.h"
|
|
|
|
|
|
|
|
namespace Storage {
|
|
|
|
namespace {
|
|
|
|
|
2019-04-11 07:59:18 +00:00
|
|
|
using namespace Media::Streaming;
|
|
|
|
|
|
|
|
constexpr auto kPartSize = Loader::kPartSize;
|
2019-12-04 07:42:55 +00:00
|
|
|
constexpr auto kRequestPartsCount = 8;
|
2019-04-10 11:26:15 +00:00
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
StreamedFileDownloader::StreamedFileDownloader(
|
2020-06-08 15:17:33 +00:00
|
|
|
not_null<Main::Session*> session,
|
|
|
|
|
2019-04-10 11:26:15 +00:00
|
|
|
uint64 objectId,
|
2019-04-11 07:59:18 +00:00
|
|
|
MTP::DcId dcId,
|
2019-04-10 11:26:15 +00:00
|
|
|
Data::FileOrigin origin,
|
2019-04-12 07:34:39 +00:00
|
|
|
Cache::Key cacheKey,
|
|
|
|
MediaKey fileLocationKey,
|
2019-04-11 07:59:18 +00:00
|
|
|
std::shared_ptr<Reader> reader,
|
2019-04-10 11:26:15 +00:00
|
|
|
|
|
|
|
// For FileLoader
|
|
|
|
const QString &toFile,
|
|
|
|
int32 size,
|
|
|
|
LocationType locationType,
|
|
|
|
LoadToCacheSetting toCache,
|
|
|
|
LoadFromCloudSetting fromCloud,
|
|
|
|
bool autoLoading,
|
|
|
|
uint8 cacheTag)
|
|
|
|
: FileLoader(
|
2020-06-08 15:17:33 +00:00
|
|
|
session,
|
2019-04-10 11:26:15 +00:00
|
|
|
toFile,
|
|
|
|
size,
|
2020-08-26 13:28:56 +00:00
|
|
|
size,
|
2019-04-10 11:26:15 +00:00
|
|
|
locationType,
|
|
|
|
toCache,
|
|
|
|
fromCloud,
|
|
|
|
autoLoading,
|
|
|
|
cacheTag)
|
|
|
|
, _objectId(objectId)
|
|
|
|
, _origin(origin)
|
|
|
|
, _cacheKey(cacheKey)
|
2019-04-12 07:34:39 +00:00
|
|
|
, _fileLocationKey(fileLocationKey)
|
2019-04-12 08:00:06 +00:00
|
|
|
, _reader(std::move(reader))
|
|
|
|
, _partsCount((size + kPartSize - 1) / kPartSize) {
|
|
|
|
_partIsSaved.resize(_partsCount, false);
|
2019-04-11 07:59:18 +00:00
|
|
|
|
|
|
|
_reader->partsForDownloader(
|
|
|
|
) | rpl::start_with_next([=](const LoadedPart &part) {
|
|
|
|
if (part.offset == LoadedPart::kFailedOffset) {
|
|
|
|
cancel(true);
|
|
|
|
} else {
|
|
|
|
savePart(std::move(part));
|
|
|
|
}
|
|
|
|
}, _lifetime);
|
2019-04-10 11:26:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
StreamedFileDownloader::~StreamedFileDownloader() {
|
2020-04-15 14:06:34 +00:00
|
|
|
if (!_finished) {
|
|
|
|
cancel();
|
2020-06-04 08:16:44 +00:00
|
|
|
} else {
|
|
|
|
_reader->cancelForDownloader(this);
|
2020-04-15 14:06:34 +00:00
|
|
|
}
|
2019-04-10 11:26:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
uint64 StreamedFileDownloader::objId() const {
|
|
|
|
return _objectId;
|
|
|
|
}
|
|
|
|
|
|
|
|
Data::FileOrigin StreamedFileDownloader::fileOrigin() const {
|
|
|
|
return _origin;
|
|
|
|
}
|
|
|
|
|
2019-12-04 07:42:55 +00:00
|
|
|
void StreamedFileDownloader::requestParts() {
|
|
|
|
while (!_finished
|
|
|
|
&& _nextPartIndex < _partsCount
|
|
|
|
&& _partsRequested < kRequestPartsCount) {
|
|
|
|
requestPart();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void StreamedFileDownloader::requestPart() {
|
|
|
|
Expects(!_finished);
|
|
|
|
|
|
|
|
const auto index = std::find(
|
|
|
|
begin(_partIsSaved) + _nextPartIndex,
|
|
|
|
end(_partIsSaved),
|
|
|
|
false
|
|
|
|
) - begin(_partIsSaved);
|
|
|
|
if (index == _partsCount) {
|
|
|
|
_nextPartIndex = _partsCount;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
_nextPartIndex = index + 1;
|
|
|
|
_reader->loadForDownloader(this, index * kPartSize);
|
|
|
|
++_partsRequested;
|
|
|
|
}
|
|
|
|
|
2019-04-12 09:25:00 +00:00
|
|
|
QByteArray StreamedFileDownloader::readLoadedPart(int offset) {
|
2020-08-26 13:28:56 +00:00
|
|
|
Expects(offset >= 0 && offset < _fullSize);
|
2019-04-12 09:25:00 +00:00
|
|
|
Expects(!(offset % kPartSize));
|
|
|
|
|
|
|
|
const auto index = (offset / kPartSize);
|
|
|
|
return _partIsSaved[index]
|
|
|
|
? readLoadedPartBack(offset, kPartSize)
|
|
|
|
: QByteArray();
|
|
|
|
}
|
|
|
|
|
2019-04-12 07:34:39 +00:00
|
|
|
Storage::Cache::Key StreamedFileDownloader::cacheKey() const {
|
2019-04-10 11:26:15 +00:00
|
|
|
return _cacheKey;
|
|
|
|
}
|
|
|
|
|
2019-04-11 07:59:18 +00:00
|
|
|
std::optional<MediaKey> StreamedFileDownloader::fileLocationKey() const {
|
2019-04-12 07:34:39 +00:00
|
|
|
return _fileLocationKey;
|
2019-04-11 07:59:18 +00:00
|
|
|
}
|
|
|
|
|
2019-12-05 08:32:33 +00:00
|
|
|
void StreamedFileDownloader::cancelHook() {
|
2019-04-12 08:00:06 +00:00
|
|
|
_partsRequested = 0;
|
2019-04-11 07:59:18 +00:00
|
|
|
_nextPartIndex = 0;
|
|
|
|
|
2019-04-12 09:25:00 +00:00
|
|
|
_reader->cancelForDownloader(this);
|
2019-04-10 11:26:15 +00:00
|
|
|
}
|
|
|
|
|
2019-12-04 12:15:58 +00:00
|
|
|
void StreamedFileDownloader::startLoading() {
|
|
|
|
requestParts();
|
2019-04-11 07:59:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void StreamedFileDownloader::savePart(const LoadedPart &part) {
|
|
|
|
Expects(part.offset >= 0 && part.offset < _reader->size());
|
|
|
|
Expects(part.offset % kPartSize == 0);
|
2019-04-12 07:34:39 +00:00
|
|
|
|
2019-04-11 07:59:18 +00:00
|
|
|
if (_finished || _cancelled) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-04-12 07:34:39 +00:00
|
|
|
const auto offset = part.offset;
|
|
|
|
const auto index = offset / kPartSize;
|
2019-04-12 08:00:06 +00:00
|
|
|
Assert(index >= 0 && index < _partsCount);
|
2019-04-11 07:59:18 +00:00
|
|
|
if (_partIsSaved[index]) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
_partIsSaved[index] = true;
|
2019-04-12 08:00:06 +00:00
|
|
|
++_partsSaved;
|
2019-04-11 07:59:18 +00:00
|
|
|
|
|
|
|
if (index < _nextPartIndex) {
|
2019-04-12 08:00:06 +00:00
|
|
|
--_partsRequested;
|
2019-04-11 07:59:18 +00:00
|
|
|
}
|
2019-04-12 07:34:39 +00:00
|
|
|
if (!writeResultPart(offset, bytes::make_span(part.bytes))) {
|
2019-04-11 07:59:18 +00:00
|
|
|
return;
|
|
|
|
}
|
2020-04-17 14:02:10 +00:00
|
|
|
_reader->doneForDownloader(offset);
|
2019-04-12 08:00:06 +00:00
|
|
|
if (_partsSaved == _partsCount) {
|
2020-04-17 14:02:10 +00:00
|
|
|
finalizeResult();
|
|
|
|
} else {
|
|
|
|
requestParts();
|
|
|
|
notifyAboutProgress();
|
2019-04-11 07:59:18 +00:00
|
|
|
}
|
2019-04-10 11:26:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace Storage
|