Commit 677be503 authored by Aleix Pol Gonzalez's avatar Aleix Pol Gonzalez 🐧

Improve how we list devices

parent 2e7b5cc4
......@@ -31,13 +31,15 @@ QString structureValue(GstStructure* device, const char* key)
// for reference, the properties can be listed with:
// gst-device-monitor-1.0 Video/Source
Device::Device(GstStructure *device, QObject* parent)
Device::Device(GstDevice *device, QObject* parent)
: QObject(parent)
, m_description(structureValue(device, "device.product.name"))
, m_udi(structureValue(device, "sysfs.path"))
, m_path(structureValue(device, "device.path"))
, m_description(gst_device_get_display_name(device))
, m_device(device)
{
qDebug() << "new device" << m_description << m_udi << m_path;
auto st = gst_device_get_properties(device);
m_udi = structureValue(st, "sysfs.path");
m_path = structureValue(st, "device.path");
gst_structure_free(st);
}
Device::~Device()
......@@ -50,6 +52,11 @@ void Device::reset()
Q_EMIT filtersChanged(m_filters);
}
bool Device::isValid() const
{
return !m_udi.isEmpty() && !m_path.isEmpty();
}
void Device::setFilters(const QString &newFilters)
{
if (newFilters == m_filters) {
......@@ -59,3 +66,8 @@ void Device::setFilters(const QString &newFilters)
m_filters = newFilters;
Q_EMIT filtersChanged(newFilters);
}
GstElement* Device::createElement()
{
return gst_device_create_element(m_device, m_description.toUtf8().constData());
}
......@@ -21,7 +21,7 @@
#define DEVICE_H
#include <QObject>
#include <gst/gststructure.h>
#include <gst/gstdevice.h>
#include <KSharedConfig>
......@@ -33,7 +33,7 @@ class Device : public QObject
Q_PROPERTY(QString filters READ filters WRITE setFilters NOTIFY filtersChanged)
public:
Device(GstStructure *structure, QObject* parent);
Device(GstDevice* device, QObject* parent);
~Device();
QString description() const { return m_description; }
QString udi() const { return m_udi; }
......@@ -41,6 +41,9 @@ class Device : public QObject
void setFilters(const QString &filters);
QString filters() const { return m_filters; }
GstElement* createElement();
bool isValid() const;
void reset();
Q_SIGNALS:
......@@ -48,8 +51,9 @@ class Device : public QObject
private:
const QString m_description;
const QString m_udi;
const QString m_path;
GstDevice *const m_device;
QString m_udi;
QString m_path;
QString m_filters;
};
......
......@@ -58,7 +58,7 @@ DeviceManager::DeviceManager() : m_playingDevice(0)
gst_bus_add_watch (bus, deviceMonitorWatch, m_monitor);
gst_object_unref (bus);
GstCaps *caps = gst_caps_new_empty_simple ("image/jpeg");
GstCaps *caps = gst_caps_new_empty_simple ("video/x-raw");
gst_device_monitor_add_filter (m_monitor, "Video/Source", caps);
gst_caps_unref (caps);
......@@ -71,15 +71,19 @@ DeviceManager::DeviceManager() : m_playingDevice(0)
}
/* Initialize camera structures */
while(devices) {
deviceAdded(GST_DEVICE(devices->data));
devices = devices->next;
for(; devices; devices = devices->next) {
auto device = new Device(GST_DEVICE(devices->data), this);
if (!device->isValid()) {
delete device;
continue;
}
m_deviceList.append(device);
}
g_list_free (devices);
if (!m_deviceList.isEmpty()) {
setPlayingDeviceUdi(m_deviceList.first()->udi());
setPlayingDeviceUdi(m_deviceList.constFirst()->udi());
}
}
......@@ -126,7 +130,8 @@ void DeviceManager::setPlayingDeviceUdi(const QString& udi)
}
}
m_playingDevice = 0;
qWarning() << "could not find device" << udi;
m_playingDevice = nullptr;
}
Device* DeviceManager::playingDevice()
......@@ -174,15 +179,11 @@ QVariant DeviceManager::data(const QModelIndex& index, int role) const
void DeviceManager::deviceAdded(GstDevice* device)
{
auto st = gst_device_get_properties(device);
const int s = m_deviceList.size();
beginInsertRows({}, s, s);
m_deviceList.append(new Device(st, this));
m_deviceList.append(new Device(device, this));
endInsertRows();
gst_structure_free(st);
if (!m_playingDevice) {
setPlayingDeviceUdi(m_deviceList.first()->udi());
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment