Advertisement
Guest User

Untitled

a guest
Jul 10th, 2015
354
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.12 KB | None | 0 0
  1. gstreamercamerahandler class:
  2.  
  3. #include <QApplication>
  4. #include <gst/rtsp/gstrtsptransport.h>
  5. #include <gst/video/videooverlay.h>
  6.  
  7. #include <QDebug>
  8.  
  9. #include "gstreamercamerahandler.h"
  10.  
  11. static void on_pad_added (GstElement *element, GstPad *pad, void *data);
  12.  
  13. static void on_pad_added (GstElement *element, GstPad *pad, void *data)
  14. {
  15.     GstElement *camerasrcbin = GST_ELEMENT (data);
  16.     GstElement *depay;
  17.     GstPad *sinkpad;
  18.  
  19.     depay = gst_bin_get_by_name(GST_BIN(camerasrcbin), "mydepay");
  20.  
  21.     sinkpad = gst_element_get_static_pad (depay, "sink");
  22.     gst_pad_link (pad, sinkpad);
  23.     gst_object_unref (sinkpad);
  24.  
  25.     gst_element_set_state(camerasrcbin, GST_STATE_PAUSED);
  26.  
  27.     gst_object_ref(camerasrcbin); /* to keep in memory */
  28.  
  29.     qDebug() << "Pad added and srcbin put to pause.";
  30. }
  31.  
  32. int gstreamerCameraHandler::createCameraSrcBin(QString location)
  33. {
  34.     GstBus     *bus;
  35.     GstElement *source;
  36.     GstElement *depay;
  37.     GstElement *parser;
  38.     GstElement *queue;
  39.  
  40.     GstPad *pad;    
  41.  
  42.     cameraSrcBin = gst_bin_new(NULL);
  43.  
  44.     source = gst_element_factory_make("rtspsrc", "mysource");
  45.     depay = gst_element_factory_make("rtph264depay", "mydepay");
  46.     parser = gst_element_factory_make("h264parse", "myparser");
  47.     queue = gst_element_factory_make("queue","myqueue");
  48.  
  49.     if(!cameraSrcBin || !source || !depay || !parser || !queue)
  50.     {
  51.         qDebug() << "Not all elements could be created.";
  52.         return -1;
  53.     }
  54.  
  55.     g_object_set(G_OBJECT(source), "location", (gchar*)location.toStdString().c_str() , NULL);
  56.     g_object_set(G_OBJECT(source), "do-rtsp-keep-alive", (gboolean) true, NULL);
  57. //    g_object_set(G_OBJECT(source), "async-handling", (gboolean) true, NULL);
  58. //    g_object_set(G_OBJECT(source), "timeout", (guint64) 1000000, NULL);
  59. //    g_object_set(G_OBJECT(source), "tcp-timeout", (guint64) 5000000, NULL);
  60.     g_object_set(G_OBJECT(source), "retry", (guint32) 100, NULL);
  61.     g_object_set(G_OBJECT(source), "udp-reconnect", (gboolean) true, NULL);
  62.     g_object_set(G_OBJECT(source), "protocols", GST_RTSP_LOWER_TRANS_UDP, NULL);
  63.     g_object_set(G_OBJECT(source), "user-id", (gchar*)"root", NULL);
  64.     g_object_set(G_OBJECT(source), "user-pw", (gchar*)"pass", NULL);
  65.     g_object_set(G_OBJECT(source), "latency", (guint32) 0, NULL);
  66.  
  67.     gst_bin_add_many(GST_BIN(cameraSrcBin), source, depay, parser, queue, NULL);
  68.     gst_element_link_many(depay, parser, queue, NULL);
  69.  
  70.     pad = gst_element_get_static_pad(queue, "src");
  71.     gst_element_add_pad(cameraSrcBin, gst_ghost_pad_new("src", pad));
  72.     gst_object_unref(GST_OBJECT(pad));
  73.     g_signal_connect (source , "pad-added", G_CALLBACK (on_pad_added), GST_ELEMENT (cameraSrcBin));
  74.  
  75.     gst_element_set_state(cameraSrcBin, GST_STATE_PLAYING);
  76.     return 0;
  77. }
  78.  
  79. gstreamerCameraHandler::gstreamerCameraHandler(QObject *parent, QString location) :
  80.     QObject(parent)
  81. {
  82.     createCameraSrcBin(location);
  83. }
  84.  
  85.  
  86.  
  87. GStreamerwidget class:
  88.  
  89. #include <QApplication>
  90. #include <gst/video/videooverlay.h>
  91. #include <qpainter.h>
  92. #include <qthread.h>
  93. #include "gstreamerwidget.h"
  94.  
  95. static GstBusSyncReply _bus_callback (GstBus *bus, GstMessage *message, gpointer user_data);
  96.  
  97. static GstBusSyncReply _bus_callback (GstBus *bus, GstMessage *message, gpointer user_data)
  98. {
  99.     if (GST_MESSAGE_TYPE (message) != GST_MESSAGE_ELEMENT)
  100.         return GST_BUS_PASS;
  101.  
  102.     if (!gst_message_has_name (message, "prepare-window-handle"))
  103.         return GST_BUS_PASS;
  104.  
  105.     qDebug("Got message of type %s from %s", GST_MESSAGE_TYPE_NAME(message),
  106.            GST_ELEMENT_NAME (GST_MESSAGE_SRC (message)));
  107.  
  108.     GStreamerWidget* widget = (GStreamerWidget*) user_data;
  109.     if(widget->winid != 0)
  110.     {
  111.         gst_video_overlay_set_window_handle(GST_VIDEO_OVERLAY (message->src), widget->winid);
  112.     }
  113.     gst_message_unref (message);
  114.  
  115.     return GST_BUS_DROP;
  116. }
  117.  
  118. GStreamerWidget::GStreamerWidget(QWidget *parent) :
  119.     QWidget(parent)
  120. {
  121.     leftOffset = 0;
  122.     rightOffset = 0;
  123.     topOffset = 0;
  124.     bottomOffset = 0;
  125.     winid = 0;
  126.  
  127.     cameraObject = NULL;
  128.     pipeline = NULL;
  129. }
  130.  
  131. GStreamerWidget::~GStreamerWidget()
  132. {
  133.     detachCamera();
  134. }
  135.  
  136. int GStreamerWidget::constructPipeline()
  137. {
  138.     GstBus *bus;
  139.  
  140.     GstElement *decoder;
  141.     GstElement *queue;
  142.     GstElement *crop;
  143.     GstElement *scale;
  144.     GstElement *postproc;
  145.     GstElement *sink;
  146.  
  147.     pipeline = gst_pipeline_new(NULL);
  148.  
  149.     decoder = gst_element_factory_make("vaapidecode", NULL);
  150.     queue = gst_element_factory_make("queue", NULL);
  151.     crop = gst_element_factory_make("videocrop", NULL);
  152.     scale = gst_element_factory_make("videoscale", NULL);
  153.     postproc = gst_element_factory_make("vaapipostproc", NULL);
  154.     sink = gst_element_factory_make("vaapisink", NULL);
  155.  
  156.  
  157.     if(!pipeline || !inputbin || !decoder || !queue || !crop || !scale || !postproc || !sink)
  158.     {
  159.         g_log(NULL, G_LOG_LEVEL_ERROR,
  160.               "Not all elements could be created.\n");
  161.         return -1;
  162.     }
  163.  
  164.     g_object_set(G_OBJECT(scale), "add-borders", true, NULL);
  165.  
  166.     g_object_set(G_OBJECT(sink), "force-aspect-ratio", false, NULL);
  167.     g_object_set(G_OBJECT(sink), "async", false, NULL);
  168.     g_object_set(G_OBJECT(sink), "sync", false, NULL);
  169.  
  170.     gst_bin_add_many(GST_BIN(pipeline), inputbin, decoder, queue, crop, scale, postproc, sink, NULL);
  171.     gst_element_link_many(inputbin, decoder, queue, crop, scale, postproc, sink, NULL);
  172.  
  173.     bus = gst_pipeline_get_bus(GST_PIPELINE(pipeline));
  174.     gst_bus_set_sync_handler (bus, _bus_callback, this, NULL);
  175.     gst_object_unref(bus);
  176.  
  177.     return 0;
  178. }
  179.  
  180. void GStreamerWidget::attachCamera(BlueprintCamera* camera)
  181. {
  182.     if(pipeline == NULL)
  183.     {
  184.         cameraObject = camera;
  185.         inputbin = cameraObject->getCameraHandler()->getCameraSrcBin();
  186.         winid = this->winId();
  187.  
  188.         constructPipeline();
  189.     }
  190. }
  191.  
  192. void GStreamerWidget::detachCamera()
  193. {
  194.     if(pipeline != NULL)
  195.     {
  196.         gst_bin_remove(GST_BIN(pipeline), inputbin);
  197.         gst_element_set_state(inputbin, GST_STATE_PAUSED);
  198.  
  199.         /* Remove the videosink explicitly, so we are sure that it is gone in time */
  200.         GstElement* videosink;
  201.         videosink = gst_bin_get_by_interface(GST_BIN(pipeline), GST_TYPE_VIDEO_OVERLAY);
  202.         gst_bin_remove(GST_BIN(pipeline), videosink);
  203.  
  204.         gst_element_set_state(pipeline, GST_STATE_NULL);
  205.         gst_object_unref(pipeline);
  206.         pipeline = NULL;
  207.     }
  208. }
  209.  
  210. void GStreamerWidget::play()
  211. {
  212.     if(pipeline != NULL)
  213.     {
  214.         gst_element_set_state(pipeline, GST_STATE_PLAYING);
  215.     }
  216. }
  217.  
  218. void GStreamerWidget::pause()
  219. {
  220.     if(pipeline != NULL)
  221.     {
  222.         gst_element_set_state(pipeline, GST_STATE_PAUSED);
  223.     }
  224. }
  225.  
  226. void GStreamerWidget::mousePressEvent(QMouseEvent *event)
  227. {
  228.     qDebug() << "mouse click..." << event;
  229.  
  230.     //    if (this->parent()) {
  231.  
  232.     //    QWidget::mousePressEvent(event);
  233.  
  234.     emit signalCameraPressed(cameraObject);
  235.  
  236.     //    } else {
  237.     //        if(this->isMaximized()) {
  238.     //            this->showNormal();
  239.     //        } else {
  240.     //            this->showMaximized();
  241.     //        }
  242.     //    }
  243. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement