Commit bb0db6b4 authored by Aleix Pol Gonzalez's avatar Aleix Pol Gonzalez 🐧
Browse files

Port away from udi+path to objectid

At the moment we're just using the udi to identify the device and we're
not even using v4l2src directly, since gst is creating the source on its
own and relying on pipewire.

This simplifies the infrastructure by removing an unused attribute (the
path) and using the right identifier.
parent 1a4c590d
......@@ -29,16 +29,9 @@ QString structureValue(GstStructure* device, const char* key)
return QString::fromUtf8(g_value_get_string(x));
}
QString udiFromProperties(GstStructure* deviceProperties)
QString objectIdFromProperties(GstStructure* st)
{
QString udi;
gboolean udev_probed;
if (gst_structure_get_boolean(deviceProperties, "udev-probed", &udev_probed) && !udev_probed)
udi = structureValue(deviceProperties, "device.path");
else
udi = structureValue(deviceProperties, "sysfs.path");
return udi;
return structureValue(st, "object.id");
}
// for reference, the properties can be listed with:
......@@ -49,8 +42,7 @@ Device::Device(GstDevice *device, QObject* parent)
, m_device(device)
{
auto st = gst_device_get_properties(device);
m_udi = udiFromProperties(st);
m_path = structureValue(st, "device.path");
m_objectId = objectIdFromProperties(st);
gst_structure_free(st);
}
......@@ -64,9 +56,9 @@ void Device::reset()
Q_EMIT filtersChanged(m_filters);
}
bool Device::isValid() const
QString Device::objectId() const
{
return !m_udi.isEmpty() && !m_path.isEmpty();
return m_objectId;
}
void Device::setFilters(const QString &newFilters)
......
......@@ -26,7 +26,7 @@
#include <KSharedConfig>
QString structureValue(GstStructure* device, const char* key);
QString udiFromProperties(GstStructure* deviceProperties);
QString objectIdFromProperties(GstStructure* deviceProperties);
class Device : public QObject
{
......@@ -37,14 +37,12 @@ class Device : public QObject
Device(GstDevice* device, QObject* parent);
~Device();
QString description() const { return m_description; }
QString udi() const { return m_udi; }
QString path() const { return m_path; }
QString objectId() const;
void setFilters(const QString &filters);
QString filters() const { return m_filters; }
GstElement* createElement();
bool isValid() const;
void reset();
Q_SIGNALS:
......@@ -53,8 +51,7 @@ class Device : public QObject
private:
const QString m_description;
GstDevice *const m_device;
QString m_udi;
QString m_path;
QString m_objectId;
QString m_filters;
};
......
......@@ -65,25 +65,20 @@ DeviceManager::DeviceManager() : m_playingDevice(0)
gst_device_monitor_start (m_monitor);
GList* devices = gst_device_monitor_get_devices (m_monitor);
if (devices == NULL) {
if (!devices) {
qWarning ("No device found");
}
/* Initialize camera structures */
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.constFirst()->udi());
setPlayingObjectId(m_deviceList.constFirst()->objectId());
}
}
......@@ -96,7 +91,7 @@ DeviceManager::~DeviceManager()
QHash<int, QByteArray> DeviceManager::roleNames() const
{
QHash< int, QByteArray > roles = QAbstractListModel::roleNames();
roles.insert(Udi, "udi");
roles.insert(ObjectId, "objectId");
return roles;
}
......@@ -106,7 +101,7 @@ void DeviceManager::save()
return;
}
Settings::self()->setDeviceUdi(m_playingDevice->udi());
Settings::self()->setDeviceObjectId(m_playingDevice->objectId());
}
/*
......@@ -117,20 +112,20 @@ int DeviceManager::rowCount(const QModelIndex& idx) const
return idx.isValid() ? 0 : m_deviceList.size();
}
void DeviceManager::setPlayingDeviceUdi(const QString& udi)
void DeviceManager::setPlayingObjectId(const QString &objectId)
{
Q_FOREACH(Device* d, m_deviceList) {
if(d->udi() == udi) {
if(d->objectId() == objectId) {
if (m_playingDevice != d) {
m_playingDevice = d;
qDebug() << "Playing device changed" << d->path();
qDebug() << "Playing device changed" << d->description();
Q_EMIT playingDeviceChanged();
}
return;
}
}
qWarning() << "could not find device" << udi;
qWarning() << "could not find device" << objectId;
m_playingDevice = nullptr;
}
......@@ -139,27 +134,18 @@ Device* DeviceManager::playingDevice()
return m_playingDevice;
}
QString DeviceManager::playingDeviceUdi() const
{
if (!m_playingDevice) {
return QString();
}
return m_playingDevice->udi();
}
QString DeviceManager::playingDevicePath() const
QString DeviceManager::playingObjectId() const
{
if (!m_playingDevice) {
return QByteArray();
return {};
}
return m_playingDevice->path();
return m_playingDevice->objectId();
}
QString DeviceManager::udiAt(int i) const
QString DeviceManager::objectIdAt(int i) const
{
return m_deviceList[i]->udi();
return m_deviceList[i]->objectId();
}
QVariant DeviceManager::data(const QModelIndex& index, int role) const
......@@ -171,8 +157,8 @@ QVariant DeviceManager::data(const QModelIndex& index, int role) const
switch(role) {
case Qt::DisplayRole:
return m_deviceList[row]->description();
case Udi:
return m_deviceList[row]->udi();
case ObjectId:
return m_deviceList[row]->objectId();
}
return QVariant();
}
......@@ -185,14 +171,14 @@ void DeviceManager::deviceAdded(GstDevice* device)
endInsertRows();
if (!m_playingDevice) {
setPlayingDeviceUdi(m_deviceList.first()->udi());
setPlayingObjectId(m_deviceList.first()->objectId());
}
}
void DeviceManager::deviceRemoved(GstDevice* device)
{
auto st = gst_device_get_properties(device);
auto udi = udiFromProperties(st);
auto objectId = objectIdFromProperties(st);
for(int i = 0, c = m_deviceList.size(); i<c; ++i) {
auto dev = m_deviceList.at(i);
......@@ -201,7 +187,7 @@ void DeviceManager::deviceRemoved(GstDevice* device)
Q_EMIT playingDeviceChanged();
}
if (dev->udi() == udi) {
if (dev->objectId() == objectId) {
beginRemoveRows({}, i, i);
dev->deleteLater();
m_deviceList.removeAt(i);
......
......@@ -30,20 +30,19 @@ struct _GstDeviceMonitor;
class DeviceManager : public QAbstractListModel
{
Q_OBJECT
Q_PROPERTY(QString playingDeviceUdi READ playingDeviceUdi WRITE setPlayingDeviceUdi NOTIFY playingDeviceChanged)
Q_PROPERTY(QString playingObjectId READ playingObjectId WRITE setPlayingObjectId NOTIFY playingDeviceChanged)
Q_PROPERTY(int count READ rowCount NOTIFY countChanged)
Q_PROPERTY(Device* playingDevice READ playingDevice NOTIFY playingDeviceChanged)
public:
static DeviceManager* self();
enum {
Udi=Qt::UserRole+1
ObjectId = Qt::UserRole+1
};
virtual QHash<int, QByteArray> roleNames() const override;
Device* playingDevice();
QString playingDevicePath() const;
QString playingDeviceUdi() const;
void setPlayingDeviceUdi(const QString& path);
QString playingObjectId() const;
void setPlayingObjectId(const QString &objectId);
bool hasDevices() const;
virtual int rowCount(const QModelIndex& = QModelIndex()) const override;
......@@ -52,7 +51,7 @@ class DeviceManager : public QAbstractListModel
void deviceRemoved(_GstDevice *device);
void deviceAdded(_GstDevice *device);
Q_SCRIPTABLE QString udiAt(int i) const;
Q_SCRIPTABLE QString objectIdAt(int i) const;
public Q_SLOTS:
void save();
......
......@@ -21,7 +21,7 @@
<default>true</default>
<label>Vertically mirror the camera view.</label>
</entry>
<entry name="deviceUdi" type="String" key="deviceUdi">
<entry name="DeviceObjectId" type="String" key="deviceObjectId">
<label>Points to the last used webcam.</label>
</entry>
</group>
......
......@@ -301,7 +301,7 @@ StackView {
textRole: "display"
visible: count>1
onActivated: {
devicesModel.playingDeviceUdi = devicesModel.udiAt(index)
devicesModel.playingObjectId = devicesModel.objectIdAt(index)
}
}
......
......@@ -262,7 +262,7 @@ bool WebcamControl::playDevice(Device *device)
Q_ASSERT(device);
//If we already have a pipeline for this device, just set it to picture mode
if (m_pipeline && m_currentDevice == device->udi()) {
if (m_pipeline && m_currentDevice == device->objectId()) {
g_object_set(m_pipeline.data(), "mode", 2, nullptr);
g_object_set(m_pipeline.data(), "location", m_tmpVideoPath.toUtf8().constData(), nullptr);
return true;
......@@ -287,7 +287,7 @@ bool WebcamControl::playDevice(Device *device)
GstElement* source;
g_object_get(m_cameraSource.data(), "video-source", &source, nullptr);
if (m_currentDevice != device->udi() || !source) {
if (m_currentDevice != device->objectId() || !source) {
source = device->createElement();
g_object_set(m_cameraSource.data(), "video-source", source, nullptr);
}
......@@ -305,7 +305,7 @@ bool WebcamControl::playDevice(Device *device)
gst_element_set_state(GST_ELEMENT(m_pipeline.data()), GST_STATE_PLAYING);
m_currentDevice = device->udi();
m_currentDevice = device->objectId();
return true;
}
......
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