update for linux.

This commit is contained in:
amass 2024-10-01 23:33:22 +08:00
parent 2d78ddbd04
commit 4d2affb862
4 changed files with 38 additions and 8 deletions

View File

@ -20,9 +20,7 @@
#include <filesystem> #include <filesystem>
#include <fstream> #include <fstream>
#include <mbedtls/md5.h> #include <mbedtls/md5.h>
#ifdef WIN32
#include "DeviceDiscovery.h" #include "DeviceDiscovery.h"
#endif
constexpr uint32_t ImageSliceSize = (4000 - 32); constexpr uint32_t ImageSliceSize = (4000 - 32);
@ -123,13 +121,11 @@ QStringList Application::availableSerialPorts() const {
QStringList Application::availableUsbVideoCameras() const { QStringList Application::availableUsbVideoCameras() const {
QStringList ret; QStringList ret;
#ifdef WIN32
DeviceDiscovery d; DeviceDiscovery d;
auto devices = d.devices(); auto devices = d.devices();
for (auto &device : devices) { for (auto &device : devices) {
ret << QString::fromStdString(device); ret << QString::fromStdString(device);
} }
#endif
return ret; return ret;
} }

View File

@ -4,7 +4,7 @@ find_package(Qt${QT_VERSION_MAJOR} REQUIRED COMPONENTS SerialPort)
set(CMAKE_AUTOMOC ON) set(CMAKE_AUTOMOC ON)
add_library(Peripheral add_library(Peripheral
$<$<PLATFORM_ID:Windows>:DeviceDiscovery.h DeviceDiscovery.cpp> DeviceDiscovery.h DeviceDiscovery.cpp
CdcUpdater.h CdcUpdater.cpp CdcUpdater.h CdcUpdater.cpp
) )

View File

@ -3,14 +3,25 @@
#include "StringUtility.h" #include "StringUtility.h"
#include <boost/scope/scope_exit.hpp> #include <boost/scope/scope_exit.hpp>
#include <cstdlib> #include <cstdlib>
#include <filesystem>
#ifdef Q_OS_WIN
#include <mfapi.h> #include <mfapi.h>
#include <mfcaptureengine.h> #include <mfcaptureengine.h>
#include <mfidl.h>
#include <mfreadwrite.h>
#else
#include <fcntl.h>
#include <linux/videodev2.h>
#include <sys/ioctl.h>
#endif
struct DeviceDiscovery::Device { struct DeviceDiscovery::Device {
Device(IMFMediaSource *source);
~Device(); ~Device();
#ifdef Q_OS_WIN
Device(IMFMediaSource *source);
IMFMediaSource *source = nullptr; IMFMediaSource *source = nullptr;
IMFSourceReader *reader = nullptr; IMFSourceReader *reader = nullptr;
#endif
}; };
template <class T> template <class T>
@ -24,6 +35,7 @@ void SafeRelease(T **ppT) {
DeviceDiscovery::DeviceDiscovery() { DeviceDiscovery::DeviceDiscovery() {
} }
#ifdef Q_OS_WIN
static std::string deviceName(IMFActivate *device) { static std::string deviceName(IMFActivate *device) {
std::string ret; std::string ret;
WCHAR *friendlyName = nullptr; WCHAR *friendlyName = nullptr;
@ -186,12 +198,36 @@ DeviceDiscovery::Device::Device(IMFMediaSource *source) : source(source) {
LOG(error) << "MFCreateSourceReaderFromMediaSource() failed, result: " << result; LOG(error) << "MFCreateSourceReaderFromMediaSource() failed, result: " << result;
} }
} }
#else
std::vector<std::string> DeviceDiscovery::devices() {
std::vector<std::string> ret;
for (const auto &entry : std::filesystem::directory_iterator("/dev")) {
if (entry.is_character_file() && entry.path().string().find("video") != std::string::npos) {
int fd = open(entry.path().c_str(), O_RDWR);
if (fd < 0) {
continue;
}
struct v4l2_capability cap;
if (ioctl(fd, VIDIOC_QUERYCAP, &cap) == 0) {
if (cap.capabilities & V4L2_CAP_VIDEO_CAPTURE) {
ret.push_back(entry.path().string());
}
}
close(fd);
}
}
return ret;
}
#endif
DeviceDiscovery::Device::~Device() { DeviceDiscovery::Device::~Device() {
#ifdef Q_OS_WIN
if (source != nullptr) { if (source != nullptr) {
source->Release(); source->Release();
} }
if (reader != nullptr) { if (reader != nullptr) {
reader->Release(); reader->Release();
} }
#endif
} }

View File

@ -2,8 +2,6 @@
#define __DEVICEDISCOVERY_H__ #define __DEVICEDISCOVERY_H__
#include <memory> #include <memory>
#include <mfidl.h>
#include <mfreadwrite.h>
#include <string> #include <string>
#include <system_error> #include <system_error>
#include <vector> #include <vector>