Deepstream SDK 4.0 :call API to save deepstream-test1 error

command line for DeepStream 4.0 is right:
gst-launch-1.0 filesrc location= sample_720p.h264 ! h264parse ! nvv4l2decoder ! m.sink_0 nvstreammux name=m width=1920 height=1080 batch-size=1 ! nvinfer config-file-path=./config/dstest2_pgie_config.txt ! nvvideoconvert ! nvdsosd ! queue ! nvvideoconvert ! “video/x-raw(memory:NVMM), format=I420” ! nvv4l2h264enc ! h264parse ! qtmux ! filesink location=out.mp4

but when call API, report the following error:
ERROR from element h264parser: Internal data stream error.
Error details: gstbaseparse.c(3611): gst_base_parse_loop (): /GstPipeline:demo-pipeline/GstH264Parse:h264parser:
streaming stopped, reason not-linked (-1)

Could you please specify how you run deepstream-test1?

gst_init(&argc, &argv);
loop = g_main_loop_new(NULL, FALSE);

pipeline = gst_pipeline_new("demo-pipeline");   
source = gst_element_factory_make("filesrc", "file-source");   
h264parser = gst_element_factory_make("h264parse", "h264parser");   
decoder = gst_element_factory_make ("nvv4l2decoder", "nvv4l2-decoder"); 
streammux = gst_element_factory_make("nvstreammux", "stream-muxer");   
pgie = gst_element_factory_make("nvinfer", "primary-nvinference-engine");
nvvidconv = gst_element_factory_make("nvvideoconvert", "nvvideo-converter->RGBA");   
nvosd = gst_element_factory_make("nvdsosd", "nv-onscreendisplay");  

queue1 = gst_element_factory_make("queue", "queue1");
nvvidconv2 = gst_element_factory_make("nvvideoconvert", "nvvideo-converter->NV12");  

filter1 = gst_element_factory_make("capsfilter", "capsfilter-NV12");   
caps1 = gst_caps_from_string ("video/x-raw(memory:NVMM), format=I420");
g_object_set (G_OBJECT (filter1), "caps", caps1, NULL);
gst_caps_unref (caps1);	
sink = gst_element_factory_make("fakesink", "fakesink");
encoder = gst_element_factory_make("nvv412h264enc", "nvv412-encoder");
h264parser2 = gst_element_factory_make("h264parse", "h264parse2");
qtmux = gst_element_factory_make("MP4mux", "muxer");
	g_printerr("Unable to create code parser\n");
g_object_set(G_OBJECT(source), "location", input_files[0], NULL);	
g_object_set(G_OBJECT(streammux), "width", 1920, "height", 1080,
			"batch-size",1, "batched-push-timeout",4000000,NULL);	
g_object_set(G_OBJECT(pgie), "config-file-path", "./config/dstest2_pgie_config.txt", 
				"unique-id", 1, NULL);	
g_object_set (G_OBJECT (sink), "location", "out.mp4",  "sync", 1,NULL);		
g_object_set(G_OBJECT(encoder), "bitrate", 2000000, NULL);
bus = gst_pipeline_get_bus(GST_PIPELINE(pipeline));
bus_watch_id = gst_bus_add_watch(bus, bus_call, loop);
	source, h264parser, decoder, streammux, nvvidconv, nvosd, queue1, nvvidconv2, filter1, encoder, h264parser2,qtmux,sink, NULL);		
gchar pad_name_sink[16] = "sink_0";
gchar pad_name_src[16] = "src";	
sinkpad = gst_element_get_request_pad(streammux, pad_name_sink);   
if (!sinkpad){
	g_printerr ("Streammux request sink pad failed. Exiting.\n");
	return -1;
srcpad = gst_element_get_static_pad(decoder, pad_name_src);
if (!srcpad){
	g_printerr ("Decoder request src pad failed. Exiting.\n");
if (gst_pad_link(srcpad, sinkpad) != GST_PAD_LINK_OK){
     g_printerr ("Failed to link decoder to stream muxer. Exiting.\n");
	return -1;
if (!gst_element_link_many(source, h264parser, decoder, NULL)){
   g_printerr ("Elements could not be linked: 1. Exiting.\n");
	return -1;
if (!gst_element_link_many(streammux, nvvidconv, nvosd, queue1, nvvidconv2, filter1, encoder, h264parser2, qtmux,sink, NULL)){
    g_printerr ("Elements could not be linked: 2. Exiting.\n");
	return -1;

osd_sink_pad = gst_element_get_static_pad(sink, "sink");
if (!osd_sink_pad)
	g_print("Unable to get sink pad\n");
	gst_pad_add_probe(osd_sink_pad, GST_PAD_PROBE_TYPE_BUFFER, osd_sink_pad_buffer_probe, NULL, NULL);

Thanks for your reply.
I just started studying DeepstreamSDK, when i try to save h264 it report error, However, the command line runs successfully in Gst-launch-1.0. Main code is above.

Seems you are attampting to save output to a file based on deepstream-test1 sample, right? if it’s the case, please refer this topic

or you can try deepstream-app which have controllable configuration where you can set sink type to 3 File,
and you can see the code for how to save output to file.

i try deepstream-app successful, but now i want to save based on deepstream-test1 sample for DS4.0.1.
the first topic you reffered is mainly for DS3.0.
i also try the code in “deepstream_sink_bin.c”, but it still reports error:

ERROR from element h264parser: Internal data stream error.
Error details: gstbaseparse.c(3611): gst_base_parse_loop (): /GstPipeline:demo-pipeline/GstH264Parse:h264parser:
streaming stopped, reason not-linked (-1)

i have notice that if i don’t add …! nvv412h264enc ! h264parse ! qtmux …, it run successful.

yeah, that link is for version 3.0, you need to change the element accordingly, or you also can refer to source
sources/apps/apps-common/src/deepstream_sink_bin.c::create_encode_file_bin for how to save to file.

Thank you very much. I have solved the problem!