gst中, srcpad buffer探针附加字符串, 下一个插件srcpad buffer探针获取, c实现, 使用gst_buffer_add_custom_meta
时间: 2023-05-11 20:01:48 浏览: 317
可以使用 gst_buffer_add_meta() 函数来添加自定义的 meta 数据。具体实现可以参考以下代码:
```
#include <gst/gst.h>
typedef struct {
GstMeta meta;
gchar *data;
} CustomMeta;
static GstMetaInfo *custom_meta_info = NULL;
static void custom_meta_init(GstBuffer *buffer, gpointer data)
{
CustomMeta *meta = (CustomMeta *)data;
meta->data = NULL;
}
static void custom_meta_free(GstBuffer *buffer, gpointer data)
{
CustomMeta *meta = (CustomMeta *)data;
g_free(meta->data);
meta->data = NULL;
}
static gboolean custom_meta_transform(GstBuffer *buffer, GstMeta *meta, GstBuffer *dest)
{
CustomMeta *src_meta = (CustomMeta *)meta;
CustomMeta *dest_meta = NULL;
dest_meta = (CustomMeta *)gst_buffer_add_meta(dest, custom_meta_info, NULL);
dest_meta->data = g_strdup(src_meta->data);
return TRUE;
}
static CustomMeta *custom_meta_new(const gchar *data)
{
CustomMeta *meta = NULL;
meta = (CustomMeta *)gst_meta_new(custom_meta_info, NULL);
meta->data = g_strdup(data);
return meta;
}
static gboolean custom_meta_add(GstBuffer *buffer, const gchar *data)
{
CustomMeta *meta = NULL;
meta = custom_meta_new(data);
gst_buffer_add_meta(buffer, (GstMeta *)meta, NULL);
return TRUE;
}
static CustomMeta *custom_meta_get(GstBuffer *buffer)
{
CustomMeta *meta = NULL;
meta = (CustomMeta *)gst_buffer_get_meta(buffer, custom_meta_info);
return meta;
}
static void custom_meta_register(void)
{
custom_meta_info = gst_meta_register(
"CustomMeta",
sizeof(CustomMeta),
custom_meta_init,
custom_meta_free,
custom_meta_transform
);
}
static gboolean srcpad_buffer_probe(GstPad *pad, GstPadProbeInfo *info, gpointer user_data)
{
GstBuffer *buffer = GST_PAD_PROBE_INFO_BUFFER(info);
CustomMeta *meta = NULL;
meta = custom_meta_get(buffer);
if (meta != NULL) {
g_print("CustomMeta data: %s\n", meta->data);
}
return TRUE;
}
static gboolean next_plugin_srcpad_buffer_probe(GstPad *pad, GstPadProbeInfo *info, gpointer user_data)
{
GstBuffer *buffer = GST_PAD_PROBE_INFO_BUFFER(info);
CustomMeta *meta = NULL;
meta = custom_meta_get(buffer);
if (meta != NULL) {
g_print("CustomMeta data: %s\n", meta->data);
}
return TRUE;
}
int main(int argc, char *argv[])
{
GstElement *pipeline, *src, *next_plugin;
GstPad *srcpad, *next_plugin_srcpad;
GstBus *bus;
GstMessage *msg;
GstStateChangeReturn ret;
/* Initialize GStreamer */
gst_init(&argc, &argv);
/* Register custom meta */
custom_meta_register();
/* Create the elements */
pipeline = gst_pipeline_new("pipeline");
src = gst_element_factory_make("videotestsrc", "src");
next_plugin = gst_element_factory_make("next_plugin", "next_plugin");
/* Add elements to the pipeline */
gst_bin_add_many(GST_BIN(pipeline), src, next_plugin, NULL);
/* Link the elements */
gst_element_link(src, next_plugin);
/* Get pads */
srcpad = gst_element_get_static_pad(src, "src");
next_plugin_srcpad = gst_element_get_static_pad(next_plugin, "src");
/* Add buffer probes */
gst_pad_add_probe(srcpad, GST_PAD_PROBE_TYPE_BUFFER, srcpad_buffer_probe, NULL, NULL);
gst_pad_add_probe(next_plugin_srcpad, GST_PAD_PROBE_TYPE_BUFFER, next_plugin_srcpad_buffer_probe, NULL, NULL);
/* Start playing */
ret = gst_element_set_state(pipeline, GST_STATE_PLAYING);
if (ret == GST_STATE_CHANGE_FAILURE) {
g_printerr("Failed to start pipeline\n");
return -1;
}
/* Wait until error or EOS */
bus = gst_element_get_bus(pipeline);
msg = gst_bus_timed_pop_filtered(bus, GST_CLOCK_TIME_NONE, GST_MESSAGE_ERROR | GST_MESSAGE_EOS);
/* Free resources */
if (msg != NULL) {
gst_message_unref(msg);
}
gst_object_unref(bus);
gst_element_set_state(pipeline, GST_STATE_NULL);
gst_object_unref(pipeline);
return 0;
}
```
在 srcpad_buffer_probe() 函数中,我们可以使用 custom_meta_get() 函数获取 buffer 中的自定义 meta 数据。在 next_plugin_srcpad_buffer_probe() 函数中同样可以获取自定义 meta 数据。在这个例子中,我们只是简单地打印出自定义 meta 数据,你可以根据实际需求进行处理。
阅读全文