From 2b9e4a8ddfed1e335619fed5bc40512b63249ce4 Mon Sep 17 00:00:00 2001 From: John Preston Date: Thu, 4 Jun 2020 17:26:06 +0400 Subject: [PATCH] Simplify playing video tracking (and fix a crash). --- .../SourceFiles/boxes/auto_download_box.cpp | 4 +- Telegram/SourceFiles/data/data_session.cpp | 45 +++++-------------- Telegram/SourceFiles/data/data_session.h | 6 +-- .../SourceFiles/history/history_widget.cpp | 2 - .../view/media/history_view_contact.cpp | 8 ++++ .../history/view/media/history_view_contact.h | 9 +--- .../history/view/media/history_view_game.h | 4 +- .../history/view/media/history_view_gif.cpp | 19 +++----- .../history/view/media/history_view_gif.h | 2 +- .../view/media/history_view_location.cpp | 8 ++++ .../view/media/history_view_location.h | 8 +--- .../history/view/media/history_view_media.h | 3 +- .../view/media/history_view_media_grouped.cpp | 14 +++--- .../view/media/history_view_media_grouped.h | 2 +- .../view/media/history_view_web_page.h | 4 +- 15 files changed, 53 insertions(+), 85 deletions(-) diff --git a/Telegram/SourceFiles/boxes/auto_download_box.cpp b/Telegram/SourceFiles/boxes/auto_download_box.cpp index 6b4ff9d485..b7bf9915a4 100644 --- a/Telegram/SourceFiles/boxes/auto_download_box.cpp +++ b/Telegram/SourceFiles/boxes/auto_download_box.cpp @@ -216,11 +216,11 @@ void AutoDownloadBox::setupContent() { _session->data().photoLoadSettingsChanged(); } if (ranges::find_if(allowMoreTypes, _1 != Type::Photo) - != allowMoreTypes.end()) { + != allowMoreTypes.end()) { _session->data().documentLoadSettingsChanged(); } if (less) { - _session->data().checkPlayingVideoFiles(); + _session->data().checkPlayingAnimations(); } closeBox(); }); diff --git a/Telegram/SourceFiles/data/data_session.cpp b/Telegram/SourceFiles/data/data_session.cpp index dfb4db597e..64fddfbe5e 100644 --- a/Telegram/SourceFiles/data/data_session.cpp +++ b/Telegram/SourceFiles/data/data_session.cpp @@ -3224,43 +3224,19 @@ void Session::unregisterContactItem( } } -void Session::registerPlayingVideoFile(not_null view) { - if (++_playingVideoFiles[view] == 1) { - registerHeavyViewPart(view); - } -} - -void Session::unregisterPlayingVideoFile(not_null view) { - const auto i = _playingVideoFiles.find(view); - if (i != _playingVideoFiles.end()) { - if (!--i->second) { - _playingVideoFiles.erase(i); - view->checkHeavyPart(); - } - } else { - view->checkHeavyPart(); - } -} - -void Session::stopPlayingVideoFiles() { - for (const auto &[view, count] : base::take(_playingVideoFiles)) { +void Session::checkPlayingAnimations() { + auto check = base::flat_set>(); + for (const auto view : _heavyViewParts) { if (const auto media = view->media()) { - media->stopAnimation(); - } - } -} - -void Session::checkPlayingVideoFiles() { - const auto old = base::take(_playingVideoFiles); - for (const auto &[view, count] : old) { - if (const auto media = view->media()) { - if (const auto left = media->checkAnimationCount()) { - _playingVideoFiles.emplace(view, left); - registerHeavyViewPart(view); - continue; + if (const auto document = media->getDocument()) { + if (document->isAnimation() || document->isVideoFile()) { + check.emplace(view); + } } } - view->checkHeavyPart(); + } + for (const auto view : check) { + view->media()->checkAnimation(); } } @@ -3355,7 +3331,6 @@ void Session::registerItemView(not_null view) { } void Session::unregisterItemView(not_null view) { - Expects(!_playingVideoFiles.contains(view)); Expects(!_heavyViewParts.contains(view)); const auto i = _views.find(view->data()); diff --git a/Telegram/SourceFiles/data/data_session.h b/Telegram/SourceFiles/data/data_session.h index 6f3ac2e834..33629cd9ac 100644 --- a/Telegram/SourceFiles/data/data_session.h +++ b/Telegram/SourceFiles/data/data_session.h @@ -613,10 +613,7 @@ public: UserId contactId, not_null item); - void registerPlayingVideoFile(not_null view); - void unregisterPlayingVideoFile(not_null view); - void checkPlayingVideoFiles(); - void stopPlayingVideoFiles(); + void checkPlayingAnimations(); HistoryItem *findWebPageItem(not_null page) const; QString findContactPhone(not_null contact) const; @@ -958,7 +955,6 @@ private: std::unordered_map< UserId, base::flat_set>> _contactViews; - base::flat_map, int> _playingVideoFiles; base::flat_set> _webpagesUpdated; base::flat_set> _gamesUpdated; diff --git a/Telegram/SourceFiles/history/history_widget.cpp b/Telegram/SourceFiles/history/history_widget.cpp index 7df52c2969..e72ccd0f68 100644 --- a/Telegram/SourceFiles/history/history_widget.cpp +++ b/Telegram/SourceFiles/history/history_widget.cpp @@ -1726,8 +1726,6 @@ void HistoryWidget::showHistory( cancelTypingAction(); } - session().data().stopPlayingVideoFiles(); - clearReplyReturns(); if (_history) { if (Ui::InFocusChain(_list)) { diff --git a/Telegram/SourceFiles/history/view/media/history_view_contact.cpp b/Telegram/SourceFiles/history/view/media/history_view_contact.cpp index 602b6e18f8..5256e0fc7a 100644 --- a/Telegram/SourceFiles/history/view/media/history_view_contact.cpp +++ b/Telegram/SourceFiles/history/view/media/history_view_contact.cpp @@ -229,4 +229,12 @@ TextState Contact::textState(QPoint point, StateRequest request) const { return result; } +void Contact::unloadHeavyPart() { + _userpic = nullptr; +} + +bool Contact::hasHeavyPart() const { + return (_userpic != nullptr); +} + } // namespace HistoryView diff --git a/Telegram/SourceFiles/history/view/media/history_view_contact.h b/Telegram/SourceFiles/history/view/media/history_view_contact.h index 6dce7c8586..291035b0de 100644 --- a/Telegram/SourceFiles/history/view/media/history_view_contact.h +++ b/Telegram/SourceFiles/history/view/media/history_view_contact.h @@ -59,13 +59,8 @@ public: // Should be called only by Data::Session. void updateSharedContactUserId(UserId userId) override; - void unloadHeavyPart() override { - _userpic = nullptr; - } - - bool hasHeavyPart() const override { - return (_userpic != nullptr); - } + void unloadHeavyPart() override; + bool hasHeavyPart() const override; private: QSize countOptimalSize() override; diff --git a/Telegram/SourceFiles/history/view/media/history_view_game.h b/Telegram/SourceFiles/history/view/media/history_view_game.h index 892609b48c..ce7d525b76 100644 --- a/Telegram/SourceFiles/history/view/media/history_view_game.h +++ b/Telegram/SourceFiles/history/view/media/history_view_game.h @@ -56,8 +56,8 @@ public: void stopAnimation() override { if (_attach) _attach->stopAnimation(); } - int checkAnimationCount() override { - return _attach ? _attach->checkAnimationCount() : 0; + void checkAnimation() override { + if (_attach) _attach->checkAnimation(); } not_null game() { diff --git a/Telegram/SourceFiles/history/view/media/history_view_gif.cpp b/Telegram/SourceFiles/history/view/media/history_view_gif.cpp index 47a1b65821..b66f21b454 100644 --- a/Telegram/SourceFiles/history/view/media/history_view_gif.cpp +++ b/Telegram/SourceFiles/history/view/media/history_view_gif.cpp @@ -1375,7 +1375,7 @@ void Gif::playAnimation(bool autoplay) { stopAnimation(); } else if (_dataMedia->canBePlayed()) { if (!autoplayEnabled()) { - history()->owner().checkPlayingVideoFiles(); + history()->owner().checkPlayingAnimations(); } createStreamedPlayer(); } @@ -1432,12 +1432,11 @@ void Gif::checkStreamedIsStarted() const { void Gif::setStreamed(std::unique_ptr value) { const auto removed = (_streamed && !value); const auto set = (!_streamed && value); - if (removed) { - history()->owner().unregisterPlayingVideoFile(_parent); - } _streamed = std::move(value); if (set) { - history()->owner().registerPlayingVideoFile(_parent); + history()->owner().registerHeavyViewPart(_parent); + } else if (removed) { + _parent->checkHeavyPart(); } } @@ -1489,14 +1488,10 @@ void Gif::stopAnimation() { } } -int Gif::checkAnimationCount() { - if (!_streamed) { - return 0; - } else if (autoplayEnabled()) { - return 1; +void Gif::checkAnimation() { + if (_streamed && !autoplayEnabled()) { + stopAnimation(); } - stopAnimation(); - return 0; } float64 Gif::dataProgress() const { diff --git a/Telegram/SourceFiles/history/view/media/history_view_gif.h b/Telegram/SourceFiles/history/view/media/history_view_gif.h index 723cc836c7..4ac8342a5b 100644 --- a/Telegram/SourceFiles/history/view/media/history_view_gif.h +++ b/Telegram/SourceFiles/history/view/media/history_view_gif.h @@ -87,7 +87,7 @@ public: StateRequest request) const override; void stopAnimation() override; - int checkAnimationCount() override; + void checkAnimation() override; TextWithEntities getCaption() const override { return _caption.toTextWithEntities(); diff --git a/Telegram/SourceFiles/history/view/media/history_view_location.cpp b/Telegram/SourceFiles/history/view/media/history_view_location.cpp index 93080b1642..ece2a3e946 100644 --- a/Telegram/SourceFiles/history/view/media/history_view_location.cpp +++ b/Telegram/SourceFiles/history/view/media/history_view_location.cpp @@ -58,6 +58,14 @@ Location::~Location() { } } +void Location::unloadHeavyPart() { + _media = nullptr; +} + +bool Location::hasHeavyPart() const { + return (_media != nullptr); +} + void Location::ensureMediaCreated() const { if (_media) { return; diff --git a/Telegram/SourceFiles/history/view/media/history_view_location.h b/Telegram/SourceFiles/history/view/media/history_view_location.h index db7fe50efd..1cfe606413 100644 --- a/Telegram/SourceFiles/history/view/media/history_view_location.h +++ b/Telegram/SourceFiles/history/view/media/history_view_location.h @@ -58,12 +58,8 @@ public: return isBubbleBottom(); } - void unloadHeavyPart() override { - _media = nullptr; - } - bool hasHeavyPart() const override { - return (_media != nullptr); - } + void unloadHeavyPart() override; + bool hasHeavyPart() const override; private: void ensureMediaCreated() const; diff --git a/Telegram/SourceFiles/history/view/media/history_view_media.h b/Telegram/SourceFiles/history/view/media/history_view_media.h index 179e3f35e7..52a8155a8e 100644 --- a/Telegram/SourceFiles/history/view/media/history_view_media.h +++ b/Telegram/SourceFiles/history/view/media/history_view_media.h @@ -141,8 +141,7 @@ public: } virtual void clearStickerLoopPlayed() { } - virtual int checkAnimationCount() { - return 0; + virtual void checkAnimation() { } [[nodiscard]] virtual QSize sizeForGrouping() const { diff --git a/Telegram/SourceFiles/history/view/media/history_view_media_grouped.cpp b/Telegram/SourceFiles/history/view/media/history_view_media_grouped.cpp index 413b89036d..9b6778fc7b 100644 --- a/Telegram/SourceFiles/history/view/media/history_view_media_grouped.cpp +++ b/Telegram/SourceFiles/history/view/media/history_view_media_grouped.cpp @@ -414,21 +414,19 @@ void GroupedMedia::updateNeedBubbleState() { } void GroupedMedia::stopAnimation() { - for (auto &part : _parts) { + for (const auto &part : _parts) { part.content->stopAnimation(); } } -int GroupedMedia::checkAnimationCount() { - auto result = 0; - for (auto &part : _parts) { - result += part.content->checkAnimationCount(); +void GroupedMedia::checkAnimation() { + for (const auto &part : _parts) { + part.content->checkAnimation(); } - return result; } bool GroupedMedia::hasHeavyPart() const { - for (auto &part : _parts) { + for (const auto &part : _parts) { if (part.content->hasHeavyPart()) { return true; } @@ -437,7 +435,7 @@ bool GroupedMedia::hasHeavyPart() const { } void GroupedMedia::unloadHeavyPart() { - for (auto &part : _parts) { + for (const auto &part : _parts) { part.content->unloadHeavyPart(); } } diff --git a/Telegram/SourceFiles/history/view/media/history_view_media_grouped.h b/Telegram/SourceFiles/history/view/media/history_view_media_grouped.h index 769e6ab7ce..184278479e 100644 --- a/Telegram/SourceFiles/history/view/media/history_view_media_grouped.h +++ b/Telegram/SourceFiles/history/view/media/history_view_media_grouped.h @@ -88,7 +88,7 @@ public: } void stopAnimation() override; - int checkAnimationCount() override; + void checkAnimation() override; bool hasHeavyPart() const override; void unloadHeavyPart() override; diff --git a/Telegram/SourceFiles/history/view/media/history_view_web_page.h b/Telegram/SourceFiles/history/view/media/history_view_web_page.h index c04cf192cc..02d955573b 100644 --- a/Telegram/SourceFiles/history/view/media/history_view_web_page.h +++ b/Telegram/SourceFiles/history/view/media/history_view_web_page.h @@ -63,8 +63,8 @@ public: void stopAnimation() override { if (_attach) _attach->stopAnimation(); } - int checkAnimationCount() override { - return _attach ? _attach->checkAnimationCount() : 0; + void checkAnimation() override { + if (_attach) _attach->checkAnimation(); } not_null webpage() {