gstreamer appsink 输出到标准输出, 给出列子
时间: 2023-08-29 18:13:33 浏览: 47
好的,这个问题属于技术问题,我可以回答。下面是一个简单的示例代码,用于将 Gstreamer 管道中的视频帧输出到标准输出:
```
#include <gst/gst.h>
static GstFlowReturn new_sample_handler(GstElement *appsink, gpointer user_data) {
GstSample *sample = NULL;
g_signal_emit_by_name(appsink, "pull-sample", &sample);
if (sample != NULL) {
GstBuffer *buffer = gst_sample_get_buffer(sample);
GstMapInfo info;
gst_buffer_map(buffer, &info, GST_MAP_READ);
fwrite(info.data, 1, info.size, stdout);
gst_buffer_unmap(buffer, &info);
gst_sample_unref(sample);
}
return GST_FLOW_OK;
}
int main(int argc, char *argv[]) {
GstElement *pipeline, *appsrc, *appsink;
GstCaps *caps;
GstBus *bus;
GstMessage *msg;
GMainLoop *loop;
gst_init(&argc, &argv);
loop = g_main_loop_new(NULL, FALSE);
pipeline = gst_pipeline_new("pipeline");
appsrc = gst_element_factory_make("appsrc", "appsrc");
appsink = gst_element_factory_make("appsink", "appsink");
caps = gst_caps_new_simple("video/x-raw",
"format", G_TYPE_STRING, "RGB",
"width", G_TYPE_INT, 640,
"height", G_TYPE_INT, 480,
"framerate", GST_TYPE_FRACTION, 30, 1,
NULL);
gst_app_src_set_caps(GST_APP_SRC(appsrc), caps);
gst_caps_unref(caps);
g_object_set(G_OBJECT(appsink),
"emit-signals", TRUE,
"sync", FALSE,
"max-buffers", 2,
"drop", TRUE,
NULL);
g_signal_connect(appsink, "new-sample", G_CALLBACK(new_sample_handler), NULL);
gst_bin_add_many(GST_BIN(pipeline), appsrc, appsink, NULL);
gst_element_link(appsrc, appsink);
gst_element_set_state(pipeline, GST_STATE_PLAYING);
g_main_loop_run(loop);
gst_element_set_state(pipeline, GST_STATE_NULL);
gst_object_unref(pipeline);
g_main_loop_unref(loop);
return 0;
}
```
这个程序使用 Gstreamer 创建一个简单的管道,通过 appsrc 向其中输出视频流,再通过 appsink 捕获视频流中的帧数据,并通过标准输出输出。注意这里的输出是二进制数据,需要用其他工具解析才能正确显示。