-
Notifications
You must be signed in to change notification settings - Fork 4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add webcam streaming #34
Conversation
Per internal communication, webcams are only available for access when the Virtual Machine's USB Controller is xHCI (USB 3.0) instead of the default OHCI (USB 2.0). I have set the development VM's USB Controller to xHCI. Great find! |
image-toolkit/sbin/startup.sh
Outdated
}" & | ||
CLVC_PID=$! | ||
# Monitor the process and device | ||
while [[ -e "/dev/video$DEVICE_NO" ]] && ps -p $CLVC_PID > /dev/null; do |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You should monitor /dev/video with inotifywait (for example, inotifywait -e delete_self /dev/video0
will exit when /dev/video0 is disconnected) and wait -n <pidlist>
(wait for any in to finish).
Monitoring /dev/video0 by repeatedly polling could miss quick reconnection events (for example, a contestant accidentally touching the webcam's wire).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for your suggestion. I will update the code accordingly.
We will use udevadm (from udev package already installed in Ubuntu 22.04) to listen for webcam-related events. |
Using fixed keyframe interval significantly reduce stream latency and reduce *.ts fragment file size.
image-toolkit/sbin/startup.sh
Outdated
udevadm monitor --udev -s video4linux | while read -r line; do | ||
if [[ "$line" == *"remove"*"/video4linux/video$DEVICE_NO"* ]] ; then | ||
echo "Device unplugged, proceed to kill cvlc" | ||
kill $CLVC_PID 2>/dev/null || : |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This logic is kinda complicated, and if VLC catch SIGTERM and fails to exit (like during startup), we'll be in a deadlock.
Let's make the udevadm process exit on device disconnect, then use wait -n
to wait for the first process among the two to exit. If udevadm
exits, we kill vlc
. Exit udevadm also clears the event queue, so that consecutive connect-disconnect will not make us restart vlc more time than is necessary.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also, for good measure, kill vlc with SIGKILL (kill -9
) if it's not killed after a while (wait with something like timeout wait $CVLC_PID
). Also, starting udevadm
before vlc
would be safer.
…vice remove event
…ead of status change
8c834a1
to
395f700
Compare
…using exit on error
image-toolkit/sbin/startup.sh
Outdated
@@ -53,6 +53,49 @@ vlc_restart_loop() { | |||
done | |||
} | |||
|
|||
webcam_stream_loop() { | |||
VIDEO_DEVICE_NO=0 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Due to obsproject/obs-studio#3003 (webcam numbering may differ after reconnection), use devices in /dev/v4l/by-id
Increases robustness of webcam_stream_loop
For audio streaming, we will have a configurable For ease of configuration, the |
c28e72e
to
de0ec14
Compare
de0ec14
to
288d557
Compare
31d4e58
to
608f8be
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
Use vlc to stream from /dev/video0 to rtmp://localhost/live/webcam and use ffmpeg-webcam.service to save local chunks.