Hi DaneLLL,
I am trying to use USB camera on Jetson Nano. It runs smoothly with deepstream-app. I also tried using test-1 sample app with the code below but output is choppy. Could you please indicate what I am doing wrong?
Best regards.
static GstElement *
add_source_bin (void)
{
GstElement *src_bin = NULL;
gchar srcbin_name[16] = { };
GstCaps *caps = NULL;
GstCaps *lnvcaps = NULL;
src_bin = gst_bin_new (srcbin_name);
GstElement *src_elem = gst_element_factory_make (NVDS_ELEM_SRC_CAMERA_V4L2, "src_elem");
gchar device[64];
g_snprintf (device, sizeof (device), "/dev/video%d", 0);
g_object_set (G_OBJECT (src_elem), "device", device, NULL);
GstElement *cap_filter = gst_element_factory_make (NVDS_ELEM_CAPS_FILTER, "src_cap_filter");
GstElement *nvvidconv1, *nvvidconv2;
GstCapsFeatures *feature = NULL;
nvvidconv1 = gst_element_factory_make ("videoconvert", "nvvidconv1");
nvvidconv2 = gst_element_factory_make (NVDS_ELEM_VIDEO_CONV, "nvvidconv2");
g_object_set (G_OBJECT (nvvidconv2), "gpu-id", 0, "nvbuf-memory-type", 0, NULL);
caps = gst_caps_new_simple("video/x-raw","format", G_TYPE_STRING, "YUY2",
"width", G_TYPE_INT, 800, "height", G_TYPE_INT, 600,
"framerate", GST_TYPE_FRACTION, 30,1, NULL);
lnvcaps = gst_caps_new_simple("video/x-raw", "format", G_TYPE_STRING,"NV12",
"width", G_TYPE_INT, 480, "height", G_TYPE_INT, 272,
"framerate", GST_TYPE_FRACTION, 30,1, NULL);
feature = gst_caps_features_new ("memory:NVMM", NULL);
gst_caps_set_features (lnvcaps, 0, feature);
g_object_set (G_OBJECT (cap_filter), "caps", lnvcaps, NULL);
gst_bin_add_many (GST_BIN (src_bin), src_elem, nvvidconv1, nvvidconv2, cap_filter, NULL);
if (!gst_element_link_filtered (src_elem, nvvidconv1, caps)){
g_printerr ("Failed to link 'src_elem, nvvidconv1'\n");
return NULL;
}
if (!gst_element_link (nvvidconv1,nvvidconv2)){
g_printerr ("Failed to link 'nvvidconv1, nvvidconv2'\n");
return NULL;
}
if (!gst_element_link (nvvidconv2, cap_filter)){
g_printerr ("Failed to link 'nvvidconv2, cap_filter'\n");
return NULL;
}
GstPad *gstpad = gst_element_get_static_pad (cap_filter, "src");
gst_element_add_pad (src_bin, gst_ghost_pad_new ("src", gstpad));
gst_object_unref(gstpad);
gst_caps_unref (caps);
gst_caps_unref (lnvcaps);
return src_bin;
}
int
main (int argc, char *argv[])
{
GMainLoop *loop = NULL;
GstElement *pipeline = NULL, *streammux = NULL, *sink = NULL, *pgie = NULL, *nvvidconv = NULL,
*nvosd = NULL;
GstElement *transform = NULL;
GstBus *bus = NULL;
guint bus_watch_id;
GstPad *osd_sink_pad = NULL;
/* Standard GStreamer initialization */
gst_init (&argc, &argv);
loop = g_main_loop_new (NULL, FALSE);
/* Create gstreamer elements */
pipeline = gst_pipeline_new ("dstest1-pipeline");
GstElement *source = add_source_bin();
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");
g_object_set (G_OBJECT (nvvidconv), "gpu-id", 0, NULL);
nvosd = gst_element_factory_make ("nvdsosd", "nv-onscreendisplay");
transform = gst_element_factory_make ("nvegltransform", "nvegl-transform");
sink = gst_element_factory_make ("nveglglessink", "nvvideo-renderer");
g_object_set (G_OBJECT (streammux), "width", MUXER_OUTPUT_WIDTH, "height",
MUXER_OUTPUT_HEIGHT, "batch-size", 1,
"batched-push-timeout", MUXER_BATCH_TIMEOUT_USEC, NULL);
g_object_set (G_OBJECT (pgie),
"config-file-path", "dstest1_pgie_config.txt", NULL);
/* we add a message handler */
bus = gst_pipeline_get_bus (GST_PIPELINE (pipeline));
bus_watch_id = gst_bus_add_watch (bus, bus_call, loop);
gst_object_unref (bus);
/* Set up the pipeline */
gst_bin_add_many (GST_BIN (pipeline),
source, streammux, pgie, nvvidconv, nvosd, transform, sink, NULL);
GstPad *sinkpad, *srcpad;
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 (source, pad_name_src);
if (!srcpad) {
g_printerr ("Source request src pad failed. Exiting.\n");
return -1;
}
if (gst_pad_link (srcpad, sinkpad) != GST_PAD_LINK_OK) {
g_printerr ("Failed to link decoder to stream muxer. Exiting.\n");
return -1;
}
gst_object_unref (sinkpad);
gst_object_unref (srcpad);
/* we link the elements together */
if (!gst_element_link_many (streammux, pgie,
nvvidconv, nvosd, transform, sink, NULL)) {
g_printerr ("Elements could not be linked: 2. Exiting.\n");
return -1;
}
/* Set the pipeline to "playing" state */
gst_element_set_state (pipeline, GST_STATE_PLAYING);
/* Wait till pipeline encounters an error or EOS */
g_main_loop_run (loop);
/* Out of the main loop, clean up nicely */
g_print ("Returned, stopping playback\n");
gst_element_set_state (pipeline, GST_STATE_NULL);
g_print ("Deleting pipeline\n");
gst_object_unref (GST_OBJECT (pipeline));
g_source_remove (bus_watch_id);
g_main_loop_unref (loop);
return 0;
}