From 4bef1e9f5925b9b97d4dce306a04d141544ecb55 Mon Sep 17 00:00:00 2001 From: John Preston Date: Tue, 1 Feb 2022 15:49:53 +0300 Subject: [PATCH] Use unreliable video duration if open with audio. --- .../media/streaming/media_streaming_file.cpp | 16 ++++++++++------ .../media/streaming/media_streaming_file.h | 5 +++-- .../streaming/media_streaming_file_delegate.h | 1 + .../media/streaming/media_streaming_player.cpp | 4 ++++ .../media/streaming/media_streaming_player.h | 1 + 5 files changed, 19 insertions(+), 8 deletions(-) diff --git a/Telegram/SourceFiles/media/streaming/media_streaming_file.cpp b/Telegram/SourceFiles/media/streaming/media_streaming_file.cpp index 535e8b9c89..874cf5cd24 100644 --- a/Telegram/SourceFiles/media/streaming/media_streaming_file.cpp +++ b/Telegram/SourceFiles/media/streaming/media_streaming_file.cpp @@ -20,8 +20,10 @@ constexpr auto kMaxQueuedPackets = 1024; [[nodiscard]] bool UnreliableFormatDuration( not_null format, - not_null stream) { - return stream->codec + not_null stream, + Mode mode) { + return (mode == Mode::Video || mode == Mode::Inspection) + && stream->codec && (stream->codec->codec_id == AV_CODEC_ID_VP9) && format->iformat && format->iformat->name @@ -145,7 +147,8 @@ void File::Context::logFatal( Stream File::Context::initStream( not_null format, - AVMediaType type) { + AVMediaType type, + Mode mode) { auto result = Stream(); const auto index = result.index = av_find_best_stream( format, @@ -186,7 +189,7 @@ Stream File::Context::initStream( result.timeBase = info->time_base; result.duration = (info->duration != AV_NOPTS_VALUE) ? FFmpeg::PtsToTime(info->duration, result.timeBase) - : UnreliableFormatDuration(format, info) + : UnreliableFormatDuration(format, info, mode) ? kTimeUnknown : FFmpeg::PtsToTime(format->duration, FFmpeg::kUniversalTimeBase); if (result.duration == kTimeUnknown) { @@ -276,12 +279,13 @@ void File::Context::start(crl::time position) { return logFatal(qstr("avformat_find_stream_info"), error); } - auto video = initStream(format.get(), AVMEDIA_TYPE_VIDEO); + const auto mode = _delegate->fileOpenMode(); + auto video = initStream(format.get(), AVMEDIA_TYPE_VIDEO, mode); if (unroll()) { return; } - auto audio = initStream(format.get(), AVMEDIA_TYPE_AUDIO); + auto audio = initStream(format.get(), AVMEDIA_TYPE_AUDIO, mode); if (unroll()) { return; } diff --git a/Telegram/SourceFiles/media/streaming/media_streaming_file.h b/Telegram/SourceFiles/media/streaming/media_streaming_file.h index 80d2a030bf..8b75bf86d0 100644 --- a/Telegram/SourceFiles/media/streaming/media_streaming_file.h +++ b/Telegram/SourceFiles/media/streaming/media_streaming_file.h @@ -72,9 +72,10 @@ private: void logFatal(QLatin1String method, FFmpeg::AvErrorWrap error); void fail(Error error); - Stream initStream( + [[nodiscard]] Stream initStream( not_null format, - AVMediaType type); + AVMediaType type, + Mode mode); void seekToPosition( not_null format, const Stream &stream, diff --git a/Telegram/SourceFiles/media/streaming/media_streaming_file_delegate.h b/Telegram/SourceFiles/media/streaming/media_streaming_file_delegate.h index 2c832b10f8..cfc8b88cb6 100644 --- a/Telegram/SourceFiles/media/streaming/media_streaming_file_delegate.h +++ b/Telegram/SourceFiles/media/streaming/media_streaming_file_delegate.h @@ -19,6 +19,7 @@ enum class Error; class FileDelegate { public: + [[nodiscard]] virtual Mode fileOpenMode() = 0; [[nodiscard]] virtual bool fileReady( int headerSize, Stream &&video, diff --git a/Telegram/SourceFiles/media/streaming/media_streaming_player.cpp b/Telegram/SourceFiles/media/streaming/media_streaming_player.cpp index bdde638ef2..39cfff81f2 100644 --- a/Telegram/SourceFiles/media/streaming/media_streaming_player.cpp +++ b/Telegram/SourceFiles/media/streaming/media_streaming_player.cpp @@ -234,6 +234,10 @@ void Player::videoPlayedTill(crl::time position) { trackPlayedTill(*_video, _information.video.state, position); } +Mode Player::fileOpenMode() { + return _options.mode; +} + bool Player::fileReady(int headerSize, Stream &&video, Stream &&audio) { _waitingForData = false; diff --git a/Telegram/SourceFiles/media/streaming/media_streaming_player.h b/Telegram/SourceFiles/media/streaming/media_streaming_player.h index 45fb6f07c3..0cfd285a81 100644 --- a/Telegram/SourceFiles/media/streaming/media_streaming_player.h +++ b/Telegram/SourceFiles/media/streaming/media_streaming_player.h @@ -97,6 +97,7 @@ private: not_null delegate(); // FileDelegate methods are called only from the File thread. + Mode fileOpenMode() override; bool fileReady(int headerSize, Stream &&video, Stream &&audio) override; void fileError(Error error) override; void fileWaitingForData() override;