@@ -16,6 +16,8 @@ UPLOAD_PIPE_FILE_NAME=${SE_UPLOAD_PIPE_FILE_NAME:-"uploadpipe"}
1616SE_SERVER_PROTOCOL=${SE_SERVER_PROTOCOL:- " http"  
1717poll_interval=${SE_VIDEO_POLL_INTERVAL:- 1} 
1818max_attempts=${SE_VIDEO_WAIT_ATTEMPTS:- 50} 
19+ file_ready_max_attempts=${SE_VIDEO_FILE_READY_WAIT_ATTEMPTS:- 10} 
20+ wait_uploader_shutdown_max_attempts=${SE_VIDEO_WAIT_UPLOADER_SHUTDOWN_ATTEMPTS:- 10} 
1921ts_format=${SE_LOG_TIMESTAMP_FORMAT:- " %Y-%m-%d %H:%M:%S,%3N"  
2022process_name=" video.recorder" 
2123
@@ -90,21 +92,20 @@ function wait_for_api_respond() {
9092}
9193
9294function  wait_util_uploader_shutdown()  {
93-   max_wait=5
9495  wait=0
9596  if  [[ " ${VIDEO_UPLOAD_ENABLED} " =  " true" &&  [[ -n  " ${UPLOAD_DESTINATION_PREFIX} " &&  [[ " ${VIDEO_INTERNAL_UPLOAD} " !=  " true" ;  then 
96-     while  [[ -f  ${FORCE_EXIT_FILE}  ]] &&  [[ ${wait}  -lt  ${max_wait }  ]];  do 
97+     while  [[ -f  ${FORCE_EXIT_FILE}  ]] &&  [[ ${wait}  -lt  ${wait_uploader_shutdown_max_attempts }  ]];  do 
9798      echo  " exit" >> ${UPLOAD_PIPE_FILE}  & 
9899      echo  " $( date -u +" ${ts_format} " ) ${process_name} ] - Waiting for force exit file to be consumed by external upload container" 
99-       sleep 1 
100+       sleep ${poll_interval} 
100101      wait=$(( wait +  1 )) 
101102    done 
102103  fi 
103104  if  [[ " ${VIDEO_UPLOAD_ENABLED} " =  " true" &&  [[ -n  " ${UPLOAD_DESTINATION_PREFIX} " &&  [[ " ${VIDEO_INTERNAL_UPLOAD} " =  " true" ;  then 
104105    while  [[ $( pgrep rclone |  wc -l) -gt  0 ]];  do 
105106      echo  " exit" >> ${UPLOAD_PIPE_FILE}  & 
106107      echo  " $( date -u +" ${ts_format} " ) ${process_name} ] - Recorder is waiting for RCLONE to finish" 
107-       sleep 1 
108+       sleep ${poll_interval} 
108109    done 
109110  fi 
110111}
@@ -134,7 +135,6 @@ function stop_ffmpeg() {
134135    if  !  pgrep -f ffmpeg > /dev/null;  then 
135136      break 
136137    fi 
137-     sleep ${poll_interval} 
138138  done 
139139}
140140
@@ -159,9 +159,27 @@ function check_if_ffmpeg_running() {
159159  return  1
160160}
161161
162+ function  wait_for_file_integrity()  {
163+   retry=0
164+   if  [[ !  -f  " ${video_file} " ;  then 
165+     echo  " $( date -u +" ${ts_format} " ) ${process_name} ] - Video file is not found, might be the recording is not started." 
166+     return  0
167+   fi 
168+   until  ffmpeg -v error -i " ${video_file} " ;  do 
169+     echo  " $( date -u +" ${ts_format} " ) ${process_name} ] - Waiting for video file ${video_file}  to be ready." 
170+     sleep ${poll_interval} 
171+     retry=$(( retry +  1 )) 
172+     if  [[ $retry  -ge  ${file_ready_max_attempts}  ]];  then 
173+       echo  " $( date -u +" ${ts_format} " ) ${process_name} ] - Video file is not ready after ${file_ready_max_attempts}  attempts, skipping..." 
174+       break 
175+     fi 
176+   done 
177+ }
178+ 
162179function  stop_if_recording_inprogress()  {
163180  if  [[ " $recording_started " =  " true" ||  check_if_ffmpeg_running;  then 
164181    stop_recording
182+     wait_for_file_integrity
165183  fi 
166184}
167185
@@ -176,6 +194,10 @@ function graceful_exit() {
176194  stop_if_recording_inprogress
177195  send_exit_signal_to_uploader
178196  wait_util_uploader_shutdown
197+ }
198+ 
199+ function  graceful_exit_force()  {
200+   graceful_exit
179201  kill  -SIGTERM " $( cat ${SE_SUPERVISORD_PID_FILE} ) " 2> /dev/null
180202  echo  " $( date -u +" ${ts_format} " ) ${process_name} ] - Ready to shutdown the recorder" 
181203  exit  0
@@ -184,13 +206,15 @@ function graceful_exit() {
184206if  [[ " ${VIDEO_UPLOAD_ENABLED} " !=  " true" &&  [[ " ${VIDEO_FILE_NAME} " !=  " auto" &&  [[ -n  " ${VIDEO_FILE_NAME} " ;  then 
185207  trap  graceful_exit SIGTERM SIGINT EXIT
186208  wait_for_display
209+   video_file=" $VIDEO_FOLDER /$VIDEO_FILE_NAME " 
187210  #  exec replaces the video.sh process with ffmpeg, this makes easier to pass the process termination signal
188211  ffmpeg -hide_banner -loglevel warning -flags low_delay -threads 2 -fflags nobuffer+genpts -strict experimental -y -f x11grab \
189-     -video_size ${VIDEO_SIZE}  -r ${FRAME_RATE}  -i ${DISPLAY}  -codec:v ${CODEC}  ${PRESET}  -pix_fmt yuv420p " $VIDEO_FOLDER  / $VIDEO_FILE_NAME " & 
212+     -video_size ${VIDEO_SIZE}  -r ${FRAME_RATE}  -i ${DISPLAY}  -codec:v ${CODEC}  ${PRESET}  -pix_fmt yuv420p " $video_file  " & 
190213  wait  $! 
214+   wait_for_file_integrity
191215
192216else 
193-   trap  graceful_exit  SIGTERM SIGINT EXIT
217+   trap  graceful_exit_force  SIGTERM SIGINT EXIT
194218  create_named_pipe
195219  wait_for_display
196220  recording_started=" false" 
224248      sleep ${poll_interval} 
225249    elif  [[ " $session_id " !=  " $prev_session_id " &&  " $recording_started " =  " true" ;  then 
226250      stop_recording
251+       wait_for_file_integrity
227252      if  [[ $max_recorded_count  -gt  0 ]] &&  [[ $recorded_count  -ge  $max_recorded_count  ]];  then 
228253        echo  " $( date -u +" ${ts_format} " ) ${process_name} ] - Node will be drained since max sessions reached count number ($max_recorded_count )" 
229254        exit 
0 commit comments