Simplify playing video tracking (and fix a crash).

This commit is contained in:
John Preston 2020-06-04 17:26:06 +04:00
parent e1d36cfd50
commit 2b9e4a8ddf
15 changed files with 53 additions and 85 deletions

View File

@ -216,11 +216,11 @@ void AutoDownloadBox::setupContent() {
_session->data().photoLoadSettingsChanged(); _session->data().photoLoadSettingsChanged();
} }
if (ranges::find_if(allowMoreTypes, _1 != Type::Photo) if (ranges::find_if(allowMoreTypes, _1 != Type::Photo)
!= allowMoreTypes.end()) { != allowMoreTypes.end()) {
_session->data().documentLoadSettingsChanged(); _session->data().documentLoadSettingsChanged();
} }
if (less) { if (less) {
_session->data().checkPlayingVideoFiles(); _session->data().checkPlayingAnimations();
} }
closeBox(); closeBox();
}); });

View File

@ -3224,43 +3224,19 @@ void Session::unregisterContactItem(
} }
} }
void Session::registerPlayingVideoFile(not_null<ViewElement*> view) { void Session::checkPlayingAnimations() {
if (++_playingVideoFiles[view] == 1) { auto check = base::flat_set<not_null<ViewElement*>>();
registerHeavyViewPart(view); for (const auto view : _heavyViewParts) {
}
}
void Session::unregisterPlayingVideoFile(not_null<ViewElement*> 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)) {
if (const auto media = view->media()) { if (const auto media = view->media()) {
media->stopAnimation(); if (const auto document = media->getDocument()) {
} if (document->isAnimation() || document->isVideoFile()) {
} check.emplace(view);
} }
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;
} }
} }
view->checkHeavyPart(); }
for (const auto view : check) {
view->media()->checkAnimation();
} }
} }
@ -3355,7 +3331,6 @@ void Session::registerItemView(not_null<ViewElement*> view) {
} }
void Session::unregisterItemView(not_null<ViewElement*> view) { void Session::unregisterItemView(not_null<ViewElement*> view) {
Expects(!_playingVideoFiles.contains(view));
Expects(!_heavyViewParts.contains(view)); Expects(!_heavyViewParts.contains(view));
const auto i = _views.find(view->data()); const auto i = _views.find(view->data());

View File

@ -613,10 +613,7 @@ public:
UserId contactId, UserId contactId,
not_null<HistoryItem*> item); not_null<HistoryItem*> item);
void registerPlayingVideoFile(not_null<ViewElement*> view); void checkPlayingAnimations();
void unregisterPlayingVideoFile(not_null<ViewElement*> view);
void checkPlayingVideoFiles();
void stopPlayingVideoFiles();
HistoryItem *findWebPageItem(not_null<WebPageData*> page) const; HistoryItem *findWebPageItem(not_null<WebPageData*> page) const;
QString findContactPhone(not_null<UserData*> contact) const; QString findContactPhone(not_null<UserData*> contact) const;
@ -958,7 +955,6 @@ private:
std::unordered_map< std::unordered_map<
UserId, UserId,
base::flat_set<not_null<ViewElement*>>> _contactViews; base::flat_set<not_null<ViewElement*>>> _contactViews;
base::flat_map<not_null<ViewElement*>, int> _playingVideoFiles;
base::flat_set<not_null<WebPageData*>> _webpagesUpdated; base::flat_set<not_null<WebPageData*>> _webpagesUpdated;
base::flat_set<not_null<GameData*>> _gamesUpdated; base::flat_set<not_null<GameData*>> _gamesUpdated;

View File

@ -1726,8 +1726,6 @@ void HistoryWidget::showHistory(
cancelTypingAction(); cancelTypingAction();
} }
session().data().stopPlayingVideoFiles();
clearReplyReturns(); clearReplyReturns();
if (_history) { if (_history) {
if (Ui::InFocusChain(_list)) { if (Ui::InFocusChain(_list)) {

View File

@ -229,4 +229,12 @@ TextState Contact::textState(QPoint point, StateRequest request) const {
return result; return result;
} }
void Contact::unloadHeavyPart() {
_userpic = nullptr;
}
bool Contact::hasHeavyPart() const {
return (_userpic != nullptr);
}
} // namespace HistoryView } // namespace HistoryView

View File

@ -59,13 +59,8 @@ public:
// Should be called only by Data::Session. // Should be called only by Data::Session.
void updateSharedContactUserId(UserId userId) override; void updateSharedContactUserId(UserId userId) override;
void unloadHeavyPart() override { void unloadHeavyPart() override;
_userpic = nullptr; bool hasHeavyPart() const override;
}
bool hasHeavyPart() const override {
return (_userpic != nullptr);
}
private: private:
QSize countOptimalSize() override; QSize countOptimalSize() override;

View File

@ -56,8 +56,8 @@ public:
void stopAnimation() override { void stopAnimation() override {
if (_attach) _attach->stopAnimation(); if (_attach) _attach->stopAnimation();
} }
int checkAnimationCount() override { void checkAnimation() override {
return _attach ? _attach->checkAnimationCount() : 0; if (_attach) _attach->checkAnimation();
} }
not_null<GameData*> game() { not_null<GameData*> game() {

View File

@ -1375,7 +1375,7 @@ void Gif::playAnimation(bool autoplay) {
stopAnimation(); stopAnimation();
} else if (_dataMedia->canBePlayed()) { } else if (_dataMedia->canBePlayed()) {
if (!autoplayEnabled()) { if (!autoplayEnabled()) {
history()->owner().checkPlayingVideoFiles(); history()->owner().checkPlayingAnimations();
} }
createStreamedPlayer(); createStreamedPlayer();
} }
@ -1432,12 +1432,11 @@ void Gif::checkStreamedIsStarted() const {
void Gif::setStreamed(std::unique_ptr<Streamed> value) { void Gif::setStreamed(std::unique_ptr<Streamed> value) {
const auto removed = (_streamed && !value); const auto removed = (_streamed && !value);
const auto set = (!_streamed && value); const auto set = (!_streamed && value);
if (removed) {
history()->owner().unregisterPlayingVideoFile(_parent);
}
_streamed = std::move(value); _streamed = std::move(value);
if (set) { 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() { void Gif::checkAnimation() {
if (!_streamed) { if (_streamed && !autoplayEnabled()) {
return 0; stopAnimation();
} else if (autoplayEnabled()) {
return 1;
} }
stopAnimation();
return 0;
} }
float64 Gif::dataProgress() const { float64 Gif::dataProgress() const {

View File

@ -87,7 +87,7 @@ public:
StateRequest request) const override; StateRequest request) const override;
void stopAnimation() override; void stopAnimation() override;
int checkAnimationCount() override; void checkAnimation() override;
TextWithEntities getCaption() const override { TextWithEntities getCaption() const override {
return _caption.toTextWithEntities(); return _caption.toTextWithEntities();

View File

@ -58,6 +58,14 @@ Location::~Location() {
} }
} }
void Location::unloadHeavyPart() {
_media = nullptr;
}
bool Location::hasHeavyPart() const {
return (_media != nullptr);
}
void Location::ensureMediaCreated() const { void Location::ensureMediaCreated() const {
if (_media) { if (_media) {
return; return;

View File

@ -58,12 +58,8 @@ public:
return isBubbleBottom(); return isBubbleBottom();
} }
void unloadHeavyPart() override { void unloadHeavyPart() override;
_media = nullptr; bool hasHeavyPart() const override;
}
bool hasHeavyPart() const override {
return (_media != nullptr);
}
private: private:
void ensureMediaCreated() const; void ensureMediaCreated() const;

View File

@ -141,8 +141,7 @@ public:
} }
virtual void clearStickerLoopPlayed() { virtual void clearStickerLoopPlayed() {
} }
virtual int checkAnimationCount() { virtual void checkAnimation() {
return 0;
} }
[[nodiscard]] virtual QSize sizeForGrouping() const { [[nodiscard]] virtual QSize sizeForGrouping() const {

View File

@ -414,21 +414,19 @@ void GroupedMedia::updateNeedBubbleState() {
} }
void GroupedMedia::stopAnimation() { void GroupedMedia::stopAnimation() {
for (auto &part : _parts) { for (const auto &part : _parts) {
part.content->stopAnimation(); part.content->stopAnimation();
} }
} }
int GroupedMedia::checkAnimationCount() { void GroupedMedia::checkAnimation() {
auto result = 0; for (const auto &part : _parts) {
for (auto &part : _parts) { part.content->checkAnimation();
result += part.content->checkAnimationCount();
} }
return result;
} }
bool GroupedMedia::hasHeavyPart() const { bool GroupedMedia::hasHeavyPart() const {
for (auto &part : _parts) { for (const auto &part : _parts) {
if (part.content->hasHeavyPart()) { if (part.content->hasHeavyPart()) {
return true; return true;
} }
@ -437,7 +435,7 @@ bool GroupedMedia::hasHeavyPart() const {
} }
void GroupedMedia::unloadHeavyPart() { void GroupedMedia::unloadHeavyPart() {
for (auto &part : _parts) { for (const auto &part : _parts) {
part.content->unloadHeavyPart(); part.content->unloadHeavyPart();
} }
} }

View File

@ -88,7 +88,7 @@ public:
} }
void stopAnimation() override; void stopAnimation() override;
int checkAnimationCount() override; void checkAnimation() override;
bool hasHeavyPart() const override; bool hasHeavyPart() const override;
void unloadHeavyPart() override; void unloadHeavyPart() override;

View File

@ -63,8 +63,8 @@ public:
void stopAnimation() override { void stopAnimation() override {
if (_attach) _attach->stopAnimation(); if (_attach) _attach->stopAnimation();
} }
int checkAnimationCount() override { void checkAnimation() override {
return _attach ? _attach->checkAnimationCount() : 0; if (_attach) _attach->checkAnimation();
} }
not_null<WebPageData*> webpage() { not_null<WebPageData*> webpage() {