@@ -304,6 +304,7 @@ uint32_t detected_width, detected_height;
304
304
bool detected_signal = false, streaming_flag = false;
305
305
306
306
pthread_t * streaming_thread = NULL ;
307
+ pthread_mutex_t streaming_mutex = PTHREAD_MUTEX_INITIALIZER ;
307
308
308
309
void write_buffer_to_file (const uint8_t * buffer , size_t length , const char * filename )
309
310
{
@@ -388,28 +389,28 @@ void *run_video_stream(void *arg)
388
389
req .count = i ;
389
390
return errno ;
390
391
}
391
- printf ("VIDIOC_QUERYBUF successful for buffer %d\n" , i );
392
+ log_info ("VIDIOC_QUERYBUF successful for buffer %d\n" , i );
392
393
393
- printf ("plane: length = %d\n" , planes_buffer -> length );
394
- printf ("plane: offset = %d\n" , planes_buffer -> m .mem_offset );
394
+ log_info ("plane: length = %d\n" , planes_buffer -> length );
395
+ log_info ("plane: offset = %d\n" , planes_buffer -> m .mem_offset );
395
396
396
397
MB_BLK blk = RK_MPI_MB_GetMB (memPool , (planes_buffer )-> length , RK_TRUE );
397
398
if (blk == NULL )
398
399
{
399
- RK_LOGE ("get mb blk failed!" );
400
+ log_error ("get mb blk failed!" );
400
401
return -1 ;
401
402
}
402
- printf ("Got memory block for buffer %d\n" , i );
403
+ log_info ("Got memory block for buffer %d\n" , i );
403
404
404
405
buffers [i ].mb_blk = blk ;
405
406
406
407
RK_S32 buf_fd = (RK_MPI_MB_Handle2Fd (blk ));
407
408
if (buf_fd < 0 )
408
409
{
409
- RK_LOGE ("RK_MPI_MB_Handle2Fd failed!" );
410
+ log_error ("RK_MPI_MB_Handle2Fd failed!" );
410
411
return -1 ;
411
412
}
412
- printf ("Converted memory block to file descriptor for buffer %d\n" , i );
413
+ log_info ("Converted memory block to file descriptor for buffer %d\n" , i );
413
414
planes_buffer -> m .fd = buf_fd ;
414
415
}
415
416
@@ -427,7 +428,7 @@ void *run_video_stream(void *arg)
427
428
perror ("VIDIOC_QBUF failed" );
428
429
return errno ;
429
430
}
430
- printf ("VIDIOC_QBUF successful for buffer %d\n" , i );
431
+ log_info ("VIDIOC_QBUF successful for buffer %d\n" , i );
431
432
}
432
433
433
434
if (ioctl (video_dev_fd , VIDIOC_STREAMON , & type ) < 0 )
@@ -472,7 +473,7 @@ void *run_video_stream(void *arg)
472
473
{
473
474
continue ;
474
475
}
475
- perror ("select in video streaming" );
476
+ log_error ("select in video streaming" );
476
477
break ;
477
478
}
478
479
memset (& buf , 0 , sizeof (buf ));
@@ -482,7 +483,7 @@ void *run_video_stream(void *arg)
482
483
buf .length = 1 ;
483
484
if (ioctl (video_dev_fd , VIDIOC_DQBUF , & buf ) < 0 )
484
485
{
485
- perror ("VIDIOC_DQBUF failed" );
486
+ log_error ("VIDIOC_DQBUF failed" );
486
487
break ;
487
488
}
488
489
// printf("got frame, bytesused = %d\n", tmp_plane.bytesused);
@@ -518,11 +519,11 @@ void *run_video_stream(void *arg)
518
519
{
519
520
if (retried == true)
520
521
{
521
- RK_LOGE ("RK_MPI_VENC_SendFrame retry failed" );
522
+ log_error ("RK_MPI_VENC_SendFrame retry failed" );
522
523
}
523
524
else
524
525
{
525
- RK_LOGE ("RK_MPI_VENC_SendFrame failed,retrying" );
526
+ log_error ("RK_MPI_VENC_SendFrame failed,retrying" );
526
527
retried = true;
527
528
usleep (1000llu );
528
529
goto retry_send_frame ;
@@ -532,12 +533,12 @@ void *run_video_stream(void *arg)
532
533
num ++ ;
533
534
534
535
if (ioctl (video_dev_fd , VIDIOC_QBUF , & buf ) < 0 )
535
- printf ("failture VIDIOC_QBUF\n" );
536
+ log_error ("failture VIDIOC_QBUF\n" );
536
537
}
537
538
cleanup :
538
539
if (ioctl (video_dev_fd , VIDIOC_STREAMOFF , & type ) < 0 )
539
540
{
540
- perror ("VIDIOC_STREAMOFF failed" );
541
+ log_error ("VIDIOC_STREAMOFF failed" );
541
542
}
542
543
543
544
venc_stop ();
@@ -560,7 +561,7 @@ void video_shutdown()
560
561
{
561
562
if (should_exit == true)
562
563
{
563
- printf ("shutting down in progress already\n" );
564
+ log_info ("shutting down in progress already\n" );
564
565
return ;
565
566
}
566
567
video_stop_streaming ();
@@ -578,14 +579,17 @@ void video_shutdown()
578
579
{
579
580
shutdown (sub_dev_fd , SHUT_RDWR );
580
581
// close(sub_dev_fd);
581
- printf ("Closed sub_dev_fd\n" );
582
+ log_info ("Closed sub_dev_fd\n" );
582
583
}
583
584
584
585
if (memPool != MB_INVALID_POOLID )
585
586
{
586
587
RK_MPI_MB_DestroyPool (memPool );
587
588
}
588
- printf ("Destroyed memory pool\n" );
589
+ log_info ("Destroyed memory pool\n" );
590
+
591
+ pthread_mutex_destroy (& streaming_mutex );
592
+ log_info ("Destroyed streaming mutex\n" );
589
593
// if (format_thread != NULL) {
590
594
// pthread_join(*format_thread, NULL);
591
595
// free(format_thread);
@@ -594,23 +598,26 @@ void video_shutdown()
594
598
// printf("Joined format detection thread\n");
595
599
}
596
600
597
- // TODO: mutex?
598
601
599
602
void video_start_streaming ()
600
603
{
604
+ pthread_mutex_lock (& streaming_mutex );
601
605
if (streaming_thread != NULL )
602
606
{
603
607
log_info ("video streaming already started" );
608
+ pthread_mutex_unlock (& streaming_mutex );
604
609
return ;
605
610
}
606
611
streaming_thread = malloc (sizeof (pthread_t ));
607
612
assert (streaming_thread != NULL );
608
613
streaming_flag = true;
609
614
pthread_create (streaming_thread , NULL , run_video_stream , NULL );
615
+ pthread_mutex_unlock (& streaming_mutex );
610
616
}
611
617
612
618
void video_stop_streaming ()
613
619
{
620
+ pthread_mutex_lock (& streaming_mutex );
614
621
if (streaming_thread != NULL )
615
622
{
616
623
streaming_flag = false;
@@ -619,6 +626,7 @@ void video_stop_streaming()
619
626
streaming_thread = NULL ;
620
627
log_info ("video streaming stopped" );
621
628
}
629
+ pthread_mutex_unlock (& streaming_mutex );
622
630
}
623
631
624
632
void * run_detect_format (void * arg )
@@ -632,7 +640,7 @@ void *run_detect_format(void *arg)
632
640
if (ioctl (sub_dev_fd , VIDIOC_SUBSCRIBE_EVENT , & sub ) == -1 )
633
641
{
634
642
log_error ("cannot subscribe to event" );
635
- perror ("Cannot subscribe to event" );
643
+ log_error ("Cannot subscribe to event" );
636
644
goto exit ;
637
645
}
638
646
@@ -657,12 +665,12 @@ void *run_detect_format(void *arg)
657
665
else if (errno == ERANGE )
658
666
{
659
667
// Timings were found, but they are out of range of the hardware capabilities.
660
- printf ("HDMI status: out of range\n" );
668
+ log_warn ("HDMI status: out of range\n" );
661
669
video_report_format (false, "out_of_range" , 0 , 0 , 0 );
662
670
}
663
671
else
664
672
{
665
- perror ("error VIDIOC_QUERY_DV_TIMINGS" );
673
+ log_error ("error VIDIOC_QUERY_DV_TIMINGS" );
666
674
sleep (1 );
667
675
continue ;
668
676
}
@@ -681,19 +689,24 @@ void *run_detect_format(void *arg)
681
689
detected_height = dv_timings .bt .height ;
682
690
detected_signal = true;
683
691
video_report_format (true, NULL , detected_width , detected_height , frames_per_second );
692
+ pthread_mutex_lock (& streaming_mutex );
684
693
if (streaming_flag == true)
685
694
{
695
+ pthread_mutex_unlock (& streaming_mutex );
686
696
log_info ("restarting on going video streaming" );
687
697
video_stop_streaming ();
688
698
video_start_streaming ();
689
699
}
700
+ else
701
+ {
702
+ pthread_mutex_unlock (& streaming_mutex );
703
+ }
690
704
}
691
705
692
706
memset (& ev , 0 , sizeof (ev ));
693
707
if (ioctl (sub_dev_fd , VIDIOC_DQEVENT , & ev ) != 0 )
694
708
{
695
709
log_error ("failed to VIDIOC_DQEVENT" );
696
- perror ("failed to VIDIOC_DQEVENT" );
697
710
break ;
698
711
}
699
712
log_info ("New event of type %u" , ev .type );
@@ -715,12 +728,18 @@ void video_set_quality_factor(float factor)
715
728
716
729
// TODO: update venc bitrate without stopping streaming
717
730
731
+ pthread_mutex_lock (& streaming_mutex );
718
732
if (streaming_flag == true)
719
733
{
734
+ pthread_mutex_unlock (& streaming_mutex );
720
735
log_info ("restarting on going video streaming due to quality factor change" );
721
736
video_stop_streaming ();
722
737
video_start_streaming ();
723
738
}
739
+ else
740
+ {
741
+ pthread_mutex_unlock (& streaming_mutex );
742
+ }
724
743
}
725
744
726
745
float video_get_quality_factor () {
0 commit comments