Skip to content

Commit

Permalink
[devices] Support setting custom names in enumerator ; make V4L2 came…
Browse files Browse the repository at this point in the history
…ras more readable
  • Loading branch information
jcelerier committed Apr 1, 2024
1 parent d56ed4f commit 81b1c74
Show file tree
Hide file tree
Showing 21 changed files with 306 additions and 82 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,12 @@ class SCORE_LIB_DEVICE_EXPORT DeviceEnumerator : public QObject
public:
virtual ~DeviceEnumerator();

virtual void enumerate(std::function<void(const Device::DeviceSettings&)>) const = 0;
virtual void
enumerate(std::function<void(const QString&, const Device::DeviceSettings&)>) const
= 0;

void deviceAdded(const Device::DeviceSettings& s)
E_SIGNAL(SCORE_LIB_DEVICE_EXPORT, deviceAdded, s)
void deviceAdded(const QString& n, const Device::DeviceSettings& s)
E_SIGNAL(SCORE_LIB_DEVICE_EXPORT, deviceAdded, n, s)
void deviceRemoved(const QString& s)
E_SIGNAL(SCORE_LIB_DEVICE_EXPORT, deviceRemoved, s)
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -268,10 +268,11 @@ void DeviceEditDialog::selectedProtocolChanged()
cat->setFlags(Qt::ItemIsEnabled);
m_devices->addTopLevelItem(cat);

auto addItem = [&, cat](const Device::DeviceSettings& settings) {
auto addItem
= [&, cat](const QString& name, const Device::DeviceSettings& settings) {
auto item = new QTreeWidgetItem;
item->setFlags(Qt::ItemIsEnabled | Qt::ItemIsSelectable);
item->setText(0, settings.name);
item->setText(0, name);
item->setData(0, Qt::UserRole, QVariant::fromValue(settings));
cat->addChild(item);
cat->setExpanded(true);
Expand Down
1 change: 1 addition & 0 deletions src/plugins/score-plugin-gfx/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ set(HDRS

Gfx/CameraSettings.hpp
Gfx/CameraDevice.hpp
Gfx/CameraDeviceEnumerator.hpp
Gfx/WindowDevice.hpp
# Gfx/graph/hap/source/hap.h

Expand Down
5 changes: 3 additions & 2 deletions src/plugins/score-plugin-gfx/Gfx/CameraDevice.avf.mm
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
#import <AVFoundation/AVFoundation.h>

#include "CameraSettings.hpp"
#include <QDebug>
#include <functional>
#include <libavutil/pixfmt.h>

#include <QDebug>

namespace Gfx
{

static void iterateCameraFormats(AVCaptureDevice* device, std::function<void(CameraSettings, QString)> func)
{
const char *name = [[device localizedName] UTF8String];
Expand Down
104 changes: 93 additions & 11 deletions src/plugins/score-plugin-gfx/Gfx/CameraDevice.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
#include <State/MessageListSerialization.hpp>
#include <State/Widgets/AddressFragmentLineEdit.hpp>

#include <Gfx/CameraDeviceEnumerator.hpp>
#include <Gfx/GfxApplicationPlugin.hpp>
#include <Media/LibavIntrospection.hpp>

#include <score/serialization/MimeVisitor.hpp>

Expand Down Expand Up @@ -65,18 +67,42 @@ bool CameraDevice::reconnect()
class CameraEnumerator : public Device::DeviceEnumerator
{
public:
void enumerate(std::function<void(const Device::DeviceSettings&)> f) const override
void enumerate(std::function<void(const QString&, const Device::DeviceSettings&)> f)
const override
{
enumerateCameraDevices([&](const CameraSettings& set, QString name) {
Device::DeviceSettings s;
s.name = name;
s.protocol = CameraProtocolFactory::static_concreteKey();
s.deviceSpecificSettings = QVariant::fromValue(set);
f(s);
f(name, s);
});
}
};

class CustomCameraEnumerator : public Device::DeviceEnumerator
{
public:
void enumerate(std::function<void(const QString&, const Device::DeviceSettings&)> f)
const override
{
CameraSettings set;
set.input = "<CUSTOM>";
set.device = "";
set.size = {};
set.fps = {};

set.codec = 0;
set.pixelformat = -1;

Device::DeviceSettings s;
s.name = "Custom";
s.protocol = CameraProtocolFactory::static_concreteKey();
s.deviceSpecificSettings = QVariant::fromValue(set);
f(s.name, s);
}
};

QString CameraProtocolFactory::prettyName() const noexcept
{
return QObject::tr("Camera input");
Expand All @@ -90,7 +116,15 @@ QString CameraProtocolFactory::category() const noexcept
Device::DeviceEnumerators
CameraProtocolFactory::getEnumerators(const score::DocumentContext& ctx) const
{
return {{"Streams", new CameraEnumerator}};
Device::DeviceEnumerators enums;
#if !defined(__linux__)
enums.push_back({"Cameras", new CameraEnumerator});
#else
auto devices = Gfx::make_camera_enumerator();
devices->registerAllEnumerators(enums);
#endif
enums.push_back({"Custom", new CustomCameraEnumerator});
return enums;
}

Device::DeviceInterface* CameraProtocolFactory::makeDevice(
Expand Down Expand Up @@ -127,11 +161,6 @@ Device::AddressDialog* CameraProtocolFactory::makeEditAddressDialog(
return nullptr;
}

Device::ProtocolSettingsWidget* CameraProtocolFactory::makeSettingsWidget()
{
return new CameraSettingsWidget;
}

QVariant
CameraProtocolFactory::makeProtocolSpecificSettings(const VisitorVariant& visitor) const
{
Expand All @@ -150,15 +179,61 @@ bool CameraProtocolFactory::checkCompatibility(
return a.name != b.name;
}

class CameraSettingsWidget final : public Device::ProtocolSettingsWidget
{
public:
explicit CameraSettingsWidget(QWidget* parent = nullptr);

Device::DeviceSettings getSettings() const override;
void setSettings(const Device::DeviceSettings& settings) override;

private:
void setDefaults();
QLineEdit* m_deviceNameEdit{};
QLineEdit* m_device{};
QComboBox* m_input{};

QFormLayout* m_layout{};

Device::DeviceSettings m_settings;
};

Device::ProtocolSettingsWidget* CameraProtocolFactory::makeSettingsWidget()
{
return new CameraSettingsWidget;
}

CameraSettingsWidget::CameraSettingsWidget(QWidget* parent)
: ProtocolSettingsWidget(parent)
{
m_deviceNameEdit = new State::AddressFragmentLineEdit{this};
checkForChanges(m_deviceNameEdit);

auto layout = new QFormLayout;
layout->addRow(tr("Device Name"), m_deviceNameEdit);
setLayout(layout);
m_device = new QLineEdit{this};
m_input = new QComboBox{this};

m_layout = new QFormLayout;
m_layout->addRow(tr("Device Name"), m_deviceNameEdit);
m_layout->addRow(tr("Device"), m_device);
m_layout->addRow(tr("Input"), m_input);

m_layout->setRowVisible(1, false);
m_layout->setRowVisible(2, false);

const auto& info = LibavIntrospection::instance();
for(auto& demux : info.demuxers)
{
QString name = demux.format->name;
if(demux.format->long_name && strlen(demux.format->long_name) > 0)
{
name += " (";
name += demux.format->long_name;
name += ")";
}
m_input->addItem(name, QVariant::fromValue((void*)demux.format));
}

setLayout(m_layout);

setDefaults();
}
Expand Down Expand Up @@ -191,6 +266,13 @@ void CameraSettingsWidget::setSettings(const Device::DeviceSettings& settings)
ossia::net::sanitize_device_name(prettyName);
}
m_deviceNameEdit->setText(prettyName);

const CameraSettings& set = settings.deviceSpecificSettings.value<CameraSettings>();
m_device->setText(set.device);

bool showCustom = set.input == "<CUSTOM>";
m_layout->setRowVisible(1, showCustom);
m_layout->setRowVisible(2, showCustom);
}

}
Expand Down
15 changes: 0 additions & 15 deletions src/plugins/score-plugin-gfx/Gfx/CameraDevice.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,21 +69,6 @@ class CameraDevice final : public GfxInputDevice
Gfx::video_texture_input_protocol* m_protocol{};
mutable std::unique_ptr<Gfx::video_texture_input_device> m_dev;
};

class CameraSettingsWidget final : public Device::ProtocolSettingsWidget
{
public:
CameraSettingsWidget(QWidget* parent = nullptr);

Device::DeviceSettings getSettings() const override;
void setSettings(const Device::DeviceSettings& settings) override;

private:
void setDefaults();
QLineEdit* m_deviceNameEdit{};
Device::DeviceSettings m_settings;
};

}

SCORE_SERIALIZE_DATASTREAM_DECLARE(, Gfx::CameraSettings);
Expand Down
Loading

0 comments on commit 81b1c74

Please sign in to comment.