From e5cd7e6d400430dd8f3a6cdc9ea416480836a154 Mon Sep 17 00:00:00 2001 From: John Preston Date: Thu, 21 Feb 2019 15:51:22 +0400 Subject: [PATCH] Fix streaming from the middle of the file. --- Telegram/SourceFiles/data/data_document.cpp | 1 + .../media/streaming/media_streaming_audio_track.cpp | 3 ++- .../media/streaming/media_streaming_audio_track.h | 2 ++ .../media/streaming/media_streaming_loader_mtproto.cpp | 7 +++++++ .../media/streaming/media_streaming_video_track.cpp | 7 ++++--- 5 files changed, 16 insertions(+), 4 deletions(-) diff --git a/Telegram/SourceFiles/data/data_document.cpp b/Telegram/SourceFiles/data/data_document.cpp index 7ef207d6c..ed44df027 100644 --- a/Telegram/SourceFiles/data/data_document.cpp +++ b/Telegram/SourceFiles/data/data_document.cpp @@ -326,6 +326,7 @@ void StartStreaming( auto options = Media::Streaming::PlaybackOptions(); options.speed = 1.; + options.position = (document->duration() / 2) * crl::time(1000); player->init(options); player->updates( ) | rpl::start_with_next_error_done([=](Update &&update) { diff --git a/Telegram/SourceFiles/media/streaming/media_streaming_audio_track.cpp b/Telegram/SourceFiles/media/streaming/media_streaming_audio_track.cpp index e183a2bdb..6d2d9cc12 100644 --- a/Telegram/SourceFiles/media/streaming/media_streaming_audio_track.cpp +++ b/Telegram/SourceFiles/media/streaming/media_streaming_audio_track.cpp @@ -25,7 +25,8 @@ AudioTrack::AudioTrack( , _stream(std::move(stream)) , _audioId(audioId) , _ready(std::move(ready)) -, _error(std::move(error)) { +, _error(std::move(error)) +, _playPosition(options.position) { Expects(_ready != nullptr); Expects(_error != nullptr); Expects(_audioId.playId() != 0); diff --git a/Telegram/SourceFiles/media/streaming/media_streaming_audio_track.h b/Telegram/SourceFiles/media/streaming/media_streaming_audio_track.h index e3f8aa743..8bcbb0eee 100644 --- a/Telegram/SourceFiles/media/streaming/media_streaming_audio_track.h +++ b/Telegram/SourceFiles/media/streaming/media_streaming_audio_track.h @@ -69,6 +69,8 @@ private: // Accessed from the main thread. base::Subscription _subscription; + // First set from the same unspecified thread before _ready is called. + // After that accessed from the main thread. rpl::variable _playPosition; }; diff --git a/Telegram/SourceFiles/media/streaming/media_streaming_loader_mtproto.cpp b/Telegram/SourceFiles/media/streaming/media_streaming_loader_mtproto.cpp index bb3d23c3b..cb941510a 100644 --- a/Telegram/SourceFiles/media/streaming/media_streaming_loader_mtproto.cpp +++ b/Telegram/SourceFiles/media/streaming/media_streaming_loader_mtproto.cpp @@ -138,6 +138,13 @@ void LoaderMtproto::cancelRequestsBefore(int offset) { _sender.requestCanceller(), &base::flat_map::value_type::second); _requests.erase(from, till); + + if (!_requests.empty() && _requests.front().first > offset) { + ranges::for_each( + base::take(_requests), + _sender.requestCanceller(), + &base::flat_map::value_type::second); + } } } // namespace Streaming diff --git a/Telegram/SourceFiles/media/streaming/media_streaming_video_track.cpp b/Telegram/SourceFiles/media/streaming/media_streaming_video_track.cpp index 99110fffb..7736edd5a 100644 --- a/Telegram/SourceFiles/media/streaming/media_streaming_video_track.cpp +++ b/Telegram/SourceFiles/media/streaming/media_streaming_video_track.cpp @@ -280,7 +280,9 @@ VideoTrackObject::TrackTime VideoTrackObject::trackTime() const { const auto correction = _audioId.playId() ? Media::Player::mixer()->getVideoTimeCorrection(_audioId) : Media::Player::Mixer::TimeCorrection(); - const auto knownValue = correction ? correction.audioPositionValue : 0; + const auto knownValue = correction + ? correction.audioPositionValue + : _startedPosition; const auto knownTime = correction ? correction.audioPositionTime : _startedTime; @@ -288,8 +290,7 @@ VideoTrackObject::TrackTime VideoTrackObject::trackTime() const { const auto sinceKnown = (worldNow - knownTime); result.worldNow = worldNow; - result.trackNow = _startedPosition - + knownValue + result.trackNow = knownValue + crl::time(std::round(sinceKnown * _options.speed)); return result; }