@@ -481,17 +481,22 @@ namespace platf {
481481 capture (const push_captured_image_cb_t &push_captured_image_cb, const pull_free_image_cb_t &pull_free_image_cb, bool *cursor) override {
482482 auto next_frame = std::chrono::steady_clock::now ();
483483
484+ sleep_overshoot_tracker.reset ();
485+
484486 while (true ) {
485487 auto now = std::chrono::steady_clock::now ();
486488
487489 if (next_frame > now) {
488- std::this_thread::sleep_for (( next_frame - now) / 3 * 2 );
490+ std::this_thread::sleep_for (next_frame - now);
489491 }
490- while (next_frame > now) {
491- std::this_thread::sleep_for (1ns);
492- now = std::chrono::steady_clock::now ();
492+ now = std::chrono::steady_clock::now ();
493+ std::chrono::nanoseconds overshoot_ns = now - next_frame;
494+ log_sleep_overshoot (overshoot_ns);
495+
496+ next_frame += delay;
497+ if (next_frame < now) { // some major slowdown happened; we couldn't keep up
498+ next_frame = now + delay;
493499 }
494- next_frame = now + delay;
495500
496501 std::shared_ptr<platf::img_t > img_out;
497502 auto status = snapshot (pull_free_image_cb, img_out, 1000ms, *cursor);
@@ -622,17 +627,22 @@ namespace platf {
622627 capture (const push_captured_image_cb_t &push_captured_image_cb, const pull_free_image_cb_t &pull_free_image_cb, bool *cursor) override {
623628 auto next_frame = std::chrono::steady_clock::now ();
624629
630+ sleep_overshoot_tracker.reset ();
631+
625632 while (true ) {
626633 auto now = std::chrono::steady_clock::now ();
627634
628635 if (next_frame > now) {
629- std::this_thread::sleep_for (( next_frame - now) / 3 * 2 );
636+ std::this_thread::sleep_for (next_frame - now);
630637 }
631- while (next_frame > now) {
632- std::this_thread::sleep_for (1ns);
633- now = std::chrono::steady_clock::now ();
638+ now = std::chrono::steady_clock::now ();
639+ std::chrono::nanoseconds overshoot_ns = now - next_frame;
640+ log_sleep_overshoot (overshoot_ns);
641+
642+ next_frame += delay;
643+ if (next_frame < now) { // some major slowdown happened; we couldn't keep up
644+ next_frame = now + delay;
634645 }
635- next_frame = now + delay;
636646
637647 std::shared_ptr<platf::img_t > img_out;
638648 auto status = snapshot (pull_free_image_cb, img_out, 1000ms, *cursor);
0 commit comments