Skip to content

Commit 6c95548

Browse files
Stablize encode timing
1 parent c4b7e8a commit 6c95548

File tree

1 file changed

+17
-6
lines changed

1 file changed

+17
-6
lines changed

src/video.cpp

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1922,12 +1922,14 @@ namespace video {
19221922
}
19231923
});
19241924

1925-
// set minimum frame time based on client-requested target framerate// set max frame time based on client-requested target framerate.
1926-
double minimum_fps_target = (config::video.minimum_fps_target > 0.0) ? config::video.minimum_fps_target : config.encodingFramerate;
1925+
// set max frame time based on client-requested target framerate.
1926+
double minimum_fps_target = (config::video.minimum_fps_target > 0.0) ? config::video.minimum_fps_target * 1000 : config.encodingFramerate;
19271927
auto max_frametime = std::chrono::nanoseconds(1000ms) * 1000 / minimum_fps_target;
1928+
// auto max_frametime = std::chrono::nanoseconds(1000ms) * 1000 / config.encodingFramerate;
19281929
auto encode_frame_threshold = std::chrono::nanoseconds(1000ms) * 1000 / config.encodingFramerate;
19291930
auto frame_variation_threshold = encode_frame_threshold / 4;
1930-
BOOST_LOG(info) << "Minimum FPS target set to ~"sv << (minimum_fps_target / 2) << "fps ("sv << max_frametime.count() * 2 << "ns)"sv;
1931+
auto min_frame_diff = encode_frame_threshold - frame_variation_threshold;
1932+
BOOST_LOG(info) << "Minimum FPS target set to ~"sv << (minimum_fps_target / 2000) << "fps ("sv << max_frametime * 2 << ")"sv;
19311933
BOOST_LOG(info) << "Encoding Frame threshold: "sv << encode_frame_threshold;
19321934

19331935
auto shutdown_event = mail->event<bool>(mail::shutdown);
@@ -1964,7 +1966,7 @@ namespace video {
19641966
}
19651967
}
19661968

1967-
std::chrono::steady_clock::time_point next_frame_start;
1969+
std::chrono::steady_clock::time_point encode_frame_timestamp;
19681970

19691971
while (true) {
19701972
// Break out of the encoding loop if any of the following are true:
@@ -2001,16 +2003,25 @@ namespace video {
20012003
if (!requested_idr_frame || images->peek()) {
20022004
if (auto img = images->pop(max_frametime)) {
20032005
frame_timestamp = img->frame_timestamp;
2006+
auto time_diff = *frame_timestamp - encode_frame_timestamp;
2007+
20042008
// If new frame comes in way too fast, just drop
2005-
if (*frame_timestamp < (next_frame_start - frame_variation_threshold)) {
2009+
if (time_diff < -frame_variation_threshold) {
20062010
continue;
20072011
}
2012+
20082013
if (session->convert(*img)) {
20092014
BOOST_LOG(error) << "Could not convert image"sv;
20102015
break;
20112016
}
20122017

2013-
next_frame_start = *frame_timestamp + encode_frame_threshold;
2018+
if (time_diff < frame_variation_threshold) {
2019+
*frame_timestamp = encode_frame_timestamp;
2020+
} else {
2021+
encode_frame_timestamp = *frame_timestamp;
2022+
}
2023+
2024+
encode_frame_timestamp += encode_frame_threshold;
20142025
} else if (!images->running()) {
20152026
break;
20162027
}

0 commit comments

Comments
 (0)