Can't view FFmpeg LAN RTSP Stream

I’m completely new to HA. I can set the live to be seen from the dashboard with this config:

show_state: true
show_name: true
camera_view: live
type: picture-entity
entity: camera.camera_ingresso
camera_image: camera.camera_ingresso
name: Ingresso

and it seems to be able to see the first frame only. Clicking the camera to see it bigger makes the live feed window load and then dissapear multiple times but sometimes I can briefly see “Error with media stream content”
Here’s everything I think is relevated:
I have a Home Assistant Supervised install on a Debian 12.
A Raspberry Pi 4B 4GB with ffmpeg creates a RTSP Stream from a USB Webcam and passes it to MediaMTX (previously known as rtsp-simple-server) and I can see and hear this stream through everything (VLC, MPC-HC and etc…) except my Home Assistant interface

This is the ffmpeg command that starts the stream from the Raspberry:

ffmpeg -f v4l2 -video_size 640x480 -r 50 -thread_queue_size 512 -i /dev/video0 -f pulse -sample_rate 16000 -channels 4 -thread_queue_size 512 -i default -b:v 2M -c:a aac -b:a 192k -ar 16000 -ac 2 -f rtsp rtsp://192.168.1.10:8554/stream.sdp

I’ve tried different codecs for both audio and video, disabling only audio or only video and setting -rtsp_transport to both tcp and udp (and changing configuration.yaml accordingly) after -f rtsp

My configuration.yaml:

ffmpeg:

camera:
  - platform: ffmpeg
    name: Camera_ingresso
    input: rtsp://192.168.1.10:8554/stream.sdp

This is all I get from the Home Assistant Core logs:

2023-08-15 19:44:24.473 ERROR (stream_worker) [homeassistant.components.stream.stream.camera.camera_ingresso] Error from stream worker: Stream ended; no additional packets

This could be irrelevant but this is what I get from MediaMTX logs when VLC watches the stream for 10 seconds:

Aug 15 20:04:57 raspberrypi mediamtx[15542]: 2023/08/15 20:04:57 INF [RTSP] [conn 192.168.1.103:9634] opened
Aug 15 20:04:57 raspberrypi mediamtx[15542]: 2023/08/15 20:04:57 INF [RTSP] [session 583327d0] created by 192.168.1.103:9634
Aug 15 20:04:57 raspberrypi mediamtx[15542]: 2023/08/15 20:04:57 INF [RTSP] [session 583327d0] is reading from path 'stream.sdp', with UDP, 2 tracks (MPEG-4 Video, MPEG-4 Audio)
Aug 15 20:05:09 raspberrypi mediamtx[15542]: 2023/08/15 20:05:09 INF [RTSP] [session 583327d0] destroyed (torn down by 192.168.1.103:9634)
Aug 15 20:05:09 raspberrypi mediamtx[15542]: 2023/08/15 20:05:09 INF [RTSP] [conn 192.168.1.103:9634] closed (EOF)

When I open the dashboard and then click the live

Aug 15 20:06:32 raspberrypi mediamtx[15542]: 2023/08/15 20:06:32 INF [RTSP] [conn 192.168.1.240:37688] opened
Aug 15 20:06:32 raspberrypi mediamtx[15542]: 2023/08/15 20:06:32 INF [RTSP] [session 49a3f0ac] created by 192.168.1.240:37688
Aug 15 20:06:32 raspberrypi mediamtx[15542]: 2023/08/15 20:06:32 INF [RTSP] [session 49a3f0ac] is reading from path 'stream.sdp', with UDP, 2 tracks (MPEG-4 Video, MPEG-4 Audio)
Aug 15 20:06:32 raspberrypi mediamtx[15542]: 2023/08/15 20:06:32 INF [RTSP] [session 49a3f0ac] destroyed (torn down by 192.168.1.240:37688)
Aug 15 20:06:32 raspberrypi mediamtx[15542]: 2023/08/15 20:06:32 INF [RTSP] [conn 192.168.1.240:37688] closed (EOF)
Aug 15 20:06:34 raspberrypi mediamtx[15542]: 2023/08/15 20:06:34 INF [RTSP] [conn 192.168.1.240:37702] opened
Aug 15 20:06:34 raspberrypi mediamtx[15542]: 2023/08/15 20:06:34 INF [RTSP] [session a842cc45] created by 192.168.1.240:37702
Aug 15 20:06:34 raspberrypi mediamtx[15542]: 2023/08/15 20:06:34 INF [RTSP] [session a842cc45] is reading from path 'stream.sdp', with UDP, 2 tracks (MPEG-4 Video, MPEG-4 Audio)
Aug 15 20:06:34 raspberrypi mediamtx[15542]: 2023/08/15 20:06:34 INF [RTSP] [session a842cc45] destroyed (torn down by 192.168.1.240:37702)
Aug 15 20:06:34 raspberrypi mediamtx[15542]: 2023/08/15 20:06:34 INF [RTSP] [conn 192.168.1.240:37702] closed (EOF)

I know I’ve included a lot but if someone notices something just tell me and I’ll look into it

Here’s the ffmpeg code in a way it can actually be read:

ffmpeg \
-f v4l2 -video_size 640x480 -r 50 -thread_queue_size 512 -i /dev/video0 \
-f pulse -sample_rate 16000 -channels 4 -thread_queue_size 512 -i default \
-b:v 2M \
-c:a aac -b:a 192k -ar 16000 -ac 2 \
-f rtsp rtsp://192.168.1.10:8554/stream.sdp

try to use “Generic Camera” integration, you should Not need to put anything in config.yaml

I didn’t mention but that’s how I first tried. It thinks it’s receiving the stream but it shows a default icon and asks if it’s okay. Doesn’t give a error code in itself or logs but if clicked while in the dashboard it does the same thing as I mentioned above
homeassistantpreviewgenericcamera

seems like caused by “wrong” configuration or url

I’ve checked the configuration, tried both UDP and TCP (by modifying the options on the ffmpeg command), tried both basic and digest (from the configuration of MediaMTX) I’ve tried a barebone
ffmpeg -i rtsp://192.168.1.10:8554/stream.sdp out.mp4 on every terminal I’ve got and it outputs the same file, working from everywhere except here, is there anyway to access the real ffmpeg output? I mean by running journalctl one of the home assistant containers?

Sorry i don’t run a Supervisor , how does you url looks like when viewing it in VLC ?
Have you tried to provide a stream over :554 (instead of 8554) ?
And do you have " default_config " in your configuration.yaml ? ( other vice you have to configured the Stream-integration your self also )

On VLC, MPC-HC and others I just use “rtsp://192.168.1.10:8554/stream.sdp”
8554 is the default listening port for MediaMTX but I’ll try changing it
Edit: Changing the port didn’t help

this is most likely the “generated image” provided over http and not the rstp-stream , so maybe HA have “problems” interpreting the .sdp or fail to communicate with MediaMTX or your Pi

If someone isn’t able to help me in the next few days I’ll try something else. I’ve seen some people using live555 for a RTSP Server but I’ve searched on their site and it seems I have to compile it myself if I want to use it on a Raspberry and I’m not able to do that
Thanks for putting your time into this by the way

There are another “well known” alternative supported by HA-Community, for streams , but basically the here mentioned integrations should work “out of the box” if it’s a proper stream “format”
How you made yours i don’t know, i would try something else than .sdp

Or use Webrtc/ go2rtc

1 Like

I didn’t find this discussion before. I’ve tried it and right now it can’t see the raw output of the stream directly, but by adding -c:v libx264 it’s now watchable in the dashboard. The problem now is that the raspberry seems not powerful enough for a 50fps stream but at least I can set it lower fps/quality for now. Thanks
This is the final ffmpeg command if anyone reads this in the future and is interested:

ffmpeg \
-f v4l2 -video_size 640x480 -r 50 -max_error_rate 1 -thread_queue_size 512 -i /dev/video0 \
-f pulse -sample_rate 16000 -channels 4 -thread_queue_size 512 -i default \
-c:v libx264 -r 50 -keyint_min 120 -g 120 -b:v 2M \
-c:a aac -b:a 192k -ar 16000 -ac 2 \
-f rtsp -rtsp_transport tcp rtsp://192.168.1.10:8554/stream.sdp
ffmpeg -f v4l2 -video_size 640x480 -r 50 -max_error_rate 1 -thread_queue_size 512 -i /dev/video0 -f pulse -sample_rate 16000 -channels 4 -thread_queue_size 512 -i default -c:v libx264 -r 50 -keyint_min 120 -g 120 -b:v 2M -c:a aac -b:a 192k -ar 16000 -ac 2 -f rtsp -rtsp_transport tcp rtsp://192.168.1.10:8554/stream.sdp

This is probably also way to much for HA, as well as i.e 4K resolution streams also “cost” in terms of CPU etc.
EDIT: Is that really coming from your USB-WebCam ? … you could easily reduce to 30fps without any noticeable effect

It’s a PS3 Eye Camera. I had a 30fps one but I like 60fps on a cam very much (it’s 50 for now as 60 triggered an error which I can only fix by rebuilding ffmpeg from scratch on the raspberry, at least according to a Raspberry Pi software engeneer in their forums).
MediaMTX has a way to start the stream when someone starts connecting so it shouldn’t be too much for the camera(overheating), raspberry(it does run other things too) or home assistant. 60fps 1080p 5M bitrate stream worked okay for a few minutes with ffmpeg (I tested by doing a stream of my windows pc with OBS to check if ffmpeg component in HA actually worked) so maybe it’s not too much for my hardware? I don’t think as it’s only 4gb of ram and an i3 4170

Yes i believe it’s a neat .sdp 's behavior , but depending upon how many cams you are setting up in HA, and in the dashboards, frame-rate and resolution makes a different ofcause
I had to reduce as i have 1 dashboard showing 6 live streams (720) and 1 dasboard with 2 (1024), but only using generic-camera and camera-integration
( not using Webrtc/ go2rtc/frigate(been thinking upon this setup, but again it’s more hardware/power consuming :slight_smile: ))

PS: 4 of the 6 cams basically monitoring static environment, like Well-pump room/ Pan-room(heating system) etc. so no need there for higher framerate.

1 Like