GStreamer daemon
GStreamer Daemon (gstd) 是一个后台服务,用于通过高层 TCP API 控制 GStreamer 管道。它能够实现对音频/视频管道的远程或解耦实时控制。
gstd 的主要优势在于它将控制逻辑与媒体处理逻辑分离。这允许通过 TCP 连接上的简单文本消息动态地创建、播放、暂停或修改管道。控制应用程序可以用任何语言编写,并在不同机器上运行,而无需 GObject 或 GLib 绑定。
这种设计使得 gstd 非常适合嵌入式设备、自动化测试以及专业的流媒体或录制应用。它可以作为用户服务(用于桌面集成)或系统服务(用于无头环境)运行。
安装
安装 gstdAUR 包。
强烈建议同时安装以下 GStreamer 插件集,以启用广泛的媒体格式和功能:
其他有用的软件包包括用于连接独立管道的 gst-plugin-interpipeAUR、用于 Web 界面示例的 lighttpd,以及用于调试的 valgrind。
配置
gstd 通过 /etc/conf.d/gstd 文件进行配置。该文件设置了守护进程监听的地址和端口。
示例配置
/etc/conf.d/gstd
# Default configuration for both user and system services GSTD_OPTS="--enable-tcp-protocol --tcp-address=127.0.0.1 --tcp-base-port=5000" # For debugging: # GSTD_OPTS="--enable-tcp-protocol --tcp-address=127.0.0.1 --tcp-base-port=5000 --gst-debug=*:2" # For remote access (SECURITY RISK): # GSTD_OPTS="--enable-tcp-protocol --tcp-address=0.0.0.0 --tcp-base-port=5000"
gstd 支持多种通信协议,其中 TCP 是主要方法。为兼容性也提供了 HTTP API 和 D-Bus。
用法
该软件包提供了两个 systemd 服务:
gstd.service- 用于桌面使用的 用户单元,可以显示窗口并使用扬声器。gstd-server.service- 用于无头/嵌入式/服务器使用的系统服务,以 gstd 用户身份运行。
一次只能 启用 一个服务。
命令行界面
使用 gst-client 或 gst-client-1.0 控制管道。该工具支持交互模式和单命令执行。
交互模式
$ gst-client $ gstd> pipeline_create testpipe videotestsrc name=vts ! autovideosink $ gstd> pipeline_play testpipe $ gstd> element_set testpipe vts pattern ball $ gstd> pipeline_delete testpipe
用于脚本的单命令模式
$ gst-client pipeline_create testpipe videotestsrc ! autovideosink $ gst-client pipeline_play testpipe $ gst-client pipeline_delete testpipe
桌面使用示例:显示测试视频
$ gst-client pipeline_create my_gui_pipe 'videotestsrc ! autovideosink'
$ gst-client pipeline_play my_gui_pipe
$ gst-client pipeline_delete my_gui_pipe
窗口中应出现测试视频图案。
服务器使用示例:从 DeckLink (720p50) 录制
启动/启用 gstd-server.service。
此示例演示了一个专业用例:从 DeckLink 卡的第一个 SDI 输入(设备编号=0)捕获 720p50 视频,并将其编码为 MP4 文件。
输出文件保存在 /var/lib/gstd/,gstd 系统用户可以写入该目录。通过 systemd 服务定义,该路径被明确授予读写访问权限。
$ gst-client pipeline_create decklink_rec 'decklinkvideosrc device-number=0 mode=17 connection=sdi ! videoconvert ! queue ! x264enc ! mp4mux ! filesink location=/var/lib/gstd/decklink_recording.mp4'
$ gst-client pipeline_play decklink_rec
优雅地停止录制:
$ gst-client event_eos decklink_rec
$ gst-client pipeline_stop decklink_rec
$ gst-client pipeline_delete decklink_rec
检查录制文件是否已创建:
# ls -l /var/lib/gstd/decklink_recording.mp4
高级功能
gstd 支持运行时管道修改,包括跳转、速度控制和属性更改。
# Seek to 30 seconds (in nanoseconds) $ gst-client element_seek player 30000000000
# Change playback speed (2x faster, 0.5x slower, -1.0 reverse) $ gst-client element_speed player 2.0
# Modify element properties during playback $ gst-client element_set camera x264enc bitrate 1000000
与 Interpipe 一起使用
gst-plugin-interpipeAUR 包允许连接多个独立管道而不进行数据复制,这对于复杂的流媒体设置非常有用。
# Create source and sink pipelines $ gst-client pipeline_create src videotestsrc ! interpipesink name=mysink $ gst-client pipeline_create sink interpipesrc listen-to=mysink ! autovideosink
# Switch connections at runtime $ gst-client element_set sink interpipesrc listen-to=othersink
直接 TCP 通信
gstd 守护进程提供多种 API 接口,以支持各种开发环境和集成需求。目前支持的 API 变体包括:
- C/C++ API – 适用于需要直接系统集成的、高性能的原生应用程序。
- Python API – 适用于脚本编写、快速原型设计和自动化用例。
- JavaScript API – 允许与基于 Web 的应用程序或 Node.js 环境集成。
- HTTP API – 一种语言无关的、类似 REST 的接口,适用于远程访问和轻量级客户端。
应用程序可以通过 TCP 直接与 gstd 通信,从而在本地或分布式系统之间实现灵活高效的集成。
使用 netcat
$ echo "pipeline_create test videotestsrc ! fakesink" | nc -q0 localhost 5000
$ echo "pipeline_play test" | nc -q0 localhost 5000
$ echo "pipeline_pause test" | nc -q0 localhost 5000
$ echo "element_set test videotestsrc1 pattern ball" | nc -q0 localhost 5000
$ echo "pipeline_stop test" | nc -q0 localhost 5000
$ echo "pipeline_delete test" | nc -q0 localhost 5000
检查管道状态:
echo "list_pipelines" | nc -q0 localhost 5000
TCP 协议使用简单的文本命令,类似于 gst-client,对于元素属性操作和管道删除特别可靠。
使用 HTTP API (RESTful API)
对于 Web 应用程序和 REST 风格的集成,gstd 在启用后提供 HTTP API。
在 /etc/conf.d/gstd 中启用 HTTP 协议。
GSTD_OPTS="--enable-http-protocol --http-address=127.0.0.1 --http-port=5000"
完整的 HTTP 工作流程(推荐使用 --data-urlencode 以进行正确的 URL 编码):
$ curl -X POST -G --data-urlencode "name=test" --data-urlencode "description=videotestsrc ! autovideosink" http://127.0.0.1:5000/pipelines
$ curl http://127.0.0.1:5000/pipelines/ # List pipelines
$ curl -X PUT 'http://127.0.0.1:5000/pipelines/test/state?name=playing'
$ curl -X PUT 'http://127.0.0.1:5000/pipelines/test/state?name=paused'
$ curl -X PUT 'http://127.0.0.1:5000/pipelines/test/state?name=null' # Stop
$ curl -X DELETE 'http://127.0.0.1:5000/pipelines?name=test' # Delete
获取管道信息:
$ curl http://127.0.0.1:5000/pipelines/test/state # Get current state
$ curl http://127.0.0.1:5000/pipelines/test/graph # Get GraphViz representation
列出管道中的所有元素:
$ curl http://127.0.0.1:5000/pipelines/test/elements/
故障排除
"地址已在使用中"
当指定的端口已被另一个服务占用,或者用户和系统实例的 gstd 同时运行时,会发生此错误。
禁用不需要的那个。
"状态错误" 或 "管道描述错误"
这通常意味着:
- 缺少所需的 GStreamer 插件。
- 管道语法无效。
解决方案
- 确保已安装 gst-plugins-good 等插件。
- 安装插件后重启 gstd 服务。
- 使用 queue、videoconvert 或 audioconvert 来辅助协商。
插件注册表问题
如果在安装插件包后新的 GStreamer 元素未出现,请重建插件注册表。
$ rm ~/.cache/gstreamer-1.0/registry.*.bin
调试
运行 gstd 并输出调试信息以进行故障排除。
$ gstd -e --gst-debug-level=3
$ gstd -e --gst-debug=gstd:5,GST_PADS:4
验证服务是否正在运行并监听。
$ systemctl --user status gstd # Running as user unit $ systemctl status gstd-server # Running as system service
$ ss -tlnp | grep 5000
管道问题的常见解决方案包括:验证是否安装了所有必需的插件,首先使用 gst-launch-1.0 测试管道,以及使用 autovideosink/autoaudiosink 进行自动输出选择。