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
4.14k stars 335 forks source link

Access multicast stream via RTSP #1265

Open esthar992 opened 1 month ago

esthar992 commented 1 month ago

Hi, is it possible to connect and view a multicast stream via RTSP to WEBRTC format in go2rtc?

I've tried without ffmpeg:

api:
  listen: :1984
  origin: "*"   
log:
  format: color
  level: debug
  output: stdout
  time: UNIXMS
rtsp:
  listen: :8554
  default_query: video
srtp:
  listen: :8443
webrtc:
  listen: :8555/tcp
streams:
  dahua: rtsp://user:pwd@192.xxx.xx.xx:554/cam/realmonitor?channel=1&subtype=0&unicast=false

Response (clicking on stream from the control panel / api ):

14:57:34,162    debug   [webrtc] add consumer error=streams: wrong response on SETUP
14:57:40,225    debug   [webrtc] new consumer src=dahua
14:57:40,303    info    [streams] can't get track error=wrong response on SETUP

and with ffmpeg:

streams:
   dauha:
      ffmpeg:rtsp://user:pwd@192.xxx.xx.xx:554/cam/realmonitor?channel=1&subtype=0&unicast=false#video=h264

Response:
[exec] [rtsp @ 0x7d7caa7e8600] method SETUP failed: 461 Unsupported transport

My cam is a dahua configured for multicasting returning h264 multicast stream.

If i set the rtsp endpoint with unicast=true all working like a charm.

Looking inside the code i see that there are controls about Transport header structure like :

(rtsp/server.go)

const transport = "RTP/AVP/TCP;unicast;interleaved="
            if strings.HasPrefix(tr, transport) {
                c.session = core.RandString(8, 10)
                c.state = StateSetup

                if c.mode == core.ModePassiveConsumer {
                    if i := reqTrackID(req); i >= 0 && i < len(c.Senders) {
                        // mark sender as SETUP
                        c.Senders[i].Media.ID = MethodSetup
                        tr = fmt.Sprintf("RTP/AVP/TCP;unicast;interleaved=%d-%d", i*2, i*2+1)
                        res.Header.Set("Transport", tr)
                    } else {
                        res.Status = "400 Bad Request"
                    }
                } else {
                    res.Header.Set("Transport", tr[:len(transport)+3])
                }
            } else {
                res.Status = "461 Unsupported transport"
            }

So it's impossible to do this or i'm doing something wrong?

Thanks in advance.

AlexxIT commented 1 month ago

You can use ffmpeg source for this. If you using docker - it should has network host setting.

esthar992 commented 1 month ago

Thanks @AlexxIT, can I ask you an example /link for accomplish this task?

I'm using it in a Docker container with network = host

esthar992 commented 1 month ago

Good morning, i'm triyng with FFMPEG in this way:

ffmepg_test: exec:ffmpeg -re -i rtsp://user:pwd@xxx.xxx.xx.xx:554/cam/realmonitor?channel=1&subtype=0&unicast=false -rtsp_transport tcp -f rtsp {output}

I'm receving errors in log below when i try to show the stream from go2rtc api panel:

08:31:55,739    info    go2rtc version=1.9.4 platform=linux/amd64 revision=a4885c2
08:31:55,739    debug   build version=go1.22.4 vcs.time=2024-06-18T18:33:36Z
08:31:55,739    info    config path=/config/go2rtc.yaml
08:31:55,740    info    [rtsp] listen addr=:8554
08:31:55,740    info    [api] listen addr=:1984
08:31:55,740    info    [webrtc] listen addr=:8555/tcp
08:31:57,843    debug   [exec] run rtsp args=ffmpeg,-re,-i,rtsp://user:pwd@xxx.xx.xx.xx:554/cam/realmonitor?channel=1&subtype=0&unicast=false,-rtsp_transport,tcp,-f,rtsp,rtsp://127.0.0.1:8554/75d07968b5506450d5c32dc251fcbac0
08:31:57,844    debug   [webrtc] new consumer src=ffmepg_test
08:31:57,870    debug   [exec] ffmpeg version 6.1.1 Copyright (c) 2000-2023 the FFmpeg developers
built with gcc 13.2.1 (Alpine 13.2.1_git20240309) 20240309
configuration: --prefix=/usr --disable-librtmp --disable-lzma --disable-static --disable-stripping --enable-avfilter --enable-gpl --enable-ladspa --enable-libaom --enable-libass --enable-libbluray --enable-libdav1d --enable-libdrm --enable-libfontconfig --enable-libfreetype --enable-libfribidi --enable-libharfbuzz --enable-libmp3lame --enable-libopenmpt --enable-libopus --enable-libplacebo --enable-libpulse --enable-librav1e --enable-librist --enable-libsoxr --enable-libsrt --enable-libssh --enable-libtheora --enable-libv4l2 --enable-libvidstab --enable-libvorbis --enable-libvpx --enable-libwebp --enable-libx264 --enable-libx265 --enable-libxcb --enable-libxml2 --enable-libxvid --enable-libzimg --enable-libzmq --enable-lto=auto --enable-lv2 --enable-openssl --enable-pic --enable-postproc --enable-pthreads --enable-shared --enable-vaapi --enable-vdpau --enable-version3 --enable-vulkan --optflags=-O3 --enable-libjxl --enable-libsvtav1 --enable-libvpl
libavutil 58. 29.100 / 58. 29.100
08:31:57,871    debug   [exec] libavcodec 60. 31.102 / 60. 31.102
libavformat 60. 16.100 / 60. 16.100
libavdevice 60. 3.100 / 60. 3.100
libavfilter 9. 12.100 / 9. 12.100
libswscale 7. 5.100 / 7. 5.100
libswresample 4. 12.100 / 4. 12.100
libpostproc 57. 3.100 / 57. 3.100
08:31:57,931    debug   [exec] [rtsp @ 0x7e3e08a65600] method SETUP failed: 461 Unsupported transport
08:32:01,270    debug   [exec] [in#0 @ 0x7e3e08b5b8c0] Error opening input: Invalid data found when processing input
08:32:01,270    debug   [exec] Error opening input file rtsp://user:pwd@xxx.xxx.xx.xx:554/cam/realmonitor?channel=1&subtype=0&unicast=false.
Error opening input files: Invalid data found when processing input
AlexxIT commented 1 month ago

Looks like you already using ffmpeg source in your first message. I missed that.

Maybe ffmpeg needs additional arguments to allow multicast. Try to Google in that direction.

esthar992 commented 1 month ago

Done!!

If this could be useful for someone this is the prompt i've used:

ffmepg_test: exec:ffmpeg -rtsp_transport udp_multicast -i "rtsp://usr:pwd@xxx.xxx.xx.xx:554/cam/realmonitor?channel=1&subtype=0&unicast=false" -c copy -rtsp_transport tcp -f rtsp {output}

AlexxIT commented 1 month ago
ffmpeg:
  rtsp/multicast: "-fflags nobuffer -flags low_delay -timeout 5000000 -user_agent go2rtc/ffmpeg -rtsp_transport udp_multicast -i {input}"

streams:
  ffmepg_test: ffmpeg:rtsp://usr:pwd@xxx.xxx.xx.xx:554/cam/realmonitor?channel=1&subtype=0&unicast=false#input=rtsp/multicast