Merged in f5soh/librepilot/update_credits (pull request #529)
[librepilot.git] / ground / gcs / src / libs / gstreamer / devicemonitor.cpp
blobb8bedddfce7ce4b1ea5227a9685940aa06322be5
1 /**
2 ******************************************************************************
4 * @file devicemonitor.cpp
5 * @author The LibrePilot Project, http://www.librepilot.org Copyright (C) 2017.
6 * @brief
7 * @see The GNU Public License (GPL) Version 3
8 * @defgroup
9 * @{
11 *****************************************************************************/
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 3 of the License, or
16 * (at your option) any later version.
18 * This program is distributed in the hope that it will be useful, but
19 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
20 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
21 * for more details.
23 * You should have received a copy of the GNU General Public License along
24 * with this program; if not, write to the Free Software Foundation, Inc.,
25 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
27 #include "devicemonitor.h"
29 #include "gst_util.h"
31 #include <gst/gst.h>
33 #include <QDebug>
35 static GstBusSyncReply my_bus_sync_func(GstBus *bus, GstMessage *message, gpointer user_data)
37 Q_UNUSED(bus)
39 DeviceMonitor * dm;
40 GstDevice *device;
41 gchar *name;
43 switch (GST_MESSAGE_TYPE(message)) {
44 case GST_MESSAGE_DEVICE_ADDED:
45 gst_message_parse_device_added(message, &device);
46 name = gst_device_get_display_name(device);
48 dm = (DeviceMonitor *)user_data;
49 QMetaObject::invokeMethod(dm, "device_added", Qt::QueuedConnection,
50 Q_ARG(QString, QString(name)));
52 g_free(name);
53 break;
54 case GST_MESSAGE_DEVICE_REMOVED:
55 gst_message_parse_device_removed(message, &device);
56 name = gst_device_get_display_name(device);
58 dm = (DeviceMonitor *)user_data;
59 QMetaObject::invokeMethod(dm, "device_removed", Qt::QueuedConnection,
60 Q_ARG(QString, QString(name)));
62 g_free(name);
63 break;
64 default:
65 break;
68 // no need to pass it to the async queue, there is none...
69 return GST_BUS_DROP;
72 DeviceMonitor::DeviceMonitor(QObject *parent) : QObject(parent)
74 // initialize gstreamer
75 gst::init(NULL, NULL);
77 monitor = gst_device_monitor_new();
79 GstBus *bus = gst_device_monitor_get_bus(monitor);
80 gst_bus_set_sync_handler(bus, (GstBusSyncHandler)my_bus_sync_func, this, NULL);
81 gst_object_unref(bus);
83 GstCaps *caps = NULL; // gst_caps_new_empty_simple("video/x-raw");
84 const gchar *classes = "Video/Source";
85 gst_device_monitor_add_filter(monitor, classes, caps);
86 if (caps) {
87 gst_caps_unref(caps);
90 if (!gst_device_monitor_start(monitor)) {
91 qWarning() << "Failed to start device monitor";
95 DeviceMonitor::~DeviceMonitor()
97 gst_device_monitor_stop(monitor);
98 gst_object_unref(monitor);
101 QList<Device> DeviceMonitor::devices() const
103 QList<Device> devices;
105 GList *list = gst_device_monitor_get_devices(monitor);
106 while (list != NULL) {
107 gchar *name;
108 gchar *device_class;
110 GstDevice *device = (GstDevice *)list->data;
111 name = gst_device_get_display_name(device);
112 device_class = gst_device_get_device_class(device);
114 devices << Device(name, device_class);
116 g_free(name);
117 g_free(device_class);
119 gst_object_unref(device);
120 list = g_list_remove_link(list, list);
123 return devices;
126 void DeviceMonitor::device_added(QString name)
128 // qDebug() << "**** ADDED:" << name;
129 emit deviceAdded(name);
132 void DeviceMonitor::device_removed(QString name)
134 // qDebug() << "**** REMOVED:" << name;
135 emit deviceRemoved(name);