- next_frame_start = compute_frame_start(frame->pts, pts_origin, video_timebase, start, rate);
- video_frame.received_timestamp = next_frame_start;
- producer_thread_should_quit.sleep_until(next_frame_start);
- frame_callback(timecode++,
- video_frame, 0, video_format,
- audio_frame, 0, audio_format);
+ for ( ;; ) {
+ next_frame_start = compute_frame_start(frame->pts, pts_origin, video_timebase, start, rate);
+ video_frame.received_timestamp = next_frame_start;
+ bool finished_wakeup = producer_thread_should_quit.sleep_until(next_frame_start);
+ if (finished_wakeup) {
+ frame_callback(timecode++,
+ video_frame, 0, video_format,
+ audio_frame, 0, audio_format);
+ break;
+ } else {
+ if (producer_thread_should_quit.should_quit()) break;
+
+ bool rewound = false;
+ if (process_queued_commands(format_ctx.get(), pathname, last_modified, &rewound)) {
+ return true;
+ }
+ // If we just rewound, drop this frame on the floor and be done.
+ if (rewound) {
+ video_frame_allocator->release_frame(video_frame);
+ break;
+ }
+ // OK, we didn't, so probably a rate change. Recalculate next_frame_start,
+ // but if it's now in the past, we'll reset the origin, so that we don't
+ // generate a huge backlog of frames that we need to run through quickly.
+ next_frame_start = compute_frame_start(frame->pts, pts_origin, video_timebase, start, rate);
+ steady_clock::time_point now = steady_clock::now();
+ if (next_frame_start < now) {
+ pts_origin = frame->pts;
+ start = next_frame_start = now;
+ }
+ }
+ }
+ last_pts = frame->pts;