Hello, everyone. I am facing a problem: when the h264 bytes stream changes the resolution, my pipeline stop working. How can I solve this problem?
// Init Gstreamer basic
gst_init(nullptr,nullptr);
// init pipeline element
pipeline = gst_pipeline_new("pipeline");
appsrc = gst_element_factory_make("appsrc", "appsrc");
h264parse = gst_element_factory_make("h264parse", "h264parse");
decoder = gst_element_factory_make("nvv4l2decoder", "decoder");
nvvidconv = gst_element_factory_make("nvvidconv", "nvvidconv");
videoconvert = gst_element_factory_make("videoconvert", "videoconvert");
appsink = gst_element_factory_make("appsink", "appsink");
if (!pipeline || !appsrc || !h264parse || !decoder || !nvvidconv || !videoconvert || !appsink) {
ERROR("Create element fail in Gstreamer!\n");
return -2;
}
// add all element to the pipeline
gst_bin_add_many(GST_BIN(pipeline), appsrc, h264parse, decoder, nvvidconv, videoconvert, appsink, NULL);
gst_element_link_many(h264parse, decoder, nvvidconv, NULL);
// Add Caps in appsrc
GstCaps *appsrc_caps = gst_caps_new_simple(
"video/x-h264",
"width", G_TYPE_INT, 1920,
"height", G_TYPE_INT, 1080,
"framerate", GST_TYPE_FRACTION, 30, 1,
"stream-format", G_TYPE_STRING, "byte-stream",
NULL);
//g_object_set(G_OBJECT(appsrc), "caps", appsrc_caps, NULL);
if (!gst_element_link_filtered(appsrc, h264parse, appsrc_caps)) {
ERROR("Failed to link appsrc to h264parse with filter.");
gst_caps_unref(appsrc_caps);
return -3;
}
gst_caps_unref(appsrc_caps);
// change features in h264parse
g_object_set(G_OBJECT(h264parse), "config-interval", 1, NULL);
// add caps between nvvidconv and videoconvert
GstCaps *nvvidconv_caps = gst_caps_new_simple("video/x-raw",
"format", G_TYPE_STRING, "BGRx",
NULL);
if (!gst_element_link_filtered(nvvidconv, videoconvert, nvvidconv_caps)) {
ERROR("Failed to link nvvidconv to videoconvert with filter.");
gst_caps_unref(nvvidconv_caps);
return -3;
}
gst_caps_unref(nvvidconv_caps);
// add caps between videoconvert and appsink
GstCaps *appsink_caps = gst_caps_new_simple("video/x-raw",
"format", G_TYPE_STRING, "BGR",
NULL);
if (!gst_element_link_filtered(videoconvert, appsink, appsink_caps)) {
ERROR("Failed to link videoconvert to appsink with filter.");
gst_caps_unref(appsink_caps);
return -4;
}
gst_caps_unref(appsink_caps);
// set appsink festures
g_object_set(appsink, "emit-signals", TRUE, "sync", FALSE, NULL);
//g_signal_connect(appsink, "new-sample", G_CALLBACK(on_new_image_static), NULL);
g_signal_connect(appsink, "new-sample", G_CALLBACK(on_new_image_static), this);
GstStateChangeReturn ret = gst_element_set_state(pipeline, GST_STATE_PLAYING);
if (ret == GST_STATE_CHANGE_FAILURE) {
ERROR("Failed to set pipeline to PLAYING state.");
return -1;
}