@@ -20,6 +20,7 @@ file_ready_max_attempts=${SE_VIDEO_FILE_READY_WAIT_ATTEMPTS:-5}
20
20
wait_uploader_shutdown_max_attempts=${SE_VIDEO_WAIT_UPLOADER_SHUTDOWN_ATTEMPTS:- 5}
21
21
ts_format=${SE_LOG_TIMESTAMP_FORMAT:- " %Y-%m-%d %H:%M:%S,%3N" }
22
22
process_name=" video.recorder"
23
+ tmux_session_name=" video_recorder"
23
24
24
25
if [ " ${SE_VIDEO_RECORD_STANDALONE} " = " true" ]; then
25
26
JQ_SESSION_ID_QUERY=" .value.nodes[]?.slots[]?.session?.sessionId"
@@ -140,22 +141,28 @@ function exit_on_max_session_reach() {
140
141
fi
141
142
}
142
143
143
- function stop_ffmpeg() {
144
- while true ; do
145
- FFMPEG_PID=$( pgrep -f ffmpeg | tr ' \n' ' ' )
146
- if [ -n " $FFMPEG_PID " ]; then
147
- kill -SIGTERM $FFMPEG_PID
148
- wait $FFMPEG_PID
149
- fi
150
- if ! pgrep -f ffmpeg > /dev/null; then
151
- break
152
- fi
153
- sleep ${poll_interval}
144
+ function stop_all_recording() {
145
+ current_record_sessions=" $( tmux list-sessions -F " #{session_name}" ) "
146
+ echo " $( date -u +" ${ts_format} " ) [${process_name} ] - Current recording session(s): $current_record_sessions "
147
+ for session in $current_record_sessions ; do
148
+ echo " Sending 'q' (quit signal) to session: $session "
149
+ tmux send-keys -t " $session " ' q'
150
+ done
151
+ }
152
+
153
+ function stop_all_recording_and_wait() {
154
+ stop_all_recording
155
+ for session in $current_record_sessions ; do
156
+ echo " Waiting for session '$session ' to finish..."
157
+ while tmux has-session -t " $session " 2> /dev/null; do
158
+ sleep 2
159
+ done
160
+ echo " Session '$session ' ended."
154
161
done
155
162
}
156
163
157
164
function stop_recording() {
158
- stop_ffmpeg
165
+ stop_all_recording_and_wait
159
166
echo " $( date -u +" ${ts_format} " ) [${process_name} ] - Video recording stopped"
160
167
recorded_count=$(( recorded_count + 1 ))
161
168
recording_started=" false"
@@ -166,12 +173,11 @@ function stop_recording() {
166
173
elif [[ " ${VIDEO_UPLOAD_ENABLED} " = " true" ]] && [[ -z " ${UPLOAD_DESTINATION_PREFIX} " ]]; then
167
174
echo " $( date -u +" ${ts_format} " ) [${process_name} ] - Upload destination not known since UPLOAD_DESTINATION_PREFIX is not set. Continue without uploading."
168
175
fi
176
+ echo " $( date -u +" ${ts_format} " ) [${process_name} ] - Waiting for a while to record next coming session if any"
169
177
}
170
178
171
- function check_if_ffmpeg_running() {
172
- if pgrep -f ffmpeg > /dev/null; then
173
- return 0
174
- fi
179
+ function check_if_any_session_running() {
180
+ tmux list-sessions -F " #{session_name}" 2> /dev/null | grep -q . && return 0
175
181
return 1
176
182
}
177
183
@@ -193,8 +199,8 @@ function wait_for_file_integrity() {
193
199
}
194
200
195
201
function stop_if_recording_inprogress() {
196
- if [[ " $recording_started " = " true" ]] || check_if_ffmpeg_running ; then
197
- stop_recording
202
+ if [[ " $recording_started " = " true" ]] || check_if_any_session_running ; then
203
+ stop_recording &
198
204
fi
199
205
}
200
206
@@ -206,7 +212,7 @@ function log_node_response() {
206
212
207
213
function graceful_exit() {
208
214
echo " $( date -u +" ${ts_format} " ) [${process_name} ] - Trapped SIGTERM/SIGINT/x so shutting down recorder"
209
- stop_if_recording_inprogress
215
+ stop_all_recording_and_wait
210
216
send_exit_signal_to_uploader
211
217
wait_util_uploader_shutdown
212
218
}
@@ -218,6 +224,12 @@ function graceful_exit_force() {
218
224
exit 0
219
225
}
220
226
227
+ function get_tmux_session_unique_index() {
228
+ tmux_index=$( tmux list-sessions 2> /dev/null | grep -o " ${tmux_session_name} :[0-9]*" | cut -d: -f2)
229
+ tmux_index=${tmux_index:- 0}
230
+ tmux_index=$(( tmux_index + 1 ))
231
+ }
232
+
221
233
if [ " ${SE_RECORD_AUDIO,,} " = " true" ]; then
222
234
echo " $( date -u +" ${ts_format} " ) [${process_name} ] - Audio source arguments: ${SE_AUDIO_SOURCE} "
223
235
else
@@ -229,12 +241,11 @@ if [[ "${VIDEO_UPLOAD_ENABLED}" != "true" ]] && [[ "${VIDEO_FILE_NAME}" != "auto
229
241
wait_for_display
230
242
video_file=" $VIDEO_FOLDER /$VIDEO_FILE_NAME "
231
243
# exec replaces the video.sh process with ffmpeg, this makes easier to pass the process termination signal
232
- ffmpeg -hide_banner -loglevel warning -flags low_delay -threads 2 -fflags nobuffer+genpts -strict experimental -y -f x11grab \
233
- -video_size ${VIDEO_SIZE} -r ${FRAME_RATE} -i ${DISPLAY} ${SE_AUDIO_SOURCE} -codec:v ${CODEC} ${PRESET} -pix_fmt yuv420p " $video_file " &
234
- FFMPEG_PID=$!
235
- if ps -p $FFMPEG_PID > /dev/null; then
236
- wait $FFMPEG_PID
237
- fi
244
+ get_tmux_session_unique_index
245
+ tmux new-session -d -s ${tmux_session_name}${tmux_index} " ffmpeg -hide_banner -loglevel warning -flags low_delay -threads 2 -fflags nobuffer+genpts -strict experimental -y -f x11grab -video_size ${VIDEO_SIZE} -r ${FRAME_RATE} -i ${DISPLAY} ${SE_AUDIO_SOURCE} -codec:v ${CODEC} ${PRESET} -pix_fmt yuv420p \" $video_file \" "
246
+ while tmux has-session -t " ${tmux_session_name}${tmux_index} " 2> /dev/null; do
247
+ sleep 5
248
+ done
238
249
239
250
else
240
251
trap graceful_exit_force SIGTERM SIGINT EXIT
@@ -263,18 +274,17 @@ else
263
274
log_node_response
264
275
video_file=" ${VIDEO_FOLDER} /$video_file_name "
265
276
echo " $( date -u +" ${ts_format} " ) [${process_name} ] - Starting to record video"
266
- ffmpeg -hide_banner -loglevel warning -flags low_delay -threads 2 -fflags nobuffer+genpts -strict experimental -y -f x11grab \
267
- -video_size ${VIDEO_SIZE} -r ${FRAME_RATE} -i ${DISPLAY} ${SE_AUDIO_SOURCE} -codec:v ${CODEC} ${PRESET} -pix_fmt yuv420p " $video_file " &
268
- FFMPEG_PID=$!
269
- if ps -p $FFMPEG_PID > /dev/null; then
277
+ get_tmux_session_unique_index
278
+ tmux new-session -d -s ${tmux_session_name}${tmux_index} " ffmpeg -hide_banner -loglevel warning -flags low_delay -threads 2 -fflags nobuffer+genpts -strict experimental -y -f x11grab -video_size ${VIDEO_SIZE} -r ${FRAME_RATE} -i ${DISPLAY} ${SE_AUDIO_SOURCE} -codec:v ${CODEC} ${PRESET} -pix_fmt yuv420p \" $video_file \" "
279
+ if tmux has-session -t ${tmux_session_name}${tmux_index} 2> /dev/null; then
270
280
recording_started=" true"
271
281
prev_session_id=$session_id
272
282
fi
273
283
echo " $( date -u +" ${ts_format} " ) [${process_name} ] - Video recording started"
274
284
sleep ${poll_interval}
275
285
fi
276
286
elif [[ " $session_id " != " $prev_session_id " && " $recording_started " = " true" ]]; then
277
- stop_recording
287
+ stop_recording &
278
288
if [[ $max_recorded_count -gt 0 ]] && [[ $recorded_count -ge $max_recorded_count ]]; then
279
289
echo " $( date -u +" ${ts_format} " ) [${process_name} ] - Node will be drained since max sessions reached count number ($max_recorded_count )"
280
290
exit
0 commit comments