我的源代碼主要來自http://gstreamer.freedesktop.org/data/doc/gstreamer/head/manual/html/section-data-spoof.html,在通過xvimagesink進行本地播放時效果很好。使用appsrc通過gstreamer進行流式傳輸udpsink
此示例應用程序將通過使用appsrc作爲源來強制格式來生成黑/白(每秒切換)視頻到Xv窗口輸出。我們使用色彩空間轉換元素來確保將正確的格式提供給X服務器。我們用變化的幀速率(0/1)配置一個視頻流,並且在輸出緩衝區中設置時間戳,以便我們每秒播放2幀。
管道喜歡這個戲時,本地:
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
謝謝!