RTSP 流
RTSP (Real Time Streaming Protocol),实时流协议,是一种应用层协议,专为流媒体使用。本文将介绍 GStreamer, VLC, FFmpeg 这几个工具,如何发送、接收 RTSP 流。
前提
GStreamer
安装:
sudo apt install libgstreamer1.0-0 gstreamer1.0-plugins-base gstreamer1.0-plugins-good gstreamer1.0-plugins-bad gstreamer1.0-plugins-ugly gstreamer1.0-libav gstreamer1.0-doc gstreamer1.0-tools gstreamer1.0-x gstreamer1.0-alsa gstreamer1.0-gl gstreamer1.0-gtk3 gstreamer1.0-qt5 gstreamer1.0-pulseaudio
查看所有插件:
# conda deactivate
gst-inspect-1.0 -a
播放 ~/Videos/test.mp4
文件:
gst-launch-1.0 filesrc location=~/Videos/test.mp4 ! decodebin ! autovideosink
播放 /dev/video0
webcam:
gst-launch-1.0 v4l2src device=/dev/video0 \
! video/x-raw, format=YUY2, width=640, height=480, framerate=30/1 \
! autovideosink
/dev/video0
相关信息,可见后文“关于 Webcam”。
GStreamer RTSP Server
编译 test-launch
例子:
sudo apt install libgstreamer1.0-dev libgstrtspserver-1.0
git clone https://github.com/GStreamer/gst-rtsp-server.git
cd gst-rtsp-server/
git checkout 1.18
cd examples/
gcc test-launch.c -o test-launch $(pkg-config --cflags --libs gstreamer-rtsp-server-1.0)
用 test-launch
发送 RTSP 测试流:
$ ./test-launch "videotestsrc ! x264enc ! rtph264pay name=pay0 pt=96"
stream ready at rtsp://127.0.0.1:8554/test
用 gst-launch
接收 RTSP 测试流:
gst-launch-1.0 playbin uri=rtsp://127.0.0.1:8554/test
发送 RTSP,以 test-launch
发送 ~/Videos/test.mp4
文件:
./test-launch "filesrc location=/home/john/Videos/test.mp4 ! decodebin ! x264enc ! rtph264pay name=pay0 pt=96"
发送 /dev/video0
webcam:
./test-launch "v4l2src device=/dev/video0 ! video/x-raw,format=YUY2,width=640,height=480 ! videoconvert ! x264enc ! rtph264pay name=pay0 pt=96"
接收 RTSP,以 gst-launch
gst-launch-1.0 playbin uri=rtsp://127.0.0.1:8554/test
# or
gst-launch-1.0 rtspsrc location=rtsp://127.0.0.1:8554/test ! decodebin ! autovideosink
参考
- GStreamer
- GStreamer RTSP Server
- Stream webcam with GStreamer (RTSP)
- x264 [error]: baseline profile doesn't support 4:2:2
VLC
安装:
sudo snap install vlc
# allow camera permission
snap connect vlc:camera :camera
查看可用模块:
vlc --list
播放 ~/Videos/test.mp4
文件:
vlc file:///home/john/Videos/test.mp4
播放 /dev/video0
webcam:
vlc v4l2:///dev/video0:chroma=mjpg:width=640:height=480:fps=30
发送 RTSP,以 vlc
发送 ~/Videos/test.mp4
文件:
vlc file:///home/john/Videos/test.mp4 \
--sout '#transcode{vcodec=h264}:rtp{sdp=rtsp://:8554/test}'
发送 /dev/video0
webcam:
vlc v4l2:///dev/video0:chroma=yuyv:width=640:height=480:fps=30 \
--sout '#transcode{vcodec=h264,width=640,height=480}:rtp{sdp=rtsp://:8554/test}'
接收 RTSP,以 vlc
vlc rtsp://127.0.0.1:8554/test
参考
FFmpeg
安装:
sudo apt install ffmpeg
发送 RTSP,以 ffserver
配置 ffserver.conf
:
HTTPPort 8091
HTTPBindAddress 0.0.0.0
RTSPPort 1554
MaxHTTPConnections 2000
MaxClients 1000
MaxBandwidth 1000
CustomLog -
<Stream test.mp4>
File "/home/john/Videos/test.mp4"
Format rtp
</Stream>
开启 ffserver:
$ nohup ffserver -f ffserver.conf &
$ tail nohup.out
libavcodec 57.107.100 / 57.107.100
libavformat 57. 83.100 / 57. 83.100
libavdevice 57. 10.100 / 57. 10.100
libavfilter 6.107.100 / 6.107.100
libavresample 3. 7. 0 / 3. 7. 0
libswscale 4. 8.100 / 4. 8.100
libswresample 2. 9.100 / 2. 9.100
libpostproc 54. 7.100 / 54. 7.100
Wed Apr 21 11:28:39 2021 Opening feed file '/home/john/Videos/test.mp4' for stream 'test.mp4'
Wed Apr 21 11:28:39 2021 FFserver started.