@@ -332,10 +332,6 @@ namespace librealsense
332332 }
333333 update_next_expected ( matcher, f );
334334
335- // We want to keep track of a "last-arrived" frame which is our current equivalent of "now" -- it contains the
336- // latest timestamp/frame-number/etc. that we can compare to.
337- auto const last_arrived = f->get_header ();
338-
339335 if ( ! _frames_queue[matcher.get ()].q .enqueue ( std::move ( f ) ) )
340336 // If we get stopped, nothing to do!
341337 return ;
@@ -345,131 +341,58 @@ namespace librealsense
345341 // If we have a Color frame but not Depth, then Depth is "missing" and needs to be
346342 // waited-for...
347343
348- std::vector< frame_holder * > frames_arrived;
349- std::vector< librealsense::matcher * > frames_arrived_matchers;
344+ std::map<librealsense::matcher *, frame_holder> frames_arrived;
350345 std::vector< int > synced_frames;
351346 std::vector< int > unsynced_frames;
352347 std::vector< librealsense::matcher * > missing_streams;
348+ std::vector< frame_holder > match;
353349
350+ LOG_DEBUG (" [WOJTEK] " << __func__ << ' :' << *f.frame );
354351 while ( true )
355352 {
356- missing_streams.clear ();
357- frames_arrived_matchers.clear ();
358- frames_arrived.clear ();
359-
360- std::vector< frame_holder > match;
361353 {
362354 // We don't want to stop while syncing!
363355 std::lock_guard< std::mutex > lock ( _mutex );
364356
365357 // We want to release one frame from each matcher. If a matcher has nothing queued, it is "missing" and
366358 // 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 ;
361+
367362 for ( auto s = _frames_queue.begin (); s != _frames_queue.end (); s++ )
368363 {
369364 librealsense::matcher * const m = s->first ;
370- if ( ! s->second .q .peek ( [&]( frame_holder & fh ) {
371- LOG_IF_ENABLE ( " ... have " << *fh.frame , env );
372- frames_arrived.push_back ( &fh );
373- frames_arrived_matchers.push_back ( m );
374- } ) )
375- {
376- missing_streams.push_back ( m );
377- }
378- }
379- if ( frames_arrived.empty () )
380- {
381- // LOG_IF_ENABLE( "... nothing more to do", env );
382- break ;
365+ while (s->second .q .try_dequeue (&frames_arrived[m]))
366+ ;
383367 }
384368
385369 // From what we collected, we want to release only the frames that are synchronized (based on timestamp,
386370 // number, etc.) -- anything else we'll leave to the next iteration. The synced frames should be the
387371 // earliest possible!
388372
389- frame_holder * curr_sync = frames_arrived[0 ];
390- synced_frames.clear ();
391- synced_frames.push_back ( 0 );
392-
393- // Sometimes we have to release newly-arrived frames even before frames we already had previously
394- // queued. If we have something like this, 'have_unsynced_frames' will be true:
395- unsynced_frames.clear ();
396- for ( auto i = 1 ; i < frames_arrived.size (); i++ )
397- {
398- if ( are_equivalent ( *curr_sync, *frames_arrived[i] ) )
399- {
400- synced_frames.push_back ( i );
401- }
402- else if ( is_smaller_than ( *frames_arrived[i], *curr_sync ) )
403- {
404- unsynced_frames.insert ( unsynced_frames.end (), synced_frames.begin (), synced_frames.end () );
405- synced_frames.clear ();
406- synced_frames.push_back ( i );
407- curr_sync = frames_arrived[i];
408- }
409- else
410- {
411- unsynced_frames.push_back ( i );
412- }
413- }
414- bool release_synced_frames = ( synced_frames.size () != 0 );
415- if ( unsynced_frames.empty () )
416- {
417- // Everything (could be only one!) matches together... but if we also have
418- // something missing, we can't release anything yet...
419- for ( auto i : missing_streams )
420- {
421- LOG_IF_ENABLE ( " ... missing " << i->get_name () << " , next expected @"
422- << rsutils::string::from ( _next_expected[i].value ) << " (from "
423- << rsutils::string::from ( _next_expected[i].fps ) << " fps)" ,
424- env );
425- if ( skip_missing_stream ( *curr_sync, i, last_arrived, env ) )
426- {
427- LOG_IF_ENABLE ( " ... cannot be synced; not waiting for it" , env );
428- continue ;
429- }
430-
431- LOG_IF_ENABLE ( " ... waiting for it" , env );
432- release_synced_frames = false ;
433- }
434- }
435- else
436- {
437- for ( auto i : unsynced_frames )
438- {
439- LOG_IF_ENABLE ( " - " << *frames_arrived[i]->frame << " is not in sync; won't be released" , env );
440- }
441- }
442- if ( ! release_synced_frames )
443- break ;
444-
445- match.reserve ( synced_frames.size () );
446-
447- for ( auto index : synced_frames )
448- {
449- frame_holder frame;
450- int const timeout_ms = 5000 ;
451- librealsense::matcher * m = frames_arrived_matchers[index];
452- _frames_queue[m].q .dequeue ( &frame, timeout_ms );
453- match.push_back ( std::move ( frame ) );
454- }
373+ for (auto & matches: frames_arrived)
374+ match.push_back (std::move (matches.second ));
455375 }
456376
457377 // The frameset should always be with the same order of streams (the first stream carries extra
458378 // meaning because it decides the frameset properties) -- so we sort them...
379+ if (match.size () > 1 ) {
459380 std::sort ( match.begin (),
460381 match.end (),
461382 []( const frame_holder & f1, const frame_holder & f2 ) {
462383 return f1.frame ->get_stream ()->get_unique_id ()
463384 > f2.frame ->get_stream ()->get_unique_id ();
464385 } );
465-
386+ }
466387
467388 frame_holder composite = env.source ->allocate_composite_frame (std::move (match));
468389 if (composite.frame )
469390 {
470- auto cb = begin_callback ();
391+ begin_callback ();
392+ LOG_DEBUG (" [WOJTEK] " << __func__ << ' :' << *composite.frame );
471393 _callback (std::move (composite), env);
472394 }
395+ break ;
473396 }
474397 }
475398
@@ -490,6 +413,7 @@ namespace librealsense
490413 }
491414 bool frame_number_composite_matcher::is_smaller_than (frame_holder & a, frame_holder & b)
492415 {
416+ if (!a || !b) return false ;
493417 return a->get_frame_number () < b->get_frame_number ();
494418 }
495419 void frame_number_composite_matcher::clean_inactive_streams (frame_holder& f)
@@ -565,16 +489,14 @@ namespace librealsense
565489 auto min_fps = std::min (a_fps, b_fps);
566490
567491 auto ts = extract_timestamps (a, b);
492+ bool equal = are_equivalent (ts.first , ts.second , min_fps);
568493
569- return are_equivalent (ts. first , ts. second , min_fps) ;
494+ return equal ;
570495 }
571496
572497 bool timestamp_composite_matcher::is_smaller_than (frame_holder & a, frame_holder & b)
573498 {
574- if (!a || !b)
575- {
576- return false ;
577- }
499+ if (!a || !b) return false ;
578500
579501 auto ts = extract_timestamps (a, b);
580502
@@ -737,8 +659,8 @@ namespace librealsense
737659 auto gap = 1000 . / fps;
738660 if ( std::abs ( a - b ) < ( gap / 2 ) )
739661 {
740- // LOG_DEBUG( "... " << rsutils::string::from( a ) << " == " << rsutils::string::from( b ) << " {diff}"
741- // << std::abs( a - b ) << " < " << rsutils::string::from( gap / 2 ) << "{gap/2}" );
662+ LOG_DEBUG ( " ... " << rsutils::string::from ( a ) << " == " << rsutils::string::from ( b ) << " {diff}"
663+ << std::abs ( a - b ) << " < " << rsutils::string::from ( gap / 2 ) << " {gap/2}" );
742664 return true ;
743665 }
744666
0 commit comments