@@ -52,7 +52,7 @@ void SyncStream::reset (String mainStreamKey)
5252 actualSampleRate = expectedSampleRate;
5353 globalStartTime = 0.0 ;
5454 isSynchronized = true ;
55- overrideHardwareTimestamps = false ; // do not override hardware timestamps for the main stream
55+ overrideHardwareTimestamps = true ; // main stream overrides hardware timestamps
5656 }
5757 else
5858 {
@@ -354,51 +354,55 @@ void Synchronizer::reset()
354354void Synchronizer::prepareForUpdate ()
355355{
356356 previousMainStreamKey = mainStreamKey;
357-
357+ mainStreamKey = String ();
358+ dataStreamObjects.clear ();
359+ streams.clear ();
358360 streamCount = 0 ;
359-
360- for (auto [id, stream] : streams)
361- stream->isActive = false ;
362361}
363362
364363void Synchronizer::finishedUpdate ()
365364{
365+ if (mainStreamKey.isEmpty () && streamCount > 0 )
366+ {
367+ // if no main stream is set, set the first non-hardware-synced stream as the main stream
368+ for (auto stream : dataStreamObjects)
369+ {
370+ if (! streamGeneratesTimestamps (stream->streamKey ))
371+ {
372+ mainStreamKey = stream->streamKey ;
373+ LOGD (" No main stream set, setting " , mainStreamKey, " as the main stream" );
374+ break ;
375+ }
376+ }
377+ }
366378}
367379
368380void Synchronizer::addDataStream (String streamKey, float expectedSampleRate, bool generatesTimestamps)
369381{
370382 LOGD (" Synchronizer adding " , streamKey, " with sample rate " , expectedSampleRate);
371- // if this is the first stream, make it the main one
372- if (mainStreamKey == " " )
373- mainStreamKey = streamKey;
374383
375384 // std::cout << "Main stream ID: " << mainStreamId << std::endl;
376385
377386 // if there's a stored value, and it appears again,
378387 // re-instantiate this as the main stream
379- if (mainStreamKey == previousMainStreamKey)
388+ if (streamKey == previousMainStreamKey)
380389 mainStreamKey = previousMainStreamKey;
381390
382- // if there's no Stream object yet, create a new one
383- if (streams.count (streamKey) == 0 )
384- {
385- // std::cout << "Creating new Stream object" << std::endl;
386- dataStreamObjects.add (new SyncStream (streamKey, expectedSampleRate, this , generatesTimestamps));
387- streams[streamKey] = dataStreamObjects.getLast ();
388- setSyncLine (streamKey, generatesTimestamps ? -1 : 0 ); // if the stream generates its own timestamps, set sync line to -1 (no sync line), otherwise set it to 0
389- }
390- else
391- {
392- // otherwise, indicate that the stream is currently active and update sample rate
393- streams[streamKey]->isActive = true ;
394- streams[streamKey]->expectedSampleRate = expectedSampleRate;
395- }
391+ // std::cout << "Creating new Stream object" << std::endl;
392+ dataStreamObjects.add (new SyncStream (streamKey, expectedSampleRate, this , generatesTimestamps));
393+ streams[streamKey] = dataStreamObjects.getLast ();
394+ setSyncLine (streamKey, generatesTimestamps ? -1 : 0 ); // if the stream generates its own timestamps, set sync line to -1 (no sync line), otherwise set it to 0
396395
397396 streamCount++;
398397}
399398
400399void Synchronizer::setMainDataStream (String streamKey)
401400{
401+ if (streams.count (streamKey) == 0 )
402+ {
403+ LOGD (" Cannot set " , streamKey, " as main data stream. Stream not found." );
404+ return ;
405+ }
402406 LOGD (" Synchronizer setting mainDataStream to " , streamKey);
403407 mainStreamKey = streamKey;
404408 reset ();
@@ -516,6 +520,9 @@ bool Synchronizer::isStreamSynced (String streamKey)
516520
517521bool Synchronizer::streamGeneratesTimestamps (String streamKey)
518522{
523+ if (streams.count (streamKey) == 0 )
524+ return false ;
525+
519526 return streams[streamKey]->generatesTimestamps && ! streams[streamKey]->overrideHardwareTimestamps ;
520527}
521528
@@ -535,12 +542,14 @@ SyncStatus Synchronizer::getStatus (String streamKey)
535542
536543void Synchronizer::hiResTimerCallback ()
537544{
545+ if (mainStreamKey.isEmpty ())
546+ return ;
538547
539548 const ScopedLock sl (synchronizerLock);
540549
541550 for (auto [key, stream] : streams)
542551 {
543- if (key != mainStreamKey)
552+ if (key != mainStreamKey && ! streamGeneratesTimestamps (key) )
544553 {
545554 stream->syncWith (streams[mainStreamKey]);
546555 }
0 commit comments