Skip to content

Instantly share code, notes, and snippets.

@bmount
Last active November 14, 2021 16:20
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save bmount/6cb201aa1a869bfdc0b3a4be49fa9c26 to your computer and use it in GitHub Desktop.
Save bmount/6cb201aa1a869bfdc0b3a4be49fa9c26 to your computer and use it in GitHub Desktop.
gstreamer udp rtsp

Snippets collected/distilled from gists/blog posts/etc. Combined here for fellow web-searchers -- goal is to have an easy/minimal sink for in-app use, and then forward that stream in another process.

Read camera, push to UDP sink (usually from appsrc, here v4l2 camera):

$ gst-launch-1.0 -vvvv v4l2src ! 'video/x-raw, width=640, height=480, framerate=30/1' ! videoconvert ! x264enc pass=qual quantizer=20 tune=zerolatency ! rtph264pay ! udpsink port=1234

Visualize above:

$ gst-launch-1.0 -vvv udpsrc port=1234 ! "application/x-rtp,media=(string)video,clock-rate=(int)90000,encoding-name=(string)H264" ! rtph264depay ! h264parse ! decodebin ! videoconvert ! xvimagesink sync=false

Instead of preceding, read UDP stream and republish over network as RTSP stream, using https://github.com/GStreamer/gst-rtsp-server/blob/master/examples/test-launch.c (build: libtool --mode=link gcc `pkg-config --cflags --libs gstreamer-1.0` -L/usr/lib/x86_64-linux-gnu -lgstrtspserver-1.0 test-launch.c -o gst-rtsp-launch)

GST_DEBUG=1 ./bin/gst-rtsp-launch "udpsrc port=1234 ! application/x-rtp,media=(string)video,clock-rate=(int)90000,encoding-name=(string)H264 ! rtph264depay ! h264parse ! decodebin ! videoconvert ! x264enc ! rtph264pay name=pay0 pt=96 "

Read RTSP stream:

$ gst-launch-1.0 -vvv rtspsrc location=rtsp://127.0.0.1:8554/test ! rtph264depay ! h264parse ! decodebin ! videoconvert ! xvimagesink sync=false

@bmount
Copy link
Author

bmount commented Mar 1, 2019

Host: GST_DEBUG=3 ./gst-rtsp-launch "nvcamerasrc ! video/x-raw(memory:NVMM), width=(int)1920, height=(int)1080, format=(string)I420, framerate=(fraction)30/1 ! omxh264enc bitrate=10000000 ! video/x-h264, profile=baseline ! rtph264pay name=pay0 pt=96 "

Desktop Client: gst-launch-1.0 -vvv rtspsrc location=rtsp://192.168.1.172:8554/test ! rtph264depay ! h264parse ! decodebin ! videoconvert ! xvimagesink

@pycoco
Copy link

pycoco commented Oct 22, 2019

hello,gst-rtsp-server how to redirct a rtsp stream

@bmount
Copy link
Author

bmount commented Oct 22, 2019

I'm not sure I follow the question exactly, but the idea above is: we want to publish a video stream from within an application, and ideally we'd do that with a single pipeline. That is, we want the hypothetical equivalent of "videotestsrc ! rtspsink". Since that doesn't exist, we instead do "videotestsrc ! udpsink", then in some other process, publish that udpsink to the network.

@pycoco
Copy link

pycoco commented Oct 25, 2019

thank u very much ,now what i do:
first --(data from pipe )push to a rtsp stream to gst-rtsp-server by command =['ffmpeg',
'-y',
'-f', 'rawvideo',
'-vcodec', 'rawvideo',
'-pix_fmt', 'bgr24',
'-s', '{}x{}'.format(opt['image']['width'],opt['image']['height']),
'-r', '{}'.format(opt['server']['fps']),
'-i', '-',
'-c:v', 'libx264',
'-pix_fmt', 'yuv422p',
'-preset:v', 'ultrafast',
'-tune:v','zerolatency',
'-f', video_format,
server_url]
second: what should i set the launch src . videotest src don't work. (perhaps i shouldn't push a rtsp stream by ffmpeg,can u give me som suggestion?)
third: what i need is gst-rtsp-server url,such as http://192.168.0.11/test.this url can play in vlc.

@paulorenanmelo
Copy link

@bmount you can try something like this
gst-launch-1.0 udpsrc port=5001 ! "application/x-rtp, encoding-name=(string)RAW, sampling=(string)RGB, depth=(string)8, width=(string)1920, height=(string)1080" ! rtpvrawdepay ! queue ! rtpvrawpay mtu=65500 ! udpsink host=127.0.0.1 port=6001 sync=true async=true

you can also have multiple sinks in one pipeline using tee. You may not need two queues like below, that's just to illustrate a way to do it.
[...] source, caps, converting, etc [...]
! tee name=t t. ! queue ! udpsink 127.0.0.1 port=6001 sync=true async=true t. ! queue ! udpsink 127.0.0.1 port=7001 sync=true async=true

@ericsonj
Copy link

ericsonj commented Aug 5, 2021

Hi
thanks for the example.

How I can server many clients?

UDP -> RTSP -> CLIENT 1
    \> RTSP -> CLIENT 2

@bmount
Copy link
Author

bmount commented Aug 5, 2021

Hi EJ,

I haven't looked at this code in quite a while but I think the RTSP server can support multiple clients, I'd try (per your plot):

UDP -> RTSP -> CLIENT 1
            \> CLIENT 2

I just saw some bug reports that this fails on certain distributions of GStreamer (eg at least one version of Linux4Tegra), but I think it should work -- do you get any specific errors or unexpected behavior with this approach?

@ericsonj
Copy link

ericsonj commented Aug 6, 2021

Hi @bmount

Thanks you for your comment, I tried this:

UDP -> RTSP -> CLIENT 1
            \> CLIENT 2

But the first client stop the video when the second client start the video. I found that test-launch re play the pipeline for each user connected.

Test

udpsrc port=1234 ! application/x-rtp,media=(string)video,clock-rate=(int)90000,encoding-name=(string)H264 ! rtph264depay ! h264parse ! decodebin ! videoconvert ! x264enc ! rtph264pay name=pay0 pt=96 

I think that the problem is that many pipeline can´t read the same UDP port.

Now I'm reading multiudpsink for try another option.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment