Apalis imx6 Gstreamer Webcam streaming via gst library

Hello, I was able to stream USB webcam via

 gst-launch-1.0 v4l2src device=/dev/video0 ! 'video/x-raw,width=640,height=480,framerate=30/1' ! videoconvert ! fbdevsink

However, when I try to use gst library with following basic gst cpp code,

#include <gst/gst.h>
#include <iostream>
using namespace std;

int main(int argc, char *argv[]) {
    GstElement *pipeline, *source, *sink;
    GstBus *bus;
    GstMessage *msg;
    GMainLoop *loop;
    GstStateChangeReturn ret;

    //initialize all elements
    gst_init(&argc, &argv);
    pipeline = gst_pipeline_new ("pipeline");
    source = gst_element_factory_make ("autovideosrc", "source");
    sink = gst_element_factory_make ("autovideosink", "sink");

    //check for null objects
    if (!pipeline || !source || !sink) {
        cout << "not all elements created: pipeline["<< !pipeline<< "]" << "source["<< !source<< "]" << "sink["<< !sink << "]" << endl;
        return -1;

    //set video source
    g_object_set(G_OBJECT (source), "location", argv[1], NULL);
    cout << "==>Set video source." << endl;
    g_object_set(G_OBJECT (sink), "sync", FALSE, NULL);
    cout << "==>Set video sink." << endl;

    //add all elements together
    gst_bin_add_many (GST_BIN (pipeline), source, sink, NULL);
    if (gst_element_link (source, sink) != TRUE) {
        cout << "Elements could not be linked." << endl;
        gst_object_unref (pipeline);
        return -1;
    cout << "==>Link elements." << endl;

    //set the pipeline state to playing
    ret = gst_element_set_state (pipeline, GST_STATE_PLAYING);
    if (ret == GST_STATE_CHANGE_FAILURE) {
        cout << "Unable to set the pipeline to the playing state." << endl;
        gst_object_unref (pipeline);
        return -1;
    cout << "==>Set video to play." << endl;

    //get pipeline's bus
    bus = gst_element_get_bus (pipeline);
    cout << "==>Setup bus." << endl;

    loop = g_main_loop_new(NULL, FALSE);
    cout << "==>Begin stream." << endl;

    gst_object_unref (bus);
    gst_element_set_state (pipeline, GST_STATE_NULL);
    gst_object_unref (pipeline);

It shows

not all elements created: pipeline[0] source[1] sink[1]

I installed all of requirement gst libraries with

opkg install gstreamer1.0-plugins-base-* gstreamer1.0-plugins-good-*

But, I don’t have any clue why I can’t make pipeline in the toradex board. Could you give any comments or advice to move forward?

Thank you in advance.

Hi @hkhunkim

Can you once try to start your pipeline with the following code:

gst_parse_launch("v4l2src device=/dev/video0 ! 'video/x-raw,width=640,height=480,framerate=30/1' ! videoconvert ! fbdevsink", NULL);

If this works you can try to do the following:

gst_parse_launch("v4l2src device=/dev/video0 ! 'video/x-raw,width=640,height=480,framerate=30/1' ! videoconvert ! appsink name=myvideo max-buffers=2 drop=false", NULL);
myVideoSink = gst_bin_get_by_name(GST_BIN(mSourcePipeline), "myvideo"); 

I didn’t test it but it’s normally easier to use gst_parse_launch instead of creating the pipeline on your own.