From ed097a5749ba8b78ffc7886aa1362c0095234ab6 Mon Sep 17 00:00:00 2001 From: "Steinar H. Gunderson" Date: Thu, 19 Apr 2018 00:43:54 +0200 Subject: [PATCH] Deliver more precise timestamps from FFmpegCapture. --- ffmpeg_capture.cpp | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/ffmpeg_capture.cpp b/ffmpeg_capture.cpp index 9ae3504..c79ecf3 100644 --- a/ffmpeg_capture.cpp +++ b/ffmpeg_capture.cpp @@ -484,7 +484,13 @@ bool FFmpegCapture::play_video(const string &pathname) } } video_frame->received_timestamp = next_frame_start; - audio_frame->received_timestamp = next_frame_start; + + // The easiest way to get all the rate conversions etc. right is to move the + // audio PTS into the video PTS timebase and go from there. (We'll get some + // rounding issues, but they should not be a big problem.) + int64_t audio_pts_as_video_pts = av_rescale_q(audio_pts, audio_timebase, video_timebase); + audio_frame->received_timestamp = compute_frame_start(audio_pts_as_video_pts, pts_origin, video_timebase, start, rate); + bool finished_wakeup = producer_thread_should_quit.sleep_until(next_frame_start); if (finished_wakeup) { if (audio_frame->len > 0) { -- 2.39.2