@@ -349,7 +349,10 @@ static void *venc_read_stream(void *arg)
349349}
350350
351351uint32_t detected_width , detected_height ;
352- bool detected_signal = false, streaming_flag = false, streaming_stopped = true;
352+ bool detected_signal = false, streaming_flag = false;
353+
354+ bool streaming_stopped = true;
355+ pthread_mutex_t streaming_stopped_mutex = PTHREAD_MUTEX_INITIALIZER ;
353356
354357pthread_t * streaming_thread = NULL ;
355358pthread_mutex_t streaming_mutex = PTHREAD_MUTEX_INITIALIZER ;
@@ -370,8 +373,27 @@ void set_streaming_flag(bool flag)
370373 pthread_mutex_lock (& streaming_mutex );
371374 streaming_flag = flag ;
372375 pthread_mutex_unlock (& streaming_mutex );
376+
377+ video_send_format_report ();
378+ }
379+
380+ void set_streaming_stopped (bool stopped )
381+ {
382+ pthread_mutex_lock (& streaming_stopped_mutex );
383+ streaming_stopped = stopped ;
384+ pthread_mutex_unlock (& streaming_stopped_mutex );
385+
386+ video_send_format_report ();
373387}
374388
389+ bool get_streaming_stopped ()
390+ {
391+ pthread_mutex_lock (& streaming_stopped_mutex );
392+ bool stopped = streaming_stopped ;
393+ pthread_mutex_unlock (& streaming_stopped_mutex );
394+ return stopped ;
395+ }
396+
375397void write_buffer_to_file (const uint8_t * buffer , size_t length , const char * filename )
376398{
377399 FILE * file = fopen (filename , "wb" );
@@ -385,8 +407,7 @@ void *run_video_stream(void *arg)
385407
386408 log_info ("running video stream" );
387409
388- streaming_stopped = false;
389-
410+ set_streaming_stopped (false);
390411 while (streaming_flag )
391412 {
392413 if (detected_signal == false)
@@ -528,6 +549,8 @@ void *run_video_stream(void *arg)
528549 uint32_t num = 0 ;
529550 VIDEO_FRAME_INFO_S stFrame ;
530551
552+
553+
531554 while (streaming_flag )
532555 {
533556 FD_ZERO (& fds );
@@ -539,6 +562,7 @@ void *run_video_stream(void *arg)
539562 if (r == 0 )
540563 {
541564 log_info ("select timeout" );
565+ ensure_sleep_mode_disabled ();
542566 break ;
543567 }
544568 if (r == -1 )
@@ -634,7 +658,7 @@ void *run_video_stream(void *arg)
634658
635659 log_info ("video stream thread exiting" );
636660
637- streaming_stopped = true;
661+ set_streaming_stopped ( true) ;
638662
639663 return NULL ;
640664}
@@ -670,9 +694,10 @@ void video_start_streaming()
670694 log_info ("starting video streaming" );
671695 if (streaming_thread != NULL )
672696 {
673- if (streaming_stopped == true) {
697+ bool stopped = get_streaming_stopped ();
698+ if (stopped == true) {
674699 log_error ("video streaming already stopped but streaming_thread is not NULL" );
675- assert (streaming_stopped == true);
700+ assert (stopped == true);
676701 }
677702 log_warn ("video streaming already started" );
678703 return ;
@@ -699,6 +724,21 @@ void video_start_streaming()
699724 streaming_thread = new_thread ;
700725}
701726
727+ bool wait_for_streaming_stopped ()
728+ {
729+ int attempts = 0 ;
730+ while (attempts < 30 ) {
731+ if (get_streaming_stopped () == true) {
732+ log_info ("video streaming stopped after %d attempts" , attempts );
733+ return true;
734+ }
735+ usleep (100000 ); // 100ms
736+ attempts ++ ;
737+ }
738+ log_error ("video streaming did not stop after 3s" );
739+ return false;
740+ }
741+
702742void video_stop_streaming ()
703743{
704744 if (streaming_thread == NULL ) {
@@ -710,14 +750,7 @@ void video_stop_streaming()
710750 set_streaming_flag (false);
711751
712752 log_info ("waiting for video streaming thread to exit" );
713- int attempts = 0 ;
714- while (!streaming_stopped && attempts < 30 ) {
715- usleep (100000 ); // 100ms
716- attempts ++ ;
717- }
718- if (!streaming_stopped ) {
719- log_error ("video streaming thread did not exit after 30s" );
720- }
753+ wait_for_streaming_stopped ();
721754
722755 pthread_join (* streaming_thread , NULL );
723756 free (streaming_thread );
@@ -726,13 +759,30 @@ void video_stop_streaming()
726759 log_info ("video streaming stopped" );
727760}
728761
762+ uint8_t video_get_streaming_status () {
763+ // streaming flag can be false when stopping streaming
764+ if (get_streaming_flag () == true) return 1 ;
765+ if (get_streaming_stopped () == false) return 2 ;
766+ return 0 ;
767+ }
768+
729769void video_restart_streaming ()
730770{
731- if (get_streaming_flag () == true)
771+ uint8_t streaming_status = video_get_streaming_status ();
772+ if (streaming_status == 0 )
732773 {
733- log_info ("restarting video streaming" );
774+ log_info ("will not restart video streaming because it's stopped" );
775+ return ;
776+ }
777+
778+ if (streaming_status == 2 ) {
734779 video_stop_streaming ();
735780 }
781+
782+ if (!wait_for_streaming_stopped ()) {
783+ return ;
784+ }
785+
736786 video_start_streaming ();
737787}
738788
0 commit comments