From ec71bf662e4809a8f915cda5937fef7c0fd50ed1 Mon Sep 17 00:00:00 2001 From: Radek Dutkiewicz Date: Wed, 17 Apr 2019 22:53:09 +0100 Subject: [PATCH] Fixed audio out of sync when playing videos Also in certain conditions, especially on slower hardware, when there is nothing to update on the screen and only videos are playing the framerate decreases significantly. Fixed by forcing screen update when at least one video is playing. Fixed video stuttering Adding a half frame offset to wait time seems to fix framerate fluctuations so the main thread has time to update the texture before it's replaced with a new one. --- src/media.cpp | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/media.cpp b/src/media.cpp index 7d07171a0..8daa49944 100644 --- a/src/media.cpp +++ b/src/media.cpp @@ -605,6 +605,7 @@ void FeVideoImp::video_thread() int64_t prev_duration = 0; sf::Time wait_time; + sf::Time half_frame_offset = sf::milliseconds( 8 ); if ((!sws_ctx) || (!rgba_buffer[0])) { @@ -634,8 +635,10 @@ void FeVideoImp::video_thread() // if ( detached_frame ) { - wait_time = (sf::Int64)detached_frame->pts * time_base - - m_parent->get_video_time(); + + wait_time = sf::seconds( detached_frame->pts + * av_q2d( m_parent->m_imp->m_format_ctx->streams[stream_id]->time_base )) + - m_parent->get_video_time() + half_frame_offset; if ( wait_time < max_sleep ) { @@ -1161,8 +1164,8 @@ bool FeMedia::tick() { m_video->display_texture->update( m_video->display_frame ); m_video->display_frame = NULL; - return true; } + return true; } return false;