@@ -20,6 +20,7 @@ file_ready_max_attempts=${SE_VIDEO_FILE_READY_WAIT_ATTEMPTS:-5}
2020wait_uploader_shutdown_max_attempts=${SE_VIDEO_WAIT_UPLOADER_SHUTDOWN_ATTEMPTS:- 5}
2121ts_format=${SE_LOG_TIMESTAMP_FORMAT:- " %Y-%m-%d %H:%M:%S,%3N" }
2222process_name=" video.recorder"
23+ tmux_session_name=" video_recorder"
2324
2425if [ " ${SE_VIDEO_RECORD_STANDALONE} " = " true" ]; then
2526 JQ_SESSION_ID_QUERY=" .value.nodes[]?.slots[]?.session?.sessionId"
@@ -140,22 +141,28 @@ function exit_on_max_session_reach() {
140141 fi
141142}
142143
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 1
159+ done
160+ echo " Session '$session ' ended."
154161 done
155162}
156163
157164function stop_recording() {
158- stop_ffmpeg
165+ stop_all_recording_and_wait
159166 echo " $( date -u +" ${ts_format} " ) [${process_name} ] - Video recording stopped"
160167 recorded_count=$(( recorded_count + 1 ))
161168 recording_started=" false"
@@ -166,12 +173,11 @@ function stop_recording() {
166173 elif [[ " ${VIDEO_UPLOAD_ENABLED} " = " true" ]] && [[ -z " ${UPLOAD_DESTINATION_PREFIX} " ]]; then
167174 echo " $( date -u +" ${ts_format} " ) [${process_name} ] - Upload destination not known since UPLOAD_DESTINATION_PREFIX is not set. Continue without uploading."
168175 fi
176+ echo " $( date -u +" ${ts_format} " ) [${process_name} ] - Waiting for a while to record next coming session if any"
169177}
170178
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
175181 return 1
176182}
177183
@@ -193,7 +199,7 @@ function wait_for_file_integrity() {
193199}
194200
195201function stop_if_recording_inprogress() {
196- if [[ " $recording_started " = " true" ]] || check_if_ffmpeg_running ; then
202+ if [[ " $recording_started " = " true" ]] || check_if_any_session_running ; then
197203 stop_recording
198204 fi
199205}
@@ -206,7 +212,7 @@ function log_node_response() {
206212
207213function graceful_exit() {
208214 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
210216 send_exit_signal_to_uploader
211217 wait_util_uploader_shutdown
212218}
@@ -218,6 +224,12 @@ function graceful_exit_force() {
218224 exit 0
219225}
220226
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+
221233if [ " ${SE_RECORD_AUDIO,,} " = " true" ]; then
222234 echo " $( date -u +" ${ts_format} " ) [${process_name} ] - Audio source arguments: ${SE_AUDIO_SOURCE} "
223235else
@@ -229,12 +241,11 @@ if [[ "${VIDEO_UPLOAD_ENABLED}" != "true" ]] && [[ "${VIDEO_FILE_NAME}" != "auto
229241 wait_for_display
230242 video_file=" $VIDEO_FOLDER /$VIDEO_FILE_NAME "
231243 # 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
238249
239250else
240251 trap graceful_exit_force SIGTERM SIGINT EXIT
@@ -263,10 +274,9 @@ else
263274 log_node_response
264275 video_file=" ${VIDEO_FOLDER} /$video_file_name "
265276 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
270280 recording_started=" true"
271281 prev_session_id=$session_id
272282 fi
0 commit comments