Skip to content
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

Merged
merged 39 commits into from
Jul 11, 2024
Merged

Add webcam streaming #34

merged 39 commits into from
Jul 11, 2024

Conversation

huytrinhm
Copy link
Contributor

Use vlc to stream from /dev/video0 to rtmp://localhost/live/webcam and use ffmpeg-webcam.service to save local chunks.

@minhnhatnoe minhnhatnoe self-requested a review June 3, 2024 11:10
@minhnhatnoe
Copy link
Contributor

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!

}" &
CLVC_PID=$!
# Monitor the process and device
while [[ -e "/dev/video$DEVICE_NO" ]] && ps -p $CLVC_PID > /dev/null; do
Copy link
Contributor

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).

Copy link
Contributor Author

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.

@huytrinhm
Copy link
Contributor Author

We will use udevadm (from udev package already installed in Ubuntu 22.04) to listen for webcam-related events.

minhnhatnoe and others added 3 commits June 9, 2024 23:06
Using fixed keyframe interval significantly reduce stream latency and reduce *.ts fragment file size.
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 || :
Copy link
Contributor

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.

Copy link
Contributor

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.

@@ -53,6 +53,49 @@ vlc_restart_loop() {
done
}

webcam_stream_loop() {
VIDEO_DEVICE_NO=0
Copy link
Contributor

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

@huytrinhm
Copy link
Contributor Author

For audio streaming, we will have a configurable AUDIO_DEVICE_NAME in config.sh. When webcam streaming starts, it will search for a hardware audio capture device (/proc/asound/card*/pcm*c) and select the first one whose name matches AUDIO_DEVICE_NAME. Options for the system default device and the first hardware device are also available via AUDIO_DEVICE_NAME=system and AUDIO_DEVICE_NAME=any, respectively.

For ease of configuration, the list_audio_devices and set_audio_device commands are also made available in the vnoiconf.sh script. The VLC instance responsible for webcam streaming is automatically restarted after set_audio_device is used.

@hung3a8 hung3a8 force-pushed the huytrinhm/webcam branch 2 times, most recently from c28e72e to de0ec14 Compare June 29, 2024 11:59
@hung3a8 hung3a8 force-pushed the huytrinhm/webcam branch from de0ec14 to 288d557 Compare June 29, 2024 12:02
@hung3a8 hung3a8 force-pushed the huytrinhm/webcam branch from 31d4e58 to 608f8be Compare June 30, 2024 13:15
Copy link
Contributor

@hung3a8 hung3a8 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@hung3a8 hung3a8 merged commit 7c84b51 into master Jul 11, 2024
1 check passed
@hung3a8 hung3a8 deleted the huytrinhm/webcam branch July 11, 2024 02:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants