嗨, 当我尝试以下脚本时,它不起作用:appsink示例为空。但是,当我直接将“ videotestsrc”链接到“ videoconvert-x264enc-appsink”时,它可以工作,而当我直接将“ appsrc”连接到“ appsink”时,它也可以工作...问题是什么?谢谢。
gst_init(NULL,NULL);
使用videotestsrc具有数据缓冲区:
GstElement *pipeline2;
pipeline2 = gst_pipeline_new("pipeline");
GstElement *appsink2;
appsink2 = gst_element_factory_make("appsink", "BufferSink2");
GstElement *videotestsrc = gst_element_factory_make("videotestsrc", "source");
g_object_set(G_OBJECT(videotestsrc), "num-buffers", 1, NULL);
gst_bin_add_many(GST_BIN(pipeline2), videotestsrc, appsink2, NULL);
GstCaps *caps2 = gst_caps_new_simple("video/x-raw", "format", G_TYPE_STRING, "GRAY8", "width", G_TYPE_INT, 100, "height", G_TYPE_INT, 100, NULL);
gst_element_link_filtered(videotestsrc, appsink2, caps2);
GstStateChangeReturn retPip2 = gst_element_set_state(pipeline2, GST_STATE_PLAYING);
GstSample *sample2 = gst_app_sink_pull_sample (GST_APP_SINK(appsink2));
GstBuffer *buffer2= gst_sample_get_buffer(sample2);
GstMapInfo map2;
gst_buffer_map(buffer2, &map2, GST_MAP_READ);
uchar source[map2.size];
memmove(source, map2.data, map2.size);
将数据放入appsrc中:
int size = map2.size;
GstFlowReturn ret;
GstElement *appsrc;
appsrc = gst_element_factory_make("appsrc", "appsrc");
g_object_set(G_OBJECT(appsrc), "blocksize", size, NULL);
g_object_set(G_OBJECT(appsrc), "num-buffers", 1, NULL);
GstBuffer *buffer;
buffer = gst_buffer_new_allocate(NULL,size, NULL);
GstMapInfo map;
gst_buffer_map(buffer, &map, GST_MAP_WRITE);
uchar *buf = map.data;
memcpy(buf, source, size);
ret = gst_app_src_push_buffer(GST_APP_SRC(appsrc), buffer);
g_object_set(G_OBJECT(appsrc), "caps", gst_caps_new_simple("video/x-raw", "format", G_TYPE_STRING, "GRAY8", "width", G_TYPE_INT, 100, "height", G_TYPE_INT, 100, NULL), NULL);
设置上限:
GstCaps *cap_appsrc_to_x264enc;
cap_appsrc_to_x264enc = gst_caps_new_simple("video/x-raw", "format", G_TYPE_STRING, "GRAY8", "width", G_TYPE_INT, 100, "height", G_TYPE_INT, 100, NULL);
GstCaps *cap_x264enc_to_sink;
cap_x264enc_to_sink = gst_caps_new_simple("video/x-h264", "profile", G_TYPE_STRING, "main", NULL);
创建对象:
GstElement *x264enc = gst_element_factory_make("x264enc", "encoder");
g_object_set(G_OBJECT(x264enc), "qp-max", 20, NULL);
g_object_set(G_OBJECT(x264enc), "pass", 17, NULL); // VBR Pass 1
g_object_set(G_OBJECT(x264enc), "bitrate", 2000, NULL);
GstCaps *caps;
caps = gst_caps_new_simple("video/x-raw", "format", G_TYPE_STRING, "I420", NULL);
GstElement *appsink;
appsink = gst_element_factory_make("appsink", "BufferSink");
GstElement *convert = gst_element_factory_make("videoconvert", "convert");
管道:
GstElement *pipeline;
pipeline = gst_pipeline_new("pipeline");
gst_bin_add_many(GST_BIN(pipeline), appsrc, convert, x264enc, appsink, NULL);
gst_element_link_filtered(appsrc, convert, cap_appsrc_to_x264enc);
gst_element_link_filtered(convert, x264enc, caps);
gst_element_link_filtered(x264enc,appsink, cap_x264enc_to_sink);
GstStateChangeReturn retPip = gst_element_set_state(pipeline, GST_STATE_PLAYING);
使用appsink将数据放入缓冲区:
GstSample *sample = gst_app_sink_pull_sample (GST_APP_SINK(appsink));
GstBuffer *encBuffer= gst_sample_get_buffer(sample);
gsize senc = gst_buffer_get_size (encBuffer);
GstMapInfo mapEnc;
gst_buffer_map(encBuffer, &mapEnc, GST_MAP_READ);
uchar pRet[mapEnc.size];
memmove(pRet, mapEnc.data, mapEnc.size);