@@ -149,17 +149,6 @@ namespace librealsense
149149 _name = create_composite_name (matchers, name);
150150 }
151151
152- composite_matcher::matcher_queue::matcher_queue ()
153- : q( QUEUE_MAX_SIZE ,
154- []( frame_holder const & fh )
155- {
156- // If queues are overrun, we'll get here
157- LOG_DEBUG ( " DROPPED frame " << fh );
158- } )
159- {
160- }
161-
162-
163152 void composite_matcher::dispatch (frame_holder f, const syncronization_environment& env)
164153 {
165154 clean_inactive_streams (f);
@@ -193,10 +182,7 @@ namespace librealsense
193182 if ( matcher )
194183 {
195184 if ( ! matcher->get_active () )
196- {
197185 matcher->set_active ( true );
198- _frames_queue[matcher.get ()].q .start ();
199- }
200186 return matcher;
201187 }
202188 }
@@ -231,9 +217,7 @@ namespace librealsense
231217 for (auto stream : matcher->get_streams ())
232218 {
233219 if (_matchers[stream])
234- {
235- _frames_queue.erase (_matchers[stream].get ());
236- }
220+ _frames.erase (_matchers[stream].get ());
237221 _matchers[stream] = matcher;
238222 _streams_id.push_back (stream);
239223 }
@@ -287,10 +271,6 @@ namespace librealsense
287271 // Mark ourselves inactive, so we don't get new dispatches
288272 set_active ( false );
289273
290- // Stop all our queues to wake up anyone waiting on them
291- for ( auto & fq : _frames_queue )
292- fq.second .q .stop ();
293-
294274 // Trickle the stop down to any children
295275 for ( auto m : _matchers )
296276 m.second ->stop ();
@@ -310,90 +290,76 @@ namespace librealsense
310290 {
311291 std::ostringstream os;
312292 os << ' [' ;
313- for ( auto m : matchers )
314- {
315- auto const & q = _frames_queue[m].q ;
316- q.peek ( [&os]( frame_holder const & fh ) {
317- os << fh;
318- } );
319- }
293+ for (auto m : matchers)
294+ os << *_frames[m];
320295 os << ' ]' ;
321296 return os.str ();
322297 }
323298
324299 void composite_matcher::sync (frame_holder f, const syncronization_environment& env)
325300 {
326- auto matcher = find_matcher (f);
327- if (!matcher)
328- {
329- LOG_ERROR (" didn't find any matcher for " << f << " will not be synchronized" );
330- _callback (std::move (f), env);
331- return ;
332- }
333- update_next_expected ( matcher, f );
334-
335- if ( ! _frames_queue[matcher.get ()].q .enqueue ( std::move ( f ) ) )
336- // If we get stopped, nothing to do!
337- return ;
301+ // We don't want to stop while syncing!
302+ std::lock_guard< std::mutex > lock ( _mutex );
338303
339- // We have a queue for each known stream we want to sync.
340- // E.g., for (Depth Color), we need to sync two frames, one from each.
341- // If we have a Color frame but not Depth, then Depth is "missing" and needs to be
342- // waited-for...
343-
344- std::map<librealsense::matcher *, frame_holder> frames_arrived;
345- std::vector< int > synced_frames;
346- std::vector< int > unsynced_frames;
347- std::vector< librealsense::matcher * > missing_streams;
348- std::vector< frame_holder > match;
349-
350- LOG_DEBUG (" [WOJTEK] " << __func__ << ' :' << *f.frame );
351- while ( true )
352- {
304+ auto matcher = find_matcher (f);
305+ if (!matcher)
353306 {
354- // We don't want to stop while syncing!
355- std::lock_guard< std::mutex > lock ( _mutex );
356-
357- // We want to release one frame from each matcher. If a matcher has nothing queued, it is "missing" and
358- // we need to consider waiting for it:
359- for ( auto s = _frames_queue.begin (); s != _frames_queue.end (); s++ )
360- if (s->second .q .empty ()) return ;
307+ LOG_ERROR (" didn't find any matcher for " << f << " will not be synchronized" );
308+ _callback (std::move (f), env);
309+ return ;
310+ }
311+ update_next_expected ( matcher, f );
361312
362- for ( auto s = _frames_queue.begin (); s != _frames_queue.end (); s++ )
363- {
364- librealsense::matcher * const m = s->first ;
365- while (s->second .q .try_dequeue (&frames_arrived[m]))
366- ;
367- }
313+ if (_frames[matcher.get ()])
314+ LOG_DEBUG (" [WOJTEK] " << __func__ << ' :' << " !new frame" << *_frames[matcher.get ()].frame << " < " << *f.frame );
315+ else
316+ LOG_DEBUG (" [WOJTEK] " << __func__ << ' :' << " new frame " << *f.frame );
317+ _frames[matcher.get ()] = std::move (f);
318+
319+ // We have a queue for each known stream we want to sync.
320+ // E.g., for (Depth Color), we need to sync two frames, one from each.
321+ // If we have a Color frame but not Depth, then Depth is "missing" and needs to be
322+ // waited-for...
323+
324+ std::vector< frame_holder > match;
325+ rs2_time_t tref = 0.0 , tmax = 0.0 ;
326+
327+ // We want to release one frame from each matcher. If a matcher has nothing queued, it is "missing" and
328+ // we need to consider waiting for it:
329+ for (auto & entry: _frames) {
330+ if (!entry.second ) {
331+ LOG_DEBUG (" [WOJTEK] " << __func__ << ' :' << " missing " << _name);
332+ return ;
333+ }
334+ tref += entry.second .frame ->get_frame_timestamp ();
335+ auto fps = entry.second .frame ->get_stream ()->get_framerate ();
336+ if (1.0 /fps > tmax) tmax = 1.0 /fps;
337+ }
368338
369- // From what we collected, we want to release only the frames that are synchronized (based on timestamp,
370- // number, etc.) -- anything else we'll leave to the next iteration. The synced frames should be the
371- // earliest possible!
339+ tref /= _frames.size ();
340+ tmax *= 1000 /2.0 /10.0 ;
372341
373- for (auto & matches: frames_arrived)
374- match.push_back (std::move (matches.second ));
342+ LOG_DEBUG (" [WOJTEK] " << __func__ << ' :' << std::fixed << tref << ' /' << tmax);
343+ for (auto & entry: _frames) {
344+ auto diff = tref - entry.second .frame ->get_frame_timestamp ();
345+ if (diff > tmax) {
346+ LOG_DEBUG (" [WOJTEK] " << __func__ << ' /' << diff << ' :' << *entry.second << " !too old" );
347+ entry.second .reset ();
348+ return ;
349+ }
350+ LOG_DEBUG (" [WOJTEK] " << __func__ << ' /' << diff << ' :' << *entry.second );
375351 }
376352
377- // The frameset should always be with the same order of streams (the first stream carries extra
378- // meaning because it decides the frameset properties) -- so we sort them...
379- if (match.size () > 1 ) {
380- std::sort ( match.begin (),
381- match.end (),
382- []( const frame_holder & f1, const frame_holder & f2 ) {
383- return f1.frame ->get_stream ()->get_unique_id ()
384- > f2.frame ->get_stream ()->get_unique_id ();
385- } );
386- }
353+ for (auto & entry: _frames)
354+ match.push_back (std::move (entry.second ));
387355
388356 frame_holder composite = env.source ->allocate_composite_frame (std::move (match));
389357 if (composite.frame )
390358 {
391- begin_callback ();
392- LOG_DEBUG (" [WOJTEK] " << __func__ << ' :' << *composite.frame );
393- _callback (std::move (composite), env);
359+ begin_callback ();
360+ LOG_DEBUG (" [WOJTEK] " << __func__ << ' :' << *composite.frame );
361+ _callback (std::move (composite), env);
394362 }
395- break ;
396- }
397363 }
398364
399365 frame_number_composite_matcher::frame_number_composite_matcher (
@@ -439,7 +405,7 @@ namespace librealsense
439405
440406 for (auto id: inactive_matchers)
441407 {
442- _frames_queue [_matchers[id].get ()].q . clear ();
408+ _frames [_matchers[id].get ()].reset ();
443409 }
444410 }
445411
@@ -468,7 +434,7 @@ namespace librealsense
468434 _next_expected[matcher.get ()].value = f.frame ->get_frame_number ()+1 .;
469435 }
470436
471- std::pair<double , double > extract_timestamps (frame_holder & a, frame_holder & b)
437+ std::pair<rs2_time_t , rs2_time_t > extract_timestamps (frame_holder & a, frame_holder & b)
472438 {
473439 if (a->get_frame_timestamp_domain () == b->get_frame_timestamp_domain ())
474440 return { a->get_frame_timestamp (), b->get_frame_timestamp () };
@@ -639,11 +605,11 @@ namespace librealsense
639605 << rsutils::string::from ( next_expected.value + threshold ) << " ; deactivating matcher!" ,
640606 env );
641607
642- auto const q_it = _frames_queue .find ( missing );
643- if ( q_it != _frames_queue .end () )
608+ auto const q_it = _frames .find ( missing );
609+ if ( q_it != _frames .end () )
644610 {
645- if ( q_it->second . q . empty () )
646- _frames_queue .erase ( q_it );
611+ if (! q_it->second )
612+ _frames .erase (q_it);
647613 }
648614 missing->set_active ( false );
649615 return true ;
0 commit comments