Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- gstreamercamerahandler class:
- #include <QApplication>
- #include <gst/rtsp/gstrtsptransport.h>
- #include <gst/video/videooverlay.h>
- #include <QDebug>
- #include "gstreamercamerahandler.h"
- static void on_pad_added (GstElement *element, GstPad *pad, void *data);
- static void on_pad_added (GstElement *element, GstPad *pad, void *data)
- {
- GstElement *camerasrcbin = GST_ELEMENT (data);
- GstElement *depay;
- GstPad *sinkpad;
- depay = gst_bin_get_by_name(GST_BIN(camerasrcbin), "mydepay");
- sinkpad = gst_element_get_static_pad (depay, "sink");
- gst_pad_link (pad, sinkpad);
- gst_object_unref (sinkpad);
- gst_element_set_state(camerasrcbin, GST_STATE_PAUSED);
- gst_object_ref(camerasrcbin); /* to keep in memory */
- qDebug() << "Pad added and srcbin put to pause.";
- }
- int gstreamerCameraHandler::createCameraSrcBin(QString location)
- {
- GstBus *bus;
- GstElement *source;
- GstElement *depay;
- GstElement *parser;
- GstElement *queue;
- GstPad *pad;
- cameraSrcBin = gst_bin_new(NULL);
- source = gst_element_factory_make("rtspsrc", "mysource");
- depay = gst_element_factory_make("rtph264depay", "mydepay");
- parser = gst_element_factory_make("h264parse", "myparser");
- queue = gst_element_factory_make("queue","myqueue");
- if(!cameraSrcBin || !source || !depay || !parser || !queue)
- {
- qDebug() << "Not all elements could be created.";
- return -1;
- }
- g_object_set(G_OBJECT(source), "location", (gchar*)location.toStdString().c_str() , NULL);
- g_object_set(G_OBJECT(source), "do-rtsp-keep-alive", (gboolean) true, NULL);
- // g_object_set(G_OBJECT(source), "async-handling", (gboolean) true, NULL);
- // g_object_set(G_OBJECT(source), "timeout", (guint64) 1000000, NULL);
- // g_object_set(G_OBJECT(source), "tcp-timeout", (guint64) 5000000, NULL);
- g_object_set(G_OBJECT(source), "retry", (guint32) 100, NULL);
- g_object_set(G_OBJECT(source), "udp-reconnect", (gboolean) true, NULL);
- g_object_set(G_OBJECT(source), "protocols", GST_RTSP_LOWER_TRANS_UDP, NULL);
- g_object_set(G_OBJECT(source), "user-id", (gchar*)"root", NULL);
- g_object_set(G_OBJECT(source), "user-pw", (gchar*)"pass", NULL);
- g_object_set(G_OBJECT(source), "latency", (guint32) 0, NULL);
- gst_bin_add_many(GST_BIN(cameraSrcBin), source, depay, parser, queue, NULL);
- gst_element_link_many(depay, parser, queue, NULL);
- pad = gst_element_get_static_pad(queue, "src");
- gst_element_add_pad(cameraSrcBin, gst_ghost_pad_new("src", pad));
- gst_object_unref(GST_OBJECT(pad));
- g_signal_connect (source , "pad-added", G_CALLBACK (on_pad_added), GST_ELEMENT (cameraSrcBin));
- gst_element_set_state(cameraSrcBin, GST_STATE_PLAYING);
- return 0;
- }
- gstreamerCameraHandler::gstreamerCameraHandler(QObject *parent, QString location) :
- QObject(parent)
- {
- createCameraSrcBin(location);
- }
- GStreamerwidget class:
- #include <QApplication>
- #include <gst/video/videooverlay.h>
- #include <qpainter.h>
- #include <qthread.h>
- #include "gstreamerwidget.h"
- static GstBusSyncReply _bus_callback (GstBus *bus, GstMessage *message, gpointer user_data);
- static GstBusSyncReply _bus_callback (GstBus *bus, GstMessage *message, gpointer user_data)
- {
- if (GST_MESSAGE_TYPE (message) != GST_MESSAGE_ELEMENT)
- return GST_BUS_PASS;
- if (!gst_message_has_name (message, "prepare-window-handle"))
- return GST_BUS_PASS;
- qDebug("Got message of type %s from %s", GST_MESSAGE_TYPE_NAME(message),
- GST_ELEMENT_NAME (GST_MESSAGE_SRC (message)));
- GStreamerWidget* widget = (GStreamerWidget*) user_data;
- if(widget->winid != 0)
- {
- gst_video_overlay_set_window_handle(GST_VIDEO_OVERLAY (message->src), widget->winid);
- }
- gst_message_unref (message);
- return GST_BUS_DROP;
- }
- GStreamerWidget::GStreamerWidget(QWidget *parent) :
- QWidget(parent)
- {
- leftOffset = 0;
- rightOffset = 0;
- topOffset = 0;
- bottomOffset = 0;
- winid = 0;
- cameraObject = NULL;
- pipeline = NULL;
- }
- GStreamerWidget::~GStreamerWidget()
- {
- detachCamera();
- }
- int GStreamerWidget::constructPipeline()
- {
- GstBus *bus;
- GstElement *decoder;
- GstElement *queue;
- GstElement *crop;
- GstElement *scale;
- GstElement *postproc;
- GstElement *sink;
- pipeline = gst_pipeline_new(NULL);
- decoder = gst_element_factory_make("vaapidecode", NULL);
- queue = gst_element_factory_make("queue", NULL);
- crop = gst_element_factory_make("videocrop", NULL);
- scale = gst_element_factory_make("videoscale", NULL);
- postproc = gst_element_factory_make("vaapipostproc", NULL);
- sink = gst_element_factory_make("vaapisink", NULL);
- if(!pipeline || !inputbin || !decoder || !queue || !crop || !scale || !postproc || !sink)
- {
- g_log(NULL, G_LOG_LEVEL_ERROR,
- "Not all elements could be created.\n");
- return -1;
- }
- g_object_set(G_OBJECT(scale), "add-borders", true, NULL);
- g_object_set(G_OBJECT(sink), "force-aspect-ratio", false, NULL);
- g_object_set(G_OBJECT(sink), "async", false, NULL);
- g_object_set(G_OBJECT(sink), "sync", false, NULL);
- gst_bin_add_many(GST_BIN(pipeline), inputbin, decoder, queue, crop, scale, postproc, sink, NULL);
- gst_element_link_many(inputbin, decoder, queue, crop, scale, postproc, sink, NULL);
- bus = gst_pipeline_get_bus(GST_PIPELINE(pipeline));
- gst_bus_set_sync_handler (bus, _bus_callback, this, NULL);
- gst_object_unref(bus);
- return 0;
- }
- void GStreamerWidget::attachCamera(BlueprintCamera* camera)
- {
- if(pipeline == NULL)
- {
- cameraObject = camera;
- inputbin = cameraObject->getCameraHandler()->getCameraSrcBin();
- winid = this->winId();
- constructPipeline();
- }
- }
- void GStreamerWidget::detachCamera()
- {
- if(pipeline != NULL)
- {
- gst_bin_remove(GST_BIN(pipeline), inputbin);
- gst_element_set_state(inputbin, GST_STATE_PAUSED);
- /* Remove the videosink explicitly, so we are sure that it is gone in time */
- GstElement* videosink;
- videosink = gst_bin_get_by_interface(GST_BIN(pipeline), GST_TYPE_VIDEO_OVERLAY);
- gst_bin_remove(GST_BIN(pipeline), videosink);
- gst_element_set_state(pipeline, GST_STATE_NULL);
- gst_object_unref(pipeline);
- pipeline = NULL;
- }
- }
- void GStreamerWidget::play()
- {
- if(pipeline != NULL)
- {
- gst_element_set_state(pipeline, GST_STATE_PLAYING);
- }
- }
- void GStreamerWidget::pause()
- {
- if(pipeline != NULL)
- {
- gst_element_set_state(pipeline, GST_STATE_PAUSED);
- }
- }
- void GStreamerWidget::mousePressEvent(QMouseEvent *event)
- {
- qDebug() << "mouse click..." << event;
- // if (this->parent()) {
- // QWidget::mousePressEvent(event);
- emit signalCameraPressed(cameraObject);
- // } else {
- // if(this->isMaximized()) {
- // this->showNormal();
- // } else {
- // this->showMaximized();
- // }
- // }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement