gst 笔记:信号、消息、事件、状态
作者:互联网
基本操作示例
gst-launch-1.0 filesrc location="/home/e0005055/Videos/test1.mp4" ! decodebin ! videoconvert ! autovideosink
代码:
#include <gst/gst.h>
static gboolean
bus_call (GstBus *bus,
GstMessage *msg,
gpointer data)
{
GMainLoop *loop = 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 = NULL;
GError *err = NULL;
gst_message_parse_error (msg, &err, &debug);
g_print ("Error: %s\n", err->message);
g_error_free (err);
if (debug) {
g_print ("Debug details: %s\n", debug);
g_free (debug);
}
g_main_loop_quit (loop);
break;
}
default:
break;
}
return TRUE;
}
static void
on_pad_added (GstElement *element,
GstPad *pad,
gpointer data)
{
GstPad *sinkpad;
GstElement *decoder = (GstElement *) data;
/* We can now link this pad with the vorbis-decoder sink pad */
g_print ("Dynamic pad created, linking demuxer/decoder\n");
sinkpad = gst_element_get_static_pad (decoder, "sink");
gst_pad_link (pad, sinkpad);
gst_object_unref (sinkpad);
}
gint main (gint argc,gchar *argv[])
{
GstStateChangeReturn ret;
GstElement *pipeline, *filesrc, *decoder, *filter, *sink;
GstElement *video_convert, *convert2, *resample;
GMainLoop *loop;
GstBus *bus;
guint watch_id;
g_print ("in %d !\n",__LINE__);
/* initialization */
gst_init (&argc, &argv);
loop = g_main_loop_new (NULL, FALSE);
if (argc != 2) {
g_print ("Usage: %s <mp3 filename>\n", argv[0]);
return 01;
}
/* create elements */
pipeline = gst_pipeline_new ("my_pipeline");
/* watch for messages on the pipeline's bus (note that this will only
* work like this when a GLib main loop is running) */
bus = gst_pipeline_get_bus (GST_PIPELINE (pipeline));
watch_id = gst_bus_add_watch (bus, bus_call, loop);
gst_object_unref (bus);
filesrc = gst_element_factory_make ("filesrc", "my_filesource");
decoder = gst_element_factory_make ("decodebin", "my_decoder");
video_convert = gst_element_factory_make ("videoconvert", "my_videoconvert");
sink = gst_element_factory_make ("autovideosink", "my_autovideosink");
if (!sink || !decoder) {
g_print ("Decoder or output could not be found - check your install\n");
return -1;
} else if (!video_convert) {
g_print ("Could not create audioconvert or audioresample element, "
"check your installation\n");
return -1;
}
g_object_set (G_OBJECT (filesrc), "location", argv[1], NULL);
gst_bin_add_many (GST_BIN (pipeline), filesrc, decoder, video_convert, sink, NULL);
/* link everything together */
if (!gst_element_link_many (filesrc, decoder, NULL)) {
g_print ("Failed to link one or more elements!\n");
return -1;
}
if (!gst_element_link_many (video_convert, sink, NULL)) {
g_print ("Failed to link one or more elements!\n");
return -1;
}
g_signal_connect (decoder, "pad-added", G_CALLBACK (on_pad_added), video_convert);
/* run */
ret = gst_element_set_state (pipeline, GST_STATE_PLAYING);
if (ret == GST_STATE_CHANGE_FAILURE) {
GstMessage *msg;
g_print ("Failed to start up pipeline!\n");
/* check if there is an error message with details on the bus */
msg = gst_bus_poll (bus, GST_MESSAGE_ERROR, 0);
if (msg) {
GError *err = NULL;
gst_message_parse_error (msg, &err, NULL);
g_print ("ERROR: %s\n", err->message);
g_error_free (err);
gst_message_unref (msg);
}
g_print ("in %d !\n",__LINE__);
return -1;
}
g_main_loop_run (loop);
/* clean up */
gst_element_set_state (pipeline, GST_STATE_NULL);
gst_object_unref (pipeline);
g_source_remove (watch_id);
g_main_loop_unref (loop);
return 0;
}
信号处理
通过g_signal_connect 连接信号,设定callback函数。
g_signal_connect (decoder, "pad-added", G_CALLBACK (on_pad_added), video_convert);
其中,元素的信号可以通过gst-inspect-1.0 decodebin查看:
# gst-inspect-1.0 decodebin
Factory Details:
Rank none (0)
Long-name Decoder Bin
Klass Generic/Bin/Decoder
……
Element Signals:
"pad-added" : void user_function (GstElement* object,
GstPad* arg0,
gpointer user_data);
"pad-removed" : void user_function (GstElement* object,
GstPad* arg0,
gpointer user_data);
"no-more-pads" : void user_function (GstElement* object,
gpointer user_data);
"unknown-type" : void user_function (GstElement* object,
GstPad* arg0,
GstCaps* arg1,
gpointer user_data);
属性设定
通过g_object_set或者g_object_get读写属性。具体属性可以通过gst-inspect-1.0 [插件名] 来查看:
# gst-inspect-1.0 filesrc
Factory Details:
Rank primary (256)
Long-name File Source
Klass Source/File
......
Element Properties:
blocksize : Size in bytes to read per buffer (-1 = default)
flags: readable, writable
Unsigned Integer. Range: 0 - 4294967295 Default: 4096
do-timestamp : Apply current stream time to buffers
flags: readable, writable
Boolean. Default: false
location : Location of the file to read
flags: readable, writable, changeable only in NULL or READY state
可以看到flag选项标明了读写条件,后面是数据类型、取值范围和默认值。
消息message
消息是从bus中获取,可以注册消息,也可以轮询消息。
使用gst_bus_add_watch +callback函数关注消息:
bus = gst_pipeline_get_bus (GST_PIPELINE (pipeline));
watch_id = gst_bus_add_watch (bus, bus_call, loop);
static gboolean bus_call (GstBus *bus, GstMessage *msg, gpointer data)
{
GMainLoop *loop = data;
switch (GST_MESSAGE_TYPE (msg)) {
case GST_MESSAGE_EOS:
break;
case GST_MESSAGE_ERROR:
break;
default:
break;
}
return TRUE;
}
使用gst_bus_poll轮询消息:
msg = gst_bus_poll (bus, GST_MESSAGE_ERROR, 0);
if (msg) {
GError *err = NULL;
gst_message_parse_error (msg, &err, NULL);
g_print ("ERROR: %s\n", err->message);
g_error_free (err);
gst_message_unref (msg);
}
状态
通过gst_element_set_state设定管道状态:
gst_element_set_state (pipeline, GST_STATE_PLAYING);
……
gst_element_set_state (pipeline, GST_STATE_NULL);
事件
事件通过管道来控制上下游的节点,通过gst_element_send_event 来设定事件:
static void
seek_to_time (GstElement *element, guint64 time_ns)
{
GstEvent *event;
event = gst_event_new_seek (1.0, GST_FORMAT_TIME,
GST_SEEK_FLAG_NONE,
GST_SEEK_METHOD_SET, time_ns,
GST_SEEK_TYPE_NONE, G_GUINT64_CONSTANT (0));
gst_element_send_event (element, event);
}
衬垫控制
按照“always”、“sometimes”、“request”情况来使用衬垫。
always衬垫
因为该衬垫一直存在,可以在任何时候连接。
gst_element_link_pads (source, "src", demux, "sink");
sometimes衬垫
在特定条件下才存在,所以在"pad-added"信号发出时连接。
g_signal_connect (demux, "pad-added", G_CALLBACK (cb_new_pad), decoder );
……
on_pad_added (GstElement *element,
GstPad *pad,
gpointer data)
{
GstPad *sinkpad;
GstElement *decoder = (GstElement *) data;
/* We can now link this pad with the vorbis-decoder sink pad */
g_print ("Dynamic pad created, linking demuxer/decoder\n");
sinkpad = gst_element_get_static_pad (decoder, "sink");
gst_pad_link (pad, sinkpad);
gst_object_unref (sinkpad);
}
request衬垫
request衬垫是按需求创建。使用gst_element_get_request_pad函数来申请衬垫:
static void some_function (GstElement * tee)
{
GstPad *pad;
gchar *name;
pad = gst_element_get_request_pad(tee, "src%d");
name = gst_pad_get_name (pad);
g_print ("A new pad %s was created\n", name);
g_free (name);
//链接衬垫
gst_object_unref (GST_OBJECT (pad));
注意官网教程api为gst_element_request_pad_simple,应该是被废弃了不能使用。
标签:gst,bus,笔记,element,pad,信号,decoder,GST 来源: https://blog.csdn.net/yuangc/article/details/119144441