From 943fb6fb4b470045648017457d5ca82bac127559 Mon Sep 17 00:00:00 2001 From: amass <168062547@qq.com> Date: Thu, 21 Nov 2024 13:12:45 +0000 Subject: [PATCH] adapt for linux. --- Analyser/Application.cpp | 2 +- Peripheral/DeviceDiscovery.cpp | 53 ++++++++-------------------------- Peripheral/DeviceDiscovery.h | 3 +- UnitTest/LinuxDeviceEnums.cpp | 2 +- 4 files changed, 15 insertions(+), 45 deletions(-) diff --git a/Analyser/Application.cpp b/Analyser/Application.cpp index 1d31c25..56cfe5f 100644 --- a/Analyser/Application.cpp +++ b/Analyser/Application.cpp @@ -122,7 +122,7 @@ QStringList Application::availableSerialPorts() const { QVariantList Application::availableUsbVideoCameras() const { QVariantList ret; DeviceDiscovery d; - auto devices = d.getDevices(); + auto devices = d.devices(); for (auto &device : devices) { QVariantMap item; item.insert("name", QString::fromStdString(device.friendlyName)); diff --git a/Peripheral/DeviceDiscovery.cpp b/Peripheral/DeviceDiscovery.cpp index b169ec7..76227f4 100644 --- a/Peripheral/DeviceDiscovery.cpp +++ b/Peripheral/DeviceDiscovery.cpp @@ -147,39 +147,7 @@ void DeviceDiscovery::enterOtaMode(const std::shared_ptr &device, std::e &llTimeStamp, &pSample); } -std::vector DeviceDiscovery::devices() { - std::vector ret; - IMFAttributes *attributes = nullptr; - boost::scope::scope_exit guard([&attributes] { SafeRelease(&attributes); }); - auto result = MFCreateAttributes(&attributes, 1); - if (FAILED(result)) { - return ret; - } - - result = attributes->SetGUID(MF_DEVSOURCE_ATTRIBUTE_SOURCE_TYPE, MF_DEVSOURCE_ATTRIBUTE_SOURCE_TYPE_VIDCAP_GUID); - if (FAILED(result)) { - return ret; - } - - UINT32 count; - IMFActivate **devices = nullptr; - result = MFEnumDeviceSources(attributes, &devices, &count); - if (FAILED(result)) { - return ret; - } - - if (count == 0) { - return ret; - } - - for (int i = 0; i < count; i++) { - auto name = ::deviceName(devices[i]); - ret.push_back(name); - } - return ret; -} - -std::vector DeviceDiscovery::getDevices() { +std::vector DeviceDiscovery::devices() { std::vector devices; ICreateDevEnum *pDevEnum = nullptr; IEnumMoniker *pEnum = nullptr; @@ -396,8 +364,8 @@ DeviceDiscovery::Device::Device(IMFMediaSource *source) : source(source) { } } #else -std::vector DeviceDiscovery::devices() { - std::vector ret; +std::vector DeviceDiscovery::devices() { + std::vector 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); @@ -409,7 +377,10 @@ std::vector DeviceDiscovery::devices() { if (ioctl(fd, VIDIOC_QUERYCAP, &cap) == 0) { if ((strstr(reinterpret_cast(cap.card), DeviceName) != nullptr) && (cap.capabilities & V4L2_CAP_VIDEO_CAPTURE)) { - ret.push_back(entry.path().string()); + Device device; + device.friendlyName = entry.path().string(); + device.alternativeName = entry.path().string(); + ret.push_back(device); } } close(fd); @@ -446,7 +417,7 @@ static std::string find_video_device_by_name(const std::string &targetName) { std::shared_ptr DeviceDiscovery::find(const std::string &deviceName, std::error_code &error) { auto ret = std::make_shared(); - ret->name = find_video_device_by_name(deviceName); + ret->friendlyName = find_video_device_by_name(deviceName); return ret; } @@ -469,9 +440,9 @@ void DeviceDiscovery::enterOtaMode(const std::shared_ptr &device, std::e } else { LOG(info) << "found ota specific resolution: " << OtaSpecificWidth << "x" << otaSpecificHeight; } - int fd = open(device->name.c_str(), O_RDWR); + int fd = open(device->friendlyName.c_str(), O_RDWR); if (fd <= 0) { - LOG(error) << "Failed to open device " << device->name; + LOG(error) << "Failed to open device " << device->friendlyName; } else { struct v4l2_format format; memset(&format, 0, sizeof(format)); @@ -549,9 +520,9 @@ void DeviceDiscovery::enterOtaMode(const std::shared_ptr &device, std::e DeviceDiscovery::Resolutions DeviceDiscovery::deviceResolutions(const std::shared_ptr &source) { Resolutions ret; - int fd = open(source->name.c_str(), O_RDWR); + int fd = open(source->friendlyName.c_str(), O_RDWR); if (fd <= 0) { - LOG(error) << "Failed to open device " << source->name; + LOG(error) << "Failed to open device " << source->friendlyName; } else { struct v4l2_fmtdesc fmt; fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; diff --git a/Peripheral/DeviceDiscovery.h b/Peripheral/DeviceDiscovery.h index fde874a..b97eaee 100644 --- a/Peripheral/DeviceDiscovery.h +++ b/Peripheral/DeviceDiscovery.h @@ -36,8 +36,7 @@ public: DeviceDiscovery(); std::shared_ptr find(const std::string &deviceName, std::error_code &error); void enterOtaMode(const std::shared_ptr &device, std::error_code &error); - std::vector devices(); - std::vector getDevices(); + std::vector devices(); bool SetResolution(const std::string &deviceName, int width, int height); protected: diff --git a/UnitTest/LinuxDeviceEnums.cpp b/UnitTest/LinuxDeviceEnums.cpp index a9e6047..5913826 100644 --- a/UnitTest/LinuxDeviceEnums.cpp +++ b/UnitTest/LinuxDeviceEnums.cpp @@ -8,7 +8,7 @@ BOOST_AUTO_TEST_CASE(EnumDevice) { auto device = discovery.find("UVC Camera", error); auto devices = discovery.devices(); for (int i = 0; i < devices.size(); i++) { - LOG(info) << "device[" << i << "] " << devices.at(i); + LOG(info) << "device[" << i << "] " << devices.at(i).friendlyName; } discovery.enterOtaMode(device, error);