]> git.sesse.net Git - nageru/blobdiff - ffmpeg_capture.cpp
Simple audio inputs are on cards, not signals.
[nageru] / ffmpeg_capture.cpp
index 6a235f5e4305a134a43b19918782f8befc7e4198..a0904dc3eb2f48881152cce24152274455eb1693 100644 (file)
@@ -208,7 +208,6 @@ YCbCrFormat decode_ycbcr_format(const AVPixFmtDescriptor *desc, const AVFrame *f
 FFmpegCapture::FFmpegCapture(const string &filename, unsigned width, unsigned height)
        : filename(filename), width(width), height(height), video_timebase{1, 1}
 {
-       // Not really used for anything.
        description = "Video: " + filename;
 
        last_frame = steady_clock::now();
@@ -510,6 +509,19 @@ bool FFmpegCapture::play_video(const string &pathname)
                                audio_frame->received_timestamp += duration_cast<steady_clock::duration>(duration<double>(offset));
                        }
 
+                       steady_clock::time_point now = steady_clock::now();
+                       if (duration<double>(now - next_frame_start).count() >= 0.1) {
+                               // If we don't have enough CPU to keep up, or if we have a live stream
+                               // where the initial origin was somehow wrong, we could be behind indefinitely.
+                               // In particular, this will give the audio resampler problems as it tries
+                               // to speed up to reduce the delay, hitting the low end of the buffer every time.
+                               fprintf(stderr, "%s: Playback %.0f ms behind, resetting time scale\n",
+                                       pathname.c_str(),
+                                       1e3 * duration<double>(now - next_frame_start).count());
+                               pts_origin = frame->pts;
+                               start = next_frame_start = now;
+                               timecode += MAX_FPS * 2 + 1;
+                       }
                        bool finished_wakeup = producer_thread_should_quit.sleep_until(next_frame_start);
                        if (finished_wakeup) {
                                if (audio_frame->len > 0) {