mpromonet / webrtc-streamer

WebRTC streamer for V4L2 capture devices, RTSP sources and Screen Capture
https://webrtcstreamer.agreeabletree-365b9a90.canadacentral.azurecontainerapps.io/?layout=2x2
The Unlicense
3.02k stars 608 forks source link

Question: Best way to run as a background process #421

Open AllonisDave opened 3 years ago

AllonisDave commented 3 years ago

What is the correct way to run webrtc-streamer at boot/login and have it run in the background? PI4 and webrtc-streamer 0.6.4

I have tried creating a service like this.

/lib/systemd/system/livepreview.service

[Unit]
Description=LivePreview
After=multi-user.target

[Service]
Type=simple
User=pi
WorkingDirectory=/usr/local/Allonis/streamer
ExecStart=/usr/local/Allonis/streamer/webrtc-streamer
KillMode=process
Restart=on-failure

[Install]
WantedBy=multi-user.target

When the service starts at boot video streaming works but audio streaming is NOT working. Here is the status from the service starting. PulseAudio is complaining.

● livepreview.service - LivePreview Loaded: loaded (/lib/systemd/system/livepreview.service; disabled; vendor preset: enabled) Active: active (running) since Thu 2021-06-03 16:08:55 PDT; 5s ago Main PID: 2643 (webrtc-streamer) Tasks: 55 (limit: 4915) CGroup: /system.slice/livepreview.service └─2643 /usr/local/Allonis/streamer/webrtc-streamer

Jun 03 16:08:55 myserver systemd[1]: Started LivePreview. Jun 03 16:08:55 myserver webrtc-streamer[2643]: Version:v0.6.4/Linux-armv7l civetweb@v1.13 webrtc@376cf07ea2-dirty live555helper@d351689 Jun 03 16:08:55 myserver webrtc-streamer[2643]: nullLogger level:3 Jun 03 16:08:55 myserver webrtc-streamer[2643]: [000:000][2643] (audio_device_pulse_linux.cc:1605): failed to connect context, error=-1 Jun 03 16:08:55 myserver webrtc-streamer[2643]: [000:000][2643] (audio_device_pulse_linux.cc:145): failed to initialize PulseAudio Jun 03 16:08:55 myserver webrtc-streamer[2643]: [000:001][2643] (audio_device_impl.cc:341): Audio device initialization failed. Jun 03 16:08:55 myserver webrtc-streamer[2643]: USB Video: USB Video=>audiocap://2 Jun 03 16:08:55 myserver webrtc-streamer[2643]: HTTP Listen at 0.0.0.0:8000

I also tried to start it in a script at login using this script /etc/init.d/streamer.sh

#! /bin/sh
### BEGIN INIT INFO
# Provides: webrtc-streamer
# Required-Start: $syslog
# Required-Stop: $syslog
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: webrtc-streamer
# Description:
### END INIT INFO

case "$1" in
    start)
        echo "streamer is starting"
        # Starting Program
        cd /usr/local/Allonis/streamer
        ./webrtc-streamer >streamer.log 2>&1 &
        ;;
    stop)
        echo "streamer is ending"
        # Ending Program
        killall webrtc-streamer
        ;;
    *)
        echo "Use: /etc/init.d/streamer {start|stop}"
        exit 1
        ;;
esac

exit 0

When this script runs at boot webrtc-streamer video will stream but again, no audio. The contents of the generated log is attached. streamer.log

also http://192.168.0.235:8000/api/getAudioDeviceList reports []

http://192.168.0.235:8000/api/getVideoDeviceList reports [ "USB Video: USB Video", "bcm2835-isp", "videocap://2" ]

Thoughts?

Best Dave...

AllonisDave commented 3 years ago

Here is my solution for running webrtc-streamer as a service on the PI. Video and Audio working now.

/lib/systemd/system/livepreview.service is

[Unit]
Description=LivePreview
After=graphical.target

[Service]
Environment=DISPLAY=:0
Environment=XAUTHORITY=/home/pi/.Xauthority
ExecStart=/usr/local/Allonis/streamer/webrtc-streamer
Restart=always
RestartSec=10s
KillMode=process
TimeoutSec=infinity

[Install]
WantedBy=graphical.target