欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 房产 > 建筑 > Gstreamer学习3----灌数据给管线之appsrc

Gstreamer学习3----灌数据给管线之appsrc

2024/11/30 10:34:37 来源:https://blog.csdn.net/aaajj/article/details/139726051  浏览:    关键词:Gstreamer学习3----灌数据给管线之appsrc

参考资料

Basic tutorial 8: Short-cutting the pipeline

gstreamer向appsrc发送帧画面的代码_gst appsrc可变帧率-CSDN博客

在官网教程Basic tutorial 8: Short-cutting the pipeline

里面,讲了一个例子,push音频数据给管线,视频的例子更加直观一些,

主要步骤:

注册一个回调,

 g_signal_connect(appsrc, "need-data", G_CALLBACK(cb_need_data), NULL);

回调方法中,设置数据,再push进处理流程中

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

这个g_signal_emit_by_name(appsrc, "push-buffer", buffer, &ret);是个同步阻塞方法,因为ret作为入参,记录执行的情况。

#include <gst/gst.h>
#define VIDEO_WIDTH   384
#define VIDEO_HEIGHT  288
#define VIDEO_FORMAT  "RGB16"
#define PIXEL_SIZE    2static 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;g_print("cb_need_data called\n");static unsigned char color = 0;size = VIDEO_WIDTH * VIDEO_HEIGHT * PIXEL_SIZE;buffer = gst_buffer_new_allocate(NULL, size, NULL);/* this makes the image black/white */// gst_buffer_memset(buffer, 0, white ? 0xFF : 0x00, size);gst_buffer_memset(buffer, 0, color, size);color += 10;// 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);gst_buffer_unref(buffer);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;GMainLoop* loop;/* init GStreamer */gst_init(NULL, NULL);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");videosink = gst_element_factory_make("autovideosink", "videosink");/* setup */g_object_set(G_OBJECT(appsrc), "caps",gst_caps_new_simple("video/x-raw","format", G_TYPE_STRING, VIDEO_FORMAT,"width", G_TYPE_INT, VIDEO_WIDTH,"height", G_TYPE_INT, VIDEO_HEIGHT,"framerate", GST_TYPE_FRACTION, 0, 1,NULL), NULL);gst_bin_add_many(GST_BIN(pipeline), appsrc, conv, videosink, NULL);gst_element_link_many(appsrc, conv, videosink, NULL);/* setup appsrc */g_object_set(G_OBJECT(appsrc),"stream-type", 0,"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;
}

可以看到颜色变化的画面

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com