From 4cdf08cbfbd161f1f75ece7b3316a19234084a67 Mon Sep 17 00:00:00 2001 From: John Preston Date: Thu, 19 Dec 2019 13:50:33 +0300 Subject: [PATCH] Fix video with autoplay disabled. --- .../history/view/media/history_view_gif.cpp | 36 ++++++++----------- .../streaming/media_streaming_player.cpp | 23 +++++++++++- .../media/streaming/media_streaming_player.h | 1 + .../streaming/media_streaming_video_track.cpp | 14 ++++++++ .../streaming/media_streaming_video_track.h | 1 + .../media/view/media_view_overlay_widget.cpp | 5 +-- 6 files changed, 54 insertions(+), 26 deletions(-) diff --git a/Telegram/SourceFiles/history/view/media/history_view_gif.cpp b/Telegram/SourceFiles/history/view/media/history_view_gif.cpp index c081f03f16..5aa24ffeb7 100644 --- a/Telegram/SourceFiles/history/view/media/history_view_gif.cpp +++ b/Telegram/SourceFiles/history/view/media/history_view_gif.cpp @@ -231,7 +231,7 @@ QSize Gif::videoSize() const { bool Gif::downloadInCorner() const { return _data->isVideoFile() - && !autoplayEnabled() + && (_data->loading() || !autoplayEnabled()) && _data->canBeStreamed() && !_data->inappPlaybackFailed() && IsServerMsgId(_parent->data()->id); @@ -284,7 +284,10 @@ void Gif::draw(Painter &p, const QRect &r, TextSelection selection, crl::time ms ? &activeOwnPlaying->instance : nullptr; - if ((!streamed || item->isSending()) && displayLoading) { + if (displayLoading + && (!streamed + || item->isSending() + || (cornerDownload && _data->loading()))) { ensureAnimation(); if (!_animation->radial.animating()) { _animation->radial.start(dataProgress()); @@ -416,9 +419,11 @@ void Gif::draw(Painter &p, const QRect &r, TextSelection selection, crl::time ms || (!streamingMode && ((!_data->loaded() && !_data->loading()) || !autoplayEnabled()))) { - const auto radialOpacity = streamed + const auto radialOpacity = item->isSending() + ? 1. + : streamed ? streamed->waitingOpacity() - : (radial && _data->loaded() && !item->isSending()) + : (radial && _data->loaded()) ? _animation->radial.opacity() : 1.; auto inner = QRect(rthumb.x() + (rthumb.width() - st::msgFileSize) / 2, rthumb.y() + (rthumb.height() - st::msgFileSize) / 2, st::msgFileSize, st::msgFileSize); @@ -441,12 +446,12 @@ void Gif::draw(Painter &p, const QRect &r, TextSelection selection, crl::time ms p.setOpacity(radialOpacity); const auto icon = [&]() -> const style::icon * { - if (streamingMode) { + if (streamingMode && !_data->uploading()) { return nullptr; - } else if ((_data->loaded() || canBePlayed) && !radial) { + } else if ((_data->loaded() || canBePlayed) && (!radial || cornerDownload)) { return &(selected ? st::historyFileThumbPlaySelected : st::historyFileThumbPlay); } else if (radial || _data->loading()) { - if (item->id > 0 || _data->uploading()) { + if (!item->isSending() || _data->uploading()) { return &(selected ? st::historyFileThumbCancelSelected : st::historyFileThumbCancel); } return nullptr; @@ -462,7 +467,7 @@ void Gif::draw(Painter &p, const QRect &r, TextSelection selection, crl::time ms const auto fg = selected ? st::historyFileThumbRadialFgSelected : st::historyFileThumbRadialFg; - if (streamed) { + if (streamed && !_data->uploading()) { Ui::InfiniteRadialAnimation::Draw( p, streamed->waitingState(), @@ -479,17 +484,6 @@ void Gif::draw(Painter &p, const QRect &r, TextSelection selection, crl::time ms fg); } } - - if (!isRound && (!streamingMode || item->isSending())) { - auto statusX = paintx + st::msgDateImgDelta + st::msgDateImgPadding.x(); - auto statusY = painty + st::msgDateImgDelta + st::msgDateImgPadding.y(); - auto statusW = st::normalFont->width(_statusText) + 2 * st::msgDateImgPadding.x(); - auto statusH = st::normalFont->height + 2 * st::msgDateImgPadding.y(); - App::roundRect(p, style::rtlrect(statusX - st::msgDateImgPadding.x(), statusY - st::msgDateImgPadding.y(), statusW, statusH, width()), selected ? st::msgDateImgBgSelected : st::msgDateImgBg, selected ? DateSelectedCorners : DateCorners); - p.setFont(st::normalFont); - p.setPen(st::msgDateImgFg); - p.drawTextLeft(statusX, statusY, width(), _statusText, statusW - 2 * st::msgDateImgPadding.x()); - } } if (displayMute) { auto muteRect = style::rtlrect(rthumb.x() + (rthumb.width() - st::historyVideoMessageMuteSize) / 2, rthumb.y() + st::msgDateImgDelta, st::historyVideoMessageMuteSize, st::historyVideoMessageMuteSize, width()); @@ -615,8 +609,8 @@ void Gif::drawCornerStatus(Painter &p, bool selected) const { : _statusText; const auto padding = st::msgDateImgPadding; const auto radial = _animation && _animation->radial.animating(); - const auto cornerMute = _streamed && _data->isVideoFile(); const auto cornerDownload = downloadInCorner() && !_data->loaded() && !_data->loadedInMediaCache(); + const auto cornerMute = _streamed && _data->isVideoFile() && !cornerDownload; const auto addLeft = cornerDownload ? (st::historyVideoDownloadSize + 2 * padding.y()) : 0; const auto addRight = cornerMute ? st::historyVideoMuteSize : 0; const auto downloadWidth = cornerDownload ? st::normalFont->width(_downloadSize) : 0; @@ -1067,7 +1061,7 @@ void Gif::updateStatusText() const { statusSize = FileStatusSizeFailed; } else if (_data->uploading()) { statusSize = _data->uploadingData->offset; - } else if (_data->loading()) { + } else if (!downloadInCorner() && _data->loading()) { statusSize = _data->loadOffset(); } else if (_data->loaded() || _data->canBePlayed()) { statusSize = FileStatusSizeLoaded; diff --git a/Telegram/SourceFiles/media/streaming/media_streaming_player.cpp b/Telegram/SourceFiles/media/streaming/media_streaming_player.cpp index 9f42666a38..56c8aa8873 100644 --- a/Telegram/SourceFiles/media/streaming/media_streaming_player.cpp +++ b/Telegram/SourceFiles/media/streaming/media_streaming_player.cpp @@ -467,6 +467,11 @@ void Player::provideStartInformation() { } else { _stage = Stage::Ready; + if (_audio && _audioFinished) { + // Audio was stopped before it was ready. + _audio->stop(); + } + // Don't keep the reference to the video cover. auto copy = _information; _information.video.cover = QImage(); @@ -583,7 +588,10 @@ void Player::stopAudio() { stop(); } else if (_audio) { _audioFinished = true; - _audio->stop(); + if (_information.audio.state.duration != kTimeUnknown) { + // Audio is ready. + _audio->stop(); + } } } @@ -803,6 +811,15 @@ void Player::setSpeed(float64 speed) { } } +void Player::setWaitForMarkAsShown(bool wait) { + if (_options.waitForMarkAsShown != wait) { + _options.waitForMarkAsShown = wait; + if (_video) { + _video->setWaitForMarkAsShown(wait); + } + } +} + bool Player::active() const { return (_stage != Stage::Uninitialized) && !finished() && !failed(); } @@ -906,6 +923,10 @@ void Player::unlock() { Expects(_locks > 0); --_locks; + if (!_locks) { + stopAudio(); + setWaitForMarkAsShown(true); + } } bool Player::locked() const { diff --git a/Telegram/SourceFiles/media/streaming/media_streaming_player.h b/Telegram/SourceFiles/media/streaming/media_streaming_player.h index 6ba43e44c9..6182af4fb7 100644 --- a/Telegram/SourceFiles/media/streaming/media_streaming_player.h +++ b/Telegram/SourceFiles/media/streaming/media_streaming_player.h @@ -53,6 +53,7 @@ public: [[nodiscard]] float64 speed() const; void setSpeed(float64 speed); // 0.5 <= speed <= 2. + void setWaitForMarkAsShown(bool wait); [[nodiscard]] bool playing() const; [[nodiscard]] bool buffering() const; diff --git a/Telegram/SourceFiles/media/streaming/media_streaming_video_track.cpp b/Telegram/SourceFiles/media/streaming/media_streaming_video_track.cpp index d726dcda72..a1e1e4b2bd 100644 --- a/Telegram/SourceFiles/media/streaming/media_streaming_video_track.cpp +++ b/Telegram/SourceFiles/media/streaming/media_streaming_video_track.cpp @@ -42,6 +42,7 @@ public: void pause(crl::time time); void resume(crl::time time); void setSpeed(float64 speed); + void setWaitForMarkAsShown(bool wait); void interrupt(); void frameShown(); void addTimelineDelay(crl::time delayed); @@ -417,6 +418,13 @@ void VideoTrackObject::setSpeed(float64 speed) { _options.speed = speed; } +void VideoTrackObject::setWaitForMarkAsShown(bool wait) { + if (interrupted()) { + return; + } + _options.waitForMarkAsShown = wait; +} + bool VideoTrackObject::interrupted() const { return (_shared == nullptr); } @@ -908,6 +916,12 @@ void VideoTrack::setSpeed(float64 speed) { }); } +void VideoTrack::setWaitForMarkAsShown(bool wait) { + _wrapped.with([=](Implementation &unwrapped) { + unwrapped.setWaitForMarkAsShown(wait); + }); +} + crl::time VideoTrack::nextFrameDisplayTime() const { return _shared->nextFrameDisplayTime(); } diff --git a/Telegram/SourceFiles/media/streaming/media_streaming_video_track.h b/Telegram/SourceFiles/media/streaming/media_streaming_video_track.h index c152adfec3..3ed55c5ea5 100644 --- a/Telegram/SourceFiles/media/streaming/media_streaming_video_track.h +++ b/Telegram/SourceFiles/media/streaming/media_streaming_video_track.h @@ -47,6 +47,7 @@ public: // Called from the main thread. void setSpeed(float64 speed); + void setWaitForMarkAsShown(bool wait); // Called from the main thread. // Returns the position of the displayed frame. diff --git a/Telegram/SourceFiles/media/view/media_view_overlay_widget.cpp b/Telegram/SourceFiles/media/view/media_view_overlay_widget.cpp index dd47feb248..50671d477b 100644 --- a/Telegram/SourceFiles/media/view/media_view_overlay_widget.cpp +++ b/Telegram/SourceFiles/media/view/media_view_overlay_widget.cpp @@ -434,10 +434,7 @@ bool OverlayWidget::documentBubbleShown() const { void OverlayWidget::clearStreaming() { _fullScreenVideo = false; - if (_streamed) { - _streamed->instance.stopAudio(); - _streamed = nullptr; - } + _streamed = nullptr; } void OverlayWidget::documentUpdated(DocumentData *doc) {