我有一个基本的gstreamer命令,用于播放从网络接收的音频流:
gst-launch-1.0 tcpserversrc host=127.0.0.1 port=5000 ! decodebin ! audioconvert ! alsasink
我尝试将其转换为C程序,但是当我运行它时,我收到“内部数据流错误”。
#include <gst/gst.h>
static gboolean bus_call (GstBus *bus, GstMessage *msg, gpointer data)
{
GMainLoop *loop = (GMainLoop *) data;
switch (GST_MESSAGE_TYPE (msg)) {
case GST_MESSAGE_EOS:
g_print ("End of stream\n");
g_main_loop_quit (loop);
break;
case GST_MESSAGE_ERROR: {
gchar *debug;
GError *error;
gst_message_parse_error (msg, &error, &debug);
g_free (debug);
g_printerr ("Error: %s\n", error->message);
g_error_free (error);
g_main_loop_quit (loop);
break;
}
default:
break;
}
return TRUE;
}
gint main (gint argc, gchar *argv[])
{
GMainLoop *loop;
GstElement *pipeline, *src, *dec, *conv, *sink;
GstBus *bus;
/* init GStreamer */
gst_init (&argc, &argv);
loop = g_main_loop_new (NULL, FALSE);
/* setup */
pipeline = gst_pipeline_new ("pipeline");
bus = gst_pipeline_get_bus (GST_PIPELINE (pipeline));
gst_bus_add_watch (bus, bus_call, loop);
gst_object_unref (bus);
src = gst_element_factory_make ("tcpserversrc", "source");
g_object_set (G_OBJECT (src), "host", "127.0.0.1",NULL);
g_object_set (G_OBJECT (src), "port", 5000 ,NULL);
dec = gst_element_factory_make ("decodebin", "decoder");
conv = gst_element_factory_make ("audioconvert", "aconv");
sink = gst_element_factory_make ("alsasink", "sink");
gst_bin_add_many (GST_BIN (pipeline), src, dec, conv, sink, NULL);
gst_element_link (src, dec);
gst_element_link (dec, conv);
gst_element_link (conv, sink);
/* run */
gst_element_set_state (pipeline, GST_STATE_PLAYING);
g_main_loop_run (loop);
/* cleanup */
gst_element_set_state (pipeline, GST_STATE_NULL);
gst_object_unref (GST_OBJECT (pipeline));
return 0;
}
这是我用来测试服务器的命令:
gst-launch-1.0 filesrc location="file.wav" ! tcpclientsink host=127.0.0.1 port=5000
感谢。
答案 0 :(得分:2)
因为decodebin
的src pad是a dynamic (sometimes) pad。当decodebin
获得源代码时,您必须将audioconvert
与decodebin
连接。
您可以使用gst-inspect-1.0
:
$ gst-inspect-1.0 decodebin
:
:
Pad Templates:
SRC template: 'src_%u'
Availability: Sometimes
Capabilities:
ANY
:
:
将pad-added
的回调函数添加到decodebin
并链接到回调中的audioconvert
。所需的变更基本上是这样的:
--- orig.c 2017-01-18 13:35:50.434605255 +0900
+++ new.c 2017-01-18 14:04:16.428847528 +0900
@@ -31,6 +31,21 @@
return TRUE;
}
+static void cb_new_pad (GstElement *element, GstPad *pad, gpointer data)
+{
+ gchar *name;
+ GstElement *other = data;
+
+ name = gst_pad_get_name (pad);
+ g_print ("A new pad %s was created for %s\n", name, gst_element_get_name(element));
+ g_free (name);
+
+ g_print ("element %s will be linked to %s\n",
+ gst_element_get_name(element),
+ gst_element_get_name(other));
+ gst_element_link(element, other);
+}
+
gint main (gint argc, gchar *argv[])
{
GMainLoop *loop;
@@ -59,9 +74,13 @@
gst_bin_add_many (GST_BIN (pipeline), src, dec, conv, sink, NULL);
gst_element_link (src, dec);
- gst_element_link (dec, conv);
gst_element_link (conv, sink);
+ /* you don't link them here */
+ /* gst_element_link (dec, conv); */
+ /* add call-back, instead */
+ g_signal_connect (dec, "pad-added", G_CALLBACK (cb_new_pad), conv);
+
/* run */
gst_element_set_state (pipeline, GST_STATE_PLAYING);
g_main_loop_run (loop);
顺便说一句,你不必自己动手,但让gst_parse_launch()
处理上述所有事情。
int main(int argc, char *argv[])
{
GstElement *pipeline;
GError *err = NULL;
GstBus *bus;
GMainLoop *loop;
gst_init(&argc, &argv);
loop = g_main_loop_new(NULL, FALSE);
pipeline = gst_parse_launch("tcpserversrc host=127.0.0.1 port=5000 ! decodebin ! audioconvert ! alsasink", &err);
gst_element_set_state(pipeline, GST_STATE_PLAYING);
bus = gst_element_get_bus(pipeline);
gst_bus_add_watch (bus, bus_call, loop);
g_main_loop_run(loop);
return 0;
}
有关stackoverflow上动态打击垫的一些问题: