You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

基于Gst-RTSP Server实现摄像头RTSP推流失败求助

问题:GStreamer RTSP Server推流管道解析失败

问题描述

基于GStreamer的RTSP Server实现网络摄像头RTSP推流,但管道无法正常工作,出现语法解析错误。

代码实现

#include <gstreamer-1.0/gst/gst.h>
#include <gstreamer-1.0/gst/rtsp-server/rtsp-server.h>
#include <iostream>

static gboolean
timeout (GstRTSPServer * server)
{
  GstRTSPSessionPool *pool;

  pool = gst_rtsp_server_get_session_pool (server);
  gst_rtsp_session_pool_cleanup (pool);
  g_object_unref (pool);

  return TRUE;
}

int main(){
    gst_init(NULL, NULL);

    GstRTSPServer *server; 
    GstRTSPMediaFactory *factory; 
    GMainLoop *loop; 
    GstRTSPAddressPool *pool; 
    GstRTSPMountPoints *mounts;

    loop = g_main_loop_new(NULL, false);
    server = gst_rtsp_server_new();
    mounts = gst_rtsp_server_get_mount_points(server);
    factory = gst_rtsp_media_factory_new();

    gst_rtsp_media_factory_set_launch (factory, "(v4l2src device=/dev/video0 ! videoconvert ! video/x-raw, width=640, height=480, format=I420 ! x264enc ! rtph264pay name=pay0 pt=96)");
    gst_rtsp_media_factory_set_shared(factory, true); 

    pool = gst_rtsp_address_pool_new();
    gst_rtsp_address_pool_add_range(pool, "192.168.0.92", "192.168.0.92", 5000, 9000, 16);

    gst_rtsp_media_factory_set_address_pool(factory, pool); 
    gst_rtsp_media_factory_set_protocols (factory, GST_RTSP_LOWER_TRANS_UDP_MCAST);
    g_object_unref (pool);

    /* attach the test factory to the /test url */
    gst_rtsp_mount_points_add_factory (mounts, "/test", factory);

    /* don't need the ref to the mapper anymore */
    g_object_unref (mounts);

    /* attach the server to the default maincontext */
    if (gst_rtsp_server_attach (server, NULL) == 0)
        goto failed;

    g_timeout_add_seconds (2, (GSourceFunc) timeout, server);

    /* start serving */
    g_print ("stream ready at rtsp://192.168.0.92:8554/test\n");
    g_main_loop_run (loop);

    return 0;

    /* ERRORS */
    failed:
    {
        g_print ("failed to attach the server\n");
        return -1;
    }
}

错误信息

stream ready at rtsp://192.168.0.92:8554/test

(rtsp_server:5974): GStreamer-RTSP-Server-CRITICAL **: 16:53:20.791: could not parse launch syntax ((v4l2src device=/dev/video0 ! videoconvert ! video/x-raw, width=640, height=480, format=I420 ! x264enc ! rtph264pay name=pay0 pt=96)): could not set property "pt" in element "pay0" to "96)"
(rtsp_server:5974): GStreamer-RTSP-Server-CRITICAL **: 16:53:20.791: could not create element
(rtsp_server:5974): GStreamer-RTSP-Server-CRITICAL **: 16:53:20.816: could not parse launch syntax ((v4l2src device=/dev/video0 ! videoconvert ! video/x-raw, width=640, height=480, format=I420 ! x264enc ! rtph264pay name=pay0 pt=96)): could not set property "pt" in element "pay0" to "96)"
(rtsp_server:5974): GStreamer-RTSP-Server-CRITICAL **: 16:53:20.816: could not create element
(rtsp_server:5974): GStreamer-RTSP-Server-CRITICAL **: 16:53:57.666: could not parse launch syntax ((v4l2src device=/dev/video0 ! videoconvert ! video/x-raw, width=640, height=480, format=I420 ! x264enc ! rtph264pay name=pay0 pt=96)): could not set property "pt" in element "pay0" to "96)"
(rtsp_server:5974): GStreamer-RTSP-Server-CRITICAL **: 16:53:57.666: could not create element

解决方案

核心错误原因

错误信息显示,GStreamer解析launch字符串时,把pt=96)当成了pay0元素的pt属性值,这是因为给launch字符串额外添加了外层括号导致的。gst_rtsp_media_factory_set_launch要求的launch语法不需要外层括号,多余的括号会被解析为语法的一部分,引发属性赋值错误。

修正后的关键代码

只需去掉launch字符串的外层括号:

// 修正后的launch语句
gst_rtsp_media_factory_set_launch (factory, "v4l2src device=/dev/video0 ! videoconvert ! video/x-raw, width=640, height=480, format=I420 ! x264enc ! rtph264pay name=pay0 pt=96");

额外优化建议

  • 给x264enc添加speed-preset参数平衡编码速度与质量,例如x264enc speed-preset=ultrafast
  • 如果摄像头原生输出格式已经是I420,可以移除videoconvert环节提升性能
  • 测试阶段建议先用UDP单播模式(GST_RTSP_LOWER_TRANS_UDP)验证推流正常,再切换到组播模式

内容的提问来源于stack exchange,提问作者Vimal William

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.30 13:18:04