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

View File

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

View File

@ -3,14 +3,25 @@
#include "StringUtility.h"
#include <boost/scope/scope_exit.hpp>
#include <cstdlib>
#include <filesystem>
#ifdef Q_OS_WIN
#include <mfapi.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 {
Device(IMFMediaSource *source);
~Device();
#ifdef Q_OS_WIN
Device(IMFMediaSource *source);
IMFMediaSource *source = nullptr;
IMFSourceReader *reader = nullptr;
#endif
};
template <class T>
@ -24,6 +35,7 @@ void SafeRelease(T **ppT) {
DeviceDiscovery::DeviceDiscovery() {
}
#ifdef Q_OS_WIN
static std::string deviceName(IMFActivate *device) {
std::string ret;
WCHAR *friendlyName = nullptr;
@ -186,12 +198,36 @@ DeviceDiscovery::Device::Device(IMFMediaSource *source) : source(source) {
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() {
#ifdef Q_OS_WIN
if (source != nullptr) {
source->Release();
}
if (reader != nullptr) {
reader->Release();
}
#endif
}

View File

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