常⽤插件
1、Source
GStreamer的Source插件用于生成和提供媒体数据流。它们可以从不同的数据源读取音频或视频,比如文件、网络、设备等。 常见的Source插件包括文件源(如 filesrc)、网络源(如 tcpserversrc)、设备源(如 v4l2src),这些插件允许开发者灵活地构建多媒体管道, 以满足不同的应用需求。通过配置Source插件,用户可以轻松获取和处理多种类型的媒体数据。
1.1、filesrc
从⽂件读取数据,⽰例如下:
#创建/tmp/test
echo 666 > /tmp/test
#读取文件数据到/tmp/test2
gst-launch-1.0 filesrc location=/tmp/test ! filesink location=/tmp/test2
#查看
cat /tmp/test2
1.2. videotestsrc
⽣成视频数据,⽰例如下:
# 使⽤默认格式输出视频
gst-launch-1.0 videotestsrc ! xvimagesink
# 使⽤指定格式输出视频
gst-launch-1.0 videotestsrc ! "video/x-raw,width=1920,height=1080,format=(string)NV12" ! xvimagesink
1.3. v4l2src
从摄像头获取视频数据,⽰例如下:
#如果不指定摄像头编号,默认使用系统的第一个视频设备/dev/video0
gst-launch-1.0 v4l2src ! video/x-raw,width=1920,height=1080,format=NV12 ! xvimagesink
#如果需要使用特定的摄像头,可以通过device属性指定设备,例如
gst-launch-1.0 v4l2src device=/dev/video0 ! video/x-raw,width=1920,height=1080,format=NV12 ! xvimagesink
1.4. rtspsrc和rtspclientsink
rtspsrc和rtspclientsink是GStreamer中用于处理RTSP流的两个不同元素。
rtspsrc:这是一个源元素,用于接收RTSP流。它可以从RTSP服务器拉取音视频流,并将其传递给管道的下游元素进行处理。
rtspclientsink:这是一个接收元素,用于将处理后的音视频流发送到RTSP服务器。
简单来说,rtspsrc用于接收流,rtspclientsink用于发送流。
系统默认没有安装插件,需要进行安装:
#安装插件
sudo apt install gstreamer1.0-rtsp
具体使用方法,参考本章节推流、拉流部分。
2、 Sink
GStreamer的Sink插件用于接收和处理媒体数据流,通常用于输出到不同的目标,例如文件、音频设备、视频显示等。 常见的Sink插件包括文件接收器(如filesink)、音频输出(如 alsasink)、视频显示(如 ximagesink)。 这些插件使开发者能够灵活地将媒体数据导出到所需的格式或设备,方便实现多媒体播放和录制功能。
2.1. filesink
将收到的数据保存为⽂件,⽰例如下:
#创建/tmp/test
echo 666 > /tmp/test
#读取文件数据到/tmp/test2
gst-launch-1.0 filesrc location=/tmp/test ! filesink location=/tmp/test2
#查看
cat /tmp/test2
2.2. fakesink
将收到的数据全部丢弃,⽰例如下:
gst-launch-1.0 filesrc location=/tmp/test ! fakesink
2.3. xvimagesink
视频Sink,接收视频并显⽰,使⽤X11接口实现,⽰例如下:
gst-launch-1.0 videotestsrc ! xvimagesink
2.4. kmssink
视频Sink,接收视频并显⽰,使⽤kms接口实现,需要独占硬解图层,⽰例如下:
gst-launch-1.0 videotestsrc ! kmssink
# 常⽤命令
connector-id #指定屏幕
plane-id #指定硬件图层
render-rectangle #指定渲染范围
2.5. waylandsink
视频Sink,接收视频并显⽰,使⽤wayland接口实现,⽰例如下:
gst-launch-1.0 videotestsrc ! waylandsink
2.6. rkximagesink
视频Sink,接收视频并显⽰,使⽤drm接口实现零拷⻉等功能,性能较好,但需要独占硬解图层。⽰例如下:
gst-launch-1.0 videotestsrc ! rkximagesink
2.7. fpsdisplaysink
视频Sink,接收视频并统计帧率,同时会将视频中转⾄下⼀级Sink显⽰,⽰例如下:
#⽇志等级为TRACE(7)即可查看实时帧率,设置为DEBUG(5)则只显⽰最⼤/最小帧率
GST_DEBUG=fpsdisplaysink:7 gst-play-1.0 --flags=3 --videosink="fpsdisplaysink \
video-sink=xvimagesink signal-fps-measurements=true text-overlay=false \
sync=false" test.mp4
#信息输出如下
Press 'k' to see a list of keyboard shortcuts.
Now playing /home/cat/test.mp4
0:00:00.111413754 7044 0x5580fc5d90 DEBUG fpsdisplaysink fpsdisplaysink.c:440:fps_display_sink_start:<fpsdisplaysink0> Use text-overlay? 0
Redistribute latency...
0:00:02.035048410 7044 0x7f6004e2d0 DEBUG fpsdisplaysink fpsdisplaysink.c:372:display_current_fps:<fpsdisplaysink0> Updated max-fps to 44.703224
0:00:02.036371046 7044 0x7f6004e2d0 DEBUG fpsdisplaysink fpsdisplaysink.c:376:display_current_fps:<fpsdisplaysink0> Updated min-fps to 44.703224
0:00:02.037667141 7044 0x7f6004e2d0 LOG fpsdisplaysink fpsdisplaysink.c:381:display_current_fps:<fpsdisplaysink0> Signaling measurements: fps:44.703224 droprate:0.000000 avg-fps:44.703224
0:00:02.552749637 7044 0x7f6004e2d0 DEBUG fpsdisplaysink fpsdisplaysink.c:372:display_current_fps:<fpsdisplaysink0> Updated max-fps to 46.356696
0:00:02.554009567 7044 0x7f6004e2d0 LOG fpsdisplaysink fpsdisplaysink.c:381:display_current_fps:<fpsdisplaysink0> Signaling measurements: fps:46.356696 droprate:0.000000 avg-fps:45.532539
3 、视频推流/拉流
3.1. 本地推流/拉流
3.1.1 USB摄像头
摄像头接口为device=/dev/video9,需要先运行接收端命令,然后再运行服务端(注意如果是ssh需要指定export DISPLAY=:0环境变量)
#板卡接收端
gst-launch-1.0 udpsrc port=1234 ! "application/x-rtp, payload=96" ! rtph264depay ! decodebin ! autovideosink sync=false
#板卡服务端测试1
gst-launch-1.0 v4l2src device=/dev/video9 ! video/x-raw,width=640,height=480,framerate=30/1 ! videoconvert ! video/x-raw,format=UYVY ! mpph264enc ! queue ! h264parse ! rtph264pay ! udpsink host=127.0.0.1 port=1234
#板卡服务端测试2
gst-launch-1.0 v4l2src device=/dev/video9 ! image/jpeg ! mppjpegdec ! videoconvert ! video/x-raw,height=1080,width=1920,frame=30/1,format=NV12 ! mpph264enc ! queue ! h264parse ! rtph264pay ! udpsink host=127.0.0.1 port=1234
说明如下:
接收端,命令通过udpsrc从指定的端口(1234)接收RTP数据流,使用rtph264depay解封装RTP数据,接着通过decodebin解码视频流,最后将解码后的视频输出到自动选择的视频窗口(autovideosink)。
服务端测试1:使用v4l2src直接捕获原始视频流,输出为YUYV格式,并进行H.264编码,然后通过rtph264pay封装成RTP数据包,最后使用udpsink将数据推送到指定的地址(127.0.0.1:1234)。
服务端测试2:同样使用v4l2src,但输入的是JPEG格式的图像,通过解码后再转换成NV12格式,并进行H.264编码,然后通过rtph264pay封装成RTP数据包,最后使用udpsink将数据推送到指定的地址(127.0.0.1:1234)。
3.1.2 ov8858 mipi摄像头
摄像头接口为device=/dev/video0,需要先运行接收端命令,然后再运行服务端(注意如果是ssh需要指定export DISPLAY=:0环境变量)
#板卡接收端
gst-launch-1.0 udpsrc port=1234 ! "application/x-rtp, payload=96" ! rtph264depay ! decodebin ! autovideosink sync=false
#板卡服务端测试1
gst-launch-1.0 v4l2src device=/dev/video0 ! video/x-raw,width=640,height=480,framerate=23/1 ! videoconvert ! video/x-raw,format=UYVY ! mpph264enc ! queue ! h264parse ! rtph264pay ! udpsink host=127.0.0.1 port=1234
#板卡服务端测试2
gst-launch-1.0 v4l2src device=/dev/video0 ! video/x-raw,width=1280,height=960,framerate=23/1 ! videoconvert ! video/x-raw,format=NV12 ! mpph264enc ! queue ! h264parse ! rtph264pay ! udpsink host=127.0.0.1 port=1234
以上和usb相似,只是ov8858不支持输出JPEG格式,测试1和测试2分辨率和格式不同。
3.2. RTSP推流/拉流
系统默认没有安装插件,需要进行安装:
#安装插件
sudo apt install gstreamer1.0-rtsp
推流前需要启动RTSP服务器,需要运行mediamtx
访问: mediamtx,下载mediamtx_vx.x.x_linux_arm64v8.tar.gz
将mediamtx_v1.9.1_linux_arm64v8.tar.gz传到板卡解压并运行:
#运行mediamtx
sudo ./mediamtx &
#信息输出如下
2024/09/18 16:24:54 INF MediaMTX v1.9.1
2024/09/18 16:24:54 INF configuration loaded from /home/cat/mediamtx.yml
2024/09/18 16:24:54 INF [RTSP] listener opened on :8554 (TCP), :8000 (UDP/RTP), :8001 (UDP/RTCP)
2024/09/18 16:24:54 INF [RTMP] listener opened on :1935
2024/09/18 16:24:54 INF [HLS] listener opened on :8888
2024/09/18 16:24:54 INF [WebRTC] listener opened on :8889 (HTTP), :8189 (ICE/UDP)
2024/09/18 16:24:54 INF [SRT] listener opened on :8890 (UDP)
将视频流发送到RTSP服务器,简单⽰例如下:
以下测试USB摄像头,摄像头接口为device=/dev/video9
gst-launch-1.0 v4l2src device=/dev/video9 ! image/jpeg ! mppjpegdec ! videoconvert ! video/x-raw,height=1080,width=1920,frame=30/1,format=NV12 ! tee name=t \
t. ! queue ! videoconvert ! autovideosink \
t. ! videoconvert ! mpph264enc ! queue ! h264parse ! rtspclientsink location=rtsp://127.0.0.1:8554/live
以下测试mipi摄像头,摄像头接口为device=/dev/video0
gst-launch-1.0 v4l2src device=/dev/video0 ! video/x-raw,width=1920,height=1080,framerate=23/1 ! tee name=t \
t. ! queue ! videoconvert ! autovideosink \
t. ! videoconvert ! mpph264enc ! queue ! h264parse ! rtspclientsink location=rtsp://127.0.0.1:8554/live
从RTSP服务器中获取视频流,简单⽰例如下:
#指定为实际推流板卡ip
gst-launch-1.0 rtspsrc location=rtsp://192.168.103.101:8554/live ! rtph264depay ! h264parse ! mppvideodec ! xvimagesink
或者使用VLC工具,打开左上角媒体->打开网络串流
然后根据推流板卡实际IP输入RTSP地址
如果链接成功则会出现摄像头画面。