Tool/software: Linux
hello:
I want to save the h264 file using appsink.
Our gstreamer pipeline is like below:
V4l2src—>vpe —> ducatih264enc —>appsink
Here's the code I wrote:
#define WIDTH 1280
#define HEIGHT 720
GstFlowReturn sink_new_sample(GstElement* object, gpointer user_data)
{
GstSample *sample;
GstBuffer *buffer;
GstCaps *caps;
GstStructure *s;
int width, height;
int res;
sample = gst_app_sink_pull_sample(GST_APP_SINK(object));
if (sample == 0)
{
return GST_FLOW_OK;
}
caps = gst_sample_get_caps(sample);
if(!caps){
printf("could not get snapshot format\n");
}
s = gst_caps_get_structure(caps, 0);
res = gst_structure_get_int(s, "width", &width);
res |= gst_structure_get_int(s, "height", &height);
if(!res){
printf("could not get snapshot dimension\n");
}
printf("width = %d, height = %d\n", width, height);
buffer = gst_sample_get_buffer(sample);
if (buffer == 0)
{
return GST_FLOW_OK;
}
GstMapInfo map;
if (gst_buffer_map(buffer, &map, GST_MAP_READ))
{
printf("======= save h264 ======\n");
static FILE *fp = 0;
fp = fopen("./test.h264", "wb");
if (fp != NULL)
{
fwrite(map.data, 1, map.size, fp);
//fclose(fp);
}
gst_buffer_unmap(buffer, &map);
}
gst_sample_unref(sample);
return GST_FLOW_OK;
}
int main(int argc, char *argv[])
{
GstElement *pipeline, *v4l2src, *filter, *vpe, *filter1, *ducatih264enc, *queue, *appsink;
GstCaps *caps, *caps1;
static GMainLoop *loop;
gst_init(&argc, &argv);
loop = g_main_loop_new(NULL, FALSE);
pipeline = gst_pipeline_new("pipeline");
if (!pipeline) {
g_printerr("%d gst_pipeline_new error \n", __LINE__);
return -1;
}
v4l2src = gst_element_factory_make("v4l2src", "v4l2src0");
if (!v4l2src) {
g_printerr("%d gst_element_factory_make error \n", __LINE__);
}
filter = gst_element_factory_make("capsfilter", "capsfilter0");
if (!filter) {
g_printerr("%d gst_element_factory_make error \n", __LINE__);
}
vpe = gst_element_factory_make("vpe", "vpe0");
if (!vpe) {
g_printerr("%d gst_element_factory_make error \n", __LINE__);
}
filter1 = gst_element_factory_make("capsfilter", "capsfilter1");
if (!filter1) {
g_printerr("%d gst_element_factory_make error \n", __LINE__);
}
ducatih264enc = gst_element_factory_make("ducatih264enc", "ducatih264enc0");
if (!ducatih264enc) {
g_printerr("%d gst_element_factory_make error \n", __LINE__);
}
queue = gst_element_factory_make("queue", "queue0");
if (!queue) {
g_printerr("%d gst_element_factory_make error \n", __LINE__);
}
appsink = gst_element_factory_make("appsink", "appsink0");
if (!appsink) {
g_printerr("%d gst_element_factory_make error \n", __LINE__);
}
GstBus *bus;
bus = gst_pipeline_get_bus((GstPipeline*)pipeline);
gst_bus_add_watch(bus, (GstBusFunc)bus_callback, 0);
gst_object_unref(bus);
g_object_set(G_OBJECT(v4l2src), "device", "/dev/video1", NULL);
g_object_set(G_OBJECT(v4l2src), "io-mode", 4, NULL);
caps = gst_caps_new_simple("video/x-raw",
"format", G_TYPE_STRING, "YUY2",
"width", G_TYPE_INT, WIDTH,
"height", G_TYPE_INT, HEIGHT,
"framerate", GST_TYPE_FRACTION, 60, 1,
NULL);
g_object_set(G_OBJECT(filter), "caps", caps, NULL);
gst_caps_unref(caps);
g_object_set(G_OBJECT(vpe), "num-input-buffers", 8, NULL);
caps1 = gst_caps_new_simple("video/x-raw",
"format", G_TYPE_STRING, "NV12",
"width", G_TYPE_INT, WIDTH,
"height", G_TYPE_INT, HEIGHT,
"framerate", GST_TYPE_FRACTION, 60, 1,
NULL);
g_object_set(G_OBJECT(filter1), "caps", caps1, NULL);
gst_caps_unref(caps1);
//appsink
printf("====== appsink ====== \n");
GstCaps *sink_caps;
sink_caps = gst_caps_new_simple("video/x-raw",
"format",G_TYPE_STRING,"NV12",
"width",G_TYPE_INT,WIDTH,
"height",G_TYPE_INT,HEIGHT,
"framerate", GST_TYPE_FRACTION, 60, 1,
NULL);
g_object_set(appsink, "emit-signals", TRUE, "caps", sink_caps,"sync", FALSE, NULL);
g_signal_connect(appsink, "new-sample", G_CALLBACK(sink_new_sample), NULL);
gst_bin_add_many(GST_BIN(pipeline), v4l2src, filter, vpe, filter1, ducatih264enc, queue, appsink, NULL);
gst_element_link_many(v4l2src, filter, vpe, filter1, ducatih264enc, queue, appsink, NULL);
gst_element_set_state(pipeline, GST_STATE_PLAYING);
g_main_loop_run(loop);
g_main_loop_unref(loop);
return 0;
}
But I can not collect data,what is the reason?