@@ -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:- 5}
20+ wait_uploader_shutdown_max_attempts=${SE_VIDEO_WAIT_UPLOADER_SHUTDOWN_ATTEMPTS:- 5}
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}
@@ -159,9 +160,27 @@ function check_if_ffmpeg_running() {
159160 return 1
160161}
161162
163+ function wait_for_file_integrity() {
164+ retry=0
165+ if [[ ! -f " ${video_file} " ]]; then
166+ echo " $( date -u +" ${ts_format} " ) [${process_name} ] - Video file is not found, might be the recording is not started."
167+ return 0
168+ fi
169+ until ffmpeg -v error -i " ${video_file} " -f null -; do
170+ echo " $( date -u +" ${ts_format} " ) [${process_name} ] - Waiting for video file ${video_file} to be ready."
171+ sleep ${poll_interval}
172+ retry=$(( retry + 1 ))
173+ if [[ $retry -ge ${file_ready_max_attempts} ]]; then
174+ echo " $( date -u +" ${ts_format} " ) [${process_name} ] - Video file is not ready after ${file_ready_max_attempts} attempts, skipping..."
175+ break
176+ fi
177+ done
178+ }
179+
162180function stop_if_recording_inprogress() {
163181 if [[ " $recording_started " = " true" ]] || check_if_ffmpeg_running; then
164182 stop_recording
183+ wait_for_file_integrity
165184 fi
166185}
167186
@@ -176,6 +195,10 @@ function graceful_exit() {
176195 stop_if_recording_inprogress
177196 send_exit_signal_to_uploader
178197 wait_util_uploader_shutdown
198+ }
199+
200+ function graceful_exit_force() {
201+ graceful_exit
179202 kill -SIGTERM " $( cat ${SE_SUPERVISORD_PID_FILE} ) " 2> /dev/null
180203 echo " $( date -u +" ${ts_format} " ) [${process_name} ] - Ready to shutdown the recorder"
181204 exit 0
@@ -184,13 +207,15 @@ function graceful_exit() {
184207if [[ " ${VIDEO_UPLOAD_ENABLED} " != " true" ]] && [[ " ${VIDEO_FILE_NAME} " != " auto" ]] && [[ -n " ${VIDEO_FILE_NAME} " ]]; then
185208 trap graceful_exit SIGTERM SIGINT EXIT
186209 wait_for_display
210+ video_file=" $VIDEO_FOLDER /$VIDEO_FILE_NAME "
187211 # exec replaces the video.sh process with ffmpeg, this makes easier to pass the process termination signal
188212 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 " &
213+ -video_size ${VIDEO_SIZE} -r ${FRAME_RATE} -i ${DISPLAY} -codec:v ${CODEC} ${PRESET} -pix_fmt yuv420p " $video_file " &
190214 wait $!
215+ wait_for_file_integrity
191216
192217else
193- trap graceful_exit SIGTERM SIGINT EXIT
218+ trap graceful_exit_force SIGTERM SIGINT EXIT
194219 create_named_pipe
195220 wait_for_display
196221 recording_started=" false"
224249 sleep ${poll_interval}
225250 elif [[ " $session_id " != " $prev_session_id " && " $recording_started " = " true" ]]; then
226251 stop_recording
252+ wait_for_file_integrity
227253 if [[ $max_recorded_count -gt 0 ]] && [[ $recorded_count -ge $max_recorded_count ]]; then
228254 echo " $( date -u +" ${ts_format} " ) [${process_name} ] - Node will be drained since max sessions reached count number ($max_recorded_count )"
229255 exit
0 commit comments