AlexxIT / go2rtc

Ultimate camera streaming application with support RTSP, RTMP, HTTP-FLV, WebRTC, MSE, HLS, MP4, MJPEG, HomeKit, FFmpeg, etc.
https://github.com/AlexxIT/Blog
MIT License
3.76k stars 304 forks source link

Raspberry Pi 5 exec changes #881

Open glossyio opened 5 months ago

glossyio commented 5 months ago

The documentation to get the RPi 5 working with the exec stream needs to be updated: https://github.com/AlexxIT/go2rtc#source-exec

Currently reads

streams:
  picam_h264: exec:libcamera-vid -t 0 --inline -o -
  picam_mjpeg: exec:libcamera-vid -t 0 --codec mjpeg -o -

The RPi5 needs to have the libav codec specified, see explanation below.

streams:
  picam_h264: exec:rpicam-vid -t 0 --inline --libav-format h264 -o -
  picam_mjpeg: exec:rpicam-vid -t 0 --codec mjpeg --libav-format mpegts -o -

Explanation, per RPi engineer at https://forums.raspberrypi.com/viewtopic.php?t=360020#p2180108

The libav codec (used on a Pi 5) tries to deduce the container/output format (different from the encoding format) based on the file name. Since you are specifying a network address as a filename, it cannot do this, so you must manually provide the output format through the --libav-format command line option.

In addition, the RPi-native libcamera-vid is being renamed rpicam-vid, per https://forums.raspberrypi.com/viewtopic.php?t=359569&sid=fee8e58134bceb78f9f5581094233a27

AlexxIT commented 1 month ago

Are you sure about mjpeg version? It's very strange to use it with mpegts.

glossyio commented 1 month ago

Are you sure about mjpeg version? It's very strange to use it with mpegts.

Thanks for looking at this. I haven't used mjpeg only the h264, so I cannot confirm. I included it because that is what the RPi engineer said to try in their forum comment.

AlexxIT commented 1 month ago

I haven't seen example with mjpeg+mpegts.

rici44 commented 3 weeks ago

Using go2rtc in docker compose container I am not able to use rpicam stream. Is Source: Exec usable inside docker compose container?

glossyio commented 3 weeks ago

Using go2rtc in docker compose container I am not able to use rpicam stream. Is Source: Exec usable inside docker compose container?

No, rpicam is not available in a Docker container unless you install the library and all dependencies in the container and then pass through the camera as a device. I found it easier to install/configure go2rtc on the host machine and call the rtsp stream in the container.

rici44 commented 3 weeks ago

Using go2rtc in docker compose container I am not able to use rpicam stream. Is Source: Exec usable inside docker compose container?

No, rpicam is not available in a Docker container unless you install the library and all dependencies in the container and then pass through the camera as a device. I found it easier to install/configure go2rtc on the host machine and call the rtsp stream in the container.

And how do you install and configure go2rtc and if you install what container do you call rtsp stream from?

glossyio commented 3 weeks ago

And how do you install and configure go2rtc and if you install what container do you call rtsp stream from?

Here's how I use the Source Exec method (on the host RPi) and consuming it in the Docker containers.

Using Raspberry Pi OS bookworm (Full Install, since Lite does not have h.264 codecs), I install go2rtc :

sudo mkdir /var/lib/go2rtc
sudo curl -L https://github.com/AlexxIT/go2rtc/releases/download/v1.8.5/go2rtc_linux_arm64 -o /var/lib/go2rtc/go2rtc_linux_arm64
sudo chmod 755 /var/lib/go2rtc/go2rtc_linux_arm64
sudo chmod +x /var/lib/go2rtc/go2rtc_linux_arm64

I set it up to run as a systemctl service, so it always runs and restarts:

echo \
  "# /etc/systemd/system/go2rtc_server.service
# to run: sudo systemctl stop/start/restart/enable/disable go2rtc_server

[Unit]
Description=go2rtc
After=network.target rc-local.service

[Service]
Restart=always
WorkingDirectory=/var/lib/go2rtc/
ExecStart=/var/lib/go2rtc/go2rtc_linux_arm64

[Install]
WantedBy=multi-user.target" | \
  sudo tee /etc/systemd/system/go2rtc_server.service > /dev/null
sudo chmod 644 /etc/systemd/system/go2rtc_server.service

And write the go2rtc config as above to /var/lib/go2rtc/go2rtc.yaml my settings look like:

"# /var/lib/go2rtc/go2rtc.yaml
streams:
  picam_h264: exec:rpicam-vid --camera 0 --mode 2304:1296 --framerate 15 --exposure sport --hdr --timeout 0 --nopreview --codec h264 --libav-video-codec h264 --libav-format h264 --inline -o -

Start it up with

sudo systemctl start go2rtc_server
sudo systemctl enable go2rtc_server

Check that it worked by accessing go2rtc's web interface at http://<HOSTIPADDRESS>:1984/

You can then use it from any Docker container by calling the host either by hostname or IP address. E.g. I call it in Frigate by setting the configuration for cameras > ffmpeg > inputs > path to rtsp://<HOSTNAME>:8554/picam_h264

rici44 commented 4 days ago

thx @glossyio that works well