-
Notifications
You must be signed in to change notification settings - Fork 79
fix(win): pace the capture loop to a deadline instead of sleeping a full period #331
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -734,6 +734,7 @@ int main(int argc, char* argv[]) { | |
| int64_t nextWebcamWriteDueHns = 0; | ||
| const int64_t nominalWebcamIntervalHns = | ||
| static_cast<int64_t>(10'000'000ULL / std::max(1, webcamCapture.fps())); | ||
| auto nextFrameDue = std::chrono::steady_clock::now(); | ||
|
|
||
| while (!control.stopRequested && !encodeFailed) { | ||
| Microsoft::WRL::ComPtr<IMFSample> videoSample; | ||
|
|
@@ -890,8 +891,25 @@ int main(int argc, char* argv[]) { | |
| } | ||
|
|
||
| frameIndex += 1; | ||
| std::this_thread::sleep_for(frameDuration); | ||
| // Pace to a deadline, not `sleep_for(frameDuration)` after the work: | ||
| // capturing, converting and encoding a 1080p frame costs ~11 ms, so | ||
| // sleeping a whole period on top of it made the real period | ||
| // `work + 1/fps` -- 30 fps requested delivered 22.5 measured. | ||
| nextFrameDue += frameDuration; | ||
| const auto now = std::chrono::steady_clock::now(); | ||
| if (nextFrameDue < now) { | ||
| // Fell behind (slow frame, or waiting on the first one). Resync | ||
| // to now rather than firing a burst of catch-up frames, same as | ||
| // the webcam cadence above. | ||
| nextFrameDue = now; | ||
| } | ||
| std::this_thread::sleep_until(nextFrameDue); | ||
| } | ||
| std::cerr << "[pacing] frames=" << frameIndex << " elapsed_ms=" | ||
| << std::chrono::duration_cast<std::chrono::milliseconds>( | ||
| std::chrono::steady_clock::now() - control.recordingStartedAt) | ||
| .count() | ||
| << std::endl; | ||
|
Comment on lines
+908
to
+912
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win Exclude shutdown time from the pacing metric. The writer logs this interval after 🤖 Prompt for AI Agents |
||
| }; | ||
|
|
||
| std::thread videoWriterThread; | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win
Reset the deadline when a pause ends.
When
control.pausedis true,nextFrameDuebecomes stale. After a long pause, Line 904 sets it tonow, and Line 906 does not wait. BecauselatestFrameTextureis still set, the loop submits two frames without oneframeDurationinterval after resume. Reset the deadline on the paused-to-running transition separately from slow-frame resynchronization.🤖 Prompt for AI Agents