如何在Qwidget中使用GstElement和udpsrc进行gstreamer流

时间:2018-07-26 06:09:57

标签: gstreamer

我正在尝试使用udpsrc在qwidget中嵌入IP摄像机的流。下面的管道工程:

  

gst-launch-1.0 udpsrc port = 20000! application / x-rtp,编码名称= JPEG,有效载荷= 26,宽度= 640,高度= 460! rtpjpegdepay! jpegparse! jpegdec!视频转换!视频规模! ximagesink sync = false

当尝试嵌入qwidget时,它显示的是纯窗口。我的代码如下:

#include <glib.h>
#include <gst/gst.h>
#include <gst/video/videooverlay.h>

#include <QApplication>
#include <QTimer>
#include <QWidget>

int main(int argc, char *argv[])
{
  if (!g_thread_supported ())
    g_thread_init (NULL);

  gst_init (&argc, &argv);
  QApplication app(argc, argv);
  app.connect(&app, SIGNAL(lastWindowClosed()), &app, SLOT(quit ()));

  // prepare the pipeline

  GstElement *pipeline = gst_pipeline_new ("pipeline");
  GstElement *src = gst_element_factory_make ("udpsrc", NULL);
  GstCaps *caps = gst_caps_from_string ("application/x-rtp,encoding-name=JPEG,payload=26,width=640,height=460");

  g_object_set(G_OBJECT(src),
               "port", 20000,
               "caps", caps, NULL);

  GstElement *parser = gst_element_factory_make ("rtpjpegdepay", NULL);
  GstElement *mux = gst_element_factory_make ("jpegparse", NULL);
  GstElement *parse2 = gst_element_factory_make ("jpegdec", NULL);
  GstElement *dec = gst_element_factory_make ("videoconvert", NULL);
  GstElement *conv = gst_element_factory_make ("videoscale", NULL);
  GstElement *sink = gst_element_factory_make ("ximagesink", NULL);
  g_object_set(G_OBJECT(sink), "sync", FALSE, NULL);
  gst_bin_add_many (GST_BIN (pipeline), src, parser, mux, parse2, dec, conv, sink, NULL);
  gst_element_link (src, sink);
 GstState state, pending;

    //this is the call to overlay the gstreamer's output to the Qt Widgets...
    gst_video_overlay_set_window_handle (GST_VIDEO_OVERLAY (sink), xwinid);

    GstBus *bus = gst_pipeline_get_bus (GST_PIPELINE (pipeline));
    gst_object_unref (bus);


    GstStateChangeReturn sret = gst_element_set_state (pipeline, GST_STATE_PLAYING); //Playback can be initiated by setting the element to PLAYING state using gst_element_set_state()

    qDebug()<<"@@@@-1"<<sret;

    if (sret == GST_STATE_CHANGE_FAILURE) {
        gst_element_set_state (pipeline, GST_STATE_NULL);
        gst_object_unref (pipeline);
        // Exit application
        QTimer::singleShot(0, QApplication::activeWindow(), SLOT(quit()));
    }

    gst_element_get_state (pipeline,
                           &state,
                           &pending,
                           10);

    qDebug()<<state<<pending;


    window->show();
    app.exec();

    g_main_loop_run (loop);
  return 0;
}

1 个答案:

答案 0 :(得分:0)

我通过使用gst_parse_launch()创建管道解决了它

GstElement *pipeline_2= gst_parse_launch("udpsrc port=20000 ! application/x-rtp,encoding-name=JPEG,payload=26 ! rtpjpegdepay ! jpegparse ! jpegdec ! videoconvert ! videoscale ! ximagesink  name=mySink", NULL);

GstElement *sink = gst_bin_get_by_name((GstBin*)pipeline_2,"mySink");

QWidget *window = new QWidget();
window->setWindowTitle("udpsrc video stream");
window->resize(700, 700);

WId xwinid = window->winId();
gst_video_overlay_set_window_handle (GST_VIDEO_OVERLAY (sink), (guintptr)xwinid);

window->show();
GstStateChangeReturn sret = gst_element_set_state (pipeline_2, GST_STATE_PLAYING);

希望这会有所帮助。