leon před 1 měsícem
rodič
revize
5a1b1680c6

+ 0 - 1
src/nodes/draw/drawNode.cpp

@@ -67,7 +67,6 @@ void DrawNode::work()
         bool has_data = false;
         for (auto& input_buffer : input_buffers_)
         {
-            printf("DrawNode %s queue size : %d\n", name_.c_str(), input_buffer.second->size());
             std::shared_ptr<meta::MetaData> metaData;
             if (!input_buffer.second->try_pop(metaData))
             {

+ 20 - 0
src/nodes/stream/streamNode.cpp

@@ -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_;

+ 6 - 0
src/nodes/stream/streamNode.hpp

@@ -98,6 +98,12 @@ private:
 
     DecodeType decode_type_ = DecodeType::GPU;
     StreamStatus status_ = StreamStatus::CLOSED;
+
+
+    double target_fps_; // Desired output FPS (0 or negative means no control)
+    std::chrono::microseconds target_duration_{0}; // Duration per frame
+    std::chrono::time_point<std::chrono::high_resolution_clock> last_push_time_; // Time when the last frame was pushed
+    
 };
 
 }