Skip to content

Instantly share code, notes, and snippets.

@pabsan-0
Last active December 24, 2023 05:51
Show Gist options
  • Save pabsan-0/26407985ba3ba33d2b062247cdb89720 to your computer and use it in GitHub Desktop.
Save pabsan-0/26407985ba3ba33d2b062247cdb89720 to your computer and use it in GitHub Desktop.
Gstreamer send-recv examples through an RTSP server

Gstreamer RTSP demos

This gist comprises several Gstreamer send-recv examples through an RTSP server.

Dependencies

  • Tmuxinator apt install tmuxinator
  • Tmux apt install tmux
  • Gstreamer fat install
apt install libgstreamer1.0-dev libgstreamer-plugins-base1.0-dev libgstreamer-plugins-bad1.0-dev gstreamer1.0-plugins-base gstreamer1.0-plugins-good gstreamer1.0-plugins-bad gstreamer1.0-plugins-ugly gstreamer1.0-rtsp gstreamer1.0-libav gstreamer1.0-tools gstreamer1.0-x gstreamer1.0-alsa gstreamer1.0-gl gstreamer1.0-gtk3 gstreamer1.0-qt5 gstreamer1.0-pulseaudio
  • Mediamtx
    • Either use docker and run with docker run (samples are written for this)
    • If you really cannot use docker, download the latest binary from the repo and tune the given scripts

Usage

Clone this gist with: git clone https://gist.github.com/pabsan-0/26407985ba3ba33d2b062247cdb89720

To run the samples:

tmuxinator start -p sample1.yml
                    sample2.yml
                    sample3.yml
                    sample4.yml
                    sample5.yml
                    sample6.yml

Use the autocompletion script if you like via . completion.sh.

To detach from a tmux session, press ^b then d. The samples are written so the active window will be closed on detach.

Notes

Samples:

  • sample1.yml: Videotest source jpeg encoded + rtp packetized.
  • sample3.yml: Video from webcam (/dev/video0) and streamed as x264 encoded + rtp packetized.
  • sample2.yml: Video from webcam (/dev/video0) and streamed as x265 encoded + rtp packetized. Camera read as jpeg because mine yields higher fps this way.
  • sample4.yml: Video from camera, three separate consumers. Note the use of sync async sink properties for real-time video.

CLI pipeline structure:

gst-launch-1.0 <args>  <source_element> ! <cap>,<caps>,<caps> ! <element> <property> <property> <property> ! <element> ! <sink_element>

Tools

  • gst-inspect-1.0 <element>: See details of an element
  • gst-device-monitor-1.0: See details of plugged hardware
  • Command line helpers:
    • gst-launch-1.0 -v <pipe>: Verbose
    • gst-launch-1.0 -e <pipe>: Safely handle EOS, useful when saving video to file
    • GST_DEBUG=5 gst-launch-1.0 <pipe>: Sets a debug level for the running gstreamer pipeline or application
  • queue: Besides from queueing data buffers towards time-wise buffering, opens up multiprocessing for downstream elements
  • decodebin: a magic Gstreamer element that will automatically try to decode your video
    • Use with verbose ON to get the caps (codec, size, needed steps to decode by hand) of unknown video
    • Not guaranteed to work all of the time
#!/bin/bash
_complete_tmuxinator_ls(){
# echo $COMP_WORDS
if [ ${COMP_WORDS[COMP_CWORD-1]} == "tmuxinator" ]
then
COMPREPLY=("start")
elif [ ${COMP_WORDS[COMP_CWORD-1]} == "start" ]
then
COMPREPLY=("-p")
elif [ $COMP_CWORD -lt 4 ]
then
COMPREPLY=($(ls -1 | grep -- "^${COMP_WORDS[COMP_CWORD]}.*.yml")) #2> /dev/null))
else
:
fi
return 0
}
complete -W "tmuxinator" -E
complete -F _complete_tmuxinator_ls tmuxinator
name: sample1
on_project_exit: tmux kill-window
windows:
- main:
layout: tiled
panes:
- docker run --rm -it --network=host aler9/rtsp-simple-server
- sleep 1; gst-launch-1.0 videotestsrc ! video/x-raw ! jpegenc ! rtspclientsink pay=pj location=rtsp://0.0.0.0:8554/test sync=0 async=0 rtpjpegpay name=pj
- sleep 3; gst-launch-1.0 rtspsrc location=rtsp://0.0.0.0:8554/test ! application/x-rtp, encoding-name=JPEG ! rtpjpegdepay ! jpegdec ! videoconvert! xvimagesink
name: sample2
on_project_exit: tmux kill-window
windows:
- main:
layout: tiled
panes:
- docker run --rm -it --network=host aler9/rtsp-simple-server
- sleep 1; gst-launch-1.0 v4l2src ! video/x-raw,height=480,width=640,fps=5/1 ! videoconvert ! queue ! x264enc ! rtspclientsink pay=enc location=rtsp://0.0.0.0:8554/test sync=0 async=0 rtph264pay name=enc
- sleep 8; gst-launch-1.0 rtspsrc location=rtsp://0.0.0.0:8554/test ! application/x-rtp ! rtph264depay ! h264parse ! avdec_h264 ! videoconvert ! xvimagesink sync=0 async=0
name: sample3
on_project_exit: tmux kill-window
windows:
- main:
layout: tiled
panes:
- docker run --rm -it --network=host aler9/rtsp-simple-server
- sleep 1; gst-launch-1.0 v4l2src ! image/jpeg ! jpegdec ! queue ! x265enc ! rtspclientsink pay=enc location=rtsp://0.0.0.0:8554/test sync=0 async=0 rtph265pay name=enc
- sleep 5; gst-launch-1.0 rtspsrc location=rtsp://0.0.0.0:8554/test ! application/x-rtp ! rtph265depay ! avdec_h265 ! videoconvert ! xvimagesink sync=0 async=0
name: sample5
on_project_exit: tmux kill-window
windows:
- main:
layout: tiled
panes:
- docker run --rm -it --network=host aler9/rtsp-simple-server
- sleep 1; gst-launch-1.0 v4l2src ! video/x-raw,width=640,height=480 ! jpegenc ! rtspclientsink pay=pj location=rtsp://0.0.0.0:8554/test sync=0 async=0 rtpjpegpay name=pj
- sleep 3; gst-launch-1.0 rtspsrc location=rtsp://0.0.0.0:8554/test ! decodebin ! textoverlay text=1 ! xvimagesink
- sleep 3; gst-launch-1.0 rtspsrc location=rtsp://0.0.0.0:8554/test ! decodebin ! textoverlay text=2 ! xvimagesink sync=0 async=0
- sleep 3; gst-launch-1.0 rtspsrc location=rtsp://0.0.0.0:8554/test ! application/x-rtp, encoding-name=JPEG ! rtpjpegdepay ! jpegdec ! videoconvert ! textoverlay text=3 ! xvimagesink sync=0 async=0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment