|
@@ -83,6 +83,26 @@ void StreamNode::work_gpu()
|
|
|
continue;
|
|
|
}
|
|
|
|
|
|
+ // ---> GPU 帧率控制逻辑 <---
|
|
|
+ if (target_duration_.count() > 0) {
|
|
|
+ auto now = std::chrono::high_resolution_clock::now();
|
|
|
+ auto elapsed = std::chrono::duration_cast<std::chrono::microseconds>(now - last_push_time_);
|
|
|
+ auto wait_duration = target_duration_ - elapsed;
|
|
|
+
|
|
|
+ if (wait_duration.count() > 0) {
|
|
|
+ // printf("Waiting for %lld us\n", (long long)wait_duration.count());
|
|
|
+ std::this_thread::sleep_for(wait_duration);
|
|
|
+ } else {
|
|
|
+ // 如果 elapsed 已经超过 target_duration,说明处理落后了,不需要等待
|
|
|
+ // 可以选择打印警告信息
|
|
|
+ // printf("Warning: Frame processing took longer than target duration (%lld us vs %lld us)\n",
|
|
|
+ // (long long)elapsed.count(), (long long)target_duration_.count());
|
|
|
+ }
|
|
|
+ // 更新时间戳,即使等待时间为0或负数,也要更新到当前时间点之后(理论上是目标时间点)
|
|
|
+ // 简单起见,我们就在 sleep 后/判断后更新到当前时间
|
|
|
+ last_push_time_ = std::chrono::high_resolution_clock::now();
|
|
|
+ }
|
|
|
+
|
|
|
auto metaData = std::make_shared<meta::MetaData>();
|
|
|
metaData->image = frame.clone();
|
|
|
metaData->from = name_;
|