Show camera footage on widget and Send the video out on udp port



  • I am new Qt & Video streaming domain. I have a an requirement to show camera footage on a widget and send the same footage over network using udp.

    I am following link http://www.qtcentre.org/threads/36073-Best-way-to-stream-and-record-a-webcam.

    But it seems that it is not working. I have not seen any packet on udp port and video is stopped in autovideosink. May be I have missed something. Please suggest. Here is the code:

    gst_init (NULL,NULL);
    
    pipeline = gst_pipeline_new ("pipeline");
    g_assert( pipeline);
    
    testSrc = gst_element_factory_make("v4l2src", "camera");
    g_assert(testSrc);
    udp = gst_element_factory_make ("udpsink", "udp");
    g_assert(udp);
    g_object_set(udp , "host", "255.255.255.255", NULL );
    g_object_set(udp , "port", 5000 , NULL );
    g_object_set(testSrc, "device", "/dev/video0", NULL);
    
    videoOut = gst_element_factory_make("autovideosink", "video out");
    g_assert(videoOut);
    
    gst_bin_add_many(GST_BIN( pipeline), testSrc, videoOut, udp, NULL);
    gst_element_link_many( testSrc, videoOut, udp, NULL);
    
    GstStateChangeReturn sret = gst_element_set_state (pipeline, GST_STATE_PLAYING);
    if (sret == GST_STATE_CHANGE_FAILURE)
    {
        gst_element_set_state (pipeline, GST_STATE_NULL);
        gst_object_unref (pipeline);
    }
    

    Also why can't it be done by

    QCamera->setViewfinder(QCameraViewfinder);
    QVideoProbe->setSource(QCamera)

  • Lifetime Qt Champion

    Hi,

    If you'd like to mix GStreamer and Qt I'd recommend taking a look at the QtGstreamer project.

    As for your other question, because you are are trying to use classes rather that instances.



  • @SGaist I am using instances like:

    QCamera *camera = new QCamera(QCameraInfo::availableCameras()[0]);
    QCameraViewfinder *viewfinder = new QCameraViewfinder;
    QVideoProbe* videoProbe = new QVideoProbe(this);
    
    camera->setViewfinder(viewfinder);
    if (videoProbe->setSource(camera))
    {
        qDebug()<<"setSource success";
        // Probing succeeded, videoProbe->isValid() should be true.
        connect(videoProbe, SIGNAL(videoFrameProbed(QVideoFrame)),
                this, SLOT(sendDataToUdp(QVideoFrame)));
    }
    

    But it is not working.


  • Lifetime Qt Champion

    What exactly is "not working" ? You have to give details, "not working" doesn't explain anything useful.



  • @SGaist, I am able to see video data on viewfinder but videoProbe->setSource(camera) is returning false, hence Slot sendDataToUdp is never getting called.


  • Lifetime Qt Champion

    You can see the status of the different backends in the wiki.

    But again, since you already have something working with GStreamer, try the Qt GStreamer module. It provides everything you need to show on a widget and stream at the same time. You'll just have to modify your gst pipeline a bit for that.



  • @gsharma Not to go against SGaist suggestion about the Qt GStreamer module, but I've just tested the brief code below and it works, slot sendDataToUdp is called. For the sake of simplicity I'm not working with the frame as I guess it needs to be converted to some appropriate format (i.e. H264?) so I'm just sending a brief message (see attached ![screenshot](
    Screenshot: https://imgur.com/a/Ujupf))

    MainWindow::MainWindow(QWidget *parent) :
        QMainWindow(parent),
        ui(new Ui::MainWindow)
    {
        ui->setupUi(this);
    
        udpWriter = new QUdpSocket(this);
    
        camera = new QCamera(this);
        viewfinder = new QCameraViewfinder(this);
        camera->setViewfinder(viewfinder);
        setCentralWidget(viewfinder);
        viewfinder->show();
        camera->start();
    
        videoProbe = new QVideoProbe(this);
        if (videoProbe->setSource(camera))
        {
            qDebug() << "setSource success";
            // Probing succeeded, videoProbe->isValid() should be true.
            connect(videoProbe, SIGNAL(videoFrameProbed(QVideoFrame)),
                    this, SLOT(sendDataToUdp(QVideoFrame)));
        }
    }
    
    MainWindow::~MainWindow()
    {
        delete ui;
    }
    
    void MainWindow::sendDataToUdp(const QVideoFrame &videoFrame) {
        Q_UNUSED(videoFrame);
    
        QByteArray datagram = "Broadcasting frame " + QByteArray::number(messageNo);
        udpWriter->writeDatagram(datagram.data(), datagram.size(), QHostAddress::Broadcast, 45454);
        ++messageNo;
    }
    


  • @Pablo-J.-Rogina, I have tested the code. It is not working on QT-5.6.2 but it is working on QT-5.9.1.



  • @gsharma Ok, I forgot to mention my test environment, sorry about that.
    Well, maybe it's a good reason to update your Qt version...
    Best regards.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.