From 8c0cd9b9e90f932aeb57116f125d5435b0f12d63 Mon Sep 17 00:00:00 2001 From: John Preston Date: Wed, 10 Apr 2019 15:26:15 +0400 Subject: [PATCH] Prepare Media::Streaming::Reader to be shared. --- Telegram/SourceFiles/data/data_document.cpp | 34 +++++--- Telegram/SourceFiles/data/data_document.h | 4 +- Telegram/SourceFiles/data/data_session.cpp | 54 +++++++++++-- Telegram/SourceFiles/data/data_session.h | 12 +++ .../media/player/media_player_instance.cpp | 20 ++--- .../media/player/media_player_instance.h | 4 +- .../media/streaming/media_streaming_file.cpp | 10 +-- .../media/streaming/media_streaming_file.h | 5 +- .../streaming/media_streaming_player.cpp | 4 +- .../media/streaming/media_streaming_player.h | 4 +- .../media/view/media_view_overlay_widget.cpp | 10 +-- .../SourceFiles/storage/file_download.cpp | 33 ++++---- Telegram/SourceFiles/storage/file_download.h | 12 +-- Telegram/SourceFiles/storage/file_upload.h | 3 - .../storage/streamed_file_downloader.cpp | 80 +++++++++++++++++++ .../storage/streamed_file_downloader.h | 60 ++++++++++++++ Telegram/gyp/telegram_sources.txt | 2 + 17 files changed, 281 insertions(+), 70 deletions(-) create mode 100644 Telegram/SourceFiles/storage/streamed_file_downloader.cpp create mode 100644 Telegram/SourceFiles/storage/streamed_file_downloader.h diff --git a/Telegram/SourceFiles/data/data_document.cpp b/Telegram/SourceFiles/data/data_document.cpp index 2bbbd6c70..a5f1b73ab 100644 --- a/Telegram/SourceFiles/data/data_document.cpp +++ b/Telegram/SourceFiles/data/data_document.cpp @@ -17,7 +17,10 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #include "core/mime_type.h" #include "media/audio/media_audio.h" #include "media/player/media_player_instance.h" +#include "media/streaming/media_streaming_loader_mtproto.h" +#include "media/streaming/media_streaming_loader_local.h" #include "storage/localstorage.h" +#include "storage/streamed_file_downloader.h" #include "platform/platform_specific.h" #include "history/history.h" #include "history/history_item.h" @@ -29,8 +32,6 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #include "mainwindow.h" #include "core/application.h" #include "lottie/lottie_animation.h" -#include "media/streaming/media_streaming_loader_mtproto.h" -#include "media/streaming/media_streaming_loader_local.h" namespace { @@ -736,7 +737,7 @@ void DocumentData::automaticLoadSettingsChanged() { bool DocumentData::loaded(FilePathResolve resolve) const { if (loading() && _loader->finished()) { if (_loader->cancelled()) { - destroyLoader(CancelledMtpFileLoader); + destroyLoader(CancelledFileLoader); } else { auto that = const_cast(this); that->_location = FileLocation(_loader->fileName()); @@ -768,7 +769,7 @@ bool DocumentData::loaded(FilePathResolve resolve) const { return !data().isEmpty() || !filepath(resolve).isEmpty(); } -void DocumentData::destroyLoader(mtpFileLoader *newValue) const { +void DocumentData::destroyLoader(FileLoader *newValue) const { const auto loader = std::exchange(_loader, newValue); if (cancelled()) { loader->cancel(); @@ -803,7 +804,7 @@ float64 DocumentData::progress() const { return loading() ? _loader->currentProgress() : (loaded() ? 1. : 0.); } -int32 DocumentData::loadOffset() const { +int DocumentData::loadOffset() const { return loading() ? _loader->currentOffset() : 0; } @@ -854,7 +855,7 @@ void DocumentData::save( } if (_loader) { if (!_loader->setFileName(toFile)) { - cancel(); // changes _actionOnLoad + cancel(); _loader = nullptr; } } @@ -865,7 +866,22 @@ void DocumentData::save( } } else { status = FileReady; - if (hasWebLocation()) { +/* if (auto reader = owner().documentStreamedReader(this, origin)) { + _loader = new Storage::StreamedFileDownloader( + id, + origin, + (saveToCache() + ? std::make_optional(Data::DocumentCacheKey(_dc, id)) + : std::nullopt), + std::move(reader), + toFile, + size, + locationType(), + (saveToCache() ? LoadToCacheAsWell : LoadToFileOnly), + fromCloud, + autoLoading, + cacheTag()); + } else */if (hasWebLocation()) { _loader = new mtpFileLoader( _urlLocation, size, @@ -913,13 +929,13 @@ void DocumentData::cancel() { return; } - destroyLoader(CancelledMtpFileLoader); + destroyLoader(CancelledFileLoader); _owner->notifyDocumentLayoutChanged(this); App::main()->documentLoadProgress(this); } bool DocumentData::cancelled() const { - return (_loader == CancelledMtpFileLoader); + return (_loader == CancelledFileLoader); } VoiceWaveform documentWaveformDecode(const QByteArray &encoded5bit) { diff --git a/Telegram/SourceFiles/data/data_document.h b/Telegram/SourceFiles/data/data_document.h index 6ea24f362..e8836d92d 100644 --- a/Telegram/SourceFiles/data/data_document.h +++ b/Telegram/SourceFiles/data/data_document.h @@ -112,7 +112,7 @@ public: void cancel(); [[nodiscard]] bool cancelled() const; [[nodiscard]] float64 progress() const; - [[nodiscard]] int32 loadOffset() const; + [[nodiscard]] int loadOffset() const; [[nodiscard]] bool uploading() const; void setWaitingForAlbum(); @@ -247,7 +247,7 @@ private: void validateGoodThumbnail(); void setMaybeSupportsStreaming(bool supports); - void destroyLoader(mtpFileLoader *newValue = nullptr) const; + void destroyLoader(FileLoader *newValue = nullptr) const; [[nodiscard]] bool thumbnailEnoughForSticker() const; diff --git a/Telegram/SourceFiles/data/data_session.cpp b/Telegram/SourceFiles/data/data_session.cpp index e5a885028..f212d375d 100644 --- a/Telegram/SourceFiles/data/data_session.cpp +++ b/Telegram/SourceFiles/data/data_session.cpp @@ -12,9 +12,9 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #include "apiwrap.h" #include "mainwidget.h" #include "core/application.h" -#include "core/crash_reports.h" // for CrashReports::SetAnnotation +#include "core/crash_reports.h" // CrashReports::SetAnnotation #include "ui/image/image.h" -#include "ui/image/image_source.h" // for Images::LocalFileSource +#include "ui/image/image_source.h" // Images::LocalFileSource #include "export/export_controller.h" #include "export/view/export_view_panel_controller.h" #include "window/notifications_manager.h" @@ -25,11 +25,13 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #include "inline_bots/inline_bot_layout_item.h" #include "storage/localstorage.h" #include "storage/storage_encrypted_file.h" -#include "media/player/media_player_instance.h" // for instance()->play(). +#include "media/player/media_player_instance.h" // instance()->play() +#include "media/streaming/media_streaming_loader.h" // unique_ptr +#include "media/streaming/media_streaming_reader.h" // make_shared #include "boxes/abstract_box.h" #include "passport/passport_form_controller.h" #include "window/themes/window_theme.h" -#include "lang/lang_keys.h" // for lang(lng_deleted) in user name. +#include "lang/lang_keys.h" // lang(lng_deleted) in user name #include "data/data_media_types.h" #include "data/data_folder.h" #include "data/data_channel.h" @@ -41,7 +43,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #include "data/data_web_page.h" #include "data/data_game.h" #include "data/data_poll.h" -#include "styles/style_boxes.h" // for st::backgroundSize +#include "styles/style_boxes.h" // st::backgroundSize namespace Data { namespace { @@ -150,6 +152,26 @@ rpl::producer PinnedDialogsCountMaxValue() { }); } +bool PruneDestroyedAndSet( + base::flat_map< + not_null, + std::weak_ptr<::Media::Streaming::Reader>> &readers, + not_null document, + const std::shared_ptr<::Media::Streaming::Reader> &reader) { + auto result = false; + for (auto i = begin(readers); i != end(readers);) { + if (i->first == document) { + (i++)->second = reader; + result = true; + } else if (i->second.lock() != nullptr) { + ++i; + } else { + i = readers.erase(i); + } + } + return result; +} + } // namespace Session::Session(not_null session) @@ -1067,6 +1089,28 @@ void Session::requestDocumentViewRepaint( } } +std::shared_ptr<::Media::Streaming::Reader> Session::documentStreamedReader( + not_null document, + FileOrigin origin) { + const auto i = _streamedReaders.find(document); + if (i != end(_streamedReaders)) { + if (auto result = i->second.lock()) { + return result; + } + } + auto loader = document->createStreamingLoader(origin); + if (!loader) { + return nullptr; + } + auto result = std::make_shared<::Media::Streaming::Reader>( + this, + std::move(loader)); + if (!PruneDestroyedAndSet(_streamedReaders, document, result)) { + _streamedReaders.emplace(document, result); + } + return result; +} + void Session::requestPollViewRepaint(not_null poll) { if (const auto i = _pollViews.find(poll); i != _pollViews.end()) { for (const auto view : i->second) { diff --git a/Telegram/SourceFiles/data/data_session.h b/Telegram/SourceFiles/data/data_session.h index 805a22b39..23dd8d002 100644 --- a/Telegram/SourceFiles/data/data_session.h +++ b/Telegram/SourceFiles/data/data_session.h @@ -39,6 +39,9 @@ namespace Media { namespace Clip { class Reader; } // namespace Clip +namespace Streaming { +class Reader; +} // namespace Streaming } // namespace Media namespace Export { @@ -397,6 +400,10 @@ public: void markMediaRead(not_null document); void requestPollViewRepaint(not_null poll); + std::shared_ptr<::Media::Streaming::Reader> documentStreamedReader( + not_null document, + FileOrigin origin); + HistoryItem *addNewMessage(const MTPMessage &data, NewMessageType type); struct SendActionAnimationUpdate { @@ -915,8 +922,13 @@ private: base::flat_set> _gamesUpdated; base::flat_set> _pollsUpdated; + base::flat_map< + not_null, + std::weak_ptr<::Media::Streaming::Reader>> _streamedReaders; + base::flat_map> _folders; //rpl::variable _defaultFeedId = FeedId(); // #feed + Groups _groups; std::unordered_map< not_null, diff --git a/Telegram/SourceFiles/media/player/media_player_instance.cpp b/Telegram/SourceFiles/media/player/media_player_instance.cpp index 6fdafcf51..2a243e6c5 100644 --- a/Telegram/SourceFiles/media/player/media_player_instance.cpp +++ b/Telegram/SourceFiles/media/player/media_player_instance.cpp @@ -12,7 +12,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #include "media/audio/media_audio.h" #include "media/audio/media_audio_capture.h" #include "media/streaming/media_streaming_player.h" -#include "media/streaming/media_streaming_loader.h" +#include "media/streaming/media_streaming_reader.h" #include "media/view/media_view_playback_progress.h" #include "calls/calls_instance.h" #include "history/history.h" @@ -59,7 +59,7 @@ struct Instance::Streamed { Streamed( AudioMsgId id, not_null<::Data::Session*> owner, - std::unique_ptr loader); + std::shared_ptr reader); AudioMsgId id; Streaming::Player player; @@ -71,9 +71,9 @@ struct Instance::Streamed { Instance::Streamed::Streamed( AudioMsgId id, not_null<::Data::Session*> owner, - std::unique_ptr loader) + std::shared_ptr reader) : id(id) -, player(owner, std::move(loader)) { +, player(owner, std::move(reader)) { } Instance::Data::Data(AudioMsgId::Type type, SharedMediaType overview) @@ -355,11 +355,13 @@ void Instance::play(const AudioMsgId &audioId) { if (document->isAudioFile() || document->isVoiceMessage() || document->isVideoMessage()) { - auto loader = document->createStreamingLoader(audioId.contextId()); - if (!loader) { + auto reader = document->owner().documentStreamedReader( + document, + audioId.contextId()); + if (!reader) { return; } - playStreamed(audioId, std::move(loader)); + playStreamed(audioId, std::move(reader)); } if (document->isVoiceMessage() || document->isVideoMessage()) { document->owner().markMediaRead(document); @@ -378,7 +380,7 @@ void Instance::playPause(const AudioMsgId &audioId) { void Instance::playStreamed( const AudioMsgId &audioId, - std::unique_ptr loader) { + std::shared_ptr reader) { Expects(audioId.audio() != nullptr); const auto data = getData(audioId.type()); @@ -388,7 +390,7 @@ void Instance::playStreamed( data->streamed = std::make_unique( audioId, &audioId.audio()->owner(), - std::move(loader)); + std::move(reader)); data->streamed->player.updates( ) | rpl::start_with_next_error([=](Streaming::Update &&update) { diff --git a/Telegram/SourceFiles/media/player/media_player_instance.h b/Telegram/SourceFiles/media/player/media_player_instance.h index 6441ac2c6..c20a25b26 100644 --- a/Telegram/SourceFiles/media/player/media_player_instance.h +++ b/Telegram/SourceFiles/media/player/media_player_instance.h @@ -26,7 +26,7 @@ class PlaybackProgress; namespace Media { namespace Streaming { class Player; -class Loader; +class Reader; struct PlaybackOptions; struct Update; enum class Error; @@ -195,7 +195,7 @@ private: void setupShortcuts(); void playStreamed( const AudioMsgId &audioId, - std::unique_ptr loader); + std::shared_ptr reader); Streaming::PlaybackOptions streamingOptions( const AudioMsgId &audioId, crl::time position = 0); diff --git a/Telegram/SourceFiles/media/streaming/media_streaming_file.cpp b/Telegram/SourceFiles/media/streaming/media_streaming_file.cpp index daf324522..813df6137 100644 --- a/Telegram/SourceFiles/media/streaming/media_streaming_file.cpp +++ b/Telegram/SourceFiles/media/streaming/media_streaming_file.cpp @@ -327,14 +327,14 @@ bool File::Context::finished() const { File::File( not_null owner, - std::unique_ptr loader) -: _reader(owner, std::move(loader)) { + std::shared_ptr reader) +: _reader(std::move(reader)) { } void File::start(not_null delegate, crl::time position) { stop(); - _context.emplace(delegate, &_reader); + _context.emplace(delegate, _reader.get()); _thread = std::thread([=, context = &*_context] { context->start(position); while (!context->finished()) { @@ -354,12 +354,12 @@ void File::stop() { _context->interrupt(); _thread.join(); } - _reader.stop(); + _reader->stop(); _context.reset(); } bool File::isRemoteLoader() const { - return _reader.isRemoteLoader(); + return _reader->isRemoteLoader(); } File::~File() { diff --git a/Telegram/SourceFiles/media/streaming/media_streaming_file.h b/Telegram/SourceFiles/media/streaming/media_streaming_file.h index 5faf2473f..31523add7 100644 --- a/Telegram/SourceFiles/media/streaming/media_streaming_file.h +++ b/Telegram/SourceFiles/media/streaming/media_streaming_file.h @@ -22,12 +22,11 @@ class Session; namespace Media { namespace Streaming { -class Loader; class FileDelegate; class File final { public: - File(not_null owner, std::unique_ptr loader); + File(not_null owner, std::shared_ptr reader); File(const File &other) = delete; File &operator=(const File &other) = delete; @@ -98,7 +97,7 @@ private: }; std::optional _context; - Reader _reader; + std::shared_ptr _reader; std::thread _thread; }; diff --git a/Telegram/SourceFiles/media/streaming/media_streaming_player.cpp b/Telegram/SourceFiles/media/streaming/media_streaming_player.cpp index 5bfa16010..954060d17 100644 --- a/Telegram/SourceFiles/media/streaming/media_streaming_player.cpp +++ b/Telegram/SourceFiles/media/streaming/media_streaming_player.cpp @@ -78,8 +78,8 @@ void SaveValidStartInformation(Information &to, Information &&from) { Player::Player( not_null owner, - std::unique_ptr loader) -: _file(std::make_unique(owner, std::move(loader))) + std::shared_ptr reader) +: _file(std::make_unique(owner, std::move(reader))) , _remoteLoader(_file->isRemoteLoader()) , _renderFrameTimer([=] { checkNextFrameRender(); }) { } diff --git a/Telegram/SourceFiles/media/streaming/media_streaming_player.h b/Telegram/SourceFiles/media/streaming/media_streaming_player.h index e75985c38..5cbc06c93 100644 --- a/Telegram/SourceFiles/media/streaming/media_streaming_player.h +++ b/Telegram/SourceFiles/media/streaming/media_streaming_player.h @@ -25,7 +25,7 @@ struct TrackState; namespace Media { namespace Streaming { -class Loader; +class Reader; class File; class AudioTrack; class VideoTrack; @@ -33,7 +33,7 @@ class VideoTrack; class Player final : private FileDelegate { public: // Public interfaces is used from the main thread. - Player(not_null owner, std::unique_ptr loader); + Player(not_null owner, std::shared_ptr reader); // Because we remember 'this' in calls to crl::on_main. Player(const Player &other) = delete; diff --git a/Telegram/SourceFiles/media/view/media_view_overlay_widget.cpp b/Telegram/SourceFiles/media/view/media_view_overlay_widget.cpp index ff47b971d..818fb05fd 100644 --- a/Telegram/SourceFiles/media/view/media_view_overlay_widget.cpp +++ b/Telegram/SourceFiles/media/view/media_view_overlay_widget.cpp @@ -24,7 +24,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #include "media/view/media_view_playback_controls.h" #include "media/view/media_view_group_thumbs.h" #include "media/streaming/media_streaming_player.h" -#include "media/streaming/media_streaming_loader.h" +#include "media/streaming/media_streaming_reader.h" #include "media/player/media_player_instance.h" #include "lottie/lottie_animation.h" #include "history/history.h" @@ -172,7 +172,7 @@ struct OverlayWidget::Streamed { template Streamed( not_null owner, - std::unique_ptr loader, + std::shared_ptr reader, QWidget *controlsParent, not_null controlsDelegate, Callback &&loadingCallback); @@ -201,11 +201,11 @@ struct OverlayWidget::LottieFile { template OverlayWidget::Streamed::Streamed( not_null owner, - std::unique_ptr loader, + std::shared_ptr reader, QWidget *controlsParent, not_null controlsDelegate, Callback &&loadingCallback) -: player(owner, std::move(loader)) +: player(owner, std::move(reader)) , controls(controlsParent, controlsDelegate) , radial( std::forward(loadingCallback), @@ -2042,7 +2042,7 @@ void OverlayWidget::streamingReady(Streaming::Information &&info) { void OverlayWidget::createStreamingObjects() { _streamed = std::make_unique( &_doc->owner(), - _doc->createStreamingLoader(fileOrigin()), + _doc->owner().documentStreamedReader(_doc, fileOrigin()), this, static_cast(this), [=] { waitingAnimationCallback(); }); diff --git a/Telegram/SourceFiles/storage/file_download.cpp b/Telegram/SourceFiles/storage/file_download.cpp index 5f4e8695c..10ef27eaa 100644 --- a/Telegram/SourceFiles/storage/file_download.cpp +++ b/Telegram/SourceFiles/storage/file_download.cpp @@ -229,7 +229,7 @@ float64 FileLoader::currentProgress() const { return snap(float64(currentOffset()) / fullSize(), 0., 1.); } -int32 FileLoader::fullSize() const { +int FileLoader::fullSize() const { return _size; } @@ -281,7 +281,7 @@ void FileLoader::removeFromQueue() { if (_queue->start == this) { _queue->start = _next; } - _next = _prev = 0; + _next = _prev = nullptr; _inQueue = false; } @@ -331,7 +331,7 @@ void FileLoader::start(bool loadFirst, bool prior) { } auto currentPriority = _downloader->currentPriority(); - FileLoader *before = 0, *after = 0; + FileLoader *before = nullptr, *after = nullptr; if (prior) { if (_inQueue && _priority == currentPriority) { if (loadFirst) { @@ -491,7 +491,7 @@ void FileLoader::cancel() { } void FileLoader::cancel(bool fail) { - bool started = currentOffset(true) > 0; + const auto started = (currentOffset() > 0); cancelRequests(); _cancelled = true; _finished = true; @@ -598,9 +598,8 @@ mtpFileLoader::mtpFileLoader( _queue = &i.value(); } -int32 mtpFileLoader::currentOffset(bool includeSkipped) const { - return (_fileIsOpen ? _file.size() : _data.size()) - - (includeSkipped ? 0 : _skippedBytes); +int mtpFileLoader::currentOffset() const { + return (_fileIsOpen ? _file.size() : _data.size()) - _skippedBytes; } Data::FileOrigin mtpFileLoader::fileOrigin() const { @@ -1218,7 +1217,7 @@ bool webFileLoader::loadPart() { return false; } -int32 webFileLoader::currentOffset(bool includeSkipped) const { +int webFileLoader::currentOffset() const { return _already; } @@ -1359,8 +1358,8 @@ void stopWebLoadManager() { delete _webLoadManager; delete _webLoadMainManager; delete _webLoadThread; - _webLoadThread = 0; - _webLoadMainManager = 0; + _webLoadThread = nullptr; + _webLoadMainManager = nullptr; _webLoadManager = FinishedWebLoadManager; } } @@ -1515,9 +1514,9 @@ void WebLoadManager::onMeta() { const auto loader = j.value(); const auto pairs = reply->rawHeaderPairs(); - for (auto i = pairs.begin(), e = pairs.end(); i != e; ++i) { - if (QString::fromUtf8(i->first).toLower() == "content-range") { - const auto m = QRegularExpression(qsl("/(\\d+)([^\\d]|$)")).match(QString::fromUtf8(i->second)); + for (const auto &pair : pairs) { + if (QString::fromUtf8(pair.first).toLower() == "content-range") { + const auto m = QRegularExpression(qsl("/(\\d+)([^\\d]|$)")).match(QString::fromUtf8(pair.second)); if (m.hasMatch()) { loader->setProgress(qMax(qint64(loader->data().size()), loader->already()), m.captured(1).toLongLong()); if (!handleReplyResult(loader, WebReplyProcessProgress)) { @@ -1598,19 +1597,19 @@ void WebLoadManager::finish() { void WebLoadManager::clear() { QMutexLocker lock(&_loaderPointersMutex); - for (LoaderPointers::iterator i = _loaderPointers.begin(), e = _loaderPointers.end(); i != e; ++i) { + for (auto i = _loaderPointers.begin(), e = _loaderPointers.end(); i != e; ++i) { if (i.value()) { - i.key()->_private = 0; + i.key()->_private = nullptr; } } _loaderPointers.clear(); - for_const (webFileLoaderPrivate *loader, _loaders) { + for (const auto loader : _loaders) { delete loader; } _loaders.clear(); - for (Replies::iterator i = _replies.begin(), e = _replies.end(); i != e; ++i) { + for (auto i = _replies.begin(), e = _replies.end(); i != e; ++i) { delete i.key(); } _replies.clear(); diff --git a/Telegram/SourceFiles/storage/file_download.h b/Telegram/SourceFiles/storage/file_download.h index aeee1755b..9a1c792f2 100644 --- a/Telegram/SourceFiles/storage/file_download.h +++ b/Telegram/SourceFiles/storage/file_download.h @@ -106,8 +106,8 @@ public: } virtual Data::FileOrigin fileOrigin() const; float64 currentProgress() const; - virtual int32 currentOffset(bool includeSkipped = false) const = 0; - int32 fullSize() const; + virtual int currentOffset() const = 0; + int fullSize() const; bool setFileName(const QString &filename); // set filename for loaders to cache void permitLoadFromCloud(); @@ -191,8 +191,8 @@ protected: QByteArray _data; - int32 _size; - LocationType _locationType; + int _size = 0; + LocationType _locationType = LocationType(); base::binary_guard _localLoading; mutable QByteArray _imageFormat; @@ -227,7 +227,7 @@ public: bool autoLoading, uint8 cacheTag); - int32 currentOffset(bool includeSkipped = false) const override; + int currentOffset() const override; Data::FileOrigin fileOrigin() const override; uint64 objId() const override; @@ -325,7 +325,7 @@ public: bool autoLoading, uint8 cacheTag); - int32 currentOffset(bool includeSkipped = false) const override; + int currentOffset() const override; void onProgress(qint64 already, qint64 size); void onFinished(const QByteArray &data); diff --git a/Telegram/SourceFiles/storage/file_upload.h b/Telegram/SourceFiles/storage/file_upload.h index 6b05b7dd6..a02f7aa83 100644 --- a/Telegram/SourceFiles/storage/file_upload.h +++ b/Telegram/SourceFiles/storage/file_upload.h @@ -59,9 +59,6 @@ public: const FullMsgId &msgId, const std::shared_ptr &file); - int32 currentOffset(const FullMsgId &msgId) const; // -1 means file not found - int32 fullSize(const FullMsgId &msgId) const; - void cancel(const FullMsgId &msgId); void pause(const FullMsgId &msgId); void confirm(const FullMsgId &msgId); diff --git a/Telegram/SourceFiles/storage/streamed_file_downloader.cpp b/Telegram/SourceFiles/storage/streamed_file_downloader.cpp new file mode 100644 index 000000000..f5d4024ed --- /dev/null +++ b/Telegram/SourceFiles/storage/streamed_file_downloader.cpp @@ -0,0 +1,80 @@ +/* +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 { + +constexpr auto kPartSize = Media::Streaming::Loader::kPartSize; + +} // namespace + +StreamedFileDownloader::StreamedFileDownloader( + uint64 objectId, + Data::FileOrigin origin, + std::optional cacheKey, + std::shared_ptr reader, + + // For FileLoader + const QString &toFile, + int32 size, + LocationType locationType, + LoadToCacheSetting toCache, + LoadFromCloudSetting fromCloud, + bool autoLoading, + uint8 cacheTag) +: FileLoader( + toFile, + size, + locationType, + toCache, + fromCloud, + autoLoading, + cacheTag) +, _objectId(objectId) +, _origin(origin) +, _cacheKey(cacheKey) +, _reader(std::move(reader)) { + _partIsSaved.resize((size + kPartSize - 1) / kPartSize, false); +} + +StreamedFileDownloader::~StreamedFileDownloader() { + stop(); +} + +uint64 StreamedFileDownloader::objId() const { + return _objectId; +} + +Data::FileOrigin StreamedFileDownloader::fileOrigin() const { + return _origin; +} + +int StreamedFileDownloader::currentOffset() const { + return 0; +} + +void StreamedFileDownloader::stop() { + cancelRequests(); +} + +std::optional StreamedFileDownloader::cacheKey() const { + return _cacheKey; +} + +void StreamedFileDownloader::cancelRequests() { +} + +bool StreamedFileDownloader::loadPart() { + return false; +} + +} // namespace Storage diff --git a/Telegram/SourceFiles/storage/streamed_file_downloader.h b/Telegram/SourceFiles/storage/streamed_file_downloader.h new file mode 100644 index 000000000..9e5d1f1cf --- /dev/null +++ b/Telegram/SourceFiles/storage/streamed_file_downloader.h @@ -0,0 +1,60 @@ +/* +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 +*/ +#pragma once + +#include "storage/file_download.h" +#include "storage/cache/storage_cache_types.h" + +namespace Media { +namespace Streaming { +class Reader; +} // namespace Streaming +} // namespace Media + +namespace Storage { + +class StreamedFileDownloader final : public FileLoader { +public: + StreamedFileDownloader( + uint64 objectId, + Data::FileOrigin origin, + std::optional cacheKey, + std::shared_ptr reader, + + // For FileLoader + const QString &toFile, + int32 size, + LocationType locationType, + LoadToCacheSetting toCache, + LoadFromCloudSetting fromCloud, + bool autoLoading, + uint8 cacheTag); + ~StreamedFileDownloader(); + + uint64 objId() const override; + Data::FileOrigin fileOrigin() const override; + int currentOffset() const override; + void stop() override; + +private: + std::optional cacheKey() const override; + void cancelRequests() override; + bool loadPart() override; + +private: + uint64 _objectId = 0; + Data::FileOrigin _origin; + std::optional _cacheKey; + std::shared_ptr _reader; + + std::vector _partIsSaved; // vector :D + int _nextPartIndex = 0; + +}; + +} // namespace Storage diff --git a/Telegram/gyp/telegram_sources.txt b/Telegram/gyp/telegram_sources.txt index 38f648d59..3a7a6fcc4 100644 --- a/Telegram/gyp/telegram_sources.txt +++ b/Telegram/gyp/telegram_sources.txt @@ -683,6 +683,8 @@ <(src_loc)/storage/storage_sparse_ids_list.h <(src_loc)/storage/storage_user_photos.cpp <(src_loc)/storage/storage_user_photos.h +<(src_loc)/storage/streamed_file_downloader.cpp +<(src_loc)/storage/streamed_file_downloader.h <(src_loc)/support/support_autocomplete.cpp <(src_loc)/support/support_autocomplete.h <(src_loc)/support/support_common.cpp