使用appsrc通过gstreamer udpsink进行流式传输

时间:2015-01-26 18:23:05

标签: c video-streaming gstreamer

我的源代码主要来自http://gstreamer.freedesktop.org/data/doc/gstreamer/head/manual/html/section-data-spoof.html,当通过xvimagesink在本地播放时效果很好。

  

这个示例应用程序将生成黑/白(它每秒切换一次)视频到Xv窗口输出,方法是使用appsrc作为带有封顶的源来强制格式化。我们使用颜色空间转换元素来确保我们将正确的格式提供给您的X服务器。我们使用可变帧速率(0/1)配置视频流,并在传出缓冲区上设置时间戳,使得我们每秒播放2帧。

Pipeline在本地游戏时喜欢这个:

gst_bin_add_many (GST_BIN (pipeline), appsrc, conv, videosink, NULL);
gst_element_link_many (appsrc, conv, videosink, NULL);

然后我尝试使用udpsink替换像这样的videosink:

gst_bin_add_many (GST_BIN (pipeline), appsrc, conv, videoenc, payloader, udpsink, NULL);
gst_element_link_many (appsrc, conv, videoenc, payloader, udpsink, NULL);

它不起作用。对于调试级别3,它在下面给出了警告:

0:00:00.033631637 10534      0x1f4d4a0 FIXME                default gstutils.c:3643:gst_pad_create_stream_id_internal:<source:src> Creating random stream-id, consider implementing a deterministic way of creating a stream-id
0:00:00.034634957 10534      0x1f4d4a0 WARN                 basesrc gstbasesrc.c:2933:gst_base_src_loop:<source> error: Internal data flow error.
0:00:00.034664369 10534      0x1f4d4a0 WARN                 basesrc gstbasesrc.c:2933:gst_base_src_loop:<source> error: streaming task paused, reason not-linked (-1)

附上完整源代码。

#include <gst/gst.h>

static GMainLoop *loop;

static void
cb_need_data (GstElement *appsrc,
        guint       unused_size,
        gpointer    user_data)
{
    static gboolean white = FALSE;
    static GstClockTime timestamp = 0;
    GstBuffer *buffer;
    guint size;
    GstFlowReturn ret;

    size = 385 * 288 * 2;

    buffer = gst_buffer_new_allocate (NULL, size, NULL);

    /* this makes the image black/white */
    gst_buffer_memset (buffer, 0, white ? 0xff : 0x0, size);

    white = !white;

    GST_BUFFER_PTS (buffer) = timestamp;
    GST_BUFFER_DURATION (buffer) = gst_util_uint64_scale_int (1, GST_SECOND, 2);

    timestamp += GST_BUFFER_DURATION (buffer);

    g_signal_emit_by_name (appsrc, "push-buffer", buffer, &ret);

    if (ret != GST_FLOW_OK) {
        /* something wrong, stop pushing */
        g_main_loop_quit (loop);
    }
}

gint
main (gint   argc,
        gchar *argv[])
{
    GstElement *pipeline, *appsrc, *conv, *videosink, *payloader, *udpsink, *videoenc;

    /* init GStreamer */
    gst_init (&argc, &argv);
    loop = g_main_loop_new (NULL, FALSE);

    /* setup pipeline */
    pipeline = gst_pipeline_new ("pipeline");
    appsrc = gst_element_factory_make ("appsrc", "source");
    conv = gst_element_factory_make ("videoconvert", "conv");
    videoenc = gst_element_factory_make("ffenc_mpeg4", "ffenc_mpeg4");
    videosink = gst_element_factory_make ("xvimagesink", "videosink");
    payloader = gst_element_factory_make("rtpmp4vpay", "rtpmp4vpay");
    g_object_set(G_OBJECT(payloader),
            "config-interval", 0,
            NULL);
    udpsink = gst_element_factory_make("udpsink", "udpsink");
    g_object_set(G_OBJECT(udpsink),
            "host", "127.0.0.1",
            "port", 5000,
            NULL);

    /* setup */
    g_object_set (G_OBJECT (appsrc), "caps",
            gst_caps_new_simple ("video/x-raw",
                "format", G_TYPE_STRING, "RGB16",
                "width", G_TYPE_INT, 384,
                "height", G_TYPE_INT, 288,
                "framerate", GST_TYPE_FRACTION, 0, 1,
                NULL), NULL);

#if 0
    // THIS WORKS!
    gst_bin_add_many (GST_BIN (pipeline), appsrc, conv, videosink, NULL);
    gst_element_link_many (appsrc, conv, videosink, NULL);
#else
    // THIS DOESN'T WORK
    gst_bin_add_many (GST_BIN (pipeline), appsrc, conv, videoenc, payloader, udpsink, NULL);
    gst_element_link_many (appsrc, conv, videoenc, payloader, udpsink, NULL);
#endif

    /* setup appsrc */
    g_object_set (G_OBJECT (appsrc),
            "stream-type", 0,
            "is-live", TRUE,
            "format", GST_FORMAT_TIME, NULL);
    g_signal_connect (appsrc, "need-data", G_CALLBACK (cb_need_data), NULL);

    /* play */
    gst_element_set_state (pipeline, GST_STATE_PLAYING);
    g_main_loop_run (loop);

    /* clean up */
    gst_element_set_state (pipeline, GST_STATE_NULL);
    gst_object_unref (GST_OBJECT (pipeline));
    g_main_loop_unref (loop);

    return 0;
}

可以编译:

gcc app1.c -o app1 `pkg-config --cflags --libs gstreamer-base-1.0` `pkg-config --cflags --libs gstreamer-1.0` -g -lgstapp-1.0
用于测试的

.sdp文件:

v=0
m=video 5000 RTP/AVP 96
c=IN IP4 127.0.0.1
a=rtpmap:96 MP4V-ES/90000

谢谢!

1 个答案:

答案 0 :(得分:3)

最后我用gstreamer1.0解决了这个问题。

我猜问题是颜色空间。切换到gst1.0并将videoenc更改为avenc_mpeg4后,它可以正常工作。使用gst0.10,我认为在videoenc可以做同样的技巧之前添加一个ffmpegcolorspace。

可以在https://gist.github.com/beeender/d539734794606a38d4e3

上找到工作代码

编译:

gcc app1.c -o app1 `pkg-config --cflags --libs gstreamer-base-1.0` `pkg-config --cflags --libs gstreamer-1.0` -g -lgstapp-1.0

测试:

vlc test.sdp

在我的问题中查看sdp文件。