Skip to content

Commit

Permalink
Add session-task script for video recording
Browse files Browse the repository at this point in the history
  • Loading branch information
VietND96 committed Oct 23, 2023
1 parent 31ef618 commit 72ae9bd
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 5 deletions.
7 changes: 4 additions & 3 deletions Video/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,13 @@ RUN python3 -m pip install --no-cache --upgrade --no-cache-dir pip urllib3 setup
#======================================
# Add Supervisor configuration files
#======================================
ENV SE_VIDEO_FOLDER /videos
ENV SE_SESSION_FOLDER /sessions
RUN mkdir -p /opt/bin/
RUN mkdir -p /var/run/supervisor /var/log/supervisor /videos
RUN mkdir -p /var/run/supervisor /var/log/supervisor ${SE_VIDEO_FOLDER} ${SE_SESSION_FOLDER}

COPY supervisord.conf /etc
COPY entry_point.sh video.sh video_ready.py /opt/bin/
COPY entry_point.sh video.sh session_task.sh video_ready.py /opt/bin/

RUN chmod +x /opt/bin/*
ENTRYPOINT ["/opt/bin/entry_point.sh"]
Expand All @@ -35,7 +37,6 @@ ENV SE_FRAME_RATE 15
ENV SE_CODEC libx264
ENV SE_PRESET "-preset ultrafast"
ENV FILE_NAME video.mp4
ENV SE_VIDEO_FOLDER /videos
ENV SE_VIDEO_RECORD true
ENV SE_NODE_PORT 5555
ENV DRAIN_AFTER_SESSION_COUNT 0
Expand Down
46 changes: 46 additions & 0 deletions Video/session_task.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#!/usr/bin/env bash

max_recorded_count=$((${DRAIN_AFTER_SESSION_COUNT:-SE_DRAIN_AFTER_SESSION_COUNT}))
recorded_count=0
last_session_id=""

function get_session_id {
session_id=$(curl -s --request GET 'http://'${DISPLAY_CONTAINER_NAME}':'${SE_NODE_PORT}'/status' | jq -r '.[]?.node?.slots | .[0]?.session?.sessionId')
}

function capture_session() {
echo "${POD_NAME}" > ${SE_SESSION_FOLDER}'/'$session_id'.txt'
cat ${SE_SESSION_FOLDER}'/'$session_id'.txt'
last_session_id=$session_id
recorded_count=$((recorded_count+1))
}

function terminate_gracefully {
echo "Trapped SIGTERM/SIGINT/x so shutting down the listener..."
while true;
do
get_session_id
if [ "$session_id" != "null" -a "$session_id" != "" ];
then
echo "Session: $session_id is active, waiting for it to finish"
sleep 2
else
break
fi
done
echo "Shutdown completed!"
exit 0
}

trap terminate_gracefully SIGTERM SIGINT

while true;
do
get_session_id
if [ "$session_id" != "null" -a "$session_id" != "" ] && [ "$last_session_id" != "$session_id" ];
then
capture_session
else
sleep 2
fi
done
12 changes: 12 additions & 0 deletions Video/supervisord.conf
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,15 @@ stopsignal=TERM
redirect_stderr=true
stdout_logfile=/dev/stdout
stdout_logfile_maxbytes=0

[program:session-task]
priority=1
command=bash -c "/opt/bin/session_task.sh; EXIT_CODE=$?; kill -s SIGTERM `cat /var/run/supervisor/supervisord.pid`; exit $EXIT_CODE"
autostart=true
autorestart=unexpected
stopsignal=TERM

;Logs (all activity redirected to stdout so it can be seen through "docker logs"
redirect_stderr=true
stdout_logfile=/dev/stdout
stdout_logfile_maxbytes=0
10 changes: 8 additions & 2 deletions Video/video.sh
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@ function stop_recording {
recorded_count=$((recorded_count+1))
}

function stop_dependencies() {
pkill -15 -f "video_ready.py"
pkill -15 -f "session_task.sh"
}

function terminate_gracefully {
echo "Trapped SIGTERM/SIGINT/x so shutting down video-recording..."
while true;
Expand All @@ -36,11 +41,11 @@ function terminate_gracefully {
echo "Session: $session_id is active, waiting for it to finish"
sleep 1
else
pkill --signal TERM -f "ffmpeg"
pkill -15 -f "ffmpeg"
break
fi
done
pkill --signal TERM -f "video_ready.py"
stop_dependencies
echo "Shutdown completed!"
}

Expand Down Expand Up @@ -84,6 +89,7 @@ then
if [ $max_recorded_count -gt 0 ] && [ $recorded_count -ge $max_recorded_count ];
then
echo "Max recorded count reached, exiting"
stop_dependencies
exit 0
fi
elif [ $recording_started = "true" ];
Expand Down

0 comments on commit 72ae9bd

Please sign in to comment.