@@ -16,6 +16,8 @@ UPLOAD_PIPE_FILE_NAME=${SE_UPLOAD_PIPE_FILE_NAME:-"uploadpipe"}
16
16
SE_SERVER_PROTOCOL=${SE_SERVER_PROTOCOL:- " http" }
17
17
poll_interval=${SE_VIDEO_POLL_INTERVAL:- 1}
18
18
max_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}
19
21
ts_format=${SE_LOG_TIMESTAMP_FORMAT:- " %Y-%m-%d %H:%M:%S,%3N" }
20
22
process_name=" video.recorder"
21
23
@@ -90,21 +92,20 @@ function wait_for_api_respond() {
90
92
}
91
93
92
94
function wait_util_uploader_shutdown() {
93
- max_wait=5
94
95
wait=0
95
96
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
97
98
echo " exit" >> ${UPLOAD_PIPE_FILE} &
98
99
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}
100
101
wait=$(( wait + 1 ))
101
102
done
102
103
fi
103
104
if [[ " ${VIDEO_UPLOAD_ENABLED} " = " true" ]] && [[ -n " ${UPLOAD_DESTINATION_PREFIX} " ]] && [[ " ${VIDEO_INTERNAL_UPLOAD} " = " true" ]]; then
104
105
while [[ $( pgrep rclone | wc -l) -gt 0 ]]; do
105
106
echo " exit" >> ${UPLOAD_PIPE_FILE} &
106
107
echo " $( date -u +" ${ts_format} " ) [${process_name} ] - Recorder is waiting for RCLONE to finish"
107
- sleep 1
108
+ sleep ${poll_interval}
108
109
done
109
110
fi
110
111
}
@@ -159,9 +160,27 @@ function check_if_ffmpeg_running() {
159
160
return 1
160
161
}
161
162
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
+
162
180
function stop_if_recording_inprogress() {
163
181
if [[ " $recording_started " = " true" ]] || check_if_ffmpeg_running; then
164
182
stop_recording
183
+ wait_for_file_integrity
165
184
fi
166
185
}
167
186
@@ -176,6 +195,10 @@ function graceful_exit() {
176
195
stop_if_recording_inprogress
177
196
send_exit_signal_to_uploader
178
197
wait_util_uploader_shutdown
198
+ }
199
+
200
+ function graceful_exit_force() {
201
+ graceful_exit
179
202
kill -SIGTERM " $( cat ${SE_SUPERVISORD_PID_FILE} ) " 2> /dev/null
180
203
echo " $( date -u +" ${ts_format} " ) [${process_name} ] - Ready to shutdown the recorder"
181
204
exit 0
@@ -184,13 +207,15 @@ function graceful_exit() {
184
207
if [[ " ${VIDEO_UPLOAD_ENABLED} " != " true" ]] && [[ " ${VIDEO_FILE_NAME} " != " auto" ]] && [[ -n " ${VIDEO_FILE_NAME} " ]]; then
185
208
trap graceful_exit SIGTERM SIGINT EXIT
186
209
wait_for_display
210
+ video_file=" $VIDEO_FOLDER /$VIDEO_FILE_NAME "
187
211
# exec replaces the video.sh process with ffmpeg, this makes easier to pass the process termination signal
188
212
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 " &
190
214
wait $!
215
+ wait_for_file_integrity
191
216
192
217
else
193
- trap graceful_exit SIGTERM SIGINT EXIT
218
+ trap graceful_exit_force SIGTERM SIGINT EXIT
194
219
create_named_pipe
195
220
wait_for_display
196
221
recording_started=" false"
224
249
sleep ${poll_interval}
225
250
elif [[ " $session_id " != " $prev_session_id " && " $recording_started " = " true" ]]; then
226
251
stop_recording
252
+ wait_for_file_integrity
227
253
if [[ $max_recorded_count -gt 0 ]] && [[ $recorded_count -ge $max_recorded_count ]]; then
228
254
echo " $( date -u +" ${ts_format} " ) [${process_name} ] - Node will be drained since max sessions reached count number ($max_recorded_count )"
229
255
exit
0 commit comments