adapt for linux.
All checks were successful
Build Applications / Build (push) Successful in 3m3s
Windows CI / build (push) Successful in 3m4s

This commit is contained in:
amass 2024-11-21 13:12:45 +00:00
parent ce673bf330
commit 943fb6fb4b
4 changed files with 15 additions and 45 deletions

View File

@ -122,7 +122,7 @@ QStringList Application::availableSerialPorts() const {
QVariantList Application::availableUsbVideoCameras() const { QVariantList Application::availableUsbVideoCameras() const {
QVariantList ret; QVariantList ret;
DeviceDiscovery d; DeviceDiscovery d;
auto devices = d.getDevices(); auto devices = d.devices();
for (auto &device : devices) { for (auto &device : devices) {
QVariantMap item; QVariantMap item;
item.insert("name", QString::fromStdString(device.friendlyName)); item.insert("name", QString::fromStdString(device.friendlyName));

View File

@ -147,39 +147,7 @@ void DeviceDiscovery::enterOtaMode(const std::shared_ptr<Device> &device, std::e
&llTimeStamp, &pSample); &llTimeStamp, &pSample);
} }
std::vector<std::string> DeviceDiscovery::devices() { std::vector<DeviceDiscovery::Device> DeviceDiscovery::devices() {
std::vector<std::string> 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::Device> DeviceDiscovery::getDevices() {
std::vector<Device> devices; std::vector<Device> devices;
ICreateDevEnum *pDevEnum = nullptr; ICreateDevEnum *pDevEnum = nullptr;
IEnumMoniker *pEnum = nullptr; IEnumMoniker *pEnum = nullptr;
@ -396,8 +364,8 @@ DeviceDiscovery::Device::Device(IMFMediaSource *source) : source(source) {
} }
} }
#else #else
std::vector<std::string> DeviceDiscovery::devices() { std::vector<DeviceDiscovery::Device> DeviceDiscovery::devices() {
std::vector<std::string> ret; std::vector<Device> ret;
for (const auto &entry : std::filesystem::directory_iterator("/dev")) { for (const auto &entry : std::filesystem::directory_iterator("/dev")) {
if (entry.is_character_file() && entry.path().string().find("video") != std::string::npos) { if (entry.is_character_file() && entry.path().string().find("video") != std::string::npos) {
int fd = open(entry.path().c_str(), O_RDWR); int fd = open(entry.path().c_str(), O_RDWR);
@ -409,7 +377,10 @@ std::vector<std::string> DeviceDiscovery::devices() {
if (ioctl(fd, VIDIOC_QUERYCAP, &cap) == 0) { if (ioctl(fd, VIDIOC_QUERYCAP, &cap) == 0) {
if ((strstr(reinterpret_cast<const char *>(cap.card), DeviceName) != nullptr) && if ((strstr(reinterpret_cast<const char *>(cap.card), DeviceName) != nullptr) &&
(cap.capabilities & V4L2_CAP_VIDEO_CAPTURE)) { (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); close(fd);
@ -446,7 +417,7 @@ static std::string find_video_device_by_name(const std::string &targetName) {
std::shared_ptr<DeviceDiscovery::Device> DeviceDiscovery::find(const std::string &deviceName, std::error_code &error) { std::shared_ptr<DeviceDiscovery::Device> DeviceDiscovery::find(const std::string &deviceName, std::error_code &error) {
auto ret = std::make_shared<Device>(); auto ret = std::make_shared<Device>();
ret->name = find_video_device_by_name(deviceName); ret->friendlyName = find_video_device_by_name(deviceName);
return ret; return ret;
} }
@ -469,9 +440,9 @@ void DeviceDiscovery::enterOtaMode(const std::shared_ptr<Device> &device, std::e
} else { } else {
LOG(info) << "found ota specific resolution: " << OtaSpecificWidth << "x" << otaSpecificHeight; 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) { if (fd <= 0) {
LOG(error) << "Failed to open device " << device->name; LOG(error) << "Failed to open device " << device->friendlyName;
} else { } else {
struct v4l2_format format; struct v4l2_format format;
memset(&format, 0, sizeof(format)); memset(&format, 0, sizeof(format));
@ -549,9 +520,9 @@ void DeviceDiscovery::enterOtaMode(const std::shared_ptr<Device> &device, std::e
DeviceDiscovery::Resolutions DeviceDiscovery::deviceResolutions(const std::shared_ptr<Device> &source) { DeviceDiscovery::Resolutions DeviceDiscovery::deviceResolutions(const std::shared_ptr<Device> &source) {
Resolutions ret; Resolutions ret;
int fd = open(source->name.c_str(), O_RDWR); int fd = open(source->friendlyName.c_str(), O_RDWR);
if (fd <= 0) { if (fd <= 0) {
LOG(error) << "Failed to open device " << source->name; LOG(error) << "Failed to open device " << source->friendlyName;
} else { } else {
struct v4l2_fmtdesc fmt; struct v4l2_fmtdesc fmt;
fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;

View File

@ -36,8 +36,7 @@ public:
DeviceDiscovery(); DeviceDiscovery();
std::shared_ptr<Device> find(const std::string &deviceName, std::error_code &error); std::shared_ptr<Device> find(const std::string &deviceName, std::error_code &error);
void enterOtaMode(const std::shared_ptr<Device> &device, std::error_code &error); void enterOtaMode(const std::shared_ptr<Device> &device, std::error_code &error);
std::vector<std::string> devices(); std::vector<Device> devices();
std::vector<Device> getDevices();
bool SetResolution(const std::string &deviceName, int width, int height); bool SetResolution(const std::string &deviceName, int width, int height);
protected: protected:

View File

@ -8,7 +8,7 @@ BOOST_AUTO_TEST_CASE(EnumDevice) {
auto device = discovery.find("UVC Camera", error); auto device = discovery.find("UVC Camera", error);
auto devices = discovery.devices(); auto devices = discovery.devices();
for (int i = 0; i < devices.size(); i++) { 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); discovery.enterOtaMode(device, error);