@@ -57,51 +57,23 @@ uvc_fb_t *UvcStreamer::fbGet()
5757 return nullptr ;
5858 }
5959
60- const uint64_t now_us = static_cast < uint64_t >( esp_timer_get_time () );
60+ const int64_t now_us = esp_timer_get_time ();
6161
62- if (last_frame_time_us_ == 0 ) {
63- current_frame_ = 0 ;
64- last_frame_time_us_ = now_us;
62+ // just started to steeam
63+ if (current_frame_start_time_us_ == 0 ) {
64+ current_frame_index_ = 0 ;
65+ current_frame_start_time_us_ = now_us;
6566 }
67+ // this much time has passed since the last frame time
68+ const int64_t elapsed_us = now_us - current_frame_start_time_us_;
6669
67- // Advance frames based on per-frame delays. Use a catch-up loop so if the host
68- // polls infrequently, we don't "slow down" the animation permanently.
69- //
70- // Safety: bound iterations so we never spin if delays are odd.
71- int safety = store_.count () + 1 ;
72- while (safety-- > 0 ) {
73- const FrameStore::Frame *cur = store_.get (current_frame_);
74- if (cur == nullptr ) {
75- current_frame_ = 0 ;
76- last_frame_time_us_ = now_us;
77- break ;
78- }
79-
80- int delay_ms = cur->delayMs ();
81- if (delay_ms < kMinFrameDelayMs ) {
82- delay_ms = kMinFrameDelayMs ;
83- }
84-
85- const uint64_t elapsed_ms = (now_us - last_frame_time_us_) / 1000ULL ;
86- if (elapsed_ms < static_cast <uint64_t >(delay_ms)) {
87- break ;
88- }
89-
90- current_frame_ = (current_frame_ + 1 ) % store_.count ();
91- last_frame_time_us_ += static_cast <uint64_t >(delay_ms) * 1000ULL ;
92- if (last_frame_time_us_ > now_us) {
93- // In case of timer anomalies, re-anchor.
94- last_frame_time_us_ = now_us;
95- break ;
96- }
70+ // do we need to move to the next frame?
71+ if (elapsed_us > store_.get (current_frame_index_)->delayMs () * 1000 ) {
72+ current_frame_index_ = (current_frame_index_ + 1 ) % store_.count ();
73+ current_frame_start_time_us_ = now_us;
9774 }
9875
99- if (safety <= 0 ) {
100- // We advanced "a lot" in one go; re-anchor to avoid repeated catch-up work.
101- last_frame_time_us_ = now_us;
102- }
103-
104- const FrameStore::Frame *f = store_.get (current_frame_);
76+ const FrameStore::Frame *f = store_.get (current_frame_index_);
10577 if (f == nullptr ) {
10678 return nullptr ;
10779 }
0 commit comments